1 /* Linker command language support.
2 Copyright 1991, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
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 ldmalloc
49 #define obstack_chunk_free free
50 static CONST char *startup_file;
51 static lang_statement_list_type input_file_chain;
53 /* Points to the last statement in the .data section, so we can add
54 stuff to the data section without pain */
55 static lang_statement_list_type end_of_data_section_statement_list;
57 /* List of statements needed to handle constructors */
58 extern lang_statement_list_type constructor_list;
60 static boolean placed_commons = false;
61 static lang_output_section_statement_type *default_common_section;
62 static boolean map_option_f;
63 static bfd_vma print_dot;
64 static lang_input_statement_type *first_file;
65 static lang_statement_list_type lang_output_section_statement;
66 static CONST char *current_target;
67 static CONST char *output_target;
68 static size_t longest_section_name = 8;
69 static section_userdata_type common_section_userdata;
70 static lang_statement_list_type statement_list;
74 lang_output_section_statement_type *abs_output_section;
75 lang_statement_list_type *stat_ptr = &statement_list;
76 lang_input_statement_type *script_file = 0;
77 lang_statement_list_type file_chain =
79 CONST char *entry_symbol = 0;
80 bfd_size_type largest_section = 0;
81 boolean lang_has_input_file = false;
82 lang_output_section_statement_type *create_object_symbols = 0;
83 boolean had_output_filename = false;
84 boolean lang_float_flag = false;
85 boolean delete_output_file_on_failure = false;
88 extern char *default_target;
90 extern CONST char *output_filename;
91 extern char *current_file;
92 extern bfd *output_bfd;
93 extern enum bfd_architecture ldfile_output_architecture;
94 extern unsigned long ldfile_output_machine;
95 extern char *ldfile_output_machine_name;
96 extern ldsym_type *symbol_head;
97 extern unsigned int commons_pending;
98 extern args_type command_line;
99 extern ld_config_type config;
100 extern boolean write_map;
101 extern int g_switch_value;
104 etree_type *base; /* Relocation base - or null */
108 #define cat(a,b) a##b
110 #define cat(a,b) a/**/b
113 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
115 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
117 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
119 void lang_add_data PARAMS ((int type, union etree_union * exp));
125 return obstack_alloc (&stat_obstack, size);
131 fprintf (config.map_file, "%5x", (unsigned) value);
134 print_alignment (value)
137 fprintf (config.map_file, "2**%1u", value);
140 DEFUN (print_fill, (value),
143 fprintf (config.map_file, "%04x", (unsigned) value);
149 CONST char *CONST name;
151 fprintf (config.map_file, "%*s", -longest_section_name, name);
154 /*----------------------------------------------------------------------
155 lang_for_each_statement walks the parse tree and calls the provided
156 function for each node
160 lang_for_each_statement_worker (func, s)
162 lang_statement_union_type *s;
164 for (; s != (lang_statement_union_type *) NULL; s = s->next)
168 switch (s->header.type)
170 case lang_constructors_statement_enum:
171 lang_for_each_statement_worker (func, constructor_list.head);
173 case lang_output_section_statement_enum:
174 lang_for_each_statement_worker
176 s->output_section_statement.children.head);
178 case lang_wild_statement_enum:
179 lang_for_each_statement_worker
181 s->wild_statement.children.head);
183 case lang_data_statement_enum:
184 case lang_object_symbols_statement_enum:
185 case lang_output_statement_enum:
186 case lang_target_statement_enum:
187 case lang_input_section_enum:
188 case lang_input_statement_enum:
189 case lang_assignment_statement_enum:
190 case lang_padding_statement_enum:
191 case lang_address_statement_enum:
201 lang_for_each_statement (func)
204 lang_for_each_statement_worker (func,
205 statement_list.head);
208 /*----------------------------------------------------------------------*/
210 lang_list_init (list)
211 lang_statement_list_type *list;
213 list->head = (lang_statement_union_type *) NULL;
214 list->tail = &list->head;
217 /*----------------------------------------------------------------------
219 build a new statement node for the parse tree
224 lang_statement_union_type *
225 new_statement (type, size, list)
226 enum statement_enum type;
228 lang_statement_list_type * list;
230 lang_statement_union_type *new = (lang_statement_union_type *)
233 new->header.type = type;
234 new->header.next = (lang_statement_union_type *) NULL;
235 lang_statement_append (list, new, &new->header.next);
240 Build a new input file node for the language. There are several ways
241 in which we treat an input file, eg, we only look at symbols, or
242 prefix it with a -l etc.
244 We can be supplied with requests for input files more than once;
245 they may, for example be split over serveral lines like foo.o(.text)
246 foo.o(.data) etc, so when asked for a file we check that we havn't
247 got it already so we don't duplicate the bfd.
250 static lang_input_statement_type *
251 new_afile (name, file_type, target)
252 CONST char *CONST name;
253 CONST lang_input_file_enum_type file_type;
254 CONST char *CONST target;
257 lang_input_statement_type *p = new_stat (lang_input_statement,
260 lang_has_input_file = true;
262 p->complained = false;
265 case lang_input_file_is_symbols_only_enum:
267 p->is_archive = false;
269 p->local_sym_name = name;
270 p->just_syms_flag = true;
271 p->search_dirs_flag = false;
273 case lang_input_file_is_fake_enum:
275 p->is_archive = false;
277 p->local_sym_name = name;
278 p->just_syms_flag = false;
279 p->search_dirs_flag = false;
281 case lang_input_file_is_l_enum:
282 p->is_archive = true;
285 p->local_sym_name = concat ("-l", name, "");
286 p->just_syms_flag = false;
287 p->search_dirs_flag = true;
289 case lang_input_file_is_search_file_enum:
290 case lang_input_file_is_marker_enum:
292 p->is_archive = false;
294 p->local_sym_name = name;
295 p->just_syms_flag = false;
296 p->search_dirs_flag = true;
298 case lang_input_file_is_file_enum:
300 p->is_archive = false;
302 p->local_sym_name = name;
303 p->just_syms_flag = false;
304 p->search_dirs_flag = false;
309 p->asymbols = (asymbol **) NULL;
310 p->superfile = (lang_input_statement_type *) NULL;
311 p->next_real_file = (lang_statement_union_type *) NULL;
312 p->next = (lang_statement_union_type *) NULL;
314 p->common_output_section = (asection *) NULL;
315 lang_statement_append (&input_file_chain,
316 (lang_statement_union_type *) p,
321 lang_input_statement_type *
322 lang_add_input_file (name, file_type, target)
324 lang_input_file_enum_type file_type;
327 /* Look it up or build a new one */
328 lang_has_input_file = true;
331 lang_input_statement_type *p;
333 for (p = (lang_input_statement_type *) input_file_chain.head;
334 p != (lang_input_statement_type *) NULL;
335 p = (lang_input_statement_type *) (p->next_real_file))
337 /* Sometimes we have incomplete entries in here */
338 if (p->filename != (char *) NULL)
340 if (strcmp (name, p->filename) == 0)
346 return new_afile (name, file_type, target);
350 lang_add_keepsyms_file (filename)
351 CONST char *filename;
353 extern strip_symbols_type strip_symbols;
354 if (keepsyms_file != 0)
355 info_msg ("%X%P: error: duplicated keep-symbols-file value\n");
356 keepsyms_file = filename;
357 if (strip_symbols != STRIP_NONE)
358 info_msg ("%P: `-keep-only-symbols-file' overrides `-s' and `-S'\n");
359 strip_symbols = STRIP_SOME;
362 /* Build enough state so that the parser can build its tree */
366 obstack_begin (&stat_obstack, 1000);
368 stat_ptr = &statement_list;
370 lang_list_init (stat_ptr);
372 lang_list_init (&input_file_chain);
373 lang_list_init (&lang_output_section_statement);
374 lang_list_init (&file_chain);
375 first_file = lang_add_input_file ((char *) NULL,
376 lang_input_file_is_marker_enum,
378 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
380 abs_output_section->bfd_section = &bfd_abs_section;
384 /*----------------------------------------------------------------------
385 A region is an area of memory declared with the
386 MEMORY { name:org=exp, len=exp ... }
389 We maintain a list of all the regions here
391 If no regions are specified in the script, then the default is used
392 which is created when looked up to be the entire data space
395 static lang_memory_region_type *lang_memory_region_list;
396 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
398 lang_memory_region_type *
399 lang_memory_region_lookup (name)
400 CONST char *CONST name;
403 lang_memory_region_type *p = lang_memory_region_list;
405 for (p = lang_memory_region_list;
406 p != (lang_memory_region_type *) NULL;
409 if (strcmp (p->name, name) == 0)
414 if (strcmp (name, "*default*") == 0)
416 /* This is the default region, dig out first one on the list */
417 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
419 return lang_memory_region_list;
423 lang_memory_region_type *new =
424 (lang_memory_region_type *) stat_alloc ((bfd_size_type) (sizeof (lang_memory_region_type)));
426 new->name = buystring (name);
427 new->next = (lang_memory_region_type *) NULL;
429 *lang_memory_region_list_tail = new;
430 lang_memory_region_list_tail = &new->next;
432 new->length = ~(bfd_size_type)0;
434 new->had_full_message = false;
441 lang_output_section_statement_type *
442 lang_output_section_find (name)
443 CONST char *CONST name;
445 lang_statement_union_type *u;
446 lang_output_section_statement_type *lookup;
448 for (u = lang_output_section_statement.head;
449 u != (lang_statement_union_type *) NULL;
452 lookup = &u->output_section_statement;
453 if (strcmp (name, lookup->name) == 0)
458 return (lang_output_section_statement_type *) NULL;
461 lang_output_section_statement_type *
462 lang_output_section_statement_lookup (name)
463 CONST char *CONST name;
465 lang_output_section_statement_type *lookup;
467 lookup = lang_output_section_find (name);
468 if (lookup == (lang_output_section_statement_type *) NULL)
471 lookup = (lang_output_section_statement_type *)
472 new_stat (lang_output_section_statement, stat_ptr);
473 lookup->region = (lang_memory_region_type *) NULL;
475 lookup->block_value = 1;
478 lookup->next = (lang_statement_union_type *) NULL;
479 lookup->bfd_section = (asection *) NULL;
480 lookup->processed = false;
481 lookup->loadable = 1;
482 lookup->addr_tree = (etree_type *) NULL;
483 lang_list_init (&lookup->children);
485 lookup->memspec = (CONST char *) NULL;
487 lookup->subsection_alignment = -1;
488 lookup->section_alignment = -1;
489 lookup->load_base = (union etree_union *) NULL;
491 lang_statement_append (&lang_output_section_statement,
492 (lang_statement_union_type *) lookup,
500 print_flags (ignore_flags)
503 fprintf (config.map_file, "(");
505 if (flags->flag_read)
506 fprintf (outfile, "R");
507 if (flags->flag_write)
508 fprintf (outfile, "W");
509 if (flags->flag_executable)
510 fprintf (outfile, "X");
511 if (flags->flag_loadable)
512 fprintf (outfile, "L");
514 fprintf (config.map_file, ")");
520 lang_memory_region_type *m;
522 fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
524 fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
526 fprintf (config.map_file,
527 "name\t\torigin length r_size c_size is attributes\n");
530 for (m = lang_memory_region_list;
531 m != (lang_memory_region_type *) NULL;
534 fprintf (config.map_file, "%-16s", m->name);
535 print_address (m->origin);
537 print_address ((bfd_vma)m->length);
539 print_address ((bfd_vma)m->old_length);
541 print_address (m->current - m->origin);
544 fprintf(config.map_file," %2d%% ", ( m->current - m->origin) * 100 / m->old_length);
545 print_flags (&m->flags);
546 fprintf (config.map_file, "\n");
548 fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
549 fprintf (config.map_file, "output input virtual\n");
550 fprintf (config.map_file, "section section address tsize\n\n");
561 lang_output_section_statement_type * s;
563 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
564 section_userdata_type *new =
565 (section_userdata_type *)
566 stat_alloc ((bfd_size_type) (sizeof (section_userdata_type)));
568 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
569 if (s->bfd_section == (asection *) NULL)
570 s->bfd_section = bfd_make_section (output_bfd, s->name);
571 if (s->bfd_section == (asection *) NULL)
573 einfo ("%P%F: output format %s cannot represent section called %s\n",
574 output_bfd->xvec->name, s->name);
576 s->bfd_section->output_section = s->bfd_section;
577 /* s->bfd_section->flags = s->flags;*/
579 /* We initialize an output sections output offset to minus its own */
580 /* vma to allow us to output a section through itself */
581 s->bfd_section->output_offset = 0;
582 get_userdata (s->bfd_section) = (PTR) new;
586 /***********************************************************************
589 These expand statements like *(.text) and foo.o to a list of
590 explicit actions, like foo.o(.text), bar.o(.text) and
593 The toplevel routine, wild, takes a statement, section, file and
594 target. If either the section or file is null it is taken to be the
595 wildcard. Seperate lang_input_section statements are created for
596 each part of the expanstion, and placed after the statement provided.
601 wild_doit (ptr, section, output, file)
602 lang_statement_list_type * ptr;
604 lang_output_section_statement_type * output;
605 lang_input_statement_type * file;
607 if (output->bfd_section == (asection *) NULL)
610 /* Initialize the vma and size to the existing section. This will
611 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
613 if (section != (asection *) NULL)
615 bfd_set_section_vma (0, output->bfd_section,
616 bfd_section_vma (0, section));
617 output->bfd_section->_raw_size = section->_raw_size;
621 if (section != (asection *) NULL
622 && section->output_section == (asection *) NULL)
624 /* Add a section reference to the list */
625 lang_input_section_type *new = new_stat (lang_input_section, ptr);
627 new->section = section;
629 section->output_section = output->bfd_section;
631 /* Be selective about what the output section inherits from the
634 if ((section->flags & SEC_SHARED_LIBRARY) != 0)
635 section->output_section->flags |= section->flags;
637 section->output_section->flags |= section->flags & ~SEC_NEVER_LOAD;
639 if (!output->loadable)
641 /* Turn off load flag */
642 output->bfd_section->flags &= ~SEC_LOAD;
643 output->bfd_section->flags |= SEC_NEVER_LOAD;
645 if (section->alignment_power > output->bfd_section->alignment_power)
647 output->bfd_section->alignment_power = section->alignment_power;
649 /* If supplied an aligmnet, then force it */
650 if (output->section_alignment != -1)
652 output->bfd_section->alignment_power = output->section_alignment;
658 our_bfd_get_section_by_name (abfd, section)
662 return bfd_get_section_by_name (abfd, section);
666 wild_section (ptr, section, file, output)
667 lang_wild_statement_type * ptr;
669 lang_input_statement_type * file;
670 lang_output_section_statement_type * output;
674 if (file->just_syms_flag == false)
676 if (section == (char *) NULL)
678 /* Do the creation to all sections in the file */
679 for (s = file->the_bfd->sections; s != (asection *) NULL; s = s->next)
682 if ((s->flags & SEC_IS_COMMON) == 0)
684 wild_doit (&ptr->children, s, output, file);
690 /* Do the creation to the named section only */
691 wild_doit (&ptr->children,
692 our_bfd_get_section_by_name (file->the_bfd, section),
698 /* passed a file name (which must have been seen already and added to
699 the statement tree. We will see if it has been opened already and
700 had its symbols read. If not then we'll read it.
702 Archives are pecuilar here. We may open them once, but if they do
703 not define anything we need at the time, they won't have all their
704 symbols read. If we need them later, we'll have to redo it.
707 lang_input_statement_type *
709 CONST char *CONST name;
711 lang_input_statement_type *search;
713 for (search = (lang_input_statement_type *) input_file_chain.head;
714 search != (lang_input_statement_type *) NULL;
715 search = (lang_input_statement_type *) search->next_real_file)
717 if (search->filename == (char *) NULL && name == (char *) NULL)
721 if (search->filename != (char *) NULL && name != (char *) NULL)
723 if (strcmp (search->filename, name) == 0)
725 ldmain_open_file_read_symbol (search);
731 /* There isn't an afile entry for this file yet, this must be
732 because the name has only appeared inside a load script and not
733 on the command line */
734 search = new_afile (name, lang_input_file_is_file_enum, default_target);
735 ldmain_open_file_read_symbol (search);
742 wild (s, section, file, target, output)
743 lang_wild_statement_type * s;
744 CONST char *CONST section;
745 CONST char *CONST file;
746 CONST char *CONST target;
747 lang_output_section_statement_type * output;
749 lang_input_statement_type *f;
751 if (file == (char *) NULL)
753 /* Perform the iteration over all files in the list */
754 for (f = (lang_input_statement_type *) file_chain.head;
755 f != (lang_input_statement_type *) NULL;
756 f = (lang_input_statement_type *) f->next)
758 wild_section (s, section, f, output);
760 /* Once more for the script file */
761 wild_section(s, section, script_file, output);
765 /* Perform the iteration over a single file */
766 wild_section (s, section, lookup_name (file), output);
768 if (section != (char *) NULL
769 && strcmp (section, "COMMON") == 0
770 && default_common_section == (lang_output_section_statement_type *) NULL)
772 /* Remember the section that common is going to incase we later
773 get something which doesn't know where to put it */
774 default_common_section = output;
779 read in all the files
784 CONST char *CONST name;
786 extern unsigned long ldfile_output_machine;
787 extern enum bfd_architecture ldfile_output_architecture;
791 if (output_target == (char *) NULL)
793 if (current_target != (char *) NULL)
794 output_target = current_target;
796 output_target = default_target;
798 output = bfd_openw (name, output_target);
800 if (output == (bfd *) NULL)
802 if (bfd_error == invalid_target)
804 einfo ("%P%F: target %s not found\n", output_target);
806 einfo ("%P%F: cannot open output file %s: %E\n", name);
809 delete_output_file_on_failure = 1;
811 /* output->flags |= D_PAGED;*/
813 if (! bfd_set_format (output, bfd_object))
814 einfo ("%P%F:%s: can not make object file: %E\n", name);
815 if (! bfd_set_arch_mach (output,
816 ldfile_output_architecture,
817 ldfile_output_machine))
818 einfo ("%P%F:%s: can not set architecture: %E\n", name);
820 bfd_set_gp_size (output, g_switch_value);
828 ldlang_open_output (statement)
829 lang_statement_union_type * statement;
831 switch (statement->header.type)
833 case lang_output_statement_enum:
834 output_bfd = open_output (statement->output_statement.name);
835 ldemul_set_output_arch ();
836 if (config.magic_demand_paged && !config.relocateable_output)
837 output_bfd->flags |= D_PAGED;
839 output_bfd->flags &= ~D_PAGED;
840 if (config.text_read_only)
841 output_bfd->flags |= WP_TEXT;
843 output_bfd->flags &= ~WP_TEXT;
846 case lang_target_statement_enum:
847 current_target = statement->target_statement.target;
855 open_input_bfds (statement)
856 lang_statement_union_type * statement;
858 switch (statement->header.type)
860 case lang_target_statement_enum:
861 current_target = statement->target_statement.target;
863 case lang_wild_statement_enum:
864 /* Maybe we should load the file's symbols */
865 if (statement->wild_statement.filename)
867 (void) lookup_name (statement->wild_statement.filename);
870 case lang_input_statement_enum:
871 if (statement->input_statement.real == true)
873 statement->input_statement.target = current_target;
874 lookup_name (statement->input_statement.filename);
882 /* If there are [COMMONS] statements, put a wild one into the bss section */
885 lang_reasonable_defaults ()
891 lang_output_section_statement_lookup (".text");
892 lang_output_section_statement_lookup (".data");
894 default_common_section =
895 lang_output_section_statement_lookup (".bss");
898 if (placed_commons == false)
900 lang_wild_statement_type *new =
901 new_stat (lang_wild_statement,
902 &default_common_section->children);
904 new->section_name = "COMMON";
905 new->filename = (char *) NULL;
906 lang_list_init (&new->children);
913 Add the supplied name to the symbol table as an undefined reference.
914 Remove items from the chain as we open input bfds
916 typedef struct ldlang_undef_chain_list
918 struct ldlang_undef_chain_list *next;
920 } ldlang_undef_chain_list_type;
922 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
925 ldlang_add_undef (name)
926 CONST char *CONST name;
928 ldlang_undef_chain_list_type *new =
929 (ldlang_undef_chain_list_type
930 *) stat_alloc ((bfd_size_type) (sizeof (ldlang_undef_chain_list_type)));
932 new->next = ldlang_undef_chain_list_head;
933 ldlang_undef_chain_list_head = new;
935 new->name = buystring (name);
938 /* Run through the list of undefineds created above and place them
939 into the linker hash table as undefined symbols belonging to the
943 lang_place_undefineds ()
945 ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head;
947 while (ptr != (ldlang_undef_chain_list_type *) NULL)
950 asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **)));
952 def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd);
954 def->name = ptr->name;
955 def->section = &bfd_und_section;
956 enter_global_ref (def_ptr, ptr->name);
961 /* Copy important data from out internal form to the bfd way. Also
962 create a section for the dummy file
966 lang_create_output_section_statements ()
968 lang_statement_union_type *os;
970 for (os = lang_output_section_statement.head;
971 os != (lang_statement_union_type *) NULL;
972 os = os->output_section_statement.next)
974 lang_output_section_statement_type *s =
975 &os->output_section_statement;
983 lang_init_script_file ()
985 script_file = lang_add_input_file ("command line",
986 lang_input_file_is_fake_enum,
988 script_file->the_bfd = bfd_create ("command line", output_bfd);
989 script_file->symbol_count = 0;
990 script_file->the_bfd->sections = 0;
992 /* The user data of a bfd points to the input statement attatched */
993 script_file->the_bfd->usrdata = (void *)script_file;
994 script_file->common_section =
995 bfd_make_section(script_file->the_bfd,"COMMON");
998 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
1000 abs_output_section->bfd_section = &bfd_abs_section;
1004 /* Open input files and attatch to output sections */
1006 map_input_to_output_sections (s, target, output_section_statement)
1007 lang_statement_union_type * s;
1009 lang_output_section_statement_type * output_section_statement;
1011 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1013 switch (s->header.type)
1017 case lang_wild_statement_enum:
1018 wild (&s->wild_statement, s->wild_statement.section_name,
1019 s->wild_statement.filename, target,
1020 output_section_statement);
1023 case lang_constructors_statement_enum:
1024 map_input_to_output_sections (constructor_list.head,
1026 output_section_statement);
1028 case lang_output_section_statement_enum:
1029 map_input_to_output_sections (s->output_section_statement.children.head,
1031 &s->output_section_statement);
1033 case lang_output_statement_enum:
1035 case lang_target_statement_enum:
1036 target = s->target_statement.target;
1038 case lang_fill_statement_enum:
1039 case lang_input_section_enum:
1040 case lang_object_symbols_statement_enum:
1041 case lang_data_statement_enum:
1042 case lang_assignment_statement_enum:
1043 case lang_padding_statement_enum:
1045 case lang_afile_asection_pair_statement_enum:
1048 case lang_address_statement_enum:
1049 /* Mark the specified section with the supplied address */
1051 lang_output_section_statement_type *os =
1052 lang_output_section_statement_lookup
1053 (s->address_statement.section_name);
1055 os->addr_tree = s->address_statement.address;
1056 if (os->bfd_section == (asection *) NULL)
1058 einfo ("%P%F: cannot set the address of undefined section %s\n",
1059 s->address_statement.section_name);
1063 case lang_input_statement_enum:
1064 /* A standard input statement, has no wildcards */
1065 /* ldmain_open_file_read_symbol(&s->input_statement);*/
1076 print_output_section_statement (output_section_statement)
1077 lang_output_section_statement_type * output_section_statement;
1079 asection *section = output_section_statement->bfd_section;
1082 print_section (output_section_statement->name);
1087 print_dot = section->vma;
1091 print_address (section->vma);
1093 print_size (section->_raw_size);
1095 print_size(section->_cooked_size);
1097 print_alignment (section->alignment_power);
1100 fprintf (config.map_file, "%s flags", output_section_statement->region->name);
1101 print_flags (stdout, &output_section_statement->flags);
1103 if (section->flags & SEC_LOAD)
1104 fprintf (config.map_file, "load ");
1105 if (section->flags & SEC_ALLOC)
1106 fprintf (config.map_file, "alloc ");
1107 if (section->flags & SEC_RELOC)
1108 fprintf (config.map_file, "reloc ");
1109 if (section->flags & SEC_HAS_CONTENTS)
1110 fprintf (config.map_file, "contents ");
1115 fprintf (config.map_file, "No attached output section");
1118 if (output_section_statement->load_base)
1120 int b = exp_get_value_int(output_section_statement->load_base,
1121 0, "output base", lang_final_phase_enum);
1122 printf("Output address %08x\n", b);
1124 if (output_section_statement->section_alignment >= 0
1125 || output_section_statement->section_alignment >= 0)
1127 printf("\t\t\t\t\tforced alignment ");
1128 if ( output_section_statement->section_alignment >= 0)
1130 printf("section 2**%d ",output_section_statement->section_alignment );
1132 if ( output_section_statement->subsection_alignment >= 0)
1134 printf("subsection 2**%d ",output_section_statement->subsection_alignment );
1139 print_statement (output_section_statement->children.head,
1140 output_section_statement);
1145 print_assignment (assignment, output_section)
1146 lang_assignment_statement_type * assignment;
1147 lang_output_section_statement_type * output_section;
1149 etree_value_type result;
1155 print_address (print_dot);
1157 result = exp_fold_tree (assignment->exp->assign.src,
1159 lang_final_phase_enum,
1165 print_address (result.value);
1169 fprintf (config.map_file, "*undefined*");
1172 exp_print_tree (assignment->exp);
1174 fprintf (config.map_file, "\n");
1178 print_input_statement (statm)
1179 lang_input_statement_type * statm;
1181 if (statm->filename != (char *) NULL)
1183 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1192 fprintf (config.map_file, " ");
1194 fprintf (config.map_file, " ");
1195 print_address (outside_symbol_address (q));
1196 fprintf (config.map_file, " %s", q->name ? q->name : " ");
1197 if (q->flags & BSF_WEAK)
1198 fprintf (config.map_file, " *weak*");
1203 print_input_section (in)
1204 lang_input_section_type * in;
1206 asection *i = in->section;
1207 int size = i->reloc_done ?
1208 bfd_get_section_size_after_reloc (i) :
1209 bfd_get_section_size_before_reloc (i);
1214 fprintf (config.map_file, " ");
1215 print_section (i->name);
1216 fprintf (config.map_file, " ");
1217 if (i->output_section)
1219 print_address (i->output_section->vma + i->output_offset);
1220 fprintf (config.map_file, " ");
1221 print_size (i->_raw_size);
1222 fprintf (config.map_file, " ");
1223 print_size(i->_cooked_size);
1224 fprintf (config.map_file, " ");
1225 print_alignment (i->alignment_power);
1226 fprintf (config.map_file, " ");
1230 bfd *abfd = in->ifile->the_bfd;
1232 if (in->ifile->just_syms_flag == true)
1234 fprintf (config.map_file, "symbols only ");
1237 fprintf (config.map_file, " %s ", abfd->xvec->name);
1238 if (abfd->my_archive != (bfd *) NULL)
1240 fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
1245 fprintf (config.map_file, "%s", abfd->filename);
1247 fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
1250 /* Find all the symbols in this file defined in this section */
1252 if (in->ifile->symbol_count)
1256 for (p = in->ifile->asymbols; *p; p++)
1260 if (bfd_get_section (q) == i
1261 && (q->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
1274 print_dot = outside_section_address (i) + size;
1278 fprintf (config.map_file, "No output section allocated\n");
1284 print_fill_statement (fill)
1285 lang_fill_statement_type * fill;
1287 fprintf (config.map_file, "FILL mask ");
1288 print_fill (fill->fill);
1292 print_data_statement (data)
1293 lang_data_statement_type * data;
1295 /* bfd_vma value; */
1300 /* ASSERT(print_dot == data->output_vma);*/
1302 print_address (data->output_vma + data->output_section->vma);
1304 print_address (data->value);
1309 fprintf (config.map_file, "BYTE ");
1310 print_dot += BYTE_SIZE;
1313 fprintf (config.map_file, "SHORT ");
1314 print_dot += SHORT_SIZE;
1317 fprintf (config.map_file, "LONG ");
1318 print_dot += LONG_SIZE;
1322 exp_print_tree (data->exp);
1324 fprintf (config.map_file, "\n");
1329 print_padding_statement (s)
1330 lang_padding_statement_type * s;
1334 print_section ("*fill*");
1336 print_address (s->output_offset + s->output_section->vma);
1338 print_size (s->size);
1340 print_fill (s->fill);
1343 print_dot = s->output_offset + s->output_section->vma + s->size;
1348 print_wild_statement (w, os)
1349 lang_wild_statement_type * w;
1350 lang_output_section_statement_type * os;
1352 fprintf (config.map_file, " from ");
1353 if (w->filename != (char *) NULL)
1355 fprintf (config.map_file, "%s", w->filename);
1359 fprintf (config.map_file, "*");
1361 if (w->section_name != (char *) NULL)
1363 fprintf (config.map_file, "(%s)", w->section_name);
1367 fprintf (config.map_file, "(*)");
1370 print_statement (w->children.head, os);
1374 print_statement (s, os)
1375 lang_statement_union_type * s;
1376 lang_output_section_statement_type * os;
1380 switch (s->header.type)
1382 case lang_constructors_statement_enum:
1383 fprintf (config.map_file, "constructors:\n");
1384 print_statement (constructor_list.head, os);
1386 case lang_wild_statement_enum:
1387 print_wild_statement (&s->wild_statement, os);
1390 fprintf (config.map_file, "Fail with %d\n", s->header.type);
1393 case lang_address_statement_enum:
1394 fprintf (config.map_file, "address\n");
1396 case lang_object_symbols_statement_enum:
1397 fprintf (config.map_file, "object symbols\n");
1399 case lang_fill_statement_enum:
1400 print_fill_statement (&s->fill_statement);
1402 case lang_data_statement_enum:
1403 print_data_statement (&s->data_statement);
1405 case lang_input_section_enum:
1406 print_input_section (&s->input_section);
1408 case lang_padding_statement_enum:
1409 print_padding_statement (&s->padding_statement);
1411 case lang_output_section_statement_enum:
1412 print_output_section_statement (&s->output_section_statement);
1414 case lang_assignment_statement_enum:
1415 print_assignment (&s->assignment_statement,
1418 case lang_target_statement_enum:
1419 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1421 case lang_output_statement_enum:
1422 fprintf (config.map_file, "OUTPUT(%s %s)\n",
1423 s->output_statement.name,
1424 output_target ? output_target : "");
1426 case lang_input_statement_enum:
1427 print_input_statement (&s->input_statement);
1429 case lang_afile_asection_pair_statement_enum:
1441 print_statement (statement_list.head,
1442 abs_output_section);
1447 DEFUN (insert_pad, (this_ptr, fill, power, output_section_statement, dot),
1448 lang_statement_union_type ** this_ptr AND
1450 unsigned int power AND
1451 asection * output_section_statement AND
1454 /* Align this section first to the
1455 input sections requirement, then
1456 to the output section's requirement.
1457 If this alignment is > than any seen before,
1458 then record it too. Perform the alignment by
1459 inserting a magic 'padding' statement.
1462 unsigned int alignment_needed = align_power (dot, power) - dot;
1464 if (alignment_needed != 0)
1466 lang_statement_union_type *new =
1467 (lang_statement_union_type *)
1468 stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
1470 /* Link into existing chain */
1471 new->header.next = *this_ptr;
1473 new->header.type = lang_padding_statement_enum;
1474 new->padding_statement.output_section = output_section_statement;
1475 new->padding_statement.output_offset =
1476 dot - output_section_statement->vma;
1477 new->padding_statement.fill = fill;
1478 new->padding_statement.size = alignment_needed;
1482 /* Remember the most restrictive alignment */
1483 if (power > output_section_statement->alignment_power)
1485 output_section_statement->alignment_power = power;
1487 output_section_statement->_raw_size += alignment_needed;
1488 return alignment_needed + dot;
1492 /* Work out how much this section will move the dot point */
1494 DEFUN (size_input_section, (this_ptr, output_section_statement, fill, dot, relax),
1495 lang_statement_union_type ** this_ptr AND
1496 lang_output_section_statement_type * output_section_statement AND
1497 unsigned short fill AND
1501 lang_input_section_type *is = &((*this_ptr)->input_section);
1502 asection *i = is->section;
1504 if (is->ifile->just_syms_flag == false)
1506 if (output_section_statement->subsection_alignment != -1)
1507 i->alignment_power =
1508 output_section_statement->subsection_alignment;
1510 dot = insert_pad (this_ptr, fill, i->alignment_power,
1511 output_section_statement->bfd_section, dot);
1513 /* remember the largest size so we can malloc the largest area
1514 needed for the output stage. Only remember the size of sections
1515 which we will actually allocate */
1516 if ((i->flags & SEC_HAS_CONTENTS) != 0
1517 && (bfd_get_section_size_before_reloc (i) > largest_section))
1519 largest_section = bfd_get_section_size_before_reloc (i);
1522 /* Remember where in the output section this input section goes */
1524 i->output_offset = dot - output_section_statement->bfd_section->vma;
1526 /* Mark how big the output section must be to contain this now
1530 dot += i->_cooked_size;
1534 dot += i->_raw_size;
1536 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
1540 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1546 /* Sizing happens in two passes, first pass we allocate worst case
1547 stuff. The second pass (if relaxing), we use what we learnt to
1548 change the size of some relocs from worst case to better
1550 static boolean had_relax;
1553 DEFUN (lang_size_sections, (s, output_section_statement, prev, fill, dot, relax),
1554 lang_statement_union_type * s AND
1555 lang_output_section_statement_type * output_section_statement AND
1556 lang_statement_union_type ** prev AND
1557 unsigned short fill AND
1561 /* Size up the sections from their constituent parts */
1562 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1564 switch (s->header.type)
1567 case lang_output_section_statement_enum:
1570 lang_output_section_statement_type *os = &s->output_section_statement;
1572 /* If this is a shared library section, don't change the size
1574 if (os->bfd_section->flags & SEC_SHARED_LIBRARY)
1577 if (os->bfd_section == &bfd_abs_section)
1579 /* No matter what happens, an abs section starts at zero */
1580 bfd_set_section_vma (0, os->bfd_section, 0);
1584 if (os->addr_tree == (etree_type *) NULL)
1586 /* No address specified for this section, get one
1587 from the region specification
1589 if (os->region == (lang_memory_region_type *) NULL)
1591 os->region = lang_memory_region_lookup ("*default*");
1593 dot = os->region->current;
1599 r = exp_fold_tree (os->addr_tree,
1601 lang_allocating_phase_enum,
1603 if (r.valid == false)
1605 einfo ("%F%S: non constant address expression for section %s\n",
1610 /* The section starts here */
1611 /* First, align to what the section needs */
1614 dot = align_power (dot, os->bfd_section->alignment_power);
1615 bfd_set_section_vma (0, os->bfd_section, dot);
1617 if (os->load_base) {
1618 os->bfd_section->lma
1619 = exp_get_value_int(os->load_base, 0,"load base", lang_final_phase_enum);
1624 os->bfd_section->output_offset = 0;
1626 (void) lang_size_sections (os->children.head, os, &os->children.head,
1627 os->fill, dot, relax);
1628 /* Ignore the size of the input sections, use the vma and size to */
1631 after = ALIGN_N (os->bfd_section->vma +
1632 os->bfd_section->_raw_size,
1633 /* The coercion here is important, see ld.h. */
1634 (bfd_vma) os->block_value);
1636 os->bfd_section->_raw_size = after - os->bfd_section->vma;
1637 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1638 os->processed = true;
1640 /* Replace into region ? */
1641 if (os->addr_tree == (etree_type *) NULL
1642 && os->region != (lang_memory_region_type *) NULL)
1644 os->region->current = dot;
1645 /* Make sure this isn't silly */
1646 if (( os->region->current
1647 > os->region->origin + os->region->length)
1648 || ( os->region->origin > os->region->current ))
1650 einfo ("%X%P: region %s is full (%B section %s)\n",
1652 os->bfd_section->owner,
1653 os->bfd_section->name);
1654 /* Reset the region pointer */
1655 os->region->current = 0;
1663 case lang_constructors_statement_enum:
1664 dot = lang_size_sections (constructor_list.head,
1665 output_section_statement,
1666 &s->wild_statement.children.head,
1671 case lang_data_statement_enum:
1673 unsigned int size = 0;
1675 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1676 s->data_statement.output_section =
1677 output_section_statement->bfd_section;
1679 switch (s->data_statement.type)
1693 output_section_statement->bfd_section->_raw_size += size;
1697 case lang_wild_statement_enum:
1699 dot = lang_size_sections (s->wild_statement.children.head,
1700 output_section_statement,
1701 &s->wild_statement.children.head,
1707 case lang_object_symbols_statement_enum:
1708 create_object_symbols = output_section_statement;
1710 case lang_output_statement_enum:
1711 case lang_target_statement_enum:
1713 case lang_input_section_enum:
1718 if( relax_section (prev))
1724 (*prev)->input_section.section->_cooked_size =
1725 (*prev)->input_section.section->_raw_size ;
1728 dot = size_input_section (prev,
1729 output_section_statement,
1730 output_section_statement->fill,
1733 case lang_input_statement_enum:
1735 case lang_fill_statement_enum:
1736 s->fill_statement.output_section = output_section_statement->bfd_section;
1738 fill = s->fill_statement.fill;
1740 case lang_assignment_statement_enum:
1742 bfd_vma newdot = dot;
1744 exp_fold_tree (s->assignment_statement.exp,
1745 output_section_statement,
1746 lang_allocating_phase_enum,
1750 if (newdot != dot && !relax)
1751 /* We've been moved ! so insert a pad */
1753 lang_statement_union_type *new =
1754 (lang_statement_union_type *)
1755 stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
1757 /* Link into existing chain */
1758 new->header.next = *prev;
1760 new->header.type = lang_padding_statement_enum;
1761 new->padding_statement.output_section =
1762 output_section_statement->bfd_section;
1763 new->padding_statement.output_offset =
1764 dot - output_section_statement->bfd_section->vma;
1765 new->padding_statement.fill = fill;
1766 new->padding_statement.size = newdot - dot;
1767 output_section_statement->bfd_section->_raw_size +=
1768 new->padding_statement.size;
1777 /* This can only get here when relaxing is turned on */
1778 case lang_padding_statement_enum:
1780 case lang_address_statement_enum:
1783 prev = &s->header.next;
1789 DEFUN (lang_do_assignments, (s, output_section_statement, fill, dot),
1790 lang_statement_union_type * s AND
1791 lang_output_section_statement_type * output_section_statement AND
1792 unsigned short fill AND
1796 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1798 switch (s->header.type)
1800 case lang_constructors_statement_enum:
1801 dot = lang_do_assignments (constructor_list.head,
1802 output_section_statement,
1807 case lang_output_section_statement_enum:
1809 lang_output_section_statement_type *os =
1810 &(s->output_section_statement);
1812 dot = os->bfd_section->vma;
1813 (void) lang_do_assignments (os->children.head, os, os->fill, dot);
1814 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1817 case lang_wild_statement_enum:
1819 dot = lang_do_assignments (s->wild_statement.children.head,
1820 output_section_statement,
1825 case lang_object_symbols_statement_enum:
1826 case lang_output_statement_enum:
1827 case lang_target_statement_enum:
1829 case lang_common_statement_enum:
1832 case lang_data_statement_enum:
1834 etree_value_type value;
1836 value = exp_fold_tree (s->data_statement.exp,
1838 lang_final_phase_enum, dot, &dot);
1839 s->data_statement.value = value.value;
1840 if (value.valid == false)
1841 einfo ("%F%P: invalid data statement\n");
1843 switch (s->data_statement.type)
1856 case lang_input_section_enum:
1858 asection *in = s->input_section.section;
1860 dot += bfd_get_section_size_before_reloc (in);
1864 case lang_input_statement_enum:
1866 case lang_fill_statement_enum:
1867 fill = s->fill_statement.fill;
1869 case lang_assignment_statement_enum:
1871 exp_fold_tree (s->assignment_statement.exp,
1872 output_section_statement,
1873 lang_final_phase_enum,
1879 case lang_padding_statement_enum:
1880 dot += s->padding_statement.size;
1885 case lang_address_statement_enum:
1896 lang_relocate_globals ()
1899 Each ldsym_type maintains a chain of pointers to asymbols which
1900 references the definition. Replace each pointer to the referenence
1901 with a pointer to only one place, preferably the definition. If
1902 the defintion isn't available then the common symbol, and if
1903 there isn't one of them then choose one reference.
1906 FOR_EACH_LDSYM (lgs)
1910 /* Skip indirect symbols. */
1911 if (lgs->flags & SYM_INDIRECT)
1914 if (lgs->sdefs_chain)
1916 it = *(lgs->sdefs_chain);
1918 else if (lgs->scoms_chain != (asymbol **) NULL)
1920 it = *(lgs->scoms_chain);
1922 else if (lgs->srefs_chain != (asymbol **) NULL)
1924 it = *(lgs->srefs_chain);
1928 /* This can happen when the command line asked for a symbol to
1930 it = (asymbol *) NULL;
1932 if (it != (asymbol *) NULL)
1935 asymbol **ptr = lgs->srefs_chain;;
1936 if (lgs->flags & SYM_WARNING)
1938 produce_warnings (lgs, it);
1941 while (ptr != (asymbol **) NULL
1944 asymbol *ref = *ptr;
1947 ptr = (asymbol **) (ref->udata);
1959 int warn = config.relocateable_output != true;
1960 if (entry_symbol == (char *) NULL)
1962 /* No entry has been specified, look for start, but don't warn */
1963 entry_symbol = "start";
1966 lgs = ldsym_get_soft (entry_symbol);
1967 if (lgs && lgs->sdefs_chain)
1969 asymbol *sy = *(lgs->sdefs_chain);
1971 /* We can set the entry address*/
1972 bfd_set_start_address (output_bfd,
1973 outside_symbol_address (sy));
1978 /* Cannot find anything reasonable,
1979 use the first address in the text section
1981 asection *ts = bfd_get_section_by_name (output_bfd, ".text");
1985 einfo ("%P: warning: cannot find entry symbol %s, defaulting to %V\n",
1986 entry_symbol, ts->vma);
1988 bfd_set_start_address (output_bfd, ts->vma);
1993 einfo ("%P: warning: cannot find entry symbol %s, not setting start address\n",
1999 /* By now we know the target architecture, and we may have an */
2000 /* ldfile_output_machine_name */
2004 lang_statement_union_type *file;
2006 unsigned long input_machine;
2007 enum bfd_architecture input_architecture;
2008 CONST bfd_arch_info_type *compatible;
2010 for (file = file_chain.head;
2011 file != (lang_statement_union_type *) NULL;
2012 file = file->input_statement.next)
2014 input_bfd = file->input_statement.the_bfd;
2016 input_machine = bfd_get_mach (input_bfd);
2017 input_architecture = bfd_get_arch (input_bfd);
2020 /* Inspect the architecture and ensure we're linking like with
2023 compatible = bfd_arch_get_compatible (input_bfd,
2028 ldfile_output_machine = compatible->mach;
2029 ldfile_output_architecture = compatible->arch;
2034 info_msg ("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n",
2035 bfd_printable_name (input_bfd), input_bfd,
2036 bfd_printable_name (output_bfd));
2038 if (! bfd_set_arch_mach (output_bfd,
2041 einfo ("%P%F:%s: can't set architecture: %E\n",
2042 bfd_get_filename (output_bfd));
2049 * run through all the global common symbols and tie them
2050 * to the output section requested.
2052 As an experiment we do this 4 times, once for all the byte sizes,
2053 then all the two bytes, all the four bytes and then everything else
2062 if (config.relocateable_output == false ||
2063 command_line.force_common_definition == true)
2065 for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<= 1)
2067 for (lgs = symbol_head;
2068 lgs != (ldsym_type *) NULL;
2072 unsigned int power_of_two;
2076 if (lgs->scoms_chain != (asymbol **) NULL)
2078 com = *(lgs->scoms_chain);
2108 if (config.sort_common == false || align == power)
2112 /* Change from a common symbol into a definition of
2114 lgs->sdefs_chain = lgs->scoms_chain;
2115 lgs->scoms_chain = (asymbol **) NULL;
2118 /* Point to the correct common section */
2119 symbfd = bfd_asymbol_bfd (com);
2120 if (com->section == &bfd_com_section)
2122 ((lang_input_statement_type *) symbfd->usrdata)
2129 name = bfd_get_section_name (symbfd,
2131 newsec = bfd_get_section_by_name (symbfd,
2133 /* This section should have been created by
2134 enter_file_symbols if it did not already
2136 if (newsec == (asection *) NULL)
2137 einfo ("%P%F: no output section %s\n", name);
2138 com->section = newsec;
2141 /* Fix the size of the common section */
2143 com->section->_raw_size =
2144 ALIGN_N (com->section->_raw_size,
2145 /* The coercion here is important, see ld.h. */
2148 /* Remember if this is the biggest alignment ever seen */
2149 if (power_of_two > com->section->alignment_power)
2151 com->section->alignment_power = power_of_two;
2154 /* Symbol stops being common and starts being global, but
2155 we remember that it was common once. */
2157 com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON;
2158 com->value = com->section->_raw_size;
2160 if (write_map && config.map_file)
2162 fprintf (config.map_file, "Allocating common %s: %x at %x %s\n",
2165 (unsigned) com->value,
2166 bfd_asymbol_bfd(com)->filename);
2169 com->section->_raw_size += size;
2182 run through the input files and ensure that every input
2183 section has somewhere to go. If one is found without
2184 a destination then create an input request and place it
2185 into the statement tree.
2189 lang_place_orphans ()
2191 lang_input_statement_type *file;
2193 for (file = (lang_input_statement_type *) file_chain.head;
2194 file != (lang_input_statement_type *) NULL;
2195 file = (lang_input_statement_type *) file->next)
2199 for (s = file->the_bfd->sections;
2200 s != (asection *) NULL;
2203 if (s->output_section == (asection *) NULL)
2205 /* This section of the file is not attatched, root
2206 around for a sensible place for it to go */
2208 if (file->common_section == s)
2210 /* This is a lonely common section which must
2211 have come from an archive. We attatch to the
2212 section with the wildcard */
2213 if (config.relocateable_output != true
2214 && command_line.force_common_definition == false)
2216 if (default_common_section ==
2217 (lang_output_section_statement_type *) NULL)
2219 info_msg ("%P: no [COMMON] command, defaulting to .bss\n");
2221 default_common_section =
2222 lang_output_section_statement_lookup (".bss");
2225 wild_doit (&default_common_section->children, s,
2226 default_common_section, file);
2231 lang_output_section_statement_type *os =
2232 lang_output_section_statement_lookup (s->name);
2234 wild_doit (&os->children, s, os, file);
2243 lang_set_flags (ptr, flags)
2247 boolean state = false;
2262 /* ptr->flag_read = state; */
2265 /* ptr->flag_write = state; */
2268 /* ptr->flag_executable= state;*/
2272 /* ptr->flag_loadable= state;*/
2275 einfo ("%P%F: invalid syntax in flags\n");
2285 lang_for_each_file (func)
2286 void (*func) PARAMS ((lang_input_statement_type *));
2288 lang_input_statement_type *f;
2290 for (f = (lang_input_statement_type *) file_chain.head;
2291 f != (lang_input_statement_type *) NULL;
2292 f = (lang_input_statement_type *) f->next)
2300 lang_for_each_input_section (func)
2301 void (*func) PARAMS ((bfd * ab, asection * as));
2303 lang_input_statement_type *f;
2305 for (f = (lang_input_statement_type *) file_chain.head;
2306 f != (lang_input_statement_type *) NULL;
2307 f = (lang_input_statement_type *) f->next)
2311 for (s = f->the_bfd->sections;
2312 s != (asection *) NULL;
2315 func (f->the_bfd, s);
2323 ldlang_add_file (entry)
2324 lang_input_statement_type * entry;
2327 lang_statement_append (&file_chain,
2328 (lang_statement_union_type *) entry,
2333 lang_add_output (name, from_script)
2337 /* Make -o on command line override OUTPUT in script. */
2338 if (had_output_filename == false || !from_script)
2340 output_filename = name;
2341 had_output_filename = true;
2346 static lang_output_section_statement_type *current_section;
2348 static int topower(x)
2353 if (x < 0) return -1;
2354 for (l = 0; l < 32; l++)
2356 if (i >= x) return l;
2362 lang_enter_output_section_statement (output_section_statement_name,
2363 address_exp, flags, block_value,
2364 align, subalign, base)
2365 char *output_section_statement_name;
2366 etree_type * address_exp;
2368 bfd_vma block_value;
2370 etree_type *subalign;
2373 lang_output_section_statement_type *os;
2377 lang_output_section_statement_lookup (output_section_statement_name);
2381 /* Add this statement to tree */
2382 /* add_statement(lang_output_section_statement_enum,
2383 output_section_statement);*/
2384 /* Make next things chain into subchain of this */
2386 if (os->addr_tree ==
2387 (etree_type *) NULL)
2393 if (flags & SEC_NEVER_LOAD)
2397 os->block_value = block_value ? block_value : 1;
2398 stat_ptr = &os->children;
2400 os->subsection_alignment = topower(
2401 exp_get_value_int(subalign, -1,
2402 "subsection alignment",
2404 os->section_alignment = topower(
2405 exp_get_value_int(align, -1,
2406 "section alignment", 0));
2408 os->load_base = base;
2415 lang_output_statement_type *new =
2416 new_stat (lang_output_statement, stat_ptr);
2418 new->name = output_filename;
2421 /* Reset the current counters in the regions */
2423 reset_memory_regions ()
2425 lang_memory_region_type *p = lang_memory_region_list;
2427 for (p = lang_memory_region_list;
2428 p != (lang_memory_region_type *) NULL;
2431 p->old_length = (bfd_size_type) (p->current - p->origin);
2432 p->current = p->origin;
2439 DEFUN (create_symbol, (name, flags, section),
2440 CONST char *name AND
2444 extern lang_input_statement_type *script_file;
2445 asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **)));
2447 /* Add this definition to script file */
2448 asymbol *def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd);
2449 def->name = buystring (name);
2452 def->section = section;
2454 enter_global_ref (def_ptr, name);
2461 lang_reasonable_defaults ();
2462 current_target = default_target;
2464 lang_for_each_statement (ldlang_open_output); /* Open the output file */
2465 /* For each output section statement, create a section in the output
2467 lang_create_output_section_statements ();
2469 ldemul_create_output_section_statements ();
2471 /* Create a dummy bfd for the script */
2472 lang_init_script_file ();
2474 /* Add to the hash table all undefineds on the command line */
2475 lang_place_undefineds ();
2477 /* Create a bfd for each input file */
2478 current_target = default_target;
2479 lang_for_each_statement (open_input_bfds);
2481 /* Run through the contours of the script and attatch input sections
2482 to the correct output sections
2484 find_constructors ();
2485 map_input_to_output_sections (statement_list.head, (char *) NULL,
2486 (lang_output_section_statement_type *) NULL);
2489 /* Find any sections not attatched explicitly and handle them */
2490 lang_place_orphans ();
2492 /* Size up the common data */
2495 ldemul_before_allocation ();
2505 lang_size_sections (statement_list.head,
2506 (lang_output_section_statement_type *) NULL,
2507 &(statement_list.head), 0, (bfd_vma) 0, true);
2508 /* FIXME. Until the code in relax is fixed so that it only reads in
2509 stuff once, we cant iterate since there is no way for the linker to
2510 know what has been patched and what hasn't */
2516 /* Now run around and relax if we can */
2517 if (command_line.relax)
2519 /* First time round is a trial run to get the 'worst case'
2520 addresses of the objects if there was no relaxing. */
2521 lang_size_sections (statement_list.head,
2522 (lang_output_section_statement_type *) NULL,
2523 &(statement_list.head), 0, (bfd_vma) 0, false);
2525 /* Move the global symbols around so the second pass of relaxing
2527 lang_relocate_globals ();
2529 reset_memory_regions ();
2531 /* Do all the assignments, now that we know the final resting
2532 places of all the symbols. */
2534 lang_do_assignments (statement_list.head,
2538 /* Perform another relax pass - this time we know where the
2539 globals are, so can make better guess. */
2540 lang_size_sections (statement_list.head,
2541 (lang_output_section_statement_type *) NULL,
2542 &(statement_list.head), 0, (bfd_vma) 0, true);
2546 /* Size up the sections. */
2547 lang_size_sections (statement_list.head,
2549 &(statement_list.head), 0, (bfd_vma) 0, false);
2552 /* See if anything special should be done now we know how big
2554 ldemul_after_allocation ();
2556 /* Do all the assignments, now that we know the final restingplaces
2557 of all the symbols */
2559 lang_do_assignments (statement_list.head,
2564 /* Move the global symbols around */
2565 lang_relocate_globals ();
2567 /* Make sure that we're not mixing architectures */
2576 /* DO NOT REENABLE THIS CALL. IF THIS CALL IS MADE, THE SUN4 LINKER
2577 CAN NOT BOOTSTRAP!! No, I don't know why, but don't change it
2578 unless you fix it. */
2579 /* Size up the sections. */
2580 lang_size_sections (statement_list.head,
2582 &(statement_list.head), 0, (bfd_vma) 0, false);
2588 /* EXPORTED TO YACC */
2591 lang_add_wild (section_name, filename)
2592 CONST char *CONST section_name;
2593 CONST char *CONST filename;
2595 lang_wild_statement_type *new = new_stat (lang_wild_statement,
2598 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2600 placed_commons = true;
2602 if (filename != (char *) NULL)
2604 lang_has_input_file = true;
2606 new->section_name = section_name;
2607 new->filename = filename;
2608 lang_list_init (&new->children);
2612 lang_section_start (name, address)
2614 etree_type * address;
2616 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2618 ad->section_name = name;
2619 ad->address = address;
2623 lang_add_entry (name)
2626 entry_symbol = name;
2630 lang_add_target (name)
2633 lang_target_statement_type *new = new_stat (lang_target_statement,
2649 map_option_f = true;
2660 lang_fill_statement_type *new = new_stat (lang_fill_statement,
2667 lang_add_data (type, exp)
2669 union etree_union *exp;
2672 lang_data_statement_type *new = new_stat (lang_data_statement,
2681 lang_add_assignment (exp)
2684 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
2691 lang_add_attribute (attribute)
2692 enum statement_enum attribute;
2694 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2701 if (startup_file != (char *) NULL)
2703 einfo ("%P%Fmultiple STARTUP files\n");
2705 first_file->filename = name;
2706 first_file->local_sym_name = name;
2708 startup_file = name;
2715 lang_float_flag = maybe;
2719 lang_leave_output_section_statement (fill, memspec)
2721 CONST char *memspec;
2723 current_section->fill = fill;
2724 current_section->region = lang_memory_region_lookup (memspec);
2725 stat_ptr = &statement_list;
2727 /* We remember if we are closing a .data section, since we use it to
2728 store constructors in */
2729 if (strcmp (current_section->name, ".data") == 0)
2731 end_of_data_section_statement_list = statement_list;
2737 Create an absolute symbol with the given name with the value of the
2738 address of first byte of the section named.
2740 If the symbol already exists, then do nothing.
2743 lang_abs_symbol_at_beginning_of (section, name)
2744 CONST char *section;
2747 if (ldsym_undefined (name))
2749 asection *s = bfd_get_section_by_name (output_bfd, section);
2750 asymbol *def = create_symbol (name,
2751 BSF_GLOBAL | BSF_EXPORT,
2754 if (s != (asection *) NULL)
2756 def->value = s->vma;
2766 Create an absolute symbol with the given name with the value of the
2767 address of the first byte after the end of the section named.
2769 If the symbol already exists, then do nothing.
2772 lang_abs_symbol_at_end_of (section, name)
2773 CONST char *section;
2776 if (ldsym_undefined (name))
2778 asection *s = bfd_get_section_by_name (output_bfd, section);
2780 /* Add a symbol called _end */
2781 asymbol *def = create_symbol (name,
2782 BSF_GLOBAL | BSF_EXPORT,
2785 if (s != (asection *) NULL)
2787 def->value = s->vma + s->_raw_size;
2797 lang_statement_append (list, element, field)
2798 lang_statement_list_type * list;
2799 lang_statement_union_type * element;
2800 lang_statement_union_type ** field;
2802 *(list->tail) = element;
2806 /* Set the output format type. -oformat overrides scripts. */
2808 lang_add_output_format (format, from_script)
2812 if (output_target == NULL || !from_script)
2813 output_target = format;