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
->reloc_done
?
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
+ data
->output_section
->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
);
1139 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1143 DEFUN(print_wild_statement
,(w
,os
),
1144 lang_wild_statement_type
*w AND
1145 lang_output_section_statement_type
*os
)
1147 fprintf(config
.map_file
, " from ");
1148 if (w
->filename
!= (char *)NULL
) {
1149 fprintf(config
.map_file
, "%s",w
->filename
);
1152 fprintf(config
.map_file
, "*");
1154 if (w
->section_name
!= (char *)NULL
) {
1155 fprintf(config
.map_file
, "(%s)",w
->section_name
);
1158 fprintf(config
.map_file
, "(*)");
1161 print_statement(w
->children
.head
, os
);
1165 DEFUN(print_statement
,(s
, os
),
1166 lang_statement_union_type
*s AND
1167 lang_output_section_statement_type
*os
)
1171 switch (s
->header
.type
)
1173 case lang_constructors_statement_enum
:
1174 fprintf(config
.map_file
, "constructors:\n");
1175 print_statement(constructor_list
.head
, os
);
1177 case lang_wild_statement_enum
:
1178 print_wild_statement(&s
->wild_statement
, os
);
1181 fprintf(config
.map_file
, "Fail with %d\n",s
->header
.type
);
1184 case lang_address_statement_enum
:
1185 fprintf(config
.map_file
, "address\n");
1188 case lang_object_symbols_statement_enum
:
1189 fprintf(config
.map_file
, "object symbols\n");
1191 case lang_fill_statement_enum
:
1192 print_fill_statement(&s
->fill_statement
);
1194 case lang_data_statement_enum
:
1195 print_data_statement(&s
->data_statement
);
1197 case lang_input_section_enum
:
1198 print_input_section(&s
->input_section
);
1200 case lang_padding_statement_enum
:
1201 print_padding_statement(&s
->padding_statement
);
1203 case lang_output_section_statement_enum
:
1204 print_output_section_statement(&s
->output_section_statement
);
1206 case lang_assignment_statement_enum
:
1207 print_assignment(&s
->assignment_statement
,
1210 case lang_target_statement_enum
:
1211 fprintf(config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1213 case lang_output_statement_enum
:
1214 fprintf(config
.map_file
, "OUTPUT(%s %s)\n",
1215 s
->output_statement
.name
,
1218 case lang_input_statement_enum
:
1219 print_input_statement(&s
->input_statement
);
1221 case lang_afile_asection_pair_statement_enum
:
1231 DEFUN_VOID(print_statements
)
1233 print_statement(statement_list
.head
,
1234 (lang_output_section_statement_type
*)NULL
);
1238 DEFUN(insert_pad
,(this_ptr
, fill
, power
, output_section_statement
, dot
),
1239 lang_statement_union_type
**this_ptr AND
1241 unsigned int power AND
1242 asection
* output_section_statement AND
1245 /* Align this section first to the
1246 input sections requirement, then
1247 to the output section's requirement.
1248 If this alignment is > than any seen before,
1249 then record it too. Perform the alignment by
1250 inserting a magic 'padding' statement.
1253 unsigned int alignment_needed
= align_power(dot
, power
) - dot
;
1255 if (alignment_needed
!= 0)
1257 lang_statement_union_type
*new =
1258 (lang_statement_union_type
*)
1259 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1260 /* Link into existing chain */
1261 new->header
.next
= *this_ptr
;
1263 new->header
.type
= lang_padding_statement_enum
;
1264 new->padding_statement
.output_section
= output_section_statement
;
1265 new->padding_statement
.output_offset
=
1266 dot
- output_section_statement
->vma
;
1267 new->padding_statement
.fill
= fill
;
1268 new->padding_statement
.size
= alignment_needed
;
1272 /* Remember the most restrictive alignment */
1273 if (power
> output_section_statement
->alignment_power
) {
1274 output_section_statement
->alignment_power
= power
;
1276 output_section_statement
->_raw_size
+= alignment_needed
;
1277 return alignment_needed
+ dot
;
1281 /* Work out how much this section will move the dot point */
1283 DEFUN(size_input_section
, (this_ptr
, output_section_statement
, fill
, dot
),
1284 lang_statement_union_type
**this_ptr AND
1285 lang_output_section_statement_type
*output_section_statement AND
1286 unsigned short fill AND
1289 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1290 asection
*i
= is
->section
;
1292 if (is
->ifile
->just_syms_flag
== false) {
1293 dot
= insert_pad(this_ptr
, fill
, i
->alignment_power
,
1294 output_section_statement
->bfd_section
, dot
);
1296 /* remember the largest size so we can malloc the largest area
1297 needed for the output stage. Only remember the size of sections
1298 which we will actually allocate */
1300 (SEC_HAS_CONTENTS
| SEC_LOAD
)) == (SEC_HAS_CONTENTS
| SEC_LOAD
))
1301 && (bfd_get_section_size_before_reloc(i
) > largest_section
)) {
1302 largest_section
= bfd_get_section_size_before_reloc(i
);
1305 /* Remember where in the output section this input section goes */
1307 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1309 /* Mark how big the output section must be to contain this now */
1310 dot
+= bfd_get_section_size_before_reloc(i
);
1311 output_section_statement
->bfd_section
->_raw_size
=
1312 dot
- output_section_statement
->bfd_section
->vma
;
1316 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1323 /* Work out the size of the output sections
1324 from the sizes of the input sections */
1326 DEFUN(lang_size_sections
,(s
, output_section_statement
, prev
, fill
,
1328 lang_statement_union_type
*s AND
1329 lang_output_section_statement_type
* output_section_statement AND
1330 lang_statement_union_type
**prev AND
1331 unsigned short fill AND
1335 /* Size up the sections from their constituent parts */
1336 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1338 switch (s
->header
.type
) {
1341 case lang_output_section_statement_enum
:
1344 lang_output_section_statement_type
*os
=
1345 &(s
->output_section_statement
);
1346 /* The start of a section */
1348 if (os
->addr_tree
== (etree_type
*)NULL
) {
1349 /* No address specified for this section, get one
1350 from the region specification
1352 if (os
->region
== (lang_memory_region_type
*)NULL
) {
1353 os
->region
= lang_memory_region_lookup("*default*");
1355 dot
= os
->region
->current
;
1358 etree_value_type r
;
1359 r
= exp_fold_tree(os
->addr_tree
,
1360 (lang_output_section_statement_type
*)NULL
,
1361 lang_allocating_phase_enum
,
1363 if (r
.valid
== false) {
1364 einfo("%F%S: non constant address expression for section %s\n",
1369 /* The section starts here */
1370 /* First, align to what the section needs */
1372 dot
= align_power(dot
, os
->bfd_section
->alignment_power
);
1373 os
->bfd_section
->vma
= dot
;
1374 os
->bfd_section
->output_offset
= 0;
1376 (void) lang_size_sections(os
->children
.head
, os
, &os
->children
.head
,
1378 /* Ignore the size of the input sections, use the vma and size to */
1382 after
= ALIGN(os
->bfd_section
->vma
+
1383 os
->bfd_section
->size
,
1387 os
->bfd_section
->size
= after
- os
->bfd_section
->vma
;
1388 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->size
;
1389 os
->processed
= true;
1391 /* Replace into region ? */
1392 if (os
->addr_tree
== (etree_type
*)NULL
1393 && os
->region
!=(lang_memory_region_type
*)NULL
) {
1394 os
->region
->current
= dot
;
1399 case lang_constructors_statement_enum
:
1400 dot
= lang_size_sections(constructor_list
.head
,
1401 output_section_statement
,
1402 &s
->wild_statement
.children
.head
,
1407 case lang_data_statement_enum
:
1409 unsigned int size
= 0;
1410 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1411 s
->data_statement
.output_section
=
1412 output_section_statement
->bfd_section
;
1414 switch (s
->data_statement
.type
) {
1427 output_section_statement
->bfd_section
->size
+= size
;
1431 case lang_wild_statement_enum
:
1433 dot
= lang_size_sections(s
->wild_statement
.children
.head
,
1434 output_section_statement
,
1435 &s
->wild_statement
.children
.head
,
1441 case lang_object_symbols_statement_enum
:
1442 create_object_symbols
= output_section_statement
;
1444 case lang_output_statement_enum
:
1445 case lang_target_statement_enum
:
1447 case lang_input_section_enum
:
1448 dot
= size_input_section(prev
,
1449 output_section_statement
,
1450 output_section_statement
->fill
, dot
);
1452 case lang_input_statement_enum
:
1454 case lang_fill_statement_enum
:
1455 fill
= s
->fill_statement
.fill
;
1457 case lang_assignment_statement_enum
:
1459 bfd_vma newdot
= dot
;
1460 exp_fold_tree(s
->assignment_statement
.exp
,
1461 output_section_statement
,
1462 lang_allocating_phase_enum
,
1467 /* We've been moved ! so insert a pad */
1469 lang_statement_union_type
*new =
1470 (lang_statement_union_type
*)
1471 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1472 /* Link into existing chain */
1473 new->header
.next
= *prev
;
1475 new->header
.type
= lang_padding_statement_enum
;
1476 new->padding_statement
.output_section
=
1477 output_section_statement
->bfd_section
;
1478 new->padding_statement
.output_offset
=
1479 dot
- output_section_statement
->bfd_section
->vma
;
1480 new->padding_statement
.fill
= fill
;
1481 new->padding_statement
.size
= newdot
- dot
;
1482 output_section_statement
->bfd_section
->size
+=
1483 new->padding_statement
.size
;
1489 case lang_padding_statement_enum
:
1495 case lang_address_statement_enum
:
1498 prev
= &s
->header
.next
;
1503 /* Sizing happens in two passes, first pass we allocate worst case
1504 stuff. The second pass (if relaxing), we use what we learnt to
1505 change the size of some relocs from worst case to better
1507 static boolean had_relax
;
1510 DEFUN(lang_size_sections
,(s
, output_section_statement
, prev
, fill
,
1512 lang_statement_union_type
*s AND
1513 lang_output_section_statement_type
* output_section_statement AND
1514 lang_statement_union_type
**prev AND
1515 unsigned short fill AND
1519 /* Size up the sections from their constituent parts */
1520 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1522 switch (s
->header
.type
) {
1524 case lang_output_section_statement_enum
:
1527 lang_output_section_statement_type
*os
=
1528 &(s
->output_section_statement
);
1529 /* The start of a section */
1531 if (os
->addr_tree
== (etree_type
*)NULL
) {
1532 /* No address specified for this section, get one
1533 from the region specification
1535 if (os
->region
== (lang_memory_region_type
*)NULL
) {
1536 os
->region
= lang_memory_region_lookup("*default*");
1538 dot
= os
->region
->current
;
1541 etree_value_type r
;
1542 r
= exp_fold_tree(os
->addr_tree
,
1543 (lang_output_section_statement_type
*)NULL
,
1544 lang_allocating_phase_enum
,
1546 if (r
.valid
== false) {
1547 einfo("%F%S: non constant address expression for section %s\n",
1552 /* The section starts here */
1553 /* First, align to what the section needs */
1555 dot
= align_power(dot
, os
->bfd_section
->alignment_power
);
1556 os
->bfd_section
->vma
= dot
;
1557 os
->bfd_section
->output_offset
= 0;
1559 (void) lang_size_sections(os
->children
.head
, os
, &os
->children
.head
,
1560 os
->fill
, dot
, relax
);
1561 /* Ignore the size of the input sections, use the vma and size to */
1565 after
= ALIGN(os
->bfd_section
->vma
+
1566 os
->bfd_section
->_raw_size
,
1570 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1571 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1572 os
->processed
= true;
1574 /* Replace into region ? */
1575 if (os
->addr_tree
== (etree_type
*)NULL
1576 && os
->region
!=(lang_memory_region_type
*)NULL
) {
1577 os
->region
->current
= dot
;
1582 case lang_constructors_statement_enum
:
1583 dot
= lang_size_sections(constructor_list
.head
,
1584 output_section_statement
,
1585 &s
->wild_statement
.children
.head
,
1590 case lang_data_statement_enum
:
1592 unsigned int size
= 0;
1593 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1594 s
->data_statement
.output_section
=
1595 output_section_statement
->bfd_section
;
1597 switch (s
->data_statement
.type
) {
1610 output_section_statement
->bfd_section
->_raw_size
+= size
;
1614 case lang_wild_statement_enum
:
1616 dot
= lang_size_sections(s
->wild_statement
.children
.head
,
1617 output_section_statement
,
1618 &s
->wild_statement
.children
.head
,
1624 case lang_object_symbols_statement_enum
:
1625 create_object_symbols
= output_section_statement
;
1627 case lang_output_statement_enum
:
1628 case lang_target_statement_enum
:
1630 case lang_input_section_enum
:
1636 had_relax
|= relax_section(prev
);
1641 dot
= size_input_section(prev
,
1642 output_section_statement
,
1643 output_section_statement
->fill
, dot
);
1645 case lang_input_statement_enum
:
1647 case lang_fill_statement_enum
:
1648 fill
= s
->fill_statement
.fill
;
1650 case lang_assignment_statement_enum
:
1652 bfd_vma newdot
= dot
;
1653 exp_fold_tree(s
->assignment_statement
.exp
,
1654 output_section_statement
,
1655 lang_allocating_phase_enum
,
1660 /* We've been moved ! so insert a pad */
1662 lang_statement_union_type
*new =
1663 (lang_statement_union_type
*)
1664 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1665 /* Link into existing chain */
1666 new->header
.next
= *prev
;
1668 new->header
.type
= lang_padding_statement_enum
;
1669 new->padding_statement
.output_section
=
1670 output_section_statement
->bfd_section
;
1671 new->padding_statement
.output_offset
=
1672 dot
- output_section_statement
->bfd_section
->vma
;
1673 new->padding_statement
.fill
= fill
;
1674 new->padding_statement
.size
= newdot
- dot
;
1675 output_section_statement
->bfd_section
->_raw_size
+=
1676 new->padding_statement
.size
;
1685 /* This can only get here when relaxing is turned on */
1686 case lang_padding_statement_enum
:
1688 case lang_address_statement_enum
:
1691 prev
= &s
->header
.next
;
1698 DEFUN(lang_do_assignments
,(s
, output_section_statement
, fill
, dot
),
1699 lang_statement_union_type
*s AND
1700 lang_output_section_statement_type
* output_section_statement AND
1701 unsigned short fill AND
1705 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1707 switch (s
->header
.type
) {
1708 case lang_constructors_statement_enum
:
1709 dot
= lang_do_assignments(constructor_list
.head
,
1710 output_section_statement
,
1715 case lang_output_section_statement_enum
:
1717 lang_output_section_statement_type
*os
=
1718 &(s
->output_section_statement
);
1719 dot
= os
->bfd_section
->vma
;
1720 (void) lang_do_assignments(os
->children
.head
, os
, os
->fill
, dot
);
1721 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1724 case lang_wild_statement_enum
:
1726 dot
= lang_do_assignments(s
->wild_statement
.children
.head
,
1727 output_section_statement
,
1732 case lang_object_symbols_statement_enum
:
1733 case lang_output_statement_enum
:
1734 case lang_target_statement_enum
:
1736 case lang_common_statement_enum
:
1739 case lang_data_statement_enum
:
1741 etree_value_type value
;
1742 value
= exp_fold_tree(s
->data_statement
.exp
,
1743 0, lang_final_phase_enum
, dot
, &dot
);
1744 s
->data_statement
.value
= value
.value
;
1745 if (value
.valid
== false) einfo("%F%P: Invalid data statement\n");
1747 switch (s
->data_statement
.type
) {
1759 case lang_input_section_enum
:
1761 asection
*in
= s
->input_section
.section
;
1762 dot
+= bfd_get_section_size_before_reloc(in
);
1766 case lang_input_statement_enum
:
1768 case lang_fill_statement_enum
:
1769 fill
= s
->fill_statement
.fill
;
1771 case lang_assignment_statement_enum
:
1773 exp_fold_tree(s
->assignment_statement
.exp
,
1774 output_section_statement
,
1775 lang_final_phase_enum
,
1781 case lang_padding_statement_enum
:
1782 dot
+= s
->padding_statement
.size
;
1787 case lang_address_statement_enum
:
1798 DEFUN_VOID(lang_relocate_globals
)
1802 Each ldsym_type maintains a chain of pointers to asymbols which
1803 references the definition. Replace each pointer to the referenence
1804 with a pointer to only one place, preferably the definition. If
1805 the defintion isn't available then the common symbol, and if
1806 there isn't one of them then choose one reference.
1809 FOR_EACH_LDSYM(lgs
) {
1811 if (lgs
->sdefs_chain
) {
1812 it
= *(lgs
->sdefs_chain
);
1814 else if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1815 it
= *(lgs
->scoms_chain
);
1817 else if (lgs
->srefs_chain
!= (asymbol
**)NULL
) {
1818 it
= *(lgs
->srefs_chain
);
1821 /* This can happen when the command line asked for a symbol to
1823 it
= (asymbol
*)NULL
;
1825 if (it
!= (asymbol
*)NULL
)
1827 asymbol
**ptr
= lgs
->srefs_chain
;;
1828 if (lgs
->flags
& SYM_WARNING
)
1830 produce_warnings(lgs
, it
);
1833 while (ptr
!= (asymbol
**)NULL
) {
1834 asymbol
*ref
= *ptr
;
1836 ptr
= (asymbol
**)(ref
->udata
);
1845 DEFUN_VOID(lang_finish
)
1849 if (entry_symbol
== (char *)NULL
) {
1850 /* No entry has been specified, look for start */
1851 entry_symbol
= "start";
1853 lgs
= ldsym_get_soft(entry_symbol
);
1854 if (lgs
&& lgs
->sdefs_chain
) {
1855 asymbol
*sy
= *(lgs
->sdefs_chain
);
1856 /* We can set the entry address*/
1857 bfd_set_start_address(output_bfd
,
1858 outside_symbol_address(sy
));
1862 /* Can't find anything reasonable,
1863 use the first address in the text section
1865 asection
*ts
= bfd_get_section_by_name(output_bfd
, ".text");
1867 bfd_set_start_address(output_bfd
, ts
->vma
);
1872 /* By now we know the target architecture, and we may have an */
1873 /* ldfile_output_machine_name */
1875 DEFUN_VOID(lang_check
)
1877 lang_statement_union_type
*file
;
1880 unsigned long input_machine
;
1881 enum bfd_architecture input_architecture
;
1883 CONST bfd_arch_info_type
*compatible
;
1885 for (file
= file_chain
.head
;
1886 file
!= (lang_statement_union_type
*)NULL
;
1887 file
=file
->input_statement
.next
)
1889 unsigned long ldfile_new_output_machine
= 0;
1890 enum bfd_architecture ldfile_new_output_architecture
= bfd_arch_unknown
;
1892 input_bfd
= file
->input_statement
.the_bfd
;
1894 input_machine
= bfd_get_mach(input_bfd
);
1895 input_architecture
= bfd_get_arch(input_bfd
);
1898 /* Inspect the architecture and ensure we're linking like with
1901 compatible
=bfd_arch_get_compatible(input_bfd
,
1906 ldfile_output_machine
= compatible
->mach
;
1907 ldfile_output_architecture
= compatible
->arch
;
1912 info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1913 bfd_printable_name(input_bfd
), input_bfd
,
1914 bfd_printable_name(output_bfd
));
1916 bfd_set_arch_mach(output_bfd
,
1917 ldfile_new_output_architecture
,
1918 ldfile_new_output_machine
);
1926 * run through all the global common symbols and tie them
1927 * to the output section requested.
1929 As an experiment we do this 4 times, once for all the byte sizes,
1930 then all the two bytes, all the four bytes and then everything else
1934 DEFUN_VOID(lang_common
)
1938 if (config
.relocateable_output
== false ||
1939 command_line
.force_common_definition
== true) {
1940 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<=1) {
1941 for (lgs
= symbol_head
;
1942 lgs
!= (ldsym_type
*)NULL
;
1946 unsigned int power_of_two
;
1949 if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1950 com
= *(lgs
->scoms_chain
);
1979 if (config
.sort_common
== false || align
== power
) {
1980 /* Change from a common symbol into a definition of
1982 lgs
->sdefs_chain
= lgs
->scoms_chain
;
1983 lgs
->scoms_chain
= (asymbol
**)NULL
;
1985 /* Point to the correct common section */
1987 ((lang_input_statement_type
*)
1988 (com
->the_bfd
->usrdata
))->common_section
;
1989 /* Fix the size of the common section */
1991 com
->section
->_raw_size
=
1992 ALIGN(com
->section
->_raw_size
, align
);
1994 /* Remember if this is the biggest alignment ever seen */
1995 if (power_of_two
> com
->section
->alignment_power
) {
1996 com
->section
->alignment_power
= power_of_two
;
1999 /* Symbol stops being common and starts being global, but
2000 we remember that it was common once. */
2002 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
2003 com
->value
= com
->section
->_raw_size
;
2007 fprintf (config
.map_file
, "Allocating common %s: %x at %x %s\n",
2010 (unsigned) com
->value
,
2011 com
->the_bfd
->filename
);
2014 com
->section
->_raw_size
+= size
;
2027 run through the input files and ensure that every input
2028 section has somewhere to go. If one is found without
2029 a destination then create an input request and place it
2030 into the statement tree.
2034 DEFUN_VOID(lang_place_orphans
)
2036 lang_input_statement_type
*file
;
2037 for (file
= (lang_input_statement_type
*)file_chain
.head
;
2038 file
!= (lang_input_statement_type
*)NULL
;
2039 file
= (lang_input_statement_type
*)file
->next
) {
2041 for (s
= file
->the_bfd
->sections
;
2042 s
!= (asection
*)NULL
;
2044 if ( s
->output_section
== (asection
*)NULL
) {
2045 /* This section of the file is not attatched, root
2046 around for a sensible place for it to go */
2048 if (file
->common_section
== s
) {
2049 /* This is a lonely common section which must
2050 have come from an archive. We attatch to the
2051 section with the wildcard */
2052 if (config
.relocateable_output
!= true
2053 && command_line
.force_common_definition
== false) {
2054 if (default_common_section
==
2055 (lang_output_section_statement_type
*)NULL
) {
2056 info("%P: No [COMMON] command, defaulting to .bss\n");
2058 default_common_section
=
2059 lang_output_section_statement_lookup(".bss");
2062 wild_doit(&default_common_section
->children
, s
,
2063 default_common_section
, file
);
2067 lang_output_section_statement_type
*os
=
2068 lang_output_section_statement_lookup(s
->name
);
2070 wild_doit(&os
->children
, s
, os
, file
);
2079 DEFUN(lang_set_flags
,(ptr
, flags
),
2083 boolean state
= true;
2087 if (*flags
== '!') {
2094 /* ptr->flag_read = state; */
2097 /* ptr->flag_write = state; */
2100 /* ptr->flag_executable= state;*/
2104 /* ptr->flag_loadable= state;*/
2107 einfo("%P%F illegal syntax in flags\n");
2117 DEFUN(lang_for_each_file
,(func
),
2118 PROTO(void, (*func
),(lang_input_statement_type
*)))
2120 lang_input_statement_type
*f
;
2121 for (f
= (lang_input_statement_type
*)file_chain
.head
;
2122 f
!= (lang_input_statement_type
*)NULL
;
2123 f
= (lang_input_statement_type
*)f
->next
)
2131 DEFUN(lang_for_each_input_section
, (func
),
2132 PROTO(void ,(*func
),(bfd
*ab
, asection
*as
)))
2134 lang_input_statement_type
*f
;
2135 for (f
= (lang_input_statement_type
*)file_chain
.head
;
2136 f
!= (lang_input_statement_type
*)NULL
;
2137 f
= (lang_input_statement_type
*)f
->next
)
2140 for (s
= f
->the_bfd
->sections
;
2141 s
!= (asection
*)NULL
;
2143 func(f
->the_bfd
, s
);
2151 DEFUN(ldlang_add_file
,(entry
),
2152 lang_input_statement_type
*entry
)
2155 lang_statement_append(&file_chain
,
2156 (lang_statement_union_type
*)entry
,
2163 DEFUN(lang_add_output
,(name
),
2166 lang_output_statement_type
*new = new_stat(lang_output_statement
,
2169 had_output_filename
= true;
2173 static lang_output_section_statement_type
*current_section
;
2176 DEFUN(lang_enter_output_section_statement
,
2177 (output_section_statement_name
,
2181 char *output_section_statement_name AND
2182 etree_type
*address_exp AND
2184 bfd_vma block_value
)
2186 lang_output_section_statement_type
*os
;
2189 lang_output_section_statement_lookup(output_section_statement_name
);
2193 /* Add this statement to tree */
2194 /* add_statement(lang_output_section_statement_enum,
2195 output_section_statement);*/
2196 /* Make next things chain into subchain of this */
2198 if (os
->addr_tree
==
2199 (etree_type
*)NULL
) {
2204 os
->block_value
= block_value
;
2205 stat_ptr
= & os
->children
;
2211 DEFUN_VOID(lang_final
)
2213 if (had_output_filename
== false) {
2214 extern CONST
char *output_filename
;
2215 lang_add_output(output_filename
);
2221 /* Reset the current counters in the regions */
2223 DEFUN_VOID(reset_memory_regions
)
2225 lang_memory_region_type
*p
= lang_memory_region_list
;
2226 for (p
= lang_memory_region_list
;
2227 p
!= ( lang_memory_region_type
*)NULL
;
2230 p
->current
= p
->origin
;
2236 DEFUN(create_symbol
,(name
, flags
, section
),
2237 CONST
char *name AND
2241 extern lang_input_statement_type
*script_file
;
2242 asymbol
**def_ptr
= (asymbol
**)ldmalloc((bfd_size_type
)(sizeof(asymbol
**)));
2243 /* Add this definition to script file */
2244 asymbol
*def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
2245 def
->name
= buystring(name
);
2248 def
->section
= section
;
2251 Q_enter_global_ref(def_ptr
);
2258 DEFUN_VOID(lang_process
)
2260 if (had_script
== false) {
2261 parse_line(ldemul_get_script());
2263 lang_reasonable_defaults();
2264 current_target
= default_target
;
2266 lang_for_each_statement(ldlang_open_output
); /* Open the output file */
2267 /* For each output section statement, create a section in the output
2269 lang_create_output_section_statements();
2271 /* Create a dummy bfd for the script */
2272 lang_init_script_file();
2274 /* Add to the hash table all undefineds on the command line */
2275 lang_place_undefineds();
2277 /* Create a bfd for each input file */
2278 current_target
= default_target
;
2279 lang_for_each_statement(open_input_bfds
);
2281 common_section
.userdata
= (PTR
)&common_section_userdata
;
2284 /* Run through the contours of the script and attatch input sections
2285 to the correct output sections
2287 find_constructors();
2288 map_input_to_output_sections(statement_list
.head
, (char *)NULL
,
2289 ( lang_output_section_statement_type
*)NULL
);
2292 /* Find any sections not attatched explicitly and handle them */
2293 lang_place_orphans();
2295 /* Size up the common data */
2298 ldemul_before_allocation();
2301 /* Size up the sections */
2302 lang_size_sections(statement_list
.head
,
2303 (lang_output_section_statement_type
*)NULL
,
2304 &(statement_list
.head
), 0, (bfd_vma
)0, false);
2307 /* Move the global symbols around */
2308 lang_relocate_globals();
2310 /* Now run around and relax if we can */
2311 if (command_line
.relax
)
2313 reset_memory_regions();
2321 lang_size_sections(statement_list
.head
,
2322 (lang_output_section_statement_type
*)NULL
,
2323 &(statement_list
.head
), 0, (bfd_vma
)0, true);
2324 /* FIXME. Until the code in relax is fixed so that it only reads in
2325 stuff once, we cant iterate since there is no way for the linker to
2326 know what has been patched and what hasn't */
2337 /* See if anything special should be done now we know how big
2339 ldemul_after_allocation();
2341 /* Do all the assignments, now that we know the final restingplaces
2342 of all the symbols */
2344 lang_do_assignments(statement_list
.head
,
2345 (lang_output_section_statement_type
*)NULL
,
2349 /* Make sure that we're not mixing architectures */
2358 /* EXPORTED TO YACC */
2361 DEFUN(lang_add_wild
,(section_name
, filename
),
2362 CONST
char *CONST section_name AND
2363 CONST
char *CONST filename
)
2365 lang_wild_statement_type
*new = new_stat(lang_wild_statement
,
2368 if (section_name
!= (char *)NULL
&& strcmp(section_name
,"COMMON") == 0)
2370 placed_commons
= true;
2372 if (filename
!= (char *)NULL
) {
2373 lang_has_input_file
= true;
2375 new->section_name
= section_name
;
2376 new->filename
= filename
;
2377 lang_list_init(&new->children
);
2380 DEFUN(lang_section_start
,(name
, address
),
2381 CONST
char *name AND
2382 etree_type
*address
)
2384 lang_address_statement_type
*ad
=new_stat(lang_address_statement
, stat_ptr
);
2385 ad
->section_name
= name
;
2386 ad
->address
= address
;
2390 DEFUN(lang_add_entry
,(name
),
2393 entry_symbol
= name
;
2397 DEFUN(lang_add_target
,(name
),
2400 lang_target_statement_type
*new = new_stat(lang_target_statement
,
2410 DEFUN(lang_add_map
,(name
),
2416 map_option_f
= true;
2424 DEFUN(lang_add_fill
,(exp
),
2427 lang_fill_statement_type
*new = new_stat(lang_fill_statement
,
2433 DEFUN(lang_add_data
,(type
, exp
),
2435 union etree_union
*exp
)
2438 lang_data_statement_type
*new = new_stat(lang_data_statement
,
2445 DEFUN(lang_add_assignment
,(exp
),
2448 lang_assignment_statement_type
*new = new_stat(lang_assignment_statement
,
2454 DEFUN(lang_add_attribute
,(attribute
),
2455 enum statement_enum attribute
)
2457 new_statement(attribute
, sizeof(lang_statement_union_type
),stat_ptr
);
2463 DEFUN(lang_startup
,(name
),
2466 if (startup_file
!= (char *)NULL
) {
2467 einfo("%P%FMultiple STARTUP files\n");
2469 first_file
->filename
= name
;
2470 first_file
->local_sym_name
= name
;
2475 DEFUN(lang_float
,(maybe
),
2478 lang_float_flag
= maybe
;
2482 DEFUN(lang_leave_output_section_statement
,(fill
, memspec
),
2484 CONST
char *memspec
)
2486 current_section
->fill
= fill
;
2487 current_section
->region
= lang_memory_region_lookup(memspec
);
2488 stat_ptr
= &statement_list
;
2490 /* We remember if we are closing a .data section, since we use it to
2491 store constructors in */
2492 if (strcmp(current_section
->name
, ".data") ==0) {
2493 end_of_data_section_statement_list
= statement_list
;
2498 Create an absolute symbol with the given name with the value of the
2499 address of first byte of the section named.
2501 If the symbol already exists, then do nothing.
2504 DEFUN(lang_abs_symbol_at_beginning_of
,(section
, name
),
2505 CONST
char *section AND
2508 if (ldsym_undefined(name
)) {
2509 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2510 asymbol
*def
= create_symbol(name
,
2511 BSF_GLOBAL
| BSF_EXPORT
,
2515 if (s
!= (asection
*)NULL
) {
2516 def
->value
= s
->vma
;
2525 Create an absolute symbol with the given name with the value of the
2526 address of the first byte after the end of the section named.
2528 If the symbol already exists, then do nothing.
2531 DEFUN(lang_abs_symbol_at_end_of
,(section
, name
),
2532 CONST
char *section AND
2535 if (ldsym_undefined(name
)){
2536 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2537 /* Add a symbol called _end */
2538 asymbol
*def
= create_symbol(name
,
2539 BSF_GLOBAL
| BSF_EXPORT
,
2542 if (s
!= (asection
*)NULL
) {
2543 def
->value
= s
->vma
+ s
->_raw_size
;
2552 DEFUN(lang_statement_append
,(list
, element
, field
),
2553 lang_statement_list_type
*list AND
2554 lang_statement_union_type
*element AND
2555 lang_statement_union_type
**field
)
2557 *(list
->tail
) = element
;
2561 /* Set the output format type */
2563 DEFUN(lang_add_output_format
,(format
),
2566 output_target
= format
;