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 struct obstack stat_obstack
;
46 #define obstack_chunk_alloc ldmalloc
47 #define obstack_chunk_free free
48 static CONST
char *startup_file
;
49 static lang_statement_list_type input_file_chain
;
51 /* Points to the last statement in the .data section, so we can add
52 stuff to the data section without pain */
53 static lang_statement_list_type end_of_data_section_statement_list
;
55 /* List of statements needed to handle constructors */
56 extern lang_statement_list_type constructor_list
;
58 static boolean placed_commons
= false;
59 static lang_output_section_statement_type
*default_common_section
;
60 static boolean map_option_f
;
61 static bfd_vma print_dot
;
62 static lang_input_statement_type
*first_file
;
63 static lang_statement_list_type lang_output_section_statement
;
64 static CONST
char *current_target
;
65 static CONST
char *output_target
;
66 static size_t longest_section_name
= 8;
67 static asection common_section
;
68 static section_userdata_type common_section_userdata
;
69 static lang_statement_list_type statement_list
;
73 lang_output_section_statement_type
*abs_output_section
;
74 lang_statement_list_type
*stat_ptr
= &statement_list
;
75 lang_input_statement_type
*script_file
= 0;
76 boolean option_longmap
= false;
77 lang_statement_list_type file_chain
= {0};
78 CONST
char *entry_symbol
= 0;
79 bfd_size_type largest_section
= 0;
80 boolean lang_has_input_file
= false;
81 lang_output_section_statement_type
*create_object_symbols
= 0;
82 boolean had_output_filename
= false;
83 boolean lang_float_flag
= false;
85 extern char *default_target
;
87 extern unsigned int undefined_global_sym_count
;
88 extern char *current_file
;
89 extern bfd
*output_bfd
;
90 extern enum bfd_architecture ldfile_output_architecture
;
91 extern unsigned long ldfile_output_machine
;
92 extern char *ldfile_output_machine_name
;
93 extern ldsym_type
*symbol_head
;
94 extern unsigned int commons_pending
;
95 extern args_type command_line
;
96 extern ld_config_type config
;
97 extern boolean had_script
;
98 extern boolean write_map
;
102 #define cat(a,b) a##b
104 #define cat(a,b) a/**/b
107 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
109 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
111 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
113 void EXFUN(lang_add_data
,( int type
, union etree_union
*exp
));
117 DEFUN(stat_alloc
,(size
),
120 return obstack_alloc(&stat_obstack
, size
);
123 DEFUN(print_size
,(value
),
126 fprintf(config
.map_file
, "%5x", (unsigned)value
);
129 DEFUN(print_alignment
,(value
),
132 fprintf(config
.map_file
, "2**%1u",value
);
135 DEFUN(print_fill
,(value
),
138 fprintf(config
.map_file
, "%04x",(unsigned)value
);
143 DEFUN(print_section
,(name
),
144 CONST
char *CONST name
)
146 fprintf(config
.map_file
, "%*s", -longest_section_name
, name
);
149 /*----------------------------------------------------------------------
150 lang_for_each_statement walks the parse tree and calls the provided
151 function for each node
155 DEFUN(lang_for_each_statement_worker
,(func
, s
),
157 lang_statement_union_type
*s
)
159 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
163 switch (s
->header
.type
) {
164 case lang_constructors_statement_enum
:
165 lang_for_each_statement_worker(func
, constructor_list
.head
);
167 case lang_output_section_statement_enum
:
168 lang_for_each_statement_worker
170 s
->output_section_statement
.children
.head
);
172 case lang_wild_statement_enum
:
173 lang_for_each_statement_worker
175 s
->wild_statement
.children
.head
);
177 case lang_data_statement_enum
:
178 case lang_object_symbols_statement_enum
:
179 case lang_output_statement_enum
:
180 case lang_target_statement_enum
:
181 case lang_input_section_enum
:
182 case lang_input_statement_enum
:
183 case lang_assignment_statement_enum
:
184 case lang_padding_statement_enum
:
185 case lang_address_statement_enum
:
195 DEFUN(lang_for_each_statement
,(func
),
198 lang_for_each_statement_worker(func
,
199 statement_list
.head
);
201 /*----------------------------------------------------------------------*/
203 DEFUN(lang_list_init
,(list
),
204 lang_statement_list_type
*list
)
206 list
->head
= (lang_statement_union_type
*)NULL
;
207 list
->tail
= &list
->head
;
211 /*----------------------------------------------------------------------
213 build a new statement node for the parse tree
218 lang_statement_union_type
*
219 DEFUN(new_statement
,(type
, size
, list
),
220 enum statement_enum type AND
221 bfd_size_type size AND
222 lang_statement_list_type
*list
)
224 lang_statement_union_type
*new = (lang_statement_union_type
*)
226 new->header
.type
= type
;
227 new->header
.next
= (lang_statement_union_type
*)NULL
;
228 lang_statement_append(list
, new, &new->header
.next
);
233 Build a new input file node for the language. There are several ways
234 in which we treat an input file, eg, we only look at symbols, or
235 prefix it with a -l etc.
237 We can be supplied with requests for input files more than once;
238 they may, for example be split over serveral lines like foo.o(.text)
239 foo.o(.data) etc, so when asked for a file we check that we havn't
240 got it already so we don't duplicate the bfd.
243 static lang_input_statement_type
*
244 DEFUN(new_afile
, (name
, file_type
, target
),
245 CONST
char *CONST name AND
246 CONST lang_input_file_enum_type file_type AND
247 CONST
char *CONST target
)
250 lang_input_statement_type
*p
= new_stat(lang_input_statement
,
252 lang_has_input_file
= true;
254 p
->complained
= false;
256 case lang_input_file_is_symbols_only_enum
:
258 p
->is_archive
=false;
260 p
->local_sym_name
= name
;
261 p
->just_syms_flag
= true;
262 p
->search_dirs_flag
= false;
264 case lang_input_file_is_fake_enum
:
266 p
->is_archive
=false;
268 p
->local_sym_name
= name
;
269 p
->just_syms_flag
= false;
270 p
->search_dirs_flag
=false;
272 case lang_input_file_is_l_enum
:
273 p
->is_archive
= true;
276 p
->local_sym_name
= concat("-l",name
,"");
277 p
->just_syms_flag
= false;
278 p
->search_dirs_flag
= true;
280 case lang_input_file_is_search_file_enum
:
281 case lang_input_file_is_marker_enum
:
283 p
->is_archive
=false;
285 p
->local_sym_name
= name
;
286 p
->just_syms_flag
= false;
287 p
->search_dirs_flag
=true;
289 case lang_input_file_is_file_enum
:
291 p
->is_archive
=false;
293 p
->local_sym_name
= name
;
294 p
->just_syms_flag
= false;
295 p
->search_dirs_flag
=false;
300 p
->asymbols
= (asymbol
**)NULL
;
301 p
->superfile
= (lang_input_statement_type
*)NULL
;
302 p
->next_real_file
= (lang_statement_union_type
*)NULL
;
303 p
->next
= (lang_statement_union_type
*)NULL
;
305 p
->common_output_section
= (asection
*)NULL
;
306 lang_statement_append(&input_file_chain
,
307 (lang_statement_union_type
*)p
,
314 lang_input_statement_type
*
315 DEFUN(lang_add_input_file
,(name
, file_type
, target
),
317 lang_input_file_enum_type file_type AND
320 /* Look it up or build a new one */
321 lang_has_input_file
= true;
323 lang_input_statement_type
*p
;
325 for (p
= (lang_input_statement_type
*)input_file_chain
.head
;
326 p
!= (lang_input_statement_type
*)NULL
;
327 p
= (lang_input_statement_type
*)(p
->next_real_file
))
329 /* Sometimes we have incomplete entries in here */
330 if (p
->filename
!= (char *)NULL
) {
331 if(strcmp(name
,p
->filename
) == 0) return p
;
336 return new_afile(name
, file_type
, target
);
340 /* Build enough state so that the parser can build its tree */
342 DEFUN_VOID(lang_init
)
344 obstack_begin(&stat_obstack
, 1000);
346 stat_ptr
= &statement_list
;
348 lang_list_init(stat_ptr
);
350 lang_list_init(&input_file_chain
);
351 lang_list_init(&lang_output_section_statement
);
352 lang_list_init(&file_chain
);
353 first_file
= lang_add_input_file((char *)NULL
,
354 lang_input_file_is_marker_enum
,
356 abs_output_section
= lang_output_section_statement_lookup(BFD_ABS_SECTION_NAME
);
358 abs_output_section
->bfd_section
= &bfd_abs_section
;
363 /*----------------------------------------------------------------------
364 A region is an area of memory declared with the
365 MEMORY { name:org=exp, len=exp ... }
368 We maintain a list of all the regions here
370 If no regions are specified in the script, then the default is used
371 which is created when looked up to be the entire data space
374 static lang_memory_region_type
*lang_memory_region_list
;
375 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
377 lang_memory_region_type
*
378 DEFUN(lang_memory_region_lookup
,(name
),
379 CONST
char *CONST name
)
382 lang_memory_region_type
*p
= lang_memory_region_list
;
383 for (p
= lang_memory_region_list
;
384 p
!= ( lang_memory_region_type
*)NULL
;
386 if (strcmp(p
->name
, name
) == 0) {
390 if (strcmp(name
,"*default*")==0) {
391 /* This is the default region, dig out first one on the list */
392 if (lang_memory_region_list
!= (lang_memory_region_type
*)NULL
){
393 return lang_memory_region_list
;
397 lang_memory_region_type
*new =
398 (lang_memory_region_type
*)stat_alloc((bfd_size_type
)(sizeof(lang_memory_region_type
)));
399 new->name
= buystring(name
);
400 new->next
= (lang_memory_region_type
*)NULL
;
402 *lang_memory_region_list_tail
= new;
403 lang_memory_region_list_tail
= &new->next
;
407 new->had_full_message
= false;
414 lang_output_section_statement_type
*
415 DEFUN(lang_output_section_find
,(name
),
416 CONST
char * CONST name
)
418 lang_statement_union_type
*u
;
419 lang_output_section_statement_type
*lookup
;
421 for (u
= lang_output_section_statement
.head
;
422 u
!= (lang_statement_union_type
*)NULL
;
425 lookup
= &u
->output_section_statement
;
426 if (strcmp(name
, lookup
->name
)==0) {
430 return (lang_output_section_statement_type
*)NULL
;
433 lang_output_section_statement_type
*
434 DEFUN(lang_output_section_statement_lookup
,(name
),
435 CONST
char * CONST name
)
437 lang_output_section_statement_type
*lookup
;
438 lookup
=lang_output_section_find(name
);
439 if (lookup
== (lang_output_section_statement_type
*)NULL
) {
441 lookup
=(lang_output_section_statement_type
*)
442 new_stat(lang_output_section_statement
, stat_ptr
);
443 lookup
->region
= (lang_memory_region_type
*)NULL
;
445 lookup
->block_value
= 1;
448 lookup
->next
= (lang_statement_union_type
*)NULL
;
449 lookup
->bfd_section
= (asection
*)NULL
;
450 lookup
->processed
= false;
451 lookup
->addr_tree
= (etree_type
*)NULL
;
452 lang_list_init(&lookup
->children
);
454 lang_statement_append(&lang_output_section_statement
,
455 (lang_statement_union_type
*)lookup
,
466 DEFUN(print_flags
, ( ignore_flags
),
469 fprintf(config
.map_file
, "(");
471 if (flags
->flag_read
) fprintf(outfile
,"R");
472 if (flags
->flag_write
) fprintf(outfile
,"W");
473 if (flags
->flag_executable
) fprintf(outfile
,"X");
474 if (flags
->flag_loadable
) fprintf(outfile
,"L");
476 fprintf(config
.map_file
, ")");
482 lang_memory_region_type
*m
;
483 fprintf(config
.map_file
,"**MEMORY CONFIGURATION**\n\n");
485 fprintf(config
.map_file
,"name\t\torigin\t\tlength\t\tattributes\n");
487 fprintf(config
.map_file
,"name\t\torigin length\t\tattributes\n");
489 for (m
= lang_memory_region_list
;
490 m
!= (lang_memory_region_type
*)NULL
;
493 fprintf(config
.map_file
,"%-16s", m
->name
);
494 print_address(m
->origin
);
496 print_address(m
->length
);
498 print_flags( &m
->flags
);
499 fprintf(config
.map_file
,"\n");
501 fprintf(config
.map_file
,"\n\n**LINK EDITOR MEMORY MAP**\n\n");
502 fprintf(config
.map_file
,"output input virtual\n");
503 fprintf(config
.map_file
,"section section address tsize\n\n");
514 lang_output_section_statement_type
*s
)
516 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
517 section_userdata_type
*new =
518 (section_userdata_type
*)
519 stat_alloc((bfd_size_type
)(sizeof(section_userdata_type
)));
521 s
->bfd_section
= bfd_get_section_by_name(output_bfd
, s
->name
);
522 if (s
->bfd_section
== (asection
*)NULL
)
523 s
->bfd_section
= bfd_make_section(output_bfd
, s
->name
);
524 if (s
->bfd_section
== (asection
*)NULL
) {
525 einfo("%P%F output format %s cannot represent section called %s\n",
526 output_bfd
->xvec
->name
, s
->name
);
528 s
->bfd_section
->output_section
= s
->bfd_section
;
529 /* s->bfd_section->flags = s->flags;*/
531 /* We initialize an output sections output offset to minus its own */
532 /* vma to allow us to output a section through itself */
533 s
->bfd_section
->output_offset
= 0;
534 get_userdata( s
->bfd_section
) = (PTR
)new;
538 /***********************************************************************
541 These expand statements like *(.text) and foo.o to a list of
542 explicit actions, like foo.o(.text), bar.o(.text) and
545 The toplevel routine, wild, takes a statement, section, file and
546 target. If either the section or file is null it is taken to be the
547 wildcard. Seperate lang_input_section statements are created for
548 each part of the expanstion, and placed after the statement provided.
553 DEFUN(wild_doit
,(ptr
, section
, output
, file
),
554 lang_statement_list_type
*ptr AND
555 asection
*section AND
556 lang_output_section_statement_type
*output AND
557 lang_input_statement_type
*file
)
559 if(output
->bfd_section
== (asection
*)NULL
)
564 if (section
!= (asection
*)NULL
565 && section
->output_section
== (asection
*)NULL
) {
566 /* Add a section reference to the list */
567 lang_input_section_type
*new = new_stat(lang_input_section
, ptr
);
569 new->section
= section
;
571 section
->output_section
= output
->bfd_section
;
572 section
->output_section
->flags
|= section
->flags
;
573 if (section
->alignment_power
> output
->bfd_section
->alignment_power
) {
574 output
->bfd_section
->alignment_power
= section
->alignment_power
;
580 DEFUN(our_bfd_get_section_by_name
,(abfd
, section
),
584 return bfd_get_section_by_name(abfd
, section
);
588 DEFUN(wild_section
,(ptr
, section
, file
, output
),
589 lang_wild_statement_type
*ptr AND
590 CONST
char *section AND
591 lang_input_statement_type
*file AND
592 lang_output_section_statement_type
*output
)
595 if (file
->just_syms_flag
== false) {
596 if (section
== (char *)NULL
) {
597 /* Do the creation to all sections in the file */
598 for (s
= file
->the_bfd
->sections
; s
!= (asection
*)NULL
; s
=s
->next
) {
599 wild_doit(&ptr
->children
, s
, output
, file
);
603 /* Do the creation to the named section only */
604 wild_doit(&ptr
->children
,
605 our_bfd_get_section_by_name(file
->the_bfd
, section
),
612 /* passed a file name (which must have been seen already and added to
613 the statement tree. We will see if it has been opened already and
614 had its symbols read. If not then we'll read it.
616 Archives are pecuilar here. We may open them once, but if they do
617 not define anything we need at the time, they won't have all their
618 symbols read. If we need them later, we'll have to redo it.
621 lang_input_statement_type
*
622 DEFUN(lookup_name
,(name
),
623 CONST
char * CONST name
)
625 lang_input_statement_type
*search
;
626 for(search
= (lang_input_statement_type
*)input_file_chain
.head
;
627 search
!= (lang_input_statement_type
*)NULL
;
628 search
= (lang_input_statement_type
*)search
->next_real_file
)
630 if (search
->filename
== (char *)NULL
&& name
== (char *)NULL
) {
633 if (search
->filename
!= (char *)NULL
&& name
!= (char *)NULL
) {
634 if (strcmp(search
->filename
, name
) == 0) {
635 ldmain_open_file_read_symbol(search
);
641 /* There isn't an afile entry for this file yet, this must be
642 because the name has only appeared inside a load script and not
643 on the command line */
644 search
= new_afile(name
, lang_input_file_is_file_enum
, default_target
);
645 ldmain_open_file_read_symbol(search
);
652 DEFUN(wild
,(s
, section
, file
, target
, output
),
653 lang_wild_statement_type
*s AND
654 CONST
char *CONST section AND
655 CONST
char *CONST file AND
656 CONST
char *CONST target AND
657 lang_output_section_statement_type
*output
)
659 lang_input_statement_type
*f
;
660 if (file
== (char *)NULL
) {
661 /* Perform the iteration over all files in the list */
662 for (f
= (lang_input_statement_type
*)file_chain
.head
;
663 f
!= (lang_input_statement_type
*)NULL
;
664 f
= (lang_input_statement_type
*)f
->next
) {
665 wild_section(s
, section
, f
, output
);
669 /* Perform the iteration over a single file */
670 wild_section( s
, section
, lookup_name(file
), output
);
672 if (section
!= (char *)NULL
673 && strcmp(section
,"COMMON") == 0
674 && default_common_section
== (lang_output_section_statement_type
*)NULL
)
676 /* Remember the section that common is going to incase we later
677 get something which doesn't know where to put it */
678 default_common_section
= output
;
683 read in all the files
686 DEFUN(open_output
,(name
),
687 CONST
char *CONST name
)
689 extern CONST
char *output_filename
;
691 if (output_target
== (char *)NULL
) {
692 if (current_target
!= (char *)NULL
)
693 output_target
= current_target
;
695 output_target
= default_target
;
697 output
= bfd_openw(name
, output_target
);
698 output_filename
= name
;
700 if (output
== (bfd
*)NULL
)
702 if (bfd_error
== invalid_target
) {
703 einfo("%P%F target %s not found\n", output_target
);
705 einfo("%P%F problem opening output file %s, %E", name
);
708 /* output->flags |= D_PAGED;*/
709 bfd_set_format(output
, bfd_object
);
717 DEFUN(ldlang_open_output
,(statement
),
718 lang_statement_union_type
*statement
)
720 switch (statement
->header
.type
)
722 case lang_output_statement_enum
:
723 output_bfd
= open_output(statement
->output_statement
.name
);
724 ldemul_set_output_arch();
725 if (config
.magic_demand_paged
&& !config
.relocateable_output
)
726 output_bfd
->flags
|= D_PAGED
;
728 output_bfd
->flags
&= ~D_PAGED
;
729 if (config
.text_read_only
)
730 output_bfd
->flags
|= WP_TEXT
;
732 output_bfd
->flags
&= ~WP_TEXT
;
735 case lang_target_statement_enum
:
736 current_target
= statement
->target_statement
.target
;
744 DEFUN(open_input_bfds
,(statement
),
745 lang_statement_union_type
*statement
)
747 switch (statement
->header
.type
)
749 case lang_target_statement_enum
:
750 current_target
= statement
->target_statement
.target
;
752 case lang_wild_statement_enum
:
753 /* Maybe we should load the file's symbols */
754 if (statement
->wild_statement
.filename
)
756 (void) lookup_name(statement
->wild_statement
.filename
);
759 case lang_input_statement_enum
:
760 if (statement
->input_statement
.real
== true)
762 statement
->input_statement
.target
= current_target
;
763 lookup_name(statement
->input_statement
.filename
);
770 /* If there are [COMMONS] statements, put a wild one into the bss section */
773 lang_reasonable_defaults()
779 lang_output_section_statement_lookup(".text");
780 lang_output_section_statement_lookup(".data");
782 default_common_section
=
783 lang_output_section_statement_lookup(".bss");
786 if (placed_commons
== false) {
787 lang_wild_statement_type
*new =
788 new_stat(lang_wild_statement
,
789 &default_common_section
->children
);
790 new->section_name
= "COMMON";
791 new->filename
= (char *)NULL
;
792 lang_list_init(&new->children
);
799 Add the supplied name to the symbol table as an undefined reference.
800 Remove items from the chain as we open input bfds
802 typedef struct ldlang_undef_chain_list
{
803 struct ldlang_undef_chain_list
*next
;
805 } ldlang_undef_chain_list_type
;
807 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
810 DEFUN(ldlang_add_undef
,(name
),
811 CONST
char *CONST name
)
813 ldlang_undef_chain_list_type
*new =
814 (ldlang_undef_chain_list_type
815 *)stat_alloc((bfd_size_type
)(sizeof(ldlang_undef_chain_list_type
)));
817 new->next
= ldlang_undef_chain_list_head
;
818 ldlang_undef_chain_list_head
= new;
820 new->name
= buystring(name
);
822 /* Run through the list of undefineds created above and place them
823 into the linker hash table as undefined symbols belonging to the
827 DEFUN_VOID(lang_place_undefineds
)
829 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
830 while (ptr
!= (ldlang_undef_chain_list_type
*)NULL
) {
832 asymbol
**def_ptr
= (asymbol
**)stat_alloc((bfd_size_type
)(sizeof(asymbol
**)));
833 def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
835 def
->name
= ptr
->name
;
836 def
->section
= &bfd_und_section
;
837 Q_enter_global_ref(def_ptr
, ptr
->name
);
844 /* Copy important data from out internal form to the bfd way. Also
845 create a section for the dummy file
849 DEFUN_VOID(lang_create_output_section_statements
)
851 lang_statement_union_type
*os
;
852 for (os
= lang_output_section_statement
.head
;
853 os
!= (lang_statement_union_type
*)NULL
;
854 os
= os
->output_section_statement
.next
) {
855 lang_output_section_statement_type
*s
=
856 &os
->output_section_statement
;
863 DEFUN_VOID(lang_init_script_file
)
865 script_file
= lang_add_input_file("script file",
866 lang_input_file_is_fake_enum
,
868 script_file
->the_bfd
= bfd_create("script file", output_bfd
);
869 script_file
->symbol_count
= 0;
870 script_file
->the_bfd
->sections
= output_bfd
->sections
;
871 abs_output_section
= lang_output_section_statement_lookup(BFD_ABS_SECTION_NAME
);
873 abs_output_section
->bfd_section
= &bfd_abs_section
;
880 /* Open input files and attatch to output sections */
882 DEFUN(map_input_to_output_sections
,(s
, target
, output_section_statement
),
883 lang_statement_union_type
*s AND
884 CONST
char *target AND
885 lang_output_section_statement_type
*output_section_statement
)
887 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
889 switch (s
->header
.type
) {
892 case lang_wild_statement_enum
:
893 wild(&s
->wild_statement
, s
->wild_statement
.section_name
,
894 s
->wild_statement
.filename
, target
,
895 output_section_statement
);
898 case lang_constructors_statement_enum
:
899 map_input_to_output_sections(constructor_list
.head
,
901 output_section_statement
);
903 case lang_output_section_statement_enum
:
904 map_input_to_output_sections(s
->output_section_statement
.children
.head
,
906 &s
->output_section_statement
);
908 case lang_output_statement_enum
:
910 case lang_target_statement_enum
:
911 target
= s
->target_statement
.target
;
913 case lang_fill_statement_enum
:
914 case lang_input_section_enum
:
915 case lang_object_symbols_statement_enum
:
916 case lang_data_statement_enum
:
917 case lang_assignment_statement_enum
:
918 case lang_padding_statement_enum
:
920 case lang_afile_asection_pair_statement_enum
:
923 case lang_address_statement_enum
:
924 /* Mark the specified section with the supplied address */
926 lang_output_section_statement_type
*os
=
927 lang_output_section_statement_lookup
928 (s
->address_statement
.section_name
);
929 os
->addr_tree
= s
->address_statement
.address
;
930 if (os
->bfd_section
== (asection
*)NULL
) {
931 einfo("%P%F can't set the address of undefined section %s\n",
932 s
->address_statement
.section_name
);
936 case lang_input_statement_enum
:
937 /* A standard input statement, has no wildcards */
938 /* ldmain_open_file_read_symbol(&s->input_statement);*/
949 DEFUN(print_output_section_statement
,(output_section_statement
),
950 lang_output_section_statement_type
*output_section_statement
)
952 asection
*section
= output_section_statement
->bfd_section
;
954 print_section(output_section_statement
->name
);
957 print_dot
= section
->vma
;
961 print_address(section
->vma
);
963 print_size(bfd_get_section_size_before_reloc(section
));
965 print_alignment(section
->alignment_power
);
968 fprintf(config
.map_file
, "%s flags", output_section_statement
->region
->name
);
969 print_flags(stdout
, &output_section_statement
->flags
);
971 if (section
->flags
& SEC_LOAD
)
972 fprintf(config
.map_file
, "load ");
973 if (section
->flags
& SEC_ALLOC
)
974 fprintf(config
.map_file
, "alloc ");
975 if (section
->flags
& SEC_RELOC
)
976 fprintf(config
.map_file
, "reloc ");
977 if (section
->flags
& SEC_HAS_CONTENTS
)
978 fprintf(config
.map_file
, "contents ");
982 fprintf(config
.map_file
, "No attached output section");
985 print_statement(output_section_statement
->children
.head
,
986 output_section_statement
);
991 DEFUN(print_assignment
,(assignment
, output_section
),
992 lang_assignment_statement_type
*assignment AND
993 lang_output_section_statement_type
*output_section
)
995 etree_value_type result
;
1000 print_address(print_dot
);
1002 result
= exp_fold_tree(assignment
->exp
->assign
.src
,
1004 lang_final_phase_enum
,
1009 print_address(result
.value
);
1013 fprintf(config
.map_file
, "*undefined*");
1016 exp_print_tree(assignment
->exp
);
1018 fprintf(config
.map_file
, "\n");
1022 DEFUN(print_input_statement
,(statm
),
1023 lang_input_statement_type
*statm
)
1025 if (statm
->filename
!= (char *)NULL
) {
1026 fprintf(config
.map_file
, "LOAD %s\n",statm
->filename
);
1031 DEFUN(print_symbol
,(q
),
1035 fprintf(config
.map_file
, " ");
1037 fprintf(config
.map_file
, " ");
1038 print_address(outside_symbol_address(q
));
1039 fprintf(config
.map_file
, " %s", q
->name
? q
->name
: " ");
1044 DEFUN(print_input_section
,(in
),
1045 lang_input_section_type
*in
)
1047 asection
*i
= in
->section
;
1048 int size
= i
->reloc_done
?
1049 bfd_get_section_size_after_reloc(i
) :
1050 bfd_get_section_size_before_reloc(i
);
1055 fprintf(config
.map_file
, " ");
1056 print_section(i
->name
);
1057 fprintf(config
.map_file
, " ");
1058 if (i
->output_section
) {
1059 print_address(i
->output_section
->vma
+ i
->output_offset
);
1060 fprintf(config
.map_file
, " ");
1062 fprintf(config
.map_file
, " ");
1063 print_alignment(i
->alignment_power
);
1064 fprintf(config
.map_file
, " ");
1067 bfd
*abfd
= in
->ifile
->the_bfd
;
1068 if (in
->ifile
->just_syms_flag
== true) {
1069 fprintf(config
.map_file
, "symbols only ");
1072 fprintf(config
.map_file
, " %s ",abfd
->xvec
->name
);
1073 if(abfd
->my_archive
!= (bfd
*)NULL
) {
1074 fprintf(config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1078 fprintf(config
.map_file
, "%s", abfd
->filename
);
1080 fprintf(config
.map_file
, "(overhead %d bytes)", (int)bfd_alloc_size(abfd
));
1083 /* Find all the symbols in this file defined in this section */
1084 if (in
->ifile
->symbol_count
)
1087 for (p
= in
->ifile
->asymbols
; *p
; p
++) {
1090 if (bfd_get_section(q
) == i
&& q
->flags
& BSF_GLOBAL
) {
1101 print_dot
= outside_section_address(i
) + size
;
1104 fprintf(config
.map_file
, "No output section allocated\n");
1110 DEFUN(print_fill_statement
,(fill
),
1111 lang_fill_statement_type
*fill
)
1113 fprintf(config
.map_file
, "FILL mask ");
1114 print_fill( fill
->fill
);
1118 DEFUN(print_data_statement
,(data
),
1119 lang_data_statement_type
*data
)
1121 /* bfd_vma value; */
1126 /* ASSERT(print_dot == data->output_vma);*/
1128 print_address(data
->output_vma
+ data
->output_section
->vma
);
1130 print_address(data
->value
);
1132 switch (data
->type
) {
1134 fprintf(config
.map_file
, "BYTE ");
1135 print_dot
+= BYTE_SIZE
;
1138 fprintf(config
.map_file
, "SHORT ");
1139 print_dot
+= SHORT_SIZE
;
1142 fprintf(config
.map_file
, "LONG ");
1143 print_dot
+= LONG_SIZE
;
1147 exp_print_tree(data
->exp
);
1149 fprintf(config
.map_file
, "\n");
1154 DEFUN(print_padding_statement
,(s
),
1155 lang_padding_statement_type
*s
)
1159 print_section("*fill*");
1161 print_address(s
->output_offset
+ s
->output_section
->vma
);
1163 print_size(s
->size
);
1165 print_fill(s
->fill
);
1168 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1173 DEFUN(print_wild_statement
,(w
,os
),
1174 lang_wild_statement_type
*w AND
1175 lang_output_section_statement_type
*os
)
1177 fprintf(config
.map_file
, " from ");
1178 if (w
->filename
!= (char *)NULL
) {
1179 fprintf(config
.map_file
, "%s",w
->filename
);
1182 fprintf(config
.map_file
, "*");
1184 if (w
->section_name
!= (char *)NULL
) {
1185 fprintf(config
.map_file
, "(%s)",w
->section_name
);
1188 fprintf(config
.map_file
, "(*)");
1191 print_statement(w
->children
.head
, os
);
1195 DEFUN(print_statement
,(s
, os
),
1196 lang_statement_union_type
*s AND
1197 lang_output_section_statement_type
*os
)
1201 switch (s
->header
.type
)
1203 case lang_constructors_statement_enum
:
1204 fprintf(config
.map_file
, "constructors:\n");
1205 print_statement(constructor_list
.head
, os
);
1207 case lang_wild_statement_enum
:
1208 print_wild_statement(&s
->wild_statement
, os
);
1211 fprintf(config
.map_file
, "Fail with %d\n",s
->header
.type
);
1214 case lang_address_statement_enum
:
1215 fprintf(config
.map_file
, "address\n");
1217 case lang_object_symbols_statement_enum
:
1218 fprintf(config
.map_file
, "object symbols\n");
1220 case lang_fill_statement_enum
:
1221 print_fill_statement(&s
->fill_statement
);
1223 case lang_data_statement_enum
:
1224 print_data_statement(&s
->data_statement
);
1226 case lang_input_section_enum
:
1227 print_input_section(&s
->input_section
);
1229 case lang_padding_statement_enum
:
1230 print_padding_statement(&s
->padding_statement
);
1232 case lang_output_section_statement_enum
:
1233 print_output_section_statement(&s
->output_section_statement
);
1235 case lang_assignment_statement_enum
:
1236 print_assignment(&s
->assignment_statement
,
1239 case lang_target_statement_enum
:
1240 fprintf(config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1242 case lang_output_statement_enum
:
1243 fprintf(config
.map_file
, "OUTPUT(%s %s)\n",
1244 s
->output_statement
.name
,
1247 case lang_input_statement_enum
:
1248 print_input_statement(&s
->input_statement
);
1250 case lang_afile_asection_pair_statement_enum
:
1260 DEFUN_VOID(print_statements
)
1262 print_statement(statement_list
.head
,
1263 abs_output_section
);
1268 DEFUN(insert_pad
,(this_ptr
, fill
, power
, output_section_statement
, dot
),
1269 lang_statement_union_type
**this_ptr AND
1271 unsigned int power AND
1272 asection
* output_section_statement AND
1275 /* Align this section first to the
1276 input sections requirement, then
1277 to the output section's requirement.
1278 If this alignment is > than any seen before,
1279 then record it too. Perform the alignment by
1280 inserting a magic 'padding' statement.
1283 unsigned int alignment_needed
= align_power(dot
, power
) - dot
;
1285 if (alignment_needed
!= 0)
1287 lang_statement_union_type
*new =
1288 (lang_statement_union_type
*)
1289 stat_alloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1290 /* Link into existing chain */
1291 new->header
.next
= *this_ptr
;
1293 new->header
.type
= lang_padding_statement_enum
;
1294 new->padding_statement
.output_section
= output_section_statement
;
1295 new->padding_statement
.output_offset
=
1296 dot
- output_section_statement
->vma
;
1297 new->padding_statement
.fill
= fill
;
1298 new->padding_statement
.size
= alignment_needed
;
1302 /* Remember the most restrictive alignment */
1303 if (power
> output_section_statement
->alignment_power
) {
1304 output_section_statement
->alignment_power
= power
;
1306 output_section_statement
->_raw_size
+= alignment_needed
;
1307 return alignment_needed
+ dot
;
1311 /* Work out how much this section will move the dot point */
1313 DEFUN(size_input_section
, (this_ptr
, output_section_statement
, fill
, dot
),
1314 lang_statement_union_type
**this_ptr AND
1315 lang_output_section_statement_type
*output_section_statement AND
1316 unsigned short fill AND
1319 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1320 asection
*i
= is
->section
;
1322 if (is
->ifile
->just_syms_flag
== false) {
1323 dot
= insert_pad(this_ptr
, fill
, i
->alignment_power
,
1324 output_section_statement
->bfd_section
, dot
);
1326 /* remember the largest size so we can malloc the largest area
1327 needed for the output stage. Only remember the size of sections
1328 which we will actually allocate */
1330 (SEC_HAS_CONTENTS
| SEC_LOAD
)) == (SEC_HAS_CONTENTS
| SEC_LOAD
))
1331 && (bfd_get_section_size_before_reloc(i
) > largest_section
)) {
1332 largest_section
= bfd_get_section_size_before_reloc(i
);
1335 /* Remember where in the output section this input section goes */
1337 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1339 /* Mark how big the output section must be to contain this now */
1340 dot
+= bfd_get_section_size_before_reloc(i
);
1341 output_section_statement
->bfd_section
->_raw_size
=
1342 dot
- output_section_statement
->bfd_section
->vma
;
1346 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1353 /* Sizing happens in two passes, first pass we allocate worst case
1354 stuff. The second pass (if relaxing), we use what we learnt to
1355 change the size of some relocs from worst case to better
1357 static boolean had_relax
;
1360 DEFUN(lang_size_sections
,(s
, output_section_statement
, prev
, fill
,
1362 lang_statement_union_type
*s AND
1363 lang_output_section_statement_type
* output_section_statement AND
1364 lang_statement_union_type
**prev AND
1365 unsigned short fill AND
1369 /* Size up the sections from their constituent parts */
1370 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1372 switch (s
->header
.type
) {
1374 case lang_output_section_statement_enum
:
1377 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1379 if (os
->bfd_section
== &bfd_abs_section
)
1381 /* No matter what happens, an abs section starts at zero */
1382 os
->bfd_section
->vma
= 0;
1386 if (os
->addr_tree
== (etree_type
*)NULL
)
1388 /* No address specified for this section, get one
1389 from the region specification
1391 if (os
->region
== (lang_memory_region_type
*)NULL
) {
1392 os
->region
= lang_memory_region_lookup("*default*");
1394 dot
= os
->region
->current
;
1398 etree_value_type r
;
1399 r
= exp_fold_tree(os
->addr_tree
,
1401 lang_allocating_phase_enum
,
1403 if (r
.valid
== false)
1405 einfo("%F%S: non constant address expression for section %s\n",
1410 /* The section starts here */
1411 /* First, align to what the section needs */
1414 dot
= align_power(dot
, os
->bfd_section
->alignment_power
);
1415 os
->bfd_section
->vma
= dot
;
1419 os
->bfd_section
->output_offset
= 0;
1421 (void) lang_size_sections(os
->children
.head
, os
, &os
->children
.head
,
1422 os
->fill
, dot
, relax
);
1423 /* Ignore the size of the input sections, use the vma and size to */
1427 after
= ALIGN(os
->bfd_section
->vma
+
1428 os
->bfd_section
->_raw_size
,
1432 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1433 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1434 os
->processed
= true;
1436 /* Replace into region ? */
1437 if (os
->addr_tree
== (etree_type
*)NULL
1438 && os
->region
!=(lang_memory_region_type
*)NULL
) {
1439 os
->region
->current
= dot
;
1440 /* Make sure this isn't silly */
1441 if (os
->region
->current
>
1442 os
->region
->origin
+
1445 einfo("%X%P: Region %s is full (%B section %s)\n",
1447 os
->bfd_section
->owner
,
1448 os
->bfd_section
->name
);
1449 /* Reset the region pointer */
1450 os
->region
->current
= 0;
1458 case lang_constructors_statement_enum
:
1459 dot
= lang_size_sections(constructor_list
.head
,
1460 output_section_statement
,
1461 &s
->wild_statement
.children
.head
,
1466 case lang_data_statement_enum
:
1468 unsigned int size
= 0;
1469 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1470 s
->data_statement
.output_section
=
1471 output_section_statement
->bfd_section
;
1473 switch (s
->data_statement
.type
) {
1486 output_section_statement
->bfd_section
->_raw_size
+= size
;
1490 case lang_wild_statement_enum
:
1492 dot
= lang_size_sections(s
->wild_statement
.children
.head
,
1493 output_section_statement
,
1494 &s
->wild_statement
.children
.head
,
1500 case lang_object_symbols_statement_enum
:
1501 create_object_symbols
= output_section_statement
;
1503 case lang_output_statement_enum
:
1504 case lang_target_statement_enum
:
1506 case lang_input_section_enum
:
1511 had_relax
= had_relax
|| relax_section(prev
);
1516 dot
= size_input_section(prev
,
1517 output_section_statement
,
1518 output_section_statement
->fill
, dot
);
1520 case lang_input_statement_enum
:
1522 case lang_fill_statement_enum
:
1523 s
->fill_statement
.output_section
=output_section_statement
->bfd_section
;
1525 fill
= s
->fill_statement
.fill
;
1527 case lang_assignment_statement_enum
:
1529 bfd_vma newdot
= dot
;
1530 exp_fold_tree(s
->assignment_statement
.exp
,
1531 output_section_statement
,
1532 lang_allocating_phase_enum
,
1536 if (newdot
!= dot
&& !relax
)
1537 /* We've been moved ! so insert a pad */
1539 lang_statement_union_type
*new =
1540 (lang_statement_union_type
*)
1541 stat_alloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1542 /* Link into existing chain */
1543 new->header
.next
= *prev
;
1545 new->header
.type
= lang_padding_statement_enum
;
1546 new->padding_statement
.output_section
=
1547 output_section_statement
->bfd_section
;
1548 new->padding_statement
.output_offset
=
1549 dot
- output_section_statement
->bfd_section
->vma
;
1550 new->padding_statement
.fill
= fill
;
1551 new->padding_statement
.size
= newdot
- dot
;
1552 output_section_statement
->bfd_section
->_raw_size
+=
1553 new->padding_statement
.size
;
1562 /* This can only get here when relaxing is turned on */
1563 case lang_padding_statement_enum
:
1565 case lang_address_statement_enum
:
1568 prev
= &s
->header
.next
;
1575 DEFUN(lang_do_assignments
,(s
, output_section_statement
, fill
, dot
),
1576 lang_statement_union_type
*s AND
1577 lang_output_section_statement_type
* output_section_statement AND
1578 unsigned short fill AND
1582 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1584 switch (s
->header
.type
) {
1585 case lang_constructors_statement_enum
:
1586 dot
= lang_do_assignments(constructor_list
.head
,
1587 output_section_statement
,
1592 case lang_output_section_statement_enum
:
1594 lang_output_section_statement_type
*os
=
1595 &(s
->output_section_statement
);
1596 dot
= os
->bfd_section
->vma
;
1597 (void) lang_do_assignments(os
->children
.head
, os
, os
->fill
, dot
);
1598 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1601 case lang_wild_statement_enum
:
1603 dot
= lang_do_assignments(s
->wild_statement
.children
.head
,
1604 output_section_statement
,
1609 case lang_object_symbols_statement_enum
:
1610 case lang_output_statement_enum
:
1611 case lang_target_statement_enum
:
1613 case lang_common_statement_enum
:
1616 case lang_data_statement_enum
:
1618 etree_value_type value
;
1619 value
= exp_fold_tree(s
->data_statement
.exp
,
1621 lang_final_phase_enum
, dot
, &dot
);
1622 s
->data_statement
.value
= value
.value
;
1623 if (value
.valid
== false) einfo("%F%P: Invalid data statement\n");
1625 switch (s
->data_statement
.type
) {
1637 case lang_input_section_enum
:
1639 asection
*in
= s
->input_section
.section
;
1640 dot
+= bfd_get_section_size_before_reloc(in
);
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 exp_fold_tree(s
->assignment_statement
.exp
,
1652 output_section_statement
,
1653 lang_final_phase_enum
,
1659 case lang_padding_statement_enum
:
1660 dot
+= s
->padding_statement
.size
;
1665 case lang_address_statement_enum
:
1676 DEFUN_VOID(lang_relocate_globals
)
1680 Each ldsym_type maintains a chain of pointers to asymbols which
1681 references the definition. Replace each pointer to the referenence
1682 with a pointer to only one place, preferably the definition. If
1683 the defintion isn't available then the common symbol, and if
1684 there isn't one of them then choose one reference.
1687 FOR_EACH_LDSYM(lgs
) {
1689 if (lgs
->sdefs_chain
) {
1690 it
= *(lgs
->sdefs_chain
);
1692 else if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1693 it
= *(lgs
->scoms_chain
);
1695 else if (lgs
->srefs_chain
!= (asymbol
**)NULL
) {
1696 it
= *(lgs
->srefs_chain
);
1699 /* This can happen when the command line asked for a symbol to
1701 it
= (asymbol
*)NULL
;
1703 if (it
!= (asymbol
*)NULL
)
1705 asymbol
**ptr
= lgs
->srefs_chain
;;
1706 if (lgs
->flags
& SYM_WARNING
)
1708 produce_warnings(lgs
, it
);
1711 while (ptr
!= (asymbol
**)NULL
) {
1712 asymbol
*ref
= *ptr
;
1714 ptr
= (asymbol
**)(ref
->udata
);
1723 DEFUN_VOID(lang_finish
)
1727 if (entry_symbol
== (char *)NULL
) {
1728 /* No entry has been specified, look for start */
1729 entry_symbol
= "start";
1731 lgs
= ldsym_get_soft(entry_symbol
);
1732 if (lgs
&& lgs
->sdefs_chain
) {
1733 asymbol
*sy
= *(lgs
->sdefs_chain
);
1734 /* We can set the entry address*/
1735 bfd_set_start_address(output_bfd
,
1736 outside_symbol_address(sy
));
1740 /* Can't find anything reasonable,
1741 use the first address in the text section
1743 asection
*ts
= bfd_get_section_by_name(output_bfd
, ".text");
1745 bfd_set_start_address(output_bfd
, ts
->vma
);
1750 /* By now we know the target architecture, and we may have an */
1751 /* ldfile_output_machine_name */
1753 DEFUN_VOID(lang_check
)
1755 lang_statement_union_type
*file
;
1757 unsigned long input_machine
;
1758 enum bfd_architecture input_architecture
;
1759 CONST bfd_arch_info_type
*compatible
;
1761 for (file
= file_chain
.head
;
1762 file
!= (lang_statement_union_type
*)NULL
;
1763 file
=file
->input_statement
.next
)
1765 unsigned long ldfile_new_output_machine
= 0;
1766 enum bfd_architecture ldfile_new_output_architecture
= bfd_arch_unknown
;
1768 input_bfd
= file
->input_statement
.the_bfd
;
1770 input_machine
= bfd_get_mach(input_bfd
);
1771 input_architecture
= bfd_get_arch(input_bfd
);
1774 /* Inspect the architecture and ensure we're linking like with
1777 compatible
=bfd_arch_get_compatible(input_bfd
,
1782 ldfile_output_machine
= compatible
->mach
;
1783 ldfile_output_architecture
= compatible
->arch
;
1788 info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1789 bfd_printable_name(input_bfd
), input_bfd
,
1790 bfd_printable_name(output_bfd
));
1792 bfd_set_arch_mach(output_bfd
,
1793 ldfile_new_output_architecture
,
1794 ldfile_new_output_machine
);
1802 * run through all the global common symbols and tie them
1803 * to the output section requested.
1805 As an experiment we do this 4 times, once for all the byte sizes,
1806 then all the two bytes, all the four bytes and then everything else
1810 DEFUN_VOID(lang_common
)
1814 if (config
.relocateable_output
== false ||
1815 command_line
.force_common_definition
== true) {
1816 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<=1) {
1817 for (lgs
= symbol_head
;
1818 lgs
!= (ldsym_type
*)NULL
;
1822 unsigned int power_of_two
;
1825 if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1826 com
= *(lgs
->scoms_chain
);
1855 if (config
.sort_common
== false || align
== power
) {
1856 /* Change from a common symbol into a definition of
1858 lgs
->sdefs_chain
= lgs
->scoms_chain
;
1859 lgs
->scoms_chain
= (asymbol
**)NULL
;
1861 /* Point to the correct common section */
1863 ((lang_input_statement_type
*)
1864 (com
->the_bfd
->usrdata
))->common_section
;
1865 /* Fix the size of the common section */
1867 com
->section
->_raw_size
=
1868 ALIGN(com
->section
->_raw_size
, align
);
1870 /* Remember if this is the biggest alignment ever seen */
1871 if (power_of_two
> com
->section
->alignment_power
) {
1872 com
->section
->alignment_power
= power_of_two
;
1875 /* Symbol stops being common and starts being global, but
1876 we remember that it was common once. */
1878 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
1879 com
->value
= com
->section
->_raw_size
;
1883 fprintf (config
.map_file
, "Allocating common %s: %x at %x %s\n",
1886 (unsigned) com
->value
,
1887 com
->the_bfd
->filename
);
1890 com
->section
->_raw_size
+= size
;
1903 run through the input files and ensure that every input
1904 section has somewhere to go. If one is found without
1905 a destination then create an input request and place it
1906 into the statement tree.
1910 DEFUN_VOID(lang_place_orphans
)
1912 lang_input_statement_type
*file
;
1913 for (file
= (lang_input_statement_type
*)file_chain
.head
;
1914 file
!= (lang_input_statement_type
*)NULL
;
1915 file
= (lang_input_statement_type
*)file
->next
) {
1917 for (s
= file
->the_bfd
->sections
;
1918 s
!= (asection
*)NULL
;
1920 if ( s
->output_section
== (asection
*)NULL
) {
1921 /* This section of the file is not attatched, root
1922 around for a sensible place for it to go */
1924 if (file
->common_section
== s
) {
1925 /* This is a lonely common section which must
1926 have come from an archive. We attatch to the
1927 section with the wildcard */
1928 if (config
.relocateable_output
!= true
1929 && command_line
.force_common_definition
== false) {
1930 if (default_common_section
==
1931 (lang_output_section_statement_type
*)NULL
) {
1932 info("%P: No [COMMON] command, defaulting to .bss\n");
1934 default_common_section
=
1935 lang_output_section_statement_lookup(".bss");
1938 wild_doit(&default_common_section
->children
, s
,
1939 default_common_section
, file
);
1943 lang_output_section_statement_type
*os
=
1944 lang_output_section_statement_lookup(s
->name
);
1946 wild_doit(&os
->children
, s
, os
, file
);
1955 DEFUN(lang_set_flags
,(ptr
, flags
),
1959 boolean state
= false;
1963 if (*flags
== '!') {
1970 /* ptr->flag_read = state; */
1973 /* ptr->flag_write = state; */
1976 /* ptr->flag_executable= state;*/
1980 /* ptr->flag_loadable= state;*/
1983 einfo("%P%F illegal syntax in flags\n");
1993 DEFUN(lang_for_each_file
,(func
),
1994 PROTO(void, (*func
),(lang_input_statement_type
*)))
1996 lang_input_statement_type
*f
;
1997 for (f
= (lang_input_statement_type
*)file_chain
.head
;
1998 f
!= (lang_input_statement_type
*)NULL
;
1999 f
= (lang_input_statement_type
*)f
->next
)
2007 DEFUN(lang_for_each_input_section
, (func
),
2008 PROTO(void ,(*func
),(bfd
*ab
, asection
*as
)))
2010 lang_input_statement_type
*f
;
2011 for (f
= (lang_input_statement_type
*)file_chain
.head
;
2012 f
!= (lang_input_statement_type
*)NULL
;
2013 f
= (lang_input_statement_type
*)f
->next
)
2016 for (s
= f
->the_bfd
->sections
;
2017 s
!= (asection
*)NULL
;
2019 func(f
->the_bfd
, s
);
2027 DEFUN(ldlang_add_file
,(entry
),
2028 lang_input_statement_type
*entry
)
2031 lang_statement_append(&file_chain
,
2032 (lang_statement_union_type
*)entry
,
2039 DEFUN(lang_add_output
,(name
),
2042 lang_output_statement_type
*new = new_stat(lang_output_statement
,
2045 had_output_filename
= true;
2049 static lang_output_section_statement_type
*current_section
;
2052 DEFUN(lang_enter_output_section_statement
,
2053 (output_section_statement_name
,
2057 char *output_section_statement_name AND
2058 etree_type
*address_exp AND
2060 bfd_vma block_value
)
2062 lang_output_section_statement_type
*os
;
2065 lang_output_section_statement_lookup(output_section_statement_name
);
2069 /* Add this statement to tree */
2070 /* add_statement(lang_output_section_statement_enum,
2071 output_section_statement);*/
2072 /* Make next things chain into subchain of this */
2074 if (os
->addr_tree
==
2075 (etree_type
*)NULL
) {
2080 os
->block_value
= block_value
;
2081 stat_ptr
= & os
->children
;
2087 DEFUN_VOID(lang_final
)
2089 if (had_output_filename
== false) {
2090 extern CONST
char *output_filename
;
2091 lang_add_output(output_filename
);
2097 /* Reset the current counters in the regions */
2099 DEFUN_VOID(reset_memory_regions
)
2101 lang_memory_region_type
*p
= lang_memory_region_list
;
2102 for (p
= lang_memory_region_list
;
2103 p
!= ( lang_memory_region_type
*)NULL
;
2106 p
->current
= p
->origin
;
2112 DEFUN(create_symbol
,(name
, flags
, section
),
2113 CONST
char *name AND
2117 extern lang_input_statement_type
*script_file
;
2118 asymbol
**def_ptr
= (asymbol
**)stat_alloc((bfd_size_type
)(sizeof(asymbol
**)));
2119 /* Add this definition to script file */
2120 asymbol
*def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
2121 def
->name
= buystring(name
);
2124 def
->section
= section
;
2127 Q_enter_global_ref(def_ptr
, name
);
2134 DEFUN_VOID(lang_process
)
2137 if (had_script
== false) {
2138 parse_line(ldemul_get_script(),1);
2140 lang_reasonable_defaults();
2141 current_target
= default_target
;
2143 lang_for_each_statement(ldlang_open_output
); /* Open the output file */
2144 /* For each output section statement, create a section in the output
2146 lang_create_output_section_statements();
2148 /* Create a dummy bfd for the script */
2149 lang_init_script_file();
2151 /* Add to the hash table all undefineds on the command line */
2152 lang_place_undefineds();
2154 /* Create a bfd for each input file */
2155 current_target
= default_target
;
2156 lang_for_each_statement(open_input_bfds
);
2158 common_section
.userdata
= (PTR
)&common_section_userdata
;
2161 /* Run through the contours of the script and attatch input sections
2162 to the correct output sections
2164 find_constructors();
2165 map_input_to_output_sections(statement_list
.head
, (char *)NULL
,
2166 ( lang_output_section_statement_type
*)NULL
);
2169 /* Find any sections not attatched explicitly and handle them */
2170 lang_place_orphans();
2172 /* Size up the common data */
2175 ldemul_before_allocation();
2178 /* Size up the sections */
2179 lang_size_sections(statement_list
.head
,
2181 &(statement_list
.head
), 0, (bfd_vma
)0, false);
2184 /* Now run around and relax if we can */
2185 if (command_line
.relax
)
2187 reset_memory_regions();
2189 /* Move the global symbols around */
2190 lang_relocate_globals();
2198 lang_size_sections(statement_list
.head
,
2199 (lang_output_section_statement_type
*)NULL
,
2200 &(statement_list
.head
), 0, (bfd_vma
)0, true);
2201 /* FIXME. Until the code in relax is fixed so that it only reads in
2202 stuff once, we cant iterate since there is no way for the linker to
2203 know what has been patched and what hasn't */
2214 /* See if anything special should be done now we know how big
2216 ldemul_after_allocation();
2218 /* Do all the assignments, now that we know the final restingplaces
2219 of all the symbols */
2221 lang_do_assignments(statement_list
.head
,
2226 /* Move the global symbols around */
2227 lang_relocate_globals();
2229 /* Make sure that we're not mixing architectures */
2238 /* EXPORTED TO YACC */
2241 DEFUN(lang_add_wild
,(section_name
, filename
),
2242 CONST
char *CONST section_name AND
2243 CONST
char *CONST filename
)
2245 lang_wild_statement_type
*new = new_stat(lang_wild_statement
,
2248 if (section_name
!= (char *)NULL
&& strcmp(section_name
,"COMMON") == 0)
2250 placed_commons
= true;
2252 if (filename
!= (char *)NULL
) {
2253 lang_has_input_file
= true;
2255 new->section_name
= section_name
;
2256 new->filename
= filename
;
2257 lang_list_init(&new->children
);
2260 DEFUN(lang_section_start
,(name
, address
),
2261 CONST
char *name AND
2262 etree_type
*address
)
2264 lang_address_statement_type
*ad
=new_stat(lang_address_statement
, stat_ptr
);
2265 ad
->section_name
= name
;
2266 ad
->address
= address
;
2270 DEFUN(lang_add_entry
,(name
),
2273 entry_symbol
= name
;
2277 DEFUN(lang_add_target
,(name
),
2280 lang_target_statement_type
*new = new_stat(lang_target_statement
,
2290 DEFUN(lang_add_map
,(name
),
2296 map_option_f
= true;
2304 DEFUN(lang_add_fill
,(exp
),
2307 lang_fill_statement_type
*new = new_stat(lang_fill_statement
,
2313 DEFUN(lang_add_data
,(type
, exp
),
2315 union etree_union
*exp
)
2318 lang_data_statement_type
*new = new_stat(lang_data_statement
,
2325 DEFUN(lang_add_assignment
,(exp
),
2328 lang_assignment_statement_type
*new = new_stat(lang_assignment_statement
,
2334 DEFUN(lang_add_attribute
,(attribute
),
2335 enum statement_enum attribute
)
2337 new_statement(attribute
, sizeof(lang_statement_union_type
),stat_ptr
);
2343 DEFUN(lang_startup
,(name
),
2346 if (startup_file
!= (char *)NULL
) {
2347 einfo("%P%FMultiple STARTUP files\n");
2349 first_file
->filename
= name
;
2350 first_file
->local_sym_name
= name
;
2355 DEFUN(lang_float
,(maybe
),
2358 lang_float_flag
= maybe
;
2362 DEFUN(lang_leave_output_section_statement
,(fill
, memspec
),
2364 CONST
char *memspec
)
2366 current_section
->fill
= fill
;
2367 current_section
->region
= lang_memory_region_lookup(memspec
);
2368 stat_ptr
= &statement_list
;
2370 /* We remember if we are closing a .data section, since we use it to
2371 store constructors in */
2372 if (strcmp(current_section
->name
, ".data") ==0) {
2373 end_of_data_section_statement_list
= statement_list
;
2378 Create an absolute symbol with the given name with the value of the
2379 address of first byte of the section named.
2381 If the symbol already exists, then do nothing.
2384 DEFUN(lang_abs_symbol_at_beginning_of
,(section
, name
),
2385 CONST
char *section AND
2388 if (ldsym_undefined(name
)) {
2389 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2390 asymbol
*def
= create_symbol(name
,
2391 BSF_GLOBAL
| BSF_EXPORT
,
2395 if (s
!= (asection
*)NULL
) {
2396 def
->value
= s
->vma
;
2405 Create an absolute symbol with the given name with the value of the
2406 address of the first byte after the end of the section named.
2408 If the symbol already exists, then do nothing.
2411 DEFUN(lang_abs_symbol_at_end_of
,(section
, name
),
2412 CONST
char *section AND
2415 if (ldsym_undefined(name
)){
2416 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2417 /* Add a symbol called _end */
2418 asymbol
*def
= create_symbol(name
,
2419 BSF_GLOBAL
| BSF_EXPORT
,
2422 if (s
!= (asection
*)NULL
) {
2423 def
->value
= s
->vma
+ s
->_raw_size
;
2432 DEFUN(lang_statement_append
,(list
, element
, field
),
2433 lang_statement_list_type
*list AND
2434 lang_statement_union_type
*element AND
2435 lang_statement_union_type
**field
)
2437 *(list
->tail
) = element
;
2441 /* Set the output format type */
2443 DEFUN(lang_add_output_format
,(format
),
2446 output_target
= format
;