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