1 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
39 PROTO(static void, print_statements
,(void));
40 PROTO(static void, print_statement
,(lang_statement_union_type
*,
41 lang_output_section_statement_type
*));
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_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
= {0};
74 CONST
char *entry_symbol
= 0;
75 bfd_size_type largest_section
= 0;
76 boolean lang_has_input_file
= false;
77 lang_output_section_statement_type
*create_object_symbols
= 0;
78 boolean had_output_filename
= false;
79 boolean lang_float_flag
= false;
81 extern char *default_target
;
83 extern unsigned int undefined_global_sym_count
;
84 extern char *current_file
;
85 extern bfd
*output_bfd
;
86 extern enum bfd_architecture ldfile_output_architecture
;
87 extern unsigned long ldfile_output_machine
;
88 extern char *ldfile_output_machine_name
;
89 extern ldsym_type
*symbol_head
;
90 extern unsigned int commons_pending
;
91 extern args_type command_line
;
92 extern ld_config_type config
;
93 extern boolean had_script
;
94 extern boolean write_map
;
100 #define cat(a,b) a/**/b
103 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
105 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
107 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
109 void EXFUN(lang_add_data
,( int type
, union etree_union
*exp
));
112 DEFUN(print_size
,(value
),
115 fprintf(config
.map_file
, "%5x", (unsigned)value
);
118 DEFUN(print_alignment
,(value
),
121 fprintf(config
.map_file
, "2**%1u",value
);
124 DEFUN(print_fill
,(value
),
127 fprintf(config
.map_file
, "%04x",(unsigned)value
);
132 DEFUN(print_section
,(name
),
133 CONST
char *CONST name
)
135 fprintf(config
.map_file
, "%*s", -longest_section_name
, name
);
138 /*----------------------------------------------------------------------
139 lang_for_each_statement walks the parse tree and calls the provided
140 function for each node
144 DEFUN(lang_for_each_statement_worker
,(func
, s
),
146 lang_statement_union_type
*s
)
148 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
152 switch (s
->header
.type
) {
153 case lang_constructors_statement_enum
:
154 lang_for_each_statement_worker(func
, constructor_list
.head
);
156 case lang_output_section_statement_enum
:
157 lang_for_each_statement_worker
159 s
->output_section_statement
.children
.head
);
161 case lang_wild_statement_enum
:
162 lang_for_each_statement_worker
164 s
->wild_statement
.children
.head
);
166 case lang_data_statement_enum
:
167 case lang_object_symbols_statement_enum
:
168 case lang_output_statement_enum
:
169 case lang_target_statement_enum
:
170 case lang_input_section_enum
:
171 case lang_input_statement_enum
:
172 case lang_fill_statement_enum
:
173 case lang_assignment_statement_enum
:
174 case lang_padding_statement_enum
:
175 case lang_address_statement_enum
:
185 DEFUN(lang_for_each_statement
,(func
),
188 lang_for_each_statement_worker(func
,
189 statement_list
.head
);
191 /*----------------------------------------------------------------------*/
193 DEFUN(lang_list_init
,(list
),
194 lang_statement_list_type
*list
)
196 list
->head
= (lang_statement_union_type
*)NULL
;
197 list
->tail
= &list
->head
;
201 /*----------------------------------------------------------------------
203 build a new statement node for the parse tree
208 lang_statement_union_type
*
209 DEFUN(new_statement
,(type
, size
, list
),
210 enum statement_enum type AND
211 bfd_size_type size AND
212 lang_statement_list_type
*list
)
214 lang_statement_union_type
*new = (lang_statement_union_type
*)
216 new->header
.type
= type
;
217 new->header
.next
= (lang_statement_union_type
*)NULL
;
218 lang_statement_append(list
, new, &new->header
.next
);
223 Build a new input file node for the language. There are several ways
224 in which we treat an input file, eg, we only look at symbols, or
225 prefix it with a -l etc.
227 We can be supplied with requests for input files more than once;
228 they may, for example be split over serveral lines like foo.o(.text)
229 foo.o(.data) etc, so when asked for a file we check that we havn't
230 got it already so we don't duplicate the bfd.
233 static lang_input_statement_type
*
234 DEFUN(new_afile
, (name
, file_type
, target
),
235 CONST
char *CONST name AND
236 CONST lang_input_file_enum_type file_type AND
237 CONST
char *CONST target
)
239 lang_input_statement_type
*p
= new_stat(lang_input_statement
,
241 lang_has_input_file
= true;
244 case lang_input_file_is_symbols_only_enum
:
246 p
->is_archive
=false;
248 p
->local_sym_name
= name
;
249 p
->just_syms_flag
= true;
250 p
->search_dirs_flag
= false;
252 case lang_input_file_is_fake_enum
:
254 p
->is_archive
=false;
256 p
->local_sym_name
= name
;
257 p
->just_syms_flag
= false;
258 p
->search_dirs_flag
=false;
260 case lang_input_file_is_l_enum
:
261 p
->is_archive
= true;
264 p
->local_sym_name
= concat("-l",name
,"");
265 p
->just_syms_flag
= false;
266 p
->search_dirs_flag
= true;
268 case lang_input_file_is_search_file_enum
:
269 case lang_input_file_is_marker_enum
:
271 p
->is_archive
=false;
273 p
->local_sym_name
= name
;
274 p
->just_syms_flag
= false;
275 p
->search_dirs_flag
=true;
277 case lang_input_file_is_file_enum
:
279 p
->is_archive
=false;
281 p
->local_sym_name
= name
;
282 p
->just_syms_flag
= false;
283 p
->search_dirs_flag
=false;
288 p
->asymbols
= (asymbol
**)NULL
;
289 p
->superfile
= (lang_input_statement_type
*)NULL
;
290 p
->next_real_file
= (lang_statement_union_type
*)NULL
;
291 p
->next
= (lang_statement_union_type
*)NULL
;
293 p
->common_output_section
= (asection
*)NULL
;
294 lang_statement_append(&input_file_chain
,
295 (lang_statement_union_type
*)p
,
302 lang_input_statement_type
*
303 DEFUN(lang_add_input_file
,(name
, file_type
, target
),
305 lang_input_file_enum_type file_type AND
308 /* Look it up or build a new one */
309 lang_has_input_file
= true;
311 lang_input_statement_type
*p
;
313 for (p
= (lang_input_statement_type
*)input_file_chain
.head
;
314 p
!= (lang_input_statement_type
*)NULL
;
315 p
= (lang_input_statement_type
*)(p
->next_real_file
))
317 /* Sometimes we have incomplete entries in here */
318 if (p
->filename
!= (char *)NULL
) {
319 if(strcmp(name
,p
->filename
) == 0) return p
;
324 return new_afile(name
, file_type
, target
);
328 /* Build enough state so that the parser can build its tree */
330 DEFUN_VOID(lang_init
)
333 stat_ptr
= &statement_list
;
334 lang_list_init(stat_ptr
);
336 lang_list_init(&input_file_chain
);
337 lang_list_init(&lang_output_section_statement
);
338 lang_list_init(&file_chain
);
339 first_file
= lang_add_input_file((char *)NULL
,
340 lang_input_file_is_marker_enum
,
345 /*----------------------------------------------------------------------
346 A region is an area of memory declared with the
347 MEMORY { name:org=exp, len=exp ... }
350 We maintain a list of all the regions here
352 If no regions are specified in the script, then the default is used
353 which is created when looked up to be the entire data space
356 static lang_memory_region_type
*lang_memory_region_list
;
357 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
359 lang_memory_region_type
*
360 DEFUN(lang_memory_region_lookup
,(name
),
361 CONST
char *CONST name
)
364 lang_memory_region_type
*p
= lang_memory_region_list
;
365 for (p
= lang_memory_region_list
;
366 p
!= ( lang_memory_region_type
*)NULL
;
368 if (strcmp(p
->name
, name
) == 0) {
372 if (strcmp(name
,"*default*")==0) {
373 /* This is the default region, dig out first one on the list */
374 if (lang_memory_region_list
!= (lang_memory_region_type
*)NULL
){
375 return lang_memory_region_list
;
379 lang_memory_region_type
*new =
380 (lang_memory_region_type
*)ldmalloc((bfd_size_type
)(sizeof(lang_memory_region_type
)));
381 new->name
= buystring(name
);
382 new->next
= (lang_memory_region_type
*)NULL
;
384 *lang_memory_region_list_tail
= new;
385 lang_memory_region_list_tail
= &new->next
;
394 lang_output_section_statement_type
*
395 DEFUN(lang_output_section_find
,(name
),
396 CONST
char * CONST name
)
398 lang_statement_union_type
*u
;
399 lang_output_section_statement_type
*lookup
;
401 for (u
= lang_output_section_statement
.head
;
402 u
!= (lang_statement_union_type
*)NULL
;
405 lookup
= &u
->output_section_statement
;
406 if (strcmp(name
, lookup
->name
)==0) {
410 return (lang_output_section_statement_type
*)NULL
;
413 lang_output_section_statement_type
*
414 DEFUN(lang_output_section_statement_lookup
,(name
),
415 CONST
char * CONST name
)
417 lang_output_section_statement_type
*lookup
;
418 lookup
=lang_output_section_find(name
);
419 if (lookup
== (lang_output_section_statement_type
*)NULL
) {
421 lookup
=(lang_output_section_statement_type
*)
422 new_stat(lang_output_section_statement
, stat_ptr
);
423 lookup
->region
= (lang_memory_region_type
*)NULL
;
425 lookup
->block_value
= 1;
428 lookup
->next
= (lang_statement_union_type
*)NULL
;
429 lookup
->bfd_section
= (asection
*)NULL
;
430 lookup
->processed
= false;
431 lookup
->addr_tree
= (etree_type
*)NULL
;
432 lang_list_init(&lookup
->children
);
434 lang_statement_append(&lang_output_section_statement
,
435 (lang_statement_union_type
*)lookup
,
446 DEFUN(print_flags
, ( ignore_flags
),
449 fprintf(config
.map_file
, "(");
451 if (flags
->flag_read
) fprintf(outfile
,"R");
452 if (flags
->flag_write
) fprintf(outfile
,"W");
453 if (flags
->flag_executable
) fprintf(outfile
,"X");
454 if (flags
->flag_loadable
) fprintf(outfile
,"L");
456 fprintf(config
.map_file
, ")");
462 lang_memory_region_type
*m
;
463 fprintf(config
.map_file
,"**MEMORY CONFIGURATION**\n\n");
465 fprintf(config
.map_file
,"name\t\torigin\t\tlength\t\tattributes\n");
467 fprintf(config
.map_file
,"name\t\torigin length\t\tattributes\n");
469 for (m
= lang_memory_region_list
;
470 m
!= (lang_memory_region_type
*)NULL
;
473 fprintf(config
.map_file
,"%-16s", m
->name
);
474 print_address(m
->origin
);
476 print_address(m
->length
);
478 print_flags( &m
->flags
);
479 fprintf(config
.map_file
,"\n");
481 fprintf(config
.map_file
,"\n\n**LINK EDITOR MEMORY MAP**\n\n");
482 fprintf(config
.map_file
,"output input virtual\n");
483 fprintf(config
.map_file
,"section section address tsize\n\n");
494 lang_output_section_statement_type
*s
)
496 asection
*section
= bfd_get_section_by_name(output_bfd
, s
->name
);
497 section_userdata_type
*new =
498 (section_userdata_type
*)
499 ldmalloc((bfd_size_type
)(sizeof(section_userdata_type
)));
501 s
->bfd_section
= bfd_get_section_by_name(output_bfd
, s
->name
);
502 if (s
->bfd_section
== (asection
*)NULL
)
503 s
->bfd_section
= bfd_make_section(output_bfd
, s
->name
);
504 if (s
->bfd_section
== (asection
*)NULL
) {
505 einfo("%P%F output format %s cannot represent section called %s\n",
506 output_bfd
->xvec
->name
, s
->name
);
508 s
->bfd_section
->output_section
= s
->bfd_section
;
509 /* s->bfd_section->flags = s->flags;*/
511 /* We initialize an output sections output offset to minus its own */
512 /* vma to allow us to output a section through itself */
513 s
->bfd_section
->output_offset
= 0;
514 get_userdata( s
->bfd_section
) = (PTR
)new;
518 /***********************************************************************
521 These expand statements like *(.text) and foo.o to a list of
522 explicit actions, like foo.o(.text), bar.o(.text) and
525 The toplevel routine, wild, takes a statement, section, file and
526 target. If either the section or file is null it is taken to be the
527 wildcard. Seperate lang_input_section statements are created for
528 each part of the expanstion, and placed after the statement provided.
533 DEFUN(wild_doit
,(ptr
, section
, output
, file
),
534 lang_statement_list_type
*ptr AND
535 asection
*section AND
536 lang_output_section_statement_type
*output AND
537 lang_input_statement_type
*file
)
539 if(output
->bfd_section
== (asection
*)NULL
)
544 if (section
!= (asection
*)NULL
545 && section
->output_section
== (asection
*)NULL
) {
546 /* Add a section reference to the list */
547 lang_input_section_type
*new = new_stat(lang_input_section
, ptr
);
549 new->section
= section
;
551 section
->output_section
= output
->bfd_section
;
552 section
->output_section
->flags
|= section
->flags
;
553 if (section
->alignment_power
> output
->bfd_section
->alignment_power
) {
554 output
->bfd_section
->alignment_power
= section
->alignment_power
;
560 DEFUN(our_bfd_get_section_by_name
,(abfd
, section
),
564 return bfd_get_section_by_name(abfd
, section
);
568 DEFUN(wild_section
,(ptr
, section
, file
, output
),
569 lang_wild_statement_type
*ptr AND
570 CONST
char *section AND
571 lang_input_statement_type
*file AND
572 lang_output_section_statement_type
*output
)
575 if (file
->just_syms_flag
== false) {
576 if (section
== (char *)NULL
) {
577 /* Do the creation to all sections in the file */
578 for (s
= file
->the_bfd
->sections
; s
!= (asection
*)NULL
; s
=s
->next
) {
579 wild_doit(&ptr
->children
, s
, output
, file
);
583 /* Do the creation to the named section only */
584 wild_doit(&ptr
->children
,
585 our_bfd_get_section_by_name(file
->the_bfd
, section
),
592 /* passed a file name (which must have been seen already and added to
593 the statement tree. We will see if it has been opened already and
594 had its symbols read. If not then we'll read it.
596 Archives are pecuilar here. We may open them once, but if they do
597 not define anything we need at the time, they won't have all their
598 symbols read. If we need them later, we'll have to redo it.
601 lang_input_statement_type
*
602 DEFUN(lookup_name
,(name
),
603 CONST
char * CONST name
)
605 lang_input_statement_type
*search
;
606 for(search
= (lang_input_statement_type
*)input_file_chain
.head
;
607 search
!= (lang_input_statement_type
*)NULL
;
608 search
= (lang_input_statement_type
*)search
->next_real_file
)
610 if (search
->filename
== (char *)NULL
&& name
== (char *)NULL
) {
613 if (search
->filename
!= (char *)NULL
&& name
!= (char *)NULL
) {
614 if (strcmp(search
->filename
, name
) == 0) {
615 ldmain_open_file_read_symbol(search
);
621 /* There isn't an afile entry for this file yet, this must be
622 because the name has only appeared inside a load script and not
623 on the command line */
624 search
= new_afile(name
, lang_input_file_is_file_enum
, default_target
);
625 ldmain_open_file_read_symbol(search
);
632 DEFUN(wild
,(s
, section
, file
, target
, output
),
633 lang_wild_statement_type
*s AND
634 CONST
char *CONST section AND
635 CONST
char *CONST file AND
636 CONST
char *CONST target AND
637 lang_output_section_statement_type
*output
)
639 lang_input_statement_type
*f
;
640 if (file
== (char *)NULL
) {
641 /* Perform the iteration over all files in the list */
642 for (f
= (lang_input_statement_type
*)file_chain
.head
;
643 f
!= (lang_input_statement_type
*)NULL
;
644 f
= (lang_input_statement_type
*)f
->next
) {
645 wild_section(s
, section
, f
, output
);
649 /* Perform the iteration over a single file */
650 wild_section( s
, section
, lookup_name(file
), output
);
652 if (section
!= (char *)NULL
653 && strcmp(section
,"COMMON") == 0
654 && default_common_section
== (lang_output_section_statement_type
*)NULL
)
656 /* Remember the section that common is going to incase we later
657 get something which doesn't know where to put it */
658 default_common_section
= output
;
663 read in all the files
666 DEFUN(open_output
,(name
),
667 CONST
char *CONST name
)
669 extern CONST
char *output_filename
;
671 if (output_target
== (char *)NULL
) {
672 if (current_target
!= (char *)NULL
)
673 output_target
= current_target
;
675 output_target
= default_target
;
677 output
= bfd_openw(name
, output_target
);
678 output_filename
= name
;
680 if (output
== (bfd
*)NULL
)
682 if (bfd_error
== invalid_target
) {
683 einfo("%P%F target %s not found\n", output_target
);
685 einfo("%P%F problem opening output file %s, %E", name
);
688 /* output->flags |= D_PAGED;*/
689 bfd_set_format(output
, bfd_object
);
697 DEFUN(ldlang_open_output
,(statement
),
698 lang_statement_union_type
*statement
)
700 switch (statement
->header
.type
)
702 case lang_output_statement_enum
:
703 output_bfd
= open_output(statement
->output_statement
.name
);
704 ldemul_set_output_arch();
705 if (config
.magic_demand_paged
&& !config
.relocateable_output
)
706 output_bfd
->flags
|= D_PAGED
;
708 output_bfd
->flags
&= ~D_PAGED
;
709 if (config
.text_read_only
)
710 output_bfd
->flags
|= WP_TEXT
;
712 output_bfd
->flags
&= ~WP_TEXT
;
715 case lang_target_statement_enum
:
716 current_target
= statement
->target_statement
.target
;
724 DEFUN(open_input_bfds
,(statement
),
725 lang_statement_union_type
*statement
)
727 switch (statement
->header
.type
)
729 case lang_target_statement_enum
:
730 current_target
= statement
->target_statement
.target
;
732 case lang_wild_statement_enum
:
733 /* Maybe we should load the file's symbols */
734 if (statement
->wild_statement
.filename
)
736 (void) lookup_name(statement
->wild_statement
.filename
);
739 case lang_input_statement_enum
:
740 if (statement
->input_statement
.real
== true)
742 statement
->input_statement
.target
= current_target
;
743 lookup_name(statement
->input_statement
.filename
);
750 /* If there are [COMMONS] statements, put a wild one into the bss section */
753 lang_reasonable_defaults()
756 lang_output_section_statement_lookup(".text");
757 lang_output_section_statement_lookup(".data");
759 default_common_section
=
760 lang_output_section_statement_lookup(".bss");
763 if (placed_commons
== false) {
764 lang_wild_statement_type
*new =
765 new_stat(lang_wild_statement
,
766 &default_common_section
->children
);
767 new->section_name
= "COMMON";
768 new->filename
= (char *)NULL
;
769 lang_list_init(&new->children
);
776 Add the supplied name to the symbol table as an undefined reference.
777 Remove items from the chain as we open input bfds
779 typedef struct ldlang_undef_chain_list
{
780 struct ldlang_undef_chain_list
*next
;
782 } ldlang_undef_chain_list_type
;
784 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
787 DEFUN(ldlang_add_undef
,(name
),
788 CONST
char *CONST name
)
790 ldlang_undef_chain_list_type
*new =
791 (ldlang_undef_chain_list_type
792 *)ldmalloc((bfd_size_type
)(sizeof(ldlang_undef_chain_list_type
)));
794 new->next
= ldlang_undef_chain_list_head
;
795 ldlang_undef_chain_list_head
= new;
797 new->name
= buystring(name
);
799 /* Run through the list of undefineds created above and place them
800 into the linker hash table as undefined symbols belonging to the
804 DEFUN_VOID(lang_place_undefineds
)
806 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
807 while (ptr
!= (ldlang_undef_chain_list_type
*)NULL
) {
808 ldsym_type
*sy
= ldsym_get(ptr
->name
);
810 asymbol
**def_ptr
= (asymbol
**)ldmalloc((bfd_size_type
)(sizeof(asymbol
**)));
811 def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
813 def
->name
= ptr
->name
;
814 def
->section
= &bfd_und_section
;
815 Q_enter_global_ref(def_ptr
);
822 /* Copy important data from out internal form to the bfd way. Also
823 create a section for the dummy file
827 DEFUN_VOID(lang_create_output_section_statements
)
829 lang_statement_union_type
*os
;
830 for (os
= lang_output_section_statement
.head
;
831 os
!= (lang_statement_union_type
*)NULL
;
832 os
= os
->output_section_statement
.next
) {
833 lang_output_section_statement_type
*s
=
834 &os
->output_section_statement
;
841 DEFUN_VOID(lang_init_script_file
)
843 script_file
= lang_add_input_file("script file",
844 lang_input_file_is_fake_enum
,
846 script_file
->the_bfd
= bfd_create("script file", output_bfd
);
847 script_file
->symbol_count
= 0;
848 script_file
->the_bfd
->sections
= output_bfd
->sections
;
854 /* Open input files and attatch to output sections */
856 DEFUN(map_input_to_output_sections
,(s
, target
, output_section_statement
),
857 lang_statement_union_type
*s AND
858 CONST
char *target AND
859 lang_output_section_statement_type
*output_section_statement
)
861 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
863 switch (s
->header
.type
) {
866 case lang_wild_statement_enum
:
867 wild(&s
->wild_statement
, s
->wild_statement
.section_name
,
868 s
->wild_statement
.filename
, target
,
869 output_section_statement
);
872 case lang_constructors_statement_enum
:
873 map_input_to_output_sections(constructor_list
.head
,
875 output_section_statement
);
877 case lang_output_section_statement_enum
:
878 map_input_to_output_sections(s
->output_section_statement
.children
.head
,
880 &s
->output_section_statement
);
882 case lang_output_statement_enum
:
884 case lang_target_statement_enum
:
885 target
= s
->target_statement
.target
;
887 case lang_fill_statement_enum
:
888 case lang_input_section_enum
:
889 case lang_object_symbols_statement_enum
:
890 case lang_data_statement_enum
:
891 case lang_assignment_statement_enum
:
892 case lang_padding_statement_enum
:
894 case lang_afile_asection_pair_statement_enum
:
897 case lang_address_statement_enum
:
898 /* Mark the specified section with the supplied address */
900 lang_output_section_statement_type
*os
=
901 lang_output_section_statement_lookup
902 (s
->address_statement
.section_name
);
903 os
->addr_tree
= s
->address_statement
.address
;
904 if (os
->bfd_section
== (asection
*)NULL
) {
905 einfo("%P%F can't set the address of undefined section %s\n",
906 s
->address_statement
.section_name
);
910 case lang_input_statement_enum
:
911 /* A standard input statement, has no wildcards */
912 /* ldmain_open_file_read_symbol(&s->input_statement);*/
923 DEFUN(print_output_section_statement
,(output_section_statement
),
924 lang_output_section_statement_type
*output_section_statement
)
926 asection
*section
= output_section_statement
->bfd_section
;
928 print_section(output_section_statement
->name
);
931 print_dot
= section
->vma
;
935 print_address(section
->vma
);
937 print_size(bfd_get_section_size_before_reloc(section
));
939 print_alignment(section
->alignment_power
);
942 fprintf(config
.map_file
, "%s flags", output_section_statement
->region
->name
);
943 print_flags(stdout
, &output_section_statement
->flags
);
945 if (section
->flags
& SEC_LOAD
)
946 fprintf(config
.map_file
, "load ");
947 if (section
->flags
& SEC_ALLOC
)
948 fprintf(config
.map_file
, "alloc ");
949 if (section
->flags
& SEC_RELOC
)
950 fprintf(config
.map_file
, "reloc ");
951 if (section
->flags
& SEC_HAS_CONTENTS
)
952 fprintf(config
.map_file
, "contents ");
956 fprintf(config
.map_file
, "No attached output section");
959 print_statement(output_section_statement
->children
.head
,
960 output_section_statement
);
965 DEFUN(print_assignment
,(assignment
, output_section
),
966 lang_assignment_statement_type
*assignment AND
967 lang_output_section_statement_type
*output_section
)
969 etree_value_type result
;
974 print_address(print_dot
);
976 result
= exp_fold_tree(assignment
->exp
->assign
.src
,
978 lang_final_phase_enum
,
983 print_address(result
.value
);
987 fprintf(config
.map_file
, "*undefined*");
990 exp_print_tree(assignment
->exp
);
991 fprintf(config
.map_file
, "\n");
995 DEFUN(print_input_statement
,(statm
),
996 lang_input_statement_type
*statm
)
998 if (statm
->filename
!= (char *)NULL
) {
999 fprintf(config
.map_file
, "LOAD %s\n",statm
->filename
);
1004 DEFUN(print_symbol
,(q
),
1008 fprintf(config
.map_file
, " ");
1010 fprintf(config
.map_file
, " ");
1011 print_address(outside_symbol_address(q
));
1012 fprintf(config
.map_file
, " %s", q
->name
? q
->name
: " ");
1017 DEFUN(print_input_section
,(in
),
1018 lang_input_section_type
*in
)
1020 asection
*i
= in
->section
;
1021 int size
= i
->flags
& SEC_HAS_CONTENTS
?
1022 bfd_get_section_size_after_reloc(i
) :
1023 bfd_get_section_size_before_reloc(i
);
1028 fprintf(config
.map_file
, " ");
1029 print_section(i
->name
);
1030 fprintf(config
.map_file
, " ");
1031 if (i
->output_section
) {
1032 print_address(i
->output_section
->vma
+ i
->output_offset
);
1033 fprintf(config
.map_file
, " ");
1035 fprintf(config
.map_file
, " ");
1036 print_alignment(i
->alignment_power
);
1037 fprintf(config
.map_file
, " ");
1040 bfd
*abfd
= in
->ifile
->the_bfd
;
1041 if (in
->ifile
->just_syms_flag
== true) {
1042 fprintf(config
.map_file
, "symbols only ");
1045 fprintf(config
.map_file
, " %s ",abfd
->xvec
->name
);
1046 if(abfd
->my_archive
!= (bfd
*)NULL
) {
1047 fprintf(config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1051 fprintf(config
.map_file
, "%s", abfd
->filename
);
1053 fprintf(config
.map_file
, "(overhead %d bytes)", (int)bfd_alloc_size(abfd
));
1056 /* Find all the symbols in this file defined in this section */
1059 for (p
= in
->ifile
->asymbols
; *p
; p
++) {
1062 if (bfd_get_section(q
) == i
&& q
->flags
& BSF_GLOBAL
) {
1073 print_dot
= outside_section_address(i
) + size
;
1076 fprintf(config
.map_file
, "No output section allocated\n");
1082 DEFUN(print_fill_statement
,(fill
),
1083 lang_fill_statement_type
*fill
)
1085 fprintf(config
.map_file
, "FILL mask ");
1086 print_fill( fill
->fill
);
1090 DEFUN(print_data_statement
,(data
),
1091 lang_data_statement_type
*data
)
1093 /* bfd_vma value; */
1098 /* ASSERT(print_dot == data->output_vma);*/
1100 print_address(data
->output_vma
);
1102 print_address(data
->value
);
1104 switch (data
->type
) {
1106 fprintf(config
.map_file
, "BYTE ");
1107 print_dot
+= BYTE_SIZE
;
1110 fprintf(config
.map_file
, "SHORT ");
1111 print_dot
+= SHORT_SIZE
;
1114 fprintf(config
.map_file
, "LONG ");
1115 print_dot
+= LONG_SIZE
;
1119 exp_print_tree(data
->exp
);
1121 fprintf(config
.map_file
, "\n");
1126 DEFUN(print_padding_statement
,(s
),
1127 lang_padding_statement_type
*s
)
1131 print_section("*fill*");
1133 print_address(s
->output_offset
+ s
->output_section
->vma
);
1135 print_size(s
->size
);
1137 print_fill(s
->fill
);
1142 DEFUN(print_wild_statement
,(w
,os
),
1143 lang_wild_statement_type
*w AND
1144 lang_output_section_statement_type
*os
)
1146 fprintf(config
.map_file
, " from ");
1147 if (w
->filename
!= (char *)NULL
) {
1148 fprintf(config
.map_file
, "%s",w
->filename
);
1151 fprintf(config
.map_file
, "*");
1153 if (w
->section_name
!= (char *)NULL
) {
1154 fprintf(config
.map_file
, "(%s)",w
->section_name
);
1157 fprintf(config
.map_file
, "(*)");
1160 print_statement(w
->children
.head
, os
);
1164 DEFUN(print_statement
,(s
, os
),
1165 lang_statement_union_type
*s AND
1166 lang_output_section_statement_type
*os
)
1170 switch (s
->header
.type
)
1172 case lang_constructors_statement_enum
:
1173 fprintf(config
.map_file
, "constructors:\n");
1174 print_statement(constructor_list
.head
, os
);
1176 case lang_wild_statement_enum
:
1177 print_wild_statement(&s
->wild_statement
, os
);
1180 fprintf(config
.map_file
, "Fail with %d\n",s
->header
.type
);
1183 case lang_address_statement_enum
:
1184 fprintf(config
.map_file
, "address\n");
1187 case lang_object_symbols_statement_enum
:
1188 fprintf(config
.map_file
, "object symbols\n");
1190 case lang_fill_statement_enum
:
1191 print_fill_statement(&s
->fill_statement
);
1193 case lang_data_statement_enum
:
1194 print_data_statement(&s
->data_statement
);
1196 case lang_input_section_enum
:
1197 print_input_section(&s
->input_section
);
1199 case lang_padding_statement_enum
:
1200 print_padding_statement(&s
->padding_statement
);
1202 case lang_output_section_statement_enum
:
1203 print_output_section_statement(&s
->output_section_statement
);
1205 case lang_assignment_statement_enum
:
1206 print_assignment(&s
->assignment_statement
,
1209 case lang_target_statement_enum
:
1210 fprintf(config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1212 case lang_output_statement_enum
:
1213 fprintf(config
.map_file
, "OUTPUT(%s %s)\n",
1214 s
->output_statement
.name
,
1217 case lang_input_statement_enum
:
1218 print_input_statement(&s
->input_statement
);
1220 case lang_afile_asection_pair_statement_enum
:
1230 DEFUN_VOID(print_statements
)
1232 print_statement(statement_list
.head
,
1233 (lang_output_section_statement_type
*)NULL
);
1237 DEFUN(insert_pad
,(this_ptr
, fill
, power
, output_section_statement
, dot
),
1238 lang_statement_union_type
**this_ptr AND
1240 unsigned int power AND
1241 asection
* output_section_statement AND
1244 /* Align this section first to the
1245 input sections requirement, then
1246 to the output section's requirement.
1247 If this alignment is > than any seen before,
1248 then record it too. Perform the alignment by
1249 inserting a magic 'padding' statement.
1252 unsigned int alignment_needed
= align_power(dot
, power
) - dot
;
1254 if (alignment_needed
!= 0)
1256 lang_statement_union_type
*new =
1257 (lang_statement_union_type
*)
1258 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1259 /* Link into existing chain */
1260 new->header
.next
= *this_ptr
;
1262 new->header
.type
= lang_padding_statement_enum
;
1263 new->padding_statement
.output_section
= output_section_statement
;
1264 new->padding_statement
.output_offset
=
1265 dot
- output_section_statement
->vma
;
1266 new->padding_statement
.fill
= fill
;
1267 new->padding_statement
.size
= alignment_needed
;
1271 /* Remember the most restrictive alignment */
1272 if (power
> output_section_statement
->alignment_power
) {
1273 output_section_statement
->alignment_power
= power
;
1275 output_section_statement
->_raw_size
+= alignment_needed
;
1276 return alignment_needed
+ dot
;
1280 /* Work out how much this section will move the dot point */
1282 DEFUN(size_input_section
, (this_ptr
, output_section_statement
, fill
, dot
),
1283 lang_statement_union_type
**this_ptr AND
1284 lang_output_section_statement_type
*output_section_statement AND
1285 unsigned short fill AND
1288 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1289 asection
*i
= is
->section
;
1291 if (is
->ifile
->just_syms_flag
== false) {
1292 dot
= insert_pad(this_ptr
, fill
, i
->alignment_power
,
1293 output_section_statement
->bfd_section
, dot
);
1295 /* remember the largest size so we can malloc the largest area
1296 needed for the output stage. Only remember the size of sections
1297 which we will actually allocate */
1299 (SEC_HAS_CONTENTS
| SEC_LOAD
)) == (SEC_HAS_CONTENTS
| SEC_LOAD
))
1300 && (bfd_get_section_size_before_reloc(i
) > largest_section
)) {
1301 largest_section
= bfd_get_section_size_before_reloc(i
);
1304 /* Remember where in the output section this input section goes */
1306 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1308 /* Mark how big the output section must be to contain this now */
1309 dot
+= bfd_get_section_size_before_reloc(i
);
1310 output_section_statement
->bfd_section
->_raw_size
=
1311 dot
- output_section_statement
->bfd_section
->vma
;
1315 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1322 /* Work out the size of the output sections
1323 from the sizes of the input sections */
1325 DEFUN(lang_size_sections
,(s
, output_section_statement
, prev
, fill
,
1327 lang_statement_union_type
*s AND
1328 lang_output_section_statement_type
* output_section_statement AND
1329 lang_statement_union_type
**prev AND
1330 unsigned short fill AND
1334 /* Size up the sections from their constituent parts */
1335 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1337 switch (s
->header
.type
) {
1340 case lang_output_section_statement_enum
:
1343 lang_output_section_statement_type
*os
=
1344 &(s
->output_section_statement
);
1345 /* The start of a section */
1347 if (os
->addr_tree
== (etree_type
*)NULL
) {
1348 /* No address specified for this section, get one
1349 from the region specification
1351 if (os
->region
== (lang_memory_region_type
*)NULL
) {
1352 os
->region
= lang_memory_region_lookup("*default*");
1354 dot
= os
->region
->current
;
1357 etree_value_type r
;
1358 r
= exp_fold_tree(os
->addr_tree
,
1359 (lang_output_section_statement_type
*)NULL
,
1360 lang_allocating_phase_enum
,
1362 if (r
.valid
== false) {
1363 einfo("%F%S: non constant address expression for section %s\n",
1368 /* The section starts here */
1369 /* First, align to what the section needs */
1371 dot
= align_power(dot
, os
->bfd_section
->alignment_power
);
1372 os
->bfd_section
->vma
= dot
;
1373 os
->bfd_section
->output_offset
= 0;
1375 (void) lang_size_sections(os
->children
.head
, os
, &os
->children
.head
,
1377 /* Ignore the size of the input sections, use the vma and size to */
1381 after
= ALIGN(os
->bfd_section
->vma
+
1382 os
->bfd_section
->size
,
1386 os
->bfd_section
->size
= after
- os
->bfd_section
->vma
;
1387 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->size
;
1388 os
->processed
= true;
1390 /* Replace into region ? */
1391 if (os
->addr_tree
== (etree_type
*)NULL
1392 && os
->region
!=(lang_memory_region_type
*)NULL
) {
1393 os
->region
->current
= dot
;
1398 case lang_constructors_statement_enum
:
1399 dot
= lang_size_sections(constructor_list
.head
,
1400 output_section_statement
,
1401 &s
->wild_statement
.children
.head
,
1406 case lang_data_statement_enum
:
1408 unsigned int size
= 0;
1409 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1410 s
->data_statement
.output_section
=
1411 output_section_statement
->bfd_section
;
1413 switch (s
->data_statement
.type
) {
1426 output_section_statement
->bfd_section
->size
+= size
;
1430 case lang_wild_statement_enum
:
1432 dot
= lang_size_sections(s
->wild_statement
.children
.head
,
1433 output_section_statement
,
1434 &s
->wild_statement
.children
.head
,
1440 case lang_object_symbols_statement_enum
:
1441 create_object_symbols
= output_section_statement
;
1443 case lang_output_statement_enum
:
1444 case lang_target_statement_enum
:
1446 case lang_input_section_enum
:
1447 dot
= size_input_section(prev
,
1448 output_section_statement
,
1449 output_section_statement
->fill
, dot
);
1451 case lang_input_statement_enum
:
1453 case lang_fill_statement_enum
:
1454 fill
= s
->fill_statement
.fill
;
1456 case lang_assignment_statement_enum
:
1458 bfd_vma newdot
= dot
;
1459 exp_fold_tree(s
->assignment_statement
.exp
,
1460 output_section_statement
,
1461 lang_allocating_phase_enum
,
1466 /* We've been moved ! so insert a pad */
1468 lang_statement_union_type
*new =
1469 (lang_statement_union_type
*)
1470 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1471 /* Link into existing chain */
1472 new->header
.next
= *prev
;
1474 new->header
.type
= lang_padding_statement_enum
;
1475 new->padding_statement
.output_section
=
1476 output_section_statement
->bfd_section
;
1477 new->padding_statement
.output_offset
=
1478 dot
- output_section_statement
->bfd_section
->vma
;
1479 new->padding_statement
.fill
= fill
;
1480 new->padding_statement
.size
= newdot
- dot
;
1481 output_section_statement
->bfd_section
->size
+=
1482 new->padding_statement
.size
;
1488 case lang_padding_statement_enum
:
1494 case lang_address_statement_enum
:
1497 prev
= &s
->header
.next
;
1502 /* Sizing happens in two passes, first pass we allocate worst case
1503 stuff. The second pass (if relaxing), we use what we learnt to
1504 change the size of some relocs from worst case to better
1506 static boolean had_relax
;
1509 DEFUN(lang_size_sections
,(s
, output_section_statement
, prev
, fill
,
1511 lang_statement_union_type
*s AND
1512 lang_output_section_statement_type
* output_section_statement AND
1513 lang_statement_union_type
**prev AND
1514 unsigned short fill AND
1518 /* Size up the sections from their constituent parts */
1519 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1521 switch (s
->header
.type
) {
1523 case lang_output_section_statement_enum
:
1526 lang_output_section_statement_type
*os
=
1527 &(s
->output_section_statement
);
1528 /* The start of a section */
1530 if (os
->addr_tree
== (etree_type
*)NULL
) {
1531 /* No address specified for this section, get one
1532 from the region specification
1534 if (os
->region
== (lang_memory_region_type
*)NULL
) {
1535 os
->region
= lang_memory_region_lookup("*default*");
1537 dot
= os
->region
->current
;
1540 etree_value_type r
;
1541 r
= exp_fold_tree(os
->addr_tree
,
1542 (lang_output_section_statement_type
*)NULL
,
1543 lang_allocating_phase_enum
,
1545 if (r
.valid
== false) {
1546 einfo("%F%S: non constant address expression for section %s\n",
1551 /* The section starts here */
1552 /* First, align to what the section needs */
1554 dot
= align_power(dot
, os
->bfd_section
->alignment_power
);
1555 os
->bfd_section
->vma
= dot
;
1556 os
->bfd_section
->output_offset
= 0;
1558 (void) lang_size_sections(os
->children
.head
, os
, &os
->children
.head
,
1559 os
->fill
, dot
, relax
);
1560 /* Ignore the size of the input sections, use the vma and size to */
1564 after
= ALIGN(os
->bfd_section
->vma
+
1565 os
->bfd_section
->_raw_size
,
1569 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1570 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1571 os
->processed
= true;
1573 /* Replace into region ? */
1574 if (os
->addr_tree
== (etree_type
*)NULL
1575 && os
->region
!=(lang_memory_region_type
*)NULL
) {
1576 os
->region
->current
= dot
;
1581 case lang_constructors_statement_enum
:
1582 dot
= lang_size_sections(constructor_list
.head
,
1583 output_section_statement
,
1584 &s
->wild_statement
.children
.head
,
1589 case lang_data_statement_enum
:
1591 unsigned int size
= 0;
1592 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1593 s
->data_statement
.output_section
=
1594 output_section_statement
->bfd_section
;
1596 switch (s
->data_statement
.type
) {
1609 output_section_statement
->bfd_section
->_raw_size
+= size
;
1613 case lang_wild_statement_enum
:
1615 dot
= lang_size_sections(s
->wild_statement
.children
.head
,
1616 output_section_statement
,
1617 &s
->wild_statement
.children
.head
,
1623 case lang_object_symbols_statement_enum
:
1624 create_object_symbols
= output_section_statement
;
1626 case lang_output_statement_enum
:
1627 case lang_target_statement_enum
:
1629 case lang_input_section_enum
:
1635 had_relax
|= relax_section(prev
);
1640 dot
= size_input_section(prev
,
1641 output_section_statement
,
1642 output_section_statement
->fill
, dot
);
1644 case lang_input_statement_enum
:
1646 case lang_fill_statement_enum
:
1647 fill
= s
->fill_statement
.fill
;
1649 case lang_assignment_statement_enum
:
1651 bfd_vma newdot
= dot
;
1652 exp_fold_tree(s
->assignment_statement
.exp
,
1653 output_section_statement
,
1654 lang_allocating_phase_enum
,
1659 /* We've been moved ! so insert a pad */
1661 lang_statement_union_type
*new =
1662 (lang_statement_union_type
*)
1663 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1664 /* Link into existing chain */
1665 new->header
.next
= *prev
;
1667 new->header
.type
= lang_padding_statement_enum
;
1668 new->padding_statement
.output_section
=
1669 output_section_statement
->bfd_section
;
1670 new->padding_statement
.output_offset
=
1671 dot
- output_section_statement
->bfd_section
->vma
;
1672 new->padding_statement
.fill
= fill
;
1673 new->padding_statement
.size
= newdot
- dot
;
1674 output_section_statement
->bfd_section
->_raw_size
+=
1675 new->padding_statement
.size
;
1684 /* This can only get here when relaxing is turned on */
1685 case lang_padding_statement_enum
:
1687 case lang_address_statement_enum
:
1690 prev
= &s
->header
.next
;
1697 DEFUN(lang_do_assignments
,(s
, output_section_statement
, fill
, dot
),
1698 lang_statement_union_type
*s AND
1699 lang_output_section_statement_type
* output_section_statement AND
1700 unsigned short fill AND
1704 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1706 switch (s
->header
.type
) {
1707 case lang_constructors_statement_enum
:
1708 dot
= lang_do_assignments(constructor_list
.head
,
1709 output_section_statement
,
1714 case lang_output_section_statement_enum
:
1716 lang_output_section_statement_type
*os
=
1717 &(s
->output_section_statement
);
1718 dot
= os
->bfd_section
->vma
;
1719 (void) lang_do_assignments(os
->children
.head
, os
, os
->fill
, dot
);
1720 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1723 case lang_wild_statement_enum
:
1725 dot
= lang_do_assignments(s
->wild_statement
.children
.head
,
1726 output_section_statement
,
1731 case lang_object_symbols_statement_enum
:
1732 case lang_output_statement_enum
:
1733 case lang_target_statement_enum
:
1735 case lang_common_statement_enum
:
1738 case lang_data_statement_enum
:
1740 etree_value_type value
;
1741 value
= exp_fold_tree(s
->data_statement
.exp
,
1742 0, lang_final_phase_enum
, dot
, &dot
);
1743 s
->data_statement
.value
= value
.value
;
1744 if (value
.valid
== false) einfo("%F%P: Invalid data statement\n");
1746 switch (s
->data_statement
.type
) {
1758 case lang_input_section_enum
:
1760 asection
*in
= s
->input_section
.section
;
1761 dot
+= bfd_get_section_size_before_reloc(in
);
1765 case lang_input_statement_enum
:
1767 case lang_fill_statement_enum
:
1768 fill
= s
->fill_statement
.fill
;
1770 case lang_assignment_statement_enum
:
1772 exp_fold_tree(s
->assignment_statement
.exp
,
1773 output_section_statement
,
1774 lang_final_phase_enum
,
1780 case lang_padding_statement_enum
:
1781 dot
+= s
->padding_statement
.size
;
1786 case lang_address_statement_enum
:
1797 DEFUN_VOID(lang_relocate_globals
)
1801 Each ldsym_type maintains a chain of pointers to asymbols which
1802 references the definition. Replace each pointer to the referenence
1803 with a pointer to only one place, preferably the definition. If
1804 the defintion isn't available then the common symbol, and if
1805 there isn't one of them then choose one reference.
1808 FOR_EACH_LDSYM(lgs
) {
1810 if (lgs
->sdefs_chain
) {
1811 it
= *(lgs
->sdefs_chain
);
1813 else if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1814 it
= *(lgs
->scoms_chain
);
1816 else if (lgs
->srefs_chain
!= (asymbol
**)NULL
) {
1817 it
= *(lgs
->srefs_chain
);
1820 /* This can happen when the command line asked for a symbol to
1822 it
= (asymbol
*)NULL
;
1824 if (it
!= (asymbol
*)NULL
)
1826 asymbol
**ptr
= lgs
->srefs_chain
;;
1827 if (lgs
->flags
& SYM_WARNING
)
1829 produce_warnings(lgs
, it
);
1832 while (ptr
!= (asymbol
**)NULL
) {
1833 asymbol
*ref
= *ptr
;
1835 ptr
= (asymbol
**)(ref
->udata
);
1844 DEFUN_VOID(lang_finish
)
1848 if (entry_symbol
== (char *)NULL
) {
1849 /* No entry has been specified, look for start */
1850 entry_symbol
= "start";
1852 lgs
= ldsym_get_soft(entry_symbol
);
1853 if (lgs
&& lgs
->sdefs_chain
) {
1854 asymbol
*sy
= *(lgs
->sdefs_chain
);
1855 /* We can set the entry address*/
1856 bfd_set_start_address(output_bfd
,
1857 outside_symbol_address(sy
));
1861 /* Can't find anything reasonable,
1862 use the first address in the text section
1864 asection
*ts
= bfd_get_section_by_name(output_bfd
, ".text");
1866 bfd_set_start_address(output_bfd
, ts
->vma
);
1871 /* By now we know the target architecture, and we may have an */
1872 /* ldfile_output_machine_name */
1874 DEFUN_VOID(lang_check
)
1876 lang_statement_union_type
*file
;
1879 unsigned long input_machine
;
1880 enum bfd_architecture input_architecture
;
1882 CONST bfd_arch_info_type
*compatible
;
1884 for (file
= file_chain
.head
;
1885 file
!= (lang_statement_union_type
*)NULL
;
1886 file
=file
->input_statement
.next
)
1888 unsigned long ldfile_new_output_machine
= 0;
1889 enum bfd_architecture ldfile_new_output_architecture
= bfd_arch_unknown
;
1891 input_bfd
= file
->input_statement
.the_bfd
;
1893 input_machine
= bfd_get_mach(input_bfd
);
1894 input_architecture
= bfd_get_arch(input_bfd
);
1897 /* Inspect the architecture and ensure we're linking like with
1900 compatible
=bfd_arch_get_compatible(input_bfd
,
1905 ldfile_output_machine
= compatible
->mach
;
1906 ldfile_output_architecture
= compatible
->arch
;
1911 info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1912 bfd_printable_name(input_bfd
), input_bfd
,
1913 bfd_printable_name(output_bfd
));
1915 bfd_set_arch_mach(output_bfd
,
1916 ldfile_new_output_architecture
,
1917 ldfile_new_output_machine
);
1925 * run through all the global common symbols and tie them
1926 * to the output section requested.
1928 As an experiment we do this 4 times, once for all the byte sizes,
1929 then all the two bytes, all the four bytes and then everything else
1933 DEFUN_VOID(lang_common
)
1937 if (config
.relocateable_output
== false ||
1938 command_line
.force_common_definition
== true) {
1939 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<=1) {
1940 for (lgs
= symbol_head
;
1941 lgs
!= (ldsym_type
*)NULL
;
1945 unsigned int power_of_two
;
1948 if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1949 com
= *(lgs
->scoms_chain
);
1978 if (config
.sort_common
== false || align
== power
) {
1979 /* Change from a common symbol into a definition of
1981 lgs
->sdefs_chain
= lgs
->scoms_chain
;
1982 lgs
->scoms_chain
= (asymbol
**)NULL
;
1984 /* Point to the correct common section */
1986 ((lang_input_statement_type
*)
1987 (com
->the_bfd
->usrdata
))->common_section
;
1988 /* Fix the size of the common section */
1990 com
->section
->_raw_size
=
1991 ALIGN(com
->section
->_raw_size
, align
);
1993 /* Remember if this is the biggest alignment ever seen */
1994 if (power_of_two
> com
->section
->alignment_power
) {
1995 com
->section
->alignment_power
= power_of_two
;
1998 /* Symbol stops being common and starts being global, but
1999 we remember that it was common once. */
2001 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
2002 com
->value
= com
->section
->_raw_size
;
2006 fprintf (config
.map_file
, "Allocating common %s: %x at %x %s\n",
2009 (unsigned) com
->value
,
2010 com
->the_bfd
->filename
);
2013 com
->section
->_raw_size
+= size
;
2026 run through the input files and ensure that every input
2027 section has somewhere to go. If one is found without
2028 a destination then create an input request and place it
2029 into the statement tree.
2033 DEFUN_VOID(lang_place_orphans
)
2035 lang_input_statement_type
*file
;
2036 for (file
= (lang_input_statement_type
*)file_chain
.head
;
2037 file
!= (lang_input_statement_type
*)NULL
;
2038 file
= (lang_input_statement_type
*)file
->next
) {
2040 for (s
= file
->the_bfd
->sections
;
2041 s
!= (asection
*)NULL
;
2043 if ( s
->output_section
== (asection
*)NULL
) {
2044 /* This section of the file is not attatched, root
2045 around for a sensible place for it to go */
2047 if (file
->common_section
== s
) {
2048 /* This is a lonely common section which must
2049 have come from an archive. We attatch to the
2050 section with the wildcard */
2051 if (config
.relocateable_output
!= true
2052 && command_line
.force_common_definition
== false) {
2053 if (default_common_section
==
2054 (lang_output_section_statement_type
*)NULL
) {
2055 info("%P: No [COMMON] command, defaulting to .bss\n");
2057 default_common_section
=
2058 lang_output_section_statement_lookup(".bss");
2061 wild_doit(&default_common_section
->children
, s
,
2062 default_common_section
, file
);
2066 lang_output_section_statement_type
*os
=
2067 lang_output_section_statement_lookup(s
->name
);
2069 wild_doit(&os
->children
, s
, os
, file
);
2078 DEFUN(lang_set_flags
,(ptr
, flags
),
2082 boolean state
= true;
2086 if (*flags
== '!') {
2093 /* ptr->flag_read = state; */
2096 /* ptr->flag_write = state; */
2099 /* ptr->flag_executable= state;*/
2103 /* ptr->flag_loadable= state;*/
2106 einfo("%P%F illegal syntax in flags\n");
2116 DEFUN(lang_for_each_file
,(func
),
2117 PROTO(void, (*func
),(lang_input_statement_type
*)))
2119 lang_input_statement_type
*f
;
2120 for (f
= (lang_input_statement_type
*)file_chain
.head
;
2121 f
!= (lang_input_statement_type
*)NULL
;
2122 f
= (lang_input_statement_type
*)f
->next
)
2130 DEFUN(lang_for_each_input_section
, (func
),
2131 PROTO(void ,(*func
),(bfd
*ab
, asection
*as
)))
2133 lang_input_statement_type
*f
;
2134 for (f
= (lang_input_statement_type
*)file_chain
.head
;
2135 f
!= (lang_input_statement_type
*)NULL
;
2136 f
= (lang_input_statement_type
*)f
->next
)
2139 for (s
= f
->the_bfd
->sections
;
2140 s
!= (asection
*)NULL
;
2142 func(f
->the_bfd
, s
);
2150 DEFUN(ldlang_add_file
,(entry
),
2151 lang_input_statement_type
*entry
)
2154 lang_statement_append(&file_chain
,
2155 (lang_statement_union_type
*)entry
,
2162 DEFUN(lang_add_output
,(name
),
2165 lang_output_statement_type
*new = new_stat(lang_output_statement
,
2168 had_output_filename
= true;
2172 static lang_output_section_statement_type
*current_section
;
2175 DEFUN(lang_enter_output_section_statement
,
2176 (output_section_statement_name
,
2180 char *output_section_statement_name AND
2181 etree_type
*address_exp AND
2183 bfd_vma block_value
)
2185 lang_output_section_statement_type
*os
;
2188 lang_output_section_statement_lookup(output_section_statement_name
);
2192 /* Add this statement to tree */
2193 /* add_statement(lang_output_section_statement_enum,
2194 output_section_statement);*/
2195 /* Make next things chain into subchain of this */
2197 if (os
->addr_tree
==
2198 (etree_type
*)NULL
) {
2203 os
->block_value
= block_value
;
2204 stat_ptr
= & os
->children
;
2210 DEFUN_VOID(lang_final
)
2212 if (had_output_filename
== false) {
2213 extern CONST
char *output_filename
;
2214 lang_add_output(output_filename
);
2220 /* Reset the current counters in the regions */
2222 DEFUN_VOID(reset_memory_regions
)
2224 lang_memory_region_type
*p
= lang_memory_region_list
;
2225 for (p
= lang_memory_region_list
;
2226 p
!= ( lang_memory_region_type
*)NULL
;
2229 p
->current
= p
->origin
;
2235 DEFUN(create_symbol
,(name
, flags
, section
),
2236 CONST
char *name AND
2240 extern lang_input_statement_type
*script_file
;
2241 asymbol
**def_ptr
= (asymbol
**)ldmalloc((bfd_size_type
)(sizeof(asymbol
**)));
2242 /* Add this definition to script file */
2243 asymbol
*def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
2244 def
->name
= buystring(name
);
2247 def
->section
= section
;
2250 Q_enter_global_ref(def_ptr
);
2257 DEFUN_VOID(lang_process
)
2259 if (had_script
== false) {
2260 parse_line(ldemul_get_script());
2262 lang_reasonable_defaults();
2263 current_target
= default_target
;
2265 lang_for_each_statement(ldlang_open_output
); /* Open the output file */
2266 /* For each output section statement, create a section in the output
2268 lang_create_output_section_statements();
2270 /* Create a dummy bfd for the script */
2271 lang_init_script_file();
2273 /* Add to the hash table all undefineds on the command line */
2274 lang_place_undefineds();
2276 /* Create a bfd for each input file */
2277 current_target
= default_target
;
2278 lang_for_each_statement(open_input_bfds
);
2280 common_section
.userdata
= (PTR
)&common_section_userdata
;
2283 /* Run through the contours of the script and attatch input sections
2284 to the correct output sections
2286 find_constructors();
2287 map_input_to_output_sections(statement_list
.head
, (char *)NULL
,
2288 ( lang_output_section_statement_type
*)NULL
);
2291 /* Find any sections not attatched explicitly and handle them */
2292 lang_place_orphans();
2294 /* Size up the common data */
2297 ldemul_before_allocation();
2300 /* Size up the sections */
2301 lang_size_sections(statement_list
.head
,
2302 (lang_output_section_statement_type
*)NULL
,
2303 &(statement_list
.head
), 0, (bfd_vma
)0, false);
2306 /* Move the global symbols around */
2307 lang_relocate_globals();
2309 /* Now run around and relax if we can */
2310 if (command_line
.relax
)
2312 reset_memory_regions();
2320 lang_size_sections(statement_list
.head
,
2321 (lang_output_section_statement_type
*)NULL
,
2322 &(statement_list
.head
), 0, (bfd_vma
)0, true);
2323 /* FIXME. Until the code in relax is fixed so that it only reads in
2324 stuff once, we cant iterate since there is no way for the linker to
2325 know what has been patched and what hasn't */
2336 /* See if anything special should be done now we know how big
2338 ldemul_after_allocation();
2340 /* Do all the assignments, now that we know the final restingplaces
2341 of all the symbols */
2343 lang_do_assignments(statement_list
.head
,
2344 (lang_output_section_statement_type
*)NULL
,
2348 /* Make sure that we're not mixing architectures */
2357 /* EXPORTED TO YACC */
2360 DEFUN(lang_add_wild
,(section_name
, filename
),
2361 CONST
char *CONST section_name AND
2362 CONST
char *CONST filename
)
2364 lang_wild_statement_type
*new = new_stat(lang_wild_statement
,
2367 if (section_name
!= (char *)NULL
&& strcmp(section_name
,"COMMON") == 0)
2369 placed_commons
= true;
2371 if (filename
!= (char *)NULL
) {
2372 lang_has_input_file
= true;
2374 new->section_name
= section_name
;
2375 new->filename
= filename
;
2376 lang_list_init(&new->children
);
2379 DEFUN(lang_section_start
,(name
, address
),
2380 CONST
char *name AND
2381 etree_type
*address
)
2383 lang_address_statement_type
*ad
=new_stat(lang_address_statement
, stat_ptr
);
2384 ad
->section_name
= name
;
2385 ad
->address
= address
;
2389 DEFUN(lang_add_entry
,(name
),
2392 entry_symbol
= name
;
2396 DEFUN(lang_add_target
,(name
),
2399 lang_target_statement_type
*new = new_stat(lang_target_statement
,
2409 DEFUN(lang_add_map
,(name
),
2415 map_option_f
= true;
2423 DEFUN(lang_add_fill
,(exp
),
2426 lang_fill_statement_type
*new = new_stat(lang_fill_statement
,
2432 DEFUN(lang_add_data
,(type
, exp
),
2434 union etree_union
*exp
)
2437 lang_data_statement_type
*new = new_stat(lang_data_statement
,
2444 DEFUN(lang_add_assignment
,(exp
),
2447 lang_assignment_statement_type
*new = new_stat(lang_assignment_statement
,
2453 DEFUN(lang_add_attribute
,(attribute
),
2454 enum statement_enum attribute
)
2456 new_statement(attribute
, sizeof(lang_statement_union_type
),stat_ptr
);
2462 DEFUN(lang_startup
,(name
),
2465 if (startup_file
!= (char *)NULL
) {
2466 einfo("%P%FMultiple STARTUP files\n");
2468 first_file
->filename
= name
;
2469 first_file
->local_sym_name
= name
;
2474 DEFUN(lang_float
,(maybe
),
2477 lang_float_flag
= maybe
;
2481 DEFUN(lang_leave_output_section_statement
,(fill
, memspec
),
2483 CONST
char *memspec
)
2485 current_section
->fill
= fill
;
2486 current_section
->region
= lang_memory_region_lookup(memspec
);
2487 stat_ptr
= &statement_list
;
2489 /* We remember if we are closing a .data section, since we use it to
2490 store constructors in */
2491 if (strcmp(current_section
->name
, ".data") ==0) {
2492 end_of_data_section_statement_list
= statement_list
;
2497 Create an absolute symbol with the given name with the value of the
2498 address of first byte of the section named.
2500 If the symbol already exists, then do nothing.
2503 DEFUN(lang_abs_symbol_at_beginning_of
,(section
, name
),
2504 CONST
char *section AND
2507 if (ldsym_undefined(name
)) {
2508 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2509 asymbol
*def
= create_symbol(name
,
2510 BSF_GLOBAL
| BSF_EXPORT
,
2514 if (s
!= (asection
*)NULL
) {
2515 def
->value
= s
->vma
;
2524 Create an absolute symbol with the given name with the value of the
2525 address of the first byte after the end of the section named.
2527 If the symbol already exists, then do nothing.
2530 DEFUN(lang_abs_symbol_at_end_of
,(section
, name
),
2531 CONST
char *section AND
2534 if (ldsym_undefined(name
)){
2535 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2536 /* Add a symbol called _end */
2537 asymbol
*def
= create_symbol(name
,
2538 BSF_GLOBAL
| BSF_EXPORT
,
2541 if (s
!= (asection
*)NULL
) {
2542 def
->value
= s
->vma
+ s
->_raw_size
;
2551 DEFUN(lang_statement_append
,(list
, element
, field
),
2552 lang_statement_list_type
*list AND
2553 lang_statement_union_type
*element AND
2554 lang_statement_union_type
**field
)
2556 *(list
->tail
) = element
;
2560 /* Set the output format type */
2562 DEFUN(lang_add_output_format
,(format
),
2565 output_target
= format
;