1 /* SPU specific support for 32-bit ELF
3 Copyright 2006, 2007 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. */
27 #include "elf32-spu.h"
29 /* We use RELA style relocs. Don't define USE_REL. */
31 static bfd_reloc_status_type
spu_elf_rel9 (bfd
*, arelent
*, asymbol
*,
35 /* Values of type 'enum elf_spu_reloc_type' are used to index this
36 array, so it must be declared in the order of that type. */
38 static reloc_howto_type elf_howto_table
[] = {
39 HOWTO (R_SPU_NONE
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
40 bfd_elf_generic_reloc
, "SPU_NONE",
41 FALSE
, 0, 0x00000000, FALSE
),
42 HOWTO (R_SPU_ADDR10
, 4, 2, 10, FALSE
, 14, complain_overflow_bitfield
,
43 bfd_elf_generic_reloc
, "SPU_ADDR10",
44 FALSE
, 0, 0x00ffc000, FALSE
),
45 HOWTO (R_SPU_ADDR16
, 2, 2, 16, FALSE
, 7, complain_overflow_bitfield
,
46 bfd_elf_generic_reloc
, "SPU_ADDR16",
47 FALSE
, 0, 0x007fff80, FALSE
),
48 HOWTO (R_SPU_ADDR16_HI
, 16, 2, 16, FALSE
, 7, complain_overflow_bitfield
,
49 bfd_elf_generic_reloc
, "SPU_ADDR16_HI",
50 FALSE
, 0, 0x007fff80, FALSE
),
51 HOWTO (R_SPU_ADDR16_LO
, 0, 2, 16, FALSE
, 7, complain_overflow_dont
,
52 bfd_elf_generic_reloc
, "SPU_ADDR16_LO",
53 FALSE
, 0, 0x007fff80, FALSE
),
54 HOWTO (R_SPU_ADDR18
, 0, 2, 18, FALSE
, 7, complain_overflow_bitfield
,
55 bfd_elf_generic_reloc
, "SPU_ADDR18",
56 FALSE
, 0, 0x01ffff80, FALSE
),
57 HOWTO (R_SPU_ADDR32
, 0, 2, 32, FALSE
, 0, complain_overflow_dont
,
58 bfd_elf_generic_reloc
, "SPU_ADDR32",
59 FALSE
, 0, 0xffffffff, FALSE
),
60 HOWTO (R_SPU_REL16
, 2, 2, 16, TRUE
, 7, complain_overflow_bitfield
,
61 bfd_elf_generic_reloc
, "SPU_REL16",
62 FALSE
, 0, 0x007fff80, TRUE
),
63 HOWTO (R_SPU_ADDR7
, 0, 2, 7, FALSE
, 14, complain_overflow_dont
,
64 bfd_elf_generic_reloc
, "SPU_ADDR7",
65 FALSE
, 0, 0x001fc000, FALSE
),
66 HOWTO (R_SPU_REL9
, 2, 2, 9, TRUE
, 0, complain_overflow_signed
,
67 spu_elf_rel9
, "SPU_REL9",
68 FALSE
, 0, 0x0180007f, TRUE
),
69 HOWTO (R_SPU_REL9I
, 2, 2, 9, TRUE
, 0, complain_overflow_signed
,
70 spu_elf_rel9
, "SPU_REL9I",
71 FALSE
, 0, 0x0000c07f, TRUE
),
72 HOWTO (R_SPU_ADDR10I
, 0, 2, 10, FALSE
, 14, complain_overflow_signed
,
73 bfd_elf_generic_reloc
, "SPU_ADDR10I",
74 FALSE
, 0, 0x00ffc000, FALSE
),
75 HOWTO (R_SPU_ADDR16I
, 0, 2, 16, FALSE
, 7, complain_overflow_signed
,
76 bfd_elf_generic_reloc
, "SPU_ADDR16I",
77 FALSE
, 0, 0x007fff80, FALSE
),
78 HOWTO (R_SPU_REL32
, 0, 2, 32, TRUE
, 0, complain_overflow_dont
,
79 bfd_elf_generic_reloc
, "SPU_REL32",
80 FALSE
, 0, 0xffffffff, TRUE
),
81 HOWTO (R_SPU_ADDR16X
, 0, 2, 16, FALSE
, 7, complain_overflow_bitfield
,
82 bfd_elf_generic_reloc
, "SPU_ADDR16X",
83 FALSE
, 0, 0x007fff80, FALSE
),
84 HOWTO (R_SPU_PPU32
, 0, 2, 32, FALSE
, 0, complain_overflow_dont
,
85 bfd_elf_generic_reloc
, "SPU_PPU32",
86 FALSE
, 0, 0xffffffff, FALSE
),
87 HOWTO (R_SPU_PPU64
, 0, 4, 64, FALSE
, 0, complain_overflow_dont
,
88 bfd_elf_generic_reloc
, "SPU_PPU64",
92 static struct bfd_elf_special_section
const spu_elf_special_sections
[] = {
93 { ".toe", 4, 0, SHT_NOBITS
, SHF_ALLOC
},
97 static enum elf_spu_reloc_type
98 spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code
)
104 case BFD_RELOC_SPU_IMM10W
:
106 case BFD_RELOC_SPU_IMM16W
:
108 case BFD_RELOC_SPU_LO16
:
109 return R_SPU_ADDR16_LO
;
110 case BFD_RELOC_SPU_HI16
:
111 return R_SPU_ADDR16_HI
;
112 case BFD_RELOC_SPU_IMM18
:
114 case BFD_RELOC_SPU_PCREL16
:
116 case BFD_RELOC_SPU_IMM7
:
118 case BFD_RELOC_SPU_IMM8
:
120 case BFD_RELOC_SPU_PCREL9a
:
122 case BFD_RELOC_SPU_PCREL9b
:
124 case BFD_RELOC_SPU_IMM10
:
125 return R_SPU_ADDR10I
;
126 case BFD_RELOC_SPU_IMM16
:
127 return R_SPU_ADDR16I
;
130 case BFD_RELOC_32_PCREL
:
132 case BFD_RELOC_SPU_PPU32
:
134 case BFD_RELOC_SPU_PPU64
:
140 spu_elf_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
142 Elf_Internal_Rela
*dst
)
144 enum elf_spu_reloc_type r_type
;
146 r_type
= (enum elf_spu_reloc_type
) ELF32_R_TYPE (dst
->r_info
);
147 BFD_ASSERT (r_type
< R_SPU_max
);
148 cache_ptr
->howto
= &elf_howto_table
[(int) r_type
];
151 static reloc_howto_type
*
152 spu_elf_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
153 bfd_reloc_code_real_type code
)
155 enum elf_spu_reloc_type r_type
= spu_elf_bfd_to_reloc_type (code
);
157 if (r_type
== R_SPU_NONE
)
160 return elf_howto_table
+ r_type
;
163 static reloc_howto_type
*
164 spu_elf_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
169 for (i
= 0; i
< sizeof (elf_howto_table
) / sizeof (elf_howto_table
[0]); i
++)
170 if (elf_howto_table
[i
].name
!= NULL
171 && strcasecmp (elf_howto_table
[i
].name
, r_name
) == 0)
172 return &elf_howto_table
[i
];
177 /* Apply R_SPU_REL9 and R_SPU_REL9I relocs. */
179 static bfd_reloc_status_type
180 spu_elf_rel9 (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
181 void *data
, asection
*input_section
,
182 bfd
*output_bfd
, char **error_message
)
184 bfd_size_type octets
;
188 /* If this is a relocatable link (output_bfd test tells us), just
189 call the generic function. Any adjustment will be done at final
191 if (output_bfd
!= NULL
)
192 return bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
193 input_section
, output_bfd
, error_message
);
195 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
196 return bfd_reloc_outofrange
;
197 octets
= reloc_entry
->address
* bfd_octets_per_byte (abfd
);
199 /* Get symbol value. */
201 if (!bfd_is_com_section (symbol
->section
))
203 if (symbol
->section
->output_section
)
204 val
+= symbol
->section
->output_section
->vma
;
206 val
+= reloc_entry
->addend
;
208 /* Make it pc-relative. */
209 val
-= input_section
->output_section
->vma
+ input_section
->output_offset
;
212 if (val
+ 256 >= 512)
213 return bfd_reloc_overflow
;
215 insn
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ octets
);
217 /* Move two high bits of value to REL9I and REL9 position.
218 The mask will take care of selecting the right field. */
219 val
= (val
& 0x7f) | ((val
& 0x180) << 7) | ((val
& 0x180) << 16);
220 insn
&= ~reloc_entry
->howto
->dst_mask
;
221 insn
|= val
& reloc_entry
->howto
->dst_mask
;
222 bfd_put_32 (abfd
, insn
, (bfd_byte
*) data
+ octets
);
227 spu_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
229 if (!sec
->used_by_bfd
)
231 struct _spu_elf_section_data
*sdata
;
233 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
236 sec
->used_by_bfd
= sdata
;
239 return _bfd_elf_new_section_hook (abfd
, sec
);
242 /* Specially mark defined symbols named _EAR_* with BSF_KEEP so that
243 strip --strip-unneeded will not remove them. */
246 spu_elf_backend_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
, asymbol
*sym
)
248 if (sym
->name
!= NULL
249 && sym
->section
!= bfd_abs_section_ptr
250 && strncmp (sym
->name
, "_EAR_", 5) == 0)
251 sym
->flags
|= BSF_KEEP
;
254 /* SPU ELF linker hash table. */
256 struct spu_link_hash_table
258 struct elf_link_hash_table elf
;
260 /* The stub hash table. */
261 struct bfd_hash_table stub_hash_table
;
263 /* Shortcuts to overlay sections. */
267 struct elf_link_hash_entry
*ovly_load
;
269 /* An array of two output sections per overlay region, chosen such that
270 the first section vma is the overlay buffer vma (ie. the section has
271 the lowest vma in the group that occupy the region), and the second
272 section vma+size specifies the end of the region. We keep pointers
273 to sections like this because section vmas may change when laying
275 asection
**ovl_region
;
277 /* Number of overlay buffers. */
278 unsigned int num_buf
;
280 /* Total number of overlays. */
281 unsigned int num_overlays
;
283 /* Set if we should emit symbols for stubs. */
284 unsigned int emit_stub_syms
:1;
286 /* Set if we want stubs on calls out of overlay regions to
287 non-overlay regions. */
288 unsigned int non_overlay_stubs
: 1;
291 unsigned int stub_overflow
: 1;
293 /* Set if stack size analysis should be done. */
294 unsigned int stack_analysis
: 1;
296 /* Set if __stack_* syms will be emitted. */
297 unsigned int emit_stack_syms
: 1;
300 #define spu_hash_table(p) \
301 ((struct spu_link_hash_table *) ((p)->hash))
303 struct spu_stub_hash_entry
305 struct bfd_hash_entry root
;
307 /* Destination of this stub. */
308 asection
*target_section
;
311 /* Offset of entry in stub section. */
314 /* Offset from this stub to stub that loads the overlay index. */
318 /* Create an entry in a spu stub hash table. */
320 static struct bfd_hash_entry
*
321 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
322 struct bfd_hash_table
*table
,
325 /* Allocate the structure if it has not already been allocated by a
329 entry
= bfd_hash_allocate (table
, sizeof (struct spu_stub_hash_entry
));
334 /* Call the allocation method of the superclass. */
335 entry
= bfd_hash_newfunc (entry
, table
, string
);
338 struct spu_stub_hash_entry
*sh
= (struct spu_stub_hash_entry
*) entry
;
340 sh
->target_section
= NULL
;
349 /* Create a spu ELF linker hash table. */
351 static struct bfd_link_hash_table
*
352 spu_elf_link_hash_table_create (bfd
*abfd
)
354 struct spu_link_hash_table
*htab
;
356 htab
= bfd_malloc (sizeof (*htab
));
360 if (!_bfd_elf_link_hash_table_init (&htab
->elf
, abfd
,
361 _bfd_elf_link_hash_newfunc
,
362 sizeof (struct elf_link_hash_entry
)))
368 /* Init the stub hash table too. */
369 if (!bfd_hash_table_init (&htab
->stub_hash_table
, stub_hash_newfunc
,
370 sizeof (struct spu_stub_hash_entry
)))
373 memset (&htab
->stub
, 0,
374 sizeof (*htab
) - offsetof (struct spu_link_hash_table
, stub
));
376 return &htab
->elf
.root
;
379 /* Free the derived linker hash table. */
382 spu_elf_link_hash_table_free (struct bfd_link_hash_table
*hash
)
384 struct spu_link_hash_table
*ret
= (struct spu_link_hash_table
*) hash
;
386 bfd_hash_table_free (&ret
->stub_hash_table
);
387 _bfd_generic_link_hash_table_free (hash
);
390 /* Find the symbol for the given R_SYMNDX in IBFD and set *HP and *SYMP
391 to (hash, NULL) for global symbols, and (NULL, sym) for locals. Set
392 *SYMSECP to the symbol's section. *LOCSYMSP caches local syms. */
395 get_sym_h (struct elf_link_hash_entry
**hp
,
396 Elf_Internal_Sym
**symp
,
398 Elf_Internal_Sym
**locsymsp
,
399 unsigned long r_symndx
,
402 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
404 if (r_symndx
>= symtab_hdr
->sh_info
)
406 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (ibfd
);
407 struct elf_link_hash_entry
*h
;
409 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
410 while (h
->root
.type
== bfd_link_hash_indirect
411 || h
->root
.type
== bfd_link_hash_warning
)
412 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
422 asection
*symsec
= NULL
;
423 if (h
->root
.type
== bfd_link_hash_defined
424 || h
->root
.type
== bfd_link_hash_defweak
)
425 symsec
= h
->root
.u
.def
.section
;
431 Elf_Internal_Sym
*sym
;
432 Elf_Internal_Sym
*locsyms
= *locsymsp
;
436 locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
439 size_t symcount
= symtab_hdr
->sh_info
;
441 /* If we are reading symbols into the contents, then
442 read the global syms too. This is done to cache
443 syms for later stack analysis. */
444 if ((unsigned char **) locsymsp
== &symtab_hdr
->contents
)
445 symcount
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
446 locsyms
= bfd_elf_get_elf_syms (ibfd
, symtab_hdr
, symcount
, 0,
453 sym
= locsyms
+ r_symndx
;
463 asection
*symsec
= NULL
;
464 if ((sym
->st_shndx
!= SHN_UNDEF
465 && sym
->st_shndx
< SHN_LORESERVE
)
466 || sym
->st_shndx
> SHN_HIRESERVE
)
467 symsec
= bfd_section_from_elf_index (ibfd
, sym
->st_shndx
);
475 /* Build a name for an entry in the stub hash table. We can't use a
476 local symbol name because ld -r might generate duplicate local symbols. */
479 spu_stub_name (const asection
*sym_sec
,
480 const struct elf_link_hash_entry
*h
,
481 const Elf_Internal_Rela
*rel
)
488 len
= strlen (h
->root
.root
.string
) + 1 + 8 + 1;
489 stub_name
= bfd_malloc (len
);
490 if (stub_name
== NULL
)
493 sprintf (stub_name
, "%s+%x",
495 (int) rel
->r_addend
& 0xffffffff);
500 len
= 8 + 1 + 8 + 1 + 8 + 1;
501 stub_name
= bfd_malloc (len
);
502 if (stub_name
== NULL
)
505 sprintf (stub_name
, "%x:%x+%x",
506 sym_sec
->id
& 0xffffffff,
507 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
508 (int) rel
->r_addend
& 0xffffffff);
509 len
= strlen (stub_name
);
512 if (stub_name
[len
- 2] == '+'
513 && stub_name
[len
- 1] == '0'
514 && stub_name
[len
] == 0)
515 stub_name
[len
- 2] = 0;
520 /* Create the note section if not already present. This is done early so
521 that the linker maps the sections to the right place in the output. */
524 spu_elf_create_sections (bfd
*output_bfd
,
525 struct bfd_link_info
*info
,
530 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
532 /* Stash some options away where we can get at them later. */
533 htab
->stack_analysis
= stack_analysis
;
534 htab
->emit_stack_syms
= emit_stack_syms
;
536 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
537 if (bfd_get_section_by_name (ibfd
, SPU_PTNOTE_SPUNAME
) != NULL
)
542 /* Make SPU_PTNOTE_SPUNAME section. */
549 ibfd
= info
->input_bfds
;
550 flags
= SEC_LOAD
| SEC_READONLY
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
551 s
= bfd_make_section_anyway_with_flags (ibfd
, SPU_PTNOTE_SPUNAME
, flags
);
553 || !bfd_set_section_alignment (ibfd
, s
, 4))
556 name_len
= strlen (bfd_get_filename (output_bfd
)) + 1;
557 size
= 12 + ((sizeof (SPU_PLUGIN_NAME
) + 3) & -4);
558 size
+= (name_len
+ 3) & -4;
560 if (!bfd_set_section_size (ibfd
, s
, size
))
563 data
= bfd_zalloc (ibfd
, size
);
567 bfd_put_32 (ibfd
, sizeof (SPU_PLUGIN_NAME
), data
+ 0);
568 bfd_put_32 (ibfd
, name_len
, data
+ 4);
569 bfd_put_32 (ibfd
, 1, data
+ 8);
570 memcpy (data
+ 12, SPU_PLUGIN_NAME
, sizeof (SPU_PLUGIN_NAME
));
571 memcpy (data
+ 12 + ((sizeof (SPU_PLUGIN_NAME
) + 3) & -4),
572 bfd_get_filename (output_bfd
), name_len
);
579 /* qsort predicate to sort sections by vma. */
582 sort_sections (const void *a
, const void *b
)
584 const asection
*const *s1
= a
;
585 const asection
*const *s2
= b
;
586 bfd_signed_vma delta
= (*s1
)->vma
- (*s2
)->vma
;
589 return delta
< 0 ? -1 : 1;
591 return (*s1
)->index
- (*s2
)->index
;
594 /* Identify overlays in the output bfd, and number them. */
597 spu_elf_find_overlays (bfd
*output_bfd
, struct bfd_link_info
*info
)
599 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
600 asection
**alloc_sec
;
601 unsigned int i
, n
, ovl_index
, num_buf
;
605 if (output_bfd
->section_count
< 2)
608 alloc_sec
= bfd_malloc (output_bfd
->section_count
* sizeof (*alloc_sec
));
609 if (alloc_sec
== NULL
)
612 /* Pick out all the alloced sections. */
613 for (n
= 0, s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
614 if ((s
->flags
& SEC_ALLOC
) != 0
615 && (s
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != SEC_THREAD_LOCAL
625 /* Sort them by vma. */
626 qsort (alloc_sec
, n
, sizeof (*alloc_sec
), sort_sections
);
628 /* Look for overlapping vmas. Any with overlap must be overlays.
629 Count them. Also count the number of overlay regions and for
630 each region save a section from that region with the lowest vma
631 and another section with the highest end vma. */
632 ovl_end
= alloc_sec
[0]->vma
+ alloc_sec
[0]->size
;
633 for (ovl_index
= 0, num_buf
= 0, i
= 1; i
< n
; i
++)
636 if (s
->vma
< ovl_end
)
638 asection
*s0
= alloc_sec
[i
- 1];
640 if (spu_elf_section_data (s0
)->ovl_index
== 0)
642 spu_elf_section_data (s0
)->ovl_index
= ++ovl_index
;
643 alloc_sec
[num_buf
* 2] = s0
;
644 alloc_sec
[num_buf
* 2 + 1] = s0
;
647 spu_elf_section_data (s
)->ovl_index
= ++ovl_index
;
648 if (ovl_end
< s
->vma
+ s
->size
)
650 ovl_end
= s
->vma
+ s
->size
;
651 alloc_sec
[num_buf
* 2 - 1] = s
;
655 ovl_end
= s
->vma
+ s
->size
;
658 htab
->num_overlays
= ovl_index
;
659 htab
->num_buf
= num_buf
;
666 alloc_sec
= bfd_realloc (alloc_sec
, num_buf
* 2 * sizeof (*alloc_sec
));
667 if (alloc_sec
== NULL
)
670 htab
->ovl_region
= alloc_sec
;
674 /* One of these per stub. */
675 #define SIZEOF_STUB1 8
676 #define ILA_79 0x4200004f /* ila $79,function_address */
677 #define BR 0x32000000 /* br stub2 */
679 /* One of these per overlay. */
680 #define SIZEOF_STUB2 8
681 #define ILA_78 0x4200004e /* ila $78,overlay_number */
683 #define NOP 0x40200000
685 /* Return true for all relative and absolute branch instructions.
693 brhnz 00100011 0.. */
696 is_branch (const unsigned char *insn
)
698 return (insn
[0] & 0xec) == 0x20 && (insn
[1] & 0x80) == 0;
701 /* Return true for all indirect branch instructions.
709 bihnz 00100101 011 */
712 is_indirect_branch (const unsigned char *insn
)
714 return (insn
[0] & 0xef) == 0x25 && (insn
[1] & 0x80) == 0;
717 /* Return true for branch hint instructions.
722 is_hint (const unsigned char *insn
)
724 return (insn
[0] & 0xfc) == 0x10;
727 /* Return TRUE if this reloc symbol should possibly go via an overlay stub. */
730 needs_ovl_stub (const char *sym_name
,
732 asection
*input_section
,
733 struct spu_link_hash_table
*htab
,
734 bfd_boolean is_branch
)
736 if (htab
->num_overlays
== 0)
740 || sym_sec
->output_section
== NULL
741 || spu_elf_section_data (sym_sec
->output_section
) == NULL
)
744 /* setjmp always goes via an overlay stub, because then the return
745 and hence the longjmp goes via __ovly_return. That magically
746 makes setjmp/longjmp between overlays work. */
747 if (strncmp (sym_name
, "setjmp", 6) == 0
748 && (sym_name
[6] == '\0' || sym_name
[6] == '@'))
751 /* Usually, symbols in non-overlay sections don't need stubs. */
752 if (spu_elf_section_data (sym_sec
->output_section
)->ovl_index
== 0
753 && !htab
->non_overlay_stubs
)
756 /* A reference from some other section to a symbol in an overlay
757 section needs a stub. */
758 if (spu_elf_section_data (sym_sec
->output_section
)->ovl_index
759 != spu_elf_section_data (input_section
->output_section
)->ovl_index
)
762 /* If this insn isn't a branch then we are possibly taking the
763 address of a function and passing it out somehow. */
768 struct bfd_hash_table
*stub_hash_table
;
769 struct spu_stub_hash_entry
**sh
;
774 /* Called via elf_link_hash_traverse to allocate stubs for any _SPUEAR_
778 allocate_spuear_stubs (struct elf_link_hash_entry
*h
, void *inf
)
780 /* Symbols starting with _SPUEAR_ need a stub because they may be
781 invoked by the PPU. */
782 if ((h
->root
.type
== bfd_link_hash_defined
783 || h
->root
.type
== bfd_link_hash_defweak
)
785 && strncmp (h
->root
.root
.string
, "_SPUEAR_", 8) == 0)
787 struct stubarr
*stubs
= inf
;
788 static Elf_Internal_Rela zero_rel
;
789 char *stub_name
= spu_stub_name (h
->root
.u
.def
.section
, h
, &zero_rel
);
790 struct spu_stub_hash_entry
*sh
;
792 if (stub_name
== NULL
)
798 sh
= (struct spu_stub_hash_entry
*)
799 bfd_hash_lookup (stubs
->stub_hash_table
, stub_name
, TRUE
, FALSE
);
806 /* If this entry isn't new, we already have a stub. */
807 if (sh
->target_section
!= NULL
)
813 sh
->target_section
= h
->root
.u
.def
.section
;
814 sh
->target_off
= h
->root
.u
.def
.value
;
821 /* Called via bfd_hash_traverse to set up pointers to all symbols
822 in the stub hash table. */
825 populate_stubs (struct bfd_hash_entry
*bh
, void *inf
)
827 struct stubarr
*stubs
= inf
;
829 stubs
->sh
[--stubs
->count
] = (struct spu_stub_hash_entry
*) bh
;
833 /* qsort predicate to sort stubs by overlay number. */
836 sort_stubs (const void *a
, const void *b
)
838 const struct spu_stub_hash_entry
*const *sa
= a
;
839 const struct spu_stub_hash_entry
*const *sb
= b
;
843 i
= spu_elf_section_data ((*sa
)->target_section
->output_section
)->ovl_index
;
844 i
-= spu_elf_section_data ((*sb
)->target_section
->output_section
)->ovl_index
;
848 d
= ((*sa
)->target_section
->output_section
->vma
849 + (*sa
)->target_section
->output_offset
851 - (*sb
)->target_section
->output_section
->vma
852 - (*sb
)->target_section
->output_offset
853 - (*sb
)->target_off
);
855 return d
< 0 ? -1 : 1;
857 /* Two functions at the same address. Aliases perhaps. */
858 i
= strcmp ((*sb
)->root
.string
, (*sa
)->root
.string
);
863 /* Allocate space for overlay call and return stubs. */
866 spu_elf_size_stubs (bfd
*output_bfd
,
867 struct bfd_link_info
*info
,
868 int non_overlay_stubs
,
874 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
876 struct stubarr stubs
;
880 htab
->non_overlay_stubs
= non_overlay_stubs
;
881 stubs
.stub_hash_table
= &htab
->stub_hash_table
;
884 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
886 extern const bfd_target bfd_elf32_spu_vec
;
887 Elf_Internal_Shdr
*symtab_hdr
;
889 Elf_Internal_Sym
*local_syms
= NULL
;
892 if (ibfd
->xvec
!= &bfd_elf32_spu_vec
)
895 /* We'll need the symbol table in a second. */
896 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
897 if (symtab_hdr
->sh_info
== 0)
900 /* Arrange to read and keep global syms for later stack analysis. */
903 psyms
= &symtab_hdr
->contents
;
905 /* Walk over each section attached to the input bfd. */
906 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
908 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
910 /* If there aren't any relocs, then there's nothing more to do. */
911 if ((section
->flags
& SEC_RELOC
) == 0
912 || (section
->flags
& SEC_ALLOC
) == 0
913 || (section
->flags
& SEC_LOAD
) == 0
914 || section
->reloc_count
== 0)
917 /* If this section is a link-once section that will be
918 discarded, then don't create any stubs. */
919 if (section
->output_section
== NULL
920 || section
->output_section
->owner
!= output_bfd
)
923 /* Get the relocs. */
925 = _bfd_elf_link_read_relocs (ibfd
, section
, NULL
, NULL
,
927 if (internal_relocs
== NULL
)
928 goto error_ret_free_local
;
930 /* Now examine each relocation. */
931 irela
= internal_relocs
;
932 irelaend
= irela
+ section
->reloc_count
;
933 for (; irela
< irelaend
; irela
++)
935 enum elf_spu_reloc_type r_type
;
938 Elf_Internal_Sym
*sym
;
939 struct elf_link_hash_entry
*h
;
940 const char *sym_name
;
942 struct spu_stub_hash_entry
*sh
;
943 unsigned int sym_type
;
944 enum _insn_type
{ non_branch
, branch
, call
} insn_type
;
946 r_type
= ELF32_R_TYPE (irela
->r_info
);
947 r_indx
= ELF32_R_SYM (irela
->r_info
);
949 if (r_type
>= R_SPU_max
)
951 bfd_set_error (bfd_error_bad_value
);
952 goto error_ret_free_internal
;
955 /* Determine the reloc target section. */
956 if (!get_sym_h (&h
, &sym
, &sym_sec
, psyms
, r_indx
, ibfd
))
957 goto error_ret_free_internal
;
960 || sym_sec
->output_section
== NULL
961 || sym_sec
->output_section
->owner
!= output_bfd
)
964 /* Ensure no stubs for user supplied overlay manager syms. */
966 && (strcmp (h
->root
.root
.string
, "__ovly_load") == 0
967 || strcmp (h
->root
.root
.string
, "__ovly_return") == 0))
970 insn_type
= non_branch
;
971 if (r_type
== R_SPU_REL16
972 || r_type
== R_SPU_ADDR16
)
974 unsigned char insn
[4];
976 if (!bfd_get_section_contents (ibfd
, section
, insn
,
978 goto error_ret_free_internal
;
980 if (is_branch (insn
) || is_hint (insn
))
983 if ((insn
[0] & 0xfd) == 0x31)
988 /* We are only interested in function symbols. */
992 sym_name
= h
->root
.root
.string
;
996 sym_type
= ELF_ST_TYPE (sym
->st_info
);
997 sym_name
= bfd_elf_sym_name (sym_sec
->owner
,
1002 if (sym_type
!= STT_FUNC
)
1004 /* It's common for people to write assembly and forget
1005 to give function symbols the right type. Handle
1006 calls to such symbols, but warn so that (hopefully)
1007 people will fix their code. We need the symbol
1008 type to be correct to distinguish function pointer
1009 initialisation from other pointer initialisation. */
1010 if (insn_type
== call
)
1011 (*_bfd_error_handler
) (_("warning: call to non-function"
1012 " symbol %s defined in %B"),
1013 sym_sec
->owner
, sym_name
);
1018 if (!needs_ovl_stub (sym_name
, sym_sec
, section
, htab
,
1019 insn_type
!= non_branch
))
1022 stub_name
= spu_stub_name (sym_sec
, h
, irela
);
1023 if (stub_name
== NULL
)
1024 goto error_ret_free_internal
;
1026 sh
= (struct spu_stub_hash_entry
*)
1027 bfd_hash_lookup (&htab
->stub_hash_table
, stub_name
,
1032 error_ret_free_internal
:
1033 if (elf_section_data (section
)->relocs
!= internal_relocs
)
1034 free (internal_relocs
);
1035 error_ret_free_local
:
1036 if (local_syms
!= NULL
1037 && (symtab_hdr
->contents
1038 != (unsigned char *) local_syms
))
1043 /* If this entry isn't new, we already have a stub. */
1044 if (sh
->target_section
!= NULL
)
1050 sh
->target_section
= sym_sec
;
1052 sh
->target_off
= h
->root
.u
.def
.value
;
1054 sh
->target_off
= sym
->st_value
;
1055 sh
->target_off
+= irela
->r_addend
;
1060 /* We're done with the internal relocs, free them. */
1061 if (elf_section_data (section
)->relocs
!= internal_relocs
)
1062 free (internal_relocs
);
1065 if (local_syms
!= NULL
1066 && symtab_hdr
->contents
!= (unsigned char *) local_syms
)
1068 if (!info
->keep_memory
)
1071 symtab_hdr
->contents
= (unsigned char *) local_syms
;
1075 elf_link_hash_traverse (&htab
->elf
, allocate_spuear_stubs
, &stubs
);
1080 if (stubs
.count
== 0)
1083 ibfd
= info
->input_bfds
;
1084 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_READONLY
1085 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
);
1086 htab
->stub
= bfd_make_section_anyway_with_flags (ibfd
, ".stub", flags
);
1088 if (htab
->stub
== NULL
1089 || !bfd_set_section_alignment (ibfd
, htab
->stub
, 2))
1092 flags
= (SEC_ALLOC
| SEC_LOAD
1093 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
);
1094 htab
->ovtab
= bfd_make_section_anyway_with_flags (ibfd
, ".ovtab", flags
);
1095 *ovtab
= htab
->ovtab
;
1096 if (htab
->ovtab
== NULL
1097 || !bfd_set_section_alignment (ibfd
, htab
->stub
, 4))
1100 *toe
= bfd_make_section_anyway_with_flags (ibfd
, ".toe", SEC_ALLOC
);
1102 || !bfd_set_section_alignment (ibfd
, *toe
, 4))
1106 /* Retrieve all the stubs and sort. */
1107 stubs
.sh
= bfd_malloc (stubs
.count
* sizeof (*stubs
.sh
));
1108 if (stubs
.sh
== NULL
)
1111 bfd_hash_traverse (&htab
->stub_hash_table
, populate_stubs
, &stubs
);
1112 BFD_ASSERT (stubs
.count
== 0);
1115 qsort (stubs
.sh
, stubs
.count
, sizeof (*stubs
.sh
), sort_stubs
);
1117 /* Now that the stubs are sorted, place them in the stub section.
1118 Stubs are grouped per overlay
1132 for (i
= 0; i
< stubs
.count
; i
++)
1134 if (spu_elf_section_data (stubs
.sh
[group
]->target_section
1135 ->output_section
)->ovl_index
1136 != spu_elf_section_data (stubs
.sh
[i
]->target_section
1137 ->output_section
)->ovl_index
)
1139 htab
->stub
->size
+= SIZEOF_STUB2
;
1140 for (; group
!= i
; group
++)
1141 stubs
.sh
[group
]->delta
1142 = stubs
.sh
[i
- 1]->off
- stubs
.sh
[group
]->off
;
1145 || ((stubs
.sh
[i
- 1]->target_section
->output_section
->vma
1146 + stubs
.sh
[i
- 1]->target_section
->output_offset
1147 + stubs
.sh
[i
- 1]->target_off
)
1148 != (stubs
.sh
[i
]->target_section
->output_section
->vma
1149 + stubs
.sh
[i
]->target_section
->output_offset
1150 + stubs
.sh
[i
]->target_off
)))
1152 stubs
.sh
[i
]->off
= htab
->stub
->size
;
1153 htab
->stub
->size
+= SIZEOF_STUB1
;
1156 stubs
.sh
[i
]->off
= stubs
.sh
[i
- 1]->off
;
1159 htab
->stub
->size
+= SIZEOF_STUB2
;
1160 for (; group
!= i
; group
++)
1161 stubs
.sh
[group
]->delta
= stubs
.sh
[i
- 1]->off
- stubs
.sh
[group
]->off
;
1163 /* htab->ovtab consists of two arrays.
1173 . } _ovly_buf_table[]; */
1175 htab
->ovtab
->alignment_power
= 4;
1176 htab
->ovtab
->size
= htab
->num_overlays
* 16 + htab
->num_buf
* 4;
1181 /* Functions to handle embedded spu_ovl.o object. */
1184 ovl_mgr_open (struct bfd
*nbfd ATTRIBUTE_UNUSED
, void *stream
)
1190 ovl_mgr_pread (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1196 struct _ovl_stream
*os
;
1200 os
= (struct _ovl_stream
*) stream
;
1201 max
= (const char *) os
->end
- (const char *) os
->start
;
1203 if ((ufile_ptr
) offset
>= max
)
1207 if (count
> max
- offset
)
1208 count
= max
- offset
;
1210 memcpy (buf
, (const char *) os
->start
+ offset
, count
);
1215 spu_elf_open_builtin_lib (bfd
**ovl_bfd
, const struct _ovl_stream
*stream
)
1217 *ovl_bfd
= bfd_openr_iovec ("builtin ovl_mgr",
1224 return *ovl_bfd
!= NULL
;
1227 /* Fill in the ila and br for a stub. On the last stub for a group,
1228 write the stub that sets the overlay number too. */
1231 write_one_stub (struct bfd_hash_entry
*bh
, void *inf
)
1233 struct spu_stub_hash_entry
*ent
= (struct spu_stub_hash_entry
*) bh
;
1234 struct spu_link_hash_table
*htab
= inf
;
1235 asection
*sec
= htab
->stub
;
1236 asection
*s
= ent
->target_section
;
1240 val
= ent
->target_off
+ s
->output_offset
+ s
->output_section
->vma
;
1241 bfd_put_32 (sec
->owner
, ILA_79
+ ((val
<< 7) & 0x01ffff80),
1242 sec
->contents
+ ent
->off
);
1243 val
= ent
->delta
+ 4;
1244 bfd_put_32 (sec
->owner
, BR
+ ((val
<< 5) & 0x007fff80),
1245 sec
->contents
+ ent
->off
+ 4);
1247 /* If this is the last stub of this group, write stub2. */
1248 if (ent
->delta
== 0)
1250 bfd_put_32 (sec
->owner
, NOP
,
1251 sec
->contents
+ ent
->off
+ 4);
1253 ovl
= spu_elf_section_data (s
->output_section
)->ovl_index
;
1254 bfd_put_32 (sec
->owner
, ILA_78
+ ((ovl
<< 7) & 0x01ffff80),
1255 sec
->contents
+ ent
->off
+ 8);
1257 val
= (htab
->ovly_load
->root
.u
.def
.section
->output_section
->vma
1258 + htab
->ovly_load
->root
.u
.def
.section
->output_offset
1259 + htab
->ovly_load
->root
.u
.def
.value
1260 - (sec
->output_section
->vma
1261 + sec
->output_offset
1264 if (val
+ 0x20000 >= 0x40000)
1265 htab
->stub_overflow
= TRUE
;
1267 bfd_put_32 (sec
->owner
, BR
+ ((val
<< 5) & 0x007fff80),
1268 sec
->contents
+ ent
->off
+ 12);
1271 if (htab
->emit_stub_syms
)
1273 struct elf_link_hash_entry
*h
;
1277 len1
= sizeof ("00000000.ovl_call.") - 1;
1278 len2
= strlen (ent
->root
.string
);
1279 name
= bfd_malloc (len1
+ len2
+ 1);
1282 memcpy (name
, "00000000.ovl_call.", len1
);
1283 memcpy (name
+ len1
, ent
->root
.string
, len2
+ 1);
1284 h
= elf_link_hash_lookup (&htab
->elf
, name
, TRUE
, TRUE
, FALSE
);
1288 if (h
->root
.type
== bfd_link_hash_new
)
1290 h
->root
.type
= bfd_link_hash_defined
;
1291 h
->root
.u
.def
.section
= sec
;
1292 h
->root
.u
.def
.value
= ent
->off
;
1293 h
->size
= (ent
->delta
== 0
1294 ? SIZEOF_STUB1
+ SIZEOF_STUB2
: SIZEOF_STUB1
);
1298 h
->ref_regular_nonweak
= 1;
1299 h
->forced_local
= 1;
1307 /* Define an STT_OBJECT symbol. */
1309 static struct elf_link_hash_entry
*
1310 define_ovtab_symbol (struct spu_link_hash_table
*htab
, const char *name
)
1312 struct elf_link_hash_entry
*h
;
1314 h
= elf_link_hash_lookup (&htab
->elf
, name
, TRUE
, FALSE
, FALSE
);
1318 if (h
->root
.type
!= bfd_link_hash_defined
1321 h
->root
.type
= bfd_link_hash_defined
;
1322 h
->root
.u
.def
.section
= htab
->ovtab
;
1323 h
->type
= STT_OBJECT
;
1326 h
->ref_regular_nonweak
= 1;
1331 (*_bfd_error_handler
) (_("%B is not allowed to define %s"),
1332 h
->root
.u
.def
.section
->owner
,
1333 h
->root
.root
.string
);
1334 bfd_set_error (bfd_error_bad_value
);
1341 /* Fill in all stubs and the overlay tables. */
1344 spu_elf_build_stubs (struct bfd_link_info
*info
, int emit_syms
, asection
*toe
)
1346 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
1347 struct elf_link_hash_entry
*h
;
1353 htab
->emit_stub_syms
= emit_syms
;
1354 htab
->stub
->contents
= bfd_zalloc (htab
->stub
->owner
, htab
->stub
->size
);
1355 if (htab
->stub
->contents
== NULL
)
1358 h
= elf_link_hash_lookup (&htab
->elf
, "__ovly_load", FALSE
, FALSE
, FALSE
);
1359 htab
->ovly_load
= h
;
1360 BFD_ASSERT (h
!= NULL
1361 && (h
->root
.type
== bfd_link_hash_defined
1362 || h
->root
.type
== bfd_link_hash_defweak
)
1365 s
= h
->root
.u
.def
.section
->output_section
;
1366 if (spu_elf_section_data (s
)->ovl_index
)
1368 (*_bfd_error_handler
) (_("%s in overlay section"),
1369 h
->root
.u
.def
.section
->owner
);
1370 bfd_set_error (bfd_error_bad_value
);
1374 /* Write out all the stubs. */
1375 bfd_hash_traverse (&htab
->stub_hash_table
, write_one_stub
, htab
);
1377 if (htab
->stub_overflow
)
1379 (*_bfd_error_handler
) (_("overlay stub relocation overflow"));
1380 bfd_set_error (bfd_error_bad_value
);
1384 htab
->ovtab
->contents
= bfd_zalloc (htab
->ovtab
->owner
, htab
->ovtab
->size
);
1385 if (htab
->ovtab
->contents
== NULL
)
1388 /* Write out _ovly_table. */
1389 p
= htab
->ovtab
->contents
;
1390 obfd
= htab
->ovtab
->output_section
->owner
;
1391 for (s
= obfd
->sections
; s
!= NULL
; s
= s
->next
)
1393 unsigned int ovl_index
= spu_elf_section_data (s
)->ovl_index
;
1397 unsigned int lo
, hi
, mid
;
1398 unsigned long off
= (ovl_index
- 1) * 16;
1399 bfd_put_32 (htab
->ovtab
->owner
, s
->vma
, p
+ off
);
1400 bfd_put_32 (htab
->ovtab
->owner
, (s
->size
+ 15) & -16, p
+ off
+ 4);
1401 /* file_off written later in spu_elf_modify_program_headers. */
1407 mid
= (lo
+ hi
) >> 1;
1408 if (htab
->ovl_region
[2 * mid
+ 1]->vma
1409 + htab
->ovl_region
[2 * mid
+ 1]->size
<= s
->vma
)
1411 else if (htab
->ovl_region
[2 * mid
]->vma
> s
->vma
)
1415 bfd_put_32 (htab
->ovtab
->owner
, mid
+ 1, p
+ off
+ 12);
1419 BFD_ASSERT (lo
< hi
);
1423 /* Write out _ovly_buf_table. */
1424 p
= htab
->ovtab
->contents
+ htab
->num_overlays
* 16;
1425 for (i
= 0; i
< htab
->num_buf
; i
++)
1427 bfd_put_32 (htab
->ovtab
->owner
, 0, p
);
1431 h
= define_ovtab_symbol (htab
, "_ovly_table");
1434 h
->root
.u
.def
.value
= 0;
1435 h
->size
= htab
->num_overlays
* 16;
1437 h
= define_ovtab_symbol (htab
, "_ovly_table_end");
1440 h
->root
.u
.def
.value
= htab
->num_overlays
* 16;
1443 h
= define_ovtab_symbol (htab
, "_ovly_buf_table");
1446 h
->root
.u
.def
.value
= htab
->num_overlays
* 16;
1447 h
->size
= htab
->num_buf
* 4;
1449 h
= define_ovtab_symbol (htab
, "_ovly_buf_table_end");
1452 h
->root
.u
.def
.value
= htab
->num_overlays
* 16 + htab
->num_buf
* 4;
1455 h
= define_ovtab_symbol (htab
, "_EAR_");
1458 h
->root
.u
.def
.section
= toe
;
1459 h
->root
.u
.def
.value
= 0;
1465 /* OFFSET in SEC (presumably) is the beginning of a function prologue.
1466 Search for stack adjusting insns, and return the sp delta. */
1469 find_function_stack_adjust (asection
*sec
, bfd_vma offset
)
1474 memset (reg
, 0, sizeof (reg
));
1475 for (unrecog
= 0; offset
+ 4 <= sec
->size
&& unrecog
< 32; offset
+= 4)
1477 unsigned char buf
[4];
1481 /* Assume no relocs on stack adjusing insns. */
1482 if (!bfd_get_section_contents (sec
->owner
, sec
, buf
, offset
, 4))
1485 if (buf
[0] == 0x24 /* stqd */)
1489 ra
= ((buf
[2] & 0x3f) << 1) | (buf
[3] >> 7);
1490 /* Partly decoded immediate field. */
1491 imm
= (buf
[1] << 9) | (buf
[2] << 1) | (buf
[3] >> 7);
1493 if (buf
[0] == 0x1c /* ai */)
1496 imm
= (imm
^ 0x200) - 0x200;
1497 reg
[rt
] = reg
[ra
] + imm
;
1499 if (rt
== 1 /* sp */)
1506 else if (buf
[0] == 0x18 && (buf
[1] & 0xe0) == 0 /* a */)
1508 int rb
= ((buf
[1] & 0x1f) << 2) | ((buf
[2] & 0xc0) >> 6);
1510 reg
[rt
] = reg
[ra
] + reg
[rb
];
1514 else if ((buf
[0] & 0xfc) == 0x40 /* il, ilh, ilhu, ila */)
1516 if (buf
[0] >= 0x42 /* ila */)
1517 imm
|= (buf
[0] & 1) << 17;
1522 if (buf
[0] == 0x40 /* il */)
1524 if ((buf
[1] & 0x80) == 0)
1526 imm
= (imm
^ 0x8000) - 0x8000;
1528 else if ((buf
[1] & 0x80) == 0 /* ilhu */)
1534 else if (buf
[0] == 0x60 && (buf
[1] & 0x80) != 0 /* iohl */)
1536 reg
[rt
] |= imm
& 0xffff;
1539 else if (buf
[0] == 0x04 /* ori */)
1542 imm
= (imm
^ 0x200) - 0x200;
1543 reg
[rt
] = reg
[ra
] | imm
;
1546 else if ((buf
[0] == 0x33 && imm
== 1 /* brsl .+4 */)
1547 || (buf
[0] == 0x08 && (buf
[1] & 0xe0) == 0 /* sf */))
1549 /* Used in pic reg load. Say rt is trashed. */
1553 else if (is_branch (buf
) || is_indirect_branch (buf
))
1554 /* If we hit a branch then we must be out of the prologue. */
1563 /* qsort predicate to sort symbols by section and value. */
1565 static Elf_Internal_Sym
*sort_syms_syms
;
1566 static asection
**sort_syms_psecs
;
1569 sort_syms (const void *a
, const void *b
)
1571 Elf_Internal_Sym
*const *s1
= a
;
1572 Elf_Internal_Sym
*const *s2
= b
;
1573 asection
*sec1
,*sec2
;
1574 bfd_signed_vma delta
;
1576 sec1
= sort_syms_psecs
[*s1
- sort_syms_syms
];
1577 sec2
= sort_syms_psecs
[*s2
- sort_syms_syms
];
1580 return sec1
->index
- sec2
->index
;
1582 delta
= (*s1
)->st_value
- (*s2
)->st_value
;
1584 return delta
< 0 ? -1 : 1;
1586 delta
= (*s2
)->st_size
- (*s1
)->st_size
;
1588 return delta
< 0 ? -1 : 1;
1590 return *s1
< *s2
? -1 : 1;
1595 struct function_info
*fun
;
1596 struct call_info
*next
;
1600 struct function_info
1602 /* List of functions called. Also branches to hot/cold part of
1604 struct call_info
*call_list
;
1605 /* For hot/cold part of function, point to owner. */
1606 struct function_info
*start
;
1607 /* Symbol at start of function. */
1609 Elf_Internal_Sym
*sym
;
1610 struct elf_link_hash_entry
*h
;
1612 /* Function section. */
1614 /* Address range of (this part of) function. */
1618 /* Set if global symbol. */
1619 unsigned int global
: 1;
1620 /* Set if known to be start of function (as distinct from a hunk
1621 in hot/cold section. */
1622 unsigned int is_func
: 1;
1623 /* Flags used during call tree traversal. */
1624 unsigned int visit1
: 1;
1625 unsigned int non_root
: 1;
1626 unsigned int visit2
: 1;
1627 unsigned int marking
: 1;
1628 unsigned int visit3
: 1;
1631 struct spu_elf_stack_info
1635 /* Variable size array describing functions, one per contiguous
1636 address range belonging to a function. */
1637 struct function_info fun
[1];
1640 /* Allocate a struct spu_elf_stack_info with MAX_FUN struct function_info
1641 entries for section SEC. */
1643 static struct spu_elf_stack_info
*
1644 alloc_stack_info (asection
*sec
, int max_fun
)
1646 struct _spu_elf_section_data
*sec_data
= spu_elf_section_data (sec
);
1649 amt
= sizeof (struct spu_elf_stack_info
);
1650 amt
+= (max_fun
- 1) * sizeof (struct function_info
);
1651 sec_data
->stack_info
= bfd_zmalloc (amt
);
1652 if (sec_data
->stack_info
!= NULL
)
1653 sec_data
->stack_info
->max_fun
= max_fun
;
1654 return sec_data
->stack_info
;
1657 /* Add a new struct function_info describing a (part of a) function
1658 starting at SYM_H. Keep the array sorted by address. */
1660 static struct function_info
*
1661 maybe_insert_function (asection
*sec
,
1664 bfd_boolean is_func
)
1666 struct _spu_elf_section_data
*sec_data
= spu_elf_section_data (sec
);
1667 struct spu_elf_stack_info
*sinfo
= sec_data
->stack_info
;
1673 sinfo
= alloc_stack_info (sec
, 20);
1680 Elf_Internal_Sym
*sym
= sym_h
;
1681 off
= sym
->st_value
;
1682 size
= sym
->st_size
;
1686 struct elf_link_hash_entry
*h
= sym_h
;
1687 off
= h
->root
.u
.def
.value
;
1691 for (i
= sinfo
->num_fun
; --i
>= 0; )
1692 if (sinfo
->fun
[i
].lo
<= off
)
1697 /* Don't add another entry for an alias, but do update some
1699 if (sinfo
->fun
[i
].lo
== off
)
1701 /* Prefer globals over local syms. */
1702 if (global
&& !sinfo
->fun
[i
].global
)
1704 sinfo
->fun
[i
].global
= TRUE
;
1705 sinfo
->fun
[i
].u
.h
= sym_h
;
1708 sinfo
->fun
[i
].is_func
= TRUE
;
1709 return &sinfo
->fun
[i
];
1711 /* Ignore a zero-size symbol inside an existing function. */
1712 else if (sinfo
->fun
[i
].hi
> off
&& size
== 0)
1713 return &sinfo
->fun
[i
];
1716 if (++i
< sinfo
->num_fun
)
1717 memmove (&sinfo
->fun
[i
+ 1], &sinfo
->fun
[i
],
1718 (sinfo
->num_fun
- i
) * sizeof (sinfo
->fun
[i
]));
1719 else if (i
>= sinfo
->max_fun
)
1721 bfd_size_type amt
= sizeof (struct spu_elf_stack_info
);
1722 bfd_size_type old
= amt
;
1724 old
+= (sinfo
->max_fun
- 1) * sizeof (struct function_info
);
1725 sinfo
->max_fun
+= 20 + (sinfo
->max_fun
>> 1);
1726 amt
+= (sinfo
->max_fun
- 1) * sizeof (struct function_info
);
1727 sinfo
= bfd_realloc (sinfo
, amt
);
1730 memset ((char *) sinfo
+ old
, 0, amt
- old
);
1731 sec_data
->stack_info
= sinfo
;
1733 sinfo
->fun
[i
].is_func
= is_func
;
1734 sinfo
->fun
[i
].global
= global
;
1735 sinfo
->fun
[i
].sec
= sec
;
1737 sinfo
->fun
[i
].u
.h
= sym_h
;
1739 sinfo
->fun
[i
].u
.sym
= sym_h
;
1740 sinfo
->fun
[i
].lo
= off
;
1741 sinfo
->fun
[i
].hi
= off
+ size
;
1742 sinfo
->fun
[i
].stack
= -find_function_stack_adjust (sec
, off
);
1743 sinfo
->num_fun
+= 1;
1744 return &sinfo
->fun
[i
];
1747 /* Return the name of FUN. */
1750 func_name (struct function_info
*fun
)
1754 Elf_Internal_Shdr
*symtab_hdr
;
1756 while (fun
->start
!= NULL
)
1760 return fun
->u
.h
->root
.root
.string
;
1763 if (fun
->u
.sym
->st_name
== 0)
1765 size_t len
= strlen (sec
->name
);
1766 char *name
= bfd_malloc (len
+ 10);
1769 sprintf (name
, "%s+%lx", sec
->name
,
1770 (unsigned long) fun
->u
.sym
->st_value
& 0xffffffff);
1774 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1775 return bfd_elf_sym_name (ibfd
, symtab_hdr
, fun
->u
.sym
, sec
);
1778 /* Read the instruction at OFF in SEC. Return true iff the instruction
1779 is a nop, lnop, or stop 0 (all zero insn). */
1782 is_nop (asection
*sec
, bfd_vma off
)
1784 unsigned char insn
[4];
1786 if (off
+ 4 > sec
->size
1787 || !bfd_get_section_contents (sec
->owner
, sec
, insn
, off
, 4))
1789 if ((insn
[0] & 0xbf) == 0 && (insn
[1] & 0xe0) == 0x20)
1791 if (insn
[0] == 0 && insn
[1] == 0 && insn
[2] == 0 && insn
[3] == 0)
1796 /* Extend the range of FUN to cover nop padding up to LIMIT.
1797 Return TRUE iff some instruction other than a NOP was found. */
1800 insns_at_end (struct function_info
*fun
, bfd_vma limit
)
1802 bfd_vma off
= (fun
->hi
+ 3) & -4;
1804 while (off
< limit
&& is_nop (fun
->sec
, off
))
1815 /* Check and fix overlapping function ranges. Return TRUE iff there
1816 are gaps in the current info we have about functions in SEC. */
1819 check_function_ranges (asection
*sec
, struct bfd_link_info
*info
)
1821 struct _spu_elf_section_data
*sec_data
= spu_elf_section_data (sec
);
1822 struct spu_elf_stack_info
*sinfo
= sec_data
->stack_info
;
1824 bfd_boolean gaps
= FALSE
;
1829 for (i
= 1; i
< sinfo
->num_fun
; i
++)
1830 if (sinfo
->fun
[i
- 1].hi
> sinfo
->fun
[i
].lo
)
1832 /* Fix overlapping symbols. */
1833 const char *f1
= func_name (&sinfo
->fun
[i
- 1]);
1834 const char *f2
= func_name (&sinfo
->fun
[i
]);
1836 info
->callbacks
->einfo (_("warning: %s overlaps %s\n"), f1
, f2
);
1837 sinfo
->fun
[i
- 1].hi
= sinfo
->fun
[i
].lo
;
1839 else if (insns_at_end (&sinfo
->fun
[i
- 1], sinfo
->fun
[i
].lo
))
1842 if (sinfo
->num_fun
== 0)
1846 if (sinfo
->fun
[0].lo
!= 0)
1848 if (sinfo
->fun
[sinfo
->num_fun
- 1].hi
> sec
->size
)
1850 const char *f1
= func_name (&sinfo
->fun
[sinfo
->num_fun
- 1]);
1852 info
->callbacks
->einfo (_("warning: %s exceeds section size\n"), f1
);
1853 sinfo
->fun
[sinfo
->num_fun
- 1].hi
= sec
->size
;
1855 else if (insns_at_end (&sinfo
->fun
[sinfo
->num_fun
- 1], sec
->size
))
1861 /* Search current function info for a function that contains address
1862 OFFSET in section SEC. */
1864 static struct function_info
*
1865 find_function (asection
*sec
, bfd_vma offset
, struct bfd_link_info
*info
)
1867 struct _spu_elf_section_data
*sec_data
= spu_elf_section_data (sec
);
1868 struct spu_elf_stack_info
*sinfo
= sec_data
->stack_info
;
1872 hi
= sinfo
->num_fun
;
1875 mid
= (lo
+ hi
) / 2;
1876 if (offset
< sinfo
->fun
[mid
].lo
)
1878 else if (offset
>= sinfo
->fun
[mid
].hi
)
1881 return &sinfo
->fun
[mid
];
1883 info
->callbacks
->einfo (_("%A:0x%v not found in function table\n"),
1888 /* Add CALLEE to CALLER call list if not already present. */
1891 insert_callee (struct function_info
*caller
, struct call_info
*callee
)
1893 struct call_info
*p
;
1894 for (p
= caller
->call_list
; p
!= NULL
; p
= p
->next
)
1895 if (p
->fun
== callee
->fun
)
1897 /* Tail calls use less stack than normal calls. Retain entry
1898 for normal call over one for tail call. */
1899 if (p
->is_tail
> callee
->is_tail
)
1900 p
->is_tail
= callee
->is_tail
;
1903 callee
->next
= caller
->call_list
;
1904 caller
->call_list
= callee
;
1908 /* Rummage through the relocs for SEC, looking for function calls.
1909 If CALL_TREE is true, fill in call graph. If CALL_TREE is false,
1910 mark destination symbols on calls as being functions. Also
1911 look at branches, which may be tail calls or go to hot/cold
1912 section part of same function. */
1915 mark_functions_via_relocs (asection
*sec
,
1916 struct bfd_link_info
*info
,
1919 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
1920 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (sec
->owner
)->symtab_hdr
;
1921 Elf_Internal_Sym
*syms
;
1923 static bfd_boolean warned
;
1925 internal_relocs
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
,
1927 if (internal_relocs
== NULL
)
1930 symtab_hdr
= &elf_tdata (sec
->owner
)->symtab_hdr
;
1931 psyms
= &symtab_hdr
->contents
;
1932 syms
= *(Elf_Internal_Sym
**) psyms
;
1933 irela
= internal_relocs
;
1934 irelaend
= irela
+ sec
->reloc_count
;
1935 for (; irela
< irelaend
; irela
++)
1937 enum elf_spu_reloc_type r_type
;
1938 unsigned int r_indx
;
1940 Elf_Internal_Sym
*sym
;
1941 struct elf_link_hash_entry
*h
;
1943 unsigned char insn
[4];
1944 bfd_boolean is_call
;
1945 struct function_info
*caller
;
1946 struct call_info
*callee
;
1948 r_type
= ELF32_R_TYPE (irela
->r_info
);
1949 if (r_type
!= R_SPU_REL16
1950 && r_type
!= R_SPU_ADDR16
)
1953 r_indx
= ELF32_R_SYM (irela
->r_info
);
1954 if (!get_sym_h (&h
, &sym
, &sym_sec
, psyms
, r_indx
, sec
->owner
))
1958 || sym_sec
->output_section
== NULL
1959 || sym_sec
->output_section
->owner
!= sec
->output_section
->owner
)
1962 if (!bfd_get_section_contents (sec
->owner
, sec
, insn
,
1963 irela
->r_offset
, 4))
1965 if (!is_branch (insn
))
1968 if ((sym_sec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
))
1969 != (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
))
1973 if (!call_tree
|| !warned
)
1974 info
->callbacks
->einfo (_("%B(%A+0x%v): call to non-code section"
1975 " %B(%A), stack analysis incomplete\n"),
1976 sec
->owner
, sec
, irela
->r_offset
,
1977 sym_sec
->owner
, sym_sec
);
1981 is_call
= (insn
[0] & 0xfd) == 0x31;
1984 val
= h
->root
.u
.def
.value
;
1986 val
= sym
->st_value
;
1987 val
+= irela
->r_addend
;
1991 struct function_info
*fun
;
1993 if (irela
->r_addend
!= 0)
1995 Elf_Internal_Sym
*fake
= bfd_zmalloc (sizeof (*fake
));
1998 fake
->st_value
= val
;
2000 = _bfd_elf_section_from_bfd_section (sym_sec
->owner
, sym_sec
);
2004 fun
= maybe_insert_function (sym_sec
, sym
, FALSE
, is_call
);
2006 fun
= maybe_insert_function (sym_sec
, h
, TRUE
, is_call
);
2009 if (irela
->r_addend
!= 0
2010 && fun
->u
.sym
!= sym
)
2015 caller
= find_function (sec
, irela
->r_offset
, info
);
2018 callee
= bfd_malloc (sizeof *callee
);
2022 callee
->fun
= find_function (sym_sec
, val
, info
);
2023 if (callee
->fun
== NULL
)
2025 callee
->is_tail
= !is_call
;
2026 if (!insert_callee (caller
, callee
))
2029 && !callee
->fun
->is_func
2030 && callee
->fun
->stack
== 0)
2032 /* This is either a tail call or a branch from one part of
2033 the function to another, ie. hot/cold section. If the
2034 destination has been called by some other function then
2035 it is a separate function. We also assume that functions
2036 are not split across input files. */
2037 if (callee
->fun
->start
!= NULL
2038 || sec
->owner
!= sym_sec
->owner
)
2040 callee
->fun
->start
= NULL
;
2041 callee
->fun
->is_func
= TRUE
;
2044 callee
->fun
->start
= caller
;
2051 /* Handle something like .init or .fini, which has a piece of a function.
2052 These sections are pasted together to form a single function. */
2055 pasted_function (asection
*sec
, struct bfd_link_info
*info
)
2057 struct bfd_link_order
*l
;
2058 struct _spu_elf_section_data
*sec_data
;
2059 struct spu_elf_stack_info
*sinfo
;
2060 Elf_Internal_Sym
*fake
;
2061 struct function_info
*fun
, *fun_start
;
2063 fake
= bfd_zmalloc (sizeof (*fake
));
2067 fake
->st_size
= sec
->size
;
2069 = _bfd_elf_section_from_bfd_section (sec
->owner
, sec
);
2070 fun
= maybe_insert_function (sec
, fake
, FALSE
, FALSE
);
2074 /* Find a function immediately preceding this section. */
2076 for (l
= sec
->output_section
->map_head
.link_order
; l
!= NULL
; l
= l
->next
)
2078 if (l
->u
.indirect
.section
== sec
)
2080 if (fun_start
!= NULL
)
2082 if (fun_start
->start
)
2083 fun_start
= fun_start
->start
;
2084 fun
->start
= fun_start
;
2088 if (l
->type
== bfd_indirect_link_order
2089 && (sec_data
= spu_elf_section_data (l
->u
.indirect
.section
)) != NULL
2090 && (sinfo
= sec_data
->stack_info
) != NULL
2091 && sinfo
->num_fun
!= 0)
2092 fun_start
= &sinfo
->fun
[sinfo
->num_fun
- 1];
2095 info
->callbacks
->einfo (_("%A link_order not found\n"), sec
);
2099 /* We're only interested in code sections. */
2102 interesting_section (asection
*s
, bfd
*obfd
, struct spu_link_hash_table
*htab
)
2104 return (s
!= htab
->stub
2105 && s
->output_section
!= NULL
2106 && s
->output_section
->owner
== obfd
2107 && ((s
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
))
2108 == (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
))
2112 /* Map address ranges in code sections to functions. */
2115 discover_functions (bfd
*output_bfd
, struct bfd_link_info
*info
)
2117 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
2120 Elf_Internal_Sym
***psym_arr
;
2121 asection
***sec_arr
;
2122 bfd_boolean gaps
= FALSE
;
2125 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2128 psym_arr
= bfd_zmalloc (bfd_idx
* sizeof (*psym_arr
));
2129 if (psym_arr
== NULL
)
2131 sec_arr
= bfd_zmalloc (bfd_idx
* sizeof (*sec_arr
));
2132 if (sec_arr
== NULL
)
2136 for (ibfd
= info
->input_bfds
, bfd_idx
= 0;
2138 ibfd
= ibfd
->link_next
, bfd_idx
++)
2140 extern const bfd_target bfd_elf32_spu_vec
;
2141 Elf_Internal_Shdr
*symtab_hdr
;
2144 Elf_Internal_Sym
*syms
, *sy
, **psyms
, **psy
;
2145 asection
**psecs
, **p
;
2147 if (ibfd
->xvec
!= &bfd_elf32_spu_vec
)
2150 /* Read all the symbols. */
2151 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
2152 symcount
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
2156 syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2159 syms
= bfd_elf_get_elf_syms (ibfd
, symtab_hdr
, symcount
, 0,
2161 symtab_hdr
->contents
= (void *) syms
;
2166 /* Select defined function symbols that are going to be output. */
2167 psyms
= bfd_malloc ((symcount
+ 1) * sizeof (*psyms
));
2170 psym_arr
[bfd_idx
] = psyms
;
2171 psecs
= bfd_malloc (symcount
* sizeof (*psecs
));
2174 sec_arr
[bfd_idx
] = psecs
;
2175 for (psy
= psyms
, p
= psecs
, sy
= syms
; sy
< syms
+ symcount
; ++p
, ++sy
)
2176 if (ELF_ST_TYPE (sy
->st_info
) == STT_NOTYPE
2177 || ELF_ST_TYPE (sy
->st_info
) == STT_FUNC
)
2181 *p
= s
= bfd_section_from_elf_index (ibfd
, sy
->st_shndx
);
2182 if (s
!= NULL
&& interesting_section (s
, output_bfd
, htab
))
2185 symcount
= psy
- psyms
;
2188 /* Sort them by section and offset within section. */
2189 sort_syms_syms
= syms
;
2190 sort_syms_psecs
= psecs
;
2191 qsort (psyms
, symcount
, sizeof (*psyms
), sort_syms
);
2193 /* Now inspect the function symbols. */
2194 for (psy
= psyms
; psy
< psyms
+ symcount
; )
2196 asection
*s
= psecs
[*psy
- syms
];
2197 Elf_Internal_Sym
**psy2
;
2199 for (psy2
= psy
; ++psy2
< psyms
+ symcount
; )
2200 if (psecs
[*psy2
- syms
] != s
)
2203 if (!alloc_stack_info (s
, psy2
- psy
))
2208 /* First install info about properly typed and sized functions.
2209 In an ideal world this will cover all code sections, except
2210 when partitioning functions into hot and cold sections,
2211 and the horrible pasted together .init and .fini functions. */
2212 for (psy
= psyms
; psy
< psyms
+ symcount
; ++psy
)
2215 if (ELF_ST_TYPE (sy
->st_info
) == STT_FUNC
)
2217 asection
*s
= psecs
[sy
- syms
];
2218 if (!maybe_insert_function (s
, sy
, FALSE
, TRUE
))
2223 for (sec
= ibfd
->sections
; sec
!= NULL
&& !gaps
; sec
= sec
->next
)
2224 if (interesting_section (sec
, output_bfd
, htab
))
2225 gaps
|= check_function_ranges (sec
, info
);
2230 /* See if we can discover more function symbols by looking at
2232 for (ibfd
= info
->input_bfds
, bfd_idx
= 0;
2234 ibfd
= ibfd
->link_next
, bfd_idx
++)
2238 if (psym_arr
[bfd_idx
] == NULL
)
2241 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2242 if (interesting_section (sec
, output_bfd
, htab
)
2243 && sec
->reloc_count
!= 0)
2245 if (!mark_functions_via_relocs (sec
, info
, FALSE
))
2250 for (ibfd
= info
->input_bfds
, bfd_idx
= 0;
2252 ibfd
= ibfd
->link_next
, bfd_idx
++)
2254 Elf_Internal_Shdr
*symtab_hdr
;
2256 Elf_Internal_Sym
*syms
, *sy
, **psyms
, **psy
;
2259 if ((psyms
= psym_arr
[bfd_idx
]) == NULL
)
2262 psecs
= sec_arr
[bfd_idx
];
2264 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
2265 syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2268 for (sec
= ibfd
->sections
; sec
!= NULL
&& !gaps
; sec
= sec
->next
)
2269 if (interesting_section (sec
, output_bfd
, htab
))
2270 gaps
|= check_function_ranges (sec
, info
);
2274 /* Finally, install all globals. */
2275 for (psy
= psyms
; (sy
= *psy
) != NULL
; ++psy
)
2279 s
= psecs
[sy
- syms
];
2281 /* Global syms might be improperly typed functions. */
2282 if (ELF_ST_TYPE (sy
->st_info
) != STT_FUNC
2283 && ELF_ST_BIND (sy
->st_info
) == STB_GLOBAL
)
2285 if (!maybe_insert_function (s
, sy
, FALSE
, FALSE
))
2290 /* Some of the symbols we've installed as marking the
2291 beginning of functions may have a size of zero. Extend
2292 the range of such functions to the beginning of the
2293 next symbol of interest. */
2294 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2295 if (interesting_section (sec
, output_bfd
, htab
))
2297 struct _spu_elf_section_data
*sec_data
;
2298 struct spu_elf_stack_info
*sinfo
;
2300 sec_data
= spu_elf_section_data (sec
);
2301 sinfo
= sec_data
->stack_info
;
2305 bfd_vma hi
= sec
->size
;
2307 for (fun_idx
= sinfo
->num_fun
; --fun_idx
>= 0; )
2309 sinfo
->fun
[fun_idx
].hi
= hi
;
2310 hi
= sinfo
->fun
[fun_idx
].lo
;
2313 /* No symbols in this section. Must be .init or .fini
2314 or something similar. */
2315 else if (!pasted_function (sec
, info
))
2321 for (ibfd
= info
->input_bfds
, bfd_idx
= 0;
2323 ibfd
= ibfd
->link_next
, bfd_idx
++)
2325 if (psym_arr
[bfd_idx
] == NULL
)
2328 free (psym_arr
[bfd_idx
]);
2329 free (sec_arr
[bfd_idx
]);
2338 /* Mark nodes in the call graph that are called by some other node. */
2341 mark_non_root (struct function_info
*fun
)
2343 struct call_info
*call
;
2346 for (call
= fun
->call_list
; call
; call
= call
->next
)
2348 call
->fun
->non_root
= TRUE
;
2349 if (!call
->fun
->visit1
)
2350 mark_non_root (call
->fun
);
2354 /* Remove cycles from the call graph. */
2357 call_graph_traverse (struct function_info
*fun
, struct bfd_link_info
*info
)
2359 struct call_info
**callp
, *call
;
2362 fun
->marking
= TRUE
;
2364 callp
= &fun
->call_list
;
2365 while ((call
= *callp
) != NULL
)
2367 if (!call
->fun
->visit2
)
2368 call_graph_traverse (call
->fun
, info
);
2369 else if (call
->fun
->marking
)
2371 const char *f1
= func_name (fun
);
2372 const char *f2
= func_name (call
->fun
);
2374 info
->callbacks
->info (_("Stack analysis will ignore the call "
2377 *callp
= call
->next
;
2380 callp
= &call
->next
;
2382 fun
->marking
= FALSE
;
2385 /* Populate call_list for each function. */
2388 build_call_tree (bfd
*output_bfd
, struct bfd_link_info
*info
)
2390 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
2393 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2395 extern const bfd_target bfd_elf32_spu_vec
;
2398 if (ibfd
->xvec
!= &bfd_elf32_spu_vec
)
2401 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2403 if (!interesting_section (sec
, output_bfd
, htab
)
2404 || sec
->reloc_count
== 0)
2407 if (!mark_functions_via_relocs (sec
, info
, TRUE
))
2411 /* Transfer call info from hot/cold section part of function
2413 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2415 struct _spu_elf_section_data
*sec_data
;
2416 struct spu_elf_stack_info
*sinfo
;
2418 if ((sec_data
= spu_elf_section_data (sec
)) != NULL
2419 && (sinfo
= sec_data
->stack_info
) != NULL
)
2422 for (i
= 0; i
< sinfo
->num_fun
; ++i
)
2424 if (sinfo
->fun
[i
].start
!= NULL
)
2426 struct call_info
*call
= sinfo
->fun
[i
].call_list
;
2428 while (call
!= NULL
)
2430 struct call_info
*call_next
= call
->next
;
2431 if (!insert_callee (sinfo
->fun
[i
].start
, call
))
2435 sinfo
->fun
[i
].call_list
= NULL
;
2436 sinfo
->fun
[i
].non_root
= TRUE
;
2443 /* Find the call graph root(s). */
2444 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2446 extern const bfd_target bfd_elf32_spu_vec
;
2449 if (ibfd
->xvec
!= &bfd_elf32_spu_vec
)
2452 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2454 struct _spu_elf_section_data
*sec_data
;
2455 struct spu_elf_stack_info
*sinfo
;
2457 if ((sec_data
= spu_elf_section_data (sec
)) != NULL
2458 && (sinfo
= sec_data
->stack_info
) != NULL
)
2461 for (i
= 0; i
< sinfo
->num_fun
; ++i
)
2462 if (!sinfo
->fun
[i
].visit1
)
2463 mark_non_root (&sinfo
->fun
[i
]);
2468 /* Remove cycles from the call graph. We start from the root node(s)
2469 so that we break cycles in a reasonable place. */
2470 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2472 extern const bfd_target bfd_elf32_spu_vec
;
2475 if (ibfd
->xvec
!= &bfd_elf32_spu_vec
)
2478 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2480 struct _spu_elf_section_data
*sec_data
;
2481 struct spu_elf_stack_info
*sinfo
;
2483 if ((sec_data
= spu_elf_section_data (sec
)) != NULL
2484 && (sinfo
= sec_data
->stack_info
) != NULL
)
2487 for (i
= 0; i
< sinfo
->num_fun
; ++i
)
2488 if (!sinfo
->fun
[i
].non_root
)
2489 call_graph_traverse (&sinfo
->fun
[i
], info
);
2497 /* Descend the call graph for FUN, accumulating total stack required. */
2500 sum_stack (struct function_info
*fun
,
2501 struct bfd_link_info
*info
,
2502 int emit_stack_syms
)
2504 struct call_info
*call
;
2505 struct function_info
*max
= NULL
;
2506 bfd_vma max_stack
= fun
->stack
;
2513 for (call
= fun
->call_list
; call
; call
= call
->next
)
2515 stack
= sum_stack (call
->fun
, info
, emit_stack_syms
);
2516 /* Include caller stack for normal calls, don't do so for
2517 tail calls. fun->stack here is local stack usage for
2520 stack
+= fun
->stack
;
2521 if (max_stack
< stack
)
2528 f1
= func_name (fun
);
2529 info
->callbacks
->minfo (_("%s: 0x%v 0x%v\n"),
2530 f1
, (bfd_vma
) fun
->stack
, max_stack
);
2534 info
->callbacks
->minfo (_(" calls:\n"));
2535 for (call
= fun
->call_list
; call
; call
= call
->next
)
2537 const char *f2
= func_name (call
->fun
);
2538 const char *ann1
= call
->fun
== max
? "*" : " ";
2539 const char *ann2
= call
->is_tail
? "t" : " ";
2541 info
->callbacks
->minfo (_(" %s%s %s\n"), ann1
, ann2
, f2
);
2545 /* Now fun->stack holds cumulative stack. */
2546 fun
->stack
= max_stack
;
2549 if (emit_stack_syms
)
2551 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
2552 char *name
= bfd_malloc (18 + strlen (f1
));
2553 struct elf_link_hash_entry
*h
;
2557 if (fun
->global
|| ELF_ST_BIND (fun
->u
.sym
->st_info
) == STB_GLOBAL
)
2558 sprintf (name
, "__stack_%s", f1
);
2560 sprintf (name
, "__stack_%x_%s", fun
->sec
->id
& 0xffffffff, f1
);
2562 h
= elf_link_hash_lookup (&htab
->elf
, name
, TRUE
, TRUE
, FALSE
);
2565 && (h
->root
.type
== bfd_link_hash_new
2566 || h
->root
.type
== bfd_link_hash_undefined
2567 || h
->root
.type
== bfd_link_hash_undefweak
))
2569 h
->root
.type
= bfd_link_hash_defined
;
2570 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2571 h
->root
.u
.def
.value
= max_stack
;
2576 h
->ref_regular_nonweak
= 1;
2577 h
->forced_local
= 1;
2586 /* Provide an estimate of total stack required. */
2589 spu_elf_stack_analysis (bfd
*output_bfd
,
2590 struct bfd_link_info
*info
,
2591 int emit_stack_syms
)
2594 bfd_vma max_stack
= 0;
2596 if (!discover_functions (output_bfd
, info
))
2599 if (!build_call_tree (output_bfd
, info
))
2602 info
->callbacks
->info (_("Stack size for call graph root nodes.\n"));
2603 info
->callbacks
->minfo (_("\nStack size for functions. "
2604 "Annotations: '*' max stack, 't' tail call\n"));
2605 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2607 extern const bfd_target bfd_elf32_spu_vec
;
2610 if (ibfd
->xvec
!= &bfd_elf32_spu_vec
)
2613 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2615 struct _spu_elf_section_data
*sec_data
;
2616 struct spu_elf_stack_info
*sinfo
;
2618 if ((sec_data
= spu_elf_section_data (sec
)) != NULL
2619 && (sinfo
= sec_data
->stack_info
) != NULL
)
2622 for (i
= 0; i
< sinfo
->num_fun
; ++i
)
2624 if (!sinfo
->fun
[i
].non_root
)
2629 stack
= sum_stack (&sinfo
->fun
[i
], info
,
2631 f1
= func_name (&sinfo
->fun
[i
]);
2632 info
->callbacks
->info (_(" %s: 0x%v\n"),
2634 if (max_stack
< stack
)
2642 info
->callbacks
->info (_("Maximum stack required is 0x%v\n"), max_stack
);
2646 /* Perform a final link. */
2649 spu_elf_final_link (bfd
*output_bfd
, struct bfd_link_info
*info
)
2651 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
2653 if (htab
->stack_analysis
2654 && !spu_elf_stack_analysis (output_bfd
, info
, htab
->emit_stack_syms
))
2655 info
->callbacks
->einfo ("%X%P: stack analysis error: %E\n");
2657 return bfd_elf_final_link (output_bfd
, info
);
2660 /* Called when not normally emitting relocs, ie. !info->relocatable
2661 and !info->emitrelocations. Returns a count of special relocs
2662 that need to be emitted. */
2665 spu_elf_count_relocs (asection
*sec
, Elf_Internal_Rela
*relocs
)
2667 unsigned int count
= 0;
2668 Elf_Internal_Rela
*relend
= relocs
+ sec
->reloc_count
;
2670 for (; relocs
< relend
; relocs
++)
2672 int r_type
= ELF32_R_TYPE (relocs
->r_info
);
2673 if (r_type
== R_SPU_PPU32
|| r_type
== R_SPU_PPU64
)
2680 /* Apply RELOCS to CONTENTS of INPUT_SECTION from INPUT_BFD. */
2683 spu_elf_relocate_section (bfd
*output_bfd
,
2684 struct bfd_link_info
*info
,
2686 asection
*input_section
,
2688 Elf_Internal_Rela
*relocs
,
2689 Elf_Internal_Sym
*local_syms
,
2690 asection
**local_sections
)
2692 Elf_Internal_Shdr
*symtab_hdr
;
2693 struct elf_link_hash_entry
**sym_hashes
;
2694 Elf_Internal_Rela
*rel
, *relend
;
2695 struct spu_link_hash_table
*htab
;
2696 bfd_boolean ret
= TRUE
;
2697 bfd_boolean emit_these_relocs
= FALSE
;
2699 htab
= spu_hash_table (info
);
2700 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2701 sym_hashes
= (struct elf_link_hash_entry
**) (elf_sym_hashes (input_bfd
));
2704 relend
= relocs
+ input_section
->reloc_count
;
2705 for (; rel
< relend
; rel
++)
2708 reloc_howto_type
*howto
;
2709 unsigned long r_symndx
;
2710 Elf_Internal_Sym
*sym
;
2712 struct elf_link_hash_entry
*h
;
2713 const char *sym_name
;
2716 bfd_reloc_status_type r
;
2717 bfd_boolean unresolved_reloc
;
2721 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2722 r_type
= ELF32_R_TYPE (rel
->r_info
);
2723 if (r_type
== R_SPU_PPU32
|| r_type
== R_SPU_PPU64
)
2725 emit_these_relocs
= TRUE
;
2729 howto
= elf_howto_table
+ r_type
;
2730 unresolved_reloc
= FALSE
;
2735 if (r_symndx
< symtab_hdr
->sh_info
)
2737 sym
= local_syms
+ r_symndx
;
2738 sec
= local_sections
[r_symndx
];
2739 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, sec
);
2740 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2744 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2745 r_symndx
, symtab_hdr
, sym_hashes
,
2747 unresolved_reloc
, warned
);
2748 sym_name
= h
->root
.root
.string
;
2751 if (sec
!= NULL
&& elf_discarded_section (sec
))
2753 /* For relocs against symbols from removed linkonce sections,
2754 or sections discarded by a linker script, we just want the
2755 section contents zeroed. Avoid any special processing. */
2756 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
2762 if (info
->relocatable
)
2765 if (unresolved_reloc
)
2767 (*_bfd_error_handler
)
2768 (_("%B(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
2770 bfd_get_section_name (input_bfd
, input_section
),
2771 (long) rel
->r_offset
,
2777 /* If this symbol is in an overlay area, we may need to relocate
2778 to the overlay stub. */
2779 addend
= rel
->r_addend
;
2780 branch
= (is_branch (contents
+ rel
->r_offset
)
2781 || is_hint (contents
+ rel
->r_offset
));
2782 if (needs_ovl_stub (sym_name
, sec
, input_section
, htab
, branch
))
2785 struct spu_stub_hash_entry
*sh
;
2787 stub_name
= spu_stub_name (sec
, h
, rel
);
2788 if (stub_name
== NULL
)
2791 sh
= (struct spu_stub_hash_entry
*)
2792 bfd_hash_lookup (&htab
->stub_hash_table
, stub_name
, FALSE
, FALSE
);
2795 relocation
= (htab
->stub
->output_section
->vma
2796 + htab
->stub
->output_offset
2803 r
= _bfd_final_link_relocate (howto
,
2807 rel
->r_offset
, relocation
, addend
);
2809 if (r
!= bfd_reloc_ok
)
2811 const char *msg
= (const char *) 0;
2815 case bfd_reloc_overflow
:
2816 if (!((*info
->callbacks
->reloc_overflow
)
2817 (info
, (h
? &h
->root
: NULL
), sym_name
, howto
->name
,
2818 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
)))
2822 case bfd_reloc_undefined
:
2823 if (!((*info
->callbacks
->undefined_symbol
)
2824 (info
, sym_name
, input_bfd
, input_section
,
2825 rel
->r_offset
, TRUE
)))
2829 case bfd_reloc_outofrange
:
2830 msg
= _("internal error: out of range error");
2833 case bfd_reloc_notsupported
:
2834 msg
= _("internal error: unsupported relocation error");
2837 case bfd_reloc_dangerous
:
2838 msg
= _("internal error: dangerous error");
2842 msg
= _("internal error: unknown error");
2846 if (!((*info
->callbacks
->warning
)
2847 (info
, msg
, sym_name
, input_bfd
, input_section
,
2856 && emit_these_relocs
2857 && !info
->relocatable
2858 && !info
->emitrelocations
)
2860 Elf_Internal_Rela
*wrel
;
2861 Elf_Internal_Shdr
*rel_hdr
;
2863 wrel
= rel
= relocs
;
2864 relend
= relocs
+ input_section
->reloc_count
;
2865 for (; rel
< relend
; rel
++)
2869 r_type
= ELF32_R_TYPE (rel
->r_info
);
2870 if (r_type
== R_SPU_PPU32
|| r_type
== R_SPU_PPU64
)
2873 input_section
->reloc_count
= wrel
- relocs
;
2874 /* Backflips for _bfd_elf_link_output_relocs. */
2875 rel_hdr
= &elf_section_data (input_section
)->rel_hdr
;
2876 rel_hdr
->sh_size
= input_section
->reloc_count
* rel_hdr
->sh_entsize
;
2883 /* Adjust _SPUEAR_ syms to point at their overlay stubs. */
2886 spu_elf_output_symbol_hook (struct bfd_link_info
*info
,
2887 const char *sym_name ATTRIBUTE_UNUSED
,
2888 Elf_Internal_Sym
*sym
,
2889 asection
*sym_sec ATTRIBUTE_UNUSED
,
2890 struct elf_link_hash_entry
*h
)
2892 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
2894 if (!info
->relocatable
2895 && htab
->num_overlays
!= 0
2897 && (h
->root
.type
== bfd_link_hash_defined
2898 || h
->root
.type
== bfd_link_hash_defweak
)
2900 && strncmp (h
->root
.root
.string
, "_SPUEAR_", 8) == 0)
2902 static Elf_Internal_Rela zero_rel
;
2903 char *stub_name
= spu_stub_name (h
->root
.u
.def
.section
, h
, &zero_rel
);
2904 struct spu_stub_hash_entry
*sh
;
2906 if (stub_name
== NULL
)
2908 sh
= (struct spu_stub_hash_entry
*)
2909 bfd_hash_lookup (&htab
->stub_hash_table
, stub_name
, FALSE
, FALSE
);
2914 = _bfd_elf_section_from_bfd_section (htab
->stub
->output_section
->owner
,
2915 htab
->stub
->output_section
);
2916 sym
->st_value
= (htab
->stub
->output_section
->vma
2917 + htab
->stub
->output_offset
2924 static int spu_plugin
= 0;
2927 spu_elf_plugin (int val
)
2932 /* Set ELF header e_type for plugins. */
2935 spu_elf_post_process_headers (bfd
*abfd
,
2936 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
2940 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
2942 i_ehdrp
->e_type
= ET_DYN
;
2946 /* We may add an extra PT_LOAD segment for .toe. We also need extra
2947 segments for overlays. */
2950 spu_elf_additional_program_headers (bfd
*abfd
, struct bfd_link_info
*info
)
2952 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
2953 int extra
= htab
->num_overlays
;
2959 sec
= bfd_get_section_by_name (abfd
, ".toe");
2960 if (sec
!= NULL
&& (sec
->flags
& SEC_LOAD
) != 0)
2966 /* Remove .toe section from other PT_LOAD segments and put it in
2967 a segment of its own. Put overlays in separate segments too. */
2970 spu_elf_modify_segment_map (bfd
*abfd
, struct bfd_link_info
*info
)
2973 struct elf_segment_map
*m
;
2979 toe
= bfd_get_section_by_name (abfd
, ".toe");
2980 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2981 if (m
->p_type
== PT_LOAD
&& m
->count
> 1)
2982 for (i
= 0; i
< m
->count
; i
++)
2983 if ((s
= m
->sections
[i
]) == toe
2984 || spu_elf_section_data (s
)->ovl_index
!= 0)
2986 struct elf_segment_map
*m2
;
2989 if (i
+ 1 < m
->count
)
2991 amt
= sizeof (struct elf_segment_map
);
2992 amt
+= (m
->count
- (i
+ 2)) * sizeof (m
->sections
[0]);
2993 m2
= bfd_zalloc (abfd
, amt
);
2996 m2
->count
= m
->count
- (i
+ 1);
2997 memcpy (m2
->sections
, m
->sections
+ i
+ 1,
2998 m2
->count
* sizeof (m
->sections
[0]));
2999 m2
->p_type
= PT_LOAD
;
3007 amt
= sizeof (struct elf_segment_map
);
3008 m2
= bfd_zalloc (abfd
, amt
);
3011 m2
->p_type
= PT_LOAD
;
3013 m2
->sections
[0] = s
;
3023 /* Check that all loadable section VMAs lie in the range
3024 LO .. HI inclusive. */
3027 spu_elf_check_vma (bfd
*abfd
, bfd_vma lo
, bfd_vma hi
)
3029 struct elf_segment_map
*m
;
3032 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3033 if (m
->p_type
== PT_LOAD
)
3034 for (i
= 0; i
< m
->count
; i
++)
3035 if (m
->sections
[i
]->size
!= 0
3036 && (m
->sections
[i
]->vma
< lo
3037 || m
->sections
[i
]->vma
> hi
3038 || m
->sections
[i
]->vma
+ m
->sections
[i
]->size
- 1 > hi
))
3039 return m
->sections
[i
];
3044 /* Tweak the section type of .note.spu_name. */
3047 spu_elf_fake_sections (bfd
*obfd ATTRIBUTE_UNUSED
,
3048 Elf_Internal_Shdr
*hdr
,
3051 if (strcmp (sec
->name
, SPU_PTNOTE_SPUNAME
) == 0)
3052 hdr
->sh_type
= SHT_NOTE
;
3056 /* Tweak phdrs before writing them out. */
3059 spu_elf_modify_program_headers (bfd
*abfd
, struct bfd_link_info
*info
)
3061 const struct elf_backend_data
*bed
;
3062 struct elf_obj_tdata
*tdata
;
3063 Elf_Internal_Phdr
*phdr
, *last
;
3064 struct spu_link_hash_table
*htab
;
3071 bed
= get_elf_backend_data (abfd
);
3072 tdata
= elf_tdata (abfd
);
3074 count
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
3075 htab
= spu_hash_table (info
);
3076 if (htab
->num_overlays
!= 0)
3078 struct elf_segment_map
*m
;
3081 for (i
= 0, m
= elf_tdata (abfd
)->segment_map
; m
; ++i
, m
= m
->next
)
3083 && (o
= spu_elf_section_data (m
->sections
[0])->ovl_index
) != 0)
3085 /* Mark this as an overlay header. */
3086 phdr
[i
].p_flags
|= PF_OVERLAY
;
3088 if (htab
->ovtab
!= NULL
&& htab
->ovtab
->size
!= 0)
3090 bfd_byte
*p
= htab
->ovtab
->contents
;
3091 unsigned int off
= (o
- 1) * 16 + 8;
3093 /* Write file_off into _ovly_table. */
3094 bfd_put_32 (htab
->ovtab
->owner
, phdr
[i
].p_offset
, p
+ off
);
3099 /* Round up p_filesz and p_memsz of PT_LOAD segments to multiples
3100 of 16. This should always be possible when using the standard
3101 linker scripts, but don't create overlapping segments if
3102 someone is playing games with linker scripts. */
3104 for (i
= count
; i
-- != 0; )
3105 if (phdr
[i
].p_type
== PT_LOAD
)
3109 adjust
= -phdr
[i
].p_filesz
& 15;
3112 && phdr
[i
].p_offset
+ phdr
[i
].p_filesz
> last
->p_offset
- adjust
)
3115 adjust
= -phdr
[i
].p_memsz
& 15;
3118 && phdr
[i
].p_filesz
!= 0
3119 && phdr
[i
].p_vaddr
+ phdr
[i
].p_memsz
> last
->p_vaddr
- adjust
3120 && phdr
[i
].p_vaddr
+ phdr
[i
].p_memsz
<= last
->p_vaddr
)
3123 if (phdr
[i
].p_filesz
!= 0)
3127 if (i
== (unsigned int) -1)
3128 for (i
= count
; i
-- != 0; )
3129 if (phdr
[i
].p_type
== PT_LOAD
)
3133 adjust
= -phdr
[i
].p_filesz
& 15;
3134 phdr
[i
].p_filesz
+= adjust
;
3136 adjust
= -phdr
[i
].p_memsz
& 15;
3137 phdr
[i
].p_memsz
+= adjust
;
3143 #define TARGET_BIG_SYM bfd_elf32_spu_vec
3144 #define TARGET_BIG_NAME "elf32-spu"
3145 #define ELF_ARCH bfd_arch_spu
3146 #define ELF_MACHINE_CODE EM_SPU
3147 /* This matches the alignment need for DMA. */
3148 #define ELF_MAXPAGESIZE 0x80
3149 #define elf_backend_rela_normal 1
3150 #define elf_backend_can_gc_sections 1
3152 #define bfd_elf32_bfd_reloc_type_lookup spu_elf_reloc_type_lookup
3153 #define bfd_elf32_bfd_reloc_name_lookup spu_elf_reloc_name_lookup
3154 #define elf_info_to_howto spu_elf_info_to_howto
3155 #define elf_backend_count_relocs spu_elf_count_relocs
3156 #define elf_backend_relocate_section spu_elf_relocate_section
3157 #define elf_backend_symbol_processing spu_elf_backend_symbol_processing
3158 #define elf_backend_link_output_symbol_hook spu_elf_output_symbol_hook
3159 #define bfd_elf32_new_section_hook spu_elf_new_section_hook
3160 #define bfd_elf32_bfd_link_hash_table_create spu_elf_link_hash_table_create
3161 #define bfd_elf32_bfd_link_hash_table_free spu_elf_link_hash_table_free
3163 #define elf_backend_additional_program_headers spu_elf_additional_program_headers
3164 #define elf_backend_modify_segment_map spu_elf_modify_segment_map
3165 #define elf_backend_modify_program_headers spu_elf_modify_program_headers
3166 #define elf_backend_post_process_headers spu_elf_post_process_headers
3167 #define elf_backend_fake_sections spu_elf_fake_sections
3168 #define elf_backend_special_sections spu_elf_special_sections
3169 #define bfd_elf32_bfd_final_link spu_elf_final_link
3171 #include "elf32-target.h"