1 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
35 PROTO (static void, print_statements
, (void));
36 PROTO (static void, print_statement
, (lang_statement_union_type
*,
37 lang_output_section_statement_type
*));
40 static struct obstack stat_obstack
;
42 #define obstack_chunk_alloc ldmalloc
43 #define obstack_chunk_free free
44 static CONST
char *startup_file
;
45 static lang_statement_list_type input_file_chain
;
47 /* Points to the last statement in the .data section, so we can add
48 stuff to the data section without pain */
49 static lang_statement_list_type end_of_data_section_statement_list
;
51 /* List of statements needed to handle constructors */
52 extern lang_statement_list_type constructor_list
;
54 static boolean placed_commons
= false;
55 static lang_output_section_statement_type
*default_common_section
;
56 static boolean map_option_f
;
57 static bfd_vma print_dot
;
58 static lang_input_statement_type
*first_file
;
59 static lang_statement_list_type lang_output_section_statement
;
60 static CONST
char *current_target
;
61 static CONST
char *output_target
;
62 static size_t longest_section_name
= 8;
63 static asection common_section
;
64 static section_userdata_type common_section_userdata
;
65 static lang_statement_list_type statement_list
;
69 lang_output_section_statement_type
*abs_output_section
;
70 lang_statement_list_type
*stat_ptr
= &statement_list
;
71 lang_input_statement_type
*script_file
= 0;
72 boolean option_longmap
= false;
73 lang_statement_list_type file_chain
=
75 CONST
char *entry_symbol
= 0;
76 bfd_size_type largest_section
= 0;
77 boolean lang_has_input_file
= false;
78 lang_output_section_statement_type
*create_object_symbols
= 0;
79 boolean had_output_filename
= false;
80 boolean lang_float_flag
= false;
83 extern char *default_target
;
85 extern unsigned int undefined_global_sym_count
;
86 extern char *current_file
;
87 extern bfd
*output_bfd
;
88 extern enum bfd_architecture ldfile_output_architecture
;
89 extern unsigned long ldfile_output_machine
;
90 extern char *ldfile_output_machine_name
;
91 extern ldsym_type
*symbol_head
;
92 extern unsigned int commons_pending
;
93 extern args_type command_line
;
94 extern ld_config_type config
;
95 extern boolean had_script
;
96 extern boolean write_map
;
101 #define cat(a,b) a/**/b
104 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
106 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
108 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
110 void EXFUN (lang_add_data
, (int type
, union etree_union
* exp
));
113 DEFUN (stat_alloc
, (size
),
116 return obstack_alloc (&stat_obstack
, size
);
119 DEFUN (print_size
, (value
),
122 fprintf (config
.map_file
, "%5x", (unsigned) value
);
125 DEFUN (print_alignment
, (value
),
128 fprintf (config
.map_file
, "2**%1u", value
);
131 DEFUN (print_fill
, (value
),
134 fprintf (config
.map_file
, "%04x", (unsigned) value
);
139 DEFUN (print_section
, (name
),
140 CONST
char *CONST name
)
142 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
145 /*----------------------------------------------------------------------
146 lang_for_each_statement walks the parse tree and calls the provided
147 function for each node
151 DEFUN (lang_for_each_statement_worker
, (func
, s
),
153 lang_statement_union_type
* s
)
155 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
159 switch (s
->header
.type
)
161 case lang_constructors_statement_enum
:
162 lang_for_each_statement_worker (func
, constructor_list
.head
);
164 case lang_output_section_statement_enum
:
165 lang_for_each_statement_worker
167 s
->output_section_statement
.children
.head
);
169 case lang_wild_statement_enum
:
170 lang_for_each_statement_worker
172 s
->wild_statement
.children
.head
);
174 case lang_data_statement_enum
:
175 case lang_object_symbols_statement_enum
:
176 case lang_output_statement_enum
:
177 case lang_target_statement_enum
:
178 case lang_input_section_enum
:
179 case lang_input_statement_enum
:
180 case lang_assignment_statement_enum
:
181 case lang_padding_statement_enum
:
182 case lang_address_statement_enum
:
192 DEFUN (lang_for_each_statement
, (func
),
195 lang_for_each_statement_worker (func
,
196 statement_list
.head
);
199 /*----------------------------------------------------------------------*/
201 DEFUN (lang_list_init
, (list
),
202 lang_statement_list_type
* list
)
204 list
->head
= (lang_statement_union_type
*) NULL
;
205 list
->tail
= &list
->head
;
208 /*----------------------------------------------------------------------
210 build a new statement node for the parse tree
215 lang_statement_union_type
*
216 DEFUN (new_statement
, (type
, size
, list
),
217 enum statement_enum type AND
218 bfd_size_type size AND
219 lang_statement_list_type
* list
)
221 lang_statement_union_type
*new = (lang_statement_union_type
*)
224 new->header
.type
= type
;
225 new->header
.next
= (lang_statement_union_type
*) NULL
;
226 lang_statement_append (list
, new, &new->header
.next
);
231 Build a new input file node for the language. There are several ways
232 in which we treat an input file, eg, we only look at symbols, or
233 prefix it with a -l etc.
235 We can be supplied with requests for input files more than once;
236 they may, for example be split over serveral lines like foo.o(.text)
237 foo.o(.data) etc, so when asked for a file we check that we havn't
238 got it already so we don't duplicate the bfd.
241 static lang_input_statement_type
*
242 DEFUN (new_afile
, (name
, file_type
, target
),
243 CONST
char *CONST name AND
244 CONST lang_input_file_enum_type file_type AND
245 CONST
char *CONST target
)
248 lang_input_statement_type
*p
= new_stat (lang_input_statement
,
251 lang_has_input_file
= true;
253 p
->complained
= false;
256 case lang_input_file_is_symbols_only_enum
:
258 p
->is_archive
= false;
260 p
->local_sym_name
= name
;
261 p
->just_syms_flag
= true;
262 p
->search_dirs_flag
= false;
264 case lang_input_file_is_fake_enum
:
266 p
->is_archive
= false;
268 p
->local_sym_name
= name
;
269 p
->just_syms_flag
= false;
270 p
->search_dirs_flag
= false;
272 case lang_input_file_is_l_enum
:
273 p
->is_archive
= true;
276 p
->local_sym_name
= concat ("-l", name
, "");
277 p
->just_syms_flag
= false;
278 p
->search_dirs_flag
= true;
280 case lang_input_file_is_search_file_enum
:
281 case lang_input_file_is_marker_enum
:
283 p
->is_archive
= false;
285 p
->local_sym_name
= name
;
286 p
->just_syms_flag
= false;
287 p
->search_dirs_flag
= true;
289 case lang_input_file_is_file_enum
:
291 p
->is_archive
= false;
293 p
->local_sym_name
= name
;
294 p
->just_syms_flag
= false;
295 p
->search_dirs_flag
= false;
300 p
->asymbols
= (asymbol
**) NULL
;
301 p
->superfile
= (lang_input_statement_type
*) NULL
;
302 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
303 p
->next
= (lang_statement_union_type
*) NULL
;
305 p
->common_output_section
= (asection
*) NULL
;
306 lang_statement_append (&input_file_chain
,
307 (lang_statement_union_type
*) p
,
312 lang_input_statement_type
*
313 DEFUN (lang_add_input_file
, (name
, file_type
, target
),
315 lang_input_file_enum_type file_type AND
318 /* Look it up or build a new one */
319 lang_has_input_file
= true;
321 lang_input_statement_type
*p
;
323 for (p
= (lang_input_statement_type
*) input_file_chain
.head
;
324 p
!= (lang_input_statement_type
*) NULL
;
325 p
= (lang_input_statement_type
*) (p
->next_real_file
))
327 /* Sometimes we have incomplete entries in here */
328 if (p
->filename
!= (char *) NULL
)
330 if (strcmp (name
, p
->filename
) == 0)
336 return new_afile (name
, file_type
, target
);
339 /* Build enough state so that the parser can build its tree */
341 DEFUN_VOID (lang_init
)
343 obstack_begin (&stat_obstack
, 1000);
345 stat_ptr
= &statement_list
;
347 lang_list_init (stat_ptr
);
349 lang_list_init (&input_file_chain
);
350 lang_list_init (&lang_output_section_statement
);
351 lang_list_init (&file_chain
);
352 first_file
= lang_add_input_file ((char *) NULL
,
353 lang_input_file_is_marker_enum
,
355 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
357 abs_output_section
->bfd_section
= &bfd_abs_section
;
361 /*----------------------------------------------------------------------
362 A region is an area of memory declared with the
363 MEMORY { name:org=exp, len=exp ... }
366 We maintain a list of all the regions here
368 If no regions are specified in the script, then the default is used
369 which is created when looked up to be the entire data space
372 static lang_memory_region_type
*lang_memory_region_list
;
373 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
375 lang_memory_region_type
*
376 DEFUN (lang_memory_region_lookup
, (name
),
377 CONST
char *CONST name
)
380 lang_memory_region_type
*p
= lang_memory_region_list
;
382 for (p
= lang_memory_region_list
;
383 p
!= (lang_memory_region_type
*) NULL
;
386 if (strcmp (p
->name
, name
) == 0)
391 if (strcmp (name
, "*default*") == 0)
393 /* This is the default region, dig out first one on the list */
394 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
396 return lang_memory_region_list
;
400 lang_memory_region_type
*new =
401 (lang_memory_region_type
*) stat_alloc ((bfd_size_type
) (sizeof (lang_memory_region_type
)));
403 new->name
= buystring (name
);
404 new->next
= (lang_memory_region_type
*) NULL
;
406 *lang_memory_region_list_tail
= new;
407 lang_memory_region_list_tail
= &new->next
;
411 new->had_full_message
= false;
418 lang_output_section_statement_type
*
419 DEFUN (lang_output_section_find
, (name
),
420 CONST
char *CONST name
)
422 lang_statement_union_type
*u
;
423 lang_output_section_statement_type
*lookup
;
425 for (u
= lang_output_section_statement
.head
;
426 u
!= (lang_statement_union_type
*) NULL
;
429 lookup
= &u
->output_section_statement
;
430 if (strcmp (name
, lookup
->name
) == 0)
435 return (lang_output_section_statement_type
*) NULL
;
438 lang_output_section_statement_type
*
439 DEFUN (lang_output_section_statement_lookup
, (name
),
440 CONST
char *CONST name
)
442 lang_output_section_statement_type
*lookup
;
444 lookup
= lang_output_section_find (name
);
445 if (lookup
== (lang_output_section_statement_type
*) NULL
)
448 lookup
= (lang_output_section_statement_type
*)
449 new_stat (lang_output_section_statement
, stat_ptr
);
450 lookup
->region
= (lang_memory_region_type
*) NULL
;
452 lookup
->block_value
= 1;
455 lookup
->next
= (lang_statement_union_type
*) NULL
;
456 lookup
->bfd_section
= (asection
*) NULL
;
457 lookup
->processed
= false;
458 lookup
->loadable
= 1;
459 lookup
->addr_tree
= (etree_type
*) NULL
;
460 lang_list_init (&lookup
->children
);
462 lang_statement_append (&lang_output_section_statement
,
463 (lang_statement_union_type
*) lookup
,
471 DEFUN (print_flags
, (ignore_flags
),
474 fprintf (config
.map_file
, "(");
476 if (flags
->flag_read
)
477 fprintf (outfile
, "R");
478 if (flags
->flag_write
)
479 fprintf (outfile
, "W");
480 if (flags
->flag_executable
)
481 fprintf (outfile
, "X");
482 if (flags
->flag_loadable
)
483 fprintf (outfile
, "L");
485 fprintf (config
.map_file
, ")");
489 DEFUN_VOID (lang_map
)
491 lang_memory_region_type
*m
;
493 fprintf (config
.map_file
, "**MEMORY CONFIGURATION**\n\n");
495 fprintf (config
.map_file
, "name\t\torigin\t\tlength\t\tattributes\n");
497 fprintf (config
.map_file
,
498 "name\t\torigin length r_size c_size is attributes\n");
501 for (m
= lang_memory_region_list
;
502 m
!= (lang_memory_region_type
*) NULL
;
505 fprintf (config
.map_file
, "%-16s", m
->name
);
506 print_address (m
->origin
);
508 print_address (m
->length
);
510 print_address (m
->old_length
);
512 print_address (m
->current
- m
->origin
);
515 fprintf(config
.map_file
," %2d%% ", ( m
->current
- m
->origin
) * 100 / m
->old_length
);
516 print_flags (&m
->flags
);
517 fprintf (config
.map_file
, "\n");
519 fprintf (config
.map_file
, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
520 fprintf (config
.map_file
, "output input virtual\n");
521 fprintf (config
.map_file
, "section section address tsize\n\n");
532 lang_output_section_statement_type
* s
)
534 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
535 section_userdata_type
*new =
536 (section_userdata_type
*)
537 stat_alloc ((bfd_size_type
) (sizeof (section_userdata_type
)));
539 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
540 if (s
->bfd_section
== (asection
*) NULL
)
541 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
542 if (s
->bfd_section
== (asection
*) NULL
)
544 einfo ("%P%F output format %s cannot represent section called %s\n",
545 output_bfd
->xvec
->name
, s
->name
);
547 s
->bfd_section
->output_section
= s
->bfd_section
;
548 /* s->bfd_section->flags = s->flags;*/
550 /* We initialize an output sections output offset to minus its own */
551 /* vma to allow us to output a section through itself */
552 s
->bfd_section
->output_offset
= 0;
553 get_userdata (s
->bfd_section
) = (PTR
) new;
557 /***********************************************************************
560 These expand statements like *(.text) and foo.o to a list of
561 explicit actions, like foo.o(.text), bar.o(.text) and
564 The toplevel routine, wild, takes a statement, section, file and
565 target. If either the section or file is null it is taken to be the
566 wildcard. Seperate lang_input_section statements are created for
567 each part of the expanstion, and placed after the statement provided.
572 DEFUN (wild_doit
, (ptr
, section
, output
, file
),
573 lang_statement_list_type
* ptr AND
574 asection
* section AND
575 lang_output_section_statement_type
* output AND
576 lang_input_statement_type
* file
)
578 if (output
->bfd_section
== (asection
*) NULL
)
583 if (section
!= (asection
*) NULL
584 && section
->output_section
== (asection
*) NULL
)
586 /* Add a section reference to the list */
587 lang_input_section_type
*new = new_stat (lang_input_section
, ptr
);
589 new->section
= section
;
591 section
->output_section
= output
->bfd_section
;
592 section
->output_section
->flags
|= section
->flags
;
593 if (!output
->loadable
)
595 /* Turn of load flag */
596 output
->bfd_section
->flags
&= ~SEC_LOAD
;
598 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
600 output
->bfd_section
->alignment_power
= section
->alignment_power
;
606 DEFUN (our_bfd_get_section_by_name
, (abfd
, section
),
610 return bfd_get_section_by_name (abfd
, section
);
614 DEFUN (wild_section
, (ptr
, section
, file
, output
),
615 lang_wild_statement_type
* ptr AND
616 CONST
char *section AND
617 lang_input_statement_type
* file AND
618 lang_output_section_statement_type
* output
)
622 if (file
->just_syms_flag
== false)
624 if (section
== (char *) NULL
)
626 /* Do the creation to all sections in the file */
627 for (s
= file
->the_bfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
629 wild_doit (&ptr
->children
, s
, output
, file
);
634 /* Do the creation to the named section only */
635 wild_doit (&ptr
->children
,
636 our_bfd_get_section_by_name (file
->the_bfd
, section
),
642 /* passed a file name (which must have been seen already and added to
643 the statement tree. We will see if it has been opened already and
644 had its symbols read. If not then we'll read it.
646 Archives are pecuilar here. We may open them once, but if they do
647 not define anything we need at the time, they won't have all their
648 symbols read. If we need them later, we'll have to redo it.
651 lang_input_statement_type
*
652 DEFUN (lookup_name
, (name
),
653 CONST
char *CONST name
)
655 lang_input_statement_type
*search
;
657 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
658 search
!= (lang_input_statement_type
*) NULL
;
659 search
= (lang_input_statement_type
*) search
->next_real_file
)
661 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
665 if (search
->filename
!= (char *) NULL
&& name
!= (char *) NULL
)
667 if (strcmp (search
->filename
, name
) == 0)
669 ldmain_open_file_read_symbol (search
);
675 /* There isn't an afile entry for this file yet, this must be
676 because the name has only appeared inside a load script and not
677 on the command line */
678 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
);
679 ldmain_open_file_read_symbol (search
);
686 DEFUN (wild
, (s
, section
, file
, target
, output
),
687 lang_wild_statement_type
* s AND
688 CONST
char *CONST section AND
689 CONST
char *CONST file AND
690 CONST
char *CONST target AND
691 lang_output_section_statement_type
* output
)
693 lang_input_statement_type
*f
;
695 if (file
== (char *) NULL
)
697 /* Perform the iteration over all files in the list */
698 for (f
= (lang_input_statement_type
*) file_chain
.head
;
699 f
!= (lang_input_statement_type
*) NULL
;
700 f
= (lang_input_statement_type
*) f
->next
)
702 wild_section (s
, section
, f
, output
);
707 /* Perform the iteration over a single file */
708 wild_section (s
, section
, lookup_name (file
), output
);
710 if (section
!= (char *) NULL
711 && strcmp (section
, "COMMON") == 0
712 && default_common_section
== (lang_output_section_statement_type
*) NULL
)
714 /* Remember the section that common is going to incase we later
715 get something which doesn't know where to put it */
716 default_common_section
= output
;
721 read in all the files
724 DEFUN (open_output
, (name
),
725 CONST
char *CONST name
)
727 extern unsigned long ldfile_output_machine
;
728 extern enum bfd_architecture ldfile_output_architecture
;
730 extern CONST
char *output_filename
;
733 if (output_target
== (char *) NULL
)
735 if (current_target
!= (char *) NULL
)
736 output_target
= current_target
;
738 output_target
= default_target
;
740 output
= bfd_openw (name
, output_target
);
741 output_filename
= name
;
743 if (output
== (bfd
*) NULL
)
745 if (bfd_error
== invalid_target
)
747 einfo ("%P%F target %s not found\n", output_target
);
749 einfo ("%P%F problem opening output file %s, %E", name
);
752 /* output->flags |= D_PAGED;*/
754 bfd_set_format (output
, bfd_object
);
755 bfd_set_arch_mach (output
,
756 ldfile_output_architecture
,
757 ldfile_output_machine
);
765 DEFUN (ldlang_open_output
, (statement
),
766 lang_statement_union_type
* statement
)
768 switch (statement
->header
.type
)
770 case lang_output_statement_enum
:
771 output_bfd
= open_output (statement
->output_statement
.name
);
772 ldemul_set_output_arch ();
773 if (config
.magic_demand_paged
&& !config
.relocateable_output
)
774 output_bfd
->flags
|= D_PAGED
;
776 output_bfd
->flags
&= ~D_PAGED
;
777 if (config
.text_read_only
)
778 output_bfd
->flags
|= WP_TEXT
;
780 output_bfd
->flags
&= ~WP_TEXT
;
783 case lang_target_statement_enum
:
784 current_target
= statement
->target_statement
.target
;
792 DEFUN (open_input_bfds
, (statement
),
793 lang_statement_union_type
* statement
)
795 switch (statement
->header
.type
)
797 case lang_target_statement_enum
:
798 current_target
= statement
->target_statement
.target
;
800 case lang_wild_statement_enum
:
801 /* Maybe we should load the file's symbols */
802 if (statement
->wild_statement
.filename
)
804 (void) lookup_name (statement
->wild_statement
.filename
);
807 case lang_input_statement_enum
:
808 if (statement
->input_statement
.real
== true)
810 statement
->input_statement
.target
= current_target
;
811 lookup_name (statement
->input_statement
.filename
);
819 /* If there are [COMMONS] statements, put a wild one into the bss section */
822 lang_reasonable_defaults ()
828 lang_output_section_statement_lookup (".text");
829 lang_output_section_statement_lookup (".data");
831 default_common_section
=
832 lang_output_section_statement_lookup (".bss");
835 if (placed_commons
== false)
837 lang_wild_statement_type
*new =
838 new_stat (lang_wild_statement
,
839 &default_common_section
->children
);
841 new->section_name
= "COMMON";
842 new->filename
= (char *) NULL
;
843 lang_list_init (&new->children
);
850 Add the supplied name to the symbol table as an undefined reference.
851 Remove items from the chain as we open input bfds
853 typedef struct ldlang_undef_chain_list
855 struct ldlang_undef_chain_list
*next
;
857 } ldlang_undef_chain_list_type
;
859 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
862 DEFUN (ldlang_add_undef
, (name
),
863 CONST
char *CONST name
)
865 ldlang_undef_chain_list_type
*new =
866 (ldlang_undef_chain_list_type
867 *) stat_alloc ((bfd_size_type
) (sizeof (ldlang_undef_chain_list_type
)));
869 new->next
= ldlang_undef_chain_list_head
;
870 ldlang_undef_chain_list_head
= new;
872 new->name
= buystring (name
);
875 /* Run through the list of undefineds created above and place them
876 into the linker hash table as undefined symbols belonging to the
880 DEFUN_VOID (lang_place_undefineds
)
882 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
884 while (ptr
!= (ldlang_undef_chain_list_type
*) NULL
)
887 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
889 def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
891 def
->name
= ptr
->name
;
892 def
->section
= &bfd_und_section
;
893 Q_enter_global_ref (def_ptr
, ptr
->name
);
898 /* Copy important data from out internal form to the bfd way. Also
899 create a section for the dummy file
903 DEFUN_VOID (lang_create_output_section_statements
)
905 lang_statement_union_type
*os
;
907 for (os
= lang_output_section_statement
.head
;
908 os
!= (lang_statement_union_type
*) NULL
;
909 os
= os
->output_section_statement
.next
)
911 lang_output_section_statement_type
*s
=
912 &os
->output_section_statement
;
920 DEFUN_VOID (lang_init_script_file
)
922 script_file
= lang_add_input_file ("script file",
923 lang_input_file_is_fake_enum
,
925 script_file
->the_bfd
= bfd_create ("script file", output_bfd
);
926 script_file
->symbol_count
= 0;
927 script_file
->the_bfd
->sections
= output_bfd
->sections
;
928 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
930 abs_output_section
->bfd_section
= &bfd_abs_section
;
934 /* Open input files and attatch to output sections */
936 DEFUN (map_input_to_output_sections
, (s
, target
, output_section_statement
),
937 lang_statement_union_type
* s AND
938 CONST
char *target AND
939 lang_output_section_statement_type
* output_section_statement
)
941 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
943 switch (s
->header
.type
)
947 case lang_wild_statement_enum
:
948 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
949 s
->wild_statement
.filename
, target
,
950 output_section_statement
);
953 case lang_constructors_statement_enum
:
954 map_input_to_output_sections (constructor_list
.head
,
956 output_section_statement
);
958 case lang_output_section_statement_enum
:
959 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
961 &s
->output_section_statement
);
963 case lang_output_statement_enum
:
965 case lang_target_statement_enum
:
966 target
= s
->target_statement
.target
;
968 case lang_fill_statement_enum
:
969 case lang_input_section_enum
:
970 case lang_object_symbols_statement_enum
:
971 case lang_data_statement_enum
:
972 case lang_assignment_statement_enum
:
973 case lang_padding_statement_enum
:
975 case lang_afile_asection_pair_statement_enum
:
978 case lang_address_statement_enum
:
979 /* Mark the specified section with the supplied address */
981 lang_output_section_statement_type
*os
=
982 lang_output_section_statement_lookup
983 (s
->address_statement
.section_name
);
985 os
->addr_tree
= s
->address_statement
.address
;
986 if (os
->bfd_section
== (asection
*) NULL
)
988 einfo ("%P%F can't set the address of undefined section %s\n",
989 s
->address_statement
.section_name
);
993 case lang_input_statement_enum
:
994 /* A standard input statement, has no wildcards */
995 /* ldmain_open_file_read_symbol(&s->input_statement);*/
1006 DEFUN (print_output_section_statement
, (output_section_statement
),
1007 lang_output_section_statement_type
* output_section_statement
)
1009 asection
*section
= output_section_statement
->bfd_section
;
1012 print_section (output_section_statement
->name
);
1016 print_dot
= section
->vma
;
1020 print_address (section
->vma
);
1022 print_size (section
->_raw_size
);
1024 print_size(section
->_cooked_size
);
1026 print_alignment (section
->alignment_power
);
1029 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1030 print_flags (stdout
, &output_section_statement
->flags
);
1032 if (section
->flags
& SEC_LOAD
)
1033 fprintf (config
.map_file
, "load ");
1034 if (section
->flags
& SEC_ALLOC
)
1035 fprintf (config
.map_file
, "alloc ");
1036 if (section
->flags
& SEC_RELOC
)
1037 fprintf (config
.map_file
, "reloc ");
1038 if (section
->flags
& SEC_HAS_CONTENTS
)
1039 fprintf (config
.map_file
, "contents ");
1044 fprintf (config
.map_file
, "No attached output section");
1047 print_statement (output_section_statement
->children
.head
,
1048 output_section_statement
);
1053 DEFUN (print_assignment
, (assignment
, output_section
),
1054 lang_assignment_statement_type
* assignment AND
1055 lang_output_section_statement_type
* output_section
)
1057 etree_value_type result
;
1063 print_address (print_dot
);
1065 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1067 lang_final_phase_enum
,
1073 print_address (result
.value
);
1077 fprintf (config
.map_file
, "*undefined*");
1080 exp_print_tree (assignment
->exp
);
1082 fprintf (config
.map_file
, "\n");
1086 DEFUN (print_input_statement
, (statm
),
1087 lang_input_statement_type
* statm
)
1089 if (statm
->filename
!= (char *) NULL
)
1091 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1096 DEFUN (print_symbol
, (q
),
1100 fprintf (config
.map_file
, " ");
1102 fprintf (config
.map_file
, " ");
1103 print_address (outside_symbol_address (q
));
1104 fprintf (config
.map_file
, " %s", q
->name
? q
->name
: " ");
1109 DEFUN (print_input_section
, (in
),
1110 lang_input_section_type
* in
)
1112 asection
*i
= in
->section
;
1113 int size
= i
->reloc_done
?
1114 bfd_get_section_size_after_reloc (i
) :
1115 bfd_get_section_size_before_reloc (i
);
1120 fprintf (config
.map_file
, " ");
1121 print_section (i
->name
);
1122 fprintf (config
.map_file
, " ");
1123 if (i
->output_section
)
1125 print_address (i
->output_section
->vma
+ i
->output_offset
);
1126 fprintf (config
.map_file
, " ");
1127 print_size (i
->_raw_size
);
1128 fprintf (config
.map_file
, " ");
1129 print_size(i
->_cooked_size
);
1130 fprintf (config
.map_file
, " ");
1131 print_alignment (i
->alignment_power
);
1132 fprintf (config
.map_file
, " ");
1136 bfd
*abfd
= in
->ifile
->the_bfd
;
1138 if (in
->ifile
->just_syms_flag
== true)
1140 fprintf (config
.map_file
, "symbols only ");
1143 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1144 if (abfd
->my_archive
!= (bfd
*) NULL
)
1146 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1151 fprintf (config
.map_file
, "%s", abfd
->filename
);
1153 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1156 /* Find all the symbols in this file defined in this section */
1158 if (in
->ifile
->symbol_count
)
1162 for (p
= in
->ifile
->asymbols
; *p
; p
++)
1166 if (bfd_get_section (q
) == i
&& q
->flags
& BSF_GLOBAL
)
1179 print_dot
= outside_section_address (i
) + size
;
1183 fprintf (config
.map_file
, "No output section allocated\n");
1189 DEFUN (print_fill_statement
, (fill
),
1190 lang_fill_statement_type
* fill
)
1192 fprintf (config
.map_file
, "FILL mask ");
1193 print_fill (fill
->fill
);
1197 DEFUN (print_data_statement
, (data
),
1198 lang_data_statement_type
* data
)
1200 /* bfd_vma value; */
1205 /* ASSERT(print_dot == data->output_vma);*/
1207 print_address (data
->output_vma
+ data
->output_section
->vma
);
1209 print_address (data
->value
);
1214 fprintf (config
.map_file
, "BYTE ");
1215 print_dot
+= BYTE_SIZE
;
1218 fprintf (config
.map_file
, "SHORT ");
1219 print_dot
+= SHORT_SIZE
;
1222 fprintf (config
.map_file
, "LONG ");
1223 print_dot
+= LONG_SIZE
;
1227 exp_print_tree (data
->exp
);
1229 fprintf (config
.map_file
, "\n");
1234 DEFUN (print_padding_statement
, (s
),
1235 lang_padding_statement_type
* s
)
1239 print_section ("*fill*");
1241 print_address (s
->output_offset
+ s
->output_section
->vma
);
1243 print_size (s
->size
);
1245 print_fill (s
->fill
);
1248 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1253 DEFUN (print_wild_statement
, (w
, os
),
1254 lang_wild_statement_type
* w AND
1255 lang_output_section_statement_type
* os
)
1257 fprintf (config
.map_file
, " from ");
1258 if (w
->filename
!= (char *) NULL
)
1260 fprintf (config
.map_file
, "%s", w
->filename
);
1264 fprintf (config
.map_file
, "*");
1266 if (w
->section_name
!= (char *) NULL
)
1268 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1272 fprintf (config
.map_file
, "(*)");
1275 print_statement (w
->children
.head
, os
);
1279 DEFUN (print_statement
, (s
, os
),
1280 lang_statement_union_type
* s AND
1281 lang_output_section_statement_type
* os
)
1285 switch (s
->header
.type
)
1287 case lang_constructors_statement_enum
:
1288 fprintf (config
.map_file
, "constructors:\n");
1289 print_statement (constructor_list
.head
, os
);
1291 case lang_wild_statement_enum
:
1292 print_wild_statement (&s
->wild_statement
, os
);
1295 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1298 case lang_address_statement_enum
:
1299 fprintf (config
.map_file
, "address\n");
1301 case lang_object_symbols_statement_enum
:
1302 fprintf (config
.map_file
, "object symbols\n");
1304 case lang_fill_statement_enum
:
1305 print_fill_statement (&s
->fill_statement
);
1307 case lang_data_statement_enum
:
1308 print_data_statement (&s
->data_statement
);
1310 case lang_input_section_enum
:
1311 print_input_section (&s
->input_section
);
1313 case lang_padding_statement_enum
:
1314 print_padding_statement (&s
->padding_statement
);
1316 case lang_output_section_statement_enum
:
1317 print_output_section_statement (&s
->output_section_statement
);
1319 case lang_assignment_statement_enum
:
1320 print_assignment (&s
->assignment_statement
,
1323 case lang_target_statement_enum
:
1324 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1326 case lang_output_statement_enum
:
1327 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1328 s
->output_statement
.name
,
1331 case lang_input_statement_enum
:
1332 print_input_statement (&s
->input_statement
);
1334 case lang_afile_asection_pair_statement_enum
:
1344 DEFUN_VOID (print_statements
)
1346 print_statement (statement_list
.head
,
1347 abs_output_section
);
1352 DEFUN (insert_pad
, (this_ptr
, fill
, power
, output_section_statement
, dot
),
1353 lang_statement_union_type
** this_ptr AND
1355 unsigned int power AND
1356 asection
* output_section_statement AND
1359 /* Align this section first to the
1360 input sections requirement, then
1361 to the output section's requirement.
1362 If this alignment is > than any seen before,
1363 then record it too. Perform the alignment by
1364 inserting a magic 'padding' statement.
1367 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1369 if (alignment_needed
!= 0)
1371 lang_statement_union_type
*new =
1372 (lang_statement_union_type
*)
1373 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1375 /* Link into existing chain */
1376 new->header
.next
= *this_ptr
;
1378 new->header
.type
= lang_padding_statement_enum
;
1379 new->padding_statement
.output_section
= output_section_statement
;
1380 new->padding_statement
.output_offset
=
1381 dot
- output_section_statement
->vma
;
1382 new->padding_statement
.fill
= fill
;
1383 new->padding_statement
.size
= alignment_needed
;
1387 /* Remember the most restrictive alignment */
1388 if (power
> output_section_statement
->alignment_power
)
1390 output_section_statement
->alignment_power
= power
;
1392 output_section_statement
->_raw_size
+= alignment_needed
;
1393 return alignment_needed
+ dot
;
1397 /* Work out how much this section will move the dot point */
1399 DEFUN (size_input_section
, (this_ptr
, output_section_statement
, fill
,
1401 lang_statement_union_type
** this_ptr AND
1402 lang_output_section_statement_type
* output_section_statement AND
1403 unsigned short fill AND
1407 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1408 asection
*i
= is
->section
;
1410 if (is
->ifile
->just_syms_flag
== false)
1412 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1413 output_section_statement
->bfd_section
, dot
);
1415 /* remember the largest size so we can malloc the largest area
1416 needed for the output stage. Only remember the size of sections
1417 which we will actually allocate */
1419 (SEC_HAS_CONTENTS
| SEC_LOAD
)) == (SEC_HAS_CONTENTS
| SEC_LOAD
))
1420 && (bfd_get_section_size_before_reloc (i
) > largest_section
))
1422 largest_section
= bfd_get_section_size_before_reloc (i
);
1425 /* Remember where in the output section this input section goes */
1427 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1429 /* Mark how big the output section must be to contain this now
1433 dot
+= i
->_cooked_size
;
1437 dot
+= i
->_raw_size
;
1439 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1443 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1449 /* Sizing happens in two passes, first pass we allocate worst case
1450 stuff. The second pass (if relaxing), we use what we learnt to
1451 change the size of some relocs from worst case to better
1453 static boolean had_relax
;
1456 DEFUN (lang_size_sections
, (s
, output_section_statement
, prev
, fill
,
1458 lang_statement_union_type
* s AND
1459 lang_output_section_statement_type
* output_section_statement AND
1460 lang_statement_union_type
** prev AND
1461 unsigned short fill AND
1465 /* Size up the sections from their constituent parts */
1466 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1468 switch (s
->header
.type
)
1471 case lang_output_section_statement_enum
:
1474 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1476 if (os
->bfd_section
== &bfd_abs_section
)
1478 /* No matter what happens, an abs section starts at zero */
1479 bfd_set_section_vma (0, os
->bfd_section
, 0);
1483 if (os
->addr_tree
== (etree_type
*) NULL
)
1485 /* No address specified for this section, get one
1486 from the region specification
1488 if (os
->region
== (lang_memory_region_type
*) NULL
)
1490 os
->region
= lang_memory_region_lookup ("*default*");
1492 dot
= os
->region
->current
;
1498 r
= exp_fold_tree (os
->addr_tree
,
1500 lang_allocating_phase_enum
,
1502 if (r
.valid
== false)
1504 einfo ("%F%S: non constant address expression for section %s\n",
1509 /* The section starts here */
1510 /* First, align to what the section needs */
1513 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1514 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1518 os
->bfd_section
->output_offset
= 0;
1520 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1521 os
->fill
, dot
, relax
);
1522 /* Ignore the size of the input sections, use the vma and size to */
1526 after
= ALIGN (os
->bfd_section
->vma
+
1527 os
->bfd_section
->_raw_size
,
1531 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1532 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1533 os
->processed
= true;
1535 /* Replace into region ? */
1536 if (os
->addr_tree
== (etree_type
*) NULL
1537 && os
->region
!= (lang_memory_region_type
*) NULL
)
1539 os
->region
->current
= dot
;
1540 /* Make sure this isn't silly */
1541 if (os
->region
->current
>
1542 os
->region
->origin
+
1545 einfo ("%X%P: Region %s is full (%B section %s)\n",
1547 os
->bfd_section
->owner
,
1548 os
->bfd_section
->name
);
1549 /* Reset the region pointer */
1550 os
->region
->current
= 0;
1558 case lang_constructors_statement_enum
:
1559 dot
= lang_size_sections (constructor_list
.head
,
1560 output_section_statement
,
1561 &s
->wild_statement
.children
.head
,
1566 case lang_data_statement_enum
:
1568 unsigned int size
= 0;
1570 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1571 s
->data_statement
.output_section
=
1572 output_section_statement
->bfd_section
;
1574 switch (s
->data_statement
.type
)
1588 output_section_statement
->bfd_section
->_raw_size
+= size
;
1592 case lang_wild_statement_enum
:
1594 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
1595 output_section_statement
,
1596 &s
->wild_statement
.children
.head
,
1602 case lang_object_symbols_statement_enum
:
1603 create_object_symbols
= output_section_statement
;
1605 case lang_output_statement_enum
:
1606 case lang_target_statement_enum
:
1608 case lang_input_section_enum
:
1613 if( relax_section (prev
))
1619 (*prev
)->input_section
.section
->_cooked_size
=
1620 (*prev
)->input_section
.section
->_raw_size
;
1623 dot
= size_input_section (prev
,
1624 output_section_statement
,
1625 output_section_statement
->fill
,
1628 case lang_input_statement_enum
:
1630 case lang_fill_statement_enum
:
1631 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
1633 fill
= s
->fill_statement
.fill
;
1635 case lang_assignment_statement_enum
:
1637 bfd_vma newdot
= dot
;
1639 exp_fold_tree (s
->assignment_statement
.exp
,
1640 output_section_statement
,
1641 lang_allocating_phase_enum
,
1645 if (newdot
!= dot
&& !relax
)
1646 /* We've been moved ! so insert a pad */
1648 lang_statement_union_type
*new =
1649 (lang_statement_union_type
*)
1650 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1652 /* Link into existing chain */
1653 new->header
.next
= *prev
;
1655 new->header
.type
= lang_padding_statement_enum
;
1656 new->padding_statement
.output_section
=
1657 output_section_statement
->bfd_section
;
1658 new->padding_statement
.output_offset
=
1659 dot
- output_section_statement
->bfd_section
->vma
;
1660 new->padding_statement
.fill
= fill
;
1661 new->padding_statement
.size
= newdot
- dot
;
1662 output_section_statement
->bfd_section
->_raw_size
+=
1663 new->padding_statement
.size
;
1672 /* This can only get here when relaxing is turned on */
1673 case lang_padding_statement_enum
:
1675 case lang_address_statement_enum
:
1678 prev
= &s
->header
.next
;
1684 DEFUN (lang_do_assignments
, (s
, output_section_statement
, fill
, dot
),
1685 lang_statement_union_type
* s AND
1686 lang_output_section_statement_type
* output_section_statement AND
1687 unsigned short fill AND
1691 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1693 switch (s
->header
.type
)
1695 case lang_constructors_statement_enum
:
1696 dot
= lang_do_assignments (constructor_list
.head
,
1697 output_section_statement
,
1702 case lang_output_section_statement_enum
:
1704 lang_output_section_statement_type
*os
=
1705 &(s
->output_section_statement
);
1707 dot
= os
->bfd_section
->vma
;
1708 (void) lang_do_assignments (os
->children
.head
, os
, os
->fill
, dot
);
1709 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1712 case lang_wild_statement_enum
:
1714 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
1715 output_section_statement
,
1720 case lang_object_symbols_statement_enum
:
1721 case lang_output_statement_enum
:
1722 case lang_target_statement_enum
:
1724 case lang_common_statement_enum
:
1727 case lang_data_statement_enum
:
1729 etree_value_type value
;
1731 value
= exp_fold_tree (s
->data_statement
.exp
,
1733 lang_final_phase_enum
, dot
, &dot
);
1734 s
->data_statement
.value
= value
.value
;
1735 if (value
.valid
== false)
1736 einfo ("%F%P: Invalid data statement\n");
1738 switch (s
->data_statement
.type
)
1751 case lang_input_section_enum
:
1753 asection
*in
= s
->input_section
.section
;
1755 dot
+= bfd_get_section_size_before_reloc (in
);
1759 case lang_input_statement_enum
:
1761 case lang_fill_statement_enum
:
1762 fill
= s
->fill_statement
.fill
;
1764 case lang_assignment_statement_enum
:
1766 exp_fold_tree (s
->assignment_statement
.exp
,
1767 output_section_statement
,
1768 lang_final_phase_enum
,
1774 case lang_padding_statement_enum
:
1775 dot
+= s
->padding_statement
.size
;
1780 case lang_address_statement_enum
:
1791 DEFUN_VOID (lang_relocate_globals
)
1795 Each ldsym_type maintains a chain of pointers to asymbols which
1796 references the definition. Replace each pointer to the referenence
1797 with a pointer to only one place, preferably the definition. If
1798 the defintion isn't available then the common symbol, and if
1799 there isn't one of them then choose one reference.
1802 FOR_EACH_LDSYM (lgs
)
1806 if (lgs
->sdefs_chain
)
1808 it
= *(lgs
->sdefs_chain
);
1810 else if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
1812 it
= *(lgs
->scoms_chain
);
1814 else if (lgs
->srefs_chain
!= (asymbol
**) NULL
)
1816 it
= *(lgs
->srefs_chain
);
1820 /* This can happen when the command line asked for a symbol to
1822 it
= (asymbol
*) NULL
;
1824 if (it
!= (asymbol
*) NULL
)
1826 asymbol
**ptr
= lgs
->srefs_chain
;;
1827 if (lgs
->flags
& SYM_WARNING
)
1829 produce_warnings (lgs
, it
);
1832 while (ptr
!= (asymbol
**) NULL
)
1834 asymbol
*ref
= *ptr
;
1837 ptr
= (asymbol
**) (ref
->udata
);
1846 DEFUN_VOID (lang_finish
)
1850 if (entry_symbol
== (char *) NULL
)
1852 /* No entry has been specified, look for start */
1853 entry_symbol
= "start";
1855 lgs
= ldsym_get_soft (entry_symbol
);
1856 if (lgs
&& lgs
->sdefs_chain
)
1858 asymbol
*sy
= *(lgs
->sdefs_chain
);
1860 /* We can set the entry address*/
1861 bfd_set_start_address (output_bfd
,
1862 outside_symbol_address (sy
));
1867 /* Can't find anything reasonable,
1868 use the first address in the text section
1870 asection
*ts
= bfd_get_section_by_name (output_bfd
, ".text");
1874 bfd_set_start_address (output_bfd
, ts
->vma
);
1879 /* By now we know the target architecture, and we may have an */
1880 /* ldfile_output_machine_name */
1882 DEFUN_VOID (lang_check
)
1884 lang_statement_union_type
*file
;
1886 unsigned long input_machine
;
1887 enum bfd_architecture input_architecture
;
1888 CONST bfd_arch_info_type
*compatible
;
1890 for (file
= file_chain
.head
;
1891 file
!= (lang_statement_union_type
*) NULL
;
1892 file
= file
->input_statement
.next
)
1894 unsigned long ldfile_new_output_machine
= 0;
1895 enum bfd_architecture ldfile_new_output_architecture
= bfd_arch_unknown
;
1897 input_bfd
= file
->input_statement
.the_bfd
;
1899 input_machine
= bfd_get_mach (input_bfd
);
1900 input_architecture
= bfd_get_arch (input_bfd
);
1903 /* Inspect the architecture and ensure we're linking like with
1906 compatible
= bfd_arch_get_compatible (input_bfd
,
1911 ldfile_output_machine
= compatible
->mach
;
1912 ldfile_output_architecture
= compatible
->arch
;
1917 info ("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1918 bfd_printable_name (input_bfd
), input_bfd
,
1919 bfd_printable_name (output_bfd
));
1921 bfd_set_arch_mach (output_bfd
,
1922 ldfile_new_output_architecture
,
1923 ldfile_new_output_machine
);
1930 * run through all the global common symbols and tie them
1931 * to the output section requested.
1933 As an experiment we do this 4 times, once for all the byte sizes,
1934 then all the two bytes, all the four bytes and then everything else
1938 DEFUN_VOID (lang_common
)
1943 if (config
.relocateable_output
== false ||
1944 command_line
.force_common_definition
== true)
1946 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<= 1)
1948 for (lgs
= symbol_head
;
1949 lgs
!= (ldsym_type
*) NULL
;
1953 unsigned int power_of_two
;
1957 if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
1959 com
= *(lgs
->scoms_chain
);
1989 if (config
.sort_common
== false || align
== power
)
1991 /* Change from a common symbol into a definition of
1993 lgs
->sdefs_chain
= lgs
->scoms_chain
;
1994 lgs
->scoms_chain
= (asymbol
**) NULL
;
1996 /* Point to the correct common section */
1998 ((lang_input_statement_type
*)
1999 (com
->the_bfd
->usrdata
))->common_section
;
2000 /* Fix the size of the common section */
2002 com
->section
->_raw_size
=
2003 ALIGN (com
->section
->_raw_size
, align
);
2005 /* Remember if this is the biggest alignment ever seen */
2006 if (power_of_two
> com
->section
->alignment_power
)
2008 com
->section
->alignment_power
= power_of_two
;
2011 /* Symbol stops being common and starts being global, but
2012 we remember that it was common once. */
2014 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
2015 com
->value
= com
->section
->_raw_size
;
2019 fprintf (config
.map_file
, "Allocating common %s: %x at %x %s\n",
2022 (unsigned) com
->value
,
2023 com
->the_bfd
->filename
);
2026 com
->section
->_raw_size
+= size
;
2039 run through the input files and ensure that every input
2040 section has somewhere to go. If one is found without
2041 a destination then create an input request and place it
2042 into the statement tree.
2046 DEFUN_VOID (lang_place_orphans
)
2048 lang_input_statement_type
*file
;
2050 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2051 file
!= (lang_input_statement_type
*) NULL
;
2052 file
= (lang_input_statement_type
*) file
->next
)
2056 for (s
= file
->the_bfd
->sections
;
2057 s
!= (asection
*) NULL
;
2060 if (s
->output_section
== (asection
*) NULL
)
2062 /* This section of the file is not attatched, root
2063 around for a sensible place for it to go */
2065 if (file
->common_section
== s
)
2067 /* This is a lonely common section which must
2068 have come from an archive. We attatch to the
2069 section with the wildcard */
2070 if (config
.relocateable_output
!= true
2071 && command_line
.force_common_definition
== false)
2073 if (default_common_section
==
2074 (lang_output_section_statement_type
*) NULL
)
2076 info ("%P: No [COMMON] command, defaulting to .bss\n");
2078 default_common_section
=
2079 lang_output_section_statement_lookup (".bss");
2082 wild_doit (&default_common_section
->children
, s
,
2083 default_common_section
, file
);
2088 lang_output_section_statement_type
*os
=
2089 lang_output_section_statement_lookup (s
->name
);
2091 wild_doit (&os
->children
, s
, os
, file
);
2100 DEFUN (lang_set_flags
, (ptr
, flags
),
2104 boolean state
= false;
2119 /* ptr->flag_read = state; */
2122 /* ptr->flag_write = state; */
2125 /* ptr->flag_executable= state;*/
2129 /* ptr->flag_loadable= state;*/
2132 einfo ("%P%F illegal syntax in flags\n");
2142 DEFUN (lang_for_each_file
, (func
),
2143 PROTO (void, (*func
), (lang_input_statement_type
*)))
2145 lang_input_statement_type
*f
;
2147 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2148 f
!= (lang_input_statement_type
*) NULL
;
2149 f
= (lang_input_statement_type
*) f
->next
)
2157 DEFUN (lang_for_each_input_section
, (func
),
2158 PROTO (void, (*func
), (bfd
* ab
, asection
* as
)))
2160 lang_input_statement_type
*f
;
2162 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2163 f
!= (lang_input_statement_type
*) NULL
;
2164 f
= (lang_input_statement_type
*) f
->next
)
2168 for (s
= f
->the_bfd
->sections
;
2169 s
!= (asection
*) NULL
;
2172 func (f
->the_bfd
, s
);
2180 DEFUN (ldlang_add_file
, (entry
),
2181 lang_input_statement_type
* entry
)
2184 lang_statement_append (&file_chain
,
2185 (lang_statement_union_type
*) entry
,
2190 DEFUN (lang_add_output
, (name
),
2193 lang_output_statement_type
*new = new_stat (lang_output_statement
,
2197 had_output_filename
= true;
2201 static lang_output_section_statement_type
*current_section
;
2204 DEFUN (lang_enter_output_section_statement
,
2205 (output_section_statement_name
,
2209 char *output_section_statement_name AND
2210 etree_type
* address_exp AND
2212 bfd_vma block_value
)
2214 lang_output_section_statement_type
*os
;
2218 lang_output_section_statement_lookup (output_section_statement_name
);
2222 /* Add this statement to tree */
2223 /* add_statement(lang_output_section_statement_enum,
2224 output_section_statement);*/
2225 /* Make next things chain into subchain of this */
2227 if (os
->addr_tree
==
2228 (etree_type
*) NULL
)
2234 if (flags
& SEC_NEVER_LOAD
)
2238 os
->block_value
= block_value
;
2239 stat_ptr
= &os
->children
;
2244 DEFUN_VOID (lang_final
)
2246 if (had_output_filename
== false)
2248 extern CONST
char *output_filename
;
2250 lang_add_output (output_filename
);
2254 /* Reset the current counters in the regions */
2256 DEFUN_VOID (reset_memory_regions
)
2258 lang_memory_region_type
*p
= lang_memory_region_list
;
2260 for (p
= lang_memory_region_list
;
2261 p
!= (lang_memory_region_type
*) NULL
;
2264 p
->old_length
= p
->current
- p
->origin
;
2265 p
->current
= p
->origin
;
2272 DEFUN (create_symbol
, (name
, flags
, section
),
2273 CONST
char *name AND
2277 extern lang_input_statement_type
*script_file
;
2278 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
2280 /* Add this definition to script file */
2281 asymbol
*def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
2283 def
->name
= buystring (name
);
2286 def
->section
= section
;
2289 Q_enter_global_ref (def_ptr
, name
);
2294 DEFUN_VOID (lang_process
)
2297 if (had_script
== false)
2299 parse_line (ldemul_get_script (), 1);
2301 lang_reasonable_defaults ();
2302 current_target
= default_target
;
2304 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2305 /* For each output section statement, create a section in the output
2307 lang_create_output_section_statements ();
2309 /* Create a dummy bfd for the script */
2310 lang_init_script_file ();
2312 /* Add to the hash table all undefineds on the command line */
2313 lang_place_undefineds ();
2315 /* Create a bfd for each input file */
2316 current_target
= default_target
;
2317 lang_for_each_statement (open_input_bfds
);
2319 common_section
.userdata
= (PTR
) & common_section_userdata
;
2322 /* Run through the contours of the script and attatch input sections
2323 to the correct output sections
2325 find_constructors ();
2326 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2327 (lang_output_section_statement_type
*) NULL
);
2330 /* Find any sections not attatched explicitly and handle them */
2331 lang_place_orphans ();
2333 /* Size up the common data */
2336 ldemul_before_allocation ();
2346 lang_size_sections (statement_list
.head
,
2347 (lang_output_section_statement_type
*) NULL
,
2348 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2349 /* FIXME. Until the code in relax is fixed so that it only reads in
2350 stuff once, we cant iterate since there is no way for the linker to
2351 know what has been patched and what hasn't */
2357 /* Now run around and relax if we can */
2358 if (command_line
.relax
)
2360 /* First time round is a trial run to get the 'worst case' addresses of the
2361 objects if there was no relaxing */
2362 lang_size_sections (statement_list
.head
,
2363 (lang_output_section_statement_type
*) NULL
,
2364 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2368 /* Move the global symbols around so the second pass of relaxing can
2370 lang_relocate_globals ();
2372 reset_memory_regions ();
2374 /* Do all the assignments, now that we know the final restingplaces
2375 of all the symbols */
2377 lang_do_assignments (statement_list
.head
,
2382 /* Perform another relax pass - this time we know where the
2383 globals are, so can make better guess */
2384 lang_size_sections (statement_list
.head
,
2385 (lang_output_section_statement_type
*) NULL
,
2386 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2394 /* Size up the sections */
2395 lang_size_sections (statement_list
.head
,
2397 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2402 /* See if anything special should be done now we know how big
2404 ldemul_after_allocation ();
2406 /* Do all the assignments, now that we know the final restingplaces
2407 of all the symbols */
2409 lang_do_assignments (statement_list
.head
,
2414 /* Move the global symbols around */
2415 lang_relocate_globals ();
2417 /* Make sure that we're not mixing architectures */
2425 /* EXPORTED TO YACC */
2428 DEFUN (lang_add_wild
, (section_name
, filename
),
2429 CONST
char *CONST section_name AND
2430 CONST
char *CONST filename
)
2432 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
2435 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
2437 placed_commons
= true;
2439 if (filename
!= (char *) NULL
)
2441 lang_has_input_file
= true;
2443 new->section_name
= section_name
;
2444 new->filename
= filename
;
2445 lang_list_init (&new->children
);
2449 DEFUN (lang_section_start
, (name
, address
),
2450 CONST
char *name AND
2451 etree_type
* address
)
2453 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
2455 ad
->section_name
= name
;
2456 ad
->address
= address
;
2460 DEFUN (lang_add_entry
, (name
),
2463 entry_symbol
= name
;
2467 DEFUN (lang_add_target
, (name
),
2470 lang_target_statement_type
*new = new_stat (lang_target_statement
,
2478 DEFUN (lang_add_map
, (name
),
2486 map_option_f
= true;
2494 DEFUN (lang_add_fill
, (exp
),
2497 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
2504 DEFUN (lang_add_data
, (type
, exp
),
2506 union etree_union
*exp
)
2509 lang_data_statement_type
*new = new_stat (lang_data_statement
,
2518 DEFUN (lang_add_assignment
, (exp
),
2521 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
2528 DEFUN (lang_add_attribute
, (attribute
),
2529 enum statement_enum attribute
)
2531 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
2535 DEFUN (lang_startup
, (name
),
2538 if (startup_file
!= (char *) NULL
)
2540 einfo ("%P%FMultiple STARTUP files\n");
2542 first_file
->filename
= name
;
2543 first_file
->local_sym_name
= name
;
2545 startup_file
= name
;
2549 DEFUN (lang_float
, (maybe
),
2552 lang_float_flag
= maybe
;
2556 DEFUN (lang_leave_output_section_statement
, (fill
, memspec
),
2558 CONST
char *memspec
)
2560 current_section
->fill
= fill
;
2561 current_section
->region
= lang_memory_region_lookup (memspec
);
2562 stat_ptr
= &statement_list
;
2564 /* We remember if we are closing a .data section, since we use it to
2565 store constructors in */
2566 if (strcmp (current_section
->name
, ".data") == 0)
2568 end_of_data_section_statement_list
= statement_list
;
2574 Create an absolute symbol with the given name with the value of the
2575 address of first byte of the section named.
2577 If the symbol already exists, then do nothing.
2580 DEFUN (lang_abs_symbol_at_beginning_of
, (section
, name
),
2581 CONST
char *section AND
2584 if (ldsym_undefined (name
))
2586 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2587 asymbol
*def
= create_symbol (name
,
2588 BSF_GLOBAL
| BSF_EXPORT
,
2591 if (s
!= (asection
*) NULL
)
2593 def
->value
= s
->vma
;
2603 Create an absolute symbol with the given name with the value of the
2604 address of the first byte after the end of the section named.
2606 If the symbol already exists, then do nothing.
2609 DEFUN (lang_abs_symbol_at_end_of
, (section
, name
),
2610 CONST
char *section AND
2613 if (ldsym_undefined (name
))
2615 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2617 /* Add a symbol called _end */
2618 asymbol
*def
= create_symbol (name
,
2619 BSF_GLOBAL
| BSF_EXPORT
,
2622 if (s
!= (asection
*) NULL
)
2624 def
->value
= s
->vma
+ s
->_raw_size
;
2634 DEFUN (lang_statement_append
, (list
, element
, field
),
2635 lang_statement_list_type
* list AND
2636 lang_statement_union_type
* element AND
2637 lang_statement_union_type
** field
)
2639 *(list
->tail
) = element
;
2643 /* Set the output format type */
2645 DEFUN (lang_add_output_format
, (format
),
2648 output_target
= format
;