1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94 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. */
22 #include "libiberty.h"
37 static void print_statements PARAMS ((void));
38 static void print_statement PARAMS ((lang_statement_union_type *,
39 lang_output_section_statement_type *));
40 static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
42 lang_statement_list_type*));
46 static struct obstack stat_obstack;
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
50 static CONST char *startup_file;
51 static lang_statement_list_type input_file_chain;
53 /* Points to the last statement in the .data section, so we can add
54 stuff to the data section without pain */
55 static lang_statement_list_type end_of_data_section_statement_list;
57 static boolean placed_commons = false;
58 static lang_output_section_statement_type *default_common_section;
59 static boolean map_option_f;
60 static bfd_vma print_dot;
61 static lang_input_statement_type *first_file;
62 static lang_statement_list_type lang_output_section_statement;
63 static CONST char *current_target;
64 static CONST char *output_target;
65 static int longest_section_name = 8;
66 static lang_statement_list_type statement_list;
68 static void print_size PARAMS ((size_t value));
69 static void print_alignment PARAMS ((unsigned int value));
70 static void print_fill PARAMS ((fill_type value));
71 static void print_section PARAMS ((const char *name));
72 static void lang_for_each_statement_worker
73 PARAMS ((void (*func) (lang_statement_union_type *),
74 lang_statement_union_type *s));
75 static lang_input_statement_type *new_afile
76 PARAMS ((const char *name, lang_input_file_enum_type file_type,
77 const char *target, boolean add_to_list));
78 static void print_flags PARAMS ((int *ignore_flags));
79 static void init_os PARAMS ((lang_output_section_statement_type *s));
80 static void wild_doit PARAMS ((lang_statement_list_type *ptr,
82 lang_output_section_statement_type *output,
83 lang_input_statement_type *file));
84 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
86 lang_input_statement_type *file,
87 lang_output_section_statement_type *output));
88 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
89 static void load_symbols PARAMS ((lang_input_statement_type *entry));
90 static void wild PARAMS ((lang_wild_statement_type *s,
91 const char *section, const char *file,
93 lang_output_section_statement_type *output));
94 static bfd *open_output PARAMS ((const char *name));
95 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
96 static void open_input_bfds PARAMS ((lang_statement_union_type *statement));
97 static void lang_reasonable_defaults PARAMS ((void));
98 static void lang_place_undefineds PARAMS ((void));
99 static void map_input_to_output_sections
100 PARAMS ((lang_statement_union_type *s,
102 lang_output_section_statement_type *output_section_statement));
103 static void print_output_section_statement
104 PARAMS ((lang_output_section_statement_type *output_section_statement));
105 static void print_assignment
106 PARAMS ((lang_assignment_statement_type *assignment,
107 lang_output_section_statement_type *output_section));
108 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
109 static void print_input_section PARAMS ((lang_input_section_type *in));
110 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
111 static void print_data_statement PARAMS ((lang_data_statement_type *data));
112 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
113 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
114 static void print_wild_statement
115 PARAMS ((lang_wild_statement_type *w,
116 lang_output_section_statement_type *os));
117 static void print_statement PARAMS ((lang_statement_union_type *s,
118 lang_output_section_statement_type *os));
119 static void print_statements PARAMS ((void));
120 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
121 fill_type fill, unsigned int power,
122 asection *output_section_statement,
124 static bfd_vma size_input_section
125 PARAMS ((lang_statement_union_type **this_ptr,
126 lang_output_section_statement_type *output_section_statement,
127 fill_type fill, bfd_vma dot, boolean relax));
128 static bfd_vma lang_do_assignments
129 PARAMS ((lang_statement_union_type * s,
130 lang_output_section_statement_type *output_section_statement,
133 static void lang_finish PARAMS ((void));
134 static void lang_check PARAMS ((void));
135 static void lang_common PARAMS ((void));
136 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
137 static void lang_place_orphans PARAMS ((void));
138 static int topower PARAMS ((int));
139 static void reset_memory_regions PARAMS ((void));
142 lang_output_section_statement_type *abs_output_section;
143 lang_statement_list_type *stat_ptr = &statement_list;
144 lang_statement_list_type file_chain = { 0 };
145 static const char *entry_symbol = 0;
146 boolean lang_has_input_file = false;
147 boolean had_output_filename = false;
148 boolean lang_float_flag = false;
149 boolean delete_output_file_on_failure = false;
151 etree_type *base; /* Relocation base - or null */
154 #if defined(__STDC__) || defined(ALMOST_STDC)
155 #define cat(a,b) a##b
157 #define cat(a,b) a/**/b
160 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
162 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
164 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
170 return obstack_alloc (&stat_obstack, size);
177 fprintf (config.map_file, "%5x", (unsigned) value);
181 print_alignment (value)
184 fprintf (config.map_file, "2**%1u", value);
191 fprintf (config.map_file, "%04x", (unsigned) value);
198 fprintf (config.map_file, "%*s", -longest_section_name, name);
201 /*----------------------------------------------------------------------
202 lang_for_each_statement walks the parse tree and calls the provided
203 function for each node
207 lang_for_each_statement_worker (func, s)
208 void (*func) PARAMS ((lang_statement_union_type *));
209 lang_statement_union_type *s;
211 for (; s != (lang_statement_union_type *) NULL; s = s->next)
215 switch (s->header.type)
217 case lang_constructors_statement_enum:
218 lang_for_each_statement_worker (func, constructor_list.head);
220 case lang_output_section_statement_enum:
221 lang_for_each_statement_worker
223 s->output_section_statement.children.head);
225 case lang_wild_statement_enum:
226 lang_for_each_statement_worker
228 s->wild_statement.children.head);
230 case lang_data_statement_enum:
231 case lang_reloc_statement_enum:
232 case lang_object_symbols_statement_enum:
233 case lang_output_statement_enum:
234 case lang_target_statement_enum:
235 case lang_input_section_enum:
236 case lang_input_statement_enum:
237 case lang_assignment_statement_enum:
238 case lang_padding_statement_enum:
239 case lang_address_statement_enum:
249 lang_for_each_statement (func)
250 void (*func) PARAMS ((lang_statement_union_type *));
252 lang_for_each_statement_worker (func,
253 statement_list.head);
256 /*----------------------------------------------------------------------*/
258 lang_list_init (list)
259 lang_statement_list_type *list;
261 list->head = (lang_statement_union_type *) NULL;
262 list->tail = &list->head;
265 /*----------------------------------------------------------------------
267 build a new statement node for the parse tree
272 lang_statement_union_type *
273 new_statement (type, size, list)
274 enum statement_enum type;
276 lang_statement_list_type * list;
278 lang_statement_union_type *new = (lang_statement_union_type *)
281 new->header.type = type;
282 new->header.next = (lang_statement_union_type *) NULL;
283 lang_statement_append (list, new, &new->header.next);
288 Build a new input file node for the language. There are several ways
289 in which we treat an input file, eg, we only look at symbols, or
290 prefix it with a -l etc.
292 We can be supplied with requests for input files more than once;
293 they may, for example be split over serveral lines like foo.o(.text)
294 foo.o(.data) etc, so when asked for a file we check that we havn't
295 got it already so we don't duplicate the bfd.
298 static lang_input_statement_type *
299 new_afile (name, file_type, target, add_to_list)
301 lang_input_file_enum_type file_type;
305 lang_input_statement_type *p;
308 p = new_stat (lang_input_statement, stat_ptr);
311 p = ((lang_input_statement_type *)
312 stat_alloc (sizeof (lang_input_statement_type)));
313 p->header.next = NULL;
316 lang_has_input_file = true;
318 p->complained = false;
321 case lang_input_file_is_symbols_only_enum:
323 p->is_archive = false;
325 p->local_sym_name = name;
326 p->just_syms_flag = true;
327 p->search_dirs_flag = false;
329 case lang_input_file_is_fake_enum:
331 p->is_archive = false;
333 p->local_sym_name = name;
334 p->just_syms_flag = false;
335 p->search_dirs_flag = false;
337 case lang_input_file_is_l_enum:
338 p->is_archive = true;
341 p->local_sym_name = concat ("-l", name, (const char *) NULL);
342 p->just_syms_flag = false;
343 p->search_dirs_flag = true;
345 case lang_input_file_is_marker_enum:
347 p->is_archive = false;
349 p->local_sym_name = name;
350 p->just_syms_flag = false;
351 p->search_dirs_flag = true;
353 case lang_input_file_is_search_file_enum:
355 p->is_archive = false;
357 p->local_sym_name = name;
358 p->just_syms_flag = false;
359 p->search_dirs_flag = true;
361 case lang_input_file_is_file_enum:
363 p->is_archive = false;
365 p->local_sym_name = name;
366 p->just_syms_flag = false;
367 p->search_dirs_flag = false;
372 p->the_bfd = (bfd *) NULL;
373 p->asymbols = (asymbol **) NULL;
374 p->next_real_file = (lang_statement_union_type *) NULL;
375 p->next = (lang_statement_union_type *) NULL;
377 p->common_output_section = (asection *) NULL;
379 lang_statement_append (&input_file_chain,
380 (lang_statement_union_type *) p,
385 lang_input_statement_type *
386 lang_add_input_file (name, file_type, target)
388 lang_input_file_enum_type file_type;
391 lang_has_input_file = true;
392 return new_afile (name, file_type, target, true);
395 /* Build enough state so that the parser can build its tree */
399 obstack_begin (&stat_obstack, 1000);
401 stat_ptr = &statement_list;
403 lang_list_init (stat_ptr);
405 lang_list_init (&input_file_chain);
406 lang_list_init (&lang_output_section_statement);
407 lang_list_init (&file_chain);
408 first_file = lang_add_input_file ((char *) NULL,
409 lang_input_file_is_marker_enum,
411 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
413 abs_output_section->bfd_section = &bfd_abs_section;
417 /*----------------------------------------------------------------------
418 A region is an area of memory declared with the
419 MEMORY { name:org=exp, len=exp ... }
422 We maintain a list of all the regions here
424 If no regions are specified in the script, then the default is used
425 which is created when looked up to be the entire data space
428 static lang_memory_region_type *lang_memory_region_list;
429 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
431 lang_memory_region_type *
432 lang_memory_region_lookup (name)
433 CONST char *CONST name;
436 lang_memory_region_type *p = lang_memory_region_list;
438 for (p = lang_memory_region_list;
439 p != (lang_memory_region_type *) NULL;
442 if (strcmp (p->name, name) == 0)
447 if (strcmp (name, "*default*") == 0)
449 /* This is the default region, dig out first one on the list */
450 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
452 return lang_memory_region_list;
456 lang_memory_region_type *new =
457 (lang_memory_region_type *) stat_alloc ((bfd_size_type) (sizeof (lang_memory_region_type)));
459 new->name = buystring (name);
460 new->next = (lang_memory_region_type *) NULL;
462 *lang_memory_region_list_tail = new;
463 lang_memory_region_list_tail = &new->next;
465 new->length = ~(bfd_size_type)0;
467 new->had_full_message = false;
474 lang_output_section_statement_type *
475 lang_output_section_find (name)
476 CONST char *CONST name;
478 lang_statement_union_type *u;
479 lang_output_section_statement_type *lookup;
481 for (u = lang_output_section_statement.head;
482 u != (lang_statement_union_type *) NULL;
485 lookup = &u->output_section_statement;
486 if (strcmp (name, lookup->name) == 0)
491 return (lang_output_section_statement_type *) NULL;
494 lang_output_section_statement_type *
495 lang_output_section_statement_lookup (name)
496 CONST char *CONST name;
498 lang_output_section_statement_type *lookup;
500 lookup = lang_output_section_find (name);
501 if (lookup == (lang_output_section_statement_type *) NULL)
504 lookup = (lang_output_section_statement_type *)
505 new_stat (lang_output_section_statement, stat_ptr);
506 lookup->region = (lang_memory_region_type *) NULL;
508 lookup->block_value = 1;
511 lookup->next = (lang_statement_union_type *) NULL;
512 lookup->bfd_section = (asection *) NULL;
513 lookup->processed = false;
514 lookup->loadable = 1;
515 lookup->addr_tree = (etree_type *) NULL;
516 lang_list_init (&lookup->children);
518 lookup->memspec = (CONST char *) NULL;
520 lookup->subsection_alignment = -1;
521 lookup->section_alignment = -1;
522 lookup->load_base = (union etree_union *) NULL;
524 lang_statement_append (&lang_output_section_statement,
525 (lang_statement_union_type *) lookup,
533 print_flags (ignore_flags)
536 fprintf (config.map_file, "(");
538 if (flags->flag_read)
539 fprintf (outfile, "R");
540 if (flags->flag_write)
541 fprintf (outfile, "W");
542 if (flags->flag_executable)
543 fprintf (outfile, "X");
544 if (flags->flag_loadable)
545 fprintf (outfile, "L");
547 fprintf (config.map_file, ")");
553 lang_memory_region_type *m;
555 fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
557 fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
559 fprintf (config.map_file,
560 "name\t\torigin length r_size c_size is attributes\n");
563 for (m = lang_memory_region_list;
564 m != (lang_memory_region_type *) NULL;
567 fprintf (config.map_file, "%-16s", m->name);
568 print_address (m->origin);
570 print_address ((bfd_vma)m->length);
572 print_address ((bfd_vma)m->old_length);
574 print_address (m->current - m->origin);
577 fprintf (config.map_file, " %2d%% ",
578 (int) ((m->current - m->origin) * 100 / m->old_length));
579 print_flags (&m->flags);
580 fprintf (config.map_file, "\n");
582 fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
583 fprintf (config.map_file, "output input virtual\n");
584 fprintf (config.map_file, "section section address tsize\n\n");
595 lang_output_section_statement_type * s;
597 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
598 section_userdata_type *new =
599 (section_userdata_type *)
600 stat_alloc ((bfd_size_type) (sizeof (section_userdata_type)));
602 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
603 if (s->bfd_section == (asection *) NULL)
604 s->bfd_section = bfd_make_section (output_bfd, s->name);
605 if (s->bfd_section == (asection *) NULL)
607 einfo ("%P%F: output format %s cannot represent section called %s\n",
608 output_bfd->xvec->name, s->name);
610 s->bfd_section->output_section = s->bfd_section;
611 /* s->bfd_section->flags = s->flags;*/
613 /* We initialize an output sections output offset to minus its own */
614 /* vma to allow us to output a section through itself */
615 s->bfd_section->output_offset = 0;
616 get_userdata (s->bfd_section) = (PTR) new;
620 /***********************************************************************
623 These expand statements like *(.text) and foo.o to a list of
624 explicit actions, like foo.o(.text), bar.o(.text) and
627 The toplevel routine, wild, takes a statement, section, file and
628 target. If either the section or file is null it is taken to be the
629 wildcard. Seperate lang_input_section statements are created for
630 each part of the expanstion, and placed after the statement provided.
635 wild_doit (ptr, section, output, file)
636 lang_statement_list_type * ptr;
638 lang_output_section_statement_type * output;
639 lang_input_statement_type * file;
641 if (output->bfd_section == (asection *) NULL)
644 if (section != (asection *) NULL
645 && section->output_section == (asection *) NULL)
647 /* Add a section reference to the list */
648 lang_input_section_type *new = new_stat (lang_input_section, ptr);
650 new->section = section;
652 section->output_section = output->bfd_section;
654 /* We don't copy the SEC_NEVER_LOAD flag from an input section to
655 an output section, because we want to be able to include a
656 SEC_NEVER_LOAD section in the middle of an otherwise loaded
657 section (I don't know why we want to do this, but we do).
658 build_link_order in ldwrite.c handles this case by turning the
659 embedded SEC_NEVER_LOAD section into a fill. */
660 section->output_section->flags |=
661 section->flags & (flagword) (~ SEC_NEVER_LOAD);
663 if (!output->loadable)
665 /* Turn off load flag */
666 output->bfd_section->flags &= ~SEC_LOAD;
667 output->bfd_section->flags |= SEC_NEVER_LOAD;
669 if (section->alignment_power > output->bfd_section->alignment_power)
671 output->bfd_section->alignment_power = section->alignment_power;
673 /* If supplied an aligmnet, then force it */
674 if (output->section_alignment != -1)
676 output->bfd_section->alignment_power = output->section_alignment;
682 wild_section (ptr, section, file, output)
683 lang_wild_statement_type * ptr;
685 lang_input_statement_type * file;
686 lang_output_section_statement_type * output;
690 if (file->just_syms_flag == false)
692 if (section == (char *) NULL)
694 /* Do the creation to all sections in the file */
695 for (s = file->the_bfd->sections; s != NULL; s = s->next)
698 if ((s->flags & SEC_IS_COMMON) == 0)
700 wild_doit (&ptr->children, s, output, file);
706 /* Do the creation to the named section only */
707 s = bfd_get_section_by_name (file->the_bfd, section);
709 wild_doit (&ptr->children, s, output, file);
714 /* passed a file name (which must have been seen already and added to
715 the statement tree. We will see if it has been opened already and
716 had its symbols read. If not then we'll read it.
718 Archives are pecuilar here. We may open them once, but if they do
719 not define anything we need at the time, they won't have all their
720 symbols read. If we need them later, we'll have to redo it.
722 static lang_input_statement_type *
726 lang_input_statement_type *search;
728 for (search = (lang_input_statement_type *) input_file_chain.head;
729 search != (lang_input_statement_type *) NULL;
730 search = (lang_input_statement_type *) search->next_real_file)
732 if (search->filename == (char *) NULL && name == (char *) NULL)
734 if (search->filename != (char *) NULL
735 && name != (char *) NULL
736 && strcmp (search->filename, name) == 0)
740 if (search == (lang_input_statement_type *) NULL)
741 search = new_afile (name, lang_input_file_is_file_enum, default_target,
744 /* If we have already added this file, or this file is not real
745 (FIXME: can that ever actually happen?) or the name is NULL
746 (FIXME: can that ever actually happen?) don't add this file. */
749 || search->filename == (const char *) NULL)
752 load_symbols (search);
757 /* Get the symbols for an input file. */
761 lang_input_statement_type *entry;
766 ldfile_open_file (entry);
768 if (bfd_check_format (entry->the_bfd, bfd_object))
770 ldlang_add_file (entry);
771 if (trace_files || trace_file_tries)
772 info_msg ("%I\n", entry);
774 else if (bfd_check_format (entry->the_bfd, bfd_archive))
776 /* There is nothing to do here; the add_symbols routine will
777 call ldlang_add_file (via the add_archive_element callback)
778 for each element of the archive which is used. */
781 einfo ("%F%B: file not recognized: %E\n", entry->the_bfd);
783 if (bfd_link_add_symbols (entry->the_bfd, &link_info) == false)
784 einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd);
786 entry->loaded = true;
790 wild (s, section, file, target, output)
791 lang_wild_statement_type * s;
795 lang_output_section_statement_type * output;
797 lang_input_statement_type *f;
799 if (file == (char *) NULL)
801 /* Perform the iteration over all files in the list */
802 for (f = (lang_input_statement_type *) file_chain.head;
803 f != (lang_input_statement_type *) NULL;
804 f = (lang_input_statement_type *) f->next)
806 wild_section (s, section, f, output);
811 /* Perform the iteration over a single file */
812 wild_section (s, section, lookup_name (file), output);
814 if (section != (char *) NULL
815 && strcmp (section, "COMMON") == 0
816 && default_common_section == (lang_output_section_statement_type *) NULL)
818 /* Remember the section that common is going to incase we later
819 get something which doesn't know where to put it */
820 default_common_section = output;
825 read in all the files
834 if (output_target == (char *) NULL)
836 if (current_target != (char *) NULL)
837 output_target = current_target;
839 output_target = default_target;
841 output = bfd_openw (name, output_target);
843 if (output == (bfd *) NULL)
845 if (bfd_get_error () == bfd_error_invalid_target)
847 einfo ("%P%F: target %s not found\n", output_target);
849 einfo ("%P%F: cannot open output file %s: %E\n", name);
852 delete_output_file_on_failure = 1;
854 /* output->flags |= D_PAGED;*/
856 if (! bfd_set_format (output, bfd_object))
857 einfo ("%P%F:%s: can not make object file: %E\n", name);
858 if (! bfd_set_arch_mach (output,
859 ldfile_output_architecture,
860 ldfile_output_machine))
861 einfo ("%P%F:%s: can not set architecture: %E\n", name);
863 link_info.hash = bfd_link_hash_table_create (output);
864 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
865 einfo ("%P%F: can not create link hash table: %E\n");
867 bfd_set_gp_size (output, g_switch_value);
875 ldlang_open_output (statement)
876 lang_statement_union_type * statement;
878 switch (statement->header.type)
880 case lang_output_statement_enum:
881 ASSERT (output_bfd == (bfd *) NULL);
882 output_bfd = open_output (statement->output_statement.name);
883 ldemul_set_output_arch ();
884 if (config.magic_demand_paged && !link_info.relocateable)
885 output_bfd->flags |= D_PAGED;
887 output_bfd->flags &= ~D_PAGED;
888 if (config.text_read_only)
889 output_bfd->flags |= WP_TEXT;
891 output_bfd->flags &= ~WP_TEXT;
894 case lang_target_statement_enum:
895 current_target = statement->target_statement.target;
903 open_input_bfds (statement)
904 lang_statement_union_type * statement;
906 switch (statement->header.type)
908 case lang_target_statement_enum:
909 current_target = statement->target_statement.target;
911 case lang_wild_statement_enum:
912 /* Maybe we should load the file's symbols */
913 if (statement->wild_statement.filename)
915 (void) lookup_name (statement->wild_statement.filename);
918 case lang_input_statement_enum:
919 if (statement->input_statement.real == true)
921 statement->input_statement.target = current_target;
922 load_symbols (&statement->input_statement);
930 /* If there are [COMMONS] statements, put a wild one into the bss section */
933 lang_reasonable_defaults ()
936 lang_output_section_statement_lookup (".text");
937 lang_output_section_statement_lookup (".data");
939 default_common_section =
940 lang_output_section_statement_lookup (".bss");
943 if (placed_commons == false)
945 lang_wild_statement_type *new =
946 new_stat (lang_wild_statement,
947 &default_common_section->children);
949 new->section_name = "COMMON";
950 new->filename = (char *) NULL;
951 lang_list_init (&new->children);
958 Add the supplied name to the symbol table as an undefined reference.
959 Remove items from the chain as we open input bfds
961 typedef struct ldlang_undef_chain_list
963 struct ldlang_undef_chain_list *next;
965 } ldlang_undef_chain_list_type;
967 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
970 ldlang_add_undef (name)
971 CONST char *CONST name;
973 ldlang_undef_chain_list_type *new =
974 (ldlang_undef_chain_list_type
975 *) stat_alloc ((bfd_size_type) (sizeof (ldlang_undef_chain_list_type)));
977 new->next = ldlang_undef_chain_list_head;
978 ldlang_undef_chain_list_head = new;
980 new->name = buystring (name);
983 /* Run through the list of undefineds created above and place them
984 into the linker hash table as undefined symbols belonging to the
988 lang_place_undefineds ()
990 ldlang_undef_chain_list_type *ptr;
992 for (ptr = ldlang_undef_chain_list_head;
993 ptr != (ldlang_undef_chain_list_type *) NULL;
996 struct bfd_link_hash_entry *h;
998 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
999 if (h == (struct bfd_link_hash_entry *) NULL)
1000 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1001 if (h->type == bfd_link_hash_new)
1003 h->type = bfd_link_hash_undefined;
1004 h->u.undef.abfd = NULL;
1005 bfd_link_add_undef (link_info.hash, h);
1010 /* Open input files and attatch to output sections */
1012 map_input_to_output_sections (s, target, output_section_statement)
1013 lang_statement_union_type * s;
1015 lang_output_section_statement_type * output_section_statement;
1017 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1019 switch (s->header.type)
1023 case lang_wild_statement_enum:
1024 wild (&s->wild_statement, s->wild_statement.section_name,
1025 s->wild_statement.filename, target,
1026 output_section_statement);
1029 case lang_constructors_statement_enum:
1030 map_input_to_output_sections (constructor_list.head,
1032 output_section_statement);
1034 case lang_output_section_statement_enum:
1035 map_input_to_output_sections (s->output_section_statement.children.head,
1037 &s->output_section_statement);
1039 case lang_output_statement_enum:
1041 case lang_target_statement_enum:
1042 target = s->target_statement.target;
1044 case lang_fill_statement_enum:
1045 case lang_input_section_enum:
1046 case lang_object_symbols_statement_enum:
1047 case lang_data_statement_enum:
1048 case lang_reloc_statement_enum:
1049 case lang_assignment_statement_enum:
1050 case lang_padding_statement_enum:
1051 case lang_input_statement_enum:
1052 if (output_section_statement != NULL
1053 && output_section_statement->bfd_section == NULL)
1054 init_os (output_section_statement);
1056 case lang_afile_asection_pair_statement_enum:
1059 case lang_address_statement_enum:
1060 /* Mark the specified section with the supplied address */
1062 lang_output_section_statement_type *os =
1063 lang_output_section_statement_lookup
1064 (s->address_statement.section_name);
1066 if (os->bfd_section == NULL)
1068 os->addr_tree = s->address_statement.address;
1076 print_output_section_statement (output_section_statement)
1077 lang_output_section_statement_type * output_section_statement;
1079 asection *section = output_section_statement->bfd_section;
1082 print_section (output_section_statement->name);
1087 print_dot = section->vma;
1091 print_address (section->vma);
1093 print_size (section->_raw_size);
1095 print_size(section->_cooked_size);
1097 print_alignment (section->alignment_power);
1100 fprintf (config.map_file, "%s flags", output_section_statement->region->name);
1101 print_flags (stdout, &output_section_statement->flags);
1103 if (section->flags & SEC_LOAD)
1104 fprintf (config.map_file, "load ");
1105 if (section->flags & SEC_ALLOC)
1106 fprintf (config.map_file, "alloc ");
1107 if (section->flags & SEC_RELOC)
1108 fprintf (config.map_file, "reloc ");
1109 if (section->flags & SEC_HAS_CONTENTS)
1110 fprintf (config.map_file, "contents ");
1115 fprintf (config.map_file, " (no attached output section)");
1118 if (output_section_statement->load_base)
1120 int b = exp_get_value_int(output_section_statement->load_base,
1121 0, "output base", lang_final_phase_enum);
1122 printf("Output address %08x\n", b);
1124 if (output_section_statement->section_alignment >= 0
1125 || output_section_statement->section_alignment >= 0)
1127 printf("\t\t\t\t\tforced alignment ");
1128 if ( output_section_statement->section_alignment >= 0)
1130 printf("section 2**%d ",output_section_statement->section_alignment );
1132 if ( output_section_statement->subsection_alignment >= 0)
1134 printf("subsection 2**%d ",output_section_statement->subsection_alignment );
1139 print_statement (output_section_statement->children.head,
1140 output_section_statement);
1145 print_assignment (assignment, output_section)
1146 lang_assignment_statement_type * assignment;
1147 lang_output_section_statement_type * output_section;
1149 etree_value_type result;
1155 print_address (print_dot);
1157 result = exp_fold_tree (assignment->exp->assign.src,
1159 lang_final_phase_enum,
1165 print_address (result.value);
1169 fprintf (config.map_file, "*undefined*");
1172 exp_print_tree (assignment->exp);
1174 fprintf (config.map_file, "\n");
1178 print_input_statement (statm)
1179 lang_input_statement_type * statm;
1181 if (statm->filename != (char *) NULL)
1183 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1187 /* Print all the defined symbols for the abfd provided by in the supplied
1192 print_one_symbol (hash_entry, ptr)
1193 struct bfd_link_hash_entry *hash_entry;
1196 asection * sec = (asection *)ptr;
1198 if (hash_entry->type == bfd_link_hash_defined)
1200 if (sec == hash_entry->u.def.section) {
1202 fprintf (config.map_file, " ");
1204 fprintf (config.map_file, " ");
1205 print_address (hash_entry->u.def.value + outside_section_address (sec));
1206 fprintf (config.map_file, " %s", hash_entry->root.string);
1215 print_input_section (in)
1216 lang_input_section_type * in;
1218 asection *i = in->section;
1219 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1224 fprintf (config.map_file, " ");
1225 print_section (i->name);
1226 fprintf (config.map_file, " ");
1227 if (i->output_section)
1229 print_address (i->output_section->vma + i->output_offset);
1230 fprintf (config.map_file, " ");
1231 print_size (i->_raw_size);
1232 fprintf (config.map_file, " ");
1233 print_size(i->_cooked_size);
1234 fprintf (config.map_file, " ");
1235 print_alignment (i->alignment_power);
1236 fprintf (config.map_file, " ");
1240 bfd *abfd = in->ifile->the_bfd;
1242 if (in->ifile->just_syms_flag == true)
1244 fprintf (config.map_file, "symbols only ");
1247 fprintf (config.map_file, " %s ", abfd->xvec->name);
1248 if (abfd->my_archive != (bfd *) NULL)
1250 fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
1255 fprintf (config.map_file, "%s", abfd->filename);
1257 fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
1260 /* Print all the symbols */
1261 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1269 print_dot = outside_section_address (i) + size;
1273 fprintf (config.map_file, "No output section allocated\n");
1279 print_fill_statement (fill)
1280 lang_fill_statement_type * fill;
1282 fprintf (config.map_file, "FILL mask ");
1283 print_fill (fill->fill);
1287 print_data_statement (data)
1288 lang_data_statement_type * data;
1290 /* bfd_vma value; */
1295 /* ASSERT(print_dot == data->output_vma);*/
1297 print_address (data->output_vma + data->output_section->vma);
1299 print_address (data->value);
1304 fprintf (config.map_file, "BYTE ");
1305 print_dot += BYTE_SIZE;
1308 fprintf (config.map_file, "SHORT ");
1309 print_dot += SHORT_SIZE;
1312 fprintf (config.map_file, "LONG ");
1313 print_dot += LONG_SIZE;
1316 fprintf (config.map_file, "QUAD ");
1317 print_dot += QUAD_SIZE;
1321 exp_print_tree (data->exp);
1323 fprintf (config.map_file, "\n");
1326 /* Print a reloc statement. */
1329 print_reloc_statement (reloc)
1330 lang_reloc_statement_type *reloc;
1337 /* ASSERT(print_dot == data->output_vma);*/
1339 print_address (reloc->output_vma + reloc->output_section->vma);
1341 print_address (reloc->addend_value);
1344 fprintf (config.map_file, "RELOC %s ", reloc->howto->name);
1346 print_dot += bfd_get_reloc_size (reloc->howto);
1348 exp_print_tree (reloc->addend_exp);
1350 fprintf (config.map_file, "\n");
1354 print_padding_statement (s)
1355 lang_padding_statement_type * s;
1359 print_section ("*fill*");
1361 print_address (s->output_offset + s->output_section->vma);
1363 print_size (s->size);
1365 print_fill (s->fill);
1368 print_dot = s->output_offset + s->output_section->vma + s->size;
1373 print_wild_statement (w, os)
1374 lang_wild_statement_type * w;
1375 lang_output_section_statement_type * os;
1377 fprintf (config.map_file, " from ");
1378 if (w->filename != (char *) NULL)
1380 fprintf (config.map_file, "%s", w->filename);
1384 fprintf (config.map_file, "*");
1386 if (w->section_name != (char *) NULL)
1388 fprintf (config.map_file, "(%s)", w->section_name);
1392 fprintf (config.map_file, "(*)");
1395 print_statement (w->children.head, os);
1399 print_statement (s, os)
1400 lang_statement_union_type * s;
1401 lang_output_section_statement_type * os;
1405 switch (s->header.type)
1407 case lang_constructors_statement_enum:
1408 fprintf (config.map_file, "constructors:\n");
1409 print_statement (constructor_list.head, os);
1411 case lang_wild_statement_enum:
1412 print_wild_statement (&s->wild_statement, os);
1415 fprintf (config.map_file, "Fail with %d\n", s->header.type);
1418 case lang_address_statement_enum:
1419 fprintf (config.map_file, "address\n");
1421 case lang_object_symbols_statement_enum:
1422 fprintf (config.map_file, "object symbols\n");
1424 case lang_fill_statement_enum:
1425 print_fill_statement (&s->fill_statement);
1427 case lang_data_statement_enum:
1428 print_data_statement (&s->data_statement);
1430 case lang_reloc_statement_enum:
1431 print_reloc_statement (&s->reloc_statement);
1433 case lang_input_section_enum:
1434 print_input_section (&s->input_section);
1436 case lang_padding_statement_enum:
1437 print_padding_statement (&s->padding_statement);
1439 case lang_output_section_statement_enum:
1440 print_output_section_statement (&s->output_section_statement);
1442 case lang_assignment_statement_enum:
1443 print_assignment (&s->assignment_statement,
1446 case lang_target_statement_enum:
1447 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1449 case lang_output_statement_enum:
1450 fprintf (config.map_file, "OUTPUT(%s %s)\n",
1451 s->output_statement.name,
1452 output_target ? output_target : "");
1454 case lang_input_statement_enum:
1455 print_input_statement (&s->input_statement);
1457 case lang_afile_asection_pair_statement_enum:
1469 print_statement (statement_list.head,
1470 abs_output_section);
1475 insert_pad (this_ptr, fill, power, output_section_statement, dot)
1476 lang_statement_union_type ** this_ptr;
1479 asection * output_section_statement;
1482 /* Align this section first to the
1483 input sections requirement, then
1484 to the output section's requirement.
1485 If this alignment is > than any seen before,
1486 then record it too. Perform the alignment by
1487 inserting a magic 'padding' statement.
1490 unsigned int alignment_needed = align_power (dot, power) - dot;
1492 if (alignment_needed != 0)
1494 lang_statement_union_type *new =
1495 (lang_statement_union_type *)
1496 stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
1498 /* Link into existing chain */
1499 new->header.next = *this_ptr;
1501 new->header.type = lang_padding_statement_enum;
1502 new->padding_statement.output_section = output_section_statement;
1503 new->padding_statement.output_offset =
1504 dot - output_section_statement->vma;
1505 new->padding_statement.fill = fill;
1506 new->padding_statement.size = alignment_needed;
1510 /* Remember the most restrictive alignment */
1511 if (power > output_section_statement->alignment_power)
1513 output_section_statement->alignment_power = power;
1515 output_section_statement->_raw_size += alignment_needed;
1516 return alignment_needed + dot;
1520 /* Work out how much this section will move the dot point */
1522 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
1523 lang_statement_union_type ** this_ptr;
1524 lang_output_section_statement_type * output_section_statement;
1529 lang_input_section_type *is = &((*this_ptr)->input_section);
1530 asection *i = is->section;
1532 if (is->ifile->just_syms_flag == false)
1534 if (output_section_statement->subsection_alignment != -1)
1535 i->alignment_power =
1536 output_section_statement->subsection_alignment;
1538 dot = insert_pad (this_ptr, fill, i->alignment_power,
1539 output_section_statement->bfd_section, dot);
1541 /* Remember where in the output section this input section goes */
1543 i->output_offset = dot - output_section_statement->bfd_section->vma;
1545 /* Mark how big the output section must be to contain this now
1547 if (i->_cooked_size != 0)
1548 dot += i->_cooked_size;
1550 dot += i->_raw_size;
1551 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
1555 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1561 /* This variable indicates whether bfd_relax_section should be called
1564 static boolean relax_again;
1566 /* Set the sizes for all the output sections. */
1569 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
1570 lang_statement_union_type * s;
1571 lang_output_section_statement_type * output_section_statement;
1572 lang_statement_union_type ** prev;
1577 /* Size up the sections from their constituent parts */
1578 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1580 switch (s->header.type)
1583 case lang_output_section_statement_enum:
1586 lang_output_section_statement_type *os = &s->output_section_statement;
1588 if (os->bfd_section == NULL)
1590 /* This section was never actually created. */
1594 /* If this is a COFF shared library section, use the size and
1595 address from the input section. FIXME: This is COFF
1596 specific; it would be cleaner if there were some other way
1597 to do this, but nothing simple comes to mind. */
1598 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
1602 if (os->children.head == NULL
1603 || os->children.head->next != NULL
1604 || os->children.head->header.type != lang_input_section_enum)
1605 einfo ("%P%X: Internal error on COFF shared library section %s",
1608 input = os->children.head->input_section.section;
1609 bfd_set_section_vma (os->bfd_section->owner,
1611 bfd_section_vma (input->owner, input));
1612 os->bfd_section->_raw_size = input->_raw_size;
1616 if (os->bfd_section == &bfd_abs_section)
1618 /* No matter what happens, an abs section starts at zero */
1619 bfd_set_section_vma (0, os->bfd_section, 0);
1623 if (os->addr_tree == (etree_type *) NULL)
1625 /* No address specified for this section, get one
1626 from the region specification
1628 if (os->region == (lang_memory_region_type *) NULL)
1630 os->region = lang_memory_region_lookup ("*default*");
1632 dot = os->region->current;
1633 if (os->section_alignment == -1)
1634 dot = align_power (dot, os->bfd_section->alignment_power);
1640 r = exp_fold_tree (os->addr_tree,
1642 lang_allocating_phase_enum,
1644 if (r.valid == false)
1646 einfo ("%F%S: non constant address expression for section %s\n",
1651 /* The section starts here */
1652 /* First, align to what the section needs */
1654 if (os->section_alignment != -1)
1655 dot = align_power (dot, os->section_alignment);
1657 bfd_set_section_vma (0, os->bfd_section, dot);
1659 if (os->load_base) {
1660 os->bfd_section->lma
1661 = exp_get_value_int(os->load_base, 0,"load base", lang_final_phase_enum);
1666 os->bfd_section->output_offset = 0;
1668 (void) lang_size_sections (os->children.head, os, &os->children.head,
1669 os->fill, dot, relax);
1670 /* Ignore the size of the input sections, use the vma and size to */
1673 after = ALIGN_N (os->bfd_section->vma +
1674 os->bfd_section->_raw_size,
1675 /* The coercion here is important, see ld.h. */
1676 (bfd_vma) os->block_value);
1678 os->bfd_section->_raw_size = after - os->bfd_section->vma;
1679 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1680 os->processed = true;
1682 /* Replace into region ? */
1683 if (os->addr_tree == (etree_type *) NULL
1684 && os->region != (lang_memory_region_type *) NULL)
1686 os->region->current = dot;
1687 /* Make sure this isn't silly */
1688 if (( os->region->current
1689 > os->region->origin + os->region->length)
1690 || ( os->region->origin > os->region->current ))
1692 einfo ("%X%P: region %s is full (%B section %s)\n",
1694 os->bfd_section->owner,
1695 os->bfd_section->name);
1696 /* Reset the region pointer */
1697 os->region->current = 0;
1705 case lang_constructors_statement_enum:
1706 dot = lang_size_sections (constructor_list.head,
1707 output_section_statement,
1708 &s->wild_statement.children.head,
1713 case lang_data_statement_enum:
1715 unsigned int size = 0;
1717 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1718 s->data_statement.output_section =
1719 output_section_statement->bfd_section;
1721 switch (s->data_statement.type)
1738 output_section_statement->bfd_section->_raw_size += size;
1742 case lang_reloc_statement_enum:
1746 s->reloc_statement.output_vma =
1747 dot - output_section_statement->bfd_section->vma;
1748 s->reloc_statement.output_section =
1749 output_section_statement->bfd_section;
1750 size = bfd_get_reloc_size (s->reloc_statement.howto);
1752 output_section_statement->bfd_section->_raw_size += size;
1756 case lang_wild_statement_enum:
1758 dot = lang_size_sections (s->wild_statement.children.head,
1759 output_section_statement,
1760 &s->wild_statement.children.head,
1766 case lang_object_symbols_statement_enum:
1767 link_info.create_object_symbols_section =
1768 output_section_statement->bfd_section;
1770 case lang_output_statement_enum:
1771 case lang_target_statement_enum:
1773 case lang_input_section_enum:
1777 i = (*prev)->input_section.section;
1779 i->_cooked_size = i->_raw_size;
1784 if (! bfd_relax_section (i->owner, i, &link_info, &again))
1785 einfo ("%P%F: can't relax section: %E\n");
1789 dot = size_input_section (prev,
1790 output_section_statement,
1791 output_section_statement->fill,
1795 case lang_input_statement_enum:
1797 case lang_fill_statement_enum:
1798 s->fill_statement.output_section = output_section_statement->bfd_section;
1800 fill = s->fill_statement.fill;
1802 case lang_assignment_statement_enum:
1804 bfd_vma newdot = dot;
1806 exp_fold_tree (s->assignment_statement.exp,
1807 output_section_statement,
1808 lang_allocating_phase_enum,
1812 if (newdot != dot && !relax)
1814 /* The assignment changed dot. Insert a pad. */
1815 if (output_section_statement == abs_output_section)
1817 /* If we don't have an output section, then just adjust
1818 the default memory address. */
1819 lang_memory_region_lookup ("*default*")->current = newdot;
1823 lang_statement_union_type *new =
1824 ((lang_statement_union_type *)
1825 stat_alloc (sizeof (lang_padding_statement_type)));
1827 /* Link into existing chain */
1828 new->header.next = *prev;
1830 new->header.type = lang_padding_statement_enum;
1831 new->padding_statement.output_section =
1832 output_section_statement->bfd_section;
1833 new->padding_statement.output_offset =
1834 dot - output_section_statement->bfd_section->vma;
1835 new->padding_statement.fill = fill;
1836 new->padding_statement.size = newdot - dot;
1837 output_section_statement->bfd_section->_raw_size +=
1838 new->padding_statement.size;
1846 case lang_padding_statement_enum:
1847 /* If we are relaxing, and this is not the first pass, some
1848 padding statements may have been inserted during previous
1849 passes. We may have to move the padding statement to a new
1850 location if dot has a different value at this point in this
1851 pass than it did at this point in the previous pass. */
1852 s->padding_statement.output_offset =
1853 dot - output_section_statement->bfd_section->vma;
1854 dot += s->padding_statement.size;
1861 /* This can only get here when relaxing is turned on */
1863 case lang_address_statement_enum:
1866 prev = &s->header.next;
1872 lang_do_assignments (s, output_section_statement, fill, dot)
1873 lang_statement_union_type * s;
1874 lang_output_section_statement_type * output_section_statement;
1878 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1880 switch (s->header.type)
1882 case lang_constructors_statement_enum:
1883 dot = lang_do_assignments (constructor_list.head,
1884 output_section_statement,
1889 case lang_output_section_statement_enum:
1891 lang_output_section_statement_type *os =
1892 &(s->output_section_statement);
1894 if (os->bfd_section != NULL)
1896 dot = os->bfd_section->vma;
1897 (void) lang_do_assignments (os->children.head, os,
1899 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1903 case lang_wild_statement_enum:
1905 dot = lang_do_assignments (s->wild_statement.children.head,
1906 output_section_statement,
1911 case lang_object_symbols_statement_enum:
1912 case lang_output_statement_enum:
1913 case lang_target_statement_enum:
1915 case lang_common_statement_enum:
1918 case lang_data_statement_enum:
1920 etree_value_type value;
1922 value = exp_fold_tree (s->data_statement.exp,
1924 lang_final_phase_enum, dot, &dot);
1925 s->data_statement.value = value.value;
1926 if (value.valid == false)
1927 einfo ("%F%P: invalid data statement\n");
1929 switch (s->data_statement.type)
1946 case lang_reloc_statement_enum:
1948 etree_value_type value;
1950 value = exp_fold_tree (s->reloc_statement.addend_exp,
1952 lang_final_phase_enum, dot, &dot);
1953 s->reloc_statement.addend_value = value.value;
1954 if (value.valid == false)
1955 einfo ("%F%P: invalid reloc statement\n");
1957 dot += bfd_get_reloc_size (s->reloc_statement.howto);
1960 case lang_input_section_enum:
1962 asection *in = s->input_section.section;
1964 if (in->_cooked_size != 0)
1965 dot += in->_cooked_size;
1967 dot += in->_raw_size;
1971 case lang_input_statement_enum:
1973 case lang_fill_statement_enum:
1974 fill = s->fill_statement.fill;
1976 case lang_assignment_statement_enum:
1978 exp_fold_tree (s->assignment_statement.exp,
1979 output_section_statement,
1980 lang_final_phase_enum,
1986 case lang_padding_statement_enum:
1987 dot += s->padding_statement.size;
1992 case lang_address_statement_enum:
2003 struct bfd_link_hash_entry *h;
2004 boolean warn = link_info.relocateable ? false : true;
2006 if (entry_symbol == (char *) NULL)
2008 /* No entry has been specified. Look for start, but don't warn
2009 if we don't find it. */
2010 entry_symbol = "start";
2014 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2015 if (h != (struct bfd_link_hash_entry *) NULL
2016 && h->type == bfd_link_hash_defined)
2020 val = (h->u.def.value
2021 + bfd_get_section_vma (output_bfd,
2022 h->u.def.section->output_section)
2023 + h->u.def.section->output_offset);
2024 if (! bfd_set_start_address (output_bfd, val))
2025 einfo ("%P%F:%s: can't set start address\n", entry_symbol);
2031 /* Can't find the entry symbol. Use the first address in the
2033 ts = bfd_get_section_by_name (output_bfd, ".text");
2034 if (ts != (asection *) NULL)
2037 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2038 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2039 if (! bfd_set_start_address (output_bfd,
2040 bfd_get_section_vma (output_bfd, ts)))
2041 einfo ("%P%F: can't set start address\n");
2046 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2052 /* By now we know the target architecture, and we may have an */
2053 /* ldfile_output_machine_name */
2057 lang_statement_union_type *file;
2059 unsigned long input_machine;
2060 enum bfd_architecture input_architecture;
2061 CONST bfd_arch_info_type *compatible;
2063 for (file = file_chain.head;
2064 file != (lang_statement_union_type *) NULL;
2065 file = file->input_statement.next)
2067 input_bfd = file->input_statement.the_bfd;
2069 input_machine = bfd_get_mach (input_bfd);
2070 input_architecture = bfd_get_arch (input_bfd);
2073 /* Inspect the architecture and ensure we're linking like with
2076 compatible = bfd_arch_get_compatible (input_bfd,
2081 ldfile_output_machine = compatible->mach;
2082 ldfile_output_architecture = compatible->arch;
2087 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2088 bfd_printable_name (input_bfd), input_bfd,
2089 bfd_printable_name (output_bfd));
2091 if (! bfd_set_arch_mach (output_bfd,
2094 einfo ("%P%F:%s: can't set architecture: %E\n",
2095 bfd_get_filename (output_bfd));
2101 /* Look through all the global common symbols and attach them to the
2102 correct section. The -sort-common command line switch may be used
2103 to roughly sort the entries by size. */
2108 if (link_info.relocateable
2109 && ! command_line.force_common_definition)
2112 if (! config.sort_common)
2113 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2118 for (power = 1; power <= 16; power <<= 1)
2119 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2124 /* Place one common symbol in the correct section. */
2127 lang_one_common (h, info)
2128 struct bfd_link_hash_entry *h;
2131 unsigned int power_of_two;
2136 if (h->type != bfd_link_hash_common)
2169 if (config.sort_common && align != *(unsigned int *) info)
2172 section = h->u.c.section;
2174 /* Increase the size of the section. */
2175 section->_raw_size = ALIGN_N (section->_raw_size, align);
2177 /* Adjust the alignment if necessary. */
2178 if (power_of_two > section->alignment_power)
2179 section->alignment_power = power_of_two;
2181 /* Change the symbol from common to defined. */
2182 h->type = bfd_link_hash_defined;
2183 h->u.def.section = section;
2184 h->u.def.value = section->_raw_size;
2186 /* Increase the size of the section. */
2187 section->_raw_size += size;
2189 if (config.map_file != NULL)
2190 fprintf (config.map_file, "Allocating common %s: %lx at %lx %s\n",
2191 h->root.string, (unsigned long) size,
2192 (unsigned long) h->u.def.value, section->owner->filename);
2198 run through the input files and ensure that every input
2199 section has somewhere to go. If one is found without
2200 a destination then create an input request and place it
2201 into the statement tree.
2205 lang_place_orphans ()
2207 lang_input_statement_type *file;
2209 for (file = (lang_input_statement_type *) file_chain.head;
2210 file != (lang_input_statement_type *) NULL;
2211 file = (lang_input_statement_type *) file->next)
2215 if (file->just_syms_flag)
2218 for (s = file->the_bfd->sections;
2219 s != (asection *) NULL;
2222 if (s->output_section == (asection *) NULL)
2224 /* This section of the file is not attatched, root
2225 around for a sensible place for it to go */
2227 if (file->common_section == s)
2229 /* This is a lonely common section which must
2230 have come from an archive. We attatch to the
2231 section with the wildcard */
2232 if (! link_info.relocateable
2233 && ! command_line.force_common_definition)
2235 if (default_common_section ==
2236 (lang_output_section_statement_type *) NULL)
2238 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2240 default_common_section =
2241 lang_output_section_statement_lookup (".bss");
2244 wild_doit (&default_common_section->children, s,
2245 default_common_section, file);
2250 lang_output_section_statement_type *os =
2251 lang_output_section_statement_lookup (s->name);
2253 wild_doit (&os->children, s, os, file);
2262 lang_set_flags (ptr, flags)
2266 boolean state = false;
2281 /* ptr->flag_read = state; */
2284 /* ptr->flag_write = state; */
2287 /* ptr->flag_executable= state;*/
2291 /* ptr->flag_loadable= state;*/
2294 einfo ("%P%F: invalid syntax in flags\n");
2301 /* Call a function on each input file. This function will be called
2302 on an archive, but not on the elements. */
2305 lang_for_each_input_file (func)
2306 void (*func) PARAMS ((lang_input_statement_type *));
2308 lang_input_statement_type *f;
2310 for (f = (lang_input_statement_type *) input_file_chain.head;
2312 f = (lang_input_statement_type *) f->next_real_file)
2316 /* Call a function on each file. The function will be called on all
2317 the elements of an archive which are included in the link, but will
2318 not be called on the archive file itself. */
2321 lang_for_each_file (func)
2322 void (*func) PARAMS ((lang_input_statement_type *));
2324 lang_input_statement_type *f;
2326 for (f = (lang_input_statement_type *) file_chain.head;
2327 f != (lang_input_statement_type *) NULL;
2328 f = (lang_input_statement_type *) f->next)
2339 lang_for_each_input_section (func)
2340 void (*func) PARAMS ((bfd * ab, asection * as));
2342 lang_input_statement_type *f;
2344 for (f = (lang_input_statement_type *) file_chain.head;
2345 f != (lang_input_statement_type *) NULL;
2346 f = (lang_input_statement_type *) f->next)
2350 for (s = f->the_bfd->sections;
2351 s != (asection *) NULL;
2354 func (f->the_bfd, s);
2362 ldlang_add_file (entry)
2363 lang_input_statement_type * entry;
2367 lang_statement_append (&file_chain,
2368 (lang_statement_union_type *) entry,
2371 /* The BFD linker needs to have a list of all input BFDs involved in
2373 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
2374 ASSERT (entry->the_bfd != output_bfd);
2375 for (pp = &link_info.input_bfds;
2376 *pp != (bfd *) NULL;
2377 pp = &(*pp)->link_next)
2379 *pp = entry->the_bfd;
2380 entry->the_bfd->usrdata = (PTR) entry;
2381 bfd_set_gp_size (entry->the_bfd, g_switch_value);
2385 lang_add_output (name, from_script)
2389 /* Make -o on command line override OUTPUT in script. */
2390 if (had_output_filename == false || !from_script)
2392 output_filename = name;
2393 had_output_filename = true;
2398 static lang_output_section_statement_type *current_section;
2400 static int topower(x)
2405 if (x < 0) return -1;
2406 for (l = 0; l < 32; l++)
2408 if (i >= x) return l;
2414 lang_enter_output_section_statement (output_section_statement_name,
2415 address_exp, flags, block_value,
2416 align, subalign, ebase)
2417 const char *output_section_statement_name;
2418 etree_type * address_exp;
2420 bfd_vma block_value;
2422 etree_type *subalign;
2425 lang_output_section_statement_type *os;
2429 lang_output_section_statement_lookup (output_section_statement_name);
2433 /* Add this statement to tree */
2434 /* add_statement(lang_output_section_statement_enum,
2435 output_section_statement);*/
2436 /* Make next things chain into subchain of this */
2438 if (os->addr_tree ==
2439 (etree_type *) NULL)
2445 if (flags & SEC_NEVER_LOAD)
2449 os->block_value = block_value ? block_value : 1;
2450 stat_ptr = &os->children;
2452 os->subsection_alignment = topower(
2453 exp_get_value_int(subalign, -1,
2454 "subsection alignment",
2456 os->section_alignment = topower(
2457 exp_get_value_int(align, -1,
2458 "section alignment", 0));
2460 os->load_base = ebase;
2467 lang_output_statement_type *new =
2468 new_stat (lang_output_statement, stat_ptr);
2470 new->name = output_filename;
2473 /* Reset the current counters in the regions */
2475 reset_memory_regions ()
2477 lang_memory_region_type *p = lang_memory_region_list;
2479 for (p = lang_memory_region_list;
2480 p != (lang_memory_region_type *) NULL;
2483 p->old_length = (bfd_size_type) (p->current - p->origin);
2484 p->current = p->origin;
2491 lang_reasonable_defaults ();
2492 current_target = default_target;
2494 lang_for_each_statement (ldlang_open_output); /* Open the output file */
2496 ldemul_create_output_section_statements ();
2498 /* Add to the hash table all undefineds on the command line */
2499 lang_place_undefineds ();
2501 /* Create a bfd for each input file */
2502 current_target = default_target;
2503 lang_for_each_statement (open_input_bfds);
2505 /* Build all sets based on the information gathered from the input
2507 ldctor_build_sets ();
2509 /* Size up the common data */
2512 /* Run through the contours of the script and attatch input sections
2513 to the correct output sections
2515 map_input_to_output_sections (statement_list.head, (char *) NULL,
2516 (lang_output_section_statement_type *) NULL);
2519 /* Find any sections not attatched explicitly and handle them */
2520 lang_place_orphans ();
2522 ldemul_before_allocation ();
2524 /* Now run around and relax if we can */
2525 if (command_line.relax)
2527 /* First time round is a trial run to get the 'worst case'
2528 addresses of the objects if there was no relaxing. */
2529 lang_size_sections (statement_list.head,
2531 &(statement_list.head), 0, (bfd_vma) 0, false);
2534 reset_memory_regions ();
2536 /* Keep relaxing until bfd_relax_section gives up. */
2539 relax_again = false;
2541 /* Do all the assignments with our current guesses as to
2543 lang_do_assignments (statement_list.head,
2545 (fill_type) 0, (bfd_vma) 0);
2547 /* Perform another relax pass - this time we know where the
2548 globals are, so can make better guess. */
2549 lang_size_sections (statement_list.head,
2551 &(statement_list.head), 0, (bfd_vma) 0, true);
2553 while (relax_again);
2557 /* Size up the sections. */
2558 lang_size_sections (statement_list.head,
2560 &(statement_list.head), 0, (bfd_vma) 0, false);
2563 /* See if anything special should be done now we know how big
2565 ldemul_after_allocation ();
2567 /* Do all the assignments, now that we know the final restingplaces
2568 of all the symbols */
2570 lang_do_assignments (statement_list.head,
2572 (fill_type) 0, (bfd_vma) 0);
2574 /* Make sure that we're not mixing architectures */
2584 /* EXPORTED TO YACC */
2587 lang_add_wild (section_name, filename)
2588 CONST char *CONST section_name;
2589 CONST char *CONST filename;
2591 lang_wild_statement_type *new = new_stat (lang_wild_statement,
2594 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2596 placed_commons = true;
2598 if (filename != (char *) NULL)
2600 lang_has_input_file = true;
2602 new->section_name = section_name;
2603 new->filename = filename;
2604 lang_list_init (&new->children);
2608 lang_section_start (name, address)
2610 etree_type * address;
2612 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2614 ad->section_name = name;
2615 ad->address = address;
2618 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
2619 because of a -e argument on the command line, or zero if this is
2620 called by ENTRY in a linker script. Command line arguments take
2624 lang_add_entry (name, cmdline)
2628 static int from_cmdline;
2630 if (entry_symbol == NULL
2634 entry_symbol = name;
2635 from_cmdline = cmdline;
2640 lang_add_target (name)
2643 lang_target_statement_type *new = new_stat (lang_target_statement,
2659 map_option_f = true;
2670 lang_fill_statement_type *new = new_stat (lang_fill_statement,
2677 lang_add_data (type, exp)
2679 union etree_union *exp;
2682 lang_data_statement_type *new = new_stat (lang_data_statement,
2690 /* Create a new reloc statement. RELOC is the BFD relocation type to
2691 generate. HOWTO is the corresponding howto structure (we could
2692 look this up, but the caller has already done so). SECTION is the
2693 section to generate a reloc against, or NAME is the name of the
2694 symbol to generate a reloc against. Exactly one of SECTION and
2695 NAME must be NULL. ADDEND is an expression for the addend. */
2698 lang_add_reloc (reloc, howto, section, name, addend)
2699 bfd_reloc_code_real_type reloc;
2700 const reloc_howto_type *howto;
2703 union etree_union *addend;
2705 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
2709 p->section = section;
2711 p->addend_exp = addend;
2713 p->addend_value = 0;
2714 p->output_section = NULL;
2719 lang_add_assignment (exp)
2722 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
2729 lang_add_attribute (attribute)
2730 enum statement_enum attribute;
2732 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2739 if (startup_file != (char *) NULL)
2741 einfo ("%P%Fmultiple STARTUP files\n");
2743 first_file->filename = name;
2744 first_file->local_sym_name = name;
2745 first_file->real = true;
2747 startup_file = name;
2754 lang_float_flag = maybe;
2758 lang_leave_output_section_statement (fill, memspec)
2760 CONST char *memspec;
2762 current_section->fill = fill;
2763 current_section->region = lang_memory_region_lookup (memspec);
2764 stat_ptr = &statement_list;
2766 /* We remember if we are closing a .data section, since we use it to
2767 store constructors in */
2768 if (strcmp (current_section->name, ".data") == 0)
2770 end_of_data_section_statement_list = statement_list;
2776 Create an absolute symbol with the given name with the value of the
2777 address of first byte of the section named.
2779 If the symbol already exists, then do nothing.
2782 lang_abs_symbol_at_beginning_of (secname, name)
2783 const char *secname;
2786 struct bfd_link_hash_entry *h;
2788 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
2789 if (h == (struct bfd_link_hash_entry *) NULL)
2790 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2792 if (h->type == bfd_link_hash_new
2793 || h->type == bfd_link_hash_undefined)
2797 h->type = bfd_link_hash_defined;
2799 sec = bfd_get_section_by_name (output_bfd, secname);
2800 if (sec == (asection *) NULL)
2803 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
2805 h->u.def.section = &bfd_abs_section;
2810 Create an absolute symbol with the given name with the value of the
2811 address of the first byte after the end of the section named.
2813 If the symbol already exists, then do nothing.
2816 lang_abs_symbol_at_end_of (secname, name)
2817 const char *secname;
2820 struct bfd_link_hash_entry *h;
2822 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
2823 if (h == (struct bfd_link_hash_entry *) NULL)
2824 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
2826 if (h->type == bfd_link_hash_new
2827 || h->type == bfd_link_hash_undefined)
2831 h->type = bfd_link_hash_defined;
2833 sec = bfd_get_section_by_name (output_bfd, secname);
2834 if (sec == (asection *) NULL)
2837 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
2838 + bfd_section_size (output_bfd, sec));
2840 h->u.def.section = &bfd_abs_section;
2845 lang_statement_append (list, element, field)
2846 lang_statement_list_type * list;
2847 lang_statement_union_type * element;
2848 lang_statement_union_type ** field;
2850 *(list->tail) = element;
2854 /* Set the output format type. -oformat overrides scripts. */
2856 lang_add_output_format (format, from_script)
2860 if (output_target == NULL || !from_script)
2861 output_target = format;