1 /* SPU specific support for 32-bit ELF
3 Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
22 #include "libiberty.h"
28 #include "elf32-spu.h"
30 /* We use RELA style relocs. Don't define USE_REL. */
32 static bfd_reloc_status_type spu_elf_rel9 (bfd *, arelent *, asymbol *,
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. */
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",
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 },
99 static enum elf_spu_reloc_type
100 spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code)
106 case BFD_RELOC_SPU_IMM10W:
108 case BFD_RELOC_SPU_IMM16W:
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:
116 case BFD_RELOC_SPU_PCREL16:
118 case BFD_RELOC_SPU_IMM7:
120 case BFD_RELOC_SPU_IMM8:
122 case BFD_RELOC_SPU_PCREL9a:
124 case BFD_RELOC_SPU_PCREL9b:
126 case BFD_RELOC_SPU_IMM10:
127 return R_SPU_ADDR10I;
128 case BFD_RELOC_SPU_IMM16:
129 return R_SPU_ADDR16I;
132 case BFD_RELOC_32_PCREL:
134 case BFD_RELOC_SPU_PPU32:
136 case BFD_RELOC_SPU_PPU64:
142 spu_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
144 Elf_Internal_Rela *dst)
146 enum elf_spu_reloc_type r_type;
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];
153 static reloc_howto_type *
154 spu_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
155 bfd_reloc_code_real_type code)
157 enum elf_spu_reloc_type r_type = spu_elf_bfd_to_reloc_type (code);
159 if (r_type == R_SPU_NONE)
162 return elf_howto_table + r_type;
165 static reloc_howto_type *
166 spu_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
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];
179 /* Apply R_SPU_REL9 and R_SPU_REL9I relocs. */
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)
186 bfd_size_type octets;
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
193 if (output_bfd != NULL)
194 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
195 input_section, output_bfd, error_message);
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);
201 /* Get symbol value. */
203 if (!bfd_is_com_section (symbol->section))
205 if (symbol->section->output_section)
206 val += symbol->section->output_section->vma;
208 val += reloc_entry->addend;
210 /* Make it pc-relative. */
211 val -= input_section->output_section->vma + input_section->output_offset;
214 if (val + 256 >= 512)
215 return bfd_reloc_overflow;
217 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
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);
229 spu_elf_new_section_hook (bfd *abfd, asection *sec)
231 if (!sec->used_by_bfd)
233 struct _spu_elf_section_data *sdata;
235 sdata = bfd_zalloc (abfd, sizeof (*sdata));
238 sec->used_by_bfd = sdata;
241 return _bfd_elf_new_section_hook (abfd, sec);
244 /* Set up overlay info for executables. */
247 spu_elf_object_p (bfd *abfd)
249 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
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;
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)
262 if (last_phdr == NULL
263 || ((last_phdr->p_vaddr ^ phdr->p_vaddr) & 0x3ffff) != 0)
266 for (j = 1; j < elf_numsections (abfd); j++)
268 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[j];
270 if (ELF_IS_SECTION_IN_SEGMENT_MEMORY (shdr, phdr))
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;
282 /* Specially mark defined symbols named _EAR_* with BSF_KEEP so that
283 strip --strip-unneeded will not remove them. */
286 spu_elf_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
288 if (sym->name != NULL
289 && sym->section != bfd_abs_section_ptr
290 && strncmp (sym->name, "_EAR_", 5) == 0)
291 sym->flags |= BSF_KEEP;
294 /* SPU ELF linker hash table. */
296 struct spu_link_hash_table
298 struct elf_link_hash_table elf;
300 struct spu_elf_params *params;
302 /* Shortcuts to overlay sections. */
308 /* Count of stubs in each overlay section. */
309 unsigned int *stub_count;
311 /* The stub section for each overlay section. */
314 struct elf_link_hash_entry *ovly_entry[2];
316 /* Number of overlay buffers. */
317 unsigned int num_buf;
319 /* Total number of overlays. */
320 unsigned int num_overlays;
322 /* For soft icache. */
323 unsigned int line_size_log2;
324 unsigned int num_lines_log2;
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;
342 unsigned int stub_err : 1;
345 /* Hijack the generic got fields for overlay stub accounting. */
349 struct got_entry *next;
358 #define spu_hash_table(p) \
359 ((struct spu_link_hash_table *) ((p)->hash))
363 struct function_info *fun;
364 struct call_info *next;
366 unsigned int max_depth;
367 unsigned int is_tail : 1;
368 unsigned int is_pasted : 1;
369 unsigned int priority : 13;
374 /* List of functions called. Also branches to hot/cold part of
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. */
381 Elf_Internal_Sym *sym;
382 struct elf_link_hash_entry *h;
384 /* Function section. */
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. */
392 /* Offset where we found a store of lr, or -1 if none found. */
394 /* Offset where we found the stack adjustment insn. */
398 /* Distance from root of call tree. Tail and hot/cold branches
399 count as one deeper. We aren't counting stack frames here. */
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;
420 struct spu_elf_stack_info
424 /* Variable size array describing functions, one per contiguous
425 address range belonging to a function. */
426 struct function_info fun[1];
429 static struct function_info *find_function (asection *, bfd_vma,
430 struct bfd_link_info *);
432 /* Create a spu ELF linker hash table. */
434 static struct bfd_link_hash_table *
435 spu_elf_link_hash_table_create (bfd *abfd)
437 struct spu_link_hash_table *htab;
439 htab = bfd_malloc (sizeof (*htab));
443 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd,
444 _bfd_elf_link_hash_newfunc,
445 sizeof (struct elf_link_hash_entry)))
451 memset (&htab->ovtab, 0,
452 sizeof (*htab) - offsetof (struct spu_link_hash_table, ovtab));
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;
462 spu_elf_setup (struct bfd_link_info *info, struct spu_elf_params *params)
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);
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. */
475 get_sym_h (struct elf_link_hash_entry **hp,
476 Elf_Internal_Sym **symp,
478 Elf_Internal_Sym **locsymsp,
479 unsigned long r_symndx,
482 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
484 if (r_symndx >= symtab_hdr->sh_info)
486 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
487 struct elf_link_hash_entry *h;
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;
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;
511 Elf_Internal_Sym *sym;
512 Elf_Internal_Sym *locsyms = *locsymsp;
516 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
518 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
520 0, NULL, NULL, NULL);
525 sym = locsyms + r_symndx;
534 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
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. */
544 spu_elf_create_sections (struct bfd_link_info *info)
548 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
549 if (bfd_get_section_by_name (ibfd, SPU_PTNOTE_SPUNAME) != NULL)
554 /* Make SPU_PTNOTE_SPUNAME section. */
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);
565 || !bfd_set_section_alignment (ibfd, s, 4))
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;
572 if (!bfd_set_section_size (ibfd, s, size))
575 data = bfd_zalloc (ibfd, size);
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);
591 /* qsort predicate to sort sections by vma. */
594 sort_sections (const void *a, const void *b)
596 const asection *const *s1 = a;
597 const asection *const *s2 = b;
598 bfd_signed_vma delta = (*s1)->vma - (*s2)->vma;
601 return delta < 0 ? -1 : 1;
603 return (*s1)->index - (*s2)->index;
606 /* Identify overlays in the output bfd, and number them.
607 Returns 0 on error, 1 if no overlays, 2 if overlays. */
610 spu_elf_find_overlays (struct bfd_link_info *info)
612 struct spu_link_hash_table *htab = spu_hash_table (info);
613 asection **alloc_sec;
614 unsigned int i, n, ovl_index, num_buf;
617 static const char *const entry_names[2][2] = {
618 { "__ovly_load", "__icache_br_handler" },
619 { "__ovly_return", "__icache_call_handler" }
622 if (info->output_bfd->section_count < 2)
626 = bfd_malloc (info->output_bfd->section_count * sizeof (*alloc_sec));
627 if (alloc_sec == NULL)
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
643 /* Sort them by vma. */
644 qsort (alloc_sec, n, sizeof (*alloc_sec), sort_sections);
646 ovl_end = alloc_sec[0]->vma + alloc_sec[0]->size;
647 if (htab->params->ovly_flavour == ovly_soft_icache)
649 /* Look for an overlapping vma to find the first overlay section. */
650 bfd_vma vma_start = 0;
651 bfd_vma lma_start = 0;
653 for (i = 1; i < n; i++)
656 if (s->vma < ovl_end)
658 asection *s0 = alloc_sec[i - 1];
660 if (strncmp (s0->name, ".ovl.init", 9) != 0)
666 << (htab->num_lines_log2 + htab->line_size_log2)));
671 ovl_end = s->vma + s->size;
674 /* Now find any sections within the cache area. */
675 for (ovl_index = 0, num_buf = 0; i < n; i++)
678 if (s->vma >= ovl_end)
681 /* A section in an overlay area called .ovl.init is not
682 an overlay, in the sense that it might be loaded in
683 by the overlay manager, but rather the initial
684 section contents for the overlay buffer. */
685 if (strncmp (s->name, ".ovl.init", 9) != 0)
687 num_buf = ((s->vma - vma_start) >> htab->line_size_log2) + 1;
688 if (((s->vma - vma_start) & (htab->params->line_size - 1))
689 || ((s->lma - lma_start) & (htab->params->line_size - 1)))
691 info->callbacks->einfo (_("%X%P: overlay section %A "
692 "does not start on a cache line.\n"),
694 bfd_set_error (bfd_error_bad_value);
697 else if (s->size > htab->params->line_size)
699 info->callbacks->einfo (_("%X%P: overlay section %A "
700 "is larger than a cache line.\n"),
702 bfd_set_error (bfd_error_bad_value);
706 alloc_sec[ovl_index++] = s;
707 spu_elf_section_data (s)->u.o.ovl_index
708 = ((s->lma - lma_start) >> htab->line_size_log2) + 1;
709 spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
713 /* Ensure there are no more overlay sections. */
717 if (s->vma < ovl_end)
719 info->callbacks->einfo (_("%X%P: overlay section %A "
720 "is not in cache area.\n"),
722 bfd_set_error (bfd_error_bad_value);
726 ovl_end = s->vma + s->size;
731 /* Look for overlapping vmas. Any with overlap must be overlays.
732 Count them. Also count the number of overlay regions. */
733 for (ovl_index = 0, num_buf = 0, i = 1; i < n; i++)
736 if (s->vma < ovl_end)
738 asection *s0 = alloc_sec[i - 1];
740 if (spu_elf_section_data (s0)->u.o.ovl_index == 0)
743 if (strncmp (s0->name, ".ovl.init", 9) != 0)
745 alloc_sec[ovl_index] = s0;
746 spu_elf_section_data (s0)->u.o.ovl_index = ++ovl_index;
747 spu_elf_section_data (s0)->u.o.ovl_buf = num_buf;
750 ovl_end = s->vma + s->size;
752 if (strncmp (s->name, ".ovl.init", 9) != 0)
754 alloc_sec[ovl_index] = s;
755 spu_elf_section_data (s)->u.o.ovl_index = ++ovl_index;
756 spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
757 if (s0->vma != s->vma)
759 info->callbacks->einfo (_("%X%P: overlay sections %A "
760 "and %A do not start at the "
763 bfd_set_error (bfd_error_bad_value);
766 if (ovl_end < s->vma + s->size)
767 ovl_end = s->vma + s->size;
771 ovl_end = s->vma + s->size;
775 htab->num_overlays = ovl_index;
776 htab->num_buf = num_buf;
777 htab->ovl_sec = alloc_sec;
782 for (i = 0; i < 2; i++)
785 struct elf_link_hash_entry *h;
787 name = entry_names[i][htab->params->ovly_flavour];
788 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
792 if (h->root.type == bfd_link_hash_new)
794 h->root.type = bfd_link_hash_undefined;
796 h->ref_regular_nonweak = 1;
799 htab->ovly_entry[i] = h;
805 /* Non-zero to use bra in overlay stubs rather than br. */
808 #define BRA 0x30000000
809 #define BRASL 0x31000000
810 #define BR 0x32000000
811 #define BRSL 0x33000000
812 #define NOP 0x40200000
813 #define LNOP 0x00200000
814 #define ILA 0x42000000
816 /* Return true for all relative and absolute branch instructions.
824 brhnz 00100011 0.. */
827 is_branch (const unsigned char *insn)
829 return (insn[0] & 0xec) == 0x20 && (insn[1] & 0x80) == 0;
832 /* Return true for all indirect branch instructions.
840 bihnz 00100101 011 */
843 is_indirect_branch (const unsigned char *insn)
845 return (insn[0] & 0xef) == 0x25 && (insn[1] & 0x80) == 0;
848 /* Return true for branch hint instructions.
853 is_hint (const unsigned char *insn)
855 return (insn[0] & 0xfc) == 0x10;
858 /* True if INPUT_SECTION might need overlay stubs. */
861 maybe_needs_stubs (asection *input_section)
863 /* No stubs for debug sections and suchlike. */
864 if ((input_section->flags & SEC_ALLOC) == 0)
867 /* No stubs for link-once sections that will be discarded. */
868 if (input_section->output_section == bfd_abs_section_ptr)
871 /* Don't create stubs for .eh_frame references. */
872 if (strcmp (input_section->name, ".eh_frame") == 0)
894 /* Return non-zero if this reloc symbol should go via an overlay stub.
895 Return 2 if the stub must be in non-overlay area. */
897 static enum _stub_type
898 needs_ovl_stub (struct elf_link_hash_entry *h,
899 Elf_Internal_Sym *sym,
901 asection *input_section,
902 Elf_Internal_Rela *irela,
904 struct bfd_link_info *info)
906 struct spu_link_hash_table *htab = spu_hash_table (info);
907 enum elf_spu_reloc_type r_type;
908 unsigned int sym_type;
909 bfd_boolean branch, hint, call;
910 enum _stub_type ret = no_stub;
914 || sym_sec->output_section == bfd_abs_section_ptr
915 || spu_elf_section_data (sym_sec->output_section) == NULL)
920 /* Ensure no stubs for user supplied overlay manager syms. */
921 if (h == htab->ovly_entry[0] || h == htab->ovly_entry[1])
924 /* setjmp always goes via an overlay stub, because then the return
925 and hence the longjmp goes via __ovly_return. That magically
926 makes setjmp/longjmp between overlays work. */
927 if (strncmp (h->root.root.string, "setjmp", 6) == 0
928 && (h->root.root.string[6] == '\0' || h->root.root.string[6] == '@'))
935 sym_type = ELF_ST_TYPE (sym->st_info);
937 r_type = ELF32_R_TYPE (irela->r_info);
941 if (r_type == R_SPU_REL16 || r_type == R_SPU_ADDR16)
943 if (contents == NULL)
946 if (!bfd_get_section_contents (input_section->owner,
953 contents += irela->r_offset;
955 branch = is_branch (contents);
956 hint = is_hint (contents);
959 call = (contents[0] & 0xfd) == 0x31;
961 && sym_type != STT_FUNC
964 /* It's common for people to write assembly and forget
965 to give function symbols the right type. Handle
966 calls to such symbols, but warn so that (hopefully)
967 people will fix their code. We need the symbol
968 type to be correct to distinguish function pointer
969 initialisation from other pointer initialisations. */
970 const char *sym_name;
973 sym_name = h->root.root.string;
976 Elf_Internal_Shdr *symtab_hdr;
977 symtab_hdr = &elf_tdata (input_section->owner)->symtab_hdr;
978 sym_name = bfd_elf_sym_name (input_section->owner,
983 (*_bfd_error_handler) (_("warning: call to non-function"
984 " symbol %s defined in %B"),
985 sym_sec->owner, sym_name);
991 if ((!branch && htab->params->ovly_flavour == ovly_soft_icache)
992 || (sym_type != STT_FUNC
994 && (sym_sec->flags & SEC_CODE) == 0))
997 /* Usually, symbols in non-overlay sections don't need stubs. */
998 if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index == 0
999 && !htab->params->non_overlay_stubs)
1002 /* A reference from some other section to a symbol in an overlay
1003 section needs a stub. */
1004 if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index
1005 != spu_elf_section_data (input_section->output_section)->u.o.ovl_index)
1007 if (call || sym_type == STT_FUNC)
1008 ret = call_ovl_stub;
1011 ret = br000_ovl_stub;
1015 unsigned int lrlive = (contents[1] & 0x70) >> 4;
1021 /* If this insn isn't a branch then we are possibly taking the
1022 address of a function and passing it out somehow. Soft-icache code
1023 always generates inline code to do indirect branches. */
1024 if (!(branch || hint)
1025 && sym_type == STT_FUNC
1026 && htab->params->ovly_flavour != ovly_soft_icache)
1033 count_stub (struct spu_link_hash_table *htab,
1036 enum _stub_type stub_type,
1037 struct elf_link_hash_entry *h,
1038 const Elf_Internal_Rela *irela)
1040 unsigned int ovl = 0;
1041 struct got_entry *g, **head;
1044 /* If this instruction is a branch or call, we need a stub
1045 for it. One stub per function per overlay.
1046 If it isn't a branch, then we are taking the address of
1047 this function so need a stub in the non-overlay area
1048 for it. One stub per function. */
1049 if (stub_type != nonovl_stub)
1050 ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index;
1053 head = &h->got.glist;
1056 if (elf_local_got_ents (ibfd) == NULL)
1058 bfd_size_type amt = (elf_tdata (ibfd)->symtab_hdr.sh_info
1059 * sizeof (*elf_local_got_ents (ibfd)));
1060 elf_local_got_ents (ibfd) = bfd_zmalloc (amt);
1061 if (elf_local_got_ents (ibfd) == NULL)
1064 head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
1067 if (htab->params->ovly_flavour == ovly_soft_icache)
1069 htab->stub_count[ovl] += 1;
1075 addend = irela->r_addend;
1079 struct got_entry *gnext;
1081 for (g = *head; g != NULL; g = g->next)
1082 if (g->addend == addend && g->ovl == 0)
1087 /* Need a new non-overlay area stub. Zap other stubs. */
1088 for (g = *head; g != NULL; g = gnext)
1091 if (g->addend == addend)
1093 htab->stub_count[g->ovl] -= 1;
1101 for (g = *head; g != NULL; g = g->next)
1102 if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
1108 g = bfd_malloc (sizeof *g);
1113 g->stub_addr = (bfd_vma) -1;
1117 htab->stub_count[ovl] += 1;
1123 /* Support two sizes of overlay stubs, a slower more compact stub of two
1124 intructions, and a faster stub of four instructions.
1125 Soft-icache stubs are four or eight words. */
1128 ovl_stub_size (struct spu_elf_params *params)
1130 return 16 << params->ovly_flavour >> params->compact_stub;
1134 ovl_stub_size_log2 (struct spu_elf_params *params)
1136 return 4 + params->ovly_flavour - params->compact_stub;
1139 /* Two instruction overlay stubs look like:
1141 brsl $75,__ovly_load
1142 .word target_ovl_and_address
1144 ovl_and_address is a word with the overlay number in the top 14 bits
1145 and local store address in the bottom 18 bits.
1147 Four instruction overlay stubs look like:
1151 ila $79,target_address
1154 Software icache stubs are:
1158 .word lrlive_branchlocalstoreaddr;
1159 brasl $75,__icache_br_handler
1164 build_stub (struct bfd_link_info *info,
1167 enum _stub_type stub_type,
1168 struct elf_link_hash_entry *h,
1169 const Elf_Internal_Rela *irela,
1173 struct spu_link_hash_table *htab = spu_hash_table (info);
1174 unsigned int ovl, dest_ovl, set_id;
1175 struct got_entry *g, **head;
1177 bfd_vma addend, from, to, br_dest, patt;
1178 unsigned int lrlive;
1181 if (stub_type != nonovl_stub)
1182 ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index;
1185 head = &h->got.glist;
1187 head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
1191 addend = irela->r_addend;
1193 if (htab->params->ovly_flavour == ovly_soft_icache)
1195 g = bfd_malloc (sizeof *g);
1201 g->br_addr = (irela->r_offset
1202 + isec->output_offset
1203 + isec->output_section->vma);
1209 for (g = *head; g != NULL; g = g->next)
1210 if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
1215 if (g->ovl == 0 && ovl != 0)
1218 if (g->stub_addr != (bfd_vma) -1)
1222 sec = htab->stub_sec[ovl];
1223 dest += dest_sec->output_offset + dest_sec->output_section->vma;
1224 from = sec->size + sec->output_offset + sec->output_section->vma;
1225 g->stub_addr = from;
1226 to = (htab->ovly_entry[0]->root.u.def.value
1227 + htab->ovly_entry[0]->root.u.def.section->output_offset
1228 + htab->ovly_entry[0]->root.u.def.section->output_section->vma);
1230 if (((dest | to | from) & 3) != 0)
1235 dest_ovl = spu_elf_section_data (dest_sec->output_section)->u.o.ovl_index;
1237 if (htab->params->ovly_flavour == ovly_normal
1238 && !htab->params->compact_stub)
1240 bfd_put_32 (sec->owner, ILA + ((dest_ovl << 7) & 0x01ffff80) + 78,
1241 sec->contents + sec->size);
1242 bfd_put_32 (sec->owner, LNOP,
1243 sec->contents + sec->size + 4);
1244 bfd_put_32 (sec->owner, ILA + ((dest << 7) & 0x01ffff80) + 79,
1245 sec->contents + sec->size + 8);
1247 bfd_put_32 (sec->owner, BR + (((to - (from + 12)) << 5) & 0x007fff80),
1248 sec->contents + sec->size + 12);
1250 bfd_put_32 (sec->owner, BRA + ((to << 5) & 0x007fff80),
1251 sec->contents + sec->size + 12);
1253 else if (htab->params->ovly_flavour == ovly_normal
1254 && htab->params->compact_stub)
1257 bfd_put_32 (sec->owner, BRSL + (((to - from) << 5) & 0x007fff80) + 75,
1258 sec->contents + sec->size);
1260 bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
1261 sec->contents + sec->size);
1262 bfd_put_32 (sec->owner, (dest & 0x3ffff) | (dest_ovl << 18),
1263 sec->contents + sec->size + 4);
1265 else if (htab->params->ovly_flavour == ovly_soft_icache)
1268 if (stub_type == nonovl_stub)
1270 else if (stub_type == call_ovl_stub)
1271 /* A brsl makes lr live and *(*sp+16) is live.
1272 Tail calls have the same liveness. */
1274 else if (!htab->params->lrlive_analysis)
1275 /* Assume stack frame and lr save. */
1277 else if (irela != NULL)
1279 /* Analyse branch instructions. */
1280 struct function_info *caller;
1283 caller = find_function (isec, irela->r_offset, info);
1284 if (caller->start == NULL)
1285 off = irela->r_offset;
1288 struct function_info *found = NULL;
1290 /* Find the earliest piece of this function that
1291 has frame adjusting instructions. We might
1292 see dynamic frame adjustment (eg. for alloca)
1293 in some later piece, but functions using
1294 alloca always set up a frame earlier. Frame
1295 setup instructions are always in one piece. */
1296 if (caller->lr_store != (bfd_vma) -1
1297 || caller->sp_adjust != (bfd_vma) -1)
1299 while (caller->start != NULL)
1301 caller = caller->start;
1302 if (caller->lr_store != (bfd_vma) -1
1303 || caller->sp_adjust != (bfd_vma) -1)
1311 if (off > caller->sp_adjust)
1313 if (off > caller->lr_store)
1314 /* Only *(*sp+16) is live. */
1317 /* If no lr save, then we must be in a
1318 leaf function with a frame.
1319 lr is still live. */
1322 else if (off > caller->lr_store)
1324 /* Between lr save and stack adjust. */
1326 /* This should never happen since prologues won't
1331 /* On entry to function. */
1334 if (stub_type != br000_ovl_stub
1335 && lrlive != stub_type - br000_ovl_stub)
1336 info->callbacks->einfo (_("%A:0x%v lrlive .brinfo (%u) differs "
1337 "from analysis (%u)\n"),
1338 isec, irela->r_offset, lrlive,
1339 stub_type - br000_ovl_stub);
1342 /* If given lrlive info via .brinfo, use it. */
1343 if (stub_type > br000_ovl_stub)
1344 lrlive = stub_type - br000_ovl_stub;
1347 to = (htab->ovly_entry[1]->root.u.def.value
1348 + htab->ovly_entry[1]->root.u.def.section->output_offset
1349 + htab->ovly_entry[1]->root.u.def.section->output_section->vma);
1351 if (!htab->params->compact_stub)
1353 /* The branch that uses this stub goes to stub_addr + 12. We'll
1354 set up an xor pattern that can be used by the icache manager
1355 to modify this branch to go directly to its destination. */
1357 br_dest = g->stub_addr;
1360 /* Except in the case of _SPUEAR_ stubs, the branch in
1361 question is the one in the stub itself. */
1362 BFD_ASSERT (stub_type == nonovl_stub);
1363 g->br_addr = g->stub_addr;
1367 bfd_put_32 (sec->owner, dest_ovl - 1,
1368 sec->contents + sec->size + 0);
1369 set_id = ((dest_ovl - 1) >> htab->num_lines_log2) + 1;
1370 bfd_put_32 (sec->owner, (set_id << 18) | (dest & 0x3ffff),
1371 sec->contents + sec->size + 4);
1372 bfd_put_32 (sec->owner, (lrlive << 29) | (g->br_addr & 0x3ffff),
1373 sec->contents + sec->size + 8);
1374 bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
1375 sec->contents + sec->size + 12);
1376 patt = dest ^ br_dest;
1377 if (irela != NULL && ELF32_R_TYPE (irela->r_info) == R_SPU_REL16)
1378 patt = (dest - g->br_addr) ^ (br_dest - g->br_addr);
1379 bfd_put_32 (sec->owner, (patt << 5) & 0x007fff80,
1380 sec->contents + sec->size + 16 + (g->br_addr & 0xf));
1385 br_dest = g->stub_addr;
1388 BFD_ASSERT (stub_type == nonovl_stub);
1389 g->br_addr = g->stub_addr;
1393 set_id = ((dest_ovl - 1) >> htab->num_lines_log2) + 1;
1394 bfd_put_32 (sec->owner, (set_id << 18) | (dest & 0x3ffff),
1395 sec->contents + sec->size);
1396 bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
1397 sec->contents + sec->size + 4);
1398 bfd_put_32 (sec->owner, (lrlive << 29) | (g->br_addr & 0x3ffff),
1399 sec->contents + sec->size + 8);
1400 patt = dest ^ br_dest;
1401 if (irela != NULL && ELF32_R_TYPE (irela->r_info) == R_SPU_REL16)
1402 patt = (dest - g->br_addr) ^ (br_dest - g->br_addr);
1403 bfd_put_32 (sec->owner, (patt << 5) & 0x007fff80,
1404 sec->contents + sec->size + 12);
1408 /* Extra space for linked list entries. */
1414 sec->size += ovl_stub_size (htab->params);
1416 if (htab->params->emit_stub_syms)
1422 len = 8 + sizeof (".ovl_call.") - 1;
1424 len += strlen (h->root.root.string);
1429 add = (int) irela->r_addend & 0xffffffff;
1432 name = bfd_malloc (len);
1436 sprintf (name, "%08x.ovl_call.", g->ovl);
1438 strcpy (name + 8 + sizeof (".ovl_call.") - 1, h->root.root.string);
1440 sprintf (name + 8 + sizeof (".ovl_call.") - 1, "%x:%x",
1441 dest_sec->id & 0xffffffff,
1442 (int) ELF32_R_SYM (irela->r_info) & 0xffffffff);
1444 sprintf (name + len - 9, "+%x", add);
1446 h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE);
1450 if (h->root.type == bfd_link_hash_new)
1452 h->root.type = bfd_link_hash_defined;
1453 h->root.u.def.section = sec;
1454 h->size = ovl_stub_size (htab->params);
1455 h->root.u.def.value = sec->size - h->size;
1459 h->ref_regular_nonweak = 1;
1460 h->forced_local = 1;
1468 /* Called via elf_link_hash_traverse to allocate stubs for any _SPUEAR_
1472 allocate_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
1474 /* Symbols starting with _SPUEAR_ need a stub because they may be
1475 invoked by the PPU. */
1476 struct bfd_link_info *info = inf;
1477 struct spu_link_hash_table *htab = spu_hash_table (info);
1480 if ((h->root.type == bfd_link_hash_defined
1481 || h->root.type == bfd_link_hash_defweak)
1483 && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0
1484 && (sym_sec = h->root.u.def.section) != NULL
1485 && sym_sec->output_section != bfd_abs_section_ptr
1486 && spu_elf_section_data (sym_sec->output_section) != NULL
1487 && (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index != 0
1488 || htab->params->non_overlay_stubs))
1490 return count_stub (htab, NULL, NULL, nonovl_stub, h, NULL);
1497 build_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
1499 /* Symbols starting with _SPUEAR_ need a stub because they may be
1500 invoked by the PPU. */
1501 struct bfd_link_info *info = inf;
1502 struct spu_link_hash_table *htab = spu_hash_table (info);
1505 if ((h->root.type == bfd_link_hash_defined
1506 || h->root.type == bfd_link_hash_defweak)
1508 && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0
1509 && (sym_sec = h->root.u.def.section) != NULL
1510 && sym_sec->output_section != bfd_abs_section_ptr
1511 && spu_elf_section_data (sym_sec->output_section) != NULL
1512 && (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index != 0
1513 || htab->params->non_overlay_stubs))
1515 return build_stub (info, NULL, NULL, nonovl_stub, h, NULL,
1516 h->root.u.def.value, sym_sec);
1522 /* Size or build stubs. */
1525 process_stubs (struct bfd_link_info *info, bfd_boolean build)
1527 struct spu_link_hash_table *htab = spu_hash_table (info);
1530 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1532 extern const bfd_target bfd_elf32_spu_vec;
1533 Elf_Internal_Shdr *symtab_hdr;
1535 Elf_Internal_Sym *local_syms = NULL;
1537 if (ibfd->xvec != &bfd_elf32_spu_vec)
1540 /* We'll need the symbol table in a second. */
1541 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1542 if (symtab_hdr->sh_info == 0)
1545 /* Walk over each section attached to the input bfd. */
1546 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
1548 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1550 /* If there aren't any relocs, then there's nothing more to do. */
1551 if ((isec->flags & SEC_RELOC) == 0
1552 || isec->reloc_count == 0)
1555 if (!maybe_needs_stubs (isec))
1558 /* Get the relocs. */
1559 internal_relocs = _bfd_elf_link_read_relocs (ibfd, isec, NULL, NULL,
1561 if (internal_relocs == NULL)
1562 goto error_ret_free_local;
1564 /* Now examine each relocation. */
1565 irela = internal_relocs;
1566 irelaend = irela + isec->reloc_count;
1567 for (; irela < irelaend; irela++)
1569 enum elf_spu_reloc_type r_type;
1570 unsigned int r_indx;
1572 Elf_Internal_Sym *sym;
1573 struct elf_link_hash_entry *h;
1574 enum _stub_type stub_type;
1576 r_type = ELF32_R_TYPE (irela->r_info);
1577 r_indx = ELF32_R_SYM (irela->r_info);
1579 if (r_type >= R_SPU_max)
1581 bfd_set_error (bfd_error_bad_value);
1582 error_ret_free_internal:
1583 if (elf_section_data (isec)->relocs != internal_relocs)
1584 free (internal_relocs);
1585 error_ret_free_local:
1586 if (local_syms != NULL
1587 && (symtab_hdr->contents
1588 != (unsigned char *) local_syms))
1593 /* Determine the reloc target section. */
1594 if (!get_sym_h (&h, &sym, &sym_sec, &local_syms, r_indx, ibfd))
1595 goto error_ret_free_internal;
1597 stub_type = needs_ovl_stub (h, sym, sym_sec, isec, irela,
1599 if (stub_type == no_stub)
1601 else if (stub_type == stub_error)
1602 goto error_ret_free_internal;
1604 if (htab->stub_count == NULL)
1607 amt = (htab->num_overlays + 1) * sizeof (*htab->stub_count);
1608 htab->stub_count = bfd_zmalloc (amt);
1609 if (htab->stub_count == NULL)
1610 goto error_ret_free_internal;
1615 if (!count_stub (htab, ibfd, isec, stub_type, h, irela))
1616 goto error_ret_free_internal;
1623 dest = h->root.u.def.value;
1625 dest = sym->st_value;
1626 dest += irela->r_addend;
1627 if (!build_stub (info, ibfd, isec, stub_type, h, irela,
1629 goto error_ret_free_internal;
1633 /* We're done with the internal relocs, free them. */
1634 if (elf_section_data (isec)->relocs != internal_relocs)
1635 free (internal_relocs);
1638 if (local_syms != NULL
1639 && symtab_hdr->contents != (unsigned char *) local_syms)
1641 if (!info->keep_memory)
1644 symtab_hdr->contents = (unsigned char *) local_syms;
1651 /* Allocate space for overlay call and return stubs.
1652 Return 0 on error, 1 if no stubs, 2 otherwise. */
1655 spu_elf_size_stubs (struct bfd_link_info *info)
1657 struct spu_link_hash_table *htab;
1664 if (!process_stubs (info, FALSE))
1667 htab = spu_hash_table (info);
1668 elf_link_hash_traverse (&htab->elf, allocate_spuear_stubs, info);
1672 if (htab->stub_count == NULL)
1675 ibfd = info->input_bfds;
1676 amt = (htab->num_overlays + 1) * sizeof (*htab->stub_sec);
1677 htab->stub_sec = bfd_zmalloc (amt);
1678 if (htab->stub_sec == NULL)
1681 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
1682 | SEC_HAS_CONTENTS | SEC_IN_MEMORY);
1683 stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
1684 htab->stub_sec[0] = stub;
1686 || !bfd_set_section_alignment (ibfd, stub,
1687 ovl_stub_size_log2 (htab->params)))
1689 stub->size = htab->stub_count[0] * ovl_stub_size (htab->params);
1690 if (htab->params->ovly_flavour == ovly_soft_icache)
1691 /* Extra space for linked list entries. */
1692 stub->size += htab->stub_count[0] * 16;
1694 for (i = 0; i < htab->num_overlays; ++i)
1696 asection *osec = htab->ovl_sec[i];
1697 unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
1698 stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
1699 htab->stub_sec[ovl] = stub;
1701 || !bfd_set_section_alignment (ibfd, stub,
1702 ovl_stub_size_log2 (htab->params)))
1704 stub->size = htab->stub_count[ovl] * ovl_stub_size (htab->params);
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))
1714 if (htab->params->ovly_flavour == ovly_soft_icache)
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);
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))
1727 htab->init->size = 16;
1731 /* htab->ovtab consists of two arrays.
1741 . } _ovly_buf_table[];
1744 htab->ovtab->size = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
1747 htab->toe = bfd_make_section_anyway_with_flags (ibfd, ".toe", SEC_ALLOC);
1748 if (htab->toe == NULL
1749 || !bfd_set_section_alignment (ibfd, htab->toe, 4))
1751 htab->toe->size = 16;
1756 /* Called from ld to place overlay manager data sections. This is done
1757 after the overlay manager itself is loaded, mainly so that the
1758 linker's htab->init section is placed after any other .ovl.init
1762 spu_elf_place_overlay_data (struct bfd_link_info *info)
1764 struct spu_link_hash_table *htab = spu_hash_table (info);
1768 if (htab->stub_count == NULL)
1771 (*htab->params->place_spu_section) (htab->stub_sec[0], NULL, ".text");
1773 for (i = 0; i < htab->num_overlays; ++i)
1775 asection *osec = htab->ovl_sec[i];
1776 unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
1777 (*htab->params->place_spu_section) (htab->stub_sec[ovl], osec, NULL);
1780 if (htab->params->ovly_flavour == ovly_soft_icache)
1781 (*htab->params->place_spu_section) (htab->init, NULL, ".ovl.init");
1784 if (htab->params->ovly_flavour == ovly_soft_icache)
1785 ovout = ".data.icache";
1786 (*htab->params->place_spu_section) (htab->ovtab, NULL, ovout);
1788 (*htab->params->place_spu_section) (htab->toe, NULL, ".toe");
1791 /* Functions to handle embedded spu_ovl.o object. */
1794 ovl_mgr_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *stream)
1800 ovl_mgr_pread (struct bfd *abfd ATTRIBUTE_UNUSED,
1806 struct _ovl_stream *os;
1810 os = (struct _ovl_stream *) stream;
1811 max = (const char *) os->end - (const char *) os->start;
1813 if ((ufile_ptr) offset >= max)
1817 if (count > max - offset)
1818 count = max - offset;
1820 memcpy (buf, (const char *) os->start + offset, count);
1825 spu_elf_open_builtin_lib (bfd **ovl_bfd, const struct _ovl_stream *stream)
1827 *ovl_bfd = bfd_openr_iovec ("builtin ovl_mgr",
1834 return *ovl_bfd != NULL;
1838 overlay_index (asection *sec)
1841 || sec->output_section == bfd_abs_section_ptr)
1843 return spu_elf_section_data (sec->output_section)->u.o.ovl_index;
1846 /* Define an STT_OBJECT symbol. */
1848 static struct elf_link_hash_entry *
1849 define_ovtab_symbol (struct spu_link_hash_table *htab, const char *name)
1851 struct elf_link_hash_entry *h;
1853 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
1857 if (h->root.type != bfd_link_hash_defined
1860 h->root.type = bfd_link_hash_defined;
1861 h->root.u.def.section = htab->ovtab;
1862 h->type = STT_OBJECT;
1865 h->ref_regular_nonweak = 1;
1868 else if (h->root.u.def.section->owner != NULL)
1870 (*_bfd_error_handler) (_("%B is not allowed to define %s"),
1871 h->root.u.def.section->owner,
1872 h->root.root.string);
1873 bfd_set_error (bfd_error_bad_value);
1878 (*_bfd_error_handler) (_("you are not allowed to define %s in a script"),
1879 h->root.root.string);
1880 bfd_set_error (bfd_error_bad_value);
1887 /* Fill in all stubs and the overlay tables. */
1890 spu_elf_build_stubs (struct bfd_link_info *info)
1892 struct spu_link_hash_table *htab = spu_hash_table (info);
1893 struct elf_link_hash_entry *h;
1899 if (htab->stub_count == NULL)
1902 for (i = 0; i <= htab->num_overlays; i++)
1903 if (htab->stub_sec[i]->size != 0)
1905 htab->stub_sec[i]->contents = bfd_zalloc (htab->stub_sec[i]->owner,
1906 htab->stub_sec[i]->size);
1907 if (htab->stub_sec[i]->contents == NULL)
1909 htab->stub_sec[i]->rawsize = htab->stub_sec[i]->size;
1910 htab->stub_sec[i]->size = 0;
1913 for (i = 0; i < 2; i++)
1915 h = htab->ovly_entry[i];
1916 BFD_ASSERT (h != NULL);
1918 if ((h->root.type == bfd_link_hash_defined
1919 || h->root.type == bfd_link_hash_defweak)
1922 s = h->root.u.def.section->output_section;
1923 if (spu_elf_section_data (s)->u.o.ovl_index)
1925 (*_bfd_error_handler) (_("%s in overlay section"),
1926 h->root.root.string);
1927 bfd_set_error (bfd_error_bad_value);
1935 /* Fill in all the stubs. */
1936 process_stubs (info, TRUE);
1937 if (!htab->stub_err)
1938 elf_link_hash_traverse (&htab->elf, build_spuear_stubs, info);
1942 (*_bfd_error_handler) (_("overlay stub relocation overflow"));
1943 bfd_set_error (bfd_error_bad_value);
1947 for (i = 0; i <= htab->num_overlays; i++)
1949 if (htab->stub_sec[i]->size != htab->stub_sec[i]->rawsize)
1951 (*_bfd_error_handler) (_("stubs don't match calculated size"));
1952 bfd_set_error (bfd_error_bad_value);
1955 htab->stub_sec[i]->rawsize = 0;
1958 if (htab->ovtab == NULL || htab->ovtab->size == 0)
1961 htab->ovtab->contents = bfd_zalloc (htab->ovtab->owner, htab->ovtab->size);
1962 if (htab->ovtab->contents == NULL)
1965 p = htab->ovtab->contents;
1966 if (htab->params->ovly_flavour == ovly_soft_icache)
1968 bfd_vma off, icache_base, linklist;
1970 h = define_ovtab_symbol (htab, "__icache_tag_array");
1973 h->root.u.def.value = 0;
1974 h->size = 16 << htab->num_lines_log2;
1977 h = define_ovtab_symbol (htab, "__icache_tag_array_size");
1980 h->root.u.def.value = 16 << htab->num_lines_log2;
1981 h->root.u.def.section = bfd_abs_section_ptr;
1983 icache_base = htab->ovl_sec[0]->vma;
1984 linklist = (htab->ovtab->output_section->vma
1985 + htab->ovtab->output_offset
1987 for (i = 0; i < htab->params->num_lines; i++)
1989 bfd_vma line_end = icache_base + ((i + 1) << htab->line_size_log2);
1990 bfd_vma stub_base = line_end - htab->params->max_branch * 32;
1991 bfd_vma link_elem = linklist + i * htab->params->max_branch * 16;
1992 bfd_vma locator = link_elem - stub_base / 2;
1994 bfd_put_32 (htab->ovtab->owner, locator, p + 4);
1995 bfd_put_16 (htab->ovtab->owner, link_elem, p + 8);
1996 bfd_put_16 (htab->ovtab->owner, link_elem, p + 10);
1997 bfd_put_16 (htab->ovtab->owner, link_elem, p + 12);
1998 bfd_put_16 (htab->ovtab->owner, link_elem, p + 14);
2002 h = define_ovtab_symbol (htab, "__icache_linked_list");
2005 h->root.u.def.value = off;
2006 h->size = htab->params->max_branch << (htab->num_lines_log2 + 4);
2010 h = define_ovtab_symbol (htab, "__icache_base");
2013 h->root.u.def.value = htab->ovl_sec[0]->vma;
2014 h->root.u.def.section = bfd_abs_section_ptr;
2015 h->size = htab->num_buf << htab->line_size_log2;
2017 h = define_ovtab_symbol (htab, "__icache_neg_log2_linesize");
2020 h->root.u.def.value = -htab->line_size_log2;
2021 h->root.u.def.section = bfd_abs_section_ptr;
2023 if (htab->init != NULL && htab->init->size != 0)
2025 htab->init->contents = bfd_zalloc (htab->init->owner,
2027 if (htab->init->contents == NULL)
2030 h = define_ovtab_symbol (htab, "__icache_fileoff");
2033 h->root.u.def.value = 0;
2034 h->root.u.def.section = htab->init;
2040 /* Write out _ovly_table. */
2041 /* set low bit of .size to mark non-overlay area as present. */
2043 obfd = htab->ovtab->output_section->owner;
2044 for (s = obfd->sections; s != NULL; s = s->next)
2046 unsigned int ovl_index = spu_elf_section_data (s)->u.o.ovl_index;
2050 unsigned long off = ovl_index * 16;
2051 unsigned int ovl_buf = spu_elf_section_data (s)->u.o.ovl_buf;
2053 bfd_put_32 (htab->ovtab->owner, s->vma, p + off);
2054 bfd_put_32 (htab->ovtab->owner, (s->size + 15) & -16,
2056 /* file_off written later in spu_elf_modify_program_headers. */
2057 bfd_put_32 (htab->ovtab->owner, ovl_buf, p + off + 12);
2061 h = define_ovtab_symbol (htab, "_ovly_table");
2064 h->root.u.def.value = 16;
2065 h->size = htab->num_overlays * 16;
2067 h = define_ovtab_symbol (htab, "_ovly_table_end");
2070 h->root.u.def.value = htab->num_overlays * 16 + 16;
2073 h = define_ovtab_symbol (htab, "_ovly_buf_table");
2076 h->root.u.def.value = htab->num_overlays * 16 + 16;
2077 h->size = htab->num_buf * 4;
2079 h = define_ovtab_symbol (htab, "_ovly_buf_table_end");
2082 h->root.u.def.value = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
2086 h = define_ovtab_symbol (htab, "_EAR_");
2089 h->root.u.def.section = htab->toe;
2090 h->root.u.def.value = 0;
2096 /* Check that all loadable section VMAs lie in the range
2097 LO .. HI inclusive, and stash some parameters for --auto-overlay. */
2100 spu_elf_check_vma (struct bfd_link_info *info)
2102 struct elf_segment_map *m;
2104 struct spu_link_hash_table *htab = spu_hash_table (info);
2105 bfd *abfd = info->output_bfd;
2106 bfd_vma hi = htab->params->local_store_hi;
2107 bfd_vma lo = htab->params->local_store_lo;
2109 htab->local_store = hi + 1 - lo;
2111 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2112 if (m->p_type == PT_LOAD)
2113 for (i = 0; i < m->count; i++)
2114 if (m->sections[i]->size != 0
2115 && (m->sections[i]->vma < lo
2116 || m->sections[i]->vma > hi
2117 || m->sections[i]->vma + m->sections[i]->size - 1 > hi))
2118 return m->sections[i];
2123 /* OFFSET in SEC (presumably) is the beginning of a function prologue.
2124 Search for stack adjusting insns, and return the sp delta.
2125 If a store of lr is found save the instruction offset to *LR_STORE.
2126 If a stack adjusting instruction is found, save that offset to
2130 find_function_stack_adjust (asection *sec,
2137 memset (reg, 0, sizeof (reg));
2138 for ( ; offset + 4 <= sec->size; offset += 4)
2140 unsigned char buf[4];
2144 /* Assume no relocs on stack adjusing insns. */
2145 if (!bfd_get_section_contents (sec->owner, sec, buf, offset, 4))
2149 ra = ((buf[2] & 0x3f) << 1) | (buf[3] >> 7);
2151 if (buf[0] == 0x24 /* stqd */)
2153 if (rt == 0 /* lr */ && ra == 1 /* sp */)
2158 /* Partly decoded immediate field. */
2159 imm = (buf[1] << 9) | (buf[2] << 1) | (buf[3] >> 7);
2161 if (buf[0] == 0x1c /* ai */)
2164 imm = (imm ^ 0x200) - 0x200;
2165 reg[rt] = reg[ra] + imm;
2167 if (rt == 1 /* sp */)
2171 *sp_adjust = offset;
2175 else if (buf[0] == 0x18 && (buf[1] & 0xe0) == 0 /* a */)
2177 int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6);
2179 reg[rt] = reg[ra] + reg[rb];
2184 *sp_adjust = offset;
2188 else if (buf[0] == 0x08 && (buf[1] & 0xe0) == 0 /* sf */)
2190 int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6);
2192 reg[rt] = reg[rb] - reg[ra];
2197 *sp_adjust = offset;
2201 else if ((buf[0] & 0xfc) == 0x40 /* il, ilh, ilhu, ila */)
2203 if (buf[0] >= 0x42 /* ila */)
2204 imm |= (buf[0] & 1) << 17;
2209 if (buf[0] == 0x40 /* il */)
2211 if ((buf[1] & 0x80) == 0)
2213 imm = (imm ^ 0x8000) - 0x8000;
2215 else if ((buf[1] & 0x80) == 0 /* ilhu */)
2221 else if (buf[0] == 0x60 && (buf[1] & 0x80) != 0 /* iohl */)
2223 reg[rt] |= imm & 0xffff;
2226 else if (buf[0] == 0x04 /* ori */)
2229 imm = (imm ^ 0x200) - 0x200;
2230 reg[rt] = reg[ra] | imm;
2233 else if (buf[0] == 0x32 && (buf[1] & 0x80) != 0 /* fsmbi */)
2235 reg[rt] = ( ((imm & 0x8000) ? 0xff000000 : 0)
2236 | ((imm & 0x4000) ? 0x00ff0000 : 0)
2237 | ((imm & 0x2000) ? 0x0000ff00 : 0)
2238 | ((imm & 0x1000) ? 0x000000ff : 0));
2241 else if (buf[0] == 0x16 /* andbi */)
2247 reg[rt] = reg[ra] & imm;
2250 else if (buf[0] == 0x33 && imm == 1 /* brsl .+4 */)
2252 /* Used in pic reg load. Say rt is trashed. Won't be used
2253 in stack adjust, but we need to continue past this branch. */
2257 else if (is_branch (buf) || is_indirect_branch (buf))
2258 /* If we hit a branch then we must be out of the prologue. */
2265 /* qsort predicate to sort symbols by section and value. */
2267 static Elf_Internal_Sym *sort_syms_syms;
2268 static asection **sort_syms_psecs;
2271 sort_syms (const void *a, const void *b)
2273 Elf_Internal_Sym *const *s1 = a;
2274 Elf_Internal_Sym *const *s2 = b;
2275 asection *sec1,*sec2;
2276 bfd_signed_vma delta;
2278 sec1 = sort_syms_psecs[*s1 - sort_syms_syms];
2279 sec2 = sort_syms_psecs[*s2 - sort_syms_syms];
2282 return sec1->index - sec2->index;
2284 delta = (*s1)->st_value - (*s2)->st_value;
2286 return delta < 0 ? -1 : 1;
2288 delta = (*s2)->st_size - (*s1)->st_size;
2290 return delta < 0 ? -1 : 1;
2292 return *s1 < *s2 ? -1 : 1;
2295 /* Allocate a struct spu_elf_stack_info with MAX_FUN struct function_info
2296 entries for section SEC. */
2298 static struct spu_elf_stack_info *
2299 alloc_stack_info (asection *sec, int max_fun)
2301 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2304 amt = sizeof (struct spu_elf_stack_info);
2305 amt += (max_fun - 1) * sizeof (struct function_info);
2306 sec_data->u.i.stack_info = bfd_zmalloc (amt);
2307 if (sec_data->u.i.stack_info != NULL)
2308 sec_data->u.i.stack_info->max_fun = max_fun;
2309 return sec_data->u.i.stack_info;
2312 /* Add a new struct function_info describing a (part of a) function
2313 starting at SYM_H. Keep the array sorted by address. */
2315 static struct function_info *
2316 maybe_insert_function (asection *sec,
2319 bfd_boolean is_func)
2321 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2322 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2328 sinfo = alloc_stack_info (sec, 20);
2335 Elf_Internal_Sym *sym = sym_h;
2336 off = sym->st_value;
2337 size = sym->st_size;
2341 struct elf_link_hash_entry *h = sym_h;
2342 off = h->root.u.def.value;
2346 for (i = sinfo->num_fun; --i >= 0; )
2347 if (sinfo->fun[i].lo <= off)
2352 /* Don't add another entry for an alias, but do update some
2354 if (sinfo->fun[i].lo == off)
2356 /* Prefer globals over local syms. */
2357 if (global && !sinfo->fun[i].global)
2359 sinfo->fun[i].global = TRUE;
2360 sinfo->fun[i].u.h = sym_h;
2363 sinfo->fun[i].is_func = TRUE;
2364 return &sinfo->fun[i];
2366 /* Ignore a zero-size symbol inside an existing function. */
2367 else if (sinfo->fun[i].hi > off && size == 0)
2368 return &sinfo->fun[i];
2371 if (sinfo->num_fun >= sinfo->max_fun)
2373 bfd_size_type amt = sizeof (struct spu_elf_stack_info);
2374 bfd_size_type old = amt;
2376 old += (sinfo->max_fun - 1) * sizeof (struct function_info);
2377 sinfo->max_fun += 20 + (sinfo->max_fun >> 1);
2378 amt += (sinfo->max_fun - 1) * sizeof (struct function_info);
2379 sinfo = bfd_realloc (sinfo, amt);
2382 memset ((char *) sinfo + old, 0, amt - old);
2383 sec_data->u.i.stack_info = sinfo;
2386 if (++i < sinfo->num_fun)
2387 memmove (&sinfo->fun[i + 1], &sinfo->fun[i],
2388 (sinfo->num_fun - i) * sizeof (sinfo->fun[i]));
2389 sinfo->fun[i].is_func = is_func;
2390 sinfo->fun[i].global = global;
2391 sinfo->fun[i].sec = sec;
2393 sinfo->fun[i].u.h = sym_h;
2395 sinfo->fun[i].u.sym = sym_h;
2396 sinfo->fun[i].lo = off;
2397 sinfo->fun[i].hi = off + size;
2398 sinfo->fun[i].lr_store = -1;
2399 sinfo->fun[i].sp_adjust = -1;
2400 sinfo->fun[i].stack = -find_function_stack_adjust (sec, off,
2401 &sinfo->fun[i].lr_store,
2402 &sinfo->fun[i].sp_adjust);
2403 sinfo->num_fun += 1;
2404 return &sinfo->fun[i];
2407 /* Return the name of FUN. */
2410 func_name (struct function_info *fun)
2414 Elf_Internal_Shdr *symtab_hdr;
2416 while (fun->start != NULL)
2420 return fun->u.h->root.root.string;
2423 if (fun->u.sym->st_name == 0)
2425 size_t len = strlen (sec->name);
2426 char *name = bfd_malloc (len + 10);
2429 sprintf (name, "%s+%lx", sec->name,
2430 (unsigned long) fun->u.sym->st_value & 0xffffffff);
2434 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2435 return bfd_elf_sym_name (ibfd, symtab_hdr, fun->u.sym, sec);
2438 /* Read the instruction at OFF in SEC. Return true iff the instruction
2439 is a nop, lnop, or stop 0 (all zero insn). */
2442 is_nop (asection *sec, bfd_vma off)
2444 unsigned char insn[4];
2446 if (off + 4 > sec->size
2447 || !bfd_get_section_contents (sec->owner, sec, insn, off, 4))
2449 if ((insn[0] & 0xbf) == 0 && (insn[1] & 0xe0) == 0x20)
2451 if (insn[0] == 0 && insn[1] == 0 && insn[2] == 0 && insn[3] == 0)
2456 /* Extend the range of FUN to cover nop padding up to LIMIT.
2457 Return TRUE iff some instruction other than a NOP was found. */
2460 insns_at_end (struct function_info *fun, bfd_vma limit)
2462 bfd_vma off = (fun->hi + 3) & -4;
2464 while (off < limit && is_nop (fun->sec, off))
2475 /* Check and fix overlapping function ranges. Return TRUE iff there
2476 are gaps in the current info we have about functions in SEC. */
2479 check_function_ranges (asection *sec, struct bfd_link_info *info)
2481 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2482 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2484 bfd_boolean gaps = FALSE;
2489 for (i = 1; i < sinfo->num_fun; i++)
2490 if (sinfo->fun[i - 1].hi > sinfo->fun[i].lo)
2492 /* Fix overlapping symbols. */
2493 const char *f1 = func_name (&sinfo->fun[i - 1]);
2494 const char *f2 = func_name (&sinfo->fun[i]);
2496 info->callbacks->einfo (_("warning: %s overlaps %s\n"), f1, f2);
2497 sinfo->fun[i - 1].hi = sinfo->fun[i].lo;
2499 else if (insns_at_end (&sinfo->fun[i - 1], sinfo->fun[i].lo))
2502 if (sinfo->num_fun == 0)
2506 if (sinfo->fun[0].lo != 0)
2508 if (sinfo->fun[sinfo->num_fun - 1].hi > sec->size)
2510 const char *f1 = func_name (&sinfo->fun[sinfo->num_fun - 1]);
2512 info->callbacks->einfo (_("warning: %s exceeds section size\n"), f1);
2513 sinfo->fun[sinfo->num_fun - 1].hi = sec->size;
2515 else if (insns_at_end (&sinfo->fun[sinfo->num_fun - 1], sec->size))
2521 /* Search current function info for a function that contains address
2522 OFFSET in section SEC. */
2524 static struct function_info *
2525 find_function (asection *sec, bfd_vma offset, struct bfd_link_info *info)
2527 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2528 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2532 hi = sinfo->num_fun;
2535 mid = (lo + hi) / 2;
2536 if (offset < sinfo->fun[mid].lo)
2538 else if (offset >= sinfo->fun[mid].hi)
2541 return &sinfo->fun[mid];
2543 info->callbacks->einfo (_("%A:0x%v not found in function table\n"),
2545 bfd_set_error (bfd_error_bad_value);
2549 /* Add CALLEE to CALLER call list if not already present. Return TRUE
2550 if CALLEE was new. If this function return FALSE, CALLEE should
2554 insert_callee (struct function_info *caller, struct call_info *callee)
2556 struct call_info **pp, *p;
2558 for (pp = &caller->call_list; (p = *pp) != NULL; pp = &p->next)
2559 if (p->fun == callee->fun)
2561 /* Tail calls use less stack than normal calls. Retain entry
2562 for normal call over one for tail call. */
2563 p->is_tail &= callee->is_tail;
2566 p->fun->start = NULL;
2567 p->fun->is_func = TRUE;
2570 /* Reorder list so most recent call is first. */
2572 p->next = caller->call_list;
2573 caller->call_list = p;
2576 callee->next = caller->call_list;
2578 caller->call_list = callee;
2582 /* Copy CALL and insert the copy into CALLER. */
2585 copy_callee (struct function_info *caller, const struct call_info *call)
2587 struct call_info *callee;
2588 callee = bfd_malloc (sizeof (*callee));
2592 if (!insert_callee (caller, callee))
2597 /* We're only interested in code sections. Testing SEC_IN_MEMORY excludes
2598 overlay stub sections. */
2601 interesting_section (asection *s)
2603 return (s->output_section != bfd_abs_section_ptr
2604 && ((s->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_IN_MEMORY))
2605 == (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2609 /* Rummage through the relocs for SEC, looking for function calls.
2610 If CALL_TREE is true, fill in call graph. If CALL_TREE is false,
2611 mark destination symbols on calls as being functions. Also
2612 look at branches, which may be tail calls or go to hot/cold
2613 section part of same function. */
2616 mark_functions_via_relocs (asection *sec,
2617 struct bfd_link_info *info,
2620 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2621 Elf_Internal_Shdr *symtab_hdr;
2623 unsigned int priority = 0;
2624 static bfd_boolean warned;
2626 if (!interesting_section (sec)
2627 || sec->reloc_count == 0)
2630 internal_relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL,
2632 if (internal_relocs == NULL)
2635 symtab_hdr = &elf_tdata (sec->owner)->symtab_hdr;
2636 psyms = &symtab_hdr->contents;
2637 irela = internal_relocs;
2638 irelaend = irela + sec->reloc_count;
2639 for (; irela < irelaend; irela++)
2641 enum elf_spu_reloc_type r_type;
2642 unsigned int r_indx;
2644 Elf_Internal_Sym *sym;
2645 struct elf_link_hash_entry *h;
2647 bfd_boolean reject, is_call;
2648 struct function_info *caller;
2649 struct call_info *callee;
2652 r_type = ELF32_R_TYPE (irela->r_info);
2653 if (r_type != R_SPU_REL16
2654 && r_type != R_SPU_ADDR16)
2657 if (!(call_tree && spu_hash_table (info)->params->auto_overlay))
2661 r_indx = ELF32_R_SYM (irela->r_info);
2662 if (!get_sym_h (&h, &sym, &sym_sec, psyms, r_indx, sec->owner))
2666 || sym_sec->output_section == bfd_abs_section_ptr)
2672 unsigned char insn[4];
2674 if (!bfd_get_section_contents (sec->owner, sec, insn,
2675 irela->r_offset, 4))
2677 if (is_branch (insn))
2679 is_call = (insn[0] & 0xfd) == 0x31;
2680 priority = insn[1] & 0x0f;
2682 priority |= insn[2];
2684 priority |= insn[3];
2686 if ((sym_sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2687 != (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2690 info->callbacks->einfo
2691 (_("%B(%A+0x%v): call to non-code section"
2692 " %B(%A), analysis incomplete\n"),
2693 sec->owner, sec, irela->r_offset,
2694 sym_sec->owner, sym_sec);
2702 if (!(call_tree && spu_hash_table (info)->params->auto_overlay)
2710 /* For --auto-overlay, count possible stubs we need for
2711 function pointer references. */
2712 unsigned int sym_type;
2716 sym_type = ELF_ST_TYPE (sym->st_info);
2717 if (sym_type == STT_FUNC)
2718 spu_hash_table (info)->non_ovly_stub += 1;
2723 val = h->root.u.def.value;
2725 val = sym->st_value;
2726 val += irela->r_addend;
2730 struct function_info *fun;
2732 if (irela->r_addend != 0)
2734 Elf_Internal_Sym *fake = bfd_zmalloc (sizeof (*fake));
2737 fake->st_value = val;
2739 = _bfd_elf_section_from_bfd_section (sym_sec->owner, sym_sec);
2743 fun = maybe_insert_function (sym_sec, sym, FALSE, is_call);
2745 fun = maybe_insert_function (sym_sec, h, TRUE, is_call);
2748 if (irela->r_addend != 0
2749 && fun->u.sym != sym)
2754 caller = find_function (sec, irela->r_offset, info);
2757 callee = bfd_malloc (sizeof *callee);
2761 callee->fun = find_function (sym_sec, val, info);
2762 if (callee->fun == NULL)
2764 callee->is_tail = !is_call;
2765 callee->is_pasted = FALSE;
2766 callee->priority = priority;
2768 if (callee->fun->last_caller != sec)
2770 callee->fun->last_caller = sec;
2771 callee->fun->call_count += 1;
2773 if (!insert_callee (caller, callee))
2776 && !callee->fun->is_func
2777 && callee->fun->stack == 0)
2779 /* This is either a tail call or a branch from one part of
2780 the function to another, ie. hot/cold section. If the
2781 destination has been called by some other function then
2782 it is a separate function. We also assume that functions
2783 are not split across input files. */
2784 if (sec->owner != sym_sec->owner)
2786 callee->fun->start = NULL;
2787 callee->fun->is_func = TRUE;
2789 else if (callee->fun->start == NULL)
2791 struct function_info *caller_start = caller;
2792 while (caller_start->start)
2793 caller_start = caller_start->start;
2795 if (caller_start != callee->fun)
2796 callee->fun->start = caller_start;
2800 struct function_info *callee_start;
2801 struct function_info *caller_start;
2802 callee_start = callee->fun;
2803 while (callee_start->start)
2804 callee_start = callee_start->start;
2805 caller_start = caller;
2806 while (caller_start->start)
2807 caller_start = caller_start->start;
2808 if (caller_start != callee_start)
2810 callee->fun->start = NULL;
2811 callee->fun->is_func = TRUE;
2820 /* Handle something like .init or .fini, which has a piece of a function.
2821 These sections are pasted together to form a single function. */
2824 pasted_function (asection *sec)
2826 struct bfd_link_order *l;
2827 struct _spu_elf_section_data *sec_data;
2828 struct spu_elf_stack_info *sinfo;
2829 Elf_Internal_Sym *fake;
2830 struct function_info *fun, *fun_start;
2832 fake = bfd_zmalloc (sizeof (*fake));
2836 fake->st_size = sec->size;
2838 = _bfd_elf_section_from_bfd_section (sec->owner, sec);
2839 fun = maybe_insert_function (sec, fake, FALSE, FALSE);
2843 /* Find a function immediately preceding this section. */
2845 for (l = sec->output_section->map_head.link_order; l != NULL; l = l->next)
2847 if (l->u.indirect.section == sec)
2849 if (fun_start != NULL)
2851 struct call_info *callee = bfd_malloc (sizeof *callee);
2855 fun->start = fun_start;
2857 callee->is_tail = TRUE;
2858 callee->is_pasted = TRUE;
2860 if (!insert_callee (fun_start, callee))
2866 if (l->type == bfd_indirect_link_order
2867 && (sec_data = spu_elf_section_data (l->u.indirect.section)) != NULL
2868 && (sinfo = sec_data->u.i.stack_info) != NULL
2869 && sinfo->num_fun != 0)
2870 fun_start = &sinfo->fun[sinfo->num_fun - 1];
2873 /* Don't return an error if we did not find a function preceding this
2874 section. The section may have incorrect flags. */
2878 /* Map address ranges in code sections to functions. */
2881 discover_functions (struct bfd_link_info *info)
2885 Elf_Internal_Sym ***psym_arr;
2886 asection ***sec_arr;
2887 bfd_boolean gaps = FALSE;
2890 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2893 psym_arr = bfd_zmalloc (bfd_idx * sizeof (*psym_arr));
2894 if (psym_arr == NULL)
2896 sec_arr = bfd_zmalloc (bfd_idx * sizeof (*sec_arr));
2897 if (sec_arr == NULL)
2900 for (ibfd = info->input_bfds, bfd_idx = 0;
2902 ibfd = ibfd->link_next, bfd_idx++)
2904 extern const bfd_target bfd_elf32_spu_vec;
2905 Elf_Internal_Shdr *symtab_hdr;
2908 Elf_Internal_Sym *syms, *sy, **psyms, **psy;
2909 asection **psecs, **p;
2911 if (ibfd->xvec != &bfd_elf32_spu_vec)
2914 /* Read all the symbols. */
2915 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2916 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2920 for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
2921 if (interesting_section (sec))
2929 if (symtab_hdr->contents != NULL)
2931 /* Don't use cached symbols since the generic ELF linker
2932 code only reads local symbols, and we need globals too. */
2933 free (symtab_hdr->contents);
2934 symtab_hdr->contents = NULL;
2936 syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, 0,
2938 symtab_hdr->contents = (void *) syms;
2942 /* Select defined function symbols that are going to be output. */
2943 psyms = bfd_malloc ((symcount + 1) * sizeof (*psyms));
2946 psym_arr[bfd_idx] = psyms;
2947 psecs = bfd_malloc (symcount * sizeof (*psecs));
2950 sec_arr[bfd_idx] = psecs;
2951 for (psy = psyms, p = psecs, sy = syms; sy < syms + symcount; ++p, ++sy)
2952 if (ELF_ST_TYPE (sy->st_info) == STT_NOTYPE
2953 || ELF_ST_TYPE (sy->st_info) == STT_FUNC)
2957 *p = s = bfd_section_from_elf_index (ibfd, sy->st_shndx);
2958 if (s != NULL && interesting_section (s))
2961 symcount = psy - psyms;
2964 /* Sort them by section and offset within section. */
2965 sort_syms_syms = syms;
2966 sort_syms_psecs = psecs;
2967 qsort (psyms, symcount, sizeof (*psyms), sort_syms);
2969 /* Now inspect the function symbols. */
2970 for (psy = psyms; psy < psyms + symcount; )
2972 asection *s = psecs[*psy - syms];
2973 Elf_Internal_Sym **psy2;
2975 for (psy2 = psy; ++psy2 < psyms + symcount; )
2976 if (psecs[*psy2 - syms] != s)
2979 if (!alloc_stack_info (s, psy2 - psy))
2984 /* First install info about properly typed and sized functions.
2985 In an ideal world this will cover all code sections, except
2986 when partitioning functions into hot and cold sections,
2987 and the horrible pasted together .init and .fini functions. */
2988 for (psy = psyms; psy < psyms + symcount; ++psy)
2991 if (ELF_ST_TYPE (sy->st_info) == STT_FUNC)
2993 asection *s = psecs[sy - syms];
2994 if (!maybe_insert_function (s, sy, FALSE, TRUE))
2999 for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
3000 if (interesting_section (sec))
3001 gaps |= check_function_ranges (sec, info);
3006 /* See if we can discover more function symbols by looking at
3008 for (ibfd = info->input_bfds, bfd_idx = 0;
3010 ibfd = ibfd->link_next, bfd_idx++)
3014 if (psym_arr[bfd_idx] == NULL)
3017 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3018 if (!mark_functions_via_relocs (sec, info, FALSE))
3022 for (ibfd = info->input_bfds, bfd_idx = 0;
3024 ibfd = ibfd->link_next, bfd_idx++)
3026 Elf_Internal_Shdr *symtab_hdr;
3028 Elf_Internal_Sym *syms, *sy, **psyms, **psy;
3031 if ((psyms = psym_arr[bfd_idx]) == NULL)
3034 psecs = sec_arr[bfd_idx];
3036 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3037 syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3040 for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
3041 if (interesting_section (sec))
3042 gaps |= check_function_ranges (sec, info);
3046 /* Finally, install all globals. */
3047 for (psy = psyms; (sy = *psy) != NULL; ++psy)
3051 s = psecs[sy - syms];
3053 /* Global syms might be improperly typed functions. */
3054 if (ELF_ST_TYPE (sy->st_info) != STT_FUNC
3055 && ELF_ST_BIND (sy->st_info) == STB_GLOBAL)
3057 if (!maybe_insert_function (s, sy, FALSE, FALSE))
3063 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3065 extern const bfd_target bfd_elf32_spu_vec;
3068 if (ibfd->xvec != &bfd_elf32_spu_vec)
3071 /* Some of the symbols we've installed as marking the
3072 beginning of functions may have a size of zero. Extend
3073 the range of such functions to the beginning of the
3074 next symbol of interest. */
3075 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3076 if (interesting_section (sec))
3078 struct _spu_elf_section_data *sec_data;
3079 struct spu_elf_stack_info *sinfo;
3081 sec_data = spu_elf_section_data (sec);
3082 sinfo = sec_data->u.i.stack_info;
3083 if (sinfo != NULL && sinfo->num_fun != 0)
3086 bfd_vma hi = sec->size;
3088 for (fun_idx = sinfo->num_fun; --fun_idx >= 0; )
3090 sinfo->fun[fun_idx].hi = hi;
3091 hi = sinfo->fun[fun_idx].lo;
3094 sinfo->fun[0].lo = 0;
3096 /* No symbols in this section. Must be .init or .fini
3097 or something similar. */
3098 else if (!pasted_function (sec))
3104 for (ibfd = info->input_bfds, bfd_idx = 0;
3106 ibfd = ibfd->link_next, bfd_idx++)
3108 if (psym_arr[bfd_idx] == NULL)
3111 free (psym_arr[bfd_idx]);
3112 free (sec_arr[bfd_idx]);
3121 /* Iterate over all function_info we have collected, calling DOIT on
3122 each node if ROOT_ONLY is false. Only call DOIT on root nodes
3126 for_each_node (bfd_boolean (*doit) (struct function_info *,
3127 struct bfd_link_info *,
3129 struct bfd_link_info *info,
3135 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3137 extern const bfd_target bfd_elf32_spu_vec;
3140 if (ibfd->xvec != &bfd_elf32_spu_vec)
3143 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3145 struct _spu_elf_section_data *sec_data;
3146 struct spu_elf_stack_info *sinfo;
3148 if ((sec_data = spu_elf_section_data (sec)) != NULL
3149 && (sinfo = sec_data->u.i.stack_info) != NULL)
3152 for (i = 0; i < sinfo->num_fun; ++i)
3153 if (!root_only || !sinfo->fun[i].non_root)
3154 if (!doit (&sinfo->fun[i], info, param))
3162 /* Transfer call info attached to struct function_info entries for
3163 all of a given function's sections to the first entry. */
3166 transfer_calls (struct function_info *fun,
3167 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3168 void *param ATTRIBUTE_UNUSED)
3170 struct function_info *start = fun->start;
3174 struct call_info *call, *call_next;
3176 while (start->start != NULL)
3177 start = start->start;
3178 for (call = fun->call_list; call != NULL; call = call_next)
3180 call_next = call->next;
3181 if (!insert_callee (start, call))
3184 fun->call_list = NULL;
3189 /* Mark nodes in the call graph that are called by some other node. */
3192 mark_non_root (struct function_info *fun,
3193 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3194 void *param ATTRIBUTE_UNUSED)
3196 struct call_info *call;
3201 for (call = fun->call_list; call; call = call->next)
3203 call->fun->non_root = TRUE;
3204 mark_non_root (call->fun, 0, 0);
3209 /* Remove cycles from the call graph. Set depth of nodes. */
3212 remove_cycles (struct function_info *fun,
3213 struct bfd_link_info *info,
3216 struct call_info **callp, *call;
3217 unsigned int depth = *(unsigned int *) param;
3218 unsigned int max_depth = depth;
3222 fun->marking = TRUE;
3224 callp = &fun->call_list;
3225 while ((call = *callp) != NULL)
3227 call->max_depth = depth + !call->is_pasted;
3228 if (!call->fun->visit2)
3230 if (!remove_cycles (call->fun, info, &call->max_depth))
3232 if (max_depth < call->max_depth)
3233 max_depth = call->max_depth;
3235 else if (call->fun->marking)
3237 struct spu_link_hash_table *htab = spu_hash_table (info);
3239 if (!htab->params->auto_overlay
3240 && htab->params->stack_analysis)
3242 const char *f1 = func_name (fun);
3243 const char *f2 = func_name (call->fun);
3245 info->callbacks->info (_("Stack analysis will ignore the call "
3249 *callp = call->next;
3253 callp = &call->next;
3255 fun->marking = FALSE;
3256 *(unsigned int *) param = max_depth;
3260 /* Check that we actually visited all nodes in remove_cycles. If we
3261 didn't, then there is some cycle in the call graph not attached to
3262 any root node. Arbitrarily choose a node in the cycle as a new
3263 root and break the cycle. */
3266 mark_detached_root (struct function_info *fun,
3267 struct bfd_link_info *info,
3272 fun->non_root = FALSE;
3273 *(unsigned int *) param = 0;
3274 return remove_cycles (fun, info, param);
3277 /* Populate call_list for each function. */
3280 build_call_tree (struct bfd_link_info *info)
3285 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3287 extern const bfd_target bfd_elf32_spu_vec;
3290 if (ibfd->xvec != &bfd_elf32_spu_vec)
3293 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3294 if (!mark_functions_via_relocs (sec, info, TRUE))
3298 /* Transfer call info from hot/cold section part of function
3300 if (!spu_hash_table (info)->params->auto_overlay
3301 && !for_each_node (transfer_calls, info, 0, FALSE))
3304 /* Find the call graph root(s). */
3305 if (!for_each_node (mark_non_root, info, 0, FALSE))
3308 /* Remove cycles from the call graph. We start from the root node(s)
3309 so that we break cycles in a reasonable place. */
3311 if (!for_each_node (remove_cycles, info, &depth, TRUE))
3314 return for_each_node (mark_detached_root, info, &depth, FALSE);
3317 /* qsort predicate to sort calls by priority, max_depth then count. */
3320 sort_calls (const void *a, const void *b)
3322 struct call_info *const *c1 = a;
3323 struct call_info *const *c2 = b;
3326 delta = (*c2)->priority - (*c1)->priority;
3330 delta = (*c2)->max_depth - (*c1)->max_depth;
3334 delta = (*c2)->count - (*c1)->count;
3338 return (char *) c1 - (char *) c2;
3342 unsigned int max_overlay_size;
3345 /* Set linker_mark and gc_mark on any sections that we will put in
3346 overlays. These flags are used by the generic ELF linker, but we
3347 won't be continuing on to bfd_elf_final_link so it is OK to use
3348 them. linker_mark is clear before we get here. Set segment_mark
3349 on sections that are part of a pasted function (excluding the last
3352 Set up function rodata section if --overlay-rodata. We don't
3353 currently include merged string constant rodata sections since
3355 Sort the call graph so that the deepest nodes will be visited
3359 mark_overlay_section (struct function_info *fun,
3360 struct bfd_link_info *info,
3363 struct call_info *call;
3365 struct _mos_param *mos_param = param;
3366 struct spu_link_hash_table *htab = spu_hash_table (info);
3372 if (!fun->sec->linker_mark
3373 && (htab->params->ovly_flavour != ovly_soft_icache
3374 || htab->params->non_ia_text
3375 || strncmp (fun->sec->name, ".text.ia.", 9) == 0))
3379 fun->sec->linker_mark = 1;
3380 fun->sec->gc_mark = 1;
3381 fun->sec->segment_mark = 0;
3382 /* Ensure SEC_CODE is set on this text section (it ought to
3383 be!), and SEC_CODE is clear on rodata sections. We use
3384 this flag to differentiate the two overlay section types. */
3385 fun->sec->flags |= SEC_CODE;
3387 size = fun->sec->size;
3388 if (htab->params->auto_overlay & OVERLAY_RODATA)
3392 /* Find the rodata section corresponding to this function's
3394 if (strcmp (fun->sec->name, ".text") == 0)
3396 name = bfd_malloc (sizeof (".rodata"));
3399 memcpy (name, ".rodata", sizeof (".rodata"));
3401 else if (strncmp (fun->sec->name, ".text.", 6) == 0)
3403 size_t len = strlen (fun->sec->name);
3404 name = bfd_malloc (len + 3);
3407 memcpy (name, ".rodata", sizeof (".rodata"));
3408 memcpy (name + 7, fun->sec->name + 5, len - 4);
3410 else if (strncmp (fun->sec->name, ".gnu.linkonce.t.", 16) == 0)
3412 size_t len = strlen (fun->sec->name) + 1;
3413 name = bfd_malloc (len);
3416 memcpy (name, fun->sec->name, len);
3422 asection *rodata = NULL;
3423 asection *group_sec = elf_section_data (fun->sec)->next_in_group;
3424 if (group_sec == NULL)
3425 rodata = bfd_get_section_by_name (fun->sec->owner, name);
3427 while (group_sec != NULL && group_sec != fun->sec)
3429 if (strcmp (group_sec->name, name) == 0)
3434 group_sec = elf_section_data (group_sec)->next_in_group;
3436 fun->rodata = rodata;
3439 size += fun->rodata->size;
3440 if (htab->params->line_size != 0
3441 && size > htab->params->line_size)
3443 size -= fun->rodata->size;
3448 fun->rodata->linker_mark = 1;
3449 fun->rodata->gc_mark = 1;
3450 fun->rodata->flags &= ~SEC_CODE;
3456 if (mos_param->max_overlay_size < size)
3457 mos_param->max_overlay_size = size;
3460 for (count = 0, call = fun->call_list; call != NULL; call = call->next)
3465 struct call_info **calls = bfd_malloc (count * sizeof (*calls));
3469 for (count = 0, call = fun->call_list; call != NULL; call = call->next)
3470 calls[count++] = call;
3472 qsort (calls, count, sizeof (*calls), sort_calls);
3474 fun->call_list = NULL;
3478 calls[count]->next = fun->call_list;
3479 fun->call_list = calls[count];
3484 for (call = fun->call_list; call != NULL; call = call->next)
3486 if (call->is_pasted)
3488 /* There can only be one is_pasted call per function_info. */
3489 BFD_ASSERT (!fun->sec->segment_mark);
3490 fun->sec->segment_mark = 1;
3492 if (!mark_overlay_section (call->fun, info, param))
3496 /* Don't put entry code into an overlay. The overlay manager needs
3497 a stack! Also, don't mark .ovl.init as an overlay. */
3498 if (fun->lo + fun->sec->output_offset + fun->sec->output_section->vma
3499 == info->output_bfd->start_address
3500 || strncmp (fun->sec->output_section->name, ".ovl.init", 9) == 0)
3502 fun->sec->linker_mark = 0;
3503 if (fun->rodata != NULL)
3504 fun->rodata->linker_mark = 0;
3509 /* If non-zero then unmark functions called from those within sections
3510 that we need to unmark. Unfortunately this isn't reliable since the
3511 call graph cannot know the destination of function pointer calls. */
3512 #define RECURSE_UNMARK 0
3515 asection *exclude_input_section;
3516 asection *exclude_output_section;
3517 unsigned long clearing;
3520 /* Undo some of mark_overlay_section's work. */
3523 unmark_overlay_section (struct function_info *fun,
3524 struct bfd_link_info *info,
3527 struct call_info *call;
3528 struct _uos_param *uos_param = param;
3529 unsigned int excluded = 0;
3537 if (fun->sec == uos_param->exclude_input_section
3538 || fun->sec->output_section == uos_param->exclude_output_section)
3542 uos_param->clearing += excluded;
3544 if (RECURSE_UNMARK ? uos_param->clearing : excluded)
3546 fun->sec->linker_mark = 0;
3548 fun->rodata->linker_mark = 0;
3551 for (call = fun->call_list; call != NULL; call = call->next)
3552 if (!unmark_overlay_section (call->fun, info, param))
3556 uos_param->clearing -= excluded;
3561 unsigned int lib_size;
3562 asection **lib_sections;
3565 /* Add sections we have marked as belonging to overlays to an array
3566 for consideration as non-overlay sections. The array consist of
3567 pairs of sections, (text,rodata), for functions in the call graph. */
3570 collect_lib_sections (struct function_info *fun,
3571 struct bfd_link_info *info,
3574 struct _cl_param *lib_param = param;
3575 struct call_info *call;
3582 if (!fun->sec->linker_mark || !fun->sec->gc_mark || fun->sec->segment_mark)
3585 size = fun->sec->size;
3587 size += fun->rodata->size;
3589 if (size <= lib_param->lib_size)
3591 *lib_param->lib_sections++ = fun->sec;
3592 fun->sec->gc_mark = 0;
3593 if (fun->rodata && fun->rodata->linker_mark && fun->rodata->gc_mark)
3595 *lib_param->lib_sections++ = fun->rodata;
3596 fun->rodata->gc_mark = 0;
3599 *lib_param->lib_sections++ = NULL;
3602 for (call = fun->call_list; call != NULL; call = call->next)
3603 collect_lib_sections (call->fun, info, param);
3608 /* qsort predicate to sort sections by call count. */
3611 sort_lib (const void *a, const void *b)
3613 asection *const *s1 = a;
3614 asection *const *s2 = b;
3615 struct _spu_elf_section_data *sec_data;
3616 struct spu_elf_stack_info *sinfo;
3620 if ((sec_data = spu_elf_section_data (*s1)) != NULL
3621 && (sinfo = sec_data->u.i.stack_info) != NULL)
3624 for (i = 0; i < sinfo->num_fun; ++i)
3625 delta -= sinfo->fun[i].call_count;
3628 if ((sec_data = spu_elf_section_data (*s2)) != NULL
3629 && (sinfo = sec_data->u.i.stack_info) != NULL)
3632 for (i = 0; i < sinfo->num_fun; ++i)
3633 delta += sinfo->fun[i].call_count;
3642 /* Remove some sections from those marked to be in overlays. Choose
3643 those that are called from many places, likely library functions. */
3646 auto_ovl_lib_functions (struct bfd_link_info *info, unsigned int lib_size)
3649 asection **lib_sections;
3650 unsigned int i, lib_count;
3651 struct _cl_param collect_lib_param;
3652 struct function_info dummy_caller;
3653 struct spu_link_hash_table *htab;
3655 memset (&dummy_caller, 0, sizeof (dummy_caller));
3657 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3659 extern const bfd_target bfd_elf32_spu_vec;
3662 if (ibfd->xvec != &bfd_elf32_spu_vec)
3665 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3666 if (sec->linker_mark
3667 && sec->size < lib_size
3668 && (sec->flags & SEC_CODE) != 0)
3671 lib_sections = bfd_malloc (lib_count * 2 * sizeof (*lib_sections));
3672 if (lib_sections == NULL)
3673 return (unsigned int) -1;
3674 collect_lib_param.lib_size = lib_size;
3675 collect_lib_param.lib_sections = lib_sections;
3676 if (!for_each_node (collect_lib_sections, info, &collect_lib_param,
3678 return (unsigned int) -1;
3679 lib_count = (collect_lib_param.lib_sections - lib_sections) / 2;
3681 /* Sort sections so that those with the most calls are first. */
3683 qsort (lib_sections, lib_count, 2 * sizeof (*lib_sections), sort_lib);
3685 htab = spu_hash_table (info);
3686 for (i = 0; i < lib_count; i++)
3688 unsigned int tmp, stub_size;
3690 struct _spu_elf_section_data *sec_data;
3691 struct spu_elf_stack_info *sinfo;
3693 sec = lib_sections[2 * i];
3694 /* If this section is OK, its size must be less than lib_size. */
3696 /* If it has a rodata section, then add that too. */
3697 if (lib_sections[2 * i + 1])
3698 tmp += lib_sections[2 * i + 1]->size;
3699 /* Add any new overlay call stubs needed by the section. */
3702 && (sec_data = spu_elf_section_data (sec)) != NULL
3703 && (sinfo = sec_data->u.i.stack_info) != NULL)
3706 struct call_info *call;
3708 for (k = 0; k < sinfo->num_fun; ++k)
3709 for (call = sinfo->fun[k].call_list; call; call = call->next)
3710 if (call->fun->sec->linker_mark)
3712 struct call_info *p;
3713 for (p = dummy_caller.call_list; p; p = p->next)
3714 if (p->fun == call->fun)
3717 stub_size += ovl_stub_size (htab->params);
3720 if (tmp + stub_size < lib_size)
3722 struct call_info **pp, *p;
3724 /* This section fits. Mark it as non-overlay. */
3725 lib_sections[2 * i]->linker_mark = 0;
3726 if (lib_sections[2 * i + 1])
3727 lib_sections[2 * i + 1]->linker_mark = 0;
3728 lib_size -= tmp + stub_size;
3729 /* Call stubs to the section we just added are no longer
3731 pp = &dummy_caller.call_list;
3732 while ((p = *pp) != NULL)
3733 if (!p->fun->sec->linker_mark)
3735 lib_size += ovl_stub_size (htab->params);
3741 /* Add new call stubs to dummy_caller. */
3742 if ((sec_data = spu_elf_section_data (sec)) != NULL
3743 && (sinfo = sec_data->u.i.stack_info) != NULL)
3746 struct call_info *call;
3748 for (k = 0; k < sinfo->num_fun; ++k)
3749 for (call = sinfo->fun[k].call_list;
3752 if (call->fun->sec->linker_mark)
3754 struct call_info *callee;
3755 callee = bfd_malloc (sizeof (*callee));
3757 return (unsigned int) -1;
3759 if (!insert_callee (&dummy_caller, callee))
3765 while (dummy_caller.call_list != NULL)
3767 struct call_info *call = dummy_caller.call_list;
3768 dummy_caller.call_list = call->next;
3771 for (i = 0; i < 2 * lib_count; i++)
3772 if (lib_sections[i])
3773 lib_sections[i]->gc_mark = 1;
3774 free (lib_sections);
3778 /* Build an array of overlay sections. The deepest node's section is
3779 added first, then its parent node's section, then everything called
3780 from the parent section. The idea being to group sections to
3781 minimise calls between different overlays. */
3784 collect_overlays (struct function_info *fun,
3785 struct bfd_link_info *info,
3788 struct call_info *call;
3789 bfd_boolean added_fun;
3790 asection ***ovly_sections = param;
3796 for (call = fun->call_list; call != NULL; call = call->next)
3797 if (!call->is_pasted)
3799 if (!collect_overlays (call->fun, info, ovly_sections))
3805 if (fun->sec->linker_mark && fun->sec->gc_mark)
3807 fun->sec->gc_mark = 0;
3808 *(*ovly_sections)++ = fun->sec;
3809 if (fun->rodata && fun->rodata->linker_mark && fun->rodata->gc_mark)
3811 fun->rodata->gc_mark = 0;
3812 *(*ovly_sections)++ = fun->rodata;
3815 *(*ovly_sections)++ = NULL;
3818 /* Pasted sections must stay with the first section. We don't
3819 put pasted sections in the array, just the first section.
3820 Mark subsequent sections as already considered. */
3821 if (fun->sec->segment_mark)
3823 struct function_info *call_fun = fun;
3826 for (call = call_fun->call_list; call != NULL; call = call->next)
3827 if (call->is_pasted)
3829 call_fun = call->fun;
3830 call_fun->sec->gc_mark = 0;
3831 if (call_fun->rodata)
3832 call_fun->rodata->gc_mark = 0;
3838 while (call_fun->sec->segment_mark);
3842 for (call = fun->call_list; call != NULL; call = call->next)
3843 if (!collect_overlays (call->fun, info, ovly_sections))
3848 struct _spu_elf_section_data *sec_data;
3849 struct spu_elf_stack_info *sinfo;
3851 if ((sec_data = spu_elf_section_data (fun->sec)) != NULL
3852 && (sinfo = sec_data->u.i.stack_info) != NULL)
3855 for (i = 0; i < sinfo->num_fun; ++i)
3856 if (!collect_overlays (&sinfo->fun[i], info, ovly_sections))
3864 struct _sum_stack_param {
3866 size_t overall_stack;
3867 bfd_boolean emit_stack_syms;
3870 /* Descend the call graph for FUN, accumulating total stack required. */
3873 sum_stack (struct function_info *fun,
3874 struct bfd_link_info *info,
3877 struct call_info *call;
3878 struct function_info *max;
3879 size_t stack, cum_stack;
3881 bfd_boolean has_call;
3882 struct _sum_stack_param *sum_stack_param = param;
3883 struct spu_link_hash_table *htab;
3885 cum_stack = fun->stack;
3886 sum_stack_param->cum_stack = cum_stack;
3892 for (call = fun->call_list; call; call = call->next)
3894 if (!call->is_pasted)
3896 if (!sum_stack (call->fun, info, sum_stack_param))
3898 stack = sum_stack_param->cum_stack;
3899 /* Include caller stack for normal calls, don't do so for
3900 tail calls. fun->stack here is local stack usage for
3902 if (!call->is_tail || call->is_pasted || call->fun->start != NULL)
3903 stack += fun->stack;
3904 if (cum_stack < stack)
3911 sum_stack_param->cum_stack = cum_stack;
3913 /* Now fun->stack holds cumulative stack. */
3914 fun->stack = cum_stack;
3918 && sum_stack_param->overall_stack < cum_stack)
3919 sum_stack_param->overall_stack = cum_stack;
3921 htab = spu_hash_table (info);
3922 if (htab->params->auto_overlay)
3925 f1 = func_name (fun);
3926 if (htab->params->stack_analysis)
3929 info->callbacks->info (_(" %s: 0x%v\n"), f1, (bfd_vma) cum_stack);
3930 info->callbacks->minfo (_("%s: 0x%v 0x%v\n"),
3931 f1, (bfd_vma) stack, (bfd_vma) cum_stack);
3935 info->callbacks->minfo (_(" calls:\n"));
3936 for (call = fun->call_list; call; call = call->next)
3937 if (!call->is_pasted)
3939 const char *f2 = func_name (call->fun);
3940 const char *ann1 = call->fun == max ? "*" : " ";
3941 const char *ann2 = call->is_tail ? "t" : " ";
3943 info->callbacks->minfo (_(" %s%s %s\n"), ann1, ann2, f2);
3948 if (sum_stack_param->emit_stack_syms)
3950 char *name = bfd_malloc (18 + strlen (f1));
3951 struct elf_link_hash_entry *h;
3956 if (fun->global || ELF_ST_BIND (fun->u.sym->st_info) == STB_GLOBAL)
3957 sprintf (name, "__stack_%s", f1);
3959 sprintf (name, "__stack_%x_%s", fun->sec->id & 0xffffffff, f1);
3961 h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE);
3964 && (h->root.type == bfd_link_hash_new
3965 || h->root.type == bfd_link_hash_undefined
3966 || h->root.type == bfd_link_hash_undefweak))
3968 h->root.type = bfd_link_hash_defined;
3969 h->root.u.def.section = bfd_abs_section_ptr;
3970 h->root.u.def.value = cum_stack;
3975 h->ref_regular_nonweak = 1;
3976 h->forced_local = 1;
3984 /* SEC is part of a pasted function. Return the call_info for the
3985 next section of this function. */
3987 static struct call_info *
3988 find_pasted_call (asection *sec)
3990 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
3991 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
3992 struct call_info *call;
3995 for (k = 0; k < sinfo->num_fun; ++k)
3996 for (call = sinfo->fun[k].call_list; call != NULL; call = call->next)
3997 if (call->is_pasted)
4003 /* qsort predicate to sort bfds by file name. */
4006 sort_bfds (const void *a, const void *b)
4008 bfd *const *abfd1 = a;
4009 bfd *const *abfd2 = b;
4011 return strcmp ((*abfd1)->filename, (*abfd2)->filename);
4015 print_one_overlay_section (FILE *script,
4018 unsigned int ovlynum,
4019 unsigned int *ovly_map,
4020 asection **ovly_sections,
4021 struct bfd_link_info *info)
4025 for (j = base; j < count && ovly_map[j] == ovlynum; j++)
4027 asection *sec = ovly_sections[2 * j];
4029 if (fprintf (script, " %s%c%s (%s)\n",
4030 (sec->owner->my_archive != NULL
4031 ? sec->owner->my_archive->filename : ""),
4032 info->path_separator,
4033 sec->owner->filename,
4036 if (sec->segment_mark)
4038 struct call_info *call = find_pasted_call (sec);
4039 while (call != NULL)
4041 struct function_info *call_fun = call->fun;
4042 sec = call_fun->sec;
4043 if (fprintf (script, " %s%c%s (%s)\n",
4044 (sec->owner->my_archive != NULL
4045 ? sec->owner->my_archive->filename : ""),
4046 info->path_separator,
4047 sec->owner->filename,
4050 for (call = call_fun->call_list; call; call = call->next)
4051 if (call->is_pasted)
4057 for (j = base; j < count && ovly_map[j] == ovlynum; j++)
4059 asection *sec = ovly_sections[2 * j + 1];
4061 && fprintf (script, " %s%c%s (%s)\n",
4062 (sec->owner->my_archive != NULL
4063 ? sec->owner->my_archive->filename : ""),
4064 info->path_separator,
4065 sec->owner->filename,
4069 sec = ovly_sections[2 * j];
4070 if (sec->segment_mark)
4072 struct call_info *call = find_pasted_call (sec);
4073 while (call != NULL)
4075 struct function_info *call_fun = call->fun;
4076 sec = call_fun->rodata;
4078 && fprintf (script, " %s%c%s (%s)\n",
4079 (sec->owner->my_archive != NULL
4080 ? sec->owner->my_archive->filename : ""),
4081 info->path_separator,
4082 sec->owner->filename,
4085 for (call = call_fun->call_list; call; call = call->next)
4086 if (call->is_pasted)
4095 /* Handle --auto-overlay. */
4098 spu_elf_auto_overlay (struct bfd_link_info *info)
4102 struct elf_segment_map *m;
4103 unsigned int fixed_size, lo, hi;
4104 struct spu_link_hash_table *htab;
4105 unsigned int base, i, count, bfd_count;
4106 unsigned int region, ovlynum;
4107 asection **ovly_sections, **ovly_p;
4108 unsigned int *ovly_map;
4110 unsigned int total_overlay_size, overlay_size;
4111 const char *ovly_mgr_entry;
4112 struct elf_link_hash_entry *h;
4113 struct _mos_param mos_param;
4114 struct _uos_param uos_param;
4115 struct function_info dummy_caller;
4117 /* Find the extents of our loadable image. */
4118 lo = (unsigned int) -1;
4120 for (m = elf_tdata (info->output_bfd)->segment_map; m != NULL; m = m->next)
4121 if (m->p_type == PT_LOAD)
4122 for (i = 0; i < m->count; i++)
4123 if (m->sections[i]->size != 0)
4125 if (m->sections[i]->vma < lo)
4126 lo = m->sections[i]->vma;
4127 if (m->sections[i]->vma + m->sections[i]->size - 1 > hi)
4128 hi = m->sections[i]->vma + m->sections[i]->size - 1;
4130 fixed_size = hi + 1 - lo;
4132 if (!discover_functions (info))
4135 if (!build_call_tree (info))
4138 htab = spu_hash_table (info);
4139 if (htab->reserved == 0)
4141 struct _sum_stack_param sum_stack_param;
4143 sum_stack_param.emit_stack_syms = 0;
4144 sum_stack_param.overall_stack = 0;
4145 if (!for_each_node (sum_stack, info, &sum_stack_param, TRUE))
4147 htab->reserved = sum_stack_param.overall_stack + htab->extra_stack_space;
4150 /* No need for overlays if everything already fits. */
4151 if (fixed_size + htab->reserved <= htab->local_store
4152 && htab->params->ovly_flavour != ovly_soft_icache)
4154 htab->params->auto_overlay = 0;
4158 uos_param.exclude_input_section = 0;
4159 uos_param.exclude_output_section
4160 = bfd_get_section_by_name (info->output_bfd, ".interrupt");
4162 ovly_mgr_entry = "__ovly_load";
4163 if (htab->params->ovly_flavour == ovly_soft_icache)
4164 ovly_mgr_entry = "__icache_br_handler";
4165 h = elf_link_hash_lookup (&htab->elf, ovly_mgr_entry,
4166 FALSE, FALSE, FALSE);
4168 && (h->root.type == bfd_link_hash_defined
4169 || h->root.type == bfd_link_hash_defweak)
4172 /* We have a user supplied overlay manager. */
4173 uos_param.exclude_input_section = h->root.u.def.section;
4177 /* If no user overlay manager, spu_elf_load_ovl_mgr will add our
4178 builtin version to .text, and will adjust .text size. */
4179 fixed_size += (*htab->params->spu_elf_load_ovl_mgr) ();
4182 /* Mark overlay sections, and find max overlay section size. */
4183 mos_param.max_overlay_size = 0;
4184 if (!for_each_node (mark_overlay_section, info, &mos_param, TRUE))
4187 /* We can't put the overlay manager or interrupt routines in
4189 uos_param.clearing = 0;
4190 if ((uos_param.exclude_input_section
4191 || uos_param.exclude_output_section)
4192 && !for_each_node (unmark_overlay_section, info, &uos_param, TRUE))
4196 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4198 bfd_arr = bfd_malloc (bfd_count * sizeof (*bfd_arr));
4199 if (bfd_arr == NULL)
4202 /* Count overlay sections, and subtract their sizes from "fixed_size". */
4205 total_overlay_size = 0;
4206 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4208 extern const bfd_target bfd_elf32_spu_vec;
4210 unsigned int old_count;
4212 if (ibfd->xvec != &bfd_elf32_spu_vec)
4216 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4217 if (sec->linker_mark)
4219 if ((sec->flags & SEC_CODE) != 0)
4221 fixed_size -= sec->size;
4222 total_overlay_size += sec->size;
4224 else if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD)
4225 && sec->output_section->owner == info->output_bfd
4226 && strncmp (sec->output_section->name, ".ovl.init", 9) == 0)
4227 fixed_size -= sec->size;
4228 if (count != old_count)
4229 bfd_arr[bfd_count++] = ibfd;
4232 /* Since the overlay link script selects sections by file name and
4233 section name, ensure that file names are unique. */
4236 bfd_boolean ok = TRUE;
4238 qsort (bfd_arr, bfd_count, sizeof (*bfd_arr), sort_bfds);
4239 for (i = 1; i < bfd_count; ++i)
4240 if (strcmp (bfd_arr[i - 1]->filename, bfd_arr[i]->filename) == 0)
4242 if (bfd_arr[i - 1]->my_archive == bfd_arr[i]->my_archive)
4244 if (bfd_arr[i - 1]->my_archive && bfd_arr[i]->my_archive)
4245 info->callbacks->einfo (_("%s duplicated in %s\n"),
4246 bfd_arr[i]->filename,
4247 bfd_arr[i]->my_archive->filename);
4249 info->callbacks->einfo (_("%s duplicated\n"),
4250 bfd_arr[i]->filename);
4256 info->callbacks->einfo (_("sorry, no support for duplicate "
4257 "object files in auto-overlay script\n"));
4258 bfd_set_error (bfd_error_bad_value);
4264 fixed_size += htab->reserved;
4265 fixed_size += htab->non_ovly_stub * ovl_stub_size (htab->params);
4266 if (fixed_size + mos_param.max_overlay_size <= htab->local_store)
4268 if (htab->params->ovly_flavour == ovly_soft_icache)
4270 /* Stubs in the non-icache area are bigger. */
4271 fixed_size += htab->non_ovly_stub * 16;
4272 /* Space for icache manager tables.
4273 a) Tag array, one quadword per cache line.
4274 - word 0: ia address of present line, init to zero.
4275 - word 1: link locator. link_elem=stub_addr/2+locator
4276 - halfwords 4-7: head/tail pointers for linked lists. */
4277 fixed_size += 16 << htab->num_lines_log2;
4278 /* b) Linked list elements, max_branch per line. */
4279 fixed_size += htab->params->max_branch << (htab->num_lines_log2 + 4);
4280 /* c) Indirect branch descriptors, 8 quadwords. */
4281 fixed_size += 8 * 16;
4282 /* d) Pointer to __ea backing store (toe), 1 quadword. */
4287 /* Guess number of overlays. Assuming overlay buffer is on
4288 average only half full should be conservative. */
4289 ovlynum = (total_overlay_size * 2 * htab->params->num_lines
4290 / (htab->local_store - fixed_size));
4291 /* Space for _ovly_table[], _ovly_buf_table[] and toe. */
4292 fixed_size += ovlynum * 16 + 16 + 4 + 16;
4296 if (fixed_size + mos_param.max_overlay_size > htab->local_store)
4297 info->callbacks->einfo (_("non-overlay size of 0x%v plus maximum overlay "
4298 "size of 0x%v exceeds local store\n"),
4299 (bfd_vma) fixed_size,
4300 (bfd_vma) mos_param.max_overlay_size);
4302 /* Now see if we should put some functions in the non-overlay area. */
4303 else if (fixed_size < htab->overlay_fixed)
4305 unsigned int max_fixed, lib_size;
4307 max_fixed = htab->local_store - mos_param.max_overlay_size;
4308 if (max_fixed > htab->overlay_fixed)
4309 max_fixed = htab->overlay_fixed;
4310 lib_size = max_fixed - fixed_size;
4311 lib_size = auto_ovl_lib_functions (info, lib_size);
4312 if (lib_size == (unsigned int) -1)
4314 fixed_size = max_fixed - lib_size;
4317 /* Build an array of sections, suitably sorted to place into
4319 ovly_sections = bfd_malloc (2 * count * sizeof (*ovly_sections));
4320 if (ovly_sections == NULL)
4322 ovly_p = ovly_sections;
4323 if (!for_each_node (collect_overlays, info, &ovly_p, TRUE))
4325 count = (size_t) (ovly_p - ovly_sections) / 2;
4326 ovly_map = bfd_malloc (count * sizeof (*ovly_map));
4327 if (ovly_map == NULL)
4330 memset (&dummy_caller, 0, sizeof (dummy_caller));
4331 overlay_size = (htab->local_store - fixed_size) / htab->params->num_lines;
4332 if (htab->params->line_size != 0)
4333 overlay_size = htab->params->line_size;
4336 while (base < count)
4338 unsigned int size = 0;
4340 for (i = base; i < count; i++)
4344 unsigned int num_stubs;
4345 struct call_info *call, *pasty;
4346 struct _spu_elf_section_data *sec_data;
4347 struct spu_elf_stack_info *sinfo;
4350 /* See whether we can add this section to the current
4351 overlay without overflowing our overlay buffer. */
4352 sec = ovly_sections[2 * i];
4353 tmp = size + sec->size;
4354 if (ovly_sections[2 * i + 1])
4355 tmp += ovly_sections[2 * i + 1]->size;
4356 if (tmp > overlay_size)
4358 if (sec->segment_mark)
4360 /* Pasted sections must stay together, so add their
4362 struct call_info *pasty = find_pasted_call (sec);
4363 while (pasty != NULL)
4365 struct function_info *call_fun = pasty->fun;
4366 tmp += call_fun->sec->size;
4367 if (call_fun->rodata)
4368 tmp += call_fun->rodata->size;
4369 for (pasty = call_fun->call_list; pasty; pasty = pasty->next)
4370 if (pasty->is_pasted)
4374 if (tmp > overlay_size)
4377 /* If we add this section, we might need new overlay call
4378 stubs. Add any overlay section calls to dummy_call. */
4380 sec_data = spu_elf_section_data (sec);
4381 sinfo = sec_data->u.i.stack_info;
4382 for (k = 0; k < sinfo->num_fun; ++k)
4383 for (call = sinfo->fun[k].call_list; call; call = call->next)
4384 if (call->is_pasted)
4386 BFD_ASSERT (pasty == NULL);
4389 else if (call->fun->sec->linker_mark)
4391 if (!copy_callee (&dummy_caller, call))
4394 while (pasty != NULL)
4396 struct function_info *call_fun = pasty->fun;
4398 for (call = call_fun->call_list; call; call = call->next)
4399 if (call->is_pasted)
4401 BFD_ASSERT (pasty == NULL);
4404 else if (!copy_callee (&dummy_caller, call))
4408 /* Calculate call stub size. */
4410 for (call = dummy_caller.call_list; call; call = call->next)
4415 /* If the call is within this overlay, we won't need a
4417 for (k = base; k < i + 1; k++)
4418 if (call->fun->sec == ovly_sections[2 * k])
4424 if (htab->params->ovly_flavour == ovly_soft_icache
4425 && num_stubs > htab->params->max_branch)
4427 if (tmp + num_stubs * ovl_stub_size (htab->params)
4435 info->callbacks->einfo (_("%B:%A%s exceeds overlay size\n"),
4436 ovly_sections[2 * i]->owner,
4437 ovly_sections[2 * i],
4438 ovly_sections[2 * i + 1] ? " + rodata" : "");
4439 bfd_set_error (bfd_error_bad_value);
4443 while (dummy_caller.call_list != NULL)
4445 struct call_info *call = dummy_caller.call_list;
4446 dummy_caller.call_list = call->next;
4452 ovly_map[base++] = ovlynum;
4455 script = htab->params->spu_elf_open_overlay_script ();
4457 if (fprintf (script, "SECTIONS\n{\n") <= 0)
4460 if (htab->params->ovly_flavour == ovly_soft_icache)
4462 if (fprintf (script,
4463 " .data.icache ALIGN (16) : { *(.ovtab) *(.data.icache) }\n"
4464 " . = ALIGN (%u);\n"
4465 " .ovl.init : { *(.ovl.init) }\n"
4466 " . = ABSOLUTE (ADDR (.ovl.init));\n",
4467 htab->params->line_size) <= 0)
4472 while (base < count)
4474 unsigned int indx = ovlynum - 1;
4475 unsigned int vma, lma;
4477 vma = (indx & (htab->params->num_lines - 1)) << htab->line_size_log2;
4478 lma = indx << htab->line_size_log2;
4480 if (fprintf (script, " .ovly%u ABSOLUTE (ADDR (.ovl.init)) + %u "
4481 ": AT (ALIGN (LOADADDR (.ovl.init) + SIZEOF (.ovl.init), 16) + %u) {\n",
4482 ovlynum, vma, lma) <= 0)
4485 base = print_one_overlay_section (script, base, count, ovlynum,
4486 ovly_map, ovly_sections, info);
4487 if (base == (unsigned) -1)
4490 if (fprintf (script, " }\n") <= 0)
4496 if (fprintf (script, " . = ABSOLUTE (ADDR (.ovl.init)) + %u;\n",
4497 1 << (htab->num_lines_log2 + htab->line_size_log2)) <= 0)
4502 if (fprintf (script,
4503 " . = ALIGN (16);\n"
4504 " .ovl.init : { *(.ovl.init) }\n"
4505 " . = ABSOLUTE (ADDR (.ovl.init));\n") <= 0)
4508 for (region = 1; region <= htab->params->num_lines; region++)
4512 while (base < count && ovly_map[base] < ovlynum)
4520 /* We need to set lma since we are overlaying .ovl.init. */
4521 if (fprintf (script,
4522 " OVERLAY : AT (ALIGN (LOADADDR (.ovl.init) + SIZEOF (.ovl.init), 16))\n {\n") <= 0)
4527 if (fprintf (script, " OVERLAY :\n {\n") <= 0)
4531 while (base < count)
4533 if (fprintf (script, " .ovly%u {\n", ovlynum) <= 0)
4536 base = print_one_overlay_section (script, base, count, ovlynum,
4537 ovly_map, ovly_sections, info);
4538 if (base == (unsigned) -1)
4541 if (fprintf (script, " }\n") <= 0)
4544 ovlynum += htab->params->num_lines;
4545 while (base < count && ovly_map[base] < ovlynum)
4549 if (fprintf (script, " }\n") <= 0)
4556 free (ovly_sections);
4558 if (fprintf (script, "}\nINSERT BEFORE .text;\n") <= 0)
4560 if (fclose (script) != 0)
4563 if (htab->params->auto_overlay & AUTO_RELINK)
4564 (*htab->params->spu_elf_relink) ();
4569 bfd_set_error (bfd_error_system_call);
4571 info->callbacks->einfo ("%F%P: auto overlay error: %E\n");
4575 /* Provide an estimate of total stack required. */
4578 spu_elf_stack_analysis (struct bfd_link_info *info)
4580 struct spu_link_hash_table *htab;
4581 struct _sum_stack_param sum_stack_param;
4583 if (!discover_functions (info))
4586 if (!build_call_tree (info))
4589 htab = spu_hash_table (info);
4590 if (htab->params->stack_analysis)
4592 info->callbacks->info (_("Stack size for call graph root nodes.\n"));
4593 info->callbacks->minfo (_("\nStack size for functions. "
4594 "Annotations: '*' max stack, 't' tail call\n"));
4597 sum_stack_param.emit_stack_syms = htab->params->emit_stack_syms;
4598 sum_stack_param.overall_stack = 0;
4599 if (!for_each_node (sum_stack, info, &sum_stack_param, TRUE))
4602 if (htab->params->stack_analysis)
4603 info->callbacks->info (_("Maximum stack required is 0x%v\n"),
4604 (bfd_vma) sum_stack_param.overall_stack);
4608 /* Perform a final link. */
4611 spu_elf_final_link (bfd *output_bfd, struct bfd_link_info *info)
4613 struct spu_link_hash_table *htab = spu_hash_table (info);
4615 if (htab->params->auto_overlay)
4616 spu_elf_auto_overlay (info);
4618 if ((htab->params->stack_analysis
4619 || (htab->params->ovly_flavour == ovly_soft_icache
4620 && htab->params->lrlive_analysis))
4621 && !spu_elf_stack_analysis (info))
4622 info->callbacks->einfo ("%X%P: stack/lrlive analysis error: %E\n");
4624 if (!spu_elf_build_stubs (info))
4625 info->callbacks->einfo ("%F%P: can not build overlay stubs: %E\n");
4627 return bfd_elf_final_link (output_bfd, info);
4630 /* Called when not normally emitting relocs, ie. !info->relocatable
4631 and !info->emitrelocations. Returns a count of special relocs
4632 that need to be emitted. */
4635 spu_elf_count_relocs (struct bfd_link_info *info, asection *sec)
4637 Elf_Internal_Rela *relocs;
4638 unsigned int count = 0;
4640 relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL,
4644 Elf_Internal_Rela *rel;
4645 Elf_Internal_Rela *relend = relocs + sec->reloc_count;
4647 for (rel = relocs; rel < relend; rel++)
4649 int r_type = ELF32_R_TYPE (rel->r_info);
4650 if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
4654 if (elf_section_data (sec)->relocs != relocs)
4661 /* Apply RELOCS to CONTENTS of INPUT_SECTION from INPUT_BFD. */
4664 spu_elf_relocate_section (bfd *output_bfd,
4665 struct bfd_link_info *info,
4667 asection *input_section,
4669 Elf_Internal_Rela *relocs,
4670 Elf_Internal_Sym *local_syms,
4671 asection **local_sections)
4673 Elf_Internal_Shdr *symtab_hdr;
4674 struct elf_link_hash_entry **sym_hashes;
4675 Elf_Internal_Rela *rel, *relend;
4676 struct spu_link_hash_table *htab;
4679 bfd_boolean emit_these_relocs = FALSE;
4680 bfd_boolean is_ea_sym;
4682 unsigned int iovl = 0;
4684 htab = spu_hash_table (info);
4685 stubs = (htab->stub_sec != NULL
4686 && maybe_needs_stubs (input_section));
4687 iovl = overlay_index (input_section);
4688 ea = bfd_get_section_by_name (output_bfd, "._ea");
4689 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4690 sym_hashes = (struct elf_link_hash_entry **) (elf_sym_hashes (input_bfd));
4693 relend = relocs + input_section->reloc_count;
4694 for (; rel < relend; rel++)
4697 reloc_howto_type *howto;
4698 unsigned int r_symndx;
4699 Elf_Internal_Sym *sym;
4701 struct elf_link_hash_entry *h;
4702 const char *sym_name;
4705 bfd_reloc_status_type r;
4706 bfd_boolean unresolved_reloc;
4708 enum _stub_type stub_type;
4710 r_symndx = ELF32_R_SYM (rel->r_info);
4711 r_type = ELF32_R_TYPE (rel->r_info);
4712 howto = elf_howto_table + r_type;
4713 unresolved_reloc = FALSE;
4718 if (r_symndx < symtab_hdr->sh_info)
4720 sym = local_syms + r_symndx;
4721 sec = local_sections[r_symndx];
4722 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
4723 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4727 if (sym_hashes == NULL)
4730 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4732 while (h->root.type == bfd_link_hash_indirect
4733 || h->root.type == bfd_link_hash_warning)
4734 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4737 if (h->root.type == bfd_link_hash_defined
4738 || h->root.type == bfd_link_hash_defweak)
4740 sec = h->root.u.def.section;
4742 || sec->output_section == NULL)
4743 /* Set a flag that will be cleared later if we find a
4744 relocation value for this symbol. output_section
4745 is typically NULL for symbols satisfied by a shared
4747 unresolved_reloc = TRUE;
4749 relocation = (h->root.u.def.value
4750 + sec->output_section->vma
4751 + sec->output_offset);
4753 else if (h->root.type == bfd_link_hash_undefweak)
4755 else if (info->unresolved_syms_in_objects == RM_IGNORE
4756 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4758 else if (!info->relocatable
4759 && !(r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64))
4762 err = (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
4763 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT);
4764 if (!info->callbacks->undefined_symbol (info,
4765 h->root.root.string,
4768 rel->r_offset, err))
4772 sym_name = h->root.root.string;
4775 if (sec != NULL && elf_discarded_section (sec))
4777 /* For relocs against symbols from removed linkonce sections,
4778 or sections discarded by a linker script, we just want the
4779 section contents zeroed. Avoid any special processing. */
4780 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
4786 if (info->relocatable)
4789 is_ea_sym = (ea != NULL
4791 && sec->output_section == ea);
4793 /* If this symbol is in an overlay area, we may need to relocate
4794 to the overlay stub. */
4795 addend = rel->r_addend;
4798 && (stub_type = needs_ovl_stub (h, sym, sec, input_section, rel,
4799 contents, info)) != no_stub)
4801 unsigned int ovl = 0;
4802 struct got_entry *g, **head;
4804 if (stub_type != nonovl_stub)
4808 head = &h->got.glist;
4810 head = elf_local_got_ents (input_bfd) + r_symndx;
4812 for (g = *head; g != NULL; g = g->next)
4813 if (htab->params->ovly_flavour == ovly_soft_icache
4814 ? g->br_addr == (rel->r_offset
4815 + input_section->output_offset
4816 + input_section->output_section->vma)
4817 : g->addend == addend && (g->ovl == ovl || g->ovl == 0))
4822 relocation = g->stub_addr;
4827 /* For soft icache, encode the overlay index into addresses. */
4828 if (htab->params->ovly_flavour == ovly_soft_icache
4829 && (r_type == R_SPU_ADDR16_HI
4830 || r_type == R_SPU_ADDR32 || r_type == R_SPU_REL32)
4833 unsigned int ovl = overlay_index (sec);
4836 unsigned int set_id = ((ovl - 1) >> htab->num_lines_log2) + 1;
4837 relocation += set_id << 18;
4842 if (unresolved_reloc)
4844 else if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
4848 /* ._ea is a special section that isn't allocated in SPU
4849 memory, but rather occupies space in PPU memory as
4850 part of an embedded ELF image. If this reloc is
4851 against a symbol defined in ._ea, then transform the
4852 reloc into an equivalent one without a symbol
4853 relative to the start of the ELF image. */
4854 rel->r_addend += (relocation
4856 + elf_section_data (ea)->this_hdr.sh_offset);
4857 rel->r_info = ELF32_R_INFO (0, r_type);
4859 emit_these_relocs = TRUE;
4863 unresolved_reloc = TRUE;
4865 if (unresolved_reloc)
4867 (*_bfd_error_handler)
4868 (_("%B(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
4870 bfd_get_section_name (input_bfd, input_section),
4871 (long) rel->r_offset,
4877 r = _bfd_final_link_relocate (howto,
4881 rel->r_offset, relocation, addend);
4883 if (r != bfd_reloc_ok)
4885 const char *msg = (const char *) 0;
4889 case bfd_reloc_overflow:
4890 if (!((*info->callbacks->reloc_overflow)
4891 (info, (h ? &h->root : NULL), sym_name, howto->name,
4892 (bfd_vma) 0, input_bfd, input_section, rel->r_offset)))
4896 case bfd_reloc_undefined:
4897 if (!((*info->callbacks->undefined_symbol)
4898 (info, sym_name, input_bfd, input_section,
4899 rel->r_offset, TRUE)))
4903 case bfd_reloc_outofrange:
4904 msg = _("internal error: out of range error");
4907 case bfd_reloc_notsupported:
4908 msg = _("internal error: unsupported relocation error");
4911 case bfd_reloc_dangerous:
4912 msg = _("internal error: dangerous error");
4916 msg = _("internal error: unknown error");
4921 if (!((*info->callbacks->warning)
4922 (info, msg, sym_name, input_bfd, input_section,
4931 && emit_these_relocs
4932 && !info->emitrelocations)
4934 Elf_Internal_Rela *wrel;
4935 Elf_Internal_Shdr *rel_hdr;
4937 wrel = rel = relocs;
4938 relend = relocs + input_section->reloc_count;
4939 for (; rel < relend; rel++)
4943 r_type = ELF32_R_TYPE (rel->r_info);
4944 if (r_type == R_SPU_PPU32 || r_type == R_SPU_PPU64)
4947 input_section->reloc_count = wrel - relocs;
4948 /* Backflips for _bfd_elf_link_output_relocs. */
4949 rel_hdr = &elf_section_data (input_section)->rel_hdr;
4950 rel_hdr->sh_size = input_section->reloc_count * rel_hdr->sh_entsize;
4957 /* Adjust _SPUEAR_ syms to point at their overlay stubs. */
4960 spu_elf_output_symbol_hook (struct bfd_link_info *info,
4961 const char *sym_name ATTRIBUTE_UNUSED,
4962 Elf_Internal_Sym *sym,
4963 asection *sym_sec ATTRIBUTE_UNUSED,
4964 struct elf_link_hash_entry *h)
4966 struct spu_link_hash_table *htab = spu_hash_table (info);
4968 if (!info->relocatable
4969 && htab->stub_sec != NULL
4971 && (h->root.type == bfd_link_hash_defined
4972 || h->root.type == bfd_link_hash_defweak)
4974 && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0)
4976 struct got_entry *g;
4978 for (g = h->got.glist; g != NULL; g = g->next)
4979 if (htab->params->ovly_flavour == ovly_soft_icache
4980 ? g->br_addr == g->stub_addr
4981 : g->addend == 0 && g->ovl == 0)
4983 sym->st_shndx = (_bfd_elf_section_from_bfd_section
4984 (htab->stub_sec[0]->output_section->owner,
4985 htab->stub_sec[0]->output_section));
4986 sym->st_value = g->stub_addr;
4994 static int spu_plugin = 0;
4997 spu_elf_plugin (int val)
5002 /* Set ELF header e_type for plugins. */
5005 spu_elf_post_process_headers (bfd *abfd,
5006 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5010 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5012 i_ehdrp->e_type = ET_DYN;
5016 /* We may add an extra PT_LOAD segment for .toe. We also need extra
5017 segments for overlays. */
5020 spu_elf_additional_program_headers (bfd *abfd, struct bfd_link_info *info)
5027 struct spu_link_hash_table *htab = spu_hash_table (info);
5028 extra = htab->num_overlays;
5034 sec = bfd_get_section_by_name (abfd, ".toe");
5035 if (sec != NULL && (sec->flags & SEC_LOAD) != 0)
5041 /* Remove .toe section from other PT_LOAD segments and put it in
5042 a segment of its own. Put overlays in separate segments too. */
5045 spu_elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
5048 struct elf_segment_map *m, *m_overlay;
5049 struct elf_segment_map **p, **p_overlay;
5055 toe = bfd_get_section_by_name (abfd, ".toe");
5056 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
5057 if (m->p_type == PT_LOAD && m->count > 1)
5058 for (i = 0; i < m->count; i++)
5059 if ((s = m->sections[i]) == toe
5060 || spu_elf_section_data (s)->u.o.ovl_index != 0)
5062 struct elf_segment_map *m2;
5065 if (i + 1 < m->count)
5067 amt = sizeof (struct elf_segment_map);
5068 amt += (m->count - (i + 2)) * sizeof (m->sections[0]);
5069 m2 = bfd_zalloc (abfd, amt);
5072 m2->count = m->count - (i + 1);
5073 memcpy (m2->sections, m->sections + i + 1,
5074 m2->count * sizeof (m->sections[0]));
5075 m2->p_type = PT_LOAD;
5083 amt = sizeof (struct elf_segment_map);
5084 m2 = bfd_zalloc (abfd, amt);
5087 m2->p_type = PT_LOAD;
5089 m2->sections[0] = s;
5097 /* Some SPU ELF loaders ignore the PF_OVERLAY flag and just load all
5098 PT_LOAD segments. This can cause the .ovl.init section to be
5099 overwritten with the contents of some overlay segment. To work
5100 around this issue, we ensure that all PF_OVERLAY segments are
5101 sorted first amongst the program headers; this ensures that even
5102 with a broken loader, the .ovl.init section (which is not marked
5103 as PF_OVERLAY) will be placed into SPU local store on startup. */
5105 /* Move all overlay segments onto a separate list. */
5106 p = &elf_tdata (abfd)->segment_map;
5107 p_overlay = &m_overlay;
5110 if ((*p)->p_type == PT_LOAD && (*p)->count == 1
5111 && spu_elf_section_data ((*p)->sections[0])->u.o.ovl_index != 0)
5113 struct elf_segment_map *m = *p;
5116 p_overlay = &m->next;
5123 /* Re-insert overlay segments at the head of the segment map. */
5124 *p_overlay = elf_tdata (abfd)->segment_map;
5125 elf_tdata (abfd)->segment_map = m_overlay;
5130 /* Tweak the section type of .note.spu_name. */
5133 spu_elf_fake_sections (bfd *obfd ATTRIBUTE_UNUSED,
5134 Elf_Internal_Shdr *hdr,
5137 if (strcmp (sec->name, SPU_PTNOTE_SPUNAME) == 0)
5138 hdr->sh_type = SHT_NOTE;
5142 /* Tweak phdrs before writing them out. */
5145 spu_elf_modify_program_headers (bfd *abfd, struct bfd_link_info *info)
5147 const struct elf_backend_data *bed;
5148 struct elf_obj_tdata *tdata;
5149 Elf_Internal_Phdr *phdr, *last;
5150 struct spu_link_hash_table *htab;
5157 bed = get_elf_backend_data (abfd);
5158 tdata = elf_tdata (abfd);
5160 count = tdata->program_header_size / bed->s->sizeof_phdr;
5161 htab = spu_hash_table (info);
5162 if (htab->num_overlays != 0)
5164 struct elf_segment_map *m;
5167 for (i = 0, m = elf_tdata (abfd)->segment_map; m; ++i, m = m->next)
5169 && (o = spu_elf_section_data (m->sections[0])->u.o.ovl_index) != 0)
5171 /* Mark this as an overlay header. */
5172 phdr[i].p_flags |= PF_OVERLAY;
5174 if (htab->ovtab != NULL && htab->ovtab->size != 0
5175 && htab->params->ovly_flavour != ovly_soft_icache)
5177 bfd_byte *p = htab->ovtab->contents;
5178 unsigned int off = o * 16 + 8;
5180 /* Write file_off into _ovly_table. */
5181 bfd_put_32 (htab->ovtab->owner, phdr[i].p_offset, p + off);
5184 /* Soft-icache has its file offset put in .ovl.init. */
5185 if (htab->init != NULL && htab->init->size != 0)
5187 bfd_vma val = elf_section_data (htab->ovl_sec[0])->this_hdr.sh_offset;
5189 bfd_put_32 (htab->init->owner, val, htab->init->contents + 4);
5193 /* Round up p_filesz and p_memsz of PT_LOAD segments to multiples
5194 of 16. This should always be possible when using the standard
5195 linker scripts, but don't create overlapping segments if
5196 someone is playing games with linker scripts. */
5198 for (i = count; i-- != 0; )
5199 if (phdr[i].p_type == PT_LOAD)
5203 adjust = -phdr[i].p_filesz & 15;
5206 && phdr[i].p_offset + phdr[i].p_filesz > last->p_offset - adjust)
5209 adjust = -phdr[i].p_memsz & 15;
5212 && phdr[i].p_filesz != 0
5213 && phdr[i].p_vaddr + phdr[i].p_memsz > last->p_vaddr - adjust
5214 && phdr[i].p_vaddr + phdr[i].p_memsz <= last->p_vaddr)
5217 if (phdr[i].p_filesz != 0)
5221 if (i == (unsigned int) -1)
5222 for (i = count; i-- != 0; )
5223 if (phdr[i].p_type == PT_LOAD)
5227 adjust = -phdr[i].p_filesz & 15;
5228 phdr[i].p_filesz += adjust;
5230 adjust = -phdr[i].p_memsz & 15;
5231 phdr[i].p_memsz += adjust;
5237 #define TARGET_BIG_SYM bfd_elf32_spu_vec
5238 #define TARGET_BIG_NAME "elf32-spu"
5239 #define ELF_ARCH bfd_arch_spu
5240 #define ELF_MACHINE_CODE EM_SPU
5241 /* This matches the alignment need for DMA. */
5242 #define ELF_MAXPAGESIZE 0x80
5243 #define elf_backend_rela_normal 1
5244 #define elf_backend_can_gc_sections 1
5246 #define bfd_elf32_bfd_reloc_type_lookup spu_elf_reloc_type_lookup
5247 #define bfd_elf32_bfd_reloc_name_lookup spu_elf_reloc_name_lookup
5248 #define elf_info_to_howto spu_elf_info_to_howto
5249 #define elf_backend_count_relocs spu_elf_count_relocs
5250 #define elf_backend_relocate_section spu_elf_relocate_section
5251 #define elf_backend_symbol_processing spu_elf_backend_symbol_processing
5252 #define elf_backend_link_output_symbol_hook spu_elf_output_symbol_hook
5253 #define elf_backend_object_p spu_elf_object_p
5254 #define bfd_elf32_new_section_hook spu_elf_new_section_hook
5255 #define bfd_elf32_bfd_link_hash_table_create spu_elf_link_hash_table_create
5257 #define elf_backend_additional_program_headers spu_elf_additional_program_headers
5258 #define elf_backend_modify_segment_map spu_elf_modify_segment_map
5259 #define elf_backend_modify_program_headers spu_elf_modify_program_headers
5260 #define elf_backend_post_process_headers spu_elf_post_process_headers
5261 #define elf_backend_fake_sections spu_elf_fake_sections
5262 #define elf_backend_special_sections spu_elf_special_sections
5263 #define bfd_elf32_bfd_final_link spu_elf_final_link
5265 #include "elf32-target.h"