Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/bfd/elf32-spu.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations) (download) (as text)
Mon Jul 27 20:34:36 2009 UTC (14 years, 8 months ago) by monamour
File MIME type: text/x-csrc
File size: 146711 byte(s)
Update to HEAD.
1 /* SPU specific support for 32-bit ELF
2
3 Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/spu.h"
28 #include "elf32-spu.h"
29
30 /* We use RELA style relocs. Don't define USE_REL. */
31
32 static bfd_reloc_status_type spu_elf_rel9 (bfd *, arelent *, asymbol *,
33 void *, asection *,
34 bfd *, char **);
35
36 /* Values of type 'enum elf_spu_reloc_type' are used to index this
37 array, so it must be declared in the order of that type. */
38
39 static reloc_howto_type elf_howto_table[] = {
40 HOWTO (R_SPU_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
41 bfd_elf_generic_reloc, "SPU_NONE",
42 FALSE, 0, 0x00000000, FALSE),
43 HOWTO (R_SPU_ADDR10, 4, 2, 10, FALSE, 14, complain_overflow_bitfield,
44 bfd_elf_generic_reloc, "SPU_ADDR10",
45 FALSE, 0, 0x00ffc000, FALSE),
46 HOWTO (R_SPU_ADDR16, 2, 2, 16, FALSE, 7, complain_overflow_bitfield,
47 bfd_elf_generic_reloc, "SPU_ADDR16",
48 FALSE, 0, 0x007fff80, FALSE),
49 HOWTO (R_SPU_ADDR16_HI, 16, 2, 16, FALSE, 7, complain_overflow_bitfield,
50 bfd_elf_generic_reloc, "SPU_ADDR16_HI",
51 FALSE, 0, 0x007fff80, FALSE),
52 HOWTO (R_SPU_ADDR16_LO, 0, 2, 16, FALSE, 7, complain_overflow_dont,
53 bfd_elf_generic_reloc, "SPU_ADDR16_LO",
54 FALSE, 0, 0x007fff80, FALSE),
55 HOWTO (R_SPU_ADDR18, 0, 2, 18, FALSE, 7, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "SPU_ADDR18",
57 FALSE, 0, 0x01ffff80, FALSE),
58 HOWTO (R_SPU_ADDR32, 0, 2, 32, FALSE, 0, complain_overflow_dont,
59 bfd_elf_generic_reloc, "SPU_ADDR32",
60 FALSE, 0, 0xffffffff, FALSE),
61 HOWTO (R_SPU_REL16, 2, 2, 16, TRUE, 7, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "SPU_REL16",
63 FALSE, 0, 0x007fff80, TRUE),
64 HOWTO (R_SPU_ADDR7, 0, 2, 7, FALSE, 14, complain_overflow_dont,
65 bfd_elf_generic_reloc, "SPU_ADDR7",
66 FALSE, 0, 0x001fc000, FALSE),
67 HOWTO (R_SPU_REL9, 2, 2, 9, TRUE, 0, complain_overflow_signed,
68 spu_elf_rel9, "SPU_REL9",
69 FALSE, 0, 0x0180007f, TRUE),
70 HOWTO (R_SPU_REL9I, 2, 2, 9, TRUE, 0, complain_overflow_signed,
71 spu_elf_rel9, "SPU_REL9I",
72 FALSE, 0, 0x0000c07f, TRUE),
73 HOWTO (R_SPU_ADDR10I, 0, 2, 10, FALSE, 14, complain_overflow_signed,
74 bfd_elf_generic_reloc, "SPU_ADDR10I",
75 FALSE, 0, 0x00ffc000, FALSE),
76 HOWTO (R_SPU_ADDR16I, 0, 2, 16, FALSE, 7, complain_overflow_signed,
77 bfd_elf_generic_reloc, "SPU_ADDR16I",
78 FALSE, 0, 0x007fff80, FALSE),
79 HOWTO (R_SPU_REL32, 0, 2, 32, TRUE, 0, complain_overflow_dont,
80 bfd_elf_generic_reloc, "SPU_REL32",
81 FALSE, 0, 0xffffffff, TRUE),
82 HOWTO (R_SPU_ADDR16X, 0, 2, 16, FALSE, 7, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "SPU_ADDR16X",
84 FALSE, 0, 0x007fff80, FALSE),
85 HOWTO (R_SPU_PPU32, 0, 2, 32, FALSE, 0, complain_overflow_dont,
86 bfd_elf_generic_reloc, "SPU_PPU32",
87 FALSE, 0, 0xffffffff, FALSE),
88 HOWTO (R_SPU_PPU64, 0, 4, 64, FALSE, 0, complain_overflow_dont,
89 bfd_elf_generic_reloc, "SPU_PPU64",
90 FALSE, 0, -1, FALSE),
91 HOWTO (R_SPU_ADD_PIC, 0, 0, 0, FALSE, 0, complain_overflow_dont,
92 bfd_elf_generic_reloc, "SPU_ADD_PIC",
93 FALSE, 0, 0x00000000, FALSE),
94 };
95
96 static struct bfd_elf_special_section const spu_elf_special_sections[] = {
97 { "._ea", 4, 0, SHT_PROGBITS, SHF_WRITE },
98 { ".toe", 4, 0, SHT_NOBITS, SHF_ALLOC },
99 { NULL, 0, 0, 0, 0 }
100 };
101
102 static enum elf_spu_reloc_type
103 spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code)
104 {
105 switch (code)
106 {
107 default:
108 return R_SPU_NONE;
109 case BFD_RELOC_SPU_IMM10W:
110 return R_SPU_ADDR10;
111 case BFD_RELOC_SPU_IMM16W:
112 return R_SPU_ADDR16;
113 case BFD_RELOC_SPU_LO16:
114 return R_SPU_ADDR16_LO;
115 case BFD_RELOC_SPU_HI16:
116 return R_SPU_ADDR16_HI;
117 case BFD_RELOC_SPU_IMM18:
118 return R_SPU_ADDR18;
119 case BFD_RELOC_SPU_PCREL16:
120 return R_SPU_REL16;
121 case BFD_RELOC_SPU_IMM7:
122 return R_SPU_ADDR7;
123 case BFD_RELOC_SPU_IMM8:
124 return R_SPU_NONE;
125 case BFD_RELOC_SPU_PCREL9a:
126 return R_SPU_REL9;
127 case BFD_RELOC_SPU_PCREL9b:
128 return R_SPU_REL9I;
129 case BFD_RELOC_SPU_IMM10:
130 return R_SPU_ADDR10I;
131 case BFD_RELOC_SPU_IMM16:
132 return R_SPU_ADDR16I;
133 case BFD_RELOC_32:
134 return R_SPU_ADDR32;
135 case BFD_RELOC_32_PCREL:
136 return R_SPU_REL32;
137 case BFD_RELOC_SPU_PPU32:
138 return R_SPU_PPU32;
139 case BFD_RELOC_SPU_PPU64:
140 return R_SPU_PPU64;
141 case BFD_RELOC_SPU_ADD_PIC:
142 return R_SPU_ADD_PIC;
143 }
144 }
145
146 static void
147 spu_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
148 arelent *cache_ptr,
149 Elf_Internal_Rela *dst)
150 {
151 enum elf_spu_reloc_type r_type;
152
153 r_type = (enum elf_spu_reloc_type) ELF32_R_TYPE (dst->r_info);
154 BFD_ASSERT (r_type < R_SPU_max);
155 cache_ptr->howto = &elf_howto_table[(int) r_type];
156 }
157
158 static reloc_howto_type *
159 spu_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
160 bfd_reloc_code_real_type code)
161 {
162 enum elf_spu_reloc_type r_type = spu_elf_bfd_to_reloc_type (code);
163
164 if (r_type == R_SPU_NONE)
165 return NULL;
166
167 return elf_howto_table + r_type;
168 }
169
170 static reloc_howto_type *
171 spu_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
172 const char *r_name)
173 {
174 unsigned int i;
175
176 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
177 if (elf_howto_table[i].name != NULL
178 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
179 return &elf_howto_table[i];
180
181 return NULL;
182 }
183
184 /* Apply R_SPU_REL9 and R_SPU_REL9I relocs. */
185
186 static bfd_reloc_status_type
187 spu_elf_rel9 (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
188 void *data, asection *input_section,
189 bfd *output_bfd, char **error_message)
190 {
191 bfd_size_type octets;
192 bfd_vma val;
193 long insn;
194
195 /* If this is a relocatable link (output_bfd test tells us), just
196 call the generic function. Any adjustment will be done at final
197 link time. */
198 if (output_bfd != NULL)
199 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
200 input_section, output_bfd, error_message);
201
202 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
203 return bfd_reloc_outofrange;
204 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
205
206 /* Get symbol value. */
207 val = 0;
208 if (!bfd_is_com_section (symbol->section))
209 val = symbol->value;
210 if (symbol->section->output_section)
211 val += symbol->section->output_section->vma;
212
213 val += reloc_entry->addend;
214
215 /* Make it pc-relative. */
216 val -= input_section->output_section->vma + input_section->output_offset;
217
218 val >>= 2;
219 if (val + 256 >= 512)
220 return bfd_reloc_overflow;
221
222 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
223
224 /* Move two high bits of value to REL9I and REL9 position.
225 The mask will take care of selecting the right field. */
226 val = (val & 0x7f) | ((val & 0x180) << 7) | ((val & 0x180) << 16);
227 insn &= ~reloc_entry->howto->dst_mask;
228 insn |= val & reloc_entry->howto->dst_mask;
229 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
230 return bfd_reloc_ok;
231 }
232
233 static bfd_boolean
234 spu_elf_new_section_hook (bfd *abfd, asection *sec)
235 {
236 if (!sec->used_by_bfd)
237 {
238 struct _spu_elf_section_data *sdata;
239
240 sdata = bfd_zalloc (abfd, sizeof (*sdata));
241 if (sdata == NULL)
242 return FALSE;
243 sec->used_by_bfd = sdata;
244 }
245
246 return _bfd_elf_new_section_hook (abfd, sec);
247 }
248
249 /* Set up overlay info for executables. */
250
251 static bfd_boolean
252 spu_elf_object_p (bfd *abfd)
253 {
254 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
255 {
256 unsigned int i, num_ovl, num_buf;
257 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
258 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
259 Elf_Internal_Phdr *last_phdr = NULL;
260
261 for (num_buf = 0, num_ovl = 0, i = 0; i < ehdr->e_phnum; i++, phdr++)
262 if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_OVERLAY) != 0)
263 {
264 unsigned int j;
265
266 ++num_ovl;
267 if (last_phdr == NULL
268 || ((last_phdr->p_vaddr ^ phdr->p_vaddr) & 0x3ffff) != 0)
269 ++num_buf;
270 last_phdr = phdr;
271 for (j = 1; j < elf_numsections (abfd); j++)
272 {
273 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[j];
274
275 if (ELF_IS_SECTION_IN_SEGMENT_MEMORY (shdr, phdr))
276 {
277 asection *sec = shdr->bfd_section;
278 spu_elf_section_data (sec)->u.o.ovl_index = num_ovl;
279 spu_elf_section_data (sec)->u.o.ovl_buf = num_buf;
280 }
281 }
282 }
283 }
284 return TRUE;
285 }
286
287 /* Specially mark defined symbols named _EAR_* with BSF_KEEP so that
288 strip --strip-unneeded will not remove them. */
289
290 static void
291 spu_elf_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
292 {
293 if (sym->name != NULL
294 && sym->section != bfd_abs_section_ptr
295 && strncmp (sym->name, "_EAR_", 5) == 0)
296 sym->flags |= BSF_KEEP;
297 }
298
299 /* SPU ELF linker hash table. */
300
301 struct spu_link_hash_table
302 {
303 struct elf_link_hash_table elf;
304
305 struct spu_elf_params *params;
306
307 /* Shortcuts to overlay sections. */
308 asection *ovtab;
309 asection *init;
310 asection *toe;
311 asection **ovl_sec;
312
313 /* Count of stubs in each overlay section. */
314 unsigned int *stub_count;
315
316 /* The stub section for each overlay section. */
317 asection **stub_sec;
318
319 struct elf_link_hash_entry *ovly_entry[2];
320
321 /* Number of overlay buffers. */
322 unsigned int num_buf;
323
324 /* Total number of overlays. */
325 unsigned int num_overlays;
326
327 /* For soft icache. */
328 unsigned int line_size_log2;
329 unsigned int num_lines_log2;
330 unsigned int fromelem_size_log2;
331
332 /* How much memory we have. */
333 unsigned int local_store;
334 /* Local store --auto-overlay should reserve for non-overlay
335 functions and data. */
336 unsigned int overlay_fixed;
337 /* Local store --auto-overlay should reserve for stack and heap. */
338 unsigned int reserved;
339 /* If reserved is not specified, stack analysis will calculate a value
340 for the stack. This parameter adjusts that value to allow for
341 negative sp access (the ABI says 2000 bytes below sp are valid,
342 and the overlay manager uses some of this area). */
343 int extra_stack_space;
344 /* Count of overlay stubs needed in non-overlay area. */
345 unsigned int non_ovly_stub;
346
347 /* Set on error. */
348 unsigned int stub_err : 1;
349 };
350
351 /* Hijack the generic got fields for overlay stub accounting. */
352
353 struct got_entry
354 {
355 struct got_entry *next;
356 unsigned int ovl;
357 union {
358 bfd_vma addend;
359 bfd_vma br_addr;
360 };
361 bfd_vma stub_addr;
362 };
363
364 #define spu_hash_table(p) \
365 ((struct spu_link_hash_table *) ((p)->hash))
366
367 struct call_info
368 {
369 struct function_info *fun;
370 struct call_info *next;
371 unsigned int count;
372 unsigned int max_depth;
373 unsigned int is_tail : 1;
374 unsigned int is_pasted : 1;
375 unsigned int broken_cycle : 1;
376 unsigned int priority : 13;
377 };
378
379 struct function_info
380 {
381 /* List of functions called. Also branches to hot/cold part of
382 function. */
383 struct call_info *call_list;
384 /* For hot/cold part of function, point to owner. */
385 struct function_info *start;
386 /* Symbol at start of function. */
387 union {
388 Elf_Internal_Sym *sym;
389 struct elf_link_hash_entry *h;
390 } u;
391 /* Function section. */
392 asection *sec;
393 asection *rodata;
394 /* Where last called from, and number of sections called from. */
395 asection *last_caller;
396 unsigned int call_count;
397 /* Address range of (this part of) function. */
398 bfd_vma lo, hi;
399 /* Offset where we found a store of lr, or -1 if none found. */
400 bfd_vma lr_store;
401 /* Offset where we found the stack adjustment insn. */
402 bfd_vma sp_adjust;
403 /* Stack usage. */
404 int stack;
405 /* Distance from root of call tree. Tail and hot/cold branches
406 count as one deeper. We aren't counting stack frames here. */
407 unsigned int depth;
408 /* Set if global symbol. */
409 unsigned int global : 1;
410 /* Set if known to be start of function (as distinct from a hunk
411 in hot/cold section. */
412 unsigned int is_func : 1;
413 /* Set if not a root node. */
414 unsigned int non_root : 1;
415 /* Flags used during call tree traversal. It's cheaper to replicate
416 the visit flags than have one which needs clearing after a traversal. */
417 unsigned int visit1 : 1;
418 unsigned int visit2 : 1;
419 unsigned int marking : 1;
420 unsigned int visit3 : 1;
421 unsigned int visit4 : 1;
422 unsigned int visit5 : 1;
423 unsigned int visit6 : 1;
424 unsigned int visit7 : 1;
425 };
426
427 struct spu_elf_stack_info
428 {
429 int num_fun;
430 int max_fun;
431 /* Variable size array describing functions, one per contiguous
432 address range belonging to a function. */
433 struct function_info fun[1];
434 };
435
436 static struct function_info *find_function (asection *, bfd_vma,
437 struct bfd_link_info *);
438
439 /* Create a spu ELF linker hash table. */
440
441 static struct bfd_link_hash_table *
442 spu_elf_link_hash_table_create (bfd *abfd)
443 {
444 struct spu_link_hash_table *htab;
445
446 htab = bfd_malloc (sizeof (*htab));
447 if (htab == NULL)
448 return NULL;
449
450 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd,
451 _bfd_elf_link_hash_newfunc,
452 sizeof (struct elf_link_hash_entry)))
453 {
454 free (htab);
455 return NULL;
456 }
457
458 memset (&htab->ovtab, 0,
459 sizeof (*htab) - offsetof (struct spu_link_hash_table, ovtab));
460
461 htab->elf.init_got_refcount.refcount = 0;
462 htab->elf.init_got_refcount.glist = NULL;
463 htab->elf.init_got_offset.offset = 0;
464 htab->elf.init_got_offset.glist = NULL;
465 return &htab->elf.root;
466 }
467
468 void
469 spu_elf_setup (struct bfd_link_info *info, struct spu_elf_params *params)
470 {
471 bfd_vma max_branch_log2;
472
473 struct spu_link_hash_table *htab = spu_hash_table (info);
474 htab->params = params;
475 htab->line_size_log2 = bfd_log2 (htab->params->line_size);
476 htab->num_lines_log2 = bfd_log2 (htab->params->num_lines);
477
478 /* For the software i-cache, we provide a "from" list whose size
479 is a power-of-two number of quadwords, big enough to hold one
480 byte per outgoing branch. Compute this number here. */
481 max_branch_log2 = bfd_log2 (htab->params->max_branch);
482 htab->fromelem_size_log2 = max_branch_log2 > 4 ? max_branch_log2 - 4 : 0;
483 }
484
485 /* Find the symbol for the given R_SYMNDX in IBFD and set *HP and *SYMP
486 to (hash, NULL) for global symbols, and (NULL, sym) for locals. Set
487 *SYMSECP to the symbol's section. *LOCSYMSP caches local syms. */
488
489 static bfd_boolean
490 get_sym_h (struct elf_link_hash_entry **hp,
491 Elf_Internal_Sym **symp,
492 asection **symsecp,
493 Elf_Internal_Sym **locsymsp,
494 unsigned long r_symndx,
495 bfd *ibfd)
496 {
497 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
498
499 if (r_symndx >= symtab_hdr->sh_info)
500 {
501 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
502 struct elf_link_hash_entry *h;
503
504 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
505 while (h->root.type == bfd_link_hash_indirect
506 || h->root.type == bfd_link_hash_warning)
507 h = (struct elf_link_hash_entry *) h->root.u.i.link;
508
509 if (hp != NULL)
510 *hp = h;
511
512 if (symp != NULL)
513 *symp = NULL;
514
515 if (symsecp != NULL)
516 {
517 asection *symsec = NULL;
518 if (h->root.type == bfd_link_hash_defined
519 || h->root.type == bfd_link_hash_defweak)
520 symsec = h->root.u.def.section;
521 *symsecp = symsec;
522 }
523 }
524 else
525 {
526 Elf_Internal_Sym *sym;
527 Elf_Internal_Sym *locsyms = *locsymsp;
528
529 if (locsyms == NULL)
530 {
531 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
532 if (locsyms == NULL)
533 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
534 symtab_hdr->sh_info,
535 0, NULL, NULL, NULL);
536 if (locsyms == NULL)
537 return FALSE;
538 *locsymsp = locsyms;
539 }
540 sym = locsyms + r_symndx;
541
542 if (hp != NULL)
543 *hp = NULL;
544
545 if (symp != NULL)
546 *symp = sym;
547
548 if (symsecp != NULL)
549 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
550 }
551
552 return TRUE;
553 }
554
555 /* Create the note section if not already present. This is done early so
556 that the linker maps the sections to the right place in the output. */
557
558 bfd_boolean
559 spu_elf_create_sections (struct bfd_link_info *info)
560 {
561 bfd *ibfd;
562
563 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
564 if (bfd_get_section_by_name (ibfd, SPU_PTNOTE_SPUNAME) != NULL)
565 break;
566
567 if (ibfd == NULL)
568 {
569 /* Make SPU_PTNOTE_SPUNAME section. */
570 asection *s;
571 size_t name_len;
572 size_t size;
573 bfd_byte *data;
574 flagword flags;
575
576 ibfd = info->input_bfds;
577 flags = SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
578 s = bfd_make_section_anyway_with_flags (ibfd, SPU_PTNOTE_SPUNAME, flags);
579 if (s == NULL
580 || !bfd_set_section_alignment (ibfd, s, 4))
581 return FALSE;
582
583 name_len = strlen (bfd_get_filename (info->output_bfd)) + 1;
584 size = 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4);
585 size += (name_len + 3) & -4;
586
587 if (!bfd_set_section_size (ibfd, s, size))
588 return FALSE;
589
590 data = bfd_zalloc (ibfd, size);
591 if (data == NULL)
592 return FALSE;
593
594 bfd_put_32 (ibfd, sizeof (SPU_PLUGIN_NAME), data + 0);
595 bfd_put_32 (ibfd, name_len, data + 4);
596 bfd_put_32 (ibfd, 1, data + 8);
597 memcpy (data + 12, SPU_PLUGIN_NAME, sizeof (SPU_PLUGIN_NAME));
598 memcpy (data + 12 + ((sizeof (SPU_PLUGIN_NAME) + 3) & -4),
599 bfd_get_filename (info->output_bfd), name_len);
600 s->contents = data;
601 }
602
603 return TRUE;
604 }
605
606 /* qsort predicate to sort sections by vma. */
607
608 static int
609 sort_sections (const void *a, const void *b)
610 {
611 const asection *const *s1 = a;
612 const asection *const *s2 = b;
613 bfd_signed_vma delta = (*s1)->vma - (*s2)->vma;
614
615 if (delta != 0)
616 return delta < 0 ? -1 : 1;
617
618 return (*s1)->index - (*s2)->index;
619 }
620
621 /* Identify overlays in the output bfd, and number them.
622 Returns 0 on error, 1 if no overlays, 2 if overlays. */
623
624 int
625 spu_elf_find_overlays (struct bfd_link_info *info)
626 {
627 struct spu_link_hash_table *htab = spu_hash_table (info);
628 asection **alloc_sec;
629 unsigned int i, n, ovl_index, num_buf;
630 asection *s;
631 bfd_vma ovl_end;
632 static const char *const entry_names[2][2] = {
633 { "__ovly_load", "__icache_br_handler" },
634 { "__ovly_return", "__icache_call_handler" }
635 };
636
637 if (info->output_bfd->section_count < 2)
638 return 1;
639
640 alloc_sec
641 = bfd_malloc (info->output_bfd->section_count * sizeof (*alloc_sec));
642 if (alloc_sec == NULL)
643 return 0;
644
645 /* Pick out all the alloced sections. */
646 for (n = 0, s = info->output_bfd->sections; s != NULL; s = s->next)
647 if ((s->flags & SEC_ALLOC) != 0
648 && (s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != SEC_THREAD_LOCAL
649 && s->size != 0)
650 alloc_sec[n++] = s;
651
652 if (n == 0)
653 {
654 free (alloc_sec);
655 return 1;
656 }
657
658 /* Sort them by vma. */
659 qsort (alloc_sec, n, sizeof (*alloc_sec), sort_sections);
660
661 ovl_end = alloc_sec[0]->vma + alloc_sec[0]->size;
662 if (htab->params->ovly_flavour == ovly_soft_icache)
663 {
664 /* Look for an overlapping vma to find the first overlay section. */
665 bfd_vma vma_start = 0;
666 bfd_vma lma_start = 0;
667
668 for (i = 1; i < n; i++)
669 {
670 s = alloc_sec[i];
671 if (s->vma < ovl_end)
672 {
673 asection *s0 = alloc_sec[i - 1];
674 vma_start = s0->vma;
675 if (strncmp (s0->name, ".ovl.init", 9) != 0)
676 lma_start = s0->lma;
677 else
678 lma_start = s->lma;
679 ovl_end = (s0->vma
680 + ((bfd_vma) 1
681 << (htab->num_lines_log2 + htab->line_size_log2)));
682 --i;
683 break;
684 }
685 else
686 ovl_end = s->vma + s->size;
687 }
688
689 /* Now find any sections within the cache area. */
690 for (ovl_index = 0, num_buf = 0; i < n; i++)
691 {
692 s = alloc_sec[i];
693 if (s->vma >= ovl_end)
694 break;
695
696 /* A section in an overlay area called .ovl.init is not
697 an overlay, in the sense that it might be loaded in
698 by the overlay manager, but rather the initial
699 section contents for the overlay buffer. */
700 if (strncmp (s->name, ".ovl.init", 9) != 0)
701 {
702 num_buf = ((s->vma - vma_start) >> htab->line_size_log2) + 1;
703 if (((s->vma - vma_start) & (htab->params->line_size - 1))
704 || ((s->lma - lma_start) & (htab->params->line_size - 1)))
705 {
706 info->callbacks->einfo (_("%X%P: overlay section %A "
707 "does not start on a cache line.\n"),
708 s);
709 bfd_set_error (bfd_error_bad_value);
710 return 0;
711 }
712 else if (s->size > htab->params->line_size)
713 {
714 info->callbacks->einfo (_("%X%P: overlay section %A "
715 "is larger than a cache line.\n"),
716 s);
717 bfd_set_error (bfd_error_bad_value);
718 return 0;
719 }
720
721 alloc_sec[ovl_index++] = s;
722 spu_elf_section_data (s)->u.o.ovl_index
723 = ((s->lma - lma_start) >> htab->line_size_log2) + 1;
724 spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
725 }
726 }
727
728 /* Ensure there are no more overlay sections. */
729 for ( ; i < n; i++)
730 {
731 s = alloc_sec[i];
732 if (s->vma < ovl_end)
733 {
734 info->callbacks->einfo (_("%X%P: overlay section %A "
735 "is not in cache area.\n"),
736 alloc_sec[i-1]);
737 bfd_set_error (bfd_error_bad_value);
738 return 0;
739 }
740 else
741 ovl_end = s->vma + s->size;
742 }
743 }
744 else
745 {
746 /* Look for overlapping vmas. Any with overlap must be overlays.
747 Count them. Also count the number of overlay regions. */
748 for (ovl_index = 0, num_buf = 0, i = 1; i < n; i++)
749 {
750 s = alloc_sec[i];
751 if (s->vma < ovl_end)
752 {
753 asection *s0 = alloc_sec[i - 1];
754
755 if (spu_elf_section_data (s0)->u.o.ovl_index == 0)
756 {
757 ++num_buf;
758 if (strncmp (s0->name, ".ovl.init", 9) != 0)
759 {
760 alloc_sec[ovl_index] = s0;
761 spu_elf_section_data (s0)->u.o.ovl_index = ++ovl_index;
762 spu_elf_section_data (s0)->u.o.ovl_buf = num_buf;
763 }
764 else
765 ovl_end = s->vma + s->size;
766 }
767 if (strncmp (s->name, ".ovl.init", 9) != 0)
768 {
769 alloc_sec[ovl_index] = s;
770 spu_elf_section_data (s)->u.o.ovl_index = ++ovl_index;
771 spu_elf_section_data (s)->u.o.ovl_buf = num_buf;
772 if (s0->vma != s->vma)
773 {
774 info->callbacks->einfo (_("%X%P: overlay sections %A "
775 "and %A do not start at the "
776 "same address.\n"),
777 s0, s);
778 bfd_set_error (bfd_error_bad_value);
779 return 0;
780 }
781 if (ovl_end < s->vma + s->size)
782 ovl_end = s->vma + s->size;
783 }
784 }
785 else
786 ovl_end = s->vma + s->size;
787 }
788 }
789
790 htab->num_overlays = ovl_index;
791 htab->num_buf = num_buf;
792 htab->ovl_sec = alloc_sec;
793
794 if (ovl_index == 0)
795 return 1;
796
797 for (i = 0; i < 2; i++)
798 {
799 const char *name;
800 struct elf_link_hash_entry *h;
801
802 name = entry_names[i][htab->params->ovly_flavour];
803 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
804 if (h == NULL)
805 return 0;
806
807 if (h->root.type == bfd_link_hash_new)
808 {
809 h->root.type = bfd_link_hash_undefined;
810 h->ref_regular = 1;
811 h->ref_regular_nonweak = 1;
812 h->non_elf = 0;
813 }
814 htab->ovly_entry[i] = h;
815 }
816
817 return 2;
818 }
819
820 /* Non-zero to use bra in overlay stubs rather than br. */
821 #define BRA_STUBS 0
822
823 #define BRA 0x30000000
824 #define BRASL 0x31000000
825 #define BR 0x32000000
826 #define BRSL 0x33000000
827 #define NOP 0x40200000
828 #define LNOP 0x00200000
829 #define ILA 0x42000000
830
831 /* Return true for all relative and absolute branch instructions.
832 bra 00110000 0..
833 brasl 00110001 0..
834 br 00110010 0..
835 brsl 00110011 0..
836 brz 00100000 0..
837 brnz 00100001 0..
838 brhz 00100010 0..
839 brhnz 00100011 0.. */
840
841 static bfd_boolean
842 is_branch (const unsigned char *insn)
843 {
844 return (insn[0] & 0xec) == 0x20 && (insn[1] & 0x80) == 0;
845 }
846
847 /* Return true for all indirect branch instructions.
848 bi 00110101 000
849 bisl 00110101 001
850 iret 00110101 010
851 bisled 00110101 011
852 biz 00100101 000
853 binz 00100101 001
854 bihz 00100101 010
855 bihnz 00100101 011 */
856
857 static bfd_boolean
858 is_indirect_branch (const unsigned char *insn)
859 {
860 return (insn[0] & 0xef) == 0x25 && (insn[1] & 0x80) == 0;
861 }
862
863 /* Return true for branch hint instructions.
864 hbra 0001000..
865 hbrr 0001001.. */
866
867 static bfd_boolean
868 is_hint (const unsigned char *insn)
869 {
870 return (insn[0] & 0xfc) == 0x10;
871 }
872
873 /* True if INPUT_SECTION might need overlay stubs. */
874
875 static bfd_boolean
876 maybe_needs_stubs (asection *input_section)
877 {
878 /* No stubs for debug sections and suchlike. */
879 if ((input_section->flags & SEC_ALLOC) == 0)
880 return FALSE;
881
882 /* No stubs for link-once sections that will be discarded. */
883 if (input_section->output_section == bfd_abs_section_ptr)
884 return FALSE;
885
886 /* Don't create stubs for .eh_frame references. */
887 if (strcmp (input_section->name, ".eh_frame") == 0)
888 return FALSE;
889
890 return TRUE;
891 }
892
893 enum _stub_type
894 {
895 no_stub,
896 call_ovl_stub,
897 br000_ovl_stub,
898 br001_ovl_stub,
899 br010_ovl_stub,
900 br011_ovl_stub,
901 br100_ovl_stub,
902 br101_ovl_stub,
903 br110_ovl_stub,
904 br111_ovl_stub,
905 nonovl_stub,
906 stub_error
907 };
908
909 /* Return non-zero if this reloc symbol should go via an overlay stub.
910 Return 2 if the stub must be in non-overlay area. */
911
912 static enum _stub_type
913 needs_ovl_stub (struct elf_link_hash_entry *h,
914 Elf_Internal_Sym *sym,
915 asection *sym_sec,
916 asection *input_section,
917 Elf_Internal_Rela *irela,
918 bfd_byte *contents,
919 struct bfd_link_info *info)
920 {
921 struct spu_link_hash_table *htab = spu_hash_table (info);
922 enum elf_spu_reloc_type r_type;
923 unsigned int sym_type;
924 bfd_boolean branch, hint, call;
925 enum _stub_type ret = no_stub;
926 bfd_byte insn[4];
927
928 if (sym_sec == NULL
929 || sym_sec->output_section == bfd_abs_section_ptr
930 || spu_elf_section_data (sym_sec->output_section) == NULL)
931 return ret;
932
933 if (h != NULL)
934 {
935 /* Ensure no stubs for user supplied overlay manager syms. */
936 if (h == htab->ovly_entry[0] || h == htab->ovly_entry[1])
937 return ret;
938
939 /* setjmp always goes via an overlay stub, because then the return
940 and hence the longjmp goes via __ovly_return. That magically
941 makes setjmp/longjmp between overlays work. */
942 if (strncmp (h->root.root.string, "setjmp", 6) == 0
943 && (h->root.root.string[6] == '\0' || h->root.root.string[6] == '@'))
944 ret = call_ovl_stub;
945 }
946
947 if (h != NULL)
948 sym_type = h->type;
949 else
950 sym_type = ELF_ST_TYPE (sym->st_info);
951
952 r_type = ELF32_R_TYPE (irela->r_info);
953 branch = FALSE;
954 hint = FALSE;
955 call = FALSE;
956 if (r_type == R_SPU_REL16 || r_type == R_SPU_ADDR16)
957 {
958 if (contents == NULL)
959 {
960 contents = insn;
961 if (!bfd_get_section_contents (input_section->owner,
962 input_section,
963 contents,
964 irela->r_offset, 4))
965 return stub_error;
966 }
967 else
968 contents += irela->r_offset;
969
970 branch = is_branch (contents);
971 hint = is_hint (contents);
972 if (branch || hint)
973 {
974 call = (contents[0] & 0xfd) == 0x31;
975 if (call
976 && sym_type != STT_FUNC
977 && contents != insn)
978 {
979 /* It's common for people to write assembly and forget
980 to give function symbols the right type. Handle
981 calls to such symbols, but warn so that (hopefully)
982 people will fix their code. We need the symbol
983 type to be correct to distinguish function pointer
984 initialisation from other pointer initialisations. */
985 const char *sym_name;
986
987 if (h != NULL)
988 sym_name = h->root.root.string;
989 else
990 {
991 Elf_Internal_Shdr *symtab_hdr;
992 symtab_hdr = &elf_tdata (input_section->owner)->symtab_hdr;
993 sym_name = bfd_elf_sym_name (input_section->owner,
994 symtab_hdr,
995 sym,
996 sym_sec);
997 }
998 (*_bfd_error_handler) (_("warning: call to non-function"
999 " symbol %s defined in %B"),
1000 sym_sec->owner, sym_name);
1001
1002 }
1003 }
1004 }
1005
1006 if ((!branch && htab->params->ovly_flavour == ovly_soft_icache)
1007 || (sym_type != STT_FUNC
1008 && !(branch || hint)
1009 && (sym_sec->flags & SEC_CODE) == 0))
1010 return no_stub;
1011
1012 /* Usually, symbols in non-overlay sections don't need stubs. */
1013 if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index == 0
1014 && !htab->params->non_overlay_stubs)
1015 return ret;
1016
1017 /* A reference from some other section to a symbol in an overlay
1018 section needs a stub. */
1019 if (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index
1020 != spu_elf_section_data (input_section->output_section)->u.o.ovl_index)
1021 {
1022 unsigned int lrlive = 0;
1023 if (branch)
1024 lrlive = (contents[1] & 0x70) >> 4;
1025
1026 if (!lrlive && (call || sym_type == STT_FUNC))
1027 ret = call_ovl_stub;
1028 else
1029 ret = br000_ovl_stub + lrlive;
1030 }
1031
1032 /* If this insn isn't a branch then we are possibly taking the
1033 address of a function and passing it out somehow. Soft-icache code
1034 always generates inline code to do indirect branches. */
1035 if (!(branch || hint)
1036 && sym_type == STT_FUNC
1037 && htab->params->ovly_flavour != ovly_soft_icache)
1038 ret = nonovl_stub;
1039
1040 return ret;
1041 }
1042
1043 static bfd_boolean
1044 count_stub (struct spu_link_hash_table *htab,
1045 bfd *ibfd,
1046 asection *isec,
1047 enum _stub_type stub_type,
1048 struct elf_link_hash_entry *h,
1049 const Elf_Internal_Rela *irela)
1050 {
1051 unsigned int ovl = 0;
1052 struct got_entry *g, **head;
1053 bfd_vma addend;
1054
1055 /* If this instruction is a branch or call, we need a stub
1056 for it. One stub per function per overlay.
1057 If it isn't a branch, then we are taking the address of
1058 this function so need a stub in the non-overlay area
1059 for it. One stub per function. */
1060 if (stub_type != nonovl_stub)
1061 ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index;
1062
1063 if (h != NULL)
1064 head = &h->got.glist;
1065 else
1066 {
1067 if (elf_local_got_ents (ibfd) == NULL)
1068 {
1069 bfd_size_type amt = (elf_tdata (ibfd)->symtab_hdr.sh_info
1070 * sizeof (*elf_local_got_ents (ibfd)));
1071 elf_local_got_ents (ibfd) = bfd_zmalloc (amt);
1072 if (elf_local_got_ents (ibfd) == NULL)
1073 return FALSE;
1074 }
1075 head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
1076 }
1077
1078 if (htab->params->ovly_flavour == ovly_soft_icache)
1079 {
1080 htab->stub_count[ovl] += 1;
1081 return TRUE;
1082 }
1083
1084 addend = 0;
1085 if (irela != NULL)
1086 addend = irela->r_addend;
1087
1088 if (ovl == 0)
1089 {
1090 struct got_entry *gnext;
1091
1092 for (g = *head; g != NULL; g = g->next)
1093 if (g->addend == addend && g->ovl == 0)
1094 break;
1095
1096 if (g == NULL)
1097 {
1098 /* Need a new non-overlay area stub. Zap other stubs. */
1099 for (g = *head; g != NULL; g = gnext)
1100 {
1101 gnext = g->next;
1102 if (g->addend == addend)
1103 {
1104 htab->stub_count[g->ovl] -= 1;
1105 free (g);
1106 }
1107 }
1108 }
1109 }
1110 else
1111 {
1112 for (g = *head; g != NULL; g = g->next)
1113 if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
1114 break;
1115 }
1116
1117 if (g == NULL)
1118 {
1119 g = bfd_malloc (sizeof *g);
1120 if (g == NULL)
1121 return FALSE;
1122 g->ovl = ovl;
1123 g->addend = addend;
1124 g->stub_addr = (bfd_vma) -1;
1125 g->next = *head;
1126 *head = g;
1127
1128 htab->stub_count[ovl] += 1;
1129 }
1130
1131 return TRUE;
1132 }
1133
1134 /* Support two sizes of overlay stubs, a slower more compact stub of two
1135 intructions, and a faster stub of four instructions.
1136 Soft-icache stubs are four or eight words. */
1137
1138 static unsigned int
1139 ovl_stub_size (struct spu_elf_params *params)
1140 {
1141 return 16 << params->ovly_flavour >> params->compact_stub;
1142 }
1143
1144 static unsigned int
1145 ovl_stub_size_log2 (struct spu_elf_params *params)
1146 {
1147 return 4 + params->ovly_flavour - params->compact_stub;
1148 }
1149
1150 /* Two instruction overlay stubs look like:
1151
1152 brsl $75,__ovly_load
1153 .word target_ovl_and_address
1154
1155 ovl_and_address is a word with the overlay number in the top 14 bits
1156 and local store address in the bottom 18 bits.
1157
1158 Four instruction overlay stubs look like:
1159
1160 ila $78,ovl_number
1161 lnop
1162 ila $79,target_address
1163 br __ovly_load
1164
1165 Software icache stubs are:
1166
1167 .word target_index
1168 .word target_ia;
1169 .word lrlive_branchlocalstoreaddr;
1170 brasl $75,__icache_br_handler
1171 .quad xor_pattern
1172 */
1173
1174 static bfd_boolean
1175 build_stub (struct bfd_link_info *info,
1176 bfd *ibfd,
1177 asection *isec,
1178 enum _stub_type stub_type,
1179 struct elf_link_hash_entry *h,
1180 const Elf_Internal_Rela *irela,
1181 bfd_vma dest,
1182 asection *dest_sec)
1183 {
1184 struct spu_link_hash_table *htab = spu_hash_table (info);
1185 unsigned int ovl, dest_ovl, set_id;
1186 struct got_entry *g, **head;
1187 asection *sec;
1188 bfd_vma addend, from, to, br_dest, patt;
1189 unsigned int lrlive;
1190
1191 ovl = 0;
1192 if (stub_type != nonovl_stub)
1193 ovl = spu_elf_section_data (isec->output_section)->u.o.ovl_index;
1194
1195 if (h != NULL)
1196 head = &h->got.glist;
1197 else
1198 head = elf_local_got_ents (ibfd) + ELF32_R_SYM (irela->r_info);
1199
1200 addend = 0;
1201 if (irela != NULL)
1202 addend = irela->r_addend;
1203
1204 if (htab->params->ovly_flavour == ovly_soft_icache)
1205 {
1206 g = bfd_malloc (sizeof *g);
1207 if (g == NULL)
1208 return FALSE;
1209 g->ovl = ovl;
1210 g->br_addr = 0;
1211 if (irela != NULL)
1212 g->br_addr = (irela->r_offset
1213 + isec->output_offset
1214 + isec->output_section->vma);
1215 g->next = *head;
1216 *head = g;
1217 }
1218 else
1219 {
1220 for (g = *head; g != NULL; g = g->next)
1221 if (g->addend == addend && (g->ovl == ovl || g->ovl == 0))
1222 break;
1223 if (g == NULL)
1224 abort ();
1225
1226 if (g->ovl == 0 && ovl != 0)
1227 return TRUE;
1228
1229 if (g->stub_addr != (bfd_vma) -1)
1230 return TRUE;
1231 }
1232
1233 sec = htab->stub_sec[ovl];
1234 dest += dest_sec->output_offset + dest_sec->output_section->vma;
1235 from = sec->size + sec->output_offset + sec->output_section->vma;
1236 g->stub_addr = from;
1237 to = (htab->ovly_entry[0]->root.u.def.value
1238 + htab->ovly_entry[0]->root.u.def.section->output_offset
1239 + htab->ovly_entry[0]->root.u.def.section->output_section->vma);
1240
1241 if (((dest | to | from) & 3) != 0)
1242 {
1243 htab->stub_err = 1;
1244 return FALSE;
1245 }
1246 dest_ovl = spu_elf_section_data (dest_sec->output_section)->u.o.ovl_index;
1247
1248 if (htab->params->ovly_flavour == ovly_normal
1249 && !htab->params->compact_stub)
1250 {
1251 bfd_put_32 (sec->owner, ILA + ((dest_ovl << 7) & 0x01ffff80) + 78,
1252 sec->contents + sec->size);
1253 bfd_put_32 (sec->owner, LNOP,
1254 sec->contents + sec->size + 4);
1255 bfd_put_32 (sec->owner, ILA + ((dest << 7) & 0x01ffff80) + 79,
1256 sec->contents + sec->size + 8);
1257 if (!BRA_STUBS)
1258 bfd_put_32 (sec->owner, BR + (((to - (from + 12)) << 5) & 0x007fff80),
1259 sec->contents + sec->size + 12);
1260 else
1261 bfd_put_32 (sec->owner, BRA + ((to << 5) & 0x007fff80),
1262 sec->contents + sec->size + 12);
1263 }
1264 else if (htab->params->ovly_flavour == ovly_normal
1265 && htab->params->compact_stub)
1266 {
1267 if (!BRA_STUBS)
1268 bfd_put_32 (sec->owner, BRSL + (((to - from) << 5) & 0x007fff80) + 75,
1269 sec->contents + sec->size);
1270 else
1271 bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
1272 sec->contents + sec->size);
1273 bfd_put_32 (sec->owner, (dest & 0x3ffff) | (dest_ovl << 18),
1274 sec->contents + sec->size + 4);
1275 }
1276 else if (htab->params->ovly_flavour == ovly_soft_icache
1277 && htab->params->compact_stub)
1278 {
1279 lrlive = 0;
1280 if (stub_type == nonovl_stub)
1281 ;
1282 else if (stub_type == call_ovl_stub)
1283 /* A brsl makes lr live and *(*sp+16) is live.
1284 Tail calls have the same liveness. */
1285 lrlive = 5;
1286 else if (!htab->params->lrlive_analysis)
1287 /* Assume stack frame and lr save. */
1288 lrlive = 1;
1289 else if (irela != NULL)
1290 {
1291 /* Analyse branch instructions. */
1292 struct function_info *caller;
1293 bfd_vma off;
1294
1295 caller = find_function (isec, irela->r_offset, info);
1296 if (caller->start == NULL)
1297 off = irela->r_offset;
1298 else
1299 {
1300 struct function_info *found = NULL;
1301
1302 /* Find the earliest piece of this function that
1303 has frame adjusting instructions. We might
1304 see dynamic frame adjustment (eg. for alloca)
1305 in some later piece, but functions using
1306 alloca always set up a frame earlier. Frame
1307 setup instructions are always in one piece. */
1308 if (caller->lr_store != (bfd_vma) -1
1309 || caller->sp_adjust != (bfd_vma) -1)
1310 found = caller;
1311 while (caller->start != NULL)
1312 {
1313 caller = caller->start;
1314 if (caller->lr_store != (bfd_vma) -1
1315 || caller->sp_adjust != (bfd_vma) -1)
1316 found = caller;
1317 }
1318 if (found != NULL)
1319 caller = found;
1320 off = (bfd_vma) -1;
1321 }
1322
1323 if (off > caller->sp_adjust)
1324 {
1325 if (off > caller->lr_store)
1326 /* Only *(*sp+16) is live. */
1327 lrlive = 1;
1328 else
1329 /* If no lr save, then we must be in a
1330 leaf function with a frame.
1331 lr is still live. */
1332 lrlive = 4;
1333 }
1334 else if (off > caller->lr_store)
1335 {
1336 /* Between lr save and stack adjust. */
1337 lrlive = 3;
1338 /* This should never happen since prologues won't
1339 be split here. */
1340 BFD_ASSERT (0);
1341 }
1342 else
1343 /* On entry to function. */
1344 lrlive = 5;
1345
1346 if (stub_type != br000_ovl_stub
1347 && lrlive != stub_type - br000_ovl_stub)
1348 info->callbacks->einfo (_("%A:0x%v lrlive .brinfo (%u) differs "
1349 "from analysis (%u)\n"),
1350 isec, irela->r_offset, lrlive,
1351 stub_type - br000_ovl_stub);
1352 }
1353
1354 /* If given lrlive info via .brinfo, use it. */
1355 if (stub_type > br000_ovl_stub)
1356 lrlive = stub_type - br000_ovl_stub;
1357
1358 if (ovl == 0)
1359 to = (htab->ovly_entry[1]->root.u.def.value
1360 + htab->ovly_entry[1]->root.u.def.section->output_offset
1361 + htab->ovly_entry[1]->root.u.def.section->output_section->vma);
1362
1363 /* The branch that uses this stub goes to stub_addr + 4. We'll
1364 set up an xor pattern that can be used by the icache manager
1365 to modify this branch to go directly to its destination. */
1366 g->stub_addr += 4;
1367 br_dest = g->stub_addr;
1368 if (irela == NULL)
1369 {
1370 /* Except in the case of _SPUEAR_ stubs, the branch in
1371 question is the one in the stub itself. */
1372 BFD_ASSERT (stub_type == nonovl_stub);
1373 g->br_addr = g->stub_addr;
1374 br_dest = to;
1375 }
1376
1377 set_id = ((dest_ovl - 1) >> htab->num_lines_log2) + 1;
1378 bfd_put_32 (sec->owner, (set_id << 18) | (dest & 0x3ffff),
1379 sec->contents + sec->size);
1380 bfd_put_32 (sec->owner, BRASL + ((to << 5) & 0x007fff80) + 75,
1381 sec->contents + sec->size + 4);
1382 bfd_put_32 (sec->owner, (lrlive << 29) | (g->br_addr & 0x3ffff),
1383 sec->contents + sec->size + 8);
1384 patt = dest ^ br_dest;
1385 if (irela != NULL && ELF32_R_TYPE (irela->r_info) == R_SPU_REL16)
1386 patt = (dest - g->br_addr) ^ (br_dest - g->br_addr);
1387 bfd_put_32 (sec->owner, (patt << 5) & 0x007fff80,
1388 sec->contents + sec->size + 12);
1389
1390 if (ovl == 0)
1391 /* Extra space for linked list entries. */
1392 sec->size += 16;
1393 }
1394 else
1395 abort ();
1396
1397 sec->size += ovl_stub_size (htab->params);
1398
1399 if (htab->params->emit_stub_syms)
1400 {
1401 size_t len;
1402 char *name;
1403 int add;
1404
1405 len = 8 + sizeof (".ovl_call.") - 1;
1406 if (h != NULL)
1407 len += strlen (h->root.root.string);
1408 else
1409 len += 8 + 1 + 8;
1410 add = 0;
1411 if (irela != NULL)
1412 add = (int) irela->r_addend & 0xffffffff;
1413 if (add != 0)
1414 len += 1 + 8;
1415 name = bfd_malloc (len);
1416 if (name == NULL)
1417 return FALSE;
1418
1419 sprintf (name, "%08x.ovl_call.", g->ovl);
1420 if (h != NULL)
1421 strcpy (name + 8 + sizeof (".ovl_call.") - 1, h->root.root.string);
1422 else
1423 sprintf (name + 8 + sizeof (".ovl_call.") - 1, "%x:%x",
1424 dest_sec->id & 0xffffffff,
1425 (int) ELF32_R_SYM (irela->r_info) & 0xffffffff);
1426 if (add != 0)
1427 sprintf (name + len - 9, "+%x", add);
1428
1429 h = elf_link_hash_lookup (&htab->elf, name, TRUE, TRUE, FALSE);
1430 free (name);
1431 if (h == NULL)
1432 return FALSE;
1433 if (h->root.type == bfd_link_hash_new)
1434 {
1435 h->root.type = bfd_link_hash_defined;
1436 h->root.u.def.section = sec;
1437 h->size = ovl_stub_size (htab->params);
1438 h->root.u.def.value = sec->size - h->size;
1439 h->type = STT_FUNC;
1440 h->ref_regular = 1;
1441 h->def_regular = 1;
1442 h->ref_regular_nonweak = 1;
1443 h->forced_local = 1;
1444 h->non_elf = 0;
1445 }
1446 }
1447
1448 return TRUE;
1449 }
1450
1451 /* Called via elf_link_hash_traverse to allocate stubs for any _SPUEAR_
1452 symbols. */
1453
1454 static bfd_boolean
1455 allocate_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
1456 {
1457 /* Symbols starting with _SPUEAR_ need a stub because they may be
1458 invoked by the PPU. */
1459 struct bfd_link_info *info = inf;
1460 struct spu_link_hash_table *htab = spu_hash_table (info);
1461 asection *sym_sec;
1462
1463 if ((h->root.type == bfd_link_hash_defined
1464 || h->root.type == bfd_link_hash_defweak)
1465 && h->def_regular
1466 && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0
1467 && (sym_sec = h->root.u.def.section) != NULL
1468 && sym_sec->output_section != bfd_abs_section_ptr
1469 && spu_elf_section_data (sym_sec->output_section) != NULL
1470 && (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index != 0
1471 || htab->params->non_overlay_stubs))
1472 {
1473 return count_stub (htab, NULL, NULL, nonovl_stub, h, NULL);
1474 }
1475
1476 return TRUE;
1477 }
1478
1479 static bfd_boolean
1480 build_spuear_stubs (struct elf_link_hash_entry *h, void *inf)
1481 {
1482 /* Symbols starting with _SPUEAR_ need a stub because they may be
1483 invoked by the PPU. */
1484 struct bfd_link_info *info = inf;
1485 struct spu_link_hash_table *htab = spu_hash_table (info);
1486 asection *sym_sec;
1487
1488 if ((h->root.type == bfd_link_hash_defined
1489 || h->root.type == bfd_link_hash_defweak)
1490 && h->def_regular
1491 && strncmp (h->root.root.string, "_SPUEAR_", 8) == 0
1492 && (sym_sec = h->root.u.def.section) != NULL
1493 && sym_sec->output_section != bfd_abs_section_ptr
1494 && spu_elf_section_data (sym_sec->output_section) != NULL
1495 && (spu_elf_section_data (sym_sec->output_section)->u.o.ovl_index != 0
1496 || htab->params->non_overlay_stubs))
1497 {
1498 return build_stub (info, NULL, NULL, nonovl_stub, h, NULL,
1499 h->root.u.def.value, sym_sec);
1500 }
1501
1502 return TRUE;
1503 }
1504
1505 /* Size or build stubs. */
1506
1507 static bfd_boolean
1508 process_stubs (struct bfd_link_info *info, bfd_boolean build)
1509 {
1510 struct spu_link_hash_table *htab = spu_hash_table (info);
1511 bfd *ibfd;
1512
1513 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1514 {
1515 extern const bfd_target bfd_elf32_spu_vec;
1516 Elf_Internal_Shdr *symtab_hdr;
1517 asection *isec;
1518 Elf_Internal_Sym *local_syms = NULL;
1519
1520 if (ibfd->xvec != &bfd_elf32_spu_vec)
1521 continue;
1522
1523 /* We'll need the symbol table in a second. */
1524 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1525 if (symtab_hdr->sh_info == 0)
1526 continue;
1527
1528 /* Walk over each section attached to the input bfd. */
1529 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
1530 {
1531 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1532
1533 /* If there aren't any relocs, then there's nothing more to do. */
1534 if ((isec->flags & SEC_RELOC) == 0
1535 || isec->reloc_count == 0)
1536 continue;
1537
1538 if (!maybe_needs_stubs (isec))
1539 continue;
1540
1541 /* Get the relocs. */
1542 internal_relocs = _bfd_elf_link_read_relocs (ibfd, isec, NULL, NULL,
1543 info->keep_memory);
1544 if (internal_relocs == NULL)
1545 goto error_ret_free_local;
1546
1547 /* Now examine each relocation. */
1548 irela = internal_relocs;
1549 irelaend = irela + isec->reloc_count;
1550 for (; irela < irelaend; irela++)
1551 {
1552 enum elf_spu_reloc_type r_type;
1553 unsigned int r_indx;
1554 asection *sym_sec;
1555 Elf_Internal_Sym *sym;
1556 struct elf_link_hash_entry *h;
1557 enum _stub_type stub_type;
1558
1559 r_type = ELF32_R_TYPE (irela->r_info);
1560 r_indx = ELF32_R_SYM (irela->r_info);
1561
1562 if (r_type >= R_SPU_max)
1563 {
1564 bfd_set_error (bfd_error_bad_value);
1565 error_ret_free_internal:
1566 if (elf_section_data (isec)->relocs != internal_relocs)
1567 free (internal_relocs);
1568 error_ret_free_local:
1569 if (local_syms != NULL
1570 && (symtab_hdr->contents
1571 != (unsigned char *) local_syms))
1572 free (local_syms);
1573 return FALSE;
1574 }
1575
1576 /* Determine the reloc target section. */
1577 if (!get_sym_h (&h, &sym, &sym_sec, &local_syms, r_indx, ibfd))
1578 goto error_ret_free_internal;
1579
1580 stub_type = needs_ovl_stub (h, sym, sym_sec, isec, irela,
1581 NULL, info);
1582 if (stub_type == no_stub)
1583 continue;
1584 else if (stub_type == stub_error)
1585 goto error_ret_free_internal;
1586
1587 if (htab->stub_count == NULL)
1588 {
1589 bfd_size_type amt;
1590 amt = (htab->num_overlays + 1) * sizeof (*htab->stub_count);
1591 htab->stub_count = bfd_zmalloc (amt);
1592 if (htab->stub_count == NULL)
1593 goto error_ret_free_internal;
1594 }
1595
1596 if (!build)
1597 {
1598 if (!count_stub (htab, ibfd, isec, stub_type, h, irela))
1599 goto error_ret_free_internal;
1600 }
1601 else
1602 {
1603 bfd_vma dest;
1604
1605 if (h != NULL)
1606 dest = h->root.u.def.value;
1607 else
1608 dest = sym->st_value;
1609 dest += irela->r_addend;
1610 if (!build_stub (info, ibfd, isec, stub_type, h, irela,
1611 dest, sym_sec))
1612 goto error_ret_free_internal;
1613 }
1614 }
1615
1616 /* We're done with the internal relocs, free them. */
1617 if (elf_section_data (isec)->relocs != internal_relocs)
1618 free (internal_relocs);
1619 }
1620
1621 if (local_syms != NULL
1622 && symtab_hdr->contents != (unsigned char *) local_syms)
1623 {
1624 if (!info->keep_memory)
1625 free (local_syms);
1626 else
1627 symtab_hdr->contents = (unsigned char *) local_syms;
1628 }
1629 }
1630
1631 return TRUE;
1632 }
1633
1634 /* Allocate space for overlay call and return stubs.
1635 Return 0 on error, 1 if no overlays, 2 otherwise. */
1636
1637 int
1638 spu_elf_size_stubs (struct bfd_link_info *info)
1639 {
1640 struct spu_link_hash_table *htab;
1641 bfd *ibfd;
1642 bfd_size_type amt;
1643 flagword flags;
1644 unsigned int i;
1645 asection *stub;
1646
1647 if (!process_stubs (info, FALSE))
1648 return 0;
1649
1650 htab = spu_hash_table (info);
1651 elf_link_hash_traverse (&htab->elf, allocate_spuear_stubs, info);
1652 if (htab->stub_err)
1653 return 0;
1654
1655 ibfd = info->input_bfds;
1656 if (htab->stub_count != NULL)
1657 {
1658 amt = (htab->num_overlays + 1) * sizeof (*htab->stub_sec);
1659 htab->stub_sec = bfd_zmalloc (amt);
1660 if (htab->stub_sec == NULL)
1661 return 0;
1662
1663 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
1664 | SEC_HAS_CONTENTS | SEC_IN_MEMORY);
1665 stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
1666 htab->stub_sec[0] = stub;
1667 if (stub == NULL
1668 || !bfd_set_section_alignment (ibfd, stub,
1669 ovl_stub_size_log2 (htab->params)))
1670 return 0;
1671 stub->size = htab->stub_count[0] * ovl_stub_size (htab->params);
1672 if (htab->params->ovly_flavour == ovly_soft_icache)
1673 /* Extra space for linked list entries. */
1674 stub->size += htab->stub_count[0] * 16;
1675
1676 for (i = 0; i < htab->num_overlays; ++i)
1677 {
1678 asection *osec = htab->ovl_sec[i];
1679 unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
1680 stub = bfd_make_section_anyway_with_flags (ibfd, ".stub", flags);
1681 htab->stub_sec[ovl] = stub;
1682 if (stub == NULL
1683 || !bfd_set_section_alignment (ibfd, stub,
1684 ovl_stub_size_log2 (htab->params)))
1685 return 0;
1686 stub->size = htab->stub_count[ovl] * ovl_stub_size (htab->params);
1687 }
1688 }
1689
1690 if (htab->params->ovly_flavour == ovly_soft_icache)
1691 {
1692 /* Space for icache manager tables.
1693 a) Tag array, one quadword per cache line.
1694 b) Rewrite "to" list, one quadword per cache line.
1695 c) Rewrite "from" list, one byte per outgoing branch (rounded up to
1696 a power-of-two number of full quadwords) per cache line. */
1697
1698 flags = SEC_ALLOC;
1699 htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags);
1700 if (htab->ovtab == NULL
1701 || !bfd_set_section_alignment (ibfd, htab->ovtab, 4))
1702 return 0;
1703
1704 htab->ovtab->size = (16 + 16 + (16 << htab->fromelem_size_log2))
1705 << htab->num_lines_log2;
1706
1707 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1708 htab->init = bfd_make_section_anyway_with_flags (ibfd, ".ovini", flags);
1709 if (htab->init == NULL
1710 || !bfd_set_section_alignment (ibfd, htab->init, 4))
1711 return 0;
1712
1713 htab->init->size = 16;
1714 }
1715 else if (htab->stub_count == NULL)
1716 return 1;
1717 else
1718 {
1719 /* htab->ovtab consists of two arrays.
1720 . struct {
1721 . u32 vma;
1722 . u32 size;
1723 . u32 file_off;
1724 . u32 buf;
1725 . } _ovly_table[];
1726 .
1727 . struct {
1728 . u32 mapped;
1729 . } _ovly_buf_table[];
1730 . */
1731
1732 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1733 htab->ovtab = bfd_make_section_anyway_with_flags (ibfd, ".ovtab", flags);
1734 if (htab->ovtab == NULL
1735 || !bfd_set_section_alignment (ibfd, htab->ovtab, 4))
1736 return 0;
1737
1738 htab->ovtab->size = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
1739 }
1740
1741 htab->toe = bfd_make_section_anyway_with_flags (ibfd, ".toe", SEC_ALLOC);
1742 if (htab->toe == NULL
1743 || !bfd_set_section_alignment (ibfd, htab->toe, 4))
1744 return 0;
1745 htab->toe->size = 16;
1746
1747 return 2;
1748 }
1749
1750 /* Called from ld to place overlay manager data sections. This is done
1751 after the overlay manager itself is loaded, mainly so that the
1752 linker's htab->init section is placed after any other .ovl.init
1753 sections. */
1754
1755 void
1756 spu_elf_place_overlay_data (struct bfd_link_info *info)
1757 {
1758 struct spu_link_hash_table *htab = spu_hash_table (info);
1759 unsigned int i;
1760
1761 if (htab->stub_sec != NULL)
1762 {
1763 (*htab->params->place_spu_section) (htab->stub_sec[0], NULL, ".text");
1764
1765 for (i = 0; i < htab->num_overlays; ++i)
1766 {
1767 asection *osec = htab->ovl_sec[i];
1768 unsigned int ovl = spu_elf_section_data (osec)->u.o.ovl_index;
1769 (*htab->params->place_spu_section) (htab->stub_sec[ovl], osec, NULL);
1770 }
1771 }
1772
1773 if (htab->params->ovly_flavour == ovly_soft_icache)
1774 (*htab->params->place_spu_section) (htab->init, NULL, ".ovl.init");
1775
1776 if (htab->ovtab != NULL)
1777 {
1778 const char *ovout = ".data";
1779 if (htab->params->ovly_flavour == ovly_soft_icache)
1780 ovout = ".bss";
1781 (*htab->params->place_spu_section) (htab->ovtab, NULL, ovout);
1782 }
1783
1784 if (htab->toe != NULL)
1785 (*htab->params->place_spu_section) (htab->toe, NULL, ".toe");
1786 }
1787
1788 /* Functions to handle embedded spu_ovl.o object. */
1789
1790 static void *
1791 ovl_mgr_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *stream)
1792 {
1793 return stream;
1794 }
1795
1796 static file_ptr
1797 ovl_mgr_pread (struct bfd *abfd ATTRIBUTE_UNUSED,
1798 void *stream,
1799 void *buf,
1800 file_ptr nbytes,
1801 file_ptr offset)
1802 {
1803 struct _ovl_stream *os;
1804 size_t count;
1805 size_t max;
1806
1807 os = (struct _ovl_stream *) stream;
1808 max = (const char *) os->end - (const char *) os->start;
1809
1810 if ((ufile_ptr) offset >= max)
1811 return 0;
1812
1813 count = nbytes;
1814 if (count > max - offset)
1815 count = max - offset;
1816
1817 memcpy (buf, (const char *) os->start + offset, count);
1818 return count;
1819 }
1820
1821 bfd_boolean
1822 spu_elf_open_builtin_lib (bfd **ovl_bfd, const struct _ovl_stream *stream)
1823 {
1824 *ovl_bfd = bfd_openr_iovec ("builtin ovl_mgr",
1825 "elf32-spu",
1826 ovl_mgr_open,
1827 (void *) stream,
1828 ovl_mgr_pread,
1829 NULL,
1830 NULL);
1831 return *ovl_bfd != NULL;
1832 }
1833
1834 static unsigned int
1835 overlay_index (asection *sec)
1836 {
1837 if (sec == NULL
1838 || sec->output_section == bfd_abs_section_ptr)
1839 return 0;
1840 return spu_elf_section_data (sec->output_section)->u.o.ovl_index;
1841 }
1842
1843 /* Define an STT_OBJECT symbol. */
1844
1845 static struct elf_link_hash_entry *
1846 define_ovtab_symbol (struct spu_link_hash_table *htab, const char *name)
1847 {
1848 struct elf_link_hash_entry *h;
1849
1850 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
1851 if (h == NULL)
1852 return NULL;
1853
1854 if (h->root.type != bfd_link_hash_defined
1855 || !h->def_regular)
1856 {
1857 h->root.type = bfd_link_hash_defined;
1858 h->root.u.def.section = htab->ovtab;
1859 h->type = STT_OBJECT;
1860 h->ref_regular = 1;
1861 h->def_regular = 1;
1862 h->ref_regular_nonweak = 1;
1863 h->non_elf = 0;
1864 }
1865 else if (h->root.u.def.section->owner != NULL)
1866 {
1867 (*_bfd_error_handler) (_("%B is not allowed to define %s"),
1868 h->root.u.def.section->owner,
1869 h->root.root.string);
1870 bfd_set_error (bfd_error_bad_value);
1871 return NULL;
1872 }
1873 else
1874 {
1875 (*_bfd_error_handler) (_("you are not allowed to define %s in a script"),
1876 h->root.root.string);
1877 bfd_set_error (bfd_error_bad_value);
1878 return NULL;
1879 }
1880
1881 return h;
1882 }
1883
1884 /* Fill in all stubs and the overlay tables. */
1885
1886 static bfd_boolean
1887 spu_elf_build_stubs (struct bfd_link_info *info)
1888 {
1889 struct spu_link_hash_table *htab = spu_hash_table (info);
1890 struct elf_link_hash_entry *h;
1891 bfd_byte *p;
1892 asection *s;
1893 bfd *obfd;
1894 unsigned int i;
1895
1896 if (htab->num_overlays != 0)
1897 {
1898 for (i = 0; i < 2; i++)
1899 {
1900 h = htab->ovly_entry[i];
1901 if (h != NULL
1902 && (h->root.type == bfd_link_hash_defined
1903 || h->root.type == bfd_link_hash_defweak)
1904 && h->def_regular)
1905 {
1906 s = h->root.u.def.section->output_section;
1907 if (spu_elf_section_data (s)->u.o.ovl_index)
1908 {
1909 (*_bfd_error_handler) (_("%s in overlay section"),
1910 h->root.root.string);
1911 bfd_set_error (bfd_error_bad_value);
1912 return FALSE;
1913 }
1914 }
1915 }
1916 }
1917
1918 if (htab->stub_sec != NULL)
1919 {
1920 for (i = 0; i <= htab->num_overlays; i++)
1921 if (htab->stub_sec[i]->size != 0)
1922 {
1923 htab->stub_sec[i]->contents = bfd_zalloc (htab->stub_sec[i]->owner,
1924 htab->stub_sec[i]->size);
1925 if (htab->stub_sec[i]->contents == NULL)
1926 return FALSE;
1927 htab->stub_sec[i]->rawsize = htab->stub_sec[i]->size;
1928 htab->stub_sec[i]->size = 0;
1929 }
1930
1931 /* Fill in all the stubs. */
1932 process_stubs (info, TRUE);
1933 if (!htab->stub_err)
1934 elf_link_hash_traverse (&htab->elf, build_spuear_stubs, info);
1935
1936 if (htab->stub_err)
1937 {
1938 (*_bfd_error_handler) (_("overlay stub relocation overflow"));
1939 bfd_set_error (bfd_error_bad_value);
1940 return FALSE;
1941 }
1942
1943 for (i = 0; i <= htab->num_overlays; i++)
1944 {
1945 if (htab->stub_sec[i]->size != htab->stub_sec[i]->rawsize)
1946 {
1947 (*_bfd_error_handler) (_("stubs don't match calculated size"));
1948 bfd_set_error (bfd_error_bad_value);
1949 return FALSE;
1950 }
1951 htab->stub_sec[i]->rawsize = 0;
1952 }
1953 }
1954
1955 if (htab->ovtab == NULL || htab->ovtab->size == 0)
1956 return TRUE;
1957
1958 htab->ovtab->contents = bfd_zalloc (htab->ovtab->owner, htab->ovtab->size);
1959 if (htab->ovtab->contents == NULL)
1960 return FALSE;
1961
1962 p = htab->ovtab->contents;
1963 if (htab->params->ovly_flavour == ovly_soft_icache)
1964 {
1965 bfd_vma off;
1966
1967 h = define_ovtab_symbol (htab, "__icache_tag_array");
1968 if (h == NULL)
1969 return FALSE;
1970 h->root.u.def.value = 0;
1971 h->size = 16 << htab->num_lines_log2;
1972 off = h->size;
1973
1974 h = define_ovtab_symbol (htab, "__icache_tag_array_size");
1975 if (h == NULL)
1976 return FALSE;
1977 h->root.u.def.value = 16 << htab->num_lines_log2;
1978 h->root.u.def.section = bfd_abs_section_ptr;
1979
1980 h = define_ovtab_symbol (htab, "__icache_rewrite_to");
1981 if (h == NULL)
1982 return FALSE;
1983 h->root.u.def.value = off;
1984 h->size = 16 << htab->num_lines_log2;
1985 off += h->size;
1986
1987 h = define_ovtab_symbol (htab, "__icache_rewrite_to_size");
1988 if (h == NULL)
1989 return FALSE;
1990 h->root.u.def.value = 16 << htab->num_lines_log2;
1991 h->root.u.def.section = bfd_abs_section_ptr;
1992
1993 h = define_ovtab_symbol (htab, "__icache_rewrite_from");
1994 if (h == NULL)
1995 return FALSE;
1996 h->root.u.def.value = off;
1997 h->size = 16 << (htab->fromelem_size_log2 + htab->num_lines_log2);
1998 off += h->size;
1999
2000 h = define_ovtab_symbol (htab, "__icache_rewrite_from_size");
2001 if (h == NULL)
2002 return FALSE;
2003 h->root.u.def.value = 16 << (htab->fromelem_size_log2
2004 + htab->num_lines_log2);
2005 h->root.u.def.section = bfd_abs_section_ptr;
2006
2007 h = define_ovtab_symbol (htab, "__icache_log2_fromelemsize");
2008 if (h == NULL)
2009 return FALSE;
2010 h->root.u.def.value = htab->fromelem_size_log2;
2011 h->root.u.def.section = bfd_abs_section_ptr;
2012
2013 h = define_ovtab_symbol (htab, "__icache_base");
2014 if (h == NULL)
2015 return FALSE;
2016 h->root.u.def.value = htab->ovl_sec[0]->vma;
2017 h->root.u.def.section = bfd_abs_section_ptr;
2018 h->size = htab->num_buf << htab->line_size_log2;
2019
2020 h = define_ovtab_symbol (htab, "__icache_linesize");
2021 if (h == NULL)
2022 return FALSE;
2023 h->root.u.def.value = 1 << htab->line_size_log2;
2024 h->root.u.def.section = bfd_abs_section_ptr;
2025
2026 h = define_ovtab_symbol (htab, "__icache_log2_linesize");
2027 if (h == NULL)
2028 return FALSE;
2029 h->root.u.def.value = htab->line_size_log2;
2030 h->root.u.def.section = bfd_abs_section_ptr;
2031
2032 h = define_ovtab_symbol (htab, "__icache_neg_log2_linesize");
2033 if (h == NULL)
2034 return FALSE;
2035 h->root.u.def.value = -htab->line_size_log2;
2036 h->root.u.def.section = bfd_abs_section_ptr;
2037
2038 h = define_ovtab_symbol (htab, "__icache_cachesize");
2039 if (h == NULL)
2040 return FALSE;
2041 h->root.u.def.value = 1 << (htab->num_lines_log2 + htab->line_size_log2);
2042 h->root.u.def.section = bfd_abs_section_ptr;
2043
2044 h = define_ovtab_symbol (htab, "__icache_log2_cachesize");
2045 if (h == NULL)
2046 return FALSE;
2047 h->root.u.def.value = htab->num_lines_log2 + htab->line_size_log2;
2048 h->root.u.def.section = bfd_abs_section_ptr;
2049
2050 h = define_ovtab_symbol (htab, "__icache_neg_log2_cachesize");
2051 if (h == NULL)
2052 return FALSE;
2053 h->root.u.def.value = -(htab->num_lines_log2 + htab->line_size_log2);
2054 h->root.u.def.section = bfd_abs_section_ptr;
2055
2056 if (htab->init != NULL && htab->init->size != 0)
2057 {
2058 htab->init->contents = bfd_zalloc (htab->init->owner,
2059 htab->init->size);
2060 if (htab->init->contents == NULL)
2061 return FALSE;
2062
2063 h = define_ovtab_symbol (htab, "__icache_fileoff");
2064 if (h == NULL)
2065 return FALSE;
2066 h->root.u.def.value = 0;
2067 h->root.u.def.section = htab->init;
2068 h->size = 8;
2069 }
2070 }
2071 else
2072 {
2073 /* Write out _ovly_table. */
2074 /* set low bit of .size to mark non-overlay area as present. */
2075 p[7] = 1;
2076 obfd = htab->ovtab->output_section->owner;
2077 for (s = obfd->sections; s != NULL; s = s->next)
2078 {
2079 unsigned int ovl_index = spu_elf_section_data (s)->u.o.ovl_index;
2080
2081 if (ovl_index != 0)
2082 {
2083 unsigned long off = ovl_index * 16;
2084 unsigned int ovl_buf = spu_elf_section_data (s)->u.o.ovl_buf;
2085
2086 bfd_put_32 (htab->ovtab->owner, s->vma, p + off);
2087 bfd_put_32 (htab->ovtab->owner, (s->size + 15) & -16,
2088 p + off + 4);
2089 /* file_off written later in spu_elf_modify_program_headers. */
2090 bfd_put_32 (htab->ovtab->owner, ovl_buf, p + off + 12);
2091 }
2092 }
2093
2094 h = define_ovtab_symbol (htab, "_ovly_table");
2095 if (h == NULL)
2096 return FALSE;
2097 h->root.u.def.value = 16;
2098 h->size = htab->num_overlays * 16;
2099
2100 h = define_ovtab_symbol (htab, "_ovly_table_end");
2101 if (h == NULL)
2102 return FALSE;
2103 h->root.u.def.value = htab->num_overlays * 16 + 16;
2104 h->size = 0;
2105
2106 h = define_ovtab_symbol (htab, "_ovly_buf_table");
2107 if (h == NULL)
2108 return FALSE;
2109 h->root.u.def.value = htab->num_overlays * 16 + 16;
2110 h->size = htab->num_buf * 4;
2111
2112 h = define_ovtab_symbol (htab, "_ovly_buf_table_end");
2113 if (h == NULL)
2114 return FALSE;
2115 h->root.u.def.value = htab->num_overlays * 16 + 16 + htab->num_buf * 4;
2116 h->size = 0;
2117 }
2118
2119 h = define_ovtab_symbol (htab, "_EAR_");
2120 if (h == NULL)
2121 return FALSE;
2122 h->root.u.def.section = htab->toe;
2123 h->root.u.def.value = 0;
2124 h->size = 16;
2125
2126 return TRUE;
2127 }
2128
2129 /* Check that all loadable section VMAs lie in the range
2130 LO .. HI inclusive, and stash some parameters for --auto-overlay. */
2131
2132 asection *
2133 spu_elf_check_vma (struct bfd_link_info *info)
2134 {
2135 struct elf_segment_map *m;
2136 unsigned int i;
2137 struct spu_link_hash_table *htab = spu_hash_table (info);
2138 bfd *abfd = info->output_bfd;
2139 bfd_vma hi = htab->params->local_store_hi;
2140 bfd_vma lo = htab->params->local_store_lo;
2141
2142 htab->local_store = hi + 1 - lo;
2143
2144 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2145 if (m->p_type == PT_LOAD)
2146 for (i = 0; i < m->count; i++)
2147 if (m->sections[i]->size != 0
2148 && (m->sections[i]->vma < lo
2149 || m->sections[i]->vma > hi
2150 || m->sections[i]->vma + m->sections[i]->size - 1 > hi))
2151 return m->sections[i];
2152
2153 return NULL;
2154 }
2155
2156 /* OFFSET in SEC (presumably) is the beginning of a function prologue.
2157 Search for stack adjusting insns, and return the sp delta.
2158 If a store of lr is found save the instruction offset to *LR_STORE.
2159 If a stack adjusting instruction is found, save that offset to
2160 *SP_ADJUST. */
2161
2162 static int
2163 find_function_stack_adjust (asection *sec,
2164 bfd_vma offset,
2165 bfd_vma *lr_store,
2166 bfd_vma *sp_adjust)
2167 {
2168 int reg[128];
2169
2170 memset (reg, 0, sizeof (reg));
2171 for ( ; offset + 4 <= sec->size; offset += 4)
2172 {
2173 unsigned char buf[4];
2174 int rt, ra;
2175 int imm;
2176
2177 /* Assume no relocs on stack adjusing insns. */
2178 if (!bfd_get_section_contents (sec->owner, sec, buf, offset, 4))
2179 break;
2180
2181 rt = buf[3] & 0x7f;
2182 ra = ((buf[2] & 0x3f) << 1) | (buf[3] >> 7);
2183
2184 if (buf[0] == 0x24 /* stqd */)
2185 {
2186 if (rt == 0 /* lr */ && ra == 1 /* sp */)
2187 *lr_store = offset;
2188 continue;
2189 }
2190
2191 /* Partly decoded immediate field. */
2192 imm = (buf[1] << 9) | (buf[2] << 1) | (buf[3] >> 7);
2193
2194 if (buf[0] == 0x1c /* ai */)
2195 {
2196 imm >>= 7;
2197 imm = (imm ^ 0x200) - 0x200;
2198 reg[rt] = reg[ra] + imm;
2199
2200 if (rt == 1 /* sp */)
2201 {
2202 if (reg[rt] > 0)
2203 break;
2204 *sp_adjust = offset;
2205 return reg[rt];
2206 }
2207 }
2208 else if (buf[0] == 0x18 && (buf[1] & 0xe0) == 0 /* a */)
2209 {
2210 int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6);
2211
2212 reg[rt] = reg[ra] + reg[rb];
2213 if (rt == 1)
2214 {
2215 if (reg[rt] > 0)
2216 break;
2217 *sp_adjust = offset;
2218 return reg[rt];
2219 }
2220 }
2221 else if (buf[0] == 0x08 && (buf[1] & 0xe0) == 0 /* sf */)
2222 {
2223 int rb = ((buf[1] & 0x1f) << 2) | ((buf[2] & 0xc0) >> 6);
2224
2225 reg[rt] = reg[rb] - reg[ra];
2226 if (rt == 1)
2227 {
2228 if (reg[rt] > 0)
2229 break;
2230 *sp_adjust = offset;
2231 return reg[rt];
2232 }
2233 }
2234 else if ((buf[0] & 0xfc) == 0x40 /* il, ilh, ilhu, ila */)
2235 {
2236 if (buf[0] >= 0x42 /* ila */)
2237 imm |= (buf[0] & 1) << 17;
2238 else
2239 {
2240 imm &= 0xffff;
2241
2242 if (buf[0] == 0x40 /* il */)
2243 {
2244 if ((buf[1] & 0x80) == 0)
2245 continue;
2246 imm = (imm ^ 0x8000) - 0x8000;
2247 }
2248 else if ((buf[1] & 0x80) == 0 /* ilhu */)
2249 imm <<= 16;
2250 }
2251 reg[rt] = imm;
2252 continue;
2253 }
2254 else if (buf[0] == 0x60 && (buf[1] & 0x80) != 0 /* iohl */)
2255 {
2256 reg[rt] |= imm & 0xffff;
2257 continue;
2258 }
2259 else if (buf[0] == 0x04 /* ori */)
2260 {
2261 imm >>= 7;
2262 imm = (imm ^ 0x200) - 0x200;
2263 reg[rt] = reg[ra] | imm;
2264 continue;
2265 }
2266 else if (buf[0] == 0x32 && (buf[1] & 0x80) != 0 /* fsmbi */)
2267 {
2268 reg[rt] = ( ((imm & 0x8000) ? 0xff000000 : 0)
2269 | ((imm & 0x4000) ? 0x00ff0000 : 0)
2270 | ((imm & 0x2000) ? 0x0000ff00 : 0)
2271 | ((imm & 0x1000) ? 0x000000ff : 0));
2272 continue;
2273 }
2274 else if (buf[0] == 0x16 /* andbi */)
2275 {
2276 imm >>= 7;
2277 imm &= 0xff;
2278 imm |= imm << 8;
2279 imm |= imm << 16;
2280 reg[rt] = reg[ra] & imm;
2281 continue;
2282 }
2283 else if (buf[0] == 0x33 && imm == 1 /* brsl .+4 */)
2284 {
2285 /* Used in pic reg load. Say rt is trashed. Won't be used
2286 in stack adjust, but we need to continue past this branch. */
2287 reg[rt] = 0;
2288 continue;
2289 }
2290 else if (is_branch (buf) || is_indirect_branch (buf))
2291 /* If we hit a branch then we must be out of the prologue. */
2292 break;
2293 }
2294
2295 return 0;
2296 }
2297
2298 /* qsort predicate to sort symbols by section and value. */
2299
2300 static Elf_Internal_Sym *sort_syms_syms;
2301 static asection **sort_syms_psecs;
2302
2303 static int
2304 sort_syms (const void *a, const void *b)
2305 {
2306 Elf_Internal_Sym *const *s1 = a;
2307 Elf_Internal_Sym *const *s2 = b;
2308 asection *sec1,*sec2;
2309 bfd_signed_vma delta;
2310
2311 sec1 = sort_syms_psecs[*s1 - sort_syms_syms];
2312 sec2 = sort_syms_psecs[*s2 - sort_syms_syms];
2313
2314 if (sec1 != sec2)
2315 return sec1->index - sec2->index;
2316
2317 delta = (*s1)->st_value - (*s2)->st_value;
2318 if (delta != 0)
2319 return delta < 0 ? -1 : 1;
2320
2321 delta = (*s2)->st_size - (*s1)->st_size;
2322 if (delta != 0)
2323 return delta < 0 ? -1 : 1;
2324
2325 return *s1 < *s2 ? -1 : 1;
2326 }
2327
2328 /* Allocate a struct spu_elf_stack_info with MAX_FUN struct function_info
2329 entries for section SEC. */
2330
2331 static struct spu_elf_stack_info *
2332 alloc_stack_info (asection *sec, int max_fun)
2333 {
2334 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2335 bfd_size_type amt;
2336
2337 amt = sizeof (struct spu_elf_stack_info);
2338 amt += (max_fun - 1) * sizeof (struct function_info);
2339 sec_data->u.i.stack_info = bfd_zmalloc (amt);
2340 if (sec_data->u.i.stack_info != NULL)
2341 sec_data->u.i.stack_info->max_fun = max_fun;
2342 return sec_data->u.i.stack_info;
2343 }
2344
2345 /* Add a new struct function_info describing a (part of a) function
2346 starting at SYM_H. Keep the array sorted by address. */
2347
2348 static struct function_info *
2349 maybe_insert_function (asection *sec,
2350 void *sym_h,
2351 bfd_boolean global,
2352 bfd_boolean is_func)
2353 {
2354 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2355 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2356 int i;
2357 bfd_vma off, size;
2358
2359 if (sinfo == NULL)
2360 {
2361 sinfo = alloc_stack_info (sec, 20);
2362 if (sinfo == NULL)
2363 return NULL;
2364 }
2365
2366 if (!global)
2367 {
2368 Elf_Internal_Sym *sym = sym_h;
2369 off = sym->st_value;
2370 size = sym->st_size;
2371 }
2372 else
2373 {
2374 struct elf_link_hash_entry *h = sym_h;
2375 off = h->root.u.def.value;
2376 size = h->size;
2377 }
2378
2379 for (i = sinfo->num_fun; --i >= 0; )
2380 if (sinfo->fun[i].lo <= off)
2381 break;
2382
2383 if (i >= 0)
2384 {
2385 /* Don't add another entry for an alias, but do update some
2386 info. */
2387 if (sinfo->fun[i].lo == off)
2388 {
2389 /* Prefer globals over local syms. */
2390 if (global && !sinfo->fun[i].global)
2391 {
2392 sinfo->fun[i].global = TRUE;
2393 sinfo->fun[i].u.h = sym_h;
2394 }
2395 if (is_func)
2396 sinfo->fun[i].is_func = TRUE;
2397 return &sinfo->fun[i];
2398 }
2399 /* Ignore a zero-size symbol inside an existing function. */
2400 else if (sinfo->fun[i].hi > off && size == 0)
2401 return &sinfo->fun[i];
2402 }
2403
2404 if (sinfo->num_fun >= sinfo->max_fun)
2405 {
2406 bfd_size_type amt = sizeof (struct spu_elf_stack_info);
2407 bfd_size_type old = amt;
2408
2409 old += (sinfo->max_fun - 1) * sizeof (struct function_info);
2410 sinfo->max_fun += 20 + (sinfo->max_fun >> 1);
2411 amt += (sinfo->max_fun - 1) * sizeof (struct function_info);
2412 sinfo = bfd_realloc (sinfo, amt);
2413 if (sinfo == NULL)
2414 return NULL;
2415 memset ((char *) sinfo + old, 0, amt - old);
2416 sec_data->u.i.stack_info = sinfo;
2417 }
2418
2419 if (++i < sinfo->num_fun)
2420 memmove (&sinfo->fun[i + 1], &sinfo->fun[i],
2421 (sinfo->num_fun - i) * sizeof (sinfo->fun[i]));
2422 sinfo->fun[i].is_func = is_func;
2423 sinfo->fun[i].global = global;
2424 sinfo->fun[i].sec = sec;
2425 if (global)
2426 sinfo->fun[i].u.h = sym_h;
2427 else
2428 sinfo->fun[i].u.sym = sym_h;
2429 sinfo->fun[i].lo = off;
2430 sinfo->fun[i].hi = off + size;
2431 sinfo->fun[i].lr_store = -1;
2432 sinfo->fun[i].sp_adjust = -1;
2433 sinfo->fun[i].stack = -find_function_stack_adjust (sec, off,
2434 &sinfo->fun[i].lr_store,
2435 &sinfo->fun[i].sp_adjust);
2436 sinfo->num_fun += 1;
2437 return &sinfo->fun[i];
2438 }
2439
2440 /* Return the name of FUN. */
2441
2442 static const char *
2443 func_name (struct function_info *fun)
2444 {
2445 asection *sec;
2446 bfd *ibfd;
2447 Elf_Internal_Shdr *symtab_hdr;
2448
2449 while (fun->start != NULL)
2450 fun = fun->start;
2451
2452 if (fun->global)
2453 return fun->u.h->root.root.string;
2454
2455 sec = fun->sec;
2456 if (fun->u.sym->st_name == 0)
2457 {
2458 size_t len = strlen (sec->name);
2459 char *name = bfd_malloc (len + 10);
2460 if (name == NULL)
2461 return "(null)";
2462 sprintf (name, "%s+%lx", sec->name,
2463 (unsigned long) fun->u.sym->st_value & 0xffffffff);
2464 return name;
2465 }
2466 ibfd = sec->owner;
2467 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2468 return bfd_elf_sym_name (ibfd, symtab_hdr, fun->u.sym, sec);
2469 }
2470
2471 /* Read the instruction at OFF in SEC. Return true iff the instruction
2472 is a nop, lnop, or stop 0 (all zero insn). */
2473
2474 static bfd_boolean
2475 is_nop (asection *sec, bfd_vma off)
2476 {
2477 unsigned char insn[4];
2478
2479 if (off + 4 > sec->size
2480 || !bfd_get_section_contents (sec->owner, sec, insn, off, 4))
2481 return FALSE;
2482 if ((insn[0] & 0xbf) == 0 && (insn[1] & 0xe0) == 0x20)
2483 return TRUE;
2484 if (insn[0] == 0 && insn[1] == 0 && insn[2] == 0 && insn[3] == 0)
2485 return TRUE;
2486 return FALSE;
2487 }
2488
2489 /* Extend the range of FUN to cover nop padding up to LIMIT.
2490 Return TRUE iff some instruction other than a NOP was found. */
2491
2492 static bfd_boolean
2493 insns_at_end (struct function_info *fun, bfd_vma limit)
2494 {
2495 bfd_vma off = (fun->hi + 3) & -4;
2496
2497 while (off < limit && is_nop (fun->sec, off))
2498 off += 4;
2499 if (off < limit)
2500 {
2501 fun->hi = off;
2502 return TRUE;
2503 }
2504 fun->hi = limit;
2505 return FALSE;
2506 }
2507
2508 /* Check and fix overlapping function ranges. Return TRUE iff there
2509 are gaps in the current info we have about functions in SEC. */
2510
2511 static bfd_boolean
2512 check_function_ranges (asection *sec, struct bfd_link_info *info)
2513 {
2514 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2515 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2516 int i;
2517 bfd_boolean gaps = FALSE;
2518
2519 if (sinfo == NULL)
2520 return FALSE;
2521
2522 for (i = 1; i < sinfo->num_fun; i++)
2523 if (sinfo->fun[i - 1].hi > sinfo->fun[i].lo)
2524 {
2525 /* Fix overlapping symbols. */
2526 const char *f1 = func_name (&sinfo->fun[i - 1]);
2527 const char *f2 = func_name (&sinfo->fun[i]);
2528
2529 info->callbacks->einfo (_("warning: %s overlaps %s\n"), f1, f2);
2530 sinfo->fun[i - 1].hi = sinfo->fun[i].lo;
2531 }
2532 else if (insns_at_end (&sinfo->fun[i - 1], sinfo->fun[i].lo))
2533 gaps = TRUE;
2534
2535 if (sinfo->num_fun == 0)
2536 gaps = TRUE;
2537 else
2538 {
2539 if (sinfo->fun[0].lo != 0)
2540 gaps = TRUE;
2541 if (sinfo->fun[sinfo->num_fun - 1].hi > sec->size)
2542 {
2543 const char *f1 = func_name (&sinfo->fun[sinfo->num_fun - 1]);
2544
2545 info->callbacks->einfo (_("warning: %s exceeds section size\n"), f1);
2546 sinfo->fun[sinfo->num_fun - 1].hi = sec->size;
2547 }
2548 else if (insns_at_end (&sinfo->fun[sinfo->num_fun - 1], sec->size))
2549 gaps = TRUE;
2550 }
2551 return gaps;
2552 }
2553
2554 /* Search current function info for a function that contains address
2555 OFFSET in section SEC. */
2556
2557 static struct function_info *
2558 find_function (asection *sec, bfd_vma offset, struct bfd_link_info *info)
2559 {
2560 struct _spu_elf_section_data *sec_data = spu_elf_section_data (sec);
2561 struct spu_elf_stack_info *sinfo = sec_data->u.i.stack_info;
2562 int lo, hi, mid;
2563
2564 lo = 0;
2565 hi = sinfo->num_fun;
2566 while (lo < hi)
2567 {
2568 mid = (lo + hi) / 2;
2569 if (offset < sinfo->fun[mid].lo)
2570 hi = mid;
2571 else if (offset >= sinfo->fun[mid].hi)
2572 lo = mid + 1;
2573 else
2574 return &sinfo->fun[mid];
2575 }
2576 info->callbacks->einfo (_("%A:0x%v not found in function table\n"),
2577 sec, offset);
2578 bfd_set_error (bfd_error_bad_value);
2579 return NULL;
2580 }
2581
2582 /* Add CALLEE to CALLER call list if not already present. Return TRUE
2583 if CALLEE was new. If this function return FALSE, CALLEE should
2584 be freed. */
2585
2586 static bfd_boolean
2587 insert_callee (struct function_info *caller, struct call_info *callee)
2588 {
2589 struct call_info **pp, *p;
2590
2591 for (pp = &caller->call_list; (p = *pp) != NULL; pp = &p->next)
2592 if (p->fun == callee->fun)
2593 {
2594 /* Tail calls use less stack than normal calls. Retain entry
2595 for normal call over one for tail call. */
2596 p->is_tail &= callee->is_tail;
2597 if (!p->is_tail)
2598 {
2599 p->fun->start = NULL;
2600 p->fun->is_func = TRUE;
2601 }
2602 p->count += callee->count;
2603 /* Reorder list so most recent call is first. */
2604 *pp = p->next;
2605 p->next = caller->call_list;
2606 caller->call_list = p;
2607 return FALSE;
2608 }
2609 callee->next = caller->call_list;
2610 caller->call_list = callee;
2611 return TRUE;
2612 }
2613
2614 /* Copy CALL and insert the copy into CALLER. */
2615
2616 static bfd_boolean
2617 copy_callee (struct function_info *caller, const struct call_info *call)
2618 {
2619 struct call_info *callee;
2620 callee = bfd_malloc (sizeof (*callee));
2621 if (callee == NULL)
2622 return FALSE;
2623 *callee = *call;
2624 if (!insert_callee (caller, callee))
2625 free (callee);
2626 return TRUE;
2627 }
2628
2629 /* We're only interested in code sections. Testing SEC_IN_MEMORY excludes
2630 overlay stub sections. */
2631
2632 static bfd_boolean
2633 interesting_section (asection *s)
2634 {
2635 return (s->output_section != bfd_abs_section_ptr
2636 && ((s->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_IN_MEMORY))
2637 == (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2638 && s->size != 0);
2639 }
2640
2641 /* Rummage through the relocs for SEC, looking for function calls.
2642 If CALL_TREE is true, fill in call graph. If CALL_TREE is false,
2643 mark destination symbols on calls as being functions. Also
2644 look at branches, which may be tail calls or go to hot/cold
2645 section part of same function. */
2646
2647 static bfd_boolean
2648 mark_functions_via_relocs (asection *sec,
2649 struct bfd_link_info *info,
2650 int call_tree)
2651 {
2652 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2653 Elf_Internal_Shdr *symtab_hdr;
2654 void *psyms;
2655 unsigned int priority = 0;
2656 static bfd_boolean warned;
2657
2658 if (!interesting_section (sec)
2659 || sec->reloc_count == 0)
2660 return TRUE;
2661
2662 internal_relocs = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL,
2663 info->keep_memory);
2664 if (internal_relocs == NULL)
2665 return FALSE;
2666
2667 symtab_hdr = &elf_tdata (sec->owner)->symtab_hdr;
2668 psyms = &symtab_hdr->contents;
2669 irela = internal_relocs;
2670 irelaend = irela + sec->reloc_count;
2671 for (; irela < irelaend; irela++)
2672 {
2673 enum elf_spu_reloc_type r_type;
2674 unsigned int r_indx;
2675 asection *sym_sec;
2676 Elf_Internal_Sym *sym;
2677 struct elf_link_hash_entry *h;
2678 bfd_vma val;
2679 bfd_boolean reject, is_call;
2680 struct function_info *caller;
2681 struct call_info *callee;
2682
2683 reject = FALSE;
2684 r_type = ELF32_R_TYPE (irela->r_info);
2685 if (r_type != R_SPU_REL16
2686 && r_type != R_SPU_ADDR16)
2687 {
2688 reject = TRUE;
2689 if (!(call_tree && spu_hash_table (info)->params->auto_overlay))
2690 continue;
2691 }
2692
2693 r_indx = ELF32_R_SYM (irela->r_info);
2694 if (!get_sym_h (&h, &sym, &sym_sec, psyms, r_indx, sec->owner))
2695 return FALSE;
2696
2697 if (sym_sec == NULL
2698 || sym_sec->output_section == bfd_abs_section_ptr)
2699 continue;
2700
2701 is_call = FALSE;
2702 if (!reject)
2703 {
2704 unsigned char insn[4];
2705
2706 if (!bfd_get_section_contents (sec->owner, sec, insn,
2707 irela->r_offset, 4))
2708 return FALSE;
2709 if (is_branch (insn))
2710 {
2711 is_call = (insn[0] & 0xfd) == 0x31;
2712 priority = insn[1] & 0x0f;
2713 priority <<= 8;
2714 priority |= insn[2];
2715 priority <<= 8;
2716 priority |= insn[3];
2717 priority >>= 7;
2718 if ((sym_sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2719 != (SEC_ALLOC | SEC_LOAD | SEC_CODE))
2720 {
2721 if (!warned)
2722 info->callbacks->einfo
2723 (_("%B(%A+0x%v): call to non-code section"
2724 " %B(%A), analysis incomplete\n"),
2725 sec->owner, sec, irela->r_offset,
2726 sym_sec->owner, sym_sec);
2727 warned = TRUE;
2728 continue;
2729 }
2730 }
2731 else
2732 {
2733 reject = TRUE;
2734 if (!(call_tree && spu_hash_table (info)->params->auto_overlay)
2735 || is_hint (insn))
2736 continue;
2737 }
2738 }
2739
2740 if (reject)
2741 {
2742 /* For --auto-overlay, count possible stubs we need for
2743 function pointer references. */
2744 unsigned int sym_type;
2745 if (h)
2746 sym_type = h->type;
2747 else
2748 sym_type = ELF_ST_TYPE (sym->st_info);
2749 if (sym_type == STT_FUNC)
2750 spu_hash_table (info)->non_ovly_stub += 1;
2751 continue;
2752 }
2753
2754 if (h)
2755 val = h->root.u.def.value;
2756 else
2757 val = sym->st_value;
2758 val += irela->r_addend;
2759
2760 if (!call_tree)
2761 {
2762 struct function_info *fun;
2763
2764 if (irela->r_addend != 0)
2765 {
2766 Elf_Internal_Sym *fake = bfd_zmalloc (sizeof (*fake));
2767 if (fake == NULL)
2768 return FALSE;
2769 fake->st_value = val;
2770 fake->st_shndx
2771 = _bfd_elf_section_from_bfd_section (sym_sec->owner, sym_sec);
2772 sym = fake;
2773 }
2774 if (sym)
2775 fun = maybe_insert_function (sym_sec, sym, FALSE, is_call);
2776 else
2777 fun = maybe_insert_function (sym_sec, h, TRUE, is_call);
2778 if (fun == NULL)
2779 return FALSE;
2780 if (irela->r_addend != 0
2781 && fun->u.sym != sym)
2782 free (sym);
2783 continue;
2784 }
2785
2786 caller = find_function (sec, irela->r_offset, info);
2787 if (caller == NULL)
2788 return FALSE;
2789 callee = bfd_malloc (sizeof *callee);
2790 if (callee == NULL)
2791 return FALSE;
2792
2793 callee->fun = find_function (sym_sec, val, info);
2794 if (callee->fun == NULL)
2795 return FALSE;
2796 callee->is_tail = !is_call;
2797 callee->is_pasted = FALSE;
2798 callee->broken_cycle = FALSE;
2799 callee->priority = priority;
2800 callee->count = 1;
2801 if (callee->fun->last_caller != sec)
2802 {
2803 callee->fun->last_caller = sec;
2804 callee->fun->call_count += 1;
2805 }
2806 if (!insert_callee (caller, callee))
2807 free (callee);
2808 else if (!is_call
2809 && !callee->fun->is_func
2810 && callee->fun->stack == 0)
2811 {
2812 /* This is either a tail call or a branch from one part of
2813 the function to another, ie. hot/cold section. If the
2814 destination has been called by some other function then
2815 it is a separate function. We also assume that functions
2816 are not split across input files. */
2817 if (sec->owner != sym_sec->owner)
2818 {
2819 callee->fun->start = NULL;
2820 callee->fun->is_func = TRUE;
2821 }
2822 else if (callee->fun->start == NULL)
2823 {
2824 struct function_info *caller_start = caller;
2825 while (caller_start->start)
2826 caller_start = caller_start->start;
2827
2828 if (caller_start != callee->fun)
2829 callee->fun->start = caller_start;
2830 }
2831 else
2832 {
2833 struct function_info *callee_start;
2834 struct function_info *caller_start;
2835 callee_start = callee->fun;
2836 while (callee_start->start)
2837 callee_start = callee_start->start;
2838 caller_start = caller;
2839 while (caller_start->start)
2840 caller_start = caller_start->start;
2841 if (caller_start != callee_start)
2842 {
2843 callee->fun->start = NULL;
2844 callee->fun->is_func = TRUE;
2845 }
2846 }
2847 }
2848 }
2849
2850 return TRUE;
2851 }
2852
2853 /* Handle something like .init or .fini, which has a piece of a function.
2854 These sections are pasted together to form a single function. */
2855
2856 static bfd_boolean
2857 pasted_function (asection *sec)
2858 {
2859 struct bfd_link_order *l;
2860 struct _spu_elf_section_data *sec_data;
2861 struct spu_elf_stack_info *sinfo;
2862 Elf_Internal_Sym *fake;
2863 struct function_info *fun, *fun_start;
2864
2865 fake = bfd_zmalloc (sizeof (*fake));
2866 if (fake == NULL)
2867 return FALSE;
2868 fake->st_value = 0;
2869 fake->st_size = sec->size;
2870 fake->st_shndx
2871 = _bfd_elf_section_from_bfd_section (sec->owner, sec);
2872 fun = maybe_insert_function (sec, fake, FALSE, FALSE);
2873 if (!fun)
2874 return FALSE;
2875
2876 /* Find a function immediately preceding this section. */
2877 fun_start = NULL;
2878 for (l = sec->output_section->map_head.link_order; l != NULL; l = l->next)
2879 {
2880 if (l->u.indirect.section == sec)
2881 {
2882 if (fun_start != NULL)
2883 {
2884 struct call_info *callee = bfd_malloc (sizeof *callee);
2885 if (callee == NULL)
2886 return FALSE;
2887
2888 fun->start = fun_start;
2889 callee->fun = fun;
2890 callee->is_tail = TRUE;
2891 callee->is_pasted = TRUE;
2892 callee->broken_cycle = FALSE;
2893 callee->priority = 0;
2894 callee->count = 1;
2895 if (!insert_callee (fun_start, callee))
2896 free (callee);
2897 return TRUE;
2898 }
2899 break;
2900 }
2901 if (l->type == bfd_indirect_link_order
2902 && (sec_data = spu_elf_section_data (l->u.indirect.section)) != NULL
2903 && (sinfo = sec_data->u.i.stack_info) != NULL
2904 && sinfo->num_fun != 0)
2905 fun_start = &sinfo->fun[sinfo->num_fun - 1];
2906 }
2907
2908 /* Don't return an error if we did not find a function preceding this
2909 section. The section may have incorrect flags. */
2910 return TRUE;
2911 }
2912
2913 /* Map address ranges in code sections to functions. */
2914
2915 static bfd_boolean
2916 discover_functions (struct bfd_link_info *info)
2917 {
2918 bfd *ibfd;
2919 int bfd_idx;
2920 Elf_Internal_Sym ***psym_arr;
2921 asection ***sec_arr;
2922 bfd_boolean gaps = FALSE;
2923
2924 bfd_idx = 0;
2925 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2926 bfd_idx++;
2927
2928 psym_arr = bfd_zmalloc (bfd_idx * sizeof (*psym_arr));
2929 if (psym_arr == NULL)
2930 return FALSE;
2931 sec_arr = bfd_zmalloc (bfd_idx * sizeof (*sec_arr));
2932 if (sec_arr == NULL)
2933 return FALSE;
2934
2935 for (ibfd = info->input_bfds, bfd_idx = 0;
2936 ibfd != NULL;
2937 ibfd = ibfd->link_next, bfd_idx++)
2938 {
2939 extern const bfd_target bfd_elf32_spu_vec;
2940 Elf_Internal_Shdr *symtab_hdr;
2941 asection *sec;
2942 size_t symcount;
2943 Elf_Internal_Sym *syms, *sy, **psyms, **psy;
2944 asection **psecs, **p;
2945
2946 if (ibfd->xvec != &bfd_elf32_spu_vec)
2947 continue;
2948
2949 /* Read all the symbols. */
2950 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2951 symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
2952 if (symcount == 0)
2953 {
2954 if (!gaps)
2955 for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
2956 if (interesting_section (sec))
2957 {
2958 gaps = TRUE;
2959 break;
2960 }
2961 continue;
2962 }
2963
2964 if (symtab_hdr->contents != NULL)
2965 {
2966 /* Don't use cached symbols since the generic ELF linker
2967 code only reads local symbols, and we need globals too. */
2968 free (symtab_hdr->contents);
2969 symtab_hdr->contents = NULL;
2970 }
2971 syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, 0,
2972 NULL, NULL, NULL);
2973 symtab_hdr->contents = (void *) syms;
2974 if (syms == NULL)
2975 return FALSE;
2976
2977 /* Select defined function symbols that are going to be output. */
2978 psyms = bfd_malloc ((symcount + 1) * sizeof (*psyms));
2979 if (psyms == NULL)
2980 return FALSE;
2981 psym_arr[bfd_idx] = psyms;
2982 psecs = bfd_malloc (symcount * sizeof (*psecs));
2983 if (psecs == NULL)
2984 return FALSE;
2985 sec_arr[bfd_idx] = psecs;
2986 for (psy = psyms, p = psecs, sy = syms; sy < syms + symcount; ++p, ++sy)
2987 if (ELF_ST_TYPE (sy->st_info) == STT_NOTYPE
2988 || ELF_ST_TYPE (sy->st_info) == STT_FUNC)
2989 {
2990 asection *s;
2991
2992 *p = s = bfd_section_from_elf_index (ibfd, sy->st_shndx);
2993 if (s != NULL && interesting_section (s))
2994 *psy++ = sy;
2995 }
2996 symcount = psy - psyms;
2997 *psy = NULL;
2998
2999 /* Sort them by section and offset within section. */
3000 sort_syms_syms = syms;
3001 sort_syms_psecs = psecs;
3002 qsort (psyms, symcount, sizeof (*psyms), sort_syms);
3003
3004 /* Now inspect the function symbols. */
3005 for (psy = psyms; psy < psyms + symcount; )
3006 {
3007 asection *s = psecs[*psy - syms];
3008 Elf_Internal_Sym **psy2;
3009
3010 for (psy2 = psy; ++psy2 < psyms + symcount; )
3011 if (psecs[*psy2 - syms] != s)
3012 break;
3013
3014 if (!alloc_stack_info (s, psy2 - psy))
3015 return FALSE;
3016 psy = psy2;
3017 }
3018
3019 /* First install info about properly typed and sized functions.
3020 In an ideal world this will cover all code sections, except
3021 when partitioning functions into hot and cold sections,
3022 and the horrible pasted together .init and .fini functions. */
3023 for (psy = psyms; psy < psyms + symcount; ++psy)
3024 {
3025 sy = *psy;
3026 if (ELF_ST_TYPE (sy->st_info) == STT_FUNC)
3027 {
3028 asection *s = psecs[sy - syms];
3029 if (!maybe_insert_function (s, sy, FALSE, TRUE))
3030 return FALSE;
3031 }
3032 }
3033
3034 for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
3035 if (interesting_section (sec))
3036 gaps |= check_function_ranges (sec, info);
3037 }
3038
3039 if (gaps)
3040 {
3041 /* See if we can discover more function symbols by looking at
3042 relocations. */
3043 for (ibfd = info->input_bfds, bfd_idx = 0;
3044 ibfd != NULL;
3045 ibfd = ibfd->link_next, bfd_idx++)
3046 {
3047 asection *sec;
3048
3049 if (psym_arr[bfd_idx] == NULL)
3050 continue;
3051
3052 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3053 if (!mark_functions_via_relocs (sec, info, FALSE))
3054 return FALSE;
3055 }
3056
3057 for (ibfd = info->input_bfds, bfd_idx = 0;
3058 ibfd != NULL;
3059 ibfd = ibfd->link_next, bfd_idx++)
3060 {
3061 Elf_Internal_Shdr *symtab_hdr;
3062 asection *sec;
3063 Elf_Internal_Sym *syms, *sy, **psyms, **psy;
3064 asection **psecs;
3065
3066 if ((psyms = psym_arr[bfd_idx]) == NULL)
3067 continue;
3068
3069 psecs = sec_arr[bfd_idx];
3070
3071 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3072 syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3073
3074 gaps = FALSE;
3075 for (sec = ibfd->sections; sec != NULL && !gaps; sec = sec->next)
3076 if (interesting_section (sec))
3077 gaps |= check_function_ranges (sec, info);
3078 if (!gaps)
3079 continue;
3080
3081 /* Finally, install all globals. */
3082 for (psy = psyms; (sy = *psy) != NULL; ++psy)
3083 {
3084 asection *s;
3085
3086 s = psecs[sy - syms];
3087
3088 /* Global syms might be improperly typed functions. */
3089 if (ELF_ST_TYPE (sy->st_info) != STT_FUNC
3090 && ELF_ST_BIND (sy->st_info) == STB_GLOBAL)
3091 {
3092 if (!maybe_insert_function (s, sy, FALSE, FALSE))
3093 return FALSE;
3094 }
3095 }
3096 }
3097
3098 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3099 {
3100 extern const bfd_target bfd_elf32_spu_vec;
3101 asection *sec;
3102
3103 if (ibfd->xvec != &bfd_elf32_spu_vec)
3104 continue;
3105
3106 /* Some of the symbols we've installed as marking the
3107 beginning of functions may have a size of zero. Extend
3108 the range of such functions to the beginning of the
3109 next symbol of interest. */
3110 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3111 if (interesting_section (sec))
3112 {
3113 struct _spu_elf_section_data *sec_data;
3114 struct spu_elf_stack_info *sinfo;
3115
3116 sec_data = spu_elf_section_data (sec);
3117 sinfo = sec_data->u.i.stack_info;