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