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 asection common_section
;
65 static section_userdata_type common_section_userdata
;
66 static lang_statement_list_type statement_list
;
70 lang_output_section_statement_type
*abs_output_section
;
71 lang_statement_list_type
*stat_ptr
= &statement_list
;
72 lang_input_statement_type
*script_file
= 0;
73 boolean option_longmap
= false;
74 lang_statement_list_type file_chain
=
76 CONST
char *entry_symbol
= 0;
77 bfd_size_type largest_section
= 0;
78 boolean lang_has_input_file
= false;
79 lang_output_section_statement_type
*create_object_symbols
= 0;
80 boolean had_output_filename
= false;
81 boolean lang_float_flag
= false;
84 extern char *default_target
;
86 extern unsigned int undefined_global_sym_count
;
87 extern char *current_file
;
88 extern bfd
*output_bfd
;
89 extern enum bfd_architecture ldfile_output_architecture
;
90 extern unsigned long ldfile_output_machine
;
91 extern char *ldfile_output_machine_name
;
92 extern ldsym_type
*symbol_head
;
93 extern unsigned int commons_pending
;
94 extern args_type command_line
;
95 extern ld_config_type config
;
96 extern boolean had_script
;
97 extern boolean write_map
;
100 #define cat(a,b) a##b
102 #define cat(a,b) a/**/b
105 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
107 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
109 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
111 void lang_add_data
PARAMS ((int type
, union etree_union
* exp
));
114 DEFUN (stat_alloc
, (size
),
117 return obstack_alloc (&stat_obstack
, size
);
120 DEFUN (print_size
, (value
),
123 fprintf (config
.map_file
, "%5x", (unsigned) value
);
126 DEFUN (print_alignment
, (value
),
129 fprintf (config
.map_file
, "2**%1u", value
);
132 DEFUN (print_fill
, (value
),
135 fprintf (config
.map_file
, "%04x", (unsigned) value
);
140 DEFUN (print_section
, (name
),
141 CONST
char *CONST name
)
143 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
146 /*----------------------------------------------------------------------
147 lang_for_each_statement walks the parse tree and calls the provided
148 function for each node
152 DEFUN (lang_for_each_statement_worker
, (func
, s
),
154 lang_statement_union_type
* s
)
156 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
160 switch (s
->header
.type
)
162 case lang_constructors_statement_enum
:
163 lang_for_each_statement_worker (func
, constructor_list
.head
);
165 case lang_output_section_statement_enum
:
166 lang_for_each_statement_worker
168 s
->output_section_statement
.children
.head
);
170 case lang_wild_statement_enum
:
171 lang_for_each_statement_worker
173 s
->wild_statement
.children
.head
);
175 case lang_data_statement_enum
:
176 case lang_object_symbols_statement_enum
:
177 case lang_output_statement_enum
:
178 case lang_target_statement_enum
:
179 case lang_input_section_enum
:
180 case lang_input_statement_enum
:
181 case lang_assignment_statement_enum
:
182 case lang_padding_statement_enum
:
183 case lang_address_statement_enum
:
193 DEFUN (lang_for_each_statement
, (func
),
196 lang_for_each_statement_worker (func
,
197 statement_list
.head
);
200 /*----------------------------------------------------------------------*/
202 DEFUN (lang_list_init
, (list
),
203 lang_statement_list_type
* list
)
205 list
->head
= (lang_statement_union_type
*) NULL
;
206 list
->tail
= &list
->head
;
209 /*----------------------------------------------------------------------
211 build a new statement node for the parse tree
216 lang_statement_union_type
*
217 DEFUN (new_statement
, (type
, size
, list
),
218 enum statement_enum type AND
219 bfd_size_type size AND
220 lang_statement_list_type
* list
)
222 lang_statement_union_type
*new = (lang_statement_union_type
*)
225 new->header
.type
= type
;
226 new->header
.next
= (lang_statement_union_type
*) NULL
;
227 lang_statement_append (list
, new, &new->header
.next
);
232 Build a new input file node for the language. There are several ways
233 in which we treat an input file, eg, we only look at symbols, or
234 prefix it with a -l etc.
236 We can be supplied with requests for input files more than once;
237 they may, for example be split over serveral lines like foo.o(.text)
238 foo.o(.data) etc, so when asked for a file we check that we havn't
239 got it already so we don't duplicate the bfd.
242 static lang_input_statement_type
*
243 DEFUN (new_afile
, (name
, file_type
, target
),
244 CONST
char *CONST name AND
245 CONST lang_input_file_enum_type file_type AND
246 CONST
char *CONST target
)
249 lang_input_statement_type
*p
= new_stat (lang_input_statement
,
252 lang_has_input_file
= true;
254 p
->complained
= false;
257 case lang_input_file_is_symbols_only_enum
:
259 p
->is_archive
= false;
261 p
->local_sym_name
= name
;
262 p
->just_syms_flag
= true;
263 p
->search_dirs_flag
= false;
265 case lang_input_file_is_fake_enum
:
267 p
->is_archive
= false;
269 p
->local_sym_name
= name
;
270 p
->just_syms_flag
= false;
271 p
->search_dirs_flag
= false;
273 case lang_input_file_is_l_enum
:
274 p
->is_archive
= true;
277 p
->local_sym_name
= concat ("-l", name
, "");
278 p
->just_syms_flag
= false;
279 p
->search_dirs_flag
= true;
281 case lang_input_file_is_search_file_enum
:
282 case lang_input_file_is_marker_enum
:
284 p
->is_archive
= false;
286 p
->local_sym_name
= name
;
287 p
->just_syms_flag
= false;
288 p
->search_dirs_flag
= true;
290 case lang_input_file_is_file_enum
:
292 p
->is_archive
= false;
294 p
->local_sym_name
= name
;
295 p
->just_syms_flag
= false;
296 p
->search_dirs_flag
= false;
301 p
->asymbols
= (asymbol
**) NULL
;
302 p
->superfile
= (lang_input_statement_type
*) NULL
;
303 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
304 p
->next
= (lang_statement_union_type
*) NULL
;
306 p
->common_output_section
= (asection
*) NULL
;
307 lang_statement_append (&input_file_chain
,
308 (lang_statement_union_type
*) p
,
313 lang_input_statement_type
*
314 DEFUN (lang_add_input_file
, (name
, file_type
, target
),
316 lang_input_file_enum_type file_type AND
319 /* Look it up or build a new one */
320 lang_has_input_file
= true;
322 lang_input_statement_type
*p
;
324 for (p
= (lang_input_statement_type
*) input_file_chain
.head
;
325 p
!= (lang_input_statement_type
*) NULL
;
326 p
= (lang_input_statement_type
*) (p
->next_real_file
))
328 /* Sometimes we have incomplete entries in here */
329 if (p
->filename
!= (char *) NULL
)
331 if (strcmp (name
, p
->filename
) == 0)
337 return new_afile (name
, file_type
, target
);
341 DEFUN (lang_add_keepsyms_file
, (filename
),
342 CONST
char *filename
)
344 extern strip_symbols_type strip_symbols
;
345 if (keepsyms_file
!= 0)
346 info ("%X%P error: duplicated keep-symbols-file value\n");
347 keepsyms_file
= filename
;
348 if (strip_symbols
!= STRIP_NONE
)
349 info ("%P `-keep-only-symbols-file' overrides `-s' and `-S'\n");
350 strip_symbols
= STRIP_SOME
;
353 /* Build enough state so that the parser can build its tree */
355 DEFUN_VOID (lang_init
)
357 obstack_begin (&stat_obstack
, 1000);
359 stat_ptr
= &statement_list
;
361 lang_list_init (stat_ptr
);
363 lang_list_init (&input_file_chain
);
364 lang_list_init (&lang_output_section_statement
);
365 lang_list_init (&file_chain
);
366 first_file
= lang_add_input_file ((char *) NULL
,
367 lang_input_file_is_marker_enum
,
369 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
371 abs_output_section
->bfd_section
= &bfd_abs_section
;
375 /*----------------------------------------------------------------------
376 A region is an area of memory declared with the
377 MEMORY { name:org=exp, len=exp ... }
380 We maintain a list of all the regions here
382 If no regions are specified in the script, then the default is used
383 which is created when looked up to be the entire data space
386 static lang_memory_region_type
*lang_memory_region_list
;
387 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
389 lang_memory_region_type
*
390 DEFUN (lang_memory_region_lookup
, (name
),
391 CONST
char *CONST name
)
394 lang_memory_region_type
*p
= lang_memory_region_list
;
396 for (p
= lang_memory_region_list
;
397 p
!= (lang_memory_region_type
*) NULL
;
400 if (strcmp (p
->name
, name
) == 0)
405 if (strcmp (name
, "*default*") == 0)
407 /* This is the default region, dig out first one on the list */
408 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
410 return lang_memory_region_list
;
414 lang_memory_region_type
*new =
415 (lang_memory_region_type
*) stat_alloc ((bfd_size_type
) (sizeof (lang_memory_region_type
)));
417 new->name
= buystring (name
);
418 new->next
= (lang_memory_region_type
*) NULL
;
420 *lang_memory_region_list_tail
= new;
421 lang_memory_region_list_tail
= &new->next
;
425 new->had_full_message
= false;
432 lang_output_section_statement_type
*
433 DEFUN (lang_output_section_find
, (name
),
434 CONST
char *CONST name
)
436 lang_statement_union_type
*u
;
437 lang_output_section_statement_type
*lookup
;
439 for (u
= lang_output_section_statement
.head
;
440 u
!= (lang_statement_union_type
*) NULL
;
443 lookup
= &u
->output_section_statement
;
444 if (strcmp (name
, lookup
->name
) == 0)
449 return (lang_output_section_statement_type
*) NULL
;
452 lang_output_section_statement_type
*
453 DEFUN (lang_output_section_statement_lookup
, (name
),
454 CONST
char *CONST name
)
456 lang_output_section_statement_type
*lookup
;
458 lookup
= lang_output_section_find (name
);
459 if (lookup
== (lang_output_section_statement_type
*) NULL
)
462 lookup
= (lang_output_section_statement_type
*)
463 new_stat (lang_output_section_statement
, stat_ptr
);
464 lookup
->region
= (lang_memory_region_type
*) NULL
;
466 lookup
->block_value
= 1;
469 lookup
->next
= (lang_statement_union_type
*) NULL
;
470 lookup
->bfd_section
= (asection
*) NULL
;
471 lookup
->processed
= false;
472 lookup
->loadable
= 1;
473 lookup
->addr_tree
= (etree_type
*) NULL
;
474 lang_list_init (&lookup
->children
);
476 lang_statement_append (&lang_output_section_statement
,
477 (lang_statement_union_type
*) lookup
,
485 DEFUN (print_flags
, (ignore_flags
),
488 fprintf (config
.map_file
, "(");
490 if (flags
->flag_read
)
491 fprintf (outfile
, "R");
492 if (flags
->flag_write
)
493 fprintf (outfile
, "W");
494 if (flags
->flag_executable
)
495 fprintf (outfile
, "X");
496 if (flags
->flag_loadable
)
497 fprintf (outfile
, "L");
499 fprintf (config
.map_file
, ")");
503 DEFUN_VOID (lang_map
)
505 lang_memory_region_type
*m
;
507 fprintf (config
.map_file
, "**MEMORY CONFIGURATION**\n\n");
509 fprintf (config
.map_file
, "name\t\torigin\t\tlength\t\tattributes\n");
511 fprintf (config
.map_file
,
512 "name\t\torigin length r_size c_size is attributes\n");
515 for (m
= lang_memory_region_list
;
516 m
!= (lang_memory_region_type
*) NULL
;
519 fprintf (config
.map_file
, "%-16s", m
->name
);
520 print_address (m
->origin
);
522 print_address (m
->length
);
524 print_address (m
->old_length
);
526 print_address (m
->current
- m
->origin
);
529 fprintf(config
.map_file
," %2d%% ", ( m
->current
- m
->origin
) * 100 / m
->old_length
);
530 print_flags (&m
->flags
);
531 fprintf (config
.map_file
, "\n");
533 fprintf (config
.map_file
, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
534 fprintf (config
.map_file
, "output input virtual\n");
535 fprintf (config
.map_file
, "section section address tsize\n\n");
546 lang_output_section_statement_type
* s
)
548 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
549 section_userdata_type
*new =
550 (section_userdata_type
*)
551 stat_alloc ((bfd_size_type
) (sizeof (section_userdata_type
)));
553 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
554 if (s
->bfd_section
== (asection
*) NULL
)
555 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
556 if (s
->bfd_section
== (asection
*) NULL
)
558 einfo ("%P%F output format %s cannot represent section called %s\n",
559 output_bfd
->xvec
->name
, s
->name
);
561 s
->bfd_section
->output_section
= s
->bfd_section
;
562 /* s->bfd_section->flags = s->flags;*/
564 /* We initialize an output sections output offset to minus its own */
565 /* vma to allow us to output a section through itself */
566 s
->bfd_section
->output_offset
= 0;
567 get_userdata (s
->bfd_section
) = (PTR
) new;
571 /***********************************************************************
574 These expand statements like *(.text) and foo.o to a list of
575 explicit actions, like foo.o(.text), bar.o(.text) and
578 The toplevel routine, wild, takes a statement, section, file and
579 target. If either the section or file is null it is taken to be the
580 wildcard. Seperate lang_input_section statements are created for
581 each part of the expanstion, and placed after the statement provided.
586 DEFUN (wild_doit
, (ptr
, section
, output
, file
),
587 lang_statement_list_type
* ptr AND
588 asection
* section AND
589 lang_output_section_statement_type
* output AND
590 lang_input_statement_type
* file
)
592 if (output
->bfd_section
== (asection
*) NULL
)
595 /* Initialize the vma and size to the existing section. This will
596 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
598 if (section
!= (asection
*) NULL
)
600 bfd_set_section_vma (0, output
->bfd_section
,
601 bfd_section_vma (0, section
));
602 output
->bfd_section
->_raw_size
= section
->_raw_size
;
606 if (section
!= (asection
*) NULL
607 && section
->output_section
== (asection
*) NULL
)
609 /* Add a section reference to the list */
610 lang_input_section_type
*new = new_stat (lang_input_section
, ptr
);
612 new->section
= section
;
614 section
->output_section
= output
->bfd_section
;
615 section
->output_section
->flags
|= section
->flags
;
616 if (!output
->loadable
)
618 /* Turn of load flag */
619 output
->bfd_section
->flags
&= ~SEC_LOAD
;
621 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
623 output
->bfd_section
->alignment_power
= section
->alignment_power
;
625 /* If supplied an aligmnet, then force it */
626 if (output
->section_alignment
!= -1)
628 output
->bfd_section
->alignment_power
= output
->section_alignment
;
634 DEFUN (our_bfd_get_section_by_name
, (abfd
, section
),
638 return bfd_get_section_by_name (abfd
, section
);
642 DEFUN (wild_section
, (ptr
, section
, file
, output
),
643 lang_wild_statement_type
* ptr AND
644 CONST
char *section AND
645 lang_input_statement_type
* file AND
646 lang_output_section_statement_type
* output
)
650 if (file
->just_syms_flag
== false)
652 if (section
== (char *) NULL
)
654 /* Do the creation to all sections in the file */
655 for (s
= file
->the_bfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
657 wild_doit (&ptr
->children
, s
, output
, file
);
662 /* Do the creation to the named section only */
663 wild_doit (&ptr
->children
,
664 our_bfd_get_section_by_name (file
->the_bfd
, section
),
670 /* passed a file name (which must have been seen already and added to
671 the statement tree. We will see if it has been opened already and
672 had its symbols read. If not then we'll read it.
674 Archives are pecuilar here. We may open them once, but if they do
675 not define anything we need at the time, they won't have all their
676 symbols read. If we need them later, we'll have to redo it.
679 lang_input_statement_type
*
680 DEFUN (lookup_name
, (name
),
681 CONST
char *CONST name
)
683 lang_input_statement_type
*search
;
685 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
686 search
!= (lang_input_statement_type
*) NULL
;
687 search
= (lang_input_statement_type
*) search
->next_real_file
)
689 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
693 if (search
->filename
!= (char *) NULL
&& name
!= (char *) NULL
)
695 if (strcmp (search
->filename
, name
) == 0)
697 ldmain_open_file_read_symbol (search
);
703 /* There isn't an afile entry for this file yet, this must be
704 because the name has only appeared inside a load script and not
705 on the command line */
706 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
);
707 ldmain_open_file_read_symbol (search
);
714 DEFUN (wild
, (s
, section
, file
, target
, output
),
715 lang_wild_statement_type
* s AND
716 CONST
char *CONST section AND
717 CONST
char *CONST file AND
718 CONST
char *CONST target AND
719 lang_output_section_statement_type
* output
)
721 lang_input_statement_type
*f
;
723 if (file
== (char *) NULL
)
725 /* Perform the iteration over all files in the list */
726 for (f
= (lang_input_statement_type
*) file_chain
.head
;
727 f
!= (lang_input_statement_type
*) NULL
;
728 f
= (lang_input_statement_type
*) f
->next
)
730 wild_section (s
, section
, f
, output
);
735 /* Perform the iteration over a single file */
736 wild_section (s
, section
, lookup_name (file
), output
);
738 if (section
!= (char *) NULL
739 && strcmp (section
, "COMMON") == 0
740 && default_common_section
== (lang_output_section_statement_type
*) NULL
)
742 /* Remember the section that common is going to incase we later
743 get something which doesn't know where to put it */
744 default_common_section
= output
;
749 read in all the files
752 DEFUN (open_output
, (name
),
753 CONST
char *CONST name
)
755 extern unsigned long ldfile_output_machine
;
756 extern enum bfd_architecture ldfile_output_architecture
;
758 extern CONST
char *output_filename
;
761 if (output_target
== (char *) NULL
)
763 if (current_target
!= (char *) NULL
)
764 output_target
= current_target
;
766 output_target
= default_target
;
768 output
= bfd_openw (name
, output_target
);
769 output_filename
= name
;
771 if (output
== (bfd
*) NULL
)
773 if (bfd_error
== invalid_target
)
775 einfo ("%P%F target %s not found\n", output_target
);
777 einfo ("%P%F problem opening output file %s, %E\n", name
);
780 /* output->flags |= D_PAGED;*/
782 bfd_set_format (output
, bfd_object
);
783 bfd_set_arch_mach (output
,
784 ldfile_output_architecture
,
785 ldfile_output_machine
);
793 DEFUN (ldlang_open_output
, (statement
),
794 lang_statement_union_type
* statement
)
796 switch (statement
->header
.type
)
798 case lang_output_statement_enum
:
799 output_bfd
= open_output (statement
->output_statement
.name
);
800 ldemul_set_output_arch ();
801 if (config
.magic_demand_paged
&& !config
.relocateable_output
)
802 output_bfd
->flags
|= D_PAGED
;
804 output_bfd
->flags
&= ~D_PAGED
;
805 if (config
.text_read_only
)
806 output_bfd
->flags
|= WP_TEXT
;
808 output_bfd
->flags
&= ~WP_TEXT
;
811 case lang_target_statement_enum
:
812 current_target
= statement
->target_statement
.target
;
820 DEFUN (open_input_bfds
, (statement
),
821 lang_statement_union_type
* statement
)
823 switch (statement
->header
.type
)
825 case lang_target_statement_enum
:
826 current_target
= statement
->target_statement
.target
;
828 case lang_wild_statement_enum
:
829 /* Maybe we should load the file's symbols */
830 if (statement
->wild_statement
.filename
)
832 (void) lookup_name (statement
->wild_statement
.filename
);
835 case lang_input_statement_enum
:
836 if (statement
->input_statement
.real
== true)
838 statement
->input_statement
.target
= current_target
;
839 lookup_name (statement
->input_statement
.filename
);
847 /* If there are [COMMONS] statements, put a wild one into the bss section */
850 lang_reasonable_defaults ()
856 lang_output_section_statement_lookup (".text");
857 lang_output_section_statement_lookup (".data");
859 default_common_section
=
860 lang_output_section_statement_lookup (".bss");
863 if (placed_commons
== false)
865 lang_wild_statement_type
*new =
866 new_stat (lang_wild_statement
,
867 &default_common_section
->children
);
869 new->section_name
= "COMMON";
870 new->filename
= (char *) NULL
;
871 lang_list_init (&new->children
);
878 Add the supplied name to the symbol table as an undefined reference.
879 Remove items from the chain as we open input bfds
881 typedef struct ldlang_undef_chain_list
883 struct ldlang_undef_chain_list
*next
;
885 } ldlang_undef_chain_list_type
;
887 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
890 DEFUN (ldlang_add_undef
, (name
),
891 CONST
char *CONST name
)
893 ldlang_undef_chain_list_type
*new =
894 (ldlang_undef_chain_list_type
895 *) stat_alloc ((bfd_size_type
) (sizeof (ldlang_undef_chain_list_type
)));
897 new->next
= ldlang_undef_chain_list_head
;
898 ldlang_undef_chain_list_head
= new;
900 new->name
= buystring (name
);
903 /* Run through the list of undefineds created above and place them
904 into the linker hash table as undefined symbols belonging to the
908 DEFUN_VOID (lang_place_undefineds
)
910 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
912 while (ptr
!= (ldlang_undef_chain_list_type
*) NULL
)
915 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
917 def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
919 def
->name
= ptr
->name
;
920 def
->section
= &bfd_und_section
;
921 Q_enter_global_ref (def_ptr
, ptr
->name
);
926 /* Copy important data from out internal form to the bfd way. Also
927 create a section for the dummy file
931 DEFUN_VOID (lang_create_output_section_statements
)
933 lang_statement_union_type
*os
;
935 for (os
= lang_output_section_statement
.head
;
936 os
!= (lang_statement_union_type
*) NULL
;
937 os
= os
->output_section_statement
.next
)
939 lang_output_section_statement_type
*s
=
940 &os
->output_section_statement
;
948 DEFUN_VOID (lang_init_script_file
)
950 script_file
= lang_add_input_file ("script file",
951 lang_input_file_is_fake_enum
,
953 script_file
->the_bfd
= bfd_create ("script file", output_bfd
);
954 script_file
->symbol_count
= 0;
955 script_file
->the_bfd
->sections
= output_bfd
->sections
;
956 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
958 abs_output_section
->bfd_section
= &bfd_abs_section
;
962 /* Open input files and attatch to output sections */
964 DEFUN (map_input_to_output_sections
, (s
, target
, output_section_statement
),
965 lang_statement_union_type
* s AND
966 CONST
char *target AND
967 lang_output_section_statement_type
* output_section_statement
)
969 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
971 switch (s
->header
.type
)
975 case lang_wild_statement_enum
:
976 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
977 s
->wild_statement
.filename
, target
,
978 output_section_statement
);
981 case lang_constructors_statement_enum
:
982 map_input_to_output_sections (constructor_list
.head
,
984 output_section_statement
);
986 case lang_output_section_statement_enum
:
987 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
989 &s
->output_section_statement
);
991 case lang_output_statement_enum
:
993 case lang_target_statement_enum
:
994 target
= s
->target_statement
.target
;
996 case lang_fill_statement_enum
:
997 case lang_input_section_enum
:
998 case lang_object_symbols_statement_enum
:
999 case lang_data_statement_enum
:
1000 case lang_assignment_statement_enum
:
1001 case lang_padding_statement_enum
:
1003 case lang_afile_asection_pair_statement_enum
:
1006 case lang_address_statement_enum
:
1007 /* Mark the specified section with the supplied address */
1009 lang_output_section_statement_type
*os
=
1010 lang_output_section_statement_lookup
1011 (s
->address_statement
.section_name
);
1013 os
->addr_tree
= s
->address_statement
.address
;
1014 if (os
->bfd_section
== (asection
*) NULL
)
1016 einfo ("%P%F can't set the address of undefined section %s\n",
1017 s
->address_statement
.section_name
);
1021 case lang_input_statement_enum
:
1022 /* A standard input statement, has no wildcards */
1023 /* ldmain_open_file_read_symbol(&s->input_statement);*/
1034 DEFUN (print_output_section_statement
, (output_section_statement
),
1035 lang_output_section_statement_type
* output_section_statement
)
1037 asection
*section
= output_section_statement
->bfd_section
;
1040 print_section (output_section_statement
->name
);
1045 print_dot
= section
->vma
;
1049 print_address (section
->vma
);
1051 print_size (section
->_raw_size
);
1053 print_size(section
->_cooked_size
);
1055 print_alignment (section
->alignment_power
);
1058 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1059 print_flags (stdout
, &output_section_statement
->flags
);
1061 if (section
->flags
& SEC_LOAD
)
1062 fprintf (config
.map_file
, "load ");
1063 if (section
->flags
& SEC_ALLOC
)
1064 fprintf (config
.map_file
, "alloc ");
1065 if (section
->flags
& SEC_RELOC
)
1066 fprintf (config
.map_file
, "reloc ");
1067 if (section
->flags
& SEC_HAS_CONTENTS
)
1068 fprintf (config
.map_file
, "contents ");
1073 fprintf (config
.map_file
, "No attached output section");
1076 if (output_section_statement
->section_alignment
>= 0
1077 || output_section_statement
->section_alignment
>= 0)
1079 printf("\t\t\t\t\tforced alignment ");
1080 if ( output_section_statement
->section_alignment
>= 0)
1082 printf("section 2**%d ",output_section_statement
->section_alignment
);
1084 if ( output_section_statement
->subsection_alignment
>= 0)
1086 printf("subsection 2**%d ",output_section_statement
->subsection_alignment
);
1091 print_statement (output_section_statement
->children
.head
,
1092 output_section_statement
);
1097 DEFUN (print_assignment
, (assignment
, output_section
),
1098 lang_assignment_statement_type
* assignment AND
1099 lang_output_section_statement_type
* output_section
)
1101 etree_value_type result
;
1107 print_address (print_dot
);
1109 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1111 lang_final_phase_enum
,
1117 print_address (result
.value
);
1121 fprintf (config
.map_file
, "*undefined*");
1124 exp_print_tree (assignment
->exp
);
1126 fprintf (config
.map_file
, "\n");
1130 DEFUN (print_input_statement
, (statm
),
1131 lang_input_statement_type
* statm
)
1133 if (statm
->filename
!= (char *) NULL
)
1135 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1140 DEFUN (print_symbol
, (q
),
1144 fprintf (config
.map_file
, " ");
1146 fprintf (config
.map_file
, " ");
1147 print_address (outside_symbol_address (q
));
1148 fprintf (config
.map_file
, " %s", q
->name
? q
->name
: " ");
1153 DEFUN (print_input_section
, (in
),
1154 lang_input_section_type
* in
)
1156 asection
*i
= in
->section
;
1157 int size
= i
->reloc_done
?
1158 bfd_get_section_size_after_reloc (i
) :
1159 bfd_get_section_size_before_reloc (i
);
1164 fprintf (config
.map_file
, " ");
1165 print_section (i
->name
);
1166 fprintf (config
.map_file
, " ");
1167 if (i
->output_section
)
1169 print_address (i
->output_section
->vma
+ i
->output_offset
);
1170 fprintf (config
.map_file
, " ");
1171 print_size (i
->_raw_size
);
1172 fprintf (config
.map_file
, " ");
1173 print_size(i
->_cooked_size
);
1174 fprintf (config
.map_file
, " ");
1175 print_alignment (i
->alignment_power
);
1176 fprintf (config
.map_file
, " ");
1180 bfd
*abfd
= in
->ifile
->the_bfd
;
1182 if (in
->ifile
->just_syms_flag
== true)
1184 fprintf (config
.map_file
, "symbols only ");
1187 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1188 if (abfd
->my_archive
!= (bfd
*) NULL
)
1190 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1195 fprintf (config
.map_file
, "%s", abfd
->filename
);
1197 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1200 /* Find all the symbols in this file defined in this section */
1202 if (in
->ifile
->symbol_count
)
1206 for (p
= in
->ifile
->asymbols
; *p
; p
++)
1210 if (bfd_get_section (q
) == i
&& q
->flags
& BSF_GLOBAL
)
1223 print_dot
= outside_section_address (i
) + size
;
1227 fprintf (config
.map_file
, "No output section allocated\n");
1233 DEFUN (print_fill_statement
, (fill
),
1234 lang_fill_statement_type
* fill
)
1236 fprintf (config
.map_file
, "FILL mask ");
1237 print_fill (fill
->fill
);
1241 DEFUN (print_data_statement
, (data
),
1242 lang_data_statement_type
* data
)
1244 /* bfd_vma value; */
1249 /* ASSERT(print_dot == data->output_vma);*/
1251 print_address (data
->output_vma
+ data
->output_section
->vma
);
1253 print_address (data
->value
);
1258 fprintf (config
.map_file
, "BYTE ");
1259 print_dot
+= BYTE_SIZE
;
1262 fprintf (config
.map_file
, "SHORT ");
1263 print_dot
+= SHORT_SIZE
;
1266 fprintf (config
.map_file
, "LONG ");
1267 print_dot
+= LONG_SIZE
;
1271 exp_print_tree (data
->exp
);
1273 fprintf (config
.map_file
, "\n");
1278 DEFUN (print_padding_statement
, (s
),
1279 lang_padding_statement_type
* s
)
1283 print_section ("*fill*");
1285 print_address (s
->output_offset
+ s
->output_section
->vma
);
1287 print_size (s
->size
);
1289 print_fill (s
->fill
);
1292 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1297 DEFUN (print_wild_statement
, (w
, os
),
1298 lang_wild_statement_type
* w AND
1299 lang_output_section_statement_type
* os
)
1301 fprintf (config
.map_file
, " from ");
1302 if (w
->filename
!= (char *) NULL
)
1304 fprintf (config
.map_file
, "%s", w
->filename
);
1308 fprintf (config
.map_file
, "*");
1310 if (w
->section_name
!= (char *) NULL
)
1312 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1316 fprintf (config
.map_file
, "(*)");
1319 print_statement (w
->children
.head
, os
);
1323 DEFUN (print_statement
, (s
, os
),
1324 lang_statement_union_type
* s AND
1325 lang_output_section_statement_type
* os
)
1329 switch (s
->header
.type
)
1331 case lang_constructors_statement_enum
:
1332 fprintf (config
.map_file
, "constructors:\n");
1333 print_statement (constructor_list
.head
, os
);
1335 case lang_wild_statement_enum
:
1336 print_wild_statement (&s
->wild_statement
, os
);
1339 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1342 case lang_address_statement_enum
:
1343 fprintf (config
.map_file
, "address\n");
1345 case lang_object_symbols_statement_enum
:
1346 fprintf (config
.map_file
, "object symbols\n");
1348 case lang_fill_statement_enum
:
1349 print_fill_statement (&s
->fill_statement
);
1351 case lang_data_statement_enum
:
1352 print_data_statement (&s
->data_statement
);
1354 case lang_input_section_enum
:
1355 print_input_section (&s
->input_section
);
1357 case lang_padding_statement_enum
:
1358 print_padding_statement (&s
->padding_statement
);
1360 case lang_output_section_statement_enum
:
1361 print_output_section_statement (&s
->output_section_statement
);
1363 case lang_assignment_statement_enum
:
1364 print_assignment (&s
->assignment_statement
,
1367 case lang_target_statement_enum
:
1368 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1370 case lang_output_statement_enum
:
1371 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1372 s
->output_statement
.name
,
1373 output_target
? output_target
: "");
1375 case lang_input_statement_enum
:
1376 print_input_statement (&s
->input_statement
);
1378 case lang_afile_asection_pair_statement_enum
:
1388 DEFUN_VOID (print_statements
)
1390 print_statement (statement_list
.head
,
1391 abs_output_section
);
1396 DEFUN (insert_pad
, (this_ptr
, fill
, power
, output_section_statement
, dot
),
1397 lang_statement_union_type
** this_ptr AND
1399 unsigned int power AND
1400 asection
* output_section_statement AND
1403 /* Align this section first to the
1404 input sections requirement, then
1405 to the output section's requirement.
1406 If this alignment is > than any seen before,
1407 then record it too. Perform the alignment by
1408 inserting a magic 'padding' statement.
1411 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1413 if (alignment_needed
!= 0)
1415 lang_statement_union_type
*new =
1416 (lang_statement_union_type
*)
1417 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1419 /* Link into existing chain */
1420 new->header
.next
= *this_ptr
;
1422 new->header
.type
= lang_padding_statement_enum
;
1423 new->padding_statement
.output_section
= output_section_statement
;
1424 new->padding_statement
.output_offset
=
1425 dot
- output_section_statement
->vma
;
1426 new->padding_statement
.fill
= fill
;
1427 new->padding_statement
.size
= alignment_needed
;
1431 /* Remember the most restrictive alignment */
1432 if (power
> output_section_statement
->alignment_power
)
1434 output_section_statement
->alignment_power
= power
;
1436 output_section_statement
->_raw_size
+= alignment_needed
;
1437 return alignment_needed
+ dot
;
1441 /* Work out how much this section will move the dot point */
1443 DEFUN (size_input_section
, (this_ptr
, output_section_statement
, fill
,
1445 lang_statement_union_type
** this_ptr AND
1446 lang_output_section_statement_type
* output_section_statement AND
1447 unsigned short fill AND
1451 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1452 asection
*i
= is
->section
;
1454 if (is
->ifile
->just_syms_flag
== false)
1456 if (output_section_statement
->subsection_alignment
!= -1)
1457 i
->alignment_power
=
1458 output_section_statement
->subsection_alignment
;
1460 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1461 output_section_statement
->bfd_section
, dot
);
1463 /* remember the largest size so we can malloc the largest area
1464 needed for the output stage. Only remember the size of sections
1465 which we will actually allocate */
1467 (SEC_HAS_CONTENTS
| SEC_ALLOC
)) == (SEC_HAS_CONTENTS
| SEC_ALLOC
))
1468 && (bfd_get_section_size_before_reloc (i
) > largest_section
))
1470 largest_section
= bfd_get_section_size_before_reloc (i
);
1473 /* Remember where in the output section this input section goes */
1475 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1477 /* Mark how big the output section must be to contain this now
1481 dot
+= i
->_cooked_size
;
1485 dot
+= i
->_raw_size
;
1487 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1491 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1497 /* Sizing happens in two passes, first pass we allocate worst case
1498 stuff. The second pass (if relaxing), we use what we learnt to
1499 change the size of some relocs from worst case to better
1501 static boolean had_relax
;
1504 DEFUN (lang_size_sections
, (s
, output_section_statement
, prev
, fill
,
1506 lang_statement_union_type
* s AND
1507 lang_output_section_statement_type
* output_section_statement AND
1508 lang_statement_union_type
** prev AND
1509 unsigned short fill AND
1513 /* Size up the sections from their constituent parts */
1514 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1516 switch (s
->header
.type
)
1519 case lang_output_section_statement_enum
:
1522 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1524 /* If this is a shared library section, don't change the size
1526 if (os
->bfd_section
->flags
& SEC_SHARED_LIBRARY
)
1529 if (os
->bfd_section
== &bfd_abs_section
)
1531 /* No matter what happens, an abs section starts at zero */
1532 bfd_set_section_vma (0, os
->bfd_section
, 0);
1536 if (os
->addr_tree
== (etree_type
*) NULL
)
1538 /* No address specified for this section, get one
1539 from the region specification
1541 if (os
->region
== (lang_memory_region_type
*) NULL
)
1543 os
->region
= lang_memory_region_lookup ("*default*");
1545 dot
= os
->region
->current
;
1551 r
= exp_fold_tree (os
->addr_tree
,
1553 lang_allocating_phase_enum
,
1555 if (r
.valid
== false)
1557 einfo ("%F%S: non constant address expression for section %s\n",
1562 /* The section starts here */
1563 /* First, align to what the section needs */
1566 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1567 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1571 os
->bfd_section
->output_offset
= 0;
1573 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1574 os
->fill
, dot
, relax
);
1575 /* Ignore the size of the input sections, use the vma and size to */
1579 after
= ALIGN (os
->bfd_section
->vma
+
1580 os
->bfd_section
->_raw_size
,
1584 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1585 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1586 os
->processed
= true;
1588 /* Replace into region ? */
1589 if (os
->addr_tree
== (etree_type
*) NULL
1590 && os
->region
!= (lang_memory_region_type
*) NULL
)
1592 os
->region
->current
= dot
;
1593 /* Make sure this isn't silly */
1594 if (os
->region
->current
>
1595 os
->region
->origin
+
1598 einfo ("%X%P: Region %s is full (%B section %s)\n",
1600 os
->bfd_section
->owner
,
1601 os
->bfd_section
->name
);
1602 /* Reset the region pointer */
1603 os
->region
->current
= 0;
1611 case lang_constructors_statement_enum
:
1612 dot
= lang_size_sections (constructor_list
.head
,
1613 output_section_statement
,
1614 &s
->wild_statement
.children
.head
,
1619 case lang_data_statement_enum
:
1621 unsigned int size
= 0;
1623 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1624 s
->data_statement
.output_section
=
1625 output_section_statement
->bfd_section
;
1627 switch (s
->data_statement
.type
)
1641 output_section_statement
->bfd_section
->_raw_size
+= size
;
1645 case lang_wild_statement_enum
:
1647 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
1648 output_section_statement
,
1649 &s
->wild_statement
.children
.head
,
1655 case lang_object_symbols_statement_enum
:
1656 create_object_symbols
= output_section_statement
;
1658 case lang_output_statement_enum
:
1659 case lang_target_statement_enum
:
1661 case lang_input_section_enum
:
1666 if( relax_section (prev
))
1672 (*prev
)->input_section
.section
->_cooked_size
=
1673 (*prev
)->input_section
.section
->_raw_size
;
1676 dot
= size_input_section (prev
,
1677 output_section_statement
,
1678 output_section_statement
->fill
,
1681 case lang_input_statement_enum
:
1683 case lang_fill_statement_enum
:
1684 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
1686 fill
= s
->fill_statement
.fill
;
1688 case lang_assignment_statement_enum
:
1690 bfd_vma newdot
= dot
;
1692 exp_fold_tree (s
->assignment_statement
.exp
,
1693 output_section_statement
,
1694 lang_allocating_phase_enum
,
1698 if (newdot
!= dot
&& !relax
)
1699 /* We've been moved ! so insert a pad */
1701 lang_statement_union_type
*new =
1702 (lang_statement_union_type
*)
1703 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1705 /* Link into existing chain */
1706 new->header
.next
= *prev
;
1708 new->header
.type
= lang_padding_statement_enum
;
1709 new->padding_statement
.output_section
=
1710 output_section_statement
->bfd_section
;
1711 new->padding_statement
.output_offset
=
1712 dot
- output_section_statement
->bfd_section
->vma
;
1713 new->padding_statement
.fill
= fill
;
1714 new->padding_statement
.size
= newdot
- dot
;
1715 output_section_statement
->bfd_section
->_raw_size
+=
1716 new->padding_statement
.size
;
1725 /* This can only get here when relaxing is turned on */
1726 case lang_padding_statement_enum
:
1728 case lang_address_statement_enum
:
1731 prev
= &s
->header
.next
;
1737 DEFUN (lang_do_assignments
, (s
, output_section_statement
, fill
, dot
),
1738 lang_statement_union_type
* s AND
1739 lang_output_section_statement_type
* output_section_statement AND
1740 unsigned short fill AND
1744 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1746 switch (s
->header
.type
)
1748 case lang_constructors_statement_enum
:
1749 dot
= lang_do_assignments (constructor_list
.head
,
1750 output_section_statement
,
1755 case lang_output_section_statement_enum
:
1757 lang_output_section_statement_type
*os
=
1758 &(s
->output_section_statement
);
1760 dot
= os
->bfd_section
->vma
;
1761 (void) lang_do_assignments (os
->children
.head
, os
, os
->fill
, dot
);
1762 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1765 case lang_wild_statement_enum
:
1767 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
1768 output_section_statement
,
1773 case lang_object_symbols_statement_enum
:
1774 case lang_output_statement_enum
:
1775 case lang_target_statement_enum
:
1777 case lang_common_statement_enum
:
1780 case lang_data_statement_enum
:
1782 etree_value_type value
;
1784 value
= exp_fold_tree (s
->data_statement
.exp
,
1786 lang_final_phase_enum
, dot
, &dot
);
1787 s
->data_statement
.value
= value
.value
;
1788 if (value
.valid
== false)
1789 einfo ("%F%P: Invalid data statement\n");
1791 switch (s
->data_statement
.type
)
1804 case lang_input_section_enum
:
1806 asection
*in
= s
->input_section
.section
;
1808 dot
+= bfd_get_section_size_before_reloc (in
);
1812 case lang_input_statement_enum
:
1814 case lang_fill_statement_enum
:
1815 fill
= s
->fill_statement
.fill
;
1817 case lang_assignment_statement_enum
:
1819 exp_fold_tree (s
->assignment_statement
.exp
,
1820 output_section_statement
,
1821 lang_final_phase_enum
,
1827 case lang_padding_statement_enum
:
1828 dot
+= s
->padding_statement
.size
;
1833 case lang_address_statement_enum
:
1844 DEFUN_VOID (lang_relocate_globals
)
1848 Each ldsym_type maintains a chain of pointers to asymbols which
1849 references the definition. Replace each pointer to the referenence
1850 with a pointer to only one place, preferably the definition. If
1851 the defintion isn't available then the common symbol, and if
1852 there isn't one of them then choose one reference.
1855 FOR_EACH_LDSYM (lgs
)
1859 if (lgs
->sdefs_chain
)
1861 it
= *(lgs
->sdefs_chain
);
1863 else if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
1865 it
= *(lgs
->scoms_chain
);
1867 else if (lgs
->srefs_chain
!= (asymbol
**) NULL
)
1869 it
= *(lgs
->srefs_chain
);
1873 /* This can happen when the command line asked for a symbol to
1875 it
= (asymbol
*) NULL
;
1877 if (it
!= (asymbol
*) NULL
)
1879 asymbol
**ptr
= lgs
->srefs_chain
;;
1880 if (lgs
->flags
& SYM_WARNING
)
1882 produce_warnings (lgs
, it
);
1885 while (ptr
!= (asymbol
**) NULL
)
1887 asymbol
*ref
= *ptr
;
1890 ptr
= (asymbol
**) (ref
->udata
);
1899 DEFUN_VOID (lang_finish
)
1902 int warn
= config
.relocateable_output
!= true;
1903 if (entry_symbol
== (char *) NULL
)
1905 /* No entry has been specified, look for start, but don't warn */
1906 entry_symbol
= "start";
1909 lgs
= ldsym_get_soft (entry_symbol
);
1910 if (lgs
&& lgs
->sdefs_chain
)
1912 asymbol
*sy
= *(lgs
->sdefs_chain
);
1914 /* We can set the entry address*/
1915 bfd_set_start_address (output_bfd
,
1916 outside_symbol_address (sy
));
1921 /* Can't find anything reasonable,
1922 use the first address in the text section
1924 asection
*ts
= bfd_get_section_by_name (output_bfd
, ".text");
1928 einfo ("%P: Warning, can't find entry symbol %s, defaulting to %V\n",
1929 entry_symbol
, ts
->vma
);
1931 bfd_set_start_address (output_bfd
, ts
->vma
);
1936 einfo ("%P: Warning, can't find entry symbol %s, not setting start address\n",
1942 /* By now we know the target architecture, and we may have an */
1943 /* ldfile_output_machine_name */
1945 DEFUN_VOID (lang_check
)
1947 lang_statement_union_type
*file
;
1949 unsigned long input_machine
;
1950 enum bfd_architecture input_architecture
;
1951 CONST bfd_arch_info_type
*compatible
;
1953 for (file
= file_chain
.head
;
1954 file
!= (lang_statement_union_type
*) NULL
;
1955 file
= file
->input_statement
.next
)
1957 unsigned long ldfile_new_output_machine
= 0;
1958 enum bfd_architecture ldfile_new_output_architecture
= bfd_arch_unknown
;
1960 input_bfd
= file
->input_statement
.the_bfd
;
1962 input_machine
= bfd_get_mach (input_bfd
);
1963 input_architecture
= bfd_get_arch (input_bfd
);
1966 /* Inspect the architecture and ensure we're linking like with
1969 compatible
= bfd_arch_get_compatible (input_bfd
,
1974 ldfile_output_machine
= compatible
->mach
;
1975 ldfile_output_architecture
= compatible
->arch
;
1980 info ("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1981 bfd_printable_name (input_bfd
), input_bfd
,
1982 bfd_printable_name (output_bfd
));
1984 bfd_set_arch_mach (output_bfd
,
1993 * run through all the global common symbols and tie them
1994 * to the output section requested.
1996 As an experiment we do this 4 times, once for all the byte sizes,
1997 then all the two bytes, all the four bytes and then everything else
2001 DEFUN_VOID (lang_common
)
2006 if (config
.relocateable_output
== false ||
2007 command_line
.force_common_definition
== true)
2009 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<= 1)
2011 for (lgs
= symbol_head
;
2012 lgs
!= (ldsym_type
*) NULL
;
2016 unsigned int power_of_two
;
2020 if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
2022 com
= *(lgs
->scoms_chain
);
2052 if (config
.sort_common
== false || align
== power
)
2054 /* Change from a common symbol into a definition of
2056 lgs
->sdefs_chain
= lgs
->scoms_chain
;
2057 lgs
->scoms_chain
= (asymbol
**) NULL
;
2059 /* Point to the correct common section */
2061 ((lang_input_statement_type
*)
2062 (com
->the_bfd
->usrdata
))->common_section
;
2063 /* Fix the size of the common section */
2065 com
->section
->_raw_size
=
2066 ALIGN (com
->section
->_raw_size
, align
);
2068 /* Remember if this is the biggest alignment ever seen */
2069 if (power_of_two
> com
->section
->alignment_power
)
2071 com
->section
->alignment_power
= power_of_two
;
2074 /* Symbol stops being common and starts being global, but
2075 we remember that it was common once. */
2077 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
2078 com
->value
= com
->section
->_raw_size
;
2082 fprintf (config
.map_file
, "Allocating common %s: %x at %x %s\n",
2085 (unsigned) com
->value
,
2086 com
->the_bfd
->filename
);
2089 com
->section
->_raw_size
+= size
;
2102 run through the input files and ensure that every input
2103 section has somewhere to go. If one is found without
2104 a destination then create an input request and place it
2105 into the statement tree.
2109 DEFUN_VOID (lang_place_orphans
)
2111 lang_input_statement_type
*file
;
2113 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2114 file
!= (lang_input_statement_type
*) NULL
;
2115 file
= (lang_input_statement_type
*) file
->next
)
2119 for (s
= file
->the_bfd
->sections
;
2120 s
!= (asection
*) NULL
;
2123 if (s
->output_section
== (asection
*) NULL
)
2125 /* This section of the file is not attatched, root
2126 around for a sensible place for it to go */
2128 if (file
->common_section
== s
)
2130 /* This is a lonely common section which must
2131 have come from an archive. We attatch to the
2132 section with the wildcard */
2133 if (config
.relocateable_output
!= true
2134 && command_line
.force_common_definition
== false)
2136 if (default_common_section
==
2137 (lang_output_section_statement_type
*) NULL
)
2139 info ("%P: No [COMMON] command, defaulting to .bss\n");
2141 default_common_section
=
2142 lang_output_section_statement_lookup (".bss");
2145 wild_doit (&default_common_section
->children
, s
,
2146 default_common_section
, file
);
2151 lang_output_section_statement_type
*os
=
2152 lang_output_section_statement_lookup (s
->name
);
2154 wild_doit (&os
->children
, s
, os
, file
);
2163 DEFUN (lang_set_flags
, (ptr
, flags
),
2167 boolean state
= false;
2182 /* ptr->flag_read = state; */
2185 /* ptr->flag_write = state; */
2188 /* ptr->flag_executable= state;*/
2192 /* ptr->flag_loadable= state;*/
2195 einfo ("%P%F illegal syntax in flags\n");
2205 DEFUN (lang_for_each_file
, (func
),
2206 void (*func
) PARAMS ((lang_input_statement_type
*)))
2208 lang_input_statement_type
*f
;
2210 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2211 f
!= (lang_input_statement_type
*) NULL
;
2212 f
= (lang_input_statement_type
*) f
->next
)
2220 DEFUN (lang_for_each_input_section
, (func
),
2221 void (*func
) PARAMS ((bfd
* ab
, asection
* as
)))
2223 lang_input_statement_type
*f
;
2225 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2226 f
!= (lang_input_statement_type
*) NULL
;
2227 f
= (lang_input_statement_type
*) f
->next
)
2231 for (s
= f
->the_bfd
->sections
;
2232 s
!= (asection
*) NULL
;
2235 func (f
->the_bfd
, s
);
2243 DEFUN (ldlang_add_file
, (entry
),
2244 lang_input_statement_type
* entry
)
2247 lang_statement_append (&file_chain
,
2248 (lang_statement_union_type
*) entry
,
2253 DEFUN (lang_add_output
, (name
),
2256 lang_output_statement_type
*new = new_stat (lang_output_statement
,
2260 had_output_filename
= true;
2264 static lang_output_section_statement_type
*current_section
;
2266 static int topower(x
)
2271 if (x
< 0) return -1;
2272 for (l
= 0; l
< 32; l
++)
2274 if (i
>= x
) return l
;
2279 DEFUN (lang_enter_output_section_statement
,
2280 (output_section_statement_name
,
2285 char *output_section_statement_name AND
2286 etree_type
* address_exp AND
2288 bfd_vma block_value AND
2289 etree_type
*align AND
2290 etree_type
*subalign
)
2292 lang_output_section_statement_type
*os
;
2296 lang_output_section_statement_lookup (output_section_statement_name
);
2300 /* Add this statement to tree */
2301 /* add_statement(lang_output_section_statement_enum,
2302 output_section_statement);*/
2303 /* Make next things chain into subchain of this */
2305 if (os
->addr_tree
==
2306 (etree_type
*) NULL
)
2312 if (flags
& SEC_NEVER_LOAD
)
2316 os
->block_value
= block_value
;
2317 stat_ptr
= &os
->children
;
2319 os
->subsection_alignment
= topower(
2320 exp_get_value_int(subalign
, -1,
2321 "subsection alignment",
2323 os
->section_alignment
= topower(
2324 exp_get_value_int(align
, -1,
2325 "section alignment", 0));
2329 DEFUN_VOID (lang_final
)
2331 if (had_output_filename
== false)
2333 extern CONST
char *output_filename
;
2335 lang_add_output (output_filename
);
2339 /* Reset the current counters in the regions */
2341 DEFUN_VOID (reset_memory_regions
)
2343 lang_memory_region_type
*p
= lang_memory_region_list
;
2345 for (p
= lang_memory_region_list
;
2346 p
!= (lang_memory_region_type
*) NULL
;
2349 p
->old_length
= p
->current
- p
->origin
;
2350 p
->current
= p
->origin
;
2357 DEFUN (create_symbol
, (name
, flags
, section
),
2358 CONST
char *name AND
2362 extern lang_input_statement_type
*script_file
;
2363 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
2365 /* Add this definition to script file */
2366 asymbol
*def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
2368 def
->name
= buystring (name
);
2371 def
->section
= section
;
2374 Q_enter_global_ref (def_ptr
, name
);
2379 DEFUN_VOID (lang_process
)
2382 if (had_script
== false)
2384 parse_line (ldemul_get_script (), 1);
2386 lang_reasonable_defaults ();
2387 current_target
= default_target
;
2389 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2390 /* For each output section statement, create a section in the output
2392 lang_create_output_section_statements ();
2394 /* Create a dummy bfd for the script */
2395 lang_init_script_file ();
2397 /* Add to the hash table all undefineds on the command line */
2398 lang_place_undefineds ();
2400 /* Create a bfd for each input file */
2401 current_target
= default_target
;
2402 lang_for_each_statement (open_input_bfds
);
2404 common_section
.userdata
= (PTR
) & common_section_userdata
;
2407 /* Run through the contours of the script and attatch input sections
2408 to the correct output sections
2410 find_constructors ();
2411 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2412 (lang_output_section_statement_type
*) NULL
);
2415 /* Find any sections not attatched explicitly and handle them */
2416 lang_place_orphans ();
2418 /* Size up the common data */
2421 ldemul_before_allocation ();
2431 lang_size_sections (statement_list
.head
,
2432 (lang_output_section_statement_type
*) NULL
,
2433 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2434 /* FIXME. Until the code in relax is fixed so that it only reads in
2435 stuff once, we cant iterate since there is no way for the linker to
2436 know what has been patched and what hasn't */
2442 /* Now run around and relax if we can */
2443 if (command_line
.relax
)
2445 /* First time round is a trial run to get the 'worst case' addresses of the
2446 objects if there was no relaxing */
2447 lang_size_sections (statement_list
.head
,
2448 (lang_output_section_statement_type
*) NULL
,
2449 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2453 /* Move the global symbols around so the second pass of relaxing can
2455 lang_relocate_globals ();
2457 reset_memory_regions ();
2459 /* Do all the assignments, now that we know the final restingplaces
2460 of all the symbols */
2462 lang_do_assignments (statement_list
.head
,
2467 /* Perform another relax pass - this time we know where the
2468 globals are, so can make better guess */
2469 lang_size_sections (statement_list
.head
,
2470 (lang_output_section_statement_type
*) NULL
,
2471 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2479 /* Size up the sections */
2480 lang_size_sections (statement_list
.head
,
2482 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2487 /* See if anything special should be done now we know how big
2489 ldemul_after_allocation ();
2491 /* Do all the assignments, now that we know the final restingplaces
2492 of all the symbols */
2494 lang_do_assignments (statement_list
.head
,
2499 /* Move the global symbols around */
2500 lang_relocate_globals ();
2502 /* Make sure that we're not mixing architectures */
2510 /* EXPORTED TO YACC */
2513 DEFUN (lang_add_wild
, (section_name
, filename
),
2514 CONST
char *CONST section_name AND
2515 CONST
char *CONST filename
)
2517 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
2520 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
2522 placed_commons
= true;
2524 if (filename
!= (char *) NULL
)
2526 lang_has_input_file
= true;
2528 new->section_name
= section_name
;
2529 new->filename
= filename
;
2530 lang_list_init (&new->children
);
2534 DEFUN (lang_section_start
, (name
, address
),
2535 CONST
char *name AND
2536 etree_type
* address
)
2538 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
2540 ad
->section_name
= name
;
2541 ad
->address
= address
;
2545 DEFUN (lang_add_entry
, (name
),
2548 entry_symbol
= name
;
2552 DEFUN (lang_add_target
, (name
),
2555 lang_target_statement_type
*new = new_stat (lang_target_statement
,
2563 DEFUN (lang_add_map
, (name
),
2571 map_option_f
= true;
2579 DEFUN (lang_add_fill
, (exp
),
2582 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
2589 DEFUN (lang_add_data
, (type
, exp
),
2591 union etree_union
*exp
)
2594 lang_data_statement_type
*new = new_stat (lang_data_statement
,
2603 DEFUN (lang_add_assignment
, (exp
),
2606 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
2613 DEFUN (lang_add_attribute
, (attribute
),
2614 enum statement_enum attribute
)
2616 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
2620 DEFUN (lang_startup
, (name
),
2623 if (startup_file
!= (char *) NULL
)
2625 einfo ("%P%FMultiple STARTUP files\n");
2627 first_file
->filename
= name
;
2628 first_file
->local_sym_name
= name
;
2630 startup_file
= name
;
2634 DEFUN (lang_float
, (maybe
),
2637 lang_float_flag
= maybe
;
2641 DEFUN (lang_leave_output_section_statement
, (fill
, memspec
),
2643 CONST
char *memspec
)
2645 current_section
->fill
= fill
;
2646 current_section
->region
= lang_memory_region_lookup (memspec
);
2647 stat_ptr
= &statement_list
;
2649 /* We remember if we are closing a .data section, since we use it to
2650 store constructors in */
2651 if (strcmp (current_section
->name
, ".data") == 0)
2653 end_of_data_section_statement_list
= statement_list
;
2659 Create an absolute symbol with the given name with the value of the
2660 address of first byte of the section named.
2662 If the symbol already exists, then do nothing.
2665 DEFUN (lang_abs_symbol_at_beginning_of
, (section
, name
),
2666 CONST
char *section AND
2669 if (ldsym_undefined (name
))
2671 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2672 asymbol
*def
= create_symbol (name
,
2673 BSF_GLOBAL
| BSF_EXPORT
,
2676 if (s
!= (asection
*) NULL
)
2678 def
->value
= s
->vma
;
2688 Create an absolute symbol with the given name with the value of the
2689 address of the first byte after the end of the section named.
2691 If the symbol already exists, then do nothing.
2694 DEFUN (lang_abs_symbol_at_end_of
, (section
, name
),
2695 CONST
char *section AND
2698 if (ldsym_undefined (name
))
2700 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2702 /* Add a symbol called _end */
2703 asymbol
*def
= create_symbol (name
,
2704 BSF_GLOBAL
| BSF_EXPORT
,
2707 if (s
!= (asection
*) NULL
)
2709 def
->value
= s
->vma
+ s
->_raw_size
;
2719 DEFUN (lang_statement_append
, (list
, element
, field
),
2720 lang_statement_list_type
* list AND
2721 lang_statement_union_type
* element AND
2722 lang_statement_union_type
** field
)
2724 *(list
->tail
) = element
;
2728 /* Set the output format type */
2730 DEFUN (lang_add_output_format
, (format
),
2733 output_target
= format
;