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