bfd/
[binutils.git] / ld / ldlang.c
blobc5cfd5c45a26b40432a11db5f4ac9059424f93b6
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 bfd_boolean wildcardp (const char *);
71 static lang_input_statement_type *lookup_name (const char *);
72 static bfd_boolean load_symbols (lang_input_statement_type *,
73 lang_statement_list_type *);
74 static struct bfd_hash_entry *lang_definedness_newfunc
75 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
76 static void insert_undefined (const char *);
77 static void print_all_symbols (asection *);
78 static bfd_boolean sort_def_symbol (struct bfd_link_hash_entry *, void *);
79 static void print_statement (lang_statement_union_type *,
80 lang_output_section_statement_type *);
81 static void print_statement_list (lang_statement_union_type *,
82 lang_output_section_statement_type *);
83 static void print_statements (void);
84 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
85 static void lang_record_phdrs (void);
86 static void lang_do_version_exports_section (void);
88 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
89 asection *, lang_input_statement_type *, void *);
91 /* Exported variables. */
92 lang_output_section_statement_type *abs_output_section;
93 lang_statement_list_type lang_output_section_statement;
94 lang_statement_list_type *stat_ptr = &statement_list;
95 lang_statement_list_type file_chain = { NULL, NULL };
96 struct bfd_sym_chain entry_symbol = { NULL, NULL };
97 const char *entry_section = ".text";
98 bfd_boolean entry_from_cmdline;
99 bfd_boolean lang_has_input_file = FALSE;
100 bfd_boolean had_output_filename = FALSE;
101 bfd_boolean lang_float_flag = FALSE;
102 bfd_boolean delete_output_file_on_failure = FALSE;
103 struct lang_nocrossrefs *nocrossref_list;
104 struct unique_sections *unique_section_list;
105 static bfd_boolean ldlang_sysrooted_script = FALSE;
106 int lang_statement_iteration = 0;
108 etree_type *base; /* Relocation base - or null */
110 #define new_stat(x, y) \
111 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
113 #define outside_section_address(q) \
114 ((q)->output_offset + (q)->output_section->vma)
116 #define outside_symbol_address(q) \
117 ((q)->value + outside_section_address (q->section))
119 #define SECTION_NAME_MAP_LENGTH (16)
121 void *
122 stat_alloc (size_t size)
124 return obstack_alloc (&stat_obstack, size);
127 bfd_boolean
128 unique_section_p (const asection *sec)
130 struct unique_sections *unam;
131 const char *secnam;
133 if (link_info.relocatable
134 && sec->owner != NULL
135 && bfd_is_group_section (sec->owner, sec))
136 return TRUE;
138 secnam = sec->name;
139 for (unam = unique_section_list; unam; unam = unam->next)
140 if (wildcardp (unam->name)
141 ? fnmatch (unam->name, secnam, 0) == 0
142 : strcmp (unam->name, secnam) == 0)
144 return TRUE;
147 return FALSE;
150 /* Generic traversal routines for finding matching sections. */
152 static void
153 walk_wild_section (lang_wild_statement_type *ptr,
154 lang_input_statement_type *file,
155 callback_t callback,
156 void *data)
158 asection *s;
160 if (file->just_syms_flag)
161 return;
163 for (s = file->the_bfd->sections; s != NULL; s = s->next)
165 struct wildcard_list *sec;
167 sec = ptr->section_list;
168 if (sec == NULL)
169 (*callback) (ptr, sec, s, file, data);
171 while (sec != NULL)
173 bfd_boolean skip = FALSE;
174 struct name_list *list_tmp;
176 /* Don't process sections from files which were
177 excluded. */
178 for (list_tmp = sec->spec.exclude_name_list;
179 list_tmp;
180 list_tmp = list_tmp->next)
182 if (wildcardp (list_tmp->name))
183 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
184 else
185 skip = strcmp (list_tmp->name, file->filename) == 0;
187 /* If this file is part of an archive, and the archive is
188 excluded, exclude this file. */
189 if (! skip && file->the_bfd != NULL
190 && file->the_bfd->my_archive != NULL
191 && file->the_bfd->my_archive->filename != NULL)
193 if (wildcardp (list_tmp->name))
194 skip = fnmatch (list_tmp->name,
195 file->the_bfd->my_archive->filename,
196 0) == 0;
197 else
198 skip = strcmp (list_tmp->name,
199 file->the_bfd->my_archive->filename) == 0;
202 if (skip)
203 break;
206 if (!skip && sec->spec.name != NULL)
208 const char *sname = bfd_get_section_name (file->the_bfd, s);
210 if (wildcardp (sec->spec.name))
211 skip = fnmatch (sec->spec.name, sname, 0) != 0;
212 else
213 skip = strcmp (sec->spec.name, sname) != 0;
216 if (!skip)
217 (*callback) (ptr, sec, s, file, data);
219 sec = sec->next;
224 /* Handle a wild statement for a single file F. */
226 static void
227 walk_wild_file (lang_wild_statement_type *s,
228 lang_input_statement_type *f,
229 callback_t callback,
230 void *data)
232 if (f->the_bfd == NULL
233 || ! bfd_check_format (f->the_bfd, bfd_archive))
234 walk_wild_section (s, f, callback, data);
235 else
237 bfd *member;
239 /* This is an archive file. We must map each member of the
240 archive separately. */
241 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
242 while (member != NULL)
244 /* When lookup_name is called, it will call the add_symbols
245 entry point for the archive. For each element of the
246 archive which is included, BFD will call ldlang_add_file,
247 which will set the usrdata field of the member to the
248 lang_input_statement. */
249 if (member->usrdata != NULL)
251 walk_wild_section (s, member->usrdata, callback, data);
254 member = bfd_openr_next_archived_file (f->the_bfd, member);
259 static void
260 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
262 const char *file_spec = s->filename;
264 if (file_spec == NULL)
266 /* Perform the iteration over all files in the list. */
267 LANG_FOR_EACH_INPUT_STATEMENT (f)
269 walk_wild_file (s, f, callback, data);
272 else if (wildcardp (file_spec))
274 LANG_FOR_EACH_INPUT_STATEMENT (f)
276 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
277 walk_wild_file (s, f, callback, data);
280 else
282 lang_input_statement_type *f;
284 /* Perform the iteration over a single file. */
285 f = lookup_name (file_spec);
286 if (f)
287 walk_wild_file (s, f, callback, data);
291 /* lang_for_each_statement walks the parse tree and calls the provided
292 function for each node. */
294 static void
295 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
296 lang_statement_union_type *s)
298 for (; s != NULL; s = s->header.next)
300 func (s);
302 switch (s->header.type)
304 case lang_constructors_statement_enum:
305 lang_for_each_statement_worker (func, constructor_list.head);
306 break;
307 case lang_output_section_statement_enum:
308 lang_for_each_statement_worker
309 (func, s->output_section_statement.children.head);
310 break;
311 case lang_wild_statement_enum:
312 lang_for_each_statement_worker (func,
313 s->wild_statement.children.head);
314 break;
315 case lang_group_statement_enum:
316 lang_for_each_statement_worker (func,
317 s->group_statement.children.head);
318 break;
319 case lang_data_statement_enum:
320 case lang_reloc_statement_enum:
321 case lang_object_symbols_statement_enum:
322 case lang_output_statement_enum:
323 case lang_target_statement_enum:
324 case lang_input_section_enum:
325 case lang_input_statement_enum:
326 case lang_assignment_statement_enum:
327 case lang_padding_statement_enum:
328 case lang_address_statement_enum:
329 case lang_fill_statement_enum:
330 break;
331 default:
332 FAIL ();
333 break;
338 void
339 lang_for_each_statement (void (*func) (lang_statement_union_type *))
341 lang_for_each_statement_worker (func, statement_list.head);
344 /*----------------------------------------------------------------------*/
346 void
347 lang_list_init (lang_statement_list_type *list)
349 list->head = NULL;
350 list->tail = &list->head;
353 /* Build a new statement node for the parse tree. */
355 static lang_statement_union_type *
356 new_statement (enum statement_enum type,
357 size_t size,
358 lang_statement_list_type *list)
360 lang_statement_union_type *new;
362 new = stat_alloc (size);
363 new->header.type = type;
364 new->header.next = NULL;
365 lang_statement_append (list, new, &new->header.next);
366 return new;
369 /* Build a new input file node for the language. There are several
370 ways in which we treat an input file, eg, we only look at symbols,
371 or prefix it with a -l etc.
373 We can be supplied with requests for input files more than once;
374 they may, for example be split over several lines like foo.o(.text)
375 foo.o(.data) etc, so when asked for a file we check that we haven't
376 got it already so we don't duplicate the bfd. */
378 static lang_input_statement_type *
379 new_afile (const char *name,
380 lang_input_file_enum_type file_type,
381 const char *target,
382 bfd_boolean add_to_list)
384 lang_input_statement_type *p;
386 if (add_to_list)
387 p = new_stat (lang_input_statement, stat_ptr);
388 else
390 p = stat_alloc (sizeof (lang_input_statement_type));
391 p->header.next = NULL;
394 lang_has_input_file = TRUE;
395 p->target = target;
396 p->sysrooted = FALSE;
397 switch (file_type)
399 case lang_input_file_is_symbols_only_enum:
400 p->filename = name;
401 p->is_archive = FALSE;
402 p->real = TRUE;
403 p->local_sym_name = name;
404 p->just_syms_flag = TRUE;
405 p->search_dirs_flag = FALSE;
406 break;
407 case lang_input_file_is_fake_enum:
408 p->filename = name;
409 p->is_archive = FALSE;
410 p->real = FALSE;
411 p->local_sym_name = name;
412 p->just_syms_flag = FALSE;
413 p->search_dirs_flag = FALSE;
414 break;
415 case lang_input_file_is_l_enum:
416 p->is_archive = TRUE;
417 p->filename = name;
418 p->real = TRUE;
419 p->local_sym_name = concat ("-l", name, NULL);
420 p->just_syms_flag = FALSE;
421 p->search_dirs_flag = TRUE;
422 break;
423 case lang_input_file_is_marker_enum:
424 p->filename = name;
425 p->is_archive = FALSE;
426 p->real = FALSE;
427 p->local_sym_name = name;
428 p->just_syms_flag = FALSE;
429 p->search_dirs_flag = TRUE;
430 break;
431 case lang_input_file_is_search_file_enum:
432 p->sysrooted = ldlang_sysrooted_script;
433 p->filename = name;
434 p->is_archive = FALSE;
435 p->real = TRUE;
436 p->local_sym_name = name;
437 p->just_syms_flag = FALSE;
438 p->search_dirs_flag = TRUE;
439 break;
440 case lang_input_file_is_file_enum:
441 p->filename = name;
442 p->is_archive = FALSE;
443 p->real = TRUE;
444 p->local_sym_name = name;
445 p->just_syms_flag = FALSE;
446 p->search_dirs_flag = FALSE;
447 break;
448 default:
449 FAIL ();
451 p->the_bfd = NULL;
452 p->asymbols = NULL;
453 p->next_real_file = NULL;
454 p->next = NULL;
455 p->symbol_count = 0;
456 p->dynamic = config.dynamic_link;
457 p->add_needed = add_needed;
458 p->as_needed = as_needed;
459 p->whole_archive = whole_archive;
460 p->loaded = FALSE;
461 lang_statement_append (&input_file_chain,
462 (lang_statement_union_type *) p,
463 &p->next_real_file);
464 return p;
467 lang_input_statement_type *
468 lang_add_input_file (const char *name,
469 lang_input_file_enum_type file_type,
470 const char *target)
472 lang_has_input_file = TRUE;
473 return new_afile (name, file_type, target, TRUE);
476 /* Build enough state so that the parser can build its tree. */
478 void
479 lang_init (void)
481 obstack_begin (&stat_obstack, 1000);
483 stat_ptr = &statement_list;
485 lang_list_init (stat_ptr);
487 lang_list_init (&input_file_chain);
488 lang_list_init (&lang_output_section_statement);
489 lang_list_init (&file_chain);
490 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
491 NULL);
492 abs_output_section =
493 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
495 abs_output_section->bfd_section = bfd_abs_section_ptr;
497 /* The value "3" is ad-hoc, somewhat related to the expected number of
498 DEFINED expressions in a linker script. For most default linker
499 scripts, there are none. Why a hash table then? Well, it's somewhat
500 simpler to re-use working machinery than using a linked list in terms
501 of code-complexity here in ld, besides the initialization which just
502 looks like other code here. */
503 if (bfd_hash_table_init_n (&lang_definedness_table,
504 lang_definedness_newfunc, 3) != TRUE)
505 einfo (_("%P%F: out of memory during initialization"));
507 /* Callers of exp_fold_tree need to increment this. */
508 lang_statement_iteration = 0;
511 /*----------------------------------------------------------------------
512 A region is an area of memory declared with the
513 MEMORY { name:org=exp, len=exp ... }
514 syntax.
516 We maintain a list of all the regions here.
518 If no regions are specified in the script, then the default is used
519 which is created when looked up to be the entire data space.
521 If create is true we are creating a region inside a MEMORY block.
522 In this case it is probably an error to create a region that has
523 already been created. If we are not inside a MEMORY block it is
524 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
525 and so we issue a warning. */
527 static lang_memory_region_type *lang_memory_region_list;
528 static lang_memory_region_type **lang_memory_region_list_tail
529 = &lang_memory_region_list;
531 lang_memory_region_type *
532 lang_memory_region_lookup (const char *const name, bfd_boolean create)
534 lang_memory_region_type *p;
535 lang_memory_region_type *new;
537 /* NAME is NULL for LMA memspecs if no region was specified. */
538 if (name == NULL)
539 return NULL;
541 for (p = lang_memory_region_list; p != NULL; p = p->next)
542 if (strcmp (p->name, name) == 0)
544 if (create)
545 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
546 name);
547 return p;
550 #if 0
551 /* This code used to always use the first region in the list as the
552 default region. I changed it to instead use a region
553 encompassing all of memory as the default region. This permits
554 NOLOAD sections to work reasonably without requiring a region.
555 People should specify what region they mean, if they really want
556 a region. */
557 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
559 if (lang_memory_region_list != NULL)
560 return lang_memory_region_list;
562 #endif
564 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
565 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
567 new = stat_alloc (sizeof (lang_memory_region_type));
569 new->name = xstrdup (name);
570 new->next = NULL;
572 *lang_memory_region_list_tail = new;
573 lang_memory_region_list_tail = &new->next;
574 new->origin = 0;
575 new->flags = 0;
576 new->not_flags = 0;
577 new->length = ~(bfd_size_type) 0;
578 new->current = 0;
579 new->had_full_message = FALSE;
581 return new;
584 static lang_memory_region_type *
585 lang_memory_default (asection *section)
587 lang_memory_region_type *p;
589 flagword sec_flags = section->flags;
591 /* Override SEC_DATA to mean a writable section. */
592 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
593 sec_flags |= SEC_DATA;
595 for (p = lang_memory_region_list; p != NULL; p = p->next)
597 if ((p->flags & sec_flags) != 0
598 && (p->not_flags & sec_flags) == 0)
600 return p;
603 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
606 static lang_output_section_statement_type *
607 lang_output_section_find_1 (const char *const name, int constraint)
609 lang_statement_union_type *u;
610 lang_output_section_statement_type *lookup;
612 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
614 lookup = &u->output_section_statement;
615 if (strcmp (name, lookup->name) == 0
616 && lookup->constraint != -1
617 && (constraint == 0 || constraint == lookup->constraint))
618 return lookup;
620 return NULL;
623 lang_output_section_statement_type *
624 lang_output_section_find (const char *const name)
626 return lang_output_section_find_1 (name, 0);
629 static lang_output_section_statement_type *
630 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
632 lang_output_section_statement_type *lookup;
634 lookup = lang_output_section_find_1 (name, constraint);
635 if (lookup == NULL)
637 lookup = new_stat (lang_output_section_statement, stat_ptr);
638 lookup->region = NULL;
639 lookup->lma_region = NULL;
640 lookup->fill = 0;
641 lookup->block_value = 1;
642 lookup->name = name;
644 lookup->next = NULL;
645 lookup->bfd_section = NULL;
646 lookup->processed = 0;
647 lookup->constraint = constraint;
648 lookup->sectype = normal_section;
649 lookup->addr_tree = NULL;
650 lang_list_init (&lookup->children);
652 lookup->memspec = NULL;
653 lookup->flags = 0;
654 lookup->subsection_alignment = -1;
655 lookup->section_alignment = -1;
656 lookup->load_base = NULL;
657 lookup->update_dot_tree = NULL;
658 lookup->phdrs = NULL;
660 lang_statement_append (&lang_output_section_statement,
661 (lang_statement_union_type *) lookup,
662 &lookup->next);
664 return lookup;
667 lang_output_section_statement_type *
668 lang_output_section_statement_lookup (const char *const name)
670 return lang_output_section_statement_lookup_1 (name, 0);
673 static void
674 lang_map_flags (flagword flag)
676 if (flag & SEC_ALLOC)
677 minfo ("a");
679 if (flag & SEC_CODE)
680 minfo ("x");
682 if (flag & SEC_READONLY)
683 minfo ("r");
685 if (flag & SEC_DATA)
686 minfo ("w");
688 if (flag & SEC_LOAD)
689 minfo ("l");
692 void
693 lang_map (void)
695 lang_memory_region_type *m;
696 bfd *p;
698 minfo (_("\nMemory Configuration\n\n"));
699 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
700 _("Name"), _("Origin"), _("Length"), _("Attributes"));
702 for (m = lang_memory_region_list; m != NULL; m = m->next)
704 char buf[100];
705 int len;
707 fprintf (config.map_file, "%-16s ", m->name);
709 sprintf_vma (buf, m->origin);
710 minfo ("0x%s ", buf);
711 len = strlen (buf);
712 while (len < 16)
714 print_space ();
715 ++len;
718 minfo ("0x%V", m->length);
719 if (m->flags || m->not_flags)
721 #ifndef BFD64
722 minfo (" ");
723 #endif
724 if (m->flags)
726 print_space ();
727 lang_map_flags (m->flags);
730 if (m->not_flags)
732 minfo (" !");
733 lang_map_flags (m->not_flags);
737 print_nl ();
740 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
742 if (! command_line.reduce_memory_overheads)
744 obstack_begin (&map_obstack, 1000);
745 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
746 bfd_map_over_sections (p, init_map_userdata, 0);
747 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
749 print_statements ();
752 static void
753 init_map_userdata (abfd, sec, data)
754 bfd *abfd ATTRIBUTE_UNUSED;
755 asection *sec;
756 void *data ATTRIBUTE_UNUSED;
758 fat_section_userdata_type *new_data
759 = ((fat_section_userdata_type *) (stat_alloc
760 (sizeof (fat_section_userdata_type))));
762 ASSERT (get_userdata (sec) == NULL);
763 get_userdata (sec) = new_data;
764 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
767 static bfd_boolean
768 sort_def_symbol (hash_entry, info)
769 struct bfd_link_hash_entry *hash_entry;
770 void *info ATTRIBUTE_UNUSED;
772 if (hash_entry->type == bfd_link_hash_defined
773 || hash_entry->type == bfd_link_hash_defweak)
775 struct fat_user_section_struct *ud;
776 struct map_symbol_def *def;
778 ud = get_userdata (hash_entry->u.def.section);
779 if (! ud)
781 /* ??? What do we have to do to initialize this beforehand? */
782 /* The first time we get here is bfd_abs_section... */
783 init_map_userdata (0, hash_entry->u.def.section, 0);
784 ud = get_userdata (hash_entry->u.def.section);
786 else if (!ud->map_symbol_def_tail)
787 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
788 def = obstack_alloc (&map_obstack, sizeof *def);
789 def->entry = hash_entry;
790 *ud->map_symbol_def_tail = def;
791 ud->map_symbol_def_tail = &def->next;
793 return TRUE;
796 /* Initialize an output section. */
798 static void
799 init_os (lang_output_section_statement_type *s)
801 lean_section_userdata_type *new;
803 if (s->bfd_section != NULL)
804 return;
806 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
807 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
809 new = stat_alloc (SECTION_USERDATA_SIZE);
811 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
812 if (s->bfd_section == NULL)
813 s->bfd_section = bfd_make_section (output_bfd, s->name);
814 if (s->bfd_section == NULL)
816 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
817 output_bfd->xvec->name, s->name);
819 s->bfd_section->output_section = s->bfd_section;
821 /* We initialize an output sections output offset to minus its own
822 vma to allow us to output a section through itself. */
823 s->bfd_section->output_offset = 0;
824 get_userdata (s->bfd_section) = new;
826 /* If there is a base address, make sure that any sections it might
827 mention are initialized. */
828 if (s->addr_tree != NULL)
829 exp_init_os (s->addr_tree);
831 if (s->load_base != NULL)
832 exp_init_os (s->load_base);
835 /* Make sure that all output sections mentioned in an expression are
836 initialized. */
838 static void
839 exp_init_os (etree_type *exp)
841 switch (exp->type.node_class)
843 case etree_assign:
844 exp_init_os (exp->assign.src);
845 break;
847 case etree_binary:
848 exp_init_os (exp->binary.lhs);
849 exp_init_os (exp->binary.rhs);
850 break;
852 case etree_trinary:
853 exp_init_os (exp->trinary.cond);
854 exp_init_os (exp->trinary.lhs);
855 exp_init_os (exp->trinary.rhs);
856 break;
858 case etree_assert:
859 exp_init_os (exp->assert_s.child);
860 break;
862 case etree_unary:
863 exp_init_os (exp->unary.child);
864 break;
866 case etree_name:
867 switch (exp->type.node_code)
869 case ADDR:
870 case LOADADDR:
871 case SIZEOF:
873 lang_output_section_statement_type *os;
875 os = lang_output_section_find (exp->name.name);
876 if (os != NULL && os->bfd_section == NULL)
877 init_os (os);
880 break;
882 default:
883 break;
887 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
888 once into the output. This routine checks each section, and
889 arrange to discard it if a section of the same name has already
890 been linked. If the section has COMDAT information, then it uses
891 that to decide whether the section should be included. This code
892 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
893 that is, it does not depend solely upon the section name.
894 section_already_linked is called via bfd_map_over_sections. */
896 /* This is the shape of the elements inside the already_linked hash
897 table. It maps a name onto a list of already_linked elements with
898 the same name. It's possible to get more than one element in a
899 list if the COMDAT sections have different names. */
901 struct already_linked_hash_entry
903 struct bfd_hash_entry root;
904 struct already_linked *entry;
907 struct already_linked
909 struct already_linked *next;
910 asection *sec;
913 /* The hash table. */
915 static struct bfd_hash_table already_linked_table;
917 static void
918 section_already_linked (bfd *abfd, asection *sec, void *data)
920 lang_input_statement_type *entry = data;
921 flagword flags;
922 const char *name;
923 struct already_linked *l;
924 struct already_linked_hash_entry *already_linked_list;
926 /* If we are only reading symbols from this object, then we want to
927 discard all sections. */
928 if (entry->just_syms_flag)
930 bfd_link_just_syms (sec, &link_info);
931 return;
934 flags = sec->flags;
935 if ((flags & SEC_LINK_ONCE) == 0)
936 return;
938 /* FIXME: When doing a relocatable link, we may have trouble
939 copying relocations in other sections that refer to local symbols
940 in the section being discarded. Those relocations will have to
941 be converted somehow; as of this writing I'm not sure that any of
942 the backends handle that correctly.
944 It is tempting to instead not discard link once sections when
945 doing a relocatable link (technically, they should be discarded
946 whenever we are building constructors). However, that fails,
947 because the linker winds up combining all the link once sections
948 into a single large link once section, which defeats the purpose
949 of having link once sections in the first place.
951 Also, not merging link once sections in a relocatable link
952 causes trouble for MIPS ELF, which relies on link once semantics
953 to handle the .reginfo section correctly. */
955 name = bfd_get_section_name (abfd, sec);
957 already_linked_list =
958 ((struct already_linked_hash_entry *)
959 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
961 for (l = already_linked_list->entry; l != NULL; l = l->next)
963 if (sec->comdat == NULL
964 || l->sec->comdat == NULL
965 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
967 /* The section has already been linked. See if we should
968 issue a warning. */
969 switch (flags & SEC_LINK_DUPLICATES)
971 default:
972 abort ();
974 case SEC_LINK_DUPLICATES_DISCARD:
975 break;
977 case SEC_LINK_DUPLICATES_ONE_ONLY:
978 if (sec->comdat == NULL)
979 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
980 abfd, name);
981 else
982 einfo (_("%P: %B: warning: ignoring duplicate `%s'"
983 " section symbol `%s'\n"),
984 abfd, name, sec->comdat->name);
985 break;
987 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
988 /* FIXME: We should really dig out the contents of both
989 sections and memcmp them. The COFF/PE spec says that
990 the Microsoft linker does not implement this
991 correctly, so I'm not going to bother doing it
992 either. */
993 /* Fall through. */
994 case SEC_LINK_DUPLICATES_SAME_SIZE:
995 if (sec->size != l->sec->size)
996 einfo (_("%P: %B: warning: duplicate section `%s'"
997 " has different size\n"),
998 abfd, name);
999 break;
1002 /* Set the output_section field so that lang_add_section
1003 does not create a lang_input_section structure for this
1004 section. Since there might be a symbol in the section
1005 being discarded, we must retain a pointer to the section
1006 which we are really going to use. */
1007 sec->output_section = bfd_abs_section_ptr;
1008 sec->kept_section = l->sec;
1010 if (flags & SEC_GROUP)
1011 bfd_discard_group (abfd, sec);
1013 return;
1017 /* This is the first section with this name. Record it. Allocate
1018 the memory from the same obstack as the hash table is kept in. */
1020 l = bfd_hash_allocate (&already_linked_table, sizeof *l);
1022 l->sec = sec;
1023 l->next = already_linked_list->entry;
1024 already_linked_list->entry = l;
1027 /* Support routines for the hash table used by section_already_linked,
1028 initialize the table, fill in an entry and remove the table. */
1030 static struct bfd_hash_entry *
1031 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
1032 struct bfd_hash_table *table,
1033 const char *string ATTRIBUTE_UNUSED)
1035 struct already_linked_hash_entry *ret =
1036 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
1038 ret->entry = NULL;
1040 return &ret->root;
1043 static void
1044 already_linked_table_init (void)
1046 if (! bfd_hash_table_init_n (&already_linked_table,
1047 already_linked_newfunc,
1048 42))
1049 einfo (_("%P%F: Failed to create hash table\n"));
1052 static void
1053 already_linked_table_free (void)
1055 bfd_hash_table_free (&already_linked_table);
1058 /* The wild routines.
1060 These expand statements like *(.text) and foo.o to a list of
1061 explicit actions, like foo.o(.text), bar.o(.text) and
1062 foo.o(.text, .data). */
1064 /* Return TRUE if the PATTERN argument is a wildcard pattern.
1065 Although backslashes are treated specially if a pattern contains
1066 wildcards, we do not consider the mere presence of a backslash to
1067 be enough to cause the pattern to be treated as a wildcard.
1068 That lets us handle DOS filenames more naturally. */
1070 static bfd_boolean
1071 wildcardp (const char *pattern)
1073 const char *s;
1075 for (s = pattern; *s != '\0'; ++s)
1076 if (*s == '?'
1077 || *s == '*'
1078 || *s == '[')
1079 return TRUE;
1080 return FALSE;
1083 /* Add SECTION to the output section OUTPUT. Do this by creating a
1084 lang_input_section statement which is placed at PTR. FILE is the
1085 input file which holds SECTION. */
1087 void
1088 lang_add_section (lang_statement_list_type *ptr,
1089 asection *section,
1090 lang_output_section_statement_type *output,
1091 lang_input_statement_type *file)
1093 flagword flags = section->flags;
1094 bfd_boolean discard;
1096 /* Discard sections marked with SEC_EXCLUDE. */
1097 discard = (flags & SEC_EXCLUDE) != 0;
1099 /* Discard input sections which are assigned to a section named
1100 DISCARD_SECTION_NAME. */
1101 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1102 discard = TRUE;
1104 /* Discard debugging sections if we are stripping debugging
1105 information. */
1106 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1107 && (flags & SEC_DEBUGGING) != 0)
1108 discard = TRUE;
1110 if (discard)
1112 if (section->output_section == NULL)
1114 /* This prevents future calls from assigning this section. */
1115 section->output_section = bfd_abs_section_ptr;
1117 return;
1120 if (section->output_section == NULL)
1122 bfd_boolean first;
1123 lang_input_section_type *new;
1124 flagword flags;
1126 if (output->bfd_section == NULL)
1127 init_os (output);
1129 first = ! output->bfd_section->linker_has_input;
1130 output->bfd_section->linker_has_input = 1;
1132 /* Add a section reference to the list. */
1133 new = new_stat (lang_input_section, ptr);
1135 new->section = section;
1136 new->ifile = file;
1137 section->output_section = output->bfd_section;
1139 flags = section->flags;
1141 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1142 to an output section, because we want to be able to include a
1143 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1144 section (I don't know why we want to do this, but we do).
1145 build_link_order in ldwrite.c handles this case by turning
1146 the embedded SEC_NEVER_LOAD section into a fill. */
1148 flags &= ~ SEC_NEVER_LOAD;
1150 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1151 already been processed. One reason to do this is that on pe
1152 format targets, .text$foo sections go into .text and it's odd
1153 to see .text with SEC_LINK_ONCE set. */
1155 if (! link_info.relocatable)
1156 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1158 /* If this is not the first input section, and the SEC_READONLY
1159 flag is not currently set, then don't set it just because the
1160 input section has it set. */
1162 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1163 flags &= ~ SEC_READONLY;
1165 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1166 if (! first
1167 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1168 != (flags & (SEC_MERGE | SEC_STRINGS))
1169 || ((flags & SEC_MERGE)
1170 && section->output_section->entsize != section->entsize)))
1172 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1173 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1176 section->output_section->flags |= flags;
1178 if (flags & SEC_MERGE)
1179 section->output_section->entsize = section->entsize;
1181 /* If SEC_READONLY is not set in the input section, then clear
1182 it from the output section. */
1183 if ((section->flags & SEC_READONLY) == 0)
1184 section->output_section->flags &= ~SEC_READONLY;
1186 switch (output->sectype)
1188 case normal_section:
1189 break;
1190 case dsect_section:
1191 case copy_section:
1192 case info_section:
1193 case overlay_section:
1194 output->bfd_section->flags &= ~SEC_ALLOC;
1195 break;
1196 case noload_section:
1197 output->bfd_section->flags &= ~SEC_LOAD;
1198 output->bfd_section->flags |= SEC_NEVER_LOAD;
1199 break;
1202 /* Copy over SEC_SMALL_DATA. */
1203 if (section->flags & SEC_SMALL_DATA)
1204 section->output_section->flags |= SEC_SMALL_DATA;
1206 if (section->alignment_power > output->bfd_section->alignment_power)
1207 output->bfd_section->alignment_power = section->alignment_power;
1209 /* If supplied an alignment, then force it. */
1210 if (output->section_alignment != -1)
1211 output->bfd_section->alignment_power = output->section_alignment;
1213 if (section->flags & SEC_BLOCK)
1215 section->output_section->flags |= SEC_BLOCK;
1216 /* FIXME: This value should really be obtained from the bfd... */
1217 output->block_value = 128;
1222 /* Handle wildcard sorting. This returns the lang_input_section which
1223 should follow the one we are going to create for SECTION and FILE,
1224 based on the sorting requirements of WILD. It returns NULL if the
1225 new section should just go at the end of the current list. */
1227 static lang_statement_union_type *
1228 wild_sort (lang_wild_statement_type *wild,
1229 struct wildcard_list *sec,
1230 lang_input_statement_type *file,
1231 asection *section)
1233 const char *section_name;
1234 lang_statement_union_type *l;
1236 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1237 return NULL;
1239 section_name = bfd_get_section_name (file->the_bfd, section);
1240 for (l = wild->children.head; l != NULL; l = l->header.next)
1242 lang_input_section_type *ls;
1244 if (l->header.type != lang_input_section_enum)
1245 continue;
1246 ls = &l->input_section;
1248 /* Sorting by filename takes precedence over sorting by section
1249 name. */
1251 if (wild->filenames_sorted)
1253 const char *fn, *ln;
1254 bfd_boolean fa, la;
1255 int i;
1257 /* The PE support for the .idata section as generated by
1258 dlltool assumes that files will be sorted by the name of
1259 the archive and then the name of the file within the
1260 archive. */
1262 if (file->the_bfd != NULL
1263 && bfd_my_archive (file->the_bfd) != NULL)
1265 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1266 fa = TRUE;
1268 else
1270 fn = file->filename;
1271 fa = FALSE;
1274 if (ls->ifile->the_bfd != NULL
1275 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1277 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1278 la = TRUE;
1280 else
1282 ln = ls->ifile->filename;
1283 la = FALSE;
1286 i = strcmp (fn, ln);
1287 if (i > 0)
1288 continue;
1289 else if (i < 0)
1290 break;
1292 if (fa || la)
1294 if (fa)
1295 fn = file->filename;
1296 if (la)
1297 ln = ls->ifile->filename;
1299 i = strcmp (fn, ln);
1300 if (i > 0)
1301 continue;
1302 else if (i < 0)
1303 break;
1307 /* Here either the files are not sorted by name, or we are
1308 looking at the sections for this file. */
1310 if (sec != NULL && sec->spec.sorted)
1312 if (strcmp (section_name,
1313 bfd_get_section_name (ls->ifile->the_bfd,
1314 ls->section))
1315 < 0)
1316 break;
1320 return l;
1323 /* Expand a wild statement for a particular FILE. SECTION may be
1324 NULL, in which case it is a wild card. */
1326 static void
1327 output_section_callback (lang_wild_statement_type *ptr,
1328 struct wildcard_list *sec,
1329 asection *section,
1330 lang_input_statement_type *file,
1331 void *output)
1333 lang_statement_union_type *before;
1335 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1336 if (unique_section_p (section))
1337 return;
1339 before = wild_sort (ptr, sec, file, section);
1341 /* Here BEFORE points to the lang_input_section which
1342 should follow the one we are about to add. If BEFORE
1343 is NULL, then the section should just go at the end
1344 of the current list. */
1346 if (before == NULL)
1347 lang_add_section (&ptr->children, section,
1348 (lang_output_section_statement_type *) output,
1349 file);
1350 else
1352 lang_statement_list_type list;
1353 lang_statement_union_type **pp;
1355 lang_list_init (&list);
1356 lang_add_section (&list, section,
1357 (lang_output_section_statement_type *) output,
1358 file);
1360 /* If we are discarding the section, LIST.HEAD will
1361 be NULL. */
1362 if (list.head != NULL)
1364 ASSERT (list.head->header.next == NULL);
1366 for (pp = &ptr->children.head;
1367 *pp != before;
1368 pp = &(*pp)->header.next)
1369 ASSERT (*pp != NULL);
1371 list.head->header.next = *pp;
1372 *pp = list.head;
1377 /* Check if all sections in a wild statement for a particular FILE
1378 are readonly. */
1380 static void
1381 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1382 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1383 asection *section,
1384 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1385 void *data)
1387 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1388 if (unique_section_p (section))
1389 return;
1391 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1392 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1395 /* This is passed a file name which must have been seen already and
1396 added to the statement tree. We will see if it has been opened
1397 already and had its symbols read. If not then we'll read it. */
1399 static lang_input_statement_type *
1400 lookup_name (const char *name)
1402 lang_input_statement_type *search;
1404 for (search = (lang_input_statement_type *) input_file_chain.head;
1405 search != NULL;
1406 search = (lang_input_statement_type *) search->next_real_file)
1408 /* Use the local_sym_name as the name of the file that has
1409 already been loaded as filename might have been transformed
1410 via the search directory lookup mechanism. */
1411 const char * filename = search->local_sym_name;
1413 if (filename == NULL && name == NULL)
1414 return search;
1415 if (filename != NULL
1416 && name != NULL
1417 && strcmp (filename, name) == 0)
1418 break;
1421 if (search == NULL)
1422 search = new_afile (name, lang_input_file_is_search_file_enum,
1423 default_target, FALSE);
1425 /* If we have already added this file, or this file is not real
1426 (FIXME: can that ever actually happen?) or the name is NULL
1427 (FIXME: can that ever actually happen?) don't add this file. */
1428 if (search->loaded
1429 || ! search->real
1430 || search->filename == NULL)
1431 return search;
1433 if (! load_symbols (search, NULL))
1434 return NULL;
1436 return search;
1439 /* Get the symbols for an input file. */
1441 static bfd_boolean
1442 load_symbols (lang_input_statement_type *entry,
1443 lang_statement_list_type *place)
1445 char **matching;
1447 if (entry->loaded)
1448 return TRUE;
1450 ldfile_open_file (entry);
1452 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1453 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1455 bfd_error_type err;
1456 lang_statement_list_type *hold;
1457 bfd_boolean bad_load = TRUE;
1458 bfd_boolean save_ldlang_sysrooted_script;
1460 err = bfd_get_error ();
1462 /* See if the emulation has some special knowledge. */
1463 if (ldemul_unrecognized_file (entry))
1464 return TRUE;
1466 if (err == bfd_error_file_ambiguously_recognized)
1468 char **p;
1470 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1471 einfo (_("%B: matching formats:"), entry->the_bfd);
1472 for (p = matching; *p != NULL; p++)
1473 einfo (" %s", *p);
1474 einfo ("%F\n");
1476 else if (err != bfd_error_file_not_recognized
1477 || place == NULL)
1478 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1479 else
1480 bad_load = FALSE;
1482 bfd_close (entry->the_bfd);
1483 entry->the_bfd = NULL;
1485 /* Try to interpret the file as a linker script. */
1486 ldfile_open_command_file (entry->filename);
1488 hold = stat_ptr;
1489 stat_ptr = place;
1490 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1491 ldlang_sysrooted_script = entry->sysrooted;
1493 ldfile_assumed_script = TRUE;
1494 parser_input = input_script;
1495 yyparse ();
1496 ldfile_assumed_script = FALSE;
1498 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1499 stat_ptr = hold;
1501 return ! bad_load;
1504 if (ldemul_recognized_file (entry))
1505 return TRUE;
1507 /* We don't call ldlang_add_file for an archive. Instead, the
1508 add_symbols entry point will call ldlang_add_file, via the
1509 add_archive_element callback, for each element of the archive
1510 which is used. */
1511 switch (bfd_get_format (entry->the_bfd))
1513 default:
1514 break;
1516 case bfd_object:
1517 ldlang_add_file (entry);
1518 if (trace_files || trace_file_tries)
1519 info_msg ("%I\n", entry);
1520 break;
1522 case bfd_archive:
1523 if (entry->whole_archive)
1525 bfd *member = NULL;
1526 bfd_boolean loaded = TRUE;
1528 for (;;)
1530 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1532 if (member == NULL)
1533 break;
1535 if (! bfd_check_format (member, bfd_object))
1537 einfo (_("%F%B: member %B in archive is not an object\n"),
1538 entry->the_bfd, member);
1539 loaded = FALSE;
1542 if (! ((*link_info.callbacks->add_archive_element)
1543 (&link_info, member, "--whole-archive")))
1544 abort ();
1546 if (! bfd_link_add_symbols (member, &link_info))
1548 einfo (_("%F%B: could not read symbols: %E\n"), member);
1549 loaded = FALSE;
1553 entry->loaded = loaded;
1554 return loaded;
1556 break;
1559 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1560 entry->loaded = TRUE;
1561 else
1562 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1564 return entry->loaded;
1567 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1568 may be NULL, indicating that it is a wildcard. Separate
1569 lang_input_section statements are created for each part of the
1570 expansion; they are added after the wild statement S. OUTPUT is
1571 the output section. */
1573 static void
1574 wild (lang_wild_statement_type *s,
1575 const char *target ATTRIBUTE_UNUSED,
1576 lang_output_section_statement_type *output)
1578 struct wildcard_list *sec;
1580 walk_wild (s, output_section_callback, output);
1582 for (sec = s->section_list; sec != NULL; sec = sec->next)
1584 if (default_common_section != NULL)
1585 break;
1586 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1588 /* Remember the section that common is going to in case we
1589 later get something which doesn't know where to put it. */
1590 default_common_section = output;
1595 /* Return TRUE iff target is the sought target. */
1597 static int
1598 get_target (const bfd_target *target, void *data)
1600 const char *sought = data;
1602 return strcmp (target->name, sought) == 0;
1605 /* Like strcpy() but convert to lower case as well. */
1607 static void
1608 stricpy (char *dest, char *src)
1610 char c;
1612 while ((c = *src++) != 0)
1613 *dest++ = TOLOWER (c);
1615 *dest = 0;
1618 /* Remove the first occurrence of needle (if any) in haystack
1619 from haystack. */
1621 static void
1622 strcut (char *haystack, char *needle)
1624 haystack = strstr (haystack, needle);
1626 if (haystack)
1628 char *src;
1630 for (src = haystack + strlen (needle); *src;)
1631 *haystack++ = *src++;
1633 *haystack = 0;
1637 /* Compare two target format name strings.
1638 Return a value indicating how "similar" they are. */
1640 static int
1641 name_compare (char *first, char *second)
1643 char *copy1;
1644 char *copy2;
1645 int result;
1647 copy1 = xmalloc (strlen (first) + 1);
1648 copy2 = xmalloc (strlen (second) + 1);
1650 /* Convert the names to lower case. */
1651 stricpy (copy1, first);
1652 stricpy (copy2, second);
1654 /* Remove size and endian strings from the name. */
1655 strcut (copy1, "big");
1656 strcut (copy1, "little");
1657 strcut (copy2, "big");
1658 strcut (copy2, "little");
1660 /* Return a value based on how many characters match,
1661 starting from the beginning. If both strings are
1662 the same then return 10 * their length. */
1663 for (result = 0; copy1[result] == copy2[result]; result++)
1664 if (copy1[result] == 0)
1666 result *= 10;
1667 break;
1670 free (copy1);
1671 free (copy2);
1673 return result;
1676 /* Set by closest_target_match() below. */
1677 static const bfd_target *winner;
1679 /* Scan all the valid bfd targets looking for one that has the endianness
1680 requirement that was specified on the command line, and is the nearest
1681 match to the original output target. */
1683 static int
1684 closest_target_match (const bfd_target *target, void *data)
1686 const bfd_target *original = data;
1688 if (command_line.endian == ENDIAN_BIG
1689 && target->byteorder != BFD_ENDIAN_BIG)
1690 return 0;
1692 if (command_line.endian == ENDIAN_LITTLE
1693 && target->byteorder != BFD_ENDIAN_LITTLE)
1694 return 0;
1696 /* Must be the same flavour. */
1697 if (target->flavour != original->flavour)
1698 return 0;
1700 /* If we have not found a potential winner yet, then record this one. */
1701 if (winner == NULL)
1703 winner = target;
1704 return 0;
1707 /* Oh dear, we now have two potential candidates for a successful match.
1708 Compare their names and choose the better one. */
1709 if (name_compare (target->name, original->name)
1710 > name_compare (winner->name, original->name))
1711 winner = target;
1713 /* Keep on searching until wqe have checked them all. */
1714 return 0;
1717 /* Return the BFD target format of the first input file. */
1719 static char *
1720 get_first_input_target (void)
1722 char *target = NULL;
1724 LANG_FOR_EACH_INPUT_STATEMENT (s)
1726 if (s->header.type == lang_input_statement_enum
1727 && s->real)
1729 ldfile_open_file (s);
1731 if (s->the_bfd != NULL
1732 && bfd_check_format (s->the_bfd, bfd_object))
1734 target = bfd_get_target (s->the_bfd);
1736 if (target != NULL)
1737 break;
1742 return target;
1745 const char *
1746 lang_get_output_target (void)
1748 const char *target;
1750 /* Has the user told us which output format to use? */
1751 if (output_target != NULL)
1752 return output_target;
1754 /* No - has the current target been set to something other than
1755 the default? */
1756 if (current_target != default_target)
1757 return current_target;
1759 /* No - can we determine the format of the first input file? */
1760 target = get_first_input_target ();
1761 if (target != NULL)
1762 return target;
1764 /* Failed - use the default output target. */
1765 return default_target;
1768 /* Open the output file. */
1770 static bfd *
1771 open_output (const char *name)
1773 bfd *output;
1775 output_target = lang_get_output_target ();
1777 /* Has the user requested a particular endianness on the command
1778 line? */
1779 if (command_line.endian != ENDIAN_UNSET)
1781 const bfd_target *target;
1782 enum bfd_endian desired_endian;
1784 /* Get the chosen target. */
1785 target = bfd_search_for_target (get_target, (void *) output_target);
1787 /* If the target is not supported, we cannot do anything. */
1788 if (target != NULL)
1790 if (command_line.endian == ENDIAN_BIG)
1791 desired_endian = BFD_ENDIAN_BIG;
1792 else
1793 desired_endian = BFD_ENDIAN_LITTLE;
1795 /* See if the target has the wrong endianness. This should
1796 not happen if the linker script has provided big and
1797 little endian alternatives, but some scrips don't do
1798 this. */
1799 if (target->byteorder != desired_endian)
1801 /* If it does, then see if the target provides
1802 an alternative with the correct endianness. */
1803 if (target->alternative_target != NULL
1804 && (target->alternative_target->byteorder == desired_endian))
1805 output_target = target->alternative_target->name;
1806 else
1808 /* Try to find a target as similar as possible to
1809 the default target, but which has the desired
1810 endian characteristic. */
1811 bfd_search_for_target (closest_target_match,
1812 (void *) target);
1814 /* Oh dear - we could not find any targets that
1815 satisfy our requirements. */
1816 if (winner == NULL)
1817 einfo (_("%P: warning: could not find any targets"
1818 " that match endianness requirement\n"));
1819 else
1820 output_target = winner->name;
1826 output = bfd_openw (name, output_target);
1828 if (output == NULL)
1830 if (bfd_get_error () == bfd_error_invalid_target)
1831 einfo (_("%P%F: target %s not found\n"), output_target);
1833 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1836 delete_output_file_on_failure = TRUE;
1838 #if 0
1839 output->flags |= D_PAGED;
1840 #endif
1842 if (! bfd_set_format (output, bfd_object))
1843 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1844 if (! bfd_set_arch_mach (output,
1845 ldfile_output_architecture,
1846 ldfile_output_machine))
1847 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1849 link_info.hash = bfd_link_hash_table_create (output);
1850 if (link_info.hash == NULL)
1851 einfo (_("%P%F: can not create link hash table: %E\n"));
1853 bfd_set_gp_size (output, g_switch_value);
1854 return output;
1857 static void
1858 ldlang_open_output (lang_statement_union_type *statement)
1860 switch (statement->header.type)
1862 case lang_output_statement_enum:
1863 ASSERT (output_bfd == NULL);
1864 output_bfd = open_output (statement->output_statement.name);
1865 ldemul_set_output_arch ();
1866 if (config.magic_demand_paged && !link_info.relocatable)
1867 output_bfd->flags |= D_PAGED;
1868 else
1869 output_bfd->flags &= ~D_PAGED;
1870 if (config.text_read_only)
1871 output_bfd->flags |= WP_TEXT;
1872 else
1873 output_bfd->flags &= ~WP_TEXT;
1874 if (link_info.traditional_format)
1875 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1876 else
1877 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1878 break;
1880 case lang_target_statement_enum:
1881 current_target = statement->target_statement.target;
1882 break;
1883 default:
1884 break;
1888 /* Convert between addresses in bytes and sizes in octets.
1889 For currently supported targets, octets_per_byte is always a power
1890 of two, so we can use shifts. */
1891 #define TO_ADDR(X) ((X) >> opb_shift)
1892 #define TO_SIZE(X) ((X) << opb_shift)
1894 /* Support the above. */
1895 static unsigned int opb_shift = 0;
1897 static void
1898 init_opb (void)
1900 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1901 ldfile_output_machine);
1902 opb_shift = 0;
1903 if (x > 1)
1904 while ((x & 1) == 0)
1906 x >>= 1;
1907 ++opb_shift;
1909 ASSERT (x == 1);
1912 /* Open all the input files. */
1914 static void
1915 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1917 for (; s != NULL; s = s->header.next)
1919 switch (s->header.type)
1921 case lang_constructors_statement_enum:
1922 open_input_bfds (constructor_list.head, force);
1923 break;
1924 case lang_output_section_statement_enum:
1925 open_input_bfds (s->output_section_statement.children.head, force);
1926 break;
1927 case lang_wild_statement_enum:
1928 /* Maybe we should load the file's symbols. */
1929 if (s->wild_statement.filename
1930 && ! wildcardp (s->wild_statement.filename))
1931 lookup_name (s->wild_statement.filename);
1932 open_input_bfds (s->wild_statement.children.head, force);
1933 break;
1934 case lang_group_statement_enum:
1936 struct bfd_link_hash_entry *undefs;
1938 /* We must continually search the entries in the group
1939 until no new symbols are added to the list of undefined
1940 symbols. */
1944 undefs = link_info.hash->undefs_tail;
1945 open_input_bfds (s->group_statement.children.head, TRUE);
1947 while (undefs != link_info.hash->undefs_tail);
1949 break;
1950 case lang_target_statement_enum:
1951 current_target = s->target_statement.target;
1952 break;
1953 case lang_input_statement_enum:
1954 if (s->input_statement.real)
1956 lang_statement_list_type add;
1958 s->input_statement.target = current_target;
1960 /* If we are being called from within a group, and this
1961 is an archive which has already been searched, then
1962 force it to be researched unless the whole archive
1963 has been loaded already. */
1964 if (force
1965 && !s->input_statement.whole_archive
1966 && s->input_statement.loaded
1967 && bfd_check_format (s->input_statement.the_bfd,
1968 bfd_archive))
1969 s->input_statement.loaded = FALSE;
1971 lang_list_init (&add);
1973 if (! load_symbols (&s->input_statement, &add))
1974 config.make_executable = FALSE;
1976 if (add.head != NULL)
1978 *add.tail = s->header.next;
1979 s->header.next = add.head;
1982 break;
1983 default:
1984 break;
1989 /* If there are [COMMONS] statements, put a wild one into the bss
1990 section. */
1992 static void
1993 lang_reasonable_defaults (void)
1995 #if 0
1996 lang_output_section_statement_lookup (".text");
1997 lang_output_section_statement_lookup (".data");
1999 default_common_section = lang_output_section_statement_lookup (".bss");
2001 if (!placed_commons)
2003 lang_wild_statement_type *new =
2004 new_stat (lang_wild_statement,
2005 &default_common_section->children);
2007 new->section_name = "COMMON";
2008 new->filename = NULL;
2009 lang_list_init (&new->children);
2011 #endif
2014 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2016 void
2017 lang_track_definedness (const char *name)
2019 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2020 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2023 /* New-function for the definedness hash table. */
2025 static struct bfd_hash_entry *
2026 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2027 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2028 const char *name ATTRIBUTE_UNUSED)
2030 struct lang_definedness_hash_entry *ret
2031 = (struct lang_definedness_hash_entry *) entry;
2033 if (ret == NULL)
2034 ret = (struct lang_definedness_hash_entry *)
2035 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2037 if (ret == NULL)
2038 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2040 ret->iteration = -1;
2041 return &ret->root;
2044 /* Return the iteration when the definition of NAME was last updated. A
2045 value of -1 means that the symbol is not defined in the linker script
2046 or the command line, but may be defined in the linker symbol table. */
2049 lang_symbol_definition_iteration (const char *name)
2051 struct lang_definedness_hash_entry *defentry
2052 = (struct lang_definedness_hash_entry *)
2053 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2055 /* We've already created this one on the presence of DEFINED in the
2056 script, so it can't be NULL unless something is borked elsewhere in
2057 the code. */
2058 if (defentry == NULL)
2059 FAIL ();
2061 return defentry->iteration;
2064 /* Update the definedness state of NAME. */
2066 void
2067 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
2069 struct lang_definedness_hash_entry *defentry
2070 = (struct lang_definedness_hash_entry *)
2071 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2073 /* We don't keep track of symbols not tested with DEFINED. */
2074 if (defentry == NULL)
2075 return;
2077 /* If the symbol was already defined, and not from an earlier statement
2078 iteration, don't update the definedness iteration, because that'd
2079 make the symbol seem defined in the linker script at this point, and
2080 it wasn't; it was defined in some object. If we do anyway, DEFINED
2081 would start to yield false before this point and the construct "sym =
2082 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2083 in an object. */
2084 if (h->type != bfd_link_hash_undefined
2085 && h->type != bfd_link_hash_common
2086 && h->type != bfd_link_hash_new
2087 && defentry->iteration == -1)
2088 return;
2090 defentry->iteration = lang_statement_iteration;
2093 /* Add the supplied name to the symbol table as an undefined reference.
2094 This is a two step process as the symbol table doesn't even exist at
2095 the time the ld command line is processed. First we put the name
2096 on a list, then, once the output file has been opened, transfer the
2097 name to the symbol table. */
2099 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2101 #define ldlang_undef_chain_list_head entry_symbol.next
2103 void
2104 ldlang_add_undef (const char *const name)
2106 ldlang_undef_chain_list_type *new =
2107 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2109 new->next = ldlang_undef_chain_list_head;
2110 ldlang_undef_chain_list_head = new;
2112 new->name = xstrdup (name);
2114 if (output_bfd != NULL)
2115 insert_undefined (new->name);
2118 /* Insert NAME as undefined in the symbol table. */
2120 static void
2121 insert_undefined (const char *name)
2123 struct bfd_link_hash_entry *h;
2125 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2126 if (h == NULL)
2127 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2128 if (h->type == bfd_link_hash_new)
2130 h->type = bfd_link_hash_undefined;
2131 h->u.undef.abfd = NULL;
2132 bfd_link_add_undef (link_info.hash, h);
2136 /* Run through the list of undefineds created above and place them
2137 into the linker hash table as undefined symbols belonging to the
2138 script file. */
2140 static void
2141 lang_place_undefineds (void)
2143 ldlang_undef_chain_list_type *ptr;
2145 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2146 insert_undefined (ptr->name);
2149 /* Check for all readonly or some readwrite sections. */
2151 static void
2152 check_input_sections
2153 (lang_statement_union_type *s,
2154 lang_output_section_statement_type *output_section_statement)
2156 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2158 switch (s->header.type)
2160 case lang_wild_statement_enum:
2161 walk_wild (&s->wild_statement, check_section_callback,
2162 output_section_statement);
2163 if (! output_section_statement->all_input_readonly)
2164 return;
2165 break;
2166 case lang_constructors_statement_enum:
2167 check_input_sections (constructor_list.head,
2168 output_section_statement);
2169 if (! output_section_statement->all_input_readonly)
2170 return;
2171 break;
2172 case lang_group_statement_enum:
2173 check_input_sections (s->group_statement.children.head,
2174 output_section_statement);
2175 if (! output_section_statement->all_input_readonly)
2176 return;
2177 break;
2178 default:
2179 break;
2184 /* Open input files and attach to output sections. */
2186 static void
2187 map_input_to_output_sections
2188 (lang_statement_union_type *s, const char *target,
2189 lang_output_section_statement_type *output_section_statement)
2191 for (; s != NULL; s = s->header.next)
2193 switch (s->header.type)
2195 case lang_wild_statement_enum:
2196 wild (&s->wild_statement, target, output_section_statement);
2197 break;
2198 case lang_constructors_statement_enum:
2199 map_input_to_output_sections (constructor_list.head,
2200 target,
2201 output_section_statement);
2202 break;
2203 case lang_output_section_statement_enum:
2204 if (s->output_section_statement.constraint)
2206 if (s->output_section_statement.constraint == -1)
2207 break;
2208 s->output_section_statement.all_input_readonly = TRUE;
2209 check_input_sections (s->output_section_statement.children.head,
2210 &s->output_section_statement);
2211 if ((s->output_section_statement.all_input_readonly
2212 && s->output_section_statement.constraint == ONLY_IF_RW)
2213 || (!s->output_section_statement.all_input_readonly
2214 && s->output_section_statement.constraint == ONLY_IF_RO))
2216 s->output_section_statement.constraint = -1;
2217 break;
2221 map_input_to_output_sections (s->output_section_statement.children.head,
2222 target,
2223 &s->output_section_statement);
2224 break;
2225 case lang_output_statement_enum:
2226 break;
2227 case lang_target_statement_enum:
2228 target = s->target_statement.target;
2229 break;
2230 case lang_group_statement_enum:
2231 map_input_to_output_sections (s->group_statement.children.head,
2232 target,
2233 output_section_statement);
2234 break;
2235 case lang_data_statement_enum:
2236 /* Make sure that any sections mentioned in the expression
2237 are initialized. */
2238 exp_init_os (s->data_statement.exp);
2239 /* FALLTHROUGH */
2240 case lang_fill_statement_enum:
2241 case lang_input_section_enum:
2242 case lang_object_symbols_statement_enum:
2243 case lang_reloc_statement_enum:
2244 case lang_padding_statement_enum:
2245 case lang_input_statement_enum:
2246 if (output_section_statement != NULL
2247 && output_section_statement->bfd_section == NULL)
2248 init_os (output_section_statement);
2249 break;
2250 case lang_assignment_statement_enum:
2251 if (output_section_statement != NULL
2252 && output_section_statement->bfd_section == NULL)
2253 init_os (output_section_statement);
2255 /* Make sure that any sections mentioned in the assignment
2256 are initialized. */
2257 exp_init_os (s->assignment_statement.exp);
2258 break;
2259 case lang_afile_asection_pair_statement_enum:
2260 FAIL ();
2261 break;
2262 case lang_address_statement_enum:
2263 /* Mark the specified section with the supplied address. */
2265 lang_output_section_statement_type *os =
2266 lang_output_section_statement_lookup
2267 (s->address_statement.section_name);
2269 if (os->bfd_section == NULL)
2270 init_os (os);
2271 os->addr_tree = s->address_statement.address;
2273 break;
2278 /* An output section might have been removed after its statement was
2279 added. For example, ldemul_before_allocation can remove dynamic
2280 sections if they turn out to be not needed. Clean them up here. */
2282 static void
2283 strip_excluded_output_sections (void)
2285 lang_statement_union_type *u;
2287 for (u = lang_output_section_statement.head;
2288 u != NULL;
2289 u = u->output_section_statement.next)
2291 lang_output_section_statement_type *os;
2292 asection *s;
2294 os = &u->output_section_statement;
2295 if (os->constraint == -1)
2296 continue;
2297 s = os->bfd_section;
2298 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2300 asection **p;
2302 os->bfd_section = NULL;
2304 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2305 if (*p == s)
2307 bfd_section_list_remove (output_bfd, p);
2308 output_bfd->section_count--;
2309 break;
2315 static void
2316 print_output_section_statement
2317 (lang_output_section_statement_type *output_section_statement)
2319 asection *section = output_section_statement->bfd_section;
2320 int len;
2322 if (output_section_statement != abs_output_section)
2324 minfo ("\n%s", output_section_statement->name);
2326 if (section != NULL)
2328 print_dot = section->vma;
2330 len = strlen (output_section_statement->name);
2331 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2333 print_nl ();
2334 len = 0;
2336 while (len < SECTION_NAME_MAP_LENGTH)
2338 print_space ();
2339 ++len;
2342 minfo ("0x%V %W", section->vma, section->size);
2344 if (output_section_statement->load_base != NULL)
2346 bfd_vma addr;
2348 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2349 "load base", lang_final_phase_enum);
2350 minfo (_(" load address 0x%V"), addr);
2354 print_nl ();
2357 print_statement_list (output_section_statement->children.head,
2358 output_section_statement);
2361 static void
2362 print_assignment (lang_assignment_statement_type *assignment,
2363 lang_output_section_statement_type *output_section)
2365 int i;
2366 etree_value_type result;
2368 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2369 print_space ();
2371 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2372 lang_final_phase_enum, print_dot, &print_dot);
2373 if (result.valid_p)
2375 const char *dst;
2376 bfd_vma value;
2378 value = result.value + result.section->bfd_section->vma;
2379 dst = assignment->exp->assign.dst;
2381 minfo ("0x%V", value);
2382 if (dst[0] == '.' && dst[1] == 0)
2383 print_dot = value;
2385 else
2387 minfo ("*undef* ");
2388 #ifdef BFD64
2389 minfo (" ");
2390 #endif
2393 minfo (" ");
2395 exp_print_tree (assignment->exp);
2397 print_nl ();
2400 static void
2401 print_input_statement (lang_input_statement_type *statm)
2403 if (statm->filename != NULL)
2405 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2409 /* Print all symbols defined in a particular section. This is called
2410 via bfd_link_hash_traverse, or by print_all_symbols. */
2412 static bfd_boolean
2413 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2415 asection *sec = ptr;
2417 if ((hash_entry->type == bfd_link_hash_defined
2418 || hash_entry->type == bfd_link_hash_defweak)
2419 && sec == hash_entry->u.def.section)
2421 int i;
2423 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2424 print_space ();
2425 minfo ("0x%V ",
2426 (hash_entry->u.def.value
2427 + hash_entry->u.def.section->output_offset
2428 + hash_entry->u.def.section->output_section->vma));
2430 minfo (" %T\n", hash_entry->root.string);
2433 return TRUE;
2436 static void
2437 print_all_symbols (sec)
2438 asection *sec;
2440 struct fat_user_section_struct *ud = get_userdata (sec);
2441 struct map_symbol_def *def;
2443 *ud->map_symbol_def_tail = 0;
2444 for (def = ud->map_symbol_def_head; def; def = def->next)
2445 print_one_symbol (def->entry, sec);
2448 /* Print information about an input section to the map file. */
2450 static void
2451 print_input_section (lang_input_section_type *in)
2453 asection *i = in->section;
2454 bfd_size_type size = i->size;
2456 init_opb ();
2457 if (size != 0)
2459 int len;
2460 bfd_vma addr;
2462 print_space ();
2463 minfo ("%s", i->name);
2465 len = 1 + strlen (i->name);
2466 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2468 print_nl ();
2469 len = 0;
2471 while (len < SECTION_NAME_MAP_LENGTH)
2473 print_space ();
2474 ++len;
2477 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2478 addr = i->output_section->vma + i->output_offset;
2479 else
2481 addr = print_dot;
2482 size = 0;
2485 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2487 if (size != i->rawsize && i->rawsize != 0)
2489 len = SECTION_NAME_MAP_LENGTH + 3;
2490 #ifdef BFD64
2491 len += 16;
2492 #else
2493 len += 8;
2494 #endif
2495 while (len > 0)
2497 print_space ();
2498 --len;
2501 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2504 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2506 if (command_line.reduce_memory_overheads)
2507 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2508 else
2509 print_all_symbols (i);
2511 print_dot = addr + TO_ADDR (size);
2516 static void
2517 print_fill_statement (lang_fill_statement_type *fill)
2519 size_t size;
2520 unsigned char *p;
2521 fputs (" FILL mask 0x", config.map_file);
2522 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2523 fprintf (config.map_file, "%02x", *p);
2524 fputs ("\n", config.map_file);
2527 static void
2528 print_data_statement (lang_data_statement_type *data)
2530 int i;
2531 bfd_vma addr;
2532 bfd_size_type size;
2533 const char *name;
2535 init_opb ();
2536 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2537 print_space ();
2539 addr = data->output_vma;
2540 if (data->output_section != NULL)
2541 addr += data->output_section->vma;
2543 switch (data->type)
2545 default:
2546 abort ();
2547 case BYTE:
2548 size = BYTE_SIZE;
2549 name = "BYTE";
2550 break;
2551 case SHORT:
2552 size = SHORT_SIZE;
2553 name = "SHORT";
2554 break;
2555 case LONG:
2556 size = LONG_SIZE;
2557 name = "LONG";
2558 break;
2559 case QUAD:
2560 size = QUAD_SIZE;
2561 name = "QUAD";
2562 break;
2563 case SQUAD:
2564 size = QUAD_SIZE;
2565 name = "SQUAD";
2566 break;
2569 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2571 if (data->exp->type.node_class != etree_value)
2573 print_space ();
2574 exp_print_tree (data->exp);
2577 print_nl ();
2579 print_dot = addr + TO_ADDR (size);
2582 /* Print an address statement. These are generated by options like
2583 -Ttext. */
2585 static void
2586 print_address_statement (lang_address_statement_type *address)
2588 minfo (_("Address of section %s set to "), address->section_name);
2589 exp_print_tree (address->address);
2590 print_nl ();
2593 /* Print a reloc statement. */
2595 static void
2596 print_reloc_statement (lang_reloc_statement_type *reloc)
2598 int i;
2599 bfd_vma addr;
2600 bfd_size_type size;
2602 init_opb ();
2603 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2604 print_space ();
2606 addr = reloc->output_vma;
2607 if (reloc->output_section != NULL)
2608 addr += reloc->output_section->vma;
2610 size = bfd_get_reloc_size (reloc->howto);
2612 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2614 if (reloc->name != NULL)
2615 minfo ("%s+", reloc->name);
2616 else
2617 minfo ("%s+", reloc->section->name);
2619 exp_print_tree (reloc->addend_exp);
2621 print_nl ();
2623 print_dot = addr + TO_ADDR (size);
2626 static void
2627 print_padding_statement (lang_padding_statement_type *s)
2629 int len;
2630 bfd_vma addr;
2632 init_opb ();
2633 minfo (" *fill*");
2635 len = sizeof " *fill*" - 1;
2636 while (len < SECTION_NAME_MAP_LENGTH)
2638 print_space ();
2639 ++len;
2642 addr = s->output_offset;
2643 if (s->output_section != NULL)
2644 addr += s->output_section->vma;
2645 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
2647 if (s->fill->size != 0)
2649 size_t size;
2650 unsigned char *p;
2651 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2652 fprintf (config.map_file, "%02x", *p);
2655 print_nl ();
2657 print_dot = addr + TO_ADDR (s->size);
2660 static void
2661 print_wild_statement (lang_wild_statement_type *w,
2662 lang_output_section_statement_type *os)
2664 struct wildcard_list *sec;
2666 print_space ();
2668 if (w->filenames_sorted)
2669 minfo ("SORT(");
2670 if (w->filename != NULL)
2671 minfo ("%s", w->filename);
2672 else
2673 minfo ("*");
2674 if (w->filenames_sorted)
2675 minfo (")");
2677 minfo ("(");
2678 for (sec = w->section_list; sec; sec = sec->next)
2680 if (sec->spec.sorted)
2681 minfo ("SORT(");
2682 if (sec->spec.exclude_name_list != NULL)
2684 name_list *tmp;
2685 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2686 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2687 minfo (" %s", tmp->name);
2688 minfo (") ");
2690 if (sec->spec.name != NULL)
2691 minfo ("%s", sec->spec.name);
2692 else
2693 minfo ("*");
2694 if (sec->spec.sorted)
2695 minfo (")");
2696 if (sec->next)
2697 minfo (" ");
2699 minfo (")");
2701 print_nl ();
2703 print_statement_list (w->children.head, os);
2706 /* Print a group statement. */
2708 static void
2709 print_group (lang_group_statement_type *s,
2710 lang_output_section_statement_type *os)
2712 fprintf (config.map_file, "START GROUP\n");
2713 print_statement_list (s->children.head, os);
2714 fprintf (config.map_file, "END GROUP\n");
2717 /* Print the list of statements in S.
2718 This can be called for any statement type. */
2720 static void
2721 print_statement_list (lang_statement_union_type *s,
2722 lang_output_section_statement_type *os)
2724 while (s != NULL)
2726 print_statement (s, os);
2727 s = s->header.next;
2731 /* Print the first statement in statement list S.
2732 This can be called for any statement type. */
2734 static void
2735 print_statement (lang_statement_union_type *s,
2736 lang_output_section_statement_type *os)
2738 switch (s->header.type)
2740 default:
2741 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2742 FAIL ();
2743 break;
2744 case lang_constructors_statement_enum:
2745 if (constructor_list.head != NULL)
2747 if (constructors_sorted)
2748 minfo (" SORT (CONSTRUCTORS)\n");
2749 else
2750 minfo (" CONSTRUCTORS\n");
2751 print_statement_list (constructor_list.head, os);
2753 break;
2754 case lang_wild_statement_enum:
2755 print_wild_statement (&s->wild_statement, os);
2756 break;
2757 case lang_address_statement_enum:
2758 print_address_statement (&s->address_statement);
2759 break;
2760 case lang_object_symbols_statement_enum:
2761 minfo (" CREATE_OBJECT_SYMBOLS\n");
2762 break;
2763 case lang_fill_statement_enum:
2764 print_fill_statement (&s->fill_statement);
2765 break;
2766 case lang_data_statement_enum:
2767 print_data_statement (&s->data_statement);
2768 break;
2769 case lang_reloc_statement_enum:
2770 print_reloc_statement (&s->reloc_statement);
2771 break;
2772 case lang_input_section_enum:
2773 print_input_section (&s->input_section);
2774 break;
2775 case lang_padding_statement_enum:
2776 print_padding_statement (&s->padding_statement);
2777 break;
2778 case lang_output_section_statement_enum:
2779 print_output_section_statement (&s->output_section_statement);
2780 break;
2781 case lang_assignment_statement_enum:
2782 print_assignment (&s->assignment_statement, os);
2783 break;
2784 case lang_target_statement_enum:
2785 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2786 break;
2787 case lang_output_statement_enum:
2788 minfo ("OUTPUT(%s", s->output_statement.name);
2789 if (output_target != NULL)
2790 minfo (" %s", output_target);
2791 minfo (")\n");
2792 break;
2793 case lang_input_statement_enum:
2794 print_input_statement (&s->input_statement);
2795 break;
2796 case lang_group_statement_enum:
2797 print_group (&s->group_statement, os);
2798 break;
2799 case lang_afile_asection_pair_statement_enum:
2800 FAIL ();
2801 break;
2805 static void
2806 print_statements (void)
2808 print_statement_list (statement_list.head, abs_output_section);
2811 /* Print the first N statements in statement list S to STDERR.
2812 If N == 0, nothing is printed.
2813 If N < 0, the entire list is printed.
2814 Intended to be called from GDB. */
2816 void
2817 dprint_statement (lang_statement_union_type *s, int n)
2819 FILE *map_save = config.map_file;
2821 config.map_file = stderr;
2823 if (n < 0)
2824 print_statement_list (s, abs_output_section);
2825 else
2827 while (s && --n >= 0)
2829 print_statement (s, abs_output_section);
2830 s = s->header.next;
2834 config.map_file = map_save;
2837 static void
2838 insert_pad (lang_statement_union_type **ptr,
2839 fill_type *fill,
2840 unsigned int alignment_needed,
2841 asection *output_section,
2842 bfd_vma dot)
2844 static fill_type zero_fill = { 1, { 0 } };
2845 lang_statement_union_type *pad;
2847 pad = ((lang_statement_union_type *)
2848 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2849 if (ptr != &statement_list.head
2850 && pad->header.type == lang_padding_statement_enum
2851 && pad->padding_statement.output_section == output_section)
2853 /* Use the existing pad statement. The above test on output
2854 section is probably redundant, but it doesn't hurt to check. */
2856 else
2858 /* Make a new padding statement, linked into existing chain. */
2859 pad = stat_alloc (sizeof (lang_padding_statement_type));
2860 pad->header.next = *ptr;
2861 *ptr = pad;
2862 pad->header.type = lang_padding_statement_enum;
2863 pad->padding_statement.output_section = output_section;
2864 if (fill == NULL)
2865 fill = &zero_fill;
2866 pad->padding_statement.fill = fill;
2868 pad->padding_statement.output_offset = dot - output_section->vma;
2869 pad->padding_statement.size = alignment_needed;
2870 output_section->size += alignment_needed;
2873 /* Work out how much this section will move the dot point. */
2875 static bfd_vma
2876 size_input_section
2877 (lang_statement_union_type **this_ptr,
2878 lang_output_section_statement_type *output_section_statement,
2879 fill_type *fill,
2880 bfd_vma dot)
2882 lang_input_section_type *is = &((*this_ptr)->input_section);
2883 asection *i = is->section;
2885 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
2887 unsigned int alignment_needed;
2888 asection *o;
2890 /* Align this section first to the input sections requirement,
2891 then to the output section's requirement. If this alignment
2892 is greater than any seen before, then record it too. Perform
2893 the alignment by inserting a magic 'padding' statement. */
2895 if (output_section_statement->subsection_alignment != -1)
2896 i->alignment_power = output_section_statement->subsection_alignment;
2898 o = output_section_statement->bfd_section;
2899 if (o->alignment_power < i->alignment_power)
2900 o->alignment_power = i->alignment_power;
2902 alignment_needed = align_power (dot, i->alignment_power) - dot;
2904 if (alignment_needed != 0)
2906 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2907 dot += alignment_needed;
2910 /* Remember where in the output section this input section goes. */
2912 i->output_offset = dot - o->vma;
2914 /* Mark how big the output section must be to contain this now. */
2915 dot += TO_ADDR (i->size);
2916 o->size = TO_SIZE (dot - o->vma);
2918 else
2920 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2923 return dot;
2926 #define IGNORE_SECTION(s) \
2927 (((s->flags & SEC_THREAD_LOCAL) != 0 \
2928 ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \
2929 : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \
2930 || s->size == 0)
2932 /* Check to see if any allocated sections overlap with other allocated
2933 sections. This can happen when the linker script specifically specifies
2934 the output section addresses of the two sections. */
2936 static void
2937 lang_check_section_addresses (void)
2939 asection *s;
2941 /* Scan all sections in the output list. */
2942 for (s = output_bfd->sections; s != NULL; s = s->next)
2944 asection *os;
2946 /* Ignore sections which are not loaded or which have no contents. */
2947 if (IGNORE_SECTION (s))
2948 continue;
2950 /* Once we reach section 's' stop our seach. This prevents two
2951 warning messages from being produced, one for 'section A overlaps
2952 section B' and one for 'section B overlaps section A'. */
2953 for (os = output_bfd->sections; os != s; os = os->next)
2955 bfd_vma s_start;
2956 bfd_vma s_end;
2957 bfd_vma os_start;
2958 bfd_vma os_end;
2960 /* Only consider loadable sections with real contents. */
2961 if (IGNORE_SECTION (os))
2962 continue;
2964 /* We must check the sections' LMA addresses not their
2965 VMA addresses because overlay sections can have
2966 overlapping VMAs but they must have distinct LMAs. */
2967 s_start = bfd_section_lma (output_bfd, s);
2968 os_start = bfd_section_lma (output_bfd, os);
2969 s_end = s_start + TO_ADDR (s->size) - 1;
2970 os_end = os_start + TO_ADDR (os->size) - 1;
2972 /* Look for an overlap. */
2973 if ((s_end < os_start) || (s_start > os_end))
2974 continue;
2976 einfo (
2977 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2978 s->name, s_start, s_end, os->name, os_start, os_end);
2980 /* Once we have found one overlap for this section,
2981 stop looking for others. */
2982 break;
2987 /* Make sure the new address is within the region. We explicitly permit the
2988 current address to be at the exact end of the region when the address is
2989 non-zero, in case the region is at the end of addressable memory and the
2990 calculation wraps around. */
2992 static void
2993 os_region_check (lang_output_section_statement_type *os,
2994 lang_memory_region_type *region,
2995 etree_type *tree,
2996 bfd_vma base)
2998 if ((region->current < region->origin
2999 || (region->current - region->origin > region->length))
3000 && ((region->current != region->origin + region->length)
3001 || base == 0))
3003 if (tree != NULL)
3005 einfo (_("%X%P: address 0x%v of %B section %s"
3006 " is not within region %s\n"),
3007 region->current,
3008 os->bfd_section->owner,
3009 os->bfd_section->name,
3010 region->name);
3012 else
3014 einfo (_("%X%P: region %s is full (%B section %s)\n"),
3015 region->name,
3016 os->bfd_section->owner,
3017 os->bfd_section->name);
3019 /* Reset the region pointer. */
3020 region->current = region->origin;
3024 /* Set the sizes for all the output sections. */
3026 static bfd_vma
3027 lang_size_sections_1
3028 (lang_statement_union_type *s,
3029 lang_output_section_statement_type *output_section_statement,
3030 lang_statement_union_type **prev,
3031 fill_type *fill,
3032 bfd_vma dot,
3033 bfd_boolean *relax,
3034 bfd_boolean check_regions)
3036 /* Size up the sections from their constituent parts. */
3037 for (; s != NULL; s = s->header.next)
3039 switch (s->header.type)
3041 case lang_output_section_statement_enum:
3043 bfd_vma after;
3044 lang_output_section_statement_type *os;
3046 os = &s->output_section_statement;
3047 if (os->bfd_section == NULL)
3048 /* This section was never actually created. */
3049 break;
3051 /* If this is a COFF shared library section, use the size and
3052 address from the input section. FIXME: This is COFF
3053 specific; it would be cleaner if there were some other way
3054 to do this, but nothing simple comes to mind. */
3055 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
3057 asection *input;
3059 if (os->children.head == NULL
3060 || os->children.head->header.next != NULL
3061 || (os->children.head->header.type
3062 != lang_input_section_enum))
3063 einfo (_("%P%X: Internal error on COFF shared library"
3064 " section %s\n"), os->name);
3066 input = os->children.head->input_section.section;
3067 bfd_set_section_vma (os->bfd_section->owner,
3068 os->bfd_section,
3069 bfd_section_vma (input->owner, input));
3070 os->bfd_section->size = input->size;
3071 break;
3074 if (bfd_is_abs_section (os->bfd_section))
3076 /* No matter what happens, an abs section starts at zero. */
3077 ASSERT (os->bfd_section->vma == 0);
3079 else
3081 if (os->addr_tree == NULL)
3083 /* No address specified for this section, get one
3084 from the region specification. */
3085 if (os->region == NULL
3086 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
3087 && os->region->name[0] == '*'
3088 && strcmp (os->region->name,
3089 DEFAULT_MEMORY_REGION) == 0))
3091 os->region = lang_memory_default (os->bfd_section);
3094 /* If a loadable section is using the default memory
3095 region, and some non default memory regions were
3096 defined, issue an error message. */
3097 if (!IGNORE_SECTION (os->bfd_section)
3098 && ! link_info.relocatable
3099 && check_regions
3100 && strcmp (os->region->name,
3101 DEFAULT_MEMORY_REGION) == 0
3102 && lang_memory_region_list != NULL
3103 && (strcmp (lang_memory_region_list->name,
3104 DEFAULT_MEMORY_REGION) != 0
3105 || lang_memory_region_list->next != NULL))
3107 /* By default this is an error rather than just a
3108 warning because if we allocate the section to the
3109 default memory region we can end up creating an
3110 excessively large binary, or even seg faulting when
3111 attempting to perform a negative seek. See
3112 sources.redhat.com/ml/binutils/2003-04/msg00423.html
3113 for an example of this. This behaviour can be
3114 overridden by the using the --no-check-sections
3115 switch. */
3116 if (command_line.check_section_addresses)
3117 einfo (_("%P%F: error: no memory region specified"
3118 " for loadable section `%s'\n"),
3119 bfd_get_section_name (output_bfd,
3120 os->bfd_section));
3121 else
3122 einfo (_("%P: warning: no memory region specified"
3123 " for loadable section `%s'\n"),
3124 bfd_get_section_name (output_bfd,
3125 os->bfd_section));
3128 dot = os->region->current;
3130 if (os->section_alignment == -1)
3132 bfd_vma olddot;
3134 olddot = dot;
3135 dot = align_power (dot,
3136 os->bfd_section->alignment_power);
3138 if (dot != olddot && config.warn_section_align)
3139 einfo (_("%P: warning: changing start of section"
3140 " %s by %u bytes\n"),
3141 os->name, (unsigned int) (dot - olddot));
3144 else
3146 etree_value_type r;
3148 os->processed = -1;
3149 r = exp_fold_tree (os->addr_tree,
3150 abs_output_section,
3151 lang_allocating_phase_enum,
3152 dot, &dot);
3153 os->processed = 0;
3155 if (!r.valid_p)
3156 einfo (_("%F%S: non constant or forward reference"
3157 " address expression for section %s\n"),
3158 os->name);
3160 dot = r.value + r.section->bfd_section->vma;
3163 /* The section starts here.
3164 First, align to what the section needs. */
3166 if (os->section_alignment != -1)
3167 dot = align_power (dot, os->section_alignment);
3169 bfd_set_section_vma (0, os->bfd_section, dot);
3171 os->bfd_section->output_offset = 0;
3174 lang_size_sections_1 (os->children.head, os, &os->children.head,
3175 os->fill, dot, relax, check_regions);
3177 /* Put the section within the requested block size, or
3178 align at the block boundary. */
3179 after = ((os->bfd_section->vma
3180 + TO_ADDR (os->bfd_section->size)
3181 + os->block_value - 1)
3182 & - (bfd_vma) os->block_value);
3184 if (bfd_is_abs_section (os->bfd_section))
3185 ASSERT (after == os->bfd_section->vma);
3186 else
3187 os->bfd_section->size
3188 = TO_SIZE (after - os->bfd_section->vma);
3190 dot = os->bfd_section->vma;
3191 /* .tbss sections effectively have zero size. */
3192 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3193 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3194 || link_info.relocatable)
3195 dot += TO_ADDR (os->bfd_section->size);
3197 os->processed = 1;
3199 if (os->update_dot_tree != 0)
3200 exp_fold_tree (os->update_dot_tree, abs_output_section,
3201 lang_allocating_phase_enum, dot, &dot);
3203 /* Update dot in the region ?
3204 We only do this if the section is going to be allocated,
3205 since unallocated sections do not contribute to the region's
3206 overall size in memory.
3208 If the SEC_NEVER_LOAD bit is not set, it will affect the
3209 addresses of sections after it. We have to update
3210 dot. */
3211 if (os->region != NULL
3212 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3213 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3215 os->region->current = dot;
3217 if (check_regions)
3218 /* Make sure the new address is within the region. */
3219 os_region_check (os, os->region, os->addr_tree,
3220 os->bfd_section->vma);
3222 /* If there's no load address specified, use the run
3223 region as the load region. */
3224 if (os->lma_region == NULL && os->load_base == NULL)
3225 os->lma_region = os->region;
3227 if (os->lma_region != NULL && os->lma_region != os->region)
3229 /* Set load_base, which will be handled later. */
3230 os->load_base = exp_intop (os->lma_region->current);
3231 os->lma_region->current +=
3232 TO_ADDR (os->bfd_section->size);
3233 if (check_regions)
3234 os_region_check (os, os->lma_region, NULL,
3235 os->bfd_section->lma);
3239 break;
3241 case lang_constructors_statement_enum:
3242 dot = lang_size_sections_1 (constructor_list.head,
3243 output_section_statement,
3244 &s->wild_statement.children.head,
3245 fill, dot, relax, check_regions);
3246 break;
3248 case lang_data_statement_enum:
3250 unsigned int size = 0;
3252 s->data_statement.output_vma =
3253 dot - output_section_statement->bfd_section->vma;
3254 s->data_statement.output_section =
3255 output_section_statement->bfd_section;
3257 /* We might refer to provided symbols in the expression, and
3258 need to mark them as needed. */
3259 exp_fold_tree (s->data_statement.exp, abs_output_section,
3260 lang_allocating_phase_enum, dot, &dot);
3262 switch (s->data_statement.type)
3264 default:
3265 abort ();
3266 case QUAD:
3267 case SQUAD:
3268 size = QUAD_SIZE;
3269 break;
3270 case LONG:
3271 size = LONG_SIZE;
3272 break;
3273 case SHORT:
3274 size = SHORT_SIZE;
3275 break;
3276 case BYTE:
3277 size = BYTE_SIZE;
3278 break;
3280 if (size < TO_SIZE ((unsigned) 1))
3281 size = TO_SIZE ((unsigned) 1);
3282 dot += TO_ADDR (size);
3283 output_section_statement->bfd_section->size += size;
3284 /* The output section gets contents, and then we inspect for
3285 any flags set in the input script which override any ALLOC. */
3286 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3287 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3289 output_section_statement->bfd_section->flags |=
3290 SEC_ALLOC | SEC_LOAD;
3293 break;
3295 case lang_reloc_statement_enum:
3297 int size;
3299 s->reloc_statement.output_vma =
3300 dot - output_section_statement->bfd_section->vma;
3301 s->reloc_statement.output_section =
3302 output_section_statement->bfd_section;
3303 size = bfd_get_reloc_size (s->reloc_statement.howto);
3304 dot += TO_ADDR (size);
3305 output_section_statement->bfd_section->size += size;
3307 break;
3309 case lang_wild_statement_enum:
3311 dot = lang_size_sections_1 (s->wild_statement.children.head,
3312 output_section_statement,
3313 &s->wild_statement.children.head,
3314 fill, dot, relax, check_regions);
3316 break;
3318 case lang_object_symbols_statement_enum:
3319 link_info.create_object_symbols_section =
3320 output_section_statement->bfd_section;
3321 break;
3322 case lang_output_statement_enum:
3323 case lang_target_statement_enum:
3324 break;
3325 case lang_input_section_enum:
3327 asection *i;
3329 i = (*prev)->input_section.section;
3330 if (relax)
3332 bfd_boolean again;
3334 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3335 einfo (_("%P%F: can't relax section: %E\n"));
3336 if (again)
3337 *relax = TRUE;
3339 dot = size_input_section (prev, output_section_statement,
3340 output_section_statement->fill, dot);
3342 break;
3343 case lang_input_statement_enum:
3344 break;
3345 case lang_fill_statement_enum:
3346 s->fill_statement.output_section =
3347 output_section_statement->bfd_section;
3349 fill = s->fill_statement.fill;
3350 break;
3351 case lang_assignment_statement_enum:
3353 bfd_vma newdot = dot;
3355 exp_fold_tree (s->assignment_statement.exp,
3356 output_section_statement,
3357 lang_allocating_phase_enum,
3358 dot,
3359 &newdot);
3361 if (newdot != dot)
3363 if (output_section_statement == abs_output_section)
3365 /* If we don't have an output section, then just adjust
3366 the default memory address. */
3367 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3368 FALSE)->current = newdot;
3370 else
3372 /* Insert a pad after this statement. We can't
3373 put the pad before when relaxing, in case the
3374 assignment references dot. */
3375 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3376 output_section_statement->bfd_section, dot);
3378 /* Don't neuter the pad below when relaxing. */
3379 s = s->header.next;
3382 /* If dot is advanced, this implies that the section should
3383 have space allocated to it, unless the user has explicitly
3384 stated that the section should never be loaded. */
3385 if (!(output_section_statement->flags
3386 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3387 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3389 dot = newdot;
3392 break;
3394 case lang_padding_statement_enum:
3395 /* If this is the first time lang_size_sections is called,
3396 we won't have any padding statements. If this is the
3397 second or later passes when relaxing, we should allow
3398 padding to shrink. If padding is needed on this pass, it
3399 will be added back in. */
3400 s->padding_statement.size = 0;
3402 /* Make sure output_offset is valid. If relaxation shrinks
3403 the section and this pad isn't needed, it's possible to
3404 have output_offset larger than the final size of the
3405 section. bfd_set_section_contents will complain even for
3406 a pad size of zero. */
3407 s->padding_statement.output_offset
3408 = dot - output_section_statement->bfd_section->vma;
3409 break;
3411 case lang_group_statement_enum:
3412 dot = lang_size_sections_1 (s->group_statement.children.head,
3413 output_section_statement,
3414 &s->group_statement.children.head,
3415 fill, dot, relax, check_regions);
3416 break;
3418 default:
3419 FAIL ();
3420 break;
3422 /* We can only get here when relaxing is turned on. */
3423 case lang_address_statement_enum:
3424 break;
3426 prev = &s->header.next;
3428 return dot;
3431 bfd_vma
3432 lang_size_sections
3433 (lang_statement_union_type *s,
3434 lang_output_section_statement_type *output_section_statement,
3435 lang_statement_union_type **prev,
3436 fill_type *fill,
3437 bfd_vma dot,
3438 bfd_boolean *relax,
3439 bfd_boolean check_regions)
3441 bfd_vma result;
3443 /* Callers of exp_fold_tree need to increment this. */
3444 lang_statement_iteration++;
3446 exp_data_seg.phase = exp_dataseg_none;
3447 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3448 dot, relax, check_regions);
3449 if (exp_data_seg.phase == exp_dataseg_end_seen
3450 && link_info.relro && exp_data_seg.relro_end)
3452 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3453 to put exp_data_seg.relro on a (common) page boundary. */
3455 exp_data_seg.phase = exp_dataseg_relro_adjust;
3456 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3457 dot, relax, check_regions);
3458 link_info.relro_start = exp_data_seg.base;
3459 link_info.relro_end = exp_data_seg.relro_end;
3461 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3463 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3464 a page could be saved in the data segment. */
3465 bfd_vma first, last;
3467 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3468 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3469 if (first && last
3470 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3471 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3472 && first + last <= exp_data_seg.pagesize)
3474 exp_data_seg.phase = exp_dataseg_adjust;
3475 lang_statement_iteration++;
3476 result = lang_size_sections_1 (s, output_section_statement, prev,
3477 fill, dot, relax, check_regions);
3481 return result;
3484 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3486 static bfd_vma
3487 lang_do_assignments_1
3488 (lang_statement_union_type *s,
3489 lang_output_section_statement_type *output_section_statement,
3490 fill_type *fill,
3491 bfd_vma dot)
3493 for (; s != NULL; s = s->header.next)
3495 switch (s->header.type)
3497 case lang_constructors_statement_enum:
3498 dot = lang_do_assignments_1 (constructor_list.head,
3499 output_section_statement,
3500 fill,
3501 dot);
3502 break;
3504 case lang_output_section_statement_enum:
3506 lang_output_section_statement_type *os;
3508 os = &(s->output_section_statement);
3509 if (os->bfd_section != NULL)
3511 dot = os->bfd_section->vma;
3512 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3513 /* .tbss sections effectively have zero size. */
3514 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3515 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3516 || link_info.relocatable)
3517 dot += TO_ADDR (os->bfd_section->size);
3519 if (os->load_base)
3521 /* If nothing has been placed into the output section then
3522 it won't have a bfd_section. */
3523 if (os->bfd_section)
3525 os->bfd_section->lma
3526 = exp_get_abs_int (os->load_base, 0, "load base",
3527 lang_final_phase_enum);
3531 break;
3532 case lang_wild_statement_enum:
3534 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3535 output_section_statement,
3536 fill, dot);
3538 break;
3540 case lang_object_symbols_statement_enum:
3541 case lang_output_statement_enum:
3542 case lang_target_statement_enum:
3543 #if 0
3544 case lang_common_statement_enum:
3545 #endif
3546 break;
3547 case lang_data_statement_enum:
3549 etree_value_type value;
3551 value = exp_fold_tree (s->data_statement.exp,
3552 abs_output_section,
3553 lang_final_phase_enum, dot, &dot);
3554 if (!value.valid_p)
3555 einfo (_("%F%P: invalid data statement\n"));
3556 s->data_statement.value
3557 = value.value + value.section->bfd_section->vma;
3560 unsigned int size;
3561 switch (s->data_statement.type)
3563 default:
3564 abort ();
3565 case QUAD:
3566 case SQUAD:
3567 size = QUAD_SIZE;
3568 break;
3569 case LONG:
3570 size = LONG_SIZE;
3571 break;
3572 case SHORT:
3573 size = SHORT_SIZE;
3574 break;
3575 case BYTE:
3576 size = BYTE_SIZE;
3577 break;
3579 if (size < TO_SIZE ((unsigned) 1))
3580 size = TO_SIZE ((unsigned) 1);
3581 dot += TO_ADDR (size);
3583 break;
3585 case lang_reloc_statement_enum:
3587 etree_value_type value;
3589 value = exp_fold_tree (s->reloc_statement.addend_exp,
3590 abs_output_section,
3591 lang_final_phase_enum, dot, &dot);
3592 s->reloc_statement.addend_value = value.value;
3593 if (!value.valid_p)
3594 einfo (_("%F%P: invalid reloc statement\n"));
3596 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3597 break;
3599 case lang_input_section_enum:
3601 asection *in = s->input_section.section;
3603 if ((in->flags & SEC_EXCLUDE) == 0)
3604 dot += TO_ADDR (in->size);
3606 break;
3608 case lang_input_statement_enum:
3609 break;
3610 case lang_fill_statement_enum:
3611 fill = s->fill_statement.fill;
3612 break;
3613 case lang_assignment_statement_enum:
3615 exp_fold_tree (s->assignment_statement.exp,
3616 output_section_statement,
3617 lang_final_phase_enum,
3618 dot,
3619 &dot);
3622 break;
3623 case lang_padding_statement_enum:
3624 dot += TO_ADDR (s->padding_statement.size);
3625 break;
3627 case lang_group_statement_enum:
3628 dot = lang_do_assignments_1 (s->group_statement.children.head,
3629 output_section_statement,
3630 fill, dot);
3632 break;
3634 default:
3635 FAIL ();
3636 break;
3637 case lang_address_statement_enum:
3638 break;
3642 return dot;
3645 void
3646 lang_do_assignments
3647 (lang_statement_union_type *s,
3648 lang_output_section_statement_type *output_section_statement,
3649 fill_type *fill,
3650 bfd_vma dot)
3652 /* Callers of exp_fold_tree need to increment this. */
3653 lang_statement_iteration++;
3654 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3657 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3658 operator .startof. (section_name), it produces an undefined symbol
3659 .startof.section_name. Similarly, when it sees
3660 .sizeof. (section_name), it produces an undefined symbol
3661 .sizeof.section_name. For all the output sections, we look for
3662 such symbols, and set them to the correct value. */
3664 static void
3665 lang_set_startof (void)
3667 asection *s;
3669 if (link_info.relocatable)
3670 return;
3672 for (s = output_bfd->sections; s != NULL; s = s->next)
3674 const char *secname;
3675 char *buf;
3676 struct bfd_link_hash_entry *h;
3678 secname = bfd_get_section_name (output_bfd, s);
3679 buf = xmalloc (10 + strlen (secname));
3681 sprintf (buf, ".startof.%s", secname);
3682 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3683 if (h != NULL && h->type == bfd_link_hash_undefined)
3685 h->type = bfd_link_hash_defined;
3686 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3687 h->u.def.section = bfd_abs_section_ptr;
3690 sprintf (buf, ".sizeof.%s", secname);
3691 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3692 if (h != NULL && h->type == bfd_link_hash_undefined)
3694 h->type = bfd_link_hash_defined;
3695 h->u.def.value = TO_ADDR (s->size);
3696 h->u.def.section = bfd_abs_section_ptr;
3699 free (buf);
3703 static void
3704 lang_finish (void)
3706 struct bfd_link_hash_entry *h;
3707 bfd_boolean warn;
3709 if (link_info.relocatable || link_info.shared)
3710 warn = FALSE;
3711 else
3712 warn = TRUE;
3714 if (entry_symbol.name == NULL)
3716 /* No entry has been specified. Look for start, but don't warn
3717 if we don't find it. */
3718 entry_symbol.name = "start";
3719 warn = FALSE;
3722 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3723 FALSE, FALSE, TRUE);
3724 if (h != NULL
3725 && (h->type == bfd_link_hash_defined
3726 || h->type == bfd_link_hash_defweak)
3727 && h->u.def.section->output_section != NULL)
3729 bfd_vma val;
3731 val = (h->u.def.value
3732 + bfd_get_section_vma (output_bfd,
3733 h->u.def.section->output_section)
3734 + h->u.def.section->output_offset);
3735 if (! bfd_set_start_address (output_bfd, val))
3736 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3738 else
3740 bfd_vma val;
3741 const char *send;
3743 /* We couldn't find the entry symbol. Try parsing it as a
3744 number. */
3745 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3746 if (*send == '\0')
3748 if (! bfd_set_start_address (output_bfd, val))
3749 einfo (_("%P%F: can't set start address\n"));
3751 else
3753 asection *ts;
3755 /* Can't find the entry symbol, and it's not a number. Use
3756 the first address in the text section. */
3757 ts = bfd_get_section_by_name (output_bfd, entry_section);
3758 if (ts != NULL)
3760 if (warn)
3761 einfo (_("%P: warning: cannot find entry symbol %s;"
3762 " defaulting to %V\n"),
3763 entry_symbol.name,
3764 bfd_get_section_vma (output_bfd, ts));
3765 if (! bfd_set_start_address (output_bfd,
3766 bfd_get_section_vma (output_bfd,
3767 ts)))
3768 einfo (_("%P%F: can't set start address\n"));
3770 else
3772 if (warn)
3773 einfo (_("%P: warning: cannot find entry symbol %s;"
3774 " not setting start address\n"),
3775 entry_symbol.name);
3780 /* Don't bfd_hash_table_free (&lang_definedness_table);
3781 map file output may result in a call of lang_track_definedness. */
3784 /* This is a small function used when we want to ignore errors from
3785 BFD. */
3787 static void
3788 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3790 /* Don't do anything. */
3793 /* Check that the architecture of all the input files is compatible
3794 with the output file. Also call the backend to let it do any
3795 other checking that is needed. */
3797 static void
3798 lang_check (void)
3800 lang_statement_union_type *file;
3801 bfd *input_bfd;
3802 const bfd_arch_info_type *compatible;
3804 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3806 input_bfd = file->input_statement.the_bfd;
3807 compatible
3808 = bfd_arch_get_compatible (input_bfd, output_bfd,
3809 command_line.accept_unknown_input_arch);
3811 /* In general it is not possible to perform a relocatable
3812 link between differing object formats when the input
3813 file has relocations, because the relocations in the
3814 input format may not have equivalent representations in
3815 the output format (and besides BFD does not translate
3816 relocs for other link purposes than a final link). */
3817 if ((link_info.relocatable || link_info.emitrelocations)
3818 && (compatible == NULL
3819 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3820 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3822 einfo (_("%P%F: Relocatable linking with relocations from"
3823 " format %s (%B) to format %s (%B) is not supported\n"),
3824 bfd_get_target (input_bfd), input_bfd,
3825 bfd_get_target (output_bfd), output_bfd);
3826 /* einfo with %F exits. */
3829 if (compatible == NULL)
3831 if (command_line.warn_mismatch)
3832 einfo (_("%P: warning: %s architecture of input file `%B'"
3833 " is incompatible with %s output\n"),
3834 bfd_printable_name (input_bfd), input_bfd,
3835 bfd_printable_name (output_bfd));
3837 else if (bfd_count_sections (input_bfd))
3839 /* If the input bfd has no contents, it shouldn't set the
3840 private data of the output bfd. */
3842 bfd_error_handler_type pfn = NULL;
3844 /* If we aren't supposed to warn about mismatched input
3845 files, temporarily set the BFD error handler to a
3846 function which will do nothing. We still want to call
3847 bfd_merge_private_bfd_data, since it may set up
3848 information which is needed in the output file. */
3849 if (! command_line.warn_mismatch)
3850 pfn = bfd_set_error_handler (ignore_bfd_errors);
3851 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3853 if (command_line.warn_mismatch)
3854 einfo (_("%P%X: failed to merge target specific data"
3855 " of file %B\n"), input_bfd);
3857 if (! command_line.warn_mismatch)
3858 bfd_set_error_handler (pfn);
3863 /* Look through all the global common symbols and attach them to the
3864 correct section. The -sort-common command line switch may be used
3865 to roughly sort the entries by size. */
3867 static void
3868 lang_common (void)
3870 if (command_line.inhibit_common_definition)
3871 return;
3872 if (link_info.relocatable
3873 && ! command_line.force_common_definition)
3874 return;
3876 if (! config.sort_common)
3877 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3878 else
3880 int power;
3882 for (power = 4; power >= 0; power--)
3883 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3887 /* Place one common symbol in the correct section. */
3889 static bfd_boolean
3890 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3892 unsigned int power_of_two;
3893 bfd_vma size;
3894 asection *section;
3896 if (h->type != bfd_link_hash_common)
3897 return TRUE;
3899 size = h->u.c.size;
3900 power_of_two = h->u.c.p->alignment_power;
3902 if (config.sort_common
3903 && power_of_two < (unsigned int) *(int *) info)
3904 return TRUE;
3906 section = h->u.c.p->section;
3908 /* Increase the size of the section to align the common sym. */
3909 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3910 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3912 /* Adjust the alignment if necessary. */
3913 if (power_of_two > section->alignment_power)
3914 section->alignment_power = power_of_two;
3916 /* Change the symbol from common to defined. */
3917 h->type = bfd_link_hash_defined;
3918 h->u.def.section = section;
3919 h->u.def.value = section->size;
3921 /* Increase the size of the section. */
3922 section->size += size;
3924 /* Make sure the section is allocated in memory, and make sure that
3925 it is no longer a common section. */
3926 section->flags |= SEC_ALLOC;
3927 section->flags &= ~SEC_IS_COMMON;
3929 if (config.map_file != NULL)
3931 static bfd_boolean header_printed;
3932 int len;
3933 char *name;
3934 char buf[50];
3936 if (! header_printed)
3938 minfo (_("\nAllocating common symbols\n"));
3939 minfo (_("Common symbol size file\n\n"));
3940 header_printed = TRUE;
3943 name = demangle (h->root.string);
3944 minfo ("%s", name);
3945 len = strlen (name);
3946 free (name);
3948 if (len >= 19)
3950 print_nl ();
3951 len = 0;
3953 while (len < 20)
3955 print_space ();
3956 ++len;
3959 minfo ("0x");
3960 if (size <= 0xffffffff)
3961 sprintf (buf, "%lx", (unsigned long) size);
3962 else
3963 sprintf_vma (buf, size);
3964 minfo ("%s", buf);
3965 len = strlen (buf);
3967 while (len < 16)
3969 print_space ();
3970 ++len;
3973 minfo ("%B\n", section->owner);
3976 return TRUE;
3979 /* Run through the input files and ensure that every input section has
3980 somewhere to go. If one is found without a destination then create
3981 an input request and place it into the statement tree. */
3983 static void
3984 lang_place_orphans (void)
3986 LANG_FOR_EACH_INPUT_STATEMENT (file)
3988 asection *s;
3990 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3992 if (s->output_section == NULL)
3994 /* This section of the file is not attached, root
3995 around for a sensible place for it to go. */
3997 if (file->just_syms_flag)
3998 abort ();
4000 if ((s->flags & SEC_EXCLUDE) != 0)
4001 s->output_section = bfd_abs_section_ptr;
4002 else if (strcmp (s->name, "COMMON") == 0)
4004 /* This is a lonely common section which must have
4005 come from an archive. We attach to the section
4006 with the wildcard. */
4007 if (! link_info.relocatable
4008 || command_line.force_common_definition)
4010 if (default_common_section == NULL)
4012 #if 0
4013 /* This message happens when using the
4014 svr3.ifile linker script, so I have
4015 disabled it. */
4016 info_msg (_("%P: no [COMMON] command,"
4017 " defaulting to .bss\n"));
4018 #endif
4019 default_common_section =
4020 lang_output_section_statement_lookup (".bss");
4023 lang_add_section (&default_common_section->children, s,
4024 default_common_section, file);
4027 else if (ldemul_place_orphan (file, s))
4029 else
4031 lang_output_section_statement_type *os;
4033 os = lang_output_section_statement_lookup (s->name);
4034 lang_add_section (&os->children, s, os, file);
4041 void
4042 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
4044 flagword *ptr_flags;
4046 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
4047 while (*flags)
4049 switch (*flags)
4051 case 'A': case 'a':
4052 *ptr_flags |= SEC_ALLOC;
4053 break;
4055 case 'R': case 'r':
4056 *ptr_flags |= SEC_READONLY;
4057 break;
4059 case 'W': case 'w':
4060 *ptr_flags |= SEC_DATA;
4061 break;
4063 case 'X': case 'x':
4064 *ptr_flags |= SEC_CODE;
4065 break;
4067 case 'L': case 'l':
4068 case 'I': case 'i':
4069 *ptr_flags |= SEC_LOAD;
4070 break;
4072 default:
4073 einfo (_("%P%F: invalid syntax in flags\n"));
4074 break;
4076 flags++;
4080 /* Call a function on each input file. This function will be called
4081 on an archive, but not on the elements. */
4083 void
4084 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
4086 lang_input_statement_type *f;
4088 for (f = (lang_input_statement_type *) input_file_chain.head;
4089 f != NULL;
4090 f = (lang_input_statement_type *) f->next_real_file)
4091 func (f);
4094 /* Call a function on each file. The function will be called on all
4095 the elements of an archive which are included in the link, but will
4096 not be called on the archive file itself. */
4098 void
4099 lang_for_each_file (void (*func) (lang_input_statement_type *))
4101 LANG_FOR_EACH_INPUT_STATEMENT (f)
4103 func (f);
4107 void
4108 ldlang_add_file (lang_input_statement_type *entry)
4110 bfd **pp;
4112 lang_statement_append (&file_chain,
4113 (lang_statement_union_type *) entry,
4114 &entry->next);
4116 /* The BFD linker needs to have a list of all input BFDs involved in
4117 a link. */
4118 ASSERT (entry->the_bfd->link_next == NULL);
4119 ASSERT (entry->the_bfd != output_bfd);
4120 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
4122 *pp = entry->the_bfd;
4123 entry->the_bfd->usrdata = entry;
4124 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4126 /* Look through the sections and check for any which should not be
4127 included in the link. We need to do this now, so that we can
4128 notice when the backend linker tries to report multiple
4129 definition errors for symbols which are in sections we aren't
4130 going to link. FIXME: It might be better to entirely ignore
4131 symbols which are defined in sections which are going to be
4132 discarded. This would require modifying the backend linker for
4133 each backend which might set the SEC_LINK_ONCE flag. If we do
4134 this, we should probably handle SEC_EXCLUDE in the same way. */
4136 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4139 void
4140 lang_add_output (const char *name, int from_script)
4142 /* Make -o on command line override OUTPUT in script. */
4143 if (!had_output_filename || !from_script)
4145 output_filename = name;
4146 had_output_filename = TRUE;
4150 static lang_output_section_statement_type *current_section;
4152 static int
4153 topower (int x)
4155 unsigned int i = 1;
4156 int l;
4158 if (x < 0)
4159 return -1;
4161 for (l = 0; l < 32; l++)
4163 if (i >= (unsigned int) x)
4164 return l;
4165 i <<= 1;
4168 return 0;
4171 lang_output_section_statement_type *
4172 lang_enter_output_section_statement (const char *output_section_statement_name,
4173 etree_type *address_exp,
4174 enum section_type sectype,
4175 etree_type *align,
4176 etree_type *subalign,
4177 etree_type *ebase,
4178 int constraint)
4180 lang_output_section_statement_type *os;
4182 current_section =
4183 os =
4184 lang_output_section_statement_lookup_1 (output_section_statement_name,
4185 constraint);
4187 /* Add this statement to tree. */
4188 #if 0
4189 add_statement (lang_output_section_statement_enum,
4190 output_section_statement);
4191 #endif
4192 /* Make next things chain into subchain of this. */
4194 if (os->addr_tree == NULL)
4196 os->addr_tree = address_exp;
4198 os->sectype = sectype;
4199 if (sectype != noload_section)
4200 os->flags = SEC_NO_FLAGS;
4201 else
4202 os->flags = SEC_NEVER_LOAD;
4203 os->block_value = 1;
4204 stat_ptr = &os->children;
4206 os->subsection_alignment =
4207 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4208 os->section_alignment =
4209 topower (exp_get_value_int (align, -1, "section alignment", 0));
4211 os->load_base = ebase;
4212 return os;
4215 void
4216 lang_final (void)
4218 lang_output_statement_type *new =
4219 new_stat (lang_output_statement, stat_ptr);
4221 new->name = output_filename;
4224 /* Reset the current counters in the regions. */
4226 void
4227 lang_reset_memory_regions (void)
4229 lang_memory_region_type *p = lang_memory_region_list;
4230 asection *o;
4232 for (p = lang_memory_region_list; p != NULL; p = p->next)
4234 p->old_length = (bfd_size_type) (p->current - p->origin);
4235 p->current = p->origin;
4238 for (o = output_bfd->sections; o != NULL; o = o->next)
4240 /* Save the last size for possible use by bfd_relax_section. */
4241 o->rawsize = o->size;
4242 o->size = 0;
4246 /* Worker for lang_gc_sections_1. */
4248 static void
4249 gc_section_callback (lang_wild_statement_type *ptr,
4250 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4251 asection *section,
4252 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4253 void *data ATTRIBUTE_UNUSED)
4255 /* If the wild pattern was marked KEEP, the member sections
4256 should be as well. */
4257 if (ptr->keep_sections)
4258 section->flags |= SEC_KEEP;
4261 /* Iterate over sections marking them against GC. */
4263 static void
4264 lang_gc_sections_1 (lang_statement_union_type *s)
4266 for (; s != NULL; s = s->header.next)
4268 switch (s->header.type)
4270 case lang_wild_statement_enum:
4271 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4272 break;
4273 case lang_constructors_statement_enum:
4274 lang_gc_sections_1 (constructor_list.head);
4275 break;
4276 case lang_output_section_statement_enum:
4277 lang_gc_sections_1 (s->output_section_statement.children.head);
4278 break;
4279 case lang_group_statement_enum:
4280 lang_gc_sections_1 (s->group_statement.children.head);
4281 break;
4282 default:
4283 break;
4288 static void
4289 lang_gc_sections (void)
4291 struct bfd_link_hash_entry *h;
4292 ldlang_undef_chain_list_type *ulist;
4294 /* Keep all sections so marked in the link script. */
4296 lang_gc_sections_1 (statement_list.head);
4298 /* Keep all sections containing symbols undefined on the command-line,
4299 and the section containing the entry symbol. */
4301 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4303 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4304 FALSE, FALSE, FALSE);
4306 if (h != NULL
4307 && (h->type == bfd_link_hash_defined
4308 || h->type == bfd_link_hash_defweak)
4309 && ! bfd_is_abs_section (h->u.def.section))
4311 h->u.def.section->flags |= SEC_KEEP;
4315 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4316 the special case of debug info. (See bfd/stabs.c)
4317 Twiddle the flag here, to simplify later linker code. */
4318 if (link_info.relocatable)
4320 LANG_FOR_EACH_INPUT_STATEMENT (f)
4322 asection *sec;
4323 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4324 if ((sec->flags & SEC_DEBUGGING) == 0)
4325 sec->flags &= ~SEC_EXCLUDE;
4329 if (command_line.gc_sections)
4330 bfd_gc_sections (output_bfd, &link_info);
4333 void
4334 lang_process (void)
4336 lang_reasonable_defaults ();
4337 current_target = default_target;
4339 /* Open the output file. */
4340 lang_for_each_statement (ldlang_open_output);
4341 init_opb ();
4343 ldemul_create_output_section_statements ();
4345 /* Add to the hash table all undefineds on the command line. */
4346 lang_place_undefineds ();
4348 already_linked_table_init ();
4350 /* Create a bfd for each input file. */
4351 current_target = default_target;
4352 open_input_bfds (statement_list.head, FALSE);
4354 link_info.gc_sym_list = &entry_symbol;
4355 if (entry_symbol.name == NULL)
4356 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4358 ldemul_after_open ();
4360 already_linked_table_free ();
4362 /* Make sure that we're not mixing architectures. We call this
4363 after all the input files have been opened, but before we do any
4364 other processing, so that any operations merge_private_bfd_data
4365 does on the output file will be known during the rest of the
4366 link. */
4367 lang_check ();
4369 /* Handle .exports instead of a version script if we're told to do so. */
4370 if (command_line.version_exports_section)
4371 lang_do_version_exports_section ();
4373 /* Build all sets based on the information gathered from the input
4374 files. */
4375 ldctor_build_sets ();
4377 /* Remove unreferenced sections if asked to. */
4378 lang_gc_sections ();
4380 /* Size up the common data. */
4381 lang_common ();
4383 /* Run through the contours of the script and attach input sections
4384 to the correct output sections. */
4385 map_input_to_output_sections (statement_list.head, NULL, NULL);
4387 /* Find any sections not attached explicitly and handle them. */
4388 lang_place_orphans ();
4390 if (! link_info.relocatable)
4392 asection *found;
4394 /* Merge SEC_MERGE sections. This has to be done after GC of
4395 sections, so that GCed sections are not merged, but before
4396 assigning dynamic symbols, since removing whole input sections
4397 is hard then. */
4398 bfd_merge_sections (output_bfd, &link_info);
4400 /* Look for a text section and set the readonly attribute in it. */
4401 found = bfd_get_section_by_name (output_bfd, ".text");
4403 if (found != NULL)
4405 if (config.text_read_only)
4406 found->flags |= SEC_READONLY;
4407 else
4408 found->flags &= ~SEC_READONLY;
4412 /* Do anything special before sizing sections. This is where ELF
4413 and other back-ends size dynamic sections. */
4414 ldemul_before_allocation ();
4416 if (!link_info.relocatable)
4417 strip_excluded_output_sections ();
4419 /* We must record the program headers before we try to fix the
4420 section positions, since they will affect SIZEOF_HEADERS. */
4421 lang_record_phdrs ();
4423 /* Size up the sections. */
4424 lang_size_sections (statement_list.head, abs_output_section,
4425 &statement_list.head, 0, 0, NULL,
4426 command_line.relax ? FALSE : TRUE);
4428 /* Now run around and relax if we can. */
4429 if (command_line.relax)
4431 /* Keep relaxing until bfd_relax_section gives up. */
4432 bfd_boolean relax_again;
4436 relax_again = FALSE;
4438 /* Note: pe-dll.c does something like this also. If you find
4439 you need to change this code, you probably need to change
4440 pe-dll.c also. DJ */
4442 /* Do all the assignments with our current guesses as to
4443 section sizes. */
4444 lang_do_assignments (statement_list.head, abs_output_section,
4445 NULL, 0);
4447 /* We must do this after lang_do_assignments, because it uses
4448 size. */
4449 lang_reset_memory_regions ();
4451 /* Perform another relax pass - this time we know where the
4452 globals are, so can make a better guess. */
4453 lang_size_sections (statement_list.head, abs_output_section,
4454 &statement_list.head, 0, 0, &relax_again, FALSE);
4456 /* If the normal relax is done and the relax finalize pass
4457 is not performed yet, we perform another relax pass. */
4458 if (!relax_again && link_info.need_relax_finalize)
4460 link_info.need_relax_finalize = FALSE;
4461 relax_again = TRUE;
4464 while (relax_again);
4466 /* Final extra sizing to report errors. */
4467 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4468 lang_reset_memory_regions ();
4469 lang_size_sections (statement_list.head, abs_output_section,
4470 &statement_list.head, 0, 0, NULL, TRUE);
4473 /* See if anything special should be done now we know how big
4474 everything is. */
4475 ldemul_after_allocation ();
4477 /* Fix any .startof. or .sizeof. symbols. */
4478 lang_set_startof ();
4480 /* Do all the assignments, now that we know the final resting places
4481 of all the symbols. */
4483 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4485 /* Make sure that the section addresses make sense. */
4486 if (! link_info.relocatable
4487 && command_line.check_section_addresses)
4488 lang_check_section_addresses ();
4490 /* Final stuffs. */
4492 ldemul_finish ();
4493 lang_finish ();
4496 /* EXPORTED TO YACC */
4498 void
4499 lang_add_wild (struct wildcard_spec *filespec,
4500 struct wildcard_list *section_list,
4501 bfd_boolean keep_sections)
4503 struct wildcard_list *curr, *next;
4504 lang_wild_statement_type *new;
4506 /* Reverse the list as the parser puts it back to front. */
4507 for (curr = section_list, section_list = NULL;
4508 curr != NULL;
4509 section_list = curr, curr = next)
4511 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4512 placed_commons = TRUE;
4514 next = curr->next;
4515 curr->next = section_list;
4518 if (filespec != NULL && filespec->name != NULL)
4520 if (strcmp (filespec->name, "*") == 0)
4521 filespec->name = NULL;
4522 else if (! wildcardp (filespec->name))
4523 lang_has_input_file = TRUE;
4526 new = new_stat (lang_wild_statement, stat_ptr);
4527 new->filename = NULL;
4528 new->filenames_sorted = FALSE;
4529 if (filespec != NULL)
4531 new->filename = filespec->name;
4532 new->filenames_sorted = filespec->sorted;
4534 new->section_list = section_list;
4535 new->keep_sections = keep_sections;
4536 lang_list_init (&new->children);
4539 void
4540 lang_section_start (const char *name, etree_type *address)
4542 lang_address_statement_type *ad;
4544 ad = new_stat (lang_address_statement, stat_ptr);
4545 ad->section_name = name;
4546 ad->address = address;
4549 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4550 because of a -e argument on the command line, or zero if this is
4551 called by ENTRY in a linker script. Command line arguments take
4552 precedence. */
4554 void
4555 lang_add_entry (const char *name, bfd_boolean cmdline)
4557 if (entry_symbol.name == NULL
4558 || cmdline
4559 || ! entry_from_cmdline)
4561 entry_symbol.name = name;
4562 entry_from_cmdline = cmdline;
4566 void
4567 lang_add_target (const char *name)
4569 lang_target_statement_type *new = new_stat (lang_target_statement,
4570 stat_ptr);
4572 new->target = name;
4576 void
4577 lang_add_map (const char *name)
4579 while (*name)
4581 switch (*name)
4583 case 'F':
4584 map_option_f = TRUE;
4585 break;
4587 name++;
4591 void
4592 lang_add_fill (fill_type *fill)
4594 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4595 stat_ptr);
4597 new->fill = fill;
4600 void
4601 lang_add_data (int type, union etree_union *exp)
4604 lang_data_statement_type *new = new_stat (lang_data_statement,
4605 stat_ptr);
4607 new->exp = exp;
4608 new->type = type;
4612 /* Create a new reloc statement. RELOC is the BFD relocation type to
4613 generate. HOWTO is the corresponding howto structure (we could
4614 look this up, but the caller has already done so). SECTION is the
4615 section to generate a reloc against, or NAME is the name of the
4616 symbol to generate a reloc against. Exactly one of SECTION and
4617 NAME must be NULL. ADDEND is an expression for the addend. */
4619 void
4620 lang_add_reloc (bfd_reloc_code_real_type reloc,
4621 reloc_howto_type *howto,
4622 asection *section,
4623 const char *name,
4624 union etree_union *addend)
4626 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4628 p->reloc = reloc;
4629 p->howto = howto;
4630 p->section = section;
4631 p->name = name;
4632 p->addend_exp = addend;
4634 p->addend_value = 0;
4635 p->output_section = NULL;
4636 p->output_vma = 0;
4639 lang_assignment_statement_type *
4640 lang_add_assignment (etree_type *exp)
4642 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4643 stat_ptr);
4645 new->exp = exp;
4646 return new;
4649 void
4650 lang_add_attribute (enum statement_enum attribute)
4652 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4655 void
4656 lang_startup (const char *name)
4658 if (startup_file != NULL)
4660 einfo (_("%P%Fmultiple STARTUP files\n"));
4662 first_file->filename = name;
4663 first_file->local_sym_name = name;
4664 first_file->real = TRUE;
4666 startup_file = name;
4669 void
4670 lang_float (bfd_boolean maybe)
4672 lang_float_flag = maybe;
4676 /* Work out the load- and run-time regions from a script statement, and
4677 store them in *LMA_REGION and *REGION respectively.
4679 MEMSPEC is the name of the run-time region, or the value of
4680 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4681 LMA_MEMSPEC is the name of the load-time region, or null if the
4682 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4683 had an explicit load address.
4685 It is an error to specify both a load region and a load address. */
4687 static void
4688 lang_get_regions (lang_memory_region_type **region,
4689 lang_memory_region_type **lma_region,
4690 const char *memspec,
4691 const char *lma_memspec,
4692 bfd_boolean have_lma,
4693 bfd_boolean have_vma)
4695 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4697 /* If no runtime region or VMA has been specified, but the load region
4698 has been specified, then use the load region for the runtime region
4699 as well. */
4700 if (lma_memspec != NULL
4701 && ! have_vma
4702 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4703 *region = *lma_region;
4704 else
4705 *region = lang_memory_region_lookup (memspec, FALSE);
4707 if (have_lma && lma_memspec != 0)
4708 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4711 void
4712 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4713 lang_output_section_phdr_list *phdrs,
4714 const char *lma_memspec)
4716 lang_get_regions (&current_section->region,
4717 &current_section->lma_region,
4718 memspec, lma_memspec,
4719 current_section->load_base != NULL,
4720 current_section->addr_tree != NULL);
4721 current_section->fill = fill;
4722 current_section->phdrs = phdrs;
4723 stat_ptr = &statement_list;
4726 /* Create an absolute symbol with the given name with the value of the
4727 address of first byte of the section named.
4729 If the symbol already exists, then do nothing. */
4731 void
4732 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4734 struct bfd_link_hash_entry *h;
4736 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4737 if (h == NULL)
4738 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4740 if (h->type == bfd_link_hash_new
4741 || h->type == bfd_link_hash_undefined)
4743 asection *sec;
4745 h->type = bfd_link_hash_defined;
4747 sec = bfd_get_section_by_name (output_bfd, secname);
4748 if (sec == NULL)
4749 h->u.def.value = 0;
4750 else
4751 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4753 h->u.def.section = bfd_abs_section_ptr;
4757 /* Create an absolute symbol with the given name with the value of the
4758 address of the first byte after the end of the section named.
4760 If the symbol already exists, then do nothing. */
4762 void
4763 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4765 struct bfd_link_hash_entry *h;
4767 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4768 if (h == NULL)
4769 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4771 if (h->type == bfd_link_hash_new
4772 || h->type == bfd_link_hash_undefined)
4774 asection *sec;
4776 h->type = bfd_link_hash_defined;
4778 sec = bfd_get_section_by_name (output_bfd, secname);
4779 if (sec == NULL)
4780 h->u.def.value = 0;
4781 else
4782 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4783 + TO_ADDR (sec->size));
4785 h->u.def.section = bfd_abs_section_ptr;
4789 void
4790 lang_statement_append (lang_statement_list_type *list,
4791 lang_statement_union_type *element,
4792 lang_statement_union_type **field)
4794 *(list->tail) = element;
4795 list->tail = field;
4798 /* Set the output format type. -oformat overrides scripts. */
4800 void
4801 lang_add_output_format (const char *format,
4802 const char *big,
4803 const char *little,
4804 int from_script)
4806 if (output_target == NULL || !from_script)
4808 if (command_line.endian == ENDIAN_BIG
4809 && big != NULL)
4810 format = big;
4811 else if (command_line.endian == ENDIAN_LITTLE
4812 && little != NULL)
4813 format = little;
4815 output_target = format;
4819 /* Enter a group. This creates a new lang_group_statement, and sets
4820 stat_ptr to build new statements within the group. */
4822 void
4823 lang_enter_group (void)
4825 lang_group_statement_type *g;
4827 g = new_stat (lang_group_statement, stat_ptr);
4828 lang_list_init (&g->children);
4829 stat_ptr = &g->children;
4832 /* Leave a group. This just resets stat_ptr to start writing to the
4833 regular list of statements again. Note that this will not work if
4834 groups can occur inside anything else which can adjust stat_ptr,
4835 but currently they can't. */
4837 void
4838 lang_leave_group (void)
4840 stat_ptr = &statement_list;
4843 /* Add a new program header. This is called for each entry in a PHDRS
4844 command in a linker script. */
4846 void
4847 lang_new_phdr (const char *name,
4848 etree_type *type,
4849 bfd_boolean filehdr,
4850 bfd_boolean phdrs,
4851 etree_type *at,
4852 etree_type *flags)
4854 struct lang_phdr *n, **pp;
4856 n = stat_alloc (sizeof (struct lang_phdr));
4857 n->next = NULL;
4858 n->name = name;
4859 n->type = exp_get_value_int (type, 0, "program header type",
4860 lang_final_phase_enum);
4861 n->filehdr = filehdr;
4862 n->phdrs = phdrs;
4863 n->at = at;
4864 n->flags = flags;
4866 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4868 *pp = n;
4871 /* Record the program header information in the output BFD. FIXME: We
4872 should not be calling an ELF specific function here. */
4874 static void
4875 lang_record_phdrs (void)
4877 unsigned int alc;
4878 asection **secs;
4879 lang_output_section_phdr_list *last;
4880 struct lang_phdr *l;
4881 lang_statement_union_type *u;
4883 alc = 10;
4884 secs = xmalloc (alc * sizeof (asection *));
4885 last = NULL;
4886 for (l = lang_phdr_list; l != NULL; l = l->next)
4888 unsigned int c;
4889 flagword flags;
4890 bfd_vma at;
4892 c = 0;
4893 for (u = lang_output_section_statement.head;
4894 u != NULL;
4895 u = u->output_section_statement.next)
4897 lang_output_section_statement_type *os;
4898 lang_output_section_phdr_list *pl;
4900 os = &u->output_section_statement;
4901 if (os->constraint == -1)
4902 continue;
4904 pl = os->phdrs;
4905 if (pl != NULL)
4906 last = pl;
4907 else
4909 if (os->sectype == noload_section
4910 || os->bfd_section == NULL
4911 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4912 continue;
4913 pl = last;
4916 if (os->bfd_section == NULL)
4917 continue;
4919 for (; pl != NULL; pl = pl->next)
4921 if (strcmp (pl->name, l->name) == 0)
4923 if (c >= alc)
4925 alc *= 2;
4926 secs = xrealloc (secs, alc * sizeof (asection *));
4928 secs[c] = os->bfd_section;
4929 ++c;
4930 pl->used = TRUE;
4935 if (l->flags == NULL)
4936 flags = 0;
4937 else
4938 flags = exp_get_vma (l->flags, 0, "phdr flags",
4939 lang_final_phase_enum);
4941 if (l->at == NULL)
4942 at = 0;
4943 else
4944 at = exp_get_vma (l->at, 0, "phdr load address",
4945 lang_final_phase_enum);
4947 if (! bfd_record_phdr (output_bfd, l->type,
4948 l->flags != NULL, flags, l->at != NULL,
4949 at, l->filehdr, l->phdrs, c, secs))
4950 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4953 free (secs);
4955 /* Make sure all the phdr assignments succeeded. */
4956 for (u = lang_output_section_statement.head;
4957 u != NULL;
4958 u = u->output_section_statement.next)
4960 lang_output_section_phdr_list *pl;
4962 if (u->output_section_statement.constraint == -1
4963 || u->output_section_statement.bfd_section == NULL)
4964 continue;
4966 for (pl = u->output_section_statement.phdrs;
4967 pl != NULL;
4968 pl = pl->next)
4969 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4970 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4971 u->output_section_statement.name, pl->name);
4975 /* Record a list of sections which may not be cross referenced. */
4977 void
4978 lang_add_nocrossref (lang_nocrossref_type *l)
4980 struct lang_nocrossrefs *n;
4982 n = xmalloc (sizeof *n);
4983 n->next = nocrossref_list;
4984 n->list = l;
4985 nocrossref_list = n;
4987 /* Set notice_all so that we get informed about all symbols. */
4988 link_info.notice_all = TRUE;
4991 /* Overlay handling. We handle overlays with some static variables. */
4993 /* The overlay virtual address. */
4994 static etree_type *overlay_vma;
4995 /* And subsection alignment. */
4996 static etree_type *overlay_subalign;
4998 /* An expression for the maximum section size seen so far. */
4999 static etree_type *overlay_max;
5001 /* A list of all the sections in this overlay. */
5003 struct overlay_list {
5004 struct overlay_list *next;
5005 lang_output_section_statement_type *os;
5008 static struct overlay_list *overlay_list;
5010 /* Start handling an overlay. */
5012 void
5013 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
5015 /* The grammar should prevent nested overlays from occurring. */
5016 ASSERT (overlay_vma == NULL
5017 && overlay_subalign == NULL
5018 && overlay_max == NULL);
5020 overlay_vma = vma_expr;
5021 overlay_subalign = subalign;
5024 /* Start a section in an overlay. We handle this by calling
5025 lang_enter_output_section_statement with the correct VMA.
5026 lang_leave_overlay sets up the LMA and memory regions. */
5028 void
5029 lang_enter_overlay_section (const char *name)
5031 struct overlay_list *n;
5032 etree_type *size;
5034 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5035 0, overlay_subalign, 0, 0);
5037 /* If this is the first section, then base the VMA of future
5038 sections on this one. This will work correctly even if `.' is
5039 used in the addresses. */
5040 if (overlay_list == NULL)
5041 overlay_vma = exp_nameop (ADDR, name);
5043 /* Remember the section. */
5044 n = xmalloc (sizeof *n);
5045 n->os = current_section;
5046 n->next = overlay_list;
5047 overlay_list = n;
5049 size = exp_nameop (SIZEOF, name);
5051 /* Arrange to work out the maximum section end address. */
5052 if (overlay_max == NULL)
5053 overlay_max = size;
5054 else
5055 overlay_max = exp_binop (MAX_K, overlay_max, size);
5058 /* Finish a section in an overlay. There isn't any special to do
5059 here. */
5061 void
5062 lang_leave_overlay_section (fill_type *fill,
5063 lang_output_section_phdr_list *phdrs)
5065 const char *name;
5066 char *clean, *s2;
5067 const char *s1;
5068 char *buf;
5070 name = current_section->name;
5072 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
5073 region and that no load-time region has been specified. It doesn't
5074 really matter what we say here, since lang_leave_overlay will
5075 override it. */
5076 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
5078 /* Define the magic symbols. */
5080 clean = xmalloc (strlen (name) + 1);
5081 s2 = clean;
5082 for (s1 = name; *s1 != '\0'; s1++)
5083 if (ISALNUM (*s1) || *s1 == '_')
5084 *s2++ = *s1;
5085 *s2 = '\0';
5087 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5088 sprintf (buf, "__load_start_%s", clean);
5089 lang_add_assignment (exp_assop ('=', buf,
5090 exp_nameop (LOADADDR, name)));
5092 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5093 sprintf (buf, "__load_stop_%s", clean);
5094 lang_add_assignment (exp_assop ('=', buf,
5095 exp_binop ('+',
5096 exp_nameop (LOADADDR, name),
5097 exp_nameop (SIZEOF, name))));
5099 free (clean);
5102 /* Finish an overlay. If there are any overlay wide settings, this
5103 looks through all the sections in the overlay and sets them. */
5105 void
5106 lang_leave_overlay (etree_type *lma_expr,
5107 int nocrossrefs,
5108 fill_type *fill,
5109 const char *memspec,
5110 lang_output_section_phdr_list *phdrs,
5111 const char *lma_memspec)
5113 lang_memory_region_type *region;
5114 lang_memory_region_type *lma_region;
5115 struct overlay_list *l;
5116 lang_nocrossref_type *nocrossref;
5118 lang_get_regions (&region, &lma_region,
5119 memspec, lma_memspec,
5120 lma_expr != NULL, FALSE);
5122 nocrossref = NULL;
5124 /* After setting the size of the last section, set '.' to end of the
5125 overlay region. */
5126 if (overlay_list != NULL)
5127 overlay_list->os->update_dot_tree
5128 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5130 l = overlay_list;
5131 while (l != NULL)
5133 struct overlay_list *next;
5135 if (fill != NULL && l->os->fill == NULL)
5136 l->os->fill = fill;
5138 l->os->region = region;
5139 l->os->lma_region = lma_region;
5141 /* The first section has the load address specified in the
5142 OVERLAY statement. The rest are worked out from that.
5143 The base address is not needed (and should be null) if
5144 an LMA region was specified. */
5145 if (l->next == 0)
5146 l->os->load_base = lma_expr;
5147 else if (lma_region == 0)
5148 l->os->load_base = exp_binop ('+',
5149 exp_nameop (LOADADDR, l->next->os->name),
5150 exp_nameop (SIZEOF, l->next->os->name));
5152 if (phdrs != NULL && l->os->phdrs == NULL)
5153 l->os->phdrs = phdrs;
5155 if (nocrossrefs)
5157 lang_nocrossref_type *nc;
5159 nc = xmalloc (sizeof *nc);
5160 nc->name = l->os->name;
5161 nc->next = nocrossref;
5162 nocrossref = nc;
5165 next = l->next;
5166 free (l);
5167 l = next;
5170 if (nocrossref != NULL)
5171 lang_add_nocrossref (nocrossref);
5173 overlay_vma = NULL;
5174 overlay_list = NULL;
5175 overlay_max = NULL;
5178 /* Version handling. This is only useful for ELF. */
5180 /* This global variable holds the version tree that we build. */
5182 struct bfd_elf_version_tree *lang_elf_version_info;
5184 /* If PREV is NULL, return first version pattern matching particular symbol.
5185 If PREV is non-NULL, return first version pattern matching particular
5186 symbol after PREV (previously returned by lang_vers_match). */
5188 static struct bfd_elf_version_expr *
5189 lang_vers_match (struct bfd_elf_version_expr_head *head,
5190 struct bfd_elf_version_expr *prev,
5191 const char *sym)
5193 const char *cxx_sym = sym;
5194 const char *java_sym = sym;
5195 struct bfd_elf_version_expr *expr = NULL;
5197 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5199 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5200 if (!cxx_sym)
5201 cxx_sym = sym;
5203 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5205 java_sym = cplus_demangle (sym, DMGL_JAVA);
5206 if (!java_sym)
5207 java_sym = sym;
5210 if (head->htab && (prev == NULL || prev->symbol))
5212 struct bfd_elf_version_expr e;
5214 switch (prev ? prev->mask : 0)
5216 case 0:
5217 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5219 e.symbol = sym;
5220 expr = htab_find (head->htab, &e);
5221 while (expr && strcmp (expr->symbol, sym) == 0)
5222 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5223 goto out_ret;
5224 else
5225 expr = expr->next;
5227 /* Fallthrough */
5228 case BFD_ELF_VERSION_C_TYPE:
5229 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5231 e.symbol = cxx_sym;
5232 expr = htab_find (head->htab, &e);
5233 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5234 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5235 goto out_ret;
5236 else
5237 expr = expr->next;
5239 /* Fallthrough */
5240 case BFD_ELF_VERSION_CXX_TYPE:
5241 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5243 e.symbol = java_sym;
5244 expr = htab_find (head->htab, &e);
5245 while (expr && strcmp (expr->symbol, java_sym) == 0)
5246 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5247 goto out_ret;
5248 else
5249 expr = expr->next;
5251 /* Fallthrough */
5252 default:
5253 break;
5257 /* Finally, try the wildcards. */
5258 if (prev == NULL || prev->symbol)
5259 expr = head->remaining;
5260 else
5261 expr = prev->next;
5262 while (expr)
5264 const char *s;
5266 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5267 break;
5269 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5270 s = java_sym;
5271 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5272 s = cxx_sym;
5273 else
5274 s = sym;
5275 if (fnmatch (expr->pattern, s, 0) == 0)
5276 break;
5277 expr = expr->next;
5280 out_ret:
5281 if (cxx_sym != sym)
5282 free ((char *) cxx_sym);
5283 if (java_sym != sym)
5284 free ((char *) java_sym);
5285 return expr;
5288 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5289 return a string pointing to the symbol name. */
5291 static const char *
5292 realsymbol (const char *pattern)
5294 const char *p;
5295 bfd_boolean changed = FALSE, backslash = FALSE;
5296 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5298 for (p = pattern, s = symbol; *p != '\0'; ++p)
5300 /* It is a glob pattern only if there is no preceding
5301 backslash. */
5302 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5304 free (symbol);
5305 return NULL;
5308 if (backslash)
5310 /* Remove the preceding backslash. */
5311 *(s - 1) = *p;
5312 changed = TRUE;
5314 else
5315 *s++ = *p;
5317 backslash = *p == '\\';
5320 if (changed)
5322 *s = '\0';
5323 return symbol;
5325 else
5327 free (symbol);
5328 return pattern;
5332 /* This is called for each variable name or match expression. */
5334 struct bfd_elf_version_expr *
5335 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5336 const char *new,
5337 const char *lang)
5339 struct bfd_elf_version_expr *ret;
5341 ret = xmalloc (sizeof *ret);
5342 ret->next = orig;
5343 ret->pattern = new;
5344 ret->symver = 0;
5345 ret->script = 0;
5346 ret->symbol = realsymbol (new);
5348 if (lang == NULL || strcasecmp (lang, "C") == 0)
5349 ret->mask = BFD_ELF_VERSION_C_TYPE;
5350 else if (strcasecmp (lang, "C++") == 0)
5351 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5352 else if (strcasecmp (lang, "Java") == 0)
5353 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5354 else
5356 einfo (_("%X%P: unknown language `%s' in version information\n"),
5357 lang);
5358 ret->mask = BFD_ELF_VERSION_C_TYPE;
5361 return ldemul_new_vers_pattern (ret);
5364 /* This is called for each set of variable names and match
5365 expressions. */
5367 struct bfd_elf_version_tree *
5368 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5369 struct bfd_elf_version_expr *locals)
5371 struct bfd_elf_version_tree *ret;
5373 ret = xcalloc (1, sizeof *ret);
5374 ret->globals.list = globals;
5375 ret->locals.list = locals;
5376 ret->match = lang_vers_match;
5377 ret->name_indx = (unsigned int) -1;
5378 return ret;
5381 /* This static variable keeps track of version indices. */
5383 static int version_index;
5385 static hashval_t
5386 version_expr_head_hash (const void *p)
5388 const struct bfd_elf_version_expr *e = p;
5390 return htab_hash_string (e->symbol);
5393 static int
5394 version_expr_head_eq (const void *p1, const void *p2)
5396 const struct bfd_elf_version_expr *e1 = p1;
5397 const struct bfd_elf_version_expr *e2 = p2;
5399 return strcmp (e1->symbol, e2->symbol) == 0;
5402 static void
5403 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5405 size_t count = 0;
5406 struct bfd_elf_version_expr *e, *next;
5407 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5409 for (e = head->list; e; e = e->next)
5411 if (e->symbol)
5412 count++;
5413 head->mask |= e->mask;
5416 if (count)
5418 head->htab = htab_create (count * 2, version_expr_head_hash,
5419 version_expr_head_eq, NULL);
5420 list_loc = &head->list;
5421 remaining_loc = &head->remaining;
5422 for (e = head->list; e; e = next)
5424 next = e->next;
5425 if (!e->symbol)
5427 *remaining_loc = e;
5428 remaining_loc = &e->next;
5430 else
5432 void **loc = htab_find_slot (head->htab, e, INSERT);
5434 if (*loc)
5436 struct bfd_elf_version_expr *e1, *last;
5438 e1 = *loc;
5439 last = NULL;
5442 if (e1->mask == e->mask)
5444 last = NULL;
5445 break;
5447 last = e1;
5448 e1 = e1->next;
5450 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5452 if (last == NULL)
5454 /* This is a duplicate. */
5455 /* FIXME: Memory leak. Sometimes pattern is not
5456 xmalloced alone, but in larger chunk of memory. */
5457 /* free (e->symbol); */
5458 free (e);
5460 else
5462 e->next = last->next;
5463 last->next = e;
5466 else
5468 *loc = e;
5469 *list_loc = e;
5470 list_loc = &e->next;
5474 *remaining_loc = NULL;
5475 *list_loc = head->remaining;
5477 else
5478 head->remaining = head->list;
5481 /* This is called when we know the name and dependencies of the
5482 version. */
5484 void
5485 lang_register_vers_node (const char *name,
5486 struct bfd_elf_version_tree *version,
5487 struct bfd_elf_version_deps *deps)
5489 struct bfd_elf_version_tree *t, **pp;
5490 struct bfd_elf_version_expr *e1;
5492 if (name == NULL)
5493 name = "";
5495 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5496 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5498 einfo (_("%X%P: anonymous version tag cannot be combined"
5499 " with other version tags\n"));
5500 free (version);
5501 return;
5504 /* Make sure this node has a unique name. */
5505 for (t = lang_elf_version_info; t != NULL; t = t->next)
5506 if (strcmp (t->name, name) == 0)
5507 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5509 lang_finalize_version_expr_head (&version->globals);
5510 lang_finalize_version_expr_head (&version->locals);
5512 /* Check the global and local match names, and make sure there
5513 aren't any duplicates. */
5515 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5517 for (t = lang_elf_version_info; t != NULL; t = t->next)
5519 struct bfd_elf_version_expr *e2;
5521 if (t->locals.htab && e1->symbol)
5523 e2 = htab_find (t->locals.htab, e1);
5524 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5526 if (e1->mask == e2->mask)
5527 einfo (_("%X%P: duplicate expression `%s'"
5528 " in version information\n"), e1->symbol);
5529 e2 = e2->next;
5532 else if (!e1->symbol)
5533 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5534 if (strcmp (e1->pattern, e2->pattern) == 0
5535 && e1->mask == e2->mask)
5536 einfo (_("%X%P: duplicate expression `%s'"
5537 " in version information\n"), e1->pattern);
5541 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5543 for (t = lang_elf_version_info; t != NULL; t = t->next)
5545 struct bfd_elf_version_expr *e2;
5547 if (t->globals.htab && e1->symbol)
5549 e2 = htab_find (t->globals.htab, e1);
5550 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5552 if (e1->mask == e2->mask)
5553 einfo (_("%X%P: duplicate expression `%s'"
5554 " in version information\n"),
5555 e1->symbol);
5556 e2 = e2->next;
5559 else if (!e1->symbol)
5560 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5561 if (strcmp (e1->pattern, e2->pattern) == 0
5562 && e1->mask == e2->mask)
5563 einfo (_("%X%P: duplicate expression `%s'"
5564 " in version information\n"), e1->pattern);
5568 version->deps = deps;
5569 version->name = name;
5570 if (name[0] != '\0')
5572 ++version_index;
5573 version->vernum = version_index;
5575 else
5576 version->vernum = 0;
5578 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5580 *pp = version;
5583 /* This is called when we see a version dependency. */
5585 struct bfd_elf_version_deps *
5586 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5588 struct bfd_elf_version_deps *ret;
5589 struct bfd_elf_version_tree *t;
5591 ret = xmalloc (sizeof *ret);
5592 ret->next = list;
5594 for (t = lang_elf_version_info; t != NULL; t = t->next)
5596 if (strcmp (t->name, name) == 0)
5598 ret->version_needed = t;
5599 return ret;
5603 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5605 return ret;
5608 static void
5609 lang_do_version_exports_section (void)
5611 struct bfd_elf_version_expr *greg = NULL, *lreg;
5613 LANG_FOR_EACH_INPUT_STATEMENT (is)
5615 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5616 char *contents, *p;
5617 bfd_size_type len;
5619 if (sec == NULL)
5620 continue;
5622 len = sec->size;
5623 contents = xmalloc (len);
5624 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5625 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5627 p = contents;
5628 while (p < contents + len)
5630 greg = lang_new_vers_pattern (greg, p, NULL);
5631 p = strchr (p, '\0') + 1;
5634 /* Do not free the contents, as we used them creating the regex. */
5636 /* Do not include this section in the link. */
5637 sec->flags |= SEC_EXCLUDE;
5640 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5641 lang_register_vers_node (command_line.version_exports_section,
5642 lang_new_vers_node (greg, lreg), NULL);
5645 void
5646 lang_add_unique (const char *name)
5648 struct unique_sections *ent;
5650 for (ent = unique_section_list; ent; ent = ent->next)
5651 if (strcmp (ent->name, name) == 0)
5652 return;
5654 ent = xmalloc (sizeof *ent);
5655 ent->name = xstrdup (name);
5656 ent->next = unique_section_list;
5657 unique_section_list = ent;