1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
4 Contributed by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
27 #include "elf-vxworks.h"
29 #include "libiberty.h"
30 #include "../opcodes/sh-opc.h"
32 static bfd_reloc_status_type sh_elf_reloc
33 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
34 static bfd_reloc_status_type sh_elf_ignore_reloc
35 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
36 static bfd_boolean sh_elf_relax_delete_bytes
37 (bfd
*, asection
*, bfd_vma
, int);
38 static bfd_boolean sh_elf_align_loads
39 (bfd
*, asection
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_boolean
*);
41 static bfd_boolean sh_elf_swap_insns
42 (bfd
*, asection
*, void *, bfd_byte
*, bfd_vma
);
44 static int sh_elf_optimized_tls_reloc
45 (struct bfd_link_info
*, int, int);
46 static bfd_vma dtpoff_base
47 (struct bfd_link_info
*);
49 (struct bfd_link_info
*, bfd_vma
);
51 /* The name of the dynamic interpreter. This is put in the .interp
54 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
56 #define MINUS_ONE ((bfd_vma) 0 - 1)
58 #define SH_PARTIAL32 TRUE
59 #define SH_SRC_MASK32 0xffffffff
60 #define SH_ELF_RELOC sh_elf_reloc
61 static reloc_howto_type sh_elf_howto_table
[] =
63 #include "elf32-sh-relocs.h"
66 #define SH_PARTIAL32 FALSE
67 #define SH_SRC_MASK32 0
68 #define SH_ELF_RELOC bfd_elf_generic_reloc
69 static reloc_howto_type sh_vxworks_howto_table
[] =
71 #include "elf32-sh-relocs.h"
74 /* Return true if OUTPUT_BFD is a VxWorks object. */
77 vxworks_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
79 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
80 extern const bfd_target bfd_elf32_shlvxworks_vec
;
81 extern const bfd_target bfd_elf32_shvxworks_vec
;
83 return (abfd
->xvec
== &bfd_elf32_shlvxworks_vec
84 || abfd
->xvec
== &bfd_elf32_shvxworks_vec
);
90 /* Return the howto table for ABFD. */
92 static reloc_howto_type
*
93 get_howto_table (bfd
*abfd
)
95 if (vxworks_object_p (abfd
))
96 return sh_vxworks_howto_table
;
97 return sh_elf_howto_table
;
100 static bfd_reloc_status_type
101 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
102 asection
*input_section
, bfd_byte
*contents
,
103 bfd_vma addr
, asection
*symbol_section
,
104 bfd_vma start
, bfd_vma end
)
106 static bfd_vma last_addr
;
107 static asection
*last_symbol_section
;
108 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
113 /* Sanity check the address. */
114 if (addr
> bfd_get_section_limit (input_bfd
, input_section
))
115 return bfd_reloc_outofrange
;
117 /* We require the start and end relocations to be processed consecutively -
118 although we allow then to be processed forwards or backwards. */
122 last_symbol_section
= symbol_section
;
125 if (last_addr
!= addr
)
129 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
130 return bfd_reloc_outofrange
;
132 /* Get the symbol_section contents. */
133 if (symbol_section
!= input_section
)
135 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
136 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
139 if (!bfd_malloc_and_get_section (input_bfd
, symbol_section
,
142 if (contents
!= NULL
)
144 return bfd_reloc_outofrange
;
148 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
149 start_ptr
= contents
+ start
;
150 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
152 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
155 diff
= (last_ptr
- ptr
) >> 1;
156 cum_diff
+= diff
& 1;
159 /* Calculate the start / end values to load into rs / re minus four -
160 so that will cancel out the four we would otherwise have to add to
161 addr to get the value to subtract in order to get relative addressing. */
165 end
= (ptr
+ cum_diff
* 2) - contents
;
169 bfd_vma start0
= start
- 4;
171 while (start0
&& IS_PPI (contents
+ start0
))
173 start0
= start
- 2 - ((start
- start0
) & 2);
174 start
= start0
- cum_diff
- 2;
179 && elf_section_data (symbol_section
)->this_hdr
.contents
!= contents
)
182 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
184 x
= (insn
& 0x200 ? end
: start
) - addr
;
185 if (input_section
!= symbol_section
)
186 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
187 - (input_section
->output_section
->vma
188 + input_section
->output_offset
));
190 if (x
< -128 || x
> 127)
191 return bfd_reloc_overflow
;
193 x
= (insn
& ~0xff) | (x
& 0xff);
194 bfd_put_16 (input_bfd
, (bfd_vma
) x
, contents
+ addr
);
199 /* This function is used for normal relocs. This used to be like the COFF
200 function, and is almost certainly incorrect for other ELF targets. */
202 static bfd_reloc_status_type
203 sh_elf_reloc (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol_in
,
204 void *data
, asection
*input_section
, bfd
*output_bfd
,
205 char **error_message ATTRIBUTE_UNUSED
)
209 enum elf_sh_reloc_type r_type
;
210 bfd_vma addr
= reloc_entry
->address
;
211 bfd_byte
*hit_data
= addr
+ (bfd_byte
*) data
;
213 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
215 if (output_bfd
!= NULL
)
217 /* Partial linking--do nothing. */
218 reloc_entry
->address
+= input_section
->output_offset
;
222 /* Almost all relocs have to do with relaxing. If any work must be
223 done for them, it has been done in sh_relax_section. */
224 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
227 if (symbol_in
!= NULL
228 && bfd_is_und_section (symbol_in
->section
))
229 return bfd_reloc_undefined
;
231 if (bfd_is_com_section (symbol_in
->section
))
234 sym_value
= (symbol_in
->value
+
235 symbol_in
->section
->output_section
->vma
+
236 symbol_in
->section
->output_offset
);
241 insn
= bfd_get_32 (abfd
, hit_data
);
242 insn
+= sym_value
+ reloc_entry
->addend
;
243 bfd_put_32 (abfd
, (bfd_vma
) insn
, hit_data
);
246 insn
= bfd_get_16 (abfd
, hit_data
);
247 sym_value
+= reloc_entry
->addend
;
248 sym_value
-= (input_section
->output_section
->vma
249 + input_section
->output_offset
252 sym_value
+= (insn
& 0xfff) << 1;
255 insn
= (insn
& 0xf000) | (sym_value
& 0xfff);
256 bfd_put_16 (abfd
, (bfd_vma
) insn
, hit_data
);
257 if (sym_value
< (bfd_vma
) -0x1000 || sym_value
>= 0x1000)
258 return bfd_reloc_overflow
;
268 /* This function is used for relocs which are only used for relaxing,
269 which the linker should otherwise ignore. */
271 static bfd_reloc_status_type
272 sh_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
273 asymbol
*symbol ATTRIBUTE_UNUSED
,
274 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
276 char **error_message ATTRIBUTE_UNUSED
)
278 if (output_bfd
!= NULL
)
279 reloc_entry
->address
+= input_section
->output_offset
;
283 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
287 bfd_reloc_code_real_type bfd_reloc_val
;
288 unsigned char elf_reloc_val
;
291 /* An array mapping BFD reloc codes to SH ELF relocs. */
293 static const struct elf_reloc_map sh_reloc_map
[] =
295 { BFD_RELOC_NONE
, R_SH_NONE
},
296 { BFD_RELOC_32
, R_SH_DIR32
},
297 { BFD_RELOC_16
, R_SH_DIR16
},
298 { BFD_RELOC_8
, R_SH_DIR8
},
299 { BFD_RELOC_CTOR
, R_SH_DIR32
},
300 { BFD_RELOC_32_PCREL
, R_SH_REL32
},
301 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_DIR8WPN
},
302 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_IND12W
},
303 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_DIR8WPZ
},
304 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_DIR8WPL
},
305 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
306 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
307 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
308 { BFD_RELOC_SH_USES
, R_SH_USES
},
309 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
310 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
311 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
312 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
313 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
314 { BFD_RELOC_VTABLE_INHERIT
, R_SH_GNU_VTINHERIT
},
315 { BFD_RELOC_VTABLE_ENTRY
, R_SH_GNU_VTENTRY
},
316 { BFD_RELOC_SH_LOOP_START
, R_SH_LOOP_START
},
317 { BFD_RELOC_SH_LOOP_END
, R_SH_LOOP_END
},
318 { BFD_RELOC_SH_TLS_GD_32
, R_SH_TLS_GD_32
},
319 { BFD_RELOC_SH_TLS_LD_32
, R_SH_TLS_LD_32
},
320 { BFD_RELOC_SH_TLS_LDO_32
, R_SH_TLS_LDO_32
},
321 { BFD_RELOC_SH_TLS_IE_32
, R_SH_TLS_IE_32
},
322 { BFD_RELOC_SH_TLS_LE_32
, R_SH_TLS_LE_32
},
323 { BFD_RELOC_SH_TLS_DTPMOD32
, R_SH_TLS_DTPMOD32
},
324 { BFD_RELOC_SH_TLS_DTPOFF32
, R_SH_TLS_DTPOFF32
},
325 { BFD_RELOC_SH_TLS_TPOFF32
, R_SH_TLS_TPOFF32
},
326 { BFD_RELOC_32_GOT_PCREL
, R_SH_GOT32
},
327 { BFD_RELOC_32_PLT_PCREL
, R_SH_PLT32
},
328 { BFD_RELOC_SH_COPY
, R_SH_COPY
},
329 { BFD_RELOC_SH_GLOB_DAT
, R_SH_GLOB_DAT
},
330 { BFD_RELOC_SH_JMP_SLOT
, R_SH_JMP_SLOT
},
331 { BFD_RELOC_SH_RELATIVE
, R_SH_RELATIVE
},
332 { BFD_RELOC_32_GOTOFF
, R_SH_GOTOFF
},
333 { BFD_RELOC_SH_GOTPC
, R_SH_GOTPC
},
334 { BFD_RELOC_SH_GOTPLT32
, R_SH_GOTPLT32
},
335 #ifdef INCLUDE_SHMEDIA
336 { BFD_RELOC_SH_GOT_LOW16
, R_SH_GOT_LOW16
},
337 { BFD_RELOC_SH_GOT_MEDLOW16
, R_SH_GOT_MEDLOW16
},
338 { BFD_RELOC_SH_GOT_MEDHI16
, R_SH_GOT_MEDHI16
},
339 { BFD_RELOC_SH_GOT_HI16
, R_SH_GOT_HI16
},
340 { BFD_RELOC_SH_GOTPLT_LOW16
, R_SH_GOTPLT_LOW16
},
341 { BFD_RELOC_SH_GOTPLT_MEDLOW16
, R_SH_GOTPLT_MEDLOW16
},
342 { BFD_RELOC_SH_GOTPLT_MEDHI16
, R_SH_GOTPLT_MEDHI16
},
343 { BFD_RELOC_SH_GOTPLT_HI16
, R_SH_GOTPLT_HI16
},
344 { BFD_RELOC_SH_PLT_LOW16
, R_SH_PLT_LOW16
},
345 { BFD_RELOC_SH_PLT_MEDLOW16
, R_SH_PLT_MEDLOW16
},
346 { BFD_RELOC_SH_PLT_MEDHI16
, R_SH_PLT_MEDHI16
},
347 { BFD_RELOC_SH_PLT_HI16
, R_SH_PLT_HI16
},
348 { BFD_RELOC_SH_GOTOFF_LOW16
, R_SH_GOTOFF_LOW16
},
349 { BFD_RELOC_SH_GOTOFF_MEDLOW16
, R_SH_GOTOFF_MEDLOW16
},
350 { BFD_RELOC_SH_GOTOFF_MEDHI16
, R_SH_GOTOFF_MEDHI16
},
351 { BFD_RELOC_SH_GOTOFF_HI16
, R_SH_GOTOFF_HI16
},
352 { BFD_RELOC_SH_GOTPC_LOW16
, R_SH_GOTPC_LOW16
},
353 { BFD_RELOC_SH_GOTPC_MEDLOW16
, R_SH_GOTPC_MEDLOW16
},
354 { BFD_RELOC_SH_GOTPC_MEDHI16
, R_SH_GOTPC_MEDHI16
},
355 { BFD_RELOC_SH_GOTPC_HI16
, R_SH_GOTPC_HI16
},
356 { BFD_RELOC_SH_COPY64
, R_SH_COPY64
},
357 { BFD_RELOC_SH_GLOB_DAT64
, R_SH_GLOB_DAT64
},
358 { BFD_RELOC_SH_JMP_SLOT64
, R_SH_JMP_SLOT64
},
359 { BFD_RELOC_SH_RELATIVE64
, R_SH_RELATIVE64
},
360 { BFD_RELOC_SH_GOT10BY4
, R_SH_GOT10BY4
},
361 { BFD_RELOC_SH_GOT10BY8
, R_SH_GOT10BY8
},
362 { BFD_RELOC_SH_GOTPLT10BY4
, R_SH_GOTPLT10BY4
},
363 { BFD_RELOC_SH_GOTPLT10BY8
, R_SH_GOTPLT10BY8
},
364 { BFD_RELOC_SH_PT_16
, R_SH_PT_16
},
365 { BFD_RELOC_SH_SHMEDIA_CODE
, R_SH_SHMEDIA_CODE
},
366 { BFD_RELOC_SH_IMMU5
, R_SH_DIR5U
},
367 { BFD_RELOC_SH_IMMS6
, R_SH_DIR6S
},
368 { BFD_RELOC_SH_IMMU6
, R_SH_DIR6U
},
369 { BFD_RELOC_SH_IMMS10
, R_SH_DIR10S
},
370 { BFD_RELOC_SH_IMMS10BY2
, R_SH_DIR10SW
},
371 { BFD_RELOC_SH_IMMS10BY4
, R_SH_DIR10SL
},
372 { BFD_RELOC_SH_IMMS10BY8
, R_SH_DIR10SQ
},
373 { BFD_RELOC_SH_IMMS16
, R_SH_IMMS16
},
374 { BFD_RELOC_SH_IMMU16
, R_SH_IMMU16
},
375 { BFD_RELOC_SH_IMM_LOW16
, R_SH_IMM_LOW16
},
376 { BFD_RELOC_SH_IMM_LOW16_PCREL
, R_SH_IMM_LOW16_PCREL
},
377 { BFD_RELOC_SH_IMM_MEDLOW16
, R_SH_IMM_MEDLOW16
},
378 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL
, R_SH_IMM_MEDLOW16_PCREL
},
379 { BFD_RELOC_SH_IMM_MEDHI16
, R_SH_IMM_MEDHI16
},
380 { BFD_RELOC_SH_IMM_MEDHI16_PCREL
, R_SH_IMM_MEDHI16_PCREL
},
381 { BFD_RELOC_SH_IMM_HI16
, R_SH_IMM_HI16
},
382 { BFD_RELOC_SH_IMM_HI16_PCREL
, R_SH_IMM_HI16_PCREL
},
383 { BFD_RELOC_64
, R_SH_64
},
384 { BFD_RELOC_64_PCREL
, R_SH_64_PCREL
},
385 #endif /* not INCLUDE_SHMEDIA */
388 /* Given a BFD reloc code, return the howto structure for the
389 corresponding SH ELF reloc. */
391 static reloc_howto_type
*
392 sh_elf_reloc_type_lookup (bfd
*abfd
, bfd_reloc_code_real_type code
)
396 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
398 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
399 return get_howto_table (abfd
) + (int) sh_reloc_map
[i
].elf_reloc_val
;
405 /* Given an ELF reloc, fill in the howto field of a relent. */
408 sh_elf_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
, Elf_Internal_Rela
*dst
)
412 r
= ELF32_R_TYPE (dst
->r_info
);
414 BFD_ASSERT (r
< (unsigned int) R_SH_max
);
415 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC
|| r
> R_SH_LAST_INVALID_RELOC
);
416 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_2
|| r
> R_SH_LAST_INVALID_RELOC_2
);
417 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_3
|| r
> R_SH_LAST_INVALID_RELOC_3
);
418 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_4
|| r
> R_SH_LAST_INVALID_RELOC_4
);
419 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_5
|| r
> R_SH_LAST_INVALID_RELOC_5
);
421 cache_ptr
->howto
= get_howto_table (abfd
) + r
;
424 /* This function handles relaxing for SH ELF. See the corresponding
425 function in coff-sh.c for a description of what this does. FIXME:
426 There is a lot of duplication here between this code and the COFF
427 specific code. The format of relocs and symbols is wound deeply
428 into this code, but it would still be better if the duplication
429 could be eliminated somehow. Note in particular that although both
430 functions use symbols like R_SH_CODE, those symbols have different
431 values; in coff-sh.c they come from include/coff/sh.h, whereas here
432 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
435 sh_elf_relax_section (bfd
*abfd
, asection
*sec
,
436 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
438 Elf_Internal_Shdr
*symtab_hdr
;
439 Elf_Internal_Rela
*internal_relocs
;
440 bfd_boolean have_code
;
441 Elf_Internal_Rela
*irel
, *irelend
;
442 bfd_byte
*contents
= NULL
;
443 Elf_Internal_Sym
*isymbuf
= NULL
;
447 if (link_info
->relocatable
448 || (sec
->flags
& SEC_RELOC
) == 0
449 || sec
->reloc_count
== 0)
452 #ifdef INCLUDE_SHMEDIA
453 if (elf_section_data (sec
)->this_hdr
.sh_flags
454 & (SHF_SH5_ISA32
| SHF_SH5_ISA32_MIXED
))
460 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
462 internal_relocs
= (_bfd_elf_link_read_relocs
463 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
464 link_info
->keep_memory
));
465 if (internal_relocs
== NULL
)
470 irelend
= internal_relocs
+ sec
->reloc_count
;
471 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
473 bfd_vma laddr
, paddr
, symval
;
475 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
478 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
481 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
484 /* Get the section contents. */
485 if (contents
== NULL
)
487 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
488 contents
= elf_section_data (sec
)->this_hdr
.contents
;
491 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
496 /* The r_addend field of the R_SH_USES reloc will point us to
497 the register load. The 4 is because the r_addend field is
498 computed as though it were a jump offset, which are based
499 from 4 bytes after the jump instruction. */
500 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
501 if (laddr
>= sec
->size
)
503 (*_bfd_error_handler
) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
505 (unsigned long) irel
->r_offset
);
508 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
510 /* If the instruction is not mov.l NN,rN, we don't know what to
512 if ((insn
& 0xf000) != 0xd000)
514 ((*_bfd_error_handler
)
515 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
516 abfd
, (unsigned long) irel
->r_offset
, insn
));
520 /* Get the address from which the register is being loaded. The
521 displacement in the mov.l instruction is quadrupled. It is a
522 displacement from four bytes after the movl instruction, but,
523 before adding in the PC address, two least significant bits
524 of the PC are cleared. We assume that the section is aligned
525 on a four byte boundary. */
528 paddr
+= (laddr
+ 4) &~ (bfd_vma
) 3;
529 if (paddr
>= sec
->size
)
531 ((*_bfd_error_handler
)
532 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
533 abfd
, (unsigned long) irel
->r_offset
));
537 /* Get the reloc for the address from which the register is
538 being loaded. This reloc will tell us which function is
539 actually being called. */
540 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
541 if (irelfn
->r_offset
== paddr
542 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
544 if (irelfn
>= irelend
)
546 ((*_bfd_error_handler
)
547 (_("%B: 0x%lx: warning: could not find expected reloc"),
548 abfd
, (unsigned long) paddr
));
552 /* Read this BFD's symbols if we haven't done so already. */
553 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
555 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
557 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
558 symtab_hdr
->sh_info
, 0,
564 /* Get the value of the symbol referred to by the reloc. */
565 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
567 /* A local symbol. */
568 Elf_Internal_Sym
*isym
;
570 isym
= isymbuf
+ ELF32_R_SYM (irelfn
->r_info
);
572 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd
, sec
))
574 ((*_bfd_error_handler
)
575 (_("%B: 0x%lx: warning: symbol in unexpected section"),
576 abfd
, (unsigned long) paddr
));
580 symval
= (isym
->st_value
581 + sec
->output_section
->vma
582 + sec
->output_offset
);
587 struct elf_link_hash_entry
*h
;
589 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
590 h
= elf_sym_hashes (abfd
)[indx
];
591 BFD_ASSERT (h
!= NULL
);
592 if (h
->root
.type
!= bfd_link_hash_defined
593 && h
->root
.type
!= bfd_link_hash_defweak
)
595 /* This appears to be a reference to an undefined
596 symbol. Just ignore it--it will be caught by the
597 regular reloc processing. */
601 symval
= (h
->root
.u
.def
.value
602 + h
->root
.u
.def
.section
->output_section
->vma
603 + h
->root
.u
.def
.section
->output_offset
);
606 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
607 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
609 symval
+= irelfn
->r_addend
;
611 /* See if this function call can be shortened. */
614 + sec
->output_section
->vma
617 /* A branch to an address beyond ours might be increased by an
618 .align that doesn't move when bytes behind us are deleted.
619 So, we add some slop in this calculation to allow for
621 if (foff
< -0x1000 || foff
>= 0x1000 - 8)
623 /* After all that work, we can't shorten this function call. */
627 /* Shorten the function call. */
629 /* For simplicity of coding, we are going to modify the section
630 contents, the section relocs, and the BFD symbol table. We
631 must tell the rest of the code not to free up this
632 information. It would be possible to instead create a table
633 of changes which have to be made, as is done in coff-mips.c;
634 that would be more work, but would require less memory when
635 the linker is run. */
637 elf_section_data (sec
)->relocs
= internal_relocs
;
638 elf_section_data (sec
)->this_hdr
.contents
= contents
;
639 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
641 /* Replace the jsr with a bsr. */
643 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
644 replace the jsr with a bsr. */
645 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
646 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
647 here, but that only checks if the symbol is an external symbol,
648 not if the symbol is in a different section. Besides, we need
649 a consistent meaning for the relocation, so we just assume here that
650 the value of the symbol is not available. */
652 /* We can't fully resolve this yet, because the external
653 symbol value may be changed by future relaxing. We let
654 the final link phase handle it. */
655 bfd_put_16 (abfd
, (bfd_vma
) 0xb000, contents
+ irel
->r_offset
);
659 /* When we calculated the symbol "value" we had an offset in the
660 DIR32's word in memory (we read and add it above). However,
661 the jsr we create does NOT have this offset encoded, so we
662 have to add it to the addend to preserve it. */
663 irel
->r_addend
+= bfd_get_32 (abfd
, contents
+ paddr
);
665 /* See if there is another R_SH_USES reloc referring to the same
667 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
668 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
669 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
671 if (irelscan
< irelend
)
673 /* Some other function call depends upon this register load,
674 and we have not yet converted that function call.
675 Indeed, we may never be able to convert it. There is
676 nothing else we can do at this point. */
680 /* Look for a R_SH_COUNT reloc on the location where the
681 function address is stored. Do this before deleting any
682 bytes, to avoid confusion about the address. */
683 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
684 if (irelcount
->r_offset
== paddr
685 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
688 /* Delete the register load. */
689 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
692 /* That will change things, so, just in case it permits some
693 other function call to come within range, we should relax
694 again. Note that this is not required, and it may be slow. */
697 /* Now check whether we got a COUNT reloc. */
698 if (irelcount
>= irelend
)
700 ((*_bfd_error_handler
)
701 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
702 abfd
, (unsigned long) paddr
));
706 /* The number of uses is stored in the r_addend field. We've
708 if (irelcount
->r_addend
== 0)
710 ((*_bfd_error_handler
) (_("%B: 0x%lx: warning: bad count"),
712 (unsigned long) paddr
));
716 --irelcount
->r_addend
;
718 /* If there are no more uses, we can delete the address. Reload
719 the address from irelfn, in case it was changed by the
720 previous call to sh_elf_relax_delete_bytes. */
721 if (irelcount
->r_addend
== 0)
723 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
727 /* We've done all we can with that function call. */
730 /* Look for load and store instructions that we can align on four
732 if ((elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
) != EF_SH4
737 /* Get the section contents. */
738 if (contents
== NULL
)
740 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
741 contents
= elf_section_data (sec
)->this_hdr
.contents
;
744 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
749 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
755 elf_section_data (sec
)->relocs
= internal_relocs
;
756 elf_section_data (sec
)->this_hdr
.contents
= contents
;
757 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
762 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
764 if (! link_info
->keep_memory
)
768 /* Cache the symbols for elf_link_input_bfd. */
769 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
774 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
776 if (! link_info
->keep_memory
)
780 /* Cache the section contents for elf_link_input_bfd. */
781 elf_section_data (sec
)->this_hdr
.contents
= contents
;
785 if (internal_relocs
!= NULL
786 && elf_section_data (sec
)->relocs
!= internal_relocs
)
787 free (internal_relocs
);
793 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
796 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
798 if (internal_relocs
!= NULL
799 && elf_section_data (sec
)->relocs
!= internal_relocs
)
800 free (internal_relocs
);
805 /* Delete some bytes from a section while relaxing. FIXME: There is a
806 lot of duplication between this function and sh_relax_delete_bytes
810 sh_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
,
813 Elf_Internal_Shdr
*symtab_hdr
;
814 unsigned int sec_shndx
;
816 Elf_Internal_Rela
*irel
, *irelend
;
817 Elf_Internal_Rela
*irelalign
;
819 Elf_Internal_Sym
*isymbuf
, *isym
, *isymend
;
820 struct elf_link_hash_entry
**sym_hashes
;
821 struct elf_link_hash_entry
**end_hashes
;
822 unsigned int symcount
;
825 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
826 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
828 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
830 contents
= elf_section_data (sec
)->this_hdr
.contents
;
832 /* The deletion must stop at the next ALIGN reloc for an aligment
833 power larger than the number of bytes we are deleting. */
838 irel
= elf_section_data (sec
)->relocs
;
839 irelend
= irel
+ sec
->reloc_count
;
840 for (; irel
< irelend
; irel
++)
842 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
843 && irel
->r_offset
> addr
844 && count
< (1 << irel
->r_addend
))
847 toaddr
= irel
->r_offset
;
852 /* Actually delete the bytes. */
853 memmove (contents
+ addr
, contents
+ addr
+ count
,
854 (size_t) (toaddr
- addr
- count
));
855 if (irelalign
== NULL
)
861 #define NOP_OPCODE (0x0009)
863 BFD_ASSERT ((count
& 1) == 0);
864 for (i
= 0; i
< count
; i
+= 2)
865 bfd_put_16 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
868 /* Adjust all the relocs. */
869 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
871 bfd_vma nraddr
, stop
;
874 int off
, adjust
, oinsn
;
875 bfd_signed_vma voff
= 0;
876 bfd_boolean overflow
;
878 /* Get the new reloc address. */
879 nraddr
= irel
->r_offset
;
880 if ((irel
->r_offset
> addr
881 && irel
->r_offset
< toaddr
)
882 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
883 && irel
->r_offset
== toaddr
))
886 /* See if this reloc was for the bytes we have deleted, in which
887 case we no longer care about it. Don't delete relocs which
888 represent addresses, though. */
889 if (irel
->r_offset
>= addr
890 && irel
->r_offset
< addr
+ count
891 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
892 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
893 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
894 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
895 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
898 /* If this is a PC relative reloc, see if the range it covers
899 includes the bytes we have deleted. */
900 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
909 start
= irel
->r_offset
;
910 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
914 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
921 /* If this reloc is against a symbol defined in this
922 section, and the symbol will not be adjusted below, we
923 must check the addend to see it will put the value in
924 range to be adjusted, and hence must be changed. */
925 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
927 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
928 if (isym
->st_shndx
== sec_shndx
929 && (isym
->st_value
<= addr
930 || isym
->st_value
>= toaddr
))
934 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
936 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
937 val
+= isym
->st_value
;
938 if (val
> addr
&& val
< toaddr
)
939 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
943 val
= isym
->st_value
+ irel
->r_addend
;
944 if (val
> addr
&& val
< toaddr
)
945 irel
->r_addend
-= count
;
956 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
963 /* This has been made by previous relaxation. Since the
964 relocation will be against an external symbol, the
965 final relocation will just do the right thing. */
972 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
974 /* The addend will be against the section symbol, thus
975 for adjusting the addend, the relevant start is the
976 start of the section.
977 N.B. If we want to abandon in-place changes here and
978 test directly using symbol + addend, we have to take into
979 account that the addend has already been adjusted by -4. */
980 if (stop
> addr
&& stop
< toaddr
)
981 irel
->r_addend
-= count
;
987 stop
= start
+ 4 + off
* 2;
992 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
998 /* These relocs types represent
1000 The r_addend field holds the difference between the reloc
1001 address and L1. That is the start of the reloc, and
1002 adding in the contents gives us the top. We must adjust
1003 both the r_offset field and the section contents.
1004 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1005 and the elf bfd r_offset is called r_vaddr. */
1007 stop
= irel
->r_offset
;
1008 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
1012 && (stop
<= addr
|| stop
>= toaddr
))
1013 irel
->r_addend
+= count
;
1014 else if (stop
> addr
1016 && (start
<= addr
|| start
>= toaddr
))
1017 irel
->r_addend
-= count
;
1019 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1020 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1021 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1022 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1024 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1025 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1030 start
= irel
->r_offset
;
1031 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1032 + (long) irel
->r_addend
1039 && (stop
<= addr
|| stop
>= toaddr
))
1041 else if (stop
> addr
1043 && (start
<= addr
|| start
>= toaddr
))
1052 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1061 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1063 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1068 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1070 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1074 BFD_ASSERT (adjust
== count
|| count
>= 4);
1079 if ((irel
->r_offset
& 3) == 0)
1082 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1084 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1089 if (voff
< 0 || voff
>= 0xff)
1091 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1096 if (voff
< - 0x8000 || voff
>= 0x8000)
1098 bfd_put_signed_16 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1103 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1107 irel
->r_addend
+= adjust
;
1113 ((*_bfd_error_handler
)
1114 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1115 abfd
, (unsigned long) irel
->r_offset
));
1116 bfd_set_error (bfd_error_bad_value
);
1121 irel
->r_offset
= nraddr
;
1124 /* Look through all the other sections. If there contain any IMM32
1125 relocs against internal symbols which we are not going to adjust
1126 below, we may need to adjust the addends. */
1127 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1129 Elf_Internal_Rela
*internal_relocs
;
1130 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1131 bfd_byte
*ocontents
;
1134 || (o
->flags
& SEC_RELOC
) == 0
1135 || o
->reloc_count
== 0)
1138 /* We always cache the relocs. Perhaps, if info->keep_memory is
1139 FALSE, we should free them, if we are permitted to, when we
1140 leave sh_coff_relax_section. */
1141 internal_relocs
= (_bfd_elf_link_read_relocs
1142 (abfd
, o
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
1143 if (internal_relocs
== NULL
)
1147 irelscanend
= internal_relocs
+ o
->reloc_count
;
1148 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1150 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1151 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1153 bfd_vma start
, stop
;
1154 bfd_signed_vma voff
;
1156 if (ocontents
== NULL
)
1158 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1159 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1162 /* We always cache the section contents.
1163 Perhaps, if info->keep_memory is FALSE, we
1164 should free them, if we are permitted to,
1165 when we leave sh_coff_relax_section. */
1166 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1168 if (ocontents
!= NULL
)
1173 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1177 stop
= irelscan
->r_offset
;
1179 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1181 /* STOP is in a different section, so it won't change. */
1182 if (start
> addr
&& start
< toaddr
)
1183 irelscan
->r_addend
+= count
;
1185 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1186 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1190 && (stop
<= addr
|| stop
>= toaddr
))
1191 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
+ count
,
1192 ocontents
+ irelscan
->r_offset
);
1193 else if (stop
> addr
1195 && (start
<= addr
|| start
>= toaddr
))
1196 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
- count
,
1197 ocontents
+ irelscan
->r_offset
);
1200 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1203 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1207 isym
= isymbuf
+ ELF32_R_SYM (irelscan
->r_info
);
1208 if (isym
->st_shndx
== sec_shndx
1209 && (isym
->st_value
<= addr
1210 || isym
->st_value
>= toaddr
))
1214 if (ocontents
== NULL
)
1216 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1217 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1220 /* We always cache the section contents.
1221 Perhaps, if info->keep_memory is FALSE, we
1222 should free them, if we are permitted to,
1223 when we leave sh_coff_relax_section. */
1224 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1226 if (ocontents
!= NULL
)
1231 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1235 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1236 val
+= isym
->st_value
;
1237 if (val
> addr
&& val
< toaddr
)
1238 bfd_put_32 (abfd
, val
- count
,
1239 ocontents
+ irelscan
->r_offset
);
1244 /* Adjust the local symbols defined in this section. */
1245 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1246 for (isym
= isymbuf
; isym
< isymend
; isym
++)
1248 if (isym
->st_shndx
== sec_shndx
1249 && isym
->st_value
> addr
1250 && isym
->st_value
< toaddr
)
1251 isym
->st_value
-= count
;
1254 /* Now adjust the global symbols defined in this section. */
1255 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1256 - symtab_hdr
->sh_info
);
1257 sym_hashes
= elf_sym_hashes (abfd
);
1258 end_hashes
= sym_hashes
+ symcount
;
1259 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1261 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1262 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1263 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1264 && sym_hash
->root
.u
.def
.section
== sec
1265 && sym_hash
->root
.u
.def
.value
> addr
1266 && sym_hash
->root
.u
.def
.value
< toaddr
)
1268 sym_hash
->root
.u
.def
.value
-= count
;
1272 /* See if we can move the ALIGN reloc forward. We have adjusted
1273 r_offset for it already. */
1274 if (irelalign
!= NULL
)
1276 bfd_vma alignto
, alignaddr
;
1278 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1279 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1280 1 << irelalign
->r_addend
);
1281 if (alignto
!= alignaddr
)
1283 /* Tail recursion. */
1284 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1285 (int) (alignto
- alignaddr
));
1292 /* Look for loads and stores which we can align to four byte
1293 boundaries. This is like sh_align_loads in coff-sh.c. */
1296 sh_elf_align_loads (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1297 Elf_Internal_Rela
*internal_relocs
,
1298 bfd_byte
*contents ATTRIBUTE_UNUSED
,
1299 bfd_boolean
*pswapped
)
1301 Elf_Internal_Rela
*irel
, *irelend
;
1302 bfd_vma
*labels
= NULL
;
1303 bfd_vma
*label
, *label_end
;
1308 irelend
= internal_relocs
+ sec
->reloc_count
;
1310 /* Get all the addresses with labels on them. */
1311 amt
= sec
->reloc_count
;
1312 amt
*= sizeof (bfd_vma
);
1313 labels
= (bfd_vma
*) bfd_malloc (amt
);
1317 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1319 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1321 *label_end
= irel
->r_offset
;
1326 /* Note that the assembler currently always outputs relocs in
1327 address order. If that ever changes, this code will need to sort
1328 the label values and the relocs. */
1332 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1334 bfd_vma start
, stop
;
1336 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1339 start
= irel
->r_offset
;
1341 for (irel
++; irel
< irelend
; irel
++)
1342 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1345 stop
= irel
->r_offset
;
1349 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1350 internal_relocs
, &label
,
1351 label_end
, start
, stop
, pswapped
))
1366 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1369 sh_elf_swap_insns (bfd
*abfd
, asection
*sec
, void *relocs
,
1370 bfd_byte
*contents
, bfd_vma addr
)
1372 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1373 unsigned short i1
, i2
;
1374 Elf_Internal_Rela
*irel
, *irelend
;
1376 /* Swap the instructions themselves. */
1377 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1378 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1379 bfd_put_16 (abfd
, (bfd_vma
) i2
, contents
+ addr
);
1380 bfd_put_16 (abfd
, (bfd_vma
) i1
, contents
+ addr
+ 2);
1382 /* Adjust all reloc addresses. */
1383 irelend
= internal_relocs
+ sec
->reloc_count
;
1384 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1386 enum elf_sh_reloc_type type
;
1389 /* There are a few special types of relocs that we don't want to
1390 adjust. These relocs do not apply to the instruction itself,
1391 but are only associated with the address. */
1392 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1393 if (type
== R_SH_ALIGN
1394 || type
== R_SH_CODE
1395 || type
== R_SH_DATA
1396 || type
== R_SH_LABEL
)
1399 /* If an R_SH_USES reloc points to one of the addresses being
1400 swapped, we must adjust it. It would be incorrect to do this
1401 for a jump, though, since we want to execute both
1402 instructions after the jump. (We have avoided swapping
1403 around a label, so the jump will not wind up executing an
1404 instruction it shouldn't). */
1405 if (type
== R_SH_USES
)
1409 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
1411 irel
->r_offset
+= 2;
1412 else if (off
== addr
+ 2)
1413 irel
->r_offset
-= 2;
1416 if (irel
->r_offset
== addr
)
1418 irel
->r_offset
+= 2;
1421 else if (irel
->r_offset
== addr
+ 2)
1423 irel
->r_offset
-= 2;
1432 unsigned short insn
, oinsn
;
1433 bfd_boolean overflow
;
1435 loc
= contents
+ irel
->r_offset
;
1444 insn
= bfd_get_16 (abfd
, loc
);
1447 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1449 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1453 insn
= bfd_get_16 (abfd
, loc
);
1456 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1458 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1462 /* This reloc ignores the least significant 3 bits of
1463 the program counter before adding in the offset.
1464 This means that if ADDR is at an even address, the
1465 swap will not affect the offset. If ADDR is an at an
1466 odd address, then the instruction will be crossing a
1467 four byte boundary, and must be adjusted. */
1468 if ((addr
& 3) != 0)
1470 insn
= bfd_get_16 (abfd
, loc
);
1473 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1475 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1483 ((*_bfd_error_handler
)
1484 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1485 abfd
, (unsigned long) irel
->r_offset
));
1486 bfd_set_error (bfd_error_bad_value
);
1494 #endif /* defined SH64_ELF */
1496 /* Describes one of the various PLT styles. */
1498 struct elf_sh_plt_info
1500 /* The template for the first PLT entry, or NULL if there is no special
1502 const bfd_byte
*plt0_entry
;
1504 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1505 bfd_vma plt0_entry_size
;
1507 /* Index I is the offset into PLT0_ENTRY of a pointer to
1508 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1509 if there is no such pointer. */
1510 bfd_vma plt0_got_fields
[3];
1512 /* The template for a symbol's PLT entry. */
1513 const bfd_byte
*symbol_entry
;
1515 /* The size of SYMBOL_ENTRY in bytes. */
1516 bfd_vma symbol_entry_size
;
1518 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1519 on all targets. The comments by each member indicate the value
1520 that the field must hold. */
1522 bfd_vma got_entry
; /* the address of the symbol's .got.plt entry */
1523 bfd_vma plt
; /* .plt (or a branch to .plt on VxWorks) */
1524 bfd_vma reloc_offset
; /* the offset of the symbol's JMP_SLOT reloc */
1527 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1528 bfd_vma symbol_resolve_offset
;
1531 #ifdef INCLUDE_SHMEDIA
1533 /* The size in bytes of an entry in the procedure linkage table. */
1535 #define ELF_PLT_ENTRY_SIZE 64
1537 /* First entry in an absolute procedure linkage table look like this. */
1539 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1541 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1542 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1543 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1544 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1545 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1546 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1547 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1548 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1549 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1550 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1551 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1552 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1553 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1554 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1555 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1556 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1559 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1561 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1562 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1563 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1564 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1565 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1566 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1567 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1568 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1569 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1570 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1571 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1572 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1573 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1574 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1575 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1576 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1579 /* Sebsequent entries in an absolute procedure linkage table look like
1582 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1584 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1585 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1586 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1587 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1588 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1589 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1590 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1591 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1592 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1593 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1594 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1595 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1596 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1597 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1598 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1599 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1602 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1604 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1605 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1606 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1607 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1608 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1609 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1610 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1611 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1612 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1613 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1614 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1615 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1616 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1617 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1618 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1619 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1622 /* Entries in a PIC procedure linkage table look like this. */
1624 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1626 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1627 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1628 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1629 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1630 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1631 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1632 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1633 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1634 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1635 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1636 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1637 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1638 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1639 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1640 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1641 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1644 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1646 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1647 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1648 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1649 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1650 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1651 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1652 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1653 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1654 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1655 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1656 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1657 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1658 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1659 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1660 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1661 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1664 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1667 /* Big-endian non-PIC. */
1668 elf_sh_plt0_entry_be
,
1670 { 0, MINUS_ONE
, MINUS_ONE
},
1671 elf_sh_plt_entry_be
,
1674 33 /* includes ISA encoding */
1677 /* Little-endian non-PIC. */
1678 elf_sh_plt0_entry_le
,
1680 { 0, MINUS_ONE
, MINUS_ONE
},
1681 elf_sh_plt_entry_le
,
1684 33 /* includes ISA encoding */
1689 /* Big-endian PIC. */
1690 elf_sh_plt0_entry_be
,
1692 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1693 elf_sh_pic_plt_entry_be
,
1695 { 0, MINUS_ONE
, 52 },
1696 33 /* includes ISA encoding */
1699 /* Little-endian PIC. */
1700 elf_sh_plt0_entry_le
,
1702 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1703 elf_sh_pic_plt_entry_le
,
1705 { 0, MINUS_ONE
, 52 },
1706 33 /* includes ISA encoding */
1711 /* Return offset of the linker in PLT0 entry. */
1712 #define elf_sh_plt0_gotplt_offset(info) 0
1714 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1715 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1718 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1721 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p
,
1722 unsigned long value
, bfd_byte
*addr
)
1725 bfd_put_32 (output_bfd
,
1726 bfd_get_32 (output_bfd
, addr
)
1727 | ((value
>> 6) & 0x3fffc00),
1729 bfd_put_32 (output_bfd
,
1730 bfd_get_32 (output_bfd
, addr
+ 4)
1731 | ((value
<< 10) & 0x3fffc00),
1735 /* Return the type of PLT associated with ABFD. PIC_P is true if
1736 the object is position-independent. */
1738 static const struct elf_sh_plt_info
*
1739 get_plt_info (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean pic_p
)
1741 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
1744 /* The size in bytes of an entry in the procedure linkage table. */
1746 #define ELF_PLT_ENTRY_SIZE 28
1748 /* First entry in an absolute procedure linkage table look like this. */
1750 /* Note - this code has been "optimised" not to use r2. r2 is used by
1751 GCC to return the address of large structures, so it should not be
1752 corrupted here. This does mean however, that this PLT does not conform
1753 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1754 and r2 contains the GOT id. This version stores the GOT id in r0 and
1755 ignores the type. Loaders can easily detect this difference however,
1756 since the type will always be 0 or 8, and the GOT ids will always be
1757 greater than or equal to 12. */
1758 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1760 0xd0, 0x05, /* mov.l 2f,r0 */
1761 0x60, 0x02, /* mov.l @r0,r0 */
1762 0x2f, 0x06, /* mov.l r0,@-r15 */
1763 0xd0, 0x03, /* mov.l 1f,r0 */
1764 0x60, 0x02, /* mov.l @r0,r0 */
1765 0x40, 0x2b, /* jmp @r0 */
1766 0x60, 0xf6, /* mov.l @r15+,r0 */
1767 0x00, 0x09, /* nop */
1768 0x00, 0x09, /* nop */
1769 0x00, 0x09, /* nop */
1770 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1771 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1774 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1776 0x05, 0xd0, /* mov.l 2f,r0 */
1777 0x02, 0x60, /* mov.l @r0,r0 */
1778 0x06, 0x2f, /* mov.l r0,@-r15 */
1779 0x03, 0xd0, /* mov.l 1f,r0 */
1780 0x02, 0x60, /* mov.l @r0,r0 */
1781 0x2b, 0x40, /* jmp @r0 */
1782 0xf6, 0x60, /* mov.l @r15+,r0 */
1783 0x09, 0x00, /* nop */
1784 0x09, 0x00, /* nop */
1785 0x09, 0x00, /* nop */
1786 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1787 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1790 /* Sebsequent entries in an absolute procedure linkage table look like
1793 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1795 0xd0, 0x04, /* mov.l 1f,r0 */
1796 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1797 0xd1, 0x02, /* mov.l 0f,r1 */
1798 0x40, 0x2b, /* jmp @r0 */
1799 0x60, 0x13, /* mov r1,r0 */
1800 0xd1, 0x03, /* mov.l 2f,r1 */
1801 0x40, 0x2b, /* jmp @r0 */
1802 0x00, 0x09, /* nop */
1803 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1804 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1805 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1808 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1810 0x04, 0xd0, /* mov.l 1f,r0 */
1811 0x02, 0x60, /* mov.l @r0,r0 */
1812 0x02, 0xd1, /* mov.l 0f,r1 */
1813 0x2b, 0x40, /* jmp @r0 */
1814 0x13, 0x60, /* mov r1,r0 */
1815 0x03, 0xd1, /* mov.l 2f,r1 */
1816 0x2b, 0x40, /* jmp @r0 */
1817 0x09, 0x00, /* nop */
1818 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1819 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1820 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1823 /* Entries in a PIC procedure linkage table look like this. */
1825 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1827 0xd0, 0x04, /* mov.l 1f,r0 */
1828 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1829 0x40, 0x2b, /* jmp @r0 */
1830 0x00, 0x09, /* nop */
1831 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1832 0xd1, 0x03, /* mov.l 2f,r1 */
1833 0x40, 0x2b, /* jmp @r0 */
1834 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1835 0x00, 0x09, /* nop */
1836 0x00, 0x09, /* nop */
1837 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1838 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1841 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1843 0x04, 0xd0, /* mov.l 1f,r0 */
1844 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1845 0x2b, 0x40, /* jmp @r0 */
1846 0x09, 0x00, /* nop */
1847 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1848 0x03, 0xd1, /* mov.l 2f,r1 */
1849 0x2b, 0x40, /* jmp @r0 */
1850 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1851 0x09, 0x00, /* nop */
1852 0x09, 0x00, /* nop */
1853 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1854 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1857 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1860 /* Big-endian non-PIC. */
1861 elf_sh_plt0_entry_be
,
1863 { MINUS_ONE
, 24, 20 },
1864 elf_sh_plt_entry_be
,
1870 /* Little-endian non-PIC. */
1871 elf_sh_plt0_entry_le
,
1873 { MINUS_ONE
, 24, 20 },
1874 elf_sh_plt_entry_le
,
1882 /* Big-endian PIC. */
1883 elf_sh_plt0_entry_be
,
1885 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1886 elf_sh_pic_plt_entry_be
,
1888 { 20, MINUS_ONE
, 24 },
1892 /* Little-endian PIC. */
1893 elf_sh_plt0_entry_le
,
1895 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1896 elf_sh_pic_plt_entry_le
,
1898 { 20, MINUS_ONE
, 24 },
1904 #define VXWORKS_PLT_HEADER_SIZE 12
1905 #define VXWORKS_PLT_ENTRY_SIZE 24
1907 static const bfd_byte vxworks_sh_plt0_entry_be
[VXWORKS_PLT_HEADER_SIZE
] =
1909 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1910 0x61, 0x12, /* mov.l @r1,r1 */
1911 0x41, 0x2b, /* jmp @r1 */
1912 0x00, 0x09, /* nop */
1913 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1916 static const bfd_byte vxworks_sh_plt0_entry_le
[VXWORKS_PLT_HEADER_SIZE
] =
1918 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1919 0x12, 0x61, /* mov.l @r1,r1 */
1920 0x2b, 0x41, /* jmp @r1 */
1921 0x09, 0x00, /* nop */
1922 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1925 static const bfd_byte vxworks_sh_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1927 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1928 0x60, 0x02, /* mov.l @r0,r0 */
1929 0x40, 0x2b, /* jmp @r0 */
1930 0x00, 0x09, /* nop */
1931 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1932 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1933 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1934 0x00, 0x09, /* nop */
1935 0x00, 0x09, /* nop */
1936 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1939 static const bfd_byte vxworks_sh_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1941 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1942 0x02, 0x60, /* mov.l @r0,r0 */
1943 0x2b, 0x40, /* jmp @r0 */
1944 0x09, 0x00, /* nop */
1945 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1946 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1947 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1948 0x09, 0x00, /* nop */
1949 0x09, 0x00, /* nop */
1950 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1953 static const bfd_byte vxworks_sh_pic_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1955 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1956 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1957 0x40, 0x2b, /* jmp @r0 */
1958 0x00, 0x09, /* nop */
1959 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1960 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1961 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1962 0x41, 0x2b, /* jmp @r1 */
1963 0x00, 0x09, /* nop */
1964 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1967 static const bfd_byte vxworks_sh_pic_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1969 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1970 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1971 0x2b, 0x40, /* jmp @r0 */
1972 0x09, 0x00, /* nop */
1973 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1974 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1975 0xc2, 0x51, /* mov.l @(8,r12),r1 */
1976 0x2b, 0x41, /* jmp @r1 */
1977 0x09, 0x00, /* nop */
1978 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1981 static const struct elf_sh_plt_info vxworks_sh_plts
[2][2] = {
1984 /* Big-endian non-PIC. */
1985 vxworks_sh_plt0_entry_be
,
1986 VXWORKS_PLT_HEADER_SIZE
,
1987 { MINUS_ONE
, MINUS_ONE
, 8 },
1988 vxworks_sh_plt_entry_be
,
1989 VXWORKS_PLT_ENTRY_SIZE
,
1994 /* Little-endian non-PIC. */
1995 vxworks_sh_plt0_entry_le
,
1996 VXWORKS_PLT_HEADER_SIZE
,
1997 { MINUS_ONE
, MINUS_ONE
, 8 },
1998 vxworks_sh_plt_entry_le
,
1999 VXWORKS_PLT_ENTRY_SIZE
,
2006 /* Big-endian PIC. */
2009 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2010 vxworks_sh_pic_plt_entry_be
,
2011 VXWORKS_PLT_ENTRY_SIZE
,
2012 { 8, MINUS_ONE
, 20 },
2016 /* Little-endian PIC. */
2019 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2020 vxworks_sh_pic_plt_entry_le
,
2021 VXWORKS_PLT_ENTRY_SIZE
,
2022 { 8, MINUS_ONE
, 20 },
2028 /* Return the type of PLT associated with ABFD. PIC_P is true if
2029 the object is position-independent. */
2031 static const struct elf_sh_plt_info
*
2032 get_plt_info (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean pic_p
)
2034 if (vxworks_object_p (abfd
))
2035 return &vxworks_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2036 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2039 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2040 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2044 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p ATTRIBUTE_UNUSED
,
2045 unsigned long value
, bfd_byte
*addr
)
2047 bfd_put_32 (output_bfd
, value
, addr
);
2051 /* Return the index of the PLT entry at byte offset OFFSET. */
2054 get_plt_index (const struct elf_sh_plt_info
*info
, bfd_vma offset
)
2056 return (offset
- info
->plt0_entry_size
) / info
->symbol_entry_size
;
2059 /* Do the inverse operation. */
2062 get_plt_offset (const struct elf_sh_plt_info
*info
, bfd_vma index
)
2064 return info
->plt0_entry_size
+ (index
* info
->symbol_entry_size
);
2067 /* The sh linker needs to keep track of the number of relocs that it
2068 decides to copy as dynamic relocs in check_relocs for each symbol.
2069 This is so that it can later discard them if they are found to be
2070 unnecessary. We store the information in a field extending the
2071 regular ELF linker hash table. */
2073 struct elf_sh_dyn_relocs
2075 struct elf_sh_dyn_relocs
*next
;
2077 /* The input section of the reloc. */
2080 /* Total number of relocs copied for the input section. */
2081 bfd_size_type count
;
2083 /* Number of pc-relative relocs copied for the input section. */
2084 bfd_size_type pc_count
;
2087 /* sh ELF linker hash entry. */
2089 struct elf_sh_link_hash_entry
2091 struct elf_link_hash_entry root
;
2093 #ifdef INCLUDE_SHMEDIA
2096 bfd_signed_vma refcount
;
2101 /* Track dynamic relocs copied for this symbol. */
2102 struct elf_sh_dyn_relocs
*dyn_relocs
;
2104 bfd_signed_vma gotplt_refcount
;
2107 GOT_UNKNOWN
= 0, GOT_NORMAL
, GOT_TLS_GD
, GOT_TLS_IE
2111 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2113 struct sh_elf_obj_tdata
2115 struct elf_obj_tdata root
;
2117 /* tls_type for each local got entry. */
2118 char *local_got_tls_type
;
2121 #define sh_elf_tdata(abfd) \
2122 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2124 #define sh_elf_local_got_tls_type(abfd) \
2125 (sh_elf_tdata (abfd)->local_got_tls_type)
2127 /* Override the generic function because we need to store sh_elf_obj_tdata
2128 as the specific tdata. */
2131 sh_elf_mkobject (bfd
*abfd
)
2133 if (abfd
->tdata
.any
== NULL
)
2135 bfd_size_type amt
= sizeof (struct sh_elf_obj_tdata
);
2136 abfd
->tdata
.any
= bfd_zalloc (abfd
, amt
);
2137 if (abfd
->tdata
.any
== NULL
)
2140 return bfd_elf_mkobject (abfd
);
2143 /* sh ELF linker hash table. */
2145 struct elf_sh_link_hash_table
2147 struct elf_link_hash_table root
;
2149 /* Short-cuts to get to dynamic linker sections. */
2158 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2161 /* Small local sym to section mapping cache. */
2162 struct sym_sec_cache sym_sec
;
2164 /* A counter or offset to track a TLS got entry. */
2167 bfd_signed_vma refcount
;
2171 /* The type of PLT to use. */
2172 const struct elf_sh_plt_info
*plt_info
;
2174 /* True if the target system is VxWorks. */
2175 bfd_boolean vxworks_p
;
2178 /* Traverse an sh ELF linker hash table. */
2180 #define sh_elf_link_hash_traverse(table, func, info) \
2181 (elf_link_hash_traverse \
2183 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2186 /* Get the sh ELF linker hash table from a link_info structure. */
2188 #define sh_elf_hash_table(p) \
2189 ((struct elf_sh_link_hash_table *) ((p)->hash))
2191 /* Create an entry in an sh ELF linker hash table. */
2193 static struct bfd_hash_entry
*
2194 sh_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
2195 struct bfd_hash_table
*table
,
2198 struct elf_sh_link_hash_entry
*ret
=
2199 (struct elf_sh_link_hash_entry
*) entry
;
2201 /* Allocate the structure if it has not already been allocated by a
2203 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2204 ret
= ((struct elf_sh_link_hash_entry
*)
2205 bfd_hash_allocate (table
,
2206 sizeof (struct elf_sh_link_hash_entry
)));
2207 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2208 return (struct bfd_hash_entry
*) ret
;
2210 /* Call the allocation method of the superclass. */
2211 ret
= ((struct elf_sh_link_hash_entry
*)
2212 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2214 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2216 ret
->dyn_relocs
= NULL
;
2217 ret
->gotplt_refcount
= 0;
2218 #ifdef INCLUDE_SHMEDIA
2219 ret
->datalabel_got
.refcount
= ret
->root
.got
.refcount
;
2221 ret
->tls_type
= GOT_UNKNOWN
;
2224 return (struct bfd_hash_entry
*) ret
;
2227 /* Create an sh ELF linker hash table. */
2229 static struct bfd_link_hash_table
*
2230 sh_elf_link_hash_table_create (bfd
*abfd
)
2232 struct elf_sh_link_hash_table
*ret
;
2233 bfd_size_type amt
= sizeof (struct elf_sh_link_hash_table
);
2235 ret
= (struct elf_sh_link_hash_table
*) bfd_malloc (amt
);
2236 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2239 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2240 sh_elf_link_hash_newfunc
,
2241 sizeof (struct elf_sh_link_hash_entry
)))
2248 ret
->sgotplt
= NULL
;
2249 ret
->srelgot
= NULL
;
2251 ret
->srelplt
= NULL
;
2252 ret
->sdynbss
= NULL
;
2253 ret
->srelbss
= NULL
;
2254 ret
->srelplt2
= NULL
;
2255 ret
->sym_sec
.abfd
= NULL
;
2256 ret
->tls_ldm_got
.refcount
= 0;
2257 ret
->plt_info
= NULL
;
2258 ret
->vxworks_p
= vxworks_object_p (abfd
);
2260 return &ret
->root
.root
;
2263 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2264 shortcuts to them in our hash table. */
2267 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
2269 struct elf_sh_link_hash_table
*htab
;
2271 if (! _bfd_elf_create_got_section (dynobj
, info
))
2274 htab
= sh_elf_hash_table (info
);
2275 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
2276 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
2277 if (! htab
->sgot
|| ! htab
->sgotplt
)
2280 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rela.got",
2281 (SEC_ALLOC
| SEC_LOAD
2284 | SEC_LINKER_CREATED
2286 if (htab
->srelgot
== NULL
2287 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 2))
2292 /* Create dynamic sections when linking against a dynamic object. */
2295 sh_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2297 struct elf_sh_link_hash_table
*htab
;
2298 flagword flags
, pltflags
;
2299 register asection
*s
;
2300 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2303 switch (bed
->s
->arch_size
)
2314 bfd_set_error (bfd_error_bad_value
);
2318 htab
= sh_elf_hash_table (info
);
2319 if (htab
->root
.dynamic_sections_created
)
2322 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2323 .rel[a].bss sections. */
2325 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2326 | SEC_LINKER_CREATED
);
2329 pltflags
|= SEC_CODE
;
2330 if (bed
->plt_not_loaded
)
2331 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2332 if (bed
->plt_readonly
)
2333 pltflags
|= SEC_READONLY
;
2335 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
2338 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
2341 if (bed
->want_plt_sym
)
2343 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2345 struct elf_link_hash_entry
*h
;
2346 struct bfd_link_hash_entry
*bh
= NULL
;
2348 if (! (_bfd_generic_link_add_one_symbol
2349 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2350 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
2351 get_elf_backend_data (abfd
)->collect
, &bh
)))
2354 h
= (struct elf_link_hash_entry
*) bh
;
2356 h
->type
= STT_OBJECT
;
2357 htab
->root
.hplt
= h
;
2360 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2364 s
= bfd_make_section_with_flags (abfd
,
2365 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt",
2366 flags
| SEC_READONLY
);
2369 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2372 if (htab
->sgot
== NULL
2373 && !create_got_section (abfd
, info
))
2377 const char *secname
;
2382 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2384 secflags
= bfd_get_section_flags (abfd
, sec
);
2385 if ((secflags
& (SEC_DATA
| SEC_LINKER_CREATED
))
2386 || ((secflags
& SEC_HAS_CONTENTS
) != SEC_HAS_CONTENTS
))
2388 secname
= bfd_get_section_name (abfd
, sec
);
2389 relname
= (char *) bfd_malloc ((bfd_size_type
) strlen (secname
) + 6);
2390 strcpy (relname
, ".rela");
2391 strcat (relname
, secname
);
2392 if (bfd_get_section_by_name (abfd
, secname
))
2394 s
= bfd_make_section_with_flags (abfd
, relname
,
2395 flags
| SEC_READONLY
);
2397 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2402 if (bed
->want_dynbss
)
2404 /* The .dynbss section is a place to put symbols which are defined
2405 by dynamic objects, are referenced by regular objects, and are
2406 not functions. We must allocate space for them in the process
2407 image and use a R_*_COPY reloc to tell the dynamic linker to
2408 initialize them at run time. The linker script puts the .dynbss
2409 section into the .bss section of the final image. */
2410 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
2411 SEC_ALLOC
| SEC_LINKER_CREATED
);
2416 /* The .rel[a].bss section holds copy relocs. This section is not
2417 normally needed. We need to create it here, though, so that the
2418 linker will map it to an output section. We can't just create it
2419 only if we need it, because we will not know whether we need it
2420 until we have seen all the input files, and the first time the
2421 main linker code calls BFD after examining all the input files
2422 (size_dynamic_sections) the input sections have already been
2423 mapped to the output sections. If the section turns out not to
2424 be needed, we can discard it later. We will never need this
2425 section when generating a shared object, since they do not use
2429 s
= bfd_make_section_with_flags (abfd
,
2430 (bed
->default_use_rela_p
2431 ? ".rela.bss" : ".rel.bss"),
2432 flags
| SEC_READONLY
);
2435 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2440 if (htab
->vxworks_p
)
2442 if (!elf_vxworks_create_dynamic_sections (abfd
, info
, &htab
->srelplt2
))
2449 /* Adjust a symbol defined by a dynamic object and referenced by a
2450 regular object. The current definition is in some section of the
2451 dynamic object, but we're not including those sections. We have to
2452 change the definition to something the rest of the link can
2456 sh_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2457 struct elf_link_hash_entry
*h
)
2459 struct elf_sh_link_hash_table
*htab
;
2460 struct elf_sh_link_hash_entry
*eh
;
2461 struct elf_sh_dyn_relocs
*p
;
2463 unsigned int power_of_two
;
2465 htab
= sh_elf_hash_table (info
);
2467 /* Make sure we know what is going on here. */
2468 BFD_ASSERT (htab
->root
.dynobj
!= NULL
2470 || h
->u
.weakdef
!= NULL
2473 && !h
->def_regular
)));
2475 /* If this is a function, put it in the procedure linkage table. We
2476 will fill in the contents of the procedure linkage table later,
2477 when we know the address of the .got section. */
2478 if (h
->type
== STT_FUNC
2481 if (h
->plt
.refcount
<= 0
2482 || SYMBOL_CALLS_LOCAL (info
, h
)
2483 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2484 && h
->root
.type
== bfd_link_hash_undefweak
))
2486 /* This case can occur if we saw a PLT reloc in an input
2487 file, but the symbol was never referred to by a dynamic
2488 object. In such a case, we don't actually need to build
2489 a procedure linkage table, and we can just do a REL32
2491 h
->plt
.offset
= (bfd_vma
) -1;
2498 h
->plt
.offset
= (bfd_vma
) -1;
2500 /* If this is a weak symbol, and there is a real definition, the
2501 processor independent code will have arranged for us to see the
2502 real definition first, and we can just use the same value. */
2503 if (h
->u
.weakdef
!= NULL
)
2505 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
2506 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
2507 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
2508 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
2509 if (info
->nocopyreloc
)
2510 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
2514 /* This is a reference to a symbol defined by a dynamic object which
2515 is not a function. */
2517 /* If we are creating a shared library, we must presume that the
2518 only references to the symbol are via the global offset table.
2519 For such cases we need not do anything here; the relocations will
2520 be handled correctly by relocate_section. */
2524 /* If there are no references to this symbol that do not use the
2525 GOT, we don't need to generate a copy reloc. */
2526 if (!h
->non_got_ref
)
2529 /* If -z nocopyreloc was given, we won't generate them either. */
2530 if (info
->nocopyreloc
)
2536 eh
= (struct elf_sh_link_hash_entry
*) h
;
2537 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2539 s
= p
->sec
->output_section
;
2540 if (s
!= NULL
&& (s
->flags
& (SEC_READONLY
| SEC_HAS_CONTENTS
)) != 0)
2544 /* If we didn't find any dynamic relocs in sections which needs the
2545 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2555 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
2556 h
->root
.root
.string
);
2560 /* We must allocate the symbol in our .dynbss section, which will
2561 become part of the .bss section of the executable. There will be
2562 an entry for this symbol in the .dynsym section. The dynamic
2563 object will contain position independent code, so all references
2564 from the dynamic object to this symbol will go through the global
2565 offset table. The dynamic linker will use the .dynsym entry to
2566 determine the address it must put in the global offset table, so
2567 both the dynamic object and the regular object will refer to the
2568 same memory location for the variable. */
2571 BFD_ASSERT (s
!= NULL
);
2573 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2574 copy the initial value out of the dynamic object and into the
2575 runtime process image. We need to remember the offset into the
2576 .rela.bss section we are going to use. */
2577 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
2581 srel
= htab
->srelbss
;
2582 BFD_ASSERT (srel
!= NULL
);
2583 srel
->size
+= sizeof (Elf32_External_Rela
);
2587 /* We need to figure out the alignment required for this symbol. I
2588 have no idea how ELF linkers handle this. */
2589 power_of_two
= bfd_log2 (h
->size
);
2590 if (power_of_two
> 3)
2593 /* Apply the required alignment. */
2594 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
) (1 << power_of_two
));
2595 if (power_of_two
> bfd_get_section_alignment (htab
->root
.dynobj
, s
))
2597 if (! bfd_set_section_alignment (htab
->root
.dynobj
, s
, power_of_two
))
2601 /* Define the symbol as being at this point in the section. */
2602 h
->root
.u
.def
.section
= s
;
2603 h
->root
.u
.def
.value
= s
->size
;
2605 /* Increment the section size to make room for the symbol. */
2611 /* Allocate space in .plt, .got and associated reloc sections for
2615 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2617 struct bfd_link_info
*info
;
2618 struct elf_sh_link_hash_table
*htab
;
2619 struct elf_sh_link_hash_entry
*eh
;
2620 struct elf_sh_dyn_relocs
*p
;
2622 if (h
->root
.type
== bfd_link_hash_indirect
)
2625 if (h
->root
.type
== bfd_link_hash_warning
)
2626 /* When warning symbols are created, they **replace** the "real"
2627 entry in the hash table, thus we never get to see the real
2628 symbol in a hash traversal. So look at it now. */
2629 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2631 info
= (struct bfd_link_info
*) inf
;
2632 htab
= sh_elf_hash_table (info
);
2634 eh
= (struct elf_sh_link_hash_entry
*) h
;
2635 if ((h
->got
.refcount
> 0
2637 && eh
->gotplt_refcount
> 0)
2639 /* The symbol has been forced local, or we have some direct got refs,
2640 so treat all the gotplt refs as got refs. */
2641 h
->got
.refcount
+= eh
->gotplt_refcount
;
2642 if (h
->plt
.refcount
>= eh
->gotplt_refcount
)
2643 h
->plt
.refcount
-= eh
->gotplt_refcount
;
2646 if (htab
->root
.dynamic_sections_created
2647 && h
->plt
.refcount
> 0
2648 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2649 || h
->root
.type
!= bfd_link_hash_undefweak
))
2651 /* Make sure this symbol is output as a dynamic symbol.
2652 Undefined weak syms won't yet be marked as dynamic. */
2653 if (h
->dynindx
== -1
2654 && !h
->forced_local
)
2656 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2661 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2663 asection
*s
= htab
->splt
;
2665 /* If this is the first .plt entry, make room for the special
2668 s
->size
+= htab
->plt_info
->plt0_entry_size
;
2670 h
->plt
.offset
= s
->size
;
2672 /* If this symbol is not defined in a regular file, and we are
2673 not generating a shared library, then set the symbol to this
2674 location in the .plt. This is required to make function
2675 pointers compare as equal between the normal executable and
2676 the shared library. */
2680 h
->root
.u
.def
.section
= s
;
2681 h
->root
.u
.def
.value
= h
->plt
.offset
;
2684 /* Make room for this entry. */
2685 s
->size
+= htab
->plt_info
->symbol_entry_size
;
2687 /* We also need to make an entry in the .got.plt section, which
2688 will be placed in the .got section by the linker script. */
2689 htab
->sgotplt
->size
+= 4;
2691 /* We also need to make an entry in the .rel.plt section. */
2692 htab
->srelplt
->size
+= sizeof (Elf32_External_Rela
);
2694 if (htab
->vxworks_p
&& !info
->shared
)
2696 /* VxWorks executables have a second set of relocations
2697 for each PLT entry. They go in a separate relocation
2698 section, which is processed by the kernel loader. */
2700 /* There is a relocation for the initial PLT entry:
2701 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2702 if (h
->plt
.offset
== htab
->plt_info
->plt0_entry_size
)
2703 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
);
2705 /* There are two extra relocations for each subsequent
2706 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2707 and an R_SH_DIR32 relocation for the PLT entry. */
2708 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
) * 2;
2713 h
->plt
.offset
= (bfd_vma
) -1;
2719 h
->plt
.offset
= (bfd_vma
) -1;
2723 if (h
->got
.refcount
> 0)
2727 int tls_type
= sh_elf_hash_entry (h
)->tls_type
;
2729 /* Make sure this symbol is output as a dynamic symbol.
2730 Undefined weak syms won't yet be marked as dynamic. */
2731 if (h
->dynindx
== -1
2732 && !h
->forced_local
)
2734 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2739 h
->got
.offset
= s
->size
;
2741 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2742 if (tls_type
== GOT_TLS_GD
)
2744 dyn
= htab
->root
.dynamic_sections_created
;
2745 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2746 R_SH_TLS_GD needs one if local symbol and two if global. */
2747 if ((tls_type
== GOT_TLS_GD
&& h
->dynindx
== -1)
2748 || (tls_type
== GOT_TLS_IE
&& dyn
))
2749 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2750 else if (tls_type
== GOT_TLS_GD
)
2751 htab
->srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
2752 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2753 || h
->root
.type
!= bfd_link_hash_undefweak
)
2755 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
2756 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2759 h
->got
.offset
= (bfd_vma
) -1;
2761 #ifdef INCLUDE_SHMEDIA
2762 if (eh
->datalabel_got
.refcount
> 0)
2767 /* Make sure this symbol is output as a dynamic symbol.
2768 Undefined weak syms won't yet be marked as dynamic. */
2769 if (h
->dynindx
== -1
2770 && !h
->forced_local
)
2772 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2777 eh
->datalabel_got
.offset
= s
->size
;
2779 dyn
= htab
->root
.dynamic_sections_created
;
2780 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
))
2781 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2784 eh
->datalabel_got
.offset
= (bfd_vma
) -1;
2787 if (eh
->dyn_relocs
== NULL
)
2790 /* In the shared -Bsymbolic case, discard space allocated for
2791 dynamic pc-relative relocs against symbols which turn out to be
2792 defined in regular objects. For the normal shared case, discard
2793 space for pc-relative relocs that have become local due to symbol
2794 visibility changes. */
2798 if (SYMBOL_CALLS_LOCAL (info
, h
))
2800 struct elf_sh_dyn_relocs
**pp
;
2802 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2804 p
->count
-= p
->pc_count
;
2813 /* Also discard relocs on undefined weak syms with non-default
2815 if (eh
->dyn_relocs
!= NULL
2816 && h
->root
.type
== bfd_link_hash_undefweak
)
2818 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2819 eh
->dyn_relocs
= NULL
;
2821 /* Make sure undefined weak symbols are output as a dynamic
2823 else if (h
->dynindx
== -1
2824 && !h
->forced_local
)
2826 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2833 /* For the non-shared case, discard space for relocs against
2834 symbols which turn out to need copy relocs or are not
2840 || (htab
->root
.dynamic_sections_created
2841 && (h
->root
.type
== bfd_link_hash_undefweak
2842 || h
->root
.type
== bfd_link_hash_undefined
))))
2844 /* Make sure this symbol is output as a dynamic symbol.
2845 Undefined weak syms won't yet be marked as dynamic. */
2846 if (h
->dynindx
== -1
2847 && !h
->forced_local
)
2849 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2853 /* If that succeeded, we know we'll be keeping all the
2855 if (h
->dynindx
!= -1)
2859 eh
->dyn_relocs
= NULL
;
2864 /* Finally, allocate space. */
2865 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2867 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
2868 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
2874 /* Find any dynamic relocs that apply to read-only sections. */
2877 readonly_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2879 struct elf_sh_link_hash_entry
*eh
;
2880 struct elf_sh_dyn_relocs
*p
;
2882 if (h
->root
.type
== bfd_link_hash_warning
)
2883 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2885 eh
= (struct elf_sh_link_hash_entry
*) h
;
2886 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2888 asection
*s
= p
->sec
->output_section
;
2890 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2892 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
2894 info
->flags
|= DF_TEXTREL
;
2896 /* Not an error, just cut short the traversal. */
2903 /* This function is called after all the input files have been read,
2904 and the input sections have been assigned to output sections.
2905 It's a convenient place to determine the PLT style. */
2908 sh_elf_always_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
2910 sh_elf_hash_table (info
)->plt_info
= get_plt_info (output_bfd
, info
->shared
);
2914 /* Set the sizes of the dynamic sections. */
2917 sh_elf_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2918 struct bfd_link_info
*info
)
2920 struct elf_sh_link_hash_table
*htab
;
2926 htab
= sh_elf_hash_table (info
);
2927 dynobj
= htab
->root
.dynobj
;
2928 BFD_ASSERT (dynobj
!= NULL
);
2930 if (htab
->root
.dynamic_sections_created
)
2932 /* Set the contents of the .interp section to the interpreter. */
2933 if (info
->executable
)
2935 s
= bfd_get_section_by_name (dynobj
, ".interp");
2936 BFD_ASSERT (s
!= NULL
);
2937 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
2938 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2942 /* Set up .got offsets for local syms, and space for local dynamic
2944 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2946 bfd_signed_vma
*local_got
;
2947 bfd_signed_vma
*end_local_got
;
2948 char *local_tls_type
;
2949 bfd_size_type locsymcount
;
2950 Elf_Internal_Shdr
*symtab_hdr
;
2953 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
2956 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2958 struct elf_sh_dyn_relocs
*p
;
2960 for (p
= ((struct elf_sh_dyn_relocs
*)
2961 elf_section_data (s
)->local_dynrel
);
2965 if (! bfd_is_abs_section (p
->sec
)
2966 && bfd_is_abs_section (p
->sec
->output_section
))
2968 /* Input section has been discarded, either because
2969 it is a copy of a linkonce section or due to
2970 linker script /DISCARD/, so we'll be discarding
2973 else if (p
->count
!= 0)
2975 srel
= elf_section_data (p
->sec
)->sreloc
;
2976 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
2977 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
2978 info
->flags
|= DF_TEXTREL
;
2983 local_got
= elf_local_got_refcounts (ibfd
);
2987 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
2988 locsymcount
= symtab_hdr
->sh_info
;
2989 #ifdef INCLUDE_SHMEDIA
2990 /* Count datalabel local GOT. */
2993 end_local_got
= local_got
+ locsymcount
;
2994 local_tls_type
= sh_elf_local_got_tls_type (ibfd
);
2996 srel
= htab
->srelgot
;
2997 for (; local_got
< end_local_got
; ++local_got
)
3001 *local_got
= s
->size
;
3003 if (*local_tls_type
== GOT_TLS_GD
)
3006 srel
->size
+= sizeof (Elf32_External_Rela
);
3009 *local_got
= (bfd_vma
) -1;
3014 if (htab
->tls_ldm_got
.refcount
> 0)
3016 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3018 htab
->tls_ldm_got
.offset
= htab
->sgot
->size
;
3019 htab
->sgot
->size
+= 8;
3020 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
3023 htab
->tls_ldm_got
.offset
= -1;
3025 /* Allocate global sym .plt and .got entries, and space for global
3026 sym dynamic relocs. */
3027 elf_link_hash_traverse (&htab
->root
, allocate_dynrelocs
, info
);
3029 /* We now have determined the sizes of the various dynamic sections.
3030 Allocate memory for them. */
3032 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
3034 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
3039 || s
== htab
->sgotplt
3040 || s
== htab
->sdynbss
)
3042 /* Strip this section if we don't need it; see the
3045 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
3047 if (s
->size
!= 0 && s
!= htab
->srelplt
&& s
!= htab
->srelplt2
)
3050 /* We use the reloc_count field as a counter if we need
3051 to copy relocs into the output file. */
3056 /* It's not one of our sections, so don't allocate space. */
3062 /* If we don't need this section, strip it from the
3063 output file. This is mostly to handle .rela.bss and
3064 .rela.plt. We must create both sections in
3065 create_dynamic_sections, because they must be created
3066 before the linker maps input sections to output
3067 sections. The linker does that before
3068 adjust_dynamic_symbol is called, and it is that
3069 function which decides whether anything needs to go
3070 into these sections. */
3072 s
->flags
|= SEC_EXCLUDE
;
3076 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3079 /* Allocate memory for the section contents. We use bfd_zalloc
3080 here in case unused entries are not reclaimed before the
3081 section's contents are written out. This should not happen,
3082 but this way if it does, we get a R_SH_NONE reloc instead
3084 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
3085 if (s
->contents
== NULL
)
3089 if (htab
->root
.dynamic_sections_created
)
3091 /* Add some entries to the .dynamic section. We fill in the
3092 values later, in sh_elf_finish_dynamic_sections, but we
3093 must add the entries now so that we get the correct size for
3094 the .dynamic section. The DT_DEBUG entry is filled in by the
3095 dynamic linker and used by the debugger. */
3096 #define add_dynamic_entry(TAG, VAL) \
3097 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3099 if (info
->executable
)
3101 if (! add_dynamic_entry (DT_DEBUG
, 0))
3105 if (htab
->splt
->size
!= 0)
3107 if (! add_dynamic_entry (DT_PLTGOT
, 0)
3108 || ! add_dynamic_entry (DT_PLTRELSZ
, 0)
3109 || ! add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3110 || ! add_dynamic_entry (DT_JMPREL
, 0))
3116 if (! add_dynamic_entry (DT_RELA
, 0)
3117 || ! add_dynamic_entry (DT_RELASZ
, 0)
3118 || ! add_dynamic_entry (DT_RELAENT
,
3119 sizeof (Elf32_External_Rela
)))
3122 /* If any dynamic relocs apply to a read-only section,
3123 then we need a DT_TEXTREL entry. */
3124 if ((info
->flags
& DF_TEXTREL
) == 0)
3125 elf_link_hash_traverse (&htab
->root
, readonly_dynrelocs
, info
);
3127 if ((info
->flags
& DF_TEXTREL
) != 0)
3129 if (! add_dynamic_entry (DT_TEXTREL
, 0))
3134 #undef add_dynamic_entry
3139 /* Relocate an SH ELF section. */
3142 sh_elf_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
3143 bfd
*input_bfd
, asection
*input_section
,
3144 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
3145 Elf_Internal_Sym
*local_syms
,
3146 asection
**local_sections
)
3148 struct elf_sh_link_hash_table
*htab
;
3149 Elf_Internal_Shdr
*symtab_hdr
;
3150 struct elf_link_hash_entry
**sym_hashes
;
3151 Elf_Internal_Rela
*rel
, *relend
;
3153 bfd_vma
*local_got_offsets
;
3160 htab
= sh_elf_hash_table (info
);
3161 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3162 sym_hashes
= elf_sym_hashes (input_bfd
);
3163 dynobj
= htab
->root
.dynobj
;
3164 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3167 sgotplt
= htab
->sgotplt
;
3173 relend
= relocs
+ input_section
->reloc_count
;
3174 for (; rel
< relend
; rel
++)
3177 reloc_howto_type
*howto
;
3178 unsigned long r_symndx
;
3179 Elf_Internal_Sym
*sym
;
3181 struct elf_link_hash_entry
*h
;
3183 bfd_vma addend
= (bfd_vma
) 0;
3184 bfd_reloc_status_type r
;
3185 int seen_stt_datalabel
= 0;
3189 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3191 r_type
= ELF32_R_TYPE (rel
->r_info
);
3193 /* Many of the relocs are only used for relaxing, and are
3194 handled entirely by the relaxation code. */
3195 if (r_type
>= (int) R_SH_GNU_VTINHERIT
3196 && r_type
<= (int) R_SH_LABEL
)
3198 if (r_type
== (int) R_SH_NONE
)
3202 || r_type
>= R_SH_max
3203 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
3204 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
3205 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_3
3206 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_3
)
3207 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_4
3208 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_4
)
3209 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_5
3210 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_5
)
3211 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
3212 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
))
3214 bfd_set_error (bfd_error_bad_value
);
3218 howto
= get_howto_table (output_bfd
) + r_type
;
3220 /* For relocs that aren't partial_inplace, we get the addend from
3222 if (! howto
->partial_inplace
)
3223 addend
= rel
->r_addend
;
3228 if (r_symndx
< symtab_hdr
->sh_info
)
3230 sym
= local_syms
+ r_symndx
;
3231 sec
= local_sections
[r_symndx
];
3232 relocation
= (sec
->output_section
->vma
3233 + sec
->output_offset
3235 /* A local symbol never has STO_SH5_ISA32, so we don't need
3236 datalabel processing here. Make sure this does not change
3238 if ((sym
->st_other
& STO_SH5_ISA32
) != 0)
3239 ((*info
->callbacks
->reloc_dangerous
)
3241 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3242 input_bfd
, input_section
, rel
->r_offset
));
3244 if (sec
!= NULL
&& elf_discarded_section (sec
))
3245 /* Handled below. */
3247 else if (info
->relocatable
)
3249 /* This is a relocatable link. We don't have to change
3250 anything, unless the reloc is against a section symbol,
3251 in which case we have to adjust according to where the
3252 section symbol winds up in the output section. */
3253 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3255 if (! howto
->partial_inplace
)
3257 /* For relocations with the addend in the
3258 relocation, we need just to update the addend.
3259 All real relocs are of type partial_inplace; this
3260 code is mostly for completeness. */
3261 rel
->r_addend
+= sec
->output_offset
;
3266 /* Relocs of type partial_inplace need to pick up the
3267 contents in the contents and add the offset resulting
3268 from the changed location of the section symbol.
3269 Using _bfd_final_link_relocate (e.g. goto
3270 final_link_relocate) here would be wrong, because
3271 relocations marked pc_relative would get the current
3272 location subtracted, and we must only do that at the
3274 r
= _bfd_relocate_contents (howto
, input_bfd
,
3277 contents
+ rel
->r_offset
);
3278 goto relocation_done
;
3283 else if (! howto
->partial_inplace
)
3285 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3286 addend
= rel
->r_addend
;
3288 else if ((sec
->flags
& SEC_MERGE
)
3289 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3293 if (howto
->rightshift
|| howto
->src_mask
!= 0xffffffff)
3295 (*_bfd_error_handler
)
3296 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3297 input_bfd
, input_section
,
3298 (long) rel
->r_offset
, howto
->name
);
3302 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3305 _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
)
3307 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
3308 bfd_put_32 (input_bfd
, addend
, contents
+ rel
->r_offset
);
3314 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3317 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3318 while (h
->root
.type
== bfd_link_hash_indirect
3319 || h
->root
.type
== bfd_link_hash_warning
)
3321 #ifdef INCLUDE_SHMEDIA
3322 /* If the reference passes a symbol marked with
3323 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
3325 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
3327 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3329 if (h
->root
.type
== bfd_link_hash_defined
3330 || h
->root
.type
== bfd_link_hash_defweak
)
3334 dyn
= htab
->root
.dynamic_sections_created
;
3335 sec
= h
->root
.u
.def
.section
;
3336 /* In these cases, we don't need the relocation value.
3337 We check specially because in some obscure cases
3338 sec->output_section will be NULL. */
3339 if (r_type
== R_SH_GOTPC
3340 || r_type
== R_SH_GOTPC_LOW16
3341 || r_type
== R_SH_GOTPC_MEDLOW16
3342 || r_type
== R_SH_GOTPC_MEDHI16
3343 || r_type
== R_SH_GOTPC_HI16
3344 || ((r_type
== R_SH_PLT32
3345 || r_type
== R_SH_PLT_LOW16
3346 || r_type
== R_SH_PLT_MEDLOW16
3347 || r_type
== R_SH_PLT_MEDHI16
3348 || r_type
== R_SH_PLT_HI16
)
3349 && h
->plt
.offset
!= (bfd_vma
) -1)
3350 || ((r_type
== R_SH_GOT32
3351 || r_type
== R_SH_GOT_LOW16
3352 || r_type
== R_SH_GOT_MEDLOW16
3353 || r_type
== R_SH_GOT_MEDHI16
3354 || r_type
== R_SH_GOT_HI16
)
3355 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3357 || (! info
->symbolic
&& h
->dynindx
!= -1)
3358 || !h
->def_regular
))
3359 /* The cases above are those in which relocation is
3360 overwritten in the switch block below. The cases
3361 below are those in which we must defer relocation
3362 to run-time, because we can't resolve absolute
3363 addresses when creating a shared library. */
3365 && ((! info
->symbolic
&& h
->dynindx
!= -1)
3367 && ((r_type
== R_SH_DIR32
3368 && !h
->forced_local
)
3369 || (r_type
== R_SH_REL32
3370 && !SYMBOL_CALLS_LOCAL (info
, h
)))
3371 && ((input_section
->flags
& SEC_ALLOC
) != 0
3372 /* DWARF will emit R_SH_DIR32 relocations in its
3373 sections against symbols defined externally
3374 in shared libraries. We can't do anything
3376 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
3377 && h
->def_dynamic
)))
3378 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3379 sections because such sections are not SEC_ALLOC and
3380 thus ld.so will not process them. */
3381 || (sec
->output_section
== NULL
3382 && ((input_section
->flags
& SEC_DEBUGGING
) != 0
3384 || (sec
->output_section
== NULL
3385 && (sh_elf_hash_entry (h
)->tls_type
== GOT_TLS_IE
3386 || sh_elf_hash_entry (h
)->tls_type
== GOT_TLS_GD
)))
3388 else if (sec
->output_section
!= NULL
)
3389 relocation
= ((h
->root
.u
.def
.value
3390 + sec
->output_section
->vma
3391 + sec
->output_offset
)
3392 /* A STO_SH5_ISA32 causes a "bitor 1" to the
3393 symbol value, unless we've seen
3394 STT_DATALABEL on the way to it. */
3395 | ((h
->other
& STO_SH5_ISA32
) != 0
3396 && ! seen_stt_datalabel
));
3397 else if (!info
->relocatable
)
3399 (*_bfd_error_handler
)
3400 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3403 (long) rel
->r_offset
,
3405 h
->root
.root
.string
);
3409 else if (h
->root
.type
== bfd_link_hash_undefweak
)
3411 else if (info
->unresolved_syms_in_objects
== RM_IGNORE
3412 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3414 else if (!info
->relocatable
)
3416 if (! info
->callbacks
->undefined_symbol
3417 (info
, h
->root
.root
.string
, input_bfd
,
3418 input_section
, rel
->r_offset
,
3419 (info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
3420 || ELF_ST_VISIBILITY (h
->other
))))
3425 if (sec
!= NULL
&& elf_discarded_section (sec
))
3427 /* For relocs against symbols from removed linkonce sections,
3428 or sections discarded by a linker script, we just want the
3429 section contents zeroed. Avoid any special processing. */
3430 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
3436 if (info
->relocatable
)
3439 switch ((int) r_type
)
3441 final_link_relocate
:
3442 /* COFF relocs don't use the addend. The addend is used for
3443 R_SH_DIR32 to be compatible with other compilers. */
3444 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3445 contents
, rel
->r_offset
,
3446 relocation
, addend
);
3450 goto final_link_relocate
;
3455 /* If the reloc is against the start of this section, then
3456 the assembler has already taken care of it and the reloc
3457 is here only to assist in relaxing. If the reloc is not
3458 against the start of this section, then it's against an
3459 external symbol and we must deal with it ourselves. */
3460 if (input_section
->output_section
->vma
+ input_section
->output_offset
3463 int disp
= (relocation
3464 - input_section
->output_section
->vma
3465 - input_section
->output_offset
3471 case R_SH_DIR8WPZ
: mask
= 1; break;
3472 case R_SH_DIR8WPL
: mask
= 3; break;
3473 default: mask
= 0; break;
3477 ((*_bfd_error_handler
)
3478 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
3479 input_section
->owner
,
3480 (unsigned long) rel
->r_offset
));
3481 bfd_set_error (bfd_error_bad_value
);
3485 goto final_link_relocate
;
3491 #ifdef INCLUDE_SHMEDIA
3492 if (shmedia_prepare_reloc (info
, input_bfd
, input_section
,
3493 contents
, rel
, &relocation
))
3494 goto final_link_relocate
;
3496 bfd_set_error (bfd_error_bad_value
);
3504 goto final_link_relocate
;
3510 ((*_bfd_error_handler
)
3511 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3512 input_section
->owner
,
3513 (unsigned long) rel
->r_offset
, howto
->name
,
3514 (unsigned long) relocation
));
3515 bfd_set_error (bfd_error_bad_value
);
3518 goto final_link_relocate
;
3525 ((*_bfd_error_handler
)
3526 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3527 input_section
->owner
,
3528 (unsigned long) rel
->r_offset
, howto
->name
,
3529 (unsigned long) relocation
));
3530 bfd_set_error (bfd_error_bad_value
);
3533 goto final_link_relocate
;
3536 if ((signed int)relocation
< -32
3537 || (signed int)relocation
> 32)
3539 ((*_bfd_error_handler
)
3540 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
3541 input_section
->owner
,
3542 (unsigned long) rel
->r_offset
,
3543 (unsigned long) relocation
));
3544 bfd_set_error (bfd_error_bad_value
);
3547 goto final_link_relocate
;
3550 if ((signed int)relocation
< -16
3551 || (signed int)relocation
> 16)
3553 ((*_bfd_error_handler
)
3554 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
3555 input_section
->owner
,
3556 (unsigned long) rel
->r_offset
,
3557 (unsigned long) relocation
));
3558 bfd_set_error (bfd_error_bad_value
);
3561 goto final_link_relocate
;
3565 #ifdef INCLUDE_SHMEDIA
3566 case R_SH_IMM_LOW16_PCREL
:
3567 case R_SH_IMM_MEDLOW16_PCREL
:
3568 case R_SH_IMM_MEDHI16_PCREL
:
3569 case R_SH_IMM_HI16_PCREL
:
3573 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3574 || h
->root
.type
!= bfd_link_hash_undefweak
)
3576 && (input_section
->flags
& SEC_ALLOC
) != 0
3577 && (r_type
== R_SH_DIR32
3578 || !SYMBOL_CALLS_LOCAL (info
, h
)))
3580 Elf_Internal_Rela outrel
;
3582 bfd_boolean skip
, relocate
;
3584 /* When generating a shared object, these relocations
3585 are copied into the output file to be resolved at run
3592 name
= (bfd_elf_string_from_elf_section
3594 elf_elfheader (input_bfd
)->e_shstrndx
,
3595 elf_section_data (input_section
)->rel_hdr
.sh_name
));
3599 BFD_ASSERT (CONST_STRNEQ (name
, ".rela")
3600 && strcmp (bfd_get_section_name (input_bfd
,
3604 sreloc
= bfd_get_section_by_name (dynobj
, name
);
3605 BFD_ASSERT (sreloc
!= NULL
);
3612 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3614 if (outrel
.r_offset
== (bfd_vma
) -1)
3616 else if (outrel
.r_offset
== (bfd_vma
) -2)
3617 skip
= TRUE
, relocate
= TRUE
;
3618 outrel
.r_offset
+= (input_section
->output_section
->vma
3619 + input_section
->output_offset
);
3622 memset (&outrel
, 0, sizeof outrel
);
3623 else if (r_type
== R_SH_REL32
)
3625 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3626 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
3628 = (howto
->partial_inplace
3629 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
3632 #ifdef INCLUDE_SHMEDIA
3633 else if (r_type
== R_SH_IMM_LOW16_PCREL
3634 || r_type
== R_SH_IMM_MEDLOW16_PCREL
3635 || r_type
== R_SH_IMM_MEDHI16_PCREL
3636 || r_type
== R_SH_IMM_HI16_PCREL
)
3638 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3639 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
3640 outrel
.r_addend
= addend
;
3645 /* h->dynindx may be -1 if this symbol was marked to
3648 || ((info
->symbolic
|| h
->dynindx
== -1)
3651 relocate
= howto
->partial_inplace
;
3652 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3656 BFD_ASSERT (h
->dynindx
!= -1);
3657 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
3659 outrel
.r_addend
= relocation
;
3661 += (howto
->partial_inplace
3662 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
3666 loc
= sreloc
->contents
;
3667 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3668 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3670 /* If this reloc is against an external symbol, we do
3671 not want to fiddle with the addend. Otherwise, we
3672 need to include the symbol value so that it becomes
3673 an addend for the dynamic reloc. */
3677 goto final_link_relocate
;
3680 #ifdef INCLUDE_SHMEDIA
3681 case R_SH_GOTPLT_LOW16
:
3682 case R_SH_GOTPLT_MEDLOW16
:
3683 case R_SH_GOTPLT_MEDHI16
:
3684 case R_SH_GOTPLT_HI16
:
3685 case R_SH_GOTPLT10BY4
:
3686 case R_SH_GOTPLT10BY8
:
3688 /* Relocation is to the entry for this symbol in the
3689 procedure linkage table. */
3696 || h
->plt
.offset
== (bfd_vma
) -1
3697 || h
->got
.offset
!= (bfd_vma
) -1)
3700 /* Relocation is to the entry for this symbol in the global
3701 offset table extension for the procedure linkage table. */
3703 BFD_ASSERT (sgotplt
!= NULL
);
3704 relocation
= (sgotplt
->output_offset
3705 + (get_plt_index (htab
->plt_info
, h
->plt
.offset
)
3709 relocation
-= GOT_BIAS
;
3712 goto final_link_relocate
;
3716 #ifdef INCLUDE_SHMEDIA
3717 case R_SH_GOT_LOW16
:
3718 case R_SH_GOT_MEDLOW16
:
3719 case R_SH_GOT_MEDHI16
:
3724 /* Relocation is to the entry for this symbol in the global
3727 BFD_ASSERT (sgot
!= NULL
);
3733 off
= h
->got
.offset
;
3734 #ifdef INCLUDE_SHMEDIA
3735 if (seen_stt_datalabel
)
3737 struct elf_sh_link_hash_entry
*hsh
;
3739 hsh
= (struct elf_sh_link_hash_entry
*)h
;
3740 off
= hsh
->datalabel_got
.offset
;
3743 BFD_ASSERT (off
!= (bfd_vma
) -1);
3745 dyn
= htab
->root
.dynamic_sections_created
;
3746 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3748 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3749 || (ELF_ST_VISIBILITY (h
->other
)
3750 && h
->root
.type
== bfd_link_hash_undefweak
))
3752 /* This is actually a static link, or it is a
3753 -Bsymbolic link and the symbol is defined
3754 locally, or the symbol was forced to be local
3755 because of a version file. We must initialize
3756 this entry in the global offset table. Since the
3757 offset must always be a multiple of 4, we use the
3758 least significant bit to record whether we have
3759 initialized it already.
3761 When doing a dynamic link, we create a .rela.got
3762 relocation entry to initialize the value. This
3763 is done in the finish_dynamic_symbol routine. */
3768 bfd_put_32 (output_bfd
, relocation
,
3769 sgot
->contents
+ off
);
3770 #ifdef INCLUDE_SHMEDIA
3771 if (seen_stt_datalabel
)
3773 struct elf_sh_link_hash_entry
*hsh
;
3775 hsh
= (struct elf_sh_link_hash_entry
*)h
;
3776 hsh
->datalabel_got
.offset
|= 1;
3784 relocation
= sgot
->output_offset
+ off
;
3788 #ifdef INCLUDE_SHMEDIA
3791 BFD_ASSERT (local_got_offsets
!= NULL
3792 && (local_got_offsets
[symtab_hdr
->sh_info
3796 off
= local_got_offsets
[symtab_hdr
->sh_info
3802 BFD_ASSERT (local_got_offsets
!= NULL
3803 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
3805 off
= local_got_offsets
[r_symndx
];
3806 #ifdef INCLUDE_SHMEDIA
3810 /* The offset must always be a multiple of 4. We use
3811 the least significant bit to record whether we have
3812 already generated the necessary reloc. */
3817 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
3821 Elf_Internal_Rela outrel
;
3824 if (srelgot
== NULL
)
3826 srelgot
= bfd_get_section_by_name (dynobj
,
3828 BFD_ASSERT (srelgot
!= NULL
);
3831 outrel
.r_offset
= (sgot
->output_section
->vma
3832 + sgot
->output_offset
3834 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3835 outrel
.r_addend
= relocation
;
3836 loc
= srelgot
->contents
;
3837 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3838 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3841 #ifdef INCLUDE_SHMEDIA
3843 local_got_offsets
[symtab_hdr
->sh_info
+ r_symndx
] |= 1;
3846 local_got_offsets
[r_symndx
] |= 1;
3849 relocation
= sgot
->output_offset
+ off
;
3853 relocation
-= GOT_BIAS
;
3856 goto final_link_relocate
;
3859 #ifdef INCLUDE_SHMEDIA
3860 case R_SH_GOTOFF_LOW16
:
3861 case R_SH_GOTOFF_MEDLOW16
:
3862 case R_SH_GOTOFF_MEDHI16
:
3863 case R_SH_GOTOFF_HI16
:
3865 /* Relocation is relative to the start of the global offset
3868 BFD_ASSERT (sgot
!= NULL
);
3870 /* Note that sgot->output_offset is not involved in this
3871 calculation. We always want the start of .got. If we
3872 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3873 permitted by the ABI, we might have to change this
3875 relocation
-= sgot
->output_section
->vma
;
3878 relocation
-= GOT_BIAS
;
3881 addend
= rel
->r_addend
;
3883 goto final_link_relocate
;
3886 #ifdef INCLUDE_SHMEDIA
3887 case R_SH_GOTPC_LOW16
:
3888 case R_SH_GOTPC_MEDLOW16
:
3889 case R_SH_GOTPC_MEDHI16
:
3890 case R_SH_GOTPC_HI16
:
3892 /* Use global offset table as symbol value. */
3894 BFD_ASSERT (sgot
!= NULL
);
3895 relocation
= sgot
->output_section
->vma
;
3898 relocation
+= GOT_BIAS
;
3901 addend
= rel
->r_addend
;
3903 goto final_link_relocate
;
3906 #ifdef INCLUDE_SHMEDIA
3907 case R_SH_PLT_LOW16
:
3908 case R_SH_PLT_MEDLOW16
:
3909 case R_SH_PLT_MEDHI16
:
3912 /* Relocation is to the entry for this symbol in the
3913 procedure linkage table. */
3915 /* Resolve a PLT reloc against a local symbol directly,
3916 without using the procedure linkage table. */
3918 goto final_link_relocate
;
3920 if (h
->forced_local
)
3921 goto final_link_relocate
;
3923 if (h
->plt
.offset
== (bfd_vma
) -1)
3925 /* We didn't make a PLT entry for this symbol. This
3926 happens when statically linking PIC code, or when
3927 using -Bsymbolic. */
3928 goto final_link_relocate
;
3931 BFD_ASSERT (splt
!= NULL
);
3932 relocation
= (splt
->output_section
->vma
3933 + splt
->output_offset
3936 #ifdef INCLUDE_SHMEDIA
3940 addend
= rel
->r_addend
;
3942 goto final_link_relocate
;
3944 case R_SH_LOOP_START
:
3946 static bfd_vma start
, end
;
3948 start
= (relocation
+ rel
->r_addend
3949 - (sec
->output_section
->vma
+ sec
->output_offset
));
3950 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3951 rel
->r_offset
, sec
, start
, end
);
3955 end
= (relocation
+ rel
->r_addend
3956 - (sec
->output_section
->vma
+ sec
->output_offset
));
3957 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3958 rel
->r_offset
, sec
, start
, end
);
3962 case R_SH_TLS_GD_32
:
3963 case R_SH_TLS_IE_32
:
3964 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
3965 tls_type
= GOT_UNKNOWN
;
3966 if (h
== NULL
&& local_got_offsets
)
3967 tls_type
= sh_elf_local_got_tls_type (input_bfd
) [r_symndx
];
3970 tls_type
= sh_elf_hash_entry (h
)->tls_type
;
3972 && (h
->dynindx
== -1
3974 r_type
= R_SH_TLS_LE_32
;
3977 if (r_type
== R_SH_TLS_GD_32
&& tls_type
== GOT_TLS_IE
)
3978 r_type
= R_SH_TLS_IE_32
;
3980 if (r_type
== R_SH_TLS_LE_32
)
3983 unsigned short insn
;
3985 if (ELF32_R_TYPE (rel
->r_info
) == R_SH_TLS_GD_32
)
3987 /* GD->LE transition:
3988 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
3989 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
3990 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
3992 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
3994 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
3996 offset
= rel
->r_offset
;
3997 BFD_ASSERT (offset
>= 16);
3998 /* Size of GD instructions is 16 or 18. */
4000 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4001 if ((insn
& 0xff00) == 0xc700)
4003 BFD_ASSERT (offset
>= 2);
4005 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4008 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4009 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4010 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4011 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4012 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4013 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4014 BFD_ASSERT (insn
== 0x310c);
4015 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4016 BFD_ASSERT (insn
== 0x410b);
4017 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4018 BFD_ASSERT (insn
== 0x34cc);
4020 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 2);
4021 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 4);
4022 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
4023 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4024 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4030 /* IE->LE transition:
4031 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
4032 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
4034 mov.l .Ln,rM; stc gbr,rN; nop; ...;
4037 offset
= rel
->r_offset
;
4038 BFD_ASSERT (offset
>= 16);
4039 /* Size of IE instructions is 10 or 12. */
4041 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4042 if ((insn
& 0xf0ff) == 0x0012)
4044 BFD_ASSERT (offset
>= 2);
4046 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4049 BFD_ASSERT ((insn
& 0xff00) == 0xd000);
4050 index
= insn
& 0x00ff;
4051 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4052 BFD_ASSERT ((insn
& 0xf0ff) == 0x0012);
4053 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4054 BFD_ASSERT ((insn
& 0xf0ff) == 0x00ce);
4055 insn
= 0xd000 | (insn
& 0x0f00) | index
;
4056 bfd_put_16 (output_bfd
, insn
, contents
+ offset
+ 0);
4057 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4060 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4061 contents
+ rel
->r_offset
);
4070 off
= h
->got
.offset
;
4073 if (local_got_offsets
== NULL
)
4076 off
= local_got_offsets
[r_symndx
];
4079 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4080 if (r_type
== R_SH_TLS_IE_32
4081 && ! htab
->root
.dynamic_sections_created
)
4084 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4085 sgot
->contents
+ off
);
4086 bfd_put_32 (output_bfd
, sgot
->output_offset
+ off
,
4087 contents
+ rel
->r_offset
);
4095 Elf_Internal_Rela outrel
;
4099 if (srelgot
== NULL
)
4101 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
4102 BFD_ASSERT (srelgot
!= NULL
);
4105 outrel
.r_offset
= (sgot
->output_section
->vma
4106 + sgot
->output_offset
+ off
);
4108 if (h
== NULL
|| h
->dynindx
== -1)
4113 dr_type
= (r_type
== R_SH_TLS_GD_32
? R_SH_TLS_DTPMOD32
:
4115 if (dr_type
== R_SH_TLS_TPOFF32
&& indx
== 0)
4116 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4118 outrel
.r_addend
= 0;
4119 outrel
.r_info
= ELF32_R_INFO (indx
, dr_type
);
4120 loc
= srelgot
->contents
;
4121 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4122 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4124 if (r_type
== R_SH_TLS_GD_32
)
4128 bfd_put_32 (output_bfd
,
4129 relocation
- dtpoff_base (info
),
4130 sgot
->contents
+ off
+ 4);
4134 outrel
.r_info
= ELF32_R_INFO (indx
,
4136 outrel
.r_offset
+= 4;
4137 outrel
.r_addend
= 0;
4138 srelgot
->reloc_count
++;
4139 loc
+= sizeof (Elf32_External_Rela
);
4140 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4147 local_got_offsets
[r_symndx
] |= 1;
4150 if (off
>= (bfd_vma
) -2)
4153 if (r_type
== (int) ELF32_R_TYPE (rel
->r_info
))
4154 relocation
= sgot
->output_offset
+ off
;
4158 unsigned short insn
;
4160 /* GD->IE transition:
4161 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4162 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4163 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4165 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4166 nop; nop; bra 3f; nop; .align 2;
4167 1: .long x@TPOFF; 2:...; 3:. */
4169 offset
= rel
->r_offset
;
4170 BFD_ASSERT (offset
>= 16);
4171 /* Size of GD instructions is 16 or 18. */
4173 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4174 if ((insn
& 0xff00) == 0xc700)
4176 BFD_ASSERT (offset
>= 2);
4178 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4181 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4183 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4184 bfd_put_16 (output_bfd
, insn
& 0xf0ff, contents
+ offset
);
4186 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4187 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4188 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4189 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4190 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4191 BFD_ASSERT (insn
== 0x310c);
4192 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4193 BFD_ASSERT (insn
== 0x410b);
4194 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4195 BFD_ASSERT (insn
== 0x34cc);
4197 bfd_put_16 (output_bfd
, 0x0412, contents
+ offset
+ 2);
4198 bfd_put_16 (output_bfd
, 0x00ce, contents
+ offset
+ 4);
4199 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 6);
4200 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4201 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4203 bfd_put_32 (output_bfd
, sgot
->output_offset
+ off
,
4204 contents
+ rel
->r_offset
);
4209 addend
= rel
->r_addend
;
4211 goto final_link_relocate
;
4213 case R_SH_TLS_LD_32
:
4217 unsigned short insn
;
4219 /* LD->LE transition:
4220 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4221 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4222 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4224 stc gbr,r0; nop; nop; nop;
4225 nop; nop; bra 3f; ...; 3:. */
4227 offset
= rel
->r_offset
;
4228 BFD_ASSERT (offset
>= 16);
4229 /* Size of LD instructions is 16 or 18. */
4231 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4232 if ((insn
& 0xff00) == 0xc700)
4234 BFD_ASSERT (offset
>= 2);
4236 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4239 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4240 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4241 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4242 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4243 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4244 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4245 BFD_ASSERT (insn
== 0x310c);
4246 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4247 BFD_ASSERT (insn
== 0x410b);
4248 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4249 BFD_ASSERT (insn
== 0x34cc);
4251 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 0);
4252 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 2);
4253 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4254 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
4255 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4256 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4265 off
= htab
->tls_ldm_got
.offset
;
4270 Elf_Internal_Rela outrel
;
4273 srelgot
= htab
->srelgot
;
4274 if (srelgot
== NULL
)
4277 outrel
.r_offset
= (sgot
->output_section
->vma
4278 + sgot
->output_offset
+ off
);
4279 outrel
.r_addend
= 0;
4280 outrel
.r_info
= ELF32_R_INFO (0, R_SH_TLS_DTPMOD32
);
4281 loc
= srelgot
->contents
;
4282 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4283 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4284 htab
->tls_ldm_got
.offset
|= 1;
4287 relocation
= sgot
->output_offset
+ off
;
4288 addend
= rel
->r_addend
;
4290 goto final_link_relocate
;
4292 case R_SH_TLS_LDO_32
:
4294 relocation
= tpoff (info
, relocation
);
4296 relocation
-= dtpoff_base (info
);
4298 addend
= rel
->r_addend
;
4299 goto final_link_relocate
;
4301 case R_SH_TLS_LE_32
:
4304 Elf_Internal_Rela outrel
;
4309 relocation
= tpoff (info
, relocation
);
4310 addend
= rel
->r_addend
;
4311 goto final_link_relocate
;
4318 name
= (bfd_elf_string_from_elf_section
4320 elf_elfheader (input_bfd
)->e_shstrndx
,
4321 elf_section_data (input_section
)->rel_hdr
.sh_name
));
4325 BFD_ASSERT (CONST_STRNEQ (name
, ".rela")
4326 && strcmp (bfd_get_section_name (input_bfd
,
4330 sreloc
= bfd_get_section_by_name (dynobj
, name
);
4331 BFD_ASSERT (sreloc
!= NULL
);
4334 if (h
== NULL
|| h
->dynindx
== -1)
4339 outrel
.r_offset
= (input_section
->output_section
->vma
4340 + input_section
->output_offset
4342 outrel
.r_info
= ELF32_R_INFO (indx
, R_SH_TLS_TPOFF32
);
4344 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4346 outrel
.r_addend
= 0;
4348 loc
= sreloc
->contents
;
4349 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4350 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4356 if (r
!= bfd_reloc_ok
)
4361 case bfd_reloc_outofrange
:
4363 case bfd_reloc_overflow
:
4371 name
= (bfd_elf_string_from_elf_section
4372 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
4376 name
= bfd_section_name (input_bfd
, sec
);
4378 if (! ((*info
->callbacks
->reloc_overflow
)
4379 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
4380 (bfd_vma
) 0, input_bfd
, input_section
,
4392 /* This is a version of bfd_generic_get_relocated_section_contents
4393 which uses sh_elf_relocate_section. */
4396 sh_elf_get_relocated_section_contents (bfd
*output_bfd
,
4397 struct bfd_link_info
*link_info
,
4398 struct bfd_link_order
*link_order
,
4400 bfd_boolean relocatable
,
4403 Elf_Internal_Shdr
*symtab_hdr
;
4404 asection
*input_section
= link_order
->u
.indirect
.section
;
4405 bfd
*input_bfd
= input_section
->owner
;
4406 asection
**sections
= NULL
;
4407 Elf_Internal_Rela
*internal_relocs
= NULL
;
4408 Elf_Internal_Sym
*isymbuf
= NULL
;
4410 /* We only need to handle the case of relaxing, or of having a
4411 particular set of section contents, specially. */
4413 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
4414 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
4419 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4421 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
4422 (size_t) input_section
->size
);
4424 if ((input_section
->flags
& SEC_RELOC
) != 0
4425 && input_section
->reloc_count
> 0)
4428 Elf_Internal_Sym
*isym
, *isymend
;
4431 internal_relocs
= (_bfd_elf_link_read_relocs
4432 (input_bfd
, input_section
, NULL
,
4433 (Elf_Internal_Rela
*) NULL
, FALSE
));
4434 if (internal_relocs
== NULL
)
4437 if (symtab_hdr
->sh_info
!= 0)
4439 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4440 if (isymbuf
== NULL
)
4441 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
4442 symtab_hdr
->sh_info
, 0,
4444 if (isymbuf
== NULL
)
4448 amt
= symtab_hdr
->sh_info
;
4449 amt
*= sizeof (asection
*);
4450 sections
= (asection
**) bfd_malloc (amt
);
4451 if (sections
== NULL
&& amt
!= 0)
4454 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
4455 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
4459 if (isym
->st_shndx
== SHN_UNDEF
)
4460 isec
= bfd_und_section_ptr
;
4461 else if (isym
->st_shndx
== SHN_ABS
)
4462 isec
= bfd_abs_section_ptr
;
4463 else if (isym
->st_shndx
== SHN_COMMON
)
4464 isec
= bfd_com_section_ptr
;
4466 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
4471 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
4472 input_section
, data
, internal_relocs
,
4476 if (sections
!= NULL
)
4479 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4481 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
4482 free (internal_relocs
);
4488 if (sections
!= NULL
)
4491 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4493 if (internal_relocs
!= NULL
4494 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
4495 free (internal_relocs
);
4499 /* Return the base VMA address which should be subtracted from real addresses
4500 when resolving @dtpoff relocation.
4501 This is PT_TLS segment p_vaddr. */
4504 dtpoff_base (struct bfd_link_info
*info
)
4506 /* If tls_sec is NULL, we should have signalled an error already. */
4507 if (elf_hash_table (info
)->tls_sec
== NULL
)
4509 return elf_hash_table (info
)->tls_sec
->vma
;
4512 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
4515 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
4517 /* If tls_sec is NULL, we should have signalled an error already. */
4518 if (elf_hash_table (info
)->tls_sec
== NULL
)
4520 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
4521 structure which has 2 pointer fields. */
4522 return (address
- elf_hash_table (info
)->tls_sec
->vma
4523 + align_power ((bfd_vma
) 8,
4524 elf_hash_table (info
)->tls_sec
->alignment_power
));
4528 sh_elf_gc_mark_hook (asection
*sec
,
4529 struct bfd_link_info
*info
,
4530 Elf_Internal_Rela
*rel
,
4531 struct elf_link_hash_entry
*h
,
4532 Elf_Internal_Sym
*sym
)
4535 switch (ELF32_R_TYPE (rel
->r_info
))
4537 case R_SH_GNU_VTINHERIT
:
4538 case R_SH_GNU_VTENTRY
:
4542 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
4545 /* Update the got entry reference counts for the section being removed. */
4548 sh_elf_gc_sweep_hook (bfd
*abfd
, struct bfd_link_info
*info
,
4549 asection
*sec
, const Elf_Internal_Rela
*relocs
)
4551 Elf_Internal_Shdr
*symtab_hdr
;
4552 struct elf_link_hash_entry
**sym_hashes
;
4553 bfd_signed_vma
*local_got_refcounts
;
4554 const Elf_Internal_Rela
*rel
, *relend
;
4556 elf_section_data (sec
)->local_dynrel
= NULL
;
4558 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4559 sym_hashes
= elf_sym_hashes (abfd
);
4560 local_got_refcounts
= elf_local_got_refcounts (abfd
);
4562 relend
= relocs
+ sec
->reloc_count
;
4563 for (rel
= relocs
; rel
< relend
; rel
++)
4565 unsigned long r_symndx
;
4566 unsigned int r_type
;
4567 struct elf_link_hash_entry
*h
= NULL
;
4568 #ifdef INCLUDE_SHMEDIA
4569 int seen_stt_datalabel
= 0;
4572 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4573 if (r_symndx
>= symtab_hdr
->sh_info
)
4575 struct elf_sh_link_hash_entry
*eh
;
4576 struct elf_sh_dyn_relocs
**pp
;
4577 struct elf_sh_dyn_relocs
*p
;
4579 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4580 while (h
->root
.type
== bfd_link_hash_indirect
4581 || h
->root
.type
== bfd_link_hash_warning
)
4583 #ifdef INCLUDE_SHMEDIA
4584 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
4586 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4588 eh
= (struct elf_sh_link_hash_entry
*) h
;
4589 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
4592 /* Everything must go for SEC. */
4598 r_type
= ELF32_R_TYPE (rel
->r_info
);
4599 switch (sh_elf_optimized_tls_reloc (info
, r_type
, h
!= NULL
))
4601 case R_SH_TLS_LD_32
:
4602 if (sh_elf_hash_table (info
)->tls_ldm_got
.refcount
> 0)
4603 sh_elf_hash_table (info
)->tls_ldm_got
.refcount
-= 1;
4609 #ifdef INCLUDE_SHMEDIA
4610 case R_SH_GOT_LOW16
:
4611 case R_SH_GOT_MEDLOW16
:
4612 case R_SH_GOT_MEDHI16
:
4616 case R_SH_GOTOFF_LOW16
:
4617 case R_SH_GOTOFF_MEDLOW16
:
4618 case R_SH_GOTOFF_MEDHI16
:
4619 case R_SH_GOTOFF_HI16
:
4620 case R_SH_GOTPC_LOW16
:
4621 case R_SH_GOTPC_MEDLOW16
:
4622 case R_SH_GOTPC_MEDHI16
:
4623 case R_SH_GOTPC_HI16
:
4625 case R_SH_TLS_GD_32
:
4626 case R_SH_TLS_IE_32
:
4629 #ifdef INCLUDE_SHMEDIA
4630 if (seen_stt_datalabel
)
4632 struct elf_sh_link_hash_entry
*eh
;
4633 eh
= (struct elf_sh_link_hash_entry
*) h
;
4634 if (eh
->datalabel_got
.refcount
> 0)
4635 eh
->datalabel_got
.refcount
-= 1;
4639 if (h
->got
.refcount
> 0)
4640 h
->got
.refcount
-= 1;
4642 else if (local_got_refcounts
!= NULL
)
4644 #ifdef INCLUDE_SHMEDIA
4645 if (rel
->r_addend
& 1)
4647 if (local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] > 0)
4648 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] -= 1;
4652 if (local_got_refcounts
[r_symndx
] > 0)
4653 local_got_refcounts
[r_symndx
] -= 1;
4664 #ifdef INCLUDE_SHMEDIA
4665 case R_SH_PLT_LOW16
:
4666 case R_SH_PLT_MEDLOW16
:
4667 case R_SH_PLT_MEDHI16
:
4672 if (h
->plt
.refcount
> 0)
4673 h
->plt
.refcount
-= 1;
4678 #ifdef INCLUDE_SHMEDIA
4679 case R_SH_GOTPLT_LOW16
:
4680 case R_SH_GOTPLT_MEDLOW16
:
4681 case R_SH_GOTPLT_MEDHI16
:
4682 case R_SH_GOTPLT_HI16
:
4683 case R_SH_GOTPLT10BY4
:
4684 case R_SH_GOTPLT10BY8
:
4688 struct elf_sh_link_hash_entry
*eh
;
4689 eh
= (struct elf_sh_link_hash_entry
*) h
;
4690 if (eh
->gotplt_refcount
> 0)
4692 eh
->gotplt_refcount
-= 1;
4693 if (h
->plt
.refcount
> 0)
4694 h
->plt
.refcount
-= 1;
4696 #ifdef INCLUDE_SHMEDIA
4697 else if (seen_stt_datalabel
)
4699 if (eh
->datalabel_got
.refcount
> 0)
4700 eh
->datalabel_got
.refcount
-= 1;
4703 else if (h
->got
.refcount
> 0)
4704 h
->got
.refcount
-= 1;
4706 else if (local_got_refcounts
!= NULL
)
4708 #ifdef INCLUDE_SHMEDIA
4709 if (rel
->r_addend
& 1)
4711 if (local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] > 0)
4712 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] -= 1;
4716 if (local_got_refcounts
[r_symndx
] > 0)
4717 local_got_refcounts
[r_symndx
] -= 1;
4729 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4732 sh_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
4733 struct elf_link_hash_entry
*dir
,
4734 struct elf_link_hash_entry
*ind
)
4736 struct elf_sh_link_hash_entry
*edir
, *eind
;
4738 edir
= (struct elf_sh_link_hash_entry
*) dir
;
4739 eind
= (struct elf_sh_link_hash_entry
*) ind
;
4741 if (eind
->dyn_relocs
!= NULL
)
4743 if (edir
->dyn_relocs
!= NULL
)
4745 struct elf_sh_dyn_relocs
**pp
;
4746 struct elf_sh_dyn_relocs
*p
;
4748 /* Add reloc counts against the indirect sym to the direct sym
4749 list. Merge any entries against the same section. */
4750 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
4752 struct elf_sh_dyn_relocs
*q
;
4754 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
4755 if (q
->sec
== p
->sec
)
4757 q
->pc_count
+= p
->pc_count
;
4758 q
->count
+= p
->count
;
4765 *pp
= edir
->dyn_relocs
;
4768 edir
->dyn_relocs
= eind
->dyn_relocs
;
4769 eind
->dyn_relocs
= NULL
;
4771 edir
->gotplt_refcount
= eind
->gotplt_refcount
;
4772 eind
->gotplt_refcount
= 0;
4773 #ifdef INCLUDE_SHMEDIA
4774 edir
->datalabel_got
.refcount
+= eind
->datalabel_got
.refcount
;
4775 eind
->datalabel_got
.refcount
= 0;
4778 if (ind
->root
.type
== bfd_link_hash_indirect
4779 && dir
->got
.refcount
<= 0)
4781 edir
->tls_type
= eind
->tls_type
;
4782 eind
->tls_type
= GOT_UNKNOWN
;
4785 if (ind
->root
.type
!= bfd_link_hash_indirect
4786 && dir
->dynamic_adjusted
)
4788 /* If called to transfer flags for a weakdef during processing
4789 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
4790 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4791 dir
->ref_dynamic
|= ind
->ref_dynamic
;
4792 dir
->ref_regular
|= ind
->ref_regular
;
4793 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
4794 dir
->needs_plt
|= ind
->needs_plt
;
4797 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
4801 sh_elf_optimized_tls_reloc (struct bfd_link_info
*info
, int r_type
,
4809 case R_SH_TLS_GD_32
:
4810 case R_SH_TLS_IE_32
:
4812 return R_SH_TLS_LE_32
;
4813 return R_SH_TLS_IE_32
;
4814 case R_SH_TLS_LD_32
:
4815 return R_SH_TLS_LE_32
;
4821 /* Look through the relocs for a section during the first phase.
4822 Since we don't do .gots or .plts, we just need to consider the
4823 virtual table relocs for gc. */
4826 sh_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
4827 const Elf_Internal_Rela
*relocs
)
4829 Elf_Internal_Shdr
*symtab_hdr
;
4830 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
4831 struct elf_sh_link_hash_table
*htab
;
4832 const Elf_Internal_Rela
*rel
;
4833 const Elf_Internal_Rela
*rel_end
;
4834 bfd_vma
*local_got_offsets
;
4838 unsigned int r_type
;
4839 int tls_type
, old_tls_type
;
4845 if (info
->relocatable
)
4848 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4849 sym_hashes
= elf_sym_hashes (abfd
);
4850 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
4851 if (!elf_bad_symtab (abfd
))
4852 sym_hashes_end
-= symtab_hdr
->sh_info
;
4854 htab
= sh_elf_hash_table (info
);
4855 local_got_offsets
= elf_local_got_offsets (abfd
);
4857 rel_end
= relocs
+ sec
->reloc_count
;
4858 for (rel
= relocs
; rel
< rel_end
; rel
++)
4860 struct elf_link_hash_entry
*h
;
4861 unsigned long r_symndx
;
4862 #ifdef INCLUDE_SHMEDIA
4863 int seen_stt_datalabel
= 0;
4866 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4867 r_type
= ELF32_R_TYPE (rel
->r_info
);
4869 if (r_symndx
< symtab_hdr
->sh_info
)
4873 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4874 while (h
->root
.type
== bfd_link_hash_indirect
4875 || h
->root
.type
== bfd_link_hash_warning
)
4877 #ifdef INCLUDE_SHMEDIA
4878 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
4880 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4884 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
4886 && r_type
== R_SH_TLS_IE_32
4888 && h
->root
.type
!= bfd_link_hash_undefined
4889 && h
->root
.type
!= bfd_link_hash_undefweak
4890 && (h
->dynindx
== -1
4892 r_type
= R_SH_TLS_LE_32
;
4894 /* Some relocs require a global offset table. */
4895 if (htab
->sgot
== NULL
)
4903 #ifdef INCLUDE_SHMEDIA
4904 case R_SH_GOTPLT_LOW16
:
4905 case R_SH_GOTPLT_MEDLOW16
:
4906 case R_SH_GOTPLT_MEDHI16
:
4907 case R_SH_GOTPLT_HI16
:
4908 case R_SH_GOTPLT10BY4
:
4909 case R_SH_GOTPLT10BY8
:
4910 case R_SH_GOT_LOW16
:
4911 case R_SH_GOT_MEDLOW16
:
4912 case R_SH_GOT_MEDHI16
:
4916 case R_SH_GOTOFF_LOW16
:
4917 case R_SH_GOTOFF_MEDLOW16
:
4918 case R_SH_GOTOFF_MEDHI16
:
4919 case R_SH_GOTOFF_HI16
:
4920 case R_SH_GOTPC_LOW16
:
4921 case R_SH_GOTPC_MEDLOW16
:
4922 case R_SH_GOTPC_MEDHI16
:
4923 case R_SH_GOTPC_HI16
:
4925 case R_SH_TLS_GD_32
:
4926 case R_SH_TLS_LD_32
:
4927 case R_SH_TLS_IE_32
:
4928 if (htab
->sgot
== NULL
)
4930 if (htab
->root
.dynobj
== NULL
)
4931 htab
->root
.dynobj
= abfd
;
4932 if (!create_got_section (htab
->root
.dynobj
, info
))
4944 /* This relocation describes the C++ object vtable hierarchy.
4945 Reconstruct it for later use during GC. */
4946 case R_SH_GNU_VTINHERIT
:
4947 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
4951 /* This relocation describes which C++ vtable entries are actually
4952 used. Record for later use during GC. */
4953 case R_SH_GNU_VTENTRY
:
4954 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
4958 case R_SH_TLS_IE_32
:
4960 info
->flags
|= DF_STATIC_TLS
;
4964 case R_SH_TLS_GD_32
:
4966 #ifdef INCLUDE_SHMEDIA
4967 case R_SH_GOT_LOW16
:
4968 case R_SH_GOT_MEDLOW16
:
4969 case R_SH_GOT_MEDHI16
:
4977 tls_type
= GOT_NORMAL
;
4979 case R_SH_TLS_GD_32
:
4980 tls_type
= GOT_TLS_GD
;
4982 case R_SH_TLS_IE_32
:
4983 tls_type
= GOT_TLS_IE
;
4989 #ifdef INCLUDE_SHMEDIA
4990 if (seen_stt_datalabel
)
4992 struct elf_sh_link_hash_entry
*eh
4993 = (struct elf_sh_link_hash_entry
*) h
;
4995 eh
->datalabel_got
.refcount
+= 1;
4999 h
->got
.refcount
+= 1;
5000 old_tls_type
= sh_elf_hash_entry (h
)->tls_type
;
5004 bfd_signed_vma
*local_got_refcounts
;
5006 /* This is a global offset table entry for a local
5008 local_got_refcounts
= elf_local_got_refcounts (abfd
);
5009 if (local_got_refcounts
== NULL
)
5013 size
= symtab_hdr
->sh_info
;
5014 size
*= sizeof (bfd_signed_vma
);
5015 #ifdef INCLUDE_SHMEDIA
5016 /* Reserve space for both the datalabel and
5017 codelabel local GOT offsets. */
5020 size
+= symtab_hdr
->sh_info
;
5021 local_got_refcounts
= ((bfd_signed_vma
*)
5022 bfd_zalloc (abfd
, size
));
5023 if (local_got_refcounts
== NULL
)
5025 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
5026 #ifdef INCLUDE_SHMEDIA
5027 /* Take care of both the datalabel and codelabel local
5029 sh_elf_local_got_tls_type (abfd
)
5030 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
5032 sh_elf_local_got_tls_type (abfd
)
5033 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
5036 #ifdef INCLUDE_SHMEDIA
5037 if (rel
->r_addend
& 1)
5038 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] += 1;
5041 local_got_refcounts
[r_symndx
] += 1;
5042 old_tls_type
= sh_elf_local_got_tls_type (abfd
) [r_symndx
];
5045 /* If a TLS symbol is accessed using IE at least once,
5046 there is no point to use dynamic model for it. */
5047 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
5048 && (old_tls_type
!= GOT_TLS_GD
|| tls_type
!= GOT_TLS_IE
))
5050 if (old_tls_type
== GOT_TLS_IE
&& tls_type
== GOT_TLS_GD
)
5051 tls_type
= GOT_TLS_IE
;
5054 (*_bfd_error_handler
)
5055 (_("%B: `%s' accessed both as normal and thread local symbol"),
5056 abfd
, h
->root
.root
.string
);
5061 if (old_tls_type
!= tls_type
)
5064 sh_elf_hash_entry (h
)->tls_type
= tls_type
;
5066 sh_elf_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
5071 case R_SH_TLS_LD_32
:
5072 sh_elf_hash_table(info
)->tls_ldm_got
.refcount
+= 1;
5076 #ifdef INCLUDE_SHMEDIA
5077 case R_SH_GOTPLT_LOW16
:
5078 case R_SH_GOTPLT_MEDLOW16
:
5079 case R_SH_GOTPLT_MEDHI16
:
5080 case R_SH_GOTPLT_HI16
:
5081 case R_SH_GOTPLT10BY4
:
5082 case R_SH_GOTPLT10BY8
:
5084 /* If this is a local symbol, we resolve it directly without
5085 creating a procedure linkage table entry. */
5091 || h
->dynindx
== -1)
5095 h
->plt
.refcount
+= 1;
5096 ((struct elf_sh_link_hash_entry
*) h
)->gotplt_refcount
+= 1;
5101 #ifdef INCLUDE_SHMEDIA
5102 case R_SH_PLT_LOW16
:
5103 case R_SH_PLT_MEDLOW16
:
5104 case R_SH_PLT_MEDHI16
:
5107 /* This symbol requires a procedure linkage table entry. We
5108 actually build the entry in adjust_dynamic_symbol,
5109 because this might be a case of linking PIC code which is
5110 never referenced by a dynamic object, in which case we
5111 don't need to generate a procedure linkage table entry
5114 /* If this is a local symbol, we resolve it directly without
5115 creating a procedure linkage table entry. */
5119 if (h
->forced_local
)
5123 h
->plt
.refcount
+= 1;
5128 #ifdef INCLUDE_SHMEDIA
5129 case R_SH_IMM_LOW16_PCREL
:
5130 case R_SH_IMM_MEDLOW16_PCREL
:
5131 case R_SH_IMM_MEDHI16_PCREL
:
5132 case R_SH_IMM_HI16_PCREL
:
5134 if (h
!= NULL
&& ! info
->shared
)
5137 h
->plt
.refcount
+= 1;
5140 /* If we are creating a shared library, and this is a reloc
5141 against a global symbol, or a non PC relative reloc
5142 against a local symbol, then we need to copy the reloc
5143 into the shared library. However, if we are linking with
5144 -Bsymbolic, we do not need to copy a reloc against a
5145 global symbol which is defined in an object we are
5146 including in the link (i.e., DEF_REGULAR is set). At
5147 this point we have not seen all the input files, so it is
5148 possible that DEF_REGULAR is not set now but will be set
5149 later (it is never cleared). We account for that
5150 possibility below by storing information in the
5151 dyn_relocs field of the hash table entry. A similar
5152 situation occurs when creating shared libraries and symbol
5153 visibility changes render the symbol local.
5155 If on the other hand, we are creating an executable, we
5156 may need to keep relocations for symbols satisfied by a
5157 dynamic library if we manage to avoid copy relocs for the
5160 && (sec
->flags
& SEC_ALLOC
) != 0
5161 && (r_type
!= R_SH_REL32
5163 && (! info
->symbolic
5164 || h
->root
.type
== bfd_link_hash_defweak
5165 || !h
->def_regular
))))
5167 && (sec
->flags
& SEC_ALLOC
) != 0
5169 && (h
->root
.type
== bfd_link_hash_defweak
5170 || !h
->def_regular
)))
5172 struct elf_sh_dyn_relocs
*p
;
5173 struct elf_sh_dyn_relocs
**head
;
5175 if (htab
->root
.dynobj
== NULL
)
5176 htab
->root
.dynobj
= abfd
;
5178 /* When creating a shared object, we must copy these
5179 reloc types into the output file. We create a reloc
5180 section in dynobj and make room for this reloc. */
5185 name
= (bfd_elf_string_from_elf_section
5187 elf_elfheader (abfd
)->e_shstrndx
,
5188 elf_section_data (sec
)->rel_hdr
.sh_name
));
5192 BFD_ASSERT (CONST_STRNEQ (name
, ".rela")
5193 && strcmp (bfd_get_section_name (abfd
, sec
),
5196 sreloc
= bfd_get_section_by_name (htab
->root
.dynobj
, name
);
5201 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
5202 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
5203 if ((sec
->flags
& SEC_ALLOC
) != 0)
5204 flags
|= SEC_ALLOC
| SEC_LOAD
;
5205 sreloc
= bfd_make_section_with_flags (htab
->root
.dynobj
,
5209 || ! bfd_set_section_alignment (htab
->root
.dynobj
,
5213 elf_section_data (sec
)->sreloc
= sreloc
;
5216 /* If this is a global symbol, we count the number of
5217 relocations we need for this symbol. */
5219 head
= &((struct elf_sh_link_hash_entry
*) h
)->dyn_relocs
;
5225 /* Track dynamic relocs needed for local syms too. */
5226 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
5231 vpp
= &elf_section_data (s
)->local_dynrel
;
5232 head
= (struct elf_sh_dyn_relocs
**) vpp
;
5236 if (p
== NULL
|| p
->sec
!= sec
)
5238 bfd_size_type amt
= sizeof (*p
);
5239 p
= bfd_alloc (htab
->root
.dynobj
, amt
);
5250 if (r_type
== R_SH_REL32
5251 #ifdef INCLUDE_SHMEDIA
5252 || r_type
== R_SH_IMM_LOW16_PCREL
5253 || r_type
== R_SH_IMM_MEDLOW16_PCREL
5254 || r_type
== R_SH_IMM_MEDHI16_PCREL
5255 || r_type
== R_SH_IMM_HI16_PCREL
5263 case R_SH_TLS_LE_32
:
5266 (*_bfd_error_handler
)
5267 (_("%B: TLS local exec code cannot be linked into shared objects"),
5274 case R_SH_TLS_LDO_32
:
5275 /* Nothing to do. */
5286 #ifndef sh_elf_set_mach_from_flags
5287 static unsigned int sh_ef_bfd_table
[] = { EF_SH_BFD_TABLE
};
5290 sh_elf_set_mach_from_flags (bfd
*abfd
)
5292 flagword flags
= elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
;
5294 if (flags
>= sizeof(sh_ef_bfd_table
))
5297 if (sh_ef_bfd_table
[flags
] == 0)
5300 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, sh_ef_bfd_table
[flags
]);
5306 /* Reverse table lookup for sh_ef_bfd_table[].
5307 Given a bfd MACH value from archures.c
5308 return the equivalent ELF flags from the table.
5309 Return -1 if no match is found. */
5312 sh_elf_get_flags_from_mach (unsigned long mach
)
5314 int i
= ARRAY_SIZE (sh_ef_bfd_table
) - 1;
5317 if (sh_ef_bfd_table
[i
] == mach
)
5320 /* shouldn't get here */
5325 #endif /* not sh_elf_set_mach_from_flags */
5327 #ifndef sh_elf_set_private_flags
5328 /* Function to keep SH specific file flags. */
5331 sh_elf_set_private_flags (bfd
*abfd
, flagword flags
)
5333 BFD_ASSERT (! elf_flags_init (abfd
)
5334 || elf_elfheader (abfd
)->e_flags
== flags
);
5336 elf_elfheader (abfd
)->e_flags
= flags
;
5337 elf_flags_init (abfd
) = TRUE
;
5338 return sh_elf_set_mach_from_flags (abfd
);
5340 #endif /* not sh_elf_set_private_flags */
5342 #ifndef sh_elf_copy_private_data
5343 /* Copy backend specific data from one object module to another */
5346 sh_elf_copy_private_data (bfd
* ibfd
, bfd
* obfd
)
5348 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5349 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5352 return sh_elf_set_private_flags (obfd
, elf_elfheader (ibfd
)->e_flags
);
5354 #endif /* not sh_elf_copy_private_data */
5356 #ifndef sh_elf_merge_private_data
5358 /* This function returns the ELF architecture number that
5359 corresponds to the given arch_sh* flags. */
5362 sh_find_elf_flags (unsigned int arch_set
)
5364 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
5365 unsigned long bfd_mach
= sh_get_bfd_mach_from_arch_set (arch_set
);
5367 return sh_elf_get_flags_from_mach (bfd_mach
);
5370 /* This routine initialises the elf flags when required and
5371 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
5374 sh_elf_merge_private_data (bfd
*ibfd
, bfd
*obfd
)
5376 extern bfd_boolean
sh_merge_bfd_arch (bfd
*, bfd
*);
5378 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5379 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5382 if (! elf_flags_init (obfd
))
5384 /* This happens when ld starts out with a 'blank' output file. */
5385 elf_flags_init (obfd
) = TRUE
;
5386 elf_elfheader (obfd
)->e_flags
= EF_SH1
;
5387 sh_elf_set_mach_from_flags (obfd
);
5390 if (! sh_merge_bfd_arch (ibfd
, obfd
))
5392 _bfd_error_handler ("%B: uses instructions which are incompatible "
5393 "with instructions used in previous modules",
5395 bfd_set_error (bfd_error_bad_value
);
5399 elf_elfheader (obfd
)->e_flags
=
5400 sh_elf_get_flags_from_mach (bfd_get_mach (obfd
));
5404 #endif /* not sh_elf_merge_private_data */
5406 /* Override the generic function because we need to store sh_elf_obj_tdata
5407 as the specific tdata. We set also the machine architecture from flags
5411 sh_elf_object_p (bfd
*abfd
)
5413 return sh_elf_set_mach_from_flags (abfd
);
5416 /* Finish up dynamic symbol handling. We set the contents of various
5417 dynamic sections here. */
5420 sh_elf_finish_dynamic_symbol (bfd
*output_bfd
, struct bfd_link_info
*info
,
5421 struct elf_link_hash_entry
*h
,
5422 Elf_Internal_Sym
*sym
)
5424 struct elf_sh_link_hash_table
*htab
;
5426 htab
= sh_elf_hash_table (info
);
5428 if (h
->plt
.offset
!= (bfd_vma
) -1)
5436 Elf_Internal_Rela rel
;
5439 /* This symbol has an entry in the procedure linkage table. Set
5442 BFD_ASSERT (h
->dynindx
!= -1);
5445 sgot
= htab
->sgotplt
;
5446 srel
= htab
->srelplt
;
5447 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
5449 /* Get the index in the procedure linkage table which
5450 corresponds to this symbol. This is the index of this symbol
5451 in all the symbols for which we are making plt entries. The
5452 first entry in the procedure linkage table is reserved. */
5453 plt_index
= get_plt_index (htab
->plt_info
, h
->plt
.offset
);
5455 /* Get the offset into the .got table of the entry that
5456 corresponds to this function. Each .got entry is 4 bytes.
5457 The first three are reserved. */
5458 got_offset
= (plt_index
+ 3) * 4;
5462 got_offset
-= GOT_BIAS
;
5465 /* Fill in the entry in the procedure linkage table. */
5466 memcpy (splt
->contents
+ h
->plt
.offset
,
5467 htab
->plt_info
->symbol_entry
,
5468 htab
->plt_info
->symbol_entry_size
);
5471 install_plt_field (output_bfd
, FALSE
, got_offset
,
5474 + htab
->plt_info
->symbol_fields
.got_entry
));
5477 install_plt_field (output_bfd
, FALSE
,
5478 (sgot
->output_section
->vma
5479 + sgot
->output_offset
5483 + htab
->plt_info
->symbol_fields
.got_entry
));
5484 if (htab
->vxworks_p
)
5486 unsigned int reachable_plts
, plts_per_4k
;
5489 /* Divide the PLT into groups. The first group contains
5490 REACHABLE_PLTS entries and the other groups contain
5491 PLTS_PER_4K entries. Entries in the first group can
5492 branch directly to .plt; those in later groups branch
5493 to the last element of the previous group. */
5494 /* ??? It would be better to create multiple copies of
5495 the common resolver stub. */
5496 reachable_plts
= ((4096
5497 - htab
->plt_info
->plt0_entry_size
5498 - (htab
->plt_info
->symbol_fields
.plt
+ 4))
5499 / htab
->plt_info
->symbol_entry_size
) + 1;
5500 plts_per_4k
= (4096 / htab
->plt_info
->symbol_entry_size
);
5501 if (plt_index
< reachable_plts
)
5502 distance
= -(h
->plt
.offset
5503 + htab
->plt_info
->symbol_fields
.plt
);
5505 distance
= -(((plt_index
- reachable_plts
) % plts_per_4k
+ 1)
5506 * htab
->plt_info
->symbol_entry_size
);
5508 /* Install the 'bra' with this offset. */
5509 bfd_put_16 (output_bfd
,
5510 0xa000 | (0x0fff & ((distance
- 4) / 2)),
5513 + htab
->plt_info
->symbol_fields
.plt
));
5516 install_plt_field (output_bfd
, TRUE
,
5517 splt
->output_section
->vma
+ splt
->output_offset
,
5520 + htab
->plt_info
->symbol_fields
.plt
));
5525 got_offset
+= GOT_BIAS
;
5528 install_plt_field (output_bfd
, FALSE
,
5529 plt_index
* sizeof (Elf32_External_Rela
),
5532 + htab
->plt_info
->symbol_fields
.reloc_offset
));
5534 /* Fill in the entry in the global offset table. */
5535 bfd_put_32 (output_bfd
,
5536 (splt
->output_section
->vma
5537 + splt
->output_offset
5539 + htab
->plt_info
->symbol_resolve_offset
),
5540 sgot
->contents
+ got_offset
);
5542 /* Fill in the entry in the .rela.plt section. */
5543 rel
.r_offset
= (sgot
->output_section
->vma
5544 + sgot
->output_offset
5546 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
5549 rel
.r_addend
= GOT_BIAS
;
5551 loc
= srel
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
5552 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5554 if (htab
->vxworks_p
&& !info
->shared
)
5556 /* Create the .rela.plt.unloaded relocations for this PLT entry.
5557 Begin by pointing LOC to the first such relocation. */
5558 loc
= (htab
->srelplt2
->contents
5559 + (plt_index
* 2 + 1) * sizeof (Elf32_External_Rela
));
5561 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
5562 for the PLT entry's pointer to the .got.plt entry. */
5563 rel
.r_offset
= (htab
->splt
->output_section
->vma
5564 + htab
->splt
->output_offset
5566 + htab
->plt_info
->symbol_fields
.got_entry
);
5567 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
5568 rel
.r_addend
= got_offset
;
5569 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5570 loc
+= sizeof (Elf32_External_Rela
);
5572 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
5573 the .got.plt entry, which initially points to .plt. */
5574 rel
.r_offset
= (htab
->sgotplt
->output_section
->vma
5575 + htab
->sgotplt
->output_offset
5577 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
, R_SH_DIR32
);
5579 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5582 if (!h
->def_regular
)
5584 /* Mark the symbol as undefined, rather than as defined in
5585 the .plt section. Leave the value alone. */
5586 sym
->st_shndx
= SHN_UNDEF
;
5590 if (h
->got
.offset
!= (bfd_vma
) -1
5591 && sh_elf_hash_entry (h
)->tls_type
!= GOT_TLS_GD
5592 && sh_elf_hash_entry (h
)->tls_type
!= GOT_TLS_IE
)
5596 Elf_Internal_Rela rel
;
5599 /* This symbol has an entry in the global offset table. Set it
5603 srel
= htab
->srelgot
;
5604 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
5606 rel
.r_offset
= (sgot
->output_section
->vma
5607 + sgot
->output_offset
5608 + (h
->got
.offset
&~ (bfd_vma
) 1));
5610 /* If this is a static link, or it is a -Bsymbolic link and the
5611 symbol is defined locally or was forced to be local because
5612 of a version file, we just want to emit a RELATIVE reloc.
5613 The entry in the global offset table will already have been
5614 initialized in the relocate_section function. */
5616 && SYMBOL_REFERENCES_LOCAL (info
, h
))
5618 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
5619 rel
.r_addend
= (h
->root
.u
.def
.value
5620 + h
->root
.u
.def
.section
->output_section
->vma
5621 + h
->root
.u
.def
.section
->output_offset
);
5625 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
5626 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
5630 loc
= srel
->contents
;
5631 loc
+= srel
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5632 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5635 #ifdef INCLUDE_SHMEDIA
5637 struct elf_sh_link_hash_entry
*eh
;
5639 eh
= (struct elf_sh_link_hash_entry
*) h
;
5640 if (eh
->datalabel_got
.offset
!= (bfd_vma
) -1)
5644 Elf_Internal_Rela rel
;
5647 /* This symbol has a datalabel entry in the global offset table.
5651 srel
= htab
->srelgot
;
5652 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
5654 rel
.r_offset
= (sgot
->output_section
->vma
5655 + sgot
->output_offset
5656 + (eh
->datalabel_got
.offset
&~ (bfd_vma
) 1));
5658 /* If this is a static link, or it is a -Bsymbolic link and the
5659 symbol is defined locally or was forced to be local because
5660 of a version file, we just want to emit a RELATIVE reloc.
5661 The entry in the global offset table will already have been
5662 initialized in the relocate_section function. */
5664 && SYMBOL_REFERENCES_LOCAL (info
, h
))
5666 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
5667 rel
.r_addend
= (h
->root
.u
.def
.value
5668 + h
->root
.u
.def
.section
->output_section
->vma
5669 + h
->root
.u
.def
.section
->output_offset
);
5673 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
5674 + eh
->datalabel_got
.offset
);
5675 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
5679 loc
= srel
->contents
;
5680 loc
+= srel
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5681 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5689 Elf_Internal_Rela rel
;
5692 /* This symbol needs a copy reloc. Set it up. */
5694 BFD_ASSERT (h
->dynindx
!= -1
5695 && (h
->root
.type
== bfd_link_hash_defined
5696 || h
->root
.type
== bfd_link_hash_defweak
));
5698 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
5700 BFD_ASSERT (s
!= NULL
);
5702 rel
.r_offset
= (h
->root
.u
.def
.value
5703 + h
->root
.u
.def
.section
->output_section
->vma
5704 + h
->root
.u
.def
.section
->output_offset
);
5705 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
5707 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5708 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5711 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
5712 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
5714 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
5715 || (!htab
->vxworks_p
&& h
== htab
->root
.hgot
))
5716 sym
->st_shndx
= SHN_ABS
;
5721 /* Finish up the dynamic sections. */
5724 sh_elf_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
5726 struct elf_sh_link_hash_table
*htab
;
5730 htab
= sh_elf_hash_table (info
);
5731 sgot
= htab
->sgotplt
;
5732 sdyn
= bfd_get_section_by_name (htab
->root
.dynobj
, ".dynamic");
5734 if (htab
->root
.dynamic_sections_created
)
5737 Elf32_External_Dyn
*dyncon
, *dynconend
;
5739 BFD_ASSERT (sgot
!= NULL
&& sdyn
!= NULL
);
5741 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5742 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
5743 for (; dyncon
< dynconend
; dyncon
++)
5745 Elf_Internal_Dyn dyn
;
5747 #ifdef INCLUDE_SHMEDIA
5751 bfd_elf32_swap_dyn_in (htab
->root
.dynobj
, dyncon
, &dyn
);
5758 #ifdef INCLUDE_SHMEDIA
5760 name
= info
->init_function
;
5764 name
= info
->fini_function
;
5766 if (dyn
.d_un
.d_val
!= 0)
5768 struct elf_link_hash_entry
*h
;
5770 h
= elf_link_hash_lookup (&htab
->root
, name
,
5771 FALSE
, FALSE
, TRUE
);
5772 if (h
!= NULL
&& (h
->other
& STO_SH5_ISA32
))
5774 dyn
.d_un
.d_val
|= 1;
5775 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5782 s
= htab
->sgot
->output_section
;
5786 s
= htab
->srelplt
->output_section
;
5788 BFD_ASSERT (s
!= NULL
);
5789 dyn
.d_un
.d_ptr
= s
->vma
;
5790 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5794 s
= htab
->srelplt
->output_section
;
5795 BFD_ASSERT (s
!= NULL
);
5796 dyn
.d_un
.d_val
= s
->size
;
5797 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5801 /* My reading of the SVR4 ABI indicates that the
5802 procedure linkage table relocs (DT_JMPREL) should be
5803 included in the overall relocs (DT_RELA). This is
5804 what Solaris does. However, UnixWare can not handle
5805 that case. Therefore, we override the DT_RELASZ entry
5806 here to make it not include the JMPREL relocs. Since
5807 the linker script arranges for .rela.plt to follow all
5808 other relocation sections, we don't have to worry
5809 about changing the DT_RELA entry. */
5810 if (htab
->srelplt
!= NULL
)
5812 s
= htab
->srelplt
->output_section
;
5813 dyn
.d_un
.d_val
-= s
->size
;
5815 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5820 /* Fill in the first entry in the procedure linkage table. */
5822 if (splt
&& splt
->size
> 0 && htab
->plt_info
->plt0_entry
)
5826 memcpy (splt
->contents
,
5827 htab
->plt_info
->plt0_entry
,
5828 htab
->plt_info
->plt0_entry_size
);
5829 for (i
= 0; i
< ARRAY_SIZE (htab
->plt_info
->plt0_got_fields
); i
++)
5830 if (htab
->plt_info
->plt0_got_fields
[i
] != MINUS_ONE
)
5831 install_plt_field (output_bfd
, FALSE
,
5832 (sgot
->output_section
->vma
5833 + sgot
->output_offset
5836 + htab
->plt_info
->plt0_got_fields
[i
]));
5838 if (htab
->vxworks_p
)
5840 /* Finalize the .rela.plt.unloaded contents. */
5841 Elf_Internal_Rela rel
;
5844 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
5845 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
5846 loc
= htab
->srelplt2
->contents
;
5847 rel
.r_offset
= (splt
->output_section
->vma
5848 + splt
->output_offset
5849 + htab
->plt_info
->plt0_got_fields
[2]);
5850 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
5852 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5853 loc
+= sizeof (Elf32_External_Rela
);
5855 /* Fix up the remaining .rela.plt.unloaded relocations.
5856 They may have the wrong symbol index for _G_O_T_ or
5857 _P_L_T_ depending on the order in which symbols were
5859 while (loc
< htab
->srelplt2
->contents
+ htab
->srelplt2
->size
)
5861 /* The PLT entry's pointer to the .got.plt slot. */
5862 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
5863 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
,
5865 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5866 loc
+= sizeof (Elf32_External_Rela
);
5868 /* The .got.plt slot's pointer to .plt. */
5869 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
5870 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
,
5872 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5873 loc
+= sizeof (Elf32_External_Rela
);
5877 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5878 really seem like the right value. */
5879 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
5883 /* Fill in the first three entries in the global offset table. */
5884 if (sgot
&& sgot
->size
> 0)
5887 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
5889 bfd_put_32 (output_bfd
,
5890 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
5892 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
5893 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
5895 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
5901 static enum elf_reloc_type_class
5902 sh_elf_reloc_type_class (const Elf_Internal_Rela
*rela
)
5904 switch ((int) ELF32_R_TYPE (rela
->r_info
))
5907 return reloc_class_relative
;
5909 return reloc_class_plt
;
5911 return reloc_class_copy
;
5913 return reloc_class_normal
;
5917 #if !defined SH_TARGET_ALREADY_DEFINED
5918 /* Support for Linux core dump NOTE sections. */
5921 elf32_shlin_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5926 switch (note
->descsz
)
5931 case 168: /* Linux/SH */
5933 elf_tdata (abfd
)->core_signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
5936 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
5945 /* Make a ".reg/999" section. */
5946 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5947 size
, note
->descpos
+ offset
);
5951 elf32_shlin_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5953 switch (note
->descsz
)
5958 case 124: /* Linux/SH elf_prpsinfo */
5959 elf_tdata (abfd
)->core_program
5960 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
5961 elf_tdata (abfd
)->core_command
5962 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
5965 /* Note that for some reason, a spurious space is tacked
5966 onto the end of the args in some (at least one anyway)
5967 implementations, so strip it off if it exists. */
5970 char *command
= elf_tdata (abfd
)->core_command
;
5971 int n
= strlen (command
);
5973 if (0 < n
&& command
[n
- 1] == ' ')
5974 command
[n
- 1] = '\0';
5979 #endif /* not SH_TARGET_ALREADY_DEFINED */
5982 /* Return address for Ith PLT stub in section PLT, for relocation REL
5983 or (bfd_vma) -1 if it should not be included. */
5986 sh_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
5987 const arelent
*rel ATTRIBUTE_UNUSED
)
5989 const struct elf_sh_plt_info
*plt_info
;
5991 plt_info
= get_plt_info (plt
->owner
, (plt
->owner
->flags
& DYNAMIC
) != 0);
5992 return plt
->vma
+ get_plt_offset (plt_info
, i
);
5995 #if !defined SH_TARGET_ALREADY_DEFINED
5996 #define TARGET_BIG_SYM bfd_elf32_sh_vec
5997 #define TARGET_BIG_NAME "elf32-sh"
5998 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
5999 #define TARGET_LITTLE_NAME "elf32-shl"
6002 #define ELF_ARCH bfd_arch_sh
6003 #define ELF_MACHINE_CODE EM_SH
6004 #ifdef __QNXTARGET__
6005 #define ELF_MAXPAGESIZE 0x1000
6007 #define ELF_MAXPAGESIZE 0x80
6010 #define elf_symbol_leading_char '_'
6012 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6013 #define elf_info_to_howto sh_elf_info_to_howto
6014 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
6015 #define elf_backend_relocate_section sh_elf_relocate_section
6016 #define bfd_elf32_bfd_get_relocated_section_contents \
6017 sh_elf_get_relocated_section_contents
6018 #define bfd_elf32_mkobject sh_elf_mkobject
6019 #define elf_backend_object_p sh_elf_object_p
6020 #define bfd_elf32_bfd_set_private_bfd_flags \
6021 sh_elf_set_private_flags
6022 #define bfd_elf32_bfd_copy_private_bfd_data \
6023 sh_elf_copy_private_data
6024 #define bfd_elf32_bfd_merge_private_bfd_data \
6025 sh_elf_merge_private_data
6027 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6028 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
6029 #define elf_backend_check_relocs sh_elf_check_relocs
6030 #define elf_backend_copy_indirect_symbol \
6031 sh_elf_copy_indirect_symbol
6032 #define elf_backend_create_dynamic_sections \
6033 sh_elf_create_dynamic_sections
6034 #define bfd_elf32_bfd_link_hash_table_create \
6035 sh_elf_link_hash_table_create
6036 #define elf_backend_adjust_dynamic_symbol \
6037 sh_elf_adjust_dynamic_symbol
6038 #define elf_backend_always_size_sections \
6039 sh_elf_always_size_sections
6040 #define elf_backend_size_dynamic_sections \
6041 sh_elf_size_dynamic_sections
6042 #define elf_backend_omit_section_dynsym \
6043 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
6044 #define elf_backend_finish_dynamic_symbol \
6045 sh_elf_finish_dynamic_symbol
6046 #define elf_backend_finish_dynamic_sections \
6047 sh_elf_finish_dynamic_sections
6048 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
6049 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
6051 #define elf_backend_can_gc_sections 1
6052 #define elf_backend_can_refcount 1
6053 #define elf_backend_want_got_plt 1
6054 #define elf_backend_plt_readonly 1
6055 #define elf_backend_want_plt_sym 0
6056 #define elf_backend_got_header_size 12
6058 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
6060 #include "elf32-target.h"
6062 /* NetBSD support. */
6063 #undef TARGET_BIG_SYM
6064 #define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
6065 #undef TARGET_BIG_NAME
6066 #define TARGET_BIG_NAME "elf32-sh-nbsd"
6067 #undef TARGET_LITTLE_SYM
6068 #define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
6069 #undef TARGET_LITTLE_NAME
6070 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6071 #undef ELF_MAXPAGESIZE
6072 #define ELF_MAXPAGESIZE 0x10000
6073 #undef ELF_COMMONPAGESIZE
6074 #undef elf_symbol_leading_char
6075 #define elf_symbol_leading_char 0
6077 #define elf32_bed elf32_sh_nbsd_bed
6079 #include "elf32-target.h"
6082 /* Linux support. */
6083 #undef TARGET_BIG_SYM
6084 #define TARGET_BIG_SYM bfd_elf32_shblin_vec
6085 #undef TARGET_BIG_NAME
6086 #define TARGET_BIG_NAME "elf32-shbig-linux"
6087 #undef TARGET_LITTLE_SYM
6088 #define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
6089 #undef TARGET_LITTLE_NAME
6090 #define TARGET_LITTLE_NAME "elf32-sh-linux"
6091 #undef ELF_COMMONPAGESIZE
6092 #define ELF_COMMONPAGESIZE 0x1000
6094 #undef elf_backend_grok_prstatus
6095 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6096 #undef elf_backend_grok_psinfo
6097 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
6099 #define elf32_bed elf32_sh_lin_bed
6101 #include "elf32-target.h"
6103 #undef TARGET_BIG_SYM
6104 #define TARGET_BIG_SYM bfd_elf32_shvxworks_vec
6105 #undef TARGET_BIG_NAME
6106 #define TARGET_BIG_NAME "elf32-sh-vxworks"
6107 #undef TARGET_LITTLE_SYM
6108 #define TARGET_LITTLE_SYM bfd_elf32_shlvxworks_vec
6109 #undef TARGET_LITTLE_NAME
6110 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6112 #define elf32_bed elf32_sh_vxworks_bed
6114 #undef elf_backend_want_plt_sym
6115 #define elf_backend_want_plt_sym 1
6116 #undef elf_symbol_leading_char
6117 #define elf_symbol_leading_char '_'
6118 #define elf_backend_want_got_underscore 1
6119 #undef elf_backend_grok_prstatus
6120 #undef elf_backend_grok_psinfo
6121 #undef elf_backend_add_symbol_hook
6122 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6123 #undef elf_backend_link_output_symbol_hook
6124 #define elf_backend_link_output_symbol_hook \
6125 elf_vxworks_link_output_symbol_hook
6126 #undef elf_backend_emit_relocs
6127 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6128 #undef elf_backend_final_write_processing
6129 #define elf_backend_final_write_processing \
6130 elf_vxworks_final_write_processing
6131 #undef ELF_MAXPAGESIZE
6132 #define ELF_MAXPAGESIZE 0x1000
6133 #undef ELF_COMMONPAGESIZE
6135 #include "elf32-target.h"
6137 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */