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, 2008 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 3 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,
21 MA 02110-1301, USA. */
28 #include "elf-vxworks.h"
30 #include "libiberty.h"
31 #include "../opcodes/sh-opc.h"
33 static bfd_reloc_status_type sh_elf_reloc
34 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
35 static bfd_reloc_status_type sh_elf_ignore_reloc
36 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
37 static bfd_boolean sh_elf_relax_delete_bytes
38 (bfd
*, asection
*, bfd_vma
, int);
39 static bfd_boolean sh_elf_align_loads
40 (bfd
*, asection
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_boolean
*);
42 static bfd_boolean sh_elf_swap_insns
43 (bfd
*, asection
*, void *, bfd_byte
*, bfd_vma
);
45 static int sh_elf_optimized_tls_reloc
46 (struct bfd_link_info
*, int, int);
47 static bfd_vma dtpoff_base
48 (struct bfd_link_info
*);
50 (struct bfd_link_info
*, bfd_vma
);
52 /* The name of the dynamic interpreter. This is put in the .interp
55 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
57 #define MINUS_ONE ((bfd_vma) 0 - 1)
59 #define SH_PARTIAL32 TRUE
60 #define SH_SRC_MASK32 0xffffffff
61 #define SH_ELF_RELOC sh_elf_reloc
62 static reloc_howto_type sh_elf_howto_table
[] =
64 #include "elf32-sh-relocs.h"
67 #define SH_PARTIAL32 FALSE
68 #define SH_SRC_MASK32 0
69 #define SH_ELF_RELOC bfd_elf_generic_reloc
70 static reloc_howto_type sh_vxworks_howto_table
[] =
72 #include "elf32-sh-relocs.h"
75 /* Return true if OUTPUT_BFD is a VxWorks object. */
78 vxworks_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
80 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
81 extern const bfd_target bfd_elf32_shlvxworks_vec
;
82 extern const bfd_target bfd_elf32_shvxworks_vec
;
84 return (abfd
->xvec
== &bfd_elf32_shlvxworks_vec
85 || abfd
->xvec
== &bfd_elf32_shvxworks_vec
);
91 /* Return the howto table for ABFD. */
93 static reloc_howto_type
*
94 get_howto_table (bfd
*abfd
)
96 if (vxworks_object_p (abfd
))
97 return sh_vxworks_howto_table
;
98 return sh_elf_howto_table
;
101 static bfd_reloc_status_type
102 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
103 asection
*input_section
, bfd_byte
*contents
,
104 bfd_vma addr
, asection
*symbol_section
,
105 bfd_vma start
, bfd_vma end
)
107 static bfd_vma last_addr
;
108 static asection
*last_symbol_section
;
109 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
114 /* Sanity check the address. */
115 if (addr
> bfd_get_section_limit (input_bfd
, input_section
))
116 return bfd_reloc_outofrange
;
118 /* We require the start and end relocations to be processed consecutively -
119 although we allow then to be processed forwards or backwards. */
123 last_symbol_section
= symbol_section
;
126 if (last_addr
!= addr
)
130 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
131 return bfd_reloc_outofrange
;
133 /* Get the symbol_section contents. */
134 if (symbol_section
!= input_section
)
136 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
137 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
140 if (!bfd_malloc_and_get_section (input_bfd
, symbol_section
,
143 if (contents
!= NULL
)
145 return bfd_reloc_outofrange
;
149 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
150 start_ptr
= contents
+ start
;
151 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
153 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
156 diff
= (last_ptr
- ptr
) >> 1;
157 cum_diff
+= diff
& 1;
160 /* Calculate the start / end values to load into rs / re minus four -
161 so that will cancel out the four we would otherwise have to add to
162 addr to get the value to subtract in order to get relative addressing. */
166 end
= (ptr
+ cum_diff
* 2) - contents
;
170 bfd_vma start0
= start
- 4;
172 while (start0
&& IS_PPI (contents
+ start0
))
174 start0
= start
- 2 - ((start
- start0
) & 2);
175 start
= start0
- cum_diff
- 2;
180 && elf_section_data (symbol_section
)->this_hdr
.contents
!= contents
)
183 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
185 x
= (insn
& 0x200 ? end
: start
) - addr
;
186 if (input_section
!= symbol_section
)
187 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
188 - (input_section
->output_section
->vma
189 + input_section
->output_offset
));
191 if (x
< -128 || x
> 127)
192 return bfd_reloc_overflow
;
194 x
= (insn
& ~0xff) | (x
& 0xff);
195 bfd_put_16 (input_bfd
, (bfd_vma
) x
, contents
+ addr
);
200 /* This function is used for normal relocs. This used to be like the COFF
201 function, and is almost certainly incorrect for other ELF targets. */
203 static bfd_reloc_status_type
204 sh_elf_reloc (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol_in
,
205 void *data
, asection
*input_section
, bfd
*output_bfd
,
206 char **error_message ATTRIBUTE_UNUSED
)
210 enum elf_sh_reloc_type r_type
;
211 bfd_vma addr
= reloc_entry
->address
;
212 bfd_byte
*hit_data
= addr
+ (bfd_byte
*) data
;
214 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
216 if (output_bfd
!= NULL
)
218 /* Partial linking--do nothing. */
219 reloc_entry
->address
+= input_section
->output_offset
;
223 /* Almost all relocs have to do with relaxing. If any work must be
224 done for them, it has been done in sh_relax_section. */
225 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
228 if (symbol_in
!= NULL
229 && bfd_is_und_section (symbol_in
->section
))
230 return bfd_reloc_undefined
;
232 if (bfd_is_com_section (symbol_in
->section
))
235 sym_value
= (symbol_in
->value
+
236 symbol_in
->section
->output_section
->vma
+
237 symbol_in
->section
->output_offset
);
242 insn
= bfd_get_32 (abfd
, hit_data
);
243 insn
+= sym_value
+ reloc_entry
->addend
;
244 bfd_put_32 (abfd
, (bfd_vma
) insn
, hit_data
);
247 insn
= bfd_get_16 (abfd
, hit_data
);
248 sym_value
+= reloc_entry
->addend
;
249 sym_value
-= (input_section
->output_section
->vma
250 + input_section
->output_offset
253 sym_value
+= (insn
& 0xfff) << 1;
256 insn
= (insn
& 0xf000) | (sym_value
& 0xfff);
257 bfd_put_16 (abfd
, (bfd_vma
) insn
, hit_data
);
258 if (sym_value
< (bfd_vma
) -0x1000 || sym_value
>= 0x1000)
259 return bfd_reloc_overflow
;
269 /* This function is used for relocs which are only used for relaxing,
270 which the linker should otherwise ignore. */
272 static bfd_reloc_status_type
273 sh_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
274 asymbol
*symbol ATTRIBUTE_UNUSED
,
275 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
277 char **error_message ATTRIBUTE_UNUSED
)
279 if (output_bfd
!= NULL
)
280 reloc_entry
->address
+= input_section
->output_offset
;
284 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
288 bfd_reloc_code_real_type bfd_reloc_val
;
289 unsigned char elf_reloc_val
;
292 /* An array mapping BFD reloc codes to SH ELF relocs. */
294 static const struct elf_reloc_map sh_reloc_map
[] =
296 { BFD_RELOC_NONE
, R_SH_NONE
},
297 { BFD_RELOC_32
, R_SH_DIR32
},
298 { BFD_RELOC_16
, R_SH_DIR16
},
299 { BFD_RELOC_8
, R_SH_DIR8
},
300 { BFD_RELOC_CTOR
, R_SH_DIR32
},
301 { BFD_RELOC_32_PCREL
, R_SH_REL32
},
302 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_DIR8WPN
},
303 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_IND12W
},
304 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_DIR8WPZ
},
305 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_DIR8WPL
},
306 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
307 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
308 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
309 { BFD_RELOC_SH_USES
, R_SH_USES
},
310 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
311 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
312 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
313 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
314 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
315 { BFD_RELOC_VTABLE_INHERIT
, R_SH_GNU_VTINHERIT
},
316 { BFD_RELOC_VTABLE_ENTRY
, R_SH_GNU_VTENTRY
},
317 { BFD_RELOC_SH_LOOP_START
, R_SH_LOOP_START
},
318 { BFD_RELOC_SH_LOOP_END
, R_SH_LOOP_END
},
319 { BFD_RELOC_SH_TLS_GD_32
, R_SH_TLS_GD_32
},
320 { BFD_RELOC_SH_TLS_LD_32
, R_SH_TLS_LD_32
},
321 { BFD_RELOC_SH_TLS_LDO_32
, R_SH_TLS_LDO_32
},
322 { BFD_RELOC_SH_TLS_IE_32
, R_SH_TLS_IE_32
},
323 { BFD_RELOC_SH_TLS_LE_32
, R_SH_TLS_LE_32
},
324 { BFD_RELOC_SH_TLS_DTPMOD32
, R_SH_TLS_DTPMOD32
},
325 { BFD_RELOC_SH_TLS_DTPOFF32
, R_SH_TLS_DTPOFF32
},
326 { BFD_RELOC_SH_TLS_TPOFF32
, R_SH_TLS_TPOFF32
},
327 { BFD_RELOC_32_GOT_PCREL
, R_SH_GOT32
},
328 { BFD_RELOC_32_PLT_PCREL
, R_SH_PLT32
},
329 { BFD_RELOC_SH_COPY
, R_SH_COPY
},
330 { BFD_RELOC_SH_GLOB_DAT
, R_SH_GLOB_DAT
},
331 { BFD_RELOC_SH_JMP_SLOT
, R_SH_JMP_SLOT
},
332 { BFD_RELOC_SH_RELATIVE
, R_SH_RELATIVE
},
333 { BFD_RELOC_32_GOTOFF
, R_SH_GOTOFF
},
334 { BFD_RELOC_SH_GOTPC
, R_SH_GOTPC
},
335 { BFD_RELOC_SH_GOTPLT32
, R_SH_GOTPLT32
},
336 #ifdef INCLUDE_SHMEDIA
337 { BFD_RELOC_SH_GOT_LOW16
, R_SH_GOT_LOW16
},
338 { BFD_RELOC_SH_GOT_MEDLOW16
, R_SH_GOT_MEDLOW16
},
339 { BFD_RELOC_SH_GOT_MEDHI16
, R_SH_GOT_MEDHI16
},
340 { BFD_RELOC_SH_GOT_HI16
, R_SH_GOT_HI16
},
341 { BFD_RELOC_SH_GOTPLT_LOW16
, R_SH_GOTPLT_LOW16
},
342 { BFD_RELOC_SH_GOTPLT_MEDLOW16
, R_SH_GOTPLT_MEDLOW16
},
343 { BFD_RELOC_SH_GOTPLT_MEDHI16
, R_SH_GOTPLT_MEDHI16
},
344 { BFD_RELOC_SH_GOTPLT_HI16
, R_SH_GOTPLT_HI16
},
345 { BFD_RELOC_SH_PLT_LOW16
, R_SH_PLT_LOW16
},
346 { BFD_RELOC_SH_PLT_MEDLOW16
, R_SH_PLT_MEDLOW16
},
347 { BFD_RELOC_SH_PLT_MEDHI16
, R_SH_PLT_MEDHI16
},
348 { BFD_RELOC_SH_PLT_HI16
, R_SH_PLT_HI16
},
349 { BFD_RELOC_SH_GOTOFF_LOW16
, R_SH_GOTOFF_LOW16
},
350 { BFD_RELOC_SH_GOTOFF_MEDLOW16
, R_SH_GOTOFF_MEDLOW16
},
351 { BFD_RELOC_SH_GOTOFF_MEDHI16
, R_SH_GOTOFF_MEDHI16
},
352 { BFD_RELOC_SH_GOTOFF_HI16
, R_SH_GOTOFF_HI16
},
353 { BFD_RELOC_SH_GOTPC_LOW16
, R_SH_GOTPC_LOW16
},
354 { BFD_RELOC_SH_GOTPC_MEDLOW16
, R_SH_GOTPC_MEDLOW16
},
355 { BFD_RELOC_SH_GOTPC_MEDHI16
, R_SH_GOTPC_MEDHI16
},
356 { BFD_RELOC_SH_GOTPC_HI16
, R_SH_GOTPC_HI16
},
357 { BFD_RELOC_SH_COPY64
, R_SH_COPY64
},
358 { BFD_RELOC_SH_GLOB_DAT64
, R_SH_GLOB_DAT64
},
359 { BFD_RELOC_SH_JMP_SLOT64
, R_SH_JMP_SLOT64
},
360 { BFD_RELOC_SH_RELATIVE64
, R_SH_RELATIVE64
},
361 { BFD_RELOC_SH_GOT10BY4
, R_SH_GOT10BY4
},
362 { BFD_RELOC_SH_GOT10BY8
, R_SH_GOT10BY8
},
363 { BFD_RELOC_SH_GOTPLT10BY4
, R_SH_GOTPLT10BY4
},
364 { BFD_RELOC_SH_GOTPLT10BY8
, R_SH_GOTPLT10BY8
},
365 { BFD_RELOC_SH_PT_16
, R_SH_PT_16
},
366 { BFD_RELOC_SH_SHMEDIA_CODE
, R_SH_SHMEDIA_CODE
},
367 { BFD_RELOC_SH_IMMU5
, R_SH_DIR5U
},
368 { BFD_RELOC_SH_IMMS6
, R_SH_DIR6S
},
369 { BFD_RELOC_SH_IMMU6
, R_SH_DIR6U
},
370 { BFD_RELOC_SH_IMMS10
, R_SH_DIR10S
},
371 { BFD_RELOC_SH_IMMS10BY2
, R_SH_DIR10SW
},
372 { BFD_RELOC_SH_IMMS10BY4
, R_SH_DIR10SL
},
373 { BFD_RELOC_SH_IMMS10BY8
, R_SH_DIR10SQ
},
374 { BFD_RELOC_SH_IMMS16
, R_SH_IMMS16
},
375 { BFD_RELOC_SH_IMMU16
, R_SH_IMMU16
},
376 { BFD_RELOC_SH_IMM_LOW16
, R_SH_IMM_LOW16
},
377 { BFD_RELOC_SH_IMM_LOW16_PCREL
, R_SH_IMM_LOW16_PCREL
},
378 { BFD_RELOC_SH_IMM_MEDLOW16
, R_SH_IMM_MEDLOW16
},
379 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL
, R_SH_IMM_MEDLOW16_PCREL
},
380 { BFD_RELOC_SH_IMM_MEDHI16
, R_SH_IMM_MEDHI16
},
381 { BFD_RELOC_SH_IMM_MEDHI16_PCREL
, R_SH_IMM_MEDHI16_PCREL
},
382 { BFD_RELOC_SH_IMM_HI16
, R_SH_IMM_HI16
},
383 { BFD_RELOC_SH_IMM_HI16_PCREL
, R_SH_IMM_HI16_PCREL
},
384 { BFD_RELOC_64
, R_SH_64
},
385 { BFD_RELOC_64_PCREL
, R_SH_64_PCREL
},
386 #endif /* not INCLUDE_SHMEDIA */
389 /* Given a BFD reloc code, return the howto structure for the
390 corresponding SH ELF reloc. */
392 static reloc_howto_type
*
393 sh_elf_reloc_type_lookup (bfd
*abfd
, bfd_reloc_code_real_type code
)
397 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
399 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
400 return get_howto_table (abfd
) + (int) sh_reloc_map
[i
].elf_reloc_val
;
406 static reloc_howto_type
*
407 sh_elf_reloc_name_lookup (bfd
*abfd
, const char *r_name
)
411 if (vxworks_object_p (abfd
))
414 i
< (sizeof (sh_vxworks_howto_table
)
415 / sizeof (sh_vxworks_howto_table
[0]));
417 if (sh_vxworks_howto_table
[i
].name
!= NULL
418 && strcasecmp (sh_vxworks_howto_table
[i
].name
, r_name
) == 0)
419 return &sh_vxworks_howto_table
[i
];
424 i
< (sizeof (sh_elf_howto_table
)
425 / sizeof (sh_elf_howto_table
[0]));
427 if (sh_elf_howto_table
[i
].name
!= NULL
428 && strcasecmp (sh_elf_howto_table
[i
].name
, r_name
) == 0)
429 return &sh_elf_howto_table
[i
];
435 /* Given an ELF reloc, fill in the howto field of a relent. */
438 sh_elf_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
, Elf_Internal_Rela
*dst
)
442 r
= ELF32_R_TYPE (dst
->r_info
);
444 BFD_ASSERT (r
< (unsigned int) R_SH_max
);
445 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC
|| r
> R_SH_LAST_INVALID_RELOC
);
446 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_2
|| r
> R_SH_LAST_INVALID_RELOC_2
);
447 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_3
|| r
> R_SH_LAST_INVALID_RELOC_3
);
448 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_4
|| r
> R_SH_LAST_INVALID_RELOC_4
);
449 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_5
|| r
> R_SH_LAST_INVALID_RELOC_5
);
451 cache_ptr
->howto
= get_howto_table (abfd
) + r
;
454 /* This function handles relaxing for SH ELF. See the corresponding
455 function in coff-sh.c for a description of what this does. FIXME:
456 There is a lot of duplication here between this code and the COFF
457 specific code. The format of relocs and symbols is wound deeply
458 into this code, but it would still be better if the duplication
459 could be eliminated somehow. Note in particular that although both
460 functions use symbols like R_SH_CODE, those symbols have different
461 values; in coff-sh.c they come from include/coff/sh.h, whereas here
462 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
465 sh_elf_relax_section (bfd
*abfd
, asection
*sec
,
466 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
468 Elf_Internal_Shdr
*symtab_hdr
;
469 Elf_Internal_Rela
*internal_relocs
;
470 bfd_boolean have_code
;
471 Elf_Internal_Rela
*irel
, *irelend
;
472 bfd_byte
*contents
= NULL
;
473 Elf_Internal_Sym
*isymbuf
= NULL
;
477 if (link_info
->relocatable
478 || (sec
->flags
& SEC_RELOC
) == 0
479 || sec
->reloc_count
== 0)
482 #ifdef INCLUDE_SHMEDIA
483 if (elf_section_data (sec
)->this_hdr
.sh_flags
484 & (SHF_SH5_ISA32
| SHF_SH5_ISA32_MIXED
))
490 symtab_hdr
= &elf_symtab_hdr (abfd
);
492 internal_relocs
= (_bfd_elf_link_read_relocs
493 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
494 link_info
->keep_memory
));
495 if (internal_relocs
== NULL
)
500 irelend
= internal_relocs
+ sec
->reloc_count
;
501 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
503 bfd_vma laddr
, paddr
, symval
;
505 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
508 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
511 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
514 /* Get the section contents. */
515 if (contents
== NULL
)
517 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
518 contents
= elf_section_data (sec
)->this_hdr
.contents
;
521 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
526 /* The r_addend field of the R_SH_USES reloc will point us to
527 the register load. The 4 is because the r_addend field is
528 computed as though it were a jump offset, which are based
529 from 4 bytes after the jump instruction. */
530 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
531 if (laddr
>= sec
->size
)
533 (*_bfd_error_handler
) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
535 (unsigned long) irel
->r_offset
);
538 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
540 /* If the instruction is not mov.l NN,rN, we don't know what to
542 if ((insn
& 0xf000) != 0xd000)
544 ((*_bfd_error_handler
)
545 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
546 abfd
, (unsigned long) irel
->r_offset
, insn
));
550 /* Get the address from which the register is being loaded. The
551 displacement in the mov.l instruction is quadrupled. It is a
552 displacement from four bytes after the movl instruction, but,
553 before adding in the PC address, two least significant bits
554 of the PC are cleared. We assume that the section is aligned
555 on a four byte boundary. */
558 paddr
+= (laddr
+ 4) &~ (bfd_vma
) 3;
559 if (paddr
>= sec
->size
)
561 ((*_bfd_error_handler
)
562 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
563 abfd
, (unsigned long) irel
->r_offset
));
567 /* Get the reloc for the address from which the register is
568 being loaded. This reloc will tell us which function is
569 actually being called. */
570 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
571 if (irelfn
->r_offset
== paddr
572 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
574 if (irelfn
>= irelend
)
576 ((*_bfd_error_handler
)
577 (_("%B: 0x%lx: warning: could not find expected reloc"),
578 abfd
, (unsigned long) paddr
));
582 /* Read this BFD's symbols if we haven't done so already. */
583 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
585 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
587 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
588 symtab_hdr
->sh_info
, 0,
594 /* Get the value of the symbol referred to by the reloc. */
595 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
597 /* A local symbol. */
598 Elf_Internal_Sym
*isym
;
600 isym
= isymbuf
+ ELF32_R_SYM (irelfn
->r_info
);
602 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd
, sec
))
604 ((*_bfd_error_handler
)
605 (_("%B: 0x%lx: warning: symbol in unexpected section"),
606 abfd
, (unsigned long) paddr
));
610 symval
= (isym
->st_value
611 + sec
->output_section
->vma
612 + sec
->output_offset
);
617 struct elf_link_hash_entry
*h
;
619 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
620 h
= elf_sym_hashes (abfd
)[indx
];
621 BFD_ASSERT (h
!= NULL
);
622 if (h
->root
.type
!= bfd_link_hash_defined
623 && h
->root
.type
!= bfd_link_hash_defweak
)
625 /* This appears to be a reference to an undefined
626 symbol. Just ignore it--it will be caught by the
627 regular reloc processing. */
631 symval
= (h
->root
.u
.def
.value
632 + h
->root
.u
.def
.section
->output_section
->vma
633 + h
->root
.u
.def
.section
->output_offset
);
636 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
637 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
639 symval
+= irelfn
->r_addend
;
641 /* See if this function call can be shortened. */
644 + sec
->output_section
->vma
647 /* A branch to an address beyond ours might be increased by an
648 .align that doesn't move when bytes behind us are deleted.
649 So, we add some slop in this calculation to allow for
651 if (foff
< -0x1000 || foff
>= 0x1000 - 8)
653 /* After all that work, we can't shorten this function call. */
657 /* Shorten the function call. */
659 /* For simplicity of coding, we are going to modify the section
660 contents, the section relocs, and the BFD symbol table. We
661 must tell the rest of the code not to free up this
662 information. It would be possible to instead create a table
663 of changes which have to be made, as is done in coff-mips.c;
664 that would be more work, but would require less memory when
665 the linker is run. */
667 elf_section_data (sec
)->relocs
= internal_relocs
;
668 elf_section_data (sec
)->this_hdr
.contents
= contents
;
669 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
671 /* Replace the jsr with a bsr. */
673 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
674 replace the jsr with a bsr. */
675 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
676 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
677 here, but that only checks if the symbol is an external symbol,
678 not if the symbol is in a different section. Besides, we need
679 a consistent meaning for the relocation, so we just assume here that
680 the value of the symbol is not available. */
682 /* We can't fully resolve this yet, because the external
683 symbol value may be changed by future relaxing. We let
684 the final link phase handle it. */
685 bfd_put_16 (abfd
, (bfd_vma
) 0xb000, contents
+ irel
->r_offset
);
689 /* When we calculated the symbol "value" we had an offset in the
690 DIR32's word in memory (we read and add it above). However,
691 the jsr we create does NOT have this offset encoded, so we
692 have to add it to the addend to preserve it. */
693 irel
->r_addend
+= bfd_get_32 (abfd
, contents
+ paddr
);
695 /* See if there is another R_SH_USES reloc referring to the same
697 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
698 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
699 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
701 if (irelscan
< irelend
)
703 /* Some other function call depends upon this register load,
704 and we have not yet converted that function call.
705 Indeed, we may never be able to convert it. There is
706 nothing else we can do at this point. */
710 /* Look for a R_SH_COUNT reloc on the location where the
711 function address is stored. Do this before deleting any
712 bytes, to avoid confusion about the address. */
713 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
714 if (irelcount
->r_offset
== paddr
715 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
718 /* Delete the register load. */
719 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
722 /* That will change things, so, just in case it permits some
723 other function call to come within range, we should relax
724 again. Note that this is not required, and it may be slow. */
727 /* Now check whether we got a COUNT reloc. */
728 if (irelcount
>= irelend
)
730 ((*_bfd_error_handler
)
731 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
732 abfd
, (unsigned long) paddr
));
736 /* The number of uses is stored in the r_addend field. We've
738 if (irelcount
->r_addend
== 0)
740 ((*_bfd_error_handler
) (_("%B: 0x%lx: warning: bad count"),
742 (unsigned long) paddr
));
746 --irelcount
->r_addend
;
748 /* If there are no more uses, we can delete the address. Reload
749 the address from irelfn, in case it was changed by the
750 previous call to sh_elf_relax_delete_bytes. */
751 if (irelcount
->r_addend
== 0)
753 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
757 /* We've done all we can with that function call. */
760 /* Look for load and store instructions that we can align on four
762 if ((elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
) != EF_SH4
767 /* Get the section contents. */
768 if (contents
== NULL
)
770 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
771 contents
= elf_section_data (sec
)->this_hdr
.contents
;
774 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
779 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
785 elf_section_data (sec
)->relocs
= internal_relocs
;
786 elf_section_data (sec
)->this_hdr
.contents
= contents
;
787 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
792 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
794 if (! link_info
->keep_memory
)
798 /* Cache the symbols for elf_link_input_bfd. */
799 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
804 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
806 if (! link_info
->keep_memory
)
810 /* Cache the section contents for elf_link_input_bfd. */
811 elf_section_data (sec
)->this_hdr
.contents
= contents
;
815 if (internal_relocs
!= NULL
816 && elf_section_data (sec
)->relocs
!= internal_relocs
)
817 free (internal_relocs
);
823 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
826 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
828 if (internal_relocs
!= NULL
829 && elf_section_data (sec
)->relocs
!= internal_relocs
)
830 free (internal_relocs
);
835 /* Delete some bytes from a section while relaxing. FIXME: There is a
836 lot of duplication between this function and sh_relax_delete_bytes
840 sh_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
,
843 Elf_Internal_Shdr
*symtab_hdr
;
844 unsigned int sec_shndx
;
846 Elf_Internal_Rela
*irel
, *irelend
;
847 Elf_Internal_Rela
*irelalign
;
849 Elf_Internal_Sym
*isymbuf
, *isym
, *isymend
;
850 struct elf_link_hash_entry
**sym_hashes
;
851 struct elf_link_hash_entry
**end_hashes
;
852 unsigned int symcount
;
855 symtab_hdr
= &elf_symtab_hdr (abfd
);
856 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
858 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
860 contents
= elf_section_data (sec
)->this_hdr
.contents
;
862 /* The deletion must stop at the next ALIGN reloc for an aligment
863 power larger than the number of bytes we are deleting. */
868 irel
= elf_section_data (sec
)->relocs
;
869 irelend
= irel
+ sec
->reloc_count
;
870 for (; irel
< irelend
; irel
++)
872 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
873 && irel
->r_offset
> addr
874 && count
< (1 << irel
->r_addend
))
877 toaddr
= irel
->r_offset
;
882 /* Actually delete the bytes. */
883 memmove (contents
+ addr
, contents
+ addr
+ count
,
884 (size_t) (toaddr
- addr
- count
));
885 if (irelalign
== NULL
)
891 #define NOP_OPCODE (0x0009)
893 BFD_ASSERT ((count
& 1) == 0);
894 for (i
= 0; i
< count
; i
+= 2)
895 bfd_put_16 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
898 /* Adjust all the relocs. */
899 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
901 bfd_vma nraddr
, stop
;
904 int off
, adjust
, oinsn
;
905 bfd_signed_vma voff
= 0;
906 bfd_boolean overflow
;
908 /* Get the new reloc address. */
909 nraddr
= irel
->r_offset
;
910 if ((irel
->r_offset
> addr
911 && irel
->r_offset
< toaddr
)
912 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
913 && irel
->r_offset
== toaddr
))
916 /* See if this reloc was for the bytes we have deleted, in which
917 case we no longer care about it. Don't delete relocs which
918 represent addresses, though. */
919 if (irel
->r_offset
>= addr
920 && irel
->r_offset
< addr
+ count
921 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
922 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
923 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
924 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
925 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
928 /* If this is a PC relative reloc, see if the range it covers
929 includes the bytes we have deleted. */
930 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
939 start
= irel
->r_offset
;
940 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
944 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
951 /* If this reloc is against a symbol defined in this
952 section, and the symbol will not be adjusted below, we
953 must check the addend to see it will put the value in
954 range to be adjusted, and hence must be changed. */
955 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
957 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
958 if (isym
->st_shndx
== sec_shndx
959 && (isym
->st_value
<= addr
960 || isym
->st_value
>= toaddr
))
964 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
966 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
967 val
+= isym
->st_value
;
968 if (val
> addr
&& val
< toaddr
)
969 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
973 val
= isym
->st_value
+ irel
->r_addend
;
974 if (val
> addr
&& val
< toaddr
)
975 irel
->r_addend
-= count
;
986 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
993 /* This has been made by previous relaxation. Since the
994 relocation will be against an external symbol, the
995 final relocation will just do the right thing. */
1002 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1004 /* The addend will be against the section symbol, thus
1005 for adjusting the addend, the relevant start is the
1006 start of the section.
1007 N.B. If we want to abandon in-place changes here and
1008 test directly using symbol + addend, we have to take into
1009 account that the addend has already been adjusted by -4. */
1010 if (stop
> addr
&& stop
< toaddr
)
1011 irel
->r_addend
-= count
;
1017 stop
= start
+ 4 + off
* 2;
1022 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
1028 /* These relocs types represent
1030 The r_addend field holds the difference between the reloc
1031 address and L1. That is the start of the reloc, and
1032 adding in the contents gives us the top. We must adjust
1033 both the r_offset field and the section contents.
1034 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1035 and the elf bfd r_offset is called r_vaddr. */
1037 stop
= irel
->r_offset
;
1038 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
1042 && (stop
<= addr
|| stop
>= toaddr
))
1043 irel
->r_addend
+= count
;
1044 else if (stop
> addr
1046 && (start
<= addr
|| start
>= toaddr
))
1047 irel
->r_addend
-= count
;
1049 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1050 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1051 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1052 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1054 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1055 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1060 start
= irel
->r_offset
;
1061 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1062 + (long) irel
->r_addend
1069 && (stop
<= addr
|| stop
>= toaddr
))
1071 else if (stop
> addr
1073 && (start
<= addr
|| start
>= toaddr
))
1082 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1091 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1093 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1098 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1100 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1104 BFD_ASSERT (adjust
== count
|| count
>= 4);
1109 if ((irel
->r_offset
& 3) == 0)
1112 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1114 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1119 if (voff
< 0 || voff
>= 0xff)
1121 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1126 if (voff
< - 0x8000 || voff
>= 0x8000)
1128 bfd_put_signed_16 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1133 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1137 irel
->r_addend
+= adjust
;
1143 ((*_bfd_error_handler
)
1144 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1145 abfd
, (unsigned long) irel
->r_offset
));
1146 bfd_set_error (bfd_error_bad_value
);
1151 irel
->r_offset
= nraddr
;
1154 /* Look through all the other sections. If there contain any IMM32
1155 relocs against internal symbols which we are not going to adjust
1156 below, we may need to adjust the addends. */
1157 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1159 Elf_Internal_Rela
*internal_relocs
;
1160 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1161 bfd_byte
*ocontents
;
1164 || (o
->flags
& SEC_RELOC
) == 0
1165 || o
->reloc_count
== 0)
1168 /* We always cache the relocs. Perhaps, if info->keep_memory is
1169 FALSE, we should free them, if we are permitted to, when we
1170 leave sh_coff_relax_section. */
1171 internal_relocs
= (_bfd_elf_link_read_relocs
1172 (abfd
, o
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
1173 if (internal_relocs
== NULL
)
1177 irelscanend
= internal_relocs
+ o
->reloc_count
;
1178 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1180 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1181 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1183 bfd_vma start
, stop
;
1184 bfd_signed_vma voff
;
1186 if (ocontents
== NULL
)
1188 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1189 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1192 /* We always cache the section contents.
1193 Perhaps, if info->keep_memory is FALSE, we
1194 should free them, if we are permitted to,
1195 when we leave sh_coff_relax_section. */
1196 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1198 if (ocontents
!= NULL
)
1203 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1207 stop
= irelscan
->r_offset
;
1209 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1211 /* STOP is in a different section, so it won't change. */
1212 if (start
> addr
&& start
< toaddr
)
1213 irelscan
->r_addend
+= count
;
1215 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1216 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1220 && (stop
<= addr
|| stop
>= toaddr
))
1221 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
+ count
,
1222 ocontents
+ irelscan
->r_offset
);
1223 else if (stop
> addr
1225 && (start
<= addr
|| start
>= toaddr
))
1226 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
- count
,
1227 ocontents
+ irelscan
->r_offset
);
1230 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1233 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1237 isym
= isymbuf
+ ELF32_R_SYM (irelscan
->r_info
);
1238 if (isym
->st_shndx
== sec_shndx
1239 && (isym
->st_value
<= addr
1240 || isym
->st_value
>= toaddr
))
1244 if (ocontents
== NULL
)
1246 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1247 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1250 /* We always cache the section contents.
1251 Perhaps, if info->keep_memory is FALSE, we
1252 should free them, if we are permitted to,
1253 when we leave sh_coff_relax_section. */
1254 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1256 if (ocontents
!= NULL
)
1261 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1265 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1266 val
+= isym
->st_value
;
1267 if (val
> addr
&& val
< toaddr
)
1268 bfd_put_32 (abfd
, val
- count
,
1269 ocontents
+ irelscan
->r_offset
);
1274 /* Adjust the local symbols defined in this section. */
1275 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1276 for (isym
= isymbuf
; isym
< isymend
; isym
++)
1278 if (isym
->st_shndx
== sec_shndx
1279 && isym
->st_value
> addr
1280 && isym
->st_value
< toaddr
)
1281 isym
->st_value
-= count
;
1284 /* Now adjust the global symbols defined in this section. */
1285 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1286 - symtab_hdr
->sh_info
);
1287 sym_hashes
= elf_sym_hashes (abfd
);
1288 end_hashes
= sym_hashes
+ symcount
;
1289 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1291 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1292 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1293 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1294 && sym_hash
->root
.u
.def
.section
== sec
1295 && sym_hash
->root
.u
.def
.value
> addr
1296 && sym_hash
->root
.u
.def
.value
< toaddr
)
1298 sym_hash
->root
.u
.def
.value
-= count
;
1302 /* See if we can move the ALIGN reloc forward. We have adjusted
1303 r_offset for it already. */
1304 if (irelalign
!= NULL
)
1306 bfd_vma alignto
, alignaddr
;
1308 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1309 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1310 1 << irelalign
->r_addend
);
1311 if (alignto
!= alignaddr
)
1313 /* Tail recursion. */
1314 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1315 (int) (alignto
- alignaddr
));
1322 /* Look for loads and stores which we can align to four byte
1323 boundaries. This is like sh_align_loads in coff-sh.c. */
1326 sh_elf_align_loads (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1327 Elf_Internal_Rela
*internal_relocs
,
1328 bfd_byte
*contents ATTRIBUTE_UNUSED
,
1329 bfd_boolean
*pswapped
)
1331 Elf_Internal_Rela
*irel
, *irelend
;
1332 bfd_vma
*labels
= NULL
;
1333 bfd_vma
*label
, *label_end
;
1338 irelend
= internal_relocs
+ sec
->reloc_count
;
1340 /* Get all the addresses with labels on them. */
1341 amt
= sec
->reloc_count
;
1342 amt
*= sizeof (bfd_vma
);
1343 labels
= (bfd_vma
*) bfd_malloc (amt
);
1347 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1349 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1351 *label_end
= irel
->r_offset
;
1356 /* Note that the assembler currently always outputs relocs in
1357 address order. If that ever changes, this code will need to sort
1358 the label values and the relocs. */
1362 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1364 bfd_vma start
, stop
;
1366 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1369 start
= irel
->r_offset
;
1371 for (irel
++; irel
< irelend
; irel
++)
1372 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1375 stop
= irel
->r_offset
;
1379 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1380 internal_relocs
, &label
,
1381 label_end
, start
, stop
, pswapped
))
1396 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1399 sh_elf_swap_insns (bfd
*abfd
, asection
*sec
, void *relocs
,
1400 bfd_byte
*contents
, bfd_vma addr
)
1402 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1403 unsigned short i1
, i2
;
1404 Elf_Internal_Rela
*irel
, *irelend
;
1406 /* Swap the instructions themselves. */
1407 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1408 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1409 bfd_put_16 (abfd
, (bfd_vma
) i2
, contents
+ addr
);
1410 bfd_put_16 (abfd
, (bfd_vma
) i1
, contents
+ addr
+ 2);
1412 /* Adjust all reloc addresses. */
1413 irelend
= internal_relocs
+ sec
->reloc_count
;
1414 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1416 enum elf_sh_reloc_type type
;
1419 /* There are a few special types of relocs that we don't want to
1420 adjust. These relocs do not apply to the instruction itself,
1421 but are only associated with the address. */
1422 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1423 if (type
== R_SH_ALIGN
1424 || type
== R_SH_CODE
1425 || type
== R_SH_DATA
1426 || type
== R_SH_LABEL
)
1429 /* If an R_SH_USES reloc points to one of the addresses being
1430 swapped, we must adjust it. It would be incorrect to do this
1431 for a jump, though, since we want to execute both
1432 instructions after the jump. (We have avoided swapping
1433 around a label, so the jump will not wind up executing an
1434 instruction it shouldn't). */
1435 if (type
== R_SH_USES
)
1439 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
1441 irel
->r_offset
+= 2;
1442 else if (off
== addr
+ 2)
1443 irel
->r_offset
-= 2;
1446 if (irel
->r_offset
== addr
)
1448 irel
->r_offset
+= 2;
1451 else if (irel
->r_offset
== addr
+ 2)
1453 irel
->r_offset
-= 2;
1462 unsigned short insn
, oinsn
;
1463 bfd_boolean overflow
;
1465 loc
= contents
+ irel
->r_offset
;
1474 insn
= bfd_get_16 (abfd
, loc
);
1477 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1479 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1483 insn
= bfd_get_16 (abfd
, loc
);
1486 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1488 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1492 /* This reloc ignores the least significant 3 bits of
1493 the program counter before adding in the offset.
1494 This means that if ADDR is at an even address, the
1495 swap will not affect the offset. If ADDR is an at an
1496 odd address, then the instruction will be crossing a
1497 four byte boundary, and must be adjusted. */
1498 if ((addr
& 3) != 0)
1500 insn
= bfd_get_16 (abfd
, loc
);
1503 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1505 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1513 ((*_bfd_error_handler
)
1514 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1515 abfd
, (unsigned long) irel
->r_offset
));
1516 bfd_set_error (bfd_error_bad_value
);
1524 #endif /* defined SH64_ELF */
1526 /* Describes one of the various PLT styles. */
1528 struct elf_sh_plt_info
1530 /* The template for the first PLT entry, or NULL if there is no special
1532 const bfd_byte
*plt0_entry
;
1534 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1535 bfd_vma plt0_entry_size
;
1537 /* Index I is the offset into PLT0_ENTRY of a pointer to
1538 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1539 if there is no such pointer. */
1540 bfd_vma plt0_got_fields
[3];
1542 /* The template for a symbol's PLT entry. */
1543 const bfd_byte
*symbol_entry
;
1545 /* The size of SYMBOL_ENTRY in bytes. */
1546 bfd_vma symbol_entry_size
;
1548 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1549 on all targets. The comments by each member indicate the value
1550 that the field must hold. */
1552 bfd_vma got_entry
; /* the address of the symbol's .got.plt entry */
1553 bfd_vma plt
; /* .plt (or a branch to .plt on VxWorks) */
1554 bfd_vma reloc_offset
; /* the offset of the symbol's JMP_SLOT reloc */
1557 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1558 bfd_vma symbol_resolve_offset
;
1561 #ifdef INCLUDE_SHMEDIA
1563 /* The size in bytes of an entry in the procedure linkage table. */
1565 #define ELF_PLT_ENTRY_SIZE 64
1567 /* First entry in an absolute procedure linkage table look like this. */
1569 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1571 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1572 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1573 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1574 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1575 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1576 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1577 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1578 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1579 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1580 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1581 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1582 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1583 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1584 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1585 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1586 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1589 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1591 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1592 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1593 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1594 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1595 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1596 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1597 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1598 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1599 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1600 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1601 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1602 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1603 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1604 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1605 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1606 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1609 /* Sebsequent entries in an absolute procedure linkage table look like
1612 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1614 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1615 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1616 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1617 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1618 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1619 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1620 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1621 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1622 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1623 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1624 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1625 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1626 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1627 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1628 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1629 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1632 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1634 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1635 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1636 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1637 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1638 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1639 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1640 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1641 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1642 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1643 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1644 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1645 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1646 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1647 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1648 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1649 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1652 /* Entries in a PIC procedure linkage table look like this. */
1654 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1656 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1657 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1658 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1659 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1660 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1661 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1662 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1663 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1664 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1665 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1666 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1667 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1668 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1669 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1670 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1671 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1674 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1676 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1677 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1678 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1679 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1680 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1681 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1682 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1683 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1684 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1685 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1686 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1687 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1688 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1689 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1690 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1691 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1694 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1697 /* Big-endian non-PIC. */
1698 elf_sh_plt0_entry_be
,
1700 { 0, MINUS_ONE
, MINUS_ONE
},
1701 elf_sh_plt_entry_be
,
1704 33 /* includes ISA encoding */
1707 /* Little-endian non-PIC. */
1708 elf_sh_plt0_entry_le
,
1710 { 0, MINUS_ONE
, MINUS_ONE
},
1711 elf_sh_plt_entry_le
,
1714 33 /* includes ISA encoding */
1719 /* Big-endian PIC. */
1720 elf_sh_plt0_entry_be
,
1722 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1723 elf_sh_pic_plt_entry_be
,
1725 { 0, MINUS_ONE
, 52 },
1726 33 /* includes ISA encoding */
1729 /* Little-endian PIC. */
1730 elf_sh_plt0_entry_le
,
1732 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1733 elf_sh_pic_plt_entry_le
,
1735 { 0, MINUS_ONE
, 52 },
1736 33 /* includes ISA encoding */
1741 /* Return offset of the linker in PLT0 entry. */
1742 #define elf_sh_plt0_gotplt_offset(info) 0
1744 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1745 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1748 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1751 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p
,
1752 unsigned long value
, bfd_byte
*addr
)
1755 bfd_put_32 (output_bfd
,
1756 bfd_get_32 (output_bfd
, addr
)
1757 | ((value
>> 6) & 0x3fffc00),
1759 bfd_put_32 (output_bfd
,
1760 bfd_get_32 (output_bfd
, addr
+ 4)
1761 | ((value
<< 10) & 0x3fffc00),
1765 /* Return the type of PLT associated with ABFD. PIC_P is true if
1766 the object is position-independent. */
1768 static const struct elf_sh_plt_info
*
1769 get_plt_info (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean pic_p
)
1771 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
1774 /* The size in bytes of an entry in the procedure linkage table. */
1776 #define ELF_PLT_ENTRY_SIZE 28
1778 /* First entry in an absolute procedure linkage table look like this. */
1780 /* Note - this code has been "optimised" not to use r2. r2 is used by
1781 GCC to return the address of large structures, so it should not be
1782 corrupted here. This does mean however, that this PLT does not conform
1783 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1784 and r2 contains the GOT id. This version stores the GOT id in r0 and
1785 ignores the type. Loaders can easily detect this difference however,
1786 since the type will always be 0 or 8, and the GOT ids will always be
1787 greater than or equal to 12. */
1788 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1790 0xd0, 0x05, /* mov.l 2f,r0 */
1791 0x60, 0x02, /* mov.l @r0,r0 */
1792 0x2f, 0x06, /* mov.l r0,@-r15 */
1793 0xd0, 0x03, /* mov.l 1f,r0 */
1794 0x60, 0x02, /* mov.l @r0,r0 */
1795 0x40, 0x2b, /* jmp @r0 */
1796 0x60, 0xf6, /* mov.l @r15+,r0 */
1797 0x00, 0x09, /* nop */
1798 0x00, 0x09, /* nop */
1799 0x00, 0x09, /* nop */
1800 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1801 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1804 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1806 0x05, 0xd0, /* mov.l 2f,r0 */
1807 0x02, 0x60, /* mov.l @r0,r0 */
1808 0x06, 0x2f, /* mov.l r0,@-r15 */
1809 0x03, 0xd0, /* mov.l 1f,r0 */
1810 0x02, 0x60, /* mov.l @r0,r0 */
1811 0x2b, 0x40, /* jmp @r0 */
1812 0xf6, 0x60, /* mov.l @r15+,r0 */
1813 0x09, 0x00, /* nop */
1814 0x09, 0x00, /* nop */
1815 0x09, 0x00, /* nop */
1816 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1817 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1820 /* Sebsequent entries in an absolute procedure linkage table look like
1823 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1825 0xd0, 0x04, /* mov.l 1f,r0 */
1826 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1827 0xd1, 0x02, /* mov.l 0f,r1 */
1828 0x40, 0x2b, /* jmp @r0 */
1829 0x60, 0x13, /* mov r1,r0 */
1830 0xd1, 0x03, /* mov.l 2f,r1 */
1831 0x40, 0x2b, /* jmp @r0 */
1832 0x00, 0x09, /* nop */
1833 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1834 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1835 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1838 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1840 0x04, 0xd0, /* mov.l 1f,r0 */
1841 0x02, 0x60, /* mov.l @r0,r0 */
1842 0x02, 0xd1, /* mov.l 0f,r1 */
1843 0x2b, 0x40, /* jmp @r0 */
1844 0x13, 0x60, /* mov r1,r0 */
1845 0x03, 0xd1, /* mov.l 2f,r1 */
1846 0x2b, 0x40, /* jmp @r0 */
1847 0x09, 0x00, /* nop */
1848 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1849 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1850 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1853 /* Entries in a PIC procedure linkage table look like this. */
1855 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1857 0xd0, 0x04, /* mov.l 1f,r0 */
1858 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1859 0x40, 0x2b, /* jmp @r0 */
1860 0x00, 0x09, /* nop */
1861 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1862 0xd1, 0x03, /* mov.l 2f,r1 */
1863 0x40, 0x2b, /* jmp @r0 */
1864 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1865 0x00, 0x09, /* nop */
1866 0x00, 0x09, /* nop */
1867 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1868 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1871 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1873 0x04, 0xd0, /* mov.l 1f,r0 */
1874 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1875 0x2b, 0x40, /* jmp @r0 */
1876 0x09, 0x00, /* nop */
1877 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1878 0x03, 0xd1, /* mov.l 2f,r1 */
1879 0x2b, 0x40, /* jmp @r0 */
1880 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1881 0x09, 0x00, /* nop */
1882 0x09, 0x00, /* nop */
1883 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1884 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1887 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1890 /* Big-endian non-PIC. */
1891 elf_sh_plt0_entry_be
,
1893 { MINUS_ONE
, 24, 20 },
1894 elf_sh_plt_entry_be
,
1900 /* Little-endian non-PIC. */
1901 elf_sh_plt0_entry_le
,
1903 { MINUS_ONE
, 24, 20 },
1904 elf_sh_plt_entry_le
,
1912 /* Big-endian PIC. */
1913 elf_sh_plt0_entry_be
,
1915 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1916 elf_sh_pic_plt_entry_be
,
1918 { 20, MINUS_ONE
, 24 },
1922 /* Little-endian PIC. */
1923 elf_sh_plt0_entry_le
,
1925 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1926 elf_sh_pic_plt_entry_le
,
1928 { 20, MINUS_ONE
, 24 },
1934 #define VXWORKS_PLT_HEADER_SIZE 12
1935 #define VXWORKS_PLT_ENTRY_SIZE 24
1937 static const bfd_byte vxworks_sh_plt0_entry_be
[VXWORKS_PLT_HEADER_SIZE
] =
1939 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1940 0x61, 0x12, /* mov.l @r1,r1 */
1941 0x41, 0x2b, /* jmp @r1 */
1942 0x00, 0x09, /* nop */
1943 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1946 static const bfd_byte vxworks_sh_plt0_entry_le
[VXWORKS_PLT_HEADER_SIZE
] =
1948 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1949 0x12, 0x61, /* mov.l @r1,r1 */
1950 0x2b, 0x41, /* jmp @r1 */
1951 0x09, 0x00, /* nop */
1952 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1955 static const bfd_byte vxworks_sh_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1957 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1958 0x60, 0x02, /* mov.l @r0,r0 */
1959 0x40, 0x2b, /* jmp @r0 */
1960 0x00, 0x09, /* nop */
1961 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1962 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1963 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1964 0x00, 0x09, /* nop */
1965 0x00, 0x09, /* nop */
1966 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1969 static const bfd_byte vxworks_sh_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1971 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1972 0x02, 0x60, /* mov.l @r0,r0 */
1973 0x2b, 0x40, /* jmp @r0 */
1974 0x09, 0x00, /* nop */
1975 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1976 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1977 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1978 0x09, 0x00, /* nop */
1979 0x09, 0x00, /* nop */
1980 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1983 static const bfd_byte vxworks_sh_pic_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1985 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1986 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1987 0x40, 0x2b, /* jmp @r0 */
1988 0x00, 0x09, /* nop */
1989 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1990 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1991 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1992 0x41, 0x2b, /* jmp @r1 */
1993 0x00, 0x09, /* nop */
1994 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1997 static const bfd_byte vxworks_sh_pic_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1999 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2000 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2001 0x2b, 0x40, /* jmp @r0 */
2002 0x09, 0x00, /* nop */
2003 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2004 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2005 0xc2, 0x51, /* mov.l @(8,r12),r1 */
2006 0x2b, 0x41, /* jmp @r1 */
2007 0x09, 0x00, /* nop */
2008 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2011 static const struct elf_sh_plt_info vxworks_sh_plts
[2][2] = {
2014 /* Big-endian non-PIC. */
2015 vxworks_sh_plt0_entry_be
,
2016 VXWORKS_PLT_HEADER_SIZE
,
2017 { MINUS_ONE
, MINUS_ONE
, 8 },
2018 vxworks_sh_plt_entry_be
,
2019 VXWORKS_PLT_ENTRY_SIZE
,
2024 /* Little-endian non-PIC. */
2025 vxworks_sh_plt0_entry_le
,
2026 VXWORKS_PLT_HEADER_SIZE
,
2027 { MINUS_ONE
, MINUS_ONE
, 8 },
2028 vxworks_sh_plt_entry_le
,
2029 VXWORKS_PLT_ENTRY_SIZE
,
2036 /* Big-endian PIC. */
2039 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2040 vxworks_sh_pic_plt_entry_be
,
2041 VXWORKS_PLT_ENTRY_SIZE
,
2042 { 8, MINUS_ONE
, 20 },
2046 /* Little-endian PIC. */
2049 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2050 vxworks_sh_pic_plt_entry_le
,
2051 VXWORKS_PLT_ENTRY_SIZE
,
2052 { 8, MINUS_ONE
, 20 },
2058 /* Return the type of PLT associated with ABFD. PIC_P is true if
2059 the object is position-independent. */
2061 static const struct elf_sh_plt_info
*
2062 get_plt_info (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean pic_p
)
2064 if (vxworks_object_p (abfd
))
2065 return &vxworks_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2066 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2069 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2070 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2074 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p ATTRIBUTE_UNUSED
,
2075 unsigned long value
, bfd_byte
*addr
)
2077 bfd_put_32 (output_bfd
, value
, addr
);
2081 /* Return the index of the PLT entry at byte offset OFFSET. */
2084 get_plt_index (const struct elf_sh_plt_info
*info
, bfd_vma offset
)
2086 return (offset
- info
->plt0_entry_size
) / info
->symbol_entry_size
;
2089 /* Do the inverse operation. */
2092 get_plt_offset (const struct elf_sh_plt_info
*info
, bfd_vma index
)
2094 return info
->plt0_entry_size
+ (index
* info
->symbol_entry_size
);
2097 /* The sh linker needs to keep track of the number of relocs that it
2098 decides to copy as dynamic relocs in check_relocs for each symbol.
2099 This is so that it can later discard them if they are found to be
2100 unnecessary. We store the information in a field extending the
2101 regular ELF linker hash table. */
2103 struct elf_sh_dyn_relocs
2105 struct elf_sh_dyn_relocs
*next
;
2107 /* The input section of the reloc. */
2110 /* Total number of relocs copied for the input section. */
2111 bfd_size_type count
;
2113 /* Number of pc-relative relocs copied for the input section. */
2114 bfd_size_type pc_count
;
2117 /* sh ELF linker hash entry. */
2119 struct elf_sh_link_hash_entry
2121 struct elf_link_hash_entry root
;
2123 #ifdef INCLUDE_SHMEDIA
2126 bfd_signed_vma refcount
;
2131 /* Track dynamic relocs copied for this symbol. */
2132 struct elf_sh_dyn_relocs
*dyn_relocs
;
2134 bfd_signed_vma gotplt_refcount
;
2137 GOT_UNKNOWN
= 0, GOT_NORMAL
, GOT_TLS_GD
, GOT_TLS_IE
2141 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2143 struct sh_elf_obj_tdata
2145 struct elf_obj_tdata root
;
2147 /* tls_type for each local got entry. */
2148 char *local_got_tls_type
;
2151 #define sh_elf_tdata(abfd) \
2152 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2154 #define sh_elf_local_got_tls_type(abfd) \
2155 (sh_elf_tdata (abfd)->local_got_tls_type)
2157 #define is_sh_elf(bfd) \
2158 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2159 && elf_tdata (bfd) != NULL \
2160 && elf_object_id (bfd) == SH_ELF_TDATA)
2162 /* Override the generic function because we need to store sh_elf_obj_tdata
2163 as the specific tdata. */
2166 sh_elf_mkobject (bfd
*abfd
)
2168 return bfd_elf_allocate_object (abfd
, sizeof (struct sh_elf_obj_tdata
),
2172 /* sh ELF linker hash table. */
2174 struct elf_sh_link_hash_table
2176 struct elf_link_hash_table root
;
2178 /* Short-cuts to get to dynamic linker sections. */
2187 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2190 /* Small local sym to section mapping cache. */
2191 struct sym_sec_cache sym_sec
;
2193 /* A counter or offset to track a TLS got entry. */
2196 bfd_signed_vma refcount
;
2200 /* The type of PLT to use. */
2201 const struct elf_sh_plt_info
*plt_info
;
2203 /* True if the target system is VxWorks. */
2204 bfd_boolean vxworks_p
;
2207 /* Traverse an sh ELF linker hash table. */
2209 #define sh_elf_link_hash_traverse(table, func, info) \
2210 (elf_link_hash_traverse \
2212 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2215 /* Get the sh ELF linker hash table from a link_info structure. */
2217 #define sh_elf_hash_table(p) \
2218 ((struct elf_sh_link_hash_table *) ((p)->hash))
2220 /* Create an entry in an sh ELF linker hash table. */
2222 static struct bfd_hash_entry
*
2223 sh_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
2224 struct bfd_hash_table
*table
,
2227 struct elf_sh_link_hash_entry
*ret
=
2228 (struct elf_sh_link_hash_entry
*) entry
;
2230 /* Allocate the structure if it has not already been allocated by a
2232 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2233 ret
= ((struct elf_sh_link_hash_entry
*)
2234 bfd_hash_allocate (table
,
2235 sizeof (struct elf_sh_link_hash_entry
)));
2236 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2237 return (struct bfd_hash_entry
*) ret
;
2239 /* Call the allocation method of the superclass. */
2240 ret
= ((struct elf_sh_link_hash_entry
*)
2241 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2243 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2245 ret
->dyn_relocs
= NULL
;
2246 ret
->gotplt_refcount
= 0;
2247 #ifdef INCLUDE_SHMEDIA
2248 ret
->datalabel_got
.refcount
= ret
->root
.got
.refcount
;
2250 ret
->tls_type
= GOT_UNKNOWN
;
2253 return (struct bfd_hash_entry
*) ret
;
2256 /* Create an sh ELF linker hash table. */
2258 static struct bfd_link_hash_table
*
2259 sh_elf_link_hash_table_create (bfd
*abfd
)
2261 struct elf_sh_link_hash_table
*ret
;
2262 bfd_size_type amt
= sizeof (struct elf_sh_link_hash_table
);
2264 ret
= (struct elf_sh_link_hash_table
*) bfd_malloc (amt
);
2265 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2268 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2269 sh_elf_link_hash_newfunc
,
2270 sizeof (struct elf_sh_link_hash_entry
)))
2277 ret
->sgotplt
= NULL
;
2278 ret
->srelgot
= NULL
;
2280 ret
->srelplt
= NULL
;
2281 ret
->sdynbss
= NULL
;
2282 ret
->srelbss
= NULL
;
2283 ret
->srelplt2
= NULL
;
2284 ret
->sym_sec
.abfd
= NULL
;
2285 ret
->tls_ldm_got
.refcount
= 0;
2286 ret
->plt_info
= NULL
;
2287 ret
->vxworks_p
= vxworks_object_p (abfd
);
2289 return &ret
->root
.root
;
2292 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2293 shortcuts to them in our hash table. */
2296 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
2298 struct elf_sh_link_hash_table
*htab
;
2300 if (! _bfd_elf_create_got_section (dynobj
, info
))
2303 htab
= sh_elf_hash_table (info
);
2304 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
2305 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
2306 if (! htab
->sgot
|| ! htab
->sgotplt
)
2309 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rela.got",
2310 (SEC_ALLOC
| SEC_LOAD
2313 | SEC_LINKER_CREATED
2315 if (htab
->srelgot
== NULL
2316 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 2))
2321 /* Create dynamic sections when linking against a dynamic object. */
2324 sh_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2326 struct elf_sh_link_hash_table
*htab
;
2327 flagword flags
, pltflags
;
2328 register asection
*s
;
2329 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2332 switch (bed
->s
->arch_size
)
2343 bfd_set_error (bfd_error_bad_value
);
2347 htab
= sh_elf_hash_table (info
);
2348 if (htab
->root
.dynamic_sections_created
)
2351 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2352 .rel[a].bss sections. */
2354 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2355 | SEC_LINKER_CREATED
);
2358 pltflags
|= SEC_CODE
;
2359 if (bed
->plt_not_loaded
)
2360 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2361 if (bed
->plt_readonly
)
2362 pltflags
|= SEC_READONLY
;
2364 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
2367 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
2370 if (bed
->want_plt_sym
)
2372 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2374 struct elf_link_hash_entry
*h
;
2375 struct bfd_link_hash_entry
*bh
= NULL
;
2377 if (! (_bfd_generic_link_add_one_symbol
2378 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2379 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
2380 get_elf_backend_data (abfd
)->collect
, &bh
)))
2383 h
= (struct elf_link_hash_entry
*) bh
;
2385 h
->type
= STT_OBJECT
;
2386 htab
->root
.hplt
= h
;
2389 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2393 s
= bfd_make_section_with_flags (abfd
,
2394 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt",
2395 flags
| SEC_READONLY
);
2398 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2401 if (htab
->sgot
== NULL
2402 && !create_got_section (abfd
, info
))
2406 const char *secname
;
2411 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2413 secflags
= bfd_get_section_flags (abfd
, sec
);
2414 if ((secflags
& (SEC_DATA
| SEC_LINKER_CREATED
))
2415 || ((secflags
& SEC_HAS_CONTENTS
) != SEC_HAS_CONTENTS
))
2417 secname
= bfd_get_section_name (abfd
, sec
);
2418 relname
= (char *) bfd_malloc ((bfd_size_type
) strlen (secname
) + 6);
2419 strcpy (relname
, ".rela");
2420 strcat (relname
, secname
);
2421 if (bfd_get_section_by_name (abfd
, secname
))
2423 s
= bfd_make_section_with_flags (abfd
, relname
,
2424 flags
| SEC_READONLY
);
2426 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2431 if (bed
->want_dynbss
)
2433 /* The .dynbss section is a place to put symbols which are defined
2434 by dynamic objects, are referenced by regular objects, and are
2435 not functions. We must allocate space for them in the process
2436 image and use a R_*_COPY reloc to tell the dynamic linker to
2437 initialize them at run time. The linker script puts the .dynbss
2438 section into the .bss section of the final image. */
2439 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
2440 SEC_ALLOC
| SEC_LINKER_CREATED
);
2445 /* The .rel[a].bss section holds copy relocs. This section is not
2446 normally needed. We need to create it here, though, so that the
2447 linker will map it to an output section. We can't just create it
2448 only if we need it, because we will not know whether we need it
2449 until we have seen all the input files, and the first time the
2450 main linker code calls BFD after examining all the input files
2451 (size_dynamic_sections) the input sections have already been
2452 mapped to the output sections. If the section turns out not to
2453 be needed, we can discard it later. We will never need this
2454 section when generating a shared object, since they do not use
2458 s
= bfd_make_section_with_flags (abfd
,
2459 (bed
->default_use_rela_p
2460 ? ".rela.bss" : ".rel.bss"),
2461 flags
| SEC_READONLY
);
2464 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2469 if (htab
->vxworks_p
)
2471 if (!elf_vxworks_create_dynamic_sections (abfd
, info
, &htab
->srelplt2
))
2478 /* Adjust a symbol defined by a dynamic object and referenced by a
2479 regular object. The current definition is in some section of the
2480 dynamic object, but we're not including those sections. We have to
2481 change the definition to something the rest of the link can
2485 sh_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2486 struct elf_link_hash_entry
*h
)
2488 struct elf_sh_link_hash_table
*htab
;
2489 struct elf_sh_link_hash_entry
*eh
;
2490 struct elf_sh_dyn_relocs
*p
;
2493 htab
= sh_elf_hash_table (info
);
2495 /* Make sure we know what is going on here. */
2496 BFD_ASSERT (htab
->root
.dynobj
!= NULL
2498 || h
->u
.weakdef
!= NULL
2501 && !h
->def_regular
)));
2503 /* If this is a function, put it in the procedure linkage table. We
2504 will fill in the contents of the procedure linkage table later,
2505 when we know the address of the .got section. */
2506 if (h
->type
== STT_FUNC
2509 if (h
->plt
.refcount
<= 0
2510 || SYMBOL_CALLS_LOCAL (info
, h
)
2511 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2512 && h
->root
.type
== bfd_link_hash_undefweak
))
2514 /* This case can occur if we saw a PLT reloc in an input
2515 file, but the symbol was never referred to by a dynamic
2516 object. In such a case, we don't actually need to build
2517 a procedure linkage table, and we can just do a REL32
2519 h
->plt
.offset
= (bfd_vma
) -1;
2526 h
->plt
.offset
= (bfd_vma
) -1;
2528 /* If this is a weak symbol, and there is a real definition, the
2529 processor independent code will have arranged for us to see the
2530 real definition first, and we can just use the same value. */
2531 if (h
->u
.weakdef
!= NULL
)
2533 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
2534 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
2535 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
2536 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
2537 if (info
->nocopyreloc
)
2538 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
2542 /* This is a reference to a symbol defined by a dynamic object which
2543 is not a function. */
2545 /* If we are creating a shared library, we must presume that the
2546 only references to the symbol are via the global offset table.
2547 For such cases we need not do anything here; the relocations will
2548 be handled correctly by relocate_section. */
2552 /* If there are no references to this symbol that do not use the
2553 GOT, we don't need to generate a copy reloc. */
2554 if (!h
->non_got_ref
)
2557 /* If -z nocopyreloc was given, we won't generate them either. */
2558 if (info
->nocopyreloc
)
2564 eh
= (struct elf_sh_link_hash_entry
*) h
;
2565 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2567 s
= p
->sec
->output_section
;
2568 if (s
!= NULL
&& (s
->flags
& (SEC_READONLY
| SEC_HAS_CONTENTS
)) != 0)
2572 /* If we didn't find any dynamic relocs in sections which needs the
2573 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2583 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
2584 h
->root
.root
.string
);
2588 /* We must allocate the symbol in our .dynbss section, which will
2589 become part of the .bss section of the executable. There will be
2590 an entry for this symbol in the .dynsym section. The dynamic
2591 object will contain position independent code, so all references
2592 from the dynamic object to this symbol will go through the global
2593 offset table. The dynamic linker will use the .dynsym entry to
2594 determine the address it must put in the global offset table, so
2595 both the dynamic object and the regular object will refer to the
2596 same memory location for the variable. */
2599 BFD_ASSERT (s
!= NULL
);
2601 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2602 copy the initial value out of the dynamic object and into the
2603 runtime process image. We need to remember the offset into the
2604 .rela.bss section we are going to use. */
2605 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
2609 srel
= htab
->srelbss
;
2610 BFD_ASSERT (srel
!= NULL
);
2611 srel
->size
+= sizeof (Elf32_External_Rela
);
2615 return _bfd_elf_adjust_dynamic_copy (h
, s
);
2618 /* Allocate space in .plt, .got and associated reloc sections for
2622 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2624 struct bfd_link_info
*info
;
2625 struct elf_sh_link_hash_table
*htab
;
2626 struct elf_sh_link_hash_entry
*eh
;
2627 struct elf_sh_dyn_relocs
*p
;
2629 if (h
->root
.type
== bfd_link_hash_indirect
)
2632 if (h
->root
.type
== bfd_link_hash_warning
)
2633 /* When warning symbols are created, they **replace** the "real"
2634 entry in the hash table, thus we never get to see the real
2635 symbol in a hash traversal. So look at it now. */
2636 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2638 info
= (struct bfd_link_info
*) inf
;
2639 htab
= sh_elf_hash_table (info
);
2641 eh
= (struct elf_sh_link_hash_entry
*) h
;
2642 if ((h
->got
.refcount
> 0
2644 && eh
->gotplt_refcount
> 0)
2646 /* The symbol has been forced local, or we have some direct got refs,
2647 so treat all the gotplt refs as got refs. */
2648 h
->got
.refcount
+= eh
->gotplt_refcount
;
2649 if (h
->plt
.refcount
>= eh
->gotplt_refcount
)
2650 h
->plt
.refcount
-= eh
->gotplt_refcount
;
2653 if (htab
->root
.dynamic_sections_created
2654 && h
->plt
.refcount
> 0
2655 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2656 || h
->root
.type
!= bfd_link_hash_undefweak
))
2658 /* Make sure this symbol is output as a dynamic symbol.
2659 Undefined weak syms won't yet be marked as dynamic. */
2660 if (h
->dynindx
== -1
2661 && !h
->forced_local
)
2663 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2668 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2670 asection
*s
= htab
->splt
;
2672 /* If this is the first .plt entry, make room for the special
2675 s
->size
+= htab
->plt_info
->plt0_entry_size
;
2677 h
->plt
.offset
= s
->size
;
2679 /* If this symbol is not defined in a regular file, and we are
2680 not generating a shared library, then set the symbol to this
2681 location in the .plt. This is required to make function
2682 pointers compare as equal between the normal executable and
2683 the shared library. */
2687 h
->root
.u
.def
.section
= s
;
2688 h
->root
.u
.def
.value
= h
->plt
.offset
;
2691 /* Make room for this entry. */
2692 s
->size
+= htab
->plt_info
->symbol_entry_size
;
2694 /* We also need to make an entry in the .got.plt section, which
2695 will be placed in the .got section by the linker script. */
2696 htab
->sgotplt
->size
+= 4;
2698 /* We also need to make an entry in the .rel.plt section. */
2699 htab
->srelplt
->size
+= sizeof (Elf32_External_Rela
);
2701 if (htab
->vxworks_p
&& !info
->shared
)
2703 /* VxWorks executables have a second set of relocations
2704 for each PLT entry. They go in a separate relocation
2705 section, which is processed by the kernel loader. */
2707 /* There is a relocation for the initial PLT entry:
2708 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2709 if (h
->plt
.offset
== htab
->plt_info
->plt0_entry_size
)
2710 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
);
2712 /* There are two extra relocations for each subsequent
2713 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2714 and an R_SH_DIR32 relocation for the PLT entry. */
2715 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
) * 2;
2720 h
->plt
.offset
= (bfd_vma
) -1;
2726 h
->plt
.offset
= (bfd_vma
) -1;
2730 if (h
->got
.refcount
> 0)
2734 int tls_type
= sh_elf_hash_entry (h
)->tls_type
;
2736 /* Make sure this symbol is output as a dynamic symbol.
2737 Undefined weak syms won't yet be marked as dynamic. */
2738 if (h
->dynindx
== -1
2739 && !h
->forced_local
)
2741 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2746 h
->got
.offset
= s
->size
;
2748 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2749 if (tls_type
== GOT_TLS_GD
)
2751 dyn
= htab
->root
.dynamic_sections_created
;
2752 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2753 R_SH_TLS_GD needs one if local symbol and two if global. */
2754 if ((tls_type
== GOT_TLS_GD
&& h
->dynindx
== -1)
2755 || (tls_type
== GOT_TLS_IE
&& dyn
))
2756 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2757 else if (tls_type
== GOT_TLS_GD
)
2758 htab
->srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
2759 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2760 || h
->root
.type
!= bfd_link_hash_undefweak
)
2762 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
2763 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2766 h
->got
.offset
= (bfd_vma
) -1;
2768 #ifdef INCLUDE_SHMEDIA
2769 if (eh
->datalabel_got
.refcount
> 0)
2774 /* Make sure this symbol is output as a dynamic symbol.
2775 Undefined weak syms won't yet be marked as dynamic. */
2776 if (h
->dynindx
== -1
2777 && !h
->forced_local
)
2779 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2784 eh
->datalabel_got
.offset
= s
->size
;
2786 dyn
= htab
->root
.dynamic_sections_created
;
2787 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
))
2788 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2791 eh
->datalabel_got
.offset
= (bfd_vma
) -1;
2794 if (eh
->dyn_relocs
== NULL
)
2797 /* In the shared -Bsymbolic case, discard space allocated for
2798 dynamic pc-relative relocs against symbols which turn out to be
2799 defined in regular objects. For the normal shared case, discard
2800 space for pc-relative relocs that have become local due to symbol
2801 visibility changes. */
2805 if (SYMBOL_CALLS_LOCAL (info
, h
))
2807 struct elf_sh_dyn_relocs
**pp
;
2809 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2811 p
->count
-= p
->pc_count
;
2820 if (htab
->vxworks_p
)
2822 struct elf_sh_dyn_relocs
**pp
;
2824 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2826 if (strcmp (p
->sec
->output_section
->name
, ".tls_vars") == 0)
2833 /* Also discard relocs on undefined weak syms with non-default
2835 if (eh
->dyn_relocs
!= NULL
2836 && h
->root
.type
== bfd_link_hash_undefweak
)
2838 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2839 eh
->dyn_relocs
= NULL
;
2841 /* Make sure undefined weak symbols are output as a dynamic
2843 else if (h
->dynindx
== -1
2844 && !h
->forced_local
)
2846 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2853 /* For the non-shared case, discard space for relocs against
2854 symbols which turn out to need copy relocs or are not
2860 || (htab
->root
.dynamic_sections_created
2861 && (h
->root
.type
== bfd_link_hash_undefweak
2862 || h
->root
.type
== bfd_link_hash_undefined
))))
2864 /* Make sure this symbol is output as a dynamic symbol.
2865 Undefined weak syms won't yet be marked as dynamic. */
2866 if (h
->dynindx
== -1
2867 && !h
->forced_local
)
2869 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2873 /* If that succeeded, we know we'll be keeping all the
2875 if (h
->dynindx
!= -1)
2879 eh
->dyn_relocs
= NULL
;
2884 /* Finally, allocate space. */
2885 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2887 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
2888 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
2894 /* Find any dynamic relocs that apply to read-only sections. */
2897 readonly_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2899 struct elf_sh_link_hash_entry
*eh
;
2900 struct elf_sh_dyn_relocs
*p
;
2902 if (h
->root
.type
== bfd_link_hash_warning
)
2903 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2905 eh
= (struct elf_sh_link_hash_entry
*) h
;
2906 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2908 asection
*s
= p
->sec
->output_section
;
2910 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2912 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
2914 if (info
->warn_shared_textrel
)
2915 (*_bfd_error_handler
)
2916 (_("warning: dynamic relocation in readonly section `%s'"),
2917 h
->root
.root
.string
);
2918 info
->flags
|= DF_TEXTREL
;
2920 /* Not an error, just cut short the traversal. */
2927 /* This function is called after all the input files have been read,
2928 and the input sections have been assigned to output sections.
2929 It's a convenient place to determine the PLT style. */
2932 sh_elf_always_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
2934 sh_elf_hash_table (info
)->plt_info
= get_plt_info (output_bfd
, info
->shared
);
2938 /* Set the sizes of the dynamic sections. */
2941 sh_elf_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2942 struct bfd_link_info
*info
)
2944 struct elf_sh_link_hash_table
*htab
;
2950 htab
= sh_elf_hash_table (info
);
2951 dynobj
= htab
->root
.dynobj
;
2952 BFD_ASSERT (dynobj
!= NULL
);
2954 if (htab
->root
.dynamic_sections_created
)
2956 /* Set the contents of the .interp section to the interpreter. */
2957 if (info
->executable
)
2959 s
= bfd_get_section_by_name (dynobj
, ".interp");
2960 BFD_ASSERT (s
!= NULL
);
2961 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
2962 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2966 /* Set up .got offsets for local syms, and space for local dynamic
2968 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2970 bfd_signed_vma
*local_got
;
2971 bfd_signed_vma
*end_local_got
;
2972 char *local_tls_type
;
2973 bfd_size_type locsymcount
;
2974 Elf_Internal_Shdr
*symtab_hdr
;
2977 if (! is_sh_elf (ibfd
))
2980 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2982 struct elf_sh_dyn_relocs
*p
;
2984 for (p
= ((struct elf_sh_dyn_relocs
*)
2985 elf_section_data (s
)->local_dynrel
);
2989 if (! bfd_is_abs_section (p
->sec
)
2990 && bfd_is_abs_section (p
->sec
->output_section
))
2992 /* Input section has been discarded, either because
2993 it is a copy of a linkonce section or due to
2994 linker script /DISCARD/, so we'll be discarding
2997 else if (htab
->vxworks_p
2998 && strcmp (p
->sec
->output_section
->name
,
3001 /* Relocations in vxworks .tls_vars sections are
3002 handled specially by the loader. */
3004 else if (p
->count
!= 0)
3006 srel
= elf_section_data (p
->sec
)->sreloc
;
3007 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
3008 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
3009 info
->flags
|= DF_TEXTREL
;
3014 local_got
= elf_local_got_refcounts (ibfd
);
3018 symtab_hdr
= &elf_symtab_hdr (ibfd
);
3019 locsymcount
= symtab_hdr
->sh_info
;
3020 #ifdef INCLUDE_SHMEDIA
3021 /* Count datalabel local GOT. */
3024 end_local_got
= local_got
+ locsymcount
;
3025 local_tls_type
= sh_elf_local_got_tls_type (ibfd
);
3027 srel
= htab
->srelgot
;
3028 for (; local_got
< end_local_got
; ++local_got
)
3032 *local_got
= s
->size
;
3034 if (*local_tls_type
== GOT_TLS_GD
)
3037 srel
->size
+= sizeof (Elf32_External_Rela
);
3040 *local_got
= (bfd_vma
) -1;
3045 if (htab
->tls_ldm_got
.refcount
> 0)
3047 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3049 htab
->tls_ldm_got
.offset
= htab
->sgot
->size
;
3050 htab
->sgot
->size
+= 8;
3051 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
3054 htab
->tls_ldm_got
.offset
= -1;
3056 /* Allocate global sym .plt and .got entries, and space for global
3057 sym dynamic relocs. */
3058 elf_link_hash_traverse (&htab
->root
, allocate_dynrelocs
, info
);
3060 /* We now have determined the sizes of the various dynamic sections.
3061 Allocate memory for them. */
3063 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
3065 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
3070 || s
== htab
->sgotplt
3071 || s
== htab
->sdynbss
)
3073 /* Strip this section if we don't need it; see the
3076 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
3078 if (s
->size
!= 0 && s
!= htab
->srelplt
&& s
!= htab
->srelplt2
)
3081 /* We use the reloc_count field as a counter if we need
3082 to copy relocs into the output file. */
3087 /* It's not one of our sections, so don't allocate space. */
3093 /* If we don't need this section, strip it from the
3094 output file. This is mostly to handle .rela.bss and
3095 .rela.plt. We must create both sections in
3096 create_dynamic_sections, because they must be created
3097 before the linker maps input sections to output
3098 sections. The linker does that before
3099 adjust_dynamic_symbol is called, and it is that
3100 function which decides whether anything needs to go
3101 into these sections. */
3103 s
->flags
|= SEC_EXCLUDE
;
3107 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3110 /* Allocate memory for the section contents. We use bfd_zalloc
3111 here in case unused entries are not reclaimed before the
3112 section's contents are written out. This should not happen,
3113 but this way if it does, we get a R_SH_NONE reloc instead
3115 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
3116 if (s
->contents
== NULL
)
3120 if (htab
->root
.dynamic_sections_created
)
3122 /* Add some entries to the .dynamic section. We fill in the
3123 values later, in sh_elf_finish_dynamic_sections, but we
3124 must add the entries now so that we get the correct size for
3125 the .dynamic section. The DT_DEBUG entry is filled in by the
3126 dynamic linker and used by the debugger. */
3127 #define add_dynamic_entry(TAG, VAL) \
3128 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3130 if (info
->executable
)
3132 if (! add_dynamic_entry (DT_DEBUG
, 0))
3136 if (htab
->splt
->size
!= 0)
3138 if (! add_dynamic_entry (DT_PLTGOT
, 0)
3139 || ! add_dynamic_entry (DT_PLTRELSZ
, 0)
3140 || ! add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3141 || ! add_dynamic_entry (DT_JMPREL
, 0))
3147 if (! add_dynamic_entry (DT_RELA
, 0)
3148 || ! add_dynamic_entry (DT_RELASZ
, 0)
3149 || ! add_dynamic_entry (DT_RELAENT
,
3150 sizeof (Elf32_External_Rela
)))
3153 /* If any dynamic relocs apply to a read-only section,
3154 then we need a DT_TEXTREL entry. */
3155 if ((info
->flags
& DF_TEXTREL
) == 0)
3156 elf_link_hash_traverse (&htab
->root
, readonly_dynrelocs
, info
);
3158 if ((info
->flags
& DF_TEXTREL
) != 0)
3160 if (! add_dynamic_entry (DT_TEXTREL
, 0))
3165 && !elf_vxworks_add_dynamic_entries (output_bfd
, info
))
3168 #undef add_dynamic_entry
3173 /* Relocate an SH ELF section. */
3176 sh_elf_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
3177 bfd
*input_bfd
, asection
*input_section
,
3178 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
3179 Elf_Internal_Sym
*local_syms
,
3180 asection
**local_sections
)
3182 struct elf_sh_link_hash_table
*htab
;
3183 Elf_Internal_Shdr
*symtab_hdr
;
3184 struct elf_link_hash_entry
**sym_hashes
;
3185 Elf_Internal_Rela
*rel
, *relend
;
3187 bfd_vma
*local_got_offsets
;
3193 bfd_boolean is_vxworks_tls
;
3195 BFD_ASSERT (is_sh_elf (input_bfd
));
3197 htab
= sh_elf_hash_table (info
);
3198 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
3199 sym_hashes
= elf_sym_hashes (input_bfd
);
3200 dynobj
= htab
->root
.dynobj
;
3201 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3204 sgotplt
= htab
->sgotplt
;
3208 /* We have to handle relocations in vxworks .tls_vars sections
3209 specially, because the dynamic loader is 'weird'. */
3210 is_vxworks_tls
= (htab
->vxworks_p
&& info
->shared
3211 && !strcmp (input_section
->output_section
->name
,
3215 relend
= relocs
+ input_section
->reloc_count
;
3216 for (; rel
< relend
; rel
++)
3219 reloc_howto_type
*howto
;
3220 unsigned long r_symndx
;
3221 Elf_Internal_Sym
*sym
;
3223 struct elf_link_hash_entry
*h
;
3225 bfd_vma addend
= (bfd_vma
) 0;
3226 bfd_reloc_status_type r
;
3227 int seen_stt_datalabel
= 0;
3231 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3233 r_type
= ELF32_R_TYPE (rel
->r_info
);
3235 /* Many of the relocs are only used for relaxing, and are
3236 handled entirely by the relaxation code. */
3237 if (r_type
>= (int) R_SH_GNU_VTINHERIT
3238 && r_type
<= (int) R_SH_LABEL
)
3240 if (r_type
== (int) R_SH_NONE
)
3244 || r_type
>= R_SH_max
3245 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
3246 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
3247 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_3
3248 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_3
)
3249 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_4
3250 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_4
)
3251 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_5
3252 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_5
)
3253 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
3254 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
))
3256 bfd_set_error (bfd_error_bad_value
);
3260 howto
= get_howto_table (output_bfd
) + r_type
;
3262 /* For relocs that aren't partial_inplace, we get the addend from
3264 if (! howto
->partial_inplace
)
3265 addend
= rel
->r_addend
;
3270 if (r_symndx
< symtab_hdr
->sh_info
)
3272 sym
= local_syms
+ r_symndx
;
3273 sec
= local_sections
[r_symndx
];
3274 relocation
= (sec
->output_section
->vma
3275 + sec
->output_offset
3277 /* A local symbol never has STO_SH5_ISA32, so we don't need
3278 datalabel processing here. Make sure this does not change
3280 if ((sym
->st_other
& STO_SH5_ISA32
) != 0)
3281 ((*info
->callbacks
->reloc_dangerous
)
3283 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3284 input_bfd
, input_section
, rel
->r_offset
));
3286 if (sec
!= NULL
&& elf_discarded_section (sec
))
3287 /* Handled below. */
3289 else if (info
->relocatable
)
3291 /* This is a relocatable link. We don't have to change
3292 anything, unless the reloc is against a section symbol,
3293 in which case we have to adjust according to where the
3294 section symbol winds up in the output section. */
3295 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3297 if (! howto
->partial_inplace
)
3299 /* For relocations with the addend in the
3300 relocation, we need just to update the addend.
3301 All real relocs are of type partial_inplace; this
3302 code is mostly for completeness. */
3303 rel
->r_addend
+= sec
->output_offset
;
3308 /* Relocs of type partial_inplace need to pick up the
3309 contents in the contents and add the offset resulting
3310 from the changed location of the section symbol.
3311 Using _bfd_final_link_relocate (e.g. goto
3312 final_link_relocate) here would be wrong, because
3313 relocations marked pc_relative would get the current
3314 location subtracted, and we must only do that at the
3316 r
= _bfd_relocate_contents (howto
, input_bfd
,
3319 contents
+ rel
->r_offset
);
3320 goto relocation_done
;
3325 else if (! howto
->partial_inplace
)
3327 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3328 addend
= rel
->r_addend
;
3330 else if ((sec
->flags
& SEC_MERGE
)
3331 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3335 if (howto
->rightshift
|| howto
->src_mask
!= 0xffffffff)
3337 (*_bfd_error_handler
)
3338 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3339 input_bfd
, input_section
,
3340 (long) rel
->r_offset
, howto
->name
);
3344 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3347 _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
)
3349 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
3350 bfd_put_32 (input_bfd
, addend
, contents
+ rel
->r_offset
);
3356 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3359 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3360 while (h
->root
.type
== bfd_link_hash_indirect
3361 || h
->root
.type
== bfd_link_hash_warning
)
3363 #ifdef INCLUDE_SHMEDIA
3364 /* If the reference passes a symbol marked with
3365 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
3367 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
3369 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3371 if (h
->root
.type
== bfd_link_hash_defined
3372 || h
->root
.type
== bfd_link_hash_defweak
)
3376 dyn
= htab
->root
.dynamic_sections_created
;
3377 sec
= h
->root
.u
.def
.section
;
3378 /* In these cases, we don't need the relocation value.
3379 We check specially because in some obscure cases
3380 sec->output_section will be NULL. */
3381 if (r_type
== R_SH_GOTPC
3382 || r_type
== R_SH_GOTPC_LOW16
3383 || r_type
== R_SH_GOTPC_MEDLOW16
3384 || r_type
== R_SH_GOTPC_MEDHI16
3385 || r_type
== R_SH_GOTPC_HI16
3386 || ((r_type
== R_SH_PLT32
3387 || r_type
== R_SH_PLT_LOW16
3388 || r_type
== R_SH_PLT_MEDLOW16
3389 || r_type
== R_SH_PLT_MEDHI16
3390 || r_type
== R_SH_PLT_HI16
)
3391 && h
->plt
.offset
!= (bfd_vma
) -1)
3392 || ((r_type
== R_SH_GOT32
3393 || r_type
== R_SH_GOT_LOW16
3394 || r_type
== R_SH_GOT_MEDLOW16
3395 || r_type
== R_SH_GOT_MEDHI16
3396 || r_type
== R_SH_GOT_HI16
)
3397 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3399 || (! info
->symbolic
&& h
->dynindx
!= -1)
3400 || !h
->def_regular
))
3401 /* The cases above are those in which relocation is
3402 overwritten in the switch block below. The cases
3403 below are those in which we must defer relocation
3404 to run-time, because we can't resolve absolute
3405 addresses when creating a shared library. */
3407 && ((! info
->symbolic
&& h
->dynindx
!= -1)
3409 && ((r_type
== R_SH_DIR32
3410 && !h
->forced_local
)
3411 || (r_type
== R_SH_REL32
3412 && !SYMBOL_CALLS_LOCAL (info
, h
)))
3413 && ((input_section
->flags
& SEC_ALLOC
) != 0
3414 /* DWARF will emit R_SH_DIR32 relocations in its
3415 sections against symbols defined externally
3416 in shared libraries. We can't do anything
3418 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
3419 && h
->def_dynamic
)))
3420 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3421 sections because such sections are not SEC_ALLOC and
3422 thus ld.so will not process them. */
3423 || (sec
->output_section
== NULL
3424 && ((input_section
->flags
& SEC_DEBUGGING
) != 0
3426 || (sec
->output_section
== NULL
3427 && (sh_elf_hash_entry (h
)->tls_type
== GOT_TLS_IE
3428 || sh_elf_hash_entry (h
)->tls_type
== GOT_TLS_GD
)))
3430 else if (sec
->output_section
!= NULL
)
3431 relocation
= ((h
->root
.u
.def
.value
3432 + sec
->output_section
->vma
3433 + sec
->output_offset
)
3434 /* A STO_SH5_ISA32 causes a "bitor 1" to the
3435 symbol value, unless we've seen
3436 STT_DATALABEL on the way to it. */
3437 | ((h
->other
& STO_SH5_ISA32
) != 0
3438 && ! seen_stt_datalabel
));
3439 else if (!info
->relocatable
)
3441 (*_bfd_error_handler
)
3442 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3445 (long) rel
->r_offset
,
3447 h
->root
.root
.string
);
3451 else if (h
->root
.type
== bfd_link_hash_undefweak
)
3453 else if (info
->unresolved_syms_in_objects
== RM_IGNORE
3454 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3456 else if (!info
->relocatable
)
3458 if (! info
->callbacks
->undefined_symbol
3459 (info
, h
->root
.root
.string
, input_bfd
,
3460 input_section
, rel
->r_offset
,
3461 (info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
3462 || ELF_ST_VISIBILITY (h
->other
))))
3467 if (sec
!= NULL
&& elf_discarded_section (sec
))
3469 /* For relocs against symbols from removed linkonce sections,
3470 or sections discarded by a linker script, we just want the
3471 section contents zeroed. Avoid any special processing. */
3472 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
3478 if (info
->relocatable
)
3481 switch ((int) r_type
)
3483 final_link_relocate
:
3484 /* COFF relocs don't use the addend. The addend is used for
3485 R_SH_DIR32 to be compatible with other compilers. */
3486 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3487 contents
, rel
->r_offset
,
3488 relocation
, addend
);
3492 goto final_link_relocate
;
3497 /* If the reloc is against the start of this section, then
3498 the assembler has already taken care of it and the reloc
3499 is here only to assist in relaxing. If the reloc is not
3500 against the start of this section, then it's against an
3501 external symbol and we must deal with it ourselves. */
3502 if (input_section
->output_section
->vma
+ input_section
->output_offset
3505 int disp
= (relocation
3506 - input_section
->output_section
->vma
3507 - input_section
->output_offset
3513 case R_SH_DIR8WPZ
: mask
= 1; break;
3514 case R_SH_DIR8WPL
: mask
= 3; break;
3515 default: mask
= 0; break;
3519 ((*_bfd_error_handler
)
3520 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
3521 input_section
->owner
,
3522 (unsigned long) rel
->r_offset
));
3523 bfd_set_error (bfd_error_bad_value
);
3527 goto final_link_relocate
;
3533 #ifdef INCLUDE_SHMEDIA
3534 if (shmedia_prepare_reloc (info
, input_bfd
, input_section
,
3535 contents
, rel
, &relocation
))
3536 goto final_link_relocate
;
3538 bfd_set_error (bfd_error_bad_value
);
3546 goto final_link_relocate
;
3552 ((*_bfd_error_handler
)
3553 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3554 input_section
->owner
,
3555 (unsigned long) rel
->r_offset
, howto
->name
,
3556 (unsigned long) relocation
));
3557 bfd_set_error (bfd_error_bad_value
);
3560 goto final_link_relocate
;
3567 ((*_bfd_error_handler
)
3568 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3569 input_section
->owner
,
3570 (unsigned long) rel
->r_offset
, howto
->name
,
3571 (unsigned long) relocation
));
3572 bfd_set_error (bfd_error_bad_value
);
3575 goto final_link_relocate
;
3578 if ((signed int)relocation
< -32
3579 || (signed int)relocation
> 32)
3581 ((*_bfd_error_handler
)
3582 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
3583 input_section
->owner
,
3584 (unsigned long) rel
->r_offset
,
3585 (unsigned long) relocation
));
3586 bfd_set_error (bfd_error_bad_value
);
3589 goto final_link_relocate
;
3592 if ((signed int)relocation
< -16
3593 || (signed int)relocation
> 16)
3595 ((*_bfd_error_handler
)
3596 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
3597 input_section
->owner
,
3598 (unsigned long) rel
->r_offset
,
3599 (unsigned long) relocation
));
3600 bfd_set_error (bfd_error_bad_value
);
3603 goto final_link_relocate
;
3607 #ifdef INCLUDE_SHMEDIA
3608 case R_SH_IMM_LOW16_PCREL
:
3609 case R_SH_IMM_MEDLOW16_PCREL
:
3610 case R_SH_IMM_MEDHI16_PCREL
:
3611 case R_SH_IMM_HI16_PCREL
:
3615 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3616 || h
->root
.type
!= bfd_link_hash_undefweak
)
3618 && (input_section
->flags
& SEC_ALLOC
) != 0
3620 && (r_type
== R_SH_DIR32
3621 || !SYMBOL_CALLS_LOCAL (info
, h
)))
3623 Elf_Internal_Rela outrel
;
3625 bfd_boolean skip
, relocate
;
3627 /* When generating a shared object, these relocations
3628 are copied into the output file to be resolved at run
3635 name
= (bfd_elf_string_from_elf_section
3637 elf_elfheader (input_bfd
)->e_shstrndx
,
3638 elf_section_data (input_section
)->rel_hdr
.sh_name
));
3642 BFD_ASSERT (CONST_STRNEQ (name
, ".rela")
3643 && strcmp (bfd_get_section_name (input_bfd
,
3647 sreloc
= bfd_get_section_by_name (dynobj
, name
);
3648 BFD_ASSERT (sreloc
!= NULL
);
3655 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3657 if (outrel
.r_offset
== (bfd_vma
) -1)
3659 else if (outrel
.r_offset
== (bfd_vma
) -2)
3660 skip
= TRUE
, relocate
= TRUE
;
3661 outrel
.r_offset
+= (input_section
->output_section
->vma
3662 + input_section
->output_offset
);
3665 memset (&outrel
, 0, sizeof outrel
);
3666 else if (r_type
== R_SH_REL32
)
3668 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3669 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
3671 = (howto
->partial_inplace
3672 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
3675 #ifdef INCLUDE_SHMEDIA
3676 else if (r_type
== R_SH_IMM_LOW16_PCREL
3677 || r_type
== R_SH_IMM_MEDLOW16_PCREL
3678 || r_type
== R_SH_IMM_MEDHI16_PCREL
3679 || r_type
== R_SH_IMM_HI16_PCREL
)
3681 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3682 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
3683 outrel
.r_addend
= addend
;
3688 /* h->dynindx may be -1 if this symbol was marked to
3691 || ((info
->symbolic
|| h
->dynindx
== -1)
3694 relocate
= howto
->partial_inplace
;
3695 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3699 BFD_ASSERT (h
->dynindx
!= -1);
3700 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
3702 outrel
.r_addend
= relocation
;
3704 += (howto
->partial_inplace
3705 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
3709 loc
= sreloc
->contents
;
3710 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3711 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3713 /* If this reloc is against an external symbol, we do
3714 not want to fiddle with the addend. Otherwise, we
3715 need to include the symbol value so that it becomes
3716 an addend for the dynamic reloc. */
3720 goto final_link_relocate
;
3723 #ifdef INCLUDE_SHMEDIA
3724 case R_SH_GOTPLT_LOW16
:
3725 case R_SH_GOTPLT_MEDLOW16
:
3726 case R_SH_GOTPLT_MEDHI16
:
3727 case R_SH_GOTPLT_HI16
:
3728 case R_SH_GOTPLT10BY4
:
3729 case R_SH_GOTPLT10BY8
:
3731 /* Relocation is to the entry for this symbol in the
3732 procedure linkage table. */
3739 || h
->plt
.offset
== (bfd_vma
) -1
3740 || h
->got
.offset
!= (bfd_vma
) -1)
3743 /* Relocation is to the entry for this symbol in the global
3744 offset table extension for the procedure linkage table. */
3746 BFD_ASSERT (sgotplt
!= NULL
);
3747 relocation
= (sgotplt
->output_offset
3748 + (get_plt_index (htab
->plt_info
, h
->plt
.offset
)
3752 relocation
-= GOT_BIAS
;
3755 goto final_link_relocate
;
3759 #ifdef INCLUDE_SHMEDIA
3760 case R_SH_GOT_LOW16
:
3761 case R_SH_GOT_MEDLOW16
:
3762 case R_SH_GOT_MEDHI16
:
3767 /* Relocation is to the entry for this symbol in the global
3770 BFD_ASSERT (sgot
!= NULL
);
3776 off
= h
->got
.offset
;
3777 #ifdef INCLUDE_SHMEDIA
3778 if (seen_stt_datalabel
)
3780 struct elf_sh_link_hash_entry
*hsh
;
3782 hsh
= (struct elf_sh_link_hash_entry
*)h
;
3783 off
= hsh
->datalabel_got
.offset
;
3786 BFD_ASSERT (off
!= (bfd_vma
) -1);
3788 dyn
= htab
->root
.dynamic_sections_created
;
3789 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3791 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3792 || (ELF_ST_VISIBILITY (h
->other
)
3793 && h
->root
.type
== bfd_link_hash_undefweak
))
3795 /* This is actually a static link, or it is a
3796 -Bsymbolic link and the symbol is defined
3797 locally, or the symbol was forced to be local
3798 because of a version file. We must initialize
3799 this entry in the global offset table. Since the
3800 offset must always be a multiple of 4, we use the
3801 least significant bit to record whether we have
3802 initialized it already.
3804 When doing a dynamic link, we create a .rela.got
3805 relocation entry to initialize the value. This
3806 is done in the finish_dynamic_symbol routine. */
3811 bfd_put_32 (output_bfd
, relocation
,
3812 sgot
->contents
+ off
);
3813 #ifdef INCLUDE_SHMEDIA
3814 if (seen_stt_datalabel
)
3816 struct elf_sh_link_hash_entry
*hsh
;
3818 hsh
= (struct elf_sh_link_hash_entry
*)h
;
3819 hsh
->datalabel_got
.offset
|= 1;
3827 relocation
= sgot
->output_offset
+ off
;
3831 #ifdef INCLUDE_SHMEDIA
3834 BFD_ASSERT (local_got_offsets
!= NULL
3835 && (local_got_offsets
[symtab_hdr
->sh_info
3839 off
= local_got_offsets
[symtab_hdr
->sh_info
3845 BFD_ASSERT (local_got_offsets
!= NULL
3846 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
3848 off
= local_got_offsets
[r_symndx
];
3849 #ifdef INCLUDE_SHMEDIA
3853 /* The offset must always be a multiple of 4. We use
3854 the least significant bit to record whether we have
3855 already generated the necessary reloc. */
3860 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
3864 Elf_Internal_Rela outrel
;
3867 if (srelgot
== NULL
)
3869 srelgot
= bfd_get_section_by_name (dynobj
,
3871 BFD_ASSERT (srelgot
!= NULL
);
3874 outrel
.r_offset
= (sgot
->output_section
->vma
3875 + sgot
->output_offset
3877 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3878 outrel
.r_addend
= relocation
;
3879 loc
= srelgot
->contents
;
3880 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3881 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3884 #ifdef INCLUDE_SHMEDIA
3886 local_got_offsets
[symtab_hdr
->sh_info
+ r_symndx
] |= 1;
3889 local_got_offsets
[r_symndx
] |= 1;
3892 relocation
= sgot
->output_offset
+ off
;
3896 relocation
-= GOT_BIAS
;
3899 goto final_link_relocate
;
3902 #ifdef INCLUDE_SHMEDIA
3903 case R_SH_GOTOFF_LOW16
:
3904 case R_SH_GOTOFF_MEDLOW16
:
3905 case R_SH_GOTOFF_MEDHI16
:
3906 case R_SH_GOTOFF_HI16
:
3908 /* Relocation is relative to the start of the global offset
3911 BFD_ASSERT (sgot
!= NULL
);
3913 /* Note that sgot->output_offset is not involved in this
3914 calculation. We always want the start of .got. If we
3915 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3916 permitted by the ABI, we might have to change this
3918 relocation
-= sgot
->output_section
->vma
;
3921 relocation
-= GOT_BIAS
;
3924 addend
= rel
->r_addend
;
3926 goto final_link_relocate
;
3929 #ifdef INCLUDE_SHMEDIA
3930 case R_SH_GOTPC_LOW16
:
3931 case R_SH_GOTPC_MEDLOW16
:
3932 case R_SH_GOTPC_MEDHI16
:
3933 case R_SH_GOTPC_HI16
:
3935 /* Use global offset table as symbol value. */
3937 BFD_ASSERT (sgot
!= NULL
);
3938 relocation
= sgot
->output_section
->vma
;
3941 relocation
+= GOT_BIAS
;
3944 addend
= rel
->r_addend
;
3946 goto final_link_relocate
;
3949 #ifdef INCLUDE_SHMEDIA
3950 case R_SH_PLT_LOW16
:
3951 case R_SH_PLT_MEDLOW16
:
3952 case R_SH_PLT_MEDHI16
:
3955 /* Relocation is to the entry for this symbol in the
3956 procedure linkage table. */
3958 /* Resolve a PLT reloc against a local symbol directly,
3959 without using the procedure linkage table. */
3961 goto final_link_relocate
;
3963 if (h
->forced_local
)
3964 goto final_link_relocate
;
3966 if (h
->plt
.offset
== (bfd_vma
) -1)
3968 /* We didn't make a PLT entry for this symbol. This
3969 happens when statically linking PIC code, or when
3970 using -Bsymbolic. */
3971 goto final_link_relocate
;
3974 BFD_ASSERT (splt
!= NULL
);
3975 relocation
= (splt
->output_section
->vma
3976 + splt
->output_offset
3979 #ifdef INCLUDE_SHMEDIA
3983 addend
= rel
->r_addend
;
3985 goto final_link_relocate
;
3987 case R_SH_LOOP_START
:
3989 static bfd_vma start
, end
;
3991 start
= (relocation
+ rel
->r_addend
3992 - (sec
->output_section
->vma
+ sec
->output_offset
));
3993 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3994 rel
->r_offset
, sec
, start
, end
);
3998 end
= (relocation
+ rel
->r_addend
3999 - (sec
->output_section
->vma
+ sec
->output_offset
));
4000 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
4001 rel
->r_offset
, sec
, start
, end
);
4005 case R_SH_TLS_GD_32
:
4006 case R_SH_TLS_IE_32
:
4007 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
4008 tls_type
= GOT_UNKNOWN
;
4009 if (h
== NULL
&& local_got_offsets
)
4010 tls_type
= sh_elf_local_got_tls_type (input_bfd
) [r_symndx
];
4013 tls_type
= sh_elf_hash_entry (h
)->tls_type
;
4015 && (h
->dynindx
== -1
4017 r_type
= R_SH_TLS_LE_32
;
4020 if (r_type
== R_SH_TLS_GD_32
&& tls_type
== GOT_TLS_IE
)
4021 r_type
= R_SH_TLS_IE_32
;
4023 if (r_type
== R_SH_TLS_LE_32
)
4026 unsigned short insn
;
4028 if (ELF32_R_TYPE (rel
->r_info
) == R_SH_TLS_GD_32
)
4030 /* GD->LE transition:
4031 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4032 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4033 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4035 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
4037 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
4039 offset
= rel
->r_offset
;
4040 BFD_ASSERT (offset
>= 16);
4041 /* Size of GD instructions is 16 or 18. */
4043 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4044 if ((insn
& 0xff00) == 0xc700)
4046 BFD_ASSERT (offset
>= 2);
4048 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4051 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4052 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4053 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4054 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4055 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4056 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4057 BFD_ASSERT (insn
== 0x310c);
4058 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4059 BFD_ASSERT (insn
== 0x410b);
4060 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4061 BFD_ASSERT (insn
== 0x34cc);
4063 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 2);
4064 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 4);
4065 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
4066 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4067 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4073 /* IE->LE transition:
4074 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
4075 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
4077 mov.l .Ln,rM; stc gbr,rN; nop; ...;
4080 offset
= rel
->r_offset
;
4081 BFD_ASSERT (offset
>= 16);
4082 /* Size of IE instructions is 10 or 12. */
4084 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4085 if ((insn
& 0xf0ff) == 0x0012)
4087 BFD_ASSERT (offset
>= 2);
4089 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4092 BFD_ASSERT ((insn
& 0xff00) == 0xd000);
4093 index
= insn
& 0x00ff;
4094 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4095 BFD_ASSERT ((insn
& 0xf0ff) == 0x0012);
4096 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4097 BFD_ASSERT ((insn
& 0xf0ff) == 0x00ce);
4098 insn
= 0xd000 | (insn
& 0x0f00) | index
;
4099 bfd_put_16 (output_bfd
, insn
, contents
+ offset
+ 0);
4100 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4103 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4104 contents
+ rel
->r_offset
);
4113 off
= h
->got
.offset
;
4116 if (local_got_offsets
== NULL
)
4119 off
= local_got_offsets
[r_symndx
];
4122 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4123 if (r_type
== R_SH_TLS_IE_32
4124 && ! htab
->root
.dynamic_sections_created
)
4127 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4128 sgot
->contents
+ off
);
4129 bfd_put_32 (output_bfd
, sgot
->output_offset
+ off
,
4130 contents
+ rel
->r_offset
);
4138 Elf_Internal_Rela outrel
;
4142 if (srelgot
== NULL
)
4144 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
4145 BFD_ASSERT (srelgot
!= NULL
);
4148 outrel
.r_offset
= (sgot
->output_section
->vma
4149 + sgot
->output_offset
+ off
);
4151 if (h
== NULL
|| h
->dynindx
== -1)
4156 dr_type
= (r_type
== R_SH_TLS_GD_32
? R_SH_TLS_DTPMOD32
:
4158 if (dr_type
== R_SH_TLS_TPOFF32
&& indx
== 0)
4159 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4161 outrel
.r_addend
= 0;
4162 outrel
.r_info
= ELF32_R_INFO (indx
, dr_type
);
4163 loc
= srelgot
->contents
;
4164 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4165 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4167 if (r_type
== R_SH_TLS_GD_32
)
4171 bfd_put_32 (output_bfd
,
4172 relocation
- dtpoff_base (info
),
4173 sgot
->contents
+ off
+ 4);
4177 outrel
.r_info
= ELF32_R_INFO (indx
,
4179 outrel
.r_offset
+= 4;
4180 outrel
.r_addend
= 0;
4181 srelgot
->reloc_count
++;
4182 loc
+= sizeof (Elf32_External_Rela
);
4183 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4190 local_got_offsets
[r_symndx
] |= 1;
4193 if (off
>= (bfd_vma
) -2)
4196 if (r_type
== (int) ELF32_R_TYPE (rel
->r_info
))
4197 relocation
= sgot
->output_offset
+ off
;
4201 unsigned short insn
;
4203 /* GD->IE transition:
4204 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4205 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4206 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4208 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4209 nop; nop; bra 3f; nop; .align 2;
4210 1: .long x@TPOFF; 2:...; 3:. */
4212 offset
= rel
->r_offset
;
4213 BFD_ASSERT (offset
>= 16);
4214 /* Size of GD instructions is 16 or 18. */
4216 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4217 if ((insn
& 0xff00) == 0xc700)
4219 BFD_ASSERT (offset
>= 2);
4221 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4224 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4226 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4227 bfd_put_16 (output_bfd
, insn
& 0xf0ff, contents
+ offset
);
4229 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4230 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4231 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4232 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4233 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4234 BFD_ASSERT (insn
== 0x310c);
4235 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4236 BFD_ASSERT (insn
== 0x410b);
4237 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4238 BFD_ASSERT (insn
== 0x34cc);
4240 bfd_put_16 (output_bfd
, 0x0412, contents
+ offset
+ 2);
4241 bfd_put_16 (output_bfd
, 0x00ce, contents
+ offset
+ 4);
4242 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 6);
4243 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4244 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4246 bfd_put_32 (output_bfd
, sgot
->output_offset
+ off
,
4247 contents
+ rel
->r_offset
);
4252 addend
= rel
->r_addend
;
4254 goto final_link_relocate
;
4256 case R_SH_TLS_LD_32
:
4260 unsigned short insn
;
4262 /* LD->LE transition:
4263 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4264 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4265 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4267 stc gbr,r0; nop; nop; nop;
4268 nop; nop; bra 3f; ...; 3:. */
4270 offset
= rel
->r_offset
;
4271 BFD_ASSERT (offset
>= 16);
4272 /* Size of LD instructions is 16 or 18. */
4274 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4275 if ((insn
& 0xff00) == 0xc700)
4277 BFD_ASSERT (offset
>= 2);
4279 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4282 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4283 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4284 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4285 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4286 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4287 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4288 BFD_ASSERT (insn
== 0x310c);
4289 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4290 BFD_ASSERT (insn
== 0x410b);
4291 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4292 BFD_ASSERT (insn
== 0x34cc);
4294 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 0);
4295 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 2);
4296 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4297 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
4298 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4299 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4308 off
= htab
->tls_ldm_got
.offset
;
4313 Elf_Internal_Rela outrel
;
4316 srelgot
= htab
->srelgot
;
4317 if (srelgot
== NULL
)
4320 outrel
.r_offset
= (sgot
->output_section
->vma
4321 + sgot
->output_offset
+ off
);
4322 outrel
.r_addend
= 0;
4323 outrel
.r_info
= ELF32_R_INFO (0, R_SH_TLS_DTPMOD32
);
4324 loc
= srelgot
->contents
;
4325 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4326 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4327 htab
->tls_ldm_got
.offset
|= 1;
4330 relocation
= sgot
->output_offset
+ off
;
4331 addend
= rel
->r_addend
;
4333 goto final_link_relocate
;
4335 case R_SH_TLS_LDO_32
:
4337 relocation
= tpoff (info
, relocation
);
4339 relocation
-= dtpoff_base (info
);
4341 addend
= rel
->r_addend
;
4342 goto final_link_relocate
;
4344 case R_SH_TLS_LE_32
:
4347 Elf_Internal_Rela outrel
;
4352 relocation
= tpoff (info
, relocation
);
4353 addend
= rel
->r_addend
;
4354 goto final_link_relocate
;
4361 name
= (bfd_elf_string_from_elf_section
4363 elf_elfheader (input_bfd
)->e_shstrndx
,
4364 elf_section_data (input_section
)->rel_hdr
.sh_name
));
4368 BFD_ASSERT (CONST_STRNEQ (name
, ".rela")
4369 && strcmp (bfd_get_section_name (input_bfd
,
4373 sreloc
= bfd_get_section_by_name (dynobj
, name
);
4374 BFD_ASSERT (sreloc
!= NULL
);
4377 if (h
== NULL
|| h
->dynindx
== -1)
4382 outrel
.r_offset
= (input_section
->output_section
->vma
4383 + input_section
->output_offset
4385 outrel
.r_info
= ELF32_R_INFO (indx
, R_SH_TLS_TPOFF32
);
4387 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4389 outrel
.r_addend
= 0;
4391 loc
= sreloc
->contents
;
4392 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4393 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4399 if (r
!= bfd_reloc_ok
)
4404 case bfd_reloc_outofrange
:
4406 case bfd_reloc_overflow
:
4414 name
= (bfd_elf_string_from_elf_section
4415 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
4419 name
= bfd_section_name (input_bfd
, sec
);
4421 if (! ((*info
->callbacks
->reloc_overflow
)
4422 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
4423 (bfd_vma
) 0, input_bfd
, input_section
,
4435 /* This is a version of bfd_generic_get_relocated_section_contents
4436 which uses sh_elf_relocate_section. */
4439 sh_elf_get_relocated_section_contents (bfd
*output_bfd
,
4440 struct bfd_link_info
*link_info
,
4441 struct bfd_link_order
*link_order
,
4443 bfd_boolean relocatable
,
4446 Elf_Internal_Shdr
*symtab_hdr
;
4447 asection
*input_section
= link_order
->u
.indirect
.section
;
4448 bfd
*input_bfd
= input_section
->owner
;
4449 asection
**sections
= NULL
;
4450 Elf_Internal_Rela
*internal_relocs
= NULL
;
4451 Elf_Internal_Sym
*isymbuf
= NULL
;
4453 /* We only need to handle the case of relaxing, or of having a
4454 particular set of section contents, specially. */
4456 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
4457 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
4462 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
4464 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
4465 (size_t) input_section
->size
);
4467 if ((input_section
->flags
& SEC_RELOC
) != 0
4468 && input_section
->reloc_count
> 0)
4471 Elf_Internal_Sym
*isym
, *isymend
;
4474 internal_relocs
= (_bfd_elf_link_read_relocs
4475 (input_bfd
, input_section
, NULL
,
4476 (Elf_Internal_Rela
*) NULL
, FALSE
));
4477 if (internal_relocs
== NULL
)
4480 if (symtab_hdr
->sh_info
!= 0)
4482 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4483 if (isymbuf
== NULL
)
4484 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
4485 symtab_hdr
->sh_info
, 0,
4487 if (isymbuf
== NULL
)
4491 amt
= symtab_hdr
->sh_info
;
4492 amt
*= sizeof (asection
*);
4493 sections
= (asection
**) bfd_malloc (amt
);
4494 if (sections
== NULL
&& amt
!= 0)
4497 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
4498 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
4502 if (isym
->st_shndx
== SHN_UNDEF
)
4503 isec
= bfd_und_section_ptr
;
4504 else if (isym
->st_shndx
== SHN_ABS
)
4505 isec
= bfd_abs_section_ptr
;
4506 else if (isym
->st_shndx
== SHN_COMMON
)
4507 isec
= bfd_com_section_ptr
;
4509 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
4514 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
4515 input_section
, data
, internal_relocs
,
4519 if (sections
!= NULL
)
4522 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4524 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
4525 free (internal_relocs
);
4531 if (sections
!= NULL
)
4534 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4536 if (internal_relocs
!= NULL
4537 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
4538 free (internal_relocs
);
4542 /* Return the base VMA address which should be subtracted from real addresses
4543 when resolving @dtpoff relocation.
4544 This is PT_TLS segment p_vaddr. */
4547 dtpoff_base (struct bfd_link_info
*info
)
4549 /* If tls_sec is NULL, we should have signalled an error already. */
4550 if (elf_hash_table (info
)->tls_sec
== NULL
)
4552 return elf_hash_table (info
)->tls_sec
->vma
;
4555 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
4558 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
4560 /* If tls_sec is NULL, we should have signalled an error already. */
4561 if (elf_hash_table (info
)->tls_sec
== NULL
)
4563 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
4564 structure which has 2 pointer fields. */
4565 return (address
- elf_hash_table (info
)->tls_sec
->vma
4566 + align_power ((bfd_vma
) 8,
4567 elf_hash_table (info
)->tls_sec
->alignment_power
));
4571 sh_elf_gc_mark_hook (asection
*sec
,
4572 struct bfd_link_info
*info
,
4573 Elf_Internal_Rela
*rel
,
4574 struct elf_link_hash_entry
*h
,
4575 Elf_Internal_Sym
*sym
)
4578 switch (ELF32_R_TYPE (rel
->r_info
))
4580 case R_SH_GNU_VTINHERIT
:
4581 case R_SH_GNU_VTENTRY
:
4585 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
4588 /* Update the got entry reference counts for the section being removed. */
4591 sh_elf_gc_sweep_hook (bfd
*abfd
, struct bfd_link_info
*info
,
4592 asection
*sec
, const Elf_Internal_Rela
*relocs
)
4594 Elf_Internal_Shdr
*symtab_hdr
;
4595 struct elf_link_hash_entry
**sym_hashes
;
4596 bfd_signed_vma
*local_got_refcounts
;
4597 const Elf_Internal_Rela
*rel
, *relend
;
4599 if (info
->relocatable
)
4602 elf_section_data (sec
)->local_dynrel
= NULL
;
4604 symtab_hdr
= &elf_symtab_hdr (abfd
);
4605 sym_hashes
= elf_sym_hashes (abfd
);
4606 local_got_refcounts
= elf_local_got_refcounts (abfd
);
4608 relend
= relocs
+ sec
->reloc_count
;
4609 for (rel
= relocs
; rel
< relend
; rel
++)
4611 unsigned long r_symndx
;
4612 unsigned int r_type
;
4613 struct elf_link_hash_entry
*h
= NULL
;
4614 #ifdef INCLUDE_SHMEDIA
4615 int seen_stt_datalabel
= 0;
4618 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4619 if (r_symndx
>= symtab_hdr
->sh_info
)
4621 struct elf_sh_link_hash_entry
*eh
;
4622 struct elf_sh_dyn_relocs
**pp
;
4623 struct elf_sh_dyn_relocs
*p
;
4625 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4626 while (h
->root
.type
== bfd_link_hash_indirect
4627 || h
->root
.type
== bfd_link_hash_warning
)
4629 #ifdef INCLUDE_SHMEDIA
4630 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
4632 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4634 eh
= (struct elf_sh_link_hash_entry
*) h
;
4635 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
4638 /* Everything must go for SEC. */
4644 r_type
= ELF32_R_TYPE (rel
->r_info
);
4645 switch (sh_elf_optimized_tls_reloc (info
, r_type
, h
!= NULL
))
4647 case R_SH_TLS_LD_32
:
4648 if (sh_elf_hash_table (info
)->tls_ldm_got
.refcount
> 0)
4649 sh_elf_hash_table (info
)->tls_ldm_got
.refcount
-= 1;
4655 #ifdef INCLUDE_SHMEDIA
4656 case R_SH_GOT_LOW16
:
4657 case R_SH_GOT_MEDLOW16
:
4658 case R_SH_GOT_MEDHI16
:
4662 case R_SH_GOTOFF_LOW16
:
4663 case R_SH_GOTOFF_MEDLOW16
:
4664 case R_SH_GOTOFF_MEDHI16
:
4665 case R_SH_GOTOFF_HI16
:
4666 case R_SH_GOTPC_LOW16
:
4667 case R_SH_GOTPC_MEDLOW16
:
4668 case R_SH_GOTPC_MEDHI16
:
4669 case R_SH_GOTPC_HI16
:
4671 case R_SH_TLS_GD_32
:
4672 case R_SH_TLS_IE_32
:
4675 #ifdef INCLUDE_SHMEDIA
4676 if (seen_stt_datalabel
)
4678 struct elf_sh_link_hash_entry
*eh
;
4679 eh
= (struct elf_sh_link_hash_entry
*) h
;
4680 if (eh
->datalabel_got
.refcount
> 0)
4681 eh
->datalabel_got
.refcount
-= 1;
4685 if (h
->got
.refcount
> 0)
4686 h
->got
.refcount
-= 1;
4688 else if (local_got_refcounts
!= NULL
)
4690 #ifdef INCLUDE_SHMEDIA
4691 if (rel
->r_addend
& 1)
4693 if (local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] > 0)
4694 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] -= 1;
4698 if (local_got_refcounts
[r_symndx
] > 0)
4699 local_got_refcounts
[r_symndx
] -= 1;
4710 #ifdef INCLUDE_SHMEDIA
4711 case R_SH_PLT_LOW16
:
4712 case R_SH_PLT_MEDLOW16
:
4713 case R_SH_PLT_MEDHI16
:
4718 if (h
->plt
.refcount
> 0)
4719 h
->plt
.refcount
-= 1;
4724 #ifdef INCLUDE_SHMEDIA
4725 case R_SH_GOTPLT_LOW16
:
4726 case R_SH_GOTPLT_MEDLOW16
:
4727 case R_SH_GOTPLT_MEDHI16
:
4728 case R_SH_GOTPLT_HI16
:
4729 case R_SH_GOTPLT10BY4
:
4730 case R_SH_GOTPLT10BY8
:
4734 struct elf_sh_link_hash_entry
*eh
;
4735 eh
= (struct elf_sh_link_hash_entry
*) h
;
4736 if (eh
->gotplt_refcount
> 0)
4738 eh
->gotplt_refcount
-= 1;
4739 if (h
->plt
.refcount
> 0)
4740 h
->plt
.refcount
-= 1;
4742 #ifdef INCLUDE_SHMEDIA
4743 else if (seen_stt_datalabel
)
4745 if (eh
->datalabel_got
.refcount
> 0)
4746 eh
->datalabel_got
.refcount
-= 1;
4749 else if (h
->got
.refcount
> 0)
4750 h
->got
.refcount
-= 1;
4752 else if (local_got_refcounts
!= NULL
)
4754 #ifdef INCLUDE_SHMEDIA
4755 if (rel
->r_addend
& 1)
4757 if (local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] > 0)
4758 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] -= 1;
4762 if (local_got_refcounts
[r_symndx
] > 0)
4763 local_got_refcounts
[r_symndx
] -= 1;
4775 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4778 sh_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
4779 struct elf_link_hash_entry
*dir
,
4780 struct elf_link_hash_entry
*ind
)
4782 struct elf_sh_link_hash_entry
*edir
, *eind
;
4784 edir
= (struct elf_sh_link_hash_entry
*) dir
;
4785 eind
= (struct elf_sh_link_hash_entry
*) ind
;
4787 if (eind
->dyn_relocs
!= NULL
)
4789 if (edir
->dyn_relocs
!= NULL
)
4791 struct elf_sh_dyn_relocs
**pp
;
4792 struct elf_sh_dyn_relocs
*p
;
4794 /* Add reloc counts against the indirect sym to the direct sym
4795 list. Merge any entries against the same section. */
4796 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
4798 struct elf_sh_dyn_relocs
*q
;
4800 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
4801 if (q
->sec
== p
->sec
)
4803 q
->pc_count
+= p
->pc_count
;
4804 q
->count
+= p
->count
;
4811 *pp
= edir
->dyn_relocs
;
4814 edir
->dyn_relocs
= eind
->dyn_relocs
;
4815 eind
->dyn_relocs
= NULL
;
4817 edir
->gotplt_refcount
= eind
->gotplt_refcount
;
4818 eind
->gotplt_refcount
= 0;
4819 #ifdef INCLUDE_SHMEDIA
4820 edir
->datalabel_got
.refcount
+= eind
->datalabel_got
.refcount
;
4821 eind
->datalabel_got
.refcount
= 0;
4824 if (ind
->root
.type
== bfd_link_hash_indirect
4825 && dir
->got
.refcount
<= 0)
4827 edir
->tls_type
= eind
->tls_type
;
4828 eind
->tls_type
= GOT_UNKNOWN
;
4831 if (ind
->root
.type
!= bfd_link_hash_indirect
4832 && dir
->dynamic_adjusted
)
4834 /* If called to transfer flags for a weakdef during processing
4835 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
4836 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4837 dir
->ref_dynamic
|= ind
->ref_dynamic
;
4838 dir
->ref_regular
|= ind
->ref_regular
;
4839 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
4840 dir
->needs_plt
|= ind
->needs_plt
;
4843 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
4847 sh_elf_optimized_tls_reloc (struct bfd_link_info
*info
, int r_type
,
4855 case R_SH_TLS_GD_32
:
4856 case R_SH_TLS_IE_32
:
4858 return R_SH_TLS_LE_32
;
4859 return R_SH_TLS_IE_32
;
4860 case R_SH_TLS_LD_32
:
4861 return R_SH_TLS_LE_32
;
4867 /* Look through the relocs for a section during the first phase.
4868 Since we don't do .gots or .plts, we just need to consider the
4869 virtual table relocs for gc. */
4872 sh_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
4873 const Elf_Internal_Rela
*relocs
)
4875 Elf_Internal_Shdr
*symtab_hdr
;
4876 struct elf_link_hash_entry
**sym_hashes
;
4877 struct elf_sh_link_hash_table
*htab
;
4878 const Elf_Internal_Rela
*rel
;
4879 const Elf_Internal_Rela
*rel_end
;
4880 bfd_vma
*local_got_offsets
;
4884 unsigned int r_type
;
4885 int tls_type
, old_tls_type
;
4891 if (info
->relocatable
)
4894 BFD_ASSERT (is_sh_elf (abfd
));
4896 symtab_hdr
= &elf_symtab_hdr (abfd
);
4897 sym_hashes
= elf_sym_hashes (abfd
);
4899 htab
= sh_elf_hash_table (info
);
4900 local_got_offsets
= elf_local_got_offsets (abfd
);
4902 rel_end
= relocs
+ sec
->reloc_count
;
4903 for (rel
= relocs
; rel
< rel_end
; rel
++)
4905 struct elf_link_hash_entry
*h
;
4906 unsigned long r_symndx
;
4907 #ifdef INCLUDE_SHMEDIA
4908 int seen_stt_datalabel
= 0;
4911 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4912 r_type
= ELF32_R_TYPE (rel
->r_info
);
4914 if (r_symndx
< symtab_hdr
->sh_info
)
4918 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4919 while (h
->root
.type
== bfd_link_hash_indirect
4920 || h
->root
.type
== bfd_link_hash_warning
)
4922 #ifdef INCLUDE_SHMEDIA
4923 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
4925 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4929 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
4931 && r_type
== R_SH_TLS_IE_32
4933 && h
->root
.type
!= bfd_link_hash_undefined
4934 && h
->root
.type
!= bfd_link_hash_undefweak
4935 && (h
->dynindx
== -1
4937 r_type
= R_SH_TLS_LE_32
;
4939 /* Some relocs require a global offset table. */
4940 if (htab
->sgot
== NULL
)
4948 #ifdef INCLUDE_SHMEDIA
4949 case R_SH_GOTPLT_LOW16
:
4950 case R_SH_GOTPLT_MEDLOW16
:
4951 case R_SH_GOTPLT_MEDHI16
:
4952 case R_SH_GOTPLT_HI16
:
4953 case R_SH_GOTPLT10BY4
:
4954 case R_SH_GOTPLT10BY8
:
4955 case R_SH_GOT_LOW16
:
4956 case R_SH_GOT_MEDLOW16
:
4957 case R_SH_GOT_MEDHI16
:
4961 case R_SH_GOTOFF_LOW16
:
4962 case R_SH_GOTOFF_MEDLOW16
:
4963 case R_SH_GOTOFF_MEDHI16
:
4964 case R_SH_GOTOFF_HI16
:
4965 case R_SH_GOTPC_LOW16
:
4966 case R_SH_GOTPC_MEDLOW16
:
4967 case R_SH_GOTPC_MEDHI16
:
4968 case R_SH_GOTPC_HI16
:
4970 case R_SH_TLS_GD_32
:
4971 case R_SH_TLS_LD_32
:
4972 case R_SH_TLS_IE_32
:
4973 if (htab
->sgot
== NULL
)
4975 if (htab
->root
.dynobj
== NULL
)
4976 htab
->root
.dynobj
= abfd
;
4977 if (!create_got_section (htab
->root
.dynobj
, info
))
4989 /* This relocation describes the C++ object vtable hierarchy.
4990 Reconstruct it for later use during GC. */
4991 case R_SH_GNU_VTINHERIT
:
4992 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
4996 /* This relocation describes which C++ vtable entries are actually
4997 used. Record for later use during GC. */
4998 case R_SH_GNU_VTENTRY
:
4999 BFD_ASSERT (h
!= NULL
);
5001 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
5005 case R_SH_TLS_IE_32
:
5007 info
->flags
|= DF_STATIC_TLS
;
5011 case R_SH_TLS_GD_32
:
5013 #ifdef INCLUDE_SHMEDIA
5014 case R_SH_GOT_LOW16
:
5015 case R_SH_GOT_MEDLOW16
:
5016 case R_SH_GOT_MEDHI16
:
5024 tls_type
= GOT_NORMAL
;
5026 case R_SH_TLS_GD_32
:
5027 tls_type
= GOT_TLS_GD
;
5029 case R_SH_TLS_IE_32
:
5030 tls_type
= GOT_TLS_IE
;
5036 #ifdef INCLUDE_SHMEDIA
5037 if (seen_stt_datalabel
)
5039 struct elf_sh_link_hash_entry
*eh
5040 = (struct elf_sh_link_hash_entry
*) h
;
5042 eh
->datalabel_got
.refcount
+= 1;
5046 h
->got
.refcount
+= 1;
5047 old_tls_type
= sh_elf_hash_entry (h
)->tls_type
;
5051 bfd_signed_vma
*local_got_refcounts
;
5053 /* This is a global offset table entry for a local
5055 local_got_refcounts
= elf_local_got_refcounts (abfd
);
5056 if (local_got_refcounts
== NULL
)
5060 size
= symtab_hdr
->sh_info
;
5061 size
*= sizeof (bfd_signed_vma
);
5062 #ifdef INCLUDE_SHMEDIA
5063 /* Reserve space for both the datalabel and
5064 codelabel local GOT offsets. */
5067 size
+= symtab_hdr
->sh_info
;
5068 local_got_refcounts
= ((bfd_signed_vma
*)
5069 bfd_zalloc (abfd
, size
));
5070 if (local_got_refcounts
== NULL
)
5072 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
5073 #ifdef INCLUDE_SHMEDIA
5074 /* Take care of both the datalabel and codelabel local
5076 sh_elf_local_got_tls_type (abfd
)
5077 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
5079 sh_elf_local_got_tls_type (abfd
)
5080 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
5083 #ifdef INCLUDE_SHMEDIA
5084 if (rel
->r_addend
& 1)
5085 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] += 1;
5088 local_got_refcounts
[r_symndx
] += 1;
5089 old_tls_type
= sh_elf_local_got_tls_type (abfd
) [r_symndx
];
5092 /* If a TLS symbol is accessed using IE at least once,
5093 there is no point to use dynamic model for it. */
5094 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
5095 && (old_tls_type
!= GOT_TLS_GD
|| tls_type
!= GOT_TLS_IE
))
5097 if (old_tls_type
== GOT_TLS_IE
&& tls_type
== GOT_TLS_GD
)
5098 tls_type
= GOT_TLS_IE
;
5101 (*_bfd_error_handler
)
5102 (_("%B: `%s' accessed both as normal and thread local symbol"),
5103 abfd
, h
->root
.root
.string
);
5108 if (old_tls_type
!= tls_type
)
5111 sh_elf_hash_entry (h
)->tls_type
= tls_type
;
5113 sh_elf_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
5118 case R_SH_TLS_LD_32
:
5119 sh_elf_hash_table(info
)->tls_ldm_got
.refcount
+= 1;
5123 #ifdef INCLUDE_SHMEDIA
5124 case R_SH_GOTPLT_LOW16
:
5125 case R_SH_GOTPLT_MEDLOW16
:
5126 case R_SH_GOTPLT_MEDHI16
:
5127 case R_SH_GOTPLT_HI16
:
5128 case R_SH_GOTPLT10BY4
:
5129 case R_SH_GOTPLT10BY8
:
5131 /* If this is a local symbol, we resolve it directly without
5132 creating a procedure linkage table entry. */
5138 || h
->dynindx
== -1)
5142 h
->plt
.refcount
+= 1;
5143 ((struct elf_sh_link_hash_entry
*) h
)->gotplt_refcount
+= 1;
5148 #ifdef INCLUDE_SHMEDIA
5149 case R_SH_PLT_LOW16
:
5150 case R_SH_PLT_MEDLOW16
:
5151 case R_SH_PLT_MEDHI16
:
5154 /* This symbol requires a procedure linkage table entry. We
5155 actually build the entry in adjust_dynamic_symbol,
5156 because this might be a case of linking PIC code which is
5157 never referenced by a dynamic object, in which case we
5158 don't need to generate a procedure linkage table entry
5161 /* If this is a local symbol, we resolve it directly without
5162 creating a procedure linkage table entry. */
5166 if (h
->forced_local
)
5170 h
->plt
.refcount
+= 1;
5175 #ifdef INCLUDE_SHMEDIA
5176 case R_SH_IMM_LOW16_PCREL
:
5177 case R_SH_IMM_MEDLOW16_PCREL
:
5178 case R_SH_IMM_MEDHI16_PCREL
:
5179 case R_SH_IMM_HI16_PCREL
:
5181 if (h
!= NULL
&& ! info
->shared
)
5184 h
->plt
.refcount
+= 1;
5187 /* If we are creating a shared library, and this is a reloc
5188 against a global symbol, or a non PC relative reloc
5189 against a local symbol, then we need to copy the reloc
5190 into the shared library. However, if we are linking with
5191 -Bsymbolic, we do not need to copy a reloc against a
5192 global symbol which is defined in an object we are
5193 including in the link (i.e., DEF_REGULAR is set). At
5194 this point we have not seen all the input files, so it is
5195 possible that DEF_REGULAR is not set now but will be set
5196 later (it is never cleared). We account for that
5197 possibility below by storing information in the
5198 dyn_relocs field of the hash table entry. A similar
5199 situation occurs when creating shared libraries and symbol
5200 visibility changes render the symbol local.
5202 If on the other hand, we are creating an executable, we
5203 may need to keep relocations for symbols satisfied by a
5204 dynamic library if we manage to avoid copy relocs for the
5207 && (sec
->flags
& SEC_ALLOC
) != 0
5208 && (r_type
!= R_SH_REL32
5210 && (! info
->symbolic
5211 || h
->root
.type
== bfd_link_hash_defweak
5212 || !h
->def_regular
))))
5214 && (sec
->flags
& SEC_ALLOC
) != 0
5216 && (h
->root
.type
== bfd_link_hash_defweak
5217 || !h
->def_regular
)))
5219 struct elf_sh_dyn_relocs
*p
;
5220 struct elf_sh_dyn_relocs
**head
;
5222 if (htab
->root
.dynobj
== NULL
)
5223 htab
->root
.dynobj
= abfd
;
5225 /* When creating a shared object, we must copy these
5226 reloc types into the output file. We create a reloc
5227 section in dynobj and make room for this reloc. */
5232 name
= (bfd_elf_string_from_elf_section
5234 elf_elfheader (abfd
)->e_shstrndx
,
5235 elf_section_data (sec
)->rel_hdr
.sh_name
));
5239 BFD_ASSERT (CONST_STRNEQ (name
, ".rela")
5240 && strcmp (bfd_get_section_name (abfd
, sec
),
5243 sreloc
= bfd_get_section_by_name (htab
->root
.dynobj
, name
);
5248 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
5249 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
5250 if ((sec
->flags
& SEC_ALLOC
) != 0)
5251 flags
|= SEC_ALLOC
| SEC_LOAD
;
5252 sreloc
= bfd_make_section_with_flags (htab
->root
.dynobj
,
5256 || ! bfd_set_section_alignment (htab
->root
.dynobj
,
5260 elf_section_data (sec
)->sreloc
= sreloc
;
5263 /* If this is a global symbol, we count the number of
5264 relocations we need for this symbol. */
5266 head
= &((struct elf_sh_link_hash_entry
*) h
)->dyn_relocs
;
5272 /* Track dynamic relocs needed for local syms too. */
5273 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
5278 vpp
= &elf_section_data (s
)->local_dynrel
;
5279 head
= (struct elf_sh_dyn_relocs
**) vpp
;
5283 if (p
== NULL
|| p
->sec
!= sec
)
5285 bfd_size_type amt
= sizeof (*p
);
5286 p
= bfd_alloc (htab
->root
.dynobj
, amt
);
5297 if (r_type
== R_SH_REL32
5298 #ifdef INCLUDE_SHMEDIA
5299 || r_type
== R_SH_IMM_LOW16_PCREL
5300 || r_type
== R_SH_IMM_MEDLOW16_PCREL
5301 || r_type
== R_SH_IMM_MEDHI16_PCREL
5302 || r_type
== R_SH_IMM_HI16_PCREL
5310 case R_SH_TLS_LE_32
:
5313 (*_bfd_error_handler
)
5314 (_("%B: TLS local exec code cannot be linked into shared objects"),
5321 case R_SH_TLS_LDO_32
:
5322 /* Nothing to do. */
5333 #ifndef sh_elf_set_mach_from_flags
5334 static unsigned int sh_ef_bfd_table
[] = { EF_SH_BFD_TABLE
};
5337 sh_elf_set_mach_from_flags (bfd
*abfd
)
5339 flagword flags
= elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
;
5341 if (flags
>= sizeof(sh_ef_bfd_table
))
5344 if (sh_ef_bfd_table
[flags
] == 0)
5347 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, sh_ef_bfd_table
[flags
]);
5353 /* Reverse table lookup for sh_ef_bfd_table[].
5354 Given a bfd MACH value from archures.c
5355 return the equivalent ELF flags from the table.
5356 Return -1 if no match is found. */
5359 sh_elf_get_flags_from_mach (unsigned long mach
)
5361 int i
= ARRAY_SIZE (sh_ef_bfd_table
) - 1;
5364 if (sh_ef_bfd_table
[i
] == mach
)
5367 /* shouldn't get here */
5372 #endif /* not sh_elf_set_mach_from_flags */
5374 #ifndef sh_elf_set_private_flags
5375 /* Function to keep SH specific file flags. */
5378 sh_elf_set_private_flags (bfd
*abfd
, flagword flags
)
5380 BFD_ASSERT (! elf_flags_init (abfd
)
5381 || elf_elfheader (abfd
)->e_flags
== flags
);
5383 elf_elfheader (abfd
)->e_flags
= flags
;
5384 elf_flags_init (abfd
) = TRUE
;
5385 return sh_elf_set_mach_from_flags (abfd
);
5387 #endif /* not sh_elf_set_private_flags */
5389 #ifndef sh_elf_copy_private_data
5390 /* Copy backend specific data from one object module to another */
5393 sh_elf_copy_private_data (bfd
* ibfd
, bfd
* obfd
)
5395 /* Copy object attributes. */
5396 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
5398 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
5401 return sh_elf_set_private_flags (obfd
, elf_elfheader (ibfd
)->e_flags
);
5403 #endif /* not sh_elf_copy_private_data */
5405 #ifndef sh_elf_merge_private_data
5407 /* This function returns the ELF architecture number that
5408 corresponds to the given arch_sh* flags. */
5411 sh_find_elf_flags (unsigned int arch_set
)
5413 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
5414 unsigned long bfd_mach
= sh_get_bfd_mach_from_arch_set (arch_set
);
5416 return sh_elf_get_flags_from_mach (bfd_mach
);
5419 /* This routine initialises the elf flags when required and
5420 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
5423 sh_elf_merge_private_data (bfd
*ibfd
, bfd
*obfd
)
5425 extern bfd_boolean
sh_merge_bfd_arch (bfd
*, bfd
*);
5427 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
5430 if (! elf_flags_init (obfd
))
5432 /* This happens when ld starts out with a 'blank' output file. */
5433 elf_flags_init (obfd
) = TRUE
;
5434 elf_elfheader (obfd
)->e_flags
= EF_SH1
;
5435 sh_elf_set_mach_from_flags (obfd
);
5438 if (! sh_merge_bfd_arch (ibfd
, obfd
))
5440 _bfd_error_handler ("%B: uses instructions which are incompatible "
5441 "with instructions used in previous modules",
5443 bfd_set_error (bfd_error_bad_value
);
5447 elf_elfheader (obfd
)->e_flags
=
5448 sh_elf_get_flags_from_mach (bfd_get_mach (obfd
));
5452 #endif /* not sh_elf_merge_private_data */
5454 /* Override the generic function because we need to store sh_elf_obj_tdata
5455 as the specific tdata. We set also the machine architecture from flags
5459 sh_elf_object_p (bfd
*abfd
)
5461 return sh_elf_set_mach_from_flags (abfd
);
5464 /* Finish up dynamic symbol handling. We set the contents of various
5465 dynamic sections here. */
5468 sh_elf_finish_dynamic_symbol (bfd
*output_bfd
, struct bfd_link_info
*info
,
5469 struct elf_link_hash_entry
*h
,
5470 Elf_Internal_Sym
*sym
)
5472 struct elf_sh_link_hash_table
*htab
;
5474 htab
= sh_elf_hash_table (info
);
5476 if (h
->plt
.offset
!= (bfd_vma
) -1)
5484 Elf_Internal_Rela rel
;
5487 /* This symbol has an entry in the procedure linkage table. Set
5490 BFD_ASSERT (h
->dynindx
!= -1);
5493 sgot
= htab
->sgotplt
;
5494 srel
= htab
->srelplt
;
5495 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
5497 /* Get the index in the procedure linkage table which
5498 corresponds to this symbol. This is the index of this symbol
5499 in all the symbols for which we are making plt entries. The
5500 first entry in the procedure linkage table is reserved. */
5501 plt_index
= get_plt_index (htab
->plt_info
, h
->plt
.offset
);
5503 /* Get the offset into the .got table of the entry that
5504 corresponds to this function. Each .got entry is 4 bytes.
5505 The first three are reserved. */
5506 got_offset
= (plt_index
+ 3) * 4;
5510 got_offset
-= GOT_BIAS
;
5513 /* Fill in the entry in the procedure linkage table. */
5514 memcpy (splt
->contents
+ h
->plt
.offset
,
5515 htab
->plt_info
->symbol_entry
,
5516 htab
->plt_info
->symbol_entry_size
);
5519 install_plt_field (output_bfd
, FALSE
, got_offset
,
5522 + htab
->plt_info
->symbol_fields
.got_entry
));
5525 install_plt_field (output_bfd
, FALSE
,
5526 (sgot
->output_section
->vma
5527 + sgot
->output_offset
5531 + htab
->plt_info
->symbol_fields
.got_entry
));
5532 if (htab
->vxworks_p
)
5534 unsigned int reachable_plts
, plts_per_4k
;
5537 /* Divide the PLT into groups. The first group contains
5538 REACHABLE_PLTS entries and the other groups contain
5539 PLTS_PER_4K entries. Entries in the first group can
5540 branch directly to .plt; those in later groups branch
5541 to the last element of the previous group. */
5542 /* ??? It would be better to create multiple copies of
5543 the common resolver stub. */
5544 reachable_plts
= ((4096
5545 - htab
->plt_info
->plt0_entry_size
5546 - (htab
->plt_info
->symbol_fields
.plt
+ 4))
5547 / htab
->plt_info
->symbol_entry_size
) + 1;
5548 plts_per_4k
= (4096 / htab
->plt_info
->symbol_entry_size
);
5549 if (plt_index
< reachable_plts
)
5550 distance
= -(h
->plt
.offset
5551 + htab
->plt_info
->symbol_fields
.plt
);
5553 distance
= -(((plt_index
- reachable_plts
) % plts_per_4k
+ 1)
5554 * htab
->plt_info
->symbol_entry_size
);
5556 /* Install the 'bra' with this offset. */
5557 bfd_put_16 (output_bfd
,
5558 0xa000 | (0x0fff & ((distance
- 4) / 2)),
5561 + htab
->plt_info
->symbol_fields
.plt
));
5564 install_plt_field (output_bfd
, TRUE
,
5565 splt
->output_section
->vma
+ splt
->output_offset
,
5568 + htab
->plt_info
->symbol_fields
.plt
));
5573 got_offset
+= GOT_BIAS
;
5576 install_plt_field (output_bfd
, FALSE
,
5577 plt_index
* sizeof (Elf32_External_Rela
),
5580 + htab
->plt_info
->symbol_fields
.reloc_offset
));
5582 /* Fill in the entry in the global offset table. */
5583 bfd_put_32 (output_bfd
,
5584 (splt
->output_section
->vma
5585 + splt
->output_offset
5587 + htab
->plt_info
->symbol_resolve_offset
),
5588 sgot
->contents
+ got_offset
);
5590 /* Fill in the entry in the .rela.plt section. */
5591 rel
.r_offset
= (sgot
->output_section
->vma
5592 + sgot
->output_offset
5594 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
5597 rel
.r_addend
= GOT_BIAS
;
5599 loc
= srel
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
5600 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5602 if (htab
->vxworks_p
&& !info
->shared
)
5604 /* Create the .rela.plt.unloaded relocations for this PLT entry.
5605 Begin by pointing LOC to the first such relocation. */
5606 loc
= (htab
->srelplt2
->contents
5607 + (plt_index
* 2 + 1) * sizeof (Elf32_External_Rela
));
5609 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
5610 for the PLT entry's pointer to the .got.plt entry. */
5611 rel
.r_offset
= (htab
->splt
->output_section
->vma
5612 + htab
->splt
->output_offset
5614 + htab
->plt_info
->symbol_fields
.got_entry
);
5615 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
5616 rel
.r_addend
= got_offset
;
5617 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5618 loc
+= sizeof (Elf32_External_Rela
);
5620 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
5621 the .got.plt entry, which initially points to .plt. */
5622 rel
.r_offset
= (htab
->sgotplt
->output_section
->vma
5623 + htab
->sgotplt
->output_offset
5625 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
, R_SH_DIR32
);
5627 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5630 if (!h
->def_regular
)
5632 /* Mark the symbol as undefined, rather than as defined in
5633 the .plt section. Leave the value alone. */
5634 sym
->st_shndx
= SHN_UNDEF
;
5638 if (h
->got
.offset
!= (bfd_vma
) -1
5639 && sh_elf_hash_entry (h
)->tls_type
!= GOT_TLS_GD
5640 && sh_elf_hash_entry (h
)->tls_type
!= GOT_TLS_IE
)
5644 Elf_Internal_Rela rel
;
5647 /* This symbol has an entry in the global offset table. Set it
5651 srel
= htab
->srelgot
;
5652 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
5654 rel
.r_offset
= (sgot
->output_section
->vma
5655 + sgot
->output_offset
5656 + (h
->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
+ h
->got
.offset
);
5674 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
5678 loc
= srel
->contents
;
5679 loc
+= srel
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5680 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5683 #ifdef INCLUDE_SHMEDIA
5685 struct elf_sh_link_hash_entry
*eh
;
5687 eh
= (struct elf_sh_link_hash_entry
*) h
;
5688 if (eh
->datalabel_got
.offset
!= (bfd_vma
) -1)
5692 Elf_Internal_Rela rel
;
5695 /* This symbol has a datalabel entry in the global offset table.
5699 srel
= htab
->srelgot
;
5700 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
5702 rel
.r_offset
= (sgot
->output_section
->vma
5703 + sgot
->output_offset
5704 + (eh
->datalabel_got
.offset
&~ (bfd_vma
) 1));
5706 /* If this is a static link, or it is a -Bsymbolic link and the
5707 symbol is defined locally or was forced to be local because
5708 of a version file, we just want to emit a RELATIVE reloc.
5709 The entry in the global offset table will already have been
5710 initialized in the relocate_section function. */
5712 && SYMBOL_REFERENCES_LOCAL (info
, h
))
5714 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
5715 rel
.r_addend
= (h
->root
.u
.def
.value
5716 + h
->root
.u
.def
.section
->output_section
->vma
5717 + h
->root
.u
.def
.section
->output_offset
);
5721 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
5722 + eh
->datalabel_got
.offset
);
5723 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
5727 loc
= srel
->contents
;
5728 loc
+= srel
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5729 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5737 Elf_Internal_Rela rel
;
5740 /* This symbol needs a copy reloc. Set it up. */
5742 BFD_ASSERT (h
->dynindx
!= -1
5743 && (h
->root
.type
== bfd_link_hash_defined
5744 || h
->root
.type
== bfd_link_hash_defweak
));
5746 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
5748 BFD_ASSERT (s
!= NULL
);
5750 rel
.r_offset
= (h
->root
.u
.def
.value
5751 + h
->root
.u
.def
.section
->output_section
->vma
5752 + h
->root
.u
.def
.section
->output_offset
);
5753 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
5755 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5756 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5759 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
5760 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
5762 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
5763 || (!htab
->vxworks_p
&& h
== htab
->root
.hgot
))
5764 sym
->st_shndx
= SHN_ABS
;
5769 /* Finish up the dynamic sections. */
5772 sh_elf_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
5774 struct elf_sh_link_hash_table
*htab
;
5778 htab
= sh_elf_hash_table (info
);
5779 sgot
= htab
->sgotplt
;
5780 sdyn
= bfd_get_section_by_name (htab
->root
.dynobj
, ".dynamic");
5782 if (htab
->root
.dynamic_sections_created
)
5785 Elf32_External_Dyn
*dyncon
, *dynconend
;
5787 BFD_ASSERT (sgot
!= NULL
&& sdyn
!= NULL
);
5789 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5790 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
5791 for (; dyncon
< dynconend
; dyncon
++)
5793 Elf_Internal_Dyn dyn
;
5795 #ifdef INCLUDE_SHMEDIA
5799 bfd_elf32_swap_dyn_in (htab
->root
.dynobj
, dyncon
, &dyn
);
5805 && elf_vxworks_finish_dynamic_entry (output_bfd
, &dyn
))
5806 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5809 #ifdef INCLUDE_SHMEDIA
5811 name
= info
->init_function
;
5815 name
= info
->fini_function
;
5817 if (dyn
.d_un
.d_val
!= 0)
5819 struct elf_link_hash_entry
*h
;
5821 h
= elf_link_hash_lookup (&htab
->root
, name
,
5822 FALSE
, FALSE
, TRUE
);
5823 if (h
!= NULL
&& (h
->other
& STO_SH5_ISA32
))
5825 dyn
.d_un
.d_val
|= 1;
5826 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5833 s
= htab
->sgot
->output_section
;
5837 s
= htab
->srelplt
->output_section
;
5839 BFD_ASSERT (s
!= NULL
);
5840 dyn
.d_un
.d_ptr
= s
->vma
;
5841 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5845 s
= htab
->srelplt
->output_section
;
5846 BFD_ASSERT (s
!= NULL
);
5847 dyn
.d_un
.d_val
= s
->size
;
5848 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5852 /* My reading of the SVR4 ABI indicates that the
5853 procedure linkage table relocs (DT_JMPREL) should be
5854 included in the overall relocs (DT_RELA). This is
5855 what Solaris does. However, UnixWare can not handle
5856 that case. Therefore, we override the DT_RELASZ entry
5857 here to make it not include the JMPREL relocs. Since
5858 the linker script arranges for .rela.plt to follow all
5859 other relocation sections, we don't have to worry
5860 about changing the DT_RELA entry. */
5861 if (htab
->srelplt
!= NULL
)
5863 s
= htab
->srelplt
->output_section
;
5864 dyn
.d_un
.d_val
-= s
->size
;
5866 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5871 /* Fill in the first entry in the procedure linkage table. */
5873 if (splt
&& splt
->size
> 0 && htab
->plt_info
->plt0_entry
)
5877 memcpy (splt
->contents
,
5878 htab
->plt_info
->plt0_entry
,
5879 htab
->plt_info
->plt0_entry_size
);
5880 for (i
= 0; i
< ARRAY_SIZE (htab
->plt_info
->plt0_got_fields
); i
++)
5881 if (htab
->plt_info
->plt0_got_fields
[i
] != MINUS_ONE
)
5882 install_plt_field (output_bfd
, FALSE
,
5883 (sgot
->output_section
->vma
5884 + sgot
->output_offset
5887 + htab
->plt_info
->plt0_got_fields
[i
]));
5889 if (htab
->vxworks_p
)
5891 /* Finalize the .rela.plt.unloaded contents. */
5892 Elf_Internal_Rela rel
;
5895 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
5896 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
5897 loc
= htab
->srelplt2
->contents
;
5898 rel
.r_offset
= (splt
->output_section
->vma
5899 + splt
->output_offset
5900 + htab
->plt_info
->plt0_got_fields
[2]);
5901 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
5903 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5904 loc
+= sizeof (Elf32_External_Rela
);
5906 /* Fix up the remaining .rela.plt.unloaded relocations.
5907 They may have the wrong symbol index for _G_O_T_ or
5908 _P_L_T_ depending on the order in which symbols were
5910 while (loc
< htab
->srelplt2
->contents
+ htab
->srelplt2
->size
)
5912 /* The PLT entry's pointer to the .got.plt slot. */
5913 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
5914 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
,
5916 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5917 loc
+= sizeof (Elf32_External_Rela
);
5919 /* The .got.plt slot's pointer to .plt. */
5920 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
5921 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
,
5923 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5924 loc
+= sizeof (Elf32_External_Rela
);
5928 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5929 really seem like the right value. */
5930 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
5934 /* Fill in the first three entries in the global offset table. */
5935 if (sgot
&& sgot
->size
> 0)
5938 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
5940 bfd_put_32 (output_bfd
,
5941 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
5943 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
5944 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
5946 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
5952 static enum elf_reloc_type_class
5953 sh_elf_reloc_type_class (const Elf_Internal_Rela
*rela
)
5955 switch ((int) ELF32_R_TYPE (rela
->r_info
))
5958 return reloc_class_relative
;
5960 return reloc_class_plt
;
5962 return reloc_class_copy
;
5964 return reloc_class_normal
;
5968 #if !defined SH_TARGET_ALREADY_DEFINED
5969 /* Support for Linux core dump NOTE sections. */
5972 elf32_shlin_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5977 switch (note
->descsz
)
5982 case 168: /* Linux/SH */
5984 elf_tdata (abfd
)->core_signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
5987 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
5996 /* Make a ".reg/999" section. */
5997 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5998 size
, note
->descpos
+ offset
);
6002 elf32_shlin_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6004 switch (note
->descsz
)
6009 case 124: /* Linux/SH elf_prpsinfo */
6010 elf_tdata (abfd
)->core_program
6011 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
6012 elf_tdata (abfd
)->core_command
6013 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
6016 /* Note that for some reason, a spurious space is tacked
6017 onto the end of the args in some (at least one anyway)
6018 implementations, so strip it off if it exists. */
6021 char *command
= elf_tdata (abfd
)->core_command
;
6022 int n
= strlen (command
);
6024 if (0 < n
&& command
[n
- 1] == ' ')
6025 command
[n
- 1] = '\0';
6030 #endif /* not SH_TARGET_ALREADY_DEFINED */
6033 /* Return address for Ith PLT stub in section PLT, for relocation REL
6034 or (bfd_vma) -1 if it should not be included. */
6037 sh_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
6038 const arelent
*rel ATTRIBUTE_UNUSED
)
6040 const struct elf_sh_plt_info
*plt_info
;
6042 plt_info
= get_plt_info (plt
->owner
, (plt
->owner
->flags
& DYNAMIC
) != 0);
6043 return plt
->vma
+ get_plt_offset (plt_info
, i
);
6046 #if !defined SH_TARGET_ALREADY_DEFINED
6047 #define TARGET_BIG_SYM bfd_elf32_sh_vec
6048 #define TARGET_BIG_NAME "elf32-sh"
6049 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
6050 #define TARGET_LITTLE_NAME "elf32-shl"
6053 #define ELF_ARCH bfd_arch_sh
6054 #define ELF_MACHINE_CODE EM_SH
6055 #ifdef __QNXTARGET__
6056 #define ELF_MAXPAGESIZE 0x1000
6058 #define ELF_MAXPAGESIZE 0x80
6061 #define elf_symbol_leading_char '_'
6063 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6064 #define bfd_elf32_bfd_reloc_name_lookup \
6065 sh_elf_reloc_name_lookup
6066 #define elf_info_to_howto sh_elf_info_to_howto
6067 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
6068 #define elf_backend_relocate_section sh_elf_relocate_section
6069 #define bfd_elf32_bfd_get_relocated_section_contents \
6070 sh_elf_get_relocated_section_contents
6071 #define bfd_elf32_mkobject sh_elf_mkobject
6072 #define elf_backend_object_p sh_elf_object_p
6073 #define bfd_elf32_bfd_set_private_bfd_flags \
6074 sh_elf_set_private_flags
6075 #define bfd_elf32_bfd_copy_private_bfd_data \
6076 sh_elf_copy_private_data
6077 #define bfd_elf32_bfd_merge_private_bfd_data \
6078 sh_elf_merge_private_data
6080 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6081 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
6082 #define elf_backend_check_relocs sh_elf_check_relocs
6083 #define elf_backend_copy_indirect_symbol \
6084 sh_elf_copy_indirect_symbol
6085 #define elf_backend_create_dynamic_sections \
6086 sh_elf_create_dynamic_sections
6087 #define bfd_elf32_bfd_link_hash_table_create \
6088 sh_elf_link_hash_table_create
6089 #define elf_backend_adjust_dynamic_symbol \
6090 sh_elf_adjust_dynamic_symbol
6091 #define elf_backend_always_size_sections \
6092 sh_elf_always_size_sections
6093 #define elf_backend_size_dynamic_sections \
6094 sh_elf_size_dynamic_sections
6095 #define elf_backend_omit_section_dynsym \
6096 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
6097 #define elf_backend_finish_dynamic_symbol \
6098 sh_elf_finish_dynamic_symbol
6099 #define elf_backend_finish_dynamic_sections \
6100 sh_elf_finish_dynamic_sections
6101 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
6102 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
6104 #define elf_backend_can_gc_sections 1
6105 #define elf_backend_can_refcount 1
6106 #define elf_backend_want_got_plt 1
6107 #define elf_backend_plt_readonly 1
6108 #define elf_backend_want_plt_sym 0
6109 #define elf_backend_got_header_size 12
6111 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
6113 #include "elf32-target.h"
6115 /* NetBSD support. */
6116 #undef TARGET_BIG_SYM
6117 #define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
6118 #undef TARGET_BIG_NAME
6119 #define TARGET_BIG_NAME "elf32-sh-nbsd"
6120 #undef TARGET_LITTLE_SYM
6121 #define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
6122 #undef TARGET_LITTLE_NAME
6123 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6124 #undef ELF_MAXPAGESIZE
6125 #define ELF_MAXPAGESIZE 0x10000
6126 #undef ELF_COMMONPAGESIZE
6127 #undef elf_symbol_leading_char
6128 #define elf_symbol_leading_char 0
6130 #define elf32_bed elf32_sh_nbsd_bed
6132 #include "elf32-target.h"
6135 /* Linux support. */
6136 #undef TARGET_BIG_SYM
6137 #define TARGET_BIG_SYM bfd_elf32_shblin_vec
6138 #undef TARGET_BIG_NAME
6139 #define TARGET_BIG_NAME "elf32-shbig-linux"
6140 #undef TARGET_LITTLE_SYM
6141 #define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
6142 #undef TARGET_LITTLE_NAME
6143 #define TARGET_LITTLE_NAME "elf32-sh-linux"
6144 #undef ELF_COMMONPAGESIZE
6145 #define ELF_COMMONPAGESIZE 0x1000
6147 #undef elf_backend_grok_prstatus
6148 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6149 #undef elf_backend_grok_psinfo
6150 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
6152 #define elf32_bed elf32_sh_lin_bed
6154 #include "elf32-target.h"
6156 #undef TARGET_BIG_SYM
6157 #define TARGET_BIG_SYM bfd_elf32_shvxworks_vec
6158 #undef TARGET_BIG_NAME
6159 #define TARGET_BIG_NAME "elf32-sh-vxworks"
6160 #undef TARGET_LITTLE_SYM
6161 #define TARGET_LITTLE_SYM bfd_elf32_shlvxworks_vec
6162 #undef TARGET_LITTLE_NAME
6163 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6165 #define elf32_bed elf32_sh_vxworks_bed
6167 #undef elf_backend_want_plt_sym
6168 #define elf_backend_want_plt_sym 1
6169 #undef elf_symbol_leading_char
6170 #define elf_symbol_leading_char '_'
6171 #define elf_backend_want_got_underscore 1
6172 #undef elf_backend_grok_prstatus
6173 #undef elf_backend_grok_psinfo
6174 #undef elf_backend_add_symbol_hook
6175 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6176 #undef elf_backend_link_output_symbol_hook
6177 #define elf_backend_link_output_symbol_hook \
6178 elf_vxworks_link_output_symbol_hook
6179 #undef elf_backend_emit_relocs
6180 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6181 #undef elf_backend_final_write_processing
6182 #define elf_backend_final_write_processing \
6183 elf_vxworks_final_write_processing
6184 #undef ELF_MAXPAGESIZE
6185 #define ELF_MAXPAGESIZE 0x1000
6186 #undef ELF_COMMONPAGESIZE
6188 #include "elf32-target.h"
6190 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */