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.20.1/] [ld/] [emultempl/] [xtensaelf.em] - Blame information for rev 337

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

Line No. Rev Author Line
1 205 julius
# This shell script emits a C file. -*- C -*-
2
#   Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009
3
#   Free Software Foundation, Inc.
4
#
5
# This file is part of the GNU Binutils.
6
#
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
# MA 02110-1301, USA.
21
#
22
 
23
# This file is sourced from elf32.em, and defines extra xtensa-elf
24
# specific routines.
25
#
26
fragment <
27
 
28
#include 
29
#include "../bfd/elf-bfd.h"
30
#include "../bfd/libbfd.h"
31
#include "elf/xtensa.h"
32
#include "bfd.h"
33
 
34
/* Provide default values for new configuration settings.  */
35
#ifndef XSHAL_ABI
36
#define XSHAL_ABI 0
37
#endif
38
 
39
static void xtensa_wild_group_interleave (lang_statement_union_type *);
40
static void xtensa_colocate_output_literals (lang_statement_union_type *);
41
static void xtensa_strip_inconsistent_linkonce_sections
42
  (lang_statement_list_type *);
43
 
44
 
45
/* Flag for the emulation-specific "--no-relax" option.  */
46
static bfd_boolean disable_relaxation = FALSE;
47
 
48
/* This number is irrelevant until we turn on use_literal_pages */
49
static bfd_vma xtensa_page_power = 12; /* 4K pages.  */
50
 
51
/* To force a page break between literals and text, change
52
   xtensa_use_literal_pages to "TRUE".  */
53
static bfd_boolean xtensa_use_literal_pages = FALSE;
54
 
55
#define EXTRA_VALIDATION 0
56
 
57
 
58
static char *
59
elf_xtensa_choose_target (int argc ATTRIBUTE_UNUSED,
60
                          char **argv ATTRIBUTE_UNUSED)
61
{
62
  if (XCHAL_HAVE_BE)
63
    return "${BIG_OUTPUT_FORMAT}";
64
  else
65
    return "${LITTLE_OUTPUT_FORMAT}";
66
}
67
 
68
 
69
static void
70
elf_xtensa_before_parse (void)
71
{
72
  /* Just call the default hook.... Tensilica's version of this function
73
     does some other work that isn't relevant here.  */
74
  gld${EMULATION_NAME}_before_parse ();
75
}
76
 
77
 
78
static void
79
remove_section (bfd *abfd, asection *os)
80
{
81
  asection **spp;
82
  for (spp = &abfd->sections; *spp; spp = &(*spp)->next)
83
    if (*spp == os)
84
      {
85
        *spp = os->next;
86
        os->owner->section_count--;
87
        break;
88
      }
89
}
90
 
91
 
92
static bfd_boolean
93
replace_insn_sec_with_prop_sec (bfd *abfd,
94
                                const char *insn_sec_name,
95
                                const char *prop_sec_name,
96
                                char **error_message)
97
{
98
  asection *insn_sec;
99
  asection *prop_sec;
100
  bfd_byte *prop_contents = NULL;
101
  bfd_byte *insn_contents = NULL;
102
  unsigned entry_count;
103
  unsigned entry;
104
  Elf_Internal_Shdr *symtab_hdr;
105
  Elf_Internal_Rela *internal_relocs = NULL;
106
  unsigned reloc_count;
107
 
108
  *error_message = "";
109
  insn_sec = bfd_get_section_by_name (abfd, insn_sec_name);
110
  if (insn_sec == NULL)
111
    return TRUE;
112
  entry_count = insn_sec->size / 8;
113
 
114
  prop_sec = bfd_get_section_by_name (abfd, prop_sec_name);
115
  if (prop_sec != NULL && insn_sec != NULL)
116
    {
117
      *error_message = _("file already has property tables");
118
      return FALSE;
119
    }
120
 
121
  if (insn_sec->size != 0)
122
    {
123
      insn_contents = (bfd_byte *) bfd_malloc (insn_sec->size);
124
      if (insn_contents == NULL)
125
        {
126
          *error_message = _("out of memory");
127
          goto cleanup;
128
        }
129
      if (! bfd_get_section_contents (abfd, insn_sec, insn_contents,
130
                                      (file_ptr) 0, insn_sec->size))
131
        {
132
          *error_message = _("failed to read section contents");
133
          goto cleanup;
134
        }
135
    }
136
 
137
  /* Create a property table section for it.  */
138
  prop_sec_name = strdup (prop_sec_name);
139
  prop_sec = bfd_make_section_with_flags
140
    (abfd, prop_sec_name, bfd_get_section_flags (abfd, insn_sec));
141
  if (prop_sec == NULL
142
      || ! bfd_set_section_alignment (abfd, prop_sec, 2))
143
    {
144
      *error_message = _("could not create new section");
145
      goto cleanup;
146
    }
147
 
148
  prop_sec->size = entry_count * 12;
149
  prop_contents = (bfd_byte *) bfd_zalloc (abfd, prop_sec->size);
150
  elf_section_data (prop_sec)->this_hdr.contents = prop_contents;
151
 
152
  /* The entry size and size must be set to allow the linker to compute
153
     the number of relocations since it does not use reloc_count.  */
154
  elf_section_data (prop_sec)->rel_hdr.sh_entsize =
155
    sizeof (Elf32_External_Rela);
156
  elf_section_data (prop_sec)->rel_hdr.sh_size =
157
    elf_section_data (insn_sec)->rel_hdr.sh_size;
158
 
159
  if (prop_contents == NULL && prop_sec->size != 0)
160
    {
161
      *error_message = _("could not allocate section contents");
162
      goto cleanup;
163
    }
164
 
165
  /* Read the relocations.  */
166
  reloc_count = insn_sec->reloc_count;
167
  if (reloc_count != 0)
168
    {
169
      /* If there is already an internal_reloc, then save it so that the
170
         read_relocs function freshly allocates a copy.  */
171
      Elf_Internal_Rela *saved_relocs = elf_section_data (insn_sec)->relocs;
172
 
173
      elf_section_data (insn_sec)->relocs = NULL;
174
      internal_relocs =
175
        _bfd_elf_link_read_relocs (abfd, insn_sec, NULL, NULL, FALSE);
176
      elf_section_data (insn_sec)->relocs = saved_relocs;
177
 
178
      if (internal_relocs == NULL)
179
        {
180
          *error_message = _("out of memory");
181
          goto cleanup;
182
        }
183
    }
184
 
185
  /* Create a relocation section for the property section.  */
186
  if (internal_relocs != NULL)
187
    {
188
      elf_section_data (prop_sec)->relocs = internal_relocs;
189
      prop_sec->reloc_count = reloc_count;
190
    }
191
 
192
  /* Now copy each insn table entry to the prop table entry with
193
     appropriate flags.  */
194
  for (entry = 0; entry < entry_count; ++entry)
195
    {
196
      unsigned value;
197
      unsigned flags = (XTENSA_PROP_INSN | XTENSA_PROP_NO_TRANSFORM
198
                        | XTENSA_PROP_INSN_NO_REORDER);
199
      value = bfd_get_32 (abfd, insn_contents + entry * 8 + 0);
200
      bfd_put_32 (abfd, value, prop_contents + entry * 12 + 0);
201
      value = bfd_get_32 (abfd, insn_contents + entry * 8 + 4);
202
      bfd_put_32 (abfd, value, prop_contents + entry * 12 + 4);
203
      bfd_put_32 (abfd, flags, prop_contents + entry * 12 + 8);
204
    }
205
 
206
  /* Now copy all of the relocations.  Change offsets for the
207
     instruction table section to offsets in the property table
208
     section.  */
209
  if (internal_relocs)
210
    {
211
      unsigned i;
212
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
213
 
214
      for (i = 0; i < reloc_count; i++)
215
        {
216
          Elf_Internal_Rela *rela;
217
          unsigned r_offset;
218
 
219
          rela = &internal_relocs[i];
220
 
221
          /* If this relocation is to the .xt.insn section,
222
             change the section number and the offset.  */
223
          r_offset = rela->r_offset;
224
          r_offset += 4 * (r_offset / 8);
225
          rela->r_offset = r_offset;
226
        }
227
    }
228
 
229
  remove_section (abfd, insn_sec);
230
 
231
  if (insn_contents)
232
    free (insn_contents);
233
 
234
  return TRUE;
235
 
236
 cleanup:
237
  if (prop_sec && prop_sec->owner)
238
    remove_section (abfd, prop_sec);
239
  if (insn_contents)
240
    free (insn_contents);
241
  if (internal_relocs)
242
    free (internal_relocs);
243
 
244
  return FALSE;
245
}
246
 
247
 
248
#define PROP_SEC_BASE_NAME ".xt.prop"
249
#define INSN_SEC_BASE_NAME ".xt.insn"
250
#define LINKONCE_SEC_OLD_TEXT_BASE_NAME ".gnu.linkonce.x."
251
 
252
 
253
static void
254
replace_instruction_table_sections (bfd *abfd, asection *sec)
255
{
256
  char *message = "";
257
  const char *insn_sec_name = NULL;
258
  char *prop_sec_name = NULL;
259
  char *owned_prop_sec_name = NULL;
260
  const char *sec_name;
261
 
262
  sec_name = bfd_get_section_name (abfd, sec);
263
  if (strcmp (sec_name, INSN_SEC_BASE_NAME) == 0)
264
    {
265
      insn_sec_name = INSN_SEC_BASE_NAME;
266
      prop_sec_name = PROP_SEC_BASE_NAME;
267
    }
268
  else if (CONST_STRNEQ (sec_name, LINKONCE_SEC_OLD_TEXT_BASE_NAME))
269
    {
270
      insn_sec_name = sec_name;
271
      owned_prop_sec_name = (char *) xmalloc (strlen (sec_name) + 20);
272
      prop_sec_name = owned_prop_sec_name;
273
      strcpy (prop_sec_name, ".gnu.linkonce.prop.t.");
274
      strcat (prop_sec_name,
275
              sec_name + strlen (LINKONCE_SEC_OLD_TEXT_BASE_NAME));
276
    }
277
  if (insn_sec_name != NULL)
278
    {
279
      if (! replace_insn_sec_with_prop_sec (abfd, insn_sec_name, prop_sec_name,
280
                                            &message))
281
        {
282
          einfo (_("%P: warning: failed to convert %s table in %B (%s); subsequent disassembly may be incomplete\n"),
283
                 insn_sec_name, abfd, message);
284
        }
285
    }
286
  if (owned_prop_sec_name)
287
    free (owned_prop_sec_name);
288
}
289
 
290
 
291
/* This is called after all input sections have been opened to convert
292
   instruction tables (.xt.insn, gnu.linkonce.x.*) tables into property
293
   tables (.xt.prop) before any section placement.  */
294
 
295
static void
296
elf_xtensa_after_open (void)
297
{
298
  /* First call the ELF version.  */
299
  gld${EMULATION_NAME}_after_open ();
300
 
301
  /* Now search the input files looking for instruction table sections.  */
302
  LANG_FOR_EACH_INPUT_STATEMENT (f)
303
    {
304
      asection *sec = f->the_bfd->sections;
305
      asection *next_sec;
306
 
307
      /* Do not use bfd_map_over_sections here since we are removing
308
         sections as we iterate.  */
309
      while (sec != NULL)
310
        {
311
          next_sec = sec->next;
312
          replace_instruction_table_sections (f->the_bfd, sec);
313
          sec = next_sec;
314
        }
315
    }
316
}
317
 
318
 
319
static bfd_boolean
320
xt_config_info_unpack_and_check (char *data,
321
                                 bfd_boolean *pmismatch,
322
                                 char **pmsg)
323
{
324
  char *d, *key;
325
  unsigned num;
326
 
327
  *pmismatch = FALSE;
328
 
329
  d = data;
330
  while (*d)
331
    {
332
      key = d;
333
      d = strchr (d, '=');
334
      if (! d)
335
        goto error;
336
 
337
      /* Overwrite the equal sign.  */
338
      *d++ = 0;
339
 
340
      /* Check if this is a quoted string or a number.  */
341
      if (*d == '"')
342
        {
343
          /* No string values are currently checked by LD;
344
             just skip over the quotes.  */
345
          d++;
346
          d = strchr (d, '"');
347
          if (! d)
348
            goto error;
349
          /* Overwrite the trailing quote.  */
350
          *d++ = 0;
351
        }
352
      else
353
        {
354
          if (*d == 0)
355
            goto error;
356
          num = strtoul (d, &d, 0);
357
 
358
          if (! strcmp (key, "ABI"))
359
            {
360
              if (num != XSHAL_ABI)
361
                {
362
                  *pmismatch = TRUE;
363
                  *pmsg = "ABI does not match";
364
                }
365
            }
366
          else if (! strcmp (key, "USE_ABSOLUTE_LITERALS"))
367
            {
368
              if (num != XSHAL_USE_ABSOLUTE_LITERALS)
369
                {
370
                  *pmismatch = TRUE;
371
                  *pmsg = "incompatible use of the Extended L32R option";
372
                }
373
            }
374
        }
375
 
376
      if (*d++ != '\n')
377
        goto error;
378
    }
379
 
380
  return TRUE;
381
 
382
 error:
383
  return FALSE;
384
}
385
 
386
 
387
#define XTINFO_NAME "Xtensa_Info"
388
#define XTINFO_NAMESZ 12
389
#define XTINFO_TYPE 1
390
 
391
static void
392
check_xtensa_info (bfd *abfd, asection *info_sec)
393
{
394
  char *data, *errmsg = "";
395
  bfd_boolean mismatch;
396
 
397
  data = xmalloc (info_sec->size);
398
  if (! bfd_get_section_contents (abfd, info_sec, data, 0, info_sec->size))
399
    einfo (_("%F%P:%B: cannot read contents of section %A\n"), abfd, info_sec);
400
 
401
  if (info_sec->size > 24
402
      && info_sec->size >= 24 + bfd_get_32 (abfd, data + 4)
403
      && bfd_get_32 (abfd, data + 0) == XTINFO_NAMESZ
404
      && bfd_get_32 (abfd, data + 8) == XTINFO_TYPE
405
      && strcmp (data + 12, XTINFO_NAME) == 0
406
      && xt_config_info_unpack_and_check (data + 12 + XTINFO_NAMESZ,
407
                                          &mismatch, &errmsg))
408
    {
409
      if (mismatch)
410
        einfo (_("%P:%B: warning: incompatible Xtensa configuration (%s)\n"),
411
               abfd, errmsg);
412
    }
413
  else
414
    einfo (_("%P:%B: warning: cannot parse .xtensa.info section\n"), abfd);
415
 
416
  free (data);
417
}
418
 
419
 
420
/* This is called after the sections have been attached to output
421
   sections, but before any sizes or addresses have been set.  */
422
 
423
static void
424
elf_xtensa_before_allocation (void)
425
{
426
  asection *info_sec, *first_info_sec;
427
  bfd *first_bfd;
428
  bfd_boolean is_big_endian = XCHAL_HAVE_BE;
429
 
430
  /* Check that the output endianness matches the Xtensa
431
     configuration.  The BFD library always includes both big and
432
     little endian target vectors for Xtensa, but it only supports the
433
     detailed instruction encode/decode operations (such as are
434
     required to process relocations) for the selected Xtensa
435
     configuration.  */
436
 
437
  if (is_big_endian
438
      && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
439
    {
440
      einfo (_("%F%P: little endian output does not match "
441
               "Xtensa configuration\n"));
442
    }
443
  if (!is_big_endian
444
      && link_info.output_bfd->xvec->byteorder == BFD_ENDIAN_BIG)
445
    {
446
      einfo (_("%F%P: big endian output does not match "
447
               "Xtensa configuration\n"));
448
    }
449
 
450
  /* Keep track of the first input .xtensa.info section, and as a fallback,
451
     the first input bfd where a .xtensa.info section could be created.
452
     After the input .xtensa.info has been checked, the contents of the
453
     first one will be replaced with the output .xtensa.info table.  */
454
  first_info_sec = 0;
455
  first_bfd = 0;
456
 
457
  LANG_FOR_EACH_INPUT_STATEMENT (f)
458
    {
459
      /* Check that the endianness for each input file matches the output.
460
         The merge_private_bfd_data hook has already reported any mismatches
461
         as errors, but those errors are not fatal.  At this point, we
462
         cannot go any further if there are any mismatches.  */
463
      if ((is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_LITTLE)
464
          || (!is_big_endian && f->the_bfd->xvec->byteorder == BFD_ENDIAN_BIG))
465
        einfo (_("%F%P: cross-endian linking for %B not supported\n"),
466
               f->the_bfd);
467
 
468
      if (! first_bfd)
469
        first_bfd = f->the_bfd;
470
 
471
      info_sec = bfd_get_section_by_name (f->the_bfd, ".xtensa.info");
472
      if (! info_sec)
473
        continue;
474
 
475
      if (! first_info_sec)
476
        first_info_sec = info_sec;
477
 
478
      /* Unpack the .xtensa.info section and check it against the current
479
         Xtensa configuration.  */
480
      check_xtensa_info (f->the_bfd, info_sec);
481
 
482
      /* Do not include this copy of .xtensa.info in the output.  */
483
      info_sec->size = 0;
484
      info_sec->flags |= SEC_EXCLUDE;
485
    }
486
 
487
  /* Reuse the first .xtensa.info input section to hold the output
488
     .xtensa.info; or, if none were found, create a new section in the
489
     first input bfd (assuming there is one).  */
490
  info_sec = first_info_sec;
491
  if (! info_sec && first_bfd)
492
    {
493
      info_sec = bfd_make_section_with_flags (first_bfd, ".xtensa.info",
494
                                              SEC_HAS_CONTENTS | SEC_READONLY);
495
      if (! info_sec)
496
        einfo (_("%F%P: failed to create .xtensa.info section\n"));
497
    }
498
  if (info_sec)
499
    {
500
      int xtensa_info_size;
501
      char *data;
502
 
503
      info_sec->flags &= ~SEC_EXCLUDE;
504
      info_sec->flags |= SEC_IN_MEMORY;
505
 
506
      data = xmalloc (100);
507
      sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
508
               XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
509
      xtensa_info_size = strlen (data) + 1;
510
 
511
      /* Add enough null terminators to pad to a word boundary.  */
512
      do
513
        data[xtensa_info_size++] = 0;
514
      while ((xtensa_info_size & 3) != 0);
515
 
516
      info_sec->size = 12 + XTINFO_NAMESZ + xtensa_info_size;
517
      info_sec->contents = xmalloc (info_sec->size);
518
      bfd_put_32 (info_sec->owner, XTINFO_NAMESZ, info_sec->contents + 0);
519
      bfd_put_32 (info_sec->owner, xtensa_info_size, info_sec->contents + 4);
520
      bfd_put_32 (info_sec->owner, XTINFO_TYPE, info_sec->contents + 8);
521
      memcpy (info_sec->contents + 12, XTINFO_NAME, XTINFO_NAMESZ);
522
      memcpy (info_sec->contents + 12 + XTINFO_NAMESZ, data, xtensa_info_size);
523
      free (data);
524
    }
525
 
526
  /* Enable relaxation by default if the "--no-relax" option was not
527
     specified.  This is done here instead of in the before_parse hook
528
     because there is a check in main() to prohibit use of --relax and
529
     -r together and that combination should be allowed for Xtensa.  */
530
 
531
  if (!disable_relaxation)
532
    command_line.relax = TRUE;
533
 
534
  xtensa_strip_inconsistent_linkonce_sections (stat_ptr);
535
 
536
  gld${EMULATION_NAME}_before_allocation ();
537
 
538
  xtensa_wild_group_interleave (stat_ptr->head);
539
  if (command_line.relax)
540
    xtensa_colocate_output_literals (stat_ptr->head);
541
 
542
  /* TBD: We need to force the page alignments to here and only do
543
     them as needed for the entire output section.  Finally, if this
544
     is a relocatable link then we need to add alignment notes so
545
     that the literals can be separated later.  */
546
}
547
 
548
 
549
typedef struct wildcard_list section_name_list;
550
 
551
typedef struct reloc_deps_e_t reloc_deps_e;
552
typedef struct reloc_deps_section_t reloc_deps_section;
553
typedef struct reloc_deps_graph_t reloc_deps_graph;
554
 
555
 
556
struct reloc_deps_e_t
557
{
558
  asection *src; /* Contains l32rs.  */
559
  asection *tgt; /* Contains literals.  */
560
  reloc_deps_e *next;
561
};
562
 
563
/* Place these in the userdata field.  */
564
struct reloc_deps_section_t
565
{
566
  reloc_deps_e *preds;
567
  reloc_deps_e *succs;
568
  bfd_boolean is_only_literal;
569
};
570
 
571
 
572
struct reloc_deps_graph_t
573
{
574
  size_t count;
575
  size_t size;
576
  asection **sections;
577
};
578
 
579
static void xtensa_layout_wild
580
  (const reloc_deps_graph *, lang_wild_statement_type *);
581
 
582
typedef void (*deps_callback_t) (asection *, /* src_sec */
583
                                 bfd_vma,    /* src_offset */
584
                                 asection *, /* target_sec */
585
                                 bfd_vma,    /* target_offset */
586
                                 void *);    /* closure */
587
 
588
extern bfd_boolean xtensa_callback_required_dependence
589
  (bfd *, asection *, struct bfd_link_info *, deps_callback_t, void *);
590
static void xtensa_ldlang_clear_addresses (lang_statement_union_type *);
591
static bfd_boolean ld_local_file_relocations_fit
592
  (lang_statement_union_type *, const reloc_deps_graph *);
593
static bfd_vma ld_assign_relative_paged_dot
594
  (bfd_vma, lang_statement_union_type *, const reloc_deps_graph *,
595
   bfd_boolean);
596
static bfd_vma ld_xtensa_insert_page_offsets
597
  (bfd_vma, lang_statement_union_type *, reloc_deps_graph *, bfd_boolean);
598
#if EXTRA_VALIDATION
599
static size_t ld_count_children (lang_statement_union_type *);
600
#endif
601
 
602
extern lang_statement_list_type constructor_list;
603
 
604
/*  Begin verbatim code from ldlang.c:
605
    the following are copied from ldlang.c because they are defined
606
    there statically.  */
607
 
608
static void
609
lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
610
                                lang_statement_union_type *s)
611
{
612
  for (; s != (lang_statement_union_type *) NULL; s = s->header.next)
613
    {
614
      func (s);
615
 
616
      switch (s->header.type)
617
        {
618
        case lang_constructors_statement_enum:
619
          lang_for_each_statement_worker (func, constructor_list.head);
620
          break;
621
        case lang_output_section_statement_enum:
622
          lang_for_each_statement_worker
623
            (func,
624
             s->output_section_statement.children.head);
625
          break;
626
        case lang_wild_statement_enum:
627
          lang_for_each_statement_worker
628
            (func,
629
             s->wild_statement.children.head);
630
          break;
631
        case lang_group_statement_enum:
632
          lang_for_each_statement_worker (func,
633
                                          s->group_statement.children.head);
634
          break;
635
        case lang_data_statement_enum:
636
        case lang_reloc_statement_enum:
637
        case lang_object_symbols_statement_enum:
638
        case lang_output_statement_enum:
639
        case lang_target_statement_enum:
640
        case lang_input_section_enum:
641
        case lang_input_statement_enum:
642
        case lang_assignment_statement_enum:
643
        case lang_padding_statement_enum:
644
        case lang_address_statement_enum:
645
        case lang_fill_statement_enum:
646
          break;
647
        default:
648
          FAIL ();
649
          break;
650
        }
651
    }
652
}
653
 
654
/* End of verbatim code from ldlang.c.  */
655
 
656
 
657
static reloc_deps_section *
658
xtensa_get_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
659
                         asection *sec)
660
{
661
  /* We have a separate function for this so that
662
     we could in the future keep a completely independent
663
     structure that maps a section to its dependence edges.
664
     For now, we place these in the sec->userdata field.  */
665
  reloc_deps_section *sec_deps = sec->userdata;
666
  return sec_deps;
667
}
668
 
669
static void
670
xtensa_set_section_deps (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
671
                         asection *sec,
672
                         reloc_deps_section *deps_section)
673
{
674
  sec->userdata = deps_section;
675
}
676
 
677
 
678
/* This is used to keep a list of all of the sections participating in
679
   the graph so we can clean them up quickly.  */
680
 
681
static void
682
xtensa_append_section_deps (reloc_deps_graph *deps, asection *sec)
683
{
684
  if (deps->size <= deps->count)
685
    {
686
      asection **new_sections;
687
      size_t i;
688
      size_t new_size;
689
 
690
      new_size = deps->size * 2;
691
      if (new_size == 0)
692
        new_size = 20;
693
 
694
      new_sections = xmalloc (sizeof (asection *) * new_size);
695
      memset (new_sections, 0, sizeof (asection *) * new_size);
696
      for (i = 0; i < deps->count; i++)
697
        {
698
          new_sections[i] = deps->sections[i];
699
        }
700
      if (deps->sections != NULL)
701
        free (deps->sections);
702
      deps->sections = new_sections;
703
      deps->size = new_size;
704
    }
705
  deps->sections[deps->count] = sec;
706
  deps->count++;
707
}
708
 
709
 
710
static void
711
free_reloc_deps_graph (reloc_deps_graph *deps)
712
{
713
  size_t i;
714
  for (i = 0; i < deps->count; i++)
715
    {
716
      asection *sec = deps->sections[i];
717
      reloc_deps_section *sec_deps;
718
      sec_deps = xtensa_get_section_deps (deps, sec);
719
      if (sec_deps)
720
        {
721
          reloc_deps_e *next;
722
          while (sec_deps->succs != NULL)
723
            {
724
              next = sec_deps->succs->next;
725
              free (sec_deps->succs);
726
              sec_deps->succs = next;
727
            }
728
 
729
          while (sec_deps->preds != NULL)
730
            {
731
              next = sec_deps->preds->next;
732
              free (sec_deps->preds);
733
              sec_deps->preds = next;
734
            }
735
          free (sec_deps);
736
        }
737
      xtensa_set_section_deps (deps, sec, NULL);
738
    }
739
  if (deps->sections)
740
    free (deps->sections);
741
 
742
  free (deps);
743
}
744
 
745
 
746
static bfd_boolean
747
section_is_source (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
748
                   lang_statement_union_type *s)
749
{
750
  asection *sec;
751
  const reloc_deps_section *sec_deps;
752
 
753
  if (s->header.type != lang_input_section_enum)
754
    return FALSE;
755
  sec = s->input_section.section;
756
 
757
  sec_deps = xtensa_get_section_deps (deps, sec);
758
  return sec_deps && sec_deps->succs != NULL;
759
}
760
 
761
 
762
static bfd_boolean
763
section_is_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
764
                   lang_statement_union_type *s)
765
{
766
  asection *sec;
767
  const reloc_deps_section *sec_deps;
768
 
769
  if (s->header.type != lang_input_section_enum)
770
    return FALSE;
771
  sec = s->input_section.section;
772
 
773
  sec_deps = xtensa_get_section_deps (deps, sec);
774
  return sec_deps && sec_deps->preds != NULL;
775
}
776
 
777
 
778
static bfd_boolean
779
section_is_source_or_target (const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
780
                             lang_statement_union_type *s)
781
{
782
  return (section_is_source (deps, s)
783
          || section_is_target (deps, s));
784
}
785
 
786
 
787
typedef struct xtensa_ld_iter_stack_t xtensa_ld_iter_stack;
788
typedef struct xtensa_ld_iter_t xtensa_ld_iter;
789
 
790
struct xtensa_ld_iter_t
791
{
792
  lang_statement_union_type *parent;    /* Parent of the list.  */
793
  lang_statement_list_type *l;          /* List that holds it.  */
794
  lang_statement_union_type **loc;      /* Place in the list.  */
795
};
796
 
797
struct xtensa_ld_iter_stack_t
798
{
799
  xtensa_ld_iter iterloc;               /* List that hold it.  */
800
 
801
  xtensa_ld_iter_stack *next;           /* Next in the stack.  */
802
  xtensa_ld_iter_stack *prev;           /* Back pointer for stack.  */
803
};
804
 
805
 
806
static void
807
ld_xtensa_move_section_after (xtensa_ld_iter *to, xtensa_ld_iter *current)
808
{
809
  lang_statement_union_type *to_next;
810
  lang_statement_union_type *current_next;
811
  lang_statement_union_type **e;
812
 
813
#if EXTRA_VALIDATION
814
  size_t old_to_count, new_to_count;
815
  size_t old_current_count, new_current_count;
816
#endif
817
 
818
  if (to == current)
819
    return;
820
 
821
#if EXTRA_VALIDATION
822
  old_to_count = ld_count_children (to->parent);
823
  old_current_count = ld_count_children (current->parent);
824
#endif
825
 
826
  to_next = *(to->loc);
827
  current_next = (*current->loc)->header.next;
828
 
829
  *(to->loc) = *(current->loc);
830
 
831
  *(current->loc) = current_next;
832
  (*(to->loc))->header.next = to_next;
833
 
834
  /* reset "to" list tail */
835
  for (e = &to->l->head; *e != NULL; e = &(*e)->header.next)
836
    ;
837
  to->l->tail = e;
838
 
839
  /* reset "current" list tail */
840
  for (e = ¤t->l->head; *e != NULL; e = &(*e)->header.next)
841
    ;
842
  current->l->tail = e;
843
 
844
#if EXTRA_VALIDATION
845
  new_to_count = ld_count_children (to->parent);
846
  new_current_count = ld_count_children (current->parent);
847
 
848
  ASSERT ((old_to_count + old_current_count)
849
          == (new_to_count + new_current_count));
850
#endif
851
}
852
 
853
 
854
/* Can only be called with lang_statements that have lists.  Returns
855
   FALSE if the list is empty.  */
856
 
857
static bfd_boolean
858
iter_stack_empty (xtensa_ld_iter_stack **stack_p)
859
{
860
  return *stack_p == NULL;
861
}
862
 
863
 
864
static bfd_boolean
865
iter_stack_push (xtensa_ld_iter_stack **stack_p,
866
                 lang_statement_union_type *parent)
867
{
868
  xtensa_ld_iter_stack *stack;
869
  lang_statement_list_type *l = NULL;
870
 
871
  switch (parent->header.type)
872
    {
873
    case lang_output_section_statement_enum:
874
      l = &parent->output_section_statement.children;
875
      break;
876
    case lang_wild_statement_enum:
877
      l = &parent->wild_statement.children;
878
      break;
879
    case lang_group_statement_enum:
880
      l = &parent->group_statement.children;
881
      break;
882
    default:
883
      ASSERT (0);
884
      return FALSE;
885
    }
886
 
887
  /* Empty. do not push.  */
888
  if (l->tail == &l->head)
889
    return FALSE;
890
 
891
  stack = xmalloc (sizeof (xtensa_ld_iter_stack));
892
  memset (stack, 0, sizeof (xtensa_ld_iter_stack));
893
  stack->iterloc.parent = parent;
894
  stack->iterloc.l = l;
895
  stack->iterloc.loc = &l->head;
896
 
897
  stack->next = *stack_p;
898
  stack->prev = NULL;
899
  if (*stack_p != NULL)
900
    (*stack_p)->prev = stack;
901
  *stack_p = stack;
902
  return TRUE;
903
}
904
 
905
 
906
static void
907
iter_stack_pop (xtensa_ld_iter_stack **stack_p)
908
{
909
  xtensa_ld_iter_stack *stack;
910
 
911
  stack = *stack_p;
912
 
913
  if (stack == NULL)
914
    {
915
      ASSERT (stack != NULL);
916
      return;
917
    }
918
 
919
  if (stack->next != NULL)
920
    stack->next->prev = NULL;
921
 
922
  *stack_p = stack->next;
923
  free (stack);
924
}
925
 
926
 
927
/* This MUST be called if, during iteration, the user changes the
928
   underlying structure.  It will check for a NULL current and advance
929
   accordingly.  */
930
 
931
static void
932
iter_stack_update (xtensa_ld_iter_stack **stack_p)
933
{
934
  if (!iter_stack_empty (stack_p)
935
      && (*(*stack_p)->iterloc.loc) == NULL)
936
    {
937
      iter_stack_pop (stack_p);
938
 
939
      while (!iter_stack_empty (stack_p)
940
             && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
941
        {
942
          iter_stack_pop (stack_p);
943
        }
944
      if (!iter_stack_empty (stack_p))
945
        (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
946
    }
947
}
948
 
949
 
950
static void
951
iter_stack_next (xtensa_ld_iter_stack **stack_p)
952
{
953
  xtensa_ld_iter_stack *stack;
954
  lang_statement_union_type *current;
955
  stack = *stack_p;
956
 
957
  current = *stack->iterloc.loc;
958
  /* If we are on the first element.  */
959
  if (current != NULL)
960
    {
961
      switch (current->header.type)
962
        {
963
        case lang_output_section_statement_enum:
964
        case lang_wild_statement_enum:
965
        case lang_group_statement_enum:
966
          /* If the list if not empty, we are done.  */
967
          if (iter_stack_push (stack_p, *stack->iterloc.loc))
968
            return;
969
          /* Otherwise increment the pointer as normal.  */
970
          break;
971
        default:
972
          break;
973
        }
974
    }
975
 
976
  while (!iter_stack_empty (stack_p)
977
         && ((*(*stack_p)->iterloc.loc)->header.next == NULL))
978
    {
979
      iter_stack_pop (stack_p);
980
    }
981
  if (!iter_stack_empty (stack_p))
982
    (*stack_p)->iterloc.loc = &(*(*stack_p)->iterloc.loc)->header.next;
983
}
984
 
985
 
986
static lang_statement_union_type *
987
iter_stack_current (xtensa_ld_iter_stack **stack_p)
988
{
989
  return *((*stack_p)->iterloc.loc);
990
}
991
 
992
 
993
/* The iter stack is a preorder.  */
994
 
995
static void
996
iter_stack_create (xtensa_ld_iter_stack **stack_p,
997
                   lang_statement_union_type *parent)
998
{
999
  iter_stack_push (stack_p, parent);
1000
}
1001
 
1002
 
1003
static void
1004
iter_stack_copy_current (xtensa_ld_iter_stack **stack_p, xtensa_ld_iter *front)
1005
{
1006
  *front = (*stack_p)->iterloc;
1007
}
1008
 
1009
 
1010
static void
1011
xtensa_colocate_literals (reloc_deps_graph *deps,
1012
                          lang_statement_union_type *statement)
1013
{
1014
  /* Keep a stack of pointers to control iteration through the contours.  */
1015
  xtensa_ld_iter_stack *stack = NULL;
1016
  xtensa_ld_iter_stack **stack_p = &stack;
1017
 
1018
  xtensa_ld_iter front;  /* Location where new insertion should occur.  */
1019
  xtensa_ld_iter *front_p = NULL;
1020
 
1021
  xtensa_ld_iter current; /* Location we are checking.  */
1022
  xtensa_ld_iter *current_p = NULL;
1023
  bfd_boolean in_literals = FALSE;
1024
 
1025
  if (deps->count == 0)
1026
    return;
1027
 
1028
  iter_stack_create (stack_p, statement);
1029
 
1030
  while (!iter_stack_empty (stack_p))
1031
    {
1032
      bfd_boolean skip_increment = FALSE;
1033
      lang_statement_union_type *l = iter_stack_current (stack_p);
1034
 
1035
      switch (l->header.type)
1036
        {
1037
        case lang_assignment_statement_enum:
1038
          /* Any assignment statement should block reordering across it.  */
1039
          front_p = NULL;
1040
          in_literals = FALSE;
1041
          break;
1042
 
1043
        case lang_input_section_enum:
1044
          if (front_p == NULL)
1045
            {
1046
              in_literals = (section_is_target (deps, l)
1047
                             && !section_is_source (deps, l));
1048
              if (in_literals)
1049
                {
1050
                  front_p = &front;
1051
                  iter_stack_copy_current (stack_p, front_p);
1052
                }
1053
            }
1054
          else
1055
            {
1056
              bfd_boolean is_target;
1057
              current_p = ¤t;
1058
              iter_stack_copy_current (stack_p, current_p);
1059
              is_target = (section_is_target (deps, l)
1060
                           && !section_is_source (deps, l));
1061
 
1062
              if (in_literals)
1063
                {
1064
                  iter_stack_copy_current (stack_p, front_p);
1065
                  if (!is_target)
1066
                    in_literals = FALSE;
1067
                }
1068
              else
1069
                {
1070
                  if (is_target)
1071
                    {
1072
                      /* Try to insert in place.  */
1073
                      ld_xtensa_move_section_after (front_p, current_p);
1074
                      ld_assign_relative_paged_dot (0x100000,
1075
                                                    statement,
1076
                                                    deps,
1077
                                                    xtensa_use_literal_pages);
1078
 
1079
                      /* We use this code because it's already written.  */
1080
                      if (!ld_local_file_relocations_fit (statement, deps))
1081
                        {
1082
                          /* Move it back.  */
1083
                          ld_xtensa_move_section_after (current_p, front_p);
1084
                          /* Reset the literal placement.  */
1085
                          iter_stack_copy_current (stack_p, front_p);
1086
                        }
1087
                      else
1088
                        {
1089
                          /* Move front pointer up by one.  */
1090
                          front_p->loc = &(*front_p->loc)->header.next;
1091
 
1092
                          /* Do not increment the current pointer.  */
1093
                          skip_increment = TRUE;
1094
                        }
1095
                    }
1096
                }
1097
            }
1098
          break;
1099
        default:
1100
          break;
1101
        }
1102
 
1103
      if (!skip_increment)
1104
        iter_stack_next (stack_p);
1105
      else
1106
        /* Be careful to update the stack_p if it now is a null.  */
1107
        iter_stack_update (stack_p);
1108
    }
1109
 
1110
  lang_for_each_statement_worker (xtensa_ldlang_clear_addresses, statement);
1111
}
1112
 
1113
 
1114
static void
1115
xtensa_move_dependencies_to_front (reloc_deps_graph *deps,
1116
                                   lang_wild_statement_type *w)
1117
{
1118
  /* Keep a front pointer and a current pointer.  */
1119
  lang_statement_union_type **front;
1120
  lang_statement_union_type **current;
1121
 
1122
  /* Walk to the end of the targets.  */
1123
  for (front = &w->children.head;
1124
       (*front != NULL) && section_is_source_or_target (deps, *front);
1125
       front = &(*front)->header.next)
1126
    ;
1127
 
1128
  if (*front == NULL)
1129
    return;
1130
 
1131
  current = &(*front)->header.next;
1132
  while (*current != NULL)
1133
    {
1134
      if (section_is_source_or_target (deps, *current))
1135
        {
1136
          /* Insert in place.  */
1137
          xtensa_ld_iter front_iter;
1138
          xtensa_ld_iter current_iter;
1139
 
1140
          front_iter.parent = (lang_statement_union_type *) w;
1141
          front_iter.l = &w->children;
1142
          front_iter.loc = front;
1143
 
1144
          current_iter.parent = (lang_statement_union_type *) w;
1145
          current_iter.l = &w->children;
1146
          current_iter.loc = current;
1147
 
1148
          ld_xtensa_move_section_after (&front_iter, ¤t_iter);
1149
          front = &(*front)->header.next;
1150
        }
1151
      else
1152
        {
1153
          current = &(*current)->header.next;
1154
        }
1155
    }
1156
}
1157
 
1158
 
1159
static bfd_boolean
1160
deps_has_sec_edge (const reloc_deps_graph *deps, asection *src, asection *tgt)
1161
{
1162
  const reloc_deps_section *sec_deps;
1163
  const reloc_deps_e *sec_deps_e;
1164
 
1165
  sec_deps = xtensa_get_section_deps (deps, src);
1166
  if (sec_deps == NULL)
1167
    return FALSE;
1168
 
1169
  for (sec_deps_e = sec_deps->succs;
1170
       sec_deps_e != NULL;
1171
       sec_deps_e = sec_deps_e->next)
1172
    {
1173
      ASSERT (sec_deps_e->src == src);
1174
      if (sec_deps_e->tgt == tgt)
1175
        return TRUE;
1176
    }
1177
  return FALSE;
1178
}
1179
 
1180
 
1181
static bfd_boolean
1182
deps_has_edge (const reloc_deps_graph *deps,
1183
               lang_statement_union_type *src,
1184
               lang_statement_union_type *tgt)
1185
{
1186
  if (!section_is_source (deps, src))
1187
    return FALSE;
1188
  if (!section_is_target (deps, tgt))
1189
    return FALSE;
1190
 
1191
  if (src->header.type != lang_input_section_enum)
1192
    return FALSE;
1193
  if (tgt->header.type != lang_input_section_enum)
1194
    return FALSE;
1195
 
1196
  return deps_has_sec_edge (deps, src->input_section.section,
1197
                            tgt->input_section.section);
1198
}
1199
 
1200
 
1201
static void
1202
add_deps_edge (reloc_deps_graph *deps, asection *src_sec, asection *tgt_sec)
1203
{
1204
  reloc_deps_section *src_sec_deps;
1205
  reloc_deps_section *tgt_sec_deps;
1206
 
1207
  reloc_deps_e *src_edge;
1208
  reloc_deps_e *tgt_edge;
1209
 
1210
  if (deps_has_sec_edge (deps, src_sec, tgt_sec))
1211
    return;
1212
 
1213
  src_sec_deps = xtensa_get_section_deps (deps, src_sec);
1214
  if (src_sec_deps == NULL)
1215
    {
1216
      /* Add a section.  */
1217
      src_sec_deps = xmalloc (sizeof (reloc_deps_section));
1218
      memset (src_sec_deps, 0, sizeof (reloc_deps_section));
1219
      src_sec_deps->is_only_literal = 0;
1220
      src_sec_deps->preds = NULL;
1221
      src_sec_deps->succs = NULL;
1222
      xtensa_set_section_deps (deps, src_sec, src_sec_deps);
1223
      xtensa_append_section_deps (deps, src_sec);
1224
    }
1225
 
1226
  tgt_sec_deps = xtensa_get_section_deps (deps, tgt_sec);
1227
  if (tgt_sec_deps == NULL)
1228
    {
1229
      /* Add a section.  */
1230
      tgt_sec_deps = xmalloc (sizeof (reloc_deps_section));
1231
      memset (tgt_sec_deps, 0, sizeof (reloc_deps_section));
1232
      tgt_sec_deps->is_only_literal = 0;
1233
      tgt_sec_deps->preds = NULL;
1234
      tgt_sec_deps->succs = NULL;
1235
      xtensa_set_section_deps (deps, tgt_sec, tgt_sec_deps);
1236
      xtensa_append_section_deps (deps, tgt_sec);
1237
    }
1238
 
1239
  /* Add the edges.  */
1240
  src_edge = xmalloc (sizeof (reloc_deps_e));
1241
  memset (src_edge, 0, sizeof (reloc_deps_e));
1242
  src_edge->src = src_sec;
1243
  src_edge->tgt = tgt_sec;
1244
  src_edge->next = src_sec_deps->succs;
1245
  src_sec_deps->succs = src_edge;
1246
 
1247
  tgt_edge = xmalloc (sizeof (reloc_deps_e));
1248
  memset (tgt_edge, 0, sizeof (reloc_deps_e));
1249
  tgt_edge->src = src_sec;
1250
  tgt_edge->tgt = tgt_sec;
1251
  tgt_edge->next = tgt_sec_deps->preds;
1252
  tgt_sec_deps->preds = tgt_edge;
1253
}
1254
 
1255
 
1256
static void
1257
build_deps_graph_callback (asection *src_sec,
1258
                           bfd_vma src_offset ATTRIBUTE_UNUSED,
1259
                           asection *target_sec,
1260
                           bfd_vma target_offset ATTRIBUTE_UNUSED,
1261
                           void *closure)
1262
{
1263
  reloc_deps_graph *deps = closure;
1264
 
1265
  /* If the target is defined.  */
1266
  if (target_sec != NULL)
1267
    add_deps_edge (deps, src_sec, target_sec);
1268
}
1269
 
1270
 
1271
static reloc_deps_graph *
1272
ld_build_required_section_dependence (lang_statement_union_type *s)
1273
{
1274
  reloc_deps_graph *deps;
1275
  xtensa_ld_iter_stack *stack = NULL;
1276
 
1277
  deps = xmalloc (sizeof (reloc_deps_graph));
1278
  deps->sections = NULL;
1279
  deps->count = 0;
1280
  deps->size = 0;
1281
 
1282
  for (iter_stack_create (&stack, s);
1283
       !iter_stack_empty (&stack);
1284
       iter_stack_next (&stack))
1285
    {
1286
      lang_statement_union_type *l = iter_stack_current (&stack);
1287
 
1288
      if (l->header.type == lang_input_section_enum)
1289
        {
1290
          lang_input_section_type *input;
1291
          input = &l->input_section;
1292
          xtensa_callback_required_dependence (input->section->owner,
1293
                                               input->section,
1294
                                               &link_info,
1295
                                               /* Use the same closure.  */
1296
                                               build_deps_graph_callback,
1297
                                               deps);
1298
        }
1299
    }
1300
  return deps;
1301
}
1302
 
1303
 
1304
#if EXTRA_VALIDATION
1305
static size_t
1306
ld_count_children (lang_statement_union_type *s)
1307
{
1308
  size_t count = 0;
1309
  xtensa_ld_iter_stack *stack = NULL;
1310
  for (iter_stack_create (&stack, s);
1311
       !iter_stack_empty (&stack);
1312
       iter_stack_next (&stack))
1313
    {
1314
      lang_statement_union_type *l = iter_stack_current (&stack);
1315
      ASSERT (l != NULL);
1316
      count++;
1317
    }
1318
  return count;
1319
}
1320
#endif /* EXTRA_VALIDATION */
1321
 
1322
 
1323
/* Check if a particular section is included in the link.  This will only
1324
   be true for one instance of a particular linkonce section.  */
1325
 
1326
static bfd_boolean input_section_found = FALSE;
1327
static asection *input_section_target = NULL;
1328
 
1329
static void
1330
input_section_linked_worker (lang_statement_union_type *statement)
1331
{
1332
  if ((statement->header.type == lang_input_section_enum
1333
       && (statement->input_section.section == input_section_target)))
1334
    input_section_found = TRUE;
1335
}
1336
 
1337
static bfd_boolean
1338
input_section_linked (asection *sec)
1339
{
1340
  input_section_found = FALSE;
1341
  input_section_target = sec;
1342
  lang_for_each_statement_worker (input_section_linked_worker, stat_ptr->head);
1343
  return input_section_found;
1344
}
1345
 
1346
 
1347
/* Strip out any linkonce property tables or XCC exception tables where the
1348
   associated linkonce text is from a different object file.  Normally,
1349
   a matching set of linkonce sections is taken from the same object file,
1350
   but sometimes the files are compiled differently so that some of the
1351
   linkonce sections are not present in all files.  Stripping the
1352
   inconsistent sections like this is not completely robust -- a much
1353
   better solution is to use comdat groups.  */
1354
 
1355
static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
1356
 
1357
static bfd_boolean
1358
is_inconsistent_linkonce_section (asection *sec)
1359
{
1360
  bfd *abfd = sec->owner;
1361
  const char *sec_name = bfd_get_section_name (abfd, sec);
1362
  const char *name;
1363
 
1364
  if ((bfd_get_section_flags (abfd, sec) & SEC_LINK_ONCE) == 0
1365
      || strncmp (sec_name, ".gnu.linkonce.", linkonce_len) != 0)
1366
    return FALSE;
1367
 
1368
  /* Check if this is an Xtensa property section or an exception table
1369
     for Tensilica's XCC compiler.  */
1370
  name = sec_name + linkonce_len;
1371
  if (CONST_STRNEQ (name, "prop."))
1372
    name = strchr (name + 5, '.') + 1;
1373
  else if (name[1] == '.'
1374
           && (name[0] == 'p' || name[0] == 'e' || name[0] == 'h'))
1375
    name += 2;
1376
  else
1377
    name = 0;
1378
 
1379
  if (name)
1380
    {
1381
      char *dep_sec_name = xmalloc (strlen (sec_name) + 1);
1382
      asection *dep_sec;
1383
 
1384
      /* Get the associated linkonce text section and check if it is
1385
         included in the link.  If not, this section is inconsistent
1386
         and should be stripped.  */
1387
      strcpy (dep_sec_name, ".gnu.linkonce.t.");
1388
      strcat (dep_sec_name, name);
1389
      dep_sec = bfd_get_section_by_name (abfd, dep_sec_name);
1390
      if (dep_sec == NULL || ! input_section_linked (dep_sec))
1391
        {
1392
          free (dep_sec_name);
1393
          return TRUE;
1394
        }
1395
      free (dep_sec_name);
1396
    }
1397
 
1398
  return FALSE;
1399
}
1400
 
1401
 
1402
static void
1403
xtensa_strip_inconsistent_linkonce_sections (lang_statement_list_type *slist)
1404
{
1405
  lang_statement_union_type **s_p = &slist->head;
1406
  while (*s_p)
1407
    {
1408
      lang_statement_union_type *s = *s_p;
1409
      lang_statement_union_type *s_next = (*s_p)->header.next;
1410
 
1411
      switch (s->header.type)
1412
        {
1413
        case lang_input_section_enum:
1414
          if (is_inconsistent_linkonce_section (s->input_section.section))
1415
            {
1416
              s->input_section.section->output_section = bfd_abs_section_ptr;
1417
              *s_p = s_next;
1418
              continue;
1419
            }
1420
          break;
1421
 
1422
        case lang_constructors_statement_enum:
1423
          xtensa_strip_inconsistent_linkonce_sections (&constructor_list);
1424
          break;
1425
 
1426
        case lang_output_section_statement_enum:
1427
          if (s->output_section_statement.children.head)
1428
            xtensa_strip_inconsistent_linkonce_sections
1429
              (&s->output_section_statement.children);
1430
          break;
1431
 
1432
        case lang_wild_statement_enum:
1433
          xtensa_strip_inconsistent_linkonce_sections
1434
            (&s->wild_statement.children);
1435
          break;
1436
 
1437
        case lang_group_statement_enum:
1438
          xtensa_strip_inconsistent_linkonce_sections
1439
            (&s->group_statement.children);
1440
          break;
1441
 
1442
        case lang_data_statement_enum:
1443
        case lang_reloc_statement_enum:
1444
        case lang_object_symbols_statement_enum:
1445
        case lang_output_statement_enum:
1446
        case lang_target_statement_enum:
1447
        case lang_input_statement_enum:
1448
        case lang_assignment_statement_enum:
1449
        case lang_padding_statement_enum:
1450
        case lang_address_statement_enum:
1451
        case lang_fill_statement_enum:
1452
          break;
1453
 
1454
        default:
1455
          FAIL ();
1456
          break;
1457
        }
1458
 
1459
      s_p = &(*s_p)->header.next;
1460
    }
1461
 
1462
  /* Reset the tail of the list, in case the last entry was removed.  */
1463
  if (s_p != slist->tail)
1464
    slist->tail = s_p;
1465
}
1466
 
1467
 
1468
static void
1469
xtensa_wild_group_interleave_callback (lang_statement_union_type *statement)
1470
{
1471
  lang_wild_statement_type *w;
1472
  reloc_deps_graph *deps;
1473
  if (statement->header.type == lang_wild_statement_enum)
1474
    {
1475
#if EXTRA_VALIDATION
1476
      size_t old_child_count;
1477
      size_t new_child_count;
1478
#endif
1479
      bfd_boolean no_reorder;
1480
 
1481
      w = &statement->wild_statement;
1482
 
1483
      no_reorder = FALSE;
1484
 
1485
      /* If it has 0 or 1 section bound, then do not reorder.  */
1486
      if (w->children.head == NULL
1487
          || (w->children.head->header.type == lang_input_section_enum
1488
              && w->children.head->header.next == NULL))
1489
        no_reorder = TRUE;
1490
 
1491
      if (w->filenames_sorted)
1492
        no_reorder = TRUE;
1493
 
1494
      /* Check for sorting in a section list wildcard spec as well.  */
1495
      if (!no_reorder)
1496
        {
1497
          struct wildcard_list *l;
1498
          for (l = w->section_list; l != NULL; l = l->next)
1499
            {
1500
              if (l->spec.sorted == TRUE)
1501
                {
1502
                  no_reorder = TRUE;
1503
                  break;
1504
                }
1505
            }
1506
        }
1507
 
1508
      /* Special case until the NOREORDER linker directive is supported:
1509
         *(.init) output sections and *(.fini) specs may NOT be reordered.  */
1510
 
1511
      /* Check for sorting in a section list wildcard spec as well.  */
1512
      if (!no_reorder)
1513
        {
1514
          struct wildcard_list *l;
1515
          for (l = w->section_list; l != NULL; l = l->next)
1516
            {
1517
              if (l->spec.name
1518
                  && ((strcmp (".init", l->spec.name) == 0)
1519
                      || (strcmp (".fini", l->spec.name) == 0)))
1520
                {
1521
                  no_reorder = TRUE;
1522
                  break;
1523
                }
1524
            }
1525
        }
1526
 
1527
#if EXTRA_VALIDATION
1528
      old_child_count = ld_count_children (statement);
1529
#endif
1530
 
1531
      /* It is now officially a target.  Build the graph of source
1532
         section -> target section (kept as a list of edges).  */
1533
      deps = ld_build_required_section_dependence (statement);
1534
 
1535
      /* If this wildcard does not reorder....  */
1536
      if (!no_reorder && deps->count != 0)
1537
        {
1538
          /* First check for reverse dependences.  Fix if possible.  */
1539
          xtensa_layout_wild (deps, w);
1540
 
1541
          xtensa_move_dependencies_to_front (deps, w);
1542
#if EXTRA_VALIDATION
1543
          new_child_count = ld_count_children (statement);
1544
          ASSERT (new_child_count == old_child_count);
1545
#endif
1546
 
1547
          xtensa_colocate_literals (deps, statement);
1548
 
1549
#if EXTRA_VALIDATION
1550
          new_child_count = ld_count_children (statement);
1551
          ASSERT (new_child_count == old_child_count);
1552
#endif
1553
        }
1554
 
1555
      /* Clean up.  */
1556
      free_reloc_deps_graph (deps);
1557
    }
1558
}
1559
 
1560
 
1561
static void
1562
xtensa_wild_group_interleave (lang_statement_union_type *s)
1563
{
1564
  lang_for_each_statement_worker (xtensa_wild_group_interleave_callback, s);
1565
}
1566
 
1567
 
1568
static void
1569
xtensa_layout_wild (const reloc_deps_graph *deps, lang_wild_statement_type *w)
1570
{
1571
  /* If it does not fit initially, we need to do this step.  Move all
1572
     of the wild literal sections to a new list, then move each of
1573
     them back in just before the first section they depend on.  */
1574
  lang_statement_union_type **s_p;
1575
#if EXTRA_VALIDATION
1576
  size_t old_count, new_count;
1577
  size_t ct1, ct2;
1578
#endif
1579
 
1580
  lang_wild_statement_type literal_wild;
1581
  literal_wild.header.next = NULL;
1582
  literal_wild.header.type = lang_wild_statement_enum;
1583
  literal_wild.filename = NULL;
1584
  literal_wild.filenames_sorted = FALSE;
1585
  literal_wild.section_list = NULL;
1586
  literal_wild.keep_sections = FALSE;
1587
  literal_wild.children.head = NULL;
1588
  literal_wild.children.tail = &literal_wild.children.head;
1589
 
1590
#if EXTRA_VALIDATION
1591
  old_count = ld_count_children ((lang_statement_union_type*) w);
1592
#endif
1593
 
1594
  s_p = &w->children.head;
1595
  while (*s_p != NULL)
1596
    {
1597
      lang_statement_union_type *l = *s_p;
1598
      if (l->header.type == lang_input_section_enum)
1599
        {
1600
          if (section_is_target (deps, l)
1601
              && ! section_is_source (deps, l))
1602
            {
1603
              /* Detach.  */
1604
              *s_p = l->header.next;
1605
              if (*s_p == NULL)
1606
                w->children.tail = s_p;
1607
              l->header.next = NULL;
1608
 
1609
              /* Append.  */
1610
              *literal_wild.children.tail = l;
1611
              literal_wild.children.tail = &l->header.next;
1612
              continue;
1613
            }
1614
        }
1615
      s_p = &(*s_p)->header.next;
1616
    }
1617
 
1618
#if EXTRA_VALIDATION
1619
  ct1 = ld_count_children ((lang_statement_union_type*) w);
1620
  ct2 = ld_count_children ((lang_statement_union_type*) &literal_wild);
1621
 
1622
  ASSERT (old_count == (ct1 + ct2));
1623
#endif
1624
 
1625
  /* Now place them back in front of their dependent sections.  */
1626
 
1627
  while (literal_wild.children.head != NULL)
1628
    {
1629
      lang_statement_union_type *lit = literal_wild.children.head;
1630
      bfd_boolean placed = FALSE;
1631
 
1632
#if EXTRA_VALIDATION
1633
      ASSERT (ct2 > 0);
1634
      ct2--;
1635
#endif
1636
 
1637
      /* Detach.  */
1638
      literal_wild.children.head = lit->header.next;
1639
      if (literal_wild.children.head == NULL)
1640
        literal_wild.children.tail = &literal_wild.children.head;
1641
      lit->header.next = NULL;
1642
 
1643
      /* Find a spot to place it.  */
1644
      for (s_p = &w->children.head; *s_p != NULL; s_p = &(*s_p)->header.next)
1645
        {
1646
          lang_statement_union_type *src = *s_p;
1647
          if (deps_has_edge (deps, src, lit))
1648
            {
1649
              /* Place it here.  */
1650
              lit->header.next = *s_p;
1651
              *s_p = lit;
1652
              placed = TRUE;
1653
              break;
1654
            }
1655
        }
1656
 
1657
      if (!placed)
1658
        {
1659
          /* Put it at the end.  */
1660
          *w->children.tail = lit;
1661
          w->children.tail = &lit->header.next;
1662
        }
1663
    }
1664
 
1665
#if EXTRA_VALIDATION
1666
  new_count = ld_count_children ((lang_statement_union_type*) w);
1667
  ASSERT (new_count == old_count);
1668
#endif
1669
}
1670
 
1671
 
1672
static void
1673
xtensa_colocate_output_literals_callback (lang_statement_union_type *statement)
1674
{
1675
  lang_output_section_statement_type *os;
1676
  reloc_deps_graph *deps;
1677
  if (statement->header.type == lang_output_section_statement_enum)
1678
    {
1679
      /* Now, we walk over the contours of the output section statement.
1680
 
1681
         First we build the literal section dependences as before.
1682
 
1683
         At the first uniquely_literal section, we mark it as a good
1684
         spot to place other literals.  Continue walking (and counting
1685
         sizes) until we find the next literal section.  If this
1686
         section can be moved to the first one, then we move it.  If
1687
         we every find a modification of ".", start over.  If we find
1688
         a labeling of the current location, start over.  Finally, at
1689
         the end, if we require page alignment, add page alignments.  */
1690
 
1691
#if EXTRA_VALIDATION
1692
      size_t old_child_count;
1693
      size_t new_child_count;
1694
#endif
1695
      bfd_boolean no_reorder = FALSE;
1696
 
1697
      os = &statement->output_section_statement;
1698
 
1699
#if EXTRA_VALIDATION
1700
      old_child_count = ld_count_children (statement);
1701
#endif
1702
 
1703
      /* It is now officially a target.  Build the graph of source
1704
         section -> target section (kept as a list of edges).  */
1705
 
1706
      deps = ld_build_required_section_dependence (statement);
1707
 
1708
      /* If this wildcard does not reorder....  */
1709
      if (!no_reorder)
1710
        {
1711
          /* First check for reverse dependences.  Fix if possible.  */
1712
          xtensa_colocate_literals (deps, statement);
1713
 
1714
#if EXTRA_VALIDATION
1715
          new_child_count = ld_count_children (statement);
1716
          ASSERT (new_child_count == old_child_count);
1717
#endif
1718
        }
1719
 
1720
      /* Insert align/offset assignment statement.  */
1721
      if (xtensa_use_literal_pages)
1722
        {
1723
          ld_xtensa_insert_page_offsets (0, statement, deps,
1724
                                         xtensa_use_literal_pages);
1725
          lang_for_each_statement_worker (xtensa_ldlang_clear_addresses,
1726
                                          statement);
1727
        }
1728
 
1729
      /* Clean up.  */
1730
      free_reloc_deps_graph (deps);
1731
    }
1732
}
1733
 
1734
 
1735
static void
1736
xtensa_colocate_output_literals (lang_statement_union_type *s)
1737
{
1738
  lang_for_each_statement_worker (xtensa_colocate_output_literals_callback, s);
1739
}
1740
 
1741
 
1742
static void
1743
xtensa_ldlang_clear_addresses (lang_statement_union_type *statement)
1744
{
1745
  switch (statement->header.type)
1746
    {
1747
    case lang_input_section_enum:
1748
      {
1749
        asection *bfd_section = statement->input_section.section;
1750
        bfd_section->output_offset = 0;
1751
      }
1752
      break;
1753
    default:
1754
      break;
1755
    }
1756
}
1757
 
1758
 
1759
static bfd_vma
1760
ld_assign_relative_paged_dot (bfd_vma dot,
1761
                              lang_statement_union_type *s,
1762
                              const reloc_deps_graph *deps ATTRIBUTE_UNUSED,
1763
                              bfd_boolean lit_align)
1764
{
1765
  /* Walk through all of the input statements in this wild statement
1766
     assign dot to all of them.  */
1767
 
1768
  xtensa_ld_iter_stack *stack = NULL;
1769
  xtensa_ld_iter_stack **stack_p = &stack;
1770
 
1771
  bfd_boolean first_section = FALSE;
1772
  bfd_boolean in_literals = FALSE;
1773
 
1774
  for (iter_stack_create (stack_p, s);
1775
       !iter_stack_empty (stack_p);
1776
       iter_stack_next (stack_p))
1777
    {
1778
      lang_statement_union_type *l = iter_stack_current (stack_p);
1779
 
1780
      switch (l->header.type)
1781
        {
1782
        case lang_input_section_enum:
1783
          {
1784
            asection *section = l->input_section.section;
1785
            size_t align_pow = section->alignment_power;
1786
            bfd_boolean do_xtensa_alignment = FALSE;
1787
 
1788
            if (lit_align)
1789
              {
1790
                bfd_boolean sec_is_target = section_is_target (deps, l);
1791
                bfd_boolean sec_is_source = section_is_source (deps, l);
1792
 
1793
                if (section->size != 0
1794
                    && (first_section
1795
                        || (in_literals && !sec_is_target)
1796
                        || (!in_literals && sec_is_target)))
1797
                  {
1798
                    do_xtensa_alignment = TRUE;
1799
                  }
1800
                first_section = FALSE;
1801
                if (section->size != 0)
1802
                  in_literals = (sec_is_target && !sec_is_source);
1803
              }
1804
 
1805
            if (do_xtensa_alignment && xtensa_page_power != 0)
1806
              dot += (1 << xtensa_page_power);
1807
 
1808
            dot = align_power (dot, align_pow);
1809
            section->output_offset = dot;
1810
            dot += section->size;
1811
          }
1812
          break;
1813
        case lang_fill_statement_enum:
1814
          dot += l->fill_statement.size;
1815
          break;
1816
        case lang_padding_statement_enum:
1817
          dot += l->padding_statement.size;
1818
          break;
1819
        default:
1820
          break;
1821
        }
1822
    }
1823
  return dot;
1824
}
1825
 
1826
 
1827
static bfd_boolean
1828
ld_local_file_relocations_fit (lang_statement_union_type *statement,
1829
                               const reloc_deps_graph *deps ATTRIBUTE_UNUSED)
1830
{
1831
  /* Walk over all of the dependencies that we identified and make
1832
     sure that IF the source and target are here (addr != 0):
1833
     1) target addr < source addr
1834
     2) (roundup(source + source_size, 4) - rounddown(target, 4))
1835
        < (256K - (1 << bad align))
1836
     Need a worst-case proof....  */
1837
 
1838
  xtensa_ld_iter_stack *stack = NULL;
1839
  xtensa_ld_iter_stack **stack_p = &stack;
1840
  size_t max_align_power = 0;
1841
  size_t align_penalty = 256;
1842
  reloc_deps_e *e;
1843
  size_t i;
1844
 
1845
  /* Find the worst-case alignment requirement for this set of statements.  */
1846
  for (iter_stack_create (stack_p, statement);
1847
       !iter_stack_empty (stack_p);
1848
       iter_stack_next (stack_p))
1849
    {
1850
      lang_statement_union_type *l = iter_stack_current (stack_p);
1851
      if (l->header.type == lang_input_section_enum)
1852
        {
1853
          lang_input_section_type *input = &l->input_section;
1854
          asection *section = input->section;
1855
          if (section->alignment_power > max_align_power)
1856
            max_align_power = section->alignment_power;
1857
        }
1858
    }
1859
 
1860
  /* Now check that everything fits.  */
1861
  for (i = 0; i < deps->count; i++)
1862
    {
1863
      asection *sec = deps->sections[i];
1864
      const reloc_deps_section *deps_section =
1865
        xtensa_get_section_deps (deps, sec);
1866
      if (deps_section)
1867
        {
1868
          /* We choose to walk through the successors.  */
1869
          for (e = deps_section->succs; e != NULL; e = e->next)
1870
            {
1871
              if (e->src != e->tgt
1872
                  && e->src->output_section == e->tgt->output_section
1873
                  && e->src->output_offset != 0
1874
                  && e->tgt->output_offset != 0)
1875
                {
1876
                  bfd_vma l32r_addr =
1877
                    align_power (e->src->output_offset + e->src->size, 2);
1878
                  bfd_vma target_addr = e->tgt->output_offset & ~3;
1879
                  if (l32r_addr < target_addr)
1880
                    {
1881
                      fprintf (stderr, "Warning: "
1882
                               "l32r target section before l32r\n");
1883
                      return FALSE;
1884
                    }
1885
 
1886
                  if (l32r_addr - target_addr > 256 * 1024 - align_penalty)
1887
                    return FALSE;
1888
                }
1889
            }
1890
        }
1891
    }
1892
 
1893
  return TRUE;
1894
}
1895
 
1896
 
1897
static bfd_vma
1898
ld_xtensa_insert_page_offsets (bfd_vma dot,
1899
                               lang_statement_union_type *s,
1900
                               reloc_deps_graph *deps,
1901
                               bfd_boolean lit_align)
1902
{
1903
  xtensa_ld_iter_stack *stack = NULL;
1904
  xtensa_ld_iter_stack **stack_p = &stack;
1905
 
1906
  bfd_boolean first_section = FALSE;
1907
  bfd_boolean in_literals = FALSE;
1908
 
1909
  if (!lit_align)
1910
    return FALSE;
1911
 
1912
  for (iter_stack_create (stack_p, s);
1913
       !iter_stack_empty (stack_p);
1914
       iter_stack_next (stack_p))
1915
    {
1916
      lang_statement_union_type *l = iter_stack_current (stack_p);
1917
 
1918
      switch (l->header.type)
1919
        {
1920
        case lang_input_section_enum:
1921
          {
1922
            asection *section = l->input_section.section;
1923
            bfd_boolean do_xtensa_alignment = FALSE;
1924
 
1925
            if (lit_align)
1926
              {
1927
                if (section->size != 0
1928
                    && (first_section
1929
                        || (in_literals && !section_is_target (deps, l))
1930
                        || (!in_literals && section_is_target (deps, l))))
1931
                  {
1932
                    do_xtensa_alignment = TRUE;
1933
                  }
1934
                first_section = FALSE;
1935
                if (section->size != 0)
1936
                  {
1937
                    in_literals = (section_is_target (deps, l)
1938
                                   && !section_is_source (deps, l));
1939
                  }
1940
              }
1941
 
1942
            if (do_xtensa_alignment && xtensa_page_power != 0)
1943
              {
1944
                /* Create an expression that increments the current address,
1945
                   i.e., "dot", by (1 << xtensa_align_power).  */
1946
                etree_type *name_op = exp_nameop (NAME, ".");
1947
                etree_type *addend_op = exp_intop (1 << xtensa_page_power);
1948
                etree_type *add_op = exp_binop ('+', name_op, addend_op);
1949
                etree_type *assign_op = exp_assop ('=', ".", add_op);
1950
 
1951
                lang_assignment_statement_type *assign_stmt;
1952
                lang_statement_union_type *assign_union;
1953
                lang_statement_list_type tmplist;
1954
 
1955
                /* There is hidden state in "lang_add_assignment".  It
1956
                   appends the new assignment statement to the stat_ptr
1957
                   list.  Thus, we swap it before and after the call.  */
1958
 
1959
                lang_list_init (&tmplist);
1960
                push_stat_ptr (&tmplist);
1961
                /* Warning: side effect; statement appended to stat_ptr.  */
1962
                assign_stmt = lang_add_assignment (assign_op);
1963
                assign_union = (lang_statement_union_type *) assign_stmt;
1964
                pop_stat_ptr ();
1965
 
1966
                assign_union->header.next = l;
1967
                *(*stack_p)->iterloc.loc = assign_union;
1968
                iter_stack_next (stack_p);
1969
              }
1970
          }
1971
          break;
1972
        default:
1973
          break;
1974
        }
1975
    }
1976
  return dot;
1977
}
1978
 
1979
EOF
1980
 
1981
# Define some shell vars to insert bits of code into the standard ELF
1982
# parse_args and list_options functions.
1983
#
1984
PARSE_AND_LIST_PROLOGUE='
1985
#define OPTION_OPT_SIZEOPT              (300)
1986
#define OPTION_NO_RELAX                 (OPTION_OPT_SIZEOPT + 1)
1987
#define OPTION_LITERAL_MOVEMENT         (OPTION_NO_RELAX + 1)
1988
#define OPTION_NO_LITERAL_MOVEMENT      (OPTION_LITERAL_MOVEMENT + 1)
1989
extern int elf32xtensa_size_opt;
1990
extern int elf32xtensa_no_literal_movement;
1991
'
1992
 
1993
PARSE_AND_LIST_LONGOPTS='
1994
  { "size-opt", no_argument, NULL, OPTION_OPT_SIZEOPT},
1995
  { "no-relax", no_argument, NULL, OPTION_NO_RELAX},
1996
  { "literal-movement", no_argument, NULL, OPTION_LITERAL_MOVEMENT},
1997
  { "no-literal-movement", no_argument, NULL, OPTION_NO_LITERAL_MOVEMENT},
1998
'
1999
 
2000
PARSE_AND_LIST_OPTIONS='
2001
  fprintf (file, _("\
2002
  --size-opt                  When relaxing longcalls, prefer size\n\
2003
                                optimization over branch target alignment\n"));
2004
  fprintf (file, _("\
2005
  --no-relax                  Do not relax branches or coalesce literals\n"));
2006
'
2007
 
2008
PARSE_AND_LIST_ARGS_CASES='
2009
    case OPTION_OPT_SIZEOPT:
2010
      elf32xtensa_size_opt = 1;
2011
      break;
2012
    case OPTION_NO_RELAX:
2013
      disable_relaxation = TRUE;
2014
      break;
2015
    case OPTION_LITERAL_MOVEMENT:
2016
      elf32xtensa_no_literal_movement = 0;
2017
      break;
2018
    case OPTION_NO_LITERAL_MOVEMENT:
2019
      elf32xtensa_no_literal_movement = 1;
2020
      break;
2021
'
2022
 
2023
# Replace some of the standard ELF functions with our own versions.
2024
#
2025
LDEMUL_BEFORE_PARSE=elf_xtensa_before_parse
2026
LDEMUL_AFTER_OPEN=elf_xtensa_after_open
2027
LDEMUL_CHOOSE_TARGET=elf_xtensa_choose_target
2028
LDEMUL_BEFORE_ALLOCATION=elf_xtensa_before_allocation

powered by: WebSVN 2.1.0

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