Fix initialisation of debug_line_pointer_sizes array so that it is done as needed
[binutils.git] / ld / ldlang.c
blob55714c97667b6ba10c633ec82d51ff2afbdd1c3e
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_unary:
779 exp_init_os (exp->unary.child);
780 break;
782 case etree_name:
783 switch (exp->type.node_code)
785 case ADDR:
786 case LOADADDR:
787 case SIZEOF:
789 lang_output_section_statement_type *os;
791 os = lang_output_section_find (exp->name.name);
792 if (os != NULL && os->bfd_section == NULL)
793 init_os (os);
796 break;
798 default:
799 break;
803 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
804 once into the output. This routine checks each section, and
805 arrange to discard it if a section of the same name has already
806 been linked. If the section has COMDAT information, then it uses
807 that to decide whether the section should be included. This code
808 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
809 that is, it does not depend solely upon the section name.
810 section_already_linked is called via bfd_map_over_sections. */
812 /* This is the shape of the elements inside the already_linked hash
813 table. It maps a name onto a list of already_linked elements with
814 the same name. It's possible to get more than one element in a
815 list if the COMDAT sections have different names. */
817 struct already_linked_hash_entry
819 struct bfd_hash_entry root;
820 struct already_linked *entry;
823 struct already_linked
825 struct already_linked *next;
826 asection *sec;
829 /* The hash table. */
831 static struct bfd_hash_table already_linked_table;
833 static void
834 section_already_linked (bfd *abfd, asection *sec, void *data)
836 lang_input_statement_type *entry = data;
837 flagword flags;
838 const char *name;
839 struct already_linked *l;
840 struct already_linked_hash_entry *already_linked_list;
842 /* If we are only reading symbols from this object, then we want to
843 discard all sections. */
844 if (entry->just_syms_flag)
846 bfd_link_just_syms (sec, &link_info);
847 return;
850 flags = bfd_get_section_flags (abfd, sec);
852 if ((flags & SEC_LINK_ONCE) == 0)
853 return;
855 /* FIXME: When doing a relocatable link, we may have trouble
856 copying relocations in other sections that refer to local symbols
857 in the section being discarded. Those relocations will have to
858 be converted somehow; as of this writing I'm not sure that any of
859 the backends handle that correctly.
861 It is tempting to instead not discard link once sections when
862 doing a relocatable link (technically, they should be discarded
863 whenever we are building constructors). However, that fails,
864 because the linker winds up combining all the link once sections
865 into a single large link once section, which defeats the purpose
866 of having link once sections in the first place.
868 Also, not merging link once sections in a relocatable link
869 causes trouble for MIPS ELF, which relies on link once semantics
870 to handle the .reginfo section correctly. */
872 name = bfd_get_section_name (abfd, sec);
874 already_linked_list =
875 ((struct already_linked_hash_entry *)
876 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
878 for (l = already_linked_list->entry; l != NULL; l = l->next)
880 if (sec->comdat == NULL
881 || l->sec->comdat == NULL
882 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
884 /* The section has already been linked. See if we should
885 issue a warning. */
886 switch (flags & SEC_LINK_DUPLICATES)
888 default:
889 abort ();
891 case SEC_LINK_DUPLICATES_DISCARD:
892 break;
894 case SEC_LINK_DUPLICATES_ONE_ONLY:
895 if (sec->comdat == NULL)
896 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
897 abfd, name);
898 else
899 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
900 abfd, name, sec->comdat->name);
901 break;
903 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
904 /* FIXME: We should really dig out the contents of both
905 sections and memcmp them. The COFF/PE spec says that
906 the Microsoft linker does not implement this
907 correctly, so I'm not going to bother doing it
908 either. */
909 /* Fall through. */
910 case SEC_LINK_DUPLICATES_SAME_SIZE:
911 if (bfd_section_size (abfd, sec)
912 != bfd_section_size (l->sec->owner, l->sec))
913 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
914 abfd, name);
915 break;
918 /* Set the output_section field so that lang_add_section
919 does not create a lang_input_section structure for this
920 section. Since there might be a symbol in the section
921 being discarded, we must retain a pointer to the section
922 which we are really going to use. */
923 sec->output_section = bfd_abs_section_ptr;
924 sec->kept_section = l->sec;
926 if (flags & SEC_GROUP)
927 bfd_discard_group (abfd, sec);
929 return;
933 /* This is the first section with this name. Record it. Allocate
934 the memory from the same obstack as the hash table is kept in. */
936 l = bfd_hash_allocate (&already_linked_table, sizeof *l);
938 l->sec = sec;
939 l->next = already_linked_list->entry;
940 already_linked_list->entry = l;
943 /* Support routines for the hash table used by section_already_linked,
944 initialize the table, fill in an entry and remove the table. */
946 static struct bfd_hash_entry *
947 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
948 struct bfd_hash_table *table,
949 const char *string ATTRIBUTE_UNUSED)
951 struct already_linked_hash_entry *ret =
952 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
954 ret->entry = NULL;
956 return &ret->root;
959 static void
960 already_linked_table_init (void)
962 if (! bfd_hash_table_init_n (&already_linked_table,
963 already_linked_newfunc,
964 42))
965 einfo (_("%P%F: Failed to create hash table\n"));
968 static void
969 already_linked_table_free (void)
971 bfd_hash_table_free (&already_linked_table);
974 /* The wild routines.
976 These expand statements like *(.text) and foo.o to a list of
977 explicit actions, like foo.o(.text), bar.o(.text) and
978 foo.o(.text, .data). */
980 /* Return TRUE if the PATTERN argument is a wildcard pattern.
981 Although backslashes are treated specially if a pattern contains
982 wildcards, we do not consider the mere presence of a backslash to
983 be enough to cause the pattern to be treated as a wildcard.
984 That lets us handle DOS filenames more naturally. */
986 static bfd_boolean
987 wildcardp (const char *pattern)
989 const char *s;
991 for (s = pattern; *s != '\0'; ++s)
992 if (*s == '?'
993 || *s == '*'
994 || *s == '[')
995 return TRUE;
996 return FALSE;
999 /* Add SECTION to the output section OUTPUT. Do this by creating a
1000 lang_input_section statement which is placed at PTR. FILE is the
1001 input file which holds SECTION. */
1003 void
1004 lang_add_section (lang_statement_list_type *ptr,
1005 asection *section,
1006 lang_output_section_statement_type *output,
1007 lang_input_statement_type *file)
1009 flagword flags;
1010 bfd_boolean discard;
1012 flags = bfd_get_section_flags (section->owner, section);
1014 discard = FALSE;
1016 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1017 link. Discard debugging sections marked with SEC_EXCLUDE on a
1018 relocatable link too. */
1019 if ((flags & SEC_EXCLUDE) != 0
1020 && ((flags & SEC_DEBUGGING) != 0 || !link_info.relocatable))
1021 discard = TRUE;
1023 /* Discard input sections which are assigned to a section named
1024 DISCARD_SECTION_NAME. */
1025 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1026 discard = TRUE;
1028 /* Discard debugging sections if we are stripping debugging
1029 information. */
1030 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1031 && (flags & SEC_DEBUGGING) != 0)
1032 discard = TRUE;
1034 if (discard)
1036 if (section->output_section == NULL)
1038 /* This prevents future calls from assigning this section. */
1039 section->output_section = bfd_abs_section_ptr;
1041 return;
1044 if (section->output_section == NULL)
1046 bfd_boolean first;
1047 lang_input_section_type *new;
1048 flagword flags;
1050 if (output->bfd_section == NULL)
1051 init_os (output);
1053 first = ! output->bfd_section->linker_has_input;
1054 output->bfd_section->linker_has_input = 1;
1056 /* Add a section reference to the list. */
1057 new = new_stat (lang_input_section, ptr);
1059 new->section = section;
1060 new->ifile = file;
1061 section->output_section = output->bfd_section;
1063 flags = section->flags;
1065 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1066 to an output section, because we want to be able to include a
1067 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1068 section (I don't know why we want to do this, but we do).
1069 build_link_order in ldwrite.c handles this case by turning
1070 the embedded SEC_NEVER_LOAD section into a fill. */
1072 flags &= ~ SEC_NEVER_LOAD;
1074 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1075 already been processed. One reason to do this is that on pe
1076 format targets, .text$foo sections go into .text and it's odd
1077 to see .text with SEC_LINK_ONCE set. */
1079 if (! link_info.relocatable)
1080 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1082 /* If this is not the first input section, and the SEC_READONLY
1083 flag is not currently set, then don't set it just because the
1084 input section has it set. */
1086 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1087 flags &= ~ SEC_READONLY;
1089 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1090 if (! first
1091 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1092 != (flags & (SEC_MERGE | SEC_STRINGS))
1093 || ((flags & SEC_MERGE)
1094 && section->output_section->entsize != section->entsize)))
1096 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1097 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1100 /* For now make .tbss normal section. */
1101 if ((flags & SEC_THREAD_LOCAL) && ! link_info.relocatable)
1102 flags |= SEC_LOAD;
1104 section->output_section->flags |= flags;
1106 if (flags & SEC_MERGE)
1107 section->output_section->entsize = section->entsize;
1109 /* If SEC_READONLY is not set in the input section, then clear
1110 it from the output section. */
1111 if ((section->flags & SEC_READONLY) == 0)
1112 section->output_section->flags &= ~SEC_READONLY;
1114 switch (output->sectype)
1116 case normal_section:
1117 break;
1118 case dsect_section:
1119 case copy_section:
1120 case info_section:
1121 case overlay_section:
1122 output->bfd_section->flags &= ~SEC_ALLOC;
1123 break;
1124 case noload_section:
1125 output->bfd_section->flags &= ~SEC_LOAD;
1126 output->bfd_section->flags |= SEC_NEVER_LOAD;
1127 break;
1130 /* Copy over SEC_SMALL_DATA. */
1131 if (section->flags & SEC_SMALL_DATA)
1132 section->output_section->flags |= SEC_SMALL_DATA;
1134 if (section->alignment_power > output->bfd_section->alignment_power)
1135 output->bfd_section->alignment_power = section->alignment_power;
1137 /* If supplied an alignment, then force it. */
1138 if (output->section_alignment != -1)
1139 output->bfd_section->alignment_power = output->section_alignment;
1141 if (section->flags & SEC_BLOCK)
1143 section->output_section->flags |= SEC_BLOCK;
1144 /* FIXME: This value should really be obtained from the bfd... */
1145 output->block_value = 128;
1150 /* Handle wildcard sorting. This returns the lang_input_section which
1151 should follow the one we are going to create for SECTION and FILE,
1152 based on the sorting requirements of WILD. It returns NULL if the
1153 new section should just go at the end of the current list. */
1155 static lang_statement_union_type *
1156 wild_sort (lang_wild_statement_type *wild,
1157 struct wildcard_list *sec,
1158 lang_input_statement_type *file,
1159 asection *section)
1161 const char *section_name;
1162 lang_statement_union_type *l;
1164 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1165 return NULL;
1167 section_name = bfd_get_section_name (file->the_bfd, section);
1168 for (l = wild->children.head; l != NULL; l = l->header.next)
1170 lang_input_section_type *ls;
1172 if (l->header.type != lang_input_section_enum)
1173 continue;
1174 ls = &l->input_section;
1176 /* Sorting by filename takes precedence over sorting by section
1177 name. */
1179 if (wild->filenames_sorted)
1181 const char *fn, *ln;
1182 bfd_boolean fa, la;
1183 int i;
1185 /* The PE support for the .idata section as generated by
1186 dlltool assumes that files will be sorted by the name of
1187 the archive and then the name of the file within the
1188 archive. */
1190 if (file->the_bfd != NULL
1191 && bfd_my_archive (file->the_bfd) != NULL)
1193 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1194 fa = TRUE;
1196 else
1198 fn = file->filename;
1199 fa = FALSE;
1202 if (ls->ifile->the_bfd != NULL
1203 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1205 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1206 la = TRUE;
1208 else
1210 ln = ls->ifile->filename;
1211 la = FALSE;
1214 i = strcmp (fn, ln);
1215 if (i > 0)
1216 continue;
1217 else if (i < 0)
1218 break;
1220 if (fa || la)
1222 if (fa)
1223 fn = file->filename;
1224 if (la)
1225 ln = ls->ifile->filename;
1227 i = strcmp (fn, ln);
1228 if (i > 0)
1229 continue;
1230 else if (i < 0)
1231 break;
1235 /* Here either the files are not sorted by name, or we are
1236 looking at the sections for this file. */
1238 if (sec != NULL && sec->spec.sorted)
1240 if (strcmp (section_name,
1241 bfd_get_section_name (ls->ifile->the_bfd,
1242 ls->section))
1243 < 0)
1244 break;
1248 return l;
1251 /* Expand a wild statement for a particular FILE. SECTION may be
1252 NULL, in which case it is a wild card. */
1254 static void
1255 output_section_callback (lang_wild_statement_type *ptr,
1256 struct wildcard_list *sec,
1257 asection *section,
1258 lang_input_statement_type *file,
1259 void *output)
1261 lang_statement_union_type *before;
1263 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1264 if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1265 return;
1267 /* If the wild pattern was marked KEEP, the member sections
1268 should be as well. */
1269 if (ptr->keep_sections)
1270 section->flags |= SEC_KEEP;
1272 before = wild_sort (ptr, sec, file, section);
1274 /* Here BEFORE points to the lang_input_section which
1275 should follow the one we are about to add. If BEFORE
1276 is NULL, then the section should just go at the end
1277 of the current list. */
1279 if (before == NULL)
1280 lang_add_section (&ptr->children, section,
1281 (lang_output_section_statement_type *) output,
1282 file);
1283 else
1285 lang_statement_list_type list;
1286 lang_statement_union_type **pp;
1288 lang_list_init (&list);
1289 lang_add_section (&list, section,
1290 (lang_output_section_statement_type *) output,
1291 file);
1293 /* If we are discarding the section, LIST.HEAD will
1294 be NULL. */
1295 if (list.head != NULL)
1297 ASSERT (list.head->header.next == NULL);
1299 for (pp = &ptr->children.head;
1300 *pp != before;
1301 pp = &(*pp)->header.next)
1302 ASSERT (*pp != NULL);
1304 list.head->header.next = *pp;
1305 *pp = list.head;
1310 /* This is passed a file name which must have been seen already and
1311 added to the statement tree. We will see if it has been opened
1312 already and had its symbols read. If not then we'll read it. */
1314 static lang_input_statement_type *
1315 lookup_name (const char *name)
1317 lang_input_statement_type *search;
1319 for (search = (lang_input_statement_type *) input_file_chain.head;
1320 search != NULL;
1321 search = (lang_input_statement_type *) search->next_real_file)
1323 /* Use the local_sym_name as the name of the file that has
1324 already been loaded as filename might have been transformed
1325 via the search directory lookup mechanism. */
1326 const char * filename = search->local_sym_name;
1328 if (filename == NULL && name == NULL)
1329 return search;
1330 if (filename != NULL
1331 && name != NULL
1332 && strcmp (filename, name) == 0)
1333 break;
1336 if (search == NULL)
1337 search = new_afile (name, lang_input_file_is_search_file_enum, default_target,
1338 FALSE);
1340 /* If we have already added this file, or this file is not real
1341 (FIXME: can that ever actually happen?) or the name is NULL
1342 (FIXME: can that ever actually happen?) don't add this file. */
1343 if (search->loaded
1344 || ! search->real
1345 || search->filename == NULL)
1346 return search;
1348 if (! load_symbols (search, NULL))
1349 return NULL;
1351 return search;
1354 /* Get the symbols for an input file. */
1356 static bfd_boolean
1357 load_symbols (lang_input_statement_type *entry,
1358 lang_statement_list_type *place)
1360 char **matching;
1362 if (entry->loaded)
1363 return TRUE;
1365 ldfile_open_file (entry);
1367 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1368 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1370 bfd_error_type err;
1371 lang_statement_list_type *hold;
1372 bfd_boolean bad_load = TRUE;
1373 bfd_boolean save_ldlang_sysrooted_script;
1375 err = bfd_get_error ();
1377 /* See if the emulation has some special knowledge. */
1378 if (ldemul_unrecognized_file (entry))
1379 return TRUE;
1381 if (err == bfd_error_file_ambiguously_recognized)
1383 char **p;
1385 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1386 einfo (_("%B: matching formats:"), entry->the_bfd);
1387 for (p = matching; *p != NULL; p++)
1388 einfo (" %s", *p);
1389 einfo ("%F\n");
1391 else if (err != bfd_error_file_not_recognized
1392 || place == NULL)
1393 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1394 else
1395 bad_load = FALSE;
1397 bfd_close (entry->the_bfd);
1398 entry->the_bfd = NULL;
1400 /* Try to interpret the file as a linker script. */
1401 ldfile_open_command_file (entry->filename);
1403 hold = stat_ptr;
1404 stat_ptr = place;
1405 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1406 ldlang_sysrooted_script = entry->sysrooted;
1408 ldfile_assumed_script = TRUE;
1409 parser_input = input_script;
1410 yyparse ();
1411 ldfile_assumed_script = FALSE;
1413 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1414 stat_ptr = hold;
1416 return ! bad_load;
1419 if (ldemul_recognized_file (entry))
1420 return TRUE;
1422 /* We don't call ldlang_add_file for an archive. Instead, the
1423 add_symbols entry point will call ldlang_add_file, via the
1424 add_archive_element callback, for each element of the archive
1425 which is used. */
1426 switch (bfd_get_format (entry->the_bfd))
1428 default:
1429 break;
1431 case bfd_object:
1432 ldlang_add_file (entry);
1433 if (trace_files || trace_file_tries)
1434 info_msg ("%I\n", entry);
1435 break;
1437 case bfd_archive:
1438 if (entry->whole_archive)
1440 bfd *member = NULL;
1441 bfd_boolean loaded = TRUE;
1443 for (;;)
1445 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1447 if (member == NULL)
1448 break;
1450 if (! bfd_check_format (member, bfd_object))
1452 einfo (_("%F%B: member %B in archive is not an object\n"),
1453 entry->the_bfd, member);
1454 loaded = FALSE;
1457 if (! ((*link_info.callbacks->add_archive_element)
1458 (&link_info, member, "--whole-archive")))
1459 abort ();
1461 if (! bfd_link_add_symbols (member, &link_info))
1463 einfo (_("%F%B: could not read symbols: %E\n"), member);
1464 loaded = FALSE;
1468 entry->loaded = loaded;
1469 return loaded;
1471 break;
1474 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1475 entry->loaded = TRUE;
1476 else
1477 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1479 return entry->loaded;
1482 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1483 may be NULL, indicating that it is a wildcard. Separate
1484 lang_input_section statements are created for each part of the
1485 expansion; they are added after the wild statement S. OUTPUT is
1486 the output section. */
1488 static void
1489 wild (lang_wild_statement_type *s,
1490 const char *target ATTRIBUTE_UNUSED,
1491 lang_output_section_statement_type *output)
1493 struct wildcard_list *sec;
1495 walk_wild (s, output_section_callback, output);
1497 for (sec = s->section_list; sec != NULL; sec = sec->next)
1499 if (default_common_section != NULL)
1500 break;
1501 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1503 /* Remember the section that common is going to in case we
1504 later get something which doesn't know where to put it. */
1505 default_common_section = output;
1510 /* Return TRUE iff target is the sought target. */
1512 static int
1513 get_target (const bfd_target *target, void *data)
1515 const char *sought = data;
1517 return strcmp (target->name, sought) == 0;
1520 /* Like strcpy() but convert to lower case as well. */
1522 static void
1523 stricpy (char *dest, char *src)
1525 char c;
1527 while ((c = *src++) != 0)
1528 *dest++ = TOLOWER (c);
1530 *dest = 0;
1533 /* Remove the first occurrence of needle (if any) in haystack
1534 from haystack. */
1536 static void
1537 strcut (char *haystack, char *needle)
1539 haystack = strstr (haystack, needle);
1541 if (haystack)
1543 char *src;
1545 for (src = haystack + strlen (needle); *src;)
1546 *haystack++ = *src++;
1548 *haystack = 0;
1552 /* Compare two target format name strings.
1553 Return a value indicating how "similar" they are. */
1555 static int
1556 name_compare (char *first, char *second)
1558 char *copy1;
1559 char *copy2;
1560 int result;
1562 copy1 = xmalloc (strlen (first) + 1);
1563 copy2 = xmalloc (strlen (second) + 1);
1565 /* Convert the names to lower case. */
1566 stricpy (copy1, first);
1567 stricpy (copy2, second);
1569 /* Remove size and endian strings from the name. */
1570 strcut (copy1, "big");
1571 strcut (copy1, "little");
1572 strcut (copy2, "big");
1573 strcut (copy2, "little");
1575 /* Return a value based on how many characters match,
1576 starting from the beginning. If both strings are
1577 the same then return 10 * their length. */
1578 for (result = 0; copy1[result] == copy2[result]; result++)
1579 if (copy1[result] == 0)
1581 result *= 10;
1582 break;
1585 free (copy1);
1586 free (copy2);
1588 return result;
1591 /* Set by closest_target_match() below. */
1592 static const bfd_target *winner;
1594 /* Scan all the valid bfd targets looking for one that has the endianness
1595 requirement that was specified on the command line, and is the nearest
1596 match to the original output target. */
1598 static int
1599 closest_target_match (const bfd_target *target, void *data)
1601 const bfd_target *original = data;
1603 if (command_line.endian == ENDIAN_BIG
1604 && target->byteorder != BFD_ENDIAN_BIG)
1605 return 0;
1607 if (command_line.endian == ENDIAN_LITTLE
1608 && target->byteorder != BFD_ENDIAN_LITTLE)
1609 return 0;
1611 /* Must be the same flavour. */
1612 if (target->flavour != original->flavour)
1613 return 0;
1615 /* If we have not found a potential winner yet, then record this one. */
1616 if (winner == NULL)
1618 winner = target;
1619 return 0;
1622 /* Oh dear, we now have two potential candidates for a successful match.
1623 Compare their names and choose the better one. */
1624 if (name_compare (target->name, original->name)
1625 > name_compare (winner->name, original->name))
1626 winner = target;
1628 /* Keep on searching until wqe have checked them all. */
1629 return 0;
1632 /* Return the BFD target format of the first input file. */
1634 static char *
1635 get_first_input_target (void)
1637 char *target = NULL;
1639 LANG_FOR_EACH_INPUT_STATEMENT (s)
1641 if (s->header.type == lang_input_statement_enum
1642 && s->real)
1644 ldfile_open_file (s);
1646 if (s->the_bfd != NULL
1647 && bfd_check_format (s->the_bfd, bfd_object))
1649 target = bfd_get_target (s->the_bfd);
1651 if (target != NULL)
1652 break;
1657 return target;
1660 const char *
1661 lang_get_output_target (void)
1663 const char *target;
1665 /* Has the user told us which output format to use? */
1666 if (output_target != NULL)
1667 return output_target;
1669 /* No - has the current target been set to something other than
1670 the default? */
1671 if (current_target != default_target)
1672 return current_target;
1674 /* No - can we determine the format of the first input file? */
1675 target = get_first_input_target ();
1676 if (target != NULL)
1677 return target;
1679 /* Failed - use the default output target. */
1680 return default_target;
1683 /* Open the output file. */
1685 static bfd *
1686 open_output (const char *name)
1688 bfd *output;
1690 output_target = lang_get_output_target ();
1692 /* Has the user requested a particular endianness on the command
1693 line? */
1694 if (command_line.endian != ENDIAN_UNSET)
1696 const bfd_target *target;
1697 enum bfd_endian desired_endian;
1699 /* Get the chosen target. */
1700 target = bfd_search_for_target (get_target, (void *) output_target);
1702 /* If the target is not supported, we cannot do anything. */
1703 if (target != NULL)
1705 if (command_line.endian == ENDIAN_BIG)
1706 desired_endian = BFD_ENDIAN_BIG;
1707 else
1708 desired_endian = BFD_ENDIAN_LITTLE;
1710 /* See if the target has the wrong endianness. This should
1711 not happen if the linker script has provided big and
1712 little endian alternatives, but some scrips don't do
1713 this. */
1714 if (target->byteorder != desired_endian)
1716 /* If it does, then see if the target provides
1717 an alternative with the correct endianness. */
1718 if (target->alternative_target != NULL
1719 && (target->alternative_target->byteorder == desired_endian))
1720 output_target = target->alternative_target->name;
1721 else
1723 /* Try to find a target as similar as possible to
1724 the default target, but which has the desired
1725 endian characteristic. */
1726 bfd_search_for_target (closest_target_match,
1727 (void *) target);
1729 /* Oh dear - we could not find any targets that
1730 satisfy our requirements. */
1731 if (winner == NULL)
1732 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1733 else
1734 output_target = winner->name;
1740 output = bfd_openw (name, output_target);
1742 if (output == NULL)
1744 if (bfd_get_error () == bfd_error_invalid_target)
1745 einfo (_("%P%F: target %s not found\n"), output_target);
1747 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1750 delete_output_file_on_failure = TRUE;
1752 #if 0
1753 output->flags |= D_PAGED;
1754 #endif
1756 if (! bfd_set_format (output, bfd_object))
1757 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1758 if (! bfd_set_arch_mach (output,
1759 ldfile_output_architecture,
1760 ldfile_output_machine))
1761 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1763 link_info.hash = bfd_link_hash_table_create (output);
1764 if (link_info.hash == NULL)
1765 einfo (_("%P%F: can not create link hash table: %E\n"));
1767 bfd_set_gp_size (output, g_switch_value);
1768 return output;
1771 static void
1772 ldlang_open_output (lang_statement_union_type *statement)
1774 switch (statement->header.type)
1776 case lang_output_statement_enum:
1777 ASSERT (output_bfd == NULL);
1778 output_bfd = open_output (statement->output_statement.name);
1779 ldemul_set_output_arch ();
1780 if (config.magic_demand_paged && !link_info.relocatable)
1781 output_bfd->flags |= D_PAGED;
1782 else
1783 output_bfd->flags &= ~D_PAGED;
1784 if (config.text_read_only)
1785 output_bfd->flags |= WP_TEXT;
1786 else
1787 output_bfd->flags &= ~WP_TEXT;
1788 if (link_info.traditional_format)
1789 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1790 else
1791 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1792 break;
1794 case lang_target_statement_enum:
1795 current_target = statement->target_statement.target;
1796 break;
1797 default:
1798 break;
1802 /* Convert between addresses in bytes and sizes in octets.
1803 For currently supported targets, octets_per_byte is always a power
1804 of two, so we can use shifts. */
1805 #define TO_ADDR(X) ((X) >> opb_shift)
1806 #define TO_SIZE(X) ((X) << opb_shift)
1808 /* Support the above. */
1809 static unsigned int opb_shift = 0;
1811 static void
1812 init_opb (void)
1814 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1815 ldfile_output_machine);
1816 opb_shift = 0;
1817 if (x > 1)
1818 while ((x & 1) == 0)
1820 x >>= 1;
1821 ++opb_shift;
1823 ASSERT (x == 1);
1826 /* Open all the input files. */
1828 static void
1829 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1831 for (; s != NULL; s = s->header.next)
1833 switch (s->header.type)
1835 case lang_constructors_statement_enum:
1836 open_input_bfds (constructor_list.head, force);
1837 break;
1838 case lang_output_section_statement_enum:
1839 open_input_bfds (s->output_section_statement.children.head, force);
1840 break;
1841 case lang_wild_statement_enum:
1842 /* Maybe we should load the file's symbols. */
1843 if (s->wild_statement.filename
1844 && ! wildcardp (s->wild_statement.filename))
1845 (void) lookup_name (s->wild_statement.filename);
1846 open_input_bfds (s->wild_statement.children.head, force);
1847 break;
1848 case lang_group_statement_enum:
1850 struct bfd_link_hash_entry *undefs;
1852 /* We must continually search the entries in the group
1853 until no new symbols are added to the list of undefined
1854 symbols. */
1858 undefs = link_info.hash->undefs_tail;
1859 open_input_bfds (s->group_statement.children.head, TRUE);
1861 while (undefs != link_info.hash->undefs_tail);
1863 break;
1864 case lang_target_statement_enum:
1865 current_target = s->target_statement.target;
1866 break;
1867 case lang_input_statement_enum:
1868 if (s->input_statement.real)
1870 lang_statement_list_type add;
1872 s->input_statement.target = current_target;
1874 /* If we are being called from within a group, and this
1875 is an archive which has already been searched, then
1876 force it to be researched unless the whole archive
1877 has been loaded already. */
1878 if (force
1879 && !s->input_statement.whole_archive
1880 && s->input_statement.loaded
1881 && bfd_check_format (s->input_statement.the_bfd,
1882 bfd_archive))
1883 s->input_statement.loaded = FALSE;
1885 lang_list_init (&add);
1887 if (! load_symbols (&s->input_statement, &add))
1888 config.make_executable = FALSE;
1890 if (add.head != NULL)
1892 *add.tail = s->header.next;
1893 s->header.next = add.head;
1896 break;
1897 default:
1898 break;
1903 /* If there are [COMMONS] statements, put a wild one into the bss
1904 section. */
1906 static void
1907 lang_reasonable_defaults (void)
1909 #if 0
1910 lang_output_section_statement_lookup (".text");
1911 lang_output_section_statement_lookup (".data");
1913 default_common_section = lang_output_section_statement_lookup (".bss");
1915 if (!placed_commons)
1917 lang_wild_statement_type *new =
1918 new_stat (lang_wild_statement,
1919 &default_common_section->children);
1921 new->section_name = "COMMON";
1922 new->filename = NULL;
1923 lang_list_init (&new->children);
1925 #endif
1928 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1930 void
1931 lang_track_definedness (const char *name)
1933 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1934 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1937 /* New-function for the definedness hash table. */
1939 static struct bfd_hash_entry *
1940 lang_definedness_newfunc (struct bfd_hash_entry *entry,
1941 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1942 const char *name ATTRIBUTE_UNUSED)
1944 struct lang_definedness_hash_entry *ret
1945 = (struct lang_definedness_hash_entry *) entry;
1947 if (ret == NULL)
1948 ret = (struct lang_definedness_hash_entry *)
1949 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1951 if (ret == NULL)
1952 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1954 ret->iteration = -1;
1955 return &ret->root;
1958 /* Return the iteration when the definition of NAME was last updated. A
1959 value of -1 means that the symbol is not defined in the linker script
1960 or the command line, but may be defined in the linker symbol table. */
1963 lang_symbol_definition_iteration (const char *name)
1965 struct lang_definedness_hash_entry *defentry
1966 = (struct lang_definedness_hash_entry *)
1967 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1969 /* We've already created this one on the presence of DEFINED in the
1970 script, so it can't be NULL unless something is borked elsewhere in
1971 the code. */
1972 if (defentry == NULL)
1973 FAIL ();
1975 return defentry->iteration;
1978 /* Update the definedness state of NAME. */
1980 void
1981 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1983 struct lang_definedness_hash_entry *defentry
1984 = (struct lang_definedness_hash_entry *)
1985 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1987 /* We don't keep track of symbols not tested with DEFINED. */
1988 if (defentry == NULL)
1989 return;
1991 /* If the symbol was already defined, and not from an earlier statement
1992 iteration, don't update the definedness iteration, because that'd
1993 make the symbol seem defined in the linker script at this point, and
1994 it wasn't; it was defined in some object. If we do anyway, DEFINED
1995 would start to yield false before this point and the construct "sym =
1996 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1997 in an object. */
1998 if (h->type != bfd_link_hash_undefined
1999 && h->type != bfd_link_hash_common
2000 && h->type != bfd_link_hash_new
2001 && defentry->iteration == -1)
2002 return;
2004 defentry->iteration = lang_statement_iteration;
2007 /* Add the supplied name to the symbol table as an undefined reference.
2008 This is a two step process as the symbol table doesn't even exist at
2009 the time the ld command line is processed. First we put the name
2010 on a list, then, once the output file has been opened, transfer the
2011 name to the symbol table. */
2013 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2015 #define ldlang_undef_chain_list_head entry_symbol.next
2017 void
2018 ldlang_add_undef (const char *const name)
2020 ldlang_undef_chain_list_type *new =
2021 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2023 new->next = ldlang_undef_chain_list_head;
2024 ldlang_undef_chain_list_head = new;
2026 new->name = xstrdup (name);
2028 if (output_bfd != NULL)
2029 insert_undefined (new->name);
2032 /* Insert NAME as undefined in the symbol table. */
2034 static void
2035 insert_undefined (const char *name)
2037 struct bfd_link_hash_entry *h;
2039 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2040 if (h == NULL)
2041 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2042 if (h->type == bfd_link_hash_new)
2044 h->type = bfd_link_hash_undefined;
2045 h->u.undef.abfd = NULL;
2046 bfd_link_add_undef (link_info.hash, h);
2050 /* Run through the list of undefineds created above and place them
2051 into the linker hash table as undefined symbols belonging to the
2052 script file. */
2054 static void
2055 lang_place_undefineds (void)
2057 ldlang_undef_chain_list_type *ptr;
2059 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2060 insert_undefined (ptr->name);
2063 /* Open input files and attach to output sections. */
2065 static void
2066 map_input_to_output_sections
2067 (lang_statement_union_type *s, const char *target,
2068 lang_output_section_statement_type *output_section_statement)
2070 for (; s != NULL; s = s->header.next)
2072 switch (s->header.type)
2074 case lang_wild_statement_enum:
2075 wild (&s->wild_statement, target, output_section_statement);
2076 break;
2077 case lang_constructors_statement_enum:
2078 map_input_to_output_sections (constructor_list.head,
2079 target,
2080 output_section_statement);
2081 break;
2082 case lang_output_section_statement_enum:
2083 map_input_to_output_sections (s->output_section_statement.children.head,
2084 target,
2085 &s->output_section_statement);
2086 break;
2087 case lang_output_statement_enum:
2088 break;
2089 case lang_target_statement_enum:
2090 target = s->target_statement.target;
2091 break;
2092 case lang_group_statement_enum:
2093 map_input_to_output_sections (s->group_statement.children.head,
2094 target,
2095 output_section_statement);
2096 break;
2097 case lang_fill_statement_enum:
2098 case lang_input_section_enum:
2099 case lang_object_symbols_statement_enum:
2100 case lang_data_statement_enum:
2101 case lang_reloc_statement_enum:
2102 case lang_padding_statement_enum:
2103 case lang_input_statement_enum:
2104 if (output_section_statement != NULL
2105 && output_section_statement->bfd_section == NULL)
2106 init_os (output_section_statement);
2107 break;
2108 case lang_assignment_statement_enum:
2109 if (output_section_statement != NULL
2110 && output_section_statement->bfd_section == NULL)
2111 init_os (output_section_statement);
2113 /* Make sure that any sections mentioned in the assignment
2114 are initialized. */
2115 exp_init_os (s->assignment_statement.exp);
2116 break;
2117 case lang_afile_asection_pair_statement_enum:
2118 FAIL ();
2119 break;
2120 case lang_address_statement_enum:
2121 /* Mark the specified section with the supplied address. */
2123 lang_output_section_statement_type *os =
2124 lang_output_section_statement_lookup
2125 (s->address_statement.section_name);
2127 if (os->bfd_section == NULL)
2128 init_os (os);
2129 os->addr_tree = s->address_statement.address;
2131 break;
2136 /* An output section might have been removed after its statement was
2137 added. For example, ldemul_before_allocation can remove dynamic
2138 sections if they turn out to be not needed. Clean them up here. */
2140 static void
2141 strip_excluded_output_sections (void)
2143 lang_statement_union_type *u;
2145 for (u = lang_output_section_statement.head;
2146 u != NULL;
2147 u = u->output_section_statement.next)
2149 lang_output_section_statement_type *os;
2150 asection *s;
2152 os = &u->output_section_statement;
2153 s = os->bfd_section;
2154 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2156 asection **p;
2158 os->bfd_section = NULL;
2160 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2161 if (*p == s)
2163 bfd_section_list_remove (output_bfd, p);
2164 output_bfd->section_count--;
2165 break;
2171 static void
2172 print_output_section_statement
2173 (lang_output_section_statement_type *output_section_statement)
2175 asection *section = output_section_statement->bfd_section;
2176 int len;
2178 if (output_section_statement != abs_output_section)
2180 minfo ("\n%s", output_section_statement->name);
2182 if (section != NULL)
2184 print_dot = section->vma;
2186 len = strlen (output_section_statement->name);
2187 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2189 print_nl ();
2190 len = 0;
2192 while (len < SECTION_NAME_MAP_LENGTH)
2194 print_space ();
2195 ++len;
2198 minfo ("0x%V %W", section->vma, section->_raw_size);
2200 if (output_section_statement->load_base != NULL)
2202 bfd_vma addr;
2204 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2205 "load base", lang_final_phase_enum);
2206 minfo (_(" load address 0x%V"), addr);
2210 print_nl ();
2213 print_statement_list (output_section_statement->children.head,
2214 output_section_statement);
2217 static void
2218 print_assignment (lang_assignment_statement_type *assignment,
2219 lang_output_section_statement_type *output_section)
2221 int i;
2222 etree_value_type result;
2224 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2225 print_space ();
2227 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2228 lang_final_phase_enum, print_dot, &print_dot);
2229 if (result.valid_p)
2231 const char *dst;
2232 bfd_vma value;
2234 value = result.value + result.section->bfd_section->vma;
2235 dst = assignment->exp->assign.dst;
2237 minfo ("0x%V", value);
2238 if (dst[0] == '.' && dst[1] == 0)
2239 print_dot = value;
2241 else
2243 minfo ("*undef* ");
2244 #ifdef BFD64
2245 minfo (" ");
2246 #endif
2249 minfo (" ");
2251 exp_print_tree (assignment->exp);
2253 print_nl ();
2256 static void
2257 print_input_statement (lang_input_statement_type *statm)
2259 if (statm->filename != NULL)
2261 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2265 /* Print all symbols defined in a particular section. This is called
2266 via bfd_link_hash_traverse. */
2268 static bfd_boolean
2269 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2271 asection *sec = ptr;
2273 if ((hash_entry->type == bfd_link_hash_defined
2274 || hash_entry->type == bfd_link_hash_defweak)
2275 && sec == hash_entry->u.def.section)
2277 int i;
2279 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2280 print_space ();
2281 minfo ("0x%V ",
2282 (hash_entry->u.def.value
2283 + hash_entry->u.def.section->output_offset
2284 + hash_entry->u.def.section->output_section->vma));
2286 minfo (" %T\n", hash_entry->root.string);
2289 return TRUE;
2292 /* Print information about an input section to the map file. */
2294 static void
2295 print_input_section (lang_input_section_type *in)
2297 asection *i = in->section;
2298 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2300 init_opb ();
2301 if (size != 0)
2303 print_space ();
2305 minfo ("%s", i->name);
2307 if (i->output_section != NULL)
2309 int len;
2311 len = 1 + strlen (i->name);
2312 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2314 print_nl ();
2315 len = 0;
2317 while (len < SECTION_NAME_MAP_LENGTH)
2319 print_space ();
2320 ++len;
2323 minfo ("0x%V %W %B\n",
2324 i->output_section->vma + i->output_offset, TO_ADDR (size),
2325 i->owner);
2327 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2329 len = SECTION_NAME_MAP_LENGTH + 3;
2330 #ifdef BFD64
2331 len += 16;
2332 #else
2333 len += 8;
2334 #endif
2335 while (len > 0)
2337 print_space ();
2338 --len;
2341 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2344 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2346 print_dot = (i->output_section->vma + i->output_offset
2347 + TO_ADDR (size));
2352 static void
2353 print_fill_statement (lang_fill_statement_type *fill)
2355 size_t size;
2356 unsigned char *p;
2357 fputs (" FILL mask 0x", config.map_file);
2358 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2359 fprintf (config.map_file, "%02x", *p);
2360 fputs ("\n", config.map_file);
2363 static void
2364 print_data_statement (lang_data_statement_type *data)
2366 int i;
2367 bfd_vma addr;
2368 bfd_size_type size;
2369 const char *name;
2371 init_opb ();
2372 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2373 print_space ();
2375 addr = data->output_vma;
2376 if (data->output_section != NULL)
2377 addr += data->output_section->vma;
2379 switch (data->type)
2381 default:
2382 abort ();
2383 case BYTE:
2384 size = BYTE_SIZE;
2385 name = "BYTE";
2386 break;
2387 case SHORT:
2388 size = SHORT_SIZE;
2389 name = "SHORT";
2390 break;
2391 case LONG:
2392 size = LONG_SIZE;
2393 name = "LONG";
2394 break;
2395 case QUAD:
2396 size = QUAD_SIZE;
2397 name = "QUAD";
2398 break;
2399 case SQUAD:
2400 size = QUAD_SIZE;
2401 name = "SQUAD";
2402 break;
2405 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2407 if (data->exp->type.node_class != etree_value)
2409 print_space ();
2410 exp_print_tree (data->exp);
2413 print_nl ();
2415 print_dot = addr + TO_ADDR (size);
2418 /* Print an address statement. These are generated by options like
2419 -Ttext. */
2421 static void
2422 print_address_statement (lang_address_statement_type *address)
2424 minfo (_("Address of section %s set to "), address->section_name);
2425 exp_print_tree (address->address);
2426 print_nl ();
2429 /* Print a reloc statement. */
2431 static void
2432 print_reloc_statement (lang_reloc_statement_type *reloc)
2434 int i;
2435 bfd_vma addr;
2436 bfd_size_type size;
2438 init_opb ();
2439 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2440 print_space ();
2442 addr = reloc->output_vma;
2443 if (reloc->output_section != NULL)
2444 addr += reloc->output_section->vma;
2446 size = bfd_get_reloc_size (reloc->howto);
2448 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2450 if (reloc->name != NULL)
2451 minfo ("%s+", reloc->name);
2452 else
2453 minfo ("%s+", reloc->section->name);
2455 exp_print_tree (reloc->addend_exp);
2457 print_nl ();
2459 print_dot = addr + TO_ADDR (size);
2462 static void
2463 print_padding_statement (lang_padding_statement_type *s)
2465 int len;
2466 bfd_vma addr;
2468 init_opb ();
2469 minfo (" *fill*");
2471 len = sizeof " *fill*" - 1;
2472 while (len < SECTION_NAME_MAP_LENGTH)
2474 print_space ();
2475 ++len;
2478 addr = s->output_offset;
2479 if (s->output_section != NULL)
2480 addr += s->output_section->vma;
2481 minfo ("0x%V %W ", addr, s->size);
2483 if (s->fill->size != 0)
2485 size_t size;
2486 unsigned char *p;
2487 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2488 fprintf (config.map_file, "%02x", *p);
2491 print_nl ();
2493 print_dot = addr + TO_ADDR (s->size);
2496 static void
2497 print_wild_statement (lang_wild_statement_type *w,
2498 lang_output_section_statement_type *os)
2500 struct wildcard_list *sec;
2502 print_space ();
2504 if (w->filenames_sorted)
2505 minfo ("SORT(");
2506 if (w->filename != NULL)
2507 minfo ("%s", w->filename);
2508 else
2509 minfo ("*");
2510 if (w->filenames_sorted)
2511 minfo (")");
2513 minfo ("(");
2514 for (sec = w->section_list; sec; sec = sec->next)
2516 if (sec->spec.sorted)
2517 minfo ("SORT(");
2518 if (sec->spec.exclude_name_list != NULL)
2520 name_list *tmp;
2521 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2522 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2523 minfo (" %s", tmp->name);
2524 minfo (") ");
2526 if (sec->spec.name != NULL)
2527 minfo ("%s", sec->spec.name);
2528 else
2529 minfo ("*");
2530 if (sec->spec.sorted)
2531 minfo (")");
2532 if (sec->next)
2533 minfo (" ");
2535 minfo (")");
2537 print_nl ();
2539 print_statement_list (w->children.head, os);
2542 /* Print a group statement. */
2544 static void
2545 print_group (lang_group_statement_type *s,
2546 lang_output_section_statement_type *os)
2548 fprintf (config.map_file, "START GROUP\n");
2549 print_statement_list (s->children.head, os);
2550 fprintf (config.map_file, "END GROUP\n");
2553 /* Print the list of statements in S.
2554 This can be called for any statement type. */
2556 static void
2557 print_statement_list (lang_statement_union_type *s,
2558 lang_output_section_statement_type *os)
2560 while (s != NULL)
2562 print_statement (s, os);
2563 s = s->header.next;
2567 /* Print the first statement in statement list S.
2568 This can be called for any statement type. */
2570 static void
2571 print_statement (lang_statement_union_type *s,
2572 lang_output_section_statement_type *os)
2574 switch (s->header.type)
2576 default:
2577 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2578 FAIL ();
2579 break;
2580 case lang_constructors_statement_enum:
2581 if (constructor_list.head != NULL)
2583 if (constructors_sorted)
2584 minfo (" SORT (CONSTRUCTORS)\n");
2585 else
2586 minfo (" CONSTRUCTORS\n");
2587 print_statement_list (constructor_list.head, os);
2589 break;
2590 case lang_wild_statement_enum:
2591 print_wild_statement (&s->wild_statement, os);
2592 break;
2593 case lang_address_statement_enum:
2594 print_address_statement (&s->address_statement);
2595 break;
2596 case lang_object_symbols_statement_enum:
2597 minfo (" CREATE_OBJECT_SYMBOLS\n");
2598 break;
2599 case lang_fill_statement_enum:
2600 print_fill_statement (&s->fill_statement);
2601 break;
2602 case lang_data_statement_enum:
2603 print_data_statement (&s->data_statement);
2604 break;
2605 case lang_reloc_statement_enum:
2606 print_reloc_statement (&s->reloc_statement);
2607 break;
2608 case lang_input_section_enum:
2609 print_input_section (&s->input_section);
2610 break;
2611 case lang_padding_statement_enum:
2612 print_padding_statement (&s->padding_statement);
2613 break;
2614 case lang_output_section_statement_enum:
2615 print_output_section_statement (&s->output_section_statement);
2616 break;
2617 case lang_assignment_statement_enum:
2618 print_assignment (&s->assignment_statement, os);
2619 break;
2620 case lang_target_statement_enum:
2621 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2622 break;
2623 case lang_output_statement_enum:
2624 minfo ("OUTPUT(%s", s->output_statement.name);
2625 if (output_target != NULL)
2626 minfo (" %s", output_target);
2627 minfo (")\n");
2628 break;
2629 case lang_input_statement_enum:
2630 print_input_statement (&s->input_statement);
2631 break;
2632 case lang_group_statement_enum:
2633 print_group (&s->group_statement, os);
2634 break;
2635 case lang_afile_asection_pair_statement_enum:
2636 FAIL ();
2637 break;
2641 static void
2642 print_statements (void)
2644 print_statement_list (statement_list.head, abs_output_section);
2647 /* Print the first N statements in statement list S to STDERR.
2648 If N == 0, nothing is printed.
2649 If N < 0, the entire list is printed.
2650 Intended to be called from GDB. */
2652 void
2653 dprint_statement (lang_statement_union_type *s, int n)
2655 FILE *map_save = config.map_file;
2657 config.map_file = stderr;
2659 if (n < 0)
2660 print_statement_list (s, abs_output_section);
2661 else
2663 while (s && --n >= 0)
2665 print_statement (s, abs_output_section);
2666 s = s->header.next;
2670 config.map_file = map_save;
2673 static void
2674 insert_pad (lang_statement_union_type **ptr,
2675 fill_type *fill,
2676 unsigned int alignment_needed,
2677 asection *output_section,
2678 bfd_vma dot)
2680 static fill_type zero_fill = { 1, { 0 } };
2681 lang_statement_union_type *pad;
2683 pad = ((lang_statement_union_type *)
2684 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2685 if (ptr != &statement_list.head
2686 && pad->header.type == lang_padding_statement_enum
2687 && pad->padding_statement.output_section == output_section)
2689 /* Use the existing pad statement. The above test on output
2690 section is probably redundant, but it doesn't hurt to check. */
2692 else
2694 /* Make a new padding statement, linked into existing chain. */
2695 pad = stat_alloc (sizeof (lang_padding_statement_type));
2696 pad->header.next = *ptr;
2697 *ptr = pad;
2698 pad->header.type = lang_padding_statement_enum;
2699 pad->padding_statement.output_section = output_section;
2700 if (fill == NULL)
2701 fill = &zero_fill;
2702 pad->padding_statement.fill = fill;
2704 pad->padding_statement.output_offset = dot - output_section->vma;
2705 pad->padding_statement.size = alignment_needed;
2706 output_section->_raw_size += alignment_needed;
2709 /* Work out how much this section will move the dot point. */
2711 static bfd_vma
2712 size_input_section (lang_statement_union_type **this_ptr,
2713 lang_output_section_statement_type *output_section_statement,
2714 fill_type *fill,
2715 bfd_vma dot)
2717 lang_input_section_type *is = &((*this_ptr)->input_section);
2718 asection *i = is->section;
2720 if (!is->ifile->just_syms_flag)
2722 unsigned int alignment_needed;
2723 asection *o;
2725 /* Align this section first to the input sections requirement,
2726 then to the output section's requirement. If this alignment
2727 is greater than any seen before, then record it too. Perform
2728 the alignment by inserting a magic 'padding' statement. */
2730 if (output_section_statement->subsection_alignment != -1)
2731 i->alignment_power = output_section_statement->subsection_alignment;
2733 o = output_section_statement->bfd_section;
2734 if (o->alignment_power < i->alignment_power)
2735 o->alignment_power = i->alignment_power;
2737 alignment_needed = align_power (dot, i->alignment_power) - dot;
2739 if (alignment_needed != 0)
2741 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2742 dot += alignment_needed;
2745 /* Remember where in the output section this input section goes. */
2747 i->output_offset = dot - o->vma;
2749 /* Mark how big the output section must be to contain this now. */
2750 if (i->_cooked_size != 0)
2751 dot += TO_ADDR (i->_cooked_size);
2752 else
2753 dot += TO_ADDR (i->_raw_size);
2754 o->_raw_size = TO_SIZE (dot - o->vma);
2756 else
2758 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2761 return dot;
2764 #define IGNORE_SECTION(bfd, s) \
2765 (((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_LOAD)) \
2766 != (SEC_ALLOC | SEC_LOAD)) \
2767 || bfd_section_size (bfd, s) == 0)
2769 /* Check to see if any allocated sections overlap with other allocated
2770 sections. This can happen when the linker script specifically specifies
2771 the output section addresses of the two sections. */
2773 static void
2774 lang_check_section_addresses (void)
2776 asection *s;
2778 /* Scan all sections in the output list. */
2779 for (s = output_bfd->sections; s != NULL; s = s->next)
2781 asection *os;
2783 /* Ignore sections which are not loaded or which have no contents. */
2784 if (IGNORE_SECTION (output_bfd, s))
2785 continue;
2787 /* Once we reach section 's' stop our seach. This prevents two
2788 warning messages from being produced, one for 'section A overlaps
2789 section B' and one for 'section B overlaps section A'. */
2790 for (os = output_bfd->sections; os != s; os = os->next)
2792 bfd_vma s_start;
2793 bfd_vma s_end;
2794 bfd_vma os_start;
2795 bfd_vma os_end;
2797 /* Only consider loadable sections with real contents. */
2798 if (IGNORE_SECTION (output_bfd, os))
2799 continue;
2801 /* We must check the sections' LMA addresses not their
2802 VMA addresses because overlay sections can have
2803 overlapping VMAs but they must have distinct LMAs. */
2804 s_start = bfd_section_lma (output_bfd, s);
2805 os_start = bfd_section_lma (output_bfd, os);
2806 s_end = s_start + TO_ADDR (bfd_section_size (output_bfd, s)) - 1;
2807 os_end = os_start + TO_ADDR (bfd_section_size (output_bfd, os)) - 1;
2809 /* Look for an overlap. */
2810 if ((s_end < os_start) || (s_start > os_end))
2811 continue;
2813 einfo (
2814 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2815 s->name, s_start, s_end, os->name, os_start, os_end);
2817 /* Once we have found one overlap for this section,
2818 stop looking for others. */
2819 break;
2824 /* Make sure the new address is within the region. We explicitly permit the
2825 current address to be at the exact end of the region when the address is
2826 non-zero, in case the region is at the end of addressable memory and the
2827 calculation wraps around. */
2829 static void
2830 os_region_check (lang_output_section_statement_type *os,
2831 struct memory_region_struct *region,
2832 etree_type *tree,
2833 bfd_vma base)
2835 if ((region->current < region->origin
2836 || (region->current - region->origin > region->length))
2837 && ((region->current != region->origin + region->length)
2838 || base == 0))
2840 if (tree != NULL)
2842 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2843 region->current,
2844 os->bfd_section->owner,
2845 os->bfd_section->name,
2846 region->name);
2848 else
2850 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2851 region->name,
2852 os->bfd_section->owner,
2853 os->bfd_section->name);
2855 /* Reset the region pointer. */
2856 region->current = region->origin;
2860 /* Set the sizes for all the output sections. */
2862 static bfd_vma
2863 lang_size_sections_1
2864 (lang_statement_union_type *s,
2865 lang_output_section_statement_type *output_section_statement,
2866 lang_statement_union_type **prev,
2867 fill_type *fill,
2868 bfd_vma dot,
2869 bfd_boolean *relax,
2870 bfd_boolean check_regions)
2872 /* Size up the sections from their constituent parts. */
2873 for (; s != NULL; s = s->header.next)
2875 switch (s->header.type)
2877 case lang_output_section_statement_enum:
2879 bfd_vma after;
2880 lang_output_section_statement_type *os;
2882 os = &s->output_section_statement;
2883 if (os->bfd_section == NULL)
2884 /* This section was never actually created. */
2885 break;
2887 /* If this is a COFF shared library section, use the size and
2888 address from the input section. FIXME: This is COFF
2889 specific; it would be cleaner if there were some other way
2890 to do this, but nothing simple comes to mind. */
2891 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2893 asection *input;
2895 if (os->children.head == NULL
2896 || os->children.head->header.next != NULL
2897 || os->children.head->header.type != lang_input_section_enum)
2898 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2899 os->name);
2901 input = os->children.head->input_section.section;
2902 bfd_set_section_vma (os->bfd_section->owner,
2903 os->bfd_section,
2904 bfd_section_vma (input->owner, input));
2905 os->bfd_section->_raw_size = input->_raw_size;
2906 break;
2909 if (bfd_is_abs_section (os->bfd_section))
2911 /* No matter what happens, an abs section starts at zero. */
2912 ASSERT (os->bfd_section->vma == 0);
2914 else
2916 if (os->addr_tree == NULL)
2918 /* No address specified for this section, get one
2919 from the region specification. */
2920 if (os->region == NULL
2921 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2922 & (SEC_ALLOC | SEC_LOAD)) != 0)
2923 && os->region->name[0] == '*'
2924 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0))
2926 os->region = lang_memory_default (os->bfd_section);
2929 /* If a loadable section is using the default memory
2930 region, and some non default memory regions were
2931 defined, issue an error message. */
2932 if (!IGNORE_SECTION (output_bfd, os->bfd_section)
2933 && (bfd_get_section_flags (output_bfd, os->bfd_section)
2934 & SEC_NEVER_LOAD) == 0
2935 && ! link_info.relocatable
2936 && check_regions
2937 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0
2938 && lang_memory_region_list != NULL
2939 && (strcmp (lang_memory_region_list->name,
2940 DEFAULT_MEMORY_REGION) != 0
2941 || lang_memory_region_list->next != NULL))
2943 /* By default this is an error rather than just a
2944 warning because if we allocate the section to the
2945 default memory region we can end up creating an
2946 excessively large binary, or even seg faulting when
2947 attempting to perform a negative seek. See
2948 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
2949 for an example of this. This behaviour can be
2950 overridden by the using the --no-check-sections
2951 switch. */
2952 if (command_line.check_section_addresses)
2953 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2954 bfd_get_section_name (output_bfd,
2955 os->bfd_section));
2956 else
2957 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2958 bfd_get_section_name (output_bfd,
2959 os->bfd_section));
2962 dot = os->region->current;
2964 if (os->section_alignment == -1)
2966 bfd_vma olddot;
2968 olddot = dot;
2969 dot = align_power (dot,
2970 os->bfd_section->alignment_power);
2972 if (dot != olddot && config.warn_section_align)
2973 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2974 os->name, (unsigned int) (dot - olddot));
2977 else
2979 etree_value_type r;
2981 r = exp_fold_tree (os->addr_tree,
2982 abs_output_section,
2983 lang_allocating_phase_enum,
2984 dot, &dot);
2985 if (!r.valid_p)
2986 einfo (_("%F%S: non constant address expression for section %s\n"),
2987 os->name);
2989 dot = r.value + r.section->bfd_section->vma;
2992 /* The section starts here.
2993 First, align to what the section needs. */
2995 if (os->section_alignment != -1)
2996 dot = align_power (dot, os->section_alignment);
2998 bfd_set_section_vma (0, os->bfd_section, dot);
3000 os->bfd_section->output_offset = 0;
3003 lang_size_sections_1 (os->children.head, os, &os->children.head,
3004 os->fill, dot, relax, check_regions);
3006 /* Put the section within the requested block size, or
3007 align at the block boundary. */
3008 after = ((os->bfd_section->vma
3009 + TO_ADDR (os->bfd_section->_raw_size)
3010 + os->block_value - 1)
3011 & - (bfd_vma) os->block_value);
3013 if (bfd_is_abs_section (os->bfd_section))
3014 ASSERT (after == os->bfd_section->vma);
3015 else if ((os->bfd_section->flags & SEC_HAS_CONTENTS) == 0
3016 && (os->bfd_section->flags & SEC_THREAD_LOCAL)
3017 && ! link_info.relocatable)
3018 os->bfd_section->_raw_size = 0;
3019 else
3020 os->bfd_section->_raw_size
3021 = TO_SIZE (after - os->bfd_section->vma);
3023 dot = os->bfd_section->vma + TO_ADDR (os->bfd_section->_raw_size);
3024 os->processed = TRUE;
3026 if (os->update_dot_tree != 0)
3027 exp_fold_tree (os->update_dot_tree, abs_output_section,
3028 lang_allocating_phase_enum, dot, &dot);
3030 /* Update dot in the region ?
3031 We only do this if the section is going to be allocated,
3032 since unallocated sections do not contribute to the region's
3033 overall size in memory.
3035 If the SEC_NEVER_LOAD bit is not set, it will affect the
3036 addresses of sections after it. We have to update
3037 dot. */
3038 if (os->region != NULL
3039 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3040 & SEC_NEVER_LOAD) == 0
3041 || (bfd_get_section_flags (output_bfd, os->bfd_section)
3042 & (SEC_ALLOC | SEC_LOAD))))
3044 os->region->current = dot;
3046 if (check_regions)
3047 /* Make sure the new address is within the region. */
3048 os_region_check (os, os->region, os->addr_tree,
3049 os->bfd_section->vma);
3051 /* If there's no load address specified, use the run
3052 region as the load region. */
3053 if (os->lma_region == NULL && os->load_base == NULL)
3054 os->lma_region = os->region;
3056 if (os->lma_region != NULL && os->lma_region != os->region)
3058 /* Set load_base, which will be handled later. */
3059 os->load_base = exp_intop (os->lma_region->current);
3060 os->lma_region->current +=
3061 TO_ADDR (os->bfd_section->_raw_size);
3062 if (check_regions)
3063 os_region_check (os, os->lma_region, NULL,
3064 os->bfd_section->lma);
3068 break;
3070 case lang_constructors_statement_enum:
3071 dot = lang_size_sections_1 (constructor_list.head,
3072 output_section_statement,
3073 &s->wild_statement.children.head,
3074 fill, dot, relax, check_regions);
3075 break;
3077 case lang_data_statement_enum:
3079 unsigned int size = 0;
3081 s->data_statement.output_vma =
3082 dot - output_section_statement->bfd_section->vma;
3083 s->data_statement.output_section =
3084 output_section_statement->bfd_section;
3086 switch (s->data_statement.type)
3088 default:
3089 abort ();
3090 case QUAD:
3091 case SQUAD:
3092 size = QUAD_SIZE;
3093 break;
3094 case LONG:
3095 size = LONG_SIZE;
3096 break;
3097 case SHORT:
3098 size = SHORT_SIZE;
3099 break;
3100 case BYTE:
3101 size = BYTE_SIZE;
3102 break;
3104 if (size < TO_SIZE ((unsigned) 1))
3105 size = TO_SIZE ((unsigned) 1);
3106 dot += TO_ADDR (size);
3107 output_section_statement->bfd_section->_raw_size += size;
3108 /* The output section gets contents, and then we inspect for
3109 any flags set in the input script which override any ALLOC. */
3110 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3111 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3113 output_section_statement->bfd_section->flags |=
3114 SEC_ALLOC | SEC_LOAD;
3117 break;
3119 case lang_reloc_statement_enum:
3121 int size;
3123 s->reloc_statement.output_vma =
3124 dot - output_section_statement->bfd_section->vma;
3125 s->reloc_statement.output_section =
3126 output_section_statement->bfd_section;
3127 size = bfd_get_reloc_size (s->reloc_statement.howto);
3128 dot += TO_ADDR (size);
3129 output_section_statement->bfd_section->_raw_size += size;
3131 break;
3133 case lang_wild_statement_enum:
3135 dot = lang_size_sections_1 (s->wild_statement.children.head,
3136 output_section_statement,
3137 &s->wild_statement.children.head,
3138 fill, dot, relax, check_regions);
3140 break;
3142 case lang_object_symbols_statement_enum:
3143 link_info.create_object_symbols_section =
3144 output_section_statement->bfd_section;
3145 break;
3146 case lang_output_statement_enum:
3147 case lang_target_statement_enum:
3148 break;
3149 case lang_input_section_enum:
3151 asection *i;
3153 i = (*prev)->input_section.section;
3154 if (! relax)
3156 if (i->_cooked_size == 0)
3157 i->_cooked_size = i->_raw_size;
3159 else
3161 bfd_boolean again;
3163 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3164 einfo (_("%P%F: can't relax section: %E\n"));
3165 if (again)
3166 *relax = TRUE;
3168 dot = size_input_section (prev, output_section_statement,
3169 output_section_statement->fill, dot);
3171 break;
3172 case lang_input_statement_enum:
3173 break;
3174 case lang_fill_statement_enum:
3175 s->fill_statement.output_section =
3176 output_section_statement->bfd_section;
3178 fill = s->fill_statement.fill;
3179 break;
3180 case lang_assignment_statement_enum:
3182 bfd_vma newdot = dot;
3184 exp_fold_tree (s->assignment_statement.exp,
3185 output_section_statement,
3186 lang_allocating_phase_enum,
3187 dot,
3188 &newdot);
3190 if (newdot != dot)
3192 if (output_section_statement == abs_output_section)
3194 /* If we don't have an output section, then just adjust
3195 the default memory address. */
3196 lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE)->current = newdot;
3198 else
3200 /* Insert a pad after this statement. We can't
3201 put the pad before when relaxing, in case the
3202 assignment references dot. */
3203 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3204 output_section_statement->bfd_section, dot);
3206 /* Don't neuter the pad below when relaxing. */
3207 s = s->header.next;
3210 dot = newdot;
3213 break;
3215 case lang_padding_statement_enum:
3216 /* If this is the first time lang_size_sections is called,
3217 we won't have any padding statements. If this is the
3218 second or later passes when relaxing, we should allow
3219 padding to shrink. If padding is needed on this pass, it
3220 will be added back in. */
3221 s->padding_statement.size = 0;
3223 /* Make sure output_offset is valid. If relaxation shrinks
3224 the section and this pad isn't needed, it's possible to
3225 have output_offset larger than the final size of the
3226 section. bfd_set_section_contents will complain even for
3227 a pad size of zero. */
3228 s->padding_statement.output_offset
3229 = dot - output_section_statement->bfd_section->vma;
3230 break;
3232 case lang_group_statement_enum:
3233 dot = lang_size_sections_1 (s->group_statement.children.head,
3234 output_section_statement,
3235 &s->group_statement.children.head,
3236 fill, dot, relax, check_regions);
3237 break;
3239 default:
3240 FAIL ();
3241 break;
3243 /* We can only get here when relaxing is turned on. */
3244 case lang_address_statement_enum:
3245 break;
3247 prev = &s->header.next;
3249 return dot;
3252 bfd_vma
3253 lang_size_sections
3254 (lang_statement_union_type *s,
3255 lang_output_section_statement_type *output_section_statement,
3256 lang_statement_union_type **prev,
3257 fill_type *fill,
3258 bfd_vma dot,
3259 bfd_boolean *relax,
3260 bfd_boolean check_regions)
3262 bfd_vma result;
3263 asection *o;
3265 /* Callers of exp_fold_tree need to increment this. */
3266 lang_statement_iteration++;
3268 exp_data_seg.phase = exp_dataseg_none;
3269 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3270 dot, relax, check_regions);
3271 if (exp_data_seg.phase == exp_dataseg_end_seen)
3273 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3274 a page could be saved in the data segment. */
3275 bfd_vma first, last;
3277 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3278 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3279 if (first && last
3280 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3281 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3282 && first + last <= exp_data_seg.pagesize)
3284 exp_data_seg.phase = exp_dataseg_adjust;
3285 result = lang_size_sections_1 (s, output_section_statement, prev,
3286 fill, dot, relax, check_regions);
3290 /* Some backend relaxers want to refer to the output section size. Give
3291 them a section size that does not change on the next call while they
3292 relax. We can't set this at top because lang_reset_memory_regions
3293 which is called before we get here, sets _raw_size to 0 on relaxing
3294 rounds. */
3295 for (o = output_bfd->sections; o != NULL; o = o->next)
3296 o->_cooked_size = o->_raw_size;
3298 return result;
3301 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3303 static bfd_vma
3304 lang_do_assignments_1
3305 (lang_statement_union_type *s,
3306 lang_output_section_statement_type *output_section_statement,
3307 fill_type *fill,
3308 bfd_vma dot)
3310 for (; s != NULL; s = s->header.next)
3312 switch (s->header.type)
3314 case lang_constructors_statement_enum:
3315 dot = lang_do_assignments_1 (constructor_list.head,
3316 output_section_statement,
3317 fill,
3318 dot);
3319 break;
3321 case lang_output_section_statement_enum:
3323 lang_output_section_statement_type *os;
3325 os = &(s->output_section_statement);
3326 if (os->bfd_section != NULL)
3328 dot = os->bfd_section->vma;
3329 (void) lang_do_assignments_1 (os->children.head, os,
3330 os->fill, dot);
3331 dot = (os->bfd_section->vma
3332 + TO_ADDR (os->bfd_section->_raw_size));
3335 if (os->load_base)
3337 /* If nothing has been placed into the output section then
3338 it won't have a bfd_section. */
3339 if (os->bfd_section)
3341 os->bfd_section->lma
3342 = exp_get_abs_int (os->load_base, 0, "load base",
3343 lang_final_phase_enum);
3347 break;
3348 case lang_wild_statement_enum:
3350 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3351 output_section_statement,
3352 fill, dot);
3354 break;
3356 case lang_object_symbols_statement_enum:
3357 case lang_output_statement_enum:
3358 case lang_target_statement_enum:
3359 #if 0
3360 case lang_common_statement_enum:
3361 #endif
3362 break;
3363 case lang_data_statement_enum:
3365 etree_value_type value;
3367 value = exp_fold_tree (s->data_statement.exp,
3368 abs_output_section,
3369 lang_final_phase_enum, dot, &dot);
3370 s->data_statement.value = value.value;
3371 if (!value.valid_p)
3372 einfo (_("%F%P: invalid data statement\n"));
3375 unsigned int size;
3376 switch (s->data_statement.type)
3378 default:
3379 abort ();
3380 case QUAD:
3381 case SQUAD:
3382 size = QUAD_SIZE;
3383 break;
3384 case LONG:
3385 size = LONG_SIZE;
3386 break;
3387 case SHORT:
3388 size = SHORT_SIZE;
3389 break;
3390 case BYTE:
3391 size = BYTE_SIZE;
3392 break;
3394 if (size < TO_SIZE ((unsigned) 1))
3395 size = TO_SIZE ((unsigned) 1);
3396 dot += TO_ADDR (size);
3398 break;
3400 case lang_reloc_statement_enum:
3402 etree_value_type value;
3404 value = exp_fold_tree (s->reloc_statement.addend_exp,
3405 abs_output_section,
3406 lang_final_phase_enum, dot, &dot);
3407 s->reloc_statement.addend_value = value.value;
3408 if (!value.valid_p)
3409 einfo (_("%F%P: invalid reloc statement\n"));
3411 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3412 break;
3414 case lang_input_section_enum:
3416 asection *in = s->input_section.section;
3418 if (in->_cooked_size != 0)
3419 dot += TO_ADDR (in->_cooked_size);
3420 else
3421 dot += TO_ADDR (in->_raw_size);
3423 break;
3425 case lang_input_statement_enum:
3426 break;
3427 case lang_fill_statement_enum:
3428 fill = s->fill_statement.fill;
3429 break;
3430 case lang_assignment_statement_enum:
3432 exp_fold_tree (s->assignment_statement.exp,
3433 output_section_statement,
3434 lang_final_phase_enum,
3435 dot,
3436 &dot);
3439 break;
3440 case lang_padding_statement_enum:
3441 dot += TO_ADDR (s->padding_statement.size);
3442 break;
3444 case lang_group_statement_enum:
3445 dot = lang_do_assignments_1 (s->group_statement.children.head,
3446 output_section_statement,
3447 fill, dot);
3449 break;
3451 default:
3452 FAIL ();
3453 break;
3454 case lang_address_statement_enum:
3455 break;
3459 return dot;
3462 void
3463 lang_do_assignments (lang_statement_union_type *s,
3464 lang_output_section_statement_type *output_section_statement,
3465 fill_type *fill,
3466 bfd_vma dot)
3468 /* Callers of exp_fold_tree need to increment this. */
3469 lang_statement_iteration++;
3470 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3473 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3474 operator .startof. (section_name), it produces an undefined symbol
3475 .startof.section_name. Similarly, when it sees
3476 .sizeof. (section_name), it produces an undefined symbol
3477 .sizeof.section_name. For all the output sections, we look for
3478 such symbols, and set them to the correct value. */
3480 static void
3481 lang_set_startof (void)
3483 asection *s;
3485 if (link_info.relocatable)
3486 return;
3488 for (s = output_bfd->sections; s != NULL; s = s->next)
3490 const char *secname;
3491 char *buf;
3492 struct bfd_link_hash_entry *h;
3494 secname = bfd_get_section_name (output_bfd, s);
3495 buf = xmalloc (10 + strlen (secname));
3497 sprintf (buf, ".startof.%s", secname);
3498 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3499 if (h != NULL && h->type == bfd_link_hash_undefined)
3501 h->type = bfd_link_hash_defined;
3502 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3503 h->u.def.section = bfd_abs_section_ptr;
3506 sprintf (buf, ".sizeof.%s", secname);
3507 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3508 if (h != NULL && h->type == bfd_link_hash_undefined)
3510 h->type = bfd_link_hash_defined;
3511 if (s->_cooked_size != 0)
3512 h->u.def.value = TO_ADDR (s->_cooked_size);
3513 else
3514 h->u.def.value = TO_ADDR (s->_raw_size);
3515 h->u.def.section = bfd_abs_section_ptr;
3518 free (buf);
3522 static void
3523 lang_finish (void)
3525 struct bfd_link_hash_entry *h;
3526 bfd_boolean warn;
3528 if (link_info.relocatable || link_info.shared)
3529 warn = FALSE;
3530 else
3531 warn = TRUE;
3533 if (entry_symbol.name == NULL)
3535 /* No entry has been specified. Look for start, but don't warn
3536 if we don't find it. */
3537 entry_symbol.name = "start";
3538 warn = FALSE;
3541 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3542 FALSE, FALSE, TRUE);
3543 if (h != NULL
3544 && (h->type == bfd_link_hash_defined
3545 || h->type == bfd_link_hash_defweak)
3546 && h->u.def.section->output_section != NULL)
3548 bfd_vma val;
3550 val = (h->u.def.value
3551 + bfd_get_section_vma (output_bfd,
3552 h->u.def.section->output_section)
3553 + h->u.def.section->output_offset);
3554 if (! bfd_set_start_address (output_bfd, val))
3555 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3557 else
3559 bfd_vma val;
3560 const char *send;
3562 /* We couldn't find the entry symbol. Try parsing it as a
3563 number. */
3564 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3565 if (*send == '\0')
3567 if (! bfd_set_start_address (output_bfd, val))
3568 einfo (_("%P%F: can't set start address\n"));
3570 else
3572 asection *ts;
3574 /* Can't find the entry symbol, and it's not a number. Use
3575 the first address in the text section. */
3576 ts = bfd_get_section_by_name (output_bfd, entry_section);
3577 if (ts != NULL)
3579 if (warn)
3580 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3581 entry_symbol.name,
3582 bfd_get_section_vma (output_bfd, ts));
3583 if (! bfd_set_start_address (output_bfd,
3584 bfd_get_section_vma (output_bfd,
3585 ts)))
3586 einfo (_("%P%F: can't set start address\n"));
3588 else
3590 if (warn)
3591 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3592 entry_symbol.name);
3597 bfd_hash_table_free (&lang_definedness_table);
3600 /* This is a small function used when we want to ignore errors from
3601 BFD. */
3603 static void
3604 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3606 /* Don't do anything. */
3609 /* Check that the architecture of all the input files is compatible
3610 with the output file. Also call the backend to let it do any
3611 other checking that is needed. */
3613 static void
3614 lang_check (void)
3616 lang_statement_union_type *file;
3617 bfd *input_bfd;
3618 const bfd_arch_info_type *compatible;
3620 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3622 input_bfd = file->input_statement.the_bfd;
3623 compatible = bfd_arch_get_compatible (input_bfd, output_bfd,
3624 command_line.accept_unknown_input_arch);
3626 /* In general it is not possible to perform a relocatable
3627 link between differing object formats when the input
3628 file has relocations, because the relocations in the
3629 input format may not have equivalent representations in
3630 the output format (and besides BFD does not translate
3631 relocs for other link purposes than a final link). */
3632 if ((link_info.relocatable || link_info.emitrelocations)
3633 && (compatible == NULL
3634 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3635 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3637 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3638 bfd_get_target (input_bfd), input_bfd,
3639 bfd_get_target (output_bfd), output_bfd);
3640 /* einfo with %F exits. */
3643 if (compatible == NULL)
3645 if (command_line.warn_mismatch)
3646 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3647 bfd_printable_name (input_bfd), input_bfd,
3648 bfd_printable_name (output_bfd));
3650 else if (bfd_count_sections (input_bfd))
3652 /* If the input bfd has no contents, it shouldn't set the
3653 private data of the output bfd. */
3655 bfd_error_handler_type pfn = NULL;
3657 /* If we aren't supposed to warn about mismatched input
3658 files, temporarily set the BFD error handler to a
3659 function which will do nothing. We still want to call
3660 bfd_merge_private_bfd_data, since it may set up
3661 information which is needed in the output file. */
3662 if (! command_line.warn_mismatch)
3663 pfn = bfd_set_error_handler (ignore_bfd_errors);
3664 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3666 if (command_line.warn_mismatch)
3667 einfo (_("%E%X: failed to merge target specific data of file %B\n"),
3668 input_bfd);
3670 if (! command_line.warn_mismatch)
3671 bfd_set_error_handler (pfn);
3676 /* Look through all the global common symbols and attach them to the
3677 correct section. The -sort-common command line switch may be used
3678 to roughly sort the entries by size. */
3680 static void
3681 lang_common (void)
3683 if (command_line.inhibit_common_definition)
3684 return;
3685 if (link_info.relocatable
3686 && ! command_line.force_common_definition)
3687 return;
3689 if (! config.sort_common)
3690 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3691 else
3693 int power;
3695 for (power = 4; power >= 0; power--)
3696 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3700 /* Place one common symbol in the correct section. */
3702 static bfd_boolean
3703 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3705 unsigned int power_of_two;
3706 bfd_vma size;
3707 asection *section;
3709 if (h->type != bfd_link_hash_common)
3710 return TRUE;
3712 size = h->u.c.size;
3713 power_of_two = h->u.c.p->alignment_power;
3715 if (config.sort_common
3716 && power_of_two < (unsigned int) *(int *) info)
3717 return TRUE;
3719 section = h->u.c.p->section;
3721 /* Increase the size of the section to align the common sym. */
3722 section->_cooked_size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3723 section->_cooked_size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3725 /* Adjust the alignment if necessary. */
3726 if (power_of_two > section->alignment_power)
3727 section->alignment_power = power_of_two;
3729 /* Change the symbol from common to defined. */
3730 h->type = bfd_link_hash_defined;
3731 h->u.def.section = section;
3732 h->u.def.value = section->_cooked_size;
3734 /* Increase the size of the section. */
3735 section->_cooked_size += size;
3737 /* Make sure the section is allocated in memory, and make sure that
3738 it is no longer a common section. */
3739 section->flags |= SEC_ALLOC;
3740 section->flags &= ~SEC_IS_COMMON;
3742 if (config.map_file != NULL)
3744 static bfd_boolean header_printed;
3745 int len;
3746 char *name;
3747 char buf[50];
3749 if (! header_printed)
3751 minfo (_("\nAllocating common symbols\n"));
3752 minfo (_("Common symbol size file\n\n"));
3753 header_printed = TRUE;
3756 name = demangle (h->root.string);
3757 minfo ("%s", name);
3758 len = strlen (name);
3759 free (name);
3761 if (len >= 19)
3763 print_nl ();
3764 len = 0;
3766 while (len < 20)
3768 print_space ();
3769 ++len;
3772 minfo ("0x");
3773 if (size <= 0xffffffff)
3774 sprintf (buf, "%lx", (unsigned long) size);
3775 else
3776 sprintf_vma (buf, size);
3777 minfo ("%s", buf);
3778 len = strlen (buf);
3780 while (len < 16)
3782 print_space ();
3783 ++len;
3786 minfo ("%B\n", section->owner);
3789 return TRUE;
3792 /* Run through the input files and ensure that every input section has
3793 somewhere to go. If one is found without a destination then create
3794 an input request and place it into the statement tree. */
3796 static void
3797 lang_place_orphans (void)
3799 LANG_FOR_EACH_INPUT_STATEMENT (file)
3801 asection *s;
3803 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3805 if (s->output_section == NULL)
3807 /* This section of the file is not attached, root
3808 around for a sensible place for it to go. */
3810 if (file->just_syms_flag)
3812 abort ();
3814 else if (strcmp (s->name, "COMMON") == 0)
3816 /* This is a lonely common section which must have
3817 come from an archive. We attach to the section
3818 with the wildcard. */
3819 if (! link_info.relocatable
3820 || command_line.force_common_definition)
3822 if (default_common_section == NULL)
3824 #if 0
3825 /* This message happens when using the
3826 svr3.ifile linker script, so I have
3827 disabled it. */
3828 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3829 #endif
3830 default_common_section =
3831 lang_output_section_statement_lookup (".bss");
3834 lang_add_section (&default_common_section->children, s,
3835 default_common_section, file);
3838 else if (ldemul_place_orphan (file, s))
3840 else
3842 lang_output_section_statement_type *os;
3844 os = lang_output_section_statement_lookup (s->name);
3845 lang_add_section (&os->children, s, os, file);
3852 void
3853 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
3855 flagword *ptr_flags;
3857 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3858 while (*flags)
3860 switch (*flags)
3862 case 'A': case 'a':
3863 *ptr_flags |= SEC_ALLOC;
3864 break;
3866 case 'R': case 'r':
3867 *ptr_flags |= SEC_READONLY;
3868 break;
3870 case 'W': case 'w':
3871 *ptr_flags |= SEC_DATA;
3872 break;
3874 case 'X': case 'x':
3875 *ptr_flags |= SEC_CODE;
3876 break;
3878 case 'L': case 'l':
3879 case 'I': case 'i':
3880 *ptr_flags |= SEC_LOAD;
3881 break;
3883 default:
3884 einfo (_("%P%F: invalid syntax in flags\n"));
3885 break;
3887 flags++;
3891 /* Call a function on each input file. This function will be called
3892 on an archive, but not on the elements. */
3894 void
3895 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
3897 lang_input_statement_type *f;
3899 for (f = (lang_input_statement_type *) input_file_chain.head;
3900 f != NULL;
3901 f = (lang_input_statement_type *) f->next_real_file)
3902 func (f);
3905 /* Call a function on each file. The function will be called on all
3906 the elements of an archive which are included in the link, but will
3907 not be called on the archive file itself. */
3909 void
3910 lang_for_each_file (void (*func) (lang_input_statement_type *))
3912 LANG_FOR_EACH_INPUT_STATEMENT (f)
3914 func (f);
3918 #if 0
3920 /* Not used. */
3922 void
3923 lang_for_each_input_section (void (*func) (bfd *ab, asection *as))
3925 LANG_FOR_EACH_INPUT_STATEMENT (f)
3927 asection *s;
3929 for (s = f->the_bfd->sections; s != NULL; s = s->next)
3930 func (f->the_bfd, s);
3934 #endif
3936 void
3937 ldlang_add_file (lang_input_statement_type *entry)
3939 bfd **pp;
3941 lang_statement_append (&file_chain,
3942 (lang_statement_union_type *) entry,
3943 &entry->next);
3945 /* The BFD linker needs to have a list of all input BFDs involved in
3946 a link. */
3947 ASSERT (entry->the_bfd->link_next == NULL);
3948 ASSERT (entry->the_bfd != output_bfd);
3949 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
3951 *pp = entry->the_bfd;
3952 entry->the_bfd->usrdata = entry;
3953 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3955 /* Look through the sections and check for any which should not be
3956 included in the link. We need to do this now, so that we can
3957 notice when the backend linker tries to report multiple
3958 definition errors for symbols which are in sections we aren't
3959 going to link. FIXME: It might be better to entirely ignore
3960 symbols which are defined in sections which are going to be
3961 discarded. This would require modifying the backend linker for
3962 each backend which might set the SEC_LINK_ONCE flag. If we do
3963 this, we should probably handle SEC_EXCLUDE in the same way. */
3965 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
3968 void
3969 lang_add_output (const char *name, int from_script)
3971 /* Make -o on command line override OUTPUT in script. */
3972 if (!had_output_filename || !from_script)
3974 output_filename = name;
3975 had_output_filename = TRUE;
3979 static lang_output_section_statement_type *current_section;
3981 static int
3982 topower (int x)
3984 unsigned int i = 1;
3985 int l;
3987 if (x < 0)
3988 return -1;
3990 for (l = 0; l < 32; l++)
3992 if (i >= (unsigned int) x)
3993 return l;
3994 i <<= 1;
3997 return 0;
4000 lang_output_section_statement_type *
4001 lang_enter_output_section_statement (const char *output_section_statement_name,
4002 etree_type *address_exp,
4003 enum section_type sectype,
4004 etree_type *align,
4005 etree_type *subalign,
4006 etree_type *ebase)
4008 lang_output_section_statement_type *os;
4010 current_section =
4011 os =
4012 lang_output_section_statement_lookup (output_section_statement_name);
4014 /* Add this statement to tree. */
4015 #if 0
4016 add_statement (lang_output_section_statement_enum,
4017 output_section_statement);
4018 #endif
4019 /* Make next things chain into subchain of this. */
4021 if (os->addr_tree == NULL)
4023 os->addr_tree = address_exp;
4025 os->sectype = sectype;
4026 if (sectype != noload_section)
4027 os->flags = SEC_NO_FLAGS;
4028 else
4029 os->flags = SEC_NEVER_LOAD;
4030 os->block_value = 1;
4031 stat_ptr = &os->children;
4033 os->subsection_alignment =
4034 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4035 os->section_alignment =
4036 topower (exp_get_value_int (align, -1, "section alignment", 0));
4038 os->load_base = ebase;
4039 return os;
4042 void
4043 lang_final (void)
4045 lang_output_statement_type *new =
4046 new_stat (lang_output_statement, stat_ptr);
4048 new->name = output_filename;
4051 /* Reset the current counters in the regions. */
4053 void
4054 lang_reset_memory_regions (void)
4056 lang_memory_region_type *p = lang_memory_region_list;
4057 asection *o;
4059 for (p = lang_memory_region_list; p != NULL; p = p->next)
4061 p->old_length = (bfd_size_type) (p->current - p->origin);
4062 p->current = p->origin;
4065 for (o = output_bfd->sections; o != NULL; o = o->next)
4066 o->_raw_size = 0;
4069 /* If the wild pattern was marked KEEP, the member sections
4070 should be as well. */
4072 static void
4073 gc_section_callback (lang_wild_statement_type *ptr,
4074 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4075 asection *section,
4076 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4077 void *data ATTRIBUTE_UNUSED)
4079 if (ptr->keep_sections)
4080 section->flags |= SEC_KEEP;
4083 /* Handle a wild statement, marking it against GC. */
4085 static void
4086 lang_gc_wild (lang_wild_statement_type *s)
4088 walk_wild (s, gc_section_callback, NULL);
4091 /* Iterate over sections marking them against GC. */
4093 static void
4094 lang_gc_sections_1 (lang_statement_union_type *s)
4096 for (; s != NULL; s = s->header.next)
4098 switch (s->header.type)
4100 case lang_wild_statement_enum:
4101 lang_gc_wild (&s->wild_statement);
4102 break;
4103 case lang_constructors_statement_enum:
4104 lang_gc_sections_1 (constructor_list.head);
4105 break;
4106 case lang_output_section_statement_enum:
4107 lang_gc_sections_1 (s->output_section_statement.children.head);
4108 break;
4109 case lang_group_statement_enum:
4110 lang_gc_sections_1 (s->group_statement.children.head);
4111 break;
4112 default:
4113 break;
4118 static void
4119 lang_gc_sections (void)
4121 struct bfd_link_hash_entry *h;
4122 ldlang_undef_chain_list_type *ulist;
4124 /* Keep all sections so marked in the link script. */
4126 lang_gc_sections_1 (statement_list.head);
4128 /* Keep all sections containing symbols undefined on the command-line,
4129 and the section containing the entry symbol. */
4131 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4133 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4134 FALSE, FALSE, FALSE);
4136 if (h != NULL
4137 && (h->type == bfd_link_hash_defined
4138 || h->type == bfd_link_hash_defweak)
4139 && ! bfd_is_abs_section (h->u.def.section))
4141 h->u.def.section->flags |= SEC_KEEP;
4145 bfd_gc_sections (output_bfd, &link_info);
4148 void
4149 lang_process (void)
4151 lang_reasonable_defaults ();
4152 current_target = default_target;
4154 /* Open the output file. */
4155 lang_for_each_statement (ldlang_open_output);
4156 init_opb ();
4158 ldemul_create_output_section_statements ();
4160 /* Add to the hash table all undefineds on the command line. */
4161 lang_place_undefineds ();
4163 already_linked_table_init ();
4165 /* Create a bfd for each input file. */
4166 current_target = default_target;
4167 open_input_bfds (statement_list.head, FALSE);
4169 link_info.gc_sym_list = &entry_symbol;
4170 if (entry_symbol.name == NULL)
4171 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4173 ldemul_after_open ();
4175 already_linked_table_free ();
4177 /* Make sure that we're not mixing architectures. We call this
4178 after all the input files have been opened, but before we do any
4179 other processing, so that any operations merge_private_bfd_data
4180 does on the output file will be known during the rest of the
4181 link. */
4182 lang_check ();
4184 /* Handle .exports instead of a version script if we're told to do so. */
4185 if (command_line.version_exports_section)
4186 lang_do_version_exports_section ();
4188 /* Build all sets based on the information gathered from the input
4189 files. */
4190 ldctor_build_sets ();
4192 /* Remove unreferenced sections if asked to. */
4193 if (command_line.gc_sections)
4194 lang_gc_sections ();
4196 /* If there were any SEC_MERGE sections, finish their merging, so that
4197 section sizes can be computed. This has to be done after GC of sections,
4198 so that GCed sections are not merged, but before assigning output
4199 sections, since removing whole input sections is hard then. */
4200 bfd_merge_sections (output_bfd, &link_info);
4202 /* Size up the common data. */
4203 lang_common ();
4205 /* Run through the contours of the script and attach input sections
4206 to the correct output sections. */
4207 map_input_to_output_sections (statement_list.head, NULL, NULL);
4209 /* Find any sections not attached explicitly and handle them. */
4210 lang_place_orphans ();
4212 if (! link_info.relocatable)
4214 /* Look for a text section and set the readonly attribute in it. */
4215 asection *found = bfd_get_section_by_name (output_bfd, ".text");
4217 if (found != NULL)
4219 if (config.text_read_only)
4220 found->flags |= SEC_READONLY;
4221 else
4222 found->flags &= ~SEC_READONLY;
4226 /* Do anything special before sizing sections. This is where ELF
4227 and other back-ends size dynamic sections. */
4228 ldemul_before_allocation ();
4230 if (!link_info.relocatable)
4231 strip_excluded_output_sections ();
4233 /* We must record the program headers before we try to fix the
4234 section positions, since they will affect SIZEOF_HEADERS. */
4235 lang_record_phdrs ();
4237 /* Size up the sections. */
4238 lang_size_sections (statement_list.head, abs_output_section,
4239 &statement_list.head, 0, 0, NULL,
4240 command_line.relax ? FALSE : TRUE);
4242 /* Now run around and relax if we can. */
4243 if (command_line.relax)
4245 /* Keep relaxing until bfd_relax_section gives up. */
4246 bfd_boolean relax_again;
4250 relax_again = FALSE;
4252 /* Note: pe-dll.c does something like this also. If you find
4253 you need to change this code, you probably need to change
4254 pe-dll.c also. DJ */
4256 /* Do all the assignments with our current guesses as to
4257 section sizes. */
4258 lang_do_assignments (statement_list.head, abs_output_section,
4259 NULL, 0);
4261 /* We must do this after lang_do_assignments, because it uses
4262 _raw_size. */
4263 lang_reset_memory_regions ();
4265 /* Perform another relax pass - this time we know where the
4266 globals are, so can make a better guess. */
4267 lang_size_sections (statement_list.head, abs_output_section,
4268 &statement_list.head, 0, 0, &relax_again, FALSE);
4270 /* If the normal relax is done and the relax finalize pass
4271 is not performed yet, we perform another relax pass. */
4272 if (!relax_again && link_info.need_relax_finalize)
4274 link_info.need_relax_finalize = FALSE;
4275 relax_again = TRUE;
4278 while (relax_again);
4280 /* Final extra sizing to report errors. */
4281 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4282 lang_reset_memory_regions ();
4283 lang_size_sections (statement_list.head, abs_output_section,
4284 &statement_list.head, 0, 0, NULL, TRUE);
4287 /* See if anything special should be done now we know how big
4288 everything is. */
4289 ldemul_after_allocation ();
4291 /* Fix any .startof. or .sizeof. symbols. */
4292 lang_set_startof ();
4294 /* Do all the assignments, now that we know the final resting places
4295 of all the symbols. */
4297 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4299 /* Make sure that the section addresses make sense. */
4300 if (! link_info.relocatable
4301 && command_line.check_section_addresses)
4302 lang_check_section_addresses ();
4304 /* Final stuffs. */
4306 ldemul_finish ();
4307 lang_finish ();
4310 /* EXPORTED TO YACC */
4312 void
4313 lang_add_wild (struct wildcard_spec *filespec,
4314 struct wildcard_list *section_list,
4315 bfd_boolean keep_sections)
4317 struct wildcard_list *curr, *next;
4318 lang_wild_statement_type *new;
4320 /* Reverse the list as the parser puts it back to front. */
4321 for (curr = section_list, section_list = NULL;
4322 curr != NULL;
4323 section_list = curr, curr = next)
4325 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4326 placed_commons = TRUE;
4328 next = curr->next;
4329 curr->next = section_list;
4332 if (filespec != NULL && filespec->name != NULL)
4334 if (strcmp (filespec->name, "*") == 0)
4335 filespec->name = NULL;
4336 else if (! wildcardp (filespec->name))
4337 lang_has_input_file = TRUE;
4340 new = new_stat (lang_wild_statement, stat_ptr);
4341 new->filename = NULL;
4342 new->filenames_sorted = FALSE;
4343 if (filespec != NULL)
4345 new->filename = filespec->name;
4346 new->filenames_sorted = filespec->sorted;
4348 new->section_list = section_list;
4349 new->keep_sections = keep_sections;
4350 lang_list_init (&new->children);
4353 void
4354 lang_section_start (const char *name, etree_type *address)
4356 lang_address_statement_type *ad;
4358 ad = new_stat (lang_address_statement, stat_ptr);
4359 ad->section_name = name;
4360 ad->address = address;
4363 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4364 because of a -e argument on the command line, or zero if this is
4365 called by ENTRY in a linker script. Command line arguments take
4366 precedence. */
4368 void
4369 lang_add_entry (const char *name, bfd_boolean cmdline)
4371 if (entry_symbol.name == NULL
4372 || cmdline
4373 || ! entry_from_cmdline)
4375 entry_symbol.name = name;
4376 entry_from_cmdline = cmdline;
4380 void
4381 lang_add_target (const char *name)
4383 lang_target_statement_type *new = new_stat (lang_target_statement,
4384 stat_ptr);
4386 new->target = name;
4390 void
4391 lang_add_map (const char *name)
4393 while (*name)
4395 switch (*name)
4397 case 'F':
4398 map_option_f = TRUE;
4399 break;
4401 name++;
4405 void
4406 lang_add_fill (fill_type *fill)
4408 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4409 stat_ptr);
4411 new->fill = fill;
4414 void
4415 lang_add_data (int type, union etree_union *exp)
4418 lang_data_statement_type *new = new_stat (lang_data_statement,
4419 stat_ptr);
4421 new->exp = exp;
4422 new->type = type;
4426 /* Create a new reloc statement. RELOC is the BFD relocation type to
4427 generate. HOWTO is the corresponding howto structure (we could
4428 look this up, but the caller has already done so). SECTION is the
4429 section to generate a reloc against, or NAME is the name of the
4430 symbol to generate a reloc against. Exactly one of SECTION and
4431 NAME must be NULL. ADDEND is an expression for the addend. */
4433 void
4434 lang_add_reloc (bfd_reloc_code_real_type reloc,
4435 reloc_howto_type *howto,
4436 asection *section,
4437 const char *name,
4438 union etree_union *addend)
4440 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4442 p->reloc = reloc;
4443 p->howto = howto;
4444 p->section = section;
4445 p->name = name;
4446 p->addend_exp = addend;
4448 p->addend_value = 0;
4449 p->output_section = NULL;
4450 p->output_vma = 0;
4453 lang_assignment_statement_type *
4454 lang_add_assignment (etree_type *exp)
4456 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4457 stat_ptr);
4459 new->exp = exp;
4460 return new;
4463 void
4464 lang_add_attribute (enum statement_enum attribute)
4466 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4469 void
4470 lang_startup (const char *name)
4472 if (startup_file != NULL)
4474 einfo (_("%P%Fmultiple STARTUP files\n"));
4476 first_file->filename = name;
4477 first_file->local_sym_name = name;
4478 first_file->real = TRUE;
4480 startup_file = name;
4483 void
4484 lang_float (bfd_boolean maybe)
4486 lang_float_flag = maybe;
4490 /* Work out the load- and run-time regions from a script statement, and
4491 store them in *LMA_REGION and *REGION respectively.
4493 MEMSPEC is the name of the run-time region, or the value of
4494 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4495 LMA_MEMSPEC is the name of the load-time region, or null if the
4496 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4497 had an explicit load address.
4499 It is an error to specify both a load region and a load address. */
4501 static void
4502 lang_get_regions (struct memory_region_struct **region,
4503 struct memory_region_struct **lma_region,
4504 const char *memspec,
4505 const char *lma_memspec,
4506 int have_lma_p)
4508 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4510 /* If no runtime region has been given, but the load region has
4511 been, use the load region. */
4512 if (lma_memspec != 0 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4513 *region = *lma_region;
4514 else
4515 *region = lang_memory_region_lookup (memspec, FALSE);
4517 if (have_lma_p && lma_memspec != 0)
4518 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4521 void
4522 lang_leave_output_section_statement
4523 (fill_type *fill, const char *memspec,
4524 struct lang_output_section_phdr_list *phdrs, const char *lma_memspec)
4526 lang_get_regions (&current_section->region,
4527 &current_section->lma_region,
4528 memspec, lma_memspec,
4529 current_section->load_base != 0);
4530 current_section->fill = fill;
4531 current_section->phdrs = phdrs;
4532 stat_ptr = &statement_list;
4535 /* Create an absolute symbol with the given name with the value of the
4536 address of first byte of the section named.
4538 If the symbol already exists, then do nothing. */
4540 void
4541 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4543 struct bfd_link_hash_entry *h;
4545 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4546 if (h == NULL)
4547 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4549 if (h->type == bfd_link_hash_new
4550 || h->type == bfd_link_hash_undefined)
4552 asection *sec;
4554 h->type = bfd_link_hash_defined;
4556 sec = bfd_get_section_by_name (output_bfd, secname);
4557 if (sec == NULL)
4558 h->u.def.value = 0;
4559 else
4560 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4562 h->u.def.section = bfd_abs_section_ptr;
4566 /* Create an absolute symbol with the given name with the value of the
4567 address of the first byte after the end of the section named.
4569 If the symbol already exists, then do nothing. */
4571 void
4572 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4574 struct bfd_link_hash_entry *h;
4576 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4577 if (h == NULL)
4578 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4580 if (h->type == bfd_link_hash_new
4581 || h->type == bfd_link_hash_undefined)
4583 asection *sec;
4585 h->type = bfd_link_hash_defined;
4587 sec = bfd_get_section_by_name (output_bfd, secname);
4588 if (sec == NULL)
4589 h->u.def.value = 0;
4590 else
4591 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4592 + TO_ADDR (bfd_section_size (output_bfd, sec)));
4594 h->u.def.section = bfd_abs_section_ptr;
4598 void
4599 lang_statement_append (lang_statement_list_type *list,
4600 lang_statement_union_type *element,
4601 lang_statement_union_type **field)
4603 *(list->tail) = element;
4604 list->tail = field;
4607 /* Set the output format type. -oformat overrides scripts. */
4609 void
4610 lang_add_output_format (const char *format,
4611 const char *big,
4612 const char *little,
4613 int from_script)
4615 if (output_target == NULL || !from_script)
4617 if (command_line.endian == ENDIAN_BIG
4618 && big != NULL)
4619 format = big;
4620 else if (command_line.endian == ENDIAN_LITTLE
4621 && little != NULL)
4622 format = little;
4624 output_target = format;
4628 /* Enter a group. This creates a new lang_group_statement, and sets
4629 stat_ptr to build new statements within the group. */
4631 void
4632 lang_enter_group (void)
4634 lang_group_statement_type *g;
4636 g = new_stat (lang_group_statement, stat_ptr);
4637 lang_list_init (&g->children);
4638 stat_ptr = &g->children;
4641 /* Leave a group. This just resets stat_ptr to start writing to the
4642 regular list of statements again. Note that this will not work if
4643 groups can occur inside anything else which can adjust stat_ptr,
4644 but currently they can't. */
4646 void
4647 lang_leave_group (void)
4649 stat_ptr = &statement_list;
4652 /* Add a new program header. This is called for each entry in a PHDRS
4653 command in a linker script. */
4655 void
4656 lang_new_phdr (const char *name,
4657 etree_type *type,
4658 bfd_boolean filehdr,
4659 bfd_boolean phdrs,
4660 etree_type *at,
4661 etree_type *flags)
4663 struct lang_phdr *n, **pp;
4665 n = stat_alloc (sizeof (struct lang_phdr));
4666 n->next = NULL;
4667 n->name = name;
4668 n->type = exp_get_value_int (type, 0, "program header type",
4669 lang_final_phase_enum);
4670 n->filehdr = filehdr;
4671 n->phdrs = phdrs;
4672 n->at = at;
4673 n->flags = flags;
4675 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4677 *pp = n;
4680 /* Record the program header information in the output BFD. FIXME: We
4681 should not be calling an ELF specific function here. */
4683 static void
4684 lang_record_phdrs (void)
4686 unsigned int alc;
4687 asection **secs;
4688 struct lang_output_section_phdr_list *last;
4689 struct lang_phdr *l;
4690 lang_statement_union_type *u;
4692 alc = 10;
4693 secs = xmalloc (alc * sizeof (asection *));
4694 last = NULL;
4695 for (l = lang_phdr_list; l != NULL; l = l->next)
4697 unsigned int c;
4698 flagword flags;
4699 bfd_vma at;
4701 c = 0;
4702 for (u = lang_output_section_statement.head;
4703 u != NULL;
4704 u = u->output_section_statement.next)
4706 lang_output_section_statement_type *os;
4707 struct lang_output_section_phdr_list *pl;
4709 os = &u->output_section_statement;
4711 pl = os->phdrs;
4712 if (pl != NULL)
4713 last = pl;
4714 else
4716 if (os->sectype == noload_section
4717 || os->bfd_section == NULL
4718 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4719 continue;
4720 pl = last;
4723 if (os->bfd_section == NULL)
4724 continue;
4726 for (; pl != NULL; pl = pl->next)
4728 if (strcmp (pl->name, l->name) == 0)
4730 if (c >= alc)
4732 alc *= 2;
4733 secs = xrealloc (secs, alc * sizeof (asection *));
4735 secs[c] = os->bfd_section;
4736 ++c;
4737 pl->used = TRUE;
4742 if (l->flags == NULL)
4743 flags = 0;
4744 else
4745 flags = exp_get_vma (l->flags, 0, "phdr flags",
4746 lang_final_phase_enum);
4748 if (l->at == NULL)
4749 at = 0;
4750 else
4751 at = exp_get_vma (l->at, 0, "phdr load address",
4752 lang_final_phase_enum);
4754 if (! bfd_record_phdr (output_bfd, l->type,
4755 l->flags != NULL, flags, l->at != NULL,
4756 at, l->filehdr, l->phdrs, c, secs))
4757 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4760 free (secs);
4762 /* Make sure all the phdr assignments succeeded. */
4763 for (u = lang_output_section_statement.head;
4764 u != NULL;
4765 u = u->output_section_statement.next)
4767 struct lang_output_section_phdr_list *pl;
4769 if (u->output_section_statement.bfd_section == NULL)
4770 continue;
4772 for (pl = u->output_section_statement.phdrs;
4773 pl != NULL;
4774 pl = pl->next)
4775 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4776 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4777 u->output_section_statement.name, pl->name);
4781 /* Record a list of sections which may not be cross referenced. */
4783 void
4784 lang_add_nocrossref (struct lang_nocrossref *l)
4786 struct lang_nocrossrefs *n;
4788 n = xmalloc (sizeof *n);
4789 n->next = nocrossref_list;
4790 n->list = l;
4791 nocrossref_list = n;
4793 /* Set notice_all so that we get informed about all symbols. */
4794 link_info.notice_all = TRUE;
4797 /* Overlay handling. We handle overlays with some static variables. */
4799 /* The overlay virtual address. */
4800 static etree_type *overlay_vma;
4801 /* And subsection alignment. */
4802 static etree_type *overlay_subalign;
4804 /* An expression for the maximum section size seen so far. */
4805 static etree_type *overlay_max;
4807 /* A list of all the sections in this overlay. */
4809 struct overlay_list {
4810 struct overlay_list *next;
4811 lang_output_section_statement_type *os;
4814 static struct overlay_list *overlay_list;
4816 /* Start handling an overlay. */
4818 void
4819 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
4821 /* The grammar should prevent nested overlays from occurring. */
4822 ASSERT (overlay_vma == NULL
4823 && overlay_subalign == NULL
4824 && overlay_max == NULL);
4826 overlay_vma = vma_expr;
4827 overlay_subalign = subalign;
4830 /* Start a section in an overlay. We handle this by calling
4831 lang_enter_output_section_statement with the correct VMA.
4832 lang_leave_overlay sets up the LMA and memory regions. */
4834 void
4835 lang_enter_overlay_section (const char *name)
4837 struct overlay_list *n;
4838 etree_type *size;
4840 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4841 0, overlay_subalign, 0);
4843 /* If this is the first section, then base the VMA of future
4844 sections on this one. This will work correctly even if `.' is
4845 used in the addresses. */
4846 if (overlay_list == NULL)
4847 overlay_vma = exp_nameop (ADDR, name);
4849 /* Remember the section. */
4850 n = xmalloc (sizeof *n);
4851 n->os = current_section;
4852 n->next = overlay_list;
4853 overlay_list = n;
4855 size = exp_nameop (SIZEOF, name);
4857 /* Arrange to work out the maximum section end address. */
4858 if (overlay_max == NULL)
4859 overlay_max = size;
4860 else
4861 overlay_max = exp_binop (MAX_K, overlay_max, size);
4864 /* Finish a section in an overlay. There isn't any special to do
4865 here. */
4867 void
4868 lang_leave_overlay_section (fill_type *fill,
4869 struct lang_output_section_phdr_list *phdrs)
4871 const char *name;
4872 char *clean, *s2;
4873 const char *s1;
4874 char *buf;
4876 name = current_section->name;
4878 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4879 region and that no load-time region has been specified. It doesn't
4880 really matter what we say here, since lang_leave_overlay will
4881 override it. */
4882 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
4884 /* Define the magic symbols. */
4886 clean = xmalloc (strlen (name) + 1);
4887 s2 = clean;
4888 for (s1 = name; *s1 != '\0'; s1++)
4889 if (ISALNUM (*s1) || *s1 == '_')
4890 *s2++ = *s1;
4891 *s2 = '\0';
4893 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4894 sprintf (buf, "__load_start_%s", clean);
4895 lang_add_assignment (exp_assop ('=', buf,
4896 exp_nameop (LOADADDR, name)));
4898 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4899 sprintf (buf, "__load_stop_%s", clean);
4900 lang_add_assignment (exp_assop ('=', buf,
4901 exp_binop ('+',
4902 exp_nameop (LOADADDR, name),
4903 exp_nameop (SIZEOF, name))));
4905 free (clean);
4908 /* Finish an overlay. If there are any overlay wide settings, this
4909 looks through all the sections in the overlay and sets them. */
4911 void
4912 lang_leave_overlay (etree_type *lma_expr,
4913 int nocrossrefs,
4914 fill_type *fill,
4915 const char *memspec,
4916 struct lang_output_section_phdr_list *phdrs,
4917 const char *lma_memspec)
4919 lang_memory_region_type *region;
4920 lang_memory_region_type *lma_region;
4921 struct overlay_list *l;
4922 struct lang_nocrossref *nocrossref;
4924 lang_get_regions (&region, &lma_region,
4925 memspec, lma_memspec,
4926 lma_expr != 0);
4928 nocrossref = NULL;
4930 /* After setting the size of the last section, set '.' to end of the
4931 overlay region. */
4932 if (overlay_list != NULL)
4933 overlay_list->os->update_dot_tree
4934 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
4936 l = overlay_list;
4937 while (l != NULL)
4939 struct overlay_list *next;
4941 if (fill != NULL && l->os->fill == NULL)
4942 l->os->fill = fill;
4944 l->os->region = region;
4945 l->os->lma_region = lma_region;
4947 /* The first section has the load address specified in the
4948 OVERLAY statement. The rest are worked out from that.
4949 The base address is not needed (and should be null) if
4950 an LMA region was specified. */
4951 if (l->next == 0)
4952 l->os->load_base = lma_expr;
4953 else if (lma_region == 0)
4954 l->os->load_base = exp_binop ('+',
4955 exp_nameop (LOADADDR, l->next->os->name),
4956 exp_nameop (SIZEOF, l->next->os->name));
4958 if (phdrs != NULL && l->os->phdrs == NULL)
4959 l->os->phdrs = phdrs;
4961 if (nocrossrefs)
4963 struct lang_nocrossref *nc;
4965 nc = xmalloc (sizeof *nc);
4966 nc->name = l->os->name;
4967 nc->next = nocrossref;
4968 nocrossref = nc;
4971 next = l->next;
4972 free (l);
4973 l = next;
4976 if (nocrossref != NULL)
4977 lang_add_nocrossref (nocrossref);
4979 overlay_vma = NULL;
4980 overlay_list = NULL;
4981 overlay_max = NULL;
4984 /* Version handling. This is only useful for ELF. */
4986 /* This global variable holds the version tree that we build. */
4988 struct bfd_elf_version_tree *lang_elf_version_info;
4990 /* If PREV is NULL, return first version pattern matching particular symbol.
4991 If PREV is non-NULL, return first version pattern matching particular
4992 symbol after PREV (previously returned by lang_vers_match). */
4994 static struct bfd_elf_version_expr *
4995 lang_vers_match (struct bfd_elf_version_expr_head *head,
4996 struct bfd_elf_version_expr *prev,
4997 const char *sym)
4999 const char *cxx_sym = sym;
5000 const char *java_sym = sym;
5001 struct bfd_elf_version_expr *expr = NULL;
5003 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5005 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5006 if (!cxx_sym)
5007 cxx_sym = sym;
5009 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5011 java_sym = cplus_demangle (sym, DMGL_JAVA);
5012 if (!java_sym)
5013 java_sym = sym;
5016 if (head->htab && (prev == NULL || prev->symbol))
5018 struct bfd_elf_version_expr e;
5020 switch (prev ? prev->mask : 0)
5022 case 0:
5023 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5025 e.symbol = sym;
5026 expr = htab_find (head->htab, &e);
5027 while (expr && strcmp (expr->symbol, sym) == 0)
5028 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5029 goto out_ret;
5030 else
5031 expr = expr->next;
5033 /* Fallthrough */
5034 case BFD_ELF_VERSION_C_TYPE:
5035 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5037 e.symbol = cxx_sym;
5038 expr = htab_find (head->htab, &e);
5039 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5040 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5041 goto out_ret;
5042 else
5043 expr = expr->next;
5045 /* Fallthrough */
5046 case BFD_ELF_VERSION_CXX_TYPE:
5047 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5049 e.symbol = java_sym;
5050 expr = htab_find (head->htab, &e);
5051 while (expr && strcmp (expr->symbol, java_sym) == 0)
5052 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5053 goto out_ret;
5054 else
5055 expr = expr->next;
5057 /* Fallthrough */
5058 default:
5059 break;
5063 /* Finally, try the wildcards. */
5064 if (prev == NULL || prev->symbol)
5065 expr = head->remaining;
5066 else
5067 expr = prev->next;
5068 while (expr)
5070 const char *s;
5072 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5073 break;
5075 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5076 s = java_sym;
5077 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5078 s = cxx_sym;
5079 else
5080 s = sym;
5081 if (fnmatch (expr->pattern, s, 0) == 0)
5082 break;
5083 expr = expr->next;
5086 out_ret:
5087 if (cxx_sym != sym)
5088 free ((char *) cxx_sym);
5089 if (java_sym != sym)
5090 free ((char *) java_sym);
5091 return expr;
5094 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5095 return a string pointing to the symbol name. */
5097 static const char *
5098 realsymbol (const char *pattern)
5100 const char *p;
5101 bfd_boolean changed = FALSE, backslash = FALSE;
5102 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5104 for (p = pattern, s = symbol; *p != '\0'; ++p)
5106 /* It is a glob pattern only if there is no preceding
5107 backslash. */
5108 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5110 free (symbol);
5111 return NULL;
5114 if (backslash)
5116 /* Remove the preceding backslash. */
5117 *(s - 1) = *p;
5118 changed = TRUE;
5120 else
5121 *s++ = *p;
5123 backslash = *p == '\\';
5126 if (changed)
5128 *s = '\0';
5129 return symbol;
5131 else
5133 free (symbol);
5134 return pattern;
5138 /* This is called for each variable name or match expression. */
5140 struct bfd_elf_version_expr *
5141 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5142 const char *new,
5143 const char *lang)
5145 struct bfd_elf_version_expr *ret;
5147 ret = xmalloc (sizeof *ret);
5148 ret->next = orig;
5149 ret->pattern = new;
5150 ret->symver = 0;
5151 ret->script = 0;
5152 ret->symbol = realsymbol (new);
5154 if (lang == NULL || strcasecmp (lang, "C") == 0)
5155 ret->mask = BFD_ELF_VERSION_C_TYPE;
5156 else if (strcasecmp (lang, "C++") == 0)
5157 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5158 else if (strcasecmp (lang, "Java") == 0)
5159 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5160 else
5162 einfo (_("%X%P: unknown language `%s' in version information\n"),
5163 lang);
5164 ret->mask = BFD_ELF_VERSION_C_TYPE;
5167 return ldemul_new_vers_pattern (ret);
5170 /* This is called for each set of variable names and match
5171 expressions. */
5173 struct bfd_elf_version_tree *
5174 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5175 struct bfd_elf_version_expr *locals)
5177 struct bfd_elf_version_tree *ret;
5179 ret = xcalloc (1, sizeof *ret);
5180 ret->globals.list = globals;
5181 ret->locals.list = locals;
5182 ret->match = lang_vers_match;
5183 ret->name_indx = (unsigned int) -1;
5184 return ret;
5187 /* This static variable keeps track of version indices. */
5189 static int version_index;
5191 static hashval_t
5192 version_expr_head_hash (const void *p)
5194 const struct bfd_elf_version_expr *e = p;
5196 return htab_hash_string (e->symbol);
5199 static int
5200 version_expr_head_eq (const void *p1, const void *p2)
5202 const struct bfd_elf_version_expr *e1 = p1;
5203 const struct bfd_elf_version_expr *e2 = p2;
5205 return strcmp (e1->symbol, e2->symbol) == 0;
5208 static void
5209 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5211 size_t count = 0;
5212 struct bfd_elf_version_expr *e, *next;
5213 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5215 for (e = head->list; e; e = e->next)
5217 if (e->symbol)
5218 count++;
5219 head->mask |= e->mask;
5222 if (count)
5224 head->htab = htab_create (count * 2, version_expr_head_hash,
5225 version_expr_head_eq, NULL);
5226 list_loc = &head->list;
5227 remaining_loc = &head->remaining;
5228 for (e = head->list; e; e = next)
5230 next = e->next;
5231 if (!e->symbol)
5233 *remaining_loc = e;
5234 remaining_loc = &e->next;
5236 else
5238 void **loc = htab_find_slot (head->htab, e, INSERT);
5240 if (*loc)
5242 struct bfd_elf_version_expr *e1, *last;
5244 e1 = *loc;
5245 last = NULL;
5248 if (e1->mask == e->mask)
5250 last = NULL;
5251 break;
5253 last = e1;
5254 e1 = e1->next;
5256 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5258 if (last == NULL)
5260 /* This is a duplicate. */
5261 /* FIXME: Memory leak. Sometimes pattern is not
5262 xmalloced alone, but in larger chunk of memory. */
5263 /* free (e->symbol); */
5264 free (e);
5266 else
5268 e->next = last->next;
5269 last->next = e;
5272 else
5274 *loc = e;
5275 *list_loc = e;
5276 list_loc = &e->next;
5280 *remaining_loc = NULL;
5281 *list_loc = head->remaining;
5283 else
5284 head->remaining = head->list;
5287 /* This is called when we know the name and dependencies of the
5288 version. */
5290 void
5291 lang_register_vers_node (const char *name,
5292 struct bfd_elf_version_tree *version,
5293 struct bfd_elf_version_deps *deps)
5295 struct bfd_elf_version_tree *t, **pp;
5296 struct bfd_elf_version_expr *e1;
5298 if (name == NULL)
5299 name = "";
5301 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5302 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5304 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5305 free (version);
5306 return;
5309 /* Make sure this node has a unique name. */
5310 for (t = lang_elf_version_info; t != NULL; t = t->next)
5311 if (strcmp (t->name, name) == 0)
5312 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5314 lang_finalize_version_expr_head (&version->globals);
5315 lang_finalize_version_expr_head (&version->locals);
5317 /* Check the global and local match names, and make sure there
5318 aren't any duplicates. */
5320 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5322 for (t = lang_elf_version_info; t != NULL; t = t->next)
5324 struct bfd_elf_version_expr *e2;
5326 if (t->locals.htab && e1->symbol)
5328 e2 = htab_find (t->locals.htab, e1);
5329 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5331 if (e1->mask == e2->mask)
5332 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5333 e1->symbol);
5334 e2 = e2->next;
5337 else if (!e1->symbol)
5338 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5339 if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5340 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5341 e1->pattern);
5345 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5347 for (t = lang_elf_version_info; t != NULL; t = t->next)
5349 struct bfd_elf_version_expr *e2;
5351 if (t->globals.htab && e1->symbol)
5353 e2 = htab_find (t->globals.htab, e1);
5354 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5356 if (e1->mask == e2->mask)
5357 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5358 e1->symbol);
5359 e2 = e2->next;
5362 else if (!e1->symbol)
5363 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5364 if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5365 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5366 e1->pattern);
5370 version->deps = deps;
5371 version->name = name;
5372 if (name[0] != '\0')
5374 ++version_index;
5375 version->vernum = version_index;
5377 else
5378 version->vernum = 0;
5380 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5382 *pp = version;
5385 /* This is called when we see a version dependency. */
5387 struct bfd_elf_version_deps *
5388 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5390 struct bfd_elf_version_deps *ret;
5391 struct bfd_elf_version_tree *t;
5393 ret = xmalloc (sizeof *ret);
5394 ret->next = list;
5396 for (t = lang_elf_version_info; t != NULL; t = t->next)
5398 if (strcmp (t->name, name) == 0)
5400 ret->version_needed = t;
5401 return ret;
5405 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5407 return ret;
5410 static void
5411 lang_do_version_exports_section (void)
5413 struct bfd_elf_version_expr *greg = NULL, *lreg;
5415 LANG_FOR_EACH_INPUT_STATEMENT (is)
5417 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5418 char *contents, *p;
5419 bfd_size_type len;
5421 if (sec == NULL)
5422 continue;
5424 len = bfd_section_size (is->the_bfd, sec);
5425 contents = xmalloc (len);
5426 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5427 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5429 p = contents;
5430 while (p < contents + len)
5432 greg = lang_new_vers_pattern (greg, p, NULL);
5433 p = strchr (p, '\0') + 1;
5436 /* Do not free the contents, as we used them creating the regex. */
5438 /* Do not include this section in the link. */
5439 bfd_set_section_flags (is->the_bfd, sec,
5440 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5443 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5444 lang_register_vers_node (command_line.version_exports_section,
5445 lang_new_vers_node (greg, lreg), NULL);
5448 void
5449 lang_add_unique (const char *name)
5451 struct unique_sections *ent;
5453 for (ent = unique_section_list; ent; ent = ent->next)
5454 if (strcmp (ent->name, name) == 0)
5455 return;
5457 ent = xmalloc (sizeof *ent);
5458 ent->name = xstrdup (name);
5459 ent->next = unique_section_list;
5460 unique_section_list = ent;