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