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 int longest_section_name
= 8;
58 static lang_statement_list_type statement_list
;
59 static struct lang_phdr
*lang_phdr_list
;
61 static void print_size
PARAMS ((size_t value
));
62 static void print_alignment
PARAMS ((unsigned int value
));
63 static void print_fill
PARAMS ((fill_type value
));
64 static void print_section
PARAMS ((const char *name
));
65 static void lang_for_each_statement_worker
66 PARAMS ((void (*func
) (lang_statement_union_type
*),
67 lang_statement_union_type
*s
));
68 static lang_input_statement_type
*new_afile
69 PARAMS ((const char *name
, lang_input_file_enum_type file_type
,
70 const char *target
, boolean add_to_list
));
71 static void print_flags
PARAMS ((int *ignore_flags
));
72 static void init_os
PARAMS ((lang_output_section_statement_type
*s
));
73 static void section_already_linked
PARAMS ((bfd
*, asection
*, PTR
));
74 static void wild_section
PARAMS ((lang_wild_statement_type
*ptr
,
76 lang_input_statement_type
*file
,
77 lang_output_section_statement_type
*output
));
78 static lang_input_statement_type
*lookup_name
PARAMS ((const char *name
));
79 static void load_symbols
PARAMS ((lang_input_statement_type
*entry
,
80 lang_statement_list_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_reloc_statement
PARAMS ((lang_reloc_statement_type
*reloc
));
105 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*s
));
106 static void print_wild_statement
107 PARAMS ((lang_wild_statement_type
*w
,
108 lang_output_section_statement_type
*os
));
109 static void print_group
110 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
111 static void print_statement
PARAMS ((lang_statement_union_type
*s
,
112 lang_output_section_statement_type
*os
));
113 static void print_statement_list
PARAMS ((lang_statement_union_type
*s
,
114 lang_output_section_statement_type
*os
));
115 static void print_statements
PARAMS ((void));
116 static bfd_vma insert_pad
PARAMS ((lang_statement_union_type
**this_ptr
,
117 fill_type fill
, unsigned int power
,
118 asection
*output_section_statement
,
120 static bfd_vma size_input_section
121 PARAMS ((lang_statement_union_type
**this_ptr
,
122 lang_output_section_statement_type
*output_section_statement
,
123 fill_type fill
, bfd_vma dot
, boolean relax
));
124 static void lang_finish
PARAMS ((void));
125 static void lang_check
PARAMS ((void));
126 static void lang_common
PARAMS ((void));
127 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
128 static void lang_place_orphans
PARAMS ((void));
129 static int topower
PARAMS ((int));
130 static void lang_set_startof
PARAMS ((void));
131 static void reset_memory_regions
PARAMS ((void));
132 static void lang_record_phdrs
PARAMS ((void));
135 lang_output_section_statement_type
*abs_output_section
;
136 lang_statement_list_type
*stat_ptr
= &statement_list
;
137 lang_statement_list_type file_chain
= { 0 };
138 const char *entry_symbol
= NULL
;
139 boolean entry_from_cmdline
;
140 boolean lang_has_input_file
= false;
141 boolean had_output_filename
= false;
142 boolean lang_float_flag
= false;
143 boolean delete_output_file_on_failure
= false;
145 etree_type
*base
; /* Relocation base - or null */
148 #if defined(__STDC__) || defined(ALMOST_STDC)
149 #define cat(a,b) a##b
151 #define cat(a,b) a/**/b
154 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
156 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
158 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
164 return obstack_alloc (&stat_obstack
, size
);
171 fprintf (config
.map_file
, "%5x", (unsigned) value
);
175 print_alignment (value
)
178 fprintf (config
.map_file
, "2**%1u", value
);
185 fprintf (config
.map_file
, "%04x", (unsigned) value
);
192 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
195 /*----------------------------------------------------------------------
196 lang_for_each_statement walks the parse tree and calls the provided
197 function for each node
201 lang_for_each_statement_worker (func
, s
)
202 void (*func
) PARAMS ((lang_statement_union_type
*));
203 lang_statement_union_type
*s
;
205 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
209 switch (s
->header
.type
)
211 case lang_constructors_statement_enum
:
212 lang_for_each_statement_worker (func
, constructor_list
.head
);
214 case lang_output_section_statement_enum
:
215 lang_for_each_statement_worker
217 s
->output_section_statement
.children
.head
);
219 case lang_wild_statement_enum
:
220 lang_for_each_statement_worker
222 s
->wild_statement
.children
.head
);
224 case lang_group_statement_enum
:
225 lang_for_each_statement_worker (func
,
226 s
->group_statement
.children
.head
);
228 case lang_data_statement_enum
:
229 case lang_reloc_statement_enum
:
230 case lang_object_symbols_statement_enum
:
231 case lang_output_statement_enum
:
232 case lang_target_statement_enum
:
233 case lang_input_section_enum
:
234 case lang_input_statement_enum
:
235 case lang_assignment_statement_enum
:
236 case lang_padding_statement_enum
:
237 case lang_address_statement_enum
:
247 lang_for_each_statement (func
)
248 void (*func
) PARAMS ((lang_statement_union_type
*));
250 lang_for_each_statement_worker (func
,
251 statement_list
.head
);
254 /*----------------------------------------------------------------------*/
256 lang_list_init (list
)
257 lang_statement_list_type
*list
;
259 list
->head
= (lang_statement_union_type
*) NULL
;
260 list
->tail
= &list
->head
;
263 /*----------------------------------------------------------------------
265 build a new statement node for the parse tree
270 lang_statement_union_type
*
271 new_statement (type
, size
, list
)
272 enum statement_enum type
;
274 lang_statement_list_type
* list
;
276 lang_statement_union_type
*new = (lang_statement_union_type
*)
279 new->header
.type
= type
;
280 new->header
.next
= (lang_statement_union_type
*) NULL
;
281 lang_statement_append (list
, new, &new->header
.next
);
286 Build a new input file node for the language. There are several ways
287 in which we treat an input file, eg, we only look at symbols, or
288 prefix it with a -l etc.
290 We can be supplied with requests for input files more than once;
291 they may, for example be split over serveral lines like foo.o(.text)
292 foo.o(.data) etc, so when asked for a file we check that we havn't
293 got it already so we don't duplicate the bfd.
296 static lang_input_statement_type
*
297 new_afile (name
, file_type
, target
, add_to_list
)
299 lang_input_file_enum_type file_type
;
303 lang_input_statement_type
*p
;
306 p
= new_stat (lang_input_statement
, stat_ptr
);
309 p
= ((lang_input_statement_type
*)
310 stat_alloc (sizeof (lang_input_statement_type
)));
311 p
->header
.next
= NULL
;
314 lang_has_input_file
= true;
316 p
->complained
= false;
319 case lang_input_file_is_symbols_only_enum
:
321 p
->is_archive
= false;
323 p
->local_sym_name
= name
;
324 p
->just_syms_flag
= true;
325 p
->search_dirs_flag
= false;
327 case lang_input_file_is_fake_enum
:
329 p
->is_archive
= false;
331 p
->local_sym_name
= name
;
332 p
->just_syms_flag
= false;
333 p
->search_dirs_flag
= false;
335 case lang_input_file_is_l_enum
:
336 p
->is_archive
= true;
339 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
340 p
->just_syms_flag
= false;
341 p
->search_dirs_flag
= true;
343 case lang_input_file_is_marker_enum
:
345 p
->is_archive
= false;
347 p
->local_sym_name
= name
;
348 p
->just_syms_flag
= false;
349 p
->search_dirs_flag
= true;
351 case lang_input_file_is_search_file_enum
:
353 p
->is_archive
= false;
355 p
->local_sym_name
= name
;
356 p
->just_syms_flag
= false;
357 p
->search_dirs_flag
= true;
359 case lang_input_file_is_file_enum
:
361 p
->is_archive
= false;
363 p
->local_sym_name
= name
;
364 p
->just_syms_flag
= false;
365 p
->search_dirs_flag
= false;
370 p
->the_bfd
= (bfd
*) NULL
;
371 p
->asymbols
= (asymbol
**) NULL
;
372 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
373 p
->next
= (lang_statement_union_type
*) NULL
;
375 p
->common_output_section
= (asection
*) NULL
;
376 p
->dynamic
= config
.dynamic_link
;
377 p
->whole_archive
= whole_archive
;
379 lang_statement_append (&input_file_chain
,
380 (lang_statement_union_type
*) p
,
385 lang_input_statement_type
*
386 lang_add_input_file (name
, file_type
, target
)
388 lang_input_file_enum_type file_type
;
391 lang_has_input_file
= true;
392 return new_afile (name
, file_type
, target
, true);
395 /* Build enough state so that the parser can build its tree */
399 obstack_begin (&stat_obstack
, 1000);
401 stat_ptr
= &statement_list
;
403 lang_list_init (stat_ptr
);
405 lang_list_init (&input_file_chain
);
406 lang_list_init (&lang_output_section_statement
);
407 lang_list_init (&file_chain
);
408 first_file
= lang_add_input_file ((char *) NULL
,
409 lang_input_file_is_marker_enum
,
411 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
413 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
417 /*----------------------------------------------------------------------
418 A region is an area of memory declared with the
419 MEMORY { name:org=exp, len=exp ... }
422 We maintain a list of all the regions here
424 If no regions are specified in the script, then the default is used
425 which is created when looked up to be the entire data space
428 static lang_memory_region_type
*lang_memory_region_list
;
429 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
431 lang_memory_region_type
*
432 lang_memory_region_lookup (name
)
433 CONST
char *CONST name
;
436 lang_memory_region_type
*p
= lang_memory_region_list
;
438 for (p
= lang_memory_region_list
;
439 p
!= (lang_memory_region_type
*) NULL
;
442 if (strcmp (p
->name
, name
) == 0)
449 /* This code used to always use the first region in the list as the
450 default region. I changed it to instead use a region
451 encompassing all of memory as the default region. This permits
452 NOLOAD sections to work reasonably without requiring a region.
453 People should specify what region they mean, if they really want
455 if (strcmp (name
, "*default*") == 0)
457 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
459 return lang_memory_region_list
;
465 lang_memory_region_type
*new =
466 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
468 new->name
= buystring (name
);
469 new->next
= (lang_memory_region_type
*) NULL
;
471 *lang_memory_region_list_tail
= new;
472 lang_memory_region_list_tail
= &new->next
;
474 new->length
= ~(bfd_size_type
)0;
476 new->had_full_message
= false;
483 lang_output_section_statement_type
*
484 lang_output_section_find (name
)
485 CONST
char *CONST name
;
487 lang_statement_union_type
*u
;
488 lang_output_section_statement_type
*lookup
;
490 for (u
= lang_output_section_statement
.head
;
491 u
!= (lang_statement_union_type
*) NULL
;
494 lookup
= &u
->output_section_statement
;
495 if (strcmp (name
, lookup
->name
) == 0)
500 return (lang_output_section_statement_type
*) NULL
;
503 lang_output_section_statement_type
*
504 lang_output_section_statement_lookup (name
)
505 CONST
char *CONST name
;
507 lang_output_section_statement_type
*lookup
;
509 lookup
= lang_output_section_find (name
);
510 if (lookup
== (lang_output_section_statement_type
*) NULL
)
513 lookup
= (lang_output_section_statement_type
*)
514 new_stat (lang_output_section_statement
, stat_ptr
);
515 lookup
->region
= (lang_memory_region_type
*) NULL
;
517 lookup
->block_value
= 1;
520 lookup
->next
= (lang_statement_union_type
*) NULL
;
521 lookup
->bfd_section
= (asection
*) NULL
;
522 lookup
->processed
= false;
523 lookup
->loadable
= 1;
524 lookup
->addr_tree
= (etree_type
*) NULL
;
525 lang_list_init (&lookup
->children
);
527 lookup
->memspec
= (CONST
char *) NULL
;
529 lookup
->subsection_alignment
= -1;
530 lookup
->section_alignment
= -1;
531 lookup
->load_base
= (union etree_union
*) NULL
;
532 lookup
->phdrs
= NULL
;
534 lang_statement_append (&lang_output_section_statement
,
535 (lang_statement_union_type
*) lookup
,
543 print_flags (ignore_flags
)
546 fprintf (config
.map_file
, "(");
548 if (flags
->flag_read
)
549 fprintf (outfile
, "R");
550 if (flags
->flag_write
)
551 fprintf (outfile
, "W");
552 if (flags
->flag_executable
)
553 fprintf (outfile
, "X");
554 if (flags
->flag_loadable
)
555 fprintf (outfile
, "L");
557 fprintf (config
.map_file
, ")");
563 lang_memory_region_type
*m
;
565 fprintf (config
.map_file
, "**MEMORY CONFIGURATION**\n\n");
567 fprintf (config
.map_file
, "name\t\torigin\t\tlength\t\tattributes\n");
569 fprintf (config
.map_file
,
570 "name\t\torigin length r_size c_size is attributes\n");
573 for (m
= lang_memory_region_list
;
574 m
!= (lang_memory_region_type
*) NULL
;
577 fprintf (config
.map_file
, "%-16s", m
->name
);
578 print_address (m
->origin
);
580 print_address ((bfd_vma
)m
->length
);
582 print_address ((bfd_vma
)m
->old_length
);
584 print_address (m
->current
- m
->origin
);
587 fprintf (config
.map_file
, " %2d%% ",
588 (int) ((m
->current
- m
->origin
) * 100 / m
->old_length
));
589 print_flags (&m
->flags
);
590 fprintf (config
.map_file
, "\n");
592 fprintf (config
.map_file
, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
593 fprintf (config
.map_file
, "output input virtual\n");
594 fprintf (config
.map_file
, "section section address tsize\n\n");
605 lang_output_section_statement_type
* s
;
607 section_userdata_type
*new;
609 if (strcmp (s
->name
, DISCARD_SECTION_NAME
) == 0)
610 einfo ("%P%F: Illegal use of `%s' section", DISCARD_SECTION_NAME
);
612 new = ((section_userdata_type
*)
613 stat_alloc (sizeof (section_userdata_type
)));
615 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
616 if (s
->bfd_section
== (asection
*) NULL
)
617 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
618 if (s
->bfd_section
== (asection
*) NULL
)
620 einfo ("%P%F: output format %s cannot represent section called %s\n",
621 output_bfd
->xvec
->name
, s
->name
);
623 s
->bfd_section
->output_section
= s
->bfd_section
;
625 /* We initialize an output sections output offset to minus its own */
626 /* vma to allow us to output a section through itself */
627 s
->bfd_section
->output_offset
= 0;
628 get_userdata (s
->bfd_section
) = (PTR
) new;
631 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
632 once into the output. This routine checks each sections, and
633 arranges to discard it if a section of the same name has already
634 been linked. This code assumes that all relevant sections have the
635 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
636 section name. This is called via bfd_map_over_sections. */
640 section_already_linked (abfd
, sec
, ignore
)
647 struct sec_link_once
*next
;
650 static struct sec_link_once
*sec_link_once_list
;
653 struct sec_link_once
*l
;
655 flags
= bfd_get_section_flags (abfd
, sec
);
657 if ((flags
& SEC_LINK_ONCE
) == 0)
660 name
= bfd_get_section_name (abfd
, sec
);
662 for (l
= sec_link_once_list
; l
!= NULL
; l
= l
->next
)
664 if (strcmp (name
, bfd_get_section_name (l
->sec
->owner
, l
->sec
)) == 0)
666 /* The section has already been linked. See if we should
668 switch (flags
& SEC_LINK_DUPLICATES
)
673 case SEC_LINK_DUPLICATES_DISCARD
:
676 case SEC_LINK_DUPLICATES_ONE_ONLY
:
677 einfo ("%P: %B: warning: ignoring duplicate section `%s'",
681 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
682 /* FIXME: We should really dig out the contents of both
683 sections and memcmp them. The COFF/PE spec says that
684 the Microsoft linker does not implement this
685 correctly, so I'm not going to bother doing it
688 case SEC_LINK_DUPLICATES_SAME_SIZE
:
689 if (bfd_section_size (abfd
, sec
)
690 != bfd_section_size (l
->sec
->owner
, l
->sec
))
691 einfo ("%P: %B: warning: duplicate section `%s' has different size",
696 /* Set the output_section field so that wild_doit does not
697 create a lang_input_section structure for this section. */
698 sec
->output_section
= bfd_abs_section_ptr
;
704 /* This is the first section with this name. Record it. */
706 l
= (struct sec_link_once
*) xmalloc (sizeof *l
);
708 l
->next
= sec_link_once_list
;
709 sec_link_once_list
= l
;
712 /* The wild routines.
714 These expand statements like *(.text) and foo.o to a list of
715 explicit actions, like foo.o(.text), bar.o(.text) and
716 foo.o(.text, .data). */
718 /* Add SECTION to the output section OUTPUT. Do this by creating a
719 lang_input_section statement which is placed at PTR. FILE is the
720 input file which holds SECTION. */
723 wild_doit (ptr
, section
, output
, file
)
724 lang_statement_list_type
*ptr
;
726 lang_output_section_statement_type
*output
;
727 lang_input_statement_type
*file
;
732 flags
= bfd_get_section_flags (section
->owner
, section
);
736 /* If we are doing a final link, discard sections marked with
738 if (! link_info
.relocateable
739 && (flags
& SEC_EXCLUDE
) != 0)
742 /* Discard input sections which are assigned to a section named
743 DISCARD_SECTION_NAME. */
744 if (strcmp (output
->name
, DISCARD_SECTION_NAME
) == 0)
747 /* Discard debugging sections if we are stripping debugging
749 if ((link_info
.strip
== strip_debugger
|| link_info
.strip
== strip_all
)
750 && (flags
& SEC_DEBUGGING
) != 0)
755 if (section
->output_section
== NULL
)
757 /* This prevents future calls from assigning this section. */
758 section
->output_section
= bfd_abs_section_ptr
;
763 if (section
->output_section
== NULL
)
765 lang_input_section_type
*new;
767 if (output
->bfd_section
== NULL
)
770 /* Add a section reference to the list */
771 new = new_stat (lang_input_section
, ptr
);
773 new->section
= section
;
775 section
->output_section
= output
->bfd_section
;
777 /* We don't copy the SEC_NEVER_LOAD flag from an input section
778 to an output section, because we want to be able to include a
779 SEC_NEVER_LOAD section in the middle of an otherwise loaded
780 section (I don't know why we want to do this, but we do).
781 build_link_order in ldwrite.c handles this case by turning
782 the embedded SEC_NEVER_LOAD section into a fill. */
783 section
->output_section
->flags
|=
784 section
->flags
& (flagword
) (~ SEC_NEVER_LOAD
);
786 if (! output
->loadable
)
788 /* Turn off load flag */
789 output
->bfd_section
->flags
&= ~SEC_LOAD
;
790 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
793 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
794 output
->bfd_section
->alignment_power
= section
->alignment_power
;
796 /* If supplied an aligment, then force it. */
797 if (output
->section_alignment
!= -1)
798 output
->bfd_section
->alignment_power
= output
->section_alignment
;
802 /* Expand a wild statement for a particular FILE. SECTION may be
803 NULL, in which case it is a wild card. */
806 wild_section (ptr
, section
, file
, output
)
807 lang_wild_statement_type
*ptr
;
809 lang_input_statement_type
*file
;
810 lang_output_section_statement_type
*output
;
812 if (file
->just_syms_flag
== false)
814 register asection
*s
;
816 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
818 /* Attach all sections named SECTION. If SECTION is NULL,
819 then attach all sections.
821 Previously, if SECTION was NULL, this code did not call
822 wild_doit if the SEC_IS_COMMON flag was set for the
823 section. I did not understand that, and I took it out.
827 || strcmp (bfd_get_section_name (file
->the_bfd
, s
),
829 wild_doit (&ptr
->children
, s
, output
, file
);
834 /* This is passed a file name which must have been seen already and
835 added to the statement tree. We will see if it has been opened
836 already and had its symbols read. If not then we'll read it. */
838 static lang_input_statement_type
*
842 lang_input_statement_type
*search
;
844 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
845 search
!= (lang_input_statement_type
*) NULL
;
846 search
= (lang_input_statement_type
*) search
->next_real_file
)
848 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
850 if (search
->filename
!= (char *) NULL
851 && name
!= (char *) NULL
852 && strcmp (search
->filename
, name
) == 0)
856 if (search
== (lang_input_statement_type
*) NULL
)
857 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
860 /* If we have already added this file, or this file is not real
861 (FIXME: can that ever actually happen?) or the name is NULL
862 (FIXME: can that ever actually happen?) don't add this file. */
865 || search
->filename
== (const char *) NULL
)
868 load_symbols (search
, (lang_statement_list_type
*) NULL
);
873 /* Get the symbols for an input file. */
876 load_symbols (entry
, place
)
877 lang_input_statement_type
*entry
;
878 lang_statement_list_type
*place
;
885 ldfile_open_file (entry
);
887 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
888 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
891 lang_statement_list_type
*hold
;
893 err
= bfd_get_error ();
894 if (err
== bfd_error_file_ambiguously_recognized
)
898 einfo ("%B: file not recognized: %E\n", entry
->the_bfd
);
899 einfo ("%B: matching formats:", entry
->the_bfd
);
900 for (p
= matching
; *p
!= NULL
; p
++)
904 else if (err
!= bfd_error_file_not_recognized
906 einfo ("%F%B: file not recognized: %E\n", entry
->the_bfd
);
908 bfd_close (entry
->the_bfd
);
909 entry
->the_bfd
= NULL
;
911 /* See if the emulation has some special knowledge. */
913 if (ldemul_unrecognized_file (entry
))
916 /* Try to interpret the file as a linker script. */
918 ldfile_open_command_file (entry
->filename
);
923 ldfile_assumed_script
= true;
924 parser_input
= input_script
;
926 ldfile_assumed_script
= false;
933 /* We don't call ldlang_add_file for an archive. Instead, the
934 add_symbols entry point will call ldlang_add_file, via the
935 add_archive_element callback, for each element of the archive
937 switch (bfd_get_format (entry
->the_bfd
))
943 ldlang_add_file (entry
);
944 if (trace_files
|| trace_file_tries
)
945 info_msg ("%I\n", entry
);
949 if (entry
->whole_archive
)
951 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
953 while (member
!= NULL
)
955 if (! bfd_check_format (member
, bfd_object
))
956 einfo ("%F%B: object %B in archive is not object\n",
957 entry
->the_bfd
, member
);
958 if (! ((*link_info
.callbacks
->add_archive_element
)
959 (&link_info
, member
, "-whole-archive")))
961 if (! bfd_link_add_symbols (member
, &link_info
))
962 einfo ("%F%B: could not read symbols: %E\n", member
);
963 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
967 entry
->loaded
= true;
973 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
974 einfo ("%F%B: could not read symbols: %E\n", entry
->the_bfd
);
976 entry
->loaded
= true;
979 /* Handle a wild statement. SECTION or FILE or both may be NULL,
980 indicating that it is a wildcard. Separate lang_input_section
981 statements are created for each part of the expansion; they are
982 added after the wild statement S. OUTPUT is the output section. */
985 wild (s
, section
, file
, target
, output
)
986 lang_wild_statement_type
*s
;
990 lang_output_section_statement_type
*output
;
992 lang_input_statement_type
*f
;
994 if (file
== (char *) NULL
)
996 /* Perform the iteration over all files in the list */
997 for (f
= (lang_input_statement_type
*) file_chain
.head
;
998 f
!= (lang_input_statement_type
*) NULL
;
999 f
= (lang_input_statement_type
*) f
->next
)
1001 wild_section (s
, section
, f
, output
);
1006 /* Perform the iteration over a single file */
1007 f
= lookup_name (file
);
1008 if (f
->the_bfd
== NULL
1009 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
1010 wild_section (s
, section
, f
, output
);
1015 /* This is an archive file. We must map each member of the
1016 archive separately. */
1017 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
1018 while (member
!= NULL
)
1020 /* When lookup_name is called, it will call the
1021 add_symbols entry point for the archive. For each
1022 element of the archive which is included, BFD will
1023 call ldlang_add_file, which will set the usrdata
1024 field of the member to the lang_input_statement. */
1025 if (member
->usrdata
!= NULL
)
1027 wild_section (s
, section
,
1028 (lang_input_statement_type
*) member
->usrdata
,
1032 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
1037 if (section
!= (char *) NULL
1038 && strcmp (section
, "COMMON") == 0
1039 && default_common_section
== NULL
)
1041 /* Remember the section that common is going to in case we later
1042 get something which doesn't know where to put it. */
1043 default_common_section
= output
;
1047 /* Open the output file. */
1055 if (output_target
== (char *) NULL
)
1057 if (current_target
!= (char *) NULL
)
1058 output_target
= current_target
;
1060 output_target
= default_target
;
1062 output
= bfd_openw (name
, output_target
);
1064 if (output
== (bfd
*) NULL
)
1066 if (bfd_get_error () == bfd_error_invalid_target
)
1068 einfo ("%P%F: target %s not found\n", output_target
);
1070 einfo ("%P%F: cannot open output file %s: %E\n", name
);
1073 delete_output_file_on_failure
= 1;
1075 /* output->flags |= D_PAGED;*/
1077 if (! bfd_set_format (output
, bfd_object
))
1078 einfo ("%P%F:%s: can not make object file: %E\n", name
);
1079 if (! bfd_set_arch_mach (output
,
1080 ldfile_output_architecture
,
1081 ldfile_output_machine
))
1082 einfo ("%P%F:%s: can not set architecture: %E\n", name
);
1084 link_info
.hash
= bfd_link_hash_table_create (output
);
1085 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
1086 einfo ("%P%F: can not create link hash table: %E\n");
1088 bfd_set_gp_size (output
, g_switch_value
);
1096 ldlang_open_output (statement
)
1097 lang_statement_union_type
* statement
;
1099 switch (statement
->header
.type
)
1101 case lang_output_statement_enum
:
1102 ASSERT (output_bfd
== (bfd
*) NULL
);
1103 output_bfd
= open_output (statement
->output_statement
.name
);
1104 ldemul_set_output_arch ();
1105 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
1106 output_bfd
->flags
|= D_PAGED
;
1108 output_bfd
->flags
&= ~D_PAGED
;
1109 if (config
.text_read_only
)
1110 output_bfd
->flags
|= WP_TEXT
;
1112 output_bfd
->flags
&= ~WP_TEXT
;
1113 if (config
.traditional_format
)
1114 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1116 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
1119 case lang_target_statement_enum
:
1120 current_target
= statement
->target_statement
.target
;
1127 /* Open all the input files. */
1130 open_input_bfds (s
, force
)
1131 lang_statement_union_type
*s
;
1134 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1136 switch (s
->header
.type
)
1138 case lang_constructors_statement_enum
:
1139 open_input_bfds (constructor_list
.head
, force
);
1141 case lang_output_section_statement_enum
:
1142 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1144 case lang_wild_statement_enum
:
1145 /* Maybe we should load the file's symbols */
1146 if (s
->wild_statement
.filename
)
1147 (void) lookup_name (s
->wild_statement
.filename
);
1148 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1150 case lang_group_statement_enum
:
1152 struct bfd_link_hash_entry
*undefs
;
1154 /* We must continually search the entries in the group
1155 until no new symbols are added to the list of undefined
1160 undefs
= link_info
.hash
->undefs_tail
;
1161 open_input_bfds (s
->group_statement
.children
.head
, true);
1163 while (undefs
!= link_info
.hash
->undefs_tail
);
1166 case lang_target_statement_enum
:
1167 current_target
= s
->target_statement
.target
;
1169 case lang_input_statement_enum
:
1170 if (s
->input_statement
.real
== true)
1172 lang_statement_list_type add
;
1174 s
->input_statement
.target
= current_target
;
1176 /* If we are being called from within a group, and this
1177 is an archive which has already been searched, then
1178 force it to be researched. */
1180 && s
->input_statement
.loaded
1181 && bfd_check_format (s
->input_statement
.the_bfd
,
1183 s
->input_statement
.loaded
= false;
1185 lang_list_init (&add
);
1187 load_symbols (&s
->input_statement
, &add
);
1189 if (add
.head
!= NULL
)
1191 *add
.tail
= s
->next
;
1202 /* If there are [COMMONS] statements, put a wild one into the bss section */
1205 lang_reasonable_defaults ()
1208 lang_output_section_statement_lookup (".text");
1209 lang_output_section_statement_lookup (".data");
1211 default_common_section
=
1212 lang_output_section_statement_lookup (".bss");
1215 if (placed_commons
== false)
1217 lang_wild_statement_type
*new =
1218 new_stat (lang_wild_statement
,
1219 &default_common_section
->children
);
1221 new->section_name
= "COMMON";
1222 new->filename
= (char *) NULL
;
1223 lang_list_init (&new->children
);
1230 Add the supplied name to the symbol table as an undefined reference.
1231 Remove items from the chain as we open input bfds
1233 typedef struct ldlang_undef_chain_list
1235 struct ldlang_undef_chain_list
*next
;
1237 } ldlang_undef_chain_list_type
;
1239 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1242 ldlang_add_undef (name
)
1243 CONST
char *CONST name
;
1245 ldlang_undef_chain_list_type
*new =
1246 ((ldlang_undef_chain_list_type
*)
1247 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1249 new->next
= ldlang_undef_chain_list_head
;
1250 ldlang_undef_chain_list_head
= new;
1252 new->name
= buystring (name
);
1255 /* Run through the list of undefineds created above and place them
1256 into the linker hash table as undefined symbols belonging to the
1260 lang_place_undefineds ()
1262 ldlang_undef_chain_list_type
*ptr
;
1264 for (ptr
= ldlang_undef_chain_list_head
;
1265 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1268 struct bfd_link_hash_entry
*h
;
1270 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1271 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1272 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1273 if (h
->type
== bfd_link_hash_new
)
1275 h
->type
= bfd_link_hash_undefined
;
1276 h
->u
.undef
.abfd
= NULL
;
1277 bfd_link_add_undef (link_info
.hash
, h
);
1282 /* Open input files and attatch to output sections */
1284 map_input_to_output_sections (s
, target
, output_section_statement
)
1285 lang_statement_union_type
* s
;
1287 lang_output_section_statement_type
* output_section_statement
;
1289 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1291 switch (s
->header
.type
)
1295 case lang_wild_statement_enum
:
1296 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1297 s
->wild_statement
.filename
, target
,
1298 output_section_statement
);
1301 case lang_constructors_statement_enum
:
1302 map_input_to_output_sections (constructor_list
.head
,
1304 output_section_statement
);
1306 case lang_output_section_statement_enum
:
1307 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1309 &s
->output_section_statement
);
1311 case lang_output_statement_enum
:
1313 case lang_target_statement_enum
:
1314 target
= s
->target_statement
.target
;
1316 case lang_group_statement_enum
:
1317 map_input_to_output_sections (s
->group_statement
.children
.head
,
1319 output_section_statement
);
1321 case lang_fill_statement_enum
:
1322 case lang_input_section_enum
:
1323 case lang_object_symbols_statement_enum
:
1324 case lang_data_statement_enum
:
1325 case lang_reloc_statement_enum
:
1326 case lang_assignment_statement_enum
:
1327 case lang_padding_statement_enum
:
1328 case lang_input_statement_enum
:
1329 if (output_section_statement
!= NULL
1330 && output_section_statement
->bfd_section
== NULL
)
1331 init_os (output_section_statement
);
1333 case lang_afile_asection_pair_statement_enum
:
1336 case lang_address_statement_enum
:
1337 /* Mark the specified section with the supplied address */
1339 lang_output_section_statement_type
*os
=
1340 lang_output_section_statement_lookup
1341 (s
->address_statement
.section_name
);
1343 if (os
->bfd_section
== NULL
)
1345 os
->addr_tree
= s
->address_statement
.address
;
1353 print_output_section_statement (output_section_statement
)
1354 lang_output_section_statement_type
* output_section_statement
;
1356 asection
*section
= output_section_statement
->bfd_section
;
1359 print_section (output_section_statement
->name
);
1364 print_dot
= section
->vma
;
1368 print_address (section
->vma
);
1370 print_size (section
->_raw_size
);
1372 print_size(section
->_cooked_size
);
1374 print_alignment (section
->alignment_power
);
1377 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1378 print_flags (stdout
, &output_section_statement
->flags
);
1380 if (section
->flags
& SEC_LOAD
)
1381 fprintf (config
.map_file
, "load ");
1382 if (section
->flags
& SEC_ALLOC
)
1383 fprintf (config
.map_file
, "alloc ");
1384 if (section
->flags
& SEC_RELOC
)
1385 fprintf (config
.map_file
, "reloc ");
1386 if (section
->flags
& SEC_HAS_CONTENTS
)
1387 fprintf (config
.map_file
, "contents ");
1392 fprintf (config
.map_file
, " (no attached output section)");
1395 if (output_section_statement
->load_base
)
1397 bfd_vma b
= exp_get_abs_int(output_section_statement
->load_base
,
1398 0, "output base", lang_final_phase_enum
);
1399 fprintf (config
.map_file
, "Output address ");
1400 fprintf_vma (config
.map_file
, b
);
1401 fprintf (config
.map_file
, "\n");
1403 if (output_section_statement
->section_alignment
>= 0
1404 || output_section_statement
->subsection_alignment
>= 0)
1406 fprintf (config
.map_file
, "\t\t\t\t\tforced alignment ");
1407 if (output_section_statement
->section_alignment
>= 0)
1409 fprintf (config
.map_file
, "section 2**%d ",output_section_statement
->section_alignment
);
1411 if ( output_section_statement
->subsection_alignment
>= 0)
1413 fprintf (config
.map_file
, "subsection 2**%d ",output_section_statement
->subsection_alignment
);
1418 print_statement_list (output_section_statement
->children
.head
,
1419 output_section_statement
);
1424 print_assignment (assignment
, output_section
)
1425 lang_assignment_statement_type
* assignment
;
1426 lang_output_section_statement_type
* output_section
;
1428 etree_value_type result
;
1434 print_address (print_dot
);
1436 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1438 lang_final_phase_enum
,
1444 print_address (result
.value
);
1448 fprintf (config
.map_file
, "*undefined*");
1451 exp_print_tree (assignment
->exp
);
1453 fprintf (config
.map_file
, "\n");
1457 print_input_statement (statm
)
1458 lang_input_statement_type
* statm
;
1460 if (statm
->filename
!= (char *) NULL
)
1462 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1466 /* Print all the defined symbols for the abfd provided by in the supplied
1471 print_one_symbol (hash_entry
, ptr
)
1472 struct bfd_link_hash_entry
*hash_entry
;
1475 asection
* sec
= (asection
*)ptr
;
1477 if (hash_entry
->type
== bfd_link_hash_defined
1478 || hash_entry
->type
== bfd_link_hash_defweak
)
1480 if (sec
== hash_entry
->u
.def
.section
) {
1482 fprintf (config
.map_file
, " ");
1484 fprintf (config
.map_file
, " ");
1485 print_address (hash_entry
->u
.def
.value
+ outside_section_address (sec
));
1486 fprintf (config
.map_file
, " %s", hash_entry
->root
.string
);
1495 print_input_section (in
)
1496 lang_input_section_type
* in
;
1498 asection
*i
= in
->section
;
1499 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
1504 fprintf (config
.map_file
, " ");
1505 print_section (i
->name
);
1506 fprintf (config
.map_file
, " ");
1507 if (i
->output_section
)
1509 print_address (i
->output_section
->vma
+ i
->output_offset
);
1510 fprintf (config
.map_file
, " ");
1511 print_size (i
->_raw_size
);
1512 fprintf (config
.map_file
, " ");
1513 print_size(i
->_cooked_size
);
1514 fprintf (config
.map_file
, " ");
1515 print_alignment (i
->alignment_power
);
1516 fprintf (config
.map_file
, " ");
1520 bfd
*abfd
= in
->ifile
->the_bfd
;
1522 if (in
->ifile
->just_syms_flag
== true)
1524 fprintf (config
.map_file
, "symbols only ");
1527 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1528 if (abfd
->my_archive
!= (bfd
*) NULL
)
1530 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1535 fprintf (config
.map_file
, "%s", abfd
->filename
);
1537 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1540 /* Print all the symbols */
1541 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
1549 print_dot
= outside_section_address (i
) + size
;
1553 fprintf (config
.map_file
, "No output section allocated\n");
1559 print_fill_statement (fill
)
1560 lang_fill_statement_type
* fill
;
1562 fprintf (config
.map_file
, "FILL mask ");
1563 print_fill (fill
->fill
);
1567 print_data_statement (data
)
1568 lang_data_statement_type
* data
;
1570 /* bfd_vma value; */
1575 /* ASSERT(print_dot == data->output_vma);*/
1577 /* data->output_section may be NULL if called from gdb. */
1578 if (data
->output_section
)
1579 print_address (data
->output_vma
+ data
->output_section
->vma
);
1581 print_address (data
->output_vma
);
1583 print_address (data
->value
);
1588 fprintf (config
.map_file
, "BYTE ");
1589 print_dot
+= BYTE_SIZE
;
1592 fprintf (config
.map_file
, "SHORT ");
1593 print_dot
+= SHORT_SIZE
;
1596 fprintf (config
.map_file
, "LONG ");
1597 print_dot
+= LONG_SIZE
;
1600 fprintf (config
.map_file
, "QUAD ");
1601 print_dot
+= QUAD_SIZE
;
1605 exp_print_tree (data
->exp
);
1607 fprintf (config
.map_file
, "\n");
1610 /* Print a reloc statement. */
1613 print_reloc_statement (reloc
)
1614 lang_reloc_statement_type
*reloc
;
1621 /* ASSERT(print_dot == data->output_vma);*/
1623 /* reloc->output_section may be NULL if called from gdb. */
1624 if (reloc
->output_section
)
1625 print_address (reloc
->output_vma
+ reloc
->output_section
->vma
);
1627 print_address (reloc
->output_vma
);
1629 print_address (reloc
->addend_value
);
1632 fprintf (config
.map_file
, "RELOC %s ", reloc
->howto
->name
);
1634 print_dot
+= bfd_get_reloc_size (reloc
->howto
);
1636 exp_print_tree (reloc
->addend_exp
);
1638 fprintf (config
.map_file
, "\n");
1642 print_padding_statement (s
)
1643 lang_padding_statement_type
* s
;
1647 print_section ("*fill*");
1649 /* s->output_section may be NULL if called from gdb. */
1650 if (s
->output_section
)
1651 print_address (s
->output_offset
+ s
->output_section
->vma
);
1653 print_address (s
->output_offset
);
1655 print_size (s
->size
);
1657 print_fill (s
->fill
);
1660 print_dot
= s
->output_offset
+ s
->size
;
1661 /* s->output_section may be NULL if called from gdb. */
1662 if (s
->output_section
)
1663 print_dot
+= s
->output_section
->vma
;
1667 print_wild_statement (w
, os
)
1668 lang_wild_statement_type
* w
;
1669 lang_output_section_statement_type
* os
;
1671 fprintf (config
.map_file
, " from ");
1672 if (w
->filename
!= (char *) NULL
)
1674 fprintf (config
.map_file
, "%s", w
->filename
);
1678 fprintf (config
.map_file
, "*");
1680 if (w
->section_name
!= (char *) NULL
)
1682 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1686 fprintf (config
.map_file
, "(*)");
1689 print_statement_list (w
->children
.head
, os
);
1693 /* Print a group statement. */
1697 lang_group_statement_type
*s
;
1698 lang_output_section_statement_type
*os
;
1700 fprintf (config
.map_file
, "START GROUP\n");
1701 print_statement_list (s
->children
.head
, os
);
1702 fprintf (config
.map_file
, "END GROUP\n");
1705 /* Print the list of statements in S.
1706 This can be called for any statement type. */
1709 print_statement_list (s
, os
)
1710 lang_statement_union_type
* s
;
1711 lang_output_section_statement_type
* os
;
1715 print_statement (s
, os
);
1720 /* Print the first statement in statement list S.
1721 This can be called for any statement type. */
1724 print_statement (s
, os
)
1725 lang_statement_union_type
* s
;
1726 lang_output_section_statement_type
* os
;
1728 switch (s
->header
.type
)
1730 case lang_constructors_statement_enum
:
1731 fprintf (config
.map_file
, "constructors:\n");
1732 print_statement_list (constructor_list
.head
, os
);
1734 case lang_wild_statement_enum
:
1735 print_wild_statement (&s
->wild_statement
, os
);
1738 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1741 case lang_address_statement_enum
:
1742 fprintf (config
.map_file
, "address\n");
1744 case lang_object_symbols_statement_enum
:
1745 fprintf (config
.map_file
, "object symbols\n");
1747 case lang_fill_statement_enum
:
1748 print_fill_statement (&s
->fill_statement
);
1750 case lang_data_statement_enum
:
1751 print_data_statement (&s
->data_statement
);
1753 case lang_reloc_statement_enum
:
1754 print_reloc_statement (&s
->reloc_statement
);
1756 case lang_input_section_enum
:
1757 print_input_section (&s
->input_section
);
1759 case lang_padding_statement_enum
:
1760 print_padding_statement (&s
->padding_statement
);
1762 case lang_output_section_statement_enum
:
1763 print_output_section_statement (&s
->output_section_statement
);
1765 case lang_assignment_statement_enum
:
1766 print_assignment (&s
->assignment_statement
,
1769 case lang_target_statement_enum
:
1770 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1772 case lang_output_statement_enum
:
1773 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1774 s
->output_statement
.name
,
1775 output_target
? output_target
: "");
1777 case lang_input_statement_enum
:
1778 print_input_statement (&s
->input_statement
);
1780 case lang_group_statement_enum
:
1781 print_group (&s
->group_statement
, os
);
1783 case lang_afile_asection_pair_statement_enum
:
1793 print_statement_list (statement_list
.head
,
1794 abs_output_section
);
1798 /* Print the first N statements in statement list S to STDERR.
1799 If N == 0, nothing is printed.
1800 If N < 0, the entire list is printed.
1801 Intended to be called from GDB. */
1804 dprint_statement (s
, n
)
1805 lang_statement_union_type
* s
;
1808 FILE *map_save
= config
.map_file
;
1810 config
.map_file
= stderr
;
1813 print_statement_list (s
, abs_output_section
);
1818 print_statement (s
, abs_output_section
);
1823 config
.map_file
= map_save
;
1827 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
1828 lang_statement_union_type
** this_ptr
;
1831 asection
* output_section_statement
;
1834 /* Align this section first to the
1835 input sections requirement, then
1836 to the output section's requirement.
1837 If this alignment is > than any seen before,
1838 then record it too. Perform the alignment by
1839 inserting a magic 'padding' statement.
1842 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1844 if (alignment_needed
!= 0)
1846 lang_statement_union_type
*new =
1847 ((lang_statement_union_type
*)
1848 stat_alloc (sizeof (lang_padding_statement_type
)));
1850 /* Link into existing chain */
1851 new->header
.next
= *this_ptr
;
1853 new->header
.type
= lang_padding_statement_enum
;
1854 new->padding_statement
.output_section
= output_section_statement
;
1855 new->padding_statement
.output_offset
=
1856 dot
- output_section_statement
->vma
;
1857 new->padding_statement
.fill
= fill
;
1858 new->padding_statement
.size
= alignment_needed
;
1862 /* Remember the most restrictive alignment */
1863 if (power
> output_section_statement
->alignment_power
)
1865 output_section_statement
->alignment_power
= power
;
1867 output_section_statement
->_raw_size
+= alignment_needed
;
1868 return alignment_needed
+ dot
;
1872 /* Work out how much this section will move the dot point */
1874 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
1875 lang_statement_union_type
** this_ptr
;
1876 lang_output_section_statement_type
* output_section_statement
;
1881 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1882 asection
*i
= is
->section
;
1884 if (is
->ifile
->just_syms_flag
== false)
1886 if (output_section_statement
->subsection_alignment
!= -1)
1887 i
->alignment_power
=
1888 output_section_statement
->subsection_alignment
;
1890 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1891 output_section_statement
->bfd_section
, dot
);
1893 /* Remember where in the output section this input section goes */
1895 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1897 /* Mark how big the output section must be to contain this now
1899 if (i
->_cooked_size
!= 0)
1900 dot
+= i
->_cooked_size
;
1902 dot
+= i
->_raw_size
;
1903 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1907 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1913 /* This variable indicates whether bfd_relax_section should be called
1916 static boolean relax_again
;
1918 /* Set the sizes for all the output sections. */
1921 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
1922 lang_statement_union_type
* s
;
1923 lang_output_section_statement_type
* output_section_statement
;
1924 lang_statement_union_type
** prev
;
1929 /* Size up the sections from their constituent parts */
1930 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1932 switch (s
->header
.type
)
1935 case lang_output_section_statement_enum
:
1938 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1940 if (os
->bfd_section
== NULL
)
1942 /* This section was never actually created. */
1946 /* If this is a COFF shared library section, use the size and
1947 address from the input section. FIXME: This is COFF
1948 specific; it would be cleaner if there were some other way
1949 to do this, but nothing simple comes to mind. */
1950 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
1954 if (os
->children
.head
== NULL
1955 || os
->children
.head
->next
!= NULL
1956 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
1957 einfo ("%P%X: Internal error on COFF shared library section %s",
1960 input
= os
->children
.head
->input_section
.section
;
1961 bfd_set_section_vma (os
->bfd_section
->owner
,
1963 bfd_section_vma (input
->owner
, input
));
1964 os
->bfd_section
->_raw_size
= input
->_raw_size
;
1968 if (bfd_is_abs_section (os
->bfd_section
))
1970 /* No matter what happens, an abs section starts at zero */
1971 ASSERT (os
->bfd_section
->vma
== 0);
1975 if (os
->addr_tree
== (etree_type
*) NULL
)
1977 /* No address specified for this section, get one
1978 from the region specification
1980 if (os
->region
== (lang_memory_region_type
*) NULL
)
1982 os
->region
= lang_memory_region_lookup ("*default*");
1984 dot
= os
->region
->current
;
1985 if (os
->section_alignment
== -1)
1986 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1992 r
= exp_fold_tree (os
->addr_tree
,
1994 lang_allocating_phase_enum
,
1996 if (r
.valid
== false)
1998 einfo ("%F%S: non constant address expression for section %s\n",
2003 /* The section starts here */
2004 /* First, align to what the section needs */
2006 if (os
->section_alignment
!= -1)
2007 dot
= align_power (dot
, os
->section_alignment
);
2009 bfd_set_section_vma (0, os
->bfd_section
, dot
);
2011 os
->bfd_section
->output_offset
= 0;
2014 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
2015 os
->fill
, dot
, relax
);
2016 /* Ignore the size of the input sections, use the vma and size to */
2019 after
= ALIGN_N (os
->bfd_section
->vma
+
2020 os
->bfd_section
->_raw_size
,
2021 /* The coercion here is important, see ld.h. */
2022 (bfd_vma
) os
->block_value
);
2024 if (bfd_is_abs_section (os
->bfd_section
))
2025 ASSERT (after
== os
->bfd_section
->vma
);
2027 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
2028 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
2029 os
->processed
= true;
2031 /* Replace into region ? */
2032 if (os
->region
!= (lang_memory_region_type
*) NULL
)
2034 os
->region
->current
= dot
;
2035 /* Make sure this isn't silly. */
2036 if (os
->region
->current
< os
->region
->origin
2037 || (os
->region
->current
- os
->region
->origin
2038 > os
->region
->length
))
2040 if (os
->addr_tree
!= (etree_type
*) NULL
)
2042 einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
2043 os
->region
->current
,
2044 os
->bfd_section
->owner
,
2045 os
->bfd_section
->name
,
2050 einfo ("%X%P: region %s is full (%B section %s)\n",
2052 os
->bfd_section
->owner
,
2053 os
->bfd_section
->name
);
2055 /* Reset the region pointer. */
2056 os
->region
->current
= os
->region
->origin
;
2062 case lang_constructors_statement_enum
:
2063 dot
= lang_size_sections (constructor_list
.head
,
2064 output_section_statement
,
2065 &s
->wild_statement
.children
.head
,
2070 case lang_data_statement_enum
:
2072 unsigned int size
= 0;
2074 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
2075 s
->data_statement
.output_section
=
2076 output_section_statement
->bfd_section
;
2078 switch (s
->data_statement
.type
)
2095 output_section_statement
->bfd_section
->_raw_size
+= size
;
2096 /* The output section gets contents, and then we inspect for
2097 any flags set in the input script which override any ALLOC */
2098 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
2099 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
)) {
2100 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
2105 case lang_reloc_statement_enum
:
2109 s
->reloc_statement
.output_vma
=
2110 dot
- output_section_statement
->bfd_section
->vma
;
2111 s
->reloc_statement
.output_section
=
2112 output_section_statement
->bfd_section
;
2113 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2115 output_section_statement
->bfd_section
->_raw_size
+= size
;
2119 case lang_wild_statement_enum
:
2121 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
2122 output_section_statement
,
2123 &s
->wild_statement
.children
.head
,
2129 case lang_object_symbols_statement_enum
:
2130 link_info
.create_object_symbols_section
=
2131 output_section_statement
->bfd_section
;
2133 case lang_output_statement_enum
:
2134 case lang_target_statement_enum
:
2136 case lang_input_section_enum
:
2140 i
= (*prev
)->input_section
.section
;
2142 i
->_cooked_size
= i
->_raw_size
;
2147 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
2148 einfo ("%P%F: can't relax section: %E\n");
2152 dot
= size_input_section (prev
,
2153 output_section_statement
,
2154 output_section_statement
->fill
,
2158 case lang_input_statement_enum
:
2160 case lang_fill_statement_enum
:
2161 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
2163 fill
= s
->fill_statement
.fill
;
2165 case lang_assignment_statement_enum
:
2167 bfd_vma newdot
= dot
;
2169 exp_fold_tree (s
->assignment_statement
.exp
,
2170 output_section_statement
,
2171 lang_allocating_phase_enum
,
2175 if (newdot
!= dot
&& !relax
)
2177 /* The assignment changed dot. Insert a pad. */
2178 if (output_section_statement
== abs_output_section
)
2180 /* If we don't have an output section, then just adjust
2181 the default memory address. */
2182 lang_memory_region_lookup ("*default*")->current
= newdot
;
2186 lang_statement_union_type
*new =
2187 ((lang_statement_union_type
*)
2188 stat_alloc (sizeof (lang_padding_statement_type
)));
2190 /* Link into existing chain */
2191 new->header
.next
= *prev
;
2193 new->header
.type
= lang_padding_statement_enum
;
2194 new->padding_statement
.output_section
=
2195 output_section_statement
->bfd_section
;
2196 new->padding_statement
.output_offset
=
2197 dot
- output_section_statement
->bfd_section
->vma
;
2198 new->padding_statement
.fill
= fill
;
2199 new->padding_statement
.size
= newdot
- dot
;
2200 output_section_statement
->bfd_section
->_raw_size
+=
2201 new->padding_statement
.size
;
2209 case lang_padding_statement_enum
:
2210 /* If we are relaxing, and this is not the first pass, some
2211 padding statements may have been inserted during previous
2212 passes. We may have to move the padding statement to a new
2213 location if dot has a different value at this point in this
2214 pass than it did at this point in the previous pass. */
2215 s
->padding_statement
.output_offset
=
2216 dot
- output_section_statement
->bfd_section
->vma
;
2217 dot
+= s
->padding_statement
.size
;
2218 output_section_statement
->bfd_section
->_raw_size
+=
2219 s
->padding_statement
.size
;
2222 case lang_group_statement_enum
:
2223 dot
= lang_size_sections (s
->group_statement
.children
.head
,
2224 output_section_statement
,
2225 &s
->group_statement
.children
.head
,
2233 /* This can only get here when relaxing is turned on */
2235 case lang_address_statement_enum
:
2238 prev
= &s
->header
.next
;
2244 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
2245 lang_statement_union_type
* s
;
2246 lang_output_section_statement_type
* output_section_statement
;
2250 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2252 switch (s
->header
.type
)
2254 case lang_constructors_statement_enum
:
2255 dot
= lang_do_assignments (constructor_list
.head
,
2256 output_section_statement
,
2261 case lang_output_section_statement_enum
:
2263 lang_output_section_statement_type
*os
=
2264 &(s
->output_section_statement
);
2266 if (os
->bfd_section
!= NULL
)
2268 dot
= os
->bfd_section
->vma
;
2269 (void) lang_do_assignments (os
->children
.head
, os
,
2271 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
2275 /* If nothing has been placed into the output section then
2276 it won't have a bfd_section. */
2277 if (os
->bfd_section
)
2279 os
->bfd_section
->lma
2280 = exp_get_abs_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
2285 case lang_wild_statement_enum
:
2287 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
2288 output_section_statement
,
2293 case lang_object_symbols_statement_enum
:
2294 case lang_output_statement_enum
:
2295 case lang_target_statement_enum
:
2297 case lang_common_statement_enum
:
2300 case lang_data_statement_enum
:
2302 etree_value_type value
;
2304 value
= exp_fold_tree (s
->data_statement
.exp
,
2306 lang_final_phase_enum
, dot
, &dot
);
2307 s
->data_statement
.value
= value
.value
;
2308 if (value
.valid
== false)
2309 einfo ("%F%P: invalid data statement\n");
2311 switch (s
->data_statement
.type
)
2328 case lang_reloc_statement_enum
:
2330 etree_value_type value
;
2332 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
2334 lang_final_phase_enum
, dot
, &dot
);
2335 s
->reloc_statement
.addend_value
= value
.value
;
2336 if (value
.valid
== false)
2337 einfo ("%F%P: invalid reloc statement\n");
2339 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2342 case lang_input_section_enum
:
2344 asection
*in
= s
->input_section
.section
;
2346 if (in
->_cooked_size
!= 0)
2347 dot
+= in
->_cooked_size
;
2349 dot
+= in
->_raw_size
;
2353 case lang_input_statement_enum
:
2355 case lang_fill_statement_enum
:
2356 fill
= s
->fill_statement
.fill
;
2358 case lang_assignment_statement_enum
:
2360 exp_fold_tree (s
->assignment_statement
.exp
,
2361 output_section_statement
,
2362 lang_final_phase_enum
,
2368 case lang_padding_statement_enum
:
2369 dot
+= s
->padding_statement
.size
;
2372 case lang_group_statement_enum
:
2373 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
2374 output_section_statement
,
2382 case lang_address_statement_enum
:
2390 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2391 operator .startof. (section_name), it produces an undefined symbol
2392 .startof.section_name. Similarly, when it sees
2393 .sizeof. (section_name), it produces an undefined symbol
2394 .sizeof.section_name. For all the output sections, we look for
2395 such symbols, and set them to the correct value. */
2402 if (link_info
.relocateable
)
2405 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2407 const char *secname
;
2409 struct bfd_link_hash_entry
*h
;
2411 secname
= bfd_get_section_name (output_bfd
, s
);
2412 buf
= xmalloc (10 + strlen (secname
));
2414 sprintf (buf
, ".startof.%s", secname
);
2415 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
2416 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
2418 h
->type
= bfd_link_hash_defined
;
2419 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
2420 h
->u
.def
.section
= bfd_abs_section_ptr
;
2423 sprintf (buf
, ".sizeof.%s", secname
);
2424 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
2425 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
2427 h
->type
= bfd_link_hash_defined
;
2428 if (s
->_cooked_size
!= 0)
2429 h
->u
.def
.value
= s
->_cooked_size
;
2431 h
->u
.def
.value
= s
->_raw_size
;
2432 h
->u
.def
.section
= bfd_abs_section_ptr
;
2442 struct bfd_link_hash_entry
*h
;
2445 if (link_info
.relocateable
|| link_info
.shared
)
2450 if (entry_symbol
== (char *) NULL
)
2452 /* No entry has been specified. Look for start, but don't warn
2453 if we don't find it. */
2454 entry_symbol
= "start";
2458 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
2459 if (h
!= (struct bfd_link_hash_entry
*) NULL
2460 && (h
->type
== bfd_link_hash_defined
2461 || h
->type
== bfd_link_hash_defweak
)
2462 && h
->u
.def
.section
->output_section
!= NULL
)
2466 val
= (h
->u
.def
.value
2467 + bfd_get_section_vma (output_bfd
,
2468 h
->u
.def
.section
->output_section
)
2469 + h
->u
.def
.section
->output_offset
);
2470 if (! bfd_set_start_address (output_bfd
, val
))
2471 einfo ("%P%F:%s: can't set start address\n", entry_symbol
);
2477 /* Can't find the entry symbol. Use the first address in the
2479 ts
= bfd_get_section_by_name (output_bfd
, ".text");
2480 if (ts
!= (asection
*) NULL
)
2483 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2484 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
2485 if (! bfd_set_start_address (output_bfd
,
2486 bfd_get_section_vma (output_bfd
, ts
)))
2487 einfo ("%P%F: can't set start address\n");
2492 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2498 /* Check that the architecture of all the input files is compatible
2499 with the output file. Also call the backend to let it do any
2500 other checking that is needed. */
2505 lang_statement_union_type
*file
;
2507 CONST bfd_arch_info_type
*compatible
;
2509 for (file
= file_chain
.head
;
2510 file
!= (lang_statement_union_type
*) NULL
;
2511 file
= file
->input_statement
.next
)
2513 input_bfd
= file
->input_statement
.the_bfd
;
2514 compatible
= bfd_arch_get_compatible (input_bfd
,
2516 if (compatible
== NULL
)
2517 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2518 bfd_printable_name (input_bfd
), input_bfd
,
2519 bfd_printable_name (output_bfd
));
2522 bfd_merge_private_bfd_data (input_bfd
, output_bfd
);
2526 /* Look through all the global common symbols and attach them to the
2527 correct section. The -sort-common command line switch may be used
2528 to roughly sort the entries by size. */
2533 if (link_info
.relocateable
2534 && ! command_line
.force_common_definition
)
2537 if (! config
.sort_common
)
2538 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
2543 for (power
= 4; power
>= 0; power
--)
2544 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
2549 /* Place one common symbol in the correct section. */
2552 lang_one_common (h
, info
)
2553 struct bfd_link_hash_entry
*h
;
2556 unsigned int power_of_two
;
2560 if (h
->type
!= bfd_link_hash_common
)
2564 power_of_two
= h
->u
.c
.p
->alignment_power
;
2566 if (config
.sort_common
2567 && power_of_two
< (unsigned int) *(int *) info
)
2570 section
= h
->u
.c
.p
->section
;
2572 /* Increase the size of the section. */
2573 section
->_raw_size
= ALIGN_N (section
->_raw_size
,
2574 (bfd_size_type
) (1 << power_of_two
));
2576 /* Adjust the alignment if necessary. */
2577 if (power_of_two
> section
->alignment_power
)
2578 section
->alignment_power
= power_of_two
;
2580 /* Change the symbol from common to defined. */
2581 h
->type
= bfd_link_hash_defined
;
2582 h
->u
.def
.section
= section
;
2583 h
->u
.def
.value
= section
->_raw_size
;
2585 /* Increase the size of the section. */
2586 section
->_raw_size
+= size
;
2588 /* Make sure the section is allocated in memory. */
2589 section
->flags
|= SEC_ALLOC
;
2591 if (config
.map_file
!= NULL
)
2592 fprintf (config
.map_file
, "Allocating common %s: %lx at %lx %s\n",
2593 h
->root
.string
, (unsigned long) size
,
2594 (unsigned long) h
->u
.def
.value
, section
->owner
->filename
);
2600 run through the input files and ensure that every input
2601 section has somewhere to go. If one is found without
2602 a destination then create an input request and place it
2603 into the statement tree.
2607 lang_place_orphans ()
2609 lang_input_statement_type
*file
;
2611 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2612 file
!= (lang_input_statement_type
*) NULL
;
2613 file
= (lang_input_statement_type
*) file
->next
)
2617 for (s
= file
->the_bfd
->sections
;
2618 s
!= (asection
*) NULL
;
2621 if (s
->output_section
== (asection
*) NULL
)
2623 /* This section of the file is not attatched, root
2624 around for a sensible place for it to go */
2626 if (file
->just_syms_flag
)
2628 /* We are only retrieving symbol values from this
2629 file. We want the symbols to act as though the
2630 values in the file are absolute. */
2631 s
->output_section
= bfd_abs_section_ptr
;
2632 s
->output_offset
= s
->vma
;
2634 else if (file
->common_section
== s
)
2636 /* This is a lonely common section which must
2637 have come from an archive. We attatch to the
2638 section with the wildcard */
2639 if (! link_info
.relocateable
2640 && ! command_line
.force_common_definition
)
2642 if (default_common_section
==
2643 (lang_output_section_statement_type
*) NULL
)
2645 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2647 default_common_section
=
2648 lang_output_section_statement_lookup (".bss");
2651 wild_doit (&default_common_section
->children
, s
,
2652 default_common_section
, file
);
2655 else if (ldemul_place_orphan (file
, s
))
2659 lang_output_section_statement_type
*os
=
2660 lang_output_section_statement_lookup (s
->name
);
2662 wild_doit (&os
->children
, s
, os
, file
);
2671 lang_set_flags (ptr
, flags
)
2675 boolean state
= false;
2690 /* ptr->flag_read = state; */
2693 /* ptr->flag_write = state; */
2696 /* ptr->flag_executable= state;*/
2700 /* ptr->flag_loadable= state;*/
2703 einfo ("%P%F: invalid syntax in flags\n");
2710 /* Call a function on each input file. This function will be called
2711 on an archive, but not on the elements. */
2714 lang_for_each_input_file (func
)
2715 void (*func
) PARAMS ((lang_input_statement_type
*));
2717 lang_input_statement_type
*f
;
2719 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
2721 f
= (lang_input_statement_type
*) f
->next_real_file
)
2725 /* Call a function on each file. The function will be called on all
2726 the elements of an archive which are included in the link, but will
2727 not be called on the archive file itself. */
2730 lang_for_each_file (func
)
2731 void (*func
) PARAMS ((lang_input_statement_type
*));
2733 lang_input_statement_type
*f
;
2735 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2736 f
!= (lang_input_statement_type
*) NULL
;
2737 f
= (lang_input_statement_type
*) f
->next
)
2748 lang_for_each_input_section (func
)
2749 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
2751 lang_input_statement_type
*f
;
2753 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2754 f
!= (lang_input_statement_type
*) NULL
;
2755 f
= (lang_input_statement_type
*) f
->next
)
2759 for (s
= f
->the_bfd
->sections
;
2760 s
!= (asection
*) NULL
;
2763 func (f
->the_bfd
, s
);
2771 ldlang_add_file (entry
)
2772 lang_input_statement_type
* entry
;
2776 lang_statement_append (&file_chain
,
2777 (lang_statement_union_type
*) entry
,
2780 /* The BFD linker needs to have a list of all input BFDs involved in
2782 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
2783 ASSERT (entry
->the_bfd
!= output_bfd
);
2784 for (pp
= &link_info
.input_bfds
;
2785 *pp
!= (bfd
*) NULL
;
2786 pp
= &(*pp
)->link_next
)
2788 *pp
= entry
->the_bfd
;
2789 entry
->the_bfd
->usrdata
= (PTR
) entry
;
2790 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
2792 /* Look through the sections and check for any which should not be
2793 included in the link. We need to do this now, so that we can
2794 notice when the backend linker tries to report multiple
2795 definition errors for symbols which are in sections we aren't
2796 going to link. FIXME: It might be better to entirely ignore
2797 symbols which are defined in sections which are going to be
2798 discarded. This would require modifying the backend linker for
2799 each backend which might set the SEC_LINK_ONCE flag. If we do
2800 this, we should probably handle SEC_EXCLUDE in the same way. */
2802 bfd_map_over_sections (entry
->the_bfd
, section_already_linked
, (PTR
) NULL
);
2806 lang_add_output (name
, from_script
)
2810 /* Make -o on command line override OUTPUT in script. */
2811 if (had_output_filename
== false || !from_script
)
2813 output_filename
= name
;
2814 had_output_filename
= true;
2819 static lang_output_section_statement_type
*current_section
;
2831 for (l
= 0; l
< 32; l
++)
2833 if (i
>= (unsigned int) x
)
2841 lang_enter_output_section_statement (output_section_statement_name
,
2842 address_exp
, flags
, block_value
,
2843 align
, subalign
, ebase
)
2844 const char *output_section_statement_name
;
2845 etree_type
* address_exp
;
2847 bfd_vma block_value
;
2849 etree_type
*subalign
;
2852 lang_output_section_statement_type
*os
;
2856 lang_output_section_statement_lookup (output_section_statement_name
);
2860 /* Add this statement to tree */
2861 /* add_statement(lang_output_section_statement_enum,
2862 output_section_statement);*/
2863 /* Make next things chain into subchain of this */
2865 if (os
->addr_tree
==
2866 (etree_type
*) NULL
)
2872 if (flags
& SEC_NEVER_LOAD
)
2876 os
->block_value
= block_value
? block_value
: 1;
2877 stat_ptr
= &os
->children
;
2879 os
->subsection_alignment
= topower(
2880 exp_get_value_int(subalign
, -1,
2881 "subsection alignment",
2883 os
->section_alignment
= topower(
2884 exp_get_value_int(align
, -1,
2885 "section alignment", 0));
2887 os
->load_base
= ebase
;
2894 lang_output_statement_type
*new =
2895 new_stat (lang_output_statement
, stat_ptr
);
2897 new->name
= output_filename
;
2900 /* Reset the current counters in the regions */
2902 reset_memory_regions ()
2904 lang_memory_region_type
*p
= lang_memory_region_list
;
2906 for (p
= lang_memory_region_list
;
2907 p
!= (lang_memory_region_type
*) NULL
;
2910 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
2911 p
->current
= p
->origin
;
2918 lang_reasonable_defaults ();
2919 current_target
= default_target
;
2921 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2923 ldemul_create_output_section_statements ();
2925 /* Add to the hash table all undefineds on the command line */
2926 lang_place_undefineds ();
2928 /* Create a bfd for each input file */
2929 current_target
= default_target
;
2930 open_input_bfds (statement_list
.head
, false);
2932 ldemul_after_open ();
2934 /* Build all sets based on the information gathered from the input
2936 ldctor_build_sets ();
2938 /* Size up the common data */
2941 /* Run through the contours of the script and attatch input sections
2942 to the correct output sections
2944 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2945 (lang_output_section_statement_type
*) NULL
);
2948 /* Find any sections not attatched explicitly and handle them */
2949 lang_place_orphans ();
2951 ldemul_before_allocation ();
2953 /* We must record the program headers before we try to fix the
2954 section positions, since they will affect SIZEOF_HEADERS. */
2955 lang_record_phdrs ();
2957 /* Now run around and relax if we can */
2958 if (command_line
.relax
)
2960 /* First time round is a trial run to get the 'worst case'
2961 addresses of the objects if there was no relaxing. */
2962 lang_size_sections (statement_list
.head
,
2964 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2966 /* Keep relaxing until bfd_relax_section gives up. */
2969 reset_memory_regions ();
2971 relax_again
= false;
2973 /* Do all the assignments with our current guesses as to
2975 lang_do_assignments (statement_list
.head
,
2977 (fill_type
) 0, (bfd_vma
) 0);
2979 /* Perform another relax pass - this time we know where the
2980 globals are, so can make better guess. */
2981 lang_size_sections (statement_list
.head
,
2983 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2985 while (relax_again
);
2989 /* Size up the sections. */
2990 lang_size_sections (statement_list
.head
,
2992 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2995 /* See if anything special should be done now we know how big
2997 ldemul_after_allocation ();
2999 /* Fix any .startof. or .sizeof. symbols. */
3000 lang_set_startof ();
3002 /* Do all the assignments, now that we know the final restingplaces
3003 of all the symbols */
3005 lang_do_assignments (statement_list
.head
,
3007 (fill_type
) 0, (bfd_vma
) 0);
3009 /* Make sure that we're not mixing architectures */
3019 /* EXPORTED TO YACC */
3022 lang_add_wild (section_name
, filename
)
3023 CONST
char *CONST section_name
;
3024 CONST
char *CONST filename
;
3026 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
3029 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
3031 placed_commons
= true;
3033 if (filename
!= (char *) NULL
)
3035 lang_has_input_file
= true;
3037 new->section_name
= section_name
;
3038 new->filename
= filename
;
3039 lang_list_init (&new->children
);
3043 lang_section_start (name
, address
)
3045 etree_type
* address
;
3047 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
3049 ad
->section_name
= name
;
3050 ad
->address
= address
;
3053 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3054 because of a -e argument on the command line, or zero if this is
3055 called by ENTRY in a linker script. Command line arguments take
3058 /* WINDOWS_NT. When an entry point has been specified, we will also force
3059 this symbol to be defined by calling ldlang_add_undef (equivalent to
3060 having switch -u entry_name on the command line). The reason we do
3061 this is so that the user doesn't have to because they would have to use
3062 the -u switch if they were specifying an entry point other than
3063 _mainCRTStartup. Specifically, if creating a windows application, entry
3064 point _WinMainCRTStartup must be specified.
3065 What I have found for non console applications (entry not _mainCRTStartup)
3066 is that the .obj that contains mainCRTStartup is brought in since it is
3067 the first encountered in libc.lib and it has other symbols in it which will
3068 be pulled in by the link process. To avoid this, adding -u with the entry
3069 point name specified forces the correct .obj to be used. We can avoid
3070 making the user do this by always adding the entry point name as an
3071 undefined symbol. */
3074 lang_add_entry (name
, cmdline
)
3078 if (entry_symbol
== NULL
3080 || ! entry_from_cmdline
)
3082 entry_symbol
= name
;
3083 entry_from_cmdline
= cmdline
;
3086 /* don't do this yet. It seems to work (the executables run), but the
3087 image created is very different from what I was getting before indicating
3088 that something else is being pulled in. When everything else is working,
3089 then try to put this back in to see if it will do the right thing for
3090 other more complicated applications */
3091 ldlang_add_undef (name
);
3096 lang_add_target (name
)
3099 lang_target_statement_type
*new = new_stat (lang_target_statement
,
3115 map_option_f
= true;
3126 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
3133 lang_add_data (type
, exp
)
3135 union etree_union
*exp
;
3138 lang_data_statement_type
*new = new_stat (lang_data_statement
,
3146 /* Create a new reloc statement. RELOC is the BFD relocation type to
3147 generate. HOWTO is the corresponding howto structure (we could
3148 look this up, but the caller has already done so). SECTION is the
3149 section to generate a reloc against, or NAME is the name of the
3150 symbol to generate a reloc against. Exactly one of SECTION and
3151 NAME must be NULL. ADDEND is an expression for the addend. */
3154 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
3155 bfd_reloc_code_real_type reloc
;
3156 reloc_howto_type
*howto
;
3159 union etree_union
*addend
;
3161 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
3165 p
->section
= section
;
3167 p
->addend_exp
= addend
;
3169 p
->addend_value
= 0;
3170 p
->output_section
= NULL
;
3175 lang_add_assignment (exp
)
3178 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
3185 lang_add_attribute (attribute
)
3186 enum statement_enum attribute
;
3188 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
3195 if (startup_file
!= (char *) NULL
)
3197 einfo ("%P%Fmultiple STARTUP files\n");
3199 first_file
->filename
= name
;
3200 first_file
->local_sym_name
= name
;
3201 first_file
->real
= true;
3203 startup_file
= name
;
3210 lang_float_flag
= maybe
;
3214 lang_leave_output_section_statement (fill
, memspec
)
3216 CONST
char *memspec
;
3218 current_section
->fill
= fill
;
3219 current_section
->region
= lang_memory_region_lookup (memspec
);
3220 stat_ptr
= &statement_list
;
3224 Create an absolute symbol with the given name with the value of the
3225 address of first byte of the section named.
3227 If the symbol already exists, then do nothing.
3230 lang_abs_symbol_at_beginning_of (secname
, name
)
3231 const char *secname
;
3234 struct bfd_link_hash_entry
*h
;
3236 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
3237 if (h
== (struct bfd_link_hash_entry
*) NULL
)
3238 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3240 if (h
->type
== bfd_link_hash_new
3241 || h
->type
== bfd_link_hash_undefined
)
3245 h
->type
= bfd_link_hash_defined
;
3247 sec
= bfd_get_section_by_name (output_bfd
, secname
);
3248 if (sec
== (asection
*) NULL
)
3251 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
3253 h
->u
.def
.section
= bfd_abs_section_ptr
;
3258 Create an absolute symbol with the given name with the value of the
3259 address of the first byte after the end of the section named.
3261 If the symbol already exists, then do nothing.
3264 lang_abs_symbol_at_end_of (secname
, name
)
3265 const char *secname
;
3268 struct bfd_link_hash_entry
*h
;
3270 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
3271 if (h
== (struct bfd_link_hash_entry
*) NULL
)
3272 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3274 if (h
->type
== bfd_link_hash_new
3275 || h
->type
== bfd_link_hash_undefined
)
3279 h
->type
= bfd_link_hash_defined
;
3281 sec
= bfd_get_section_by_name (output_bfd
, secname
);
3282 if (sec
== (asection
*) NULL
)
3285 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
3286 + bfd_section_size (output_bfd
, sec
));
3288 h
->u
.def
.section
= bfd_abs_section_ptr
;
3293 lang_statement_append (list
, element
, field
)
3294 lang_statement_list_type
* list
;
3295 lang_statement_union_type
* element
;
3296 lang_statement_union_type
** field
;
3298 *(list
->tail
) = element
;
3302 /* Set the output format type. -oformat overrides scripts. */
3305 lang_add_output_format (format
, big
, little
, from_script
)
3311 if (output_target
== NULL
|| !from_script
)
3313 if (command_line
.endian
== ENDIAN_BIG
3316 else if (command_line
.endian
== ENDIAN_LITTLE
3320 output_target
= format
;
3324 /* Enter a group. This creates a new lang_group_statement, and sets
3325 stat_ptr to build new statements within the group. */
3330 lang_group_statement_type
*g
;
3332 g
= new_stat (lang_group_statement
, stat_ptr
);
3333 lang_list_init (&g
->children
);
3334 stat_ptr
= &g
->children
;
3337 /* Leave a group. This just resets stat_ptr to start writing to the
3338 regular list of statements again. Note that this will not work if
3339 groups can occur inside anything else which can adjust stat_ptr,
3340 but currently they can't. */
3345 stat_ptr
= &statement_list
;
3348 /* Add a new program header. This is called for each entry in a PHDRS
3349 command in a linker script. */
3352 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
3360 struct lang_phdr
*n
, **pp
;
3362 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
3365 n
->type
= exp_get_value_int (type
, 0, "program header type",
3366 lang_final_phase_enum
);
3367 n
->filehdr
= filehdr
;
3372 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3377 /* Record that a section should be placed in a phdr. */
3380 lang_section_in_phdr (name
)
3383 struct lang_output_section_phdr_list
*n
;
3385 n
= ((struct lang_output_section_phdr_list
*)
3386 stat_alloc (sizeof (struct lang_output_section_phdr_list
)));
3389 n
->next
= current_section
->phdrs
;
3390 current_section
->phdrs
= n
;
3393 /* Record the program header information in the output BFD. FIXME: We
3394 should not be calling an ELF specific function here. */
3397 lang_record_phdrs ()
3401 struct lang_output_section_phdr_list
*last
;
3402 struct lang_phdr
*l
;
3403 lang_statement_union_type
*u
;
3406 secs
= xmalloc (alc
* sizeof (asection
*));
3408 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
3415 for (u
= lang_output_section_statement
.head
;
3417 u
= u
->output_section_statement
.next
)
3419 lang_output_section_statement_type
*os
;
3420 struct lang_output_section_phdr_list
*pl
;
3422 os
= &u
->output_section_statement
;
3430 || os
->bfd_section
== NULL
3431 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
3436 if (os
->bfd_section
== NULL
)
3439 for (; pl
!= NULL
; pl
= pl
->next
)
3441 if (strcmp (pl
->name
, l
->name
) == 0)
3446 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
3448 secs
[c
] = os
->bfd_section
;
3455 if (l
->flags
== NULL
)
3458 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
3459 lang_final_phase_enum
);
3464 at
= exp_get_vma (l
->at
, 0, "phdr load address",
3465 lang_final_phase_enum
);
3467 if (! bfd_record_phdr (output_bfd
, l
->type
,
3468 l
->flags
== NULL
? false : true,
3470 l
->at
== NULL
? false : true,
3471 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
3472 einfo ("%F%P: bfd_record_phdr failed: %E\n");
3477 /* Make sure all the phdr assignments succeeded. */
3478 for (u
= lang_output_section_statement
.head
;
3480 u
= u
->output_section_statement
.next
)
3482 struct lang_output_section_phdr_list
*pl
;
3484 if (u
->output_section_statement
.bfd_section
== NULL
)
3487 for (pl
= u
->output_section_statement
.phdrs
;
3490 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
3491 einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
3492 u
->output_section_statement
.name
, pl
->name
);