bfd/
[binutils-gdb.git] / ld / ldlang.c
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.
5
6 This file is part of GLD, the Gnu Linker.
7
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.
12
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.
17
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. */
22
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"
29
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"
43
44 #ifndef offsetof
45 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
46 #endif
47
48 /* Locals variables. */
49 static struct obstack stat_obstack;
50
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;
65
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);
83
84 typedef void (*callback_t) (lang_wild_statement_type *, struct wildcard_list *,
85 asection *, lang_input_statement_type *, void *);
86
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;
103
104 etree_type *base; /* Relocation base - or null */
105
106 #define new_stat(x, y) \
107 (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
108
109 #define outside_section_address(q) \
110 ((q)->output_offset + (q)->output_section->vma)
111
112 #define outside_symbol_address(q) \
113 ((q)->value + outside_section_address (q->section))
114
115 #define SECTION_NAME_MAP_LENGTH (16)
116
117 void *
118 stat_alloc (size_t size)
119 {
120 return obstack_alloc (&stat_obstack, size);
121 }
122
123 bfd_boolean
124 unique_section_p (const char *secnam)
125 {
126 struct unique_sections *unam;
127
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)
132 {
133 return TRUE;
134 }
135
136 return FALSE;
137 }
138
139 /* Generic traversal routines for finding matching sections. */
140
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)
146 {
147 asection *s;
148
149 if (file->just_syms_flag)
150 return;
151
152 for (s = file->the_bfd->sections; s != NULL; s = s->next)
153 {
154 struct wildcard_list *sec;
155
156 sec = ptr->section_list;
157 if (sec == NULL)
158 (*callback) (ptr, sec, s, file, data);
159
160 while (sec != NULL)
161 {
162 bfd_boolean skip = FALSE;
163 struct name_list *list_tmp;
164
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)
170 {
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;
175
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)
181 {
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;
189 }
190
191 if (skip)
192 break;
193 }
194
195 if (!skip && sec->spec.name != NULL)
196 {
197 const char *sname = bfd_get_section_name (file->the_bfd, s);
198
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;
203 }
204
205 if (!skip)
206 (*callback) (ptr, sec, s, file, data);
207
208 sec = sec->next;
209 }
210 }
211 }
212
213 /* Handle a wild statement for a single file F. */
214
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)
220 {
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
225 {
226 bfd *member;
227
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)
232 {
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)
239 {
240 walk_wild_section (s, member->usrdata, callback, data);
241 }
242
243 member = bfd_openr_next_archived_file (f->the_bfd, member);
244 }
245 }
246 }
247
248 static void
249 walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
250 {
251 const char *file_spec = s->filename;
252
253 if (file_spec == NULL)
254 {
255 /* Perform the iteration over all files in the list. */
256 LANG_FOR_EACH_INPUT_STATEMENT (f)
257 {
258 walk_wild_file (s, f, callback, data);
259 }
260 }
261 else if (wildcardp (file_spec))
262 {
263 LANG_FOR_EACH_INPUT_STATEMENT (f)
264 {
265 if (fnmatch (file_spec, f->filename, FNM_FILE_NAME) == 0)
266 walk_wild_file (s, f, callback, data);
267 }
268 }
269 else
270 {
271 lang_input_statement_type *f;
272
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);
277 }
278 }
279
280 /* lang_for_each_statement walks the parse tree and calls the provided
281 function for each node. */
282
283 static void
284 lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
285 lang_statement_union_type *s)
286 {
287 for (; s != NULL; s = s->header.next)
288 {
289 func (s);
290
291 switch (s->header.type)
292 {
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;
325 }
326 }
327 }
328
329 void
330 lang_for_each_statement (void (*func) (lang_statement_union_type *))
331 {
332 lang_for_each_statement_worker (func, statement_list.head);
333 }
334
335 /*----------------------------------------------------------------------*/
336
337 void
338 lang_list_init (lang_statement_list_type *list)
339 {
340 list->head = NULL;
341 list->tail = &list->head;
342 }
343
344 /* Build a new statement node for the parse tree. */
345
346 static lang_statement_union_type *
347 new_statement (enum statement_enum type,
348 size_t size,
349 lang_statement_list_type *list)
350 {
351 lang_statement_union_type *new;
352
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;
358 }
359
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.
363
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. */
368
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)
374 {
375 lang_input_statement_type *p;
376
377 if (add_to_list)
378 p = new_stat (lang_input_statement, stat_ptr);
379 else
380 {
381 p = stat_alloc (sizeof (lang_input_statement_type));
382 p->header.next = NULL;
383 }
384
385 lang_has_input_file = TRUE;
386 p->target = target;
387 p->sysrooted = FALSE;
388 switch (file_type)
389 {
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 ();
441 }
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->as_needed = as_needed;
449 p->whole_archive = whole_archive;
450 p->loaded = FALSE;
451 lang_statement_append (&input_file_chain,
452 (lang_statement_union_type *) p,
453 &p->next_real_file);
454 return p;
455 }
456
457 lang_input_statement_type *
458 lang_add_input_file (const char *name,
459 lang_input_file_enum_type file_type,
460 const char *target)
461 {
462 lang_has_input_file = TRUE;
463 return new_afile (name, file_type, target, TRUE);
464 }
465
466 /* Build enough state so that the parser can build its tree. */
467
468 void
469 lang_init (void)
470 {
471 obstack_begin (&stat_obstack, 1000);
472
473 stat_ptr = &statement_list;
474
475 lang_list_init (stat_ptr);
476
477 lang_list_init (&input_file_chain);
478 lang_list_init (&lang_output_section_statement);
479 lang_list_init (&file_chain);
480 first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
481 NULL);
482 abs_output_section =
483 lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
484
485 abs_output_section->bfd_section = bfd_abs_section_ptr;
486
487 /* The value "3" is ad-hoc, somewhat related to the expected number of
488 DEFINED expressions in a linker script. For most default linker
489 scripts, there are none. Why a hash table then? Well, it's somewhat
490 simpler to re-use working machinery than using a linked list in terms
491 of code-complexity here in ld, besides the initialization which just
492 looks like other code here. */
493 if (bfd_hash_table_init_n (&lang_definedness_table,
494 lang_definedness_newfunc, 3) != TRUE)
495 einfo (_("%P%F: out of memory during initialization"));
496
497 /* Callers of exp_fold_tree need to increment this. */
498 lang_statement_iteration = 0;
499 }
500
501 /*----------------------------------------------------------------------
502 A region is an area of memory declared with the
503 MEMORY { name:org=exp, len=exp ... }
504 syntax.
505
506 We maintain a list of all the regions here.
507
508 If no regions are specified in the script, then the default is used
509 which is created when looked up to be the entire data space.
510
511 If create is true we are creating a region inside a MEMORY block.
512 In this case it is probably an error to create a region that has
513 already been created. If we are not inside a MEMORY block it is
514 dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
515 and so we issue a warning. */
516
517 static lang_memory_region_type *lang_memory_region_list;
518 static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
519
520 lang_memory_region_type *
521 lang_memory_region_lookup (const char *const name, bfd_boolean create)
522 {
523 lang_memory_region_type *p;
524 lang_memory_region_type *new;
525
526 /* NAME is NULL for LMA memspecs if no region was specified. */
527 if (name == NULL)
528 return NULL;
529
530 for (p = lang_memory_region_list; p != NULL; p = p->next)
531 if (strcmp (p->name, name) == 0)
532 {
533 if (create)
534 einfo (_("%P:%S: warning: redeclaration of memory region '%s'\n"), name);
535 return p;
536 }
537
538 #if 0
539 /* This code used to always use the first region in the list as the
540 default region. I changed it to instead use a region
541 encompassing all of memory as the default region. This permits
542 NOLOAD sections to work reasonably without requiring a region.
543 People should specify what region they mean, if they really want
544 a region. */
545 if (strcmp (name, DEFAULT_MEMORY_REGION) == 0)
546 {
547 if (lang_memory_region_list != NULL)
548 return lang_memory_region_list;
549 }
550 #endif
551
552 if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
553 einfo (_("%P:%S: warning: memory region %s not declared\n"), name);
554
555 new = stat_alloc (sizeof (lang_memory_region_type));
556
557 new->name = xstrdup (name);
558 new->next = NULL;
559
560 *lang_memory_region_list_tail = new;
561 lang_memory_region_list_tail = &new->next;
562 new->origin = 0;
563 new->flags = 0;
564 new->not_flags = 0;
565 new->length = ~(bfd_size_type) 0;
566 new->current = 0;
567 new->had_full_message = FALSE;
568
569 return new;
570 }
571
572 static lang_memory_region_type *
573 lang_memory_default (asection *section)
574 {
575 lang_memory_region_type *p;
576
577 flagword sec_flags = section->flags;
578
579 /* Override SEC_DATA to mean a writable section. */
580 if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
581 sec_flags |= SEC_DATA;
582
583 for (p = lang_memory_region_list; p != NULL; p = p->next)
584 {
585 if ((p->flags & sec_flags) != 0
586 && (p->not_flags & sec_flags) == 0)
587 {
588 return p;
589 }
590 }
591 return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE);
592 }
593
594 lang_output_section_statement_type *
595 lang_output_section_find (const char *const name)
596 {
597 lang_statement_union_type *u;
598 lang_output_section_statement_type *lookup;
599
600 for (u = lang_output_section_statement.head; u != NULL; u = lookup->next)
601 {
602 lookup = &u->output_section_statement;
603 if (strcmp (name, lookup->name) == 0)
604 return lookup;
605 }
606 return NULL;
607 }
608
609 lang_output_section_statement_type *
610 lang_output_section_statement_lookup (const char *const name)
611 {
612 lang_output_section_statement_type *lookup;
613
614 lookup = lang_output_section_find (name);
615 if (lookup == NULL)
616 {
617 lookup = new_stat (lang_output_section_statement, stat_ptr);
618 lookup->region = NULL;
619 lookup->lma_region = NULL;
620 lookup->fill = 0;
621 lookup->block_value = 1;
622 lookup->name = name;
623
624 lookup->next = NULL;
625 lookup->bfd_section = NULL;
626 lookup->processed = 0;
627 lookup->sectype = normal_section;
628 lookup->addr_tree = NULL;
629 lang_list_init (&lookup->children);
630
631 lookup->memspec = NULL;
632 lookup->flags = 0;
633 lookup->subsection_alignment = -1;
634 lookup->section_alignment = -1;
635 lookup->load_base = NULL;
636 lookup->update_dot_tree = NULL;
637 lookup->phdrs = NULL;
638
639 lang_statement_append (&lang_output_section_statement,
640 (lang_statement_union_type *) lookup,
641 &lookup->next);
642 }
643 return lookup;
644 }
645
646 static void
647 lang_map_flags (flagword flag)
648 {
649 if (flag & SEC_ALLOC)
650 minfo ("a");
651
652 if (flag & SEC_CODE)
653 minfo ("x");
654
655 if (flag & SEC_READONLY)
656 minfo ("r");
657
658 if (flag & SEC_DATA)
659 minfo ("w");
660
661 if (flag & SEC_LOAD)
662 minfo ("l");
663 }
664
665 void
666 lang_map (void)
667 {
668 lang_memory_region_type *m;
669
670 minfo (_("\nMemory Configuration\n\n"));
671 fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
672 _("Name"), _("Origin"), _("Length"), _("Attributes"));
673
674 for (m = lang_memory_region_list; m != NULL; m = m->next)
675 {
676 char buf[100];
677 int len;
678
679 fprintf (config.map_file, "%-16s ", m->name);
680
681 sprintf_vma (buf, m->origin);
682 minfo ("0x%s ", buf);
683 len = strlen (buf);
684 while (len < 16)
685 {
686 print_space ();
687 ++len;
688 }
689
690 minfo ("0x%V", m->length);
691 if (m->flags || m->not_flags)
692 {
693 #ifndef BFD64
694 minfo (" ");
695 #endif
696 if (m->flags)
697 {
698 print_space ();
699 lang_map_flags (m->flags);
700 }
701
702 if (m->not_flags)
703 {
704 minfo (" !");
705 lang_map_flags (m->not_flags);
706 }
707 }
708
709 print_nl ();
710 }
711
712 fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
713
714 print_statements ();
715 }
716
717 /* Initialize an output section. */
718
719 static void
720 init_os (lang_output_section_statement_type *s)
721 {
722 section_userdata_type *new;
723
724 if (s->bfd_section != NULL)
725 return;
726
727 if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
728 einfo (_("%P%F: Illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
729
730 new = stat_alloc (sizeof (section_userdata_type));
731
732 s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
733 if (s->bfd_section == NULL)
734 s->bfd_section = bfd_make_section (output_bfd, s->name);
735 if (s->bfd_section == NULL)
736 {
737 einfo (_("%P%F: output format %s cannot represent section called %s\n"),
738 output_bfd->xvec->name, s->name);
739 }
740 s->bfd_section->output_section = s->bfd_section;
741
742 /* We initialize an output sections output offset to minus its own
743 vma to allow us to output a section through itself. */
744 s->bfd_section->output_offset = 0;
745 get_userdata (s->bfd_section) = new;
746
747 /* If there is a base address, make sure that any sections it might
748 mention are initialized. */
749 if (s->addr_tree != NULL)
750 exp_init_os (s->addr_tree);
751
752 if (s->load_base != NULL)
753 exp_init_os (s->load_base);
754 }
755
756 /* Make sure that all output sections mentioned in an expression are
757 initialized. */
758
759 static void
760 exp_init_os (etree_type *exp)
761 {
762 switch (exp->type.node_class)
763 {
764 case etree_assign:
765 exp_init_os (exp->assign.src);
766 break;
767
768 case etree_binary:
769 exp_init_os (exp->binary.lhs);
770 exp_init_os (exp->binary.rhs);
771 break;
772
773 case etree_trinary:
774 exp_init_os (exp->trinary.cond);
775 exp_init_os (exp->trinary.lhs);
776 exp_init_os (exp->trinary.rhs);
777 break;
778
779 case etree_assert:
780 exp_init_os (exp->assert_s.child);
781 break;
782
783 case etree_unary:
784 exp_init_os (exp->unary.child);
785 break;
786
787 case etree_name:
788 switch (exp->type.node_code)
789 {
790 case ADDR:
791 case LOADADDR:
792 case SIZEOF:
793 {
794 lang_output_section_statement_type *os;
795
796 os = lang_output_section_find (exp->name.name);
797 if (os != NULL && os->bfd_section == NULL)
798 init_os (os);
799 }
800 }
801 break;
802
803 default:
804 break;
805 }
806 }
807 \f
808 /* Sections marked with the SEC_LINK_ONCE flag should only be linked
809 once into the output. This routine checks each section, and
810 arrange to discard it if a section of the same name has already
811 been linked. If the section has COMDAT information, then it uses
812 that to decide whether the section should be included. This code
813 assumes that all relevant sections have the SEC_LINK_ONCE flag set;
814 that is, it does not depend solely upon the section name.
815 section_already_linked is called via bfd_map_over_sections. */
816
817 /* This is the shape of the elements inside the already_linked hash
818 table. It maps a name onto a list of already_linked elements with
819 the same name. It's possible to get more than one element in a
820 list if the COMDAT sections have different names. */
821
822 struct already_linked_hash_entry
823 {
824 struct bfd_hash_entry root;
825 struct already_linked *entry;
826 };
827
828 struct already_linked
829 {
830 struct already_linked *next;
831 asection *sec;
832 };
833
834 /* The hash table. */
835
836 static struct bfd_hash_table already_linked_table;
837
838 static void
839 section_already_linked (bfd *abfd, asection *sec, void *data)
840 {
841 lang_input_statement_type *entry = data;
842 flagword flags;
843 const char *name;
844 struct already_linked *l;
845 struct already_linked_hash_entry *already_linked_list;
846
847 /* If we are only reading symbols from this object, then we want to
848 discard all sections. */
849 if (entry->just_syms_flag)
850 {
851 bfd_link_just_syms (sec, &link_info);
852 return;
853 }
854
855 flags = bfd_get_section_flags (abfd, sec);
856
857 if ((flags & SEC_LINK_ONCE) == 0)
858 return;
859
860 /* FIXME: When doing a relocatable link, we may have trouble
861 copying relocations in other sections that refer to local symbols
862 in the section being discarded. Those relocations will have to
863 be converted somehow; as of this writing I'm not sure that any of
864 the backends handle that correctly.
865
866 It is tempting to instead not discard link once sections when
867 doing a relocatable link (technically, they should be discarded
868 whenever we are building constructors). However, that fails,
869 because the linker winds up combining all the link once sections
870 into a single large link once section, which defeats the purpose
871 of having link once sections in the first place.
872
873 Also, not merging link once sections in a relocatable link
874 causes trouble for MIPS ELF, which relies on link once semantics
875 to handle the .reginfo section correctly. */
876
877 name = bfd_get_section_name (abfd, sec);
878
879 already_linked_list =
880 ((struct already_linked_hash_entry *)
881 bfd_hash_lookup (&already_linked_table, name, TRUE, FALSE));
882
883 for (l = already_linked_list->entry; l != NULL; l = l->next)
884 {
885 if (sec->comdat == NULL
886 || l->sec->comdat == NULL
887 || strcmp (sec->comdat->name, l->sec->comdat->name) == 0)
888 {
889 /* The section has already been linked. See if we should
890 issue a warning. */
891 switch (flags & SEC_LINK_DUPLICATES)
892 {
893 default:
894 abort ();
895
896 case SEC_LINK_DUPLICATES_DISCARD:
897 break;
898
899 case SEC_LINK_DUPLICATES_ONE_ONLY:
900 if (sec->comdat == NULL)
901 einfo (_("%P: %B: warning: ignoring duplicate section `%s'\n"),
902 abfd, name);
903 else
904 einfo (_("%P: %B: warning: ignoring duplicate `%s' section symbol `%s'\n"),
905 abfd, name, sec->comdat->name);
906 break;
907
908 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
909 /* FIXME: We should really dig out the contents of both
910 sections and memcmp them. The COFF/PE spec says that
911 the Microsoft linker does not implement this
912 correctly, so I'm not going to bother doing it
913 either. */
914 /* Fall through. */
915 case SEC_LINK_DUPLICATES_SAME_SIZE:
916 if (bfd_section_size (abfd, sec)
917 != bfd_section_size (l->sec->owner, l->sec))
918 einfo (_("%P: %B: warning: duplicate section `%s' has different size\n"),
919 abfd, name);
920 break;
921 }
922
923 /* Set the output_section field so that lang_add_section
924 does not create a lang_input_section structure for this
925 section. Since there might be a symbol in the section
926 being discarded, we must retain a pointer to the section
927 which we are really going to use. */
928 sec->output_section = bfd_abs_section_ptr;
929 sec->kept_section = l->sec;
930
931 if (flags & SEC_GROUP)
932 bfd_discard_group (abfd, sec);
933
934 return;
935 }
936 }
937
938 /* This is the first section with this name. Record it. Allocate
939 the memory from the same obstack as the hash table is kept in. */
940
941 l = bfd_hash_allocate (&already_linked_table, sizeof *l);
942
943 l->sec = sec;
944 l->next = already_linked_list->entry;
945 already_linked_list->entry = l;
946 }
947
948 /* Support routines for the hash table used by section_already_linked,
949 initialize the table, fill in an entry and remove the table. */
950
951 static struct bfd_hash_entry *
952 already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
953 struct bfd_hash_table *table,
954 const char *string ATTRIBUTE_UNUSED)
955 {
956 struct already_linked_hash_entry *ret =
957 bfd_hash_allocate (table, sizeof (struct already_linked_hash_entry));
958
959 ret->entry = NULL;
960
961 return &ret->root;
962 }
963
964 static void
965 already_linked_table_init (void)
966 {
967 if (! bfd_hash_table_init_n (&already_linked_table,
968 already_linked_newfunc,
969 42))
970 einfo (_("%P%F: Failed to create hash table\n"));
971 }
972
973 static void
974 already_linked_table_free (void)
975 {
976 bfd_hash_table_free (&already_linked_table);
977 }
978 \f
979 /* The wild routines.
980
981 These expand statements like *(.text) and foo.o to a list of
982 explicit actions, like foo.o(.text), bar.o(.text) and
983 foo.o(.text, .data). */
984
985 /* Return TRUE if the PATTERN argument is a wildcard pattern.
986 Although backslashes are treated specially if a pattern contains
987 wildcards, we do not consider the mere presence of a backslash to
988 be enough to cause the pattern to be treated as a wildcard.
989 That lets us handle DOS filenames more naturally. */
990
991 static bfd_boolean
992 wildcardp (const char *pattern)
993 {
994 const char *s;
995
996 for (s = pattern; *s != '\0'; ++s)
997 if (*s == '?'
998 || *s == '*'
999 || *s == '[')
1000 return TRUE;
1001 return FALSE;
1002 }
1003
1004 /* Add SECTION to the output section OUTPUT. Do this by creating a
1005 lang_input_section statement which is placed at PTR. FILE is the
1006 input file which holds SECTION. */
1007
1008 void
1009 lang_add_section (lang_statement_list_type *ptr,
1010 asection *section,
1011 lang_output_section_statement_type *output,
1012 lang_input_statement_type *file)
1013 {
1014 flagword flags;
1015 bfd_boolean discard;
1016
1017 flags = bfd_get_section_flags (section->owner, section);
1018
1019 discard = FALSE;
1020
1021 /* Discard sections marked with SEC_EXCLUDE if we are doing a final
1022 link. Discard debugging sections marked with SEC_EXCLUDE on a
1023 relocatable link too. */
1024 if ((flags & SEC_EXCLUDE) != 0
1025 && ((flags & SEC_DEBUGGING) != 0 || !link_info.relocatable))
1026 discard = TRUE;
1027
1028 /* Discard input sections which are assigned to a section named
1029 DISCARD_SECTION_NAME. */
1030 if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
1031 discard = TRUE;
1032
1033 /* Discard debugging sections if we are stripping debugging
1034 information. */
1035 if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
1036 && (flags & SEC_DEBUGGING) != 0)
1037 discard = TRUE;
1038
1039 if (discard)
1040 {
1041 if (section->output_section == NULL)
1042 {
1043 /* This prevents future calls from assigning this section. */
1044 section->output_section = bfd_abs_section_ptr;
1045 }
1046 return;
1047 }
1048
1049 if (section->output_section == NULL)
1050 {
1051 bfd_boolean first;
1052 lang_input_section_type *new;
1053 flagword flags;
1054
1055 if (output->bfd_section == NULL)
1056 init_os (output);
1057
1058 first = ! output->bfd_section->linker_has_input;
1059 output->bfd_section->linker_has_input = 1;
1060
1061 /* Add a section reference to the list. */
1062 new = new_stat (lang_input_section, ptr);
1063
1064 new->section = section;
1065 new->ifile = file;
1066 section->output_section = output->bfd_section;
1067
1068 flags = section->flags;
1069
1070 /* We don't copy the SEC_NEVER_LOAD flag from an input section
1071 to an output section, because we want to be able to include a
1072 SEC_NEVER_LOAD section in the middle of an otherwise loaded
1073 section (I don't know why we want to do this, but we do).
1074 build_link_order in ldwrite.c handles this case by turning
1075 the embedded SEC_NEVER_LOAD section into a fill. */
1076
1077 flags &= ~ SEC_NEVER_LOAD;
1078
1079 /* If final link, don't copy the SEC_LINK_ONCE flags, they've
1080 already been processed. One reason to do this is that on pe
1081 format targets, .text$foo sections go into .text and it's odd
1082 to see .text with SEC_LINK_ONCE set. */
1083
1084 if (! link_info.relocatable)
1085 flags &= ~ (SEC_LINK_ONCE | SEC_LINK_DUPLICATES);
1086
1087 /* If this is not the first input section, and the SEC_READONLY
1088 flag is not currently set, then don't set it just because the
1089 input section has it set. */
1090
1091 if (! first && (section->output_section->flags & SEC_READONLY) == 0)
1092 flags &= ~ SEC_READONLY;
1093
1094 /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
1095 if (! first
1096 && ((section->output_section->flags & (SEC_MERGE | SEC_STRINGS))
1097 != (flags & (SEC_MERGE | SEC_STRINGS))
1098 || ((flags & SEC_MERGE)
1099 && section->output_section->entsize != section->entsize)))
1100 {
1101 section->output_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
1102 flags &= ~ (SEC_MERGE | SEC_STRINGS);
1103 }
1104
1105 section->output_section->flags |= flags;
1106
1107 if (flags & SEC_MERGE)
1108 section->output_section->entsize = section->entsize;
1109
1110 /* If SEC_READONLY is not set in the input section, then clear
1111 it from the output section. */
1112 if ((section->flags & SEC_READONLY) == 0)
1113 section->output_section->flags &= ~SEC_READONLY;
1114
1115 switch (output->sectype)
1116 {
1117 case normal_section:
1118 break;
1119 case dsect_section:
1120 case copy_section:
1121 case info_section:
1122 case overlay_section:
1123 output->bfd_section->flags &= ~SEC_ALLOC;
1124 break;
1125 case noload_section:
1126 output->bfd_section->flags &= ~SEC_LOAD;
1127 output->bfd_section->flags |= SEC_NEVER_LOAD;
1128 break;
1129 }
1130
1131 /* Copy over SEC_SMALL_DATA. */
1132 if (section->flags & SEC_SMALL_DATA)
1133 section->output_section->flags |= SEC_SMALL_DATA;
1134
1135 if (section->alignment_power > output->bfd_section->alignment_power)
1136 output->bfd_section->alignment_power = section->alignment_power;
1137
1138 /* If supplied an alignment, then force it. */
1139 if (output->section_alignment != -1)
1140 output->bfd_section->alignment_power = output->section_alignment;
1141
1142 if (section->flags & SEC_BLOCK)
1143 {
1144 section->output_section->flags |= SEC_BLOCK;
1145 /* FIXME: This value should really be obtained from the bfd... */
1146 output->block_value = 128;
1147 }
1148 }
1149 }
1150
1151 /* Handle wildcard sorting. This returns the lang_input_section which
1152 should follow the one we are going to create for SECTION and FILE,
1153 based on the sorting requirements of WILD. It returns NULL if the
1154 new section should just go at the end of the current list. */
1155
1156 static lang_statement_union_type *
1157 wild_sort (lang_wild_statement_type *wild,
1158 struct wildcard_list *sec,
1159 lang_input_statement_type *file,
1160 asection *section)
1161 {
1162 const char *section_name;
1163 lang_statement_union_type *l;
1164
1165 if (!wild->filenames_sorted && (sec == NULL || !sec->spec.sorted))
1166 return NULL;
1167
1168 section_name = bfd_get_section_name (file->the_bfd, section);
1169 for (l = wild->children.head; l != NULL; l = l->header.next)
1170 {
1171 lang_input_section_type *ls;
1172
1173 if (l->header.type != lang_input_section_enum)
1174 continue;
1175 ls = &l->input_section;
1176
1177 /* Sorting by filename takes precedence over sorting by section
1178 name. */
1179
1180 if (wild->filenames_sorted)
1181 {
1182 const char *fn, *ln;
1183 bfd_boolean fa, la;
1184 int i;
1185
1186 /* The PE support for the .idata section as generated by
1187 dlltool assumes that files will be sorted by the name of
1188 the archive and then the name of the file within the
1189 archive. */
1190
1191 if (file->the_bfd != NULL
1192 && bfd_my_archive (file->the_bfd) != NULL)
1193 {
1194 fn = bfd_get_filename (bfd_my_archive (file->the_bfd));
1195 fa = TRUE;
1196 }
1197 else
1198 {
1199 fn = file->filename;
1200 fa = FALSE;
1201 }
1202
1203 if (ls->ifile->the_bfd != NULL
1204 && bfd_my_archive (ls->ifile->the_bfd) != NULL)
1205 {
1206 ln = bfd_get_filename (bfd_my_archive (ls->ifile->the_bfd));
1207 la = TRUE;
1208 }
1209 else
1210 {
1211 ln = ls->ifile->filename;
1212 la = FALSE;
1213 }
1214
1215 i = strcmp (fn, ln);
1216 if (i > 0)
1217 continue;
1218 else if (i < 0)
1219 break;
1220
1221 if (fa || la)
1222 {
1223 if (fa)
1224 fn = file->filename;
1225 if (la)
1226 ln = ls->ifile->filename;
1227
1228 i = strcmp (fn, ln);
1229 if (i > 0)
1230 continue;
1231 else if (i < 0)
1232 break;
1233 }
1234 }
1235
1236 /* Here either the files are not sorted by name, or we are
1237 looking at the sections for this file. */
1238
1239 if (sec != NULL && sec->spec.sorted)
1240 {
1241 if (strcmp (section_name,
1242 bfd_get_section_name (ls->ifile->the_bfd,
1243 ls->section))
1244 < 0)
1245 break;
1246 }
1247 }
1248
1249 return l;
1250 }
1251
1252 /* Expand a wild statement for a particular FILE. SECTION may be
1253 NULL, in which case it is a wild card. */
1254
1255 static void
1256 output_section_callback (lang_wild_statement_type *ptr,
1257 struct wildcard_list *sec,
1258 asection *section,
1259 lang_input_statement_type *file,
1260 void *output)
1261 {
1262 lang_statement_union_type *before;
1263
1264 /* Exclude sections that match UNIQUE_SECTION_LIST. */
1265 if (unique_section_p (bfd_get_section_name (file->the_bfd, section)))
1266 return;
1267
1268 /* If the wild pattern was marked KEEP, the member sections
1269 should be as well. */
1270 if (ptr->keep_sections)
1271 section->flags |= SEC_KEEP;
1272
1273 before = wild_sort (ptr, sec, file, section);
1274
1275 /* Here BEFORE points to the lang_input_section which
1276 should follow the one we are about to add. If BEFORE
1277 is NULL, then the section should just go at the end
1278 of the current list. */
1279
1280 if (before == NULL)
1281 lang_add_section (&ptr->children, section,
1282 (lang_output_section_statement_type *) output,
1283 file);
1284 else
1285 {
1286 lang_statement_list_type list;
1287 lang_statement_union_type **pp;
1288
1289 lang_list_init (&list);
1290 lang_add_section (&list, section,
1291 (lang_output_section_statement_type *) output,
1292 file);
1293
1294 /* If we are discarding the section, LIST.HEAD will
1295 be NULL. */
1296 if (list.head != NULL)
1297 {
1298 ASSERT (list.head->header.next == NULL);
1299
1300 for (pp = &ptr->children.head;
1301 *pp != before;
1302 pp = &(*pp)->header.next)
1303 ASSERT (*pp != NULL);
1304
1305 list.head->header.next = *pp;
1306 *pp = list.head;
1307 }
1308 }
1309 }
1310
1311 /* This is passed a file name which must have been seen already and
1312 added to the statement tree. We will see if it has been opened
1313 already and had its symbols read. If not then we'll read it. */
1314
1315 static lang_input_statement_type *
1316 lookup_name (const char *name)
1317 {
1318 lang_input_statement_type *search;
1319
1320 for (search = (lang_input_statement_type *) input_file_chain.head;
1321 search != NULL;
1322 search = (lang_input_statement_type *) search->next_real_file)
1323 {
1324 /* Use the local_sym_name as the name of the file that has
1325 already been loaded as filename might have been transformed
1326 via the search directory lookup mechanism. */
1327 const char * filename = search->local_sym_name;
1328
1329 if (filename == NULL && name == NULL)
1330 return search;
1331 if (filename != NULL
1332 && name != NULL
1333 && strcmp (filename, name) == 0)
1334 break;
1335 }
1336
1337 if (search == NULL)
1338 search = new_afile (name, lang_input_file_is_search_file_enum, default_target,
1339 FALSE);
1340
1341 /* If we have already added this file, or this file is not real
1342 (FIXME: can that ever actually happen?) or the name is NULL
1343 (FIXME: can that ever actually happen?) don't add this file. */
1344 if (search->loaded
1345 || ! search->real
1346 || search->filename == NULL)
1347 return search;
1348
1349 if (! load_symbols (search, NULL))
1350 return NULL;
1351
1352 return search;
1353 }
1354
1355 /* Get the symbols for an input file. */
1356
1357 static bfd_boolean
1358 load_symbols (lang_input_statement_type *entry,
1359 lang_statement_list_type *place)
1360 {
1361 char **matching;
1362
1363 if (entry->loaded)
1364 return TRUE;
1365
1366 ldfile_open_file (entry);
1367
1368 if (! bfd_check_format (entry->the_bfd, bfd_archive)
1369 && ! bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
1370 {
1371 bfd_error_type err;
1372 lang_statement_list_type *hold;
1373 bfd_boolean bad_load = TRUE;
1374 bfd_boolean save_ldlang_sysrooted_script;
1375
1376 err = bfd_get_error ();
1377
1378 /* See if the emulation has some special knowledge. */
1379 if (ldemul_unrecognized_file (entry))
1380 return TRUE;
1381
1382 if (err == bfd_error_file_ambiguously_recognized)
1383 {
1384 char **p;
1385
1386 einfo (_("%B: file not recognized: %E\n"), entry->the_bfd);
1387 einfo (_("%B: matching formats:"), entry->the_bfd);
1388 for (p = matching; *p != NULL; p++)
1389 einfo (" %s", *p);
1390 einfo ("%F\n");
1391 }
1392 else if (err != bfd_error_file_not_recognized
1393 || place == NULL)
1394 einfo (_("%F%B: file not recognized: %E\n"), entry->the_bfd);
1395 else
1396 bad_load = FALSE;
1397
1398 bfd_close (entry->the_bfd);
1399 entry->the_bfd = NULL;
1400
1401 /* Try to interpret the file as a linker script. */
1402 ldfile_open_command_file (entry->filename);
1403
1404 hold = stat_ptr;
1405 stat_ptr = place;
1406 save_ldlang_sysrooted_script = ldlang_sysrooted_script;
1407 ldlang_sysrooted_script = entry->sysrooted;
1408
1409 ldfile_assumed_script = TRUE;
1410 parser_input = input_script;
1411 yyparse ();
1412 ldfile_assumed_script = FALSE;
1413
1414 ldlang_sysrooted_script = save_ldlang_sysrooted_script;
1415 stat_ptr = hold;
1416
1417 return ! bad_load;
1418 }
1419
1420 if (ldemul_recognized_file (entry))
1421 return TRUE;
1422
1423 /* We don't call ldlang_add_file for an archive. Instead, the
1424 add_symbols entry point will call ldlang_add_file, via the
1425 add_archive_element callback, for each element of the archive
1426 which is used. */
1427 switch (bfd_get_format (entry->the_bfd))
1428 {
1429 default:
1430 break;
1431
1432 case bfd_object:
1433 ldlang_add_file (entry);
1434 if (trace_files || trace_file_tries)
1435 info_msg ("%I\n", entry);
1436 break;
1437
1438 case bfd_archive:
1439 if (entry->whole_archive)
1440 {
1441 bfd *member = NULL;
1442 bfd_boolean loaded = TRUE;
1443
1444 for (;;)
1445 {
1446 member = bfd_openr_next_archived_file (entry->the_bfd, member);
1447
1448 if (member == NULL)
1449 break;
1450
1451 if (! bfd_check_format (member, bfd_object))
1452 {
1453 einfo (_("%F%B: member %B in archive is not an object\n"),
1454 entry->the_bfd, member);
1455 loaded = FALSE;
1456 }
1457
1458 if (! ((*link_info.callbacks->add_archive_element)
1459 (&link_info, member, "--whole-archive")))
1460 abort ();
1461
1462 if (! bfd_link_add_symbols (member, &link_info))
1463 {
1464 einfo (_("%F%B: could not read symbols: %E\n"), member);
1465 loaded = FALSE;
1466 }
1467 }
1468
1469 entry->loaded = loaded;
1470 return loaded;
1471 }
1472 break;
1473 }
1474
1475 if (bfd_link_add_symbols (entry->the_bfd, &link_info))
1476 entry->loaded = TRUE;
1477 else
1478 einfo (_("%F%B: could not read symbols: %E\n"), entry->the_bfd);
1479
1480 return entry->loaded;
1481 }
1482
1483 /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
1484 may be NULL, indicating that it is a wildcard. Separate
1485 lang_input_section statements are created for each part of the
1486 expansion; they are added after the wild statement S. OUTPUT is
1487 the output section. */
1488
1489 static void
1490 wild (lang_wild_statement_type *s,
1491 const char *target ATTRIBUTE_UNUSED,
1492 lang_output_section_statement_type *output)
1493 {
1494 struct wildcard_list *sec;
1495
1496 walk_wild (s, output_section_callback, output);
1497
1498 for (sec = s->section_list; sec != NULL; sec = sec->next)
1499 {
1500 if (default_common_section != NULL)
1501 break;
1502 if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
1503 {
1504 /* Remember the section that common is going to in case we
1505 later get something which doesn't know where to put it. */
1506 default_common_section = output;
1507 }
1508 }
1509 }
1510
1511 /* Return TRUE iff target is the sought target. */
1512
1513 static int
1514 get_target (const bfd_target *target, void *data)
1515 {
1516 const char *sought = data;
1517
1518 return strcmp (target->name, sought) == 0;
1519 }
1520
1521 /* Like strcpy() but convert to lower case as well. */
1522
1523 static void
1524 stricpy (char *dest, char *src)
1525 {
1526 char c;
1527
1528 while ((c = *src++) != 0)
1529 *dest++ = TOLOWER (c);
1530
1531 *dest = 0;
1532 }
1533
1534 /* Remove the first occurrence of needle (if any) in haystack
1535 from haystack. */
1536
1537 static void
1538 strcut (char *haystack, char *needle)
1539 {
1540 haystack = strstr (haystack, needle);
1541
1542 if (haystack)
1543 {
1544 char *src;
1545
1546 for (src = haystack + strlen (needle); *src;)
1547 *haystack++ = *src++;
1548
1549 *haystack = 0;
1550 }
1551 }
1552
1553 /* Compare two target format name strings.
1554 Return a value indicating how "similar" they are. */
1555
1556 static int
1557 name_compare (char *first, char *second)
1558 {
1559 char *copy1;
1560 char *copy2;
1561 int result;
1562
1563 copy1 = xmalloc (strlen (first) + 1);
1564 copy2 = xmalloc (strlen (second) + 1);
1565
1566 /* Convert the names to lower case. */
1567 stricpy (copy1, first);
1568 stricpy (copy2, second);
1569
1570 /* Remove size and endian strings from the name. */
1571 strcut (copy1, "big");
1572 strcut (copy1, "little");
1573 strcut (copy2, "big");
1574 strcut (copy2, "little");
1575
1576 /* Return a value based on how many characters match,
1577 starting from the beginning. If both strings are
1578 the same then return 10 * their length. */
1579 for (result = 0; copy1[result] == copy2[result]; result++)
1580 if (copy1[result] == 0)
1581 {
1582 result *= 10;
1583 break;
1584 }
1585
1586 free (copy1);
1587 free (copy2);
1588
1589 return result;
1590 }
1591
1592 /* Set by closest_target_match() below. */
1593 static const bfd_target *winner;
1594
1595 /* Scan all the valid bfd targets looking for one that has the endianness
1596 requirement that was specified on the command line, and is the nearest
1597 match to the original output target. */
1598
1599 static int
1600 closest_target_match (const bfd_target *target, void *data)
1601 {
1602 const bfd_target *original = data;
1603
1604 if (command_line.endian == ENDIAN_BIG
1605 && target->byteorder != BFD_ENDIAN_BIG)
1606 return 0;
1607
1608 if (command_line.endian == ENDIAN_LITTLE
1609 && target->byteorder != BFD_ENDIAN_LITTLE)
1610 return 0;
1611
1612 /* Must be the same flavour. */
1613 if (target->flavour != original->flavour)
1614 return 0;
1615
1616 /* If we have not found a potential winner yet, then record this one. */
1617 if (winner == NULL)
1618 {
1619 winner = target;
1620 return 0;
1621 }
1622
1623 /* Oh dear, we now have two potential candidates for a successful match.
1624 Compare their names and choose the better one. */
1625 if (name_compare (target->name, original->name)
1626 > name_compare (winner->name, original->name))
1627 winner = target;
1628
1629 /* Keep on searching until wqe have checked them all. */
1630 return 0;
1631 }
1632
1633 /* Return the BFD target format of the first input file. */
1634
1635 static char *
1636 get_first_input_target (void)
1637 {
1638 char *target = NULL;
1639
1640 LANG_FOR_EACH_INPUT_STATEMENT (s)
1641 {
1642 if (s->header.type == lang_input_statement_enum
1643 && s->real)
1644 {
1645 ldfile_open_file (s);
1646
1647 if (s->the_bfd != NULL
1648 && bfd_check_format (s->the_bfd, bfd_object))
1649 {
1650 target = bfd_get_target (s->the_bfd);
1651
1652 if (target != NULL)
1653 break;
1654 }
1655 }
1656 }
1657
1658 return target;
1659 }
1660
1661 const char *
1662 lang_get_output_target (void)
1663 {
1664 const char *target;
1665
1666 /* Has the user told us which output format to use? */
1667 if (output_target != NULL)
1668 return output_target;
1669
1670 /* No - has the current target been set to something other than
1671 the default? */
1672 if (current_target != default_target)
1673 return current_target;
1674
1675 /* No - can we determine the format of the first input file? */
1676 target = get_first_input_target ();
1677 if (target != NULL)
1678 return target;
1679
1680 /* Failed - use the default output target. */
1681 return default_target;
1682 }
1683
1684 /* Open the output file. */
1685
1686 static bfd *
1687 open_output (const char *name)
1688 {
1689 bfd *output;
1690
1691 output_target = lang_get_output_target ();
1692
1693 /* Has the user requested a particular endianness on the command
1694 line? */
1695 if (command_line.endian != ENDIAN_UNSET)
1696 {
1697 const bfd_target *target;
1698 enum bfd_endian desired_endian;
1699
1700 /* Get the chosen target. */
1701 target = bfd_search_for_target (get_target, (void *) output_target);
1702
1703 /* If the target is not supported, we cannot do anything. */
1704 if (target != NULL)
1705 {
1706 if (command_line.endian == ENDIAN_BIG)
1707 desired_endian = BFD_ENDIAN_BIG;
1708 else
1709 desired_endian = BFD_ENDIAN_LITTLE;
1710
1711 /* See if the target has the wrong endianness. This should
1712 not happen if the linker script has provided big and
1713 little endian alternatives, but some scrips don't do
1714 this. */
1715 if (target->byteorder != desired_endian)
1716 {
1717 /* If it does, then see if the target provides
1718 an alternative with the correct endianness. */
1719 if (target->alternative_target != NULL
1720 && (target->alternative_target->byteorder == desired_endian))
1721 output_target = target->alternative_target->name;
1722 else
1723 {
1724 /* Try to find a target as similar as possible to
1725 the default target, but which has the desired
1726 endian characteristic. */
1727 bfd_search_for_target (closest_target_match,
1728 (void *) target);
1729
1730 /* Oh dear - we could not find any targets that
1731 satisfy our requirements. */
1732 if (winner == NULL)
1733 einfo (_("%P: warning: could not find any targets that match endianness requirement\n"));
1734 else
1735 output_target = winner->name;
1736 }
1737 }
1738 }
1739 }
1740
1741 output = bfd_openw (name, output_target);
1742
1743 if (output == NULL)
1744 {
1745 if (bfd_get_error () == bfd_error_invalid_target)
1746 einfo (_("%P%F: target %s not found\n"), output_target);
1747
1748 einfo (_("%P%F: cannot open output file %s: %E\n"), name);
1749 }
1750
1751 delete_output_file_on_failure = TRUE;
1752
1753 #if 0
1754 output->flags |= D_PAGED;
1755 #endif
1756
1757 if (! bfd_set_format (output, bfd_object))
1758 einfo (_("%P%F:%s: can not make object file: %E\n"), name);
1759 if (! bfd_set_arch_mach (output,
1760 ldfile_output_architecture,
1761 ldfile_output_machine))
1762 einfo (_("%P%F:%s: can not set architecture: %E\n"), name);
1763
1764 link_info.hash = bfd_link_hash_table_create (output);
1765 if (link_info.hash == NULL)
1766 einfo (_("%P%F: can not create link hash table: %E\n"));
1767
1768 bfd_set_gp_size (output, g_switch_value);
1769 return output;
1770 }
1771
1772 static void
1773 ldlang_open_output (lang_statement_union_type *statement)
1774 {
1775 switch (statement->header.type)
1776 {
1777 case lang_output_statement_enum:
1778 ASSERT (output_bfd == NULL);
1779 output_bfd = open_output (statement->output_statement.name);
1780 ldemul_set_output_arch ();
1781 if (config.magic_demand_paged && !link_info.relocatable)
1782 output_bfd->flags |= D_PAGED;
1783 else
1784 output_bfd->flags &= ~D_PAGED;
1785 if (config.text_read_only)
1786 output_bfd->flags |= WP_TEXT;
1787 else
1788 output_bfd->flags &= ~WP_TEXT;
1789 if (link_info.traditional_format)
1790 output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
1791 else
1792 output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
1793 break;
1794
1795 case lang_target_statement_enum:
1796 current_target = statement->target_statement.target;
1797 break;
1798 default:
1799 break;
1800 }
1801 }
1802
1803 /* Convert between addresses in bytes and sizes in octets.
1804 For currently supported targets, octets_per_byte is always a power
1805 of two, so we can use shifts. */
1806 #define TO_ADDR(X) ((X) >> opb_shift)
1807 #define TO_SIZE(X) ((X) << opb_shift)
1808
1809 /* Support the above. */
1810 static unsigned int opb_shift = 0;
1811
1812 static void
1813 init_opb (void)
1814 {
1815 unsigned x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
1816 ldfile_output_machine);
1817 opb_shift = 0;
1818 if (x > 1)
1819 while ((x & 1) == 0)
1820 {
1821 x >>= 1;
1822 ++opb_shift;
1823 }
1824 ASSERT (x == 1);
1825 }
1826
1827 /* Open all the input files. */
1828
1829 static void
1830 open_input_bfds (lang_statement_union_type *s, bfd_boolean force)
1831 {
1832 for (; s != NULL; s = s->header.next)
1833 {
1834 switch (s->header.type)
1835 {
1836 case lang_constructors_statement_enum:
1837 open_input_bfds (constructor_list.head, force);
1838 break;
1839 case lang_output_section_statement_enum:
1840 open_input_bfds (s->output_section_statement.children.head, force);
1841 break;
1842 case lang_wild_statement_enum:
1843 /* Maybe we should load the file's symbols. */
1844 if (s->wild_statement.filename
1845 && ! wildcardp (s->wild_statement.filename))
1846 lookup_name (s->wild_statement.filename);
1847 open_input_bfds (s->wild_statement.children.head, force);
1848 break;
1849 case lang_group_statement_enum:
1850 {
1851 struct bfd_link_hash_entry *undefs;
1852
1853 /* We must continually search the entries in the group
1854 until no new symbols are added to the list of undefined
1855 symbols. */
1856
1857 do
1858 {
1859 undefs = link_info.hash->undefs_tail;
1860 open_input_bfds (s->group_statement.children.head, TRUE);
1861 }
1862 while (undefs != link_info.hash->undefs_tail);
1863 }
1864 break;
1865 case lang_target_statement_enum:
1866 current_target = s->target_statement.target;
1867 break;
1868 case lang_input_statement_enum:
1869 if (s->input_statement.real)
1870 {
1871 lang_statement_list_type add;
1872
1873 s->input_statement.target = current_target;
1874
1875 /* If we are being called from within a group, and this
1876 is an archive which has already been searched, then
1877 force it to be researched unless the whole archive
1878 has been loaded already. */
1879 if (force
1880 && !s->input_statement.whole_archive
1881 && s->input_statement.loaded
1882 && bfd_check_format (s->input_statement.the_bfd,
1883 bfd_archive))
1884 s->input_statement.loaded = FALSE;
1885
1886 lang_list_init (&add);
1887
1888 if (! load_symbols (&s->input_statement, &add))
1889 config.make_executable = FALSE;
1890
1891 if (add.head != NULL)
1892 {
1893 *add.tail = s->header.next;
1894 s->header.next = add.head;
1895 }
1896 }
1897 break;
1898 default:
1899 break;
1900 }
1901 }
1902 }
1903
1904 /* If there are [COMMONS] statements, put a wild one into the bss
1905 section. */
1906
1907 static void
1908 lang_reasonable_defaults (void)
1909 {
1910 #if 0
1911 lang_output_section_statement_lookup (".text");
1912 lang_output_section_statement_lookup (".data");
1913
1914 default_common_section = lang_output_section_statement_lookup (".bss");
1915
1916 if (!placed_commons)
1917 {
1918 lang_wild_statement_type *new =
1919 new_stat (lang_wild_statement,
1920 &default_common_section->children);
1921
1922 new->section_name = "COMMON";
1923 new->filename = NULL;
1924 lang_list_init (&new->children);
1925 }
1926 #endif
1927 }
1928
1929 /* Add a symbol to a hash of symbols used in DEFINED (NAME) expressions. */
1930
1931 void
1932 lang_track_definedness (const char *name)
1933 {
1934 if (bfd_hash_lookup (&lang_definedness_table, name, TRUE, FALSE) == NULL)
1935 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
1936 }
1937
1938 /* New-function for the definedness hash table. */
1939
1940 static struct bfd_hash_entry *
1941 lang_definedness_newfunc (struct bfd_hash_entry *entry,
1942 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
1943 const char *name ATTRIBUTE_UNUSED)
1944 {
1945 struct lang_definedness_hash_entry *ret
1946 = (struct lang_definedness_hash_entry *) entry;
1947
1948 if (ret == NULL)
1949 ret = (struct lang_definedness_hash_entry *)
1950 bfd_hash_allocate (table, sizeof (struct lang_definedness_hash_entry));
1951
1952 if (ret == NULL)
1953 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
1954
1955 ret->iteration = -1;
1956 return &ret->root;
1957 }
1958
1959 /* Return the iteration when the definition of NAME was last updated. A
1960 value of -1 means that the symbol is not defined in the linker script
1961 or the command line, but may be defined in the linker symbol table. */
1962
1963 int
1964 lang_symbol_definition_iteration (const char *name)
1965 {
1966 struct lang_definedness_hash_entry *defentry
1967 = (struct lang_definedness_hash_entry *)
1968 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1969
1970 /* We've already created this one on the presence of DEFINED in the
1971 script, so it can't be NULL unless something is borked elsewhere in
1972 the code. */
1973 if (defentry == NULL)
1974 FAIL ();
1975
1976 return defentry->iteration;
1977 }
1978
1979 /* Update the definedness state of NAME. */
1980
1981 void
1982 lang_update_definedness (const char *name, struct bfd_link_hash_entry *h)
1983 {
1984 struct lang_definedness_hash_entry *defentry
1985 = (struct lang_definedness_hash_entry *)
1986 bfd_hash_lookup (&lang_definedness_table, name, FALSE, FALSE);
1987
1988 /* We don't keep track of symbols not tested with DEFINED. */
1989 if (defentry == NULL)
1990 return;
1991
1992 /* If the symbol was already defined, and not from an earlier statement
1993 iteration, don't update the definedness iteration, because that'd
1994 make the symbol seem defined in the linker script at this point, and
1995 it wasn't; it was defined in some object. If we do anyway, DEFINED
1996 would start to yield false before this point and the construct "sym =
1997 DEFINED (sym) ? sym : X;" would change sym to X despite being defined
1998 in an object. */
1999 if (h->type != bfd_link_hash_undefined
2000 && h->type != bfd_link_hash_common
2001 && h->type != bfd_link_hash_new
2002 && defentry->iteration == -1)
2003 return;
2004
2005 defentry->iteration = lang_statement_iteration;
2006 }
2007
2008 /* Add the supplied name to the symbol table as an undefined reference.
2009 This is a two step process as the symbol table doesn't even exist at
2010 the time the ld command line is processed. First we put the name
2011 on a list, then, once the output file has been opened, transfer the
2012 name to the symbol table. */
2013
2014 typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
2015
2016 #define ldlang_undef_chain_list_head entry_symbol.next
2017
2018 void
2019 ldlang_add_undef (const char *const name)
2020 {
2021 ldlang_undef_chain_list_type *new =
2022 stat_alloc (sizeof (ldlang_undef_chain_list_type));
2023
2024 new->next = ldlang_undef_chain_list_head;
2025 ldlang_undef_chain_list_head = new;
2026
2027 new->name = xstrdup (name);
2028
2029 if (output_bfd != NULL)
2030 insert_undefined (new->name);
2031 }
2032
2033 /* Insert NAME as undefined in the symbol table. */
2034
2035 static void
2036 insert_undefined (const char *name)
2037 {
2038 struct bfd_link_hash_entry *h;
2039
2040 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, FALSE, TRUE);
2041 if (h == NULL)
2042 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
2043 if (h->type == bfd_link_hash_new)
2044 {
2045 h->type = bfd_link_hash_undefined;
2046 h->u.undef.abfd = NULL;
2047 bfd_link_add_undef (link_info.hash, h);
2048 }
2049 }
2050
2051 /* Run through the list of undefineds created above and place them
2052 into the linker hash table as undefined symbols belonging to the
2053 script file. */
2054
2055 static void
2056 lang_place_undefineds (void)
2057 {
2058 ldlang_undef_chain_list_type *ptr;
2059
2060 for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
2061 insert_undefined (ptr->name);
2062 }
2063
2064 /* Open input files and attach to output sections. */
2065
2066 static void
2067 map_input_to_output_sections
2068 (lang_statement_union_type *s, const char *target,
2069 lang_output_section_statement_type *output_section_statement)
2070 {
2071 for (; s != NULL; s = s->header.next)
2072 {
2073 switch (s->header.type)
2074 {
2075 case lang_wild_statement_enum:
2076 wild (&s->wild_statement, target, output_section_statement);
2077 break;
2078 case lang_constructors_statement_enum:
2079 map_input_to_output_sections (constructor_list.head,
2080 target,
2081 output_section_statement);
2082 break;
2083 case lang_output_section_statement_enum:
2084 map_input_to_output_sections (s->output_section_statement.children.head,
2085 target,
2086 &s->output_section_statement);
2087 break;
2088 case lang_output_statement_enum:
2089 break;
2090 case lang_target_statement_enum:
2091 target = s->target_statement.target;
2092 break;
2093 case lang_group_statement_enum:
2094 map_input_to_output_sections (s->group_statement.children.head,
2095 target,
2096 output_section_statement);
2097 break;
2098 case lang_data_statement_enum:
2099 /* Make sure that any sections mentioned in the expression
2100 are initialized. */
2101 exp_init_os (s->data_statement.exp);
2102 /* FALLTHROUGH */
2103 case lang_fill_statement_enum:
2104 case lang_input_section_enum:
2105 case lang_object_symbols_statement_enum:
2106 case lang_reloc_statement_enum:
2107 case lang_padding_statement_enum:
2108 case lang_input_statement_enum:
2109 if (output_section_statement != NULL
2110 && output_section_statement->bfd_section == NULL)
2111 init_os (output_section_statement);
2112 break;
2113 case lang_assignment_statement_enum:
2114 if (output_section_statement != NULL
2115 && output_section_statement->bfd_section == NULL)
2116 init_os (output_section_statement);
2117
2118 /* Make sure that any sections mentioned in the assignment
2119 are initialized. */
2120 exp_init_os (s->assignment_statement.exp);
2121 break;
2122 case lang_afile_asection_pair_statement_enum:
2123 FAIL ();
2124 break;
2125 case lang_address_statement_enum:
2126 /* Mark the specified section with the supplied address. */
2127 {
2128 lang_output_section_statement_type *os =
2129 lang_output_section_statement_lookup
2130 (s->address_statement.section_name);
2131
2132 if (os->bfd_section == NULL)
2133 init_os (os);
2134 os->addr_tree = s->address_statement.address;
2135 }
2136 break;
2137 }
2138 }
2139 }
2140
2141 /* An output section might have been removed after its statement was
2142 added. For example, ldemul_before_allocation can remove dynamic
2143 sections if they turn out to be not needed. Clean them up here. */
2144
2145 static void
2146 strip_excluded_output_sections (void)
2147 {
2148 lang_statement_union_type *u;
2149
2150 for (u = lang_output_section_statement.head;
2151 u != NULL;
2152 u = u->output_section_statement.next)
2153 {
2154 lang_output_section_statement_type *os;
2155 asection *s;
2156
2157 os = &u->output_section_statement;
2158 s = os->bfd_section;
2159 if (s != NULL && (s->flags & SEC_EXCLUDE) != 0)
2160 {
2161 asection **p;
2162
2163 os->bfd_section = NULL;
2164
2165 for (p = &output_bfd->sections; *p; p = &(*p)->next)
2166 if (*p == s)
2167 {
2168 bfd_section_list_remove (output_bfd, p);
2169 output_bfd->section_count--;
2170 break;
2171 }
2172 }
2173 }
2174 }
2175
2176 static void
2177 print_output_section_statement
2178 (lang_output_section_statement_type *output_section_statement)
2179 {
2180 asection *section = output_section_statement->bfd_section;
2181 int len;
2182
2183 if (output_section_statement != abs_output_section)
2184 {
2185 minfo ("\n%s", output_section_statement->name);
2186
2187 if (section != NULL)
2188 {
2189 print_dot = section->vma;
2190
2191 len = strlen (output_section_statement->name);
2192 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2193 {
2194 print_nl ();
2195 len = 0;
2196 }
2197 while (len < SECTION_NAME_MAP_LENGTH)
2198 {
2199 print_space ();
2200 ++len;
2201 }
2202
2203 minfo ("0x%V %W", section->vma, section->_raw_size);
2204
2205 if (output_section_statement->load_base != NULL)
2206 {
2207 bfd_vma addr;
2208
2209 addr = exp_get_abs_int (output_section_statement->load_base, 0,
2210 "load base", lang_final_phase_enum);
2211 minfo (_(" load address 0x%V"), addr);
2212 }
2213 }
2214
2215 print_nl ();
2216 }
2217
2218 print_statement_list (output_section_statement->children.head,
2219 output_section_statement);
2220 }
2221
2222 static void
2223 print_assignment (lang_assignment_statement_type *assignment,
2224 lang_output_section_statement_type *output_section)
2225 {
2226 int i;
2227 etree_value_type result;
2228
2229 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2230 print_space ();
2231
2232 result = exp_fold_tree (assignment->exp->assign.src, output_section,
2233 lang_final_phase_enum, print_dot, &print_dot);
2234 if (result.valid_p)
2235 {
2236 const char *dst;
2237 bfd_vma value;
2238
2239 value = result.value + result.section->bfd_section->vma;
2240 dst = assignment->exp->assign.dst;
2241
2242 minfo ("0x%V", value);
2243 if (dst[0] == '.' && dst[1] == 0)
2244 print_dot = value;
2245 }
2246 else
2247 {
2248 minfo ("*undef* ");
2249 #ifdef BFD64
2250 minfo (" ");
2251 #endif
2252 }
2253
2254 minfo (" ");
2255
2256 exp_print_tree (assignment->exp);
2257
2258 print_nl ();
2259 }
2260
2261 static void
2262 print_input_statement (lang_input_statement_type *statm)
2263 {
2264 if (statm->filename != NULL)
2265 {
2266 fprintf (config.map_file, "LOAD %s\n", statm->filename);
2267 }
2268 }
2269
2270 /* Print all symbols defined in a particular section. This is called
2271 via bfd_link_hash_traverse. */
2272
2273 static bfd_boolean
2274 print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
2275 {
2276 asection *sec = ptr;
2277
2278 if ((hash_entry->type == bfd_link_hash_defined
2279 || hash_entry->type == bfd_link_hash_defweak)
2280 && sec == hash_entry->u.def.section)
2281 {
2282 int i;
2283
2284 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2285 print_space ();
2286 minfo ("0x%V ",
2287 (hash_entry->u.def.value
2288 + hash_entry->u.def.section->output_offset
2289 + hash_entry->u.def.section->output_section->vma));
2290
2291 minfo (" %T\n", hash_entry->root.string);
2292 }
2293
2294 return TRUE;
2295 }
2296
2297 /* Print information about an input section to the map file. */
2298
2299 static void
2300 print_input_section (lang_input_section_type *in)
2301 {
2302 asection *i = in->section;
2303 bfd_size_type size = i->_cooked_size != 0 ? i->_cooked_size : i->_raw_size;
2304
2305 init_opb ();
2306 if (size != 0)
2307 {
2308 print_space ();
2309
2310 minfo ("%s", i->name);
2311
2312 if (i->output_section != NULL)
2313 {
2314 int len;
2315
2316 len = 1 + strlen (i->name);
2317 if (len >= SECTION_NAME_MAP_LENGTH - 1)
2318 {
2319 print_nl ();
2320 len = 0;
2321 }
2322 while (len < SECTION_NAME_MAP_LENGTH)
2323 {
2324 print_space ();
2325 ++len;
2326 }
2327
2328 minfo ("0x%V %W %B\n",
2329 i->output_section->vma + i->output_offset, TO_ADDR (size),
2330 i->owner);
2331
2332 if (i->_cooked_size != 0 && i->_cooked_size != i->_raw_size)
2333 {
2334 len = SECTION_NAME_MAP_LENGTH + 3;
2335 #ifdef BFD64
2336 len += 16;
2337 #else
2338 len += 8;
2339 #endif
2340 while (len > 0)
2341 {
2342 print_space ();
2343 --len;
2344 }
2345
2346 minfo (_("%W (size before relaxing)\n"), i->_raw_size);
2347 }
2348
2349 bfd_link_hash_traverse (link_info.hash, print_one_symbol, i);
2350
2351 print_dot = (i->output_section->vma + i->output_offset
2352 + TO_ADDR (size));
2353 }
2354 }
2355 }
2356
2357 static void
2358 print_fill_statement (lang_fill_statement_type *fill)
2359 {
2360 size_t size;
2361 unsigned char *p;
2362 fputs (" FILL mask 0x", config.map_file);
2363 for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
2364 fprintf (config.map_file, "%02x", *p);
2365 fputs ("\n", config.map_file);
2366 }
2367
2368 static void
2369 print_data_statement (lang_data_statement_type *data)
2370 {
2371 int i;
2372 bfd_vma addr;
2373 bfd_size_type size;
2374 const char *name;
2375
2376 init_opb ();
2377 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2378 print_space ();
2379
2380 addr = data->output_vma;
2381 if (data->output_section != NULL)
2382 addr += data->output_section->vma;
2383
2384 switch (data->type)
2385 {
2386 default:
2387 abort ();
2388 case BYTE:
2389 size = BYTE_SIZE;
2390 name = "BYTE";
2391 break;
2392 case SHORT:
2393 size = SHORT_SIZE;
2394 name = "SHORT";
2395 break;
2396 case LONG:
2397 size = LONG_SIZE;
2398 name = "LONG";
2399 break;
2400 case QUAD:
2401 size = QUAD_SIZE;
2402 name = "QUAD";
2403 break;
2404 case SQUAD:
2405 size = QUAD_SIZE;
2406 name = "SQUAD";
2407 break;
2408 }
2409
2410 minfo ("0x%V %W %s 0x%v", addr, size, name, data->value);
2411
2412 if (data->exp->type.node_class != etree_value)
2413 {
2414 print_space ();
2415 exp_print_tree (data->exp);
2416 }
2417
2418 print_nl ();
2419
2420 print_dot = addr + TO_ADDR (size);
2421 }
2422
2423 /* Print an address statement. These are generated by options like
2424 -Ttext. */
2425
2426 static void
2427 print_address_statement (lang_address_statement_type *address)
2428 {
2429 minfo (_("Address of section %s set to "), address->section_name);
2430 exp_print_tree (address->address);
2431 print_nl ();
2432 }
2433
2434 /* Print a reloc statement. */
2435
2436 static void
2437 print_reloc_statement (lang_reloc_statement_type *reloc)
2438 {
2439 int i;
2440 bfd_vma addr;
2441 bfd_size_type size;
2442
2443 init_opb ();
2444 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
2445 print_space ();
2446
2447 addr = reloc->output_vma;
2448 if (reloc->output_section != NULL)
2449 addr += reloc->output_section->vma;
2450
2451 size = bfd_get_reloc_size (reloc->howto);
2452
2453 minfo ("0x%V %W RELOC %s ", addr, size, reloc->howto->name);
2454
2455 if (reloc->name != NULL)
2456 minfo ("%s+", reloc->name);
2457 else
2458 minfo ("%s+", reloc->section->name);
2459
2460 exp_print_tree (reloc->addend_exp);
2461
2462 print_nl ();
2463
2464 print_dot = addr + TO_ADDR (size);
2465 }
2466
2467 static void
2468 print_padding_statement (lang_padding_statement_type *s)
2469 {
2470 int len;
2471 bfd_vma addr;
2472
2473 init_opb ();
2474 minfo (" *fill*");
2475
2476 len = sizeof " *fill*" - 1;
2477 while (len < SECTION_NAME_MAP_LENGTH)
2478 {
2479 print_space ();
2480 ++len;
2481 }
2482
2483 addr = s->output_offset;
2484 if (s->output_section != NULL)
2485 addr += s->output_section->vma;
2486 minfo ("0x%V %W ", addr, s->size);
2487
2488 if (s->fill->size != 0)
2489 {
2490 size_t size;
2491 unsigned char *p;
2492 for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
2493 fprintf (config.map_file, "%02x", *p);
2494 }
2495
2496 print_nl ();
2497
2498 print_dot = addr + TO_ADDR (s->size);
2499 }
2500
2501 static void
2502 print_wild_statement (lang_wild_statement_type *w,
2503 lang_output_section_statement_type *os)
2504 {
2505 struct wildcard_list *sec;
2506
2507 print_space ();
2508
2509 if (w->filenames_sorted)
2510 minfo ("SORT(");
2511 if (w->filename != NULL)
2512 minfo ("%s", w->filename);
2513 else
2514 minfo ("*");
2515 if (w->filenames_sorted)
2516 minfo (")");
2517
2518 minfo ("(");
2519 for (sec = w->section_list; sec; sec = sec->next)
2520 {
2521 if (sec->spec.sorted)
2522 minfo ("SORT(");
2523 if (sec->spec.exclude_name_list != NULL)
2524 {
2525 name_list *tmp;
2526 minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
2527 for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
2528 minfo (" %s", tmp->name);
2529 minfo (") ");
2530 }
2531 if (sec->spec.name != NULL)
2532 minfo ("%s", sec->spec.name);
2533 else
2534 minfo ("*");
2535 if (sec->spec.sorted)
2536 minfo (")");
2537 if (sec->next)
2538 minfo (" ");
2539 }
2540 minfo (")");
2541
2542 print_nl ();
2543
2544 print_statement_list (w->children.head, os);
2545 }
2546
2547 /* Print a group statement. */
2548
2549 static void
2550 print_group (lang_group_statement_type *s,
2551 lang_output_section_statement_type *os)
2552 {
2553 fprintf (config.map_file, "START GROUP\n");
2554 print_statement_list (s->children.head, os);
2555 fprintf (config.map_file, "END GROUP\n");
2556 }
2557
2558 /* Print the list of statements in S.
2559 This can be called for any statement type. */
2560
2561 static void
2562 print_statement_list (lang_statement_union_type *s,
2563 lang_output_section_statement_type *os)
2564 {
2565 while (s != NULL)
2566 {
2567 print_statement (s, os);
2568 s = s->header.next;
2569 }
2570 }
2571
2572 /* Print the first statement in statement list S.
2573 This can be called for any statement type. */
2574
2575 static void
2576 print_statement (lang_statement_union_type *s,
2577 lang_output_section_statement_type *os)
2578 {
2579 switch (s->header.type)
2580 {
2581 default:
2582 fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
2583 FAIL ();
2584 break;
2585 case lang_constructors_statement_enum:
2586 if (constructor_list.head != NULL)
2587 {
2588 if (constructors_sorted)
2589 minfo (" SORT (CONSTRUCTORS)\n");
2590 else
2591 minfo (" CONSTRUCTORS\n");
2592 print_statement_list (constructor_list.head, os);
2593 }
2594 break;
2595 case lang_wild_statement_enum:
2596 print_wild_statement (&s->wild_statement, os);
2597 break;
2598 case lang_address_statement_enum:
2599 print_address_statement (&s->address_statement);
2600 break;
2601 case lang_object_symbols_statement_enum:
2602 minfo (" CREATE_OBJECT_SYMBOLS\n");
2603 break;
2604 case lang_fill_statement_enum:
2605 print_fill_statement (&s->fill_statement);
2606 break;
2607 case lang_data_statement_enum:
2608 print_data_statement (&s->data_statement);
2609 break;
2610 case lang_reloc_statement_enum:
2611 print_reloc_statement (&s->reloc_statement);
2612 break;
2613 case lang_input_section_enum:
2614 print_input_section (&s->input_section);
2615 break;
2616 case lang_padding_statement_enum:
2617 print_padding_statement (&s->padding_statement);
2618 break;
2619 case lang_output_section_statement_enum:
2620 print_output_section_statement (&s->output_section_statement);
2621 break;
2622 case lang_assignment_statement_enum:
2623 print_assignment (&s->assignment_statement, os);
2624 break;
2625 case lang_target_statement_enum:
2626 fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
2627 break;
2628 case lang_output_statement_enum:
2629 minfo ("OUTPUT(%s", s->output_statement.name);
2630 if (output_target != NULL)
2631 minfo (" %s", output_target);
2632 minfo (")\n");
2633 break;
2634 case lang_input_statement_enum:
2635 print_input_statement (&s->input_statement);
2636 break;
2637 case lang_group_statement_enum:
2638 print_group (&s->group_statement, os);
2639 break;
2640 case lang_afile_asection_pair_statement_enum:
2641 FAIL ();
2642 break;
2643 }
2644 }
2645
2646 static void
2647 print_statements (void)
2648 {
2649 print_statement_list (statement_list.head, abs_output_section);
2650 }
2651
2652 /* Print the first N statements in statement list S to STDERR.
2653 If N == 0, nothing is printed.
2654 If N < 0, the entire list is printed.
2655 Intended to be called from GDB. */
2656
2657 void
2658 dprint_statement (lang_statement_union_type *s, int n)
2659 {
2660 FILE *map_save = config.map_file;
2661
2662 config.map_file = stderr;
2663
2664 if (n < 0)
2665 print_statement_list (s, abs_output_section);
2666 else
2667 {
2668 while (s && --n >= 0)
2669 {
2670 print_statement (s, abs_output_section);
2671 s = s->header.next;
2672 }
2673 }
2674
2675 config.map_file = map_save;
2676 }
2677
2678 static void
2679 insert_pad (lang_statement_union_type **ptr,
2680 fill_type *fill,
2681 unsigned int alignment_needed,
2682 asection *output_section,
2683 bfd_vma dot)
2684 {
2685 static fill_type zero_fill = { 1, { 0 } };
2686 lang_statement_union_type *pad;
2687
2688 pad = ((lang_statement_union_type *)
2689 ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
2690 if (ptr != &statement_list.head
2691 && pad->header.type == lang_padding_statement_enum
2692 && pad->padding_statement.output_section == output_section)
2693 {
2694 /* Use the existing pad statement. The above test on output
2695 section is probably redundant, but it doesn't hurt to check. */
2696 }
2697 else
2698 {
2699 /* Make a new padding statement, linked into existing chain. */
2700 pad = stat_alloc (sizeof (lang_padding_statement_type));
2701 pad->header.next = *ptr;
2702 *ptr = pad;
2703 pad->header.type = lang_padding_statement_enum;
2704 pad->padding_statement.output_section = output_section;
2705 if (fill == NULL)
2706 fill = &zero_fill;
2707 pad->padding_statement.fill = fill;
2708 }
2709 pad->padding_statement.output_offset = dot - output_section->vma;
2710 pad->padding_statement.size = alignment_needed;
2711 output_section->_raw_size += alignment_needed;
2712 }
2713
2714 /* Work out how much this section will move the dot point. */
2715
2716 static bfd_vma
2717 size_input_section (lang_statement_union_type **this_ptr,
2718 lang_output_section_statement_type *output_section_statement,
2719 fill_type *fill,
2720 bfd_vma dot)
2721 {
2722 lang_input_section_type *is = &((*this_ptr)->input_section);
2723 asection *i = is->section;
2724
2725 if (!is->ifile->just_syms_flag)
2726 {
2727 unsigned int alignment_needed;
2728 asection *o;
2729
2730 /* Align this section first to the input sections requirement,
2731 then to the output section's requirement. If this alignment
2732 is greater than any seen before, then record it too. Perform
2733 the alignment by inserting a magic 'padding' statement. */
2734
2735 if (output_section_statement->subsection_alignment != -1)
2736 i->alignment_power = output_section_statement->subsection_alignment;
2737
2738 o = output_section_statement->bfd_section;
2739 if (o->alignment_power < i->alignment_power)
2740 o->alignment_power = i->alignment_power;
2741
2742 alignment_needed = align_power (dot, i->alignment_power) - dot;
2743
2744 if (alignment_needed != 0)
2745 {
2746 insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
2747 dot += alignment_needed;
2748 }
2749
2750 /* Remember where in the output section this input section goes. */
2751
2752 i->output_offset = dot - o->vma;
2753
2754 /* Mark how big the output section must be to contain this now. */
2755 if (i->_cooked_size != 0)
2756 dot += TO_ADDR (i->_cooked_size);
2757 else
2758 dot += TO_ADDR (i->_raw_size);
2759 o->_raw_size = TO_SIZE (dot - o->vma);
2760 }
2761 else
2762 {
2763 i->output_offset = i->vma - output_section_statement->bfd_section->vma;
2764 }
2765
2766 return dot;
2767 }
2768
2769 #define IGNORE_SECTION(bfd, s) \
2770 (((bfd_get_section_flags (bfd, s) & SEC_THREAD_LOCAL) \
2771 ? ((bfd_get_section_flags (bfd, s) & (SEC_LOAD | SEC_NEVER_LOAD)) \
2772 != SEC_LOAD) \
2773 : ((bfd_get_section_flags (bfd, s) & (SEC_ALLOC | SEC_NEVER_LOAD)) \
2774 != SEC_ALLOC)) \
2775 || bfd_section_size (bfd, s) == 0)
2776
2777 /* Check to see if any allocated sections overlap with other allocated
2778 sections. This can happen when the linker script specifically specifies
2779 the output section addresses of the two sections. */
2780
2781 static void
2782 lang_check_section_addresses (void)
2783 {
2784 asection *s;
2785
2786 /* Scan all sections in the output list. */
2787 for (s = output_bfd->sections; s != NULL; s = s->next)
2788 {
2789 asection *os;
2790
2791 /* Ignore sections which are not loaded or which have no contents. */
2792 if (IGNORE_SECTION (output_bfd, s))
2793 continue;
2794
2795 /* Once we reach section 's' stop our seach. This prevents two
2796 warning messages from being produced, one for 'section A overlaps
2797 section B' and one for 'section B overlaps section A'. */
2798 for (os = output_bfd->sections; os != s; os = os->next)
2799 {
2800 bfd_vma s_start;
2801 bfd_vma s_end;
2802 bfd_vma os_start;
2803 bfd_vma os_end;
2804
2805 /* Only consider loadable sections with real contents. */
2806 if (IGNORE_SECTION (output_bfd, os))
2807 continue;
2808
2809 /* We must check the sections' LMA addresses not their
2810 VMA addresses because overlay sections can have
2811 overlapping VMAs but they must have distinct LMAs. */
2812 s_start = bfd_section_lma (output_bfd, s);
2813 os_start = bfd_section_lma (output_bfd, os);
2814 s_end = s_start + TO_ADDR (bfd_section_size (output_bfd, s)) - 1;
2815 os_end = os_start + TO_ADDR (bfd_section_size (output_bfd, os)) - 1;
2816
2817 /* Look for an overlap. */
2818 if ((s_end < os_start) || (s_start > os_end))
2819 continue;
2820
2821 einfo (
2822 _("%X%P: section %s [%V -> %V] overlaps section %s [%V -> %V]\n"),
2823 s->name, s_start, s_end, os->name, os_start, os_end);
2824
2825 /* Once we have found one overlap for this section,
2826 stop looking for others. */
2827 break;
2828 }
2829 }
2830 }
2831
2832 /* Make sure the new address is within the region. We explicitly permit the
2833 current address to be at the exact end of the region when the address is
2834 non-zero, in case the region is at the end of addressable memory and the
2835 calculation wraps around. */
2836
2837 static void
2838 os_region_check (lang_output_section_statement_type *os,
2839 lang_memory_region_type *region,
2840 etree_type *tree,
2841 bfd_vma base)
2842 {
2843 if ((region->current < region->origin
2844 || (region->current - region->origin > region->length))
2845 && ((region->current != region->origin + region->length)
2846 || base == 0))
2847 {
2848 if (tree != NULL)
2849 {
2850 einfo (_("%X%P: address 0x%v of %B section %s is not within region %s\n"),
2851 region->current,
2852 os->bfd_section->owner,
2853 os->bfd_section->name,
2854 region->name);
2855 }
2856 else
2857 {
2858 einfo (_("%X%P: region %s is full (%B section %s)\n"),
2859 region->name,
2860 os->bfd_section->owner,
2861 os->bfd_section->name);
2862 }
2863 /* Reset the region pointer. */
2864 region->current = region->origin;
2865 }
2866 }
2867
2868 /* Set the sizes for all the output sections. */
2869
2870 static bfd_vma
2871 lang_size_sections_1
2872 (lang_statement_union_type *s,
2873 lang_output_section_statement_type *output_section_statement,
2874 lang_statement_union_type **prev,
2875 fill_type *fill,
2876 bfd_vma dot,
2877 bfd_boolean *relax,
2878 bfd_boolean check_regions)
2879 {
2880 /* Size up the sections from their constituent parts. */
2881 for (; s != NULL; s = s->header.next)
2882 {
2883 switch (s->header.type)
2884 {
2885 case lang_output_section_statement_enum:
2886 {
2887 bfd_vma after;
2888 lang_output_section_statement_type *os;
2889
2890 os = &s->output_section_statement;
2891 if (os->bfd_section == NULL)
2892 /* This section was never actually created. */
2893 break;
2894
2895 /* If this is a COFF shared library section, use the size and
2896 address from the input section. FIXME: This is COFF
2897 specific; it would be cleaner if there were some other way
2898 to do this, but nothing simple comes to mind. */
2899 if ((os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2900 {
2901 asection *input;
2902
2903 if (os->children.head == NULL
2904 || os->children.head->header.next != NULL
2905 || os->children.head->header.type != lang_input_section_enum)
2906 einfo (_("%P%X: Internal error on COFF shared library section %s\n"),
2907 os->name);
2908
2909 input = os->children.head->input_section.section;
2910 bfd_set_section_vma (os->bfd_section->owner,
2911 os->bfd_section,
2912 bfd_section_vma (input->owner, input));
2913 os->bfd_section->_raw_size = input->_raw_size;
2914 break;
2915 }
2916
2917 if (bfd_is_abs_section (os->bfd_section))
2918 {
2919 /* No matter what happens, an abs section starts at zero. */
2920 ASSERT (os->bfd_section->vma == 0);
2921 }
2922 else
2923 {
2924 if (os->addr_tree == NULL)
2925 {
2926 /* No address specified for this section, get one
2927 from the region specification. */
2928 if (os->region == NULL
2929 || (((bfd_get_section_flags (output_bfd, os->bfd_section)
2930 & (SEC_ALLOC | SEC_LOAD)) != 0)
2931 && os->region->name[0] == '*'
2932 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0))
2933 {
2934 os->region = lang_memory_default (os->bfd_section);
2935 }
2936
2937 /* If a loadable section is using the default memory
2938 region, and some non default memory regions were
2939 defined, issue an error message. */
2940 if (!IGNORE_SECTION (output_bfd, os->bfd_section)
2941 && ! link_info.relocatable
2942 && check_regions
2943 && strcmp (os->region->name, DEFAULT_MEMORY_REGION) == 0
2944 && lang_memory_region_list != NULL
2945 && (strcmp (lang_memory_region_list->name,
2946 DEFAULT_MEMORY_REGION) != 0
2947 || lang_memory_region_list->next != NULL))
2948 {
2949 /* By default this is an error rather than just a
2950 warning because if we allocate the section to the
2951 default memory region we can end up creating an
2952 excessively large binary, or even seg faulting when
2953 attempting to perform a negative seek. See
2954 http://sources.redhat.com/ml/binutils/2003-04/msg00423.html
2955 for an example of this. This behaviour can be
2956 overridden by the using the --no-check-sections
2957 switch. */
2958 if (command_line.check_section_addresses)
2959 einfo (_("%P%F: error: no memory region specified for loadable section `%s'\n"),
2960 bfd_get_section_name (output_bfd,
2961 os->bfd_section));
2962 else
2963 einfo (_("%P: warning: no memory region specified for loadable section `%s'\n"),
2964 bfd_get_section_name (output_bfd,
2965 os->bfd_section));
2966 }
2967
2968 dot = os->region->current;
2969
2970 if (os->section_alignment == -1)
2971 {
2972 bfd_vma olddot;
2973
2974 olddot = dot;
2975 dot = align_power (dot,
2976 os->bfd_section->alignment_power);
2977
2978 if (dot != olddot && config.warn_section_align)
2979 einfo (_("%P: warning: changing start of section %s by %u bytes\n"),
2980 os->name, (unsigned int) (dot - olddot));
2981 }
2982 }
2983 else
2984 {
2985 etree_value_type r;
2986
2987 os->processed = -1;
2988 r = exp_fold_tree (os->addr_tree,
2989 abs_output_section,
2990 lang_allocating_phase_enum,
2991 dot, &dot);
2992 os->processed = 0;
2993
2994 if (!r.valid_p)
2995 einfo (_("%F%S: non constant or forward reference address expression for section %s\n"),
2996 os->name);
2997
2998 dot = r.value + r.section->bfd_section->vma;
2999 }
3000
3001 /* The section starts here.
3002 First, align to what the section needs. */
3003
3004 if (os->section_alignment != -1)
3005 dot = align_power (dot, os->section_alignment);
3006
3007 bfd_set_section_vma (0, os->bfd_section, dot);
3008
3009 os->bfd_section->output_offset = 0;
3010 }
3011
3012 lang_size_sections_1 (os->children.head, os, &os->children.head,
3013 os->fill, dot, relax, check_regions);
3014
3015 /* Put the section within the requested block size, or
3016 align at the block boundary. */
3017 after = ((os->bfd_section->vma
3018 + TO_ADDR (os->bfd_section->_raw_size)
3019 + os->block_value - 1)
3020 & - (bfd_vma) os->block_value);
3021
3022 if (bfd_is_abs_section (os->bfd_section))
3023 ASSERT (after == os->bfd_section->vma);
3024 else
3025 os->bfd_section->_raw_size
3026 = TO_SIZE (after - os->bfd_section->vma);
3027
3028 dot = os->bfd_section->vma;
3029 /* .tbss sections effectively have zero size. */
3030 if ((os->bfd_section->flags & SEC_HAS_CONTENTS) != 0
3031 || (os->bfd_section->flags & SEC_THREAD_LOCAL) == 0
3032 || link_info.relocatable)
3033 dot += TO_ADDR (os->bfd_section->_raw_size);
3034
3035 os->processed = 1;
3036
3037 if (os->update_dot_tree != 0)
3038 exp_fold_tree (os->update_dot_tree, abs_output_section,
3039 lang_allocating_phase_enum, dot, &dot);
3040
3041 /* Update dot in the region ?
3042 We only do this if the section is going to be allocated,
3043 since unallocated sections do not contribute to the region's
3044 overall size in memory.
3045
3046 If the SEC_NEVER_LOAD bit is not set, it will affect the
3047 addresses of sections after it. We have to update
3048 dot. */
3049 if (os->region != NULL
3050 && ((bfd_get_section_flags (output_bfd, os->bfd_section)
3051 & SEC_NEVER_LOAD) == 0
3052 || (bfd_get_section_flags (output_bfd, os->bfd_section)
3053 & (SEC_ALLOC | SEC_LOAD))))
3054 {
3055 os->region->current = dot;
3056
3057 if (check_regions)
3058 /* Make sure the new address is within the region. */
3059 os_region_check (os, os->region, os->addr_tree,
3060 os->bfd_section->vma);
3061
3062 /* If there's no load address specified, use the run
3063 region as the load region. */
3064 if (os->lma_region == NULL && os->load_base == NULL)
3065 os->lma_region = os->region;
3066
3067 if (os->lma_region != NULL && os->lma_region != os->region)
3068 {
3069 /* Set load_base, which will be handled later. */
3070 os->load_base = exp_intop (os->lma_region->current);
3071 os->lma_region->current +=
3072 TO_ADDR (os->bfd_section->_raw_size);
3073 if (check_regions)
3074 os_region_check (os, os->lma_region, NULL,
3075 os->bfd_section->lma);
3076 }
3077 }
3078 }
3079 break;
3080
3081 case lang_constructors_statement_enum:
3082 dot = lang_size_sections_1 (constructor_list.head,
3083 output_section_statement,
3084 &s->wild_statement.children.head,
3085 fill, dot, relax, check_regions);
3086 break;
3087
3088 case lang_data_statement_enum:
3089 {
3090 unsigned int size = 0;
3091
3092 s->data_statement.output_vma =
3093 dot - output_section_statement->bfd_section->vma;
3094 s->data_statement.output_section =
3095 output_section_statement->bfd_section;
3096
3097 /* We might refer to provided symbols in the expression, and
3098 need to mark them as needed. */
3099 exp_fold_tree (s->data_statement.exp, abs_output_section,
3100 lang_allocating_phase_enum, dot, &dot);
3101
3102 switch (s->data_statement.type)
3103 {
3104 default:
3105 abort ();
3106 case QUAD:
3107 case SQUAD:
3108 size = QUAD_SIZE;
3109 break;
3110 case LONG:
3111 size = LONG_SIZE;
3112 break;
3113 case SHORT:
3114 size = SHORT_SIZE;
3115 break;
3116 case BYTE:
3117 size = BYTE_SIZE;
3118 break;
3119 }
3120 if (size < TO_SIZE ((unsigned) 1))
3121 size = TO_SIZE ((unsigned) 1);
3122 dot += TO_ADDR (size);
3123 output_section_statement->bfd_section->_raw_size += size;
3124 /* The output section gets contents, and then we inspect for
3125 any flags set in the input script which override any ALLOC. */
3126 output_section_statement->bfd_section->flags |= SEC_HAS_CONTENTS;
3127 if (!(output_section_statement->flags & SEC_NEVER_LOAD))
3128 {
3129 output_section_statement->bfd_section->flags |=
3130 SEC_ALLOC | SEC_LOAD;
3131 }
3132 }
3133 break;
3134
3135 case lang_reloc_statement_enum:
3136 {
3137 int size;
3138
3139 s->reloc_statement.output_vma =
3140 dot - output_section_statement->bfd_section->vma;
3141 s->reloc_statement.output_section =
3142 output_section_statement->bfd_section;
3143 size = bfd_get_reloc_size (s->reloc_statement.howto);
3144 dot += TO_ADDR (size);
3145 output_section_statement->bfd_section->_raw_size += size;
3146 }
3147 break;
3148
3149 case lang_wild_statement_enum:
3150
3151 dot = lang_size_sections_1 (s->wild_statement.children.head,
3152 output_section_statement,
3153 &s->wild_statement.children.head,
3154 fill, dot, relax, check_regions);
3155
3156 break;
3157
3158 case lang_object_symbols_statement_enum:
3159 link_info.create_object_symbols_section =
3160 output_section_statement->bfd_section;
3161 break;
3162 case lang_output_statement_enum:
3163 case lang_target_statement_enum:
3164 break;
3165 case lang_input_section_enum:
3166 {
3167 asection *i;
3168
3169 i = (*prev)->input_section.section;
3170 if (! relax)
3171 {
3172 if (i->_cooked_size == 0)
3173 i->_cooked_size = i->_raw_size;
3174 }
3175 else
3176 {
3177 bfd_boolean again;
3178
3179 if (! bfd_relax_section (i->owner, i, &link_info, &again))
3180 einfo (_("%P%F: can't relax section: %E\n"));
3181 if (again)
3182 *relax = TRUE;
3183 }
3184 dot = size_input_section (prev, output_section_statement,
3185 output_section_statement->fill, dot);
3186 }
3187 break;
3188 case lang_input_statement_enum:
3189 break;
3190 case lang_fill_statement_enum:
3191 s->fill_statement.output_section =
3192 output_section_statement->bfd_section;
3193
3194 fill = s->fill_statement.fill;
3195 break;
3196 case lang_assignment_statement_enum:
3197 {
3198 bfd_vma newdot = dot;
3199
3200 exp_fold_tree (s->assignment_statement.exp,
3201 output_section_statement,
3202 lang_allocating_phase_enum,
3203 dot,
3204 &newdot);
3205
3206 if (newdot != dot)
3207 {
3208 if (output_section_statement == abs_output_section)
3209 {
3210 /* If we don't have an output section, then just adjust
3211 the default memory address. */
3212 lang_memory_region_lookup (DEFAULT_MEMORY_REGION, FALSE)->current = newdot;
3213 }
3214 else
3215 {
3216 /* Insert a pad after this statement. We can't
3217 put the pad before when relaxing, in case the
3218 assignment references dot. */
3219 insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
3220 output_section_statement->bfd_section, dot);
3221
3222 /* Don't neuter the pad below when relaxing. */
3223 s = s->header.next;
3224 }
3225
3226 /* If dot is advanced, this implies that the section should
3227 have space allocated to it, unless the user has explicitly
3228 stated that the section should never be loaded. */
3229 if (!(output_section_statement->flags & (SEC_NEVER_LOAD | SEC_ALLOC)))
3230 output_section_statement->bfd_section->flags |= SEC_ALLOC;
3231
3232 dot = newdot;
3233 }
3234 }
3235 break;
3236
3237 case lang_padding_statement_enum:
3238 /* If this is the first time lang_size_sections is called,
3239 we won't have any padding statements. If this is the
3240 second or later passes when relaxing, we should allow
3241 padding to shrink. If padding is needed on this pass, it
3242 will be added back in. */
3243 s->padding_statement.size = 0;
3244
3245 /* Make sure output_offset is valid. If relaxation shrinks
3246 the section and this pad isn't needed, it's possible to
3247 have output_offset larger than the final size of the
3248 section. bfd_set_section_contents will complain even for
3249 a pad size of zero. */
3250 s->padding_statement.output_offset
3251 = dot - output_section_statement->bfd_section->vma;
3252 break;
3253
3254 case lang_group_statement_enum:
3255 dot = lang_size_sections_1 (s->group_statement.children.head,
3256 output_section_statement,
3257 &s->group_statement.children.head,
3258 fill, dot, relax, check_regions);
3259 break;
3260
3261 default:
3262 FAIL ();
3263 break;
3264
3265 /* We can only get here when relaxing is turned on. */
3266 case lang_address_statement_enum:
3267 break;
3268 }
3269 prev = &s->header.next;
3270 }
3271 return dot;
3272 }
3273
3274 bfd_vma
3275 lang_size_sections
3276 (lang_statement_union_type *s,
3277 lang_output_section_statement_type *output_section_statement,
3278 lang_statement_union_type **prev,
3279 fill_type *fill,
3280 bfd_vma dot,
3281 bfd_boolean *relax,
3282 bfd_boolean check_regions)
3283 {
3284 bfd_vma result;
3285 asection *o;
3286
3287 /* Callers of exp_fold_tree need to increment this. */
3288 lang_statement_iteration++;
3289
3290 exp_data_seg.phase = exp_dataseg_none;
3291 result = lang_size_sections_1 (s, output_section_statement, prev, fill,
3292 dot, relax, check_regions);
3293 if (exp_data_seg.phase == exp_dataseg_end_seen)
3294 {
3295 /* If DATA_SEGMENT_ALIGN DATA_SEGMENT_END pair was seen, check whether
3296 a page could be saved in the data segment. */
3297 bfd_vma first, last;
3298
3299 first = -exp_data_seg.base & (exp_data_seg.pagesize - 1);
3300 last = exp_data_seg.end & (exp_data_seg.pagesize - 1);
3301 if (first && last
3302 && ((exp_data_seg.base & ~(exp_data_seg.pagesize - 1))
3303 != (exp_data_seg.end & ~(exp_data_seg.pagesize - 1)))
3304 && first + last <= exp_data_seg.pagesize)
3305 {
3306 exp_data_seg.phase = exp_dataseg_adjust;
3307 lang_statement_iteration++;
3308 result = lang_size_sections_1 (s, output_section_statement, prev,
3309 fill, dot, relax, check_regions);
3310 }
3311 }
3312
3313 /* Some backend relaxers want to refer to the output section size. Give
3314 them a section size that does not change on the next call while they
3315 relax. We can't set this at top because lang_reset_memory_regions
3316 which is called before we get here, sets _raw_size to 0 on relaxing
3317 rounds. */
3318 for (o = output_bfd->sections; o != NULL; o = o->next)
3319 o->_cooked_size = o->_raw_size;
3320
3321 return result;
3322 }
3323
3324 /* Worker function for lang_do_assignments. Recursiveness goes here. */
3325
3326 static bfd_vma
3327 lang_do_assignments_1
3328 (lang_statement_union_type *s,
3329 lang_output_section_statement_type *output_section_statement,
3330 fill_type *fill,
3331 bfd_vma dot)
3332 {
3333 for (; s != NULL; s = s->header.next)
3334 {
3335 switch (s->header.type)
3336 {
3337 case lang_constructors_statement_enum:
3338 dot = lang_do_assignments_1 (constructor_list.head,
3339 output_section_statement,
3340 fill,
3341 dot);
3342 break;
3343
3344 case lang_output_section_statement_enum:
3345 {
3346 lang_output_section_statement_type *os;
3347
3348 os = &(s->output_section_statement);
3349 if (os->bfd_section != NULL)
3350 {
3351 dot = os->bfd_section->vma;
3352 lang_do_assignments_1 (os->children.head, os, os->fill, dot);
3353 dot = (os->bfd_section->vma
3354 + TO_ADDR (os->bfd_section->_raw_size));
3355
3356 }
3357 if (os->load_base)
3358 {
3359 /* If nothing has been placed into the output section then
3360 it won't have a bfd_section. */
3361 if (os->bfd_section)
3362 {
3363 os->bfd_section->lma
3364 = exp_get_abs_int (os->load_base, 0, "load base",
3365 lang_final_phase_enum);
3366 }
3367 }
3368 }
3369 break;
3370 case lang_wild_statement_enum:
3371
3372 dot = lang_do_assignments_1 (s->wild_statement.children.head,
3373 output_section_statement,
3374 fill, dot);
3375
3376 break;
3377
3378 case lang_object_symbols_statement_enum:
3379 case lang_output_statement_enum:
3380 case lang_target_statement_enum:
3381 #if 0
3382 case lang_common_statement_enum:
3383 #endif
3384 break;
3385 case lang_data_statement_enum:
3386 {
3387 etree_value_type value;
3388
3389 value = exp_fold_tree (s->data_statement.exp,
3390 abs_output_section,
3391 lang_final_phase_enum, dot, &dot);
3392 if (!value.valid_p)
3393 einfo (_("%F%P: invalid data statement\n"));
3394 s->data_statement.value
3395 = value.value + value.section->bfd_section->vma;
3396 }
3397 {
3398 unsigned int size;
3399 switch (s->data_statement.type)
3400 {
3401 default:
3402 abort ();
3403 case QUAD:
3404 case SQUAD:
3405 size = QUAD_SIZE;
3406 break;
3407 case LONG:
3408 size = LONG_SIZE;
3409 break;
3410 case SHORT:
3411 size = SHORT_SIZE;
3412 break;
3413 case BYTE:
3414 size = BYTE_SIZE;
3415 break;
3416 }
3417 if (size < TO_SIZE ((unsigned) 1))
3418 size = TO_SIZE ((unsigned) 1);
3419 dot += TO_ADDR (size);
3420 }
3421 break;
3422
3423 case lang_reloc_statement_enum:
3424 {
3425 etree_value_type value;
3426
3427 value = exp_fold_tree (s->reloc_statement.addend_exp,
3428 abs_output_section,
3429 lang_final_phase_enum, dot, &dot);
3430 s->reloc_statement.addend_value = value.value;
3431 if (!value.valid_p)
3432 einfo (_("%F%P: invalid reloc statement\n"));
3433 }
3434 dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
3435 break;
3436
3437 case lang_input_section_enum:
3438 {
3439 asection *in = s->input_section.section;
3440
3441 if (in->_cooked_size != 0)
3442 dot += TO_ADDR (in->_cooked_size);
3443 else
3444 dot += TO_ADDR (in->_raw_size);
3445 }
3446 break;
3447
3448 case lang_input_statement_enum:
3449 break;
3450 case lang_fill_statement_enum:
3451 fill = s->fill_statement.fill;
3452 break;
3453 case lang_assignment_statement_enum:
3454 {
3455 exp_fold_tree (s->assignment_statement.exp,
3456 output_section_statement,
3457 lang_final_phase_enum,
3458 dot,
3459 &dot);
3460 }
3461
3462 break;
3463 case lang_padding_statement_enum:
3464 dot += TO_ADDR (s->padding_statement.size);
3465 break;
3466
3467 case lang_group_statement_enum:
3468 dot = lang_do_assignments_1 (s->group_statement.children.head,
3469 output_section_statement,
3470 fill, dot);
3471
3472 break;
3473
3474 default:
3475 FAIL ();
3476 break;
3477 case lang_address_statement_enum:
3478 break;
3479 }
3480
3481 }
3482 return dot;
3483 }
3484
3485 void
3486 lang_do_assignments (lang_statement_union_type *s,
3487 lang_output_section_statement_type *output_section_statement,
3488 fill_type *fill,
3489 bfd_vma dot)
3490 {
3491 /* Callers of exp_fold_tree need to increment this. */
3492 lang_statement_iteration++;
3493 lang_do_assignments_1 (s, output_section_statement, fill, dot);
3494 }
3495
3496 /* Fix any .startof. or .sizeof. symbols. When the assemblers see the
3497 operator .startof. (section_name), it produces an undefined symbol
3498 .startof.section_name. Similarly, when it sees
3499 .sizeof. (section_name), it produces an undefined symbol
3500 .sizeof.section_name. For all the output sections, we look for
3501 such symbols, and set them to the correct value. */
3502
3503 static void
3504 lang_set_startof (void)
3505 {
3506 asection *s;
3507
3508 if (link_info.relocatable)
3509 return;
3510
3511 for (s = output_bfd->sections; s != NULL; s = s->next)
3512 {
3513 const char *secname;
3514 char *buf;
3515 struct bfd_link_hash_entry *h;
3516
3517 secname = bfd_get_section_name (output_bfd, s);
3518 buf = xmalloc (10 + strlen (secname));
3519
3520 sprintf (buf, ".startof.%s", secname);
3521 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3522 if (h != NULL && h->type == bfd_link_hash_undefined)
3523 {
3524 h->type = bfd_link_hash_defined;
3525 h->u.def.value = bfd_get_section_vma (output_bfd, s);
3526 h->u.def.section = bfd_abs_section_ptr;
3527 }
3528
3529 sprintf (buf, ".sizeof.%s", secname);
3530 h = bfd_link_hash_lookup (link_info.hash, buf, FALSE, FALSE, TRUE);
3531 if (h != NULL && h->type == bfd_link_hash_undefined)
3532 {
3533 h->type = bfd_link_hash_defined;
3534 if (s->_cooked_size != 0)
3535 h->u.def.value = TO_ADDR (s->_cooked_size);
3536 else
3537 h->u.def.value = TO_ADDR (s->_raw_size);
3538 h->u.def.section = bfd_abs_section_ptr;
3539 }
3540
3541 free (buf);
3542 }
3543 }
3544
3545 static void
3546 lang_finish (void)
3547 {
3548 struct bfd_link_hash_entry *h;
3549 bfd_boolean warn;
3550
3551 if (link_info.relocatable || link_info.shared)
3552 warn = FALSE;
3553 else
3554 warn = TRUE;
3555
3556 if (entry_symbol.name == NULL)
3557 {
3558 /* No entry has been specified. Look for start, but don't warn
3559 if we don't find it. */
3560 entry_symbol.name = "start";
3561 warn = FALSE;
3562 }
3563
3564 h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
3565 FALSE, FALSE, TRUE);
3566 if (h != NULL
3567 && (h->type == bfd_link_hash_defined
3568 || h->type == bfd_link_hash_defweak)
3569 && h->u.def.section->output_section != NULL)
3570 {
3571 bfd_vma val;
3572
3573 val = (h->u.def.value
3574 + bfd_get_section_vma (output_bfd,
3575 h->u.def.section->output_section)
3576 + h->u.def.section->output_offset);
3577 if (! bfd_set_start_address (output_bfd, val))
3578 einfo (_("%P%F:%s: can't set start address\n"), entry_symbol.name);
3579 }
3580 else
3581 {
3582 bfd_vma val;
3583 const char *send;
3584
3585 /* We couldn't find the entry symbol. Try parsing it as a
3586 number. */
3587 val = bfd_scan_vma (entry_symbol.name, &send, 0);
3588 if (*send == '\0')
3589 {
3590 if (! bfd_set_start_address (output_bfd, val))
3591 einfo (_("%P%F: can't set start address\n"));
3592 }
3593 else
3594 {
3595 asection *ts;
3596
3597 /* Can't find the entry symbol, and it's not a number. Use
3598 the first address in the text section. */
3599 ts = bfd_get_section_by_name (output_bfd, entry_section);
3600 if (ts != NULL)
3601 {
3602 if (warn)
3603 einfo (_("%P: warning: cannot find entry symbol %s; defaulting to %V\n"),
3604 entry_symbol.name,
3605 bfd_get_section_vma (output_bfd, ts));
3606 if (! bfd_set_start_address (output_bfd,
3607 bfd_get_section_vma (output_bfd,
3608 ts)))
3609 einfo (_("%P%F: can't set start address\n"));
3610 }
3611 else
3612 {
3613 if (warn)
3614 einfo (_("%P: warning: cannot find entry symbol %s; not setting start address\n"),
3615 entry_symbol.name);
3616 }
3617 }
3618 }
3619
3620 bfd_hash_table_free (&lang_definedness_table);
3621 }
3622
3623 /* This is a small function used when we want to ignore errors from
3624 BFD. */
3625
3626 static void
3627 ignore_bfd_errors (const char *s ATTRIBUTE_UNUSED, ...)
3628 {
3629 /* Don't do anything. */
3630 }
3631
3632 /* Check that the architecture of all the input files is compatible
3633 with the output file. Also call the backend to let it do any
3634 other checking that is needed. */
3635
3636 static void
3637 lang_check (void)
3638 {
3639 lang_statement_union_type *file;
3640 bfd *input_bfd;
3641 const bfd_arch_info_type *compatible;
3642
3643 for (file = file_chain.head; file != NULL; file = file->input_statement.next)
3644 {
3645 input_bfd = file->input_statement.the_bfd;
3646 compatible = bfd_arch_get_compatible (input_bfd, output_bfd,
3647 command_line.accept_unknown_input_arch);
3648
3649 /* In general it is not possible to perform a relocatable
3650 link between differing object formats when the input
3651 file has relocations, because the relocations in the
3652 input format may not have equivalent representations in
3653 the output format (and besides BFD does not translate
3654 relocs for other link purposes than a final link). */
3655 if ((link_info.relocatable || link_info.emitrelocations)
3656 && (compatible == NULL
3657 || bfd_get_flavour (input_bfd) != bfd_get_flavour (output_bfd))
3658 && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
3659 {
3660 einfo (_("%P%F: Relocatable linking with relocations from format %s (%B) to format %s (%B) is not supported\n"),
3661 bfd_get_target (input_bfd), input_bfd,
3662 bfd_get_target (output_bfd), output_bfd);
3663 /* einfo with %F exits. */
3664 }
3665
3666 if (compatible == NULL)
3667 {
3668 if (command_line.warn_mismatch)
3669 einfo (_("%P: warning: %s architecture of input file `%B' is incompatible with %s output\n"),
3670 bfd_printable_name (input_bfd), input_bfd,
3671 bfd_printable_name (output_bfd));
3672 }
3673 else if (bfd_count_sections (input_bfd))
3674 {
3675 /* If the input bfd has no contents, it shouldn't set the
3676 private data of the output bfd. */
3677
3678 bfd_error_handler_type pfn = NULL;
3679
3680 /* If we aren't supposed to warn about mismatched input
3681 files, temporarily set the BFD error handler to a
3682 function which will do nothing. We still want to call
3683 bfd_merge_private_bfd_data, since it may set up
3684 information which is needed in the output file. */
3685 if (! command_line.warn_mismatch)
3686 pfn = bfd_set_error_handler (ignore_bfd_errors);
3687 if (! bfd_merge_private_bfd_data (input_bfd, output_bfd))
3688 {
3689 if (command_line.warn_mismatch)
3690 einfo (_("%P%X: failed to merge target specific data of file %B\n"),
3691 input_bfd);
3692 }
3693 if (! command_line.warn_mismatch)
3694 bfd_set_error_handler (pfn);
3695 }
3696 }
3697 }
3698
3699 /* Look through all the global common symbols and attach them to the
3700 correct section. The -sort-common command line switch may be used
3701 to roughly sort the entries by size. */
3702
3703 static void
3704 lang_common (void)
3705 {
3706 if (command_line.inhibit_common_definition)
3707 return;
3708 if (link_info.relocatable
3709 && ! command_line.force_common_definition)
3710 return;
3711
3712 if (! config.sort_common)
3713 bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
3714 else
3715 {
3716 int power;
3717
3718 for (power = 4; power >= 0; power--)
3719 bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
3720 }
3721 }
3722
3723 /* Place one common symbol in the correct section. */
3724
3725 static bfd_boolean
3726 lang_one_common (struct bfd_link_hash_entry *h, void *info)
3727 {
3728 unsigned int power_of_two;
3729 bfd_vma size;
3730 asection *section;
3731
3732 if (h->type != bfd_link_hash_common)
3733 return TRUE;
3734
3735 size = h->u.c.size;
3736 power_of_two = h->u.c.p->alignment_power;
3737
3738 if (config.sort_common
3739 && power_of_two < (unsigned int) *(int *) info)
3740 return TRUE;
3741
3742 section = h->u.c.p->section;
3743
3744 /* Increase the size of the section to align the common sym. */
3745 section->_cooked_size += ((bfd_vma) 1 << (power_of_two + opb_shift)) - 1;
3746 section->_cooked_size &= (- (bfd_vma) 1 << (power_of_two + opb_shift));
3747
3748 /* Adjust the alignment if necessary. */
3749 if (power_of_two > section->alignment_power)
3750 section->alignment_power = power_of_two;
3751
3752 /* Change the symbol from common to defined. */
3753 h->type = bfd_link_hash_defined;
3754 h->u.def.section = section;
3755 h->u.def.value = section->_cooked_size;
3756
3757 /* Increase the size of the section. */
3758 section->_cooked_size += size;
3759
3760 /* Make sure the section is allocated in memory, and make sure that
3761 it is no longer a common section. */
3762 section->flags |= SEC_ALLOC;
3763 section->flags &= ~SEC_IS_COMMON;
3764
3765 if (config.map_file != NULL)
3766 {
3767 static bfd_boolean header_printed;
3768 int len;
3769 char *name;
3770 char buf[50];
3771
3772 if (! header_printed)
3773 {
3774 minfo (_("\nAllocating common symbols\n"));
3775 minfo (_("Common symbol size file\n\n"));
3776 header_printed = TRUE;
3777 }
3778
3779 name = demangle (h->root.string);
3780 minfo ("%s", name);
3781 len = strlen (name);
3782 free (name);
3783
3784 if (len >= 19)
3785 {
3786 print_nl ();
3787 len = 0;
3788 }
3789 while (len < 20)
3790 {
3791 print_space ();
3792 ++len;
3793 }
3794
3795 minfo ("0x");
3796 if (size <= 0xffffffff)
3797 sprintf (buf, "%lx", (unsigned long) size);
3798 else
3799 sprintf_vma (buf, size);
3800 minfo ("%s", buf);
3801 len = strlen (buf);
3802
3803 while (len < 16)
3804 {
3805 print_space ();
3806 ++len;
3807 }
3808
3809 minfo ("%B\n", section->owner);
3810 }
3811
3812 return TRUE;
3813 }
3814
3815 /* Run through the input files and ensure that every input section has
3816 somewhere to go. If one is found without a destination then create
3817 an input request and place it into the statement tree. */
3818
3819 static void
3820 lang_place_orphans (void)
3821 {
3822 LANG_FOR_EACH_INPUT_STATEMENT (file)
3823 {
3824 asection *s;
3825
3826 for (s = file->the_bfd->sections; s != NULL; s = s->next)
3827 {
3828 if (s->output_section == NULL)
3829 {
3830 /* This section of the file is not attached, root
3831 around for a sensible place for it to go. */
3832
3833 if (file->just_syms_flag)
3834 {
3835 abort ();
3836 }
3837 else if (strcmp (s->name, "COMMON") == 0)
3838 {
3839 /* This is a lonely common section which must have
3840 come from an archive. We attach to the section
3841 with the wildcard. */
3842 if (! link_info.relocatable
3843 || command_line.force_common_definition)
3844 {
3845 if (default_common_section == NULL)
3846 {
3847 #if 0
3848 /* This message happens when using the
3849 svr3.ifile linker script, so I have
3850 disabled it. */
3851 info_msg (_("%P: no [COMMON] command, defaulting to .bss\n"));
3852 #endif
3853 default_common_section =
3854 lang_output_section_statement_lookup (".bss");
3855
3856 }
3857 lang_add_section (&default_common_section->children, s,
3858 default_common_section, file);
3859 }
3860 }
3861 else if (ldemul_place_orphan (file, s))
3862 ;
3863 else
3864 {
3865 lang_output_section_statement_type *os;
3866
3867 os = lang_output_section_statement_lookup (s->name);
3868 lang_add_section (&os->children, s, os, file);
3869 }
3870 }
3871 }
3872 }
3873 }
3874
3875 void
3876 lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
3877 {
3878 flagword *ptr_flags;
3879
3880 ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
3881 while (*flags)
3882 {
3883 switch (*flags)
3884 {
3885 case 'A': case 'a':
3886 *ptr_flags |= SEC_ALLOC;
3887 break;
3888
3889 case 'R': case 'r':
3890 *ptr_flags |= SEC_READONLY;
3891 break;
3892
3893 case 'W': case 'w':
3894 *ptr_flags |= SEC_DATA;
3895 break;
3896
3897 case 'X': case 'x':
3898 *ptr_flags |= SEC_CODE;
3899 break;
3900
3901 case 'L': case 'l':
3902 case 'I': case 'i':
3903 *ptr_flags |= SEC_LOAD;
3904 break;
3905
3906 default:
3907 einfo (_("%P%F: invalid syntax in flags\n"));
3908 break;
3909 }
3910 flags++;
3911 }
3912 }
3913
3914 /* Call a function on each input file. This function will be called
3915 on an archive, but not on the elements. */
3916
3917 void
3918 lang_for_each_input_file (void (*func) (lang_input_statement_type *))
3919 {
3920 lang_input_statement_type *f;
3921
3922 for (f = (lang_input_statement_type *) input_file_chain.head;
3923 f != NULL;
3924 f = (lang_input_statement_type *) f->next_real_file)
3925 func (f);
3926 }
3927
3928 /* Call a function on each file. The function will be called on all
3929 the elements of an archive which are included in the link, but will
3930 not be called on the archive file itself. */
3931
3932 void
3933 lang_for_each_file (void (*func) (lang_input_statement_type *))
3934 {
3935 LANG_FOR_EACH_INPUT_STATEMENT (f)
3936 {
3937 func (f);
3938 }
3939 }
3940
3941 void
3942 ldlang_add_file (lang_input_statement_type *entry)
3943 {
3944 bfd **pp;
3945
3946 lang_statement_append (&file_chain,
3947 (lang_statement_union_type *) entry,
3948 &entry->next);
3949
3950 /* The BFD linker needs to have a list of all input BFDs involved in
3951 a link. */
3952 ASSERT (entry->the_bfd->link_next == NULL);
3953 ASSERT (entry->the_bfd != output_bfd);
3954 for (pp = &link_info.input_bfds; *pp != NULL; pp = &(*pp)->link_next)
3955 ;
3956 *pp = entry->the_bfd;
3957 entry->the_bfd->usrdata = entry;
3958 bfd_set_gp_size (entry->the_bfd, g_switch_value);
3959
3960 /* Look through the sections and check for any which should not be
3961 included in the link. We need to do this now, so that we can
3962 notice when the backend linker tries to report multiple
3963 definition errors for symbols which are in sections we aren't
3964 going to link. FIXME: It might be better to entirely ignore
3965 symbols which are defined in sections which are going to be
3966 discarded. This would require modifying the backend linker for
3967 each backend which might set the SEC_LINK_ONCE flag. If we do
3968 this, we should probably handle SEC_EXCLUDE in the same way. */
3969
3970 bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
3971 }
3972
3973 void
3974 lang_add_output (const char *name, int from_script)
3975 {
3976 /* Make -o on command line override OUTPUT in script. */
3977 if (!had_output_filename || !from_script)
3978 {
3979 output_filename = name;
3980 had_output_filename = TRUE;
3981 }
3982 }
3983
3984 static lang_output_section_statement_type *current_section;
3985
3986 static int
3987 topower (int x)
3988 {
3989 unsigned int i = 1;
3990 int l;
3991
3992 if (x < 0)
3993 return -1;
3994
3995 for (l = 0; l < 32; l++)
3996 {
3997 if (i >= (unsigned int) x)
3998 return l;
3999 i <<= 1;
4000 }
4001
4002 return 0;
4003 }
4004
4005 lang_output_section_statement_type *
4006 lang_enter_output_section_statement (const char *output_section_statement_name,
4007 etree_type *address_exp,
4008 enum section_type sectype,
4009 etree_type *align,
4010 etree_type *subalign,
4011 etree_type *ebase)
4012 {
4013 lang_output_section_statement_type *os;
4014
4015 current_section =
4016 os =
4017 lang_output_section_statement_lookup (output_section_statement_name);
4018
4019 /* Add this statement to tree. */
4020 #if 0
4021 add_statement (lang_output_section_statement_enum,
4022 output_section_statement);
4023 #endif
4024 /* Make next things chain into subchain of this. */
4025
4026 if (os->addr_tree == NULL)
4027 {
4028 os->addr_tree = address_exp;
4029 }
4030 os->sectype = sectype;
4031 if (sectype != noload_section)
4032 os->flags = SEC_NO_FLAGS;
4033 else
4034 os->flags = SEC_NEVER_LOAD;
4035 os->block_value = 1;
4036 stat_ptr = &os->children;
4037
4038 os->subsection_alignment =
4039 topower (exp_get_value_int (subalign, -1, "subsection alignment", 0));
4040 os->section_alignment =
4041 topower (exp_get_value_int (align, -1, "section alignment", 0));
4042
4043 os->load_base = ebase;
4044 return os;
4045 }
4046
4047 void
4048 lang_final (void)
4049 {
4050 lang_output_statement_type *new =
4051 new_stat (lang_output_statement, stat_ptr);
4052
4053 new->name = output_filename;
4054 }
4055
4056 /* Reset the current counters in the regions. */
4057
4058 void
4059 lang_reset_memory_regions (void)
4060 {
4061 lang_memory_region_type *p = lang_memory_region_list;
4062 asection *o;
4063
4064 for (p = lang_memory_region_list; p != NULL; p = p->next)
4065 {
4066 p->old_length = (bfd_size_type) (p->current - p->origin);
4067 p->current = p->origin;
4068 }
4069
4070 for (o = output_bfd->sections; o != NULL; o = o->next)
4071 o->_raw_size = 0;
4072 }
4073
4074 /* If the wild pattern was marked KEEP, the member sections
4075 should be as well. */
4076
4077 static void
4078 gc_section_callback (lang_wild_statement_type *ptr,
4079 struct wildcard_list *sec ATTRIBUTE_UNUSED,
4080 asection *section,
4081 lang_input_statement_type *file ATTRIBUTE_UNUSED,
4082 void *data ATTRIBUTE_UNUSED)
4083 {
4084 if (ptr->keep_sections)
4085 section->flags |= SEC_KEEP;
4086 }
4087
4088 /* Handle a wild statement, marking it against GC. */
4089
4090 static void
4091 lang_gc_wild (lang_wild_statement_type *s)
4092 {
4093 walk_wild (s, gc_section_callback, NULL);
4094 }
4095
4096 /* Iterate over sections marking them against GC. */
4097
4098 static void
4099 lang_gc_sections_1 (lang_statement_union_type *s)
4100 {
4101 for (; s != NULL; s = s->header.next)
4102 {
4103 switch (s->header.type)
4104 {
4105 case lang_wild_statement_enum:
4106 lang_gc_wild (&s->wild_statement);
4107 break;
4108 case lang_constructors_statement_enum:
4109 lang_gc_sections_1 (constructor_list.head);
4110 break;
4111 case lang_output_section_statement_enum:
4112 lang_gc_sections_1 (s->output_section_statement.children.head);
4113 break;
4114 case lang_group_statement_enum:
4115 lang_gc_sections_1 (s->group_statement.children.head);
4116 break;
4117 default:
4118 break;
4119 }
4120 }
4121 }
4122
4123 static void
4124 lang_gc_sections (void)
4125 {
4126 struct bfd_link_hash_entry *h;
4127 ldlang_undef_chain_list_type *ulist;
4128
4129 /* Keep all sections so marked in the link script. */
4130
4131 lang_gc_sections_1 (statement_list.head);
4132
4133 /* Keep all sections containing symbols undefined on the command-line,
4134 and the section containing the entry symbol. */
4135
4136 for (ulist = link_info.gc_sym_list; ulist; ulist = ulist->next)
4137 {
4138 h = bfd_link_hash_lookup (link_info.hash, ulist->name,
4139 FALSE, FALSE, FALSE);
4140
4141 if (h != NULL
4142 && (h->type == bfd_link_hash_defined
4143 || h->type == bfd_link_hash_defweak)
4144 && ! bfd_is_abs_section (h->u.def.section))
4145 {
4146 h->u.def.section->flags |= SEC_KEEP;
4147 }
4148 }
4149
4150 bfd_gc_sections (output_bfd, &link_info);
4151 }
4152
4153 void
4154 lang_process (void)
4155 {
4156 lang_reasonable_defaults ();
4157 current_target = default_target;
4158
4159 /* Open the output file. */
4160 lang_for_each_statement (ldlang_open_output);
4161 init_opb ();
4162
4163 ldemul_create_output_section_statements ();
4164
4165 /* Add to the hash table all undefineds on the command line. */
4166 lang_place_undefineds ();
4167
4168 already_linked_table_init ();
4169
4170 /* Create a bfd for each input file. */
4171 current_target = default_target;
4172 open_input_bfds (statement_list.head, FALSE);
4173
4174 link_info.gc_sym_list = &entry_symbol;
4175 if (entry_symbol.name == NULL)
4176 link_info.gc_sym_list = ldlang_undef_chain_list_head;
4177
4178 ldemul_after_open ();
4179
4180 already_linked_table_free ();
4181
4182 /* Make sure that we're not mixing architectures. We call this
4183 after all the input files have been opened, but before we do any
4184 other processing, so that any operations merge_private_bfd_data
4185 does on the output file will be known during the rest of the
4186 link. */
4187 lang_check ();
4188
4189 /* Handle .exports instead of a version script if we're told to do so. */
4190 if (command_line.version_exports_section)
4191 lang_do_version_exports_section ();
4192
4193 /* Build all sets based on the information gathered from the input
4194 files. */
4195 ldctor_build_sets ();
4196
4197 /* Remove unreferenced sections if asked to. */
4198 if (command_line.gc_sections)
4199 lang_gc_sections ();
4200
4201 /* If there were any SEC_MERGE sections, finish their merging, so that
4202 section sizes can be computed. This has to be done after GC of sections,
4203 so that GCed sections are not merged, but before assigning output
4204 sections, since removing whole input sections is hard then. */
4205 bfd_merge_sections (output_bfd, &link_info);
4206
4207 /* Size up the common data. */
4208 lang_common ();
4209
4210 /* Run through the contours of the script and attach input sections
4211 to the correct output sections. */
4212 map_input_to_output_sections (statement_list.head, NULL, NULL);
4213
4214 /* Find any sections not attached explicitly and handle them. */
4215 lang_place_orphans ();
4216
4217 if (! link_info.relocatable)
4218 {
4219 /* Look for a text section and set the readonly attribute in it. */
4220 asection *found = bfd_get_section_by_name (output_bfd, ".text");
4221
4222 if (found != NULL)
4223 {
4224 if (config.text_read_only)
4225 found->flags |= SEC_READONLY;
4226 else
4227 found->flags &= ~SEC_READONLY;
4228 }
4229 }
4230
4231 /* Do anything special before sizing sections. This is where ELF
4232 and other back-ends size dynamic sections. */
4233 ldemul_before_allocation ();
4234
4235 if (!link_info.relocatable)
4236 strip_excluded_output_sections ();
4237
4238 /* We must record the program headers before we try to fix the
4239 section positions, since they will affect SIZEOF_HEADERS. */
4240 lang_record_phdrs ();
4241
4242 /* Size up the sections. */
4243 lang_size_sections (statement_list.head, abs_output_section,
4244 &statement_list.head, 0, 0, NULL,
4245 command_line.relax ? FALSE : TRUE);
4246
4247 /* Now run around and relax if we can. */
4248 if (command_line.relax)
4249 {
4250 /* Keep relaxing until bfd_relax_section gives up. */
4251 bfd_boolean relax_again;
4252
4253 do
4254 {
4255 relax_again = FALSE;
4256
4257 /* Note: pe-dll.c does something like this also. If you find
4258 you need to change this code, you probably need to change
4259 pe-dll.c also. DJ */
4260
4261 /* Do all the assignments with our current guesses as to
4262 section sizes. */
4263 lang_do_assignments (statement_list.head, abs_output_section,
4264 NULL, 0);
4265
4266 /* We must do this after lang_do_assignments, because it uses
4267 _raw_size. */
4268 lang_reset_memory_regions ();
4269
4270 /* Perform another relax pass - this time we know where the
4271 globals are, so can make a better guess. */
4272 lang_size_sections (statement_list.head, abs_output_section,
4273 &statement_list.head, 0, 0, &relax_again, FALSE);
4274
4275 /* If the normal relax is done and the relax finalize pass
4276 is not performed yet, we perform another relax pass. */
4277 if (!relax_again && link_info.need_relax_finalize)
4278 {
4279 link_info.need_relax_finalize = FALSE;
4280 relax_again = TRUE;
4281 }
4282 }
4283 while (relax_again);
4284
4285 /* Final extra sizing to report errors. */
4286 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4287 lang_reset_memory_regions ();
4288 lang_size_sections (statement_list.head, abs_output_section,
4289 &statement_list.head, 0, 0, NULL, TRUE);
4290 }
4291
4292 /* See if anything special should be done now we know how big
4293 everything is. */
4294 ldemul_after_allocation ();
4295
4296 /* Fix any .startof. or .sizeof. symbols. */
4297 lang_set_startof ();
4298
4299 /* Do all the assignments, now that we know the final resting places
4300 of all the symbols. */
4301
4302 lang_do_assignments (statement_list.head, abs_output_section, NULL, 0);
4303
4304 /* Make sure that the section addresses make sense. */
4305 if (! link_info.relocatable
4306 && command_line.check_section_addresses)
4307 lang_check_section_addresses ();
4308
4309 /* Final stuffs. */
4310
4311 ldemul_finish ();
4312 lang_finish ();
4313 }
4314
4315 /* EXPORTED TO YACC */
4316
4317 void
4318 lang_add_wild (struct wildcard_spec *filespec,
4319 struct wildcard_list *section_list,
4320 bfd_boolean keep_sections)
4321 {
4322 struct wildcard_list *curr, *next;
4323 lang_wild_statement_type *new;
4324
4325 /* Reverse the list as the parser puts it back to front. */
4326 for (curr = section_list, section_list = NULL;
4327 curr != NULL;
4328 section_list = curr, curr = next)
4329 {
4330 if (curr->spec.name != NULL && strcmp (curr->spec.name, "COMMON") == 0)
4331 placed_commons = TRUE;
4332
4333 next = curr->next;
4334 curr->next = section_list;
4335 }
4336
4337 if (filespec != NULL && filespec->name != NULL)
4338 {
4339 if (strcmp (filespec->name, "*") == 0)
4340 filespec->name = NULL;
4341 else if (! wildcardp (filespec->name))
4342 lang_has_input_file = TRUE;
4343 }
4344
4345 new = new_stat (lang_wild_statement, stat_ptr);
4346 new->filename = NULL;
4347 new->filenames_sorted = FALSE;
4348 if (filespec != NULL)
4349 {
4350 new->filename = filespec->name;
4351 new->filenames_sorted = filespec->sorted;
4352 }
4353 new->section_list = section_list;
4354 new->keep_sections = keep_sections;
4355 lang_list_init (&new->children);
4356 }
4357
4358 void
4359 lang_section_start (const char *name, etree_type *address)
4360 {
4361 lang_address_statement_type *ad;
4362
4363 ad = new_stat (lang_address_statement, stat_ptr);
4364 ad->section_name = name;
4365 ad->address = address;
4366 }
4367
4368 /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
4369 because of a -e argument on the command line, or zero if this is
4370 called by ENTRY in a linker script. Command line arguments take
4371 precedence. */
4372
4373 void
4374 lang_add_entry (const char *name, bfd_boolean cmdline)
4375 {
4376 if (entry_symbol.name == NULL
4377 || cmdline
4378 || ! entry_from_cmdline)
4379 {
4380 entry_symbol.name = name;
4381 entry_from_cmdline = cmdline;
4382 }
4383 }
4384
4385 void
4386 lang_add_target (const char *name)
4387 {
4388 lang_target_statement_type *new = new_stat (lang_target_statement,
4389 stat_ptr);
4390
4391 new->target = name;
4392
4393 }
4394
4395 void
4396 lang_add_map (const char *name)
4397 {
4398 while (*name)
4399 {
4400 switch (*name)
4401 {
4402 case 'F':
4403 map_option_f = TRUE;
4404 break;
4405 }
4406 name++;
4407 }
4408 }
4409
4410 void
4411 lang_add_fill (fill_type *fill)
4412 {
4413 lang_fill_statement_type *new = new_stat (lang_fill_statement,
4414 stat_ptr);
4415
4416 new->fill = fill;
4417 }
4418
4419 void
4420 lang_add_data (int type, union etree_union *exp)
4421 {
4422
4423 lang_data_statement_type *new = new_stat (lang_data_statement,
4424 stat_ptr);
4425
4426 new->exp = exp;
4427 new->type = type;
4428
4429 }
4430
4431 /* Create a new reloc statement. RELOC is the BFD relocation type to
4432 generate. HOWTO is the corresponding howto structure (we could
4433 look this up, but the caller has already done so). SECTION is the
4434 section to generate a reloc against, or NAME is the name of the
4435 symbol to generate a reloc against. Exactly one of SECTION and
4436 NAME must be NULL. ADDEND is an expression for the addend. */
4437
4438 void
4439 lang_add_reloc (bfd_reloc_code_real_type reloc,
4440 reloc_howto_type *howto,
4441 asection *section,
4442 const char *name,
4443 union etree_union *addend)
4444 {
4445 lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
4446
4447 p->reloc = reloc;
4448 p->howto = howto;
4449 p->section = section;
4450 p->name = name;
4451 p->addend_exp = addend;
4452
4453 p->addend_value = 0;
4454 p->output_section = NULL;
4455 p->output_vma = 0;
4456 }
4457
4458 lang_assignment_statement_type *
4459 lang_add_assignment (etree_type *exp)
4460 {
4461 lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
4462 stat_ptr);
4463
4464 new->exp = exp;
4465 return new;
4466 }
4467
4468 void
4469 lang_add_attribute (enum statement_enum attribute)
4470 {
4471 new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
4472 }
4473
4474 void
4475 lang_startup (const char *name)
4476 {
4477 if (startup_file != NULL)
4478 {
4479 einfo (_("%P%Fmultiple STARTUP files\n"));
4480 }
4481 first_file->filename = name;
4482 first_file->local_sym_name = name;
4483 first_file->real = TRUE;
4484
4485 startup_file = name;
4486 }
4487
4488 void
4489 lang_float (bfd_boolean maybe)
4490 {
4491 lang_float_flag = maybe;
4492 }
4493
4494
4495 /* Work out the load- and run-time regions from a script statement, and
4496 store them in *LMA_REGION and *REGION respectively.
4497
4498 MEMSPEC is the name of the run-time region, or the value of
4499 DEFAULT_MEMORY_REGION if the statement didn't specify one.
4500 LMA_MEMSPEC is the name of the load-time region, or null if the
4501 statement didn't specify one.HAVE_LMA_P is TRUE if the statement
4502 had an explicit load address.
4503
4504 It is an error to specify both a load region and a load address. */
4505
4506 static void
4507 lang_get_regions (lang_memory_region_type **region,
4508 lang_memory_region_type **lma_region,
4509 const char *memspec,
4510 const char *lma_memspec,
4511 bfd_boolean have_lma,
4512 bfd_boolean have_vma)
4513 {
4514 *lma_region = lang_memory_region_lookup (lma_memspec, FALSE);
4515
4516 /* If no runtime region or VMA has been specified, but the load region has
4517 been specified, then use the load region for the runtime region as well. */
4518 if (lma_memspec != NULL
4519 && ! have_vma
4520 && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
4521 *region = *lma_region;
4522 else
4523 *region = lang_memory_region_lookup (memspec, FALSE);
4524
4525 if (have_lma && lma_memspec != 0)
4526 einfo (_("%X%P:%S: section has both a load address and a load region\n"));
4527 }
4528
4529 void
4530 lang_leave_output_section_statement (fill_type *fill, const char *memspec,
4531 lang_output_section_phdr_list *phdrs,
4532 const char *lma_memspec)
4533 {
4534 lang_get_regions (&current_section->region,
4535 &current_section->lma_region,
4536 memspec, lma_memspec,
4537 current_section->load_base != NULL,
4538 current_section->addr_tree != NULL);
4539 current_section->fill = fill;
4540 current_section->phdrs = phdrs;
4541 stat_ptr = &statement_list;
4542 }
4543
4544 /* Create an absolute symbol with the given name with the value of the
4545 address of first byte of the section named.
4546
4547 If the symbol already exists, then do nothing. */
4548
4549 void
4550 lang_abs_symbol_at_beginning_of (const char *secname, const char *name)
4551 {
4552 struct bfd_link_hash_entry *h;
4553
4554 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4555 if (h == NULL)
4556 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4557
4558 if (h->type == bfd_link_hash_new
4559 || h->type == bfd_link_hash_undefined)
4560 {
4561 asection *sec;
4562
4563 h->type = bfd_link_hash_defined;
4564
4565 sec = bfd_get_section_by_name (output_bfd, secname);
4566 if (sec == NULL)
4567 h->u.def.value = 0;
4568 else
4569 h->u.def.value = bfd_get_section_vma (output_bfd, sec);
4570
4571 h->u.def.section = bfd_abs_section_ptr;
4572 }
4573 }
4574
4575 /* Create an absolute symbol with the given name with the value of the
4576 address of the first byte after the end of the section named.
4577
4578 If the symbol already exists, then do nothing. */
4579
4580 void
4581 lang_abs_symbol_at_end_of (const char *secname, const char *name)
4582 {
4583 struct bfd_link_hash_entry *h;
4584
4585 h = bfd_link_hash_lookup (link_info.hash, name, TRUE, TRUE, TRUE);
4586 if (h == NULL)
4587 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
4588
4589 if (h->type == bfd_link_hash_new
4590 || h->type == bfd_link_hash_undefined)
4591 {
4592 asection *sec;
4593
4594 h->type = bfd_link_hash_defined;
4595
4596 sec = bfd_get_section_by_name (output_bfd, secname);
4597 if (sec == NULL)
4598 h->u.def.value = 0;
4599 else
4600 h->u.def.value = (bfd_get_section_vma (output_bfd, sec)
4601 + TO_ADDR (bfd_section_size (output_bfd, sec)));
4602
4603 h->u.def.section = bfd_abs_section_ptr;
4604 }
4605 }
4606
4607 void
4608 lang_statement_append (lang_statement_list_type *list,
4609 lang_statement_union_type *element,
4610 lang_statement_union_type **field)
4611 {
4612 *(list->tail) = element;
4613 list->tail = field;
4614 }
4615
4616 /* Set the output format type. -oformat overrides scripts. */
4617
4618 void
4619 lang_add_output_format (const char *format,
4620 const char *big,
4621 const char *little,
4622 int from_script)
4623 {
4624 if (output_target == NULL || !from_script)
4625 {
4626 if (command_line.endian == ENDIAN_BIG
4627 && big != NULL)
4628 format = big;
4629 else if (command_line.endian == ENDIAN_LITTLE
4630 && little != NULL)
4631 format = little;
4632
4633 output_target = format;
4634 }
4635 }
4636
4637 /* Enter a group. This creates a new lang_group_statement, and sets
4638 stat_ptr to build new statements within the group. */
4639
4640 void
4641 lang_enter_group (void)
4642 {
4643 lang_group_statement_type *g;
4644
4645 g = new_stat (lang_group_statement, stat_ptr);
4646 lang_list_init (&g->children);
4647 stat_ptr = &g->children;
4648 }
4649
4650 /* Leave a group. This just resets stat_ptr to start writing to the
4651 regular list of statements again. Note that this will not work if
4652 groups can occur inside anything else which can adjust stat_ptr,
4653 but currently they can't. */
4654
4655 void
4656 lang_leave_group (void)
4657 {
4658 stat_ptr = &statement_list;
4659 }
4660
4661 /* Add a new program header. This is called for each entry in a PHDRS
4662 command in a linker script. */
4663
4664 void
4665 lang_new_phdr (const char *name,
4666 etree_type *type,
4667 bfd_boolean filehdr,
4668 bfd_boolean phdrs,
4669 etree_type *at,
4670 etree_type *flags)
4671 {
4672 struct lang_phdr *n, **pp;
4673
4674 n = stat_alloc (sizeof (struct lang_phdr));
4675 n->next = NULL;
4676 n->name = name;
4677 n->type = exp_get_value_int (type, 0, "program header type",
4678 lang_final_phase_enum);
4679 n->filehdr = filehdr;
4680 n->phdrs = phdrs;
4681 n->at = at;
4682 n->flags = flags;
4683
4684 for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
4685 ;
4686 *pp = n;
4687 }
4688
4689 /* Record the program header information in the output BFD. FIXME: We
4690 should not be calling an ELF specific function here. */
4691
4692 static void
4693 lang_record_phdrs (void)
4694 {
4695 unsigned int alc;
4696 asection **secs;
4697 lang_output_section_phdr_list *last;
4698 struct lang_phdr *l;
4699 lang_statement_union_type *u;
4700
4701 alc = 10;
4702 secs = xmalloc (alc * sizeof (asection *));
4703 last = NULL;
4704 for (l = lang_phdr_list; l != NULL; l = l->next)
4705 {
4706 unsigned int c;
4707 flagword flags;
4708 bfd_vma at;
4709
4710 c = 0;
4711 for (u = lang_output_section_statement.head;
4712 u != NULL;
4713 u = u->output_section_statement.next)
4714 {
4715 lang_output_section_statement_type *os;
4716 lang_output_section_phdr_list *pl;
4717
4718 os = &u->output_section_statement;
4719
4720 pl = os->phdrs;
4721 if (pl != NULL)
4722 last = pl;
4723 else
4724 {
4725 if (os->sectype == noload_section
4726 || os->bfd_section == NULL
4727 || (os->bfd_section->flags & SEC_ALLOC) == 0)
4728 continue;
4729 pl = last;
4730 }
4731
4732 if (os->bfd_section == NULL)
4733 continue;
4734
4735 for (; pl != NULL; pl = pl->next)
4736 {
4737 if (strcmp (pl->name, l->name) == 0)
4738 {
4739 if (c >= alc)
4740 {
4741 alc *= 2;
4742 secs = xrealloc (secs, alc * sizeof (asection *));
4743 }
4744 secs[c] = os->bfd_section;
4745 ++c;
4746 pl->used = TRUE;
4747 }
4748 }
4749 }
4750
4751 if (l->flags == NULL)
4752 flags = 0;
4753 else
4754 flags = exp_get_vma (l->flags, 0, "phdr flags",
4755 lang_final_phase_enum);
4756
4757 if (l->at == NULL)
4758 at = 0;
4759 else
4760 at = exp_get_vma (l->at, 0, "phdr load address",
4761 lang_final_phase_enum);
4762
4763 if (! bfd_record_phdr (output_bfd, l->type,
4764 l->flags != NULL, flags, l->at != NULL,
4765 at, l->filehdr, l->phdrs, c, secs))
4766 einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
4767 }
4768
4769 free (secs);
4770
4771 /* Make sure all the phdr assignments succeeded. */
4772 for (u = lang_output_section_statement.head;
4773 u != NULL;
4774 u = u->output_section_statement.next)
4775 {
4776 lang_output_section_phdr_list *pl;
4777
4778 if (u->output_section_statement.bfd_section == NULL)
4779 continue;
4780
4781 for (pl = u->output_section_statement.phdrs;
4782 pl != NULL;
4783 pl = pl->next)
4784 if (! pl->used && strcmp (pl->name, "NONE") != 0)
4785 einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
4786 u->output_section_statement.name, pl->name);
4787 }
4788 }
4789
4790 /* Record a list of sections which may not be cross referenced. */
4791
4792 void
4793 lang_add_nocrossref (lang_nocrossref_type *l)
4794 {
4795 struct lang_nocrossrefs *n;
4796
4797 n = xmalloc (sizeof *n);
4798 n->next = nocrossref_list;
4799 n->list = l;
4800 nocrossref_list = n;
4801
4802 /* Set notice_all so that we get informed about all symbols. */
4803 link_info.notice_all = TRUE;
4804 }
4805 \f
4806 /* Overlay handling. We handle overlays with some static variables. */
4807
4808 /* The overlay virtual address. */
4809 static etree_type *overlay_vma;
4810 /* And subsection alignment. */
4811 static etree_type *overlay_subalign;
4812
4813 /* An expression for the maximum section size seen so far. */
4814 static etree_type *overlay_max;
4815
4816 /* A list of all the sections in this overlay. */
4817
4818 struct overlay_list {
4819 struct overlay_list *next;
4820 lang_output_section_statement_type *os;
4821 };
4822
4823 static struct overlay_list *overlay_list;
4824
4825 /* Start handling an overlay. */
4826
4827 void
4828 lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
4829 {
4830 /* The grammar should prevent nested overlays from occurring. */
4831 ASSERT (overlay_vma == NULL
4832 && overlay_subalign == NULL
4833 && overlay_max == NULL);
4834
4835 overlay_vma = vma_expr;
4836 overlay_subalign = subalign;
4837 }
4838
4839 /* Start a section in an overlay. We handle this by calling
4840 lang_enter_output_section_statement with the correct VMA.
4841 lang_leave_overlay sets up the LMA and memory regions. */
4842
4843 void
4844 lang_enter_overlay_section (const char *name)
4845 {
4846 struct overlay_list *n;
4847 etree_type *size;
4848
4849 lang_enter_output_section_statement (name, overlay_vma, normal_section,
4850 0, overlay_subalign, 0);
4851
4852 /* If this is the first section, then base the VMA of future
4853 sections on this one. This will work correctly even if `.' is
4854 used in the addresses. */
4855 if (overlay_list == NULL)
4856 overlay_vma = exp_nameop (ADDR, name);
4857
4858 /* Remember the section. */
4859 n = xmalloc (sizeof *n);
4860 n->os = current_section;
4861 n->next = overlay_list;
4862 overlay_list = n;
4863
4864 size = exp_nameop (SIZEOF, name);
4865
4866 /* Arrange to work out the maximum section end address. */
4867 if (overlay_max == NULL)
4868 overlay_max = size;
4869 else
4870 overlay_max = exp_binop (MAX_K, overlay_max, size);
4871 }
4872
4873 /* Finish a section in an overlay. There isn't any special to do
4874 here. */
4875
4876 void
4877 lang_leave_overlay_section (fill_type *fill,
4878 lang_output_section_phdr_list *phdrs)
4879 {
4880 const char *name;
4881 char *clean, *s2;
4882 const char *s1;
4883 char *buf;
4884
4885 name = current_section->name;
4886
4887 /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
4888 region and that no load-time region has been specified. It doesn't
4889 really matter what we say here, since lang_leave_overlay will
4890 override it. */
4891 lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
4892
4893 /* Define the magic symbols. */
4894
4895 clean = xmalloc (strlen (name) + 1);
4896 s2 = clean;
4897 for (s1 = name; *s1 != '\0'; s1++)
4898 if (ISALNUM (*s1) || *s1 == '_')
4899 *s2++ = *s1;
4900 *s2 = '\0';
4901
4902 buf = xmalloc (strlen (clean) + sizeof "__load_start_");
4903 sprintf (buf, "__load_start_%s", clean);
4904 lang_add_assignment (exp_assop ('=', buf,
4905 exp_nameop (LOADADDR, name)));
4906
4907 buf = xmalloc (strlen (clean) + sizeof "__load_stop_");
4908 sprintf (buf, "__load_stop_%s", clean);
4909 lang_add_assignment (exp_assop ('=', buf,
4910 exp_binop ('+',
4911 exp_nameop (LOADADDR, name),
4912 exp_nameop (SIZEOF, name))));
4913
4914 free (clean);
4915 }
4916
4917 /* Finish an overlay. If there are any overlay wide settings, this
4918 looks through all the sections in the overlay and sets them. */
4919
4920 void
4921 lang_leave_overlay (etree_type *lma_expr,
4922 int nocrossrefs,
4923 fill_type *fill,
4924 const char *memspec,
4925 lang_output_section_phdr_list *phdrs,
4926 const char *lma_memspec)
4927 {
4928 lang_memory_region_type *region;
4929 lang_memory_region_type *lma_region;
4930 struct overlay_list *l;
4931 lang_nocrossref_type *nocrossref;
4932
4933 lang_get_regions (&region, &lma_region,
4934 memspec, lma_memspec,
4935 lma_expr != NULL, FALSE);
4936
4937 nocrossref = NULL;
4938
4939 /* After setting the size of the last section, set '.' to end of the
4940 overlay region. */
4941 if (overlay_list != NULL)
4942 overlay_list->os->update_dot_tree
4943 = exp_assop ('=', ".", exp_binop ('+', overlay_vma, overlay_max));
4944
4945 l = overlay_list;
4946 while (l != NULL)
4947 {
4948 struct overlay_list *next;
4949
4950 if (fill != NULL && l->os->fill == NULL)
4951 l->os->fill = fill;
4952
4953 l->os->region = region;
4954 l->os->lma_region = lma_region;
4955
4956 /* The first section has the load address specified in the
4957 OVERLAY statement. The rest are worked out from that.
4958 The base address is not needed (and should be null) if
4959 an LMA region was specified. */
4960 if (l->next == 0)
4961 l->os->load_base = lma_expr;
4962 else if (lma_region == 0)
4963 l->os->load_base = exp_binop ('+',
4964 exp_nameop (LOADADDR, l->next->os->name),
4965 exp_nameop (SIZEOF, l->next->os->name));
4966
4967 if (phdrs != NULL && l->os->phdrs == NULL)
4968 l->os->phdrs = phdrs;
4969
4970 if (nocrossrefs)
4971 {
4972 lang_nocrossref_type *nc;
4973
4974 nc = xmalloc (sizeof *nc);
4975 nc->name = l->os->name;
4976 nc->next = nocrossref;
4977 nocrossref = nc;
4978 }
4979
4980 next = l->next;
4981 free (l);
4982 l = next;
4983 }
4984
4985 if (nocrossref != NULL)
4986 lang_add_nocrossref (nocrossref);
4987
4988 overlay_vma = NULL;
4989 overlay_list = NULL;
4990 overlay_max = NULL;
4991 }
4992 \f
4993 /* Version handling. This is only useful for ELF. */
4994
4995 /* This global variable holds the version tree that we build. */
4996
4997 struct bfd_elf_version_tree *lang_elf_version_info;
4998
4999 /* If PREV is NULL, return first version pattern matching particular symbol.
5000 If PREV is non-NULL, return first version pattern matching particular
5001 symbol after PREV (previously returned by lang_vers_match). */
5002
5003 static struct bfd_elf_version_expr *
5004 lang_vers_match (struct bfd_elf_version_expr_head *head,
5005 struct bfd_elf_version_expr *prev,
5006 const char *sym)
5007 {
5008 const char *cxx_sym = sym;
5009 const char *java_sym = sym;
5010 struct bfd_elf_version_expr *expr = NULL;
5011
5012 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5013 {
5014 cxx_sym = cplus_demangle (sym, DMGL_PARAMS | DMGL_ANSI);
5015 if (!cxx_sym)
5016 cxx_sym = sym;
5017 }
5018 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5019 {
5020 java_sym = cplus_demangle (sym, DMGL_JAVA);
5021 if (!java_sym)
5022 java_sym = sym;
5023 }
5024
5025 if (head->htab && (prev == NULL || prev->symbol))
5026 {
5027 struct bfd_elf_version_expr e;
5028
5029 switch (prev ? prev->mask : 0)
5030 {
5031 case 0:
5032 if (head->mask & BFD_ELF_VERSION_C_TYPE)
5033 {
5034 e.symbol = sym;
5035 expr = htab_find (head->htab, &e);
5036 while (expr && strcmp (expr->symbol, sym) == 0)
5037 if (expr->mask == BFD_ELF_VERSION_C_TYPE)
5038 goto out_ret;
5039 else
5040 expr = expr->next;
5041 }
5042 /* Fallthrough */
5043 case BFD_ELF_VERSION_C_TYPE:
5044 if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
5045 {
5046 e.symbol = cxx_sym;
5047 expr = htab_find (head->htab, &e);
5048 while (expr && strcmp (expr->symbol, cxx_sym) == 0)
5049 if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5050 goto out_ret;
5051 else
5052 expr = expr->next;
5053 }
5054 /* Fallthrough */
5055 case BFD_ELF_VERSION_CXX_TYPE:
5056 if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
5057 {
5058 e.symbol = java_sym;
5059 expr = htab_find (head->htab, &e);
5060 while (expr && strcmp (expr->symbol, java_sym) == 0)
5061 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5062 goto out_ret;
5063 else
5064 expr = expr->next;
5065 }
5066 /* Fallthrough */
5067 default:
5068 break;
5069 }
5070 }
5071
5072 /* Finally, try the wildcards. */
5073 if (prev == NULL || prev->symbol)
5074 expr = head->remaining;
5075 else
5076 expr = prev->next;
5077 while (expr)
5078 {
5079 const char *s;
5080
5081 if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
5082 break;
5083
5084 if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
5085 s = java_sym;
5086 else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
5087 s = cxx_sym;
5088 else
5089 s = sym;
5090 if (fnmatch (expr->pattern, s, 0) == 0)
5091 break;
5092 expr = expr->next;
5093 }
5094
5095 out_ret:
5096 if (cxx_sym != sym)
5097 free ((char *) cxx_sym);
5098 if (java_sym != sym)
5099 free ((char *) java_sym);
5100 return expr;
5101 }
5102
5103 /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
5104 return a string pointing to the symbol name. */
5105
5106 static const char *
5107 realsymbol (const char *pattern)
5108 {
5109 const char *p;
5110 bfd_boolean changed = FALSE, backslash = FALSE;
5111 char *s, *symbol = xmalloc (strlen (pattern) + 1);
5112
5113 for (p = pattern, s = symbol; *p != '\0'; ++p)
5114 {
5115 /* It is a glob pattern only if there is no preceding
5116 backslash. */
5117 if (! backslash && (*p == '?' || *p == '*' || *p == '['))
5118 {
5119 free (symbol);
5120 return NULL;
5121 }
5122
5123 if (backslash)
5124 {
5125 /* Remove the preceding backslash. */
5126 *(s - 1) = *p;
5127 changed = TRUE;
5128 }
5129 else
5130 *s++ = *p;
5131
5132 backslash = *p == '\\';
5133 }
5134
5135 if (changed)
5136 {
5137 *s = '\0';
5138 return symbol;
5139 }
5140 else
5141 {
5142 free (symbol);
5143 return pattern;
5144 }
5145 }
5146
5147 /* This is called for each variable name or match expression. */
5148
5149 struct bfd_elf_version_expr *
5150 lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
5151 const char *new,
5152 const char *lang)
5153 {
5154 struct bfd_elf_version_expr *ret;
5155
5156 ret = xmalloc (sizeof *ret);
5157 ret->next = orig;
5158 ret->pattern = new;
5159 ret->symver = 0;
5160 ret->script = 0;
5161 ret->symbol = realsymbol (new);
5162
5163 if (lang == NULL || strcasecmp (lang, "C") == 0)
5164 ret->mask = BFD_ELF_VERSION_C_TYPE;
5165 else if (strcasecmp (lang, "C++") == 0)
5166 ret->mask = BFD_ELF_VERSION_CXX_TYPE;
5167 else if (strcasecmp (lang, "Java") == 0)
5168 ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
5169 else
5170 {
5171 einfo (_("%X%P: unknown language `%s' in version information\n"),
5172 lang);
5173 ret->mask = BFD_ELF_VERSION_C_TYPE;
5174 }
5175
5176 return ldemul_new_vers_pattern (ret);
5177 }
5178
5179 /* This is called for each set of variable names and match
5180 expressions. */
5181
5182 struct bfd_elf_version_tree *
5183 lang_new_vers_node (struct bfd_elf_version_expr *globals,
5184 struct bfd_elf_version_expr *locals)
5185 {
5186 struct bfd_elf_version_tree *ret;
5187
5188 ret = xcalloc (1, sizeof *ret);
5189 ret->globals.list = globals;
5190 ret->locals.list = locals;
5191 ret->match = lang_vers_match;
5192 ret->name_indx = (unsigned int) -1;
5193 return ret;
5194 }
5195
5196 /* This static variable keeps track of version indices. */
5197
5198 static int version_index;
5199
5200 static hashval_t
5201 version_expr_head_hash (const void *p)
5202 {
5203 const struct bfd_elf_version_expr *e = p;
5204
5205 return htab_hash_string (e->symbol);
5206 }
5207
5208 static int
5209 version_expr_head_eq (const void *p1, const void *p2)
5210 {
5211 const struct bfd_elf_version_expr *e1 = p1;
5212 const struct bfd_elf_version_expr *e2 = p2;
5213
5214 return strcmp (e1->symbol, e2->symbol) == 0;
5215 }
5216
5217 static void
5218 lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
5219 {
5220 size_t count = 0;
5221 struct bfd_elf_version_expr *e, *next;
5222 struct bfd_elf_version_expr **list_loc, **remaining_loc;
5223
5224 for (e = head->list; e; e = e->next)
5225 {
5226 if (e->symbol)
5227 count++;
5228 head->mask |= e->mask;
5229 }
5230
5231 if (count)
5232 {
5233 head->htab = htab_create (count * 2, version_expr_head_hash,
5234 version_expr_head_eq, NULL);
5235 list_loc = &head->list;
5236 remaining_loc = &head->remaining;
5237 for (e = head->list; e; e = next)
5238 {
5239 next = e->next;
5240 if (!e->symbol)
5241 {
5242 *remaining_loc = e;
5243 remaining_loc = &e->next;
5244 }
5245 else
5246 {
5247 void **loc = htab_find_slot (head->htab, e, INSERT);
5248
5249 if (*loc)
5250 {
5251 struct bfd_elf_version_expr *e1, *last;
5252
5253 e1 = *loc;
5254 last = NULL;
5255 do
5256 {
5257 if (e1->mask == e->mask)
5258 {
5259 last = NULL;
5260 break;
5261 }
5262 last = e1;
5263 e1 = e1->next;
5264 }
5265 while (e1 && strcmp (e1->symbol, e->symbol) == 0);
5266
5267 if (last == NULL)
5268 {
5269 /* This is a duplicate. */
5270 /* FIXME: Memory leak. Sometimes pattern is not
5271 xmalloced alone, but in larger chunk of memory. */
5272 /* free (e->symbol); */
5273 free (e);
5274 }
5275 else
5276 {
5277 e->next = last->next;
5278 last->next = e;
5279 }
5280 }
5281 else
5282 {
5283 *loc = e;
5284 *list_loc = e;
5285 list_loc = &e->next;
5286 }
5287 }
5288 }
5289 *remaining_loc = NULL;
5290 *list_loc = head->remaining;
5291 }
5292 else
5293 head->remaining = head->list;
5294 }
5295
5296 /* This is called when we know the name and dependencies of the
5297 version. */
5298
5299 void
5300 lang_register_vers_node (const char *name,
5301 struct bfd_elf_version_tree *version,
5302 struct bfd_elf_version_deps *deps)
5303 {
5304 struct bfd_elf_version_tree *t, **pp;
5305 struct bfd_elf_version_expr *e1;
5306
5307 if (name == NULL)
5308 name = "";
5309
5310 if ((name[0] == '\0' && lang_elf_version_info != NULL)
5311 || (lang_elf_version_info && lang_elf_version_info->name[0] == '\0'))
5312 {
5313 einfo (_("%X%P: anonymous version tag cannot be combined with other version tags\n"));
5314 free (version);
5315 return;
5316 }
5317
5318 /* Make sure this node has a unique name. */
5319 for (t = lang_elf_version_info; t != NULL; t = t->next)
5320 if (strcmp (t->name, name) == 0)
5321 einfo (_("%X%P: duplicate version tag `%s'\n"), name);
5322
5323 lang_finalize_version_expr_head (&version->globals);
5324 lang_finalize_version_expr_head (&version->locals);
5325
5326 /* Check the global and local match names, and make sure there
5327 aren't any duplicates. */
5328
5329 for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
5330 {
5331 for (t = lang_elf_version_info; t != NULL; t = t->next)
5332 {
5333 struct bfd_elf_version_expr *e2;
5334
5335 if (t->locals.htab && e1->symbol)
5336 {
5337 e2 = htab_find (t->locals.htab, e1);
5338 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5339 {
5340 if (e1->mask == e2->mask)
5341 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5342 e1->symbol);
5343 e2 = e2->next;
5344 }
5345 }
5346 else if (!e1->symbol)
5347 for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
5348 if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5349 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5350 e1->pattern);
5351 }
5352 }
5353
5354 for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
5355 {
5356 for (t = lang_elf_version_info; t != NULL; t = t->next)
5357 {
5358 struct bfd_elf_version_expr *e2;
5359
5360 if (t->globals.htab && e1->symbol)
5361 {
5362 e2 = htab_find (t->globals.htab, e1);
5363 while (e2 && strcmp (e1->symbol, e2->symbol) == 0)
5364 {
5365 if (e1->mask == e2->mask)
5366 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5367 e1->symbol);
5368 e2 = e2->next;
5369 }
5370 }
5371 else if (!e1->symbol)
5372 for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
5373 if (strcmp (e1->pattern, e2->pattern) == 0 && e1->mask == e2->mask)
5374 einfo (_("%X%P: duplicate expression `%s' in version information\n"),
5375 e1->pattern);
5376 }
5377 }
5378
5379 version->deps = deps;
5380 version->name = name;
5381 if (name[0] != '\0')
5382 {
5383 ++version_index;
5384 version->vernum = version_index;
5385 }
5386 else
5387 version->vernum = 0;
5388
5389 for (pp = &lang_elf_version_info; *pp != NULL; pp = &(*pp)->next)
5390 ;
5391 *pp = version;
5392 }
5393
5394 /* This is called when we see a version dependency. */
5395
5396 struct bfd_elf_version_deps *
5397 lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
5398 {
5399 struct bfd_elf_version_deps *ret;
5400 struct bfd_elf_version_tree *t;
5401
5402 ret = xmalloc (sizeof *ret);
5403 ret->next = list;
5404
5405 for (t = lang_elf_version_info; t != NULL; t = t->next)
5406 {
5407 if (strcmp (t->name, name) == 0)
5408 {
5409 ret->version_needed = t;
5410 return ret;
5411 }
5412 }
5413
5414 einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
5415
5416 return ret;
5417 }
5418
5419 static void
5420 lang_do_version_exports_section (void)
5421 {
5422 struct bfd_elf_version_expr *greg = NULL, *lreg;
5423
5424 LANG_FOR_EACH_INPUT_STATEMENT (is)
5425 {
5426 asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
5427 char *contents, *p;
5428 bfd_size_type len;
5429
5430 if (sec == NULL)
5431 continue;
5432
5433 len = bfd_section_size (is->the_bfd, sec);
5434 contents = xmalloc (len);
5435 if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
5436 einfo (_("%X%P: unable to read .exports section contents\n"), sec);
5437
5438 p = contents;
5439 while (p < contents + len)
5440 {
5441 greg = lang_new_vers_pattern (greg, p, NULL);
5442 p = strchr (p, '\0') + 1;
5443 }
5444
5445 /* Do not free the contents, as we used them creating the regex. */
5446
5447 /* Do not include this section in the link. */
5448 bfd_set_section_flags (is->the_bfd, sec,
5449 bfd_get_section_flags (is->the_bfd, sec) | SEC_EXCLUDE);
5450 }
5451
5452 lreg = lang_new_vers_pattern (NULL, "*", NULL);
5453 lang_register_vers_node (command_line.version_exports_section,
5454 lang_new_vers_node (greg, lreg), NULL);
5455 }
5456
5457 void
5458 lang_add_unique (const char *name)
5459 {
5460 struct unique_sections *ent;
5461
5462 for (ent = unique_section_list; ent; ent = ent->next)
5463 if (strcmp (ent->name, name) == 0)
5464 return;
5465
5466 ent = xmalloc (sizeof *ent);
5467 ent->name = xstrdup (name);
5468 ent->next = unique_section_list;
5469 unique_section_list = ent;
5470 }