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