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 2 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
),
83 static struct bfd_elf_special_section
const spu_elf_special_sections
[] = {
84 { ".toe", 4, 0, SHT_NOBITS
, SHF_ALLOC
},
88 static enum elf_spu_reloc_type
89 spu_elf_bfd_to_reloc_type (bfd_reloc_code_real_type code
)
95 case BFD_RELOC_SPU_IMM10W
:
97 case BFD_RELOC_SPU_IMM16W
:
99 case BFD_RELOC_SPU_LO16
:
100 return R_SPU_ADDR16_LO
;
101 case BFD_RELOC_SPU_HI16
:
102 return R_SPU_ADDR16_HI
;
103 case BFD_RELOC_SPU_IMM18
:
105 case BFD_RELOC_SPU_PCREL16
:
107 case BFD_RELOC_SPU_IMM7
:
109 case BFD_RELOC_SPU_IMM8
:
111 case BFD_RELOC_SPU_PCREL9a
:
113 case BFD_RELOC_SPU_PCREL9b
:
115 case BFD_RELOC_SPU_IMM10
:
116 return R_SPU_ADDR10I
;
117 case BFD_RELOC_SPU_IMM16
:
118 return R_SPU_ADDR16I
;
121 case BFD_RELOC_32_PCREL
:
127 spu_elf_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
129 Elf_Internal_Rela
*dst
)
131 enum elf_spu_reloc_type r_type
;
133 r_type
= (enum elf_spu_reloc_type
) ELF32_R_TYPE (dst
->r_info
);
134 BFD_ASSERT (r_type
< R_SPU_max
);
135 cache_ptr
->howto
= &elf_howto_table
[(int) r_type
];
138 static reloc_howto_type
*
139 spu_elf_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
140 bfd_reloc_code_real_type code
)
142 return elf_howto_table
+ spu_elf_bfd_to_reloc_type (code
);
145 static reloc_howto_type
*
146 spu_elf_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
151 for (i
= 0; i
< sizeof (elf_howto_table
) / sizeof (elf_howto_table
[0]); i
++)
152 if (elf_howto_table
[i
].name
!= NULL
153 && strcasecmp (elf_howto_table
[i
].name
, r_name
) == 0)
154 return &elf_howto_table
[i
];
159 /* Apply R_SPU_REL9 and R_SPU_REL9I relocs. */
161 static bfd_reloc_status_type
162 spu_elf_rel9 (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol
,
163 void *data
, asection
*input_section
,
164 bfd
*output_bfd
, char **error_message
)
166 bfd_size_type octets
;
170 /* If this is a relocatable link (output_bfd test tells us), just
171 call the generic function. Any adjustment will be done at final
173 if (output_bfd
!= NULL
)
174 return bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
175 input_section
, output_bfd
, error_message
);
177 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
178 return bfd_reloc_outofrange
;
179 octets
= reloc_entry
->address
* bfd_octets_per_byte (abfd
);
181 /* Get symbol value. */
183 if (!bfd_is_com_section (symbol
->section
))
185 if (symbol
->section
->output_section
)
186 val
+= symbol
->section
->output_section
->vma
;
188 val
+= reloc_entry
->addend
;
190 /* Make it pc-relative. */
191 val
-= input_section
->output_section
->vma
+ input_section
->output_offset
;
194 if (val
+ 256 >= 512)
195 return bfd_reloc_overflow
;
197 insn
= bfd_get_32 (abfd
, (bfd_byte
*) data
+ octets
);
199 /* Move two high bits of value to REL9I and REL9 position.
200 The mask will take care of selecting the right field. */
201 val
= (val
& 0x7f) | ((val
& 0x180) << 7) | ((val
& 0x180) << 16);
202 insn
&= ~reloc_entry
->howto
->dst_mask
;
203 insn
|= val
& reloc_entry
->howto
->dst_mask
;
204 bfd_put_32 (abfd
, insn
, (bfd_byte
*) data
+ octets
);
209 spu_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
211 if (!sec
->used_by_bfd
)
213 struct _spu_elf_section_data
*sdata
;
215 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
218 sec
->used_by_bfd
= sdata
;
221 return _bfd_elf_new_section_hook (abfd
, sec
);
224 /* Specially mark defined symbols named _EAR_* with BSF_KEEP so that
225 strip --strip-unneeded will not remove them. */
228 spu_elf_backend_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
, asymbol
*sym
)
230 if (sym
->name
!= NULL
231 && sym
->section
!= bfd_abs_section_ptr
232 && strncmp (sym
->name
, "_EAR_", 5) == 0)
233 sym
->flags
|= BSF_KEEP
;
236 /* SPU ELF linker hash table. */
238 struct spu_link_hash_table
240 struct elf_link_hash_table elf
;
242 /* The stub hash table. */
243 struct bfd_hash_table stub_hash_table
;
245 /* Shortcuts to overlay sections. */
249 struct elf_link_hash_entry
*ovly_load
;
251 /* An array of two output sections per overlay region, chosen such that
252 the first section vma is the overlay buffer vma (ie. the section has
253 the lowest vma in the group that occupy the region), and the second
254 section vma+size specifies the end of the region. We keep pointers
255 to sections like this because section vmas may change when laying
257 asection
**ovl_region
;
259 /* Number of overlay buffers. */
260 unsigned int num_buf
;
262 /* Total number of overlays. */
263 unsigned int num_overlays
;
265 /* Set if we should emit symbols for stubs. */
266 unsigned int emit_stub_syms
:1;
268 /* Set if we want stubs on calls out of overlay regions to
269 non-overlay regions. */
270 unsigned int non_overlay_stubs
: 1;
273 unsigned int stub_overflow
: 1;
276 #define spu_hash_table(p) \
277 ((struct spu_link_hash_table *) ((p)->hash))
279 struct spu_stub_hash_entry
281 struct bfd_hash_entry root
;
283 /* Destination of this stub. */
284 asection
*target_section
;
287 /* Offset of entry in stub section. */
290 /* Offset from this stub to stub that loads the overlay index. */
294 /* Create an entry in a spu stub hash table. */
296 static struct bfd_hash_entry
*
297 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
298 struct bfd_hash_table
*table
,
301 /* Allocate the structure if it has not already been allocated by a
305 entry
= bfd_hash_allocate (table
, sizeof (struct spu_stub_hash_entry
));
310 /* Call the allocation method of the superclass. */
311 entry
= bfd_hash_newfunc (entry
, table
, string
);
314 struct spu_stub_hash_entry
*sh
= (struct spu_stub_hash_entry
*) entry
;
316 sh
->target_section
= NULL
;
325 /* Create a spu ELF linker hash table. */
327 static struct bfd_link_hash_table
*
328 spu_elf_link_hash_table_create (bfd
*abfd
)
330 struct spu_link_hash_table
*htab
;
332 htab
= bfd_malloc (sizeof (*htab
));
336 if (!_bfd_elf_link_hash_table_init (&htab
->elf
, abfd
,
337 _bfd_elf_link_hash_newfunc
,
338 sizeof (struct elf_link_hash_entry
)))
344 /* Init the stub hash table too. */
345 if (!bfd_hash_table_init (&htab
->stub_hash_table
, stub_hash_newfunc
,
346 sizeof (struct spu_stub_hash_entry
)))
349 memset (&htab
->stub
, 0,
350 sizeof (*htab
) - offsetof (struct spu_link_hash_table
, stub
));
352 return &htab
->elf
.root
;
355 /* Free the derived linker hash table. */
358 spu_elf_link_hash_table_free (struct bfd_link_hash_table
*hash
)
360 struct spu_link_hash_table
*ret
= (struct spu_link_hash_table
*) hash
;
362 bfd_hash_table_free (&ret
->stub_hash_table
);
363 _bfd_generic_link_hash_table_free (hash
);
366 /* Find the symbol for the given R_SYMNDX in IBFD and set *HP and *SYMP
367 to (hash, NULL) for global symbols, and (NULL, sym) for locals. Set
368 *SYMSECP to the symbol's section. *LOCSYMSP caches local syms. */
371 get_sym_h (struct elf_link_hash_entry
**hp
,
372 Elf_Internal_Sym
**symp
,
374 Elf_Internal_Sym
**locsymsp
,
375 unsigned long r_symndx
,
378 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
380 if (r_symndx
>= symtab_hdr
->sh_info
)
382 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (ibfd
);
383 struct elf_link_hash_entry
*h
;
385 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
386 while (h
->root
.type
== bfd_link_hash_indirect
387 || h
->root
.type
== bfd_link_hash_warning
)
388 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
398 asection
*symsec
= NULL
;
399 if (h
->root
.type
== bfd_link_hash_defined
400 || h
->root
.type
== bfd_link_hash_defweak
)
401 symsec
= h
->root
.u
.def
.section
;
407 Elf_Internal_Sym
*sym
;
408 Elf_Internal_Sym
*locsyms
= *locsymsp
;
412 locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
414 locsyms
= bfd_elf_get_elf_syms (ibfd
, symtab_hdr
,
416 0, NULL
, NULL
, NULL
);
421 sym
= locsyms
+ r_symndx
;
431 asection
*symsec
= NULL
;
432 if ((sym
->st_shndx
!= SHN_UNDEF
433 && sym
->st_shndx
< SHN_LORESERVE
)
434 || sym
->st_shndx
> SHN_HIRESERVE
)
435 symsec
= bfd_section_from_elf_index (ibfd
, sym
->st_shndx
);
442 /* Build a name for an entry in the stub hash table. We can't use a
443 local symbol name because ld -r might generate duplicate local symbols. */
446 spu_stub_name (const asection
*sym_sec
,
447 const struct elf_link_hash_entry
*h
,
448 const Elf_Internal_Rela
*rel
)
455 len
= strlen (h
->root
.root
.string
) + 1 + 8 + 1;
456 stub_name
= bfd_malloc (len
);
457 if (stub_name
== NULL
)
460 sprintf (stub_name
, "%s+%x",
462 (int) rel
->r_addend
& 0xffffffff);
467 len
= 8 + 1 + 8 + 1 + 8 + 1;
468 stub_name
= bfd_malloc (len
);
469 if (stub_name
== NULL
)
472 sprintf (stub_name
, "%x:%x+%x",
473 sym_sec
->id
& 0xffffffff,
474 (int) ELF32_R_SYM (rel
->r_info
) & 0xffffffff,
475 (int) rel
->r_addend
& 0xffffffff);
476 len
= strlen (stub_name
);
479 if (stub_name
[len
- 2] == '+'
480 && stub_name
[len
- 1] == '0'
481 && stub_name
[len
] == 0)
482 stub_name
[len
- 2] = 0;
487 /* Create the note section if not already present. This is done early so
488 that the linker maps the sections to the right place in the output. */
491 spu_elf_create_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
495 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->next
)
496 if (bfd_get_section_by_name (ibfd
, SPU_PTNOTE_SPUNAME
) != NULL
)
501 /* Make SPU_PTNOTE_SPUNAME section. */
508 ibfd
= info
->input_bfds
;
509 flags
= SEC_LOAD
| SEC_READONLY
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
510 s
= bfd_make_section_anyway_with_flags (ibfd
, SPU_PTNOTE_SPUNAME
, flags
);
512 || !bfd_set_section_alignment (ibfd
, s
, 4))
515 name_len
= strlen (bfd_get_filename (output_bfd
)) + 1;
516 size
= 12 + ((sizeof (SPU_PLUGIN_NAME
) + 3) & -4);
517 size
+= (name_len
+ 3) & -4;
519 if (!bfd_set_section_size (ibfd
, s
, size
))
522 data
= bfd_zalloc (ibfd
, size
);
526 bfd_put_32 (ibfd
, sizeof (SPU_PLUGIN_NAME
), data
+ 0);
527 bfd_put_32 (ibfd
, name_len
, data
+ 4);
528 bfd_put_32 (ibfd
, 1, data
+ 8);
529 memcpy (data
+ 12, SPU_PLUGIN_NAME
, sizeof (SPU_PLUGIN_NAME
));
530 memcpy (data
+ 12 + ((sizeof (SPU_PLUGIN_NAME
) + 3) & -4),
531 bfd_get_filename (output_bfd
), name_len
);
538 /* Return the section that should be marked against GC for a given
542 spu_elf_gc_mark_hook (asection
*sec
,
543 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
544 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
545 struct elf_link_hash_entry
*h
,
546 Elf_Internal_Sym
*sym
)
550 switch (h
->root
.type
)
552 case bfd_link_hash_defined
:
553 case bfd_link_hash_defweak
:
554 return h
->root
.u
.def
.section
;
556 case bfd_link_hash_common
:
557 return h
->root
.u
.c
.p
->section
;
564 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
569 /* qsort predicate to sort sections by vma. */
572 sort_sections (const void *a
, const void *b
)
574 const asection
*const *s1
= a
;
575 const asection
*const *s2
= b
;
576 bfd_signed_vma delta
= (*s1
)->vma
- (*s2
)->vma
;
579 return delta
< 0 ? -1 : 1;
581 return (*s1
)->index
- (*s2
)->index
;
584 /* Identify overlays in the output bfd, and number them. */
587 spu_elf_find_overlays (bfd
*output_bfd
, struct bfd_link_info
*info
)
589 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
590 asection
**alloc_sec
;
591 unsigned int i
, n
, ovl_index
, num_buf
;
595 if (output_bfd
->section_count
< 2)
598 alloc_sec
= bfd_malloc (output_bfd
->section_count
* sizeof (*alloc_sec
));
599 if (alloc_sec
== NULL
)
602 /* Pick out all the alloced sections. */
603 for (n
= 0, s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
604 if ((s
->flags
& SEC_ALLOC
) != 0
605 && (s
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != SEC_THREAD_LOCAL
615 /* Sort them by vma. */
616 qsort (alloc_sec
, n
, sizeof (*alloc_sec
), sort_sections
);
618 /* Look for overlapping vmas. Any with overlap must be overlays.
619 Count them. Also count the number of overlay regions and for
620 each region save a section from that region with the lowest vma
621 and another section with the highest end vma. */
622 ovl_end
= alloc_sec
[0]->vma
+ alloc_sec
[0]->size
;
623 for (ovl_index
= 0, num_buf
= 0, i
= 1; i
< n
; i
++)
626 if (s
->vma
< ovl_end
)
628 asection
*s0
= alloc_sec
[i
- 1];
630 if (spu_elf_section_data (s0
)->ovl_index
== 0)
632 spu_elf_section_data (s0
)->ovl_index
= ++ovl_index
;
633 alloc_sec
[num_buf
* 2] = s0
;
634 alloc_sec
[num_buf
* 2 + 1] = s0
;
637 spu_elf_section_data (s
)->ovl_index
= ++ovl_index
;
638 if (ovl_end
< s
->vma
+ s
->size
)
640 ovl_end
= s
->vma
+ s
->size
;
641 alloc_sec
[num_buf
* 2 - 1] = s
;
645 ovl_end
= s
->vma
+ s
->size
;
648 htab
->num_overlays
= ovl_index
;
649 htab
->num_buf
= num_buf
;
656 alloc_sec
= bfd_realloc (alloc_sec
, num_buf
* 2 * sizeof (*alloc_sec
));
657 if (alloc_sec
== NULL
)
660 htab
->ovl_region
= alloc_sec
;
664 /* One of these per stub. */
665 #define SIZEOF_STUB1 8
666 #define ILA_79 0x4200004f /* ila $79,function_address */
667 #define BR 0x32000000 /* br stub2 */
669 /* One of these per overlay. */
670 #define SIZEOF_STUB2 8
671 #define ILA_78 0x4200004e /* ila $78,overlay_number */
673 #define NOP 0x40200000
675 /* Return true for all relative and absolute branch and hint instructions.
688 is_branch (const unsigned char *insn
)
690 return (((insn
[0] & 0xec) == 0x20 && (insn
[1] & 0x80) == 0)
691 || (insn
[0] & 0xfc) == 0x10);
694 /* Return TRUE if this reloc symbol should possibly go via an overlay stub. */
697 needs_ovl_stub (const char *sym_name
,
699 asection
*input_section
,
700 struct spu_link_hash_table
*htab
,
701 bfd_boolean is_branch
)
703 if (htab
->num_overlays
== 0)
707 || sym_sec
->output_section
== NULL
)
710 /* setjmp always goes via an overlay stub, because then the return
711 and hence the longjmp goes via __ovly_return. That magically
712 makes setjmp/longjmp between overlays work. */
713 if (strncmp (sym_name
, "setjmp", 6) == 0
714 && (sym_name
[6] == '\0' || sym_name
[6] == '@'))
717 /* Usually, symbols in non-overlay sections don't need stubs. */
718 if (spu_elf_section_data (sym_sec
->output_section
)->ovl_index
== 0
719 && !htab
->non_overlay_stubs
)
722 /* A reference from some other section to a symbol in an overlay
723 section needs a stub. */
724 if (spu_elf_section_data (sym_sec
->output_section
)->ovl_index
725 != spu_elf_section_data (input_section
->output_section
)->ovl_index
)
728 /* If this insn isn't a branch then we are possibly taking the
729 address of a function and passing it out somehow. */
734 struct bfd_hash_table
*stub_hash_table
;
735 struct spu_stub_hash_entry
**sh
;
740 /* Called via elf_link_hash_traverse to allocate stubs for any _SPUEAR_
744 allocate_spuear_stubs (struct elf_link_hash_entry
*h
, void *inf
)
746 /* Symbols starting with _SPUEAR_ need a stub because they may be
747 invoked by the PPU. */
748 if ((h
->root
.type
== bfd_link_hash_defined
749 || h
->root
.type
== bfd_link_hash_defweak
)
751 && strncmp (h
->root
.root
.string
, "_SPUEAR_", 8) == 0)
753 struct stubarr
*stubs
= inf
;
754 static Elf_Internal_Rela zero_rel
;
755 char *stub_name
= spu_stub_name (h
->root
.u
.def
.section
, h
, &zero_rel
);
756 struct spu_stub_hash_entry
*sh
;
758 if (stub_name
== NULL
)
764 sh
= (struct spu_stub_hash_entry
*)
765 bfd_hash_lookup (stubs
->stub_hash_table
, stub_name
, TRUE
, FALSE
);
772 /* If this entry isn't new, we already have a stub. */
773 if (sh
->target_section
!= NULL
)
779 sh
->target_section
= h
->root
.u
.def
.section
;
780 sh
->target_off
= h
->root
.u
.def
.value
;
787 /* Called via bfd_hash_traverse to set up pointers to all symbols
788 in the stub hash table. */
791 populate_stubs (struct bfd_hash_entry
*bh
, void *inf
)
793 struct stubarr
*stubs
= inf
;
795 stubs
->sh
[--stubs
->count
] = (struct spu_stub_hash_entry
*) bh
;
799 /* qsort predicate to sort stubs by overlay number. */
802 sort_stubs (const void *a
, const void *b
)
804 const struct spu_stub_hash_entry
*const *sa
= a
;
805 const struct spu_stub_hash_entry
*const *sb
= b
;
809 i
= spu_elf_section_data ((*sa
)->target_section
->output_section
)->ovl_index
;
810 i
-= spu_elf_section_data ((*sb
)->target_section
->output_section
)->ovl_index
;
814 d
= ((*sa
)->target_section
->output_section
->vma
815 + (*sa
)->target_section
->output_offset
817 - (*sb
)->target_section
->output_section
->vma
818 - (*sb
)->target_section
->output_offset
819 - (*sb
)->target_off
);
821 return d
< 0 ? -1 : 1;
823 /* Two functions at the same address. Aliases perhaps. */
824 i
= strcmp ((*sb
)->root
.string
, (*sa
)->root
.string
);
829 /* Allocate space for overlay call and return stubs. */
832 spu_elf_size_stubs (bfd
*output_bfd
,
833 struct bfd_link_info
*info
,
834 int non_overlay_stubs
,
839 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
841 struct stubarr stubs
;
845 htab
->non_overlay_stubs
= non_overlay_stubs
;
846 stubs
.stub_hash_table
= &htab
->stub_hash_table
;
849 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
851 extern const bfd_target bfd_elf32_spu_vec
;
852 Elf_Internal_Shdr
*symtab_hdr
;
854 Elf_Internal_Sym
*local_syms
= NULL
;
856 if (ibfd
->xvec
!= &bfd_elf32_spu_vec
)
859 /* We'll need the symbol table in a second. */
860 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
861 if (symtab_hdr
->sh_info
== 0)
864 /* Walk over each section attached to the input bfd. */
865 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
867 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
869 /* If there aren't any relocs, then there's nothing more to do. */
870 if ((section
->flags
& SEC_RELOC
) == 0
871 || (section
->flags
& SEC_ALLOC
) == 0
872 || (section
->flags
& SEC_LOAD
) == 0
873 || section
->reloc_count
== 0)
876 /* If this section is a link-once section that will be
877 discarded, then don't create any stubs. */
878 if (section
->output_section
== NULL
879 || section
->output_section
->owner
!= output_bfd
)
882 /* Get the relocs. */
884 = _bfd_elf_link_read_relocs (ibfd
, section
, NULL
, NULL
,
886 if (internal_relocs
== NULL
)
887 goto error_ret_free_local
;
889 /* Now examine each relocation. */
890 irela
= internal_relocs
;
891 irelaend
= irela
+ section
->reloc_count
;
892 for (; irela
< irelaend
; irela
++)
894 enum elf_spu_reloc_type r_type
;
897 Elf_Internal_Sym
*sym
;
898 struct elf_link_hash_entry
*h
;
899 const char *sym_name
;
901 struct spu_stub_hash_entry
*sh
;
902 unsigned int sym_type
;
903 enum _insn_type
{ non_branch
, branch
, call
} insn_type
;
905 r_type
= ELF32_R_TYPE (irela
->r_info
);
906 r_indx
= ELF32_R_SYM (irela
->r_info
);
908 if (r_type
>= R_SPU_max
)
910 bfd_set_error (bfd_error_bad_value
);
911 goto error_ret_free_internal
;
914 /* Determine the reloc target section. */
915 if (!get_sym_h (&h
, &sym
, &sym_sec
, &local_syms
, r_indx
, ibfd
))
916 goto error_ret_free_internal
;
919 || sym_sec
->output_section
== NULL
920 || sym_sec
->output_section
->owner
!= output_bfd
)
923 /* Ensure no stubs for user supplied overlay manager syms. */
925 && (strcmp (h
->root
.root
.string
, "__ovly_load") == 0
926 || strcmp (h
->root
.root
.string
, "__ovly_return") == 0))
929 insn_type
= non_branch
;
930 if (r_type
== R_SPU_REL16
931 || r_type
== R_SPU_ADDR16
)
933 unsigned char insn
[4];
935 if (!bfd_get_section_contents (ibfd
, section
, insn
,
937 goto error_ret_free_internal
;
939 if (is_branch (insn
))
942 if ((insn
[0] & 0xfd) == 0x31)
947 /* We are only interested in function symbols. */
951 sym_name
= h
->root
.root
.string
;
955 sym_type
= ELF_ST_TYPE (sym
->st_info
);
956 sym_name
= bfd_elf_sym_name (sym_sec
->owner
,
961 if (sym_type
!= STT_FUNC
)
963 /* It's common for people to write assembly and forget
964 to give function symbols the right type. Handle
965 calls to such symbols, but warn so that (hopefully)
966 people will fix their code. We need the symbol
967 type to be correct to distinguish function pointer
968 initialisation from other pointer initialisation. */
969 if (insn_type
== call
)
970 (*_bfd_error_handler
) (_("warning: call to non-function"
971 " symbol %s defined in %B"),
972 sym_sec
->owner
, sym_name
);
977 if (!needs_ovl_stub (sym_name
, sym_sec
, section
, htab
,
978 insn_type
!= non_branch
))
981 stub_name
= spu_stub_name (sym_sec
, h
, irela
);
982 if (stub_name
== NULL
)
983 goto error_ret_free_internal
;
985 sh
= (struct spu_stub_hash_entry
*)
986 bfd_hash_lookup (&htab
->stub_hash_table
, stub_name
,
991 error_ret_free_internal
:
992 if (elf_section_data (section
)->relocs
!= internal_relocs
)
993 free (internal_relocs
);
994 error_ret_free_local
:
995 if (local_syms
!= NULL
996 && (symtab_hdr
->contents
997 != (unsigned char *) local_syms
))
1002 /* If this entry isn't new, we already have a stub. */
1003 if (sh
->target_section
!= NULL
)
1009 sh
->target_section
= sym_sec
;
1011 sh
->target_off
= h
->root
.u
.def
.value
;
1013 sh
->target_off
= sym
->st_value
;
1014 sh
->target_off
+= irela
->r_addend
;
1019 /* We're done with the internal relocs, free them. */
1020 if (elf_section_data (section
)->relocs
!= internal_relocs
)
1021 free (internal_relocs
);
1024 if (local_syms
!= NULL
1025 && symtab_hdr
->contents
!= (unsigned char *) local_syms
)
1027 if (!info
->keep_memory
)
1030 symtab_hdr
->contents
= (unsigned char *) local_syms
;
1034 elf_link_hash_traverse (&htab
->elf
, allocate_spuear_stubs
, &stubs
);
1039 if (stubs
.count
== 0)
1042 ibfd
= info
->input_bfds
;
1043 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_READONLY
1044 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
);
1045 htab
->stub
= bfd_make_section_anyway_with_flags (ibfd
, ".stub", flags
);
1047 if (htab
->stub
== NULL
1048 || !bfd_set_section_alignment (ibfd
, htab
->stub
, 2))
1051 flags
= (SEC_ALLOC
| SEC_LOAD
1052 | SEC_HAS_CONTENTS
| SEC_IN_MEMORY
);
1053 htab
->ovtab
= bfd_make_section_anyway_with_flags (ibfd
, ".ovtab", flags
);
1054 *ovtab
= htab
->ovtab
;
1055 if (htab
->ovtab
== NULL
1056 || !bfd_set_section_alignment (ibfd
, htab
->stub
, 4))
1059 *toe
= bfd_make_section_anyway_with_flags (ibfd
, ".toe", SEC_ALLOC
);
1061 || !bfd_set_section_alignment (ibfd
, *toe
, 4))
1065 /* Retrieve all the stubs and sort. */
1066 stubs
.sh
= bfd_malloc (stubs
.count
* sizeof (*stubs
.sh
));
1067 if (stubs
.sh
== NULL
)
1070 bfd_hash_traverse (&htab
->stub_hash_table
, populate_stubs
, &stubs
);
1071 BFD_ASSERT (stubs
.count
== 0);
1074 qsort (stubs
.sh
, stubs
.count
, sizeof (*stubs
.sh
), sort_stubs
);
1076 /* Now that the stubs are sorted, place them in the stub section.
1077 Stubs are grouped per overlay
1091 for (i
= 0; i
< stubs
.count
; i
++)
1093 if (spu_elf_section_data (stubs
.sh
[group
]->target_section
1094 ->output_section
)->ovl_index
1095 != spu_elf_section_data (stubs
.sh
[i
]->target_section
1096 ->output_section
)->ovl_index
)
1098 htab
->stub
->size
+= SIZEOF_STUB2
;
1099 for (; group
!= i
; group
++)
1100 stubs
.sh
[group
]->delta
1101 = stubs
.sh
[i
- 1]->off
- stubs
.sh
[group
]->off
;
1104 || ((stubs
.sh
[i
- 1]->target_section
->output_section
->vma
1105 + stubs
.sh
[i
- 1]->target_section
->output_offset
1106 + stubs
.sh
[i
- 1]->target_off
)
1107 != (stubs
.sh
[i
]->target_section
->output_section
->vma
1108 + stubs
.sh
[i
]->target_section
->output_offset
1109 + stubs
.sh
[i
]->target_off
)))
1111 stubs
.sh
[i
]->off
= htab
->stub
->size
;
1112 htab
->stub
->size
+= SIZEOF_STUB1
;
1115 stubs
.sh
[i
]->off
= stubs
.sh
[i
- 1]->off
;
1118 htab
->stub
->size
+= SIZEOF_STUB2
;
1119 for (; group
!= i
; group
++)
1120 stubs
.sh
[group
]->delta
= stubs
.sh
[i
- 1]->off
- stubs
.sh
[group
]->off
;
1122 /* htab->ovtab consists of two arrays.
1132 . } _ovly_buf_table[]; */
1134 htab
->ovtab
->alignment_power
= 4;
1135 htab
->ovtab
->size
= htab
->num_overlays
* 16 + htab
->num_buf
* 4;
1140 /* Functions to handle embedded spu_ovl.o object. */
1143 ovl_mgr_open (struct bfd
*nbfd ATTRIBUTE_UNUSED
, void *stream
)
1149 ovl_mgr_pread (struct bfd
*abfd ATTRIBUTE_UNUSED
,
1155 struct _ovl_stream
*os
;
1159 os
= (struct _ovl_stream
*) stream
;
1160 max
= (const char *) os
->end
- (const char *) os
->start
;
1162 if ((ufile_ptr
) offset
>= max
)
1166 if (count
> max
- offset
)
1167 count
= max
- offset
;
1169 memcpy (buf
, (const char *) os
->start
+ offset
, count
);
1174 spu_elf_open_builtin_lib (bfd
**ovl_bfd
, const struct _ovl_stream
*stream
)
1176 *ovl_bfd
= bfd_openr_iovec ("builtin ovl_mgr",
1183 return *ovl_bfd
!= NULL
;
1186 /* Fill in the ila and br for a stub. On the last stub for a group,
1187 write the stub that sets the overlay number too. */
1190 write_one_stub (struct bfd_hash_entry
*bh
, void *inf
)
1192 struct spu_stub_hash_entry
*ent
= (struct spu_stub_hash_entry
*) bh
;
1193 struct spu_link_hash_table
*htab
= inf
;
1194 asection
*sec
= htab
->stub
;
1195 asection
*s
= ent
->target_section
;
1199 val
= ent
->target_off
+ s
->output_offset
+ s
->output_section
->vma
;
1200 bfd_put_32 (sec
->owner
, ILA_79
+ ((val
<< 7) & 0x01ffff80),
1201 sec
->contents
+ ent
->off
);
1202 val
= ent
->delta
+ 4;
1203 bfd_put_32 (sec
->owner
, BR
+ ((val
<< 5) & 0x007fff80),
1204 sec
->contents
+ ent
->off
+ 4);
1206 /* If this is the last stub of this group, write stub2. */
1207 if (ent
->delta
== 0)
1209 bfd_put_32 (sec
->owner
, NOP
,
1210 sec
->contents
+ ent
->off
+ 4);
1212 ovl
= spu_elf_section_data (s
->output_section
)->ovl_index
;
1213 bfd_put_32 (sec
->owner
, ILA_78
+ ((ovl
<< 7) & 0x01ffff80),
1214 sec
->contents
+ ent
->off
+ 8);
1216 val
= (htab
->ovly_load
->root
.u
.def
.section
->output_section
->vma
1217 + htab
->ovly_load
->root
.u
.def
.section
->output_offset
1218 + htab
->ovly_load
->root
.u
.def
.value
1219 - (sec
->output_section
->vma
1220 + sec
->output_offset
1223 if (val
+ 0x20000 >= 0x40000)
1224 htab
->stub_overflow
= TRUE
;
1226 bfd_put_32 (sec
->owner
, BR
+ ((val
<< 5) & 0x007fff80),
1227 sec
->contents
+ ent
->off
+ 12);
1230 if (htab
->emit_stub_syms
)
1232 struct elf_link_hash_entry
*h
;
1236 len1
= sizeof ("00000000.ovl_call.") - 1;
1237 len2
= strlen (ent
->root
.string
);
1238 name
= bfd_malloc (len1
+ len2
+ 1);
1241 memcpy (name
, "00000000.ovl_call.", len1
);
1242 memcpy (name
+ len1
, ent
->root
.string
, len2
+ 1);
1243 h
= elf_link_hash_lookup (&htab
->elf
, name
, TRUE
, FALSE
, FALSE
);
1246 if (h
->root
.type
== bfd_link_hash_new
)
1248 h
->root
.type
= bfd_link_hash_defined
;
1249 h
->root
.u
.def
.section
= sec
;
1250 h
->root
.u
.def
.value
= ent
->off
;
1251 h
->size
= (ent
->delta
== 0
1252 ? SIZEOF_STUB1
+ SIZEOF_STUB2
: SIZEOF_STUB1
);
1256 h
->ref_regular_nonweak
= 1;
1257 h
->forced_local
= 1;
1265 /* Define an STT_OBJECT symbol. */
1267 static struct elf_link_hash_entry
*
1268 define_ovtab_symbol (struct spu_link_hash_table
*htab
, const char *name
)
1270 struct elf_link_hash_entry
*h
;
1272 h
= elf_link_hash_lookup (&htab
->elf
, name
, TRUE
, FALSE
, FALSE
);
1276 if (h
->root
.type
!= bfd_link_hash_defined
1279 h
->root
.type
= bfd_link_hash_defined
;
1280 h
->root
.u
.def
.section
= htab
->ovtab
;
1281 h
->type
= STT_OBJECT
;
1284 h
->ref_regular_nonweak
= 1;
1289 (*_bfd_error_handler
) (_("%B is not allowed to define %s"),
1290 h
->root
.u
.def
.section
->owner
,
1291 h
->root
.root
.string
);
1292 bfd_set_error (bfd_error_bad_value
);
1299 /* Fill in all stubs and the overlay tables. */
1302 spu_elf_build_stubs (struct bfd_link_info
*info
, int emit_syms
, asection
*toe
)
1304 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
1305 struct elf_link_hash_entry
*h
;
1311 htab
->emit_stub_syms
= emit_syms
;
1312 htab
->stub
->contents
= bfd_zalloc (htab
->stub
->owner
, htab
->stub
->size
);
1313 if (htab
->stub
->contents
== NULL
)
1316 h
= elf_link_hash_lookup (&htab
->elf
, "__ovly_load", FALSE
, FALSE
, FALSE
);
1317 htab
->ovly_load
= h
;
1318 BFD_ASSERT (h
!= NULL
1319 && (h
->root
.type
== bfd_link_hash_defined
1320 || h
->root
.type
== bfd_link_hash_defweak
)
1323 s
= h
->root
.u
.def
.section
->output_section
;
1324 if (spu_elf_section_data (s
)->ovl_index
)
1326 (*_bfd_error_handler
) (_("%s in overlay section"),
1327 h
->root
.u
.def
.section
->owner
);
1328 bfd_set_error (bfd_error_bad_value
);
1332 /* Write out all the stubs. */
1333 bfd_hash_traverse (&htab
->stub_hash_table
, write_one_stub
, htab
);
1335 if (htab
->stub_overflow
)
1337 (*_bfd_error_handler
) (_("overlay stub relocation overflow"));
1338 bfd_set_error (bfd_error_bad_value
);
1342 htab
->ovtab
->contents
= bfd_zalloc (htab
->ovtab
->owner
, htab
->ovtab
->size
);
1343 if (htab
->ovtab
->contents
== NULL
)
1346 /* Write out _ovly_table. */
1347 p
= htab
->ovtab
->contents
;
1348 obfd
= htab
->ovtab
->output_section
->owner
;
1349 for (s
= obfd
->sections
; s
!= NULL
; s
= s
->next
)
1351 unsigned int ovl_index
= spu_elf_section_data (s
)->ovl_index
;
1355 unsigned int lo
, hi
, mid
;
1356 unsigned long off
= (ovl_index
- 1) * 16;
1357 bfd_put_32 (htab
->ovtab
->owner
, s
->vma
, p
+ off
);
1358 bfd_put_32 (htab
->ovtab
->owner
, (s
->size
+ 15) & -16, p
+ off
+ 4);
1359 /* file_off written later in spu_elf_modify_program_headers. */
1365 mid
= (lo
+ hi
) >> 1;
1366 if (htab
->ovl_region
[2 * mid
+ 1]->vma
1367 + htab
->ovl_region
[2 * mid
+ 1]->size
<= s
->vma
)
1369 else if (htab
->ovl_region
[2 * mid
]->vma
> s
->vma
)
1373 bfd_put_32 (htab
->ovtab
->owner
, mid
+ 1, p
+ off
+ 12);
1377 BFD_ASSERT (lo
< hi
);
1381 /* Write out _ovly_buf_table. */
1382 p
= htab
->ovtab
->contents
+ htab
->num_overlays
* 16;
1383 for (i
= 0; i
< htab
->num_buf
; i
++)
1385 bfd_put_32 (htab
->ovtab
->owner
, 0, p
);
1389 h
= define_ovtab_symbol (htab
, "_ovly_table");
1392 h
->root
.u
.def
.value
= 0;
1393 h
->size
= htab
->num_overlays
* 16;
1395 h
= define_ovtab_symbol (htab
, "_ovly_table_end");
1398 h
->root
.u
.def
.value
= htab
->num_overlays
* 16;
1401 h
= define_ovtab_symbol (htab
, "_ovly_buf_table");
1404 h
->root
.u
.def
.value
= htab
->num_overlays
* 16;
1405 h
->size
= htab
->num_buf
* 4;
1407 h
= define_ovtab_symbol (htab
, "_ovly_buf_table_end");
1410 h
->root
.u
.def
.value
= htab
->num_overlays
* 16 + htab
->num_buf
* 4;
1413 h
= define_ovtab_symbol (htab
, "_EAR_");
1416 h
->root
.u
.def
.section
= toe
;
1417 h
->root
.u
.def
.value
= 0;
1423 /* Apply RELOCS to CONTENTS of INPUT_SECTION from INPUT_BFD. */
1426 spu_elf_relocate_section (bfd
*output_bfd
,
1427 struct bfd_link_info
*info
,
1429 asection
*input_section
,
1431 Elf_Internal_Rela
*relocs
,
1432 Elf_Internal_Sym
*local_syms
,
1433 asection
**local_sections
)
1435 Elf_Internal_Shdr
*symtab_hdr
;
1436 struct elf_link_hash_entry
**sym_hashes
;
1437 Elf_Internal_Rela
*rel
, *relend
;
1438 struct spu_link_hash_table
*htab
;
1439 bfd_boolean ret
= TRUE
;
1441 htab
= spu_hash_table (info
);
1442 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1443 sym_hashes
= (struct elf_link_hash_entry
**) (elf_sym_hashes (input_bfd
));
1446 relend
= relocs
+ input_section
->reloc_count
;
1447 for (; rel
< relend
; rel
++)
1450 reloc_howto_type
*howto
;
1451 unsigned long r_symndx
;
1452 Elf_Internal_Sym
*sym
;
1454 struct elf_link_hash_entry
*h
;
1455 const char *sym_name
;
1458 bfd_reloc_status_type r
;
1459 bfd_boolean unresolved_reloc
;
1462 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1463 r_type
= ELF32_R_TYPE (rel
->r_info
);
1464 howto
= elf_howto_table
+ r_type
;
1465 unresolved_reloc
= FALSE
;
1471 if (r_symndx
< symtab_hdr
->sh_info
)
1473 sym
= local_syms
+ r_symndx
;
1474 sec
= local_sections
[r_symndx
];
1475 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, sec
);
1476 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1480 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1481 r_symndx
, symtab_hdr
, sym_hashes
,
1483 unresolved_reloc
, warned
);
1484 sym_name
= h
->root
.root
.string
;
1487 if (sec
!= NULL
&& elf_discarded_section (sec
))
1489 /* For relocs against symbols from removed linkonce sections,
1490 or sections discarded by a linker script, we just want the
1491 section contents zeroed. Avoid any special processing. */
1492 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
1498 if (info
->relocatable
)
1501 if (unresolved_reloc
)
1503 (*_bfd_error_handler
)
1504 (_("%B(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
1506 bfd_get_section_name (input_bfd
, input_section
),
1507 (long) rel
->r_offset
,
1513 /* If this symbol is in an overlay area, we may need to relocate
1514 to the overlay stub. */
1515 addend
= rel
->r_addend
;
1516 if (needs_ovl_stub (sym_name
, sec
, input_section
, htab
,
1517 is_branch (contents
+ rel
->r_offset
)))
1520 struct spu_stub_hash_entry
*sh
;
1522 stub_name
= spu_stub_name (sec
, h
, rel
);
1523 if (stub_name
== NULL
)
1526 sh
= (struct spu_stub_hash_entry
*)
1527 bfd_hash_lookup (&htab
->stub_hash_table
, stub_name
, FALSE
, FALSE
);
1530 relocation
= (htab
->stub
->output_section
->vma
1531 + htab
->stub
->output_offset
1538 r
= _bfd_final_link_relocate (howto
,
1542 rel
->r_offset
, relocation
, addend
);
1544 if (r
!= bfd_reloc_ok
)
1546 const char *msg
= (const char *) 0;
1550 case bfd_reloc_overflow
:
1551 if (!((*info
->callbacks
->reloc_overflow
)
1552 (info
, (h
? &h
->root
: NULL
), sym_name
, howto
->name
,
1553 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
)))
1557 case bfd_reloc_undefined
:
1558 if (!((*info
->callbacks
->undefined_symbol
)
1559 (info
, sym_name
, input_bfd
, input_section
,
1560 rel
->r_offset
, TRUE
)))
1564 case bfd_reloc_outofrange
:
1565 msg
= _("internal error: out of range error");
1568 case bfd_reloc_notsupported
:
1569 msg
= _("internal error: unsupported relocation error");
1572 case bfd_reloc_dangerous
:
1573 msg
= _("internal error: dangerous error");
1577 msg
= _("internal error: unknown error");
1581 if (!((*info
->callbacks
->warning
)
1582 (info
, msg
, sym_name
, input_bfd
, input_section
,
1593 static int spu_plugin
= 0;
1596 spu_elf_plugin (int val
)
1601 /* Set ELF header e_type for plugins. */
1604 spu_elf_post_process_headers (bfd
*abfd
,
1605 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1609 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
1611 i_ehdrp
->e_type
= ET_DYN
;
1615 /* We may add an extra PT_LOAD segment for .toe. We also need extra
1616 segments for overlays. */
1619 spu_elf_additional_program_headers (bfd
*abfd
, struct bfd_link_info
*info
)
1621 struct spu_link_hash_table
*htab
= spu_hash_table (info
);
1622 int extra
= htab
->num_overlays
;
1628 sec
= bfd_get_section_by_name (abfd
, ".toe");
1629 if (sec
!= NULL
&& (sec
->flags
& SEC_LOAD
) != 0)
1635 /* Remove .toe section from other PT_LOAD segments and put it in
1636 a segment of its own. Put overlays in separate segments too. */
1639 spu_elf_modify_segment_map (bfd
*abfd
, struct bfd_link_info
*info
)
1642 struct elf_segment_map
*m
;
1648 toe
= bfd_get_section_by_name (abfd
, ".toe");
1649 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
1650 if (m
->p_type
== PT_LOAD
&& m
->count
> 1)
1651 for (i
= 0; i
< m
->count
; i
++)
1652 if ((s
= m
->sections
[i
]) == toe
1653 || spu_elf_section_data (s
)->ovl_index
!= 0)
1655 struct elf_segment_map
*m2
;
1658 if (i
+ 1 < m
->count
)
1660 amt
= sizeof (struct elf_segment_map
);
1661 amt
+= (m
->count
- (i
+ 2)) * sizeof (m
->sections
[0]);
1662 m2
= bfd_zalloc (abfd
, amt
);
1665 m2
->count
= m
->count
- (i
+ 1);
1666 memcpy (m2
->sections
, m
->sections
+ i
+ 1,
1667 m2
->count
* sizeof (m
->sections
[0]));
1668 m2
->p_type
= PT_LOAD
;
1676 amt
= sizeof (struct elf_segment_map
);
1677 m2
= bfd_zalloc (abfd
, amt
);
1680 m2
->p_type
= PT_LOAD
;
1682 m2
->sections
[0] = s
;
1692 /* Check that all loadable section VMAs lie in the range
1693 LO .. HI inclusive. */
1696 spu_elf_check_vma (bfd
*abfd
, bfd_vma lo
, bfd_vma hi
)
1698 struct elf_segment_map
*m
;
1701 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
1702 if (m
->p_type
== PT_LOAD
)
1703 for (i
= 0; i
< m
->count
; i
++)
1704 if (m
->sections
[i
]->size
!= 0
1705 && (m
->sections
[i
]->vma
< lo
1706 || m
->sections
[i
]->vma
> hi
1707 || m
->sections
[i
]->vma
+ m
->sections
[i
]->size
- 1 > hi
))
1708 return m
->sections
[i
];
1713 /* Tweak phdrs before writing them out. */
1716 spu_elf_modify_program_headers (bfd
*abfd
, struct bfd_link_info
*info
)
1718 const struct elf_backend_data
*bed
;
1719 struct elf_obj_tdata
*tdata
;
1720 Elf_Internal_Phdr
*phdr
, *last
;
1721 struct spu_link_hash_table
*htab
;
1728 bed
= get_elf_backend_data (abfd
);
1729 tdata
= elf_tdata (abfd
);
1731 count
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
1732 htab
= spu_hash_table (info
);
1733 if (htab
->num_overlays
!= 0)
1735 struct elf_segment_map
*m
;
1738 for (i
= 0, m
= elf_tdata (abfd
)->segment_map
; m
; ++i
, m
= m
->next
)
1740 && (o
= spu_elf_section_data (m
->sections
[0])->ovl_index
) != 0)
1742 /* Mark this as an overlay header. */
1743 phdr
[i
].p_flags
|= PF_OVERLAY
;
1745 if (htab
->ovtab
!= NULL
&& htab
->ovtab
->size
!= 0)
1747 bfd_byte
*p
= htab
->ovtab
->contents
;
1748 unsigned int off
= (o
- 1) * 16 + 8;
1750 /* Write file_off into _ovly_table. */
1751 bfd_put_32 (htab
->ovtab
->owner
, phdr
[i
].p_offset
, p
+ off
);
1756 /* Round up p_filesz and p_memsz of PT_LOAD segments to multiples
1757 of 16. This should always be possible when using the standard
1758 linker scripts, but don't create overlapping segments if
1759 someone is playing games with linker scripts. */
1761 for (i
= count
; i
-- != 0; )
1762 if (phdr
[i
].p_type
== PT_LOAD
)
1766 adjust
= -phdr
[i
].p_filesz
& 15;
1769 && phdr
[i
].p_offset
+ phdr
[i
].p_filesz
> last
->p_offset
- adjust
)
1772 adjust
= -phdr
[i
].p_memsz
& 15;
1775 && phdr
[i
].p_filesz
!= 0
1776 && phdr
[i
].p_vaddr
+ phdr
[i
].p_memsz
> last
->p_vaddr
- adjust
1777 && phdr
[i
].p_vaddr
+ phdr
[i
].p_memsz
<= last
->p_vaddr
)
1780 if (phdr
[i
].p_filesz
!= 0)
1784 if (i
== (unsigned int) -1)
1785 for (i
= count
; i
-- != 0; )
1786 if (phdr
[i
].p_type
== PT_LOAD
)
1790 adjust
= -phdr
[i
].p_filesz
& 15;
1791 phdr
[i
].p_filesz
+= adjust
;
1793 adjust
= -phdr
[i
].p_memsz
& 15;
1794 phdr
[i
].p_memsz
+= adjust
;
1800 /* Arrange for our linker created section to be output. */
1803 spu_elf_section_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
1804 Elf_Internal_Shdr
*i_shdrp
)
1808 sec
= i_shdrp
->bfd_section
;
1810 && (sec
->flags
& SEC_LINKER_CREATED
) != 0
1811 && sec
->name
!= NULL
1812 && strcmp (sec
->name
, SPU_PTNOTE_SPUNAME
) == 0)
1813 i_shdrp
->contents
= sec
->contents
;
1818 #define TARGET_BIG_SYM bfd_elf32_spu_vec
1819 #define TARGET_BIG_NAME "elf32-spu"
1820 #define ELF_ARCH bfd_arch_spu
1821 #define ELF_MACHINE_CODE EM_SPU
1822 /* This matches the alignment need for DMA. */
1823 #define ELF_MAXPAGESIZE 0x80
1824 #define elf_backend_rela_normal 1
1825 #define elf_backend_can_gc_sections 1
1827 #define bfd_elf32_bfd_reloc_type_lookup spu_elf_reloc_type_lookup
1828 #define bfd_elf32_bfd_reloc_name_lookup spu_elf_reloc_name_lookup
1829 #define elf_info_to_howto spu_elf_info_to_howto
1830 #define elf_backend_gc_mark_hook spu_elf_gc_mark_hook
1831 #define elf_backend_relocate_section spu_elf_relocate_section
1832 #define elf_backend_symbol_processing spu_elf_backend_symbol_processing
1833 #define bfd_elf32_new_section_hook spu_elf_new_section_hook
1834 #define bfd_elf32_bfd_link_hash_table_create spu_elf_link_hash_table_create
1835 #define bfd_elf32_bfd_link_hash_table_free spu_elf_link_hash_table_free
1837 #define elf_backend_additional_program_headers spu_elf_additional_program_headers
1838 #define elf_backend_modify_segment_map spu_elf_modify_segment_map
1839 #define elf_backend_modify_program_headers spu_elf_modify_program_headers
1840 #define elf_backend_post_process_headers spu_elf_post_process_headers
1841 #define elf_backend_section_processing spu_elf_section_processing
1842 #define elf_backend_special_sections spu_elf_special_sections
1844 #include "elf32-target.h"