1 /* Linker command language support.
2 Copyright 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
36 static void print_statements PARAMS ((void));
37 static void print_statement PARAMS ((lang_statement_union_type *,
38 lang_output_section_statement_type *));
41 static struct obstack stat_obstack;
43 #define obstack_chunk_alloc ldmalloc
44 #define obstack_chunk_free free
45 static CONST char *startup_file;
46 static lang_statement_list_type input_file_chain;
48 /* Points to the last statement in the .data section, so we can add
49 stuff to the data section without pain */
50 static lang_statement_list_type end_of_data_section_statement_list;
52 /* List of statements needed to handle constructors */
53 extern lang_statement_list_type constructor_list;
55 static boolean placed_commons = false;
56 static lang_output_section_statement_type *default_common_section;
57 static boolean map_option_f;
58 static bfd_vma print_dot;
59 static lang_input_statement_type *first_file;
60 static lang_statement_list_type lang_output_section_statement;
61 static CONST char *current_target;
62 static CONST char *output_target;
63 static size_t longest_section_name = 8;
64 static section_userdata_type common_section_userdata;
65 static lang_statement_list_type statement_list;
69 lang_output_section_statement_type *abs_output_section;
70 lang_statement_list_type *stat_ptr = &statement_list;
71 lang_input_statement_type *script_file = 0;
72 boolean option_longmap = false;
73 lang_statement_list_type file_chain =
75 CONST char *entry_symbol = 0;
76 bfd_size_type largest_section = 0;
77 boolean lang_has_input_file = false;
78 lang_output_section_statement_type *create_object_symbols = 0;
79 boolean had_output_filename = false;
80 boolean lang_float_flag = false;
83 extern char *default_target;
85 extern unsigned int undefined_global_sym_count;
86 extern char *current_file;
87 extern bfd *output_bfd;
88 extern enum bfd_architecture ldfile_output_architecture;
89 extern unsigned long ldfile_output_machine;
90 extern char *ldfile_output_machine_name;
91 extern ldsym_type *symbol_head;
92 extern unsigned int commons_pending;
93 extern args_type command_line;
94 extern ld_config_type config;
95 extern boolean had_script;
96 extern boolean write_map;
97 extern int g_switch_value;
100 etree_type *base; /* Relocation base - or null */
104 #define cat(a,b) a##b
106 #define cat(a,b) a/**/b
109 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
111 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
113 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
115 void lang_add_data PARAMS ((int type, union etree_union * exp));
118 DEFUN (stat_alloc, (size),
121 return obstack_alloc (&stat_obstack, size);
124 DEFUN (print_size, (value),
127 fprintf (config.map_file, "%5x", (unsigned) value);
130 DEFUN (print_alignment, (value),
133 fprintf (config.map_file, "2**%1u", value);
136 DEFUN (print_fill, (value),
139 fprintf (config.map_file, "%04x", (unsigned) value);
144 DEFUN (print_section, (name),
145 CONST char *CONST name)
147 fprintf (config.map_file, "%*s", -longest_section_name, name);
150 /*----------------------------------------------------------------------
151 lang_for_each_statement walks the parse tree and calls the provided
152 function for each node
156 DEFUN (lang_for_each_statement_worker, (func, s),
158 lang_statement_union_type * s)
160 for (; s != (lang_statement_union_type *) NULL; s = s->next)
164 switch (s->header.type)
166 case lang_constructors_statement_enum:
167 lang_for_each_statement_worker (func, constructor_list.head);
169 case lang_output_section_statement_enum:
170 lang_for_each_statement_worker
172 s->output_section_statement.children.head);
174 case lang_wild_statement_enum:
175 lang_for_each_statement_worker
177 s->wild_statement.children.head);
179 case lang_data_statement_enum:
180 case lang_object_symbols_statement_enum:
181 case lang_output_statement_enum:
182 case lang_target_statement_enum:
183 case lang_input_section_enum:
184 case lang_input_statement_enum:
185 case lang_assignment_statement_enum:
186 case lang_padding_statement_enum:
187 case lang_address_statement_enum:
197 DEFUN (lang_for_each_statement, (func),
200 lang_for_each_statement_worker (func,
201 statement_list.head);
204 /*----------------------------------------------------------------------*/
206 DEFUN (lang_list_init, (list),
207 lang_statement_list_type * list)
209 list->head = (lang_statement_union_type *) NULL;
210 list->tail = &list->head;
213 /*----------------------------------------------------------------------
215 build a new statement node for the parse tree
220 lang_statement_union_type *
221 DEFUN (new_statement, (type, size, list),
222 enum statement_enum type AND
223 bfd_size_type size AND
224 lang_statement_list_type * list)
226 lang_statement_union_type *new = (lang_statement_union_type *)
229 new->header.type = type;
230 new->header.next = (lang_statement_union_type *) NULL;
231 lang_statement_append (list, new, &new->header.next);
236 Build a new input file node for the language. There are several ways
237 in which we treat an input file, eg, we only look at symbols, or
238 prefix it with a -l etc.
240 We can be supplied with requests for input files more than once;
241 they may, for example be split over serveral lines like foo.o(.text)
242 foo.o(.data) etc, so when asked for a file we check that we havn't
243 got it already so we don't duplicate the bfd.
246 static lang_input_statement_type *
247 DEFUN (new_afile, (name, file_type, target),
248 CONST char *CONST name AND
249 CONST lang_input_file_enum_type file_type AND
250 CONST char *CONST target)
253 lang_input_statement_type *p = new_stat (lang_input_statement,
256 lang_has_input_file = true;
258 p->complained = false;
261 case lang_input_file_is_symbols_only_enum:
263 p->is_archive = false;
265 p->local_sym_name = name;
266 p->just_syms_flag = true;
267 p->search_dirs_flag = false;
269 case lang_input_file_is_fake_enum:
271 p->is_archive = false;
273 p->local_sym_name = name;
274 p->just_syms_flag = false;
275 p->search_dirs_flag = false;
277 case lang_input_file_is_l_enum:
278 p->is_archive = true;
281 p->local_sym_name = concat ("-l", name, "");
282 p->just_syms_flag = false;
283 p->search_dirs_flag = true;
285 case lang_input_file_is_search_file_enum:
286 case lang_input_file_is_marker_enum:
288 p->is_archive = false;
290 p->local_sym_name = name;
291 p->just_syms_flag = false;
292 p->search_dirs_flag = true;
294 case lang_input_file_is_file_enum:
296 p->is_archive = false;
298 p->local_sym_name = name;
299 p->just_syms_flag = false;
300 p->search_dirs_flag = false;
305 p->asymbols = (asymbol **) NULL;
306 p->superfile = (lang_input_statement_type *) NULL;
307 p->next_real_file = (lang_statement_union_type *) NULL;
308 p->next = (lang_statement_union_type *) NULL;
310 p->common_output_section = (asection *) NULL;
311 lang_statement_append (&input_file_chain,
312 (lang_statement_union_type *) p,
317 lang_input_statement_type *
318 DEFUN (lang_add_input_file, (name, file_type, target),
320 lang_input_file_enum_type file_type AND
323 /* Look it up or build a new one */
324 lang_has_input_file = true;
326 lang_input_statement_type *p;
328 for (p = (lang_input_statement_type *) input_file_chain.head;
329 p != (lang_input_statement_type *) NULL;
330 p = (lang_input_statement_type *) (p->next_real_file))
332 /* Sometimes we have incomplete entries in here */
333 if (p->filename != (char *) NULL)
335 if (strcmp (name, p->filename) == 0)
341 return new_afile (name, file_type, target);
345 DEFUN (lang_add_keepsyms_file, (filename),
346 CONST char *filename)
348 extern strip_symbols_type strip_symbols;
349 if (keepsyms_file != 0)
350 info ("%X%P error: duplicated keep-symbols-file value\n");
351 keepsyms_file = filename;
352 if (strip_symbols != STRIP_NONE)
353 info ("%P `-keep-only-symbols-file' overrides `-s' and `-S'\n");
354 strip_symbols = STRIP_SOME;
357 /* Build enough state so that the parser can build its tree */
359 DEFUN_VOID (lang_init)
361 obstack_begin (&stat_obstack, 1000);
363 stat_ptr = &statement_list;
365 lang_list_init (stat_ptr);
367 lang_list_init (&input_file_chain);
368 lang_list_init (&lang_output_section_statement);
369 lang_list_init (&file_chain);
370 first_file = lang_add_input_file ((char *) NULL,
371 lang_input_file_is_marker_enum,
373 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
375 abs_output_section->bfd_section = &bfd_abs_section;
379 /*----------------------------------------------------------------------
380 A region is an area of memory declared with the
381 MEMORY { name:org=exp, len=exp ... }
384 We maintain a list of all the regions here
386 If no regions are specified in the script, then the default is used
387 which is created when looked up to be the entire data space
390 static lang_memory_region_type *lang_memory_region_list;
391 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
393 lang_memory_region_type *
394 DEFUN (lang_memory_region_lookup, (name),
395 CONST char *CONST name)
398 lang_memory_region_type *p = lang_memory_region_list;
400 for (p = lang_memory_region_list;
401 p != (lang_memory_region_type *) NULL;
404 if (strcmp (p->name, name) == 0)
409 if (strcmp (name, "*default*") == 0)
411 /* This is the default region, dig out first one on the list */
412 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
414 return lang_memory_region_list;
418 lang_memory_region_type *new =
419 (lang_memory_region_type *) stat_alloc ((bfd_size_type) (sizeof (lang_memory_region_type)));
421 new->name = buystring (name);
422 new->next = (lang_memory_region_type *) NULL;
424 *lang_memory_region_list_tail = new;
425 lang_memory_region_list_tail = &new->next;
429 new->had_full_message = false;
436 lang_output_section_statement_type *
437 DEFUN (lang_output_section_find, (name),
438 CONST char *CONST name)
440 lang_statement_union_type *u;
441 lang_output_section_statement_type *lookup;
443 for (u = lang_output_section_statement.head;
444 u != (lang_statement_union_type *) NULL;
447 lookup = &u->output_section_statement;
448 if (strcmp (name, lookup->name) == 0)
453 return (lang_output_section_statement_type *) NULL;
456 lang_output_section_statement_type *
457 DEFUN (lang_output_section_statement_lookup, (name),
458 CONST char *CONST name)
460 lang_output_section_statement_type *lookup;
462 lookup = lang_output_section_find (name);
463 if (lookup == (lang_output_section_statement_type *) NULL)
466 lookup = (lang_output_section_statement_type *)
467 new_stat (lang_output_section_statement, stat_ptr);
468 lookup->region = (lang_memory_region_type *) NULL;
470 lookup->block_value = 1;
473 lookup->next = (lang_statement_union_type *) NULL;
474 lookup->bfd_section = (asection *) NULL;
475 lookup->processed = false;
476 lookup->loadable = 1;
477 lookup->addr_tree = (etree_type *) NULL;
478 lang_list_init (&lookup->children);
480 lookup->memspec = (CONST char *) NULL;
482 lookup->subsection_alignment = -1;
483 lookup->section_alignment = -1;
484 lookup->load_base = (union etree_union *) NULL;
486 lang_statement_append (&lang_output_section_statement,
487 (lang_statement_union_type *) lookup,
495 DEFUN (print_flags, (ignore_flags),
498 fprintf (config.map_file, "(");
500 if (flags->flag_read)
501 fprintf (outfile, "R");
502 if (flags->flag_write)
503 fprintf (outfile, "W");
504 if (flags->flag_executable)
505 fprintf (outfile, "X");
506 if (flags->flag_loadable)
507 fprintf (outfile, "L");
509 fprintf (config.map_file, ")");
513 DEFUN_VOID (lang_map)
515 lang_memory_region_type *m;
517 fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
519 fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
521 fprintf (config.map_file,
522 "name\t\torigin length r_size c_size is attributes\n");
525 for (m = lang_memory_region_list;
526 m != (lang_memory_region_type *) NULL;
529 fprintf (config.map_file, "%-16s", m->name);
530 print_address (m->origin);
532 print_address (m->length);
534 print_address (m->old_length);
536 print_address (m->current - m->origin);
539 fprintf(config.map_file," %2d%% ", ( m->current - m->origin) * 100 / m->old_length);
540 print_flags (&m->flags);
541 fprintf (config.map_file, "\n");
543 fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
544 fprintf (config.map_file, "output input virtual\n");
545 fprintf (config.map_file, "section section address tsize\n\n");
556 lang_output_section_statement_type * s)
558 /* asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
559 section_userdata_type *new =
560 (section_userdata_type *)
561 stat_alloc ((bfd_size_type) (sizeof (section_userdata_type)));
563 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
564 if (s->bfd_section == (asection *) NULL)
565 s->bfd_section = bfd_make_section (output_bfd, s->name);
566 if (s->bfd_section == (asection *) NULL)
568 einfo ("%P%F output format %s cannot represent section called %s\n",
569 output_bfd->xvec->name, s->name);
571 s->bfd_section->output_section = s->bfd_section;
572 /* s->bfd_section->flags = s->flags;*/
574 /* We initialize an output sections output offset to minus its own */
575 /* vma to allow us to output a section through itself */
576 s->bfd_section->output_offset = 0;
577 get_userdata (s->bfd_section) = (PTR) new;
581 /***********************************************************************
584 These expand statements like *(.text) and foo.o to a list of
585 explicit actions, like foo.o(.text), bar.o(.text) and
588 The toplevel routine, wild, takes a statement, section, file and
589 target. If either the section or file is null it is taken to be the
590 wildcard. Seperate lang_input_section statements are created for
591 each part of the expanstion, and placed after the statement provided.
596 DEFUN (wild_doit, (ptr, section, output, file),
597 lang_statement_list_type * ptr AND
598 asection * section AND
599 lang_output_section_statement_type * output AND
600 lang_input_statement_type * file)
602 if (output->bfd_section == (asection *) NULL)
605 /* Initialize the vma and size to the existing section. This will
606 be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
608 if (section != (asection *) NULL)
610 bfd_set_section_vma (0, output->bfd_section,
611 bfd_section_vma (0, section));
612 output->bfd_section->_raw_size = section->_raw_size;
616 if (section != (asection *) NULL
617 && section->output_section == (asection *) NULL)
619 /* Add a section reference to the list */
620 lang_input_section_type *new = new_stat (lang_input_section, ptr);
622 new->section = section;
624 section->output_section = output->bfd_section;
626 /* Be selective about what the output section inherits from the
629 section->output_section->flags |= section->flags & ~SEC_NEVER_LOAD;
631 if (!output->loadable)
633 /* Turn off load flag */
634 output->bfd_section->flags &= ~SEC_LOAD;
635 output->bfd_section->flags |= SEC_NEVER_LOAD;
637 if (section->alignment_power > output->bfd_section->alignment_power)
639 output->bfd_section->alignment_power = section->alignment_power;
641 /* If supplied an aligmnet, then force it */
642 if (output->section_alignment != -1)
644 output->bfd_section->alignment_power = output->section_alignment;
650 DEFUN (our_bfd_get_section_by_name, (abfd, section),
654 return bfd_get_section_by_name (abfd, section);
658 DEFUN (wild_section, (ptr, section, file, output),
659 lang_wild_statement_type * ptr AND
660 CONST char *section AND
661 lang_input_statement_type * file AND
662 lang_output_section_statement_type * output)
666 if (file->just_syms_flag == false)
668 if (section == (char *) NULL)
670 /* Do the creation to all sections in the file */
671 for (s = file->the_bfd->sections; s != (asection *) NULL; s = s->next)
674 if ((s->flags & SEC_IS_COMMON) == 0)
676 wild_doit (&ptr->children, s, output, file);
682 /* Do the creation to the named section only */
683 wild_doit (&ptr->children,
684 our_bfd_get_section_by_name (file->the_bfd, section),
690 /* passed a file name (which must have been seen already and added to
691 the statement tree. We will see if it has been opened already and
692 had its symbols read. If not then we'll read it.
694 Archives are pecuilar here. We may open them once, but if they do
695 not define anything we need at the time, they won't have all their
696 symbols read. If we need them later, we'll have to redo it.
699 lang_input_statement_type *
700 DEFUN (lookup_name, (name),
701 CONST char *CONST name)
703 lang_input_statement_type *search;
705 for (search = (lang_input_statement_type *) input_file_chain.head;
706 search != (lang_input_statement_type *) NULL;
707 search = (lang_input_statement_type *) search->next_real_file)
709 if (search->filename == (char *) NULL && name == (char *) NULL)
713 if (search->filename != (char *) NULL && name != (char *) NULL)
715 if (strcmp (search->filename, name) == 0)
717 ldmain_open_file_read_symbol (search);
723 /* There isn't an afile entry for this file yet, this must be
724 because the name has only appeared inside a load script and not
725 on the command line */
726 search = new_afile (name, lang_input_file_is_file_enum, default_target);
727 ldmain_open_file_read_symbol (search);
734 DEFUN (wild, (s, section, file, target, output),
735 lang_wild_statement_type * s AND
736 CONST char *CONST section AND
737 CONST char *CONST file AND
738 CONST char *CONST target AND
739 lang_output_section_statement_type * output)
741 lang_input_statement_type *f;
743 if (file == (char *) NULL)
745 /* Perform the iteration over all files in the list */
746 for (f = (lang_input_statement_type *) file_chain.head;
747 f != (lang_input_statement_type *) NULL;
748 f = (lang_input_statement_type *) f->next)
750 wild_section (s, section, f, output);
752 /* Once more for the script file */
753 wild_section(s, section, script_file, output);
757 /* Perform the iteration over a single file */
758 wild_section (s, section, lookup_name (file), output);
760 if (section != (char *) NULL
761 && strcmp (section, "COMMON") == 0
762 && default_common_section == (lang_output_section_statement_type *) NULL)
764 /* Remember the section that common is going to incase we later
765 get something which doesn't know where to put it */
766 default_common_section = output;
771 read in all the files
774 DEFUN (open_output, (name),
775 CONST char *CONST name)
777 extern unsigned long ldfile_output_machine;
778 extern enum bfd_architecture ldfile_output_architecture;
780 extern CONST char *output_filename;
783 if (output_target == (char *) NULL)
785 if (current_target != (char *) NULL)
786 output_target = current_target;
788 output_target = default_target;
790 output = bfd_openw (name, output_target);
791 output_filename = name;
793 if (output == (bfd *) NULL)
795 if (bfd_error == invalid_target)
797 einfo ("%P%F target %s not found\n", output_target);
799 einfo ("%P%F problem opening output file %s, %E\n", name);
802 /* output->flags |= D_PAGED;*/
804 bfd_set_format (output, bfd_object);
805 bfd_set_arch_mach (output,
806 ldfile_output_architecture,
807 ldfile_output_machine);
808 bfd_set_gp_size (output, g_switch_value);
816 DEFUN (ldlang_open_output, (statement),
817 lang_statement_union_type * statement)
819 switch (statement->header.type)
821 case lang_output_statement_enum:
822 output_bfd = open_output (statement->output_statement.name);
823 ldemul_set_output_arch ();
824 if (config.magic_demand_paged && !config.relocateable_output)
825 output_bfd->flags |= D_PAGED;
827 output_bfd->flags &= ~D_PAGED;
828 if (config.text_read_only)
829 output_bfd->flags |= WP_TEXT;
831 output_bfd->flags &= ~WP_TEXT;
834 case lang_target_statement_enum:
835 current_target = statement->target_statement.target;
843 DEFUN (open_input_bfds, (statement),
844 lang_statement_union_type * statement)
846 switch (statement->header.type)
848 case lang_target_statement_enum:
849 current_target = statement->target_statement.target;
851 case lang_wild_statement_enum:
852 /* Maybe we should load the file's symbols */
853 if (statement->wild_statement.filename)
855 (void) lookup_name (statement->wild_statement.filename);
858 case lang_input_statement_enum:
859 if (statement->input_statement.real == true)
861 statement->input_statement.target = current_target;
862 lookup_name (statement->input_statement.filename);
870 /* If there are [COMMONS] statements, put a wild one into the bss section */
873 lang_reasonable_defaults ()
879 lang_output_section_statement_lookup (".text");
880 lang_output_section_statement_lookup (".data");
882 default_common_section =
883 lang_output_section_statement_lookup (".bss");
886 if (placed_commons == false)
888 lang_wild_statement_type *new =
889 new_stat (lang_wild_statement,
890 &default_common_section->children);
892 new->section_name = "COMMON";
893 new->filename = (char *) NULL;
894 lang_list_init (&new->children);
901 Add the supplied name to the symbol table as an undefined reference.
902 Remove items from the chain as we open input bfds
904 typedef struct ldlang_undef_chain_list
906 struct ldlang_undef_chain_list *next;
908 } ldlang_undef_chain_list_type;
910 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
913 DEFUN (ldlang_add_undef, (name),
914 CONST char *CONST name)
916 ldlang_undef_chain_list_type *new =
917 (ldlang_undef_chain_list_type
918 *) stat_alloc ((bfd_size_type) (sizeof (ldlang_undef_chain_list_type)));
920 new->next = ldlang_undef_chain_list_head;
921 ldlang_undef_chain_list_head = new;
923 new->name = buystring (name);
926 /* Run through the list of undefineds created above and place them
927 into the linker hash table as undefined symbols belonging to the
931 DEFUN_VOID (lang_place_undefineds)
933 ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head;
935 while (ptr != (ldlang_undef_chain_list_type *) NULL)
938 asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **)));
940 def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd);
942 def->name = ptr->name;
943 def->section = &bfd_und_section;
944 Q_enter_global_ref (def_ptr, ptr->name);
949 /* Copy important data from out internal form to the bfd way. Also
950 create a section for the dummy file
954 DEFUN_VOID (lang_create_output_section_statements)
956 lang_statement_union_type *os;
958 for (os = lang_output_section_statement.head;
959 os != (lang_statement_union_type *) NULL;
960 os = os->output_section_statement.next)
962 lang_output_section_statement_type *s =
963 &os->output_section_statement;
971 DEFUN_VOID (lang_init_script_file)
973 script_file = lang_add_input_file ("command line",
974 lang_input_file_is_fake_enum,
976 script_file->the_bfd = bfd_create ("command line", output_bfd);
977 script_file->symbol_count = 0;
978 script_file->the_bfd->sections = 0;
980 /* The user data of a bfd points to the input statement attatched */
981 script_file->the_bfd->usrdata = (void *)script_file;
982 script_file->common_section =
983 bfd_make_section(script_file->the_bfd,"COMMON");
986 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
988 abs_output_section->bfd_section = &bfd_abs_section;
992 /* Open input files and attatch to output sections */
994 DEFUN (map_input_to_output_sections, (s, target, output_section_statement),
995 lang_statement_union_type * s AND
996 CONST char *target AND
997 lang_output_section_statement_type * output_section_statement)
999 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1001 switch (s->header.type)
1005 case lang_wild_statement_enum:
1006 wild (&s->wild_statement, s->wild_statement.section_name,
1007 s->wild_statement.filename, target,
1008 output_section_statement);
1011 case lang_constructors_statement_enum:
1012 map_input_to_output_sections (constructor_list.head,
1014 output_section_statement);
1016 case lang_output_section_statement_enum:
1017 map_input_to_output_sections (s->output_section_statement.children.head,
1019 &s->output_section_statement);
1021 case lang_output_statement_enum:
1023 case lang_target_statement_enum:
1024 target = s->target_statement.target;
1026 case lang_fill_statement_enum:
1027 case lang_input_section_enum:
1028 case lang_object_symbols_statement_enum:
1029 case lang_data_statement_enum:
1030 case lang_assignment_statement_enum:
1031 case lang_padding_statement_enum:
1033 case lang_afile_asection_pair_statement_enum:
1036 case lang_address_statement_enum:
1037 /* Mark the specified section with the supplied address */
1039 lang_output_section_statement_type *os =
1040 lang_output_section_statement_lookup
1041 (s->address_statement.section_name);
1043 os->addr_tree = s->address_statement.address;
1044 if (os->bfd_section == (asection *) NULL)
1046 einfo ("%P%F can't set the address of undefined section %s\n",
1047 s->address_statement.section_name);
1051 case lang_input_statement_enum:
1052 /* A standard input statement, has no wildcards */
1053 /* ldmain_open_file_read_symbol(&s->input_statement);*/
1064 DEFUN (print_output_section_statement, (output_section_statement),
1065 lang_output_section_statement_type * output_section_statement)
1067 asection *section = output_section_statement->bfd_section;
1070 print_section (output_section_statement->name);
1075 print_dot = section->vma;
1079 print_address (section->vma);
1081 print_size (section->_raw_size);
1083 print_size(section->_cooked_size);
1085 print_alignment (section->alignment_power);
1088 fprintf (config.map_file, "%s flags", output_section_statement->region->name);
1089 print_flags (stdout, &output_section_statement->flags);
1091 if (section->flags & SEC_LOAD)
1092 fprintf (config.map_file, "load ");
1093 if (section->flags & SEC_ALLOC)
1094 fprintf (config.map_file, "alloc ");
1095 if (section->flags & SEC_RELOC)
1096 fprintf (config.map_file, "reloc ");
1097 if (section->flags & SEC_HAS_CONTENTS)
1098 fprintf (config.map_file, "contents ");
1103 fprintf (config.map_file, "No attached output section");
1106 if (output_section_statement->load_base)
1108 int b = exp_get_value_int(output_section_statement->load_base,
1109 0, "output base", lang_final_phase_enum);
1110 printf("Output address %08x\n", b);
1112 if (output_section_statement->section_alignment >= 0
1113 || output_section_statement->section_alignment >= 0)
1115 printf("\t\t\t\t\tforced alignment ");
1116 if ( output_section_statement->section_alignment >= 0)
1118 printf("section 2**%d ",output_section_statement->section_alignment );
1120 if ( output_section_statement->subsection_alignment >= 0)
1122 printf("subsection 2**%d ",output_section_statement->subsection_alignment );
1127 print_statement (output_section_statement->children.head,
1128 output_section_statement);
1133 DEFUN (print_assignment, (assignment, output_section),
1134 lang_assignment_statement_type * assignment AND
1135 lang_output_section_statement_type * output_section)
1137 etree_value_type result;
1143 print_address (print_dot);
1145 result = exp_fold_tree (assignment->exp->assign.src,
1147 lang_final_phase_enum,
1153 print_address (result.value);
1157 fprintf (config.map_file, "*undefined*");
1160 exp_print_tree (assignment->exp);
1162 fprintf (config.map_file, "\n");
1166 DEFUN (print_input_statement, (statm),
1167 lang_input_statement_type * statm)
1169 if (statm->filename != (char *) NULL)
1171 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1176 DEFUN (print_symbol, (q),
1180 fprintf (config.map_file, " ");
1182 fprintf (config.map_file, " ");
1183 print_address (outside_symbol_address (q));
1184 fprintf (config.map_file, " %s", q->name ? q->name : " ");
1189 DEFUN (print_input_section, (in),
1190 lang_input_section_type * in)
1192 asection *i = in->section;
1193 int size = i->reloc_done ?
1194 bfd_get_section_size_after_reloc (i) :
1195 bfd_get_section_size_before_reloc (i);
1200 fprintf (config.map_file, " ");
1201 print_section (i->name);
1202 fprintf (config.map_file, " ");
1203 if (i->output_section)
1205 print_address (i->output_section->vma + i->output_offset);
1206 fprintf (config.map_file, " ");
1207 print_size (i->_raw_size);
1208 fprintf (config.map_file, " ");
1209 print_size(i->_cooked_size);
1210 fprintf (config.map_file, " ");
1211 print_alignment (i->alignment_power);
1212 fprintf (config.map_file, " ");
1216 bfd *abfd = in->ifile->the_bfd;
1218 if (in->ifile->just_syms_flag == true)
1220 fprintf (config.map_file, "symbols only ");
1223 fprintf (config.map_file, " %s ", abfd->xvec->name);
1224 if (abfd->my_archive != (bfd *) NULL)
1226 fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
1231 fprintf (config.map_file, "%s", abfd->filename);
1233 fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
1236 /* Find all the symbols in this file defined in this section */
1238 if (in->ifile->symbol_count)
1242 for (p = in->ifile->asymbols; *p; p++)
1246 if (bfd_get_section (q) == i && q->flags & BSF_GLOBAL)
1259 print_dot = outside_section_address (i) + size;
1263 fprintf (config.map_file, "No output section allocated\n");
1269 DEFUN (print_fill_statement, (fill),
1270 lang_fill_statement_type * fill)
1272 fprintf (config.map_file, "FILL mask ");
1273 print_fill (fill->fill);
1277 DEFUN (print_data_statement, (data),
1278 lang_data_statement_type * data)
1280 /* bfd_vma value; */
1285 /* ASSERT(print_dot == data->output_vma);*/
1287 print_address (data->output_vma + data->output_section->vma);
1289 print_address (data->value);
1294 fprintf (config.map_file, "BYTE ");
1295 print_dot += BYTE_SIZE;
1298 fprintf (config.map_file, "SHORT ");
1299 print_dot += SHORT_SIZE;
1302 fprintf (config.map_file, "LONG ");
1303 print_dot += LONG_SIZE;
1307 exp_print_tree (data->exp);
1309 fprintf (config.map_file, "\n");
1314 DEFUN (print_padding_statement, (s),
1315 lang_padding_statement_type * s)
1319 print_section ("*fill*");
1321 print_address (s->output_offset + s->output_section->vma);
1323 print_size (s->size);
1325 print_fill (s->fill);
1328 print_dot = s->output_offset + s->output_section->vma + s->size;
1333 DEFUN (print_wild_statement, (w, os),
1334 lang_wild_statement_type * w AND
1335 lang_output_section_statement_type * os)
1337 fprintf (config.map_file, " from ");
1338 if (w->filename != (char *) NULL)
1340 fprintf (config.map_file, "%s", w->filename);
1344 fprintf (config.map_file, "*");
1346 if (w->section_name != (char *) NULL)
1348 fprintf (config.map_file, "(%s)", w->section_name);
1352 fprintf (config.map_file, "(*)");
1355 print_statement (w->children.head, os);
1359 DEFUN (print_statement, (s, os),
1360 lang_statement_union_type * s AND
1361 lang_output_section_statement_type * os)
1365 switch (s->header.type)
1367 case lang_constructors_statement_enum:
1368 fprintf (config.map_file, "constructors:\n");
1369 print_statement (constructor_list.head, os);
1371 case lang_wild_statement_enum:
1372 print_wild_statement (&s->wild_statement, os);
1375 fprintf (config.map_file, "Fail with %d\n", s->header.type);
1378 case lang_address_statement_enum:
1379 fprintf (config.map_file, "address\n");
1381 case lang_object_symbols_statement_enum:
1382 fprintf (config.map_file, "object symbols\n");
1384 case lang_fill_statement_enum:
1385 print_fill_statement (&s->fill_statement);
1387 case lang_data_statement_enum:
1388 print_data_statement (&s->data_statement);
1390 case lang_input_section_enum:
1391 print_input_section (&s->input_section);
1393 case lang_padding_statement_enum:
1394 print_padding_statement (&s->padding_statement);
1396 case lang_output_section_statement_enum:
1397 print_output_section_statement (&s->output_section_statement);
1399 case lang_assignment_statement_enum:
1400 print_assignment (&s->assignment_statement,
1403 case lang_target_statement_enum:
1404 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
1406 case lang_output_statement_enum:
1407 fprintf (config.map_file, "OUTPUT(%s %s)\n",
1408 s->output_statement.name,
1409 output_target ? output_target : "");
1411 case lang_input_statement_enum:
1412 print_input_statement (&s->input_statement);
1414 case lang_afile_asection_pair_statement_enum:
1424 DEFUN_VOID (print_statements)
1426 print_statement (statement_list.head,
1427 abs_output_section);
1432 DEFUN (insert_pad, (this_ptr, fill, power, output_section_statement, dot),
1433 lang_statement_union_type ** this_ptr AND
1435 unsigned int power AND
1436 asection * output_section_statement AND
1439 /* Align this section first to the
1440 input sections requirement, then
1441 to the output section's requirement.
1442 If this alignment is > than any seen before,
1443 then record it too. Perform the alignment by
1444 inserting a magic 'padding' statement.
1447 unsigned int alignment_needed = align_power (dot, power) - dot;
1449 if (alignment_needed != 0)
1451 lang_statement_union_type *new =
1452 (lang_statement_union_type *)
1453 stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
1455 /* Link into existing chain */
1456 new->header.next = *this_ptr;
1458 new->header.type = lang_padding_statement_enum;
1459 new->padding_statement.output_section = output_section_statement;
1460 new->padding_statement.output_offset =
1461 dot - output_section_statement->vma;
1462 new->padding_statement.fill = fill;
1463 new->padding_statement.size = alignment_needed;
1467 /* Remember the most restrictive alignment */
1468 if (power > output_section_statement->alignment_power)
1470 output_section_statement->alignment_power = power;
1472 output_section_statement->_raw_size += alignment_needed;
1473 return alignment_needed + dot;
1477 /* Work out how much this section will move the dot point */
1479 DEFUN (size_input_section, (this_ptr, output_section_statement, fill,
1481 lang_statement_union_type ** this_ptr AND
1482 lang_output_section_statement_type * output_section_statement AND
1483 unsigned short fill AND
1487 lang_input_section_type *is = &((*this_ptr)->input_section);
1488 asection *i = is->section;
1490 if (is->ifile->just_syms_flag == false)
1492 if (output_section_statement->subsection_alignment != -1)
1493 i->alignment_power =
1494 output_section_statement->subsection_alignment;
1496 dot = insert_pad (this_ptr, fill, i->alignment_power,
1497 output_section_statement->bfd_section, dot);
1499 /* remember the largest size so we can malloc the largest area
1500 needed for the output stage. Only remember the size of sections
1501 which we will actually allocate */
1503 (SEC_HAS_CONTENTS | SEC_ALLOC)) == (SEC_HAS_CONTENTS | SEC_ALLOC))
1504 && (bfd_get_section_size_before_reloc (i) > largest_section))
1506 largest_section = bfd_get_section_size_before_reloc (i);
1509 /* Remember where in the output section this input section goes */
1511 i->output_offset = dot - output_section_statement->bfd_section->vma;
1513 /* Mark how big the output section must be to contain this now
1517 dot += i->_cooked_size;
1521 dot += i->_raw_size;
1523 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
1527 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
1533 /* Sizing happens in two passes, first pass we allocate worst case
1534 stuff. The second pass (if relaxing), we use what we learnt to
1535 change the size of some relocs from worst case to better
1537 static boolean had_relax;
1540 DEFUN (lang_size_sections, (s, output_section_statement, prev, fill,
1542 lang_statement_union_type * s AND
1543 lang_output_section_statement_type * output_section_statement AND
1544 lang_statement_union_type ** prev AND
1545 unsigned short fill AND
1549 /* Size up the sections from their constituent parts */
1550 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1552 switch (s->header.type)
1555 case lang_output_section_statement_enum:
1558 lang_output_section_statement_type *os = &s->output_section_statement;
1560 /* If this is a shared library section, don't change the size
1562 if (os->bfd_section->flags & SEC_SHARED_LIBRARY)
1565 if (os->bfd_section == &bfd_abs_section)
1567 /* No matter what happens, an abs section starts at zero */
1568 bfd_set_section_vma (0, os->bfd_section, 0);
1572 if (os->addr_tree == (etree_type *) NULL)
1574 /* No address specified for this section, get one
1575 from the region specification
1577 if (os->region == (lang_memory_region_type *) NULL)
1579 os->region = lang_memory_region_lookup ("*default*");
1581 dot = os->region->current;
1587 r = exp_fold_tree (os->addr_tree,
1589 lang_allocating_phase_enum,
1591 if (r.valid == false)
1593 einfo ("%F%S: non constant address expression for section %s\n",
1598 /* The section starts here */
1599 /* First, align to what the section needs */
1602 dot = align_power (dot, os->bfd_section->alignment_power);
1603 bfd_set_section_vma (0, os->bfd_section, dot);
1605 if (os->load_base) {
1606 os->bfd_section->lma
1607 = exp_get_value_int(os->load_base, 0,"load base", lang_final_phase_enum);
1612 os->bfd_section->output_offset = 0;
1614 (void) lang_size_sections (os->children.head, os, &os->children.head,
1615 os->fill, dot, relax);
1616 /* Ignore the size of the input sections, use the vma and size to */
1620 after = ALIGN (os->bfd_section->vma +
1621 os->bfd_section->_raw_size,
1625 os->bfd_section->_raw_size = after - os->bfd_section->vma;
1626 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1627 os->processed = true;
1629 /* Replace into region ? */
1630 if (os->addr_tree == (etree_type *) NULL
1631 && os->region != (lang_memory_region_type *) NULL)
1633 os->region->current = dot;
1634 /* Make sure this isn't silly */
1635 if (( os->region->current
1636 > os->region->origin + os->region->length)
1637 || ( os->region->origin > os->region->current ))
1639 einfo ("%X%P: Region %s is full (%B section %s)\n",
1641 os->bfd_section->owner,
1642 os->bfd_section->name);
1643 /* Reset the region pointer */
1644 os->region->current = 0;
1652 case lang_constructors_statement_enum:
1653 dot = lang_size_sections (constructor_list.head,
1654 output_section_statement,
1655 &s->wild_statement.children.head,
1660 case lang_data_statement_enum:
1662 unsigned int size = 0;
1664 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
1665 s->data_statement.output_section =
1666 output_section_statement->bfd_section;
1668 switch (s->data_statement.type)
1682 output_section_statement->bfd_section->_raw_size += size;
1686 case lang_wild_statement_enum:
1688 dot = lang_size_sections (s->wild_statement.children.head,
1689 output_section_statement,
1690 &s->wild_statement.children.head,
1696 case lang_object_symbols_statement_enum:
1697 create_object_symbols = output_section_statement;
1699 case lang_output_statement_enum:
1700 case lang_target_statement_enum:
1702 case lang_input_section_enum:
1707 if( relax_section (prev))
1713 (*prev)->input_section.section->_cooked_size =
1714 (*prev)->input_section.section->_raw_size ;
1717 dot = size_input_section (prev,
1718 output_section_statement,
1719 output_section_statement->fill,
1722 case lang_input_statement_enum:
1724 case lang_fill_statement_enum:
1725 s->fill_statement.output_section = output_section_statement->bfd_section;
1727 fill = s->fill_statement.fill;
1729 case lang_assignment_statement_enum:
1731 bfd_vma newdot = dot;
1733 exp_fold_tree (s->assignment_statement.exp,
1734 output_section_statement,
1735 lang_allocating_phase_enum,
1739 if (newdot != dot && !relax)
1740 /* We've been moved ! so insert a pad */
1742 lang_statement_union_type *new =
1743 (lang_statement_union_type *)
1744 stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
1746 /* Link into existing chain */
1747 new->header.next = *prev;
1749 new->header.type = lang_padding_statement_enum;
1750 new->padding_statement.output_section =
1751 output_section_statement->bfd_section;
1752 new->padding_statement.output_offset =
1753 dot - output_section_statement->bfd_section->vma;
1754 new->padding_statement.fill = fill;
1755 new->padding_statement.size = newdot - dot;
1756 output_section_statement->bfd_section->_raw_size +=
1757 new->padding_statement.size;
1766 /* This can only get here when relaxing is turned on */
1767 case lang_padding_statement_enum:
1769 case lang_address_statement_enum:
1772 prev = &s->header.next;
1778 DEFUN (lang_do_assignments, (s, output_section_statement, fill, dot),
1779 lang_statement_union_type * s AND
1780 lang_output_section_statement_type * output_section_statement AND
1781 unsigned short fill AND
1785 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1787 switch (s->header.type)
1789 case lang_constructors_statement_enum:
1790 dot = lang_do_assignments (constructor_list.head,
1791 output_section_statement,
1796 case lang_output_section_statement_enum:
1798 lang_output_section_statement_type *os =
1799 &(s->output_section_statement);
1801 dot = os->bfd_section->vma;
1802 (void) lang_do_assignments (os->children.head, os, os->fill, dot);
1803 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
1806 case lang_wild_statement_enum:
1808 dot = lang_do_assignments (s->wild_statement.children.head,
1809 output_section_statement,
1814 case lang_object_symbols_statement_enum:
1815 case lang_output_statement_enum:
1816 case lang_target_statement_enum:
1818 case lang_common_statement_enum:
1821 case lang_data_statement_enum:
1823 etree_value_type value;
1825 value = exp_fold_tree (s->data_statement.exp,
1827 lang_final_phase_enum, dot, &dot);
1828 s->data_statement.value = value.value;
1829 if (value.valid == false)
1830 einfo ("%F%P: Invalid data statement\n");
1832 switch (s->data_statement.type)
1845 case lang_input_section_enum:
1847 asection *in = s->input_section.section;
1849 dot += bfd_get_section_size_before_reloc (in);
1853 case lang_input_statement_enum:
1855 case lang_fill_statement_enum:
1856 fill = s->fill_statement.fill;
1858 case lang_assignment_statement_enum:
1860 exp_fold_tree (s->assignment_statement.exp,
1861 output_section_statement,
1862 lang_final_phase_enum,
1868 case lang_padding_statement_enum:
1869 dot += s->padding_statement.size;
1874 case lang_address_statement_enum:
1885 DEFUN_VOID (lang_relocate_globals)
1889 Each ldsym_type maintains a chain of pointers to asymbols which
1890 references the definition. Replace each pointer to the referenence
1891 with a pointer to only one place, preferably the definition. If
1892 the defintion isn't available then the common symbol, and if
1893 there isn't one of them then choose one reference.
1896 FOR_EACH_LDSYM (lgs)
1900 if (lgs->sdefs_chain)
1902 it = *(lgs->sdefs_chain);
1904 else if (lgs->scoms_chain != (asymbol **) NULL)
1906 it = *(lgs->scoms_chain);
1908 else if (lgs->srefs_chain != (asymbol **) NULL)
1910 it = *(lgs->srefs_chain);
1914 /* This can happen when the command line asked for a symbol to
1916 it = (asymbol *) NULL;
1918 if (it != (asymbol *) NULL)
1921 asymbol **ptr = lgs->srefs_chain;;
1922 if (lgs->flags & SYM_WARNING)
1924 produce_warnings (lgs, it);
1927 while (ptr != (asymbol **) NULL
1930 asymbol *ref = *ptr;
1933 ptr = (asymbol **) (ref->udata);
1942 DEFUN_VOID (lang_finish)
1945 int warn = config.relocateable_output != true;
1946 if (entry_symbol == (char *) NULL)
1948 /* No entry has been specified, look for start, but don't warn */
1949 entry_symbol = "start";
1952 lgs = ldsym_get_soft (entry_symbol);
1953 if (lgs && lgs->sdefs_chain)
1955 asymbol *sy = *(lgs->sdefs_chain);
1957 /* We can set the entry address*/
1958 bfd_set_start_address (output_bfd,
1959 outside_symbol_address (sy));
1964 /* Can't find anything reasonable,
1965 use the first address in the text section
1967 asection *ts = bfd_get_section_by_name (output_bfd, ".text");
1971 einfo ("%P: Warning, can't find entry symbol %s, defaulting to %V\n",
1972 entry_symbol, ts->vma);
1974 bfd_set_start_address (output_bfd, ts->vma);
1979 einfo ("%P: Warning, can't find entry symbol %s, not setting start address\n",
1985 /* By now we know the target architecture, and we may have an */
1986 /* ldfile_output_machine_name */
1988 DEFUN_VOID (lang_check)
1990 lang_statement_union_type *file;
1992 unsigned long input_machine;
1993 enum bfd_architecture input_architecture;
1994 CONST bfd_arch_info_type *compatible;
1996 for (file = file_chain.head;
1997 file != (lang_statement_union_type *) NULL;
1998 file = file->input_statement.next)
2000 input_bfd = file->input_statement.the_bfd;
2002 input_machine = bfd_get_mach (input_bfd);
2003 input_architecture = bfd_get_arch (input_bfd);
2006 /* Inspect the architecture and ensure we're linking like with
2009 compatible = bfd_arch_get_compatible (input_bfd,
2014 ldfile_output_machine = compatible->mach;
2015 ldfile_output_architecture = compatible->arch;
2020 info ("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
2021 bfd_printable_name (input_bfd), input_bfd,
2022 bfd_printable_name (output_bfd));
2024 bfd_set_arch_mach (output_bfd,
2033 * run through all the global common symbols and tie them
2034 * to the output section requested.
2036 As an experiment we do this 4 times, once for all the byte sizes,
2037 then all the two bytes, all the four bytes and then everything else
2041 DEFUN_VOID (lang_common)
2046 if (config.relocateable_output == false ||
2047 command_line.force_common_definition == true)
2049 for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<= 1)
2051 for (lgs = symbol_head;
2052 lgs != (ldsym_type *) NULL;
2056 unsigned int power_of_two;
2060 if (lgs->scoms_chain != (asymbol **) NULL)
2062 com = *(lgs->scoms_chain);
2092 if (config.sort_common == false || align == power)
2096 /* Change from a common symbol into a definition of
2098 lgs->sdefs_chain = lgs->scoms_chain;
2099 lgs->scoms_chain = (asymbol **) NULL;
2102 /* Point to the correct common section */
2103 symbfd = bfd_asymbol_bfd (com);
2104 if (com->section == &bfd_com_section)
2106 ((lang_input_statement_type *) symbfd->usrdata)
2113 name = bfd_get_section_name (symbfd,
2115 newsec = bfd_get_section_by_name (symbfd,
2117 /* BFD backend must provide this section. */
2118 if (newsec == (asection *) NULL)
2119 einfo ("%P%F: No output section %s", name);
2120 com->section = newsec;
2123 /* Fix the size of the common section */
2125 com->section->_raw_size =
2126 ALIGN (com->section->_raw_size, align);
2128 /* Remember if this is the biggest alignment ever seen */
2129 if (power_of_two > com->section->alignment_power)
2131 com->section->alignment_power = power_of_two;
2134 /* Symbol stops being common and starts being global, but
2135 we remember that it was common once. */
2137 com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON;
2138 com->value = com->section->_raw_size;
2140 if (write_map && config.map_file)
2142 fprintf (config.map_file, "Allocating common %s: %x at %x %s\n",
2145 (unsigned) com->value,
2146 bfd_asymbol_bfd(com)->filename);
2149 com->section->_raw_size += size;
2162 run through the input files and ensure that every input
2163 section has somewhere to go. If one is found without
2164 a destination then create an input request and place it
2165 into the statement tree.
2169 DEFUN_VOID (lang_place_orphans)
2171 lang_input_statement_type *file;
2173 for (file = (lang_input_statement_type *) file_chain.head;
2174 file != (lang_input_statement_type *) NULL;
2175 file = (lang_input_statement_type *) file->next)
2179 for (s = file->the_bfd->sections;
2180 s != (asection *) NULL;
2183 if (s->output_section == (asection *) NULL)
2185 /* This section of the file is not attatched, root
2186 around for a sensible place for it to go */
2188 if (file->common_section == s)
2190 /* This is a lonely common section which must
2191 have come from an archive. We attatch to the
2192 section with the wildcard */
2193 if (config.relocateable_output != true
2194 && command_line.force_common_definition == false)
2196 if (default_common_section ==
2197 (lang_output_section_statement_type *) NULL)
2199 info ("%P: No [COMMON] command, defaulting to .bss\n");
2201 default_common_section =
2202 lang_output_section_statement_lookup (".bss");
2205 wild_doit (&default_common_section->children, s,
2206 default_common_section, file);
2211 lang_output_section_statement_type *os =
2212 lang_output_section_statement_lookup (s->name);
2214 wild_doit (&os->children, s, os, file);
2223 DEFUN (lang_set_flags, (ptr, flags),
2227 boolean state = false;
2242 /* ptr->flag_read = state; */
2245 /* ptr->flag_write = state; */
2248 /* ptr->flag_executable= state;*/
2252 /* ptr->flag_loadable= state;*/
2255 einfo ("%P%F illegal syntax in flags\n");
2265 DEFUN (lang_for_each_file, (func),
2266 void (*func) PARAMS ((lang_input_statement_type *)))
2268 lang_input_statement_type *f;
2270 for (f = (lang_input_statement_type *) file_chain.head;
2271 f != (lang_input_statement_type *) NULL;
2272 f = (lang_input_statement_type *) f->next)
2280 DEFUN (lang_for_each_input_section, (func),
2281 void (*func) PARAMS ((bfd * ab, asection * as)))
2283 lang_input_statement_type *f;
2285 for (f = (lang_input_statement_type *) file_chain.head;
2286 f != (lang_input_statement_type *) NULL;
2287 f = (lang_input_statement_type *) f->next)
2291 for (s = f->the_bfd->sections;
2292 s != (asection *) NULL;
2295 func (f->the_bfd, s);
2303 DEFUN (ldlang_add_file, (entry),
2304 lang_input_statement_type * entry)
2307 lang_statement_append (&file_chain,
2308 (lang_statement_union_type *) entry,
2313 DEFUN (lang_add_output, (name),
2316 lang_output_statement_type *new = new_stat (lang_output_statement,
2320 had_output_filename = true;
2324 static lang_output_section_statement_type *current_section;
2326 static int topower(x)
2331 if (x < 0) return -1;
2332 for (l = 0; l < 32; l++)
2334 if (i >= x) return l;
2340 DEFUN (lang_enter_output_section_statement,
2341 (output_section_statement_name,
2345 align, subalign, base),
2346 char *output_section_statement_name AND
2347 etree_type * address_exp AND
2349 bfd_vma block_value AND
2350 etree_type *align AND
2351 etree_type *subalign AND
2354 lang_output_section_statement_type *os;
2358 lang_output_section_statement_lookup (output_section_statement_name);
2362 /* Add this statement to tree */
2363 /* add_statement(lang_output_section_statement_enum,
2364 output_section_statement);*/
2365 /* Make next things chain into subchain of this */
2367 if (os->addr_tree ==
2368 (etree_type *) NULL)
2374 if (flags & SEC_NEVER_LOAD)
2378 os->block_value = block_value ? block_value : 1;
2379 stat_ptr = &os->children;
2381 os->subsection_alignment = topower(
2382 exp_get_value_int(subalign, -1,
2383 "subsection alignment",
2385 os->section_alignment = topower(
2386 exp_get_value_int(align, -1,
2387 "section alignment", 0));
2389 os->load_base = base;
2394 DEFUN_VOID (lang_final)
2396 if (had_output_filename == false)
2398 extern CONST char *output_filename;
2400 lang_add_output (output_filename);
2404 /* Reset the current counters in the regions */
2406 DEFUN_VOID (reset_memory_regions)
2408 lang_memory_region_type *p = lang_memory_region_list;
2410 for (p = lang_memory_region_list;
2411 p != (lang_memory_region_type *) NULL;
2414 p->old_length = p->current - p->origin;
2415 p->current = p->origin;
2422 DEFUN (create_symbol, (name, flags, section),
2423 CONST char *name AND
2427 extern lang_input_statement_type *script_file;
2428 asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **)));
2430 /* Add this definition to script file */
2431 asymbol *def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd);
2432 def->name = buystring (name);
2435 def->section = section;
2437 Q_enter_global_ref (def_ptr, name);
2442 DEFUN_VOID (lang_process)
2445 if (had_script == false)
2447 parse_line (ldemul_get_script (), 1);
2449 lang_reasonable_defaults ();
2450 current_target = default_target;
2452 lang_for_each_statement (ldlang_open_output); /* Open the output file */
2453 /* For each output section statement, create a section in the output
2455 lang_create_output_section_statements ();
2457 /* Create a dummy bfd for the script */
2458 lang_init_script_file ();
2460 /* Add to the hash table all undefineds on the command line */
2461 lang_place_undefineds ();
2463 /* Create a bfd for each input file */
2464 current_target = default_target;
2465 lang_for_each_statement (open_input_bfds);
2467 /* Run through the contours of the script and attatch input sections
2468 to the correct output sections
2470 find_constructors ();
2471 map_input_to_output_sections (statement_list.head, (char *) NULL,
2472 (lang_output_section_statement_type *) NULL);
2475 /* Find any sections not attatched explicitly and handle them */
2476 lang_place_orphans ();
2478 /* Size up the common data */
2481 ldemul_before_allocation ();
2491 lang_size_sections (statement_list.head,
2492 (lang_output_section_statement_type *) NULL,
2493 &(statement_list.head), 0, (bfd_vma) 0, true);
2494 /* FIXME. Until the code in relax is fixed so that it only reads in
2495 stuff once, we cant iterate since there is no way for the linker to
2496 know what has been patched and what hasn't */
2502 /* Now run around and relax if we can */
2503 if (command_line.relax)
2505 /* First time round is a trial run to get the 'worst case' addresses of the
2506 objects if there was no relaxing */
2507 lang_size_sections (statement_list.head,
2508 (lang_output_section_statement_type *) NULL,
2509 &(statement_list.head), 0, (bfd_vma) 0, false);
2513 /* Move the global symbols around so the second pass of relaxing can
2515 lang_relocate_globals ();
2517 reset_memory_regions ();
2519 /* Do all the assignments, now that we know the final restingplaces
2520 of all the symbols */
2522 lang_do_assignments (statement_list.head,
2527 /* Perform another relax pass - this time we know where the
2528 globals are, so can make better guess */
2529 lang_size_sections (statement_list.head,
2530 (lang_output_section_statement_type *) NULL,
2531 &(statement_list.head), 0, (bfd_vma) 0, true);
2539 /* Size up the sections */
2540 lang_size_sections (statement_list.head,
2542 &(statement_list.head), 0, (bfd_vma) 0, false);
2547 /* See if anything special should be done now we know how big
2549 ldemul_after_allocation ();
2551 /* Do all the assignments, now that we know the final restingplaces
2552 of all the symbols */
2554 lang_do_assignments (statement_list.head,
2559 /* Move the global symbols around */
2560 lang_relocate_globals ();
2562 /* Make sure that we're not mixing architectures */
2570 /* EXPORTED TO YACC */
2573 DEFUN (lang_add_wild, (section_name, filename),
2574 CONST char *CONST section_name AND
2575 CONST char *CONST filename)
2577 lang_wild_statement_type *new = new_stat (lang_wild_statement,
2580 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
2582 placed_commons = true;
2584 if (filename != (char *) NULL)
2586 lang_has_input_file = true;
2588 new->section_name = section_name;
2589 new->filename = filename;
2590 lang_list_init (&new->children);
2594 DEFUN (lang_section_start, (name, address),
2595 CONST char *name AND
2596 etree_type * address)
2598 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
2600 ad->section_name = name;
2601 ad->address = address;
2605 DEFUN (lang_add_entry, (name),
2608 entry_symbol = name;
2612 DEFUN (lang_add_target, (name),
2615 lang_target_statement_type *new = new_stat (lang_target_statement,
2623 DEFUN (lang_add_map, (name),
2631 map_option_f = true;
2639 DEFUN (lang_add_fill, (exp),
2642 lang_fill_statement_type *new = new_stat (lang_fill_statement,
2649 DEFUN (lang_add_data, (type, exp),
2651 union etree_union *exp)
2654 lang_data_statement_type *new = new_stat (lang_data_statement,
2663 DEFUN (lang_add_assignment, (exp),
2666 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
2673 DEFUN (lang_add_attribute, (attribute),
2674 enum statement_enum attribute)
2676 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
2680 DEFUN (lang_startup, (name),
2683 if (startup_file != (char *) NULL)
2685 einfo ("%P%FMultiple STARTUP files\n");
2687 first_file->filename = name;
2688 first_file->local_sym_name = name;
2690 startup_file = name;
2694 DEFUN (lang_float, (maybe),
2697 lang_float_flag = maybe;
2701 DEFUN (lang_leave_output_section_statement, (fill, memspec),
2703 CONST char *memspec)
2705 current_section->fill = fill;
2706 current_section->region = lang_memory_region_lookup (memspec);
2707 stat_ptr = &statement_list;
2709 /* We remember if we are closing a .data section, since we use it to
2710 store constructors in */
2711 if (strcmp (current_section->name, ".data") == 0)
2713 end_of_data_section_statement_list = statement_list;
2719 Create an absolute symbol with the given name with the value of the
2720 address of first byte of the section named.
2722 If the symbol already exists, then do nothing.
2725 DEFUN (lang_abs_symbol_at_beginning_of, (section, name),
2726 CONST char *section AND
2729 if (ldsym_undefined (name))
2731 asection *s = bfd_get_section_by_name (output_bfd, section);
2732 asymbol *def = create_symbol (name,
2733 BSF_GLOBAL | BSF_EXPORT,
2736 if (s != (asection *) NULL)
2738 def->value = s->vma;
2748 Create an absolute symbol with the given name with the value of the
2749 address of the first byte after the end of the section named.
2751 If the symbol already exists, then do nothing.
2754 DEFUN (lang_abs_symbol_at_end_of, (section, name),
2755 CONST char *section AND
2758 if (ldsym_undefined (name))
2760 asection *s = bfd_get_section_by_name (output_bfd, section);
2762 /* Add a symbol called _end */
2763 asymbol *def = create_symbol (name,
2764 BSF_GLOBAL | BSF_EXPORT,
2767 if (s != (asection *) NULL)
2769 def->value = s->vma + s->_raw_size;
2779 DEFUN (lang_statement_append, (list, element, field),
2780 lang_statement_list_type * list AND
2781 lang_statement_union_type * element AND
2782 lang_statement_union_type ** field)
2784 *(list->tail) = element;
2788 /* Set the output format type */
2790 DEFUN (lang_add_output_format, (format),
2793 output_target = format;