1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
36 static void print_statements
PARAMS ((void));
37 static void print_statement
PARAMS ((lang_statement_union_type
*,
38 lang_output_section_statement_type
*));
39 static lang_statement_union_type
*new_statement
PARAMS ((enum statement_enum
,
41 lang_statement_list_type
*));
45 static struct obstack stat_obstack
;
47 #define obstack_chunk_alloc xmalloc
48 #define obstack_chunk_free free
49 static CONST
char *startup_file
;
50 static lang_statement_list_type input_file_chain
;
52 /* Points to the last statement in the .data section, so we can add
53 stuff to the data section without pain */
54 static lang_statement_list_type end_of_data_section_statement_list
;
56 static boolean placed_commons
= false;
57 static lang_output_section_statement_type
*default_common_section
;
58 static boolean map_option_f
;
59 static bfd_vma print_dot
;
60 static lang_input_statement_type
*first_file
;
61 static lang_statement_list_type lang_output_section_statement
;
62 static CONST
char *current_target
;
63 static CONST
char *output_target
;
64 static int longest_section_name
= 8;
65 static lang_statement_list_type statement_list
;
67 static void print_size
PARAMS ((size_t value
));
68 static void print_alignment
PARAMS ((unsigned int value
));
69 static void print_fill
PARAMS ((fill_type value
));
70 static void print_section
PARAMS ((const char *name
));
71 static void lang_for_each_statement_worker
72 PARAMS ((void (*func
) (lang_statement_union_type
*),
73 lang_statement_union_type
*s
));
74 static lang_input_statement_type
*new_afile
75 PARAMS ((const char *name
, lang_input_file_enum_type file_type
,
77 static void print_flags
PARAMS ((int *ignore_flags
));
78 static void init_os
PARAMS ((lang_output_section_statement_type
*s
));
79 static void wild_doit
PARAMS ((lang_statement_list_type
*ptr
,
81 lang_output_section_statement_type
*output
,
82 lang_input_statement_type
*file
));
83 static asection
*our_bfd_get_section_by_name
PARAMS ((bfd
*abfd
,
84 const char *section
));
85 static void wild_section
PARAMS ((lang_wild_statement_type
*ptr
,
87 lang_input_statement_type
*file
,
88 lang_output_section_statement_type
*output
));
89 static lang_input_statement_type
*lookup_name
PARAMS ((const char *name
,
91 static void wild
PARAMS ((lang_wild_statement_type
*s
,
92 const char *section
, const char *file
,
94 lang_output_section_statement_type
*output
));
95 static bfd
*open_output
PARAMS ((const char *name
));
96 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*statement
));
97 static void open_input_bfds
PARAMS ((lang_statement_union_type
*statement
));
98 static void lang_reasonable_defaults
PARAMS ((void));
99 static void lang_place_undefineds
PARAMS ((void));
100 static void lang_create_output_section_statements
PARAMS ((void));
101 static void map_input_to_output_sections
102 PARAMS ((lang_statement_union_type
*s
,
104 lang_output_section_statement_type
*output_section_statement
));
105 static void print_output_section_statement
106 PARAMS ((lang_output_section_statement_type
*output_section_statement
));
107 static void print_assignment
108 PARAMS ((lang_assignment_statement_type
*assignment
,
109 lang_output_section_statement_type
*output_section
));
110 static void print_input_statement
PARAMS ((lang_input_statement_type
*statm
));
111 static void print_input_section
PARAMS ((lang_input_section_type
*in
));
112 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*fill
));
113 static void print_data_statement
PARAMS ((lang_data_statement_type
*data
));
114 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*s
));
115 static void print_wild_statement
116 PARAMS ((lang_wild_statement_type
*w
,
117 lang_output_section_statement_type
*os
));
118 static void print_statement
PARAMS ((lang_statement_union_type
*s
,
119 lang_output_section_statement_type
*os
));
120 static void print_statements
PARAMS ((void));
121 static bfd_vma insert_pad
PARAMS ((lang_statement_union_type
**this_ptr
,
122 fill_type fill
, unsigned int power
,
123 asection
*output_section_statement
,
125 static bfd_vma size_input_section
126 PARAMS ((lang_statement_union_type
**this_ptr
,
127 lang_output_section_statement_type
*output_section_statement
,
128 fill_type fill
, bfd_vma dot
, boolean relax
));
129 static bfd_vma lang_size_sections
130 PARAMS ((lang_statement_union_type
*s
,
131 lang_output_section_statement_type
*output_section_statement
,
132 lang_statement_union_type
**prev
, fill_type fill
,
133 bfd_vma dot
, boolean relax
));
134 static bfd_vma lang_do_assignments
135 PARAMS ((lang_statement_union_type
* s
,
136 lang_output_section_statement_type
*output_section_statement
,
139 static void lang_finish
PARAMS ((void));
140 static void lang_check
PARAMS ((void));
141 static void lang_common
PARAMS ((void));
142 static boolean lang_one_common
PARAMS ((struct bfd_link_hash_entry
*, PTR
));
143 static void lang_place_orphans
PARAMS ((void));
144 static int topower
PARAMS ((int));
145 static void reset_memory_regions
PARAMS ((void));
148 lang_output_section_statement_type
*abs_output_section
;
149 lang_statement_list_type
*stat_ptr
= &statement_list
;
150 lang_statement_list_type file_chain
=
152 CONST
char *entry_symbol
= 0;
153 bfd_size_type largest_section
= 0;
154 boolean lang_has_input_file
= false;
155 boolean had_output_filename
= false;
156 boolean lang_float_flag
= false;
157 boolean delete_output_file_on_failure
= false;
159 etree_type
*base
; /* Relocation base - or null */
163 #define cat(a,b) a##b
165 #define cat(a,b) a/**/b
168 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
170 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
172 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
178 return obstack_alloc (&stat_obstack
, size
);
185 fprintf (config
.map_file
, "%5x", (unsigned) value
);
189 print_alignment (value
)
192 fprintf (config
.map_file
, "2**%1u", value
);
199 fprintf (config
.map_file
, "%04x", (unsigned) value
);
206 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
209 /*----------------------------------------------------------------------
210 lang_for_each_statement walks the parse tree and calls the provided
211 function for each node
215 lang_for_each_statement_worker (func
, s
)
216 void (*func
) PARAMS ((lang_statement_union_type
*));
217 lang_statement_union_type
*s
;
219 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
223 switch (s
->header
.type
)
225 case lang_constructors_statement_enum
:
226 lang_for_each_statement_worker (func
, constructor_list
.head
);
228 case lang_output_section_statement_enum
:
229 lang_for_each_statement_worker
231 s
->output_section_statement
.children
.head
);
233 case lang_wild_statement_enum
:
234 lang_for_each_statement_worker
236 s
->wild_statement
.children
.head
);
238 case lang_data_statement_enum
:
239 case lang_object_symbols_statement_enum
:
240 case lang_output_statement_enum
:
241 case lang_target_statement_enum
:
242 case lang_input_section_enum
:
243 case lang_input_statement_enum
:
244 case lang_assignment_statement_enum
:
245 case lang_padding_statement_enum
:
246 case lang_address_statement_enum
:
256 lang_for_each_statement (func
)
257 void (*func
) PARAMS ((lang_statement_union_type
*));
259 lang_for_each_statement_worker (func
,
260 statement_list
.head
);
263 /*----------------------------------------------------------------------*/
265 lang_list_init (list
)
266 lang_statement_list_type
*list
;
268 list
->head
= (lang_statement_union_type
*) NULL
;
269 list
->tail
= &list
->head
;
272 /*----------------------------------------------------------------------
274 build a new statement node for the parse tree
279 lang_statement_union_type
*
280 new_statement (type
, size
, list
)
281 enum statement_enum type
;
283 lang_statement_list_type
* list
;
285 lang_statement_union_type
*new = (lang_statement_union_type
*)
288 new->header
.type
= type
;
289 new->header
.next
= (lang_statement_union_type
*) NULL
;
290 lang_statement_append (list
, new, &new->header
.next
);
295 Build a new input file node for the language. There are several ways
296 in which we treat an input file, eg, we only look at symbols, or
297 prefix it with a -l etc.
299 We can be supplied with requests for input files more than once;
300 they may, for example be split over serveral lines like foo.o(.text)
301 foo.o(.data) etc, so when asked for a file we check that we havn't
302 got it already so we don't duplicate the bfd.
305 static lang_input_statement_type
*
306 new_afile (name
, file_type
, target
)
308 lang_input_file_enum_type file_type
;
312 lang_input_statement_type
*p
= new_stat (lang_input_statement
,
315 lang_has_input_file
= true;
317 p
->complained
= false;
320 case lang_input_file_is_symbols_only_enum
:
322 p
->is_archive
= false;
324 p
->local_sym_name
= name
;
325 p
->just_syms_flag
= true;
326 p
->search_dirs_flag
= false;
328 case lang_input_file_is_fake_enum
:
330 p
->is_archive
= false;
332 p
->local_sym_name
= name
;
333 p
->just_syms_flag
= false;
334 p
->search_dirs_flag
= false;
336 case lang_input_file_is_l_enum
:
337 p
->is_archive
= true;
340 p
->local_sym_name
= concat ("-l", name
, "");
341 p
->just_syms_flag
= false;
342 p
->search_dirs_flag
= true;
344 case lang_input_file_is_search_file_enum
:
345 case lang_input_file_is_marker_enum
:
347 p
->is_archive
= false;
349 p
->local_sym_name
= name
;
350 p
->just_syms_flag
= false;
351 p
->search_dirs_flag
= true;
353 case lang_input_file_is_file_enum
:
355 p
->is_archive
= false;
357 p
->local_sym_name
= name
;
358 p
->just_syms_flag
= false;
359 p
->search_dirs_flag
= false;
364 p
->asymbols
= (asymbol
**) NULL
;
365 p
->superfile
= (lang_input_statement_type
*) NULL
;
366 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
367 p
->next
= (lang_statement_union_type
*) NULL
;
369 p
->common_output_section
= (asection
*) NULL
;
371 lang_statement_append (&input_file_chain
,
372 (lang_statement_union_type
*) p
,
377 lang_input_statement_type
*
378 lang_add_input_file (name
, file_type
, target
)
380 lang_input_file_enum_type file_type
;
383 /* Look it up or build a new one */
384 lang_has_input_file
= true;
387 lang_input_statement_type
*p
;
389 for (p
= (lang_input_statement_type
*) input_file_chain
.head
;
390 p
!= (lang_input_statement_type
*) NULL
;
391 p
= (lang_input_statement_type
*) (p
->next_real_file
))
393 /* Sometimes we have incomplete entries in here */
394 if (p
->filename
!= (char *) NULL
)
396 if (strcmp (name
, p
->filename
) == 0)
402 return new_afile (name
, file_type
, target
);
405 /* Build enough state so that the parser can build its tree */
409 obstack_begin (&stat_obstack
, 1000);
411 stat_ptr
= &statement_list
;
413 lang_list_init (stat_ptr
);
415 lang_list_init (&input_file_chain
);
416 lang_list_init (&lang_output_section_statement
);
417 lang_list_init (&file_chain
);
418 first_file
= lang_add_input_file ((char *) NULL
,
419 lang_input_file_is_marker_enum
,
421 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
423 abs_output_section
->bfd_section
= &bfd_abs_section
;
427 /*----------------------------------------------------------------------
428 A region is an area of memory declared with the
429 MEMORY { name:org=exp, len=exp ... }
432 We maintain a list of all the regions here
434 If no regions are specified in the script, then the default is used
435 which is created when looked up to be the entire data space
438 static lang_memory_region_type
*lang_memory_region_list
;
439 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
441 lang_memory_region_type
*
442 lang_memory_region_lookup (name
)
443 CONST
char *CONST name
;
446 lang_memory_region_type
*p
= lang_memory_region_list
;
448 for (p
= lang_memory_region_list
;
449 p
!= (lang_memory_region_type
*) NULL
;
452 if (strcmp (p
->name
, name
) == 0)
457 if (strcmp (name
, "*default*") == 0)
459 /* This is the default region, dig out first one on the list */
460 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
462 return lang_memory_region_list
;
466 lang_memory_region_type
*new =
467 (lang_memory_region_type
*) stat_alloc ((bfd_size_type
) (sizeof (lang_memory_region_type
)));
469 new->name
= buystring (name
);
470 new->next
= (lang_memory_region_type
*) NULL
;
472 *lang_memory_region_list_tail
= new;
473 lang_memory_region_list_tail
= &new->next
;
475 new->length
= ~(bfd_size_type
)0;
477 new->had_full_message
= false;
484 lang_output_section_statement_type
*
485 lang_output_section_find (name
)
486 CONST
char *CONST name
;
488 lang_statement_union_type
*u
;
489 lang_output_section_statement_type
*lookup
;
491 for (u
= lang_output_section_statement
.head
;
492 u
!= (lang_statement_union_type
*) NULL
;
495 lookup
= &u
->output_section_statement
;
496 if (strcmp (name
, lookup
->name
) == 0)
501 return (lang_output_section_statement_type
*) NULL
;
504 lang_output_section_statement_type
*
505 lang_output_section_statement_lookup (name
)
506 CONST
char *CONST name
;
508 lang_output_section_statement_type
*lookup
;
510 lookup
= lang_output_section_find (name
);
511 if (lookup
== (lang_output_section_statement_type
*) NULL
)
514 lookup
= (lang_output_section_statement_type
*)
515 new_stat (lang_output_section_statement
, stat_ptr
);
516 lookup
->region
= (lang_memory_region_type
*) NULL
;
518 lookup
->block_value
= 1;
521 lookup
->next
= (lang_statement_union_type
*) NULL
;
522 lookup
->bfd_section
= (asection
*) NULL
;
523 lookup
->processed
= false;
524 lookup
->loadable
= 1;
525 lookup
->addr_tree
= (etree_type
*) NULL
;
526 lang_list_init (&lookup
->children
);
528 lookup
->memspec
= (CONST
char *) NULL
;
530 lookup
->subsection_alignment
= -1;
531 lookup
->section_alignment
= -1;
532 lookup
->load_base
= (union etree_union
*) 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 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
608 section_userdata_type
*new =
609 (section_userdata_type
*)
610 stat_alloc ((bfd_size_type
) (sizeof (section_userdata_type
)));
612 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
613 if (s
->bfd_section
== (asection
*) NULL
)
614 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
615 if (s
->bfd_section
== (asection
*) NULL
)
617 einfo ("%P%F: output format %s cannot represent section called %s\n",
618 output_bfd
->xvec
->name
, s
->name
);
620 s
->bfd_section
->output_section
= s
->bfd_section
;
621 /* s->bfd_section->flags = s->flags;*/
623 /* We initialize an output sections output offset to minus its own */
624 /* vma to allow us to output a section through itself */
625 s
->bfd_section
->output_offset
= 0;
626 get_userdata (s
->bfd_section
) = (PTR
) new;
630 /***********************************************************************
633 These expand statements like *(.text) and foo.o to a list of
634 explicit actions, like foo.o(.text), bar.o(.text) and
637 The toplevel routine, wild, takes a statement, section, file and
638 target. If either the section or file is null it is taken to be the
639 wildcard. Seperate lang_input_section statements are created for
640 each part of the expanstion, and placed after the statement provided.
645 wild_doit (ptr
, section
, output
, file
)
646 lang_statement_list_type
* ptr
;
648 lang_output_section_statement_type
* output
;
649 lang_input_statement_type
* file
;
651 if (output
->bfd_section
== (asection
*) NULL
)
654 /* Initialize the vma and size to the existing section. This will
655 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
657 if (section
!= (asection
*) NULL
)
659 bfd_set_section_vma (0, output
->bfd_section
,
660 bfd_section_vma (0, section
));
661 output
->bfd_section
->_raw_size
= section
->_raw_size
;
665 if (section
!= (asection
*) NULL
666 && section
->output_section
== (asection
*) NULL
)
668 /* Add a section reference to the list */
669 lang_input_section_type
*new = new_stat (lang_input_section
, ptr
);
671 new->section
= section
;
673 section
->output_section
= output
->bfd_section
;
675 /* Be selective about what the output section inherits from the
678 if ((section
->flags
& SEC_SHARED_LIBRARY
) != 0)
679 section
->output_section
->flags
|= section
->flags
;
681 section
->output_section
->flags
|=
682 section
->flags
& (flagword
) (~ SEC_NEVER_LOAD
);
684 if (!output
->loadable
)
686 /* Turn off load flag */
687 output
->bfd_section
->flags
&= ~SEC_LOAD
;
688 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
690 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
692 output
->bfd_section
->alignment_power
= section
->alignment_power
;
694 /* If supplied an aligmnet, then force it */
695 if (output
->section_alignment
!= -1)
697 output
->bfd_section
->alignment_power
= output
->section_alignment
;
703 our_bfd_get_section_by_name (abfd
, section
)
707 return bfd_get_section_by_name (abfd
, section
);
711 wild_section (ptr
, section
, file
, output
)
712 lang_wild_statement_type
* ptr
;
714 lang_input_statement_type
* file
;
715 lang_output_section_statement_type
* output
;
719 if (file
->just_syms_flag
== false)
721 if (section
== (char *) NULL
)
723 /* Do the creation to all sections in the file */
724 for (s
= file
->the_bfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
727 if ((s
->flags
& SEC_IS_COMMON
) == 0)
729 wild_doit (&ptr
->children
, s
, output
, file
);
735 /* Do the creation to the named section only */
736 wild_doit (&ptr
->children
,
737 our_bfd_get_section_by_name (file
->the_bfd
, section
),
743 /* passed a file name (which must have been seen already and added to
744 the statement tree. We will see if it has been opened already and
745 had its symbols read. If not then we'll read it.
747 Archives are pecuilar here. We may open them once, but if they do
748 not define anything we need at the time, they won't have all their
749 symbols read. If we need them later, we'll have to redo it.
752 lang_input_statement_type
*
753 lookup_name (name
, force_load
)
757 lang_input_statement_type
*search
;
759 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
760 search
!= (lang_input_statement_type
*) NULL
;
761 search
= (lang_input_statement_type
*) search
->next_real_file
)
763 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
765 if (search
->filename
!= (char *) NULL
766 && name
!= (char *) NULL
767 && strcmp (search
->filename
, name
) == 0)
771 if (search
== (lang_input_statement_type
*) NULL
)
773 /* There isn't an afile entry for this file yet, this must be
774 because the name has only appeared inside a load script and
775 not on the command line */
776 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
);
779 /* If we have already added this file, or this file is not real
780 (FIXME: can that ever actually happen?) or the name is NULL
781 (FIXME: can that ever actually happen?) don't add this file. */
782 if ((search
->loaded
&& ! force_load
)
784 || search
->filename
== (const char *) NULL
)
787 ldfile_open_file (search
);
789 if (bfd_check_format (search
->the_bfd
, bfd_object
))
791 ldlang_add_file (search
);
792 if (trace_files
|| trace_file_tries
)
793 info_msg ("%I\n", search
);
795 else if (bfd_check_format (search
->the_bfd
, bfd_archive
))
797 /* There is nothing to do here; the add_symbols routine will
798 call ldlang_add_file (via the add_archive_element callback)
799 for each element of the archive which is used. */
802 einfo ("%F%B: file not recognized: %E\n", search
->the_bfd
);
804 bfd_set_gp_size (search
->the_bfd
, g_switch_value
);
806 if (bfd_link_add_symbols (search
->the_bfd
, &link_info
) == false)
807 einfo ("%F%B: could not read symbols: %E\n", search
->the_bfd
);
809 search
->loaded
= true;
815 wild (s
, section
, file
, target
, output
)
816 lang_wild_statement_type
* s
;
820 lang_output_section_statement_type
* output
;
822 lang_input_statement_type
*f
;
824 if (file
== (char *) NULL
)
826 /* Perform the iteration over all files in the list */
827 for (f
= (lang_input_statement_type
*) file_chain
.head
;
828 f
!= (lang_input_statement_type
*) NULL
;
829 f
= (lang_input_statement_type
*) f
->next
)
831 wild_section (s
, section
, f
, output
);
836 /* Perform the iteration over a single file */
837 wild_section (s
, section
, lookup_name (file
, 0), output
);
839 if (section
!= (char *) NULL
840 && strcmp (section
, "COMMON") == 0
841 && default_common_section
== (lang_output_section_statement_type
*) NULL
)
843 /* Remember the section that common is going to incase we later
844 get something which doesn't know where to put it */
845 default_common_section
= output
;
850 read in all the files
859 if (output_target
== (char *) NULL
)
861 if (current_target
!= (char *) NULL
)
862 output_target
= current_target
;
864 output_target
= default_target
;
866 output
= bfd_openw (name
, output_target
);
868 if (output
== (bfd
*) NULL
)
870 if (bfd_error
== invalid_target
)
872 einfo ("%P%F: target %s not found\n", output_target
);
874 einfo ("%P%F: cannot open output file %s: %E\n", name
);
877 delete_output_file_on_failure
= 1;
879 /* output->flags |= D_PAGED;*/
881 if (! bfd_set_format (output
, bfd_object
))
882 einfo ("%P%F:%s: can not make object file: %E\n", name
);
883 if (! bfd_set_arch_mach (output
,
884 ldfile_output_architecture
,
885 ldfile_output_machine
))
886 einfo ("%P%F:%s: can not set architecture: %E\n", name
);
888 link_info
.hash
= bfd_link_hash_table_create (output
);
889 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
890 einfo ("%P%F: can not create link hash table: %E\n");
892 bfd_set_gp_size (output
, g_switch_value
);
900 ldlang_open_output (statement
)
901 lang_statement_union_type
* statement
;
903 switch (statement
->header
.type
)
905 case lang_output_statement_enum
:
906 ASSERT (output_bfd
== (bfd
*) NULL
);
907 output_bfd
= open_output (statement
->output_statement
.name
);
908 ldemul_set_output_arch ();
909 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
910 output_bfd
->flags
|= D_PAGED
;
912 output_bfd
->flags
&= ~D_PAGED
;
913 if (config
.text_read_only
)
914 output_bfd
->flags
|= WP_TEXT
;
916 output_bfd
->flags
&= ~WP_TEXT
;
919 case lang_target_statement_enum
:
920 current_target
= statement
->target_statement
.target
;
928 open_input_bfds (statement
)
929 lang_statement_union_type
* statement
;
931 switch (statement
->header
.type
)
933 case lang_target_statement_enum
:
934 current_target
= statement
->target_statement
.target
;
936 case lang_wild_statement_enum
:
937 /* Maybe we should load the file's symbols */
938 if (statement
->wild_statement
.filename
)
940 (void) lookup_name (statement
->wild_statement
.filename
, 1);
943 case lang_input_statement_enum
:
944 if (statement
->input_statement
.real
== true)
946 statement
->input_statement
.target
= current_target
;
947 lookup_name (statement
->input_statement
.filename
, 1);
955 /* If there are [COMMONS] statements, put a wild one into the bss section */
958 lang_reasonable_defaults ()
961 lang_output_section_statement_lookup (".text");
962 lang_output_section_statement_lookup (".data");
964 default_common_section
=
965 lang_output_section_statement_lookup (".bss");
968 if (placed_commons
== false)
970 lang_wild_statement_type
*new =
971 new_stat (lang_wild_statement
,
972 &default_common_section
->children
);
974 new->section_name
= "COMMON";
975 new->filename
= (char *) NULL
;
976 lang_list_init (&new->children
);
983 Add the supplied name to the symbol table as an undefined reference.
984 Remove items from the chain as we open input bfds
986 typedef struct ldlang_undef_chain_list
988 struct ldlang_undef_chain_list
*next
;
990 } ldlang_undef_chain_list_type
;
992 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
995 ldlang_add_undef (name
)
996 CONST
char *CONST name
;
998 ldlang_undef_chain_list_type
*new =
999 (ldlang_undef_chain_list_type
1000 *) stat_alloc ((bfd_size_type
) (sizeof (ldlang_undef_chain_list_type
)));
1002 new->next
= ldlang_undef_chain_list_head
;
1003 ldlang_undef_chain_list_head
= new;
1005 new->name
= buystring (name
);
1008 /* Run through the list of undefineds created above and place them
1009 into the linker hash table as undefined symbols belonging to the
1013 lang_place_undefineds ()
1015 ldlang_undef_chain_list_type
*ptr
;
1017 for (ptr
= ldlang_undef_chain_list_head
;
1018 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1021 struct bfd_link_hash_entry
*h
;
1023 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1024 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1025 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1026 if (h
->type
== bfd_link_hash_new
)
1028 h
->type
= bfd_link_hash_undefined
;
1029 h
->u
.undef
.abfd
= NULL
;
1030 bfd_link_add_undef (link_info
.hash
, h
);
1035 /* Copy important data from out internal form to the bfd way. Also
1036 create a section for the dummy file
1040 lang_create_output_section_statements ()
1042 lang_statement_union_type
*os
;
1044 for (os
= lang_output_section_statement
.head
;
1045 os
!= (lang_statement_union_type
*) NULL
;
1046 os
= os
->output_section_statement
.next
)
1048 lang_output_section_statement_type
*s
=
1049 &os
->output_section_statement
;
1056 /* Open input files and attatch to output sections */
1058 map_input_to_output_sections (s
, target
, output_section_statement
)
1059 lang_statement_union_type
* s
;
1061 lang_output_section_statement_type
* output_section_statement
;
1063 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1065 switch (s
->header
.type
)
1069 case lang_wild_statement_enum
:
1070 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1071 s
->wild_statement
.filename
, target
,
1072 output_section_statement
);
1075 case lang_constructors_statement_enum
:
1076 map_input_to_output_sections (constructor_list
.head
,
1078 output_section_statement
);
1080 case lang_output_section_statement_enum
:
1081 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1083 &s
->output_section_statement
);
1085 case lang_output_statement_enum
:
1087 case lang_target_statement_enum
:
1088 target
= s
->target_statement
.target
;
1090 case lang_fill_statement_enum
:
1091 case lang_input_section_enum
:
1092 case lang_object_symbols_statement_enum
:
1093 case lang_data_statement_enum
:
1094 case lang_assignment_statement_enum
:
1095 case lang_padding_statement_enum
:
1097 case lang_afile_asection_pair_statement_enum
:
1100 case lang_address_statement_enum
:
1101 /* Mark the specified section with the supplied address */
1103 lang_output_section_statement_type
*os
=
1104 lang_output_section_statement_lookup
1105 (s
->address_statement
.section_name
);
1107 os
->addr_tree
= s
->address_statement
.address
;
1108 if (os
->bfd_section
== (asection
*) NULL
)
1110 einfo ("%P%F: cannot set the address of undefined section %s\n",
1111 s
->address_statement
.section_name
);
1115 case lang_input_statement_enum
:
1116 /* A standard input statement, has no wildcards */
1123 print_output_section_statement (output_section_statement
)
1124 lang_output_section_statement_type
* output_section_statement
;
1126 asection
*section
= output_section_statement
->bfd_section
;
1129 print_section (output_section_statement
->name
);
1134 print_dot
= section
->vma
;
1138 print_address (section
->vma
);
1140 print_size (section
->_raw_size
);
1142 print_size(section
->_cooked_size
);
1144 print_alignment (section
->alignment_power
);
1147 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1148 print_flags (stdout
, &output_section_statement
->flags
);
1150 if (section
->flags
& SEC_LOAD
)
1151 fprintf (config
.map_file
, "load ");
1152 if (section
->flags
& SEC_ALLOC
)
1153 fprintf (config
.map_file
, "alloc ");
1154 if (section
->flags
& SEC_RELOC
)
1155 fprintf (config
.map_file
, "reloc ");
1156 if (section
->flags
& SEC_HAS_CONTENTS
)
1157 fprintf (config
.map_file
, "contents ");
1162 fprintf (config
.map_file
, "No attached output section");
1165 if (output_section_statement
->load_base
)
1167 int b
= exp_get_value_int(output_section_statement
->load_base
,
1168 0, "output base", lang_final_phase_enum
);
1169 printf("Output address %08x\n", b
);
1171 if (output_section_statement
->section_alignment
>= 0
1172 || output_section_statement
->section_alignment
>= 0)
1174 printf("\t\t\t\t\tforced alignment ");
1175 if ( output_section_statement
->section_alignment
>= 0)
1177 printf("section 2**%d ",output_section_statement
->section_alignment
);
1179 if ( output_section_statement
->subsection_alignment
>= 0)
1181 printf("subsection 2**%d ",output_section_statement
->subsection_alignment
);
1186 print_statement (output_section_statement
->children
.head
,
1187 output_section_statement
);
1192 print_assignment (assignment
, output_section
)
1193 lang_assignment_statement_type
* assignment
;
1194 lang_output_section_statement_type
* output_section
;
1196 etree_value_type result
;
1202 print_address (print_dot
);
1204 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1206 lang_final_phase_enum
,
1212 print_address (result
.value
);
1216 fprintf (config
.map_file
, "*undefined*");
1219 exp_print_tree (assignment
->exp
);
1221 fprintf (config
.map_file
, "\n");
1225 print_input_statement (statm
)
1226 lang_input_statement_type
* statm
;
1228 if (statm
->filename
!= (char *) NULL
)
1230 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1234 /* Print all the defined symbols for the abfd provided by in the supplied
1239 print_one_symbol (hash_entry
, ptr
)
1240 struct bfd_link_hash_entry
*hash_entry
;
1243 asection
* sec
= (asection
*)ptr
;
1245 if (hash_entry
->type
== bfd_link_hash_defined
)
1247 if (sec
== hash_entry
->u
.def
.section
) {
1249 fprintf (config
.map_file
, " ");
1251 fprintf (config
.map_file
, " ");
1252 print_address (hash_entry
->u
.def
.value
+ outside_section_address (sec
));
1253 fprintf (config
.map_file
, " %s", hash_entry
->root
.string
);
1262 print_input_section (in
)
1263 lang_input_section_type
* in
;
1265 asection
*i
= in
->section
;
1266 int size
= i
->reloc_done
?
1267 bfd_get_section_size_after_reloc (i
) :
1268 bfd_get_section_size_before_reloc (i
);
1273 fprintf (config
.map_file
, " ");
1274 print_section (i
->name
);
1275 fprintf (config
.map_file
, " ");
1276 if (i
->output_section
)
1278 print_address (i
->output_section
->vma
+ i
->output_offset
);
1279 fprintf (config
.map_file
, " ");
1280 print_size (i
->_raw_size
);
1281 fprintf (config
.map_file
, " ");
1282 print_size(i
->_cooked_size
);
1283 fprintf (config
.map_file
, " ");
1284 print_alignment (i
->alignment_power
);
1285 fprintf (config
.map_file
, " ");
1289 bfd
*abfd
= in
->ifile
->the_bfd
;
1291 if (in
->ifile
->just_syms_flag
== true)
1293 fprintf (config
.map_file
, "symbols only ");
1296 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1297 if (abfd
->my_archive
!= (bfd
*) NULL
)
1299 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1304 fprintf (config
.map_file
, "%s", abfd
->filename
);
1306 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1309 /* Print all the symbols */
1310 bfd_link_hash_traverse (link_info
.hash
, print_one_symbol
, (PTR
) i
);
1318 print_dot
= outside_section_address (i
) + size
;
1322 fprintf (config
.map_file
, "No output section allocated\n");
1328 print_fill_statement (fill
)
1329 lang_fill_statement_type
* fill
;
1331 fprintf (config
.map_file
, "FILL mask ");
1332 print_fill (fill
->fill
);
1336 print_data_statement (data
)
1337 lang_data_statement_type
* data
;
1339 /* bfd_vma value; */
1344 /* ASSERT(print_dot == data->output_vma);*/
1346 print_address (data
->output_vma
+ data
->output_section
->vma
);
1348 print_address (data
->value
);
1353 fprintf (config
.map_file
, "BYTE ");
1354 print_dot
+= BYTE_SIZE
;
1357 fprintf (config
.map_file
, "SHORT ");
1358 print_dot
+= SHORT_SIZE
;
1361 fprintf (config
.map_file
, "LONG ");
1362 print_dot
+= LONG_SIZE
;
1365 fprintf (config
.map_file
, "QUAD ");
1366 print_dot
+= QUAD_SIZE
;
1370 exp_print_tree (data
->exp
);
1372 fprintf (config
.map_file
, "\n");
1377 print_padding_statement (s
)
1378 lang_padding_statement_type
* s
;
1382 print_section ("*fill*");
1384 print_address (s
->output_offset
+ s
->output_section
->vma
);
1386 print_size (s
->size
);
1388 print_fill (s
->fill
);
1391 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1396 print_wild_statement (w
, os
)
1397 lang_wild_statement_type
* w
;
1398 lang_output_section_statement_type
* os
;
1400 fprintf (config
.map_file
, " from ");
1401 if (w
->filename
!= (char *) NULL
)
1403 fprintf (config
.map_file
, "%s", w
->filename
);
1407 fprintf (config
.map_file
, "*");
1409 if (w
->section_name
!= (char *) NULL
)
1411 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1415 fprintf (config
.map_file
, "(*)");
1418 print_statement (w
->children
.head
, os
);
1422 print_statement (s
, os
)
1423 lang_statement_union_type
* s
;
1424 lang_output_section_statement_type
* os
;
1428 switch (s
->header
.type
)
1430 case lang_constructors_statement_enum
:
1431 fprintf (config
.map_file
, "constructors:\n");
1432 print_statement (constructor_list
.head
, os
);
1434 case lang_wild_statement_enum
:
1435 print_wild_statement (&s
->wild_statement
, os
);
1438 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1441 case lang_address_statement_enum
:
1442 fprintf (config
.map_file
, "address\n");
1444 case lang_object_symbols_statement_enum
:
1445 fprintf (config
.map_file
, "object symbols\n");
1447 case lang_fill_statement_enum
:
1448 print_fill_statement (&s
->fill_statement
);
1450 case lang_data_statement_enum
:
1451 print_data_statement (&s
->data_statement
);
1453 case lang_input_section_enum
:
1454 print_input_section (&s
->input_section
);
1456 case lang_padding_statement_enum
:
1457 print_padding_statement (&s
->padding_statement
);
1459 case lang_output_section_statement_enum
:
1460 print_output_section_statement (&s
->output_section_statement
);
1462 case lang_assignment_statement_enum
:
1463 print_assignment (&s
->assignment_statement
,
1466 case lang_target_statement_enum
:
1467 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1469 case lang_output_statement_enum
:
1470 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1471 s
->output_statement
.name
,
1472 output_target
? output_target
: "");
1474 case lang_input_statement_enum
:
1475 print_input_statement (&s
->input_statement
);
1477 case lang_afile_asection_pair_statement_enum
:
1489 print_statement (statement_list
.head
,
1490 abs_output_section
);
1495 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
1496 lang_statement_union_type
** this_ptr
;
1499 asection
* output_section_statement
;
1502 /* Align this section first to the
1503 input sections requirement, then
1504 to the output section's requirement.
1505 If this alignment is > than any seen before,
1506 then record it too. Perform the alignment by
1507 inserting a magic 'padding' statement.
1510 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1512 if (alignment_needed
!= 0)
1514 lang_statement_union_type
*new =
1515 (lang_statement_union_type
*)
1516 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1518 /* Link into existing chain */
1519 new->header
.next
= *this_ptr
;
1521 new->header
.type
= lang_padding_statement_enum
;
1522 new->padding_statement
.output_section
= output_section_statement
;
1523 new->padding_statement
.output_offset
=
1524 dot
- output_section_statement
->vma
;
1525 new->padding_statement
.fill
= fill
;
1526 new->padding_statement
.size
= alignment_needed
;
1530 /* Remember the most restrictive alignment */
1531 if (power
> output_section_statement
->alignment_power
)
1533 output_section_statement
->alignment_power
= power
;
1535 output_section_statement
->_raw_size
+= alignment_needed
;
1536 return alignment_needed
+ dot
;
1540 /* Work out how much this section will move the dot point */
1542 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
1543 lang_statement_union_type
** this_ptr
;
1544 lang_output_section_statement_type
* output_section_statement
;
1549 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1550 asection
*i
= is
->section
;
1552 if (is
->ifile
->just_syms_flag
== false)
1554 if (output_section_statement
->subsection_alignment
!= -1)
1555 i
->alignment_power
=
1556 output_section_statement
->subsection_alignment
;
1558 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1559 output_section_statement
->bfd_section
, dot
);
1561 /* remember the largest size so we can malloc the largest area
1562 needed for the output stage. Only remember the size of sections
1563 which we will actually allocate */
1564 if ((i
->flags
& SEC_HAS_CONTENTS
) != 0
1565 && (bfd_get_section_size_before_reloc (i
) > largest_section
))
1567 largest_section
= bfd_get_section_size_before_reloc (i
);
1570 /* Remember where in the output section this input section goes */
1572 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1574 /* Mark how big the output section must be to contain this now
1578 dot
+= i
->_cooked_size
;
1582 dot
+= i
->_raw_size
;
1584 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1588 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1594 /* Sizing happens in two passes, first pass we allocate worst case
1595 stuff. The second pass (if relaxing), we use what we learnt to
1596 change the size of some relocs from worst case to better
1598 static boolean had_relax
;
1601 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
1602 lang_statement_union_type
* s
;
1603 lang_output_section_statement_type
* output_section_statement
;
1604 lang_statement_union_type
** prev
;
1609 /* Size up the sections from their constituent parts */
1610 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1612 switch (s
->header
.type
)
1615 case lang_output_section_statement_enum
:
1618 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1620 /* If this is a shared library section, don't change the size
1622 if (os
->bfd_section
->flags
& SEC_SHARED_LIBRARY
)
1625 if (os
->bfd_section
== &bfd_abs_section
)
1627 /* No matter what happens, an abs section starts at zero */
1628 bfd_set_section_vma (0, os
->bfd_section
, 0);
1632 if (os
->addr_tree
== (etree_type
*) NULL
)
1634 /* No address specified for this section, get one
1635 from the region specification
1637 if (os
->region
== (lang_memory_region_type
*) NULL
)
1639 os
->region
= lang_memory_region_lookup ("*default*");
1641 dot
= os
->region
->current
;
1647 r
= exp_fold_tree (os
->addr_tree
,
1649 lang_allocating_phase_enum
,
1651 if (r
.valid
== false)
1653 einfo ("%F%S: non constant address expression for section %s\n",
1658 /* The section starts here */
1659 /* First, align to what the section needs */
1662 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1663 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1665 if (os
->load_base
) {
1666 os
->bfd_section
->lma
1667 = exp_get_value_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
1672 os
->bfd_section
->output_offset
= 0;
1674 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1675 os
->fill
, dot
, relax
);
1676 /* Ignore the size of the input sections, use the vma and size to */
1679 after
= ALIGN_N (os
->bfd_section
->vma
+
1680 os
->bfd_section
->_raw_size
,
1681 /* The coercion here is important, see ld.h. */
1682 (bfd_vma
) os
->block_value
);
1684 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1685 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1686 os
->processed
= true;
1688 /* Replace into region ? */
1689 if (os
->addr_tree
== (etree_type
*) NULL
1690 && os
->region
!= (lang_memory_region_type
*) NULL
)
1692 os
->region
->current
= dot
;
1693 /* Make sure this isn't silly */
1694 if (( os
->region
->current
1695 > os
->region
->origin
+ os
->region
->length
)
1696 || ( os
->region
->origin
> os
->region
->current
))
1698 einfo ("%X%P: region %s is full (%B section %s)\n",
1700 os
->bfd_section
->owner
,
1701 os
->bfd_section
->name
);
1702 /* Reset the region pointer */
1703 os
->region
->current
= 0;
1711 case lang_constructors_statement_enum
:
1712 dot
= lang_size_sections (constructor_list
.head
,
1713 output_section_statement
,
1714 &s
->wild_statement
.children
.head
,
1719 case lang_data_statement_enum
:
1721 unsigned int size
= 0;
1723 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1724 s
->data_statement
.output_section
=
1725 output_section_statement
->bfd_section
;
1727 switch (s
->data_statement
.type
)
1744 output_section_statement
->bfd_section
->_raw_size
+= size
;
1748 case lang_wild_statement_enum
:
1750 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
1751 output_section_statement
,
1752 &s
->wild_statement
.children
.head
,
1758 case lang_object_symbols_statement_enum
:
1759 link_info
.create_object_symbols_section
=
1760 output_section_statement
->bfd_section
;
1762 case lang_output_statement_enum
:
1763 case lang_target_statement_enum
:
1765 case lang_input_section_enum
:
1768 lang_input_section_type
*is
;
1771 is
= &(*prev
)->input_section
;
1774 /* FIXME: The interface to bfd_relax_section should be changed
1775 to not require the generic symbols to be read. Changing
1776 this would require changing both b_out_relax_section and
1777 bfd_coff_relax16_section. */
1778 if (is
->ifile
->asymbols
== (asymbol
**) NULL
)
1780 unsigned int symsize
;
1782 symsize
= get_symtab_upper_bound (i
->owner
);
1783 is
->ifile
->asymbols
= (asymbol
**) xmalloc (symsize
);
1784 is
->ifile
->symbol_count
=
1785 bfd_canonicalize_symtab (i
->owner
, is
->ifile
->asymbols
);
1787 /* The generic linker code in BFD requires that these
1788 symbols be stored in the outsymbols and symcount
1789 fields. When the bfd_relax_section is interface is
1790 fixed this should also be fixed. */
1791 i
->owner
->outsymbols
= is
->ifile
->asymbols
;
1792 i
->owner
->symcount
= is
->ifile
->symbol_count
;
1795 bfd_error
= no_error
;
1796 if (bfd_relax_section (i
->owner
, i
, &link_info
, is
->ifile
->asymbols
))
1798 else if (bfd_error
!= no_error
)
1799 einfo ("%P%F: can't relax section: %E");
1802 (*prev
)->input_section
.section
->_cooked_size
=
1803 (*prev
)->input_section
.section
->_raw_size
;
1806 dot
= size_input_section (prev
,
1807 output_section_statement
,
1808 output_section_statement
->fill
,
1811 case lang_input_statement_enum
:
1813 case lang_fill_statement_enum
:
1814 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
1816 fill
= s
->fill_statement
.fill
;
1818 case lang_assignment_statement_enum
:
1820 bfd_vma newdot
= dot
;
1822 exp_fold_tree (s
->assignment_statement
.exp
,
1823 output_section_statement
,
1824 lang_allocating_phase_enum
,
1828 if (newdot
!= dot
&& !relax
)
1829 /* We've been moved ! so insert a pad */
1831 lang_statement_union_type
*new =
1832 (lang_statement_union_type
*)
1833 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1835 /* Link into existing chain */
1836 new->header
.next
= *prev
;
1838 new->header
.type
= lang_padding_statement_enum
;
1839 new->padding_statement
.output_section
=
1840 output_section_statement
->bfd_section
;
1841 new->padding_statement
.output_offset
=
1842 dot
- output_section_statement
->bfd_section
->vma
;
1843 new->padding_statement
.fill
= fill
;
1844 new->padding_statement
.size
= newdot
- dot
;
1845 output_section_statement
->bfd_section
->_raw_size
+=
1846 new->padding_statement
.size
;
1855 /* This can only get here when relaxing is turned on */
1856 case lang_padding_statement_enum
:
1858 case lang_address_statement_enum
:
1861 prev
= &s
->header
.next
;
1867 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
1868 lang_statement_union_type
* s
;
1869 lang_output_section_statement_type
* output_section_statement
;
1873 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1875 switch (s
->header
.type
)
1877 case lang_constructors_statement_enum
:
1878 dot
= lang_do_assignments (constructor_list
.head
,
1879 output_section_statement
,
1884 case lang_output_section_statement_enum
:
1886 lang_output_section_statement_type
*os
=
1887 &(s
->output_section_statement
);
1889 dot
= os
->bfd_section
->vma
;
1890 (void) lang_do_assignments (os
->children
.head
, os
, os
->fill
, dot
);
1891 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1894 case lang_wild_statement_enum
:
1896 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
1897 output_section_statement
,
1902 case lang_object_symbols_statement_enum
:
1903 case lang_output_statement_enum
:
1904 case lang_target_statement_enum
:
1906 case lang_common_statement_enum
:
1909 case lang_data_statement_enum
:
1911 etree_value_type value
;
1913 value
= exp_fold_tree (s
->data_statement
.exp
,
1915 lang_final_phase_enum
, dot
, &dot
);
1916 s
->data_statement
.value
= value
.value
;
1917 if (value
.valid
== false)
1918 einfo ("%F%P: invalid data statement\n");
1920 switch (s
->data_statement
.type
)
1936 case lang_input_section_enum
:
1938 asection
*in
= s
->input_section
.section
;
1940 dot
+= bfd_get_section_size_before_reloc (in
);
1944 case lang_input_statement_enum
:
1946 case lang_fill_statement_enum
:
1947 fill
= s
->fill_statement
.fill
;
1949 case lang_assignment_statement_enum
:
1951 exp_fold_tree (s
->assignment_statement
.exp
,
1952 output_section_statement
,
1953 lang_final_phase_enum
,
1959 case lang_padding_statement_enum
:
1960 dot
+= s
->padding_statement
.size
;
1965 case lang_address_statement_enum
:
1976 struct bfd_link_hash_entry
*h
;
1977 boolean warn
= link_info
.relocateable
? false : true;
1979 if (entry_symbol
== (char *) NULL
)
1981 /* No entry has been specified. Look for start, but don't warn
1982 if we don't find it. */
1983 entry_symbol
= "start";
1987 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
1988 if (h
!= (struct bfd_link_hash_entry
*) NULL
1989 && h
->type
== bfd_link_hash_defined
)
1993 val
= (h
->u
.def
.value
1994 + bfd_get_section_vma (output_bfd
,
1995 h
->u
.def
.section
->output_section
)
1996 + h
->u
.def
.section
->output_offset
);
1997 if (! bfd_set_start_address (output_bfd
, val
))
1998 einfo ("%P%F:%s: can't set start address\n", entry_symbol
);
2004 /* Can't find the entry symbol. Use the first address in the
2006 ts
= bfd_get_section_by_name (output_bfd
, ".text");
2007 if (ts
!= (asection
*) NULL
)
2010 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2011 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
2012 if (! bfd_set_start_address (output_bfd
,
2013 bfd_get_section_vma (output_bfd
, ts
)))
2014 einfo ("%P%F: can't set start address\n");
2019 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2025 /* By now we know the target architecture, and we may have an */
2026 /* ldfile_output_machine_name */
2030 lang_statement_union_type
*file
;
2032 unsigned long input_machine
;
2033 enum bfd_architecture input_architecture
;
2034 CONST bfd_arch_info_type
*compatible
;
2036 for (file
= file_chain
.head
;
2037 file
!= (lang_statement_union_type
*) NULL
;
2038 file
= file
->input_statement
.next
)
2040 input_bfd
= file
->input_statement
.the_bfd
;
2042 input_machine
= bfd_get_mach (input_bfd
);
2043 input_architecture
= bfd_get_arch (input_bfd
);
2046 /* Inspect the architecture and ensure we're linking like with
2049 compatible
= bfd_arch_get_compatible (input_bfd
,
2054 ldfile_output_machine
= compatible
->mach
;
2055 ldfile_output_architecture
= compatible
->arch
;
2060 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2061 bfd_printable_name (input_bfd
), input_bfd
,
2062 bfd_printable_name (output_bfd
));
2064 if (! bfd_set_arch_mach (output_bfd
,
2067 einfo ("%P%F:%s: can't set architecture: %E\n",
2068 bfd_get_filename (output_bfd
));
2074 /* Look through all the global common symbols and attach them to the
2075 correct section. The -sort-common command line switch may be used
2076 to roughly sort the entries by size. */
2081 if (link_info
.relocateable
2082 && ! command_line
.force_common_definition
)
2085 if (! config
.sort_common
)
2086 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
2091 for (power
= 1; power
<= 16; power
<<= 1)
2092 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
2097 /* Place one common symbol in the correct section. */
2100 lang_one_common (h
, info
)
2101 struct bfd_link_hash_entry
*h
;
2104 unsigned int power_of_two
;
2109 if (h
->type
!= bfd_link_hash_common
)
2142 if (config
.sort_common
&& align
!= *(unsigned int *) info
)
2145 section
= h
->u
.c
.section
;
2147 /* Increase the size of the section. */
2148 section
->_raw_size
= ALIGN_N (section
->_raw_size
, align
);
2150 /* Adjust the alignment if necessary. */
2151 if (power_of_two
> section
->alignment_power
)
2152 section
->alignment_power
= power_of_two
;
2154 /* Change the symbol from common to defined. */
2155 h
->type
= bfd_link_hash_defined
;
2156 h
->u
.def
.section
= section
;
2157 h
->u
.def
.value
= section
->_raw_size
;
2159 /* Increase the size of the section. */
2160 section
->_raw_size
+= size
;
2162 if (write_map
&& config
.map_file
!= NULL
)
2163 fprintf (config
.map_file
, "Allocating common %s: %lx at %lx %s\n",
2164 h
->root
.string
, (unsigned long) size
,
2165 (unsigned long) h
->u
.def
.value
, section
->owner
->filename
);
2171 run through the input files and ensure that every input
2172 section has somewhere to go. If one is found without
2173 a destination then create an input request and place it
2174 into the statement tree.
2178 lang_place_orphans ()
2180 lang_input_statement_type
*file
;
2182 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2183 file
!= (lang_input_statement_type
*) NULL
;
2184 file
= (lang_input_statement_type
*) file
->next
)
2188 for (s
= file
->the_bfd
->sections
;
2189 s
!= (asection
*) NULL
;
2192 if (s
->output_section
== (asection
*) NULL
)
2194 /* This section of the file is not attatched, root
2195 around for a sensible place for it to go */
2197 if (file
->common_section
== s
)
2199 /* This is a lonely common section which must
2200 have come from an archive. We attatch to the
2201 section with the wildcard */
2202 if (! link_info
.relocateable
2203 && ! command_line
.force_common_definition
)
2205 if (default_common_section
==
2206 (lang_output_section_statement_type
*) NULL
)
2208 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2210 default_common_section
=
2211 lang_output_section_statement_lookup (".bss");
2214 wild_doit (&default_common_section
->children
, s
,
2215 default_common_section
, file
);
2220 lang_output_section_statement_type
*os
=
2221 lang_output_section_statement_lookup (s
->name
);
2223 wild_doit (&os
->children
, s
, os
, file
);
2232 lang_set_flags (ptr
, flags
)
2236 boolean state
= false;
2251 /* ptr->flag_read = state; */
2254 /* ptr->flag_write = state; */
2257 /* ptr->flag_executable= state;*/
2261 /* ptr->flag_loadable= state;*/
2264 einfo ("%P%F: invalid syntax in flags\n");
2274 lang_for_each_file (func
)
2275 void (*func
) PARAMS ((lang_input_statement_type
*));
2277 lang_input_statement_type
*f
;
2279 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2280 f
!= (lang_input_statement_type
*) NULL
;
2281 f
= (lang_input_statement_type
*) f
->next
)
2292 lang_for_each_input_section (func
)
2293 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
2295 lang_input_statement_type
*f
;
2297 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2298 f
!= (lang_input_statement_type
*) NULL
;
2299 f
= (lang_input_statement_type
*) f
->next
)
2303 for (s
= f
->the_bfd
->sections
;
2304 s
!= (asection
*) NULL
;
2307 func (f
->the_bfd
, s
);
2315 ldlang_add_file (entry
)
2316 lang_input_statement_type
* entry
;
2320 lang_statement_append (&file_chain
,
2321 (lang_statement_union_type
*) entry
,
2324 /* The BFD linker needs to have a list of all input BFDs involved in
2326 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
2327 ASSERT (entry
->the_bfd
!= output_bfd
);
2328 for (pp
= &link_info
.input_bfds
;
2329 *pp
!= (bfd
*) NULL
;
2330 pp
= &(*pp
)->link_next
)
2332 *pp
= entry
->the_bfd
;
2333 entry
->the_bfd
->usrdata
= (PTR
) entry
;
2337 lang_add_output (name
, from_script
)
2341 /* Make -o on command line override OUTPUT in script. */
2342 if (had_output_filename
== false || !from_script
)
2344 output_filename
= name
;
2345 had_output_filename
= true;
2350 static lang_output_section_statement_type
*current_section
;
2352 static int topower(x
)
2357 if (x
< 0) return -1;
2358 for (l
= 0; l
< 32; l
++)
2360 if (i
>= x
) return l
;
2366 lang_enter_output_section_statement (output_section_statement_name
,
2367 address_exp
, flags
, block_value
,
2368 align
, subalign
, ebase
)
2369 const char *output_section_statement_name
;
2370 etree_type
* address_exp
;
2372 bfd_vma block_value
;
2374 etree_type
*subalign
;
2377 lang_output_section_statement_type
*os
;
2381 lang_output_section_statement_lookup (output_section_statement_name
);
2385 /* Add this statement to tree */
2386 /* add_statement(lang_output_section_statement_enum,
2387 output_section_statement);*/
2388 /* Make next things chain into subchain of this */
2390 if (os
->addr_tree
==
2391 (etree_type
*) NULL
)
2397 if (flags
& SEC_NEVER_LOAD
)
2401 os
->block_value
= block_value
? block_value
: 1;
2402 stat_ptr
= &os
->children
;
2404 os
->subsection_alignment
= topower(
2405 exp_get_value_int(subalign
, -1,
2406 "subsection alignment",
2408 os
->section_alignment
= topower(
2409 exp_get_value_int(align
, -1,
2410 "section alignment", 0));
2412 os
->load_base
= ebase
;
2419 lang_output_statement_type
*new =
2420 new_stat (lang_output_statement
, stat_ptr
);
2422 new->name
= output_filename
;
2425 /* Reset the current counters in the regions */
2427 reset_memory_regions ()
2429 lang_memory_region_type
*p
= lang_memory_region_list
;
2431 for (p
= lang_memory_region_list
;
2432 p
!= (lang_memory_region_type
*) NULL
;
2435 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
2436 p
->current
= p
->origin
;
2443 lang_reasonable_defaults ();
2444 current_target
= default_target
;
2446 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2447 /* For each output section statement, create a section in the output
2449 lang_create_output_section_statements ();
2451 ldemul_create_output_section_statements ();
2453 /* Add to the hash table all undefineds on the command line */
2454 lang_place_undefineds ();
2456 /* Create a bfd for each input file */
2457 current_target
= default_target
;
2458 lang_for_each_statement (open_input_bfds
);
2460 /* Build all sets based on the information gathered from the input
2462 ldctor_build_sets ();
2464 /* Run through the contours of the script and attatch input sections
2465 to the correct output sections
2467 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2468 (lang_output_section_statement_type
*) NULL
);
2471 /* Find any sections not attatched explicitly and handle them */
2472 lang_place_orphans ();
2474 /* Size up the common data */
2477 ldemul_before_allocation ();
2487 lang_size_sections (statement_list
.head
,
2488 (lang_output_section_statement_type
*) NULL
,
2489 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2490 /* FIXME. Until the code in relax is fixed so that it only reads in
2491 stuff once, we cant iterate since there is no way for the linker to
2492 know what has been patched and what hasn't */
2498 /* Now run around and relax if we can */
2499 if (command_line
.relax
)
2501 /* First time round is a trial run to get the 'worst case'
2502 addresses of the objects if there was no relaxing. */
2503 lang_size_sections (statement_list
.head
,
2504 (lang_output_section_statement_type
*) NULL
,
2505 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2508 reset_memory_regions ();
2510 /* Do all the assignments, now that we know the final resting
2511 places of all the symbols. */
2513 lang_do_assignments (statement_list
.head
,
2515 (fill_type
) 0, (bfd_vma
) 0);
2517 /* Perform another relax pass - this time we know where the
2518 globals are, so can make better guess. */
2519 lang_size_sections (statement_list
.head
,
2520 (lang_output_section_statement_type
*) NULL
,
2521 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2525 /* Size up the sections. */
2526 lang_size_sections (statement_list
.head
,
2528 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2531 /* See if anything special should be done now we know how big
2533 ldemul_after_allocation ();
2535 /* Do all the assignments, now that we know the final restingplaces
2536 of all the symbols */
2538 lang_do_assignments (statement_list
.head
,
2540 (fill_type
) 0, (bfd_vma
) 0);
2542 /* Make sure that we're not mixing architectures */
2551 /* DO NOT REENABLE THIS CALL. IF THIS CALL IS MADE, THE SUN4 LINKER
2552 CAN NOT BOOTSTRAP!! No, I don't know why, but don't change it
2553 unless you fix it. */
2554 /* Size up the sections. */
2555 lang_size_sections (statement_list
.head
,
2557 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2563 /* EXPORTED TO YACC */
2566 lang_add_wild (section_name
, filename
)
2567 CONST
char *CONST section_name
;
2568 CONST
char *CONST filename
;
2570 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
2573 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
2575 placed_commons
= true;
2577 if (filename
!= (char *) NULL
)
2579 lang_has_input_file
= true;
2581 new->section_name
= section_name
;
2582 new->filename
= filename
;
2583 lang_list_init (&new->children
);
2587 lang_section_start (name
, address
)
2589 etree_type
* address
;
2591 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
2593 ad
->section_name
= name
;
2594 ad
->address
= address
;
2598 lang_add_entry (name
)
2601 entry_symbol
= name
;
2605 lang_add_target (name
)
2608 lang_target_statement_type
*new = new_stat (lang_target_statement
,
2624 map_option_f
= true;
2635 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
2642 lang_add_data (type
, exp
)
2644 union etree_union
*exp
;
2647 lang_data_statement_type
*new = new_stat (lang_data_statement
,
2656 lang_add_assignment (exp
)
2659 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
2666 lang_add_attribute (attribute
)
2667 enum statement_enum attribute
;
2669 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
2676 if (startup_file
!= (char *) NULL
)
2678 einfo ("%P%Fmultiple STARTUP files\n");
2680 first_file
->filename
= name
;
2681 first_file
->local_sym_name
= name
;
2683 startup_file
= name
;
2690 lang_float_flag
= maybe
;
2694 lang_leave_output_section_statement (fill
, memspec
)
2696 CONST
char *memspec
;
2698 current_section
->fill
= fill
;
2699 current_section
->region
= lang_memory_region_lookup (memspec
);
2700 stat_ptr
= &statement_list
;
2702 /* We remember if we are closing a .data section, since we use it to
2703 store constructors in */
2704 if (strcmp (current_section
->name
, ".data") == 0)
2706 end_of_data_section_statement_list
= statement_list
;
2712 Create an absolute symbol with the given name with the value of the
2713 address of first byte of the section named.
2715 If the symbol already exists, then do nothing.
2718 lang_abs_symbol_at_beginning_of (secname
, name
)
2719 const char *secname
;
2722 struct bfd_link_hash_entry
*h
;
2724 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
2725 if (h
== (struct bfd_link_hash_entry
*) NULL
)
2726 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2728 if (h
->type
== bfd_link_hash_new
2729 || h
->type
== bfd_link_hash_undefined
)
2733 h
->type
= bfd_link_hash_defined
;
2735 sec
= bfd_get_section_by_name (output_bfd
, secname
);
2736 if (sec
== (asection
*) NULL
)
2739 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
2741 h
->u
.def
.section
= &bfd_abs_section
;
2746 Create an absolute symbol with the given name with the value of the
2747 address of the first byte after the end of the section named.
2749 If the symbol already exists, then do nothing.
2752 lang_abs_symbol_at_end_of (secname
, name
)
2753 const char *secname
;
2756 struct bfd_link_hash_entry
*h
;
2758 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
2759 if (h
== (struct bfd_link_hash_entry
*) NULL
)
2760 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2762 if (h
->type
== bfd_link_hash_new
2763 || h
->type
== bfd_link_hash_undefined
)
2767 h
->type
= bfd_link_hash_defined
;
2769 sec
= bfd_get_section_by_name (output_bfd
, secname
);
2770 if (sec
== (asection
*) NULL
)
2773 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
2774 + bfd_section_size (output_bfd
, sec
));
2776 h
->u
.def
.section
= &bfd_abs_section
;
2781 lang_statement_append (list
, element
, field
)
2782 lang_statement_list_type
* list
;
2783 lang_statement_union_type
* element
;
2784 lang_statement_union_type
** field
;
2786 *(list
->tail
) = element
;
2790 /* Set the output format type. -oformat overrides scripts. */
2792 lang_add_output_format (format
, from_script
)
2796 if (output_target
== NULL
|| !from_script
)
2797 output_target
= format
;