1 /* Linker command language support.
2 Copyright 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
36 static void print_statements
PARAMS ((void));
37 static void print_statement
PARAMS ((lang_statement_union_type
*,
38 lang_output_section_statement_type
*));
41 static struct obstack stat_obstack
;
43 #define obstack_chunk_alloc ldmalloc
44 #define obstack_chunk_free free
45 static CONST
char *startup_file
;
46 static lang_statement_list_type input_file_chain
;
48 /* Points to the last statement in the .data section, so we can add
49 stuff to the data section without pain */
50 static lang_statement_list_type end_of_data_section_statement_list
;
52 /* List of statements needed to handle constructors */
53 extern lang_statement_list_type constructor_list
;
55 static boolean placed_commons
= false;
56 static lang_output_section_statement_type
*default_common_section
;
57 static boolean map_option_f
;
58 static bfd_vma print_dot
;
59 static lang_input_statement_type
*first_file
;
60 static lang_statement_list_type lang_output_section_statement
;
61 static CONST
char *current_target
;
62 static CONST
char *output_target
;
63 static size_t longest_section_name
= 8;
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
;
99 etree_type
*base
; /* Relocation base - or null */
103 #define cat(a,b) a##b
105 #define cat(a,b) a/**/b
108 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
110 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
112 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
114 void lang_add_data
PARAMS ((int type
, union etree_union
* exp
));
117 DEFUN (stat_alloc
, (size
),
120 return obstack_alloc (&stat_obstack
, size
);
123 DEFUN (print_size
, (value
),
126 fprintf (config
.map_file
, "%5x", (unsigned) value
);
129 DEFUN (print_alignment
, (value
),
132 fprintf (config
.map_file
, "2**%1u", value
);
135 DEFUN (print_fill
, (value
),
138 fprintf (config
.map_file
, "%04x", (unsigned) value
);
143 DEFUN (print_section
, (name
),
144 CONST
char *CONST name
)
146 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
149 /*----------------------------------------------------------------------
150 lang_for_each_statement walks the parse tree and calls the provided
151 function for each node
155 DEFUN (lang_for_each_statement_worker
, (func
, s
),
157 lang_statement_union_type
* s
)
159 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
163 switch (s
->header
.type
)
165 case lang_constructors_statement_enum
:
166 lang_for_each_statement_worker (func
, constructor_list
.head
);
168 case lang_output_section_statement_enum
:
169 lang_for_each_statement_worker
171 s
->output_section_statement
.children
.head
);
173 case lang_wild_statement_enum
:
174 lang_for_each_statement_worker
176 s
->wild_statement
.children
.head
);
178 case lang_data_statement_enum
:
179 case lang_object_symbols_statement_enum
:
180 case lang_output_statement_enum
:
181 case lang_target_statement_enum
:
182 case lang_input_section_enum
:
183 case lang_input_statement_enum
:
184 case lang_assignment_statement_enum
:
185 case lang_padding_statement_enum
:
186 case lang_address_statement_enum
:
196 DEFUN (lang_for_each_statement
, (func
),
199 lang_for_each_statement_worker (func
,
200 statement_list
.head
);
203 /*----------------------------------------------------------------------*/
205 DEFUN (lang_list_init
, (list
),
206 lang_statement_list_type
* list
)
208 list
->head
= (lang_statement_union_type
*) NULL
;
209 list
->tail
= &list
->head
;
212 /*----------------------------------------------------------------------
214 build a new statement node for the parse tree
219 lang_statement_union_type
*
220 DEFUN (new_statement
, (type
, size
, list
),
221 enum statement_enum type AND
222 bfd_size_type size AND
223 lang_statement_list_type
* list
)
225 lang_statement_union_type
*new = (lang_statement_union_type
*)
228 new->header
.type
= type
;
229 new->header
.next
= (lang_statement_union_type
*) NULL
;
230 lang_statement_append (list
, new, &new->header
.next
);
235 Build a new input file node for the language. There are several ways
236 in which we treat an input file, eg, we only look at symbols, or
237 prefix it with a -l etc.
239 We can be supplied with requests for input files more than once;
240 they may, for example be split over serveral lines like foo.o(.text)
241 foo.o(.data) etc, so when asked for a file we check that we havn't
242 got it already so we don't duplicate the bfd.
245 static lang_input_statement_type
*
246 DEFUN (new_afile
, (name
, file_type
, target
),
247 CONST
char *CONST name AND
248 CONST lang_input_file_enum_type file_type AND
249 CONST
char *CONST target
)
252 lang_input_statement_type
*p
= new_stat (lang_input_statement
,
255 lang_has_input_file
= true;
257 p
->complained
= false;
260 case lang_input_file_is_symbols_only_enum
:
262 p
->is_archive
= false;
264 p
->local_sym_name
= name
;
265 p
->just_syms_flag
= true;
266 p
->search_dirs_flag
= false;
268 case lang_input_file_is_fake_enum
:
270 p
->is_archive
= false;
272 p
->local_sym_name
= name
;
273 p
->just_syms_flag
= false;
274 p
->search_dirs_flag
= false;
276 case lang_input_file_is_l_enum
:
277 p
->is_archive
= true;
280 p
->local_sym_name
= concat ("-l", name
, "");
281 p
->just_syms_flag
= false;
282 p
->search_dirs_flag
= true;
284 case lang_input_file_is_search_file_enum
:
285 case lang_input_file_is_marker_enum
:
287 p
->is_archive
= false;
289 p
->local_sym_name
= name
;
290 p
->just_syms_flag
= false;
291 p
->search_dirs_flag
= true;
293 case lang_input_file_is_file_enum
:
295 p
->is_archive
= false;
297 p
->local_sym_name
= name
;
298 p
->just_syms_flag
= false;
299 p
->search_dirs_flag
= false;
304 p
->asymbols
= (asymbol
**) NULL
;
305 p
->superfile
= (lang_input_statement_type
*) NULL
;
306 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
307 p
->next
= (lang_statement_union_type
*) NULL
;
309 p
->common_output_section
= (asection
*) NULL
;
310 lang_statement_append (&input_file_chain
,
311 (lang_statement_union_type
*) p
,
316 lang_input_statement_type
*
317 DEFUN (lang_add_input_file
, (name
, file_type
, target
),
319 lang_input_file_enum_type file_type AND
322 /* Look it up or build a new one */
323 lang_has_input_file
= true;
325 lang_input_statement_type
*p
;
327 for (p
= (lang_input_statement_type
*) input_file_chain
.head
;
328 p
!= (lang_input_statement_type
*) NULL
;
329 p
= (lang_input_statement_type
*) (p
->next_real_file
))
331 /* Sometimes we have incomplete entries in here */
332 if (p
->filename
!= (char *) NULL
)
334 if (strcmp (name
, p
->filename
) == 0)
340 return new_afile (name
, file_type
, target
);
344 DEFUN (lang_add_keepsyms_file
, (filename
),
345 CONST
char *filename
)
347 extern strip_symbols_type strip_symbols
;
348 if (keepsyms_file
!= 0)
349 info ("%X%P error: duplicated keep-symbols-file value\n");
350 keepsyms_file
= filename
;
351 if (strip_symbols
!= STRIP_NONE
)
352 info ("%P `-keep-only-symbols-file' overrides `-s' and `-S'\n");
353 strip_symbols
= STRIP_SOME
;
356 /* Build enough state so that the parser can build its tree */
358 DEFUN_VOID (lang_init
)
360 obstack_begin (&stat_obstack
, 1000);
362 stat_ptr
= &statement_list
;
364 lang_list_init (stat_ptr
);
366 lang_list_init (&input_file_chain
);
367 lang_list_init (&lang_output_section_statement
);
368 lang_list_init (&file_chain
);
369 first_file
= lang_add_input_file ((char *) NULL
,
370 lang_input_file_is_marker_enum
,
372 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
374 abs_output_section
->bfd_section
= &bfd_abs_section
;
378 /*----------------------------------------------------------------------
379 A region is an area of memory declared with the
380 MEMORY { name:org=exp, len=exp ... }
383 We maintain a list of all the regions here
385 If no regions are specified in the script, then the default is used
386 which is created when looked up to be the entire data space
389 static lang_memory_region_type
*lang_memory_region_list
;
390 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
392 lang_memory_region_type
*
393 DEFUN (lang_memory_region_lookup
, (name
),
394 CONST
char *CONST name
)
397 lang_memory_region_type
*p
= lang_memory_region_list
;
399 for (p
= lang_memory_region_list
;
400 p
!= (lang_memory_region_type
*) NULL
;
403 if (strcmp (p
->name
, name
) == 0)
408 if (strcmp (name
, "*default*") == 0)
410 /* This is the default region, dig out first one on the list */
411 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
413 return lang_memory_region_list
;
417 lang_memory_region_type
*new =
418 (lang_memory_region_type
*) stat_alloc ((bfd_size_type
) (sizeof (lang_memory_region_type
)));
420 new->name
= buystring (name
);
421 new->next
= (lang_memory_region_type
*) NULL
;
423 *lang_memory_region_list_tail
= new;
424 lang_memory_region_list_tail
= &new->next
;
428 new->had_full_message
= false;
435 lang_output_section_statement_type
*
436 DEFUN (lang_output_section_find
, (name
),
437 CONST
char *CONST name
)
439 lang_statement_union_type
*u
;
440 lang_output_section_statement_type
*lookup
;
442 for (u
= lang_output_section_statement
.head
;
443 u
!= (lang_statement_union_type
*) NULL
;
446 lookup
= &u
->output_section_statement
;
447 if (strcmp (name
, lookup
->name
) == 0)
452 return (lang_output_section_statement_type
*) NULL
;
455 lang_output_section_statement_type
*
456 DEFUN (lang_output_section_statement_lookup
, (name
),
457 CONST
char *CONST name
)
459 lang_output_section_statement_type
*lookup
;
461 lookup
= lang_output_section_find (name
);
462 if (lookup
== (lang_output_section_statement_type
*) NULL
)
465 lookup
= (lang_output_section_statement_type
*)
466 new_stat (lang_output_section_statement
, stat_ptr
);
467 lookup
->region
= (lang_memory_region_type
*) NULL
;
469 lookup
->block_value
= 1;
472 lookup
->next
= (lang_statement_union_type
*) NULL
;
473 lookup
->bfd_section
= (asection
*) NULL
;
474 lookup
->processed
= false;
475 lookup
->loadable
= 1;
476 lookup
->addr_tree
= (etree_type
*) NULL
;
477 lang_list_init (&lookup
->children
);
479 lang_statement_append (&lang_output_section_statement
,
480 (lang_statement_union_type
*) lookup
,
488 DEFUN (print_flags
, (ignore_flags
),
491 fprintf (config
.map_file
, "(");
493 if (flags
->flag_read
)
494 fprintf (outfile
, "R");
495 if (flags
->flag_write
)
496 fprintf (outfile
, "W");
497 if (flags
->flag_executable
)
498 fprintf (outfile
, "X");
499 if (flags
->flag_loadable
)
500 fprintf (outfile
, "L");
502 fprintf (config
.map_file
, ")");
506 DEFUN_VOID (lang_map
)
508 lang_memory_region_type
*m
;
510 fprintf (config
.map_file
, "**MEMORY CONFIGURATION**\n\n");
512 fprintf (config
.map_file
, "name\t\torigin\t\tlength\t\tattributes\n");
514 fprintf (config
.map_file
,
515 "name\t\torigin length r_size c_size is attributes\n");
518 for (m
= lang_memory_region_list
;
519 m
!= (lang_memory_region_type
*) NULL
;
522 fprintf (config
.map_file
, "%-16s", m
->name
);
523 print_address (m
->origin
);
525 print_address (m
->length
);
527 print_address (m
->old_length
);
529 print_address (m
->current
- m
->origin
);
532 fprintf(config
.map_file
," %2d%% ", ( m
->current
- m
->origin
) * 100 / m
->old_length
);
533 print_flags (&m
->flags
);
534 fprintf (config
.map_file
, "\n");
536 fprintf (config
.map_file
, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
537 fprintf (config
.map_file
, "output input virtual\n");
538 fprintf (config
.map_file
, "section section address tsize\n\n");
549 lang_output_section_statement_type
* s
)
551 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
552 section_userdata_type
*new =
553 (section_userdata_type
*)
554 stat_alloc ((bfd_size_type
) (sizeof (section_userdata_type
)));
556 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
557 if (s
->bfd_section
== (asection
*) NULL
)
558 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
559 if (s
->bfd_section
== (asection
*) NULL
)
561 einfo ("%P%F output format %s cannot represent section called %s\n",
562 output_bfd
->xvec
->name
, s
->name
);
564 s
->bfd_section
->output_section
= s
->bfd_section
;
565 /* s->bfd_section->flags = s->flags;*/
567 /* We initialize an output sections output offset to minus its own */
568 /* vma to allow us to output a section through itself */
569 s
->bfd_section
->output_offset
= 0;
570 get_userdata (s
->bfd_section
) = (PTR
) new;
574 /***********************************************************************
577 These expand statements like *(.text) and foo.o to a list of
578 explicit actions, like foo.o(.text), bar.o(.text) and
581 The toplevel routine, wild, takes a statement, section, file and
582 target. If either the section or file is null it is taken to be the
583 wildcard. Seperate lang_input_section statements are created for
584 each part of the expanstion, and placed after the statement provided.
589 DEFUN (wild_doit
, (ptr
, section
, output
, file
),
590 lang_statement_list_type
* ptr AND
591 asection
* section AND
592 lang_output_section_statement_type
* output AND
593 lang_input_statement_type
* file
)
595 if (output
->bfd_section
== (asection
*) NULL
)
598 /* Initialize the vma and size to the existing section. This will
599 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
601 if (section
!= (asection
*) NULL
)
603 bfd_set_section_vma (0, output
->bfd_section
,
604 bfd_section_vma (0, section
));
605 output
->bfd_section
->_raw_size
= section
->_raw_size
;
609 if (section
!= (asection
*) NULL
610 && section
->output_section
== (asection
*) NULL
)
612 /* Add a section reference to the list */
613 lang_input_section_type
*new = new_stat (lang_input_section
, ptr
);
615 new->section
= section
;
617 section
->output_section
= output
->bfd_section
;
619 /* Be selective about what the output section inherits from the
622 section
->output_section
->flags
|= section
->flags
& ~SEC_NEVER_LOAD
;
624 if (!output
->loadable
)
626 /* Turn off load flag */
627 output
->bfd_section
->flags
&= ~SEC_LOAD
;
628 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
630 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
632 output
->bfd_section
->alignment_power
= section
->alignment_power
;
634 /* If supplied an aligmnet, then force it */
635 if (output
->section_alignment
!= -1)
637 output
->bfd_section
->alignment_power
= output
->section_alignment
;
643 DEFUN (our_bfd_get_section_by_name
, (abfd
, section
),
647 return bfd_get_section_by_name (abfd
, section
);
651 DEFUN (wild_section
, (ptr
, section
, file
, output
),
652 lang_wild_statement_type
* ptr AND
653 CONST
char *section AND
654 lang_input_statement_type
* file AND
655 lang_output_section_statement_type
* output
)
659 if (file
->just_syms_flag
== false)
661 if (section
== (char *) NULL
)
663 /* Do the creation to all sections in the file */
664 for (s
= file
->the_bfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
667 if ((s
->flags
& SEC_IS_COMMON
) == 0)
669 wild_doit (&ptr
->children
, s
, output
, file
);
675 /* Do the creation to the named section only */
676 wild_doit (&ptr
->children
,
677 our_bfd_get_section_by_name (file
->the_bfd
, section
),
683 /* passed a file name (which must have been seen already and added to
684 the statement tree. We will see if it has been opened already and
685 had its symbols read. If not then we'll read it.
687 Archives are pecuilar here. We may open them once, but if they do
688 not define anything we need at the time, they won't have all their
689 symbols read. If we need them later, we'll have to redo it.
692 lang_input_statement_type
*
693 DEFUN (lookup_name
, (name
),
694 CONST
char *CONST name
)
696 lang_input_statement_type
*search
;
698 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
699 search
!= (lang_input_statement_type
*) NULL
;
700 search
= (lang_input_statement_type
*) search
->next_real_file
)
702 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
706 if (search
->filename
!= (char *) NULL
&& name
!= (char *) NULL
)
708 if (strcmp (search
->filename
, name
) == 0)
710 ldmain_open_file_read_symbol (search
);
716 /* There isn't an afile entry for this file yet, this must be
717 because the name has only appeared inside a load script and not
718 on the command line */
719 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
);
720 ldmain_open_file_read_symbol (search
);
727 DEFUN (wild
, (s
, section
, file
, target
, output
),
728 lang_wild_statement_type
* s AND
729 CONST
char *CONST section AND
730 CONST
char *CONST file AND
731 CONST
char *CONST target AND
732 lang_output_section_statement_type
* output
)
734 lang_input_statement_type
*f
;
736 if (file
== (char *) NULL
)
738 /* Perform the iteration over all files in the list */
739 for (f
= (lang_input_statement_type
*) file_chain
.head
;
740 f
!= (lang_input_statement_type
*) NULL
;
741 f
= (lang_input_statement_type
*) f
->next
)
743 wild_section (s
, section
, f
, output
);
745 /* Once more for the script file */
746 wild_section(s
, section
, script_file
, output
);
750 /* Perform the iteration over a single file */
751 wild_section (s
, section
, lookup_name (file
), output
);
753 if (section
!= (char *) NULL
754 && strcmp (section
, "COMMON") == 0
755 && default_common_section
== (lang_output_section_statement_type
*) NULL
)
757 /* Remember the section that common is going to incase we later
758 get something which doesn't know where to put it */
759 default_common_section
= output
;
764 read in all the files
767 DEFUN (open_output
, (name
),
768 CONST
char *CONST name
)
770 extern unsigned long ldfile_output_machine
;
771 extern enum bfd_architecture ldfile_output_architecture
;
773 extern CONST
char *output_filename
;
776 if (output_target
== (char *) NULL
)
778 if (current_target
!= (char *) NULL
)
779 output_target
= current_target
;
781 output_target
= default_target
;
783 output
= bfd_openw (name
, output_target
);
784 output_filename
= name
;
786 if (output
== (bfd
*) NULL
)
788 if (bfd_error
== invalid_target
)
790 einfo ("%P%F target %s not found\n", output_target
);
792 einfo ("%P%F problem opening output file %s, %E\n", name
);
795 /* output->flags |= D_PAGED;*/
797 bfd_set_format (output
, bfd_object
);
798 bfd_set_arch_mach (output
,
799 ldfile_output_architecture
,
800 ldfile_output_machine
);
808 DEFUN (ldlang_open_output
, (statement
),
809 lang_statement_union_type
* statement
)
811 switch (statement
->header
.type
)
813 case lang_output_statement_enum
:
814 output_bfd
= open_output (statement
->output_statement
.name
);
815 ldemul_set_output_arch ();
816 if (config
.magic_demand_paged
&& !config
.relocateable_output
)
817 output_bfd
->flags
|= D_PAGED
;
819 output_bfd
->flags
&= ~D_PAGED
;
820 if (config
.text_read_only
)
821 output_bfd
->flags
|= WP_TEXT
;
823 output_bfd
->flags
&= ~WP_TEXT
;
826 case lang_target_statement_enum
:
827 current_target
= statement
->target_statement
.target
;
835 DEFUN (open_input_bfds
, (statement
),
836 lang_statement_union_type
* statement
)
838 switch (statement
->header
.type
)
840 case lang_target_statement_enum
:
841 current_target
= statement
->target_statement
.target
;
843 case lang_wild_statement_enum
:
844 /* Maybe we should load the file's symbols */
845 if (statement
->wild_statement
.filename
)
847 (void) lookup_name (statement
->wild_statement
.filename
);
850 case lang_input_statement_enum
:
851 if (statement
->input_statement
.real
== true)
853 statement
->input_statement
.target
= current_target
;
854 lookup_name (statement
->input_statement
.filename
);
862 /* If there are [COMMONS] statements, put a wild one into the bss section */
865 lang_reasonable_defaults ()
871 lang_output_section_statement_lookup (".text");
872 lang_output_section_statement_lookup (".data");
874 default_common_section
=
875 lang_output_section_statement_lookup (".bss");
878 if (placed_commons
== false)
880 lang_wild_statement_type
*new =
881 new_stat (lang_wild_statement
,
882 &default_common_section
->children
);
884 new->section_name
= "COMMON";
885 new->filename
= (char *) NULL
;
886 lang_list_init (&new->children
);
893 Add the supplied name to the symbol table as an undefined reference.
894 Remove items from the chain as we open input bfds
896 typedef struct ldlang_undef_chain_list
898 struct ldlang_undef_chain_list
*next
;
900 } ldlang_undef_chain_list_type
;
902 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
905 DEFUN (ldlang_add_undef
, (name
),
906 CONST
char *CONST name
)
908 ldlang_undef_chain_list_type
*new =
909 (ldlang_undef_chain_list_type
910 *) stat_alloc ((bfd_size_type
) (sizeof (ldlang_undef_chain_list_type
)));
912 new->next
= ldlang_undef_chain_list_head
;
913 ldlang_undef_chain_list_head
= new;
915 new->name
= buystring (name
);
918 /* Run through the list of undefineds created above and place them
919 into the linker hash table as undefined symbols belonging to the
923 DEFUN_VOID (lang_place_undefineds
)
925 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
927 while (ptr
!= (ldlang_undef_chain_list_type
*) NULL
)
930 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
932 def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
934 def
->name
= ptr
->name
;
935 def
->section
= &bfd_und_section
;
936 Q_enter_global_ref (def_ptr
, ptr
->name
);
941 /* Copy important data from out internal form to the bfd way. Also
942 create a section for the dummy file
946 DEFUN_VOID (lang_create_output_section_statements
)
948 lang_statement_union_type
*os
;
950 for (os
= lang_output_section_statement
.head
;
951 os
!= (lang_statement_union_type
*) NULL
;
952 os
= os
->output_section_statement
.next
)
954 lang_output_section_statement_type
*s
=
955 &os
->output_section_statement
;
963 DEFUN_VOID (lang_init_script_file
)
965 script_file
= lang_add_input_file ("command line",
966 lang_input_file_is_fake_enum
,
968 script_file
->the_bfd
= bfd_create ("command line", output_bfd
);
969 script_file
->symbol_count
= 0;
970 script_file
->the_bfd
->sections
= 0;
972 /* The user data of a bfd points to the input statement attatched */
973 script_file
->the_bfd
->usrdata
= (void *)script_file
;
974 script_file
->common_section
=
975 bfd_make_section(script_file
->the_bfd
,"COMMON");
978 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
980 abs_output_section
->bfd_section
= &bfd_abs_section
;
984 /* Open input files and attatch to output sections */
986 DEFUN (map_input_to_output_sections
, (s
, target
, output_section_statement
),
987 lang_statement_union_type
* s AND
988 CONST
char *target AND
989 lang_output_section_statement_type
* output_section_statement
)
991 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
993 switch (s
->header
.type
)
997 case lang_wild_statement_enum
:
998 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
999 s
->wild_statement
.filename
, target
,
1000 output_section_statement
);
1003 case lang_constructors_statement_enum
:
1004 map_input_to_output_sections (constructor_list
.head
,
1006 output_section_statement
);
1008 case lang_output_section_statement_enum
:
1009 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1011 &s
->output_section_statement
);
1013 case lang_output_statement_enum
:
1015 case lang_target_statement_enum
:
1016 target
= s
->target_statement
.target
;
1018 case lang_fill_statement_enum
:
1019 case lang_input_section_enum
:
1020 case lang_object_symbols_statement_enum
:
1021 case lang_data_statement_enum
:
1022 case lang_assignment_statement_enum
:
1023 case lang_padding_statement_enum
:
1025 case lang_afile_asection_pair_statement_enum
:
1028 case lang_address_statement_enum
:
1029 /* Mark the specified section with the supplied address */
1031 lang_output_section_statement_type
*os
=
1032 lang_output_section_statement_lookup
1033 (s
->address_statement
.section_name
);
1035 os
->addr_tree
= s
->address_statement
.address
;
1036 if (os
->bfd_section
== (asection
*) NULL
)
1038 einfo ("%P%F can't set the address of undefined section %s\n",
1039 s
->address_statement
.section_name
);
1043 case lang_input_statement_enum
:
1044 /* A standard input statement, has no wildcards */
1045 /* ldmain_open_file_read_symbol(&s->input_statement);*/
1056 DEFUN (print_output_section_statement
, (output_section_statement
),
1057 lang_output_section_statement_type
* output_section_statement
)
1059 asection
*section
= output_section_statement
->bfd_section
;
1062 print_section (output_section_statement
->name
);
1067 print_dot
= section
->vma
;
1071 print_address (section
->vma
);
1073 print_size (section
->_raw_size
);
1075 print_size(section
->_cooked_size
);
1077 print_alignment (section
->alignment_power
);
1080 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1081 print_flags (stdout
, &output_section_statement
->flags
);
1083 if (section
->flags
& SEC_LOAD
)
1084 fprintf (config
.map_file
, "load ");
1085 if (section
->flags
& SEC_ALLOC
)
1086 fprintf (config
.map_file
, "alloc ");
1087 if (section
->flags
& SEC_RELOC
)
1088 fprintf (config
.map_file
, "reloc ");
1089 if (section
->flags
& SEC_HAS_CONTENTS
)
1090 fprintf (config
.map_file
, "contents ");
1095 fprintf (config
.map_file
, "No attached output section");
1098 if (output_section_statement
->load_base
)
1100 int b
= exp_get_value_int(output_section_statement
->load_base
,
1101 0, "output base", lang_final_phase_enum
);
1102 printf("Output address %08x\n", b
);
1104 if (output_section_statement
->section_alignment
>= 0
1105 || output_section_statement
->section_alignment
>= 0)
1107 printf("\t\t\t\t\tforced alignment ");
1108 if ( output_section_statement
->section_alignment
>= 0)
1110 printf("section 2**%d ",output_section_statement
->section_alignment
);
1112 if ( output_section_statement
->subsection_alignment
>= 0)
1114 printf("subsection 2**%d ",output_section_statement
->subsection_alignment
);
1119 print_statement (output_section_statement
->children
.head
,
1120 output_section_statement
);
1125 DEFUN (print_assignment
, (assignment
, output_section
),
1126 lang_assignment_statement_type
* assignment AND
1127 lang_output_section_statement_type
* output_section
)
1129 etree_value_type result
;
1135 print_address (print_dot
);
1137 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1139 lang_final_phase_enum
,
1145 print_address (result
.value
);
1149 fprintf (config
.map_file
, "*undefined*");
1152 exp_print_tree (assignment
->exp
);
1154 fprintf (config
.map_file
, "\n");
1158 DEFUN (print_input_statement
, (statm
),
1159 lang_input_statement_type
* statm
)
1161 if (statm
->filename
!= (char *) NULL
)
1163 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1168 DEFUN (print_symbol
, (q
),
1172 fprintf (config
.map_file
, " ");
1174 fprintf (config
.map_file
, " ");
1175 print_address (outside_symbol_address (q
));
1176 fprintf (config
.map_file
, " %s", q
->name
? q
->name
: " ");
1181 DEFUN (print_input_section
, (in
),
1182 lang_input_section_type
* in
)
1184 asection
*i
= in
->section
;
1185 int size
= i
->reloc_done
?
1186 bfd_get_section_size_after_reloc (i
) :
1187 bfd_get_section_size_before_reloc (i
);
1192 fprintf (config
.map_file
, " ");
1193 print_section (i
->name
);
1194 fprintf (config
.map_file
, " ");
1195 if (i
->output_section
)
1197 print_address (i
->output_section
->vma
+ i
->output_offset
);
1198 fprintf (config
.map_file
, " ");
1199 print_size (i
->_raw_size
);
1200 fprintf (config
.map_file
, " ");
1201 print_size(i
->_cooked_size
);
1202 fprintf (config
.map_file
, " ");
1203 print_alignment (i
->alignment_power
);
1204 fprintf (config
.map_file
, " ");
1208 bfd
*abfd
= in
->ifile
->the_bfd
;
1210 if (in
->ifile
->just_syms_flag
== true)
1212 fprintf (config
.map_file
, "symbols only ");
1215 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1216 if (abfd
->my_archive
!= (bfd
*) NULL
)
1218 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1223 fprintf (config
.map_file
, "%s", abfd
->filename
);
1225 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1228 /* Find all the symbols in this file defined in this section */
1230 if (in
->ifile
->symbol_count
)
1234 for (p
= in
->ifile
->asymbols
; *p
; p
++)
1238 if (bfd_get_section (q
) == i
&& q
->flags
& BSF_GLOBAL
)
1251 print_dot
= outside_section_address (i
) + size
;
1255 fprintf (config
.map_file
, "No output section allocated\n");
1261 DEFUN (print_fill_statement
, (fill
),
1262 lang_fill_statement_type
* fill
)
1264 fprintf (config
.map_file
, "FILL mask ");
1265 print_fill (fill
->fill
);
1269 DEFUN (print_data_statement
, (data
),
1270 lang_data_statement_type
* data
)
1272 /* bfd_vma value; */
1277 /* ASSERT(print_dot == data->output_vma);*/
1279 print_address (data
->output_vma
+ data
->output_section
->vma
);
1281 print_address (data
->value
);
1286 fprintf (config
.map_file
, "BYTE ");
1287 print_dot
+= BYTE_SIZE
;
1290 fprintf (config
.map_file
, "SHORT ");
1291 print_dot
+= SHORT_SIZE
;
1294 fprintf (config
.map_file
, "LONG ");
1295 print_dot
+= LONG_SIZE
;
1299 exp_print_tree (data
->exp
);
1301 fprintf (config
.map_file
, "\n");
1306 DEFUN (print_padding_statement
, (s
),
1307 lang_padding_statement_type
* s
)
1311 print_section ("*fill*");
1313 print_address (s
->output_offset
+ s
->output_section
->vma
);
1315 print_size (s
->size
);
1317 print_fill (s
->fill
);
1320 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1325 DEFUN (print_wild_statement
, (w
, os
),
1326 lang_wild_statement_type
* w AND
1327 lang_output_section_statement_type
* os
)
1329 fprintf (config
.map_file
, " from ");
1330 if (w
->filename
!= (char *) NULL
)
1332 fprintf (config
.map_file
, "%s", w
->filename
);
1336 fprintf (config
.map_file
, "*");
1338 if (w
->section_name
!= (char *) NULL
)
1340 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1344 fprintf (config
.map_file
, "(*)");
1347 print_statement (w
->children
.head
, os
);
1351 DEFUN (print_statement
, (s
, os
),
1352 lang_statement_union_type
* s AND
1353 lang_output_section_statement_type
* os
)
1357 switch (s
->header
.type
)
1359 case lang_constructors_statement_enum
:
1360 fprintf (config
.map_file
, "constructors:\n");
1361 print_statement (constructor_list
.head
, os
);
1363 case lang_wild_statement_enum
:
1364 print_wild_statement (&s
->wild_statement
, os
);
1367 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1370 case lang_address_statement_enum
:
1371 fprintf (config
.map_file
, "address\n");
1373 case lang_object_symbols_statement_enum
:
1374 fprintf (config
.map_file
, "object symbols\n");
1376 case lang_fill_statement_enum
:
1377 print_fill_statement (&s
->fill_statement
);
1379 case lang_data_statement_enum
:
1380 print_data_statement (&s
->data_statement
);
1382 case lang_input_section_enum
:
1383 print_input_section (&s
->input_section
);
1385 case lang_padding_statement_enum
:
1386 print_padding_statement (&s
->padding_statement
);
1388 case lang_output_section_statement_enum
:
1389 print_output_section_statement (&s
->output_section_statement
);
1391 case lang_assignment_statement_enum
:
1392 print_assignment (&s
->assignment_statement
,
1395 case lang_target_statement_enum
:
1396 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1398 case lang_output_statement_enum
:
1399 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1400 s
->output_statement
.name
,
1401 output_target
? output_target
: "");
1403 case lang_input_statement_enum
:
1404 print_input_statement (&s
->input_statement
);
1406 case lang_afile_asection_pair_statement_enum
:
1416 DEFUN_VOID (print_statements
)
1418 print_statement (statement_list
.head
,
1419 abs_output_section
);
1424 DEFUN (insert_pad
, (this_ptr
, fill
, power
, output_section_statement
, dot
),
1425 lang_statement_union_type
** this_ptr AND
1427 unsigned int power AND
1428 asection
* output_section_statement AND
1431 /* Align this section first to the
1432 input sections requirement, then
1433 to the output section's requirement.
1434 If this alignment is > than any seen before,
1435 then record it too. Perform the alignment by
1436 inserting a magic 'padding' statement.
1439 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1441 if (alignment_needed
!= 0)
1443 lang_statement_union_type
*new =
1444 (lang_statement_union_type
*)
1445 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1447 /* Link into existing chain */
1448 new->header
.next
= *this_ptr
;
1450 new->header
.type
= lang_padding_statement_enum
;
1451 new->padding_statement
.output_section
= output_section_statement
;
1452 new->padding_statement
.output_offset
=
1453 dot
- output_section_statement
->vma
;
1454 new->padding_statement
.fill
= fill
;
1455 new->padding_statement
.size
= alignment_needed
;
1459 /* Remember the most restrictive alignment */
1460 if (power
> output_section_statement
->alignment_power
)
1462 output_section_statement
->alignment_power
= power
;
1464 output_section_statement
->_raw_size
+= alignment_needed
;
1465 return alignment_needed
+ dot
;
1469 /* Work out how much this section will move the dot point */
1471 DEFUN (size_input_section
, (this_ptr
, output_section_statement
, fill
,
1473 lang_statement_union_type
** this_ptr AND
1474 lang_output_section_statement_type
* output_section_statement AND
1475 unsigned short fill AND
1479 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1480 asection
*i
= is
->section
;
1482 if (is
->ifile
->just_syms_flag
== false)
1484 if (output_section_statement
->subsection_alignment
!= -1)
1485 i
->alignment_power
=
1486 output_section_statement
->subsection_alignment
;
1488 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1489 output_section_statement
->bfd_section
, dot
);
1491 /* remember the largest size so we can malloc the largest area
1492 needed for the output stage. Only remember the size of sections
1493 which we will actually allocate */
1495 (SEC_HAS_CONTENTS
| SEC_ALLOC
)) == (SEC_HAS_CONTENTS
| SEC_ALLOC
))
1496 && (bfd_get_section_size_before_reloc (i
) > largest_section
))
1498 largest_section
= bfd_get_section_size_before_reloc (i
);
1501 /* Remember where in the output section this input section goes */
1503 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1505 /* Mark how big the output section must be to contain this now
1509 dot
+= i
->_cooked_size
;
1513 dot
+= i
->_raw_size
;
1515 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1519 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1525 /* Sizing happens in two passes, first pass we allocate worst case
1526 stuff. The second pass (if relaxing), we use what we learnt to
1527 change the size of some relocs from worst case to better
1529 static boolean had_relax
;
1532 DEFUN (lang_size_sections
, (s
, output_section_statement
, prev
, fill
,
1534 lang_statement_union_type
* s AND
1535 lang_output_section_statement_type
* output_section_statement AND
1536 lang_statement_union_type
** prev AND
1537 unsigned short fill AND
1541 /* Size up the sections from their constituent parts */
1542 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1544 switch (s
->header
.type
)
1547 case lang_output_section_statement_enum
:
1550 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1552 /* If this is a shared library section, don't change the size
1554 if (os
->bfd_section
->flags
& SEC_SHARED_LIBRARY
)
1557 if (os
->bfd_section
== &bfd_abs_section
)
1559 /* No matter what happens, an abs section starts at zero */
1560 bfd_set_section_vma (0, os
->bfd_section
, 0);
1564 if (os
->addr_tree
== (etree_type
*) NULL
)
1566 /* No address specified for this section, get one
1567 from the region specification
1569 if (os
->region
== (lang_memory_region_type
*) NULL
)
1571 os
->region
= lang_memory_region_lookup ("*default*");
1573 dot
= os
->region
->current
;
1579 r
= exp_fold_tree (os
->addr_tree
,
1581 lang_allocating_phase_enum
,
1583 if (r
.valid
== false)
1585 einfo ("%F%S: non constant address expression for section %s\n",
1590 /* The section starts here */
1591 /* First, align to what the section needs */
1594 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1595 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1597 if (os
->load_base
) {
1598 os
->bfd_section
->lma
1599 = exp_get_value_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
1604 os
->bfd_section
->output_offset
= 0;
1606 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1607 os
->fill
, dot
, relax
);
1608 /* Ignore the size of the input sections, use the vma and size to */
1612 after
= ALIGN (os
->bfd_section
->vma
+
1613 os
->bfd_section
->_raw_size
,
1617 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1618 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1619 os
->processed
= true;
1621 /* Replace into region ? */
1622 if (os
->addr_tree
== (etree_type
*) NULL
1623 && os
->region
!= (lang_memory_region_type
*) NULL
)
1625 os
->region
->current
= dot
;
1626 /* Make sure this isn't silly */
1627 if (( os
->region
->current
1628 > os
->region
->origin
+ os
->region
->length
)
1629 || ( os
->region
->origin
> os
->region
->current
))
1631 einfo ("%X%P: Region %s is full (%B section %s)\n",
1633 os
->bfd_section
->owner
,
1634 os
->bfd_section
->name
);
1635 /* Reset the region pointer */
1636 os
->region
->current
= 0;
1644 case lang_constructors_statement_enum
:
1645 dot
= lang_size_sections (constructor_list
.head
,
1646 output_section_statement
,
1647 &s
->wild_statement
.children
.head
,
1652 case lang_data_statement_enum
:
1654 unsigned int size
= 0;
1656 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1657 s
->data_statement
.output_section
=
1658 output_section_statement
->bfd_section
;
1660 switch (s
->data_statement
.type
)
1674 output_section_statement
->bfd_section
->_raw_size
+= size
;
1678 case lang_wild_statement_enum
:
1680 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
1681 output_section_statement
,
1682 &s
->wild_statement
.children
.head
,
1688 case lang_object_symbols_statement_enum
:
1689 create_object_symbols
= output_section_statement
;
1691 case lang_output_statement_enum
:
1692 case lang_target_statement_enum
:
1694 case lang_input_section_enum
:
1699 if( relax_section (prev
))
1705 (*prev
)->input_section
.section
->_cooked_size
=
1706 (*prev
)->input_section
.section
->_raw_size
;
1709 dot
= size_input_section (prev
,
1710 output_section_statement
,
1711 output_section_statement
->fill
,
1714 case lang_input_statement_enum
:
1716 case lang_fill_statement_enum
:
1717 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
1719 fill
= s
->fill_statement
.fill
;
1721 case lang_assignment_statement_enum
:
1723 bfd_vma newdot
= dot
;
1725 exp_fold_tree (s
->assignment_statement
.exp
,
1726 output_section_statement
,
1727 lang_allocating_phase_enum
,
1731 if (newdot
!= dot
&& !relax
)
1732 /* We've been moved ! so insert a pad */
1734 lang_statement_union_type
*new =
1735 (lang_statement_union_type
*)
1736 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1738 /* Link into existing chain */
1739 new->header
.next
= *prev
;
1741 new->header
.type
= lang_padding_statement_enum
;
1742 new->padding_statement
.output_section
=
1743 output_section_statement
->bfd_section
;
1744 new->padding_statement
.output_offset
=
1745 dot
- output_section_statement
->bfd_section
->vma
;
1746 new->padding_statement
.fill
= fill
;
1747 new->padding_statement
.size
= newdot
- dot
;
1748 output_section_statement
->bfd_section
->_raw_size
+=
1749 new->padding_statement
.size
;
1758 /* This can only get here when relaxing is turned on */
1759 case lang_padding_statement_enum
:
1761 case lang_address_statement_enum
:
1764 prev
= &s
->header
.next
;
1770 DEFUN (lang_do_assignments
, (s
, output_section_statement
, fill
, dot
),
1771 lang_statement_union_type
* s AND
1772 lang_output_section_statement_type
* output_section_statement AND
1773 unsigned short fill AND
1777 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1779 switch (s
->header
.type
)
1781 case lang_constructors_statement_enum
:
1782 dot
= lang_do_assignments (constructor_list
.head
,
1783 output_section_statement
,
1788 case lang_output_section_statement_enum
:
1790 lang_output_section_statement_type
*os
=
1791 &(s
->output_section_statement
);
1793 dot
= os
->bfd_section
->vma
;
1794 (void) lang_do_assignments (os
->children
.head
, os
, os
->fill
, dot
);
1795 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1798 case lang_wild_statement_enum
:
1800 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
1801 output_section_statement
,
1806 case lang_object_symbols_statement_enum
:
1807 case lang_output_statement_enum
:
1808 case lang_target_statement_enum
:
1810 case lang_common_statement_enum
:
1813 case lang_data_statement_enum
:
1815 etree_value_type value
;
1817 value
= exp_fold_tree (s
->data_statement
.exp
,
1819 lang_final_phase_enum
, dot
, &dot
);
1820 s
->data_statement
.value
= value
.value
;
1821 if (value
.valid
== false)
1822 einfo ("%F%P: Invalid data statement\n");
1824 switch (s
->data_statement
.type
)
1837 case lang_input_section_enum
:
1839 asection
*in
= s
->input_section
.section
;
1841 dot
+= bfd_get_section_size_before_reloc (in
);
1845 case lang_input_statement_enum
:
1847 case lang_fill_statement_enum
:
1848 fill
= s
->fill_statement
.fill
;
1850 case lang_assignment_statement_enum
:
1852 exp_fold_tree (s
->assignment_statement
.exp
,
1853 output_section_statement
,
1854 lang_final_phase_enum
,
1860 case lang_padding_statement_enum
:
1861 dot
+= s
->padding_statement
.size
;
1866 case lang_address_statement_enum
:
1877 DEFUN_VOID (lang_relocate_globals
)
1881 Each ldsym_type maintains a chain of pointers to asymbols which
1882 references the definition. Replace each pointer to the referenence
1883 with a pointer to only one place, preferably the definition. If
1884 the defintion isn't available then the common symbol, and if
1885 there isn't one of them then choose one reference.
1888 FOR_EACH_LDSYM (lgs
)
1892 if (lgs
->sdefs_chain
)
1894 it
= *(lgs
->sdefs_chain
);
1896 else if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
1898 it
= *(lgs
->scoms_chain
);
1900 else if (lgs
->srefs_chain
!= (asymbol
**) NULL
)
1902 it
= *(lgs
->srefs_chain
);
1906 /* This can happen when the command line asked for a symbol to
1908 it
= (asymbol
*) NULL
;
1910 if (it
!= (asymbol
*) NULL
)
1913 asymbol
**ptr
= lgs
->srefs_chain
;;
1914 if (lgs
->flags
& SYM_WARNING
)
1916 produce_warnings (lgs
, it
);
1919 while (ptr
!= (asymbol
**) NULL
1922 asymbol
*ref
= *ptr
;
1925 ptr
= (asymbol
**) (ref
->udata
);
1934 DEFUN_VOID (lang_finish
)
1937 int warn
= config
.relocateable_output
!= true;
1938 if (entry_symbol
== (char *) NULL
)
1940 /* No entry has been specified, look for start, but don't warn */
1941 entry_symbol
= "start";
1944 lgs
= ldsym_get_soft (entry_symbol
);
1945 if (lgs
&& lgs
->sdefs_chain
)
1947 asymbol
*sy
= *(lgs
->sdefs_chain
);
1949 /* We can set the entry address*/
1950 bfd_set_start_address (output_bfd
,
1951 outside_symbol_address (sy
));
1956 /* Can't find anything reasonable,
1957 use the first address in the text section
1959 asection
*ts
= bfd_get_section_by_name (output_bfd
, ".text");
1963 einfo ("%P: Warning, can't find entry symbol %s, defaulting to %V\n",
1964 entry_symbol
, ts
->vma
);
1966 bfd_set_start_address (output_bfd
, ts
->vma
);
1971 einfo ("%P: Warning, can't find entry symbol %s, not setting start address\n",
1977 /* By now we know the target architecture, and we may have an */
1978 /* ldfile_output_machine_name */
1980 DEFUN_VOID (lang_check
)
1982 lang_statement_union_type
*file
;
1984 unsigned long input_machine
;
1985 enum bfd_architecture input_architecture
;
1986 CONST bfd_arch_info_type
*compatible
;
1988 for (file
= file_chain
.head
;
1989 file
!= (lang_statement_union_type
*) NULL
;
1990 file
= file
->input_statement
.next
)
1992 input_bfd
= file
->input_statement
.the_bfd
;
1994 input_machine
= bfd_get_mach (input_bfd
);
1995 input_architecture
= bfd_get_arch (input_bfd
);
1998 /* Inspect the architecture and ensure we're linking like with
2001 compatible
= bfd_arch_get_compatible (input_bfd
,
2006 ldfile_output_machine
= compatible
->mach
;
2007 ldfile_output_architecture
= compatible
->arch
;
2012 info ("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
2013 bfd_printable_name (input_bfd
), input_bfd
,
2014 bfd_printable_name (output_bfd
));
2016 bfd_set_arch_mach (output_bfd
,
2025 * run through all the global common symbols and tie them
2026 * to the output section requested.
2028 As an experiment we do this 4 times, once for all the byte sizes,
2029 then all the two bytes, all the four bytes and then everything else
2033 DEFUN_VOID (lang_common
)
2038 if (config
.relocateable_output
== false ||
2039 command_line
.force_common_definition
== true)
2041 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<= 1)
2043 for (lgs
= symbol_head
;
2044 lgs
!= (ldsym_type
*) NULL
;
2048 unsigned int power_of_two
;
2052 if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
2054 com
= *(lgs
->scoms_chain
);
2084 if (config
.sort_common
== false || align
== power
)
2088 /* Change from a common symbol into a definition of
2090 lgs
->sdefs_chain
= lgs
->scoms_chain
;
2091 lgs
->scoms_chain
= (asymbol
**) NULL
;
2094 /* Point to the correct common section */
2095 symbfd
= bfd_asymbol_bfd (com
);
2096 if (com
->section
== &bfd_com_section
)
2098 ((lang_input_statement_type
*) symbfd
->usrdata
)
2105 name
= bfd_get_section_name (symbfd
,
2107 newsec
= bfd_get_section_by_name (symbfd
,
2109 /* BFD backend must provide this section. */
2110 if (newsec
== (asection
*) NULL
)
2111 einfo ("%P%F: No output section %s", name
);
2112 com
->section
= newsec
;
2115 /* Fix the size of the common section */
2117 com
->section
->_raw_size
=
2118 ALIGN (com
->section
->_raw_size
, align
);
2120 /* Remember if this is the biggest alignment ever seen */
2121 if (power_of_two
> com
->section
->alignment_power
)
2123 com
->section
->alignment_power
= power_of_two
;
2126 /* Symbol stops being common and starts being global, but
2127 we remember that it was common once. */
2129 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
2130 com
->value
= com
->section
->_raw_size
;
2132 if (write_map
&& config
.map_file
)
2134 fprintf (config
.map_file
, "Allocating common %s: %x at %x %s\n",
2137 (unsigned) com
->value
,
2138 bfd_asymbol_bfd(com
)->filename
);
2141 com
->section
->_raw_size
+= size
;
2154 run through the input files and ensure that every input
2155 section has somewhere to go. If one is found without
2156 a destination then create an input request and place it
2157 into the statement tree.
2161 DEFUN_VOID (lang_place_orphans
)
2163 lang_input_statement_type
*file
;
2165 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2166 file
!= (lang_input_statement_type
*) NULL
;
2167 file
= (lang_input_statement_type
*) file
->next
)
2171 for (s
= file
->the_bfd
->sections
;
2172 s
!= (asection
*) NULL
;
2175 if (s
->output_section
== (asection
*) NULL
)
2177 /* This section of the file is not attatched, root
2178 around for a sensible place for it to go */
2180 if (file
->common_section
== s
)
2182 /* This is a lonely common section which must
2183 have come from an archive. We attatch to the
2184 section with the wildcard */
2185 if (config
.relocateable_output
!= true
2186 && command_line
.force_common_definition
== false)
2188 if (default_common_section
==
2189 (lang_output_section_statement_type
*) NULL
)
2191 info ("%P: No [COMMON] command, defaulting to .bss\n");
2193 default_common_section
=
2194 lang_output_section_statement_lookup (".bss");
2197 wild_doit (&default_common_section
->children
, s
,
2198 default_common_section
, file
);
2203 lang_output_section_statement_type
*os
=
2204 lang_output_section_statement_lookup (s
->name
);
2206 wild_doit (&os
->children
, s
, os
, file
);
2215 DEFUN (lang_set_flags
, (ptr
, flags
),
2219 boolean state
= false;
2234 /* ptr->flag_read = state; */
2237 /* ptr->flag_write = state; */
2240 /* ptr->flag_executable= state;*/
2244 /* ptr->flag_loadable= state;*/
2247 einfo ("%P%F illegal syntax in flags\n");
2257 DEFUN (lang_for_each_file
, (func
),
2258 void (*func
) PARAMS ((lang_input_statement_type
*)))
2260 lang_input_statement_type
*f
;
2262 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2263 f
!= (lang_input_statement_type
*) NULL
;
2264 f
= (lang_input_statement_type
*) f
->next
)
2272 DEFUN (lang_for_each_input_section
, (func
),
2273 void (*func
) PARAMS ((bfd
* ab
, asection
* as
)))
2275 lang_input_statement_type
*f
;
2277 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2278 f
!= (lang_input_statement_type
*) NULL
;
2279 f
= (lang_input_statement_type
*) f
->next
)
2283 for (s
= f
->the_bfd
->sections
;
2284 s
!= (asection
*) NULL
;
2287 func (f
->the_bfd
, s
);
2295 DEFUN (ldlang_add_file
, (entry
),
2296 lang_input_statement_type
* entry
)
2299 lang_statement_append (&file_chain
,
2300 (lang_statement_union_type
*) entry
,
2305 DEFUN (lang_add_output
, (name
),
2308 lang_output_statement_type
*new = new_stat (lang_output_statement
,
2312 had_output_filename
= true;
2316 static lang_output_section_statement_type
*current_section
;
2318 static int topower(x
)
2323 if (x
< 0) return -1;
2324 for (l
= 0; l
< 32; l
++)
2326 if (i
>= x
) return l
;
2332 DEFUN (lang_enter_output_section_statement
,
2333 (output_section_statement_name
,
2337 align
, subalign
, base
),
2338 char *output_section_statement_name AND
2339 etree_type
* address_exp AND
2341 bfd_vma block_value AND
2342 etree_type
*align AND
2343 etree_type
*subalign AND
2346 lang_output_section_statement_type
*os
;
2350 lang_output_section_statement_lookup (output_section_statement_name
);
2354 /* Add this statement to tree */
2355 /* add_statement(lang_output_section_statement_enum,
2356 output_section_statement);*/
2357 /* Make next things chain into subchain of this */
2359 if (os
->addr_tree
==
2360 (etree_type
*) NULL
)
2366 if (flags
& SEC_NEVER_LOAD
)
2370 os
->block_value
= block_value
? block_value
: 1;
2371 stat_ptr
= &os
->children
;
2373 os
->subsection_alignment
= topower(
2374 exp_get_value_int(subalign
, -1,
2375 "subsection alignment",
2377 os
->section_alignment
= topower(
2378 exp_get_value_int(align
, -1,
2379 "section alignment", 0));
2381 os
->load_base
= base
;
2386 DEFUN_VOID (lang_final
)
2388 if (had_output_filename
== false)
2390 extern CONST
char *output_filename
;
2392 lang_add_output (output_filename
);
2396 /* Reset the current counters in the regions */
2398 DEFUN_VOID (reset_memory_regions
)
2400 lang_memory_region_type
*p
= lang_memory_region_list
;
2402 for (p
= lang_memory_region_list
;
2403 p
!= (lang_memory_region_type
*) NULL
;
2406 p
->old_length
= p
->current
- p
->origin
;
2407 p
->current
= p
->origin
;
2414 DEFUN (create_symbol
, (name
, flags
, section
),
2415 CONST
char *name AND
2419 extern lang_input_statement_type
*script_file
;
2420 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
2422 /* Add this definition to script file */
2423 asymbol
*def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
2424 def
->name
= buystring (name
);
2427 def
->section
= section
;
2429 Q_enter_global_ref (def_ptr
, name
);
2434 DEFUN_VOID (lang_process
)
2437 if (had_script
== false)
2439 parse_line (ldemul_get_script (), 1);
2441 lang_reasonable_defaults ();
2442 current_target
= default_target
;
2444 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2445 /* For each output section statement, create a section in the output
2447 lang_create_output_section_statements ();
2449 /* Create a dummy bfd for the script */
2450 lang_init_script_file ();
2452 /* Add to the hash table all undefineds on the command line */
2453 lang_place_undefineds ();
2455 /* Create a bfd for each input file */
2456 current_target
= default_target
;
2457 lang_for_each_statement (open_input_bfds
);
2459 /* Run through the contours of the script and attatch input sections
2460 to the correct output sections
2462 find_constructors ();
2463 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2464 (lang_output_section_statement_type
*) NULL
);
2467 /* Find any sections not attatched explicitly and handle them */
2468 lang_place_orphans ();
2470 /* Size up the common data */
2473 ldemul_before_allocation ();
2483 lang_size_sections (statement_list
.head
,
2484 (lang_output_section_statement_type
*) NULL
,
2485 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2486 /* FIXME. Until the code in relax is fixed so that it only reads in
2487 stuff once, we cant iterate since there is no way for the linker to
2488 know what has been patched and what hasn't */
2494 /* Now run around and relax if we can */
2495 if (command_line
.relax
)
2497 /* First time round is a trial run to get the 'worst case' addresses of the
2498 objects if there was no relaxing */
2499 lang_size_sections (statement_list
.head
,
2500 (lang_output_section_statement_type
*) NULL
,
2501 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2505 /* Move the global symbols around so the second pass of relaxing can
2507 lang_relocate_globals ();
2509 reset_memory_regions ();
2511 /* Do all the assignments, now that we know the final restingplaces
2512 of all the symbols */
2514 lang_do_assignments (statement_list
.head
,
2519 /* Perform another relax pass - this time we know where the
2520 globals are, so can make better guess */
2521 lang_size_sections (statement_list
.head
,
2522 (lang_output_section_statement_type
*) NULL
,
2523 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2531 /* Size up the sections */
2532 lang_size_sections (statement_list
.head
,
2534 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2539 /* See if anything special should be done now we know how big
2541 ldemul_after_allocation ();
2543 /* Do all the assignments, now that we know the final restingplaces
2544 of all the symbols */
2546 lang_do_assignments (statement_list
.head
,
2551 /* Move the global symbols around */
2552 lang_relocate_globals ();
2554 /* Make sure that we're not mixing architectures */
2562 /* EXPORTED TO YACC */
2565 DEFUN (lang_add_wild
, (section_name
, filename
),
2566 CONST
char *CONST section_name AND
2567 CONST
char *CONST filename
)
2569 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
2572 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
2574 placed_commons
= true;
2576 if (filename
!= (char *) NULL
)
2578 lang_has_input_file
= true;
2580 new->section_name
= section_name
;
2581 new->filename
= filename
;
2582 lang_list_init (&new->children
);
2586 DEFUN (lang_section_start
, (name
, address
),
2587 CONST
char *name AND
2588 etree_type
* address
)
2590 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
2592 ad
->section_name
= name
;
2593 ad
->address
= address
;
2597 DEFUN (lang_add_entry
, (name
),
2600 entry_symbol
= name
;
2604 DEFUN (lang_add_target
, (name
),
2607 lang_target_statement_type
*new = new_stat (lang_target_statement
,
2615 DEFUN (lang_add_map
, (name
),
2623 map_option_f
= true;
2631 DEFUN (lang_add_fill
, (exp
),
2634 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
2641 DEFUN (lang_add_data
, (type
, exp
),
2643 union etree_union
*exp
)
2646 lang_data_statement_type
*new = new_stat (lang_data_statement
,
2655 DEFUN (lang_add_assignment
, (exp
),
2658 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
2665 DEFUN (lang_add_attribute
, (attribute
),
2666 enum statement_enum attribute
)
2668 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
2672 DEFUN (lang_startup
, (name
),
2675 if (startup_file
!= (char *) NULL
)
2677 einfo ("%P%FMultiple STARTUP files\n");
2679 first_file
->filename
= name
;
2680 first_file
->local_sym_name
= name
;
2682 startup_file
= name
;
2686 DEFUN (lang_float
, (maybe
),
2689 lang_float_flag
= maybe
;
2693 DEFUN (lang_leave_output_section_statement
, (fill
, memspec
),
2695 CONST
char *memspec
)
2697 current_section
->fill
= fill
;
2698 current_section
->region
= lang_memory_region_lookup (memspec
);
2699 stat_ptr
= &statement_list
;
2701 /* We remember if we are closing a .data section, since we use it to
2702 store constructors in */
2703 if (strcmp (current_section
->name
, ".data") == 0)
2705 end_of_data_section_statement_list
= statement_list
;
2711 Create an absolute symbol with the given name with the value of the
2712 address of first byte of the section named.
2714 If the symbol already exists, then do nothing.
2717 DEFUN (lang_abs_symbol_at_beginning_of
, (section
, name
),
2718 CONST
char *section AND
2721 if (ldsym_undefined (name
))
2723 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2724 asymbol
*def
= create_symbol (name
,
2725 BSF_GLOBAL
| BSF_EXPORT
,
2728 if (s
!= (asection
*) NULL
)
2730 def
->value
= s
->vma
;
2740 Create an absolute symbol with the given name with the value of the
2741 address of the first byte after the end of the section named.
2743 If the symbol already exists, then do nothing.
2746 DEFUN (lang_abs_symbol_at_end_of
, (section
, name
),
2747 CONST
char *section AND
2750 if (ldsym_undefined (name
))
2752 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2754 /* Add a symbol called _end */
2755 asymbol
*def
= create_symbol (name
,
2756 BSF_GLOBAL
| BSF_EXPORT
,
2759 if (s
!= (asection
*) NULL
)
2761 def
->value
= s
->vma
+ s
->_raw_size
;
2771 DEFUN (lang_statement_append
, (list
, element
, field
),
2772 lang_statement_list_type
* list AND
2773 lang_statement_union_type
* element AND
2774 lang_statement_union_type
** field
)
2776 *(list
->tail
) = element
;
2780 /* Set the output format type */
2782 DEFUN (lang_add_output_format
, (format
),
2785 output_target
= format
;