1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 1995 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
52 static boolean placed_commons = false;
53 static lang_output_section_statement_type *default_common_section;
54 static boolean map_option_f;
55 static bfd_vma print_dot;
56 static lang_input_statement_type *first_file;
57 static lang_statement_list_type lang_output_section_statement;
58 static CONST char *current_target;
59 static CONST char *output_target;
60 static int longest_section_name = 8;
61 static lang_statement_list_type statement_list;
63 static void print_size PARAMS ((size_t value));
64 static void print_alignment PARAMS ((unsigned int value));
65 static void print_fill PARAMS ((fill_type value));
66 static void print_section PARAMS ((const char *name));
67 static void lang_for_each_statement_worker
68 PARAMS ((void (*func) (lang_statement_union_type *),
69 lang_statement_union_type *s));
70 static lang_input_statement_type *new_afile
71 PARAMS ((const char *name, lang_input_file_enum_type file_type,
72 const char *target, boolean add_to_list));
73 static void print_flags PARAMS ((int *ignore_flags));
74 static void init_os PARAMS ((lang_output_section_statement_type *s));
75 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
77 lang_input_statement_type *file,
78 lang_output_section_statement_type *output));
79 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
80 static void load_symbols PARAMS ((lang_input_statement_type *entry,
81 lang_statement_list_type *));
82 static void wild PARAMS ((lang_wild_statement_type *s,
83 const char *section, const char *file,
85 lang_output_section_statement_type *output));
86 static bfd *open_output PARAMS ((const char *name));
87 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
88 static void open_input_bfds
89 PARAMS ((lang_statement_union_type *statement, boolean));
90 static void lang_reasonable_defaults PARAMS ((void));
91 static void lang_place_undefineds PARAMS ((void));
92 static void map_input_to_output_sections
93 PARAMS ((lang_statement_union_type *s,
95 lang_output_section_statement_type *output_section_statement));
96 static void print_output_section_statement
97 PARAMS ((lang_output_section_statement_type *output_section_statement));
98 static void print_assignment
99 PARAMS ((lang_assignment_statement_type *assignment,
100 lang_output_section_statement_type *output_section));
101 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
102 static void print_input_section PARAMS ((lang_input_section_type *in));
103 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
104 static void print_data_statement PARAMS ((lang_data_statement_type *data));
105 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
106 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
107 static void print_wild_statement
108 PARAMS ((lang_wild_statement_type *w,
109 lang_output_section_statement_type *os));
110 static void print_group
111 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
112 static void print_statement PARAMS ((lang_statement_union_type *s,
113 lang_output_section_statement_type *os));
114 static void print_statements PARAMS ((void));
115 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
116 fill_type fill, unsigned int power,
117 asection *output_section_statement,
119 static bfd_vma size_input_section
120 PARAMS ((lang_statement_union_type **this_ptr,
121 lang_output_section_statement_type *output_section_statement,
122 fill_type fill, bfd_vma dot, boolean relax));
123 static void lang_finish PARAMS ((void));
124 static void lang_check PARAMS ((void));
125 static void lang_common PARAMS ((void));
126 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
127 static void lang_place_orphans PARAMS ((void));
128 static int topower PARAMS ((int));
129 static void lang_set_startof PARAMS ((void));
130 static void reset_memory_regions PARAMS ((void));
133 lang_output_section_statement_type *abs_output_section;
134 lang_statement_list_type *stat_ptr = &statement_list;
135 lang_statement_list_type file_chain = { 0 };
136 static const char *entry_symbol = 0;
137 boolean entry_from_cmdline;
138 boolean lang_has_input_file = false;
139 boolean had_output_filename = false;
140 boolean lang_float_flag = false;
141 boolean delete_output_file_on_failure = false;
143 etree_type *base; /* Relocation base - or null */
146 #if defined(__STDC__) || defined(ALMOST_STDC)
147 #define cat(a,b) a##b
149 #define cat(a,b) a/**/b
152 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
154 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
156 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
162 return obstack_alloc (&stat_obstack, size);
169 fprintf (config.map_file, "%5x", (unsigned) value);
173 print_alignment (value)
176 fprintf (config.map_file, "2**%1u", value);
183 fprintf (config.map_file, "%04x", (unsigned) value);
190 fprintf (config.map_file, "%*s", -longest_section_name, name);
193 /*----------------------------------------------------------------------
194 lang_for_each_statement walks the parse tree and calls the provided
195 function for each node
199 lang_for_each_statement_worker (func, s)
200 void (*func) PARAMS ((lang_statement_union_type *));
201 lang_statement_union_type *s;
203 for (; s != (lang_statement_union_type *) NULL; s = s->next)
207 switch (s->header.type)
209 case lang_constructors_statement_enum:
210 lang_for_each_statement_worker (func, constructor_list.head);
212 case lang_output_section_statement_enum:
213 lang_for_each_statement_worker
215 s->output_section_statement.children.head);
217 case lang_wild_statement_enum:
218 lang_for_each_statement_worker
220 s->wild_statement.children.head);
222 case lang_group_statement_enum:
223 lang_for_each_statement_worker (func,
224 s->group_statement.children.head);
226 case lang_data_statement_enum:
227 case lang_reloc_statement_enum:
228 case lang_object_symbols_statement_enum:
229 case lang_output_statement_enum:
230 case lang_target_statement_enum:
231 case lang_input_section_enum:
232 case lang_input_statement_enum:
233 case lang_assignment_statement_enum:
234 case lang_padding_statement_enum:
235 case lang_address_statement_enum:
245 lang_for_each_statement (func)
246 void (*func) PARAMS ((lang_statement_union_type *));
248 lang_for_each_statement_worker (func,
249 statement_list.head);
252 /*----------------------------------------------------------------------*/
254 lang_list_init (list)
255 lang_statement_list_type *list;
257 list->head = (lang_statement_union_type *) NULL;
258 list->tail = &list->head;
261 /*----------------------------------------------------------------------
263 build a new statement node for the parse tree
268 lang_statement_union_type *
269 new_statement (type, size, list)
270 enum statement_enum type;
272 lang_statement_list_type * list;
274 lang_statement_union_type *new = (lang_statement_union_type *)
277 new->header.type = type;
278 new->header.next = (lang_statement_union_type *) NULL;
279 lang_statement_append (list, new, &new->header.next);
284 Build a new input file node for the language. There are several ways
285 in which we treat an input file, eg, we only look at symbols, or
286 prefix it with a -l etc.
288 We can be supplied with requests for input files more than once;
289 they may, for example be split over serveral lines like foo.o(.text)
290 foo.o(.data) etc, so when asked for a file we check that we havn't
291 got it already so we don't duplicate the bfd.
294 static lang_input_statement_type *
295 new_afile (name, file_type, target, add_to_list)
297 lang_input_file_enum_type file_type;
301 lang_input_statement_type *p;
304 p = new_stat (lang_input_statement, stat_ptr);
307 p = ((lang_input_statement_type *)
308 stat_alloc (sizeof (lang_input_statement_type)));
309 p->header.next = NULL;
312 lang_has_input_file = true;
314 p->complained = false;
317 case lang_input_file_is_symbols_only_enum:
319 p->is_archive = false;
321 p->local_sym_name = name;
322 p->just_syms_flag = true;
323 p->search_dirs_flag = false;
325 case lang_input_file_is_fake_enum:
327 p->is_archive = false;
329 p->local_sym_name = name;
330 p->just_syms_flag = false;
331 p->search_dirs_flag = false;
333 case lang_input_file_is_l_enum:
334 p->is_archive = true;
337 p->local_sym_name = concat ("-l", name, (const char *) NULL);
338 p->just_syms_flag = false;
339 p->search_dirs_flag = true;
341 case lang_input_file_is_marker_enum:
343 p->is_archive = false;
345 p->local_sym_name = name;
346 p->just_syms_flag = false;
347 p->search_dirs_flag = true;
349 case lang_input_file_is_search_file_enum:
351 p->is_archive = false;
353 p->local_sym_name = name;
354 p->just_syms_flag = false;
355 p->search_dirs_flag = true;
357 case lang_input_file_is_file_enum:
359 p->is_archive = false;
361 p->local_sym_name = name;
362 p->just_syms_flag = false;
363 p->search_dirs_flag = false;
368 p->the_bfd = (bfd *) NULL;
369 p->asymbols = (asymbol **) NULL;
370 p->next_real_file = (lang_statement_union_type *) NULL;
371 p->next = (lang_statement_union_type *) NULL;
373 p->common_output_section = (asection *) NULL;
374 p->dynamic = config.dynamic_link;
376 lang_statement_append (&input_file_chain,
377 (lang_statement_union_type *) p,
382 lang_input_statement_type *
383 lang_add_input_file (name, file_type, target)
385 lang_input_file_enum_type file_type;
388 lang_has_input_file = true;
389 return new_afile (name, file_type, target, true);
392 /* Build enough state so that the parser can build its tree */
396 obstack_begin (&stat_obstack, 1000);
398 stat_ptr = &statement_list;
400 lang_list_init (stat_ptr);
402 lang_list_init (&input_file_chain);
403 lang_list_init (&lang_output_section_statement);
404 lang_list_init (&file_chain);
405 first_file = lang_add_input_file ((char *) NULL,
406 lang_input_file_is_marker_enum,
408 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
410 abs_output_section->bfd_section = bfd_abs_section_ptr;
414 /*----------------------------------------------------------------------
415 A region is an area of memory declared with the
416 MEMORY { name:org=exp, len=exp ... }
419 We maintain a list of all the regions here
421 If no regions are specified in the script, then the default is used
422 which is created when looked up to be the entire data space
425 static lang_memory_region_type *lang_memory_region_list;
426 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
428 lang_memory_region_type *
429 lang_memory_region_lookup (name)
430 CONST char *CONST name;
433 lang_memory_region_type *p = lang_memory_region_list;
435 for (p = lang_memory_region_list;
436 p != (lang_memory_region_type *) NULL;
439 if (strcmp (p->name, name) == 0)
446 /* This code used to always use the first region in the list as the
447 default region. I changed it to instead use a region
448 encompassing all of memory as the default region. This permits
449 NOLOAD sections to work reasonably without requiring a region.
450 People should specify what region they mean, if they really want
452 if (strcmp (name, "*default*") == 0)
454 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
456 return lang_memory_region_list;
462 lang_memory_region_type *new =
463 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
465 new->name = buystring (name);
466 new->next = (lang_memory_region_type *) NULL;
468 *lang_memory_region_list_tail = new;
469 lang_memory_region_list_tail = &new->next;
471 new->length = ~(bfd_size_type)0;
473 new->had_full_message = false;
480 lang_output_section_statement_type *
481 lang_output_section_find (name)
482 CONST char *CONST name;
484 lang_statement_union_type *u;
485 lang_output_section_statement_type *lookup;
487 for (u = lang_output_section_statement.head;
488 u != (lang_statement_union_type *) NULL;
491 lookup = &u->output_section_statement;
492 if (strcmp (name, lookup->name) == 0)
497 return (lang_output_section_statement_type *) NULL;
500 lang_output_section_statement_type *
501 lang_output_section_statement_lookup (name)
502 CONST char *CONST name;
504 lang_output_section_statement_type *lookup;
506 lookup = lang_output_section_find (name);
507 if (lookup == (lang_output_section_statement_type *) NULL)
510 lookup = (lang_output_section_statement_type *)
511 new_stat (lang_output_section_statement, stat_ptr);
512 lookup->region = (lang_memory_region_type *) NULL;
514 lookup->block_value = 1;
517 lookup->next = (lang_statement_union_type *) NULL;
518 lookup->bfd_section = (asection *) NULL;
519 lookup->processed = false;
520 lookup->loadable = 1;
521 lookup->addr_tree = (etree_type *) NULL;
522 lang_list_init (&lookup->children);
524 lookup->memspec = (CONST char *) NULL;
526 lookup->subsection_alignment = -1;
527 lookup->section_alignment = -1;
528 lookup->load_base = (union etree_union *) NULL;
530 lang_statement_append (&lang_output_section_statement,
531 (lang_statement_union_type *) lookup,
539 print_flags (ignore_flags)
542 fprintf (config.map_file, "(");
544 if (flags->flag_read)
545 fprintf (outfile, "R");
546 if (flags->flag_write)
547 fprintf (outfile, "W");
548 if (flags->flag_executable)
549 fprintf (outfile, "X");
550 if (flags->flag_loadable)
551 fprintf (outfile, "L");
553 fprintf (config.map_file, ")");
559 lang_memory_region_type *m;
561 fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
563 fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
565 fprintf (config.map_file,
566 "name\t\torigin length r_size c_size is attributes\n");
569 for (m = lang_memory_region_list;
570 m != (lang_memory_region_type *) NULL;
573 fprintf (config.map_file, "%-16s", m->name);
574 print_address (m->origin);
576 print_address ((bfd_vma)m->length);
578 print_address ((bfd_vma)m->old_length);
580 print_address (m->current - m->origin);
583 fprintf (config.map_file, " %2d%% ",
584 (int) ((m->current - m->origin) * 100 / m->old_length));
585 print_flags (&m->flags);
586 fprintf (config.map_file, "\n");
588 fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
589 fprintf (config.map_file, "output input virtual\n");
590 fprintf (config.map_file, "section section address tsize\n\n");
601 lang_output_section_statement_type * s;
603 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
604 section_userdata_type *new =
605 (section_userdata_type *)
606 stat_alloc (sizeof (section_userdata_type));
608 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
609 if (s->bfd_section == (asection *) NULL)
610 s->bfd_section = bfd_make_section (output_bfd, s->name);
611 if (s->bfd_section == (asection *) NULL)
613 einfo ("%P%F: output format %s cannot represent section called %s\n",
614 output_bfd->xvec->name, s->name);
616 s->bfd_section->output_section = s->bfd_section;
618 /* We initialize an output sections output offset to minus its own */
619 /* vma to allow us to output a section through itself */
620 s->bfd_section->output_offset = 0;
621 get_userdata (s->bfd_section) = (PTR) new;
625 /***********************************************************************
628 These expand statements like *(.text) and foo.o to a list of
629 explicit actions, like foo.o(.text), bar.o(.text) and
632 The toplevel routine, wild, takes a statement, section, file and
633 target. If either the section or file is null it is taken to be the
634 wildcard. Seperate lang_input_section statements are created for
635 each part of the expanstion, and placed after the statement provided.
640 wild_doit (ptr, section, output, file)
641 lang_statement_list_type * ptr;
643 lang_output_section_statement_type * output;
644 lang_input_statement_type * file;
646 if (output->bfd_section == (asection *) NULL)
649 if (section != (asection *) NULL
650 && section->output_section == (asection *) NULL)
652 /* Add a section reference to the list */
653 lang_input_section_type *new = new_stat (lang_input_section, ptr);
655 new->section = section;
657 section->output_section = output->bfd_section;
659 /* We don't copy the SEC_NEVER_LOAD flag from an input section to
660 an output section, because we want to be able to include a
661 SEC_NEVER_LOAD section in the middle of an otherwise loaded
662 section (I don't know why we want to do this, but we do).
663 build_link_order in ldwrite.c handles this case by turning the
664 embedded SEC_NEVER_LOAD section into a fill. */
665 section->output_section->flags |=
666 section->flags & (flagword) (~ SEC_NEVER_LOAD);
668 if (!output->loadable)
670 /* Turn off load flag */
671 output->bfd_section->flags &= ~SEC_LOAD;
672 output->bfd_section->flags |= SEC_NEVER_LOAD;
674 if (section->alignment_power > output->bfd_section->alignment_power)
676 output->bfd_section->alignment_power = section->alignment_power;
678 /* If supplied an aligmnet, then force it */
679 if (output->section_alignment != -1)
681 output->bfd_section->alignment_power = output->section_alignment;
687 wild_section (ptr, section, file, output)
688 lang_wild_statement_type *ptr;
690 lang_input_statement_type *file;
691 lang_output_section_statement_type *output;
693 if (file->just_syms_flag == false)
695 register asection *s;
697 for (s = file->the_bfd->sections; s != NULL; s = s->next)
699 /* Attach all sections named SECTION. If SECTION is NULL,
700 then attach all sections.
702 Previously, if SECTION was NULL, this code did not call
703 wild_doit if the SEC_IS_COMMON flag was set for the
704 section. I did not understand that, and I took it out.
708 || strcmp (bfd_get_section_name (file->the_bfd, s),
710 wild_doit (&ptr->children, s, output, file);
715 /* passed a file name (which must have been seen already and added to
716 the statement tree. We will see if it has been opened already and
717 had its symbols read. If not then we'll read it.
719 Archives are pecuilar here. We may open them once, but if they do
720 not define anything we need at the time, they won't have all their
721 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, (lang_statement_list_type *) NULL);
757 /* Get the symbols for an input file. */
760 load_symbols (entry, place)
761 lang_input_statement_type *entry;
762 lang_statement_list_type *place;
769 ldfile_open_file (entry);
771 if (! bfd_check_format (entry->the_bfd, bfd_archive)
772 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
775 lang_statement_list_type *hold;
777 err = bfd_get_error ();
778 if (err == bfd_error_file_ambiguously_recognized)
782 einfo ("%B: file not recognized: %E\n", entry->the_bfd);
783 einfo ("%B: matching formats:", entry->the_bfd);
784 for (p = matching; *p != NULL; p++)
788 else if (err != bfd_error_file_not_recognized
790 einfo ("%F%B: file not recognized: %E\n", entry->the_bfd);
792 /* Try to interpret the file as a linker script. */
794 bfd_close (entry->the_bfd);
795 entry->the_bfd = NULL;
797 ldfile_open_command_file (entry->filename);
802 ldfile_assumed_script = true;
803 parser_input = input_script;
805 ldfile_assumed_script = false;
812 /* We don't call ldlang_add_file for an archive. Instead, the
813 add_symbols entry point will call ldlang_add_file, via the
814 add_archive_element callback, for each element of the archive
816 switch (bfd_get_format (entry->the_bfd))
822 ldlang_add_file (entry);
823 if (trace_files || trace_file_tries)
824 info_msg ("%I\n", entry);
830 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
832 while (member != NULL)
834 if (! bfd_check_format (member, bfd_object))
835 einfo ("%F%B: object %B in archive is not object\n",
836 entry->the_bfd, member);
837 if (! ((*link_info.callbacks->add_archive_element)
838 (&link_info, member, "-whole-archive")))
840 if (! bfd_link_add_symbols (member, &link_info))
841 einfo ("%F%B: could not read symbols: %E\n", member);
842 member = bfd_openr_next_archived_file (entry->the_bfd,
846 entry->loaded = true;
852 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
853 einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd);
855 entry->loaded = true;
859 wild (s, section, file, target, output)
860 lang_wild_statement_type * s;
864 lang_output_section_statement_type * output;
866 lang_input_statement_type *f;
868 if (file == (char *) NULL)
870 /* Perform the iteration over all files in the list */
871 for (f = (lang_input_statement_type *) file_chain.head;
872 f != (lang_input_statement_type *) NULL;
873 f = (lang_input_statement_type *) f->next)
875 wild_section (s, section, f, output);
880 /* Perform the iteration over a single file */
881 wild_section (s, section, lookup_name (file), output);
883 if (section != (char *) NULL
884 && strcmp (section, "COMMON") == 0
885 && default_common_section == (lang_output_section_statement_type *) NULL)
887 /* Remember the section that common is going to incase we later
888 get something which doesn't know where to put it */
889 default_common_section = output;
894 read in all the files
903 if (output_target == (char *) NULL)
905 if (current_target != (char *) NULL)
906 output_target = current_target;
908 output_target = default_target;
910 output = bfd_openw (name, output_target);
912 if (output == (bfd *) NULL)
914 if (bfd_get_error () == bfd_error_invalid_target)
916 einfo ("%P%F: target %s not found\n", output_target);
918 einfo ("%P%F: cannot open output file %s: %E\n", name);
921 delete_output_file_on_failure = 1;
923 /* output->flags |= D_PAGED;*/
925 if (! bfd_set_format (output, bfd_object))
926 einfo ("%P%F:%s: can not make object file: %E\n", name);
927 if (! bfd_set_arch_mach (output,
928 ldfile_output_architecture,
929 ldfile_output_machine))
930 einfo ("%P%F:%s: can not set architecture: %E\n", name);
932 link_info.hash = bfd_link_hash_table_create (output);
933 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
934 einfo ("%P%F: can not create link hash table: %E\n");
936 bfd_set_gp_size (output, g_switch_value);
944 ldlang_open_output (statement)
945 lang_statement_union_type * statement;
947 switch (statement->header.type)
949 case lang_output_statement_enum:
950 ASSERT (output_bfd == (bfd *) NULL);
951 output_bfd = open_output (statement->output_statement.name);
952 ldemul_set_output_arch ();
953 if (config.magic_demand_paged && !link_info.relocateable)
954 output_bfd->flags |= D_PAGED;
956 output_bfd->flags &= ~D_PAGED;
957 if (config.text_read_only)
958 output_bfd->flags |= WP_TEXT;
960 output_bfd->flags &= ~WP_TEXT;
961 if (config.traditional_format)
962 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
964 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
967 case lang_target_statement_enum:
968 current_target = statement->target_statement.target;
975 /* Open all the input files. */
978 open_input_bfds (s, force)
979 lang_statement_union_type *s;
982 for (; s != (lang_statement_union_type *) NULL; s = s->next)
984 switch (s->header.type)
986 case lang_constructors_statement_enum:
987 open_input_bfds (constructor_list.head, force);
989 case lang_output_section_statement_enum:
990 open_input_bfds (s->output_section_statement.children.head, force);
992 case lang_wild_statement_enum:
993 /* Maybe we should load the file's symbols */
994 if (s->wild_statement.filename)
995 (void) lookup_name (s->wild_statement.filename);
996 open_input_bfds (s->wild_statement.children.head, force);
998 case lang_group_statement_enum:
1000 struct bfd_link_hash_entry *undefs;
1002 /* We must continually search the entries in the group
1003 until no new symbols are added to the list of undefined
1008 undefs = link_info.hash->undefs_tail;
1009 open_input_bfds (s->group_statement.children.head, true);
1011 while (undefs != link_info.hash->undefs_tail);
1014 case lang_target_statement_enum:
1015 current_target = s->target_statement.target;
1017 case lang_input_statement_enum:
1018 if (s->input_statement.real == true)
1020 lang_statement_list_type add;
1022 s->input_statement.target = current_target;
1024 /* If we are being called from within a group, and this
1025 is an archive which has already been searched, then
1026 force it to be researched. */
1028 && s->input_statement.loaded
1029 && bfd_check_format (s->input_statement.the_bfd,
1031 s->input_statement.loaded = false;
1033 lang_list_init (&add);
1035 load_symbols (&s->input_statement, &add);
1037 if (add.head != NULL)
1039 *add.tail = s->next;
1050 /* If there are [COMMONS] statements, put a wild one into the bss section */
1053 lang_reasonable_defaults ()
1056 lang_output_section_statement_lookup (".text");
1057 lang_output_section_statement_lookup (".data");
1059 default_common_section =
1060 lang_output_section_statement_lookup (".bss");
1063 if (placed_commons == false)
1065 lang_wild_statement_type *new =
1066 new_stat (lang_wild_statement,
1067 &default_common_section->children);
1069 new->section_name = "COMMON";
1070 new->filename = (char *) NULL;
1071 lang_list_init (&new->children);
1078 Add the supplied name to the symbol table as an undefined reference.
1079 Remove items from the chain as we open input bfds
1081 typedef struct ldlang_undef_chain_list
1083 struct ldlang_undef_chain_list *next;
1085 } ldlang_undef_chain_list_type;
1087 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1090 ldlang_add_undef (name)
1091 CONST char *CONST name;
1093 ldlang_undef_chain_list_type *new =
1094 ((ldlang_undef_chain_list_type *)
1095 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1097 new->next = ldlang_undef_chain_list_head;
1098 ldlang_undef_chain_list_head = new;
1100 new->name = buystring (name);
1103 /* Run through the list of undefineds created above and place them
1104 into the linker hash table as undefined symbols belonging to the
1108 lang_place_undefineds ()
1110 ldlang_undef_chain_list_type *ptr;
1112 for (ptr = ldlang_undef_chain_list_head;
1113 ptr != (ldlang_undef_chain_list_type *) NULL;
1116 struct bfd_link_hash_entry *h;
1118 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1119 if (h == (struct bfd_link_hash_entry *) NULL)
1120 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1121 if (h->type == bfd_link_hash_new)
1123 h->type = bfd_link_hash_undefined;
1124 h->u.undef.abfd = NULL;
1125 bfd_link_add_undef (link_info.hash, h);
1130 /* Open input files and attatch to output sections */
1132 map_input_to_output_sections (s, target, output_section_statement)
1133 lang_statement_union_type * s;
1135 lang_output_section_statement_type * output_section_statement;
1137 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1139 switch (s->header.type)
1143 case lang_wild_statement_enum:
1144 wild (&s->wild_statement, s->wild_statement.section_name,
1145 s->wild_statement.filename, target,
1146 output_section_statement);
1149 case lang_constructors_statement_enum:
1150 map_input_to_output_sections (constructor_list.head,
1152 output_section_statement);
1154 case lang_output_section_statement_enum:
1155 map_input_to_output_sections (s->output_section_statement.children.head,
1157 &s->output_section_statement);
1159 case lang_output_statement_enum:
1161 case lang_target_statement_enum:
1162 target = s->target_statement.target;
1164 case lang_group_statement_enum:
1165 map_input_to_output_sections (s->group_statement.children.head,
1167 output_section_statement);
1169 case lang_fill_statement_enum:
1170 case lang_input_section_enum:
1171 case lang_object_symbols_statement_enum:
1172 case lang_data_statement_enum:
1173 case lang_reloc_statement_enum:
1174 case lang_assignment_statement_enum:
1175 case lang_padding_statement_enum:
1176 case lang_input_statement_enum:
1177 if (output_section_statement != NULL
1178 && output_section_statement->bfd_section == NULL)
1179 init_os (output_section_statement);
1181 case lang_afile_asection_pair_statement_enum:
1184 case lang_address_statement_enum:
1185 /* Mark the specified section with the supplied address */
1187 lang_output_section_statement_type *os =
1188 lang_output_section_statement_lookup
1189 (s->address_statement.section_name);
1191 if (os->bfd_section == NULL)
1193 os->addr_tree = s->address_statement.address;
1201 print_output_section_statement (output_section_statement)
1202 lang_output_section_statement_type * output_section_statement;
1204 asection *section = output_section_statement->bfd_section;
1207 print_section (output_section_statement->name);
1212 print_dot = section->vma;
1216 print_address (section->vma);
1218 print_size (section->_raw_size);
1220 print_size(section->_cooked_size);
1222 print_alignment (section->alignment_power);
1225 fprintf (config.map_file, "%s flags", output_section_statement->region->name);
1226 print_flags (stdout, &output_section_statement->flags);
1228 if (section->flags & SEC_LOAD)
1229 fprintf (config.map_file, "load ");
1230 if (section->flags & SEC_ALLOC)
1231 fprintf (config.map_file, "alloc ");
1232 if (section->flags & SEC_RELOC)
1233 fprintf (config.map_file, "reloc ");
1234 if (section->flags & SEC_HAS_CONTENTS)
1235 fprintf (config.map_file, "contents ");
1240 fprintf (config.map_file, " (no attached output section)");
1243 if (output_section_statement->load_base)
1245 bfd_vma b = exp_get_abs_int(output_section_statement->load_base,
1246 0, "output base", lang_final_phase_enum);
1247 fprintf (config.map_file, "Output address ");
1248 fprintf_vma (config.map_file, b);
1249 fprintf (config.map_file, "\n");
1251 if (output_section_statement->section_alignment >= 0
1252 || output_section_statement->subsection_alignment >= 0)
1254 fprintf (config.map_file, "\t\t\t\t\tforced alignment ");
1255 if (output_section_statement->section_alignment >= 0)
1257 fprintf (config.map_file, "section 2**%d ",output_section_statement->section_alignment );
1259 if ( output_section_statement->subsection_alignment >= 0)
1261 fprintf (config.map_file, "subsection 2**%d ",output_section_statement->subsection_alignment );
1266 print_statement (output_section_statement->children.head,
1267 output_section_statement);
1272 print_assignment (assignment, output_section)
1273 lang_assignment_statement_type * assignment;
1274 lang_output_section_statement_type * output_section;
1276 etree_value_type result;
1282 print_address (print_dot);
1284 result = exp_fold_tree (assignment->exp->assign.src,
1286 lang_final_phase_enum,
1292 print_address (result.value);
1296 fprintf (config.map_file, "*undefined*");
1299 exp_print_tree (assignment->exp);
1301 fprintf (config.map_file, "\n");
1305 print_input_statement (statm)
1306 lang_input_statement_type * statm;
1308 if (statm->filename != (char *) NULL)
1310 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1314 /* Print all the defined symbols for the abfd provided by in the supplied
1319 print_one_symbol (hash_entry, ptr)
1320 struct bfd_link_hash_entry *hash_entry;
1323 asection * sec = (asection *)ptr;
1325 if (hash_entry->type == bfd_link_hash_defined
1326 || hash_entry->type == bfd_link_hash_defweak)
1328 if (sec == hash_entry->u.def.section) {
1330 fprintf (config.map_file, " ");
1332 fprintf (config.map_file, " ");
1333 print_address (hash_entry->u.def.value + outside_section_address (sec));
1334 fprintf (config.map_file, " %s", hash_entry->root.string);
1343 print_input_section (in)
1344 lang_input_section_type * in;
1346 asection *i = in->section;
1347 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1352 fprintf (config.map_file, " ");
1353 print_section (i->name);
1354 fprintf (config.map_file, " ");
1355 if (i->output_section)
1357 print_address (i->output_section->vma + i->output_offset);
1358 fprintf (config.map_file, " ");
1359 print_size (i->_raw_size);
1360 fprintf (config.map_file, " ");
1361 print_size(i->_cooked_size);
1362 fprintf (config.map_file, " ");
1363 print_alignment (i->alignment_power);
1364 fprintf (config.map_file, " ");
1368 bfd *abfd = in->ifile->the_bfd;
1370 if (in->ifile->just_syms_flag == true)
1372 fprintf (config.map_file, "symbols only ");
1375 fprintf (config.map_file, " %s ", abfd->xvec->name);
1376 if (abfd->my_archive != (bfd *) NULL)
1378 fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
1383 fprintf (config.map_file, "%s", abfd->filename);
1385 fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
1388 /* Print all the symbols */
1389 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1397 print_dot = outside_section_address (i) + size;
1401 fprintf (config.map_file, "No output section allocated\n");
1407 print_fill_statement (fill)
1408 lang_fill_statement_type * fill;
1410 fprintf (config.map_file, "FILL mask ");
1411 print_fill (fill->fill);
1415 print_data_statement (data)
1416 lang_data_statement_type * data;
1418 /* bfd_vma value; */
1423 /* ASSERT(print_dot == data->output_vma);*/
1425 print_address (data->output_vma + data->output_section->vma);
1427 print_address (data->value);
1432 fprintf (config.map_file, "BYTE ");
1433 print_dot += BYTE_SIZE;
1436 fprintf (config.map_file, "SHORT ");
1437 print_dot += SHORT_SIZE;
1440 fprintf (config.map_file, "LONG ");
1441 print_dot += LONG_SIZE;
1444 fprintf (config.map_file, "QUAD ");
1445 print_dot += QUAD_SIZE;
1449 exp_print_tree (data->exp);
1451 fprintf (config.map_file, "\n");
1454 /* Print a reloc statement. */
1457 print_reloc_statement (reloc)
1458 lang_reloc_statement_type *reloc;
1465 /* ASSERT(print_dot == data->output_vma);*/
1467 print_address (reloc->output_vma + reloc->output_section->vma);
1469 print_address (reloc->addend_value);
1472 fprintf (config.map_file, "RELOC %s ", reloc->howto->name);
1474 print_dot += bfd_get_reloc_size (reloc->howto);
1476 exp_print_tree (reloc->addend_exp);
1478 fprintf (config.map_file, "\n");
1482 print_padding_statement (s)
1483 lang_padding_statement_type * s;
1487 print_section ("*fill*");
1489 print_address (s->output_offset + s->output_section->vma);
1491 print_size (s->size);
1493 print_fill (s->fill);
1496 print_dot = s->output_offset + s->output_section->vma + s->size;
1501 print_wild_statement (w, os)
1502 lang_wild_statement_type * w;
1503 lang_output_section_statement_type * os;
1505 fprintf (config.map_file, " from ");
1506 if (w->filename != (char *) NULL)
1508 fprintf (config.map_file, "%s", w->filename);
1512 fprintf (config.map_file, "*");
1514 if (w->section_name != (char *) NULL)
1516 fprintf (config.map_file, "(%s)", w->section_name);
1520 fprintf (config.map_file, "(*)");
1523 print_statement (w->children.head, os);
1527 /* Print a group statement. */
1531 lang_group_statement_type *s;
1532 lang_output_section_statement_type *os;
1534 fprintf (config.map_file, "START GROUP\n");
1535 print_statement (s->children.head, os);
1536 fprintf (config.map_file, "END GROUP\n");
1540 print_statement (s, os)
1541 lang_statement_union_type * s;
1542 lang_output_section_statement_type * os;
1546 switch (s->header.type)
1548 case lang_constructors_statement_enum:
1549 fprintf (config.map_file, "constructors:\n");
1550 print_statement (constructor_list.head, os);
1552 case lang_wild_statement_enum:
1553 print_wild_statement (&s->wild_statement, os);
1556 fprintf (config.map_file, "Fail with %d\n", s->header.type);
1559 case lang_address_statement_enum:
1560 fprintf (config.map_file, "address\n");
1562 case lang_object_symbols_statement_enum:
1563 fprintf (config.map_file, "object symbols\n");
1565 case lang_fill_statement_enum:
1566 print_fill_statement (&s->fill_statement);
1568 case lang_data_statement_enum:
1569 print_data_statement (&s->data_statement);
1571 case lang_reloc_statement_enum:
1572 print_reloc_statement (&s->reloc_statement);
1574 case lang_input_section_enum:
1575 print_input_section (&s->input_section);
1577 case lang_padding_statement_enum:
1578 print_padding_statement (&s->padding_statement);
1580 case lang_output_section_statement_enum:
1581 print_output_section_statement (&s->output_section_statement);
1583 case lang_assignment_statement_enum:
1584 print_assignment (&s->assignment_statement,
1587 case lang_target_statement_enum:
1588 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1590 case lang_output_statement_enum:
1591 fprintf (config.map_file, "OUTPUT(%s %s)\n",
1592 s->output_statement.name,
1593 output_target ? output_target : "");
1595 case lang_input_statement_enum:
1596 print_input_statement (&s->input_statement);
1598 case lang_group_statement_enum:
1599 print_group (&s->group_statement, os);
1601 case lang_afile_asection_pair_statement_enum:
1613 print_statement (statement_list.head,
1614 abs_output_section);
1619 insert_pad (this_ptr, fill, power, output_section_statement, dot)
1620 lang_statement_union_type ** this_ptr;
1623 asection * output_section_statement;
1626 /* Align this section first to the
1627 input sections requirement, then
1628 to the output section's requirement.
1629 If this alignment is > than any seen before,
1630 then record it too. Perform the alignment by
1631 inserting a magic 'padding' statement.
1634 unsigned int alignment_needed = align_power (dot, power) - dot;
1636 if (alignment_needed != 0)
1638 lang_statement_union_type *new =
1639 ((lang_statement_union_type *)
1640 stat_alloc (sizeof (lang_padding_statement_type)));
1642 /* Link into existing chain */
1643 new->header.next = *this_ptr;
1645 new->header.type = lang_padding_statement_enum;
1646 new->padding_statement.output_section = output_section_statement;
1647 new->padding_statement.output_offset =
1648 dot - output_section_statement->vma;
1649 new->padding_statement.fill = fill;
1650 new->padding_statement.size = alignment_needed;
1654 /* Remember the most restrictive alignment */
1655 if (power > output_section_statement->alignment_power)
1657 output_section_statement->alignment_power = power;
1659 output_section_statement->_raw_size += alignment_needed;
1660 return alignment_needed + dot;
1664 /* Work out how much this section will move the dot point */
1666 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
1667 lang_statement_union_type ** this_ptr;
1668 lang_output_section_statement_type * output_section_statement;
1673 lang_input_section_type *is = &((*this_ptr)->input_section);
1674 asection *i = is->section;
1676 if (is->ifile->just_syms_flag == false)
1678 if (output_section_statement->subsection_alignment != -1)
1679 i->alignment_power =
1680 output_section_statement->subsection_alignment;
1682 dot = insert_pad (this_ptr, fill, i->alignment_power,
1683 output_section_statement->bfd_section, dot);
1685 /* Remember where in the output section this input section goes */
1687 i->output_offset = dot - output_section_statement->bfd_section->vma;
1689 /* Mark how big the output section must be to contain this now
1691 if (i->_cooked_size != 0)
1692 dot += i->_cooked_size;
1694 dot += i->_raw_size;
1695 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
1699 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1705 /* This variable indicates whether bfd_relax_section should be called
1708 static boolean relax_again;
1710 /* Set the sizes for all the output sections. */
1713 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
1714 lang_statement_union_type * s;
1715 lang_output_section_statement_type * output_section_statement;
1716 lang_statement_union_type ** prev;
1721 /* Size up the sections from their constituent parts */
1722 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1724 switch (s->header.type)
1727 case lang_output_section_statement_enum:
1730 lang_output_section_statement_type *os = &s->output_section_statement;
1732 if (os->bfd_section == NULL)
1734 /* This section was never actually created. */
1738 /* If this is a COFF shared library section, use the size and
1739 address from the input section. FIXME: This is COFF
1740 specific; it would be cleaner if there were some other way
1741 to do this, but nothing simple comes to mind. */
1742 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
1746 if (os->children.head == NULL
1747 || os->children.head->next != NULL
1748 || os->children.head->header.type != lang_input_section_enum)
1749 einfo ("%P%X: Internal error on COFF shared library section %s",
1752 input = os->children.head->input_section.section;
1753 bfd_set_section_vma (os->bfd_section->owner,
1755 bfd_section_vma (input->owner, input));
1756 os->bfd_section->_raw_size = input->_raw_size;
1760 if (bfd_is_abs_section (os->bfd_section))
1762 /* No matter what happens, an abs section starts at zero */
1763 ASSERT (os->bfd_section->vma == 0);
1767 if (os->addr_tree == (etree_type *) NULL)
1769 /* No address specified for this section, get one
1770 from the region specification
1772 if (os->region == (lang_memory_region_type *) NULL)
1774 os->region = lang_memory_region_lookup ("*default*");
1776 dot = os->region->current;
1777 if (os->section_alignment == -1)
1778 dot = align_power (dot, os->bfd_section->alignment_power);
1784 r = exp_fold_tree (os->addr_tree,
1786 lang_allocating_phase_enum,
1788 if (r.valid == false)
1790 einfo ("%F%S: non constant address expression for section %s\n",
1795 /* The section starts here */
1796 /* First, align to what the section needs */
1798 if (os->section_alignment != -1)
1799 dot = align_power (dot, os->section_alignment);
1801 bfd_set_section_vma (0, os->bfd_section, dot);
1803 os->bfd_section->output_offset = 0;
1806 (void) lang_size_sections (os->children.head, os, &os->children.head,
1807 os->fill, dot, relax);
1808 /* Ignore the size of the input sections, use the vma and size to */
1811 after = ALIGN_N (os->bfd_section->vma +
1812 os->bfd_section->_raw_size,
1813 /* The coercion here is important, see ld.h. */
1814 (bfd_vma) os->block_value);
1816 if (bfd_is_abs_section (os->bfd_section))
1817 ASSERT (after == os->bfd_section->vma);
1819 os->bfd_section->_raw_size = after - os->bfd_section->vma;
1820 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1821 os->processed = true;
1823 /* Replace into region ? */
1824 if (os->region != (lang_memory_region_type *) NULL)
1826 os->region->current = dot;
1827 /* Make sure this isn't silly. */
1828 if ((os->region->current < os->region->origin)
1829 || (os->region->current
1830 > os->region->origin + os->region->length))
1832 if (os->addr_tree != (etree_type *) NULL)
1834 einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
1835 os->region->current,
1836 os->bfd_section->owner,
1837 os->bfd_section->name,
1842 einfo ("%X%P: region %s is full (%B section %s)\n",
1844 os->bfd_section->owner,
1845 os->bfd_section->name);
1847 /* Reset the region pointer. */
1848 os->region->current = os->region->origin;
1854 case lang_constructors_statement_enum:
1855 dot = lang_size_sections (constructor_list.head,
1856 output_section_statement,
1857 &s->wild_statement.children.head,
1862 case lang_data_statement_enum:
1864 unsigned int size = 0;
1866 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1867 s->data_statement.output_section =
1868 output_section_statement->bfd_section;
1870 switch (s->data_statement.type)
1887 output_section_statement->bfd_section->_raw_size += size;
1888 /* The output section gets contents, and then we inspect for
1889 any flags set in the input script which override any ALLOC */
1890 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
1891 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
1892 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
1897 case lang_reloc_statement_enum:
1901 s->reloc_statement.output_vma =
1902 dot - output_section_statement->bfd_section->vma;
1903 s->reloc_statement.output_section =
1904 output_section_statement->bfd_section;
1905 size = bfd_get_reloc_size (s->reloc_statement.howto);
1907 output_section_statement->bfd_section->_raw_size += size;
1911 case lang_wild_statement_enum:
1913 dot = lang_size_sections (s->wild_statement.children.head,
1914 output_section_statement,
1915 &s->wild_statement.children.head,
1921 case lang_object_symbols_statement_enum:
1922 link_info.create_object_symbols_section =
1923 output_section_statement->bfd_section;
1925 case lang_output_statement_enum:
1926 case lang_target_statement_enum:
1928 case lang_input_section_enum:
1932 i = (*prev)->input_section.section;
1934 i->_cooked_size = i->_raw_size;
1939 if (! bfd_relax_section (i->owner, i, &link_info, &again))
1940 einfo ("%P%F: can't relax section: %E\n");
1944 dot = size_input_section (prev,
1945 output_section_statement,
1946 output_section_statement->fill,
1950 case lang_input_statement_enum:
1952 case lang_fill_statement_enum:
1953 s->fill_statement.output_section = output_section_statement->bfd_section;
1955 fill = s->fill_statement.fill;
1957 case lang_assignment_statement_enum:
1959 bfd_vma newdot = dot;
1961 exp_fold_tree (s->assignment_statement.exp,
1962 output_section_statement,
1963 lang_allocating_phase_enum,
1967 if (newdot != dot && !relax)
1969 /* The assignment changed dot. Insert a pad. */
1970 if (output_section_statement == abs_output_section)
1972 /* If we don't have an output section, then just adjust
1973 the default memory address. */
1974 lang_memory_region_lookup ("*default*")->current = newdot;
1978 lang_statement_union_type *new =
1979 ((lang_statement_union_type *)
1980 stat_alloc (sizeof (lang_padding_statement_type)));
1982 /* Link into existing chain */
1983 new->header.next = *prev;
1985 new->header.type = lang_padding_statement_enum;
1986 new->padding_statement.output_section =
1987 output_section_statement->bfd_section;
1988 new->padding_statement.output_offset =
1989 dot - output_section_statement->bfd_section->vma;
1990 new->padding_statement.fill = fill;
1991 new->padding_statement.size = newdot - dot;
1992 output_section_statement->bfd_section->_raw_size +=
1993 new->padding_statement.size;
2001 case lang_padding_statement_enum:
2002 /* If we are relaxing, and this is not the first pass, some
2003 padding statements may have been inserted during previous
2004 passes. We may have to move the padding statement to a new
2005 location if dot has a different value at this point in this
2006 pass than it did at this point in the previous pass. */
2007 s->padding_statement.output_offset =
2008 dot - output_section_statement->bfd_section->vma;
2009 dot += s->padding_statement.size;
2012 case lang_group_statement_enum:
2013 dot = lang_size_sections (s->group_statement.children.head,
2014 output_section_statement,
2015 &s->group_statement.children.head,
2023 /* This can only get here when relaxing is turned on */
2025 case lang_address_statement_enum:
2028 prev = &s->header.next;
2034 lang_do_assignments (s, output_section_statement, fill, dot)
2035 lang_statement_union_type * s;
2036 lang_output_section_statement_type * output_section_statement;
2040 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2042 switch (s->header.type)
2044 case lang_constructors_statement_enum:
2045 dot = lang_do_assignments (constructor_list.head,
2046 output_section_statement,
2051 case lang_output_section_statement_enum:
2053 lang_output_section_statement_type *os =
2054 &(s->output_section_statement);
2056 if (os->bfd_section != NULL)
2058 dot = os->bfd_section->vma;
2059 (void) lang_do_assignments (os->children.head, os,
2061 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2065 /* If nothing has been placed into the output section then
2066 it won't have a bfd_section. */
2067 if (os->bfd_section)
2069 os->bfd_section->lma
2070 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2075 case lang_wild_statement_enum:
2077 dot = lang_do_assignments (s->wild_statement.children.head,
2078 output_section_statement,
2083 case lang_object_symbols_statement_enum:
2084 case lang_output_statement_enum:
2085 case lang_target_statement_enum:
2087 case lang_common_statement_enum:
2090 case lang_data_statement_enum:
2092 etree_value_type value;
2094 value = exp_fold_tree (s->data_statement.exp,
2096 lang_final_phase_enum, dot, &dot);
2097 s->data_statement.value = value.value;
2098 if (value.valid == false)
2099 einfo ("%F%P: invalid data statement\n");
2101 switch (s->data_statement.type)
2118 case lang_reloc_statement_enum:
2120 etree_value_type value;
2122 value = exp_fold_tree (s->reloc_statement.addend_exp,
2124 lang_final_phase_enum, dot, &dot);
2125 s->reloc_statement.addend_value = value.value;
2126 if (value.valid == false)
2127 einfo ("%F%P: invalid reloc statement\n");
2129 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2132 case lang_input_section_enum:
2134 asection *in = s->input_section.section;
2136 if (in->_cooked_size != 0)
2137 dot += in->_cooked_size;
2139 dot += in->_raw_size;
2143 case lang_input_statement_enum:
2145 case lang_fill_statement_enum:
2146 fill = s->fill_statement.fill;
2148 case lang_assignment_statement_enum:
2150 exp_fold_tree (s->assignment_statement.exp,
2151 output_section_statement,
2152 lang_final_phase_enum,
2158 case lang_padding_statement_enum:
2159 dot += s->padding_statement.size;
2162 case lang_group_statement_enum:
2163 dot = lang_do_assignments (s->group_statement.children.head,
2164 output_section_statement,
2172 case lang_address_statement_enum:
2180 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2181 operator .startof. (section_name), it produces an undefined symbol
2182 .startof.section_name. Similarly, when it sees
2183 .sizeof. (section_name), it produces an undefined symbol
2184 .sizeof.section_name. For all the output sections, we look for
2185 such symbols, and set them to the correct value. */
2192 for (s = output_bfd->sections; s != NULL; s = s->next)
2194 const char *secname;
2196 struct bfd_link_hash_entry *h;
2198 secname = bfd_get_section_name (output_bfd, s);
2199 buf = xmalloc (10 + strlen (secname));
2201 sprintf (buf, ".startof.%s", secname);
2202 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2203 if (h != NULL && h->type == bfd_link_hash_undefined)
2205 h->type = bfd_link_hash_defined;
2206 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2207 h->u.def.section = bfd_abs_section_ptr;
2210 sprintf (buf, ".sizeof.%s", secname);
2211 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2212 if (h != NULL && h->type == bfd_link_hash_undefined)
2214 h->type = bfd_link_hash_defined;
2215 if (s->_cooked_size != 0)
2216 h->u.def.value = s->_cooked_size;
2218 h->u.def.value = s->_raw_size;
2219 h->u.def.section = bfd_abs_section_ptr;
2229 struct bfd_link_hash_entry *h;
2232 if (link_info.relocateable || link_info.shared)
2237 if (entry_symbol == (char *) NULL)
2239 /* No entry has been specified. Look for start, but don't warn
2240 if we don't find it. */
2241 entry_symbol = "start";
2245 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2246 if (h != (struct bfd_link_hash_entry *) NULL
2247 && (h->type == bfd_link_hash_defined
2248 || h->type == bfd_link_hash_defweak))
2252 val = (h->u.def.value
2253 + bfd_get_section_vma (output_bfd,
2254 h->u.def.section->output_section)
2255 + h->u.def.section->output_offset);
2256 if (! bfd_set_start_address (output_bfd, val))
2257 einfo ("%P%F:%s: can't set start address\n", entry_symbol);
2263 /* Can't find the entry symbol. Use the first address in the
2265 ts = bfd_get_section_by_name (output_bfd, ".text");
2266 if (ts != (asection *) NULL)
2269 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2270 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2271 if (! bfd_set_start_address (output_bfd,
2272 bfd_get_section_vma (output_bfd, ts)))
2273 einfo ("%P%F: can't set start address\n");
2278 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2284 /* Check that the architecture of all the input files is compatible
2285 with the output file. Also call the backend to let it do any
2286 other checking that is needed. */
2291 lang_statement_union_type *file;
2293 CONST bfd_arch_info_type *compatible;
2295 for (file = file_chain.head;
2296 file != (lang_statement_union_type *) NULL;
2297 file = file->input_statement.next)
2299 input_bfd = file->input_statement.the_bfd;
2300 compatible = bfd_arch_get_compatible (input_bfd,
2302 if (compatible == NULL)
2303 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2304 bfd_printable_name (input_bfd), input_bfd,
2305 bfd_printable_name (output_bfd));
2308 bfd_merge_private_bfd_data (input_bfd, output_bfd);
2312 /* Look through all the global common symbols and attach them to the
2313 correct section. The -sort-common command line switch may be used
2314 to roughly sort the entries by size. */
2319 if (link_info.relocateable
2320 && ! command_line.force_common_definition)
2323 if (! config.sort_common)
2324 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2329 for (power = 4; power >= 0; power--)
2330 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2335 /* Place one common symbol in the correct section. */
2338 lang_one_common (h, info)
2339 struct bfd_link_hash_entry *h;
2342 unsigned int power_of_two;
2346 if (h->type != bfd_link_hash_common)
2350 power_of_two = h->u.c.p->alignment_power;
2352 if (config.sort_common
2353 && power_of_two < (unsigned int) *(int *) info)
2356 section = h->u.c.p->section;
2358 /* Increase the size of the section. */
2359 section->_raw_size = ALIGN_N (section->_raw_size,
2360 (bfd_size_type) (1 << power_of_two));
2362 /* Adjust the alignment if necessary. */
2363 if (power_of_two > section->alignment_power)
2364 section->alignment_power = power_of_two;
2366 /* Change the symbol from common to defined. */
2367 h->type = bfd_link_hash_defined;
2368 h->u.def.section = section;
2369 h->u.def.value = section->_raw_size;
2371 /* Increase the size of the section. */
2372 section->_raw_size += size;
2374 /* Make sure the section is allocated in memory. */
2375 section->flags |= SEC_ALLOC;
2377 if (config.map_file != NULL)
2378 fprintf (config.map_file, "Allocating common %s: %lx at %lx %s\n",
2379 h->root.string, (unsigned long) size,
2380 (unsigned long) h->u.def.value, section->owner->filename);
2386 run through the input files and ensure that every input
2387 section has somewhere to go. If one is found without
2388 a destination then create an input request and place it
2389 into the statement tree.
2393 lang_place_orphans ()
2395 lang_input_statement_type *file;
2397 for (file = (lang_input_statement_type *) file_chain.head;
2398 file != (lang_input_statement_type *) NULL;
2399 file = (lang_input_statement_type *) file->next)
2403 for (s = file->the_bfd->sections;
2404 s != (asection *) NULL;
2407 if (s->output_section == (asection *) NULL)
2409 /* This section of the file is not attatched, root
2410 around for a sensible place for it to go */
2412 if (file->just_syms_flag)
2414 /* We are only retrieving symbol values from this
2415 file. We want the symbols to act as though the
2416 values in the file are absolute. */
2417 s->output_section = bfd_abs_section_ptr;
2418 s->output_offset = s->vma;
2420 else if (file->common_section == s)
2422 /* This is a lonely common section which must
2423 have come from an archive. We attatch to the
2424 section with the wildcard */
2425 if (! link_info.relocateable
2426 && ! command_line.force_common_definition)
2428 if (default_common_section ==
2429 (lang_output_section_statement_type *) NULL)
2431 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2433 default_common_section =
2434 lang_output_section_statement_lookup (".bss");
2437 wild_doit (&default_common_section->children, s,
2438 default_common_section, file);
2441 else if (ldemul_place_orphan (file, s))
2445 lang_output_section_statement_type *os =
2446 lang_output_section_statement_lookup (s->name);
2448 wild_doit (&os->children, s, os, file);
2457 lang_set_flags (ptr, flags)
2461 boolean state = false;
2476 /* ptr->flag_read = state; */
2479 /* ptr->flag_write = state; */
2482 /* ptr->flag_executable= state;*/
2486 /* ptr->flag_loadable= state;*/
2489 einfo ("%P%F: invalid syntax in flags\n");
2496 /* Call a function on each input file. This function will be called
2497 on an archive, but not on the elements. */
2500 lang_for_each_input_file (func)
2501 void (*func) PARAMS ((lang_input_statement_type *));
2503 lang_input_statement_type *f;
2505 for (f = (lang_input_statement_type *) input_file_chain.head;
2507 f = (lang_input_statement_type *) f->next_real_file)
2511 /* Call a function on each file. The function will be called on all
2512 the elements of an archive which are included in the link, but will
2513 not be called on the archive file itself. */
2516 lang_for_each_file (func)
2517 void (*func) PARAMS ((lang_input_statement_type *));
2519 lang_input_statement_type *f;
2521 for (f = (lang_input_statement_type *) file_chain.head;
2522 f != (lang_input_statement_type *) NULL;
2523 f = (lang_input_statement_type *) f->next)
2534 lang_for_each_input_section (func)
2535 void (*func) PARAMS ((bfd * ab, asection * as));
2537 lang_input_statement_type *f;
2539 for (f = (lang_input_statement_type *) file_chain.head;
2540 f != (lang_input_statement_type *) NULL;
2541 f = (lang_input_statement_type *) f->next)
2545 for (s = f->the_bfd->sections;
2546 s != (asection *) NULL;
2549 func (f->the_bfd, s);
2557 ldlang_add_file (entry)
2558 lang_input_statement_type * entry;
2562 lang_statement_append (&file_chain,
2563 (lang_statement_union_type *) entry,
2566 /* The BFD linker needs to have a list of all input BFDs involved in
2568 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
2569 ASSERT (entry->the_bfd != output_bfd);
2570 for (pp = &link_info.input_bfds;
2571 *pp != (bfd *) NULL;
2572 pp = &(*pp)->link_next)
2574 *pp = entry->the_bfd;
2575 entry->the_bfd->usrdata = (PTR) entry;
2576 bfd_set_gp_size (entry->the_bfd, g_switch_value);
2580 lang_add_output (name, from_script)
2584 /* Make -o on command line override OUTPUT in script. */
2585 if (had_output_filename == false || !from_script)
2587 output_filename = name;
2588 had_output_filename = true;
2593 static lang_output_section_statement_type *current_section;
2605 for (l = 0; l < 32; l++)
2607 if (i >= (unsigned int) x)
2615 lang_enter_output_section_statement (output_section_statement_name,
2616 address_exp, flags, block_value,
2617 align, subalign, ebase)
2618 const char *output_section_statement_name;
2619 etree_type * address_exp;
2621 bfd_vma block_value;
2623 etree_type *subalign;
2626 lang_output_section_statement_type *os;
2630 lang_output_section_statement_lookup (output_section_statement_name);
2634 /* Add this statement to tree */
2635 /* add_statement(lang_output_section_statement_enum,
2636 output_section_statement);*/
2637 /* Make next things chain into subchain of this */
2639 if (os->addr_tree ==
2640 (etree_type *) NULL)
2646 if (flags & SEC_NEVER_LOAD)
2650 os->block_value = block_value ? block_value : 1;
2651 stat_ptr = &os->children;
2653 os->subsection_alignment = topower(
2654 exp_get_value_int(subalign, -1,
2655 "subsection alignment",
2657 os->section_alignment = topower(
2658 exp_get_value_int(align, -1,
2659 "section alignment", 0));
2661 os->load_base = ebase;
2668 lang_output_statement_type *new =
2669 new_stat (lang_output_statement, stat_ptr);
2671 new->name = output_filename;
2674 /* Reset the current counters in the regions */
2676 reset_memory_regions ()
2678 lang_memory_region_type *p = lang_memory_region_list;
2680 for (p = lang_memory_region_list;
2681 p != (lang_memory_region_type *) NULL;
2684 p->old_length = (bfd_size_type) (p->current - p->origin);
2685 p->current = p->origin;
2692 lang_reasonable_defaults ();
2693 current_target = default_target;
2695 lang_for_each_statement (ldlang_open_output); /* Open the output file */
2697 ldemul_create_output_section_statements ();
2699 /* Add to the hash table all undefineds on the command line */
2700 lang_place_undefineds ();
2702 /* Create a bfd for each input file */
2703 current_target = default_target;
2704 open_input_bfds (statement_list.head, false);
2706 ldemul_after_open ();
2708 /* Build all sets based on the information gathered from the input
2710 ldctor_build_sets ();
2712 /* Size up the common data */
2715 /* Run through the contours of the script and attatch input sections
2716 to the correct output sections
2718 map_input_to_output_sections (statement_list.head, (char *) NULL,
2719 (lang_output_section_statement_type *) NULL);
2722 /* Find any sections not attatched explicitly and handle them */
2723 lang_place_orphans ();
2725 ldemul_before_allocation ();
2727 /* Now run around and relax if we can */
2728 if (command_line.relax)
2730 /* First time round is a trial run to get the 'worst case'
2731 addresses of the objects if there was no relaxing. */
2732 lang_size_sections (statement_list.head,
2734 &(statement_list.head), 0, (bfd_vma) 0, false);
2736 /* Keep relaxing until bfd_relax_section gives up. */
2739 reset_memory_regions ();
2741 relax_again = false;
2743 /* Do all the assignments with our current guesses as to
2745 lang_do_assignments (statement_list.head,
2747 (fill_type) 0, (bfd_vma) 0);
2749 /* Perform another relax pass - this time we know where the
2750 globals are, so can make better guess. */
2751 lang_size_sections (statement_list.head,
2753 &(statement_list.head), 0, (bfd_vma) 0, true);
2755 while (relax_again);
2759 /* Size up the sections. */
2760 lang_size_sections (statement_list.head,
2762 &(statement_list.head), 0, (bfd_vma) 0, false);
2765 /* See if anything special should be done now we know how big
2767 ldemul_after_allocation ();
2769 /* Fix any .startof. or .sizeof. symbols. */
2770 lang_set_startof ();
2772 /* Do all the assignments, now that we know the final restingplaces
2773 of all the symbols */
2775 lang_do_assignments (statement_list.head,
2777 (fill_type) 0, (bfd_vma) 0);
2779 /* Make sure that we're not mixing architectures */
2789 /* EXPORTED TO YACC */
2792 lang_add_wild (section_name, filename)
2793 CONST char *CONST section_name;
2794 CONST char *CONST filename;
2796 lang_wild_statement_type *new = new_stat (lang_wild_statement,
2799 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2801 placed_commons = true;
2803 if (filename != (char *) NULL)
2805 lang_has_input_file = true;
2807 new->section_name = section_name;
2808 new->filename = filename;
2809 lang_list_init (&new->children);
2813 lang_section_start (name, address)
2815 etree_type * address;
2817 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2819 ad->section_name = name;
2820 ad->address = address;
2823 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
2824 because of a -e argument on the command line, or zero if this is
2825 called by ENTRY in a linker script. Command line arguments take
2828 /* WINDOWS_NT. When an entry point has been specified, we will also force
2829 this symbol to be defined by calling ldlang_add_undef (equivalent to
2830 having switch -u entry_name on the command line). The reason we do
2831 this is so that the user doesn't have to because they would have to use
2832 the -u switch if they were specifying an entry point other than
2833 _mainCRTStartup. Specifically, if creating a windows application, entry
2834 point _WinMainCRTStartup must be specified.
2835 What I have found for non console applications (entry not _mainCRTStartup)
2836 is that the .obj that contains mainCRTStartup is brought in since it is
2837 the first encountered in libc.lib and it has other symbols in it which will
2838 be pulled in by the link process. To avoid this, adding -u with the entry
2839 point name specified forces the correct .obj to be used. We can avoid
2840 making the user do this by always adding the entry point name as an
2841 undefined symbol. */
2844 lang_add_entry (name, cmdline)
2848 if (entry_symbol == NULL
2850 || ! entry_from_cmdline)
2852 entry_symbol = name;
2853 entry_from_cmdline = cmdline;
2856 /* don't do this yet. It seems to work (the executables run), but the
2857 image created is very different from what I was getting before indicating
2858 that something else is being pulled in. When everything else is working,
2859 then try to put this back in to see if it will do the right thing for
2860 other more complicated applications */
2861 ldlang_add_undef (name);
2866 lang_add_target (name)
2869 lang_target_statement_type *new = new_stat (lang_target_statement,
2885 map_option_f = true;
2896 lang_fill_statement_type *new = new_stat (lang_fill_statement,
2903 lang_add_data (type, exp)
2905 union etree_union *exp;
2908 lang_data_statement_type *new = new_stat (lang_data_statement,
2916 /* Create a new reloc statement. RELOC is the BFD relocation type to
2917 generate. HOWTO is the corresponding howto structure (we could
2918 look this up, but the caller has already done so). SECTION is the
2919 section to generate a reloc against, or NAME is the name of the
2920 symbol to generate a reloc against. Exactly one of SECTION and
2921 NAME must be NULL. ADDEND is an expression for the addend. */
2924 lang_add_reloc (reloc, howto, section, name, addend)
2925 bfd_reloc_code_real_type reloc;
2926 reloc_howto_type *howto;
2929 union etree_union *addend;
2931 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
2935 p->section = section;
2937 p->addend_exp = addend;
2939 p->addend_value = 0;
2940 p->output_section = NULL;
2945 lang_add_assignment (exp)
2948 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
2955 lang_add_attribute (attribute)
2956 enum statement_enum attribute;
2958 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2965 if (startup_file != (char *) NULL)
2967 einfo ("%P%Fmultiple STARTUP files\n");
2969 first_file->filename = name;
2970 first_file->local_sym_name = name;
2971 first_file->real = true;
2973 startup_file = name;
2980 lang_float_flag = maybe;
2984 lang_leave_output_section_statement (fill, memspec)
2986 CONST char *memspec;
2988 current_section->fill = fill;
2989 current_section->region = lang_memory_region_lookup (memspec);
2990 stat_ptr = &statement_list;
2994 Create an absolute symbol with the given name with the value of the
2995 address of first byte of the section named.
2997 If the symbol already exists, then do nothing.
3000 lang_abs_symbol_at_beginning_of (secname, name)
3001 const char *secname;
3004 struct bfd_link_hash_entry *h;
3006 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3007 if (h == (struct bfd_link_hash_entry *) NULL)
3008 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3010 if (h->type == bfd_link_hash_new
3011 || h->type == bfd_link_hash_undefined)
3015 h->type = bfd_link_hash_defined;
3017 sec = bfd_get_section_by_name (output_bfd, secname);
3018 if (sec == (asection *) NULL)
3021 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3023 h->u.def.section = bfd_abs_section_ptr;
3028 Create an absolute symbol with the given name with the value of the
3029 address of the first byte after the end of the section named.
3031 If the symbol already exists, then do nothing.
3034 lang_abs_symbol_at_end_of (secname, name)
3035 const char *secname;
3038 struct bfd_link_hash_entry *h;
3040 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3041 if (h == (struct bfd_link_hash_entry *) NULL)
3042 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3044 if (h->type == bfd_link_hash_new
3045 || h->type == bfd_link_hash_undefined)
3049 h->type = bfd_link_hash_defined;
3051 sec = bfd_get_section_by_name (output_bfd, secname);
3052 if (sec == (asection *) NULL)
3055 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3056 + bfd_section_size (output_bfd, sec));
3058 h->u.def.section = bfd_abs_section_ptr;
3063 lang_statement_append (list, element, field)
3064 lang_statement_list_type * list;
3065 lang_statement_union_type * element;
3066 lang_statement_union_type ** field;
3068 *(list->tail) = element;
3072 /* Set the output format type. -oformat overrides scripts. */
3075 lang_add_output_format (format, big, little, from_script)
3081 if (output_target == NULL || !from_script)
3083 if (command_line.endian == ENDIAN_BIG
3086 else if (command_line.endian == ENDIAN_LITTLE
3090 output_target = format;
3094 /* Enter a group. This creates a new lang_group_statement, and sets
3095 stat_ptr to build new statements within the group. */
3100 lang_group_statement_type *g;
3102 g = new_stat (lang_group_statement, stat_ptr);
3103 lang_list_init (&g->children);
3104 stat_ptr = &g->children;
3107 /* Leave a group. This just resets stat_ptr to start writing to the
3108 regular list of statements again. Note that this will not work if
3109 groups can occur inside anything else which can adjust stat_ptr,
3110 but currently they can't. */
3115 stat_ptr = &statement_list;