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