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