* reloc16.c (bfd_coff_reloc16_relax_section): Set rawsize.
[binutils.git] / ld / ldlang.c
blob3d4098207f56fca708597e672616c1e0f6751d15
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->as_needed = as_needed;
458 p->whole_archive = whole_archive;
459 p->loaded = FALSE;
460 lang_statement_append (&input_file_chain,
461 (lang_statement_union_type *) p,
462 &p->next_real_file);
463 return p;
466 lang_input_statement_type *
467 lang_add_input_file (const char *name,
468 lang_input_file_enum_type file_type,
469 const char *target)
471 lang_has_input_file = TRUE;
472 return new_afile (name, file_type, target, TRUE);
475 /* Build enough state so that the parser can build its tree. */
477 void
478 lang_init (void)
480 obstack_begin (&stat_obstack, 1000);
482 stat_ptr = &statement_list;
484 lang_list_init (stat_ptr);
486 lang_list_init (&input_file_chain);
487 lang_list_init (&lang_output_section_statement);
488 lang_list_init (&file_chain);
489 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
490 NULL);
491 abs_output_section =
492 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
494 abs_output_section->bfd_section = bfd_abs_section_ptr;
496 /* The value "3" is ad-hoc, somewhat related to the expected number of
497 DEFINED expressions in a linker script. For most default linker
498 scripts, there are none. Why a hash table then? Well, it's somewhat
499 simpler to re-use working machinery than using a linked list in terms
500 of code-complexity here in ld, besides the initialization which just
501 looks like other code here. */
502 if (bfd_hash_table_init_n (&lang_definedness_table,
503 lang_definedness_newfunc, 3) != TRUE)
504 einfo (_("%P%F: out of memory during initialization"));
506 /* Callers of exp_fold_tree need to increment this. */
507 lang_statement_iteration = 0;
510 /*----------------------------------------------------------------------
511 A region is an area of memory declared with the
512 MEMORY { name:org=exp, len=exp ... }
513 syntax.
515 We maintain a list of all the regions here.
517 If no regions are specified in the script, then the default is used
518 which is created when looked up to be the entire data space.
520 If create is true we are creating a region inside a MEMORY block.
521 In this case it is probably an error to create a region that has
522 already been created. If we are not inside a MEMORY block it is
523 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
524 and so we issue a warning. */
526 static lang_memory_region_type *lang_memory_region_list;
527 static lang_memory_region_type **lang_memory_region_list_tail
528 = &lang_memory_region_list;
530 lang_memory_region_type *
531 lang_memory_region_lookup (const char *const name, bfd_boolean create)
533 lang_memory_region_type *p;
534 lang_memory_region_type *new;
536 /* NAME is NULL for LMA memspecs if no region was specified. */
537 if (name == NULL)
538 return NULL;
540 for (p = lang_memory_region_list; p != NULL; p = p->next)
541 if (strcmp (p->name, name) == 0)
543 if (create)
544 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"),
545 name);
546 return p;
549 #if 0
550 /* This code used to always use the first region in the list as the
551 default region. I changed it to instead use a region
552 encompassing all of memory as the default region. This permits
553 NOLOAD sections to work reasonably without requiring a region.
554 People should specify what region they mean, if they really want
555 a region. */
556 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
558 if (lang_memory_region_list != NULL)
559 return lang_memory_region_list;
561 #endif
563 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
564 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
566 new = stat_alloc (sizeof (lang_memory_region_type));
568 new->name = xstrdup (name);
569 new->next = NULL;
571 *lang_memory_region_list_tail = new;
572 lang_memory_region_list_tail = &new->next;
573 new->origin = 0;
574 new->flags = 0;
575 new->not_flags = 0;
576 new->length = ~(bfd_size_type) 0;
577 new->current = 0;
578 new->had_full_message = FALSE;
580 return new;
583 static lang_memory_region_type *
584 lang_memory_default (asection *section)
586 lang_memory_region_type *p;
588 flagword sec_flags = section->flags;
590 /* Override SEC_DATA to mean a writable section. */
591 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
592 sec_flags |= SEC_DATA;
594 for (p = lang_memory_region_list; p != NULL; p = p->next)
596 if ((p->flags & sec_flags) != 0
597 && (p->not_flags & sec_flags) == 0)
599 return p;
602 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
605 static lang_output_section_statement_type *
606 lang_output_section_find_1 (const char *const name, int constraint)
608 lang_statement_union_type *u;
609 lang_output_section_statement_type *lookup;
611 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
613 lookup = &u->output_section_statement;
614 if (strcmp (name, lookup->name) == 0
615 && lookup->constraint != -1
616 && (constraint == 0 || constraint == lookup->constraint))
617 return lookup;
619 return NULL;
622 lang_output_section_statement_type *
623 lang_output_section_find (const char *const name)
625 return lang_output_section_find_1 (name, 0);
628 static lang_output_section_statement_type *
629 lang_output_section_statement_lookup_1 (const char *const name, int constraint)
631 lang_output_section_statement_type *lookup;
633 lookup = lang_output_section_find_1 (name, constraint);
634 if (lookup == NULL)
636 lookup = new_stat (lang_output_section_statement, stat_ptr);
637 lookup->region = NULL;
638 lookup->lma_region = NULL;
639 lookup->fill = 0;
640 lookup->block_value = 1;
641 lookup->name = name;
643 lookup->next = NULL;
644 lookup->bfd_section = NULL;
645 lookup->processed = 0;
646 lookup->constraint = constraint;
647 lookup->sectype = normal_section;
648 lookup->addr_tree = NULL;
649 lang_list_init (&lookup->children);
651 lookup->memspec = NULL;
652 lookup->flags = 0;
653 lookup->subsection_alignment = -1;
654 lookup->section_alignment = -1;
655 lookup->load_base = NULL;
656 lookup->update_dot_tree = NULL;
657 lookup->phdrs = NULL;
659 lang_statement_append (&lang_output_section_statement,
660 (lang_statement_union_type *) lookup,
661 &lookup->next);
663 return lookup;
666 lang_output_section_statement_type *
667 lang_output_section_statement_lookup (const char *const name)
669 return lang_output_section_statement_lookup_1 (name, 0);
672 static void
673 lang_map_flags (flagword flag)
675 if (flag & SEC_ALLOC)
676 minfo ("a");
678 if (flag & SEC_CODE)
679 minfo ("x");
681 if (flag & SEC_READONLY)
682 minfo ("r");
684 if (flag & SEC_DATA)
685 minfo ("w");
687 if (flag & SEC_LOAD)
688 minfo ("l");
691 void
692 lang_map (void)
694 lang_memory_region_type *m;
695 bfd *p;
697 minfo (_("\nMemory Configuration\n\n"));
698 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
699 _("Name"), _("Origin"), _("Length"), _("Attributes"));
701 for (m = lang_memory_region_list; m != NULL; m = m->next)
703 char buf[100];
704 int len;
706 fprintf (config.map_file, "%-16s ", m->name);
708 sprintf_vma (buf, m->origin);
709 minfo ("0x%s ", buf);
710 len = strlen (buf);
711 while (len < 16)
713 print_space ();
714 ++len;
717 minfo ("0x%V", m->length);
718 if (m->flags || m->not_flags)
720 #ifndef BFD64
721 minfo (" ");
722 #endif
723 if (m->flags)
725 print_space ();
726 lang_map_flags (m->flags);
729 if (m->not_flags)
731 minfo (" !");
732 lang_map_flags (m->not_flags);
736 print_nl ();
739 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
741 if (! command_line.reduce_memory_overheads)
743 obstack_begin (&map_obstack, 1000);
744 for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
745 bfd_map_over_sections (p, init_map_userdata, 0);
746 bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
748 print_statements ();
751 static void
752 init_map_userdata (abfd, sec, data)
753 bfd *abfd ATTRIBUTE_UNUSED;
754 asection *sec;
755 void *data ATTRIBUTE_UNUSED;
757 fat_section_userdata_type *new_data
758 = ((fat_section_userdata_type *) (stat_alloc
759 (sizeof (fat_section_userdata_type))));
761 ASSERT (get_userdata (sec) == NULL);
762 get_userdata (sec) = new_data;
763 new_data->map_symbol_def_tail = &new_data->map_symbol_def_head;
766 static bfd_boolean
767 sort_def_symbol (hash_entry, info)
768 struct bfd_link_hash_entry *hash_entry;
769 void *info ATTRIBUTE_UNUSED;
771 if (hash_entry->type == bfd_link_hash_defined
772 || hash_entry->type == bfd_link_hash_defweak)
774 struct fat_user_section_struct *ud;
775 struct map_symbol_def *def;
777 ud = get_userdata (hash_entry->u.def.section);
778 if (! ud)
780 /* ??? What do we have to do to initialize this beforehand? */
781 /* The first time we get here is bfd_abs_section... */
782 init_map_userdata (0, hash_entry->u.def.section, 0);
783 ud = get_userdata (hash_entry->u.def.section);
785 else if (!ud->map_symbol_def_tail)
786 ud->map_symbol_def_tail = &ud->map_symbol_def_head;
787 def = obstack_alloc (&map_obstack, sizeof *def);
788 def->entry = hash_entry;
789 *ud->map_symbol_def_tail = def;
790 ud->map_symbol_def_tail = &def->next;
792 return TRUE;
795 /* Initialize an output section. */
797 static void
798 init_os (lang_output_section_statement_type *s)
800 lean_section_userdata_type *new;
802 if (s->bfd_section != NULL)
803 return;
805 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
806 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
808 new = stat_alloc (SECTION_USERDATA_SIZE);
810 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
811 if (s->bfd_section == NULL)
812 s->bfd_section = bfd_make_section (output_bfd, s->name);
813 if (s->bfd_section == NULL)
815 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
816 output_bfd->xvec->name, s->name);
818 s->bfd_section->output_section = s->bfd_section;
820 /* We initialize an output sections output offset to minus its own
821 vma to allow us to output a section through itself. */
822 s->bfd_section->output_offset = 0;
823 get_userdata (s->bfd_section) = new;
825 /* If there is a base address, make sure that any sections it might
826 mention are initialized. */
827 if (s->addr_tree != NULL)
828 exp_init_os (s->addr_tree);
830 if (s->load_base != NULL)
831 exp_init_os (s->load_base);
834 /* Make sure that all output sections mentioned in an expression are
835 initialized. */
837 static void
838 exp_init_os (etree_type *exp)
840 switch (exp->type.node_class)
842 case etree_assign:
843 exp_init_os (exp->assign.src);
844 break;
846 case etree_binary:
847 exp_init_os (exp->binary.lhs);
848 exp_init_os (exp->binary.rhs);
849 break;
851 case etree_trinary:
852 exp_init_os (exp->trinary.cond);
853 exp_init_os (exp->trinary.lhs);
854 exp_init_os (exp->trinary.rhs);
855 break;
857 case etree_assert:
858 exp_init_os (exp->assert_s.child);
859 break;
861 case etree_unary:
862 exp_init_os (exp->unary.child);
863 break;
865 case etree_name:
866 switch (exp->type.node_code)
868 case ADDR:
869 case LOADADDR:
870 case SIZEOF:
872 lang_output_section_statement_type *os;
874 os = lang_output_section_find (exp->name.name);
875 if (os != NULL && os->bfd_section == NULL)
876 init_os (os);
879 break;
881 default:
882 break;
886 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
887 once into the output. This routine checks each section, and
888 arrange to discard it if a section of the same name has already
889 been linked. If the section has COMDAT information, then it uses
890 that to decide whether the section should be included. This code
891 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
892 that is, it does not depend solely upon the section name.
893 section_already_linked is called via bfd_map_over_sections. */
895 /* This is the shape of the elements inside the already_linked hash
896 table. It maps a name onto a list of already_linked elements with
897 the same name. It's possible to get more than one element in a
898 list if the COMDAT sections have different names. */
900 struct already_linked_hash_entry
902 struct bfd_hash_entry root;
903 struct already_linked *entry;
906 struct already_linked
908 struct already_linked *next;
909 asection *sec;
912 /* The hash table. */
914 static struct bfd_hash_table already_linked_table;
916 static void
917 section_already_linked (bfd *abfd, asection *sec, void *data)
919 lang_input_statement_type *entry = data;
920 flagword flags;
921 const char *name;
922 struct already_linked *l;
923 struct already_linked_hash_entry *already_linked_list;
925 /* If we are only reading symbols from this object, then we want to
926 discard all sections. */
927 if (entry->just_syms_flag)
929 bfd_link_just_syms (sec, &link_info);
930 return;
933 flags = sec->flags;
934 if ((flags & SEC_LINK_ONCE) == 0)
935 return;
937 /* FIXME: When doing a relocatable link, we may have trouble
938 copying relocations in other sections that refer to local symbols
939 in the section being discarded. Those relocations will have to
940 be converted somehow; as of this writing I'm not sure that any of
941 the backends handle that correctly.
943 It is tempting to instead not discard link once sections when
944 doing a relocatable link (technically, they should be discarded
945 whenever we are building constructors). However, that fails,
946 because the linker winds up combining all the link once sections
947 into a single large link once section, which defeats the purpose
948 of having link once sections in the first place.
950 Also, not merging link once sections in a relocatable link
951 causes trouble for MIPS ELF, which relies on link once semantics
952 to handle the .reginfo section correctly. */
954 name = bfd_get_section_name (abfd, sec);
956 already_linked_list =
957 ((struct already_linked_hash_entry *)
958 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
960 for (l = already_linked_list->entry; l != NULL; l = l->next)
962 if (sec->comdat == NULL
963 || l->sec->comdat == NULL
964 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
966 /* The section has already been linked. See if we should
967 issue a warning. */
968 switch (flags & SEC_LINK_DUPLICATES)
970 default:
971 abort ();
973 case SEC_LINK_DUPLICATES_DISCARD:
974 break;
976 case SEC_LINK_DUPLICATES_ONE_ONLY:
977 if (sec->comdat == NULL)
978 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
979 abfd, name);
980 else
981 einfo (_("%P: %B: warning: ignoring duplicate `%s'"
982 " section symbol `%s'\n"),
983 abfd, name, sec->comdat->name);
984 break;
986 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
987 /* FIXME: We should really dig out the contents of both
988 sections and memcmp them. The COFF/PE spec says that
989 the Microsoft linker does not implement this
990 correctly, so I'm not going to bother doing it
991 either. */
992 /* Fall through. */
993 case SEC_LINK_DUPLICATES_SAME_SIZE:
994 if (sec->size != l->sec->size)
995 einfo (_("%P: %B: warning: duplicate section `%s'"
996 " has different size\n"),
997 abfd, name);
998 break;
1001 /* Set the output_section field so that lang_add_section
1002 does not create a lang_input_section structure for this
1003 section. Since there might be a symbol in the section
1004 being discarded, we must retain a pointer to the section
1005 which we are really going to use. */
1006 sec->output_section = bfd_abs_section_ptr;
1007 sec->kept_section = l->sec;
1009 if (flags & SEC_GROUP)
1010 bfd_discard_group (abfd, sec);
1012 return;
1016 /* This is the first section with this name. Record it. Allocate
1017 the memory from the same obstack as the hash table is kept in. */
1019 l = bfd_hash_allocate (&already_linked_table, sizeof *l);
1021 l->sec = sec;
1022 l->next = already_linked_list->entry;
1023 already_linked_list->entry = l;
1026 /* Support routines for the hash table used by section_already_linked,
1027 initialize the table, fill in an entry and remove the table. */
1029 static struct bfd_hash_entry *
1030 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
1031 struct bfd_hash_table *table,
1032 const char *string ATTRIBUTE_UNUSED)
1034 struct already_linked_hash_entry *ret =
1035 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
1037 ret->entry = NULL;
1039 return &ret->root;
1042 static void
1043 already_linked_table_init (void)
1045 if (! bfd_hash_table_init_n (&already_linked_table,
1046 already_linked_newfunc,
1047 42))
1048 einfo (_("%P%F: Failed to create hash table\n"));
1051 static void
1052 already_linked_table_free (void)
1054 bfd_hash_table_free (&already_linked_table);
1057 /* The wild routines.
1059 These expand statements like *(.text) and foo.o to a list of
1060 explicit actions, like foo.o(.text), bar.o(.text) and
1061 foo.o(.text, .data). */
1063 /* Return TRUE if the PATTERN argument is a wildcard pattern.
1064 Although backslashes are treated specially if a pattern contains
1065 wildcards, we do not consider the mere presence of a backslash to
1066 be enough to cause the pattern to be treated as a wildcard.
1067 That lets us handle DOS filenames more naturally. */
1069 static bfd_boolean
1070 wildcardp (const char *pattern)
1072 const char *s;
1074 for (s = pattern; *s != '\0'; ++s)
1075 if (*s == '?'
1076 || *s == '*'
1077 || *s == '[')
1078 return TRUE;
1079 return FALSE;
1082 /* Add SECTION to the output section OUTPUT. Do this by creating a
1083 lang_input_section statement which is placed at PTR. FILE is the
1084 input file which holds SECTION. */
1086 void
1087 lang_add_section (lang_statement_list_type *ptr,
1088 asection *section,
1089 lang_output_section_statement_type *output,
1090 lang_input_statement_type *file)
1092 flagword flags = section->flags;
1093 bfd_boolean discard;
1095 /* Discard sections marked with SEC_EXCLUDE. */
1096 discard = (flags & SEC_EXCLUDE) != 0;
1098 /* Discard input sections which are assigned to a section named
1099 DISCARD_SECTION_NAME. */
1100 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1101 discard = TRUE;
1103 /* Discard debugging sections if we are stripping debugging
1104 information. */
1105 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1106 && (flags & SEC_DEBUGGING) != 0)
1107 discard = TRUE;
1109 if (discard)
1111 if (section->output_section == NULL)
1113 /* This prevents future calls from assigning this section. */
1114 section->output_section = bfd_abs_section_ptr;
1116 return;
1119 if (section->output_section == NULL)
1121 bfd_boolean first;
1122 lang_input_section_type *new;
1123 flagword flags;
1125 if (output->bfd_section == NULL)
1126 init_os (output);
1128 first = ! output->bfd_section->linker_has_input;
1129 output->bfd_section->linker_has_input = 1;
1131 /* Add a section reference to the list. */
1132 new = new_stat (lang_input_section, ptr);
1134 new->section = section;
1135 new->ifile = file;
1136 section->output_section = output->bfd_section;
1138 flags = section->flags;
1140 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1141 to an output section, because we want to be able to include a
1142 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1143 section (I don't know why we want to do this, but we do).
1144 build_link_order in ldwrite.c handles this case by turning
1145 the embedded SEC_NEVER_LOAD section into a fill. */
1147 flags &= ~ SEC_NEVER_LOAD;
1149 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1150 already been processed. One reason to do this is that on pe
1151 format targets, .text$foo sections go into .text and it's odd
1152 to see .text with SEC_LINK_ONCE set. */
1154 if (! link_info.relocatable)
1155 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1157 /* If this is not the first input section, and the SEC_READONLY
1158 flag is not currently set, then don't set it just because the
1159 input section has it set. */
1161 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1162 flags &= ~ SEC_READONLY;
1164 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1165 if (! first
1166 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1167 != (flags & (SEC_MERGE | SEC_STRINGS))
1168 || ((flags & SEC_MERGE)
1169 && section->output_section->entsize != section->entsize)))
1171 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1172 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1175 section->output_section->flags |= flags;
1177 if (flags & SEC_MERGE)
1178 section->output_section->entsize = section->entsize;
1180 /* If SEC_READONLY is not set in the input section, then clear
1181 it from the output section. */
1182 if ((section->flags & SEC_READONLY) == 0)
1183 section->output_section->flags &= ~SEC_READONLY;
1185 switch (output->sectype)
1187 case normal_section:
1188 break;
1189 case dsect_section:
1190 case copy_section:
1191 case info_section:
1192 case overlay_section:
1193 output->bfd_section->flags &= ~SEC_ALLOC;
1194 break;
1195 case noload_section:
1196 output->bfd_section->flags &= ~SEC_LOAD;
1197 output->bfd_section->flags |= SEC_NEVER_LOAD;
1198 break;
1201 /* Copy over SEC_SMALL_DATA. */
1202 if (section->flags & SEC_SMALL_DATA)
1203 section->output_section->flags |= SEC_SMALL_DATA;
1205 if (section->alignment_power > output->bfd_section->alignment_power)
1206 output->bfd_section->alignment_power = section->alignment_power;
1208 /* If supplied an alignment, then force it. */
1209 if (output->section_alignment != -1)
1210 output->bfd_section->alignment_power = output->section_alignment;
1212 if (section->flags & SEC_BLOCK)
1214 section->output_section->flags |= SEC_BLOCK;
1215 /* FIXME: This value should really be obtained from the bfd... */
1216 output->block_value = 128;
1221 /* Handle wildcard sorting. This returns the lang_input_section which
1222 should follow the one we are going to create for SECTION and FILE,
1223 based on the sorting requirements of WILD. It returns NULL if the
1224 new section should just go at the end of the current list. */
1226 static lang_statement_union_type *
1227 wild_sort (lang_wild_statement_type *wild,
1228 struct wildcard_list *sec,
1229 lang_input_statement_type *file,
1230 asection *section)
1232 const char *section_name;
1233 lang_statement_union_type *l;
1235 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1236 return NULL;
1238 section_name = bfd_get_section_name (file->the_bfd, section);
1239 for (l = wild->children.head; l != NULL; l = l->header.next)
1241 lang_input_section_type *ls;
1243 if (l->header.type != lang_input_section_enum)
1244 continue;
1245 ls = &l->input_section;
1247 /* Sorting by filename takes precedence over sorting by section
1248 name. */
1250 if (wild->filenames_sorted)
1252 const char *fn, *ln;
1253 bfd_boolean fa, la;
1254 int i;
1256 /* The PE support for the .idata section as generated by
1257 dlltool assumes that files will be sorted by the name of
1258 the archive and then the name of the file within the
1259 archive. */
1261 if (file->the_bfd != NULL
1262 && bfd_my_archive (file->the_bfd) != NULL)
1264 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1265 fa = TRUE;
1267 else
1269 fn = file->filename;
1270 fa = FALSE;
1273 if (ls->ifile->the_bfd != NULL
1274 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1276 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1277 la = TRUE;
1279 else
1281 ln = ls->ifile->filename;
1282 la = FALSE;
1285 i = strcmp (fn, ln);
1286 if (i > 0)
1287 continue;
1288 else if (i < 0)
1289 break;
1291 if (fa || la)
1293 if (fa)
1294 fn = file->filename;
1295 if (la)
1296 ln = ls->ifile->filename;
1298 i = strcmp (fn, ln);
1299 if (i > 0)
1300 continue;
1301 else if (i < 0)
1302 break;
1306 /* Here either the files are not sorted by name, or we are
1307 looking at the sections for this file. */
1309 if (sec != NULL && sec->spec.sorted)
1311 if (strcmp (section_name,
1312 bfd_get_section_name (ls->ifile->the_bfd,
1313 ls->section))
1314 < 0)
1315 break;
1319 return l;
1322 /* Expand a wild statement for a particular FILE. SECTION may be
1323 NULL, in which case it is a wild card. */
1325 static void
1326 output_section_callback (lang_wild_statement_type *ptr,
1327 struct wildcard_list *sec,
1328 asection *section,
1329 lang_input_statement_type *file,
1330 void *output)
1332 lang_statement_union_type *before;
1334 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1335 if (unique_section_p (section))
1336 return;
1338 before = wild_sort (ptr, sec, file, section);
1340 /* Here BEFORE points to the lang_input_section which
1341 should follow the one we are about to add. If BEFORE
1342 is NULL, then the section should just go at the end
1343 of the current list. */
1345 if (before == NULL)
1346 lang_add_section (&ptr->children, section,
1347 (lang_output_section_statement_type *) output,
1348 file);
1349 else
1351 lang_statement_list_type list;
1352 lang_statement_union_type **pp;
1354 lang_list_init (&list);
1355 lang_add_section (&list, section,
1356 (lang_output_section_statement_type *) output,
1357 file);
1359 /* If we are discarding the section, LIST.HEAD will
1360 be NULL. */
1361 if (list.head != NULL)
1363 ASSERT (list.head->header.next == NULL);
1365 for (pp = &ptr->children.head;
1366 *pp != before;
1367 pp = &(*pp)->header.next)
1368 ASSERT (*pp != NULL);
1370 list.head->header.next = *pp;
1371 *pp = list.head;
1376 /* Check if all sections in a wild statement for a particular FILE
1377 are readonly. */
1379 static void
1380 check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
1381 struct wildcard_list *sec ATTRIBUTE_UNUSED,
1382 asection *section,
1383 lang_input_statement_type *file ATTRIBUTE_UNUSED,
1384 void *data)
1386 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1387 if (unique_section_p (section))
1388 return;
1390 if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
1391 ((lang_output_section_statement_type *) data)->all_input_readonly = FALSE;
1394 /* This is passed a file name which must have been seen already and
1395 added to the statement tree. We will see if it has been opened
1396 already and had its symbols read. If not then we'll read it. */
1398 static lang_input_statement_type *
1399 lookup_name (const char *name)
1401 lang_input_statement_type *search;
1403 for (search = (lang_input_statement_type *) input_file_chain.head;
1404 search != NULL;
1405 search = (lang_input_statement_type *) search->next_real_file)
1407 /* Use the local_sym_name as the name of the file that has
1408 already been loaded as filename might have been transformed
1409 via the search directory lookup mechanism. */
1410 const char * filename = search->local_sym_name;
1412 if (filename == NULL && name == NULL)
1413 return search;
1414 if (filename != NULL
1415 && name != NULL
1416 && strcmp (filename, name) == 0)
1417 break;
1420 if (search == NULL)
1421 search = new_afile (name, lang_input_file_is_search_file_enum,
1422 default_target, FALSE);
1424 /* If we have already added this file, or this file is not real
1425 (FIXME: can that ever actually happen?) or the name is NULL
1426 (FIXME: can that ever actually happen?) don't add this file. */
1427 if (search->loaded
1428 || ! search->real
1429 || search->filename == NULL)
1430 return search;
1432 if (! load_symbols (search, NULL))
1433 return NULL;
1435 return search;
1438 /* Get the symbols for an input file. */
1440 static bfd_boolean
1441 load_symbols (lang_input_statement_type *entry,
1442 lang_statement_list_type *place)
1444 char **matching;
1446 if (entry->loaded)
1447 return TRUE;
1449 ldfile_open_file (entry);
1451 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1452 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1454 bfd_error_type err;
1455 lang_statement_list_type *hold;
1456 bfd_boolean bad_load = TRUE;
1457 bfd_boolean save_ldlang_sysrooted_script;
1459 err = bfd_get_error ();
1461 /* See if the emulation has some special knowledge. */
1462 if (ldemul_unrecognized_file (entry))
1463 return TRUE;
1465 if (err == bfd_error_file_ambiguously_recognized)
1467 char **p;
1469 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1470 einfo (_("%B: matching formats:"), entry->the_bfd);
1471 for (p = matching; *p != NULL; p++)
1472 einfo (" %s", *p);
1473 einfo ("%F\n");
1475 else if (err != bfd_error_file_not_recognized
1476 || place == NULL)
1477 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1478 else
1479 bad_load = FALSE;
1481 bfd_close (entry->the_bfd);
1482 entry->the_bfd = NULL;
1484 /* Try to interpret the file as a linker script. */
1485 ldfile_open_command_file (entry->filename);
1487 hold = stat_ptr;
1488 stat_ptr = place;
1489 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1490 ldlang_sysrooted_script = entry->sysrooted;
1492 ldfile_assumed_script = TRUE;
1493 parser_input = input_script;
1494 yyparse ();
1495 ldfile_assumed_script = FALSE;
1497 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1498 stat_ptr = hold;
1500 return ! bad_load;
1503 if (ldemul_recognized_file (entry))
1504 return TRUE;
1506 /* We don't call ldlang_add_file for an archive. Instead, the
1507 add_symbols entry point will call ldlang_add_file, via the
1508 add_archive_element callback, for each element of the archive
1509 which is used. */
1510 switch (bfd_get_format (entry->the_bfd))
1512 default:
1513 break;
1515 case bfd_object:
1516 ldlang_add_file (entry);
1517 if (trace_files || trace_file_tries)
1518 info_msg ("%I\n", entry);
1519 break;
1521 case bfd_archive:
1522 if (entry->whole_archive)
1524 bfd *member = NULL;
1525 bfd_boolean loaded = TRUE;
1527 for (;;)
1529 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1531 if (member == NULL)
1532 break;
1534 if (! bfd_check_format (member, bfd_object))
1536 einfo (_("%F%B: member %B in archive is not an object\n"),
1537 entry->the_bfd, member);
1538 loaded = FALSE;
1541 if (! ((*link_info.callbacks->add_archive_element)
1542 (&link_info, member, "--whole-archive")))
1543 abort ();
1545 if (! bfd_link_add_symbols (member, &link_info))
1547 einfo (_("%F%B: could not read symbols: %E\n"), member);
1548 loaded = FALSE;
1552 entry->loaded = loaded;
1553 return loaded;
1555 break;
1558 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1559 entry->loaded = TRUE;
1560 else
1561 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1563 return entry->loaded;
1566 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1567 may be NULL, indicating that it is a wildcard. Separate
1568 lang_input_section statements are created for each part of the
1569 expansion; they are added after the wild statement S. OUTPUT is
1570 the output section. */
1572 static void
1573 wild (lang_wild_statement_type *s,
1574 const char *target ATTRIBUTE_UNUSED,
1575 lang_output_section_statement_type *output)
1577 struct wildcard_list *sec;
1579 walk_wild (s, output_section_callback, output);
1581 for (sec = s->section_list; sec != NULL; sec = sec->next)
1583 if (default_common_section != NULL)
1584 break;
1585 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1587 /* Remember the section that common is going to in case we
1588 later get something which doesn't know where to put it. */
1589 default_common_section = output;
1594 /* Return TRUE iff target is the sought target. */
1596 static int
1597 get_target (const bfd_target *target, void *data)
1599 const char *sought = data;
1601 return strcmp (target->name, sought) == 0;
1604 /* Like strcpy() but convert to lower case as well. */
1606 static void
1607 stricpy (char *dest, char *src)
1609 char c;
1611 while ((c = *src++) != 0)
1612 *dest++ = TOLOWER (c);
1614 *dest = 0;
1617 /* Remove the first occurrence of needle (if any) in haystack
1618 from haystack. */
1620 static void
1621 strcut (char *haystack, char *needle)
1623 haystack = strstr (haystack, needle);
1625 if (haystack)
1627 char *src;
1629 for (src = haystack + strlen (needle); *src;)
1630 *haystack++ = *src++;
1632 *haystack = 0;
1636 /* Compare two target format name strings.
1637 Return a value indicating how "similar" they are. */
1639 static int
1640 name_compare (char *first, char *second)
1642 char *copy1;
1643 char *copy2;
1644 int result;
1646 copy1 = xmalloc (strlen (first) + 1);
1647 copy2 = xmalloc (strlen (second) + 1);
1649 /* Convert the names to lower case. */
1650 stricpy (copy1, first);
1651 stricpy (copy2, second);
1653 /* Remove size and endian strings from the name. */
1654 strcut (copy1, "big");
1655 strcut (copy1, "little");
1656 strcut (copy2, "big");
1657 strcut (copy2, "little");
1659 /* Return a value based on how many characters match,
1660 starting from the beginning. If both strings are
1661 the same then return 10 * their length. */
1662 for (result = 0; copy1[result] == copy2[result]; result++)
1663 if (copy1[result] == 0)
1665 result *= 10;
1666 break;
1669 free (copy1);
1670 free (copy2);
1672 return result;
1675 /* Set by closest_target_match() below. */
1676 static const bfd_target *winner;
1678 /* Scan all the valid bfd targets looking for one that has the endianness
1679 requirement that was specified on the command line, and is the nearest
1680 match to the original output target. */
1682 static int
1683 closest_target_match (const bfd_target *target, void *data)
1685 const bfd_target *original = data;
1687 if (command_line.endian == ENDIAN_BIG
1688 && target->byteorder != BFD_ENDIAN_BIG)
1689 return 0;
1691 if (command_line.endian == ENDIAN_LITTLE
1692 && target->byteorder != BFD_ENDIAN_LITTLE)
1693 return 0;
1695 /* Must be the same flavour. */
1696 if (target->flavour != original->flavour)
1697 return 0;
1699 /* If we have not found a potential winner yet, then record this one. */
1700 if (winner == NULL)
1702 winner = target;
1703 return 0;
1706 /* Oh dear, we now have two potential candidates for a successful match.
1707 Compare their names and choose the better one. */
1708 if (name_compare (target->name, original->name)
1709 > name_compare (winner->name, original->name))
1710 winner = target;
1712 /* Keep on searching until wqe have checked them all. */
1713 return 0;
1716 /* Return the BFD target format of the first input file. */
1718 static char *
1719 get_first_input_target (void)
1721 char *target = NULL;
1723 LANG_FOR_EACH_INPUT_STATEMENT (s)
1725 if (s->header.type == lang_input_statement_enum
1726 && s->real)
1728 ldfile_open_file (s);
1730 if (s->the_bfd != NULL
1731 && bfd_check_format (s->the_bfd, bfd_object))
1733 target = bfd_get_target (s->the_bfd);
1735 if (target != NULL)
1736 break;
1741 return target;
1744 const char *
1745 lang_get_output_target (void)
1747 const char *target;
1749 /* Has the user told us which output format to use? */
1750 if (output_target != NULL)
1751 return output_target;
1753 /* No - has the current target been set to something other than
1754 the default? */
1755 if (current_target != default_target)
1756 return current_target;
1758 /* No - can we determine the format of the first input file? */
1759 target = get_first_input_target ();
1760 if (target != NULL)
1761 return target;
1763 /* Failed - use the default output target. */
1764 return default_target;
1767 /* Open the output file. */
1769 static bfd *
1770 open_output (const char *name)
1772 bfd *output;
1774 output_target = lang_get_output_target ();
1776 /* Has the user requested a particular endianness on the command
1777 line? */
1778 if (command_line.endian != ENDIAN_UNSET)
1780 const bfd_target *target;
1781 enum bfd_endian desired_endian;
1783 /* Get the chosen target. */
1784 target = bfd_search_for_target (get_target, (void *) output_target);
1786 /* If the target is not supported, we cannot do anything. */
1787 if (target != NULL)
1789 if (command_line.endian == ENDIAN_BIG)
1790 desired_endian = BFD_ENDIAN_BIG;
1791 else
1792 desired_endian = BFD_ENDIAN_LITTLE;
1794 /* See if the target has the wrong endianness. This should
1795 not happen if the linker script has provided big and
1796 little endian alternatives, but some scrips don't do
1797 this. */
1798 if (target->byteorder != desired_endian)
1800 /* If it does, then see if the target provides
1801 an alternative with the correct endianness. */
1802 if (target->alternative_target != NULL
1803 && (target->alternative_target->byteorder == desired_endian))
1804 output_target = target->alternative_target->name;
1805 else
1807 /* Try to find a target as similar as possible to
1808 the default target, but which has the desired
1809 endian characteristic. */
1810 bfd_search_for_target (closest_target_match,
1811 (void *) target);
1813 /* Oh dear - we could not find any targets that
1814 satisfy our requirements. */
1815 if (winner == NULL)
1816 einfo (_("%P: warning: could not find any targets"
1817 " that match endianness requirement\n"));
1818 else
1819 output_target = winner->name;
1825 output = bfd_openw (name, output_target);
1827 if (output == NULL)
1829 if (bfd_get_error () == bfd_error_invalid_target)
1830 einfo (_("%P%F: target %s not found\n"), output_target);
1832 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1835 delete_output_file_on_failure = TRUE;
1837 #if 0
1838 output->flags |= D_PAGED;
1839 #endif
1841 if (! bfd_set_format (output, bfd_object))
1842 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1843 if (! bfd_set_arch_mach (output,
1844 ldfile_output_architecture,
1845 ldfile_output_machine))
1846 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1848 link_info.hash = bfd_link_hash_table_create (output);
1849 if (link_info.hash == NULL)
1850 einfo (_("%P%F: can not create link hash table: %E\n"));
1852 bfd_set_gp_size (output, g_switch_value);
1853 return output;
1856 static void
1857 ldlang_open_output (lang_statement_union_type *statement)
1859 switch (statement->header.type)
1861 case lang_output_statement_enum:
1862 ASSERT (output_bfd == NULL);
1863 output_bfd = open_output (statement->output_statement.name);
1864 ldemul_set_output_arch ();
1865 if (config.magic_demand_paged && !link_info.relocatable)
1866 output_bfd->flags |= D_PAGED;
1867 else
1868 output_bfd->flags &= ~D_PAGED;
1869 if (config.text_read_only)
1870 output_bfd->flags |= WP_TEXT;
1871 else
1872 output_bfd->flags &= ~WP_TEXT;
1873 if (link_info.traditional_format)
1874 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1875 else
1876 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1877 break;
1879 case lang_target_statement_enum:
1880 current_target = statement->target_statement.target;
1881 break;
1882 default:
1883 break;
1887 /* Convert between addresses in bytes and sizes in octets.
1888 For currently supported targets, octets_per_byte is always a power
1889 of two, so we can use shifts. */
1890 #define TO_ADDR(X) ((X) >> opb_shift)
1891 #define TO_SIZE(X) ((X) << opb_shift)
1893 /* Support the above. */
1894 static unsigned int opb_shift = 0;
1896 static void
1897 init_opb (void)
1899 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1900 ldfile_output_machine);
1901 opb_shift = 0;
1902 if (x > 1)
1903 while ((x & 1) == 0)
1905 x >>= 1;
1906 ++opb_shift;
1908 ASSERT (x == 1);
1911 /* Open all the input files. */
1913 static void
1914 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1916 for (; s != NULL; s = s->header.next)
1918 switch (s->header.type)
1920 case lang_constructors_statement_enum:
1921 open_input_bfds (constructor_list.head, force);
1922 break;
1923 case lang_output_section_statement_enum:
1924 open_input_bfds (s->output_section_statement.children.head, force);
1925 break;
1926 case lang_wild_statement_enum:
1927 /* Maybe we should load the file's symbols. */
1928 if (s->wild_statement.filename
1929 && ! wildcardp (s->wild_statement.filename))
1930 lookup_name (s->wild_statement.filename);
1931 open_input_bfds (s->wild_statement.children.head, force);
1932 break;
1933 case lang_group_statement_enum:
1935 struct bfd_link_hash_entry *undefs;
1937 /* We must continually search the entries in the group
1938 until no new symbols are added to the list of undefined
1939 symbols. */
1943 undefs = link_info.hash->undefs_tail;
1944 open_input_bfds (s->group_statement.children.head, TRUE);
1946 while (undefs != link_info.hash->undefs_tail);
1948 break;
1949 case lang_target_statement_enum:
1950 current_target = s->target_statement.target;
1951 break;
1952 case lang_input_statement_enum:
1953 if (s->input_statement.real)
1955 lang_statement_list_type add;
1957 s->input_statement.target = current_target;
1959 /* If we are being called from within a group, and this
1960 is an archive which has already been searched, then
1961 force it to be researched unless the whole archive
1962 has been loaded already. */
1963 if (force
1964 && !s->input_statement.whole_archive
1965 && s->input_statement.loaded
1966 && bfd_check_format (s->input_statement.the_bfd,
1967 bfd_archive))
1968 s->input_statement.loaded = FALSE;
1970 lang_list_init (&add);
1972 if (! load_symbols (&s->input_statement, &add))
1973 config.make_executable = FALSE;
1975 if (add.head != NULL)
1977 *add.tail = s->header.next;
1978 s->header.next = add.head;
1981 break;
1982 default:
1983 break;
1988 /* If there are [COMMONS] statements, put a wild one into the bss
1989 section. */
1991 static void
1992 lang_reasonable_defaults (void)
1994 #if 0
1995 lang_output_section_statement_lookup (".text");
1996 lang_output_section_statement_lookup (".data");
1998 default_common_section = lang_output_section_statement_lookup (".bss");
2000 if (!placed_commons)
2002 lang_wild_statement_type *new =
2003 new_stat (lang_wild_statement,
2004 &default_common_section->children);
2006 new->section_name = "COMMON";
2007 new->filename = NULL;
2008 lang_list_init (&new->children);
2010 #endif
2013 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
2015 void
2016 lang_track_definedness (const char *name)
2018 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
2019 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
2022 /* New-function for the definedness hash table. */
2024 static struct bfd_hash_entry *
2025 lang_definedness_newfunc (struct bfd_hash_entry *entry,
2026 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
2027 const char *name ATTRIBUTE_UNUSED)
2029 struct lang_definedness_hash_entry *ret
2030 = (struct lang_definedness_hash_entry *) entry;
2032 if (ret == NULL)
2033 ret = (struct lang_definedness_hash_entry *)
2034 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
2036 if (ret == NULL)
2037 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
2039 ret->iteration = -1;
2040 return &ret->root;
2043 /* Return the iteration when the definition of NAME was last updated. A
2044 value of -1 means that the symbol is not defined in the linker script
2045 or the command line, but may be defined in the linker symbol table. */
2048 lang_symbol_definition_iteration (const char *name)
2050 struct lang_definedness_hash_entry *defentry
2051 = (struct lang_definedness_hash_entry *)
2052 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2054 /* We've already created this one on the presence of DEFINED in the
2055 script, so it can't be NULL unless something is borked elsewhere in
2056 the code. */
2057 if (defentry == NULL)
2058 FAIL ();
2060 return defentry->iteration;
2063 /* Update the definedness state of NAME. */
2065 void
2066 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
2068 struct lang_definedness_hash_entry *defentry
2069 = (struct lang_definedness_hash_entry *)
2070 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
2072 /* We don't keep track of symbols not tested with DEFINED. */
2073 if (defentry == NULL)
2074 return;
2076 /* If the symbol was already defined, and not from an earlier statement
2077 iteration, don't update the definedness iteration, because that'd
2078 make the symbol seem defined in the linker script at this point, and
2079 it wasn't; it was defined in some object. If we do anyway, DEFINED
2080 would start to yield false before this point and the construct "sym =
2081 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2082 in an object. */
2083 if (h->type != bfd_link_hash_undefined
2084 && h->type != bfd_link_hash_common
2085 && h->type != bfd_link_hash_new
2086 && defentry->iteration == -1)
2087 return;
2089 defentry->iteration = lang_statement_iteration;
2092 /* Add the supplied name to the symbol table as an undefined reference.
2093 This is a two step process as the symbol table doesn't even exist at
2094 the time the ld command line is processed. First we put the name
2095 on a list, then, once the output file has been opened, transfer the
2096 name to the symbol table. */
2098 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2100 #define ldlang_undef_chain_list_head entry_symbol.next
2102 void
2103 ldlang_add_undef (const char *const name)
2105 ldlang_undef_chain_list_type *new =
2106 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2108 new->next = ldlang_undef_chain_list_head;
2109 ldlang_undef_chain_list_head = new;
2111 new->name = xstrdup (name);
2113 if (output_bfd != NULL)
2114 insert_undefined (new->name);
2117 /* Insert NAME as undefined in the symbol table. */
2119 static void
2120 insert_undefined (const char *name)
2122 struct bfd_link_hash_entry *h;
2124 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2125 if (h == NULL)
2126 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2127 if (h->type == bfd_link_hash_new)
2129 h->type = bfd_link_hash_undefined;
2130 h->u.undef.abfd = NULL;
2131 bfd_link_add_undef (link_info.hash, h);
2135 /* Run through the list of undefineds created above and place them
2136 into the linker hash table as undefined symbols belonging to the
2137 script file. */
2139 static void
2140 lang_place_undefineds (void)
2142 ldlang_undef_chain_list_type *ptr;
2144 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2145 insert_undefined (ptr->name);
2148 /* Check for all readonly or some readwrite sections. */
2150 static void
2151 check_input_sections
2152 (lang_statement_union_type *s,
2153 lang_output_section_statement_type *output_section_statement)
2155 for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
2157 switch (s->header.type)
2159 case lang_wild_statement_enum:
2160 walk_wild (&s->wild_statement, check_section_callback,
2161 output_section_statement);
2162 if (! output_section_statement->all_input_readonly)
2163 return;
2164 break;
2165 case lang_constructors_statement_enum:
2166 check_input_sections (constructor_list.head,
2167 output_section_statement);
2168 if (! output_section_statement->all_input_readonly)
2169 return;
2170 break;
2171 case lang_group_statement_enum:
2172 check_input_sections (s->group_statement.children.head,
2173 output_section_statement);
2174 if (! output_section_statement->all_input_readonly)
2175 return;
2176 break;
2177 default:
2178 break;
2183 /* Open input files and attach to output sections. */
2185 static void
2186 map_input_to_output_sections
2187 (lang_statement_union_type *s, const char *target,
2188 lang_output_section_statement_type *output_section_statement)
2190 for (; s != NULL; s = s->header.next)
2192 switch (s->header.type)
2194 case lang_wild_statement_enum:
2195 wild (&s->wild_statement, target, output_section_statement);
2196 break;
2197 case lang_constructors_statement_enum:
2198 map_input_to_output_sections (constructor_list.head,
2199 target,
2200 output_section_statement);
2201 break;
2202 case lang_output_section_statement_enum:
2203 if (s->output_section_statement.constraint)
2205 if (s->output_section_statement.constraint == -1)
2206 break;
2207 s->output_section_statement.all_input_readonly = TRUE;
2208 check_input_sections (s->output_section_statement.children.head,
2209 &s->output_section_statement);
2210 if ((s->output_section_statement.all_input_readonly
2211 && s->output_section_statement.constraint == ONLY_IF_RW)
2212 || (!s->output_section_statement.all_input_readonly
2213 && s->output_section_statement.constraint == ONLY_IF_RO))
2215 s->output_section_statement.constraint = -1;
2216 break;
2220 map_input_to_output_sections (s->output_section_statement.children.head,
2221 target,
2222 &s->output_section_statement);
2223 break;
2224 case lang_output_statement_enum:
2225 break;
2226 case lang_target_statement_enum:
2227 target = s->target_statement.target;
2228 break;
2229 case lang_group_statement_enum:
2230 map_input_to_output_sections (s->group_statement.children.head,
2231 target,
2232 output_section_statement);
2233 break;
2234 case lang_data_statement_enum:
2235 /* Make sure that any sections mentioned in the expression
2236 are initialized. */
2237 exp_init_os (s->data_statement.exp);
2238 /* FALLTHROUGH */
2239 case lang_fill_statement_enum:
2240 case lang_input_section_enum:
2241 case lang_object_symbols_statement_enum:
2242 case lang_reloc_statement_enum:
2243 case lang_padding_statement_enum:
2244 case lang_input_statement_enum:
2245 if (output_section_statement != NULL
2246 && output_section_statement->bfd_section == NULL)
2247 init_os (output_section_statement);
2248 break;
2249 case lang_assignment_statement_enum:
2250 if (output_section_statement != NULL
2251 && output_section_statement->bfd_section == NULL)
2252 init_os (output_section_statement);
2254 /* Make sure that any sections mentioned in the assignment
2255 are initialized. */
2256 exp_init_os (s->assignment_statement.exp);
2257 break;
2258 case lang_afile_asection_pair_statement_enum:
2259 FAIL ();
2260 break;
2261 case lang_address_statement_enum:
2262 /* Mark the specified section with the supplied address. */
2264 lang_output_section_statement_type *os =
2265 lang_output_section_statement_lookup
2266 (s->address_statement.section_name);
2268 if (os->bfd_section == NULL)
2269 init_os (os);
2270 os->addr_tree = s->address_statement.address;
2272 break;
2277 /* An output section might have been removed after its statement was
2278 added. For example, ldemul_before_allocation can remove dynamic
2279 sections if they turn out to be not needed. Clean them up here. */
2281 static void
2282 strip_excluded_output_sections (void)
2284 lang_statement_union_type *u;
2286 for (u = lang_output_section_statement.head;
2287 u != NULL;
2288 u = u->output_section_statement.next)
2290 lang_output_section_statement_type *os;
2291 asection *s;
2293 os = &u->output_section_statement;
2294 if (os->constraint == -1)
2295 continue;
2296 s = os->bfd_section;
2297 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2299 asection **p;
2301 os->bfd_section = NULL;
2303 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2304 if (*p == s)
2306 bfd_section_list_remove (output_bfd, p);
2307 output_bfd->section_count--;
2308 break;
2314 static void
2315 print_output_section_statement
2316 (lang_output_section_statement_type *output_section_statement)
2318 asection *section = output_section_statement->bfd_section;
2319 int len;
2321 if (output_section_statement != abs_output_section)
2323 minfo ("\n%s", output_section_statement->name);
2325 if (section != NULL)
2327 print_dot = section->vma;
2329 len = strlen (output_section_statement->name);
2330 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2332 print_nl ();
2333 len = 0;
2335 while (len < SECTION_NAME_MAP_LENGTH)
2337 print_space ();
2338 ++len;
2341 minfo ("0x%V %W", section->vma, section->size);
2343 if (output_section_statement->load_base != NULL)
2345 bfd_vma addr;
2347 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2348 "load base", lang_final_phase_enum);
2349 minfo (_(" load address 0x%V"), addr);
2353 print_nl ();
2356 print_statement_list (output_section_statement->children.head,
2357 output_section_statement);
2360 static void
2361 print_assignment (lang_assignment_statement_type *assignment,
2362 lang_output_section_statement_type *output_section)
2364 int i;
2365 etree_value_type result;
2367 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2368 print_space ();
2370 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2371 lang_final_phase_enum, print_dot, &print_dot);
2372 if (result.valid_p)
2374 const char *dst;
2375 bfd_vma value;
2377 value = result.value + result.section->bfd_section->vma;
2378 dst = assignment->exp->assign.dst;
2380 minfo ("0x%V", value);
2381 if (dst[0] == '.' && dst[1] == 0)
2382 print_dot = value;
2384 else
2386 minfo ("*undef* ");
2387 #ifdef BFD64
2388 minfo (" ");
2389 #endif
2392 minfo (" ");
2394 exp_print_tree (assignment->exp);
2396 print_nl ();
2399 static void
2400 print_input_statement (lang_input_statement_type *statm)
2402 if (statm->filename != NULL)
2404 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2408 /* Print all symbols defined in a particular section. This is called
2409 via bfd_link_hash_traverse, or by print_all_symbols. */
2411 static bfd_boolean
2412 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2414 asection *sec = ptr;
2416 if ((hash_entry->type == bfd_link_hash_defined
2417 || hash_entry->type == bfd_link_hash_defweak)
2418 && sec == hash_entry->u.def.section)
2420 int i;
2422 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2423 print_space ();
2424 minfo ("0x%V ",
2425 (hash_entry->u.def.value
2426 + hash_entry->u.def.section->output_offset
2427 + hash_entry->u.def.section->output_section->vma));
2429 minfo (" %T\n", hash_entry->root.string);
2432 return TRUE;
2435 static void
2436 print_all_symbols (sec)
2437 asection *sec;
2439 struct fat_user_section_struct *ud = get_userdata (sec);
2440 struct map_symbol_def *def;
2442 *ud->map_symbol_def_tail = 0;
2443 for (def = ud->map_symbol_def_head; def; def = def->next)
2444 print_one_symbol (def->entry, sec);
2447 /* Print information about an input section to the map file. */
2449 static void
2450 print_input_section (lang_input_section_type *in)
2452 asection *i = in->section;
2453 bfd_size_type size = i->size;
2455 init_opb ();
2456 if (size != 0)
2458 int len;
2459 bfd_vma addr;
2461 print_space ();
2462 minfo ("%s", i->name);
2464 len = 1 + strlen (i->name);
2465 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2467 print_nl ();
2468 len = 0;
2470 while (len < SECTION_NAME_MAP_LENGTH)
2472 print_space ();
2473 ++len;
2476 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2477 addr = i->output_section->vma + i->output_offset;
2478 else
2480 addr = print_dot;
2481 size = 0;
2484 minfo ("0x%V %W %B\n", addr, TO_ADDR (size), i->owner);
2486 if (size != i->rawsize && i->rawsize != 0)
2488 len = SECTION_NAME_MAP_LENGTH + 3;
2489 #ifdef BFD64
2490 len += 16;
2491 #else
2492 len += 8;
2493 #endif
2494 while (len > 0)
2496 print_space ();
2497 --len;
2500 minfo (_("%W (size before relaxing)\n"), i->rawsize);
2503 if (i->output_section != NULL && (i->flags & SEC_EXCLUDE) == 0)
2505 if (command_line.reduce_memory_overheads)
2506 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2507 else
2508 print_all_symbols (i);
2510 print_dot = addr + TO_ADDR (size);
2515 static void
2516 print_fill_statement (lang_fill_statement_type *fill)
2518 size_t size;
2519 unsigned char *p;
2520 fputs (" FILL mask 0x", config.map_file);
2521 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2522 fprintf (config.map_file, "%02x", *p);
2523 fputs ("\n", config.map_file);
2526 static void
2527 print_data_statement (lang_data_statement_type *data)
2529 int i;
2530 bfd_vma addr;
2531 bfd_size_type size;
2532 const char *name;
2534 init_opb ();
2535 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2536 print_space ();
2538 addr = data->output_vma;
2539 if (data->output_section != NULL)
2540 addr += data->output_section->vma;
2542 switch (data->type)
2544 default:
2545 abort ();
2546 case BYTE:
2547 size = BYTE_SIZE;
2548 name = "BYTE";
2549 break;
2550 case SHORT:
2551 size = SHORT_SIZE;
2552 name = "SHORT";
2553 break;
2554 case LONG:
2555 size = LONG_SIZE;
2556 name = "LONG";
2557 break;
2558 case QUAD:
2559 size = QUAD_SIZE;
2560 name = "QUAD";
2561 break;
2562 case SQUAD:
2563 size = QUAD_SIZE;
2564 name = "SQUAD";
2565 break;
2568 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2570 if (data->exp->type.node_class != etree_value)
2572 print_space ();
2573 exp_print_tree (data->exp);
2576 print_nl ();
2578 print_dot = addr + TO_ADDR (size);
2581 /* Print an address statement. These are generated by options like
2582 -Ttext. */
2584 static void
2585 print_address_statement (lang_address_statement_type *address)
2587 minfo (_("Address of section %s set to "), address->section_name);
2588 exp_print_tree (address->address);
2589 print_nl ();
2592 /* Print a reloc statement. */
2594 static void
2595 print_reloc_statement (lang_reloc_statement_type *reloc)
2597 int i;
2598 bfd_vma addr;
2599 bfd_size_type size;
2601 init_opb ();
2602 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2603 print_space ();
2605 addr = reloc->output_vma;
2606 if (reloc->output_section != NULL)
2607 addr += reloc->output_section->vma;
2609 size = bfd_get_reloc_size (reloc->howto);
2611 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2613 if (reloc->name != NULL)
2614 minfo ("%s+", reloc->name);
2615 else
2616 minfo ("%s+", reloc->section->name);
2618 exp_print_tree (reloc->addend_exp);
2620 print_nl ();
2622 print_dot = addr + TO_ADDR (size);
2625 static void
2626 print_padding_statement (lang_padding_statement_type *s)
2628 int len;
2629 bfd_vma addr;
2631 init_opb ();
2632 minfo (" *fill*");
2634 len = sizeof " *fill*" - 1;
2635 while (len < SECTION_NAME_MAP_LENGTH)
2637 print_space ();
2638 ++len;
2641 addr = s->output_offset;
2642 if (s->output_section != NULL)
2643 addr += s->output_section->vma;
2644 minfo ("0x%V %W ", addr, (bfd_vma) s->size);
2646 if (s->fill->size != 0)
2648 size_t size;
2649 unsigned char *p;
2650 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2651 fprintf (config.map_file, "%02x", *p);
2654 print_nl ();
2656 print_dot = addr + TO_ADDR (s->size);
2659 static void
2660 print_wild_statement (lang_wild_statement_type *w,
2661 lang_output_section_statement_type *os)
2663 struct wildcard_list *sec;
2665 print_space ();
2667 if (w->filenames_sorted)
2668 minfo ("SORT(");
2669 if (w->filename != NULL)
2670 minfo ("%s", w->filename);
2671 else
2672 minfo ("*");
2673 if (w->filenames_sorted)
2674 minfo (")");
2676 minfo ("(");
2677 for (sec = w->section_list; sec; sec = sec->next)
2679 if (sec->spec.sorted)
2680 minfo ("SORT(");
2681 if (sec->spec.exclude_name_list != NULL)
2683 name_list *tmp;
2684 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2685 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2686 minfo (" %s", tmp->name);
2687 minfo (") ");
2689 if (sec->spec.name != NULL)
2690 minfo ("%s", sec->spec.name);
2691 else
2692 minfo ("*");
2693 if (sec->spec.sorted)
2694 minfo (")");
2695 if (sec->next)
2696 minfo (" ");
2698 minfo (")");
2700 print_nl ();
2702 print_statement_list (w->children.head, os);
2705 /* Print a group statement. */
2707 static void
2708 print_group (lang_group_statement_type *s,
2709 lang_output_section_statement_type *os)
2711 fprintf (config.map_file, "START GROUP\n");
2712 print_statement_list (s->children.head, os);
2713 fprintf (config.map_file, "END GROUP\n");
2716 /* Print the list of statements in S.
2717 This can be called for any statement type. */
2719 static void
2720 print_statement_list (lang_statement_union_type *s,
2721 lang_output_section_statement_type *os)
2723 while (s != NULL)
2725 print_statement (s, os);
2726 s = s->header.next;
2730 /* Print the first statement in statement list S.
2731 This can be called for any statement type. */
2733 static void
2734 print_statement (lang_statement_union_type *s,
2735 lang_output_section_statement_type *os)
2737 switch (s->header.type)
2739 default:
2740 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2741 FAIL ();
2742 break;
2743 case lang_constructors_statement_enum:
2744 if (constructor_list.head != NULL)
2746 if (constructors_sorted)
2747 minfo (" SORT (CONSTRUCTORS)\n");
2748 else
2749 minfo (" CONSTRUCTORS\n");
2750 print_statement_list (constructor_list.head, os);
2752 break;
2753 case lang_wild_statement_enum:
2754 print_wild_statement (&s->wild_statement, os);
2755 break;
2756 case lang_address_statement_enum:
2757 print_address_statement (&s->address_statement);
2758 break;
2759 case lang_object_symbols_statement_enum:
2760 minfo (" CREATE_OBJECT_SYMBOLS\n");
2761 break;
2762 case lang_fill_statement_enum:
2763 print_fill_statement (&s->fill_statement);
2764 break;
2765 case lang_data_statement_enum:
2766 print_data_statement (&s->data_statement);
2767 break;
2768 case lang_reloc_statement_enum:
2769 print_reloc_statement (&s->reloc_statement);
2770 break;
2771 case lang_input_section_enum:
2772 print_input_section (&s->input_section);
2773 break;
2774 case lang_padding_statement_enum:
2775 print_padding_statement (&s->padding_statement);
2776 break;
2777 case lang_output_section_statement_enum:
2778 print_output_section_statement (&s->output_section_statement);
2779 break;
2780 case lang_assignment_statement_enum:
2781 print_assignment (&s->assignment_statement, os);
2782 break;
2783 case lang_target_statement_enum:
2784 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2785 break;
2786 case lang_output_statement_enum:
2787 minfo ("OUTPUT(%s", s->output_statement.name);
2788 if (output_target != NULL)
2789 minfo (" %s", output_target);
2790 minfo (")\n");
2791 break;
2792 case lang_input_statement_enum:
2793 print_input_statement (&s->input_statement);
2794 break;
2795 case lang_group_statement_enum:
2796 print_group (&s->group_statement, os);
2797 break;
2798 case lang_afile_asection_pair_statement_enum:
2799 FAIL ();
2800 break;
2804 static void
2805 print_statements (void)
2807 print_statement_list (statement_list.head, abs_output_section);
2810 /* Print the first N statements in statement list S to STDERR.
2811 If N == 0, nothing is printed.
2812 If N < 0, the entire list is printed.
2813 Intended to be called from GDB. */
2815 void
2816 dprint_statement (lang_statement_union_type *s, int n)
2818 FILE *map_save = config.map_file;
2820 config.map_file = stderr;
2822 if (n < 0)
2823 print_statement_list (s, abs_output_section);
2824 else
2826 while (s && --n >= 0)
2828 print_statement (s, abs_output_section);
2829 s = s->header.next;
2833 config.map_file = map_save;
2836 static void
2837 insert_pad (lang_statement_union_type **ptr,
2838 fill_type *fill,
2839 unsigned int alignment_needed,
2840 asection *output_section,
2841 bfd_vma dot)
2843 static fill_type zero_fill = { 1, { 0 } };
2844 lang_statement_union_type *pad;
2846 pad = ((lang_statement_union_type *)
2847 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2848 if (ptr != &statement_list.head
2849 && pad->header.type == lang_padding_statement_enum
2850 && pad->padding_statement.output_section == output_section)
2852 /* Use the existing pad statement. The above test on output
2853 section is probably redundant, but it doesn't hurt to check. */
2855 else
2857 /* Make a new padding statement, linked into existing chain. */
2858 pad = stat_alloc (sizeof (lang_padding_statement_type));
2859 pad->header.next = *ptr;
2860 *ptr = pad;
2861 pad->header.type = lang_padding_statement_enum;
2862 pad->padding_statement.output_section = output_section;
2863 if (fill == NULL)
2864 fill = &zero_fill;
2865 pad->padding_statement.fill = fill;
2867 pad->padding_statement.output_offset = dot - output_section->vma;
2868 pad->padding_statement.size = alignment_needed;
2869 output_section->size += alignment_needed;
2872 /* Work out how much this section will move the dot point. */
2874 static bfd_vma
2875 size_input_section
2876 (lang_statement_union_type **this_ptr,
2877 lang_output_section_statement_type *output_section_statement,
2878 fill_type *fill,
2879 bfd_vma dot)
2881 lang_input_section_type *is = &((*this_ptr)->input_section);
2882 asection *i = is->section;
2884 if (!is->ifile->just_syms_flag && (i->flags & SEC_EXCLUDE) == 0)
2886 unsigned int alignment_needed;
2887 asection *o;
2889 /* Align this section first to the input sections requirement,
2890 then to the output section's requirement. If this alignment
2891 is greater than any seen before, then record it too. Perform
2892 the alignment by inserting a magic 'padding' statement. */
2894 if (output_section_statement->subsection_alignment != -1)
2895 i->alignment_power = output_section_statement->subsection_alignment;
2897 o = output_section_statement->bfd_section;
2898 if (o->alignment_power < i->alignment_power)
2899 o->alignment_power = i->alignment_power;
2901 alignment_needed = align_power (dot, i->alignment_power) - dot;
2903 if (alignment_needed != 0)
2905 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2906 dot += alignment_needed;
2909 /* Remember where in the output section this input section goes. */
2911 i->output_offset = dot - o->vma;
2913 /* Mark how big the output section must be to contain this now. */
2914 dot += TO_ADDR (i->size);
2915 o->size = TO_SIZE (dot - o->vma);
2917 else
2919 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2922 return dot;
2925 #define IGNORE_SECTION(s) \
2926 (((s->flags & SEC_THREAD_LOCAL) != 0 \
2927 ? (s->flags & (SEC_LOAD | SEC_NEVER_LOAD)) != SEC_LOAD \
2928 : (s->flags & (SEC_ALLOC | SEC_NEVER_LOAD)) != SEC_ALLOC) \
2929 || s->size == 0)
2931 /* Check to see if any allocated sections overlap with other allocated
2932 sections. This can happen when the linker script specifically specifies
2933 the output section addresses of the two sections. */
2935 static void
2936 lang_check_section_addresses (void)
2938 asection *s;
2940 /* Scan all sections in the output list. */
2941 for (s = output_bfd->sections; s != NULL; s = s->next)
2943 asection *os;
2945 /* Ignore sections which are not loaded or which have no contents. */
2946 if (IGNORE_SECTION (s))
2947 continue;
2949 /* Once we reach section 's' stop our seach. This prevents two
2950 warning messages from being produced, one for 'section A overlaps
2951 section B' and one for 'section B overlaps section A'. */
2952 for (os = output_bfd->sections; os != s; os = os->next)
2954 bfd_vma s_start;
2955 bfd_vma s_end;
2956 bfd_vma os_start;
2957 bfd_vma os_end;
2959 /* Only consider loadable sections with real contents. */
2960 if (IGNORE_SECTION (os))
2961 continue;
2963 /* We must check the sections' LMA addresses not their
2964 VMA addresses because overlay sections can have
2965 overlapping VMAs but they must have distinct LMAs. */
2966 s_start = bfd_section_lma (output_bfd, s);
2967 os_start = bfd_section_lma (output_bfd, os);
2968 s_end = s_start + TO_ADDR (s->size) - 1;
2969 os_end = os_start + TO_ADDR (os->size) - 1;
2971 /* Look for an overlap. */
2972 if ((s_end < os_start) || (s_start > os_end))
2973 continue;
2975 einfo (
2976 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2977 s->name, s_start, s_end, os->name, os_start, os_end);
2979 /* Once we have found one overlap for this section,
2980 stop looking for others. */
2981 break;
2986 /* Make sure the new address is within the region. We explicitly permit the
2987 current address to be at the exact end of the region when the address is
2988 non-zero, in case the region is at the end of addressable memory and the
2989 calculation wraps around. */
2991 static void
2992 os_region_check (lang_output_section_statement_type *os,
2993 lang_memory_region_type *region,
2994 etree_type *tree,
2995 bfd_vma base)
2997 if ((region->current < region->origin
2998 || (region->current - region->origin > region->length))
2999 && ((region->current != region->origin + region->length)
3000 || base == 0))
3002 if (tree != NULL)
3004 einfo (_("%X%P: address 0x%v of %B section %s"
3005 " is not within region %s\n"),
3006 region->current,
3007 os->bfd_section->owner,
3008 os->bfd_section->name,
3009 region->name);
3011 else
3013 einfo (_("%X%P: region %s is full (%B section %s)\n"),
3014 region->name,
3015 os->bfd_section->owner,
3016 os->bfd_section->name);
3018 /* Reset the region pointer. */
3019 region->current = region->origin;
3023 /* Set the sizes for all the output sections. */
3025 static bfd_vma
3026 lang_size_sections_1
3027 (lang_statement_union_type *s,
3028 lang_output_section_statement_type *output_section_statement,
3029 lang_statement_union_type **prev,
3030 fill_type *fill,
3031 bfd_vma dot,
3032 bfd_boolean *relax,
3033 bfd_boolean check_regions)
3035 /* Size up the sections from their constituent parts. */
3036 for (; s != NULL; s = s->header.next)
3038 switch (s->header.type)
3040 case lang_output_section_statement_enum:
3042 bfd_vma after;
3043 lang_output_section_statement_type *os;
3045 os = &s->output_section_statement;
3046 if (os->bfd_section == NULL)
3047 /* This section was never actually created. */
3048 break;
3050 /* If this is a COFF shared library section, use the size and
3051 address from the input section. FIXME: This is COFF
3052 specific; it would be cleaner if there were some other way
3053 to do this, but nothing simple comes to mind. */
3054 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
3056 asection *input;
3058 if (os->children.head == NULL
3059 || os->children.head->header.next != NULL
3060 || (os->children.head->header.type
3061 != lang_input_section_enum))
3062 einfo (_("%P%X: Internal error on COFF shared library"
3063 " section %s\n"), os->name);
3065 input = os->children.head->input_section.section;
3066 bfd_set_section_vma (os->bfd_section->owner,
3067 os->bfd_section,
3068 bfd_section_vma (input->owner, input));
3069 os->bfd_section->size = input->size;
3070 break;
3073 if (bfd_is_abs_section (os->bfd_section))
3075 /* No matter what happens, an abs section starts at zero. */
3076 ASSERT (os->bfd_section->vma == 0);
3078 else
3080 if (os->addr_tree == NULL)
3082 /* No address specified for this section, get one
3083 from the region specification. */
3084 if (os->region == NULL
3085 || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
3086 && os->region->name[0] == '*'
3087 && strcmp (os->region->name,
3088 DEFAULT_MEMORY_REGION) == 0))
3090 os->region = lang_memory_default (os->bfd_section);
3093 /* If a loadable section is using the default memory
3094 region, and some non default memory regions were
3095 defined, issue an error message. */
3096 if (!IGNORE_SECTION (os->bfd_section)
3097 && ! link_info.relocatable
3098 && check_regions
3099 && strcmp (os->region->name,
3100 DEFAULT_MEMORY_REGION) == 0
3101 && lang_memory_region_list != NULL
3102 && (strcmp (lang_memory_region_list->name,
3103 DEFAULT_MEMORY_REGION) != 0
3104 || lang_memory_region_list->next != NULL))
3106 /* By default this is an error rather than just a
3107 warning because if we allocate the section to the
3108 default memory region we can end up creating an
3109 excessively large binary, or even seg faulting when
3110 attempting to perform a negative seek. See
3111 sources.redhat.com/ml/binutils/2003-04/msg00423.html
3112 for an example of this. This behaviour can be
3113 overridden by the using the --no-check-sections
3114 switch. */
3115 if (command_line.check_section_addresses)
3116 einfo (_("%P%F: error: no memory region specified"
3117 " for loadable section `%s'\n"),
3118 bfd_get_section_name (output_bfd,
3119 os->bfd_section));
3120 else
3121 einfo (_("%P: warning: no memory region specified"
3122 " for loadable section `%s'\n"),
3123 bfd_get_section_name (output_bfd,
3124 os->bfd_section));
3127 dot = os->region->current;
3129 if (os->section_alignment == -1)
3131 bfd_vma olddot;
3133 olddot = dot;
3134 dot = align_power (dot,
3135 os->bfd_section->alignment_power);
3137 if (dot != olddot && config.warn_section_align)
3138 einfo (_("%P: warning: changing start of section"
3139 " %s by %u bytes\n"),
3140 os->name, (unsigned int) (dot - olddot));
3143 else
3145 etree_value_type r;
3147 os->processed = -1;
3148 r = exp_fold_tree (os->addr_tree,
3149 abs_output_section,
3150 lang_allocating_phase_enum,
3151 dot, &dot);
3152 os->processed = 0;
3154 if (!r.valid_p)
3155 einfo (_("%F%S: non constant or forward reference"
3156 " address expression for section %s\n"),
3157 os->name);
3159 dot = r.value + r.section->bfd_section->vma;
3162 /* The section starts here.
3163 First, align to what the section needs. */
3165 if (os->section_alignment != -1)
3166 dot = align_power (dot, os->section_alignment);
3168 bfd_set_section_vma (0, os->bfd_section, dot);
3170 os->bfd_section->output_offset = 0;
3173 lang_size_sections_1 (os->children.head, os, &os->children.head,
3174 os->fill, dot, relax, check_regions);
3176 /* Put the section within the requested block size, or
3177 align at the block boundary. */
3178 after = ((os->bfd_section->vma
3179 + TO_ADDR (os->bfd_section->size)
3180 + os->block_value - 1)
3181 & - (bfd_vma) os->block_value);
3183 if (bfd_is_abs_section (os->bfd_section))
3184 ASSERT (after == os->bfd_section->vma);
3185 else
3186 os->bfd_section->size
3187 = TO_SIZE (after - os->bfd_section->vma);
3189 dot = os->bfd_section->vma;
3190 /* .tbss sections effectively have zero size. */
3191 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3192 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3193 || link_info.relocatable)
3194 dot += TO_ADDR (os->bfd_section->size);
3196 os->processed = 1;
3198 if (os->update_dot_tree != 0)
3199 exp_fold_tree (os->update_dot_tree, abs_output_section,
3200 lang_allocating_phase_enum, dot, &dot);
3202 /* Update dot in the region ?
3203 We only do this if the section is going to be allocated,
3204 since unallocated sections do not contribute to the region's
3205 overall size in memory.
3207 If the SEC_NEVER_LOAD bit is not set, it will affect the
3208 addresses of sections after it. We have to update
3209 dot. */
3210 if (os->region != NULL
3211 && ((os->bfd_section->flags & SEC_NEVER_LOAD) == 0
3212 || (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))))
3214 os->region->current = dot;
3216 if (check_regions)
3217 /* Make sure the new address is within the region. */
3218 os_region_check (os, os->region, os->addr_tree,
3219 os->bfd_section->vma);
3221 /* If there's no load address specified, use the run
3222 region as the load region. */
3223 if (os->lma_region == NULL && os->load_base == NULL)
3224 os->lma_region = os->region;
3226 if (os->lma_region != NULL && os->lma_region != os->region)
3228 /* Set load_base, which will be handled later. */
3229 os->load_base = exp_intop (os->lma_region->current);
3230 os->lma_region->current +=
3231 TO_ADDR (os->bfd_section->size);
3232 if (check_regions)
3233 os_region_check (os, os->lma_region, NULL,
3234 os->bfd_section->lma);
3238 break;
3240 case lang_constructors_statement_enum:
3241 dot = lang_size_sections_1 (constructor_list.head,
3242 output_section_statement,
3243 &s->wild_statement.children.head,
3244 fill, dot, relax, check_regions);
3245 break;
3247 case lang_data_statement_enum:
3249 unsigned int size = 0;
3251 s->data_statement.output_vma =
3252 dot - output_section_statement->bfd_section->vma;
3253 s->data_statement.output_section =
3254 output_section_statement->bfd_section;
3256 /* We might refer to provided symbols in the expression, and
3257 need to mark them as needed. */
3258 exp_fold_tree (s->data_statement.exp, abs_output_section,
3259 lang_allocating_phase_enum, dot, &dot);
3261 switch (s->data_statement.type)
3263 default:
3264 abort ();
3265 case QUAD:
3266 case SQUAD:
3267 size = QUAD_SIZE;
3268 break;
3269 case LONG:
3270 size = LONG_SIZE;
3271 break;
3272 case SHORT:
3273 size = SHORT_SIZE;
3274 break;
3275 case BYTE:
3276 size = BYTE_SIZE;
3277 break;
3279 if (size < TO_SIZE ((unsigned) 1))
3280 size = TO_SIZE ((unsigned) 1);
3281 dot += TO_ADDR (size);
3282 output_section_statement->bfd_section->size += size;
3283 /* The output section gets contents, and then we inspect for
3284 any flags set in the input script which override any ALLOC. */
3285 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3286 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3288 output_section_statement->bfd_section->flags |=
3289 SEC_ALLOC | SEC_LOAD;
3292 break;
3294 case lang_reloc_statement_enum:
3296 int size;
3298 s->reloc_statement.output_vma =
3299 dot - output_section_statement->bfd_section->vma;
3300 s->reloc_statement.output_section =
3301 output_section_statement->bfd_section;
3302 size = bfd_get_reloc_size (s->reloc_statement.howto);
3303 dot += TO_ADDR (size);
3304 output_section_statement->bfd_section->size += size;
3306 break;
3308 case lang_wild_statement_enum:
3310 dot = lang_size_sections_1 (s->wild_statement.children.head,
3311 output_section_statement,
3312 &s->wild_statement.children.head,
3313 fill, dot, relax, check_regions);
3315 break;
3317 case lang_object_symbols_statement_enum:
3318 link_info.create_object_symbols_section =
3319 output_section_statement->bfd_section;
3320 break;
3321 case lang_output_statement_enum:
3322 case lang_target_statement_enum:
3323 break;
3324 case lang_input_section_enum:
3326 asection *i;
3328 i = (*prev)->input_section.section;
3329 if (relax)
3331 bfd_boolean again;
3333 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3334 einfo (_("%P%F: can't relax section: %E\n"));
3335 if (again)
3336 *relax = TRUE;
3338 dot = size_input_section (prev, output_section_statement,
3339 output_section_statement->fill, dot);
3341 break;
3342 case lang_input_statement_enum:
3343 break;
3344 case lang_fill_statement_enum:
3345 s->fill_statement.output_section =
3346 output_section_statement->bfd_section;
3348 fill = s->fill_statement.fill;
3349 break;
3350 case lang_assignment_statement_enum:
3352 bfd_vma newdot = dot;
3354 exp_fold_tree (s->assignment_statement.exp,
3355 output_section_statement,
3356 lang_allocating_phase_enum,
3357 dot,
3358 &newdot);
3360 if (newdot != dot)
3362 if (output_section_statement == abs_output_section)
3364 /* If we don't have an output section, then just adjust
3365 the default memory address. */
3366 lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
3367 FALSE)->current = newdot;
3369 else
3371 /* Insert a pad after this statement. We can't
3372 put the pad before when relaxing, in case the
3373 assignment references dot. */
3374 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3375 output_section_statement->bfd_section, dot);
3377 /* Don't neuter the pad below when relaxing. */
3378 s = s->header.next;
3381 /* If dot is advanced, this implies that the section should
3382 have space allocated to it, unless the user has explicitly
3383 stated that the section should never be loaded. */
3384 if (!(output_section_statement->flags
3385 & (SEC_NEVER_LOAD | SEC_ALLOC)))
3386 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3388 dot = newdot;
3391 break;
3393 case lang_padding_statement_enum:
3394 /* If this is the first time lang_size_sections is called,
3395 we won't have any padding statements. If this is the
3396 second or later passes when relaxing, we should allow
3397 padding to shrink. If padding is needed on this pass, it
3398 will be added back in. */
3399 s->padding_statement.size = 0;
3401 /* Make sure output_offset is valid. If relaxation shrinks
3402 the section and this pad isn't needed, it's possible to
3403 have output_offset larger than the final size of the
3404 section. bfd_set_section_contents will complain even for
3405 a pad size of zero. */
3406 s->padding_statement.output_offset
3407 = dot - output_section_statement->bfd_section->vma;
3408 break;
3410 case lang_group_statement_enum:
3411 dot = lang_size_sections_1 (s->group_statement.children.head,
3412 output_section_statement,
3413 &s->group_statement.children.head,
3414 fill, dot, relax, check_regions);
3415 break;
3417 default:
3418 FAIL ();
3419 break;
3421 /* We can only get here when relaxing is turned on. */
3422 case lang_address_statement_enum:
3423 break;
3425 prev = &s->header.next;
3427 return dot;
3430 bfd_vma
3431 lang_size_sections
3432 (lang_statement_union_type *s,
3433 lang_output_section_statement_type *output_section_statement,
3434 lang_statement_union_type **prev,
3435 fill_type *fill,
3436 bfd_vma dot,
3437 bfd_boolean *relax,
3438 bfd_boolean check_regions)
3440 bfd_vma result;
3442 /* Callers of exp_fold_tree need to increment this. */
3443 lang_statement_iteration++;
3445 exp_data_seg.phase = exp_dataseg_none;
3446 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3447 dot, relax, check_regions);
3448 if (exp_data_seg.phase == exp_dataseg_end_seen
3449 && link_info.relro && exp_data_seg.relro_end)
3451 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END pair was seen, try
3452 to put exp_data_seg.relro on a (common) page boundary. */
3454 exp_data_seg.phase = exp_dataseg_relro_adjust;
3455 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3456 dot, relax, check_regions);
3457 link_info.relro_start = exp_data_seg.base;
3458 link_info.relro_end = exp_data_seg.relro_end;
3460 else if (exp_data_seg.phase == exp_dataseg_end_seen)
3462 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3463 a page could be saved in the data segment. */
3464 bfd_vma first, last;
3466 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3467 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3468 if (first && last
3469 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3470 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3471 && first + last <= exp_data_seg.pagesize)
3473 exp_data_seg.phase = exp_dataseg_adjust;
3474 lang_statement_iteration++;
3475 result = lang_size_sections_1 (s, output_section_statement, prev,
3476 fill, dot, relax, check_regions);
3480 return result;
3483 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3485 static bfd_vma
3486 lang_do_assignments_1
3487 (lang_statement_union_type *s,
3488 lang_output_section_statement_type *output_section_statement,
3489 fill_type *fill,
3490 bfd_vma dot)
3492 for (; s != NULL; s = s->header.next)
3494 switch (s->header.type)
3496 case lang_constructors_statement_enum:
3497 dot = lang_do_assignments_1 (constructor_list.head,
3498 output_section_statement,
3499 fill,
3500 dot);
3501 break;
3503 case lang_output_section_statement_enum:
3505 lang_output_section_statement_type *os;
3507 os = &(s->output_section_statement);
3508 if (os->bfd_section != NULL)
3510 dot = os->bfd_section->vma;
3511 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3512 /* .tbss sections effectively have zero size. */
3513 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3514 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3515 || link_info.relocatable)
3516 dot += TO_ADDR (os->bfd_section->size);
3518 if (os->load_base)
3520 /* If nothing has been placed into the output section then
3521 it won't have a bfd_section. */
3522 if (os->bfd_section)
3524 os->bfd_section->lma
3525 = exp_get_abs_int (os->load_base, 0, "load base",
3526 lang_final_phase_enum);
3530 break;
3531 case lang_wild_statement_enum:
3533 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3534 output_section_statement,
3535 fill, dot);
3537 break;
3539 case lang_object_symbols_statement_enum:
3540 case lang_output_statement_enum:
3541 case lang_target_statement_enum:
3542 #if 0
3543 case lang_common_statement_enum:
3544 #endif
3545 break;
3546 case lang_data_statement_enum:
3548 etree_value_type value;
3550 value = exp_fold_tree (s->data_statement.exp,
3551 abs_output_section,
3552 lang_final_phase_enum, dot, &dot);
3553 if (!value.valid_p)
3554 einfo (_("%F%P: invalid data statement\n"));
3555 s->data_statement.value
3556 = value.value + value.section->bfd_section->vma;
3559 unsigned int size;
3560 switch (s->data_statement.type)
3562 default:
3563 abort ();
3564 case QUAD:
3565 case SQUAD:
3566 size = QUAD_SIZE;
3567 break;
3568 case LONG:
3569 size = LONG_SIZE;
3570 break;
3571 case SHORT:
3572 size = SHORT_SIZE;
3573 break;
3574 case BYTE:
3575 size = BYTE_SIZE;
3576 break;
3578 if (size < TO_SIZE ((unsigned) 1))
3579 size = TO_SIZE ((unsigned) 1);
3580 dot += TO_ADDR (size);
3582 break;
3584 case lang_reloc_statement_enum:
3586 etree_value_type value;
3588 value = exp_fold_tree (s->reloc_statement.addend_exp,
3589 abs_output_section,
3590 lang_final_phase_enum, dot, &dot);
3591 s->reloc_statement.addend_value = value.value;
3592 if (!value.valid_p)
3593 einfo (_("%F%P: invalid reloc statement\n"));
3595 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3596 break;
3598 case lang_input_section_enum:
3600 asection *in = s->input_section.section;
3602 if ((in->flags & SEC_EXCLUDE) == 0)
3603 dot += TO_ADDR (in->size);
3605 break;
3607 case lang_input_statement_enum:
3608 break;
3609 case lang_fill_statement_enum:
3610 fill = s->fill_statement.fill;
3611 break;
3612 case lang_assignment_statement_enum:
3614 exp_fold_tree (s->assignment_statement.exp,
3615 output_section_statement,
3616 lang_final_phase_enum,
3617 dot,
3618 &dot);
3621 break;
3622 case lang_padding_statement_enum:
3623 dot += TO_ADDR (s->padding_statement.size);
3624 break;
3626 case lang_group_statement_enum:
3627 dot = lang_do_assignments_1 (s->group_statement.children.head,
3628 output_section_statement,
3629 fill, dot);
3631 break;
3633 default:
3634 FAIL ();
3635 break;
3636 case lang_address_statement_enum:
3637 break;
3641 return dot;
3644 void
3645 lang_do_assignments
3646 (lang_statement_union_type *s,
3647 lang_output_section_statement_type *output_section_statement,
3648 fill_type *fill,
3649 bfd_vma dot)
3651 /* Callers of exp_fold_tree need to increment this. */
3652 lang_statement_iteration++;
3653 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3656 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3657 operator .startof. (section_name), it produces an undefined symbol
3658 .startof.section_name. Similarly, when it sees
3659 .sizeof. (section_name), it produces an undefined symbol
3660 .sizeof.section_name. For all the output sections, we look for
3661 such symbols, and set them to the correct value. */
3663 static void
3664 lang_set_startof (void)
3666 asection *s;
3668 if (link_info.relocatable)
3669 return;
3671 for (s = output_bfd->sections; s != NULL; s = s->next)
3673 const char *secname;
3674 char *buf;
3675 struct bfd_link_hash_entry *h;
3677 secname = bfd_get_section_name (output_bfd, s);
3678 buf = xmalloc (10 + strlen (secname));
3680 sprintf (buf, ".startof.%s", secname);
3681 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3682 if (h != NULL && h->type == bfd_link_hash_undefined)
3684 h->type = bfd_link_hash_defined;
3685 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3686 h->u.def.section = bfd_abs_section_ptr;
3689 sprintf (buf, ".sizeof.%s", secname);
3690 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3691 if (h != NULL && h->type == bfd_link_hash_undefined)
3693 h->type = bfd_link_hash_defined;
3694 h->u.def.value = TO_ADDR (s->size);
3695 h->u.def.section = bfd_abs_section_ptr;
3698 free (buf);
3702 static void
3703 lang_finish (void)
3705 struct bfd_link_hash_entry *h;
3706 bfd_boolean warn;
3708 if (link_info.relocatable || link_info.shared)
3709 warn = FALSE;
3710 else
3711 warn = TRUE;
3713 if (entry_symbol.name == NULL)
3715 /* No entry has been specified. Look for start, but don't warn
3716 if we don't find it. */
3717 entry_symbol.name = "start";
3718 warn = FALSE;
3721 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3722 FALSE, FALSE, TRUE);
3723 if (h != NULL
3724 && (h->type == bfd_link_hash_defined
3725 || h->type == bfd_link_hash_defweak)
3726 && h->u.def.section->output_section != NULL)
3728 bfd_vma val;
3730 val = (h->u.def.value
3731 + bfd_get_section_vma (output_bfd,
3732 h->u.def.section->output_section)
3733 + h->u.def.section->output_offset);
3734 if (! bfd_set_start_address (output_bfd, val))
3735 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3737 else
3739 bfd_vma val;
3740 const char *send;
3742 /* We couldn't find the entry symbol. Try parsing it as a
3743 number. */
3744 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3745 if (*send == '\0')
3747 if (! bfd_set_start_address (output_bfd, val))
3748 einfo (_("%P%F: can't set start address\n"));
3750 else
3752 asection *ts;
3754 /* Can't find the entry symbol, and it's not a number. Use
3755 the first address in the text section. */
3756 ts = bfd_get_section_by_name (output_bfd, entry_section);
3757 if (ts != NULL)
3759 if (warn)
3760 einfo (_("%P: warning: cannot find entry symbol %s;"
3761 " defaulting to %V\n"),
3762 entry_symbol.name,
3763 bfd_get_section_vma (output_bfd, ts));
3764 if (! bfd_set_start_address (output_bfd,
3765 bfd_get_section_vma (output_bfd,
3766 ts)))
3767 einfo (_("%P%F: can't set start address\n"));
3769 else
3771 if (warn)
3772 einfo (_("%P: warning: cannot find entry symbol %s;"
3773 " not setting start address\n"),
3774 entry_symbol.name);
3779 /* Don't bfd_hash_table_free (&lang_definedness_table);
3780 map file output may result in a call of lang_track_definedness. */
3783 /* This is a small function used when we want to ignore errors from
3784 BFD. */
3786 static void
3787 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3789 /* Don't do anything. */
3792 /* Check that the architecture of all the input files is compatible
3793 with the output file. Also call the backend to let it do any
3794 other checking that is needed. */
3796 static void
3797 lang_check (void)
3799 lang_statement_union_type *file;
3800 bfd *input_bfd;
3801 const bfd_arch_info_type *compatible;
3803 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3805 input_bfd = file->input_statement.the_bfd;
3806 compatible
3807 = bfd_arch_get_compatible (input_bfd, output_bfd,
3808 command_line.accept_unknown_input_arch);
3810 /* In general it is not possible to perform a relocatable
3811 link between differing object formats when the input
3812 file has relocations, because the relocations in the
3813 input format may not have equivalent representations in
3814 the output format (and besides BFD does not translate
3815 relocs for other link purposes than a final link). */
3816 if ((link_info.relocatable || link_info.emitrelocations)
3817 && (compatible == NULL
3818 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3819 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3821 einfo (_("%P%F: Relocatable linking with relocations from"
3822 " format %s (%B) to format %s (%B) is not supported\n"),
3823 bfd_get_target (input_bfd), input_bfd,
3824 bfd_get_target (output_bfd), output_bfd);
3825 /* einfo with %F exits. */
3828 if (compatible == NULL)
3830 if (command_line.warn_mismatch)
3831 einfo (_("%P: warning: %s architecture of input file `%B'"
3832 " is incompatible with %s output\n"),
3833 bfd_printable_name (input_bfd), input_bfd,
3834 bfd_printable_name (output_bfd));
3836 else if (bfd_count_sections (input_bfd))
3838 /* If the input bfd has no contents, it shouldn't set the
3839 private data of the output bfd. */
3841 bfd_error_handler_type pfn = NULL;
3843 /* If we aren't supposed to warn about mismatched input
3844 files, temporarily set the BFD error handler to a
3845 function which will do nothing. We still want to call
3846 bfd_merge_private_bfd_data, since it may set up
3847 information which is needed in the output file. */
3848 if (! command_line.warn_mismatch)
3849 pfn = bfd_set_error_handler (ignore_bfd_errors);
3850 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3852 if (command_line.warn_mismatch)
3853 einfo (_("%P%X: failed to merge target specific data"
3854 " of file %B\n"), input_bfd);
3856 if (! command_line.warn_mismatch)
3857 bfd_set_error_handler (pfn);
3862 /* Look through all the global common symbols and attach them to the
3863 correct section. The -sort-common command line switch may be used
3864 to roughly sort the entries by size. */
3866 static void
3867 lang_common (void)
3869 if (command_line.inhibit_common_definition)
3870 return;
3871 if (link_info.relocatable
3872 && ! command_line.force_common_definition)
3873 return;
3875 if (! config.sort_common)
3876 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3877 else
3879 int power;
3881 for (power = 4; power >= 0; power--)
3882 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3886 /* Place one common symbol in the correct section. */
3888 static bfd_boolean
3889 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3891 unsigned int power_of_two;
3892 bfd_vma size;
3893 asection *section;
3895 if (h->type != bfd_link_hash_common)
3896 return TRUE;
3898 size = h->u.c.size;
3899 power_of_two = h->u.c.p->alignment_power;
3901 if (config.sort_common
3902 && power_of_two < (unsigned int) *(int *) info)
3903 return TRUE;
3905 section = h->u.c.p->section;
3907 /* Increase the size of the section to align the common sym. */
3908 section->size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3909 section->size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3911 /* Adjust the alignment if necessary. */
3912 if (power_of_two > section->alignment_power)
3913 section->alignment_power = power_of_two;
3915 /* Change the symbol from common to defined. */
3916 h->type = bfd_link_hash_defined;
3917 h->u.def.section = section;
3918 h->u.def.value = section->size;
3920 /* Increase the size of the section. */
3921 section->size += size;
3923 /* Make sure the section is allocated in memory, and make sure that
3924 it is no longer a common section. */
3925 section->flags |= SEC_ALLOC;
3926 section->flags &= ~SEC_IS_COMMON;
3928 if (config.map_file != NULL)
3930 static bfd_boolean header_printed;
3931 int len;
3932 char *name;
3933 char buf[50];
3935 if (! header_printed)
3937 minfo (_("\nAllocating common symbols\n"));
3938 minfo (_("Common symbol size file\n\n"));
3939 header_printed = TRUE;
3942 name = demangle (h->root.string);
3943 minfo ("%s", name);
3944 len = strlen (name);
3945 free (name);
3947 if (len >= 19)
3949 print_nl ();
3950 len = 0;
3952 while (len < 20)
3954 print_space ();
3955 ++len;
3958 minfo ("0x");
3959 if (size <= 0xffffffff)
3960 sprintf (buf, "%lx", (unsigned long) size);
3961 else
3962 sprintf_vma (buf, size);
3963 minfo ("%s", buf);
3964 len = strlen (buf);
3966 while (len < 16)
3968 print_space ();
3969 ++len;
3972 minfo ("%B\n", section->owner);
3975 return TRUE;
3978 /* Run through the input files and ensure that every input section has
3979 somewhere to go. If one is found without a destination then create
3980 an input request and place it into the statement tree. */
3982 static void
3983 lang_place_orphans (void)
3985 LANG_FOR_EACH_INPUT_STATEMENT (file)
3987 asection *s;
3989 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3991 if (s->output_section == NULL)
3993 /* This section of the file is not attached, root
3994 around for a sensible place for it to go. */
3996 if (file->just_syms_flag)
3997 abort ();
3999 if ((s->flags & SEC_EXCLUDE) != 0)
4000 s->output_section = bfd_abs_section_ptr;
4001 else if (strcmp (s->name, "COMMON") == 0)
4003 /* This is a lonely common section which must have
4004 come from an archive. We attach to the section
4005 with the wildcard. */
4006 if (! link_info.relocatable
4007 || command_line.force_common_definition)
4009 if (default_common_section == NULL)
4011 #if 0
4012 /* This message happens when using the
4013 svr3.ifile linker script, so I have
4014 disabled it. */
4015 info_msg (_("%P: no [COMMON] command,"
4016 " defaulting to .bss\n"));
4017 #endif
4018 default_common_section =
4019 lang_output_section_statement_lookup (".bss");
4022 lang_add_section (&default_common_section->children, s,
4023 default_common_section, file);
4026 else if (ldemul_place_orphan (file, s))
4028 else
4030 lang_output_section_statement_type *os;
4032 os = lang_output_section_statement_lookup (s->name);
4033 lang_add_section (&os->children, s, os, file);
4040 void
4041 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
4043 flagword *ptr_flags;
4045 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
4046 while (*flags)
4048 switch (*flags)
4050 case 'A': case 'a':
4051 *ptr_flags |= SEC_ALLOC;
4052 break;
4054 case 'R': case 'r':
4055 *ptr_flags |= SEC_READONLY;
4056 break;
4058 case 'W': case 'w':
4059 *ptr_flags |= SEC_DATA;
4060 break;
4062 case 'X': case 'x':
4063 *ptr_flags |= SEC_CODE;
4064 break;
4066 case 'L': case 'l':
4067 case 'I': case 'i':
4068 *ptr_flags |= SEC_LOAD;
4069 break;
4071 default:
4072 einfo (_("%P%F: invalid syntax in flags\n"));
4073 break;
4075 flags++;
4079 /* Call a function on each input file. This function will be called
4080 on an archive, but not on the elements. */
4082 void
4083 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
4085 lang_input_statement_type *f;
4087 for (f = (lang_input_statement_type *) input_file_chain.head;
4088 f != NULL;
4089 f = (lang_input_statement_type *) f->next_real_file)
4090 func (f);
4093 /* Call a function on each file. The function will be called on all
4094 the elements of an archive which are included in the link, but will
4095 not be called on the archive file itself. */
4097 void
4098 lang_for_each_file (void (*func) (lang_input_statement_type *))
4100 LANG_FOR_EACH_INPUT_STATEMENT (f)
4102 func (f);
4106 void
4107 ldlang_add_file (lang_input_statement_type *entry)
4109 bfd **pp;
4111 lang_statement_append (&file_chain,
4112 (lang_statement_union_type *) entry,
4113 &entry->next);
4115 /* The BFD linker needs to have a list of all input BFDs involved in
4116 a link. */
4117 ASSERT (entry->the_bfd->link_next == NULL);
4118 ASSERT (entry->the_bfd != output_bfd);
4119 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
4121 *pp = entry->the_bfd;
4122 entry->the_bfd->usrdata = entry;
4123 bfd_set_gp_size (entry->the_bfd, g_switch_value);
4125 /* Look through the sections and check for any which should not be
4126 included in the link. We need to do this now, so that we can
4127 notice when the backend linker tries to report multiple
4128 definition errors for symbols which are in sections we aren't
4129 going to link. FIXME: It might be better to entirely ignore
4130 symbols which are defined in sections which are going to be
4131 discarded. This would require modifying the backend linker for
4132 each backend which might set the SEC_LINK_ONCE flag. If we do
4133 this, we should probably handle SEC_EXCLUDE in the same way. */
4135 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
4138 void
4139 lang_add_output (const char *name, int from_script)
4141 /* Make -o on command line override OUTPUT in script. */
4142 if (!had_output_filename || !from_script)
4144 output_filename = name;
4145 had_output_filename = TRUE;
4149 static lang_output_section_statement_type *current_section;
4151 static int
4152 topower (int x)
4154 unsigned int i = 1;
4155 int l;
4157 if (x < 0)
4158 return -1;
4160 for (l = 0; l < 32; l++)
4162 if (i >= (unsigned int) x)
4163 return l;
4164 i <<= 1;
4167 return 0;
4170 lang_output_section_statement_type *
4171 lang_enter_output_section_statement (const char *output_section_statement_name,
4172 etree_type *address_exp,
4173 enum section_type sectype,
4174 etree_type *align,
4175 etree_type *subalign,
4176 etree_type *ebase,
4177 int constraint)
4179 lang_output_section_statement_type *os;
4181 current_section =
4182 os =
4183 lang_output_section_statement_lookup_1 (output_section_statement_name,
4184 constraint);
4186 /* Add this statement to tree. */
4187 #if 0
4188 add_statement (lang_output_section_statement_enum,
4189 output_section_statement);
4190 #endif
4191 /* Make next things chain into subchain of this. */
4193 if (os->addr_tree == NULL)
4195 os->addr_tree = address_exp;
4197 os->sectype = sectype;
4198 if (sectype != noload_section)
4199 os->flags = SEC_NO_FLAGS;
4200 else
4201 os->flags = SEC_NEVER_LOAD;
4202 os->block_value = 1;
4203 stat_ptr = &os->children;
4205 os->subsection_alignment =
4206 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4207 os->section_alignment =
4208 topower (exp_get_value_int (align, -1, "section alignment", 0));
4210 os->load_base = ebase;
4211 return os;
4214 void
4215 lang_final (void)
4217 lang_output_statement_type *new =
4218 new_stat (lang_output_statement, stat_ptr);
4220 new->name = output_filename;
4223 /* Reset the current counters in the regions. */
4225 void
4226 lang_reset_memory_regions (void)
4228 lang_memory_region_type *p = lang_memory_region_list;
4229 asection *o;
4231 for (p = lang_memory_region_list; p != NULL; p = p->next)
4233 p->old_length = (bfd_size_type) (p->current - p->origin);
4234 p->current = p->origin;
4237 for (o = output_bfd->sections; o != NULL; o = o->next)
4238 o->size = 0;
4241 /* Worker for lang_gc_sections_1. */
4243 static void
4244 gc_section_callback (lang_wild_statement_type *ptr,
4245 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4246 asection *section,
4247 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4248 void *data ATTRIBUTE_UNUSED)
4250 /* If the wild pattern was marked KEEP, the member sections
4251 should be as well. */
4252 if (ptr->keep_sections)
4253 section->flags |= SEC_KEEP;
4256 /* Iterate over sections marking them against GC. */
4258 static void
4259 lang_gc_sections_1 (lang_statement_union_type *s)
4261 for (; s != NULL; s = s->header.next)
4263 switch (s->header.type)
4265 case lang_wild_statement_enum:
4266 walk_wild (&s->wild_statement, gc_section_callback, NULL);
4267 break;
4268 case lang_constructors_statement_enum:
4269 lang_gc_sections_1 (constructor_list.head);
4270 break;
4271 case lang_output_section_statement_enum:
4272 lang_gc_sections_1 (s->output_section_statement.children.head);
4273 break;
4274 case lang_group_statement_enum:
4275 lang_gc_sections_1 (s->group_statement.children.head);
4276 break;
4277 default:
4278 break;
4283 static void
4284 lang_gc_sections (void)
4286 struct bfd_link_hash_entry *h;
4287 ldlang_undef_chain_list_type *ulist;
4289 /* Keep all sections so marked in the link script. */
4291 lang_gc_sections_1 (statement_list.head);
4293 /* Keep all sections containing symbols undefined on the command-line,
4294 and the section containing the entry symbol. */
4296 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4298 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4299 FALSE, FALSE, FALSE);
4301 if (h != NULL
4302 && (h->type == bfd_link_hash_defined
4303 || h->type == bfd_link_hash_defweak)
4304 && ! bfd_is_abs_section (h->u.def.section))
4306 h->u.def.section->flags |= SEC_KEEP;
4310 /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
4311 the special case of debug info. (See bfd/stabs.c)
4312 Twiddle the flag here, to simplify later linker code. */
4313 if (link_info.relocatable)
4315 LANG_FOR_EACH_INPUT_STATEMENT (f)
4317 asection *sec;
4318 for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
4319 if ((sec->flags & SEC_DEBUGGING) == 0)
4320 sec->flags &= ~SEC_EXCLUDE;
4324 if (command_line.gc_sections)
4325 bfd_gc_sections (output_bfd, &link_info);
4328 void
4329 lang_process (void)
4331 lang_reasonable_defaults ();
4332 current_target = default_target;
4334 /* Open the output file. */
4335 lang_for_each_statement (ldlang_open_output);
4336 init_opb ();
4338 ldemul_create_output_section_statements ();
4340 /* Add to the hash table all undefineds on the command line. */
4341 lang_place_undefineds ();
4343 already_linked_table_init ();
4345 /* Create a bfd for each input file. */
4346 current_target = default_target;
4347 open_input_bfds (statement_list.head, FALSE);
4349 link_info.gc_sym_list = &entry_symbol;
4350 if (entry_symbol.name == NULL)
4351 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4353 ldemul_after_open ();
4355 already_linked_table_free ();
4357 /* Make sure that we're not mixing architectures. We call this
4358 after all the input files have been opened, but before we do any
4359 other processing, so that any operations merge_private_bfd_data
4360 does on the output file will be known during the rest of the
4361 link. */
4362 lang_check ();
4364 /* Handle .exports instead of a version script if we're told to do so. */
4365 if (command_line.version_exports_section)
4366 lang_do_version_exports_section ();
4368 /* Build all sets based on the information gathered from the input
4369 files. */
4370 ldctor_build_sets ();
4372 /* Remove unreferenced sections if asked to. */
4373 lang_gc_sections ();
4375 /* Size up the common data. */
4376 lang_common ();
4378 /* Run through the contours of the script and attach input sections
4379 to the correct output sections. */
4380 map_input_to_output_sections (statement_list.head, NULL, NULL);
4382 /* Find any sections not attached explicitly and handle them. */
4383 lang_place_orphans ();
4385 if (! link_info.relocatable)
4387 asection *found;
4389 /* Merge SEC_MERGE sections. This has to be done after GC of
4390 sections, so that GCed sections are not merged, but before
4391 assigning dynamic symbols, since removing whole input sections
4392 is hard then. */
4393 bfd_merge_sections (output_bfd, &link_info);
4395 /* Look for a text section and set the readonly attribute in it. */
4396 found = bfd_get_section_by_name (output_bfd, ".text");
4398 if (found != NULL)
4400 if (config.text_read_only)
4401 found->flags |= SEC_READONLY;
4402 else
4403 found->flags &= ~SEC_READONLY;
4407 /* Do anything special before sizing sections. This is where ELF
4408 and other back-ends size dynamic sections. */
4409 ldemul_before_allocation ();
4411 if (!link_info.relocatable)
4412 strip_excluded_output_sections ();
4414 /* We must record the program headers before we try to fix the
4415 section positions, since they will affect SIZEOF_HEADERS. */
4416 lang_record_phdrs ();
4418 /* Size up the sections. */
4419 lang_size_sections (statement_list.head, abs_output_section,
4420 &statement_list.head, 0, 0, NULL,
4421 command_line.relax ? FALSE : TRUE);
4423 /* Now run around and relax if we can. */
4424 if (command_line.relax)
4426 /* Keep relaxing until bfd_relax_section gives up. */
4427 bfd_boolean relax_again;
4431 relax_again = FALSE;
4433 /* Note: pe-dll.c does something like this also. If you find
4434 you need to change this code, you probably need to change
4435 pe-dll.c also. DJ */
4437 /* Do all the assignments with our current guesses as to
4438 section sizes. */
4439 lang_do_assignments (statement_list.head, abs_output_section,
4440 NULL, 0);
4442 /* We must do this after lang_do_assignments, because it uses
4443 size. */
4444 lang_reset_memory_regions ();
4446 /* Perform another relax pass - this time we know where the
4447 globals are, so can make a better guess. */
4448 lang_size_sections (statement_list.head, abs_output_section,
4449 &statement_list.head, 0, 0, &relax_again, FALSE);
4451 /* If the normal relax is done and the relax finalize pass
4452 is not performed yet, we perform another relax pass. */
4453 if (!relax_again && link_info.need_relax_finalize)
4455 link_info.need_relax_finalize = FALSE;
4456 relax_again = TRUE;
4459 while (relax_again);
4461 /* Final extra sizing to report errors. */
4462 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4463 lang_reset_memory_regions ();
4464 lang_size_sections (statement_list.head, abs_output_section,
4465 &statement_list.head, 0, 0, NULL, TRUE);
4468 /* See if anything special should be done now we know how big
4469 everything is. */
4470 ldemul_after_allocation ();
4472 /* Fix any .startof. or .sizeof. symbols. */
4473 lang_set_startof ();
4475 /* Do all the assignments, now that we know the final resting places
4476 of all the symbols. */
4478 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4480 /* Make sure that the section addresses make sense. */
4481 if (! link_info.relocatable
4482 && command_line.check_section_addresses)
4483 lang_check_section_addresses ();
4485 /* Final stuffs. */
4487 ldemul_finish ();
4488 lang_finish ();
4491 /* EXPORTED TO YACC */
4493 void
4494 lang_add_wild (struct wildcard_spec *filespec,
4495 struct wildcard_list *section_list,
4496 bfd_boolean keep_sections)
4498 struct wildcard_list *curr, *next;
4499 lang_wild_statement_type *new;
4501 /* Reverse the list as the parser puts it back to front. */
4502 for (curr = section_list, section_list = NULL;
4503 curr != NULL;
4504 section_list = curr, curr = next)
4506 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4507 placed_commons = TRUE;
4509 next = curr->next;
4510 curr->next = section_list;
4513 if (filespec != NULL && filespec->name != NULL)
4515 if (strcmp (filespec->name, "*") == 0)
4516 filespec->name = NULL;
4517 else if (! wildcardp (filespec->name))
4518 lang_has_input_file = TRUE;
4521 new = new_stat (lang_wild_statement, stat_ptr);
4522 new->filename = NULL;
4523 new->filenames_sorted = FALSE;
4524 if (filespec != NULL)
4526 new->filename = filespec->name;
4527 new->filenames_sorted = filespec->sorted;
4529 new->section_list = section_list;
4530 new->keep_sections = keep_sections;
4531 lang_list_init (&new->children);
4534 void
4535 lang_section_start (const char *name, etree_type *address)
4537 lang_address_statement_type *ad;
4539 ad = new_stat (lang_address_statement, stat_ptr);
4540 ad->section_name = name;
4541 ad->address = address;
4544 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4545 because of a -e argument on the command line, or zero if this is
4546 called by ENTRY in a linker script. Command line arguments take
4547 precedence. */
4549 void
4550 lang_add_entry (const char *name, bfd_boolean cmdline)
4552 if (entry_symbol.name == NULL
4553 || cmdline
4554 || ! entry_from_cmdline)
4556 entry_symbol.name = name;
4557 entry_from_cmdline = cmdline;
4561 void
4562 lang_add_target (const char *name)
4564 lang_target_statement_type *new = new_stat (lang_target_statement,
4565 stat_ptr);
4567 new->target = name;
4571 void
4572 lang_add_map (const char *name)
4574 while (*name)
4576 switch (*name)
4578 case 'F':
4579 map_option_f = TRUE;
4580 break;
4582 name++;
4586 void
4587 lang_add_fill (fill_type *fill)
4589 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4590 stat_ptr);
4592 new->fill = fill;
4595 void
4596 lang_add_data (int type, union etree_union *exp)
4599 lang_data_statement_type *new = new_stat (lang_data_statement,
4600 stat_ptr);
4602 new->exp = exp;
4603 new->type = type;
4607 /* Create a new reloc statement. RELOC is the BFD relocation type to
4608 generate. HOWTO is the corresponding howto structure (we could
4609 look this up, but the caller has already done so). SECTION is the
4610 section to generate a reloc against, or NAME is the name of the
4611 symbol to generate a reloc against. Exactly one of SECTION and
4612 NAME must be NULL. ADDEND is an expression for the addend. */
4614 void
4615 lang_add_reloc (bfd_reloc_code_real_type reloc,
4616 reloc_howto_type *howto,
4617 asection *section,
4618 const char *name,
4619 union etree_union *addend)
4621 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4623 p->reloc = reloc;
4624 p->howto = howto;
4625 p->section = section;
4626 p->name = name;
4627 p->addend_exp = addend;
4629 p->addend_value = 0;
4630 p->output_section = NULL;
4631 p->output_vma = 0;
4634 lang_assignment_statement_type *
4635 lang_add_assignment (etree_type *exp)
4637 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4638 stat_ptr);
4640 new->exp = exp;
4641 return new;
4644 void
4645 lang_add_attribute (enum statement_enum attribute)
4647 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4650 void
4651 lang_startup (const char *name)
4653 if (startup_file != NULL)
4655 einfo (_("%P%Fmultiple STARTUP files\n"));
4657 first_file->filename = name;
4658 first_file->local_sym_name = name;
4659 first_file->real = TRUE;
4661 startup_file = name;
4664 void
4665 lang_float (bfd_boolean maybe)
4667 lang_float_flag = maybe;
4671 /* Work out the load- and run-time regions from a script statement, and
4672 store them in *LMA_REGION and *REGION respectively.
4674 MEMSPEC is the name of the run-time region, or the value of
4675 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4676 LMA_MEMSPEC is the name of the load-time region, or null if the
4677 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4678 had an explicit load address.
4680 It is an error to specify both a load region and a load address. */
4682 static void
4683 lang_get_regions (lang_memory_region_type **region,
4684 lang_memory_region_type **lma_region,
4685 const char *memspec,
4686 const char *lma_memspec,
4687 bfd_boolean have_lma,
4688 bfd_boolean have_vma)
4690 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4692 /* If no runtime region or VMA has been specified, but the load region
4693 has been specified, then use the load region for the runtime region
4694 as well. */
4695 if (lma_memspec != NULL
4696 && ! have_vma
4697 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4698 *region = *lma_region;
4699 else
4700 *region = lang_memory_region_lookup (memspec, FALSE);
4702 if (have_lma && lma_memspec != 0)
4703 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4706 void
4707 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4708 lang_output_section_phdr_list *phdrs,
4709 const char *lma_memspec)
4711 lang_get_regions (&current_section->region,
4712 &current_section->lma_region,
4713 memspec, lma_memspec,
4714 current_section->load_base != NULL,
4715 current_section->addr_tree != NULL);
4716 current_section->fill = fill;
4717 current_section->phdrs = phdrs;
4718 stat_ptr = &statement_list;
4721 /* Create an absolute symbol with the given name with the value of the
4722 address of first byte of the section named.
4724 If the symbol already exists, then do nothing. */
4726 void
4727 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4729 struct bfd_link_hash_entry *h;
4731 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4732 if (h == NULL)
4733 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4735 if (h->type == bfd_link_hash_new
4736 || h->type == bfd_link_hash_undefined)
4738 asection *sec;
4740 h->type = bfd_link_hash_defined;
4742 sec = bfd_get_section_by_name (output_bfd, secname);
4743 if (sec == NULL)
4744 h->u.def.value = 0;
4745 else
4746 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4748 h->u.def.section = bfd_abs_section_ptr;
4752 /* Create an absolute symbol with the given name with the value of the
4753 address of the first byte after the end of the section named.
4755 If the symbol already exists, then do nothing. */
4757 void
4758 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4760 struct bfd_link_hash_entry *h;
4762 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4763 if (h == NULL)
4764 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4766 if (h->type == bfd_link_hash_new
4767 || h->type == bfd_link_hash_undefined)
4769 asection *sec;
4771 h->type = bfd_link_hash_defined;
4773 sec = bfd_get_section_by_name (output_bfd, secname);
4774 if (sec == NULL)
4775 h->u.def.value = 0;
4776 else
4777 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4778 + TO_ADDR (sec->size));
4780 h->u.def.section = bfd_abs_section_ptr;
4784 void
4785 lang_statement_append (lang_statement_list_type *list,
4786 lang_statement_union_type *element,
4787 lang_statement_union_type **field)
4789 *(list->tail) = element;
4790 list->tail = field;
4793 /* Set the output format type. -oformat overrides scripts. */
4795 void
4796 lang_add_output_format (const char *format,
4797 const char *big,
4798 const char *little,
4799 int from_script)
4801 if (output_target == NULL || !from_script)
4803 if (command_line.endian == ENDIAN_BIG
4804 && big != NULL)
4805 format = big;
4806 else if (command_line.endian == ENDIAN_LITTLE
4807 && little != NULL)
4808 format = little;
4810 output_target = format;
4814 /* Enter a group. This creates a new lang_group_statement, and sets
4815 stat_ptr to build new statements within the group. */
4817 void
4818 lang_enter_group (void)
4820 lang_group_statement_type *g;
4822 g = new_stat (lang_group_statement, stat_ptr);
4823 lang_list_init (&g->children);
4824 stat_ptr = &g->children;
4827 /* Leave a group. This just resets stat_ptr to start writing to the
4828 regular list of statements again. Note that this will not work if
4829 groups can occur inside anything else which can adjust stat_ptr,
4830 but currently they can't. */
4832 void
4833 lang_leave_group (void)
4835 stat_ptr = &statement_list;
4838 /* Add a new program header. This is called for each entry in a PHDRS
4839 command in a linker script. */
4841 void
4842 lang_new_phdr (const char *name,
4843 etree_type *type,
4844 bfd_boolean filehdr,
4845 bfd_boolean phdrs,
4846 etree_type *at,
4847 etree_type *flags)
4849 struct lang_phdr *n, **pp;
4851 n = stat_alloc (sizeof (struct lang_phdr));
4852 n->next = NULL;
4853 n->name = name;
4854 n->type = exp_get_value_int (type, 0, "program header type",
4855 lang_final_phase_enum);
4856 n->filehdr = filehdr;
4857 n->phdrs = phdrs;
4858 n->at = at;
4859 n->flags = flags;
4861 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4863 *pp = n;
4866 /* Record the program header information in the output BFD. FIXME: We
4867 should not be calling an ELF specific function here. */
4869 static void
4870 lang_record_phdrs (void)
4872 unsigned int alc;
4873 asection **secs;
4874 lang_output_section_phdr_list *last;
4875 struct lang_phdr *l;
4876 lang_statement_union_type *u;
4878 alc = 10;
4879 secs = xmalloc (alc * sizeof (asection *));
4880 last = NULL;
4881 for (l = lang_phdr_list; l != NULL; l = l->next)
4883 unsigned int c;
4884 flagword flags;
4885 bfd_vma at;
4887 c = 0;
4888 for (u = lang_output_section_statement.head;
4889 u != NULL;
4890 u = u->output_section_statement.next)
4892 lang_output_section_statement_type *os;
4893 lang_output_section_phdr_list *pl;
4895 os = &u->output_section_statement;
4896 if (os->constraint == -1)
4897 continue;
4899 pl = os->phdrs;
4900 if (pl != NULL)
4901 last = pl;
4902 else
4904 if (os->sectype == noload_section
4905 || os->bfd_section == NULL
4906 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4907 continue;
4908 pl = last;
4911 if (os->bfd_section == NULL)
4912 continue;
4914 for (; pl != NULL; pl = pl->next)
4916 if (strcmp (pl->name, l->name) == 0)
4918 if (c >= alc)
4920 alc *= 2;
4921 secs = xrealloc (secs, alc * sizeof (asection *));
4923 secs[c] = os->bfd_section;
4924 ++c;
4925 pl->used = TRUE;
4930 if (l->flags == NULL)
4931 flags = 0;
4932 else
4933 flags = exp_get_vma (l->flags, 0, "phdr flags",
4934 lang_final_phase_enum);
4936 if (l->at == NULL)
4937 at = 0;
4938 else
4939 at = exp_get_vma (l->at, 0, "phdr load address",
4940 lang_final_phase_enum);
4942 if (! bfd_record_phdr (output_bfd, l->type,
4943 l->flags != NULL, flags, l->at != NULL,
4944 at, l->filehdr, l->phdrs, c, secs))
4945 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4948 free (secs);
4950 /* Make sure all the phdr assignments succeeded. */
4951 for (u = lang_output_section_statement.head;
4952 u != NULL;
4953 u = u->output_section_statement.next)
4955 lang_output_section_phdr_list *pl;
4957 if (u->output_section_statement.constraint == -1
4958 || u->output_section_statement.bfd_section == NULL)
4959 continue;
4961 for (pl = u->output_section_statement.phdrs;
4962 pl != NULL;
4963 pl = pl->next)
4964 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4965 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4966 u->output_section_statement.name, pl->name);
4970 /* Record a list of sections which may not be cross referenced. */
4972 void
4973 lang_add_nocrossref (lang_nocrossref_type *l)
4975 struct lang_nocrossrefs *n;
4977 n = xmalloc (sizeof *n);
4978 n->next = nocrossref_list;
4979 n->list = l;
4980 nocrossref_list = n;
4982 /* Set notice_all so that we get informed about all symbols. */
4983 link_info.notice_all = TRUE;
4986 /* Overlay handling. We handle overlays with some static variables. */
4988 /* The overlay virtual address. */
4989 static etree_type *overlay_vma;
4990 /* And subsection alignment. */
4991 static etree_type *overlay_subalign;
4993 /* An expression for the maximum section size seen so far. */
4994 static etree_type *overlay_max;
4996 /* A list of all the sections in this overlay. */
4998 struct overlay_list {
4999 struct overlay_list *next;
5000 lang_output_section_statement_type *os;
5003 static struct overlay_list *overlay_list;
5005 /* Start handling an overlay. */
5007 void
5008 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
5010 /* The grammar should prevent nested overlays from occurring. */
5011 ASSERT (overlay_vma == NULL
5012 && overlay_subalign == NULL
5013 && overlay_max == NULL);
5015 overlay_vma = vma_expr;
5016 overlay_subalign = subalign;
5019 /* Start a section in an overlay. We handle this by calling
5020 lang_enter_output_section_statement with the correct VMA.
5021 lang_leave_overlay sets up the LMA and memory regions. */
5023 void
5024 lang_enter_overlay_section (const char *name)
5026 struct overlay_list *n;
5027 etree_type *size;
5029 lang_enter_output_section_statement (name, overlay_vma, normal_section,
5030 0, overlay_subalign, 0, 0);
5032 /* If this is the first section, then base the VMA of future
5033 sections on this one. This will work correctly even if `.' is
5034 used in the addresses. */
5035 if (overlay_list == NULL)
5036 overlay_vma = exp_nameop (ADDR, name);
5038 /* Remember the section. */
5039 n = xmalloc (sizeof *n);
5040 n->os = current_section;
5041 n->next = overlay_list;
5042 overlay_list = n;
5044 size = exp_nameop (SIZEOF, name);
5046 /* Arrange to work out the maximum section end address. */
5047 if (overlay_max == NULL)
5048 overlay_max = size;
5049 else
5050 overlay_max = exp_binop (MAX_K, overlay_max, size);
5053 /* Finish a section in an overlay. There isn't any special to do
5054 here. */
5056 void
5057 lang_leave_overlay_section (fill_type *fill,
5058 lang_output_section_phdr_list *phdrs)
5060 const char *name;
5061 char *clean, *s2;
5062 const char *s1;
5063 char *buf;
5065 name = current_section->name;
5067 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
5068 region and that no load-time region has been specified. It doesn't
5069 really matter what we say here, since lang_leave_overlay will
5070 override it. */
5071 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
5073 /* Define the magic symbols. */
5075 clean = xmalloc (strlen (name) + 1);
5076 s2 = clean;
5077 for (s1 = name; *s1 != '\0'; s1++)
5078 if (ISALNUM (*s1) || *s1 == '_')
5079 *s2++ = *s1;
5080 *s2 = '\0';
5082 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
5083 sprintf (buf, "__load_start_%s", clean);
5084 lang_add_assignment (exp_assop ('=', buf,
5085 exp_nameop (LOADADDR, name)));
5087 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
5088 sprintf (buf, "__load_stop_%s", clean);
5089 lang_add_assignment (exp_assop ('=', buf,
5090 exp_binop ('+',
5091 exp_nameop (LOADADDR, name),
5092 exp_nameop (SIZEOF, name))));
5094 free (clean);
5097 /* Finish an overlay. If there are any overlay wide settings, this
5098 looks through all the sections in the overlay and sets them. */
5100 void
5101 lang_leave_overlay (etree_type *lma_expr,
5102 int nocrossrefs,
5103 fill_type *fill,
5104 const char *memspec,
5105 lang_output_section_phdr_list *phdrs,
5106 const char *lma_memspec)
5108 lang_memory_region_type *region;
5109 lang_memory_region_type *lma_region;
5110 struct overlay_list *l;
5111 lang_nocrossref_type *nocrossref;
5113 lang_get_regions (&region, &lma_region,
5114 memspec, lma_memspec,
5115 lma_expr != NULL, FALSE);
5117 nocrossref = NULL;
5119 /* After setting the size of the last section, set '.' to end of the
5120 overlay region. */
5121 if (overlay_list != NULL)
5122 overlay_list->os->update_dot_tree
5123 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
5125 l = overlay_list;
5126 while (l != NULL)
5128 struct overlay_list *next;
5130 if (fill != NULL && l->os->fill == NULL)
5131 l->os->fill = fill;
5133 l->os->region = region;
5134 l->os->lma_region = lma_region;
5136 /* The first section has the load address specified in the
5137 OVERLAY statement. The rest are worked out from that.
5138 The base address is not needed (and should be null) if
5139 an LMA region was specified. */
5140 if (l->next == 0)
5141 l->os->load_base = lma_expr;
5142 else if (lma_region == 0)
5143 l->os->load_base = exp_binop ('+',
5144 exp_nameop (LOADADDR, l->next->os->name),
5145 exp_nameop (SIZEOF, l->next->os->name));
5147 if (phdrs != NULL && l->os->phdrs == NULL)
5148 l->os->phdrs = phdrs;
5150 if (nocrossrefs)
5152 lang_nocrossref_type *nc;
5154 nc = xmalloc (sizeof *nc);
5155 nc->name = l->os->name;
5156 nc->next = nocrossref;
5157 nocrossref = nc;
5160 next = l->next;
5161 free (l);
5162 l = next;
5165 if (nocrossref != NULL)
5166 lang_add_nocrossref (nocrossref);
5168 overlay_vma = NULL;
5169 overlay_list = NULL;
5170 overlay_max = NULL;
5173 /* Version handling. This is only useful for ELF. */
5175 /* This global variable holds the version tree that we build. */
5177 struct bfd_elf_version_tree *lang_elf_version_info;
5179 /* If PREV is NULL, return first version pattern matching particular symbol.
5180 If PREV is non-NULL, return first version pattern matching particular
5181 symbol after PREV (previously returned by lang_vers_match). */
5183 static struct bfd_elf_version_expr *
5184 lang_vers_match (struct bfd_elf_version_expr_head *head,
5185 struct bfd_elf_version_expr *prev,
5186 const char *sym)
5188 const char *cxx_sym = sym;
5189 const char *java_sym = sym;
5190 struct bfd_elf_version_expr *expr = NULL;
5192 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5194 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5195 if (!cxx_sym)
5196 cxx_sym = sym;
5198 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5200 java_sym = cplus_demangle (sym, DMGL_JAVA);
5201 if (!java_sym)
5202 java_sym = sym;
5205 if (head->htab && (prev == NULL || prev->symbol))
5207 struct bfd_elf_version_expr e;
5209 switch (prev ? prev->mask : 0)
5211 case 0:
5212 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5214 e.symbol = sym;
5215 expr = htab_find (head->htab, &e);
5216 while (expr && strcmp (expr->symbol, sym) == 0)
5217 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5218 goto out_ret;
5219 else
5220 expr = expr->next;
5222 /* Fallthrough */
5223 case BFD_ELF_VERSION_C_TYPE:
5224 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5226 e.symbol = cxx_sym;
5227 expr = htab_find (head->htab, &e);
5228 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5229 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5230 goto out_ret;
5231 else
5232 expr = expr->next;
5234 /* Fallthrough */
5235 case BFD_ELF_VERSION_CXX_TYPE:
5236 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5238 e.symbol = java_sym;
5239 expr = htab_find (head->htab, &e);
5240 while (expr && strcmp (expr->symbol, java_sym) == 0)
5241 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5242 goto out_ret;
5243 else
5244 expr = expr->next;
5246 /* Fallthrough */
5247 default:
5248 break;
5252 /* Finally, try the wildcards. */
5253 if (prev == NULL || prev->symbol)
5254 expr = head->remaining;
5255 else
5256 expr = prev->next;
5257 while (expr)
5259 const char *s;
5261 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5262 break;
5264 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5265 s = java_sym;
5266 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5267 s = cxx_sym;
5268 else
5269 s = sym;
5270 if (fnmatch (expr->pattern, s, 0) == 0)
5271 break;
5272 expr = expr->next;
5275 out_ret:
5276 if (cxx_sym != sym)
5277 free ((char *) cxx_sym);
5278 if (java_sym != sym)
5279 free ((char *) java_sym);
5280 return expr;
5283 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5284 return a string pointing to the symbol name. */
5286 static const char *
5287 realsymbol (const char *pattern)
5289 const char *p;
5290 bfd_boolean changed = FALSE, backslash = FALSE;
5291 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5293 for (p = pattern, s = symbol; *p != '\0'; ++p)
5295 /* It is a glob pattern only if there is no preceding
5296 backslash. */
5297 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5299 free (symbol);
5300 return NULL;
5303 if (backslash)
5305 /* Remove the preceding backslash. */
5306 *(s - 1) = *p;
5307 changed = TRUE;
5309 else
5310 *s++ = *p;
5312 backslash = *p == '\\';
5315 if (changed)
5317 *s = '\0';
5318 return symbol;
5320 else
5322 free (symbol);
5323 return pattern;
5327 /* This is called for each variable name or match expression. */
5329 struct bfd_elf_version_expr *
5330 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5331 const char *new,
5332 const char *lang)
5334 struct bfd_elf_version_expr *ret;
5336 ret = xmalloc (sizeof *ret);
5337 ret->next = orig;
5338 ret->pattern = new;
5339 ret->symver = 0;
5340 ret->script = 0;
5341 ret->symbol = realsymbol (new);
5343 if (lang == NULL || strcasecmp (lang, "C") == 0)
5344 ret->mask = BFD_ELF_VERSION_C_TYPE;
5345 else if (strcasecmp (lang, "C++") == 0)
5346 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5347 else if (strcasecmp (lang, "Java") == 0)
5348 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5349 else
5351 einfo (_("%X%P: unknown language `%s' in version information\n"),
5352 lang);
5353 ret->mask = BFD_ELF_VERSION_C_TYPE;
5356 return ldemul_new_vers_pattern (ret);
5359 /* This is called for each set of variable names and match
5360 expressions. */
5362 struct bfd_elf_version_tree *
5363 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5364 struct bfd_elf_version_expr *locals)
5366 struct bfd_elf_version_tree *ret;
5368 ret = xcalloc (1, sizeof *ret);
5369 ret->globals.list = globals;
5370 ret->locals.list = locals;
5371 ret->match = lang_vers_match;
5372 ret->name_indx = (unsigned int) -1;
5373 return ret;
5376 /* This static variable keeps track of version indices. */
5378 static int version_index;
5380 static hashval_t
5381 version_expr_head_hash (const void *p)
5383 const struct bfd_elf_version_expr *e = p;
5385 return htab_hash_string (e->symbol);
5388 static int
5389 version_expr_head_eq (const void *p1, const void *p2)
5391 const struct bfd_elf_version_expr *e1 = p1;
5392 const struct bfd_elf_version_expr *e2 = p2;
5394 return strcmp (e1->symbol, e2->symbol) == 0;
5397 static void
5398 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5400 size_t count = 0;
5401 struct bfd_elf_version_expr *e, *next;
5402 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5404 for (e = head->list; e; e = e->next)
5406 if (e->symbol)
5407 count++;
5408 head->mask |= e->mask;
5411 if (count)
5413 head->htab = htab_create (count * 2, version_expr_head_hash,
5414 version_expr_head_eq, NULL);
5415 list_loc = &head->list;
5416 remaining_loc = &head->remaining;
5417 for (e = head->list; e; e = next)
5419 next = e->next;
5420 if (!e->symbol)
5422 *remaining_loc = e;
5423 remaining_loc = &e->next;
5425 else
5427 void **loc = htab_find_slot (head->htab, e, INSERT);
5429 if (*loc)
5431 struct bfd_elf_version_expr *e1, *last;
5433 e1 = *loc;
5434 last = NULL;
5437 if (e1->mask == e->mask)
5439 last = NULL;
5440 break;
5442 last = e1;
5443 e1 = e1->next;
5445 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5447 if (last == NULL)
5449 /* This is a duplicate. */
5450 /* FIXME: Memory leak. Sometimes pattern is not
5451 xmalloced alone, but in larger chunk of memory. */
5452 /* free (e->symbol); */
5453 free (e);
5455 else
5457 e->next = last->next;
5458 last->next = e;
5461 else
5463 *loc = e;
5464 *list_loc = e;
5465 list_loc = &e->next;
5469 *remaining_loc = NULL;
5470 *list_loc = head->remaining;
5472 else
5473 head->remaining = head->list;
5476 /* This is called when we know the name and dependencies of the
5477 version. */
5479 void
5480 lang_register_vers_node (const char *name,
5481 struct bfd_elf_version_tree *version,
5482 struct bfd_elf_version_deps *deps)
5484 struct bfd_elf_version_tree *t, **pp;
5485 struct bfd_elf_version_expr *e1;
5487 if (name == NULL)
5488 name = "";
5490 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5491 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5493 einfo (_("%X%P: anonymous version tag cannot be combined"
5494 " with other version tags\n"));
5495 free (version);
5496 return;
5499 /* Make sure this node has a unique name. */
5500 for (t = lang_elf_version_info; t != NULL; t = t->next)
5501 if (strcmp (t->name, name) == 0)
5502 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5504 lang_finalize_version_expr_head (&version->globals);
5505 lang_finalize_version_expr_head (&version->locals);
5507 /* Check the global and local match names, and make sure there
5508 aren't any duplicates. */
5510 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5512 for (t = lang_elf_version_info; t != NULL; t = t->next)
5514 struct bfd_elf_version_expr *e2;
5516 if (t->locals.htab && e1->symbol)
5518 e2 = htab_find (t->locals.htab, e1);
5519 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5521 if (e1->mask == e2->mask)
5522 einfo (_("%X%P: duplicate expression `%s'"
5523 " in version information\n"), e1->symbol);
5524 e2 = e2->next;
5527 else if (!e1->symbol)
5528 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5529 if (strcmp (e1->pattern, e2->pattern) == 0
5530 && e1->mask == e2->mask)
5531 einfo (_("%X%P: duplicate expression `%s'"
5532 " in version information\n"), e1->pattern);
5536 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5538 for (t = lang_elf_version_info; t != NULL; t = t->next)
5540 struct bfd_elf_version_expr *e2;
5542 if (t->globals.htab && e1->symbol)
5544 e2 = htab_find (t->globals.htab, e1);
5545 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5547 if (e1->mask == e2->mask)
5548 einfo (_("%X%P: duplicate expression `%s'"
5549 " in version information\n"),
5550 e1->symbol);
5551 e2 = e2->next;
5554 else if (!e1->symbol)
5555 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5556 if (strcmp (e1->pattern, e2->pattern) == 0
5557 && e1->mask == e2->mask)
5558 einfo (_("%X%P: duplicate expression `%s'"
5559 " in version information\n"), e1->pattern);
5563 version->deps = deps;
5564 version->name = name;
5565 if (name[0] != '\0')
5567 ++version_index;
5568 version->vernum = version_index;
5570 else
5571 version->vernum = 0;
5573 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5575 *pp = version;
5578 /* This is called when we see a version dependency. */
5580 struct bfd_elf_version_deps *
5581 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5583 struct bfd_elf_version_deps *ret;
5584 struct bfd_elf_version_tree *t;
5586 ret = xmalloc (sizeof *ret);
5587 ret->next = list;
5589 for (t = lang_elf_version_info; t != NULL; t = t->next)
5591 if (strcmp (t->name, name) == 0)
5593 ret->version_needed = t;
5594 return ret;
5598 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5600 return ret;
5603 static void
5604 lang_do_version_exports_section (void)
5606 struct bfd_elf_version_expr *greg = NULL, *lreg;
5608 LANG_FOR_EACH_INPUT_STATEMENT (is)
5610 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5611 char *contents, *p;
5612 bfd_size_type len;
5614 if (sec == NULL)
5615 continue;
5617 len = sec->size;
5618 contents = xmalloc (len);
5619 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5620 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5622 p = contents;
5623 while (p < contents + len)
5625 greg = lang_new_vers_pattern (greg, p, NULL);
5626 p = strchr (p, '\0') + 1;
5629 /* Do not free the contents, as we used them creating the regex. */
5631 /* Do not include this section in the link. */
5632 sec->flags |= SEC_EXCLUDE;
5635 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5636 lang_register_vers_node (command_line.version_exports_section,
5637 lang_new_vers_node (greg, lreg), NULL);
5640 void
5641 lang_add_unique (const char *name)
5643 struct unique_sections *ent;
5645 for (ent = unique_section_list; ent; ent = ent->next)
5646 if (strcmp (ent->name, name) == 0)
5647 return;
5649 ent = xmalloc (sizeof *ent);
5650 ent->name = xstrdup (name);
5651 ent->next = unique_section_list;
5652 unique_section_list = ent;