* ldgram.y (exp): Add two operand ALIGN.
[binutils.git] / ld / ldlang.c
bloba25f5f300a4578cf20700dd684205ab29c0bfeba
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;
51 #define obstack_chunk_alloc xmalloc
52 #define obstack_chunk_free free
53 static const char *startup_file;
54 static lang_statement_list_type input_file_chain;
55 static bfd_boolean placed_commons = FALSE;
56 static lang_output_section_statement_type *default_common_section;
57 static bfd_boolean map_option_f;
58 static bfd_vma print_dot;
59 static lang_input_statement_type *first_file;
60 static const char *current_target;
61 static const char *output_target;
62 static lang_statement_list_type statement_list;
63 static struct lang_phdr *lang_phdr_list;
64 static struct bfd_hash_table lang_definedness_table;
66 /* Forward declarations. */
67 static void exp_init_os (etree_type *);
68 static bfd_boolean wildcardp (const char *);
69 static lang_input_statement_type *lookup_name (const char *);
70 static bfd_boolean load_symbols (lang_input_statement_type *,
71 lang_statement_list_type *);
72 static struct bfd_hash_entry *lang_definedness_newfunc
73 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
74 static void insert_undefined (const char *);
75 static void print_statement (lang_statement_union_type *,
76 lang_output_section_statement_type *);
77 static void print_statement_list (lang_statement_union_type *,
78 lang_output_section_statement_type *);
79 static void print_statements (void);
80 static bfd_boolean lang_one_common (struct bfd_link_hash_entry *, void *);
81 static void lang_record_phdrs (void);
82 static void lang_do_version_exports_section (void);
84 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
85 asection *, lang_input_statement_type *, void *);
87 /* Exported variables. */
88 lang_output_section_statement_type *abs_output_section;
89 lang_statement_list_type lang_output_section_statement;
90 lang_statement_list_type *stat_ptr = &statement_list;
91 lang_statement_list_type file_chain = { NULL, NULL };
92 struct bfd_sym_chain entry_symbol = { NULL, NULL };
93 const char *entry_section = ".text";
94 bfd_boolean entry_from_cmdline;
95 bfd_boolean lang_has_input_file = FALSE;
96 bfd_boolean had_output_filename = FALSE;
97 bfd_boolean lang_float_flag = FALSE;
98 bfd_boolean delete_output_file_on_failure = FALSE;
99 struct lang_nocrossrefs *nocrossref_list;
100 struct unique_sections *unique_section_list;
101 static bfd_boolean ldlang_sysrooted_script = FALSE;
102 int lang_statement_iteration = 0;
104 etree_type *base; /* Relocation base - or null */
106 #define new_stat(x, y) \
107 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
109 #define outside_section_address(q) \
110 ((q)->output_offset + (q)->output_section->vma)
112 #define outside_symbol_address(q) \
113 ((q)->value + outside_section_address (q->section))
115 #define SECTION_NAME_MAP_LENGTH (16)
117 void *
118 stat_alloc (size_t size)
120 return obstack_alloc (&stat_obstack, size);
123 bfd_boolean
124 unique_section_p (const char *secnam)
126 struct unique_sections *unam;
128 for (unam = unique_section_list; unam; unam = unam->next)
129 if (wildcardp (unam->name)
130 ? fnmatch (unam->name, secnam, 0) == 0
131 : strcmp (unam->name, secnam) == 0)
133 return TRUE;
136 return FALSE;
139 /* Generic traversal routines for finding matching sections. */
141 static void
142 walk_wild_section (lang_wild_statement_type *ptr,
143 lang_input_statement_type *file,
144 callback_t callback,
145 void *data)
147 asection *s;
149 if (file->just_syms_flag)
150 return;
152 for (s = file->the_bfd->sections; s != NULL; s = s->next)
154 struct wildcard_list *sec;
156 sec = ptr->section_list;
157 if (sec == NULL)
158 (*callback) (ptr, sec, s, file, data);
160 while (sec != NULL)
162 bfd_boolean skip = FALSE;
163 struct name_list *list_tmp;
165 /* Don't process sections from files which were
166 excluded. */
167 for (list_tmp = sec->spec.exclude_name_list;
168 list_tmp;
169 list_tmp = list_tmp->next)
171 if (wildcardp (list_tmp->name))
172 skip = fnmatch (list_tmp->name, file->filename, 0) == 0;
173 else
174 skip = strcmp (list_tmp->name, file->filename) == 0;
176 /* If this file is part of an archive, and the archive is
177 excluded, exclude this file. */
178 if (! skip && file->the_bfd != NULL
179 && file->the_bfd->my_archive != NULL
180 && file->the_bfd->my_archive->filename != NULL)
182 if (wildcardp (list_tmp->name))
183 skip = fnmatch (list_tmp->name,
184 file->the_bfd->my_archive->filename,
185 0) == 0;
186 else
187 skip = strcmp (list_tmp->name,
188 file->the_bfd->my_archive->filename) == 0;
191 if (skip)
192 break;
195 if (!skip && sec->spec.name != NULL)
197 const char *sname = bfd_get_section_name (file->the_bfd, s);
199 if (wildcardp (sec->spec.name))
200 skip = fnmatch (sec->spec.name, sname, 0) != 0;
201 else
202 skip = strcmp (sec->spec.name, sname) != 0;
205 if (!skip)
206 (*callback) (ptr, sec, s, file, data);
208 sec = sec->next;
213 /* Handle a wild statement for a single file F. */
215 static void
216 walk_wild_file (lang_wild_statement_type *s,
217 lang_input_statement_type *f,
218 callback_t callback,
219 void *data)
221 if (f->the_bfd == NULL
222 || ! bfd_check_format (f->the_bfd, bfd_archive))
223 walk_wild_section (s, f, callback, data);
224 else
226 bfd *member;
228 /* This is an archive file. We must map each member of the
229 archive separately. */
230 member = bfd_openr_next_archived_file (f->the_bfd, NULL);
231 while (member != NULL)
233 /* When lookup_name is called, it will call the add_symbols
234 entry point for the archive. For each element of the
235 archive which is included, BFD will call ldlang_add_file,
236 which will set the usrdata field of the member to the
237 lang_input_statement. */
238 if (member->usrdata != NULL)
240 walk_wild_section (s, member->usrdata, callback, data);
243 member = bfd_openr_next_archived_file (f->the_bfd, member);
248 static void
249 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
251 const char *file_spec = s->filename;
253 if (file_spec == NULL)
255 /* Perform the iteration over all files in the list. */
256 LANG_FOR_EACH_INPUT_STATEMENT (f)
258 walk_wild_file (s, f, callback, data);
261 else if (wildcardp (file_spec))
263 LANG_FOR_EACH_INPUT_STATEMENT (f)
265 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
266 walk_wild_file (s, f, callback, data);
269 else
271 lang_input_statement_type *f;
273 /* Perform the iteration over a single file. */
274 f = lookup_name (file_spec);
275 if (f)
276 walk_wild_file (s, f, callback, data);
280 /* lang_for_each_statement walks the parse tree and calls the provided
281 function for each node. */
283 static void
284 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
285 lang_statement_union_type *s)
287 for (; s != NULL; s = s->header.next)
289 func (s);
291 switch (s->header.type)
293 case lang_constructors_statement_enum:
294 lang_for_each_statement_worker (func, constructor_list.head);
295 break;
296 case lang_output_section_statement_enum:
297 lang_for_each_statement_worker
298 (func,
299 s->output_section_statement.children.head);
300 break;
301 case lang_wild_statement_enum:
302 lang_for_each_statement_worker
303 (func,
304 s->wild_statement.children.head);
305 break;
306 case lang_group_statement_enum:
307 lang_for_each_statement_worker (func,
308 s->group_statement.children.head);
309 break;
310 case lang_data_statement_enum:
311 case lang_reloc_statement_enum:
312 case lang_object_symbols_statement_enum:
313 case lang_output_statement_enum:
314 case lang_target_statement_enum:
315 case lang_input_section_enum:
316 case lang_input_statement_enum:
317 case lang_assignment_statement_enum:
318 case lang_padding_statement_enum:
319 case lang_address_statement_enum:
320 case lang_fill_statement_enum:
321 break;
322 default:
323 FAIL ();
324 break;
329 void
330 lang_for_each_statement (void (*func) (lang_statement_union_type *))
332 lang_for_each_statement_worker (func, statement_list.head);
335 /*----------------------------------------------------------------------*/
337 void
338 lang_list_init (lang_statement_list_type *list)
340 list->head = NULL;
341 list->tail = &list->head;
344 /* Build a new statement node for the parse tree. */
346 static lang_statement_union_type *
347 new_statement (enum statement_enum type,
348 size_t size,
349 lang_statement_list_type *list)
351 lang_statement_union_type *new;
353 new = stat_alloc (size);
354 new->header.type = type;
355 new->header.next = NULL;
356 lang_statement_append (list, new, &new->header.next);
357 return new;
360 /* Build a new input file node for the language. There are several
361 ways in which we treat an input file, eg, we only look at symbols,
362 or prefix it with a -l etc.
364 We can be supplied with requests for input files more than once;
365 they may, for example be split over several lines like foo.o(.text)
366 foo.o(.data) etc, so when asked for a file we check that we haven't
367 got it already so we don't duplicate the bfd. */
369 static lang_input_statement_type *
370 new_afile (const char *name,
371 lang_input_file_enum_type file_type,
372 const char *target,
373 bfd_boolean add_to_list)
375 lang_input_statement_type *p;
377 if (add_to_list)
378 p = new_stat (lang_input_statement, stat_ptr);
379 else
381 p = stat_alloc (sizeof (lang_input_statement_type));
382 p->header.next = NULL;
385 lang_has_input_file = TRUE;
386 p->target = target;
387 p->sysrooted = FALSE;
388 switch (file_type)
390 case lang_input_file_is_symbols_only_enum:
391 p->filename = name;
392 p->is_archive = FALSE;
393 p->real = TRUE;
394 p->local_sym_name = name;
395 p->just_syms_flag = TRUE;
396 p->search_dirs_flag = FALSE;
397 break;
398 case lang_input_file_is_fake_enum:
399 p->filename = name;
400 p->is_archive = FALSE;
401 p->real = FALSE;
402 p->local_sym_name = name;
403 p->just_syms_flag = FALSE;
404 p->search_dirs_flag = FALSE;
405 break;
406 case lang_input_file_is_l_enum:
407 p->is_archive = TRUE;
408 p->filename = name;
409 p->real = TRUE;
410 p->local_sym_name = concat ("-l", name, NULL);
411 p->just_syms_flag = FALSE;
412 p->search_dirs_flag = TRUE;
413 break;
414 case lang_input_file_is_marker_enum:
415 p->filename = name;
416 p->is_archive = FALSE;
417 p->real = FALSE;
418 p->local_sym_name = name;
419 p->just_syms_flag = FALSE;
420 p->search_dirs_flag = TRUE;
421 break;
422 case lang_input_file_is_search_file_enum:
423 p->sysrooted = ldlang_sysrooted_script;
424 p->filename = name;
425 p->is_archive = FALSE;
426 p->real = TRUE;
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_file_enum:
432 p->filename = name;
433 p->is_archive = FALSE;
434 p->real = TRUE;
435 p->local_sym_name = name;
436 p->just_syms_flag = FALSE;
437 p->search_dirs_flag = FALSE;
438 break;
439 default:
440 FAIL ();
442 p->the_bfd = NULL;
443 p->asymbols = NULL;
444 p->next_real_file = NULL;
445 p->next = NULL;
446 p->symbol_count = 0;
447 p->dynamic = config.dynamic_link;
448 p->whole_archive = whole_archive;
449 p->loaded = FALSE;
450 lang_statement_append (&input_file_chain,
451 (lang_statement_union_type *) p,
452 &p->next_real_file);
453 return p;
456 lang_input_statement_type *
457 lang_add_input_file (const char *name,
458 lang_input_file_enum_type file_type,
459 const char *target)
461 lang_has_input_file = TRUE;
462 return new_afile (name, file_type, target, TRUE);
465 /* Build enough state so that the parser can build its tree. */
467 void
468 lang_init (void)
470 obstack_begin (&stat_obstack, 1000);
472 stat_ptr = &statement_list;
474 lang_list_init (stat_ptr);
476 lang_list_init (&input_file_chain);
477 lang_list_init (&lang_output_section_statement);
478 lang_list_init (&file_chain);
479 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
480 NULL);
481 abs_output_section =
482 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
484 abs_output_section->bfd_section = bfd_abs_section_ptr;
486 /* The value "3" is ad-hoc, somewhat related to the expected number of
487 DEFINED expressions in a linker script. For most default linker
488 scripts, there are none. Why a hash table then? Well, it's somewhat
489 simpler to re-use working machinery than using a linked list in terms
490 of code-complexity here in ld, besides the initialization which just
491 looks like other code here. */
492 if (bfd_hash_table_init_n (&lang_definedness_table,
493 lang_definedness_newfunc, 3) != TRUE)
494 einfo (_("%P%F: out of memory during initialization"));
496 /* Callers of exp_fold_tree need to increment this. */
497 lang_statement_iteration = 0;
500 /*----------------------------------------------------------------------
501 A region is an area of memory declared with the
502 MEMORY { name:org=exp, len=exp ... }
503 syntax.
505 We maintain a list of all the regions here.
507 If no regions are specified in the script, then the default is used
508 which is created when looked up to be the entire data space.
510 If create is true we are creating a region inside a MEMORY block.
511 In this case it is probably an error to create a region that has
512 already been created. If we are not inside a MEMORY block it is
513 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
514 and so we issue a warning. */
516 static lang_memory_region_type *lang_memory_region_list;
517 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
519 lang_memory_region_type *
520 lang_memory_region_lookup (const char *const name, bfd_boolean create)
522 lang_memory_region_type *p;
523 lang_memory_region_type *new;
525 /* NAME is NULL for LMA memspecs if no region was specified. */
526 if (name == NULL)
527 return NULL;
529 for (p = lang_memory_region_list; p != NULL; p = p->next)
530 if (strcmp (p->name, name) == 0)
532 if (create)
533 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), name);
534 return p;
537 #if 0
538 /* This code used to always use the first region in the list as the
539 default region. I changed it to instead use a region
540 encompassing all of memory as the default region. This permits
541 NOLOAD sections to work reasonably without requiring a region.
542 People should specify what region they mean, if they really want
543 a region. */
544 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
546 if (lang_memory_region_list != NULL)
547 return lang_memory_region_list;
549 #endif
551 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
552 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
554 new = stat_alloc (sizeof (lang_memory_region_type));
556 new->name = xstrdup (name);
557 new->next = NULL;
559 *lang_memory_region_list_tail = new;
560 lang_memory_region_list_tail = &new->next;
561 new->origin = 0;
562 new->flags = 0;
563 new->not_flags = 0;
564 new->length = ~(bfd_size_type) 0;
565 new->current = 0;
566 new->had_full_message = FALSE;
568 return new;
571 static lang_memory_region_type *
572 lang_memory_default (asection *section)
574 lang_memory_region_type *p;
576 flagword sec_flags = section->flags;
578 /* Override SEC_DATA to mean a writable section. */
579 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
580 sec_flags |= SEC_DATA;
582 for (p = lang_memory_region_list; p != NULL; p = p->next)
584 if ((p->flags & sec_flags) != 0
585 && (p->not_flags & sec_flags) == 0)
587 return p;
590 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
593 lang_output_section_statement_type *
594 lang_output_section_find (const char *const name)
596 lang_statement_union_type *u;
597 lang_output_section_statement_type *lookup;
599 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
601 lookup = &u->output_section_statement;
602 if (strcmp (name, lookup->name) == 0)
603 return lookup;
605 return NULL;
608 lang_output_section_statement_type *
609 lang_output_section_statement_lookup (const char *const name)
611 lang_output_section_statement_type *lookup;
613 lookup = lang_output_section_find (name);
614 if (lookup == NULL)
616 lookup = new_stat (lang_output_section_statement, stat_ptr);
617 lookup->region = NULL;
618 lookup->lma_region = NULL;
619 lookup->fill = 0;
620 lookup->block_value = 1;
621 lookup->name = name;
623 lookup->next = NULL;
624 lookup->bfd_section = NULL;
625 lookup->processed = FALSE;
626 lookup->sectype = normal_section;
627 lookup->addr_tree = NULL;
628 lang_list_init (&lookup->children);
630 lookup->memspec = NULL;
631 lookup->flags = 0;
632 lookup->subsection_alignment = -1;
633 lookup->section_alignment = -1;
634 lookup->load_base = NULL;
635 lookup->update_dot_tree = NULL;
636 lookup->phdrs = NULL;
638 lang_statement_append (&lang_output_section_statement,
639 (lang_statement_union_type *) lookup,
640 &lookup->next);
642 return lookup;
645 static void
646 lang_map_flags (flagword flag)
648 if (flag & SEC_ALLOC)
649 minfo ("a");
651 if (flag & SEC_CODE)
652 minfo ("x");
654 if (flag & SEC_READONLY)
655 minfo ("r");
657 if (flag & SEC_DATA)
658 minfo ("w");
660 if (flag & SEC_LOAD)
661 minfo ("l");
664 void
665 lang_map (void)
667 lang_memory_region_type *m;
669 minfo (_("\nMemory Configuration\n\n"));
670 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
671 _("Name"), _("Origin"), _("Length"), _("Attributes"));
673 for (m = lang_memory_region_list; m != NULL; m = m->next)
675 char buf[100];
676 int len;
678 fprintf (config.map_file, "%-16s ", m->name);
680 sprintf_vma (buf, m->origin);
681 minfo ("0x%s ", buf);
682 len = strlen (buf);
683 while (len < 16)
685 print_space ();
686 ++len;
689 minfo ("0x%V", m->length);
690 if (m->flags || m->not_flags)
692 #ifndef BFD64
693 minfo (" ");
694 #endif
695 if (m->flags)
697 print_space ();
698 lang_map_flags (m->flags);
701 if (m->not_flags)
703 minfo (" !");
704 lang_map_flags (m->not_flags);
708 print_nl ();
711 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
713 print_statements ();
716 /* Initialize an output section. */
718 static void
719 init_os (lang_output_section_statement_type *s)
721 section_userdata_type *new;
723 if (s->bfd_section != NULL)
724 return;
726 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
727 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
729 new = stat_alloc (sizeof (section_userdata_type));
731 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
732 if (s->bfd_section == NULL)
733 s->bfd_section = bfd_make_section (output_bfd, s->name);
734 if (s->bfd_section == NULL)
736 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
737 output_bfd->xvec->name, s->name);
739 s->bfd_section->output_section = s->bfd_section;
741 /* We initialize an output sections output offset to minus its own
742 vma to allow us to output a section through itself. */
743 s->bfd_section->output_offset = 0;
744 get_userdata (s->bfd_section) = new;
746 /* If there is a base address, make sure that any sections it might
747 mention are initialized. */
748 if (s->addr_tree != NULL)
749 exp_init_os (s->addr_tree);
751 if (s->load_base != NULL)
752 exp_init_os (s->load_base);
755 /* Make sure that all output sections mentioned in an expression are
756 initialized. */
758 static void
759 exp_init_os (etree_type *exp)
761 switch (exp->type.node_class)
763 case etree_assign:
764 exp_init_os (exp->assign.src);
765 break;
767 case etree_binary:
768 exp_init_os (exp->binary.lhs);
769 exp_init_os (exp->binary.rhs);
770 break;
772 case etree_trinary:
773 exp_init_os (exp->trinary.cond);
774 exp_init_os (exp->trinary.lhs);
775 exp_init_os (exp->trinary.rhs);
776 break;
778 case etree_assert:
779 exp_init_os (exp->assert_s.child);
780 break;
782 case etree_unary:
783 exp_init_os (exp->unary.child);
784 break;
786 case etree_name:
787 switch (exp->type.node_code)
789 case ADDR:
790 case LOADADDR:
791 case SIZEOF:
793 lang_output_section_statement_type *os;
795 os = lang_output_section_find (exp->name.name);
796 if (os != NULL && os->bfd_section == NULL)
797 init_os (os);
800 break;
802 default:
803 break;
807 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
808 once into the output. This routine checks each section, and
809 arrange to discard it if a section of the same name has already
810 been linked. If the section has COMDAT information, then it uses
811 that to decide whether the section should be included. This code
812 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
813 that is, it does not depend solely upon the section name.
814 section_already_linked is called via bfd_map_over_sections. */
816 /* This is the shape of the elements inside the already_linked hash
817 table. It maps a name onto a list of already_linked elements with
818 the same name. It's possible to get more than one element in a
819 list if the COMDAT sections have different names. */
821 struct already_linked_hash_entry
823 struct bfd_hash_entry root;
824 struct already_linked *entry;
827 struct already_linked
829 struct already_linked *next;
830 asection *sec;
833 /* The hash table. */
835 static struct bfd_hash_table already_linked_table;
837 static void
838 section_already_linked (bfd *abfd, asection *sec, void *data)
840 lang_input_statement_type *entry = data;
841 flagword flags;
842 const char *name;
843 struct already_linked *l;
844 struct already_linked_hash_entry *already_linked_list;
846 /* If we are only reading symbols from this object, then we want to
847 discard all sections. */
848 if (entry->just_syms_flag)
850 bfd_link_just_syms (sec, &link_info);
851 return;
854 flags = bfd_get_section_flags (abfd, sec);
856 if ((flags & SEC_LINK_ONCE) == 0)
857 return;
859 /* FIXME: When doing a relocatable link, we may have trouble
860 copying relocations in other sections that refer to local symbols
861 in the section being discarded. Those relocations will have to
862 be converted somehow; as of this writing I'm not sure that any of
863 the backends handle that correctly.
865 It is tempting to instead not discard link once sections when
866 doing a relocatable link (technically, they should be discarded
867 whenever we are building constructors). However, that fails,
868 because the linker winds up combining all the link once sections
869 into a single large link once section, which defeats the purpose
870 of having link once sections in the first place.
872 Also, not merging link once sections in a relocatable link
873 causes trouble for MIPS ELF, which relies on link once semantics
874 to handle the .reginfo section correctly. */
876 name = bfd_get_section_name (abfd, sec);
878 already_linked_list =
879 ((struct already_linked_hash_entry *)
880 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
882 for (l = already_linked_list->entry; l != NULL; l = l->next)
884 if (sec->comdat == NULL
885 || l->sec->comdat == NULL
886 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
888 /* The section has already been linked. See if we should
889 issue a warning. */
890 switch (flags & SEC_LINK_DUPLICATES)
892 default:
893 abort ();
895 case SEC_LINK_DUPLICATES_DISCARD:
896 break;
898 case SEC_LINK_DUPLICATES_ONE_ONLY:
899 if (sec->comdat == NULL)
900 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
901 abfd, name);
902 else
903 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
904 abfd, name, sec->comdat->name);
905 break;
907 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
908 /* FIXME: We should really dig out the contents of both
909 sections and memcmp them. The COFF/PE spec says that
910 the Microsoft linker does not implement this
911 correctly, so I'm not going to bother doing it
912 either. */
913 /* Fall through. */
914 case SEC_LINK_DUPLICATES_SAME_SIZE:
915 if (bfd_section_size (abfd, sec)
916 != bfd_section_size (l->sec->owner, l->sec))
917 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
918 abfd, name);
919 break;
922 /* Set the output_section field so that lang_add_section
923 does not create a lang_input_section structure for this
924 section. Since there might be a symbol in the section
925 being discarded, we must retain a pointer to the section
926 which we are really going to use. */
927 sec->output_section = bfd_abs_section_ptr;
928 sec->kept_section = l->sec;
930 if (flags & SEC_GROUP)
931 bfd_discard_group (abfd, sec);
933 return;
937 /* This is the first section with this name. Record it. Allocate
938 the memory from the same obstack as the hash table is kept in. */
940 l = bfd_hash_allocate (&already_linked_table, sizeof *l);
942 l->sec = sec;
943 l->next = already_linked_list->entry;
944 already_linked_list->entry = l;
947 /* Support routines for the hash table used by section_already_linked,
948 initialize the table, fill in an entry and remove the table. */
950 static struct bfd_hash_entry *
951 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
952 struct bfd_hash_table *table,
953 const char *string ATTRIBUTE_UNUSED)
955 struct already_linked_hash_entry *ret =
956 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
958 ret->entry = NULL;
960 return &ret->root;
963 static void
964 already_linked_table_init (void)
966 if (! bfd_hash_table_init_n (&already_linked_table,
967 already_linked_newfunc,
968 42))
969 einfo (_("%P%F: Failed to create hash table\n"));
972 static void
973 already_linked_table_free (void)
975 bfd_hash_table_free (&already_linked_table);
978 /* The wild routines.
980 These expand statements like *(.text) and foo.o to a list of
981 explicit actions, like foo.o(.text), bar.o(.text) and
982 foo.o(.text, .data). */
984 /* Return TRUE if the PATTERN argument is a wildcard pattern.
985 Although backslashes are treated specially if a pattern contains
986 wildcards, we do not consider the mere presence of a backslash to
987 be enough to cause the pattern to be treated as a wildcard.
988 That lets us handle DOS filenames more naturally. */
990 static bfd_boolean
991 wildcardp (const char *pattern)
993 const char *s;
995 for (s = pattern; *s != '\0'; ++s)
996 if (*s == '?'
997 || *s == '*'
998 || *s == '[')
999 return TRUE;
1000 return FALSE;
1003 /* Add SECTION to the output section OUTPUT. Do this by creating a
1004 lang_input_section statement which is placed at PTR. FILE is the
1005 input file which holds SECTION. */
1007 void
1008 lang_add_section (lang_statement_list_type *ptr,
1009 asection *section,
1010 lang_output_section_statement_type *output,
1011 lang_input_statement_type *file)
1013 flagword flags;
1014 bfd_boolean discard;
1016 flags = bfd_get_section_flags (section->owner, section);
1018 discard = FALSE;
1020 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1021 link. Discard debugging sections marked with SEC_EXCLUDE on a
1022 relocatable link too. */
1023 if ((flags & SEC_EXCLUDE) != 0
1024 && ((flags & SEC_DEBUGGING) != 0 || !link_info.relocatable))
1025 discard = TRUE;
1027 /* Discard input sections which are assigned to a section named
1028 DISCARD_SECTION_NAME. */
1029 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1030 discard = TRUE;
1032 /* Discard debugging sections if we are stripping debugging
1033 information. */
1034 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1035 && (flags & SEC_DEBUGGING) != 0)
1036 discard = TRUE;
1038 if (discard)
1040 if (section->output_section == NULL)
1042 /* This prevents future calls from assigning this section. */
1043 section->output_section = bfd_abs_section_ptr;
1045 return;
1048 if (section->output_section == NULL)
1050 bfd_boolean first;
1051 lang_input_section_type *new;
1052 flagword flags;
1054 if (output->bfd_section == NULL)
1055 init_os (output);
1057 first = ! output->bfd_section->linker_has_input;
1058 output->bfd_section->linker_has_input = 1;
1060 /* Add a section reference to the list. */
1061 new = new_stat (lang_input_section, ptr);
1063 new->section = section;
1064 new->ifile = file;
1065 section->output_section = output->bfd_section;
1067 flags = section->flags;
1069 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1070 to an output section, because we want to be able to include a
1071 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1072 section (I don't know why we want to do this, but we do).
1073 build_link_order in ldwrite.c handles this case by turning
1074 the embedded SEC_NEVER_LOAD section into a fill. */
1076 flags &= ~ SEC_NEVER_LOAD;
1078 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1079 already been processed. One reason to do this is that on pe
1080 format targets, .text$foo sections go into .text and it's odd
1081 to see .text with SEC_LINK_ONCE set. */
1083 if (! link_info.relocatable)
1084 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1086 /* If this is not the first input section, and the SEC_READONLY
1087 flag is not currently set, then don't set it just because the
1088 input section has it set. */
1090 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1091 flags &= ~ SEC_READONLY;
1093 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1094 if (! first
1095 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1096 != (flags & (SEC_MERGE | SEC_STRINGS))
1097 || ((flags & SEC_MERGE)
1098 && section->output_section->entsize != section->entsize)))
1100 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1101 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1104 /* For now make .tbss normal section. */
1105 if ((flags & SEC_THREAD_LOCAL) && ! link_info.relocatable)
1106 flags |= SEC_LOAD;
1108 section->output_section->flags |= flags;
1110 if (flags & SEC_MERGE)
1111 section->output_section->entsize = section->entsize;
1113 /* If SEC_READONLY is not set in the input section, then clear
1114 it from the output section. */
1115 if ((section->flags & SEC_READONLY) == 0)
1116 section->output_section->flags &= ~SEC_READONLY;
1118 switch (output->sectype)
1120 case normal_section:
1121 break;
1122 case dsect_section:
1123 case copy_section:
1124 case info_section:
1125 case overlay_section:
1126 output->bfd_section->flags &= ~SEC_ALLOC;
1127 break;
1128 case noload_section:
1129 output->bfd_section->flags &= ~SEC_LOAD;
1130 output->bfd_section->flags |= SEC_NEVER_LOAD;
1131 break;
1134 /* Copy over SEC_SMALL_DATA. */
1135 if (section->flags & SEC_SMALL_DATA)
1136 section->output_section->flags |= SEC_SMALL_DATA;
1138 if (section->alignment_power > output->bfd_section->alignment_power)
1139 output->bfd_section->alignment_power = section->alignment_power;
1141 /* If supplied an alignment, then force it. */
1142 if (output->section_alignment != -1)
1143 output->bfd_section->alignment_power = output->section_alignment;
1145 if (section->flags & SEC_BLOCK)
1147 section->output_section->flags |= SEC_BLOCK;
1148 /* FIXME: This value should really be obtained from the bfd... */
1149 output->block_value = 128;
1154 /* Handle wildcard sorting. This returns the lang_input_section which
1155 should follow the one we are going to create for SECTION and FILE,
1156 based on the sorting requirements of WILD. It returns NULL if the
1157 new section should just go at the end of the current list. */
1159 static lang_statement_union_type *
1160 wild_sort (lang_wild_statement_type *wild,
1161 struct wildcard_list *sec,
1162 lang_input_statement_type *file,
1163 asection *section)
1165 const char *section_name;
1166 lang_statement_union_type *l;
1168 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1169 return NULL;
1171 section_name = bfd_get_section_name (file->the_bfd, section);
1172 for (l = wild->children.head; l != NULL; l = l->header.next)
1174 lang_input_section_type *ls;
1176 if (l->header.type != lang_input_section_enum)
1177 continue;
1178 ls = &l->input_section;
1180 /* Sorting by filename takes precedence over sorting by section
1181 name. */
1183 if (wild->filenames_sorted)
1185 const char *fn, *ln;
1186 bfd_boolean fa, la;
1187 int i;
1189 /* The PE support for the .idata section as generated by
1190 dlltool assumes that files will be sorted by the name of
1191 the archive and then the name of the file within the
1192 archive. */
1194 if (file->the_bfd != NULL
1195 && bfd_my_archive (file->the_bfd) != NULL)
1197 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1198 fa = TRUE;
1200 else
1202 fn = file->filename;
1203 fa = FALSE;
1206 if (ls->ifile->the_bfd != NULL
1207 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1209 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1210 la = TRUE;
1212 else
1214 ln = ls->ifile->filename;
1215 la = FALSE;
1218 i = strcmp (fn, ln);
1219 if (i > 0)
1220 continue;
1221 else if (i < 0)
1222 break;
1224 if (fa || la)
1226 if (fa)
1227 fn = file->filename;
1228 if (la)
1229 ln = ls->ifile->filename;
1231 i = strcmp (fn, ln);
1232 if (i > 0)
1233 continue;
1234 else if (i < 0)
1235 break;
1239 /* Here either the files are not sorted by name, or we are
1240 looking at the sections for this file. */
1242 if (sec != NULL && sec->spec.sorted)
1244 if (strcmp (section_name,
1245 bfd_get_section_name (ls->ifile->the_bfd,
1246 ls->section))
1247 < 0)
1248 break;
1252 return l;
1255 /* Expand a wild statement for a particular FILE. SECTION may be
1256 NULL, in which case it is a wild card. */
1258 static void
1259 output_section_callback (lang_wild_statement_type *ptr,
1260 struct wildcard_list *sec,
1261 asection *section,
1262 lang_input_statement_type *file,
1263 void *output)
1265 lang_statement_union_type *before;
1267 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1268 if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1269 return;
1271 /* If the wild pattern was marked KEEP, the member sections
1272 should be as well. */
1273 if (ptr->keep_sections)
1274 section->flags |= SEC_KEEP;
1276 before = wild_sort (ptr, sec, file, section);
1278 /* Here BEFORE points to the lang_input_section which
1279 should follow the one we are about to add. If BEFORE
1280 is NULL, then the section should just go at the end
1281 of the current list. */
1283 if (before == NULL)
1284 lang_add_section (&ptr->children, section,
1285 (lang_output_section_statement_type *) output,
1286 file);
1287 else
1289 lang_statement_list_type list;
1290 lang_statement_union_type **pp;
1292 lang_list_init (&list);
1293 lang_add_section (&list, section,
1294 (lang_output_section_statement_type *) output,
1295 file);
1297 /* If we are discarding the section, LIST.HEAD will
1298 be NULL. */
1299 if (list.head != NULL)
1301 ASSERT (list.head->header.next == NULL);
1303 for (pp = &ptr->children.head;
1304 *pp != before;
1305 pp = &(*pp)->header.next)
1306 ASSERT (*pp != NULL);
1308 list.head->header.next = *pp;
1309 *pp = list.head;
1314 /* This is passed a file name which must have been seen already and
1315 added to the statement tree. We will see if it has been opened
1316 already and had its symbols read. If not then we'll read it. */
1318 static lang_input_statement_type *
1319 lookup_name (const char *name)
1321 lang_input_statement_type *search;
1323 for (search = (lang_input_statement_type *) input_file_chain.head;
1324 search != NULL;
1325 search = (lang_input_statement_type *) search->next_real_file)
1327 /* Use the local_sym_name as the name of the file that has
1328 already been loaded as filename might have been transformed
1329 via the search directory lookup mechanism. */
1330 const char * filename = search->local_sym_name;
1332 if (filename == NULL && name == NULL)
1333 return search;
1334 if (filename != NULL
1335 && name != NULL
1336 && strcmp (filename, name) == 0)
1337 break;
1340 if (search == NULL)
1341 search = new_afile (name, lang_input_file_is_search_file_enum, default_target,
1342 FALSE);
1344 /* If we have already added this file, or this file is not real
1345 (FIXME: can that ever actually happen?) or the name is NULL
1346 (FIXME: can that ever actually happen?) don't add this file. */
1347 if (search->loaded
1348 || ! search->real
1349 || search->filename == NULL)
1350 return search;
1352 if (! load_symbols (search, NULL))
1353 return NULL;
1355 return search;
1358 /* Get the symbols for an input file. */
1360 static bfd_boolean
1361 load_symbols (lang_input_statement_type *entry,
1362 lang_statement_list_type *place)
1364 char **matching;
1366 if (entry->loaded)
1367 return TRUE;
1369 ldfile_open_file (entry);
1371 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1372 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1374 bfd_error_type err;
1375 lang_statement_list_type *hold;
1376 bfd_boolean bad_load = TRUE;
1377 bfd_boolean save_ldlang_sysrooted_script;
1379 err = bfd_get_error ();
1381 /* See if the emulation has some special knowledge. */
1382 if (ldemul_unrecognized_file (entry))
1383 return TRUE;
1385 if (err == bfd_error_file_ambiguously_recognized)
1387 char **p;
1389 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1390 einfo (_("%B: matching formats:"), entry->the_bfd);
1391 for (p = matching; *p != NULL; p++)
1392 einfo (" %s", *p);
1393 einfo ("%F\n");
1395 else if (err != bfd_error_file_not_recognized
1396 || place == NULL)
1397 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1398 else
1399 bad_load = FALSE;
1401 bfd_close (entry->the_bfd);
1402 entry->the_bfd = NULL;
1404 /* Try to interpret the file as a linker script. */
1405 ldfile_open_command_file (entry->filename);
1407 hold = stat_ptr;
1408 stat_ptr = place;
1409 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1410 ldlang_sysrooted_script = entry->sysrooted;
1412 ldfile_assumed_script = TRUE;
1413 parser_input = input_script;
1414 yyparse ();
1415 ldfile_assumed_script = FALSE;
1417 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1418 stat_ptr = hold;
1420 return ! bad_load;
1423 if (ldemul_recognized_file (entry))
1424 return TRUE;
1426 /* We don't call ldlang_add_file for an archive. Instead, the
1427 add_symbols entry point will call ldlang_add_file, via the
1428 add_archive_element callback, for each element of the archive
1429 which is used. */
1430 switch (bfd_get_format (entry->the_bfd))
1432 default:
1433 break;
1435 case bfd_object:
1436 ldlang_add_file (entry);
1437 if (trace_files || trace_file_tries)
1438 info_msg ("%I\n", entry);
1439 break;
1441 case bfd_archive:
1442 if (entry->whole_archive)
1444 bfd *member = NULL;
1445 bfd_boolean loaded = TRUE;
1447 for (;;)
1449 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1451 if (member == NULL)
1452 break;
1454 if (! bfd_check_format (member, bfd_object))
1456 einfo (_("%F%B: member %B in archive is not an object\n"),
1457 entry->the_bfd, member);
1458 loaded = FALSE;
1461 if (! ((*link_info.callbacks->add_archive_element)
1462 (&link_info, member, "--whole-archive")))
1463 abort ();
1465 if (! bfd_link_add_symbols (member, &link_info))
1467 einfo (_("%F%B: could not read symbols: %E\n"), member);
1468 loaded = FALSE;
1472 entry->loaded = loaded;
1473 return loaded;
1475 break;
1478 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1479 entry->loaded = TRUE;
1480 else
1481 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1483 return entry->loaded;
1486 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1487 may be NULL, indicating that it is a wildcard. Separate
1488 lang_input_section statements are created for each part of the
1489 expansion; they are added after the wild statement S. OUTPUT is
1490 the output section. */
1492 static void
1493 wild (lang_wild_statement_type *s,
1494 const char *target ATTRIBUTE_UNUSED,
1495 lang_output_section_statement_type *output)
1497 struct wildcard_list *sec;
1499 walk_wild (s, output_section_callback, output);
1501 for (sec = s->section_list; sec != NULL; sec = sec->next)
1503 if (default_common_section != NULL)
1504 break;
1505 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1507 /* Remember the section that common is going to in case we
1508 later get something which doesn't know where to put it. */
1509 default_common_section = output;
1514 /* Return TRUE iff target is the sought target. */
1516 static int
1517 get_target (const bfd_target *target, void *data)
1519 const char *sought = data;
1521 return strcmp (target->name, sought) == 0;
1524 /* Like strcpy() but convert to lower case as well. */
1526 static void
1527 stricpy (char *dest, char *src)
1529 char c;
1531 while ((c = *src++) != 0)
1532 *dest++ = TOLOWER (c);
1534 *dest = 0;
1537 /* Remove the first occurrence of needle (if any) in haystack
1538 from haystack. */
1540 static void
1541 strcut (char *haystack, char *needle)
1543 haystack = strstr (haystack, needle);
1545 if (haystack)
1547 char *src;
1549 for (src = haystack + strlen (needle); *src;)
1550 *haystack++ = *src++;
1552 *haystack = 0;
1556 /* Compare two target format name strings.
1557 Return a value indicating how "similar" they are. */
1559 static int
1560 name_compare (char *first, char *second)
1562 char *copy1;
1563 char *copy2;
1564 int result;
1566 copy1 = xmalloc (strlen (first) + 1);
1567 copy2 = xmalloc (strlen (second) + 1);
1569 /* Convert the names to lower case. */
1570 stricpy (copy1, first);
1571 stricpy (copy2, second);
1573 /* Remove size and endian strings from the name. */
1574 strcut (copy1, "big");
1575 strcut (copy1, "little");
1576 strcut (copy2, "big");
1577 strcut (copy2, "little");
1579 /* Return a value based on how many characters match,
1580 starting from the beginning. If both strings are
1581 the same then return 10 * their length. */
1582 for (result = 0; copy1[result] == copy2[result]; result++)
1583 if (copy1[result] == 0)
1585 result *= 10;
1586 break;
1589 free (copy1);
1590 free (copy2);
1592 return result;
1595 /* Set by closest_target_match() below. */
1596 static const bfd_target *winner;
1598 /* Scan all the valid bfd targets looking for one that has the endianness
1599 requirement that was specified on the command line, and is the nearest
1600 match to the original output target. */
1602 static int
1603 closest_target_match (const bfd_target *target, void *data)
1605 const bfd_target *original = data;
1607 if (command_line.endian == ENDIAN_BIG
1608 && target->byteorder != BFD_ENDIAN_BIG)
1609 return 0;
1611 if (command_line.endian == ENDIAN_LITTLE
1612 && target->byteorder != BFD_ENDIAN_LITTLE)
1613 return 0;
1615 /* Must be the same flavour. */
1616 if (target->flavour != original->flavour)
1617 return 0;
1619 /* If we have not found a potential winner yet, then record this one. */
1620 if (winner == NULL)
1622 winner = target;
1623 return 0;
1626 /* Oh dear, we now have two potential candidates for a successful match.
1627 Compare their names and choose the better one. */
1628 if (name_compare (target->name, original->name)
1629 > name_compare (winner->name, original->name))
1630 winner = target;
1632 /* Keep on searching until wqe have checked them all. */
1633 return 0;
1636 /* Return the BFD target format of the first input file. */
1638 static char *
1639 get_first_input_target (void)
1641 char *target = NULL;
1643 LANG_FOR_EACH_INPUT_STATEMENT (s)
1645 if (s->header.type == lang_input_statement_enum
1646 && s->real)
1648 ldfile_open_file (s);
1650 if (s->the_bfd != NULL
1651 && bfd_check_format (s->the_bfd, bfd_object))
1653 target = bfd_get_target (s->the_bfd);
1655 if (target != NULL)
1656 break;
1661 return target;
1664 const char *
1665 lang_get_output_target (void)
1667 const char *target;
1669 /* Has the user told us which output format to use? */
1670 if (output_target != NULL)
1671 return output_target;
1673 /* No - has the current target been set to something other than
1674 the default? */
1675 if (current_target != default_target)
1676 return current_target;
1678 /* No - can we determine the format of the first input file? */
1679 target = get_first_input_target ();
1680 if (target != NULL)
1681 return target;
1683 /* Failed - use the default output target. */
1684 return default_target;
1687 /* Open the output file. */
1689 static bfd *
1690 open_output (const char *name)
1692 bfd *output;
1694 output_target = lang_get_output_target ();
1696 /* Has the user requested a particular endianness on the command
1697 line? */
1698 if (command_line.endian != ENDIAN_UNSET)
1700 const bfd_target *target;
1701 enum bfd_endian desired_endian;
1703 /* Get the chosen target. */
1704 target = bfd_search_for_target (get_target, (void *) output_target);
1706 /* If the target is not supported, we cannot do anything. */
1707 if (target != NULL)
1709 if (command_line.endian == ENDIAN_BIG)
1710 desired_endian = BFD_ENDIAN_BIG;
1711 else
1712 desired_endian = BFD_ENDIAN_LITTLE;
1714 /* See if the target has the wrong endianness. This should
1715 not happen if the linker script has provided big and
1716 little endian alternatives, but some scrips don't do
1717 this. */
1718 if (target->byteorder != desired_endian)
1720 /* If it does, then see if the target provides
1721 an alternative with the correct endianness. */
1722 if (target->alternative_target != NULL
1723 && (target->alternative_target->byteorder == desired_endian))
1724 output_target = target->alternative_target->name;
1725 else
1727 /* Try to find a target as similar as possible to
1728 the default target, but which has the desired
1729 endian characteristic. */
1730 bfd_search_for_target (closest_target_match,
1731 (void *) target);
1733 /* Oh dear - we could not find any targets that
1734 satisfy our requirements. */
1735 if (winner == NULL)
1736 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1737 else
1738 output_target = winner->name;
1744 output = bfd_openw (name, output_target);
1746 if (output == NULL)
1748 if (bfd_get_error () == bfd_error_invalid_target)
1749 einfo (_("%P%F: target %s not found\n"), output_target);
1751 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1754 delete_output_file_on_failure = TRUE;
1756 #if 0
1757 output->flags |= D_PAGED;
1758 #endif
1760 if (! bfd_set_format (output, bfd_object))
1761 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1762 if (! bfd_set_arch_mach (output,
1763 ldfile_output_architecture,
1764 ldfile_output_machine))
1765 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1767 link_info.hash = bfd_link_hash_table_create (output);
1768 if (link_info.hash == NULL)
1769 einfo (_("%P%F: can not create link hash table: %E\n"));
1771 bfd_set_gp_size (output, g_switch_value);
1772 return output;
1775 static void
1776 ldlang_open_output (lang_statement_union_type *statement)
1778 switch (statement->header.type)
1780 case lang_output_statement_enum:
1781 ASSERT (output_bfd == NULL);
1782 output_bfd = open_output (statement->output_statement.name);
1783 ldemul_set_output_arch ();
1784 if (config.magic_demand_paged && !link_info.relocatable)
1785 output_bfd->flags |= D_PAGED;
1786 else
1787 output_bfd->flags &= ~D_PAGED;
1788 if (config.text_read_only)
1789 output_bfd->flags |= WP_TEXT;
1790 else
1791 output_bfd->flags &= ~WP_TEXT;
1792 if (link_info.traditional_format)
1793 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1794 else
1795 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1796 break;
1798 case lang_target_statement_enum:
1799 current_target = statement->target_statement.target;
1800 break;
1801 default:
1802 break;
1806 /* Convert between addresses in bytes and sizes in octets.
1807 For currently supported targets, octets_per_byte is always a power
1808 of two, so we can use shifts. */
1809 #define TO_ADDR(X) ((X) >> opb_shift)
1810 #define TO_SIZE(X) ((X) << opb_shift)
1812 /* Support the above. */
1813 static unsigned int opb_shift = 0;
1815 static void
1816 init_opb (void)
1818 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1819 ldfile_output_machine);
1820 opb_shift = 0;
1821 if (x > 1)
1822 while ((x & 1) == 0)
1824 x >>= 1;
1825 ++opb_shift;
1827 ASSERT (x == 1);
1830 /* Open all the input files. */
1832 static void
1833 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1835 for (; s != NULL; s = s->header.next)
1837 switch (s->header.type)
1839 case lang_constructors_statement_enum:
1840 open_input_bfds (constructor_list.head, force);
1841 break;
1842 case lang_output_section_statement_enum:
1843 open_input_bfds (s->output_section_statement.children.head, force);
1844 break;
1845 case lang_wild_statement_enum:
1846 /* Maybe we should load the file's symbols. */
1847 if (s->wild_statement.filename
1848 && ! wildcardp (s->wild_statement.filename))
1849 (void) lookup_name (s->wild_statement.filename);
1850 open_input_bfds (s->wild_statement.children.head, force);
1851 break;
1852 case lang_group_statement_enum:
1854 struct bfd_link_hash_entry *undefs;
1856 /* We must continually search the entries in the group
1857 until no new symbols are added to the list of undefined
1858 symbols. */
1862 undefs = link_info.hash->undefs_tail;
1863 open_input_bfds (s->group_statement.children.head, TRUE);
1865 while (undefs != link_info.hash->undefs_tail);
1867 break;
1868 case lang_target_statement_enum:
1869 current_target = s->target_statement.target;
1870 break;
1871 case lang_input_statement_enum:
1872 if (s->input_statement.real)
1874 lang_statement_list_type add;
1876 s->input_statement.target = current_target;
1878 /* If we are being called from within a group, and this
1879 is an archive which has already been searched, then
1880 force it to be researched unless the whole archive
1881 has been loaded already. */
1882 if (force
1883 && !s->input_statement.whole_archive
1884 && s->input_statement.loaded
1885 && bfd_check_format (s->input_statement.the_bfd,
1886 bfd_archive))
1887 s->input_statement.loaded = FALSE;
1889 lang_list_init (&add);
1891 if (! load_symbols (&s->input_statement, &add))
1892 config.make_executable = FALSE;
1894 if (add.head != NULL)
1896 *add.tail = s->header.next;
1897 s->header.next = add.head;
1900 break;
1901 default:
1902 break;
1907 /* If there are [COMMONS] statements, put a wild one into the bss
1908 section. */
1910 static void
1911 lang_reasonable_defaults (void)
1913 #if 0
1914 lang_output_section_statement_lookup (".text");
1915 lang_output_section_statement_lookup (".data");
1917 default_common_section = lang_output_section_statement_lookup (".bss");
1919 if (!placed_commons)
1921 lang_wild_statement_type *new =
1922 new_stat (lang_wild_statement,
1923 &default_common_section->children);
1925 new->section_name = "COMMON";
1926 new->filename = NULL;
1927 lang_list_init (&new->children);
1929 #endif
1932 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1934 void
1935 lang_track_definedness (const char *name)
1937 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1938 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1941 /* New-function for the definedness hash table. */
1943 static struct bfd_hash_entry *
1944 lang_definedness_newfunc (struct bfd_hash_entry *entry,
1945 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1946 const char *name ATTRIBUTE_UNUSED)
1948 struct lang_definedness_hash_entry *ret
1949 = (struct lang_definedness_hash_entry *) entry;
1951 if (ret == NULL)
1952 ret = (struct lang_definedness_hash_entry *)
1953 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1955 if (ret == NULL)
1956 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1958 ret->iteration = -1;
1959 return &ret->root;
1962 /* Return the iteration when the definition of NAME was last updated. A
1963 value of -1 means that the symbol is not defined in the linker script
1964 or the command line, but may be defined in the linker symbol table. */
1967 lang_symbol_definition_iteration (const char *name)
1969 struct lang_definedness_hash_entry *defentry
1970 = (struct lang_definedness_hash_entry *)
1971 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1973 /* We've already created this one on the presence of DEFINED in the
1974 script, so it can't be NULL unless something is borked elsewhere in
1975 the code. */
1976 if (defentry == NULL)
1977 FAIL ();
1979 return defentry->iteration;
1982 /* Update the definedness state of NAME. */
1984 void
1985 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1987 struct lang_definedness_hash_entry *defentry
1988 = (struct lang_definedness_hash_entry *)
1989 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1991 /* We don't keep track of symbols not tested with DEFINED. */
1992 if (defentry == NULL)
1993 return;
1995 /* If the symbol was already defined, and not from an earlier statement
1996 iteration, don't update the definedness iteration, because that'd
1997 make the symbol seem defined in the linker script at this point, and
1998 it wasn't; it was defined in some object. If we do anyway, DEFINED
1999 would start to yield false before this point and the construct "sym =
2000 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
2001 in an object. */
2002 if (h->type != bfd_link_hash_undefined
2003 && h->type != bfd_link_hash_common
2004 && h->type != bfd_link_hash_new
2005 && defentry->iteration == -1)
2006 return;
2008 defentry->iteration = lang_statement_iteration;
2011 /* Add the supplied name to the symbol table as an undefined reference.
2012 This is a two step process as the symbol table doesn't even exist at
2013 the time the ld command line is processed. First we put the name
2014 on a list, then, once the output file has been opened, transfer the
2015 name to the symbol table. */
2017 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2019 #define ldlang_undef_chain_list_head entry_symbol.next
2021 void
2022 ldlang_add_undef (const char *const name)
2024 ldlang_undef_chain_list_type *new =
2025 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2027 new->next = ldlang_undef_chain_list_head;
2028 ldlang_undef_chain_list_head = new;
2030 new->name = xstrdup (name);
2032 if (output_bfd != NULL)
2033 insert_undefined (new->name);
2036 /* Insert NAME as undefined in the symbol table. */
2038 static void
2039 insert_undefined (const char *name)
2041 struct bfd_link_hash_entry *h;
2043 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2044 if (h == NULL)
2045 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2046 if (h->type == bfd_link_hash_new)
2048 h->type = bfd_link_hash_undefined;
2049 h->u.undef.abfd = NULL;
2050 bfd_link_add_undef (link_info.hash, h);
2054 /* Run through the list of undefineds created above and place them
2055 into the linker hash table as undefined symbols belonging to the
2056 script file. */
2058 static void
2059 lang_place_undefineds (void)
2061 ldlang_undef_chain_list_type *ptr;
2063 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2064 insert_undefined (ptr->name);
2067 /* Open input files and attach to output sections. */
2069 static void
2070 map_input_to_output_sections
2071 (lang_statement_union_type *s, const char *target,
2072 lang_output_section_statement_type *output_section_statement)
2074 for (; s != NULL; s = s->header.next)
2076 switch (s->header.type)
2078 case lang_wild_statement_enum:
2079 wild (&s->wild_statement, target, output_section_statement);
2080 break;
2081 case lang_constructors_statement_enum:
2082 map_input_to_output_sections (constructor_list.head,
2083 target,
2084 output_section_statement);
2085 break;
2086 case lang_output_section_statement_enum:
2087 map_input_to_output_sections (s->output_section_statement.children.head,
2088 target,
2089 &s->output_section_statement);
2090 break;
2091 case lang_output_statement_enum:
2092 break;
2093 case lang_target_statement_enum:
2094 target = s->target_statement.target;
2095 break;
2096 case lang_group_statement_enum:
2097 map_input_to_output_sections (s->group_statement.children.head,
2098 target,
2099 output_section_statement);
2100 break;
2101 case lang_data_statement_enum:
2102 /* Make sure that any sections mentioned in the expression
2103 are initialized. */
2104 exp_init_os (s->data_statement.exp);
2105 /* FALLTHROUGH */
2106 case lang_fill_statement_enum:
2107 case lang_input_section_enum:
2108 case lang_object_symbols_statement_enum:
2109 case lang_reloc_statement_enum:
2110 case lang_padding_statement_enum:
2111 case lang_input_statement_enum:
2112 if (output_section_statement != NULL
2113 && output_section_statement->bfd_section == NULL)
2114 init_os (output_section_statement);
2115 break;
2116 case lang_assignment_statement_enum:
2117 if (output_section_statement != NULL
2118 && output_section_statement->bfd_section == NULL)
2119 init_os (output_section_statement);
2121 /* Make sure that any sections mentioned in the assignment
2122 are initialized. */
2123 exp_init_os (s->assignment_statement.exp);
2124 break;
2125 case lang_afile_asection_pair_statement_enum:
2126 FAIL ();
2127 break;
2128 case lang_address_statement_enum:
2129 /* Mark the specified section with the supplied address. */
2131 lang_output_section_statement_type *os =
2132 lang_output_section_statement_lookup
2133 (s->address_statement.section_name);
2135 if (os->bfd_section == NULL)
2136 init_os (os);
2137 os->addr_tree = s->address_statement.address;
2139 break;
2144 /* An output section might have been removed after its statement was
2145 added. For example, ldemul_before_allocation can remove dynamic
2146 sections if they turn out to be not needed. Clean them up here. */
2148 static void
2149 strip_excluded_output_sections (void)
2151 lang_statement_union_type *u;
2153 for (u = lang_output_section_statement.head;
2154 u != NULL;
2155 u = u->output_section_statement.next)
2157 lang_output_section_statement_type *os;
2158 asection *s;
2160 os = &u->output_section_statement;
2161 s = os->bfd_section;
2162 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2164 asection **p;
2166 os->bfd_section = NULL;
2168 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2169 if (*p == s)
2171 bfd_section_list_remove (output_bfd, p);
2172 output_bfd->section_count--;
2173 break;
2179 static void
2180 print_output_section_statement
2181 (lang_output_section_statement_type *output_section_statement)
2183 asection *section = output_section_statement->bfd_section;
2184 int len;
2186 if (output_section_statement != abs_output_section)
2188 minfo ("\n%s", output_section_statement->name);
2190 if (section != NULL)
2192 print_dot = section->vma;
2194 len = strlen (output_section_statement->name);
2195 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2197 print_nl ();
2198 len = 0;
2200 while (len < SECTION_NAME_MAP_LENGTH)
2202 print_space ();
2203 ++len;
2206 minfo ("0x%V %W", section->vma, section->_raw_size);
2208 if (output_section_statement->load_base != NULL)
2210 bfd_vma addr;
2212 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2213 "load base", lang_final_phase_enum);
2214 minfo (_(" load address 0x%V"), addr);
2218 print_nl ();
2221 print_statement_list (output_section_statement->children.head,
2222 output_section_statement);
2225 static void
2226 print_assignment (lang_assignment_statement_type *assignment,
2227 lang_output_section_statement_type *output_section)
2229 int i;
2230 etree_value_type result;
2232 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2233 print_space ();
2235 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2236 lang_final_phase_enum, print_dot, &print_dot);
2237 if (result.valid_p)
2239 const char *dst;
2240 bfd_vma value;
2242 value = result.value + result.section->bfd_section->vma;
2243 dst = assignment->exp->assign.dst;
2245 minfo ("0x%V", value);
2246 if (dst[0] == '.' && dst[1] == 0)
2247 print_dot = value;
2249 else
2251 minfo ("*undef* ");
2252 #ifdef BFD64
2253 minfo (" ");
2254 #endif
2257 minfo (" ");
2259 exp_print_tree (assignment->exp);
2261 print_nl ();
2264 static void
2265 print_input_statement (lang_input_statement_type *statm)
2267 if (statm->filename != NULL)
2269 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2273 /* Print all symbols defined in a particular section. This is called
2274 via bfd_link_hash_traverse. */
2276 static bfd_boolean
2277 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2279 asection *sec = ptr;
2281 if ((hash_entry->type == bfd_link_hash_defined
2282 || hash_entry->type == bfd_link_hash_defweak)
2283 && sec == hash_entry->u.def.section)
2285 int i;
2287 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2288 print_space ();
2289 minfo ("0x%V ",
2290 (hash_entry->u.def.value
2291 + hash_entry->u.def.section->output_offset
2292 + hash_entry->u.def.section->output_section->vma));
2294 minfo (" %T\n", hash_entry->root.string);
2297 return TRUE;
2300 /* Print information about an input section to the map file. */
2302 static void
2303 print_input_section (lang_input_section_type *in)
2305 asection *i = in->section;
2306 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2308 init_opb ();
2309 if (size != 0)
2311 print_space ();
2313 minfo ("%s", i->name);
2315 if (i->output_section != NULL)
2317 int len;
2319 len = 1 + strlen (i->name);
2320 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2322 print_nl ();
2323 len = 0;
2325 while (len < SECTION_NAME_MAP_LENGTH)
2327 print_space ();
2328 ++len;
2331 minfo ("0x%V %W %B\n",
2332 i->output_section->vma + i->output_offset, TO_ADDR (size),
2333 i->owner);
2335 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2337 len = SECTION_NAME_MAP_LENGTH + 3;
2338 #ifdef BFD64
2339 len += 16;
2340 #else
2341 len += 8;
2342 #endif
2343 while (len > 0)
2345 print_space ();
2346 --len;
2349 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2352 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2354 print_dot = (i->output_section->vma + i->output_offset
2355 + TO_ADDR (size));
2360 static void
2361 print_fill_statement (lang_fill_statement_type *fill)
2363 size_t size;
2364 unsigned char *p;
2365 fputs (" FILL mask 0x", config.map_file);
2366 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2367 fprintf (config.map_file, "%02x", *p);
2368 fputs ("\n", config.map_file);
2371 static void
2372 print_data_statement (lang_data_statement_type *data)
2374 int i;
2375 bfd_vma addr;
2376 bfd_size_type size;
2377 const char *name;
2379 init_opb ();
2380 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2381 print_space ();
2383 addr = data->output_vma;
2384 if (data->output_section != NULL)
2385 addr += data->output_section->vma;
2387 switch (data->type)
2389 default:
2390 abort ();
2391 case BYTE:
2392 size = BYTE_SIZE;
2393 name = "BYTE";
2394 break;
2395 case SHORT:
2396 size = SHORT_SIZE;
2397 name = "SHORT";
2398 break;
2399 case LONG:
2400 size = LONG_SIZE;
2401 name = "LONG";
2402 break;
2403 case QUAD:
2404 size = QUAD_SIZE;
2405 name = "QUAD";
2406 break;
2407 case SQUAD:
2408 size = QUAD_SIZE;
2409 name = "SQUAD";
2410 break;
2413 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2415 if (data->exp->type.node_class != etree_value)
2417 print_space ();
2418 exp_print_tree (data->exp);
2421 print_nl ();
2423 print_dot = addr + TO_ADDR (size);
2426 /* Print an address statement. These are generated by options like
2427 -Ttext. */
2429 static void
2430 print_address_statement (lang_address_statement_type *address)
2432 minfo (_("Address of section %s set to "), address->section_name);
2433 exp_print_tree (address->address);
2434 print_nl ();
2437 /* Print a reloc statement. */
2439 static void
2440 print_reloc_statement (lang_reloc_statement_type *reloc)
2442 int i;
2443 bfd_vma addr;
2444 bfd_size_type size;
2446 init_opb ();
2447 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2448 print_space ();
2450 addr = reloc->output_vma;
2451 if (reloc->output_section != NULL)
2452 addr += reloc->output_section->vma;
2454 size = bfd_get_reloc_size (reloc->howto);
2456 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2458 if (reloc->name != NULL)
2459 minfo ("%s+", reloc->name);
2460 else
2461 minfo ("%s+", reloc->section->name);
2463 exp_print_tree (reloc->addend_exp);
2465 print_nl ();
2467 print_dot = addr + TO_ADDR (size);
2470 static void
2471 print_padding_statement (lang_padding_statement_type *s)
2473 int len;
2474 bfd_vma addr;
2476 init_opb ();
2477 minfo (" *fill*");
2479 len = sizeof " *fill*" - 1;
2480 while (len < SECTION_NAME_MAP_LENGTH)
2482 print_space ();
2483 ++len;
2486 addr = s->output_offset;
2487 if (s->output_section != NULL)
2488 addr += s->output_section->vma;
2489 minfo ("0x%V %W ", addr, s->size);
2491 if (s->fill->size != 0)
2493 size_t size;
2494 unsigned char *p;
2495 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2496 fprintf (config.map_file, "%02x", *p);
2499 print_nl ();
2501 print_dot = addr + TO_ADDR (s->size);
2504 static void
2505 print_wild_statement (lang_wild_statement_type *w,
2506 lang_output_section_statement_type *os)
2508 struct wildcard_list *sec;
2510 print_space ();
2512 if (w->filenames_sorted)
2513 minfo ("SORT(");
2514 if (w->filename != NULL)
2515 minfo ("%s", w->filename);
2516 else
2517 minfo ("*");
2518 if (w->filenames_sorted)
2519 minfo (")");
2521 minfo ("(");
2522 for (sec = w->section_list; sec; sec = sec->next)
2524 if (sec->spec.sorted)
2525 minfo ("SORT(");
2526 if (sec->spec.exclude_name_list != NULL)
2528 name_list *tmp;
2529 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2530 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2531 minfo (" %s", tmp->name);
2532 minfo (") ");
2534 if (sec->spec.name != NULL)
2535 minfo ("%s", sec->spec.name);
2536 else
2537 minfo ("*");
2538 if (sec->spec.sorted)
2539 minfo (")");
2540 if (sec->next)
2541 minfo (" ");
2543 minfo (")");
2545 print_nl ();
2547 print_statement_list (w->children.head, os);
2550 /* Print a group statement. */
2552 static void
2553 print_group (lang_group_statement_type *s,
2554 lang_output_section_statement_type *os)
2556 fprintf (config.map_file, "START GROUP\n");
2557 print_statement_list (s->children.head, os);
2558 fprintf (config.map_file, "END GROUP\n");
2561 /* Print the list of statements in S.
2562 This can be called for any statement type. */
2564 static void
2565 print_statement_list (lang_statement_union_type *s,
2566 lang_output_section_statement_type *os)
2568 while (s != NULL)
2570 print_statement (s, os);
2571 s = s->header.next;
2575 /* Print the first statement in statement list S.
2576 This can be called for any statement type. */
2578 static void
2579 print_statement (lang_statement_union_type *s,
2580 lang_output_section_statement_type *os)
2582 switch (s->header.type)
2584 default:
2585 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2586 FAIL ();
2587 break;
2588 case lang_constructors_statement_enum:
2589 if (constructor_list.head != NULL)
2591 if (constructors_sorted)
2592 minfo (" SORT (CONSTRUCTORS)\n");
2593 else
2594 minfo (" CONSTRUCTORS\n");
2595 print_statement_list (constructor_list.head, os);
2597 break;
2598 case lang_wild_statement_enum:
2599 print_wild_statement (&s->wild_statement, os);
2600 break;
2601 case lang_address_statement_enum:
2602 print_address_statement (&s->address_statement);
2603 break;
2604 case lang_object_symbols_statement_enum:
2605 minfo (" CREATE_OBJECT_SYMBOLS\n");
2606 break;
2607 case lang_fill_statement_enum:
2608 print_fill_statement (&s->fill_statement);
2609 break;
2610 case lang_data_statement_enum:
2611 print_data_statement (&s->data_statement);
2612 break;
2613 case lang_reloc_statement_enum:
2614 print_reloc_statement (&s->reloc_statement);
2615 break;
2616 case lang_input_section_enum:
2617 print_input_section (&s->input_section);
2618 break;
2619 case lang_padding_statement_enum:
2620 print_padding_statement (&s->padding_statement);
2621 break;
2622 case lang_output_section_statement_enum:
2623 print_output_section_statement (&s->output_section_statement);
2624 break;
2625 case lang_assignment_statement_enum:
2626 print_assignment (&s->assignment_statement, os);
2627 break;
2628 case lang_target_statement_enum:
2629 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2630 break;
2631 case lang_output_statement_enum:
2632 minfo ("OUTPUT(%s", s->output_statement.name);
2633 if (output_target != NULL)
2634 minfo (" %s", output_target);
2635 minfo (")\n");
2636 break;
2637 case lang_input_statement_enum:
2638 print_input_statement (&s->input_statement);
2639 break;
2640 case lang_group_statement_enum:
2641 print_group (&s->group_statement, os);
2642 break;
2643 case lang_afile_asection_pair_statement_enum:
2644 FAIL ();
2645 break;
2649 static void
2650 print_statements (void)
2652 print_statement_list (statement_list.head, abs_output_section);
2655 /* Print the first N statements in statement list S to STDERR.
2656 If N == 0, nothing is printed.
2657 If N < 0, the entire list is printed.
2658 Intended to be called from GDB. */
2660 void
2661 dprint_statement (lang_statement_union_type *s, int n)
2663 FILE *map_save = config.map_file;
2665 config.map_file = stderr;
2667 if (n < 0)
2668 print_statement_list (s, abs_output_section);
2669 else
2671 while (s && --n >= 0)
2673 print_statement (s, abs_output_section);
2674 s = s->header.next;
2678 config.map_file = map_save;
2681 static void
2682 insert_pad (lang_statement_union_type **ptr,
2683 fill_type *fill,
2684 unsigned int alignment_needed,
2685 asection *output_section,
2686 bfd_vma dot)
2688 static fill_type zero_fill = { 1, { 0 } };
2689 lang_statement_union_type *pad;
2691 pad = ((lang_statement_union_type *)
2692 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2693 if (ptr != &statement_list.head
2694 && pad->header.type == lang_padding_statement_enum
2695 && pad->padding_statement.output_section == output_section)
2697 /* Use the existing pad statement. The above test on output
2698 section is probably redundant, but it doesn't hurt to check. */
2700 else
2702 /* Make a new padding statement, linked into existing chain. */
2703 pad = stat_alloc (sizeof (lang_padding_statement_type));
2704 pad->header.next = *ptr;
2705 *ptr = pad;
2706 pad->header.type = lang_padding_statement_enum;
2707 pad->padding_statement.output_section = output_section;
2708 if (fill == NULL)
2709 fill = &zero_fill;
2710 pad->padding_statement.fill = fill;
2712 pad->padding_statement.output_offset = dot - output_section->vma;
2713 pad->padding_statement.size = alignment_needed;
2714 output_section->_raw_size += alignment_needed;
2717 /* Work out how much this section will move the dot point. */
2719 static bfd_vma
2720 size_input_section (lang_statement_union_type **this_ptr,
2721 lang_output_section_statement_type *output_section_statement,
2722 fill_type *fill,
2723 bfd_vma dot)
2725 lang_input_section_type *is = &((*this_ptr)->input_section);
2726 asection *i = is->section;
2728 if (!is->ifile->just_syms_flag)
2730 unsigned int alignment_needed;
2731 asection *o;
2733 /* Align this section first to the input sections requirement,
2734 then to the output section's requirement. If this alignment
2735 is greater than any seen before, then record it too. Perform
2736 the alignment by inserting a magic 'padding' statement. */
2738 if (output_section_statement->subsection_alignment != -1)
2739 i->alignment_power = output_section_statement->subsection_alignment;
2741 o = output_section_statement->bfd_section;
2742 if (o->alignment_power < i->alignment_power)
2743 o->alignment_power = i->alignment_power;
2745 alignment_needed = align_power (dot, i->alignment_power) - dot;
2747 if (alignment_needed != 0)
2749 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2750 dot += alignment_needed;
2753 /* Remember where in the output section this input section goes. */
2755 i->output_offset = dot - o->vma;
2757 /* Mark how big the output section must be to contain this now. */
2758 if (i->_cooked_size != 0)
2759 dot += TO_ADDR (i->_cooked_size);
2760 else
2761 dot += TO_ADDR (i->_raw_size);
2762 o->_raw_size = TO_SIZE (dot - o->vma);
2764 else
2766 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2769 return dot;
2772 #define IGNORE_SECTION(bfd, s) \
2773 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_NEVER_LOAD)) \
2774 != SEC_ALLOC) \
2775 || bfd_section_size (bfd, s) == 0)
2777 /* Check to see if any allocated sections overlap with other allocated
2778 sections. This can happen when the linker script specifically specifies
2779 the output section addresses of the two sections. */
2781 static void
2782 lang_check_section_addresses (void)
2784 asection *s;
2786 /* Scan all sections in the output list. */
2787 for (s = output_bfd->sections; s != NULL; s = s->next)
2789 asection *os;
2791 /* Ignore sections which are not loaded or which have no contents. */
2792 if (IGNORE_SECTION (output_bfd, s))
2793 continue;
2795 /* Once we reach section 's' stop our seach. This prevents two
2796 warning messages from being produced, one for 'section A overlaps
2797 section B' and one for 'section B overlaps section A'. */
2798 for (os = output_bfd->sections; os != s; os = os->next)
2800 bfd_vma s_start;
2801 bfd_vma s_end;
2802 bfd_vma os_start;
2803 bfd_vma os_end;
2805 /* Only consider loadable sections with real contents. */
2806 if (IGNORE_SECTION (output_bfd, os))
2807 continue;
2809 /* We must check the sections' LMA addresses not their
2810 VMA addresses because overlay sections can have
2811 overlapping VMAs but they must have distinct LMAs. */
2812 s_start = bfd_section_lma (output_bfd, s);
2813 os_start = bfd_section_lma (output_bfd, os);
2814 s_end = s_start + TO_ADDR (bfd_section_size (output_bfd, s)) - 1;
2815 os_end = os_start + TO_ADDR (bfd_section_size (output_bfd, os)) - 1;
2817 /* Look for an overlap. */
2818 if ((s_end < os_start) || (s_start > os_end))
2819 continue;
2821 einfo (
2822 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2823 s->name, s_start, s_end, os->name, os_start, os_end);
2825 /* Once we have found one overlap for this section,
2826 stop looking for others. */
2827 break;
2832 /* Make sure the new address is within the region. We explicitly permit the
2833 current address to be at the exact end of the region when the address is
2834 non-zero, in case the region is at the end of addressable memory and the
2835 calculation wraps around. */
2837 static void
2838 os_region_check (lang_output_section_statement_type *os,
2839 lang_memory_region_type *region,
2840 etree_type *tree,
2841 bfd_vma base)
2843 if ((region->current < region->origin
2844 || (region->current - region->origin > region->length))
2845 && ((region->current != region->origin + region->length)
2846 || base == 0))
2848 if (tree != NULL)
2850 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2851 region->current,
2852 os->bfd_section->owner,
2853 os->bfd_section->name,
2854 region->name);
2856 else
2858 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2859 region->name,
2860 os->bfd_section->owner,
2861 os->bfd_section->name);
2863 /* Reset the region pointer. */
2864 region->current = region->origin;
2868 /* Set the sizes for all the output sections. */
2870 static bfd_vma
2871 lang_size_sections_1
2872 (lang_statement_union_type *s,
2873 lang_output_section_statement_type *output_section_statement,
2874 lang_statement_union_type **prev,
2875 fill_type *fill,
2876 bfd_vma dot,
2877 bfd_boolean *relax,
2878 bfd_boolean check_regions)
2880 /* Size up the sections from their constituent parts. */
2881 for (; s != NULL; s = s->header.next)
2883 switch (s->header.type)
2885 case lang_output_section_statement_enum:
2887 bfd_vma after;
2888 lang_output_section_statement_type *os;
2890 os = &s->output_section_statement;
2891 if (os->bfd_section == NULL)
2892 /* This section was never actually created. */
2893 break;
2895 /* If this is a COFF shared library section, use the size and
2896 address from the input section. FIXME: This is COFF
2897 specific; it would be cleaner if there were some other way
2898 to do this, but nothing simple comes to mind. */
2899 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2901 asection *input;
2903 if (os->children.head == NULL
2904 || os->children.head->header.next != NULL
2905 || os->children.head->header.type != lang_input_section_enum)
2906 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2907 os->name);
2909 input = os->children.head->input_section.section;
2910 bfd_set_section_vma (os->bfd_section->owner,
2911 os->bfd_section,
2912 bfd_section_vma (input->owner, input));
2913 os->bfd_section->_raw_size = input->_raw_size;
2914 break;
2917 if (bfd_is_abs_section (os->bfd_section))
2919 /* No matter what happens, an abs section starts at zero. */
2920 ASSERT (os->bfd_section->vma == 0);
2922 else
2924 if (os->addr_tree == NULL)
2926 /* No address specified for this section, get one
2927 from the region specification. */
2928 if (os->region == NULL
2929 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2930 & (SEC_ALLOC | SEC_LOAD)) != 0)
2931 && os->region->name[0] == '*'
2932 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0))
2934 os->region = lang_memory_default (os->bfd_section);
2937 /* If a loadable section is using the default memory
2938 region, and some non default memory regions were
2939 defined, issue an error message. */
2940 if (!IGNORE_SECTION (output_bfd, os->bfd_section)
2941 && ! link_info.relocatable
2942 && check_regions
2943 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0
2944 && lang_memory_region_list != NULL
2945 && (strcmp (lang_memory_region_list->name,
2946 DEFAULT_MEMORY_REGION) != 0
2947 || lang_memory_region_list->next != NULL))
2949 /* By default this is an error rather than just a
2950 warning because if we allocate the section to the
2951 default memory region we can end up creating an
2952 excessively large binary, or even seg faulting when
2953 attempting to perform a negative seek. See
2954 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
2955 for an example of this. This behaviour can be
2956 overridden by the using the --no-check-sections
2957 switch. */
2958 if (command_line.check_section_addresses)
2959 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2960 bfd_get_section_name (output_bfd,
2961 os->bfd_section));
2962 else
2963 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2964 bfd_get_section_name (output_bfd,
2965 os->bfd_section));
2968 dot = os->region->current;
2970 if (os->section_alignment == -1)
2972 bfd_vma olddot;
2974 olddot = dot;
2975 dot = align_power (dot,
2976 os->bfd_section->alignment_power);
2978 if (dot != olddot && config.warn_section_align)
2979 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2980 os->name, (unsigned int) (dot - olddot));
2983 else
2985 etree_value_type r;
2987 r = exp_fold_tree (os->addr_tree,
2988 abs_output_section,
2989 lang_allocating_phase_enum,
2990 dot, &dot);
2991 if (!r.valid_p)
2992 einfo (_("%F%S: non constant address expression for section %s\n"),
2993 os->name);
2995 dot = r.value + r.section->bfd_section->vma;
2998 /* The section starts here.
2999 First, align to what the section needs. */
3001 if (os->section_alignment != -1)
3002 dot = align_power (dot, os->section_alignment);
3004 bfd_set_section_vma (0, os->bfd_section, dot);
3006 os->bfd_section->output_offset = 0;
3009 lang_size_sections_1 (os->children.head, os, &os->children.head,
3010 os->fill, dot, relax, check_regions);
3012 /* Put the section within the requested block size, or
3013 align at the block boundary. */
3014 after = ((os->bfd_section->vma
3015 + TO_ADDR (os->bfd_section->_raw_size)
3016 + os->block_value - 1)
3017 & - (bfd_vma) os->block_value);
3019 if (bfd_is_abs_section (os->bfd_section))
3020 ASSERT (after == os->bfd_section->vma);
3021 else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
3022 && (os->bfd_section->flags & SEC_THREAD_LOCAL)
3023 && ! link_info.relocatable)
3024 os->bfd_section->_raw_size = 0;
3025 else
3026 os->bfd_section->_raw_size
3027 = TO_SIZE (after - os->bfd_section->vma);
3029 dot = os->bfd_section->vma + TO_ADDR (os->bfd_section->_raw_size);
3030 os->processed = TRUE;
3032 if (os->update_dot_tree != 0)
3033 exp_fold_tree (os->update_dot_tree, abs_output_section,
3034 lang_allocating_phase_enum, dot, &dot);
3036 /* Update dot in the region ?
3037 We only do this if the section is going to be allocated,
3038 since unallocated sections do not contribute to the region's
3039 overall size in memory.
3041 If the SEC_NEVER_LOAD bit is not set, it will affect the
3042 addresses of sections after it. We have to update
3043 dot. */
3044 if (os->region != NULL
3045 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3046 & SEC_NEVER_LOAD) == 0
3047 || (bfd_get_section_flags (output_bfd, os->bfd_section)
3048 & (SEC_ALLOC | SEC_LOAD))))
3050 os->region->current = dot;
3052 if (check_regions)
3053 /* Make sure the new address is within the region. */
3054 os_region_check (os, os->region, os->addr_tree,
3055 os->bfd_section->vma);
3057 /* If there's no load address specified, use the run
3058 region as the load region. */
3059 if (os->lma_region == NULL && os->load_base == NULL)
3060 os->lma_region = os->region;
3062 if (os->lma_region != NULL && os->lma_region != os->region)
3064 /* Set load_base, which will be handled later. */
3065 os->load_base = exp_intop (os->lma_region->current);
3066 os->lma_region->current +=
3067 TO_ADDR (os->bfd_section->_raw_size);
3068 if (check_regions)
3069 os_region_check (os, os->lma_region, NULL,
3070 os->bfd_section->lma);
3074 break;
3076 case lang_constructors_statement_enum:
3077 dot = lang_size_sections_1 (constructor_list.head,
3078 output_section_statement,
3079 &s->wild_statement.children.head,
3080 fill, dot, relax, check_regions);
3081 break;
3083 case lang_data_statement_enum:
3085 unsigned int size = 0;
3087 s->data_statement.output_vma =
3088 dot - output_section_statement->bfd_section->vma;
3089 s->data_statement.output_section =
3090 output_section_statement->bfd_section;
3092 switch (s->data_statement.type)
3094 default:
3095 abort ();
3096 case QUAD:
3097 case SQUAD:
3098 size = QUAD_SIZE;
3099 break;
3100 case LONG:
3101 size = LONG_SIZE;
3102 break;
3103 case SHORT:
3104 size = SHORT_SIZE;
3105 break;
3106 case BYTE:
3107 size = BYTE_SIZE;
3108 break;
3110 if (size < TO_SIZE ((unsigned) 1))
3111 size = TO_SIZE ((unsigned) 1);
3112 dot += TO_ADDR (size);
3113 output_section_statement->bfd_section->_raw_size += size;
3114 /* The output section gets contents, and then we inspect for
3115 any flags set in the input script which override any ALLOC. */
3116 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3117 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3119 output_section_statement->bfd_section->flags |=
3120 SEC_ALLOC | SEC_LOAD;
3123 break;
3125 case lang_reloc_statement_enum:
3127 int size;
3129 s->reloc_statement.output_vma =
3130 dot - output_section_statement->bfd_section->vma;
3131 s->reloc_statement.output_section =
3132 output_section_statement->bfd_section;
3133 size = bfd_get_reloc_size (s->reloc_statement.howto);
3134 dot += TO_ADDR (size);
3135 output_section_statement->bfd_section->_raw_size += size;
3137 break;
3139 case lang_wild_statement_enum:
3141 dot = lang_size_sections_1 (s->wild_statement.children.head,
3142 output_section_statement,
3143 &s->wild_statement.children.head,
3144 fill, dot, relax, check_regions);
3146 break;
3148 case lang_object_symbols_statement_enum:
3149 link_info.create_object_symbols_section =
3150 output_section_statement->bfd_section;
3151 break;
3152 case lang_output_statement_enum:
3153 case lang_target_statement_enum:
3154 break;
3155 case lang_input_section_enum:
3157 asection *i;
3159 i = (*prev)->input_section.section;
3160 if (! relax)
3162 if (i->_cooked_size == 0)
3163 i->_cooked_size = i->_raw_size;
3165 else
3167 bfd_boolean again;
3169 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3170 einfo (_("%P%F: can't relax section: %E\n"));
3171 if (again)
3172 *relax = TRUE;
3174 dot = size_input_section (prev, output_section_statement,
3175 output_section_statement->fill, dot);
3177 break;
3178 case lang_input_statement_enum:
3179 break;
3180 case lang_fill_statement_enum:
3181 s->fill_statement.output_section =
3182 output_section_statement->bfd_section;
3184 fill = s->fill_statement.fill;
3185 break;
3186 case lang_assignment_statement_enum:
3188 bfd_vma newdot = dot;
3190 exp_fold_tree (s->assignment_statement.exp,
3191 output_section_statement,
3192 lang_allocating_phase_enum,
3193 dot,
3194 &newdot);
3196 if (newdot != dot)
3198 if (output_section_statement == abs_output_section)
3200 /* If we don't have an output section, then just adjust
3201 the default memory address. */
3202 lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE)->current = newdot;
3204 else
3206 /* Insert a pad after this statement. We can't
3207 put the pad before when relaxing, in case the
3208 assignment references dot. */
3209 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3210 output_section_statement->bfd_section, dot);
3212 /* Don't neuter the pad below when relaxing. */
3213 s = s->header.next;
3216 /* If dot is advanced, this implies that the section should
3217 have space allocated to it, unless the user has explicitly
3218 stated that the section should never be loaded. */
3219 if (!(output_section_statement->flags & (SEC_NEVER_LOAD | SEC_ALLOC)))
3220 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3222 dot = newdot;
3225 break;
3227 case lang_padding_statement_enum:
3228 /* If this is the first time lang_size_sections is called,
3229 we won't have any padding statements. If this is the
3230 second or later passes when relaxing, we should allow
3231 padding to shrink. If padding is needed on this pass, it
3232 will be added back in. */
3233 s->padding_statement.size = 0;
3235 /* Make sure output_offset is valid. If relaxation shrinks
3236 the section and this pad isn't needed, it's possible to
3237 have output_offset larger than the final size of the
3238 section. bfd_set_section_contents will complain even for
3239 a pad size of zero. */
3240 s->padding_statement.output_offset
3241 = dot - output_section_statement->bfd_section->vma;
3242 break;
3244 case lang_group_statement_enum:
3245 dot = lang_size_sections_1 (s->group_statement.children.head,
3246 output_section_statement,
3247 &s->group_statement.children.head,
3248 fill, dot, relax, check_regions);
3249 break;
3251 default:
3252 FAIL ();
3253 break;
3255 /* We can only get here when relaxing is turned on. */
3256 case lang_address_statement_enum:
3257 break;
3259 prev = &s->header.next;
3261 return dot;
3264 bfd_vma
3265 lang_size_sections
3266 (lang_statement_union_type *s,
3267 lang_output_section_statement_type *output_section_statement,
3268 lang_statement_union_type **prev,
3269 fill_type *fill,
3270 bfd_vma dot,
3271 bfd_boolean *relax,
3272 bfd_boolean check_regions)
3274 bfd_vma result;
3275 asection *o;
3277 /* Callers of exp_fold_tree need to increment this. */
3278 lang_statement_iteration++;
3280 exp_data_seg.phase = exp_dataseg_none;
3281 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3282 dot, relax, check_regions);
3283 if (exp_data_seg.phase == exp_dataseg_end_seen)
3285 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3286 a page could be saved in the data segment. */
3287 bfd_vma first, last;
3289 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3290 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3291 if (first && last
3292 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3293 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3294 && first + last <= exp_data_seg.pagesize)
3296 exp_data_seg.phase = exp_dataseg_adjust;
3297 result = lang_size_sections_1 (s, output_section_statement, prev,
3298 fill, dot, relax, check_regions);
3302 /* Some backend relaxers want to refer to the output section size. Give
3303 them a section size that does not change on the next call while they
3304 relax. We can't set this at top because lang_reset_memory_regions
3305 which is called before we get here, sets _raw_size to 0 on relaxing
3306 rounds. */
3307 for (o = output_bfd->sections; o != NULL; o = o->next)
3308 o->_cooked_size = o->_raw_size;
3310 return result;
3313 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3315 static bfd_vma
3316 lang_do_assignments_1
3317 (lang_statement_union_type *s,
3318 lang_output_section_statement_type *output_section_statement,
3319 fill_type *fill,
3320 bfd_vma dot)
3322 for (; s != NULL; s = s->header.next)
3324 switch (s->header.type)
3326 case lang_constructors_statement_enum:
3327 dot = lang_do_assignments_1 (constructor_list.head,
3328 output_section_statement,
3329 fill,
3330 dot);
3331 break;
3333 case lang_output_section_statement_enum:
3335 lang_output_section_statement_type *os;
3337 os = &(s->output_section_statement);
3338 if (os->bfd_section != NULL)
3340 dot = os->bfd_section->vma;
3341 (void) lang_do_assignments_1 (os->children.head, os,
3342 os->fill, dot);
3343 dot = (os->bfd_section->vma
3344 + TO_ADDR (os->bfd_section->_raw_size));
3347 if (os->load_base)
3349 /* If nothing has been placed into the output section then
3350 it won't have a bfd_section. */
3351 if (os->bfd_section)
3353 os->bfd_section->lma
3354 = exp_get_abs_int (os->load_base, 0, "load base",
3355 lang_final_phase_enum);
3359 break;
3360 case lang_wild_statement_enum:
3362 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3363 output_section_statement,
3364 fill, dot);
3366 break;
3368 case lang_object_symbols_statement_enum:
3369 case lang_output_statement_enum:
3370 case lang_target_statement_enum:
3371 #if 0
3372 case lang_common_statement_enum:
3373 #endif
3374 break;
3375 case lang_data_statement_enum:
3377 etree_value_type value;
3379 value = exp_fold_tree (s->data_statement.exp,
3380 abs_output_section,
3381 lang_final_phase_enum, dot, &dot);
3382 if (!value.valid_p)
3383 einfo (_("%F%P: invalid data statement\n"));
3384 s->data_statement.value
3385 = value.value + value.section->bfd_section->vma;
3388 unsigned int size;
3389 switch (s->data_statement.type)
3391 default:
3392 abort ();
3393 case QUAD:
3394 case SQUAD:
3395 size = QUAD_SIZE;
3396 break;
3397 case LONG:
3398 size = LONG_SIZE;
3399 break;
3400 case SHORT:
3401 size = SHORT_SIZE;
3402 break;
3403 case BYTE:
3404 size = BYTE_SIZE;
3405 break;
3407 if (size < TO_SIZE ((unsigned) 1))
3408 size = TO_SIZE ((unsigned) 1);
3409 dot += TO_ADDR (size);
3411 break;
3413 case lang_reloc_statement_enum:
3415 etree_value_type value;
3417 value = exp_fold_tree (s->reloc_statement.addend_exp,
3418 abs_output_section,
3419 lang_final_phase_enum, dot, &dot);
3420 s->reloc_statement.addend_value = value.value;
3421 if (!value.valid_p)
3422 einfo (_("%F%P: invalid reloc statement\n"));
3424 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3425 break;
3427 case lang_input_section_enum:
3429 asection *in = s->input_section.section;
3431 if (in->_cooked_size != 0)
3432 dot += TO_ADDR (in->_cooked_size);
3433 else
3434 dot += TO_ADDR (in->_raw_size);
3436 break;
3438 case lang_input_statement_enum:
3439 break;
3440 case lang_fill_statement_enum:
3441 fill = s->fill_statement.fill;
3442 break;
3443 case lang_assignment_statement_enum:
3445 exp_fold_tree (s->assignment_statement.exp,
3446 output_section_statement,
3447 lang_final_phase_enum,
3448 dot,
3449 &dot);
3452 break;
3453 case lang_padding_statement_enum:
3454 dot += TO_ADDR (s->padding_statement.size);
3455 break;
3457 case lang_group_statement_enum:
3458 dot = lang_do_assignments_1 (s->group_statement.children.head,
3459 output_section_statement,
3460 fill, dot);
3462 break;
3464 default:
3465 FAIL ();
3466 break;
3467 case lang_address_statement_enum:
3468 break;
3472 return dot;
3475 void
3476 lang_do_assignments (lang_statement_union_type *s,
3477 lang_output_section_statement_type *output_section_statement,
3478 fill_type *fill,
3479 bfd_vma dot)
3481 /* Callers of exp_fold_tree need to increment this. */
3482 lang_statement_iteration++;
3483 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3486 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3487 operator .startof. (section_name), it produces an undefined symbol
3488 .startof.section_name. Similarly, when it sees
3489 .sizeof. (section_name), it produces an undefined symbol
3490 .sizeof.section_name. For all the output sections, we look for
3491 such symbols, and set them to the correct value. */
3493 static void
3494 lang_set_startof (void)
3496 asection *s;
3498 if (link_info.relocatable)
3499 return;
3501 for (s = output_bfd->sections; s != NULL; s = s->next)
3503 const char *secname;
3504 char *buf;
3505 struct bfd_link_hash_entry *h;
3507 secname = bfd_get_section_name (output_bfd, s);
3508 buf = xmalloc (10 + strlen (secname));
3510 sprintf (buf, ".startof.%s", secname);
3511 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3512 if (h != NULL && h->type == bfd_link_hash_undefined)
3514 h->type = bfd_link_hash_defined;
3515 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3516 h->u.def.section = bfd_abs_section_ptr;
3519 sprintf (buf, ".sizeof.%s", secname);
3520 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3521 if (h != NULL && h->type == bfd_link_hash_undefined)
3523 h->type = bfd_link_hash_defined;
3524 if (s->_cooked_size != 0)
3525 h->u.def.value = TO_ADDR (s->_cooked_size);
3526 else
3527 h->u.def.value = TO_ADDR (s->_raw_size);
3528 h->u.def.section = bfd_abs_section_ptr;
3531 free (buf);
3535 static void
3536 lang_finish (void)
3538 struct bfd_link_hash_entry *h;
3539 bfd_boolean warn;
3541 if (link_info.relocatable || link_info.shared)
3542 warn = FALSE;
3543 else
3544 warn = TRUE;
3546 if (entry_symbol.name == NULL)
3548 /* No entry has been specified. Look for start, but don't warn
3549 if we don't find it. */
3550 entry_symbol.name = "start";
3551 warn = FALSE;
3554 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3555 FALSE, FALSE, TRUE);
3556 if (h != NULL
3557 && (h->type == bfd_link_hash_defined
3558 || h->type == bfd_link_hash_defweak)
3559 && h->u.def.section->output_section != NULL)
3561 bfd_vma val;
3563 val = (h->u.def.value
3564 + bfd_get_section_vma (output_bfd,
3565 h->u.def.section->output_section)
3566 + h->u.def.section->output_offset);
3567 if (! bfd_set_start_address (output_bfd, val))
3568 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3570 else
3572 bfd_vma val;
3573 const char *send;
3575 /* We couldn't find the entry symbol. Try parsing it as a
3576 number. */
3577 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3578 if (*send == '\0')
3580 if (! bfd_set_start_address (output_bfd, val))
3581 einfo (_("%P%F: can't set start address\n"));
3583 else
3585 asection *ts;
3587 /* Can't find the entry symbol, and it's not a number. Use
3588 the first address in the text section. */
3589 ts = bfd_get_section_by_name (output_bfd, entry_section);
3590 if (ts != NULL)
3592 if (warn)
3593 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3594 entry_symbol.name,
3595 bfd_get_section_vma (output_bfd, ts));
3596 if (! bfd_set_start_address (output_bfd,
3597 bfd_get_section_vma (output_bfd,
3598 ts)))
3599 einfo (_("%P%F: can't set start address\n"));
3601 else
3603 if (warn)
3604 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3605 entry_symbol.name);
3610 bfd_hash_table_free (&lang_definedness_table);
3613 /* This is a small function used when we want to ignore errors from
3614 BFD. */
3616 static void
3617 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3619 /* Don't do anything. */
3622 /* Check that the architecture of all the input files is compatible
3623 with the output file. Also call the backend to let it do any
3624 other checking that is needed. */
3626 static void
3627 lang_check (void)
3629 lang_statement_union_type *file;
3630 bfd *input_bfd;
3631 const bfd_arch_info_type *compatible;
3633 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3635 input_bfd = file->input_statement.the_bfd;
3636 compatible = bfd_arch_get_compatible (input_bfd, output_bfd,
3637 command_line.accept_unknown_input_arch);
3639 /* In general it is not possible to perform a relocatable
3640 link between differing object formats when the input
3641 file has relocations, because the relocations in the
3642 input format may not have equivalent representations in
3643 the output format (and besides BFD does not translate
3644 relocs for other link purposes than a final link). */
3645 if ((link_info.relocatable || link_info.emitrelocations)
3646 && (compatible == NULL
3647 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3648 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3650 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3651 bfd_get_target (input_bfd), input_bfd,
3652 bfd_get_target (output_bfd), output_bfd);
3653 /* einfo with %F exits. */
3656 if (compatible == NULL)
3658 if (command_line.warn_mismatch)
3659 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3660 bfd_printable_name (input_bfd), input_bfd,
3661 bfd_printable_name (output_bfd));
3663 else if (bfd_count_sections (input_bfd))
3665 /* If the input bfd has no contents, it shouldn't set the
3666 private data of the output bfd. */
3668 bfd_error_handler_type pfn = NULL;
3670 /* If we aren't supposed to warn about mismatched input
3671 files, temporarily set the BFD error handler to a
3672 function which will do nothing. We still want to call
3673 bfd_merge_private_bfd_data, since it may set up
3674 information which is needed in the output file. */
3675 if (! command_line.warn_mismatch)
3676 pfn = bfd_set_error_handler (ignore_bfd_errors);
3677 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3679 if (command_line.warn_mismatch)
3680 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3681 input_bfd);
3683 if (! command_line.warn_mismatch)
3684 bfd_set_error_handler (pfn);
3689 /* Look through all the global common symbols and attach them to the
3690 correct section. The -sort-common command line switch may be used
3691 to roughly sort the entries by size. */
3693 static void
3694 lang_common (void)
3696 if (command_line.inhibit_common_definition)
3697 return;
3698 if (link_info.relocatable
3699 && ! command_line.force_common_definition)
3700 return;
3702 if (! config.sort_common)
3703 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3704 else
3706 int power;
3708 for (power = 4; power >= 0; power--)
3709 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3713 /* Place one common symbol in the correct section. */
3715 static bfd_boolean
3716 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3718 unsigned int power_of_two;
3719 bfd_vma size;
3720 asection *section;
3722 if (h->type != bfd_link_hash_common)
3723 return TRUE;
3725 size = h->u.c.size;
3726 power_of_two = h->u.c.p->alignment_power;
3728 if (config.sort_common
3729 && power_of_two < (unsigned int) *(int *) info)
3730 return TRUE;
3732 section = h->u.c.p->section;
3734 /* Increase the size of the section to align the common sym. */
3735 section->_cooked_size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3736 section->_cooked_size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3738 /* Adjust the alignment if necessary. */
3739 if (power_of_two > section->alignment_power)
3740 section->alignment_power = power_of_two;
3742 /* Change the symbol from common to defined. */
3743 h->type = bfd_link_hash_defined;
3744 h->u.def.section = section;
3745 h->u.def.value = section->_cooked_size;
3747 /* Increase the size of the section. */
3748 section->_cooked_size += size;
3750 /* Make sure the section is allocated in memory, and make sure that
3751 it is no longer a common section. */
3752 section->flags |= SEC_ALLOC;
3753 section->flags &= ~SEC_IS_COMMON;
3755 if (config.map_file != NULL)
3757 static bfd_boolean header_printed;
3758 int len;
3759 char *name;
3760 char buf[50];
3762 if (! header_printed)
3764 minfo (_("\nAllocating common symbols\n"));
3765 minfo (_("Common symbol size file\n\n"));
3766 header_printed = TRUE;
3769 name = demangle (h->root.string);
3770 minfo ("%s", name);
3771 len = strlen (name);
3772 free (name);
3774 if (len >= 19)
3776 print_nl ();
3777 len = 0;
3779 while (len < 20)
3781 print_space ();
3782 ++len;
3785 minfo ("0x");
3786 if (size <= 0xffffffff)
3787 sprintf (buf, "%lx", (unsigned long) size);
3788 else
3789 sprintf_vma (buf, size);
3790 minfo ("%s", buf);
3791 len = strlen (buf);
3793 while (len < 16)
3795 print_space ();
3796 ++len;
3799 minfo ("%B\n", section->owner);
3802 return TRUE;
3805 /* Run through the input files and ensure that every input section has
3806 somewhere to go. If one is found without a destination then create
3807 an input request and place it into the statement tree. */
3809 static void
3810 lang_place_orphans (void)
3812 LANG_FOR_EACH_INPUT_STATEMENT (file)
3814 asection *s;
3816 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3818 if (s->output_section == NULL)
3820 /* This section of the file is not attached, root
3821 around for a sensible place for it to go. */
3823 if (file->just_syms_flag)
3825 abort ();
3827 else if (strcmp (s->name, "COMMON") == 0)
3829 /* This is a lonely common section which must have
3830 come from an archive. We attach to the section
3831 with the wildcard. */
3832 if (! link_info.relocatable
3833 || command_line.force_common_definition)
3835 if (default_common_section == NULL)
3837 #if 0
3838 /* This message happens when using the
3839 svr3.ifile linker script, so I have
3840 disabled it. */
3841 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3842 #endif
3843 default_common_section =
3844 lang_output_section_statement_lookup (".bss");
3847 lang_add_section (&default_common_section->children, s,
3848 default_common_section, file);
3851 else if (ldemul_place_orphan (file, s))
3853 else
3855 lang_output_section_statement_type *os;
3857 os = lang_output_section_statement_lookup (s->name);
3858 lang_add_section (&os->children, s, os, file);
3865 void
3866 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
3868 flagword *ptr_flags;
3870 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3871 while (*flags)
3873 switch (*flags)
3875 case 'A': case 'a':
3876 *ptr_flags |= SEC_ALLOC;
3877 break;
3879 case 'R': case 'r':
3880 *ptr_flags |= SEC_READONLY;
3881 break;
3883 case 'W': case 'w':
3884 *ptr_flags |= SEC_DATA;
3885 break;
3887 case 'X': case 'x':
3888 *ptr_flags |= SEC_CODE;
3889 break;
3891 case 'L': case 'l':
3892 case 'I': case 'i':
3893 *ptr_flags |= SEC_LOAD;
3894 break;
3896 default:
3897 einfo (_("%P%F: invalid syntax in flags\n"));
3898 break;
3900 flags++;
3904 /* Call a function on each input file. This function will be called
3905 on an archive, but not on the elements. */
3907 void
3908 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
3910 lang_input_statement_type *f;
3912 for (f = (lang_input_statement_type *) input_file_chain.head;
3913 f != NULL;
3914 f = (lang_input_statement_type *) f->next_real_file)
3915 func (f);
3918 /* Call a function on each file. The function will be called on all
3919 the elements of an archive which are included in the link, but will
3920 not be called on the archive file itself. */
3922 void
3923 lang_for_each_file (void (*func) (lang_input_statement_type *))
3925 LANG_FOR_EACH_INPUT_STATEMENT (f)
3927 func (f);
3931 #if 0
3933 /* Not used. */
3935 void
3936 lang_for_each_input_section (void (*func) (bfd *ab, asection *as))
3938 LANG_FOR_EACH_INPUT_STATEMENT (f)
3940 asection *s;
3942 for (s = f->the_bfd->sections; s != NULL; s = s->next)
3943 func (f->the_bfd, s);
3947 #endif
3949 void
3950 ldlang_add_file (lang_input_statement_type *entry)
3952 bfd **pp;
3954 lang_statement_append (&file_chain,
3955 (lang_statement_union_type *) entry,
3956 &entry->next);
3958 /* The BFD linker needs to have a list of all input BFDs involved in
3959 a link. */
3960 ASSERT (entry->the_bfd->link_next == NULL);
3961 ASSERT (entry->the_bfd != output_bfd);
3962 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
3964 *pp = entry->the_bfd;
3965 entry->the_bfd->usrdata = entry;
3966 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3968 /* Look through the sections and check for any which should not be
3969 included in the link. We need to do this now, so that we can
3970 notice when the backend linker tries to report multiple
3971 definition errors for symbols which are in sections we aren't
3972 going to link. FIXME: It might be better to entirely ignore
3973 symbols which are defined in sections which are going to be
3974 discarded. This would require modifying the backend linker for
3975 each backend which might set the SEC_LINK_ONCE flag. If we do
3976 this, we should probably handle SEC_EXCLUDE in the same way. */
3978 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
3981 void
3982 lang_add_output (const char *name, int from_script)
3984 /* Make -o on command line override OUTPUT in script. */
3985 if (!had_output_filename || !from_script)
3987 output_filename = name;
3988 had_output_filename = TRUE;
3992 static lang_output_section_statement_type *current_section;
3994 static int
3995 topower (int x)
3997 unsigned int i = 1;
3998 int l;
4000 if (x < 0)
4001 return -1;
4003 for (l = 0; l < 32; l++)
4005 if (i >= (unsigned int) x)
4006 return l;
4007 i <<= 1;
4010 return 0;
4013 lang_output_section_statement_type *
4014 lang_enter_output_section_statement (const char *output_section_statement_name,
4015 etree_type *address_exp,
4016 enum section_type sectype,
4017 etree_type *align,
4018 etree_type *subalign,
4019 etree_type *ebase)
4021 lang_output_section_statement_type *os;
4023 current_section =
4024 os =
4025 lang_output_section_statement_lookup (output_section_statement_name);
4027 /* Add this statement to tree. */
4028 #if 0
4029 add_statement (lang_output_section_statement_enum,
4030 output_section_statement);
4031 #endif
4032 /* Make next things chain into subchain of this. */
4034 if (os->addr_tree == NULL)
4036 os->addr_tree = address_exp;
4038 os->sectype = sectype;
4039 if (sectype != noload_section)
4040 os->flags = SEC_NO_FLAGS;
4041 else
4042 os->flags = SEC_NEVER_LOAD;
4043 os->block_value = 1;
4044 stat_ptr = &os->children;
4046 os->subsection_alignment =
4047 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4048 os->section_alignment =
4049 topower (exp_get_value_int (align, -1, "section alignment", 0));
4051 os->load_base = ebase;
4052 return os;
4055 void
4056 lang_final (void)
4058 lang_output_statement_type *new =
4059 new_stat (lang_output_statement, stat_ptr);
4061 new->name = output_filename;
4064 /* Reset the current counters in the regions. */
4066 void
4067 lang_reset_memory_regions (void)
4069 lang_memory_region_type *p = lang_memory_region_list;
4070 asection *o;
4072 for (p = lang_memory_region_list; p != NULL; p = p->next)
4074 p->old_length = (bfd_size_type) (p->current - p->origin);
4075 p->current = p->origin;
4078 for (o = output_bfd->sections; o != NULL; o = o->next)
4079 o->_raw_size = 0;
4082 /* If the wild pattern was marked KEEP, the member sections
4083 should be as well. */
4085 static void
4086 gc_section_callback (lang_wild_statement_type *ptr,
4087 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4088 asection *section,
4089 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4090 void *data ATTRIBUTE_UNUSED)
4092 if (ptr->keep_sections)
4093 section->flags |= SEC_KEEP;
4096 /* Handle a wild statement, marking it against GC. */
4098 static void
4099 lang_gc_wild (lang_wild_statement_type *s)
4101 walk_wild (s, gc_section_callback, NULL);
4104 /* Iterate over sections marking them against GC. */
4106 static void
4107 lang_gc_sections_1 (lang_statement_union_type *s)
4109 for (; s != NULL; s = s->header.next)
4111 switch (s->header.type)
4113 case lang_wild_statement_enum:
4114 lang_gc_wild (&s->wild_statement);
4115 break;
4116 case lang_constructors_statement_enum:
4117 lang_gc_sections_1 (constructor_list.head);
4118 break;
4119 case lang_output_section_statement_enum:
4120 lang_gc_sections_1 (s->output_section_statement.children.head);
4121 break;
4122 case lang_group_statement_enum:
4123 lang_gc_sections_1 (s->group_statement.children.head);
4124 break;
4125 default:
4126 break;
4131 static void
4132 lang_gc_sections (void)
4134 struct bfd_link_hash_entry *h;
4135 ldlang_undef_chain_list_type *ulist;
4137 /* Keep all sections so marked in the link script. */
4139 lang_gc_sections_1 (statement_list.head);
4141 /* Keep all sections containing symbols undefined on the command-line,
4142 and the section containing the entry symbol. */
4144 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4146 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4147 FALSE, FALSE, FALSE);
4149 if (h != NULL
4150 && (h->type == bfd_link_hash_defined
4151 || h->type == bfd_link_hash_defweak)
4152 && ! bfd_is_abs_section (h->u.def.section))
4154 h->u.def.section->flags |= SEC_KEEP;
4158 bfd_gc_sections (output_bfd, &link_info);
4161 void
4162 lang_process (void)
4164 lang_reasonable_defaults ();
4165 current_target = default_target;
4167 /* Open the output file. */
4168 lang_for_each_statement (ldlang_open_output);
4169 init_opb ();
4171 ldemul_create_output_section_statements ();
4173 /* Add to the hash table all undefineds on the command line. */
4174 lang_place_undefineds ();
4176 already_linked_table_init ();
4178 /* Create a bfd for each input file. */
4179 current_target = default_target;
4180 open_input_bfds (statement_list.head, FALSE);
4182 link_info.gc_sym_list = &entry_symbol;
4183 if (entry_symbol.name == NULL)
4184 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4186 ldemul_after_open ();
4188 already_linked_table_free ();
4190 /* Make sure that we're not mixing architectures. We call this
4191 after all the input files have been opened, but before we do any
4192 other processing, so that any operations merge_private_bfd_data
4193 does on the output file will be known during the rest of the
4194 link. */
4195 lang_check ();
4197 /* Handle .exports instead of a version script if we're told to do so. */
4198 if (command_line.version_exports_section)
4199 lang_do_version_exports_section ();
4201 /* Build all sets based on the information gathered from the input
4202 files. */
4203 ldctor_build_sets ();
4205 /* Remove unreferenced sections if asked to. */
4206 if (command_line.gc_sections)
4207 lang_gc_sections ();
4209 /* If there were any SEC_MERGE sections, finish their merging, so that
4210 section sizes can be computed. This has to be done after GC of sections,
4211 so that GCed sections are not merged, but before assigning output
4212 sections, since removing whole input sections is hard then. */
4213 bfd_merge_sections (output_bfd, &link_info);
4215 /* Size up the common data. */
4216 lang_common ();
4218 /* Run through the contours of the script and attach input sections
4219 to the correct output sections. */
4220 map_input_to_output_sections (statement_list.head, NULL, NULL);
4222 /* Find any sections not attached explicitly and handle them. */
4223 lang_place_orphans ();
4225 if (! link_info.relocatable)
4227 /* Look for a text section and set the readonly attribute in it. */
4228 asection *found = bfd_get_section_by_name (output_bfd, ".text");
4230 if (found != NULL)
4232 if (config.text_read_only)
4233 found->flags |= SEC_READONLY;
4234 else
4235 found->flags &= ~SEC_READONLY;
4239 /* Do anything special before sizing sections. This is where ELF
4240 and other back-ends size dynamic sections. */
4241 ldemul_before_allocation ();
4243 if (!link_info.relocatable)
4244 strip_excluded_output_sections ();
4246 /* We must record the program headers before we try to fix the
4247 section positions, since they will affect SIZEOF_HEADERS. */
4248 lang_record_phdrs ();
4250 /* Size up the sections. */
4251 lang_size_sections (statement_list.head, abs_output_section,
4252 &statement_list.head, 0, 0, NULL,
4253 command_line.relax ? FALSE : TRUE);
4255 /* Now run around and relax if we can. */
4256 if (command_line.relax)
4258 /* Keep relaxing until bfd_relax_section gives up. */
4259 bfd_boolean relax_again;
4263 relax_again = FALSE;
4265 /* Note: pe-dll.c does something like this also. If you find
4266 you need to change this code, you probably need to change
4267 pe-dll.c also. DJ */
4269 /* Do all the assignments with our current guesses as to
4270 section sizes. */
4271 lang_do_assignments (statement_list.head, abs_output_section,
4272 NULL, 0);
4274 /* We must do this after lang_do_assignments, because it uses
4275 _raw_size. */
4276 lang_reset_memory_regions ();
4278 /* Perform another relax pass - this time we know where the
4279 globals are, so can make a better guess. */
4280 lang_size_sections (statement_list.head, abs_output_section,
4281 &statement_list.head, 0, 0, &relax_again, FALSE);
4283 /* If the normal relax is done and the relax finalize pass
4284 is not performed yet, we perform another relax pass. */
4285 if (!relax_again && link_info.need_relax_finalize)
4287 link_info.need_relax_finalize = FALSE;
4288 relax_again = TRUE;
4291 while (relax_again);
4293 /* Final extra sizing to report errors. */
4294 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4295 lang_reset_memory_regions ();
4296 lang_size_sections (statement_list.head, abs_output_section,
4297 &statement_list.head, 0, 0, NULL, TRUE);
4300 /* See if anything special should be done now we know how big
4301 everything is. */
4302 ldemul_after_allocation ();
4304 /* Fix any .startof. or .sizeof. symbols. */
4305 lang_set_startof ();
4307 /* Do all the assignments, now that we know the final resting places
4308 of all the symbols. */
4310 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4312 /* Make sure that the section addresses make sense. */
4313 if (! link_info.relocatable
4314 && command_line.check_section_addresses)
4315 lang_check_section_addresses ();
4317 /* Final stuffs. */
4319 ldemul_finish ();
4320 lang_finish ();
4323 /* EXPORTED TO YACC */
4325 void
4326 lang_add_wild (struct wildcard_spec *filespec,
4327 struct wildcard_list *section_list,
4328 bfd_boolean keep_sections)
4330 struct wildcard_list *curr, *next;
4331 lang_wild_statement_type *new;
4333 /* Reverse the list as the parser puts it back to front. */
4334 for (curr = section_list, section_list = NULL;
4335 curr != NULL;
4336 section_list = curr, curr = next)
4338 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4339 placed_commons = TRUE;
4341 next = curr->next;
4342 curr->next = section_list;
4345 if (filespec != NULL && filespec->name != NULL)
4347 if (strcmp (filespec->name, "*") == 0)
4348 filespec->name = NULL;
4349 else if (! wildcardp (filespec->name))
4350 lang_has_input_file = TRUE;
4353 new = new_stat (lang_wild_statement, stat_ptr);
4354 new->filename = NULL;
4355 new->filenames_sorted = FALSE;
4356 if (filespec != NULL)
4358 new->filename = filespec->name;
4359 new->filenames_sorted = filespec->sorted;
4361 new->section_list = section_list;
4362 new->keep_sections = keep_sections;
4363 lang_list_init (&new->children);
4366 void
4367 lang_section_start (const char *name, etree_type *address)
4369 lang_address_statement_type *ad;
4371 ad = new_stat (lang_address_statement, stat_ptr);
4372 ad->section_name = name;
4373 ad->address = address;
4376 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4377 because of a -e argument on the command line, or zero if this is
4378 called by ENTRY in a linker script. Command line arguments take
4379 precedence. */
4381 void
4382 lang_add_entry (const char *name, bfd_boolean cmdline)
4384 if (entry_symbol.name == NULL
4385 || cmdline
4386 || ! entry_from_cmdline)
4388 entry_symbol.name = name;
4389 entry_from_cmdline = cmdline;
4393 void
4394 lang_add_target (const char *name)
4396 lang_target_statement_type *new = new_stat (lang_target_statement,
4397 stat_ptr);
4399 new->target = name;
4403 void
4404 lang_add_map (const char *name)
4406 while (*name)
4408 switch (*name)
4410 case 'F':
4411 map_option_f = TRUE;
4412 break;
4414 name++;
4418 void
4419 lang_add_fill (fill_type *fill)
4421 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4422 stat_ptr);
4424 new->fill = fill;
4427 void
4428 lang_add_data (int type, union etree_union *exp)
4431 lang_data_statement_type *new = new_stat (lang_data_statement,
4432 stat_ptr);
4434 new->exp = exp;
4435 new->type = type;
4439 /* Create a new reloc statement. RELOC is the BFD relocation type to
4440 generate. HOWTO is the corresponding howto structure (we could
4441 look this up, but the caller has already done so). SECTION is the
4442 section to generate a reloc against, or NAME is the name of the
4443 symbol to generate a reloc against. Exactly one of SECTION and
4444 NAME must be NULL. ADDEND is an expression for the addend. */
4446 void
4447 lang_add_reloc (bfd_reloc_code_real_type reloc,
4448 reloc_howto_type *howto,
4449 asection *section,
4450 const char *name,
4451 union etree_union *addend)
4453 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4455 p->reloc = reloc;
4456 p->howto = howto;
4457 p->section = section;
4458 p->name = name;
4459 p->addend_exp = addend;
4461 p->addend_value = 0;
4462 p->output_section = NULL;
4463 p->output_vma = 0;
4466 lang_assignment_statement_type *
4467 lang_add_assignment (etree_type *exp)
4469 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4470 stat_ptr);
4472 new->exp = exp;
4473 return new;
4476 void
4477 lang_add_attribute (enum statement_enum attribute)
4479 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4482 void
4483 lang_startup (const char *name)
4485 if (startup_file != NULL)
4487 einfo (_("%P%Fmultiple STARTUP files\n"));
4489 first_file->filename = name;
4490 first_file->local_sym_name = name;
4491 first_file->real = TRUE;
4493 startup_file = name;
4496 void
4497 lang_float (bfd_boolean maybe)
4499 lang_float_flag = maybe;
4503 /* Work out the load- and run-time regions from a script statement, and
4504 store them in *LMA_REGION and *REGION respectively.
4506 MEMSPEC is the name of the run-time region, or the value of
4507 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4508 LMA_MEMSPEC is the name of the load-time region, or null if the
4509 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4510 had an explicit load address.
4512 It is an error to specify both a load region and a load address. */
4514 static void
4515 lang_get_regions (lang_memory_region_type **region,
4516 lang_memory_region_type **lma_region,
4517 const char *memspec,
4518 const char *lma_memspec,
4519 bfd_boolean have_lma,
4520 bfd_boolean have_vma)
4522 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4524 /* If no runtime region or VMA has been specified, but the load region has
4525 been specified, then use the load region for the runtime region as well. */
4526 if (lma_memspec != NULL
4527 && ! have_vma
4528 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4529 *region = *lma_region;
4530 else
4531 *region = lang_memory_region_lookup (memspec, FALSE);
4533 if (have_lma && lma_memspec != 0)
4534 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4537 void
4538 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4539 lang_output_section_phdr_list *phdrs,
4540 const char *lma_memspec)
4542 lang_get_regions (&current_section->region,
4543 &current_section->lma_region,
4544 memspec, lma_memspec,
4545 current_section->load_base != NULL,
4546 current_section->addr_tree != NULL);
4547 current_section->fill = fill;
4548 current_section->phdrs = phdrs;
4549 stat_ptr = &statement_list;
4552 /* Create an absolute symbol with the given name with the value of the
4553 address of first byte of the section named.
4555 If the symbol already exists, then do nothing. */
4557 void
4558 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4560 struct bfd_link_hash_entry *h;
4562 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4563 if (h == NULL)
4564 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4566 if (h->type == bfd_link_hash_new
4567 || h->type == bfd_link_hash_undefined)
4569 asection *sec;
4571 h->type = bfd_link_hash_defined;
4573 sec = bfd_get_section_by_name (output_bfd, secname);
4574 if (sec == NULL)
4575 h->u.def.value = 0;
4576 else
4577 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4579 h->u.def.section = bfd_abs_section_ptr;
4583 /* Create an absolute symbol with the given name with the value of the
4584 address of the first byte after the end of the section named.
4586 If the symbol already exists, then do nothing. */
4588 void
4589 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4591 struct bfd_link_hash_entry *h;
4593 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4594 if (h == NULL)
4595 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4597 if (h->type == bfd_link_hash_new
4598 || h->type == bfd_link_hash_undefined)
4600 asection *sec;
4602 h->type = bfd_link_hash_defined;
4604 sec = bfd_get_section_by_name (output_bfd, secname);
4605 if (sec == NULL)
4606 h->u.def.value = 0;
4607 else
4608 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4609 + TO_ADDR (bfd_section_size (output_bfd, sec)));
4611 h->u.def.section = bfd_abs_section_ptr;
4615 void
4616 lang_statement_append (lang_statement_list_type *list,
4617 lang_statement_union_type *element,
4618 lang_statement_union_type **field)
4620 *(list->tail) = element;
4621 list->tail = field;
4624 /* Set the output format type. -oformat overrides scripts. */
4626 void
4627 lang_add_output_format (const char *format,
4628 const char *big,
4629 const char *little,
4630 int from_script)
4632 if (output_target == NULL || !from_script)
4634 if (command_line.endian == ENDIAN_BIG
4635 && big != NULL)
4636 format = big;
4637 else if (command_line.endian == ENDIAN_LITTLE
4638 && little != NULL)
4639 format = little;
4641 output_target = format;
4645 /* Enter a group. This creates a new lang_group_statement, and sets
4646 stat_ptr to build new statements within the group. */
4648 void
4649 lang_enter_group (void)
4651 lang_group_statement_type *g;
4653 g = new_stat (lang_group_statement, stat_ptr);
4654 lang_list_init (&g->children);
4655 stat_ptr = &g->children;
4658 /* Leave a group. This just resets stat_ptr to start writing to the
4659 regular list of statements again. Note that this will not work if
4660 groups can occur inside anything else which can adjust stat_ptr,
4661 but currently they can't. */
4663 void
4664 lang_leave_group (void)
4666 stat_ptr = &statement_list;
4669 /* Add a new program header. This is called for each entry in a PHDRS
4670 command in a linker script. */
4672 void
4673 lang_new_phdr (const char *name,
4674 etree_type *type,
4675 bfd_boolean filehdr,
4676 bfd_boolean phdrs,
4677 etree_type *at,
4678 etree_type *flags)
4680 struct lang_phdr *n, **pp;
4682 n = stat_alloc (sizeof (struct lang_phdr));
4683 n->next = NULL;
4684 n->name = name;
4685 n->type = exp_get_value_int (type, 0, "program header type",
4686 lang_final_phase_enum);
4687 n->filehdr = filehdr;
4688 n->phdrs = phdrs;
4689 n->at = at;
4690 n->flags = flags;
4692 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4694 *pp = n;
4697 /* Record the program header information in the output BFD. FIXME: We
4698 should not be calling an ELF specific function here. */
4700 static void
4701 lang_record_phdrs (void)
4703 unsigned int alc;
4704 asection **secs;
4705 lang_output_section_phdr_list *last;
4706 struct lang_phdr *l;
4707 lang_statement_union_type *u;
4709 alc = 10;
4710 secs = xmalloc (alc * sizeof (asection *));
4711 last = NULL;
4712 for (l = lang_phdr_list; l != NULL; l = l->next)
4714 unsigned int c;
4715 flagword flags;
4716 bfd_vma at;
4718 c = 0;
4719 for (u = lang_output_section_statement.head;
4720 u != NULL;
4721 u = u->output_section_statement.next)
4723 lang_output_section_statement_type *os;
4724 lang_output_section_phdr_list *pl;
4726 os = &u->output_section_statement;
4728 pl = os->phdrs;
4729 if (pl != NULL)
4730 last = pl;
4731 else
4733 if (os->sectype == noload_section
4734 || os->bfd_section == NULL
4735 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4736 continue;
4737 pl = last;
4740 if (os->bfd_section == NULL)
4741 continue;
4743 for (; pl != NULL; pl = pl->next)
4745 if (strcmp (pl->name, l->name) == 0)
4747 if (c >= alc)
4749 alc *= 2;
4750 secs = xrealloc (secs, alc * sizeof (asection *));
4752 secs[c] = os->bfd_section;
4753 ++c;
4754 pl->used = TRUE;
4759 if (l->flags == NULL)
4760 flags = 0;
4761 else
4762 flags = exp_get_vma (l->flags, 0, "phdr flags",
4763 lang_final_phase_enum);
4765 if (l->at == NULL)
4766 at = 0;
4767 else
4768 at = exp_get_vma (l->at, 0, "phdr load address",
4769 lang_final_phase_enum);
4771 if (! bfd_record_phdr (output_bfd, l->type,
4772 l->flags != NULL, flags, l->at != NULL,
4773 at, l->filehdr, l->phdrs, c, secs))
4774 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4777 free (secs);
4779 /* Make sure all the phdr assignments succeeded. */
4780 for (u = lang_output_section_statement.head;
4781 u != NULL;
4782 u = u->output_section_statement.next)
4784 lang_output_section_phdr_list *pl;
4786 if (u->output_section_statement.bfd_section == NULL)
4787 continue;
4789 for (pl = u->output_section_statement.phdrs;
4790 pl != NULL;
4791 pl = pl->next)
4792 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4793 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4794 u->output_section_statement.name, pl->name);
4798 /* Record a list of sections which may not be cross referenced. */
4800 void
4801 lang_add_nocrossref (lang_nocrossref_type *l)
4803 struct lang_nocrossrefs *n;
4805 n = xmalloc (sizeof *n);
4806 n->next = nocrossref_list;
4807 n->list = l;
4808 nocrossref_list = n;
4810 /* Set notice_all so that we get informed about all symbols. */
4811 link_info.notice_all = TRUE;
4814 /* Overlay handling. We handle overlays with some static variables. */
4816 /* The overlay virtual address. */
4817 static etree_type *overlay_vma;
4818 /* And subsection alignment. */
4819 static etree_type *overlay_subalign;
4821 /* An expression for the maximum section size seen so far. */
4822 static etree_type *overlay_max;
4824 /* A list of all the sections in this overlay. */
4826 struct overlay_list {
4827 struct overlay_list *next;
4828 lang_output_section_statement_type *os;
4831 static struct overlay_list *overlay_list;
4833 /* Start handling an overlay. */
4835 void
4836 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
4838 /* The grammar should prevent nested overlays from occurring. */
4839 ASSERT (overlay_vma == NULL
4840 && overlay_subalign == NULL
4841 && overlay_max == NULL);
4843 overlay_vma = vma_expr;
4844 overlay_subalign = subalign;
4847 /* Start a section in an overlay. We handle this by calling
4848 lang_enter_output_section_statement with the correct VMA.
4849 lang_leave_overlay sets up the LMA and memory regions. */
4851 void
4852 lang_enter_overlay_section (const char *name)
4854 struct overlay_list *n;
4855 etree_type *size;
4857 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4858 0, overlay_subalign, 0);
4860 /* If this is the first section, then base the VMA of future
4861 sections on this one. This will work correctly even if `.' is
4862 used in the addresses. */
4863 if (overlay_list == NULL)
4864 overlay_vma = exp_nameop (ADDR, name);
4866 /* Remember the section. */
4867 n = xmalloc (sizeof *n);
4868 n->os = current_section;
4869 n->next = overlay_list;
4870 overlay_list = n;
4872 size = exp_nameop (SIZEOF, name);
4874 /* Arrange to work out the maximum section end address. */
4875 if (overlay_max == NULL)
4876 overlay_max = size;
4877 else
4878 overlay_max = exp_binop (MAX_K, overlay_max, size);
4881 /* Finish a section in an overlay. There isn't any special to do
4882 here. */
4884 void
4885 lang_leave_overlay_section (fill_type *fill,
4886 lang_output_section_phdr_list *phdrs)
4888 const char *name;
4889 char *clean, *s2;
4890 const char *s1;
4891 char *buf;
4893 name = current_section->name;
4895 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4896 region and that no load-time region has been specified. It doesn't
4897 really matter what we say here, since lang_leave_overlay will
4898 override it. */
4899 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
4901 /* Define the magic symbols. */
4903 clean = xmalloc (strlen (name) + 1);
4904 s2 = clean;
4905 for (s1 = name; *s1 != '\0'; s1++)
4906 if (ISALNUM (*s1) || *s1 == '_')
4907 *s2++ = *s1;
4908 *s2 = '\0';
4910 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4911 sprintf (buf, "__load_start_%s", clean);
4912 lang_add_assignment (exp_assop ('=', buf,
4913 exp_nameop (LOADADDR, name)));
4915 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4916 sprintf (buf, "__load_stop_%s", clean);
4917 lang_add_assignment (exp_assop ('=', buf,
4918 exp_binop ('+',
4919 exp_nameop (LOADADDR, name),
4920 exp_nameop (SIZEOF, name))));
4922 free (clean);
4925 /* Finish an overlay. If there are any overlay wide settings, this
4926 looks through all the sections in the overlay and sets them. */
4928 void
4929 lang_leave_overlay (etree_type *lma_expr,
4930 int nocrossrefs,
4931 fill_type *fill,
4932 const char *memspec,
4933 lang_output_section_phdr_list *phdrs,
4934 const char *lma_memspec)
4936 lang_memory_region_type *region;
4937 lang_memory_region_type *lma_region;
4938 struct overlay_list *l;
4939 lang_nocrossref_type *nocrossref;
4941 lang_get_regions (&region, &lma_region,
4942 memspec, lma_memspec,
4943 lma_expr != NULL, FALSE);
4945 nocrossref = NULL;
4947 /* After setting the size of the last section, set '.' to end of the
4948 overlay region. */
4949 if (overlay_list != NULL)
4950 overlay_list->os->update_dot_tree
4951 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
4953 l = overlay_list;
4954 while (l != NULL)
4956 struct overlay_list *next;
4958 if (fill != NULL && l->os->fill == NULL)
4959 l->os->fill = fill;
4961 l->os->region = region;
4962 l->os->lma_region = lma_region;
4964 /* The first section has the load address specified in the
4965 OVERLAY statement. The rest are worked out from that.
4966 The base address is not needed (and should be null) if
4967 an LMA region was specified. */
4968 if (l->next == 0)
4969 l->os->load_base = lma_expr;
4970 else if (lma_region == 0)
4971 l->os->load_base = exp_binop ('+',
4972 exp_nameop (LOADADDR, l->next->os->name),
4973 exp_nameop (SIZEOF, l->next->os->name));
4975 if (phdrs != NULL && l->os->phdrs == NULL)
4976 l->os->phdrs = phdrs;
4978 if (nocrossrefs)
4980 lang_nocrossref_type *nc;
4982 nc = xmalloc (sizeof *nc);
4983 nc->name = l->os->name;
4984 nc->next = nocrossref;
4985 nocrossref = nc;
4988 next = l->next;
4989 free (l);
4990 l = next;
4993 if (nocrossref != NULL)
4994 lang_add_nocrossref (nocrossref);
4996 overlay_vma = NULL;
4997 overlay_list = NULL;
4998 overlay_max = NULL;
5001 /* Version handling. This is only useful for ELF. */
5003 /* This global variable holds the version tree that we build. */
5005 struct bfd_elf_version_tree *lang_elf_version_info;
5007 /* If PREV is NULL, return first version pattern matching particular symbol.
5008 If PREV is non-NULL, return first version pattern matching particular
5009 symbol after PREV (previously returned by lang_vers_match). */
5011 static struct bfd_elf_version_expr *
5012 lang_vers_match (struct bfd_elf_version_expr_head *head,
5013 struct bfd_elf_version_expr *prev,
5014 const char *sym)
5016 const char *cxx_sym = sym;
5017 const char *java_sym = sym;
5018 struct bfd_elf_version_expr *expr = NULL;
5020 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5022 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5023 if (!cxx_sym)
5024 cxx_sym = sym;
5026 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5028 java_sym = cplus_demangle (sym, DMGL_JAVA);
5029 if (!java_sym)
5030 java_sym = sym;
5033 if (head->htab && (prev == NULL || prev->symbol))
5035 struct bfd_elf_version_expr e;
5037 switch (prev ? prev->mask : 0)
5039 case 0:
5040 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5042 e.symbol = sym;
5043 expr = htab_find (head->htab, &e);
5044 while (expr && strcmp (expr->symbol, sym) == 0)
5045 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5046 goto out_ret;
5047 else
5048 expr = expr->next;
5050 /* Fallthrough */
5051 case BFD_ELF_VERSION_C_TYPE:
5052 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5054 e.symbol = cxx_sym;
5055 expr = htab_find (head->htab, &e);
5056 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5057 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5058 goto out_ret;
5059 else
5060 expr = expr->next;
5062 /* Fallthrough */
5063 case BFD_ELF_VERSION_CXX_TYPE:
5064 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5066 e.symbol = java_sym;
5067 expr = htab_find (head->htab, &e);
5068 while (expr && strcmp (expr->symbol, java_sym) == 0)
5069 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5070 goto out_ret;
5071 else
5072 expr = expr->next;
5074 /* Fallthrough */
5075 default:
5076 break;
5080 /* Finally, try the wildcards. */
5081 if (prev == NULL || prev->symbol)
5082 expr = head->remaining;
5083 else
5084 expr = prev->next;
5085 while (expr)
5087 const char *s;
5089 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5090 break;
5092 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5093 s = java_sym;
5094 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5095 s = cxx_sym;
5096 else
5097 s = sym;
5098 if (fnmatch (expr->pattern, s, 0) == 0)
5099 break;
5100 expr = expr->next;
5103 out_ret:
5104 if (cxx_sym != sym)
5105 free ((char *) cxx_sym);
5106 if (java_sym != sym)
5107 free ((char *) java_sym);
5108 return expr;
5111 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5112 return a string pointing to the symbol name. */
5114 static const char *
5115 realsymbol (const char *pattern)
5117 const char *p;
5118 bfd_boolean changed = FALSE, backslash = FALSE;
5119 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5121 for (p = pattern, s = symbol; *p != '\0'; ++p)
5123 /* It is a glob pattern only if there is no preceding
5124 backslash. */
5125 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5127 free (symbol);
5128 return NULL;
5131 if (backslash)
5133 /* Remove the preceding backslash. */
5134 *(s - 1) = *p;
5135 changed = TRUE;
5137 else
5138 *s++ = *p;
5140 backslash = *p == '\\';
5143 if (changed)
5145 *s = '\0';
5146 return symbol;
5148 else
5150 free (symbol);
5151 return pattern;
5155 /* This is called for each variable name or match expression. */
5157 struct bfd_elf_version_expr *
5158 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5159 const char *new,
5160 const char *lang)
5162 struct bfd_elf_version_expr *ret;
5164 ret = xmalloc (sizeof *ret);
5165 ret->next = orig;
5166 ret->pattern = new;
5167 ret->symver = 0;
5168 ret->script = 0;
5169 ret->symbol = realsymbol (new);
5171 if (lang == NULL || strcasecmp (lang, "C") == 0)
5172 ret->mask = BFD_ELF_VERSION_C_TYPE;
5173 else if (strcasecmp (lang, "C++") == 0)
5174 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5175 else if (strcasecmp (lang, "Java") == 0)
5176 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5177 else
5179 einfo (_("%X%P: unknown language `%s' in version information\n"),
5180 lang);
5181 ret->mask = BFD_ELF_VERSION_C_TYPE;
5184 return ldemul_new_vers_pattern (ret);
5187 /* This is called for each set of variable names and match
5188 expressions. */
5190 struct bfd_elf_version_tree *
5191 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5192 struct bfd_elf_version_expr *locals)
5194 struct bfd_elf_version_tree *ret;
5196 ret = xcalloc (1, sizeof *ret);
5197 ret->globals.list = globals;
5198 ret->locals.list = locals;
5199 ret->match = lang_vers_match;
5200 ret->name_indx = (unsigned int) -1;
5201 return ret;
5204 /* This static variable keeps track of version indices. */
5206 static int version_index;
5208 static hashval_t
5209 version_expr_head_hash (const void *p)
5211 const struct bfd_elf_version_expr *e = p;
5213 return htab_hash_string (e->symbol);
5216 static int
5217 version_expr_head_eq (const void *p1, const void *p2)
5219 const struct bfd_elf_version_expr *e1 = p1;
5220 const struct bfd_elf_version_expr *e2 = p2;
5222 return strcmp (e1->symbol, e2->symbol) == 0;
5225 static void
5226 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5228 size_t count = 0;
5229 struct bfd_elf_version_expr *e, *next;
5230 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5232 for (e = head->list; e; e = e->next)
5234 if (e->symbol)
5235 count++;
5236 head->mask |= e->mask;
5239 if (count)
5241 head->htab = htab_create (count * 2, version_expr_head_hash,
5242 version_expr_head_eq, NULL);
5243 list_loc = &head->list;
5244 remaining_loc = &head->remaining;
5245 for (e = head->list; e; e = next)
5247 next = e->next;
5248 if (!e->symbol)
5250 *remaining_loc = e;
5251 remaining_loc = &e->next;
5253 else
5255 void **loc = htab_find_slot (head->htab, e, INSERT);
5257 if (*loc)
5259 struct bfd_elf_version_expr *e1, *last;
5261 e1 = *loc;
5262 last = NULL;
5265 if (e1->mask == e->mask)
5267 last = NULL;
5268 break;
5270 last = e1;
5271 e1 = e1->next;
5273 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5275 if (last == NULL)
5277 /* This is a duplicate. */
5278 /* FIXME: Memory leak. Sometimes pattern is not
5279 xmalloced alone, but in larger chunk of memory. */
5280 /* free (e->symbol); */
5281 free (e);
5283 else
5285 e->next = last->next;
5286 last->next = e;
5289 else
5291 *loc = e;
5292 *list_loc = e;
5293 list_loc = &e->next;
5297 *remaining_loc = NULL;
5298 *list_loc = head->remaining;
5300 else
5301 head->remaining = head->list;
5304 /* This is called when we know the name and dependencies of the
5305 version. */
5307 void
5308 lang_register_vers_node (const char *name,
5309 struct bfd_elf_version_tree *version,
5310 struct bfd_elf_version_deps *deps)
5312 struct bfd_elf_version_tree *t, **pp;
5313 struct bfd_elf_version_expr *e1;
5315 if (name == NULL)
5316 name = "";
5318 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5319 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5321 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5322 free (version);
5323 return;
5326 /* Make sure this node has a unique name. */
5327 for (t = lang_elf_version_info; t != NULL; t = t->next)
5328 if (strcmp (t->name, name) == 0)
5329 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5331 lang_finalize_version_expr_head (&version->globals);
5332 lang_finalize_version_expr_head (&version->locals);
5334 /* Check the global and local match names, and make sure there
5335 aren't any duplicates. */
5337 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5339 for (t = lang_elf_version_info; t != NULL; t = t->next)
5341 struct bfd_elf_version_expr *e2;
5343 if (t->locals.htab && e1->symbol)
5345 e2 = htab_find (t->locals.htab, e1);
5346 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5348 if (e1->mask == e2->mask)
5349 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5350 e1->symbol);
5351 e2 = e2->next;
5354 else if (!e1->symbol)
5355 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5356 if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5357 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5358 e1->pattern);
5362 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5364 for (t = lang_elf_version_info; t != NULL; t = t->next)
5366 struct bfd_elf_version_expr *e2;
5368 if (t->globals.htab && e1->symbol)
5370 e2 = htab_find (t->globals.htab, e1);
5371 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5373 if (e1->mask == e2->mask)
5374 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5375 e1->symbol);
5376 e2 = e2->next;
5379 else if (!e1->symbol)
5380 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5381 if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5382 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5383 e1->pattern);
5387 version->deps = deps;
5388 version->name = name;
5389 if (name[0] != '\0')
5391 ++version_index;
5392 version->vernum = version_index;
5394 else
5395 version->vernum = 0;
5397 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5399 *pp = version;
5402 /* This is called when we see a version dependency. */
5404 struct bfd_elf_version_deps *
5405 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5407 struct bfd_elf_version_deps *ret;
5408 struct bfd_elf_version_tree *t;
5410 ret = xmalloc (sizeof *ret);
5411 ret->next = list;
5413 for (t = lang_elf_version_info; t != NULL; t = t->next)
5415 if (strcmp (t->name, name) == 0)
5417 ret->version_needed = t;
5418 return ret;
5422 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5424 return ret;
5427 static void
5428 lang_do_version_exports_section (void)
5430 struct bfd_elf_version_expr *greg = NULL, *lreg;
5432 LANG_FOR_EACH_INPUT_STATEMENT (is)
5434 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5435 char *contents, *p;
5436 bfd_size_type len;
5438 if (sec == NULL)
5439 continue;
5441 len = bfd_section_size (is->the_bfd, sec);
5442 contents = xmalloc (len);
5443 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5444 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5446 p = contents;
5447 while (p < contents + len)
5449 greg = lang_new_vers_pattern (greg, p, NULL);
5450 p = strchr (p, '\0') + 1;
5453 /* Do not free the contents, as we used them creating the regex. */
5455 /* Do not include this section in the link. */
5456 bfd_set_section_flags (is->the_bfd, sec,
5457 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5460 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5461 lang_register_vers_node (command_line.version_exports_section,
5462 lang_new_vers_node (greg, lreg), NULL);
5465 void
5466 lang_add_unique (const char *name)
5468 struct unique_sections *ent;
5470 for (ent = unique_section_list; ent; ent = ent->next)
5471 if (strcmp (ent->name, name) == 0)
5472 return;
5474 ent = xmalloc (sizeof *ent);
5475 ent->name = xstrdup (name);
5476 ent->next = unique_section_list;
5477 unique_section_list = ent;