1 /* Linker command language support.
2 Copyright 1991, 1992, 1993 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 ldmalloc
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
));
90 static void wild
PARAMS ((lang_wild_statement_type
*s
,
91 const char *section
, const char *file
,
93 lang_output_section_statement_type
*output
));
94 static bfd
*open_output
PARAMS ((const char *name
));
95 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*statement
));
96 static void open_input_bfds
PARAMS ((lang_statement_union_type
*statement
));
97 static void lang_reasonable_defaults
PARAMS ((void));
98 static void lang_place_undefineds
PARAMS ((void));
99 static void lang_create_output_section_statements
PARAMS ((void));
100 static void map_input_to_output_sections
101 PARAMS ((lang_statement_union_type
*s
,
103 lang_output_section_statement_type
*output_section_statement
));
104 static void print_output_section_statement
105 PARAMS ((lang_output_section_statement_type
*output_section_statement
));
106 static void print_assignment
107 PARAMS ((lang_assignment_statement_type
*assignment
,
108 lang_output_section_statement_type
*output_section
));
109 static void print_input_statement
PARAMS ((lang_input_statement_type
*statm
));
110 static void print_symbol
PARAMS ((asymbol
*q
));
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));
149 lang_output_section_statement_type
*abs_output_section
;
150 lang_statement_list_type
*stat_ptr
= &statement_list
;
151 lang_statement_list_type file_chain
=
153 CONST
char *entry_symbol
= 0;
154 bfd_size_type largest_section
= 0;
155 boolean lang_has_input_file
= false;
156 boolean had_output_filename
= false;
157 boolean lang_float_flag
= false;
158 boolean delete_output_file_on_failure
= false;
160 etree_type
*base
; /* Relocation base - or null */
164 #define cat(a,b) a##b
166 #define cat(a,b) a/**/b
169 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
171 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
173 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
179 return obstack_alloc (&stat_obstack
, size
);
186 fprintf (config
.map_file
, "%5x", (unsigned) value
);
190 print_alignment (value
)
193 fprintf (config
.map_file
, "2**%1u", value
);
200 fprintf (config
.map_file
, "%04x", (unsigned) value
);
207 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
210 /*----------------------------------------------------------------------
211 lang_for_each_statement walks the parse tree and calls the provided
212 function for each node
216 lang_for_each_statement_worker (func
, s
)
217 void (*func
) PARAMS ((lang_statement_union_type
*));
218 lang_statement_union_type
*s
;
220 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
224 switch (s
->header
.type
)
226 case lang_constructors_statement_enum
:
227 lang_for_each_statement_worker (func
, constructor_list
.head
);
229 case lang_output_section_statement_enum
:
230 lang_for_each_statement_worker
232 s
->output_section_statement
.children
.head
);
234 case lang_wild_statement_enum
:
235 lang_for_each_statement_worker
237 s
->wild_statement
.children
.head
);
239 case lang_data_statement_enum
:
240 case lang_object_symbols_statement_enum
:
241 case lang_output_statement_enum
:
242 case lang_target_statement_enum
:
243 case lang_input_section_enum
:
244 case lang_input_statement_enum
:
245 case lang_assignment_statement_enum
:
246 case lang_padding_statement_enum
:
247 case lang_address_statement_enum
:
257 lang_for_each_statement (func
)
258 void (*func
) PARAMS ((lang_statement_union_type
*));
260 lang_for_each_statement_worker (func
,
261 statement_list
.head
);
264 /*----------------------------------------------------------------------*/
266 lang_list_init (list
)
267 lang_statement_list_type
*list
;
269 list
->head
= (lang_statement_union_type
*) NULL
;
270 list
->tail
= &list
->head
;
273 /*----------------------------------------------------------------------
275 build a new statement node for the parse tree
280 lang_statement_union_type
*
281 new_statement (type
, size
, list
)
282 enum statement_enum type
;
284 lang_statement_list_type
* list
;
286 lang_statement_union_type
*new = (lang_statement_union_type
*)
289 new->header
.type
= type
;
290 new->header
.next
= (lang_statement_union_type
*) NULL
;
291 lang_statement_append (list
, new, &new->header
.next
);
296 Build a new input file node for the language. There are several ways
297 in which we treat an input file, eg, we only look at symbols, or
298 prefix it with a -l etc.
300 We can be supplied with requests for input files more than once;
301 they may, for example be split over serveral lines like foo.o(.text)
302 foo.o(.data) etc, so when asked for a file we check that we havn't
303 got it already so we don't duplicate the bfd.
306 static lang_input_statement_type
*
307 new_afile (name
, file_type
, target
)
309 lang_input_file_enum_type file_type
;
313 lang_input_statement_type
*p
= new_stat (lang_input_statement
,
316 lang_has_input_file
= true;
318 p
->complained
= false;
321 case lang_input_file_is_symbols_only_enum
:
323 p
->is_archive
= false;
325 p
->local_sym_name
= name
;
326 p
->just_syms_flag
= true;
327 p
->search_dirs_flag
= false;
329 case lang_input_file_is_fake_enum
:
331 p
->is_archive
= false;
333 p
->local_sym_name
= name
;
334 p
->just_syms_flag
= false;
335 p
->search_dirs_flag
= false;
337 case lang_input_file_is_l_enum
:
338 p
->is_archive
= true;
341 p
->local_sym_name
= concat ("-l", name
, "");
342 p
->just_syms_flag
= false;
343 p
->search_dirs_flag
= true;
345 case lang_input_file_is_search_file_enum
:
346 case lang_input_file_is_marker_enum
:
348 p
->is_archive
= false;
350 p
->local_sym_name
= name
;
351 p
->just_syms_flag
= false;
352 p
->search_dirs_flag
= true;
354 case lang_input_file_is_file_enum
:
356 p
->is_archive
= false;
358 p
->local_sym_name
= name
;
359 p
->just_syms_flag
= false;
360 p
->search_dirs_flag
= false;
365 p
->asymbols
= (asymbol
**) NULL
;
366 p
->superfile
= (lang_input_statement_type
*) NULL
;
367 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
368 p
->next
= (lang_statement_union_type
*) NULL
;
370 p
->common_output_section
= (asection
*) NULL
;
372 lang_statement_append (&input_file_chain
,
373 (lang_statement_union_type
*) p
,
378 lang_input_statement_type
*
379 lang_add_input_file (name
, file_type
, target
)
381 lang_input_file_enum_type file_type
;
384 /* Look it up or build a new one */
385 lang_has_input_file
= true;
388 lang_input_statement_type
*p
;
390 for (p
= (lang_input_statement_type
*) input_file_chain
.head
;
391 p
!= (lang_input_statement_type
*) NULL
;
392 p
= (lang_input_statement_type
*) (p
->next_real_file
))
394 /* Sometimes we have incomplete entries in here */
395 if (p
->filename
!= (char *) NULL
)
397 if (strcmp (name
, p
->filename
) == 0)
403 return new_afile (name
, file_type
, target
);
406 /* Build enough state so that the parser can build its tree */
410 obstack_begin (&stat_obstack
, 1000);
412 stat_ptr
= &statement_list
;
414 lang_list_init (stat_ptr
);
416 lang_list_init (&input_file_chain
);
417 lang_list_init (&lang_output_section_statement
);
418 lang_list_init (&file_chain
);
419 first_file
= lang_add_input_file ((char *) NULL
,
420 lang_input_file_is_marker_enum
,
422 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
424 abs_output_section
->bfd_section
= &bfd_abs_section
;
428 /*----------------------------------------------------------------------
429 A region is an area of memory declared with the
430 MEMORY { name:org=exp, len=exp ... }
433 We maintain a list of all the regions here
435 If no regions are specified in the script, then the default is used
436 which is created when looked up to be the entire data space
439 static lang_memory_region_type
*lang_memory_region_list
;
440 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
442 lang_memory_region_type
*
443 lang_memory_region_lookup (name
)
444 CONST
char *CONST name
;
447 lang_memory_region_type
*p
= lang_memory_region_list
;
449 for (p
= lang_memory_region_list
;
450 p
!= (lang_memory_region_type
*) NULL
;
453 if (strcmp (p
->name
, name
) == 0)
458 if (strcmp (name
, "*default*") == 0)
460 /* This is the default region, dig out first one on the list */
461 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
463 return lang_memory_region_list
;
467 lang_memory_region_type
*new =
468 (lang_memory_region_type
*) stat_alloc ((bfd_size_type
) (sizeof (lang_memory_region_type
)));
470 new->name
= buystring (name
);
471 new->next
= (lang_memory_region_type
*) NULL
;
473 *lang_memory_region_list_tail
= new;
474 lang_memory_region_list_tail
= &new->next
;
476 new->length
= ~(bfd_size_type
)0;
478 new->had_full_message
= false;
485 lang_output_section_statement_type
*
486 lang_output_section_find (name
)
487 CONST
char *CONST name
;
489 lang_statement_union_type
*u
;
490 lang_output_section_statement_type
*lookup
;
492 for (u
= lang_output_section_statement
.head
;
493 u
!= (lang_statement_union_type
*) NULL
;
496 lookup
= &u
->output_section_statement
;
497 if (strcmp (name
, lookup
->name
) == 0)
502 return (lang_output_section_statement_type
*) NULL
;
505 lang_output_section_statement_type
*
506 lang_output_section_statement_lookup (name
)
507 CONST
char *CONST name
;
509 lang_output_section_statement_type
*lookup
;
511 lookup
= lang_output_section_find (name
);
512 if (lookup
== (lang_output_section_statement_type
*) NULL
)
515 lookup
= (lang_output_section_statement_type
*)
516 new_stat (lang_output_section_statement
, stat_ptr
);
517 lookup
->region
= (lang_memory_region_type
*) NULL
;
519 lookup
->block_value
= 1;
522 lookup
->next
= (lang_statement_union_type
*) NULL
;
523 lookup
->bfd_section
= (asection
*) NULL
;
524 lookup
->processed
= false;
525 lookup
->loadable
= 1;
526 lookup
->addr_tree
= (etree_type
*) NULL
;
527 lang_list_init (&lookup
->children
);
529 lookup
->memspec
= (CONST
char *) NULL
;
531 lookup
->subsection_alignment
= -1;
532 lookup
->section_alignment
= -1;
533 lookup
->load_base
= (union etree_union
*) NULL
;
535 lang_statement_append (&lang_output_section_statement
,
536 (lang_statement_union_type
*) lookup
,
544 print_flags (ignore_flags
)
547 fprintf (config
.map_file
, "(");
549 if (flags
->flag_read
)
550 fprintf (outfile
, "R");
551 if (flags
->flag_write
)
552 fprintf (outfile
, "W");
553 if (flags
->flag_executable
)
554 fprintf (outfile
, "X");
555 if (flags
->flag_loadable
)
556 fprintf (outfile
, "L");
558 fprintf (config
.map_file
, ")");
564 lang_memory_region_type
*m
;
566 fprintf (config
.map_file
, "**MEMORY CONFIGURATION**\n\n");
568 fprintf (config
.map_file
, "name\t\torigin\t\tlength\t\tattributes\n");
570 fprintf (config
.map_file
,
571 "name\t\torigin length r_size c_size is attributes\n");
574 for (m
= lang_memory_region_list
;
575 m
!= (lang_memory_region_type
*) NULL
;
578 fprintf (config
.map_file
, "%-16s", m
->name
);
579 print_address (m
->origin
);
581 print_address ((bfd_vma
)m
->length
);
583 print_address ((bfd_vma
)m
->old_length
);
585 print_address (m
->current
- m
->origin
);
588 fprintf (config
.map_file
, " %2d%% ",
589 (int) ((m
->current
- m
->origin
) * 100 / m
->old_length
));
590 print_flags (&m
->flags
);
591 fprintf (config
.map_file
, "\n");
593 fprintf (config
.map_file
, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
594 fprintf (config
.map_file
, "output input virtual\n");
595 fprintf (config
.map_file
, "section section address tsize\n\n");
606 lang_output_section_statement_type
* s
;
608 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
609 section_userdata_type
*new =
610 (section_userdata_type
*)
611 stat_alloc ((bfd_size_type
) (sizeof (section_userdata_type
)));
613 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
614 if (s
->bfd_section
== (asection
*) NULL
)
615 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
616 if (s
->bfd_section
== (asection
*) NULL
)
618 einfo ("%P%F: output format %s cannot represent section called %s\n",
619 output_bfd
->xvec
->name
, s
->name
);
621 s
->bfd_section
->output_section
= s
->bfd_section
;
622 /* s->bfd_section->flags = s->flags;*/
624 /* We initialize an output sections output offset to minus its own */
625 /* vma to allow us to output a section through itself */
626 s
->bfd_section
->output_offset
= 0;
627 get_userdata (s
->bfd_section
) = (PTR
) new;
631 /***********************************************************************
634 These expand statements like *(.text) and foo.o to a list of
635 explicit actions, like foo.o(.text), bar.o(.text) and
638 The toplevel routine, wild, takes a statement, section, file and
639 target. If either the section or file is null it is taken to be the
640 wildcard. Seperate lang_input_section statements are created for
641 each part of the expanstion, and placed after the statement provided.
646 wild_doit (ptr
, section
, output
, file
)
647 lang_statement_list_type
* ptr
;
649 lang_output_section_statement_type
* output
;
650 lang_input_statement_type
* file
;
652 if (output
->bfd_section
== (asection
*) NULL
)
655 /* Initialize the vma and size to the existing section. This will
656 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
658 if (section
!= (asection
*) NULL
)
660 bfd_set_section_vma (0, output
->bfd_section
,
661 bfd_section_vma (0, section
));
662 output
->bfd_section
->_raw_size
= section
->_raw_size
;
666 if (section
!= (asection
*) NULL
667 && section
->output_section
== (asection
*) NULL
)
669 /* Add a section reference to the list */
670 lang_input_section_type
*new = new_stat (lang_input_section
, ptr
);
672 new->section
= section
;
674 section
->output_section
= output
->bfd_section
;
676 /* Be selective about what the output section inherits from the
679 if ((section
->flags
& SEC_SHARED_LIBRARY
) != 0)
680 section
->output_section
->flags
|= section
->flags
;
682 section
->output_section
->flags
|=
683 section
->flags
& (flagword
) (~ SEC_NEVER_LOAD
);
685 if (!output
->loadable
)
687 /* Turn off load flag */
688 output
->bfd_section
->flags
&= ~SEC_LOAD
;
689 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
691 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
693 output
->bfd_section
->alignment_power
= section
->alignment_power
;
695 /* If supplied an aligmnet, then force it */
696 if (output
->section_alignment
!= -1)
698 output
->bfd_section
->alignment_power
= output
->section_alignment
;
704 our_bfd_get_section_by_name (abfd
, section
)
708 return bfd_get_section_by_name (abfd
, section
);
712 wild_section (ptr
, section
, file
, output
)
713 lang_wild_statement_type
* ptr
;
715 lang_input_statement_type
* file
;
716 lang_output_section_statement_type
* output
;
720 if (file
->just_syms_flag
== false)
722 if (section
== (char *) NULL
)
724 /* Do the creation to all sections in the file */
725 for (s
= file
->the_bfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
728 if ((s
->flags
& SEC_IS_COMMON
) == 0)
730 wild_doit (&ptr
->children
, s
, output
, file
);
736 /* Do the creation to the named section only */
737 wild_doit (&ptr
->children
,
738 our_bfd_get_section_by_name (file
->the_bfd
, section
),
744 /* passed a file name (which must have been seen already and added to
745 the statement tree. We will see if it has been opened already and
746 had its symbols read. If not then we'll read it.
748 Archives are pecuilar here. We may open them once, but if they do
749 not define anything we need at the time, they won't have all their
750 symbols read. If we need them later, we'll have to redo it.
753 lang_input_statement_type
*
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. */
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 if (bfd_link_add_symbols (search
->the_bfd
, &link_info
) == false)
805 einfo ("%F%B: could not read symbols: %E\n", search
->the_bfd
);
807 search
->loaded
= true;
813 wild (s
, section
, file
, target
, output
)
814 lang_wild_statement_type
* s
;
818 lang_output_section_statement_type
* output
;
820 lang_input_statement_type
*f
;
822 if (file
== (char *) NULL
)
824 /* Perform the iteration over all files in the list */
825 for (f
= (lang_input_statement_type
*) file_chain
.head
;
826 f
!= (lang_input_statement_type
*) NULL
;
827 f
= (lang_input_statement_type
*) f
->next
)
829 wild_section (s
, section
, f
, output
);
834 /* Perform the iteration over a single file */
835 wild_section (s
, section
, lookup_name (file
), output
);
837 if (section
!= (char *) NULL
838 && strcmp (section
, "COMMON") == 0
839 && default_common_section
== (lang_output_section_statement_type
*) NULL
)
841 /* Remember the section that common is going to incase we later
842 get something which doesn't know where to put it */
843 default_common_section
= output
;
848 read in all the files
857 if (output_target
== (char *) NULL
)
859 if (current_target
!= (char *) NULL
)
860 output_target
= current_target
;
862 output_target
= default_target
;
864 output
= bfd_openw (name
, output_target
);
866 if (output
== (bfd
*) NULL
)
868 if (bfd_error
== invalid_target
)
870 einfo ("%P%F: target %s not found\n", output_target
);
872 einfo ("%P%F: cannot open output file %s: %E\n", name
);
875 delete_output_file_on_failure
= 1;
877 /* output->flags |= D_PAGED;*/
879 if (! bfd_set_format (output
, bfd_object
))
880 einfo ("%P%F:%s: can not make object file: %E\n", name
);
881 if (! bfd_set_arch_mach (output
,
882 ldfile_output_architecture
,
883 ldfile_output_machine
))
884 einfo ("%P%F:%s: can not set architecture: %E\n", name
);
886 link_info
.hash
= bfd_link_hash_table_create (output
);
887 if (link_info
.hash
== (struct bfd_link_hash_table
*) NULL
)
888 einfo ("%P%F: can not create link hash table: %E\n");
890 bfd_set_gp_size (output
, g_switch_value
);
898 ldlang_open_output (statement
)
899 lang_statement_union_type
* statement
;
901 switch (statement
->header
.type
)
903 case lang_output_statement_enum
:
904 ASSERT (output_bfd
== (bfd
*) NULL
);
905 output_bfd
= open_output (statement
->output_statement
.name
);
906 ldemul_set_output_arch ();
907 if (config
.magic_demand_paged
&& !link_info
.relocateable
)
908 output_bfd
->flags
|= D_PAGED
;
910 output_bfd
->flags
&= ~D_PAGED
;
911 if (config
.text_read_only
)
912 output_bfd
->flags
|= WP_TEXT
;
914 output_bfd
->flags
&= ~WP_TEXT
;
917 case lang_target_statement_enum
:
918 current_target
= statement
->target_statement
.target
;
926 open_input_bfds (statement
)
927 lang_statement_union_type
* statement
;
929 switch (statement
->header
.type
)
931 case lang_target_statement_enum
:
932 current_target
= statement
->target_statement
.target
;
934 case lang_wild_statement_enum
:
935 /* Maybe we should load the file's symbols */
936 if (statement
->wild_statement
.filename
)
938 (void) lookup_name (statement
->wild_statement
.filename
);
941 case lang_input_statement_enum
:
942 if (statement
->input_statement
.real
== true)
944 statement
->input_statement
.target
= current_target
;
945 lookup_name (statement
->input_statement
.filename
);
953 /* If there are [COMMONS] statements, put a wild one into the bss section */
956 lang_reasonable_defaults ()
959 lang_output_section_statement_lookup (".text");
960 lang_output_section_statement_lookup (".data");
962 default_common_section
=
963 lang_output_section_statement_lookup (".bss");
966 if (placed_commons
== false)
968 lang_wild_statement_type
*new =
969 new_stat (lang_wild_statement
,
970 &default_common_section
->children
);
972 new->section_name
= "COMMON";
973 new->filename
= (char *) NULL
;
974 lang_list_init (&new->children
);
981 Add the supplied name to the symbol table as an undefined reference.
982 Remove items from the chain as we open input bfds
984 typedef struct ldlang_undef_chain_list
986 struct ldlang_undef_chain_list
*next
;
988 } ldlang_undef_chain_list_type
;
990 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
993 ldlang_add_undef (name
)
994 CONST
char *CONST name
;
996 ldlang_undef_chain_list_type
*new =
997 (ldlang_undef_chain_list_type
998 *) stat_alloc ((bfd_size_type
) (sizeof (ldlang_undef_chain_list_type
)));
1000 new->next
= ldlang_undef_chain_list_head
;
1001 ldlang_undef_chain_list_head
= new;
1003 new->name
= buystring (name
);
1006 /* Run through the list of undefineds created above and place them
1007 into the linker hash table as undefined symbols belonging to the
1011 lang_place_undefineds ()
1013 ldlang_undef_chain_list_type
*ptr
;
1015 for (ptr
= ldlang_undef_chain_list_head
;
1016 ptr
!= (ldlang_undef_chain_list_type
*) NULL
;
1019 struct bfd_link_hash_entry
*h
;
1021 h
= bfd_link_hash_lookup (link_info
.hash
, ptr
->name
, true, false, true);
1022 if (h
== (struct bfd_link_hash_entry
*) NULL
)
1023 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1024 if (h
->type
== bfd_link_hash_new
)
1026 h
->type
= bfd_link_hash_undefined
;
1027 h
->u
.undef
.abfd
= NULL
;
1028 bfd_link_add_undef (link_info
.hash
, h
);
1033 /* Copy important data from out internal form to the bfd way. Also
1034 create a section for the dummy file
1038 lang_create_output_section_statements ()
1040 lang_statement_union_type
*os
;
1042 for (os
= lang_output_section_statement
.head
;
1043 os
!= (lang_statement_union_type
*) NULL
;
1044 os
= os
->output_section_statement
.next
)
1046 lang_output_section_statement_type
*s
=
1047 &os
->output_section_statement
;
1054 /* Open input files and attatch to output sections */
1056 map_input_to_output_sections (s
, target
, output_section_statement
)
1057 lang_statement_union_type
* s
;
1059 lang_output_section_statement_type
* output_section_statement
;
1061 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1063 switch (s
->header
.type
)
1067 case lang_wild_statement_enum
:
1068 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1069 s
->wild_statement
.filename
, target
,
1070 output_section_statement
);
1073 case lang_constructors_statement_enum
:
1074 map_input_to_output_sections (constructor_list
.head
,
1076 output_section_statement
);
1078 case lang_output_section_statement_enum
:
1079 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1081 &s
->output_section_statement
);
1083 case lang_output_statement_enum
:
1085 case lang_target_statement_enum
:
1086 target
= s
->target_statement
.target
;
1088 case lang_fill_statement_enum
:
1089 case lang_input_section_enum
:
1090 case lang_object_symbols_statement_enum
:
1091 case lang_data_statement_enum
:
1092 case lang_assignment_statement_enum
:
1093 case lang_padding_statement_enum
:
1095 case lang_afile_asection_pair_statement_enum
:
1098 case lang_address_statement_enum
:
1099 /* Mark the specified section with the supplied address */
1101 lang_output_section_statement_type
*os
=
1102 lang_output_section_statement_lookup
1103 (s
->address_statement
.section_name
);
1105 os
->addr_tree
= s
->address_statement
.address
;
1106 if (os
->bfd_section
== (asection
*) NULL
)
1108 einfo ("%P%F: cannot set the address of undefined section %s\n",
1109 s
->address_statement
.section_name
);
1113 case lang_input_statement_enum
:
1114 /* A standard input statement, has no wildcards */
1121 print_output_section_statement (output_section_statement
)
1122 lang_output_section_statement_type
* output_section_statement
;
1124 asection
*section
= output_section_statement
->bfd_section
;
1127 print_section (output_section_statement
->name
);
1132 print_dot
= section
->vma
;
1136 print_address (section
->vma
);
1138 print_size (section
->_raw_size
);
1140 print_size(section
->_cooked_size
);
1142 print_alignment (section
->alignment_power
);
1145 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1146 print_flags (stdout
, &output_section_statement
->flags
);
1148 if (section
->flags
& SEC_LOAD
)
1149 fprintf (config
.map_file
, "load ");
1150 if (section
->flags
& SEC_ALLOC
)
1151 fprintf (config
.map_file
, "alloc ");
1152 if (section
->flags
& SEC_RELOC
)
1153 fprintf (config
.map_file
, "reloc ");
1154 if (section
->flags
& SEC_HAS_CONTENTS
)
1155 fprintf (config
.map_file
, "contents ");
1160 fprintf (config
.map_file
, "No attached output section");
1163 if (output_section_statement
->load_base
)
1165 int b
= exp_get_value_int(output_section_statement
->load_base
,
1166 0, "output base", lang_final_phase_enum
);
1167 printf("Output address %08x\n", b
);
1169 if (output_section_statement
->section_alignment
>= 0
1170 || output_section_statement
->section_alignment
>= 0)
1172 printf("\t\t\t\t\tforced alignment ");
1173 if ( output_section_statement
->section_alignment
>= 0)
1175 printf("section 2**%d ",output_section_statement
->section_alignment
);
1177 if ( output_section_statement
->subsection_alignment
>= 0)
1179 printf("subsection 2**%d ",output_section_statement
->subsection_alignment
);
1184 print_statement (output_section_statement
->children
.head
,
1185 output_section_statement
);
1190 print_assignment (assignment
, output_section
)
1191 lang_assignment_statement_type
* assignment
;
1192 lang_output_section_statement_type
* output_section
;
1194 etree_value_type result
;
1200 print_address (print_dot
);
1202 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1204 lang_final_phase_enum
,
1210 print_address (result
.value
);
1214 fprintf (config
.map_file
, "*undefined*");
1217 exp_print_tree (assignment
->exp
);
1219 fprintf (config
.map_file
, "\n");
1223 print_input_statement (statm
)
1224 lang_input_statement_type
* statm
;
1226 if (statm
->filename
!= (char *) NULL
)
1228 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1237 fprintf (config
.map_file
, " ");
1239 fprintf (config
.map_file
, " ");
1240 print_address (outside_symbol_address (q
));
1241 fprintf (config
.map_file
, " %s", q
->name
? q
->name
: " ");
1242 if (q
->flags
& BSF_WEAK
)
1243 fprintf (config
.map_file
, " *weak*");
1248 print_input_section (in
)
1249 lang_input_section_type
* in
;
1251 asection
*i
= in
->section
;
1252 int size
= i
->reloc_done
?
1253 bfd_get_section_size_after_reloc (i
) :
1254 bfd_get_section_size_before_reloc (i
);
1259 fprintf (config
.map_file
, " ");
1260 print_section (i
->name
);
1261 fprintf (config
.map_file
, " ");
1262 if (i
->output_section
)
1264 print_address (i
->output_section
->vma
+ i
->output_offset
);
1265 fprintf (config
.map_file
, " ");
1266 print_size (i
->_raw_size
);
1267 fprintf (config
.map_file
, " ");
1268 print_size(i
->_cooked_size
);
1269 fprintf (config
.map_file
, " ");
1270 print_alignment (i
->alignment_power
);
1271 fprintf (config
.map_file
, " ");
1275 bfd
*abfd
= in
->ifile
->the_bfd
;
1277 if (in
->ifile
->just_syms_flag
== true)
1279 fprintf (config
.map_file
, "symbols only ");
1282 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1283 if (abfd
->my_archive
!= (bfd
*) NULL
)
1285 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1290 fprintf (config
.map_file
, "%s", abfd
->filename
);
1292 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1295 /* Find all the symbols in this file defined in this section */
1297 if (in
->ifile
->symbol_count
)
1301 for (p
= in
->ifile
->asymbols
; *p
; p
++)
1305 if (bfd_get_section (q
) == i
1306 && (q
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
1319 print_dot
= outside_section_address (i
) + size
;
1323 fprintf (config
.map_file
, "No output section allocated\n");
1329 print_fill_statement (fill
)
1330 lang_fill_statement_type
* fill
;
1332 fprintf (config
.map_file
, "FILL mask ");
1333 print_fill (fill
->fill
);
1337 print_data_statement (data
)
1338 lang_data_statement_type
* data
;
1340 /* bfd_vma value; */
1345 /* ASSERT(print_dot == data->output_vma);*/
1347 print_address (data
->output_vma
+ data
->output_section
->vma
);
1349 print_address (data
->value
);
1354 fprintf (config
.map_file
, "BYTE ");
1355 print_dot
+= BYTE_SIZE
;
1358 fprintf (config
.map_file
, "SHORT ");
1359 print_dot
+= SHORT_SIZE
;
1362 fprintf (config
.map_file
, "LONG ");
1363 print_dot
+= LONG_SIZE
;
1366 fprintf (config
.map_file
, "QUAD ");
1367 print_dot
+= QUAD_SIZE
;
1371 exp_print_tree (data
->exp
);
1373 fprintf (config
.map_file
, "\n");
1378 print_padding_statement (s
)
1379 lang_padding_statement_type
* s
;
1383 print_section ("*fill*");
1385 print_address (s
->output_offset
+ s
->output_section
->vma
);
1387 print_size (s
->size
);
1389 print_fill (s
->fill
);
1392 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1397 print_wild_statement (w
, os
)
1398 lang_wild_statement_type
* w
;
1399 lang_output_section_statement_type
* os
;
1401 fprintf (config
.map_file
, " from ");
1402 if (w
->filename
!= (char *) NULL
)
1404 fprintf (config
.map_file
, "%s", w
->filename
);
1408 fprintf (config
.map_file
, "*");
1410 if (w
->section_name
!= (char *) NULL
)
1412 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1416 fprintf (config
.map_file
, "(*)");
1419 print_statement (w
->children
.head
, os
);
1423 print_statement (s
, os
)
1424 lang_statement_union_type
* s
;
1425 lang_output_section_statement_type
* os
;
1429 switch (s
->header
.type
)
1431 case lang_constructors_statement_enum
:
1432 fprintf (config
.map_file
, "constructors:\n");
1433 print_statement (constructor_list
.head
, os
);
1435 case lang_wild_statement_enum
:
1436 print_wild_statement (&s
->wild_statement
, os
);
1439 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1442 case lang_address_statement_enum
:
1443 fprintf (config
.map_file
, "address\n");
1445 case lang_object_symbols_statement_enum
:
1446 fprintf (config
.map_file
, "object symbols\n");
1448 case lang_fill_statement_enum
:
1449 print_fill_statement (&s
->fill_statement
);
1451 case lang_data_statement_enum
:
1452 print_data_statement (&s
->data_statement
);
1454 case lang_input_section_enum
:
1455 print_input_section (&s
->input_section
);
1457 case lang_padding_statement_enum
:
1458 print_padding_statement (&s
->padding_statement
);
1460 case lang_output_section_statement_enum
:
1461 print_output_section_statement (&s
->output_section_statement
);
1463 case lang_assignment_statement_enum
:
1464 print_assignment (&s
->assignment_statement
,
1467 case lang_target_statement_enum
:
1468 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1470 case lang_output_statement_enum
:
1471 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1472 s
->output_statement
.name
,
1473 output_target
? output_target
: "");
1475 case lang_input_statement_enum
:
1476 print_input_statement (&s
->input_statement
);
1478 case lang_afile_asection_pair_statement_enum
:
1490 print_statement (statement_list
.head
,
1491 abs_output_section
);
1496 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
1497 lang_statement_union_type
** this_ptr
;
1500 asection
* output_section_statement
;
1503 /* Align this section first to the
1504 input sections requirement, then
1505 to the output section's requirement.
1506 If this alignment is > than any seen before,
1507 then record it too. Perform the alignment by
1508 inserting a magic 'padding' statement.
1511 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1513 if (alignment_needed
!= 0)
1515 lang_statement_union_type
*new =
1516 (lang_statement_union_type
*)
1517 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1519 /* Link into existing chain */
1520 new->header
.next
= *this_ptr
;
1522 new->header
.type
= lang_padding_statement_enum
;
1523 new->padding_statement
.output_section
= output_section_statement
;
1524 new->padding_statement
.output_offset
=
1525 dot
- output_section_statement
->vma
;
1526 new->padding_statement
.fill
= fill
;
1527 new->padding_statement
.size
= alignment_needed
;
1531 /* Remember the most restrictive alignment */
1532 if (power
> output_section_statement
->alignment_power
)
1534 output_section_statement
->alignment_power
= power
;
1536 output_section_statement
->_raw_size
+= alignment_needed
;
1537 return alignment_needed
+ dot
;
1541 /* Work out how much this section will move the dot point */
1543 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
1544 lang_statement_union_type
** this_ptr
;
1545 lang_output_section_statement_type
* output_section_statement
;
1550 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1551 asection
*i
= is
->section
;
1553 if (is
->ifile
->just_syms_flag
== false)
1555 if (output_section_statement
->subsection_alignment
!= -1)
1556 i
->alignment_power
=
1557 output_section_statement
->subsection_alignment
;
1559 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1560 output_section_statement
->bfd_section
, dot
);
1562 /* remember the largest size so we can malloc the largest area
1563 needed for the output stage. Only remember the size of sections
1564 which we will actually allocate */
1565 if ((i
->flags
& SEC_HAS_CONTENTS
) != 0
1566 && (bfd_get_section_size_before_reloc (i
) > largest_section
))
1568 largest_section
= bfd_get_section_size_before_reloc (i
);
1571 /* Remember where in the output section this input section goes */
1573 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1575 /* Mark how big the output section must be to contain this now
1579 dot
+= i
->_cooked_size
;
1583 dot
+= i
->_raw_size
;
1585 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1589 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1595 /* Sizing happens in two passes, first pass we allocate worst case
1596 stuff. The second pass (if relaxing), we use what we learnt to
1597 change the size of some relocs from worst case to better
1599 static boolean had_relax
;
1602 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
1603 lang_statement_union_type
* s
;
1604 lang_output_section_statement_type
* output_section_statement
;
1605 lang_statement_union_type
** prev
;
1610 /* Size up the sections from their constituent parts */
1611 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1613 switch (s
->header
.type
)
1616 case lang_output_section_statement_enum
:
1619 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1621 /* If this is a shared library section, don't change the size
1623 if (os
->bfd_section
->flags
& SEC_SHARED_LIBRARY
)
1626 if (os
->bfd_section
== &bfd_abs_section
)
1628 /* No matter what happens, an abs section starts at zero */
1629 bfd_set_section_vma (0, os
->bfd_section
, 0);
1633 if (os
->addr_tree
== (etree_type
*) NULL
)
1635 /* No address specified for this section, get one
1636 from the region specification
1638 if (os
->region
== (lang_memory_region_type
*) NULL
)
1640 os
->region
= lang_memory_region_lookup ("*default*");
1642 dot
= os
->region
->current
;
1648 r
= exp_fold_tree (os
->addr_tree
,
1650 lang_allocating_phase_enum
,
1652 if (r
.valid
== false)
1654 einfo ("%F%S: non constant address expression for section %s\n",
1659 /* The section starts here */
1660 /* First, align to what the section needs */
1663 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1664 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1666 if (os
->load_base
) {
1667 os
->bfd_section
->lma
1668 = exp_get_value_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
1673 os
->bfd_section
->output_offset
= 0;
1675 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1676 os
->fill
, dot
, relax
);
1677 /* Ignore the size of the input sections, use the vma and size to */
1680 after
= ALIGN_N (os
->bfd_section
->vma
+
1681 os
->bfd_section
->_raw_size
,
1682 /* The coercion here is important, see ld.h. */
1683 (bfd_vma
) os
->block_value
);
1685 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1686 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1687 os
->processed
= true;
1689 /* Replace into region ? */
1690 if (os
->addr_tree
== (etree_type
*) NULL
1691 && os
->region
!= (lang_memory_region_type
*) NULL
)
1693 os
->region
->current
= dot
;
1694 /* Make sure this isn't silly */
1695 if (( os
->region
->current
1696 > os
->region
->origin
+ os
->region
->length
)
1697 || ( os
->region
->origin
> os
->region
->current
))
1699 einfo ("%X%P: region %s is full (%B section %s)\n",
1701 os
->bfd_section
->owner
,
1702 os
->bfd_section
->name
);
1703 /* Reset the region pointer */
1704 os
->region
->current
= 0;
1712 case lang_constructors_statement_enum
:
1713 dot
= lang_size_sections (constructor_list
.head
,
1714 output_section_statement
,
1715 &s
->wild_statement
.children
.head
,
1720 case lang_data_statement_enum
:
1722 unsigned int size
= 0;
1724 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1725 s
->data_statement
.output_section
=
1726 output_section_statement
->bfd_section
;
1728 switch (s
->data_statement
.type
)
1745 output_section_statement
->bfd_section
->_raw_size
+= size
;
1749 case lang_wild_statement_enum
:
1751 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
1752 output_section_statement
,
1753 &s
->wild_statement
.children
.head
,
1759 case lang_object_symbols_statement_enum
:
1760 link_info
.create_object_symbols_section
=
1761 output_section_statement
->bfd_section
;
1763 case lang_output_statement_enum
:
1764 case lang_target_statement_enum
:
1766 case lang_input_section_enum
:
1769 lang_input_section_type
*is
;
1774 is
= &(*prev
)->input_section
;
1777 if (bfd_relax_section (i
->owner
, i
, &link_info
, is
->ifile
->asymbols
))
1783 (*prev
)->input_section
.section
->_cooked_size
=
1784 (*prev
)->input_section
.section
->_raw_size
;
1787 dot
= size_input_section (prev
,
1788 output_section_statement
,
1789 output_section_statement
->fill
,
1792 case lang_input_statement_enum
:
1794 case lang_fill_statement_enum
:
1795 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
1797 fill
= s
->fill_statement
.fill
;
1799 case lang_assignment_statement_enum
:
1801 bfd_vma newdot
= dot
;
1803 exp_fold_tree (s
->assignment_statement
.exp
,
1804 output_section_statement
,
1805 lang_allocating_phase_enum
,
1809 if (newdot
!= dot
&& !relax
)
1810 /* We've been moved ! so insert a pad */
1812 lang_statement_union_type
*new =
1813 (lang_statement_union_type
*)
1814 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1816 /* Link into existing chain */
1817 new->header
.next
= *prev
;
1819 new->header
.type
= lang_padding_statement_enum
;
1820 new->padding_statement
.output_section
=
1821 output_section_statement
->bfd_section
;
1822 new->padding_statement
.output_offset
=
1823 dot
- output_section_statement
->bfd_section
->vma
;
1824 new->padding_statement
.fill
= fill
;
1825 new->padding_statement
.size
= newdot
- dot
;
1826 output_section_statement
->bfd_section
->_raw_size
+=
1827 new->padding_statement
.size
;
1836 /* This can only get here when relaxing is turned on */
1837 case lang_padding_statement_enum
:
1839 case lang_address_statement_enum
:
1842 prev
= &s
->header
.next
;
1848 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
1849 lang_statement_union_type
* s
;
1850 lang_output_section_statement_type
* output_section_statement
;
1854 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1856 switch (s
->header
.type
)
1858 case lang_constructors_statement_enum
:
1859 dot
= lang_do_assignments (constructor_list
.head
,
1860 output_section_statement
,
1865 case lang_output_section_statement_enum
:
1867 lang_output_section_statement_type
*os
=
1868 &(s
->output_section_statement
);
1870 dot
= os
->bfd_section
->vma
;
1871 (void) lang_do_assignments (os
->children
.head
, os
, os
->fill
, dot
);
1872 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1875 case lang_wild_statement_enum
:
1877 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
1878 output_section_statement
,
1883 case lang_object_symbols_statement_enum
:
1884 case lang_output_statement_enum
:
1885 case lang_target_statement_enum
:
1887 case lang_common_statement_enum
:
1890 case lang_data_statement_enum
:
1892 etree_value_type value
;
1894 value
= exp_fold_tree (s
->data_statement
.exp
,
1896 lang_final_phase_enum
, dot
, &dot
);
1897 s
->data_statement
.value
= value
.value
;
1898 if (value
.valid
== false)
1899 einfo ("%F%P: invalid data statement\n");
1901 switch (s
->data_statement
.type
)
1917 case lang_input_section_enum
:
1919 asection
*in
= s
->input_section
.section
;
1921 dot
+= bfd_get_section_size_before_reloc (in
);
1925 case lang_input_statement_enum
:
1927 case lang_fill_statement_enum
:
1928 fill
= s
->fill_statement
.fill
;
1930 case lang_assignment_statement_enum
:
1932 exp_fold_tree (s
->assignment_statement
.exp
,
1933 output_section_statement
,
1934 lang_final_phase_enum
,
1940 case lang_padding_statement_enum
:
1941 dot
+= s
->padding_statement
.size
;
1946 case lang_address_statement_enum
:
1957 struct bfd_link_hash_entry
*h
;
1958 boolean warn
= link_info
.relocateable
? false : true;
1960 if (entry_symbol
== (char *) NULL
)
1962 /* No entry has been specified. Look for start, but don't warn
1963 if we don't find it. */
1964 entry_symbol
= "start";
1968 h
= bfd_link_hash_lookup (link_info
.hash
, entry_symbol
, false, false, true);
1969 if (h
!= (struct bfd_link_hash_entry
*) NULL
1970 && h
->type
== bfd_link_hash_defined
)
1974 val
= (h
->u
.def
.value
1975 + bfd_get_section_vma (output_bfd
,
1976 h
->u
.def
.section
->output_section
)
1977 + h
->u
.def
.section
->output_offset
);
1978 if (! bfd_set_start_address (output_bfd
, val
))
1979 einfo ("%P%F:%s: can't set start address\n", entry_symbol
);
1985 /* Can't find the entry symbol. Use the first address in the
1987 ts
= bfd_get_section_by_name (output_bfd
, ".text");
1988 if (ts
!= (asection
*) NULL
)
1991 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
1992 entry_symbol
, bfd_get_section_vma (output_bfd
, ts
));
1993 if (! bfd_set_start_address (output_bfd
,
1994 bfd_get_section_vma (output_bfd
, ts
)))
1995 einfo ("%P%F: can't set start address\n");
2000 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2006 /* By now we know the target architecture, and we may have an */
2007 /* ldfile_output_machine_name */
2011 lang_statement_union_type
*file
;
2013 unsigned long input_machine
;
2014 enum bfd_architecture input_architecture
;
2015 CONST bfd_arch_info_type
*compatible
;
2017 for (file
= file_chain
.head
;
2018 file
!= (lang_statement_union_type
*) NULL
;
2019 file
= file
->input_statement
.next
)
2021 input_bfd
= file
->input_statement
.the_bfd
;
2023 input_machine
= bfd_get_mach (input_bfd
);
2024 input_architecture
= bfd_get_arch (input_bfd
);
2027 /* Inspect the architecture and ensure we're linking like with
2030 compatible
= bfd_arch_get_compatible (input_bfd
,
2035 ldfile_output_machine
= compatible
->mach
;
2036 ldfile_output_architecture
= compatible
->arch
;
2041 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2042 bfd_printable_name (input_bfd
), input_bfd
,
2043 bfd_printable_name (output_bfd
));
2045 if (! bfd_set_arch_mach (output_bfd
,
2048 einfo ("%P%F:%s: can't set architecture: %E\n",
2049 bfd_get_filename (output_bfd
));
2055 /* Look through all the global common symbols and attach them to the
2056 correct section. The -sort-common command line switch may be used
2057 to roughly sort the entries by size. */
2062 if (link_info
.relocateable
2063 && ! command_line
.force_common_definition
)
2066 if (! config
.sort_common
)
2067 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
, (PTR
) NULL
);
2072 for (power
= 1; power
<= 16; power
<<= 1)
2073 bfd_link_hash_traverse (link_info
.hash
, lang_one_common
,
2078 /* Place one common symbol in the correct section. */
2081 lang_one_common (h
, info
)
2082 struct bfd_link_hash_entry
*h
;
2085 unsigned int power_of_two
;
2090 if (h
->type
!= bfd_link_hash_common
)
2123 if (config
.sort_common
&& align
!= *(unsigned int *) info
)
2126 section
= h
->u
.c
.section
;
2128 /* Increase the size of the section. */
2129 section
->_raw_size
= ALIGN_N (section
->_raw_size
, align
);
2131 /* Adjust the alignment if necessary. */
2132 if (power_of_two
> section
->alignment_power
)
2133 section
->alignment_power
= power_of_two
;
2135 /* Change the symbol from common to defined. */
2136 h
->type
= bfd_link_hash_defined
;
2137 h
->u
.def
.section
= section
;
2138 h
->u
.def
.value
= section
->_raw_size
;
2140 /* Increase the size of the section. */
2141 section
->_raw_size
+= size
;
2143 if (write_map
&& config
.map_file
!= NULL
)
2144 fprintf (config
.map_file
, "Allocating common %s: %lx at %lx %s\n",
2145 h
->root
.string
, (unsigned long) size
,
2146 (unsigned long) h
->u
.def
.value
, section
->owner
->filename
);
2152 run through the input files and ensure that every input
2153 section has somewhere to go. If one is found without
2154 a destination then create an input request and place it
2155 into the statement tree.
2159 lang_place_orphans ()
2161 lang_input_statement_type
*file
;
2163 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2164 file
!= (lang_input_statement_type
*) NULL
;
2165 file
= (lang_input_statement_type
*) file
->next
)
2169 for (s
= file
->the_bfd
->sections
;
2170 s
!= (asection
*) NULL
;
2173 if (s
->output_section
== (asection
*) NULL
)
2175 /* This section of the file is not attatched, root
2176 around for a sensible place for it to go */
2178 if (file
->common_section
== s
)
2180 /* This is a lonely common section which must
2181 have come from an archive. We attatch to the
2182 section with the wildcard */
2183 if (! link_info
.relocateable
2184 && ! command_line
.force_common_definition
)
2186 if (default_common_section
==
2187 (lang_output_section_statement_type
*) NULL
)
2189 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2191 default_common_section
=
2192 lang_output_section_statement_lookup (".bss");
2195 wild_doit (&default_common_section
->children
, s
,
2196 default_common_section
, file
);
2201 lang_output_section_statement_type
*os
=
2202 lang_output_section_statement_lookup (s
->name
);
2204 wild_doit (&os
->children
, s
, os
, file
);
2213 lang_set_flags (ptr
, flags
)
2217 boolean state
= false;
2232 /* ptr->flag_read = state; */
2235 /* ptr->flag_write = state; */
2238 /* ptr->flag_executable= state;*/
2242 /* ptr->flag_loadable= state;*/
2245 einfo ("%P%F: invalid syntax in flags\n");
2255 lang_for_each_file (func
)
2256 void (*func
) PARAMS ((lang_input_statement_type
*));
2258 lang_input_statement_type
*f
;
2260 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2261 f
!= (lang_input_statement_type
*) NULL
;
2262 f
= (lang_input_statement_type
*) f
->next
)
2273 lang_for_each_input_section (func
)
2274 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
2276 lang_input_statement_type
*f
;
2278 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2279 f
!= (lang_input_statement_type
*) NULL
;
2280 f
= (lang_input_statement_type
*) f
->next
)
2284 for (s
= f
->the_bfd
->sections
;
2285 s
!= (asection
*) NULL
;
2288 func (f
->the_bfd
, s
);
2296 ldlang_add_file (entry
)
2297 lang_input_statement_type
* entry
;
2301 lang_statement_append (&file_chain
,
2302 (lang_statement_union_type
*) entry
,
2305 /* The BFD linker needs to have a list of all input BFDs involved in
2307 ASSERT (entry
->the_bfd
->link_next
== (bfd
*) NULL
);
2308 ASSERT (entry
->the_bfd
!= output_bfd
);
2309 for (pp
= &link_info
.input_bfds
;
2310 *pp
!= (bfd
*) NULL
;
2311 pp
= &(*pp
)->link_next
)
2313 *pp
= entry
->the_bfd
;
2314 entry
->the_bfd
->usrdata
= (PTR
) entry
;
2318 lang_add_output (name
, from_script
)
2322 /* Make -o on command line override OUTPUT in script. */
2323 if (had_output_filename
== false || !from_script
)
2325 output_filename
= name
;
2326 had_output_filename
= true;
2331 static lang_output_section_statement_type
*current_section
;
2333 static int topower(x
)
2338 if (x
< 0) return -1;
2339 for (l
= 0; l
< 32; l
++)
2341 if (i
>= x
) return l
;
2347 lang_enter_output_section_statement (output_section_statement_name
,
2348 address_exp
, flags
, block_value
,
2349 align
, subalign
, ebase
)
2350 const char *output_section_statement_name
;
2351 etree_type
* address_exp
;
2353 bfd_vma block_value
;
2355 etree_type
*subalign
;
2358 lang_output_section_statement_type
*os
;
2362 lang_output_section_statement_lookup (output_section_statement_name
);
2366 /* Add this statement to tree */
2367 /* add_statement(lang_output_section_statement_enum,
2368 output_section_statement);*/
2369 /* Make next things chain into subchain of this */
2371 if (os
->addr_tree
==
2372 (etree_type
*) NULL
)
2378 if (flags
& SEC_NEVER_LOAD
)
2382 os
->block_value
= block_value
? block_value
: 1;
2383 stat_ptr
= &os
->children
;
2385 os
->subsection_alignment
= topower(
2386 exp_get_value_int(subalign
, -1,
2387 "subsection alignment",
2389 os
->section_alignment
= topower(
2390 exp_get_value_int(align
, -1,
2391 "section alignment", 0));
2393 os
->load_base
= ebase
;
2400 lang_output_statement_type
*new =
2401 new_stat (lang_output_statement
, stat_ptr
);
2403 new->name
= output_filename
;
2406 /* Reset the current counters in the regions */
2408 reset_memory_regions ()
2410 lang_memory_region_type
*p
= lang_memory_region_list
;
2412 for (p
= lang_memory_region_list
;
2413 p
!= (lang_memory_region_type
*) NULL
;
2416 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
2417 p
->current
= p
->origin
;
2424 lang_reasonable_defaults ();
2425 current_target
= default_target
;
2427 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2428 /* For each output section statement, create a section in the output
2430 lang_create_output_section_statements ();
2432 ldemul_create_output_section_statements ();
2434 /* Add to the hash table all undefineds on the command line */
2435 lang_place_undefineds ();
2437 /* Create a bfd for each input file */
2438 current_target
= default_target
;
2439 lang_for_each_statement (open_input_bfds
);
2441 /* Build all sets based on the information gathered from the input
2443 ldctor_build_sets ();
2445 /* Run through the contours of the script and attatch input sections
2446 to the correct output sections
2448 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2449 (lang_output_section_statement_type
*) NULL
);
2452 /* Find any sections not attatched explicitly and handle them */
2453 lang_place_orphans ();
2455 /* Size up the common data */
2458 ldemul_before_allocation ();
2468 lang_size_sections (statement_list
.head
,
2469 (lang_output_section_statement_type
*) NULL
,
2470 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2471 /* FIXME. Until the code in relax is fixed so that it only reads in
2472 stuff once, we cant iterate since there is no way for the linker to
2473 know what has been patched and what hasn't */
2479 /* Now run around and relax if we can */
2480 if (command_line
.relax
)
2482 /* First time round is a trial run to get the 'worst case'
2483 addresses of the objects if there was no relaxing. */
2484 lang_size_sections (statement_list
.head
,
2485 (lang_output_section_statement_type
*) NULL
,
2486 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2489 reset_memory_regions ();
2491 /* Do all the assignments, now that we know the final resting
2492 places of all the symbols. */
2494 lang_do_assignments (statement_list
.head
,
2496 (fill_type
) 0, (bfd_vma
) 0);
2498 /* Perform another relax pass - this time we know where the
2499 globals are, so can make better guess. */
2500 lang_size_sections (statement_list
.head
,
2501 (lang_output_section_statement_type
*) NULL
,
2502 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2506 /* Size up the sections. */
2507 lang_size_sections (statement_list
.head
,
2509 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2512 /* See if anything special should be done now we know how big
2514 ldemul_after_allocation ();
2516 /* Do all the assignments, now that we know the final restingplaces
2517 of all the symbols */
2519 lang_do_assignments (statement_list
.head
,
2521 (fill_type
) 0, (bfd_vma
) 0);
2523 /* Make sure that we're not mixing architectures */
2532 /* DO NOT REENABLE THIS CALL. IF THIS CALL IS MADE, THE SUN4 LINKER
2533 CAN NOT BOOTSTRAP!! No, I don't know why, but don't change it
2534 unless you fix it. */
2535 /* Size up the sections. */
2536 lang_size_sections (statement_list
.head
,
2538 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2544 /* EXPORTED TO YACC */
2547 lang_add_wild (section_name
, filename
)
2548 CONST
char *CONST section_name
;
2549 CONST
char *CONST filename
;
2551 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
2554 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
2556 placed_commons
= true;
2558 if (filename
!= (char *) NULL
)
2560 lang_has_input_file
= true;
2562 new->section_name
= section_name
;
2563 new->filename
= filename
;
2564 lang_list_init (&new->children
);
2568 lang_section_start (name
, address
)
2570 etree_type
* address
;
2572 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
2574 ad
->section_name
= name
;
2575 ad
->address
= address
;
2579 lang_add_entry (name
)
2582 entry_symbol
= name
;
2586 lang_add_target (name
)
2589 lang_target_statement_type
*new = new_stat (lang_target_statement
,
2605 map_option_f
= true;
2616 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
2623 lang_add_data (type
, exp
)
2625 union etree_union
*exp
;
2628 lang_data_statement_type
*new = new_stat (lang_data_statement
,
2637 lang_add_assignment (exp
)
2640 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
2647 lang_add_attribute (attribute
)
2648 enum statement_enum attribute
;
2650 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
2657 if (startup_file
!= (char *) NULL
)
2659 einfo ("%P%Fmultiple STARTUP files\n");
2661 first_file
->filename
= name
;
2662 first_file
->local_sym_name
= name
;
2664 startup_file
= name
;
2671 lang_float_flag
= maybe
;
2675 lang_leave_output_section_statement (fill
, memspec
)
2677 CONST
char *memspec
;
2679 current_section
->fill
= fill
;
2680 current_section
->region
= lang_memory_region_lookup (memspec
);
2681 stat_ptr
= &statement_list
;
2683 /* We remember if we are closing a .data section, since we use it to
2684 store constructors in */
2685 if (strcmp (current_section
->name
, ".data") == 0)
2687 end_of_data_section_statement_list
= statement_list
;
2693 Create an absolute symbol with the given name with the value of the
2694 address of first byte of the section named.
2696 If the symbol already exists, then do nothing.
2699 lang_abs_symbol_at_beginning_of (secname
, name
)
2700 const char *secname
;
2703 struct bfd_link_hash_entry
*h
;
2705 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
2706 if (h
== (struct bfd_link_hash_entry
*) NULL
)
2707 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2709 if (h
->type
== bfd_link_hash_new
2710 || h
->type
== bfd_link_hash_undefined
)
2714 h
->type
= bfd_link_hash_defined
;
2716 sec
= bfd_get_section_by_name (output_bfd
, secname
);
2717 if (sec
== (asection
*) NULL
)
2720 h
->u
.def
.value
= bfd_get_section_vma (output_bfd
, sec
);
2722 h
->u
.def
.section
= &bfd_abs_section
;
2727 Create an absolute symbol with the given name with the value of the
2728 address of the first byte after the end of the section named.
2730 If the symbol already exists, then do nothing.
2733 lang_abs_symbol_at_end_of (secname
, name
)
2734 const char *secname
;
2737 struct bfd_link_hash_entry
*h
;
2739 h
= bfd_link_hash_lookup (link_info
.hash
, name
, true, true, true);
2740 if (h
== (struct bfd_link_hash_entry
*) NULL
)
2741 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2743 if (h
->type
== bfd_link_hash_new
2744 || h
->type
== bfd_link_hash_undefined
)
2748 h
->type
= bfd_link_hash_defined
;
2750 sec
= bfd_get_section_by_name (output_bfd
, secname
);
2751 if (sec
== (asection
*) NULL
)
2754 h
->u
.def
.value
= (bfd_get_section_vma (output_bfd
, sec
)
2755 + bfd_section_size (output_bfd
, sec
));
2757 h
->u
.def
.section
= &bfd_abs_section
;
2762 lang_statement_append (list
, element
, field
)
2763 lang_statement_list_type
* list
;
2764 lang_statement_union_type
* element
;
2765 lang_statement_union_type
** field
;
2767 *(list
->tail
) = element
;
2771 /* Set the output format type. -oformat overrides scripts. */
2773 lang_add_output_format (format
, from_script
)
2777 if (output_target
== NULL
|| !from_script
)
2778 output_target
= format
;