Initial revision
[binutils.git] / ld / ldlang.c
blobaa5c68131ddae2d516f75709977640165f127793
1 /* Linker command language support.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
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 lang_statement_list_type lang_output_section_statement;
62 static CONST char *current_target;
63 static CONST char *output_target;
64 static lang_statement_list_type statement_list;
65 static struct lang_phdr *lang_phdr_list;
67 static void lang_for_each_statement_worker
68 PARAMS ((void (*func) (lang_statement_union_type *),
69 lang_statement_union_type *s));
70 static lang_input_statement_type *new_afile
71 PARAMS ((const char *name, lang_input_file_enum_type file_type,
72 const char *target, boolean add_to_list));
73 static void init_os PARAMS ((lang_output_section_statement_type *s));
74 static void exp_init_os PARAMS ((etree_type *));
75 static void section_already_linked PARAMS ((bfd *, asection *, PTR));
76 static boolean wildcardp PARAMS ((const char *));
77 static lang_statement_union_type *wild_sort
78 PARAMS ((lang_wild_statement_type *, lang_input_statement_type *,
79 asection *));
80 static void wild_section PARAMS ((lang_wild_statement_type *ptr,
81 const char *section,
82 lang_input_statement_type *file,
83 lang_output_section_statement_type *output));
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_file PARAMS ((lang_wild_statement_type *, const char *,
88 lang_input_statement_type *,
89 lang_output_section_statement_type *));
90 static void wild PARAMS ((lang_wild_statement_type *s,
91 const char *section, const char *file,
92 const char *target,
93 lang_output_section_statement_type *output));
94 static bfd *open_output PARAMS ((const char *name));
95 static void ldlang_open_output PARAMS ((lang_statement_union_type *statement));
96 static void open_input_bfds
97 PARAMS ((lang_statement_union_type *statement, boolean));
98 static void lang_reasonable_defaults PARAMS ((void));
99 static void lang_place_undefineds PARAMS ((void));
100 static void map_input_to_output_sections
101 PARAMS ((lang_statement_union_type *s,
102 const char *target,
103 lang_output_section_statement_type *output_section_statement));
104 static void print_output_section_statement
105 PARAMS ((lang_output_section_statement_type *output_section_statement));
106 static void print_assignment
107 PARAMS ((lang_assignment_statement_type *assignment,
108 lang_output_section_statement_type *output_section));
109 static void print_input_statement PARAMS ((lang_input_statement_type *statm));
110 static boolean print_one_symbol PARAMS ((struct bfd_link_hash_entry *, PTR));
111 static void print_input_section PARAMS ((lang_input_section_type *in));
112 static void print_fill_statement PARAMS ((lang_fill_statement_type *fill));
113 static void print_data_statement PARAMS ((lang_data_statement_type *data));
114 static void print_address_statement PARAMS ((lang_address_statement_type *));
115 static void print_reloc_statement PARAMS ((lang_reloc_statement_type *reloc));
116 static void print_padding_statement PARAMS ((lang_padding_statement_type *s));
117 static void print_wild_statement
118 PARAMS ((lang_wild_statement_type *w,
119 lang_output_section_statement_type *os));
120 static void print_group
121 PARAMS ((lang_group_statement_type *, lang_output_section_statement_type *));
122 static void print_statement PARAMS ((lang_statement_union_type *s,
123 lang_output_section_statement_type *os));
124 static void print_statement_list PARAMS ((lang_statement_union_type *s,
125 lang_output_section_statement_type *os));
126 static void print_statements PARAMS ((void));
127 static bfd_vma insert_pad PARAMS ((lang_statement_union_type **this_ptr,
128 fill_type fill, unsigned int power,
129 asection *output_section_statement,
130 bfd_vma dot));
131 static bfd_vma size_input_section
132 PARAMS ((lang_statement_union_type **this_ptr,
133 lang_output_section_statement_type *output_section_statement,
134 fill_type fill, bfd_vma dot, boolean relax));
135 static void lang_finish PARAMS ((void));
136 static void ignore_bfd_errors PARAMS ((const char *, ...));
137 static void lang_check PARAMS ((void));
138 static void lang_common PARAMS ((void));
139 static boolean lang_one_common PARAMS ((struct bfd_link_hash_entry *, PTR));
140 static void lang_place_orphans PARAMS ((void));
141 static int topower PARAMS ((int));
142 static void lang_set_startof PARAMS ((void));
143 static void reset_memory_regions PARAMS ((void));
144 static void lang_record_phdrs PARAMS ((void));
145 static void lang_gc_wild_section
146 PARAMS ((lang_wild_statement_type *, const char *,
147 lang_input_statement_type *));
148 static void lang_gc_wild_file
149 PARAMS ((lang_wild_statement_type *, const char *,
150 lang_input_statement_type *));
151 static void lang_gc_wild
152 PARAMS ((lang_wild_statement_type *, const char *, const char *));
153 static void lang_gc_sections_1 PARAMS ((lang_statement_union_type *));
154 static void lang_gc_sections PARAMS ((void));
155 static void lang_do_version_exports_section PARAMS ((void));
156 static void lang_check_section_addresses PARAMS ((void));
159 /* EXPORTS */
160 lang_output_section_statement_type *abs_output_section;
161 lang_statement_list_type *stat_ptr = &statement_list;
162 lang_statement_list_type file_chain = { 0 };
163 const char *entry_symbol = NULL;
164 boolean entry_from_cmdline;
165 boolean lang_has_input_file = false;
166 boolean had_output_filename = false;
167 boolean lang_float_flag = false;
168 boolean delete_output_file_on_failure = false;
169 struct lang_nocrossrefs *nocrossref_list;
171 etree_type *base; /* Relocation base - or null */
174 #if defined(__STDC__) || defined(ALMOST_STDC)
175 #define cat(a,b) a##b
176 #else
177 #define cat(a,b) a/**/b
178 #endif
180 #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
182 #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
184 #define outside_symbol_address(q) ((q)->value + outside_section_address(q->section))
186 #define SECTION_NAME_MAP_LENGTH (16)
189 stat_alloc (size)
190 size_t size;
192 return obstack_alloc (&stat_obstack, size);
195 /*----------------------------------------------------------------------
196 lang_for_each_statement walks the parse tree and calls the provided
197 function for each node
200 static void
201 lang_for_each_statement_worker (func, s)
202 void (*func) PARAMS ((lang_statement_union_type *));
203 lang_statement_union_type *s;
205 for (; s != (lang_statement_union_type *) NULL; s = s->next)
207 func (s);
209 switch (s->header.type)
211 case lang_constructors_statement_enum:
212 lang_for_each_statement_worker (func, constructor_list.head);
213 break;
214 case lang_output_section_statement_enum:
215 lang_for_each_statement_worker
216 (func,
217 s->output_section_statement.children.head);
218 break;
219 case lang_wild_statement_enum:
220 lang_for_each_statement_worker
221 (func,
222 s->wild_statement.children.head);
223 break;
224 case lang_group_statement_enum:
225 lang_for_each_statement_worker (func,
226 s->group_statement.children.head);
227 break;
228 case lang_data_statement_enum:
229 case lang_reloc_statement_enum:
230 case lang_object_symbols_statement_enum:
231 case lang_output_statement_enum:
232 case lang_target_statement_enum:
233 case lang_input_section_enum:
234 case lang_input_statement_enum:
235 case lang_assignment_statement_enum:
236 case lang_padding_statement_enum:
237 case lang_address_statement_enum:
238 case lang_fill_statement_enum:
239 break;
240 default:
241 FAIL ();
242 break;
247 void
248 lang_for_each_statement (func)
249 void (*func) PARAMS ((lang_statement_union_type *));
251 lang_for_each_statement_worker (func,
252 statement_list.head);
255 /*----------------------------------------------------------------------*/
256 void
257 lang_list_init (list)
258 lang_statement_list_type *list;
260 list->head = (lang_statement_union_type *) NULL;
261 list->tail = &list->head;
264 /*----------------------------------------------------------------------
266 build a new statement node for the parse tree
270 static
271 lang_statement_union_type *
272 new_statement (type, size, list)
273 enum statement_enum type;
274 size_t size;
275 lang_statement_list_type * list;
277 lang_statement_union_type *new = (lang_statement_union_type *)
278 stat_alloc (size);
280 new->header.type = type;
281 new->header.next = (lang_statement_union_type *) NULL;
282 lang_statement_append (list, new, &new->header.next);
283 return new;
287 Build a new input file node for the language. There are several ways
288 in which we treat an input file, eg, we only look at symbols, or
289 prefix it with a -l etc.
291 We can be supplied with requests for input files more than once;
292 they may, for example be split over serveral lines like foo.o(.text)
293 foo.o(.data) etc, so when asked for a file we check that we havn't
294 got it already so we don't duplicate the bfd.
297 static lang_input_statement_type *
298 new_afile (name, file_type, target, add_to_list)
299 CONST char *name;
300 lang_input_file_enum_type file_type;
301 CONST char *target;
302 boolean add_to_list;
304 lang_input_statement_type *p;
306 if (add_to_list)
307 p = new_stat (lang_input_statement, stat_ptr);
308 else
310 p = ((lang_input_statement_type *)
311 stat_alloc (sizeof (lang_input_statement_type)));
312 p->header.next = NULL;
315 lang_has_input_file = true;
316 p->target = target;
317 switch (file_type)
319 case lang_input_file_is_symbols_only_enum:
320 p->filename = name;
321 p->is_archive = false;
322 p->real = true;
323 p->local_sym_name = name;
324 p->just_syms_flag = true;
325 p->search_dirs_flag = false;
326 break;
327 case lang_input_file_is_fake_enum:
328 p->filename = name;
329 p->is_archive = false;
330 p->real = false;
331 p->local_sym_name = name;
332 p->just_syms_flag = false;
333 p->search_dirs_flag = false;
334 break;
335 case lang_input_file_is_l_enum:
336 p->is_archive = true;
337 p->filename = name;
338 p->real = true;
339 p->local_sym_name = concat ("-l", name, (const char *) NULL);
340 p->just_syms_flag = false;
341 p->search_dirs_flag = true;
342 break;
343 case lang_input_file_is_marker_enum:
344 p->filename = name;
345 p->is_archive = false;
346 p->real = false;
347 p->local_sym_name = name;
348 p->just_syms_flag = false;
349 p->search_dirs_flag = true;
350 break;
351 case lang_input_file_is_search_file_enum:
352 p->filename = name;
353 p->is_archive = false;
354 p->real = true;
355 p->local_sym_name = name;
356 p->just_syms_flag = false;
357 p->search_dirs_flag = true;
358 break;
359 case lang_input_file_is_file_enum:
360 p->filename = name;
361 p->is_archive = false;
362 p->real = true;
363 p->local_sym_name = name;
364 p->just_syms_flag = false;
365 p->search_dirs_flag = false;
366 break;
367 default:
368 FAIL ();
370 p->the_bfd = (bfd *) NULL;
371 p->asymbols = (asymbol **) NULL;
372 p->next_real_file = (lang_statement_union_type *) NULL;
373 p->next = (lang_statement_union_type *) NULL;
374 p->symbol_count = 0;
375 p->dynamic = config.dynamic_link;
376 p->whole_archive = whole_archive;
377 p->loaded = false;
378 lang_statement_append (&input_file_chain,
379 (lang_statement_union_type *) p,
380 &p->next_real_file);
381 return p;
384 lang_input_statement_type *
385 lang_add_input_file (name, file_type, target)
386 CONST char *name;
387 lang_input_file_enum_type file_type;
388 CONST char *target;
390 lang_has_input_file = true;
391 return new_afile (name, file_type, target, true);
394 /* Build enough state so that the parser can build its tree */
395 void
396 lang_init ()
398 obstack_begin (&stat_obstack, 1000);
400 stat_ptr = &statement_list;
402 lang_list_init (stat_ptr);
404 lang_list_init (&input_file_chain);
405 lang_list_init (&lang_output_section_statement);
406 lang_list_init (&file_chain);
407 first_file = lang_add_input_file ((char *) NULL,
408 lang_input_file_is_marker_enum,
409 (char *) NULL);
410 abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
412 abs_output_section->bfd_section = bfd_abs_section_ptr;
416 /*----------------------------------------------------------------------
417 A region is an area of memory declared with the
418 MEMORY { name:org=exp, len=exp ... }
419 syntax.
421 We maintain a list of all the regions here
423 If no regions are specified in the script, then the default is used
424 which is created when looked up to be the entire data space
427 static lang_memory_region_type *lang_memory_region_list;
428 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
430 lang_memory_region_type *
431 lang_memory_region_lookup (name)
432 CONST char *CONST name;
434 lang_memory_region_type *p;
436 for (p = lang_memory_region_list;
437 p != (lang_memory_region_type *) NULL;
438 p = p->next)
440 if (strcmp (p->name, name) == 0)
442 return p;
446 #if 0
447 /* This code used to always use the first region in the list as the
448 default region. I changed it to instead use a region
449 encompassing all of memory as the default region. This permits
450 NOLOAD sections to work reasonably without requiring a region.
451 People should specify what region they mean, if they really want
452 a region. */
453 if (strcmp (name, "*default*") == 0)
455 if (lang_memory_region_list != (lang_memory_region_type *) NULL)
457 return lang_memory_region_list;
460 #endif
463 lang_memory_region_type *new =
464 (lang_memory_region_type *) stat_alloc (sizeof (lang_memory_region_type));
466 new->name = buystring (name);
467 new->next = (lang_memory_region_type *) NULL;
469 *lang_memory_region_list_tail = new;
470 lang_memory_region_list_tail = &new->next;
471 new->origin = 0;
472 new->flags = 0;
473 new->not_flags = 0;
474 new->length = ~(bfd_size_type)0;
475 new->current = 0;
476 new->had_full_message = false;
478 return new;
483 lang_memory_region_type *
484 lang_memory_default (section)
485 asection *section;
487 lang_memory_region_type *p;
489 flagword sec_flags = section->flags;
491 /* Override SEC_DATA to mean a writable section. */
492 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
493 sec_flags |= SEC_DATA;
495 for (p = lang_memory_region_list;
496 p != (lang_memory_region_type *) NULL;
497 p = p->next)
499 if ((p->flags & sec_flags) != 0
500 && (p->not_flags & sec_flags) == 0)
502 return p;
505 return lang_memory_region_lookup ("*default*");
508 lang_output_section_statement_type *
509 lang_output_section_find (name)
510 CONST char *CONST name;
512 lang_statement_union_type *u;
513 lang_output_section_statement_type *lookup;
515 for (u = lang_output_section_statement.head;
516 u != (lang_statement_union_type *) NULL;
517 u = lookup->next)
519 lookup = &u->output_section_statement;
520 if (strcmp (name, lookup->name) == 0)
522 return lookup;
525 return (lang_output_section_statement_type *) NULL;
528 lang_output_section_statement_type *
529 lang_output_section_statement_lookup (name)
530 CONST char *CONST name;
532 lang_output_section_statement_type *lookup;
534 lookup = lang_output_section_find (name);
535 if (lookup == (lang_output_section_statement_type *) NULL)
538 lookup = (lang_output_section_statement_type *)
539 new_stat (lang_output_section_statement, stat_ptr);
540 lookup->region = (lang_memory_region_type *) NULL;
541 lookup->fill = 0;
542 lookup->block_value = 1;
543 lookup->name = name;
545 lookup->next = (lang_statement_union_type *) NULL;
546 lookup->bfd_section = (asection *) NULL;
547 lookup->processed = false;
548 lookup->sectype = normal_section;
549 lookup->addr_tree = (etree_type *) NULL;
550 lang_list_init (&lookup->children);
552 lookup->memspec = (CONST char *) NULL;
553 lookup->flags = 0;
554 lookup->subsection_alignment = -1;
555 lookup->section_alignment = -1;
556 lookup->load_base = (union etree_union *) NULL;
557 lookup->phdrs = NULL;
559 lang_statement_append (&lang_output_section_statement,
560 (lang_statement_union_type *) lookup,
561 &lookup->next);
563 return lookup;
566 static void
567 lang_map_flags (flag)
568 flagword flag;
570 if (flag & SEC_ALLOC)
571 minfo ("a");
573 if (flag & SEC_CODE)
574 minfo ("x");
576 if (flag & SEC_READONLY)
577 minfo ("r");
579 if (flag & SEC_DATA)
580 minfo ("w");
582 if (flag & SEC_LOAD)
583 minfo ("l");
586 void
587 lang_map ()
589 lang_memory_region_type *m;
591 minfo (_("\nMemory Configuration\n\n"));
592 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
593 _("Name"), _("Origin"), _("Length"), _("Attributes"));
595 for (m = lang_memory_region_list;
596 m != (lang_memory_region_type *) NULL;
597 m = m->next)
599 char buf[100];
600 int len;
602 fprintf (config.map_file, "%-16s ", m->name);
604 sprintf_vma (buf, m->origin);
605 minfo ("0x%s ", buf);
606 len = strlen (buf);
607 while (len < 16)
609 print_space ();
610 ++len;
613 minfo ("0x%V", m->length);
614 if (m->flags || m->not_flags)
616 #ifndef BFD64
617 minfo (" ");
618 #endif
619 if (m->flags)
621 print_space ();
622 lang_map_flags (m->flags);
625 if (m->not_flags)
627 minfo (" !");
628 lang_map_flags (m->not_flags);
632 print_nl ();
635 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
637 print_statements ();
640 /* Initialize an output section. */
642 static void
643 init_os (s)
644 lang_output_section_statement_type *s;
646 section_userdata_type *new;
648 if (s->bfd_section != NULL)
649 return;
651 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
652 einfo (_("%P%F: Illegal use of `%s' section"), DISCARD_SECTION_NAME);
654 new = ((section_userdata_type *)
655 stat_alloc (sizeof (section_userdata_type)));
657 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
658 if (s->bfd_section == (asection *) NULL)
659 s->bfd_section = bfd_make_section (output_bfd, s->name);
660 if (s->bfd_section == (asection *) NULL)
662 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
663 output_bfd->xvec->name, s->name);
665 s->bfd_section->output_section = s->bfd_section;
667 /* We initialize an output sections output offset to minus its own */
668 /* vma to allow us to output a section through itself */
669 s->bfd_section->output_offset = 0;
670 get_userdata (s->bfd_section) = (PTR) new;
672 /* If there is a base address, make sure that any sections it might
673 mention are initialized. */
674 if (s->addr_tree != NULL)
675 exp_init_os (s->addr_tree);
678 /* Make sure that all output sections mentioned in an expression are
679 initialized. */
681 static void
682 exp_init_os (exp)
683 etree_type *exp;
685 switch (exp->type.node_class)
687 case etree_assign:
688 exp_init_os (exp->assign.src);
689 break;
691 case etree_binary:
692 exp_init_os (exp->binary.lhs);
693 exp_init_os (exp->binary.rhs);
694 break;
696 case etree_trinary:
697 exp_init_os (exp->trinary.cond);
698 exp_init_os (exp->trinary.lhs);
699 exp_init_os (exp->trinary.rhs);
700 break;
702 case etree_unary:
703 exp_init_os (exp->unary.child);
704 break;
706 case etree_name:
707 switch (exp->type.node_code)
709 case ADDR:
710 case LOADADDR:
711 case SIZEOF:
713 lang_output_section_statement_type *os;
715 os = lang_output_section_find (exp->name.name);
716 if (os != NULL && os->bfd_section == NULL)
717 init_os (os);
720 break;
722 default:
723 break;
727 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
728 once into the output. This routine checks each sections, and
729 arranges to discard it if a section of the same name has already
730 been linked. This code assumes that all relevant sections have the
731 SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
732 section name. This is called via bfd_map_over_sections. */
734 /*ARGSUSED*/
735 static void
736 section_already_linked (abfd, sec, data)
737 bfd *abfd;
738 asection *sec;
739 PTR data;
741 lang_input_statement_type *entry = (lang_input_statement_type *) data;
742 struct sec_link_once
744 struct sec_link_once *next;
745 asection *sec;
747 static struct sec_link_once *sec_link_once_list;
748 flagword flags;
749 const char *name;
750 struct sec_link_once *l;
752 /* If we are only reading symbols from this object, then we want to
753 discard all sections. */
754 if (entry->just_syms_flag)
756 sec->output_section = bfd_abs_section_ptr;
757 sec->output_offset = sec->vma;
758 return;
761 flags = bfd_get_section_flags (abfd, sec);
763 if ((flags & SEC_LINK_ONCE) == 0)
764 return;
766 name = bfd_get_section_name (abfd, sec);
768 for (l = sec_link_once_list; l != NULL; l = l->next)
770 if (strcmp (name, bfd_get_section_name (l->sec->owner, l->sec)) == 0)
772 /* The section has already been linked. See if we should
773 issue a warning. */
774 switch (flags & SEC_LINK_DUPLICATES)
776 default:
777 abort ();
779 case SEC_LINK_DUPLICATES_DISCARD:
780 break;
782 case SEC_LINK_DUPLICATES_ONE_ONLY:
783 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
784 abfd, name);
785 break;
787 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
788 /* FIXME: We should really dig out the contents of both
789 sections and memcmp them. The COFF/PE spec says that
790 the Microsoft linker does not implement this
791 correctly, so I'm not going to bother doing it
792 either. */
793 /* Fall through. */
794 case SEC_LINK_DUPLICATES_SAME_SIZE:
795 if (bfd_section_size (abfd, sec)
796 != bfd_section_size (l->sec->owner, l->sec))
797 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
798 abfd, name);
799 break;
802 /* Set the output_section field so that wild_doit does not
803 create a lang_input_section structure for this section. */
804 sec->output_section = bfd_abs_section_ptr;
806 return;
810 /* This is the first section with this name. Record it. */
812 l = (struct sec_link_once *) xmalloc (sizeof *l);
813 l->sec = sec;
814 l->next = sec_link_once_list;
815 sec_link_once_list = l;
818 /* The wild routines.
820 These expand statements like *(.text) and foo.o to a list of
821 explicit actions, like foo.o(.text), bar.o(.text) and
822 foo.o(.text, .data). */
824 /* Return true if the PATTERN argument is a wildcard pattern.
825 Although backslashes are treated specially if a pattern contains
826 wildcards, we do not consider the mere presence of a backslash to
827 be enough to cause the the pattern to be treated as a wildcard.
828 That lets us handle DOS filenames more naturally. */
830 static boolean
831 wildcardp (pattern)
832 const char *pattern;
834 const char *s;
836 for (s = pattern; *s != '\0'; ++s)
837 if (*s == '?'
838 || *s == '*'
839 || *s == '[')
840 return true;
841 return false;
844 /* Add SECTION to the output section OUTPUT. Do this by creating a
845 lang_input_section statement which is placed at PTR. FILE is the
846 input file which holds SECTION. */
848 void
849 wild_doit (ptr, section, output, file)
850 lang_statement_list_type *ptr;
851 asection *section;
852 lang_output_section_statement_type *output;
853 lang_input_statement_type *file;
855 flagword flags;
856 boolean discard;
858 flags = bfd_get_section_flags (section->owner, section);
860 discard = false;
862 /* If we are doing a final link, discard sections marked with
863 SEC_EXCLUDE. */
864 if (! link_info.relocateable
865 && (flags & SEC_EXCLUDE) != 0)
866 discard = true;
868 /* Discard input sections which are assigned to a section named
869 DISCARD_SECTION_NAME. */
870 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
871 discard = true;
873 /* Discard debugging sections if we are stripping debugging
874 information. */
875 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
876 && (flags & SEC_DEBUGGING) != 0)
877 discard = true;
879 if (discard)
881 if (section->output_section == NULL)
883 /* This prevents future calls from assigning this section. */
884 section->output_section = bfd_abs_section_ptr;
886 return;
889 if (section->output_section == NULL)
891 boolean first;
892 lang_input_section_type *new;
893 flagword flags;
895 if (output->bfd_section == NULL)
897 init_os (output);
898 first = true;
900 else
901 first = false;
903 /* Add a section reference to the list */
904 new = new_stat (lang_input_section, ptr);
906 new->section = section;
907 new->ifile = file;
908 section->output_section = output->bfd_section;
910 flags = section->flags;
912 /* We don't copy the SEC_NEVER_LOAD flag from an input section
913 to an output section, because we want to be able to include a
914 SEC_NEVER_LOAD section in the middle of an otherwise loaded
915 section (I don't know why we want to do this, but we do).
916 build_link_order in ldwrite.c handles this case by turning
917 the embedded SEC_NEVER_LOAD section into a fill. */
919 flags &= ~ SEC_NEVER_LOAD;
921 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
922 already been processed. One reason to do this is that on pe
923 format targets, .text$foo sections go into .text and it's odd
924 to see .text with SEC_LINK_ONCE set. */
926 if (! link_info.relocateable)
927 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
929 /* If this is not the first input section, and the SEC_READONLY
930 flag is not currently set, then don't set it just because the
931 input section has it set. */
933 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
934 flags &= ~ SEC_READONLY;
936 section->output_section->flags |= flags;
938 /* If SEC_READONLY is not set in the input section, then clear
939 it from the output section. */
940 if ((section->flags & SEC_READONLY) == 0)
941 section->output_section->flags &= ~SEC_READONLY;
943 switch (output->sectype)
945 case normal_section:
946 break;
947 case dsect_section:
948 case copy_section:
949 case info_section:
950 case overlay_section:
951 output->bfd_section->flags &= ~SEC_ALLOC;
952 break;
953 case noload_section:
954 output->bfd_section->flags &= ~SEC_LOAD;
955 output->bfd_section->flags |= SEC_NEVER_LOAD;
956 break;
959 if (section->alignment_power > output->bfd_section->alignment_power)
960 output->bfd_section->alignment_power = section->alignment_power;
962 /* If supplied an aligment, then force it. */
963 if (output->section_alignment != -1)
964 output->bfd_section->alignment_power = output->section_alignment;
968 /* Handle wildcard sorting. This returns the lang_input_section which
969 should follow the one we are going to create for SECTION and FILE,
970 based on the sorting requirements of WILD. It returns NULL if the
971 new section should just go at the end of the current list. */
973 static lang_statement_union_type *
974 wild_sort (wild, file, section)
975 lang_wild_statement_type *wild;
976 lang_input_statement_type *file;
977 asection *section;
979 const char *section_name;
980 lang_statement_union_type *l;
982 if (! wild->filenames_sorted && ! wild->sections_sorted)
983 return NULL;
985 section_name = bfd_get_section_name (file->the_bfd, section);
986 for (l = wild->children.head; l != NULL; l = l->next)
988 lang_input_section_type *ls;
990 if (l->header.type != lang_input_section_enum)
991 continue;
992 ls = &l->input_section;
994 /* Sorting by filename takes precedence over sorting by section
995 name. */
997 if (wild->filenames_sorted)
999 const char *fn, *ln;
1000 boolean fa, la;
1001 int i;
1003 /* The PE support for the .idata section as generated by
1004 dlltool assumes that files will be sorted by the name of
1005 the archive and then the name of the file within the
1006 archive. */
1008 if (file->the_bfd != NULL
1009 && bfd_my_archive (file->the_bfd) != NULL)
1011 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1012 fa = true;
1014 else
1016 fn = file->filename;
1017 fa = false;
1020 if (ls->ifile->the_bfd != NULL
1021 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1023 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1024 la = true;
1026 else
1028 ln = ls->ifile->filename;
1029 la = false;
1032 i = strcmp (fn, ln);
1033 if (i > 0)
1034 continue;
1035 else if (i < 0)
1036 break;
1038 if (fa || la)
1040 if (fa)
1041 fn = file->filename;
1042 if (la)
1043 ln = ls->ifile->filename;
1045 i = strcmp (fn, ln);
1046 if (i > 0)
1047 continue;
1048 else if (i < 0)
1049 break;
1053 /* Here either the files are not sorted by name, or we are
1054 looking at the sections for this file. */
1056 if (wild->sections_sorted)
1058 if (strcmp (section_name,
1059 bfd_get_section_name (ls->ifile->the_bfd,
1060 ls->section))
1061 < 0)
1062 break;
1066 return l;
1069 /* Expand a wild statement for a particular FILE. SECTION may be
1070 NULL, in which case it is a wild card. */
1072 static void
1073 wild_section (ptr, section, file, output)
1074 lang_wild_statement_type *ptr;
1075 const char *section;
1076 lang_input_statement_type *file;
1077 lang_output_section_statement_type *output;
1080 /* Don't process sections from files which were excluded. */
1081 if (ptr->exclude_filename != NULL)
1083 boolean match;
1085 if (wildcardp (ptr->exclude_filename))
1086 match = fnmatch (ptr->exclude_filename, file->filename, 0) == 0 ? true : false;
1087 else
1088 match = strcmp (ptr->exclude_filename, file->filename) == 0 ? true : false;
1090 if (match)
1091 return;
1094 if (file->just_syms_flag == false)
1096 register asection *s;
1097 boolean wildcard;
1099 if (section == NULL)
1100 wildcard = false;
1101 else
1102 wildcard = wildcardp (section);
1104 for (s = file->the_bfd->sections; s != NULL; s = s->next)
1106 boolean match;
1108 /* Attach all sections named SECTION. If SECTION is NULL,
1109 then attach all sections.
1111 Previously, if SECTION was NULL, this code did not call
1112 wild_doit if the SEC_IS_COMMON flag was set for the
1113 section. I did not understand that, and I took it out.
1114 --ian@cygnus.com. */
1116 if (section == NULL)
1117 match = true;
1118 else
1120 const char *name;
1122 name = bfd_get_section_name (file->the_bfd, s);
1123 if (wildcard)
1124 match = fnmatch (section, name, 0) == 0 ? true : false;
1125 else
1126 match = strcmp (section, name) == 0 ? true : false;
1129 if (match)
1131 lang_statement_union_type *before;
1133 /* If the wild pattern was marked KEEP, the member sections
1134 should be as well. */
1135 if (ptr->keep_sections)
1136 s->flags |= SEC_KEEP;
1138 before = wild_sort (ptr, file, s);
1140 /* Here BEFORE points to the lang_input_section which
1141 should follow the one we are about to add. If BEFORE
1142 is NULL, then the section should just go at the end
1143 of the current list. */
1145 if (before == NULL)
1146 wild_doit (&ptr->children, s, output, file);
1147 else
1149 lang_statement_list_type list;
1150 lang_statement_union_type **pp;
1152 lang_list_init (&list);
1153 wild_doit (&list, s, output, file);
1155 /* If we are discarding the section, LIST.HEAD will
1156 be NULL. */
1157 if (list.head != NULL)
1159 ASSERT (list.head->next == NULL);
1161 for (pp = &ptr->children.head;
1162 *pp != before;
1163 pp = &(*pp)->next)
1164 ASSERT (*pp != NULL);
1166 list.head->next = *pp;
1167 *pp = list.head;
1175 /* This is passed a file name which must have been seen already and
1176 added to the statement tree. We will see if it has been opened
1177 already and had its symbols read. If not then we'll read it. */
1179 static lang_input_statement_type *
1180 lookup_name (name)
1181 const char *name;
1183 lang_input_statement_type *search;
1185 for (search = (lang_input_statement_type *) input_file_chain.head;
1186 search != (lang_input_statement_type *) NULL;
1187 search = (lang_input_statement_type *) search->next_real_file)
1189 if (search->filename == (char *) NULL && name == (char *) NULL)
1190 return search;
1191 if (search->filename != (char *) NULL
1192 && name != (char *) NULL
1193 && strcmp (search->filename, name) == 0)
1194 break;
1197 if (search == (lang_input_statement_type *) NULL)
1198 search = new_afile (name, lang_input_file_is_file_enum, default_target,
1199 false);
1201 /* If we have already added this file, or this file is not real
1202 (FIXME: can that ever actually happen?) or the name is NULL
1203 (FIXME: can that ever actually happen?) don't add this file. */
1204 if (search->loaded
1205 || ! search->real
1206 || search->filename == (const char *) NULL)
1207 return search;
1209 load_symbols (search, (lang_statement_list_type *) NULL);
1211 return search;
1214 /* Get the symbols for an input file. */
1216 static void
1217 load_symbols (entry, place)
1218 lang_input_statement_type *entry;
1219 lang_statement_list_type *place;
1221 char **matching;
1223 if (entry->loaded)
1224 return;
1226 ldfile_open_file (entry);
1228 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1229 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1231 bfd_error_type err;
1232 lang_statement_list_type *hold;
1234 err = bfd_get_error ();
1235 if (err == bfd_error_file_ambiguously_recognized)
1237 char **p;
1239 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1240 einfo (_("%B: matching formats:"), entry->the_bfd);
1241 for (p = matching; *p != NULL; p++)
1242 einfo (" %s", *p);
1243 einfo ("%F\n");
1245 else if (err != bfd_error_file_not_recognized
1246 || place == NULL)
1247 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1249 bfd_close (entry->the_bfd);
1250 entry->the_bfd = NULL;
1252 /* See if the emulation has some special knowledge. */
1254 if (ldemul_unrecognized_file (entry))
1255 return;
1257 /* Try to interpret the file as a linker script. */
1259 ldfile_open_command_file (entry->filename);
1261 hold = stat_ptr;
1262 stat_ptr = place;
1264 ldfile_assumed_script = true;
1265 parser_input = input_script;
1266 yyparse ();
1267 ldfile_assumed_script = false;
1269 stat_ptr = hold;
1271 return;
1274 if (ldemul_recognized_file (entry))
1275 return;
1277 /* We don't call ldlang_add_file for an archive. Instead, the
1278 add_symbols entry point will call ldlang_add_file, via the
1279 add_archive_element callback, for each element of the archive
1280 which is used. */
1281 switch (bfd_get_format (entry->the_bfd))
1283 default:
1284 break;
1286 case bfd_object:
1287 ldlang_add_file (entry);
1288 if (trace_files || trace_file_tries)
1289 info_msg ("%I\n", entry);
1290 break;
1292 case bfd_archive:
1293 if (entry->whole_archive)
1295 bfd *member = bfd_openr_next_archived_file (entry->the_bfd,
1296 (bfd *) NULL);
1297 while (member != NULL)
1299 if (! bfd_check_format (member, bfd_object))
1300 einfo (_("%F%B: object %B in archive is not object\n"),
1301 entry->the_bfd, member);
1302 if (! ((*link_info.callbacks->add_archive_element)
1303 (&link_info, member, "--whole-archive")))
1304 abort ();
1305 if (! bfd_link_add_symbols (member, &link_info))
1306 einfo (_("%F%B: could not read symbols: %E\n"), member);
1307 member = bfd_openr_next_archived_file (entry->the_bfd,
1308 member);
1311 entry->loaded = true;
1313 return;
1317 if (! bfd_link_add_symbols (entry->the_bfd, &link_info))
1318 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1320 entry->loaded = true;
1323 /* Handle a wild statement for a single file F. */
1325 static void
1326 wild_file (s, section, f, output)
1327 lang_wild_statement_type *s;
1328 const char *section;
1329 lang_input_statement_type *f;
1330 lang_output_section_statement_type *output;
1332 if (f->the_bfd == NULL
1333 || ! bfd_check_format (f->the_bfd, bfd_archive))
1334 wild_section (s, section, f, output);
1335 else
1337 bfd *member;
1339 /* This is an archive file. We must map each member of the
1340 archive separately. */
1341 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
1342 while (member != NULL)
1344 /* When lookup_name is called, it will call the add_symbols
1345 entry point for the archive. For each element of the
1346 archive which is included, BFD will call ldlang_add_file,
1347 which will set the usrdata field of the member to the
1348 lang_input_statement. */
1349 if (member->usrdata != NULL)
1351 wild_section (s, section,
1352 (lang_input_statement_type *) member->usrdata,
1353 output);
1356 member = bfd_openr_next_archived_file (f->the_bfd, member);
1361 /* Handle a wild statement. SECTION or FILE or both may be NULL,
1362 indicating that it is a wildcard. Separate lang_input_section
1363 statements are created for each part of the expansion; they are
1364 added after the wild statement S. OUTPUT is the output section. */
1366 static void
1367 wild (s, section, file, target, output)
1368 lang_wild_statement_type *s;
1369 const char *section;
1370 const char *file;
1371 const char *target;
1372 lang_output_section_statement_type *output;
1374 lang_input_statement_type *f;
1376 if (file == (char *) NULL)
1378 /* Perform the iteration over all files in the list */
1379 for (f = (lang_input_statement_type *) file_chain.head;
1380 f != (lang_input_statement_type *) NULL;
1381 f = (lang_input_statement_type *) f->next)
1383 wild_file (s, section, f, output);
1386 else if (wildcardp (file))
1388 for (f = (lang_input_statement_type *) file_chain.head;
1389 f != (lang_input_statement_type *) NULL;
1390 f = (lang_input_statement_type *) f->next)
1392 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
1393 wild_file (s, section, f, output);
1396 else
1398 /* Perform the iteration over a single file */
1399 f = lookup_name (file);
1400 wild_file (s, section, f, output);
1403 if (section != (char *) NULL
1404 && strcmp (section, "COMMON") == 0
1405 && default_common_section == NULL)
1407 /* Remember the section that common is going to in case we later
1408 get something which doesn't know where to put it. */
1409 default_common_section = output;
1413 /* Open the output file. */
1415 static bfd *
1416 open_output (name)
1417 const char *name;
1419 bfd *output;
1421 if (output_target == (char *) NULL)
1423 if (current_target != (char *) NULL)
1424 output_target = current_target;
1425 else
1426 output_target = default_target;
1428 output = bfd_openw (name, output_target);
1430 if (output == (bfd *) NULL)
1432 if (bfd_get_error () == bfd_error_invalid_target)
1434 einfo (_("%P%F: target %s not found\n"), output_target);
1436 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1439 delete_output_file_on_failure = true;
1441 /* output->flags |= D_PAGED;*/
1443 if (! bfd_set_format (output, bfd_object))
1444 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1445 if (! bfd_set_arch_mach (output,
1446 ldfile_output_architecture,
1447 ldfile_output_machine))
1448 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1450 link_info.hash = bfd_link_hash_table_create (output);
1451 if (link_info.hash == (struct bfd_link_hash_table *) NULL)
1452 einfo (_("%P%F: can not create link hash table: %E\n"));
1454 bfd_set_gp_size (output, g_switch_value);
1455 return output;
1461 static void
1462 ldlang_open_output (statement)
1463 lang_statement_union_type * statement;
1465 switch (statement->header.type)
1467 case lang_output_statement_enum:
1468 ASSERT (output_bfd == (bfd *) NULL);
1469 output_bfd = open_output (statement->output_statement.name);
1470 ldemul_set_output_arch ();
1471 if (config.magic_demand_paged && !link_info.relocateable)
1472 output_bfd->flags |= D_PAGED;
1473 else
1474 output_bfd->flags &= ~D_PAGED;
1475 if (config.text_read_only)
1476 output_bfd->flags |= WP_TEXT;
1477 else
1478 output_bfd->flags &= ~WP_TEXT;
1479 if (link_info.traditional_format)
1480 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1481 else
1482 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1483 break;
1485 case lang_target_statement_enum:
1486 current_target = statement->target_statement.target;
1487 break;
1488 default:
1489 break;
1493 /* Open all the input files. */
1495 static void
1496 open_input_bfds (s, force)
1497 lang_statement_union_type *s;
1498 boolean force;
1500 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1502 switch (s->header.type)
1504 case lang_constructors_statement_enum:
1505 open_input_bfds (constructor_list.head, force);
1506 break;
1507 case lang_output_section_statement_enum:
1508 open_input_bfds (s->output_section_statement.children.head, force);
1509 break;
1510 case lang_wild_statement_enum:
1511 /* Maybe we should load the file's symbols */
1512 if (s->wild_statement.filename
1513 && ! wildcardp (s->wild_statement.filename))
1514 (void) lookup_name (s->wild_statement.filename);
1515 open_input_bfds (s->wild_statement.children.head, force);
1516 break;
1517 case lang_group_statement_enum:
1519 struct bfd_link_hash_entry *undefs;
1521 /* We must continually search the entries in the group
1522 until no new symbols are added to the list of undefined
1523 symbols. */
1527 undefs = link_info.hash->undefs_tail;
1528 open_input_bfds (s->group_statement.children.head, true);
1530 while (undefs != link_info.hash->undefs_tail);
1532 break;
1533 case lang_target_statement_enum:
1534 current_target = s->target_statement.target;
1535 break;
1536 case lang_input_statement_enum:
1537 if (s->input_statement.real == true)
1539 lang_statement_list_type add;
1541 s->input_statement.target = current_target;
1543 /* If we are being called from within a group, and this
1544 is an archive which has already been searched, then
1545 force it to be researched. */
1546 if (force
1547 && s->input_statement.loaded
1548 && bfd_check_format (s->input_statement.the_bfd,
1549 bfd_archive))
1550 s->input_statement.loaded = false;
1552 lang_list_init (&add);
1554 load_symbols (&s->input_statement, &add);
1556 if (add.head != NULL)
1558 *add.tail = s->next;
1559 s->next = add.head;
1562 break;
1563 default:
1564 break;
1569 /* If there are [COMMONS] statements, put a wild one into the bss section */
1571 static void
1572 lang_reasonable_defaults ()
1574 #if 0
1575 lang_output_section_statement_lookup (".text");
1576 lang_output_section_statement_lookup (".data");
1578 default_common_section =
1579 lang_output_section_statement_lookup (".bss");
1582 if (placed_commons == false)
1584 lang_wild_statement_type *new =
1585 new_stat (lang_wild_statement,
1586 &default_common_section->children);
1588 new->section_name = "COMMON";
1589 new->filename = (char *) NULL;
1590 lang_list_init (&new->children);
1592 #endif
1597 Add the supplied name to the symbol table as an undefined reference.
1598 Remove items from the chain as we open input bfds
1600 typedef struct ldlang_undef_chain_list
1602 struct ldlang_undef_chain_list *next;
1603 char *name;
1604 } ldlang_undef_chain_list_type;
1606 static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
1608 void
1609 ldlang_add_undef (name)
1610 CONST char *CONST name;
1612 ldlang_undef_chain_list_type *new =
1613 ((ldlang_undef_chain_list_type *)
1614 stat_alloc (sizeof (ldlang_undef_chain_list_type)));
1616 new->next = ldlang_undef_chain_list_head;
1617 ldlang_undef_chain_list_head = new;
1619 new->name = buystring (name);
1622 /* Run through the list of undefineds created above and place them
1623 into the linker hash table as undefined symbols belonging to the
1624 script file.
1626 static void
1627 lang_place_undefineds ()
1629 ldlang_undef_chain_list_type *ptr;
1631 for (ptr = ldlang_undef_chain_list_head;
1632 ptr != (ldlang_undef_chain_list_type *) NULL;
1633 ptr = ptr->next)
1635 struct bfd_link_hash_entry *h;
1637 h = bfd_link_hash_lookup (link_info.hash, ptr->name, true, false, true);
1638 if (h == (struct bfd_link_hash_entry *) NULL)
1639 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
1640 if (h->type == bfd_link_hash_new)
1642 h->type = bfd_link_hash_undefined;
1643 h->u.undef.abfd = NULL;
1644 bfd_link_add_undef (link_info.hash, h);
1649 /* Open input files and attatch to output sections */
1650 static void
1651 map_input_to_output_sections (s, target, output_section_statement)
1652 lang_statement_union_type * s;
1653 CONST char *target;
1654 lang_output_section_statement_type * output_section_statement;
1656 for (; s != (lang_statement_union_type *) NULL; s = s->next)
1658 switch (s->header.type)
1662 case lang_wild_statement_enum:
1663 wild (&s->wild_statement, s->wild_statement.section_name,
1664 s->wild_statement.filename, target,
1665 output_section_statement);
1667 break;
1668 case lang_constructors_statement_enum:
1669 map_input_to_output_sections (constructor_list.head,
1670 target,
1671 output_section_statement);
1672 break;
1673 case lang_output_section_statement_enum:
1674 map_input_to_output_sections (s->output_section_statement.children.head,
1675 target,
1676 &s->output_section_statement);
1677 break;
1678 case lang_output_statement_enum:
1679 break;
1680 case lang_target_statement_enum:
1681 target = s->target_statement.target;
1682 break;
1683 case lang_group_statement_enum:
1684 map_input_to_output_sections (s->group_statement.children.head,
1685 target,
1686 output_section_statement);
1687 break;
1688 case lang_fill_statement_enum:
1689 case lang_input_section_enum:
1690 case lang_object_symbols_statement_enum:
1691 case lang_data_statement_enum:
1692 case lang_reloc_statement_enum:
1693 case lang_padding_statement_enum:
1694 case lang_input_statement_enum:
1695 if (output_section_statement != NULL
1696 && output_section_statement->bfd_section == NULL)
1697 init_os (output_section_statement);
1698 break;
1699 case lang_assignment_statement_enum:
1700 if (output_section_statement != NULL
1701 && output_section_statement->bfd_section == NULL)
1702 init_os (output_section_statement);
1704 /* Make sure that any sections mentioned in the assignment
1705 are initialized. */
1706 exp_init_os (s->assignment_statement.exp);
1707 break;
1708 case lang_afile_asection_pair_statement_enum:
1709 FAIL ();
1710 break;
1711 case lang_address_statement_enum:
1712 /* Mark the specified section with the supplied address */
1714 lang_output_section_statement_type *os =
1715 lang_output_section_statement_lookup
1716 (s->address_statement.section_name);
1718 if (os->bfd_section == NULL)
1719 init_os (os);
1720 os->addr_tree = s->address_statement.address;
1722 break;
1727 static void
1728 print_output_section_statement (output_section_statement)
1729 lang_output_section_statement_type * output_section_statement;
1731 asection *section = output_section_statement->bfd_section;
1732 int len;
1734 if (output_section_statement != abs_output_section)
1736 minfo ("\n%s", output_section_statement->name);
1738 if (section != NULL)
1740 print_dot = section->vma;
1742 len = strlen (output_section_statement->name);
1743 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1745 print_nl ();
1746 len = 0;
1748 while (len < SECTION_NAME_MAP_LENGTH)
1750 print_space ();
1751 ++len;
1754 minfo ("0x%V %W", section->vma, section->_raw_size);
1756 if (output_section_statement->load_base != NULL)
1758 bfd_vma addr;
1760 addr = exp_get_abs_int (output_section_statement->load_base, 0,
1761 "load base", lang_final_phase_enum);
1762 minfo (_(" load address 0x%V"), addr);
1766 print_nl ();
1769 print_statement_list (output_section_statement->children.head,
1770 output_section_statement);
1773 static void
1774 print_assignment (assignment, output_section)
1775 lang_assignment_statement_type * assignment;
1776 lang_output_section_statement_type * output_section;
1778 int i;
1779 etree_value_type result;
1781 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1782 print_space ();
1784 result = exp_fold_tree (assignment->exp->assign.src, output_section,
1785 lang_final_phase_enum, print_dot, &print_dot);
1786 if (result.valid_p)
1787 minfo ("0x%V", result.value + result.section->bfd_section->vma);
1788 else
1790 minfo ("*undef* ");
1791 #ifdef BFD64
1792 minfo (" ");
1793 #endif
1796 minfo (" ");
1798 exp_print_tree (assignment->exp);
1800 print_nl ();
1803 static void
1804 print_input_statement (statm)
1805 lang_input_statement_type * statm;
1807 if (statm->filename != (char *) NULL)
1809 fprintf (config.map_file, "LOAD %s\n", statm->filename);
1813 /* Print all symbols defined in a particular section. This is called
1814 via bfd_link_hash_traverse. */
1816 static boolean
1817 print_one_symbol (hash_entry, ptr)
1818 struct bfd_link_hash_entry *hash_entry;
1819 PTR ptr;
1821 asection *sec = (asection *) ptr;
1823 if ((hash_entry->type == bfd_link_hash_defined
1824 || hash_entry->type == bfd_link_hash_defweak)
1825 && sec == hash_entry->u.def.section)
1827 int i;
1829 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1830 print_space ();
1831 minfo ("0x%V ",
1832 (hash_entry->u.def.value
1833 + hash_entry->u.def.section->output_offset
1834 + hash_entry->u.def.section->output_section->vma));
1836 minfo (" %T\n", hash_entry->root.string);
1839 return true;
1842 /* Print information about an input section to the map file. */
1844 static void
1845 print_input_section (in)
1846 lang_input_section_type * in;
1848 asection *i = in->section;
1849 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
1851 if (size != 0)
1853 print_space ();
1855 minfo ("%s", i->name);
1857 if (i->output_section != NULL)
1859 int len;
1861 len = 1 + strlen (i->name);
1862 if (len >= SECTION_NAME_MAP_LENGTH - 1)
1864 print_nl ();
1865 len = 0;
1867 while (len < SECTION_NAME_MAP_LENGTH)
1869 print_space ();
1870 ++len;
1873 minfo ("0x%V %W %B\n",
1874 i->output_section->vma + i->output_offset, size,
1875 i->owner);
1877 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
1879 len = SECTION_NAME_MAP_LENGTH + 3;
1880 #ifdef BFD64
1881 len += 16;
1882 #else
1883 len += 8;
1884 #endif
1885 while (len > 0)
1887 print_space ();
1888 --len;
1891 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
1894 bfd_link_hash_traverse (link_info.hash, print_one_symbol, (PTR) i);
1896 print_dot = i->output_section->vma + i->output_offset + size;
1901 static void
1902 print_fill_statement (fill)
1903 lang_fill_statement_type * fill;
1905 fprintf (config.map_file, " FILL mask 0x%x\n", fill->fill);
1908 static void
1909 print_data_statement (data)
1910 lang_data_statement_type * data;
1912 int i;
1913 bfd_vma addr;
1914 bfd_size_type size;
1915 const char *name;
1917 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1918 print_space ();
1920 addr = data->output_vma;
1921 if (data->output_section != NULL)
1922 addr += data->output_section->vma;
1924 switch (data->type)
1926 default:
1927 abort ();
1928 case BYTE:
1929 size = BYTE_SIZE;
1930 name = "BYTE";
1931 break;
1932 case SHORT:
1933 size = SHORT_SIZE;
1934 name = "SHORT";
1935 break;
1936 case LONG:
1937 size = LONG_SIZE;
1938 name = "LONG";
1939 break;
1940 case QUAD:
1941 size = QUAD_SIZE;
1942 name = "QUAD";
1943 break;
1944 case SQUAD:
1945 size = QUAD_SIZE;
1946 name = "SQUAD";
1947 break;
1950 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
1952 if (data->exp->type.node_class != etree_value)
1954 print_space ();
1955 exp_print_tree (data->exp);
1958 print_nl ();
1960 print_dot = addr + size;
1963 /* Print an address statement. These are generated by options like
1964 -Ttext. */
1966 static void
1967 print_address_statement (address)
1968 lang_address_statement_type *address;
1970 minfo (_("Address of section %s set to "), address->section_name);
1971 exp_print_tree (address->address);
1972 print_nl ();
1975 /* Print a reloc statement. */
1977 static void
1978 print_reloc_statement (reloc)
1979 lang_reloc_statement_type *reloc;
1981 int i;
1982 bfd_vma addr;
1983 bfd_size_type size;
1985 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1986 print_space ();
1988 addr = reloc->output_vma;
1989 if (reloc->output_section != NULL)
1990 addr += reloc->output_section->vma;
1992 size = bfd_get_reloc_size (reloc->howto);
1994 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
1996 if (reloc->name != NULL)
1997 minfo ("%s+", reloc->name);
1998 else
1999 minfo ("%s+", reloc->section->name);
2001 exp_print_tree (reloc->addend_exp);
2003 print_nl ();
2005 print_dot = addr + size;
2008 static void
2009 print_padding_statement (s)
2010 lang_padding_statement_type *s;
2012 int len;
2013 bfd_vma addr;
2015 minfo (" *fill*");
2017 len = sizeof " *fill*" - 1;
2018 while (len < SECTION_NAME_MAP_LENGTH)
2020 print_space ();
2021 ++len;
2024 addr = s->output_offset;
2025 if (s->output_section != NULL)
2026 addr += s->output_section->vma;
2027 minfo ("0x%V %W", addr, s->size);
2029 if (s->fill != 0)
2030 minfo (" %u", s->fill);
2032 print_nl ();
2034 print_dot = addr + s->size;
2037 static void
2038 print_wild_statement (w, os)
2039 lang_wild_statement_type * w;
2040 lang_output_section_statement_type * os;
2042 print_space ();
2044 if (w->filenames_sorted)
2045 minfo ("SORT(");
2046 if (w->exclude_filename != NULL)
2047 minfo ("EXCLUDE_FILE ( %s )", w->exclude_filename);
2048 if (w->filename != NULL)
2049 minfo ("%s", w->filename);
2050 else
2051 minfo ("*");
2052 if (w->filenames_sorted)
2053 minfo (")");
2055 minfo ("(");
2056 if (w->sections_sorted)
2057 minfo ("SORT(");
2058 if (w->section_name != NULL)
2059 minfo ("%s", w->section_name);
2060 else
2061 minfo ("*");
2062 if (w->sections_sorted)
2063 minfo (")");
2064 minfo (")");
2066 print_nl ();
2068 print_statement_list (w->children.head, os);
2071 /* Print a group statement. */
2073 static void
2074 print_group (s, os)
2075 lang_group_statement_type *s;
2076 lang_output_section_statement_type *os;
2078 fprintf (config.map_file, "START GROUP\n");
2079 print_statement_list (s->children.head, os);
2080 fprintf (config.map_file, "END GROUP\n");
2083 /* Print the list of statements in S.
2084 This can be called for any statement type. */
2086 static void
2087 print_statement_list (s, os)
2088 lang_statement_union_type *s;
2089 lang_output_section_statement_type *os;
2091 while (s != NULL)
2093 print_statement (s, os);
2094 s = s->next;
2098 /* Print the first statement in statement list S.
2099 This can be called for any statement type. */
2101 static void
2102 print_statement (s, os)
2103 lang_statement_union_type *s;
2104 lang_output_section_statement_type *os;
2106 switch (s->header.type)
2108 default:
2109 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2110 FAIL ();
2111 break;
2112 case lang_constructors_statement_enum:
2113 if (constructor_list.head != NULL)
2115 if (constructors_sorted)
2116 minfo (" SORT (CONSTRUCTORS)\n");
2117 else
2118 minfo (" CONSTRUCTORS\n");
2119 print_statement_list (constructor_list.head, os);
2121 break;
2122 case lang_wild_statement_enum:
2123 print_wild_statement (&s->wild_statement, os);
2124 break;
2125 case lang_address_statement_enum:
2126 print_address_statement (&s->address_statement);
2127 break;
2128 case lang_object_symbols_statement_enum:
2129 minfo (" CREATE_OBJECT_SYMBOLS\n");
2130 break;
2131 case lang_fill_statement_enum:
2132 print_fill_statement (&s->fill_statement);
2133 break;
2134 case lang_data_statement_enum:
2135 print_data_statement (&s->data_statement);
2136 break;
2137 case lang_reloc_statement_enum:
2138 print_reloc_statement (&s->reloc_statement);
2139 break;
2140 case lang_input_section_enum:
2141 print_input_section (&s->input_section);
2142 break;
2143 case lang_padding_statement_enum:
2144 print_padding_statement (&s->padding_statement);
2145 break;
2146 case lang_output_section_statement_enum:
2147 print_output_section_statement (&s->output_section_statement);
2148 break;
2149 case lang_assignment_statement_enum:
2150 print_assignment (&s->assignment_statement, os);
2151 break;
2152 case lang_target_statement_enum:
2153 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2154 break;
2155 case lang_output_statement_enum:
2156 minfo ("OUTPUT(%s", s->output_statement.name);
2157 if (output_target != NULL)
2158 minfo (" %s", output_target);
2159 minfo (")\n");
2160 break;
2161 case lang_input_statement_enum:
2162 print_input_statement (&s->input_statement);
2163 break;
2164 case lang_group_statement_enum:
2165 print_group (&s->group_statement, os);
2166 break;
2167 case lang_afile_asection_pair_statement_enum:
2168 FAIL ();
2169 break;
2173 static void
2174 print_statements ()
2176 print_statement_list (statement_list.head, abs_output_section);
2179 /* Print the first N statements in statement list S to STDERR.
2180 If N == 0, nothing is printed.
2181 If N < 0, the entire list is printed.
2182 Intended to be called from GDB. */
2184 void
2185 dprint_statement (s, n)
2186 lang_statement_union_type * s;
2187 int n;
2189 FILE *map_save = config.map_file;
2191 config.map_file = stderr;
2193 if (n < 0)
2194 print_statement_list (s, abs_output_section);
2195 else
2197 while (s && --n >= 0)
2199 print_statement (s, abs_output_section);
2200 s = s->next;
2204 config.map_file = map_save;
2207 static bfd_vma
2208 insert_pad (this_ptr, fill, power, output_section_statement, dot)
2209 lang_statement_union_type ** this_ptr;
2210 fill_type fill;
2211 unsigned int power;
2212 asection * output_section_statement;
2213 bfd_vma dot;
2215 /* Align this section first to the
2216 input sections requirement, then
2217 to the output section's requirement.
2218 If this alignment is > than any seen before,
2219 then record it too. Perform the alignment by
2220 inserting a magic 'padding' statement.
2223 unsigned int alignment_needed = align_power (dot, power) - dot;
2225 if (alignment_needed != 0)
2227 lang_statement_union_type *new =
2228 ((lang_statement_union_type *)
2229 stat_alloc (sizeof (lang_padding_statement_type)));
2231 /* Link into existing chain */
2232 new->header.next = *this_ptr;
2233 *this_ptr = new;
2234 new->header.type = lang_padding_statement_enum;
2235 new->padding_statement.output_section = output_section_statement;
2236 new->padding_statement.output_offset =
2237 dot - output_section_statement->vma;
2238 new->padding_statement.fill = fill;
2239 new->padding_statement.size = alignment_needed;
2243 /* Remember the most restrictive alignment */
2244 if (power > output_section_statement->alignment_power)
2246 output_section_statement->alignment_power = power;
2248 output_section_statement->_raw_size += alignment_needed;
2249 return alignment_needed + dot;
2253 /* Work out how much this section will move the dot point */
2254 static bfd_vma
2255 size_input_section (this_ptr, output_section_statement, fill, dot, relax)
2256 lang_statement_union_type ** this_ptr;
2257 lang_output_section_statement_type * output_section_statement;
2258 fill_type fill;
2259 bfd_vma dot;
2260 boolean relax;
2262 lang_input_section_type *is = &((*this_ptr)->input_section);
2263 asection *i = is->section;
2265 if (is->ifile->just_syms_flag == false)
2267 if (output_section_statement->subsection_alignment != -1)
2268 i->alignment_power =
2269 output_section_statement->subsection_alignment;
2271 dot = insert_pad (this_ptr, fill, i->alignment_power,
2272 output_section_statement->bfd_section, dot);
2274 /* Remember where in the output section this input section goes */
2276 i->output_offset = dot - output_section_statement->bfd_section->vma;
2278 /* Mark how big the output section must be to contain this now
2280 if (i->_cooked_size != 0)
2281 dot += i->_cooked_size;
2282 else
2283 dot += i->_raw_size;
2284 output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
2286 else
2288 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2291 return dot;
2294 /* Check to see if any allocated sections overlap with other allocated
2295 sections. This can happen when the linker script specifically specifies
2296 the output section addresses of the two sections. */
2297 static void
2298 lang_check_section_addresses ()
2300 asection * s;
2302 /* Scan all sections in the output list. */
2303 for (s = output_bfd->sections; s != NULL; s = s->next)
2304 /* Ignore sections which are not loaded or which have no contents. */
2305 if ((bfd_get_section_flags (output_bfd, s) & (SEC_ALLOC | SEC_LOAD))
2306 && bfd_section_size (output_bfd, s) != 0)
2308 asection * os;
2310 /* Once we reach section 's' stop our seach. This prevents two
2311 warning messages from being produced, one for 'section A overlaps
2312 section B' and one for 'section B overlaps section A'. */
2313 for (os = output_bfd->sections; os != s; os = os->next)
2315 bfd_vma s_start;
2316 bfd_vma s_end;
2317 bfd_vma os_start;
2318 bfd_vma os_end;
2320 /* Only consider loadable sections with real contents. */
2321 if (((bfd_get_section_flags (output_bfd, os)
2322 & (SEC_ALLOC | SEC_LOAD)) == 0)
2323 || bfd_section_size (output_bfd, os) == 0)
2324 continue;
2326 /* We must check the sections' LMA addresses not their
2327 VMA addresses because overlay sections can have
2328 overlapping VMAs but they must have distinct LMAs. */
2329 s_start = bfd_section_lma (output_bfd, s);
2330 os_start = bfd_section_lma (output_bfd, os);
2331 s_end = s_start + bfd_section_size (output_bfd, s) - 1;
2332 os_end = os_start + bfd_section_size (output_bfd, os) - 1;
2334 /* Look for an overlap. */
2335 if ((s_end < os_start) || (s_start > os_end))
2336 continue;
2338 einfo (
2339 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2340 s->name, s_start, s_end, os->name, os_start, os_end);
2342 /* Once we have found one overlap for this section,
2343 stop looking for others. */
2344 break;
2349 /* This variable indicates whether bfd_relax_section should be called
2350 again. */
2352 static boolean relax_again;
2354 /* Set the sizes for all the output sections. */
2356 bfd_vma
2357 lang_size_sections (s, output_section_statement, prev, fill, dot, relax)
2358 lang_statement_union_type * s;
2359 lang_output_section_statement_type * output_section_statement;
2360 lang_statement_union_type ** prev;
2361 fill_type fill;
2362 bfd_vma dot;
2363 boolean relax;
2365 /* Size up the sections from their constituent parts. */
2366 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2368 switch (s->header.type)
2370 case lang_output_section_statement_enum:
2372 bfd_vma after;
2373 lang_output_section_statement_type *os = &s->output_section_statement;
2375 if (os->bfd_section == NULL)
2376 /* This section was never actually created. */
2377 break;
2379 /* If this is a COFF shared library section, use the size and
2380 address from the input section. FIXME: This is COFF
2381 specific; it would be cleaner if there were some other way
2382 to do this, but nothing simple comes to mind. */
2383 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2385 asection * input;
2387 if (os->children.head == NULL
2388 || os->children.head->next != NULL
2389 || os->children.head->header.type != lang_input_section_enum)
2390 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2391 os->name);
2393 input = os->children.head->input_section.section;
2394 bfd_set_section_vma (os->bfd_section->owner,
2395 os->bfd_section,
2396 bfd_section_vma (input->owner, input));
2397 os->bfd_section->_raw_size = input->_raw_size;
2398 break;
2401 if (bfd_is_abs_section (os->bfd_section))
2403 /* No matter what happens, an abs section starts at zero. */
2404 ASSERT (os->bfd_section->vma == 0);
2406 else
2408 if (os->addr_tree == (etree_type *) NULL)
2410 /* No address specified for this section, get one
2411 from the region specification. */
2412 if (os->region == (lang_memory_region_type *) NULL
2413 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2414 & (SEC_ALLOC | SEC_LOAD)) != 0)
2415 && os->region->name[0] == '*'
2416 && strcmp (os->region->name, "*default*") == 0))
2418 os->region = lang_memory_default (os->bfd_section);
2421 /* If a loadable section is using the default memory
2422 region, and some non default memory regions were
2423 defined, issue a warning. */
2424 if ((bfd_get_section_flags (output_bfd, os->bfd_section)
2425 & (SEC_ALLOC | SEC_LOAD)) != 0
2426 && ! link_info.relocateable
2427 && strcmp (os->region->name, "*default*") == 0
2428 && lang_memory_region_list != NULL
2429 && (strcmp (lang_memory_region_list->name, "*default*") != 0
2430 || lang_memory_region_list->next != NULL))
2431 einfo (_("%P: warning: no memory region specified for section `%s'\n"),
2432 bfd_get_section_name (output_bfd, os->bfd_section));
2434 dot = os->region->current;
2436 if (os->section_alignment == -1)
2438 bfd_vma olddot;
2440 olddot = dot;
2441 dot = align_power (dot, os->bfd_section->alignment_power);
2443 if (dot != olddot && config.warn_section_align)
2444 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2445 os->name, (unsigned int) (dot - olddot));
2448 else
2450 etree_value_type r;
2452 r = exp_fold_tree (os->addr_tree,
2453 abs_output_section,
2454 lang_allocating_phase_enum,
2455 dot, &dot);
2456 if (r.valid_p == false)
2458 einfo (_("%F%S: non constant address expression for section %s\n"),
2459 os->name);
2461 dot = r.value + r.section->bfd_section->vma;
2464 /* The section starts here.
2465 First, align to what the section needs. */
2467 if (os->section_alignment != -1)
2468 dot = align_power (dot, os->section_alignment);
2470 bfd_set_section_vma (0, os->bfd_section, dot);
2472 os->bfd_section->output_offset = 0;
2475 (void) lang_size_sections (os->children.head, os, &os->children.head,
2476 os->fill, dot, relax);
2478 /* Ignore the size of the input sections, use the vma and size to
2479 align against. */
2481 after = ALIGN_N (os->bfd_section->vma +
2482 os->bfd_section->_raw_size,
2483 /* The coercion here is important, see ld.h. */
2484 (bfd_vma) os->block_value);
2486 if (bfd_is_abs_section (os->bfd_section))
2487 ASSERT (after == os->bfd_section->vma);
2488 else
2489 os->bfd_section->_raw_size = after - os->bfd_section->vma;
2490 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2491 os->processed = true;
2493 /* Update dot in the region ?
2494 We only do this if the section is going to be allocated,
2495 since unallocated sections do not contribute to the region's
2496 overall size in memory. */
2497 if (os->region != (lang_memory_region_type *) NULL
2498 && (bfd_get_section_flags (output_bfd, os->bfd_section)
2499 & (SEC_ALLOC | SEC_LOAD)))
2501 os->region->current = dot;
2503 /* Make sure this isn't silly. */
2504 if (os->region->current < os->region->origin
2505 || (os->region->current - os->region->origin
2506 > os->region->length))
2508 if (os->addr_tree != (etree_type *) NULL)
2510 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2511 os->region->current,
2512 os->bfd_section->owner,
2513 os->bfd_section->name,
2514 os->region->name);
2516 else
2518 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2519 os->region->name,
2520 os->bfd_section->owner,
2521 os->bfd_section->name);
2523 /* Reset the region pointer. */
2524 os->region->current = os->region->origin;
2528 break;
2530 case lang_constructors_statement_enum:
2531 dot = lang_size_sections (constructor_list.head,
2532 output_section_statement,
2533 &s->wild_statement.children.head,
2534 fill,
2535 dot, relax);
2536 break;
2538 case lang_data_statement_enum:
2540 unsigned int size = 0;
2542 s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
2543 s->data_statement.output_section =
2544 output_section_statement->bfd_section;
2546 switch (s->data_statement.type)
2548 case QUAD:
2549 case SQUAD:
2550 size = QUAD_SIZE;
2551 break;
2552 case LONG:
2553 size = LONG_SIZE;
2554 break;
2555 case SHORT:
2556 size = SHORT_SIZE;
2557 break;
2558 case BYTE:
2559 size = BYTE_SIZE;
2560 break;
2563 dot += size;
2564 output_section_statement->bfd_section->_raw_size += size;
2565 /* The output section gets contents, and then we inspect for
2566 any flags set in the input script which override any ALLOC. */
2567 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
2568 if (!(output_section_statement->flags & SEC_NEVER_LOAD)) {
2569 output_section_statement->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2572 break;
2574 case lang_reloc_statement_enum:
2576 int size;
2578 s->reloc_statement.output_vma =
2579 dot - output_section_statement->bfd_section->vma;
2580 s->reloc_statement.output_section =
2581 output_section_statement->bfd_section;
2582 size = bfd_get_reloc_size (s->reloc_statement.howto);
2583 dot += size;
2584 output_section_statement->bfd_section->_raw_size += size;
2586 break;
2588 case lang_wild_statement_enum:
2590 dot = lang_size_sections (s->wild_statement.children.head,
2591 output_section_statement,
2592 &s->wild_statement.children.head,
2594 fill, dot, relax);
2596 break;
2598 case lang_object_symbols_statement_enum:
2599 link_info.create_object_symbols_section =
2600 output_section_statement->bfd_section;
2601 break;
2602 case lang_output_statement_enum:
2603 case lang_target_statement_enum:
2604 break;
2605 case lang_input_section_enum:
2607 asection *i;
2609 i = (*prev)->input_section.section;
2610 if (! relax)
2612 if (i->_cooked_size == 0)
2613 i->_cooked_size = i->_raw_size;
2615 else
2617 boolean again;
2619 if (! bfd_relax_section (i->owner, i, &link_info, &again))
2620 einfo (_("%P%F: can't relax section: %E\n"));
2621 if (again)
2622 relax_again = true;
2624 dot = size_input_section (prev,
2625 output_section_statement,
2626 output_section_statement->fill,
2627 dot, relax);
2629 break;
2630 case lang_input_statement_enum:
2631 break;
2632 case lang_fill_statement_enum:
2633 s->fill_statement.output_section = output_section_statement->bfd_section;
2635 fill = s->fill_statement.fill;
2636 break;
2637 case lang_assignment_statement_enum:
2639 bfd_vma newdot = dot;
2641 exp_fold_tree (s->assignment_statement.exp,
2642 output_section_statement,
2643 lang_allocating_phase_enum,
2644 dot,
2645 &newdot);
2647 if (newdot != dot)
2649 /* The assignment changed dot. Insert a pad. */
2650 if (output_section_statement == abs_output_section)
2652 /* If we don't have an output section, then just adjust
2653 the default memory address. */
2654 lang_memory_region_lookup ("*default*")->current = newdot;
2656 else if (!relax)
2658 lang_statement_union_type *new =
2659 ((lang_statement_union_type *)
2660 stat_alloc (sizeof (lang_padding_statement_type)));
2662 /* Link into existing chain. */
2663 new->header.next = *prev;
2664 *prev = new;
2665 new->header.type = lang_padding_statement_enum;
2666 new->padding_statement.output_section =
2667 output_section_statement->bfd_section;
2668 new->padding_statement.output_offset =
2669 dot - output_section_statement->bfd_section->vma;
2670 new->padding_statement.fill = fill;
2671 new->padding_statement.size = newdot - dot;
2672 output_section_statement->bfd_section->_raw_size +=
2673 new->padding_statement.size;
2676 dot = newdot;
2679 break;
2681 case lang_padding_statement_enum:
2682 /* If we are relaxing, and this is not the first pass, some
2683 padding statements may have been inserted during previous
2684 passes. We may have to move the padding statement to a new
2685 location if dot has a different value at this point in this
2686 pass than it did at this point in the previous pass. */
2687 s->padding_statement.output_offset =
2688 dot - output_section_statement->bfd_section->vma;
2689 dot += s->padding_statement.size;
2690 output_section_statement->bfd_section->_raw_size +=
2691 s->padding_statement.size;
2692 break;
2694 case lang_group_statement_enum:
2695 dot = lang_size_sections (s->group_statement.children.head,
2696 output_section_statement,
2697 &s->group_statement.children.head,
2698 fill, dot, relax);
2699 break;
2701 default:
2702 FAIL ();
2703 break;
2705 /* This can only get here when relaxing is turned on. */
2707 case lang_address_statement_enum:
2708 break;
2710 prev = &s->header.next;
2712 return dot;
2715 bfd_vma
2716 lang_do_assignments (s, output_section_statement, fill, dot)
2717 lang_statement_union_type * s;
2718 lang_output_section_statement_type * output_section_statement;
2719 fill_type fill;
2720 bfd_vma dot;
2722 for (; s != (lang_statement_union_type *) NULL; s = s->next)
2724 switch (s->header.type)
2726 case lang_constructors_statement_enum:
2727 dot = lang_do_assignments (constructor_list.head,
2728 output_section_statement,
2729 fill,
2730 dot);
2731 break;
2733 case lang_output_section_statement_enum:
2735 lang_output_section_statement_type *os =
2736 &(s->output_section_statement);
2738 if (os->bfd_section != NULL)
2740 dot = os->bfd_section->vma;
2741 (void) lang_do_assignments (os->children.head, os,
2742 os->fill, dot);
2743 dot = os->bfd_section->vma + os->bfd_section->_raw_size;
2745 if (os->load_base)
2747 /* If nothing has been placed into the output section then
2748 it won't have a bfd_section. */
2749 if (os->bfd_section)
2751 os->bfd_section->lma
2752 = exp_get_abs_int(os->load_base, 0,"load base", lang_final_phase_enum);
2756 break;
2757 case lang_wild_statement_enum:
2759 dot = lang_do_assignments (s->wild_statement.children.head,
2760 output_section_statement,
2761 fill, dot);
2763 break;
2765 case lang_object_symbols_statement_enum:
2766 case lang_output_statement_enum:
2767 case lang_target_statement_enum:
2768 #if 0
2769 case lang_common_statement_enum:
2770 #endif
2771 break;
2772 case lang_data_statement_enum:
2774 etree_value_type value;
2776 value = exp_fold_tree (s->data_statement.exp,
2777 abs_output_section,
2778 lang_final_phase_enum, dot, &dot);
2779 s->data_statement.value = value.value;
2780 if (value.valid_p == false)
2781 einfo (_("%F%P: invalid data statement\n"));
2783 switch (s->data_statement.type)
2785 case QUAD:
2786 case SQUAD:
2787 dot += QUAD_SIZE;
2788 break;
2789 case LONG:
2790 dot += LONG_SIZE;
2791 break;
2792 case SHORT:
2793 dot += SHORT_SIZE;
2794 break;
2795 case BYTE:
2796 dot += BYTE_SIZE;
2797 break;
2799 break;
2801 case lang_reloc_statement_enum:
2803 etree_value_type value;
2805 value = exp_fold_tree (s->reloc_statement.addend_exp,
2806 abs_output_section,
2807 lang_final_phase_enum, dot, &dot);
2808 s->reloc_statement.addend_value = value.value;
2809 if (value.valid_p == false)
2810 einfo (_("%F%P: invalid reloc statement\n"));
2812 dot += bfd_get_reloc_size (s->reloc_statement.howto);
2813 break;
2815 case lang_input_section_enum:
2817 asection *in = s->input_section.section;
2819 if (in->_cooked_size != 0)
2820 dot += in->_cooked_size;
2821 else
2822 dot += in->_raw_size;
2824 break;
2826 case lang_input_statement_enum:
2827 break;
2828 case lang_fill_statement_enum:
2829 fill = s->fill_statement.fill;
2830 break;
2831 case lang_assignment_statement_enum:
2833 exp_fold_tree (s->assignment_statement.exp,
2834 output_section_statement,
2835 lang_final_phase_enum,
2836 dot,
2837 &dot);
2840 break;
2841 case lang_padding_statement_enum:
2842 dot += s->padding_statement.size;
2843 break;
2845 case lang_group_statement_enum:
2846 dot = lang_do_assignments (s->group_statement.children.head,
2847 output_section_statement,
2848 fill, dot);
2850 break;
2852 default:
2853 FAIL ();
2854 break;
2855 case lang_address_statement_enum:
2856 break;
2860 return dot;
2863 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
2864 operator .startof. (section_name), it produces an undefined symbol
2865 .startof.section_name. Similarly, when it sees
2866 .sizeof. (section_name), it produces an undefined symbol
2867 .sizeof.section_name. For all the output sections, we look for
2868 such symbols, and set them to the correct value. */
2870 static void
2871 lang_set_startof ()
2873 asection *s;
2875 if (link_info.relocateable)
2876 return;
2878 for (s = output_bfd->sections; s != NULL; s = s->next)
2880 const char *secname;
2881 char *buf;
2882 struct bfd_link_hash_entry *h;
2884 secname = bfd_get_section_name (output_bfd, s);
2885 buf = xmalloc (10 + strlen (secname));
2887 sprintf (buf, ".startof.%s", secname);
2888 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2889 if (h != NULL && h->type == bfd_link_hash_undefined)
2891 h->type = bfd_link_hash_defined;
2892 h->u.def.value = bfd_get_section_vma (output_bfd, s);
2893 h->u.def.section = bfd_abs_section_ptr;
2896 sprintf (buf, ".sizeof.%s", secname);
2897 h = bfd_link_hash_lookup (link_info.hash, buf, false, false, true);
2898 if (h != NULL && h->type == bfd_link_hash_undefined)
2900 h->type = bfd_link_hash_defined;
2901 if (s->_cooked_size != 0)
2902 h->u.def.value = s->_cooked_size;
2903 else
2904 h->u.def.value = s->_raw_size;
2905 h->u.def.section = bfd_abs_section_ptr;
2908 free (buf);
2912 static void
2913 lang_finish ()
2915 struct bfd_link_hash_entry *h;
2916 boolean warn;
2918 if (link_info.relocateable || link_info.shared)
2919 warn = false;
2920 else
2921 warn = true;
2923 if (entry_symbol == (char *) NULL)
2925 /* No entry has been specified. Look for start, but don't warn
2926 if we don't find it. */
2927 entry_symbol = "start";
2928 warn = false;
2931 h = bfd_link_hash_lookup (link_info.hash, entry_symbol, false, false, true);
2932 if (h != (struct bfd_link_hash_entry *) NULL
2933 && (h->type == bfd_link_hash_defined
2934 || h->type == bfd_link_hash_defweak)
2935 && h->u.def.section->output_section != NULL)
2937 bfd_vma val;
2939 val = (h->u.def.value
2940 + bfd_get_section_vma (output_bfd,
2941 h->u.def.section->output_section)
2942 + h->u.def.section->output_offset);
2943 if (! bfd_set_start_address (output_bfd, val))
2944 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol);
2946 else
2948 bfd_vma val;
2949 CONST char *send;
2951 /* We couldn't find the entry symbol. Try parsing it as a
2952 number. */
2953 val = bfd_scan_vma (entry_symbol, &send, 0);
2954 if (*send == '\0')
2956 if (! bfd_set_start_address (output_bfd, val))
2957 einfo (_("%P%F: can't set start address\n"));
2959 else
2961 asection *ts;
2963 /* Can't find the entry symbol, and it's not a number. Use
2964 the first address in the text section. */
2965 ts = bfd_get_section_by_name (output_bfd, ".text");
2966 if (ts != (asection *) NULL)
2968 if (warn)
2969 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
2970 entry_symbol, bfd_get_section_vma (output_bfd, ts));
2971 if (! bfd_set_start_address (output_bfd,
2972 bfd_get_section_vma (output_bfd,
2973 ts)))
2974 einfo (_("%P%F: can't set start address\n"));
2976 else
2978 if (warn)
2979 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
2980 entry_symbol);
2986 /* This is a small function used when we want to ignore errors from
2987 BFD. */
2989 static void
2990 #ifdef ANSI_PROTOTYPES
2991 ignore_bfd_errors (const char *s, ...)
2992 #else
2993 ignore_bfd_errors (s)
2994 const char *s;
2995 #endif
2997 /* Don't do anything. */
3000 /* Check that the architecture of all the input files is compatible
3001 with the output file. Also call the backend to let it do any
3002 other checking that is needed. */
3004 static void
3005 lang_check ()
3007 lang_statement_union_type *file;
3008 bfd *input_bfd;
3009 CONST bfd_arch_info_type *compatible;
3011 for (file = file_chain.head;
3012 file != (lang_statement_union_type *) NULL;
3013 file = file->input_statement.next)
3015 input_bfd = file->input_statement.the_bfd;
3016 compatible = bfd_arch_get_compatible (input_bfd,
3017 output_bfd);
3018 if (compatible == NULL)
3020 if (command_line.warn_mismatch)
3021 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3022 bfd_printable_name (input_bfd), input_bfd,
3023 bfd_printable_name (output_bfd));
3025 else
3027 bfd_error_handler_type pfn = NULL;
3029 /* If we aren't supposed to warn about mismatched input
3030 files, temporarily set the BFD error handler to a
3031 function which will do nothing. We still want to call
3032 bfd_merge_private_bfd_data, since it may set up
3033 information which is needed in the output file. */
3034 if (! command_line.warn_mismatch)
3035 pfn = bfd_set_error_handler (ignore_bfd_errors);
3036 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3038 if (command_line.warn_mismatch)
3039 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3040 input_bfd);
3042 if (! command_line.warn_mismatch)
3043 bfd_set_error_handler (pfn);
3048 /* Look through all the global common symbols and attach them to the
3049 correct section. The -sort-common command line switch may be used
3050 to roughly sort the entries by size. */
3052 static void
3053 lang_common ()
3055 if (link_info.relocateable
3056 && ! command_line.force_common_definition)
3057 return;
3059 if (! config.sort_common)
3060 bfd_link_hash_traverse (link_info.hash, lang_one_common, (PTR) NULL);
3061 else
3063 int power;
3065 for (power = 4; power >= 0; power--)
3066 bfd_link_hash_traverse (link_info.hash, lang_one_common,
3067 (PTR) &power);
3071 /* Place one common symbol in the correct section. */
3073 static boolean
3074 lang_one_common (h, info)
3075 struct bfd_link_hash_entry *h;
3076 PTR info;
3078 unsigned int power_of_two;
3079 bfd_vma size;
3080 asection *section;
3082 if (h->type != bfd_link_hash_common)
3083 return true;
3085 size = h->u.c.size;
3086 power_of_two = h->u.c.p->alignment_power;
3088 if (config.sort_common
3089 && power_of_two < (unsigned int) *(int *) info)
3090 return true;
3092 section = h->u.c.p->section;
3094 /* Increase the size of the section. */
3095 section->_cooked_size = ALIGN_N (section->_cooked_size,
3096 (bfd_size_type) (1 << power_of_two));
3098 /* Adjust the alignment if necessary. */
3099 if (power_of_two > section->alignment_power)
3100 section->alignment_power = power_of_two;
3102 /* Change the symbol from common to defined. */
3103 h->type = bfd_link_hash_defined;
3104 h->u.def.section = section;
3105 h->u.def.value = section->_cooked_size;
3107 /* Increase the size of the section. */
3108 section->_cooked_size += size;
3110 /* Make sure the section is allocated in memory, and make sure that
3111 it is no longer a common section. */
3112 section->flags |= SEC_ALLOC;
3113 section->flags &= ~ SEC_IS_COMMON;
3115 if (config.map_file != NULL)
3117 static boolean header_printed;
3118 int len;
3119 char *name;
3120 char buf[50];
3122 if (! header_printed)
3124 minfo (_("\nAllocating common symbols\n"));
3125 minfo (_("Common symbol size file\n\n"));
3126 header_printed = true;
3129 name = demangle (h->root.string);
3130 minfo ("%s", name);
3131 len = strlen (name);
3132 free (name);
3134 if (len >= 19)
3136 print_nl ();
3137 len = 0;
3139 while (len < 20)
3141 print_space ();
3142 ++len;
3145 minfo ("0x");
3146 if (size <= 0xffffffff)
3147 sprintf (buf, "%lx", (unsigned long) size);
3148 else
3149 sprintf_vma (buf, size);
3150 minfo ("%s", buf);
3151 len = strlen (buf);
3153 while (len < 16)
3155 print_space ();
3156 ++len;
3159 minfo ("%B\n", section->owner);
3162 return true;
3166 run through the input files and ensure that every input
3167 section has somewhere to go. If one is found without
3168 a destination then create an input request and place it
3169 into the statement tree.
3172 static void
3173 lang_place_orphans ()
3175 lang_input_statement_type *file;
3177 for (file = (lang_input_statement_type *) file_chain.head;
3178 file != (lang_input_statement_type *) NULL;
3179 file = (lang_input_statement_type *) file->next)
3181 asection *s;
3183 for (s = file->the_bfd->sections;
3184 s != (asection *) NULL;
3185 s = s->next)
3187 if (s->output_section == (asection *) NULL)
3189 /* This section of the file is not attatched, root
3190 around for a sensible place for it to go */
3192 if (file->just_syms_flag)
3194 /* We are only retrieving symbol values from this
3195 file. We want the symbols to act as though the
3196 values in the file are absolute. */
3197 s->output_section = bfd_abs_section_ptr;
3198 s->output_offset = s->vma;
3200 else if (strcmp (s->name, "COMMON") == 0)
3202 /* This is a lonely common section which must have
3203 come from an archive. We attach to the section
3204 with the wildcard. */
3205 if (! link_info.relocateable
3206 || command_line.force_common_definition)
3208 if (default_common_section == NULL)
3210 #if 0
3211 /* This message happens when using the
3212 svr3.ifile linker script, so I have
3213 disabled it. */
3214 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3215 #endif
3216 default_common_section =
3217 lang_output_section_statement_lookup (".bss");
3220 wild_doit (&default_common_section->children, s,
3221 default_common_section, file);
3224 else if (ldemul_place_orphan (file, s))
3226 else
3228 lang_output_section_statement_type *os =
3229 lang_output_section_statement_lookup (s->name);
3231 wild_doit (&os->children, s, os, file);
3239 void
3240 lang_set_flags (ptr, flags)
3241 lang_memory_region_type *ptr;
3242 CONST char *flags;
3244 flagword *ptr_flags = &ptr->flags;
3246 ptr->flags = ptr->not_flags = 0;
3247 while (*flags)
3249 switch (*flags)
3251 case '!':
3252 ptr_flags = (ptr_flags == &ptr->flags) ? &ptr->not_flags : &ptr->flags;
3253 break;
3255 case 'A': case 'a':
3256 *ptr_flags |= SEC_ALLOC;
3257 break;
3259 case 'R': case 'r':
3260 *ptr_flags |= SEC_READONLY;
3261 break;
3263 case 'W': case 'w':
3264 *ptr_flags |= SEC_DATA;
3265 break;
3267 case 'X': case 'x':
3268 *ptr_flags |= SEC_CODE;
3269 break;
3271 case 'L': case 'l':
3272 case 'I': case 'i':
3273 *ptr_flags |= SEC_LOAD;
3274 break;
3276 default:
3277 einfo (_("%P%F: invalid syntax in flags\n"));
3278 break;
3280 flags++;
3284 /* Call a function on each input file. This function will be called
3285 on an archive, but not on the elements. */
3287 void
3288 lang_for_each_input_file (func)
3289 void (*func) PARAMS ((lang_input_statement_type *));
3291 lang_input_statement_type *f;
3293 for (f = (lang_input_statement_type *) input_file_chain.head;
3294 f != NULL;
3295 f = (lang_input_statement_type *) f->next_real_file)
3296 func (f);
3299 /* Call a function on each file. The function will be called on all
3300 the elements of an archive which are included in the link, but will
3301 not be called on the archive file itself. */
3303 void
3304 lang_for_each_file (func)
3305 void (*func) PARAMS ((lang_input_statement_type *));
3307 lang_input_statement_type *f;
3309 for (f = (lang_input_statement_type *) file_chain.head;
3310 f != (lang_input_statement_type *) NULL;
3311 f = (lang_input_statement_type *) f->next)
3313 func (f);
3317 #if 0
3319 /* Not used. */
3321 void
3322 lang_for_each_input_section (func)
3323 void (*func) PARAMS ((bfd * ab, asection * as));
3325 lang_input_statement_type *f;
3327 for (f = (lang_input_statement_type *) file_chain.head;
3328 f != (lang_input_statement_type *) NULL;
3329 f = (lang_input_statement_type *) f->next)
3331 asection *s;
3333 for (s = f->the_bfd->sections;
3334 s != (asection *) NULL;
3335 s = s->next)
3337 func (f->the_bfd, s);
3342 #endif
3344 void
3345 ldlang_add_file (entry)
3346 lang_input_statement_type * entry;
3348 bfd **pp;
3350 lang_statement_append (&file_chain,
3351 (lang_statement_union_type *) entry,
3352 &entry->next);
3354 /* The BFD linker needs to have a list of all input BFDs involved in
3355 a link. */
3356 ASSERT (entry->the_bfd->link_next == (bfd *) NULL);
3357 ASSERT (entry->the_bfd != output_bfd);
3358 for (pp = &link_info.input_bfds;
3359 *pp != (bfd *) NULL;
3360 pp = &(*pp)->link_next)
3362 *pp = entry->the_bfd;
3363 entry->the_bfd->usrdata = (PTR) entry;
3364 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3366 /* Look through the sections and check for any which should not be
3367 included in the link. We need to do this now, so that we can
3368 notice when the backend linker tries to report multiple
3369 definition errors for symbols which are in sections we aren't
3370 going to link. FIXME: It might be better to entirely ignore
3371 symbols which are defined in sections which are going to be
3372 discarded. This would require modifying the backend linker for
3373 each backend which might set the SEC_LINK_ONCE flag. If we do
3374 this, we should probably handle SEC_EXCLUDE in the same way. */
3376 bfd_map_over_sections (entry->the_bfd, section_already_linked, (PTR) entry);
3379 void
3380 lang_add_output (name, from_script)
3381 CONST char *name;
3382 int from_script;
3384 /* Make -o on command line override OUTPUT in script. */
3385 if (had_output_filename == false || !from_script)
3387 output_filename = name;
3388 had_output_filename = true;
3393 static lang_output_section_statement_type *current_section;
3395 static int
3396 topower (x)
3397 int x;
3399 unsigned int i = 1;
3400 int l;
3402 if (x < 0)
3403 return -1;
3405 for (l = 0; l < 32; l++)
3407 if (i >= (unsigned int) x)
3408 return l;
3409 i <<= 1;
3412 return 0;
3415 void
3416 lang_enter_output_section_statement (output_section_statement_name,
3417 address_exp, sectype, block_value,
3418 align, subalign, ebase)
3419 const char *output_section_statement_name;
3420 etree_type * address_exp;
3421 enum section_type sectype;
3422 bfd_vma block_value;
3423 etree_type *align;
3424 etree_type *subalign;
3425 etree_type *ebase;
3427 lang_output_section_statement_type *os;
3429 current_section =
3430 os =
3431 lang_output_section_statement_lookup (output_section_statement_name);
3435 /* Add this statement to tree */
3436 /* add_statement(lang_output_section_statement_enum,
3437 output_section_statement);*/
3438 /* Make next things chain into subchain of this */
3440 if (os->addr_tree ==
3441 (etree_type *) NULL)
3443 os->addr_tree =
3444 address_exp;
3446 os->sectype = sectype;
3447 if (sectype != noload_section)
3448 os->flags = SEC_NO_FLAGS;
3449 else
3450 os->flags = SEC_NEVER_LOAD;
3451 os->block_value = block_value ? block_value : 1;
3452 stat_ptr = &os->children;
3454 os->subsection_alignment = topower(
3455 exp_get_value_int(subalign, -1,
3456 "subsection alignment",
3457 0));
3458 os->section_alignment = topower(
3459 exp_get_value_int(align, -1,
3460 "section alignment", 0));
3462 os->load_base = ebase;
3466 void
3467 lang_final ()
3469 lang_output_statement_type *new =
3470 new_stat (lang_output_statement, stat_ptr);
3472 new->name = output_filename;
3475 /* Reset the current counters in the regions */
3476 static void
3477 reset_memory_regions ()
3479 lang_memory_region_type *p = lang_memory_region_list;
3481 for (p = lang_memory_region_list;
3482 p != (lang_memory_region_type *) NULL;
3483 p = p->next)
3485 p->old_length = (bfd_size_type) (p->current - p->origin);
3486 p->current = p->origin;
3490 /* ??? At some point this traversal for GC should share code with the
3491 traversal for manipulating the output file. */
3493 /* Expand a wild statement for a particular FILE, marking its sections KEEP
3494 as needed. SECTION may be NULL, in which case it is a wild card. */
3496 static void
3497 lang_gc_wild_section (ptr, section, file)
3498 lang_wild_statement_type *ptr;
3499 const char *section;
3500 lang_input_statement_type *file;
3502 if (file->just_syms_flag == false)
3504 register asection *s;
3505 boolean wildcard;
3507 if (section == NULL)
3508 wildcard = false;
3509 else
3510 wildcard = wildcardp (section);
3512 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3514 boolean match;
3516 if (section == NULL)
3517 match = true;
3518 else
3520 const char *name;
3522 name = bfd_get_section_name (file->the_bfd, s);
3523 if (wildcard)
3524 match = fnmatch (section, name, 0) == 0 ? true : false;
3525 else
3526 match = strcmp (section, name) == 0 ? true : false;
3529 if (match)
3531 /* If the wild pattern was marked KEEP, the member sections
3532 should be as well. */
3533 if (ptr->keep_sections)
3534 s->flags |= SEC_KEEP;
3540 /* Handle a wild statement for a single file F. */
3542 static void
3543 lang_gc_wild_file (s, section, f)
3544 lang_wild_statement_type *s;
3545 const char *section;
3546 lang_input_statement_type *f;
3548 if (f->the_bfd == NULL
3549 || ! bfd_check_format (f->the_bfd, bfd_archive))
3550 lang_gc_wild_section (s, section, f);
3551 else
3553 bfd *member;
3555 /* This is an archive file. We must map each member of the
3556 archive separately. */
3557 member = bfd_openr_next_archived_file (f->the_bfd, (bfd *) NULL);
3558 while (member != NULL)
3560 /* When lookup_name is called, it will call the add_symbols
3561 entry point for the archive. For each element of the
3562 archive which is included, BFD will call ldlang_add_file,
3563 which will set the usrdata field of the member to the
3564 lang_input_statement. */
3565 if (member->usrdata != NULL)
3567 lang_gc_wild_section (s, section,
3568 (lang_input_statement_type *) member->usrdata);
3571 member = bfd_openr_next_archived_file (f->the_bfd, member);
3576 /* Handle a wild statement, marking it against GC. SECTION or FILE or both
3577 may be NULL, indicating that it is a wildcard. */
3579 static void
3580 lang_gc_wild (s, section, file)
3581 lang_wild_statement_type *s;
3582 const char *section;
3583 const char *file;
3585 lang_input_statement_type *f;
3587 if (file == (char *) NULL)
3589 /* Perform the iteration over all files in the list */
3590 for (f = (lang_input_statement_type *) file_chain.head;
3591 f != (lang_input_statement_type *) NULL;
3592 f = (lang_input_statement_type *) f->next)
3594 lang_gc_wild_file (s, section, f);
3597 else if (wildcardp (file))
3599 for (f = (lang_input_statement_type *) file_chain.head;
3600 f != (lang_input_statement_type *) NULL;
3601 f = (lang_input_statement_type *) f->next)
3603 if (fnmatch (file, f->filename, FNM_FILE_NAME) == 0)
3604 lang_gc_wild_file (s, section, f);
3607 else
3609 /* Perform the iteration over a single file */
3610 f = lookup_name (file);
3611 lang_gc_wild_file (s, section, f);
3615 /* Iterate over sections marking them against GC. */
3617 static void
3618 lang_gc_sections_1 (s)
3619 lang_statement_union_type * s;
3621 for (; s != (lang_statement_union_type *) NULL; s = s->next)
3623 switch (s->header.type)
3625 case lang_wild_statement_enum:
3626 lang_gc_wild (&s->wild_statement,
3627 s->wild_statement.section_name,
3628 s->wild_statement.filename);
3629 break;
3630 case lang_constructors_statement_enum:
3631 lang_gc_sections_1 (constructor_list.head);
3632 break;
3633 case lang_output_section_statement_enum:
3634 lang_gc_sections_1 (s->output_section_statement.children.head);
3635 break;
3636 case lang_group_statement_enum:
3637 lang_gc_sections_1 (s->group_statement.children.head);
3638 break;
3639 default:
3640 break;
3645 static void
3646 lang_gc_sections ()
3648 struct bfd_link_hash_entry *h;
3649 ldlang_undef_chain_list_type *ulist, fake_list_start;
3651 /* Keep all sections so marked in the link script. */
3653 lang_gc_sections_1 (statement_list.head);
3655 /* Keep all sections containing symbols undefined on the command-line.
3656 Handle the entry symbol at the same time. */
3658 fake_list_start.next = ldlang_undef_chain_list_head;
3659 if (entry_symbol == NULL)
3660 fake_list_start.name = "start";
3661 else
3662 fake_list_start.name = (char *) entry_symbol;
3664 for (ulist = &fake_list_start; ulist; ulist = ulist->next)
3666 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
3667 false, false, false);
3669 if (h != (struct bfd_link_hash_entry *) NULL
3670 && (h->type == bfd_link_hash_defined
3671 || h->type == bfd_link_hash_defweak)
3672 && ! bfd_is_abs_section (h->u.def.section))
3674 h->u.def.section->flags |= SEC_KEEP;
3678 bfd_gc_sections (output_bfd, &link_info);
3681 void
3682 lang_process ()
3684 lang_reasonable_defaults ();
3685 current_target = default_target;
3687 lang_for_each_statement (ldlang_open_output); /* Open the output file */
3689 ldemul_create_output_section_statements ();
3691 /* Add to the hash table all undefineds on the command line */
3692 lang_place_undefineds ();
3694 /* Create a bfd for each input file */
3695 current_target = default_target;
3696 open_input_bfds (statement_list.head, false);
3698 ldemul_after_open ();
3700 /* Make sure that we're not mixing architectures. We call this
3701 after all the input files have been opened, but before we do any
3702 other processing, so that any operations merge_private_bfd_data
3703 does on the output file will be known during the rest of the
3704 link. */
3705 lang_check ();
3707 /* Handle .exports instead of a version script if we're told to do so. */
3708 if (command_line.version_exports_section)
3709 lang_do_version_exports_section ();
3711 /* Build all sets based on the information gathered from the input
3712 files. */
3713 ldctor_build_sets ();
3715 /* Remove unreferenced sections if asked to. */
3716 if (command_line.gc_sections)
3717 lang_gc_sections ();
3719 /* Size up the common data */
3720 lang_common ();
3722 /* Run through the contours of the script and attach input sections
3723 to the correct output sections
3725 map_input_to_output_sections (statement_list.head, (char *) NULL,
3726 (lang_output_section_statement_type *) NULL);
3729 /* Find any sections not attached explicitly and handle them */
3730 lang_place_orphans ();
3732 ldemul_before_allocation ();
3734 /* We must record the program headers before we try to fix the
3735 section positions, since they will affect SIZEOF_HEADERS. */
3736 lang_record_phdrs ();
3738 /* Now run around and relax if we can */
3739 if (command_line.relax)
3741 /* First time round is a trial run to get the 'worst case'
3742 addresses of the objects if there was no relaxing. */
3743 lang_size_sections (statement_list.head,
3744 abs_output_section,
3745 &(statement_list.head), 0, (bfd_vma) 0, false);
3747 /* Keep relaxing until bfd_relax_section gives up. */
3750 reset_memory_regions ();
3752 relax_again = false;
3754 /* Note: pe-dll.c does something like this also. If you find
3755 you need to change this code, you probably need to change
3756 pe-dll.c also. DJ */
3758 /* Do all the assignments with our current guesses as to
3759 section sizes. */
3760 lang_do_assignments (statement_list.head,
3761 abs_output_section,
3762 (fill_type) 0, (bfd_vma) 0);
3764 /* Perform another relax pass - this time we know where the
3765 globals are, so can make better guess. */
3766 lang_size_sections (statement_list.head,
3767 abs_output_section,
3768 &(statement_list.head), 0, (bfd_vma) 0, true);
3770 while (relax_again);
3772 else
3774 /* Size up the sections. */
3775 lang_size_sections (statement_list.head,
3776 abs_output_section,
3777 &(statement_list.head), 0, (bfd_vma) 0, false);
3780 /* See if anything special should be done now we know how big
3781 everything is. */
3782 ldemul_after_allocation ();
3784 /* Fix any .startof. or .sizeof. symbols. */
3785 lang_set_startof ();
3787 /* Do all the assignments, now that we know the final restingplaces
3788 of all the symbols */
3790 lang_do_assignments (statement_list.head,
3791 abs_output_section,
3792 (fill_type) 0, (bfd_vma) 0);
3794 /* Make sure that the section addresses make sense. */
3795 if (! link_info.relocateable
3796 && command_line.check_section_addresses)
3797 lang_check_section_addresses ();
3799 /* Final stuffs */
3801 ldemul_finish ();
3802 lang_finish ();
3805 /* EXPORTED TO YACC */
3807 void
3808 lang_add_wild (section_name, sections_sorted, filename, filenames_sorted,
3809 keep_sections, exclude_filename)
3810 const char *const section_name;
3811 boolean sections_sorted;
3812 const char *const filename;
3813 boolean filenames_sorted;
3814 boolean keep_sections;
3815 const char *exclude_filename;
3817 lang_wild_statement_type *new = new_stat (lang_wild_statement,
3818 stat_ptr);
3820 if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
3822 placed_commons = true;
3824 if (filename != NULL && ! wildcardp (filename))
3826 lang_has_input_file = true;
3828 new->section_name = section_name;
3829 new->sections_sorted = sections_sorted;
3830 new->filename = filename;
3831 new->filenames_sorted = filenames_sorted;
3832 new->keep_sections = keep_sections;
3833 new->exclude_filename = exclude_filename;
3834 lang_list_init (&new->children);
3837 void
3838 lang_section_start (name, address)
3839 CONST char *name;
3840 etree_type * address;
3842 lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
3844 ad->section_name = name;
3845 ad->address = address;
3848 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
3849 because of a -e argument on the command line, or zero if this is
3850 called by ENTRY in a linker script. Command line arguments take
3851 precedence. */
3853 void
3854 lang_add_entry (name, cmdline)
3855 CONST char *name;
3856 boolean cmdline;
3858 if (entry_symbol == NULL
3859 || cmdline
3860 || ! entry_from_cmdline)
3862 entry_symbol = name;
3863 entry_from_cmdline = cmdline;
3867 void
3868 lang_add_target (name)
3869 CONST char *name;
3871 lang_target_statement_type *new = new_stat (lang_target_statement,
3872 stat_ptr);
3874 new->target = name;
3878 void
3879 lang_add_map (name)
3880 CONST char *name;
3882 while (*name)
3884 switch (*name)
3886 case 'F':
3887 map_option_f = true;
3888 break;
3890 name++;
3894 void
3895 lang_add_fill (exp)
3896 int exp;
3898 lang_fill_statement_type *new = new_stat (lang_fill_statement,
3899 stat_ptr);
3901 new->fill = exp;
3904 void
3905 lang_add_data (type, exp)
3906 int type;
3907 union etree_union *exp;
3910 lang_data_statement_type *new = new_stat (lang_data_statement,
3911 stat_ptr);
3913 new->exp = exp;
3914 new->type = type;
3918 /* Create a new reloc statement. RELOC is the BFD relocation type to
3919 generate. HOWTO is the corresponding howto structure (we could
3920 look this up, but the caller has already done so). SECTION is the
3921 section to generate a reloc against, or NAME is the name of the
3922 symbol to generate a reloc against. Exactly one of SECTION and
3923 NAME must be NULL. ADDEND is an expression for the addend. */
3925 void
3926 lang_add_reloc (reloc, howto, section, name, addend)
3927 bfd_reloc_code_real_type reloc;
3928 reloc_howto_type *howto;
3929 asection *section;
3930 const char *name;
3931 union etree_union *addend;
3933 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
3935 p->reloc = reloc;
3936 p->howto = howto;
3937 p->section = section;
3938 p->name = name;
3939 p->addend_exp = addend;
3941 p->addend_value = 0;
3942 p->output_section = NULL;
3943 p->output_vma = 0;
3946 lang_assignment_statement_type *
3947 lang_add_assignment (exp)
3948 etree_type * exp;
3950 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
3951 stat_ptr);
3953 new->exp = exp;
3954 return new;
3957 void
3958 lang_add_attribute (attribute)
3959 enum statement_enum attribute;
3961 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
3964 void
3965 lang_startup (name)
3966 CONST char *name;
3968 if (startup_file != (char *) NULL)
3970 einfo (_("%P%Fmultiple STARTUP files\n"));
3972 first_file->filename = name;
3973 first_file->local_sym_name = name;
3974 first_file->real = true;
3976 startup_file = name;
3979 void
3980 lang_float (maybe)
3981 boolean maybe;
3983 lang_float_flag = maybe;
3986 void
3987 lang_leave_output_section_statement (fill, memspec, phdrs)
3988 bfd_vma fill;
3989 const char *memspec;
3990 struct lang_output_section_phdr_list *phdrs;
3992 current_section->fill = fill;
3993 current_section->region = lang_memory_region_lookup (memspec);
3994 current_section->phdrs = phdrs;
3995 stat_ptr = &statement_list;
3999 Create an absolute symbol with the given name with the value of the
4000 address of first byte of the section named.
4002 If the symbol already exists, then do nothing.
4004 void
4005 lang_abs_symbol_at_beginning_of (secname, name)
4006 const char *secname;
4007 const char *name;
4009 struct bfd_link_hash_entry *h;
4011 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4012 if (h == (struct bfd_link_hash_entry *) NULL)
4013 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4015 if (h->type == bfd_link_hash_new
4016 || h->type == bfd_link_hash_undefined)
4018 asection *sec;
4020 h->type = bfd_link_hash_defined;
4022 sec = bfd_get_section_by_name (output_bfd, secname);
4023 if (sec == (asection *) NULL)
4024 h->u.def.value = 0;
4025 else
4026 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4028 h->u.def.section = bfd_abs_section_ptr;
4033 Create an absolute symbol with the given name with the value of the
4034 address of the first byte after the end of the section named.
4036 If the symbol already exists, then do nothing.
4038 void
4039 lang_abs_symbol_at_end_of (secname, name)
4040 const char *secname;
4041 const char *name;
4043 struct bfd_link_hash_entry *h;
4045 h = bfd_link_hash_lookup (link_info.hash, name, true, true, true);
4046 if (h == (struct bfd_link_hash_entry *) NULL)
4047 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4049 if (h->type == bfd_link_hash_new
4050 || h->type == bfd_link_hash_undefined)
4052 asection *sec;
4054 h->type = bfd_link_hash_defined;
4056 sec = bfd_get_section_by_name (output_bfd, secname);
4057 if (sec == (asection *) NULL)
4058 h->u.def.value = 0;
4059 else
4060 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4061 + bfd_section_size (output_bfd, sec));
4063 h->u.def.section = bfd_abs_section_ptr;
4067 void
4068 lang_statement_append (list, element, field)
4069 lang_statement_list_type * list;
4070 lang_statement_union_type * element;
4071 lang_statement_union_type ** field;
4073 *(list->tail) = element;
4074 list->tail = field;
4077 /* Set the output format type. -oformat overrides scripts. */
4079 void
4080 lang_add_output_format (format, big, little, from_script)
4081 const char *format;
4082 const char *big;
4083 const char *little;
4084 int from_script;
4086 if (output_target == NULL || !from_script)
4088 if (command_line.endian == ENDIAN_BIG
4089 && big != NULL)
4090 format = big;
4091 else if (command_line.endian == ENDIAN_LITTLE
4092 && little != NULL)
4093 format = little;
4095 output_target = format;
4099 /* Enter a group. This creates a new lang_group_statement, and sets
4100 stat_ptr to build new statements within the group. */
4102 void
4103 lang_enter_group ()
4105 lang_group_statement_type *g;
4107 g = new_stat (lang_group_statement, stat_ptr);
4108 lang_list_init (&g->children);
4109 stat_ptr = &g->children;
4112 /* Leave a group. This just resets stat_ptr to start writing to the
4113 regular list of statements again. Note that this will not work if
4114 groups can occur inside anything else which can adjust stat_ptr,
4115 but currently they can't. */
4117 void
4118 lang_leave_group ()
4120 stat_ptr = &statement_list;
4123 /* Add a new program header. This is called for each entry in a PHDRS
4124 command in a linker script. */
4126 void
4127 lang_new_phdr (name, type, filehdr, phdrs, at, flags)
4128 const char *name;
4129 etree_type *type;
4130 boolean filehdr;
4131 boolean phdrs;
4132 etree_type *at;
4133 etree_type *flags;
4135 struct lang_phdr *n, **pp;
4137 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
4138 n->next = NULL;
4139 n->name = name;
4140 n->type = exp_get_value_int (type, 0, "program header type",
4141 lang_final_phase_enum);
4142 n->filehdr = filehdr;
4143 n->phdrs = phdrs;
4144 n->at = at;
4145 n->flags = flags;
4147 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4149 *pp = n;
4152 /* Record the program header information in the output BFD. FIXME: We
4153 should not be calling an ELF specific function here. */
4155 static void
4156 lang_record_phdrs ()
4158 unsigned int alc;
4159 asection **secs;
4160 struct lang_output_section_phdr_list *last;
4161 struct lang_phdr *l;
4162 lang_statement_union_type *u;
4164 alc = 10;
4165 secs = (asection **) xmalloc (alc * sizeof (asection *));
4166 last = NULL;
4167 for (l = lang_phdr_list; l != NULL; l = l->next)
4169 unsigned int c;
4170 flagword flags;
4171 bfd_vma at;
4173 c = 0;
4174 for (u = lang_output_section_statement.head;
4175 u != NULL;
4176 u = u->output_section_statement.next)
4178 lang_output_section_statement_type *os;
4179 struct lang_output_section_phdr_list *pl;
4181 os = &u->output_section_statement;
4183 pl = os->phdrs;
4184 if (pl != NULL)
4185 last = pl;
4186 else
4188 if (os->sectype == noload_section
4189 || os->bfd_section == NULL
4190 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4191 continue;
4192 pl = last;
4195 if (os->bfd_section == NULL)
4196 continue;
4198 for (; pl != NULL; pl = pl->next)
4200 if (strcmp (pl->name, l->name) == 0)
4202 if (c >= alc)
4204 alc *= 2;
4205 secs = ((asection **)
4206 xrealloc (secs, alc * sizeof (asection *)));
4208 secs[c] = os->bfd_section;
4209 ++c;
4210 pl->used = true;
4215 if (l->flags == NULL)
4216 flags = 0;
4217 else
4218 flags = exp_get_vma (l->flags, 0, "phdr flags",
4219 lang_final_phase_enum);
4221 if (l->at == NULL)
4222 at = 0;
4223 else
4224 at = exp_get_vma (l->at, 0, "phdr load address",
4225 lang_final_phase_enum);
4227 if (! bfd_record_phdr (output_bfd, l->type,
4228 l->flags == NULL ? false : true,
4229 flags,
4230 l->at == NULL ? false : true,
4231 at, l->filehdr, l->phdrs, c, secs))
4232 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4235 free (secs);
4237 /* Make sure all the phdr assignments succeeded. */
4238 for (u = lang_output_section_statement.head;
4239 u != NULL;
4240 u = u->output_section_statement.next)
4242 struct lang_output_section_phdr_list *pl;
4244 if (u->output_section_statement.bfd_section == NULL)
4245 continue;
4247 for (pl = u->output_section_statement.phdrs;
4248 pl != NULL;
4249 pl = pl->next)
4250 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4251 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4252 u->output_section_statement.name, pl->name);
4256 /* Record a list of sections which may not be cross referenced. */
4258 void
4259 lang_add_nocrossref (l)
4260 struct lang_nocrossref *l;
4262 struct lang_nocrossrefs *n;
4264 n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
4265 n->next = nocrossref_list;
4266 n->list = l;
4267 nocrossref_list = n;
4269 /* Set notice_all so that we get informed about all symbols. */
4270 link_info.notice_all = true;
4273 /* Overlay handling. We handle overlays with some static variables. */
4275 /* The overlay virtual address. */
4276 static etree_type *overlay_vma;
4278 /* The overlay load address. */
4279 static etree_type *overlay_lma;
4281 /* Whether nocrossrefs is set for this overlay. */
4282 static int overlay_nocrossrefs;
4284 /* An expression for the maximum section size seen so far. */
4285 static etree_type *overlay_max;
4287 /* A list of all the sections in this overlay. */
4289 struct overlay_list
4291 struct overlay_list *next;
4292 lang_output_section_statement_type *os;
4295 static struct overlay_list *overlay_list;
4297 /* Start handling an overlay. */
4299 void
4300 lang_enter_overlay (vma_expr, lma_expr, nocrossrefs)
4301 etree_type *vma_expr;
4302 etree_type *lma_expr;
4303 int nocrossrefs;
4305 /* The grammar should prevent nested overlays from occurring. */
4306 ASSERT (overlay_vma == NULL
4307 && overlay_lma == NULL
4308 && overlay_list == NULL
4309 && overlay_max == NULL);
4311 overlay_vma = vma_expr;
4312 overlay_lma = lma_expr;
4313 overlay_nocrossrefs = nocrossrefs;
4316 /* Start a section in an overlay. We handle this by calling
4317 lang_enter_output_section_statement with the correct VMA and LMA. */
4319 void
4320 lang_enter_overlay_section (name)
4321 const char *name;
4323 struct overlay_list *n;
4324 etree_type *size;
4326 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4327 0, 0, 0, overlay_lma);
4329 /* If this is the first section, then base the VMA and LMA of future
4330 sections on this one. This will work correctly even if `.' is
4331 used in the addresses. */
4332 if (overlay_list == NULL)
4334 overlay_vma = exp_nameop (ADDR, name);
4335 overlay_lma = exp_nameop (LOADADDR, name);
4338 /* Remember the section. */
4339 n = (struct overlay_list *) xmalloc (sizeof *n);
4340 n->os = current_section;
4341 n->next = overlay_list;
4342 overlay_list = n;
4344 size = exp_nameop (SIZEOF, name);
4346 /* Adjust the LMA for the next section. */
4347 overlay_lma = exp_binop ('+', overlay_lma, size);
4349 /* Arrange to work out the maximum section end address. */
4350 if (overlay_max == NULL)
4351 overlay_max = size;
4352 else
4353 overlay_max = exp_binop (MAX_K, overlay_max, size);
4356 /* Finish a section in an overlay. There isn't any special to do
4357 here. */
4359 void
4360 lang_leave_overlay_section (fill, phdrs)
4361 bfd_vma fill;
4362 struct lang_output_section_phdr_list *phdrs;
4364 const char *name;
4365 char *clean, *s2;
4366 const char *s1;
4367 char *buf;
4369 name = current_section->name;
4371 lang_leave_output_section_statement (fill, "*default*", phdrs);
4373 /* Define the magic symbols. */
4375 clean = xmalloc (strlen (name) + 1);
4376 s2 = clean;
4377 for (s1 = name; *s1 != '\0'; s1++)
4378 if (isalnum ((unsigned char) *s1) || *s1 == '_')
4379 *s2++ = *s1;
4380 *s2 = '\0';
4382 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4383 sprintf (buf, "__load_start_%s", clean);
4384 lang_add_assignment (exp_assop ('=', buf,
4385 exp_nameop (LOADADDR, name)));
4387 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4388 sprintf (buf, "__load_stop_%s", clean);
4389 lang_add_assignment (exp_assop ('=', buf,
4390 exp_binop ('+',
4391 exp_nameop (LOADADDR, name),
4392 exp_nameop (SIZEOF, name))));
4394 free (clean);
4397 /* Finish an overlay. If there are any overlay wide settings, this
4398 looks through all the sections in the overlay and sets them. */
4400 void
4401 lang_leave_overlay (fill, memspec, phdrs)
4402 bfd_vma fill;
4403 const char *memspec;
4404 struct lang_output_section_phdr_list *phdrs;
4406 lang_memory_region_type *region;
4407 struct overlay_list *l;
4408 struct lang_nocrossref *nocrossref;
4410 if (memspec == NULL)
4411 region = NULL;
4412 else
4413 region = lang_memory_region_lookup (memspec);
4415 nocrossref = NULL;
4417 l = overlay_list;
4418 while (l != NULL)
4420 struct overlay_list *next;
4422 if (fill != 0 && l->os->fill == 0)
4423 l->os->fill = fill;
4424 if (region != NULL && l->os->region == NULL)
4425 l->os->region = region;
4426 if (phdrs != NULL && l->os->phdrs == NULL)
4427 l->os->phdrs = phdrs;
4429 if (overlay_nocrossrefs)
4431 struct lang_nocrossref *nc;
4433 nc = (struct lang_nocrossref *) xmalloc (sizeof *nc);
4434 nc->name = l->os->name;
4435 nc->next = nocrossref;
4436 nocrossref = nc;
4439 next = l->next;
4440 free (l);
4441 l = next;
4444 if (nocrossref != NULL)
4445 lang_add_nocrossref (nocrossref);
4447 /* Update . for the end of the overlay. */
4448 lang_add_assignment (exp_assop ('=', ".",
4449 exp_binop ('+', overlay_vma, overlay_max)));
4451 overlay_vma = NULL;
4452 overlay_lma = NULL;
4453 overlay_nocrossrefs = 0;
4454 overlay_list = NULL;
4455 overlay_max = NULL;
4458 /* Version handling. This is only useful for ELF. */
4460 /* This global variable holds the version tree that we build. */
4462 struct bfd_elf_version_tree *lang_elf_version_info;
4464 static int
4465 lang_vers_match_lang_c (expr, sym)
4466 struct bfd_elf_version_expr *expr;
4467 const char *sym;
4469 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4470 return 1;
4471 return fnmatch (expr->pattern, sym, 0) == 0;
4474 static int
4475 lang_vers_match_lang_cplusplus (expr, sym)
4476 struct bfd_elf_version_expr *expr;
4477 const char *sym;
4479 char *alt_sym;
4480 int result;
4482 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4483 return 1;
4485 alt_sym = cplus_demangle(sym, /* DMGL_NO_TPARAMS */ 0);
4486 if (!alt_sym)
4488 /* cplus_demangle (also) returns NULL when it is not a C++ symbol.
4489 Should we early out false in this case? */
4490 result = fnmatch (expr->pattern, sym, 0) == 0;
4492 else
4494 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4495 free (alt_sym);
4498 return result;
4501 static int
4502 lang_vers_match_lang_java (expr, sym)
4503 struct bfd_elf_version_expr *expr;
4504 const char *sym;
4506 char *alt_sym;
4507 int result;
4509 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
4510 return 1;
4512 alt_sym = cplus_demangle(sym, DMGL_JAVA);
4513 if (!alt_sym)
4515 /* cplus_demangle (also) returns NULL when it is not a Java symbol.
4516 Should we early out false in this case? */
4517 result = fnmatch (expr->pattern, sym, 0) == 0;
4519 else
4521 result = fnmatch (expr->pattern, alt_sym, 0) == 0;
4522 free (alt_sym);
4525 return result;
4528 /* This is called for each variable name or match expression. */
4530 struct bfd_elf_version_expr *
4531 lang_new_vers_regex (orig, new, lang)
4532 struct bfd_elf_version_expr *orig;
4533 const char *new;
4534 const char *lang;
4536 struct bfd_elf_version_expr *ret;
4538 ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
4539 ret->next = orig;
4540 ret->pattern = new;
4542 if (lang == NULL || strcasecmp (lang, "C") == 0)
4543 ret->match = lang_vers_match_lang_c;
4544 else if (strcasecmp (lang, "C++") == 0)
4545 ret->match = lang_vers_match_lang_cplusplus;
4546 else if (strcasecmp (lang, "Java") == 0)
4547 ret->match = lang_vers_match_lang_java;
4548 else
4550 einfo (_("%X%P: unknown language `%s' in version information\n"),
4551 lang);
4552 ret->match = lang_vers_match_lang_c;
4555 return ret;
4558 /* This is called for each set of variable names and match
4559 expressions. */
4561 struct bfd_elf_version_tree *
4562 lang_new_vers_node (globals, locals)
4563 struct bfd_elf_version_expr *globals;
4564 struct bfd_elf_version_expr *locals;
4566 struct bfd_elf_version_tree *ret;
4568 ret = (struct bfd_elf_version_tree *) xmalloc (sizeof *ret);
4569 ret->next = NULL;
4570 ret->name = NULL;
4571 ret->vernum = 0;
4572 ret->globals = globals;
4573 ret->locals = locals;
4574 ret->deps = NULL;
4575 ret->name_indx = (unsigned int) -1;
4576 ret->used = 0;
4577 return ret;
4580 /* This static variable keeps track of version indices. */
4582 static int version_index;
4584 /* This is called when we know the name and dependencies of the
4585 version. */
4587 void
4588 lang_register_vers_node (name, version, deps)
4589 const char *name;
4590 struct bfd_elf_version_tree *version;
4591 struct bfd_elf_version_deps *deps;
4593 struct bfd_elf_version_tree *t, **pp;
4594 struct bfd_elf_version_expr *e1;
4596 /* Make sure this node has a unique name. */
4597 for (t = lang_elf_version_info; t != NULL; t = t->next)
4598 if (strcmp (t->name, name) == 0)
4599 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
4601 /* Check the global and local match names, and make sure there
4602 aren't any duplicates. */
4604 for (e1 = version->globals; e1 != NULL; e1 = e1->next)
4606 for (t = lang_elf_version_info; t != NULL; t = t->next)
4608 struct bfd_elf_version_expr *e2;
4610 for (e2 = t->locals; e2 != NULL; e2 = e2->next)
4611 if (strcmp (e1->pattern, e2->pattern) == 0)
4612 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4613 e1->pattern);
4617 for (e1 = version->locals; e1 != NULL; e1 = e1->next)
4619 for (t = lang_elf_version_info; t != NULL; t = t->next)
4621 struct bfd_elf_version_expr *e2;
4623 for (e2 = t->globals; e2 != NULL; e2 = e2->next)
4624 if (strcmp (e1->pattern, e2->pattern) == 0)
4625 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
4626 e1->pattern);
4630 version->deps = deps;
4631 version->name = name;
4632 ++version_index;
4633 version->vernum = version_index;
4635 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
4637 *pp = version;
4640 /* This is called when we see a version dependency. */
4642 struct bfd_elf_version_deps *
4643 lang_add_vers_depend (list, name)
4644 struct bfd_elf_version_deps *list;
4645 const char *name;
4647 struct bfd_elf_version_deps *ret;
4648 struct bfd_elf_version_tree *t;
4650 ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
4651 ret->next = list;
4653 for (t = lang_elf_version_info; t != NULL; t = t->next)
4655 if (strcmp (t->name, name) == 0)
4657 ret->version_needed = t;
4658 return ret;
4662 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
4664 return ret;
4667 static void
4668 lang_do_version_exports_section ()
4670 struct bfd_elf_version_expr *greg = NULL, *lreg;
4672 LANG_FOR_EACH_INPUT_STATEMENT (is)
4674 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
4675 char *contents, *p;
4676 bfd_size_type len;
4678 if (sec == NULL)
4679 continue;
4681 len = bfd_section_size (is->the_bfd, sec);
4682 contents = xmalloc (len);
4683 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
4684 einfo (_("%X%P: unable to read .exports section contents"), sec);
4686 p = contents;
4687 while (p < contents+len)
4689 greg = lang_new_vers_regex (greg, p, NULL);
4690 p = strchr (p, '\0') + 1;
4693 /* Do not free the contents, as we used them creating the regex. */
4695 /* Do not include this section in the link. */
4696 bfd_set_section_flags (is->the_bfd, sec,
4697 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
4700 lreg = lang_new_vers_regex (NULL, "*", NULL);
4701 lang_register_vers_node (command_line.version_exports_section,
4702 lang_new_vers_node (greg, lreg), NULL);