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 lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
39 lang_statement_list_type*));
43 static struct obstack stat_obstack;
45 #define obstack_chunk_alloc xmalloc
46 #define obstack_chunk_free free
47 static CONST char *startup_file;
48 static lang_statement_list_type input_file_chain;
49 static boolean placed_commons = false;
50 static lang_output_section_statement_type *default_common_section;
51 static boolean map_option_f;
52 static bfd_vma print_dot;
53 static lang_input_statement_type *first_file;
54 static lang_statement_list_type lang_output_section_statement;
55 static CONST char *current_target;
56 static CONST char *output_target;
57 static int longest_section_name = 8;
58 static lang_statement_list_type statement_list;
59 static struct lang_phdr *lang_phdr_list;
61 static void print_size PARAMS ((size_t value));
62 static void print_alignment PARAMS ((unsigned int value));
63 static void print_fill PARAMS ((fill_type value));
64 static void print_section PARAMS ((const char *name));
65 static void lang_for_each_statement_worker
66 PARAMS ((void (*func) (lang_statement_union_type *),
67 lang_statement_union_type *s));
68 static lang_input_statement_type *new_afile
69 PARAMS ((const char *name, lang_input_file_enum_type file_type,
70 const char *target, boolean add_to_list));
71 static void print_flags PARAMS ((int *ignore_flags));
72 static void init_os PARAMS ((lang_output_section_statement_type *s));
73 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
75 lang_input_statement_type *file,
76 lang_output_section_statement_type *output));
77 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
78 static void load_symbols PARAMS ((lang_input_statement_type *entry,
79 lang_statement_list_type *));
80 static void wild PARAMS ((lang_wild_statement_type *s,
81 const char *section, const char *file,
83 lang_output_section_statement_type *output));
84 static bfd *open_output PARAMS ((const char *name));
85 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
86 static void open_input_bfds
87 PARAMS ((lang_statement_union_type *statement, boolean));
88 static void lang_reasonable_defaults PARAMS ((void));
89 static void lang_place_undefineds PARAMS ((void));
90 static void map_input_to_output_sections
91 PARAMS ((lang_statement_union_type *s,
93 lang_output_section_statement_type *output_section_statement));
94 static void print_output_section_statement
95 PARAMS ((lang_output_section_statement_type *output_section_statement));
96 static void print_assignment
97 PARAMS ((lang_assignment_statement_type *assignment,
98 lang_output_section_statement_type *output_section));
99 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
100 static void print_input_section PARAMS ((lang_input_section_type *in));
101 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
102 static void print_data_statement PARAMS ((lang_data_statement_type *data));
103 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
104 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
105 static void print_wild_statement
106 PARAMS ((lang_wild_statement_type *w,
107 lang_output_section_statement_type *os));
108 static void print_group
109 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
110 static void print_statement PARAMS ((lang_statement_union_type *s,
111 lang_output_section_statement_type *os));
112 static void print_statement_list 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));
131 static void lang_record_phdrs PARAMS ((void));
134 lang_output_section_statement_type *abs_output_section;
135 lang_statement_list_type *stat_ptr = &statement_list;
136 lang_statement_list_type file_chain = { 0 };
137 const char *entry_symbol = NULL;
138 boolean entry_from_cmdline;
139 boolean lang_has_input_file = false;
140 boolean had_output_filename = false;
141 boolean lang_float_flag = false;
142 boolean delete_output_file_on_failure = false;
144 etree_type *base; /* Relocation base - or null */
147 #if defined(__STDC__) || defined(ALMOST_STDC)
148 #define cat(a,b) a##b
150 #define cat(a,b) a/**/b
153 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
155 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
157 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
163 return obstack_alloc (&stat_obstack, size);
170 fprintf (config.map_file, "%5x", (unsigned) value);
174 print_alignment (value)
177 fprintf (config.map_file, "2**%1u", value);
184 fprintf (config.map_file, "%04x", (unsigned) value);
191 fprintf (config.map_file, "%*s", -longest_section_name, name);
194 /*----------------------------------------------------------------------
195 lang_for_each_statement walks the parse tree and calls the provided
196 function for each node
200 lang_for_each_statement_worker (func, s)
201 void (*func) PARAMS ((lang_statement_union_type *));
202 lang_statement_union_type *s;
204 for (; s != (lang_statement_union_type *) NULL; s = s->next)
208 switch (s->header.type)
210 case lang_constructors_statement_enum:
211 lang_for_each_statement_worker (func, constructor_list.head);
213 case lang_output_section_statement_enum:
214 lang_for_each_statement_worker
216 s->output_section_statement.children.head);
218 case lang_wild_statement_enum:
219 lang_for_each_statement_worker
221 s->wild_statement.children.head);
223 case lang_group_statement_enum:
224 lang_for_each_statement_worker (func,
225 s->group_statement.children.head);
227 case lang_data_statement_enum:
228 case lang_reloc_statement_enum:
229 case lang_object_symbols_statement_enum:
230 case lang_output_statement_enum:
231 case lang_target_statement_enum:
232 case lang_input_section_enum:
233 case lang_input_statement_enum:
234 case lang_assignment_statement_enum:
235 case lang_padding_statement_enum:
236 case lang_address_statement_enum:
246 lang_for_each_statement (func)
247 void (*func) PARAMS ((lang_statement_union_type *));
249 lang_for_each_statement_worker (func,
250 statement_list.head);
253 /*----------------------------------------------------------------------*/
255 lang_list_init (list)
256 lang_statement_list_type *list;
258 list->head = (lang_statement_union_type *) NULL;
259 list->tail = &list->head;
262 /*----------------------------------------------------------------------
264 build a new statement node for the parse tree
269 lang_statement_union_type *
270 new_statement (type, size, list)
271 enum statement_enum type;
273 lang_statement_list_type * list;
275 lang_statement_union_type *new = (lang_statement_union_type *)
278 new->header.type = type;
279 new->header.next = (lang_statement_union_type *) NULL;
280 lang_statement_append (list, new, &new->header.next);
285 Build a new input file node for the language. There are several ways
286 in which we treat an input file, eg, we only look at symbols, or
287 prefix it with a -l etc.
289 We can be supplied with requests for input files more than once;
290 they may, for example be split over serveral lines like foo.o(.text)
291 foo.o(.data) etc, so when asked for a file we check that we havn't
292 got it already so we don't duplicate the bfd.
295 static lang_input_statement_type *
296 new_afile (name, file_type, target, add_to_list)
298 lang_input_file_enum_type file_type;
302 lang_input_statement_type *p;
305 p = new_stat (lang_input_statement, stat_ptr);
308 p = ((lang_input_statement_type *)
309 stat_alloc (sizeof (lang_input_statement_type)));
310 p->header.next = NULL;
313 lang_has_input_file = true;
315 p->complained = false;
318 case lang_input_file_is_symbols_only_enum:
320 p->is_archive = false;
322 p->local_sym_name = name;
323 p->just_syms_flag = true;
324 p->search_dirs_flag = false;
326 case lang_input_file_is_fake_enum:
328 p->is_archive = false;
330 p->local_sym_name = name;
331 p->just_syms_flag = false;
332 p->search_dirs_flag = false;
334 case lang_input_file_is_l_enum:
335 p->is_archive = true;
338 p->local_sym_name = concat ("-l", name, (const char *) NULL);
339 p->just_syms_flag = false;
340 p->search_dirs_flag = true;
342 case lang_input_file_is_marker_enum:
344 p->is_archive = false;
346 p->local_sym_name = name;
347 p->just_syms_flag = false;
348 p->search_dirs_flag = true;
350 case lang_input_file_is_search_file_enum:
352 p->is_archive = false;
354 p->local_sym_name = name;
355 p->just_syms_flag = false;
356 p->search_dirs_flag = true;
358 case lang_input_file_is_file_enum:
360 p->is_archive = false;
362 p->local_sym_name = name;
363 p->just_syms_flag = false;
364 p->search_dirs_flag = false;
369 p->the_bfd = (bfd *) NULL;
370 p->asymbols = (asymbol **) NULL;
371 p->next_real_file = (lang_statement_union_type *) NULL;
372 p->next = (lang_statement_union_type *) NULL;
374 p->common_output_section = (asection *) NULL;
375 p->dynamic = config.dynamic_link;
377 lang_statement_append (&input_file_chain,
378 (lang_statement_union_type *) p,
383 lang_input_statement_type *
384 lang_add_input_file (name, file_type, target)
386 lang_input_file_enum_type file_type;
389 lang_has_input_file = true;
390 return new_afile (name, file_type, target, true);
393 /* Build enough state so that the parser can build its tree */
397 obstack_begin (&stat_obstack, 1000);
399 stat_ptr = &statement_list;
401 lang_list_init (stat_ptr);
403 lang_list_init (&input_file_chain);
404 lang_list_init (&lang_output_section_statement);
405 lang_list_init (&file_chain);
406 first_file = lang_add_input_file ((char *) NULL,
407 lang_input_file_is_marker_enum,
409 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
411 abs_output_section->bfd_section = bfd_abs_section_ptr;
415 /*----------------------------------------------------------------------
416 A region is an area of memory declared with the
417 MEMORY { name:org=exp, len=exp ... }
420 We maintain a list of all the regions here
422 If no regions are specified in the script, then the default is used
423 which is created when looked up to be the entire data space
426 static lang_memory_region_type *lang_memory_region_list;
427 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
429 lang_memory_region_type *
430 lang_memory_region_lookup (name)
431 CONST char *CONST name;
434 lang_memory_region_type *p = lang_memory_region_list;
436 for (p = lang_memory_region_list;
437 p != (lang_memory_region_type *) NULL;
440 if (strcmp (p->name, name) == 0)
447 /* This code used to always use the first region in the list as the
448 default region. I changed it to instead use a region
449 encompassing all of memory as the default region. This permits
450 NOLOAD sections to work reasonably without requiring a region.
451 People should specify what region they mean, if they really want
453 if (strcmp (name, "*default*") == 0)
455 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
457 return lang_memory_region_list;
463 lang_memory_region_type *new =
464 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
466 new->name = buystring (name);
467 new->next = (lang_memory_region_type *) NULL;
469 *lang_memory_region_list_tail = new;
470 lang_memory_region_list_tail = &new->next;
472 new->length = ~(bfd_size_type)0;
474 new->had_full_message = false;
481 lang_output_section_statement_type *
482 lang_output_section_find (name)
483 CONST char *CONST name;
485 lang_statement_union_type *u;
486 lang_output_section_statement_type *lookup;
488 for (u = lang_output_section_statement.head;
489 u != (lang_statement_union_type *) NULL;
492 lookup = &u->output_section_statement;
493 if (strcmp (name, lookup->name) == 0)
498 return (lang_output_section_statement_type *) NULL;
501 lang_output_section_statement_type *
502 lang_output_section_statement_lookup (name)
503 CONST char *CONST name;
505 lang_output_section_statement_type *lookup;
507 lookup = lang_output_section_find (name);
508 if (lookup == (lang_output_section_statement_type *) NULL)
511 lookup = (lang_output_section_statement_type *)
512 new_stat (lang_output_section_statement, stat_ptr);
513 lookup->region = (lang_memory_region_type *) NULL;
515 lookup->block_value = 1;
518 lookup->next = (lang_statement_union_type *) NULL;
519 lookup->bfd_section = (asection *) NULL;
520 lookup->processed = false;
521 lookup->loadable = 1;
522 lookup->addr_tree = (etree_type *) NULL;
523 lang_list_init (&lookup->children);
525 lookup->memspec = (CONST char *) NULL;
527 lookup->subsection_alignment = -1;
528 lookup->section_alignment = -1;
529 lookup->load_base = (union etree_union *) NULL;
530 lookup->phdrs = NULL;
532 lang_statement_append (&lang_output_section_statement,
533 (lang_statement_union_type *) lookup,
541 print_flags (ignore_flags)
544 fprintf (config.map_file, "(");
546 if (flags->flag_read)
547 fprintf (outfile, "R");
548 if (flags->flag_write)
549 fprintf (outfile, "W");
550 if (flags->flag_executable)
551 fprintf (outfile, "X");
552 if (flags->flag_loadable)
553 fprintf (outfile, "L");
555 fprintf (config.map_file, ")");
561 lang_memory_region_type *m;
563 fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
565 fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
567 fprintf (config.map_file,
568 "name\t\torigin length r_size c_size is attributes\n");
571 for (m = lang_memory_region_list;
572 m != (lang_memory_region_type *) NULL;
575 fprintf (config.map_file, "%-16s", m->name);
576 print_address (m->origin);
578 print_address ((bfd_vma)m->length);
580 print_address ((bfd_vma)m->old_length);
582 print_address (m->current - m->origin);
585 fprintf (config.map_file, " %2d%% ",
586 (int) ((m->current - m->origin) * 100 / m->old_length));
587 print_flags (&m->flags);
588 fprintf (config.map_file, "\n");
590 fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
591 fprintf (config.map_file, "output input virtual\n");
592 fprintf (config.map_file, "section section address tsize\n\n");
603 lang_output_section_statement_type * s;
605 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
606 section_userdata_type *new =
607 (section_userdata_type *)
608 stat_alloc (sizeof (section_userdata_type));
610 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
611 if (s->bfd_section == (asection *) NULL)
612 s->bfd_section = bfd_make_section (output_bfd, s->name);
613 if (s->bfd_section == (asection *) NULL)
615 einfo ("%P%F: output format %s cannot represent section called %s\n",
616 output_bfd->xvec->name, s->name);
618 s->bfd_section->output_section = s->bfd_section;
620 /* We initialize an output sections output offset to minus its own */
621 /* vma to allow us to output a section through itself */
622 s->bfd_section->output_offset = 0;
623 get_userdata (s->bfd_section) = (PTR) new;
627 /***********************************************************************
630 These expand statements like *(.text) and foo.o to a list of
631 explicit actions, like foo.o(.text), bar.o(.text) and
634 The toplevel routine, wild, takes a statement, section, file and
635 target. If either the section or file is null it is taken to be the
636 wildcard. Seperate lang_input_section statements are created for
637 each part of the expanstion, and placed after the statement provided.
642 wild_doit (ptr, section, output, file)
643 lang_statement_list_type * ptr;
645 lang_output_section_statement_type * output;
646 lang_input_statement_type * file;
648 if (output->bfd_section == (asection *) NULL)
651 if (section != (asection *) NULL
652 && section->output_section == (asection *) NULL)
654 /* Add a section reference to the list */
655 lang_input_section_type *new = new_stat (lang_input_section, ptr);
657 new->section = section;
659 section->output_section = output->bfd_section;
661 /* We don't copy the SEC_NEVER_LOAD flag from an input section to
662 an output section, because we want to be able to include a
663 SEC_NEVER_LOAD section in the middle of an otherwise loaded
664 section (I don't know why we want to do this, but we do).
665 build_link_order in ldwrite.c handles this case by turning the
666 embedded SEC_NEVER_LOAD section into a fill. */
667 section->output_section->flags |=
668 section->flags & (flagword) (~ SEC_NEVER_LOAD);
670 if (!output->loadable)
672 /* Turn off load flag */
673 output->bfd_section->flags &= ~SEC_LOAD;
674 output->bfd_section->flags |= SEC_NEVER_LOAD;
676 if (section->alignment_power > output->bfd_section->alignment_power)
678 output->bfd_section->alignment_power = section->alignment_power;
680 /* If supplied an aligmnet, then force it */
681 if (output->section_alignment != -1)
683 output->bfd_section->alignment_power = output->section_alignment;
689 wild_section (ptr, section, file, output)
690 lang_wild_statement_type *ptr;
692 lang_input_statement_type *file;
693 lang_output_section_statement_type *output;
695 if (file->just_syms_flag == false)
697 register asection *s;
699 for (s = file->the_bfd->sections; s != NULL; s = s->next)
701 /* Attach all sections named SECTION. If SECTION is NULL,
702 then attach all sections.
704 Previously, if SECTION was NULL, this code did not call
705 wild_doit if the SEC_IS_COMMON flag was set for the
706 section. I did not understand that, and I took it out.
710 || strcmp (bfd_get_section_name (file->the_bfd, s),
712 wild_doit (&ptr->children, s, output, file);
717 /* passed a file name (which must have been seen already and added to
718 the statement tree. We will see if it has been opened already and
719 had its symbols read. If not then we'll read it.
721 Archives are pecuilar here. We may open them once, but if they do
722 not define anything we need at the time, they won't have all their
723 symbols read. If we need them later, we'll have to redo it. */
724 static lang_input_statement_type *
728 lang_input_statement_type *search;
730 for (search = (lang_input_statement_type *) input_file_chain.head;
731 search != (lang_input_statement_type *) NULL;
732 search = (lang_input_statement_type *) search->next_real_file)
734 if (search->filename == (char *) NULL && name == (char *) NULL)
736 if (search->filename != (char *) NULL
737 && name != (char *) NULL
738 && strcmp (search->filename, name) == 0)
742 if (search == (lang_input_statement_type *) NULL)
743 search = new_afile (name, lang_input_file_is_file_enum, default_target,
746 /* If we have already added this file, or this file is not real
747 (FIXME: can that ever actually happen?) or the name is NULL
748 (FIXME: can that ever actually happen?) don't add this file. */
751 || search->filename == (const char *) NULL)
754 load_symbols (search, (lang_statement_list_type *) NULL);
759 /* Get the symbols for an input file. */
762 load_symbols (entry, place)
763 lang_input_statement_type *entry;
764 lang_statement_list_type *place;
771 ldfile_open_file (entry);
773 if (! bfd_check_format (entry->the_bfd, bfd_archive)
774 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
777 lang_statement_list_type *hold;
779 err = bfd_get_error ();
780 if (err == bfd_error_file_ambiguously_recognized)
784 einfo ("%B: file not recognized: %E\n", entry->the_bfd);
785 einfo ("%B: matching formats:", entry->the_bfd);
786 for (p = matching; *p != NULL; p++)
790 else if (err != bfd_error_file_not_recognized
792 einfo ("%F%B: file not recognized: %E\n", entry->the_bfd);
794 /* Try to interpret the file as a linker script. */
796 bfd_close (entry->the_bfd);
797 entry->the_bfd = NULL;
799 ldfile_open_command_file (entry->filename);
804 ldfile_assumed_script = true;
805 parser_input = input_script;
807 ldfile_assumed_script = false;
814 /* We don't call ldlang_add_file for an archive. Instead, the
815 add_symbols entry point will call ldlang_add_file, via the
816 add_archive_element callback, for each element of the archive
818 switch (bfd_get_format (entry->the_bfd))
824 ldlang_add_file (entry);
825 if (trace_files || trace_file_tries)
826 info_msg ("%I\n", entry);
832 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
834 while (member != NULL)
836 if (! bfd_check_format (member, bfd_object))
837 einfo ("%F%B: object %B in archive is not object\n",
838 entry->the_bfd, member);
839 if (! ((*link_info.callbacks->add_archive_element)
840 (&link_info, member, "-whole-archive")))
842 if (! bfd_link_add_symbols (member, &link_info))
843 einfo ("%F%B: could not read symbols: %E\n", member);
844 member = bfd_openr_next_archived_file (entry->the_bfd,
848 entry->loaded = true;
854 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
855 einfo ("%F%B: could not read symbols: %E\n", entry->the_bfd);
857 entry->loaded = true;
861 wild (s, section, file, target, output)
862 lang_wild_statement_type * s;
866 lang_output_section_statement_type * output;
868 lang_input_statement_type *f;
870 if (file == (char *) NULL)
872 /* Perform the iteration over all files in the list */
873 for (f = (lang_input_statement_type *) file_chain.head;
874 f != (lang_input_statement_type *) NULL;
875 f = (lang_input_statement_type *) f->next)
877 wild_section (s, section, f, output);
882 /* Perform the iteration over a single file */
883 wild_section (s, section, lookup_name (file), output);
885 if (section != (char *) NULL
886 && strcmp (section, "COMMON") == 0
887 && default_common_section == (lang_output_section_statement_type *) NULL)
889 /* Remember the section that common is going to incase we later
890 get something which doesn't know where to put it */
891 default_common_section = output;
896 read in all the files
905 if (output_target == (char *) NULL)
907 if (current_target != (char *) NULL)
908 output_target = current_target;
910 output_target = default_target;
912 output = bfd_openw (name, output_target);
914 if (output == (bfd *) NULL)
916 if (bfd_get_error () == bfd_error_invalid_target)
918 einfo ("%P%F: target %s not found\n", output_target);
920 einfo ("%P%F: cannot open output file %s: %E\n", name);
923 delete_output_file_on_failure = 1;
925 /* output->flags |= D_PAGED;*/
927 if (! bfd_set_format (output, bfd_object))
928 einfo ("%P%F:%s: can not make object file: %E\n", name);
929 if (! bfd_set_arch_mach (output,
930 ldfile_output_architecture,
931 ldfile_output_machine))
932 einfo ("%P%F:%s: can not set architecture: %E\n", name);
934 link_info.hash = bfd_link_hash_table_create (output);
935 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
936 einfo ("%P%F: can not create link hash table: %E\n");
938 bfd_set_gp_size (output, g_switch_value);
946 ldlang_open_output (statement)
947 lang_statement_union_type * statement;
949 switch (statement->header.type)
951 case lang_output_statement_enum:
952 ASSERT (output_bfd == (bfd *) NULL);
953 output_bfd = open_output (statement->output_statement.name);
954 ldemul_set_output_arch ();
955 if (config.magic_demand_paged && !link_info.relocateable)
956 output_bfd->flags |= D_PAGED;
958 output_bfd->flags &= ~D_PAGED;
959 if (config.text_read_only)
960 output_bfd->flags |= WP_TEXT;
962 output_bfd->flags &= ~WP_TEXT;
963 if (config.traditional_format)
964 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
966 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
969 case lang_target_statement_enum:
970 current_target = statement->target_statement.target;
977 /* Open all the input files. */
980 open_input_bfds (s, force)
981 lang_statement_union_type *s;
984 for (; s != (lang_statement_union_type *) NULL; s = s->next)
986 switch (s->header.type)
988 case lang_constructors_statement_enum:
989 open_input_bfds (constructor_list.head, force);
991 case lang_output_section_statement_enum:
992 open_input_bfds (s->output_section_statement.children.head, force);
994 case lang_wild_statement_enum:
995 /* Maybe we should load the file's symbols */
996 if (s->wild_statement.filename)
997 (void) lookup_name (s->wild_statement.filename);
998 open_input_bfds (s->wild_statement.children.head, force);
1000 case lang_group_statement_enum:
1002 struct bfd_link_hash_entry *undefs;
1004 /* We must continually search the entries in the group
1005 until no new symbols are added to the list of undefined
1010 undefs = link_info.hash->undefs_tail;
1011 open_input_bfds (s->group_statement.children.head, true);
1013 while (undefs != link_info.hash->undefs_tail);
1016 case lang_target_statement_enum:
1017 current_target = s->target_statement.target;
1019 case lang_input_statement_enum:
1020 if (s->input_statement.real == true)
1022 lang_statement_list_type add;
1024 s->input_statement.target = current_target;
1026 /* If we are being called from within a group, and this
1027 is an archive which has already been searched, then
1028 force it to be researched. */
1030 && s->input_statement.loaded
1031 && bfd_check_format (s->input_statement.the_bfd,
1033 s->input_statement.loaded = false;
1035 lang_list_init (&add);
1037 load_symbols (&s->input_statement, &add);
1039 if (add.head != NULL)
1041 *add.tail = s->next;
1052 /* If there are [COMMONS] statements, put a wild one into the bss section */
1055 lang_reasonable_defaults ()
1058 lang_output_section_statement_lookup (".text");
1059 lang_output_section_statement_lookup (".data");
1061 default_common_section =
1062 lang_output_section_statement_lookup (".bss");
1065 if (placed_commons == false)
1067 lang_wild_statement_type *new =
1068 new_stat (lang_wild_statement,
1069 &default_common_section->children);
1071 new->section_name = "COMMON";
1072 new->filename = (char *) NULL;
1073 lang_list_init (&new->children);
1080 Add the supplied name to the symbol table as an undefined reference.
1081 Remove items from the chain as we open input bfds
1083 typedef struct ldlang_undef_chain_list
1085 struct ldlang_undef_chain_list *next;
1087 } ldlang_undef_chain_list_type;
1089 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1092 ldlang_add_undef (name)
1093 CONST char *CONST name;
1095 ldlang_undef_chain_list_type *new =
1096 ((ldlang_undef_chain_list_type *)
1097 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1099 new->next = ldlang_undef_chain_list_head;
1100 ldlang_undef_chain_list_head = new;
1102 new->name = buystring (name);
1105 /* Run through the list of undefineds created above and place them
1106 into the linker hash table as undefined symbols belonging to the
1110 lang_place_undefineds ()
1112 ldlang_undef_chain_list_type *ptr;
1114 for (ptr = ldlang_undef_chain_list_head;
1115 ptr != (ldlang_undef_chain_list_type *) NULL;
1118 struct bfd_link_hash_entry *h;
1120 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1121 if (h == (struct bfd_link_hash_entry *) NULL)
1122 einfo ("%P%F: bfd_link_hash_lookup failed: %E");
1123 if (h->type == bfd_link_hash_new)
1125 h->type = bfd_link_hash_undefined;
1126 h->u.undef.abfd = NULL;
1127 bfd_link_add_undef (link_info.hash, h);
1132 /* Open input files and attatch to output sections */
1134 map_input_to_output_sections (s, target, output_section_statement)
1135 lang_statement_union_type * s;
1137 lang_output_section_statement_type * output_section_statement;
1139 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1141 switch (s->header.type)
1145 case lang_wild_statement_enum:
1146 wild (&s->wild_statement, s->wild_statement.section_name,
1147 s->wild_statement.filename, target,
1148 output_section_statement);
1151 case lang_constructors_statement_enum:
1152 map_input_to_output_sections (constructor_list.head,
1154 output_section_statement);
1156 case lang_output_section_statement_enum:
1157 map_input_to_output_sections (s->output_section_statement.children.head,
1159 &s->output_section_statement);
1161 case lang_output_statement_enum:
1163 case lang_target_statement_enum:
1164 target = s->target_statement.target;
1166 case lang_group_statement_enum:
1167 map_input_to_output_sections (s->group_statement.children.head,
1169 output_section_statement);
1171 case lang_fill_statement_enum:
1172 case lang_input_section_enum:
1173 case lang_object_symbols_statement_enum:
1174 case lang_data_statement_enum:
1175 case lang_reloc_statement_enum:
1176 case lang_assignment_statement_enum:
1177 case lang_padding_statement_enum:
1178 case lang_input_statement_enum:
1179 if (output_section_statement != NULL
1180 && output_section_statement->bfd_section == NULL)
1181 init_os (output_section_statement);
1183 case lang_afile_asection_pair_statement_enum:
1186 case lang_address_statement_enum:
1187 /* Mark the specified section with the supplied address */
1189 lang_output_section_statement_type *os =
1190 lang_output_section_statement_lookup
1191 (s->address_statement.section_name);
1193 if (os->bfd_section == NULL)
1195 os->addr_tree = s->address_statement.address;
1203 print_output_section_statement (output_section_statement)
1204 lang_output_section_statement_type * output_section_statement;
1206 asection *section = output_section_statement->bfd_section;
1209 print_section (output_section_statement->name);
1214 print_dot = section->vma;
1218 print_address (section->vma);
1220 print_size (section->_raw_size);
1222 print_size(section->_cooked_size);
1224 print_alignment (section->alignment_power);
1227 fprintf (config.map_file, "%s flags", output_section_statement->region->name);
1228 print_flags (stdout, &output_section_statement->flags);
1230 if (section->flags & SEC_LOAD)
1231 fprintf (config.map_file, "load ");
1232 if (section->flags & SEC_ALLOC)
1233 fprintf (config.map_file, "alloc ");
1234 if (section->flags & SEC_RELOC)
1235 fprintf (config.map_file, "reloc ");
1236 if (section->flags & SEC_HAS_CONTENTS)
1237 fprintf (config.map_file, "contents ");
1242 fprintf (config.map_file, " (no attached output section)");
1245 if (output_section_statement->load_base)
1247 bfd_vma b = exp_get_abs_int(output_section_statement->load_base,
1248 0, "output base", lang_final_phase_enum);
1249 fprintf (config.map_file, "Output address ");
1250 fprintf_vma (config.map_file, b);
1251 fprintf (config.map_file, "\n");
1253 if (output_section_statement->section_alignment >= 0
1254 || output_section_statement->subsection_alignment >= 0)
1256 fprintf (config.map_file, "\t\t\t\t\tforced alignment ");
1257 if (output_section_statement->section_alignment >= 0)
1259 fprintf (config.map_file, "section 2**%d ",output_section_statement->section_alignment );
1261 if ( output_section_statement->subsection_alignment >= 0)
1263 fprintf (config.map_file, "subsection 2**%d ",output_section_statement->subsection_alignment );
1268 print_statement_list (output_section_statement->children.head,
1269 output_section_statement);
1274 print_assignment (assignment, output_section)
1275 lang_assignment_statement_type * assignment;
1276 lang_output_section_statement_type * output_section;
1278 etree_value_type result;
1284 print_address (print_dot);
1286 result = exp_fold_tree (assignment->exp->assign.src,
1288 lang_final_phase_enum,
1294 print_address (result.value);
1298 fprintf (config.map_file, "*undefined*");
1301 exp_print_tree (assignment->exp);
1303 fprintf (config.map_file, "\n");
1307 print_input_statement (statm)
1308 lang_input_statement_type * statm;
1310 if (statm->filename != (char *) NULL)
1312 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1316 /* Print all the defined symbols for the abfd provided by in the supplied
1321 print_one_symbol (hash_entry, ptr)
1322 struct bfd_link_hash_entry *hash_entry;
1325 asection * sec = (asection *)ptr;
1327 if (hash_entry->type == bfd_link_hash_defined
1328 || hash_entry->type == bfd_link_hash_defweak)
1330 if (sec == hash_entry->u.def.section) {
1332 fprintf (config.map_file, " ");
1334 fprintf (config.map_file, " ");
1335 print_address (hash_entry->u.def.value + outside_section_address (sec));
1336 fprintf (config.map_file, " %s", hash_entry->root.string);
1345 print_input_section (in)
1346 lang_input_section_type * in;
1348 asection *i = in->section;
1349 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1354 fprintf (config.map_file, " ");
1355 print_section (i->name);
1356 fprintf (config.map_file, " ");
1357 if (i->output_section)
1359 print_address (i->output_section->vma + i->output_offset);
1360 fprintf (config.map_file, " ");
1361 print_size (i->_raw_size);
1362 fprintf (config.map_file, " ");
1363 print_size(i->_cooked_size);
1364 fprintf (config.map_file, " ");
1365 print_alignment (i->alignment_power);
1366 fprintf (config.map_file, " ");
1370 bfd *abfd = in->ifile->the_bfd;
1372 if (in->ifile->just_syms_flag == true)
1374 fprintf (config.map_file, "symbols only ");
1377 fprintf (config.map_file, " %s ", abfd->xvec->name);
1378 if (abfd->my_archive != (bfd *) NULL)
1380 fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
1385 fprintf (config.map_file, "%s", abfd->filename);
1387 fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
1390 /* Print all the symbols */
1391 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1399 print_dot = outside_section_address (i) + size;
1403 fprintf (config.map_file, "No output section allocated\n");
1409 print_fill_statement (fill)
1410 lang_fill_statement_type * fill;
1412 fprintf (config.map_file, "FILL mask ");
1413 print_fill (fill->fill);
1417 print_data_statement (data)
1418 lang_data_statement_type * data;
1420 /* bfd_vma value; */
1425 /* ASSERT(print_dot == data->output_vma);*/
1427 /* data->output_section may be NULL if called from gdb. */
1428 if (data->output_section)
1429 print_address (data->output_vma + data->output_section->vma);
1431 print_address (data->output_vma);
1433 print_address (data->value);
1438 fprintf (config.map_file, "BYTE ");
1439 print_dot += BYTE_SIZE;
1442 fprintf (config.map_file, "SHORT ");
1443 print_dot += SHORT_SIZE;
1446 fprintf (config.map_file, "LONG ");
1447 print_dot += LONG_SIZE;
1450 fprintf (config.map_file, "QUAD ");
1451 print_dot += QUAD_SIZE;
1455 exp_print_tree (data->exp);
1457 fprintf (config.map_file, "\n");
1460 /* Print a reloc statement. */
1463 print_reloc_statement (reloc)
1464 lang_reloc_statement_type *reloc;
1471 /* ASSERT(print_dot == data->output_vma);*/
1473 /* reloc->output_section may be NULL if called from gdb. */
1474 if (reloc->output_section)
1475 print_address (reloc->output_vma + reloc->output_section->vma);
1477 print_address (reloc->output_vma);
1479 print_address (reloc->addend_value);
1482 fprintf (config.map_file, "RELOC %s ", reloc->howto->name);
1484 print_dot += bfd_get_reloc_size (reloc->howto);
1486 exp_print_tree (reloc->addend_exp);
1488 fprintf (config.map_file, "\n");
1492 print_padding_statement (s)
1493 lang_padding_statement_type * s;
1497 print_section ("*fill*");
1499 /* s->output_section may be NULL if called from gdb. */
1500 if (s->output_section)
1501 print_address (s->output_offset + s->output_section->vma);
1503 print_address (s->output_offset);
1505 print_size (s->size);
1507 print_fill (s->fill);
1510 print_dot = s->output_offset + s->size;
1511 /* s->output_section may be NULL if called from gdb. */
1512 if (s->output_section)
1513 print_dot += s->output_section->vma;
1517 print_wild_statement (w, os)
1518 lang_wild_statement_type * w;
1519 lang_output_section_statement_type * os;
1521 fprintf (config.map_file, " from ");
1522 if (w->filename != (char *) NULL)
1524 fprintf (config.map_file, "%s", w->filename);
1528 fprintf (config.map_file, "*");
1530 if (w->section_name != (char *) NULL)
1532 fprintf (config.map_file, "(%s)", w->section_name);
1536 fprintf (config.map_file, "(*)");
1539 print_statement_list (w->children.head, os);
1543 /* Print a group statement. */
1547 lang_group_statement_type *s;
1548 lang_output_section_statement_type *os;
1550 fprintf (config.map_file, "START GROUP\n");
1551 print_statement_list (s->children.head, os);
1552 fprintf (config.map_file, "END GROUP\n");
1555 /* Print the list of statements in S.
1556 This can be called for any statement type. */
1559 print_statement_list (s, os)
1560 lang_statement_union_type * s;
1561 lang_output_section_statement_type * os;
1565 print_statement (s, os);
1570 /* Print the first statement in statement list S.
1571 This can be called for any statement type. */
1574 print_statement (s, os)
1575 lang_statement_union_type * s;
1576 lang_output_section_statement_type * os;
1578 switch (s->header.type)
1580 case lang_constructors_statement_enum:
1581 fprintf (config.map_file, "constructors:\n");
1582 print_statement_list (constructor_list.head, os);
1584 case lang_wild_statement_enum:
1585 print_wild_statement (&s->wild_statement, os);
1588 fprintf (config.map_file, "Fail with %d\n", s->header.type);
1591 case lang_address_statement_enum:
1592 fprintf (config.map_file, "address\n");
1594 case lang_object_symbols_statement_enum:
1595 fprintf (config.map_file, "object symbols\n");
1597 case lang_fill_statement_enum:
1598 print_fill_statement (&s->fill_statement);
1600 case lang_data_statement_enum:
1601 print_data_statement (&s->data_statement);
1603 case lang_reloc_statement_enum:
1604 print_reloc_statement (&s->reloc_statement);
1606 case lang_input_section_enum:
1607 print_input_section (&s->input_section);
1609 case lang_padding_statement_enum:
1610 print_padding_statement (&s->padding_statement);
1612 case lang_output_section_statement_enum:
1613 print_output_section_statement (&s->output_section_statement);
1615 case lang_assignment_statement_enum:
1616 print_assignment (&s->assignment_statement,
1619 case lang_target_statement_enum:
1620 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1622 case lang_output_statement_enum:
1623 fprintf (config.map_file, "OUTPUT(%s %s)\n",
1624 s->output_statement.name,
1625 output_target ? output_target : "");
1627 case lang_input_statement_enum:
1628 print_input_statement (&s->input_statement);
1630 case lang_group_statement_enum:
1631 print_group (&s->group_statement, os);
1633 case lang_afile_asection_pair_statement_enum:
1643 print_statement_list (statement_list.head,
1644 abs_output_section);
1648 /* Print the first N statements in statement list S to STDERR.
1649 If N == 0, nothing is printed.
1650 If N < 0, the entire list is printed.
1651 Intended to be called from GDB. */
1654 dprint_statement (s, n)
1655 lang_statement_union_type * s;
1658 FILE *map_save = config.map_file;
1660 config.map_file = stderr;
1663 print_statement_list (s, abs_output_section);
1668 print_statement (s, abs_output_section);
1673 config.map_file = map_save;
1677 insert_pad (this_ptr, fill, power, output_section_statement, dot)
1678 lang_statement_union_type ** this_ptr;
1681 asection * output_section_statement;
1684 /* Align this section first to the
1685 input sections requirement, then
1686 to the output section's requirement.
1687 If this alignment is > than any seen before,
1688 then record it too. Perform the alignment by
1689 inserting a magic 'padding' statement.
1692 unsigned int alignment_needed = align_power (dot, power) - dot;
1694 if (alignment_needed != 0)
1696 lang_statement_union_type *new =
1697 ((lang_statement_union_type *)
1698 stat_alloc (sizeof (lang_padding_statement_type)));
1700 /* Link into existing chain */
1701 new->header.next = *this_ptr;
1703 new->header.type = lang_padding_statement_enum;
1704 new->padding_statement.output_section = output_section_statement;
1705 new->padding_statement.output_offset =
1706 dot - output_section_statement->vma;
1707 new->padding_statement.fill = fill;
1708 new->padding_statement.size = alignment_needed;
1712 /* Remember the most restrictive alignment */
1713 if (power > output_section_statement->alignment_power)
1715 output_section_statement->alignment_power = power;
1717 output_section_statement->_raw_size += alignment_needed;
1718 return alignment_needed + dot;
1722 /* Work out how much this section will move the dot point */
1724 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
1725 lang_statement_union_type ** this_ptr;
1726 lang_output_section_statement_type * output_section_statement;
1731 lang_input_section_type *is = &((*this_ptr)->input_section);
1732 asection *i = is->section;
1734 if (is->ifile->just_syms_flag == false)
1736 if (output_section_statement->subsection_alignment != -1)
1737 i->alignment_power =
1738 output_section_statement->subsection_alignment;
1740 dot = insert_pad (this_ptr, fill, i->alignment_power,
1741 output_section_statement->bfd_section, dot);
1743 /* Remember where in the output section this input section goes */
1745 i->output_offset = dot - output_section_statement->bfd_section->vma;
1747 /* Mark how big the output section must be to contain this now
1749 if (i->_cooked_size != 0)
1750 dot += i->_cooked_size;
1752 dot += i->_raw_size;
1753 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
1757 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1763 /* This variable indicates whether bfd_relax_section should be called
1766 static boolean relax_again;
1768 /* Set the sizes for all the output sections. */
1771 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
1772 lang_statement_union_type * s;
1773 lang_output_section_statement_type * output_section_statement;
1774 lang_statement_union_type ** prev;
1779 /* Size up the sections from their constituent parts */
1780 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1782 switch (s->header.type)
1785 case lang_output_section_statement_enum:
1788 lang_output_section_statement_type *os = &s->output_section_statement;
1790 if (os->bfd_section == NULL)
1792 /* This section was never actually created. */
1796 /* If this is a COFF shared library section, use the size and
1797 address from the input section. FIXME: This is COFF
1798 specific; it would be cleaner if there were some other way
1799 to do this, but nothing simple comes to mind. */
1800 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
1804 if (os->children.head == NULL
1805 || os->children.head->next != NULL
1806 || os->children.head->header.type != lang_input_section_enum)
1807 einfo ("%P%X: Internal error on COFF shared library section %s",
1810 input = os->children.head->input_section.section;
1811 bfd_set_section_vma (os->bfd_section->owner,
1813 bfd_section_vma (input->owner, input));
1814 os->bfd_section->_raw_size = input->_raw_size;
1818 if (bfd_is_abs_section (os->bfd_section))
1820 /* No matter what happens, an abs section starts at zero */
1821 ASSERT (os->bfd_section->vma == 0);
1825 if (os->addr_tree == (etree_type *) NULL)
1827 /* No address specified for this section, get one
1828 from the region specification
1830 if (os->region == (lang_memory_region_type *) NULL)
1832 os->region = lang_memory_region_lookup ("*default*");
1834 dot = os->region->current;
1835 if (os->section_alignment == -1)
1836 dot = align_power (dot, os->bfd_section->alignment_power);
1842 r = exp_fold_tree (os->addr_tree,
1844 lang_allocating_phase_enum,
1846 if (r.valid == false)
1848 einfo ("%F%S: non constant address expression for section %s\n",
1853 /* The section starts here */
1854 /* First, align to what the section needs */
1856 if (os->section_alignment != -1)
1857 dot = align_power (dot, os->section_alignment);
1859 bfd_set_section_vma (0, os->bfd_section, dot);
1861 os->bfd_section->output_offset = 0;
1864 (void) lang_size_sections (os->children.head, os, &os->children.head,
1865 os->fill, dot, relax);
1866 /* Ignore the size of the input sections, use the vma and size to */
1869 after = ALIGN_N (os->bfd_section->vma +
1870 os->bfd_section->_raw_size,
1871 /* The coercion here is important, see ld.h. */
1872 (bfd_vma) os->block_value);
1874 if (bfd_is_abs_section (os->bfd_section))
1875 ASSERT (after == os->bfd_section->vma);
1877 os->bfd_section->_raw_size = after - os->bfd_section->vma;
1878 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1879 os->processed = true;
1881 /* Replace into region ? */
1882 if (os->region != (lang_memory_region_type *) NULL)
1884 os->region->current = dot;
1885 /* Make sure this isn't silly. */
1886 if ((os->region->current < os->region->origin)
1887 || (os->region->current
1888 > os->region->origin + os->region->length))
1890 if (os->addr_tree != (etree_type *) NULL)
1892 einfo ("%X%P: address 0x%v of %B section %s is not within region %s\n",
1893 os->region->current,
1894 os->bfd_section->owner,
1895 os->bfd_section->name,
1900 einfo ("%X%P: region %s is full (%B section %s)\n",
1902 os->bfd_section->owner,
1903 os->bfd_section->name);
1905 /* Reset the region pointer. */
1906 os->region->current = os->region->origin;
1912 case lang_constructors_statement_enum:
1913 dot = lang_size_sections (constructor_list.head,
1914 output_section_statement,
1915 &s->wild_statement.children.head,
1920 case lang_data_statement_enum:
1922 unsigned int size = 0;
1924 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1925 s->data_statement.output_section =
1926 output_section_statement->bfd_section;
1928 switch (s->data_statement.type)
1945 output_section_statement->bfd_section->_raw_size += size;
1946 /* The output section gets contents, and then we inspect for
1947 any flags set in the input script which override any ALLOC */
1948 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
1949 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
1950 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
1955 case lang_reloc_statement_enum:
1959 s->reloc_statement.output_vma =
1960 dot - output_section_statement->bfd_section->vma;
1961 s->reloc_statement.output_section =
1962 output_section_statement->bfd_section;
1963 size = bfd_get_reloc_size (s->reloc_statement.howto);
1965 output_section_statement->bfd_section->_raw_size += size;
1969 case lang_wild_statement_enum:
1971 dot = lang_size_sections (s->wild_statement.children.head,
1972 output_section_statement,
1973 &s->wild_statement.children.head,
1979 case lang_object_symbols_statement_enum:
1980 link_info.create_object_symbols_section =
1981 output_section_statement->bfd_section;
1983 case lang_output_statement_enum:
1984 case lang_target_statement_enum:
1986 case lang_input_section_enum:
1990 i = (*prev)->input_section.section;
1992 i->_cooked_size = i->_raw_size;
1997 if (! bfd_relax_section (i->owner, i, &link_info, &again))
1998 einfo ("%P%F: can't relax section: %E\n");
2002 dot = size_input_section (prev,
2003 output_section_statement,
2004 output_section_statement->fill,
2008 case lang_input_statement_enum:
2010 case lang_fill_statement_enum:
2011 s->fill_statement.output_section = output_section_statement->bfd_section;
2013 fill = s->fill_statement.fill;
2015 case lang_assignment_statement_enum:
2017 bfd_vma newdot = dot;
2019 exp_fold_tree (s->assignment_statement.exp,
2020 output_section_statement,
2021 lang_allocating_phase_enum,
2025 if (newdot != dot && !relax)
2027 /* The assignment changed dot. Insert a pad. */
2028 if (output_section_statement == abs_output_section)
2030 /* If we don't have an output section, then just adjust
2031 the default memory address. */
2032 lang_memory_region_lookup ("*default*")->current = newdot;
2036 lang_statement_union_type *new =
2037 ((lang_statement_union_type *)
2038 stat_alloc (sizeof (lang_padding_statement_type)));
2040 /* Link into existing chain */
2041 new->header.next = *prev;
2043 new->header.type = lang_padding_statement_enum;
2044 new->padding_statement.output_section =
2045 output_section_statement->bfd_section;
2046 new->padding_statement.output_offset =
2047 dot - output_section_statement->bfd_section->vma;
2048 new->padding_statement.fill = fill;
2049 new->padding_statement.size = newdot - dot;
2050 output_section_statement->bfd_section->_raw_size +=
2051 new->padding_statement.size;
2059 case lang_padding_statement_enum:
2060 /* If we are relaxing, and this is not the first pass, some
2061 padding statements may have been inserted during previous
2062 passes. We may have to move the padding statement to a new
2063 location if dot has a different value at this point in this
2064 pass than it did at this point in the previous pass. */
2065 s->padding_statement.output_offset =
2066 dot - output_section_statement->bfd_section->vma;
2067 dot += s->padding_statement.size;
2068 output_section_statement->bfd_section->_raw_size +=
2069 s->padding_statement.size;
2072 case lang_group_statement_enum:
2073 dot = lang_size_sections (s->group_statement.children.head,
2074 output_section_statement,
2075 &s->group_statement.children.head,
2083 /* This can only get here when relaxing is turned on */
2085 case lang_address_statement_enum:
2088 prev = &s->header.next;
2094 lang_do_assignments (s, output_section_statement, fill, dot)
2095 lang_statement_union_type * s;
2096 lang_output_section_statement_type * output_section_statement;
2100 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2102 switch (s->header.type)
2104 case lang_constructors_statement_enum:
2105 dot = lang_do_assignments (constructor_list.head,
2106 output_section_statement,
2111 case lang_output_section_statement_enum:
2113 lang_output_section_statement_type *os =
2114 &(s->output_section_statement);
2116 if (os->bfd_section != NULL)
2118 dot = os->bfd_section->vma;
2119 (void) lang_do_assignments (os->children.head, os,
2121 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2125 /* If nothing has been placed into the output section then
2126 it won't have a bfd_section. */
2127 if (os->bfd_section)
2129 os->bfd_section->lma
2130 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2135 case lang_wild_statement_enum:
2137 dot = lang_do_assignments (s->wild_statement.children.head,
2138 output_section_statement,
2143 case lang_object_symbols_statement_enum:
2144 case lang_output_statement_enum:
2145 case lang_target_statement_enum:
2147 case lang_common_statement_enum:
2150 case lang_data_statement_enum:
2152 etree_value_type value;
2154 value = exp_fold_tree (s->data_statement.exp,
2156 lang_final_phase_enum, dot, &dot);
2157 s->data_statement.value = value.value;
2158 if (value.valid == false)
2159 einfo ("%F%P: invalid data statement\n");
2161 switch (s->data_statement.type)
2178 case lang_reloc_statement_enum:
2180 etree_value_type value;
2182 value = exp_fold_tree (s->reloc_statement.addend_exp,
2184 lang_final_phase_enum, dot, &dot);
2185 s->reloc_statement.addend_value = value.value;
2186 if (value.valid == false)
2187 einfo ("%F%P: invalid reloc statement\n");
2189 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2192 case lang_input_section_enum:
2194 asection *in = s->input_section.section;
2196 if (in->_cooked_size != 0)
2197 dot += in->_cooked_size;
2199 dot += in->_raw_size;
2203 case lang_input_statement_enum:
2205 case lang_fill_statement_enum:
2206 fill = s->fill_statement.fill;
2208 case lang_assignment_statement_enum:
2210 exp_fold_tree (s->assignment_statement.exp,
2211 output_section_statement,
2212 lang_final_phase_enum,
2218 case lang_padding_statement_enum:
2219 dot += s->padding_statement.size;
2222 case lang_group_statement_enum:
2223 dot = lang_do_assignments (s->group_statement.children.head,
2224 output_section_statement,
2232 case lang_address_statement_enum:
2240 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2241 operator .startof. (section_name), it produces an undefined symbol
2242 .startof.section_name. Similarly, when it sees
2243 .sizeof. (section_name), it produces an undefined symbol
2244 .sizeof.section_name. For all the output sections, we look for
2245 such symbols, and set them to the correct value. */
2252 for (s = output_bfd->sections; s != NULL; s = s->next)
2254 const char *secname;
2256 struct bfd_link_hash_entry *h;
2258 secname = bfd_get_section_name (output_bfd, s);
2259 buf = xmalloc (10 + strlen (secname));
2261 sprintf (buf, ".startof.%s", secname);
2262 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2263 if (h != NULL && h->type == bfd_link_hash_undefined)
2265 h->type = bfd_link_hash_defined;
2266 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2267 h->u.def.section = bfd_abs_section_ptr;
2270 sprintf (buf, ".sizeof.%s", secname);
2271 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2272 if (h != NULL && h->type == bfd_link_hash_undefined)
2274 h->type = bfd_link_hash_defined;
2275 if (s->_cooked_size != 0)
2276 h->u.def.value = s->_cooked_size;
2278 h->u.def.value = s->_raw_size;
2279 h->u.def.section = bfd_abs_section_ptr;
2289 struct bfd_link_hash_entry *h;
2292 if (link_info.relocateable || link_info.shared)
2297 if (entry_symbol == (char *) NULL)
2299 /* No entry has been specified. Look for start, but don't warn
2300 if we don't find it. */
2301 entry_symbol = "start";
2305 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2306 if (h != (struct bfd_link_hash_entry *) NULL
2307 && (h->type == bfd_link_hash_defined
2308 || h->type == bfd_link_hash_defweak)
2309 && h->u.def.section->output_section != NULL)
2313 val = (h->u.def.value
2314 + bfd_get_section_vma (output_bfd,
2315 h->u.def.section->output_section)
2316 + h->u.def.section->output_offset);
2317 if (! bfd_set_start_address (output_bfd, val))
2318 einfo ("%P%F:%s: can't set start address\n", entry_symbol);
2324 /* Can't find the entry symbol. Use the first address in the
2326 ts = bfd_get_section_by_name (output_bfd, ".text");
2327 if (ts != (asection *) NULL)
2330 einfo ("%P: warning: cannot find entry symbol %s; defaulting to %V\n",
2331 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2332 if (! bfd_set_start_address (output_bfd,
2333 bfd_get_section_vma (output_bfd, ts)))
2334 einfo ("%P%F: can't set start address\n");
2339 einfo ("%P: warning: cannot find entry symbol %s; not setting start address\n",
2345 /* Check that the architecture of all the input files is compatible
2346 with the output file. Also call the backend to let it do any
2347 other checking that is needed. */
2352 lang_statement_union_type *file;
2354 CONST bfd_arch_info_type *compatible;
2356 for (file = file_chain.head;
2357 file != (lang_statement_union_type *) NULL;
2358 file = file->input_statement.next)
2360 input_bfd = file->input_statement.the_bfd;
2361 compatible = bfd_arch_get_compatible (input_bfd,
2363 if (compatible == NULL)
2364 einfo ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2365 bfd_printable_name (input_bfd), input_bfd,
2366 bfd_printable_name (output_bfd));
2369 bfd_merge_private_bfd_data (input_bfd, output_bfd);
2373 /* Look through all the global common symbols and attach them to the
2374 correct section. The -sort-common command line switch may be used
2375 to roughly sort the entries by size. */
2380 if (link_info.relocateable
2381 && ! command_line.force_common_definition)
2384 if (! config.sort_common)
2385 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
2390 for (power = 4; power >= 0; power--)
2391 bfd_link_hash_traverse (link_info.hash, lang_one_common,
2396 /* Place one common symbol in the correct section. */
2399 lang_one_common (h, info)
2400 struct bfd_link_hash_entry *h;
2403 unsigned int power_of_two;
2407 if (h->type != bfd_link_hash_common)
2411 power_of_two = h->u.c.p->alignment_power;
2413 if (config.sort_common
2414 && power_of_two < (unsigned int) *(int *) info)
2417 section = h->u.c.p->section;
2419 /* Increase the size of the section. */
2420 section->_raw_size = ALIGN_N (section->_raw_size,
2421 (bfd_size_type) (1 << power_of_two));
2423 /* Adjust the alignment if necessary. */
2424 if (power_of_two > section->alignment_power)
2425 section->alignment_power = power_of_two;
2427 /* Change the symbol from common to defined. */
2428 h->type = bfd_link_hash_defined;
2429 h->u.def.section = section;
2430 h->u.def.value = section->_raw_size;
2432 /* Increase the size of the section. */
2433 section->_raw_size += size;
2435 /* Make sure the section is allocated in memory. */
2436 section->flags |= SEC_ALLOC;
2438 if (config.map_file != NULL)
2439 fprintf (config.map_file, "Allocating common %s: %lx at %lx %s\n",
2440 h->root.string, (unsigned long) size,
2441 (unsigned long) h->u.def.value, section->owner->filename);
2447 run through the input files and ensure that every input
2448 section has somewhere to go. If one is found without
2449 a destination then create an input request and place it
2450 into the statement tree.
2454 lang_place_orphans ()
2456 lang_input_statement_type *file;
2458 for (file = (lang_input_statement_type *) file_chain.head;
2459 file != (lang_input_statement_type *) NULL;
2460 file = (lang_input_statement_type *) file->next)
2464 for (s = file->the_bfd->sections;
2465 s != (asection *) NULL;
2468 if (s->output_section == (asection *) NULL)
2470 /* This section of the file is not attatched, root
2471 around for a sensible place for it to go */
2473 if (file->just_syms_flag)
2475 /* We are only retrieving symbol values from this
2476 file. We want the symbols to act as though the
2477 values in the file are absolute. */
2478 s->output_section = bfd_abs_section_ptr;
2479 s->output_offset = s->vma;
2481 else if (file->common_section == s)
2483 /* This is a lonely common section which must
2484 have come from an archive. We attatch to the
2485 section with the wildcard */
2486 if (! link_info.relocateable
2487 && ! command_line.force_common_definition)
2489 if (default_common_section ==
2490 (lang_output_section_statement_type *) NULL)
2492 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2494 default_common_section =
2495 lang_output_section_statement_lookup (".bss");
2498 wild_doit (&default_common_section->children, s,
2499 default_common_section, file);
2502 else if (ldemul_place_orphan (file, s))
2506 lang_output_section_statement_type *os =
2507 lang_output_section_statement_lookup (s->name);
2509 wild_doit (&os->children, s, os, file);
2518 lang_set_flags (ptr, flags)
2522 boolean state = false;
2537 /* ptr->flag_read = state; */
2540 /* ptr->flag_write = state; */
2543 /* ptr->flag_executable= state;*/
2547 /* ptr->flag_loadable= state;*/
2550 einfo ("%P%F: invalid syntax in flags\n");
2557 /* Call a function on each input file. This function will be called
2558 on an archive, but not on the elements. */
2561 lang_for_each_input_file (func)
2562 void (*func) PARAMS ((lang_input_statement_type *));
2564 lang_input_statement_type *f;
2566 for (f = (lang_input_statement_type *) input_file_chain.head;
2568 f = (lang_input_statement_type *) f->next_real_file)
2572 /* Call a function on each file. The function will be called on all
2573 the elements of an archive which are included in the link, but will
2574 not be called on the archive file itself. */
2577 lang_for_each_file (func)
2578 void (*func) PARAMS ((lang_input_statement_type *));
2580 lang_input_statement_type *f;
2582 for (f = (lang_input_statement_type *) file_chain.head;
2583 f != (lang_input_statement_type *) NULL;
2584 f = (lang_input_statement_type *) f->next)
2595 lang_for_each_input_section (func)
2596 void (*func) PARAMS ((bfd * ab, asection * as));
2598 lang_input_statement_type *f;
2600 for (f = (lang_input_statement_type *) file_chain.head;
2601 f != (lang_input_statement_type *) NULL;
2602 f = (lang_input_statement_type *) f->next)
2606 for (s = f->the_bfd->sections;
2607 s != (asection *) NULL;
2610 func (f->the_bfd, s);
2618 ldlang_add_file (entry)
2619 lang_input_statement_type * entry;
2623 lang_statement_append (&file_chain,
2624 (lang_statement_union_type *) entry,
2627 /* The BFD linker needs to have a list of all input BFDs involved in
2629 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
2630 ASSERT (entry->the_bfd != output_bfd);
2631 for (pp = &link_info.input_bfds;
2632 *pp != (bfd *) NULL;
2633 pp = &(*pp)->link_next)
2635 *pp = entry->the_bfd;
2636 entry->the_bfd->usrdata = (PTR) entry;
2637 bfd_set_gp_size (entry->the_bfd, g_switch_value);
2641 lang_add_output (name, from_script)
2645 /* Make -o on command line override OUTPUT in script. */
2646 if (had_output_filename == false || !from_script)
2648 output_filename = name;
2649 had_output_filename = true;
2654 static lang_output_section_statement_type *current_section;
2666 for (l = 0; l < 32; l++)
2668 if (i >= (unsigned int) x)
2676 lang_enter_output_section_statement (output_section_statement_name,
2677 address_exp, flags, block_value,
2678 align, subalign, ebase)
2679 const char *output_section_statement_name;
2680 etree_type * address_exp;
2682 bfd_vma block_value;
2684 etree_type *subalign;
2687 lang_output_section_statement_type *os;
2691 lang_output_section_statement_lookup (output_section_statement_name);
2695 /* Add this statement to tree */
2696 /* add_statement(lang_output_section_statement_enum,
2697 output_section_statement);*/
2698 /* Make next things chain into subchain of this */
2700 if (os->addr_tree ==
2701 (etree_type *) NULL)
2707 if (flags & SEC_NEVER_LOAD)
2711 os->block_value = block_value ? block_value : 1;
2712 stat_ptr = &os->children;
2714 os->subsection_alignment = topower(
2715 exp_get_value_int(subalign, -1,
2716 "subsection alignment",
2718 os->section_alignment = topower(
2719 exp_get_value_int(align, -1,
2720 "section alignment", 0));
2722 os->load_base = ebase;
2729 lang_output_statement_type *new =
2730 new_stat (lang_output_statement, stat_ptr);
2732 new->name = output_filename;
2735 /* Reset the current counters in the regions */
2737 reset_memory_regions ()
2739 lang_memory_region_type *p = lang_memory_region_list;
2741 for (p = lang_memory_region_list;
2742 p != (lang_memory_region_type *) NULL;
2745 p->old_length = (bfd_size_type) (p->current - p->origin);
2746 p->current = p->origin;
2753 lang_reasonable_defaults ();
2754 current_target = default_target;
2756 lang_for_each_statement (ldlang_open_output); /* Open the output file */
2758 ldemul_create_output_section_statements ();
2760 /* Add to the hash table all undefineds on the command line */
2761 lang_place_undefineds ();
2763 /* Create a bfd for each input file */
2764 current_target = default_target;
2765 open_input_bfds (statement_list.head, false);
2767 ldemul_after_open ();
2769 /* Build all sets based on the information gathered from the input
2771 ldctor_build_sets ();
2773 /* Size up the common data */
2776 /* Run through the contours of the script and attatch input sections
2777 to the correct output sections
2779 map_input_to_output_sections (statement_list.head, (char *) NULL,
2780 (lang_output_section_statement_type *) NULL);
2783 /* Find any sections not attatched explicitly and handle them */
2784 lang_place_orphans ();
2786 ldemul_before_allocation ();
2788 /* We must record the program headers before we try to fix the
2789 section positions, since they will affect SIZEOF_HEADERS. */
2790 lang_record_phdrs ();
2792 /* Now run around and relax if we can */
2793 if (command_line.relax)
2795 /* First time round is a trial run to get the 'worst case'
2796 addresses of the objects if there was no relaxing. */
2797 lang_size_sections (statement_list.head,
2799 &(statement_list.head), 0, (bfd_vma) 0, false);
2801 /* Keep relaxing until bfd_relax_section gives up. */
2804 reset_memory_regions ();
2806 relax_again = false;
2808 /* Do all the assignments with our current guesses as to
2810 lang_do_assignments (statement_list.head,
2812 (fill_type) 0, (bfd_vma) 0);
2814 /* Perform another relax pass - this time we know where the
2815 globals are, so can make better guess. */
2816 lang_size_sections (statement_list.head,
2818 &(statement_list.head), 0, (bfd_vma) 0, true);
2820 while (relax_again);
2824 /* Size up the sections. */
2825 lang_size_sections (statement_list.head,
2827 &(statement_list.head), 0, (bfd_vma) 0, false);
2830 /* See if anything special should be done now we know how big
2832 ldemul_after_allocation ();
2834 /* Fix any .startof. or .sizeof. symbols. */
2835 lang_set_startof ();
2837 /* Do all the assignments, now that we know the final restingplaces
2838 of all the symbols */
2840 lang_do_assignments (statement_list.head,
2842 (fill_type) 0, (bfd_vma) 0);
2844 /* Make sure that we're not mixing architectures */
2854 /* EXPORTED TO YACC */
2857 lang_add_wild (section_name, filename)
2858 CONST char *CONST section_name;
2859 CONST char *CONST filename;
2861 lang_wild_statement_type *new = new_stat (lang_wild_statement,
2864 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2866 placed_commons = true;
2868 if (filename != (char *) NULL)
2870 lang_has_input_file = true;
2872 new->section_name = section_name;
2873 new->filename = filename;
2874 lang_list_init (&new->children);
2878 lang_section_start (name, address)
2880 etree_type * address;
2882 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2884 ad->section_name = name;
2885 ad->address = address;
2888 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
2889 because of a -e argument on the command line, or zero if this is
2890 called by ENTRY in a linker script. Command line arguments take
2893 /* WINDOWS_NT. When an entry point has been specified, we will also force
2894 this symbol to be defined by calling ldlang_add_undef (equivalent to
2895 having switch -u entry_name on the command line). The reason we do
2896 this is so that the user doesn't have to because they would have to use
2897 the -u switch if they were specifying an entry point other than
2898 _mainCRTStartup. Specifically, if creating a windows application, entry
2899 point _WinMainCRTStartup must be specified.
2900 What I have found for non console applications (entry not _mainCRTStartup)
2901 is that the .obj that contains mainCRTStartup is brought in since it is
2902 the first encountered in libc.lib and it has other symbols in it which will
2903 be pulled in by the link process. To avoid this, adding -u with the entry
2904 point name specified forces the correct .obj to be used. We can avoid
2905 making the user do this by always adding the entry point name as an
2906 undefined symbol. */
2909 lang_add_entry (name, cmdline)
2913 if (entry_symbol == NULL
2915 || ! entry_from_cmdline)
2917 entry_symbol = name;
2918 entry_from_cmdline = cmdline;
2921 /* don't do this yet. It seems to work (the executables run), but the
2922 image created is very different from what I was getting before indicating
2923 that something else is being pulled in. When everything else is working,
2924 then try to put this back in to see if it will do the right thing for
2925 other more complicated applications */
2926 ldlang_add_undef (name);
2931 lang_add_target (name)
2934 lang_target_statement_type *new = new_stat (lang_target_statement,
2950 map_option_f = true;
2961 lang_fill_statement_type *new = new_stat (lang_fill_statement,
2968 lang_add_data (type, exp)
2970 union etree_union *exp;
2973 lang_data_statement_type *new = new_stat (lang_data_statement,
2981 /* Create a new reloc statement. RELOC is the BFD relocation type to
2982 generate. HOWTO is the corresponding howto structure (we could
2983 look this up, but the caller has already done so). SECTION is the
2984 section to generate a reloc against, or NAME is the name of the
2985 symbol to generate a reloc against. Exactly one of SECTION and
2986 NAME must be NULL. ADDEND is an expression for the addend. */
2989 lang_add_reloc (reloc, howto, section, name, addend)
2990 bfd_reloc_code_real_type reloc;
2991 reloc_howto_type *howto;
2994 union etree_union *addend;
2996 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3000 p->section = section;
3002 p->addend_exp = addend;
3004 p->addend_value = 0;
3005 p->output_section = NULL;
3010 lang_add_assignment (exp)
3013 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3020 lang_add_attribute (attribute)
3021 enum statement_enum attribute;
3023 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
3030 if (startup_file != (char *) NULL)
3032 einfo ("%P%Fmultiple STARTUP files\n");
3034 first_file->filename = name;
3035 first_file->local_sym_name = name;
3036 first_file->real = true;
3038 startup_file = name;
3045 lang_float_flag = maybe;
3049 lang_leave_output_section_statement (fill, memspec)
3051 CONST char *memspec;
3053 current_section->fill = fill;
3054 current_section->region = lang_memory_region_lookup (memspec);
3055 stat_ptr = &statement_list;
3059 Create an absolute symbol with the given name with the value of the
3060 address of first byte of the section named.
3062 If the symbol already exists, then do nothing.
3065 lang_abs_symbol_at_beginning_of (secname, name)
3066 const char *secname;
3069 struct bfd_link_hash_entry *h;
3071 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3072 if (h == (struct bfd_link_hash_entry *) NULL)
3073 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3075 if (h->type == bfd_link_hash_new
3076 || h->type == bfd_link_hash_undefined)
3080 h->type = bfd_link_hash_defined;
3082 sec = bfd_get_section_by_name (output_bfd, secname);
3083 if (sec == (asection *) NULL)
3086 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
3088 h->u.def.section = bfd_abs_section_ptr;
3093 Create an absolute symbol with the given name with the value of the
3094 address of the first byte after the end of the section named.
3096 If the symbol already exists, then do nothing.
3099 lang_abs_symbol_at_end_of (secname, name)
3100 const char *secname;
3103 struct bfd_link_hash_entry *h;
3105 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
3106 if (h == (struct bfd_link_hash_entry *) NULL)
3107 einfo ("%P%F: bfd_link_hash_lookup failed: %E\n");
3109 if (h->type == bfd_link_hash_new
3110 || h->type == bfd_link_hash_undefined)
3114 h->type = bfd_link_hash_defined;
3116 sec = bfd_get_section_by_name (output_bfd, secname);
3117 if (sec == (asection *) NULL)
3120 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
3121 + bfd_section_size (output_bfd, sec));
3123 h->u.def.section = bfd_abs_section_ptr;
3128 lang_statement_append (list, element, field)
3129 lang_statement_list_type * list;
3130 lang_statement_union_type * element;
3131 lang_statement_union_type ** field;
3133 *(list->tail) = element;
3137 /* Set the output format type. -oformat overrides scripts. */
3140 lang_add_output_format (format, big, little, from_script)
3146 if (output_target == NULL || !from_script)
3148 if (command_line.endian == ENDIAN_BIG
3151 else if (command_line.endian == ENDIAN_LITTLE
3155 output_target = format;
3159 /* Enter a group. This creates a new lang_group_statement, and sets
3160 stat_ptr to build new statements within the group. */
3165 lang_group_statement_type *g;
3167 g = new_stat (lang_group_statement, stat_ptr);
3168 lang_list_init (&g->children);
3169 stat_ptr = &g->children;
3172 /* Leave a group. This just resets stat_ptr to start writing to the
3173 regular list of statements again. Note that this will not work if
3174 groups can occur inside anything else which can adjust stat_ptr,
3175 but currently they can't. */
3180 stat_ptr = &statement_list;
3183 /* Add a new program header. This is called for each entry in a PHDRS
3184 command in a linker script. */
3187 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
3195 struct lang_phdr *n, **pp;
3197 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
3200 n->type = exp_get_value_int (type, 0, "program header type",
3201 lang_final_phase_enum);
3202 n->filehdr = filehdr;
3207 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
3212 /* Record that a section should be placed in a phdr. */
3215 lang_section_in_phdr (name)
3218 struct lang_output_section_phdr_list *n;
3220 n = ((struct lang_output_section_phdr_list *)
3221 stat_alloc (sizeof (struct lang_output_section_phdr_list)));
3224 n->next = current_section->phdrs;
3225 current_section->phdrs = n;
3228 /* Record the program header information in the output BFD. FIXME: We
3229 should not be calling an ELF specific function here. */
3232 lang_record_phdrs ()
3236 struct lang_output_section_phdr_list *last;
3237 struct lang_phdr *l;
3238 lang_statement_union_type *u;
3241 secs = xmalloc (alc * sizeof (asection *));
3243 for (l = lang_phdr_list; l != NULL; l = l->next)
3250 for (u = lang_output_section_statement.head;
3252 u = u->output_section_statement.next)
3254 lang_output_section_statement_type *os;
3255 struct lang_output_section_phdr_list *pl;
3257 os = &u->output_section_statement;
3265 || os->bfd_section == NULL
3266 || (os->bfd_section->flags & SEC_ALLOC) == 0)
3271 if (os->bfd_section == NULL)
3274 for (; pl != NULL; pl = pl->next)
3276 if (strcmp (pl->name, l->name) == 0)
3281 secs = xrealloc (secs, alc * sizeof (asection *));
3283 secs[c] = os->bfd_section;
3290 if (l->flags == NULL)
3293 flags = exp_get_vma (l->flags, 0, "phdr flags",
3294 lang_final_phase_enum);
3299 at = exp_get_vma (l->at, 0, "phdr load address",
3300 lang_final_phase_enum);
3302 if (! bfd_record_phdr (output_bfd, l->type,
3303 l->flags == NULL ? false : true,
3305 l->at == NULL ? false : true,
3306 at, l->filehdr, l->phdrs, c, secs))
3307 einfo ("%F%P: bfd_record_phdr failed: %E\n");
3312 /* Make sure all the phdr assignments succeeded. */
3313 for (u = lang_output_section_statement.head;
3315 u = u->output_section_statement.next)
3317 struct lang_output_section_phdr_list *pl;
3319 if (u->output_section_statement.bfd_section == NULL)
3322 for (pl = u->output_section_statement.phdrs;
3325 if (! pl->used && strcmp (pl->name, "NONE") != 0)
3326 einfo ("%X%P: section `%s' assigned to non-existent phdr `%s'\n",
3327 u->output_section_statement.name, pl->name);