* elf32-spu.c (struct got_entry): Add "addend" field.
[deliverable/binutils-gdb.git] / bfd / elf32-spu.c
CommitLineData
e9f53129
AM
1/* SPU specific support for 32-bit ELF
2
d16c7321 3 Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
e9f53129
AM
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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
e9f53129
AM
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
e9f53129 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
e9f53129
AM
23#include "bfdlink.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/spu.h"
27#include "elf32-spu.h"
28
29/* We use RELA style relocs. Don't define USE_REL. */
30
31static bfd_reloc_status_type spu_elf_rel9 (bfd *, arelent *, asymbol *,
32 void *, asection *,
33 bfd *, char **);
34
35/* Values of type 'enum elf_spu_reloc_type' are used to index this
36 array, so it must be declared in the order of that type. */
37
38static reloc_howto_type elf_howto_table[] = {
39 HOWTO (R_SPU_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
40 bfd_elf_generic_reloc, "SPU_NONE",
41 FALSE, 0, 0x00000000, FALSE),
42 HOWTO (R_SPU_ADDR10, 4, 2, 10, FALSE, 14, complain_overflow_bitfield,
43 bfd_elf_generic_reloc, "SPU_ADDR10",
44 FALSE, 0, 0x00ffc000, FALSE),
45 HOWTO (R_SPU_ADDR16, 2, 2, 16, FALSE, 7, complain_overflow_bitfield,
46 bfd_elf_generic_reloc, "SPU_ADDR16",
47 FALSE, 0, 0x007fff80, FALSE),
48 HOWTO (R_SPU_ADDR16_HI, 16, 2, 16, FALSE, 7, complain_overflow_bitfield,
49 bfd_elf_generic_reloc, "SPU_ADDR16_HI",
50 FALSE, 0, 0x007fff80, FALSE),
51 HOWTO (R_SPU_ADDR16_LO, 0, 2, 16, FALSE, 7, complain_overflow_dont,
52 bfd_elf_generic_reloc, "SPU_ADDR16_LO",
53 FALSE, 0, 0x007fff80, FALSE),
54 HOWTO (R_SPU_ADDR18, 0, 2, 18, FALSE, 7, complain_overflow_bitfield,
55 bfd_elf_generic_reloc, "SPU_ADDR18",
56 FALSE, 0, 0x01ffff80, FALSE),
b427ea91 57 HOWTO (R_SPU_ADDR32, 0, 2, 32, FALSE, 0, complain_overflow_dont,
e9f53129
AM
58 bfd_elf_generic_reloc, "SPU_ADDR32",
59 FALSE, 0, 0xffffffff, FALSE),
60 HOWTO (R_SPU_REL16, 2, 2, 16, TRUE, 7, complain_overflow_bitfield,
61 bfd_elf_generic_reloc, "SPU_REL16",
62 FALSE, 0, 0x007fff80, TRUE),
63 HOWTO (R_SPU_ADDR7, 0, 2, 7, FALSE, 14, complain_overflow_dont,
64 bfd_elf_generic_reloc, "SPU_ADDR7",
65 FALSE, 0, 0x001fc000, FALSE),
66 HOWTO (R_SPU_REL9, 2, 2, 9, TRUE, 0, complain_overflow_signed,
67 spu_elf_rel9, "SPU_REL9",
68 FALSE, 0, 0x0180007f, TRUE),
69 HOWTO (R_SPU_REL9I, 2, 2, 9, TRUE, 0, complain_overflow_signed,
70 spu_elf_rel9, "SPU_REL9I",
71 FALSE, 0, 0x0000c07f, TRUE),
72 HOWTO (R_SPU_ADDR10I, 0, 2, 10, FALSE, 14, complain_overflow_signed,
73 bfd_elf_generic_reloc, "SPU_ADDR10I",
74 FALSE, 0, 0x00ffc000, FALSE),
75 HOWTO (R_SPU_ADDR16I, 0, 2, 16, FALSE, 7, complain_overflow_signed,
76 bfd_elf_generic_reloc, "SPU_ADDR16I",
77 FALSE, 0, 0x007fff80, FALSE),
b427ea91 78 HOWTO (R_SPU_REL32, 0, 2, 32, TRUE, 0, complain_overflow_dont,
e9f53129
AM
79 bfd_elf_generic_reloc, "SPU_REL32",
80 FALSE, 0, 0xffffffff, TRUE),
4f4416b5
AM
81 HOWTO (R_SPU_ADDR16X, 0, 2, 16, FALSE, 7, complain_overflow_bitfield,
82 bfd_elf_generic_reloc, "SPU_ADDR16X",
83 FALSE, 0, 0x007fff80, FALSE),
b427ea91 84 HOWTO (R_SPU_PPU32, 0, 2, 32, FALSE, 0, complain_overflow_dont,
ece5ef60
AM
85 bfd_elf_generic_reloc, "SPU_PPU32",
86 FALSE, 0, 0xffffffff, FALSE),
b427ea91 87 HOWTO (R_SPU_PPU64, 0, 4, 64, FALSE, 0, complain_overflow_dont,
ece5ef60
AM
88 bfd_elf_generic_reloc, "SPU_PPU64",
89 FALSE, 0, -1, FALSE),
e9f53129
AM
90};
91
92static struct bfd_elf_special_section const spu_elf_special_sections[] = {
93 { ".toe", 4, 0, SHT_NOBITS, SHF_ALLOC },
94 { NULL, 0, 0, 0, 0 }
95};
96
97static enum elf_spu_reloc_type
98spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code)
99{
100 switch (code)
101 {
102 default:
103 return R_SPU_NONE;
104 case BFD_RELOC_SPU_IMM10W:
105 return R_SPU_ADDR10;
106 case BFD_RELOC_SPU_IMM16W:
107 return R_SPU_ADDR16;
108 case BFD_RELOC_SPU_LO16:
109 return R_SPU_ADDR16_LO;
110 case BFD_RELOC_SPU_HI16:
111 return R_SPU_ADDR16_HI;
112 case BFD_RELOC_SPU_IMM18:
113 return R_SPU_ADDR18;
114 case BFD_RELOC_SPU_PCREL16:
115 return R_SPU_REL16;
116 case BFD_RELOC_SPU_IMM7:
117 return R_SPU_ADDR7;
118 case BFD_RELOC_SPU_IMM8:
119 return R_SPU_NONE;
120 case BFD_RELOC_SPU_PCREL9a:
121 return R_SPU_REL9;
122 case BFD_RELOC_SPU_PCREL9b:
123 return R_SPU_REL9I;
124 case BFD_RELOC_SPU_IMM10:
125 return R_SPU_ADDR10I;
126 case BFD_RELOC_SPU_IMM16:
127 return R_SPU_ADDR16I;
128 case BFD_RELOC_32:
129 return R_SPU_ADDR32;
130 case BFD_RELOC_32_PCREL:
131 return R_SPU_REL32;
ece5ef60
AM
132 case BFD_RELOC_SPU_PPU32:
133 return R_SPU_PPU32;
134 case BFD_RELOC_SPU_PPU64:
135 return R_SPU_PPU64;
e9f53129
AM
136 }
137}
138
139static void
140spu_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
141 arelent *cache_ptr,
142 Elf_Internal_Rela *dst)
143{
144 enum elf_spu_reloc_type r_type;
145
146 r_type = (enum elf_spu_reloc_type) ELF32_R_TYPE (dst->r_info);
147 BFD_ASSERT (r_type < R_SPU_max);
148 cache_ptr->howto = &elf_howto_table[(int) r_type];
149}
150
151static reloc_howto_type *
152spu_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
153 bfd_reloc_code_real_type code)
154{
b16f296e
AM
155 enum elf_spu_reloc_type r_type = spu_elf_bfd_to_reloc_type (code);
156
157 if (r_type == R_SPU_NONE)
158 return NULL;
159
160 return elf_howto_table + r_type;
e9f53129
AM
161}
162
157090f7
AM
163static reloc_howto_type *
164spu_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
165 const char *r_name)
166{
167 unsigned int i;
168
169 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
170 if (elf_howto_table[i].name != NULL
171 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
172 return &elf_howto_table[i];
173
174 return NULL;
175}
176
e9f53129
AM
177/* Apply R_SPU_REL9 and R_SPU_REL9I relocs. */
178
179static bfd_reloc_status_type
180spu_elf_rel9 (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
181 void *data, asection *input_section,
182 bfd *output_bfd, char **error_message)
183{
184 bfd_size_type octets;
185 bfd_vma val;
186 long insn;
187
188 /* If this is a relocatable link (output_bfd test tells us), just
189 call the generic function. Any adjustment will be done at final
190 link time. */
191 if (output_bfd != NULL)
192 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
193 input_section, output_bfd, error_message);
194
195 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
196 return bfd_reloc_outofrange;
197 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
198
199 /* Get symbol value. */
200 val = 0;
201 if (!bfd_is_com_section (symbol->section))
202 val = symbol->value;
203 if (symbol->section->output_section)
204 val += symbol->section->output_section->vma;
205
206 val += reloc_entry->addend;
207
208 /* Make it pc-relative. */
209 val -= input_section->output_section->vma + input_section->output_offset;
210
211 val >>= 2;
212 if (val + 256 >= 512)
213 return bfd_reloc_overflow;
214
215 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
216
217 /* Move two high bits of value to REL9I and REL9 position.
218 The mask will take care of selecting the right field. */
219 val = (val & 0x7f) | ((val & 0x180) << 7) | ((val & 0x180) << 16);
220 insn &= ~reloc_entry->howto->dst_mask;
221 insn |= val & reloc_entry->howto->dst_mask;
222 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
223 return bfd_reloc_ok;
224}
225
226static bfd_boolean
227spu_elf_new_section_hook (bfd *abfd, asection *sec)
228{
229 if (!sec->used_by_bfd)
230 {
231 struct _spu_elf_section_data *sdata;
232
233 sdata = bfd_zalloc (abfd, sizeof (*sdata));
234 if (sdata == NULL)
235 return FALSE;
236 sec->used_by_bfd = sdata;
237 }
238
239 return _bfd_elf_new_section_hook (abfd, sec);
240}
241
242/* Specially mark defined symbols named _EAR_* with BSF_KEEP so that
243 strip --strip-unneeded will not remove them. */
244
245static void
246spu_elf_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
247{
248 if (sym->name != NULL
249 && sym->section != bfd_abs_section_ptr
250 && strncmp (sym->name, "_EAR_", 5) == 0)
251 sym->flags |= BSF_KEEP;
252}
253
254/* SPU ELF linker hash table. */
255
256struct spu_link_hash_table
257{
258 struct elf_link_hash_table elf;
259
e9f53129 260 /* Shortcuts to overlay sections. */
e9f53129 261 asection *ovtab;
47f6dab9
AM
262 asection *toe;
263 asection **ovl_sec;
264
265 /* Count of stubs in each overlay section. */
266 unsigned int *stub_count;
267
268 /* The stub section for each overlay section. */
269 asection **stub_sec;
e9f53129
AM
270
271 struct elf_link_hash_entry *ovly_load;
47f6dab9 272 struct elf_link_hash_entry *ovly_return;
2cb5950e 273 unsigned long ovly_load_r_symndx;
e9f53129 274
e9f53129
AM
275 /* Number of overlay buffers. */
276 unsigned int num_buf;
277
278 /* Total number of overlays. */
279 unsigned int num_overlays;
280
281 /* Set if we should emit symbols for stubs. */
282 unsigned int emit_stub_syms:1;
283
284 /* Set if we want stubs on calls out of overlay regions to
285 non-overlay regions. */
286 unsigned int non_overlay_stubs : 1;
287
288 /* Set on error. */
47f6dab9 289 unsigned int stub_err : 1;
49fa1e15
AM
290
291 /* Set if stack size analysis should be done. */
292 unsigned int stack_analysis : 1;
293
294 /* Set if __stack_* syms will be emitted. */
295 unsigned int emit_stack_syms : 1;
e9f53129
AM
296};
297
47f6dab9 298/* Hijack the generic got fields for overlay stub accounting. */
e9f53129 299
47f6dab9 300struct got_entry
e9f53129 301{
47f6dab9
AM
302 struct got_entry *next;
303 unsigned int ovl;
4a628337 304 bfd_vma addend;
47f6dab9 305 bfd_vma stub_addr;
e9f53129
AM
306};
307
47f6dab9
AM
308#define spu_hash_table(p) \
309 ((struct spu_link_hash_table *) ((p)->hash))
e9f53129
AM
310
311/* Create a spu ELF linker hash table. */
312
313static struct bfd_link_hash_table *
314spu_elf_link_hash_table_create (bfd *abfd)
315{
316 struct spu_link_hash_table *htab;
317
318 htab = bfd_malloc (sizeof (*htab));
319 if (htab == NULL)
320 return NULL;
321
322 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd,
323 _bfd_elf_link_hash_newfunc,
324 sizeof (struct elf_link_hash_entry)))
325 {
326 free (htab);
327 return NULL;
328 }
329
47f6dab9
AM
330 memset (&htab->ovtab, 0,
331 sizeof (*htab) - offsetof (struct spu_link_hash_table, ovtab));
e9f53129 332
47f6dab9
AM
333 htab->elf.init_got_refcount.refcount = 0;
334 htab->elf.init_got_refcount.glist = NULL;
335 htab->elf.init_got_offset.offset = 0;
336 htab->elf.init_got_offset.glist = NULL;
e9f53129
AM
337 return &htab->elf.root;
338}
339
e9f53129
AM
340/* Find the symbol for the given R_SYMNDX in IBFD and set *HP and *SYMP
341 to (hash, NULL) for global symbols, and (NULL, sym) for locals. Set
342 *SYMSECP to the symbol's section. *LOCSYMSP caches local syms. */
343
344static bfd_boolean
345get_sym_h (struct elf_link_hash_entry **hp,
346 Elf_Internal_Sym **symp,
347 asection **symsecp,
348 Elf_Internal_Sym **locsymsp,
349 unsigned long r_symndx,
350 bfd *ibfd)
351{
352 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
353
354 if (r_symndx >= symtab_hdr->sh_info)
355 {
356 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
357 struct elf_link_hash_entry *h;
358
359 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
360 while (h->root.type == bfd_link_hash_indirect
361 || h->root.type == bfd_link_hash_warning)
362 h = (struct elf_link_hash_entry *) h->root.u.i.link;
363
364 if (hp != NULL)
365 *hp = h;
366
367 if (symp != NULL)
368 *symp = NULL;
369
370 if (symsecp != NULL)
371 {
372 asection *symsec = NULL;
373 if (h->root.type == bfd_link_hash_defined
374 || h->root.type == bfd_link_hash_defweak)
375 symsec = h->root.u.def.section;
376 *symsecp = symsec;
377 }
378 }
379 else
380 {
381 Elf_Internal_Sym *sym;
382 Elf_Internal_Sym *locsyms = *locsymsp;
383
384 if (locsyms == NULL)
385 {
386 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
387 if (locsyms == NULL)
49fa1e15
AM
388 {
389 size_t symcount = symtab_hdr->sh_info;
390
391 /* If we are reading symbols into the contents, then
392 read the global syms too. This is done to cache
393 syms for later stack analysis. */
394 if ((unsigned char **) locsymsp == &symtab_hdr->contents)
395 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
396 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, 0,
397 NULL, NULL, NULL);
398 }
e9f53129
AM
399 if (locsyms == NULL)
400 return FALSE;
401 *locsymsp = locsyms;
402 }
403 sym = locsyms + r_symndx;
404
405 if (hp != NULL)
406 *hp = NULL;
407
408 if (symp != NULL)
409 *symp = sym;
410
411 if (symsecp != NULL)
412 {
413 asection *symsec = NULL;
414 if ((sym->st_shndx != SHN_UNDEF
415 && sym->st_shndx < SHN_LORESERVE)
416 || sym->st_shndx > SHN_HIRESERVE)
417 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
418 *symsecp = symsec;
419 }
420 }
49fa1e15 421
e9f53129
AM
422 return TRUE;
423}
424
e9f53129
AM
425/* Create the note section if not already present. This is done early so
426 that the linker maps the sections to the right place in the output. */
427
428bfd_boolean
49fa1e15
AM
429spu_elf_create_sections (bfd *output_bfd,
430 struct bfd_link_info *info,
431 int stack_analysis,
432 int emit_stack_syms)
e9f53129
AM
433{
434 bfd *ibfd;
49fa1e15
AM
435 struct spu_link_hash_table *htab = spu_hash_table (info);
436
437 /* Stash some options away where we can get at them later. */
438 htab->stack_analysis = stack_analysis;
439 htab->emit_stack_syms = emit_stack_syms;
e9f53129 440
58eb693e 441 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
e9f53129
AM
442 if (bfd_get_section_by_name (ibfd, SPU_PTNOTE_SPUNAME) != NULL)
443 break;
444
445 if (ibfd == NULL)
446 {
447 /* Make SPU_PTNOTE_SPUNAME section. */
448 asection *s;
449 size_t name_len;
450 size_t size;
451 bfd_byte *data;
452 flagword flags;
453
454 ibfd = info->input_bfds;
455 flags = SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
456 s = bfd_make_section_anyway_with_flags (ibfd, SPU_PTNOTE_SPUNAME, flags);
457 if (s == NULL
458 || !bfd_set_section_alignment (ibfd, s, 4))
459 return FALSE;
460
461 name_len = strlen (bfd_get_filename (output_bfd)) + 1;
462 size = 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4);
463 size += (name_len + 3) & -4;
464
465 if (!bfd_set_section_size (ibfd, s, size))
466 return FALSE;
467
468 data = bfd_zalloc (ibfd, size);
469 if (data == NULL)
470 return FALSE;
471
472 bfd_put_32 (ibfd, sizeof (SPU_PLUGIN_NAME), data + 0);
473 bfd_put_32 (ibfd, name_len, data + 4);
474 bfd_put_32 (ibfd, 1, data + 8);
475 memcpy (data + 12, SPU_PLUGIN_NAME, sizeof (SPU_PLUGIN_NAME));
476 memcpy (data + 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4),
477 bfd_get_filename (output_bfd), name_len);
478 s->contents = data;
479 }
480
481 return TRUE;
482}
483
e9f53129
AM
484/* qsort predicate to sort sections by vma. */
485
486static int
487sort_sections (const void *a, const void *b)
488{
489 const asection *const *s1 = a;
490 const asection *const *s2 = b;
491 bfd_signed_vma delta = (*s1)->vma - (*s2)->vma;
492
493 if (delta != 0)
494 return delta < 0 ? -1 : 1;
495
496 return (*s1)->index - (*s2)->index;
497}
498
499/* Identify overlays in the output bfd, and number them. */
500
501bfd_boolean
502spu_elf_find_overlays (bfd *output_bfd, struct bfd_link_info *info)
503{
504 struct spu_link_hash_table *htab = spu_hash_table (info);
505 asection **alloc_sec;
506 unsigned int i, n, ovl_index, num_buf;
507 asection *s;
508 bfd_vma ovl_end;
509
510 if (output_bfd->section_count < 2)
511 return FALSE;
512
513 alloc_sec = bfd_malloc (output_bfd->section_count * sizeof (*alloc_sec));
514 if (alloc_sec == NULL)
515 return FALSE;
516
517 /* Pick out all the alloced sections. */
518 for (n = 0, s = output_bfd->sections; s != NULL; s = s->next)
519 if ((s->flags & SEC_ALLOC) != 0
520 && (s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != SEC_THREAD_LOCAL
521 && s->size != 0)
522 alloc_sec[n++] = s;
523
524 if (n == 0)
525 {
526 free (alloc_sec);
527 return FALSE;
528 }
529
530 /* Sort them by vma. */
531 qsort (alloc_sec, n, sizeof (*alloc_sec), sort_sections);
532
533 /* Look for overlapping vmas. Any with overlap must be overlays.
47f6dab9 534 Count them. Also count the number of overlay regions. */
e9f53129
AM
535 ovl_end = alloc_sec[0]->vma + alloc_sec[0]->size;
536 for (ovl_index = 0, num_buf = 0, i = 1; i < n; i++)
537 {
538 s = alloc_sec[i];
539 if (s->vma < ovl_end)
540 {
541 asection *s0 = alloc_sec[i - 1];
542
47f6dab9 543 if (spu_elf_section_data (s0)->u.o.ovl_index == 0)
e9f53129 544 {
47f6dab9
AM
545 alloc_sec[ovl_index] = s0;
546 spu_elf_section_data (s0)->u.o.ovl_index = ++ovl_index;
547 spu_elf_section_data (s0)->u.o.ovl_buf = ++num_buf;
e9f53129 548 }
47f6dab9
AM
549 alloc_sec[ovl_index] = s;
550 spu_elf_section_data (s)->u.o.ovl_index = ++ovl_index;
551 spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
552 if (s0->vma != s->vma)
e9f53129 553 {
47f6dab9
AM
554 info->callbacks->einfo (_("%X%P: overlay sections %A and %A "
555 "do not start at the same address.\n"),
556 s0, s);
557 return FALSE;
e9f53129 558 }
47f6dab9
AM
559 if (ovl_end < s->vma + s->size)
560 ovl_end = s->vma + s->size;
e9f53129
AM
561 }
562 else
563 ovl_end = s->vma + s->size;
564 }
565
566 htab->num_overlays = ovl_index;
567 htab->num_buf = num_buf;
47f6dab9
AM
568 htab->ovl_sec = alloc_sec;
569 return ovl_index != 0;
e9f53129
AM
570}
571
47f6dab9
AM
572/* Support two sizes of overlay stubs, a slower more compact stub of two
573 intructions, and a faster stub of four instructions. */
574#ifndef OVL_STUB_SIZE
575/* Default to faster. */
576#define OVL_STUB_SIZE 16
577/* #define OVL_STUB_SIZE 8 */
578#endif
579#define BRSL 0x33000000
580#define BR 0x32000000
e9f53129 581#define NOP 0x40200000
47f6dab9
AM
582#define LNOP 0x00200000
583#define ILA 0x42000000
e9f53129 584
49fa1e15 585/* Return true for all relative and absolute branch instructions.
e9f53129
AM
586 bra 00110000 0..
587 brasl 00110001 0..
588 br 00110010 0..
589 brsl 00110011 0..
590 brz 00100000 0..
591 brnz 00100001 0..
592 brhz 00100010 0..
49fa1e15
AM
593 brhnz 00100011 0.. */
594
595static bfd_boolean
596is_branch (const unsigned char *insn)
597{
598 return (insn[0] & 0xec) == 0x20 && (insn[1] & 0x80) == 0;
599}
600
fad9eaf0
AM
601/* Return true for all indirect branch instructions.
602 bi 00110101 000
603 bisl 00110101 001
604 iret 00110101 010
605 bisled 00110101 011
606 biz 00100101 000
607 binz 00100101 001
608 bihz 00100101 010
609 bihnz 00100101 011 */
610
611static bfd_boolean
612is_indirect_branch (const unsigned char *insn)
613{
614 return (insn[0] & 0xef) == 0x25 && (insn[1] & 0x80) == 0;
615}
616
49fa1e15 617/* Return true for branch hint instructions.
e9f53129
AM
618 hbra 0001000..
619 hbrr 0001001.. */
620
621static bfd_boolean
49fa1e15 622is_hint (const unsigned char *insn)
e9f53129 623{
49fa1e15 624 return (insn[0] & 0xfc) == 0x10;
e9f53129
AM
625}
626
aa7a0635
AM
627/* Return TRUE if this reloc symbol should possibly go via an overlay stub. */
628
629static bfd_boolean
630needs_ovl_stub (const char *sym_name,
631 asection *sym_sec,
632 asection *input_section,
633 struct spu_link_hash_table *htab,
634 bfd_boolean is_branch)
635{
636 if (htab->num_overlays == 0)
637 return FALSE;
638
639 if (sym_sec == NULL
2c67c5f3
AM
640 || sym_sec->output_section == NULL
641 || spu_elf_section_data (sym_sec->output_section) == NULL)
aa7a0635
AM
642 return FALSE;
643
644 /* setjmp always goes via an overlay stub, because then the return
645 and hence the longjmp goes via __ovly_return. That magically
646 makes setjmp/longjmp between overlays work. */
647 if (strncmp (sym_name, "setjmp", 6) == 0
648 && (sym_name[6] == '\0' || sym_name[6] == '@'))
649 return TRUE;
650
651 /* Usually, symbols in non-overlay sections don't need stubs. */
47f6dab9 652 if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index == 0
aa7a0635
AM
653 && !htab->non_overlay_stubs)
654 return FALSE;
655
656 /* A reference from some other section to a symbol in an overlay
657 section needs a stub. */
47f6dab9
AM
658 if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index
659 != spu_elf_section_data (input_section->output_section)->u.o.ovl_index)
aa7a0635
AM
660 return TRUE;
661
662 /* If this insn isn't a branch then we are possibly taking the
663 address of a function and passing it out somehow. */
664 return !is_branch;
665}
666
47f6dab9
AM
667enum _insn_type { non_branch, branch, call };
668
669static bfd_boolean
670count_stub (struct spu_link_hash_table *htab,
671 bfd *ibfd,
672 asection *isec,
673 enum _insn_type insn_type,
674 struct elf_link_hash_entry *h,
675 const Elf_Internal_Rela *irela)
676{
677 unsigned int ovl = 0;
678 struct got_entry *g, **head;
4a628337 679 bfd_vma addend;
47f6dab9
AM
680
681 /* If this instruction is a branch or call, we need a stub
682 for it. One stub per function per overlay.
683 If it isn't a branch, then we are taking the address of
684 this function so need a stub in the non-overlay area
685 for it. One stub per function. */
686 if (insn_type != non_branch)
687 ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index;
688
689 if (h != NULL)
690 head = &h->got.glist;
691 else
692 {
693 if (elf_local_got_ents (ibfd) == NULL)
694 {
695 bfd_size_type amt = (elf_tdata (ibfd)->symtab_hdr.sh_info
696 * sizeof (*elf_local_got_ents (ibfd)));
697 elf_local_got_ents (ibfd) = bfd_zmalloc (amt);
698 if (elf_local_got_ents (ibfd) == NULL)
699 return FALSE;
700 }
701 head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
702 }
703
4a628337
AM
704 addend = 0;
705 if (irela != NULL)
706 addend = irela->r_addend;
47f6dab9
AM
707
708 if (ovl == 0)
709 {
710 struct got_entry *gnext;
711
4a628337
AM
712 for (g = *head; g != NULL; g = g->next)
713 if (g->addend == addend && g->ovl == 0)
714 break;
715
716 if (g == NULL)
47f6dab9 717 {
4a628337
AM
718 /* Need a new non-overlay area stub. Zap other stubs. */
719 for (g = *head; g != NULL; g = gnext)
720 {
721 gnext = g->next;
722 if (g->addend == addend)
723 {
724 htab->stub_count[g->ovl] -= 1;
725 free (g);
726 }
727 }
47f6dab9
AM
728 }
729 }
730 else
731 {
4a628337
AM
732 for (g = *head; g != NULL; g = g->next)
733 if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
47f6dab9
AM
734 break;
735 }
736
737 if (g == NULL)
738 {
739 g = bfd_malloc (sizeof *g);
740 if (g == NULL)
741 return FALSE;
742 g->ovl = ovl;
4a628337 743 g->addend = addend;
47f6dab9
AM
744 g->stub_addr = (bfd_vma) -1;
745 g->next = *head;
746 *head = g;
747
748 htab->stub_count[ovl] += 1;
749 }
750
751 return TRUE;
752}
753
754/* Two instruction overlay stubs look like:
755
756 brsl $75,__ovly_load
757 .word target_ovl_and_address
758
759 ovl_and_address is a word with the overlay number in the top 14 bits
760 and local store address in the bottom 18 bits.
761
762 Four instruction overlay stubs look like:
763
764 ila $78,ovl_number
765 lnop
766 ila $79,target_address
767 br __ovly_load */
768
769static bfd_boolean
770build_stub (struct spu_link_hash_table *htab,
771 bfd *ibfd,
772 asection *isec,
773 enum _insn_type insn_type,
774 struct elf_link_hash_entry *h,
775 const Elf_Internal_Rela *irela,
776 bfd_vma dest,
777 asection *dest_sec)
778{
779 unsigned int ovl;
780 struct got_entry *g, **head;
781 asection *sec;
4a628337 782 bfd_vma addend, val, from, to;
47f6dab9
AM
783
784 ovl = 0;
785 if (insn_type != non_branch)
786 ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index;
787
788 if (h != NULL)
789 head = &h->got.glist;
790 else
791 head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
792
4a628337
AM
793 addend = 0;
794 if (irela != NULL)
795 addend = irela->r_addend;
47f6dab9 796
4a628337
AM
797 for (g = *head; g != NULL; g = g->next)
798 if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
47f6dab9
AM
799 break;
800 if (g == NULL)
801 abort ();
802
4a628337
AM
803 if (g->ovl == 0 && ovl != 0)
804 return TRUE;
805
47f6dab9
AM
806 if (g->stub_addr != (bfd_vma) -1)
807 return TRUE;
808
809 sec = htab->stub_sec[ovl];
810 dest += dest_sec->output_offset + dest_sec->output_section->vma;
811 from = sec->size + sec->output_offset + sec->output_section->vma;
812 g->stub_addr = from;
813 to = (htab->ovly_load->root.u.def.value
814 + htab->ovly_load->root.u.def.section->output_offset
815 + htab->ovly_load->root.u.def.section->output_section->vma);
816 val = to - from;
817 if (OVL_STUB_SIZE == 16)
818 val -= 12;
819 if (((dest | to | from) & 3) != 0
820 || val + 0x20000 >= 0x40000)
821 {
822 htab->stub_err = 1;
823 return FALSE;
824 }
825 ovl = spu_elf_section_data (dest_sec->output_section)->u.o.ovl_index;
826
827 if (OVL_STUB_SIZE == 16)
828 {
829 bfd_put_32 (sec->owner, ILA + ((ovl << 7) & 0x01ffff80) + 78,
830 sec->contents + sec->size);
831 bfd_put_32 (sec->owner, LNOP,
832 sec->contents + sec->size + 4);
833 bfd_put_32 (sec->owner, ILA + ((dest << 7) & 0x01ffff80) + 79,
834 sec->contents + sec->size + 8);
835 bfd_put_32 (sec->owner, BR + ((val << 5) & 0x007fff80),
836 sec->contents + sec->size + 12);
837 }
838 else if (OVL_STUB_SIZE == 8)
839 {
840 bfd_put_32 (sec->owner, BRSL + ((val << 5) & 0x007fff80) + 75,
841 sec->contents + sec->size);
842
843 val = (dest & 0x3ffff) | (ovl << 14);
844 bfd_put_32 (sec->owner, val,
845 sec->contents + sec->size + 4);
846 }
847 else
848 abort ();
849 sec->size += OVL_STUB_SIZE;
850
851 if (htab->emit_stub_syms)
852 {
853 size_t len;
854 char *name;
855 int add;
856
857 len = 8 + sizeof (".ovl_call.") - 1;
858 if (h != NULL)
859 len += strlen (h->root.root.string);
860 else
861 len += 8 + 1 + 8;
862 add = 0;
863 if (irela != NULL)
864 add = (int) irela->r_addend & 0xffffffff;
865 if (add != 0)
866 len += 1 + 8;
867 name = bfd_malloc (len);
868 if (name == NULL)
869 return FALSE;
870
871 sprintf (name, "%08x.ovl_call.", g->ovl);
872 if (h != NULL)
873 strcpy (name + 8 + sizeof (".ovl_call.") - 1, h->root.root.string);
874 else
875 sprintf (name + 8 + sizeof (".ovl_call.") - 1, "%x:%x",
876 dest_sec->id & 0xffffffff,
877 (int) ELF32_R_SYM (irela->r_info) & 0xffffffff);
878 if (add != 0)
879 sprintf (name + len - 9, "+%x", add);
880
881 h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE);
882 free (name);
883 if (h == NULL)
884 return FALSE;
885 if (h->root.type == bfd_link_hash_new)
886 {
887 h->root.type = bfd_link_hash_defined;
888 h->root.u.def.section = sec;
889 h->root.u.def.value = sec->size - OVL_STUB_SIZE;
890 h->size = OVL_STUB_SIZE;
891 h->type = STT_FUNC;
892 h->ref_regular = 1;
893 h->def_regular = 1;
894 h->ref_regular_nonweak = 1;
895 h->forced_local = 1;
896 h->non_elf = 0;
897 }
898 }
899
900 return TRUE;
901}
902
f4b39977
AM
903/* Called via elf_link_hash_traverse to allocate stubs for any _SPUEAR_
904 symbols. */
905
906static bfd_boolean
907allocate_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
908{
909 /* Symbols starting with _SPUEAR_ need a stub because they may be
910 invoked by the PPU. */
911 if ((h->root.type == bfd_link_hash_defined
912 || h->root.type == bfd_link_hash_defweak)
913 && h->def_regular
914 && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0)
915 {
98e89a7d 916 struct spu_link_hash_table *htab = inf;
f4b39977 917
47f6dab9 918 count_stub (htab, NULL, NULL, non_branch, h, NULL);
f4b39977
AM
919 }
920
921 return TRUE;
922}
923
e9f53129 924static bfd_boolean
47f6dab9 925build_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
e9f53129 926{
47f6dab9
AM
927 /* Symbols starting with _SPUEAR_ need a stub because they may be
928 invoked by the PPU. */
929 if ((h->root.type == bfd_link_hash_defined
930 || h->root.type == bfd_link_hash_defweak)
931 && h->def_regular
932 && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0)
933 {
934 struct spu_link_hash_table *htab = inf;
e9f53129 935
47f6dab9
AM
936 build_stub (htab, NULL, NULL, non_branch, h, NULL,
937 h->root.u.def.value, h->root.u.def.section);
938 }
939
e9f53129
AM
940 return TRUE;
941}
942
47f6dab9 943/* Size or build stubs. */
e9f53129 944
47f6dab9
AM
945static bfd_boolean
946process_stubs (bfd *output_bfd,
947 struct bfd_link_info *info,
948 bfd_boolean build)
e9f53129
AM
949{
950 struct spu_link_hash_table *htab = spu_hash_table (info);
951 bfd *ibfd;
e9f53129 952
e9f53129
AM
953 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
954 {
955 extern const bfd_target bfd_elf32_spu_vec;
956 Elf_Internal_Shdr *symtab_hdr;
47f6dab9 957 asection *isec;
e9f53129 958 Elf_Internal_Sym *local_syms = NULL;
d0249648 959 void *psyms;
e9f53129
AM
960
961 if (ibfd->xvec != &bfd_elf32_spu_vec)
962 continue;
963
964 /* We'll need the symbol table in a second. */
965 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
966 if (symtab_hdr->sh_info == 0)
967 continue;
968
49fa1e15
AM
969 /* Arrange to read and keep global syms for later stack analysis. */
970 psyms = &local_syms;
47f6dab9 971 if (htab->stack_analysis)
d0249648 972 psyms = &symtab_hdr->contents;
49fa1e15 973
e9f53129 974 /* Walk over each section attached to the input bfd. */
47f6dab9 975 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
e9f53129
AM
976 {
977 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
978
979 /* If there aren't any relocs, then there's nothing more to do. */
47f6dab9
AM
980 if ((isec->flags & SEC_RELOC) == 0
981 || (isec->flags & SEC_ALLOC) == 0
982 || (isec->flags & SEC_LOAD) == 0
983 || isec->reloc_count == 0)
e9f53129
AM
984 continue;
985
986 /* If this section is a link-once section that will be
987 discarded, then don't create any stubs. */
47f6dab9
AM
988 if (isec->output_section == NULL
989 || isec->output_section->owner != output_bfd)
e9f53129
AM
990 continue;
991
992 /* Get the relocs. */
47f6dab9
AM
993 internal_relocs = _bfd_elf_link_read_relocs (ibfd, isec, NULL, NULL,
994 info->keep_memory);
e9f53129
AM
995 if (internal_relocs == NULL)
996 goto error_ret_free_local;
997
998 /* Now examine each relocation. */
999 irela = internal_relocs;
47f6dab9 1000 irelaend = irela + isec->reloc_count;
e9f53129
AM
1001 for (; irela < irelaend; irela++)
1002 {
1003 enum elf_spu_reloc_type r_type;
1004 unsigned int r_indx;
1005 asection *sym_sec;
1006 Elf_Internal_Sym *sym;
1007 struct elf_link_hash_entry *h;
aa7a0635 1008 const char *sym_name;
e9f53129 1009 unsigned int sym_type;
47f6dab9 1010 enum _insn_type insn_type;
e9f53129
AM
1011
1012 r_type = ELF32_R_TYPE (irela->r_info);
1013 r_indx = ELF32_R_SYM (irela->r_info);
1014
1015 if (r_type >= R_SPU_max)
1016 {
1017 bfd_set_error (bfd_error_bad_value);
47f6dab9
AM
1018 error_ret_free_internal:
1019 if (elf_section_data (isec)->relocs != internal_relocs)
1020 free (internal_relocs);
1021 error_ret_free_local:
1022 if (local_syms != NULL
1023 && (symtab_hdr->contents
1024 != (unsigned char *) local_syms))
1025 free (local_syms);
1026 return FALSE;
e9f53129
AM
1027 }
1028
1029 /* Determine the reloc target section. */
49fa1e15 1030 if (!get_sym_h (&h, &sym, &sym_sec, psyms, r_indx, ibfd))
e9f53129
AM
1031 goto error_ret_free_internal;
1032
1033 if (sym_sec == NULL
1034 || sym_sec->output_section == NULL
1035 || sym_sec->output_section->owner != output_bfd)
1036 continue;
1037
1038 /* Ensure no stubs for user supplied overlay manager syms. */
1039 if (h != NULL
1040 && (strcmp (h->root.root.string, "__ovly_load") == 0
1041 || strcmp (h->root.root.string, "__ovly_return") == 0))
1042 continue;
1043
1044 insn_type = non_branch;
1045 if (r_type == R_SPU_REL16
1046 || r_type == R_SPU_ADDR16)
1047 {
1048 unsigned char insn[4];
1049
47f6dab9 1050 if (!bfd_get_section_contents (ibfd, isec, insn,
e9f53129
AM
1051 irela->r_offset, 4))
1052 goto error_ret_free_internal;
1053
49fa1e15 1054 if (is_branch (insn) || is_hint (insn))
e9f53129
AM
1055 {
1056 insn_type = branch;
1057 if ((insn[0] & 0xfd) == 0x31)
1058 insn_type = call;
1059 }
1060 }
1061
1062 /* We are only interested in function symbols. */
1063 if (h != NULL)
aa7a0635
AM
1064 {
1065 sym_type = h->type;
1066 sym_name = h->root.root.string;
1067 }
e9f53129 1068 else
aa7a0635
AM
1069 {
1070 sym_type = ELF_ST_TYPE (sym->st_info);
1071 sym_name = bfd_elf_sym_name (sym_sec->owner,
1072 symtab_hdr,
1073 sym,
1074 sym_sec);
1075 }
d688b66e 1076
e9f53129
AM
1077 if (sym_type != STT_FUNC)
1078 {
1079 /* It's common for people to write assembly and forget
1080 to give function symbols the right type. Handle
1081 calls to such symbols, but warn so that (hopefully)
1082 people will fix their code. We need the symbol
1083 type to be correct to distinguish function pointer
1084 initialisation from other pointer initialisation. */
1085 if (insn_type == call)
aa7a0635
AM
1086 (*_bfd_error_handler) (_("warning: call to non-function"
1087 " symbol %s defined in %B"),
1088 sym_sec->owner, sym_name);
d688b66e 1089 else if (insn_type == non_branch)
e9f53129
AM
1090 continue;
1091 }
1092
47f6dab9 1093 if (!needs_ovl_stub (sym_name, sym_sec, isec, htab,
aa7a0635 1094 insn_type != non_branch))
e9f53129
AM
1095 continue;
1096
47f6dab9 1097 if (htab->stub_count == NULL)
e9f53129 1098 {
47f6dab9
AM
1099 bfd_size_type amt;
1100 amt = (htab->num_overlays + 1) * sizeof (*htab->stub_count);
1101 htab->stub_count = bfd_zmalloc (amt);
1102 if (htab->stub_count == NULL)
1103 goto error_ret_free_internal;
e9f53129
AM
1104 }
1105
47f6dab9 1106 if (!build)
e9f53129 1107 {
47f6dab9
AM
1108 if (!count_stub (htab, ibfd, isec, insn_type, h, irela))
1109 goto error_ret_free_internal;
e9f53129 1110 }
e9f53129 1111 else
47f6dab9
AM
1112 {
1113 bfd_vma dest;
1114
1115 if (h != NULL)
1116 dest = h->root.u.def.value;
1117 else
1118 dest = sym->st_value;
4a628337 1119 dest += irela->r_addend;
47f6dab9
AM
1120 if (!build_stub (htab, ibfd, isec, insn_type, h, irela,
1121 dest, sym_sec))
1122 goto error_ret_free_internal;
1123 }
e9f53129
AM
1124 }
1125
1126 /* We're done with the internal relocs, free them. */
47f6dab9 1127 if (elf_section_data (isec)->relocs != internal_relocs)
e9f53129
AM
1128 free (internal_relocs);
1129 }
1130
1131 if (local_syms != NULL
1132 && symtab_hdr->contents != (unsigned char *) local_syms)
1133 {
1134 if (!info->keep_memory)
1135 free (local_syms);
1136 else
1137 symtab_hdr->contents = (unsigned char *) local_syms;
1138 }
1139 }
1140
47f6dab9
AM
1141 return TRUE;
1142}
1143
1144/* Allocate space for overlay call and return stubs. */
1145
1146int
1147spu_elf_size_stubs (bfd *output_bfd,
1148 struct bfd_link_info *info,
1149 void (*place_spu_section) (asection *, asection *,
1150 const char *),
1151 int non_overlay_stubs)
1152{
1153 struct spu_link_hash_table *htab = spu_hash_table (info);
1154 bfd *ibfd;
1155 bfd_size_type amt;
1156 flagword flags;
1157 unsigned int i;
1158 asection *stub;
1159
1160 htab->non_overlay_stubs = non_overlay_stubs;
1161 if (!process_stubs (output_bfd, info, FALSE))
1162 return 0;
1163
98e89a7d 1164 elf_link_hash_traverse (&htab->elf, allocate_spuear_stubs, htab);
47f6dab9
AM
1165 if (htab->stub_err)
1166 return 0;
f4b39977 1167
47f6dab9
AM
1168 if (htab->stub_count == NULL)
1169 return 1;
e9f53129
AM
1170
1171 ibfd = info->input_bfds;
47f6dab9
AM
1172 amt = (htab->num_overlays + 1) * sizeof (*htab->stub_sec);
1173 htab->stub_sec = bfd_zmalloc (amt);
1174 if (htab->stub_sec == NULL)
1175 return 0;
e9f53129 1176
47f6dab9 1177 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
e9f53129 1178 | SEC_HAS_CONTENTS | SEC_IN_MEMORY);
47f6dab9
AM
1179 stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
1180 htab->stub_sec[0] = stub;
1181 if (stub == NULL
1182 || !bfd_set_section_alignment (ibfd, stub, 3 + (OVL_STUB_SIZE > 8)))
1183 return 0;
1184 stub->size = htab->stub_count[0] * OVL_STUB_SIZE;
1185 (*place_spu_section) (stub, NULL, ".text");
e9f53129 1186
47f6dab9 1187 for (i = 0; i < htab->num_overlays; ++i)
e9f53129 1188 {
47f6dab9
AM
1189 asection *osec = htab->ovl_sec[i];
1190 unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
1191 stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
1192 htab->stub_sec[ovl] = stub;
1193 if (stub == NULL
1194 || !bfd_set_section_alignment (ibfd, stub, 3 + (OVL_STUB_SIZE > 8)))
1195 return 0;
1196 stub->size = htab->stub_count[ovl] * OVL_STUB_SIZE;
1197 (*place_spu_section) (stub, osec, NULL);
e9f53129 1198 }
e9f53129
AM
1199
1200 /* htab->ovtab consists of two arrays.
1201 . struct {
1202 . u32 vma;
1203 . u32 size;
1204 . u32 file_off;
1205 . u32 buf;
1206 . } _ovly_table[];
1207 .
1208 . struct {
1209 . u32 mapped;
47f6dab9
AM
1210 . } _ovly_buf_table[];
1211 . */
e9f53129 1212
47f6dab9
AM
1213 flags = (SEC_ALLOC | SEC_LOAD
1214 | SEC_HAS_CONTENTS | SEC_IN_MEMORY);
1215 htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags);
1216 if (htab->ovtab == NULL
1217 || !bfd_set_section_alignment (ibfd, htab->ovtab, 4))
1218 return 0;
e9f53129 1219
2e444bea 1220 htab->ovtab->size = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
47f6dab9
AM
1221 (*place_spu_section) (htab->ovtab, NULL, ".data");
1222
1223 htab->toe = bfd_make_section_anyway_with_flags (ibfd, ".toe", SEC_ALLOC);
1224 if (htab->toe == NULL
1225 || !bfd_set_section_alignment (ibfd, htab->toe, 4))
1226 return 0;
1227 htab->toe->size = 16;
1228 (*place_spu_section) (htab->toe, NULL, ".toe");
1229
1230 return 2;
e9f53129
AM
1231}
1232
1233/* Functions to handle embedded spu_ovl.o object. */
1234
1235static void *
1236ovl_mgr_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *stream)
1237{
1238 return stream;
1239}
1240
1241static file_ptr
1242ovl_mgr_pread (struct bfd *abfd ATTRIBUTE_UNUSED,
1243 void *stream,
1244 void *buf,
1245 file_ptr nbytes,
1246 file_ptr offset)
1247{
1248 struct _ovl_stream *os;
1249 size_t count;
1250 size_t max;
1251
1252 os = (struct _ovl_stream *) stream;
7a8757b3 1253 max = (const char *) os->end - (const char *) os->start;
e9f53129
AM
1254
1255 if ((ufile_ptr) offset >= max)
1256 return 0;
1257
1258 count = nbytes;
1259 if (count > max - offset)
1260 count = max - offset;
1261
7a8757b3 1262 memcpy (buf, (const char *) os->start + offset, count);
e9f53129
AM
1263 return count;
1264}
1265
1266bfd_boolean
1267spu_elf_open_builtin_lib (bfd **ovl_bfd, const struct _ovl_stream *stream)
1268{
1269 *ovl_bfd = bfd_openr_iovec ("builtin ovl_mgr",
1270 "elf32-spu",
1271 ovl_mgr_open,
1272 (void *) stream,
1273 ovl_mgr_pread,
f6cf9273 1274 NULL,
e9f53129
AM
1275 NULL);
1276 return *ovl_bfd != NULL;
1277}
1278
e9f53129
AM
1279/* Define an STT_OBJECT symbol. */
1280
1281static struct elf_link_hash_entry *
1282define_ovtab_symbol (struct spu_link_hash_table *htab, const char *name)
1283{
1284 struct elf_link_hash_entry *h;
1285
1286 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
1287 if (h == NULL)
1288 return NULL;
1289
1290 if (h->root.type != bfd_link_hash_defined
1291 || !h->def_regular)
1292 {
1293 h->root.type = bfd_link_hash_defined;
1294 h->root.u.def.section = htab->ovtab;
1295 h->type = STT_OBJECT;
1296 h->ref_regular = 1;
1297 h->def_regular = 1;
1298 h->ref_regular_nonweak = 1;
1299 h->non_elf = 0;
1300 }
1301 else
1302 {
1303 (*_bfd_error_handler) (_("%B is not allowed to define %s"),
1304 h->root.u.def.section->owner,
1305 h->root.root.string);
1306 bfd_set_error (bfd_error_bad_value);
1307 return NULL;
1308 }
1309
1310 return h;
1311}
1312
1313/* Fill in all stubs and the overlay tables. */
1314
1315bfd_boolean
47f6dab9 1316spu_elf_build_stubs (struct bfd_link_info *info, int emit_syms)
e9f53129
AM
1317{
1318 struct spu_link_hash_table *htab = spu_hash_table (info);
1319 struct elf_link_hash_entry *h;
1320 bfd_byte *p;
1321 asection *s;
1322 bfd *obfd;
1323 unsigned int i;
1324
1325 htab->emit_stub_syms = emit_syms;
47f6dab9
AM
1326 if (htab->stub_count == NULL)
1327 return TRUE;
1328
1329 for (i = 0; i <= htab->num_overlays; i++)
1330 if (htab->stub_sec[i]->size != 0)
1331 {
1332 htab->stub_sec[i]->contents = bfd_zalloc (htab->stub_sec[i]->owner,
1333 htab->stub_sec[i]->size);
1334 if (htab->stub_sec[i]->contents == NULL)
1335 return FALSE;
1336 htab->stub_sec[i]->rawsize = htab->stub_sec[i]->size;
1337 htab->stub_sec[i]->size = 0;
1338 }
e9f53129
AM
1339
1340 h = elf_link_hash_lookup (&htab->elf, "__ovly_load", FALSE, FALSE, FALSE);
1341 htab->ovly_load = h;
1342 BFD_ASSERT (h != NULL
1343 && (h->root.type == bfd_link_hash_defined
1344 || h->root.type == bfd_link_hash_defweak)
1345 && h->def_regular);
1346
1347 s = h->root.u.def.section->output_section;
47f6dab9 1348 if (spu_elf_section_data (s)->u.o.ovl_index)
e9f53129
AM
1349 {
1350 (*_bfd_error_handler) (_("%s in overlay section"),
1351 h->root.u.def.section->owner);
1352 bfd_set_error (bfd_error_bad_value);
1353 return FALSE;
1354 }
1355
47f6dab9
AM
1356 h = elf_link_hash_lookup (&htab->elf, "__ovly_return", FALSE, FALSE, FALSE);
1357 htab->ovly_return = h;
1358
e9f53129 1359 /* Write out all the stubs. */
47f6dab9
AM
1360 obfd = htab->ovtab->output_section->owner;
1361 process_stubs (obfd, info, TRUE);
1362
1363 elf_link_hash_traverse (&htab->elf, build_spuear_stubs, htab);
1364 if (htab->stub_err)
1365 return FALSE;
e9f53129 1366
47f6dab9
AM
1367 for (i = 0; i <= htab->num_overlays; i++)
1368 {
1369 if (htab->stub_sec[i]->size != htab->stub_sec[i]->rawsize)
1370 {
1371 (*_bfd_error_handler) (_("stubs don't match calculated size"));
1372 bfd_set_error (bfd_error_bad_value);
1373 return FALSE;
1374 }
1375 htab->stub_sec[i]->rawsize = 0;
1376 }
1377
1378 if (htab->stub_err)
e9f53129
AM
1379 {
1380 (*_bfd_error_handler) (_("overlay stub relocation overflow"));
1381 bfd_set_error (bfd_error_bad_value);
1382 return FALSE;
1383 }
1384
1385 htab->ovtab->contents = bfd_zalloc (htab->ovtab->owner, htab->ovtab->size);
1386 if (htab->ovtab->contents == NULL)
1387 return FALSE;
1388
1389 /* Write out _ovly_table. */
1390 p = htab->ovtab->contents;
2e444bea
AM
1391 /* set low bit of .size to mark non-overlay area as present. */
1392 p[7] = 1;
e9f53129
AM
1393 for (s = obfd->sections; s != NULL; s = s->next)
1394 {
47f6dab9 1395 unsigned int ovl_index = spu_elf_section_data (s)->u.o.ovl_index;
e9f53129
AM
1396
1397 if (ovl_index != 0)
1398 {
47f6dab9
AM
1399 unsigned long off = ovl_index * 16;
1400 unsigned int ovl_buf = spu_elf_section_data (s)->u.o.ovl_buf;
1401
e9f53129
AM
1402 bfd_put_32 (htab->ovtab->owner, s->vma, p + off);
1403 bfd_put_32 (htab->ovtab->owner, (s->size + 15) & -16, p + off + 4);
1404 /* file_off written later in spu_elf_modify_program_headers. */
2e444bea 1405 bfd_put_32 (htab->ovtab->owner, ovl_buf, p + off + 12);
e9f53129
AM
1406 }
1407 }
1408
e9f53129
AM
1409 h = define_ovtab_symbol (htab, "_ovly_table");
1410 if (h == NULL)
1411 return FALSE;
47f6dab9 1412 h->root.u.def.value = 16;
e9f53129
AM
1413 h->size = htab->num_overlays * 16;
1414
1415 h = define_ovtab_symbol (htab, "_ovly_table_end");
1416 if (h == NULL)
1417 return FALSE;
47f6dab9 1418 h->root.u.def.value = htab->num_overlays * 16 + 16;
e9f53129
AM
1419 h->size = 0;
1420
1421 h = define_ovtab_symbol (htab, "_ovly_buf_table");
1422 if (h == NULL)
1423 return FALSE;
47f6dab9 1424 h->root.u.def.value = htab->num_overlays * 16 + 16;
2e444bea 1425 h->size = htab->num_buf * 4;
e9f53129
AM
1426
1427 h = define_ovtab_symbol (htab, "_ovly_buf_table_end");
1428 if (h == NULL)
1429 return FALSE;
2e444bea 1430 h->root.u.def.value = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
e9f53129
AM
1431 h->size = 0;
1432
1433 h = define_ovtab_symbol (htab, "_EAR_");
1434 if (h == NULL)
1435 return FALSE;
47f6dab9 1436 h->root.u.def.section = htab->toe;
e9f53129
AM
1437 h->root.u.def.value = 0;
1438 h->size = 16;
1439
1440 return TRUE;
1441}
1442
49fa1e15
AM
1443/* OFFSET in SEC (presumably) is the beginning of a function prologue.
1444 Search for stack adjusting insns, and return the sp delta. */
1445
1446static int
1447find_function_stack_adjust (asection *sec, bfd_vma offset)
1448{
1449 int unrecog;
1450 int reg[128];
1451
1452 memset (reg, 0, sizeof (reg));
1453 for (unrecog = 0; offset + 4 <= sec->size && unrecog < 32; offset += 4)
1454 {
1455 unsigned char buf[4];
1456 int rt, ra;
1457 int imm;
1458
1459 /* Assume no relocs on stack adjusing insns. */
1460 if (!bfd_get_section_contents (sec->owner, sec, buf, offset, 4))
1461 break;
1462
1463 if (buf[0] == 0x24 /* stqd */)
1464 continue;
1465
1466 rt = buf[3] & 0x7f;
1467 ra = ((buf[2] & 0x3f) << 1) | (buf[3] >> 7);
1468 /* Partly decoded immediate field. */
1469 imm = (buf[1] << 9) | (buf[2] << 1) | (buf[3] >> 7);
1470
1471 if (buf[0] == 0x1c /* ai */)
1472 {
1473 imm >>= 7;
1474 imm = (imm ^ 0x200) - 0x200;
1475 reg[rt] = reg[ra] + imm;
1476
1477 if (rt == 1 /* sp */)
1478 {
1479 if (imm > 0)
1480 break;
1481 return reg[rt];
1482 }
1483 }
1484 else if (buf[0] == 0x18 && (buf[1] & 0xe0) == 0 /* a */)
1485 {
1486 int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6);
1487
1488 reg[rt] = reg[ra] + reg[rb];
1489 if (rt == 1)
1490 return reg[rt];
1491 }
1492 else if ((buf[0] & 0xfc) == 0x40 /* il, ilh, ilhu, ila */)
1493 {
1494 if (buf[0] >= 0x42 /* ila */)
1495 imm |= (buf[0] & 1) << 17;
1496 else
1497 {
1498 imm &= 0xffff;
1499
1500 if (buf[0] == 0x40 /* il */)
1501 {
1502 if ((buf[1] & 0x80) == 0)
1503 goto unknown_insn;
1504 imm = (imm ^ 0x8000) - 0x8000;
1505 }
1506 else if ((buf[1] & 0x80) == 0 /* ilhu */)
1507 imm <<= 16;
1508 }
1509 reg[rt] = imm;
1510 continue;
1511 }
1512 else if (buf[0] == 0x60 && (buf[1] & 0x80) != 0 /* iohl */)
1513 {
1514 reg[rt] |= imm & 0xffff;
1515 continue;
1516 }
1517 else if (buf[0] == 0x04 /* ori */)
1518 {
1519 imm >>= 7;
1520 imm = (imm ^ 0x200) - 0x200;
1521 reg[rt] = reg[ra] | imm;
1522 continue;
1523 }
1524 else if ((buf[0] == 0x33 && imm == 1 /* brsl .+4 */)
1525 || (buf[0] == 0x08 && (buf[1] & 0xe0) == 0 /* sf */))
1526 {
1527 /* Used in pic reg load. Say rt is trashed. */
1528 reg[rt] = 0;
1529 continue;
1530 }
fad9eaf0 1531 else if (is_branch (buf) || is_indirect_branch (buf))
49fa1e15
AM
1532 /* If we hit a branch then we must be out of the prologue. */
1533 break;
1534 unknown_insn:
1535 ++unrecog;
1536 }
1537
1538 return 0;
1539}
1540
1541/* qsort predicate to sort symbols by section and value. */
1542
1543static Elf_Internal_Sym *sort_syms_syms;
1544static asection **sort_syms_psecs;
1545
1546static int
1547sort_syms (const void *a, const void *b)
1548{
1549 Elf_Internal_Sym *const *s1 = a;
1550 Elf_Internal_Sym *const *s2 = b;
1551 asection *sec1,*sec2;
1552 bfd_signed_vma delta;
1553
1554 sec1 = sort_syms_psecs[*s1 - sort_syms_syms];
1555 sec2 = sort_syms_psecs[*s2 - sort_syms_syms];
1556
1557 if (sec1 != sec2)
1558 return sec1->index - sec2->index;
1559
1560 delta = (*s1)->st_value - (*s2)->st_value;
1561 if (delta != 0)
1562 return delta < 0 ? -1 : 1;
1563
1564 delta = (*s2)->st_size - (*s1)->st_size;
1565 if (delta != 0)
1566 return delta < 0 ? -1 : 1;
1567
1568 return *s1 < *s2 ? -1 : 1;
1569}
1570
1571struct call_info
1572{
1573 struct function_info *fun;
1574 struct call_info *next;
1575 int is_tail;
1576};
1577
1578struct function_info
1579{
1580 /* List of functions called. Also branches to hot/cold part of
1581 function. */
1582 struct call_info *call_list;
1583 /* For hot/cold part of function, point to owner. */
1584 struct function_info *start;
1585 /* Symbol at start of function. */
1586 union {
1587 Elf_Internal_Sym *sym;
1588 struct elf_link_hash_entry *h;
1589 } u;
1590 /* Function section. */
1591 asection *sec;
1592 /* Address range of (this part of) function. */
1593 bfd_vma lo, hi;
1594 /* Stack usage. */
1595 int stack;
1596 /* Set if global symbol. */
1597 unsigned int global : 1;
1598 /* Set if known to be start of function (as distinct from a hunk
1599 in hot/cold section. */
1600 unsigned int is_func : 1;
1601 /* Flags used during call tree traversal. */
1602 unsigned int visit1 : 1;
1603 unsigned int non_root : 1;
1604 unsigned int visit2 : 1;
1605 unsigned int marking : 1;
1606 unsigned int visit3 : 1;
1607};
1608
1609struct spu_elf_stack_info
1610{
1611 int num_fun;
1612 int max_fun;
1613 /* Variable size array describing functions, one per contiguous
1614 address range belonging to a function. */
1615 struct function_info fun[1];
1616};
1617
1618/* Allocate a struct spu_elf_stack_info with MAX_FUN struct function_info
1619 entries for section SEC. */
1620
1621static struct spu_elf_stack_info *
1622alloc_stack_info (asection *sec, int max_fun)
1623{
1624 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
1625 bfd_size_type amt;
1626
1627 amt = sizeof (struct spu_elf_stack_info);
1628 amt += (max_fun - 1) * sizeof (struct function_info);
47f6dab9
AM
1629 sec_data->u.i.stack_info = bfd_zmalloc (amt);
1630 if (sec_data->u.i.stack_info != NULL)
1631 sec_data->u.i.stack_info->max_fun = max_fun;
1632 return sec_data->u.i.stack_info;
49fa1e15
AM
1633}
1634
1635/* Add a new struct function_info describing a (part of a) function
1636 starting at SYM_H. Keep the array sorted by address. */
1637
1638static struct function_info *
1639maybe_insert_function (asection *sec,
1640 void *sym_h,
1641 bfd_boolean global,
1642 bfd_boolean is_func)
1643{
1644 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
47f6dab9 1645 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
49fa1e15
AM
1646 int i;
1647 bfd_vma off, size;
1648
1649 if (sinfo == NULL)
1650 {
1651 sinfo = alloc_stack_info (sec, 20);
1652 if (sinfo == NULL)
1653 return NULL;
1654 }
1655
1656 if (!global)
1657 {
1658 Elf_Internal_Sym *sym = sym_h;
1659 off = sym->st_value;
1660 size = sym->st_size;
1661 }
1662 else
1663 {
1664 struct elf_link_hash_entry *h = sym_h;
1665 off = h->root.u.def.value;
1666 size = h->size;
1667 }
1668
1669 for (i = sinfo->num_fun; --i >= 0; )
1670 if (sinfo->fun[i].lo <= off)
1671 break;
1672
1673 if (i >= 0)
1674 {
1675 /* Don't add another entry for an alias, but do update some
1676 info. */
1677 if (sinfo->fun[i].lo == off)
1678 {
1679 /* Prefer globals over local syms. */
1680 if (global && !sinfo->fun[i].global)
1681 {
1682 sinfo->fun[i].global = TRUE;
1683 sinfo->fun[i].u.h = sym_h;
1684 }
1685 if (is_func)
1686 sinfo->fun[i].is_func = TRUE;
1687 return &sinfo->fun[i];
1688 }
1689 /* Ignore a zero-size symbol inside an existing function. */
1690 else if (sinfo->fun[i].hi > off && size == 0)
1691 return &sinfo->fun[i];
1692 }
1693
1694 if (++i < sinfo->num_fun)
1695 memmove (&sinfo->fun[i + 1], &sinfo->fun[i],
1696 (sinfo->num_fun - i) * sizeof (sinfo->fun[i]));
1697 else if (i >= sinfo->max_fun)
1698 {
1699 bfd_size_type amt = sizeof (struct spu_elf_stack_info);
1700 bfd_size_type old = amt;
1701
1702 old += (sinfo->max_fun - 1) * sizeof (struct function_info);
1703 sinfo->max_fun += 20 + (sinfo->max_fun >> 1);
1704 amt += (sinfo->max_fun - 1) * sizeof (struct function_info);
1705 sinfo = bfd_realloc (sinfo, amt);
1706 if (sinfo == NULL)
1707 return NULL;
1708 memset ((char *) sinfo + old, 0, amt - old);
47f6dab9 1709 sec_data->u.i.stack_info = sinfo;
49fa1e15
AM
1710 }
1711 sinfo->fun[i].is_func = is_func;
1712 sinfo->fun[i].global = global;
1713 sinfo->fun[i].sec = sec;
1714 if (global)
1715 sinfo->fun[i].u.h = sym_h;
1716 else
1717 sinfo->fun[i].u.sym = sym_h;
1718 sinfo->fun[i].lo = off;
1719 sinfo->fun[i].hi = off + size;
1720 sinfo->fun[i].stack = -find_function_stack_adjust (sec, off);
1721 sinfo->num_fun += 1;
1722 return &sinfo->fun[i];
1723}
1724
1725/* Return the name of FUN. */
1726
1727static const char *
1728func_name (struct function_info *fun)
1729{
1730 asection *sec;
1731 bfd *ibfd;
1732 Elf_Internal_Shdr *symtab_hdr;
1733
1734 while (fun->start != NULL)
1735 fun = fun->start;
1736
1737 if (fun->global)
1738 return fun->u.h->root.root.string;
1739
1740 sec = fun->sec;
1741 if (fun->u.sym->st_name == 0)
1742 {
1743 size_t len = strlen (sec->name);
1744 char *name = bfd_malloc (len + 10);
1745 if (name == NULL)
1746 return "(null)";
1747 sprintf (name, "%s+%lx", sec->name,
1748 (unsigned long) fun->u.sym->st_value & 0xffffffff);
1749 return name;
1750 }
1751 ibfd = sec->owner;
1752 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1753 return bfd_elf_sym_name (ibfd, symtab_hdr, fun->u.sym, sec);
1754}
1755
1756/* Read the instruction at OFF in SEC. Return true iff the instruction
1757 is a nop, lnop, or stop 0 (all zero insn). */
1758
1759static bfd_boolean
1760is_nop (asection *sec, bfd_vma off)
1761{
1762 unsigned char insn[4];
1763
1764 if (off + 4 > sec->size
1765 || !bfd_get_section_contents (sec->owner, sec, insn, off, 4))
1766 return FALSE;
1767 if ((insn[0] & 0xbf) == 0 && (insn[1] & 0xe0) == 0x20)
1768 return TRUE;
1769 if (insn[0] == 0 && insn[1] == 0 && insn[2] == 0 && insn[3] == 0)
1770 return TRUE;
1771 return FALSE;
1772}
1773
1774/* Extend the range of FUN to cover nop padding up to LIMIT.
1775 Return TRUE iff some instruction other than a NOP was found. */
1776
1777static bfd_boolean
1778insns_at_end (struct function_info *fun, bfd_vma limit)
1779{
1780 bfd_vma off = (fun->hi + 3) & -4;
1781
1782 while (off < limit && is_nop (fun->sec, off))
1783 off += 4;
1784 if (off < limit)
1785 {
1786 fun->hi = off;
1787 return TRUE;
1788 }
1789 fun->hi = limit;
1790 return FALSE;
1791}
1792
1793/* Check and fix overlapping function ranges. Return TRUE iff there
1794 are gaps in the current info we have about functions in SEC. */
1795
1796static bfd_boolean
1797check_function_ranges (asection *sec, struct bfd_link_info *info)
1798{
1799 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
47f6dab9 1800 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
49fa1e15
AM
1801 int i;
1802 bfd_boolean gaps = FALSE;
1803
1804 if (sinfo == NULL)
1805 return FALSE;
1806
1807 for (i = 1; i < sinfo->num_fun; i++)
1808 if (sinfo->fun[i - 1].hi > sinfo->fun[i].lo)
1809 {
1810 /* Fix overlapping symbols. */
1811 const char *f1 = func_name (&sinfo->fun[i - 1]);
1812 const char *f2 = func_name (&sinfo->fun[i]);
1813
1814 info->callbacks->einfo (_("warning: %s overlaps %s\n"), f1, f2);
1815 sinfo->fun[i - 1].hi = sinfo->fun[i].lo;
1816 }
1817 else if (insns_at_end (&sinfo->fun[i - 1], sinfo->fun[i].lo))
1818 gaps = TRUE;
1819
1820 if (sinfo->num_fun == 0)
1821 gaps = TRUE;
1822 else
1823 {
1824 if (sinfo->fun[0].lo != 0)
1825 gaps = TRUE;
1826 if (sinfo->fun[sinfo->num_fun - 1].hi > sec->size)
1827 {
1828 const char *f1 = func_name (&sinfo->fun[sinfo->num_fun - 1]);
1829
1830 info->callbacks->einfo (_("warning: %s exceeds section size\n"), f1);
1831 sinfo->fun[sinfo->num_fun - 1].hi = sec->size;
1832 }
1833 else if (insns_at_end (&sinfo->fun[sinfo->num_fun - 1], sec->size))
1834 gaps = TRUE;
1835 }
1836 return gaps;
1837}
1838
1839/* Search current function info for a function that contains address
1840 OFFSET in section SEC. */
1841
1842static struct function_info *
1843find_function (asection *sec, bfd_vma offset, struct bfd_link_info *info)
1844{
1845 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
47f6dab9 1846 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
49fa1e15
AM
1847 int lo, hi, mid;
1848
1849 lo = 0;
1850 hi = sinfo->num_fun;
1851 while (lo < hi)
1852 {
1853 mid = (lo + hi) / 2;
1854 if (offset < sinfo->fun[mid].lo)
1855 hi = mid;
1856 else if (offset >= sinfo->fun[mid].hi)
1857 lo = mid + 1;
1858 else
1859 return &sinfo->fun[mid];
1860 }
1861 info->callbacks->einfo (_("%A:0x%v not found in function table\n"),
1862 sec, offset);
1863 return NULL;
1864}
1865
1866/* Add CALLEE to CALLER call list if not already present. */
1867
1868static bfd_boolean
1869insert_callee (struct function_info *caller, struct call_info *callee)
1870{
1871 struct call_info *p;
1872 for (p = caller->call_list; p != NULL; p = p->next)
1873 if (p->fun == callee->fun)
1874 {
1875 /* Tail calls use less stack than normal calls. Retain entry
1876 for normal call over one for tail call. */
1877 if (p->is_tail > callee->is_tail)
1878 p->is_tail = callee->is_tail;
1879 return FALSE;
1880 }
1881 callee->next = caller->call_list;
1882 caller->call_list = callee;
1883 return TRUE;
1884}
1885
1886/* Rummage through the relocs for SEC, looking for function calls.
1887 If CALL_TREE is true, fill in call graph. If CALL_TREE is false,
1888 mark destination symbols on calls as being functions. Also
1889 look at branches, which may be tail calls or go to hot/cold
1890 section part of same function. */
1891
1892static bfd_boolean
1893mark_functions_via_relocs (asection *sec,
1894 struct bfd_link_info *info,
1895 int call_tree)
1896{
1897 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1898 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (sec->owner)->symtab_hdr;
d0249648
AM
1899 Elf_Internal_Sym *syms;
1900 void *psyms;
49fa1e15
AM
1901 static bfd_boolean warned;
1902
1903 internal_relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL,
1904 info->keep_memory);
1905 if (internal_relocs == NULL)
1906 return FALSE;
1907
1908 symtab_hdr = &elf_tdata (sec->owner)->symtab_hdr;
d0249648
AM
1909 psyms = &symtab_hdr->contents;
1910 syms = *(Elf_Internal_Sym **) psyms;
49fa1e15
AM
1911 irela = internal_relocs;
1912 irelaend = irela + sec->reloc_count;
1913 for (; irela < irelaend; irela++)
1914 {
1915 enum elf_spu_reloc_type r_type;
1916 unsigned int r_indx;
1917 asection *sym_sec;
1918 Elf_Internal_Sym *sym;
1919 struct elf_link_hash_entry *h;
1920 bfd_vma val;
1921 unsigned char insn[4];
1922 bfd_boolean is_call;
1923 struct function_info *caller;
1924 struct call_info *callee;
1925
1926 r_type = ELF32_R_TYPE (irela->r_info);
1927 if (r_type != R_SPU_REL16
1928 && r_type != R_SPU_ADDR16)
1929 continue;
1930
1931 r_indx = ELF32_R_SYM (irela->r_info);
1932 if (!get_sym_h (&h, &sym, &sym_sec, psyms, r_indx, sec->owner))
1933 return FALSE;
1934
1935 if (sym_sec == NULL
1936 || sym_sec->output_section == NULL
1937 || sym_sec->output_section->owner != sec->output_section->owner)
1938 continue;
1939
1940 if (!bfd_get_section_contents (sec->owner, sec, insn,
1941 irela->r_offset, 4))
1942 return FALSE;
1943 if (!is_branch (insn))
1944 continue;
1945
1946 if ((sym_sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE))
1947 != (SEC_ALLOC | SEC_LOAD | SEC_CODE))
1948 {
1949 if (!call_tree)
1950 warned = TRUE;
1951 if (!call_tree || !warned)
1952 info->callbacks->einfo (_("%B(%A+0x%v): call to non-code section"
1953 " %B(%A), stack analysis incomplete\n"),
1954 sec->owner, sec, irela->r_offset,
1955 sym_sec->owner, sym_sec);
1956 continue;
1957 }
1958
1959 is_call = (insn[0] & 0xfd) == 0x31;
1960
1961 if (h)
1962 val = h->root.u.def.value;
1963 else
1964 val = sym->st_value;
1965 val += irela->r_addend;
1966
1967 if (!call_tree)
1968 {
1969 struct function_info *fun;
1970
1971 if (irela->r_addend != 0)
1972 {
1973 Elf_Internal_Sym *fake = bfd_zmalloc (sizeof (*fake));
1974 if (fake == NULL)
1975 return FALSE;
1976 fake->st_value = val;
1977 fake->st_shndx
1978 = _bfd_elf_section_from_bfd_section (sym_sec->owner, sym_sec);
1979 sym = fake;
1980 }
1981 if (sym)
1982 fun = maybe_insert_function (sym_sec, sym, FALSE, is_call);
1983 else
1984 fun = maybe_insert_function (sym_sec, h, TRUE, is_call);
1985 if (fun == NULL)
1986 return FALSE;
1987 if (irela->r_addend != 0
1988 && fun->u.sym != sym)
1989 free (sym);
1990 continue;
1991 }
1992
1993 caller = find_function (sec, irela->r_offset, info);
1994 if (caller == NULL)
1995 return FALSE;
1996 callee = bfd_malloc (sizeof *callee);
1997 if (callee == NULL)
1998 return FALSE;
1999
2000 callee->fun = find_function (sym_sec, val, info);
2001 if (callee->fun == NULL)
2002 return FALSE;
2003 callee->is_tail = !is_call;
2004 if (!insert_callee (caller, callee))
2005 free (callee);
2006 else if (!is_call
2007 && !callee->fun->is_func
2008 && callee->fun->stack == 0)
2009 {
2010 /* This is either a tail call or a branch from one part of
2011 the function to another, ie. hot/cold section. If the
2012 destination has been called by some other function then
2013 it is a separate function. We also assume that functions
2014 are not split across input files. */
911f096e 2015 if (sec->owner != sym_sec->owner)
49fa1e15
AM
2016 {
2017 callee->fun->start = NULL;
2018 callee->fun->is_func = TRUE;
2019 }
911f096e 2020 else if (callee->fun->start == NULL)
49fa1e15 2021 callee->fun->start = caller;
911f096e
AM
2022 else
2023 {
2024 struct function_info *callee_start;
2025 struct function_info *caller_start;
2026 callee_start = callee->fun;
2027 while (callee_start->start)
2028 callee_start = callee_start->start;
2029 caller_start = caller;
2030 while (caller_start->start)
2031 caller_start = caller_start->start;
2032 if (caller_start != callee_start)
2033 {
2034 callee->fun->start = NULL;
2035 callee->fun->is_func = TRUE;
2036 }
2037 }
49fa1e15
AM
2038 }
2039 }
2040
2041 return TRUE;
2042}
2043
2044/* Handle something like .init or .fini, which has a piece of a function.
2045 These sections are pasted together to form a single function. */
2046
2047static bfd_boolean
2048pasted_function (asection *sec, struct bfd_link_info *info)
2049{
2050 struct bfd_link_order *l;
2051 struct _spu_elf_section_data *sec_data;
2052 struct spu_elf_stack_info *sinfo;
2053 Elf_Internal_Sym *fake;
2054 struct function_info *fun, *fun_start;
2055
2056 fake = bfd_zmalloc (sizeof (*fake));
2057 if (fake == NULL)
2058 return FALSE;
2059 fake->st_value = 0;
2060 fake->st_size = sec->size;
2061 fake->st_shndx
2062 = _bfd_elf_section_from_bfd_section (sec->owner, sec);
2063 fun = maybe_insert_function (sec, fake, FALSE, FALSE);
2064 if (!fun)
2065 return FALSE;
2066
2067 /* Find a function immediately preceding this section. */
2068 fun_start = NULL;
2069 for (l = sec->output_section->map_head.link_order; l != NULL; l = l->next)
2070 {
2071 if (l->u.indirect.section == sec)
2072 {
2073 if (fun_start != NULL)
911f096e 2074 fun->start = fun_start;
49fa1e15
AM
2075 return TRUE;
2076 }
2077 if (l->type == bfd_indirect_link_order
2078 && (sec_data = spu_elf_section_data (l->u.indirect.section)) != NULL
47f6dab9 2079 && (sinfo = sec_data->u.i.stack_info) != NULL
49fa1e15
AM
2080 && sinfo->num_fun != 0)
2081 fun_start = &sinfo->fun[sinfo->num_fun - 1];
2082 }
2083
2084 info->callbacks->einfo (_("%A link_order not found\n"), sec);
2085 return FALSE;
2086}
2087
47f6dab9
AM
2088/* We're only interested in code sections. Testing SEC_IN_MEMORY excludes
2089 overlay stub sections. */
49fa1e15
AM
2090
2091static bfd_boolean
47f6dab9 2092interesting_section (asection *s, bfd *obfd)
49fa1e15 2093{
47f6dab9 2094 return (s->output_section != NULL
49fa1e15 2095 && s->output_section->owner == obfd
47f6dab9 2096 && ((s->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_IN_MEMORY))
49fa1e15
AM
2097 == (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2098 && s->size != 0);
2099}
2100
2101/* Map address ranges in code sections to functions. */
2102
2103static bfd_boolean
2104discover_functions (bfd *output_bfd, struct bfd_link_info *info)
2105{
49fa1e15
AM
2106 bfd *ibfd;
2107 int bfd_idx;
2108 Elf_Internal_Sym ***psym_arr;
2109 asection ***sec_arr;
2110 bfd_boolean gaps = FALSE;
2111
2112 bfd_idx = 0;
2113 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2114 bfd_idx++;
2115
2116 psym_arr = bfd_zmalloc (bfd_idx * sizeof (*psym_arr));
2117 if (psym_arr == NULL)
2118 return FALSE;
2119 sec_arr = bfd_zmalloc (bfd_idx * sizeof (*sec_arr));
2120 if (sec_arr == NULL)
2121 return FALSE;
2122
2123
2124 for (ibfd = info->input_bfds, bfd_idx = 0;
2125 ibfd != NULL;
2126 ibfd = ibfd->link_next, bfd_idx++)
2127 {
2128 extern const bfd_target bfd_elf32_spu_vec;
2129 Elf_Internal_Shdr *symtab_hdr;
2130 asection *sec;
2131 size_t symcount;
2132 Elf_Internal_Sym *syms, *sy, **psyms, **psy;
2133 asection **psecs, **p;
2134
2135 if (ibfd->xvec != &bfd_elf32_spu_vec)
2136 continue;
2137
2138 /* Read all the symbols. */
2139 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2140 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2141 if (symcount == 0)
2142 continue;
2143
2144 syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2145 if (syms == NULL)
2146 {
2147 syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, 0,
2148 NULL, NULL, NULL);
2149 symtab_hdr->contents = (void *) syms;
2150 if (syms == NULL)
2151 return FALSE;
2152 }
2153
2154 /* Select defined function symbols that are going to be output. */
2155 psyms = bfd_malloc ((symcount + 1) * sizeof (*psyms));
2156 if (psyms == NULL)
2157 return FALSE;
2158 psym_arr[bfd_idx] = psyms;
2159 psecs = bfd_malloc (symcount * sizeof (*psecs));
2160 if (psecs == NULL)
2161 return FALSE;
2162 sec_arr[bfd_idx] = psecs;
2163 for (psy = psyms, p = psecs, sy = syms; sy < syms + symcount; ++p, ++sy)
2164 if (ELF_ST_TYPE (sy->st_info) == STT_NOTYPE
2165 || ELF_ST_TYPE (sy->st_info) == STT_FUNC)
2166 {
2167 asection *s;
2168
2169 *p = s = bfd_section_from_elf_index (ibfd, sy->st_shndx);
47f6dab9 2170 if (s != NULL && interesting_section (s, output_bfd))
49fa1e15
AM
2171 *psy++ = sy;
2172 }
2173 symcount = psy - psyms;
2174 *psy = NULL;
2175
2176 /* Sort them by section and offset within section. */
2177 sort_syms_syms = syms;
2178 sort_syms_psecs = psecs;
2179 qsort (psyms, symcount, sizeof (*psyms), sort_syms);
2180
2181 /* Now inspect the function symbols. */
2182 for (psy = psyms; psy < psyms + symcount; )
2183 {
2184 asection *s = psecs[*psy - syms];
2185 Elf_Internal_Sym **psy2;
2186
2187 for (psy2 = psy; ++psy2 < psyms + symcount; )
2188 if (psecs[*psy2 - syms] != s)
2189 break;
2190
2191 if (!alloc_stack_info (s, psy2 - psy))
2192 return FALSE;
2193 psy = psy2;
2194 }
2195
2196 /* First install info about properly typed and sized functions.
2197 In an ideal world this will cover all code sections, except
2198 when partitioning functions into hot and cold sections,
2199 and the horrible pasted together .init and .fini functions. */
2200 for (psy = psyms; psy < psyms + symcount; ++psy)
2201 {
2202 sy = *psy;
2203 if (ELF_ST_TYPE (sy->st_info) == STT_FUNC)
2204 {
2205 asection *s = psecs[sy - syms];
2206 if (!maybe_insert_function (s, sy, FALSE, TRUE))
2207 return FALSE;
2208 }
2209 }
2210
2211 for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
47f6dab9 2212 if (interesting_section (sec, output_bfd))
49fa1e15
AM
2213 gaps |= check_function_ranges (sec, info);
2214 }
2215
2216 if (gaps)
2217 {
2218 /* See if we can discover more function symbols by looking at
2219 relocations. */
2220 for (ibfd = info->input_bfds, bfd_idx = 0;
2221 ibfd != NULL;
2222 ibfd = ibfd->link_next, bfd_idx++)
2223 {
2224 asection *sec;
2225
2226 if (psym_arr[bfd_idx] == NULL)
2227 continue;
2228
2229 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
47f6dab9 2230 if (interesting_section (sec, output_bfd)
49fa1e15
AM
2231 && sec->reloc_count != 0)
2232 {
2233 if (!mark_functions_via_relocs (sec, info, FALSE))
2234 return FALSE;
2235 }
2236 }
2237
2238 for (ibfd = info->input_bfds, bfd_idx = 0;
2239 ibfd != NULL;
2240 ibfd = ibfd->link_next, bfd_idx++)
2241 {
2242 Elf_Internal_Shdr *symtab_hdr;
2243 asection *sec;
2244 Elf_Internal_Sym *syms, *sy, **psyms, **psy;
2245 asection **psecs;
2246
2247 if ((psyms = psym_arr[bfd_idx]) == NULL)
2248 continue;
2249
2250 psecs = sec_arr[bfd_idx];
2251
2252 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2253 syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2254
2255 gaps = FALSE;
2256 for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
47f6dab9 2257 if (interesting_section (sec, output_bfd))
49fa1e15
AM
2258 gaps |= check_function_ranges (sec, info);
2259 if (!gaps)
2260 continue;
2261
2262 /* Finally, install all globals. */
2263 for (psy = psyms; (sy = *psy) != NULL; ++psy)
2264 {
2265 asection *s;
2266
2267 s = psecs[sy - syms];
2268
2269 /* Global syms might be improperly typed functions. */
2270 if (ELF_ST_TYPE (sy->st_info) != STT_FUNC
2271 && ELF_ST_BIND (sy->st_info) == STB_GLOBAL)
2272 {
2273 if (!maybe_insert_function (s, sy, FALSE, FALSE))
2274 return FALSE;
2275 }
2276 }
2277
2278 /* Some of the symbols we've installed as marking the
2279 beginning of functions may have a size of zero. Extend
2280 the range of such functions to the beginning of the
2281 next symbol of interest. */
2282 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
47f6dab9 2283 if (interesting_section (sec, output_bfd))
49fa1e15
AM
2284 {
2285 struct _spu_elf_section_data *sec_data;
2286 struct spu_elf_stack_info *sinfo;
2287
2288 sec_data = spu_elf_section_data (sec);
47f6dab9 2289 sinfo = sec_data->u.i.stack_info;
49fa1e15
AM
2290 if (sinfo != NULL)
2291 {
2292 int fun_idx;
2293 bfd_vma hi = sec->size;
2294
2295 for (fun_idx = sinfo->num_fun; --fun_idx >= 0; )
2296 {
2297 sinfo->fun[fun_idx].hi = hi;
2298 hi = sinfo->fun[fun_idx].lo;
2299 }
2300 }
2301 /* No symbols in this section. Must be .init or .fini
2302 or something similar. */
2303 else if (!pasted_function (sec, info))
2304 return FALSE;
2305 }
2306 }
2307 }
2308
2309 for (ibfd = info->input_bfds, bfd_idx = 0;
2310 ibfd != NULL;
2311 ibfd = ibfd->link_next, bfd_idx++)
2312 {
2313 if (psym_arr[bfd_idx] == NULL)
2314 continue;
2315
2316 free (psym_arr[bfd_idx]);
2317 free (sec_arr[bfd_idx]);
2318 }
2319
2320 free (psym_arr);
2321 free (sec_arr);
2322
2323 return TRUE;
2324}
2325
2326/* Mark nodes in the call graph that are called by some other node. */
2327
2328static void
2329mark_non_root (struct function_info *fun)
2330{
2331 struct call_info *call;
2332
2333 fun->visit1 = TRUE;
2334 for (call = fun->call_list; call; call = call->next)
2335 {
2336 call->fun->non_root = TRUE;
2337 if (!call->fun->visit1)
2338 mark_non_root (call->fun);
2339 }
2340}
2341
2342/* Remove cycles from the call graph. */
2343
2344static void
2345call_graph_traverse (struct function_info *fun, struct bfd_link_info *info)
2346{
2347 struct call_info **callp, *call;
2348
2349 fun->visit2 = TRUE;
2350 fun->marking = TRUE;
2351
2352 callp = &fun->call_list;
2353 while ((call = *callp) != NULL)
2354 {
2355 if (!call->fun->visit2)
2356 call_graph_traverse (call->fun, info);
2357 else if (call->fun->marking)
2358 {
2359 const char *f1 = func_name (fun);
2360 const char *f2 = func_name (call->fun);
2361
2362 info->callbacks->info (_("Stack analysis will ignore the call "
2363 "from %s to %s\n"),
2364 f1, f2);
2365 *callp = call->next;
2366 continue;
2367 }
2368 callp = &call->next;
2369 }
2370 fun->marking = FALSE;
2371}
2372
2373/* Populate call_list for each function. */
2374
2375static bfd_boolean
2376build_call_tree (bfd *output_bfd, struct bfd_link_info *info)
2377{
49fa1e15
AM
2378 bfd *ibfd;
2379
2380 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2381 {
2382 extern const bfd_target bfd_elf32_spu_vec;
2383 asection *sec;
2384
2385 if (ibfd->xvec != &bfd_elf32_spu_vec)
2386 continue;
2387
2388 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2389 {
47f6dab9 2390 if (!interesting_section (sec, output_bfd)
49fa1e15
AM
2391 || sec->reloc_count == 0)
2392 continue;
2393
2394 if (!mark_functions_via_relocs (sec, info, TRUE))
2395 return FALSE;
2396 }
2397
2398 /* Transfer call info from hot/cold section part of function
2399 to main entry. */
2400 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2401 {
2402 struct _spu_elf_section_data *sec_data;
2403 struct spu_elf_stack_info *sinfo;
2404
2405 if ((sec_data = spu_elf_section_data (sec)) != NULL
47f6dab9 2406 && (sinfo = sec_data->u.i.stack_info) != NULL)
49fa1e15
AM
2407 {
2408 int i;
2409 for (i = 0; i < sinfo->num_fun; ++i)
2410 {
911f096e
AM
2411 struct function_info *start = sinfo->fun[i].start;
2412
2413 if (start != NULL)
49fa1e15 2414 {
911f096e 2415 struct call_info *call;
49fa1e15 2416
911f096e
AM
2417 while (start->start != NULL)
2418 start = start->start;
2419 call = sinfo->fun[i].call_list;
49fa1e15
AM
2420 while (call != NULL)
2421 {
2422 struct call_info *call_next = call->next;
911f096e 2423 if (!insert_callee (start, call))
49fa1e15
AM
2424 free (call);
2425 call = call_next;
2426 }
2427 sinfo->fun[i].call_list = NULL;
2428 sinfo->fun[i].non_root = TRUE;
2429 }
2430 }
2431 }
2432 }
2433 }
2434
2435 /* Find the call graph root(s). */
2436 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2437 {
2438 extern const bfd_target bfd_elf32_spu_vec;
2439 asection *sec;
2440
2441 if (ibfd->xvec != &bfd_elf32_spu_vec)
2442 continue;
2443
2444 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2445 {
2446 struct _spu_elf_section_data *sec_data;
2447 struct spu_elf_stack_info *sinfo;
2448
2449 if ((sec_data = spu_elf_section_data (sec)) != NULL
47f6dab9 2450 && (sinfo = sec_data->u.i.stack_info) != NULL)
49fa1e15
AM
2451 {
2452 int i;
2453 for (i = 0; i < sinfo->num_fun; ++i)
2454 if (!sinfo->fun[i].visit1)
2455 mark_non_root (&sinfo->fun[i]);
2456 }
2457 }
2458 }
2459
2460 /* Remove cycles from the call graph. We start from the root node(s)
2461 so that we break cycles in a reasonable place. */
2462 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2463 {
2464 extern const bfd_target bfd_elf32_spu_vec;
2465 asection *sec;
2466
2467 if (ibfd->xvec != &bfd_elf32_spu_vec)
2468 continue;
2469
2470 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2471 {
2472 struct _spu_elf_section_data *sec_data;
2473 struct spu_elf_stack_info *sinfo;
2474
2475 if ((sec_data = spu_elf_section_data (sec)) != NULL
47f6dab9 2476 && (sinfo = sec_data->u.i.stack_info) != NULL)
49fa1e15
AM
2477 {
2478 int i;
2479 for (i = 0; i < sinfo->num_fun; ++i)
2480 if (!sinfo->fun[i].non_root)
2481 call_graph_traverse (&sinfo->fun[i], info);
2482 }
2483 }
2484 }
2485
2486 return TRUE;
2487}
2488
2489/* Descend the call graph for FUN, accumulating total stack required. */
2490
2491static bfd_vma
2492sum_stack (struct function_info *fun,
2493 struct bfd_link_info *info,
2494 int emit_stack_syms)
2495{
2496 struct call_info *call;
2497 struct function_info *max = NULL;
2498 bfd_vma max_stack = fun->stack;
2499 bfd_vma stack;
2500 const char *f1;
2501
2502 if (fun->visit3)
2503 return max_stack;
2504
2505 for (call = fun->call_list; call; call = call->next)
2506 {
2507 stack = sum_stack (call->fun, info, emit_stack_syms);
2508 /* Include caller stack for normal calls, don't do so for
2509 tail calls. fun->stack here is local stack usage for
2510 this function. */
2511 if (!call->is_tail)
2512 stack += fun->stack;
2513 if (max_stack < stack)
2514 {
2515 max_stack = stack;
2516 max = call->fun;
2517 }
2518 }
2519
2520 f1 = func_name (fun);
fad9eaf0
AM
2521 info->callbacks->minfo (_("%s: 0x%v 0x%v\n"),
2522 f1, (bfd_vma) fun->stack, max_stack);
49fa1e15
AM
2523
2524 if (fun->call_list)
2525 {
2526 info->callbacks->minfo (_(" calls:\n"));
2527 for (call = fun->call_list; call; call = call->next)
2528 {
2529 const char *f2 = func_name (call->fun);
2530 const char *ann1 = call->fun == max ? "*" : " ";
2531 const char *ann2 = call->is_tail ? "t" : " ";
2532
2533 info->callbacks->minfo (_(" %s%s %s\n"), ann1, ann2, f2);
2534 }
2535 }
2536
2537 /* Now fun->stack holds cumulative stack. */
2538 fun->stack = max_stack;
2539 fun->visit3 = TRUE;
2540
2541 if (emit_stack_syms)
2542 {
2543 struct spu_link_hash_table *htab = spu_hash_table (info);
2544 char *name = bfd_malloc (18 + strlen (f1));
2545 struct elf_link_hash_entry *h;
2546
2547 if (name != NULL)
2548 {
2549 if (fun->global || ELF_ST_BIND (fun->u.sym->st_info) == STB_GLOBAL)
2550 sprintf (name, "__stack_%s", f1);
2551 else
2552 sprintf (name, "__stack_%x_%s", fun->sec->id & 0xffffffff, f1);
2553
2554 h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE);
2555 free (name);
2556 if (h != NULL
2557 && (h->root.type == bfd_link_hash_new
2558 || h->root.type == bfd_link_hash_undefined
2559 || h->root.type == bfd_link_hash_undefweak))
2560 {
2561 h->root.type = bfd_link_hash_defined;
2562 h->root.u.def.section = bfd_abs_section_ptr;
2563 h->root.u.def.value = max_stack;
2564 h->size = 0;
2565 h->type = 0;
2566 h->ref_regular = 1;
2567 h->def_regular = 1;
2568 h->ref_regular_nonweak = 1;
2569 h->forced_local = 1;
2570 h->non_elf = 0;
2571 }
2572 }
2573 }
2574
2575 return max_stack;
2576}
2577
2578/* Provide an estimate of total stack required. */
2579
2580static bfd_boolean
2581spu_elf_stack_analysis (bfd *output_bfd,
2582 struct bfd_link_info *info,
2583 int emit_stack_syms)
2584{
2585 bfd *ibfd;
2586 bfd_vma max_stack = 0;
2587
2588 if (!discover_functions (output_bfd, info))
2589 return FALSE;
2590
2591 if (!build_call_tree (output_bfd, info))
2592 return FALSE;
2593
2594 info->callbacks->info (_("Stack size for call graph root nodes.\n"));
2595 info->callbacks->minfo (_("\nStack size for functions. "
2596 "Annotations: '*' max stack, 't' tail call\n"));
2597 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2598 {
2599 extern const bfd_target bfd_elf32_spu_vec;
2600 asection *sec;
2601
2602 if (ibfd->xvec != &bfd_elf32_spu_vec)
2603 continue;
2604
2605 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2606 {
2607 struct _spu_elf_section_data *sec_data;
2608 struct spu_elf_stack_info *sinfo;
2609
2610 if ((sec_data = spu_elf_section_data (sec)) != NULL
47f6dab9 2611 && (sinfo = sec_data->u.i.stack_info) != NULL)
49fa1e15
AM
2612 {
2613 int i;
2614 for (i = 0; i < sinfo->num_fun; ++i)
2615 {
2616 if (!sinfo->fun[i].non_root)
2617 {
2618 bfd_vma stack;
2619 const char *f1;
2620
2621 stack = sum_stack (&sinfo->fun[i], info,
2622 emit_stack_syms);
2623 f1 = func_name (&sinfo->fun[i]);
2624 info->callbacks->info (_(" %s: 0x%v\n"),
2625 f1, stack);
2626 if (max_stack < stack)
2627 max_stack = stack;
2628 }
2629 }
2630 }
2631 }
2632 }
2633
2634 info->callbacks->info (_("Maximum stack required is 0x%v\n"), max_stack);
2635 return TRUE;
2636}
2637
2638/* Perform a final link. */
2639
2640static bfd_boolean
2641spu_elf_final_link (bfd *output_bfd, struct bfd_link_info *info)
2642{
2643 struct spu_link_hash_table *htab = spu_hash_table (info);
2644
2645 if (htab->stack_analysis
2646 && !spu_elf_stack_analysis (output_bfd, info, htab->emit_stack_syms))
2647 info->callbacks->einfo ("%X%P: stack analysis error: %E\n");
2648
2649 return bfd_elf_final_link (output_bfd, info);
2650}
2651
ece5ef60
AM
2652/* Called when not normally emitting relocs, ie. !info->relocatable
2653 and !info->emitrelocations. Returns a count of special relocs
2654 that need to be emitted. */
2655
2656static unsigned int
2657spu_elf_count_relocs (asection *sec, Elf_Internal_Rela *relocs)
2658{
2659 unsigned int count = 0;
2660 Elf_Internal_Rela *relend = relocs + sec->reloc_count;
2661
2662 for (; relocs < relend; relocs++)
2663 {
2664 int r_type = ELF32_R_TYPE (relocs->r_info);
2665 if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
2666 ++count;
2667 }
2668
2669 return count;
2670}
2671
e9f53129
AM
2672/* Apply RELOCS to CONTENTS of INPUT_SECTION from INPUT_BFD. */
2673
d16c7321 2674static int
e9f53129
AM
2675spu_elf_relocate_section (bfd *output_bfd,
2676 struct bfd_link_info *info,
2677 bfd *input_bfd,
2678 asection *input_section,
2679 bfd_byte *contents,
2680 Elf_Internal_Rela *relocs,
2681 Elf_Internal_Sym *local_syms,
2682 asection **local_sections)
2683{
2684 Elf_Internal_Shdr *symtab_hdr;
2685 struct elf_link_hash_entry **sym_hashes;
2686 Elf_Internal_Rela *rel, *relend;
2687 struct spu_link_hash_table *htab;
d16c7321 2688 int ret = TRUE;
ece5ef60 2689 bfd_boolean emit_these_relocs = FALSE;
e9f53129 2690
e9f53129
AM
2691 htab = spu_hash_table (info);
2692 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2693 sym_hashes = (struct elf_link_hash_entry **) (elf_sym_hashes (input_bfd));
2694
2695 rel = relocs;
2696 relend = relocs + input_section->reloc_count;
2697 for (; rel < relend; rel++)
2698 {
2699 int r_type;
2700 reloc_howto_type *howto;
2701 unsigned long r_symndx;
2702 Elf_Internal_Sym *sym;
2703 asection *sec;
2704 struct elf_link_hash_entry *h;
2705 const char *sym_name;
2706 bfd_vma relocation;
2707 bfd_vma addend;
2708 bfd_reloc_status_type r;
2709 bfd_boolean unresolved_reloc;
2710 bfd_boolean warned;
49fa1e15 2711 bfd_boolean branch;
e9f53129
AM
2712
2713 r_symndx = ELF32_R_SYM (rel->r_info);
2714 r_type = ELF32_R_TYPE (rel->r_info);
ece5ef60
AM
2715 if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
2716 {
2717 emit_these_relocs = TRUE;
2718 continue;
2719 }
2720
e9f53129
AM
2721 howto = elf_howto_table + r_type;
2722 unresolved_reloc = FALSE;
2723 warned = FALSE;
e9f53129
AM
2724 h = NULL;
2725 sym = NULL;
2726 sec = NULL;
2727 if (r_symndx < symtab_hdr->sh_info)
2728 {
2729 sym = local_syms + r_symndx;
2730 sec = local_sections[r_symndx];
2731 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
2732 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2733 }
2734 else
2735 {
2736 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2737 r_symndx, symtab_hdr, sym_hashes,
2738 h, sec, relocation,
2739 unresolved_reloc, warned);
2740 sym_name = h->root.root.string;
2741 }
2742
ab96bf03
AM
2743 if (sec != NULL && elf_discarded_section (sec))
2744 {
2745 /* For relocs against symbols from removed linkonce sections,
2746 or sections discarded by a linker script, we just want the
2747 section contents zeroed. Avoid any special processing. */
2748 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2749 rel->r_info = 0;
2750 rel->r_addend = 0;
2751 continue;
2752 }
2753
2754 if (info->relocatable)
2755 continue;
2756
e9f53129
AM
2757 if (unresolved_reloc)
2758 {
2759 (*_bfd_error_handler)
2760 (_("%B(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
2761 input_bfd,
2762 bfd_get_section_name (input_bfd, input_section),
2763 (long) rel->r_offset,
2764 howto->name,
2765 sym_name);
2766 ret = FALSE;
2767 }
2768
2769 /* If this symbol is in an overlay area, we may need to relocate
2770 to the overlay stub. */
2771 addend = rel->r_addend;
49fa1e15
AM
2772 branch = (is_branch (contents + rel->r_offset)
2773 || is_hint (contents + rel->r_offset));
47f6dab9
AM
2774 if (htab->stub_sec != NULL
2775 && needs_ovl_stub (sym_name, sec, input_section, htab, branch)
2776 && (h == NULL
2777 || (h != htab->ovly_load && h != htab->ovly_return)))
e9f53129 2778 {
47f6dab9
AM
2779 unsigned int ovl = 0;
2780 struct got_entry *g, **head;
e9f53129 2781
47f6dab9
AM
2782 if (branch)
2783 ovl = (spu_elf_section_data (input_section->output_section)
2784 ->u.o.ovl_index);
e9f53129 2785
47f6dab9
AM
2786 if (h != NULL)
2787 head = &h->got.glist;
2788 else
2789 head = elf_local_got_ents (input_bfd) + r_symndx;
2790
2791 for (g = *head; g != NULL; g = g->next)
4a628337 2792 if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
47f6dab9
AM
2793 break;
2794 if (g == NULL)
2795 abort ();
2796
2797 relocation = g->stub_addr;
2798 addend = 0;
e9f53129
AM
2799 }
2800
2801 r = _bfd_final_link_relocate (howto,
2802 input_bfd,
2803 input_section,
2804 contents,
2805 rel->r_offset, relocation, addend);
2806
2807 if (r != bfd_reloc_ok)
2808 {
2809 const char *msg = (const char *) 0;
2810
2811 switch (r)
2812 {
2813 case bfd_reloc_overflow:
2814 if (!((*info->callbacks->reloc_overflow)
2815 (info, (h ? &h->root : NULL), sym_name, howto->name,
2816 (bfd_vma) 0, input_bfd, input_section, rel->r_offset)))
2817 return FALSE;
2818 break;
2819
2820 case bfd_reloc_undefined:
2821 if (!((*info->callbacks->undefined_symbol)
2822 (info, sym_name, input_bfd, input_section,
2823 rel->r_offset, TRUE)))
2824 return FALSE;
2825 break;
2826
2827 case bfd_reloc_outofrange:
2828 msg = _("internal error: out of range error");
2829 goto common_error;
2830
2831 case bfd_reloc_notsupported:
2832 msg = _("internal error: unsupported relocation error");
2833 goto common_error;
2834
2835 case bfd_reloc_dangerous:
2836 msg = _("internal error: dangerous error");
2837 goto common_error;
2838
2839 default:
2840 msg = _("internal error: unknown error");
2841 /* fall through */
2842
2843 common_error:
d16c7321 2844 ret = FALSE;
e9f53129
AM
2845 if (!((*info->callbacks->warning)
2846 (info, msg, sym_name, input_bfd, input_section,
2847 rel->r_offset)))
2848 return FALSE;
2849 break;
2850 }
2851 }
2852 }
2853
ece5ef60
AM
2854 if (ret
2855 && emit_these_relocs
2856 && !info->relocatable
2857 && !info->emitrelocations)
2858 {
2859 Elf_Internal_Rela *wrel;
2860 Elf_Internal_Shdr *rel_hdr;
2861
2862 wrel = rel = relocs;
2863 relend = relocs + input_section->reloc_count;
2864 for (; rel < relend; rel++)
2865 {
2866 int r_type;
2867
2868 r_type = ELF32_R_TYPE (rel->r_info);
2869 if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
2870 *wrel++ = *rel;
2871 }
2872 input_section->reloc_count = wrel - relocs;
2873 /* Backflips for _bfd_elf_link_output_relocs. */
2874 rel_hdr = &elf_section_data (input_section)->rel_hdr;
2875 rel_hdr->sh_size = input_section->reloc_count * rel_hdr->sh_entsize;
2876 ret = 2;
2877 }
2878
e9f53129
AM
2879 return ret;
2880}
2881
c1b2796f
AM
2882/* Adjust _SPUEAR_ syms to point at their overlay stubs. */
2883
2884static bfd_boolean
2885spu_elf_output_symbol_hook (struct bfd_link_info *info,
2886 const char *sym_name ATTRIBUTE_UNUSED,
2887 Elf_Internal_Sym *sym,
2888 asection *sym_sec ATTRIBUTE_UNUSED,
2889 struct elf_link_hash_entry *h)
2890{
2891 struct spu_link_hash_table *htab = spu_hash_table (info);
2892
2893 if (!info->relocatable
47f6dab9 2894 && htab->stub_sec != NULL
c1b2796f
AM
2895 && h != NULL
2896 && (h->root.type == bfd_link_hash_defined
2897 || h->root.type == bfd_link_hash_defweak)
2898 && h->def_regular
2899 && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0)
2900 {
4a628337 2901 struct got_entry *g;
c1b2796f 2902
4a628337
AM
2903 for (g = h->got.glist; g != NULL; g = g->next)
2904 if (g->addend == 0 && g->ovl == 0)
2905 {
2906 sym->st_shndx = (_bfd_elf_section_from_bfd_section
2907 (htab->stub_sec[0]->output_section->owner,
2908 htab->stub_sec[0]->output_section));
2909 sym->st_value = g->stub_addr;
2910 break;
2911 }
c1b2796f
AM
2912 }
2913
2914 return TRUE;
2915}
2916
e9f53129
AM
2917static int spu_plugin = 0;
2918
2919void
2920spu_elf_plugin (int val)
2921{
2922 spu_plugin = val;
2923}
2924
2925/* Set ELF header e_type for plugins. */
2926
2927static void
2928spu_elf_post_process_headers (bfd *abfd,
2929 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2930{
2931 if (spu_plugin)
2932 {
2933 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
2934
2935 i_ehdrp->e_type = ET_DYN;
2936 }
2937}
2938
2939/* We may add an extra PT_LOAD segment for .toe. We also need extra
2940 segments for overlays. */
2941
2942static int
2943spu_elf_additional_program_headers (bfd *abfd, struct bfd_link_info *info)
2944{
2945 struct spu_link_hash_table *htab = spu_hash_table (info);
2946 int extra = htab->num_overlays;
2947 asection *sec;
2948
2949 if (extra)
2950 ++extra;
2951
2952 sec = bfd_get_section_by_name (abfd, ".toe");
2953 if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
2954 ++extra;
2955
2956 return extra;
2957}
2958
2959/* Remove .toe section from other PT_LOAD segments and put it in
2960 a segment of its own. Put overlays in separate segments too. */
2961
2962static bfd_boolean
2963spu_elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
2964{
2965 asection *toe, *s;
2966 struct elf_segment_map *m;
2967 unsigned int i;
2968
2969 if (info == NULL)
2970 return TRUE;
2971
2972 toe = bfd_get_section_by_name (abfd, ".toe");
2973 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2974 if (m->p_type == PT_LOAD && m->count > 1)
2975 for (i = 0; i < m->count; i++)
2976 if ((s = m->sections[i]) == toe
47f6dab9 2977 || spu_elf_section_data (s)->u.o.ovl_index != 0)
e9f53129
AM
2978 {
2979 struct elf_segment_map *m2;
2980 bfd_vma amt;
2981
2982 if (i + 1 < m->count)
2983 {
2984 amt = sizeof (struct elf_segment_map);
2985 amt += (m->count - (i + 2)) * sizeof (m->sections[0]);
2986 m2 = bfd_zalloc (abfd, amt);
2987 if (m2 == NULL)
2988 return FALSE;
2989 m2->count = m->count - (i + 1);
2990 memcpy (m2->sections, m->sections + i + 1,
2991 m2->count * sizeof (m->sections[0]));
2992 m2->p_type = PT_LOAD;
2993 m2->next = m->next;
2994 m->next = m2;
2995 }
2996 m->count = 1;
2997 if (i != 0)
2998 {
2999 m->count = i;
3000 amt = sizeof (struct elf_segment_map);
3001 m2 = bfd_zalloc (abfd, amt);
3002 if (m2 == NULL)
3003 return FALSE;
3004 m2->p_type = PT_LOAD;
3005 m2->count = 1;
3006 m2->sections[0] = s;
3007 m2->next = m->next;
3008 m->next = m2;
3009 }
3010 break;
3011 }
3012
3013 return TRUE;
3014}
3015
3016/* Check that all loadable section VMAs lie in the range
3017 LO .. HI inclusive. */
3018
3019asection *
3020spu_elf_check_vma (bfd *abfd, bfd_vma lo, bfd_vma hi)
3021{
3022 struct elf_segment_map *m;
3023 unsigned int i;
3024
3025 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3026 if (m->p_type == PT_LOAD)
3027 for (i = 0; i < m->count; i++)
3028 if (m->sections[i]->size != 0
3029 && (m->sections[i]->vma < lo
3030 || m->sections[i]->vma > hi
3031 || m->sections[i]->vma + m->sections[i]->size - 1 > hi))
3032 return m->sections[i];
3033
3034 return NULL;
3035}
3036
7d3287cb
AM
3037/* Tweak the section type of .note.spu_name. */
3038
3039static bfd_boolean
3040spu_elf_fake_sections (bfd *obfd ATTRIBUTE_UNUSED,
3041 Elf_Internal_Shdr *hdr,
3042 asection *sec)
3043{
3044 if (strcmp (sec->name, SPU_PTNOTE_SPUNAME) == 0)
3045 hdr->sh_type = SHT_NOTE;
3046 return TRUE;
3047}
3048
e9f53129
AM
3049/* Tweak phdrs before writing them out. */
3050
3051static int
3052spu_elf_modify_program_headers (bfd *abfd, struct bfd_link_info *info)
3053{
3054 const struct elf_backend_data *bed;
3055 struct elf_obj_tdata *tdata;
3056 Elf_Internal_Phdr *phdr, *last;
3057 struct spu_link_hash_table *htab;
3058 unsigned int count;
3059 unsigned int i;
3060
3061 if (info == NULL)
3062 return TRUE;
3063
3064 bed = get_elf_backend_data (abfd);
3065 tdata = elf_tdata (abfd);
3066 phdr = tdata->phdr;
3067 count = tdata->program_header_size / bed->s->sizeof_phdr;
3068 htab = spu_hash_table (info);
3069 if (htab->num_overlays != 0)
3070 {
3071 struct elf_segment_map *m;
3072 unsigned int o;
3073
3074 for (i = 0, m = elf_tdata (abfd)->segment_map; m; ++i, m = m->next)
3075 if (m->count != 0
47f6dab9 3076 && (o = spu_elf_section_data (m->sections[0])->u.o.ovl_index) != 0)
e9f53129
AM
3077 {
3078 /* Mark this as an overlay header. */
3079 phdr[i].p_flags |= PF_OVERLAY;
3080
3081 if (htab->ovtab != NULL && htab->ovtab->size != 0)
3082 {
3083 bfd_byte *p = htab->ovtab->contents;
47f6dab9 3084 unsigned int off = o * 16 + 8;
e9f53129
AM
3085
3086 /* Write file_off into _ovly_table. */
3087 bfd_put_32 (htab->ovtab->owner, phdr[i].p_offset, p + off);
3088 }
3089 }
3090 }
3091
3092 /* Round up p_filesz and p_memsz of PT_LOAD segments to multiples
3093 of 16. This should always be possible when using the standard
3094 linker scripts, but don't create overlapping segments if
3095 someone is playing games with linker scripts. */
3096 last = NULL;
3097 for (i = count; i-- != 0; )
3098 if (phdr[i].p_type == PT_LOAD)
3099 {
3100 unsigned adjust;
3101
3102 adjust = -phdr[i].p_filesz & 15;
3103 if (adjust != 0
3104 && last != NULL
3105 && phdr[i].p_offset + phdr[i].p_filesz > last->p_offset - adjust)
3106 break;
3107
3108 adjust = -phdr[i].p_memsz & 15;
3109 if (adjust != 0
3110 && last != NULL
3111 && phdr[i].p_filesz != 0
3112 && phdr[i].p_vaddr + phdr[i].p_memsz > last->p_vaddr - adjust
3113 && phdr[i].p_vaddr + phdr[i].p_memsz <= last->p_vaddr)
3114 break;
3115
3116 if (phdr[i].p_filesz != 0)
3117 last = &phdr[i];
3118 }
3119
3120 if (i == (unsigned int) -1)
3121 for (i = count; i-- != 0; )
3122 if (phdr[i].p_type == PT_LOAD)
3123 {
3124 unsigned adjust;
3125
3126 adjust = -phdr[i].p_filesz & 15;
3127 phdr[i].p_filesz += adjust;
3128
3129 adjust = -phdr[i].p_memsz & 15;
3130 phdr[i].p_memsz += adjust;
3131 }
3132
3133 return TRUE;
3134}
3135
e9f53129
AM
3136#define TARGET_BIG_SYM bfd_elf32_spu_vec
3137#define TARGET_BIG_NAME "elf32-spu"
3138#define ELF_ARCH bfd_arch_spu
3139#define ELF_MACHINE_CODE EM_SPU
3140/* This matches the alignment need for DMA. */
3141#define ELF_MAXPAGESIZE 0x80
3142#define elf_backend_rela_normal 1
3143#define elf_backend_can_gc_sections 1
3144
3145#define bfd_elf32_bfd_reloc_type_lookup spu_elf_reloc_type_lookup
157090f7 3146#define bfd_elf32_bfd_reloc_name_lookup spu_elf_reloc_name_lookup
e9f53129 3147#define elf_info_to_howto spu_elf_info_to_howto
ece5ef60 3148#define elf_backend_count_relocs spu_elf_count_relocs
e9f53129
AM
3149#define elf_backend_relocate_section spu_elf_relocate_section
3150#define elf_backend_symbol_processing spu_elf_backend_symbol_processing
c1b2796f 3151#define elf_backend_link_output_symbol_hook spu_elf_output_symbol_hook
e9f53129
AM
3152#define bfd_elf32_new_section_hook spu_elf_new_section_hook
3153#define bfd_elf32_bfd_link_hash_table_create spu_elf_link_hash_table_create
e9f53129
AM
3154
3155#define elf_backend_additional_program_headers spu_elf_additional_program_headers
3156#define elf_backend_modify_segment_map spu_elf_modify_segment_map
3157#define elf_backend_modify_program_headers spu_elf_modify_program_headers
3158#define elf_backend_post_process_headers spu_elf_post_process_headers
7d3287cb 3159#define elf_backend_fake_sections spu_elf_fake_sections
e9f53129 3160#define elf_backend_special_sections spu_elf_special_sections
49fa1e15 3161#define bfd_elf32_bfd_final_link spu_elf_final_link
e9f53129
AM
3162
3163#include "elf32-target.h"
This page took 0.270676 seconds and 4 git commands to generate.