Coverage Report

Created: 2026-07-25 10:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-spu.c
Line
Count
Source
1
/* SPU specific support for 32-bit ELF
2
3
   Copyright (C) 2006-2026 Free Software Foundation, Inc.
4
5
   This file is part of BFD, the Binary File Descriptor library.
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 along
18
   with this program; if not, write to the Free Software Foundation, Inc.,
19
   51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "libiberty.h"
23
#include "bfd.h"
24
#include "bfdlink.h"
25
#include "libbfd.h"
26
#include "elf-bfd.h"
27
#include "elf/spu.h"
28
#include "elf32-spu.h"
29
30
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
31
0
#define OCTETS_PER_BYTE(ABFD, SEC) 1
32
33
/* We use RELA style relocs.  Don't define USE_REL.  */
34
35
static bfd_reloc_status_type spu_elf_rel9 (bfd *, arelent *, asymbol *,
36
             void *, asection *,
37
             bfd *, char **);
38
39
/* Values of type 'enum elf_spu_reloc_type' are used to index this
40
   array, so it must be declared in the order of that type.  */
41
42
static reloc_howto_type elf_howto_table[] = {
43
  HOWTO (R_SPU_NONE,     0, 0,  0, false,  0, complain_overflow_dont,
44
   bfd_elf_generic_reloc, "SPU_NONE",
45
   false, 0, 0x00000000, false),
46
  HOWTO (R_SPU_ADDR10,     4, 4, 10, false, 14, complain_overflow_bitfield,
47
   bfd_elf_generic_reloc, "SPU_ADDR10",
48
   false, 0, 0x00ffc000, false),
49
  HOWTO (R_SPU_ADDR16,     2, 4, 16, false,  7, complain_overflow_bitfield,
50
   bfd_elf_generic_reloc, "SPU_ADDR16",
51
   false, 0, 0x007fff80, false),
52
  HOWTO (R_SPU_ADDR16_HI, 16, 4, 16, false,  7, complain_overflow_bitfield,
53
   bfd_elf_generic_reloc, "SPU_ADDR16_HI",
54
   false, 0, 0x007fff80, false),
55
  HOWTO (R_SPU_ADDR16_LO,  0, 4, 16, false,  7, complain_overflow_dont,
56
   bfd_elf_generic_reloc, "SPU_ADDR16_LO",
57
   false, 0, 0x007fff80, false),
58
  HOWTO (R_SPU_ADDR18,     0, 4, 18, false,  7, complain_overflow_bitfield,
59
   bfd_elf_generic_reloc, "SPU_ADDR18",
60
   false, 0, 0x01ffff80, false),
61
  HOWTO (R_SPU_ADDR32,     0, 4, 32, false,  0, complain_overflow_dont,
62
   bfd_elf_generic_reloc, "SPU_ADDR32",
63
   false, 0, 0xffffffff, false),
64
  HOWTO (R_SPU_REL16,    2, 4, 16,  true,  7, complain_overflow_bitfield,
65
   bfd_elf_generic_reloc, "SPU_REL16",
66
   false, 0, 0x007fff80, true),
67
  HOWTO (R_SPU_ADDR7,    0, 4,  7, false, 14, complain_overflow_dont,
68
   bfd_elf_generic_reloc, "SPU_ADDR7",
69
   false, 0, 0x001fc000, false),
70
  HOWTO (R_SPU_REL9,     2, 4,  9,  true,  0, complain_overflow_signed,
71
   spu_elf_rel9,    "SPU_REL9",
72
   false, 0, 0x0180007f, true),
73
  HOWTO (R_SPU_REL9I,    2, 4,  9,  true,  0, complain_overflow_signed,
74
   spu_elf_rel9,    "SPU_REL9I",
75
   false, 0, 0x0000c07f, true),
76
  HOWTO (R_SPU_ADDR10I,    0, 4, 10, false, 14, complain_overflow_signed,
77
   bfd_elf_generic_reloc, "SPU_ADDR10I",
78
   false, 0, 0x00ffc000, false),
79
  HOWTO (R_SPU_ADDR16I,    0, 4, 16, false,  7, complain_overflow_signed,
80
   bfd_elf_generic_reloc, "SPU_ADDR16I",
81
   false, 0, 0x007fff80, false),
82
  HOWTO (R_SPU_REL32,    0, 4, 32, true,  0, complain_overflow_dont,
83
   bfd_elf_generic_reloc, "SPU_REL32",
84
   false, 0, 0xffffffff, true),
85
  HOWTO (R_SPU_ADDR16X,    0, 4, 16, false,  7, complain_overflow_bitfield,
86
   bfd_elf_generic_reloc, "SPU_ADDR16X",
87
   false, 0, 0x007fff80, false),
88
  HOWTO (R_SPU_PPU32,    0, 4, 32, false,  0, complain_overflow_dont,
89
   bfd_elf_generic_reloc, "SPU_PPU32",
90
   false, 0, 0xffffffff, false),
91
  HOWTO (R_SPU_PPU64,    0, 8, 64, false,  0, complain_overflow_dont,
92
   bfd_elf_generic_reloc, "SPU_PPU64",
93
   false, 0, -1, false),
94
  HOWTO (R_SPU_ADD_PIC,    0, 0,  0, false,  0, complain_overflow_dont,
95
   bfd_elf_generic_reloc, "SPU_ADD_PIC",
96
   false, 0, 0x00000000, false),
97
};
98
99
static struct bfd_elf_special_section const spu_elf_special_sections[] = {
100
  { "._ea", 4, 0, SHT_PROGBITS, SHF_WRITE },
101
  { ".toe", 4, 0, SHT_NOBITS, SHF_ALLOC },
102
  { NULL, 0, 0, 0, 0 }
103
};
104
105
static enum elf_spu_reloc_type
106
spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code)
107
0
{
108
0
  switch (code)
109
0
    {
110
0
    default:
111
0
      return (enum elf_spu_reloc_type) -1;
112
0
    case BFD_RELOC_NONE:
113
0
      return R_SPU_NONE;
114
0
    case BFD_RELOC_SPU_IMM10W:
115
0
      return R_SPU_ADDR10;
116
0
    case BFD_RELOC_SPU_IMM16W:
117
0
      return R_SPU_ADDR16;
118
0
    case BFD_RELOC_SPU_LO16:
119
0
      return R_SPU_ADDR16_LO;
120
0
    case BFD_RELOC_SPU_HI16:
121
0
      return R_SPU_ADDR16_HI;
122
0
    case BFD_RELOC_SPU_IMM18:
123
0
      return R_SPU_ADDR18;
124
0
    case BFD_RELOC_SPU_PCREL16:
125
0
      return R_SPU_REL16;
126
0
    case BFD_RELOC_SPU_IMM7:
127
0
      return R_SPU_ADDR7;
128
0
    case BFD_RELOC_SPU_IMM8:
129
0
      return R_SPU_NONE;
130
0
    case BFD_RELOC_SPU_PCREL9a:
131
0
      return R_SPU_REL9;
132
0
    case BFD_RELOC_SPU_PCREL9b:
133
0
      return R_SPU_REL9I;
134
0
    case BFD_RELOC_SPU_IMM10:
135
0
      return R_SPU_ADDR10I;
136
0
    case BFD_RELOC_SPU_IMM16:
137
0
      return R_SPU_ADDR16I;
138
0
    case BFD_RELOC_32:
139
0
      return R_SPU_ADDR32;
140
0
    case BFD_RELOC_32_PCREL:
141
0
      return R_SPU_REL32;
142
0
    case BFD_RELOC_SPU_PPU32:
143
0
      return R_SPU_PPU32;
144
0
    case BFD_RELOC_SPU_PPU64:
145
0
      return R_SPU_PPU64;
146
0
    case BFD_RELOC_SPU_ADD_PIC:
147
0
      return R_SPU_ADD_PIC;
148
0
    }
149
0
}
150
151
static bool
152
spu_elf_info_to_howto (bfd *abfd,
153
           arelent *cache_ptr,
154
           Elf_Internal_Rela *dst)
155
74
{
156
74
  enum elf_spu_reloc_type r_type;
157
158
74
  r_type = (enum elf_spu_reloc_type) ELF32_R_TYPE (dst->r_info);
159
  /* PR 17512: file: 90c2a92e.  */
160
74
  if (r_type >= R_SPU_max)
161
7
    {
162
      /* xgettext:c-format */
163
7
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
164
7
        abfd, r_type);
165
7
      bfd_set_error (bfd_error_bad_value);
166
7
      return false;
167
7
    }
168
67
  cache_ptr->howto = &elf_howto_table[(int) r_type];
169
67
  return true;
170
74
}
171
172
static reloc_howto_type *
173
spu_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
174
         bfd_reloc_code_real_type code)
175
0
{
176
0
  enum elf_spu_reloc_type r_type = spu_elf_bfd_to_reloc_type (code);
177
178
0
  if (r_type == (enum elf_spu_reloc_type) -1)
179
0
    return NULL;
180
181
0
  return elf_howto_table + r_type;
182
0
}
183
184
static reloc_howto_type *
185
spu_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
186
         const char *r_name)
187
0
{
188
0
  unsigned int i;
189
190
0
  for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
191
0
    if (elf_howto_table[i].name != NULL
192
0
  && strcasecmp (elf_howto_table[i].name, r_name) == 0)
193
0
      return &elf_howto_table[i];
194
195
0
  return NULL;
196
0
}
197
198
/* Apply R_SPU_REL9 and R_SPU_REL9I relocs.  */
199
200
static bfd_reloc_status_type
201
spu_elf_rel9 (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
202
        void *data, asection *input_section,
203
        bfd *output_bfd, char **error_message)
204
0
{
205
0
  bfd_size_type octets;
206
0
  bfd_vma val;
207
0
  long insn;
208
209
  /* If this is a relocatable link (output_bfd test tells us), just
210
     call the generic function.  Any adjustment will be done at final
211
     link time.  */
212
0
  if (output_bfd != NULL)
213
0
    return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
214
0
          input_section, output_bfd, error_message);
215
216
0
  octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
217
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
218
0
          input_section, octets))
219
0
    return bfd_reloc_outofrange;
220
221
  /* Get symbol value.  */
222
0
  val = 0;
223
0
  if (!bfd_is_com_section (symbol->section))
224
0
    val = symbol->value;
225
0
  if (symbol->section->output_section)
226
0
    val += symbol->section->output_section->vma;
227
228
0
  val += reloc_entry->addend;
229
230
  /* Make it pc-relative.  */
231
0
  val -= input_section->output_section->vma + input_section->output_offset;
232
233
0
  val >>= 2;
234
0
  if (val + 256 >= 512)
235
0
    return bfd_reloc_overflow;
236
237
0
  insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
238
239
  /* Move two high bits of value to REL9I and REL9 position.
240
     The mask will take care of selecting the right field.  */
241
0
  val = (val & 0x7f) | ((val & 0x180) << 7) | ((val & 0x180) << 16);
242
0
  insn &= ~reloc_entry->howto->dst_mask;
243
0
  insn |= val & reloc_entry->howto->dst_mask;
244
0
  bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
245
0
  return bfd_reloc_ok;
246
0
}
247
248
static bool
249
spu_elf_new_section_hook (bfd *abfd, asection *sec)
250
9.84k
{
251
9.84k
  struct _spu_elf_section_data *sdata;
252
253
9.84k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
254
9.84k
  if (sdata == NULL)
255
0
    return false;
256
9.84k
  sec->used_by_bfd = sdata;
257
258
9.84k
  return _bfd_elf_new_section_hook (abfd, sec);
259
9.84k
}
260
261
/* Set up overlay info for executables.  */
262
263
static bool
264
spu_elf_object_p (bfd *abfd)
265
427
{
266
427
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
267
176
    {
268
176
      unsigned int i, num_ovl, num_buf;
269
176
      Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
270
176
      Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
271
176
      Elf_Internal_Phdr *last_phdr = NULL;
272
273
21.5k
      for (num_buf = 0, num_ovl = 0, i = 0; i < ehdr->e_phnum; i++, phdr++)
274
21.3k
  if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_OVERLAY) != 0)
275
376
    {
276
376
      unsigned int j;
277
278
376
      ++num_ovl;
279
376
      if (last_phdr == NULL
280
286
    || ((last_phdr->p_vaddr ^ phdr->p_vaddr) & 0x3ffff) != 0)
281
196
        ++num_buf;
282
376
      last_phdr = phdr;
283
500
      for (j = 1; j < elf_numsections (abfd); j++)
284
124
        {
285
124
    Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[j];
286
287
124
    if (shdr->bfd_section != NULL
288
0
        && ELF_SECTION_SIZE (shdr, phdr) != 0
289
0
        && ELF_SECTION_IN_SEGMENT (shdr, phdr))
290
0
      {
291
0
        asection *sec = shdr->bfd_section;
292
0
        spu_elf_section_data (sec)->u.o.ovl_index = num_ovl;
293
0
        spu_elf_section_data (sec)->u.o.ovl_buf = num_buf;
294
0
      }
295
124
        }
296
376
    }
297
176
    }
298
427
  return true;
299
427
}
300
301
/* Specially mark defined symbols named _EAR_* with BSF_KEEP so that
302
   strip --strip-unneeded will not remove them.  */
303
304
static void
305
spu_elf_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
306
85
{
307
85
  if (sym->name != NULL
308
85
      && sym->section != bfd_abs_section_ptr
309
54
      && startswith (sym->name, "_EAR_"))
310
0
    sym->flags |= BSF_KEEP;
311
85
}
312
313
/* SPU ELF linker hash table.  */
314
315
struct spu_link_hash_table
316
{
317
  struct elf_link_hash_table elf;
318
319
  struct spu_elf_params *params;
320
321
  /* Shortcuts to overlay sections.  */
322
  asection *ovtab;
323
  asection *init;
324
  asection *toe;
325
  asection **ovl_sec;
326
327
  /* Count of stubs in each overlay section.  */
328
  unsigned int *stub_count;
329
330
  /* The stub section for each overlay section.  */
331
  asection **stub_sec;
332
333
  struct elf_link_hash_entry *ovly_entry[2];
334
335
  /* Number of overlay buffers.  */
336
  unsigned int num_buf;
337
338
  /* Total number of overlays.  */
339
  unsigned int num_overlays;
340
341
  /* For soft icache.  */
342
  unsigned int line_size_log2;
343
  unsigned int num_lines_log2;
344
  unsigned int fromelem_size_log2;
345
346
  /* How much memory we have.  */
347
  unsigned int local_store;
348
349
  /* Count of overlay stubs needed in non-overlay area.  */
350
  unsigned int non_ovly_stub;
351
352
  /* Pointer to the fixup section */
353
  asection *sfixup;
354
355
  /* Set on error.  */
356
  unsigned int stub_err : 1;
357
};
358
359
/* Hijack the generic got fields for overlay stub accounting.  */
360
361
struct got_entry
362
{
363
  struct got_entry *next;
364
  unsigned int ovl;
365
  union {
366
    bfd_vma addend;
367
    bfd_vma br_addr;
368
  };
369
  bfd_vma stub_addr;
370
};
371
372
#define spu_hash_table(p) \
373
0
  ((is_elf_hash_table ((p)->hash)          \
374
0
    && elf_hash_table_id (elf_hash_table (p)) == SPU_ELF_DATA)   \
375
0
   ? (struct spu_link_hash_table *) (p)->hash : NULL)
376
377
struct call_info
378
{
379
  struct function_info *fun;
380
  struct call_info *next;
381
  unsigned int count;
382
  unsigned int max_depth;
383
  unsigned int is_tail : 1;
384
  unsigned int is_pasted : 1;
385
  unsigned int broken_cycle : 1;
386
  unsigned int priority : 13;
387
};
388
389
struct function_info
390
{
391
  /* List of functions called.  Also branches to hot/cold part of
392
     function.  */
393
  struct call_info *call_list;
394
  /* For hot/cold part of function, point to owner.  */
395
  struct function_info *start;
396
  /* Symbol at start of function.  */
397
  union {
398
    Elf_Internal_Sym *sym;
399
    struct elf_link_hash_entry *h;
400
  } u;
401
  /* Function section.  */
402
  asection *sec;
403
  asection *rodata;
404
  /* Where last called from, and number of sections called from.  */
405
  asection *last_caller;
406
  unsigned int call_count;
407
  /* Address range of (this part of) function.  */
408
  bfd_vma lo, hi;
409
  /* Offset where we found a store of lr, or -1 if none found.  */
410
  bfd_vma lr_store;
411
  /* Offset where we found the stack adjustment insn.  */
412
  bfd_vma sp_adjust;
413
  /* Stack usage.  */
414
  int stack;
415
  /* Distance from root of call tree.  Tail and hot/cold branches
416
     count as one deeper.  We aren't counting stack frames here.  */
417
  unsigned int depth;
418
  /* Set if global symbol.  */
419
  unsigned int global : 1;
420
  /* Set if known to be start of function (as distinct from a hunk
421
     in hot/cold section.  */
422
  unsigned int is_func : 1;
423
  /* Set if not a root node.  */
424
  unsigned int non_root : 1;
425
  /* Flags used during call tree traversal.  It's cheaper to replicate
426
     the visit flags than have one which needs clearing after a traversal.  */
427
  unsigned int visit1 : 1;
428
  unsigned int visit2 : 1;
429
  unsigned int marking : 1;
430
  unsigned int visit3 : 1;
431
  unsigned int visit4 : 1;
432
  unsigned int visit5 : 1;
433
  unsigned int visit6 : 1;
434
  unsigned int visit7 : 1;
435
};
436
437
struct spu_elf_stack_info
438
{
439
  int num_fun;
440
  int max_fun;
441
  /* Variable size array describing functions, one per contiguous
442
     address range belonging to a function.  */
443
  struct function_info fun[1];
444
};
445
446
static struct function_info *find_function (asection *, bfd_vma,
447
              struct bfd_link_info *);
448
449
/* Create a spu ELF linker hash table.  */
450
451
static struct bfd_link_hash_table *
452
spu_elf_link_hash_table_create (bfd *abfd)
453
0
{
454
0
  struct spu_link_hash_table *htab;
455
456
0
  htab = bfd_zmalloc (sizeof (*htab));
457
0
  if (htab == NULL)
458
0
    return NULL;
459
460
0
  if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd,
461
0
              _bfd_elf_link_hash_newfunc,
462
0
              sizeof (struct elf_link_hash_entry)))
463
0
    {
464
0
      free (htab);
465
0
      return NULL;
466
0
    }
467
468
0
  htab->elf.init_got_refcount.refcount = 0;
469
0
  htab->elf.init_got_refcount.glist = NULL;
470
0
  htab->elf.init_got_offset.offset = 0;
471
0
  htab->elf.init_got_offset.glist = NULL;
472
0
  return &htab->elf.root;
473
0
}
474
475
void
476
spu_elf_setup (struct bfd_link_info *info, struct spu_elf_params *params)
477
0
{
478
0
  bfd_vma max_branch_log2;
479
480
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
481
0
  htab->params = params;
482
0
  htab->line_size_log2 = bfd_log2 (htab->params->line_size);
483
0
  htab->num_lines_log2 = bfd_log2 (htab->params->num_lines);
484
485
  /* For the software i-cache, we provide a "from" list whose size
486
     is a power-of-two number of quadwords, big enough to hold one
487
     byte per outgoing branch.  Compute this number here.  */
488
0
  max_branch_log2 = bfd_log2 (htab->params->max_branch);
489
0
  htab->fromelem_size_log2 = max_branch_log2 > 4 ? max_branch_log2 - 4 : 0;
490
0
}
491
492
/* Find the symbol for the given R_SYMNDX in IBFD and set *HP and *SYMP
493
   to (hash, NULL) for global symbols, and (NULL, sym) for locals.  Set
494
   *SYMSECP to the symbol's section.  *LOCSYMSP caches local syms.  */
495
496
static bool
497
get_sym_h (struct elf_link_hash_entry **hp,
498
     Elf_Internal_Sym **symp,
499
     asection **symsecp,
500
     Elf_Internal_Sym **locsymsp,
501
     unsigned long r_symndx,
502
     bfd *ibfd)
503
0
{
504
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
505
506
0
  if (r_symndx >= symtab_hdr->sh_info)
507
0
    {
508
0
      struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
509
0
      struct elf_link_hash_entry *h;
510
511
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
512
0
      while (h->root.type == bfd_link_hash_indirect
513
0
       || h->root.type == bfd_link_hash_warning)
514
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
515
516
0
      if (hp != NULL)
517
0
  *hp = h;
518
519
0
      if (symp != NULL)
520
0
  *symp = NULL;
521
522
0
      if (symsecp != NULL)
523
0
  {
524
0
    asection *symsec = NULL;
525
0
    if (h->root.type == bfd_link_hash_defined
526
0
        || h->root.type == bfd_link_hash_defweak)
527
0
      symsec = h->root.u.def.section;
528
0
    *symsecp = symsec;
529
0
  }
530
0
    }
531
0
  else
532
0
    {
533
0
      Elf_Internal_Sym *sym;
534
0
      Elf_Internal_Sym *locsyms = *locsymsp;
535
536
0
      if (locsyms == NULL)
537
0
  {
538
0
    locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
539
0
    if (locsyms == NULL)
540
0
      locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
541
0
              symtab_hdr->sh_info,
542
0
              0, NULL, NULL, NULL);
543
0
    if (locsyms == NULL)
544
0
      return false;
545
0
    *locsymsp = locsyms;
546
0
  }
547
0
      sym = locsyms + r_symndx;
548
549
0
      if (hp != NULL)
550
0
  *hp = NULL;
551
552
0
      if (symp != NULL)
553
0
  *symp = sym;
554
555
0
      if (symsecp != NULL)
556
0
  *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
557
0
    }
558
559
0
  return true;
560
0
}
561
562
/* Create the note section if not already present.  This is done early so
563
   that the linker maps the sections to the right place in the output.  */
564
565
bool
566
spu_elf_create_sections (struct bfd_link_info *info)
567
0
{
568
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
569
0
  bfd *ibfd;
570
571
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
572
0
    if (bfd_get_section_by_name (ibfd, SPU_PTNOTE_SPUNAME) != NULL)
573
0
      break;
574
575
0
  if (ibfd == NULL)
576
0
    {
577
      /* Make SPU_PTNOTE_SPUNAME section.  */
578
0
      asection *s;
579
0
      size_t name_len;
580
0
      size_t size;
581
0
      bfd_byte *data;
582
0
      flagword flags;
583
584
0
      ibfd = info->input_bfds;
585
      /* This should really be SEC_LINKER_CREATED, but then we'd need
586
   to write out the section ourselves.  */
587
0
      flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
588
0
      s = bfd_make_section_anyway_with_flags (ibfd, SPU_PTNOTE_SPUNAME, flags);
589
0
      if (s == NULL
590
0
    || !bfd_set_section_alignment (s, 2))
591
0
  return false;
592
      /* Because we didn't set SEC_LINKER_CREATED we need to set the
593
   proper section type.  */
594
0
      elf_section_type (s) = SHT_NOTE;
595
596
0
      name_len = strlen (bfd_get_filename (info->output_bfd)) + 1;
597
0
      size = 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4);
598
0
      size += (name_len + 3) & -4;
599
600
0
      if (!bfd_set_section_size (s, size))
601
0
  return false;
602
603
0
      data = bfd_zalloc (ibfd, size);
604
0
      if (data == NULL)
605
0
  return false;
606
607
0
      bfd_put_32 (ibfd, sizeof (SPU_PLUGIN_NAME), data + 0);
608
0
      bfd_put_32 (ibfd, name_len, data + 4);
609
0
      bfd_put_32 (ibfd, 1, data + 8);
610
0
      memcpy (data + 12, SPU_PLUGIN_NAME, sizeof (SPU_PLUGIN_NAME));
611
0
      memcpy (data + 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4),
612
0
        bfd_get_filename (info->output_bfd), name_len);
613
0
      s->contents = data;
614
0
      s->alloced = 1;
615
0
    }
616
617
0
  if (htab->params->emit_fixups)
618
0
    {
619
0
      asection *s;
620
0
      flagword flags;
621
622
0
      if (htab->elf.dynobj == NULL)
623
0
  htab->elf.dynobj = ibfd;
624
0
      ibfd = htab->elf.dynobj;
625
0
      flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY | SEC_HAS_CONTENTS
626
0
         | SEC_IN_MEMORY | SEC_LINKER_CREATED);
627
0
      s = bfd_make_section_anyway_with_flags (ibfd, ".fixup", flags);
628
0
      if (s == NULL || !bfd_set_section_alignment (s, 2))
629
0
  return false;
630
0
      htab->sfixup = s;
631
0
    }
632
633
0
  return true;
634
0
}
635
636
/* qsort predicate to sort sections by vma.  */
637
638
static int
639
sort_sections (const void *a, const void *b)
640
0
{
641
0
  const asection *const *s1 = a;
642
0
  const asection *const *s2 = b;
643
0
  bfd_signed_vma delta = (*s1)->vma - (*s2)->vma;
644
645
0
  if (delta != 0)
646
0
    return delta < 0 ? -1 : 1;
647
648
0
  return (*s1)->index - (*s2)->index;
649
0
}
650
651
/* Identify overlays in the output bfd, and number them.
652
   Returns 0 on error, 1 if no overlays, 2 if overlays.  */
653
654
int
655
spu_elf_find_overlays (struct bfd_link_info *info)
656
0
{
657
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
658
0
  asection **alloc_sec;
659
0
  unsigned int i, n, ovl_index, num_buf;
660
0
  asection *s;
661
0
  bfd_vma ovl_end;
662
0
  static const char *const entry_names[2][2] = {
663
0
    { "__ovly_load", "__icache_br_handler" },
664
0
    { "__ovly_return", "__icache_call_handler" }
665
0
  };
666
667
0
  if (info->output_bfd->section_count < 2)
668
0
    return 1;
669
670
0
  alloc_sec
671
0
    = bfd_malloc (info->output_bfd->section_count * sizeof (*alloc_sec));
672
0
  if (alloc_sec == NULL)
673
0
    return 0;
674
675
  /* Pick out all the alloced sections.  */
676
0
  for (n = 0, s = info->output_bfd->sections; s != NULL; s = s->next)
677
0
    if ((s->flags & SEC_ALLOC) != 0
678
0
  && (s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != SEC_THREAD_LOCAL
679
0
  && s->size != 0)
680
0
      alloc_sec[n++] = s;
681
682
0
  if (n == 0)
683
0
    {
684
0
      free (alloc_sec);
685
0
      return 1;
686
0
    }
687
688
  /* Sort them by vma.  */
689
0
  qsort (alloc_sec, n, sizeof (*alloc_sec), sort_sections);
690
691
0
  ovl_end = alloc_sec[0]->vma + alloc_sec[0]->size;
692
0
  if (htab->params->ovly_flavour == ovly_soft_icache)
693
0
    {
694
0
      unsigned int prev_buf = 0, set_id = 0;
695
696
      /* Look for an overlapping vma to find the first overlay section.  */
697
0
      bfd_vma vma_start = 0;
698
699
0
      for (i = 1; i < n; i++)
700
0
  {
701
0
    s = alloc_sec[i];
702
0
    if (s->vma < ovl_end)
703
0
      {
704
0
        asection *s0 = alloc_sec[i - 1];
705
0
        vma_start = s0->vma;
706
0
        ovl_end = (s0->vma
707
0
       + ((bfd_vma) 1
708
0
          << (htab->num_lines_log2 + htab->line_size_log2)));
709
0
        --i;
710
0
        break;
711
0
      }
712
0
    else
713
0
      ovl_end = s->vma + s->size;
714
0
  }
715
716
      /* Now find any sections within the cache area.  */
717
0
      for (ovl_index = 0, num_buf = 0; i < n; i++)
718
0
  {
719
0
    s = alloc_sec[i];
720
0
    if (s->vma >= ovl_end)
721
0
      break;
722
723
    /* A section in an overlay area called .ovl.init is not
724
       an overlay, in the sense that it might be loaded in
725
       by the overlay manager, but rather the initial
726
       section contents for the overlay buffer.  */
727
0
    if (!startswith (s->name, ".ovl.init"))
728
0
      {
729
0
        num_buf = ((s->vma - vma_start) >> htab->line_size_log2) + 1;
730
0
        set_id = (num_buf == prev_buf)? set_id + 1 : 0;
731
0
        prev_buf = num_buf;
732
733
0
        if ((s->vma - vma_start) & (htab->params->line_size - 1))
734
0
    {
735
0
      info->callbacks->einfo (_("%X%P: overlay section %pA "
736
0
              "does not start on a cache line\n"),
737
0
            s);
738
0
      bfd_set_error (bfd_error_bad_value);
739
0
      return 0;
740
0
    }
741
0
        else if (s->size > htab->params->line_size)
742
0
    {
743
0
      info->callbacks->einfo (_("%X%P: overlay section %pA "
744
0
              "is larger than a cache line\n"),
745
0
            s);
746
0
      bfd_set_error (bfd_error_bad_value);
747
0
      return 0;
748
0
    }
749
750
0
        alloc_sec[ovl_index++] = s;
751
0
        spu_elf_section_data (s)->u.o.ovl_index
752
0
    = (set_id << htab->num_lines_log2) + num_buf;
753
0
        spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
754
0
      }
755
0
  }
756
757
      /* Ensure there are no more overlay sections.  */
758
0
      for ( ; i < n; i++)
759
0
  {
760
0
    s = alloc_sec[i];
761
0
    if (s->vma < ovl_end)
762
0
      {
763
0
        info->callbacks->einfo (_("%X%P: overlay section %pA "
764
0
          "is not in cache area\n"),
765
0
              alloc_sec[i-1]);
766
0
        bfd_set_error (bfd_error_bad_value);
767
0
        return 0;
768
0
      }
769
0
    else
770
0
      ovl_end = s->vma + s->size;
771
0
  }
772
0
    }
773
0
  else
774
0
    {
775
      /* Look for overlapping vmas.  Any with overlap must be overlays.
776
   Count them.  Also count the number of overlay regions.  */
777
0
      for (ovl_index = 0, num_buf = 0, i = 1; i < n; i++)
778
0
  {
779
0
    s = alloc_sec[i];
780
0
    if (s->vma < ovl_end)
781
0
      {
782
0
        asection *s0 = alloc_sec[i - 1];
783
784
0
        if (spu_elf_section_data (s0)->u.o.ovl_index == 0)
785
0
    {
786
0
      ++num_buf;
787
0
      if (!startswith (s0->name, ".ovl.init"))
788
0
        {
789
0
          alloc_sec[ovl_index] = s0;
790
0
          spu_elf_section_data (s0)->u.o.ovl_index = ++ovl_index;
791
0
          spu_elf_section_data (s0)->u.o.ovl_buf = num_buf;
792
0
        }
793
0
      else
794
0
        ovl_end = s->vma + s->size;
795
0
    }
796
0
        if (!startswith (s->name, ".ovl.init"))
797
0
    {
798
0
      alloc_sec[ovl_index] = s;
799
0
      spu_elf_section_data (s)->u.o.ovl_index = ++ovl_index;
800
0
      spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
801
0
      if (s0->vma != s->vma)
802
0
        {
803
          /* xgettext:c-format */
804
0
          info->callbacks->einfo (_("%X%P: overlay sections %pA "
805
0
            "and %pA do not start at the "
806
0
            "same address\n"),
807
0
                s0, s);
808
0
          bfd_set_error (bfd_error_bad_value);
809
0
          return 0;
810
0
        }
811
0
      if (ovl_end < s->vma + s->size)
812
0
        ovl_end = s->vma + s->size;
813
0
    }
814
0
      }
815
0
    else
816
0
      ovl_end = s->vma + s->size;
817
0
  }
818
0
    }
819
820
0
  htab->num_overlays = ovl_index;
821
0
  htab->num_buf = num_buf;
822
0
  htab->ovl_sec = alloc_sec;
823
824
0
  if (ovl_index == 0)
825
0
    return 1;
826
827
0
  for (i = 0; i < 2; i++)
828
0
    {
829
0
      const char *name;
830
0
      struct elf_link_hash_entry *h;
831
832
0
      name = entry_names[i][htab->params->ovly_flavour];
833
0
      h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
834
0
      if (h == NULL)
835
0
  return 0;
836
837
0
      if (h->root.type == bfd_link_hash_new)
838
0
  {
839
0
    h->root.type = bfd_link_hash_undefined;
840
0
    h->ref_regular = 1;
841
0
    h->ref_regular_nonweak = 1;
842
0
    h->non_elf = 0;
843
0
  }
844
0
      htab->ovly_entry[i] = h;
845
0
    }
846
847
0
  return 2;
848
0
}
849
850
/* Non-zero to use bra in overlay stubs rather than br.  */
851
0
#define BRA_STUBS 0
852
853
#define BRA 0x30000000
854
#define BRASL 0x31000000
855
#define BR  0x32000000
856
#define BRSL  0x33000000
857
#define NOP 0x40200000
858
#define LNOP  0x00200000
859
#define ILA 0x42000000
860
861
/* Return true for all relative and absolute branch instructions.
862
   bra   00110000 0..
863
   brasl 00110001 0..
864
   br    00110010 0..
865
   brsl  00110011 0..
866
   brz   00100000 0..
867
   brnz  00100001 0..
868
   brhz  00100010 0..
869
   brhnz 00100011 0..  */
870
871
static bool
872
is_branch (const unsigned char *insn)
873
0
{
874
0
  return (insn[0] & 0xec) == 0x20 && (insn[1] & 0x80) == 0;
875
0
}
876
877
/* Return true for all indirect branch instructions.
878
   bi     00110101 000
879
   bisl   00110101 001
880
   iret   00110101 010
881
   bisled 00110101 011
882
   biz    00100101 000
883
   binz   00100101 001
884
   bihz   00100101 010
885
   bihnz  00100101 011  */
886
887
static bool
888
is_indirect_branch (const unsigned char *insn)
889
0
{
890
0
  return (insn[0] & 0xef) == 0x25 && (insn[1] & 0x80) == 0;
891
0
}
892
893
/* Return true for branch hint instructions.
894
   hbra  0001000..
895
   hbrr  0001001..  */
896
897
static bool
898
is_hint (const unsigned char *insn)
899
0
{
900
0
  return (insn[0] & 0xfc) == 0x10;
901
0
}
902
903
/* True if INPUT_SECTION might need overlay stubs.  */
904
905
static bool
906
maybe_needs_stubs (asection *input_section)
907
0
{
908
  /* No stubs for debug sections and suchlike.  */
909
0
  if ((input_section->flags & SEC_ALLOC) == 0)
910
0
    return false;
911
912
  /* No stubs for link-once sections that will be discarded.  */
913
0
  if (input_section->output_section == bfd_abs_section_ptr)
914
0
    return false;
915
916
  /* Don't create stubs for .eh_frame references.  */
917
0
  if (strcmp (input_section->name, ".eh_frame") == 0)
918
0
    return false;
919
920
0
  return true;
921
0
}
922
923
enum _stub_type
924
{
925
  no_stub,
926
  call_ovl_stub,
927
  br000_ovl_stub,
928
  br001_ovl_stub,
929
  br010_ovl_stub,
930
  br011_ovl_stub,
931
  br100_ovl_stub,
932
  br101_ovl_stub,
933
  br110_ovl_stub,
934
  br111_ovl_stub,
935
  nonovl_stub,
936
  stub_error
937
};
938
939
/* Return non-zero if this reloc symbol should go via an overlay stub.
940
   Return 2 if the stub must be in non-overlay area.  */
941
942
static enum _stub_type
943
needs_ovl_stub (struct elf_link_hash_entry *h,
944
    Elf_Internal_Sym *sym,
945
    asection *sym_sec,
946
    asection *input_section,
947
    Elf_Internal_Rela *irela,
948
    bfd_byte *contents,
949
    struct bfd_link_info *info)
950
0
{
951
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
952
0
  enum elf_spu_reloc_type r_type;
953
0
  unsigned int sym_type;
954
0
  bool branch, hint, call;
955
0
  enum _stub_type ret = no_stub;
956
0
  bfd_byte insn[4];
957
958
0
  if (sym_sec == NULL
959
0
      || sym_sec->output_section == bfd_abs_section_ptr
960
0
      || spu_elf_section_data (sym_sec->output_section) == NULL)
961
0
    return ret;
962
963
0
  if (h != NULL)
964
0
    {
965
      /* Ensure no stubs for user supplied overlay manager syms.  */
966
0
      if (h == htab->ovly_entry[0] || h == htab->ovly_entry[1])
967
0
  return ret;
968
969
      /* setjmp always goes via an overlay stub, because then the return
970
   and hence the longjmp goes via __ovly_return.  That magically
971
   makes setjmp/longjmp between overlays work.  */
972
0
      if (startswith (h->root.root.string, "setjmp")
973
0
    && (h->root.root.string[6] == '\0' || h->root.root.string[6] == '@'))
974
0
  ret = call_ovl_stub;
975
0
    }
976
977
0
  if (h != NULL)
978
0
    sym_type = h->type;
979
0
  else
980
0
    sym_type = ELF_ST_TYPE (sym->st_info);
981
982
0
  r_type = ELF32_R_TYPE (irela->r_info);
983
0
  branch = false;
984
0
  hint = false;
985
0
  call = false;
986
0
  if (r_type == R_SPU_REL16 || r_type == R_SPU_ADDR16)
987
0
    {
988
0
      if (contents == NULL)
989
0
  {
990
0
    contents = insn;
991
0
    if (!bfd_get_section_contents (input_section->owner,
992
0
           input_section,
993
0
           contents,
994
0
           irela->r_offset, 4))
995
0
      return stub_error;
996
0
  }
997
0
      else
998
0
  contents += irela->r_offset;
999
1000
0
      branch = is_branch (contents);
1001
0
      hint = is_hint (contents);
1002
0
      if (branch || hint)
1003
0
  {
1004
0
    call = (contents[0] & 0xfd) == 0x31;
1005
0
    if (call
1006
0
        && sym_type != STT_FUNC
1007
0
        && contents != insn)
1008
0
      {
1009
        /* It's common for people to write assembly and forget
1010
     to give function symbols the right type.  Handle
1011
     calls to such symbols, but warn so that (hopefully)
1012
     people will fix their code.  We need the symbol
1013
     type to be correct to distinguish function pointer
1014
     initialisation from other pointer initialisations.  */
1015
0
        const char *sym_name;
1016
1017
0
        if (h != NULL)
1018
0
    sym_name = h->root.root.string;
1019
0
        else
1020
0
    {
1021
0
      Elf_Internal_Shdr *symtab_hdr;
1022
0
      symtab_hdr = &elf_symtab_hdr (input_section->owner);
1023
0
      sym_name = bfd_elf_sym_name (input_section->owner,
1024
0
                 symtab_hdr,
1025
0
                 sym,
1026
0
                 sym_sec);
1027
0
    }
1028
0
        _bfd_error_handler
1029
    /* xgettext:c-format */
1030
0
    (_("warning: call to non-function symbol %s defined in %pB"),
1031
0
     sym_name, sym_sec->owner);
1032
1033
0
      }
1034
0
  }
1035
0
    }
1036
1037
0
  if ((!branch && htab->params->ovly_flavour == ovly_soft_icache)
1038
0
      || (sym_type != STT_FUNC
1039
0
    && !(branch || hint)
1040
0
    && (sym_sec->flags & SEC_CODE) == 0))
1041
0
    return no_stub;
1042
1043
  /* Usually, symbols in non-overlay sections don't need stubs.  */
1044
0
  if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index == 0
1045
0
      && !htab->params->non_overlay_stubs)
1046
0
    return ret;
1047
1048
  /* A reference from some other section to a symbol in an overlay
1049
     section needs a stub.  */
1050
0
  if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index
1051
0
       != spu_elf_section_data (input_section->output_section)->u.o.ovl_index)
1052
0
    {
1053
0
      unsigned int lrlive = 0;
1054
0
      if (branch)
1055
0
  lrlive = (contents[1] & 0x70) >> 4;
1056
1057
0
      if (!lrlive && (call || sym_type == STT_FUNC))
1058
0
  ret = call_ovl_stub;
1059
0
      else
1060
0
  ret = br000_ovl_stub + lrlive;
1061
0
    }
1062
1063
  /* If this insn isn't a branch then we are possibly taking the
1064
     address of a function and passing it out somehow.  Soft-icache code
1065
     always generates inline code to do indirect branches.  */
1066
0
  if (!(branch || hint)
1067
0
      && sym_type == STT_FUNC
1068
0
      && htab->params->ovly_flavour != ovly_soft_icache)
1069
0
    ret = nonovl_stub;
1070
1071
0
  return ret;
1072
0
}
1073
1074
static bool
1075
count_stub (struct spu_link_hash_table *htab,
1076
      bfd *ibfd,
1077
      asection *isec,
1078
      enum _stub_type stub_type,
1079
      struct elf_link_hash_entry *h,
1080
      const Elf_Internal_Rela *irela)
1081
0
{
1082
0
  unsigned int ovl = 0;
1083
0
  struct got_entry *g, **head;
1084
0
  bfd_vma addend;
1085
1086
  /* If this instruction is a branch or call, we need a stub
1087
     for it.  One stub per function per overlay.
1088
     If it isn't a branch, then we are taking the address of
1089
     this function so need a stub in the non-overlay area
1090
     for it.  One stub per function.  */
1091
0
  if (stub_type != nonovl_stub)
1092
0
    ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index;
1093
1094
0
  if (h != NULL)
1095
0
    head = &h->got.glist;
1096
0
  else
1097
0
    {
1098
0
      if (elf_local_got_ents (ibfd) == NULL)
1099
0
  {
1100
0
    bfd_size_type amt = (elf_symtab_hdr (ibfd).sh_info
1101
0
             * sizeof (*elf_local_got_ents (ibfd)));
1102
0
    elf_local_got_ents (ibfd) = bfd_zmalloc (amt);
1103
0
    if (elf_local_got_ents (ibfd) == NULL)
1104
0
      return false;
1105
0
  }
1106
0
      head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
1107
0
    }
1108
1109
0
  if (htab->params->ovly_flavour == ovly_soft_icache)
1110
0
    {
1111
0
      htab->stub_count[ovl] += 1;
1112
0
      return true;
1113
0
    }
1114
1115
0
  addend = 0;
1116
0
  if (irela != NULL)
1117
0
    addend = irela->r_addend;
1118
1119
0
  if (ovl == 0)
1120
0
    {
1121
0
      struct got_entry *gnext;
1122
1123
0
      for (g = *head; g != NULL; g = g->next)
1124
0
  if (g->addend == addend && g->ovl == 0)
1125
0
    break;
1126
1127
0
      if (g == NULL)
1128
0
  {
1129
    /* Need a new non-overlay area stub.  Zap other stubs.  */
1130
0
    for (g = *head; g != NULL; g = gnext)
1131
0
      {
1132
0
        gnext = g->next;
1133
0
        if (g->addend == addend)
1134
0
    {
1135
0
      htab->stub_count[g->ovl] -= 1;
1136
0
      free (g);
1137
0
    }
1138
0
      }
1139
0
  }
1140
0
    }
1141
0
  else
1142
0
    {
1143
0
      for (g = *head; g != NULL; g = g->next)
1144
0
  if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
1145
0
    break;
1146
0
    }
1147
1148
0
  if (g == NULL)
1149
0
    {
1150
0
      g = bfd_malloc (sizeof *g);
1151
0
      if (g == NULL)
1152
0
  return false;
1153
0
      g->ovl = ovl;
1154
0
      g->addend = addend;
1155
0
      g->stub_addr = (bfd_vma) -1;
1156
0
      g->next = *head;
1157
0
      *head = g;
1158
1159
0
      htab->stub_count[ovl] += 1;
1160
0
    }
1161
1162
0
  return true;
1163
0
}
1164
1165
/* Support two sizes of overlay stubs, a slower more compact stub of two
1166
   instructions, and a faster stub of four instructions.
1167
   Soft-icache stubs are four or eight words.  */
1168
1169
static unsigned int
1170
ovl_stub_size (struct spu_elf_params *params)
1171
0
{
1172
0
  return 16 << params->ovly_flavour >> params->compact_stub;
1173
0
}
1174
1175
static unsigned int
1176
ovl_stub_size_log2 (struct spu_elf_params *params)
1177
0
{
1178
0
  return 4 + params->ovly_flavour - params->compact_stub;
1179
0
}
1180
1181
/* Two instruction overlay stubs look like:
1182
1183
   brsl $75,__ovly_load
1184
   .word target_ovl_and_address
1185
1186
   ovl_and_address is a word with the overlay number in the top 14 bits
1187
   and local store address in the bottom 18 bits.
1188
1189
   Four instruction overlay stubs look like:
1190
1191
   ila $78,ovl_number
1192
   lnop
1193
   ila $79,target_address
1194
   br __ovly_load
1195
1196
   Software icache stubs are:
1197
1198
   .word target_index
1199
   .word target_ia;
1200
   .word lrlive_branchlocalstoreaddr;
1201
   brasl $75,__icache_br_handler
1202
   .quad xor_pattern
1203
*/
1204
1205
static bool
1206
build_stub (struct bfd_link_info *info,
1207
      bfd *ibfd,
1208
      asection *isec,
1209
      enum _stub_type stub_type,
1210
      struct elf_link_hash_entry *h,
1211
      const Elf_Internal_Rela *irela,
1212
      bfd_vma dest,
1213
      asection *dest_sec)
1214
0
{
1215
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
1216
0
  unsigned int ovl, dest_ovl, set_id;
1217
0
  struct got_entry *g, **head;
1218
0
  asection *sec;
1219
0
  bfd_vma addend, from, to, br_dest, patt;
1220
0
  unsigned int lrlive;
1221
1222
0
  ovl = 0;
1223
0
  if (stub_type != nonovl_stub)
1224
0
    ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index;
1225
1226
0
  if (h != NULL)
1227
0
    head = &h->got.glist;
1228
0
  else
1229
0
    head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
1230
1231
0
  addend = 0;
1232
0
  if (irela != NULL)
1233
0
    addend = irela->r_addend;
1234
1235
0
  if (htab->params->ovly_flavour == ovly_soft_icache)
1236
0
    {
1237
0
      g = bfd_malloc (sizeof *g);
1238
0
      if (g == NULL)
1239
0
  return false;
1240
0
      g->ovl = ovl;
1241
0
      g->br_addr = 0;
1242
0
      if (irela != NULL)
1243
0
  g->br_addr = (irela->r_offset
1244
0
          + isec->output_offset
1245
0
          + isec->output_section->vma);
1246
0
      g->next = *head;
1247
0
      *head = g;
1248
0
    }
1249
0
  else
1250
0
    {
1251
0
      for (g = *head; g != NULL; g = g->next)
1252
0
  if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
1253
0
    break;
1254
0
      if (g == NULL)
1255
0
  abort ();
1256
1257
0
      if (g->ovl == 0 && ovl != 0)
1258
0
  return true;
1259
1260
0
      if (g->stub_addr != (bfd_vma) -1)
1261
0
  return true;
1262
0
    }
1263
1264
0
  sec = htab->stub_sec[ovl];
1265
0
  dest += dest_sec->output_offset + dest_sec->output_section->vma;
1266
0
  from = sec->size + sec->output_offset + sec->output_section->vma;
1267
0
  g->stub_addr = from;
1268
0
  to = (htab->ovly_entry[0]->root.u.def.value
1269
0
  + htab->ovly_entry[0]->root.u.def.section->output_offset
1270
0
  + htab->ovly_entry[0]->root.u.def.section->output_section->vma);
1271
1272
0
  if (((dest | to | from) & 3) != 0)
1273
0
    {
1274
0
      htab->stub_err = 1;
1275
0
      return false;
1276
0
    }
1277
0
  dest_ovl = spu_elf_section_data (dest_sec->output_section)->u.o.ovl_index;
1278
1279
0
  if (htab->params->ovly_flavour == ovly_normal
1280
0
      && !htab->params->compact_stub)
1281
0
    {
1282
0
      bfd_put_32 (sec->owner, ILA + ((dest_ovl << 7) & 0x01ffff80) + 78,
1283
0
      sec->contents + sec->size);
1284
0
      bfd_put_32 (sec->owner, LNOP,
1285
0
      sec->contents + sec->size + 4);
1286
0
      bfd_put_32 (sec->owner, ILA + ((dest << 7) & 0x01ffff80) + 79,
1287
0
      sec->contents + sec->size + 8);
1288
0
      if (!BRA_STUBS)
1289
0
  bfd_put_32 (sec->owner, BR + (((to - (from + 12)) << 5) & 0x007fff80),
1290
0
        sec->contents + sec->size + 12);
1291
0
      else
1292
0
  bfd_put_32 (sec->owner, BRA + ((to << 5) & 0x007fff80),
1293
0
        sec->contents + sec->size + 12);
1294
0
    }
1295
0
  else if (htab->params->ovly_flavour == ovly_normal
1296
0
     && htab->params->compact_stub)
1297
0
    {
1298
0
      if (!BRA_STUBS)
1299
0
  bfd_put_32 (sec->owner, BRSL + (((to - from) << 5) & 0x007fff80) + 75,
1300
0
        sec->contents + sec->size);
1301
0
      else
1302
0
  bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
1303
0
        sec->contents + sec->size);
1304
0
      bfd_put_32 (sec->owner, (dest & 0x3ffff) | (dest_ovl << 18),
1305
0
      sec->contents + sec->size + 4);
1306
0
    }
1307
0
  else if (htab->params->ovly_flavour == ovly_soft_icache
1308
0
     && htab->params->compact_stub)
1309
0
    {
1310
0
      lrlive = 0;
1311
0
      if (stub_type == nonovl_stub)
1312
0
  ;
1313
0
      else if (stub_type == call_ovl_stub)
1314
  /* A brsl makes lr live and *(*sp+16) is live.
1315
     Tail calls have the same liveness.  */
1316
0
  lrlive = 5;
1317
0
      else if (!htab->params->lrlive_analysis)
1318
  /* Assume stack frame and lr save.  */
1319
0
  lrlive = 1;
1320
0
      else if (irela != NULL)
1321
0
  {
1322
    /* Analyse branch instructions.  */
1323
0
    struct function_info *caller;
1324
0
    bfd_vma off;
1325
1326
0
    caller = find_function (isec, irela->r_offset, info);
1327
0
    if (caller->start == NULL)
1328
0
      off = irela->r_offset;
1329
0
    else
1330
0
      {
1331
0
        struct function_info *found = NULL;
1332
1333
        /* Find the earliest piece of this function that
1334
     has frame adjusting instructions.  We might
1335
     see dynamic frame adjustment (eg. for alloca)
1336
     in some later piece, but functions using
1337
     alloca always set up a frame earlier.  Frame
1338
     setup instructions are always in one piece.  */
1339
0
        if (caller->lr_store != (bfd_vma) -1
1340
0
      || caller->sp_adjust != (bfd_vma) -1)
1341
0
    found = caller;
1342
0
        while (caller->start != NULL)
1343
0
    {
1344
0
      caller = caller->start;
1345
0
      if (caller->lr_store != (bfd_vma) -1
1346
0
          || caller->sp_adjust != (bfd_vma) -1)
1347
0
        found = caller;
1348
0
    }
1349
0
        if (found != NULL)
1350
0
    caller = found;
1351
0
        off = (bfd_vma) -1;
1352
0
      }
1353
1354
0
    if (off > caller->sp_adjust)
1355
0
      {
1356
0
        if (off > caller->lr_store)
1357
    /* Only *(*sp+16) is live.  */
1358
0
    lrlive = 1;
1359
0
        else
1360
    /* If no lr save, then we must be in a
1361
       leaf function with a frame.
1362
       lr is still live.  */
1363
0
    lrlive = 4;
1364
0
      }
1365
0
    else if (off > caller->lr_store)
1366
0
      {
1367
        /* Between lr save and stack adjust.  */
1368
0
        lrlive = 3;
1369
        /* This should never happen since prologues won't
1370
     be split here.  */
1371
0
        BFD_ASSERT (0);
1372
0
      }
1373
0
    else
1374
      /* On entry to function.  */
1375
0
      lrlive = 5;
1376
1377
0
    if (stub_type != br000_ovl_stub
1378
0
        && lrlive != stub_type - br000_ovl_stub)
1379
      /* xgettext:c-format */
1380
0
      info->callbacks->einfo (_("%pA:0x%v lrlive .brinfo (%u) differs "
1381
0
              "from analysis (%u)\n"),
1382
0
            isec, irela->r_offset, lrlive,
1383
0
            stub_type - br000_ovl_stub);
1384
0
  }
1385
1386
      /* If given lrlive info via .brinfo, use it.  */
1387
0
      if (stub_type > br000_ovl_stub)
1388
0
  lrlive = stub_type - br000_ovl_stub;
1389
1390
0
      if (ovl == 0)
1391
0
  to = (htab->ovly_entry[1]->root.u.def.value
1392
0
        + htab->ovly_entry[1]->root.u.def.section->output_offset
1393
0
        + htab->ovly_entry[1]->root.u.def.section->output_section->vma);
1394
1395
      /* The branch that uses this stub goes to stub_addr + 4.  We'll
1396
   set up an xor pattern that can be used by the icache manager
1397
   to modify this branch to go directly to its destination.  */
1398
0
      g->stub_addr += 4;
1399
0
      br_dest = g->stub_addr;
1400
0
      if (irela == NULL)
1401
0
  {
1402
    /* Except in the case of _SPUEAR_ stubs, the branch in
1403
       question is the one in the stub itself.  */
1404
0
    BFD_ASSERT (stub_type == nonovl_stub);
1405
0
    g->br_addr = g->stub_addr;
1406
0
    br_dest = to;
1407
0
  }
1408
1409
0
      set_id = ((dest_ovl - 1) >> htab->num_lines_log2) + 1;
1410
0
      bfd_put_32 (sec->owner, (set_id << 18) | (dest & 0x3ffff),
1411
0
      sec->contents + sec->size);
1412
0
      bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
1413
0
      sec->contents + sec->size + 4);
1414
0
      bfd_put_32 (sec->owner, (lrlive << 29) | (g->br_addr & 0x3ffff),
1415
0
      sec->contents + sec->size + 8);
1416
0
      patt = dest ^ br_dest;
1417
0
      if (irela != NULL && ELF32_R_TYPE (irela->r_info) == R_SPU_REL16)
1418
0
  patt = (dest - g->br_addr) ^ (br_dest - g->br_addr);
1419
0
      bfd_put_32 (sec->owner, (patt << 5) & 0x007fff80,
1420
0
      sec->contents + sec->size + 12);
1421
1422
0
      if (ovl == 0)
1423
  /* Extra space for linked list entries.  */
1424
0
  sec->size += 16;
1425
0
    }
1426
0
  else
1427
0
    abort ();
1428
1429
0
  sec->size += ovl_stub_size (htab->params);
1430
1431
0
  if (htab->params->emit_stub_syms)
1432
0
    {
1433
0
      size_t len;
1434
0
      char *name;
1435
0
      int add;
1436
1437
0
      len = 8 + sizeof (".ovl_call.") - 1;
1438
0
      if (h != NULL)
1439
0
  len += strlen (h->root.root.string);
1440
0
      else
1441
0
  len += 8 + 1 + 8;
1442
0
      add = 0;
1443
0
      if (irela != NULL)
1444
0
  add = (int) irela->r_addend & 0xffffffff;
1445
0
      if (add != 0)
1446
0
  len += 1 + 8;
1447
0
      name = bfd_malloc (len + 1);
1448
0
      if (name == NULL)
1449
0
  return false;
1450
1451
0
      sprintf (name, "%08x.ovl_call.", g->ovl);
1452
0
      if (h != NULL)
1453
0
  strcpy (name + 8 + sizeof (".ovl_call.") - 1, h->root.root.string);
1454
0
      else
1455
0
  sprintf (name + 8 + sizeof (".ovl_call.") - 1, "%x:%x",
1456
0
     dest_sec->id & 0xffffffff,
1457
0
     (int) ELF32_R_SYM (irela->r_info) & 0xffffffff);
1458
0
      if (add != 0)
1459
0
  sprintf (name + len - 9, "+%x", add);
1460
1461
0
      h = elf_link_hash_lookup (&htab->elf, name, true, true, false);
1462
0
      free (name);
1463
0
      if (h == NULL)
1464
0
  return false;
1465
0
      if (h->root.type == bfd_link_hash_new)
1466
0
  {
1467
0
    h->root.type = bfd_link_hash_defined;
1468
0
    h->root.u.def.section = sec;
1469
0
    h->size = ovl_stub_size (htab->params);
1470
0
    h->root.u.def.value = sec->size - h->size;
1471
0
    h->type = STT_FUNC;
1472
0
    h->ref_regular = 1;
1473
0
    h->def_regular = 1;
1474
0
    h->ref_regular_nonweak = 1;
1475
0
    h->forced_local = 1;
1476
0
    h->non_elf = 0;
1477
0
  }
1478
0
    }
1479
1480
0
  return true;
1481
0
}
1482
1483
/* Called via elf_link_hash_traverse to allocate stubs for any _SPUEAR_
1484
   symbols.  */
1485
1486
static bool
1487
allocate_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
1488
0
{
1489
  /* Symbols starting with _SPUEAR_ need a stub because they may be
1490
     invoked by the PPU.  */
1491
0
  struct bfd_link_info *info = inf;
1492
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
1493
0
  asection *sym_sec;
1494
1495
0
  if ((h->root.type == bfd_link_hash_defined
1496
0
       || h->root.type == bfd_link_hash_defweak)
1497
0
      && h->def_regular
1498
0
      && startswith (h->root.root.string, "_SPUEAR_")
1499
0
      && (sym_sec = h->root.u.def.section) != NULL
1500
0
      && sym_sec->output_section != bfd_abs_section_ptr
1501
0
      && spu_elf_section_data (sym_sec->output_section) != NULL
1502
0
      && (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index != 0
1503
0
    || htab->params->non_overlay_stubs))
1504
0
    {
1505
0
      return count_stub (htab, NULL, NULL, nonovl_stub, h, NULL);
1506
0
    }
1507
1508
0
  return true;
1509
0
}
1510
1511
static bool
1512
build_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
1513
0
{
1514
  /* Symbols starting with _SPUEAR_ need a stub because they may be
1515
     invoked by the PPU.  */
1516
0
  struct bfd_link_info *info = inf;
1517
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
1518
0
  asection *sym_sec;
1519
1520
0
  if ((h->root.type == bfd_link_hash_defined
1521
0
       || h->root.type == bfd_link_hash_defweak)
1522
0
      && h->def_regular
1523
0
      && startswith (h->root.root.string, "_SPUEAR_")
1524
0
      && (sym_sec = h->root.u.def.section) != NULL
1525
0
      && sym_sec->output_section != bfd_abs_section_ptr
1526
0
      && spu_elf_section_data (sym_sec->output_section) != NULL
1527
0
      && (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index != 0
1528
0
    || htab->params->non_overlay_stubs))
1529
0
    {
1530
0
      return build_stub (info, NULL, NULL, nonovl_stub, h, NULL,
1531
0
       h->root.u.def.value, sym_sec);
1532
0
    }
1533
1534
0
  return true;
1535
0
}
1536
1537
/* Size or build stubs.  */
1538
1539
static bool
1540
process_stubs (struct bfd_link_info *info, bool build)
1541
0
{
1542
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
1543
0
  bfd *ibfd;
1544
1545
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1546
0
    {
1547
0
      extern const bfd_target spu_elf32_vec;
1548
0
      Elf_Internal_Shdr *symtab_hdr;
1549
0
      asection *isec;
1550
0
      Elf_Internal_Sym *local_syms = NULL;
1551
1552
0
      if (ibfd->xvec != &spu_elf32_vec)
1553
0
  continue;
1554
1555
      /* We'll need the symbol table in a second.  */
1556
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
1557
0
      if (symtab_hdr->sh_info == 0)
1558
0
  continue;
1559
1560
      /* Walk over each section attached to the input bfd.  */
1561
0
      for (isec = ibfd->sections; isec != NULL; isec = isec->next)
1562
0
  {
1563
0
    Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1564
1565
    /* If there aren't any relocs, then there's nothing more to do.  */
1566
0
    if ((isec->flags & SEC_RELOC) == 0
1567
0
        || isec->reloc_count == 0)
1568
0
      continue;
1569
1570
0
    if (!maybe_needs_stubs (isec))
1571
0
      continue;
1572
1573
    /* Get the relocs.  */
1574
0
    internal_relocs = _bfd_elf_link_read_relocs (ibfd, isec, NULL, NULL,
1575
0
                   info->keep_memory);
1576
0
    if (internal_relocs == NULL)
1577
0
      goto error_ret_free_local;
1578
1579
    /* Now examine each relocation.  */
1580
0
    irela = internal_relocs;
1581
0
    irelaend = irela + isec->reloc_count;
1582
0
    for (; irela < irelaend; irela++)
1583
0
      {
1584
0
        enum elf_spu_reloc_type r_type;
1585
0
        unsigned int r_indx;
1586
0
        asection *sym_sec;
1587
0
        Elf_Internal_Sym *sym;
1588
0
        struct elf_link_hash_entry *h;
1589
0
        enum _stub_type stub_type;
1590
1591
0
        r_type = ELF32_R_TYPE (irela->r_info);
1592
0
        r_indx = ELF32_R_SYM (irela->r_info);
1593
1594
0
        if (r_type >= R_SPU_max)
1595
0
    {
1596
0
      bfd_set_error (bfd_error_bad_value);
1597
0
    error_ret_free_internal:
1598
0
      if (elf_section_data (isec)->relocs != internal_relocs)
1599
0
        free (internal_relocs);
1600
0
    error_ret_free_local:
1601
0
      if (symtab_hdr->contents != (unsigned char *) local_syms)
1602
0
        free (local_syms);
1603
0
      return false;
1604
0
    }
1605
1606
        /* Determine the reloc target section.  */
1607
0
        if (!get_sym_h (&h, &sym, &sym_sec, &local_syms, r_indx, ibfd))
1608
0
    goto error_ret_free_internal;
1609
1610
0
        stub_type = needs_ovl_stub (h, sym, sym_sec, isec, irela,
1611
0
            NULL, info);
1612
0
        if (stub_type == no_stub)
1613
0
    continue;
1614
0
        else if (stub_type == stub_error)
1615
0
    goto error_ret_free_internal;
1616
1617
0
        if (htab->stub_count == NULL)
1618
0
    {
1619
0
      bfd_size_type amt;
1620
0
      amt = (htab->num_overlays + 1) * sizeof (*htab->stub_count);
1621
0
      htab->stub_count = bfd_zmalloc (amt);
1622
0
      if (htab->stub_count == NULL)
1623
0
        goto error_ret_free_internal;
1624
0
    }
1625
1626
0
        if (!build)
1627
0
    {
1628
0
      if (!count_stub (htab, ibfd, isec, stub_type, h, irela))
1629
0
        goto error_ret_free_internal;
1630
0
    }
1631
0
        else
1632
0
    {
1633
0
      bfd_vma dest;
1634
1635
0
      if (h != NULL)
1636
0
        dest = h->root.u.def.value;
1637
0
      else
1638
0
        dest = sym->st_value;
1639
0
      dest += irela->r_addend;
1640
0
      if (!build_stub (info, ibfd, isec, stub_type, h, irela,
1641
0
           dest, sym_sec))
1642
0
        goto error_ret_free_internal;
1643
0
    }
1644
0
      }
1645
1646
    /* We're done with the internal relocs, free them.  */
1647
0
    if (elf_section_data (isec)->relocs != internal_relocs)
1648
0
      free (internal_relocs);
1649
0
  }
1650
1651
0
      if (local_syms != NULL
1652
0
    && symtab_hdr->contents != (unsigned char *) local_syms)
1653
0
  {
1654
0
    if (!info->keep_memory)
1655
0
      free (local_syms);
1656
0
    else
1657
0
      symtab_hdr->contents = (unsigned char *) local_syms;
1658
0
  }
1659
0
    }
1660
1661
0
  return true;
1662
0
}
1663
1664
/* Allocate space for overlay call and return stubs.
1665
   Return 0 on error, 1 if no overlays, 2 otherwise.  */
1666
1667
int
1668
spu_elf_size_stubs (struct bfd_link_info *info)
1669
0
{
1670
0
  struct spu_link_hash_table *htab;
1671
0
  bfd *ibfd;
1672
0
  bfd_size_type amt;
1673
0
  flagword flags;
1674
0
  unsigned int i;
1675
0
  asection *stub;
1676
1677
0
  if (!process_stubs (info, false))
1678
0
    return 0;
1679
1680
0
  htab = spu_hash_table (info);
1681
0
  elf_link_hash_traverse (&htab->elf, allocate_spuear_stubs, info);
1682
0
  if (htab->stub_err)
1683
0
    return 0;
1684
1685
0
  ibfd = info->input_bfds;
1686
0
  if (htab->stub_count != NULL)
1687
0
    {
1688
0
      amt = (htab->num_overlays + 1) * sizeof (*htab->stub_sec);
1689
0
      htab->stub_sec = bfd_zmalloc (amt);
1690
0
      if (htab->stub_sec == NULL)
1691
0
  return 0;
1692
1693
0
      flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
1694
0
         | SEC_HAS_CONTENTS | SEC_IN_MEMORY);
1695
0
      stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
1696
0
      htab->stub_sec[0] = stub;
1697
0
      if (stub == NULL
1698
0
    || !bfd_set_section_alignment (stub,
1699
0
           ovl_stub_size_log2 (htab->params)))
1700
0
  return 0;
1701
0
      stub->size = htab->stub_count[0] * ovl_stub_size (htab->params);
1702
0
      if (htab->params->ovly_flavour == ovly_soft_icache)
1703
  /* Extra space for linked list entries.  */
1704
0
  stub->size += htab->stub_count[0] * 16;
1705
1706
0
      for (i = 0; i < htab->num_overlays; ++i)
1707
0
  {
1708
0
    asection *osec = htab->ovl_sec[i];
1709
0
    unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
1710
0
    stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
1711
0
    htab->stub_sec[ovl] = stub;
1712
0
    if (stub == NULL
1713
0
        || !bfd_set_section_alignment (stub,
1714
0
               ovl_stub_size_log2 (htab->params)))
1715
0
      return 0;
1716
0
    stub->size = htab->stub_count[ovl] * ovl_stub_size (htab->params);
1717
0
  }
1718
0
    }
1719
1720
0
  if (htab->params->ovly_flavour == ovly_soft_icache)
1721
0
    {
1722
      /* Space for icache manager tables.
1723
   a) Tag array, one quadword per cache line.
1724
   b) Rewrite "to" list, one quadword per cache line.
1725
   c) Rewrite "from" list, one byte per outgoing branch (rounded up to
1726
      a power-of-two number of full quadwords) per cache line.  */
1727
1728
0
      flags = SEC_ALLOC;
1729
0
      htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags);
1730
0
      if (htab->ovtab == NULL
1731
0
    || !bfd_set_section_alignment (htab->ovtab, 4))
1732
0
  return 0;
1733
1734
0
      htab->ovtab->size = (16 + 16 + (16 << htab->fromelem_size_log2))
1735
0
        << htab->num_lines_log2;
1736
1737
0
      flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1738
0
      htab->init = bfd_make_section_anyway_with_flags (ibfd, ".ovini", flags);
1739
0
      if (htab->init == NULL
1740
0
    || !bfd_set_section_alignment (htab->init, 4))
1741
0
  return 0;
1742
1743
0
      htab->init->size = 16;
1744
0
    }
1745
0
  else if (htab->stub_count == NULL)
1746
0
    return 1;
1747
0
  else
1748
0
    {
1749
      /* htab->ovtab consists of two arrays.
1750
   .  struct {
1751
   .    u32 vma;
1752
   .    u32 size;
1753
   .    u32 file_off;
1754
   .    u32 buf;
1755
   .  } _ovly_table[];
1756
   .
1757
   .  struct {
1758
   .    u32 mapped;
1759
   .  } _ovly_buf_table[];
1760
   .  */
1761
1762
0
      flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1763
0
      htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags);
1764
0
      if (htab->ovtab == NULL
1765
0
    || !bfd_set_section_alignment (htab->ovtab, 4))
1766
0
  return 0;
1767
1768
0
      htab->ovtab->size = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
1769
0
    }
1770
1771
0
  htab->toe = bfd_make_section_anyway_with_flags (ibfd, ".toe", SEC_ALLOC);
1772
0
  if (htab->toe == NULL
1773
0
      || !bfd_set_section_alignment (htab->toe, 4))
1774
0
    return 0;
1775
0
  htab->toe->size = 16;
1776
1777
0
  return 2;
1778
0
}
1779
1780
/* Called from ld to place overlay manager data sections.  This is done
1781
   after the overlay manager itself is loaded, mainly so that the
1782
   linker's htab->init section is placed after any other .ovl.init
1783
   sections.  */
1784
1785
void
1786
spu_elf_place_overlay_data (struct bfd_link_info *info)
1787
0
{
1788
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
1789
0
  unsigned int i;
1790
1791
0
  if (htab->stub_sec != NULL)
1792
0
    {
1793
0
      (*htab->params->place_spu_section) (htab->stub_sec[0], NULL, ".text");
1794
1795
0
      for (i = 0; i < htab->num_overlays; ++i)
1796
0
  {
1797
0
    asection *osec = htab->ovl_sec[i];
1798
0
    unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
1799
0
    (*htab->params->place_spu_section) (htab->stub_sec[ovl], osec, NULL);
1800
0
  }
1801
0
    }
1802
1803
0
  if (htab->params->ovly_flavour == ovly_soft_icache)
1804
0
    (*htab->params->place_spu_section) (htab->init, NULL, ".ovl.init");
1805
1806
0
  if (htab->ovtab != NULL)
1807
0
    {
1808
0
      const char *ovout = ".data";
1809
0
      if (htab->params->ovly_flavour == ovly_soft_icache)
1810
0
  ovout = ".bss";
1811
0
      (*htab->params->place_spu_section) (htab->ovtab, NULL, ovout);
1812
0
    }
1813
1814
0
  if (htab->toe != NULL)
1815
0
    (*htab->params->place_spu_section) (htab->toe, NULL, ".toe");
1816
0
}
1817
1818
/* Functions to handle embedded spu_ovl.o object.  */
1819
1820
static void *
1821
ovl_mgr_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *stream)
1822
0
{
1823
0
  return stream;
1824
0
}
1825
1826
static file_ptr
1827
ovl_mgr_pread (struct bfd *abfd ATTRIBUTE_UNUSED,
1828
         void *stream,
1829
         void *buf,
1830
         file_ptr nbytes,
1831
         file_ptr offset)
1832
0
{
1833
0
  struct _ovl_stream *os;
1834
0
  size_t count;
1835
0
  size_t max;
1836
1837
0
  os = (struct _ovl_stream *) stream;
1838
0
  max = (const char *) os->end - (const char *) os->start;
1839
1840
0
  if ((ufile_ptr) offset >= max)
1841
0
    return 0;
1842
1843
0
  count = nbytes;
1844
0
  if (count > max - offset)
1845
0
    count = max - offset;
1846
1847
0
  memcpy (buf, (const char *) os->start + offset, count);
1848
0
  return count;
1849
0
}
1850
1851
static int
1852
ovl_mgr_stat (struct bfd *abfd ATTRIBUTE_UNUSED,
1853
        void *stream,
1854
        struct stat *sb)
1855
0
{
1856
0
  struct _ovl_stream *os = (struct _ovl_stream *) stream;
1857
1858
0
  memset (sb, 0, sizeof (*sb));
1859
0
  sb->st_size = (const char *) os->end - (const char *) os->start;
1860
0
  return 0;
1861
0
}
1862
1863
bool
1864
spu_elf_open_builtin_lib (bfd **ovl_bfd, const struct _ovl_stream *stream)
1865
0
{
1866
0
  *ovl_bfd = bfd_openr_iovec ("builtin ovl_mgr",
1867
0
            "elf32-spu",
1868
0
            ovl_mgr_open,
1869
0
            (void *) stream,
1870
0
            ovl_mgr_pread,
1871
0
            NULL,
1872
0
            ovl_mgr_stat);
1873
0
  return *ovl_bfd != NULL;
1874
0
}
1875
1876
static unsigned int
1877
overlay_index (asection *sec)
1878
0
{
1879
0
  if (sec == NULL
1880
0
      || sec->output_section == bfd_abs_section_ptr)
1881
0
    return 0;
1882
0
  return spu_elf_section_data (sec->output_section)->u.o.ovl_index;
1883
0
}
1884
1885
/* Define an STT_OBJECT symbol.  */
1886
1887
static struct elf_link_hash_entry *
1888
define_ovtab_symbol (struct spu_link_hash_table *htab, const char *name)
1889
0
{
1890
0
  struct elf_link_hash_entry *h;
1891
1892
0
  h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
1893
0
  if (h == NULL)
1894
0
    return NULL;
1895
1896
0
  if (h->root.type != bfd_link_hash_defined
1897
0
      || !h->def_regular)
1898
0
    {
1899
0
      h->root.type = bfd_link_hash_defined;
1900
0
      h->root.u.def.section = htab->ovtab;
1901
0
      h->type = STT_OBJECT;
1902
0
      h->ref_regular = 1;
1903
0
      h->def_regular = 1;
1904
0
      h->ref_regular_nonweak = 1;
1905
0
      h->non_elf = 0;
1906
0
    }
1907
0
  else if (h->root.u.def.section->owner != NULL)
1908
0
    {
1909
      /* xgettext:c-format */
1910
0
      _bfd_error_handler (_("%pB is not allowed to define %s"),
1911
0
        h->root.u.def.section->owner,
1912
0
        h->root.root.string);
1913
0
      bfd_set_error (bfd_error_bad_value);
1914
0
      return NULL;
1915
0
    }
1916
0
  else
1917
0
    {
1918
0
      _bfd_error_handler (_("you are not allowed to define %s in a script"),
1919
0
        h->root.root.string);
1920
0
      bfd_set_error (bfd_error_bad_value);
1921
0
      return NULL;
1922
0
    }
1923
1924
0
  return h;
1925
0
}
1926
1927
/* Fill in all stubs and the overlay tables.  */
1928
1929
static bool
1930
spu_elf_build_stubs (struct bfd_link_info *info)
1931
0
{
1932
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
1933
0
  struct elf_link_hash_entry *h;
1934
0
  bfd_byte *p;
1935
0
  asection *s;
1936
0
  bfd *obfd;
1937
0
  unsigned int i;
1938
1939
0
  if (htab->num_overlays != 0)
1940
0
    {
1941
0
      for (i = 0; i < 2; i++)
1942
0
  {
1943
0
    h = htab->ovly_entry[i];
1944
0
    if (h != NULL
1945
0
        && (h->root.type == bfd_link_hash_defined
1946
0
      || h->root.type == bfd_link_hash_defweak)
1947
0
        && h->def_regular)
1948
0
      {
1949
0
        s = h->root.u.def.section->output_section;
1950
0
        if (spu_elf_section_data (s)->u.o.ovl_index)
1951
0
    {
1952
0
      _bfd_error_handler (_("%s in overlay section"),
1953
0
              h->root.root.string);
1954
0
      bfd_set_error (bfd_error_bad_value);
1955
0
      return false;
1956
0
    }
1957
0
      }
1958
0
  }
1959
0
    }
1960
1961
0
  if (htab->stub_sec != NULL)
1962
0
    {
1963
0
      for (i = 0; i <= htab->num_overlays; i++)
1964
0
  if (htab->stub_sec[i]->size != 0)
1965
0
    {
1966
0
      htab->stub_sec[i]->contents = bfd_zalloc (htab->stub_sec[i]->owner,
1967
0
                  htab->stub_sec[i]->size);
1968
0
      if (htab->stub_sec[i]->contents == NULL)
1969
0
        return false;
1970
0
      htab->stub_sec[i]->alloced = 1;
1971
0
      htab->stub_sec[i]->rawsize = htab->stub_sec[i]->size;
1972
0
      htab->stub_sec[i]->size = 0;
1973
0
    }
1974
1975
      /* Fill in all the stubs.  */
1976
0
      process_stubs (info, true);
1977
0
      if (!htab->stub_err)
1978
0
  elf_link_hash_traverse (&htab->elf, build_spuear_stubs, info);
1979
1980
0
      if (htab->stub_err)
1981
0
  {
1982
0
    _bfd_error_handler (_("overlay stub relocation overflow"));
1983
0
    bfd_set_error (bfd_error_bad_value);
1984
0
    return false;
1985
0
  }
1986
1987
0
      for (i = 0; i <= htab->num_overlays; i++)
1988
0
  {
1989
0
    if (htab->stub_sec[i]->size != htab->stub_sec[i]->rawsize)
1990
0
      {
1991
0
        _bfd_error_handler  (_("stubs don't match calculated size"));
1992
0
        bfd_set_error (bfd_error_bad_value);
1993
0
        return false;
1994
0
      }
1995
0
    htab->stub_sec[i]->rawsize = 0;
1996
0
  }
1997
0
    }
1998
1999
0
  if (htab->ovtab == NULL || htab->ovtab->size == 0)
2000
0
    return true;
2001
2002
0
  htab->ovtab->contents = bfd_zalloc (htab->ovtab->owner, htab->ovtab->size);
2003
0
  if (htab->ovtab->contents == NULL)
2004
0
    return false;
2005
0
  htab->ovtab->alloced = 1;
2006
2007
0
  p = htab->ovtab->contents;
2008
0
  if (htab->params->ovly_flavour == ovly_soft_icache)
2009
0
    {
2010
0
      bfd_vma off;
2011
2012
0
      h = define_ovtab_symbol (htab, "__icache_tag_array");
2013
0
      if (h == NULL)
2014
0
  return false;
2015
0
      h->root.u.def.value = 0;
2016
0
      h->size = 16 << htab->num_lines_log2;
2017
0
      off = h->size;
2018
2019
0
      h = define_ovtab_symbol (htab, "__icache_tag_array_size");
2020
0
      if (h == NULL)
2021
0
  return false;
2022
0
      h->root.u.def.value = 16 << htab->num_lines_log2;
2023
0
      h->root.u.def.section = bfd_abs_section_ptr;
2024
2025
0
      h = define_ovtab_symbol (htab, "__icache_rewrite_to");
2026
0
      if (h == NULL)
2027
0
  return false;
2028
0
      h->root.u.def.value = off;
2029
0
      h->size = 16 << htab->num_lines_log2;
2030
0
      off += h->size;
2031
2032
0
      h = define_ovtab_symbol (htab, "__icache_rewrite_to_size");
2033
0
      if (h == NULL)
2034
0
  return false;
2035
0
      h->root.u.def.value = 16 << htab->num_lines_log2;
2036
0
      h->root.u.def.section = bfd_abs_section_ptr;
2037
2038
0
      h = define_ovtab_symbol (htab, "__icache_rewrite_from");
2039
0
      if (h == NULL)
2040
0
  return false;
2041
0
      h->root.u.def.value = off;
2042
0
      h->size = 16 << (htab->fromelem_size_log2 + htab->num_lines_log2);
2043
0
      off += h->size;
2044
2045
0
      h = define_ovtab_symbol (htab, "__icache_rewrite_from_size");
2046
0
      if (h == NULL)
2047
0
  return false;
2048
0
      h->root.u.def.value = 16 << (htab->fromelem_size_log2
2049
0
           + htab->num_lines_log2);
2050
0
      h->root.u.def.section = bfd_abs_section_ptr;
2051
2052
0
      h = define_ovtab_symbol (htab, "__icache_log2_fromelemsize");
2053
0
      if (h == NULL)
2054
0
  return false;
2055
0
      h->root.u.def.value = htab->fromelem_size_log2;
2056
0
      h->root.u.def.section = bfd_abs_section_ptr;
2057
2058
0
      h = define_ovtab_symbol (htab, "__icache_base");
2059
0
      if (h == NULL)
2060
0
  return false;
2061
0
      h->root.u.def.value = htab->ovl_sec[0]->vma;
2062
0
      h->root.u.def.section = bfd_abs_section_ptr;
2063
0
      h->size = htab->num_buf << htab->line_size_log2;
2064
2065
0
      h = define_ovtab_symbol (htab, "__icache_linesize");
2066
0
      if (h == NULL)
2067
0
  return false;
2068
0
      h->root.u.def.value = 1 << htab->line_size_log2;
2069
0
      h->root.u.def.section = bfd_abs_section_ptr;
2070
2071
0
      h = define_ovtab_symbol (htab, "__icache_log2_linesize");
2072
0
      if (h == NULL)
2073
0
  return false;
2074
0
      h->root.u.def.value = htab->line_size_log2;
2075
0
      h->root.u.def.section = bfd_abs_section_ptr;
2076
2077
0
      h = define_ovtab_symbol (htab, "__icache_neg_log2_linesize");
2078
0
      if (h == NULL)
2079
0
  return false;
2080
0
      h->root.u.def.value = -htab->line_size_log2;
2081
0
      h->root.u.def.section = bfd_abs_section_ptr;
2082
2083
0
      h = define_ovtab_symbol (htab, "__icache_cachesize");
2084
0
      if (h == NULL)
2085
0
  return false;
2086
0
      h->root.u.def.value = 1 << (htab->num_lines_log2 + htab->line_size_log2);
2087
0
      h->root.u.def.section = bfd_abs_section_ptr;
2088
2089
0
      h = define_ovtab_symbol (htab, "__icache_log2_cachesize");
2090
0
      if (h == NULL)
2091
0
  return false;
2092
0
      h->root.u.def.value = htab->num_lines_log2 + htab->line_size_log2;
2093
0
      h->root.u.def.section = bfd_abs_section_ptr;
2094
2095
0
      h = define_ovtab_symbol (htab, "__icache_neg_log2_cachesize");
2096
0
      if (h == NULL)
2097
0
  return false;
2098
0
      h->root.u.def.value = -(htab->num_lines_log2 + htab->line_size_log2);
2099
0
      h->root.u.def.section = bfd_abs_section_ptr;
2100
2101
0
      if (htab->init != NULL && htab->init->size != 0)
2102
0
  {
2103
0
    htab->init->contents = bfd_zalloc (htab->init->owner,
2104
0
               htab->init->size);
2105
0
    if (htab->init->contents == NULL)
2106
0
      return false;
2107
0
    htab->init->alloced = 1;
2108
2109
0
    h = define_ovtab_symbol (htab, "__icache_fileoff");
2110
0
    if (h == NULL)
2111
0
      return false;
2112
0
    h->root.u.def.value = 0;
2113
0
    h->root.u.def.section = htab->init;
2114
0
    h->size = 8;
2115
0
  }
2116
0
    }
2117
0
  else
2118
0
    {
2119
      /* Write out _ovly_table.  */
2120
      /* set low bit of .size to mark non-overlay area as present.  */
2121
0
      p[7] = 1;
2122
0
      obfd = htab->ovtab->output_section->owner;
2123
0
      for (s = obfd->sections; s != NULL; s = s->next)
2124
0
  {
2125
0
    unsigned int ovl_index = spu_elf_section_data (s)->u.o.ovl_index;
2126
2127
0
    if (ovl_index != 0)
2128
0
      {
2129
0
        unsigned long off = ovl_index * 16;
2130
0
        unsigned int ovl_buf = spu_elf_section_data (s)->u.o.ovl_buf;
2131
2132
0
        bfd_put_32 (htab->ovtab->owner, s->vma, p + off);
2133
0
        bfd_put_32 (htab->ovtab->owner, (s->size + 15) & -16,
2134
0
        p + off + 4);
2135
        /* file_off written later in spu_elf_modify_headers.  */
2136
0
        bfd_put_32 (htab->ovtab->owner, ovl_buf, p + off + 12);
2137
0
      }
2138
0
  }
2139
2140
0
      h = define_ovtab_symbol (htab, "_ovly_table");
2141
0
      if (h == NULL)
2142
0
  return false;
2143
0
      h->root.u.def.value = 16;
2144
0
      h->size = htab->num_overlays * 16;
2145
2146
0
      h = define_ovtab_symbol (htab, "_ovly_table_end");
2147
0
      if (h == NULL)
2148
0
  return false;
2149
0
      h->root.u.def.value = htab->num_overlays * 16 + 16;
2150
0
      h->size = 0;
2151
2152
0
      h = define_ovtab_symbol (htab, "_ovly_buf_table");
2153
0
      if (h == NULL)
2154
0
  return false;
2155
0
      h->root.u.def.value = htab->num_overlays * 16 + 16;
2156
0
      h->size = htab->num_buf * 4;
2157
2158
0
      h = define_ovtab_symbol (htab, "_ovly_buf_table_end");
2159
0
      if (h == NULL)
2160
0
  return false;
2161
0
      h->root.u.def.value = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
2162
0
      h->size = 0;
2163
0
    }
2164
2165
0
  h = define_ovtab_symbol (htab, "_EAR_");
2166
0
  if (h == NULL)
2167
0
    return false;
2168
0
  h->root.u.def.section = htab->toe;
2169
0
  h->root.u.def.value = 0;
2170
0
  h->size = 16;
2171
2172
0
  return true;
2173
0
}
2174
2175
/* Check that all loadable section VMAs lie in the range
2176
   LO .. HI inclusive, and stash some parameters for --auto-overlay.  */
2177
2178
asection *
2179
spu_elf_check_vma (struct bfd_link_info *info)
2180
0
{
2181
0
  struct elf_segment_map *m;
2182
0
  unsigned int i;
2183
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
2184
0
  bfd *abfd = info->output_bfd;
2185
0
  bfd_vma hi = htab->params->local_store_hi;
2186
0
  bfd_vma lo = htab->params->local_store_lo;
2187
2188
0
  htab->local_store = hi + 1 - lo;
2189
2190
0
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
2191
0
    if (m->p_type == PT_LOAD)
2192
0
      for (i = 0; i < m->count; i++)
2193
0
  if (m->sections[i]->size != 0
2194
0
      && (m->sections[i]->vma < lo
2195
0
    || m->sections[i]->vma > hi
2196
0
    || m->sections[i]->vma + m->sections[i]->size - 1 > hi))
2197
0
    return m->sections[i];
2198
2199
0
  return NULL;
2200
0
}
2201
2202
/* OFFSET in SEC (presumably) is the beginning of a function prologue.
2203
   Search for stack adjusting insns, and return the sp delta.
2204
   If a store of lr is found save the instruction offset to *LR_STORE.
2205
   If a stack adjusting instruction is found, save that offset to
2206
   *SP_ADJUST.  */
2207
2208
static int
2209
find_function_stack_adjust (asection *sec,
2210
          bfd_vma offset,
2211
          bfd_vma *lr_store,
2212
          bfd_vma *sp_adjust)
2213
0
{
2214
0
  int32_t reg[128];
2215
2216
0
  memset (reg, 0, sizeof (reg));
2217
0
  for ( ; offset + 4 <= sec->size; offset += 4)
2218
0
    {
2219
0
      unsigned char buf[4];
2220
0
      int rt, ra;
2221
0
      uint32_t imm;
2222
2223
      /* Assume no relocs on stack adjusing insns.  */
2224
0
      if (!bfd_get_section_contents (sec->owner, sec, buf, offset, 4))
2225
0
  break;
2226
2227
0
      rt = buf[3] & 0x7f;
2228
0
      ra = ((buf[2] & 0x3f) << 1) | (buf[3] >> 7);
2229
2230
0
      if (buf[0] == 0x24 /* stqd */)
2231
0
  {
2232
0
    if (rt == 0 /* lr */ && ra == 1 /* sp */)
2233
0
      *lr_store = offset;
2234
0
    continue;
2235
0
  }
2236
2237
      /* Partly decoded immediate field.  */
2238
0
      imm = (buf[1] << 9) | (buf[2] << 1) | (buf[3] >> 7);
2239
2240
0
      if (buf[0] == 0x1c /* ai */)
2241
0
  {
2242
0
    imm >>= 7;
2243
0
    imm = (imm ^ 0x200) - 0x200;
2244
0
    reg[rt] = reg[ra] + imm;
2245
2246
0
    if (rt == 1 /* sp */)
2247
0
      {
2248
0
        if (reg[rt] > 0)
2249
0
    break;
2250
0
        *sp_adjust = offset;
2251
0
        return reg[rt];
2252
0
      }
2253
0
  }
2254
0
      else if (buf[0] == 0x18 && (buf[1] & 0xe0) == 0 /* a */)
2255
0
  {
2256
0
    int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6);
2257
2258
0
    reg[rt] = reg[ra] + reg[rb];
2259
0
    if (rt == 1)
2260
0
      {
2261
0
        if (reg[rt] > 0)
2262
0
    break;
2263
0
        *sp_adjust = offset;
2264
0
        return reg[rt];
2265
0
      }
2266
0
  }
2267
0
      else if (buf[0] == 0x08 && (buf[1] & 0xe0) == 0 /* sf */)
2268
0
  {
2269
0
    int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6);
2270
2271
0
    reg[rt] = reg[rb] - reg[ra];
2272
0
    if (rt == 1)
2273
0
      {
2274
0
        if (reg[rt] > 0)
2275
0
    break;
2276
0
        *sp_adjust = offset;
2277
0
        return reg[rt];
2278
0
      }
2279
0
  }
2280
0
      else if ((buf[0] & 0xfc) == 0x40 /* il, ilh, ilhu, ila */)
2281
0
  {
2282
0
    if (buf[0] >= 0x42 /* ila */)
2283
0
      imm |= (buf[0] & 1) << 17;
2284
0
    else
2285
0
      {
2286
0
        imm &= 0xffff;
2287
2288
0
        if (buf[0] == 0x40 /* il */)
2289
0
    {
2290
0
      if ((buf[1] & 0x80) == 0)
2291
0
        continue;
2292
0
      imm = (imm ^ 0x8000) - 0x8000;
2293
0
    }
2294
0
        else if ((buf[1] & 0x80) == 0 /* ilhu */)
2295
0
    imm <<= 16;
2296
0
      }
2297
0
    reg[rt] = imm;
2298
0
    continue;
2299
0
  }
2300
0
      else if (buf[0] == 0x60 && (buf[1] & 0x80) != 0 /* iohl */)
2301
0
  {
2302
0
    reg[rt] |= imm & 0xffff;
2303
0
    continue;
2304
0
  }
2305
0
      else if (buf[0] == 0x04 /* ori */)
2306
0
  {
2307
0
    imm >>= 7;
2308
0
    imm = (imm ^ 0x200) - 0x200;
2309
0
    reg[rt] = reg[ra] | imm;
2310
0
    continue;
2311
0
  }
2312
0
      else if (buf[0] == 0x32 && (buf[1] & 0x80) != 0 /* fsmbi */)
2313
0
  {
2314
0
    reg[rt] = (  ((imm & 0x8000) ? 0xff000000 : 0)
2315
0
         | ((imm & 0x4000) ? 0x00ff0000 : 0)
2316
0
         | ((imm & 0x2000) ? 0x0000ff00 : 0)
2317
0
         | ((imm & 0x1000) ? 0x000000ff : 0));
2318
0
    continue;
2319
0
  }
2320
0
      else if (buf[0] == 0x16 /* andbi */)
2321
0
  {
2322
0
    imm >>= 7;
2323
0
    imm &= 0xff;
2324
0
    imm |= imm << 8;
2325
0
    imm |= imm << 16;
2326
0
    reg[rt] = reg[ra] & imm;
2327
0
    continue;
2328
0
  }
2329
0
      else if (buf[0] == 0x33 && imm == 1 /* brsl .+4 */)
2330
0
  {
2331
    /* Used in pic reg load.  Say rt is trashed.  Won't be used
2332
       in stack adjust, but we need to continue past this branch.  */
2333
0
    reg[rt] = 0;
2334
0
    continue;
2335
0
  }
2336
0
      else if (is_branch (buf) || is_indirect_branch (buf))
2337
  /* If we hit a branch then we must be out of the prologue.  */
2338
0
  break;
2339
0
    }
2340
2341
0
  return 0;
2342
0
}
2343
2344
/* qsort predicate to sort symbols by section and value.  */
2345
2346
static Elf_Internal_Sym *sort_syms_syms;
2347
static asection **sort_syms_psecs;
2348
2349
static int
2350
sort_syms (const void *a, const void *b)
2351
0
{
2352
0
  Elf_Internal_Sym *const *s1 = a;
2353
0
  Elf_Internal_Sym *const *s2 = b;
2354
0
  asection *sec1,*sec2;
2355
0
  bfd_signed_vma delta;
2356
2357
0
  sec1 = sort_syms_psecs[*s1 - sort_syms_syms];
2358
0
  sec2 = sort_syms_psecs[*s2 - sort_syms_syms];
2359
2360
0
  if (sec1 != sec2)
2361
0
    return sec1->index - sec2->index;
2362
2363
0
  delta = (*s1)->st_value - (*s2)->st_value;
2364
0
  if (delta != 0)
2365
0
    return delta < 0 ? -1 : 1;
2366
2367
0
  delta = (*s2)->st_size - (*s1)->st_size;
2368
0
  if (delta != 0)
2369
0
    return delta < 0 ? -1 : 1;
2370
2371
0
  return *s1 < *s2 ? -1 : 1;
2372
0
}
2373
2374
/* Allocate a struct spu_elf_stack_info with MAX_FUN struct function_info
2375
   entries for section SEC.  */
2376
2377
static struct spu_elf_stack_info *
2378
alloc_stack_info (asection *sec, int max_fun)
2379
0
{
2380
0
  struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2381
0
  bfd_size_type amt;
2382
2383
0
  amt = sizeof (struct spu_elf_stack_info);
2384
0
  amt += (max_fun - 1) * sizeof (struct function_info);
2385
0
  sec_data->u.i.stack_info = bfd_zmalloc (amt);
2386
0
  if (sec_data->u.i.stack_info != NULL)
2387
0
    sec_data->u.i.stack_info->max_fun = max_fun;
2388
0
  return sec_data->u.i.stack_info;
2389
0
}
2390
2391
/* Add a new struct function_info describing a (part of a) function
2392
   starting at SYM_H.  Keep the array sorted by address.  */
2393
2394
static struct function_info *
2395
maybe_insert_function (asection *sec,
2396
           void *sym_h,
2397
           bool global,
2398
           bool is_func)
2399
0
{
2400
0
  struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2401
0
  struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2402
0
  int i;
2403
0
  bfd_vma off, size;
2404
2405
0
  if (sinfo == NULL)
2406
0
    {
2407
0
      sinfo = alloc_stack_info (sec, 20);
2408
0
      if (sinfo == NULL)
2409
0
  return NULL;
2410
0
    }
2411
2412
0
  if (!global)
2413
0
    {
2414
0
      Elf_Internal_Sym *sym = sym_h;
2415
0
      off = sym->st_value;
2416
0
      size = sym->st_size;
2417
0
    }
2418
0
  else
2419
0
    {
2420
0
      struct elf_link_hash_entry *h = sym_h;
2421
0
      off = h->root.u.def.value;
2422
0
      size = h->size;
2423
0
    }
2424
2425
0
  for (i = sinfo->num_fun; --i >= 0; )
2426
0
    if (sinfo->fun[i].lo <= off)
2427
0
      break;
2428
2429
0
  if (i >= 0)
2430
0
    {
2431
      /* Don't add another entry for an alias, but do update some
2432
   info.  */
2433
0
      if (sinfo->fun[i].lo == off)
2434
0
  {
2435
    /* Prefer globals over local syms.  */
2436
0
    if (global && !sinfo->fun[i].global)
2437
0
      {
2438
0
        sinfo->fun[i].global = true;
2439
0
        sinfo->fun[i].u.h = sym_h;
2440
0
      }
2441
0
    if (is_func)
2442
0
      sinfo->fun[i].is_func = true;
2443
0
    return &sinfo->fun[i];
2444
0
  }
2445
      /* Ignore a zero-size symbol inside an existing function.  */
2446
0
      else if (sinfo->fun[i].hi > off && size == 0)
2447
0
  return &sinfo->fun[i];
2448
0
    }
2449
2450
0
  if (sinfo->num_fun >= sinfo->max_fun)
2451
0
    {
2452
0
      bfd_size_type amt = sizeof (struct spu_elf_stack_info);
2453
0
      bfd_size_type old = amt;
2454
2455
0
      old += (sinfo->max_fun - 1) * sizeof (struct function_info);
2456
0
      sinfo->max_fun += 20 + (sinfo->max_fun >> 1);
2457
0
      amt += (sinfo->max_fun - 1) * sizeof (struct function_info);
2458
0
      sinfo = bfd_realloc (sinfo, amt);
2459
0
      if (sinfo == NULL)
2460
0
  return NULL;
2461
0
      memset ((char *) sinfo + old, 0, amt - old);
2462
0
      sec_data->u.i.stack_info = sinfo;
2463
0
    }
2464
2465
0
  if (++i < sinfo->num_fun)
2466
0
    memmove (&sinfo->fun[i + 1], &sinfo->fun[i],
2467
0
       (sinfo->num_fun - i) * sizeof (sinfo->fun[i]));
2468
0
  sinfo->fun[i].is_func = is_func;
2469
0
  sinfo->fun[i].global = global;
2470
0
  sinfo->fun[i].sec = sec;
2471
0
  if (global)
2472
0
    sinfo->fun[i].u.h = sym_h;
2473
0
  else
2474
0
    sinfo->fun[i].u.sym = sym_h;
2475
0
  sinfo->fun[i].lo = off;
2476
0
  sinfo->fun[i].hi = off + size;
2477
0
  sinfo->fun[i].lr_store = -1;
2478
0
  sinfo->fun[i].sp_adjust = -1;
2479
0
  sinfo->fun[i].stack = -find_function_stack_adjust (sec, off,
2480
0
                 &sinfo->fun[i].lr_store,
2481
0
                 &sinfo->fun[i].sp_adjust);
2482
0
  sinfo->num_fun += 1;
2483
0
  return &sinfo->fun[i];
2484
0
}
2485
2486
/* Return the name of FUN.  */
2487
2488
static const char *
2489
func_name (struct function_info *fun)
2490
0
{
2491
0
  asection *sec;
2492
0
  bfd *ibfd;
2493
0
  Elf_Internal_Shdr *symtab_hdr;
2494
2495
0
  while (fun->start != NULL)
2496
0
    fun = fun->start;
2497
2498
0
  if (fun->global)
2499
0
    return fun->u.h->root.root.string;
2500
2501
0
  sec = fun->sec;
2502
0
  if (fun->u.sym->st_name == 0)
2503
0
    {
2504
0
      size_t len = strlen (sec->name);
2505
0
      char *name = bfd_malloc (len + 10);
2506
0
      if (name == NULL)
2507
0
  return "(null)";
2508
0
      sprintf (name, "%s+%lx", sec->name,
2509
0
         (unsigned long) fun->u.sym->st_value & 0xffffffff);
2510
0
      return name;
2511
0
    }
2512
0
  ibfd = sec->owner;
2513
0
  symtab_hdr = &elf_symtab_hdr (ibfd);
2514
0
  return bfd_elf_sym_name (ibfd, symtab_hdr, fun->u.sym, sec);
2515
0
}
2516
2517
/* Read the instruction at OFF in SEC.  Return true iff the instruction
2518
   is a nop, lnop, or stop 0 (all zero insn).  */
2519
2520
static bool
2521
is_nop (asection *sec, bfd_vma off)
2522
0
{
2523
0
  unsigned char insn[4];
2524
2525
0
  if (off + 4 > sec->size
2526
0
      || !bfd_get_section_contents (sec->owner, sec, insn, off, 4))
2527
0
    return false;
2528
0
  if ((insn[0] & 0xbf) == 0 && (insn[1] & 0xe0) == 0x20)
2529
0
    return true;
2530
0
  if (insn[0] == 0 && insn[1] == 0 && insn[2] == 0 && insn[3] == 0)
2531
0
    return true;
2532
0
  return false;
2533
0
}
2534
2535
/* Extend the range of FUN to cover nop padding up to LIMIT.
2536
   Return TRUE iff some instruction other than a NOP was found.  */
2537
2538
static bool
2539
insns_at_end (struct function_info *fun, bfd_vma limit)
2540
0
{
2541
0
  bfd_vma off = (fun->hi + 3) & -4;
2542
2543
0
  while (off < limit && is_nop (fun->sec, off))
2544
0
    off += 4;
2545
0
  if (off < limit)
2546
0
    {
2547
0
      fun->hi = off;
2548
0
      return true;
2549
0
    }
2550
0
  fun->hi = limit;
2551
0
  return false;
2552
0
}
2553
2554
/* Check and fix overlapping function ranges.  Return TRUE iff there
2555
   are gaps in the current info we have about functions in SEC.  */
2556
2557
static bool
2558
check_function_ranges (asection *sec, struct bfd_link_info *info)
2559
0
{
2560
0
  struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2561
0
  struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2562
0
  int i;
2563
0
  bool gaps = false;
2564
2565
0
  if (sinfo == NULL)
2566
0
    return false;
2567
2568
0
  for (i = 1; i < sinfo->num_fun; i++)
2569
0
    if (sinfo->fun[i - 1].hi > sinfo->fun[i].lo)
2570
0
      {
2571
  /* Fix overlapping symbols.  */
2572
0
  const char *f1 = func_name (&sinfo->fun[i - 1]);
2573
0
  const char *f2 = func_name (&sinfo->fun[i]);
2574
2575
  /* xgettext:c-format */
2576
0
  info->callbacks->einfo (_("warning: %s overlaps %s\n"), f1, f2);
2577
0
  sinfo->fun[i - 1].hi = sinfo->fun[i].lo;
2578
0
      }
2579
0
    else if (insns_at_end (&sinfo->fun[i - 1], sinfo->fun[i].lo))
2580
0
      gaps = true;
2581
2582
0
  if (sinfo->num_fun == 0)
2583
0
    gaps = true;
2584
0
  else
2585
0
    {
2586
0
      if (sinfo->fun[0].lo != 0)
2587
0
  gaps = true;
2588
0
      if (sinfo->fun[sinfo->num_fun - 1].hi > sec->size)
2589
0
  {
2590
0
    const char *f1 = func_name (&sinfo->fun[sinfo->num_fun - 1]);
2591
2592
0
    info->callbacks->einfo (_("warning: %s exceeds section size\n"), f1);
2593
0
    sinfo->fun[sinfo->num_fun - 1].hi = sec->size;
2594
0
  }
2595
0
      else if (insns_at_end (&sinfo->fun[sinfo->num_fun - 1], sec->size))
2596
0
  gaps = true;
2597
0
    }
2598
0
  return gaps;
2599
0
}
2600
2601
/* Search current function info for a function that contains address
2602
   OFFSET in section SEC.  */
2603
2604
static struct function_info *
2605
find_function (asection *sec, bfd_vma offset, struct bfd_link_info *info)
2606
0
{
2607
0
  struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2608
0
  struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2609
0
  int lo, hi, mid;
2610
2611
0
  lo = 0;
2612
0
  hi = sinfo->num_fun;
2613
0
  while (lo < hi)
2614
0
    {
2615
0
      mid = (lo + hi) / 2;
2616
0
      if (offset < sinfo->fun[mid].lo)
2617
0
  hi = mid;
2618
0
      else if (offset >= sinfo->fun[mid].hi)
2619
0
  lo = mid + 1;
2620
0
      else
2621
0
  return &sinfo->fun[mid];
2622
0
    }
2623
  /* xgettext:c-format */
2624
0
  info->callbacks->einfo (_("%pA:0x%v not found in function table\n"),
2625
0
        sec, offset);
2626
0
  bfd_set_error (bfd_error_bad_value);
2627
0
  return NULL;
2628
0
}
2629
2630
/* Add CALLEE to CALLER call list if not already present.  Return TRUE
2631
   if CALLEE was new.  If this function return FALSE, CALLEE should
2632
   be freed.  */
2633
2634
static bool
2635
insert_callee (struct function_info *caller, struct call_info *callee)
2636
0
{
2637
0
  struct call_info **pp, *p;
2638
2639
0
  for (pp = &caller->call_list; (p = *pp) != NULL; pp = &p->next)
2640
0
    if (p->fun == callee->fun)
2641
0
      {
2642
  /* Tail calls use less stack than normal calls.  Retain entry
2643
     for normal call over one for tail call.  */
2644
0
  p->is_tail &= callee->is_tail;
2645
0
  if (!p->is_tail)
2646
0
    {
2647
0
      p->fun->start = NULL;
2648
0
      p->fun->is_func = true;
2649
0
    }
2650
0
  p->count += callee->count;
2651
  /* Reorder list so most recent call is first.  */
2652
0
  *pp = p->next;
2653
0
  p->next = caller->call_list;
2654
0
  caller->call_list = p;
2655
0
  return false;
2656
0
      }
2657
0
  callee->next = caller->call_list;
2658
0
  caller->call_list = callee;
2659
0
  return true;
2660
0
}
2661
2662
/* Copy CALL and insert the copy into CALLER.  */
2663
2664
static bool
2665
copy_callee (struct function_info *caller, const struct call_info *call)
2666
0
{
2667
0
  struct call_info *callee;
2668
0
  callee = bfd_malloc (sizeof (*callee));
2669
0
  if (callee == NULL)
2670
0
    return false;
2671
0
  *callee = *call;
2672
0
  if (!insert_callee (caller, callee))
2673
0
    free (callee);
2674
0
  return true;
2675
0
}
2676
2677
/* We're only interested in code sections.  Testing SEC_IN_MEMORY excludes
2678
   overlay stub sections.  */
2679
2680
static bool
2681
interesting_section (asection *s)
2682
0
{
2683
0
  return (s->output_section != bfd_abs_section_ptr
2684
0
    && ((s->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_IN_MEMORY))
2685
0
        == (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2686
0
    && s->size != 0);
2687
0
}
2688
2689
/* Rummage through the relocs for SEC, looking for function calls.
2690
   If CALL_TREE is true, fill in call graph.  If CALL_TREE is false,
2691
   mark destination symbols on calls as being functions.  Also
2692
   look at branches, which may be tail calls or go to hot/cold
2693
   section part of same function.  */
2694
2695
static bool
2696
mark_functions_via_relocs (asection *sec,
2697
         struct bfd_link_info *info,
2698
         int call_tree)
2699
0
{
2700
0
  Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2701
0
  Elf_Internal_Shdr *symtab_hdr;
2702
0
  void *psyms;
2703
0
  unsigned int priority = 0;
2704
0
  static bool warned;
2705
2706
0
  if (!interesting_section (sec)
2707
0
      || sec->reloc_count == 0)
2708
0
    return true;
2709
2710
0
  internal_relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL,
2711
0
                 info->keep_memory);
2712
0
  if (internal_relocs == NULL)
2713
0
    return false;
2714
2715
0
  symtab_hdr = &elf_symtab_hdr (sec->owner);
2716
0
  psyms = &symtab_hdr->contents;
2717
0
  irela = internal_relocs;
2718
0
  irelaend = irela + sec->reloc_count;
2719
0
  for (; irela < irelaend; irela++)
2720
0
    {
2721
0
      enum elf_spu_reloc_type r_type;
2722
0
      unsigned int r_indx;
2723
0
      asection *sym_sec;
2724
0
      Elf_Internal_Sym *sym;
2725
0
      struct elf_link_hash_entry *h;
2726
0
      bfd_vma val;
2727
0
      bool nonbranch, is_call;
2728
0
      struct function_info *caller;
2729
0
      struct call_info *callee;
2730
2731
0
      r_type = ELF32_R_TYPE (irela->r_info);
2732
0
      nonbranch = r_type != R_SPU_REL16 && r_type != R_SPU_ADDR16;
2733
2734
0
      r_indx = ELF32_R_SYM (irela->r_info);
2735
0
      if (!get_sym_h (&h, &sym, &sym_sec, psyms, r_indx, sec->owner))
2736
0
  return false;
2737
2738
0
      if (sym_sec == NULL
2739
0
    || sym_sec->output_section == bfd_abs_section_ptr)
2740
0
  continue;
2741
2742
0
      is_call = false;
2743
0
      if (!nonbranch)
2744
0
  {
2745
0
    unsigned char insn[4];
2746
2747
0
    if (!bfd_get_section_contents (sec->owner, sec, insn,
2748
0
           irela->r_offset, 4))
2749
0
      return false;
2750
0
    if (is_branch (insn))
2751
0
      {
2752
0
        is_call = (insn[0] & 0xfd) == 0x31;
2753
0
        priority = insn[1] & 0x0f;
2754
0
        priority <<= 8;
2755
0
        priority |= insn[2];
2756
0
        priority <<= 8;
2757
0
        priority |= insn[3];
2758
0
        priority >>= 7;
2759
0
        if ((sym_sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2760
0
      != (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2761
0
    {
2762
0
      if (!warned)
2763
0
        info->callbacks->einfo
2764
          /* xgettext:c-format */
2765
0
          (_("%pB(%pA+0x%v): call to non-code section"
2766
0
       " %pB(%pA), analysis incomplete\n"),
2767
0
           sec->owner, sec, irela->r_offset,
2768
0
           sym_sec->owner, sym_sec);
2769
0
      warned = true;
2770
0
      continue;
2771
0
    }
2772
0
      }
2773
0
    else
2774
0
      {
2775
0
        nonbranch = true;
2776
0
        if (is_hint (insn))
2777
0
    continue;
2778
0
      }
2779
0
  }
2780
2781
0
      if (nonbranch)
2782
0
  {
2783
    /* For --auto-overlay, count possible stubs we need for
2784
       function pointer references.  */
2785
0
    unsigned int sym_type;
2786
0
    if (h)
2787
0
      sym_type = h->type;
2788
0
    else
2789
0
      sym_type = ELF_ST_TYPE (sym->st_info);
2790
0
    if (sym_type == STT_FUNC)
2791
0
      {
2792
0
        if (call_tree && spu_hash_table (info)->params->auto_overlay)
2793
0
    spu_hash_table (info)->non_ovly_stub += 1;
2794
        /* If the symbol type is STT_FUNC then this must be a
2795
     function pointer initialisation.  */
2796
0
        continue;
2797
0
      }
2798
    /* Ignore data references.  */
2799
0
    if ((sym_sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2800
0
        != (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2801
0
      continue;
2802
    /* Otherwise we probably have a jump table reloc for
2803
       a switch statement or some other reference to a
2804
       code label.  */
2805
0
  }
2806
2807
0
      if (h)
2808
0
  val = h->root.u.def.value;
2809
0
      else
2810
0
  val = sym->st_value;
2811
0
      val += irela->r_addend;
2812
2813
0
      if (!call_tree)
2814
0
  {
2815
0
    struct function_info *fun;
2816
2817
0
    if (irela->r_addend != 0)
2818
0
      {
2819
0
        Elf_Internal_Sym *fake = bfd_zmalloc (sizeof (*fake));
2820
0
        if (fake == NULL)
2821
0
    return false;
2822
0
        fake->st_value = val;
2823
0
        fake->st_shndx
2824
0
    = _bfd_elf_section_from_bfd_section (sym_sec->owner, sym_sec);
2825
0
        sym = fake;
2826
0
      }
2827
0
    if (sym)
2828
0
      fun = maybe_insert_function (sym_sec, sym, false, is_call);
2829
0
    else
2830
0
      fun = maybe_insert_function (sym_sec, h, true, is_call);
2831
0
    if (fun == NULL)
2832
0
      return false;
2833
0
    if (irela->r_addend != 0
2834
0
        && fun->u.sym != sym)
2835
0
      free (sym);
2836
0
    continue;
2837
0
  }
2838
2839
0
      caller = find_function (sec, irela->r_offset, info);
2840
0
      if (caller == NULL)
2841
0
  return false;
2842
0
      callee = bfd_malloc (sizeof *callee);
2843
0
      if (callee == NULL)
2844
0
  return false;
2845
2846
0
      callee->fun = find_function (sym_sec, val, info);
2847
0
      if (callee->fun == NULL)
2848
0
  return false;
2849
0
      callee->is_tail = !is_call;
2850
0
      callee->is_pasted = false;
2851
0
      callee->broken_cycle = false;
2852
0
      callee->priority = priority;
2853
0
      callee->count = nonbranch? 0 : 1;
2854
0
      if (callee->fun->last_caller != sec)
2855
0
  {
2856
0
    callee->fun->last_caller = sec;
2857
0
    callee->fun->call_count += 1;
2858
0
  }
2859
0
      if (!insert_callee (caller, callee))
2860
0
  free (callee);
2861
0
      else if (!is_call
2862
0
         && !callee->fun->is_func
2863
0
         && callee->fun->stack == 0)
2864
0
  {
2865
    /* This is either a tail call or a branch from one part of
2866
       the function to another, ie. hot/cold section.  If the
2867
       destination has been called by some other function then
2868
       it is a separate function.  We also assume that functions
2869
       are not split across input files.  */
2870
0
    if (sec->owner != sym_sec->owner)
2871
0
      {
2872
0
        callee->fun->start = NULL;
2873
0
        callee->fun->is_func = true;
2874
0
      }
2875
0
    else if (callee->fun->start == NULL)
2876
0
      {
2877
0
        struct function_info *caller_start = caller;
2878
0
        while (caller_start->start)
2879
0
    caller_start = caller_start->start;
2880
2881
0
        if (caller_start != callee->fun)
2882
0
    callee->fun->start = caller_start;
2883
0
      }
2884
0
    else
2885
0
      {
2886
0
        struct function_info *callee_start;
2887
0
        struct function_info *caller_start;
2888
0
        callee_start = callee->fun;
2889
0
        while (callee_start->start)
2890
0
    callee_start = callee_start->start;
2891
0
        caller_start = caller;
2892
0
        while (caller_start->start)
2893
0
    caller_start = caller_start->start;
2894
0
        if (caller_start != callee_start)
2895
0
    {
2896
0
      callee->fun->start = NULL;
2897
0
      callee->fun->is_func = true;
2898
0
    }
2899
0
      }
2900
0
  }
2901
0
    }
2902
2903
0
  return true;
2904
0
}
2905
2906
/* Handle something like .init or .fini, which has a piece of a function.
2907
   These sections are pasted together to form a single function.  */
2908
2909
static bool
2910
pasted_function (asection *sec)
2911
0
{
2912
0
  struct bfd_link_order *l;
2913
0
  struct _spu_elf_section_data *sec_data;
2914
0
  struct spu_elf_stack_info *sinfo;
2915
0
  Elf_Internal_Sym *fake;
2916
0
  struct function_info *fun, *fun_start;
2917
2918
0
  fake = bfd_zmalloc (sizeof (*fake));
2919
0
  if (fake == NULL)
2920
0
    return false;
2921
0
  fake->st_value = 0;
2922
0
  fake->st_size = sec->size;
2923
0
  fake->st_shndx
2924
0
    = _bfd_elf_section_from_bfd_section (sec->owner, sec);
2925
0
  fun = maybe_insert_function (sec, fake, false, false);
2926
0
  if (!fun)
2927
0
    return false;
2928
2929
  /* Find a function immediately preceding this section.  */
2930
0
  fun_start = NULL;
2931
0
  for (l = sec->output_section->map_head.link_order; l != NULL; l = l->next)
2932
0
    {
2933
0
      if (l->u.indirect.section == sec)
2934
0
  {
2935
0
    if (fun_start != NULL)
2936
0
      {
2937
0
        struct call_info *callee = bfd_malloc (sizeof *callee);
2938
0
        if (callee == NULL)
2939
0
    return false;
2940
2941
0
        fun->start = fun_start;
2942
0
        callee->fun = fun;
2943
0
        callee->is_tail = true;
2944
0
        callee->is_pasted = true;
2945
0
        callee->broken_cycle = false;
2946
0
        callee->priority = 0;
2947
0
        callee->count = 1;
2948
0
        if (!insert_callee (fun_start, callee))
2949
0
    free (callee);
2950
0
        return true;
2951
0
      }
2952
0
    break;
2953
0
  }
2954
0
      if (l->type == bfd_indirect_link_order
2955
0
    && (sec_data = spu_elf_section_data (l->u.indirect.section)) != NULL
2956
0
    && (sinfo = sec_data->u.i.stack_info) != NULL
2957
0
    && sinfo->num_fun != 0)
2958
0
  fun_start = &sinfo->fun[sinfo->num_fun - 1];
2959
0
    }
2960
2961
  /* Don't return an error if we did not find a function preceding this
2962
     section.  The section may have incorrect flags.  */
2963
0
  return true;
2964
0
}
2965
2966
/* Map address ranges in code sections to functions.  */
2967
2968
static bool
2969
discover_functions (struct bfd_link_info *info)
2970
0
{
2971
0
  bfd *ibfd;
2972
0
  int bfd_idx;
2973
0
  Elf_Internal_Sym ***psym_arr;
2974
0
  asection ***sec_arr;
2975
0
  bool gaps = false;
2976
2977
0
  bfd_idx = 0;
2978
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2979
0
    bfd_idx++;
2980
2981
0
  psym_arr = bfd_zmalloc (bfd_idx * sizeof (*psym_arr));
2982
0
  if (psym_arr == NULL)
2983
0
    return false;
2984
0
  sec_arr = bfd_zmalloc (bfd_idx * sizeof (*sec_arr));
2985
0
  if (sec_arr == NULL)
2986
0
    return false;
2987
2988
0
  for (ibfd = info->input_bfds, bfd_idx = 0;
2989
0
       ibfd != NULL;
2990
0
       ibfd = ibfd->link.next, bfd_idx++)
2991
0
    {
2992
0
      extern const bfd_target spu_elf32_vec;
2993
0
      Elf_Internal_Shdr *symtab_hdr;
2994
0
      asection *sec;
2995
0
      size_t symcount;
2996
0
      Elf_Internal_Sym *syms, *sy, **psyms, **psy;
2997
0
      asection **psecs, **p;
2998
2999
0
      if (ibfd->xvec != &spu_elf32_vec)
3000
0
  continue;
3001
3002
      /* Read all the symbols.  */
3003
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
3004
0
      symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
3005
0
      if (symcount == 0)
3006
0
  {
3007
0
    if (!gaps)
3008
0
      for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
3009
0
        if (interesting_section (sec))
3010
0
    {
3011
0
      gaps = true;
3012
0
      break;
3013
0
    }
3014
0
    continue;
3015
0
  }
3016
3017
      /* Don't use cached symbols since the generic ELF linker
3018
   code only reads local symbols, and we need globals too.  */
3019
0
      free (symtab_hdr->contents);
3020
0
      symtab_hdr->contents = NULL;
3021
0
      syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, 0,
3022
0
           NULL, NULL, NULL);
3023
0
      symtab_hdr->contents = (void *) syms;
3024
0
      if (syms == NULL)
3025
0
  return false;
3026
3027
      /* Select defined function symbols that are going to be output.  */
3028
0
      psyms = bfd_malloc ((symcount + 1) * sizeof (*psyms));
3029
0
      if (psyms == NULL)
3030
0
  return false;
3031
0
      psym_arr[bfd_idx] = psyms;
3032
0
      psecs = bfd_malloc (symcount * sizeof (*psecs));
3033
0
      if (psecs == NULL)
3034
0
  return false;
3035
0
      sec_arr[bfd_idx] = psecs;
3036
0
      for (psy = psyms, p = psecs, sy = syms; sy < syms + symcount; ++p, ++sy)
3037
0
  if (ELF_ST_TYPE (sy->st_info) == STT_NOTYPE
3038
0
      || ELF_ST_TYPE (sy->st_info) == STT_FUNC)
3039
0
    {
3040
0
      asection *s;
3041
3042
0
      *p = s = bfd_section_from_elf_index (ibfd, sy->st_shndx);
3043
0
      if (s != NULL && interesting_section (s))
3044
0
        *psy++ = sy;
3045
0
    }
3046
0
      symcount = psy - psyms;
3047
0
      *psy = NULL;
3048
3049
      /* Sort them by section and offset within section.  */
3050
0
      sort_syms_syms = syms;
3051
0
      sort_syms_psecs = psecs;
3052
0
      qsort (psyms, symcount, sizeof (*psyms), sort_syms);
3053
3054
      /* Now inspect the function symbols.  */
3055
0
      for (psy = psyms; psy < psyms + symcount; )
3056
0
  {
3057
0
    asection *s = psecs[*psy - syms];
3058
0
    Elf_Internal_Sym **psy2;
3059
3060
0
    for (psy2 = psy; ++psy2 < psyms + symcount; )
3061
0
      if (psecs[*psy2 - syms] != s)
3062
0
        break;
3063
3064
0
    if (!alloc_stack_info (s, psy2 - psy))
3065
0
      return false;
3066
0
    psy = psy2;
3067
0
  }
3068
3069
      /* First install info about properly typed and sized functions.
3070
   In an ideal world this will cover all code sections, except
3071
   when partitioning functions into hot and cold sections,
3072
   and the horrible pasted together .init and .fini functions.  */
3073
0
      for (psy = psyms; psy < psyms + symcount; ++psy)
3074
0
  {
3075
0
    sy = *psy;
3076
0
    if (ELF_ST_TYPE (sy->st_info) == STT_FUNC)
3077
0
      {
3078
0
        asection *s = psecs[sy - syms];
3079
0
        if (!maybe_insert_function (s, sy, false, true))
3080
0
    return false;
3081
0
      }
3082
0
  }
3083
3084
0
      for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
3085
0
  if (interesting_section (sec))
3086
0
    gaps |= check_function_ranges (sec, info);
3087
0
    }
3088
3089
0
  if (gaps)
3090
0
    {
3091
      /* See if we can discover more function symbols by looking at
3092
   relocations.  */
3093
0
      for (ibfd = info->input_bfds, bfd_idx = 0;
3094
0
     ibfd != NULL;
3095
0
     ibfd = ibfd->link.next, bfd_idx++)
3096
0
  {
3097
0
    asection *sec;
3098
3099
0
    if (psym_arr[bfd_idx] == NULL)
3100
0
      continue;
3101
3102
0
    for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3103
0
      if (!mark_functions_via_relocs (sec, info, false))
3104
0
        return false;
3105
0
  }
3106
3107
0
      for (ibfd = info->input_bfds, bfd_idx = 0;
3108
0
     ibfd != NULL;
3109
0
     ibfd = ibfd->link.next, bfd_idx++)
3110
0
  {
3111
0
    Elf_Internal_Shdr *symtab_hdr;
3112
0
    asection *sec;
3113
0
    Elf_Internal_Sym *syms, *sy, **psyms, **psy;
3114
0
    asection **psecs;
3115
3116
0
    if ((psyms = psym_arr[bfd_idx]) == NULL)
3117
0
      continue;
3118
3119
0
    psecs = sec_arr[bfd_idx];
3120
3121
0
    symtab_hdr = &elf_symtab_hdr (ibfd);
3122
0
    syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3123
3124
0
    gaps = false;
3125
0
    for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
3126
0
      if (interesting_section (sec))
3127
0
        gaps |= check_function_ranges (sec, info);
3128
0
    if (!gaps)
3129
0
      continue;
3130
3131
    /* Finally, install all globals.  */
3132
0
    for (psy = psyms; (sy = *psy) != NULL; ++psy)
3133
0
      {
3134
0
        asection *s;
3135
3136
0
        s = psecs[sy - syms];
3137
3138
        /* Global syms might be improperly typed functions.  */
3139
0
        if (ELF_ST_TYPE (sy->st_info) != STT_FUNC
3140
0
      && ELF_ST_BIND (sy->st_info) == STB_GLOBAL)
3141
0
    {
3142
0
      if (!maybe_insert_function (s, sy, false, false))
3143
0
        return false;
3144
0
    }
3145
0
      }
3146
0
  }
3147
3148
0
      for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3149
0
  {
3150
0
    extern const bfd_target spu_elf32_vec;
3151
0
    asection *sec;
3152
3153
0
    if (ibfd->xvec != &spu_elf32_vec)
3154
0
      continue;
3155
3156
    /* Some of the symbols we've installed as marking the
3157
       beginning of functions may have a size of zero.  Extend
3158
       the range of such functions to the beginning of the
3159
       next symbol of interest.  */
3160
0
    for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3161
0
      if (interesting_section (sec))
3162
0
        {
3163
0
    struct _spu_elf_section_data *sec_data;
3164
0
    struct spu_elf_stack_info *sinfo;
3165
3166
0
    sec_data = spu_elf_section_data (sec);
3167
0
    sinfo = sec_data->u.i.stack_info;
3168
0
    if (sinfo != NULL && sinfo->num_fun != 0)
3169
0
      {
3170
0
        int fun_idx;
3171
0
        bfd_vma hi = sec->size;
3172
3173
0
        for (fun_idx = sinfo->num_fun; --fun_idx >= 0; )
3174
0
          {
3175
0
      sinfo->fun[fun_idx].hi = hi;
3176
0
      hi = sinfo->fun[fun_idx].lo;
3177
0
          }
3178
3179
0
        sinfo->fun[0].lo = 0;
3180
0
      }
3181
    /* No symbols in this section.  Must be .init or .fini
3182
       or something similar.  */
3183
0
    else if (!pasted_function (sec))
3184
0
      return false;
3185
0
        }
3186
0
  }
3187
0
    }
3188
3189
0
  for (ibfd = info->input_bfds, bfd_idx = 0;
3190
0
       ibfd != NULL;
3191
0
       ibfd = ibfd->link.next, bfd_idx++)
3192
0
    {
3193
0
      if (psym_arr[bfd_idx] == NULL)
3194
0
  continue;
3195
3196
0
      free (psym_arr[bfd_idx]);
3197
0
      free (sec_arr[bfd_idx]);
3198
0
    }
3199
3200
0
  free (psym_arr);
3201
0
  free (sec_arr);
3202
3203
0
  return true;
3204
0
}
3205
3206
/* Iterate over all function_info we have collected, calling DOIT on
3207
   each node if ROOT_ONLY is false.  Only call DOIT on root nodes
3208
   if ROOT_ONLY.  */
3209
3210
static bool
3211
for_each_node (bool (*doit) (struct function_info *,
3212
           struct bfd_link_info *,
3213
           void *),
3214
         struct bfd_link_info *info,
3215
         void *param,
3216
         int root_only)
3217
0
{
3218
0
  bfd *ibfd;
3219
3220
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3221
0
    {
3222
0
      extern const bfd_target spu_elf32_vec;
3223
0
      asection *sec;
3224
3225
0
      if (ibfd->xvec != &spu_elf32_vec)
3226
0
  continue;
3227
3228
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3229
0
  {
3230
0
    struct _spu_elf_section_data *sec_data;
3231
0
    struct spu_elf_stack_info *sinfo;
3232
3233
0
    if ((sec_data = spu_elf_section_data (sec)) != NULL
3234
0
        && (sinfo = sec_data->u.i.stack_info) != NULL)
3235
0
      {
3236
0
        int i;
3237
0
        for (i = 0; i < sinfo->num_fun; ++i)
3238
0
    if (!root_only || !sinfo->fun[i].non_root)
3239
0
      if (!doit (&sinfo->fun[i], info, param))
3240
0
        return false;
3241
0
      }
3242
0
  }
3243
0
    }
3244
0
  return true;
3245
0
}
3246
3247
/* Transfer call info attached to struct function_info entries for
3248
   all of a given function's sections to the first entry.  */
3249
3250
static bool
3251
transfer_calls (struct function_info *fun,
3252
    struct bfd_link_info *info ATTRIBUTE_UNUSED,
3253
    void *param ATTRIBUTE_UNUSED)
3254
0
{
3255
0
  struct function_info *start = fun->start;
3256
3257
0
  if (start != NULL)
3258
0
    {
3259
0
      struct call_info *call, *call_next;
3260
3261
0
      while (start->start != NULL)
3262
0
  start = start->start;
3263
0
      for (call = fun->call_list; call != NULL; call = call_next)
3264
0
  {
3265
0
    call_next = call->next;
3266
0
    if (!insert_callee (start, call))
3267
0
      free (call);
3268
0
  }
3269
0
      fun->call_list = NULL;
3270
0
    }
3271
0
  return true;
3272
0
}
3273
3274
/* Mark nodes in the call graph that are called by some other node.  */
3275
3276
static bool
3277
mark_non_root (struct function_info *fun,
3278
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
3279
         void *param ATTRIBUTE_UNUSED)
3280
0
{
3281
0
  struct call_info *call;
3282
3283
0
  if (fun->visit1)
3284
0
    return true;
3285
0
  fun->visit1 = true;
3286
0
  for (call = fun->call_list; call; call = call->next)
3287
0
    {
3288
0
      call->fun->non_root = true;
3289
0
      mark_non_root (call->fun, 0, 0);
3290
0
    }
3291
0
  return true;
3292
0
}
3293
3294
/* Remove cycles from the call graph.  Set depth of nodes.  */
3295
3296
static bool
3297
remove_cycles (struct function_info *fun,
3298
         struct bfd_link_info *info,
3299
         void *param)
3300
0
{
3301
0
  struct call_info **callp, *call;
3302
0
  unsigned int depth = *(unsigned int *) param;
3303
0
  unsigned int max_depth = depth;
3304
3305
0
  fun->depth = depth;
3306
0
  fun->visit2 = true;
3307
0
  fun->marking = true;
3308
3309
0
  callp = &fun->call_list;
3310
0
  while ((call = *callp) != NULL)
3311
0
    {
3312
0
      call->max_depth = depth + !call->is_pasted;
3313
0
      if (!call->fun->visit2)
3314
0
  {
3315
0
    if (!remove_cycles (call->fun, info, &call->max_depth))
3316
0
      return false;
3317
0
    if (max_depth < call->max_depth)
3318
0
      max_depth = call->max_depth;
3319
0
  }
3320
0
      else if (call->fun->marking)
3321
0
  {
3322
0
    struct spu_link_hash_table *htab = spu_hash_table (info);
3323
3324
0
    if (!htab->params->auto_overlay
3325
0
        && htab->params->stack_analysis)
3326
0
      {
3327
0
        const char *f1 = func_name (fun);
3328
0
        const char *f2 = func_name (call->fun);
3329
3330
        /* xgettext:c-format */
3331
0
        info->callbacks->info (_("stack analysis will ignore the call "
3332
0
               "from %s to %s\n"),
3333
0
             f1, f2);
3334
0
      }
3335
3336
0
    call->broken_cycle = true;
3337
0
  }
3338
0
      callp = &call->next;
3339
0
    }
3340
0
  fun->marking = false;
3341
0
  *(unsigned int *) param = max_depth;
3342
0
  return true;
3343
0
}
3344
3345
/* Check that we actually visited all nodes in remove_cycles.  If we
3346
   didn't, then there is some cycle in the call graph not attached to
3347
   any root node.  Arbitrarily choose a node in the cycle as a new
3348
   root and break the cycle.  */
3349
3350
static bool
3351
mark_detached_root (struct function_info *fun,
3352
        struct bfd_link_info *info,
3353
        void *param)
3354
0
{
3355
0
  if (fun->visit2)
3356
0
    return true;
3357
0
  fun->non_root = false;
3358
0
  *(unsigned int *) param = 0;
3359
0
  return remove_cycles (fun, info, param);
3360
0
}
3361
3362
/* Populate call_list for each function.  */
3363
3364
static bool
3365
build_call_tree (struct bfd_link_info *info)
3366
0
{
3367
0
  bfd *ibfd;
3368
0
  unsigned int depth;
3369
3370
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3371
0
    {
3372
0
      extern const bfd_target spu_elf32_vec;
3373
0
      asection *sec;
3374
3375
0
      if (ibfd->xvec != &spu_elf32_vec)
3376
0
  continue;
3377
3378
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3379
0
  if (!mark_functions_via_relocs (sec, info, true))
3380
0
    return false;
3381
0
    }
3382
3383
  /* Transfer call info from hot/cold section part of function
3384
     to main entry.  */
3385
0
  if (!spu_hash_table (info)->params->auto_overlay
3386
0
      && !for_each_node (transfer_calls, info, 0, false))
3387
0
    return false;
3388
3389
  /* Find the call graph root(s).  */
3390
0
  if (!for_each_node (mark_non_root, info, 0, false))
3391
0
    return false;
3392
3393
  /* Remove cycles from the call graph.  We start from the root node(s)
3394
     so that we break cycles in a reasonable place.  */
3395
0
  depth = 0;
3396
0
  if (!for_each_node (remove_cycles, info, &depth, true))
3397
0
    return false;
3398
3399
0
  return for_each_node (mark_detached_root, info, &depth, false);
3400
0
}
3401
3402
/* qsort predicate to sort calls by priority, max_depth then count.  */
3403
3404
static int
3405
sort_calls (const void *a, const void *b)
3406
0
{
3407
0
  struct call_info *const *c1 = a;
3408
0
  struct call_info *const *c2 = b;
3409
0
  int delta;
3410
3411
0
  delta = (*c2)->priority - (*c1)->priority;
3412
0
  if (delta != 0)
3413
0
    return delta;
3414
3415
0
  delta = (*c2)->max_depth - (*c1)->max_depth;
3416
0
  if (delta != 0)
3417
0
    return delta;
3418
3419
0
  delta = (*c2)->count - (*c1)->count;
3420
0
  if (delta != 0)
3421
0
    return delta;
3422
3423
0
  return (char *) c1 - (char *) c2;
3424
0
}
3425
3426
struct _mos_param {
3427
  unsigned int max_overlay_size;
3428
};
3429
3430
/* Set linker_mark and gc_mark on any sections that we will put in
3431
   overlays.  These flags are used by the generic ELF linker, but we
3432
   won't be continuing on to _bfd_elf_final_link() so it is OK to use
3433
   them.  linker_mark is clear before we get here.  Set segment_mark
3434
   on sections that are part of a pasted function (excluding the last
3435
   section).
3436
3437
   Set up function rodata section if --overlay-rodata.  We don't
3438
   currently include merged string constant rodata sections since
3439
3440
   Sort the call graph so that the deepest nodes will be visited
3441
   first.  */
3442
3443
static bool
3444
mark_overlay_section (struct function_info *fun,
3445
          struct bfd_link_info *info,
3446
          void *param)
3447
0
{
3448
0
  struct call_info *call;
3449
0
  unsigned int count;
3450
0
  struct _mos_param *mos_param = param;
3451
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
3452
3453
0
  if (fun->visit4)
3454
0
    return true;
3455
3456
0
  fun->visit4 = true;
3457
0
  if (!fun->sec->linker_mark
3458
0
      && (htab->params->ovly_flavour != ovly_soft_icache
3459
0
    || htab->params->non_ia_text
3460
0
    || startswith (fun->sec->name, ".text.ia.")
3461
0
    || strcmp (fun->sec->name, ".init") == 0
3462
0
    || strcmp (fun->sec->name, ".fini") == 0))
3463
0
    {
3464
0
      unsigned int size;
3465
3466
0
      fun->sec->linker_mark = 1;
3467
0
      fun->sec->gc_mark = 1;
3468
0
      fun->sec->segment_mark = 0;
3469
      /* Ensure SEC_CODE is set on this text section (it ought to
3470
   be!), and SEC_CODE is clear on rodata sections.  We use
3471
   this flag to differentiate the two overlay section types.  */
3472
0
      fun->sec->flags |= SEC_CODE;
3473
3474
0
      size = fun->sec->size;
3475
0
      if (htab->params->auto_overlay & OVERLAY_RODATA)
3476
0
  {
3477
0
    char *name = NULL;
3478
3479
    /* Find the rodata section corresponding to this function's
3480
       text section.  */
3481
0
    if (strcmp (fun->sec->name, ".text") == 0)
3482
0
      {
3483
0
        name = bfd_malloc (sizeof (".rodata"));
3484
0
        if (name == NULL)
3485
0
    return false;
3486
0
        memcpy (name, ".rodata", sizeof (".rodata"));
3487
0
      }
3488
0
    else if (startswith (fun->sec->name, ".text."))
3489
0
      {
3490
0
        size_t len = strlen (fun->sec->name);
3491
0
        name = bfd_malloc (len + 3);
3492
0
        if (name == NULL)
3493
0
    return false;
3494
0
        memcpy (name, ".rodata", sizeof (".rodata"));
3495
0
        memcpy (name + 7, fun->sec->name + 5, len - 4);
3496
0
      }
3497
0
    else if (startswith (fun->sec->name, ".gnu.linkonce.t."))
3498
0
      {
3499
0
        size_t len = strlen (fun->sec->name) + 1;
3500
0
        name = bfd_malloc (len);
3501
0
        if (name == NULL)
3502
0
    return false;
3503
0
        memcpy (name, fun->sec->name, len);
3504
0
        name[14] = 'r';
3505
0
      }
3506
3507
0
    if (name != NULL)
3508
0
      {
3509
0
        asection *rodata = NULL;
3510
0
        asection *group_sec = elf_section_data (fun->sec)->next_in_group;
3511
0
        if (group_sec == NULL)
3512
0
    rodata = bfd_get_section_by_name (fun->sec->owner, name);
3513
0
        else
3514
0
    while (group_sec != NULL && group_sec != fun->sec)
3515
0
      {
3516
0
        if (strcmp (group_sec->name, name) == 0)
3517
0
          {
3518
0
      rodata = group_sec;
3519
0
      break;
3520
0
          }
3521
0
        group_sec = elf_section_data (group_sec)->next_in_group;
3522
0
      }
3523
0
        fun->rodata = rodata;
3524
0
        if (fun->rodata)
3525
0
    {
3526
0
      size += fun->rodata->size;
3527
0
      if (htab->params->line_size != 0
3528
0
          && size > htab->params->line_size)
3529
0
        {
3530
0
          size -= fun->rodata->size;
3531
0
          fun->rodata = NULL;
3532
0
        }
3533
0
      else
3534
0
        {
3535
0
          fun->rodata->linker_mark = 1;
3536
0
          fun->rodata->gc_mark = 1;
3537
0
          fun->rodata->flags &= ~SEC_CODE;
3538
0
        }
3539
0
    }
3540
0
        free (name);
3541
0
      }
3542
0
  }
3543
0
      if (mos_param->max_overlay_size < size)
3544
0
  mos_param->max_overlay_size = size;
3545
0
    }
3546
3547
0
  for (count = 0, call = fun->call_list; call != NULL; call = call->next)
3548
0
    count += 1;
3549
3550
0
  if (count > 1)
3551
0
    {
3552
0
      struct call_info **calls = bfd_malloc (count * sizeof (*calls));
3553
0
      if (calls == NULL)
3554
0
  return false;
3555
3556
0
      for (count = 0, call = fun->call_list; call != NULL; call = call->next)
3557
0
  calls[count++] = call;
3558
3559
0
      qsort (calls, count, sizeof (*calls), sort_calls);
3560
3561
0
      fun->call_list = NULL;
3562
0
      while (count != 0)
3563
0
  {
3564
0
    --count;
3565
0
    calls[count]->next = fun->call_list;
3566
0
    fun->call_list = calls[count];
3567
0
  }
3568
0
      free (calls);
3569
0
    }
3570
3571
0
  for (call = fun->call_list; call != NULL; call = call->next)
3572
0
    {
3573
0
      if (call->is_pasted)
3574
0
  {
3575
    /* There can only be one is_pasted call per function_info.  */
3576
0
    BFD_ASSERT (!fun->sec->segment_mark);
3577
0
    fun->sec->segment_mark = 1;
3578
0
  }
3579
0
      if (!call->broken_cycle
3580
0
    && !mark_overlay_section (call->fun, info, param))
3581
0
  return false;
3582
0
    }
3583
3584
  /* Don't put entry code into an overlay.  The overlay manager needs
3585
     a stack!  Also, don't mark .ovl.init as an overlay.  */
3586
0
  if (fun->lo + fun->sec->output_offset + fun->sec->output_section->vma
3587
0
      == info->output_bfd->start_address
3588
0
      || startswith (fun->sec->output_section->name, ".ovl.init"))
3589
0
    {
3590
0
      fun->sec->linker_mark = 0;
3591
0
      if (fun->rodata != NULL)
3592
0
  fun->rodata->linker_mark = 0;
3593
0
    }
3594
0
  return true;
3595
0
}
3596
3597
/* If non-zero then unmark functions called from those within sections
3598
   that we need to unmark.  Unfortunately this isn't reliable since the
3599
   call graph cannot know the destination of function pointer calls.  */
3600
0
#define RECURSE_UNMARK 0
3601
3602
struct _uos_param {
3603
  asection *exclude_input_section;
3604
  asection *exclude_output_section;
3605
  unsigned long clearing;
3606
};
3607
3608
/* Undo some of mark_overlay_section's work.  */
3609
3610
static bool
3611
unmark_overlay_section (struct function_info *fun,
3612
      struct bfd_link_info *info,
3613
      void *param)
3614
0
{
3615
0
  struct call_info *call;
3616
0
  struct _uos_param *uos_param = param;
3617
0
  unsigned int excluded = 0;
3618
3619
0
  if (fun->visit5)
3620
0
    return true;
3621
3622
0
  fun->visit5 = true;
3623
3624
0
  excluded = 0;
3625
0
  if (fun->sec == uos_param->exclude_input_section
3626
0
      || fun->sec->output_section == uos_param->exclude_output_section)
3627
0
    excluded = 1;
3628
3629
0
  if (RECURSE_UNMARK)
3630
0
    uos_param->clearing += excluded;
3631
3632
0
  if (RECURSE_UNMARK ? uos_param->clearing : excluded)
3633
0
    {
3634
0
      fun->sec->linker_mark = 0;
3635
0
      if (fun->rodata)
3636
0
  fun->rodata->linker_mark = 0;
3637
0
    }
3638
3639
0
  for (call = fun->call_list; call != NULL; call = call->next)
3640
0
    if (!call->broken_cycle
3641
0
  && !unmark_overlay_section (call->fun, info, param))
3642
0
      return false;
3643
3644
0
  if (RECURSE_UNMARK)
3645
0
    uos_param->clearing -= excluded;
3646
0
  return true;
3647
0
}
3648
3649
struct _cl_param {
3650
  unsigned int lib_size;
3651
  asection **lib_sections;
3652
};
3653
3654
/* Add sections we have marked as belonging to overlays to an array
3655
   for consideration as non-overlay sections.  The array consist of
3656
   pairs of sections, (text,rodata), for functions in the call graph.  */
3657
3658
static bool
3659
collect_lib_sections (struct function_info *fun,
3660
          struct bfd_link_info *info,
3661
          void *param)
3662
0
{
3663
0
  struct _cl_param *lib_param = param;
3664
0
  struct call_info *call;
3665
0
  unsigned int size;
3666
3667
0
  if (fun->visit6)
3668
0
    return true;
3669
3670
0
  fun->visit6 = true;
3671
0
  if (!fun->sec->linker_mark || !fun->sec->gc_mark || fun->sec->segment_mark)
3672
0
    return true;
3673
3674
0
  size = fun->sec->size;
3675
0
  if (fun->rodata)
3676
0
    size += fun->rodata->size;
3677
3678
0
  if (size <= lib_param->lib_size)
3679
0
    {
3680
0
      *lib_param->lib_sections++ = fun->sec;
3681
0
      fun->sec->gc_mark = 0;
3682
0
      if (fun->rodata && fun->rodata->linker_mark && fun->rodata->gc_mark)
3683
0
  {
3684
0
    *lib_param->lib_sections++ = fun->rodata;
3685
0
    fun->rodata->gc_mark = 0;
3686
0
  }
3687
0
      else
3688
0
  *lib_param->lib_sections++ = NULL;
3689
0
    }
3690
3691
0
  for (call = fun->call_list; call != NULL; call = call->next)
3692
0
    if (!call->broken_cycle)
3693
0
      collect_lib_sections (call->fun, info, param);
3694
3695
0
  return true;
3696
0
}
3697
3698
/* qsort predicate to sort sections by call count.  */
3699
3700
static int
3701
sort_lib (const void *a, const void *b)
3702
0
{
3703
0
  asection *const *s1 = a;
3704
0
  asection *const *s2 = b;
3705
0
  struct _spu_elf_section_data *sec_data;
3706
0
  struct spu_elf_stack_info *sinfo;
3707
0
  int delta;
3708
3709
0
  delta = 0;
3710
0
  if ((sec_data = spu_elf_section_data (*s1)) != NULL
3711
0
      && (sinfo = sec_data->u.i.stack_info) != NULL)
3712
0
    {
3713
0
      int i;
3714
0
      for (i = 0; i < sinfo->num_fun; ++i)
3715
0
  delta -= sinfo->fun[i].call_count;
3716
0
    }
3717
3718
0
  if ((sec_data = spu_elf_section_data (*s2)) != NULL
3719
0
      && (sinfo = sec_data->u.i.stack_info) != NULL)
3720
0
    {
3721
0
      int i;
3722
0
      for (i = 0; i < sinfo->num_fun; ++i)
3723
0
  delta += sinfo->fun[i].call_count;
3724
0
    }
3725
3726
0
  if (delta != 0)
3727
0
    return delta;
3728
3729
0
  return s1 - s2;
3730
0
}
3731
3732
/* Remove some sections from those marked to be in overlays.  Choose
3733
   those that are called from many places, likely library functions.  */
3734
3735
static unsigned int
3736
auto_ovl_lib_functions (struct bfd_link_info *info, unsigned int lib_size)
3737
0
{
3738
0
  bfd *ibfd;
3739
0
  asection **lib_sections;
3740
0
  unsigned int i, lib_count;
3741
0
  struct _cl_param collect_lib_param;
3742
0
  struct function_info dummy_caller;
3743
0
  struct spu_link_hash_table *htab;
3744
3745
0
  memset (&dummy_caller, 0, sizeof (dummy_caller));
3746
0
  lib_count = 0;
3747
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3748
0
    {
3749
0
      extern const bfd_target spu_elf32_vec;
3750
0
      asection *sec;
3751
3752
0
      if (ibfd->xvec != &spu_elf32_vec)
3753
0
  continue;
3754
3755
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3756
0
  if (sec->linker_mark
3757
0
      && sec->size < lib_size
3758
0
      && (sec->flags & SEC_CODE) != 0)
3759
0
    lib_count += 1;
3760
0
    }
3761
0
  lib_sections = bfd_malloc (lib_count * 2 * sizeof (*lib_sections));
3762
0
  if (lib_sections == NULL)
3763
0
    return (unsigned int) -1;
3764
0
  collect_lib_param.lib_size = lib_size;
3765
0
  collect_lib_param.lib_sections = lib_sections;
3766
0
  if (!for_each_node (collect_lib_sections, info, &collect_lib_param,
3767
0
          true))
3768
0
    return (unsigned int) -1;
3769
0
  lib_count = (collect_lib_param.lib_sections - lib_sections) / 2;
3770
3771
  /* Sort sections so that those with the most calls are first.  */
3772
0
  if (lib_count > 1)
3773
0
    qsort (lib_sections, lib_count, 2 * sizeof (*lib_sections), sort_lib);
3774
3775
0
  htab = spu_hash_table (info);
3776
0
  for (i = 0; i < lib_count; i++)
3777
0
    {
3778
0
      unsigned int tmp, stub_size;
3779
0
      asection *sec;
3780
0
      struct _spu_elf_section_data *sec_data;
3781
0
      struct spu_elf_stack_info *sinfo;
3782
3783
0
      sec = lib_sections[2 * i];
3784
      /* If this section is OK, its size must be less than lib_size.  */
3785
0
      tmp = sec->size;
3786
      /* If it has a rodata section, then add that too.  */
3787
0
      if (lib_sections[2 * i + 1])
3788
0
  tmp += lib_sections[2 * i + 1]->size;
3789
      /* Add any new overlay call stubs needed by the section.  */
3790
0
      stub_size = 0;
3791
0
      if (tmp < lib_size
3792
0
    && (sec_data = spu_elf_section_data (sec)) != NULL
3793
0
    && (sinfo = sec_data->u.i.stack_info) != NULL)
3794
0
  {
3795
0
    int k;
3796
0
    struct call_info *call;
3797
3798
0
    for (k = 0; k < sinfo->num_fun; ++k)
3799
0
      for (call = sinfo->fun[k].call_list; call; call = call->next)
3800
0
        if (call->fun->sec->linker_mark)
3801
0
    {
3802
0
      struct call_info *p;
3803
0
      for (p = dummy_caller.call_list; p; p = p->next)
3804
0
        if (p->fun == call->fun)
3805
0
          break;
3806
0
      if (!p)
3807
0
        stub_size += ovl_stub_size (htab->params);
3808
0
    }
3809
0
  }
3810
0
      if (tmp + stub_size < lib_size)
3811
0
  {
3812
0
    struct call_info **pp, *p;
3813
3814
    /* This section fits.  Mark it as non-overlay.  */
3815
0
    lib_sections[2 * i]->linker_mark = 0;
3816
0
    if (lib_sections[2 * i + 1])
3817
0
      lib_sections[2 * i + 1]->linker_mark = 0;
3818
0
    lib_size -= tmp + stub_size;
3819
    /* Call stubs to the section we just added are no longer
3820
       needed.  */
3821
0
    pp = &dummy_caller.call_list;
3822
0
    while ((p = *pp) != NULL)
3823
0
      if (!p->fun->sec->linker_mark)
3824
0
        {
3825
0
    lib_size += ovl_stub_size (htab->params);
3826
0
    *pp = p->next;
3827
0
    free (p);
3828
0
        }
3829
0
      else
3830
0
        pp = &p->next;
3831
    /* Add new call stubs to dummy_caller.  */
3832
0
    if ((sec_data = spu_elf_section_data (sec)) != NULL
3833
0
        && (sinfo = sec_data->u.i.stack_info) != NULL)
3834
0
      {
3835
0
        int k;
3836
0
        struct call_info *call;
3837
3838
0
        for (k = 0; k < sinfo->num_fun; ++k)
3839
0
    for (call = sinfo->fun[k].call_list;
3840
0
         call;
3841
0
         call = call->next)
3842
0
      if (call->fun->sec->linker_mark)
3843
0
        {
3844
0
          struct call_info *callee;
3845
0
          callee = bfd_malloc (sizeof (*callee));
3846
0
          if (callee == NULL)
3847
0
      return (unsigned int) -1;
3848
0
          *callee = *call;
3849
0
          if (!insert_callee (&dummy_caller, callee))
3850
0
      free (callee);
3851
0
        }
3852
0
      }
3853
0
  }
3854
0
    }
3855
0
  while (dummy_caller.call_list != NULL)
3856
0
    {
3857
0
      struct call_info *call = dummy_caller.call_list;
3858
0
      dummy_caller.call_list = call->next;
3859
0
      free (call);
3860
0
    }
3861
0
  for (i = 0; i < 2 * lib_count; i++)
3862
0
    if (lib_sections[i])
3863
0
      lib_sections[i]->gc_mark = 1;
3864
0
  free (lib_sections);
3865
0
  return lib_size;
3866
0
}
3867
3868
/* Build an array of overlay sections.  The deepest node's section is
3869
   added first, then its parent node's section, then everything called
3870
   from the parent section.  The idea being to group sections to
3871
   minimise calls between different overlays.  */
3872
3873
static bool
3874
collect_overlays (struct function_info *fun,
3875
      struct bfd_link_info *info,
3876
      void *param)
3877
0
{
3878
0
  struct call_info *call;
3879
0
  bool added_fun;
3880
0
  asection ***ovly_sections = param;
3881
3882
0
  if (fun->visit7)
3883
0
    return true;
3884
3885
0
  fun->visit7 = true;
3886
0
  for (call = fun->call_list; call != NULL; call = call->next)
3887
0
    if (!call->is_pasted && !call->broken_cycle)
3888
0
      {
3889
0
  if (!collect_overlays (call->fun, info, ovly_sections))
3890
0
    return false;
3891
0
  break;
3892
0
      }
3893
3894
0
  added_fun = false;
3895
0
  if (fun->sec->linker_mark && fun->sec->gc_mark)
3896
0
    {
3897
0
      fun->sec->gc_mark = 0;
3898
0
      *(*ovly_sections)++ = fun->sec;
3899
0
      if (fun->rodata && fun->rodata->linker_mark && fun->rodata->gc_mark)
3900
0
  {
3901
0
    fun->rodata->gc_mark = 0;
3902
0
    *(*ovly_sections)++ = fun->rodata;
3903
0
  }
3904
0
      else
3905
0
  *(*ovly_sections)++ = NULL;
3906
0
      added_fun = true;
3907
3908
      /* Pasted sections must stay with the first section.  We don't
3909
   put pasted sections in the array, just the first section.
3910
   Mark subsequent sections as already considered.  */
3911
0
      if (fun->sec->segment_mark)
3912
0
  {
3913
0
    struct function_info *call_fun = fun;
3914
0
    do
3915
0
      {
3916
0
        for (call = call_fun->call_list; call != NULL; call = call->next)
3917
0
    if (call->is_pasted)
3918
0
      {
3919
0
        call_fun = call->fun;
3920
0
        call_fun->sec->gc_mark = 0;
3921
0
        if (call_fun->rodata)
3922
0
          call_fun->rodata->gc_mark = 0;
3923
0
        break;
3924
0
      }
3925
0
        if (call == NULL)
3926
0
    abort ();
3927
0
      }
3928
0
    while (call_fun->sec->segment_mark);
3929
0
  }
3930
0
    }
3931
3932
0
  for (call = fun->call_list; call != NULL; call = call->next)
3933
0
    if (!call->broken_cycle
3934
0
  && !collect_overlays (call->fun, info, ovly_sections))
3935
0
      return false;
3936
3937
0
  if (added_fun)
3938
0
    {
3939
0
      struct _spu_elf_section_data *sec_data;
3940
0
      struct spu_elf_stack_info *sinfo;
3941
3942
0
      if ((sec_data = spu_elf_section_data (fun->sec)) != NULL
3943
0
    && (sinfo = sec_data->u.i.stack_info) != NULL)
3944
0
  {
3945
0
    int i;
3946
0
    for (i = 0; i < sinfo->num_fun; ++i)
3947
0
      if (!collect_overlays (&sinfo->fun[i], info, ovly_sections))
3948
0
        return false;
3949
0
  }
3950
0
    }
3951
3952
0
  return true;
3953
0
}
3954
3955
struct _sum_stack_param {
3956
  size_t cum_stack;
3957
  size_t overall_stack;
3958
  bool emit_stack_syms;
3959
};
3960
3961
/* Descend the call graph for FUN, accumulating total stack required.  */
3962
3963
static bool
3964
sum_stack (struct function_info *fun,
3965
     struct bfd_link_info *info,
3966
     void *param)
3967
0
{
3968
0
  struct call_info *call;
3969
0
  struct function_info *max;
3970
0
  size_t stack, cum_stack;
3971
0
  const char *f1;
3972
0
  bool has_call;
3973
0
  struct _sum_stack_param *sum_stack_param = param;
3974
0
  struct spu_link_hash_table *htab;
3975
3976
0
  cum_stack = fun->stack;
3977
0
  sum_stack_param->cum_stack = cum_stack;
3978
0
  if (fun->visit3)
3979
0
    return true;
3980
3981
0
  has_call = false;
3982
0
  max = NULL;
3983
0
  for (call = fun->call_list; call; call = call->next)
3984
0
    {
3985
0
      if (call->broken_cycle)
3986
0
  continue;
3987
0
      if (!call->is_pasted)
3988
0
  has_call = true;
3989
0
      if (!sum_stack (call->fun, info, sum_stack_param))
3990
0
  return false;
3991
0
      stack = sum_stack_param->cum_stack;
3992
      /* Include caller stack for normal calls, don't do so for
3993
   tail calls.  fun->stack here is local stack usage for
3994
   this function.  */
3995
0
      if (!call->is_tail || call->is_pasted || call->fun->start != NULL)
3996
0
  stack += fun->stack;
3997
0
      if (cum_stack < stack)
3998
0
  {
3999
0
    cum_stack = stack;
4000
0
    max = call->fun;
4001
0
  }
4002
0
    }
4003
4004
0
  sum_stack_param->cum_stack = cum_stack;
4005
0
  stack = fun->stack;
4006
  /* Now fun->stack holds cumulative stack.  */
4007
0
  fun->stack = cum_stack;
4008
0
  fun->visit3 = true;
4009
4010
0
  if (!fun->non_root
4011
0
      && sum_stack_param->overall_stack < cum_stack)
4012
0
    sum_stack_param->overall_stack = cum_stack;
4013
4014
0
  htab = spu_hash_table (info);
4015
0
  if (htab->params->auto_overlay)
4016
0
    return true;
4017
4018
0
  f1 = func_name (fun);
4019
0
  if (htab->params->stack_analysis)
4020
0
    {
4021
0
      if (!fun->non_root)
4022
0
  info->callbacks->info ("  %s: 0x%v\n", f1, (bfd_vma) cum_stack);
4023
0
      info->callbacks->minfo ("%s: 0x%v 0x%v\n",
4024
0
            f1, (bfd_vma) stack, (bfd_vma) cum_stack);
4025
4026
0
      if (has_call)
4027
0
  {
4028
0
    info->callbacks->minfo (_("  calls:\n"));
4029
0
    for (call = fun->call_list; call; call = call->next)
4030
0
      if (!call->is_pasted && !call->broken_cycle)
4031
0
        {
4032
0
    const char *f2 = func_name (call->fun);
4033
0
    const char *ann1 = call->fun == max ? "*" : " ";
4034
0
    const char *ann2 = call->is_tail ? "t" : " ";
4035
4036
0
    info->callbacks->minfo ("   %s%s %s\n", ann1, ann2, f2);
4037
0
        }
4038
0
  }
4039
0
    }
4040
4041
0
  if (sum_stack_param->emit_stack_syms)
4042
0
    {
4043
0
      char *name = bfd_malloc (18 + strlen (f1));
4044
0
      struct elf_link_hash_entry *h;
4045
4046
0
      if (name == NULL)
4047
0
  return false;
4048
4049
0
      if (fun->global || ELF_ST_BIND (fun->u.sym->st_info) == STB_GLOBAL)
4050
0
  sprintf (name, "__stack_%s", f1);
4051
0
      else
4052
0
  sprintf (name, "__stack_%x_%s", fun->sec->id & 0xffffffff, f1);
4053
4054
0
      h = elf_link_hash_lookup (&htab->elf, name, true, true, false);
4055
0
      free (name);
4056
0
      if (h != NULL
4057
0
    && (h->root.type == bfd_link_hash_new
4058
0
        || h->root.type == bfd_link_hash_undefined
4059
0
        || h->root.type == bfd_link_hash_undefweak))
4060
0
  {
4061
0
    h->root.type = bfd_link_hash_defined;
4062
0
    h->root.u.def.section = bfd_abs_section_ptr;
4063
0
    h->root.u.def.value = cum_stack;
4064
0
    h->size = 0;
4065
0
    h->type = 0;
4066
0
    h->ref_regular = 1;
4067
0
    h->def_regular = 1;
4068
0
    h->ref_regular_nonweak = 1;
4069
0
    h->forced_local = 1;
4070
0
    h->non_elf = 0;
4071
0
  }
4072
0
    }
4073
4074
0
  return true;
4075
0
}
4076
4077
/* SEC is part of a pasted function.  Return the call_info for the
4078
   next section of this function.  */
4079
4080
static struct call_info *
4081
find_pasted_call (asection *sec)
4082
0
{
4083
0
  struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
4084
0
  struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
4085
0
  struct call_info *call;
4086
0
  int k;
4087
4088
0
  for (k = 0; k < sinfo->num_fun; ++k)
4089
0
    for (call = sinfo->fun[k].call_list; call != NULL; call = call->next)
4090
0
      if (call->is_pasted)
4091
0
  return call;
4092
0
  abort ();
4093
0
  return 0;
4094
0
}
4095
4096
/* qsort predicate to sort bfds by file name.  */
4097
4098
static int
4099
sort_bfds (const void *a, const void *b)
4100
0
{
4101
0
  bfd *const *abfd1 = a;
4102
0
  bfd *const *abfd2 = b;
4103
4104
0
  return filename_cmp (bfd_get_filename (*abfd1), bfd_get_filename (*abfd2));
4105
0
}
4106
4107
static unsigned int
4108
print_one_overlay_section (FILE *script,
4109
         unsigned int base,
4110
         unsigned int count,
4111
         unsigned int ovlynum,
4112
         unsigned int *ovly_map,
4113
         asection **ovly_sections,
4114
         struct bfd_link_info *info)
4115
0
{
4116
0
  unsigned int j;
4117
4118
0
  for (j = base; j < count && ovly_map[j] == ovlynum; j++)
4119
0
    {
4120
0
      asection *sec = ovly_sections[2 * j];
4121
4122
0
      if (fprintf (script, "   %s%c%s (%s)\n",
4123
0
       (sec->owner->my_archive != NULL
4124
0
        ? bfd_get_filename (sec->owner->my_archive) : ""),
4125
0
       info->path_separator,
4126
0
       bfd_get_filename (sec->owner),
4127
0
       sec->name) <= 0)
4128
0
  return -1;
4129
0
      if (sec->segment_mark)
4130
0
  {
4131
0
    struct call_info *call = find_pasted_call (sec);
4132
0
    while (call != NULL)
4133
0
      {
4134
0
        struct function_info *call_fun = call->fun;
4135
0
        sec = call_fun->sec;
4136
0
        if (fprintf (script, "   %s%c%s (%s)\n",
4137
0
         (sec->owner->my_archive != NULL
4138
0
          ? bfd_get_filename (sec->owner->my_archive) : ""),
4139
0
         info->path_separator,
4140
0
         bfd_get_filename (sec->owner),
4141
0
         sec->name) <= 0)
4142
0
    return -1;
4143
0
        for (call = call_fun->call_list; call; call = call->next)
4144
0
    if (call->is_pasted)
4145
0
      break;
4146
0
      }
4147
0
  }
4148
0
    }
4149
4150
0
  for (j = base; j < count && ovly_map[j] == ovlynum; j++)
4151
0
    {
4152
0
      asection *sec = ovly_sections[2 * j + 1];
4153
0
      if (sec != NULL
4154
0
    && fprintf (script, "   %s%c%s (%s)\n",
4155
0
          (sec->owner->my_archive != NULL
4156
0
           ? bfd_get_filename (sec->owner->my_archive) : ""),
4157
0
          info->path_separator,
4158
0
          bfd_get_filename (sec->owner),
4159
0
          sec->name) <= 0)
4160
0
  return -1;
4161
4162
0
      sec = ovly_sections[2 * j];
4163
0
      if (sec->segment_mark)
4164
0
  {
4165
0
    struct call_info *call = find_pasted_call (sec);
4166
0
    while (call != NULL)
4167
0
      {
4168
0
        struct function_info *call_fun = call->fun;
4169
0
        sec = call_fun->rodata;
4170
0
        if (sec != NULL
4171
0
      && fprintf (script, "   %s%c%s (%s)\n",
4172
0
            (sec->owner->my_archive != NULL
4173
0
             ? bfd_get_filename (sec->owner->my_archive) : ""),
4174
0
            info->path_separator,
4175
0
            bfd_get_filename (sec->owner),
4176
0
            sec->name) <= 0)
4177
0
    return -1;
4178
0
        for (call = call_fun->call_list; call; call = call->next)
4179
0
    if (call->is_pasted)
4180
0
      break;
4181
0
      }
4182
0
  }
4183
0
    }
4184
4185
0
  return j;
4186
0
}
4187
4188
/* Handle --auto-overlay.  */
4189
4190
static void
4191
spu_elf_auto_overlay (struct bfd_link_info *info)
4192
0
{
4193
0
  bfd *ibfd;
4194
0
  bfd **bfd_arr;
4195
0
  struct elf_segment_map *m;
4196
0
  unsigned int fixed_size, lo, hi;
4197
0
  unsigned int reserved;
4198
0
  struct spu_link_hash_table *htab;
4199
0
  unsigned int base, i, count, bfd_count;
4200
0
  unsigned int region, ovlynum;
4201
0
  asection **ovly_sections, **ovly_p;
4202
0
  unsigned int *ovly_map;
4203
0
  FILE *script;
4204
0
  unsigned int total_overlay_size, overlay_size;
4205
0
  const char *ovly_mgr_entry;
4206
0
  struct elf_link_hash_entry *h;
4207
0
  struct _mos_param mos_param;
4208
0
  struct _uos_param uos_param;
4209
0
  struct function_info dummy_caller;
4210
4211
  /* Find the extents of our loadable image.  */
4212
0
  lo = (unsigned int) -1;
4213
0
  hi = 0;
4214
0
  for (m = elf_seg_map (info->output_bfd); m != NULL; m = m->next)
4215
0
    if (m->p_type == PT_LOAD)
4216
0
      for (i = 0; i < m->count; i++)
4217
0
  if (m->sections[i]->size != 0)
4218
0
    {
4219
0
      if (m->sections[i]->vma < lo)
4220
0
        lo = m->sections[i]->vma;
4221
0
      if (m->sections[i]->vma + m->sections[i]->size - 1 > hi)
4222
0
        hi = m->sections[i]->vma + m->sections[i]->size - 1;
4223
0
    }
4224
0
  fixed_size = hi + 1 - lo;
4225
4226
0
  if (!discover_functions (info))
4227
0
    goto err_exit;
4228
4229
0
  if (!build_call_tree (info))
4230
0
    goto err_exit;
4231
4232
0
  htab = spu_hash_table (info);
4233
0
  reserved = htab->params->auto_overlay_reserved;
4234
0
  if (reserved == 0)
4235
0
    {
4236
0
      struct _sum_stack_param sum_stack_param;
4237
4238
0
      sum_stack_param.emit_stack_syms = 0;
4239
0
      sum_stack_param.overall_stack = 0;
4240
0
      if (!for_each_node (sum_stack, info, &sum_stack_param, true))
4241
0
  goto err_exit;
4242
0
      reserved = (sum_stack_param.overall_stack
4243
0
      + htab->params->extra_stack_space);
4244
0
    }
4245
4246
  /* No need for overlays if everything already fits.  */
4247
0
  if (fixed_size + reserved <= htab->local_store
4248
0
      && htab->params->ovly_flavour != ovly_soft_icache)
4249
0
    {
4250
0
      htab->params->auto_overlay = 0;
4251
0
      return;
4252
0
    }
4253
4254
0
  uos_param.exclude_input_section = 0;
4255
0
  uos_param.exclude_output_section
4256
0
    = bfd_get_section_by_name (info->output_bfd, ".interrupt");
4257
4258
0
  ovly_mgr_entry = "__ovly_load";
4259
0
  if (htab->params->ovly_flavour == ovly_soft_icache)
4260
0
    ovly_mgr_entry = "__icache_br_handler";
4261
0
  h = elf_link_hash_lookup (&htab->elf, ovly_mgr_entry,
4262
0
          false, false, false);
4263
0
  if (h != NULL
4264
0
      && (h->root.type == bfd_link_hash_defined
4265
0
    || h->root.type == bfd_link_hash_defweak)
4266
0
      && h->def_regular)
4267
0
    {
4268
      /* We have a user supplied overlay manager.  */
4269
0
      uos_param.exclude_input_section = h->root.u.def.section;
4270
0
    }
4271
0
  else
4272
0
    {
4273
      /* If no user overlay manager, spu_elf_load_ovl_mgr will add our
4274
   builtin version to .text, and will adjust .text size.  */
4275
0
      fixed_size += (*htab->params->spu_elf_load_ovl_mgr) ();
4276
0
    }
4277
4278
  /* Mark overlay sections, and find max overlay section size.  */
4279
0
  mos_param.max_overlay_size = 0;
4280
0
  if (!for_each_node (mark_overlay_section, info, &mos_param, true))
4281
0
    goto err_exit;
4282
4283
  /* We can't put the overlay manager or interrupt routines in
4284
     overlays.  */
4285
0
  uos_param.clearing = 0;
4286
0
  if ((uos_param.exclude_input_section
4287
0
       || uos_param.exclude_output_section)
4288
0
      && !for_each_node (unmark_overlay_section, info, &uos_param, true))
4289
0
    goto err_exit;
4290
4291
0
  bfd_count = 0;
4292
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4293
0
    ++bfd_count;
4294
0
  bfd_arr = bfd_malloc (bfd_count * sizeof (*bfd_arr));
4295
0
  if (bfd_arr == NULL)
4296
0
    goto err_exit;
4297
4298
  /* Count overlay sections, and subtract their sizes from "fixed_size".  */
4299
0
  count = 0;
4300
0
  bfd_count = 0;
4301
0
  total_overlay_size = 0;
4302
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4303
0
    {
4304
0
      extern const bfd_target spu_elf32_vec;
4305
0
      asection *sec;
4306
0
      unsigned int old_count;
4307
4308
0
      if (ibfd->xvec != &spu_elf32_vec)
4309
0
  continue;
4310
4311
0
      old_count = count;
4312
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4313
0
  if (sec->linker_mark)
4314
0
    {
4315
0
      if ((sec->flags & SEC_CODE) != 0)
4316
0
        count += 1;
4317
0
      fixed_size -= sec->size;
4318
0
      total_overlay_size += sec->size;
4319
0
    }
4320
0
  else if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD)
4321
0
     && sec->output_section->owner == info->output_bfd
4322
0
     && startswith (sec->output_section->name, ".ovl.init"))
4323
0
    fixed_size -= sec->size;
4324
0
      if (count != old_count)
4325
0
  bfd_arr[bfd_count++] = ibfd;
4326
0
    }
4327
4328
  /* Since the overlay link script selects sections by file name and
4329
     section name, ensure that file names are unique.  */
4330
0
  if (bfd_count > 1)
4331
0
    {
4332
0
      bool ok = true;
4333
4334
0
      qsort (bfd_arr, bfd_count, sizeof (*bfd_arr), sort_bfds);
4335
0
      for (i = 1; i < bfd_count; ++i)
4336
0
  if (filename_cmp (bfd_get_filename (bfd_arr[i - 1]),
4337
0
        bfd_get_filename (bfd_arr[i])) == 0)
4338
0
    {
4339
0
      if (bfd_arr[i - 1]->my_archive == bfd_arr[i]->my_archive)
4340
0
        {
4341
0
    if (bfd_arr[i - 1]->my_archive && bfd_arr[i]->my_archive)
4342
      /* xgettext:c-format */
4343
0
      info->callbacks->einfo (_("%s duplicated in %s\n"),
4344
0
            bfd_get_filename (bfd_arr[i]),
4345
0
            bfd_get_filename (bfd_arr[i]->my_archive));
4346
0
    else
4347
0
      info->callbacks->einfo (_("%s duplicated\n"),
4348
0
            bfd_get_filename (bfd_arr[i]));
4349
0
    ok = false;
4350
0
        }
4351
0
    }
4352
0
      if (!ok)
4353
0
  {
4354
0
    info->callbacks->einfo (_("sorry, no support for duplicate "
4355
0
            "object files in auto-overlay script\n"));
4356
0
    bfd_set_error (bfd_error_bad_value);
4357
0
    goto err_exit;
4358
0
  }
4359
0
    }
4360
0
  free (bfd_arr);
4361
4362
0
  fixed_size += reserved;
4363
0
  fixed_size += htab->non_ovly_stub * ovl_stub_size (htab->params);
4364
0
  if (fixed_size + mos_param.max_overlay_size <= htab->local_store)
4365
0
    {
4366
0
      if (htab->params->ovly_flavour == ovly_soft_icache)
4367
0
  {
4368
    /* Stubs in the non-icache area are bigger.  */
4369
0
    fixed_size += htab->non_ovly_stub * 16;
4370
    /* Space for icache manager tables.
4371
       a) Tag array, one quadword per cache line.
4372
       - word 0: ia address of present line, init to zero.  */
4373
0
    fixed_size += 16 << htab->num_lines_log2;
4374
    /* b) Rewrite "to" list, one quadword per cache line.  */
4375
0
    fixed_size += 16 << htab->num_lines_log2;
4376
    /* c) Rewrite "from" list, one byte per outgoing branch (rounded up
4377
    to a power-of-two number of full quadwords) per cache line.  */
4378
0
    fixed_size += 16 << (htab->fromelem_size_log2
4379
0
             + htab->num_lines_log2);
4380
    /* d) Pointer to __ea backing store (toe), 1 quadword.  */
4381
0
    fixed_size += 16;
4382
0
  }
4383
0
      else
4384
0
  {
4385
    /* Guess number of overlays.  Assuming overlay buffer is on
4386
       average only half full should be conservative.  */
4387
0
    ovlynum = (total_overlay_size * 2 * htab->params->num_lines
4388
0
         / (htab->local_store - fixed_size));
4389
    /* Space for _ovly_table[], _ovly_buf_table[] and toe.  */
4390
0
    fixed_size += ovlynum * 16 + 16 + 4 + 16;
4391
0
  }
4392
0
    }
4393
4394
0
  if (fixed_size + mos_param.max_overlay_size > htab->local_store)
4395
    /* xgettext:c-format */
4396
0
    info->callbacks->einfo (_("non-overlay size of 0x%v plus maximum overlay "
4397
0
            "size of 0x%v exceeds local store\n"),
4398
0
          (bfd_vma) fixed_size,
4399
0
          (bfd_vma) mos_param.max_overlay_size);
4400
4401
  /* Now see if we should put some functions in the non-overlay area.  */
4402
0
  else if (fixed_size < htab->params->auto_overlay_fixed)
4403
0
    {
4404
0
      unsigned int max_fixed, lib_size;
4405
4406
0
      max_fixed = htab->local_store - mos_param.max_overlay_size;
4407
0
      if (max_fixed > htab->params->auto_overlay_fixed)
4408
0
  max_fixed = htab->params->auto_overlay_fixed;
4409
0
      lib_size = max_fixed - fixed_size;
4410
0
      lib_size = auto_ovl_lib_functions (info, lib_size);
4411
0
      if (lib_size == (unsigned int) -1)
4412
0
  goto err_exit;
4413
0
      fixed_size = max_fixed - lib_size;
4414
0
    }
4415
4416
  /* Build an array of sections, suitably sorted to place into
4417
     overlays.  */
4418
0
  ovly_sections = bfd_malloc (2 * count * sizeof (*ovly_sections));
4419
0
  if (ovly_sections == NULL)
4420
0
    goto err_exit;
4421
0
  ovly_p = ovly_sections;
4422
0
  if (!for_each_node (collect_overlays, info, &ovly_p, true))
4423
0
    goto err_exit;
4424
0
  count = (size_t) (ovly_p - ovly_sections) / 2;
4425
0
  ovly_map = bfd_malloc (count * sizeof (*ovly_map));
4426
0
  if (ovly_map == NULL)
4427
0
    goto err_exit;
4428
4429
0
  memset (&dummy_caller, 0, sizeof (dummy_caller));
4430
0
  overlay_size = (htab->local_store - fixed_size) / htab->params->num_lines;
4431
0
  if (htab->params->line_size != 0)
4432
0
    overlay_size = htab->params->line_size;
4433
0
  base = 0;
4434
0
  ovlynum = 0;
4435
0
  while (base < count)
4436
0
    {
4437
0
      unsigned int size = 0, rosize = 0, roalign = 0;
4438
4439
0
      for (i = base; i < count; i++)
4440
0
  {
4441
0
    asection *sec, *rosec;
4442
0
    unsigned int tmp, rotmp;
4443
0
    unsigned int num_stubs;
4444
0
    struct call_info *call, *pasty;
4445
0
    struct _spu_elf_section_data *sec_data;
4446
0
    struct spu_elf_stack_info *sinfo;
4447
0
    unsigned int k;
4448
4449
    /* See whether we can add this section to the current
4450
       overlay without overflowing our overlay buffer.  */
4451
0
    sec = ovly_sections[2 * i];
4452
0
    tmp = align_power (size, sec->alignment_power) + sec->size;
4453
0
    rotmp = rosize;
4454
0
    rosec = ovly_sections[2 * i + 1];
4455
0
    if (rosec != NULL)
4456
0
      {
4457
0
        rotmp = align_power (rotmp, rosec->alignment_power) + rosec->size;
4458
0
        if (roalign < rosec->alignment_power)
4459
0
    roalign = rosec->alignment_power;
4460
0
      }
4461
0
    if (align_power (tmp, roalign) + rotmp > overlay_size)
4462
0
      break;
4463
0
    if (sec->segment_mark)
4464
0
      {
4465
        /* Pasted sections must stay together, so add their
4466
     sizes too.  */
4467
0
        pasty = find_pasted_call (sec);
4468
0
        while (pasty != NULL)
4469
0
    {
4470
0
      struct function_info *call_fun = pasty->fun;
4471
0
      tmp = (align_power (tmp, call_fun->sec->alignment_power)
4472
0
       + call_fun->sec->size);
4473
0
      if (call_fun->rodata)
4474
0
        {
4475
0
          rotmp = (align_power (rotmp,
4476
0
              call_fun->rodata->alignment_power)
4477
0
             + call_fun->rodata->size);
4478
0
          if (roalign < rosec->alignment_power)
4479
0
      roalign = rosec->alignment_power;
4480
0
        }
4481
0
      for (pasty = call_fun->call_list; pasty; pasty = pasty->next)
4482
0
        if (pasty->is_pasted)
4483
0
          break;
4484
0
    }
4485
0
      }
4486
0
    if (align_power (tmp, roalign) + rotmp > overlay_size)
4487
0
      break;
4488
4489
    /* If we add this section, we might need new overlay call
4490
       stubs.  Add any overlay section calls to dummy_call.  */
4491
0
    pasty = NULL;
4492
0
    sec_data = spu_elf_section_data (sec);
4493
0
    sinfo = sec_data->u.i.stack_info;
4494
0
    for (k = 0; k < (unsigned) sinfo->num_fun; ++k)
4495
0
      for (call = sinfo->fun[k].call_list; call; call = call->next)
4496
0
        if (call->is_pasted)
4497
0
    {
4498
0
      BFD_ASSERT (pasty == NULL);
4499
0
      pasty = call;
4500
0
    }
4501
0
        else if (call->fun->sec->linker_mark)
4502
0
    {
4503
0
      if (!copy_callee (&dummy_caller, call))
4504
0
        goto err_exit;
4505
0
    }
4506
0
    while (pasty != NULL)
4507
0
      {
4508
0
        struct function_info *call_fun = pasty->fun;
4509
0
        pasty = NULL;
4510
0
        for (call = call_fun->call_list; call; call = call->next)
4511
0
    if (call->is_pasted)
4512
0
      {
4513
0
        BFD_ASSERT (pasty == NULL);
4514
0
        pasty = call;
4515
0
      }
4516
0
    else if (!copy_callee (&dummy_caller, call))
4517
0
      goto err_exit;
4518
0
      }
4519
4520
    /* Calculate call stub size.  */
4521
0
    num_stubs = 0;
4522
0
    for (call = dummy_caller.call_list; call; call = call->next)
4523
0
      {
4524
0
        unsigned int stub_delta = 1;
4525
4526
0
        if (htab->params->ovly_flavour == ovly_soft_icache)
4527
0
    stub_delta = call->count;
4528
0
        num_stubs += stub_delta;
4529
4530
        /* If the call is within this overlay, we won't need a
4531
     stub.  */
4532
0
        for (k = base; k < i + 1; k++)
4533
0
    if (call->fun->sec == ovly_sections[2 * k])
4534
0
      {
4535
0
        num_stubs -= stub_delta;
4536
0
        break;
4537
0
      }
4538
0
      }
4539
0
    if (htab->params->ovly_flavour == ovly_soft_icache
4540
0
        && num_stubs > htab->params->max_branch)
4541
0
      break;
4542
0
    if (align_power (tmp, roalign) + rotmp
4543
0
        + num_stubs * ovl_stub_size (htab->params) > overlay_size)
4544
0
      break;
4545
0
    size = tmp;
4546
0
    rosize = rotmp;
4547
0
  }
4548
4549
0
      if (i == base)
4550
0
  {
4551
    /* xgettext:c-format */
4552
0
    info->callbacks->einfo (_("%pB:%pA%s exceeds overlay size\n"),
4553
0
          ovly_sections[2 * i]->owner,
4554
0
          ovly_sections[2 * i],
4555
0
          ovly_sections[2 * i + 1] ? " + rodata" : "");
4556
0
    bfd_set_error (bfd_error_bad_value);
4557
0
    goto err_exit;
4558
0
  }
4559
4560
0
      while (dummy_caller.call_list != NULL)
4561
0
  {
4562
0
    struct call_info *call = dummy_caller.call_list;
4563
0
    dummy_caller.call_list = call->next;
4564
0
    free (call);
4565
0
  }
4566
4567
0
      ++ovlynum;
4568
0
      while (base < i)
4569
0
  ovly_map[base++] = ovlynum;
4570
0
    }
4571
4572
0
  script = htab->params->spu_elf_open_overlay_script ();
4573
4574
0
  if (htab->params->ovly_flavour == ovly_soft_icache)
4575
0
    {
4576
0
      if (fprintf (script, "SECTIONS\n{\n") <= 0)
4577
0
  goto file_err;
4578
4579
0
      if (fprintf (script,
4580
0
       " . = ALIGN (%u);\n"
4581
0
       " .ovl.init : { *(.ovl.init) }\n"
4582
0
       " . = ABSOLUTE (ADDR (.ovl.init));\n",
4583
0
       htab->params->line_size) <= 0)
4584
0
  goto file_err;
4585
4586
0
      base = 0;
4587
0
      ovlynum = 1;
4588
0
      while (base < count)
4589
0
  {
4590
0
    unsigned int indx = ovlynum - 1;
4591
0
    unsigned int vma, lma;
4592
4593
0
    vma = (indx & (htab->params->num_lines - 1)) << htab->line_size_log2;
4594
0
    lma = vma + (((indx >> htab->num_lines_log2) + 1) << 18);
4595
4596
0
    if (fprintf (script, " .ovly%u ABSOLUTE (ADDR (.ovl.init)) + %u "
4597
0
             ": AT (LOADADDR (.ovl.init) + %u) {\n",
4598
0
           ovlynum, vma, lma) <= 0)
4599
0
      goto file_err;
4600
4601
0
    base = print_one_overlay_section (script, base, count, ovlynum,
4602
0
              ovly_map, ovly_sections, info);
4603
0
    if (base == (unsigned) -1)
4604
0
      goto file_err;
4605
4606
0
    if (fprintf (script, "  }\n") <= 0)
4607
0
      goto file_err;
4608
4609
0
    ovlynum++;
4610
0
  }
4611
4612
0
      if (fprintf (script, " . = ABSOLUTE (ADDR (.ovl.init)) + %u;\n",
4613
0
       1 << (htab->num_lines_log2 + htab->line_size_log2)) <= 0)
4614
0
  goto file_err;
4615
4616
0
      if (fprintf (script, "}\nINSERT AFTER .toe;\n") <= 0)
4617
0
  goto file_err;
4618
0
    }
4619
0
  else
4620
0
    {
4621
0
      if (fprintf (script, "SECTIONS\n{\n") <= 0)
4622
0
  goto file_err;
4623
4624
0
      if (fprintf (script,
4625
0
       " . = ALIGN (16);\n"
4626
0
       " .ovl.init : { *(.ovl.init) }\n"
4627
0
       " . = ABSOLUTE (ADDR (.ovl.init));\n") <= 0)
4628
0
  goto file_err;
4629
4630
0
      for (region = 1; region <= htab->params->num_lines; region++)
4631
0
  {
4632
0
    ovlynum = region;
4633
0
    base = 0;
4634
0
    while (base < count && ovly_map[base] < ovlynum)
4635
0
      base++;
4636
4637
0
    if (base == count)
4638
0
      break;
4639
4640
0
    if (region == 1)
4641
0
      {
4642
        /* We need to set lma since we are overlaying .ovl.init.  */
4643
0
        if (fprintf (script,
4644
0
         " OVERLAY : AT (ALIGN (LOADADDR (.ovl.init) + SIZEOF (.ovl.init), 16))\n {\n") <= 0)
4645
0
    goto file_err;
4646
0
      }
4647
0
    else
4648
0
      {
4649
0
        if (fprintf (script, " OVERLAY :\n {\n") <= 0)
4650
0
    goto file_err;
4651
0
      }
4652
4653
0
    while (base < count)
4654
0
      {
4655
0
        if (fprintf (script, "  .ovly%u {\n", ovlynum) <= 0)
4656
0
    goto file_err;
4657
4658
0
        base = print_one_overlay_section (script, base, count, ovlynum,
4659
0
            ovly_map, ovly_sections, info);
4660
0
        if (base == (unsigned) -1)
4661
0
    goto file_err;
4662
4663
0
        if (fprintf (script, "  }\n") <= 0)
4664
0
    goto file_err;
4665
4666
0
        ovlynum += htab->params->num_lines;
4667
0
        while (base < count && ovly_map[base] < ovlynum)
4668
0
    base++;
4669
0
      }
4670
4671
0
    if (fprintf (script, " }\n") <= 0)
4672
0
      goto file_err;
4673
0
  }
4674
4675
0
      if (fprintf (script, "}\nINSERT BEFORE .text;\n") <= 0)
4676
0
  goto file_err;
4677
0
    }
4678
4679
0
  free (ovly_map);
4680
0
  free (ovly_sections);
4681
4682
0
  if (fclose (script) != 0)
4683
0
    goto file_err;
4684
4685
0
  if (htab->params->auto_overlay & AUTO_RELINK)
4686
0
    (*htab->params->spu_elf_relink) ();
4687
4688
0
  xexit (0);
4689
4690
0
 file_err:
4691
0
  bfd_set_error (bfd_error_system_call);
4692
0
 err_exit:
4693
0
  info->callbacks->fatal (_("%P: auto overlay error: %E\n"));
4694
0
}
4695
4696
/* Provide an estimate of total stack required.  */
4697
4698
static bool
4699
spu_elf_stack_analysis (struct bfd_link_info *info)
4700
0
{
4701
0
  struct spu_link_hash_table *htab;
4702
0
  struct _sum_stack_param sum_stack_param;
4703
4704
0
  if (!discover_functions (info))
4705
0
    return false;
4706
4707
0
  if (!build_call_tree (info))
4708
0
    return false;
4709
4710
0
  htab = spu_hash_table (info);
4711
0
  if (htab->params->stack_analysis)
4712
0
    {
4713
0
      info->callbacks->info (_("Stack size for call graph root nodes.\n"));
4714
0
      info->callbacks->minfo (_("\nStack size for functions.  "
4715
0
        "Annotations: '*' max stack, 't' tail call\n"));
4716
0
    }
4717
4718
0
  sum_stack_param.emit_stack_syms = htab->params->emit_stack_syms;
4719
0
  sum_stack_param.overall_stack = 0;
4720
0
  if (!for_each_node (sum_stack, info, &sum_stack_param, true))
4721
0
    return false;
4722
4723
0
  if (htab->params->stack_analysis)
4724
0
    info->callbacks->info (_("Maximum stack required is 0x%v\n"),
4725
0
         (bfd_vma) sum_stack_param.overall_stack);
4726
0
  return true;
4727
0
}
4728
4729
/* Perform a final link.  */
4730
4731
static bool
4732
spu_elf_final_link (bfd *output_bfd, struct bfd_link_info *info)
4733
0
{
4734
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
4735
4736
0
  if (htab->params->auto_overlay)
4737
0
    spu_elf_auto_overlay (info);
4738
4739
0
  if ((htab->params->stack_analysis
4740
0
       || (htab->params->ovly_flavour == ovly_soft_icache
4741
0
     && htab->params->lrlive_analysis))
4742
0
      && !spu_elf_stack_analysis (info))
4743
0
    info->callbacks->einfo (_("%X%P: stack/lrlive analysis error: %E\n"));
4744
4745
0
  if (!spu_elf_build_stubs (info))
4746
0
    info->callbacks->fatal (_("%P: can not build overlay stubs: %E\n"));
4747
4748
0
  return _bfd_elf_final_link (output_bfd, info);
4749
0
}
4750
4751
/* Called when not normally emitting relocs, ie. !bfd_link_relocatable (info)
4752
   and !info->emitrelocations.  Returns a count of special relocs
4753
   that need to be emitted.  */
4754
4755
static unsigned int
4756
spu_elf_count_relocs (struct bfd_link_info *info, asection *sec)
4757
0
{
4758
0
  Elf_Internal_Rela *relocs;
4759
0
  unsigned int count = 0;
4760
4761
0
  relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL,
4762
0
              info->keep_memory);
4763
0
  if (relocs != NULL)
4764
0
    {
4765
0
      Elf_Internal_Rela *rel;
4766
0
      Elf_Internal_Rela *relend = relocs + sec->reloc_count;
4767
4768
0
      for (rel = relocs; rel < relend; rel++)
4769
0
  {
4770
0
    int r_type = ELF32_R_TYPE (rel->r_info);
4771
0
    if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
4772
0
      ++count;
4773
0
  }
4774
4775
0
      if (elf_section_data (sec)->relocs != relocs)
4776
0
  free (relocs);
4777
0
    }
4778
4779
0
  return count;
4780
0
}
4781
4782
/* Functions for adding fixup records to .fixup */
4783
4784
0
#define FIXUP_RECORD_SIZE 4
4785
4786
#define FIXUP_PUT(output_bfd,htab,index,addr) \
4787
0
    bfd_put_32 (output_bfd, addr, \
4788
0
          htab->sfixup->contents + FIXUP_RECORD_SIZE * (index))
4789
#define FIXUP_GET(output_bfd,htab,index) \
4790
0
    bfd_get_32 (output_bfd, \
4791
0
          htab->sfixup->contents + FIXUP_RECORD_SIZE * (index))
4792
4793
/* Store OFFSET in .fixup.  This assumes it will be called with an
4794
   increasing OFFSET.  When this OFFSET fits with the last base offset,
4795
   it just sets a bit, otherwise it adds a new fixup record.  */
4796
static void
4797
spu_elf_emit_fixup (bfd * output_bfd, struct bfd_link_info *info,
4798
        bfd_vma offset)
4799
0
{
4800
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
4801
0
  asection *sfixup = htab->sfixup;
4802
0
  bfd_vma qaddr = offset & ~(bfd_vma) 15;
4803
0
  bfd_vma bit = ((bfd_vma) 8) >> ((offset & 15) >> 2);
4804
0
  if (sfixup->reloc_count == 0)
4805
0
    {
4806
0
      FIXUP_PUT (output_bfd, htab, 0, qaddr | bit);
4807
0
      sfixup->reloc_count++;
4808
0
    }
4809
0
  else
4810
0
    {
4811
0
      bfd_vma base = FIXUP_GET (output_bfd, htab, sfixup->reloc_count - 1);
4812
0
      if (qaddr != (base & ~(bfd_vma) 15))
4813
0
  {
4814
0
    if ((sfixup->reloc_count + 1) * FIXUP_RECORD_SIZE > sfixup->size)
4815
0
      _bfd_error_handler (_("fatal error while creating .fixup"));
4816
0
    FIXUP_PUT (output_bfd, htab, sfixup->reloc_count, qaddr | bit);
4817
0
    sfixup->reloc_count++;
4818
0
  }
4819
0
      else
4820
0
  FIXUP_PUT (output_bfd, htab, sfixup->reloc_count - 1, base | bit);
4821
0
    }
4822
0
}
4823
4824
/* Apply RELOCS to CONTENTS of INPUT_SECTION from INPUT_BFD.  */
4825
4826
static int
4827
spu_elf_relocate_section (struct bfd_link_info *info,
4828
        bfd *input_bfd,
4829
        asection *input_section,
4830
        bfd_byte *contents,
4831
        Elf_Internal_Rela *relocs,
4832
        Elf_Internal_Sym *local_syms,
4833
        asection **local_sections)
4834
0
{
4835
0
  Elf_Internal_Shdr *symtab_hdr;
4836
0
  struct elf_link_hash_entry **sym_hashes;
4837
0
  Elf_Internal_Rela *rel, *relend;
4838
0
  struct spu_link_hash_table *htab;
4839
0
  asection *ea;
4840
0
  int ret = true;
4841
0
  bool emit_these_relocs = false;
4842
0
  bool is_ea_sym;
4843
0
  bool stubs;
4844
0
  unsigned int iovl = 0;
4845
4846
0
  htab = spu_hash_table (info);
4847
0
  stubs = (htab->stub_sec != NULL
4848
0
     && maybe_needs_stubs (input_section));
4849
0
  iovl = overlay_index (input_section);
4850
0
  ea = bfd_get_section_by_name (info->output_bfd, "._ea");
4851
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
4852
0
  sym_hashes = (struct elf_link_hash_entry **) (elf_sym_hashes (input_bfd));
4853
4854
0
  rel = relocs;
4855
0
  relend = relocs + input_section->reloc_count;
4856
0
  for (; rel < relend; rel++)
4857
0
    {
4858
0
      int r_type;
4859
0
      reloc_howto_type *howto;
4860
0
      unsigned int r_symndx;
4861
0
      Elf_Internal_Sym *sym;
4862
0
      asection *sec;
4863
0
      struct elf_link_hash_entry *h;
4864
0
      const char *sym_name;
4865
0
      bfd_vma relocation;
4866
0
      bfd_vma addend;
4867
0
      bfd_reloc_status_type r;
4868
0
      bool unresolved_reloc;
4869
0
      enum _stub_type stub_type;
4870
4871
0
      r_symndx = ELF32_R_SYM (rel->r_info);
4872
0
      r_type = ELF32_R_TYPE (rel->r_info);
4873
0
      howto = elf_howto_table + r_type;
4874
0
      unresolved_reloc = false;
4875
0
      h = NULL;
4876
0
      sym = NULL;
4877
0
      sec = NULL;
4878
0
      if (r_symndx < symtab_hdr->sh_info)
4879
0
  {
4880
0
    sym = local_syms + r_symndx;
4881
0
    sec = local_sections[r_symndx];
4882
0
    sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
4883
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
4884
0
            sym, &sec, rel);
4885
0
  }
4886
0
      else
4887
0
  {
4888
0
    if (sym_hashes == NULL)
4889
0
      return false;
4890
4891
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4892
4893
0
    if (info->wrap_hash != NULL
4894
0
        && (input_section->flags & SEC_DEBUGGING) != 0)
4895
0
      h = ((struct elf_link_hash_entry *)
4896
0
     unwrap_hash_lookup (info, input_bfd, &h->root));
4897
4898
0
    while (h->root.type == bfd_link_hash_indirect
4899
0
     || h->root.type == bfd_link_hash_warning)
4900
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4901
4902
0
    relocation = 0;
4903
0
    if (h->root.type == bfd_link_hash_defined
4904
0
        || h->root.type == bfd_link_hash_defweak)
4905
0
      {
4906
0
        sec = h->root.u.def.section;
4907
0
        if (sec == NULL
4908
0
      || sec->output_section == NULL)
4909
    /* Set a flag that will be cleared later if we find a
4910
       relocation value for this symbol.  output_section
4911
       is typically NULL for symbols satisfied by a shared
4912
       library.  */
4913
0
    unresolved_reloc = true;
4914
0
        else
4915
0
    relocation = (h->root.u.def.value
4916
0
            + sec->output_section->vma
4917
0
            + sec->output_offset);
4918
0
      }
4919
0
    else if (h->root.type == bfd_link_hash_undefweak)
4920
0
      ;
4921
0
    else if (info->unresolved_syms_in_objects == RM_IGNORE
4922
0
       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4923
0
      ;
4924
0
    else if (!bfd_link_relocatable (info)
4925
0
       && !(r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64))
4926
0
      {
4927
0
        bool err;
4928
4929
0
        err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
4930
0
         && !info->warn_unresolved_syms)
4931
0
    || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT;
4932
4933
0
        info->callbacks->undefined_symbol
4934
0
    (info, h->root.root.string, input_bfd,
4935
0
     input_section, rel->r_offset, err);
4936
0
      }
4937
0
    sym_name = h->root.root.string;
4938
0
  }
4939
4940
0
      if (sec != NULL && discarded_section (sec))
4941
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4942
0
           rel, 1, relend, R_SPU_NONE,
4943
0
           howto, 0, contents);
4944
4945
0
      if (bfd_link_relocatable (info))
4946
0
  continue;
4947
4948
      /* Change "a rt,ra,rb" to "ai rt,ra,0". */
4949
0
      if (r_type == R_SPU_ADD_PIC
4950
0
    && h != NULL
4951
0
    && !(h->def_regular || ELF_COMMON_DEF_P (h)))
4952
0
  {
4953
0
    bfd_byte *loc = contents + rel->r_offset;
4954
0
    loc[0] = 0x1c;
4955
0
    loc[1] = 0x00;
4956
0
    loc[2] &= 0x3f;
4957
0
  }
4958
4959
0
      is_ea_sym = (ea != NULL
4960
0
       && sec != NULL
4961
0
       && sec->output_section == ea);
4962
4963
      /* If this symbol is in an overlay area, we may need to relocate
4964
   to the overlay stub.  */
4965
0
      addend = rel->r_addend;
4966
0
      if (stubs
4967
0
    && !is_ea_sym
4968
0
    && (stub_type = needs_ovl_stub (h, sym, sec, input_section, rel,
4969
0
            contents, info)) != no_stub)
4970
0
  {
4971
0
    unsigned int ovl = 0;
4972
0
    struct got_entry *g, **head;
4973
4974
0
    if (stub_type != nonovl_stub)
4975
0
      ovl = iovl;
4976
4977
0
    if (h != NULL)
4978
0
      head = &h->got.glist;
4979
0
    else
4980
0
      head = elf_local_got_ents (input_bfd) + r_symndx;
4981
4982
0
    for (g = *head; g != NULL; g = g->next)
4983
0
      if (htab->params->ovly_flavour == ovly_soft_icache
4984
0
    ? (g->ovl == ovl
4985
0
       && g->br_addr == (rel->r_offset
4986
0
             + input_section->output_offset
4987
0
             + input_section->output_section->vma))
4988
0
    : g->addend == addend && (g->ovl == ovl || g->ovl == 0))
4989
0
        break;
4990
0
    if (g == NULL)
4991
0
      abort ();
4992
4993
0
    relocation = g->stub_addr;
4994
0
    addend = 0;
4995
0
  }
4996
0
      else
4997
0
  {
4998
    /* For soft icache, encode the overlay index into addresses.  */
4999
0
    if (htab->params->ovly_flavour == ovly_soft_icache
5000
0
        && (r_type == R_SPU_ADDR16_HI
5001
0
      || r_type == R_SPU_ADDR32 || r_type == R_SPU_REL32)
5002
0
        && !is_ea_sym)
5003
0
      {
5004
0
        unsigned int ovl = overlay_index (sec);
5005
0
        if (ovl != 0)
5006
0
    {
5007
0
      unsigned int set_id = ((ovl - 1) >> htab->num_lines_log2) + 1;
5008
0
      relocation += set_id << 18;
5009
0
    }
5010
0
      }
5011
0
  }
5012
5013
0
      if (htab->params->emit_fixups && !bfd_link_relocatable (info)
5014
0
    && (input_section->flags & SEC_ALLOC) != 0
5015
0
    && r_type == R_SPU_ADDR32)
5016
0
  {
5017
0
    bfd_vma offset;
5018
0
    offset = rel->r_offset + input_section->output_section->vma
5019
0
       + input_section->output_offset;
5020
0
    spu_elf_emit_fixup (info->output_bfd, info, offset);
5021
0
  }
5022
5023
0
      if (unresolved_reloc)
5024
0
  ;
5025
0
      else if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
5026
0
  {
5027
0
    if (is_ea_sym)
5028
0
      {
5029
        /* ._ea is a special section that isn't allocated in SPU
5030
     memory, but rather occupies space in PPU memory as
5031
     part of an embedded ELF image.  If this reloc is
5032
     against a symbol defined in ._ea, then transform the
5033
     reloc into an equivalent one without a symbol
5034
     relative to the start of the ELF image.  */
5035
0
        rel->r_addend += (relocation
5036
0
        - ea->vma
5037
0
        + elf_section_data (ea)->this_hdr.sh_offset);
5038
0
        rel->r_info = ELF32_R_INFO (0, r_type);
5039
0
      }
5040
0
    emit_these_relocs = true;
5041
0
    continue;
5042
0
  }
5043
0
      else if (is_ea_sym)
5044
0
  unresolved_reloc = true;
5045
5046
0
      if (unresolved_reloc
5047
0
    && _bfd_elf_section_offset (info->output_bfd, info, input_section,
5048
0
              rel->r_offset) != (bfd_vma) -1)
5049
0
  {
5050
0
    _bfd_error_handler
5051
      /* xgettext:c-format */
5052
0
      (_("%pB(%s+%#" PRIx64 "): "
5053
0
         "unresolvable %s relocation against symbol `%s'"),
5054
0
       input_bfd,
5055
0
       bfd_section_name (input_section),
5056
0
       (uint64_t) rel->r_offset,
5057
0
       howto->name,
5058
0
       sym_name);
5059
0
    ret = false;
5060
0
  }
5061
5062
0
      r = _bfd_final_link_relocate (howto,
5063
0
            input_bfd,
5064
0
            input_section,
5065
0
            contents,
5066
0
            rel->r_offset, relocation, addend);
5067
5068
0
      if (r != bfd_reloc_ok)
5069
0
  {
5070
0
    const char *msg = (const char *) 0;
5071
5072
0
    switch (r)
5073
0
      {
5074
0
      case bfd_reloc_overflow:
5075
0
        (*info->callbacks->reloc_overflow)
5076
0
    (info, (h ? &h->root : NULL), sym_name, howto->name,
5077
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5078
0
        break;
5079
5080
0
      case bfd_reloc_undefined:
5081
0
        (*info->callbacks->undefined_symbol)
5082
0
    (info, sym_name, input_bfd, input_section, rel->r_offset, true);
5083
0
        break;
5084
5085
0
      case bfd_reloc_outofrange:
5086
0
        msg = _("internal error: out of range error");
5087
0
        goto common_error;
5088
5089
0
      case bfd_reloc_notsupported:
5090
0
        msg = _("internal error: unsupported relocation error");
5091
0
        goto common_error;
5092
5093
0
      case bfd_reloc_dangerous:
5094
0
        msg = _("internal error: dangerous error");
5095
0
        goto common_error;
5096
5097
0
      default:
5098
0
        msg = _("internal error: unknown error");
5099
        /* fall through */
5100
5101
0
      common_error:
5102
0
        ret = false;
5103
0
        (*info->callbacks->warning) (info, msg, sym_name, input_bfd,
5104
0
             input_section, rel->r_offset);
5105
0
        break;
5106
0
      }
5107
0
  }
5108
0
    }
5109
5110
0
  if (ret
5111
0
      && emit_these_relocs
5112
0
      && !info->emitrelocations)
5113
0
    {
5114
0
      Elf_Internal_Rela *wrel;
5115
0
      Elf_Internal_Shdr *rel_hdr;
5116
5117
0
      wrel = rel = relocs;
5118
0
      relend = relocs + input_section->reloc_count;
5119
0
      for (; rel < relend; rel++)
5120
0
  {
5121
0
    int r_type;
5122
5123
0
    r_type = ELF32_R_TYPE (rel->r_info);
5124
0
    if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
5125
0
      *wrel++ = *rel;
5126
0
  }
5127
0
      input_section->reloc_count = wrel - relocs;
5128
      /* Backflips for _bfd_elf_link_output_relocs.  */
5129
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5130
0
      rel_hdr->sh_size = input_section->reloc_count * rel_hdr->sh_entsize;
5131
0
      ret = 2;
5132
0
    }
5133
5134
0
  return ret;
5135
0
}
5136
5137
static bool
5138
spu_elf_finish_dynamic_sections (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5139
         bfd_byte *buf ATTRIBUTE_UNUSED)
5140
0
{
5141
0
  return true;
5142
0
}
5143
5144
/* Adjust _SPUEAR_ syms to point at their overlay stubs.  */
5145
5146
static int
5147
spu_elf_output_symbol_hook (struct bfd_link_info *info,
5148
          const char *sym_name ATTRIBUTE_UNUSED,
5149
          Elf_Internal_Sym *sym,
5150
          asection *sym_sec ATTRIBUTE_UNUSED,
5151
          struct elf_link_hash_entry *h)
5152
0
{
5153
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
5154
5155
0
  if (!bfd_link_relocatable (info)
5156
0
      && htab->stub_sec != NULL
5157
0
      && h != NULL
5158
0
      && (h->root.type == bfd_link_hash_defined
5159
0
    || h->root.type == bfd_link_hash_defweak)
5160
0
      && h->def_regular
5161
0
      && startswith (h->root.root.string, "_SPUEAR_"))
5162
0
    {
5163
0
      struct got_entry *g;
5164
5165
0
      for (g = h->got.glist; g != NULL; g = g->next)
5166
0
  if (htab->params->ovly_flavour == ovly_soft_icache
5167
0
      ? g->br_addr == g->stub_addr
5168
0
      : g->addend == 0 && g->ovl == 0)
5169
0
    {
5170
0
      sym->st_shndx = (_bfd_elf_section_from_bfd_section
5171
0
           (htab->stub_sec[0]->output_section->owner,
5172
0
            htab->stub_sec[0]->output_section));
5173
0
      sym->st_value = g->stub_addr;
5174
0
      break;
5175
0
    }
5176
0
    }
5177
5178
0
  return 1;
5179
0
}
5180
5181
static int spu_plugin = 0;
5182
5183
void
5184
spu_elf_plugin (int val)
5185
0
{
5186
0
  spu_plugin = val;
5187
0
}
5188
5189
/* Set ELF header e_type for plugins.  */
5190
5191
static bool
5192
spu_elf_init_file_header (bfd *abfd, struct bfd_link_info *info)
5193
6
{
5194
6
  if (!_bfd_elf_init_file_header (abfd, info))
5195
0
    return false;
5196
5197
6
  if (spu_plugin)
5198
0
    {
5199
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5200
5201
0
      i_ehdrp->e_type = ET_DYN;
5202
0
    }
5203
6
  return true;
5204
6
}
5205
5206
/* We may add an extra PT_LOAD segment for .toe.  We also need extra
5207
   segments for overlays.  */
5208
5209
static int
5210
spu_elf_additional_program_headers (bfd *abfd, struct bfd_link_info *info)
5211
0
{
5212
0
  int extra = 0;
5213
0
  asection *sec;
5214
5215
0
  if (info != NULL)
5216
0
    {
5217
0
      struct spu_link_hash_table *htab = spu_hash_table (info);
5218
0
      extra = htab->num_overlays;
5219
0
    }
5220
5221
0
  if (extra)
5222
0
    ++extra;
5223
5224
0
  sec = bfd_get_section_by_name (abfd, ".toe");
5225
0
  if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
5226
0
    ++extra;
5227
5228
0
  return extra;
5229
0
}
5230
5231
/* Remove .toe section from other PT_LOAD segments and put it in
5232
   a segment of its own.  Put overlays in separate segments too.  */
5233
5234
static bool
5235
spu_elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
5236
3
{
5237
3
  asection *toe, *s;
5238
3
  struct elf_segment_map *m, *m_overlay;
5239
3
  struct elf_segment_map **p, **p_overlay, **first_load;
5240
3
  unsigned int i;
5241
5242
3
  if (info == NULL)
5243
3
    return true;
5244
5245
0
  toe = bfd_get_section_by_name (abfd, ".toe");
5246
0
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5247
0
    if (m->p_type == PT_LOAD && m->count > 1)
5248
0
      for (i = 0; i < m->count; i++)
5249
0
  if ((s = m->sections[i]) == toe
5250
0
      || spu_elf_section_data (s)->u.o.ovl_index != 0)
5251
0
    {
5252
0
      struct elf_segment_map *m2;
5253
0
      bfd_vma amt;
5254
5255
0
      if (i + 1 < m->count)
5256
0
        {
5257
0
    amt = sizeof (struct elf_segment_map);
5258
0
    amt += (m->count - (i + 2)) * sizeof (m->sections[0]);
5259
0
    m2 = bfd_zalloc (abfd, amt);
5260
0
    if (m2 == NULL)
5261
0
      return false;
5262
0
    m2->count = m->count - (i + 1);
5263
0
    memcpy (m2->sections, m->sections + i + 1,
5264
0
      m2->count * sizeof (m->sections[0]));
5265
0
    m2->p_type = PT_LOAD;
5266
0
    m2->next = m->next;
5267
0
    m->next = m2;
5268
0
        }
5269
0
      m->count = 1;
5270
0
      if (i != 0)
5271
0
        {
5272
0
    m->count = i;
5273
0
    amt = sizeof (struct elf_segment_map);
5274
0
    m2 = bfd_zalloc (abfd, amt);
5275
0
    if (m2 == NULL)
5276
0
      return false;
5277
0
    m2->p_type = PT_LOAD;
5278
0
    m2->count = 1;
5279
0
    m2->sections[0] = s;
5280
0
    m2->next = m->next;
5281
0
    m->next = m2;
5282
0
        }
5283
0
      break;
5284
0
    }
5285
5286
5287
  /* Some SPU ELF loaders ignore the PF_OVERLAY flag and just load all
5288
     PT_LOAD segments.  This can cause the .ovl.init section to be
5289
     overwritten with the contents of some overlay segment.  To work
5290
     around this issue, we ensure that all PF_OVERLAY segments are
5291
     sorted first amongst the program headers; this ensures that even
5292
     with a broken loader, the .ovl.init section (which is not marked
5293
     as PF_OVERLAY) will be placed into SPU local store on startup.  */
5294
5295
  /* Move all overlay segments onto a separate list.  */
5296
0
  p = &elf_seg_map (abfd);
5297
0
  p_overlay = &m_overlay;
5298
0
  m_overlay = NULL;
5299
0
  first_load = NULL;
5300
0
  while (*p != NULL)
5301
0
    {
5302
0
      if ((*p)->p_type == PT_LOAD)
5303
0
  {
5304
0
    if (!first_load)
5305
0
      first_load = p;
5306
0
    if ((*p)->count == 1
5307
0
        && spu_elf_section_data ((*p)->sections[0])->u.o.ovl_index != 0)
5308
0
      {
5309
0
        m = *p;
5310
0
        m->no_sort_lma = 1;
5311
0
        *p = m->next;
5312
0
        *p_overlay = m;
5313
0
        p_overlay = &m->next;
5314
0
        continue;
5315
0
      }
5316
0
  }
5317
0
      p = &((*p)->next);
5318
0
    }
5319
5320
  /* Re-insert overlay segments at the head of the segment map.  */
5321
0
  if (m_overlay != NULL)
5322
0
    {
5323
0
      p = first_load;
5324
0
      if (*p != NULL && (*p)->p_type == PT_LOAD && (*p)->includes_filehdr)
5325
  /* It doesn't really make sense for someone to include the ELF
5326
     file header into an spu image, but if they do the code that
5327
     assigns p_offset needs to see the segment containing the
5328
     header first.  */
5329
0
  p = &(*p)->next;
5330
0
      *p_overlay = *p;
5331
0
      *p = m_overlay;
5332
0
    }
5333
5334
0
  return true;
5335
0
}
5336
5337
/* Tweak the section type of .note.spu_name.  */
5338
5339
static bool
5340
spu_elf_fake_sections (bfd *obfd ATTRIBUTE_UNUSED,
5341
           Elf_Internal_Shdr *hdr,
5342
           asection *sec)
5343
6
{
5344
6
  if (strcmp (sec->name, SPU_PTNOTE_SPUNAME) == 0)
5345
0
    {
5346
0
      hdr->sh_type = SHT_NOTE;
5347
0
      hdr->sh_addralign = 16;
5348
0
    }
5349
6
  return true;
5350
6
}
5351
5352
/* .note.spu_name is read by the PPU from the object, and possibly
5353
   needs the file offset to be aligned to 16 bytes.  We used to simply
5354
   call bfd_set_section_alignment to set the alignment to 16, but if
5355
   left like that it triggers a readelf warning.  Now for layout we
5356
   tweak the alignment to 16 in spu_elf_fake_sections, and restore it
5357
   to 4 here.  */
5358
5359
static bool
5360
spu_elf_final_write_processing (bfd *abfd)
5361
6
{
5362
6
  asection *sec = bfd_get_section_by_name (abfd, SPU_PTNOTE_SPUNAME);
5363
6
  if (sec != NULL)
5364
0
    {
5365
0
      struct bfd_elf_section_data *esd = elf_section_data (sec);
5366
0
      esd->this_hdr.sh_addralign = 4;
5367
0
    }
5368
6
  return _bfd_elf_final_write_processing (abfd);
5369
6
}
5370
5371
/* Tweak phdrs before writing them out.  */
5372
5373
static bool
5374
spu_elf_modify_headers (bfd *abfd, struct bfd_link_info *info)
5375
6
{
5376
6
  if (info != NULL)
5377
0
    {
5378
0
      elf_backend_data *bed;
5379
0
      struct elf_obj_tdata *tdata;
5380
0
      Elf_Internal_Phdr *phdr, *last;
5381
0
      struct spu_link_hash_table *htab;
5382
0
      unsigned int count;
5383
0
      unsigned int i;
5384
5385
0
      bed = get_elf_backend_data (abfd);
5386
0
      tdata = elf_tdata (abfd);
5387
0
      phdr = tdata->phdr;
5388
0
      count = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5389
0
      htab = spu_hash_table (info);
5390
0
      if (htab->num_overlays != 0)
5391
0
  {
5392
0
    struct elf_segment_map *m;
5393
0
    unsigned int o;
5394
5395
0
    for (i = 0, m = elf_seg_map (abfd); m; ++i, m = m->next)
5396
0
      if (m->count != 0
5397
0
    && ((o = spu_elf_section_data (m->sections[0])->u.o.ovl_index)
5398
0
        != 0))
5399
0
        {
5400
    /* Mark this as an overlay header.  */
5401
0
    phdr[i].p_flags |= PF_OVERLAY;
5402
5403
0
    if (htab->ovtab != NULL && htab->ovtab->size != 0
5404
0
        && htab->params->ovly_flavour != ovly_soft_icache)
5405
0
      {
5406
0
        bfd_byte *p = htab->ovtab->contents;
5407
0
        unsigned int off = o * 16 + 8;
5408
5409
        /* Write file_off into _ovly_table.  */
5410
0
        bfd_put_32 (htab->ovtab->owner, phdr[i].p_offset, p + off);
5411
0
      }
5412
0
        }
5413
    /* Soft-icache has its file offset put in .ovl.init.  */
5414
0
    if (htab->init != NULL && htab->init->size != 0)
5415
0
      {
5416
0
        bfd_vma val
5417
0
    = elf_section_data (htab->ovl_sec[0])->this_hdr.sh_offset;
5418
5419
0
        bfd_put_32 (htab->init->owner, val, htab->init->contents + 4);
5420
0
      }
5421
0
  }
5422
5423
      /* Round up p_filesz and p_memsz of PT_LOAD segments to multiples
5424
   of 16.  This should always be possible when using the standard
5425
   linker scripts, but don't create overlapping segments if
5426
   someone is playing games with linker scripts.  */
5427
0
      last = NULL;
5428
0
      for (i = count; i-- != 0; )
5429
0
  if (phdr[i].p_type == PT_LOAD)
5430
0
    {
5431
0
      unsigned adjust;
5432
5433
0
      adjust = -phdr[i].p_filesz & 15;
5434
0
      if (adjust != 0
5435
0
    && last != NULL
5436
0
    && (phdr[i].p_offset + phdr[i].p_filesz
5437
0
        > last->p_offset - adjust))
5438
0
        break;
5439
5440
0
      adjust = -phdr[i].p_memsz & 15;
5441
0
      if (adjust != 0
5442
0
    && last != NULL
5443
0
    && phdr[i].p_filesz != 0
5444
0
    && phdr[i].p_vaddr + phdr[i].p_memsz > last->p_vaddr - adjust
5445
0
    && phdr[i].p_vaddr + phdr[i].p_memsz <= last->p_vaddr)
5446
0
        break;
5447
5448
0
      if (phdr[i].p_filesz != 0)
5449
0
        last = &phdr[i];
5450
0
    }
5451
5452
0
      if (i == (unsigned int) -1)
5453
0
  for (i = count; i-- != 0; )
5454
0
    if (phdr[i].p_type == PT_LOAD)
5455
0
      {
5456
0
        unsigned adjust;
5457
5458
0
        adjust = -phdr[i].p_filesz & 15;
5459
0
        phdr[i].p_filesz += adjust;
5460
5461
0
        adjust = -phdr[i].p_memsz & 15;
5462
0
        phdr[i].p_memsz += adjust;
5463
0
      }
5464
0
    }
5465
5466
6
  return _bfd_elf_modify_headers (abfd, info);
5467
6
}
5468
5469
bool
5470
spu_elf_size_sections (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
5471
0
{
5472
0
  struct spu_link_hash_table *htab = spu_hash_table (info);
5473
0
  if (htab->params->emit_fixups)
5474
0
    {
5475
0
      asection *sfixup = htab->sfixup;
5476
0
      int fixup_count = 0;
5477
0
      bfd *ibfd;
5478
0
      size_t size;
5479
5480
0
      for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5481
0
  {
5482
0
    asection *isec;
5483
5484
0
    if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5485
0
      continue;
5486
5487
    /* Walk over each section attached to the input bfd.  */
5488
0
    for (isec = ibfd->sections; isec != NULL; isec = isec->next)
5489
0
      {
5490
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
5491
0
        bfd_vma base_end;
5492
5493
        /* If there aren't any relocs, then there's nothing more
5494
     to do.  */
5495
0
        if ((isec->flags & SEC_ALLOC) == 0
5496
0
      || (isec->flags & SEC_RELOC) == 0
5497
0
      || isec->reloc_count == 0)
5498
0
    continue;
5499
5500
        /* Get the relocs.  */
5501
0
        internal_relocs =
5502
0
    _bfd_elf_link_read_relocs (ibfd, isec, NULL, NULL,
5503
0
             info->keep_memory);
5504
0
        if (internal_relocs == NULL)
5505
0
    return false;
5506
5507
        /* 1 quadword can contain up to 4 R_SPU_ADDR32
5508
     relocations.  They are stored in a single word by
5509
     saving the upper 28 bits of the address and setting the
5510
     lower 4 bits to a bit mask of the words that have the
5511
     relocation.  BASE_END keeps track of the next quadword. */
5512
0
        irela = internal_relocs;
5513
0
        irelaend = irela + isec->reloc_count;
5514
0
        base_end = 0;
5515
0
        for (; irela < irelaend; irela++)
5516
0
    if (ELF32_R_TYPE (irela->r_info) == R_SPU_ADDR32
5517
0
        && irela->r_offset >= base_end)
5518
0
      {
5519
0
        base_end = (irela->r_offset & ~(bfd_vma) 15) + 16;
5520
0
        fixup_count++;
5521
0
      }
5522
0
      }
5523
0
  }
5524
5525
      /* We always have a NULL fixup as a sentinel */
5526
0
      size = (fixup_count + 1) * FIXUP_RECORD_SIZE;
5527
0
      if (!bfd_set_section_size (sfixup, size))
5528
0
  return false;
5529
0
      sfixup->contents = (bfd_byte *) bfd_zalloc (info->input_bfds, size);
5530
0
      if (sfixup->contents == NULL)
5531
0
  return false;
5532
0
      sfixup->alloced = 1;
5533
0
    }
5534
0
  return true;
5535
0
}
5536
5537
#define TARGET_BIG_SYM    spu_elf32_vec
5538
#define TARGET_BIG_NAME   "elf32-spu"
5539
#define ELF_ARCH    bfd_arch_spu
5540
#define ELF_TARGET_ID   SPU_ELF_DATA
5541
#define ELF_MACHINE_CODE  EM_SPU
5542
/* This matches the alignment need for DMA.  */
5543
#define ELF_MAXPAGESIZE   0x80
5544
#define elf_backend_rela_normal   1
5545
#define elf_backend_can_gc_sections 1
5546
5547
#define bfd_elf32_bfd_reloc_type_lookup   spu_elf_reloc_type_lookup
5548
#define bfd_elf32_bfd_reloc_name_lookup   spu_elf_reloc_name_lookup
5549
#define elf_info_to_howto     spu_elf_info_to_howto
5550
#define elf_backend_count_relocs    spu_elf_count_relocs
5551
#define elf_backend_relocate_section    spu_elf_relocate_section
5552
#define elf_backend_finish_dynamic_sections spu_elf_finish_dynamic_sections
5553
#define elf_backend_symbol_processing   spu_elf_backend_symbol_processing
5554
#define elf_backend_link_output_symbol_hook spu_elf_output_symbol_hook
5555
#define elf_backend_object_p      spu_elf_object_p
5556
#define bfd_elf32_new_section_hook    spu_elf_new_section_hook
5557
#define bfd_elf32_bfd_link_hash_table_create  spu_elf_link_hash_table_create
5558
5559
#define elf_backend_additional_program_headers  spu_elf_additional_program_headers
5560
#define elf_backend_modify_segment_map    spu_elf_modify_segment_map
5561
#define elf_backend_modify_headers    spu_elf_modify_headers
5562
#define elf_backend_init_file_header    spu_elf_init_file_header
5563
#define elf_backend_fake_sections   spu_elf_fake_sections
5564
#define elf_backend_final_write_processing  spu_elf_final_write_processing
5565
#define elf_backend_special_sections    spu_elf_special_sections
5566
#define bfd_elf32_bfd_final_link    spu_elf_final_link
5567
5568
#include "elf32-target.h"