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. */
37 PROTO(static void, print_statements
,(void));
38 PROTO(static void, print_statement
,(lang_statement_union_type
*,
39 lang_output_section_statement_type
*));
43 static CONST
char *startup_file
;
44 static lang_statement_list_type input_file_chain
;
46 /* Points to the last statement in the .data section, so we can add
47 stuff to the data section without pain */
48 static lang_statement_list_type end_of_data_section_statement_list
;
50 /* List of statements needed to handle consxtructors */
51 static lang_statement_list_type constructor_list
;
53 static boolean placed_commons
= false;
54 static lang_output_section_statement_type
*default_common_section
;
55 static boolean map_option_f
;
56 static bfd_vma print_dot
;
57 static lang_input_statement_type
*first_file
;
58 static lang_statement_list_type lang_output_section_statement
;
59 static CONST
char *current_target
;
60 static CONST
char *output_target
;
61 static size_t longest_section_name
= 8;
62 static asection common_section
;
63 static section_userdata_type common_section_userdata
;
64 static lang_statement_list_type statement_list
;
67 lang_statement_list_type
*stat_ptr
= &statement_list
;
68 lang_input_statement_type
*script_file
= 0;
69 boolean option_longmap
= false;
70 lang_statement_list_type file_chain
= {0};
71 CONST
char *entry_symbol
= 0;
72 bfd_size_type largest_section
= 0;
73 boolean lang_has_input_file
= false;
74 lang_output_section_statement_type
*create_object_symbols
= 0;
75 boolean had_output_filename
= false;
76 boolean lang_float_flag
= false;
78 extern char *default_target
;
80 extern unsigned int undefined_global_sym_count
;
81 extern char *current_file
;
82 extern bfd
*output_bfd
;
83 extern enum bfd_architecture ldfile_output_architecture
;
84 extern unsigned long ldfile_output_machine
;
85 extern char *ldfile_output_machine_name
;
86 extern ldsym_type
*symbol_head
;
87 extern unsigned int commons_pending
;
88 extern args_type command_line
;
89 extern ld_config_type config
;
90 extern boolean had_script
;
91 extern boolean write_map
;
97 #define cat(a,b) a/**/b
100 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
102 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
104 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
106 void EXFUN(lang_add_data
,( int type
, union etree_union
*exp
));
109 DEFUN(print_size
,(value
),
112 printf("%5x", (unsigned)value
);
115 DEFUN(print_alignment
,(value
),
118 printf("2**%1u",value
);
121 DEFUN(print_fill
,(value
),
124 printf("%04x",(unsigned)value
);
129 DEFUN(print_section
,(name
),
130 CONST
char *CONST name
)
132 printf("%*s", -longest_section_name
, name
);
135 /*----------------------------------------------------------------------
136 lang_for_each_statement walks the parse tree and calls the provided
137 function for each node
141 DEFUN(lang_for_each_statement_worker
,(func
, s
),
143 lang_statement_union_type
*s
)
145 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
149 switch (s
->header
.type
) {
150 case lang_constructors_statement_enum
:
151 lang_for_each_statement_worker(func
, constructor_list
.head
);
153 case lang_output_section_statement_enum
:
154 lang_for_each_statement_worker
156 s
->output_section_statement
.children
.head
);
158 case lang_wild_statement_enum
:
159 lang_for_each_statement_worker
161 s
->wild_statement
.children
.head
);
163 case lang_data_statement_enum
:
164 case lang_object_symbols_statement_enum
:
165 case lang_output_statement_enum
:
166 case lang_target_statement_enum
:
167 case lang_input_section_enum
:
168 case lang_input_statement_enum
:
169 case lang_fill_statement_enum
:
170 case lang_assignment_statement_enum
:
171 case lang_padding_statement_enum
:
172 case lang_address_statement_enum
:
182 DEFUN(lang_for_each_statement
,(func
),
185 lang_for_each_statement_worker(func
,
186 statement_list
.head
);
188 /*----------------------------------------------------------------------*/
190 DEFUN(lang_list_init
,(list
),
191 lang_statement_list_type
*list
)
193 list
->head
= (lang_statement_union_type
*)NULL
;
194 list
->tail
= &list
->head
;
198 /*----------------------------------------------------------------------
200 build a new statement node for the parse tree
205 lang_statement_union_type
*
206 DEFUN(new_statement
,(type
, size
, list
),
207 enum statement_enum type AND
208 bfd_size_type size AND
209 lang_statement_list_type
*list
)
211 lang_statement_union_type
*new = (lang_statement_union_type
*)
213 new->header
.type
= type
;
214 new->header
.next
= (lang_statement_union_type
*)NULL
;
215 lang_statement_append(list
, new, &new->header
.next
);
220 Build a new input file node for the language. There are several ways
221 in which we treat an input file, eg, we only look at symbols, or
222 prefix it with a -l etc.
224 We can be supplied with requests for input files more than once;
225 they may, for example be split over serveral lines like foo.o(.text)
226 foo.o(.data) etc, so when asked for a file we check that we havn't
227 got it already so we don't duplicate the bfd.
230 static lang_input_statement_type
*
231 DEFUN(new_afile
, (name
, file_type
, target
),
232 CONST
char *CONST name AND
233 CONST lang_input_file_enum_type file_type AND
234 CONST
char *CONST target
)
236 lang_input_statement_type
*p
= new_stat(lang_input_statement
,
238 lang_has_input_file
= true;
241 case lang_input_file_is_symbols_only_enum
:
243 p
->is_archive
=false;
245 p
->local_sym_name
= name
;
246 p
->just_syms_flag
= true;
247 p
->search_dirs_flag
= false;
249 case lang_input_file_is_fake_enum
:
251 p
->is_archive
=false;
253 p
->local_sym_name
= name
;
254 p
->just_syms_flag
= false;
255 p
->search_dirs_flag
=false;
257 case lang_input_file_is_l_enum
:
258 p
->is_archive
= true;
261 p
->local_sym_name
= concat("-l",name
,"");
262 p
->just_syms_flag
= false;
263 p
->search_dirs_flag
= true;
265 case lang_input_file_is_search_file_enum
:
266 case lang_input_file_is_marker_enum
:
268 p
->is_archive
=false;
270 p
->local_sym_name
= name
;
271 p
->just_syms_flag
= false;
272 p
->search_dirs_flag
=true;
274 case lang_input_file_is_file_enum
:
276 p
->is_archive
=false;
278 p
->local_sym_name
= name
;
279 p
->just_syms_flag
= false;
280 p
->search_dirs_flag
=false;
285 p
->asymbols
= (asymbol
**)NULL
;
286 p
->superfile
= (lang_input_statement_type
*)NULL
;
287 p
->next_real_file
= (lang_statement_union_type
*)NULL
;
288 p
->next
= (lang_statement_union_type
*)NULL
;
290 p
->common_output_section
= (asection
*)NULL
;
291 lang_statement_append(&input_file_chain
,
292 (lang_statement_union_type
*)p
,
299 lang_input_statement_type
*
300 DEFUN(lang_add_input_file
,(name
, file_type
, target
),
302 lang_input_file_enum_type file_type AND
305 /* Look it up or build a new one */
306 lang_has_input_file
= true;
308 lang_input_statement_type
*p
;
310 for (p
= (lang_input_statement_type
*)input_file_chain
.head
;
311 p
!= (lang_input_statement_type
*)NULL
;
312 p
= (lang_input_statement_type
*)(p
->next_real_file
))
314 /* Sometimes we have incomplete entries in here */
315 if (p
->filename
!= (char *)NULL
) {
316 if(strcmp(name
,p
->filename
) == 0) return p
;
321 return new_afile(name
, file_type
, target
);
325 /* Build enough state so that the parser can build its tree */
327 DEFUN_VOID(lang_init
)
330 stat_ptr
= &statement_list
;
331 lang_list_init(stat_ptr
);
333 lang_list_init(&input_file_chain
);
334 lang_list_init(&lang_output_section_statement
);
335 lang_list_init(&file_chain
);
336 first_file
= lang_add_input_file((char *)NULL
,
337 lang_input_file_is_marker_enum
,
342 /*----------------------------------------------------------------------
343 A region is an area of memory declared with the
344 MEMORY { name:org=exp, len=exp ... }
347 We maintain a list of all the regions here
349 If no regions are specified in the script, then the default is used
350 which is created when looked up to be the entire data space
353 static lang_memory_region_type
*lang_memory_region_list
;
354 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
356 lang_memory_region_type
*
357 DEFUN(lang_memory_region_lookup
,(name
),
358 CONST
char *CONST name
)
361 lang_memory_region_type
*p
= lang_memory_region_list
;
362 for (p
= lang_memory_region_list
;
363 p
!= ( lang_memory_region_type
*)NULL
;
365 if (strcmp(p
->name
, name
) == 0) {
369 if (strcmp(name
,"*default*")==0) {
370 /* This is the default region, dig out first one on the list */
371 if (lang_memory_region_list
!= (lang_memory_region_type
*)NULL
){
372 return lang_memory_region_list
;
376 lang_memory_region_type
*new =
377 (lang_memory_region_type
*)ldmalloc((bfd_size_type
)(sizeof(lang_memory_region_type
)));
378 new->name
= buystring(name
);
379 new->next
= (lang_memory_region_type
*)NULL
;
381 *lang_memory_region_list_tail
= new;
382 lang_memory_region_list_tail
= &new->next
;
391 lang_output_section_statement_type
*
392 DEFUN(lang_output_section_find
,(name
),
393 CONST
char * CONST name
)
395 lang_statement_union_type
*u
;
396 lang_output_section_statement_type
*lookup
;
398 for (u
= lang_output_section_statement
.head
;
399 u
!= (lang_statement_union_type
*)NULL
;
402 lookup
= &u
->output_section_statement
;
403 if (strcmp(name
, lookup
->name
)==0) {
407 return (lang_output_section_statement_type
*)NULL
;
410 lang_output_section_statement_type
*
411 DEFUN(lang_output_section_statement_lookup
,(name
),
412 CONST
char * CONST name
)
414 lang_output_section_statement_type
*lookup
;
415 lookup
=lang_output_section_find(name
);
416 if (lookup
== (lang_output_section_statement_type
*)NULL
) {
418 lookup
=(lang_output_section_statement_type
*)
419 new_stat(lang_output_section_statement
, stat_ptr
);
420 lookup
->region
= (lang_memory_region_type
*)NULL
;
422 lookup
->block_value
= 1;
425 lookup
->next
= (lang_statement_union_type
*)NULL
;
426 lookup
->bfd_section
= (asection
*)NULL
;
427 lookup
->processed
= false;
428 lookup
->addr_tree
= (etree_type
*)NULL
;
429 lang_list_init(&lookup
->children
);
431 lang_statement_append(&lang_output_section_statement
,
432 (lang_statement_union_type
*)lookup
,
443 DEFUN(print_flags
, (outfile
, ignore_flags
),
447 fprintf(outfile
,"(");
449 if (flags
->flag_read
) fprintf(outfile
,"R");
450 if (flags
->flag_write
) fprintf(outfile
,"W");
451 if (flags
->flag_executable
) fprintf(outfile
,"X");
452 if (flags
->flag_loadable
) fprintf(outfile
,"L");
454 fprintf(outfile
,")");
458 DEFUN(lang_map
,(outfile
),
461 lang_memory_region_type
*m
;
462 fprintf(outfile
,"**MEMORY CONFIGURATION**\n\n");
464 fprintf(outfile
,"name\t\torigin\t\tlength\t\tattributes\n");
466 fprintf(outfile
,"name\t\torigin length\t\tattributes\n");
468 for (m
= lang_memory_region_list
;
469 m
!= (lang_memory_region_type
*)NULL
;
472 fprintf(outfile
,"%-16s", m
->name
);
473 print_address(m
->origin
);
475 print_address(m
->length
);
477 print_flags(outfile
, &m
->flags
);
478 fprintf(outfile
,"\n");
480 fprintf(outfile
,"\n\n**LINK EDITOR MEMORY MAP**\n\n");
481 fprintf(outfile
,"output input virtual\n");
482 fprintf(outfile
,"section section address tsize\n\n");
493 lang_output_section_statement_type
*s
)
495 section_userdata_type
*new =
496 (section_userdata_type
*)
497 ldmalloc((bfd_size_type
)(sizeof(section_userdata_type
)));
499 s
->bfd_section
= bfd_get_section_by_name(output_bfd
, s
->name
);
500 if (s
->bfd_section
== (asection
*)NULL
)
501 s
->bfd_section
= bfd_make_section(output_bfd
, s
->name
);
502 if (s
->bfd_section
== (asection
*)NULL
) {
503 info("%P%F output format %s cannot represent section called %s\n",
504 output_bfd
->xvec
->name
,
507 s
->bfd_section
->output_section
= s
->bfd_section
;
508 /* s->bfd_section->flags = s->flags;*/
510 /* We initialize an output sections output offset to minus its own */
511 /* vma to allow us to output a section through itself */
512 s
->bfd_section
->output_offset
= 0;
513 get_userdata( s
->bfd_section
) = (PTR
)new;
516 /***********************************************************************
519 These expand statements like *(.text) and foo.o to a list of
520 explicit actions, like foo.o(.text), bar.o(.text) and
523 The toplevel routine, wild, takes a statement, section, file and
524 target. If either the section or file is null it is taken to be the
525 wildcard. Seperate lang_input_section statements are created for
526 each part of the expanstion, and placed after the statement provided.
531 DEFUN(wild_doit
,(ptr
, section
, output
, file
),
532 lang_statement_list_type
*ptr AND
533 asection
*section AND
534 lang_output_section_statement_type
*output AND
535 lang_input_statement_type
*file
)
537 if(output
->bfd_section
== (asection
*)NULL
)
542 if (section
!= (asection
*)NULL
543 && section
->output_section
== (asection
*)NULL
) {
544 /* Add a section reference to the list */
545 lang_input_section_type
*new = new_stat(lang_input_section
, ptr
);
547 new->section
= section
;
549 section
->output_section
= output
->bfd_section
;
550 section
->output_section
->flags
|= section
->flags
;
551 if (section
->alignment_power
> output
->bfd_section
->alignment_power
) {
552 output
->bfd_section
->alignment_power
= section
->alignment_power
;
558 DEFUN(our_bfd_get_section_by_name
,(abfd
, section
),
562 return bfd_get_section_by_name(abfd
, section
);
566 DEFUN(wild_section
,(ptr
, section
, file
, output
),
567 lang_wild_statement_type
*ptr AND
568 CONST
char *section AND
569 lang_input_statement_type
*file AND
570 lang_output_section_statement_type
*output
)
573 if (file
->just_syms_flag
== false) {
574 if (section
== (char *)NULL
) {
575 /* Do the creation to all sections in the file */
576 for (s
= file
->the_bfd
->sections
; s
!= (asection
*)NULL
; s
=s
->next
) {
577 wild_doit(&ptr
->children
, s
, output
, file
);
581 /* Do the creation to the named section only */
582 wild_doit(&ptr
->children
,
583 our_bfd_get_section_by_name(file
->the_bfd
, section
),
590 /* passed a file name (which must have been seen already and added to
591 the statement tree. We will see if it has been opened already and
592 had its symbols read. If not then we'll read it.
594 Archives are pecuilar here. We may open them once, but if they do
595 not define anything we need at the time, they won't have all their
596 symbols read. If we need them later, we'll have to redo it.
599 lang_input_statement_type
*
600 DEFUN(lookup_name
,(name
),
601 CONST
char * CONST name
)
603 lang_input_statement_type
*search
;
604 for(search
= (lang_input_statement_type
*)input_file_chain
.head
;
605 search
!= (lang_input_statement_type
*)NULL
;
606 search
= (lang_input_statement_type
*)search
->next_real_file
)
608 if (search
->filename
== (char *)NULL
&& name
== (char *)NULL
) {
611 if (search
->filename
!= (char *)NULL
&& name
!= (char *)NULL
) {
612 if (strcmp(search
->filename
, name
) == 0) {
613 ldmain_open_file_read_symbol(search
);
619 /* There isn't an afile entry for this file yet, this must be
620 because the name has only appeared inside a load script and not
621 on the command line */
622 search
= new_afile(name
, lang_input_file_is_file_enum
, default_target
);
623 ldmain_open_file_read_symbol(search
);
630 DEFUN(wild
,(s
, section
, file
, target
, output
),
631 lang_wild_statement_type
*s AND
632 CONST
char *CONST section AND
633 CONST
char *CONST file AND
634 CONST
char *CONST target AND
635 lang_output_section_statement_type
*output
)
637 lang_input_statement_type
*f
;
638 if (file
== (char *)NULL
) {
639 /* Perform the iteration over all files in the list */
640 for (f
= (lang_input_statement_type
*)file_chain
.head
;
641 f
!= (lang_input_statement_type
*)NULL
;
642 f
= (lang_input_statement_type
*)f
->next
) {
643 wild_section(s
, section
, f
, output
);
647 /* Perform the iteration over a single file */
648 wild_section( s
, section
, lookup_name(file
), output
);
650 if (section
!= (char *)NULL
651 && strcmp(section
,"COMMON") == 0
652 && default_common_section
== (lang_output_section_statement_type
*)NULL
)
654 /* Remember the section that common is going to incase we later
655 get something which doesn't know where to put it */
656 default_common_section
= output
;
661 read in all the files
664 DEFUN(open_output
,(name
),
665 CONST
char *CONST name
)
667 extern CONST
char *output_filename
;
669 if (output_target
== (char *)NULL
) {
670 if (current_target
!= (char *)NULL
)
671 output_target
= current_target
;
673 output_target
= default_target
;
675 output
= bfd_openw(name
, output_target
);
676 output_filename
= name
;
678 if (output
== (bfd
*)NULL
)
680 if (bfd_error
== invalid_target
) {
681 info("%P%F target %s not found\n", output_target
);
683 info("%P%F problem opening output file %s, %E", name
);
686 output
->flags
|= D_PAGED
;
687 bfd_set_format(output
, bfd_object
);
695 DEFUN(ldlang_open_output
,(statement
),
696 lang_statement_union_type
*statement
)
698 switch (statement
->header
.type
)
700 case lang_output_statement_enum
:
701 output_bfd
= open_output(statement
->output_statement
.name
);
702 ldemul_set_output_arch();
703 if (config
.magic_demand_paged
&& !config
.relocateable_output
)
704 output_bfd
->flags
|= ~D_PAGED
;
706 output_bfd
->flags
&= ~D_PAGED
;
707 if (config
.text_read_only
)
708 output_bfd
->flags
|= WP_TEXT
;
710 output_bfd
->flags
&= ~WP_TEXT
;
713 case lang_target_statement_enum
:
714 current_target
= statement
->target_statement
.target
;
722 DEFUN(open_input_bfds
,(statement
),
723 lang_statement_union_type
*statement
)
725 switch (statement
->header
.type
)
727 case lang_target_statement_enum
:
728 current_target
= statement
->target_statement
.target
;
730 case lang_wild_statement_enum
:
731 /* Maybe we should load the file's symbols */
732 if (statement
->wild_statement
.filename
)
734 (void) lookup_name(statement
->wild_statement
.filename
);
737 case lang_input_statement_enum
:
738 if (statement
->input_statement
.real
== true)
740 statement
->input_statement
.target
= current_target
;
741 lookup_name(statement
->input_statement
.filename
);
748 /* If there are [COMMONS] statements, put a wild one into the bss section */
751 lang_reasonable_defaults()
754 lang_output_section_statement_lookup(".text");
755 lang_output_section_statement_lookup(".data");
757 default_common_section
=
758 lang_output_section_statement_lookup(".bss");
761 if (placed_commons
== false) {
762 lang_wild_statement_type
*new =
763 new_stat(lang_wild_statement
,
764 &default_common_section
->children
);
765 new->section_name
= "COMMON";
766 new->filename
= (char *)NULL
;
767 lang_list_init(&new->children
);
774 Add the supplied name to the symbol table as an undefined reference.
775 Remove items from the chain as we open input bfds
777 typedef struct ldlang_undef_chain_list
{
778 struct ldlang_undef_chain_list
*next
;
780 } ldlang_undef_chain_list_type
;
782 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
785 DEFUN(ldlang_add_undef
,(name
),
786 CONST
char *CONST name
)
788 ldlang_undef_chain_list_type
*new =
789 (ldlang_undef_chain_list_type
790 *)ldmalloc((bfd_size_type
)(sizeof(ldlang_undef_chain_list_type
)));
792 new->next
= ldlang_undef_chain_list_head
;
793 ldlang_undef_chain_list_head
= new;
795 new->name
= buystring(name
);
797 /* Run through the list of undefineds created above and place them
798 into the linker hash table as undefined symbols belonging to the
802 DEFUN_VOID(lang_place_undefineds
)
804 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
805 while (ptr
!= (ldlang_undef_chain_list_type
*)NULL
) {
806 ldsym_type
*sy
= ldsym_get(ptr
->name
);
808 asymbol
**def_ptr
= (asymbol
**)ldmalloc((bfd_size_type
)(sizeof(asymbol
**)));
809 def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
811 def
->name
= ptr
->name
;
812 def
->flags
= BSF_UNDEFINED
;
813 def
->section
= (asection
*)NULL
;
814 Q_enter_global_ref(def_ptr
);
821 /* Copy important data from out internal form to the bfd way. Also
822 create a section for the dummy file
826 DEFUN_VOID(lang_create_output_section_statements
)
828 lang_statement_union_type
*os
;
829 for (os
= lang_output_section_statement
.head
;
830 os
!= (lang_statement_union_type
*)NULL
;
831 os
= os
->output_section_statement
.next
) {
832 lang_output_section_statement_type
*s
=
833 &os
->output_section_statement
;
840 DEFUN_VOID(lang_init_script_file
)
842 script_file
= lang_add_input_file("script file",
843 lang_input_file_is_fake_enum
,
845 script_file
->the_bfd
= bfd_create("script file", output_bfd
);
846 script_file
->symbol_count
= 0;
847 script_file
->the_bfd
->sections
= output_bfd
->sections
;
853 /* Open input files and attatch to output sections */
855 DEFUN(map_input_to_output_sections
,(s
, target
, output_section_statement
),
856 lang_statement_union_type
*s AND
857 CONST
char *target AND
858 lang_output_section_statement_type
*output_section_statement
)
860 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
862 switch (s
->header
.type
) {
865 case lang_wild_statement_enum
:
866 wild(&s
->wild_statement
, s
->wild_statement
.section_name
,
867 s
->wild_statement
.filename
, target
,
868 output_section_statement
);
871 case lang_constructors_statement_enum
:
872 map_input_to_output_sections(constructor_list
.head
,
874 output_section_statement
);
876 case lang_output_section_statement_enum
:
877 map_input_to_output_sections(s
->output_section_statement
.children
.head
,
879 &s
->output_section_statement
);
881 case lang_output_statement_enum
:
883 case lang_target_statement_enum
:
884 target
= s
->target_statement
.target
;
886 case lang_fill_statement_enum
:
887 case lang_input_section_enum
:
888 case lang_object_symbols_statement_enum
:
889 case lang_data_statement_enum
:
890 case lang_assignment_statement_enum
:
891 case lang_padding_statement_enum
:
893 case lang_afile_asection_pair_statement_enum
:
896 case lang_address_statement_enum
:
897 /* Mark the specified section with the supplied address */
899 lang_output_section_statement_type
*os
=
900 lang_output_section_statement_lookup
901 (s
->address_statement
.section_name
);
902 os
->addr_tree
= s
->address_statement
.address
;
903 if (os
->bfd_section
== (asection
*)NULL
) {
904 info("%P%F can't set the address of undefined section %s\n",
905 s
->address_statement
.section_name
);
909 case lang_input_statement_enum
:
910 /* A standard input statement, has no wildcards */
911 /* ldmain_open_file_read_symbol(&s->input_statement);*/
922 DEFUN(print_output_section_statement
,(output_section_statement
),
923 lang_output_section_statement_type
*output_section_statement
)
925 asection
*section
= output_section_statement
->bfd_section
;
927 print_section(output_section_statement
->name
);
930 print_dot
= section
->vma
;
934 print_address(section
->vma
);
936 print_size(section
->size
);
938 print_alignment(section
->alignment_power
);
941 printf("%s flags", output_section_statement
->region
->name
);
942 print_flags(stdout
, &output_section_statement
->flags
);
944 if (section
->flags
& SEC_LOAD
)
946 if (section
->flags
& SEC_ALLOC
)
948 if (section
->flags
& SEC_RELOC
)
950 if (section
->flags
& SEC_HAS_CONTENTS
)
955 printf("No attached output section");
958 print_statement(output_section_statement
->children
.head
,
959 output_section_statement
);
964 DEFUN(print_assignment
,(assignment
, output_section
),
965 lang_assignment_statement_type
*assignment AND
966 lang_output_section_statement_type
*output_section
)
968 etree_value_type result
;
973 print_address(print_dot
);
975 result
= exp_fold_tree(assignment
->exp
->assign
.src
,
977 lang_final_phase_enum
,
982 print_address(result
.value
);
986 printf("*undefined*");
989 exp_print_tree(stdout
, assignment
->exp
);
994 DEFUN(print_input_statement
,(statm
),
995 lang_input_statement_type
*statm
)
997 if (statm
->filename
!= (char *)NULL
) {
998 printf("LOAD %s\n",statm
->filename
);
1003 DEFUN(print_symbol
,(q
),
1010 print_address(outside_symbol_address(q
));
1011 printf(" %s", q
->name
? q
->name
: " ");
1016 DEFUN(print_input_section
,(in
),
1017 lang_input_section_type
*in
)
1019 asection
*i
= in
->section
;
1024 print_section(i
->name
);
1026 if (i
->output_section
) {
1027 print_address(i
->output_section
->vma
+ i
->output_offset
);
1029 print_size(i
->size
);
1031 print_alignment(i
->alignment_power
);
1035 bfd
*abfd
= in
->ifile
->the_bfd
;
1036 if (in
->ifile
->just_syms_flag
== true) {
1037 printf("symbols only ");
1040 printf(" %s ",abfd
->xvec
->name
);
1041 if(abfd
->my_archive
!= (bfd
*)NULL
) {
1042 printf("[%s]%s", abfd
->my_archive
->filename
,
1046 printf("%s", abfd
->filename
);
1048 printf("(overhead %d bytes)", (int)bfd_alloc_size(abfd
));
1051 /* Find all the symbols in this file defined in this section */
1054 for (p
= in
->ifile
->asymbols
; *p
; p
++) {
1057 if (bfd_get_section(q
) == i
&& q
->flags
& BSF_GLOBAL
) {
1068 print_dot
= outside_section_address(i
) + i
->size
;
1071 printf("No output section allocated\n");
1077 DEFUN(print_fill_statement
,(fill
),
1078 lang_fill_statement_type
*fill
)
1080 printf("FILL mask ");
1081 print_fill( fill
->fill
);
1085 DEFUN(print_data_statement
,(data
),
1086 lang_data_statement_type
*data
)
1088 /* bfd_vma value; */
1093 /* ASSERT(print_dot == data->output_vma);*/
1095 print_address(data
->output_vma
);
1097 print_address(data
->value
);
1099 switch (data
->type
) {
1102 print_dot
+= BYTE_SIZE
;
1106 print_dot
+= SHORT_SIZE
;
1110 print_dot
+= LONG_SIZE
;
1114 exp_print_tree(stdout
, data
->exp
);
1121 DEFUN(print_padding_statement
,(s
),
1122 lang_padding_statement_type
*s
)
1126 print_section("*fill*");
1128 print_address(s
->output_offset
+ s
->output_section
->vma
);
1130 print_size(s
->size
);
1132 print_fill(s
->fill
);
1137 DEFUN(print_wild_statement
,(w
,os
),
1138 lang_wild_statement_type
*w AND
1139 lang_output_section_statement_type
*os
)
1142 if (w
->filename
!= (char *)NULL
) {
1143 printf("%s",w
->filename
);
1148 if (w
->section_name
!= (char *)NULL
) {
1149 printf("(%s)",w
->section_name
);
1155 print_statement(w
->children
.head
, os
);
1159 DEFUN(print_statement
,(s
, os
),
1160 lang_statement_union_type
*s AND
1161 lang_output_section_statement_type
*os
)
1164 switch (s
->header
.type
) {
1165 case lang_constructors_statement_enum
:
1166 printf("constructors:\n");
1167 print_statement(constructor_list
.head
, os
);
1170 case lang_wild_statement_enum
:
1171 print_wild_statement(&s
->wild_statement
, os
);
1174 printf("Fail with %d\n",s
->header
.type
);
1177 case lang_address_statement_enum
:
1178 printf("address\n");
1181 case lang_object_symbols_statement_enum
:
1182 printf("object symbols\n");
1184 case lang_fill_statement_enum
:
1185 print_fill_statement(&s
->fill_statement
);
1187 case lang_data_statement_enum
:
1188 print_data_statement(&s
->data_statement
);
1190 case lang_input_section_enum
:
1191 print_input_section(&s
->input_section
);
1193 case lang_padding_statement_enum
:
1194 print_padding_statement(&s
->padding_statement
);
1196 case lang_output_section_statement_enum
:
1197 print_output_section_statement(&s
->output_section_statement
);
1199 case lang_assignment_statement_enum
:
1200 print_assignment(&s
->assignment_statement
,
1205 case lang_target_statement_enum
:
1206 printf("TARGET(%s)\n", s
->target_statement
.target
);
1208 case lang_output_statement_enum
:
1209 printf("OUTPUT(%s %s)\n",
1210 s
->output_statement
.name
,
1213 case lang_input_statement_enum
:
1214 print_input_statement(&s
->input_statement
);
1216 case lang_afile_asection_pair_statement_enum
:
1226 DEFUN_VOID(print_statements
)
1228 print_statement(statement_list
.head
,
1229 (lang_output_section_statement_type
*)NULL
);
1233 DEFUN(insert_pad
,(this_ptr
, fill
, power
, output_section_statement
, dot
),
1234 lang_statement_union_type
**this_ptr AND
1236 unsigned int power AND
1237 asection
* output_section_statement AND
1240 /* Align this section first to the
1241 input sections requirement, then
1242 to the output section's requirement.
1243 If this alignment is > than any seen before,
1244 then record it too. Perform the alignment by
1245 inserting a magic 'padding' statement.
1248 unsigned int alignment_needed
= align_power(dot
, power
) - dot
;
1250 if (alignment_needed
!= 0)
1252 lang_statement_union_type
*new =
1253 (lang_statement_union_type
*)
1254 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1255 /* Link into existing chain */
1256 new->header
.next
= *this_ptr
;
1258 new->header
.type
= lang_padding_statement_enum
;
1259 new->padding_statement
.output_section
= output_section_statement
;
1260 new->padding_statement
.output_offset
=
1261 dot
- output_section_statement
->vma
;
1262 new->padding_statement
.fill
= fill
;
1263 new->padding_statement
.size
= alignment_needed
;
1267 /* Remember the most restrictive alignment */
1268 if (power
> output_section_statement
->alignment_power
) {
1269 output_section_statement
->alignment_power
= power
;
1271 output_section_statement
->size
+= alignment_needed
;
1272 return alignment_needed
+ dot
;
1276 /* Work out how much this section will move the dot point */
1278 DEFUN(size_input_section
, (this_ptr
, output_section_statement
, fill
, dot
),
1279 lang_statement_union_type
**this_ptr AND
1280 lang_output_section_statement_type
*output_section_statement AND
1281 unsigned short fill AND
1284 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1285 asection
*i
= is
->section
;
1287 if (is
->ifile
->just_syms_flag
== false) {
1288 dot
= insert_pad(this_ptr
, fill
, i
->alignment_power
,
1289 output_section_statement
->bfd_section
, dot
);
1291 /* remember the largest size so we can malloc the largest area
1292 needed for the output stage. Only remember the size of sections
1293 which we will actually allocate */
1295 (SEC_HAS_CONTENTS
| SEC_LOAD
)) == (SEC_HAS_CONTENTS
| SEC_LOAD
))
1296 && (i
->size
> largest_section
)) {
1297 largest_section
= i
->size
;
1300 /* Remember where in the output section this input section goes */
1302 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1304 /* Mark how big the output section must be to contain this now */
1306 output_section_statement
->bfd_section
->size
=
1307 dot
- output_section_statement
->bfd_section
->vma
;
1311 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1318 /* Work out the size of the output sections
1319 from the sizes of the input sections */
1321 DEFUN(lang_size_sections
,(s
, output_section_statement
, prev
, fill
, dot
),
1322 lang_statement_union_type
*s AND
1323 lang_output_section_statement_type
* output_section_statement AND
1324 lang_statement_union_type
**prev AND
1325 unsigned short fill AND
1328 /* Size up the sections from their constituent parts */
1329 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1331 switch (s
->header
.type
) {
1334 case lang_output_section_statement_enum
:
1337 lang_output_section_statement_type
*os
=
1338 &(s
->output_section_statement
);
1339 /* The start of a section */
1341 if (os
->addr_tree
== (etree_type
*)NULL
) {
1342 /* No address specified for this section, get one
1343 from the region specification
1345 if (os
->region
== (lang_memory_region_type
*)NULL
) {
1346 os
->region
= lang_memory_region_lookup("*default*");
1348 dot
= os
->region
->current
;
1351 etree_value_type r
;
1352 r
= exp_fold_tree(os
->addr_tree
,
1353 (lang_output_section_statement_type
*)NULL
,
1354 lang_allocating_phase_enum
,
1356 if (r
.valid
== false) {
1357 info("%F%S: non constant address expression for section %s\n",
1362 /* The section starts here */
1363 /* First, align to what the section needs */
1365 dot
= align_power(dot
, os
->bfd_section
->alignment_power
);
1366 os
->bfd_section
->vma
= dot
;
1367 os
->bfd_section
->output_offset
= 0;
1369 (void) lang_size_sections(os
->children
.head
, os
, &os
->children
.head
,
1371 /* Ignore the size of the input sections, use the vma and size to */
1375 after
= ALIGN(os
->bfd_section
->vma
+
1376 os
->bfd_section
->size
,
1380 os
->bfd_section
->size
= after
- os
->bfd_section
->vma
;
1381 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->size
;
1382 os
->processed
= true;
1384 /* Replace into region ? */
1385 if (os
->addr_tree
== (etree_type
*)NULL
1386 && os
->region
!=(lang_memory_region_type
*)NULL
) {
1387 os
->region
->current
= dot
;
1392 case lang_constructors_statement_enum
:
1393 dot
= lang_size_sections(constructor_list
.head
,
1394 output_section_statement
,
1395 &s
->wild_statement
.children
.head
,
1400 case lang_data_statement_enum
:
1403 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1404 s
->data_statement
.output_section
=
1405 output_section_statement
->bfd_section
;
1407 switch (s
->data_statement
.type
) {
1420 output_section_statement
->bfd_section
->size
+= size
;
1424 case lang_wild_statement_enum
:
1426 dot
= lang_size_sections(s
->wild_statement
.children
.head
,
1427 output_section_statement
,
1428 &s
->wild_statement
.children
.head
,
1434 case lang_object_symbols_statement_enum
:
1435 create_object_symbols
= output_section_statement
;
1437 case lang_output_statement_enum
:
1438 case lang_target_statement_enum
:
1440 case lang_input_section_enum
:
1441 dot
= size_input_section(prev
,
1442 output_section_statement
,
1443 output_section_statement
->fill
, dot
);
1445 case lang_input_statement_enum
:
1447 case lang_fill_statement_enum
:
1448 fill
= s
->fill_statement
.fill
;
1450 case lang_assignment_statement_enum
:
1452 bfd_vma newdot
= dot
;
1453 exp_fold_tree(s
->assignment_statement
.exp
,
1454 output_section_statement
,
1455 lang_allocating_phase_enum
,
1460 /* We've been moved ! so insert a pad */
1462 lang_statement_union_type
*new =
1463 (lang_statement_union_type
*)
1464 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1465 /* Link into existing chain */
1466 new->header
.next
= *prev
;
1468 new->header
.type
= lang_padding_statement_enum
;
1469 new->padding_statement
.output_section
=
1470 output_section_statement
->bfd_section
;
1471 new->padding_statement
.output_offset
=
1472 dot
- output_section_statement
->bfd_section
->vma
;
1473 new->padding_statement
.fill
= fill
;
1474 new->padding_statement
.size
= newdot
- dot
;
1475 output_section_statement
->bfd_section
->size
+=
1476 new->padding_statement
.size
;
1482 case lang_padding_statement_enum
:
1488 case lang_address_statement_enum
:
1491 prev
= &s
->header
.next
;
1498 DEFUN(lang_do_assignments
,(s
, output_section_statement
, fill
, dot
),
1499 lang_statement_union_type
*s AND
1500 lang_output_section_statement_type
* output_section_statement AND
1501 unsigned short fill AND
1505 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1507 switch (s
->header
.type
) {
1508 case lang_constructors_statement_enum
:
1509 dot
= lang_do_assignments(constructor_list
.head
,
1510 output_section_statement
,
1515 case lang_output_section_statement_enum
:
1517 lang_output_section_statement_type
*os
=
1518 &(s
->output_section_statement
);
1519 dot
= os
->bfd_section
->vma
;
1520 (void) lang_do_assignments(os
->children
.head
, os
, os
->fill
, dot
);
1521 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->size
;
1524 case lang_wild_statement_enum
:
1526 dot
= lang_do_assignments(s
->wild_statement
.children
.head
,
1527 output_section_statement
,
1532 case lang_object_symbols_statement_enum
:
1533 case lang_output_statement_enum
:
1534 case lang_target_statement_enum
:
1536 case lang_common_statement_enum
:
1539 case lang_data_statement_enum
:
1541 etree_value_type value
;
1542 value
= exp_fold_tree(s
->data_statement
.exp
,
1543 0, lang_final_phase_enum
, dot
, &dot
);
1544 s
->data_statement
.value
= value
.value
;
1545 if (value
.valid
== false) info("%F%P: Invalid data statement\n");
1547 switch (s
->data_statement
.type
) {
1559 case lang_input_section_enum
:
1561 asection
*in
= s
->input_section
.section
;
1566 case lang_input_statement_enum
:
1568 case lang_fill_statement_enum
:
1569 fill
= s
->fill_statement
.fill
;
1571 case lang_assignment_statement_enum
:
1573 exp_fold_tree(s
->assignment_statement
.exp
,
1574 output_section_statement
,
1575 lang_final_phase_enum
,
1581 case lang_padding_statement_enum
:
1582 dot
+= s
->padding_statement
.size
;
1587 case lang_address_statement_enum
:
1598 DEFUN_VOID(lang_relocate_globals
)
1602 Each ldsym_type maintains a chain of pointers to asymbols which
1603 references the definition. Replace each pointer to the referenence
1604 with a pointer to only one place, preferably the definition. If
1605 the defintion isn't available then the common symbol, and if
1606 there isn't one of them then choose one reference.
1609 FOR_EACH_LDSYM(lgs
) {
1611 if (lgs
->sdefs_chain
) {
1612 it
= *(lgs
->sdefs_chain
);
1614 else if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1615 it
= *(lgs
->scoms_chain
);
1617 else if (lgs
->srefs_chain
!= (asymbol
**)NULL
) {
1618 it
= *(lgs
->srefs_chain
);
1621 /* This can happen when the command line asked for a symbol to
1623 it
= (asymbol
*)NULL
;
1625 if (it
!= (asymbol
*)NULL
)
1627 asymbol
**ptr
= lgs
->srefs_chain
;;
1628 if (lgs
->flags
& SYM_WARNING
)
1630 produce_warnings(lgs
, it
);
1633 while (ptr
!= (asymbol
**)NULL
) {
1634 asymbol
*ref
= *ptr
;
1636 ptr
= (asymbol
**)(ref
->udata
);
1645 DEFUN_VOID(lang_finish
)
1649 if (entry_symbol
== (char *)NULL
) {
1650 /* No entry has been specified, look for start */
1651 entry_symbol
= "start";
1653 lgs
= ldsym_get_soft(entry_symbol
);
1654 if (lgs
&& lgs
->sdefs_chain
) {
1655 asymbol
*sy
= *(lgs
->sdefs_chain
);
1656 /* We can set the entry address*/
1657 bfd_set_start_address(output_bfd
,
1658 outside_symbol_address(sy
));
1662 /* Can't find anything reasonable,
1663 use the first address in the text section
1665 asection
*ts
= bfd_get_section_by_name(output_bfd
, ".text");
1667 bfd_set_start_address(output_bfd
, ts
->vma
);
1672 /* By now we know the target architecture, and we may have an */
1673 /* ldfile_output_machine_name */
1675 DEFUN_VOID(lang_check
)
1677 lang_statement_union_type
*file
;
1680 unsigned long input_machine
;
1681 enum bfd_architecture input_architecture
;
1683 CONST bfd_arch_info_type
*compatible
;
1685 for (file
= file_chain
.head
;
1686 file
!= (lang_statement_union_type
*)NULL
;
1687 file
=file
->input_statement
.next
)
1689 unsigned long ldfile_new_output_machine
= 0;
1690 enum bfd_architecture ldfile_new_output_architecture
= bfd_arch_unknown
;
1692 input_bfd
= file
->input_statement
.the_bfd
;
1694 input_machine
= bfd_get_mach(input_bfd
);
1695 input_architecture
= bfd_get_arch(input_bfd
);
1698 /* Inspect the architecture and ensure we're linking like with
1701 compatible
=bfd_arch_get_compatible(input_bfd
,
1706 ldfile_output_machine
= compatible
->mach
;
1707 ldfile_output_architecture
= compatible
->arch
;
1712 info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1713 bfd_printable_name(input_bfd
), input_bfd
,
1714 bfd_printable_name(output_bfd
));
1716 bfd_set_arch_mach(output_bfd
,
1717 ldfile_new_output_architecture
,
1718 ldfile_new_output_machine
);
1726 * run through all the global common symbols and tie them
1727 * to the output section requested.
1729 As an experiment we do this 4 times, once for all the byte sizes,
1730 then all the two bytes, all the four bytes and then everything else
1734 DEFUN_VOID(lang_common
)
1738 if (config
.relocateable_output
== false ||
1739 command_line
.force_common_definition
== true) {
1740 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<=1) {
1741 for (lgs
= symbol_head
;
1742 lgs
!= (ldsym_type
*)NULL
;
1746 unsigned int power_of_two
;
1749 if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1750 com
= *(lgs
->scoms_chain
);
1779 if (config
.sort_common
== false || align
== power
) {
1780 /* Change from a common symbol into a definition of
1782 lgs
->sdefs_chain
= lgs
->scoms_chain
;
1783 lgs
->scoms_chain
= (asymbol
**)NULL
;
1785 /* Point to the correct common section */
1787 ((lang_input_statement_type
*)
1788 (com
->the_bfd
->usrdata
))->common_section
;
1789 /* Fix the size of the common section */
1790 com
->section
->size
= ALIGN(com
->section
->size
, align
);
1792 /* Remember if this is the biggest alignment ever seen */
1793 if (power_of_two
> com
->section
->alignment_power
) {
1794 com
->section
->alignment_power
= power_of_two
;
1797 /* Symbol stops being common and starts being global, but
1798 we remember that it was common once. */
1800 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
1801 com
->value
= com
->section
->size
;
1805 printf ("Allocating common %s: %x at %x %s\n",
1808 (unsigned) com
->value
,
1809 com
->the_bfd
->filename
);
1812 com
->section
->size
+= size
;
1825 run through the input files and ensure that every input
1826 section has somewhere to go. If one is found without
1827 a destination then create an input request and place it
1828 into the statement tree.
1832 DEFUN_VOID(lang_place_orphans
)
1834 lang_input_statement_type
*file
;
1835 for (file
= (lang_input_statement_type
*)file_chain
.head
;
1836 file
!= (lang_input_statement_type
*)NULL
;
1837 file
= (lang_input_statement_type
*)file
->next
) {
1839 for (s
= file
->the_bfd
->sections
;
1840 s
!= (asection
*)NULL
;
1842 if ( s
->output_section
== (asection
*)NULL
) {
1843 /* This section of the file is not attatched, root
1844 around for a sensible place for it to go */
1846 if (file
->common_section
== s
) {
1847 /* This is a lonely common section which must
1848 have come from an archive. We attatch to the
1849 section with the wildcard */
1850 if (config
.relocateable_output
!= true
1851 && command_line
.force_common_definition
== false) {
1852 if (default_common_section
==
1853 (lang_output_section_statement_type
*)NULL
) {
1854 info("%P: No [COMMON] command, defaulting to .bss\n");
1856 default_common_section
=
1857 lang_output_section_statement_lookup(".bss");
1860 wild_doit(&default_common_section
->children
, s
,
1861 default_common_section
, file
);
1865 lang_output_section_statement_type
*os
=
1866 lang_output_section_statement_lookup(s
->name
);
1868 wild_doit(&os
->children
, s
, os
, file
);
1877 DEFUN(lang_set_flags
,(ptr
, flags
),
1881 boolean state
= true;
1885 if (*flags
== '!') {
1892 /* ptr->flag_read = state; */
1895 /* ptr->flag_write = state; */
1898 /* ptr->flag_executable= state;*/
1902 /* ptr->flag_loadable= state;*/
1905 info("%P%F illegal syntax in flags\n");
1915 DEFUN(lang_for_each_file
,(func
),
1916 PROTO(void, (*func
),(lang_input_statement_type
*)))
1918 lang_input_statement_type
*f
;
1919 for (f
= (lang_input_statement_type
*)file_chain
.head
;
1920 f
!= (lang_input_statement_type
*)NULL
;
1921 f
= (lang_input_statement_type
*)f
->next
)
1929 DEFUN(lang_for_each_input_section
, (func
),
1930 PROTO(void ,(*func
),(bfd
*ab
, asection
*as
)))
1932 lang_input_statement_type
*f
;
1933 for (f
= (lang_input_statement_type
*)file_chain
.head
;
1934 f
!= (lang_input_statement_type
*)NULL
;
1935 f
= (lang_input_statement_type
*)f
->next
)
1938 for (s
= f
->the_bfd
->sections
;
1939 s
!= (asection
*)NULL
;
1941 func(f
->the_bfd
, s
);
1949 DEFUN(ldlang_add_file
,(entry
),
1950 lang_input_statement_type
*entry
)
1953 lang_statement_append(&file_chain
,
1954 (lang_statement_union_type
*)entry
,
1961 DEFUN(lang_add_output
,(name
),
1964 lang_output_statement_type
*new = new_stat(lang_output_statement
,
1967 had_output_filename
= true;
1971 static lang_output_section_statement_type
*current_section
;
1974 DEFUN(lang_enter_output_section_statement
,
1975 (output_section_statement_name
,
1979 char *output_section_statement_name AND
1980 etree_type
*address_exp AND
1982 bfd_vma block_value
)
1984 lang_output_section_statement_type
*os
;
1987 lang_output_section_statement_lookup(output_section_statement_name
);
1991 /* Add this statement to tree */
1992 /* add_statement(lang_output_section_statement_enum,
1993 output_section_statement);*/
1994 /* Make next things chain into subchain of this */
1996 if (os
->addr_tree
==
1997 (etree_type
*)NULL
) {
2002 os
->block_value
= block_value
;
2003 stat_ptr
= & os
->children
;
2009 DEFUN_VOID(lang_final
)
2011 if (had_output_filename
== false) {
2012 extern CONST
char *output_filename
;
2013 lang_add_output(output_filename
);
2022 DEFUN(create_symbol
,(name
, flags
, section
),
2023 CONST
char *name AND
2027 extern lang_input_statement_type
*script_file
;
2028 asymbol
**def_ptr
= (asymbol
**)ldmalloc((bfd_size_type
)(sizeof(asymbol
**)));
2029 /* Add this definition to script file */
2030 asymbol
*def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
2031 def
->name
= buystring(name
);
2034 def
->section
= section
;
2037 Q_enter_global_ref(def_ptr
);
2041 /* run through the symbol table, find all the symbols which are
2042 constructors and for each one, create statements to do something
2049 LONG(__CTOR_LIST_END - . / 4 - 2)
2053 Put these statements onto a special list.
2057 typedef struct constructor_list
2060 struct constructor_list
*next
;
2061 } constructor_list_type
;
2063 static constructor_list_type
*constructor_name_list
;
2066 DEFUN(ldlang_add_constructor
,(name
),
2070 constructor_list_type
*next
= constructor_name_list
;
2072 if (name
->flags
& SYM_CONSTRUCTOR
) return;
2074 next
= (constructor_list_type
*) ldmalloc(sizeof(constructor_list_type
));
2075 next
->next
= constructor_name_list
;
2077 name
->flags
|= SYM_CONSTRUCTOR
;
2078 constructor_name_list
= next
;
2083 DEFUN_VOID(find_constructors
)
2085 lang_statement_list_type
*old
= stat_ptr
;
2086 constructor_list_type
*p
= constructor_name_list
;
2087 stat_ptr
= & constructor_list
;
2088 lang_list_init(stat_ptr
);
2089 while (p
!= (constructor_list_type
*)NULL
)
2091 /* Have we already done this one ? */
2092 CONST
char *name
= p
->sym
->name
;
2093 int len
= strlen(name
);
2094 char *end
= ldmalloc(len
+3);
2099 ( exp_assop('=',name
, exp_nameop(NAME
,".")));
2102 (LONG
, exp_binop('-',
2105 exp_nameop(NAME
, end
),
2106 exp_nameop(NAME
,".")),
2112 lang_add_wild(name
, (char *)NULL
);
2113 lang_add_data(LONG
, exp_intop(0));
2115 (exp_assop('=', end
, exp_nameop(NAME
,".")));
2125 DEFUN_VOID(lang_process
)
2127 if (had_script
== false) {
2128 parse_line(ldemul_get_script());
2130 lang_reasonable_defaults();
2131 current_target
= default_target
;
2133 lang_for_each_statement(ldlang_open_output
); /* Open the output file */
2134 /* For each output section statement, create a section in the output
2136 lang_create_output_section_statements();
2138 /* Create a dummy bfd for the script */
2139 lang_init_script_file();
2141 /* Add to the hash table all undefineds on the command line */
2142 lang_place_undefineds();
2144 /* Create a bfd for each input file */
2145 current_target
= default_target
;
2146 lang_for_each_statement(open_input_bfds
);
2148 common_section
.userdata
= (PTR
)&common_section_userdata
;
2151 /* Run through the contours of the script and attatch input sections
2152 to the correct output sections
2154 find_constructors();
2155 map_input_to_output_sections(statement_list
.head
, (char *)NULL
,
2156 ( lang_output_section_statement_type
*)NULL
);
2159 /* Find any sections not attatched explicitly and handle them */
2160 lang_place_orphans();
2162 /* Size up the common data */
2165 ldemul_before_allocation();
2167 /* Size up the sections */
2168 lang_size_sections(statement_list
.head
,
2169 (lang_output_section_statement_type
*)NULL
,
2170 &(statement_list
.head
), 0, (bfd_vma
)0);
2172 /* See if anything special should be done now we know how big
2174 ldemul_after_allocation();
2176 /* Do all the assignments, now that we know the final restingplaces
2177 of all the symbols */
2179 lang_do_assignments(statement_list
.head
,
2180 (lang_output_section_statement_type
*)NULL
,
2183 /* Make sure that we're not mixing architectures */
2187 /* Move the global symbols around */
2188 lang_relocate_globals();
2195 /* EXPORTED TO YACC */
2198 DEFUN(lang_add_wild
,(section_name
, filename
),
2199 CONST
char *CONST section_name AND
2200 CONST
char *CONST filename
)
2202 lang_wild_statement_type
*new = new_stat(lang_wild_statement
,
2205 if (section_name
!= (char *)NULL
&& strcmp(section_name
,"COMMON") == 0)
2207 placed_commons
= true;
2209 if (filename
!= (char *)NULL
) {
2210 lang_has_input_file
= true;
2212 new->section_name
= section_name
;
2213 new->filename
= filename
;
2214 lang_list_init(&new->children
);
2217 DEFUN(lang_section_start
,(name
, address
),
2218 CONST
char *name AND
2219 etree_type
*address
)
2221 lang_address_statement_type
*ad
=new_stat(lang_address_statement
, stat_ptr
);
2222 ad
->section_name
= name
;
2223 ad
->address
= address
;
2227 DEFUN(lang_add_entry
,(name
),
2230 entry_symbol
= name
;
2234 DEFUN(lang_add_target
,(name
),
2237 lang_target_statement_type
*new = new_stat(lang_target_statement
,
2247 DEFUN(lang_add_map
,(name
),
2253 map_option_f
= true;
2261 DEFUN(lang_add_fill
,(exp
),
2264 lang_fill_statement_type
*new = new_stat(lang_fill_statement
,
2270 DEFUN(lang_add_data
,(type
, exp
),
2272 union etree_union
*exp
)
2275 lang_data_statement_type
*new = new_stat(lang_data_statement
,
2282 DEFUN(lang_add_assignment
,(exp
),
2285 lang_assignment_statement_type
*new = new_stat(lang_assignment_statement
,
2291 DEFUN(lang_add_attribute
,(attribute
),
2292 enum statement_enum attribute
)
2294 new_statement(attribute
, sizeof(lang_statement_union_type
),stat_ptr
);
2300 DEFUN(lang_startup
,(name
),
2303 if (startup_file
!= (char *)NULL
) {
2304 info("%P%FMultiple STARTUP files\n");
2306 first_file
->filename
= name
;
2307 first_file
->local_sym_name
= name
;
2312 DEFUN(lang_float
,(maybe
),
2315 lang_float_flag
= maybe
;
2319 DEFUN(lang_leave_output_section_statement
,(fill
, memspec
),
2321 CONST
char *memspec
)
2323 current_section
->fill
= fill
;
2324 current_section
->region
= lang_memory_region_lookup(memspec
);
2325 stat_ptr
= &statement_list
;
2327 /* We remember if we are closing a .data section, since we use it to
2328 store constructors in */
2329 if (strcmp(current_section
->name
, ".data") ==0) {
2330 end_of_data_section_statement_list
= statement_list
;
2335 Create an absolute symbol with the given name with the value of the
2336 address of first byte of the section named.
2338 If the symbol already exists, then do nothing.
2341 DEFUN(lang_abs_symbol_at_beginning_of
,(section
, name
),
2342 CONST
char *section AND
2345 if (ldsym_undefined(name
)) {
2346 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2347 asymbol
*def
= create_symbol(name
,
2348 BSF_GLOBAL
| BSF_EXPORT
|
2351 if (s
!= (asection
*)NULL
) {
2352 def
->value
= s
->vma
;
2361 Create an absolute symbol with the given name with the value of the
2362 address of the first byte after the end of the section named.
2364 If the symbol already exists, then do nothing.
2367 DEFUN(lang_abs_symbol_at_end_of
,(section
, name
),
2368 CONST
char *section AND
2371 if (ldsym_undefined(name
)){
2372 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2373 /* Add a symbol called _end */
2374 asymbol
*def
= create_symbol(name
,
2375 BSF_GLOBAL
| BSF_EXPORT
|
2378 if (s
!= (asection
*)NULL
) {
2379 def
->value
= s
->vma
+ s
->size
;
2388 DEFUN(lang_statement_append
,(list
, element
, field
),
2389 lang_statement_list_type
*list AND
2390 lang_statement_union_type
*element AND
2391 lang_statement_union_type
**field
)
2393 *(list
->tail
) = element
;
2397 /* Set the output format type */
2399 DEFUN(lang_add_output_format
,(format
),
2402 output_target
= format
;