file make-gas.com was initially added on branch binutils-2_10-branch.
[binutils.git] / ld / ldlang.c
blobd43140f06b2fa4970b3833e848110fb1dd2df06f
1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libiberty.h"
25 #include "obstack.h"
26 #include "bfdlink.h"
28 #include "ld.h"
29 #include "ldmain.h"
30 #include "ldgram.h"
31 #include "ldexp.h"
32 #include "ldlang.h"
33 #include "ldemul.h"
34 #include "ldlex.h"
35 #include "ldmisc.h"
36 #include "ldctor.h"
37 #include "ldfile.h"
38 #include "fnmatch.h"
39 #include "demangle.h"
41 #include <ctype.h>
43 /* FORWARDS */
44 static lang_statement_union_type *new_statement PARAMS ((enum statement_enum,
45 size_t,
46 lang_statement_list_type*));
49 /* LOCALS */
50 static struct obstack stat_obstack;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
54 static CONST char *startup_file;
55 static lang_statement_list_type input_file_chain;
56 static boolean placed_commons = false;
57 static lang_output_section_statement_type *default_common_section;
58 static boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static CONST char *current_target;
62 static CONST char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
66 static void lang_for_each_statement_worker
67 PARAMS ((void (*func) (lang_statement_union_type *),
68 lang_statement_union_type *s));
69 static lang_input_statement_type *new_afile
70 PARAMS ((const char *name, lang_input_file_enum_type file_type,
71 const char *target, boolean add_to_list));
72 static void init_os PARAMS ((lang_output_section_statement_type *s));
73 static void exp_init_os PARAMS ((etree_type *));
74 static void section_already_linked PARAMS ((bfd *, asection *, PTR));
75 static struct bfd_hash_entry *already_linked_newfunc
76 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
77 const char *string));
78 static void already_linked_table_init PARAMS ((void));
79 static void already_linked_table_free PARAMS ((void));
80 static boolean wildcardp PARAMS ((const char *));
81 static lang_statement_union_type *wild_sort
82 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
83 asection *));
84 static lang_input_statement_type *lookup_name PARAMS ((const char *name));
85 static void load_symbols PARAMS ((lang_input_statement_type *entry,
86 lang_statement_list_type *));
87 static void wild PARAMS ((lang_wild_statement_type *s,
88 const char *section, const char *file,
89 const char *target,
90 lang_output_section_statement_type *output));
91 static bfd *open_output PARAMS ((const char *name));
92 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
93 static void open_input_bfds
94 PARAMS ((lang_statement_union_type *statement, boolean));
95 static void lang_reasonable_defaults PARAMS ((void));
96 static void lang_place_undefineds PARAMS ((void));
97 static void map_input_to_output_sections
98 PARAMS ((lang_statement_union_type *s,
99 const char *target,
100 lang_output_section_statement_type *output_section_statement));
101 static void print_output_section_statement
102 PARAMS ((lang_output_section_statement_type *output_section_statement));
103 static void print_assignment
104 PARAMS ((lang_assignment_statement_type *assignment,
105 lang_output_section_statement_type *output_section));
106 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
107 static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
108 static void print_input_section PARAMS ((lang_input_section_type *in));
109 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
110 static void print_data_statement PARAMS ((lang_data_statement_type *data));
111 static void print_address_statement PARAMS ((lang_address_statement_type *));
112 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
113 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
114 static void print_wild_statement
115 PARAMS ((lang_wild_statement_type *w,
116 lang_output_section_statement_type *os));
117 static void print_group
118 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
119 static void print_statement PARAMS ((lang_statement_union_type *s,
120 lang_output_section_statement_type *os));
121 static void print_statement_list PARAMS ((lang_statement_union_type *s,
122 lang_output_section_statement_type *os));
123 static void print_statements PARAMS ((void));
124 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
125 fill_type fill, unsigned int power,
126 asection *output_section_statement,
127 bfd_vma dot));
128 static bfd_vma size_input_section
129 PARAMS ((lang_statement_union_type **this_ptr,
130 lang_output_section_statement_type *output_section_statement,
131 fill_type fill, bfd_vma dot, boolean relax));
132 static void lang_finish PARAMS ((void));
133 static void ignore_bfd_errors PARAMS ((const char *, ...));
134 static void lang_check PARAMS ((void));
135 static void lang_common PARAMS ((void));
136 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
137 static void lang_place_orphans PARAMS ((void));
138 static int topower PARAMS ((int));
139 static void lang_set_startof PARAMS ((void));
140 static void reset_memory_regions PARAMS ((void));
141 static void lang_record_phdrs PARAMS ((void));
142 static void lang_gc_wild
143 PARAMS ((lang_wild_statement_type *, const char *, const char *));
144 static void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
145 static void lang_gc_sections PARAMS ((void));
146 static void lang_do_version_exports_section PARAMS ((void));
147 static void lang_check_section_addresses PARAMS ((void));
149 typedef void (*callback_t) PARAMS ((lang_wild_statement_type *,
150 asection *, lang_input_statement_type *,
151 void *));
152 static void walk_wild_section
153 PARAMS ((lang_wild_statement_type *, const char *,
154 lang_input_statement_type *, callback_t, void *));
155 static void walk_wild_file
156 PARAMS ((lang_wild_statement_type *, const char *,
157 lang_input_statement_type *, callback_t, void *));
159 static int get_target PARAMS ((const bfd_target *, void *));
160 static void stricpy PARAMS ((char *, char *));
161 static void strcut PARAMS ((char *, char *));
162 static int name_compare PARAMS ((char *, char *));
163 static int closest_target_match PARAMS ((const bfd_target *, void *));
164 static char * get_first_input_target PARAMS ((void));
166 /* EXPORTS */
167 lang_output_section_statement_type *abs_output_section;
168 lang_statement_list_type lang_output_section_statement;
169 lang_statement_list_type *stat_ptr = &statement_list;
170 lang_statement_list_type file_chain = { NULL, NULL };
171 const char *entry_symbol = NULL;
172 boolean entry_from_cmdline;
173 boolean lang_has_input_file = false;
174 boolean had_output_filename = false;
175 boolean lang_float_flag = false;
176 boolean delete_output_file_on_failure = false;
177 struct lang_nocrossrefs *nocrossref_list;
179 etree_type *base; /* Relocation base - or null */
182 #if defined(__STDC__) || defined(ALMOST_STDC)
183 #define cat(a,b) a##b
184 #else
185 #define cat(a,b) a/**/b
186 #endif
188 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
190 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
192 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
194 #define SECTION_NAME_MAP_LENGTH (16)
197 stat_alloc (size)
198 size_t size;
200 return obstack_alloc (&stat_obstack, size);
203 /*----------------------------------------------------------------------
204 Generic traversal routines for finding matching sections.
207 static void
208 walk_wild_section (ptr, section, file, callback, data)
209 lang_wild_statement_type *ptr;
210 const char *section;
211 lang_input_statement_type *file;
212 callback_t callback;
213 void *data;
215 /* Don't process sections from files which were excluded. */
216 if (ptr->exclude_filename_list != NULL)
218 struct name_list *list_tmp;
219 for (list_tmp = ptr->exclude_filename_list; list_tmp; list_tmp = list_tmp->next)
221 boolean match;
223 if (wildcardp (list_tmp->name))
224 match = fnmatch (list_tmp->name, file->filename, 0) == 0 ? true : false;
225 else
226 match = strcmp (list_tmp->name, file->filename) == 0 ? true : false;
228 if (match)
229 return;
233 if (file->just_syms_flag == false)
235 register asection *s;
236 boolean wildcard;
238 if (section == NULL)
239 wildcard = false;
240 else
241 wildcard = wildcardp (section);
243 for (s = file->the_bfd->sections; s != NULL; s = s->next)
245 boolean match;
247 if (section == NULL)
248 match = true;
249 else
251 const char *name;
253 name = bfd_get_section_name (file->the_bfd, s);
254 if (wildcard)
255 match = fnmatch (section, name, 0) == 0 ? true : false;
256 else
257 match = strcmp (section, name) == 0 ? true : false;
260 if (match)
261 (*callback) (ptr, s, file, data);
266 /* Handle a wild statement for a single file F. */
268 static void
269 walk_wild_file (s, section, f, callback, data)
270 lang_wild_statement_type *s;
271 const char *section;
272 lang_input_statement_type *f;
273 callback_t callback;
274 void *data;
276 if (f->the_bfd == NULL
277 || ! bfd_check_format (f->the_bfd, bfd_archive))
278 walk_wild_section (s, section, f, callback, data);
279 else
281 bfd *member;
283 /* This is an archive file. We must map each member of the
284 archive separately. */
285 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
286 while (member != NULL)
288 /* When lookup_name is called, it will call the add_symbols
289 entry point for the archive. For each element of the
290 archive which is included, BFD will call ldlang_add_file,
291 which will set the usrdata field of the member to the
292 lang_input_statement. */
293 if (member->usrdata != NULL)
295 walk_wild_section (s, section,
296 (lang_input_statement_type *) member->usrdata,
297 callback, data);
300 member = bfd_openr_next_archived_file (f->the_bfd, member);
305 static void
306 walk_wild (s, section, file, callback, data)
307 lang_wild_statement_type *s;
308 const char *section;
309 const char *file;
310 callback_t callback;
311 void *data;
313 if (file == (char *) NULL)
315 /* Perform the iteration over all files in the list. */
316 LANG_FOR_EACH_INPUT_STATEMENT (f)
318 walk_wild_file (s, section, f, callback, data);
321 else if (wildcardp (file))
323 LANG_FOR_EACH_INPUT_STATEMENT (f)
325 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
326 walk_wild_file (s, section, f, callback, data);
329 else
331 lang_input_statement_type *f;
333 /* Perform the iteration over a single file. */
334 f = lookup_name (file);
335 walk_wild_file (s, section, f, callback, data);
339 /*----------------------------------------------------------------------
340 lang_for_each_statement walks the parse tree and calls the provided
341 function for each node
344 static void
345 lang_for_each_statement_worker (func, s)
346 void (*func) PARAMS ((lang_statement_union_type *));
347 lang_statement_union_type *s;
349 for (; s != (lang_statement_union_type *) NULL; s = s->next)
351 func (s);
353 switch (s->header.type)
355 case lang_constructors_statement_enum:
356 lang_for_each_statement_worker (func, constructor_list.head);
357 break;
358 case lang_output_section_statement_enum:
359 lang_for_each_statement_worker
360 (func,
361 s->output_section_statement.children.head);
362 break;
363 case lang_wild_statement_enum:
364 lang_for_each_statement_worker
365 (func,
366 s->wild_statement.children.head);
367 break;
368 case lang_group_statement_enum:
369 lang_for_each_statement_worker (func,
370 s->group_statement.children.head);
371 break;
372 case lang_data_statement_enum:
373 case lang_reloc_statement_enum:
374 case lang_object_symbols_statement_enum:
375 case lang_output_statement_enum:
376 case lang_target_statement_enum:
377 case lang_input_section_enum:
378 case lang_input_statement_enum:
379 case lang_assignment_statement_enum:
380 case lang_padding_statement_enum:
381 case lang_address_statement_enum:
382 case lang_fill_statement_enum:
383 break;
384 default:
385 FAIL ();
386 break;
391 void
392 lang_for_each_statement (func)
393 void (*func) PARAMS ((lang_statement_union_type *));
395 lang_for_each_statement_worker (func,
396 statement_list.head);
399 /*----------------------------------------------------------------------*/
400 void
401 lang_list_init (list)
402 lang_statement_list_type *list;
404 list->head = (lang_statement_union_type *) NULL;
405 list->tail = &list->head;
408 /*----------------------------------------------------------------------
410 build a new statement node for the parse tree
414 static
415 lang_statement_union_type *
416 new_statement (type, size, list)
417 enum statement_enum type;
418 size_t size;
419 lang_statement_list_type * list;
421 lang_statement_union_type *new = (lang_statement_union_type *)
422 stat_alloc (size);
424 new->header.type = type;
425 new->header.next = (lang_statement_union_type *) NULL;
426 lang_statement_append (list, new, &new->header.next);
427 return new;
431 Build a new input file node for the language. There are several ways
432 in which we treat an input file, eg, we only look at symbols, or
433 prefix it with a -l etc.
435 We can be supplied with requests for input files more than once;
436 they may, for example be split over serveral lines like foo.o(.text)
437 foo.o(.data) etc, so when asked for a file we check that we havn't
438 got it already so we don't duplicate the bfd.
441 static lang_input_statement_type *
442 new_afile (name, file_type, target, add_to_list)
443 CONST char *name;
444 lang_input_file_enum_type file_type;
445 CONST char *target;
446 boolean add_to_list;
448 lang_input_statement_type *p;
450 if (add_to_list)
451 p = new_stat (lang_input_statement, stat_ptr);
452 else
454 p = ((lang_input_statement_type *)
455 stat_alloc (sizeof (lang_input_statement_type)));
456 p->header.next = NULL;
459 lang_has_input_file = true;
460 p->target = target;
461 switch (file_type)
463 case lang_input_file_is_symbols_only_enum:
464 p->filename = name;
465 p->is_archive = false;
466 p->real = true;
467 p->local_sym_name = name;
468 p->just_syms_flag = true;
469 p->search_dirs_flag = false;
470 break;
471 case lang_input_file_is_fake_enum:
472 p->filename = name;
473 p->is_archive = false;
474 p->real = false;
475 p->local_sym_name = name;
476 p->just_syms_flag = false;
477 p->search_dirs_flag = false;
478 break;
479 case lang_input_file_is_l_enum:
480 p->is_archive = true;
481 p->filename = name;
482 p->real = true;
483 p->local_sym_name = concat ("-l", name, (const char *) NULL);
484 p->just_syms_flag = false;
485 p->search_dirs_flag = true;
486 break;
487 case lang_input_file_is_marker_enum:
488 p->filename = name;
489 p->is_archive = false;
490 p->real = false;
491 p->local_sym_name = name;
492 p->just_syms_flag = false;
493 p->search_dirs_flag = true;
494 break;
495 case lang_input_file_is_search_file_enum:
496 p->filename = name;
497 p->is_archive = false;
498 p->real = true;
499 p->local_sym_name = name;
500 p->just_syms_flag = false;
501 p->search_dirs_flag = true;
502 break;
503 case lang_input_file_is_file_enum:
504 p->filename = name;
505 p->is_archive = false;
506 p->real = true;
507 p->local_sym_name = name;
508 p->just_syms_flag = false;
509 p->search_dirs_flag = false;
510 break;
511 default:
512 FAIL ();
514 p->the_bfd = (bfd *) NULL;
515 p->asymbols = (asymbol **) NULL;
516 p->next_real_file = (lang_statement_union_type *) NULL;
517 p->next = (lang_statement_union_type *) NULL;
518 p->symbol_count = 0;
519 p->dynamic = config.dynamic_link;
520 p->whole_archive = whole_archive;
521 p->loaded = false;
522 lang_statement_append (&input_file_chain,
523 (lang_statement_union_type *) p,
524 &p->next_real_file);
525 return p;
528 lang_input_statement_type *
529 lang_add_input_file (name, file_type, target)
530 CONST char *name;
531 lang_input_file_enum_type file_type;
532 CONST char *target;
534 lang_has_input_file = true;
535 return new_afile (name, file_type, target, true);
538 /* Build enough state so that the parser can build its tree */
539 void
540 lang_init ()
542 obstack_begin (&stat_obstack, 1000);
544 stat_ptr = &statement_list;
546 lang_list_init (stat_ptr);
548 lang_list_init (&input_file_chain);
549 lang_list_init (&lang_output_section_statement);
550 lang_list_init (&file_chain);
551 first_file = lang_add_input_file ((char *) NULL,
552 lang_input_file_is_marker_enum,
553 (char *) NULL);
554 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
556 abs_output_section->bfd_section = bfd_abs_section_ptr;
560 /*----------------------------------------------------------------------
561 A region is an area of memory declared with the
562 MEMORY { name:org=exp, len=exp ... }
563 syntax.
565 We maintain a list of all the regions here
567 If no regions are specified in the script, then the default is used
568 which is created when looked up to be the entire data space
571 static lang_memory_region_type *lang_memory_region_list;
572 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
574 lang_memory_region_type *
575 lang_memory_region_lookup (name)
576 CONST char *CONST name;
578 lang_memory_region_type *p;
580 for (p = lang_memory_region_list;
581 p != (lang_memory_region_type *) NULL;
582 p = p->next)
584 if (strcmp (p->name, name) == 0)
586 return p;
590 #if 0
591 /* This code used to always use the first region in the list as the
592 default region. I changed it to instead use a region
593 encompassing all of memory as the default region. This permits
594 NOLOAD sections to work reasonably without requiring a region.
595 People should specify what region they mean, if they really want
596 a region. */
597 if (strcmp (name, "*default*") == 0)
599 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
601 return lang_memory_region_list;
604 #endif
607 lang_memory_region_type *new =
608 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
610 new->name = buystring (name);
611 new->next = (lang_memory_region_type *) NULL;
613 *lang_memory_region_list_tail = new;
614 lang_memory_region_list_tail = &new->next;
615 new->origin = 0;
616 new->flags = 0;
617 new->not_flags = 0;
618 new->length = ~(bfd_size_type)0;
619 new->current = 0;
620 new->had_full_message = false;
622 return new;
627 lang_memory_region_type *
628 lang_memory_default (section)
629 asection *section;
631 lang_memory_region_type *p;
633 flagword sec_flags = section->flags;
635 /* Override SEC_DATA to mean a writable section. */
636 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
637 sec_flags |= SEC_DATA;
639 for (p = lang_memory_region_list;
640 p != (lang_memory_region_type *) NULL;
641 p = p->next)
643 if ((p->flags & sec_flags) != 0
644 && (p->not_flags & sec_flags) == 0)
646 return p;
649 return lang_memory_region_lookup ("*default*");
652 lang_output_section_statement_type *
653 lang_output_section_find (name)
654 CONST char *CONST name;
656 lang_statement_union_type *u;
657 lang_output_section_statement_type *lookup;
659 for (u = lang_output_section_statement.head;
660 u != (lang_statement_union_type *) NULL;
661 u = lookup->next)
663 lookup = &u->output_section_statement;
664 if (strcmp (name, lookup->name) == 0)
666 return lookup;
669 return (lang_output_section_statement_type *) NULL;
672 lang_output_section_statement_type *
673 lang_output_section_statement_lookup (name)
674 CONST char *CONST name;
676 lang_output_section_statement_type *lookup;
678 lookup = lang_output_section_find (name);
679 if (lookup == (lang_output_section_statement_type *) NULL)
682 lookup = (lang_output_section_statement_type *)
683 new_stat (lang_output_section_statement, stat_ptr);
684 lookup->region = (lang_memory_region_type *) NULL;
685 lookup->lma_region = (lang_memory_region_type *) NULL;
686 lookup->fill = 0;
687 lookup->block_value = 1;
688 lookup->name = name;
690 lookup->next = (lang_statement_union_type *) NULL;
691 lookup->bfd_section = (asection *) NULL;
692 lookup->processed = false;
693 lookup->sectype = normal_section;
694 lookup->addr_tree = (etree_type *) NULL;
695 lang_list_init (&lookup->children);
697 lookup->memspec = (CONST char *) NULL;
698 lookup->flags = 0;
699 lookup->subsection_alignment = -1;
700 lookup->section_alignment = -1;
701 lookup->load_base = (union etree_union *) NULL;
702 lookup->phdrs = NULL;
704 lang_statement_append (&lang_output_section_statement,
705 (lang_statement_union_type *) lookup,
706 &lookup->next);
708 return lookup;
711 static void
712 lang_map_flags (flag)
713 flagword flag;
715 if (flag & SEC_ALLOC)
716 minfo ("a");
718 if (flag & SEC_CODE)
719 minfo ("x");
721 if (flag & SEC_READONLY)
722 minfo ("r");
724 if (flag & SEC_DATA)
725 minfo ("w");
727 if (flag & SEC_LOAD)
728 minfo ("l");
731 void
732 lang_map ()
734 lang_memory_region_type *m;
736 minfo (_("\nMemory Configuration\n\n"));
737 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
738 _("Name"), _("Origin"), _("Length"), _("Attributes"));
740 for (m = lang_memory_region_list;
741 m != (lang_memory_region_type *) NULL;
742 m = m->next)
744 char buf[100];
745 int len;
747 fprintf (config.map_file, "%-16s ", m->name);
749 sprintf_vma (buf, m->origin);
750 minfo ("0x%s ", buf);
751 len = strlen (buf);
752 while (len < 16)
754 print_space ();
755 ++len;
758 minfo ("0x%V", m->length);
759 if (m->flags || m->not_flags)
761 #ifndef BFD64
762 minfo (" ");
763 #endif
764 if (m->flags)
766 print_space ();
767 lang_map_flags (m->flags);
770 if (m->not_flags)
772 minfo (" !");
773 lang_map_flags (m->not_flags);
777 print_nl ();
780 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
782 print_statements ();
785 /* Initialize an output section. */
787 static void
788 init_os (s)
789 lang_output_section_statement_type *s;
791 section_userdata_type *new;
793 if (s->bfd_section != NULL)
794 return;
796 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
797 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME);
799 new = ((section_userdata_type *)
800 stat_alloc (sizeof (section_userdata_type)));
802 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
803 if (s->bfd_section == (asection *) NULL)
804 s->bfd_section = bfd_make_section (output_bfd, s->name);
805 if (s->bfd_section == (asection *) NULL)
807 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
808 output_bfd->xvec->name, s->name);
810 s->bfd_section->output_section = s->bfd_section;
812 /* We initialize an output sections output offset to minus its own */
813 /* vma to allow us to output a section through itself */
814 s->bfd_section->output_offset = 0;
815 get_userdata (s->bfd_section) = (PTR) new;
817 /* If there is a base address, make sure that any sections it might
818 mention are initialized. */
819 if (s->addr_tree != NULL)
820 exp_init_os (s->addr_tree);
823 /* Make sure that all output sections mentioned in an expression are
824 initialized. */
826 static void
827 exp_init_os (exp)
828 etree_type *exp;
830 switch (exp->type.node_class)
832 case etree_assign:
833 exp_init_os (exp->assign.src);
834 break;
836 case etree_binary:
837 exp_init_os (exp->binary.lhs);
838 exp_init_os (exp->binary.rhs);
839 break;
841 case etree_trinary:
842 exp_init_os (exp->trinary.cond);
843 exp_init_os (exp->trinary.lhs);
844 exp_init_os (exp->trinary.rhs);
845 break;
847 case etree_unary:
848 exp_init_os (exp->unary.child);
849 break;
851 case etree_name:
852 switch (exp->type.node_code)
854 case ADDR:
855 case LOADADDR:
856 case SIZEOF:
858 lang_output_section_statement_type *os;
860 os = lang_output_section_find (exp->name.name);
861 if (os != NULL && os->bfd_section == NULL)
862 init_os (os);
865 break;
867 default:
868 break;
872 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
873 once into the output. This routine checks each section, and
874 arrange to discard it if a section of the same name has already
875 been linked. If the section has COMDAT information, then it uses
876 that to decide whether the section should be included. This code
877 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
878 that is, it does not depend solely upon the section name.
879 section_already_linked is called via bfd_map_over_sections. */
881 /* This is the shape of the elements inside the already_linked hash
882 table. It maps a name onto a list of already_linked elements with
883 the same name. It's possible to get more than one element in a
884 list if the COMDAT sections have different names. */
886 struct already_linked_hash_entry
888 struct bfd_hash_entry root;
889 struct already_linked *entry;
892 struct already_linked
894 struct already_linked *next;
895 asection *sec;
898 /* The hash table. */
900 static struct bfd_hash_table already_linked_table;
902 /*ARGSUSED*/
903 static void
904 section_already_linked (abfd, sec, data)
905 bfd *abfd;
906 asection *sec;
907 PTR data;
909 lang_input_statement_type *entry = (lang_input_statement_type *) data;
910 flagword flags;
911 const char *name;
912 struct already_linked *l;
913 struct already_linked_hash_entry *already_linked_list;
915 /* If we are only reading symbols from this object, then we want to
916 discard all sections. */
917 if (entry->just_syms_flag)
919 sec->output_section = bfd_abs_section_ptr;
920 sec->output_offset = sec->vma;
921 return;
924 flags = bfd_get_section_flags (abfd, sec);
926 if ((flags & SEC_LINK_ONCE) == 0)
927 return;
929 /* FIXME: When doing a relocateable link, we may have trouble
930 copying relocations in other sections that refer to local symbols
931 in the section being discarded. Those relocations will have to
932 be converted somehow; as of this writing I'm not sure that any of
933 the backends handle that correctly.
935 It is tempting to instead not discard link once sections when
936 doing a relocateable link (technically, they should be discarded
937 whenever we are building constructors). However, that fails,
938 because the linker winds up combining all the link once sections
939 into a single large link once section, which defeats the purpose
940 of having link once sections in the first place.
942 Also, not merging link once sections in a relocateable link
943 causes trouble for MIPS ELF, which relies in link once semantics
944 to handle the .reginfo section correctly. */
946 name = bfd_get_section_name (abfd, sec);
948 already_linked_list =
949 ((struct already_linked_hash_entry *)
950 bfd_hash_lookup (&already_linked_table, name, true, false));
952 for (l = already_linked_list->entry; l != NULL; l = l->next)
954 if (sec->comdat == NULL
955 || l->sec->comdat == NULL
956 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
958 /* The section has already been linked. See if we should
959 issue a warning. */
960 switch (flags & SEC_LINK_DUPLICATES)
962 default:
963 abort ();
965 case SEC_LINK_DUPLICATES_DISCARD:
966 break;
968 case SEC_LINK_DUPLICATES_ONE_ONLY:
969 if (sec->comdat == NULL)
970 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
971 abfd, name);
972 else
973 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
974 abfd, name, sec->comdat->name);
975 break;
977 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
978 /* FIXME: We should really dig out the contents of both
979 sections and memcmp them. The COFF/PE spec says that
980 the Microsoft linker does not implement this
981 correctly, so I'm not going to bother doing it
982 either. */
983 /* Fall through. */
984 case SEC_LINK_DUPLICATES_SAME_SIZE:
985 if (bfd_section_size (abfd, sec)
986 != bfd_section_size (l->sec->owner, l->sec))
987 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
988 abfd, name);
989 break;
992 /* Set the output_section field so that wild_doit does not
993 create a lang_input_section structure for this section.
994 Since there might be a symbol in the section being
995 discarded, we must retain a pointer to the section which
996 we are really going to use. */
997 sec->output_section = bfd_abs_section_ptr;
998 if (sec->comdat != NULL)
999 sec->comdat->sec = l->sec;
1001 return;
1005 /* This is the first section with this name. Record it. Allocate
1006 the memory from the same obstack as the hash table is kept in. */
1008 l = ((struct already_linked *)
1009 bfd_hash_allocate (&already_linked_table, sizeof *l));
1011 l->sec = sec;
1012 l->next = already_linked_list->entry;
1013 already_linked_list->entry = l;
1016 /* Support routines for the hash table used by section_already_linked,
1017 initialize the table, fill in an entry and remove the table. */
1019 static struct bfd_hash_entry *
1020 already_linked_newfunc (entry, table, string)
1021 struct bfd_hash_entry *entry ATTRIBUTE_UNUSED;
1022 struct bfd_hash_table *table;
1023 const char *string ATTRIBUTE_UNUSED;
1025 struct already_linked_hash_entry *ret =
1026 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
1028 ret->entry = NULL;
1030 return (struct bfd_hash_entry *) ret;
1033 static void
1034 already_linked_table_init ()
1036 if (! bfd_hash_table_init_n (&already_linked_table,
1037 already_linked_newfunc,
1038 42))
1039 einfo (_("%P%F: Failed to create hash table\n"));
1042 static void
1043 already_linked_table_free ()
1045 bfd_hash_table_free (&already_linked_table);
1048 /* The wild routines.
1050 These expand statements like *(.text) and foo.o to a list of
1051 explicit actions, like foo.o(.text), bar.o(.text) and
1052 foo.o(.text, .data). */
1054 /* Return true if the PATTERN argument is a wildcard pattern.
1055 Although backslashes are treated specially if a pattern contains
1056 wildcards, we do not consider the mere presence of a backslash to
1057 be enough to cause the the pattern to be treated as a wildcard.
1058 That lets us handle DOS filenames more naturally. */
1060 static boolean
1061 wildcardp (pattern)
1062 const char *pattern;
1064 const char *s;
1066 for (s = pattern; *s != '\0'; ++s)
1067 if (*s == '?'
1068 || *s == '*'
1069 || *s == '[')
1070 return true;
1071 return false;
1074 /* Add SECTION to the output section OUTPUT. Do this by creating a
1075 lang_input_section statement which is placed at PTR. FILE is the
1076 input file which holds SECTION. */
1078 void
1079 wild_doit (ptr, section, output, file)
1080 lang_statement_list_type *ptr;
1081 asection *section;
1082 lang_output_section_statement_type *output;
1083 lang_input_statement_type *file;
1085 flagword flags;
1086 boolean discard;
1088 flags = bfd_get_section_flags (section->owner, section);
1090 discard = false;
1092 /* If we are doing a final link, discard sections marked with
1093 SEC_EXCLUDE. */
1094 if (! link_info.relocateable
1095 && (flags & SEC_EXCLUDE) != 0)
1096 discard = true;
1098 /* Discard input sections which are assigned to a section named
1099 DISCARD_SECTION_NAME. */
1100 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1101 discard = true;
1103 /* Discard debugging sections if we are stripping debugging
1104 information. */
1105 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1106 && (flags & SEC_DEBUGGING) != 0)
1107 discard = true;
1109 if (discard)
1111 if (section->output_section == NULL)
1113 /* This prevents future calls from assigning this section. */
1114 section->output_section = bfd_abs_section_ptr;
1116 return;
1119 if (section->output_section == NULL)
1121 boolean first;
1122 lang_input_section_type *new;
1123 flagword flags;
1125 if (output->bfd_section == NULL)
1127 init_os (output);
1128 first = true;
1130 else
1131 first = false;
1133 /* Add a section reference to the list */
1134 new = new_stat (lang_input_section, ptr);
1136 new->section = section;
1137 new->ifile = file;
1138 section->output_section = output->bfd_section;
1140 flags = section->flags;
1142 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1143 to an output section, because we want to be able to include a
1144 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1145 section (I don't know why we want to do this, but we do).
1146 build_link_order in ldwrite.c handles this case by turning
1147 the embedded SEC_NEVER_LOAD section into a fill. */
1149 flags &= ~ SEC_NEVER_LOAD;
1151 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1152 already been processed. One reason to do this is that on pe
1153 format targets, .text$foo sections go into .text and it's odd
1154 to see .text with SEC_LINK_ONCE set. */
1156 if (! link_info.relocateable)
1157 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1159 /* If this is not the first input section, and the SEC_READONLY
1160 flag is not currently set, then don't set it just because the
1161 input section has it set. */
1163 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1164 flags &= ~ SEC_READONLY;
1166 section->output_section->flags |= flags;
1168 /* If SEC_READONLY is not set in the input section, then clear
1169 it from the output section. */
1170 if ((section->flags & SEC_READONLY) == 0)
1171 section->output_section->flags &= ~SEC_READONLY;
1173 switch (output->sectype)
1175 case normal_section:
1176 break;
1177 case dsect_section:
1178 case copy_section:
1179 case info_section:
1180 case overlay_section:
1181 output->bfd_section->flags &= ~SEC_ALLOC;
1182 break;
1183 case noload_section:
1184 output->bfd_section->flags &= ~SEC_LOAD;
1185 output->bfd_section->flags |= SEC_NEVER_LOAD;
1186 break;
1189 /* Copy over SEC_SMALL_DATA. */
1190 if (section->flags & SEC_SMALL_DATA)
1191 section->output_section->flags |= SEC_SMALL_DATA;
1193 if (section->alignment_power > output->bfd_section->alignment_power)
1194 output->bfd_section->alignment_power = section->alignment_power;
1196 /* If supplied an aligment, then force it. */
1197 if (output->section_alignment != -1)
1198 output->bfd_section->alignment_power = output->section_alignment;
1202 /* Handle wildcard sorting. This returns the lang_input_section which
1203 should follow the one we are going to create for SECTION and FILE,
1204 based on the sorting requirements of WILD. It returns NULL if the
1205 new section should just go at the end of the current list. */
1207 static lang_statement_union_type *
1208 wild_sort (wild, file, section)
1209 lang_wild_statement_type *wild;
1210 lang_input_statement_type *file;
1211 asection *section;
1213 const char *section_name;
1214 lang_statement_union_type *l;
1216 if (! wild->filenames_sorted && ! wild->sections_sorted)
1217 return NULL;
1219 section_name = bfd_get_section_name (file->the_bfd, section);
1220 for (l = wild->children.head; l != NULL; l = l->next)
1222 lang_input_section_type *ls;
1224 if (l->header.type != lang_input_section_enum)
1225 continue;
1226 ls = &l->input_section;
1228 /* Sorting by filename takes precedence over sorting by section
1229 name. */
1231 if (wild->filenames_sorted)
1233 const char *fn, *ln;
1234 boolean fa, la;
1235 int i;
1237 /* The PE support for the .idata section as generated by
1238 dlltool assumes that files will be sorted by the name of
1239 the archive and then the name of the file within the
1240 archive. */
1242 if (file->the_bfd != NULL
1243 && bfd_my_archive (file->the_bfd) != NULL)
1245 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1246 fa = true;
1248 else
1250 fn = file->filename;
1251 fa = false;
1254 if (ls->ifile->the_bfd != NULL
1255 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1257 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1258 la = true;
1260 else
1262 ln = ls->ifile->filename;
1263 la = false;
1266 i = strcmp (fn, ln);
1267 if (i > 0)
1268 continue;
1269 else if (i < 0)
1270 break;
1272 if (fa || la)
1274 if (fa)
1275 fn = file->filename;
1276 if (la)
1277 ln = ls->ifile->filename;
1279 i = strcmp (fn, ln);
1280 if (i > 0)
1281 continue;
1282 else if (i < 0)
1283 break;
1287 /* Here either the files are not sorted by name, or we are
1288 looking at the sections for this file. */
1290 if (wild->sections_sorted)
1292 if (strcmp (section_name,
1293 bfd_get_section_name (ls->ifile->the_bfd,
1294 ls->section))
1295 < 0)
1296 break;
1300 return l;
1303 /* Expand a wild statement for a particular FILE. SECTION may be
1304 NULL, in which case it is a wild card. */
1306 static void
1307 output_section_callback (ptr, section, file, output)
1308 lang_wild_statement_type *ptr;
1309 asection *section;
1310 lang_input_statement_type *file;
1311 void *output;
1313 lang_statement_union_type *before;
1315 /* If the wild pattern was marked KEEP, the member sections
1316 should be as well. */
1317 if (ptr->keep_sections)
1318 section->flags |= SEC_KEEP;
1320 before = wild_sort (ptr, file, section);
1322 /* Here BEFORE points to the lang_input_section which
1323 should follow the one we are about to add. If BEFORE
1324 is NULL, then the section should just go at the end
1325 of the current list. */
1327 if (before == NULL)
1328 wild_doit (&ptr->children, section,
1329 (lang_output_section_statement_type *) output,
1330 file);
1331 else
1333 lang_statement_list_type list;
1334 lang_statement_union_type **pp;
1336 lang_list_init (&list);
1337 wild_doit (&list, section,
1338 (lang_output_section_statement_type *) output,
1339 file);
1341 /* If we are discarding the section, LIST.HEAD will
1342 be NULL. */
1343 if (list.head != NULL)
1345 ASSERT (list.head->next == NULL);
1347 for (pp = &ptr->children.head;
1348 *pp != before;
1349 pp = &(*pp)->next)
1350 ASSERT (*pp != NULL);
1352 list.head->next = *pp;
1353 *pp = list.head;
1358 /* This is passed a file name which must have been seen already and
1359 added to the statement tree. We will see if it has been opened
1360 already and had its symbols read. If not then we'll read it. */
1362 static lang_input_statement_type *
1363 lookup_name (name)
1364 const char *name;
1366 lang_input_statement_type *search;
1368 for (search = (lang_input_statement_type *) input_file_chain.head;
1369 search != (lang_input_statement_type *) NULL;
1370 search = (lang_input_statement_type *) search->next_real_file)
1372 if (search->filename == (char *) NULL && name == (char *) NULL)
1373 return search;
1374 if (search->filename != (char *) NULL
1375 && name != (char *) NULL
1376 && strcmp (search->filename, name) == 0)
1377 break;
1380 if (search == (lang_input_statement_type *) NULL)
1381 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1382 false);
1384 /* If we have already added this file, or this file is not real
1385 (FIXME: can that ever actually happen?) or the name is NULL
1386 (FIXME: can that ever actually happen?) don't add this file. */
1387 if (search->loaded
1388 || ! search->real
1389 || search->filename == (const char *) NULL)
1390 return search;
1392 load_symbols (search, (lang_statement_list_type *) NULL);
1394 return search;
1397 /* Get the symbols for an input file. */
1399 static void
1400 load_symbols (entry, place)
1401 lang_input_statement_type *entry;
1402 lang_statement_list_type *place;
1404 char **matching;
1406 if (entry->loaded)
1407 return;
1409 ldfile_open_file (entry);
1411 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1412 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1414 bfd_error_type err;
1415 lang_statement_list_type *hold;
1417 err = bfd_get_error ();
1418 if (err == bfd_error_file_ambiguously_recognized)
1420 char **p;
1422 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1423 einfo (_("%B: matching formats:"), entry->the_bfd);
1424 for (p = matching; *p != NULL; p++)
1425 einfo (" %s", *p);
1426 einfo ("%F\n");
1428 else if (err != bfd_error_file_not_recognized
1429 || place == NULL)
1430 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1432 bfd_close (entry->the_bfd);
1433 entry->the_bfd = NULL;
1435 /* See if the emulation has some special knowledge. */
1437 if (ldemul_unrecognized_file (entry))
1438 return;
1440 /* Try to interpret the file as a linker script. */
1442 ldfile_open_command_file (entry->filename);
1444 hold = stat_ptr;
1445 stat_ptr = place;
1447 ldfile_assumed_script = true;
1448 parser_input = input_script;
1449 yyparse ();
1450 ldfile_assumed_script = false;
1452 stat_ptr = hold;
1454 return;
1457 if (ldemul_recognized_file (entry))
1458 return;
1460 /* We don't call ldlang_add_file for an archive. Instead, the
1461 add_symbols entry point will call ldlang_add_file, via the
1462 add_archive_element callback, for each element of the archive
1463 which is used. */
1464 switch (bfd_get_format (entry->the_bfd))
1466 default:
1467 break;
1469 case bfd_object:
1470 ldlang_add_file (entry);
1471 if (trace_files || trace_file_tries)
1472 info_msg ("%I\n", entry);
1473 break;
1475 case bfd_archive:
1476 if (entry->whole_archive)
1478 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1479 (bfd *) NULL);
1480 while (member != NULL)
1482 if (! bfd_check_format (member, bfd_object))
1483 einfo (_("%F%B: object %B in archive is not object\n"),
1484 entry->the_bfd, member);
1485 if (! ((*link_info.callbacks->add_archive_element)
1486 (&link_info, member, "--whole-archive")))
1487 abort ();
1488 if (! bfd_link_add_symbols (member, &link_info))
1489 einfo (_("%F%B: could not read symbols: %E\n"), member);
1490 member = bfd_openr_next_archived_file (entry->the_bfd,
1491 member);
1494 entry->loaded = true;
1496 return;
1500 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
1501 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1503 entry->loaded = true;
1508 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1509 indicating that it is a wildcard. Separate lang_input_section
1510 statements are created for each part of the expansion; they are
1511 added after the wild statement S. OUTPUT is the output section. */
1513 static void
1514 wild (s, section, file, target, output)
1515 lang_wild_statement_type *s;
1516 const char *section;
1517 const char *file;
1518 const char *target ATTRIBUTE_UNUSED;
1519 lang_output_section_statement_type *output;
1521 walk_wild (s, section, file, output_section_callback, (void *) output);
1523 if (section != (char *) NULL
1524 && strcmp (section, "COMMON") == 0
1525 && default_common_section == NULL)
1527 /* Remember the section that common is going to in case we later
1528 get something which doesn't know where to put it. */
1529 default_common_section = output;
1533 /* Return true iff target is the sought target. */
1534 static int
1535 get_target (target, data)
1536 const bfd_target * target;
1537 void * data;
1539 const char * sought = (const char *) data;
1541 return strcmp (target->name, sought) == 0;
1544 /* Like strcpy() but convert to lower case as well. */
1545 static void
1546 stricpy (dest, src)
1547 char * dest;
1548 char * src;
1550 char c;
1552 while ((c = * src ++) != 0)
1554 if (isupper ((unsigned char) c))
1555 c = tolower (c);
1557 * dest ++ = c;
1560 * dest = 0;
1563 /* Remove the first occurance of needle (if any) in haystack
1564 from haystack. */
1565 static void
1566 strcut (haystack, needle)
1567 char * haystack;
1568 char * needle;
1570 haystack = strstr (haystack, needle);
1572 if (haystack)
1574 char * src;
1576 for (src = haystack + strlen (needle); * src;)
1577 * haystack ++ = * src ++;
1579 * haystack = 0;
1583 /* Compare two target format name strings.
1584 Return a value indicating how "similar" they are. */
1585 static int
1586 name_compare (first, second)
1587 char * first;
1588 char * second;
1590 char * copy1;
1591 char * copy2;
1592 int result;
1594 copy1 = xmalloc (strlen (first) + 1);
1595 copy2 = xmalloc (strlen (second) + 1);
1597 /* Convert the names to lower case. */
1598 stricpy (copy1, first);
1599 stricpy (copy2, second);
1601 /* Remove and endian strings from the name. */
1602 strcut (copy1, "big");
1603 strcut (copy1, "little");
1604 strcut (copy2, "big");
1605 strcut (copy2, "little");
1607 /* Return a value based on how many characters match,
1608 starting from the beginning. If both strings are
1609 the same then return 10 * their length. */
1610 for (result = 0; copy1 [result] == copy2 [result]; result ++)
1611 if (copy1 [result] == 0)
1613 result *= 10;
1614 break;
1617 free (copy1);
1618 free (copy2);
1620 return result;
1623 /* Set by closest_target_match() below. */
1624 static const bfd_target * winner;
1626 /* Scan all the valid bfd targets looking for one that has the endianness
1627 requirement that was specified on the command line, and is the nearest
1628 match to the original output target. */
1629 static int
1630 closest_target_match (target, data)
1631 const bfd_target * target;
1632 void * data;
1634 const bfd_target * original = (const bfd_target *) data;
1636 if (command_line.endian == ENDIAN_BIG && target->byteorder != BFD_ENDIAN_BIG)
1637 return 0;
1639 if (command_line.endian == ENDIAN_LITTLE && target->byteorder != BFD_ENDIAN_LITTLE)
1640 return 0;
1642 /* Must be the same flavour. */
1643 if (target->flavour != original->flavour)
1644 return 0;
1646 /* If we have not found a potential winner yet, then record this one. */
1647 if (winner == NULL)
1649 winner = target;
1650 return 0;
1653 /* Oh dear, we now have two potential candidates for a successful match.
1654 Compare their names and choose the better one. */
1655 if (name_compare (target->name, original->name) > name_compare (winner->name, original->name))
1656 winner = target;
1658 /* Keep on searching until wqe have checked them all. */
1659 return 0;
1662 /* Return the BFD target format of the first input file. */
1663 static char *
1664 get_first_input_target ()
1666 char * target = NULL;
1668 LANG_FOR_EACH_INPUT_STATEMENT (s)
1670 if (s->header.type == lang_input_statement_enum
1671 && s->real)
1673 ldfile_open_file (s);
1675 if (s->the_bfd != NULL
1676 && bfd_check_format (s->the_bfd, bfd_object))
1678 target = bfd_get_target (s->the_bfd);
1680 if (target != NULL)
1681 break;
1686 return target;
1689 /* Open the output file. */
1691 static bfd *
1692 open_output (name)
1693 const char * name;
1695 bfd * output;
1697 /* Has the user told us which output format to use ? */
1698 if (output_target == (char *) NULL)
1700 /* No - has the current target been set to something other than the default ? */
1701 if (current_target != default_target)
1702 output_target = current_target;
1704 /* No - can we determine the format of the first input file ? */
1705 else
1707 output_target = get_first_input_target ();
1709 /* Failed - use the default output target. */
1710 if (output_target == NULL)
1711 output_target = default_target;
1715 /* Has the user requested a particular endianness on the command line ? */
1716 if (command_line.endian != ENDIAN_UNSET)
1718 const bfd_target * target;
1719 enum bfd_endian desired_endian;
1721 /* Get the chosen target. */
1722 target = bfd_search_for_target (get_target, (void *) output_target);
1724 if (command_line.endian == ENDIAN_BIG)
1725 desired_endian = BFD_ENDIAN_BIG;
1726 else
1727 desired_endian = BFD_ENDIAN_LITTLE;
1729 /* See if the target has the wrong endianness. This should not happen
1730 if the linker script has provided big and little endian alternatives,
1731 but some scrips don't do this. */
1732 if (target->byteorder != desired_endian)
1734 /* If it does, then see if the target provides
1735 an alternative with the correct endianness. */
1736 if (target->alternative_target != NULL
1737 && (target->alternative_target->byteorder == desired_endian))
1738 output_target = target->alternative_target->name;
1739 else
1741 /* Try to find a target as similar as possible to the default
1742 target, but which has the desired endian characteristic. */
1743 (void) bfd_search_for_target (closest_target_match, (void *) target);
1745 /* Oh dear - we could not find any targets that satisfy our requirements. */
1746 if (winner == NULL)
1747 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1748 else
1749 output_target = winner->name;
1754 output = bfd_openw (name, output_target);
1756 if (output == (bfd *) NULL)
1758 if (bfd_get_error () == bfd_error_invalid_target)
1759 einfo (_("%P%F: target %s not found\n"), output_target);
1761 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1764 delete_output_file_on_failure = true;
1766 /* output->flags |= D_PAGED;*/
1768 if (! bfd_set_format (output, bfd_object))
1769 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1770 if (! bfd_set_arch_mach (output,
1771 ldfile_output_architecture,
1772 ldfile_output_machine))
1773 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1775 link_info.hash = bfd_link_hash_table_create (output);
1776 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1777 einfo (_("%P%F: can not create link hash table: %E\n"));
1779 bfd_set_gp_size (output, g_switch_value);
1780 return output;
1783 static void
1784 ldlang_open_output (statement)
1785 lang_statement_union_type * statement;
1787 switch (statement->header.type)
1789 case lang_output_statement_enum:
1790 ASSERT (output_bfd == (bfd *) NULL);
1791 output_bfd = open_output (statement->output_statement.name);
1792 ldemul_set_output_arch ();
1793 if (config.magic_demand_paged && !link_info.relocateable)
1794 output_bfd->flags |= D_PAGED;
1795 else
1796 output_bfd->flags &= ~D_PAGED;
1797 if (config.text_read_only)
1798 output_bfd->flags |= WP_TEXT;
1799 else
1800 output_bfd->flags &= ~WP_TEXT;
1801 if (link_info.traditional_format)
1802 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1803 else
1804 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1805 break;
1807 case lang_target_statement_enum:
1808 current_target = statement->target_statement.target;
1809 break;
1810 default:
1811 break;
1815 /* Open all the input files. */
1817 static void
1818 open_input_bfds (s, force)
1819 lang_statement_union_type *s;
1820 boolean force;
1822 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1824 switch (s->header.type)
1826 case lang_constructors_statement_enum:
1827 open_input_bfds (constructor_list.head, force);
1828 break;
1829 case lang_output_section_statement_enum:
1830 open_input_bfds (s->output_section_statement.children.head, force);
1831 break;
1832 case lang_wild_statement_enum:
1833 /* Maybe we should load the file's symbols */
1834 if (s->wild_statement.filename
1835 && ! wildcardp (s->wild_statement.filename))
1836 (void) lookup_name (s->wild_statement.filename);
1837 open_input_bfds (s->wild_statement.children.head, force);
1838 break;
1839 case lang_group_statement_enum:
1841 struct bfd_link_hash_entry *undefs;
1843 /* We must continually search the entries in the group
1844 until no new symbols are added to the list of undefined
1845 symbols. */
1849 undefs = link_info.hash->undefs_tail;
1850 open_input_bfds (s->group_statement.children.head, true);
1852 while (undefs != link_info.hash->undefs_tail);
1854 break;
1855 case lang_target_statement_enum:
1856 current_target = s->target_statement.target;
1857 break;
1858 case lang_input_statement_enum:
1859 if (s->input_statement.real)
1861 lang_statement_list_type add;
1863 s->input_statement.target = current_target;
1865 /* If we are being called from within a group, and this
1866 is an archive which has already been searched, then
1867 force it to be researched unless the whole archive
1868 has been loaded already. */
1869 if (force
1870 && !s->input_statement.whole_archive
1871 && s->input_statement.loaded
1872 && bfd_check_format (s->input_statement.the_bfd,
1873 bfd_archive))
1874 s->input_statement.loaded = false;
1876 lang_list_init (&add);
1878 load_symbols (&s->input_statement, &add);
1880 if (add.head != NULL)
1882 *add.tail = s->next;
1883 s->next = add.head;
1886 break;
1887 default:
1888 break;
1893 /* If there are [COMMONS] statements, put a wild one into the bss section */
1895 static void
1896 lang_reasonable_defaults ()
1898 #if 0
1899 lang_output_section_statement_lookup (".text");
1900 lang_output_section_statement_lookup (".data");
1902 default_common_section =
1903 lang_output_section_statement_lookup (".bss");
1906 if (placed_commons == false)
1908 lang_wild_statement_type *new =
1909 new_stat (lang_wild_statement,
1910 &default_common_section->children);
1912 new->section_name = "COMMON";
1913 new->filename = (char *) NULL;
1914 lang_list_init (&new->children);
1916 #endif
1921 Add the supplied name to the symbol table as an undefined reference.
1922 Remove items from the chain as we open input bfds
1924 typedef struct ldlang_undef_chain_list
1926 struct ldlang_undef_chain_list *next;
1927 char *name;
1928 } ldlang_undef_chain_list_type;
1930 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1932 void
1933 ldlang_add_undef (name)
1934 CONST char *CONST name;
1936 ldlang_undef_chain_list_type *new =
1937 ((ldlang_undef_chain_list_type *)
1938 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1940 new->next = ldlang_undef_chain_list_head;
1941 ldlang_undef_chain_list_head = new;
1943 new->name = buystring (name);
1946 /* Run through the list of undefineds created above and place them
1947 into the linker hash table as undefined symbols belonging to the
1948 script file.
1950 static void
1951 lang_place_undefineds ()
1953 ldlang_undef_chain_list_type *ptr;
1955 for (ptr = ldlang_undef_chain_list_head;
1956 ptr != (ldlang_undef_chain_list_type *) NULL;
1957 ptr = ptr->next)
1959 struct bfd_link_hash_entry *h;
1961 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1962 if (h == (struct bfd_link_hash_entry *) NULL)
1963 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1964 if (h->type == bfd_link_hash_new)
1966 h->type = bfd_link_hash_undefined;
1967 h->u.undef.abfd = NULL;
1968 bfd_link_add_undef (link_info.hash, h);
1973 /* Open input files and attatch to output sections */
1974 static void
1975 map_input_to_output_sections (s, target, output_section_statement)
1976 lang_statement_union_type * s;
1977 CONST char *target;
1978 lang_output_section_statement_type * output_section_statement;
1980 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1982 switch (s->header.type)
1986 case lang_wild_statement_enum:
1987 wild (&s->wild_statement, s->wild_statement.section_name,
1988 s->wild_statement.filename, target,
1989 output_section_statement);
1991 break;
1992 case lang_constructors_statement_enum:
1993 map_input_to_output_sections (constructor_list.head,
1994 target,
1995 output_section_statement);
1996 break;
1997 case lang_output_section_statement_enum:
1998 map_input_to_output_sections (s->output_section_statement.children.head,
1999 target,
2000 &s->output_section_statement);
2001 break;
2002 case lang_output_statement_enum:
2003 break;
2004 case lang_target_statement_enum:
2005 target = s->target_statement.target;
2006 break;
2007 case lang_group_statement_enum:
2008 map_input_to_output_sections (s->group_statement.children.head,
2009 target,
2010 output_section_statement);
2011 break;
2012 case lang_fill_statement_enum:
2013 case lang_input_section_enum:
2014 case lang_object_symbols_statement_enum:
2015 case lang_data_statement_enum:
2016 case lang_reloc_statement_enum:
2017 case lang_padding_statement_enum:
2018 case lang_input_statement_enum:
2019 if (output_section_statement != NULL
2020 && output_section_statement->bfd_section == NULL)
2021 init_os (output_section_statement);
2022 break;
2023 case lang_assignment_statement_enum:
2024 if (output_section_statement != NULL
2025 && output_section_statement->bfd_section == NULL)
2026 init_os (output_section_statement);
2028 /* Make sure that any sections mentioned in the assignment
2029 are initialized. */
2030 exp_init_os (s->assignment_statement.exp);
2031 break;
2032 case lang_afile_asection_pair_statement_enum:
2033 FAIL ();
2034 break;
2035 case lang_address_statement_enum:
2036 /* Mark the specified section with the supplied address */
2038 lang_output_section_statement_type *os =
2039 lang_output_section_statement_lookup
2040 (s->address_statement.section_name);
2042 if (os->bfd_section == NULL)
2043 init_os (os);
2044 os->addr_tree = s->address_statement.address;
2046 break;
2051 static void
2052 print_output_section_statement (output_section_statement)
2053 lang_output_section_statement_type * output_section_statement;
2055 asection *section = output_section_statement->bfd_section;
2056 int len;
2058 if (output_section_statement != abs_output_section)
2060 minfo ("\n%s", output_section_statement->name);
2062 if (section != NULL)
2064 print_dot = section->vma;
2066 len = strlen (output_section_statement->name);
2067 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2069 print_nl ();
2070 len = 0;
2072 while (len < SECTION_NAME_MAP_LENGTH)
2074 print_space ();
2075 ++len;
2078 minfo ("0x%V %W", section->vma, section->_raw_size);
2080 if (output_section_statement->load_base != NULL)
2082 bfd_vma addr;
2084 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2085 "load base", lang_final_phase_enum);
2086 minfo (_(" load address 0x%V"), addr);
2090 print_nl ();
2093 print_statement_list (output_section_statement->children.head,
2094 output_section_statement);
2097 static void
2098 print_assignment (assignment, output_section)
2099 lang_assignment_statement_type * assignment;
2100 lang_output_section_statement_type * output_section;
2102 int i;
2103 etree_value_type result;
2105 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2106 print_space ();
2108 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2109 lang_final_phase_enum, print_dot, &print_dot);
2110 if (result.valid_p)
2111 minfo ("0x%V", result.value + result.section->bfd_section->vma);
2112 else
2114 minfo ("*undef* ");
2115 #ifdef BFD64
2116 minfo (" ");
2117 #endif
2120 minfo (" ");
2122 exp_print_tree (assignment->exp);
2124 print_nl ();
2127 static void
2128 print_input_statement (statm)
2129 lang_input_statement_type * statm;
2131 if (statm->filename != (char *) NULL)
2133 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2137 /* Print all symbols defined in a particular section. This is called
2138 via bfd_link_hash_traverse. */
2140 static boolean
2141 print_one_symbol (hash_entry, ptr)
2142 struct bfd_link_hash_entry *hash_entry;
2143 PTR ptr;
2145 asection *sec = (asection *) ptr;
2147 if ((hash_entry->type == bfd_link_hash_defined
2148 || hash_entry->type == bfd_link_hash_defweak)
2149 && sec == hash_entry->u.def.section)
2151 int i;
2153 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2154 print_space ();
2155 minfo ("0x%V ",
2156 (hash_entry->u.def.value
2157 + hash_entry->u.def.section->output_offset
2158 + hash_entry->u.def.section->output_section->vma));
2160 minfo (" %T\n", hash_entry->root.string);
2163 return true;
2166 /* Print information about an input section to the map file. */
2168 static void
2169 print_input_section (in)
2170 lang_input_section_type * in;
2172 asection *i = in->section;
2173 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2174 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2175 ldfile_output_machine);
2176 if (size != 0)
2178 print_space ();
2180 minfo ("%s", i->name);
2182 if (i->output_section != NULL)
2184 int len;
2186 len = 1 + strlen (i->name);
2187 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2189 print_nl ();
2190 len = 0;
2192 while (len < SECTION_NAME_MAP_LENGTH)
2194 print_space ();
2195 ++len;
2198 minfo ("0x%V %W %B\n",
2199 i->output_section->vma + i->output_offset, size / opb,
2200 i->owner);
2202 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2204 len = SECTION_NAME_MAP_LENGTH + 3;
2205 #ifdef BFD64
2206 len += 16;
2207 #else
2208 len += 8;
2209 #endif
2210 while (len > 0)
2212 print_space ();
2213 --len;
2216 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2219 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
2221 print_dot = i->output_section->vma + i->output_offset + size / opb;
2226 static void
2227 print_fill_statement (fill)
2228 lang_fill_statement_type * fill;
2230 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
2233 static void
2234 print_data_statement (data)
2235 lang_data_statement_type * data;
2237 int i;
2238 bfd_vma addr;
2239 bfd_size_type size;
2240 const char *name;
2241 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2242 ldfile_output_machine);
2244 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2245 print_space ();
2247 addr = data->output_vma;
2248 if (data->output_section != NULL)
2249 addr += data->output_section->vma;
2251 switch (data->type)
2253 default:
2254 abort ();
2255 case BYTE:
2256 size = BYTE_SIZE;
2257 name = "BYTE";
2258 break;
2259 case SHORT:
2260 size = SHORT_SIZE;
2261 name = "SHORT";
2262 break;
2263 case LONG:
2264 size = LONG_SIZE;
2265 name = "LONG";
2266 break;
2267 case QUAD:
2268 size = QUAD_SIZE;
2269 name = "QUAD";
2270 break;
2271 case SQUAD:
2272 size = QUAD_SIZE;
2273 name = "SQUAD";
2274 break;
2277 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2279 if (data->exp->type.node_class != etree_value)
2281 print_space ();
2282 exp_print_tree (data->exp);
2285 print_nl ();
2287 print_dot = addr + size / opb;
2291 /* Print an address statement. These are generated by options like
2292 -Ttext. */
2294 static void
2295 print_address_statement (address)
2296 lang_address_statement_type *address;
2298 minfo (_("Address of section %s set to "), address->section_name);
2299 exp_print_tree (address->address);
2300 print_nl ();
2303 /* Print a reloc statement. */
2305 static void
2306 print_reloc_statement (reloc)
2307 lang_reloc_statement_type *reloc;
2309 int i;
2310 bfd_vma addr;
2311 bfd_size_type size;
2312 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2313 ldfile_output_machine);
2315 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2316 print_space ();
2318 addr = reloc->output_vma;
2319 if (reloc->output_section != NULL)
2320 addr += reloc->output_section->vma;
2322 size = bfd_get_reloc_size (reloc->howto);
2324 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2326 if (reloc->name != NULL)
2327 minfo ("%s+", reloc->name);
2328 else
2329 minfo ("%s+", reloc->section->name);
2331 exp_print_tree (reloc->addend_exp);
2333 print_nl ();
2335 print_dot = addr + size / opb;
2338 static void
2339 print_padding_statement (s)
2340 lang_padding_statement_type *s;
2342 int len;
2343 bfd_vma addr;
2344 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2345 ldfile_output_machine);
2347 minfo (" *fill*");
2349 len = sizeof " *fill*" - 1;
2350 while (len < SECTION_NAME_MAP_LENGTH)
2352 print_space ();
2353 ++len;
2356 addr = s->output_offset;
2357 if (s->output_section != NULL)
2358 addr += s->output_section->vma;
2359 minfo ("0x%V %W", addr, s->size);
2361 if (s->fill != 0)
2362 minfo (" %u", s->fill);
2364 print_nl ();
2366 print_dot = addr + s->size / opb;
2369 static void
2370 print_wild_statement (w, os)
2371 lang_wild_statement_type * w;
2372 lang_output_section_statement_type * os;
2374 print_space ();
2376 if (w->filenames_sorted)
2377 minfo ("SORT(");
2378 if (w->exclude_filename_list != NULL)
2380 name_list *tmp;
2381 minfo ("EXCLUDE_FILE ( %s", w->exclude_filename_list->name);
2382 for (tmp=w->exclude_filename_list->next; tmp; tmp = tmp->next)
2383 minfo (", %s", tmp->name);
2384 minfo (")");
2386 if (w->filename != NULL)
2387 minfo ("%s", w->filename);
2388 else
2389 minfo ("*");
2390 if (w->filenames_sorted)
2391 minfo (")");
2393 minfo ("(");
2394 if (w->sections_sorted)
2395 minfo ("SORT(");
2396 if (w->section_name != NULL)
2397 minfo ("%s", w->section_name);
2398 else
2399 minfo ("*");
2400 if (w->sections_sorted)
2401 minfo (")");
2402 minfo (")");
2404 print_nl ();
2406 print_statement_list (w->children.head, os);
2409 /* Print a group statement. */
2411 static void
2412 print_group (s, os)
2413 lang_group_statement_type *s;
2414 lang_output_section_statement_type *os;
2416 fprintf (config.map_file, "START GROUP\n");
2417 print_statement_list (s->children.head, os);
2418 fprintf (config.map_file, "END GROUP\n");
2421 /* Print the list of statements in S.
2422 This can be called for any statement type. */
2424 static void
2425 print_statement_list (s, os)
2426 lang_statement_union_type *s;
2427 lang_output_section_statement_type *os;
2429 while (s != NULL)
2431 print_statement (s, os);
2432 s = s->next;
2436 /* Print the first statement in statement list S.
2437 This can be called for any statement type. */
2439 static void
2440 print_statement (s, os)
2441 lang_statement_union_type *s;
2442 lang_output_section_statement_type *os;
2444 switch (s->header.type)
2446 default:
2447 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2448 FAIL ();
2449 break;
2450 case lang_constructors_statement_enum:
2451 if (constructor_list.head != NULL)
2453 if (constructors_sorted)
2454 minfo (" SORT (CONSTRUCTORS)\n");
2455 else
2456 minfo (" CONSTRUCTORS\n");
2457 print_statement_list (constructor_list.head, os);
2459 break;
2460 case lang_wild_statement_enum:
2461 print_wild_statement (&s->wild_statement, os);
2462 break;
2463 case lang_address_statement_enum:
2464 print_address_statement (&s->address_statement);
2465 break;
2466 case lang_object_symbols_statement_enum:
2467 minfo (" CREATE_OBJECT_SYMBOLS\n");
2468 break;
2469 case lang_fill_statement_enum:
2470 print_fill_statement (&s->fill_statement);
2471 break;
2472 case lang_data_statement_enum:
2473 print_data_statement (&s->data_statement);
2474 break;
2475 case lang_reloc_statement_enum:
2476 print_reloc_statement (&s->reloc_statement);
2477 break;
2478 case lang_input_section_enum:
2479 print_input_section (&s->input_section);
2480 break;
2481 case lang_padding_statement_enum:
2482 print_padding_statement (&s->padding_statement);
2483 break;
2484 case lang_output_section_statement_enum:
2485 print_output_section_statement (&s->output_section_statement);
2486 break;
2487 case lang_assignment_statement_enum:
2488 print_assignment (&s->assignment_statement, os);
2489 break;
2490 case lang_target_statement_enum:
2491 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2492 break;
2493 case lang_output_statement_enum:
2494 minfo ("OUTPUT(%s", s->output_statement.name);
2495 if (output_target != NULL)
2496 minfo (" %s", output_target);
2497 minfo (")\n");
2498 break;
2499 case lang_input_statement_enum:
2500 print_input_statement (&s->input_statement);
2501 break;
2502 case lang_group_statement_enum:
2503 print_group (&s->group_statement, os);
2504 break;
2505 case lang_afile_asection_pair_statement_enum:
2506 FAIL ();
2507 break;
2511 static void
2512 print_statements ()
2514 print_statement_list (statement_list.head, abs_output_section);
2517 /* Print the first N statements in statement list S to STDERR.
2518 If N == 0, nothing is printed.
2519 If N < 0, the entire list is printed.
2520 Intended to be called from GDB. */
2522 void
2523 dprint_statement (s, n)
2524 lang_statement_union_type * s;
2525 int n;
2527 FILE *map_save = config.map_file;
2529 config.map_file = stderr;
2531 if (n < 0)
2532 print_statement_list (s, abs_output_section);
2533 else
2535 while (s && --n >= 0)
2537 print_statement (s, abs_output_section);
2538 s = s->next;
2542 config.map_file = map_save;
2545 static bfd_vma
2546 insert_pad (this_ptr, fill, power, output_section_statement, dot)
2547 lang_statement_union_type ** this_ptr;
2548 fill_type fill;
2549 unsigned int power;
2550 asection * output_section_statement;
2551 bfd_vma dot;
2553 /* Align this section first to the
2554 input sections requirement, then
2555 to the output section's requirement.
2556 If this alignment is > than any seen before,
2557 then record it too. Perform the alignment by
2558 inserting a magic 'padding' statement.
2561 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2562 ldfile_output_machine);
2563 unsigned int alignment_needed = align_power (dot, power) - dot;
2565 if (alignment_needed != 0)
2567 lang_statement_union_type *new =
2568 ((lang_statement_union_type *)
2569 stat_alloc (sizeof (lang_padding_statement_type)));
2571 /* Link into existing chain */
2572 new->header.next = *this_ptr;
2573 *this_ptr = new;
2574 new->header.type = lang_padding_statement_enum;
2575 new->padding_statement.output_section = output_section_statement;
2576 new->padding_statement.output_offset =
2577 dot - output_section_statement->vma;
2578 new->padding_statement.fill = fill;
2579 new->padding_statement.size = alignment_needed * opb;
2583 /* Remember the most restrictive alignment */
2584 if (power > output_section_statement->alignment_power)
2586 output_section_statement->alignment_power = power;
2588 output_section_statement->_raw_size += alignment_needed * opb;
2590 return dot + alignment_needed;
2593 /* Work out how much this section will move the dot point */
2594 static bfd_vma
2595 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2596 lang_statement_union_type ** this_ptr;
2597 lang_output_section_statement_type * output_section_statement;
2598 fill_type fill;
2599 bfd_vma dot;
2600 boolean relax ATTRIBUTE_UNUSED;
2602 lang_input_section_type *is = &((*this_ptr)->input_section);
2603 asection *i = is->section;
2604 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2605 ldfile_output_machine);
2607 if (is->ifile->just_syms_flag == false)
2609 if (output_section_statement->subsection_alignment != -1)
2610 i->alignment_power =
2611 output_section_statement->subsection_alignment;
2613 dot = insert_pad (this_ptr, fill, i->alignment_power,
2614 output_section_statement->bfd_section, dot);
2616 /* Remember where in the output section this input section goes */
2618 i->output_offset = dot - output_section_statement->bfd_section->vma;
2620 /* Mark how big the output section must be to contain this now
2622 if (i->_cooked_size != 0)
2623 dot += i->_cooked_size / opb;
2624 else
2625 dot += i->_raw_size / opb;
2626 output_section_statement->bfd_section->_raw_size =
2627 (dot - output_section_statement->bfd_section->vma) * opb;
2629 else
2631 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2634 return dot;
2637 #define IGNORE_SECTION(bfd, s) \
2638 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) != (SEC_ALLOC | SEC_LOAD)) \
2639 || bfd_section_size (bfd, s) == 0)
2641 /* Check to see if any allocated sections overlap with other allocated
2642 sections. This can happen when the linker script specifically specifies
2643 the output section addresses of the two sections. */
2644 static void
2645 lang_check_section_addresses ()
2647 asection * s;
2648 unsigned opb = bfd_octets_per_byte (output_bfd);
2650 /* Scan all sections in the output list. */
2651 for (s = output_bfd->sections; s != NULL; s = s->next)
2653 asection * os;
2655 /* Ignore sections which are not loaded or which have no contents. */
2656 if (IGNORE_SECTION (output_bfd, s))
2657 continue;
2659 /* Once we reach section 's' stop our seach. This prevents two
2660 warning messages from being produced, one for 'section A overlaps
2661 section B' and one for 'section B overlaps section A'. */
2662 for (os = output_bfd->sections; os != s; os = os->next)
2664 bfd_vma s_start;
2665 bfd_vma s_end;
2666 bfd_vma os_start;
2667 bfd_vma os_end;
2669 /* Only consider loadable sections with real contents. */
2670 if (IGNORE_SECTION (output_bfd, os))
2671 continue;
2673 /* We must check the sections' LMA addresses not their
2674 VMA addresses because overlay sections can have
2675 overlapping VMAs but they must have distinct LMAs. */
2676 s_start = bfd_section_lma (output_bfd, s);
2677 os_start = bfd_section_lma (output_bfd, os);
2678 s_end = s_start + bfd_section_size (output_bfd, s) / opb - 1;
2679 os_end = os_start + bfd_section_size (output_bfd, os) / opb - 1;
2681 /* Look for an overlap. */
2682 if ((s_end < os_start) || (s_start > os_end))
2683 continue;
2685 einfo (
2686 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2687 s->name, s_start, s_end, os->name, os_start, os_end);
2689 /* Once we have found one overlap for this section,
2690 stop looking for others. */
2691 break;
2696 /* This variable indicates whether bfd_relax_section should be called
2697 again. */
2699 static boolean relax_again;
2701 /* Make sure the new address is within the region. We explicitly permit the
2702 current address to be at the exact end of the region when the address is
2703 non-zero, in case the region is at the end of addressable memory and the
2704 calculation wraps around. */
2706 static void
2707 os_region_check (os, region, tree, base)
2708 lang_output_section_statement_type *os;
2709 struct memory_region_struct *region;
2710 etree_type *tree;
2711 bfd_vma base;
2713 if ((region->current < region->origin
2714 || (region->current - region->origin > region->length))
2715 && ((region->current != region->origin + region->length)
2716 || base == 0))
2718 if (tree != (etree_type *) NULL)
2720 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2721 region->current,
2722 os->bfd_section->owner,
2723 os->bfd_section->name,
2724 region->name);
2726 else
2728 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2729 region->name,
2730 os->bfd_section->owner,
2731 os->bfd_section->name);
2733 /* Reset the region pointer. */
2734 region->current = region->origin;
2738 /* Set the sizes for all the output sections. */
2740 bfd_vma
2741 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2742 lang_statement_union_type * s;
2743 lang_output_section_statement_type * output_section_statement;
2744 lang_statement_union_type ** prev;
2745 fill_type fill;
2746 bfd_vma dot;
2747 boolean relax;
2749 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2750 ldfile_output_machine);
2752 /* Size up the sections from their constituent parts. */
2753 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2755 switch (s->header.type)
2757 case lang_output_section_statement_enum:
2759 bfd_vma after;
2760 lang_output_section_statement_type *os = &s->output_section_statement;
2762 if (os->bfd_section == NULL)
2763 /* This section was never actually created. */
2764 break;
2766 /* If this is a COFF shared library section, use the size and
2767 address from the input section. FIXME: This is COFF
2768 specific; it would be cleaner if there were some other way
2769 to do this, but nothing simple comes to mind. */
2770 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2772 asection * input;
2774 if (os->children.head == NULL
2775 || os->children.head->next != NULL
2776 || os->children.head->header.type != lang_input_section_enum)
2777 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2778 os->name);
2780 input = os->children.head->input_section.section;
2781 bfd_set_section_vma (os->bfd_section->owner,
2782 os->bfd_section,
2783 bfd_section_vma (input->owner, input));
2784 os->bfd_section->_raw_size = input->_raw_size;
2785 break;
2788 if (bfd_is_abs_section (os->bfd_section))
2790 /* No matter what happens, an abs section starts at zero. */
2791 ASSERT (os->bfd_section->vma == 0);
2793 else
2795 if (os->addr_tree == (etree_type *) NULL)
2797 /* No address specified for this section, get one
2798 from the region specification. */
2799 if (os->region == (lang_memory_region_type *) NULL
2800 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2801 & (SEC_ALLOC | SEC_LOAD)) != 0)
2802 && os->region->name[0] == '*'
2803 && strcmp (os->region->name, "*default*") == 0))
2805 os->region = lang_memory_default (os->bfd_section);
2808 /* If a loadable section is using the default memory
2809 region, and some non default memory regions were
2810 defined, issue a warning. */
2811 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2812 & (SEC_ALLOC | SEC_LOAD)) != 0
2813 && ! link_info.relocateable
2814 && strcmp (os->region->name, "*default*") == 0
2815 && lang_memory_region_list != NULL
2816 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2817 || lang_memory_region_list->next != NULL))
2818 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2819 bfd_get_section_name (output_bfd, os->bfd_section));
2821 dot = os->region->current;
2823 if (os->section_alignment == -1)
2825 bfd_vma olddot;
2827 olddot = dot;
2828 dot = align_power (dot, os->bfd_section->alignment_power);
2830 if (dot != olddot && config.warn_section_align)
2831 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2832 os->name, (unsigned int) (dot - olddot));
2835 else
2837 etree_value_type r;
2839 r = exp_fold_tree (os->addr_tree,
2840 abs_output_section,
2841 lang_allocating_phase_enum,
2842 dot, &dot);
2843 if (r.valid_p == false)
2845 einfo (_("%F%S: non constant address expression for section %s\n"),
2846 os->name);
2848 dot = r.value + r.section->bfd_section->vma;
2851 /* The section starts here.
2852 First, align to what the section needs. */
2854 if (os->section_alignment != -1)
2855 dot = align_power (dot, os->section_alignment);
2857 bfd_set_section_vma (0, os->bfd_section, dot);
2859 os->bfd_section->output_offset = 0;
2862 (void) lang_size_sections (os->children.head, os, &os->children.head,
2863 os->fill, dot, relax);
2865 /* put the section within the requested block size, or align at
2866 the block boundary */
2867 after = ALIGN_N (os->bfd_section->vma
2868 + os->bfd_section->_raw_size / opb,
2869 /* The coercion here is important, see ld.h. */
2870 (bfd_vma) os->block_value);
2872 if (bfd_is_abs_section (os->bfd_section))
2873 ASSERT (after == os->bfd_section->vma);
2874 else
2875 os->bfd_section->_raw_size =
2876 (after - os->bfd_section->vma) * opb;
2877 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
2878 os->processed = true;
2880 /* Update dot in the region ?
2881 We only do this if the section is going to be allocated,
2882 since unallocated sections do not contribute to the region's
2883 overall size in memory.
2885 If the SEC_NEVER_LOAD bit is not set, it will affect the
2886 addresses of sections after it. We have to update
2887 dot. */
2888 if (os->region != (lang_memory_region_type *) NULL
2889 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
2890 & SEC_NEVER_LOAD) == 0
2891 || (bfd_get_section_flags (output_bfd, os->bfd_section)
2892 & (SEC_ALLOC | SEC_LOAD))))
2894 os->region->current = dot;
2896 /* Make sure the new address is within the region. */
2897 os_region_check (os, os->region, os->addr_tree,
2898 os->bfd_section->vma);
2900 /* if there's no load address specified, use the run region as
2901 the load region */
2902 if (os->lma_region == NULL && os->load_base == NULL)
2903 os->lma_region = os->region;
2905 if (os->lma_region != NULL)
2907 if (os->load_base != NULL)
2909 einfo (_("%X%P: use an absolute load address or a load memory region, not both\n"));
2911 else
2913 /* don't allocate twice */
2914 if (os->lma_region != os->region)
2916 /* set load_base, which will be handled later */
2917 os->load_base = exp_intop (os->lma_region->current);
2918 os->lma_region->current +=
2919 os->bfd_section->_raw_size / opb;
2920 os_region_check (os, os->lma_region, NULL,
2921 os->bfd_section->lma);
2927 break;
2929 case lang_constructors_statement_enum:
2930 dot = lang_size_sections (constructor_list.head,
2931 output_section_statement,
2932 &s->wild_statement.children.head,
2933 fill,
2934 dot, relax);
2935 break;
2937 case lang_data_statement_enum:
2939 unsigned int size = 0;
2941 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2942 s->data_statement.output_section =
2943 output_section_statement->bfd_section;
2945 switch (s->data_statement.type)
2947 default:
2948 abort();
2949 case QUAD:
2950 case SQUAD:
2951 size = QUAD_SIZE;
2952 break;
2953 case LONG:
2954 size = LONG_SIZE;
2955 break;
2956 case SHORT:
2957 size = SHORT_SIZE;
2958 break;
2959 case BYTE:
2960 size = BYTE_SIZE;
2961 break;
2963 if (size < opb)
2964 size = opb;
2965 dot += size / opb;
2966 output_section_statement->bfd_section->_raw_size += size;
2967 /* The output section gets contents, and then we inspect for
2968 any flags set in the input script which override any ALLOC. */
2969 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
2970 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2971 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2974 break;
2976 case lang_reloc_statement_enum:
2978 int size;
2980 s->reloc_statement.output_vma =
2981 dot - output_section_statement->bfd_section->vma;
2982 s->reloc_statement.output_section =
2983 output_section_statement->bfd_section;
2984 size = bfd_get_reloc_size (s->reloc_statement.howto);
2985 dot += size / opb;
2986 output_section_statement->bfd_section->_raw_size += size;
2988 break;
2990 case lang_wild_statement_enum:
2992 dot = lang_size_sections (s->wild_statement.children.head,
2993 output_section_statement,
2994 &s->wild_statement.children.head,
2996 fill, dot, relax);
2998 break;
3000 case lang_object_symbols_statement_enum:
3001 link_info.create_object_symbols_section =
3002 output_section_statement->bfd_section;
3003 break;
3004 case lang_output_statement_enum:
3005 case lang_target_statement_enum:
3006 break;
3007 case lang_input_section_enum:
3009 asection *i;
3011 i = (*prev)->input_section.section;
3012 if (! relax)
3014 if (i->_cooked_size == 0)
3015 i->_cooked_size = i->_raw_size;
3017 else
3019 boolean again;
3021 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3022 einfo (_("%P%F: can't relax section: %E\n"));
3023 if (again)
3024 relax_again = true;
3026 dot = size_input_section (prev,
3027 output_section_statement,
3028 output_section_statement->fill,
3029 dot, relax);
3031 break;
3032 case lang_input_statement_enum:
3033 break;
3034 case lang_fill_statement_enum:
3035 s->fill_statement.output_section = output_section_statement->bfd_section;
3037 fill = s->fill_statement.fill;
3038 break;
3039 case lang_assignment_statement_enum:
3041 bfd_vma newdot = dot;
3043 exp_fold_tree (s->assignment_statement.exp,
3044 output_section_statement,
3045 lang_allocating_phase_enum,
3046 dot,
3047 &newdot);
3049 if (newdot != dot)
3051 /* The assignment changed dot. Insert a pad. */
3052 if (output_section_statement == abs_output_section)
3054 /* If we don't have an output section, then just adjust
3055 the default memory address. */
3056 lang_memory_region_lookup ("*default*")->current = newdot;
3058 else if (!relax)
3060 lang_statement_union_type *new =
3061 ((lang_statement_union_type *)
3062 stat_alloc (sizeof (lang_padding_statement_type)));
3064 /* Link into existing chain. */
3065 new->header.next = *prev;
3066 *prev = new;
3067 new->header.type = lang_padding_statement_enum;
3068 new->padding_statement.output_section =
3069 output_section_statement->bfd_section;
3070 new->padding_statement.output_offset =
3071 dot - output_section_statement->bfd_section->vma;
3072 new->padding_statement.fill = fill;
3073 new->padding_statement.size = (newdot - dot) * opb;
3074 output_section_statement->bfd_section->_raw_size +=
3075 new->padding_statement.size;
3078 dot = newdot;
3081 break;
3083 case lang_padding_statement_enum:
3084 /* If we are relaxing, and this is not the first pass, some
3085 padding statements may have been inserted during previous
3086 passes. We may have to move the padding statement to a new
3087 location if dot has a different value at this point in this
3088 pass than it did at this point in the previous pass. */
3089 s->padding_statement.output_offset =
3090 dot - output_section_statement->bfd_section->vma;
3091 dot += s->padding_statement.size / opb;
3092 output_section_statement->bfd_section->_raw_size +=
3093 s->padding_statement.size;
3094 break;
3096 case lang_group_statement_enum:
3097 dot = lang_size_sections (s->group_statement.children.head,
3098 output_section_statement,
3099 &s->group_statement.children.head,
3100 fill, dot, relax);
3101 break;
3103 default:
3104 FAIL ();
3105 break;
3107 /* This can only get here when relaxing is turned on. */
3109 case lang_address_statement_enum:
3110 break;
3112 prev = &s->header.next;
3114 return dot;
3117 bfd_vma
3118 lang_do_assignments (s, output_section_statement, fill, dot)
3119 lang_statement_union_type * s;
3120 lang_output_section_statement_type * output_section_statement;
3121 fill_type fill;
3122 bfd_vma dot;
3124 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3125 ldfile_output_machine);
3127 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3129 switch (s->header.type)
3131 case lang_constructors_statement_enum:
3132 dot = lang_do_assignments (constructor_list.head,
3133 output_section_statement,
3134 fill,
3135 dot);
3136 break;
3138 case lang_output_section_statement_enum:
3140 lang_output_section_statement_type *os =
3141 &(s->output_section_statement);
3143 if (os->bfd_section != NULL)
3145 dot = os->bfd_section->vma;
3146 (void) lang_do_assignments (os->children.head, os,
3147 os->fill, dot);
3148 dot = os->bfd_section->vma + os->bfd_section->_raw_size / opb;
3151 if (os->load_base)
3153 /* If nothing has been placed into the output section then
3154 it won't have a bfd_section. */
3155 if (os->bfd_section)
3157 os->bfd_section->lma
3158 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
3162 break;
3163 case lang_wild_statement_enum:
3165 dot = lang_do_assignments (s->wild_statement.children.head,
3166 output_section_statement,
3167 fill, dot);
3169 break;
3171 case lang_object_symbols_statement_enum:
3172 case lang_output_statement_enum:
3173 case lang_target_statement_enum:
3174 #if 0
3175 case lang_common_statement_enum:
3176 #endif
3177 break;
3178 case lang_data_statement_enum:
3180 etree_value_type value;
3182 value = exp_fold_tree (s->data_statement.exp,
3183 abs_output_section,
3184 lang_final_phase_enum, dot, &dot);
3185 s->data_statement.value = value.value;
3186 if (value.valid_p == false)
3187 einfo (_("%F%P: invalid data statement\n"));
3190 unsigned int size;
3191 switch (s->data_statement.type)
3193 default:
3194 abort();
3195 case QUAD:
3196 case SQUAD:
3197 size = QUAD_SIZE;
3198 break;
3199 case LONG:
3200 size = LONG_SIZE;
3201 break;
3202 case SHORT:
3203 size = SHORT_SIZE;
3204 break;
3205 case BYTE:
3206 size = BYTE_SIZE;
3207 break;
3209 if (size < opb)
3210 size = opb;
3211 dot += size / opb;
3213 break;
3215 case lang_reloc_statement_enum:
3217 etree_value_type value;
3219 value = exp_fold_tree (s->reloc_statement.addend_exp,
3220 abs_output_section,
3221 lang_final_phase_enum, dot, &dot);
3222 s->reloc_statement.addend_value = value.value;
3223 if (value.valid_p == false)
3224 einfo (_("%F%P: invalid reloc statement\n"));
3226 dot += bfd_get_reloc_size (s->reloc_statement.howto) / opb;
3227 break;
3229 case lang_input_section_enum:
3231 asection *in = s->input_section.section;
3233 if (in->_cooked_size != 0)
3234 dot += in->_cooked_size / opb;
3235 else
3236 dot += in->_raw_size / opb;
3238 break;
3240 case lang_input_statement_enum:
3241 break;
3242 case lang_fill_statement_enum:
3243 fill = s->fill_statement.fill;
3244 break;
3245 case lang_assignment_statement_enum:
3247 exp_fold_tree (s->assignment_statement.exp,
3248 output_section_statement,
3249 lang_final_phase_enum,
3250 dot,
3251 &dot);
3254 break;
3255 case lang_padding_statement_enum:
3256 dot += s->padding_statement.size / opb;
3257 break;
3259 case lang_group_statement_enum:
3260 dot = lang_do_assignments (s->group_statement.children.head,
3261 output_section_statement,
3262 fill, dot);
3264 break;
3266 default:
3267 FAIL ();
3268 break;
3269 case lang_address_statement_enum:
3270 break;
3274 return dot;
3277 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3278 operator .startof. (section_name), it produces an undefined symbol
3279 .startof.section_name. Similarly, when it sees
3280 .sizeof. (section_name), it produces an undefined symbol
3281 .sizeof.section_name. For all the output sections, we look for
3282 such symbols, and set them to the correct value. */
3284 static void
3285 lang_set_startof ()
3287 asection *s;
3289 if (link_info.relocateable)
3290 return;
3292 for (s = output_bfd->sections; s != NULL; s = s->next)
3294 const char *secname;
3295 char *buf;
3296 struct bfd_link_hash_entry *h;
3298 secname = bfd_get_section_name (output_bfd, s);
3299 buf = xmalloc (10 + strlen (secname));
3301 sprintf (buf, ".startof.%s", secname);
3302 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
3303 if (h != NULL && h->type == bfd_link_hash_undefined)
3305 h->type = bfd_link_hash_defined;
3306 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3307 h->u.def.section = bfd_abs_section_ptr;
3310 sprintf (buf, ".sizeof.%s", secname);
3311 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
3312 if (h != NULL && h->type == bfd_link_hash_undefined)
3314 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3315 ldfile_output_machine);
3316 h->type = bfd_link_hash_defined;
3317 if (s->_cooked_size != 0)
3318 h->u.def.value = s->_cooked_size / opb;
3319 else
3320 h->u.def.value = s->_raw_size / opb;
3321 h->u.def.section = bfd_abs_section_ptr;
3324 free (buf);
3328 static void
3329 lang_finish ()
3331 struct bfd_link_hash_entry *h;
3332 boolean warn;
3334 if (link_info.relocateable || link_info.shared)
3335 warn = false;
3336 else
3337 warn = true;
3339 if (entry_symbol == (char *) NULL)
3341 /* No entry has been specified. Look for start, but don't warn
3342 if we don't find it. */
3343 entry_symbol = "start";
3344 warn = false;
3347 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
3348 if (h != (struct bfd_link_hash_entry *) NULL
3349 && (h->type == bfd_link_hash_defined
3350 || h->type == bfd_link_hash_defweak)
3351 && h->u.def.section->output_section != NULL)
3353 bfd_vma val;
3355 val = (h->u.def.value
3356 + bfd_get_section_vma (output_bfd,
3357 h->u.def.section->output_section)
3358 + h->u.def.section->output_offset);
3359 if (! bfd_set_start_address (output_bfd, val))
3360 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
3362 else
3364 bfd_vma val;
3365 CONST char *send;
3367 /* We couldn't find the entry symbol. Try parsing it as a
3368 number. */
3369 val = bfd_scan_vma (entry_symbol, &send, 0);
3370 if (*send == '\0')
3372 if (! bfd_set_start_address (output_bfd, val))
3373 einfo (_("%P%F: can't set start address\n"));
3375 else
3377 asection *ts;
3379 /* Can't find the entry symbol, and it's not a number. Use
3380 the first address in the text section. */
3381 ts = bfd_get_section_by_name (output_bfd, ".text");
3382 if (ts != (asection *) NULL)
3384 if (warn)
3385 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3386 entry_symbol, bfd_get_section_vma (output_bfd, ts));
3387 if (! bfd_set_start_address (output_bfd,
3388 bfd_get_section_vma (output_bfd,
3389 ts)))
3390 einfo (_("%P%F: can't set start address\n"));
3392 else
3394 if (warn)
3395 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3396 entry_symbol);
3402 /* This is a small function used when we want to ignore errors from
3403 BFD. */
3405 static void
3406 #ifdef ANSI_PROTOTYPES
3407 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3408 #else
3409 ignore_bfd_errors (s)
3410 const char *s ATTRIBUTE_UNUSED;
3411 #endif
3413 /* Don't do anything. */
3416 /* Check that the architecture of all the input files is compatible
3417 with the output file. Also call the backend to let it do any
3418 other checking that is needed. */
3420 static void
3421 lang_check ()
3423 lang_statement_union_type *file;
3424 bfd *input_bfd;
3425 CONST bfd_arch_info_type *compatible;
3427 for (file = file_chain.head;
3428 file != (lang_statement_union_type *) NULL;
3429 file = file->input_statement.next)
3431 input_bfd = file->input_statement.the_bfd;
3432 compatible = bfd_arch_get_compatible (input_bfd,
3433 output_bfd);
3434 if (compatible == NULL)
3436 if (command_line.warn_mismatch)
3437 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3438 bfd_printable_name (input_bfd), input_bfd,
3439 bfd_printable_name (output_bfd));
3441 else
3443 bfd_error_handler_type pfn = NULL;
3445 /* If we aren't supposed to warn about mismatched input
3446 files, temporarily set the BFD error handler to a
3447 function which will do nothing. We still want to call
3448 bfd_merge_private_bfd_data, since it may set up
3449 information which is needed in the output file. */
3450 if (! command_line.warn_mismatch)
3451 pfn = bfd_set_error_handler (ignore_bfd_errors);
3452 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3454 if (command_line.warn_mismatch)
3455 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3456 input_bfd);
3458 if (! command_line.warn_mismatch)
3459 bfd_set_error_handler (pfn);
3464 /* Look through all the global common symbols and attach them to the
3465 correct section. The -sort-common command line switch may be used
3466 to roughly sort the entries by size. */
3468 static void
3469 lang_common ()
3471 if (link_info.relocateable
3472 && ! command_line.force_common_definition)
3473 return;
3475 if (! config.sort_common)
3476 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
3477 else
3479 int power;
3481 for (power = 4; power >= 0; power--)
3482 bfd_link_hash_traverse (link_info.hash, lang_one_common,
3483 (PTR) &power);
3487 /* Place one common symbol in the correct section. */
3489 static boolean
3490 lang_one_common (h, info)
3491 struct bfd_link_hash_entry *h;
3492 PTR info;
3494 unsigned int power_of_two;
3495 bfd_vma size;
3496 asection *section;
3497 unsigned opb = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
3498 ldfile_output_machine);
3500 if (h->type != bfd_link_hash_common)
3501 return true;
3503 size = h->u.c.size;
3504 power_of_two = h->u.c.p->alignment_power;
3506 if (config.sort_common
3507 && power_of_two < (unsigned int) *(int *) info)
3508 return true;
3510 section = h->u.c.p->section;
3512 /* Increase the size of the section. */
3513 section->_cooked_size = ALIGN_N ((section->_cooked_size + opb - 1) / opb,
3514 (bfd_size_type) (1 << power_of_two)) * opb;
3516 /* Adjust the alignment if necessary. */
3517 if (power_of_two > section->alignment_power)
3518 section->alignment_power = power_of_two;
3520 /* Change the symbol from common to defined. */
3521 h->type = bfd_link_hash_defined;
3522 h->u.def.section = section;
3523 h->u.def.value = section->_cooked_size;
3525 /* Increase the size of the section. */
3526 section->_cooked_size += size;
3528 /* Make sure the section is allocated in memory, and make sure that
3529 it is no longer a common section. */
3530 section->flags |= SEC_ALLOC;
3531 section->flags &= ~ SEC_IS_COMMON;
3533 if (config.map_file != NULL)
3535 static boolean header_printed;
3536 int len;
3537 char *name;
3538 char buf[50];
3540 if (! header_printed)
3542 minfo (_("\nAllocating common symbols\n"));
3543 minfo (_("Common symbol size file\n\n"));
3544 header_printed = true;
3547 name = demangle (h->root.string);
3548 minfo ("%s", name);
3549 len = strlen (name);
3550 free (name);
3552 if (len >= 19)
3554 print_nl ();
3555 len = 0;
3557 while (len < 20)
3559 print_space ();
3560 ++len;
3563 minfo ("0x");
3564 if (size <= 0xffffffff)
3565 sprintf (buf, "%lx", (unsigned long) size);
3566 else
3567 sprintf_vma (buf, size);
3568 minfo ("%s", buf);
3569 len = strlen (buf);
3571 while (len < 16)
3573 print_space ();
3574 ++len;
3577 minfo ("%B\n", section->owner);
3580 return true;
3584 run through the input files and ensure that every input
3585 section has somewhere to go. If one is found without
3586 a destination then create an input request and place it
3587 into the statement tree.
3590 static void
3591 lang_place_orphans ()
3593 LANG_FOR_EACH_INPUT_STATEMENT (file)
3595 asection *s;
3597 for (s = file->the_bfd->sections;
3598 s != (asection *) NULL;
3599 s = s->next)
3601 if (s->output_section == (asection *) NULL)
3603 /* This section of the file is not attatched, root
3604 around for a sensible place for it to go */
3606 if (file->just_syms_flag)
3608 /* We are only retrieving symbol values from this
3609 file. We want the symbols to act as though the
3610 values in the file are absolute. */
3611 s->output_section = bfd_abs_section_ptr;
3612 s->output_offset = s->vma;
3614 else if (strcmp (s->name, "COMMON") == 0)
3616 /* This is a lonely common section which must have
3617 come from an archive. We attach to the section
3618 with the wildcard. */
3619 if (! link_info.relocateable
3620 || command_line.force_common_definition)
3622 if (default_common_section == NULL)
3624 #if 0
3625 /* This message happens when using the
3626 svr3.ifile linker script, so I have
3627 disabled it. */
3628 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3629 #endif
3630 default_common_section =
3631 lang_output_section_statement_lookup (".bss");
3634 wild_doit (&default_common_section->children, s,
3635 default_common_section, file);
3638 else if (ldemul_place_orphan (file, s))
3640 else
3642 lang_output_section_statement_type *os =
3643 lang_output_section_statement_lookup (s->name);
3645 wild_doit (&os->children, s, os, file);
3653 void
3654 lang_set_flags (ptr, flags, invert)
3655 lang_memory_region_type *ptr;
3656 CONST char *flags;
3657 int invert;
3659 flagword *ptr_flags;
3661 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3662 while (*flags)
3664 switch (*flags)
3666 case 'A': case 'a':
3667 *ptr_flags |= SEC_ALLOC;
3668 break;
3670 case 'R': case 'r':
3671 *ptr_flags |= SEC_READONLY;
3672 break;
3674 case 'W': case 'w':
3675 *ptr_flags |= SEC_DATA;
3676 break;
3678 case 'X': case 'x':
3679 *ptr_flags |= SEC_CODE;
3680 break;
3682 case 'L': case 'l':
3683 case 'I': case 'i':
3684 *ptr_flags |= SEC_LOAD;
3685 break;
3687 default:
3688 einfo (_("%P%F: invalid syntax in flags\n"));
3689 break;
3691 flags++;
3695 /* Call a function on each input file. This function will be called
3696 on an archive, but not on the elements. */
3698 void
3699 lang_for_each_input_file (func)
3700 void (*func) PARAMS ((lang_input_statement_type *));
3702 lang_input_statement_type *f;
3704 for (f = (lang_input_statement_type *) input_file_chain.head;
3705 f != NULL;
3706 f = (lang_input_statement_type *) f->next_real_file)
3707 func (f);
3710 /* Call a function on each file. The function will be called on all
3711 the elements of an archive which are included in the link, but will
3712 not be called on the archive file itself. */
3714 void
3715 lang_for_each_file (func)
3716 void (*func) PARAMS ((lang_input_statement_type *));
3718 LANG_FOR_EACH_INPUT_STATEMENT (f)
3720 func (f);
3724 #if 0
3726 /* Not used. */
3728 void
3729 lang_for_each_input_section (func)
3730 void (*func) PARAMS ((bfd * ab, asection * as));
3732 LANG_FOR_EACH_INPUT_STATEMENT (f)
3734 asection * s;
3736 for (s = f->the_bfd->sections;
3737 s != (asection *) NULL;
3738 s = s->next)
3740 func (f->the_bfd, s);
3745 #endif
3747 void
3748 ldlang_add_file (entry)
3749 lang_input_statement_type * entry;
3751 bfd **pp;
3753 lang_statement_append (&file_chain,
3754 (lang_statement_union_type *) entry,
3755 &entry->next);
3757 /* The BFD linker needs to have a list of all input BFDs involved in
3758 a link. */
3759 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3760 ASSERT (entry->the_bfd != output_bfd);
3761 for (pp = &link_info.input_bfds;
3762 *pp != (bfd *) NULL;
3763 pp = &(*pp)->link_next)
3765 *pp = entry->the_bfd;
3766 entry->the_bfd->usrdata = (PTR) entry;
3767 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3769 /* Look through the sections and check for any which should not be
3770 included in the link. We need to do this now, so that we can
3771 notice when the backend linker tries to report multiple
3772 definition errors for symbols which are in sections we aren't
3773 going to link. FIXME: It might be better to entirely ignore
3774 symbols which are defined in sections which are going to be
3775 discarded. This would require modifying the backend linker for
3776 each backend which might set the SEC_LINK_ONCE flag. If we do
3777 this, we should probably handle SEC_EXCLUDE in the same way. */
3779 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3782 void
3783 lang_add_output (name, from_script)
3784 CONST char *name;
3785 int from_script;
3787 /* Make -o on command line override OUTPUT in script. */
3788 if (had_output_filename == false || !from_script)
3790 output_filename = name;
3791 had_output_filename = true;
3796 static lang_output_section_statement_type *current_section;
3798 static int
3799 topower (x)
3800 int x;
3802 unsigned int i = 1;
3803 int l;
3805 if (x < 0)
3806 return -1;
3808 for (l = 0; l < 32; l++)
3810 if (i >= (unsigned int) x)
3811 return l;
3812 i <<= 1;
3815 return 0;
3818 lang_output_section_statement_type *
3819 lang_enter_output_section_statement (output_section_statement_name,
3820 address_exp, sectype, block_value,
3821 align, subalign, ebase)
3822 const char *output_section_statement_name;
3823 etree_type * address_exp;
3824 enum section_type sectype;
3825 bfd_vma block_value;
3826 etree_type *align;
3827 etree_type *subalign;
3828 etree_type *ebase;
3830 lang_output_section_statement_type *os;
3832 current_section =
3833 os =
3834 lang_output_section_statement_lookup (output_section_statement_name);
3838 /* Add this statement to tree */
3839 /* add_statement(lang_output_section_statement_enum,
3840 output_section_statement);*/
3841 /* Make next things chain into subchain of this */
3843 if (os->addr_tree ==
3844 (etree_type *) NULL)
3846 os->addr_tree =
3847 address_exp;
3849 os->sectype = sectype;
3850 if (sectype != noload_section)
3851 os->flags = SEC_NO_FLAGS;
3852 else
3853 os->flags = SEC_NEVER_LOAD;
3854 os->block_value = block_value ? block_value : 1;
3855 stat_ptr = &os->children;
3857 os->subsection_alignment = topower(
3858 exp_get_value_int(subalign, -1,
3859 "subsection alignment",
3860 0));
3861 os->section_alignment = topower(
3862 exp_get_value_int(align, -1,
3863 "section alignment", 0));
3865 os->load_base = ebase;
3866 return os;
3870 void
3871 lang_final ()
3873 lang_output_statement_type *new =
3874 new_stat (lang_output_statement, stat_ptr);
3876 new->name = output_filename;
3879 /* Reset the current counters in the regions */
3880 static void
3881 reset_memory_regions ()
3883 lang_memory_region_type *p = lang_memory_region_list;
3885 for (p = lang_memory_region_list;
3886 p != (lang_memory_region_type *) NULL;
3887 p = p->next)
3889 p->old_length = (bfd_size_type) (p->current - p->origin);
3890 p->current = p->origin;
3894 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3895 as needed. SECTION may be NULL, in which case it is a wild card. */
3897 static void
3898 gc_section_callback (ptr, section, file, data)
3899 lang_wild_statement_type *ptr;
3900 asection *section;
3901 lang_input_statement_type *file ATTRIBUTE_UNUSED;
3902 void *data ATTRIBUTE_UNUSED;
3904 /* If the wild pattern was marked KEEP, the member sections
3905 should be as well. */
3906 if (ptr->keep_sections)
3907 section->flags |= SEC_KEEP;
3910 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3911 may be NULL, indicating that it is a wildcard. */
3913 static void
3914 lang_gc_wild (s, section, file)
3915 lang_wild_statement_type *s;
3916 const char *section;
3917 const char *file;
3919 walk_wild (s, section, file, gc_section_callback, NULL);
3922 /* Iterate over sections marking them against GC. */
3924 static void
3925 lang_gc_sections_1 (s)
3926 lang_statement_union_type * s;
3928 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3930 switch (s->header.type)
3932 case lang_wild_statement_enum:
3933 lang_gc_wild (&s->wild_statement,
3934 s->wild_statement.section_name,
3935 s->wild_statement.filename);
3936 break;
3937 case lang_constructors_statement_enum:
3938 lang_gc_sections_1 (constructor_list.head);
3939 break;
3940 case lang_output_section_statement_enum:
3941 lang_gc_sections_1 (s->output_section_statement.children.head);
3942 break;
3943 case lang_group_statement_enum:
3944 lang_gc_sections_1 (s->group_statement.children.head);
3945 break;
3946 default:
3947 break;
3952 static void
3953 lang_gc_sections ()
3955 struct bfd_link_hash_entry *h;
3956 ldlang_undef_chain_list_type *ulist, fake_list_start;
3958 /* Keep all sections so marked in the link script. */
3960 lang_gc_sections_1 (statement_list.head);
3962 /* Keep all sections containing symbols undefined on the command-line.
3963 Handle the entry symbol at the same time. */
3965 if (entry_symbol != NULL)
3967 fake_list_start.next = ldlang_undef_chain_list_head;
3968 fake_list_start.name = (char *) entry_symbol;
3969 ulist = &fake_list_start;
3971 else
3972 ulist = ldlang_undef_chain_list_head;
3974 for (; ulist; ulist = ulist->next)
3976 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
3977 false, false, false);
3979 if (h != (struct bfd_link_hash_entry *) NULL
3980 && (h->type == bfd_link_hash_defined
3981 || h->type == bfd_link_hash_defweak)
3982 && ! bfd_is_abs_section (h->u.def.section))
3984 h->u.def.section->flags |= SEC_KEEP;
3988 bfd_gc_sections (output_bfd, &link_info);
3991 void
3992 lang_process ()
3994 lang_reasonable_defaults ();
3995 current_target = default_target;
3997 lang_for_each_statement (ldlang_open_output); /* Open the output file */
3999 ldemul_create_output_section_statements ();
4001 /* Add to the hash table all undefineds on the command line */
4002 lang_place_undefineds ();
4004 already_linked_table_init ();
4006 /* Create a bfd for each input file */
4007 current_target = default_target;
4008 open_input_bfds (statement_list.head, false);
4010 ldemul_after_open ();
4012 already_linked_table_free ();
4014 /* Make sure that we're not mixing architectures. We call this
4015 after all the input files have been opened, but before we do any
4016 other processing, so that any operations merge_private_bfd_data
4017 does on the output file will be known during the rest of the
4018 link. */
4019 lang_check ();
4021 /* Handle .exports instead of a version script if we're told to do so. */
4022 if (command_line.version_exports_section)
4023 lang_do_version_exports_section ();
4025 /* Build all sets based on the information gathered from the input
4026 files. */
4027 ldctor_build_sets ();
4029 /* Remove unreferenced sections if asked to. */
4030 if (command_line.gc_sections)
4031 lang_gc_sections ();
4033 /* Size up the common data */
4034 lang_common ();
4036 /* Run through the contours of the script and attach input sections
4037 to the correct output sections
4039 map_input_to_output_sections (statement_list.head, (char *) NULL,
4040 (lang_output_section_statement_type *) NULL);
4043 /* Find any sections not attached explicitly and handle them */
4044 lang_place_orphans ();
4046 ldemul_before_allocation ();
4048 /* We must record the program headers before we try to fix the
4049 section positions, since they will affect SIZEOF_HEADERS. */
4050 lang_record_phdrs ();
4052 /* Now run around and relax if we can */
4053 if (command_line.relax)
4055 /* First time round is a trial run to get the 'worst case'
4056 addresses of the objects if there was no relaxing. */
4057 lang_size_sections (statement_list.head,
4058 abs_output_section,
4059 &(statement_list.head), 0, (bfd_vma) 0, false);
4061 /* Keep relaxing until bfd_relax_section gives up. */
4064 reset_memory_regions ();
4066 relax_again = false;
4068 /* Note: pe-dll.c does something like this also. If you find
4069 you need to change this code, you probably need to change
4070 pe-dll.c also. DJ */
4072 /* Do all the assignments with our current guesses as to
4073 section sizes. */
4074 lang_do_assignments (statement_list.head,
4075 abs_output_section,
4076 (fill_type) 0, (bfd_vma) 0);
4078 /* Perform another relax pass - this time we know where the
4079 globals are, so can make better guess. */
4080 lang_size_sections (statement_list.head,
4081 abs_output_section,
4082 &(statement_list.head), 0, (bfd_vma) 0, true);
4084 while (relax_again);
4086 else
4088 /* Size up the sections. */
4089 lang_size_sections (statement_list.head,
4090 abs_output_section,
4091 &(statement_list.head), 0, (bfd_vma) 0, false);
4094 /* See if anything special should be done now we know how big
4095 everything is. */
4096 ldemul_after_allocation ();
4098 /* Fix any .startof. or .sizeof. symbols. */
4099 lang_set_startof ();
4101 /* Do all the assignments, now that we know the final restingplaces
4102 of all the symbols */
4104 lang_do_assignments (statement_list.head,
4105 abs_output_section,
4106 (fill_type) 0, (bfd_vma) 0);
4108 /* Make sure that the section addresses make sense. */
4109 if (! link_info.relocateable
4110 && command_line.check_section_addresses)
4111 lang_check_section_addresses ();
4113 /* Final stuffs */
4115 ldemul_finish ();
4116 lang_finish ();
4119 /* EXPORTED TO YACC */
4121 void
4122 lang_add_wild (section_name, sections_sorted, filename, filenames_sorted,
4123 keep_sections, exclude_filename_list)
4124 const char *const section_name;
4125 boolean sections_sorted;
4126 const char *const filename;
4127 boolean filenames_sorted;
4128 boolean keep_sections;
4129 struct name_list *exclude_filename_list;
4131 lang_wild_statement_type *new = new_stat (lang_wild_statement,
4132 stat_ptr);
4134 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
4136 placed_commons = true;
4138 if (filename != NULL && ! wildcardp (filename))
4140 lang_has_input_file = true;
4142 new->section_name = section_name;
4143 new->sections_sorted = sections_sorted;
4144 new->filename = filename;
4145 new->filenames_sorted = filenames_sorted;
4146 new->keep_sections = keep_sections;
4147 new->exclude_filename_list = exclude_filename_list;
4148 lang_list_init (&new->children);
4151 void
4152 lang_section_start (name, address)
4153 CONST char *name;
4154 etree_type * address;
4156 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
4158 ad->section_name = name;
4159 ad->address = address;
4162 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4163 because of a -e argument on the command line, or zero if this is
4164 called by ENTRY in a linker script. Command line arguments take
4165 precedence. */
4167 void
4168 lang_add_entry (name, cmdline)
4169 CONST char *name;
4170 boolean cmdline;
4172 if (entry_symbol == NULL
4173 || cmdline
4174 || ! entry_from_cmdline)
4176 entry_symbol = name;
4177 entry_from_cmdline = cmdline;
4181 void
4182 lang_add_target (name)
4183 CONST char *name;
4185 lang_target_statement_type *new = new_stat (lang_target_statement,
4186 stat_ptr);
4188 new->target = name;
4192 void
4193 lang_add_map (name)
4194 CONST char *name;
4196 while (*name)
4198 switch (*name)
4200 case 'F':
4201 map_option_f = true;
4202 break;
4204 name++;
4208 void
4209 lang_add_fill (exp)
4210 int exp;
4212 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4213 stat_ptr);
4215 new->fill = exp;
4218 void
4219 lang_add_data (type, exp)
4220 int type;
4221 union etree_union *exp;
4224 lang_data_statement_type *new = new_stat (lang_data_statement,
4225 stat_ptr);
4227 new->exp = exp;
4228 new->type = type;
4232 /* Create a new reloc statement. RELOC is the BFD relocation type to
4233 generate. HOWTO is the corresponding howto structure (we could
4234 look this up, but the caller has already done so). SECTION is the
4235 section to generate a reloc against, or NAME is the name of the
4236 symbol to generate a reloc against. Exactly one of SECTION and
4237 NAME must be NULL. ADDEND is an expression for the addend. */
4239 void
4240 lang_add_reloc (reloc, howto, section, name, addend)
4241 bfd_reloc_code_real_type reloc;
4242 reloc_howto_type *howto;
4243 asection *section;
4244 const char *name;
4245 union etree_union *addend;
4247 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4249 p->reloc = reloc;
4250 p->howto = howto;
4251 p->section = section;
4252 p->name = name;
4253 p->addend_exp = addend;
4255 p->addend_value = 0;
4256 p->output_section = NULL;
4257 p->output_vma = 0;
4260 lang_assignment_statement_type *
4261 lang_add_assignment (exp)
4262 etree_type * exp;
4264 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4265 stat_ptr);
4267 new->exp = exp;
4268 return new;
4271 void
4272 lang_add_attribute (attribute)
4273 enum statement_enum attribute;
4275 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4278 void
4279 lang_startup (name)
4280 CONST char *name;
4282 if (startup_file != (char *) NULL)
4284 einfo (_("%P%Fmultiple STARTUP files\n"));
4286 first_file->filename = name;
4287 first_file->local_sym_name = name;
4288 first_file->real = true;
4290 startup_file = name;
4293 void
4294 lang_float (maybe)
4295 boolean maybe;
4297 lang_float_flag = maybe;
4300 void
4301 lang_leave_output_section_statement (fill, memspec, phdrs, lma_memspec)
4302 bfd_vma fill;
4303 const char *memspec;
4304 struct lang_output_section_phdr_list *phdrs;
4305 const char *lma_memspec;
4307 current_section->fill = fill;
4308 current_section->region = lang_memory_region_lookup (memspec);
4309 if (strcmp (lma_memspec, "*default*") != 0)
4311 current_section->lma_region = lang_memory_region_lookup (lma_memspec);
4312 /* if no runtime region has been given, but the load region has been,
4313 use the load region */
4314 if (strcmp (memspec, "*default*") == 0)
4315 current_section->region = lang_memory_region_lookup (lma_memspec);
4317 current_section->phdrs = phdrs;
4318 stat_ptr = &statement_list;
4322 Create an absolute symbol with the given name with the value of the
4323 address of first byte of the section named.
4325 If the symbol already exists, then do nothing.
4327 void
4328 lang_abs_symbol_at_beginning_of (secname, name)
4329 const char *secname;
4330 const char *name;
4332 struct bfd_link_hash_entry *h;
4334 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4335 if (h == (struct bfd_link_hash_entry *) NULL)
4336 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4338 if (h->type == bfd_link_hash_new
4339 || h->type == bfd_link_hash_undefined)
4341 asection *sec;
4343 h->type = bfd_link_hash_defined;
4345 sec = bfd_get_section_by_name (output_bfd, secname);
4346 if (sec == (asection *) NULL)
4347 h->u.def.value = 0;
4348 else
4349 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4351 h->u.def.section = bfd_abs_section_ptr;
4356 Create an absolute symbol with the given name with the value of the
4357 address of the first byte after the end of the section named.
4359 If the symbol already exists, then do nothing.
4361 void
4362 lang_abs_symbol_at_end_of (secname, name)
4363 const char *secname;
4364 const char *name;
4366 struct bfd_link_hash_entry *h;
4368 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4369 if (h == (struct bfd_link_hash_entry *) NULL)
4370 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4372 if (h->type == bfd_link_hash_new
4373 || h->type == bfd_link_hash_undefined)
4375 asection *sec;
4377 h->type = bfd_link_hash_defined;
4379 sec = bfd_get_section_by_name (output_bfd, secname);
4380 if (sec == (asection *) NULL)
4381 h->u.def.value = 0;
4382 else
4383 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4384 + bfd_section_size (output_bfd, sec) /
4385 bfd_octets_per_byte (output_bfd));
4387 h->u.def.section = bfd_abs_section_ptr;
4391 void
4392 lang_statement_append (list, element, field)
4393 lang_statement_list_type * list;
4394 lang_statement_union_type * element;
4395 lang_statement_union_type ** field;
4397 *(list->tail) = element;
4398 list->tail = field;
4401 /* Set the output format type. -oformat overrides scripts. */
4403 void
4404 lang_add_output_format (format, big, little, from_script)
4405 const char *format;
4406 const char *big;
4407 const char *little;
4408 int from_script;
4410 if (output_target == NULL || !from_script)
4412 if (command_line.endian == ENDIAN_BIG
4413 && big != NULL)
4414 format = big;
4415 else if (command_line.endian == ENDIAN_LITTLE
4416 && little != NULL)
4417 format = little;
4419 output_target = format;
4423 /* Enter a group. This creates a new lang_group_statement, and sets
4424 stat_ptr to build new statements within the group. */
4426 void
4427 lang_enter_group ()
4429 lang_group_statement_type *g;
4431 g = new_stat (lang_group_statement, stat_ptr);
4432 lang_list_init (&g->children);
4433 stat_ptr = &g->children;
4436 /* Leave a group. This just resets stat_ptr to start writing to the
4437 regular list of statements again. Note that this will not work if
4438 groups can occur inside anything else which can adjust stat_ptr,
4439 but currently they can't. */
4441 void
4442 lang_leave_group ()
4444 stat_ptr = &statement_list;
4447 /* Add a new program header. This is called for each entry in a PHDRS
4448 command in a linker script. */
4450 void
4451 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
4452 const char *name;
4453 etree_type *type;
4454 boolean filehdr;
4455 boolean phdrs;
4456 etree_type *at;
4457 etree_type *flags;
4459 struct lang_phdr *n, **pp;
4461 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4462 n->next = NULL;
4463 n->name = name;
4464 n->type = exp_get_value_int (type, 0, "program header type",
4465 lang_final_phase_enum);
4466 n->filehdr = filehdr;
4467 n->phdrs = phdrs;
4468 n->at = at;
4469 n->flags = flags;
4471 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4473 *pp = n;
4476 /* Record the program header information in the output BFD. FIXME: We
4477 should not be calling an ELF specific function here. */
4479 static void
4480 lang_record_phdrs ()
4482 unsigned int alc;
4483 asection **secs;
4484 struct lang_output_section_phdr_list *last;
4485 struct lang_phdr *l;
4486 lang_statement_union_type *u;
4488 alc = 10;
4489 secs = (asection **) xmalloc (alc * sizeof (asection *));
4490 last = NULL;
4491 for (l = lang_phdr_list; l != NULL; l = l->next)
4493 unsigned int c;
4494 flagword flags;
4495 bfd_vma at;
4497 c = 0;
4498 for (u = lang_output_section_statement.head;
4499 u != NULL;
4500 u = u->output_section_statement.next)
4502 lang_output_section_statement_type *os;
4503 struct lang_output_section_phdr_list *pl;
4505 os = &u->output_section_statement;
4507 pl = os->phdrs;
4508 if (pl != NULL)
4509 last = pl;
4510 else
4512 if (os->sectype == noload_section
4513 || os->bfd_section == NULL
4514 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4515 continue;
4516 pl = last;
4519 if (os->bfd_section == NULL)
4520 continue;
4522 for (; pl != NULL; pl = pl->next)
4524 if (strcmp (pl->name, l->name) == 0)
4526 if (c >= alc)
4528 alc *= 2;
4529 secs = ((asection **)
4530 xrealloc (secs, alc * sizeof (asection *)));
4532 secs[c] = os->bfd_section;
4533 ++c;
4534 pl->used = true;
4539 if (l->flags == NULL)
4540 flags = 0;
4541 else
4542 flags = exp_get_vma (l->flags, 0, "phdr flags",
4543 lang_final_phase_enum);
4545 if (l->at == NULL)
4546 at = 0;
4547 else
4548 at = exp_get_vma (l->at, 0, "phdr load address",
4549 lang_final_phase_enum);
4551 if (! bfd_record_phdr (output_bfd, l->type,
4552 l->flags == NULL ? false : true,
4553 flags,
4554 l->at == NULL ? false : true,
4555 at, l->filehdr, l->phdrs, c, secs))
4556 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4559 free (secs);
4561 /* Make sure all the phdr assignments succeeded. */
4562 for (u = lang_output_section_statement.head;
4563 u != NULL;
4564 u = u->output_section_statement.next)
4566 struct lang_output_section_phdr_list *pl;
4568 if (u->output_section_statement.bfd_section == NULL)
4569 continue;
4571 for (pl = u->output_section_statement.phdrs;
4572 pl != NULL;
4573 pl = pl->next)
4574 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4575 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4576 u->output_section_statement.name, pl->name);
4580 /* Record a list of sections which may not be cross referenced. */
4582 void
4583 lang_add_nocrossref (l)
4584 struct lang_nocrossref *l;
4586 struct lang_nocrossrefs *n;
4588 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4589 n->next = nocrossref_list;
4590 n->list = l;
4591 nocrossref_list = n;
4593 /* Set notice_all so that we get informed about all symbols. */
4594 link_info.notice_all = true;
4597 /* Overlay handling. We handle overlays with some static variables. */
4599 /* The overlay virtual address. */
4600 static etree_type *overlay_vma;
4602 /* The overlay load address. */
4603 static etree_type *overlay_lma;
4605 /* Whether nocrossrefs is set for this overlay. */
4606 static int overlay_nocrossrefs;
4608 /* An expression for the maximum section size seen so far. */
4609 static etree_type *overlay_max;
4611 /* A list of all the sections in this overlay. */
4613 struct overlay_list
4615 struct overlay_list *next;
4616 lang_output_section_statement_type *os;
4619 static struct overlay_list *overlay_list;
4621 /* Start handling an overlay. */
4623 void
4624 lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
4625 etree_type *vma_expr;
4626 etree_type *lma_expr;
4627 int nocrossrefs;
4629 /* The grammar should prevent nested overlays from occurring. */
4630 ASSERT (overlay_vma == NULL
4631 && overlay_lma == NULL
4632 && overlay_list == NULL
4633 && overlay_max == NULL);
4635 overlay_vma = vma_expr;
4636 overlay_lma = lma_expr;
4637 overlay_nocrossrefs = nocrossrefs;
4640 /* Start a section in an overlay. We handle this by calling
4641 lang_enter_output_section_statement with the correct VMA and LMA. */
4643 void
4644 lang_enter_overlay_section (name)
4645 const char *name;
4647 struct overlay_list *n;
4648 etree_type *size;
4650 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4651 0, 0, 0, overlay_lma);
4653 /* If this is the first section, then base the VMA and LMA of future
4654 sections on this one. This will work correctly even if `.' is
4655 used in the addresses. */
4656 if (overlay_list == NULL)
4658 overlay_vma = exp_nameop (ADDR, name);
4659 overlay_lma = exp_nameop (LOADADDR, name);
4662 /* Remember the section. */
4663 n = (struct overlay_list *) xmalloc (sizeof *n);
4664 n->os = current_section;
4665 n->next = overlay_list;
4666 overlay_list = n;
4668 size = exp_nameop (SIZEOF, name);
4670 /* Adjust the LMA for the next section. */
4671 overlay_lma = exp_binop ('+', overlay_lma, size);
4673 /* Arrange to work out the maximum section end address. */
4674 if (overlay_max == NULL)
4675 overlay_max = size;
4676 else
4677 overlay_max = exp_binop (MAX_K, overlay_max, size);
4680 /* Finish a section in an overlay. There isn't any special to do
4681 here. */
4683 void
4684 lang_leave_overlay_section (fill, phdrs)
4685 bfd_vma fill;
4686 struct lang_output_section_phdr_list *phdrs;
4688 const char *name;
4689 char *clean, *s2;
4690 const char *s1;
4691 char *buf;
4693 name = current_section->name;
4695 lang_leave_output_section_statement (fill, "*default*",
4696 phdrs, "*default*");
4698 /* Define the magic symbols. */
4700 clean = xmalloc (strlen (name) + 1);
4701 s2 = clean;
4702 for (s1 = name; *s1 != '\0'; s1++)
4703 if (isalnum ((unsigned char) *s1) || *s1 == '_')
4704 *s2++ = *s1;
4705 *s2 = '\0';
4707 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4708 sprintf (buf, "__load_start_%s", clean);
4709 lang_add_assignment (exp_assop ('=', buf,
4710 exp_nameop (LOADADDR, name)));
4712 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4713 sprintf (buf, "__load_stop_%s", clean);
4714 lang_add_assignment (exp_assop ('=', buf,
4715 exp_binop ('+',
4716 exp_nameop (LOADADDR, name),
4717 exp_nameop (SIZEOF, name))));
4719 free (clean);
4722 /* Finish an overlay. If there are any overlay wide settings, this
4723 looks through all the sections in the overlay and sets them. */
4725 void
4726 lang_leave_overlay (fill, memspec, phdrs, lma_memspec)
4727 bfd_vma fill;
4728 const char *memspec;
4729 struct lang_output_section_phdr_list *phdrs;
4730 const char *lma_memspec;
4732 lang_memory_region_type *region;
4733 lang_memory_region_type *lma_region;
4734 struct overlay_list *l;
4735 struct lang_nocrossref *nocrossref;
4737 if (memspec == NULL)
4738 region = NULL;
4739 else
4740 region = lang_memory_region_lookup (memspec);
4742 if (lma_memspec == NULL)
4743 lma_region = NULL;
4744 else
4745 lma_region = lang_memory_region_lookup (lma_memspec);
4747 nocrossref = NULL;
4749 l = overlay_list;
4750 while (l != NULL)
4752 struct overlay_list *next;
4754 if (fill != 0 && l->os->fill == 0)
4755 l->os->fill = fill;
4756 if (region != NULL && l->os->region == NULL)
4757 l->os->region = region;
4758 if (lma_region != NULL && l->os->lma_region == NULL)
4759 l->os->lma_region = lma_region;
4760 if (phdrs != NULL && l->os->phdrs == NULL)
4761 l->os->phdrs = phdrs;
4763 if (overlay_nocrossrefs)
4765 struct lang_nocrossref *nc;
4767 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4768 nc->name = l->os->name;
4769 nc->next = nocrossref;
4770 nocrossref = nc;
4773 next = l->next;
4774 free (l);
4775 l = next;
4778 if (nocrossref != NULL)
4779 lang_add_nocrossref (nocrossref);
4781 /* Update . for the end of the overlay. */
4782 lang_add_assignment (exp_assop ('=', ".",
4783 exp_binop ('+', overlay_vma, overlay_max)));
4785 overlay_vma = NULL;
4786 overlay_lma = NULL;
4787 overlay_nocrossrefs = 0;
4788 overlay_list = NULL;
4789 overlay_max = NULL;
4792 /* Version handling. This is only useful for ELF. */
4794 /* This global variable holds the version tree that we build. */
4796 struct bfd_elf_version_tree *lang_elf_version_info;
4798 static int
4799 lang_vers_match_lang_c (expr, sym)
4800 struct bfd_elf_version_expr *expr;
4801 const char *sym;
4803 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4804 return 1;
4805 return fnmatch (expr->pattern, sym, 0) == 0;
4808 static int
4809 lang_vers_match_lang_cplusplus (expr, sym)
4810 struct bfd_elf_version_expr *expr;
4811 const char *sym;
4813 char *alt_sym;
4814 int result;
4816 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4817 return 1;
4819 alt_sym = cplus_demangle(sym, /* DMGL_NO_TPARAMS */ 0);
4820 if (!alt_sym)
4822 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4823 Should we early out false in this case? */
4824 result = fnmatch (expr->pattern, sym, 0) == 0;
4826 else
4828 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4829 free (alt_sym);
4832 return result;
4835 static int
4836 lang_vers_match_lang_java (expr, sym)
4837 struct bfd_elf_version_expr *expr;
4838 const char *sym;
4840 char *alt_sym;
4841 int result;
4843 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4844 return 1;
4846 alt_sym = cplus_demangle(sym, DMGL_JAVA);
4847 if (!alt_sym)
4849 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4850 Should we early out false in this case? */
4851 result = fnmatch (expr->pattern, sym, 0) == 0;
4853 else
4855 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4856 free (alt_sym);
4859 return result;
4862 /* This is called for each variable name or match expression. */
4864 struct bfd_elf_version_expr *
4865 lang_new_vers_regex (orig, new, lang)
4866 struct bfd_elf_version_expr *orig;
4867 const char *new;
4868 const char *lang;
4870 struct bfd_elf_version_expr *ret;
4872 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4873 ret->next = orig;
4874 ret->pattern = new;
4876 if (lang == NULL || strcasecmp (lang, "C") == 0)
4877 ret->match = lang_vers_match_lang_c;
4878 else if (strcasecmp (lang, "C++") == 0)
4879 ret->match = lang_vers_match_lang_cplusplus;
4880 else if (strcasecmp (lang, "Java") == 0)
4881 ret->match = lang_vers_match_lang_java;
4882 else
4884 einfo (_("%X%P: unknown language `%s' in version information\n"),
4885 lang);
4886 ret->match = lang_vers_match_lang_c;
4889 return ret;
4892 /* This is called for each set of variable names and match
4893 expressions. */
4895 struct bfd_elf_version_tree *
4896 lang_new_vers_node (globals, locals)
4897 struct bfd_elf_version_expr *globals;
4898 struct bfd_elf_version_expr *locals;
4900 struct bfd_elf_version_tree *ret;
4902 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4903 ret->next = NULL;
4904 ret->name = NULL;
4905 ret->vernum = 0;
4906 ret->globals = globals;
4907 ret->locals = locals;
4908 ret->deps = NULL;
4909 ret->name_indx = (unsigned int) -1;
4910 ret->used = 0;
4911 return ret;
4914 /* This static variable keeps track of version indices. */
4916 static int version_index;
4918 /* This is called when we know the name and dependencies of the
4919 version. */
4921 void
4922 lang_register_vers_node (name, version, deps)
4923 const char *name;
4924 struct bfd_elf_version_tree *version;
4925 struct bfd_elf_version_deps *deps;
4927 struct bfd_elf_version_tree *t, **pp;
4928 struct bfd_elf_version_expr *e1;
4930 /* Make sure this node has a unique name. */
4931 for (t = lang_elf_version_info; t != NULL; t = t->next)
4932 if (strcmp (t->name, name) == 0)
4933 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
4935 /* Check the global and local match names, and make sure there
4936 aren't any duplicates. */
4938 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4940 for (t = lang_elf_version_info; t != NULL; t = t->next)
4942 struct bfd_elf_version_expr *e2;
4944 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4945 if (strcmp (e1->pattern, e2->pattern) == 0)
4946 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4947 e1->pattern);
4951 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4953 for (t = lang_elf_version_info; t != NULL; t = t->next)
4955 struct bfd_elf_version_expr *e2;
4957 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4958 if (strcmp (e1->pattern, e2->pattern) == 0)
4959 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4960 e1->pattern);
4964 version->deps = deps;
4965 version->name = name;
4966 ++version_index;
4967 version->vernum = version_index;
4969 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4971 *pp = version;
4974 /* This is called when we see a version dependency. */
4976 struct bfd_elf_version_deps *
4977 lang_add_vers_depend (list, name)
4978 struct bfd_elf_version_deps *list;
4979 const char *name;
4981 struct bfd_elf_version_deps *ret;
4982 struct bfd_elf_version_tree *t;
4984 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4985 ret->next = list;
4987 for (t = lang_elf_version_info; t != NULL; t = t->next)
4989 if (strcmp (t->name, name) == 0)
4991 ret->version_needed = t;
4992 return ret;
4996 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
4998 return ret;
5001 static void
5002 lang_do_version_exports_section ()
5004 struct bfd_elf_version_expr *greg = NULL, *lreg;
5006 LANG_FOR_EACH_INPUT_STATEMENT (is)
5008 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5009 char *contents, *p;
5010 bfd_size_type len;
5012 if (sec == NULL)
5013 continue;
5015 len = bfd_section_size (is->the_bfd, sec);
5016 contents = xmalloc (len);
5017 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5018 einfo (_("%X%P: unable to read .exports section contents"), sec);
5020 p = contents;
5021 while (p < contents+len)
5023 greg = lang_new_vers_regex (greg, p, NULL);
5024 p = strchr (p, '\0') + 1;
5027 /* Do not free the contents, as we used them creating the regex. */
5029 /* Do not include this section in the link. */
5030 bfd_set_section_flags (is->the_bfd, sec,
5031 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5034 lreg = lang_new_vers_regex (NULL, "*", NULL);
5035 lang_register_vers_node (command_line.version_exports_section,
5036 lang_new_vers_node (greg, lreg), NULL);