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_make_section(output_bfd
, s
->name
);
500 if (s
->bfd_section
== (asection
*)NULL
) {
501 info("%P%F output format %s cannot represent section called %s\n",
502 output_bfd
->xvec
->name
,
505 s
->bfd_section
->output_section
= s
->bfd_section
;
506 /* s->bfd_section->flags = s->flags;*/
508 /* We initialize an output sections output offset to minus its own */
509 /* vma to allow us to output a section through itself */
510 s
->bfd_section
->output_offset
= 0;
511 get_userdata( s
->bfd_section
) = (PTR
)new;
514 /***********************************************************************
517 These expand statements like *(.text) and foo.o to a list of
518 explicit actions, like foo.o(.text), bar.o(.text) and
521 The toplevel routine, wild, takes a statement, section, file and
522 target. If either the section or file is null it is taken to be the
523 wildcard. Seperate lang_input_section statements are created for
524 each part of the expanstion, and placed after the statement provided.
529 DEFUN(wild_doit
,(ptr
, section
, output
, file
),
530 lang_statement_list_type
*ptr AND
531 asection
*section AND
532 lang_output_section_statement_type
*output AND
533 lang_input_statement_type
*file
)
535 if(output
->bfd_section
== (asection
*)NULL
)
540 if (section
!= (asection
*)NULL
541 && section
->output_section
== (asection
*)NULL
) {
542 /* Add a section reference to the list */
543 lang_input_section_type
*new = new_stat(lang_input_section
, ptr
);
545 new->section
= section
;
547 section
->output_section
= output
->bfd_section
;
548 section
->output_section
->flags
|= section
->flags
;
549 if (section
->alignment_power
> output
->bfd_section
->alignment_power
) {
550 output
->bfd_section
->alignment_power
= section
->alignment_power
;
556 DEFUN(our_bfd_get_section_by_name
,(abfd
, section
),
560 return bfd_get_section_by_name(abfd
, section
);
564 DEFUN(wild_section
,(ptr
, section
, file
, output
),
565 lang_wild_statement_type
*ptr AND
566 CONST
char *section AND
567 lang_input_statement_type
*file AND
568 lang_output_section_statement_type
*output
)
571 if (file
->just_syms_flag
== false) {
572 if (section
== (char *)NULL
) {
573 /* Do the creation to all sections in the file */
574 for (s
= file
->the_bfd
->sections
; s
!= (asection
*)NULL
; s
=s
->next
) {
575 wild_doit(&ptr
->children
, s
, output
, file
);
579 /* Do the creation to the named section only */
580 wild_doit(&ptr
->children
,
581 our_bfd_get_section_by_name(file
->the_bfd
, section
),
588 /* passed a file name (which must have been seen already and added to
589 the statement tree. We will see if it has been opened already and
590 had its symbols read. If not then we'll read it.
592 Archives are pecuilar here. We may open them once, but if they do
593 not define anything we need at the time, they won't have all their
594 symbols read. If we need them later, we'll have to redo it.
597 lang_input_statement_type
*
598 DEFUN(lookup_name
,(name
),
599 CONST
char * CONST name
)
601 lang_input_statement_type
*search
;
602 for(search
= (lang_input_statement_type
*)input_file_chain
.head
;
603 search
!= (lang_input_statement_type
*)NULL
;
604 search
= (lang_input_statement_type
*)search
->next_real_file
)
606 if (search
->filename
== (char *)NULL
&& name
== (char *)NULL
) {
609 if (search
->filename
!= (char *)NULL
&& name
!= (char *)NULL
) {
610 if (strcmp(search
->filename
, name
) == 0) {
611 ldmain_open_file_read_symbol(search
);
617 /* There isn't an afile entry for this file yet, this must be
618 because the name has only appeared inside a load script and not
619 on the command line */
620 search
= new_afile(name
, lang_input_file_is_file_enum
, default_target
);
621 ldmain_open_file_read_symbol(search
);
628 DEFUN(wild
,(s
, section
, file
, target
, output
),
629 lang_wild_statement_type
*s AND
630 CONST
char *CONST section AND
631 CONST
char *CONST file AND
632 CONST
char *CONST target AND
633 lang_output_section_statement_type
*output
)
635 lang_input_statement_type
*f
;
636 if (file
== (char *)NULL
) {
637 /* Perform the iteration over all files in the list */
638 for (f
= (lang_input_statement_type
*)file_chain
.head
;
639 f
!= (lang_input_statement_type
*)NULL
;
640 f
= (lang_input_statement_type
*)f
->next
) {
641 wild_section(s
, section
, f
, output
);
645 /* Perform the iteration over a single file */
646 wild_section( s
, section
, lookup_name(file
), output
);
648 if (section
!= (char *)NULL
649 && strcmp(section
,"COMMON") == 0
650 && default_common_section
== (lang_output_section_statement_type
*)NULL
)
652 /* Remember the section that common is going to incase we later
653 get something which doesn't know where to put it */
654 default_common_section
= output
;
659 read in all the files
662 DEFUN(open_output
,(name
),
663 CONST
char *CONST name
)
665 extern CONST
char *output_filename
;
667 if (output_target
== (char *)NULL
) {
668 if (current_target
!= (char *)NULL
)
669 output_target
= current_target
;
671 output_target
= default_target
;
673 output
= bfd_openw(name
, output_target
);
674 output_filename
= name
;
676 if (output
== (bfd
*)NULL
)
678 if (bfd_error
== invalid_target
) {
679 info("%P%F target %s not found\n", output_target
);
681 info("%P%F problem opening output file %s, %E", name
);
684 output
->flags
|= D_PAGED
;
685 bfd_set_format(output
, bfd_object
);
693 DEFUN(ldlang_open_output
,(statement
),
694 lang_statement_union_type
*statement
)
696 switch (statement
->header
.type
)
698 case lang_output_statement_enum
:
699 output_bfd
= open_output(statement
->output_statement
.name
);
700 ldemul_set_output_arch();
703 case lang_target_statement_enum
:
704 current_target
= statement
->target_statement
.target
;
712 DEFUN(open_input_bfds
,(statement
),
713 lang_statement_union_type
*statement
)
715 switch (statement
->header
.type
)
717 case lang_target_statement_enum
:
718 current_target
= statement
->target_statement
.target
;
720 case lang_wild_statement_enum
:
721 /* Maybe we should load the file's symbols */
722 if (statement
->wild_statement
.filename
)
724 (void) lookup_name(statement
->wild_statement
.filename
);
727 case lang_input_statement_enum
:
728 if (statement
->input_statement
.real
== true)
730 statement
->input_statement
.target
= current_target
;
731 lookup_name(statement
->input_statement
.filename
);
738 /* If there are [COMMONS] statements, put a wild one into the bss section */
741 lang_reasonable_defaults()
744 lang_output_section_statement_lookup(".text");
745 lang_output_section_statement_lookup(".data");
747 default_common_section
=
748 lang_output_section_statement_lookup(".bss");
751 if (placed_commons
== false) {
752 lang_wild_statement_type
*new =
753 new_stat(lang_wild_statement
,
754 &default_common_section
->children
);
755 new->section_name
= "COMMON";
756 new->filename
= (char *)NULL
;
757 lang_list_init(&new->children
);
764 Add the supplied name to the symbol table as an undefined reference.
765 Remove items from the chain as we open input bfds
767 typedef struct ldlang_undef_chain_list
{
768 struct ldlang_undef_chain_list
*next
;
770 } ldlang_undef_chain_list_type
;
772 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
775 DEFUN(ldlang_add_undef
,(name
),
776 CONST
char *CONST name
)
778 ldlang_undef_chain_list_type
*new =
779 (ldlang_undef_chain_list_type
780 *)ldmalloc((bfd_size_type
)(sizeof(ldlang_undef_chain_list_type
)));
782 new->next
= ldlang_undef_chain_list_head
;
783 ldlang_undef_chain_list_head
= new;
785 new->name
= buystring(name
);
787 /* Run through the list of undefineds created above and place them
788 into the linker hash table as undefined symbols belonging to the
792 DEFUN_VOID(lang_place_undefineds
)
794 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
795 while (ptr
!= (ldlang_undef_chain_list_type
*)NULL
) {
796 ldsym_type
*sy
= ldsym_get(ptr
->name
);
798 asymbol
**def_ptr
= (asymbol
**)ldmalloc((bfd_size_type
)(sizeof(asymbol
**)));
799 def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
801 def
->name
= ptr
->name
;
802 def
->flags
= BSF_UNDEFINED
;
803 def
->section
= (asection
*)NULL
;
804 Q_enter_global_ref(def_ptr
);
811 /* Copy important data from out internal form to the bfd way. Also
812 create a section for the dummy file
816 DEFUN_VOID(lang_create_output_section_statements
)
818 lang_statement_union_type
*os
;
819 for (os
= lang_output_section_statement
.head
;
820 os
!= (lang_statement_union_type
*)NULL
;
821 os
= os
->output_section_statement
.next
) {
822 lang_output_section_statement_type
*s
=
823 &os
->output_section_statement
;
830 DEFUN_VOID(lang_init_script_file
)
832 script_file
= lang_add_input_file("script file",
833 lang_input_file_is_fake_enum
,
835 script_file
->the_bfd
= bfd_create("script file", output_bfd
);
836 script_file
->symbol_count
= 0;
837 script_file
->the_bfd
->sections
= output_bfd
->sections
;
843 /* Open input files and attatch to output sections */
845 DEFUN(map_input_to_output_sections
,(s
, target
, output_section_statement
),
846 lang_statement_union_type
*s AND
847 CONST
char *target AND
848 lang_output_section_statement_type
*output_section_statement
)
850 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
852 switch (s
->header
.type
) {
855 case lang_wild_statement_enum
:
856 wild(&s
->wild_statement
, s
->wild_statement
.section_name
,
857 s
->wild_statement
.filename
, target
,
858 output_section_statement
);
861 case lang_constructors_statement_enum
:
862 map_input_to_output_sections(constructor_list
.head
,
864 output_section_statement
);
866 case lang_output_section_statement_enum
:
867 map_input_to_output_sections(s
->output_section_statement
.children
.head
,
869 &s
->output_section_statement
);
871 case lang_output_statement_enum
:
873 case lang_target_statement_enum
:
874 target
= s
->target_statement
.target
;
876 case lang_fill_statement_enum
:
877 case lang_input_section_enum
:
878 case lang_object_symbols_statement_enum
:
879 case lang_data_statement_enum
:
880 case lang_assignment_statement_enum
:
881 case lang_padding_statement_enum
:
883 case lang_afile_asection_pair_statement_enum
:
886 case lang_address_statement_enum
:
887 /* Mark the specified section with the supplied address */
889 lang_output_section_statement_type
*os
=
890 lang_output_section_statement_lookup
891 (s
->address_statement
.section_name
);
892 os
->addr_tree
= s
->address_statement
.address
;
893 if (os
->bfd_section
== (asection
*)NULL
) {
894 info("%P%F can't set the address of undefined section %s\n",
895 s
->address_statement
.section_name
);
899 case lang_input_statement_enum
:
900 /* A standard input statement, has no wildcards */
901 /* ldmain_open_file_read_symbol(&s->input_statement);*/
912 DEFUN(print_output_section_statement
,(output_section_statement
),
913 lang_output_section_statement_type
*output_section_statement
)
915 asection
*section
= output_section_statement
->bfd_section
;
917 print_section(output_section_statement
->name
);
920 print_dot
= section
->vma
;
924 print_address(section
->vma
);
926 print_size(section
->size
);
928 print_alignment(section
->alignment_power
);
931 printf("%s flags", output_section_statement
->region
->name
);
932 print_flags(stdout
, &output_section_statement
->flags
);
934 if (section
->flags
& SEC_LOAD
)
936 if (section
->flags
& SEC_ALLOC
)
938 if (section
->flags
& SEC_RELOC
)
940 if (section
->flags
& SEC_HAS_CONTENTS
)
945 printf("No attached output section");
948 print_statement(output_section_statement
->children
.head
,
949 output_section_statement
);
954 DEFUN(print_assignment
,(assignment
, output_section
),
955 lang_assignment_statement_type
*assignment AND
956 lang_output_section_statement_type
*output_section
)
958 etree_value_type result
;
963 print_address(print_dot
);
965 result
= exp_fold_tree(assignment
->exp
->assign
.src
,
967 lang_final_phase_enum
,
972 print_address(result
.value
);
976 printf("*undefined*");
979 exp_print_tree(stdout
, assignment
->exp
);
984 DEFUN(print_input_statement
,(statm
),
985 lang_input_statement_type
*statm
)
987 if (statm
->filename
!= (char *)NULL
) {
988 printf("LOAD %s\n",statm
->filename
);
993 DEFUN(print_symbol
,(q
),
1000 print_address(outside_symbol_address(q
));
1001 printf(" %s", q
->name
? q
->name
: " ");
1006 DEFUN(print_input_section
,(in
),
1007 lang_input_section_type
*in
)
1009 asection
*i
= in
->section
;
1014 print_section(i
->name
);
1016 if (i
->output_section
) {
1017 print_address(i
->output_section
->vma
+ i
->output_offset
);
1019 print_size(i
->size
);
1021 print_alignment(i
->alignment_power
);
1025 bfd
*abfd
= in
->ifile
->the_bfd
;
1026 if (in
->ifile
->just_syms_flag
== true) {
1027 printf("symbols only ");
1030 printf(" %s ",abfd
->xvec
->name
);
1031 if(abfd
->my_archive
!= (bfd
*)NULL
) {
1032 printf("[%s]%s", abfd
->my_archive
->filename
,
1036 printf("%s", abfd
->filename
);
1038 printf("(overhead %d bytes)", (int)bfd_alloc_size(abfd
));
1041 /* Find all the symbols in this file defined in this section */
1044 for (p
= in
->ifile
->asymbols
; *p
; p
++) {
1047 if (bfd_get_section(q
) == i
&& q
->flags
& BSF_GLOBAL
) {
1058 print_dot
= outside_section_address(i
) + i
->size
;
1061 printf("No output section allocated\n");
1067 DEFUN(print_fill_statement
,(fill
),
1068 lang_fill_statement_type
*fill
)
1070 printf("FILL mask ");
1071 print_fill( fill
->fill
);
1075 DEFUN(print_data_statement
,(data
),
1076 lang_data_statement_type
*data
)
1078 /* bfd_vma value; */
1083 /* ASSERT(print_dot == data->output_vma);*/
1085 print_address(data
->output_vma
);
1087 print_address(data
->value
);
1089 switch (data
->type
) {
1092 print_dot
+= BYTE_SIZE
;
1096 print_dot
+= SHORT_SIZE
;
1100 print_dot
+= LONG_SIZE
;
1104 exp_print_tree(stdout
, data
->exp
);
1111 DEFUN(print_padding_statement
,(s
),
1112 lang_padding_statement_type
*s
)
1116 print_section("*fill*");
1118 print_address(s
->output_offset
+ s
->output_section
->vma
);
1120 print_size(s
->size
);
1122 print_fill(s
->fill
);
1127 DEFUN(print_wild_statement
,(w
,os
),
1128 lang_wild_statement_type
*w AND
1129 lang_output_section_statement_type
*os
)
1132 if (w
->filename
!= (char *)NULL
) {
1133 printf("%s",w
->filename
);
1138 if (w
->section_name
!= (char *)NULL
) {
1139 printf("(%s)",w
->section_name
);
1145 print_statement(w
->children
.head
, os
);
1149 DEFUN(print_statement
,(s
, os
),
1150 lang_statement_union_type
*s AND
1151 lang_output_section_statement_type
*os
)
1154 switch (s
->header
.type
) {
1155 case lang_constructors_statement_enum
:
1156 printf("constructors:\n");
1157 print_statement(constructor_list
.head
, os
);
1160 case lang_wild_statement_enum
:
1161 print_wild_statement(&s
->wild_statement
, os
);
1164 printf("Fail with %d\n",s
->header
.type
);
1167 case lang_address_statement_enum
:
1168 printf("address\n");
1171 case lang_object_symbols_statement_enum
:
1172 printf("object symbols\n");
1174 case lang_fill_statement_enum
:
1175 print_fill_statement(&s
->fill_statement
);
1177 case lang_data_statement_enum
:
1178 print_data_statement(&s
->data_statement
);
1180 case lang_input_section_enum
:
1181 print_input_section(&s
->input_section
);
1183 case lang_padding_statement_enum
:
1184 print_padding_statement(&s
->padding_statement
);
1186 case lang_output_section_statement_enum
:
1187 print_output_section_statement(&s
->output_section_statement
);
1189 case lang_assignment_statement_enum
:
1190 print_assignment(&s
->assignment_statement
,
1195 case lang_target_statement_enum
:
1196 printf("TARGET(%s)\n", s
->target_statement
.target
);
1198 case lang_output_statement_enum
:
1199 printf("OUTPUT(%s %s)\n",
1200 s
->output_statement
.name
,
1203 case lang_input_statement_enum
:
1204 print_input_statement(&s
->input_statement
);
1206 case lang_afile_asection_pair_statement_enum
:
1216 DEFUN_VOID(print_statements
)
1218 print_statement(statement_list
.head
,
1219 (lang_output_section_statement_type
*)NULL
);
1223 DEFUN(insert_pad
,(this_ptr
, fill
, power
, output_section_statement
, dot
),
1224 lang_statement_union_type
**this_ptr AND
1226 unsigned int power AND
1227 asection
* output_section_statement AND
1230 /* Align this section first to the
1231 input sections requirement, then
1232 to the output section's requirement.
1233 If this alignment is > than any seen before,
1234 then record it too. Perform the alignment by
1235 inserting a magic 'padding' statement.
1238 unsigned int alignment_needed
= align_power(dot
, power
) - dot
;
1240 if (alignment_needed
!= 0)
1242 lang_statement_union_type
*new =
1243 (lang_statement_union_type
*)
1244 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1245 /* Link into existing chain */
1246 new->header
.next
= *this_ptr
;
1248 new->header
.type
= lang_padding_statement_enum
;
1249 new->padding_statement
.output_section
= output_section_statement
;
1250 new->padding_statement
.output_offset
=
1251 dot
- output_section_statement
->vma
;
1252 new->padding_statement
.fill
= fill
;
1253 new->padding_statement
.size
= alignment_needed
;
1257 /* Remember the most restrictive alignment */
1258 if (power
> output_section_statement
->alignment_power
) {
1259 output_section_statement
->alignment_power
= power
;
1261 output_section_statement
->size
+= alignment_needed
;
1262 return alignment_needed
+ dot
;
1266 /* Work out how much this section will move the dot point */
1268 DEFUN(size_input_section
, (this_ptr
, output_section_statement
, fill
, dot
),
1269 lang_statement_union_type
**this_ptr AND
1270 lang_output_section_statement_type
*output_section_statement AND
1271 unsigned short fill AND
1274 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1275 asection
*i
= is
->section
;
1277 if (is
->ifile
->just_syms_flag
== false) {
1278 dot
= insert_pad(this_ptr
, fill
, i
->alignment_power
,
1279 output_section_statement
->bfd_section
, dot
);
1281 /* remember the largest size so we can malloc the largest area
1282 needed for the output stage. Only remember the size of sections
1283 which we will actually allocate */
1285 (SEC_HAS_CONTENTS
| SEC_LOAD
)) == (SEC_HAS_CONTENTS
| SEC_LOAD
))
1286 && (i
->size
> largest_section
)) {
1287 largest_section
= i
->size
;
1290 /* Remember where in the output section this input section goes */
1292 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1294 /* Mark how big the output section must be to contain this now */
1296 output_section_statement
->bfd_section
->size
=
1297 dot
- output_section_statement
->bfd_section
->vma
;
1301 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1308 /* Work out the size of the output sections
1309 from the sizes of the input sections */
1311 DEFUN(lang_size_sections
,(s
, output_section_statement
, prev
, fill
, dot
),
1312 lang_statement_union_type
*s AND
1313 lang_output_section_statement_type
* output_section_statement AND
1314 lang_statement_union_type
**prev AND
1315 unsigned short fill AND
1318 /* Size up the sections from their constituent parts */
1319 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1321 switch (s
->header
.type
) {
1324 case lang_output_section_statement_enum
:
1327 lang_output_section_statement_type
*os
=
1328 &(s
->output_section_statement
);
1329 /* The start of a section */
1331 if (os
->addr_tree
== (etree_type
*)NULL
) {
1332 /* No address specified for this section, get one
1333 from the region specification
1335 if (os
->region
== (lang_memory_region_type
*)NULL
) {
1336 os
->region
= lang_memory_region_lookup("*default*");
1338 dot
= os
->region
->current
;
1341 etree_value_type r
;
1342 r
= exp_fold_tree(os
->addr_tree
,
1343 (lang_output_section_statement_type
*)NULL
,
1344 lang_allocating_phase_enum
,
1346 if (r
.valid
== false) {
1347 info("%F%S: non constant address expression for section %s\n",
1352 /* The section starts here */
1353 /* First, align to what the section needs */
1355 dot
= align_power(dot
, os
->bfd_section
->alignment_power
);
1356 os
->bfd_section
->vma
= dot
;
1357 os
->bfd_section
->output_offset
= 0;
1359 (void) lang_size_sections(os
->children
.head
, os
, &os
->children
.head
,
1361 /* Ignore the size of the input sections, use the vma and size to */
1365 after
= ALIGN(os
->bfd_section
->vma
+
1366 os
->bfd_section
->size
,
1370 os
->bfd_section
->size
= after
- os
->bfd_section
->vma
;
1371 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->size
;
1372 os
->processed
= true;
1374 /* Replace into region ? */
1375 if (os
->addr_tree
== (etree_type
*)NULL
1376 && os
->region
!=(lang_memory_region_type
*)NULL
) {
1377 os
->region
->current
= dot
;
1382 case lang_constructors_statement_enum
:
1383 dot
= lang_size_sections(constructor_list
.head
,
1384 output_section_statement
,
1385 &s
->wild_statement
.children
.head
,
1390 case lang_data_statement_enum
:
1393 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1394 s
->data_statement
.output_section
=
1395 output_section_statement
->bfd_section
;
1397 switch (s
->data_statement
.type
) {
1410 output_section_statement
->bfd_section
->size
+= size
;
1414 case lang_wild_statement_enum
:
1416 dot
= lang_size_sections(s
->wild_statement
.children
.head
,
1417 output_section_statement
,
1418 &s
->wild_statement
.children
.head
,
1424 case lang_object_symbols_statement_enum
:
1425 create_object_symbols
= output_section_statement
;
1427 case lang_output_statement_enum
:
1428 case lang_target_statement_enum
:
1430 case lang_input_section_enum
:
1431 dot
= size_input_section(prev
,
1432 output_section_statement
,
1433 output_section_statement
->fill
, dot
);
1435 case lang_input_statement_enum
:
1437 case lang_fill_statement_enum
:
1438 fill
= s
->fill_statement
.fill
;
1440 case lang_assignment_statement_enum
:
1442 bfd_vma newdot
= dot
;
1443 exp_fold_tree(s
->assignment_statement
.exp
,
1444 output_section_statement
,
1445 lang_allocating_phase_enum
,
1450 /* We've been moved ! so insert a pad */
1452 lang_statement_union_type
*new =
1453 (lang_statement_union_type
*)
1454 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1455 /* Link into existing chain */
1456 new->header
.next
= *prev
;
1458 new->header
.type
= lang_padding_statement_enum
;
1459 new->padding_statement
.output_section
=
1460 output_section_statement
->bfd_section
;
1461 new->padding_statement
.output_offset
=
1462 dot
- output_section_statement
->bfd_section
->vma
;
1463 new->padding_statement
.fill
= fill
;
1464 new->padding_statement
.size
= newdot
- dot
;
1465 output_section_statement
->bfd_section
->size
+=
1466 new->padding_statement
.size
;
1472 case lang_padding_statement_enum
:
1478 case lang_address_statement_enum
:
1481 prev
= &s
->header
.next
;
1488 DEFUN(lang_do_assignments
,(s
, output_section_statement
, fill
, dot
),
1489 lang_statement_union_type
*s AND
1490 lang_output_section_statement_type
* output_section_statement AND
1491 unsigned short fill AND
1495 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1497 switch (s
->header
.type
) {
1498 case lang_constructors_statement_enum
:
1499 dot
= lang_do_assignments(constructor_list
.head
,
1500 output_section_statement
,
1505 case lang_output_section_statement_enum
:
1507 lang_output_section_statement_type
*os
=
1508 &(s
->output_section_statement
);
1509 dot
= os
->bfd_section
->vma
;
1510 (void) lang_do_assignments(os
->children
.head
, os
, os
->fill
, dot
);
1511 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->size
;
1514 case lang_wild_statement_enum
:
1516 dot
= lang_do_assignments(s
->wild_statement
.children
.head
,
1517 output_section_statement
,
1522 case lang_object_symbols_statement_enum
:
1523 case lang_output_statement_enum
:
1524 case lang_target_statement_enum
:
1526 case lang_common_statement_enum
:
1529 case lang_data_statement_enum
:
1531 etree_value_type value
;
1532 value
= exp_fold_tree(s
->data_statement
.exp
,
1533 0, lang_final_phase_enum
, dot
, &dot
);
1534 s
->data_statement
.value
= value
.value
;
1535 if (value
.valid
== false) info("%F%P: Invalid data statement\n");
1537 switch (s
->data_statement
.type
) {
1549 case lang_input_section_enum
:
1551 asection
*in
= s
->input_section
.section
;
1556 case lang_input_statement_enum
:
1558 case lang_fill_statement_enum
:
1559 fill
= s
->fill_statement
.fill
;
1561 case lang_assignment_statement_enum
:
1563 exp_fold_tree(s
->assignment_statement
.exp
,
1564 output_section_statement
,
1565 lang_final_phase_enum
,
1571 case lang_padding_statement_enum
:
1572 dot
+= s
->padding_statement
.size
;
1577 case lang_address_statement_enum
:
1588 DEFUN_VOID(lang_relocate_globals
)
1592 Each ldsym_type maintains a chain of pointers to asymbols which
1593 references the definition. Replace each pointer to the referenence
1594 with a pointer to only one place, preferably the definition. If
1595 the defintion isn't available then the common symbol, and if
1596 there isn't one of them then choose one reference.
1599 FOR_EACH_LDSYM(lgs
) {
1601 if (lgs
->sdefs_chain
) {
1602 it
= *(lgs
->sdefs_chain
);
1604 else if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1605 it
= *(lgs
->scoms_chain
);
1607 else if (lgs
->srefs_chain
!= (asymbol
**)NULL
) {
1608 it
= *(lgs
->srefs_chain
);
1611 /* This can happen when the command line asked for a symbol to
1613 it
= (asymbol
*)NULL
;
1615 if (it
!= (asymbol
*)NULL
)
1617 asymbol
**ptr
= lgs
->srefs_chain
;;
1618 if (lgs
->flags
& SYM_WARNING
)
1620 produce_warnings(lgs
, it
);
1623 while (ptr
!= (asymbol
**)NULL
) {
1624 asymbol
*ref
= *ptr
;
1626 ptr
= (asymbol
**)(ref
->udata
);
1635 DEFUN_VOID(lang_finish
)
1639 if (entry_symbol
== (char *)NULL
) {
1640 /* No entry has been specified, look for start */
1641 entry_symbol
= "start";
1643 lgs
= ldsym_get_soft(entry_symbol
);
1644 if (lgs
&& lgs
->sdefs_chain
) {
1645 asymbol
*sy
= *(lgs
->sdefs_chain
);
1646 /* We can set the entry address*/
1647 bfd_set_start_address(output_bfd
,
1648 outside_symbol_address(sy
));
1652 /* Can't find anything reasonable,
1653 use the first address in the text section
1655 asection
*ts
= bfd_get_section_by_name(output_bfd
, ".text");
1657 bfd_set_start_address(output_bfd
, ts
->vma
);
1662 /* By now we know the target architecture, and we may have an */
1663 /* ldfile_output_machine_name */
1665 DEFUN_VOID(lang_check
)
1667 lang_statement_union_type
*file
;
1670 unsigned long input_machine
;
1671 enum bfd_architecture input_architecture
;
1673 CONST bfd_arch_info_type
*compatible
;
1675 for (file
= file_chain
.head
;
1676 file
!= (lang_statement_union_type
*)NULL
;
1677 file
=file
->input_statement
.next
)
1679 unsigned long ldfile_new_output_machine
= 0;
1680 enum bfd_architecture ldfile_new_output_architecture
= bfd_arch_unknown
;
1682 input_bfd
= file
->input_statement
.the_bfd
;
1684 input_machine
= bfd_get_mach(input_bfd
);
1685 input_architecture
= bfd_get_arch(input_bfd
);
1688 /* Inspect the architecture and ensure we're linking like with
1691 compatible
=bfd_arch_get_compatible(input_bfd
,
1696 ldfile_output_machine
= compatible
->mach
;
1697 ldfile_output_architecture
= compatible
->arch
;
1702 info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1703 bfd_printable_name(input_bfd
), input_bfd
,
1704 bfd_printable_name(output_bfd
));
1706 bfd_set_arch_mach(output_bfd
,
1707 ldfile_new_output_architecture
,
1708 ldfile_new_output_machine
);
1716 * run through all the global common symbols and tie them
1717 * to the output section requested.
1719 As an experiment we do this 4 times, once for all the byte sizes,
1720 then all the two bytes, all the four bytes and then everything else
1724 DEFUN_VOID(lang_common
)
1728 if (config
.relocateable_output
== false ||
1729 command_line
.force_common_definition
== true) {
1730 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<=1) {
1731 for (lgs
= symbol_head
;
1732 lgs
!= (ldsym_type
*)NULL
;
1736 unsigned int power_of_two
;
1739 if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1740 com
= *(lgs
->scoms_chain
);
1769 if (config
.sort_common
== false || align
== power
) {
1770 /* Change from a common symbol into a definition of
1772 lgs
->sdefs_chain
= lgs
->scoms_chain
;
1773 lgs
->scoms_chain
= (asymbol
**)NULL
;
1775 /* Point to the correct common section */
1777 ((lang_input_statement_type
*)
1778 (com
->the_bfd
->usrdata
))->common_section
;
1779 /* Fix the size of the common section */
1780 com
->section
->size
= ALIGN(com
->section
->size
, align
);
1782 /* Remember if this is the biggest alignment ever seen */
1783 if (power_of_two
> com
->section
->alignment_power
) {
1784 com
->section
->alignment_power
= power_of_two
;
1787 /* Symbol stops being common and starts being global, but
1788 we remember that it was common once. */
1790 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
1791 com
->value
= com
->section
->size
;
1795 printf ("Allocating common %s: %x at %x %s\n",
1798 (unsigned) com
->value
,
1799 com
->the_bfd
->filename
);
1802 com
->section
->size
+= size
;
1815 run through the input files and ensure that every input
1816 section has somewhere to go. If one is found without
1817 a destination then create an input request and place it
1818 into the statement tree.
1822 DEFUN_VOID(lang_place_orphans
)
1824 lang_input_statement_type
*file
;
1825 for (file
= (lang_input_statement_type
*)file_chain
.head
;
1826 file
!= (lang_input_statement_type
*)NULL
;
1827 file
= (lang_input_statement_type
*)file
->next
) {
1829 for (s
= file
->the_bfd
->sections
;
1830 s
!= (asection
*)NULL
;
1832 if ( s
->output_section
== (asection
*)NULL
) {
1833 /* This section of the file is not attatched, root
1834 around for a sensible place for it to go */
1836 if (file
->common_section
== s
) {
1837 /* This is a lonely common section which must
1838 have come from an archive. We attatch to the
1839 section with the wildcard */
1840 if (config
.relocateable_output
!= true
1841 && command_line
.force_common_definition
== false) {
1842 if (default_common_section
==
1843 (lang_output_section_statement_type
*)NULL
) {
1844 info("%P: No [COMMON] command, defaulting to .bss\n");
1846 default_common_section
=
1847 lang_output_section_statement_lookup(".bss");
1850 wild_doit(&default_common_section
->children
, s
,
1851 default_common_section
, file
);
1855 lang_output_section_statement_type
*os
=
1856 lang_output_section_statement_lookup(s
->name
);
1858 wild_doit(&os
->children
, s
, os
, file
);
1867 DEFUN(lang_set_flags
,(ptr
, flags
),
1871 boolean state
= true;
1875 if (*flags
== '!') {
1882 /* ptr->flag_read = state; */
1885 /* ptr->flag_write = state; */
1888 /* ptr->flag_executable= state;*/
1892 /* ptr->flag_loadable= state;*/
1895 info("%P%F illegal syntax in flags\n");
1905 DEFUN(lang_for_each_file
,(func
),
1906 PROTO(void, (*func
),(lang_input_statement_type
*)))
1908 lang_input_statement_type
*f
;
1909 for (f
= (lang_input_statement_type
*)file_chain
.head
;
1910 f
!= (lang_input_statement_type
*)NULL
;
1911 f
= (lang_input_statement_type
*)f
->next
)
1919 DEFUN(lang_for_each_input_section
, (func
),
1920 PROTO(void ,(*func
),(bfd
*ab
, asection
*as
)))
1922 lang_input_statement_type
*f
;
1923 for (f
= (lang_input_statement_type
*)file_chain
.head
;
1924 f
!= (lang_input_statement_type
*)NULL
;
1925 f
= (lang_input_statement_type
*)f
->next
)
1928 for (s
= f
->the_bfd
->sections
;
1929 s
!= (asection
*)NULL
;
1931 func(f
->the_bfd
, s
);
1939 DEFUN(ldlang_add_file
,(entry
),
1940 lang_input_statement_type
*entry
)
1943 lang_statement_append(&file_chain
,
1944 (lang_statement_union_type
*)entry
,
1951 DEFUN(lang_add_output
,(name
),
1954 lang_output_statement_type
*new = new_stat(lang_output_statement
,
1957 had_output_filename
= true;
1961 static lang_output_section_statement_type
*current_section
;
1964 DEFUN(lang_enter_output_section_statement
,
1965 (output_section_statement_name
,
1969 char *output_section_statement_name AND
1970 etree_type
*address_exp AND
1972 bfd_vma block_value
)
1974 lang_output_section_statement_type
*os
;
1977 lang_output_section_statement_lookup(output_section_statement_name
);
1981 /* Add this statement to tree */
1982 /* add_statement(lang_output_section_statement_enum,
1983 output_section_statement);*/
1984 /* Make next things chain into subchain of this */
1986 if (os
->addr_tree
==
1987 (etree_type
*)NULL
) {
1992 os
->block_value
= block_value
;
1993 stat_ptr
= & os
->children
;
1999 DEFUN_VOID(lang_final
)
2001 if (had_output_filename
== false) {
2002 extern CONST
char *output_filename
;
2003 lang_add_output(output_filename
);
2012 DEFUN(create_symbol
,(name
, flags
, section
),
2013 CONST
char *name AND
2017 extern lang_input_statement_type
*script_file
;
2018 asymbol
**def_ptr
= (asymbol
**)ldmalloc((bfd_size_type
)(sizeof(asymbol
**)));
2019 /* Add this definition to script file */
2020 asymbol
*def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
2021 def
->name
= buystring(name
);
2024 def
->section
= section
;
2027 Q_enter_global_ref(def_ptr
);
2031 /* run through the symbol table, find all the symbols which are
2032 constructors and for each one, create statements to do something
2039 LONG(__CTOR_LIST_END - . / 4 - 2)
2043 Put these statements onto a special list.
2047 typedef struct constructor_list
2050 struct constructor_list
*next
;
2051 } constructor_list_type
;
2053 static constructor_list_type
*constructor_name_list
;
2056 DEFUN(ldlang_add_constructor
,(name
),
2060 constructor_list_type
*next
= constructor_name_list
;
2062 if (name
->flags
& SYM_CONSTRUCTOR
) return;
2064 next
= (constructor_list_type
*) ldmalloc(sizeof(constructor_list_type
));
2065 next
->next
= constructor_name_list
;
2067 name
->flags
|= SYM_CONSTRUCTOR
;
2068 constructor_name_list
= next
;
2073 DEFUN_VOID(find_constructors
)
2075 lang_statement_list_type
*old
= stat_ptr
;
2076 constructor_list_type
*p
= constructor_name_list
;
2077 stat_ptr
= & constructor_list
;
2078 lang_list_init(stat_ptr
);
2079 while (p
!= (constructor_list_type
*)NULL
)
2081 /* Have we already done this one ? */
2082 CONST
char *name
= p
->sym
->name
;
2083 int len
= strlen(name
);
2084 char *end
= ldmalloc(len
+3);
2089 ( exp_assop('=',name
, exp_nameop(NAME
,".")));
2092 (LONG
, exp_binop('-',
2095 exp_nameop(NAME
, end
),
2096 exp_nameop(NAME
,".")),
2102 lang_add_wild(name
, (char *)NULL
);
2103 lang_add_data(LONG
, exp_intop(0));
2105 (exp_assop('=', end
, exp_nameop(NAME
,".")));
2115 DEFUN_VOID(lang_process
)
2117 if (had_script
== false) {
2118 parse_line(ldemul_get_script());
2120 lang_reasonable_defaults();
2121 current_target
= default_target
;
2123 lang_for_each_statement(ldlang_open_output
); /* Open the output file */
2124 /* For each output section statement, create a section in the output
2126 lang_create_output_section_statements();
2128 /* Create a dummy bfd for the script */
2129 lang_init_script_file();
2131 /* Add to the hash table all undefineds on the command line */
2132 lang_place_undefineds();
2134 /* Create a bfd for each input file */
2135 current_target
= default_target
;
2136 lang_for_each_statement(open_input_bfds
);
2138 common_section
.userdata
= (PTR
)&common_section_userdata
;
2141 /* Run through the contours of the script and attatch input sections
2142 to the correct output sections
2144 find_constructors();
2145 map_input_to_output_sections(statement_list
.head
, (char *)NULL
,
2146 ( lang_output_section_statement_type
*)NULL
);
2149 /* Find any sections not attatched explicitly and handle them */
2150 lang_place_orphans();
2152 /* Size up the common data */
2155 ldemul_before_allocation();
2157 /* Size up the sections */
2158 lang_size_sections(statement_list
.head
,
2159 (lang_output_section_statement_type
*)NULL
,
2160 &(statement_list
.head
), 0, (bfd_vma
)0);
2162 /* See if anything special should be done now we know how big
2164 ldemul_after_allocation();
2166 /* Do all the assignments, now that we know the final restingplaces
2167 of all the symbols */
2169 lang_do_assignments(statement_list
.head
,
2170 (lang_output_section_statement_type
*)NULL
,
2173 /* Make sure that we're not mixing architectures */
2177 /* Move the global symbols around */
2178 lang_relocate_globals();
2185 /* EXPORTED TO YACC */
2188 DEFUN(lang_add_wild
,(section_name
, filename
),
2189 CONST
char *CONST section_name AND
2190 CONST
char *CONST filename
)
2192 lang_wild_statement_type
*new = new_stat(lang_wild_statement
,
2195 if (section_name
!= (char *)NULL
&& strcmp(section_name
,"COMMON") == 0)
2197 placed_commons
= true;
2199 if (filename
!= (char *)NULL
) {
2200 lang_has_input_file
= true;
2202 new->section_name
= section_name
;
2203 new->filename
= filename
;
2204 lang_list_init(&new->children
);
2207 DEFUN(lang_section_start
,(name
, address
),
2208 CONST
char *name AND
2209 etree_type
*address
)
2211 lang_address_statement_type
*ad
=new_stat(lang_address_statement
, stat_ptr
);
2212 ad
->section_name
= name
;
2213 ad
->address
= address
;
2217 DEFUN(lang_add_entry
,(name
),
2220 entry_symbol
= name
;
2224 DEFUN(lang_add_target
,(name
),
2227 lang_target_statement_type
*new = new_stat(lang_target_statement
,
2237 DEFUN(lang_add_map
,(name
),
2243 map_option_f
= true;
2251 DEFUN(lang_add_fill
,(exp
),
2254 lang_fill_statement_type
*new = new_stat(lang_fill_statement
,
2260 DEFUN(lang_add_data
,(type
, exp
),
2262 union etree_union
*exp
)
2265 lang_data_statement_type
*new = new_stat(lang_data_statement
,
2272 DEFUN(lang_add_assignment
,(exp
),
2275 lang_assignment_statement_type
*new = new_stat(lang_assignment_statement
,
2281 DEFUN(lang_add_attribute
,(attribute
),
2282 enum statement_enum attribute
)
2284 new_statement(attribute
, sizeof(lang_statement_union_type
),stat_ptr
);
2290 DEFUN(lang_startup
,(name
),
2293 if (startup_file
!= (char *)NULL
) {
2294 info("%P%FMultiple STARTUP files\n");
2296 first_file
->filename
= name
;
2297 first_file
->local_sym_name
= name
;
2302 DEFUN(lang_float
,(maybe
),
2305 lang_float_flag
= maybe
;
2309 DEFUN(lang_leave_output_section_statement
,(fill
, memspec
),
2311 CONST
char *memspec
)
2313 current_section
->fill
= fill
;
2314 current_section
->region
= lang_memory_region_lookup(memspec
);
2315 stat_ptr
= &statement_list
;
2317 /* We remember if we are closing a .data section, since we use it to
2318 store constructors in */
2319 if (strcmp(current_section
->name
, ".data") ==0) {
2320 end_of_data_section_statement_list
= statement_list
;
2325 Create an absolute symbol with the given name with the value of the
2326 address of first byte of the section named.
2328 If the symbol already exists, then do nothing.
2331 DEFUN(lang_abs_symbol_at_beginning_of
,(section
, name
),
2332 CONST
char *section AND
2335 if (ldsym_undefined(name
)) {
2336 extern bfd
*output_bfd
;
2337 extern asymbol
*create_symbol();
2338 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2339 asymbol
*def
= create_symbol(name
,
2340 BSF_GLOBAL
| BSF_EXPORT
|
2343 if (s
!= (asection
*)NULL
) {
2344 def
->value
= s
->vma
;
2353 Create an absolute symbol with the given name with the value of the
2354 address of the first byte after the end of the section named.
2356 If the symbol already exists, then do nothing.
2359 DEFUN(lang_abs_symbol_at_end_of
,(section
, name
),
2360 CONST
char *section AND
2363 if (ldsym_undefined(name
)){
2364 extern bfd
*output_bfd
;
2365 extern asymbol
*create_symbol();
2366 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2367 /* Add a symbol called _end */
2368 asymbol
*def
= create_symbol(name
,
2369 BSF_GLOBAL
| BSF_EXPORT
|
2372 if (s
!= (asection
*)NULL
) {
2373 def
->value
= s
->vma
+ s
->size
;
2382 DEFUN(lang_statement_append
,(list
, element
, field
),
2383 lang_statement_list_type
*list AND
2384 lang_statement_union_type
*element AND
2385 lang_statement_union_type
**field
)
2387 *(list
->tail
) = element
;
2391 /* Set the output format type */
2393 DEFUN(lang_add_output_format
,(format
),
2396 output_target
= format
;