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