merge from gcc
[binutils.git] / ld / ldlang.c
blob636dc7f5c475a92e1bac8b7b1a3dbdb066cb606e
1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GLD, the Gnu Linker.
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libiberty.h"
26 #include "safe-ctype.h"
27 #include "obstack.h"
28 #include "bfdlink.h"
30 #include "ld.h"
31 #include "ldmain.h"
32 #include "ldexp.h"
33 #include "ldlang.h"
34 #include <ldgram.h>
35 #include "ldlex.h"
36 #include "ldmisc.h"
37 #include "ldctor.h"
38 #include "ldfile.h"
39 #include "ldemul.h"
40 #include "fnmatch.h"
41 #include "demangle.h"
42 #include "hashtab.h"
44 #ifndef offsetof
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
46 #endif
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50 static struct obstack map_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 bfd_boolean placed_commons = FALSE;
57 static lang_output_section_statement_type *default_common_section;
58 static bfd_boolean map_option_f;
59 static bfd_vma print_dot;
60 static lang_input_statement_type *first_file;
61 static const char *current_target;
62 static const char *output_target;
63 static lang_statement_list_type statement_list;
64 static struct lang_phdr *lang_phdr_list;
65 static struct bfd_hash_table lang_definedness_table;
67 /* Forward declarations. */
68 static void exp_init_os (etree_type *);
69 static void init_map_userdata (bfd *, asection *, void *);
70 static lang_input_statement_type *lookup_name (const char *);
71 static bfd_boolean load_symbols (lang_input_statement_type *,
72 lang_statement_list_type *);
73 static struct bfd_hash_entry *lang_definedness_newfunc
74 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
75 static void insert_undefined (const char *);
76 static void print_all_symbols (asection *);
77 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
78 static void print_statement (lang_statement_union_type *,
79 lang_output_section_statement_type *);
80 static void print_statement_list (lang_statement_union_type *,
81 lang_output_section_statement_type *);
82 static void print_statements (void);
83 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
84 static void lang_record_phdrs (void);
85 static void lang_do_version_exports_section (void);
87 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
88 asection *, lang_input_statement_type *, void *);
90 /* Exported variables. */
91 lang_output_section_statement_type *abs_output_section;
92 lang_statement_list_type lang_output_section_statement;
93 lang_statement_list_type *stat_ptr = &statement_list;
94 lang_statement_list_type file_chain = { NULL, NULL };
95 struct bfd_sym_chain entry_symbol = { NULL, NULL };
96 const char *entry_section = ".text";
97 bfd_boolean entry_from_cmdline;
98 bfd_boolean lang_has_input_file = FALSE;
99 bfd_boolean had_output_filename = FALSE;
100 bfd_boolean lang_float_flag = FALSE;
101 bfd_boolean delete_output_file_on_failure = FALSE;
102 struct lang_nocrossrefs *nocrossref_list;
103 struct unique_sections *unique_section_list;
104 static bfd_boolean ldlang_sysrooted_script = FALSE;
105 int lang_statement_iteration = 0;
107 etree_type *base; /* Relocation base - or null */
109 /* Return TRUE if the PATTERN argument is a wildcard pattern.
110 Although backslashes are treated specially if a pattern contains
111 wildcards, we do not consider the mere presence of a backslash to
112 be enough to cause the pattern to be treated as a wildcard.
113 That lets us handle DOS filenames more naturally. */
114 #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
116 #define new_stat(x, y) \
117 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
119 #define outside_section_address(q) \
120 ((q)->output_offset + (q)->output_section->vma)
122 #define outside_symbol_address(q) \
123 ((q)->value + outside_section_address (q->section))
125 #define SECTION_NAME_MAP_LENGTH (16)
127 void *
128 stat_alloc (size_t size)
130 return obstack_alloc (&stat_obstack, size);
133 bfd_boolean
134 unique_section_p (const asection *sec)
136 struct unique_sections *unam;
137 const char *secnam;
139 if (link_info.relocatable
140 && sec->owner != NULL
141 && bfd_is_group_section (sec->owner, sec))
142 return TRUE;
144 secnam = sec->name;
145 for (unam = unique_section_list; unam; unam = unam->next)
146 if (wildcardp (unam->name)
147 ? fnmatch (unam->name, secnam, 0) == 0
148 : strcmp (unam->name, secnam) == 0)
150 return TRUE;
153 return FALSE;
156 /* Generic traversal routines for finding matching sections. */
158 static void
159 walk_wild_section (lang_wild_statement_type *ptr,
160 lang_input_statement_type *file,
161 callback_t callback,
162 void *data)
164 asection *s;
166 if (file->just_syms_flag)
167 return;
169 for (s = file->the_bfd->sections; s != NULL; s = s->next)
171 struct wildcard_list *sec;
173 sec = ptr->section_list;
174 if (sec == NULL)
175 (*callback) (ptr, sec, s, file, data);
177 while (sec != NULL)
179 bfd_boolean skip = FALSE;
180 struct name_list *list_tmp;
182 /* Don't process sections from files which were
183 excluded. */
184 for (list_tmp = sec->spec.exclude_name_list;
185 list_tmp;
186 list_tmp = list_tmp->next)
188 if (wildcardp (list_tmp->name))
189 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
190 else
191 skip = strcmp (list_tmp->name, file->filename) == 0;
193 /* If this file is part of an archive, and the archive is
194 excluded, exclude this file. */
195 if (! skip && file->the_bfd != NULL
196 && file->the_bfd->my_archive != NULL
197 && file->the_bfd->my_archive->filename != NULL)
199 if (wildcardp (list_tmp->name))
200 skip = fnmatch (list_tmp->name,
201 file->the_bfd->my_archive->filename,
202 0) == 0;
203 else
204 skip = strcmp (list_tmp->name,
205 file->the_bfd->my_archive->filename) == 0;
208 if (skip)
209 break;
212 if (!skip && sec->spec.name != NULL)
214 const char *sname = bfd_get_section_name (file->the_bfd, s);
216 if (wildcardp (sec->spec.name))
217 skip = fnmatch (sec->spec.name, sname, 0) != 0;
218 else
219 skip = strcmp (sec->spec.name, sname) != 0;
222 if (!skip)
223 (*callback) (ptr, sec, s, file, data);
225 sec = sec->next;
230 /* Handle a wild statement for a single file F. */
232 static void
233 walk_wild_file (lang_wild_statement_type *s,
234 lang_input_statement_type *f,
235 callback_t callback,
236 void *data)
238 if (f->the_bfd == NULL
239 || ! bfd_check_format (f->the_bfd, bfd_archive))
240 walk_wild_section (s, f, callback, data);
241 else
243 bfd *member;
245 /* This is an archive file. We must map each member of the
246 archive separately. */
247 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
248 while (member != NULL)
250 /* When lookup_name is called, it will call the add_symbols
251 entry point for the archive. For each element of the
252 archive which is included, BFD will call ldlang_add_file,
253 which will set the usrdata field of the member to the
254 lang_input_statement. */
255 if (member->usrdata != NULL)
257 walk_wild_section (s, member->usrdata, callback, data);
260 member = bfd_openr_next_archived_file (f->the_bfd, member);
265 static void
266 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
268 const char *file_spec = s->filename;
270 if (file_spec == NULL)
272 /* Perform the iteration over all files in the list. */
273 LANG_FOR_EACH_INPUT_STATEMENT (f)
275 walk_wild_file (s, f, callback, data);
278 else if (wildcardp (file_spec))
280 LANG_FOR_EACH_INPUT_STATEMENT (f)
282 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
283 walk_wild_file (s, f, callback, data);
286 else
288 lang_input_statement_type *f;
290 /* Perform the iteration over a single file. */
291 f = lookup_name (file_spec);
292 if (f)
293 walk_wild_file (s, f, callback, data);
297 /* lang_for_each_statement walks the parse tree and calls the provided
298 function for each node. */
300 static void
301 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
302 lang_statement_union_type *s)
304 for (; s != NULL; s = s->header.next)
306 func (s);
308 switch (s->header.type)
310 case lang_constructors_statement_enum:
311 lang_for_each_statement_worker (func, constructor_list.head);
312 break;
313 case lang_output_section_statement_enum:
314 lang_for_each_statement_worker
315 (func, s->output_section_statement.children.head);
316 break;
317 case lang_wild_statement_enum:
318 lang_for_each_statement_worker (func,
319 s->wild_statement.children.head);
320 break;
321 case lang_group_statement_enum:
322 lang_for_each_statement_worker (func,
323 s->group_statement.children.head);
324 break;
325 case lang_data_statement_enum:
326 case lang_reloc_statement_enum:
327 case lang_object_symbols_statement_enum:
328 case lang_output_statement_enum:
329 case lang_target_statement_enum:
330 case lang_input_section_enum:
331 case lang_input_statement_enum:
332 case lang_assignment_statement_enum:
333 case lang_padding_statement_enum:
334 case lang_address_statement_enum:
335 case lang_fill_statement_enum:
336 break;
337 default:
338 FAIL ();
339 break;
344 void
345 lang_for_each_statement (void (*func) (lang_statement_union_type *))
347 lang_for_each_statement_worker (func, statement_list.head);
350 /*----------------------------------------------------------------------*/
352 void
353 lang_list_init (lang_statement_list_type *list)
355 list->head = NULL;
356 list->tail = &list->head;
359 /* Build a new statement node for the parse tree. */
361 static lang_statement_union_type *
362 new_statement (enum statement_enum type,
363 size_t size,
364 lang_statement_list_type *list)
366 lang_statement_union_type *new;
368 new = stat_alloc (size);
369 new->header.type = type;
370 new->header.next = NULL;
371 lang_statement_append (list, new, &new->header.next);
372 return new;
375 /* Build a new input file node for the language. There are several
376 ways in which we treat an input file, eg, we only look at symbols,
377 or prefix it with a -l etc.
379 We can be supplied with requests for input files more than once;
380 they may, for example be split over several lines like foo.o(.text)
381 foo.o(.data) etc, so when asked for a file we check that we haven't
382 got it already so we don't duplicate the bfd. */
384 static lang_input_statement_type *
385 new_afile (const char *name,
386 lang_input_file_enum_type file_type,
387 const char *target,
388 bfd_boolean add_to_list)
390 lang_input_statement_type *p;
392 if (add_to_list)
393 p = new_stat (lang_input_statement, stat_ptr);
394 else
396 p = stat_alloc (sizeof (lang_input_statement_type));
397 p->header.next = NULL;
400 lang_has_input_file = TRUE;
401 p->target = target;
402 p->sysrooted = FALSE;
403 switch (file_type)
405 case lang_input_file_is_symbols_only_enum:
406 p->filename = name;
407 p->is_archive = FALSE;
408 p->real = TRUE;
409 p->local_sym_name = name;
410 p->just_syms_flag = TRUE;
411 p->search_dirs_flag = FALSE;
412 break;
413 case lang_input_file_is_fake_enum:
414 p->filename = name;
415 p->is_archive = FALSE;
416 p->real = FALSE;
417 p->local_sym_name = name;
418 p->just_syms_flag = FALSE;
419 p->search_dirs_flag = FALSE;
420 break;
421 case lang_input_file_is_l_enum:
422 p->is_archive = TRUE;
423 p->filename = name;
424 p->real = TRUE;
425 p->local_sym_name = concat ("-l", name, NULL);
426 p->just_syms_flag = FALSE;
427 p->search_dirs_flag = TRUE;
428 break;
429 case lang_input_file_is_marker_enum:
430 p->filename = name;
431 p->is_archive = FALSE;
432 p->real = FALSE;
433 p->local_sym_name = name;
434 p->just_syms_flag = FALSE;
435 p->search_dirs_flag = TRUE;
436 break;
437 case lang_input_file_is_search_file_enum:
438 p->sysrooted = ldlang_sysrooted_script;
439 p->filename = name;
440 p->is_archive = FALSE;
441 p->real = TRUE;
442 p->local_sym_name = name;
443 p->just_syms_flag = FALSE;
444 p->search_dirs_flag = TRUE;
445 break;
446 case lang_input_file_is_file_enum:
447 p->filename = name;
448 p->is_archive = FALSE;
449 p->real = TRUE;
450 p->local_sym_name = name;
451 p->just_syms_flag = FALSE;
452 p->search_dirs_flag = FALSE;
453 break;
454 default:
455 FAIL ();
457 p->the_bfd = NULL;
458 p->asymbols = NULL;
459 p->next_real_file = NULL;
460 p->next = NULL;
461 p->symbol_count = 0;
462 p->dynamic = config.dynamic_link;
463 p->add_needed = add_needed;
464 p->as_needed = as_needed;
465 p->whole_archive = whole_archive;
466 p->loaded = FALSE;
467 lang_statement_append (&input_file_chain,
468 (lang_statement_union_type *) p,
469 &p->next_real_file);
470 return p;
473 lang_input_statement_type *
474 lang_add_input_file (const char *name,
475 lang_input_file_enum_type file_type,
476 const char *target)
478 lang_has_input_file = TRUE;
479 return new_afile (name, file_type, target, TRUE);
482 /* Build enough state so that the parser can build its tree. */
484 void
485 lang_init (void)
487 obstack_begin (&stat_obstack, 1000);
489 stat_ptr = &statement_list;
491 lang_list_init (stat_ptr);
493 lang_list_init (&input_file_chain);
494 lang_list_init (&lang_output_section_statement);
495 lang_list_init (&file_chain);
496 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
497 NULL);
498 abs_output_section =
499 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
501 abs_output_section->bfd_section = bfd_abs_section_ptr;
503 /* The value "3" is ad-hoc, somewhat related to the expected number of
504 DEFINED expressions in a linker script. For most default linker
505 scripts, there are none. Why a hash table then? Well, it's somewhat
506 simpler to re-use working machinery than using a linked list in terms
507 of code-complexity here in ld, besides the initialization which just
508 looks like other code here. */
509 if (!bfd_hash_table_init_n (&lang_definedness_table,
510 lang_definedness_newfunc, 3))
511 einfo (_("%P%F: out of memory during initialization"));
513 /* Callers of exp_fold_tree need to increment this. */
514 lang_statement_iteration = 0;
517 /*----------------------------------------------------------------------
518 A region is an area of memory declared with the
519 MEMORY { name:org=exp, len=exp ... }
520 syntax.
522 We maintain a list of all the regions here.
524 If no regions are specified in the script, then the default is used
525 which is created when looked up to be the entire data space.
527 If create is true we are creating a region inside a MEMORY block.
528 In this case it is probably an error to create a region that has
529 already been created. If we are not inside a MEMORY block it is
530 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
531 and so we issue a warning. */
533 static lang_memory_region_type *lang_memory_region_list;
534 static lang_memory_region_type **lang_memory_region_list_tail
535 = &lang_memory_region_list;
537 lang_memory_region_type *
538 lang_memory_region_lookup (const char *const name, bfd_boolean create)
540 lang_memory_region_type *p;
541 lang_memory_region_type *new;
543 /* NAME is NULL for LMA memspecs if no region was specified. */
544 if (name == NULL)
545 return NULL;
547 for (p = lang_memory_region_list; p != NULL; p = p->next)
548 if (strcmp (p->name, name) == 0)
550 if (create)
551 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
552 name);
553 return p;
556 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
557 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
559 new = stat_alloc (sizeof (lang_memory_region_type));
561 new->name = xstrdup (name);
562 new->next = NULL;
564 *lang_memory_region_list_tail = new;
565 lang_memory_region_list_tail = &new->next;
566 new->origin = 0;
567 new->flags = 0;
568 new->not_flags = 0;
569 new->length = ~(bfd_size_type) 0;
570 new->current = 0;
571 new->had_full_message = FALSE;
573 return new;
576 static lang_memory_region_type *
577 lang_memory_default (asection *section)
579 lang_memory_region_type *p;
581 flagword sec_flags = section->flags;
583 /* Override SEC_DATA to mean a writable section. */
584 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
585 sec_flags |= SEC_DATA;
587 for (p = lang_memory_region_list; p != NULL; p = p->next)
589 if ((p->flags & sec_flags) != 0
590 && (p->not_flags & sec_flags) == 0)
592 return p;
595 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
598 static lang_output_section_statement_type *
599 lang_output_section_find_1 (const char *const name, int constraint)
601 lang_output_section_statement_type *lookup;
603 for (lookup = &lang_output_section_statement.head->output_section_statement;
604 lookup != NULL;
605 lookup = lookup->next)
607 if (strcmp (name, lookup->name) == 0
608 && lookup->constraint != -1
609 && (constraint == 0 || constraint == lookup->constraint))
610 return lookup;
612 return NULL;
615 lang_output_section_statement_type *
616 lang_output_section_find (const char *const name)
618 return lang_output_section_find_1 (name, 0);
621 static lang_output_section_statement_type *
622 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
624 lang_output_section_statement_type *lookup;
626 lookup = lang_output_section_find_1 (name, constraint);
627 if (lookup == NULL)
629 lookup = new_stat (lang_output_section_statement, stat_ptr);
630 lookup->region = NULL;
631 lookup->lma_region = NULL;
632 lookup->fill = 0;
633 lookup->block_value = 1;
634 lookup->name = name;
636 lookup->next = NULL;
637 lookup->bfd_section = NULL;
638 lookup->processed = 0;
639 lookup->constraint = constraint;
640 lookup->sectype = normal_section;
641 lookup->addr_tree = NULL;
642 lang_list_init (&lookup->children);
644 lookup->memspec = NULL;
645 lookup->flags = 0;
646 lookup->subsection_alignment = -1;
647 lookup->section_alignment = -1;
648 lookup->load_base = NULL;
649 lookup->update_dot_tree = NULL;
650 lookup->phdrs = NULL;
652 lang_statement_append (&lang_output_section_statement,
653 (lang_statement_union_type *) lookup,
654 (lang_statement_union_type **) &lookup->next);
656 return lookup;
659 lang_output_section_statement_type *
660 lang_output_section_statement_lookup (const char *const name)
662 return lang_output_section_statement_lookup_1 (name, 0);
665 /* A variant of lang_output_section_find used by place_orphan.
666 Returns the output statement that should precede a new output
667 statement for SEC. If an exact match is found on certain flags,
668 sets *EXACT too. */
670 lang_output_section_statement_type *
671 lang_output_section_find_by_flags (const asection *sec,
672 lang_output_section_statement_type **exact)
674 lang_output_section_statement_type *first, *look, *found;
675 flagword flags;
677 /* We know the first statement on this list is *ABS*. May as well
678 skip it. */
679 first = &lang_output_section_statement.head->output_section_statement;
680 first = first->next;
682 /* First try for an exact match. */
683 found = NULL;
684 for (look = first; look; look = look->next)
686 flags = look->flags;
687 if (look->bfd_section != NULL)
688 flags = look->bfd_section->flags;
689 flags ^= sec->flags;
690 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
691 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
692 found = look;
694 if (found != NULL)
696 *exact = found;
697 return found;
700 if (sec->flags & SEC_CODE)
702 /* Try for a rw code section. */
703 for (look = first; look; look = look->next)
705 flags = look->flags;
706 if (look->bfd_section != NULL)
707 flags = look->bfd_section->flags;
708 flags ^= sec->flags;
709 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
710 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
711 found = look;
713 return found;
716 if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
718 /* .rodata can go after .text, .sdata2 after .rodata. */
719 for (look = first; look; look = look->next)
721 flags = look->flags;
722 if (look->bfd_section != NULL)
723 flags = look->bfd_section->flags;
724 flags ^= sec->flags;
725 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
726 | SEC_READONLY))
727 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
728 found = look;
730 return found;
733 if (sec->flags & SEC_SMALL_DATA)
735 /* .sdata goes after .data, .sbss after .sdata. */
736 for (look = first; look; look = look->next)
738 flags = look->flags;
739 if (look->bfd_section != NULL)
740 flags = look->bfd_section->flags;
741 flags ^= sec->flags;
742 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
743 | SEC_THREAD_LOCAL))
744 || ((look->flags & SEC_SMALL_DATA)
745 && !(sec->flags & SEC_HAS_CONTENTS)))
746 found = look;
748 return found;
751 if (sec->flags & SEC_HAS_CONTENTS)
753 /* .data goes after .rodata. */
754 for (look = first; look; look = look->next)
756 flags = look->flags;
757 if (look->bfd_section != NULL)
758 flags = look->bfd_section->flags;
759 flags ^= sec->flags;
760 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
761 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
762 found = look;
764 return found;
767 /* .bss goes last. */
768 for (look = first; look; look = look->next)
770 flags = look->flags;
771 if (look->bfd_section != NULL)
772 flags = look->bfd_section->flags;
773 flags ^= sec->flags;
774 if (!(flags & SEC_ALLOC))
775 found = look;
778 return found;
781 /* Find the last output section before given output statement.
782 Used by place_orphan. */
784 static asection *
785 output_prev_sec_find (lang_output_section_statement_type *os)
787 asection *s = (asection *) NULL;
788 lang_output_section_statement_type *lookup;
790 for (lookup = &lang_output_section_statement.head->output_section_statement;
791 lookup != NULL;
792 lookup = lookup->next)
794 if (lookup->constraint == -1)
795 continue;
796 if (lookup == os)
797 return s;
799 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
800 s = lookup->bfd_section;
803 return NULL;
806 lang_output_section_statement_type *
807 lang_insert_orphan (lang_input_statement_type *file,
808 asection *s,
809 const char *secname,
810 lang_output_section_statement_type *after,
811 struct orphan_save *place,
812 etree_type *address,
813 lang_statement_list_type *add_child)
815 lang_statement_list_type *old;
816 lang_statement_list_type add;
817 const char *ps;
818 etree_type *load_base;
819 lang_output_section_statement_type *os;
820 lang_output_section_statement_type **os_tail;
822 /* Start building a list of statements for this section.
823 First save the current statement pointer. */
824 old = stat_ptr;
826 /* If we have found an appropriate place for the output section
827 statements for this orphan, add them to our own private list,
828 inserting them later into the global statement list. */
829 if (after != NULL)
831 stat_ptr = &add;
832 lang_list_init (stat_ptr);
835 ps = NULL;
836 if (config.build_constructors)
838 /* If the name of the section is representable in C, then create
839 symbols to mark the start and the end of the section. */
840 for (ps = secname; *ps != '\0'; ps++)
841 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
842 break;
843 if (*ps == '\0')
845 char *symname;
846 etree_type *e_align;
848 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
849 symname[0] = bfd_get_symbol_leading_char (output_bfd);
850 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
851 e_align = exp_unop (ALIGN_K,
852 exp_intop ((bfd_vma) 1 << s->alignment_power));
853 lang_add_assignment (exp_assop ('=', ".", e_align));
854 lang_add_assignment (exp_assop ('=', symname,
855 exp_nameop (NAME, ".")));
859 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
860 address = exp_intop (0);
862 load_base = NULL;
863 if (after != NULL && after->load_base != NULL)
865 etree_type *lma_from_vma;
866 lma_from_vma = exp_binop ('-', after->load_base,
867 exp_nameop (ADDR, after->name));
868 load_base = exp_binop ('+', lma_from_vma,
869 exp_nameop (ADDR, secname));
872 os_tail = ((lang_output_section_statement_type **)
873 lang_output_section_statement.tail);
874 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
875 load_base, 0);
877 if (add_child == NULL)
878 add_child = &os->children;
879 lang_add_section (add_child, s, os, file);
881 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
883 if (config.build_constructors && *ps == '\0')
885 char *symname;
887 /* lang_leave_ouput_section_statement resets stat_ptr.
888 Put stat_ptr back where we want it. */
889 if (after != NULL)
890 stat_ptr = &add;
892 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
893 symname[0] = bfd_get_symbol_leading_char (output_bfd);
894 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
895 lang_add_assignment (exp_assop ('=', symname,
896 exp_nameop (NAME, ".")));
899 /* Restore the global list pointer. */
900 if (after != NULL)
901 stat_ptr = old;
903 if (after != NULL && os->bfd_section != NULL)
905 asection *snew, **pps;
907 snew = os->bfd_section;
909 /* Shuffle the bfd section list to make the output file look
910 neater. This is really only cosmetic. */
911 if (place->section == NULL
912 && after != (&lang_output_section_statement.head
913 ->output_section_statement))
915 asection *bfd_section = after->bfd_section;
917 /* If the output statement hasn't been used to place any input
918 sections (and thus doesn't have an output bfd_section),
919 look for the closest prior output statement having an
920 output section. */
921 if (bfd_section == NULL)
922 bfd_section = output_prev_sec_find (after);
924 if (bfd_section != NULL && bfd_section != snew)
925 place->section = &bfd_section->next;
928 if (place->section == NULL)
929 place->section = &output_bfd->sections;
931 /* Unlink the section. */
932 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
933 continue;
934 bfd_section_list_remove (output_bfd, pps);
936 /* Now tack it back on in the right place. */
937 bfd_section_list_insert (output_bfd, place->section, snew);
939 /* Save the end of this list. Further ophans of this type will
940 follow the one we've just added. */
941 place->section = &snew->next;
943 /* The following is non-cosmetic. We try to put the output
944 statements in some sort of reasonable order here, because they
945 determine the final load addresses of the orphan sections.
946 In addition, placing output statements in the wrong order may
947 require extra segments. For instance, given a typical
948 situation of all read-only sections placed in one segment and
949 following that a segment containing all the read-write
950 sections, we wouldn't want to place an orphan read/write
951 section before or amongst the read-only ones. */
952 if (add.head != NULL)
954 lang_output_section_statement_type *newly_added_os;
956 if (place->stmt == NULL)
958 lang_statement_union_type **where;
959 lang_statement_union_type **assign = NULL;
961 /* Look for a suitable place for the new statement list.
962 The idea is to skip over anything that might be inside
963 a SECTIONS {} statement in a script, before we find
964 another output_section_statement. Assignments to "dot"
965 before an output section statement are assumed to
966 belong to it. */
967 for (where = &after->header.next;
968 *where != NULL;
969 where = &(*where)->header.next)
971 switch ((*where)->header.type)
973 case lang_assignment_statement_enum:
974 if (assign == NULL)
976 lang_assignment_statement_type *ass;
977 ass = &(*where)->assignment_statement;
978 if (ass->exp->type.node_class != etree_assert
979 && ass->exp->assign.dst[0] == '.'
980 && ass->exp->assign.dst[1] == 0)
981 assign = where;
983 continue;
984 case lang_wild_statement_enum:
985 case lang_input_section_enum:
986 case lang_object_symbols_statement_enum:
987 case lang_fill_statement_enum:
988 case lang_data_statement_enum:
989 case lang_reloc_statement_enum:
990 case lang_padding_statement_enum:
991 case lang_constructors_statement_enum:
992 assign = NULL;
993 continue;
994 case lang_output_section_statement_enum:
995 if (assign != NULL)
996 where = assign;
997 case lang_input_statement_enum:
998 case lang_address_statement_enum:
999 case lang_target_statement_enum:
1000 case lang_output_statement_enum:
1001 case lang_group_statement_enum:
1002 case lang_afile_asection_pair_statement_enum:
1003 break;
1005 break;
1008 *add.tail = *where;
1009 *where = add.head;
1011 place->os_tail = &after->next;
1013 else
1015 /* Put it after the last orphan statement we added. */
1016 *add.tail = *place->stmt;
1017 *place->stmt = add.head;
1020 /* Fix the global list pointer if we happened to tack our
1021 new list at the tail. */
1022 if (*old->tail == add.head)
1023 old->tail = add.tail;
1025 /* Save the end of this list. */
1026 place->stmt = add.tail;
1028 /* Do the same for the list of output section statements. */
1029 newly_added_os = *os_tail;
1030 *os_tail = NULL;
1031 newly_added_os->next = *place->os_tail;
1032 *place->os_tail = newly_added_os;
1033 place->os_tail = &newly_added_os->next;
1035 /* Fixing the global list pointer here is a little different.
1036 We added to the list in lang_enter_output_section_statement,
1037 trimmed off the new output_section_statment above when
1038 assigning *os_tail = NULL, but possibly added it back in
1039 the same place when assigning *place->os_tail. */
1040 if (*os_tail == NULL)
1041 lang_output_section_statement.tail
1042 = (lang_statement_union_type **) os_tail;
1045 return os;
1048 static void
1049 lang_map_flags (flagword flag)
1051 if (flag & SEC_ALLOC)
1052 minfo ("a");
1054 if (flag & SEC_CODE)
1055 minfo ("x");
1057 if (flag & SEC_READONLY)
1058 minfo ("r");
1060 if (flag & SEC_DATA)
1061 minfo ("w");
1063 if (flag & SEC_LOAD)
1064 minfo ("l");
1067 void
1068 lang_map (void)
1070 lang_memory_region_type *m;
1071 bfd *p;
1073 minfo (_("\nMemory Configuration\n\n"));
1074 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1075 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1077 for (m = lang_memory_region_list; m != NULL; m = m->next)
1079 char buf[100];
1080 int len;
1082 fprintf (config.map_file, "%-16s ", m->name);
1084 sprintf_vma (buf, m->origin);
1085 minfo ("0x%s ", buf);
1086 len = strlen (buf);
1087 while (len < 16)
1089 print_space ();
1090 ++len;
1093 minfo ("0x%V", m->length);
1094 if (m->flags || m->not_flags)
1096 #ifndef BFD64
1097 minfo (" ");
1098 #endif
1099 if (m->flags)
1101 print_space ();
1102 lang_map_flags (m->flags);
1105 if (m->not_flags)
1107 minfo (" !");
1108 lang_map_flags (m->not_flags);
1112 print_nl ();
1115 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1117 if (! command_line.reduce_memory_overheads)
1119 obstack_begin (&map_obstack, 1000);
1120 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1121 bfd_map_over_sections (p, init_map_userdata, 0);
1122 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1124 print_statements ();
1127 static void
1128 init_map_userdata (abfd, sec, data)
1129 bfd *abfd ATTRIBUTE_UNUSED;
1130 asection *sec;
1131 void *data ATTRIBUTE_UNUSED;
1133 fat_section_userdata_type *new_data
1134 = ((fat_section_userdata_type *) (stat_alloc
1135 (sizeof (fat_section_userdata_type))));
1137 ASSERT (get_userdata (sec) == NULL);
1138 get_userdata (sec) = new_data;
1139 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1142 static bfd_boolean
1143 sort_def_symbol (hash_entry, info)
1144 struct bfd_link_hash_entry *hash_entry;
1145 void *info ATTRIBUTE_UNUSED;
1147 if (hash_entry->type == bfd_link_hash_defined
1148 || hash_entry->type == bfd_link_hash_defweak)
1150 struct fat_user_section_struct *ud;
1151 struct map_symbol_def *def;
1153 ud = get_userdata (hash_entry->u.def.section);
1154 if (! ud)
1156 /* ??? What do we have to do to initialize this beforehand? */
1157 /* The first time we get here is bfd_abs_section... */
1158 init_map_userdata (0, hash_entry->u.def.section, 0);
1159 ud = get_userdata (hash_entry->u.def.section);
1161 else if (!ud->map_symbol_def_tail)
1162 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1164 def = obstack_alloc (&map_obstack, sizeof *def);
1165 def->entry = hash_entry;
1166 *(ud->map_symbol_def_tail) = def;
1167 ud->map_symbol_def_tail = &def->next;
1169 return TRUE;
1172 /* Initialize an output section. */
1174 static void
1175 init_os (lang_output_section_statement_type *s)
1177 lean_section_userdata_type *new;
1179 if (s->bfd_section != NULL)
1180 return;
1182 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1183 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1185 new = stat_alloc (SECTION_USERDATA_SIZE);
1186 memset (new, 0, SECTION_USERDATA_SIZE);
1188 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
1189 if (s->bfd_section == NULL)
1190 s->bfd_section = bfd_make_section (output_bfd, s->name);
1191 if (s->bfd_section == NULL)
1193 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1194 output_bfd->xvec->name, s->name);
1196 s->bfd_section->output_section = s->bfd_section;
1198 /* We initialize an output sections output offset to minus its own
1199 vma to allow us to output a section through itself. */
1200 s->bfd_section->output_offset = 0;
1201 get_userdata (s->bfd_section) = new;
1203 /* If there is a base address, make sure that any sections it might
1204 mention are initialized. */
1205 if (s->addr_tree != NULL)
1206 exp_init_os (s->addr_tree);
1208 if (s->load_base != NULL)
1209 exp_init_os (s->load_base);
1212 /* Make sure that all output sections mentioned in an expression are
1213 initialized. */
1215 static void
1216 exp_init_os (etree_type *exp)
1218 switch (exp->type.node_class)
1220 case etree_assign:
1221 exp_init_os (exp->assign.src);
1222 break;
1224 case etree_binary:
1225 exp_init_os (exp->binary.lhs);
1226 exp_init_os (exp->binary.rhs);
1227 break;
1229 case etree_trinary:
1230 exp_init_os (exp->trinary.cond);
1231 exp_init_os (exp->trinary.lhs);
1232 exp_init_os (exp->trinary.rhs);
1233 break;
1235 case etree_assert:
1236 exp_init_os (exp->assert_s.child);
1237 break;
1239 case etree_unary:
1240 exp_init_os (exp->unary.child);
1241 break;
1243 case etree_name:
1244 switch (exp->type.node_code)
1246 case ADDR:
1247 case LOADADDR:
1248 case SIZEOF:
1250 lang_output_section_statement_type *os;
1252 os = lang_output_section_find (exp->name.name);
1253 if (os != NULL && os->bfd_section == NULL)
1254 init_os (os);
1257 break;
1259 default:
1260 break;
1264 static void
1265 section_already_linked (bfd *abfd, asection *sec, void *data)
1267 lang_input_statement_type *entry = data;
1269 /* If we are only reading symbols from this object, then we want to
1270 discard all sections. */
1271 if (entry->just_syms_flag)
1273 bfd_link_just_syms (abfd, sec, &link_info);
1274 return;
1277 if (!(abfd->flags & DYNAMIC))
1278 bfd_section_already_linked (abfd, sec);
1281 /* The wild routines.
1283 These expand statements like *(.text) and foo.o to a list of
1284 explicit actions, like foo.o(.text), bar.o(.text) and
1285 foo.o(.text, .data). */
1287 /* Add SECTION to the output section OUTPUT. Do this by creating a
1288 lang_input_section statement which is placed at PTR. FILE is the
1289 input file which holds SECTION. */
1291 void
1292 lang_add_section (lang_statement_list_type *ptr,
1293 asection *section,
1294 lang_output_section_statement_type *output,
1295 lang_input_statement_type *file)
1297 flagword flags = section->flags;
1298 bfd_boolean discard;
1300 /* Discard sections marked with SEC_EXCLUDE. */
1301 discard = (flags & SEC_EXCLUDE) != 0;
1303 /* Discard input sections which are assigned to a section named
1304 DISCARD_SECTION_NAME. */
1305 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1306 discard = TRUE;
1308 /* Discard debugging sections if we are stripping debugging
1309 information. */
1310 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1311 && (flags & SEC_DEBUGGING) != 0)
1312 discard = TRUE;
1314 if (discard)
1316 if (section->output_section == NULL)
1318 /* This prevents future calls from assigning this section. */
1319 section->output_section = bfd_abs_section_ptr;
1321 return;
1324 if (section->output_section == NULL)
1326 bfd_boolean first;
1327 lang_input_section_type *new;
1328 flagword flags;
1330 if (output->bfd_section == NULL)
1331 init_os (output);
1333 first = ! output->bfd_section->linker_has_input;
1334 output->bfd_section->linker_has_input = 1;
1336 /* Add a section reference to the list. */
1337 new = new_stat (lang_input_section, ptr);
1339 new->section = section;
1340 new->ifile = file;
1341 section->output_section = output->bfd_section;
1343 flags = section->flags;
1345 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1346 to an output section, because we want to be able to include a
1347 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1348 section (I don't know why we want to do this, but we do).
1349 build_link_order in ldwrite.c handles this case by turning
1350 the embedded SEC_NEVER_LOAD section into a fill. */
1352 flags &= ~ SEC_NEVER_LOAD;
1354 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1355 already been processed. One reason to do this is that on pe
1356 format targets, .text$foo sections go into .text and it's odd
1357 to see .text with SEC_LINK_ONCE set. */
1359 if (! link_info.relocatable)
1360 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1362 /* If this is not the first input section, and the SEC_READONLY
1363 flag is not currently set, then don't set it just because the
1364 input section has it set. */
1366 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
1367 flags &= ~ SEC_READONLY;
1369 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1370 if (! first
1371 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
1372 != (flags & (SEC_MERGE | SEC_STRINGS))
1373 || ((flags & SEC_MERGE)
1374 && output->bfd_section->entsize != section->entsize)))
1376 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1377 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1380 output->bfd_section->flags |= flags;
1382 if (flags & SEC_MERGE)
1383 output->bfd_section->entsize = section->entsize;
1385 /* If SEC_READONLY is not set in the input section, then clear
1386 it from the output section. */
1387 if ((section->flags & SEC_READONLY) == 0)
1388 output->bfd_section->flags &= ~SEC_READONLY;
1390 switch (output->sectype)
1392 case normal_section:
1393 break;
1394 case dsect_section:
1395 case copy_section:
1396 case info_section:
1397 case overlay_section:
1398 output->bfd_section->flags &= ~SEC_ALLOC;
1399 break;
1400 case noload_section:
1401 output->bfd_section->flags &= ~SEC_LOAD;
1402 output->bfd_section->flags |= SEC_NEVER_LOAD;
1403 break;
1406 /* Copy over SEC_SMALL_DATA. */
1407 if (section->flags & SEC_SMALL_DATA)
1408 output->bfd_section->flags |= SEC_SMALL_DATA;
1410 if (section->alignment_power > output->bfd_section->alignment_power)
1411 output->bfd_section->alignment_power = section->alignment_power;
1413 /* If supplied an alignment, then force it. */
1414 if (output->section_alignment != -1)
1415 output->bfd_section->alignment_power = output->section_alignment;
1417 if (bfd_get_arch (section->owner) == bfd_arch_tic54x
1418 && (section->flags & SEC_TIC54X_BLOCK) != 0)
1420 output->bfd_section->flags |= SEC_TIC54X_BLOCK;
1421 /* FIXME: This value should really be obtained from the bfd... */
1422 output->block_value = 128;
1427 /* Compare sections ASEC and BSEC according to SORT. */
1429 static int
1430 compare_section (sort_type sort, asection *asec, asection *bsec)
1432 int ret;
1434 switch (sort)
1436 default:
1437 abort ();
1439 case by_alignment_name:
1440 ret = (bfd_section_alignment (bsec->owner, bsec)
1441 - bfd_section_alignment (asec->owner, asec));
1442 if (ret)
1443 break;
1444 /* Fall through. */
1446 case by_name:
1447 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1448 bfd_get_section_name (bsec->owner, bsec));
1449 break;
1451 case by_name_alignment:
1452 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1453 bfd_get_section_name (bsec->owner, bsec));
1454 if (ret)
1455 break;
1456 /* Fall through. */
1458 case by_alignment:
1459 ret = (bfd_section_alignment (bsec->owner, bsec)
1460 - bfd_section_alignment (asec->owner, asec));
1461 break;
1464 return ret;
1467 /* Handle wildcard sorting. This returns the lang_input_section which
1468 should follow the one we are going to create for SECTION and FILE,
1469 based on the sorting requirements of WILD. It returns NULL if the
1470 new section should just go at the end of the current list. */
1472 static lang_statement_union_type *
1473 wild_sort (lang_wild_statement_type *wild,
1474 struct wildcard_list *sec,
1475 lang_input_statement_type *file,
1476 asection *section)
1478 const char *section_name;
1479 lang_statement_union_type *l;
1481 if (!wild->filenames_sorted
1482 && (sec == NULL || sec->spec.sorted == none))
1483 return NULL;
1485 section_name = bfd_get_section_name (file->the_bfd, section);
1486 for (l = wild->children.head; l != NULL; l = l->header.next)
1488 lang_input_section_type *ls;
1490 if (l->header.type != lang_input_section_enum)
1491 continue;
1492 ls = &l->input_section;
1494 /* Sorting by filename takes precedence over sorting by section
1495 name. */
1497 if (wild->filenames_sorted)
1499 const char *fn, *ln;
1500 bfd_boolean fa, la;
1501 int i;
1503 /* The PE support for the .idata section as generated by
1504 dlltool assumes that files will be sorted by the name of
1505 the archive and then the name of the file within the
1506 archive. */
1508 if (file->the_bfd != NULL
1509 && bfd_my_archive (file->the_bfd) != NULL)
1511 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1512 fa = TRUE;
1514 else
1516 fn = file->filename;
1517 fa = FALSE;
1520 if (ls->ifile->the_bfd != NULL
1521 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1523 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1524 la = TRUE;
1526 else
1528 ln = ls->ifile->filename;
1529 la = FALSE;
1532 i = strcmp (fn, ln);
1533 if (i > 0)
1534 continue;
1535 else if (i < 0)
1536 break;
1538 if (fa || la)
1540 if (fa)
1541 fn = file->filename;
1542 if (la)
1543 ln = ls->ifile->filename;
1545 i = strcmp (fn, ln);
1546 if (i > 0)
1547 continue;
1548 else if (i < 0)
1549 break;
1553 /* Here either the files are not sorted by name, or we are
1554 looking at the sections for this file. */
1556 if (sec != NULL && sec->spec.sorted != none)
1558 if (compare_section (sec->spec.sorted, section,
1559 ls->section) < 0)
1560 break;
1564 return l;
1567 /* Expand a wild statement for a particular FILE. SECTION may be
1568 NULL, in which case it is a wild card. */
1570 static void
1571 output_section_callback (lang_wild_statement_type *ptr,
1572 struct wildcard_list *sec,
1573 asection *section,
1574 lang_input_statement_type *file,
1575 void *output)
1577 lang_statement_union_type *before;
1579 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1580 if (unique_section_p (section))
1581 return;
1583 before = wild_sort (ptr, sec, file, section);
1585 /* Here BEFORE points to the lang_input_section which
1586 should follow the one we are about to add. If BEFORE
1587 is NULL, then the section should just go at the end
1588 of the current list. */
1590 if (before == NULL)
1591 lang_add_section (&ptr->children, section,
1592 (lang_output_section_statement_type *) output,
1593 file);
1594 else
1596 lang_statement_list_type list;
1597 lang_statement_union_type **pp;
1599 lang_list_init (&list);
1600 lang_add_section (&list, section,
1601 (lang_output_section_statement_type *) output,
1602 file);
1604 /* If we are discarding the section, LIST.HEAD will
1605 be NULL. */
1606 if (list.head != NULL)
1608 ASSERT (list.head->header.next == NULL);
1610 for (pp = &ptr->children.head;
1611 *pp != before;
1612 pp = &(*pp)->header.next)
1613 ASSERT (*pp != NULL);
1615 list.head->header.next = *pp;
1616 *pp = list.head;
1621 /* Check if all sections in a wild statement for a particular FILE
1622 are readonly. */
1624 static void
1625 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1626 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1627 asection *section,
1628 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1629 void *data)
1631 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1632 if (unique_section_p (section))
1633 return;
1635 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1636 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1639 /* This is passed a file name which must have been seen already and
1640 added to the statement tree. We will see if it has been opened
1641 already and had its symbols read. If not then we'll read it. */
1643 static lang_input_statement_type *
1644 lookup_name (const char *name)
1646 lang_input_statement_type *search;
1648 for (search = (lang_input_statement_type *) input_file_chain.head;
1649 search != NULL;
1650 search = (lang_input_statement_type *) search->next_real_file)
1652 /* Use the local_sym_name as the name of the file that has
1653 already been loaded as filename might have been transformed
1654 via the search directory lookup mechanism. */
1655 const char * filename = search->local_sym_name;
1657 if (filename == NULL && name == NULL)
1658 return search;
1659 if (filename != NULL
1660 && name != NULL
1661 && strcmp (filename, name) == 0)
1662 break;
1665 if (search == NULL)
1666 search = new_afile (name, lang_input_file_is_search_file_enum,
1667 default_target, FALSE);
1669 /* If we have already added this file, or this file is not real
1670 (FIXME: can that ever actually happen?) or the name is NULL
1671 (FIXME: can that ever actually happen?) don't add this file. */
1672 if (search->loaded
1673 || ! search->real
1674 || search->filename == NULL)
1675 return search;
1677 if (! load_symbols (search, NULL))
1678 return NULL;
1680 return search;
1683 /* Save LIST as a list of libraries whose symbols should not be exported. */
1685 struct excluded_lib
1687 char *name;
1688 struct excluded_lib *next;
1690 static struct excluded_lib *excluded_libs;
1692 void
1693 add_excluded_libs (const char *list)
1695 const char *p = list, *end;
1697 while (*p != '\0')
1699 struct excluded_lib *entry;
1700 end = strpbrk (p, ",:");
1701 if (end == NULL)
1702 end = p + strlen (p);
1703 entry = xmalloc (sizeof (*entry));
1704 entry->next = excluded_libs;
1705 entry->name = xmalloc (end - p + 1);
1706 memcpy (entry->name, p, end - p);
1707 entry->name[end - p] = '\0';
1708 excluded_libs = entry;
1709 if (*end == '\0')
1710 break;
1711 p = end + 1;
1715 static void
1716 check_excluded_libs (bfd *abfd)
1718 struct excluded_lib *lib = excluded_libs;
1720 while (lib)
1722 int len = strlen (lib->name);
1723 const char *filename = lbasename (abfd->filename);
1725 if (strcmp (lib->name, "ALL") == 0)
1727 abfd->no_export = TRUE;
1728 return;
1731 if (strncmp (lib->name, filename, len) == 0
1732 && (filename[len] == '\0'
1733 || (filename[len] == '.' && filename[len + 1] == 'a'
1734 && filename[len + 2] == '\0')))
1736 abfd->no_export = TRUE;
1737 return;
1740 lib = lib->next;
1744 /* Get the symbols for an input file. */
1746 static bfd_boolean
1747 load_symbols (lang_input_statement_type *entry,
1748 lang_statement_list_type *place)
1750 char **matching;
1752 if (entry->loaded)
1753 return TRUE;
1755 ldfile_open_file (entry);
1757 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1758 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1760 bfd_error_type err;
1761 lang_statement_list_type *hold;
1762 bfd_boolean bad_load = TRUE;
1763 bfd_boolean save_ldlang_sysrooted_script;
1765 err = bfd_get_error ();
1767 /* See if the emulation has some special knowledge. */
1768 if (ldemul_unrecognized_file (entry))
1769 return TRUE;
1771 if (err == bfd_error_file_ambiguously_recognized)
1773 char **p;
1775 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1776 einfo (_("%B: matching formats:"), entry->the_bfd);
1777 for (p = matching; *p != NULL; p++)
1778 einfo (" %s", *p);
1779 einfo ("%F\n");
1781 else if (err != bfd_error_file_not_recognized
1782 || place == NULL)
1783 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1784 else
1785 bad_load = FALSE;
1787 bfd_close (entry->the_bfd);
1788 entry->the_bfd = NULL;
1790 /* Try to interpret the file as a linker script. */
1791 ldfile_open_command_file (entry->filename);
1793 hold = stat_ptr;
1794 stat_ptr = place;
1795 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1796 ldlang_sysrooted_script = entry->sysrooted;
1798 ldfile_assumed_script = TRUE;
1799 parser_input = input_script;
1800 /* We want to use the same -Bdynamic/-Bstatic as the one for
1801 ENTRY. */
1802 config.dynamic_link = entry->dynamic;
1803 yyparse ();
1804 ldfile_assumed_script = FALSE;
1806 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1807 stat_ptr = hold;
1809 return ! bad_load;
1812 if (ldemul_recognized_file (entry))
1813 return TRUE;
1815 /* We don't call ldlang_add_file for an archive. Instead, the
1816 add_symbols entry point will call ldlang_add_file, via the
1817 add_archive_element callback, for each element of the archive
1818 which is used. */
1819 switch (bfd_get_format (entry->the_bfd))
1821 default:
1822 break;
1824 case bfd_object:
1825 ldlang_add_file (entry);
1826 if (trace_files || trace_file_tries)
1827 info_msg ("%I\n", entry);
1828 break;
1830 case bfd_archive:
1831 check_excluded_libs (entry->the_bfd);
1833 if (entry->whole_archive)
1835 bfd *member = NULL;
1836 bfd_boolean loaded = TRUE;
1838 for (;;)
1840 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1842 if (member == NULL)
1843 break;
1845 if (! bfd_check_format (member, bfd_object))
1847 einfo (_("%F%B: member %B in archive is not an object\n"),
1848 entry->the_bfd, member);
1849 loaded = FALSE;
1852 if (! ((*link_info.callbacks->add_archive_element)
1853 (&link_info, member, "--whole-archive")))
1854 abort ();
1856 if (! bfd_link_add_symbols (member, &link_info))
1858 einfo (_("%F%B: could not read symbols: %E\n"), member);
1859 loaded = FALSE;
1863 entry->loaded = loaded;
1864 return loaded;
1866 break;
1869 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1870 entry->loaded = TRUE;
1871 else
1872 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1874 return entry->loaded;
1877 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1878 may be NULL, indicating that it is a wildcard. Separate
1879 lang_input_section statements are created for each part of the
1880 expansion; they are added after the wild statement S. OUTPUT is
1881 the output section. */
1883 static void
1884 wild (lang_wild_statement_type *s,
1885 const char *target ATTRIBUTE_UNUSED,
1886 lang_output_section_statement_type *output)
1888 struct wildcard_list *sec;
1890 walk_wild (s, output_section_callback, output);
1892 for (sec = s->section_list; sec != NULL; sec = sec->next)
1894 if (default_common_section != NULL)
1895 break;
1896 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1898 /* Remember the section that common is going to in case we
1899 later get something which doesn't know where to put it. */
1900 default_common_section = output;
1905 /* Return TRUE iff target is the sought target. */
1907 static int
1908 get_target (const bfd_target *target, void *data)
1910 const char *sought = data;
1912 return strcmp (target->name, sought) == 0;
1915 /* Like strcpy() but convert to lower case as well. */
1917 static void
1918 stricpy (char *dest, char *src)
1920 char c;
1922 while ((c = *src++) != 0)
1923 *dest++ = TOLOWER (c);
1925 *dest = 0;
1928 /* Remove the first occurrence of needle (if any) in haystack
1929 from haystack. */
1931 static void
1932 strcut (char *haystack, char *needle)
1934 haystack = strstr (haystack, needle);
1936 if (haystack)
1938 char *src;
1940 for (src = haystack + strlen (needle); *src;)
1941 *haystack++ = *src++;
1943 *haystack = 0;
1947 /* Compare two target format name strings.
1948 Return a value indicating how "similar" they are. */
1950 static int
1951 name_compare (char *first, char *second)
1953 char *copy1;
1954 char *copy2;
1955 int result;
1957 copy1 = xmalloc (strlen (first) + 1);
1958 copy2 = xmalloc (strlen (second) + 1);
1960 /* Convert the names to lower case. */
1961 stricpy (copy1, first);
1962 stricpy (copy2, second);
1964 /* Remove size and endian strings from the name. */
1965 strcut (copy1, "big");
1966 strcut (copy1, "little");
1967 strcut (copy2, "big");
1968 strcut (copy2, "little");
1970 /* Return a value based on how many characters match,
1971 starting from the beginning. If both strings are
1972 the same then return 10 * their length. */
1973 for (result = 0; copy1[result] == copy2[result]; result++)
1974 if (copy1[result] == 0)
1976 result *= 10;
1977 break;
1980 free (copy1);
1981 free (copy2);
1983 return result;
1986 /* Set by closest_target_match() below. */
1987 static const bfd_target *winner;
1989 /* Scan all the valid bfd targets looking for one that has the endianness
1990 requirement that was specified on the command line, and is the nearest
1991 match to the original output target. */
1993 static int
1994 closest_target_match (const bfd_target *target, void *data)
1996 const bfd_target *original = data;
1998 if (command_line.endian == ENDIAN_BIG
1999 && target->byteorder != BFD_ENDIAN_BIG)
2000 return 0;
2002 if (command_line.endian == ENDIAN_LITTLE
2003 && target->byteorder != BFD_ENDIAN_LITTLE)
2004 return 0;
2006 /* Must be the same flavour. */
2007 if (target->flavour != original->flavour)
2008 return 0;
2010 /* If we have not found a potential winner yet, then record this one. */
2011 if (winner == NULL)
2013 winner = target;
2014 return 0;
2017 /* Oh dear, we now have two potential candidates for a successful match.
2018 Compare their names and choose the better one. */
2019 if (name_compare (target->name, original->name)
2020 > name_compare (winner->name, original->name))
2021 winner = target;
2023 /* Keep on searching until wqe have checked them all. */
2024 return 0;
2027 /* Return the BFD target format of the first input file. */
2029 static char *
2030 get_first_input_target (void)
2032 char *target = NULL;
2034 LANG_FOR_EACH_INPUT_STATEMENT (s)
2036 if (s->header.type == lang_input_statement_enum
2037 && s->real)
2039 ldfile_open_file (s);
2041 if (s->the_bfd != NULL
2042 && bfd_check_format (s->the_bfd, bfd_object))
2044 target = bfd_get_target (s->the_bfd);
2046 if (target != NULL)
2047 break;
2052 return target;
2055 const char *
2056 lang_get_output_target (void)
2058 const char *target;
2060 /* Has the user told us which output format to use? */
2061 if (output_target != NULL)
2062 return output_target;
2064 /* No - has the current target been set to something other than
2065 the default? */
2066 if (current_target != default_target)
2067 return current_target;
2069 /* No - can we determine the format of the first input file? */
2070 target = get_first_input_target ();
2071 if (target != NULL)
2072 return target;
2074 /* Failed - use the default output target. */
2075 return default_target;
2078 /* Open the output file. */
2080 static bfd *
2081 open_output (const char *name)
2083 bfd *output;
2085 output_target = lang_get_output_target ();
2087 /* Has the user requested a particular endianness on the command
2088 line? */
2089 if (command_line.endian != ENDIAN_UNSET)
2091 const bfd_target *target;
2092 enum bfd_endian desired_endian;
2094 /* Get the chosen target. */
2095 target = bfd_search_for_target (get_target, (void *) output_target);
2097 /* If the target is not supported, we cannot do anything. */
2098 if (target != NULL)
2100 if (command_line.endian == ENDIAN_BIG)
2101 desired_endian = BFD_ENDIAN_BIG;
2102 else
2103 desired_endian = BFD_ENDIAN_LITTLE;
2105 /* See if the target has the wrong endianness. This should
2106 not happen if the linker script has provided big and
2107 little endian alternatives, but some scrips don't do
2108 this. */
2109 if (target->byteorder != desired_endian)
2111 /* If it does, then see if the target provides
2112 an alternative with the correct endianness. */
2113 if (target->alternative_target != NULL
2114 && (target->alternative_target->byteorder == desired_endian))
2115 output_target = target->alternative_target->name;
2116 else
2118 /* Try to find a target as similar as possible to
2119 the default target, but which has the desired
2120 endian characteristic. */
2121 bfd_search_for_target (closest_target_match,
2122 (void *) target);
2124 /* Oh dear - we could not find any targets that
2125 satisfy our requirements. */
2126 if (winner == NULL)
2127 einfo (_("%P: warning: could not find any targets"
2128 " that match endianness requirement\n"));
2129 else
2130 output_target = winner->name;
2136 output = bfd_openw (name, output_target);
2138 if (output == NULL)
2140 if (bfd_get_error () == bfd_error_invalid_target)
2141 einfo (_("%P%F: target %s not found\n"), output_target);
2143 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2146 delete_output_file_on_failure = TRUE;
2148 if (! bfd_set_format (output, bfd_object))
2149 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2150 if (! bfd_set_arch_mach (output,
2151 ldfile_output_architecture,
2152 ldfile_output_machine))
2153 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2155 link_info.hash = bfd_link_hash_table_create (output);
2156 if (link_info.hash == NULL)
2157 einfo (_("%P%F: can not create link hash table: %E\n"));
2159 bfd_set_gp_size (output, g_switch_value);
2160 return output;
2163 static void
2164 ldlang_open_output (lang_statement_union_type *statement)
2166 switch (statement->header.type)
2168 case lang_output_statement_enum:
2169 ASSERT (output_bfd == NULL);
2170 output_bfd = open_output (statement->output_statement.name);
2171 ldemul_set_output_arch ();
2172 if (config.magic_demand_paged && !link_info.relocatable)
2173 output_bfd->flags |= D_PAGED;
2174 else
2175 output_bfd->flags &= ~D_PAGED;
2176 if (config.text_read_only)
2177 output_bfd->flags |= WP_TEXT;
2178 else
2179 output_bfd->flags &= ~WP_TEXT;
2180 if (link_info.traditional_format)
2181 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2182 else
2183 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2184 break;
2186 case lang_target_statement_enum:
2187 current_target = statement->target_statement.target;
2188 break;
2189 default:
2190 break;
2194 /* Convert between addresses in bytes and sizes in octets.
2195 For currently supported targets, octets_per_byte is always a power
2196 of two, so we can use shifts. */
2197 #define TO_ADDR(X) ((X) >> opb_shift)
2198 #define TO_SIZE(X) ((X) << opb_shift)
2200 /* Support the above. */
2201 static unsigned int opb_shift = 0;
2203 static void
2204 init_opb (void)
2206 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2207 ldfile_output_machine);
2208 opb_shift = 0;
2209 if (x > 1)
2210 while ((x & 1) == 0)
2212 x >>= 1;
2213 ++opb_shift;
2215 ASSERT (x == 1);
2218 /* Open all the input files. */
2220 static void
2221 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2223 for (; s != NULL; s = s->header.next)
2225 switch (s->header.type)
2227 case lang_constructors_statement_enum:
2228 open_input_bfds (constructor_list.head, force);
2229 break;
2230 case lang_output_section_statement_enum:
2231 open_input_bfds (s->output_section_statement.children.head, force);
2232 break;
2233 case lang_wild_statement_enum:
2234 /* Maybe we should load the file's symbols. */
2235 if (s->wild_statement.filename
2236 && ! wildcardp (s->wild_statement.filename))
2237 lookup_name (s->wild_statement.filename);
2238 open_input_bfds (s->wild_statement.children.head, force);
2239 break;
2240 case lang_group_statement_enum:
2242 struct bfd_link_hash_entry *undefs;
2244 /* We must continually search the entries in the group
2245 until no new symbols are added to the list of undefined
2246 symbols. */
2250 undefs = link_info.hash->undefs_tail;
2251 open_input_bfds (s->group_statement.children.head, TRUE);
2253 while (undefs != link_info.hash->undefs_tail);
2255 break;
2256 case lang_target_statement_enum:
2257 current_target = s->target_statement.target;
2258 break;
2259 case lang_input_statement_enum:
2260 if (s->input_statement.real)
2262 lang_statement_list_type add;
2264 s->input_statement.target = current_target;
2266 /* If we are being called from within a group, and this
2267 is an archive which has already been searched, then
2268 force it to be researched unless the whole archive
2269 has been loaded already. */
2270 if (force
2271 && !s->input_statement.whole_archive
2272 && s->input_statement.loaded
2273 && bfd_check_format (s->input_statement.the_bfd,
2274 bfd_archive))
2275 s->input_statement.loaded = FALSE;
2277 lang_list_init (&add);
2279 if (! load_symbols (&s->input_statement, &add))
2280 config.make_executable = FALSE;
2282 if (add.head != NULL)
2284 *add.tail = s->header.next;
2285 s->header.next = add.head;
2288 break;
2289 default:
2290 break;
2295 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2297 void
2298 lang_track_definedness (const char *name)
2300 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2301 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2304 /* New-function for the definedness hash table. */
2306 static struct bfd_hash_entry *
2307 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2308 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2309 const char *name ATTRIBUTE_UNUSED)
2311 struct lang_definedness_hash_entry *ret
2312 = (struct lang_definedness_hash_entry *) entry;
2314 if (ret == NULL)
2315 ret = (struct lang_definedness_hash_entry *)
2316 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2318 if (ret == NULL)
2319 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2321 ret->iteration = -1;
2322 return &ret->root;
2325 /* Return the iteration when the definition of NAME was last updated. A
2326 value of -1 means that the symbol is not defined in the linker script
2327 or the command line, but may be defined in the linker symbol table. */
2330 lang_symbol_definition_iteration (const char *name)
2332 struct lang_definedness_hash_entry *defentry
2333 = (struct lang_definedness_hash_entry *)
2334 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2336 /* We've already created this one on the presence of DEFINED in the
2337 script, so it can't be NULL unless something is borked elsewhere in
2338 the code. */
2339 if (defentry == NULL)
2340 FAIL ();
2342 return defentry->iteration;
2345 /* Update the definedness state of NAME. */
2347 void
2348 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
2350 struct lang_definedness_hash_entry *defentry
2351 = (struct lang_definedness_hash_entry *)
2352 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2354 /* We don't keep track of symbols not tested with DEFINED. */
2355 if (defentry == NULL)
2356 return;
2358 /* If the symbol was already defined, and not from an earlier statement
2359 iteration, don't update the definedness iteration, because that'd
2360 make the symbol seem defined in the linker script at this point, and
2361 it wasn't; it was defined in some object. If we do anyway, DEFINED
2362 would start to yield false before this point and the construct "sym =
2363 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2364 in an object. */
2365 if (h->type != bfd_link_hash_undefined
2366 && h->type != bfd_link_hash_common
2367 && h->type != bfd_link_hash_new
2368 && defentry->iteration == -1)
2369 return;
2371 defentry->iteration = lang_statement_iteration;
2374 /* Add the supplied name to the symbol table as an undefined reference.
2375 This is a two step process as the symbol table doesn't even exist at
2376 the time the ld command line is processed. First we put the name
2377 on a list, then, once the output file has been opened, transfer the
2378 name to the symbol table. */
2380 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2382 #define ldlang_undef_chain_list_head entry_symbol.next
2384 void
2385 ldlang_add_undef (const char *const name)
2387 ldlang_undef_chain_list_type *new =
2388 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2390 new->next = ldlang_undef_chain_list_head;
2391 ldlang_undef_chain_list_head = new;
2393 new->name = xstrdup (name);
2395 if (output_bfd != NULL)
2396 insert_undefined (new->name);
2399 /* Insert NAME as undefined in the symbol table. */
2401 static void
2402 insert_undefined (const char *name)
2404 struct bfd_link_hash_entry *h;
2406 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2407 if (h == NULL)
2408 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2409 if (h->type == bfd_link_hash_new)
2411 h->type = bfd_link_hash_undefined;
2412 h->u.undef.abfd = NULL;
2413 bfd_link_add_undef (link_info.hash, h);
2417 /* Run through the list of undefineds created above and place them
2418 into the linker hash table as undefined symbols belonging to the
2419 script file. */
2421 static void
2422 lang_place_undefineds (void)
2424 ldlang_undef_chain_list_type *ptr;
2426 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2427 insert_undefined (ptr->name);
2430 /* Check for all readonly or some readwrite sections. */
2432 static void
2433 check_input_sections
2434 (lang_statement_union_type *s,
2435 lang_output_section_statement_type *output_section_statement)
2437 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2439 switch (s->header.type)
2441 case lang_wild_statement_enum:
2442 walk_wild (&s->wild_statement, check_section_callback,
2443 output_section_statement);
2444 if (! output_section_statement->all_input_readonly)
2445 return;
2446 break;
2447 case lang_constructors_statement_enum:
2448 check_input_sections (constructor_list.head,
2449 output_section_statement);
2450 if (! output_section_statement->all_input_readonly)
2451 return;
2452 break;
2453 case lang_group_statement_enum:
2454 check_input_sections (s->group_statement.children.head,
2455 output_section_statement);
2456 if (! output_section_statement->all_input_readonly)
2457 return;
2458 break;
2459 default:
2460 break;
2465 /* Update wildcard statements if needed. */
2467 static void
2468 update_wild_statements (lang_statement_union_type *s)
2470 struct wildcard_list *sec;
2472 switch (sort_section)
2474 default:
2475 FAIL ();
2477 case none:
2478 break;
2480 case by_name:
2481 case by_alignment:
2482 for (; s != NULL; s = s->header.next)
2484 switch (s->header.type)
2486 default:
2487 break;
2489 case lang_wild_statement_enum:
2490 sec = s->wild_statement.section_list;
2491 if (sec != NULL)
2493 switch (sec->spec.sorted)
2495 case none:
2496 sec->spec.sorted = sort_section;
2497 break;
2498 case by_name:
2499 if (sort_section == by_alignment)
2500 sec->spec.sorted = by_name_alignment;
2501 break;
2502 case by_alignment:
2503 if (sort_section == by_name)
2504 sec->spec.sorted = by_alignment_name;
2505 break;
2506 default:
2507 break;
2510 break;
2512 case lang_constructors_statement_enum:
2513 update_wild_statements (constructor_list.head);
2514 break;
2516 case lang_output_section_statement_enum:
2517 update_wild_statements
2518 (s->output_section_statement.children.head);
2519 break;
2521 case lang_group_statement_enum:
2522 update_wild_statements (s->group_statement.children.head);
2523 break;
2526 break;
2530 /* Open input files and attach to output sections. */
2532 static void
2533 map_input_to_output_sections
2534 (lang_statement_union_type *s, const char *target,
2535 lang_output_section_statement_type *os)
2537 for (; s != NULL; s = s->header.next)
2539 switch (s->header.type)
2541 case lang_wild_statement_enum:
2542 wild (&s->wild_statement, target, os);
2543 break;
2544 case lang_constructors_statement_enum:
2545 map_input_to_output_sections (constructor_list.head,
2546 target,
2547 os);
2548 break;
2549 case lang_output_section_statement_enum:
2550 if (s->output_section_statement.constraint)
2552 if (s->output_section_statement.constraint == -1)
2553 break;
2554 s->output_section_statement.all_input_readonly = TRUE;
2555 check_input_sections (s->output_section_statement.children.head,
2556 &s->output_section_statement);
2557 if ((s->output_section_statement.all_input_readonly
2558 && s->output_section_statement.constraint == ONLY_IF_RW)
2559 || (!s->output_section_statement.all_input_readonly
2560 && s->output_section_statement.constraint == ONLY_IF_RO))
2562 s->output_section_statement.constraint = -1;
2563 break;
2567 map_input_to_output_sections (s->output_section_statement.children.head,
2568 target,
2569 &s->output_section_statement);
2570 break;
2571 case lang_output_statement_enum:
2572 break;
2573 case lang_target_statement_enum:
2574 target = s->target_statement.target;
2575 break;
2576 case lang_group_statement_enum:
2577 map_input_to_output_sections (s->group_statement.children.head,
2578 target,
2579 os);
2580 break;
2581 case lang_data_statement_enum:
2582 /* Make sure that any sections mentioned in the expression
2583 are initialized. */
2584 exp_init_os (s->data_statement.exp);
2585 if (os != NULL && os->bfd_section == NULL)
2586 init_os (os);
2587 /* The output section gets contents, and then we inspect for
2588 any flags set in the input script which override any ALLOC. */
2589 os->bfd_section->flags |= SEC_HAS_CONTENTS;
2590 if (!(os->flags & SEC_NEVER_LOAD))
2591 os->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2592 break;
2593 case lang_fill_statement_enum:
2594 case lang_input_section_enum:
2595 case lang_object_symbols_statement_enum:
2596 case lang_reloc_statement_enum:
2597 case lang_padding_statement_enum:
2598 case lang_input_statement_enum:
2599 if (os != NULL && os->bfd_section == NULL)
2600 init_os (os);
2601 break;
2602 case lang_assignment_statement_enum:
2603 if (os != NULL && os->bfd_section == NULL)
2604 init_os (os);
2606 /* Make sure that any sections mentioned in the assignment
2607 are initialized. */
2608 exp_init_os (s->assignment_statement.exp);
2609 break;
2610 case lang_afile_asection_pair_statement_enum:
2611 FAIL ();
2612 break;
2613 case lang_address_statement_enum:
2614 /* Mark the specified section with the supplied address.
2616 If this section was actually a segment marker, then the
2617 directive is ignored if the linker script explicitly
2618 processed the segment marker. Originally, the linker
2619 treated segment directives (like -Ttext on the
2620 command-line) as section directives. We honor the
2621 section directive semantics for backwards compatibilty;
2622 linker scripts that do not specifically check for
2623 SEGMENT_START automatically get the old semantics. */
2624 if (!s->address_statement.segment
2625 || !s->address_statement.segment->used)
2627 lang_output_section_statement_type *aos
2628 = (lang_output_section_statement_lookup
2629 (s->address_statement.section_name));
2631 if (aos->bfd_section == NULL)
2632 init_os (aos);
2633 aos->addr_tree = s->address_statement.address;
2635 break;
2640 /* An output section might have been removed after its statement was
2641 added. For example, ldemul_before_allocation can remove dynamic
2642 sections if they turn out to be not needed. Clean them up here. */
2644 static void
2645 strip_excluded_output_sections (void)
2647 lang_output_section_statement_type *os;
2649 for (os = &lang_output_section_statement.head->output_section_statement;
2650 os != NULL;
2651 os = os->next)
2653 asection *s;
2655 if (os->constraint == -1)
2656 continue;
2657 s = os->bfd_section;
2658 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2660 asection **p;
2662 os->bfd_section = NULL;
2664 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2665 if (*p == s)
2667 bfd_section_list_remove (output_bfd, p);
2668 output_bfd->section_count--;
2669 break;
2675 static void
2676 print_output_section_statement
2677 (lang_output_section_statement_type *output_section_statement)
2679 asection *section = output_section_statement->bfd_section;
2680 int len;
2682 if (output_section_statement != abs_output_section)
2684 minfo ("\n%s", output_section_statement->name);
2686 if (section != NULL)
2688 print_dot = section->vma;
2690 len = strlen (output_section_statement->name);
2691 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2693 print_nl ();
2694 len = 0;
2696 while (len < SECTION_NAME_MAP_LENGTH)
2698 print_space ();
2699 ++len;
2702 minfo ("0x%V %W", section->vma, section->size);
2704 if (output_section_statement->load_base != NULL)
2706 bfd_vma addr;
2708 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2709 "load base", lang_final_phase_enum);
2710 minfo (_(" load address 0x%V"), addr);
2714 print_nl ();
2717 print_statement_list (output_section_statement->children.head,
2718 output_section_statement);
2721 static void
2722 print_assignment (lang_assignment_statement_type *assignment,
2723 lang_output_section_statement_type *output_section)
2725 int i;
2726 int is_dot;
2727 etree_type *tree;
2728 etree_value_type result;
2730 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2731 print_space ();
2733 if (assignment->exp->type.node_class == etree_assert)
2735 is_dot = 0;
2736 tree = assignment->exp->assert_s.child;
2738 else
2740 const char *dst = assignment->exp->assign.dst;
2741 is_dot = dst[0] == '.' && dst[1] == 0;
2742 tree = assignment->exp->assign.src;
2745 result = exp_fold_tree (tree, output_section, lang_final_phase_enum,
2746 print_dot, &print_dot);
2747 if (result.valid_p)
2749 bfd_vma value;
2751 value = result.value + result.section->bfd_section->vma;
2753 minfo ("0x%V", value);
2754 if (is_dot)
2755 print_dot = value;
2757 else
2759 minfo ("*undef* ");
2760 #ifdef BFD64
2761 minfo (" ");
2762 #endif
2765 minfo (" ");
2766 exp_print_tree (assignment->exp);
2767 print_nl ();
2770 static void
2771 print_input_statement (lang_input_statement_type *statm)
2773 if (statm->filename != NULL)
2775 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2779 /* Print all symbols defined in a particular section. This is called
2780 via bfd_link_hash_traverse, or by print_all_symbols. */
2782 static bfd_boolean
2783 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2785 asection *sec = ptr;
2787 if ((hash_entry->type == bfd_link_hash_defined
2788 || hash_entry->type == bfd_link_hash_defweak)
2789 && sec == hash_entry->u.def.section)
2791 int i;
2793 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2794 print_space ();
2795 minfo ("0x%V ",
2796 (hash_entry->u.def.value
2797 + hash_entry->u.def.section->output_offset
2798 + hash_entry->u.def.section->output_section->vma));
2800 minfo (" %T\n", hash_entry->root.string);
2803 return TRUE;
2806 static void
2807 print_all_symbols (sec)
2808 asection *sec;
2810 struct fat_user_section_struct *ud = get_userdata (sec);
2811 struct map_symbol_def *def;
2813 if (!ud)
2814 return;
2816 *ud->map_symbol_def_tail = 0;
2817 for (def = ud->map_symbol_def_head; def; def = def->next)
2818 print_one_symbol (def->entry, sec);
2821 /* Print information about an input section to the map file. */
2823 static void
2824 print_input_section (lang_input_section_type *in)
2826 asection *i = in->section;
2827 bfd_size_type size = i->size;
2829 init_opb ();
2830 if (size != 0)
2832 int len;
2833 bfd_vma addr;
2835 print_space ();
2836 minfo ("%s", i->name);
2838 len = 1 + strlen (i->name);
2839 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2841 print_nl ();
2842 len = 0;
2844 while (len < SECTION_NAME_MAP_LENGTH)
2846 print_space ();
2847 ++len;
2850 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2851 addr = i->output_section->vma + i->output_offset;
2852 else
2854 addr = print_dot;
2855 size = 0;
2858 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2860 if (size != i->rawsize && i->rawsize != 0)
2862 len = SECTION_NAME_MAP_LENGTH + 3;
2863 #ifdef BFD64
2864 len += 16;
2865 #else
2866 len += 8;
2867 #endif
2868 while (len > 0)
2870 print_space ();
2871 --len;
2874 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2877 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2879 if (command_line.reduce_memory_overheads)
2880 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2881 else
2882 print_all_symbols (i);
2884 print_dot = addr + TO_ADDR (size);
2889 static void
2890 print_fill_statement (lang_fill_statement_type *fill)
2892 size_t size;
2893 unsigned char *p;
2894 fputs (" FILL mask 0x", config.map_file);
2895 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2896 fprintf (config.map_file, "%02x", *p);
2897 fputs ("\n", config.map_file);
2900 static void
2901 print_data_statement (lang_data_statement_type *data)
2903 int i;
2904 bfd_vma addr;
2905 bfd_size_type size;
2906 const char *name;
2908 init_opb ();
2909 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2910 print_space ();
2912 addr = data->output_vma;
2913 if (data->output_section != NULL)
2914 addr += data->output_section->vma;
2916 switch (data->type)
2918 default:
2919 abort ();
2920 case BYTE:
2921 size = BYTE_SIZE;
2922 name = "BYTE";
2923 break;
2924 case SHORT:
2925 size = SHORT_SIZE;
2926 name = "SHORT";
2927 break;
2928 case LONG:
2929 size = LONG_SIZE;
2930 name = "LONG";
2931 break;
2932 case QUAD:
2933 size = QUAD_SIZE;
2934 name = "QUAD";
2935 break;
2936 case SQUAD:
2937 size = QUAD_SIZE;
2938 name = "SQUAD";
2939 break;
2942 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2944 if (data->exp->type.node_class != etree_value)
2946 print_space ();
2947 exp_print_tree (data->exp);
2950 print_nl ();
2952 print_dot = addr + TO_ADDR (size);
2955 /* Print an address statement. These are generated by options like
2956 -Ttext. */
2958 static void
2959 print_address_statement (lang_address_statement_type *address)
2961 minfo (_("Address of section %s set to "), address->section_name);
2962 exp_print_tree (address->address);
2963 print_nl ();
2966 /* Print a reloc statement. */
2968 static void
2969 print_reloc_statement (lang_reloc_statement_type *reloc)
2971 int i;
2972 bfd_vma addr;
2973 bfd_size_type size;
2975 init_opb ();
2976 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2977 print_space ();
2979 addr = reloc->output_vma;
2980 if (reloc->output_section != NULL)
2981 addr += reloc->output_section->vma;
2983 size = bfd_get_reloc_size (reloc->howto);
2985 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2987 if (reloc->name != NULL)
2988 minfo ("%s+", reloc->name);
2989 else
2990 minfo ("%s+", reloc->section->name);
2992 exp_print_tree (reloc->addend_exp);
2994 print_nl ();
2996 print_dot = addr + TO_ADDR (size);
2999 static void
3000 print_padding_statement (lang_padding_statement_type *s)
3002 int len;
3003 bfd_vma addr;
3005 init_opb ();
3006 minfo (" *fill*");
3008 len = sizeof " *fill*" - 1;
3009 while (len < SECTION_NAME_MAP_LENGTH)
3011 print_space ();
3012 ++len;
3015 addr = s->output_offset;
3016 if (s->output_section != NULL)
3017 addr += s->output_section->vma;
3018 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3020 if (s->fill->size != 0)
3022 size_t size;
3023 unsigned char *p;
3024 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3025 fprintf (config.map_file, "%02x", *p);
3028 print_nl ();
3030 print_dot = addr + TO_ADDR (s->size);
3033 static void
3034 print_wild_statement (lang_wild_statement_type *w,
3035 lang_output_section_statement_type *os)
3037 struct wildcard_list *sec;
3039 print_space ();
3041 if (w->filenames_sorted)
3042 minfo ("SORT(");
3043 if (w->filename != NULL)
3044 minfo ("%s", w->filename);
3045 else
3046 minfo ("*");
3047 if (w->filenames_sorted)
3048 minfo (")");
3050 minfo ("(");
3051 for (sec = w->section_list; sec; sec = sec->next)
3053 if (sec->spec.sorted)
3054 minfo ("SORT(");
3055 if (sec->spec.exclude_name_list != NULL)
3057 name_list *tmp;
3058 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
3059 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
3060 minfo (" %s", tmp->name);
3061 minfo (") ");
3063 if (sec->spec.name != NULL)
3064 minfo ("%s", sec->spec.name);
3065 else
3066 minfo ("*");
3067 if (sec->spec.sorted)
3068 minfo (")");
3069 if (sec->next)
3070 minfo (" ");
3072 minfo (")");
3074 print_nl ();
3076 print_statement_list (w->children.head, os);
3079 /* Print a group statement. */
3081 static void
3082 print_group (lang_group_statement_type *s,
3083 lang_output_section_statement_type *os)
3085 fprintf (config.map_file, "START GROUP\n");
3086 print_statement_list (s->children.head, os);
3087 fprintf (config.map_file, "END GROUP\n");
3090 /* Print the list of statements in S.
3091 This can be called for any statement type. */
3093 static void
3094 print_statement_list (lang_statement_union_type *s,
3095 lang_output_section_statement_type *os)
3097 while (s != NULL)
3099 print_statement (s, os);
3100 s = s->header.next;
3104 /* Print the first statement in statement list S.
3105 This can be called for any statement type. */
3107 static void
3108 print_statement (lang_statement_union_type *s,
3109 lang_output_section_statement_type *os)
3111 switch (s->header.type)
3113 default:
3114 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
3115 FAIL ();
3116 break;
3117 case lang_constructors_statement_enum:
3118 if (constructor_list.head != NULL)
3120 if (constructors_sorted)
3121 minfo (" SORT (CONSTRUCTORS)\n");
3122 else
3123 minfo (" CONSTRUCTORS\n");
3124 print_statement_list (constructor_list.head, os);
3126 break;
3127 case lang_wild_statement_enum:
3128 print_wild_statement (&s->wild_statement, os);
3129 break;
3130 case lang_address_statement_enum:
3131 print_address_statement (&s->address_statement);
3132 break;
3133 case lang_object_symbols_statement_enum:
3134 minfo (" CREATE_OBJECT_SYMBOLS\n");
3135 break;
3136 case lang_fill_statement_enum:
3137 print_fill_statement (&s->fill_statement);
3138 break;
3139 case lang_data_statement_enum:
3140 print_data_statement (&s->data_statement);
3141 break;
3142 case lang_reloc_statement_enum:
3143 print_reloc_statement (&s->reloc_statement);
3144 break;
3145 case lang_input_section_enum:
3146 print_input_section (&s->input_section);
3147 break;
3148 case lang_padding_statement_enum:
3149 print_padding_statement (&s->padding_statement);
3150 break;
3151 case lang_output_section_statement_enum:
3152 print_output_section_statement (&s->output_section_statement);
3153 break;
3154 case lang_assignment_statement_enum:
3155 print_assignment (&s->assignment_statement, os);
3156 break;
3157 case lang_target_statement_enum:
3158 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
3159 break;
3160 case lang_output_statement_enum:
3161 minfo ("OUTPUT(%s", s->output_statement.name);
3162 if (output_target != NULL)
3163 minfo (" %s", output_target);
3164 minfo (")\n");
3165 break;
3166 case lang_input_statement_enum:
3167 print_input_statement (&s->input_statement);
3168 break;
3169 case lang_group_statement_enum:
3170 print_group (&s->group_statement, os);
3171 break;
3172 case lang_afile_asection_pair_statement_enum:
3173 FAIL ();
3174 break;
3178 static void
3179 print_statements (void)
3181 print_statement_list (statement_list.head, abs_output_section);
3184 /* Print the first N statements in statement list S to STDERR.
3185 If N == 0, nothing is printed.
3186 If N < 0, the entire list is printed.
3187 Intended to be called from GDB. */
3189 void
3190 dprint_statement (lang_statement_union_type *s, int n)
3192 FILE *map_save = config.map_file;
3194 config.map_file = stderr;
3196 if (n < 0)
3197 print_statement_list (s, abs_output_section);
3198 else
3200 while (s && --n >= 0)
3202 print_statement (s, abs_output_section);
3203 s = s->header.next;
3207 config.map_file = map_save;
3210 static void
3211 insert_pad (lang_statement_union_type **ptr,
3212 fill_type *fill,
3213 unsigned int alignment_needed,
3214 asection *output_section,
3215 bfd_vma dot)
3217 static fill_type zero_fill = { 1, { 0 } };
3218 lang_statement_union_type *pad;
3220 pad = ((lang_statement_union_type *)
3221 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
3222 if (ptr != &statement_list.head
3223 && pad->header.type == lang_padding_statement_enum
3224 && pad->padding_statement.output_section == output_section)
3226 /* Use the existing pad statement. The above test on output
3227 section is probably redundant, but it doesn't hurt to check. */
3229 else
3231 /* Make a new padding statement, linked into existing chain. */
3232 pad = stat_alloc (sizeof (lang_padding_statement_type));
3233 pad->header.next = *ptr;
3234 *ptr = pad;
3235 pad->header.type = lang_padding_statement_enum;
3236 pad->padding_statement.output_section = output_section;
3237 if (fill == NULL)
3238 fill = &zero_fill;
3239 pad->padding_statement.fill = fill;
3241 pad->padding_statement.output_offset = dot - output_section->vma;
3242 pad->padding_statement.size = alignment_needed;
3243 output_section->size += alignment_needed;
3246 /* Work out how much this section will move the dot point. */
3248 static bfd_vma
3249 size_input_section
3250 (lang_statement_union_type **this_ptr,
3251 lang_output_section_statement_type *output_section_statement,
3252 fill_type *fill,
3253 bfd_vma dot)
3255 lang_input_section_type *is = &((*this_ptr)->input_section);
3256 asection *i = is->section;
3258 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
3260 unsigned int alignment_needed;
3261 asection *o;
3263 /* Align this section first to the input sections requirement,
3264 then to the output section's requirement. If this alignment
3265 is greater than any seen before, then record it too. Perform
3266 the alignment by inserting a magic 'padding' statement. */
3268 if (output_section_statement->subsection_alignment != -1)
3269 i->alignment_power = output_section_statement->subsection_alignment;
3271 o = output_section_statement->bfd_section;
3272 if (o->alignment_power < i->alignment_power)
3273 o->alignment_power = i->alignment_power;
3275 alignment_needed = align_power (dot, i->alignment_power) - dot;
3277 if (alignment_needed != 0)
3279 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
3280 dot += alignment_needed;
3283 /* Remember where in the output section this input section goes. */
3285 i->output_offset = dot - o->vma;
3287 /* Mark how big the output section must be to contain this now. */
3288 dot += TO_ADDR (i->size);
3289 o->size = TO_SIZE (dot - o->vma);
3291 else
3293 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
3296 return dot;
3299 #define IGNORE_SECTION(s) \
3300 ((s->flags & SEC_NEVER_LOAD) != 0 \
3301 || (s->flags & SEC_ALLOC) == 0 \
3302 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
3303 && (s->flags & SEC_LOAD) == 0) \
3304 || s->size == 0)
3306 /* Check to see if any allocated sections overlap with other allocated
3307 sections. This can happen if a linker script specifies the output
3308 section addresses of the two sections. */
3310 static void
3311 lang_check_section_addresses (void)
3313 asection *s;
3315 /* Scan all sections in the output list. */
3316 for (s = output_bfd->sections; s != NULL; s = s->next)
3318 asection *os;
3320 /* Ignore sections which are not loaded or which have no contents. */
3321 if (IGNORE_SECTION (s))
3322 continue;
3324 /* Once we reach section 's' stop our seach. This prevents two
3325 warning messages from being produced, one for 'section A overlaps
3326 section B' and one for 'section B overlaps section A'. */
3327 for (os = output_bfd->sections; os != s; os = os->next)
3329 bfd_vma s_start;
3330 bfd_vma s_end;
3331 bfd_vma os_start;
3332 bfd_vma os_end;
3334 /* Only consider loadable sections with real contents. */
3335 if (IGNORE_SECTION (os))
3336 continue;
3338 /* We must check the sections' LMA addresses not their
3339 VMA addresses because overlay sections can have
3340 overlapping VMAs but they must have distinct LMAs. */
3341 s_start = bfd_section_lma (output_bfd, s);
3342 os_start = bfd_section_lma (output_bfd, os);
3343 s_end = s_start + TO_ADDR (s->size) - 1;
3344 os_end = os_start + TO_ADDR (os->size) - 1;
3346 /* Look for an overlap. */
3347 if ((s_end < os_start) || (s_start > os_end))
3348 continue;
3350 einfo (
3351 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
3352 s->name, s_start, s_end, os->name, os_start, os_end);
3354 /* Once we have found one overlap for this section,
3355 stop looking for others. */
3356 break;
3361 /* Make sure the new address is within the region. We explicitly permit the
3362 current address to be at the exact end of the region when the address is
3363 non-zero, in case the region is at the end of addressable memory and the
3364 calculation wraps around. */
3366 static void
3367 os_region_check (lang_output_section_statement_type *os,
3368 lang_memory_region_type *region,
3369 etree_type *tree,
3370 bfd_vma base)
3372 if ((region->current < region->origin
3373 || (region->current - region->origin > region->length))
3374 && ((region->current != region->origin + region->length)
3375 || base == 0))
3377 if (tree != NULL)
3379 einfo (_("%X%P: address 0x%v of %B section %s"
3380 " is not within region %s\n"),
3381 region->current,
3382 os->bfd_section->owner,
3383 os->bfd_section->name,
3384 region->name);
3386 else
3388 einfo (_("%X%P: region %s is full (%B section %s)\n"),
3389 region->name,
3390 os->bfd_section->owner,
3391 os->bfd_section->name);
3393 /* Reset the region pointer. */
3394 region->current = region->origin;
3398 /* Set the sizes for all the output sections. */
3400 static bfd_vma
3401 lang_size_sections_1
3402 (lang_statement_union_type *s,
3403 lang_output_section_statement_type *output_section_statement,
3404 lang_statement_union_type **prev,
3405 fill_type *fill,
3406 bfd_vma dot,
3407 bfd_boolean *relax,
3408 bfd_boolean check_regions)
3410 /* Size up the sections from their constituent parts. */
3411 for (; s != NULL; s = s->header.next)
3413 switch (s->header.type)
3415 case lang_output_section_statement_enum:
3417 bfd_vma after;
3418 lang_output_section_statement_type *os;
3420 os = &s->output_section_statement;
3421 if (os->bfd_section == NULL)
3422 /* This section was never actually created. */
3423 break;
3425 /* If this is a COFF shared library section, use the size and
3426 address from the input section. FIXME: This is COFF
3427 specific; it would be cleaner if there were some other way
3428 to do this, but nothing simple comes to mind. */
3429 if ((bfd_get_flavour (output_bfd) == bfd_target_ecoff_flavour
3430 || bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
3431 && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
3433 asection *input;
3435 if (os->children.head == NULL
3436 || os->children.head->header.next != NULL
3437 || (os->children.head->header.type
3438 != lang_input_section_enum))
3439 einfo (_("%P%X: Internal error on COFF shared library"
3440 " section %s\n"), os->name);
3442 input = os->children.head->input_section.section;
3443 bfd_set_section_vma (os->bfd_section->owner,
3444 os->bfd_section,
3445 bfd_section_vma (input->owner, input));
3446 os->bfd_section->size = input->size;
3447 break;
3450 if (bfd_is_abs_section (os->bfd_section))
3452 /* No matter what happens, an abs section starts at zero. */
3453 ASSERT (os->bfd_section->vma == 0);
3455 else
3457 if (os->addr_tree == NULL)
3459 /* No address specified for this section, get one
3460 from the region specification. */
3461 if (os->region == NULL
3462 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
3463 && os->region->name[0] == '*'
3464 && strcmp (os->region->name,
3465 DEFAULT_MEMORY_REGION) == 0))
3467 os->region = lang_memory_default (os->bfd_section);
3470 /* If a loadable section is using the default memory
3471 region, and some non default memory regions were
3472 defined, issue an error message. */
3473 if (!IGNORE_SECTION (os->bfd_section)
3474 && ! link_info.relocatable
3475 && check_regions
3476 && strcmp (os->region->name,
3477 DEFAULT_MEMORY_REGION) == 0
3478 && lang_memory_region_list != NULL
3479 && (strcmp (lang_memory_region_list->name,
3480 DEFAULT_MEMORY_REGION) != 0
3481 || lang_memory_region_list->next != NULL))
3483 /* By default this is an error rather than just a
3484 warning because if we allocate the section to the
3485 default memory region we can end up creating an
3486 excessively large binary, or even seg faulting when
3487 attempting to perform a negative seek. See
3488 sources.redhat.com/ml/binutils/2003-04/msg00423.html
3489 for an example of this. This behaviour can be
3490 overridden by the using the --no-check-sections
3491 switch. */
3492 if (command_line.check_section_addresses)
3493 einfo (_("%P%F: error: no memory region specified"
3494 " for loadable section `%s'\n"),
3495 bfd_get_section_name (output_bfd,
3496 os->bfd_section));
3497 else
3498 einfo (_("%P: warning: no memory region specified"
3499 " for loadable section `%s'\n"),
3500 bfd_get_section_name (output_bfd,
3501 os->bfd_section));
3504 dot = os->region->current;
3506 if (os->section_alignment == -1)
3508 bfd_vma olddot;
3510 olddot = dot;
3511 dot = align_power (dot,
3512 os->bfd_section->alignment_power);
3514 if (dot != olddot && config.warn_section_align)
3515 einfo (_("%P: warning: changing start of section"
3516 " %s by %u bytes\n"),
3517 os->name, (unsigned int) (dot - olddot));
3520 else
3522 etree_value_type r;
3524 os->processed = -1;
3525 r = exp_fold_tree (os->addr_tree,
3526 abs_output_section,
3527 lang_allocating_phase_enum,
3528 dot, &dot);
3529 os->processed = 0;
3531 if (!r.valid_p)
3532 einfo (_("%F%S: non constant or forward reference"
3533 " address expression for section %s\n"),
3534 os->name);
3536 dot = r.value + r.section->bfd_section->vma;
3539 /* The section starts here.
3540 First, align to what the section needs. */
3542 if (os->section_alignment != -1)
3543 dot = align_power (dot, os->section_alignment);
3545 bfd_set_section_vma (0, os->bfd_section, dot);
3547 os->bfd_section->output_offset = 0;
3550 lang_size_sections_1 (os->children.head, os, &os->children.head,
3551 os->fill, dot, relax, check_regions);
3553 /* Put the section within the requested block size, or
3554 align at the block boundary. */
3555 after = ((os->bfd_section->vma
3556 + TO_ADDR (os->bfd_section->size)
3557 + os->block_value - 1)
3558 & - (bfd_vma) os->block_value);
3560 if (bfd_is_abs_section (os->bfd_section))
3561 ASSERT (after == os->bfd_section->vma);
3562 else
3563 os->bfd_section->size
3564 = TO_SIZE (after - os->bfd_section->vma);
3566 dot = os->bfd_section->vma;
3567 /* .tbss sections effectively have zero size. */
3568 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3569 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3570 || link_info.relocatable)
3571 dot += TO_ADDR (os->bfd_section->size);
3573 os->processed = 1;
3575 if (os->update_dot_tree != 0)
3576 exp_fold_tree (os->update_dot_tree, abs_output_section,
3577 lang_allocating_phase_enum, dot, &dot);
3579 /* Update dot in the region ?
3580 We only do this if the section is going to be allocated,
3581 since unallocated sections do not contribute to the region's
3582 overall size in memory.
3584 If the SEC_NEVER_LOAD bit is not set, it will affect the
3585 addresses of sections after it. We have to update
3586 dot. */
3587 if (os->region != NULL
3588 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3589 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3591 os->region->current = dot;
3593 if (check_regions)
3594 /* Make sure the new address is within the region. */
3595 os_region_check (os, os->region, os->addr_tree,
3596 os->bfd_section->vma);
3598 /* If there's no load address specified, use the run
3599 region as the load region. */
3600 if (os->lma_region == NULL && os->load_base == NULL)
3601 os->lma_region = os->region;
3603 if (os->lma_region != NULL && os->lma_region != os->region)
3605 /* Set load_base, which will be handled later. */
3606 os->load_base = exp_intop (os->lma_region->current);
3607 os->lma_region->current +=
3608 TO_ADDR (os->bfd_section->size);
3609 if (check_regions)
3610 os_region_check (os, os->lma_region, NULL,
3611 os->bfd_section->lma);
3615 break;
3617 case lang_constructors_statement_enum:
3618 dot = lang_size_sections_1 (constructor_list.head,
3619 output_section_statement,
3620 &s->wild_statement.children.head,
3621 fill, dot, relax, check_regions);
3622 break;
3624 case lang_data_statement_enum:
3626 unsigned int size = 0;
3628 s->data_statement.output_vma =
3629 dot - output_section_statement->bfd_section->vma;
3630 s->data_statement.output_section =
3631 output_section_statement->bfd_section;
3633 /* We might refer to provided symbols in the expression, and
3634 need to mark them as needed. */
3635 exp_fold_tree (s->data_statement.exp, abs_output_section,
3636 lang_allocating_phase_enum, dot, &dot);
3638 switch (s->data_statement.type)
3640 default:
3641 abort ();
3642 case QUAD:
3643 case SQUAD:
3644 size = QUAD_SIZE;
3645 break;
3646 case LONG:
3647 size = LONG_SIZE;
3648 break;
3649 case SHORT:
3650 size = SHORT_SIZE;
3651 break;
3652 case BYTE:
3653 size = BYTE_SIZE;
3654 break;
3656 if (size < TO_SIZE ((unsigned) 1))
3657 size = TO_SIZE ((unsigned) 1);
3658 dot += TO_ADDR (size);
3659 output_section_statement->bfd_section->size += size;
3661 break;
3663 case lang_reloc_statement_enum:
3665 int size;
3667 s->reloc_statement.output_vma =
3668 dot - output_section_statement->bfd_section->vma;
3669 s->reloc_statement.output_section =
3670 output_section_statement->bfd_section;
3671 size = bfd_get_reloc_size (s->reloc_statement.howto);
3672 dot += TO_ADDR (size);
3673 output_section_statement->bfd_section->size += size;
3675 break;
3677 case lang_wild_statement_enum:
3679 dot = lang_size_sections_1 (s->wild_statement.children.head,
3680 output_section_statement,
3681 &s->wild_statement.children.head,
3682 fill, dot, relax, check_regions);
3684 break;
3686 case lang_object_symbols_statement_enum:
3687 link_info.create_object_symbols_section =
3688 output_section_statement->bfd_section;
3689 break;
3690 case lang_output_statement_enum:
3691 case lang_target_statement_enum:
3692 break;
3693 case lang_input_section_enum:
3695 asection *i;
3697 i = (*prev)->input_section.section;
3698 if (relax)
3700 bfd_boolean again;
3702 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3703 einfo (_("%P%F: can't relax section: %E\n"));
3704 if (again)
3705 *relax = TRUE;
3707 dot = size_input_section (prev, output_section_statement,
3708 output_section_statement->fill, dot);
3710 break;
3711 case lang_input_statement_enum:
3712 break;
3713 case lang_fill_statement_enum:
3714 s->fill_statement.output_section =
3715 output_section_statement->bfd_section;
3717 fill = s->fill_statement.fill;
3718 break;
3719 case lang_assignment_statement_enum:
3721 bfd_vma newdot = dot;
3723 exp_fold_tree (s->assignment_statement.exp,
3724 output_section_statement,
3725 lang_allocating_phase_enum,
3726 dot,
3727 &newdot);
3729 if (newdot != dot)
3731 if (output_section_statement == abs_output_section)
3733 /* If we don't have an output section, then just adjust
3734 the default memory address. */
3735 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3736 FALSE)->current = newdot;
3738 else
3740 /* Insert a pad after this statement. We can't
3741 put the pad before when relaxing, in case the
3742 assignment references dot. */
3743 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3744 output_section_statement->bfd_section, dot);
3746 /* Don't neuter the pad below when relaxing. */
3747 s = s->header.next;
3750 /* If dot is advanced, this implies that the section should
3751 have space allocated to it, unless the user has explicitly
3752 stated that the section should never be loaded. */
3753 if (!(output_section_statement->flags
3754 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3755 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3757 dot = newdot;
3760 break;
3762 case lang_padding_statement_enum:
3763 /* If this is the first time lang_size_sections is called,
3764 we won't have any padding statements. If this is the
3765 second or later passes when relaxing, we should allow
3766 padding to shrink. If padding is needed on this pass, it
3767 will be added back in. */
3768 s->padding_statement.size = 0;
3770 /* Make sure output_offset is valid. If relaxation shrinks
3771 the section and this pad isn't needed, it's possible to
3772 have output_offset larger than the final size of the
3773 section. bfd_set_section_contents will complain even for
3774 a pad size of zero. */
3775 s->padding_statement.output_offset
3776 = dot - output_section_statement->bfd_section->vma;
3777 break;
3779 case lang_group_statement_enum:
3780 dot = lang_size_sections_1 (s->group_statement.children.head,
3781 output_section_statement,
3782 &s->group_statement.children.head,
3783 fill, dot, relax, check_regions);
3784 break;
3786 default:
3787 FAIL ();
3788 break;
3790 /* We can only get here when relaxing is turned on. */
3791 case lang_address_statement_enum:
3792 break;
3794 prev = &s->header.next;
3796 return dot;
3799 bfd_vma
3800 lang_size_sections
3801 (lang_statement_union_type *s,
3802 lang_output_section_statement_type *output_section_statement,
3803 lang_statement_union_type **prev,
3804 fill_type *fill,
3805 bfd_vma dot,
3806 bfd_boolean *relax,
3807 bfd_boolean check_regions)
3809 bfd_vma result;
3811 /* Callers of exp_fold_tree need to increment this. */
3812 lang_statement_iteration++;
3814 exp_data_seg.phase = exp_dataseg_none;
3815 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3816 dot, relax, check_regions);
3817 if (exp_data_seg.phase == exp_dataseg_end_seen
3818 && link_info.relro && exp_data_seg.relro_end)
3820 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3821 to put exp_data_seg.relro on a (common) page boundary. */
3822 bfd_vma old_min_base, relro_end, maxpage;
3824 exp_data_seg.phase = exp_dataseg_relro_adjust;
3825 old_min_base = exp_data_seg.min_base;
3826 maxpage = exp_data_seg.maxpagesize;
3827 exp_data_seg.base += (-exp_data_seg.relro_end
3828 & (exp_data_seg.pagesize - 1));
3829 /* Compute the expected PT_GNU_RELRO segment end. */
3830 relro_end = (exp_data_seg.relro_end + exp_data_seg.pagesize - 1)
3831 & ~(exp_data_seg.pagesize - 1);
3832 if (old_min_base + maxpage < exp_data_seg.base)
3834 exp_data_seg.base -= maxpage;
3835 relro_end -= maxpage;
3837 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3838 dot, relax, check_regions);
3839 if (exp_data_seg.relro_end > relro_end)
3841 /* The alignment of sections between DATA_SEGMENT_ALIGN
3842 and DATA_SEGMENT_RELRO_END caused huge padding to be
3843 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
3844 asection *sec;
3845 unsigned int max_alignment_power = 0;
3847 /* Find maximum alignment power of sections between
3848 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
3849 for (sec = output_bfd->sections; sec; sec = sec->next)
3850 if (sec->vma >= exp_data_seg.base
3851 && sec->vma < exp_data_seg.relro_end
3852 && sec->alignment_power > max_alignment_power)
3853 max_alignment_power = sec->alignment_power;
3855 if (((bfd_vma) 1 << max_alignment_power) < exp_data_seg.pagesize)
3857 if (exp_data_seg.base - (1 << max_alignment_power)
3858 < old_min_base)
3859 exp_data_seg.base += exp_data_seg.pagesize;
3860 exp_data_seg.base -= (1 << max_alignment_power);
3861 result = lang_size_sections_1 (s, output_section_statement,
3862 prev, fill, dot, relax,
3863 check_regions);
3866 link_info.relro_start = exp_data_seg.base;
3867 link_info.relro_end = exp_data_seg.relro_end;
3869 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3871 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3872 a page could be saved in the data segment. */
3873 bfd_vma first, last;
3875 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3876 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3877 if (first && last
3878 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3879 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3880 && first + last <= exp_data_seg.pagesize)
3882 exp_data_seg.phase = exp_dataseg_adjust;
3883 lang_statement_iteration++;
3884 result = lang_size_sections_1 (s, output_section_statement, prev,
3885 fill, dot, relax, check_regions);
3889 return result;
3892 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3894 static bfd_vma
3895 lang_do_assignments_1
3896 (lang_statement_union_type *s,
3897 lang_output_section_statement_type *output_section_statement,
3898 fill_type *fill,
3899 bfd_vma dot)
3901 for (; s != NULL; s = s->header.next)
3903 switch (s->header.type)
3905 case lang_constructors_statement_enum:
3906 dot = lang_do_assignments_1 (constructor_list.head,
3907 output_section_statement,
3908 fill,
3909 dot);
3910 break;
3912 case lang_output_section_statement_enum:
3914 lang_output_section_statement_type *os;
3916 os = &(s->output_section_statement);
3917 if (os->bfd_section != NULL)
3919 dot = os->bfd_section->vma;
3920 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3921 /* .tbss sections effectively have zero size. */
3922 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3923 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3924 || link_info.relocatable)
3925 dot += TO_ADDR (os->bfd_section->size);
3927 if (os->load_base)
3929 /* If nothing has been placed into the output section then
3930 it won't have a bfd_section. */
3931 if (os->bfd_section)
3933 os->bfd_section->lma
3934 = exp_get_abs_int (os->load_base, 0, "load base",
3935 lang_final_phase_enum);
3939 break;
3940 case lang_wild_statement_enum:
3942 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3943 output_section_statement,
3944 fill, dot);
3946 break;
3948 case lang_object_symbols_statement_enum:
3949 case lang_output_statement_enum:
3950 case lang_target_statement_enum:
3951 break;
3952 case lang_data_statement_enum:
3954 etree_value_type value;
3956 value = exp_fold_tree (s->data_statement.exp,
3957 abs_output_section,
3958 lang_final_phase_enum, dot, &dot);
3959 if (!value.valid_p)
3960 einfo (_("%F%P: invalid data statement\n"));
3961 s->data_statement.value
3962 = value.value + value.section->bfd_section->vma;
3965 unsigned int size;
3966 switch (s->data_statement.type)
3968 default:
3969 abort ();
3970 case QUAD:
3971 case SQUAD:
3972 size = QUAD_SIZE;
3973 break;
3974 case LONG:
3975 size = LONG_SIZE;
3976 break;
3977 case SHORT:
3978 size = SHORT_SIZE;
3979 break;
3980 case BYTE:
3981 size = BYTE_SIZE;
3982 break;
3984 if (size < TO_SIZE ((unsigned) 1))
3985 size = TO_SIZE ((unsigned) 1);
3986 dot += TO_ADDR (size);
3988 break;
3990 case lang_reloc_statement_enum:
3992 etree_value_type value;
3994 value = exp_fold_tree (s->reloc_statement.addend_exp,
3995 abs_output_section,
3996 lang_final_phase_enum, dot, &dot);
3997 s->reloc_statement.addend_value = value.value;
3998 if (!value.valid_p)
3999 einfo (_("%F%P: invalid reloc statement\n"));
4001 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
4002 break;
4004 case lang_input_section_enum:
4006 asection *in = s->input_section.section;
4008 if ((in->flags & SEC_EXCLUDE) == 0)
4009 dot += TO_ADDR (in->size);
4011 break;
4013 case lang_input_statement_enum:
4014 break;
4015 case lang_fill_statement_enum:
4016 fill = s->fill_statement.fill;
4017 break;
4018 case lang_assignment_statement_enum:
4020 exp_fold_tree (s->assignment_statement.exp,
4021 output_section_statement,
4022 lang_final_phase_enum,
4023 dot,
4024 &dot);
4027 break;
4028 case lang_padding_statement_enum:
4029 dot += TO_ADDR (s->padding_statement.size);
4030 break;
4032 case lang_group_statement_enum:
4033 dot = lang_do_assignments_1 (s->group_statement.children.head,
4034 output_section_statement,
4035 fill, dot);
4037 break;
4039 default:
4040 FAIL ();
4041 break;
4042 case lang_address_statement_enum:
4043 break;
4047 return dot;
4050 void
4051 lang_do_assignments
4052 (lang_statement_union_type *s,
4053 lang_output_section_statement_type *output_section_statement,
4054 fill_type *fill,
4055 bfd_vma dot)
4057 /* Callers of exp_fold_tree need to increment this. */
4058 lang_statement_iteration++;
4059 lang_do_assignments_1 (s, output_section_statement, fill, dot);
4062 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4063 operator .startof. (section_name), it produces an undefined symbol
4064 .startof.section_name. Similarly, when it sees
4065 .sizeof. (section_name), it produces an undefined symbol
4066 .sizeof.section_name. For all the output sections, we look for
4067 such symbols, and set them to the correct value. */
4069 static void
4070 lang_set_startof (void)
4072 asection *s;
4074 if (link_info.relocatable)
4075 return;
4077 for (s = output_bfd->sections; s != NULL; s = s->next)
4079 const char *secname;
4080 char *buf;
4081 struct bfd_link_hash_entry *h;
4083 secname = bfd_get_section_name (output_bfd, s);
4084 buf = xmalloc (10 + strlen (secname));
4086 sprintf (buf, ".startof.%s", secname);
4087 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4088 if (h != NULL && h->type == bfd_link_hash_undefined)
4090 h->type = bfd_link_hash_defined;
4091 h->u.def.value = bfd_get_section_vma (output_bfd, s);
4092 h->u.def.section = bfd_abs_section_ptr;
4095 sprintf (buf, ".sizeof.%s", secname);
4096 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4097 if (h != NULL && h->type == bfd_link_hash_undefined)
4099 h->type = bfd_link_hash_defined;
4100 h->u.def.value = TO_ADDR (s->size);
4101 h->u.def.section = bfd_abs_section_ptr;
4104 free (buf);
4108 static void
4109 lang_finish (void)
4111 struct bfd_link_hash_entry *h;
4112 bfd_boolean warn;
4114 if (link_info.relocatable || link_info.shared)
4115 warn = FALSE;
4116 else
4117 warn = TRUE;
4119 if (entry_symbol.name == NULL)
4121 /* No entry has been specified. Look for start, but don't warn
4122 if we don't find it. */
4123 entry_symbol.name = "start";
4124 warn = FALSE;
4127 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
4128 FALSE, FALSE, TRUE);
4129 if (h != NULL
4130 && (h->type == bfd_link_hash_defined
4131 || h->type == bfd_link_hash_defweak)
4132 && h->u.def.section->output_section != NULL)
4134 bfd_vma val;
4136 val = (h->u.def.value
4137 + bfd_get_section_vma (output_bfd,
4138 h->u.def.section->output_section)
4139 + h->u.def.section->output_offset);
4140 if (! bfd_set_start_address (output_bfd, val))
4141 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
4143 else
4145 bfd_vma val;
4146 const char *send;
4148 /* We couldn't find the entry symbol. Try parsing it as a
4149 number. */
4150 val = bfd_scan_vma (entry_symbol.name, &send, 0);
4151 if (*send == '\0')
4153 if (! bfd_set_start_address (output_bfd, val))
4154 einfo (_("%P%F: can't set start address\n"));
4156 else
4158 asection *ts;
4160 /* Can't find the entry symbol, and it's not a number. Use
4161 the first address in the text section. */
4162 ts = bfd_get_section_by_name (output_bfd, entry_section);
4163 if (ts != NULL)
4165 if (warn)
4166 einfo (_("%P: warning: cannot find entry symbol %s;"
4167 " defaulting to %V\n"),
4168 entry_symbol.name,
4169 bfd_get_section_vma (output_bfd, ts));
4170 if (! bfd_set_start_address (output_bfd,
4171 bfd_get_section_vma (output_bfd,
4172 ts)))
4173 einfo (_("%P%F: can't set start address\n"));
4175 else
4177 if (warn)
4178 einfo (_("%P: warning: cannot find entry symbol %s;"
4179 " not setting start address\n"),
4180 entry_symbol.name);
4185 /* Don't bfd_hash_table_free (&lang_definedness_table);
4186 map file output may result in a call of lang_track_definedness. */
4189 /* This is a small function used when we want to ignore errors from
4190 BFD. */
4192 static void
4193 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
4195 /* Don't do anything. */
4198 /* Check that the architecture of all the input files is compatible
4199 with the output file. Also call the backend to let it do any
4200 other checking that is needed. */
4202 static void
4203 lang_check (void)
4205 lang_statement_union_type *file;
4206 bfd *input_bfd;
4207 const bfd_arch_info_type *compatible;
4209 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
4211 input_bfd = file->input_statement.the_bfd;
4212 compatible
4213 = bfd_arch_get_compatible (input_bfd, output_bfd,
4214 command_line.accept_unknown_input_arch);
4216 /* In general it is not possible to perform a relocatable
4217 link between differing object formats when the input
4218 file has relocations, because the relocations in the
4219 input format may not have equivalent representations in
4220 the output format (and besides BFD does not translate
4221 relocs for other link purposes than a final link). */
4222 if ((link_info.relocatable || link_info.emitrelocations)
4223 && (compatible == NULL
4224 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
4225 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
4227 einfo (_("%P%F: Relocatable linking with relocations from"
4228 " format %s (%B) to format %s (%B) is not supported\n"),
4229 bfd_get_target (input_bfd), input_bfd,
4230 bfd_get_target (output_bfd), output_bfd);
4231 /* einfo with %F exits. */
4234 if (compatible == NULL)
4236 if (command_line.warn_mismatch)
4237 einfo (_("%P: warning: %s architecture of input file `%B'"
4238 " is incompatible with %s output\n"),
4239 bfd_printable_name (input_bfd), input_bfd,
4240 bfd_printable_name (output_bfd));
4242 else if (bfd_count_sections (input_bfd))
4244 /* If the input bfd has no contents, it shouldn't set the
4245 private data of the output bfd. */
4247 bfd_error_handler_type pfn = NULL;
4249 /* If we aren't supposed to warn about mismatched input
4250 files, temporarily set the BFD error handler to a
4251 function which will do nothing. We still want to call
4252 bfd_merge_private_bfd_data, since it may set up
4253 information which is needed in the output file. */
4254 if (! command_line.warn_mismatch)
4255 pfn = bfd_set_error_handler (ignore_bfd_errors);
4256 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
4258 if (command_line.warn_mismatch)
4259 einfo (_("%P%X: failed to merge target specific data"
4260 " of file %B\n"), input_bfd);
4262 if (! command_line.warn_mismatch)
4263 bfd_set_error_handler (pfn);
4268 /* Look through all the global common symbols and attach them to the
4269 correct section. The -sort-common command line switch may be used
4270 to roughly sort the entries by size. */
4272 static void
4273 lang_common (void)
4275 if (command_line.inhibit_common_definition)
4276 return;
4277 if (link_info.relocatable
4278 && ! command_line.force_common_definition)
4279 return;
4281 if (! config.sort_common)
4282 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
4283 else
4285 int power;
4287 for (power = 4; power >= 0; power--)
4288 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
4292 /* Place one common symbol in the correct section. */
4294 static bfd_boolean
4295 lang_one_common (struct bfd_link_hash_entry *h, void *info)
4297 unsigned int power_of_two;
4298 bfd_vma size;
4299 asection *section;
4301 if (h->type != bfd_link_hash_common)
4302 return TRUE;
4304 size = h->u.c.size;
4305 power_of_two = h->u.c.p->alignment_power;
4307 if (config.sort_common
4308 && power_of_two < (unsigned int) *(int *) info)
4309 return TRUE;
4311 section = h->u.c.p->section;
4313 /* Increase the size of the section to align the common sym. */
4314 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
4315 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
4317 /* Adjust the alignment if necessary. */
4318 if (power_of_two > section->alignment_power)
4319 section->alignment_power = power_of_two;
4321 /* Change the symbol from common to defined. */
4322 h->type = bfd_link_hash_defined;
4323 h->u.def.section = section;
4324 h->u.def.value = section->size;
4326 /* Increase the size of the section. */
4327 section->size += size;
4329 /* Make sure the section is allocated in memory, and make sure that
4330 it is no longer a common section. */
4331 section->flags |= SEC_ALLOC;
4332 section->flags &= ~SEC_IS_COMMON;
4334 if (config.map_file != NULL)
4336 static bfd_boolean header_printed;
4337 int len;
4338 char *name;
4339 char buf[50];
4341 if (! header_printed)
4343 minfo (_("\nAllocating common symbols\n"));
4344 minfo (_("Common symbol size file\n\n"));
4345 header_printed = TRUE;
4348 name = demangle (h->root.string);
4349 minfo ("%s", name);
4350 len = strlen (name);
4351 free (name);
4353 if (len >= 19)
4355 print_nl ();
4356 len = 0;
4358 while (len < 20)
4360 print_space ();
4361 ++len;
4364 minfo ("0x");
4365 if (size <= 0xffffffff)
4366 sprintf (buf, "%lx", (unsigned long) size);
4367 else
4368 sprintf_vma (buf, size);
4369 minfo ("%s", buf);
4370 len = strlen (buf);
4372 while (len < 16)
4374 print_space ();
4375 ++len;
4378 minfo ("%B\n", section->owner);
4381 return TRUE;
4384 /* Run through the input files and ensure that every input section has
4385 somewhere to go. If one is found without a destination then create
4386 an input request and place it into the statement tree. */
4388 static void
4389 lang_place_orphans (void)
4391 LANG_FOR_EACH_INPUT_STATEMENT (file)
4393 asection *s;
4395 for (s = file->the_bfd->sections; s != NULL; s = s->next)
4397 if (s->output_section == NULL)
4399 /* This section of the file is not attached, root
4400 around for a sensible place for it to go. */
4402 if (file->just_syms_flag)
4403 bfd_link_just_syms (file->the_bfd, s, &link_info);
4404 else if ((s->flags & SEC_EXCLUDE) != 0)
4405 s->output_section = bfd_abs_section_ptr;
4406 else if (strcmp (s->name, "COMMON") == 0)
4408 /* This is a lonely common section which must have
4409 come from an archive. We attach to the section
4410 with the wildcard. */
4411 if (! link_info.relocatable
4412 || command_line.force_common_definition)
4414 if (default_common_section == NULL)
4416 default_common_section =
4417 lang_output_section_statement_lookup (".bss");
4420 lang_add_section (&default_common_section->children, s,
4421 default_common_section, file);
4424 else if (ldemul_place_orphan (file, s))
4426 else
4428 lang_output_section_statement_type *os;
4430 os = lang_output_section_statement_lookup (s->name);
4431 lang_add_section (&os->children, s, os, file);
4438 void
4439 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
4441 flagword *ptr_flags;
4443 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
4444 while (*flags)
4446 switch (*flags)
4448 case 'A': case 'a':
4449 *ptr_flags |= SEC_ALLOC;
4450 break;
4452 case 'R': case 'r':
4453 *ptr_flags |= SEC_READONLY;
4454 break;
4456 case 'W': case 'w':
4457 *ptr_flags |= SEC_DATA;
4458 break;
4460 case 'X': case 'x':
4461 *ptr_flags |= SEC_CODE;
4462 break;
4464 case 'L': case 'l':
4465 case 'I': case 'i':
4466 *ptr_flags |= SEC_LOAD;
4467 break;
4469 default:
4470 einfo (_("%P%F: invalid syntax in flags\n"));
4471 break;
4473 flags++;
4477 /* Call a function on each input file. This function will be called
4478 on an archive, but not on the elements. */
4480 void
4481 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
4483 lang_input_statement_type *f;
4485 for (f = (lang_input_statement_type *) input_file_chain.head;
4486 f != NULL;
4487 f = (lang_input_statement_type *) f->next_real_file)
4488 func (f);
4491 /* Call a function on each file. The function will be called on all
4492 the elements of an archive which are included in the link, but will
4493 not be called on the archive file itself. */
4495 void
4496 lang_for_each_file (void (*func) (lang_input_statement_type *))
4498 LANG_FOR_EACH_INPUT_STATEMENT (f)
4500 func (f);
4504 void
4505 ldlang_add_file (lang_input_statement_type *entry)
4507 bfd **pp;
4509 lang_statement_append (&file_chain,
4510 (lang_statement_union_type *) entry,
4511 &entry->next);
4513 /* The BFD linker needs to have a list of all input BFDs involved in
4514 a link. */
4515 ASSERT (entry->the_bfd->link_next == NULL);
4516 ASSERT (entry->the_bfd != output_bfd);
4517 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
4519 *pp = entry->the_bfd;
4520 entry->the_bfd->usrdata = entry;
4521 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4523 /* Look through the sections and check for any which should not be
4524 included in the link. We need to do this now, so that we can
4525 notice when the backend linker tries to report multiple
4526 definition errors for symbols which are in sections we aren't
4527 going to link. FIXME: It might be better to entirely ignore
4528 symbols which are defined in sections which are going to be
4529 discarded. This would require modifying the backend linker for
4530 each backend which might set the SEC_LINK_ONCE flag. If we do
4531 this, we should probably handle SEC_EXCLUDE in the same way. */
4533 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4536 void
4537 lang_add_output (const char *name, int from_script)
4539 /* Make -o on command line override OUTPUT in script. */
4540 if (!had_output_filename || !from_script)
4542 output_filename = name;
4543 had_output_filename = TRUE;
4547 static lang_output_section_statement_type *current_section;
4549 static int
4550 topower (int x)
4552 unsigned int i = 1;
4553 int l;
4555 if (x < 0)
4556 return -1;
4558 for (l = 0; l < 32; l++)
4560 if (i >= (unsigned int) x)
4561 return l;
4562 i <<= 1;
4565 return 0;
4568 lang_output_section_statement_type *
4569 lang_enter_output_section_statement (const char *output_section_statement_name,
4570 etree_type *address_exp,
4571 enum section_type sectype,
4572 etree_type *align,
4573 etree_type *subalign,
4574 etree_type *ebase,
4575 int constraint)
4577 lang_output_section_statement_type *os;
4579 current_section =
4580 os =
4581 lang_output_section_statement_lookup_1 (output_section_statement_name,
4582 constraint);
4584 /* Make next things chain into subchain of this. */
4586 if (os->addr_tree == NULL)
4588 os->addr_tree = address_exp;
4590 os->sectype = sectype;
4591 if (sectype != noload_section)
4592 os->flags = SEC_NO_FLAGS;
4593 else
4594 os->flags = SEC_NEVER_LOAD;
4595 os->block_value = 1;
4596 stat_ptr = &os->children;
4598 os->subsection_alignment =
4599 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4600 os->section_alignment =
4601 topower (exp_get_value_int (align, -1, "section alignment", 0));
4603 os->load_base = ebase;
4604 return os;
4607 void
4608 lang_final (void)
4610 lang_output_statement_type *new =
4611 new_stat (lang_output_statement, stat_ptr);
4613 new->name = output_filename;
4616 /* Reset the current counters in the regions. */
4618 void
4619 lang_reset_memory_regions (void)
4621 lang_memory_region_type *p = lang_memory_region_list;
4622 asection *o;
4624 for (p = lang_memory_region_list; p != NULL; p = p->next)
4626 p->old_length = (bfd_size_type) (p->current - p->origin);
4627 p->current = p->origin;
4630 for (o = output_bfd->sections; o != NULL; o = o->next)
4632 /* Save the last size for possible use by bfd_relax_section. */
4633 o->rawsize = o->size;
4634 o->size = 0;
4638 /* Worker for lang_gc_sections_1. */
4640 static void
4641 gc_section_callback (lang_wild_statement_type *ptr,
4642 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4643 asection *section,
4644 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4645 void *data ATTRIBUTE_UNUSED)
4647 /* If the wild pattern was marked KEEP, the member sections
4648 should be as well. */
4649 if (ptr->keep_sections)
4650 section->flags |= SEC_KEEP;
4653 /* Iterate over sections marking them against GC. */
4655 static void
4656 lang_gc_sections_1 (lang_statement_union_type *s)
4658 for (; s != NULL; s = s->header.next)
4660 switch (s->header.type)
4662 case lang_wild_statement_enum:
4663 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4664 break;
4665 case lang_constructors_statement_enum:
4666 lang_gc_sections_1 (constructor_list.head);
4667 break;
4668 case lang_output_section_statement_enum:
4669 lang_gc_sections_1 (s->output_section_statement.children.head);
4670 break;
4671 case lang_group_statement_enum:
4672 lang_gc_sections_1 (s->group_statement.children.head);
4673 break;
4674 default:
4675 break;
4680 static void
4681 lang_gc_sections (void)
4683 struct bfd_link_hash_entry *h;
4684 ldlang_undef_chain_list_type *ulist;
4686 /* Keep all sections so marked in the link script. */
4688 lang_gc_sections_1 (statement_list.head);
4690 /* Keep all sections containing symbols undefined on the command-line,
4691 and the section containing the entry symbol. */
4693 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4695 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4696 FALSE, FALSE, FALSE);
4698 if (h != NULL
4699 && (h->type == bfd_link_hash_defined
4700 || h->type == bfd_link_hash_defweak)
4701 && ! bfd_is_abs_section (h->u.def.section))
4703 h->u.def.section->flags |= SEC_KEEP;
4707 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4708 the special case of debug info. (See bfd/stabs.c)
4709 Twiddle the flag here, to simplify later linker code. */
4710 if (link_info.relocatable)
4712 LANG_FOR_EACH_INPUT_STATEMENT (f)
4714 asection *sec;
4715 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4716 if ((sec->flags & SEC_DEBUGGING) == 0)
4717 sec->flags &= ~SEC_EXCLUDE;
4721 if (command_line.gc_sections)
4722 bfd_gc_sections (output_bfd, &link_info);
4725 void
4726 lang_process (void)
4728 current_target = default_target;
4730 /* Open the output file. */
4731 lang_for_each_statement (ldlang_open_output);
4732 init_opb ();
4734 ldemul_create_output_section_statements ();
4736 /* Add to the hash table all undefineds on the command line. */
4737 lang_place_undefineds ();
4739 if (!bfd_section_already_linked_table_init ())
4740 einfo (_("%P%F: Failed to create hash table\n"));
4742 /* Create a bfd for each input file. */
4743 current_target = default_target;
4744 open_input_bfds (statement_list.head, FALSE);
4746 link_info.gc_sym_list = &entry_symbol;
4747 if (entry_symbol.name == NULL)
4748 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4750 ldemul_after_open ();
4752 bfd_section_already_linked_table_free ();
4754 /* Make sure that we're not mixing architectures. We call this
4755 after all the input files have been opened, but before we do any
4756 other processing, so that any operations merge_private_bfd_data
4757 does on the output file will be known during the rest of the
4758 link. */
4759 lang_check ();
4761 /* Handle .exports instead of a version script if we're told to do so. */
4762 if (command_line.version_exports_section)
4763 lang_do_version_exports_section ();
4765 /* Build all sets based on the information gathered from the input
4766 files. */
4767 ldctor_build_sets ();
4769 /* Remove unreferenced sections if asked to. */
4770 lang_gc_sections ();
4772 /* Size up the common data. */
4773 lang_common ();
4775 /* Update wild statements. */
4776 update_wild_statements (statement_list.head);
4778 /* Run through the contours of the script and attach input sections
4779 to the correct output sections. */
4780 map_input_to_output_sections (statement_list.head, NULL, NULL);
4782 /* Find any sections not attached explicitly and handle them. */
4783 lang_place_orphans ();
4785 if (! link_info.relocatable)
4787 asection *found;
4789 /* Merge SEC_MERGE sections. This has to be done after GC of
4790 sections, so that GCed sections are not merged, but before
4791 assigning dynamic symbols, since removing whole input sections
4792 is hard then. */
4793 bfd_merge_sections (output_bfd, &link_info);
4795 /* Look for a text section and set the readonly attribute in it. */
4796 found = bfd_get_section_by_name (output_bfd, ".text");
4798 if (found != NULL)
4800 if (config.text_read_only)
4801 found->flags |= SEC_READONLY;
4802 else
4803 found->flags &= ~SEC_READONLY;
4807 /* Do anything special before sizing sections. This is where ELF
4808 and other back-ends size dynamic sections. */
4809 ldemul_before_allocation ();
4811 if (!link_info.relocatable)
4812 strip_excluded_output_sections ();
4814 /* We must record the program headers before we try to fix the
4815 section positions, since they will affect SIZEOF_HEADERS. */
4816 lang_record_phdrs ();
4818 /* Size up the sections. */
4819 lang_size_sections (statement_list.head, abs_output_section,
4820 &statement_list.head, 0, 0, NULL,
4821 command_line.relax ? FALSE : TRUE);
4823 /* Now run around and relax if we can. */
4824 if (command_line.relax)
4826 /* Keep relaxing until bfd_relax_section gives up. */
4827 bfd_boolean relax_again;
4831 relax_again = FALSE;
4833 /* Note: pe-dll.c does something like this also. If you find
4834 you need to change this code, you probably need to change
4835 pe-dll.c also. DJ */
4837 /* Do all the assignments with our current guesses as to
4838 section sizes. */
4839 lang_do_assignments (statement_list.head, abs_output_section,
4840 NULL, 0);
4842 /* We must do this after lang_do_assignments, because it uses
4843 size. */
4844 lang_reset_memory_regions ();
4846 /* Perform another relax pass - this time we know where the
4847 globals are, so can make a better guess. */
4848 lang_size_sections (statement_list.head, abs_output_section,
4849 &statement_list.head, 0, 0, &relax_again, FALSE);
4851 /* If the normal relax is done and the relax finalize pass
4852 is not performed yet, we perform another relax pass. */
4853 if (!relax_again && link_info.need_relax_finalize)
4855 link_info.need_relax_finalize = FALSE;
4856 relax_again = TRUE;
4859 while (relax_again);
4861 /* Final extra sizing to report errors. */
4862 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4863 lang_reset_memory_regions ();
4864 lang_size_sections (statement_list.head, abs_output_section,
4865 &statement_list.head, 0, 0, NULL, TRUE);
4868 /* See if anything special should be done now we know how big
4869 everything is. */
4870 ldemul_after_allocation ();
4872 /* Fix any .startof. or .sizeof. symbols. */
4873 lang_set_startof ();
4875 /* Do all the assignments, now that we know the final resting places
4876 of all the symbols. */
4878 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4880 /* Make sure that the section addresses make sense. */
4881 if (! link_info.relocatable
4882 && command_line.check_section_addresses)
4883 lang_check_section_addresses ();
4885 /* Final stuffs. */
4887 ldemul_finish ();
4888 lang_finish ();
4891 /* EXPORTED TO YACC */
4893 void
4894 lang_add_wild (struct wildcard_spec *filespec,
4895 struct wildcard_list *section_list,
4896 bfd_boolean keep_sections)
4898 struct wildcard_list *curr, *next;
4899 lang_wild_statement_type *new;
4901 /* Reverse the list as the parser puts it back to front. */
4902 for (curr = section_list, section_list = NULL;
4903 curr != NULL;
4904 section_list = curr, curr = next)
4906 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4907 placed_commons = TRUE;
4909 next = curr->next;
4910 curr->next = section_list;
4913 if (filespec != NULL && filespec->name != NULL)
4915 if (strcmp (filespec->name, "*") == 0)
4916 filespec->name = NULL;
4917 else if (! wildcardp (filespec->name))
4918 lang_has_input_file = TRUE;
4921 new = new_stat (lang_wild_statement, stat_ptr);
4922 new->filename = NULL;
4923 new->filenames_sorted = FALSE;
4924 if (filespec != NULL)
4926 new->filename = filespec->name;
4927 new->filenames_sorted = filespec->sorted == by_name;
4929 new->section_list = section_list;
4930 new->keep_sections = keep_sections;
4931 lang_list_init (&new->children);
4934 void
4935 lang_section_start (const char *name, etree_type *address,
4936 const segment_type *segment)
4938 lang_address_statement_type *ad;
4940 ad = new_stat (lang_address_statement, stat_ptr);
4941 ad->section_name = name;
4942 ad->address = address;
4943 ad->segment = segment;
4946 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4947 because of a -e argument on the command line, or zero if this is
4948 called by ENTRY in a linker script. Command line arguments take
4949 precedence. */
4951 void
4952 lang_add_entry (const char *name, bfd_boolean cmdline)
4954 if (entry_symbol.name == NULL
4955 || cmdline
4956 || ! entry_from_cmdline)
4958 entry_symbol.name = name;
4959 entry_from_cmdline = cmdline;
4963 void
4964 lang_add_target (const char *name)
4966 lang_target_statement_type *new = new_stat (lang_target_statement,
4967 stat_ptr);
4969 new->target = name;
4973 void
4974 lang_add_map (const char *name)
4976 while (*name)
4978 switch (*name)
4980 case 'F':
4981 map_option_f = TRUE;
4982 break;
4984 name++;
4988 void
4989 lang_add_fill (fill_type *fill)
4991 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4992 stat_ptr);
4994 new->fill = fill;
4997 void
4998 lang_add_data (int type, union etree_union *exp)
5001 lang_data_statement_type *new = new_stat (lang_data_statement,
5002 stat_ptr);
5004 new->exp = exp;
5005 new->type = type;
5009 /* Create a new reloc statement. RELOC is the BFD relocation type to
5010 generate. HOWTO is the corresponding howto structure (we could
5011 look this up, but the caller has already done so). SECTION is the
5012 section to generate a reloc against, or NAME is the name of the
5013 symbol to generate a reloc against. Exactly one of SECTION and
5014 NAME must be NULL. ADDEND is an expression for the addend. */
5016 void
5017 lang_add_reloc (bfd_reloc_code_real_type reloc,
5018 reloc_howto_type *howto,
5019 asection *section,
5020 const char *name,
5021 union etree_union *addend)
5023 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5025 p->reloc = reloc;
5026 p->howto = howto;
5027 p->section = section;
5028 p->name = name;
5029 p->addend_exp = addend;
5031 p->addend_value = 0;
5032 p->output_section = NULL;
5033 p->output_vma = 0;
5036 lang_assignment_statement_type *
5037 lang_add_assignment (etree_type *exp)
5039 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
5040 stat_ptr);
5042 new->exp = exp;
5043 return new;
5046 void
5047 lang_add_attribute (enum statement_enum attribute)
5049 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
5052 void
5053 lang_startup (const char *name)
5055 if (startup_file != NULL)
5057 einfo (_("%P%Fmultiple STARTUP files\n"));
5059 first_file->filename = name;
5060 first_file->local_sym_name = name;
5061 first_file->real = TRUE;
5063 startup_file = name;
5066 void
5067 lang_float (bfd_boolean maybe)
5069 lang_float_flag = maybe;
5073 /* Work out the load- and run-time regions from a script statement, and
5074 store them in *LMA_REGION and *REGION respectively.
5076 MEMSPEC is the name of the run-time region, or the value of
5077 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5078 LMA_MEMSPEC is the name of the load-time region, or null if the
5079 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5080 had an explicit load address.
5082 It is an error to specify both a load region and a load address. */
5084 static void
5085 lang_get_regions (lang_memory_region_type **region,
5086 lang_memory_region_type **lma_region,
5087 const char *memspec,
5088 const char *lma_memspec,
5089 bfd_boolean have_lma,
5090 bfd_boolean have_vma)
5092 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
5094 /* If no runtime region or VMA has been specified, but the load region
5095 has been specified, then use the load region for the runtime region
5096 as well. */
5097 if (lma_memspec != NULL
5098 && ! have_vma
5099 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
5100 *region = *lma_region;
5101 else
5102 *region = lang_memory_region_lookup (memspec, FALSE);
5104 if (have_lma && lma_memspec != 0)
5105 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5108 void
5109 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
5110 lang_output_section_phdr_list *phdrs,
5111 const char *lma_memspec)
5113 lang_get_regions (&current_section->region,
5114 &current_section->lma_region,
5115 memspec, lma_memspec,
5116 current_section->load_base != NULL,
5117 current_section->addr_tree != NULL);
5118 current_section->fill = fill;
5119 current_section->phdrs = phdrs;
5120 stat_ptr = &statement_list;
5123 /* Create an absolute symbol with the given name with the value of the
5124 address of first byte of the section named.
5126 If the symbol already exists, then do nothing. */
5128 void
5129 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
5131 struct bfd_link_hash_entry *h;
5133 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5134 if (h == NULL)
5135 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5137 if (h->type == bfd_link_hash_new
5138 || h->type == bfd_link_hash_undefined)
5140 asection *sec;
5142 h->type = bfd_link_hash_defined;
5144 sec = bfd_get_section_by_name (output_bfd, secname);
5145 if (sec == NULL)
5146 h->u.def.value = 0;
5147 else
5148 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
5150 h->u.def.section = bfd_abs_section_ptr;
5154 /* Create an absolute symbol with the given name with the value of the
5155 address of the first byte after the end of the section named.
5157 If the symbol already exists, then do nothing. */
5159 void
5160 lang_abs_symbol_at_end_of (const char *secname, const char *name)
5162 struct bfd_link_hash_entry *h;
5164 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5165 if (h == NULL)
5166 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5168 if (h->type == bfd_link_hash_new
5169 || h->type == bfd_link_hash_undefined)
5171 asection *sec;
5173 h->type = bfd_link_hash_defined;
5175 sec = bfd_get_section_by_name (output_bfd, secname);
5176 if (sec == NULL)
5177 h->u.def.value = 0;
5178 else
5179 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
5180 + TO_ADDR (sec->size));
5182 h->u.def.section = bfd_abs_section_ptr;
5186 void
5187 lang_statement_append (lang_statement_list_type *list,
5188 lang_statement_union_type *element,
5189 lang_statement_union_type **field)
5191 *(list->tail) = element;
5192 list->tail = field;
5195 /* Set the output format type. -oformat overrides scripts. */
5197 void
5198 lang_add_output_format (const char *format,
5199 const char *big,
5200 const char *little,
5201 int from_script)
5203 if (output_target == NULL || !from_script)
5205 if (command_line.endian == ENDIAN_BIG
5206 && big != NULL)
5207 format = big;
5208 else if (command_line.endian == ENDIAN_LITTLE
5209 && little != NULL)
5210 format = little;
5212 output_target = format;
5216 /* Enter a group. This creates a new lang_group_statement, and sets
5217 stat_ptr to build new statements within the group. */
5219 void
5220 lang_enter_group (void)
5222 lang_group_statement_type *g;
5224 g = new_stat (lang_group_statement, stat_ptr);
5225 lang_list_init (&g->children);
5226 stat_ptr = &g->children;
5229 /* Leave a group. This just resets stat_ptr to start writing to the
5230 regular list of statements again. Note that this will not work if
5231 groups can occur inside anything else which can adjust stat_ptr,
5232 but currently they can't. */
5234 void
5235 lang_leave_group (void)
5237 stat_ptr = &statement_list;
5240 /* Add a new program header. This is called for each entry in a PHDRS
5241 command in a linker script. */
5243 void
5244 lang_new_phdr (const char *name,
5245 etree_type *type,
5246 bfd_boolean filehdr,
5247 bfd_boolean phdrs,
5248 etree_type *at,
5249 etree_type *flags)
5251 struct lang_phdr *n, **pp;
5253 n = stat_alloc (sizeof (struct lang_phdr));
5254 n->next = NULL;
5255 n->name = name;
5256 n->type = exp_get_value_int (type, 0, "program header type",
5257 lang_final_phase_enum);
5258 n->filehdr = filehdr;
5259 n->phdrs = phdrs;
5260 n->at = at;
5261 n->flags = flags;
5263 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
5265 *pp = n;
5268 /* Record the program header information in the output BFD. FIXME: We
5269 should not be calling an ELF specific function here. */
5271 static void
5272 lang_record_phdrs (void)
5274 unsigned int alc;
5275 asection **secs;
5276 lang_output_section_phdr_list *last;
5277 struct lang_phdr *l;
5278 lang_output_section_statement_type *os;
5280 alc = 10;
5281 secs = xmalloc (alc * sizeof (asection *));
5282 last = NULL;
5283 for (l = lang_phdr_list; l != NULL; l = l->next)
5285 unsigned int c;
5286 flagword flags;
5287 bfd_vma at;
5289 c = 0;
5290 for (os = &lang_output_section_statement.head->output_section_statement;
5291 os != NULL;
5292 os = os->next)
5294 lang_output_section_phdr_list *pl;
5296 if (os->constraint == -1)
5297 continue;
5299 pl = os->phdrs;
5300 if (pl != NULL)
5301 last = pl;
5302 else
5304 if (os->sectype == noload_section
5305 || os->bfd_section == NULL
5306 || (os->bfd_section->flags & SEC_ALLOC) == 0)
5307 continue;
5308 pl = last;
5311 if (os->bfd_section == NULL)
5312 continue;
5314 for (; pl != NULL; pl = pl->next)
5316 if (strcmp (pl->name, l->name) == 0)
5318 if (c >= alc)
5320 alc *= 2;
5321 secs = xrealloc (secs, alc * sizeof (asection *));
5323 secs[c] = os->bfd_section;
5324 ++c;
5325 pl->used = TRUE;
5330 if (l->flags == NULL)
5331 flags = 0;
5332 else
5333 flags = exp_get_vma (l->flags, 0, "phdr flags",
5334 lang_final_phase_enum);
5336 if (l->at == NULL)
5337 at = 0;
5338 else
5339 at = exp_get_vma (l->at, 0, "phdr load address",
5340 lang_final_phase_enum);
5342 if (! bfd_record_phdr (output_bfd, l->type,
5343 l->flags != NULL, flags, l->at != NULL,
5344 at, l->filehdr, l->phdrs, c, secs))
5345 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
5348 free (secs);
5350 /* Make sure all the phdr assignments succeeded. */
5351 for (os = &lang_output_section_statement.head->output_section_statement;
5352 os != NULL;
5353 os = os->next)
5355 lang_output_section_phdr_list *pl;
5357 if (os->constraint == -1
5358 || os->bfd_section == NULL)
5359 continue;
5361 for (pl = os->phdrs;
5362 pl != NULL;
5363 pl = pl->next)
5364 if (! pl->used && strcmp (pl->name, "NONE") != 0)
5365 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
5366 os->name, pl->name);
5370 /* Record a list of sections which may not be cross referenced. */
5372 void
5373 lang_add_nocrossref (lang_nocrossref_type *l)
5375 struct lang_nocrossrefs *n;
5377 n = xmalloc (sizeof *n);
5378 n->next = nocrossref_list;
5379 n->list = l;
5380 nocrossref_list = n;
5382 /* Set notice_all so that we get informed about all symbols. */
5383 link_info.notice_all = TRUE;
5386 /* Overlay handling. We handle overlays with some static variables. */
5388 /* The overlay virtual address. */
5389 static etree_type *overlay_vma;
5390 /* And subsection alignment. */
5391 static etree_type *overlay_subalign;
5393 /* An expression for the maximum section size seen so far. */
5394 static etree_type *overlay_max;
5396 /* A list of all the sections in this overlay. */
5398 struct overlay_list {
5399 struct overlay_list *next;
5400 lang_output_section_statement_type *os;
5403 static struct overlay_list *overlay_list;
5405 /* Start handling an overlay. */
5407 void
5408 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
5410 /* The grammar should prevent nested overlays from occurring. */
5411 ASSERT (overlay_vma == NULL
5412 && overlay_subalign == NULL
5413 && overlay_max == NULL);
5415 overlay_vma = vma_expr;
5416 overlay_subalign = subalign;
5419 /* Start a section in an overlay. We handle this by calling
5420 lang_enter_output_section_statement with the correct VMA.
5421 lang_leave_overlay sets up the LMA and memory regions. */
5423 void
5424 lang_enter_overlay_section (const char *name)
5426 struct overlay_list *n;
5427 etree_type *size;
5429 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5430 0, overlay_subalign, 0, 0);
5432 /* If this is the first section, then base the VMA of future
5433 sections on this one. This will work correctly even if `.' is
5434 used in the addresses. */
5435 if (overlay_list == NULL)
5436 overlay_vma = exp_nameop (ADDR, name);
5438 /* Remember the section. */
5439 n = xmalloc (sizeof *n);
5440 n->os = current_section;
5441 n->next = overlay_list;
5442 overlay_list = n;
5444 size = exp_nameop (SIZEOF, name);
5446 /* Arrange to work out the maximum section end address. */
5447 if (overlay_max == NULL)
5448 overlay_max = size;
5449 else
5450 overlay_max = exp_binop (MAX_K, overlay_max, size);
5453 /* Finish a section in an overlay. There isn't any special to do
5454 here. */
5456 void
5457 lang_leave_overlay_section (fill_type *fill,
5458 lang_output_section_phdr_list *phdrs)
5460 const char *name;
5461 char *clean, *s2;
5462 const char *s1;
5463 char *buf;
5465 name = current_section->name;
5467 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
5468 region and that no load-time region has been specified. It doesn't
5469 really matter what we say here, since lang_leave_overlay will
5470 override it. */
5471 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
5473 /* Define the magic symbols. */
5475 clean = xmalloc (strlen (name) + 1);
5476 s2 = clean;
5477 for (s1 = name; *s1 != '\0'; s1++)
5478 if (ISALNUM (*s1) || *s1 == '_')
5479 *s2++ = *s1;
5480 *s2 = '\0';
5482 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5483 sprintf (buf, "__load_start_%s", clean);
5484 lang_add_assignment (exp_assop ('=', buf,
5485 exp_nameop (LOADADDR, name)));
5487 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5488 sprintf (buf, "__load_stop_%s", clean);
5489 lang_add_assignment (exp_assop ('=', buf,
5490 exp_binop ('+',
5491 exp_nameop (LOADADDR, name),
5492 exp_nameop (SIZEOF, name))));
5494 free (clean);
5497 /* Finish an overlay. If there are any overlay wide settings, this
5498 looks through all the sections in the overlay and sets them. */
5500 void
5501 lang_leave_overlay (etree_type *lma_expr,
5502 int nocrossrefs,
5503 fill_type *fill,
5504 const char *memspec,
5505 lang_output_section_phdr_list *phdrs,
5506 const char *lma_memspec)
5508 lang_memory_region_type *region;
5509 lang_memory_region_type *lma_region;
5510 struct overlay_list *l;
5511 lang_nocrossref_type *nocrossref;
5513 lang_get_regions (&region, &lma_region,
5514 memspec, lma_memspec,
5515 lma_expr != NULL, FALSE);
5517 nocrossref = NULL;
5519 /* After setting the size of the last section, set '.' to end of the
5520 overlay region. */
5521 if (overlay_list != NULL)
5522 overlay_list->os->update_dot_tree
5523 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5525 l = overlay_list;
5526 while (l != NULL)
5528 struct overlay_list *next;
5530 if (fill != NULL && l->os->fill == NULL)
5531 l->os->fill = fill;
5533 l->os->region = region;
5534 l->os->lma_region = lma_region;
5536 /* The first section has the load address specified in the
5537 OVERLAY statement. The rest are worked out from that.
5538 The base address is not needed (and should be null) if
5539 an LMA region was specified. */
5540 if (l->next == 0)
5541 l->os->load_base = lma_expr;
5542 else if (lma_region == 0)
5543 l->os->load_base = exp_binop ('+',
5544 exp_nameop (LOADADDR, l->next->os->name),
5545 exp_nameop (SIZEOF, l->next->os->name));
5547 if (phdrs != NULL && l->os->phdrs == NULL)
5548 l->os->phdrs = phdrs;
5550 if (nocrossrefs)
5552 lang_nocrossref_type *nc;
5554 nc = xmalloc (sizeof *nc);
5555 nc->name = l->os->name;
5556 nc->next = nocrossref;
5557 nocrossref = nc;
5560 next = l->next;
5561 free (l);
5562 l = next;
5565 if (nocrossref != NULL)
5566 lang_add_nocrossref (nocrossref);
5568 overlay_vma = NULL;
5569 overlay_list = NULL;
5570 overlay_max = NULL;
5573 /* Version handling. This is only useful for ELF. */
5575 /* This global variable holds the version tree that we build. */
5577 struct bfd_elf_version_tree *lang_elf_version_info;
5579 /* If PREV is NULL, return first version pattern matching particular symbol.
5580 If PREV is non-NULL, return first version pattern matching particular
5581 symbol after PREV (previously returned by lang_vers_match). */
5583 static struct bfd_elf_version_expr *
5584 lang_vers_match (struct bfd_elf_version_expr_head *head,
5585 struct bfd_elf_version_expr *prev,
5586 const char *sym)
5588 const char *cxx_sym = sym;
5589 const char *java_sym = sym;
5590 struct bfd_elf_version_expr *expr = NULL;
5592 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5594 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5595 if (!cxx_sym)
5596 cxx_sym = sym;
5598 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5600 java_sym = cplus_demangle (sym, DMGL_JAVA);
5601 if (!java_sym)
5602 java_sym = sym;
5605 if (head->htab && (prev == NULL || prev->symbol))
5607 struct bfd_elf_version_expr e;
5609 switch (prev ? prev->mask : 0)
5611 case 0:
5612 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5614 e.symbol = sym;
5615 expr = htab_find (head->htab, &e);
5616 while (expr && strcmp (expr->symbol, sym) == 0)
5617 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5618 goto out_ret;
5619 else
5620 expr = expr->next;
5622 /* Fallthrough */
5623 case BFD_ELF_VERSION_C_TYPE:
5624 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5626 e.symbol = cxx_sym;
5627 expr = htab_find (head->htab, &e);
5628 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5629 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5630 goto out_ret;
5631 else
5632 expr = expr->next;
5634 /* Fallthrough */
5635 case BFD_ELF_VERSION_CXX_TYPE:
5636 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5638 e.symbol = java_sym;
5639 expr = htab_find (head->htab, &e);
5640 while (expr && strcmp (expr->symbol, java_sym) == 0)
5641 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5642 goto out_ret;
5643 else
5644 expr = expr->next;
5646 /* Fallthrough */
5647 default:
5648 break;
5652 /* Finally, try the wildcards. */
5653 if (prev == NULL || prev->symbol)
5654 expr = head->remaining;
5655 else
5656 expr = prev->next;
5657 while (expr)
5659 const char *s;
5661 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5662 break;
5664 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5665 s = java_sym;
5666 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5667 s = cxx_sym;
5668 else
5669 s = sym;
5670 if (fnmatch (expr->pattern, s, 0) == 0)
5671 break;
5672 expr = expr->next;
5675 out_ret:
5676 if (cxx_sym != sym)
5677 free ((char *) cxx_sym);
5678 if (java_sym != sym)
5679 free ((char *) java_sym);
5680 return expr;
5683 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5684 return a string pointing to the symbol name. */
5686 static const char *
5687 realsymbol (const char *pattern)
5689 const char *p;
5690 bfd_boolean changed = FALSE, backslash = FALSE;
5691 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5693 for (p = pattern, s = symbol; *p != '\0'; ++p)
5695 /* It is a glob pattern only if there is no preceding
5696 backslash. */
5697 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5699 free (symbol);
5700 return NULL;
5703 if (backslash)
5705 /* Remove the preceding backslash. */
5706 *(s - 1) = *p;
5707 changed = TRUE;
5709 else
5710 *s++ = *p;
5712 backslash = *p == '\\';
5715 if (changed)
5717 *s = '\0';
5718 return symbol;
5720 else
5722 free (symbol);
5723 return pattern;
5727 /* This is called for each variable name or match expression. */
5729 struct bfd_elf_version_expr *
5730 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5731 const char *new,
5732 const char *lang)
5734 struct bfd_elf_version_expr *ret;
5736 ret = xmalloc (sizeof *ret);
5737 ret->next = orig;
5738 ret->pattern = new;
5739 ret->symver = 0;
5740 ret->script = 0;
5741 ret->symbol = realsymbol (new);
5743 if (lang == NULL || strcasecmp (lang, "C") == 0)
5744 ret->mask = BFD_ELF_VERSION_C_TYPE;
5745 else if (strcasecmp (lang, "C++") == 0)
5746 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5747 else if (strcasecmp (lang, "Java") == 0)
5748 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5749 else
5751 einfo (_("%X%P: unknown language `%s' in version information\n"),
5752 lang);
5753 ret->mask = BFD_ELF_VERSION_C_TYPE;
5756 return ldemul_new_vers_pattern (ret);
5759 /* This is called for each set of variable names and match
5760 expressions. */
5762 struct bfd_elf_version_tree *
5763 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5764 struct bfd_elf_version_expr *locals)
5766 struct bfd_elf_version_tree *ret;
5768 ret = xcalloc (1, sizeof *ret);
5769 ret->globals.list = globals;
5770 ret->locals.list = locals;
5771 ret->match = lang_vers_match;
5772 ret->name_indx = (unsigned int) -1;
5773 return ret;
5776 /* This static variable keeps track of version indices. */
5778 static int version_index;
5780 static hashval_t
5781 version_expr_head_hash (const void *p)
5783 const struct bfd_elf_version_expr *e = p;
5785 return htab_hash_string (e->symbol);
5788 static int
5789 version_expr_head_eq (const void *p1, const void *p2)
5791 const struct bfd_elf_version_expr *e1 = p1;
5792 const struct bfd_elf_version_expr *e2 = p2;
5794 return strcmp (e1->symbol, e2->symbol) == 0;
5797 static void
5798 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5800 size_t count = 0;
5801 struct bfd_elf_version_expr *e, *next;
5802 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5804 for (e = head->list; e; e = e->next)
5806 if (e->symbol)
5807 count++;
5808 head->mask |= e->mask;
5811 if (count)
5813 head->htab = htab_create (count * 2, version_expr_head_hash,
5814 version_expr_head_eq, NULL);
5815 list_loc = &head->list;
5816 remaining_loc = &head->remaining;
5817 for (e = head->list; e; e = next)
5819 next = e->next;
5820 if (!e->symbol)
5822 *remaining_loc = e;
5823 remaining_loc = &e->next;
5825 else
5827 void **loc = htab_find_slot (head->htab, e, INSERT);
5829 if (*loc)
5831 struct bfd_elf_version_expr *e1, *last;
5833 e1 = *loc;
5834 last = NULL;
5837 if (e1->mask == e->mask)
5839 last = NULL;
5840 break;
5842 last = e1;
5843 e1 = e1->next;
5845 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5847 if (last == NULL)
5849 /* This is a duplicate. */
5850 /* FIXME: Memory leak. Sometimes pattern is not
5851 xmalloced alone, but in larger chunk of memory. */
5852 /* free (e->symbol); */
5853 free (e);
5855 else
5857 e->next = last->next;
5858 last->next = e;
5861 else
5863 *loc = e;
5864 *list_loc = e;
5865 list_loc = &e->next;
5869 *remaining_loc = NULL;
5870 *list_loc = head->remaining;
5872 else
5873 head->remaining = head->list;
5876 /* This is called when we know the name and dependencies of the
5877 version. */
5879 void
5880 lang_register_vers_node (const char *name,
5881 struct bfd_elf_version_tree *version,
5882 struct bfd_elf_version_deps *deps)
5884 struct bfd_elf_version_tree *t, **pp;
5885 struct bfd_elf_version_expr *e1;
5887 if (name == NULL)
5888 name = "";
5890 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5891 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5893 einfo (_("%X%P: anonymous version tag cannot be combined"
5894 " with other version tags\n"));
5895 free (version);
5896 return;
5899 /* Make sure this node has a unique name. */
5900 for (t = lang_elf_version_info; t != NULL; t = t->next)
5901 if (strcmp (t->name, name) == 0)
5902 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5904 lang_finalize_version_expr_head (&version->globals);
5905 lang_finalize_version_expr_head (&version->locals);
5907 /* Check the global and local match names, and make sure there
5908 aren't any duplicates. */
5910 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5912 for (t = lang_elf_version_info; t != NULL; t = t->next)
5914 struct bfd_elf_version_expr *e2;
5916 if (t->locals.htab && e1->symbol)
5918 e2 = htab_find (t->locals.htab, e1);
5919 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5921 if (e1->mask == e2->mask)
5922 einfo (_("%X%P: duplicate expression `%s'"
5923 " in version information\n"), e1->symbol);
5924 e2 = e2->next;
5927 else if (!e1->symbol)
5928 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5929 if (strcmp (e1->pattern, e2->pattern) == 0
5930 && e1->mask == e2->mask)
5931 einfo (_("%X%P: duplicate expression `%s'"
5932 " in version information\n"), e1->pattern);
5936 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5938 for (t = lang_elf_version_info; t != NULL; t = t->next)
5940 struct bfd_elf_version_expr *e2;
5942 if (t->globals.htab && e1->symbol)
5944 e2 = htab_find (t->globals.htab, e1);
5945 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5947 if (e1->mask == e2->mask)
5948 einfo (_("%X%P: duplicate expression `%s'"
5949 " in version information\n"),
5950 e1->symbol);
5951 e2 = e2->next;
5954 else if (!e1->symbol)
5955 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5956 if (strcmp (e1->pattern, e2->pattern) == 0
5957 && e1->mask == e2->mask)
5958 einfo (_("%X%P: duplicate expression `%s'"
5959 " in version information\n"), e1->pattern);
5963 version->deps = deps;
5964 version->name = name;
5965 if (name[0] != '\0')
5967 ++version_index;
5968 version->vernum = version_index;
5970 else
5971 version->vernum = 0;
5973 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5975 *pp = version;
5978 /* This is called when we see a version dependency. */
5980 struct bfd_elf_version_deps *
5981 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5983 struct bfd_elf_version_deps *ret;
5984 struct bfd_elf_version_tree *t;
5986 ret = xmalloc (sizeof *ret);
5987 ret->next = list;
5989 for (t = lang_elf_version_info; t != NULL; t = t->next)
5991 if (strcmp (t->name, name) == 0)
5993 ret->version_needed = t;
5994 return ret;
5998 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
6000 return ret;
6003 static void
6004 lang_do_version_exports_section (void)
6006 struct bfd_elf_version_expr *greg = NULL, *lreg;
6008 LANG_FOR_EACH_INPUT_STATEMENT (is)
6010 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
6011 char *contents, *p;
6012 bfd_size_type len;
6014 if (sec == NULL)
6015 continue;
6017 len = sec->size;
6018 contents = xmalloc (len);
6019 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6020 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
6022 p = contents;
6023 while (p < contents + len)
6025 greg = lang_new_vers_pattern (greg, p, NULL);
6026 p = strchr (p, '\0') + 1;
6029 /* Do not free the contents, as we used them creating the regex. */
6031 /* Do not include this section in the link. */
6032 sec->flags |= SEC_EXCLUDE;
6035 lreg = lang_new_vers_pattern (NULL, "*", NULL);
6036 lang_register_vers_node (command_line.version_exports_section,
6037 lang_new_vers_node (greg, lreg), NULL);
6040 void
6041 lang_add_unique (const char *name)
6043 struct unique_sections *ent;
6045 for (ent = unique_section_list; ent; ent = ent->next)
6046 if (strcmp (ent->name, name) == 0)
6047 return;
6049 ent = xmalloc (sizeof *ent);
6050 ent->name = xstrdup (name);
6051 ent->next = unique_section_list;
6052 unique_section_list = ent;