OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.18.50/] [ld/] [ldlang.c] - Blame information for rev 303

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.