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