PR gas/619
[binutils.git] / ld / ldlang.c
blobab34090e412d8ede8bef78dec9a1068da9cc1c97
1 /* Linker command language support.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004
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 0
557 /* This code used to always use the first region in the list as the
558 default region. I changed it to instead use a region
559 encompassing all of memory as the default region. This permits
560 NOLOAD sections to work reasonably without requiring a region.
561 People should specify what region they mean, if they really want
562 a region. */
563 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
565 if (lang_memory_region_list != NULL)
566 return lang_memory_region_list;
568 #endif
570 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
571 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
573 new = stat_alloc (sizeof (lang_memory_region_type));
575 new->name = xstrdup (name);
576 new->next = NULL;
578 *lang_memory_region_list_tail = new;
579 lang_memory_region_list_tail = &new->next;
580 new->origin = 0;
581 new->flags = 0;
582 new->not_flags = 0;
583 new->length = ~(bfd_size_type) 0;
584 new->current = 0;
585 new->had_full_message = FALSE;
587 return new;
590 static lang_memory_region_type *
591 lang_memory_default (asection *section)
593 lang_memory_region_type *p;
595 flagword sec_flags = section->flags;
597 /* Override SEC_DATA to mean a writable section. */
598 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
599 sec_flags |= SEC_DATA;
601 for (p = lang_memory_region_list; p != NULL; p = p->next)
603 if ((p->flags & sec_flags) != 0
604 && (p->not_flags & sec_flags) == 0)
606 return p;
609 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
612 static lang_output_section_statement_type *
613 lang_output_section_find_1 (const char *const name, int constraint)
615 lang_output_section_statement_type *lookup;
617 for (lookup = &lang_output_section_statement.head->output_section_statement;
618 lookup != NULL;
619 lookup = lookup->next)
621 if (strcmp (name, lookup->name) == 0
622 && lookup->constraint != -1
623 && (constraint == 0 || constraint == lookup->constraint))
624 return lookup;
626 return NULL;
629 lang_output_section_statement_type *
630 lang_output_section_find (const char *const name)
632 return lang_output_section_find_1 (name, 0);
635 static lang_output_section_statement_type *
636 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
638 lang_output_section_statement_type *lookup;
640 lookup = lang_output_section_find_1 (name, constraint);
641 if (lookup == NULL)
643 lookup = new_stat (lang_output_section_statement, stat_ptr);
644 lookup->region = NULL;
645 lookup->lma_region = NULL;
646 lookup->fill = 0;
647 lookup->block_value = 1;
648 lookup->name = name;
650 lookup->next = NULL;
651 lookup->bfd_section = NULL;
652 lookup->processed = 0;
653 lookup->constraint = constraint;
654 lookup->sectype = normal_section;
655 lookup->addr_tree = NULL;
656 lang_list_init (&lookup->children);
658 lookup->memspec = NULL;
659 lookup->flags = 0;
660 lookup->subsection_alignment = -1;
661 lookup->section_alignment = -1;
662 lookup->load_base = NULL;
663 lookup->update_dot_tree = NULL;
664 lookup->phdrs = NULL;
666 lang_statement_append (&lang_output_section_statement,
667 (lang_statement_union_type *) lookup,
668 (lang_statement_union_type **) &lookup->next);
670 return lookup;
673 lang_output_section_statement_type *
674 lang_output_section_statement_lookup (const char *const name)
676 return lang_output_section_statement_lookup_1 (name, 0);
679 /* A variant of lang_output_section_find used by place_orphan.
680 Returns the output statement that should precede a new output
681 statement for SEC. If an exact match is found on certain flags,
682 sets *EXACT too. */
684 lang_output_section_statement_type *
685 lang_output_section_find_by_flags (const asection *sec,
686 lang_output_section_statement_type **exact)
688 lang_output_section_statement_type *first, *look, *found;
689 flagword flags;
691 /* We know the first statement on this list is *ABS*. May as well
692 skip it. */
693 first = &lang_output_section_statement.head->output_section_statement;
694 first = first->next;
696 /* First try for an exact match. */
697 found = NULL;
698 for (look = first; look; look = look->next)
700 flags = look->flags;
701 if (look->bfd_section != NULL)
702 flags = look->bfd_section->flags;
703 flags ^= sec->flags;
704 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
705 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
706 found = look;
708 if (found != NULL)
710 *exact = found;
711 return found;
714 if (sec->flags & SEC_CODE)
716 /* Try for a rw code section. */
717 for (look = first; look; look = look->next)
719 flags = look->flags;
720 if (look->bfd_section != NULL)
721 flags = look->bfd_section->flags;
722 flags ^= sec->flags;
723 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
724 | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
725 found = look;
727 return found;
730 if (sec->flags & (SEC_READONLY | SEC_THREAD_LOCAL))
732 /* .rodata can go after .text, .sdata2 after .rodata. */
733 for (look = first; look; look = look->next)
735 flags = look->flags;
736 if (look->bfd_section != NULL)
737 flags = look->bfd_section->flags;
738 flags ^= sec->flags;
739 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
740 | SEC_READONLY))
741 && !(look->flags & (SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
742 found = look;
744 return found;
747 if (sec->flags & SEC_SMALL_DATA)
749 /* .sdata goes after .data, .sbss after .sdata. */
750 for (look = first; look; look = look->next)
752 flags = look->flags;
753 if (look->bfd_section != NULL)
754 flags = look->bfd_section->flags;
755 flags ^= sec->flags;
756 if (!(flags & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
757 | SEC_THREAD_LOCAL))
758 || ((look->flags & SEC_SMALL_DATA)
759 && !(sec->flags & SEC_HAS_CONTENTS)))
760 found = look;
762 return found;
765 if (sec->flags & SEC_HAS_CONTENTS)
767 /* .data goes after .rodata. */
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_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
775 | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
776 found = look;
778 return found;
781 /* .bss goes last. */
782 for (look = first; look; look = look->next)
784 flags = look->flags;
785 if (look->bfd_section != NULL)
786 flags = look->bfd_section->flags;
787 flags ^= sec->flags;
788 if (!(flags & SEC_ALLOC))
789 found = look;
792 return found;
795 /* Find the last output section before given output statement.
796 Used by place_orphan. */
798 static asection *
799 output_prev_sec_find (lang_output_section_statement_type *os)
801 asection *s = (asection *) NULL;
802 lang_output_section_statement_type *lookup;
804 for (lookup = &lang_output_section_statement.head->output_section_statement;
805 lookup != NULL;
806 lookup = lookup->next)
808 if (lookup->constraint == -1)
809 continue;
810 if (lookup == os)
811 return s;
813 if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
814 s = lookup->bfd_section;
817 return NULL;
820 lang_output_section_statement_type *
821 lang_insert_orphan (lang_input_statement_type *file,
822 asection *s,
823 const char *secname,
824 lang_output_section_statement_type *after,
825 struct orphan_save *place,
826 etree_type *address,
827 lang_statement_list_type *add_child)
829 lang_statement_list_type *old;
830 lang_statement_list_type add;
831 const char *ps;
832 etree_type *load_base;
833 lang_output_section_statement_type *os;
834 lang_output_section_statement_type **os_tail;
836 /* Start building a list of statements for this section.
837 First save the current statement pointer. */
838 old = stat_ptr;
840 /* If we have found an appropriate place for the output section
841 statements for this orphan, add them to our own private list,
842 inserting them later into the global statement list. */
843 if (after != NULL)
845 stat_ptr = &add;
846 lang_list_init (stat_ptr);
849 ps = NULL;
850 if (config.build_constructors)
852 /* If the name of the section is representable in C, then create
853 symbols to mark the start and the end of the section. */
854 for (ps = secname; *ps != '\0'; ps++)
855 if (! ISALNUM ((unsigned char) *ps) && *ps != '_')
856 break;
857 if (*ps == '\0')
859 char *symname;
860 etree_type *e_align;
862 symname = (char *) xmalloc (ps - secname + sizeof "__start_" + 1);
863 symname[0] = bfd_get_symbol_leading_char (output_bfd);
864 sprintf (symname + (symname[0] != 0), "__start_%s", secname);
865 e_align = exp_unop (ALIGN_K,
866 exp_intop ((bfd_vma) 1 << s->alignment_power));
867 lang_add_assignment (exp_assop ('=', ".", e_align));
868 lang_add_assignment (exp_assop ('=', symname,
869 exp_nameop (NAME, ".")));
873 if (link_info.relocatable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
874 address = exp_intop (0);
876 load_base = NULL;
877 if (after != NULL && after->load_base != NULL)
879 etree_type *lma_from_vma;
880 lma_from_vma = exp_binop ('-', after->load_base,
881 exp_nameop (ADDR, after->name));
882 load_base = exp_binop ('+', lma_from_vma,
883 exp_nameop (ADDR, secname));
886 os_tail = ((lang_output_section_statement_type **)
887 lang_output_section_statement.tail);
888 os = lang_enter_output_section_statement (secname, address, 0, NULL, NULL,
889 load_base, 0);
891 if (add_child == NULL)
892 add_child = &os->children;
893 lang_add_section (add_child, s, os, file);
895 lang_leave_output_section_statement (0, "*default*", NULL, NULL);
897 if (config.build_constructors && *ps == '\0')
899 char *symname;
901 /* lang_leave_ouput_section_statement resets stat_ptr.
902 Put stat_ptr back where we want it. */
903 if (after != NULL)
904 stat_ptr = &add;
906 symname = (char *) xmalloc (ps - secname + sizeof "__stop_" + 1);
907 symname[0] = bfd_get_symbol_leading_char (output_bfd);
908 sprintf (symname + (symname[0] != 0), "__stop_%s", secname);
909 lang_add_assignment (exp_assop ('=', symname,
910 exp_nameop (NAME, ".")));
913 /* Restore the global list pointer. */
914 if (after != NULL)
915 stat_ptr = old;
917 if (after != NULL && os->bfd_section != NULL)
919 asection *snew, **pps;
921 snew = os->bfd_section;
923 /* Shuffle the bfd section list to make the output file look
924 neater. This is really only cosmetic. */
925 if (place->section == NULL
926 && after != (&lang_output_section_statement.head
927 ->output_section_statement))
929 asection *bfd_section = after->bfd_section;
931 /* If the output statement hasn't been used to place any input
932 sections (and thus doesn't have an output bfd_section),
933 look for the closest prior output statement having an
934 output section. */
935 if (bfd_section == NULL)
936 bfd_section = output_prev_sec_find (after);
938 if (bfd_section != NULL && bfd_section != snew)
939 place->section = &bfd_section->next;
942 if (place->section == NULL)
943 place->section = &output_bfd->sections;
945 /* Unlink the section. */
946 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
947 continue;
948 bfd_section_list_remove (output_bfd, pps);
950 /* Now tack it back on in the right place. */
951 bfd_section_list_insert (output_bfd, place->section, snew);
953 /* Save the end of this list. Further ophans of this type will
954 follow the one we've just added. */
955 place->section = &snew->next;
957 /* The following is non-cosmetic. We try to put the output
958 statements in some sort of reasonable order here, because they
959 determine the final load addresses of the orphan sections.
960 In addition, placing output statements in the wrong order may
961 require extra segments. For instance, given a typical
962 situation of all read-only sections placed in one segment and
963 following that a segment containing all the read-write
964 sections, we wouldn't want to place an orphan read/write
965 section before or amongst the read-only ones. */
966 if (add.head != NULL)
968 lang_output_section_statement_type *newly_added_os;
970 if (place->stmt == NULL)
972 lang_statement_union_type **where;
973 lang_statement_union_type **assign = NULL;
975 /* Look for a suitable place for the new statement list.
976 The idea is to skip over anything that might be inside
977 a SECTIONS {} statement in a script, before we find
978 another output_section_statement. Assignments to "dot"
979 before an output section statement are assumed to
980 belong to it. */
981 for (where = &after->header.next;
982 *where != NULL;
983 where = &(*where)->header.next)
985 switch ((*where)->header.type)
987 case lang_assignment_statement_enum:
988 if (assign == NULL)
990 lang_assignment_statement_type *ass;
991 ass = &(*where)->assignment_statement;
992 if (ass->exp->type.node_class != etree_assert
993 && ass->exp->assign.dst[0] == '.'
994 && ass->exp->assign.dst[1] == 0)
995 assign = where;
997 continue;
998 case lang_wild_statement_enum:
999 case lang_input_section_enum:
1000 case lang_object_symbols_statement_enum:
1001 case lang_fill_statement_enum:
1002 case lang_data_statement_enum:
1003 case lang_reloc_statement_enum:
1004 case lang_padding_statement_enum:
1005 case lang_constructors_statement_enum:
1006 assign = NULL;
1007 continue;
1008 case lang_output_section_statement_enum:
1009 if (assign != NULL)
1010 where = assign;
1011 case lang_input_statement_enum:
1012 case lang_address_statement_enum:
1013 case lang_target_statement_enum:
1014 case lang_output_statement_enum:
1015 case lang_group_statement_enum:
1016 case lang_afile_asection_pair_statement_enum:
1017 break;
1019 break;
1022 *add.tail = *where;
1023 *where = add.head;
1025 place->os_tail = &after->next;
1027 else
1029 /* Put it after the last orphan statement we added. */
1030 *add.tail = *place->stmt;
1031 *place->stmt = add.head;
1034 /* Fix the global list pointer if we happened to tack our
1035 new list at the tail. */
1036 if (*old->tail == add.head)
1037 old->tail = add.tail;
1039 /* Save the end of this list. */
1040 place->stmt = add.tail;
1042 /* Do the same for the list of output section statements. */
1043 newly_added_os = *os_tail;
1044 *os_tail = NULL;
1045 newly_added_os->next = *place->os_tail;
1046 *place->os_tail = newly_added_os;
1047 place->os_tail = &newly_added_os->next;
1049 /* Fixing the global list pointer here is a little different.
1050 We added to the list in lang_enter_output_section_statement,
1051 trimmed off the new output_section_statment above when
1052 assigning *os_tail = NULL, but possibly added it back in
1053 the same place when assigning *place->os_tail. */
1054 if (*os_tail == NULL)
1055 lang_output_section_statement.tail
1056 = (lang_statement_union_type **) os_tail;
1059 return os;
1062 static void
1063 lang_map_flags (flagword flag)
1065 if (flag & SEC_ALLOC)
1066 minfo ("a");
1068 if (flag & SEC_CODE)
1069 minfo ("x");
1071 if (flag & SEC_READONLY)
1072 minfo ("r");
1074 if (flag & SEC_DATA)
1075 minfo ("w");
1077 if (flag & SEC_LOAD)
1078 minfo ("l");
1081 void
1082 lang_map (void)
1084 lang_memory_region_type *m;
1085 bfd *p;
1087 minfo (_("\nMemory Configuration\n\n"));
1088 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
1089 _("Name"), _("Origin"), _("Length"), _("Attributes"));
1091 for (m = lang_memory_region_list; m != NULL; m = m->next)
1093 char buf[100];
1094 int len;
1096 fprintf (config.map_file, "%-16s ", m->name);
1098 sprintf_vma (buf, m->origin);
1099 minfo ("0x%s ", buf);
1100 len = strlen (buf);
1101 while (len < 16)
1103 print_space ();
1104 ++len;
1107 minfo ("0x%V", m->length);
1108 if (m->flags || m->not_flags)
1110 #ifndef BFD64
1111 minfo (" ");
1112 #endif
1113 if (m->flags)
1115 print_space ();
1116 lang_map_flags (m->flags);
1119 if (m->not_flags)
1121 minfo (" !");
1122 lang_map_flags (m->not_flags);
1126 print_nl ();
1129 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
1131 if (! command_line.reduce_memory_overheads)
1133 obstack_begin (&map_obstack, 1000);
1134 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
1135 bfd_map_over_sections (p, init_map_userdata, 0);
1136 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
1138 print_statements ();
1141 static void
1142 init_map_userdata (abfd, sec, data)
1143 bfd *abfd ATTRIBUTE_UNUSED;
1144 asection *sec;
1145 void *data ATTRIBUTE_UNUSED;
1147 fat_section_userdata_type *new_data
1148 = ((fat_section_userdata_type *) (stat_alloc
1149 (sizeof (fat_section_userdata_type))));
1151 ASSERT (get_userdata (sec) == NULL);
1152 get_userdata (sec) = new_data;
1153 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
1156 static bfd_boolean
1157 sort_def_symbol (hash_entry, info)
1158 struct bfd_link_hash_entry *hash_entry;
1159 void *info ATTRIBUTE_UNUSED;
1161 if (hash_entry->type == bfd_link_hash_defined
1162 || hash_entry->type == bfd_link_hash_defweak)
1164 struct fat_user_section_struct *ud;
1165 struct map_symbol_def *def;
1167 ud = get_userdata (hash_entry->u.def.section);
1168 if (! ud)
1170 /* ??? What do we have to do to initialize this beforehand? */
1171 /* The first time we get here is bfd_abs_section... */
1172 init_map_userdata (0, hash_entry->u.def.section, 0);
1173 ud = get_userdata (hash_entry->u.def.section);
1175 else if (!ud->map_symbol_def_tail)
1176 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
1178 def = obstack_alloc (&map_obstack, sizeof *def);
1179 def->entry = hash_entry;
1180 *(ud->map_symbol_def_tail) = def;
1181 ud->map_symbol_def_tail = &def->next;
1183 return TRUE;
1186 /* Initialize an output section. */
1188 static void
1189 init_os (lang_output_section_statement_type *s)
1191 lean_section_userdata_type *new;
1193 if (s->bfd_section != NULL)
1194 return;
1196 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
1197 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
1199 new = stat_alloc (SECTION_USERDATA_SIZE);
1200 memset (new, 0, SECTION_USERDATA_SIZE);
1202 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
1203 if (s->bfd_section == NULL)
1204 s->bfd_section = bfd_make_section (output_bfd, s->name);
1205 if (s->bfd_section == NULL)
1207 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
1208 output_bfd->xvec->name, s->name);
1210 s->bfd_section->output_section = s->bfd_section;
1212 /* We initialize an output sections output offset to minus its own
1213 vma to allow us to output a section through itself. */
1214 s->bfd_section->output_offset = 0;
1215 get_userdata (s->bfd_section) = new;
1217 /* If there is a base address, make sure that any sections it might
1218 mention are initialized. */
1219 if (s->addr_tree != NULL)
1220 exp_init_os (s->addr_tree);
1222 if (s->load_base != NULL)
1223 exp_init_os (s->load_base);
1226 /* Make sure that all output sections mentioned in an expression are
1227 initialized. */
1229 static void
1230 exp_init_os (etree_type *exp)
1232 switch (exp->type.node_class)
1234 case etree_assign:
1235 exp_init_os (exp->assign.src);
1236 break;
1238 case etree_binary:
1239 exp_init_os (exp->binary.lhs);
1240 exp_init_os (exp->binary.rhs);
1241 break;
1243 case etree_trinary:
1244 exp_init_os (exp->trinary.cond);
1245 exp_init_os (exp->trinary.lhs);
1246 exp_init_os (exp->trinary.rhs);
1247 break;
1249 case etree_assert:
1250 exp_init_os (exp->assert_s.child);
1251 break;
1253 case etree_unary:
1254 exp_init_os (exp->unary.child);
1255 break;
1257 case etree_name:
1258 switch (exp->type.node_code)
1260 case ADDR:
1261 case LOADADDR:
1262 case SIZEOF:
1264 lang_output_section_statement_type *os;
1266 os = lang_output_section_find (exp->name.name);
1267 if (os != NULL && os->bfd_section == NULL)
1268 init_os (os);
1271 break;
1273 default:
1274 break;
1278 static void
1279 section_already_linked (bfd *abfd, asection *sec, void *data)
1281 lang_input_statement_type *entry = data;
1283 /* If we are only reading symbols from this object, then we want to
1284 discard all sections. */
1285 if (entry->just_syms_flag)
1287 bfd_link_just_syms (sec, &link_info);
1288 return;
1291 bfd_section_already_linked (abfd, sec);
1294 /* The wild routines.
1296 These expand statements like *(.text) and foo.o to a list of
1297 explicit actions, like foo.o(.text), bar.o(.text) and
1298 foo.o(.text, .data). */
1300 /* Add SECTION to the output section OUTPUT. Do this by creating a
1301 lang_input_section statement which is placed at PTR. FILE is the
1302 input file which holds SECTION. */
1304 void
1305 lang_add_section (lang_statement_list_type *ptr,
1306 asection *section,
1307 lang_output_section_statement_type *output,
1308 lang_input_statement_type *file)
1310 flagword flags = section->flags;
1311 bfd_boolean discard;
1313 /* Discard sections marked with SEC_EXCLUDE. */
1314 discard = (flags & SEC_EXCLUDE) != 0;
1316 /* Discard input sections which are assigned to a section named
1317 DISCARD_SECTION_NAME. */
1318 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1319 discard = TRUE;
1321 /* Discard debugging sections if we are stripping debugging
1322 information. */
1323 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1324 && (flags & SEC_DEBUGGING) != 0)
1325 discard = TRUE;
1327 if (discard)
1329 if (section->output_section == NULL)
1331 /* This prevents future calls from assigning this section. */
1332 section->output_section = bfd_abs_section_ptr;
1334 return;
1337 if (section->output_section == NULL)
1339 bfd_boolean first;
1340 lang_input_section_type *new;
1341 flagword flags;
1343 if (output->bfd_section == NULL)
1344 init_os (output);
1346 first = ! output->bfd_section->linker_has_input;
1347 output->bfd_section->linker_has_input = 1;
1349 /* Add a section reference to the list. */
1350 new = new_stat (lang_input_section, ptr);
1352 new->section = section;
1353 new->ifile = file;
1354 section->output_section = output->bfd_section;
1356 flags = section->flags;
1358 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1359 to an output section, because we want to be able to include a
1360 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1361 section (I don't know why we want to do this, but we do).
1362 build_link_order in ldwrite.c handles this case by turning
1363 the embedded SEC_NEVER_LOAD section into a fill. */
1365 flags &= ~ SEC_NEVER_LOAD;
1367 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1368 already been processed. One reason to do this is that on pe
1369 format targets, .text$foo sections go into .text and it's odd
1370 to see .text with SEC_LINK_ONCE set. */
1372 if (! link_info.relocatable)
1373 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1375 /* If this is not the first input section, and the SEC_READONLY
1376 flag is not currently set, then don't set it just because the
1377 input section has it set. */
1379 if (! first && (output->bfd_section->flags & SEC_READONLY) == 0)
1380 flags &= ~ SEC_READONLY;
1382 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1383 if (! first
1384 && ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
1385 != (flags & (SEC_MERGE | SEC_STRINGS))
1386 || ((flags & SEC_MERGE)
1387 && output->bfd_section->entsize != section->entsize)))
1389 output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1390 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1393 output->bfd_section->flags |= flags;
1395 if (flags & SEC_MERGE)
1396 output->bfd_section->entsize = section->entsize;
1398 /* If SEC_READONLY is not set in the input section, then clear
1399 it from the output section. */
1400 if ((section->flags & SEC_READONLY) == 0)
1401 output->bfd_section->flags &= ~SEC_READONLY;
1403 switch (output->sectype)
1405 case normal_section:
1406 break;
1407 case dsect_section:
1408 case copy_section:
1409 case info_section:
1410 case overlay_section:
1411 output->bfd_section->flags &= ~SEC_ALLOC;
1412 break;
1413 case noload_section:
1414 output->bfd_section->flags &= ~SEC_LOAD;
1415 output->bfd_section->flags |= SEC_NEVER_LOAD;
1416 break;
1419 /* Copy over SEC_SMALL_DATA. */
1420 if (section->flags & SEC_SMALL_DATA)
1421 output->bfd_section->flags |= SEC_SMALL_DATA;
1423 if (section->alignment_power > output->bfd_section->alignment_power)
1424 output->bfd_section->alignment_power = section->alignment_power;
1426 /* If supplied an alignment, then force it. */
1427 if (output->section_alignment != -1)
1428 output->bfd_section->alignment_power = output->section_alignment;
1430 if (section->flags & SEC_BLOCK)
1432 output->bfd_section->flags |= SEC_BLOCK;
1433 /* FIXME: This value should really be obtained from the bfd... */
1434 output->block_value = 128;
1439 /* Compare sections ASEC and BSEC according to SORT. */
1441 static int
1442 compare_section (sort_type sort, asection *asec, asection *bsec)
1444 int ret;
1446 switch (sort)
1448 default:
1449 abort ();
1451 case by_alignment_name:
1452 ret = (bfd_section_alignment (bsec->owner, bsec)
1453 - bfd_section_alignment (asec->owner, asec));
1454 if (ret)
1455 break;
1456 /* Fall through. */
1458 case by_name:
1459 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1460 bfd_get_section_name (bsec->owner, bsec));
1461 break;
1463 case by_name_alignment:
1464 ret = strcmp (bfd_get_section_name (asec->owner, asec),
1465 bfd_get_section_name (bsec->owner, bsec));
1466 if (ret)
1467 break;
1468 /* Fall through. */
1470 case by_alignment:
1471 ret = (bfd_section_alignment (bsec->owner, bsec)
1472 - bfd_section_alignment (asec->owner, asec));
1473 break;
1476 return ret;
1479 /* Handle wildcard sorting. This returns the lang_input_section which
1480 should follow the one we are going to create for SECTION and FILE,
1481 based on the sorting requirements of WILD. It returns NULL if the
1482 new section should just go at the end of the current list. */
1484 static lang_statement_union_type *
1485 wild_sort (lang_wild_statement_type *wild,
1486 struct wildcard_list *sec,
1487 lang_input_statement_type *file,
1488 asection *section)
1490 const char *section_name;
1491 lang_statement_union_type *l;
1493 if (!wild->filenames_sorted
1494 && (sec == NULL || sec->spec.sorted == none))
1495 return NULL;
1497 section_name = bfd_get_section_name (file->the_bfd, section);
1498 for (l = wild->children.head; l != NULL; l = l->header.next)
1500 lang_input_section_type *ls;
1502 if (l->header.type != lang_input_section_enum)
1503 continue;
1504 ls = &l->input_section;
1506 /* Sorting by filename takes precedence over sorting by section
1507 name. */
1509 if (wild->filenames_sorted)
1511 const char *fn, *ln;
1512 bfd_boolean fa, la;
1513 int i;
1515 /* The PE support for the .idata section as generated by
1516 dlltool assumes that files will be sorted by the name of
1517 the archive and then the name of the file within the
1518 archive. */
1520 if (file->the_bfd != NULL
1521 && bfd_my_archive (file->the_bfd) != NULL)
1523 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1524 fa = TRUE;
1526 else
1528 fn = file->filename;
1529 fa = FALSE;
1532 if (ls->ifile->the_bfd != NULL
1533 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1535 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1536 la = TRUE;
1538 else
1540 ln = ls->ifile->filename;
1541 la = FALSE;
1544 i = strcmp (fn, ln);
1545 if (i > 0)
1546 continue;
1547 else if (i < 0)
1548 break;
1550 if (fa || la)
1552 if (fa)
1553 fn = file->filename;
1554 if (la)
1555 ln = ls->ifile->filename;
1557 i = strcmp (fn, ln);
1558 if (i > 0)
1559 continue;
1560 else if (i < 0)
1561 break;
1565 /* Here either the files are not sorted by name, or we are
1566 looking at the sections for this file. */
1568 if (sec != NULL && sec->spec.sorted != none)
1570 if (compare_section (sec->spec.sorted, section,
1571 ls->section) < 0)
1572 break;
1576 return l;
1579 /* Expand a wild statement for a particular FILE. SECTION may be
1580 NULL, in which case it is a wild card. */
1582 static void
1583 output_section_callback (lang_wild_statement_type *ptr,
1584 struct wildcard_list *sec,
1585 asection *section,
1586 lang_input_statement_type *file,
1587 void *output)
1589 lang_statement_union_type *before;
1591 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1592 if (unique_section_p (section))
1593 return;
1595 before = wild_sort (ptr, sec, file, section);
1597 /* Here BEFORE points to the lang_input_section which
1598 should follow the one we are about to add. If BEFORE
1599 is NULL, then the section should just go at the end
1600 of the current list. */
1602 if (before == NULL)
1603 lang_add_section (&ptr->children, section,
1604 (lang_output_section_statement_type *) output,
1605 file);
1606 else
1608 lang_statement_list_type list;
1609 lang_statement_union_type **pp;
1611 lang_list_init (&list);
1612 lang_add_section (&list, section,
1613 (lang_output_section_statement_type *) output,
1614 file);
1616 /* If we are discarding the section, LIST.HEAD will
1617 be NULL. */
1618 if (list.head != NULL)
1620 ASSERT (list.head->header.next == NULL);
1622 for (pp = &ptr->children.head;
1623 *pp != before;
1624 pp = &(*pp)->header.next)
1625 ASSERT (*pp != NULL);
1627 list.head->header.next = *pp;
1628 *pp = list.head;
1633 /* Check if all sections in a wild statement for a particular FILE
1634 are readonly. */
1636 static void
1637 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1638 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1639 asection *section,
1640 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1641 void *data)
1643 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1644 if (unique_section_p (section))
1645 return;
1647 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1648 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1651 /* This is passed a file name which must have been seen already and
1652 added to the statement tree. We will see if it has been opened
1653 already and had its symbols read. If not then we'll read it. */
1655 static lang_input_statement_type *
1656 lookup_name (const char *name)
1658 lang_input_statement_type *search;
1660 for (search = (lang_input_statement_type *) input_file_chain.head;
1661 search != NULL;
1662 search = (lang_input_statement_type *) search->next_real_file)
1664 /* Use the local_sym_name as the name of the file that has
1665 already been loaded as filename might have been transformed
1666 via the search directory lookup mechanism. */
1667 const char * filename = search->local_sym_name;
1669 if (filename == NULL && name == NULL)
1670 return search;
1671 if (filename != NULL
1672 && name != NULL
1673 && strcmp (filename, name) == 0)
1674 break;
1677 if (search == NULL)
1678 search = new_afile (name, lang_input_file_is_search_file_enum,
1679 default_target, FALSE);
1681 /* If we have already added this file, or this file is not real
1682 (FIXME: can that ever actually happen?) or the name is NULL
1683 (FIXME: can that ever actually happen?) don't add this file. */
1684 if (search->loaded
1685 || ! search->real
1686 || search->filename == NULL)
1687 return search;
1689 if (! load_symbols (search, NULL))
1690 return NULL;
1692 return search;
1695 /* Save LIST as a list of libraries whose symbols should not be exported. */
1697 struct excluded_lib
1699 char *name;
1700 struct excluded_lib *next;
1702 static struct excluded_lib *excluded_libs;
1704 void
1705 add_excluded_libs (const char *list)
1707 const char *p = list, *end;
1709 while (*p != '\0')
1711 struct excluded_lib *entry;
1712 end = strpbrk (p, ",:");
1713 if (end == NULL)
1714 end = p + strlen (p);
1715 entry = xmalloc (sizeof (*entry));
1716 entry->next = excluded_libs;
1717 entry->name = xmalloc (end - p + 1);
1718 memcpy (entry->name, p, end - p);
1719 entry->name[end - p] = '\0';
1720 excluded_libs = entry;
1721 if (*end == '\0')
1722 break;
1723 p = end + 1;
1727 static void
1728 check_excluded_libs (bfd *abfd)
1730 struct excluded_lib *lib = excluded_libs;
1732 while (lib)
1734 int len = strlen (lib->name);
1735 const char *filename = lbasename (abfd->filename);
1737 if (strcmp (lib->name, "ALL") == 0)
1739 abfd->no_export = TRUE;
1740 return;
1743 if (strncmp (lib->name, filename, len) == 0
1744 && (filename[len] == '\0'
1745 || (filename[len] == '.' && filename[len + 1] == 'a'
1746 && filename[len + 2] == '\0')))
1748 abfd->no_export = TRUE;
1749 return;
1752 lib = lib->next;
1756 /* Get the symbols for an input file. */
1758 static bfd_boolean
1759 load_symbols (lang_input_statement_type *entry,
1760 lang_statement_list_type *place)
1762 char **matching;
1764 if (entry->loaded)
1765 return TRUE;
1767 ldfile_open_file (entry);
1769 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1770 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1772 bfd_error_type err;
1773 lang_statement_list_type *hold;
1774 bfd_boolean bad_load = TRUE;
1775 bfd_boolean save_ldlang_sysrooted_script;
1777 err = bfd_get_error ();
1779 /* See if the emulation has some special knowledge. */
1780 if (ldemul_unrecognized_file (entry))
1781 return TRUE;
1783 if (err == bfd_error_file_ambiguously_recognized)
1785 char **p;
1787 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1788 einfo (_("%B: matching formats:"), entry->the_bfd);
1789 for (p = matching; *p != NULL; p++)
1790 einfo (" %s", *p);
1791 einfo ("%F\n");
1793 else if (err != bfd_error_file_not_recognized
1794 || place == NULL)
1795 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1796 else
1797 bad_load = FALSE;
1799 bfd_close (entry->the_bfd);
1800 entry->the_bfd = NULL;
1802 /* Try to interpret the file as a linker script. */
1803 ldfile_open_command_file (entry->filename);
1805 hold = stat_ptr;
1806 stat_ptr = place;
1807 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1808 ldlang_sysrooted_script = entry->sysrooted;
1810 ldfile_assumed_script = TRUE;
1811 parser_input = input_script;
1812 /* We want to use the same -Bdynamic/-Bstatic as the one for
1813 ENTRY. */
1814 config.dynamic_link = entry->dynamic;
1815 yyparse ();
1816 ldfile_assumed_script = FALSE;
1818 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1819 stat_ptr = hold;
1821 return ! bad_load;
1824 if (ldemul_recognized_file (entry))
1825 return TRUE;
1827 /* We don't call ldlang_add_file for an archive. Instead, the
1828 add_symbols entry point will call ldlang_add_file, via the
1829 add_archive_element callback, for each element of the archive
1830 which is used. */
1831 switch (bfd_get_format (entry->the_bfd))
1833 default:
1834 break;
1836 case bfd_object:
1837 ldlang_add_file (entry);
1838 if (trace_files || trace_file_tries)
1839 info_msg ("%I\n", entry);
1840 break;
1842 case bfd_archive:
1843 check_excluded_libs (entry->the_bfd);
1845 if (entry->whole_archive)
1847 bfd *member = NULL;
1848 bfd_boolean loaded = TRUE;
1850 for (;;)
1852 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1854 if (member == NULL)
1855 break;
1857 if (! bfd_check_format (member, bfd_object))
1859 einfo (_("%F%B: member %B in archive is not an object\n"),
1860 entry->the_bfd, member);
1861 loaded = FALSE;
1864 if (! ((*link_info.callbacks->add_archive_element)
1865 (&link_info, member, "--whole-archive")))
1866 abort ();
1868 if (! bfd_link_add_symbols (member, &link_info))
1870 einfo (_("%F%B: could not read symbols: %E\n"), member);
1871 loaded = FALSE;
1875 entry->loaded = loaded;
1876 return loaded;
1878 break;
1881 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1882 entry->loaded = TRUE;
1883 else
1884 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1886 return entry->loaded;
1889 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1890 may be NULL, indicating that it is a wildcard. Separate
1891 lang_input_section statements are created for each part of the
1892 expansion; they are added after the wild statement S. OUTPUT is
1893 the output section. */
1895 static void
1896 wild (lang_wild_statement_type *s,
1897 const char *target ATTRIBUTE_UNUSED,
1898 lang_output_section_statement_type *output)
1900 struct wildcard_list *sec;
1902 walk_wild (s, output_section_callback, output);
1904 for (sec = s->section_list; sec != NULL; sec = sec->next)
1906 if (default_common_section != NULL)
1907 break;
1908 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1910 /* Remember the section that common is going to in case we
1911 later get something which doesn't know where to put it. */
1912 default_common_section = output;
1917 /* Return TRUE iff target is the sought target. */
1919 static int
1920 get_target (const bfd_target *target, void *data)
1922 const char *sought = data;
1924 return strcmp (target->name, sought) == 0;
1927 /* Like strcpy() but convert to lower case as well. */
1929 static void
1930 stricpy (char *dest, char *src)
1932 char c;
1934 while ((c = *src++) != 0)
1935 *dest++ = TOLOWER (c);
1937 *dest = 0;
1940 /* Remove the first occurrence of needle (if any) in haystack
1941 from haystack. */
1943 static void
1944 strcut (char *haystack, char *needle)
1946 haystack = strstr (haystack, needle);
1948 if (haystack)
1950 char *src;
1952 for (src = haystack + strlen (needle); *src;)
1953 *haystack++ = *src++;
1955 *haystack = 0;
1959 /* Compare two target format name strings.
1960 Return a value indicating how "similar" they are. */
1962 static int
1963 name_compare (char *first, char *second)
1965 char *copy1;
1966 char *copy2;
1967 int result;
1969 copy1 = xmalloc (strlen (first) + 1);
1970 copy2 = xmalloc (strlen (second) + 1);
1972 /* Convert the names to lower case. */
1973 stricpy (copy1, first);
1974 stricpy (copy2, second);
1976 /* Remove size and endian strings from the name. */
1977 strcut (copy1, "big");
1978 strcut (copy1, "little");
1979 strcut (copy2, "big");
1980 strcut (copy2, "little");
1982 /* Return a value based on how many characters match,
1983 starting from the beginning. If both strings are
1984 the same then return 10 * their length. */
1985 for (result = 0; copy1[result] == copy2[result]; result++)
1986 if (copy1[result] == 0)
1988 result *= 10;
1989 break;
1992 free (copy1);
1993 free (copy2);
1995 return result;
1998 /* Set by closest_target_match() below. */
1999 static const bfd_target *winner;
2001 /* Scan all the valid bfd targets looking for one that has the endianness
2002 requirement that was specified on the command line, and is the nearest
2003 match to the original output target. */
2005 static int
2006 closest_target_match (const bfd_target *target, void *data)
2008 const bfd_target *original = data;
2010 if (command_line.endian == ENDIAN_BIG
2011 && target->byteorder != BFD_ENDIAN_BIG)
2012 return 0;
2014 if (command_line.endian == ENDIAN_LITTLE
2015 && target->byteorder != BFD_ENDIAN_LITTLE)
2016 return 0;
2018 /* Must be the same flavour. */
2019 if (target->flavour != original->flavour)
2020 return 0;
2022 /* If we have not found a potential winner yet, then record this one. */
2023 if (winner == NULL)
2025 winner = target;
2026 return 0;
2029 /* Oh dear, we now have two potential candidates for a successful match.
2030 Compare their names and choose the better one. */
2031 if (name_compare (target->name, original->name)
2032 > name_compare (winner->name, original->name))
2033 winner = target;
2035 /* Keep on searching until wqe have checked them all. */
2036 return 0;
2039 /* Return the BFD target format of the first input file. */
2041 static char *
2042 get_first_input_target (void)
2044 char *target = NULL;
2046 LANG_FOR_EACH_INPUT_STATEMENT (s)
2048 if (s->header.type == lang_input_statement_enum
2049 && s->real)
2051 ldfile_open_file (s);
2053 if (s->the_bfd != NULL
2054 && bfd_check_format (s->the_bfd, bfd_object))
2056 target = bfd_get_target (s->the_bfd);
2058 if (target != NULL)
2059 break;
2064 return target;
2067 const char *
2068 lang_get_output_target (void)
2070 const char *target;
2072 /* Has the user told us which output format to use? */
2073 if (output_target != NULL)
2074 return output_target;
2076 /* No - has the current target been set to something other than
2077 the default? */
2078 if (current_target != default_target)
2079 return current_target;
2081 /* No - can we determine the format of the first input file? */
2082 target = get_first_input_target ();
2083 if (target != NULL)
2084 return target;
2086 /* Failed - use the default output target. */
2087 return default_target;
2090 /* Open the output file. */
2092 static bfd *
2093 open_output (const char *name)
2095 bfd *output;
2097 output_target = lang_get_output_target ();
2099 /* Has the user requested a particular endianness on the command
2100 line? */
2101 if (command_line.endian != ENDIAN_UNSET)
2103 const bfd_target *target;
2104 enum bfd_endian desired_endian;
2106 /* Get the chosen target. */
2107 target = bfd_search_for_target (get_target, (void *) output_target);
2109 /* If the target is not supported, we cannot do anything. */
2110 if (target != NULL)
2112 if (command_line.endian == ENDIAN_BIG)
2113 desired_endian = BFD_ENDIAN_BIG;
2114 else
2115 desired_endian = BFD_ENDIAN_LITTLE;
2117 /* See if the target has the wrong endianness. This should
2118 not happen if the linker script has provided big and
2119 little endian alternatives, but some scrips don't do
2120 this. */
2121 if (target->byteorder != desired_endian)
2123 /* If it does, then see if the target provides
2124 an alternative with the correct endianness. */
2125 if (target->alternative_target != NULL
2126 && (target->alternative_target->byteorder == desired_endian))
2127 output_target = target->alternative_target->name;
2128 else
2130 /* Try to find a target as similar as possible to
2131 the default target, but which has the desired
2132 endian characteristic. */
2133 bfd_search_for_target (closest_target_match,
2134 (void *) target);
2136 /* Oh dear - we could not find any targets that
2137 satisfy our requirements. */
2138 if (winner == NULL)
2139 einfo (_("%P: warning: could not find any targets"
2140 " that match endianness requirement\n"));
2141 else
2142 output_target = winner->name;
2148 output = bfd_openw (name, output_target);
2150 if (output == NULL)
2152 if (bfd_get_error () == bfd_error_invalid_target)
2153 einfo (_("%P%F: target %s not found\n"), output_target);
2155 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
2158 delete_output_file_on_failure = TRUE;
2160 #if 0
2161 output->flags |= D_PAGED;
2162 #endif
2164 if (! bfd_set_format (output, bfd_object))
2165 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
2166 if (! bfd_set_arch_mach (output,
2167 ldfile_output_architecture,
2168 ldfile_output_machine))
2169 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
2171 link_info.hash = bfd_link_hash_table_create (output);
2172 if (link_info.hash == NULL)
2173 einfo (_("%P%F: can not create link hash table: %E\n"));
2175 bfd_set_gp_size (output, g_switch_value);
2176 return output;
2179 static void
2180 ldlang_open_output (lang_statement_union_type *statement)
2182 switch (statement->header.type)
2184 case lang_output_statement_enum:
2185 ASSERT (output_bfd == NULL);
2186 output_bfd = open_output (statement->output_statement.name);
2187 ldemul_set_output_arch ();
2188 if (config.magic_demand_paged && !link_info.relocatable)
2189 output_bfd->flags |= D_PAGED;
2190 else
2191 output_bfd->flags &= ~D_PAGED;
2192 if (config.text_read_only)
2193 output_bfd->flags |= WP_TEXT;
2194 else
2195 output_bfd->flags &= ~WP_TEXT;
2196 if (link_info.traditional_format)
2197 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
2198 else
2199 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
2200 break;
2202 case lang_target_statement_enum:
2203 current_target = statement->target_statement.target;
2204 break;
2205 default:
2206 break;
2210 /* Convert between addresses in bytes and sizes in octets.
2211 For currently supported targets, octets_per_byte is always a power
2212 of two, so we can use shifts. */
2213 #define TO_ADDR(X) ((X) >> opb_shift)
2214 #define TO_SIZE(X) ((X) << opb_shift)
2216 /* Support the above. */
2217 static unsigned int opb_shift = 0;
2219 static void
2220 init_opb (void)
2222 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
2223 ldfile_output_machine);
2224 opb_shift = 0;
2225 if (x > 1)
2226 while ((x & 1) == 0)
2228 x >>= 1;
2229 ++opb_shift;
2231 ASSERT (x == 1);
2234 /* Open all the input files. */
2236 static void
2237 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
2239 for (; s != NULL; s = s->header.next)
2241 switch (s->header.type)
2243 case lang_constructors_statement_enum:
2244 open_input_bfds (constructor_list.head, force);
2245 break;
2246 case lang_output_section_statement_enum:
2247 open_input_bfds (s->output_section_statement.children.head, force);
2248 break;
2249 case lang_wild_statement_enum:
2250 /* Maybe we should load the file's symbols. */
2251 if (s->wild_statement.filename
2252 && ! wildcardp (s->wild_statement.filename))
2253 lookup_name (s->wild_statement.filename);
2254 open_input_bfds (s->wild_statement.children.head, force);
2255 break;
2256 case lang_group_statement_enum:
2258 struct bfd_link_hash_entry *undefs;
2260 /* We must continually search the entries in the group
2261 until no new symbols are added to the list of undefined
2262 symbols. */
2266 undefs = link_info.hash->undefs_tail;
2267 open_input_bfds (s->group_statement.children.head, TRUE);
2269 while (undefs != link_info.hash->undefs_tail);
2271 break;
2272 case lang_target_statement_enum:
2273 current_target = s->target_statement.target;
2274 break;
2275 case lang_input_statement_enum:
2276 if (s->input_statement.real)
2278 lang_statement_list_type add;
2280 s->input_statement.target = current_target;
2282 /* If we are being called from within a group, and this
2283 is an archive which has already been searched, then
2284 force it to be researched unless the whole archive
2285 has been loaded already. */
2286 if (force
2287 && !s->input_statement.whole_archive
2288 && s->input_statement.loaded
2289 && bfd_check_format (s->input_statement.the_bfd,
2290 bfd_archive))
2291 s->input_statement.loaded = FALSE;
2293 lang_list_init (&add);
2295 if (! load_symbols (&s->input_statement, &add))
2296 config.make_executable = FALSE;
2298 if (add.head != NULL)
2300 *add.tail = s->header.next;
2301 s->header.next = add.head;
2304 break;
2305 default:
2306 break;
2311 /* If there are [COMMONS] statements, put a wild one into the bss
2312 section. */
2314 static void
2315 lang_reasonable_defaults (void)
2317 #if 0
2318 lang_output_section_statement_lookup (".text");
2319 lang_output_section_statement_lookup (".data");
2321 default_common_section = lang_output_section_statement_lookup (".bss");
2323 if (!placed_commons)
2325 lang_wild_statement_type *new =
2326 new_stat (lang_wild_statement,
2327 &default_common_section->children);
2329 new->section_name = "COMMON";
2330 new->filename = NULL;
2331 lang_list_init (&new->children);
2333 #endif
2336 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2338 void
2339 lang_track_definedness (const char *name)
2341 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2342 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2345 /* New-function for the definedness hash table. */
2347 static struct bfd_hash_entry *
2348 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2349 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2350 const char *name ATTRIBUTE_UNUSED)
2352 struct lang_definedness_hash_entry *ret
2353 = (struct lang_definedness_hash_entry *) entry;
2355 if (ret == NULL)
2356 ret = (struct lang_definedness_hash_entry *)
2357 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2359 if (ret == NULL)
2360 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2362 ret->iteration = -1;
2363 return &ret->root;
2366 /* Return the iteration when the definition of NAME was last updated. A
2367 value of -1 means that the symbol is not defined in the linker script
2368 or the command line, but may be defined in the linker symbol table. */
2371 lang_symbol_definition_iteration (const char *name)
2373 struct lang_definedness_hash_entry *defentry
2374 = (struct lang_definedness_hash_entry *)
2375 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2377 /* We've already created this one on the presence of DEFINED in the
2378 script, so it can't be NULL unless something is borked elsewhere in
2379 the code. */
2380 if (defentry == NULL)
2381 FAIL ();
2383 return defentry->iteration;
2386 /* Update the definedness state of NAME. */
2388 void
2389 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
2391 struct lang_definedness_hash_entry *defentry
2392 = (struct lang_definedness_hash_entry *)
2393 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2395 /* We don't keep track of symbols not tested with DEFINED. */
2396 if (defentry == NULL)
2397 return;
2399 /* If the symbol was already defined, and not from an earlier statement
2400 iteration, don't update the definedness iteration, because that'd
2401 make the symbol seem defined in the linker script at this point, and
2402 it wasn't; it was defined in some object. If we do anyway, DEFINED
2403 would start to yield false before this point and the construct "sym =
2404 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2405 in an object. */
2406 if (h->type != bfd_link_hash_undefined
2407 && h->type != bfd_link_hash_common
2408 && h->type != bfd_link_hash_new
2409 && defentry->iteration == -1)
2410 return;
2412 defentry->iteration = lang_statement_iteration;
2415 /* Add the supplied name to the symbol table as an undefined reference.
2416 This is a two step process as the symbol table doesn't even exist at
2417 the time the ld command line is processed. First we put the name
2418 on a list, then, once the output file has been opened, transfer the
2419 name to the symbol table. */
2421 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2423 #define ldlang_undef_chain_list_head entry_symbol.next
2425 void
2426 ldlang_add_undef (const char *const name)
2428 ldlang_undef_chain_list_type *new =
2429 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2431 new->next = ldlang_undef_chain_list_head;
2432 ldlang_undef_chain_list_head = new;
2434 new->name = xstrdup (name);
2436 if (output_bfd != NULL)
2437 insert_undefined (new->name);
2440 /* Insert NAME as undefined in the symbol table. */
2442 static void
2443 insert_undefined (const char *name)
2445 struct bfd_link_hash_entry *h;
2447 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2448 if (h == NULL)
2449 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2450 if (h->type == bfd_link_hash_new)
2452 h->type = bfd_link_hash_undefined;
2453 h->u.undef.abfd = NULL;
2454 bfd_link_add_undef (link_info.hash, h);
2458 /* Run through the list of undefineds created above and place them
2459 into the linker hash table as undefined symbols belonging to the
2460 script file. */
2462 static void
2463 lang_place_undefineds (void)
2465 ldlang_undef_chain_list_type *ptr;
2467 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2468 insert_undefined (ptr->name);
2471 /* Check for all readonly or some readwrite sections. */
2473 static void
2474 check_input_sections
2475 (lang_statement_union_type *s,
2476 lang_output_section_statement_type *output_section_statement)
2478 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2480 switch (s->header.type)
2482 case lang_wild_statement_enum:
2483 walk_wild (&s->wild_statement, check_section_callback,
2484 output_section_statement);
2485 if (! output_section_statement->all_input_readonly)
2486 return;
2487 break;
2488 case lang_constructors_statement_enum:
2489 check_input_sections (constructor_list.head,
2490 output_section_statement);
2491 if (! output_section_statement->all_input_readonly)
2492 return;
2493 break;
2494 case lang_group_statement_enum:
2495 check_input_sections (s->group_statement.children.head,
2496 output_section_statement);
2497 if (! output_section_statement->all_input_readonly)
2498 return;
2499 break;
2500 default:
2501 break;
2506 /* Update wildcard statements if needed. */
2508 static void
2509 update_wild_statements (lang_statement_union_type *s)
2511 struct wildcard_list *sec;
2513 switch (sort_section)
2515 default:
2516 FAIL ();
2518 case none:
2519 break;
2521 case by_name:
2522 case by_alignment:
2523 for (; s != NULL; s = s->header.next)
2525 switch (s->header.type)
2527 default:
2528 break;
2530 case lang_wild_statement_enum:
2531 sec = s->wild_statement.section_list;
2532 if (sec != NULL)
2534 switch (sec->spec.sorted)
2536 case none:
2537 sec->spec.sorted = sort_section;
2538 break;
2539 case by_name:
2540 if (sort_section == by_alignment)
2541 sec->spec.sorted = by_name_alignment;
2542 break;
2543 case by_alignment:
2544 if (sort_section == by_name)
2545 sec->spec.sorted = by_alignment_name;
2546 break;
2547 default:
2548 break;
2551 break;
2553 case lang_constructors_statement_enum:
2554 update_wild_statements (constructor_list.head);
2555 break;
2557 case lang_output_section_statement_enum:
2558 update_wild_statements
2559 (s->output_section_statement.children.head);
2560 break;
2562 case lang_group_statement_enum:
2563 update_wild_statements (s->group_statement.children.head);
2564 break;
2567 break;
2571 /* Open input files and attach to output sections. */
2573 static void
2574 map_input_to_output_sections
2575 (lang_statement_union_type *s, const char *target,
2576 lang_output_section_statement_type *os)
2578 for (; s != NULL; s = s->header.next)
2580 switch (s->header.type)
2582 case lang_wild_statement_enum:
2583 wild (&s->wild_statement, target, os);
2584 break;
2585 case lang_constructors_statement_enum:
2586 map_input_to_output_sections (constructor_list.head,
2587 target,
2588 os);
2589 break;
2590 case lang_output_section_statement_enum:
2591 if (s->output_section_statement.constraint)
2593 if (s->output_section_statement.constraint == -1)
2594 break;
2595 s->output_section_statement.all_input_readonly = TRUE;
2596 check_input_sections (s->output_section_statement.children.head,
2597 &s->output_section_statement);
2598 if ((s->output_section_statement.all_input_readonly
2599 && s->output_section_statement.constraint == ONLY_IF_RW)
2600 || (!s->output_section_statement.all_input_readonly
2601 && s->output_section_statement.constraint == ONLY_IF_RO))
2603 s->output_section_statement.constraint = -1;
2604 break;
2608 map_input_to_output_sections (s->output_section_statement.children.head,
2609 target,
2610 &s->output_section_statement);
2611 break;
2612 case lang_output_statement_enum:
2613 break;
2614 case lang_target_statement_enum:
2615 target = s->target_statement.target;
2616 break;
2617 case lang_group_statement_enum:
2618 map_input_to_output_sections (s->group_statement.children.head,
2619 target,
2620 os);
2621 break;
2622 case lang_data_statement_enum:
2623 /* Make sure that any sections mentioned in the expression
2624 are initialized. */
2625 exp_init_os (s->data_statement.exp);
2626 if (os != NULL && os->bfd_section == NULL)
2627 init_os (os);
2628 /* The output section gets contents, and then we inspect for
2629 any flags set in the input script which override any ALLOC. */
2630 os->bfd_section->flags |= SEC_HAS_CONTENTS;
2631 if (!(os->flags & SEC_NEVER_LOAD))
2632 os->bfd_section->flags |= SEC_ALLOC | SEC_LOAD;
2633 break;
2634 case lang_fill_statement_enum:
2635 case lang_input_section_enum:
2636 case lang_object_symbols_statement_enum:
2637 case lang_reloc_statement_enum:
2638 case lang_padding_statement_enum:
2639 case lang_input_statement_enum:
2640 if (os != NULL && os->bfd_section == NULL)
2641 init_os (os);
2642 break;
2643 case lang_assignment_statement_enum:
2644 if (os != NULL && os->bfd_section == NULL)
2645 init_os (os);
2647 /* Make sure that any sections mentioned in the assignment
2648 are initialized. */
2649 exp_init_os (s->assignment_statement.exp);
2650 break;
2651 case lang_afile_asection_pair_statement_enum:
2652 FAIL ();
2653 break;
2654 case lang_address_statement_enum:
2655 /* Mark the specified section with the supplied address.
2657 If this section was actually a segment marker, then the
2658 directive is ignored if the linker script explicitly
2659 processed the segment marker. Originally, the linker
2660 treated segment directives (like -Ttext on the
2661 command-line) as section directives. We honor the
2662 section directive semantics for backwards compatibilty;
2663 linker scripts that do not specifically check for
2664 SEGMENT_START automatically get the old semantics. */
2665 if (!s->address_statement.segment
2666 || !s->address_statement.segment->used)
2668 lang_output_section_statement_type *aos
2669 = (lang_output_section_statement_lookup
2670 (s->address_statement.section_name));
2672 if (aos->bfd_section == NULL)
2673 init_os (aos);
2674 aos->addr_tree = s->address_statement.address;
2676 break;
2681 /* An output section might have been removed after its statement was
2682 added. For example, ldemul_before_allocation can remove dynamic
2683 sections if they turn out to be not needed. Clean them up here. */
2685 static void
2686 strip_excluded_output_sections (void)
2688 lang_output_section_statement_type *os;
2690 for (os = &lang_output_section_statement.head->output_section_statement;
2691 os != NULL;
2692 os = os->next)
2694 asection *s;
2696 if (os->constraint == -1)
2697 continue;
2698 s = os->bfd_section;
2699 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2701 asection **p;
2703 os->bfd_section = NULL;
2705 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2706 if (*p == s)
2708 bfd_section_list_remove (output_bfd, p);
2709 output_bfd->section_count--;
2710 break;
2716 static void
2717 print_output_section_statement
2718 (lang_output_section_statement_type *output_section_statement)
2720 asection *section = output_section_statement->bfd_section;
2721 int len;
2723 if (output_section_statement != abs_output_section)
2725 minfo ("\n%s", output_section_statement->name);
2727 if (section != NULL)
2729 print_dot = section->vma;
2731 len = strlen (output_section_statement->name);
2732 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2734 print_nl ();
2735 len = 0;
2737 while (len < SECTION_NAME_MAP_LENGTH)
2739 print_space ();
2740 ++len;
2743 minfo ("0x%V %W", section->vma, section->size);
2745 if (output_section_statement->load_base != NULL)
2747 bfd_vma addr;
2749 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2750 "load base", lang_final_phase_enum);
2751 minfo (_(" load address 0x%V"), addr);
2755 print_nl ();
2758 print_statement_list (output_section_statement->children.head,
2759 output_section_statement);
2762 static void
2763 print_assignment (lang_assignment_statement_type *assignment,
2764 lang_output_section_statement_type *output_section)
2766 int i;
2767 int is_dot;
2768 etree_type *tree;
2769 etree_value_type result;
2771 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2772 print_space ();
2774 if (assignment->exp->type.node_class == etree_assert)
2776 is_dot = 0;
2777 tree = assignment->exp->assert_s.child;
2779 else
2781 const char *dst = assignment->exp->assign.dst;
2782 is_dot = dst[0] == '.' && dst[1] == 0;
2783 tree = assignment->exp->assign.src;
2786 result = exp_fold_tree (tree, output_section, lang_final_phase_enum,
2787 print_dot, &print_dot);
2788 if (result.valid_p)
2790 bfd_vma value;
2792 value = result.value + result.section->bfd_section->vma;
2794 minfo ("0x%V", value);
2795 if (is_dot)
2796 print_dot = value;
2798 else
2800 minfo ("*undef* ");
2801 #ifdef BFD64
2802 minfo (" ");
2803 #endif
2806 minfo (" ");
2807 exp_print_tree (assignment->exp);
2808 print_nl ();
2811 static void
2812 print_input_statement (lang_input_statement_type *statm)
2814 if (statm->filename != NULL)
2816 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2820 /* Print all symbols defined in a particular section. This is called
2821 via bfd_link_hash_traverse, or by print_all_symbols. */
2823 static bfd_boolean
2824 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2826 asection *sec = ptr;
2828 if ((hash_entry->type == bfd_link_hash_defined
2829 || hash_entry->type == bfd_link_hash_defweak)
2830 && sec == hash_entry->u.def.section)
2832 int i;
2834 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2835 print_space ();
2836 minfo ("0x%V ",
2837 (hash_entry->u.def.value
2838 + hash_entry->u.def.section->output_offset
2839 + hash_entry->u.def.section->output_section->vma));
2841 minfo (" %T\n", hash_entry->root.string);
2844 return TRUE;
2847 static void
2848 print_all_symbols (sec)
2849 asection *sec;
2851 struct fat_user_section_struct *ud = get_userdata (sec);
2852 struct map_symbol_def *def;
2854 if (!ud)
2855 return;
2857 *ud->map_symbol_def_tail = 0;
2858 for (def = ud->map_symbol_def_head; def; def = def->next)
2859 print_one_symbol (def->entry, sec);
2862 /* Print information about an input section to the map file. */
2864 static void
2865 print_input_section (lang_input_section_type *in)
2867 asection *i = in->section;
2868 bfd_size_type size = i->size;
2870 init_opb ();
2871 if (size != 0)
2873 int len;
2874 bfd_vma addr;
2876 print_space ();
2877 minfo ("%s", i->name);
2879 len = 1 + strlen (i->name);
2880 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2882 print_nl ();
2883 len = 0;
2885 while (len < SECTION_NAME_MAP_LENGTH)
2887 print_space ();
2888 ++len;
2891 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2892 addr = i->output_section->vma + i->output_offset;
2893 else
2895 addr = print_dot;
2896 size = 0;
2899 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2901 if (size != i->rawsize && i->rawsize != 0)
2903 len = SECTION_NAME_MAP_LENGTH + 3;
2904 #ifdef BFD64
2905 len += 16;
2906 #else
2907 len += 8;
2908 #endif
2909 while (len > 0)
2911 print_space ();
2912 --len;
2915 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2918 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2920 if (command_line.reduce_memory_overheads)
2921 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2922 else
2923 print_all_symbols (i);
2925 print_dot = addr + TO_ADDR (size);
2930 static void
2931 print_fill_statement (lang_fill_statement_type *fill)
2933 size_t size;
2934 unsigned char *p;
2935 fputs (" FILL mask 0x", config.map_file);
2936 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2937 fprintf (config.map_file, "%02x", *p);
2938 fputs ("\n", config.map_file);
2941 static void
2942 print_data_statement (lang_data_statement_type *data)
2944 int i;
2945 bfd_vma addr;
2946 bfd_size_type size;
2947 const char *name;
2949 init_opb ();
2950 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2951 print_space ();
2953 addr = data->output_vma;
2954 if (data->output_section != NULL)
2955 addr += data->output_section->vma;
2957 switch (data->type)
2959 default:
2960 abort ();
2961 case BYTE:
2962 size = BYTE_SIZE;
2963 name = "BYTE";
2964 break;
2965 case SHORT:
2966 size = SHORT_SIZE;
2967 name = "SHORT";
2968 break;
2969 case LONG:
2970 size = LONG_SIZE;
2971 name = "LONG";
2972 break;
2973 case QUAD:
2974 size = QUAD_SIZE;
2975 name = "QUAD";
2976 break;
2977 case SQUAD:
2978 size = QUAD_SIZE;
2979 name = "SQUAD";
2980 break;
2983 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2985 if (data->exp->type.node_class != etree_value)
2987 print_space ();
2988 exp_print_tree (data->exp);
2991 print_nl ();
2993 print_dot = addr + TO_ADDR (size);
2996 /* Print an address statement. These are generated by options like
2997 -Ttext. */
2999 static void
3000 print_address_statement (lang_address_statement_type *address)
3002 minfo (_("Address of section %s set to "), address->section_name);
3003 exp_print_tree (address->address);
3004 print_nl ();
3007 /* Print a reloc statement. */
3009 static void
3010 print_reloc_statement (lang_reloc_statement_type *reloc)
3012 int i;
3013 bfd_vma addr;
3014 bfd_size_type size;
3016 init_opb ();
3017 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
3018 print_space ();
3020 addr = reloc->output_vma;
3021 if (reloc->output_section != NULL)
3022 addr += reloc->output_section->vma;
3024 size = bfd_get_reloc_size (reloc->howto);
3026 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
3028 if (reloc->name != NULL)
3029 minfo ("%s+", reloc->name);
3030 else
3031 minfo ("%s+", reloc->section->name);
3033 exp_print_tree (reloc->addend_exp);
3035 print_nl ();
3037 print_dot = addr + TO_ADDR (size);
3040 static void
3041 print_padding_statement (lang_padding_statement_type *s)
3043 int len;
3044 bfd_vma addr;
3046 init_opb ();
3047 minfo (" *fill*");
3049 len = sizeof " *fill*" - 1;
3050 while (len < SECTION_NAME_MAP_LENGTH)
3052 print_space ();
3053 ++len;
3056 addr = s->output_offset;
3057 if (s->output_section != NULL)
3058 addr += s->output_section->vma;
3059 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
3061 if (s->fill->size != 0)
3063 size_t size;
3064 unsigned char *p;
3065 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
3066 fprintf (config.map_file, "%02x", *p);
3069 print_nl ();
3071 print_dot = addr + TO_ADDR (s->size);
3074 static void
3075 print_wild_statement (lang_wild_statement_type *w,
3076 lang_output_section_statement_type *os)
3078 struct wildcard_list *sec;
3080 print_space ();
3082 if (w->filenames_sorted)
3083 minfo ("SORT(");
3084 if (w->filename != NULL)
3085 minfo ("%s", w->filename);
3086 else
3087 minfo ("*");
3088 if (w->filenames_sorted)
3089 minfo (")");
3091 minfo ("(");
3092 for (sec = w->section_list; sec; sec = sec->next)
3094 if (sec->spec.sorted)
3095 minfo ("SORT(");
3096 if (sec->spec.exclude_name_list != NULL)
3098 name_list *tmp;
3099 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
3100 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
3101 minfo (" %s", tmp->name);
3102 minfo (") ");
3104 if (sec->spec.name != NULL)
3105 minfo ("%s", sec->spec.name);
3106 else
3107 minfo ("*");
3108 if (sec->spec.sorted)
3109 minfo (")");
3110 if (sec->next)
3111 minfo (" ");
3113 minfo (")");
3115 print_nl ();
3117 print_statement_list (w->children.head, os);
3120 /* Print a group statement. */
3122 static void
3123 print_group (lang_group_statement_type *s,
3124 lang_output_section_statement_type *os)
3126 fprintf (config.map_file, "START GROUP\n");
3127 print_statement_list (s->children.head, os);
3128 fprintf (config.map_file, "END GROUP\n");
3131 /* Print the list of statements in S.
3132 This can be called for any statement type. */
3134 static void
3135 print_statement_list (lang_statement_union_type *s,
3136 lang_output_section_statement_type *os)
3138 while (s != NULL)
3140 print_statement (s, os);
3141 s = s->header.next;
3145 /* Print the first statement in statement list S.
3146 This can be called for any statement type. */
3148 static void
3149 print_statement (lang_statement_union_type *s,
3150 lang_output_section_statement_type *os)
3152 switch (s->header.type)
3154 default:
3155 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
3156 FAIL ();
3157 break;
3158 case lang_constructors_statement_enum:
3159 if (constructor_list.head != NULL)
3161 if (constructors_sorted)
3162 minfo (" SORT (CONSTRUCTORS)\n");
3163 else
3164 minfo (" CONSTRUCTORS\n");
3165 print_statement_list (constructor_list.head, os);
3167 break;
3168 case lang_wild_statement_enum:
3169 print_wild_statement (&s->wild_statement, os);
3170 break;
3171 case lang_address_statement_enum:
3172 print_address_statement (&s->address_statement);
3173 break;
3174 case lang_object_symbols_statement_enum:
3175 minfo (" CREATE_OBJECT_SYMBOLS\n");
3176 break;
3177 case lang_fill_statement_enum:
3178 print_fill_statement (&s->fill_statement);
3179 break;
3180 case lang_data_statement_enum:
3181 print_data_statement (&s->data_statement);
3182 break;
3183 case lang_reloc_statement_enum:
3184 print_reloc_statement (&s->reloc_statement);
3185 break;
3186 case lang_input_section_enum:
3187 print_input_section (&s->input_section);
3188 break;
3189 case lang_padding_statement_enum:
3190 print_padding_statement (&s->padding_statement);
3191 break;
3192 case lang_output_section_statement_enum:
3193 print_output_section_statement (&s->output_section_statement);
3194 break;
3195 case lang_assignment_statement_enum:
3196 print_assignment (&s->assignment_statement, os);
3197 break;
3198 case lang_target_statement_enum:
3199 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
3200 break;
3201 case lang_output_statement_enum:
3202 minfo ("OUTPUT(%s", s->output_statement.name);
3203 if (output_target != NULL)
3204 minfo (" %s", output_target);
3205 minfo (")\n");
3206 break;
3207 case lang_input_statement_enum:
3208 print_input_statement (&s->input_statement);
3209 break;
3210 case lang_group_statement_enum:
3211 print_group (&s->group_statement, os);
3212 break;
3213 case lang_afile_asection_pair_statement_enum:
3214 FAIL ();
3215 break;
3219 static void
3220 print_statements (void)
3222 print_statement_list (statement_list.head, abs_output_section);
3225 /* Print the first N statements in statement list S to STDERR.
3226 If N == 0, nothing is printed.
3227 If N < 0, the entire list is printed.
3228 Intended to be called from GDB. */
3230 void
3231 dprint_statement (lang_statement_union_type *s, int n)
3233 FILE *map_save = config.map_file;
3235 config.map_file = stderr;
3237 if (n < 0)
3238 print_statement_list (s, abs_output_section);
3239 else
3241 while (s && --n >= 0)
3243 print_statement (s, abs_output_section);
3244 s = s->header.next;
3248 config.map_file = map_save;
3251 static void
3252 insert_pad (lang_statement_union_type **ptr,
3253 fill_type *fill,
3254 unsigned int alignment_needed,
3255 asection *output_section,
3256 bfd_vma dot)
3258 static fill_type zero_fill = { 1, { 0 } };
3259 lang_statement_union_type *pad;
3261 pad = ((lang_statement_union_type *)
3262 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
3263 if (ptr != &statement_list.head
3264 && pad->header.type == lang_padding_statement_enum
3265 && pad->padding_statement.output_section == output_section)
3267 /* Use the existing pad statement. The above test on output
3268 section is probably redundant, but it doesn't hurt to check. */
3270 else
3272 /* Make a new padding statement, linked into existing chain. */
3273 pad = stat_alloc (sizeof (lang_padding_statement_type));
3274 pad->header.next = *ptr;
3275 *ptr = pad;
3276 pad->header.type = lang_padding_statement_enum;
3277 pad->padding_statement.output_section = output_section;
3278 if (fill == NULL)
3279 fill = &zero_fill;
3280 pad->padding_statement.fill = fill;
3282 pad->padding_statement.output_offset = dot - output_section->vma;
3283 pad->padding_statement.size = alignment_needed;
3284 output_section->size += alignment_needed;
3287 /* Work out how much this section will move the dot point. */
3289 static bfd_vma
3290 size_input_section
3291 (lang_statement_union_type **this_ptr,
3292 lang_output_section_statement_type *output_section_statement,
3293 fill_type *fill,
3294 bfd_vma dot)
3296 lang_input_section_type *is = &((*this_ptr)->input_section);
3297 asection *i = is->section;
3299 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
3301 unsigned int alignment_needed;
3302 asection *o;
3304 /* Align this section first to the input sections requirement,
3305 then to the output section's requirement. If this alignment
3306 is greater than any seen before, then record it too. Perform
3307 the alignment by inserting a magic 'padding' statement. */
3309 if (output_section_statement->subsection_alignment != -1)
3310 i->alignment_power = output_section_statement->subsection_alignment;
3312 o = output_section_statement->bfd_section;
3313 if (o->alignment_power < i->alignment_power)
3314 o->alignment_power = i->alignment_power;
3316 alignment_needed = align_power (dot, i->alignment_power) - dot;
3318 if (alignment_needed != 0)
3320 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
3321 dot += alignment_needed;
3324 /* Remember where in the output section this input section goes. */
3326 i->output_offset = dot - o->vma;
3328 /* Mark how big the output section must be to contain this now. */
3329 dot += TO_ADDR (i->size);
3330 o->size = TO_SIZE (dot - o->vma);
3332 else
3334 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
3337 return dot;
3340 #define IGNORE_SECTION(s) \
3341 ((s->flags & SEC_NEVER_LOAD) != 0 \
3342 || (s->flags & SEC_ALLOC) == 0 \
3343 || ((s->flags & SEC_THREAD_LOCAL) != 0 \
3344 && (s->flags & SEC_LOAD) == 0) \
3345 || s->size == 0)
3347 /* Check to see if any allocated sections overlap with other allocated
3348 sections. This can happen if a linker script specifies the output
3349 section addresses of the two sections. */
3351 static void
3352 lang_check_section_addresses (void)
3354 asection *s;
3356 /* Scan all sections in the output list. */
3357 for (s = output_bfd->sections; s != NULL; s = s->next)
3359 asection *os;
3361 /* Ignore sections which are not loaded or which have no contents. */
3362 if (IGNORE_SECTION (s))
3363 continue;
3365 /* Once we reach section 's' stop our seach. This prevents two
3366 warning messages from being produced, one for 'section A overlaps
3367 section B' and one for 'section B overlaps section A'. */
3368 for (os = output_bfd->sections; os != s; os = os->next)
3370 bfd_vma s_start;
3371 bfd_vma s_end;
3372 bfd_vma os_start;
3373 bfd_vma os_end;
3375 /* Only consider loadable sections with real contents. */
3376 if (IGNORE_SECTION (os))
3377 continue;
3379 /* We must check the sections' LMA addresses not their
3380 VMA addresses because overlay sections can have
3381 overlapping VMAs but they must have distinct LMAs. */
3382 s_start = bfd_section_lma (output_bfd, s);
3383 os_start = bfd_section_lma (output_bfd, os);
3384 s_end = s_start + TO_ADDR (s->size) - 1;
3385 os_end = os_start + TO_ADDR (os->size) - 1;
3387 /* Look for an overlap. */
3388 if ((s_end < os_start) || (s_start > os_end))
3389 continue;
3391 einfo (
3392 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
3393 s->name, s_start, s_end, os->name, os_start, os_end);
3395 /* Once we have found one overlap for this section,
3396 stop looking for others. */
3397 break;
3402 /* Make sure the new address is within the region. We explicitly permit the
3403 current address to be at the exact end of the region when the address is
3404 non-zero, in case the region is at the end of addressable memory and the
3405 calculation wraps around. */
3407 static void
3408 os_region_check (lang_output_section_statement_type *os,
3409 lang_memory_region_type *region,
3410 etree_type *tree,
3411 bfd_vma base)
3413 if ((region->current < region->origin
3414 || (region->current - region->origin > region->length))
3415 && ((region->current != region->origin + region->length)
3416 || base == 0))
3418 if (tree != NULL)
3420 einfo (_("%X%P: address 0x%v of %B section %s"
3421 " is not within region %s\n"),
3422 region->current,
3423 os->bfd_section->owner,
3424 os->bfd_section->name,
3425 region->name);
3427 else
3429 einfo (_("%X%P: region %s is full (%B section %s)\n"),
3430 region->name,
3431 os->bfd_section->owner,
3432 os->bfd_section->name);
3434 /* Reset the region pointer. */
3435 region->current = region->origin;
3439 /* Set the sizes for all the output sections. */
3441 static bfd_vma
3442 lang_size_sections_1
3443 (lang_statement_union_type *s,
3444 lang_output_section_statement_type *output_section_statement,
3445 lang_statement_union_type **prev,
3446 fill_type *fill,
3447 bfd_vma dot,
3448 bfd_boolean *relax,
3449 bfd_boolean check_regions)
3451 /* Size up the sections from their constituent parts. */
3452 for (; s != NULL; s = s->header.next)
3454 switch (s->header.type)
3456 case lang_output_section_statement_enum:
3458 bfd_vma after;
3459 lang_output_section_statement_type *os;
3461 os = &s->output_section_statement;
3462 if (os->bfd_section == NULL)
3463 /* This section was never actually created. */
3464 break;
3466 /* If this is a COFF shared library section, use the size and
3467 address from the input section. FIXME: This is COFF
3468 specific; it would be cleaner if there were some other way
3469 to do this, but nothing simple comes to mind. */
3470 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
3472 asection *input;
3474 if (os->children.head == NULL
3475 || os->children.head->header.next != NULL
3476 || (os->children.head->header.type
3477 != lang_input_section_enum))
3478 einfo (_("%P%X: Internal error on COFF shared library"
3479 " section %s\n"), os->name);
3481 input = os->children.head->input_section.section;
3482 bfd_set_section_vma (os->bfd_section->owner,
3483 os->bfd_section,
3484 bfd_section_vma (input->owner, input));
3485 os->bfd_section->size = input->size;
3486 break;
3489 if (bfd_is_abs_section (os->bfd_section))
3491 /* No matter what happens, an abs section starts at zero. */
3492 ASSERT (os->bfd_section->vma == 0);
3494 else
3496 if (os->addr_tree == NULL)
3498 /* No address specified for this section, get one
3499 from the region specification. */
3500 if (os->region == NULL
3501 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
3502 && os->region->name[0] == '*'
3503 && strcmp (os->region->name,
3504 DEFAULT_MEMORY_REGION) == 0))
3506 os->region = lang_memory_default (os->bfd_section);
3509 /* If a loadable section is using the default memory
3510 region, and some non default memory regions were
3511 defined, issue an error message. */
3512 if (!IGNORE_SECTION (os->bfd_section)
3513 && ! link_info.relocatable
3514 && check_regions
3515 && strcmp (os->region->name,
3516 DEFAULT_MEMORY_REGION) == 0
3517 && lang_memory_region_list != NULL
3518 && (strcmp (lang_memory_region_list->name,
3519 DEFAULT_MEMORY_REGION) != 0
3520 || lang_memory_region_list->next != NULL))
3522 /* By default this is an error rather than just a
3523 warning because if we allocate the section to the
3524 default memory region we can end up creating an
3525 excessively large binary, or even seg faulting when
3526 attempting to perform a negative seek. See
3527 sources.redhat.com/ml/binutils/2003-04/msg00423.html
3528 for an example of this. This behaviour can be
3529 overridden by the using the --no-check-sections
3530 switch. */
3531 if (command_line.check_section_addresses)
3532 einfo (_("%P%F: error: no memory region specified"
3533 " for loadable section `%s'\n"),
3534 bfd_get_section_name (output_bfd,
3535 os->bfd_section));
3536 else
3537 einfo (_("%P: warning: no memory region specified"
3538 " for loadable section `%s'\n"),
3539 bfd_get_section_name (output_bfd,
3540 os->bfd_section));
3543 dot = os->region->current;
3545 if (os->section_alignment == -1)
3547 bfd_vma olddot;
3549 olddot = dot;
3550 dot = align_power (dot,
3551 os->bfd_section->alignment_power);
3553 if (dot != olddot && config.warn_section_align)
3554 einfo (_("%P: warning: changing start of section"
3555 " %s by %u bytes\n"),
3556 os->name, (unsigned int) (dot - olddot));
3559 else
3561 etree_value_type r;
3563 os->processed = -1;
3564 r = exp_fold_tree (os->addr_tree,
3565 abs_output_section,
3566 lang_allocating_phase_enum,
3567 dot, &dot);
3568 os->processed = 0;
3570 if (!r.valid_p)
3571 einfo (_("%F%S: non constant or forward reference"
3572 " address expression for section %s\n"),
3573 os->name);
3575 dot = r.value + r.section->bfd_section->vma;
3578 /* The section starts here.
3579 First, align to what the section needs. */
3581 if (os->section_alignment != -1)
3582 dot = align_power (dot, os->section_alignment);
3584 bfd_set_section_vma (0, os->bfd_section, dot);
3586 os->bfd_section->output_offset = 0;
3589 lang_size_sections_1 (os->children.head, os, &os->children.head,
3590 os->fill, dot, relax, check_regions);
3592 /* Put the section within the requested block size, or
3593 align at the block boundary. */
3594 after = ((os->bfd_section->vma
3595 + TO_ADDR (os->bfd_section->size)
3596 + os->block_value - 1)
3597 & - (bfd_vma) os->block_value);
3599 if (bfd_is_abs_section (os->bfd_section))
3600 ASSERT (after == os->bfd_section->vma);
3601 else
3602 os->bfd_section->size
3603 = TO_SIZE (after - os->bfd_section->vma);
3605 dot = os->bfd_section->vma;
3606 /* .tbss sections effectively have zero size. */
3607 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3608 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3609 || link_info.relocatable)
3610 dot += TO_ADDR (os->bfd_section->size);
3612 os->processed = 1;
3614 if (os->update_dot_tree != 0)
3615 exp_fold_tree (os->update_dot_tree, abs_output_section,
3616 lang_allocating_phase_enum, dot, &dot);
3618 /* Update dot in the region ?
3619 We only do this if the section is going to be allocated,
3620 since unallocated sections do not contribute to the region's
3621 overall size in memory.
3623 If the SEC_NEVER_LOAD bit is not set, it will affect the
3624 addresses of sections after it. We have to update
3625 dot. */
3626 if (os->region != NULL
3627 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3628 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3630 os->region->current = dot;
3632 if (check_regions)
3633 /* Make sure the new address is within the region. */
3634 os_region_check (os, os->region, os->addr_tree,
3635 os->bfd_section->vma);
3637 /* If there's no load address specified, use the run
3638 region as the load region. */
3639 if (os->lma_region == NULL && os->load_base == NULL)
3640 os->lma_region = os->region;
3642 if (os->lma_region != NULL && os->lma_region != os->region)
3644 /* Set load_base, which will be handled later. */
3645 os->load_base = exp_intop (os->lma_region->current);
3646 os->lma_region->current +=
3647 TO_ADDR (os->bfd_section->size);
3648 if (check_regions)
3649 os_region_check (os, os->lma_region, NULL,
3650 os->bfd_section->lma);
3654 break;
3656 case lang_constructors_statement_enum:
3657 dot = lang_size_sections_1 (constructor_list.head,
3658 output_section_statement,
3659 &s->wild_statement.children.head,
3660 fill, dot, relax, check_regions);
3661 break;
3663 case lang_data_statement_enum:
3665 unsigned int size = 0;
3667 s->data_statement.output_vma =
3668 dot - output_section_statement->bfd_section->vma;
3669 s->data_statement.output_section =
3670 output_section_statement->bfd_section;
3672 /* We might refer to provided symbols in the expression, and
3673 need to mark them as needed. */
3674 exp_fold_tree (s->data_statement.exp, abs_output_section,
3675 lang_allocating_phase_enum, dot, &dot);
3677 switch (s->data_statement.type)
3679 default:
3680 abort ();
3681 case QUAD:
3682 case SQUAD:
3683 size = QUAD_SIZE;
3684 break;
3685 case LONG:
3686 size = LONG_SIZE;
3687 break;
3688 case SHORT:
3689 size = SHORT_SIZE;
3690 break;
3691 case BYTE:
3692 size = BYTE_SIZE;
3693 break;
3695 if (size < TO_SIZE ((unsigned) 1))
3696 size = TO_SIZE ((unsigned) 1);
3697 dot += TO_ADDR (size);
3698 output_section_statement->bfd_section->size += size;
3700 break;
3702 case lang_reloc_statement_enum:
3704 int size;
3706 s->reloc_statement.output_vma =
3707 dot - output_section_statement->bfd_section->vma;
3708 s->reloc_statement.output_section =
3709 output_section_statement->bfd_section;
3710 size = bfd_get_reloc_size (s->reloc_statement.howto);
3711 dot += TO_ADDR (size);
3712 output_section_statement->bfd_section->size += size;
3714 break;
3716 case lang_wild_statement_enum:
3718 dot = lang_size_sections_1 (s->wild_statement.children.head,
3719 output_section_statement,
3720 &s->wild_statement.children.head,
3721 fill, dot, relax, check_regions);
3723 break;
3725 case lang_object_symbols_statement_enum:
3726 link_info.create_object_symbols_section =
3727 output_section_statement->bfd_section;
3728 break;
3729 case lang_output_statement_enum:
3730 case lang_target_statement_enum:
3731 break;
3732 case lang_input_section_enum:
3734 asection *i;
3736 i = (*prev)->input_section.section;
3737 if (relax)
3739 bfd_boolean again;
3741 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3742 einfo (_("%P%F: can't relax section: %E\n"));
3743 if (again)
3744 *relax = TRUE;
3746 dot = size_input_section (prev, output_section_statement,
3747 output_section_statement->fill, dot);
3749 break;
3750 case lang_input_statement_enum:
3751 break;
3752 case lang_fill_statement_enum:
3753 s->fill_statement.output_section =
3754 output_section_statement->bfd_section;
3756 fill = s->fill_statement.fill;
3757 break;
3758 case lang_assignment_statement_enum:
3760 bfd_vma newdot = dot;
3762 exp_fold_tree (s->assignment_statement.exp,
3763 output_section_statement,
3764 lang_allocating_phase_enum,
3765 dot,
3766 &newdot);
3768 if (newdot != dot)
3770 if (output_section_statement == abs_output_section)
3772 /* If we don't have an output section, then just adjust
3773 the default memory address. */
3774 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3775 FALSE)->current = newdot;
3777 else
3779 /* Insert a pad after this statement. We can't
3780 put the pad before when relaxing, in case the
3781 assignment references dot. */
3782 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3783 output_section_statement->bfd_section, dot);
3785 /* Don't neuter the pad below when relaxing. */
3786 s = s->header.next;
3789 /* If dot is advanced, this implies that the section should
3790 have space allocated to it, unless the user has explicitly
3791 stated that the section should never be loaded. */
3792 if (!(output_section_statement->flags
3793 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3794 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3796 dot = newdot;
3799 break;
3801 case lang_padding_statement_enum:
3802 /* If this is the first time lang_size_sections is called,
3803 we won't have any padding statements. If this is the
3804 second or later passes when relaxing, we should allow
3805 padding to shrink. If padding is needed on this pass, it
3806 will be added back in. */
3807 s->padding_statement.size = 0;
3809 /* Make sure output_offset is valid. If relaxation shrinks
3810 the section and this pad isn't needed, it's possible to
3811 have output_offset larger than the final size of the
3812 section. bfd_set_section_contents will complain even for
3813 a pad size of zero. */
3814 s->padding_statement.output_offset
3815 = dot - output_section_statement->bfd_section->vma;
3816 break;
3818 case lang_group_statement_enum:
3819 dot = lang_size_sections_1 (s->group_statement.children.head,
3820 output_section_statement,
3821 &s->group_statement.children.head,
3822 fill, dot, relax, check_regions);
3823 break;
3825 default:
3826 FAIL ();
3827 break;
3829 /* We can only get here when relaxing is turned on. */
3830 case lang_address_statement_enum:
3831 break;
3833 prev = &s->header.next;
3835 return dot;
3838 bfd_vma
3839 lang_size_sections
3840 (lang_statement_union_type *s,
3841 lang_output_section_statement_type *output_section_statement,
3842 lang_statement_union_type **prev,
3843 fill_type *fill,
3844 bfd_vma dot,
3845 bfd_boolean *relax,
3846 bfd_boolean check_regions)
3848 bfd_vma result;
3850 /* Callers of exp_fold_tree need to increment this. */
3851 lang_statement_iteration++;
3853 exp_data_seg.phase = exp_dataseg_none;
3854 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3855 dot, relax, check_regions);
3856 if (exp_data_seg.phase == exp_dataseg_end_seen
3857 && link_info.relro && exp_data_seg.relro_end)
3859 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3860 to put exp_data_seg.relro on a (common) page boundary. */
3861 bfd_vma old_min_base, relro_end, maxpage;
3863 exp_data_seg.phase = exp_dataseg_relro_adjust;
3864 old_min_base = exp_data_seg.min_base;
3865 maxpage = exp_data_seg.maxpagesize;
3866 exp_data_seg.base += (-exp_data_seg.relro_end
3867 & (exp_data_seg.pagesize - 1));
3868 /* Compute the expected PT_GNU_RELRO segment end. */
3869 relro_end = (exp_data_seg.relro_end + exp_data_seg.pagesize - 1)
3870 & ~(exp_data_seg.pagesize - 1);
3871 if (old_min_base + maxpage < exp_data_seg.base)
3873 exp_data_seg.base -= maxpage;
3874 relro_end -= maxpage;
3876 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3877 dot, relax, check_regions);
3878 if (exp_data_seg.relro_end > relro_end)
3880 /* The alignment of sections between DATA_SEGMENT_ALIGN
3881 and DATA_SEGMENT_RELRO_END caused huge padding to be
3882 inserted at DATA_SEGMENT_RELRO_END. Try some other base. */
3883 asection *sec;
3884 unsigned int max_alignment_power = 0;
3886 /* Find maximum alignment power of sections between
3887 DATA_SEGMENT_ALIGN and DATA_SEGMENT_RELRO_END. */
3888 for (sec = output_bfd->sections; sec; sec = sec->next)
3889 if (sec->vma >= exp_data_seg.base
3890 && sec->vma < exp_data_seg.relro_end
3891 && sec->alignment_power > max_alignment_power)
3892 max_alignment_power = sec->alignment_power;
3894 if (((bfd_vma) 1 << max_alignment_power) < exp_data_seg.pagesize)
3896 if (exp_data_seg.base - (1 << max_alignment_power)
3897 < old_min_base)
3898 exp_data_seg.base += exp_data_seg.pagesize;
3899 exp_data_seg.base -= (1 << max_alignment_power);
3900 result = lang_size_sections_1 (s, output_section_statement,
3901 prev, fill, dot, relax,
3902 check_regions);
3905 link_info.relro_start = exp_data_seg.base;
3906 link_info.relro_end = exp_data_seg.relro_end;
3908 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3910 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3911 a page could be saved in the data segment. */
3912 bfd_vma first, last;
3914 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3915 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3916 if (first && last
3917 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3918 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3919 && first + last <= exp_data_seg.pagesize)
3921 exp_data_seg.phase = exp_dataseg_adjust;
3922 lang_statement_iteration++;
3923 result = lang_size_sections_1 (s, output_section_statement, prev,
3924 fill, dot, relax, check_regions);
3928 return result;
3931 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3933 static bfd_vma
3934 lang_do_assignments_1
3935 (lang_statement_union_type *s,
3936 lang_output_section_statement_type *output_section_statement,
3937 fill_type *fill,
3938 bfd_vma dot)
3940 for (; s != NULL; s = s->header.next)
3942 switch (s->header.type)
3944 case lang_constructors_statement_enum:
3945 dot = lang_do_assignments_1 (constructor_list.head,
3946 output_section_statement,
3947 fill,
3948 dot);
3949 break;
3951 case lang_output_section_statement_enum:
3953 lang_output_section_statement_type *os;
3955 os = &(s->output_section_statement);
3956 if (os->bfd_section != NULL)
3958 dot = os->bfd_section->vma;
3959 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3960 /* .tbss sections effectively have zero size. */
3961 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3962 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3963 || link_info.relocatable)
3964 dot += TO_ADDR (os->bfd_section->size);
3966 if (os->load_base)
3968 /* If nothing has been placed into the output section then
3969 it won't have a bfd_section. */
3970 if (os->bfd_section)
3972 os->bfd_section->lma
3973 = exp_get_abs_int (os->load_base, 0, "load base",
3974 lang_final_phase_enum);
3978 break;
3979 case lang_wild_statement_enum:
3981 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3982 output_section_statement,
3983 fill, dot);
3985 break;
3987 case lang_object_symbols_statement_enum:
3988 case lang_output_statement_enum:
3989 case lang_target_statement_enum:
3990 #if 0
3991 case lang_common_statement_enum:
3992 #endif
3993 break;
3994 case lang_data_statement_enum:
3996 etree_value_type value;
3998 value = exp_fold_tree (s->data_statement.exp,
3999 abs_output_section,
4000 lang_final_phase_enum, dot, &dot);
4001 if (!value.valid_p)
4002 einfo (_("%F%P: invalid data statement\n"));
4003 s->data_statement.value
4004 = value.value + value.section->bfd_section->vma;
4007 unsigned int size;
4008 switch (s->data_statement.type)
4010 default:
4011 abort ();
4012 case QUAD:
4013 case SQUAD:
4014 size = QUAD_SIZE;
4015 break;
4016 case LONG:
4017 size = LONG_SIZE;
4018 break;
4019 case SHORT:
4020 size = SHORT_SIZE;
4021 break;
4022 case BYTE:
4023 size = BYTE_SIZE;
4024 break;
4026 if (size < TO_SIZE ((unsigned) 1))
4027 size = TO_SIZE ((unsigned) 1);
4028 dot += TO_ADDR (size);
4030 break;
4032 case lang_reloc_statement_enum:
4034 etree_value_type value;
4036 value = exp_fold_tree (s->reloc_statement.addend_exp,
4037 abs_output_section,
4038 lang_final_phase_enum, dot, &dot);
4039 s->reloc_statement.addend_value = value.value;
4040 if (!value.valid_p)
4041 einfo (_("%F%P: invalid reloc statement\n"));
4043 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
4044 break;
4046 case lang_input_section_enum:
4048 asection *in = s->input_section.section;
4050 if ((in->flags & SEC_EXCLUDE) == 0)
4051 dot += TO_ADDR (in->size);
4053 break;
4055 case lang_input_statement_enum:
4056 break;
4057 case lang_fill_statement_enum:
4058 fill = s->fill_statement.fill;
4059 break;
4060 case lang_assignment_statement_enum:
4062 exp_fold_tree (s->assignment_statement.exp,
4063 output_section_statement,
4064 lang_final_phase_enum,
4065 dot,
4066 &dot);
4069 break;
4070 case lang_padding_statement_enum:
4071 dot += TO_ADDR (s->padding_statement.size);
4072 break;
4074 case lang_group_statement_enum:
4075 dot = lang_do_assignments_1 (s->group_statement.children.head,
4076 output_section_statement,
4077 fill, dot);
4079 break;
4081 default:
4082 FAIL ();
4083 break;
4084 case lang_address_statement_enum:
4085 break;
4089 return dot;
4092 void
4093 lang_do_assignments
4094 (lang_statement_union_type *s,
4095 lang_output_section_statement_type *output_section_statement,
4096 fill_type *fill,
4097 bfd_vma dot)
4099 /* Callers of exp_fold_tree need to increment this. */
4100 lang_statement_iteration++;
4101 lang_do_assignments_1 (s, output_section_statement, fill, dot);
4104 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
4105 operator .startof. (section_name), it produces an undefined symbol
4106 .startof.section_name. Similarly, when it sees
4107 .sizeof. (section_name), it produces an undefined symbol
4108 .sizeof.section_name. For all the output sections, we look for
4109 such symbols, and set them to the correct value. */
4111 static void
4112 lang_set_startof (void)
4114 asection *s;
4116 if (link_info.relocatable)
4117 return;
4119 for (s = output_bfd->sections; s != NULL; s = s->next)
4121 const char *secname;
4122 char *buf;
4123 struct bfd_link_hash_entry *h;
4125 secname = bfd_get_section_name (output_bfd, s);
4126 buf = xmalloc (10 + strlen (secname));
4128 sprintf (buf, ".startof.%s", secname);
4129 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4130 if (h != NULL && h->type == bfd_link_hash_undefined)
4132 h->type = bfd_link_hash_defined;
4133 h->u.def.value = bfd_get_section_vma (output_bfd, s);
4134 h->u.def.section = bfd_abs_section_ptr;
4137 sprintf (buf, ".sizeof.%s", secname);
4138 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
4139 if (h != NULL && h->type == bfd_link_hash_undefined)
4141 h->type = bfd_link_hash_defined;
4142 h->u.def.value = TO_ADDR (s->size);
4143 h->u.def.section = bfd_abs_section_ptr;
4146 free (buf);
4150 static void
4151 lang_finish (void)
4153 struct bfd_link_hash_entry *h;
4154 bfd_boolean warn;
4156 if (link_info.relocatable || link_info.shared)
4157 warn = FALSE;
4158 else
4159 warn = TRUE;
4161 if (entry_symbol.name == NULL)
4163 /* No entry has been specified. Look for start, but don't warn
4164 if we don't find it. */
4165 entry_symbol.name = "start";
4166 warn = FALSE;
4169 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
4170 FALSE, FALSE, TRUE);
4171 if (h != NULL
4172 && (h->type == bfd_link_hash_defined
4173 || h->type == bfd_link_hash_defweak)
4174 && h->u.def.section->output_section != NULL)
4176 bfd_vma val;
4178 val = (h->u.def.value
4179 + bfd_get_section_vma (output_bfd,
4180 h->u.def.section->output_section)
4181 + h->u.def.section->output_offset);
4182 if (! bfd_set_start_address (output_bfd, val))
4183 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
4185 else
4187 bfd_vma val;
4188 const char *send;
4190 /* We couldn't find the entry symbol. Try parsing it as a
4191 number. */
4192 val = bfd_scan_vma (entry_symbol.name, &send, 0);
4193 if (*send == '\0')
4195 if (! bfd_set_start_address (output_bfd, val))
4196 einfo (_("%P%F: can't set start address\n"));
4198 else
4200 asection *ts;
4202 /* Can't find the entry symbol, and it's not a number. Use
4203 the first address in the text section. */
4204 ts = bfd_get_section_by_name (output_bfd, entry_section);
4205 if (ts != NULL)
4207 if (warn)
4208 einfo (_("%P: warning: cannot find entry symbol %s;"
4209 " defaulting to %V\n"),
4210 entry_symbol.name,
4211 bfd_get_section_vma (output_bfd, ts));
4212 if (! bfd_set_start_address (output_bfd,
4213 bfd_get_section_vma (output_bfd,
4214 ts)))
4215 einfo (_("%P%F: can't set start address\n"));
4217 else
4219 if (warn)
4220 einfo (_("%P: warning: cannot find entry symbol %s;"
4221 " not setting start address\n"),
4222 entry_symbol.name);
4227 /* Don't bfd_hash_table_free (&lang_definedness_table);
4228 map file output may result in a call of lang_track_definedness. */
4231 /* This is a small function used when we want to ignore errors from
4232 BFD. */
4234 static void
4235 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
4237 /* Don't do anything. */
4240 /* Check that the architecture of all the input files is compatible
4241 with the output file. Also call the backend to let it do any
4242 other checking that is needed. */
4244 static void
4245 lang_check (void)
4247 lang_statement_union_type *file;
4248 bfd *input_bfd;
4249 const bfd_arch_info_type *compatible;
4251 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
4253 input_bfd = file->input_statement.the_bfd;
4254 compatible
4255 = bfd_arch_get_compatible (input_bfd, output_bfd,
4256 command_line.accept_unknown_input_arch);
4258 /* In general it is not possible to perform a relocatable
4259 link between differing object formats when the input
4260 file has relocations, because the relocations in the
4261 input format may not have equivalent representations in
4262 the output format (and besides BFD does not translate
4263 relocs for other link purposes than a final link). */
4264 if ((link_info.relocatable || link_info.emitrelocations)
4265 && (compatible == NULL
4266 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
4267 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
4269 einfo (_("%P%F: Relocatable linking with relocations from"
4270 " format %s (%B) to format %s (%B) is not supported\n"),
4271 bfd_get_target (input_bfd), input_bfd,
4272 bfd_get_target (output_bfd), output_bfd);
4273 /* einfo with %F exits. */
4276 if (compatible == NULL)
4278 if (command_line.warn_mismatch)
4279 einfo (_("%P: warning: %s architecture of input file `%B'"
4280 " is incompatible with %s output\n"),
4281 bfd_printable_name (input_bfd), input_bfd,
4282 bfd_printable_name (output_bfd));
4284 else if (bfd_count_sections (input_bfd))
4286 /* If the input bfd has no contents, it shouldn't set the
4287 private data of the output bfd. */
4289 bfd_error_handler_type pfn = NULL;
4291 /* If we aren't supposed to warn about mismatched input
4292 files, temporarily set the BFD error handler to a
4293 function which will do nothing. We still want to call
4294 bfd_merge_private_bfd_data, since it may set up
4295 information which is needed in the output file. */
4296 if (! command_line.warn_mismatch)
4297 pfn = bfd_set_error_handler (ignore_bfd_errors);
4298 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
4300 if (command_line.warn_mismatch)
4301 einfo (_("%P%X: failed to merge target specific data"
4302 " of file %B\n"), input_bfd);
4304 if (! command_line.warn_mismatch)
4305 bfd_set_error_handler (pfn);
4310 /* Look through all the global common symbols and attach them to the
4311 correct section. The -sort-common command line switch may be used
4312 to roughly sort the entries by size. */
4314 static void
4315 lang_common (void)
4317 if (command_line.inhibit_common_definition)
4318 return;
4319 if (link_info.relocatable
4320 && ! command_line.force_common_definition)
4321 return;
4323 if (! config.sort_common)
4324 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
4325 else
4327 int power;
4329 for (power = 4; power >= 0; power--)
4330 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
4334 /* Place one common symbol in the correct section. */
4336 static bfd_boolean
4337 lang_one_common (struct bfd_link_hash_entry *h, void *info)
4339 unsigned int power_of_two;
4340 bfd_vma size;
4341 asection *section;
4343 if (h->type != bfd_link_hash_common)
4344 return TRUE;
4346 size = h->u.c.size;
4347 power_of_two = h->u.c.p->alignment_power;
4349 if (config.sort_common
4350 && power_of_two < (unsigned int) *(int *) info)
4351 return TRUE;
4353 section = h->u.c.p->section;
4355 /* Increase the size of the section to align the common sym. */
4356 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
4357 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
4359 /* Adjust the alignment if necessary. */
4360 if (power_of_two > section->alignment_power)
4361 section->alignment_power = power_of_two;
4363 /* Change the symbol from common to defined. */
4364 h->type = bfd_link_hash_defined;
4365 h->u.def.section = section;
4366 h->u.def.value = section->size;
4368 /* Increase the size of the section. */
4369 section->size += size;
4371 /* Make sure the section is allocated in memory, and make sure that
4372 it is no longer a common section. */
4373 section->flags |= SEC_ALLOC;
4374 section->flags &= ~SEC_IS_COMMON;
4376 if (config.map_file != NULL)
4378 static bfd_boolean header_printed;
4379 int len;
4380 char *name;
4381 char buf[50];
4383 if (! header_printed)
4385 minfo (_("\nAllocating common symbols\n"));
4386 minfo (_("Common symbol size file\n\n"));
4387 header_printed = TRUE;
4390 name = demangle (h->root.string);
4391 minfo ("%s", name);
4392 len = strlen (name);
4393 free (name);
4395 if (len >= 19)
4397 print_nl ();
4398 len = 0;
4400 while (len < 20)
4402 print_space ();
4403 ++len;
4406 minfo ("0x");
4407 if (size <= 0xffffffff)
4408 sprintf (buf, "%lx", (unsigned long) size);
4409 else
4410 sprintf_vma (buf, size);
4411 minfo ("%s", buf);
4412 len = strlen (buf);
4414 while (len < 16)
4416 print_space ();
4417 ++len;
4420 minfo ("%B\n", section->owner);
4423 return TRUE;
4426 /* Run through the input files and ensure that every input section has
4427 somewhere to go. If one is found without a destination then create
4428 an input request and place it into the statement tree. */
4430 static void
4431 lang_place_orphans (void)
4433 LANG_FOR_EACH_INPUT_STATEMENT (file)
4435 asection *s;
4437 for (s = file->the_bfd->sections; s != NULL; s = s->next)
4439 if (s->output_section == NULL)
4441 /* This section of the file is not attached, root
4442 around for a sensible place for it to go. */
4444 if (file->just_syms_flag)
4445 abort ();
4447 if ((s->flags & SEC_EXCLUDE) != 0)
4448 s->output_section = bfd_abs_section_ptr;
4449 else if (strcmp (s->name, "COMMON") == 0)
4451 /* This is a lonely common section which must have
4452 come from an archive. We attach to the section
4453 with the wildcard. */
4454 if (! link_info.relocatable
4455 || command_line.force_common_definition)
4457 if (default_common_section == NULL)
4459 #if 0
4460 /* This message happens when using the
4461 svr3.ifile linker script, so I have
4462 disabled it. */
4463 info_msg (_("%P: no [COMMON] command,"
4464 " defaulting to .bss\n"));
4465 #endif
4466 default_common_section =
4467 lang_output_section_statement_lookup (".bss");
4470 lang_add_section (&default_common_section->children, s,
4471 default_common_section, file);
4474 else if (ldemul_place_orphan (file, s))
4476 else
4478 lang_output_section_statement_type *os;
4480 os = lang_output_section_statement_lookup (s->name);
4481 lang_add_section (&os->children, s, os, file);
4488 void
4489 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
4491 flagword *ptr_flags;
4493 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
4494 while (*flags)
4496 switch (*flags)
4498 case 'A': case 'a':
4499 *ptr_flags |= SEC_ALLOC;
4500 break;
4502 case 'R': case 'r':
4503 *ptr_flags |= SEC_READONLY;
4504 break;
4506 case 'W': case 'w':
4507 *ptr_flags |= SEC_DATA;
4508 break;
4510 case 'X': case 'x':
4511 *ptr_flags |= SEC_CODE;
4512 break;
4514 case 'L': case 'l':
4515 case 'I': case 'i':
4516 *ptr_flags |= SEC_LOAD;
4517 break;
4519 default:
4520 einfo (_("%P%F: invalid syntax in flags\n"));
4521 break;
4523 flags++;
4527 /* Call a function on each input file. This function will be called
4528 on an archive, but not on the elements. */
4530 void
4531 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
4533 lang_input_statement_type *f;
4535 for (f = (lang_input_statement_type *) input_file_chain.head;
4536 f != NULL;
4537 f = (lang_input_statement_type *) f->next_real_file)
4538 func (f);
4541 /* Call a function on each file. The function will be called on all
4542 the elements of an archive which are included in the link, but will
4543 not be called on the archive file itself. */
4545 void
4546 lang_for_each_file (void (*func) (lang_input_statement_type *))
4548 LANG_FOR_EACH_INPUT_STATEMENT (f)
4550 func (f);
4554 void
4555 ldlang_add_file (lang_input_statement_type *entry)
4557 bfd **pp;
4559 lang_statement_append (&file_chain,
4560 (lang_statement_union_type *) entry,
4561 &entry->next);
4563 /* The BFD linker needs to have a list of all input BFDs involved in
4564 a link. */
4565 ASSERT (entry->the_bfd->link_next == NULL);
4566 ASSERT (entry->the_bfd != output_bfd);
4567 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
4569 *pp = entry->the_bfd;
4570 entry->the_bfd->usrdata = entry;
4571 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4573 /* Look through the sections and check for any which should not be
4574 included in the link. We need to do this now, so that we can
4575 notice when the backend linker tries to report multiple
4576 definition errors for symbols which are in sections we aren't
4577 going to link. FIXME: It might be better to entirely ignore
4578 symbols which are defined in sections which are going to be
4579 discarded. This would require modifying the backend linker for
4580 each backend which might set the SEC_LINK_ONCE flag. If we do
4581 this, we should probably handle SEC_EXCLUDE in the same way. */
4583 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4586 void
4587 lang_add_output (const char *name, int from_script)
4589 /* Make -o on command line override OUTPUT in script. */
4590 if (!had_output_filename || !from_script)
4592 output_filename = name;
4593 had_output_filename = TRUE;
4597 static lang_output_section_statement_type *current_section;
4599 static int
4600 topower (int x)
4602 unsigned int i = 1;
4603 int l;
4605 if (x < 0)
4606 return -1;
4608 for (l = 0; l < 32; l++)
4610 if (i >= (unsigned int) x)
4611 return l;
4612 i <<= 1;
4615 return 0;
4618 lang_output_section_statement_type *
4619 lang_enter_output_section_statement (const char *output_section_statement_name,
4620 etree_type *address_exp,
4621 enum section_type sectype,
4622 etree_type *align,
4623 etree_type *subalign,
4624 etree_type *ebase,
4625 int constraint)
4627 lang_output_section_statement_type *os;
4629 current_section =
4630 os =
4631 lang_output_section_statement_lookup_1 (output_section_statement_name,
4632 constraint);
4634 /* Add this statement to tree. */
4635 #if 0
4636 add_statement (lang_output_section_statement_enum,
4637 output_section_statement);
4638 #endif
4639 /* Make next things chain into subchain of this. */
4641 if (os->addr_tree == NULL)
4643 os->addr_tree = address_exp;
4645 os->sectype = sectype;
4646 if (sectype != noload_section)
4647 os->flags = SEC_NO_FLAGS;
4648 else
4649 os->flags = SEC_NEVER_LOAD;
4650 os->block_value = 1;
4651 stat_ptr = &os->children;
4653 os->subsection_alignment =
4654 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4655 os->section_alignment =
4656 topower (exp_get_value_int (align, -1, "section alignment", 0));
4658 os->load_base = ebase;
4659 return os;
4662 void
4663 lang_final (void)
4665 lang_output_statement_type *new =
4666 new_stat (lang_output_statement, stat_ptr);
4668 new->name = output_filename;
4671 /* Reset the current counters in the regions. */
4673 void
4674 lang_reset_memory_regions (void)
4676 lang_memory_region_type *p = lang_memory_region_list;
4677 asection *o;
4679 for (p = lang_memory_region_list; p != NULL; p = p->next)
4681 p->old_length = (bfd_size_type) (p->current - p->origin);
4682 p->current = p->origin;
4685 for (o = output_bfd->sections; o != NULL; o = o->next)
4687 /* Save the last size for possible use by bfd_relax_section. */
4688 o->rawsize = o->size;
4689 o->size = 0;
4693 /* Worker for lang_gc_sections_1. */
4695 static void
4696 gc_section_callback (lang_wild_statement_type *ptr,
4697 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4698 asection *section,
4699 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4700 void *data ATTRIBUTE_UNUSED)
4702 /* If the wild pattern was marked KEEP, the member sections
4703 should be as well. */
4704 if (ptr->keep_sections)
4705 section->flags |= SEC_KEEP;
4708 /* Iterate over sections marking them against GC. */
4710 static void
4711 lang_gc_sections_1 (lang_statement_union_type *s)
4713 for (; s != NULL; s = s->header.next)
4715 switch (s->header.type)
4717 case lang_wild_statement_enum:
4718 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4719 break;
4720 case lang_constructors_statement_enum:
4721 lang_gc_sections_1 (constructor_list.head);
4722 break;
4723 case lang_output_section_statement_enum:
4724 lang_gc_sections_1 (s->output_section_statement.children.head);
4725 break;
4726 case lang_group_statement_enum:
4727 lang_gc_sections_1 (s->group_statement.children.head);
4728 break;
4729 default:
4730 break;
4735 static void
4736 lang_gc_sections (void)
4738 struct bfd_link_hash_entry *h;
4739 ldlang_undef_chain_list_type *ulist;
4741 /* Keep all sections so marked in the link script. */
4743 lang_gc_sections_1 (statement_list.head);
4745 /* Keep all sections containing symbols undefined on the command-line,
4746 and the section containing the entry symbol. */
4748 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4750 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4751 FALSE, FALSE, FALSE);
4753 if (h != NULL
4754 && (h->type == bfd_link_hash_defined
4755 || h->type == bfd_link_hash_defweak)
4756 && ! bfd_is_abs_section (h->u.def.section))
4758 h->u.def.section->flags |= SEC_KEEP;
4762 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4763 the special case of debug info. (See bfd/stabs.c)
4764 Twiddle the flag here, to simplify later linker code. */
4765 if (link_info.relocatable)
4767 LANG_FOR_EACH_INPUT_STATEMENT (f)
4769 asection *sec;
4770 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4771 if ((sec->flags & SEC_DEBUGGING) == 0)
4772 sec->flags &= ~SEC_EXCLUDE;
4776 if (command_line.gc_sections)
4777 bfd_gc_sections (output_bfd, &link_info);
4780 void
4781 lang_process (void)
4783 lang_reasonable_defaults ();
4784 current_target = default_target;
4786 /* Open the output file. */
4787 lang_for_each_statement (ldlang_open_output);
4788 init_opb ();
4790 ldemul_create_output_section_statements ();
4792 /* Add to the hash table all undefineds on the command line. */
4793 lang_place_undefineds ();
4795 if (!bfd_section_already_linked_table_init ())
4796 einfo (_("%P%F: Failed to create hash table\n"));
4798 /* Create a bfd for each input file. */
4799 current_target = default_target;
4800 open_input_bfds (statement_list.head, FALSE);
4802 link_info.gc_sym_list = &entry_symbol;
4803 if (entry_symbol.name == NULL)
4804 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4806 ldemul_after_open ();
4808 bfd_section_already_linked_table_free ();
4810 /* Make sure that we're not mixing architectures. We call this
4811 after all the input files have been opened, but before we do any
4812 other processing, so that any operations merge_private_bfd_data
4813 does on the output file will be known during the rest of the
4814 link. */
4815 lang_check ();
4817 /* Handle .exports instead of a version script if we're told to do so. */
4818 if (command_line.version_exports_section)
4819 lang_do_version_exports_section ();
4821 /* Build all sets based on the information gathered from the input
4822 files. */
4823 ldctor_build_sets ();
4825 /* Remove unreferenced sections if asked to. */
4826 lang_gc_sections ();
4828 /* Size up the common data. */
4829 lang_common ();
4831 /* Update wild statements. */
4832 update_wild_statements (statement_list.head);
4834 /* Run through the contours of the script and attach input sections
4835 to the correct output sections. */
4836 map_input_to_output_sections (statement_list.head, NULL, NULL);
4838 /* Find any sections not attached explicitly and handle them. */
4839 lang_place_orphans ();
4841 if (! link_info.relocatable)
4843 asection *found;
4845 /* Merge SEC_MERGE sections. This has to be done after GC of
4846 sections, so that GCed sections are not merged, but before
4847 assigning dynamic symbols, since removing whole input sections
4848 is hard then. */
4849 bfd_merge_sections (output_bfd, &link_info);
4851 /* Look for a text section and set the readonly attribute in it. */
4852 found = bfd_get_section_by_name (output_bfd, ".text");
4854 if (found != NULL)
4856 if (config.text_read_only)
4857 found->flags |= SEC_READONLY;
4858 else
4859 found->flags &= ~SEC_READONLY;
4863 /* Do anything special before sizing sections. This is where ELF
4864 and other back-ends size dynamic sections. */
4865 ldemul_before_allocation ();
4867 if (!link_info.relocatable)
4868 strip_excluded_output_sections ();
4870 /* We must record the program headers before we try to fix the
4871 section positions, since they will affect SIZEOF_HEADERS. */
4872 lang_record_phdrs ();
4874 /* Size up the sections. */
4875 lang_size_sections (statement_list.head, abs_output_section,
4876 &statement_list.head, 0, 0, NULL,
4877 command_line.relax ? FALSE : TRUE);
4879 /* Now run around and relax if we can. */
4880 if (command_line.relax)
4882 /* Keep relaxing until bfd_relax_section gives up. */
4883 bfd_boolean relax_again;
4887 relax_again = FALSE;
4889 /* Note: pe-dll.c does something like this also. If you find
4890 you need to change this code, you probably need to change
4891 pe-dll.c also. DJ */
4893 /* Do all the assignments with our current guesses as to
4894 section sizes. */
4895 lang_do_assignments (statement_list.head, abs_output_section,
4896 NULL, 0);
4898 /* We must do this after lang_do_assignments, because it uses
4899 size. */
4900 lang_reset_memory_regions ();
4902 /* Perform another relax pass - this time we know where the
4903 globals are, so can make a better guess. */
4904 lang_size_sections (statement_list.head, abs_output_section,
4905 &statement_list.head, 0, 0, &relax_again, FALSE);
4907 /* If the normal relax is done and the relax finalize pass
4908 is not performed yet, we perform another relax pass. */
4909 if (!relax_again && link_info.need_relax_finalize)
4911 link_info.need_relax_finalize = FALSE;
4912 relax_again = TRUE;
4915 while (relax_again);
4917 /* Final extra sizing to report errors. */
4918 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4919 lang_reset_memory_regions ();
4920 lang_size_sections (statement_list.head, abs_output_section,
4921 &statement_list.head, 0, 0, NULL, TRUE);
4924 /* See if anything special should be done now we know how big
4925 everything is. */
4926 ldemul_after_allocation ();
4928 /* Fix any .startof. or .sizeof. symbols. */
4929 lang_set_startof ();
4931 /* Do all the assignments, now that we know the final resting places
4932 of all the symbols. */
4934 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4936 /* Make sure that the section addresses make sense. */
4937 if (! link_info.relocatable
4938 && command_line.check_section_addresses)
4939 lang_check_section_addresses ();
4941 /* Final stuffs. */
4943 ldemul_finish ();
4944 lang_finish ();
4947 /* EXPORTED TO YACC */
4949 void
4950 lang_add_wild (struct wildcard_spec *filespec,
4951 struct wildcard_list *section_list,
4952 bfd_boolean keep_sections)
4954 struct wildcard_list *curr, *next;
4955 lang_wild_statement_type *new;
4957 /* Reverse the list as the parser puts it back to front. */
4958 for (curr = section_list, section_list = NULL;
4959 curr != NULL;
4960 section_list = curr, curr = next)
4962 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4963 placed_commons = TRUE;
4965 next = curr->next;
4966 curr->next = section_list;
4969 if (filespec != NULL && filespec->name != NULL)
4971 if (strcmp (filespec->name, "*") == 0)
4972 filespec->name = NULL;
4973 else if (! wildcardp (filespec->name))
4974 lang_has_input_file = TRUE;
4977 new = new_stat (lang_wild_statement, stat_ptr);
4978 new->filename = NULL;
4979 new->filenames_sorted = FALSE;
4980 if (filespec != NULL)
4982 new->filename = filespec->name;
4983 new->filenames_sorted = filespec->sorted == by_name;
4985 new->section_list = section_list;
4986 new->keep_sections = keep_sections;
4987 lang_list_init (&new->children);
4990 void
4991 lang_section_start (const char *name, etree_type *address,
4992 const segment_type *segment)
4994 lang_address_statement_type *ad;
4996 ad = new_stat (lang_address_statement, stat_ptr);
4997 ad->section_name = name;
4998 ad->address = address;
4999 ad->segment = segment;
5002 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
5003 because of a -e argument on the command line, or zero if this is
5004 called by ENTRY in a linker script. Command line arguments take
5005 precedence. */
5007 void
5008 lang_add_entry (const char *name, bfd_boolean cmdline)
5010 if (entry_symbol.name == NULL
5011 || cmdline
5012 || ! entry_from_cmdline)
5014 entry_symbol.name = name;
5015 entry_from_cmdline = cmdline;
5019 void
5020 lang_add_target (const char *name)
5022 lang_target_statement_type *new = new_stat (lang_target_statement,
5023 stat_ptr);
5025 new->target = name;
5029 void
5030 lang_add_map (const char *name)
5032 while (*name)
5034 switch (*name)
5036 case 'F':
5037 map_option_f = TRUE;
5038 break;
5040 name++;
5044 void
5045 lang_add_fill (fill_type *fill)
5047 lang_fill_statement_type *new = new_stat (lang_fill_statement,
5048 stat_ptr);
5050 new->fill = fill;
5053 void
5054 lang_add_data (int type, union etree_union *exp)
5057 lang_data_statement_type *new = new_stat (lang_data_statement,
5058 stat_ptr);
5060 new->exp = exp;
5061 new->type = type;
5065 /* Create a new reloc statement. RELOC is the BFD relocation type to
5066 generate. HOWTO is the corresponding howto structure (we could
5067 look this up, but the caller has already done so). SECTION is the
5068 section to generate a reloc against, or NAME is the name of the
5069 symbol to generate a reloc against. Exactly one of SECTION and
5070 NAME must be NULL. ADDEND is an expression for the addend. */
5072 void
5073 lang_add_reloc (bfd_reloc_code_real_type reloc,
5074 reloc_howto_type *howto,
5075 asection *section,
5076 const char *name,
5077 union etree_union *addend)
5079 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
5081 p->reloc = reloc;
5082 p->howto = howto;
5083 p->section = section;
5084 p->name = name;
5085 p->addend_exp = addend;
5087 p->addend_value = 0;
5088 p->output_section = NULL;
5089 p->output_vma = 0;
5092 lang_assignment_statement_type *
5093 lang_add_assignment (etree_type *exp)
5095 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
5096 stat_ptr);
5098 new->exp = exp;
5099 return new;
5102 void
5103 lang_add_attribute (enum statement_enum attribute)
5105 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
5108 void
5109 lang_startup (const char *name)
5111 if (startup_file != NULL)
5113 einfo (_("%P%Fmultiple STARTUP files\n"));
5115 first_file->filename = name;
5116 first_file->local_sym_name = name;
5117 first_file->real = TRUE;
5119 startup_file = name;
5122 void
5123 lang_float (bfd_boolean maybe)
5125 lang_float_flag = maybe;
5129 /* Work out the load- and run-time regions from a script statement, and
5130 store them in *LMA_REGION and *REGION respectively.
5132 MEMSPEC is the name of the run-time region, or the value of
5133 DEFAULT_MEMORY_REGION if the statement didn't specify one.
5134 LMA_MEMSPEC is the name of the load-time region, or null if the
5135 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
5136 had an explicit load address.
5138 It is an error to specify both a load region and a load address. */
5140 static void
5141 lang_get_regions (lang_memory_region_type **region,
5142 lang_memory_region_type **lma_region,
5143 const char *memspec,
5144 const char *lma_memspec,
5145 bfd_boolean have_lma,
5146 bfd_boolean have_vma)
5148 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
5150 /* If no runtime region or VMA has been specified, but the load region
5151 has been specified, then use the load region for the runtime region
5152 as well. */
5153 if (lma_memspec != NULL
5154 && ! have_vma
5155 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
5156 *region = *lma_region;
5157 else
5158 *region = lang_memory_region_lookup (memspec, FALSE);
5160 if (have_lma && lma_memspec != 0)
5161 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
5164 void
5165 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
5166 lang_output_section_phdr_list *phdrs,
5167 const char *lma_memspec)
5169 lang_get_regions (&current_section->region,
5170 &current_section->lma_region,
5171 memspec, lma_memspec,
5172 current_section->load_base != NULL,
5173 current_section->addr_tree != NULL);
5174 current_section->fill = fill;
5175 current_section->phdrs = phdrs;
5176 stat_ptr = &statement_list;
5179 /* Create an absolute symbol with the given name with the value of the
5180 address of first byte of the section named.
5182 If the symbol already exists, then do nothing. */
5184 void
5185 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
5187 struct bfd_link_hash_entry *h;
5189 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5190 if (h == NULL)
5191 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5193 if (h->type == bfd_link_hash_new
5194 || h->type == bfd_link_hash_undefined)
5196 asection *sec;
5198 h->type = bfd_link_hash_defined;
5200 sec = bfd_get_section_by_name (output_bfd, secname);
5201 if (sec == NULL)
5202 h->u.def.value = 0;
5203 else
5204 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
5206 h->u.def.section = bfd_abs_section_ptr;
5210 /* Create an absolute symbol with the given name with the value of the
5211 address of the first byte after the end of the section named.
5213 If the symbol already exists, then do nothing. */
5215 void
5216 lang_abs_symbol_at_end_of (const char *secname, const char *name)
5218 struct bfd_link_hash_entry *h;
5220 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
5221 if (h == NULL)
5222 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
5224 if (h->type == bfd_link_hash_new
5225 || h->type == bfd_link_hash_undefined)
5227 asection *sec;
5229 h->type = bfd_link_hash_defined;
5231 sec = bfd_get_section_by_name (output_bfd, secname);
5232 if (sec == NULL)
5233 h->u.def.value = 0;
5234 else
5235 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
5236 + TO_ADDR (sec->size));
5238 h->u.def.section = bfd_abs_section_ptr;
5242 void
5243 lang_statement_append (lang_statement_list_type *list,
5244 lang_statement_union_type *element,
5245 lang_statement_union_type **field)
5247 *(list->tail) = element;
5248 list->tail = field;
5251 /* Set the output format type. -oformat overrides scripts. */
5253 void
5254 lang_add_output_format (const char *format,
5255 const char *big,
5256 const char *little,
5257 int from_script)
5259 if (output_target == NULL || !from_script)
5261 if (command_line.endian == ENDIAN_BIG
5262 && big != NULL)
5263 format = big;
5264 else if (command_line.endian == ENDIAN_LITTLE
5265 && little != NULL)
5266 format = little;
5268 output_target = format;
5272 /* Enter a group. This creates a new lang_group_statement, and sets
5273 stat_ptr to build new statements within the group. */
5275 void
5276 lang_enter_group (void)
5278 lang_group_statement_type *g;
5280 g = new_stat (lang_group_statement, stat_ptr);
5281 lang_list_init (&g->children);
5282 stat_ptr = &g->children;
5285 /* Leave a group. This just resets stat_ptr to start writing to the
5286 regular list of statements again. Note that this will not work if
5287 groups can occur inside anything else which can adjust stat_ptr,
5288 but currently they can't. */
5290 void
5291 lang_leave_group (void)
5293 stat_ptr = &statement_list;
5296 /* Add a new program header. This is called for each entry in a PHDRS
5297 command in a linker script. */
5299 void
5300 lang_new_phdr (const char *name,
5301 etree_type *type,
5302 bfd_boolean filehdr,
5303 bfd_boolean phdrs,
5304 etree_type *at,
5305 etree_type *flags)
5307 struct lang_phdr *n, **pp;
5309 n = stat_alloc (sizeof (struct lang_phdr));
5310 n->next = NULL;
5311 n->name = name;
5312 n->type = exp_get_value_int (type, 0, "program header type",
5313 lang_final_phase_enum);
5314 n->filehdr = filehdr;
5315 n->phdrs = phdrs;
5316 n->at = at;
5317 n->flags = flags;
5319 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
5321 *pp = n;
5324 /* Record the program header information in the output BFD. FIXME: We
5325 should not be calling an ELF specific function here. */
5327 static void
5328 lang_record_phdrs (void)
5330 unsigned int alc;
5331 asection **secs;
5332 lang_output_section_phdr_list *last;
5333 struct lang_phdr *l;
5334 lang_output_section_statement_type *os;
5336 alc = 10;
5337 secs = xmalloc (alc * sizeof (asection *));
5338 last = NULL;
5339 for (l = lang_phdr_list; l != NULL; l = l->next)
5341 unsigned int c;
5342 flagword flags;
5343 bfd_vma at;
5345 c = 0;
5346 for (os = &lang_output_section_statement.head->output_section_statement;
5347 os != NULL;
5348 os = os->next)
5350 lang_output_section_phdr_list *pl;
5352 if (os->constraint == -1)
5353 continue;
5355 pl = os->phdrs;
5356 if (pl != NULL)
5357 last = pl;
5358 else
5360 if (os->sectype == noload_section
5361 || os->bfd_section == NULL
5362 || (os->bfd_section->flags & SEC_ALLOC) == 0)
5363 continue;
5364 pl = last;
5367 if (os->bfd_section == NULL)
5368 continue;
5370 for (; pl != NULL; pl = pl->next)
5372 if (strcmp (pl->name, l->name) == 0)
5374 if (c >= alc)
5376 alc *= 2;
5377 secs = xrealloc (secs, alc * sizeof (asection *));
5379 secs[c] = os->bfd_section;
5380 ++c;
5381 pl->used = TRUE;
5386 if (l->flags == NULL)
5387 flags = 0;
5388 else
5389 flags = exp_get_vma (l->flags, 0, "phdr flags",
5390 lang_final_phase_enum);
5392 if (l->at == NULL)
5393 at = 0;
5394 else
5395 at = exp_get_vma (l->at, 0, "phdr load address",
5396 lang_final_phase_enum);
5398 if (! bfd_record_phdr (output_bfd, l->type,
5399 l->flags != NULL, flags, l->at != NULL,
5400 at, l->filehdr, l->phdrs, c, secs))
5401 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
5404 free (secs);
5406 /* Make sure all the phdr assignments succeeded. */
5407 for (os = &lang_output_section_statement.head->output_section_statement;
5408 os != NULL;
5409 os = os->next)
5411 lang_output_section_phdr_list *pl;
5413 if (os->constraint == -1
5414 || os->bfd_section == NULL)
5415 continue;
5417 for (pl = os->phdrs;
5418 pl != NULL;
5419 pl = pl->next)
5420 if (! pl->used && strcmp (pl->name, "NONE") != 0)
5421 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
5422 os->name, pl->name);
5426 /* Record a list of sections which may not be cross referenced. */
5428 void
5429 lang_add_nocrossref (lang_nocrossref_type *l)
5431 struct lang_nocrossrefs *n;
5433 n = xmalloc (sizeof *n);
5434 n->next = nocrossref_list;
5435 n->list = l;
5436 nocrossref_list = n;
5438 /* Set notice_all so that we get informed about all symbols. */
5439 link_info.notice_all = TRUE;
5442 /* Overlay handling. We handle overlays with some static variables. */
5444 /* The overlay virtual address. */
5445 static etree_type *overlay_vma;
5446 /* And subsection alignment. */
5447 static etree_type *overlay_subalign;
5449 /* An expression for the maximum section size seen so far. */
5450 static etree_type *overlay_max;
5452 /* A list of all the sections in this overlay. */
5454 struct overlay_list {
5455 struct overlay_list *next;
5456 lang_output_section_statement_type *os;
5459 static struct overlay_list *overlay_list;
5461 /* Start handling an overlay. */
5463 void
5464 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
5466 /* The grammar should prevent nested overlays from occurring. */
5467 ASSERT (overlay_vma == NULL
5468 && overlay_subalign == NULL
5469 && overlay_max == NULL);
5471 overlay_vma = vma_expr;
5472 overlay_subalign = subalign;
5475 /* Start a section in an overlay. We handle this by calling
5476 lang_enter_output_section_statement with the correct VMA.
5477 lang_leave_overlay sets up the LMA and memory regions. */
5479 void
5480 lang_enter_overlay_section (const char *name)
5482 struct overlay_list *n;
5483 etree_type *size;
5485 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5486 0, overlay_subalign, 0, 0);
5488 /* If this is the first section, then base the VMA of future
5489 sections on this one. This will work correctly even if `.' is
5490 used in the addresses. */
5491 if (overlay_list == NULL)
5492 overlay_vma = exp_nameop (ADDR, name);
5494 /* Remember the section. */
5495 n = xmalloc (sizeof *n);
5496 n->os = current_section;
5497 n->next = overlay_list;
5498 overlay_list = n;
5500 size = exp_nameop (SIZEOF, name);
5502 /* Arrange to work out the maximum section end address. */
5503 if (overlay_max == NULL)
5504 overlay_max = size;
5505 else
5506 overlay_max = exp_binop (MAX_K, overlay_max, size);
5509 /* Finish a section in an overlay. There isn't any special to do
5510 here. */
5512 void
5513 lang_leave_overlay_section (fill_type *fill,
5514 lang_output_section_phdr_list *phdrs)
5516 const char *name;
5517 char *clean, *s2;
5518 const char *s1;
5519 char *buf;
5521 name = current_section->name;
5523 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
5524 region and that no load-time region has been specified. It doesn't
5525 really matter what we say here, since lang_leave_overlay will
5526 override it. */
5527 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
5529 /* Define the magic symbols. */
5531 clean = xmalloc (strlen (name) + 1);
5532 s2 = clean;
5533 for (s1 = name; *s1 != '\0'; s1++)
5534 if (ISALNUM (*s1) || *s1 == '_')
5535 *s2++ = *s1;
5536 *s2 = '\0';
5538 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5539 sprintf (buf, "__load_start_%s", clean);
5540 lang_add_assignment (exp_assop ('=', buf,
5541 exp_nameop (LOADADDR, name)));
5543 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5544 sprintf (buf, "__load_stop_%s", clean);
5545 lang_add_assignment (exp_assop ('=', buf,
5546 exp_binop ('+',
5547 exp_nameop (LOADADDR, name),
5548 exp_nameop (SIZEOF, name))));
5550 free (clean);
5553 /* Finish an overlay. If there are any overlay wide settings, this
5554 looks through all the sections in the overlay and sets them. */
5556 void
5557 lang_leave_overlay (etree_type *lma_expr,
5558 int nocrossrefs,
5559 fill_type *fill,
5560 const char *memspec,
5561 lang_output_section_phdr_list *phdrs,
5562 const char *lma_memspec)
5564 lang_memory_region_type *region;
5565 lang_memory_region_type *lma_region;
5566 struct overlay_list *l;
5567 lang_nocrossref_type *nocrossref;
5569 lang_get_regions (&region, &lma_region,
5570 memspec, lma_memspec,
5571 lma_expr != NULL, FALSE);
5573 nocrossref = NULL;
5575 /* After setting the size of the last section, set '.' to end of the
5576 overlay region. */
5577 if (overlay_list != NULL)
5578 overlay_list->os->update_dot_tree
5579 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5581 l = overlay_list;
5582 while (l != NULL)
5584 struct overlay_list *next;
5586 if (fill != NULL && l->os->fill == NULL)
5587 l->os->fill = fill;
5589 l->os->region = region;
5590 l->os->lma_region = lma_region;
5592 /* The first section has the load address specified in the
5593 OVERLAY statement. The rest are worked out from that.
5594 The base address is not needed (and should be null) if
5595 an LMA region was specified. */
5596 if (l->next == 0)
5597 l->os->load_base = lma_expr;
5598 else if (lma_region == 0)
5599 l->os->load_base = exp_binop ('+',
5600 exp_nameop (LOADADDR, l->next->os->name),
5601 exp_nameop (SIZEOF, l->next->os->name));
5603 if (phdrs != NULL && l->os->phdrs == NULL)
5604 l->os->phdrs = phdrs;
5606 if (nocrossrefs)
5608 lang_nocrossref_type *nc;
5610 nc = xmalloc (sizeof *nc);
5611 nc->name = l->os->name;
5612 nc->next = nocrossref;
5613 nocrossref = nc;
5616 next = l->next;
5617 free (l);
5618 l = next;
5621 if (nocrossref != NULL)
5622 lang_add_nocrossref (nocrossref);
5624 overlay_vma = NULL;
5625 overlay_list = NULL;
5626 overlay_max = NULL;
5629 /* Version handling. This is only useful for ELF. */
5631 /* This global variable holds the version tree that we build. */
5633 struct bfd_elf_version_tree *lang_elf_version_info;
5635 /* If PREV is NULL, return first version pattern matching particular symbol.
5636 If PREV is non-NULL, return first version pattern matching particular
5637 symbol after PREV (previously returned by lang_vers_match). */
5639 static struct bfd_elf_version_expr *
5640 lang_vers_match (struct bfd_elf_version_expr_head *head,
5641 struct bfd_elf_version_expr *prev,
5642 const char *sym)
5644 const char *cxx_sym = sym;
5645 const char *java_sym = sym;
5646 struct bfd_elf_version_expr *expr = NULL;
5648 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5650 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5651 if (!cxx_sym)
5652 cxx_sym = sym;
5654 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5656 java_sym = cplus_demangle (sym, DMGL_JAVA);
5657 if (!java_sym)
5658 java_sym = sym;
5661 if (head->htab && (prev == NULL || prev->symbol))
5663 struct bfd_elf_version_expr e;
5665 switch (prev ? prev->mask : 0)
5667 case 0:
5668 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5670 e.symbol = sym;
5671 expr = htab_find (head->htab, &e);
5672 while (expr && strcmp (expr->symbol, sym) == 0)
5673 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5674 goto out_ret;
5675 else
5676 expr = expr->next;
5678 /* Fallthrough */
5679 case BFD_ELF_VERSION_C_TYPE:
5680 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5682 e.symbol = cxx_sym;
5683 expr = htab_find (head->htab, &e);
5684 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5685 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5686 goto out_ret;
5687 else
5688 expr = expr->next;
5690 /* Fallthrough */
5691 case BFD_ELF_VERSION_CXX_TYPE:
5692 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5694 e.symbol = java_sym;
5695 expr = htab_find (head->htab, &e);
5696 while (expr && strcmp (expr->symbol, java_sym) == 0)
5697 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5698 goto out_ret;
5699 else
5700 expr = expr->next;
5702 /* Fallthrough */
5703 default:
5704 break;
5708 /* Finally, try the wildcards. */
5709 if (prev == NULL || prev->symbol)
5710 expr = head->remaining;
5711 else
5712 expr = prev->next;
5713 while (expr)
5715 const char *s;
5717 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5718 break;
5720 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5721 s = java_sym;
5722 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5723 s = cxx_sym;
5724 else
5725 s = sym;
5726 if (fnmatch (expr->pattern, s, 0) == 0)
5727 break;
5728 expr = expr->next;
5731 out_ret:
5732 if (cxx_sym != sym)
5733 free ((char *) cxx_sym);
5734 if (java_sym != sym)
5735 free ((char *) java_sym);
5736 return expr;
5739 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5740 return a string pointing to the symbol name. */
5742 static const char *
5743 realsymbol (const char *pattern)
5745 const char *p;
5746 bfd_boolean changed = FALSE, backslash = FALSE;
5747 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5749 for (p = pattern, s = symbol; *p != '\0'; ++p)
5751 /* It is a glob pattern only if there is no preceding
5752 backslash. */
5753 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5755 free (symbol);
5756 return NULL;
5759 if (backslash)
5761 /* Remove the preceding backslash. */
5762 *(s - 1) = *p;
5763 changed = TRUE;
5765 else
5766 *s++ = *p;
5768 backslash = *p == '\\';
5771 if (changed)
5773 *s = '\0';
5774 return symbol;
5776 else
5778 free (symbol);
5779 return pattern;
5783 /* This is called for each variable name or match expression. */
5785 struct bfd_elf_version_expr *
5786 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5787 const char *new,
5788 const char *lang)
5790 struct bfd_elf_version_expr *ret;
5792 ret = xmalloc (sizeof *ret);
5793 ret->next = orig;
5794 ret->pattern = new;
5795 ret->symver = 0;
5796 ret->script = 0;
5797 ret->symbol = realsymbol (new);
5799 if (lang == NULL || strcasecmp (lang, "C") == 0)
5800 ret->mask = BFD_ELF_VERSION_C_TYPE;
5801 else if (strcasecmp (lang, "C++") == 0)
5802 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5803 else if (strcasecmp (lang, "Java") == 0)
5804 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5805 else
5807 einfo (_("%X%P: unknown language `%s' in version information\n"),
5808 lang);
5809 ret->mask = BFD_ELF_VERSION_C_TYPE;
5812 return ldemul_new_vers_pattern (ret);
5815 /* This is called for each set of variable names and match
5816 expressions. */
5818 struct bfd_elf_version_tree *
5819 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5820 struct bfd_elf_version_expr *locals)
5822 struct bfd_elf_version_tree *ret;
5824 ret = xcalloc (1, sizeof *ret);
5825 ret->globals.list = globals;
5826 ret->locals.list = locals;
5827 ret->match = lang_vers_match;
5828 ret->name_indx = (unsigned int) -1;
5829 return ret;
5832 /* This static variable keeps track of version indices. */
5834 static int version_index;
5836 static hashval_t
5837 version_expr_head_hash (const void *p)
5839 const struct bfd_elf_version_expr *e = p;
5841 return htab_hash_string (e->symbol);
5844 static int
5845 version_expr_head_eq (const void *p1, const void *p2)
5847 const struct bfd_elf_version_expr *e1 = p1;
5848 const struct bfd_elf_version_expr *e2 = p2;
5850 return strcmp (e1->symbol, e2->symbol) == 0;
5853 static void
5854 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5856 size_t count = 0;
5857 struct bfd_elf_version_expr *e, *next;
5858 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5860 for (e = head->list; e; e = e->next)
5862 if (e->symbol)
5863 count++;
5864 head->mask |= e->mask;
5867 if (count)
5869 head->htab = htab_create (count * 2, version_expr_head_hash,
5870 version_expr_head_eq, NULL);
5871 list_loc = &head->list;
5872 remaining_loc = &head->remaining;
5873 for (e = head->list; e; e = next)
5875 next = e->next;
5876 if (!e->symbol)
5878 *remaining_loc = e;
5879 remaining_loc = &e->next;
5881 else
5883 void **loc = htab_find_slot (head->htab, e, INSERT);
5885 if (*loc)
5887 struct bfd_elf_version_expr *e1, *last;
5889 e1 = *loc;
5890 last = NULL;
5893 if (e1->mask == e->mask)
5895 last = NULL;
5896 break;
5898 last = e1;
5899 e1 = e1->next;
5901 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5903 if (last == NULL)
5905 /* This is a duplicate. */
5906 /* FIXME: Memory leak. Sometimes pattern is not
5907 xmalloced alone, but in larger chunk of memory. */
5908 /* free (e->symbol); */
5909 free (e);
5911 else
5913 e->next = last->next;
5914 last->next = e;
5917 else
5919 *loc = e;
5920 *list_loc = e;
5921 list_loc = &e->next;
5925 *remaining_loc = NULL;
5926 *list_loc = head->remaining;
5928 else
5929 head->remaining = head->list;
5932 /* This is called when we know the name and dependencies of the
5933 version. */
5935 void
5936 lang_register_vers_node (const char *name,
5937 struct bfd_elf_version_tree *version,
5938 struct bfd_elf_version_deps *deps)
5940 struct bfd_elf_version_tree *t, **pp;
5941 struct bfd_elf_version_expr *e1;
5943 if (name == NULL)
5944 name = "";
5946 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5947 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5949 einfo (_("%X%P: anonymous version tag cannot be combined"
5950 " with other version tags\n"));
5951 free (version);
5952 return;
5955 /* Make sure this node has a unique name. */
5956 for (t = lang_elf_version_info; t != NULL; t = t->next)
5957 if (strcmp (t->name, name) == 0)
5958 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5960 lang_finalize_version_expr_head (&version->globals);
5961 lang_finalize_version_expr_head (&version->locals);
5963 /* Check the global and local match names, and make sure there
5964 aren't any duplicates. */
5966 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5968 for (t = lang_elf_version_info; t != NULL; t = t->next)
5970 struct bfd_elf_version_expr *e2;
5972 if (t->locals.htab && e1->symbol)
5974 e2 = htab_find (t->locals.htab, e1);
5975 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5977 if (e1->mask == e2->mask)
5978 einfo (_("%X%P: duplicate expression `%s'"
5979 " in version information\n"), e1->symbol);
5980 e2 = e2->next;
5983 else if (!e1->symbol)
5984 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5985 if (strcmp (e1->pattern, e2->pattern) == 0
5986 && e1->mask == e2->mask)
5987 einfo (_("%X%P: duplicate expression `%s'"
5988 " in version information\n"), e1->pattern);
5992 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5994 for (t = lang_elf_version_info; t != NULL; t = t->next)
5996 struct bfd_elf_version_expr *e2;
5998 if (t->globals.htab && e1->symbol)
6000 e2 = htab_find (t->globals.htab, e1);
6001 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
6003 if (e1->mask == e2->mask)
6004 einfo (_("%X%P: duplicate expression `%s'"
6005 " in version information\n"),
6006 e1->symbol);
6007 e2 = e2->next;
6010 else if (!e1->symbol)
6011 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
6012 if (strcmp (e1->pattern, e2->pattern) == 0
6013 && e1->mask == e2->mask)
6014 einfo (_("%X%P: duplicate expression `%s'"
6015 " in version information\n"), e1->pattern);
6019 version->deps = deps;
6020 version->name = name;
6021 if (name[0] != '\0')
6023 ++version_index;
6024 version->vernum = version_index;
6026 else
6027 version->vernum = 0;
6029 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
6031 *pp = version;
6034 /* This is called when we see a version dependency. */
6036 struct bfd_elf_version_deps *
6037 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
6039 struct bfd_elf_version_deps *ret;
6040 struct bfd_elf_version_tree *t;
6042 ret = xmalloc (sizeof *ret);
6043 ret->next = list;
6045 for (t = lang_elf_version_info; t != NULL; t = t->next)
6047 if (strcmp (t->name, name) == 0)
6049 ret->version_needed = t;
6050 return ret;
6054 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
6056 return ret;
6059 static void
6060 lang_do_version_exports_section (void)
6062 struct bfd_elf_version_expr *greg = NULL, *lreg;
6064 LANG_FOR_EACH_INPUT_STATEMENT (is)
6066 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
6067 char *contents, *p;
6068 bfd_size_type len;
6070 if (sec == NULL)
6071 continue;
6073 len = sec->size;
6074 contents = xmalloc (len);
6075 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
6076 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
6078 p = contents;
6079 while (p < contents + len)
6081 greg = lang_new_vers_pattern (greg, p, NULL);
6082 p = strchr (p, '\0') + 1;
6085 /* Do not free the contents, as we used them creating the regex. */
6087 /* Do not include this section in the link. */
6088 sec->flags |= SEC_EXCLUDE;
6091 lreg = lang_new_vers_pattern (NULL, "*", NULL);
6092 lang_register_vers_node (command_line.version_exports_section,
6093 lang_new_vers_node (greg, lreg), NULL);
6096 void
6097 lang_add_unique (const char *name)
6099 struct unique_sections *ent;
6101 for (ent = unique_section_list; ent; ent = ent->next)
6102 if (strcmp (ent->name, name) == 0)
6103 return;
6105 ent = xmalloc (sizeof *ent);
6106 ent->name = xstrdup (name);
6107 ent->next = unique_section_list;
6108 unique_section_list = ent;