1 /* Linker command language support.
2 Copyright 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
36 static void print_statements
PARAMS ((void));
37 static void print_statement
PARAMS ((lang_statement_union_type
*,
38 lang_output_section_statement_type
*));
39 static lang_statement_union_type
*new_statement
PARAMS ((enum statement_enum
,
41 lang_statement_list_type
*));
45 static struct obstack stat_obstack
;
47 #define obstack_chunk_alloc ldmalloc
48 #define obstack_chunk_free free
49 static CONST
char *startup_file
;
50 static lang_statement_list_type input_file_chain
;
52 /* Points to the last statement in the .data section, so we can add
53 stuff to the data section without pain */
54 static lang_statement_list_type end_of_data_section_statement_list
;
56 /* List of statements needed to handle constructors */
57 extern lang_statement_list_type constructor_list
;
59 static boolean placed_commons
= false;
60 static lang_output_section_statement_type
*default_common_section
;
61 static boolean map_option_f
;
62 static bfd_vma print_dot
;
63 static lang_input_statement_type
*first_file
;
64 static lang_statement_list_type lang_output_section_statement
;
65 static CONST
char *current_target
;
66 static CONST
char *output_target
;
67 static size_t longest_section_name
= 8;
68 static section_userdata_type common_section_userdata
;
69 static lang_statement_list_type statement_list
;
73 lang_output_section_statement_type
*abs_output_section
;
74 lang_statement_list_type
*stat_ptr
= &statement_list
;
75 lang_input_statement_type
*script_file
= 0;
76 boolean option_longmap
= false;
77 lang_statement_list_type file_chain
=
79 CONST
char *entry_symbol
= 0;
80 bfd_size_type largest_section
= 0;
81 boolean lang_has_input_file
= false;
82 lang_output_section_statement_type
*create_object_symbols
= 0;
83 boolean had_output_filename
= false;
84 boolean lang_float_flag
= false;
87 extern char *default_target
;
89 extern unsigned int undefined_global_sym_count
;
90 extern char *current_file
;
91 extern bfd
*output_bfd
;
92 extern enum bfd_architecture ldfile_output_architecture
;
93 extern unsigned long ldfile_output_machine
;
94 extern char *ldfile_output_machine_name
;
95 extern ldsym_type
*symbol_head
;
96 extern unsigned int commons_pending
;
97 extern args_type command_line
;
98 extern ld_config_type config
;
99 extern boolean had_script
;
100 extern boolean write_map
;
101 extern int g_switch_value
;
104 etree_type
*base
; /* Relocation base - or null */
108 #define cat(a,b) a##b
110 #define cat(a,b) a/**/b
113 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
115 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
117 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
119 void lang_add_data
PARAMS ((int type
, union etree_union
* exp
));
125 return obstack_alloc (&stat_obstack
, size
);
131 fprintf (config
.map_file
, "%5x", (unsigned) value
);
134 print_alignment (value
)
137 fprintf (config
.map_file
, "2**%1u", value
);
140 DEFUN (print_fill
, (value
),
143 fprintf (config
.map_file
, "%04x", (unsigned) value
);
149 CONST
char *CONST name
;
151 fprintf (config
.map_file
, "%*s", -longest_section_name
, name
);
154 /*----------------------------------------------------------------------
155 lang_for_each_statement walks the parse tree and calls the provided
156 function for each node
160 lang_for_each_statement_worker (func
, s
)
162 lang_statement_union_type
*s
;
164 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
168 switch (s
->header
.type
)
170 case lang_constructors_statement_enum
:
171 lang_for_each_statement_worker (func
, constructor_list
.head
);
173 case lang_output_section_statement_enum
:
174 lang_for_each_statement_worker
176 s
->output_section_statement
.children
.head
);
178 case lang_wild_statement_enum
:
179 lang_for_each_statement_worker
181 s
->wild_statement
.children
.head
);
183 case lang_data_statement_enum
:
184 case lang_object_symbols_statement_enum
:
185 case lang_output_statement_enum
:
186 case lang_target_statement_enum
:
187 case lang_input_section_enum
:
188 case lang_input_statement_enum
:
189 case lang_assignment_statement_enum
:
190 case lang_padding_statement_enum
:
191 case lang_address_statement_enum
:
201 lang_for_each_statement (func
)
204 lang_for_each_statement_worker (func
,
205 statement_list
.head
);
208 /*----------------------------------------------------------------------*/
210 lang_list_init (list
)
211 lang_statement_list_type
*list
;
213 list
->head
= (lang_statement_union_type
*) NULL
;
214 list
->tail
= &list
->head
;
217 /*----------------------------------------------------------------------
219 build a new statement node for the parse tree
224 lang_statement_union_type
*
225 new_statement (type
, size
, list
)
226 enum statement_enum type
;
228 lang_statement_list_type
* list
;
230 lang_statement_union_type
*new = (lang_statement_union_type
*)
233 new->header
.type
= type
;
234 new->header
.next
= (lang_statement_union_type
*) NULL
;
235 lang_statement_append (list
, new, &new->header
.next
);
240 Build a new input file node for the language. There are several ways
241 in which we treat an input file, eg, we only look at symbols, or
242 prefix it with a -l etc.
244 We can be supplied with requests for input files more than once;
245 they may, for example be split over serveral lines like foo.o(.text)
246 foo.o(.data) etc, so when asked for a file we check that we havn't
247 got it already so we don't duplicate the bfd.
250 static lang_input_statement_type
*
251 new_afile (name
, file_type
, target
)
252 CONST
char *CONST name
;
253 CONST lang_input_file_enum_type file_type
;
254 CONST
char *CONST target
;
257 lang_input_statement_type
*p
= new_stat (lang_input_statement
,
260 lang_has_input_file
= true;
262 p
->complained
= false;
265 case lang_input_file_is_symbols_only_enum
:
267 p
->is_archive
= false;
269 p
->local_sym_name
= name
;
270 p
->just_syms_flag
= true;
271 p
->search_dirs_flag
= false;
273 case lang_input_file_is_fake_enum
:
275 p
->is_archive
= false;
277 p
->local_sym_name
= name
;
278 p
->just_syms_flag
= false;
279 p
->search_dirs_flag
= false;
281 case lang_input_file_is_l_enum
:
282 p
->is_archive
= true;
285 p
->local_sym_name
= concat ("-l", name
, "");
286 p
->just_syms_flag
= false;
287 p
->search_dirs_flag
= true;
289 case lang_input_file_is_search_file_enum
:
290 case lang_input_file_is_marker_enum
:
292 p
->is_archive
= false;
294 p
->local_sym_name
= name
;
295 p
->just_syms_flag
= false;
296 p
->search_dirs_flag
= true;
298 case lang_input_file_is_file_enum
:
300 p
->is_archive
= false;
302 p
->local_sym_name
= name
;
303 p
->just_syms_flag
= false;
304 p
->search_dirs_flag
= false;
309 p
->asymbols
= (asymbol
**) NULL
;
310 p
->superfile
= (lang_input_statement_type
*) NULL
;
311 p
->next_real_file
= (lang_statement_union_type
*) NULL
;
312 p
->next
= (lang_statement_union_type
*) NULL
;
314 p
->common_output_section
= (asection
*) NULL
;
315 lang_statement_append (&input_file_chain
,
316 (lang_statement_union_type
*) p
,
321 lang_input_statement_type
*
322 lang_add_input_file (name
, file_type
, target
)
324 lang_input_file_enum_type file_type
;
327 /* Look it up or build a new one */
328 lang_has_input_file
= true;
330 lang_input_statement_type
*p
;
332 for (p
= (lang_input_statement_type
*) input_file_chain
.head
;
333 p
!= (lang_input_statement_type
*) NULL
;
334 p
= (lang_input_statement_type
*) (p
->next_real_file
))
336 /* Sometimes we have incomplete entries in here */
337 if (p
->filename
!= (char *) NULL
)
339 if (strcmp (name
, p
->filename
) == 0)
345 return new_afile (name
, file_type
, target
);
349 lang_add_keepsyms_file (filename
)
350 CONST
char *filename
;
352 extern strip_symbols_type strip_symbols
;
353 if (keepsyms_file
!= 0)
354 info ("%X%P error: duplicated keep-symbols-file value\n");
355 keepsyms_file
= filename
;
356 if (strip_symbols
!= STRIP_NONE
)
357 info ("%P `-keep-only-symbols-file' overrides `-s' and `-S'\n");
358 strip_symbols
= STRIP_SOME
;
361 /* Build enough state so that the parser can build its tree */
365 obstack_begin (&stat_obstack
, 1000);
367 stat_ptr
= &statement_list
;
369 lang_list_init (stat_ptr
);
371 lang_list_init (&input_file_chain
);
372 lang_list_init (&lang_output_section_statement
);
373 lang_list_init (&file_chain
);
374 first_file
= lang_add_input_file ((char *) NULL
,
375 lang_input_file_is_marker_enum
,
377 abs_output_section
= lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
379 abs_output_section
->bfd_section
= &bfd_abs_section
;
383 /*----------------------------------------------------------------------
384 A region is an area of memory declared with the
385 MEMORY { name:org=exp, len=exp ... }
388 We maintain a list of all the regions here
390 If no regions are specified in the script, then the default is used
391 which is created when looked up to be the entire data space
394 static lang_memory_region_type
*lang_memory_region_list
;
395 static lang_memory_region_type
**lang_memory_region_list_tail
= &lang_memory_region_list
;
397 lang_memory_region_type
*
398 lang_memory_region_lookup (name
)
399 CONST
char *CONST name
;
402 lang_memory_region_type
*p
= lang_memory_region_list
;
404 for (p
= lang_memory_region_list
;
405 p
!= (lang_memory_region_type
*) NULL
;
408 if (strcmp (p
->name
, name
) == 0)
413 if (strcmp (name
, "*default*") == 0)
415 /* This is the default region, dig out first one on the list */
416 if (lang_memory_region_list
!= (lang_memory_region_type
*) NULL
)
418 return lang_memory_region_list
;
422 lang_memory_region_type
*new =
423 (lang_memory_region_type
*) stat_alloc ((bfd_size_type
) (sizeof (lang_memory_region_type
)));
425 new->name
= buystring (name
);
426 new->next
= (lang_memory_region_type
*) NULL
;
428 *lang_memory_region_list_tail
= new;
429 lang_memory_region_list_tail
= &new->next
;
431 new->length
= ~(bfd_size_type
)0;
433 new->had_full_message
= false;
440 lang_output_section_statement_type
*
441 lang_output_section_find (name
)
442 CONST
char *CONST name
;
444 lang_statement_union_type
*u
;
445 lang_output_section_statement_type
*lookup
;
447 for (u
= lang_output_section_statement
.head
;
448 u
!= (lang_statement_union_type
*) NULL
;
451 lookup
= &u
->output_section_statement
;
452 if (strcmp (name
, lookup
->name
) == 0)
457 return (lang_output_section_statement_type
*) NULL
;
460 lang_output_section_statement_type
*
461 lang_output_section_statement_lookup (name
)
462 CONST
char *CONST name
;
464 lang_output_section_statement_type
*lookup
;
466 lookup
= lang_output_section_find (name
);
467 if (lookup
== (lang_output_section_statement_type
*) NULL
)
470 lookup
= (lang_output_section_statement_type
*)
471 new_stat (lang_output_section_statement
, stat_ptr
);
472 lookup
->region
= (lang_memory_region_type
*) NULL
;
474 lookup
->block_value
= 1;
477 lookup
->next
= (lang_statement_union_type
*) NULL
;
478 lookup
->bfd_section
= (asection
*) NULL
;
479 lookup
->processed
= false;
480 lookup
->loadable
= 1;
481 lookup
->addr_tree
= (etree_type
*) NULL
;
482 lang_list_init (&lookup
->children
);
484 lookup
->memspec
= (CONST
char *) NULL
;
486 lookup
->subsection_alignment
= -1;
487 lookup
->section_alignment
= -1;
488 lookup
->load_base
= (union etree_union
*) NULL
;
490 lang_statement_append (&lang_output_section_statement
,
491 (lang_statement_union_type
*) lookup
,
499 print_flags (ignore_flags
)
502 fprintf (config
.map_file
, "(");
504 if (flags
->flag_read
)
505 fprintf (outfile
, "R");
506 if (flags
->flag_write
)
507 fprintf (outfile
, "W");
508 if (flags
->flag_executable
)
509 fprintf (outfile
, "X");
510 if (flags
->flag_loadable
)
511 fprintf (outfile
, "L");
513 fprintf (config
.map_file
, ")");
519 lang_memory_region_type
*m
;
521 fprintf (config
.map_file
, "**MEMORY CONFIGURATION**\n\n");
523 fprintf (config
.map_file
, "name\t\torigin\t\tlength\t\tattributes\n");
525 fprintf (config
.map_file
,
526 "name\t\torigin length r_size c_size is attributes\n");
529 for (m
= lang_memory_region_list
;
530 m
!= (lang_memory_region_type
*) NULL
;
533 fprintf (config
.map_file
, "%-16s", m
->name
);
534 print_address (m
->origin
);
536 print_address ((bfd_vma
)m
->length
);
538 print_address ((bfd_vma
)m
->old_length
);
540 print_address (m
->current
- m
->origin
);
543 fprintf(config
.map_file
," %2d%% ", ( m
->current
- m
->origin
) * 100 / m
->old_length
);
544 print_flags (&m
->flags
);
545 fprintf (config
.map_file
, "\n");
547 fprintf (config
.map_file
, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
548 fprintf (config
.map_file
, "output input virtual\n");
549 fprintf (config
.map_file
, "section section address tsize\n\n");
560 lang_output_section_statement_type
* s
;
562 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
563 section_userdata_type
*new =
564 (section_userdata_type
*)
565 stat_alloc ((bfd_size_type
) (sizeof (section_userdata_type
)));
567 s
->bfd_section
= bfd_get_section_by_name (output_bfd
, s
->name
);
568 if (s
->bfd_section
== (asection
*) NULL
)
569 s
->bfd_section
= bfd_make_section (output_bfd
, s
->name
);
570 if (s
->bfd_section
== (asection
*) NULL
)
572 einfo ("%P%F output format %s cannot represent section called %s\n",
573 output_bfd
->xvec
->name
, s
->name
);
575 s
->bfd_section
->output_section
= s
->bfd_section
;
576 /* s->bfd_section->flags = s->flags;*/
578 /* We initialize an output sections output offset to minus its own */
579 /* vma to allow us to output a section through itself */
580 s
->bfd_section
->output_offset
= 0;
581 get_userdata (s
->bfd_section
) = (PTR
) new;
585 /***********************************************************************
588 These expand statements like *(.text) and foo.o to a list of
589 explicit actions, like foo.o(.text), bar.o(.text) and
592 The toplevel routine, wild, takes a statement, section, file and
593 target. If either the section or file is null it is taken to be the
594 wildcard. Seperate lang_input_section statements are created for
595 each part of the expanstion, and placed after the statement provided.
600 wild_doit (ptr
, section
, output
, file
)
601 lang_statement_list_type
* ptr
;
603 lang_output_section_statement_type
* output
;
604 lang_input_statement_type
* file
;
606 if (output
->bfd_section
== (asection
*) NULL
)
609 /* Initialize the vma and size to the existing section. This will
610 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
612 if (section
!= (asection
*) NULL
)
614 bfd_set_section_vma (0, output
->bfd_section
,
615 bfd_section_vma (0, section
));
616 output
->bfd_section
->_raw_size
= section
->_raw_size
;
620 if (section
!= (asection
*) NULL
621 && section
->output_section
== (asection
*) NULL
)
623 /* Add a section reference to the list */
624 lang_input_section_type
*new = new_stat (lang_input_section
, ptr
);
626 new->section
= section
;
628 section
->output_section
= output
->bfd_section
;
630 /* Be selective about what the output section inherits from the
633 if ((section
->flags
& SEC_SHARED_LIBRARY
) != 0)
634 section
->output_section
->flags
|= section
->flags
;
636 section
->output_section
->flags
|= section
->flags
& ~SEC_NEVER_LOAD
;
638 if (!output
->loadable
)
640 /* Turn off load flag */
641 output
->bfd_section
->flags
&= ~SEC_LOAD
;
642 output
->bfd_section
->flags
|= SEC_NEVER_LOAD
;
644 if (section
->alignment_power
> output
->bfd_section
->alignment_power
)
646 output
->bfd_section
->alignment_power
= section
->alignment_power
;
648 /* If supplied an aligmnet, then force it */
649 if (output
->section_alignment
!= -1)
651 output
->bfd_section
->alignment_power
= output
->section_alignment
;
657 our_bfd_get_section_by_name (abfd
, section
)
661 return bfd_get_section_by_name (abfd
, section
);
665 wild_section (ptr
, section
, file
, output
)
666 lang_wild_statement_type
* ptr
;
668 lang_input_statement_type
* file
;
669 lang_output_section_statement_type
* output
;
673 if (file
->just_syms_flag
== false)
675 if (section
== (char *) NULL
)
677 /* Do the creation to all sections in the file */
678 for (s
= file
->the_bfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
681 if ((s
->flags
& SEC_IS_COMMON
) == 0)
683 wild_doit (&ptr
->children
, s
, output
, file
);
689 /* Do the creation to the named section only */
690 wild_doit (&ptr
->children
,
691 our_bfd_get_section_by_name (file
->the_bfd
, section
),
697 /* passed a file name (which must have been seen already and added to
698 the statement tree. We will see if it has been opened already and
699 had its symbols read. If not then we'll read it.
701 Archives are pecuilar here. We may open them once, but if they do
702 not define anything we need at the time, they won't have all their
703 symbols read. If we need them later, we'll have to redo it.
706 lang_input_statement_type
*
708 CONST
char *CONST name
;
710 lang_input_statement_type
*search
;
712 for (search
= (lang_input_statement_type
*) input_file_chain
.head
;
713 search
!= (lang_input_statement_type
*) NULL
;
714 search
= (lang_input_statement_type
*) search
->next_real_file
)
716 if (search
->filename
== (char *) NULL
&& name
== (char *) NULL
)
720 if (search
->filename
!= (char *) NULL
&& name
!= (char *) NULL
)
722 if (strcmp (search
->filename
, name
) == 0)
724 ldmain_open_file_read_symbol (search
);
730 /* There isn't an afile entry for this file yet, this must be
731 because the name has only appeared inside a load script and not
732 on the command line */
733 search
= new_afile (name
, lang_input_file_is_file_enum
, default_target
);
734 ldmain_open_file_read_symbol (search
);
741 wild (s
, section
, file
, target
, output
)
742 lang_wild_statement_type
* s
;
743 CONST
char *CONST section
;
744 CONST
char *CONST file
;
745 CONST
char *CONST target
;
746 lang_output_section_statement_type
* output
;
748 lang_input_statement_type
*f
;
750 if (file
== (char *) NULL
)
752 /* Perform the iteration over all files in the list */
753 for (f
= (lang_input_statement_type
*) file_chain
.head
;
754 f
!= (lang_input_statement_type
*) NULL
;
755 f
= (lang_input_statement_type
*) f
->next
)
757 wild_section (s
, section
, f
, output
);
759 /* Once more for the script file */
760 wild_section(s
, section
, script_file
, output
);
764 /* Perform the iteration over a single file */
765 wild_section (s
, section
, lookup_name (file
), output
);
767 if (section
!= (char *) NULL
768 && strcmp (section
, "COMMON") == 0
769 && default_common_section
== (lang_output_section_statement_type
*) NULL
)
771 /* Remember the section that common is going to incase we later
772 get something which doesn't know where to put it */
773 default_common_section
= output
;
778 read in all the files
782 CONST
char *CONST name
;
784 extern unsigned long ldfile_output_machine
;
785 extern enum bfd_architecture ldfile_output_architecture
;
787 extern CONST
char *output_filename
;
790 if (output_target
== (char *) NULL
)
792 if (current_target
!= (char *) NULL
)
793 output_target
= current_target
;
795 output_target
= default_target
;
797 output
= bfd_openw (name
, output_target
);
798 output_filename
= name
;
800 if (output
== (bfd
*) NULL
)
802 if (bfd_error
== invalid_target
)
804 einfo ("%P%F target %s not found\n", output_target
);
806 einfo ("%P%F problem opening output file %s, %E\n", name
);
809 /* output->flags |= D_PAGED;*/
811 bfd_set_format (output
, bfd_object
);
812 bfd_set_arch_mach (output
,
813 ldfile_output_architecture
,
814 ldfile_output_machine
);
815 bfd_set_gp_size (output
, g_switch_value
);
823 ldlang_open_output (statement
)
824 lang_statement_union_type
* statement
;
826 switch (statement
->header
.type
)
828 case lang_output_statement_enum
:
829 output_bfd
= open_output (statement
->output_statement
.name
);
830 ldemul_set_output_arch ();
831 if (config
.magic_demand_paged
&& !config
.relocateable_output
)
832 output_bfd
->flags
|= D_PAGED
;
834 output_bfd
->flags
&= ~D_PAGED
;
835 if (config
.text_read_only
)
836 output_bfd
->flags
|= WP_TEXT
;
838 output_bfd
->flags
&= ~WP_TEXT
;
841 case lang_target_statement_enum
:
842 current_target
= statement
->target_statement
.target
;
850 open_input_bfds (statement
)
851 lang_statement_union_type
* statement
;
853 switch (statement
->header
.type
)
855 case lang_target_statement_enum
:
856 current_target
= statement
->target_statement
.target
;
858 case lang_wild_statement_enum
:
859 /* Maybe we should load the file's symbols */
860 if (statement
->wild_statement
.filename
)
862 (void) lookup_name (statement
->wild_statement
.filename
);
865 case lang_input_statement_enum
:
866 if (statement
->input_statement
.real
== true)
868 statement
->input_statement
.target
= current_target
;
869 lookup_name (statement
->input_statement
.filename
);
877 /* If there are [COMMONS] statements, put a wild one into the bss section */
880 lang_reasonable_defaults ()
886 lang_output_section_statement_lookup (".text");
887 lang_output_section_statement_lookup (".data");
889 default_common_section
=
890 lang_output_section_statement_lookup (".bss");
893 if (placed_commons
== false)
895 lang_wild_statement_type
*new =
896 new_stat (lang_wild_statement
,
897 &default_common_section
->children
);
899 new->section_name
= "COMMON";
900 new->filename
= (char *) NULL
;
901 lang_list_init (&new->children
);
908 Add the supplied name to the symbol table as an undefined reference.
909 Remove items from the chain as we open input bfds
911 typedef struct ldlang_undef_chain_list
913 struct ldlang_undef_chain_list
*next
;
915 } ldlang_undef_chain_list_type
;
917 static ldlang_undef_chain_list_type
*ldlang_undef_chain_list_head
;
920 ldlang_add_undef (name
)
921 CONST
char *CONST name
;
923 ldlang_undef_chain_list_type
*new =
924 (ldlang_undef_chain_list_type
925 *) stat_alloc ((bfd_size_type
) (sizeof (ldlang_undef_chain_list_type
)));
927 new->next
= ldlang_undef_chain_list_head
;
928 ldlang_undef_chain_list_head
= new;
930 new->name
= buystring (name
);
933 /* Run through the list of undefineds created above and place them
934 into the linker hash table as undefined symbols belonging to the
938 lang_place_undefineds ()
940 ldlang_undef_chain_list_type
*ptr
= ldlang_undef_chain_list_head
;
942 while (ptr
!= (ldlang_undef_chain_list_type
*) NULL
)
945 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
947 def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
949 def
->name
= ptr
->name
;
950 def
->section
= &bfd_und_section
;
951 Q_enter_global_ref (def_ptr
, ptr
->name
);
956 /* Copy important data from out internal form to the bfd way. Also
957 create a section for the dummy file
961 lang_create_output_section_statements ()
963 lang_statement_union_type
*os
;
965 for (os
= lang_output_section_statement
.head
;
966 os
!= (lang_statement_union_type
*) NULL
;
967 os
= os
->output_section_statement
.next
)
969 lang_output_section_statement_type
*s
=
970 &os
->output_section_statement
;
978 lang_init_script_file ()
980 script_file
= lang_add_input_file ("command line",
981 lang_input_file_is_fake_enum
,
983 script_file
->the_bfd
= bfd_create ("command line", output_bfd
);
984 script_file
->symbol_count
= 0;
985 script_file
->the_bfd
->sections
= 0;
987 /* The user data of a bfd points to the input statement attatched */
988 script_file
->the_bfd
->usrdata
= (void *)script_file
;
989 script_file
->common_section
=
990 bfd_make_section(script_file
->the_bfd
,"COMMON");
993 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME
);
995 abs_output_section
->bfd_section
= &bfd_abs_section
;
999 /* Open input files and attatch to output sections */
1001 map_input_to_output_sections (s
, target
, output_section_statement
)
1002 lang_statement_union_type
* s
;
1004 lang_output_section_statement_type
* output_section_statement
;
1006 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1008 switch (s
->header
.type
)
1012 case lang_wild_statement_enum
:
1013 wild (&s
->wild_statement
, s
->wild_statement
.section_name
,
1014 s
->wild_statement
.filename
, target
,
1015 output_section_statement
);
1018 case lang_constructors_statement_enum
:
1019 map_input_to_output_sections (constructor_list
.head
,
1021 output_section_statement
);
1023 case lang_output_section_statement_enum
:
1024 map_input_to_output_sections (s
->output_section_statement
.children
.head
,
1026 &s
->output_section_statement
);
1028 case lang_output_statement_enum
:
1030 case lang_target_statement_enum
:
1031 target
= s
->target_statement
.target
;
1033 case lang_fill_statement_enum
:
1034 case lang_input_section_enum
:
1035 case lang_object_symbols_statement_enum
:
1036 case lang_data_statement_enum
:
1037 case lang_assignment_statement_enum
:
1038 case lang_padding_statement_enum
:
1040 case lang_afile_asection_pair_statement_enum
:
1043 case lang_address_statement_enum
:
1044 /* Mark the specified section with the supplied address */
1046 lang_output_section_statement_type
*os
=
1047 lang_output_section_statement_lookup
1048 (s
->address_statement
.section_name
);
1050 os
->addr_tree
= s
->address_statement
.address
;
1051 if (os
->bfd_section
== (asection
*) NULL
)
1053 einfo ("%P%F can't set the address of undefined section %s\n",
1054 s
->address_statement
.section_name
);
1058 case lang_input_statement_enum
:
1059 /* A standard input statement, has no wildcards */
1060 /* ldmain_open_file_read_symbol(&s->input_statement);*/
1071 print_output_section_statement (output_section_statement
)
1072 lang_output_section_statement_type
* output_section_statement
;
1074 asection
*section
= output_section_statement
->bfd_section
;
1077 print_section (output_section_statement
->name
);
1082 print_dot
= section
->vma
;
1086 print_address (section
->vma
);
1088 print_size (section
->_raw_size
);
1090 print_size(section
->_cooked_size
);
1092 print_alignment (section
->alignment_power
);
1095 fprintf (config
.map_file
, "%s flags", output_section_statement
->region
->name
);
1096 print_flags (stdout
, &output_section_statement
->flags
);
1098 if (section
->flags
& SEC_LOAD
)
1099 fprintf (config
.map_file
, "load ");
1100 if (section
->flags
& SEC_ALLOC
)
1101 fprintf (config
.map_file
, "alloc ");
1102 if (section
->flags
& SEC_RELOC
)
1103 fprintf (config
.map_file
, "reloc ");
1104 if (section
->flags
& SEC_HAS_CONTENTS
)
1105 fprintf (config
.map_file
, "contents ");
1110 fprintf (config
.map_file
, "No attached output section");
1113 if (output_section_statement
->load_base
)
1115 int b
= exp_get_value_int(output_section_statement
->load_base
,
1116 0, "output base", lang_final_phase_enum
);
1117 printf("Output address %08x\n", b
);
1119 if (output_section_statement
->section_alignment
>= 0
1120 || output_section_statement
->section_alignment
>= 0)
1122 printf("\t\t\t\t\tforced alignment ");
1123 if ( output_section_statement
->section_alignment
>= 0)
1125 printf("section 2**%d ",output_section_statement
->section_alignment
);
1127 if ( output_section_statement
->subsection_alignment
>= 0)
1129 printf("subsection 2**%d ",output_section_statement
->subsection_alignment
);
1134 print_statement (output_section_statement
->children
.head
,
1135 output_section_statement
);
1140 print_assignment (assignment
, output_section
)
1141 lang_assignment_statement_type
* assignment
;
1142 lang_output_section_statement_type
* output_section
;
1144 etree_value_type result
;
1150 print_address (print_dot
);
1152 result
= exp_fold_tree (assignment
->exp
->assign
.src
,
1154 lang_final_phase_enum
,
1160 print_address (result
.value
);
1164 fprintf (config
.map_file
, "*undefined*");
1167 exp_print_tree (assignment
->exp
);
1169 fprintf (config
.map_file
, "\n");
1173 print_input_statement (statm
)
1174 lang_input_statement_type
* statm
;
1176 if (statm
->filename
!= (char *) NULL
)
1178 fprintf (config
.map_file
, "LOAD %s\n", statm
->filename
);
1187 fprintf (config
.map_file
, " ");
1189 fprintf (config
.map_file
, " ");
1190 print_address (outside_symbol_address (q
));
1191 fprintf (config
.map_file
, " %s", q
->name
? q
->name
: " ");
1196 print_input_section (in
)
1197 lang_input_section_type
* in
;
1199 asection
*i
= in
->section
;
1200 int size
= i
->reloc_done
?
1201 bfd_get_section_size_after_reloc (i
) :
1202 bfd_get_section_size_before_reloc (i
);
1207 fprintf (config
.map_file
, " ");
1208 print_section (i
->name
);
1209 fprintf (config
.map_file
, " ");
1210 if (i
->output_section
)
1212 print_address (i
->output_section
->vma
+ i
->output_offset
);
1213 fprintf (config
.map_file
, " ");
1214 print_size (i
->_raw_size
);
1215 fprintf (config
.map_file
, " ");
1216 print_size(i
->_cooked_size
);
1217 fprintf (config
.map_file
, " ");
1218 print_alignment (i
->alignment_power
);
1219 fprintf (config
.map_file
, " ");
1223 bfd
*abfd
= in
->ifile
->the_bfd
;
1225 if (in
->ifile
->just_syms_flag
== true)
1227 fprintf (config
.map_file
, "symbols only ");
1230 fprintf (config
.map_file
, " %s ", abfd
->xvec
->name
);
1231 if (abfd
->my_archive
!= (bfd
*) NULL
)
1233 fprintf (config
.map_file
, "[%s]%s", abfd
->my_archive
->filename
,
1238 fprintf (config
.map_file
, "%s", abfd
->filename
);
1240 fprintf (config
.map_file
, "(overhead %d bytes)", (int) bfd_alloc_size (abfd
));
1243 /* Find all the symbols in this file defined in this section */
1245 if (in
->ifile
->symbol_count
)
1249 for (p
= in
->ifile
->asymbols
; *p
; p
++)
1253 if (bfd_get_section (q
) == i
&& q
->flags
& BSF_GLOBAL
)
1266 print_dot
= outside_section_address (i
) + size
;
1270 fprintf (config
.map_file
, "No output section allocated\n");
1276 print_fill_statement (fill
)
1277 lang_fill_statement_type
* fill
;
1279 fprintf (config
.map_file
, "FILL mask ");
1280 print_fill (fill
->fill
);
1284 print_data_statement (data
)
1285 lang_data_statement_type
* data
;
1287 /* bfd_vma value; */
1292 /* ASSERT(print_dot == data->output_vma);*/
1294 print_address (data
->output_vma
+ data
->output_section
->vma
);
1296 print_address (data
->value
);
1301 fprintf (config
.map_file
, "BYTE ");
1302 print_dot
+= BYTE_SIZE
;
1305 fprintf (config
.map_file
, "SHORT ");
1306 print_dot
+= SHORT_SIZE
;
1309 fprintf (config
.map_file
, "LONG ");
1310 print_dot
+= LONG_SIZE
;
1314 exp_print_tree (data
->exp
);
1316 fprintf (config
.map_file
, "\n");
1321 print_padding_statement (s
)
1322 lang_padding_statement_type
* s
;
1326 print_section ("*fill*");
1328 print_address (s
->output_offset
+ s
->output_section
->vma
);
1330 print_size (s
->size
);
1332 print_fill (s
->fill
);
1335 print_dot
= s
->output_offset
+ s
->output_section
->vma
+ s
->size
;
1340 print_wild_statement (w
, os
)
1341 lang_wild_statement_type
* w
;
1342 lang_output_section_statement_type
* os
;
1344 fprintf (config
.map_file
, " from ");
1345 if (w
->filename
!= (char *) NULL
)
1347 fprintf (config
.map_file
, "%s", w
->filename
);
1351 fprintf (config
.map_file
, "*");
1353 if (w
->section_name
!= (char *) NULL
)
1355 fprintf (config
.map_file
, "(%s)", w
->section_name
);
1359 fprintf (config
.map_file
, "(*)");
1362 print_statement (w
->children
.head
, os
);
1366 print_statement (s
, os
)
1367 lang_statement_union_type
* s
;
1368 lang_output_section_statement_type
* os
;
1372 switch (s
->header
.type
)
1374 case lang_constructors_statement_enum
:
1375 fprintf (config
.map_file
, "constructors:\n");
1376 print_statement (constructor_list
.head
, os
);
1378 case lang_wild_statement_enum
:
1379 print_wild_statement (&s
->wild_statement
, os
);
1382 fprintf (config
.map_file
, "Fail with %d\n", s
->header
.type
);
1385 case lang_address_statement_enum
:
1386 fprintf (config
.map_file
, "address\n");
1388 case lang_object_symbols_statement_enum
:
1389 fprintf (config
.map_file
, "object symbols\n");
1391 case lang_fill_statement_enum
:
1392 print_fill_statement (&s
->fill_statement
);
1394 case lang_data_statement_enum
:
1395 print_data_statement (&s
->data_statement
);
1397 case lang_input_section_enum
:
1398 print_input_section (&s
->input_section
);
1400 case lang_padding_statement_enum
:
1401 print_padding_statement (&s
->padding_statement
);
1403 case lang_output_section_statement_enum
:
1404 print_output_section_statement (&s
->output_section_statement
);
1406 case lang_assignment_statement_enum
:
1407 print_assignment (&s
->assignment_statement
,
1410 case lang_target_statement_enum
:
1411 fprintf (config
.map_file
, "TARGET(%s)\n", s
->target_statement
.target
);
1413 case lang_output_statement_enum
:
1414 fprintf (config
.map_file
, "OUTPUT(%s %s)\n",
1415 s
->output_statement
.name
,
1416 output_target
? output_target
: "");
1418 case lang_input_statement_enum
:
1419 print_input_statement (&s
->input_statement
);
1421 case lang_afile_asection_pair_statement_enum
:
1433 print_statement (statement_list
.head
,
1434 abs_output_section
);
1439 DEFUN (insert_pad
, (this_ptr
, fill
, power
, output_section_statement
, dot
),
1440 lang_statement_union_type
** this_ptr AND
1442 unsigned int power AND
1443 asection
* output_section_statement AND
1446 /* Align this section first to the
1447 input sections requirement, then
1448 to the output section's requirement.
1449 If this alignment is > than any seen before,
1450 then record it too. Perform the alignment by
1451 inserting a magic 'padding' statement.
1454 unsigned int alignment_needed
= align_power (dot
, power
) - dot
;
1456 if (alignment_needed
!= 0)
1458 lang_statement_union_type
*new =
1459 (lang_statement_union_type
*)
1460 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1462 /* Link into existing chain */
1463 new->header
.next
= *this_ptr
;
1465 new->header
.type
= lang_padding_statement_enum
;
1466 new->padding_statement
.output_section
= output_section_statement
;
1467 new->padding_statement
.output_offset
=
1468 dot
- output_section_statement
->vma
;
1469 new->padding_statement
.fill
= fill
;
1470 new->padding_statement
.size
= alignment_needed
;
1474 /* Remember the most restrictive alignment */
1475 if (power
> output_section_statement
->alignment_power
)
1477 output_section_statement
->alignment_power
= power
;
1479 output_section_statement
->_raw_size
+= alignment_needed
;
1480 return alignment_needed
+ dot
;
1484 /* Work out how much this section will move the dot point */
1486 DEFUN (size_input_section
, (this_ptr
, output_section_statement
, fill
, dot
, relax
),
1487 lang_statement_union_type
** this_ptr AND
1488 lang_output_section_statement_type
* output_section_statement AND
1489 unsigned short fill AND
1493 lang_input_section_type
*is
= &((*this_ptr
)->input_section
);
1494 asection
*i
= is
->section
;
1496 if (is
->ifile
->just_syms_flag
== false)
1498 if (output_section_statement
->subsection_alignment
!= -1)
1499 i
->alignment_power
=
1500 output_section_statement
->subsection_alignment
;
1502 dot
= insert_pad (this_ptr
, fill
, i
->alignment_power
,
1503 output_section_statement
->bfd_section
, dot
);
1505 /* remember the largest size so we can malloc the largest area
1506 needed for the output stage. Only remember the size of sections
1507 which we will actually allocate */
1508 if ((i
->flags
& SEC_HAS_CONTENTS
) != 0
1509 && (bfd_get_section_size_before_reloc (i
) > largest_section
))
1511 largest_section
= bfd_get_section_size_before_reloc (i
);
1514 /* Remember where in the output section this input section goes */
1516 i
->output_offset
= dot
- output_section_statement
->bfd_section
->vma
;
1518 /* Mark how big the output section must be to contain this now
1522 dot
+= i
->_cooked_size
;
1526 dot
+= i
->_raw_size
;
1528 output_section_statement
->bfd_section
->_raw_size
= dot
- output_section_statement
->bfd_section
->vma
;
1532 i
->output_offset
= i
->vma
- output_section_statement
->bfd_section
->vma
;
1538 /* Sizing happens in two passes, first pass we allocate worst case
1539 stuff. The second pass (if relaxing), we use what we learnt to
1540 change the size of some relocs from worst case to better
1542 static boolean had_relax
;
1545 DEFUN (lang_size_sections
, (s
, output_section_statement
, prev
, fill
, dot
, relax
),
1546 lang_statement_union_type
* s AND
1547 lang_output_section_statement_type
* output_section_statement AND
1548 lang_statement_union_type
** prev AND
1549 unsigned short fill AND
1553 /* Size up the sections from their constituent parts */
1554 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1556 switch (s
->header
.type
)
1559 case lang_output_section_statement_enum
:
1562 lang_output_section_statement_type
*os
= &s
->output_section_statement
;
1564 /* If this is a shared library section, don't change the size
1566 if (os
->bfd_section
->flags
& SEC_SHARED_LIBRARY
)
1569 if (os
->bfd_section
== &bfd_abs_section
)
1571 /* No matter what happens, an abs section starts at zero */
1572 bfd_set_section_vma (0, os
->bfd_section
, 0);
1576 if (os
->addr_tree
== (etree_type
*) NULL
)
1578 /* No address specified for this section, get one
1579 from the region specification
1581 if (os
->region
== (lang_memory_region_type
*) NULL
)
1583 os
->region
= lang_memory_region_lookup ("*default*");
1585 dot
= os
->region
->current
;
1591 r
= exp_fold_tree (os
->addr_tree
,
1593 lang_allocating_phase_enum
,
1595 if (r
.valid
== false)
1597 einfo ("%F%S: non constant address expression for section %s\n",
1602 /* The section starts here */
1603 /* First, align to what the section needs */
1606 dot
= align_power (dot
, os
->bfd_section
->alignment_power
);
1607 bfd_set_section_vma (0, os
->bfd_section
, dot
);
1609 if (os
->load_base
) {
1610 os
->bfd_section
->lma
1611 = exp_get_value_int(os
->load_base
, 0,"load base", lang_final_phase_enum
);
1616 os
->bfd_section
->output_offset
= 0;
1618 (void) lang_size_sections (os
->children
.head
, os
, &os
->children
.head
,
1619 os
->fill
, dot
, relax
);
1620 /* Ignore the size of the input sections, use the vma and size to */
1624 after
= ALIGN_N (os
->bfd_section
->vma
+
1625 os
->bfd_section
->_raw_size
,
1629 os
->bfd_section
->_raw_size
= after
- os
->bfd_section
->vma
;
1630 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1631 os
->processed
= true;
1633 /* Replace into region ? */
1634 if (os
->addr_tree
== (etree_type
*) NULL
1635 && os
->region
!= (lang_memory_region_type
*) NULL
)
1637 os
->region
->current
= dot
;
1638 /* Make sure this isn't silly */
1639 if (( os
->region
->current
1640 > os
->region
->origin
+ os
->region
->length
)
1641 || ( os
->region
->origin
> os
->region
->current
))
1643 einfo ("%X%P: Region %s is full (%B section %s)\n",
1645 os
->bfd_section
->owner
,
1646 os
->bfd_section
->name
);
1647 /* Reset the region pointer */
1648 os
->region
->current
= 0;
1656 case lang_constructors_statement_enum
:
1657 dot
= lang_size_sections (constructor_list
.head
,
1658 output_section_statement
,
1659 &s
->wild_statement
.children
.head
,
1664 case lang_data_statement_enum
:
1666 unsigned int size
= 0;
1668 s
->data_statement
.output_vma
= dot
- output_section_statement
->bfd_section
->vma
;
1669 s
->data_statement
.output_section
=
1670 output_section_statement
->bfd_section
;
1672 switch (s
->data_statement
.type
)
1686 output_section_statement
->bfd_section
->_raw_size
+= size
;
1690 case lang_wild_statement_enum
:
1692 dot
= lang_size_sections (s
->wild_statement
.children
.head
,
1693 output_section_statement
,
1694 &s
->wild_statement
.children
.head
,
1700 case lang_object_symbols_statement_enum
:
1701 create_object_symbols
= output_section_statement
;
1703 case lang_output_statement_enum
:
1704 case lang_target_statement_enum
:
1706 case lang_input_section_enum
:
1711 if( relax_section (prev
))
1717 (*prev
)->input_section
.section
->_cooked_size
=
1718 (*prev
)->input_section
.section
->_raw_size
;
1721 dot
= size_input_section (prev
,
1722 output_section_statement
,
1723 output_section_statement
->fill
,
1726 case lang_input_statement_enum
:
1728 case lang_fill_statement_enum
:
1729 s
->fill_statement
.output_section
= output_section_statement
->bfd_section
;
1731 fill
= s
->fill_statement
.fill
;
1733 case lang_assignment_statement_enum
:
1735 bfd_vma newdot
= dot
;
1737 exp_fold_tree (s
->assignment_statement
.exp
,
1738 output_section_statement
,
1739 lang_allocating_phase_enum
,
1743 if (newdot
!= dot
&& !relax
)
1744 /* We've been moved ! so insert a pad */
1746 lang_statement_union_type
*new =
1747 (lang_statement_union_type
*)
1748 stat_alloc ((bfd_size_type
) (sizeof (lang_padding_statement_type
)));
1750 /* Link into existing chain */
1751 new->header
.next
= *prev
;
1753 new->header
.type
= lang_padding_statement_enum
;
1754 new->padding_statement
.output_section
=
1755 output_section_statement
->bfd_section
;
1756 new->padding_statement
.output_offset
=
1757 dot
- output_section_statement
->bfd_section
->vma
;
1758 new->padding_statement
.fill
= fill
;
1759 new->padding_statement
.size
= newdot
- dot
;
1760 output_section_statement
->bfd_section
->_raw_size
+=
1761 new->padding_statement
.size
;
1770 /* This can only get here when relaxing is turned on */
1771 case lang_padding_statement_enum
:
1773 case lang_address_statement_enum
:
1776 prev
= &s
->header
.next
;
1782 DEFUN (lang_do_assignments
, (s
, output_section_statement
, fill
, dot
),
1783 lang_statement_union_type
* s AND
1784 lang_output_section_statement_type
* output_section_statement AND
1785 unsigned short fill AND
1789 for (; s
!= (lang_statement_union_type
*) NULL
; s
= s
->next
)
1791 switch (s
->header
.type
)
1793 case lang_constructors_statement_enum
:
1794 dot
= lang_do_assignments (constructor_list
.head
,
1795 output_section_statement
,
1800 case lang_output_section_statement_enum
:
1802 lang_output_section_statement_type
*os
=
1803 &(s
->output_section_statement
);
1805 dot
= os
->bfd_section
->vma
;
1806 (void) lang_do_assignments (os
->children
.head
, os
, os
->fill
, dot
);
1807 dot
= os
->bfd_section
->vma
+ os
->bfd_section
->_raw_size
;
1810 case lang_wild_statement_enum
:
1812 dot
= lang_do_assignments (s
->wild_statement
.children
.head
,
1813 output_section_statement
,
1818 case lang_object_symbols_statement_enum
:
1819 case lang_output_statement_enum
:
1820 case lang_target_statement_enum
:
1822 case lang_common_statement_enum
:
1825 case lang_data_statement_enum
:
1827 etree_value_type value
;
1829 value
= exp_fold_tree (s
->data_statement
.exp
,
1831 lang_final_phase_enum
, dot
, &dot
);
1832 s
->data_statement
.value
= value
.value
;
1833 if (value
.valid
== false)
1834 einfo ("%F%P: Invalid data statement\n");
1836 switch (s
->data_statement
.type
)
1849 case lang_input_section_enum
:
1851 asection
*in
= s
->input_section
.section
;
1853 dot
+= bfd_get_section_size_before_reloc (in
);
1857 case lang_input_statement_enum
:
1859 case lang_fill_statement_enum
:
1860 fill
= s
->fill_statement
.fill
;
1862 case lang_assignment_statement_enum
:
1864 exp_fold_tree (s
->assignment_statement
.exp
,
1865 output_section_statement
,
1866 lang_final_phase_enum
,
1872 case lang_padding_statement_enum
:
1873 dot
+= s
->padding_statement
.size
;
1878 case lang_address_statement_enum
:
1889 lang_relocate_globals ()
1892 Each ldsym_type maintains a chain of pointers to asymbols which
1893 references the definition. Replace each pointer to the referenence
1894 with a pointer to only one place, preferably the definition. If
1895 the defintion isn't available then the common symbol, and if
1896 there isn't one of them then choose one reference.
1899 FOR_EACH_LDSYM (lgs
)
1903 /* Skip indirect symbols. */
1904 if (lgs
->flags
& SYM_INDIRECT
)
1907 if (lgs
->sdefs_chain
)
1909 it
= *(lgs
->sdefs_chain
);
1911 else if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
1913 it
= *(lgs
->scoms_chain
);
1915 else if (lgs
->srefs_chain
!= (asymbol
**) NULL
)
1917 it
= *(lgs
->srefs_chain
);
1921 /* This can happen when the command line asked for a symbol to
1923 it
= (asymbol
*) NULL
;
1925 if (it
!= (asymbol
*) NULL
)
1928 asymbol
**ptr
= lgs
->srefs_chain
;;
1929 if (lgs
->flags
& SYM_WARNING
)
1931 produce_warnings (lgs
, it
);
1934 while (ptr
!= (asymbol
**) NULL
1937 asymbol
*ref
= *ptr
;
1940 ptr
= (asymbol
**) (ref
->udata
);
1952 int warn
= config
.relocateable_output
!= true;
1953 if (entry_symbol
== (char *) NULL
)
1955 /* No entry has been specified, look for start, but don't warn */
1956 entry_symbol
= "start";
1959 lgs
= ldsym_get_soft (entry_symbol
);
1960 if (lgs
&& lgs
->sdefs_chain
)
1962 asymbol
*sy
= *(lgs
->sdefs_chain
);
1964 /* We can set the entry address*/
1965 bfd_set_start_address (output_bfd
,
1966 outside_symbol_address (sy
));
1971 /* Can't find anything reasonable,
1972 use the first address in the text section
1974 asection
*ts
= bfd_get_section_by_name (output_bfd
, ".text");
1978 einfo ("%P: Warning, can't find entry symbol %s, defaulting to %V\n",
1979 entry_symbol
, ts
->vma
);
1981 bfd_set_start_address (output_bfd
, ts
->vma
);
1986 einfo ("%P: Warning, can't find entry symbol %s, not setting start address\n",
1992 /* By now we know the target architecture, and we may have an */
1993 /* ldfile_output_machine_name */
1997 lang_statement_union_type
*file
;
1999 unsigned long input_machine
;
2000 enum bfd_architecture input_architecture
;
2001 CONST bfd_arch_info_type
*compatible
;
2003 for (file
= file_chain
.head
;
2004 file
!= (lang_statement_union_type
*) NULL
;
2005 file
= file
->input_statement
.next
)
2007 input_bfd
= file
->input_statement
.the_bfd
;
2009 input_machine
= bfd_get_mach (input_bfd
);
2010 input_architecture
= bfd_get_arch (input_bfd
);
2013 /* Inspect the architecture and ensure we're linking like with
2016 compatible
= bfd_arch_get_compatible (input_bfd
,
2021 ldfile_output_machine
= compatible
->mach
;
2022 ldfile_output_architecture
= compatible
->arch
;
2027 info ("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
2028 bfd_printable_name (input_bfd
), input_bfd
,
2029 bfd_printable_name (output_bfd
));
2031 bfd_set_arch_mach (output_bfd
,
2040 * run through all the global common symbols and tie them
2041 * to the output section requested.
2043 As an experiment we do this 4 times, once for all the byte sizes,
2044 then all the two bytes, all the four bytes and then everything else
2053 if (config
.relocateable_output
== false ||
2054 command_line
.force_common_definition
== true)
2056 for (power
= 1; (config
.sort_common
== true && power
== 1) || (power
<= 16); power
<<= 1)
2058 for (lgs
= symbol_head
;
2059 lgs
!= (ldsym_type
*) NULL
;
2063 unsigned int power_of_two
;
2067 if (lgs
->scoms_chain
!= (asymbol
**) NULL
)
2069 com
= *(lgs
->scoms_chain
);
2099 if (config
.sort_common
== false || align
== power
)
2103 /* Change from a common symbol into a definition of
2105 lgs
->sdefs_chain
= lgs
->scoms_chain
;
2106 lgs
->scoms_chain
= (asymbol
**) NULL
;
2109 /* Point to the correct common section */
2110 symbfd
= bfd_asymbol_bfd (com
);
2111 if (com
->section
== &bfd_com_section
)
2113 ((lang_input_statement_type
*) symbfd
->usrdata
)
2120 name
= bfd_get_section_name (symbfd
,
2122 newsec
= bfd_get_section_by_name (symbfd
,
2124 /* BFD backend must provide this section. */
2125 if (newsec
== (asection
*) NULL
)
2126 einfo ("%P%F: No output section %s", name
);
2127 com
->section
= newsec
;
2130 /* Fix the size of the common section */
2132 com
->section
->_raw_size
=
2133 ALIGN_N (com
->section
->_raw_size
, align
);
2135 /* Remember if this is the biggest alignment ever seen */
2136 if (power_of_two
> com
->section
->alignment_power
)
2138 com
->section
->alignment_power
= power_of_two
;
2141 /* Symbol stops being common and starts being global, but
2142 we remember that it was common once. */
2144 com
->flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_OLD_COMMON
;
2145 com
->value
= com
->section
->_raw_size
;
2147 if (write_map
&& config
.map_file
)
2149 fprintf (config
.map_file
, "Allocating common %s: %x at %x %s\n",
2152 (unsigned) com
->value
,
2153 bfd_asymbol_bfd(com
)->filename
);
2156 com
->section
->_raw_size
+= size
;
2169 run through the input files and ensure that every input
2170 section has somewhere to go. If one is found without
2171 a destination then create an input request and place it
2172 into the statement tree.
2176 lang_place_orphans ()
2178 lang_input_statement_type
*file
;
2180 for (file
= (lang_input_statement_type
*) file_chain
.head
;
2181 file
!= (lang_input_statement_type
*) NULL
;
2182 file
= (lang_input_statement_type
*) file
->next
)
2186 for (s
= file
->the_bfd
->sections
;
2187 s
!= (asection
*) NULL
;
2190 if (s
->output_section
== (asection
*) NULL
)
2192 /* This section of the file is not attatched, root
2193 around for a sensible place for it to go */
2195 if (file
->common_section
== s
)
2197 /* This is a lonely common section which must
2198 have come from an archive. We attatch to the
2199 section with the wildcard */
2200 if (config
.relocateable_output
!= true
2201 && command_line
.force_common_definition
== false)
2203 if (default_common_section
==
2204 (lang_output_section_statement_type
*) NULL
)
2206 info ("%P: No [COMMON] command, defaulting to .bss\n");
2208 default_common_section
=
2209 lang_output_section_statement_lookup (".bss");
2212 wild_doit (&default_common_section
->children
, s
,
2213 default_common_section
, file
);
2218 lang_output_section_statement_type
*os
=
2219 lang_output_section_statement_lookup (s
->name
);
2221 wild_doit (&os
->children
, s
, os
, file
);
2230 lang_set_flags (ptr
, flags
)
2234 boolean state
= false;
2249 /* ptr->flag_read = state; */
2252 /* ptr->flag_write = state; */
2255 /* ptr->flag_executable= state;*/
2259 /* ptr->flag_loadable= state;*/
2262 einfo ("%P%F illegal syntax in flags\n");
2272 lang_for_each_file (func
)
2273 void (*func
) PARAMS ((lang_input_statement_type
*));
2275 lang_input_statement_type
*f
;
2277 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2278 f
!= (lang_input_statement_type
*) NULL
;
2279 f
= (lang_input_statement_type
*) f
->next
)
2287 lang_for_each_input_section (func
)
2288 void (*func
) PARAMS ((bfd
* ab
, asection
* as
));
2290 lang_input_statement_type
*f
;
2292 for (f
= (lang_input_statement_type
*) file_chain
.head
;
2293 f
!= (lang_input_statement_type
*) NULL
;
2294 f
= (lang_input_statement_type
*) f
->next
)
2298 for (s
= f
->the_bfd
->sections
;
2299 s
!= (asection
*) NULL
;
2302 func (f
->the_bfd
, s
);
2310 ldlang_add_file (entry
)
2311 lang_input_statement_type
* entry
;
2314 lang_statement_append (&file_chain
,
2315 (lang_statement_union_type
*) entry
,
2320 lang_add_output (name
)
2323 lang_output_statement_type
*new = new_stat (lang_output_statement
,
2327 had_output_filename
= true;
2331 static lang_output_section_statement_type
*current_section
;
2333 static int topower(x
)
2338 if (x
< 0) return -1;
2339 for (l
= 0; l
< 32; l
++)
2341 if (i
>= x
) return l
;
2347 lang_enter_output_section_statement (output_section_statement_name
,
2348 address_exp
, flags
, block_value
,
2349 align
, subalign
, base
)
2350 char *output_section_statement_name
;
2351 etree_type
* address_exp
;
2353 bfd_vma block_value
;
2355 etree_type
*subalign
;
2358 lang_output_section_statement_type
*os
;
2362 lang_output_section_statement_lookup (output_section_statement_name
);
2366 /* Add this statement to tree */
2367 /* add_statement(lang_output_section_statement_enum,
2368 output_section_statement);*/
2369 /* Make next things chain into subchain of this */
2371 if (os
->addr_tree
==
2372 (etree_type
*) NULL
)
2378 if (flags
& SEC_NEVER_LOAD
)
2382 os
->block_value
= block_value
? block_value
: 1;
2383 stat_ptr
= &os
->children
;
2385 os
->subsection_alignment
= topower(
2386 exp_get_value_int(subalign
, -1,
2387 "subsection alignment",
2389 os
->section_alignment
= topower(
2390 exp_get_value_int(align
, -1,
2391 "section alignment", 0));
2393 os
->load_base
= base
;
2400 if (had_output_filename
== false)
2402 extern CONST
char *output_filename
;
2404 lang_add_output (output_filename
);
2408 /* Reset the current counters in the regions */
2410 reset_memory_regions ()
2412 lang_memory_region_type
*p
= lang_memory_region_list
;
2414 for (p
= lang_memory_region_list
;
2415 p
!= (lang_memory_region_type
*) NULL
;
2418 p
->old_length
= (bfd_size_type
) (p
->current
- p
->origin
);
2419 p
->current
= p
->origin
;
2426 DEFUN (create_symbol
, (name
, flags
, section
),
2427 CONST
char *name AND
2431 extern lang_input_statement_type
*script_file
;
2432 asymbol
**def_ptr
= (asymbol
**) stat_alloc ((bfd_size_type
) (sizeof (asymbol
**)));
2434 /* Add this definition to script file */
2435 asymbol
*def
= (asymbol
*) bfd_make_empty_symbol (script_file
->the_bfd
);
2436 def
->name
= buystring (name
);
2439 def
->section
= section
;
2441 Q_enter_global_ref (def_ptr
, name
);
2448 if (had_script
== false)
2450 /* Read the emulation's appropriate default script. */
2451 char *scriptname
= ldemul_get_script ();
2452 size_t size
= strlen (scriptname
) + 13;
2453 char *buf
= (char *) ldmalloc(size
);
2455 sprintf (buf
, "-Tldscripts/%s", scriptname
);
2456 parse_line (buf
, 0);
2460 lang_reasonable_defaults ();
2461 current_target
= default_target
;
2463 lang_for_each_statement (ldlang_open_output
); /* Open the output file */
2464 /* For each output section statement, create a section in the output
2466 lang_create_output_section_statements ();
2468 /* Create a dummy bfd for the script */
2469 lang_init_script_file ();
2471 /* Add to the hash table all undefineds on the command line */
2472 lang_place_undefineds ();
2474 /* Create a bfd for each input file */
2475 current_target
= default_target
;
2476 lang_for_each_statement (open_input_bfds
);
2478 /* Run through the contours of the script and attatch input sections
2479 to the correct output sections
2481 find_constructors ();
2482 map_input_to_output_sections (statement_list
.head
, (char *) NULL
,
2483 (lang_output_section_statement_type
*) NULL
);
2486 /* Find any sections not attatched explicitly and handle them */
2487 lang_place_orphans ();
2489 /* Size up the common data */
2492 ldemul_before_allocation ();
2502 lang_size_sections (statement_list
.head
,
2503 (lang_output_section_statement_type
*) NULL
,
2504 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2505 /* FIXME. Until the code in relax is fixed so that it only reads in
2506 stuff once, we cant iterate since there is no way for the linker to
2507 know what has been patched and what hasn't */
2513 /* Now run around and relax if we can */
2514 if (command_line
.relax
)
2516 /* First time round is a trial run to get the 'worst case' addresses of the
2517 objects if there was no relaxing */
2518 lang_size_sections (statement_list
.head
,
2519 (lang_output_section_statement_type
*) NULL
,
2520 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2524 /* Move the global symbols around so the second pass of relaxing can
2526 lang_relocate_globals ();
2528 reset_memory_regions ();
2530 /* Do all the assignments, now that we know the final restingplaces
2531 of all the symbols */
2533 lang_do_assignments (statement_list
.head
,
2538 /* Perform another relax pass - this time we know where the
2539 globals are, so can make better guess */
2540 lang_size_sections (statement_list
.head
,
2541 (lang_output_section_statement_type
*) NULL
,
2542 &(statement_list
.head
), 0, (bfd_vma
) 0, true);
2550 /* Size up the sections */
2551 lang_size_sections (statement_list
.head
,
2553 &(statement_list
.head
), 0, (bfd_vma
) 0, false);
2558 /* See if anything special should be done now we know how big
2560 ldemul_after_allocation ();
2562 /* Do all the assignments, now that we know the final restingplaces
2563 of all the symbols */
2565 lang_do_assignments (statement_list
.head
,
2570 /* Move the global symbols around */
2571 lang_relocate_globals ();
2573 /* Make sure that we're not mixing architectures */
2581 /* EXPORTED TO YACC */
2584 lang_add_wild (section_name
, filename
)
2585 CONST
char *CONST section_name
;
2586 CONST
char *CONST filename
;
2588 lang_wild_statement_type
*new = new_stat (lang_wild_statement
,
2591 if (section_name
!= (char *) NULL
&& strcmp (section_name
, "COMMON") == 0)
2593 placed_commons
= true;
2595 if (filename
!= (char *) NULL
)
2597 lang_has_input_file
= true;
2599 new->section_name
= section_name
;
2600 new->filename
= filename
;
2601 lang_list_init (&new->children
);
2605 lang_section_start (name
, address
)
2607 etree_type
* address
;
2609 lang_address_statement_type
*ad
= new_stat (lang_address_statement
, stat_ptr
);
2611 ad
->section_name
= name
;
2612 ad
->address
= address
;
2616 lang_add_entry (name
)
2619 entry_symbol
= name
;
2623 lang_add_target (name
)
2626 lang_target_statement_type
*new = new_stat (lang_target_statement
,
2642 map_option_f
= true;
2653 lang_fill_statement_type
*new = new_stat (lang_fill_statement
,
2660 lang_add_data (type
, exp
)
2662 union etree_union
*exp
;
2665 lang_data_statement_type
*new = new_stat (lang_data_statement
,
2674 lang_add_assignment (exp
)
2677 lang_assignment_statement_type
*new = new_stat (lang_assignment_statement
,
2684 lang_add_attribute (attribute
)
2685 enum statement_enum attribute
;
2687 new_statement (attribute
, sizeof (lang_statement_union_type
), stat_ptr
);
2694 if (startup_file
!= (char *) NULL
)
2696 einfo ("%P%FMultiple STARTUP files\n");
2698 first_file
->filename
= name
;
2699 first_file
->local_sym_name
= name
;
2701 startup_file
= name
;
2708 lang_float_flag
= maybe
;
2712 lang_leave_output_section_statement (fill
, memspec
)
2714 CONST
char *memspec
;
2716 current_section
->fill
= fill
;
2717 current_section
->region
= lang_memory_region_lookup (memspec
);
2718 stat_ptr
= &statement_list
;
2720 /* We remember if we are closing a .data section, since we use it to
2721 store constructors in */
2722 if (strcmp (current_section
->name
, ".data") == 0)
2724 end_of_data_section_statement_list
= statement_list
;
2730 Create an absolute symbol with the given name with the value of the
2731 address of first byte of the section named.
2733 If the symbol already exists, then do nothing.
2736 lang_abs_symbol_at_beginning_of (section
, name
)
2737 CONST
char *section
;
2740 if (ldsym_undefined (name
))
2742 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2743 asymbol
*def
= create_symbol (name
,
2744 BSF_GLOBAL
| BSF_EXPORT
,
2747 if (s
!= (asection
*) NULL
)
2749 def
->value
= s
->vma
;
2759 Create an absolute symbol with the given name with the value of the
2760 address of the first byte after the end of the section named.
2762 If the symbol already exists, then do nothing.
2765 lang_abs_symbol_at_end_of (section
, name
)
2766 CONST
char *section
;
2769 if (ldsym_undefined (name
))
2771 asection
*s
= bfd_get_section_by_name (output_bfd
, section
);
2773 /* Add a symbol called _end */
2774 asymbol
*def
= create_symbol (name
,
2775 BSF_GLOBAL
| BSF_EXPORT
,
2778 if (s
!= (asection
*) NULL
)
2780 def
->value
= s
->vma
+ s
->_raw_size
;
2790 lang_statement_append (list
, element
, field
)
2791 lang_statement_list_type
* list
;
2792 lang_statement_union_type
* element
;
2793 lang_statement_union_type
** field
;
2795 *(list
->tail
) = element
;
2799 /* Set the output format type. -oformat overrides scripts. */
2801 lang_add_output_format (format
, from_script
)
2805 if (!from_script
|| output_target
== NULL
)
2806 output_target
= format
;