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