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. */
39 static void print_statements
PARAMS ((void));
40 static void print_statement
PARAMS ((lang_statement_union_type
*,
41 lang_output_section_statement_type
*));
42 static lang_statement_union_type
*new_statement
PARAMS ((enum statement_enum
,
44 lang_statement_list_type
*));
48 static struct obstack stat_obstack
;
50 #define obstack_chunk_alloc ldmalloc
51 #define obstack_chunk_free free
52 static CONST
char *startup_file
;
53 static lang_statement_list_type input_file_chain
;
55 /* Points to the last statement in the .data section, so we can add
56 stuff to the data section without pain */
57 static lang_statement_list_type end_of_data_section_statement_list
;
59 static boolean placed_commons
= false;
60 static lang_output_section_statement_type
*default_common_section
;
61 static boolean map_option_f
;
62 static bfd_vma print_dot
;
63 static lang_input_statement_type
*first_file
;
64 static lang_statement_list_type lang_output_section_statement
;
65 static CONST
char *current_target
;
66 static CONST
char *output_target
;
67 static int longest_section_name
= 8;
68 static lang_statement_list_type statement_list
;
70 static void print_size
PARAMS ((size_t value
));
71 static void print_alignment
PARAMS ((unsigned int value
));
72 static void print_fill
PARAMS ((fill_type value
));
73 static void print_section
PARAMS ((const char *name
));
74 static void lang_for_each_statement_worker
75 PARAMS ((void (*func
) (lang_statement_union_type
*),
76 lang_statement_union_type
*s
));
77 static lang_input_statement_type
*new_afile
78 PARAMS ((const char *name
, lang_input_file_enum_type file_type
,
80 static void print_flags
PARAMS ((int *ignore_flags
));
81 static void init_os
PARAMS ((lang_output_section_statement_type
*s
));
82 static void wild_doit
PARAMS ((lang_statement_list_type
*ptr
,
84 lang_output_section_statement_type
*output
,
85 lang_input_statement_type
*file
));
86 static asection
*our_bfd_get_section_by_name
PARAMS ((bfd
*abfd
,
87 const char *section
));
88 static void wild_section
PARAMS ((lang_wild_statement_type
*ptr
,
90 lang_input_statement_type
*file
,
91 lang_output_section_statement_type
*output
));
92 static lang_input_statement_type
*lookup_name
PARAMS ((const char *name
));
93 static void wild
PARAMS ((lang_wild_statement_type
*s
,
94 const char *section
, const char *file
,
96 lang_output_section_statement_type
*output
));
97 static bfd
*open_output
PARAMS ((const char *name
));
98 static void ldlang_open_output
PARAMS ((lang_statement_union_type
*statement
));
99 static void open_input_bfds
PARAMS ((lang_statement_union_type
*statement
));
100 static void lang_reasonable_defaults
PARAMS ((void));
101 static void lang_place_undefineds
PARAMS ((void));
102 static void lang_create_output_section_statements
PARAMS ((void));
103 static void lang_init_script_file
PARAMS ((void));
104 static void map_input_to_output_sections
105 PARAMS ((lang_statement_union_type
*s
,
107 lang_output_section_statement_type
*output_section_statement
));
108 static void print_output_section_statement
109 PARAMS ((lang_output_section_statement_type
*output_section_statement
));
110 static void print_assignment
111 PARAMS ((lang_assignment_statement_type
*assignment
,
112 lang_output_section_statement_type
*output_section
));
113 static void print_input_statement
PARAMS ((lang_input_statement_type
*statm
));
114 static void print_symbol
PARAMS ((asymbol
*q
));
115 static void print_input_section
PARAMS ((lang_input_section_type
*in
));
116 static void print_fill_statement
PARAMS ((lang_fill_statement_type
*fill
));
117 static void print_data_statement
PARAMS ((lang_data_statement_type
*data
));
118 static void print_padding_statement
PARAMS ((lang_padding_statement_type
*s
));
119 static void print_wild_statement
120 PARAMS ((lang_wild_statement_type
*w
,
121 lang_output_section_statement_type
*os
));
122 static void print_statement
PARAMS ((lang_statement_union_type
*s
,
123 lang_output_section_statement_type
*os
));
124 static void print_statements
PARAMS ((void));
125 static bfd_vma insert_pad
PARAMS ((lang_statement_union_type
**this_ptr
,
126 fill_type fill
, unsigned int power
,
127 asection
*output_section_statement
,
129 static bfd_vma size_input_section
130 PARAMS ((lang_statement_union_type
**this_ptr
,
131 lang_output_section_statement_type
*output_section_statement
,
132 unsigned short fill
, bfd_vma dot
, boolean relax
));
133 static bfd_vma lang_size_sections
134 PARAMS ((lang_statement_union_type
*s
,
135 lang_output_section_statement_type
*output_section_statement
,
136 lang_statement_union_type
**prev
, unsigned short fill
,
137 bfd_vma dot
, boolean relax
));
138 static bfd_vma lang_do_assignments
139 PARAMS ((lang_statement_union_type
* s
,
140 lang_output_section_statement_type
*output_section_statement
,
143 static void lang_relocate_globals
PARAMS ((void));
144 static void lang_finish
PARAMS ((void));
145 static void lang_check
PARAMS ((void));
146 static void lang_common
PARAMS ((void));
147 static void lang_place_orphans
PARAMS ((void));
148 static int topower
PARAMS ((int));
149 static void reset_memory_regions
PARAMS ((void));
153 lang_output_section_statement_type
*abs_output_section
;
154 lang_statement_list_type
*stat_ptr
= &statement_list
;
155 lang_input_statement_type
*script_file
= 0;
156 lang_statement_list_type file_chain
=
158 CONST
char *entry_symbol
= 0;
159 bfd_size_type largest_section
= 0;
160 boolean lang_has_input_file
= false;
161 lang_output_section_statement_type
*create_object_symbols
= 0;
162 boolean had_output_filename
= false;
163 boolean lang_float_flag
= false;
164 boolean delete_output_file_on_failure
= false;
166 etree_type
*base
; /* Relocation base - or null */
170 #define cat(a,b) a##b
172 #define cat(a,b) a/**/b
175 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
177 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
179 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
185 return obstack_alloc (&stat_obstack
, size
);
192 fprintf (config
.map_file
, "%5x", (unsigned) value
);
196 print_alignment (value
)
199 fprintf (config
.map_file
, "2**%1u", value
);
206 fprintf (config
.map_file
, "%04x", (unsigned) value
);
213 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
216 /*----------------------------------------------------------------------
217 lang_for_each_statement walks the parse tree and calls the provided
218 function for each node
222 lang_for_each_statement_worker (func
, s
)
223 void (*func
) (lang_statement_union_type
*);
224 lang_statement_union_type
*s
;
226 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
230 switch (s
->header
.type
)
232 case lang_constructors_statement_enum
:
233 lang_for_each_statement_worker (func
, constructor_list
.head
);
235 case lang_output_section_statement_enum
:
236 lang_for_each_statement_worker
238 s
->output_section_statement
.children
.head
);
240 case lang_wild_statement_enum
:
241 lang_for_each_statement_worker
243 s
->wild_statement
.children
.head
);
245 case lang_data_statement_enum
:
246 case lang_object_symbols_statement_enum
:
247 case lang_output_statement_enum
:
248 case lang_target_statement_enum
:
249 case lang_input_section_enum
:
250 case lang_input_statement_enum
:
251 case lang_assignment_statement_enum
:
252 case lang_padding_statement_enum
:
253 case lang_address_statement_enum
:
263 lang_for_each_statement (func
)
264 void (*func
) (lang_statement_union_type
*);
266 lang_for_each_statement_worker (func
,
267 statement_list
.head
);
270 /*----------------------------------------------------------------------*/
272 lang_list_init (list
)
273 lang_statement_list_type
*list
;
275 list
->head
= (lang_statement_union_type
*) NULL
;
276 list
->tail
= &list
->head
;
279 /*----------------------------------------------------------------------
281 build a new statement node for the parse tree
286 lang_statement_union_type
*
287 new_statement (type
, size
, list
)
288 enum statement_enum type
;
290 lang_statement_list_type
* list
;
292 lang_statement_union_type
*new = (lang_statement_union_type
*)
295 new->header
.type
= type
;
296 new->header
.next
= (lang_statement_union_type
*) NULL
;
297 lang_statement_append (list
, new, &new->header
.next
);
302 Build a new input file node for the language. There are several ways
303 in which we treat an input file, eg, we only look at symbols, or
304 prefix it with a -l etc.
306 We can be supplied with requests for input files more than once;
307 they may, for example be split over serveral lines like foo.o(.text)
308 foo.o(.data) etc, so when asked for a file we check that we havn't
309 got it already so we don't duplicate the bfd.
312 static lang_input_statement_type
*
313 new_afile (name
, file_type
, target
)
315 lang_input_file_enum_type file_type
;
319 lang_input_statement_type
*p
= new_stat (lang_input_statement
,
322 lang_has_input_file
= true;
324 p
->complained
= false;
327 case lang_input_file_is_symbols_only_enum
:
329 p
->is_archive
= false;
331 p
->local_sym_name
= name
;
332 p
->just_syms_flag
= true;
333 p
->search_dirs_flag
= false;
335 case lang_input_file_is_fake_enum
:
337 p
->is_archive
= false;
339 p
->local_sym_name
= name
;
340 p
->just_syms_flag
= false;
341 p
->search_dirs_flag
= false;
343 case lang_input_file_is_l_enum
:
344 p
->is_archive
= true;
347 p
->local_sym_name
= concat ("-l", name
, "");
348 p
->just_syms_flag
= false;
349 p
->search_dirs_flag
= true;
351 case lang_input_file_is_search_file_enum
:
352 case lang_input_file_is_marker_enum
:
354 p
->is_archive
= false;
356 p
->local_sym_name
= name
;
357 p
->just_syms_flag
= false;
358 p
->search_dirs_flag
= true;
360 case lang_input_file_is_file_enum
:
362 p
->is_archive
= false;
364 p
->local_sym_name
= name
;
365 p
->just_syms_flag
= false;
366 p
->search_dirs_flag
= false;
371 p
->asymbols
= (asymbol
**) NULL
;
372 p
->superfile
= (lang_input_statement_type
*) NULL
;
373 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
374 p
->next
= (lang_statement_union_type
*) NULL
;
376 p
->common_output_section
= (asection
*) NULL
;
377 lang_statement_append (&input_file_chain
,
378 (lang_statement_union_type
*) p
,
383 lang_input_statement_type
*
384 lang_add_input_file (name
, file_type
, target
)
386 lang_input_file_enum_type file_type
;
389 /* Look it up or build a new one */
390 lang_has_input_file
= true;
393 lang_input_statement_type
*p
;
395 for (p
= (lang_input_statement_type
*) input_file_chain
.head
;
396 p
!= (lang_input_statement_type
*) NULL
;
397 p
= (lang_input_statement_type
*) (p
->next_real_file
))
399 /* Sometimes we have incomplete entries in here */
400 if (p
->filename
!= (char *) NULL
)
402 if (strcmp (name
, p
->filename
) == 0)
408 return new_afile (name
, file_type
, target
);
412 lang_add_keepsyms_file (filename
)
413 CONST
char *filename
;
415 if (keepsyms_file
!= 0)
416 info_msg ("%X%P: error: duplicated keep-symbols-file value\n");
417 keepsyms_file
= filename
;
418 if (strip_symbols
!= STRIP_NONE
)
419 info_msg ("%P: `-keep-only-symbols-file' overrides `-s' and `-S'\n");
420 strip_symbols
= STRIP_SOME
;
423 /* Build enough state so that the parser can build its tree */
427 obstack_begin (&stat_obstack
, 1000);
429 stat_ptr
= &statement_list
;
431 lang_list_init (stat_ptr
);
433 lang_list_init (&input_file_chain
);
434 lang_list_init (&lang_output_section_statement
);
435 lang_list_init (&file_chain
);
436 first_file
= lang_add_input_file ((char *) NULL
,
437 lang_input_file_is_marker_enum
,
439 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
441 abs_output_section
->bfd_section
= &bfd_abs_section
;
445 /*----------------------------------------------------------------------
446 A region is an area of memory declared with the
447 MEMORY { name:org=exp, len=exp ... }
450 We maintain a list of all the regions here
452 If no regions are specified in the script, then the default is used
453 which is created when looked up to be the entire data space
456 static lang_memory_region_type
*lang_memory_region_list
;
457 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
459 lang_memory_region_type
*
460 lang_memory_region_lookup (name
)
461 CONST
char *CONST name
;
464 lang_memory_region_type
*p
= lang_memory_region_list
;
466 for (p
= lang_memory_region_list
;
467 p
!= (lang_memory_region_type
*) NULL
;
470 if (strcmp (p
->name
, name
) == 0)
475 if (strcmp (name
, "*default*") == 0)
477 /* This is the default region, dig out first one on the list */
478 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
480 return lang_memory_region_list
;
484 lang_memory_region_type
*new =
485 (lang_memory_region_type
*) stat_alloc ((bfd_size_type
) (sizeof (lang_memory_region_type
)));
487 new->name
= buystring (name
);
488 new->next
= (lang_memory_region_type
*) NULL
;
490 *lang_memory_region_list_tail
= new;
491 lang_memory_region_list_tail
= &new->next
;
493 new->length
= ~(bfd_size_type
)0;
495 new->had_full_message
= false;
502 lang_output_section_statement_type
*
503 lang_output_section_find (name
)
504 CONST
char *CONST name
;
506 lang_statement_union_type
*u
;
507 lang_output_section_statement_type
*lookup
;
509 for (u
= lang_output_section_statement
.head
;
510 u
!= (lang_statement_union_type
*) NULL
;
513 lookup
= &u
->output_section_statement
;
514 if (strcmp (name
, lookup
->name
) == 0)
519 return (lang_output_section_statement_type
*) NULL
;
522 lang_output_section_statement_type
*
523 lang_output_section_statement_lookup (name
)
524 CONST
char *CONST name
;
526 lang_output_section_statement_type
*lookup
;
528 lookup
= lang_output_section_find (name
);
529 if (lookup
== (lang_output_section_statement_type
*) NULL
)
532 lookup
= (lang_output_section_statement_type
*)
533 new_stat (lang_output_section_statement
, stat_ptr
);
534 lookup
->region
= (lang_memory_region_type
*) NULL
;
536 lookup
->block_value
= 1;
539 lookup
->next
= (lang_statement_union_type
*) NULL
;
540 lookup
->bfd_section
= (asection
*) NULL
;
541 lookup
->processed
= false;
542 lookup
->loadable
= 1;
543 lookup
->addr_tree
= (etree_type
*) NULL
;
544 lang_list_init (&lookup
->children
);
546 lookup
->memspec
= (CONST
char *) NULL
;
548 lookup
->subsection_alignment
= -1;
549 lookup
->section_alignment
= -1;
550 lookup
->load_base
= (union etree_union
*) NULL
;
552 lang_statement_append (&lang_output_section_statement
,
553 (lang_statement_union_type
*) lookup
,
561 print_flags (ignore_flags
)
564 fprintf (config
.map_file
, "(");
566 if (flags
->flag_read
)
567 fprintf (outfile
, "R");
568 if (flags
->flag_write
)
569 fprintf (outfile
, "W");
570 if (flags
->flag_executable
)
571 fprintf (outfile
, "X");
572 if (flags
->flag_loadable
)
573 fprintf (outfile
, "L");
575 fprintf (config
.map_file
, ")");
581 lang_memory_region_type
*m
;
583 fprintf (config
.map_file
, "**MEMORY CONFIGURATION**\n\n");
585 fprintf (config
.map_file
, "name\t\torigin\t\tlength\t\tattributes\n");
587 fprintf (config
.map_file
,
588 "name\t\torigin length r_size c_size is attributes\n");
591 for (m
= lang_memory_region_list
;
592 m
!= (lang_memory_region_type
*) NULL
;
595 fprintf (config
.map_file
, "%-16s", m
->name
);
596 print_address (m
->origin
);
598 print_address ((bfd_vma
)m
->length
);
600 print_address ((bfd_vma
)m
->old_length
);
602 print_address (m
->current
- m
->origin
);
605 fprintf (config
.map_file
, " %2d%% ",
606 (int) ((m
->current
- m
->origin
) * 100 / m
->old_length
));
607 print_flags (&m
->flags
);
608 fprintf (config
.map_file
, "\n");
610 fprintf (config
.map_file
, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
611 fprintf (config
.map_file
, "output input virtual\n");
612 fprintf (config
.map_file
, "section section address tsize\n\n");
623 lang_output_section_statement_type
* s
;
625 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
626 section_userdata_type
*new =
627 (section_userdata_type
*)
628 stat_alloc ((bfd_size_type
) (sizeof (section_userdata_type
)));
630 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
631 if (s
->bfd_section
== (asection
*) NULL
)
632 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
633 if (s
->bfd_section
== (asection
*) NULL
)
635 einfo ("%P%F: output format %s cannot represent section called %s\n",
636 output_bfd
->xvec
->name
, s
->name
);
638 s
->bfd_section
->output_section
= s
->bfd_section
;
639 /* s->bfd_section->flags = s->flags;*/
641 /* We initialize an output sections output offset to minus its own */
642 /* vma to allow us to output a section through itself */
643 s
->bfd_section
->output_offset
= 0;
644 get_userdata (s
->bfd_section
) = (PTR
) new;
648 /***********************************************************************
651 These expand statements like *(.text) and foo.o to a list of
652 explicit actions, like foo.o(.text), bar.o(.text) and
655 The toplevel routine, wild, takes a statement, section, file and
656 target. If either the section or file is null it is taken to be the
657 wildcard. Seperate lang_input_section statements are created for
658 each part of the expanstion, and placed after the statement provided.
663 wild_doit (ptr
, section
, output
, file
)
664 lang_statement_list_type
* ptr
;
666 lang_output_section_statement_type
* output
;
667 lang_input_statement_type
* file
;
669 if (output
->bfd_section
== (asection
*) NULL
)
672 /* Initialize the vma and size to the existing section. This will
673 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
675 if (section
!= (asection
*) NULL
)
677 bfd_set_section_vma (0, output
->bfd_section
,
678 bfd_section_vma (0, section
));
679 output
->bfd_section
->_raw_size
= section
->_raw_size
;
683 if (section
!= (asection
*) NULL
684 && section
->output_section
== (asection
*) NULL
)
686 /* Add a section reference to the list */
687 lang_input_section_type
*new = new_stat (lang_input_section
, ptr
);
689 new->section
= section
;
691 section
->output_section
= output
->bfd_section
;
693 /* Be selective about what the output section inherits from the
696 if ((section
->flags
& SEC_SHARED_LIBRARY
) != 0)
697 section
->output_section
->flags
|= section
->flags
;
699 section
->output_section
->flags
|=
700 section
->flags
& (flagword
) (~ SEC_NEVER_LOAD
);
702 if (!output
->loadable
)
704 /* Turn off load flag */
705 output
->bfd_section
->flags
&= ~SEC_LOAD
;
706 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
708 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
710 output
->bfd_section
->alignment_power
= section
->alignment_power
;
712 /* If supplied an aligmnet, then force it */
713 if (output
->section_alignment
!= -1)
715 output
->bfd_section
->alignment_power
= output
->section_alignment
;
721 our_bfd_get_section_by_name (abfd
, section
)
725 return bfd_get_section_by_name (abfd
, section
);
729 wild_section (ptr
, section
, file
, output
)
730 lang_wild_statement_type
* ptr
;
732 lang_input_statement_type
* file
;
733 lang_output_section_statement_type
* output
;
737 if (file
->just_syms_flag
== false)
739 if (section
== (char *) NULL
)
741 /* Do the creation to all sections in the file */
742 for (s
= file
->the_bfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
745 if ((s
->flags
& SEC_IS_COMMON
) == 0)
747 wild_doit (&ptr
->children
, s
, output
, file
);
753 /* Do the creation to the named section only */
754 wild_doit (&ptr
->children
,
755 our_bfd_get_section_by_name (file
->the_bfd
, section
),
761 /* passed a file name (which must have been seen already and added to
762 the statement tree. We will see if it has been opened already and
763 had its symbols read. If not then we'll read it.
765 Archives are pecuilar here. We may open them once, but if they do
766 not define anything we need at the time, they won't have all their
767 symbols read. If we need them later, we'll have to redo it.
770 lang_input_statement_type
*
774 lang_input_statement_type
*search
;
776 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
777 search
!= (lang_input_statement_type
*) NULL
;
778 search
= (lang_input_statement_type
*) search
->next_real_file
)
780 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
784 if (search
->filename
!= (char *) NULL
&& name
!= (char *) NULL
)
786 if (strcmp (search
->filename
, name
) == 0)
788 ldmain_open_file_read_symbol (search
);
794 /* There isn't an afile entry for this file yet, this must be
795 because the name has only appeared inside a load script and not
796 on the command line */
797 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
);
798 ldmain_open_file_read_symbol (search
);
805 wild (s
, section
, file
, target
, output
)
806 lang_wild_statement_type
* s
;
810 lang_output_section_statement_type
* output
;
812 lang_input_statement_type
*f
;
814 if (file
== (char *) NULL
)
816 /* Perform the iteration over all files in the list */
817 for (f
= (lang_input_statement_type
*) file_chain
.head
;
818 f
!= (lang_input_statement_type
*) NULL
;
819 f
= (lang_input_statement_type
*) f
->next
)
821 wild_section (s
, section
, f
, output
);
823 /* Once more for the script file */
824 wild_section(s
, section
, script_file
, output
);
828 /* Perform the iteration over a single file */
829 wild_section (s
, section
, lookup_name (file
), output
);
831 if (section
!= (char *) NULL
832 && strcmp (section
, "COMMON") == 0
833 && default_common_section
== (lang_output_section_statement_type
*) NULL
)
835 /* Remember the section that common is going to incase we later
836 get something which doesn't know where to put it */
837 default_common_section
= output
;
842 read in all the files
851 if (output_target
== (char *) NULL
)
853 if (current_target
!= (char *) NULL
)
854 output_target
= current_target
;
856 output_target
= default_target
;
858 output
= bfd_openw (name
, output_target
);
860 if (output
== (bfd
*) NULL
)
862 if (bfd_error
== invalid_target
)
864 einfo ("%P%F: target %s not found\n", output_target
);
866 einfo ("%P%F: cannot open output file %s: %E\n", name
);
869 delete_output_file_on_failure
= 1;
871 /* output->flags |= D_PAGED;*/
873 if (! bfd_set_format (output
, bfd_object
))
874 einfo ("%P%F:%s: can not make object file: %E\n", name
);
875 if (! bfd_set_arch_mach (output
,
876 ldfile_output_architecture
,
877 ldfile_output_machine
))
878 einfo ("%P%F:%s: can not set architecture: %E\n", name
);
880 bfd_set_gp_size (output
, g_switch_value
);
888 ldlang_open_output (statement
)
889 lang_statement_union_type
* statement
;
891 switch (statement
->header
.type
)
893 case lang_output_statement_enum
:
894 output_bfd
= open_output (statement
->output_statement
.name
);
895 ldemul_set_output_arch ();
896 if (config
.magic_demand_paged
&& !config
.relocateable_output
)
897 output_bfd
->flags
|= D_PAGED
;
899 output_bfd
->flags
&= ~D_PAGED
;
900 if (config
.text_read_only
)
901 output_bfd
->flags
|= WP_TEXT
;
903 output_bfd
->flags
&= ~WP_TEXT
;
906 case lang_target_statement_enum
:
907 current_target
= statement
->target_statement
.target
;
915 open_input_bfds (statement
)
916 lang_statement_union_type
* statement
;
918 switch (statement
->header
.type
)
920 case lang_target_statement_enum
:
921 current_target
= statement
->target_statement
.target
;
923 case lang_wild_statement_enum
:
924 /* Maybe we should load the file's symbols */
925 if (statement
->wild_statement
.filename
)
927 (void) lookup_name (statement
->wild_statement
.filename
);
930 case lang_input_statement_enum
:
931 if (statement
->input_statement
.real
== true)
933 statement
->input_statement
.target
= current_target
;
934 lookup_name (statement
->input_statement
.filename
);
942 /* If there are [COMMONS] statements, put a wild one into the bss section */
945 lang_reasonable_defaults ()
948 lang_output_section_statement_lookup (".text");
949 lang_output_section_statement_lookup (".data");
951 default_common_section
=
952 lang_output_section_statement_lookup (".bss");
955 if (placed_commons
== false)
957 lang_wild_statement_type
*new =
958 new_stat (lang_wild_statement
,
959 &default_common_section
->children
);
961 new->section_name
= "COMMON";
962 new->filename
= (char *) NULL
;
963 lang_list_init (&new->children
);
970 Add the supplied name to the symbol table as an undefined reference.
971 Remove items from the chain as we open input bfds
973 typedef struct ldlang_undef_chain_list
975 struct ldlang_undef_chain_list
*next
;
977 } ldlang_undef_chain_list_type
;
979 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
982 ldlang_add_undef (name
)
983 CONST
char *CONST name
;
985 ldlang_undef_chain_list_type
*new =
986 (ldlang_undef_chain_list_type
987 *) stat_alloc ((bfd_size_type
) (sizeof (ldlang_undef_chain_list_type
)));
989 new->next
= ldlang_undef_chain_list_head
;
990 ldlang_undef_chain_list_head
= new;
992 new->name
= buystring (name
);
995 /* Run through the list of undefineds created above and place them
996 into the linker hash table as undefined symbols belonging to the
1000 lang_place_undefineds ()
1002 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
1004 while (ptr
!= (ldlang_undef_chain_list_type
*) NULL
)
1007 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
1009 def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
1011 def
->name
= ptr
->name
;
1012 def
->section
= &bfd_und_section
;
1013 enter_global_ref (def_ptr
, ptr
->name
);
1018 /* Copy important data from out internal form to the bfd way. Also
1019 create a section for the dummy file
1023 lang_create_output_section_statements ()
1025 lang_statement_union_type
*os
;
1027 for (os
= lang_output_section_statement
.head
;
1028 os
!= (lang_statement_union_type
*) NULL
;
1029 os
= os
->output_section_statement
.next
)
1031 lang_output_section_statement_type
*s
=
1032 &os
->output_section_statement
;
1040 lang_init_script_file ()
1042 script_file
= lang_add_input_file ("command line",
1043 lang_input_file_is_fake_enum
,
1045 script_file
->the_bfd
= bfd_create ("command line", output_bfd
);
1046 script_file
->symbol_count
= 0;
1047 script_file
->the_bfd
->sections
= 0;
1049 /* The user data of a bfd points to the input statement attatched */
1050 script_file
->the_bfd
->usrdata
= (void *)script_file
;
1051 script_file
->common_section
=
1052 bfd_make_section(script_file
->the_bfd
,"COMMON");
1054 abs_output_section
=
1055 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
1057 abs_output_section
->bfd_section
= &bfd_abs_section
;
1061 /* Open input files and attatch to output sections */
1063 map_input_to_output_sections (s
, target
, output_section_statement
)
1064 lang_statement_union_type
* s
;
1066 lang_output_section_statement_type
* output_section_statement
;
1068 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1070 switch (s
->header
.type
)
1074 case lang_wild_statement_enum
:
1075 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1076 s
->wild_statement
.filename
, target
,
1077 output_section_statement
);
1080 case lang_constructors_statement_enum
:
1081 map_input_to_output_sections (constructor_list
.head
,
1083 output_section_statement
);
1085 case lang_output_section_statement_enum
:
1086 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1088 &s
->output_section_statement
);
1090 case lang_output_statement_enum
:
1092 case lang_target_statement_enum
:
1093 target
= s
->target_statement
.target
;
1095 case lang_fill_statement_enum
:
1096 case lang_input_section_enum
:
1097 case lang_object_symbols_statement_enum
:
1098 case lang_data_statement_enum
:
1099 case lang_assignment_statement_enum
:
1100 case lang_padding_statement_enum
:
1102 case lang_afile_asection_pair_statement_enum
:
1105 case lang_address_statement_enum
:
1106 /* Mark the specified section with the supplied address */
1108 lang_output_section_statement_type
*os
=
1109 lang_output_section_statement_lookup
1110 (s
->address_statement
.section_name
);
1112 os
->addr_tree
= s
->address_statement
.address
;
1113 if (os
->bfd_section
== (asection
*) NULL
)
1115 einfo ("%P%F: cannot set the address of undefined section %s\n",
1116 s
->address_statement
.section_name
);
1120 case lang_input_statement_enum
:
1121 /* A standard input statement, has no wildcards */
1122 /* ldmain_open_file_read_symbol(&s->input_statement);*/
1129 print_output_section_statement (output_section_statement
)
1130 lang_output_section_statement_type
* output_section_statement
;
1132 asection
*section
= output_section_statement
->bfd_section
;
1135 print_section (output_section_statement
->name
);
1140 print_dot
= section
->vma
;
1144 print_address (section
->vma
);
1146 print_size (section
->_raw_size
);
1148 print_size(section
->_cooked_size
);
1150 print_alignment (section
->alignment_power
);
1153 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1154 print_flags (stdout
, &output_section_statement
->flags
);
1156 if (section
->flags
& SEC_LOAD
)
1157 fprintf (config
.map_file
, "load ");
1158 if (section
->flags
& SEC_ALLOC
)
1159 fprintf (config
.map_file
, "alloc ");
1160 if (section
->flags
& SEC_RELOC
)
1161 fprintf (config
.map_file
, "reloc ");
1162 if (section
->flags
& SEC_HAS_CONTENTS
)
1163 fprintf (config
.map_file
, "contents ");
1168 fprintf (config
.map_file
, "No attached output section");
1171 if (output_section_statement
->load_base
)
1173 int b
= exp_get_value_int(output_section_statement
->load_base
,
1174 0, "output base", lang_final_phase_enum
);
1175 printf("Output address %08x\n", b
);
1177 if (output_section_statement
->section_alignment
>= 0
1178 || output_section_statement
->section_alignment
>= 0)
1180 printf("\t\t\t\t\tforced alignment ");
1181 if ( output_section_statement
->section_alignment
>= 0)
1183 printf("section 2**%d ",output_section_statement
->section_alignment
);
1185 if ( output_section_statement
->subsection_alignment
>= 0)
1187 printf("subsection 2**%d ",output_section_statement
->subsection_alignment
);
1192 print_statement (output_section_statement
->children
.head
,
1193 output_section_statement
);
1198 print_assignment (assignment
, output_section
)
1199 lang_assignment_statement_type
* assignment
;
1200 lang_output_section_statement_type
* output_section
;
1202 etree_value_type result
;
1208 print_address (print_dot
);
1210 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1212 lang_final_phase_enum
,
1218 print_address (result
.value
);
1222 fprintf (config
.map_file
, "*undefined*");
1225 exp_print_tree (assignment
->exp
);
1227 fprintf (config
.map_file
, "\n");
1231 print_input_statement (statm
)
1232 lang_input_statement_type
* statm
;
1234 if (statm
->filename
!= (char *) NULL
)
1236 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1245 fprintf (config
.map_file
, " ");
1247 fprintf (config
.map_file
, " ");
1248 print_address (outside_symbol_address (q
));
1249 fprintf (config
.map_file
, " %s", q
->name
? q
->name
: " ");
1250 if (q
->flags
& BSF_WEAK
)
1251 fprintf (config
.map_file
, " *weak*");
1256 print_input_section (in
)
1257 lang_input_section_type
* in
;
1259 asection
*i
= in
->section
;
1260 int size
= i
->reloc_done
?
1261 bfd_get_section_size_after_reloc (i
) :
1262 bfd_get_section_size_before_reloc (i
);
1267 fprintf (config
.map_file
, " ");
1268 print_section (i
->name
);
1269 fprintf (config
.map_file
, " ");
1270 if (i
->output_section
)
1272 print_address (i
->output_section
->vma
+ i
->output_offset
);
1273 fprintf (config
.map_file
, " ");
1274 print_size (i
->_raw_size
);
1275 fprintf (config
.map_file
, " ");
1276 print_size(i
->_cooked_size
);
1277 fprintf (config
.map_file
, " ");
1278 print_alignment (i
->alignment_power
);
1279 fprintf (config
.map_file
, " ");
1283 bfd
*abfd
= in
->ifile
->the_bfd
;
1285 if (in
->ifile
->just_syms_flag
== true)
1287 fprintf (config
.map_file
, "symbols only ");
1290 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1291 if (abfd
->my_archive
!= (bfd
*) NULL
)
1293 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1298 fprintf (config
.map_file
, "%s", abfd
->filename
);
1300 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1303 /* Find all the symbols in this file defined in this section */
1305 if (in
->ifile
->symbol_count
)
1309 for (p
= in
->ifile
->asymbols
; *p
; p
++)
1313 if (bfd_get_section (q
) == i
1314 && (q
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
1327 print_dot
= outside_section_address (i
) + size
;
1331 fprintf (config
.map_file
, "No output section allocated\n");
1337 print_fill_statement (fill
)
1338 lang_fill_statement_type
* fill
;
1340 fprintf (config
.map_file
, "FILL mask ");
1341 print_fill (fill
->fill
);
1345 print_data_statement (data
)
1346 lang_data_statement_type
* data
;
1348 /* bfd_vma value; */
1353 /* ASSERT(print_dot == data->output_vma);*/
1355 print_address (data
->output_vma
+ data
->output_section
->vma
);
1357 print_address (data
->value
);
1362 fprintf (config
.map_file
, "BYTE ");
1363 print_dot
+= BYTE_SIZE
;
1366 fprintf (config
.map_file
, "SHORT ");
1367 print_dot
+= SHORT_SIZE
;
1370 fprintf (config
.map_file
, "LONG ");
1371 print_dot
+= LONG_SIZE
;
1375 exp_print_tree (data
->exp
);
1377 fprintf (config
.map_file
, "\n");
1382 print_padding_statement (s
)
1383 lang_padding_statement_type
* s
;
1387 print_section ("*fill*");
1389 print_address (s
->output_offset
+ s
->output_section
->vma
);
1391 print_size (s
->size
);
1393 print_fill (s
->fill
);
1396 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1401 print_wild_statement (w
, os
)
1402 lang_wild_statement_type
* w
;
1403 lang_output_section_statement_type
* os
;
1405 fprintf (config
.map_file
, " from ");
1406 if (w
->filename
!= (char *) NULL
)
1408 fprintf (config
.map_file
, "%s", w
->filename
);
1412 fprintf (config
.map_file
, "*");
1414 if (w
->section_name
!= (char *) NULL
)
1416 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1420 fprintf (config
.map_file
, "(*)");
1423 print_statement (w
->children
.head
, os
);
1427 print_statement (s
, os
)
1428 lang_statement_union_type
* s
;
1429 lang_output_section_statement_type
* os
;
1433 switch (s
->header
.type
)
1435 case lang_constructors_statement_enum
:
1436 fprintf (config
.map_file
, "constructors:\n");
1437 print_statement (constructor_list
.head
, os
);
1439 case lang_wild_statement_enum
:
1440 print_wild_statement (&s
->wild_statement
, os
);
1443 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1446 case lang_address_statement_enum
:
1447 fprintf (config
.map_file
, "address\n");
1449 case lang_object_symbols_statement_enum
:
1450 fprintf (config
.map_file
, "object symbols\n");
1452 case lang_fill_statement_enum
:
1453 print_fill_statement (&s
->fill_statement
);
1455 case lang_data_statement_enum
:
1456 print_data_statement (&s
->data_statement
);
1458 case lang_input_section_enum
:
1459 print_input_section (&s
->input_section
);
1461 case lang_padding_statement_enum
:
1462 print_padding_statement (&s
->padding_statement
);
1464 case lang_output_section_statement_enum
:
1465 print_output_section_statement (&s
->output_section_statement
);
1467 case lang_assignment_statement_enum
:
1468 print_assignment (&s
->assignment_statement
,
1471 case lang_target_statement_enum
:
1472 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1474 case lang_output_statement_enum
:
1475 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1476 s
->output_statement
.name
,
1477 output_target
? output_target
: "");
1479 case lang_input_statement_enum
:
1480 print_input_statement (&s
->input_statement
);
1482 case lang_afile_asection_pair_statement_enum
:
1494 print_statement (statement_list
.head
,
1495 abs_output_section
);
1500 insert_pad (this_ptr
, fill
, power
, output_section_statement
, dot
)
1501 lang_statement_union_type
** this_ptr
;
1504 asection
* output_section_statement
;
1507 /* Align this section first to the
1508 input sections requirement, then
1509 to the output section's requirement.
1510 If this alignment is > than any seen before,
1511 then record it too. Perform the alignment by
1512 inserting a magic 'padding' statement.
1515 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1517 if (alignment_needed
!= 0)
1519 lang_statement_union_type
*new =
1520 (lang_statement_union_type
*)
1521 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1523 /* Link into existing chain */
1524 new->header
.next
= *this_ptr
;
1526 new->header
.type
= lang_padding_statement_enum
;
1527 new->padding_statement
.output_section
= output_section_statement
;
1528 new->padding_statement
.output_offset
=
1529 dot
- output_section_statement
->vma
;
1530 new->padding_statement
.fill
= fill
;
1531 new->padding_statement
.size
= alignment_needed
;
1535 /* Remember the most restrictive alignment */
1536 if (power
> output_section_statement
->alignment_power
)
1538 output_section_statement
->alignment_power
= power
;
1540 output_section_statement
->_raw_size
+= alignment_needed
;
1541 return alignment_needed
+ dot
;
1545 /* Work out how much this section will move the dot point */
1547 size_input_section (this_ptr
, output_section_statement
, fill
, dot
, relax
)
1548 lang_statement_union_type
** this_ptr
;
1549 lang_output_section_statement_type
* output_section_statement
;
1550 unsigned short fill
;
1554 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1555 asection
*i
= is
->section
;
1557 if (is
->ifile
->just_syms_flag
== false)
1559 if (output_section_statement
->subsection_alignment
!= -1)
1560 i
->alignment_power
=
1561 output_section_statement
->subsection_alignment
;
1563 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1564 output_section_statement
->bfd_section
, dot
);
1566 /* remember the largest size so we can malloc the largest area
1567 needed for the output stage. Only remember the size of sections
1568 which we will actually allocate */
1569 if ((i
->flags
& SEC_HAS_CONTENTS
) != 0
1570 && (bfd_get_section_size_before_reloc (i
) > largest_section
))
1572 largest_section
= bfd_get_section_size_before_reloc (i
);
1575 /* Remember where in the output section this input section goes */
1577 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1579 /* Mark how big the output section must be to contain this now
1583 dot
+= i
->_cooked_size
;
1587 dot
+= i
->_raw_size
;
1589 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1593 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1599 /* Sizing happens in two passes, first pass we allocate worst case
1600 stuff. The second pass (if relaxing), we use what we learnt to
1601 change the size of some relocs from worst case to better
1603 static boolean had_relax
;
1606 lang_size_sections (s
, output_section_statement
, prev
, fill
, dot
, relax
)
1607 lang_statement_union_type
* s
;
1608 lang_output_section_statement_type
* output_section_statement
;
1609 lang_statement_union_type
** prev
;
1610 unsigned short fill
;
1614 /* Size up the sections from their constituent parts */
1615 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1617 switch (s
->header
.type
)
1620 case lang_output_section_statement_enum
:
1623 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1625 /* If this is a shared library section, don't change the size
1627 if (os
->bfd_section
->flags
& SEC_SHARED_LIBRARY
)
1630 if (os
->bfd_section
== &bfd_abs_section
)
1632 /* No matter what happens, an abs section starts at zero */
1633 bfd_set_section_vma (0, os
->bfd_section
, 0);
1637 if (os
->addr_tree
== (etree_type
*) NULL
)
1639 /* No address specified for this section, get one
1640 from the region specification
1642 if (os
->region
== (lang_memory_region_type
*) NULL
)
1644 os
->region
= lang_memory_region_lookup ("*default*");
1646 dot
= os
->region
->current
;
1652 r
= exp_fold_tree (os
->addr_tree
,
1654 lang_allocating_phase_enum
,
1656 if (r
.valid
== false)
1658 einfo ("%F%S: non constant address expression for section %s\n",
1663 /* The section starts here */
1664 /* First, align to what the section needs */
1667 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1668 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1670 if (os
->load_base
) {
1671 os
->bfd_section
->lma
1672 = exp_get_value_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
1677 os
->bfd_section
->output_offset
= 0;
1679 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1680 os
->fill
, dot
, relax
);
1681 /* Ignore the size of the input sections, use the vma and size to */
1684 after
= ALIGN_N (os
->bfd_section
->vma
+
1685 os
->bfd_section
->_raw_size
,
1686 /* The coercion here is important, see ld.h. */
1687 (bfd_vma
) os
->block_value
);
1689 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1690 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1691 os
->processed
= true;
1693 /* Replace into region ? */
1694 if (os
->addr_tree
== (etree_type
*) NULL
1695 && os
->region
!= (lang_memory_region_type
*) NULL
)
1697 os
->region
->current
= dot
;
1698 /* Make sure this isn't silly */
1699 if (( os
->region
->current
1700 > os
->region
->origin
+ os
->region
->length
)
1701 || ( os
->region
->origin
> os
->region
->current
))
1703 einfo ("%X%P: region %s is full (%B section %s)\n",
1705 os
->bfd_section
->owner
,
1706 os
->bfd_section
->name
);
1707 /* Reset the region pointer */
1708 os
->region
->current
= 0;
1716 case lang_constructors_statement_enum
:
1717 dot
= lang_size_sections (constructor_list
.head
,
1718 output_section_statement
,
1719 &s
->wild_statement
.children
.head
,
1724 case lang_data_statement_enum
:
1726 unsigned int size
= 0;
1728 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1729 s
->data_statement
.output_section
=
1730 output_section_statement
->bfd_section
;
1732 switch (s
->data_statement
.type
)
1746 output_section_statement
->bfd_section
->_raw_size
+= size
;
1750 case lang_wild_statement_enum
:
1752 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
1753 output_section_statement
,
1754 &s
->wild_statement
.children
.head
,
1760 case lang_object_symbols_statement_enum
:
1761 create_object_symbols
= output_section_statement
;
1763 case lang_output_statement_enum
:
1764 case lang_target_statement_enum
:
1766 case lang_input_section_enum
:
1771 if( relax_section (prev
))
1777 (*prev
)->input_section
.section
->_cooked_size
=
1778 (*prev
)->input_section
.section
->_raw_size
;
1781 dot
= size_input_section (prev
,
1782 output_section_statement
,
1783 output_section_statement
->fill
,
1786 case lang_input_statement_enum
:
1788 case lang_fill_statement_enum
:
1789 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
1791 fill
= s
->fill_statement
.fill
;
1793 case lang_assignment_statement_enum
:
1795 bfd_vma newdot
= dot
;
1797 exp_fold_tree (s
->assignment_statement
.exp
,
1798 output_section_statement
,
1799 lang_allocating_phase_enum
,
1803 if (newdot
!= dot
&& !relax
)
1804 /* We've been moved ! so insert a pad */
1806 lang_statement_union_type
*new =
1807 (lang_statement_union_type
*)
1808 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1810 /* Link into existing chain */
1811 new->header
.next
= *prev
;
1813 new->header
.type
= lang_padding_statement_enum
;
1814 new->padding_statement
.output_section
=
1815 output_section_statement
->bfd_section
;
1816 new->padding_statement
.output_offset
=
1817 dot
- output_section_statement
->bfd_section
->vma
;
1818 new->padding_statement
.fill
= fill
;
1819 new->padding_statement
.size
= newdot
- dot
;
1820 output_section_statement
->bfd_section
->_raw_size
+=
1821 new->padding_statement
.size
;
1830 /* This can only get here when relaxing is turned on */
1831 case lang_padding_statement_enum
:
1833 case lang_address_statement_enum
:
1836 prev
= &s
->header
.next
;
1842 lang_do_assignments (s
, output_section_statement
, fill
, dot
)
1843 lang_statement_union_type
* s
;
1844 lang_output_section_statement_type
* output_section_statement
;
1845 unsigned short fill
;
1848 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1850 switch (s
->header
.type
)
1852 case lang_constructors_statement_enum
:
1853 dot
= lang_do_assignments (constructor_list
.head
,
1854 output_section_statement
,
1859 case lang_output_section_statement_enum
:
1861 lang_output_section_statement_type
*os
=
1862 &(s
->output_section_statement
);
1864 dot
= os
->bfd_section
->vma
;
1865 (void) lang_do_assignments (os
->children
.head
, os
, os
->fill
, dot
);
1866 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1869 case lang_wild_statement_enum
:
1871 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
1872 output_section_statement
,
1877 case lang_object_symbols_statement_enum
:
1878 case lang_output_statement_enum
:
1879 case lang_target_statement_enum
:
1881 case lang_common_statement_enum
:
1884 case lang_data_statement_enum
:
1886 etree_value_type value
;
1888 value
= exp_fold_tree (s
->data_statement
.exp
,
1890 lang_final_phase_enum
, dot
, &dot
);
1891 s
->data_statement
.value
= value
.value
;
1892 if (value
.valid
== false)
1893 einfo ("%F%P: invalid data statement\n");
1895 switch (s
->data_statement
.type
)
1908 case lang_input_section_enum
:
1910 asection
*in
= s
->input_section
.section
;
1912 dot
+= bfd_get_section_size_before_reloc (in
);
1916 case lang_input_statement_enum
:
1918 case lang_fill_statement_enum
:
1919 fill
= s
->fill_statement
.fill
;
1921 case lang_assignment_statement_enum
:
1923 exp_fold_tree (s
->assignment_statement
.exp
,
1924 output_section_statement
,
1925 lang_final_phase_enum
,
1931 case lang_padding_statement_enum
:
1932 dot
+= s
->padding_statement
.size
;
1937 case lang_address_statement_enum
:
1948 lang_relocate_globals ()
1951 Each ldsym_type maintains a chain of pointers to asymbols which
1952 references the definition. Replace each pointer to the referenence
1953 with a pointer to only one place, preferably the definition. If
1954 the defintion isn't available then the common symbol, and if
1955 there isn't one of them then choose one reference.
1958 FOR_EACH_LDSYM (lgs
)
1962 /* Skip indirect symbols. */
1963 if (lgs
->flags
& SYM_INDIRECT
)
1966 if (lgs
->sdefs_chain
)
1968 it
= *(lgs
->sdefs_chain
);
1970 else if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
1972 it
= *(lgs
->scoms_chain
);
1974 else if (lgs
->srefs_chain
!= (asymbol
**) NULL
)
1976 it
= *(lgs
->srefs_chain
);
1980 /* This can happen when the command line asked for a symbol to
1982 it
= (asymbol
*) NULL
;
1984 if (it
!= (asymbol
*) NULL
)
1987 asymbol
**ptr
= lgs
->srefs_chain
;;
1988 if (lgs
->flags
& SYM_WARNING
)
1990 produce_warnings (lgs
, it
);
1993 while (ptr
!= (asymbol
**) NULL
1996 asymbol
*ref
= *ptr
;
1999 ptr
= (asymbol
**) (ref
->udata
);
2011 int warn
= config
.relocateable_output
!= true;
2012 if (entry_symbol
== (char *) NULL
)
2014 /* No entry has been specified, look for start, but don't warn */
2015 entry_symbol
= "start";
2018 lgs
= ldsym_get_soft (entry_symbol
);
2019 if (lgs
&& lgs
->sdefs_chain
)
2021 asymbol
*sy
= *(lgs
->sdefs_chain
);
2023 /* We can set the entry address*/
2024 bfd_set_start_address (output_bfd
,
2025 outside_symbol_address (sy
));
2030 /* Cannot find anything reasonable,
2031 use the first address in the text section
2033 asection
*ts
= bfd_get_section_by_name (output_bfd
, ".text");
2037 einfo ("%P: warning: cannot find entry symbol %s, defaulting to %V\n",
2038 entry_symbol
, ts
->vma
);
2040 bfd_set_start_address (output_bfd
, ts
->vma
);
2045 einfo ("%P: warning: cannot find entry symbol %s, not setting start address\n",
2051 /* By now we know the target architecture, and we may have an */
2052 /* ldfile_output_machine_name */
2056 lang_statement_union_type
*file
;
2058 unsigned long input_machine
;
2059 enum bfd_architecture input_architecture
;
2060 CONST bfd_arch_info_type
*compatible
;
2062 for (file
= file_chain
.head
;
2063 file
!= (lang_statement_union_type
*) NULL
;
2064 file
= file
->input_statement
.next
)
2066 input_bfd
= file
->input_statement
.the_bfd
;
2068 input_machine
= bfd_get_mach (input_bfd
);
2069 input_architecture
= bfd_get_arch (input_bfd
);
2072 /* Inspect the architecture and ensure we're linking like with
2075 compatible
= bfd_arch_get_compatible (input_bfd
,
2080 ldfile_output_machine
= compatible
->mach
;
2081 ldfile_output_architecture
= compatible
->arch
;
2086 info_msg ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2087 bfd_printable_name (input_bfd
), input_bfd
,
2088 bfd_printable_name (output_bfd
));
2090 if (! bfd_set_arch_mach (output_bfd
,
2093 einfo ("%P%F:%s: can't set architecture: %E\n",
2094 bfd_get_filename (output_bfd
));
2101 * run through all the global common symbols and tie them
2102 * to the output section requested.
2104 As an experiment we do this 4 times, once for all the byte sizes,
2105 then all the two bytes, all the four bytes and then everything else
2114 if (config
.relocateable_output
== false ||
2115 command_line
.force_common_definition
== true)
2117 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<= 1)
2119 for (lgs
= symbol_head
;
2120 lgs
!= (ldsym_type
*) NULL
;
2124 unsigned int power_of_two
;
2128 if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
2130 com
= *(lgs
->scoms_chain
);
2160 if (config
.sort_common
== false || align
== power
)
2164 /* Change from a common symbol into a definition of
2166 lgs
->sdefs_chain
= lgs
->scoms_chain
;
2167 lgs
->scoms_chain
= (asymbol
**) NULL
;
2170 /* Point to the correct common section */
2171 symbfd
= bfd_asymbol_bfd (com
);
2172 if (com
->section
== &bfd_com_section
)
2174 ((lang_input_statement_type
*) symbfd
->usrdata
)
2181 name
= bfd_get_section_name (symbfd
,
2183 newsec
= bfd_get_section_by_name (symbfd
,
2185 /* This section should have been created by
2186 enter_file_symbols if it did not already
2188 if (newsec
== (asection
*) NULL
)
2189 einfo ("%P%F: no output section %s\n", name
);
2190 com
->section
= newsec
;
2193 /* Fix the size of the common section */
2195 com
->section
->_raw_size
=
2196 ALIGN_N (com
->section
->_raw_size
,
2197 /* The coercion here is important, see ld.h. */
2200 /* Remember if this is the biggest alignment ever seen */
2201 if (power_of_two
> com
->section
->alignment_power
)
2203 com
->section
->alignment_power
= power_of_two
;
2206 /* Symbol stops being common and starts being global, but
2207 we remember that it was common once. */
2209 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
2210 com
->value
= com
->section
->_raw_size
;
2212 if (write_map
&& config
.map_file
)
2214 fprintf (config
.map_file
, "Allocating common %s: %x at %x %s\n",
2217 (unsigned) com
->value
,
2218 bfd_asymbol_bfd(com
)->filename
);
2221 com
->section
->_raw_size
+= size
;
2234 run through the input files and ensure that every input
2235 section has somewhere to go. If one is found without
2236 a destination then create an input request and place it
2237 into the statement tree.
2241 lang_place_orphans ()
2243 lang_input_statement_type
*file
;
2245 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2246 file
!= (lang_input_statement_type
*) NULL
;
2247 file
= (lang_input_statement_type
*) file
->next
)
2251 for (s
= file
->the_bfd
->sections
;
2252 s
!= (asection
*) NULL
;
2255 if (s
->output_section
== (asection
*) NULL
)
2257 /* This section of the file is not attatched, root
2258 around for a sensible place for it to go */
2260 if (file
->common_section
== s
)
2262 /* This is a lonely common section which must
2263 have come from an archive. We attatch to the
2264 section with the wildcard */
2265 if (config
.relocateable_output
!= true
2266 && command_line
.force_common_definition
== false)
2268 if (default_common_section
==
2269 (lang_output_section_statement_type
*) NULL
)
2271 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2273 default_common_section
=
2274 lang_output_section_statement_lookup (".bss");
2277 wild_doit (&default_common_section
->children
, s
,
2278 default_common_section
, file
);
2283 lang_output_section_statement_type
*os
=
2284 lang_output_section_statement_lookup (s
->name
);
2286 wild_doit (&os
->children
, s
, os
, file
);
2295 lang_set_flags (ptr
, flags
)
2299 boolean state
= false;
2314 /* ptr->flag_read = state; */
2317 /* ptr->flag_write = state; */
2320 /* ptr->flag_executable= state;*/
2324 /* ptr->flag_loadable= state;*/
2327 einfo ("%P%F: invalid syntax in flags\n");
2337 lang_for_each_file (func
)
2338 void (*func
) PARAMS ((lang_input_statement_type
*));
2340 lang_input_statement_type
*f
;
2342 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2343 f
!= (lang_input_statement_type
*) NULL
;
2344 f
= (lang_input_statement_type
*) f
->next
)
2355 lang_for_each_input_section (func
)
2356 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
2358 lang_input_statement_type
*f
;
2360 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2361 f
!= (lang_input_statement_type
*) NULL
;
2362 f
= (lang_input_statement_type
*) f
->next
)
2366 for (s
= f
->the_bfd
->sections
;
2367 s
!= (asection
*) NULL
;
2370 func (f
->the_bfd
, s
);
2378 ldlang_add_file (entry
)
2379 lang_input_statement_type
* entry
;
2382 lang_statement_append (&file_chain
,
2383 (lang_statement_union_type
*) entry
,
2388 lang_add_output (name
, from_script
)
2392 /* Make -o on command line override OUTPUT in script. */
2393 if (had_output_filename
== false || !from_script
)
2395 output_filename
= name
;
2396 had_output_filename
= true;
2401 static lang_output_section_statement_type
*current_section
;
2403 static int topower(x
)
2408 if (x
< 0) return -1;
2409 for (l
= 0; l
< 32; l
++)
2411 if (i
>= x
) return l
;
2417 lang_enter_output_section_statement (output_section_statement_name
,
2418 address_exp
, flags
, block_value
,
2419 align
, subalign
, ebase
)
2420 const char *output_section_statement_name
;
2421 etree_type
* address_exp
;
2423 bfd_vma block_value
;
2425 etree_type
*subalign
;
2428 lang_output_section_statement_type
*os
;
2432 lang_output_section_statement_lookup (output_section_statement_name
);
2436 /* Add this statement to tree */
2437 /* add_statement(lang_output_section_statement_enum,
2438 output_section_statement);*/
2439 /* Make next things chain into subchain of this */
2441 if (os
->addr_tree
==
2442 (etree_type
*) NULL
)
2448 if (flags
& SEC_NEVER_LOAD
)
2452 os
->block_value
= block_value
? block_value
: 1;
2453 stat_ptr
= &os
->children
;
2455 os
->subsection_alignment
= topower(
2456 exp_get_value_int(subalign
, -1,
2457 "subsection alignment",
2459 os
->section_alignment
= topower(
2460 exp_get_value_int(align
, -1,
2461 "section alignment", 0));
2463 os
->load_base
= ebase
;
2470 lang_output_statement_type
*new =
2471 new_stat (lang_output_statement
, stat_ptr
);
2473 new->name
= output_filename
;
2476 /* Reset the current counters in the regions */
2478 reset_memory_regions ()
2480 lang_memory_region_type
*p
= lang_memory_region_list
;
2482 for (p
= lang_memory_region_list
;
2483 p
!= (lang_memory_region_type
*) NULL
;
2486 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
2487 p
->current
= p
->origin
;
2494 create_symbol (name
, flags
, section
)
2499 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
2501 /* Add this definition to script file */
2502 asymbol
*def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
2503 def
->name
= buystring (name
);
2506 def
->section
= section
;
2508 enter_global_ref (def_ptr
, name
);
2515 lang_reasonable_defaults ();
2516 current_target
= default_target
;
2518 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2519 /* For each output section statement, create a section in the output
2521 lang_create_output_section_statements ();
2523 ldemul_create_output_section_statements ();
2525 /* Create a dummy bfd for the script */
2526 lang_init_script_file ();
2528 /* Add to the hash table all undefineds on the command line */
2529 lang_place_undefineds ();
2531 /* Create a bfd for each input file */
2532 current_target
= default_target
;
2533 lang_for_each_statement (open_input_bfds
);
2535 /* Run through the contours of the script and attatch input sections
2536 to the correct output sections
2538 find_constructors ();
2539 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2540 (lang_output_section_statement_type
*) NULL
);
2543 /* Find any sections not attatched explicitly and handle them */
2544 lang_place_orphans ();
2546 /* Size up the common data */
2549 ldemul_before_allocation ();
2559 lang_size_sections (statement_list
.head
,
2560 (lang_output_section_statement_type
*) NULL
,
2561 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2562 /* FIXME. Until the code in relax is fixed so that it only reads in
2563 stuff once, we cant iterate since there is no way for the linker to
2564 know what has been patched and what hasn't */
2570 /* Now run around and relax if we can */
2571 if (command_line
.relax
)
2573 /* First time round is a trial run to get the 'worst case'
2574 addresses of the objects if there was no relaxing. */
2575 lang_size_sections (statement_list
.head
,
2576 (lang_output_section_statement_type
*) NULL
,
2577 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2579 /* Move the global symbols around so the second pass of relaxing
2581 lang_relocate_globals ();
2583 reset_memory_regions ();
2585 /* Do all the assignments, now that we know the final resting
2586 places of all the symbols. */
2588 lang_do_assignments (statement_list
.head
,
2592 /* Perform another relax pass - this time we know where the
2593 globals are, so can make better guess. */
2594 lang_size_sections (statement_list
.head
,
2595 (lang_output_section_statement_type
*) NULL
,
2596 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2600 /* Size up the sections. */
2601 lang_size_sections (statement_list
.head
,
2603 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2606 /* See if anything special should be done now we know how big
2608 ldemul_after_allocation ();
2610 /* Do all the assignments, now that we know the final restingplaces
2611 of all the symbols */
2613 lang_do_assignments (statement_list
.head
,
2618 /* Move the global symbols around */
2619 lang_relocate_globals ();
2621 /* Make sure that we're not mixing architectures */
2630 /* DO NOT REENABLE THIS CALL. IF THIS CALL IS MADE, THE SUN4 LINKER
2631 CAN NOT BOOTSTRAP!! No, I don't know why, but don't change it
2632 unless you fix it. */
2633 /* Size up the sections. */
2634 lang_size_sections (statement_list
.head
,
2636 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2642 /* EXPORTED TO YACC */
2645 lang_add_wild (section_name
, filename
)
2646 CONST
char *CONST section_name
;
2647 CONST
char *CONST filename
;
2649 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
2652 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
2654 placed_commons
= true;
2656 if (filename
!= (char *) NULL
)
2658 lang_has_input_file
= true;
2660 new->section_name
= section_name
;
2661 new->filename
= filename
;
2662 lang_list_init (&new->children
);
2666 lang_section_start (name
, address
)
2668 etree_type
* address
;
2670 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
2672 ad
->section_name
= name
;
2673 ad
->address
= address
;
2677 lang_add_entry (name
)
2680 entry_symbol
= name
;
2684 lang_add_target (name
)
2687 lang_target_statement_type
*new = new_stat (lang_target_statement
,
2703 map_option_f
= true;
2714 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
2721 lang_add_data (type
, exp
)
2723 union etree_union
*exp
;
2726 lang_data_statement_type
*new = new_stat (lang_data_statement
,
2735 lang_add_assignment (exp
)
2738 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
2745 lang_add_attribute (attribute
)
2746 enum statement_enum attribute
;
2748 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
2755 if (startup_file
!= (char *) NULL
)
2757 einfo ("%P%Fmultiple STARTUP files\n");
2759 first_file
->filename
= name
;
2760 first_file
->local_sym_name
= name
;
2762 startup_file
= name
;
2769 lang_float_flag
= maybe
;
2773 lang_leave_output_section_statement (fill
, memspec
)
2775 CONST
char *memspec
;
2777 current_section
->fill
= fill
;
2778 current_section
->region
= lang_memory_region_lookup (memspec
);
2779 stat_ptr
= &statement_list
;
2781 /* We remember if we are closing a .data section, since we use it to
2782 store constructors in */
2783 if (strcmp (current_section
->name
, ".data") == 0)
2785 end_of_data_section_statement_list
= statement_list
;
2791 Create an absolute symbol with the given name with the value of the
2792 address of first byte of the section named.
2794 If the symbol already exists, then do nothing.
2797 lang_abs_symbol_at_beginning_of (section
, name
)
2798 CONST
char *section
;
2801 if (ldsym_undefined (name
))
2803 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2804 asymbol
*def
= create_symbol (name
,
2805 BSF_GLOBAL
| BSF_EXPORT
,
2808 if (s
!= (asection
*) NULL
)
2810 def
->value
= s
->vma
;
2820 Create an absolute symbol with the given name with the value of the
2821 address of the first byte after the end of the section named.
2823 If the symbol already exists, then do nothing.
2826 lang_abs_symbol_at_end_of (section
, name
)
2827 CONST
char *section
;
2830 if (ldsym_undefined (name
))
2832 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2834 /* Add a symbol called _end */
2835 asymbol
*def
= create_symbol (name
,
2836 BSF_GLOBAL
| BSF_EXPORT
,
2839 if (s
!= (asection
*) NULL
)
2841 def
->value
= s
->vma
+ s
->_raw_size
;
2851 lang_statement_append (list
, element
, field
)
2852 lang_statement_list_type
* list
;
2853 lang_statement_union_type
* element
;
2854 lang_statement_union_type
** field
;
2856 *(list
->tail
) = element
;
2860 /* Set the output format type. -oformat overrides scripts. */
2862 lang_add_output_format (format
, from_script
)
2866 if (output_target
== NULL
|| !from_script
)
2867 output_target
= format
;