1 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
5 GLD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
10 GLD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GLD; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
39 PROTO(static void, print_statements
,(void));
40 PROTO(static void, print_statement
,(lang_statement_union_type
*,
41 lang_output_section_statement_type
*));
45 static CONST
char *startup_file
;
46 static lang_statement_list_type input_file_chain
;
48 /* Points to the last statement in the .data section, so we can add
49 stuff to the data section without pain */
50 static lang_statement_list_type end_of_data_section_statement_list
;
52 /* List of statements needed to handle consxtructors */
53 static lang_statement_list_type constructor_list
;
55 static boolean placed_commons
= false;
56 static lang_output_section_statement_type
*default_common_section
;
57 static boolean map_option_f
;
58 static bfd_vma print_dot
;
59 static lang_input_statement_type
*first_file
;
60 static lang_statement_list_type lang_output_section_statement
;
61 static CONST
char *current_target
;
62 static CONST
char *output_target
;
63 static size_t longest_section_name
= 8;
64 static asection common_section
;
65 static section_userdata_type common_section_userdata
;
66 static lang_statement_list_type statement_list
;
69 lang_statement_list_type
*stat_ptr
= &statement_list
;
70 lang_input_statement_type
*script_file
= 0;
71 boolean option_longmap
= false;
72 lang_statement_list_type file_chain
= {0};
73 CONST
char *entry_symbol
= 0;
74 bfd_size_type largest_section
= 0;
75 boolean lang_has_input_file
= false;
76 lang_output_section_statement_type
*create_object_symbols
= 0;
77 boolean had_output_filename
= false;
78 boolean lang_float_flag
= false;
80 extern char *default_target
;
82 extern unsigned int undefined_global_sym_count
;
83 extern char *current_file
;
84 extern bfd
*output_bfd
;
85 extern enum bfd_architecture ldfile_output_architecture
;
86 extern unsigned long ldfile_output_machine
;
87 extern char *ldfile_output_machine_name
;
88 extern ldsym_type
*symbol_head
;
89 extern unsigned int commons_pending
;
90 extern args_type command_line
;
91 extern ld_config_type config
;
92 extern boolean had_script
;
93 extern boolean write_map
;
99 #define cat(a,b) a/**/b
102 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
104 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
106 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
108 void EXFUN(lang_add_data
,( int type
, union etree_union
*exp
));
111 DEFUN(print_size
,(value
),
114 printf("%5x", (unsigned)value
);
117 DEFUN(print_alignment
,(value
),
120 printf("2**%1u",value
);
123 DEFUN(print_fill
,(value
),
126 printf("%04x",(unsigned)value
);
131 DEFUN(print_section
,(name
),
132 CONST
char *CONST name
)
134 printf("%*s", -longest_section_name
, name
);
137 /*----------------------------------------------------------------------
138 lang_for_each_statement walks the parse tree and calls the provided
139 function for each node
143 DEFUN(lang_for_each_statement_worker
,(func
, s
),
145 lang_statement_union_type
*s
)
147 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
151 switch (s
->header
.type
) {
152 case lang_constructors_statement_enum
:
153 lang_for_each_statement_worker(func
, constructor_list
.head
);
155 case lang_output_section_statement_enum
:
156 lang_for_each_statement_worker
158 s
->output_section_statement
.children
.head
);
160 case lang_wild_statement_enum
:
161 lang_for_each_statement_worker
163 s
->wild_statement
.children
.head
);
165 case lang_data_statement_enum
:
166 case lang_object_symbols_statement_enum
:
167 case lang_output_statement_enum
:
168 case lang_target_statement_enum
:
169 case lang_input_section_enum
:
170 case lang_input_statement_enum
:
171 case lang_fill_statement_enum
:
172 case lang_assignment_statement_enum
:
173 case lang_padding_statement_enum
:
174 case lang_address_statement_enum
:
184 DEFUN(lang_for_each_statement
,(func
),
187 lang_for_each_statement_worker(func
,
188 statement_list
.head
);
190 /*----------------------------------------------------------------------*/
192 DEFUN(lang_list_init
,(list
),
193 lang_statement_list_type
*list
)
195 list
->head
= (lang_statement_union_type
*)NULL
;
196 list
->tail
= &list
->head
;
200 /*----------------------------------------------------------------------
202 build a new statement node for the parse tree
207 lang_statement_union_type
*
208 DEFUN(new_statement
,(type
, size
, list
),
209 enum statement_enum type AND
210 bfd_size_type size AND
211 lang_statement_list_type
*list
)
213 lang_statement_union_type
*new = (lang_statement_union_type
*)
215 new->header
.type
= type
;
216 new->header
.next
= (lang_statement_union_type
*)NULL
;
217 lang_statement_append(list
, new, &new->header
.next
);
222 Build a new input file node for the language. There are several ways
223 in which we treat an input file, eg, we only look at symbols, or
224 prefix it with a -l etc.
226 We can be supplied with requests for input files more than once;
227 they may, for example be split over serveral lines like foo.o(.text)
228 foo.o(.data) etc, so when asked for a file we check that we havn't
229 got it already so we don't duplicate the bfd.
232 static lang_input_statement_type
*
233 DEFUN(new_afile
, (name
, file_type
, target
),
234 CONST
char *CONST name AND
235 CONST lang_input_file_enum_type file_type AND
236 CONST
char *CONST target
)
238 lang_input_statement_type
*p
= new_stat(lang_input_statement
,
240 lang_has_input_file
= true;
243 case lang_input_file_is_symbols_only_enum
:
245 p
->is_archive
=false;
247 p
->local_sym_name
= name
;
248 p
->just_syms_flag
= true;
249 p
->search_dirs_flag
= false;
251 case lang_input_file_is_fake_enum
:
253 p
->is_archive
=false;
255 p
->local_sym_name
= name
;
256 p
->just_syms_flag
= false;
257 p
->search_dirs_flag
=false;
259 case lang_input_file_is_l_enum
:
260 p
->is_archive
= true;
263 p
->local_sym_name
= concat("-l",name
,"");
264 p
->just_syms_flag
= false;
265 p
->search_dirs_flag
= true;
267 case lang_input_file_is_search_file_enum
:
268 case lang_input_file_is_marker_enum
:
270 p
->is_archive
=false;
272 p
->local_sym_name
= name
;
273 p
->just_syms_flag
= false;
274 p
->search_dirs_flag
=true;
276 case lang_input_file_is_file_enum
:
278 p
->is_archive
=false;
280 p
->local_sym_name
= name
;
281 p
->just_syms_flag
= false;
282 p
->search_dirs_flag
=false;
287 p
->asymbols
= (asymbol
**)NULL
;
288 p
->superfile
= (lang_input_statement_type
*)NULL
;
289 p
->next_real_file
= (lang_statement_union_type
*)NULL
;
290 p
->next
= (lang_statement_union_type
*)NULL
;
292 p
->common_output_section
= (asection
*)NULL
;
293 lang_statement_append(&input_file_chain
,
294 (lang_statement_union_type
*)p
,
301 lang_input_statement_type
*
302 DEFUN(lang_add_input_file
,(name
, file_type
, target
),
304 lang_input_file_enum_type file_type AND
307 /* Look it up or build a new one */
308 lang_has_input_file
= true;
310 lang_input_statement_type
*p
;
312 for (p
= (lang_input_statement_type
*)input_file_chain
.head
;
313 p
!= (lang_input_statement_type
*)NULL
;
314 p
= (lang_input_statement_type
*)(p
->next_real_file
))
316 /* Sometimes we have incomplete entries in here */
317 if (p
->filename
!= (char *)NULL
) {
318 if(strcmp(name
,p
->filename
) == 0) return p
;
323 return new_afile(name
, file_type
, target
);
327 /* Build enough state so that the parser can build its tree */
329 DEFUN_VOID(lang_init
)
332 stat_ptr
= &statement_list
;
333 lang_list_init(stat_ptr
);
335 lang_list_init(&input_file_chain
);
336 lang_list_init(&lang_output_section_statement
);
337 lang_list_init(&file_chain
);
338 first_file
= lang_add_input_file((char *)NULL
,
339 lang_input_file_is_marker_enum
,
344 /*----------------------------------------------------------------------
345 A region is an area of memory declared with the
346 MEMORY { name:org=exp, len=exp ... }
349 We maintain a list of all the regions here
351 If no regions are specified in the script, then the default is used
352 which is created when looked up to be the entire data space
355 static lang_memory_region_type
*lang_memory_region_list
;
356 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
358 lang_memory_region_type
*
359 DEFUN(lang_memory_region_lookup
,(name
),
360 CONST
char *CONST name
)
363 lang_memory_region_type
*p
= lang_memory_region_list
;
364 for (p
= lang_memory_region_list
;
365 p
!= ( lang_memory_region_type
*)NULL
;
367 if (strcmp(p
->name
, name
) == 0) {
371 if (strcmp(name
,"*default*")==0) {
372 /* This is the default region, dig out first one on the list */
373 if (lang_memory_region_list
!= (lang_memory_region_type
*)NULL
){
374 return lang_memory_region_list
;
378 lang_memory_region_type
*new =
379 (lang_memory_region_type
*)ldmalloc((bfd_size_type
)(sizeof(lang_memory_region_type
)));
380 new->name
= buystring(name
);
381 new->next
= (lang_memory_region_type
*)NULL
;
383 *lang_memory_region_list_tail
= new;
384 lang_memory_region_list_tail
= &new->next
;
393 lang_output_section_statement_type
*
394 DEFUN(lang_output_section_find
,(name
),
395 CONST
char * CONST name
)
397 lang_statement_union_type
*u
;
398 lang_output_section_statement_type
*lookup
;
400 for (u
= lang_output_section_statement
.head
;
401 u
!= (lang_statement_union_type
*)NULL
;
404 lookup
= &u
->output_section_statement
;
405 if (strcmp(name
, lookup
->name
)==0) {
409 return (lang_output_section_statement_type
*)NULL
;
412 lang_output_section_statement_type
*
413 DEFUN(lang_output_section_statement_lookup
,(name
),
414 CONST
char * CONST name
)
416 lang_output_section_statement_type
*lookup
;
417 lookup
=lang_output_section_find(name
);
418 if (lookup
== (lang_output_section_statement_type
*)NULL
) {
420 lookup
=(lang_output_section_statement_type
*)
421 new_stat(lang_output_section_statement
, stat_ptr
);
422 lookup
->region
= (lang_memory_region_type
*)NULL
;
424 lookup
->block_value
= 1;
427 lookup
->next
= (lang_statement_union_type
*)NULL
;
428 lookup
->bfd_section
= (asection
*)NULL
;
429 lookup
->processed
= false;
430 lookup
->addr_tree
= (etree_type
*)NULL
;
431 lang_list_init(&lookup
->children
);
433 lang_statement_append(&lang_output_section_statement
,
434 (lang_statement_union_type
*)lookup
,
445 DEFUN(print_flags
, (outfile
, ignore_flags
),
449 fprintf(outfile
,"(");
451 if (flags
->flag_read
) fprintf(outfile
,"R");
452 if (flags
->flag_write
) fprintf(outfile
,"W");
453 if (flags
->flag_executable
) fprintf(outfile
,"X");
454 if (flags
->flag_loadable
) fprintf(outfile
,"L");
456 fprintf(outfile
,")");
460 DEFUN(lang_map
,(outfile
),
463 lang_memory_region_type
*m
;
464 fprintf(outfile
,"**MEMORY CONFIGURATION**\n\n");
466 fprintf(outfile
,"name\t\torigin\t\tlength\t\tattributes\n");
468 fprintf(outfile
,"name\t\torigin length\t\tattributes\n");
470 for (m
= lang_memory_region_list
;
471 m
!= (lang_memory_region_type
*)NULL
;
474 fprintf(outfile
,"%-16s", m
->name
);
475 print_address(m
->origin
);
477 print_address(m
->length
);
479 print_flags(outfile
, &m
->flags
);
480 fprintf(outfile
,"\n");
482 fprintf(outfile
,"\n\n**LINK EDITOR MEMORY MAP**\n\n");
483 fprintf(outfile
,"output input virtual\n");
484 fprintf(outfile
,"section section address tsize\n\n");
495 lang_output_section_statement_type
*s
)
497 section_userdata_type
*new =
498 (section_userdata_type
*)
499 ldmalloc((bfd_size_type
)(sizeof(section_userdata_type
)));
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();
705 case lang_target_statement_enum
:
706 current_target
= statement
->target_statement
.target
;
714 DEFUN(open_input_bfds
,(statement
),
715 lang_statement_union_type
*statement
)
717 switch (statement
->header
.type
)
719 case lang_target_statement_enum
:
720 current_target
= statement
->target_statement
.target
;
722 case lang_wild_statement_enum
:
723 /* Maybe we should load the file's symbols */
724 if (statement
->wild_statement
.filename
)
726 (void) lookup_name(statement
->wild_statement
.filename
);
729 case lang_input_statement_enum
:
730 if (statement
->input_statement
.real
== true)
732 statement
->input_statement
.target
= current_target
;
733 lookup_name(statement
->input_statement
.filename
);
740 /* If there are [COMMONS] statements, put a wild one into the bss section */
743 lang_reasonable_defaults()
746 lang_output_section_statement_lookup(".text");
747 lang_output_section_statement_lookup(".data");
749 default_common_section
=
750 lang_output_section_statement_lookup(".bss");
753 if (placed_commons
== false) {
754 lang_wild_statement_type
*new =
755 new_stat(lang_wild_statement
,
756 &default_common_section
->children
);
757 new->section_name
= "COMMON";
758 new->filename
= (char *)NULL
;
759 lang_list_init(&new->children
);
766 Add the supplied name to the symbol table as an undefined reference.
767 Remove items from the chain as we open input bfds
769 typedef struct ldlang_undef_chain_list_struct
{
770 struct ldlang_undef_chain_list_struct
*next
;
772 } ldlang_undef_chain_list_type
;
774 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
777 DEFUN(ldlang_add_undef
,(name
),
778 CONST
char *CONST name
)
780 ldlang_undef_chain_list_type
*new =
781 (ldlang_undef_chain_list_type
782 *)ldmalloc((bfd_size_type
)(sizeof(ldlang_undef_chain_list_type
)));
784 new->next
= ldlang_undef_chain_list_head
;
785 ldlang_undef_chain_list_head
= new;
787 new->name
= buystring(name
);
789 /* Run through the list of undefineds created above and place them
790 into the linker hash table as undefined symbols belonging to the
794 DEFUN_VOID(lang_place_undefineds
)
796 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
797 while (ptr
!= (ldlang_undef_chain_list_type
*)NULL
) {
798 ldsym_type
*sy
= ldsym_get(ptr
->name
);
800 asymbol
**def_ptr
= (asymbol
**)ldmalloc((bfd_size_type
)(sizeof(asymbol
**)));
801 def
= (asymbol
*)bfd_make_empty_symbol(script_file
->the_bfd
);
803 def
->name
= ptr
->name
;
804 def
->flags
= BSF_UNDEFINED
;
805 def
->section
= (asection
*)NULL
;
806 Q_enter_global_ref(def_ptr
);
813 /* Copy important data from out internal form to the bfd way. Also
814 create a section for the dummy file
818 DEFUN_VOID(lang_create_output_section_statements
)
820 lang_statement_union_type
*os
;
821 for (os
= lang_output_section_statement
.head
;
822 os
!= (lang_statement_union_type
*)NULL
;
823 os
= os
->output_section_statement
.next
) {
824 lang_output_section_statement_type
*s
=
825 &os
->output_section_statement
;
832 DEFUN_VOID(lang_init_script_file
)
834 script_file
= lang_add_input_file("script file",
835 lang_input_file_is_fake_enum
,
837 script_file
->the_bfd
= bfd_create("script file", output_bfd
);
838 script_file
->symbol_count
= 0;
839 script_file
->the_bfd
->sections
= output_bfd
->sections
;
845 /* Open input files and attatch to output sections */
847 DEFUN(map_input_to_output_sections
,(s
, target
, output_section_statement
),
848 lang_statement_union_type
*s AND
849 CONST
char *target AND
850 lang_output_section_statement_type
*output_section_statement
)
852 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
854 switch (s
->header
.type
) {
857 case lang_wild_statement_enum
:
858 wild(&s
->wild_statement
, s
->wild_statement
.section_name
,
859 s
->wild_statement
.filename
, target
,
860 output_section_statement
);
863 case lang_constructors_statement_enum
:
864 map_input_to_output_sections(constructor_list
.head
,
866 output_section_statement
);
868 case lang_output_section_statement_enum
:
869 map_input_to_output_sections(s
->output_section_statement
.children
.head
,
871 &s
->output_section_statement
);
873 case lang_output_statement_enum
:
875 case lang_target_statement_enum
:
876 target
= s
->target_statement
.target
;
878 case lang_fill_statement_enum
:
879 case lang_input_section_enum
:
880 case lang_object_symbols_statement_enum
:
881 case lang_data_statement_enum
:
882 case lang_assignment_statement_enum
:
883 case lang_padding_statement_enum
:
885 case lang_afile_asection_pair_statement_enum
:
888 case lang_address_statement_enum
:
889 /* Mark the specified section with the supplied address */
891 lang_output_section_statement_type
*os
=
892 lang_output_section_statement_lookup
893 (s
->address_statement
.section_name
);
894 os
->addr_tree
= s
->address_statement
.address
;
895 if (os
->bfd_section
== (asection
*)NULL
) {
896 info("%P%F can't set the address of undefined section %s\n",
897 s
->address_statement
.section_name
);
901 case lang_input_statement_enum
:
902 /* A standard input statement, has no wildcards */
903 /* ldmain_open_file_read_symbol(&s->input_statement);*/
914 DEFUN(print_output_section_statement
,(output_section_statement
),
915 lang_output_section_statement_type
*output_section_statement
)
917 asection
*section
= output_section_statement
->bfd_section
;
919 print_section(output_section_statement
->name
);
922 print_dot
= section
->vma
;
926 print_address(section
->vma
);
928 print_size(section
->size
);
930 print_alignment(section
->alignment_power
);
933 printf("%s flags", output_section_statement
->region
->name
);
934 print_flags(stdout
, &output_section_statement
->flags
);
936 if (section
->flags
& SEC_LOAD
)
938 if (section
->flags
& SEC_ALLOC
)
940 if (section
->flags
& SEC_RELOC
)
942 if (section
->flags
& SEC_HAS_CONTENTS
)
947 printf("No attached output section");
950 print_statement(output_section_statement
->children
.head
,
951 output_section_statement
);
956 DEFUN(print_assignment
,(assignment
, output_section
),
957 lang_assignment_statement_type
*assignment AND
958 lang_output_section_statement_type
*output_section
)
960 etree_value_type result
;
965 print_address(print_dot
);
967 result
= exp_fold_tree(assignment
->exp
->assign
.src
,
969 lang_final_phase_enum
,
974 print_address(result
.value
);
978 printf("*undefined*");
981 exp_print_tree(stdout
, assignment
->exp
);
986 DEFUN(print_input_statement
,(statm
),
987 lang_input_statement_type
*statm
)
989 if (statm
->filename
!= (char *)NULL
) {
990 printf("LOAD %s\n",statm
->filename
);
995 DEFUN(print_symbol
,(q
),
1002 print_address(outside_symbol_address(q
));
1003 printf(" %s", q
->name
? q
->name
: " ");
1008 DEFUN(print_input_section
,(in
),
1009 lang_input_section_type
*in
)
1011 asection
*i
= in
->section
;
1016 print_section(i
->name
);
1018 if (i
->output_section
) {
1019 print_address(i
->output_section
->vma
+ i
->output_offset
);
1021 print_size(i
->size
);
1023 print_alignment(i
->alignment_power
);
1027 bfd
*abfd
= in
->ifile
->the_bfd
;
1028 if (in
->ifile
->just_syms_flag
== true) {
1029 printf("symbols only ");
1032 printf(" %s ",abfd
->xvec
->name
);
1033 if(abfd
->my_archive
!= (bfd
*)NULL
) {
1034 printf("[%s]%s", abfd
->my_archive
->filename
,
1038 printf("%s", abfd
->filename
);
1040 printf("(overhead %d bytes)", (int)bfd_alloc_size(abfd
));
1043 /* Find all the symbols in this file defined in this section */
1046 for (p
= in
->ifile
->asymbols
; *p
; p
++) {
1049 if (bfd_get_section(q
) == i
&& q
->flags
& BSF_GLOBAL
) {
1060 print_dot
= outside_section_address(i
) + i
->size
;
1063 printf("No output section allocated\n");
1069 DEFUN(print_fill_statement
,(fill
),
1070 lang_fill_statement_type
*fill
)
1072 printf("FILL mask ");
1073 print_fill( fill
->fill
);
1077 DEFUN(print_data_statement
,(data
),
1078 lang_data_statement_type
*data
)
1080 /* bfd_vma value; */
1085 /* ASSERT(print_dot == data->output_vma);*/
1087 print_address(data
->output_vma
);
1089 print_address(data
->value
);
1091 switch (data
->type
) {
1094 print_dot
+= BYTE_SIZE
;
1098 print_dot
+= SHORT_SIZE
;
1102 print_dot
+= LONG_SIZE
;
1106 exp_print_tree(stdout
, data
->exp
);
1113 DEFUN(print_padding_statement
,(s
),
1114 lang_padding_statement_type
*s
)
1118 print_section("*fill*");
1120 print_address(s
->output_offset
+ s
->output_section
->vma
);
1122 print_size(s
->size
);
1124 print_fill(s
->fill
);
1129 DEFUN(print_wild_statement
,(w
,os
),
1130 lang_wild_statement_type
*w AND
1131 lang_output_section_statement_type
*os
)
1134 if (w
->filename
!= (char *)NULL
) {
1135 printf("%s",w
->filename
);
1140 if (w
->section_name
!= (char *)NULL
) {
1141 printf("(%s)",w
->section_name
);
1147 print_statement(w
->children
.head
, os
);
1151 DEFUN(print_statement
,(s
, os
),
1152 lang_statement_union_type
*s AND
1153 lang_output_section_statement_type
*os
)
1156 switch (s
->header
.type
) {
1157 case lang_constructors_statement_enum
:
1158 printf("constructors:\n");
1159 print_statement(constructor_list
.head
, os
);
1162 case lang_wild_statement_enum
:
1163 print_wild_statement(&s
->wild_statement
, os
);
1166 printf("Fail with %d\n",s
->header
.type
);
1169 case lang_address_statement_enum
:
1170 printf("address\n");
1173 case lang_object_symbols_statement_enum
:
1174 printf("object symbols\n");
1176 case lang_fill_statement_enum
:
1177 print_fill_statement(&s
->fill_statement
);
1179 case lang_data_statement_enum
:
1180 print_data_statement(&s
->data_statement
);
1182 case lang_input_section_enum
:
1183 print_input_section(&s
->input_section
);
1185 case lang_padding_statement_enum
:
1186 print_padding_statement(&s
->padding_statement
);
1188 case lang_output_section_statement_enum
:
1189 print_output_section_statement(&s
->output_section_statement
);
1191 case lang_assignment_statement_enum
:
1192 print_assignment(&s
->assignment_statement
,
1197 case lang_target_statement_enum
:
1198 printf("TARGET(%s)\n", s
->target_statement
.target
);
1200 case lang_output_statement_enum
:
1201 printf("OUTPUT(%s %s)\n",
1202 s
->output_statement
.name
,
1205 case lang_input_statement_enum
:
1206 print_input_statement(&s
->input_statement
);
1208 case lang_afile_asection_pair_statement_enum
:
1218 DEFUN_VOID(print_statements
)
1220 print_statement(statement_list
.head
,
1221 (lang_output_section_statement_type
*)NULL
);
1225 DEFUN(insert_pad
,(this_ptr
, fill
, power
, output_section_statement
, dot
),
1226 lang_statement_union_type
**this_ptr AND
1228 unsigned int power AND
1229 asection
* output_section_statement AND
1232 /* Align this section first to the
1233 input sections requirement, then
1234 to the output section's requirement.
1235 If this alignment is > than any seen before,
1236 then record it too. Perform the alignment by
1237 inserting a magic 'padding' statement.
1240 unsigned int alignment_needed
= align_power(dot
, power
) - dot
;
1242 if (alignment_needed
!= 0)
1244 lang_statement_union_type
*new =
1245 (lang_statement_union_type
*)
1246 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1247 /* Link into existing chain */
1248 new->header
.next
= *this_ptr
;
1250 new->header
.type
= lang_padding_statement_enum
;
1251 new->padding_statement
.output_section
= output_section_statement
;
1252 new->padding_statement
.output_offset
=
1253 dot
- output_section_statement
->vma
;
1254 new->padding_statement
.fill
= fill
;
1255 new->padding_statement
.size
= alignment_needed
;
1259 /* Remember the most restrictive alignment */
1260 if (power
> output_section_statement
->alignment_power
) {
1261 output_section_statement
->alignment_power
= power
;
1263 output_section_statement
->size
+= alignment_needed
;
1264 return alignment_needed
+ dot
;
1268 /* Work out how much this section will move the dot point */
1270 DEFUN(size_input_section
, (this_ptr
, output_section_statement
, fill
, dot
),
1271 lang_statement_union_type
**this_ptr AND
1272 lang_output_section_statement_type
*output_section_statement AND
1273 unsigned short fill AND
1276 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1277 asection
*i
= is
->section
;
1279 if (is
->ifile
->just_syms_flag
== false) {
1280 dot
= insert_pad(this_ptr
, fill
, i
->alignment_power
,
1281 output_section_statement
->bfd_section
, dot
);
1283 /* remember the largest size so we can malloc the largest area
1284 needed for the output stage. Only remember the size of sections
1285 which we will actually allocate */
1287 (SEC_HAS_CONTENTS
| SEC_LOAD
)) == (SEC_HAS_CONTENTS
| SEC_LOAD
))
1288 && (i
->size
> largest_section
)) {
1289 largest_section
= i
->size
;
1292 /* Remember where in the output section this input section goes */
1294 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1296 /* Mark how big the output section must be to contain this now */
1298 output_section_statement
->bfd_section
->size
=
1299 dot
- output_section_statement
->bfd_section
->vma
;
1303 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1310 /* Work out the size of the output sections
1311 from the sizes of the input sections */
1313 DEFUN(lang_size_sections
,(s
, output_section_statement
, prev
, fill
, dot
),
1314 lang_statement_union_type
*s AND
1315 lang_output_section_statement_type
* output_section_statement AND
1316 lang_statement_union_type
**prev AND
1317 unsigned short fill AND
1320 /* Size up the sections from their constituent parts */
1321 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1323 switch (s
->header
.type
) {
1326 case lang_output_section_statement_enum
:
1329 lang_output_section_statement_type
*os
=
1330 &(s
->output_section_statement
);
1331 /* The start of a section */
1333 if (os
->addr_tree
== (etree_type
*)NULL
) {
1334 /* No address specified for this section, get one
1335 from the region specification
1337 if (os
->region
== (lang_memory_region_type
*)NULL
) {
1338 os
->region
= lang_memory_region_lookup("*default*");
1340 dot
= os
->region
->current
;
1343 etree_value_type r
;
1344 r
= exp_fold_tree(os
->addr_tree
,
1345 (lang_output_section_statement_type
*)NULL
,
1346 lang_allocating_phase_enum
,
1348 if (r
.valid
== false) {
1349 info("%F%S: non constant address expression for section %s\n",
1354 /* The section starts here */
1355 /* First, align to what the section needs */
1357 dot
= align_power(dot
, os
->bfd_section
->alignment_power
);
1358 os
->bfd_section
->vma
= dot
;
1359 os
->bfd_section
->output_offset
= 0;
1361 (void) lang_size_sections(os
->children
.head
, os
, &os
->children
.head
,
1363 /* Ignore the size of the input sections, use the vma and size to */
1367 after
= ALIGN(os
->bfd_section
->vma
+
1368 os
->bfd_section
->size
,
1372 os
->bfd_section
->size
= after
- os
->bfd_section
->vma
;
1373 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->size
;
1374 os
->processed
= true;
1376 /* Replace into region ? */
1377 if (os
->addr_tree
== (etree_type
*)NULL
1378 && os
->region
!=(lang_memory_region_type
*)NULL
) {
1379 os
->region
->current
= dot
;
1384 case lang_constructors_statement_enum
:
1385 dot
= lang_size_sections(constructor_list
.head
,
1386 output_section_statement
,
1387 &s
->wild_statement
.children
.head
,
1392 case lang_data_statement_enum
:
1395 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1396 s
->data_statement
.output_section
=
1397 output_section_statement
->bfd_section
;
1399 switch (s
->data_statement
.type
) {
1412 output_section_statement
->bfd_section
->size
+= size
;
1416 case lang_wild_statement_enum
:
1418 dot
= lang_size_sections(s
->wild_statement
.children
.head
,
1419 output_section_statement
,
1420 &s
->wild_statement
.children
.head
,
1426 case lang_object_symbols_statement_enum
:
1427 create_object_symbols
= output_section_statement
;
1429 case lang_output_statement_enum
:
1430 case lang_target_statement_enum
:
1432 case lang_input_section_enum
:
1433 dot
= size_input_section(prev
,
1434 output_section_statement
,
1435 output_section_statement
->fill
, dot
);
1437 case lang_input_statement_enum
:
1439 case lang_fill_statement_enum
:
1440 fill
= s
->fill_statement
.fill
;
1442 case lang_assignment_statement_enum
:
1444 bfd_vma newdot
= dot
;
1445 exp_fold_tree(s
->assignment_statement
.exp
,
1446 output_section_statement
,
1447 lang_allocating_phase_enum
,
1452 /* We've been moved ! so insert a pad */
1454 lang_statement_union_type
*new =
1455 (lang_statement_union_type
*)
1456 ldmalloc((bfd_size_type
)(sizeof(lang_padding_statement_type
)));
1457 /* Link into existing chain */
1458 new->header
.next
= *prev
;
1460 new->header
.type
= lang_padding_statement_enum
;
1461 new->padding_statement
.output_section
=
1462 output_section_statement
->bfd_section
;
1463 new->padding_statement
.output_offset
=
1464 dot
- output_section_statement
->bfd_section
->vma
;
1465 new->padding_statement
.fill
= fill
;
1466 new->padding_statement
.size
= newdot
- dot
;
1467 output_section_statement
->bfd_section
->size
+=
1468 new->padding_statement
.size
;
1474 case lang_padding_statement_enum
:
1480 case lang_address_statement_enum
:
1483 prev
= &s
->header
.next
;
1490 DEFUN(lang_do_assignments
,(s
, output_section_statement
, fill
, dot
),
1491 lang_statement_union_type
*s AND
1492 lang_output_section_statement_type
* output_section_statement AND
1493 unsigned short fill AND
1497 for (; s
!= (lang_statement_union_type
*)NULL
; s
= s
->next
)
1499 switch (s
->header
.type
) {
1500 case lang_constructors_statement_enum
:
1501 dot
= lang_do_assignments(constructor_list
.head
,
1502 output_section_statement
,
1507 case lang_output_section_statement_enum
:
1509 lang_output_section_statement_type
*os
=
1510 &(s
->output_section_statement
);
1511 dot
= os
->bfd_section
->vma
;
1512 (void) lang_do_assignments(os
->children
.head
, os
, os
->fill
, dot
);
1513 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->size
;
1516 case lang_wild_statement_enum
:
1518 dot
= lang_do_assignments(s
->wild_statement
.children
.head
,
1519 output_section_statement
,
1524 case lang_object_symbols_statement_enum
:
1525 case lang_output_statement_enum
:
1526 case lang_target_statement_enum
:
1528 case lang_common_statement_enum
:
1531 case lang_data_statement_enum
:
1533 etree_value_type value
;
1534 value
= exp_fold_tree(s
->data_statement
.exp
,
1535 0, lang_final_phase_enum
, dot
, &dot
);
1536 s
->data_statement
.value
= value
.value
;
1537 if (value
.valid
== false) info("%F%P: Invalid data statement\n");
1539 switch (s
->data_statement
.type
) {
1551 case lang_input_section_enum
:
1553 asection
*in
= s
->input_section
.section
;
1558 case lang_input_statement_enum
:
1560 case lang_fill_statement_enum
:
1561 fill
= s
->fill_statement
.fill
;
1563 case lang_assignment_statement_enum
:
1565 exp_fold_tree(s
->assignment_statement
.exp
,
1566 output_section_statement
,
1567 lang_final_phase_enum
,
1573 case lang_padding_statement_enum
:
1574 dot
+= s
->padding_statement
.size
;
1579 case lang_address_statement_enum
:
1590 DEFUN_VOID(lang_relocate_globals
)
1594 Each ldsym_type maintains a chain of pointers to asymbols which
1595 references the definition. Replace each pointer to the referenence
1596 with a pointer to only one place, preferably the definition. If
1597 the defintion isn't available then the common symbol, and if
1598 there isn't one of them then choose one reference.
1601 FOR_EACH_LDSYM(lgs
) {
1603 if (lgs
->sdefs_chain
) {
1604 it
= *(lgs
->sdefs_chain
);
1606 else if (lgs
->scoms_chain
!= (asymbol
**)NULL
) {
1607 it
= *(lgs
->scoms_chain
);
1609 else if (lgs
->srefs_chain
!= (asymbol
**)NULL
) {
1610 it
= *(lgs
->srefs_chain
);
1613 /* This can happen when the command line asked for a symbol to
1615 it
= (asymbol
*)NULL
;
1617 if (it
!= (asymbol
*)NULL
)
1619 asymbol
**ptr
= lgs
->srefs_chain
;;
1620 if (lgs
->flags
& SYM_WARNING
)
1622 produce_warnings(lgs
, it
);
1624 if (lgs
->flags
& SYM_INDIRECT
)
1629 while (ptr
!= (asymbol
**)NULL
) {
1630 asymbol
*ref
= *ptr
;
1632 ptr
= (asymbol
**)(ref
->udata
);
1641 DEFUN_VOID(lang_finish
)
1645 if (entry_symbol
== (char *)NULL
) {
1646 /* No entry has been specified, look for start */
1647 entry_symbol
= "start";
1649 lgs
= ldsym_get_soft(entry_symbol
);
1650 if (lgs
&& lgs
->sdefs_chain
) {
1651 asymbol
*sy
= *(lgs
->sdefs_chain
);
1652 /* We can set the entry address*/
1653 bfd_set_start_address(output_bfd
,
1654 outside_symbol_address(sy
));
1658 /* Can't find anything reasonable,
1659 use the first address in the text section
1661 asection
*ts
= bfd_get_section_by_name(output_bfd
, ".text");
1663 bfd_set_start_address(output_bfd
, ts
->vma
);
1668 /* By now we know the target architecture, and we may have an */
1669 /* ldfile_output_machine_name */
1671 DEFUN_VOID(lang_check
)
1673 lang_statement_union_type
*file
;
1676 unsigned long input_machine
;
1677 enum bfd_architecture input_architecture
;
1678 CONST
char *out_arch
;
1682 for (file
= file_chain
.head
;
1683 file
!= (lang_statement_union_type
*)NULL
;
1684 file
=file
->input_statement
.next
)
1686 unsigned long ldfile_new_output_machine
= 0;
1687 enum bfd_architecture ldfile_new_output_architecture
= bfd_arch_unknown
;
1689 input_bfd
= file
->input_statement
.the_bfd
;
1691 input_machine
= bfd_get_machine(input_bfd
);
1692 input_architecture
= bfd_get_architecture(input_bfd
);
1695 /* Inspect the architecture and ensure we're linking like with like */
1696 if (!bfd_arch_compatible(input_bfd
,
1698 &ldfile_new_output_architecture
,
1699 &ldfile_new_output_machine
))
1702 /* Result of bfd_printable_arch_mach is not guaranteed to stick
1703 around after next call, so we have to copy it. */
1704 out_arch
= bfd_printable_arch_mach(ldfile_output_architecture
,
1705 ldfile_output_machine
);
1706 out_arch2
= ldmalloc (strlen (out_arch
)+1);
1707 strcpy (out_arch2
, out_arch
);
1709 info("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
1710 bfd_printable_arch_mach(input_architecture
, input_machine
),
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
;
2082 DEFUN_VOID(find_constructors
)
2084 lang_statement_list_type
*old
= stat_ptr
;
2085 constructor_list_type
*p
= constructor_name_list
;
2086 stat_ptr
= & constructor_list
;
2087 lang_list_init(stat_ptr
);
2088 while (p
!= (constructor_list_type
*)NULL
)
2090 /* Have we already done this one ? */
2091 CONST
char *name
= p
->sym
->name
;
2092 int len
= strlen(name
);
2093 char *end
= ldmalloc(len
+3);
2098 ( exp_assop('=',name
, exp_nameop(NAME
,".")));
2101 (LONG
, exp_binop('-',
2104 exp_nameop(NAME
, end
),
2105 exp_nameop(NAME
,".")),
2111 lang_add_wild(name
, (char *)NULL
);
2112 lang_add_data(LONG
, exp_intop(0));
2114 (exp_assop('=', end
, exp_nameop(NAME
,".")));
2124 DEFUN_VOID(lang_process
)
2126 if (had_script
== false) {
2127 parse_line(ldemul_get_script());
2129 lang_reasonable_defaults();
2130 current_target
= default_target
;
2132 lang_for_each_statement(ldlang_open_output
); /* Open the output file */
2133 /* For each output section statement, create a section in the output
2135 lang_create_output_section_statements();
2137 /* Create a dummy bfd for the script */
2138 lang_init_script_file();
2140 /* Add to the hash table all undefineds on the command line */
2141 lang_place_undefineds();
2143 /* Create a bfd for each input file */
2144 current_target
= default_target
;
2145 lang_for_each_statement(open_input_bfds
);
2147 common_section
.userdata
= (PTR
)&common_section_userdata
;
2150 /* Run through the contours of the script and attatch input sections
2151 to the correct output sections
2153 find_constructors();
2154 map_input_to_output_sections(statement_list
.head
, (char *)NULL
,
2155 ( lang_output_section_statement_type
*)NULL
);
2158 /* Find any sections not attatched explicitly and handle them */
2159 lang_place_orphans();
2161 /* Size up the common data */
2164 ldemul_before_allocation();
2166 /* Size up the sections */
2167 lang_size_sections(statement_list
.head
,
2168 (lang_output_section_statement_type
*)NULL
,
2169 &(statement_list
.head
), 0, (bfd_vma
)0);
2171 /* See if anything special should be done now we know how big
2173 ldemul_after_allocation();
2175 /* Do all the assignments, now that we know the final restingplaces
2176 of all the symbols */
2178 lang_do_assignments(statement_list
.head
,
2179 (lang_output_section_statement_type
*)NULL
,
2182 /* Make sure that we're not mixing architectures */
2186 /* Move the global symbols around */
2187 lang_relocate_globals();
2194 /* EXPORTED TO YACC */
2197 DEFUN(lang_add_wild
,(section_name
, filename
),
2198 CONST
char *CONST section_name AND
2199 CONST
char *CONST filename
)
2201 lang_wild_statement_type
*new = new_stat(lang_wild_statement
,
2204 if (section_name
!= (char *)NULL
&& strcmp(section_name
,"COMMON") == 0)
2206 placed_commons
= true;
2208 if (filename
!= (char *)NULL
) {
2209 lang_has_input_file
= true;
2211 new->section_name
= section_name
;
2212 new->filename
= filename
;
2213 lang_list_init(&new->children
);
2216 DEFUN(lang_section_start
,(name
, address
),
2217 CONST
char *name AND
2218 etree_type
*address
)
2220 lang_address_statement_type
*ad
=new_stat(lang_address_statement
, stat_ptr
);
2221 ad
->section_name
= name
;
2222 ad
->address
= address
;
2226 DEFUN(lang_add_entry
,(name
),
2229 entry_symbol
= name
;
2233 DEFUN(lang_add_target
,(name
),
2236 lang_target_statement_type
*new = new_stat(lang_target_statement
,
2246 DEFUN(lang_add_map
,(name
),
2252 map_option_f
= true;
2260 DEFUN(lang_add_fill
,(exp
),
2263 lang_fill_statement_type
*new = new_stat(lang_fill_statement
,
2269 DEFUN(lang_add_data
,(type
, exp
),
2271 union etree_union
*exp
)
2274 lang_data_statement_type
*new = new_stat(lang_data_statement
,
2281 DEFUN(lang_add_assignment
,(exp
),
2284 lang_assignment_statement_type
*new = new_stat(lang_assignment_statement
,
2290 DEFUN(lang_add_attribute
,(attribute
),
2291 enum statement_enum attribute
)
2293 new_statement(attribute
, sizeof(lang_statement_union_type
),stat_ptr
);
2299 DEFUN(lang_startup
,(name
),
2302 if (startup_file
!= (char *)NULL
) {
2303 info("%P%FMultiple STARTUP files\n");
2305 first_file
->filename
= name
;
2306 first_file
->local_sym_name
= name
;
2311 DEFUN(lang_float
,(maybe
),
2314 lang_float_flag
= maybe
;
2318 DEFUN(lang_leave_output_section_statement
,(fill
, memspec
),
2320 CONST
char *memspec
)
2322 current_section
->fill
= fill
;
2323 current_section
->region
= lang_memory_region_lookup(memspec
);
2324 stat_ptr
= &statement_list
;
2326 /* We remember if we are closing a .data section, since we use it to
2327 store constructors in */
2328 if (strcmp(current_section
->name
, ".data") ==0) {
2329 end_of_data_section_statement_list
= statement_list
;
2334 Create an absolute symbol with the given name with the value of the
2335 address of first byte of the section named.
2337 If the symbol already exists, then do nothing.
2340 DEFUN(lang_abs_symbol_at_beginning_of
,(section
, name
),
2341 CONST
char *section AND
2344 if (ldsym_undefined(name
)) {
2345 extern bfd
*output_bfd
;
2346 extern asymbol
*create_symbol();
2347 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2348 asymbol
*def
= create_symbol(name
,
2349 BSF_GLOBAL
| BSF_EXPORT
|
2352 if (s
!= (asection
*)NULL
) {
2353 def
->value
= s
->vma
;
2362 Create an absolute symbol with the given name with the value of the
2363 address of the first byte after the end of the section named.
2365 If the symbol already exists, then do nothing.
2368 DEFUN(lang_abs_symbol_at_end_of
,(section
, name
),
2369 CONST
char *section AND
2372 if (ldsym_undefined(name
)){
2373 extern bfd
*output_bfd
;
2374 extern asymbol
*create_symbol();
2375 asection
*s
= bfd_get_section_by_name(output_bfd
, section
);
2376 /* Add a symbol called _end */
2377 asymbol
*def
= create_symbol(name
,
2378 BSF_GLOBAL
| BSF_EXPORT
|
2381 if (s
!= (asection
*)NULL
) {
2382 def
->value
= s
->vma
+ s
->size
;
2391 DEFUN(lang_statement_append
,(list
, element
, field
),
2392 lang_statement_list_type
*list AND
2393 lang_statement_union_type
*element AND
2394 lang_statement_union_type
**field
)
2396 *(list
->tail
) = element
;
2400 /* Set the output format type */
2402 DEFUN(lang_add_output_format
,(format
),
2405 output_target
= format
;