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 wild_section
PARAMS ((lang_wild_statement_type
*ptr
,
75 lang_input_statement_type
*file
,
76 lang_output_section_statement_type
*output
));
77 static lang_input_statement_type
*lookup_name
PARAMS ((const char *name
));
78 static void load_symbols
PARAMS ((lang_input_statement_type
*entry
,
79 lang_statement_list_type
*));
80 static void wild
PARAMS ((lang_wild_statement_type
*s
,
81 const char *section
, const char *file
,
83 lang_output_section_statement_type
*output
));
84 static bfd
*open_output
PARAMS ((const char *name
));
85 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*statement
));
86 static void open_input_bfds
87 PARAMS ((lang_statement_union_type
*statement
, boolean
));
88 static void lang_reasonable_defaults
PARAMS ((void));
89 static void lang_place_undefineds
PARAMS ((void));
90 static void map_input_to_output_sections
91 PARAMS ((lang_statement_union_type
*s
,
93 lang_output_section_statement_type
*output_section_statement
));
94 static void print_output_section_statement
95 PARAMS ((lang_output_section_statement_type
*output_section_statement
));
96 static void print_assignment
97 PARAMS ((lang_assignment_statement_type
*assignment
,
98 lang_output_section_statement_type
*output_section
));
99 static void print_input_statement
PARAMS ((lang_input_statement_type
*statm
));
100 static void print_input_section
PARAMS ((lang_input_section_type
*in
));
101 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*fill
));
102 static void print_data_statement
PARAMS ((lang_data_statement_type
*data
));
103 static void print_reloc_statement
PARAMS ((lang_reloc_statement_type
*reloc
));
104 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*s
));
105 static void print_wild_statement
106 PARAMS ((lang_wild_statement_type
*w
,
107 lang_output_section_statement_type
*os
));
108 static void print_group
109 PARAMS ((lang_group_statement_type
*, lang_output_section_statement_type
*));
110 static void print_statement
PARAMS ((lang_statement_union_type
*s
,
111 lang_output_section_statement_type
*os
));
112 static void print_statement_list
PARAMS ((lang_statement_union_type
*s
,
113 lang_output_section_statement_type
*os
));
114 static void print_statements
PARAMS ((void));
115 static bfd_vma insert_pad
PARAMS ((lang_statement_union_type
**this_ptr
,
116 fill_type fill
, unsigned int power
,
117 asection
*output_section_statement
,
119 static bfd_vma size_input_section
120 PARAMS ((lang_statement_union_type
**this_ptr
,
121 lang_output_section_statement_type
*output_section_statement
,
122 fill_type fill
, bfd_vma dot
, boolean relax
));
123 static void lang_finish
PARAMS ((void));
124 static void lang_check
PARAMS ((void));
125 static void lang_common
PARAMS ((void));
126 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
127 static void lang_place_orphans
PARAMS ((void));
128 static int topower
PARAMS ((int));
129 static void lang_set_startof
PARAMS ((void));
130 static void reset_memory_regions
PARAMS ((void));
131 static void lang_record_phdrs
PARAMS ((void));
134 lang_output_section_statement_type
*abs_output_section
;
135 lang_statement_list_type
*stat_ptr
= &statement_list
;
136 lang_statement_list_type file_chain
= { 0 };
137 const char *entry_symbol
= NULL
;
138 boolean entry_from_cmdline
;
139 boolean lang_has_input_file
= false;
140 boolean had_output_filename
= false;
141 boolean lang_float_flag
= false;
142 boolean delete_output_file_on_failure
= false;
144 etree_type
*base
; /* Relocation base - or null */
147 #if defined(__STDC__) || defined(ALMOST_STDC)
148 #define cat(a,b) a##b
150 #define cat(a,b) a/**/b
153 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
155 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
157 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
163 return obstack_alloc (&stat_obstack
, size
);
170 fprintf (config
.map_file
, "%5x", (unsigned) value
);
174 print_alignment (value
)
177 fprintf (config
.map_file
, "2**%1u", value
);
184 fprintf (config
.map_file
, "%04x", (unsigned) value
);
191 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
194 /*----------------------------------------------------------------------
195 lang_for_each_statement walks the parse tree and calls the provided
196 function for each node
200 lang_for_each_statement_worker (func
, s
)
201 void (*func
) PARAMS ((lang_statement_union_type
*));
202 lang_statement_union_type
*s
;
204 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
208 switch (s
->header
.type
)
210 case lang_constructors_statement_enum
:
211 lang_for_each_statement_worker (func
, constructor_list
.head
);
213 case lang_output_section_statement_enum
:
214 lang_for_each_statement_worker
216 s
->output_section_statement
.children
.head
);
218 case lang_wild_statement_enum
:
219 lang_for_each_statement_worker
221 s
->wild_statement
.children
.head
);
223 case lang_group_statement_enum
:
224 lang_for_each_statement_worker (func
,
225 s
->group_statement
.children
.head
);
227 case lang_data_statement_enum
:
228 case lang_reloc_statement_enum
:
229 case lang_object_symbols_statement_enum
:
230 case lang_output_statement_enum
:
231 case lang_target_statement_enum
:
232 case lang_input_section_enum
:
233 case lang_input_statement_enum
:
234 case lang_assignment_statement_enum
:
235 case lang_padding_statement_enum
:
236 case lang_address_statement_enum
:
246 lang_for_each_statement (func
)
247 void (*func
) PARAMS ((lang_statement_union_type
*));
249 lang_for_each_statement_worker (func
,
250 statement_list
.head
);
253 /*----------------------------------------------------------------------*/
255 lang_list_init (list
)
256 lang_statement_list_type
*list
;
258 list
->head
= (lang_statement_union_type
*) NULL
;
259 list
->tail
= &list
->head
;
262 /*----------------------------------------------------------------------
264 build a new statement node for the parse tree
269 lang_statement_union_type
*
270 new_statement (type
, size
, list
)
271 enum statement_enum type
;
273 lang_statement_list_type
* list
;
275 lang_statement_union_type
*new = (lang_statement_union_type
*)
278 new->header
.type
= type
;
279 new->header
.next
= (lang_statement_union_type
*) NULL
;
280 lang_statement_append (list
, new, &new->header
.next
);
285 Build a new input file node for the language. There are several ways
286 in which we treat an input file, eg, we only look at symbols, or
287 prefix it with a -l etc.
289 We can be supplied with requests for input files more than once;
290 they may, for example be split over serveral lines like foo.o(.text)
291 foo.o(.data) etc, so when asked for a file we check that we havn't
292 got it already so we don't duplicate the bfd.
295 static lang_input_statement_type
*
296 new_afile (name
, file_type
, target
, add_to_list
)
298 lang_input_file_enum_type file_type
;
302 lang_input_statement_type
*p
;
305 p
= new_stat (lang_input_statement
, stat_ptr
);
308 p
= ((lang_input_statement_type
*)
309 stat_alloc (sizeof (lang_input_statement_type
)));
310 p
->header
.next
= NULL
;
313 lang_has_input_file
= true;
315 p
->complained
= false;
318 case lang_input_file_is_symbols_only_enum
:
320 p
->is_archive
= false;
322 p
->local_sym_name
= name
;
323 p
->just_syms_flag
= true;
324 p
->search_dirs_flag
= false;
326 case lang_input_file_is_fake_enum
:
328 p
->is_archive
= false;
330 p
->local_sym_name
= name
;
331 p
->just_syms_flag
= false;
332 p
->search_dirs_flag
= false;
334 case lang_input_file_is_l_enum
:
335 p
->is_archive
= true;
338 p
->local_sym_name
= concat ("-l", name
, (const char *) NULL
);
339 p
->just_syms_flag
= false;
340 p
->search_dirs_flag
= true;
342 case lang_input_file_is_marker_enum
:
344 p
->is_archive
= false;
346 p
->local_sym_name
= name
;
347 p
->just_syms_flag
= false;
348 p
->search_dirs_flag
= true;
350 case lang_input_file_is_search_file_enum
:
352 p
->is_archive
= false;
354 p
->local_sym_name
= name
;
355 p
->just_syms_flag
= false;
356 p
->search_dirs_flag
= true;
358 case lang_input_file_is_file_enum
:
360 p
->is_archive
= false;
362 p
->local_sym_name
= name
;
363 p
->just_syms_flag
= false;
364 p
->search_dirs_flag
= false;
369 p
->the_bfd
= (bfd
*) NULL
;
370 p
->asymbols
= (asymbol
**) NULL
;
371 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
372 p
->next
= (lang_statement_union_type
*) NULL
;
374 p
->common_output_section
= (asection
*) NULL
;
375 p
->dynamic
= config
.dynamic_link
;
376 p
->whole_archive
= whole_archive
;
378 lang_statement_append (&input_file_chain
,
379 (lang_statement_union_type
*) p
,
384 lang_input_statement_type
*
385 lang_add_input_file (name
, file_type
, target
)
387 lang_input_file_enum_type file_type
;
390 lang_has_input_file
= true;
391 return new_afile (name
, file_type
, target
, true);
394 /* Build enough state so that the parser can build its tree */
398 obstack_begin (&stat_obstack
, 1000);
400 stat_ptr
= &statement_list
;
402 lang_list_init (stat_ptr
);
404 lang_list_init (&input_file_chain
);
405 lang_list_init (&lang_output_section_statement
);
406 lang_list_init (&file_chain
);
407 first_file
= lang_add_input_file ((char *) NULL
,
408 lang_input_file_is_marker_enum
,
410 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
412 abs_output_section
->bfd_section
= bfd_abs_section_ptr
;
416 /*----------------------------------------------------------------------
417 A region is an area of memory declared with the
418 MEMORY { name:org=exp, len=exp ... }
421 We maintain a list of all the regions here
423 If no regions are specified in the script, then the default is used
424 which is created when looked up to be the entire data space
427 static lang_memory_region_type
*lang_memory_region_list
;
428 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
430 lang_memory_region_type
*
431 lang_memory_region_lookup (name
)
432 CONST
char *CONST name
;
435 lang_memory_region_type
*p
= lang_memory_region_list
;
437 for (p
= lang_memory_region_list
;
438 p
!= (lang_memory_region_type
*) NULL
;
441 if (strcmp (p
->name
, name
) == 0)
448 /* This code used to always use the first region in the list as the
449 default region. I changed it to instead use a region
450 encompassing all of memory as the default region. This permits
451 NOLOAD sections to work reasonably without requiring a region.
452 People should specify what region they mean, if they really want
454 if (strcmp (name
, "*default*") == 0)
456 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
458 return lang_memory_region_list
;
464 lang_memory_region_type
*new =
465 (lang_memory_region_type
*) stat_alloc (sizeof (lang_memory_region_type
));
467 new->name
= buystring (name
);
468 new->next
= (lang_memory_region_type
*) NULL
;
470 *lang_memory_region_list_tail
= new;
471 lang_memory_region_list_tail
= &new->next
;
473 new->length
= ~(bfd_size_type
)0;
475 new->had_full_message
= false;
482 lang_output_section_statement_type
*
483 lang_output_section_find (name
)
484 CONST
char *CONST name
;
486 lang_statement_union_type
*u
;
487 lang_output_section_statement_type
*lookup
;
489 for (u
= lang_output_section_statement
.head
;
490 u
!= (lang_statement_union_type
*) NULL
;
493 lookup
= &u
->output_section_statement
;
494 if (strcmp (name
, lookup
->name
) == 0)
499 return (lang_output_section_statement_type
*) NULL
;
502 lang_output_section_statement_type
*
503 lang_output_section_statement_lookup (name
)
504 CONST
char *CONST name
;
506 lang_output_section_statement_type
*lookup
;
508 lookup
= lang_output_section_find (name
);
509 if (lookup
== (lang_output_section_statement_type
*) NULL
)
512 lookup
= (lang_output_section_statement_type
*)
513 new_stat (lang_output_section_statement
, stat_ptr
);
514 lookup
->region
= (lang_memory_region_type
*) NULL
;
516 lookup
->block_value
= 1;
519 lookup
->next
= (lang_statement_union_type
*) NULL
;
520 lookup
->bfd_section
= (asection
*) NULL
;
521 lookup
->processed
= false;
522 lookup
->loadable
= 1;
523 lookup
->addr_tree
= (etree_type
*) NULL
;
524 lang_list_init (&lookup
->children
);
526 lookup
->memspec
= (CONST
char *) NULL
;
528 lookup
->subsection_alignment
= -1;
529 lookup
->section_alignment
= -1;
530 lookup
->load_base
= (union etree_union
*) NULL
;
531 lookup
->phdrs
= NULL
;
533 lang_statement_append (&lang_output_section_statement
,
534 (lang_statement_union_type
*) lookup
,
542 print_flags (ignore_flags
)
545 fprintf (config
.map_file
, "(");
547 if (flags
->flag_read
)
548 fprintf (outfile
, "R");
549 if (flags
->flag_write
)
550 fprintf (outfile
, "W");
551 if (flags
->flag_executable
)
552 fprintf (outfile
, "X");
553 if (flags
->flag_loadable
)
554 fprintf (outfile
, "L");
556 fprintf (config
.map_file
, ")");
562 lang_memory_region_type
*m
;
564 fprintf (config
.map_file
, "**MEMORY CONFIGURATION**\n\n");
566 fprintf (config
.map_file
, "name\t\torigin\t\tlength\t\tattributes\n");
568 fprintf (config
.map_file
,
569 "name\t\torigin length r_size c_size is attributes\n");
572 for (m
= lang_memory_region_list
;
573 m
!= (lang_memory_region_type
*) NULL
;
576 fprintf (config
.map_file
, "%-16s", m
->name
);
577 print_address (m
->origin
);
579 print_address ((bfd_vma
)m
->length
);
581 print_address ((bfd_vma
)m
->old_length
);
583 print_address (m
->current
- m
->origin
);
586 fprintf (config
.map_file
, " %2d%% ",
587 (int) ((m
->current
- m
->origin
) * 100 / m
->old_length
));
588 print_flags (&m
->flags
);
589 fprintf (config
.map_file
, "\n");
591 fprintf (config
.map_file
, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
592 fprintf (config
.map_file
, "output input virtual\n");
593 fprintf (config
.map_file
, "section section address tsize\n\n");
604 lang_output_section_statement_type
* s
;
606 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
607 section_userdata_type
*new =
608 (section_userdata_type
*)
609 stat_alloc (sizeof (section_userdata_type
));
611 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
612 if (s
->bfd_section
== (asection
*) NULL
)
613 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
614 if (s
->bfd_section
== (asection
*) NULL
)
616 einfo ("%P%F: output format %s cannot represent section called %s\n",
617 output_bfd
->xvec
->name
, s
->name
);
619 s
->bfd_section
->output_section
= s
->bfd_section
;
621 /* We initialize an output sections output offset to minus its own */
622 /* vma to allow us to output a section through itself */
623 s
->bfd_section
->output_offset
= 0;
624 get_userdata (s
->bfd_section
) = (PTR
) new;
628 /* The wild routines.
630 These expand statements like *(.text) and foo.o to a list of
631 explicit actions, like foo.o(.text), bar.o(.text) and
632 foo.o(.text, .data). */
634 /* Add SECTION to the output section OUTPUT. Do this by creating a
635 lang_input_section statement which is placed at PTR. FILE is the
636 input file which holds SECTION. */
639 wild_doit (ptr
, section
, output
, file
)
640 lang_statement_list_type
*ptr
;
642 lang_output_section_statement_type
*output
;
643 lang_input_statement_type
*file
;
645 if (output
->bfd_section
== NULL
)
648 if (section
!= NULL
&& section
->output_section
== NULL
)
650 /* Add a section reference to the list */
651 lang_input_section_type
*new = new_stat (lang_input_section
, ptr
);
653 new->section
= section
;
655 section
->output_section
= output
->bfd_section
;
657 /* We don't copy the SEC_NEVER_LOAD flag from an input section
658 to an output section, because we want to be able to include a
659 SEC_NEVER_LOAD section in the middle of an otherwise loaded
660 section (I don't know why we want to do this, but we do).
661 build_link_order in ldwrite.c handles this case by turning
662 the embedded SEC_NEVER_LOAD section into a fill. */
663 section
->output_section
->flags
|=
664 section
->flags
& (flagword
) (~ SEC_NEVER_LOAD
);
666 if (! output
->loadable
)
668 /* Turn off load flag */
669 output
->bfd_section
->flags
&= ~SEC_LOAD
;
670 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
673 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
674 output
->bfd_section
->alignment_power
= section
->alignment_power
;
676 /* If supplied an aligment, then force it */
677 if (output
->section_alignment
!= -1)
678 output
->bfd_section
->alignment_power
= output
->section_alignment
;
682 /* Expand a wild statement for a particular FILE. SECTION may be
683 NULL, in which case it is a wild card. */
686 wild_section (ptr
, section
, file
, output
)
687 lang_wild_statement_type
*ptr
;
689 lang_input_statement_type
*file
;
690 lang_output_section_statement_type
*output
;
692 if (file
->just_syms_flag
== false)
694 register asection
*s
;
696 for (s
= file
->the_bfd
->sections
; s
!= NULL
; s
= s
->next
)
698 /* Attach all sections named SECTION. If SECTION is NULL,
699 then attach all sections.
701 Previously, if SECTION was NULL, this code did not call
702 wild_doit if the SEC_IS_COMMON flag was set for the
703 section. I did not understand that, and I took it out.
707 || strcmp (bfd_get_section_name (file
->the_bfd
, s
),
709 wild_doit (&ptr
->children
, s
, output
, file
);
714 /* This is passed a file name which must have been seen already and
715 added to the statement tree. We will see if it has been opened
716 already and had its symbols read. If not then we'll read it. */
718 static lang_input_statement_type
*
722 lang_input_statement_type
*search
;
724 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
725 search
!= (lang_input_statement_type
*) NULL
;
726 search
= (lang_input_statement_type
*) search
->next_real_file
)
728 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
730 if (search
->filename
!= (char *) NULL
731 && name
!= (char *) NULL
732 && strcmp (search
->filename
, name
) == 0)
736 if (search
== (lang_input_statement_type
*) NULL
)
737 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
,
740 /* If we have already added this file, or this file is not real
741 (FIXME: can that ever actually happen?) or the name is NULL
742 (FIXME: can that ever actually happen?) don't add this file. */
745 || search
->filename
== (const char *) NULL
)
748 load_symbols (search
, (lang_statement_list_type
*) NULL
);
753 /* Get the symbols for an input file. */
756 load_symbols (entry
, place
)
757 lang_input_statement_type
*entry
;
758 lang_statement_list_type
*place
;
765 ldfile_open_file (entry
);
767 if (! bfd_check_format (entry
->the_bfd
, bfd_archive
)
768 && ! bfd_check_format_matches (entry
->the_bfd
, bfd_object
, &matching
))
771 lang_statement_list_type
*hold
;
773 err
= bfd_get_error ();
774 if (err
== bfd_error_file_ambiguously_recognized
)
778 einfo ("%B: file not recognized: %E\n", entry
->the_bfd
);
779 einfo ("%B: matching formats:", entry
->the_bfd
);
780 for (p
= matching
; *p
!= NULL
; p
++)
784 else if (err
!= bfd_error_file_not_recognized
786 einfo ("%F%B: file not recognized: %E\n", entry
->the_bfd
);
788 bfd_close (entry
->the_bfd
);
789 entry
->the_bfd
= NULL
;
791 /* See if the emulation has some special knowledge. */
793 if (ldemul_unrecognized_file (entry
))
796 /* Try to interpret the file as a linker script. */
798 ldfile_open_command_file (entry
->filename
);
803 ldfile_assumed_script
= true;
804 parser_input
= input_script
;
806 ldfile_assumed_script
= false;
813 /* We don't call ldlang_add_file for an archive. Instead, the
814 add_symbols entry point will call ldlang_add_file, via the
815 add_archive_element callback, for each element of the archive
817 switch (bfd_get_format (entry
->the_bfd
))
823 ldlang_add_file (entry
);
824 if (trace_files
|| trace_file_tries
)
825 info_msg ("%I\n", entry
);
829 if (entry
->whole_archive
)
831 bfd
*member
= bfd_openr_next_archived_file (entry
->the_bfd
,
833 while (member
!= NULL
)
835 if (! bfd_check_format (member
, bfd_object
))
836 einfo ("%F%B: object %B in archive is not object\n",
837 entry
->the_bfd
, member
);
838 if (! ((*link_info
.callbacks
->add_archive_element
)
839 (&link_info
, member
, "-whole-archive")))
841 if (! bfd_link_add_symbols (member
, &link_info
))
842 einfo ("%F%B: could not read symbols: %E\n", member
);
843 member
= bfd_openr_next_archived_file (entry
->the_bfd
,
847 entry
->loaded
= true;
853 if (! bfd_link_add_symbols (entry
->the_bfd
, &link_info
))
854 einfo ("%F%B: could not read symbols: %E\n", entry
->the_bfd
);
856 entry
->loaded
= true;
859 /* Handle a wild statement. SECTION or FILE or both may be NULL,
860 indicating that it is a wildcard. Separate lang_input_section
861 statements are created for each part of the expansion; they are
862 added after the wild statement S. OUTPUT is the output section. */
865 wild (s
, section
, file
, target
, output
)
866 lang_wild_statement_type
*s
;
870 lang_output_section_statement_type
*output
;
872 lang_input_statement_type
*f
;
874 if (file
== (char *) NULL
)
876 /* Perform the iteration over all files in the list */
877 for (f
= (lang_input_statement_type
*) file_chain
.head
;
878 f
!= (lang_input_statement_type
*) NULL
;
879 f
= (lang_input_statement_type
*) f
->next
)
881 wild_section (s
, section
, f
, output
);
886 /* Perform the iteration over a single file */
887 f
= lookup_name (file
);
888 if (f
->the_bfd
== NULL
889 || ! bfd_check_format (f
->the_bfd
, bfd_archive
))
890 wild_section (s
, section
, f
, output
);
895 /* This is an archive file. We must map each member of the
896 archive separately. */
897 member
= bfd_openr_next_archived_file (f
->the_bfd
, (bfd
*) NULL
);
898 while (member
!= NULL
)
900 /* When lookup_name is called, it will call the
901 add_symbols entry point for the archive. For each
902 element of the archive which is included, BFD will
903 call ldlang_add_file, which will set the usrdata
904 field of the member to the lang_input_statement. */
905 if (member
->usrdata
!= NULL
)
907 wild_section (s
, section
,
908 (lang_input_statement_type
*) member
->usrdata
,
912 member
= bfd_openr_next_archived_file (f
->the_bfd
, member
);
917 if (section
!= (char *) NULL
918 && strcmp (section
, "COMMON") == 0
919 && default_common_section
== NULL
)
921 /* Remember the section that common is going to in case we later
922 get something which doesn't know where to put it. */
923 default_common_section
= output
;
927 /* Open the output file. */
935 if (output_target
== (char *) NULL
)
937 if (current_target
!= (char *) NULL
)
938 output_target
= current_target
;
940 output_target
= default_target
;
942 output
= bfd_openw (name
, output_target
);
944 if (output
== (bfd
*) NULL
)
946 if (bfd_get_error () == bfd_error_invalid_target
)
948 einfo ("%P%F: target %s not found\n", output_target
);
950 einfo ("%P%F: cannot open output file %s: %E\n", name
);
953 delete_output_file_on_failure
= 1;
955 /* output->flags |= D_PAGED;*/
957 if (! bfd_set_format (output
, bfd_object
))
958 einfo ("%P%F:%s: can not make object file: %E\n", name
);
959 if (! bfd_set_arch_mach (output
,
960 ldfile_output_architecture
,
961 ldfile_output_machine
))
962 einfo ("%P%F:%s: can not set architecture: %E\n", name
);
964 link_info
.hash
= bfd_link_hash_table_create (output
);
965 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
966 einfo ("%P%F: can not create link hash table: %E\n");
968 bfd_set_gp_size (output
, g_switch_value
);
976 ldlang_open_output (statement
)
977 lang_statement_union_type
* statement
;
979 switch (statement
->header
.type
)
981 case lang_output_statement_enum
:
982 ASSERT (output_bfd
== (bfd
*) NULL
);
983 output_bfd
= open_output (statement
->output_statement
.name
);
984 ldemul_set_output_arch ();
985 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
986 output_bfd
->flags
|= D_PAGED
;
988 output_bfd
->flags
&= ~D_PAGED
;
989 if (config
.text_read_only
)
990 output_bfd
->flags
|= WP_TEXT
;
992 output_bfd
->flags
&= ~WP_TEXT
;
993 if (config
.traditional_format
)
994 output_bfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
996 output_bfd
->flags
&= ~BFD_TRADITIONAL_FORMAT
;
999 case lang_target_statement_enum
:
1000 current_target
= statement
->target_statement
.target
;
1007 /* Open all the input files. */
1010 open_input_bfds (s
, force
)
1011 lang_statement_union_type
*s
;
1014 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1016 switch (s
->header
.type
)
1018 case lang_constructors_statement_enum
:
1019 open_input_bfds (constructor_list
.head
, force
);
1021 case lang_output_section_statement_enum
:
1022 open_input_bfds (s
->output_section_statement
.children
.head
, force
);
1024 case lang_wild_statement_enum
:
1025 /* Maybe we should load the file's symbols */
1026 if (s
->wild_statement
.filename
)
1027 (void) lookup_name (s
->wild_statement
.filename
);
1028 open_input_bfds (s
->wild_statement
.children
.head
, force
);
1030 case lang_group_statement_enum
:
1032 struct bfd_link_hash_entry
*undefs
;
1034 /* We must continually search the entries in the group
1035 until no new symbols are added to the list of undefined
1040 undefs
= link_info
.hash
->undefs_tail
;
1041 open_input_bfds (s
->group_statement
.children
.head
, true);
1043 while (undefs
!= link_info
.hash
->undefs_tail
);
1046 case lang_target_statement_enum
:
1047 current_target
= s
->target_statement
.target
;
1049 case lang_input_statement_enum
:
1050 if (s
->input_statement
.real
== true)
1052 lang_statement_list_type add
;
1054 s
->input_statement
.target
= current_target
;
1056 /* If we are being called from within a group, and this
1057 is an archive which has already been searched, then
1058 force it to be researched. */
1060 && s
->input_statement
.loaded
1061 && bfd_check_format (s
->input_statement
.the_bfd
,
1063 s
->input_statement
.loaded
= false;
1065 lang_list_init (&add
);
1067 load_symbols (&s
->input_statement
, &add
);
1069 if (add
.head
!= NULL
)
1071 *add
.tail
= s
->next
;
1082 /* If there are [COMMONS] statements, put a wild one into the bss section */
1085 lang_reasonable_defaults ()
1088 lang_output_section_statement_lookup (".text");
1089 lang_output_section_statement_lookup (".data");
1091 default_common_section
=
1092 lang_output_section_statement_lookup (".bss");
1095 if (placed_commons
== false)
1097 lang_wild_statement_type
*new =
1098 new_stat (lang_wild_statement
,
1099 &default_common_section
->children
);
1101 new->section_name
= "COMMON";
1102 new->filename
= (char *) NULL
;
1103 lang_list_init (&new->children
);
1110 Add the supplied name to the symbol table as an undefined reference.
1111 Remove items from the chain as we open input bfds
1113 typedef struct ldlang_undef_chain_list
1115 struct ldlang_undef_chain_list
*next
;
1117 } ldlang_undef_chain_list_type
;
1119 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
1122 ldlang_add_undef (name
)
1123 CONST
char *CONST name
;
1125 ldlang_undef_chain_list_type
*new =
1126 ((ldlang_undef_chain_list_type
*)
1127 stat_alloc (sizeof (ldlang_undef_chain_list_type
)));
1129 new->next
= ldlang_undef_chain_list_head
;
1130 ldlang_undef_chain_list_head
= new;
1132 new->name
= buystring (name
);
1135 /* Run through the list of undefineds created above and place them
1136 into the linker hash table as undefined symbols belonging to the
1140 lang_place_undefineds ()
1142 ldlang_undef_chain_list_type
*ptr
;
1144 for (ptr
= ldlang_undef_chain_list_head
;
1145 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1148 struct bfd_link_hash_entry
*h
;
1150 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1151 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1152 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1153 if (h
->type
== bfd_link_hash_new
)
1155 h
->type
= bfd_link_hash_undefined
;
1156 h
->u
.undef
.abfd
= NULL
;
1157 bfd_link_add_undef (link_info
.hash
, h
);
1162 /* Open input files and attatch to output sections */
1164 map_input_to_output_sections (s
, target
, output_section_statement
)
1165 lang_statement_union_type
* s
;
1167 lang_output_section_statement_type
* output_section_statement
;
1169 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1171 switch (s
->header
.type
)
1175 case lang_wild_statement_enum
:
1176 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1177 s
->wild_statement
.filename
, target
,
1178 output_section_statement
);
1181 case lang_constructors_statement_enum
:
1182 map_input_to_output_sections (constructor_list
.head
,
1184 output_section_statement
);
1186 case lang_output_section_statement_enum
:
1187 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1189 &s
->output_section_statement
);
1191 case lang_output_statement_enum
:
1193 case lang_target_statement_enum
:
1194 target
= s
->target_statement
.target
;
1196 case lang_group_statement_enum
:
1197 map_input_to_output_sections (s
->group_statement
.children
.head
,
1199 output_section_statement
);
1201 case lang_fill_statement_enum
:
1202 case lang_input_section_enum
:
1203 case lang_object_symbols_statement_enum
:
1204 case lang_data_statement_enum
:
1205 case lang_reloc_statement_enum
:
1206 case lang_assignment_statement_enum
:
1207 case lang_padding_statement_enum
:
1208 case lang_input_statement_enum
:
1209 if (output_section_statement
!= NULL
1210 && output_section_statement
->bfd_section
== NULL
)
1211 init_os (output_section_statement
);
1213 case lang_afile_asection_pair_statement_enum
:
1216 case lang_address_statement_enum
:
1217 /* Mark the specified section with the supplied address */
1219 lang_output_section_statement_type
*os
=
1220 lang_output_section_statement_lookup
1221 (s
->address_statement
.section_name
);
1223 if (os
->bfd_section
== NULL
)
1225 os
->addr_tree
= s
->address_statement
.address
;
1233 print_output_section_statement (output_section_statement
)
1234 lang_output_section_statement_type
* output_section_statement
;
1236 asection
*section
= output_section_statement
->bfd_section
;
1239 print_section (output_section_statement
->name
);
1244 print_dot
= section
->vma
;
1248 print_address (section
->vma
);
1250 print_size (section
->_raw_size
);
1252 print_size(section
->_cooked_size
);
1254 print_alignment (section
->alignment_power
);
1257 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1258 print_flags (stdout
, &output_section_statement
->flags
);
1260 if (section
->flags
& SEC_LOAD
)
1261 fprintf (config
.map_file
, "load ");
1262 if (section
->flags
& SEC_ALLOC
)
1263 fprintf (config
.map_file
, "alloc ");
1264 if (section
->flags
& SEC_RELOC
)
1265 fprintf (config
.map_file
, "reloc ");
1266 if (section
->flags
& SEC_HAS_CONTENTS
)
1267 fprintf (config
.map_file
, "contents ");
1272 fprintf (config
.map_file
, " (no attached output section)");
1275 if (output_section_statement
->load_base
)
1277 bfd_vma b
= exp_get_abs_int(output_section_statement
->load_base
,
1278 0, "output base", lang_final_phase_enum
);
1279 fprintf (config
.map_file
, "Output address ");
1280 fprintf_vma (config
.map_file
, b
);
1281 fprintf (config
.map_file
, "\n");
1283 if (output_section_statement
->section_alignment
>= 0
1284 || output_section_statement
->subsection_alignment
>= 0)
1286 fprintf (config
.map_file
, "\t\t\t\t\tforced alignment ");
1287 if (output_section_statement
->section_alignment
>= 0)
1289 fprintf (config
.map_file
, "section 2**%d ",output_section_statement
->section_alignment
);
1291 if ( output_section_statement
->subsection_alignment
>= 0)
1293 fprintf (config
.map_file
, "subsection 2**%d ",output_section_statement
->subsection_alignment
);
1298 print_statement_list (output_section_statement
->children
.head
,
1299 output_section_statement
);
1304 print_assignment (assignment
, output_section
)
1305 lang_assignment_statement_type
* assignment
;
1306 lang_output_section_statement_type
* output_section
;
1308 etree_value_type result
;
1314 print_address (print_dot
);
1316 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1318 lang_final_phase_enum
,
1324 print_address (result
.value
);
1328 fprintf (config
.map_file
, "*undefined*");
1331 exp_print_tree (assignment
->exp
);
1333 fprintf (config
.map_file
, "\n");
1337 print_input_statement (statm
)
1338 lang_input_statement_type
* statm
;
1340 if (statm
->filename
!= (char *) NULL
)
1342 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1346 /* Print all the defined symbols for the abfd provided by in the supplied
1351 print_one_symbol (hash_entry
, ptr
)
1352 struct bfd_link_hash_entry
*hash_entry
;
1355 asection
* sec
= (asection
*)ptr
;
1357 if (hash_entry
->type
== bfd_link_hash_defined
1358 || hash_entry
->type
== bfd_link_hash_defweak
)
1360 if (sec
== hash_entry
->u
.def
.section
) {
1362 fprintf (config
.map_file
, " ");
1364 fprintf (config
.map_file
, " ");
1365 print_address (hash_entry
->u
.def
.value
+ outside_section_address (sec
));
1366 fprintf (config
.map_file
, " %s", hash_entry
->root
.string
);
1375 print_input_section (in
)
1376 lang_input_section_type
* in
;
1378 asection
*i
= in
->section
;
1379 bfd_size_type size
= i
->_cooked_size
!= 0 ? i
->_cooked_size
: i
->_raw_size
;
1384 fprintf (config
.map_file
, " ");
1385 print_section (i
->name
);
1386 fprintf (config
.map_file
, " ");
1387 if (i
->output_section
)
1389 print_address (i
->output_section
->vma
+ i
->output_offset
);
1390 fprintf (config
.map_file
, " ");
1391 print_size (i
->_raw_size
);
1392 fprintf (config
.map_file
, " ");
1393 print_size(i
->_cooked_size
);
1394 fprintf (config
.map_file
, " ");
1395 print_alignment (i
->alignment_power
);
1396 fprintf (config
.map_file
, " ");
1400 bfd
*abfd
= in
->ifile
->the_bfd
;
1402 if (in
->ifile
->just_syms_flag
== true)
1404 fprintf (config
.map_file
, "symbols only ");
1407 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1408 if (abfd
->my_archive
!= (bfd
*) NULL
)
1410 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1415 fprintf (config
.map_file
, "%s", abfd
->filename
);
1417 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1420 /* Print all the symbols */
1421 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
1429 print_dot
= outside_section_address (i
) + size
;
1433 fprintf (config
.map_file
, "No output section allocated\n");
1439 print_fill_statement (fill
)
1440 lang_fill_statement_type
* fill
;
1442 fprintf (config
.map_file
, "FILL mask ");
1443 print_fill (fill
->fill
);
1447 print_data_statement (data
)
1448 lang_data_statement_type
* data
;
1450 /* bfd_vma value; */
1455 /* ASSERT(print_dot == data->output_vma);*/
1457 /* data->output_section may be NULL if called from gdb. */
1458 if (data
->output_section
)
1459 print_address (data
->output_vma
+ data
->output_section
->vma
);
1461 print_address (data
->output_vma
);
1463 print_address (data
->value
);
1468 fprintf (config
.map_file
, "BYTE ");
1469 print_dot
+= BYTE_SIZE
;
1472 fprintf (config
.map_file
, "SHORT ");
1473 print_dot
+= SHORT_SIZE
;
1476 fprintf (config
.map_file
, "LONG ");
1477 print_dot
+= LONG_SIZE
;
1480 fprintf (config
.map_file
, "QUAD ");
1481 print_dot
+= QUAD_SIZE
;
1485 exp_print_tree (data
->exp
);
1487 fprintf (config
.map_file
, "\n");
1490 /* Print a reloc statement. */
1493 print_reloc_statement (reloc
)
1494 lang_reloc_statement_type
*reloc
;
1501 /* ASSERT(print_dot == data->output_vma);*/
1503 /* reloc->output_section may be NULL if called from gdb. */
1504 if (reloc
->output_section
)
1505 print_address (reloc
->output_vma
+ reloc
->output_section
->vma
);
1507 print_address (reloc
->output_vma
);
1509 print_address (reloc
->addend_value
);
1512 fprintf (config
.map_file
, "RELOC %s ", reloc
->howto
->name
);
1514 print_dot
+= bfd_get_reloc_size (reloc
->howto
);
1516 exp_print_tree (reloc
->addend_exp
);
1518 fprintf (config
.map_file
, "\n");
1522 print_padding_statement (s
)
1523 lang_padding_statement_type
* s
;
1527 print_section ("*fill*");
1529 /* s->output_section may be NULL if called from gdb. */
1530 if (s
->output_section
)
1531 print_address (s
->output_offset
+ s
->output_section
->vma
);
1533 print_address (s
->output_offset
);
1535 print_size (s
->size
);
1537 print_fill (s
->fill
);
1540 print_dot
= s
->output_offset
+ s
->size
;
1541 /* s->output_section may be NULL if called from gdb. */
1542 if (s
->output_section
)
1543 print_dot
+= s
->output_section
->vma
;
1547 print_wild_statement (w
, os
)
1548 lang_wild_statement_type
* w
;
1549 lang_output_section_statement_type
* os
;
1551 fprintf (config
.map_file
, " from ");
1552 if (w
->filename
!= (char *) NULL
)
1554 fprintf (config
.map_file
, "%s", w
->filename
);
1558 fprintf (config
.map_file
, "*");
1560 if (w
->section_name
!= (char *) NULL
)
1562 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1566 fprintf (config
.map_file
, "(*)");
1569 print_statement_list (w
->children
.head
, os
);
1573 /* Print a group statement. */
1577 lang_group_statement_type
*s
;
1578 lang_output_section_statement_type
*os
;
1580 fprintf (config
.map_file
, "START GROUP\n");
1581 print_statement_list (s
->children
.head
, os
);
1582 fprintf (config
.map_file
, "END GROUP\n");
1585 /* Print the list of statements in S.
1586 This can be called for any statement type. */
1589 print_statement_list (s
, os
)
1590 lang_statement_union_type
* s
;
1591 lang_output_section_statement_type
* os
;
1595 print_statement (s
, os
);
1600 /* Print the first statement in statement list S.
1601 This can be called for any statement type. */
1604 print_statement (s
, os
)
1605 lang_statement_union_type
* s
;
1606 lang_output_section_statement_type
* os
;
1608 switch (s
->header
.type
)
1610 case lang_constructors_statement_enum
:
1611 fprintf (config
.map_file
, "constructors:\n");
1612 print_statement_list (constructor_list
.head
, os
);
1614 case lang_wild_statement_enum
:
1615 print_wild_statement (&s
->wild_statement
, os
);
1618 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1621 case lang_address_statement_enum
:
1622 fprintf (config
.map_file
, "address\n");
1624 case lang_object_symbols_statement_enum
:
1625 fprintf (config
.map_file
, "object symbols\n");
1627 case lang_fill_statement_enum
:
1628 print_fill_statement (&s
->fill_statement
);
1630 case lang_data_statement_enum
:
1631 print_data_statement (&s
->data_statement
);
1633 case lang_reloc_statement_enum
:
1634 print_reloc_statement (&s
->reloc_statement
);
1636 case lang_input_section_enum
:
1637 print_input_section (&s
->input_section
);
1639 case lang_padding_statement_enum
:
1640 print_padding_statement (&s
->padding_statement
);
1642 case lang_output_section_statement_enum
:
1643 print_output_section_statement (&s
->output_section_statement
);
1645 case lang_assignment_statement_enum
:
1646 print_assignment (&s
->assignment_statement
,
1649 case lang_target_statement_enum
:
1650 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1652 case lang_output_statement_enum
:
1653 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1654 s
->output_statement
.name
,
1655 output_target
? output_target
: "");
1657 case lang_input_statement_enum
:
1658 print_input_statement (&s
->input_statement
);
1660 case lang_group_statement_enum
:
1661 print_group (&s
->group_statement
, os
);
1663 case lang_afile_asection_pair_statement_enum
:
1673 print_statement_list (statement_list
.head
,
1674 abs_output_section
);
1678 /* Print the first N statements in statement list S to STDERR.
1679 If N == 0, nothing is printed.
1680 If N < 0, the entire list is printed.
1681 Intended to be called from GDB. */
1684 dprint_statement (s
, n
)
1685 lang_statement_union_type
* s
;
1688 FILE *map_save
= config
.map_file
;
1690 config
.map_file
= stderr
;
1693 print_statement_list (s
, abs_output_section
);
1698 print_statement (s
, abs_output_section
);
1703 config
.map_file
= map_save
;
1707 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
1708 lang_statement_union_type
** this_ptr
;
1711 asection
* output_section_statement
;
1714 /* Align this section first to the
1715 input sections requirement, then
1716 to the output section's requirement.
1717 If this alignment is > than any seen before,
1718 then record it too. Perform the alignment by
1719 inserting a magic 'padding' statement.
1722 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1724 if (alignment_needed
!= 0)
1726 lang_statement_union_type
*new =
1727 ((lang_statement_union_type
*)
1728 stat_alloc (sizeof (lang_padding_statement_type
)));
1730 /* Link into existing chain */
1731 new->header
.next
= *this_ptr
;
1733 new->header
.type
= lang_padding_statement_enum
;
1734 new->padding_statement
.output_section
= output_section_statement
;
1735 new->padding_statement
.output_offset
=
1736 dot
- output_section_statement
->vma
;
1737 new->padding_statement
.fill
= fill
;
1738 new->padding_statement
.size
= alignment_needed
;
1742 /* Remember the most restrictive alignment */
1743 if (power
> output_section_statement
->alignment_power
)
1745 output_section_statement
->alignment_power
= power
;
1747 output_section_statement
->_raw_size
+= alignment_needed
;
1748 return alignment_needed
+ dot
;
1752 /* Work out how much this section will move the dot point */
1754 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
1755 lang_statement_union_type
** this_ptr
;
1756 lang_output_section_statement_type
* output_section_statement
;
1761 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1762 asection
*i
= is
->section
;
1764 if (is
->ifile
->just_syms_flag
== false)
1766 if (output_section_statement
->subsection_alignment
!= -1)
1767 i
->alignment_power
=
1768 output_section_statement
->subsection_alignment
;
1770 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1771 output_section_statement
->bfd_section
, dot
);
1773 /* Remember where in the output section this input section goes */
1775 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1777 /* Mark how big the output section must be to contain this now
1779 if (i
->_cooked_size
!= 0)
1780 dot
+= i
->_cooked_size
;
1782 dot
+= i
->_raw_size
;
1783 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1787 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1793 /* This variable indicates whether bfd_relax_section should be called
1796 static boolean relax_again
;
1798 /* Set the sizes for all the output sections. */
1801 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
1802 lang_statement_union_type
* s
;
1803 lang_output_section_statement_type
* output_section_statement
;
1804 lang_statement_union_type
** prev
;
1809 /* Size up the sections from their constituent parts */
1810 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1812 switch (s
->header
.type
)
1815 case lang_output_section_statement_enum
:
1818 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1820 if (os
->bfd_section
== NULL
)
1822 /* This section was never actually created. */
1826 /* If this is a COFF shared library section, use the size and
1827 address from the input section. FIXME: This is COFF
1828 specific; it would be cleaner if there were some other way
1829 to do this, but nothing simple comes to mind. */
1830 if ((os
->bfd_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
1834 if (os
->children
.head
== NULL
1835 || os
->children
.head
->next
!= NULL
1836 || os
->children
.head
->header
.type
!= lang_input_section_enum
)
1837 einfo ("%P%X: Internal error on COFF shared library section %s",
1840 input
= os
->children
.head
->input_section
.section
;
1841 bfd_set_section_vma (os
->bfd_section
->owner
,
1843 bfd_section_vma (input
->owner
, input
));
1844 os
->bfd_section
->_raw_size
= input
->_raw_size
;
1848 if (bfd_is_abs_section (os
->bfd_section
))
1850 /* No matter what happens, an abs section starts at zero */
1851 ASSERT (os
->bfd_section
->vma
== 0);
1855 if (os
->addr_tree
== (etree_type
*) NULL
)
1857 /* No address specified for this section, get one
1858 from the region specification
1860 if (os
->region
== (lang_memory_region_type
*) NULL
)
1862 os
->region
= lang_memory_region_lookup ("*default*");
1864 dot
= os
->region
->current
;
1865 if (os
->section_alignment
== -1)
1866 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1872 r
= exp_fold_tree (os
->addr_tree
,
1874 lang_allocating_phase_enum
,
1876 if (r
.valid
== false)
1878 einfo ("%F%S: non constant address expression for section %s\n",
1883 /* The section starts here */
1884 /* First, align to what the section needs */
1886 if (os
->section_alignment
!= -1)
1887 dot
= align_power (dot
, os
->section_alignment
);
1889 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1891 os
->bfd_section
->output_offset
= 0;
1894 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1895 os
->fill
, dot
, relax
);
1896 /* Ignore the size of the input sections, use the vma and size to */
1899 after
= ALIGN_N (os
->bfd_section
->vma
+
1900 os
->bfd_section
->_raw_size
,
1901 /* The coercion here is important, see ld.h. */
1902 (bfd_vma
) os
->block_value
);
1904 if (bfd_is_abs_section (os
->bfd_section
))
1905 ASSERT (after
== os
->bfd_section
->vma
);
1907 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1908 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1909 os
->processed
= true;
1911 /* Replace into region ? */
1912 if (os
->region
!= (lang_memory_region_type
*) NULL
)
1914 os
->region
->current
= dot
;
1915 /* Make sure this isn't silly. */
1916 if ((os
->region
->current
< os
->region
->origin
)
1917 || (os
->region
->current
1918 > os
->region
->origin
+ os
->region
->length
))
1920 if (os
->addr_tree
!= (etree_type
*) NULL
)
1922 einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
1923 os
->region
->current
,
1924 os
->bfd_section
->owner
,
1925 os
->bfd_section
->name
,
1930 einfo ("%X%P: region %s is full (%B section %s)\n",
1932 os
->bfd_section
->owner
,
1933 os
->bfd_section
->name
);
1935 /* Reset the region pointer. */
1936 os
->region
->current
= os
->region
->origin
;
1942 case lang_constructors_statement_enum
:
1943 dot
= lang_size_sections (constructor_list
.head
,
1944 output_section_statement
,
1945 &s
->wild_statement
.children
.head
,
1950 case lang_data_statement_enum
:
1952 unsigned int size
= 0;
1954 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1955 s
->data_statement
.output_section
=
1956 output_section_statement
->bfd_section
;
1958 switch (s
->data_statement
.type
)
1975 output_section_statement
->bfd_section
->_raw_size
+= size
;
1976 /* The output section gets contents, and then we inspect for
1977 any flags set in the input script which override any ALLOC */
1978 output_section_statement
->bfd_section
->flags
|= SEC_HAS_CONTENTS
;
1979 if (!(output_section_statement
->flags
& SEC_NEVER_LOAD
)) {
1980 output_section_statement
->bfd_section
->flags
|= SEC_ALLOC
| SEC_LOAD
;
1985 case lang_reloc_statement_enum
:
1989 s
->reloc_statement
.output_vma
=
1990 dot
- output_section_statement
->bfd_section
->vma
;
1991 s
->reloc_statement
.output_section
=
1992 output_section_statement
->bfd_section
;
1993 size
= bfd_get_reloc_size (s
->reloc_statement
.howto
);
1995 output_section_statement
->bfd_section
->_raw_size
+= size
;
1999 case lang_wild_statement_enum
:
2001 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
2002 output_section_statement
,
2003 &s
->wild_statement
.children
.head
,
2009 case lang_object_symbols_statement_enum
:
2010 link_info
.create_object_symbols_section
=
2011 output_section_statement
->bfd_section
;
2013 case lang_output_statement_enum
:
2014 case lang_target_statement_enum
:
2016 case lang_input_section_enum
:
2020 i
= (*prev
)->input_section
.section
;
2022 i
->_cooked_size
= i
->_raw_size
;
2027 if (! bfd_relax_section (i
->owner
, i
, &link_info
, &again
))
2028 einfo ("%P%F: can't relax section: %E\n");
2032 dot
= size_input_section (prev
,
2033 output_section_statement
,
2034 output_section_statement
->fill
,
2038 case lang_input_statement_enum
:
2040 case lang_fill_statement_enum
:
2041 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
2043 fill
= s
->fill_statement
.fill
;
2045 case lang_assignment_statement_enum
:
2047 bfd_vma newdot
= dot
;
2049 exp_fold_tree (s
->assignment_statement
.exp
,
2050 output_section_statement
,
2051 lang_allocating_phase_enum
,
2055 if (newdot
!= dot
&& !relax
)
2057 /* The assignment changed dot. Insert a pad. */
2058 if (output_section_statement
== abs_output_section
)
2060 /* If we don't have an output section, then just adjust
2061 the default memory address. */
2062 lang_memory_region_lookup ("*default*")->current
= newdot
;
2066 lang_statement_union_type
*new =
2067 ((lang_statement_union_type
*)
2068 stat_alloc (sizeof (lang_padding_statement_type
)));
2070 /* Link into existing chain */
2071 new->header
.next
= *prev
;
2073 new->header
.type
= lang_padding_statement_enum
;
2074 new->padding_statement
.output_section
=
2075 output_section_statement
->bfd_section
;
2076 new->padding_statement
.output_offset
=
2077 dot
- output_section_statement
->bfd_section
->vma
;
2078 new->padding_statement
.fill
= fill
;
2079 new->padding_statement
.size
= newdot
- dot
;
2080 output_section_statement
->bfd_section
->_raw_size
+=
2081 new->padding_statement
.size
;
2089 case lang_padding_statement_enum
:
2090 /* If we are relaxing, and this is not the first pass, some
2091 padding statements may have been inserted during previous
2092 passes. We may have to move the padding statement to a new
2093 location if dot has a different value at this point in this
2094 pass than it did at this point in the previous pass. */
2095 s
->padding_statement
.output_offset
=
2096 dot
- output_section_statement
->bfd_section
->vma
;
2097 dot
+= s
->padding_statement
.size
;
2098 output_section_statement
->bfd_section
->_raw_size
+=
2099 s
->padding_statement
.size
;
2102 case lang_group_statement_enum
:
2103 dot
= lang_size_sections (s
->group_statement
.children
.head
,
2104 output_section_statement
,
2105 &s
->group_statement
.children
.head
,
2113 /* This can only get here when relaxing is turned on */
2115 case lang_address_statement_enum
:
2118 prev
= &s
->header
.next
;
2124 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
2125 lang_statement_union_type
* s
;
2126 lang_output_section_statement_type
* output_section_statement
;
2130 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
2132 switch (s
->header
.type
)
2134 case lang_constructors_statement_enum
:
2135 dot
= lang_do_assignments (constructor_list
.head
,
2136 output_section_statement
,
2141 case lang_output_section_statement_enum
:
2143 lang_output_section_statement_type
*os
=
2144 &(s
->output_section_statement
);
2146 if (os
->bfd_section
!= NULL
)
2148 dot
= os
->bfd_section
->vma
;
2149 (void) lang_do_assignments (os
->children
.head
, os
,
2151 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
2155 /* If nothing has been placed into the output section then
2156 it won't have a bfd_section. */
2157 if (os
->bfd_section
)
2159 os
->bfd_section
->lma
2160 = exp_get_abs_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
2165 case lang_wild_statement_enum
:
2167 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
2168 output_section_statement
,
2173 case lang_object_symbols_statement_enum
:
2174 case lang_output_statement_enum
:
2175 case lang_target_statement_enum
:
2177 case lang_common_statement_enum
:
2180 case lang_data_statement_enum
:
2182 etree_value_type value
;
2184 value
= exp_fold_tree (s
->data_statement
.exp
,
2186 lang_final_phase_enum
, dot
, &dot
);
2187 s
->data_statement
.value
= value
.value
;
2188 if (value
.valid
== false)
2189 einfo ("%F%P: invalid data statement\n");
2191 switch (s
->data_statement
.type
)
2208 case lang_reloc_statement_enum
:
2210 etree_value_type value
;
2212 value
= exp_fold_tree (s
->reloc_statement
.addend_exp
,
2214 lang_final_phase_enum
, dot
, &dot
);
2215 s
->reloc_statement
.addend_value
= value
.value
;
2216 if (value
.valid
== false)
2217 einfo ("%F%P: invalid reloc statement\n");
2219 dot
+= bfd_get_reloc_size (s
->reloc_statement
.howto
);
2222 case lang_input_section_enum
:
2224 asection
*in
= s
->input_section
.section
;
2226 if (in
->_cooked_size
!= 0)
2227 dot
+= in
->_cooked_size
;
2229 dot
+= in
->_raw_size
;
2233 case lang_input_statement_enum
:
2235 case lang_fill_statement_enum
:
2236 fill
= s
->fill_statement
.fill
;
2238 case lang_assignment_statement_enum
:
2240 exp_fold_tree (s
->assignment_statement
.exp
,
2241 output_section_statement
,
2242 lang_final_phase_enum
,
2248 case lang_padding_statement_enum
:
2249 dot
+= s
->padding_statement
.size
;
2252 case lang_group_statement_enum
:
2253 dot
= lang_do_assignments (s
->group_statement
.children
.head
,
2254 output_section_statement
,
2262 case lang_address_statement_enum
:
2270 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2271 operator .startof. (section_name), it produces an undefined symbol
2272 .startof.section_name. Similarly, when it sees
2273 .sizeof. (section_name), it produces an undefined symbol
2274 .sizeof.section_name. For all the output sections, we look for
2275 such symbols, and set them to the correct value. */
2282 if (link_info
.relocateable
)
2285 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
2287 const char *secname
;
2289 struct bfd_link_hash_entry
*h
;
2291 secname
= bfd_get_section_name (output_bfd
, s
);
2292 buf
= xmalloc (10 + strlen (secname
));
2294 sprintf (buf
, ".startof.%s", secname
);
2295 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
2296 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
2298 h
->type
= bfd_link_hash_defined
;
2299 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, s
);
2300 h
->u
.def
.section
= bfd_abs_section_ptr
;
2303 sprintf (buf
, ".sizeof.%s", secname
);
2304 h
= bfd_link_hash_lookup (link_info
.hash
, buf
, false, false, true);
2305 if (h
!= NULL
&& h
->type
== bfd_link_hash_undefined
)
2307 h
->type
= bfd_link_hash_defined
;
2308 if (s
->_cooked_size
!= 0)
2309 h
->u
.def
.value
= s
->_cooked_size
;
2311 h
->u
.def
.value
= s
->_raw_size
;
2312 h
->u
.def
.section
= bfd_abs_section_ptr
;
2322 struct bfd_link_hash_entry
*h
;
2325 if (link_info
.relocateable
|| link_info
.shared
)
2330 if (entry_symbol
== (char *) NULL
)
2332 /* No entry has been specified. Look for start, but don't warn
2333 if we don't find it. */
2334 entry_symbol
= "start";
2338 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
2339 if (h
!= (struct bfd_link_hash_entry
*) NULL
2340 && (h
->type
== bfd_link_hash_defined
2341 || h
->type
== bfd_link_hash_defweak
)
2342 && h
->u
.def
.section
->output_section
!= NULL
)
2346 val
= (h
->u
.def
.value
2347 + bfd_get_section_vma (output_bfd
,
2348 h
->u
.def
.section
->output_section
)
2349 + h
->u
.def
.section
->output_offset
);
2350 if (! bfd_set_start_address (output_bfd
, val
))
2351 einfo ("%P%F:%s: can't set start address\n", entry_symbol
);
2357 /* Can't find the entry symbol. Use the first address in the
2359 ts
= bfd_get_section_by_name (output_bfd
, ".text");
2360 if (ts
!= (asection
*) NULL
)
2363 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2364 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
2365 if (! bfd_set_start_address (output_bfd
,
2366 bfd_get_section_vma (output_bfd
, ts
)))
2367 einfo ("%P%F: can't set start address\n");
2372 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2378 /* Check that the architecture of all the input files is compatible
2379 with the output file. Also call the backend to let it do any
2380 other checking that is needed. */
2385 lang_statement_union_type
*file
;
2387 CONST bfd_arch_info_type
*compatible
;
2389 for (file
= file_chain
.head
;
2390 file
!= (lang_statement_union_type
*) NULL
;
2391 file
= file
->input_statement
.next
)
2393 input_bfd
= file
->input_statement
.the_bfd
;
2394 compatible
= bfd_arch_get_compatible (input_bfd
,
2396 if (compatible
== NULL
)
2397 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2398 bfd_printable_name (input_bfd
), input_bfd
,
2399 bfd_printable_name (output_bfd
));
2402 bfd_merge_private_bfd_data (input_bfd
, output_bfd
);
2406 /* Look through all the global common symbols and attach them to the
2407 correct section. The -sort-common command line switch may be used
2408 to roughly sort the entries by size. */
2413 if (link_info
.relocateable
2414 && ! command_line
.force_common_definition
)
2417 if (! config
.sort_common
)
2418 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
2423 for (power
= 4; power
>= 0; power
--)
2424 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
2429 /* Place one common symbol in the correct section. */
2432 lang_one_common (h
, info
)
2433 struct bfd_link_hash_entry
*h
;
2436 unsigned int power_of_two
;
2440 if (h
->type
!= bfd_link_hash_common
)
2444 power_of_two
= h
->u
.c
.p
->alignment_power
;
2446 if (config
.sort_common
2447 && power_of_two
< (unsigned int) *(int *) info
)
2450 section
= h
->u
.c
.p
->section
;
2452 /* Increase the size of the section. */
2453 section
->_raw_size
= ALIGN_N (section
->_raw_size
,
2454 (bfd_size_type
) (1 << power_of_two
));
2456 /* Adjust the alignment if necessary. */
2457 if (power_of_two
> section
->alignment_power
)
2458 section
->alignment_power
= power_of_two
;
2460 /* Change the symbol from common to defined. */
2461 h
->type
= bfd_link_hash_defined
;
2462 h
->u
.def
.section
= section
;
2463 h
->u
.def
.value
= section
->_raw_size
;
2465 /* Increase the size of the section. */
2466 section
->_raw_size
+= size
;
2468 /* Make sure the section is allocated in memory. */
2469 section
->flags
|= SEC_ALLOC
;
2471 if (config
.map_file
!= NULL
)
2472 fprintf (config
.map_file
, "Allocating common %s: %lx at %lx %s\n",
2473 h
->root
.string
, (unsigned long) size
,
2474 (unsigned long) h
->u
.def
.value
, section
->owner
->filename
);
2480 run through the input files and ensure that every input
2481 section has somewhere to go. If one is found without
2482 a destination then create an input request and place it
2483 into the statement tree.
2487 lang_place_orphans ()
2489 lang_input_statement_type
*file
;
2491 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2492 file
!= (lang_input_statement_type
*) NULL
;
2493 file
= (lang_input_statement_type
*) file
->next
)
2497 for (s
= file
->the_bfd
->sections
;
2498 s
!= (asection
*) NULL
;
2501 if (s
->output_section
== (asection
*) NULL
)
2503 /* This section of the file is not attatched, root
2504 around for a sensible place for it to go */
2506 if (file
->just_syms_flag
)
2508 /* We are only retrieving symbol values from this
2509 file. We want the symbols to act as though the
2510 values in the file are absolute. */
2511 s
->output_section
= bfd_abs_section_ptr
;
2512 s
->output_offset
= s
->vma
;
2514 else if (file
->common_section
== s
)
2516 /* This is a lonely common section which must
2517 have come from an archive. We attatch to the
2518 section with the wildcard */
2519 if (! link_info
.relocateable
2520 && ! command_line
.force_common_definition
)
2522 if (default_common_section
==
2523 (lang_output_section_statement_type
*) NULL
)
2525 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2527 default_common_section
=
2528 lang_output_section_statement_lookup (".bss");
2531 wild_doit (&default_common_section
->children
, s
,
2532 default_common_section
, file
);
2535 else if (ldemul_place_orphan (file
, s
))
2539 lang_output_section_statement_type
*os
=
2540 lang_output_section_statement_lookup (s
->name
);
2542 wild_doit (&os
->children
, s
, os
, file
);
2551 lang_set_flags (ptr
, flags
)
2555 boolean state
= false;
2570 /* ptr->flag_read = state; */
2573 /* ptr->flag_write = state; */
2576 /* ptr->flag_executable= state;*/
2580 /* ptr->flag_loadable= state;*/
2583 einfo ("%P%F: invalid syntax in flags\n");
2590 /* Call a function on each input file. This function will be called
2591 on an archive, but not on the elements. */
2594 lang_for_each_input_file (func
)
2595 void (*func
) PARAMS ((lang_input_statement_type
*));
2597 lang_input_statement_type
*f
;
2599 for (f
= (lang_input_statement_type
*) input_file_chain
.head
;
2601 f
= (lang_input_statement_type
*) f
->next_real_file
)
2605 /* Call a function on each file. The function will be called on all
2606 the elements of an archive which are included in the link, but will
2607 not be called on the archive file itself. */
2610 lang_for_each_file (func
)
2611 void (*func
) PARAMS ((lang_input_statement_type
*));
2613 lang_input_statement_type
*f
;
2615 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2616 f
!= (lang_input_statement_type
*) NULL
;
2617 f
= (lang_input_statement_type
*) f
->next
)
2628 lang_for_each_input_section (func
)
2629 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
2631 lang_input_statement_type
*f
;
2633 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2634 f
!= (lang_input_statement_type
*) NULL
;
2635 f
= (lang_input_statement_type
*) f
->next
)
2639 for (s
= f
->the_bfd
->sections
;
2640 s
!= (asection
*) NULL
;
2643 func (f
->the_bfd
, s
);
2651 ldlang_add_file (entry
)
2652 lang_input_statement_type
* entry
;
2656 lang_statement_append (&file_chain
,
2657 (lang_statement_union_type
*) entry
,
2660 /* The BFD linker needs to have a list of all input BFDs involved in
2662 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
2663 ASSERT (entry
->the_bfd
!= output_bfd
);
2664 for (pp
= &link_info
.input_bfds
;
2665 *pp
!= (bfd
*) NULL
;
2666 pp
= &(*pp
)->link_next
)
2668 *pp
= entry
->the_bfd
;
2669 entry
->the_bfd
->usrdata
= (PTR
) entry
;
2670 bfd_set_gp_size (entry
->the_bfd
, g_switch_value
);
2674 lang_add_output (name
, from_script
)
2678 /* Make -o on command line override OUTPUT in script. */
2679 if (had_output_filename
== false || !from_script
)
2681 output_filename
= name
;
2682 had_output_filename
= true;
2687 static lang_output_section_statement_type
*current_section
;
2699 for (l
= 0; l
< 32; l
++)
2701 if (i
>= (unsigned int) x
)
2709 lang_enter_output_section_statement (output_section_statement_name
,
2710 address_exp
, flags
, block_value
,
2711 align
, subalign
, ebase
)
2712 const char *output_section_statement_name
;
2713 etree_type
* address_exp
;
2715 bfd_vma block_value
;
2717 etree_type
*subalign
;
2720 lang_output_section_statement_type
*os
;
2724 lang_output_section_statement_lookup (output_section_statement_name
);
2728 /* Add this statement to tree */
2729 /* add_statement(lang_output_section_statement_enum,
2730 output_section_statement);*/
2731 /* Make next things chain into subchain of this */
2733 if (os
->addr_tree
==
2734 (etree_type
*) NULL
)
2740 if (flags
& SEC_NEVER_LOAD
)
2744 os
->block_value
= block_value
? block_value
: 1;
2745 stat_ptr
= &os
->children
;
2747 os
->subsection_alignment
= topower(
2748 exp_get_value_int(subalign
, -1,
2749 "subsection alignment",
2751 os
->section_alignment
= topower(
2752 exp_get_value_int(align
, -1,
2753 "section alignment", 0));
2755 os
->load_base
= ebase
;
2762 lang_output_statement_type
*new =
2763 new_stat (lang_output_statement
, stat_ptr
);
2765 new->name
= output_filename
;
2768 /* Reset the current counters in the regions */
2770 reset_memory_regions ()
2772 lang_memory_region_type
*p
= lang_memory_region_list
;
2774 for (p
= lang_memory_region_list
;
2775 p
!= (lang_memory_region_type
*) NULL
;
2778 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
2779 p
->current
= p
->origin
;
2786 lang_reasonable_defaults ();
2787 current_target
= default_target
;
2789 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2791 ldemul_create_output_section_statements ();
2793 /* Add to the hash table all undefineds on the command line */
2794 lang_place_undefineds ();
2796 /* Create a bfd for each input file */
2797 current_target
= default_target
;
2798 open_input_bfds (statement_list
.head
, false);
2800 ldemul_after_open ();
2802 /* Build all sets based on the information gathered from the input
2804 ldctor_build_sets ();
2806 /* Size up the common data */
2809 /* Run through the contours of the script and attatch input sections
2810 to the correct output sections
2812 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2813 (lang_output_section_statement_type
*) NULL
);
2816 /* Find any sections not attatched explicitly and handle them */
2817 lang_place_orphans ();
2819 ldemul_before_allocation ();
2821 /* We must record the program headers before we try to fix the
2822 section positions, since they will affect SIZEOF_HEADERS. */
2823 lang_record_phdrs ();
2825 /* Now run around and relax if we can */
2826 if (command_line
.relax
)
2828 /* First time round is a trial run to get the 'worst case'
2829 addresses of the objects if there was no relaxing. */
2830 lang_size_sections (statement_list
.head
,
2832 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2834 /* Keep relaxing until bfd_relax_section gives up. */
2837 reset_memory_regions ();
2839 relax_again
= false;
2841 /* Do all the assignments with our current guesses as to
2843 lang_do_assignments (statement_list
.head
,
2845 (fill_type
) 0, (bfd_vma
) 0);
2847 /* Perform another relax pass - this time we know where the
2848 globals are, so can make better guess. */
2849 lang_size_sections (statement_list
.head
,
2851 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2853 while (relax_again
);
2857 /* Size up the sections. */
2858 lang_size_sections (statement_list
.head
,
2860 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2863 /* See if anything special should be done now we know how big
2865 ldemul_after_allocation ();
2867 /* Fix any .startof. or .sizeof. symbols. */
2868 lang_set_startof ();
2870 /* Do all the assignments, now that we know the final restingplaces
2871 of all the symbols */
2873 lang_do_assignments (statement_list
.head
,
2875 (fill_type
) 0, (bfd_vma
) 0);
2877 /* Make sure that we're not mixing architectures */
2887 /* EXPORTED TO YACC */
2890 lang_add_wild (section_name
, filename
)
2891 CONST
char *CONST section_name
;
2892 CONST
char *CONST filename
;
2894 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
2897 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
2899 placed_commons
= true;
2901 if (filename
!= (char *) NULL
)
2903 lang_has_input_file
= true;
2905 new->section_name
= section_name
;
2906 new->filename
= filename
;
2907 lang_list_init (&new->children
);
2911 lang_section_start (name
, address
)
2913 etree_type
* address
;
2915 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
2917 ad
->section_name
= name
;
2918 ad
->address
= address
;
2921 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
2922 because of a -e argument on the command line, or zero if this is
2923 called by ENTRY in a linker script. Command line arguments take
2926 /* WINDOWS_NT. When an entry point has been specified, we will also force
2927 this symbol to be defined by calling ldlang_add_undef (equivalent to
2928 having switch -u entry_name on the command line). The reason we do
2929 this is so that the user doesn't have to because they would have to use
2930 the -u switch if they were specifying an entry point other than
2931 _mainCRTStartup. Specifically, if creating a windows application, entry
2932 point _WinMainCRTStartup must be specified.
2933 What I have found for non console applications (entry not _mainCRTStartup)
2934 is that the .obj that contains mainCRTStartup is brought in since it is
2935 the first encountered in libc.lib and it has other symbols in it which will
2936 be pulled in by the link process. To avoid this, adding -u with the entry
2937 point name specified forces the correct .obj to be used. We can avoid
2938 making the user do this by always adding the entry point name as an
2939 undefined symbol. */
2942 lang_add_entry (name
, cmdline
)
2946 if (entry_symbol
== NULL
2948 || ! entry_from_cmdline
)
2950 entry_symbol
= name
;
2951 entry_from_cmdline
= cmdline
;
2954 /* don't do this yet. It seems to work (the executables run), but the
2955 image created is very different from what I was getting before indicating
2956 that something else is being pulled in. When everything else is working,
2957 then try to put this back in to see if it will do the right thing for
2958 other more complicated applications */
2959 ldlang_add_undef (name
);
2964 lang_add_target (name
)
2967 lang_target_statement_type
*new = new_stat (lang_target_statement
,
2983 map_option_f
= true;
2994 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
3001 lang_add_data (type
, exp
)
3003 union etree_union
*exp
;
3006 lang_data_statement_type
*new = new_stat (lang_data_statement
,
3014 /* Create a new reloc statement. RELOC is the BFD relocation type to
3015 generate. HOWTO is the corresponding howto structure (we could
3016 look this up, but the caller has already done so). SECTION is the
3017 section to generate a reloc against, or NAME is the name of the
3018 symbol to generate a reloc against. Exactly one of SECTION and
3019 NAME must be NULL. ADDEND is an expression for the addend. */
3022 lang_add_reloc (reloc
, howto
, section
, name
, addend
)
3023 bfd_reloc_code_real_type reloc
;
3024 reloc_howto_type
*howto
;
3027 union etree_union
*addend
;
3029 lang_reloc_statement_type
*p
= new_stat (lang_reloc_statement
, stat_ptr
);
3033 p
->section
= section
;
3035 p
->addend_exp
= addend
;
3037 p
->addend_value
= 0;
3038 p
->output_section
= NULL
;
3043 lang_add_assignment (exp
)
3046 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
3053 lang_add_attribute (attribute
)
3054 enum statement_enum attribute
;
3056 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
3063 if (startup_file
!= (char *) NULL
)
3065 einfo ("%P%Fmultiple STARTUP files\n");
3067 first_file
->filename
= name
;
3068 first_file
->local_sym_name
= name
;
3069 first_file
->real
= true;
3071 startup_file
= name
;
3078 lang_float_flag
= maybe
;
3082 lang_leave_output_section_statement (fill
, memspec
)
3084 CONST
char *memspec
;
3086 current_section
->fill
= fill
;
3087 current_section
->region
= lang_memory_region_lookup (memspec
);
3088 stat_ptr
= &statement_list
;
3092 Create an absolute symbol with the given name with the value of the
3093 address of first byte of the section named.
3095 If the symbol already exists, then do nothing.
3098 lang_abs_symbol_at_beginning_of (secname
, name
)
3099 const char *secname
;
3102 struct bfd_link_hash_entry
*h
;
3104 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
3105 if (h
== (struct bfd_link_hash_entry
*) NULL
)
3106 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3108 if (h
->type
== bfd_link_hash_new
3109 || h
->type
== bfd_link_hash_undefined
)
3113 h
->type
= bfd_link_hash_defined
;
3115 sec
= bfd_get_section_by_name (output_bfd
, secname
);
3116 if (sec
== (asection
*) NULL
)
3119 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
3121 h
->u
.def
.section
= bfd_abs_section_ptr
;
3126 Create an absolute symbol with the given name with the value of the
3127 address of the first byte after the end of the section named.
3129 If the symbol already exists, then do nothing.
3132 lang_abs_symbol_at_end_of (secname
, name
)
3133 const char *secname
;
3136 struct bfd_link_hash_entry
*h
;
3138 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
3139 if (h
== (struct bfd_link_hash_entry
*) NULL
)
3140 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3142 if (h
->type
== bfd_link_hash_new
3143 || h
->type
== bfd_link_hash_undefined
)
3147 h
->type
= bfd_link_hash_defined
;
3149 sec
= bfd_get_section_by_name (output_bfd
, secname
);
3150 if (sec
== (asection
*) NULL
)
3153 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
3154 + bfd_section_size (output_bfd
, sec
));
3156 h
->u
.def
.section
= bfd_abs_section_ptr
;
3161 lang_statement_append (list
, element
, field
)
3162 lang_statement_list_type
* list
;
3163 lang_statement_union_type
* element
;
3164 lang_statement_union_type
** field
;
3166 *(list
->tail
) = element
;
3170 /* Set the output format type. -oformat overrides scripts. */
3173 lang_add_output_format (format
, big
, little
, from_script
)
3179 if (output_target
== NULL
|| !from_script
)
3181 if (command_line
.endian
== ENDIAN_BIG
3184 else if (command_line
.endian
== ENDIAN_LITTLE
3188 output_target
= format
;
3192 /* Enter a group. This creates a new lang_group_statement, and sets
3193 stat_ptr to build new statements within the group. */
3198 lang_group_statement_type
*g
;
3200 g
= new_stat (lang_group_statement
, stat_ptr
);
3201 lang_list_init (&g
->children
);
3202 stat_ptr
= &g
->children
;
3205 /* Leave a group. This just resets stat_ptr to start writing to the
3206 regular list of statements again. Note that this will not work if
3207 groups can occur inside anything else which can adjust stat_ptr,
3208 but currently they can't. */
3213 stat_ptr
= &statement_list
;
3216 /* Add a new program header. This is called for each entry in a PHDRS
3217 command in a linker script. */
3220 lang_new_phdr (name
, type
, filehdr
, phdrs
, at
, flags
)
3228 struct lang_phdr
*n
, **pp
;
3230 n
= (struct lang_phdr
*) stat_alloc (sizeof (struct lang_phdr
));
3233 n
->type
= exp_get_value_int (type
, 0, "program header type",
3234 lang_final_phase_enum
);
3235 n
->filehdr
= filehdr
;
3240 for (pp
= &lang_phdr_list
; *pp
!= NULL
; pp
= &(*pp
)->next
)
3245 /* Record that a section should be placed in a phdr. */
3248 lang_section_in_phdr (name
)
3251 struct lang_output_section_phdr_list
*n
;
3253 n
= ((struct lang_output_section_phdr_list
*)
3254 stat_alloc (sizeof (struct lang_output_section_phdr_list
)));
3257 n
->next
= current_section
->phdrs
;
3258 current_section
->phdrs
= n
;
3261 /* Record the program header information in the output BFD. FIXME: We
3262 should not be calling an ELF specific function here. */
3265 lang_record_phdrs ()
3269 struct lang_output_section_phdr_list
*last
;
3270 struct lang_phdr
*l
;
3271 lang_statement_union_type
*u
;
3274 secs
= xmalloc (alc
* sizeof (asection
*));
3276 for (l
= lang_phdr_list
; l
!= NULL
; l
= l
->next
)
3283 for (u
= lang_output_section_statement
.head
;
3285 u
= u
->output_section_statement
.next
)
3287 lang_output_section_statement_type
*os
;
3288 struct lang_output_section_phdr_list
*pl
;
3290 os
= &u
->output_section_statement
;
3298 || os
->bfd_section
== NULL
3299 || (os
->bfd_section
->flags
& SEC_ALLOC
) == 0)
3304 if (os
->bfd_section
== NULL
)
3307 for (; pl
!= NULL
; pl
= pl
->next
)
3309 if (strcmp (pl
->name
, l
->name
) == 0)
3314 secs
= xrealloc (secs
, alc
* sizeof (asection
*));
3316 secs
[c
] = os
->bfd_section
;
3323 if (l
->flags
== NULL
)
3326 flags
= exp_get_vma (l
->flags
, 0, "phdr flags",
3327 lang_final_phase_enum
);
3332 at
= exp_get_vma (l
->at
, 0, "phdr load address",
3333 lang_final_phase_enum
);
3335 if (! bfd_record_phdr (output_bfd
, l
->type
,
3336 l
->flags
== NULL
? false : true,
3338 l
->at
== NULL
? false : true,
3339 at
, l
->filehdr
, l
->phdrs
, c
, secs
))
3340 einfo ("%F%P: bfd_record_phdr failed: %E\n");
3345 /* Make sure all the phdr assignments succeeded. */
3346 for (u
= lang_output_section_statement
.head
;
3348 u
= u
->output_section_statement
.next
)
3350 struct lang_output_section_phdr_list
*pl
;
3352 if (u
->output_section_statement
.bfd_section
== NULL
)
3355 for (pl
= u
->output_section_statement
.phdrs
;
3358 if (! pl
->used
&& strcmp (pl
->name
, "NONE") != 0)
3359 einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
3360 u
->output_section_statement
.name
, pl
->name
);