Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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