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