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