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