1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006 Free Software Foundation, Inc.
4 Contributed by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
27 #include "elf-vxworks.h"
29 #include "libiberty.h"
30 #include "../opcodes/sh-opc.h"
32 static bfd_reloc_status_type sh_elf_reloc
33 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
34 static bfd_reloc_status_type sh_elf_ignore_reloc
35 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
36 static bfd_boolean sh_elf_relax_delete_bytes
37 (bfd
*, asection
*, bfd_vma
, int);
38 static bfd_boolean sh_elf_align_loads
39 (bfd
*, asection
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_boolean
*);
41 static bfd_boolean sh_elf_swap_insns
42 (bfd
*, asection
*, void *, bfd_byte
*, bfd_vma
);
44 static int sh_elf_optimized_tls_reloc
45 (struct bfd_link_info
*, int, int);
46 static bfd_vma dtpoff_base
47 (struct bfd_link_info
*);
49 (struct bfd_link_info
*, bfd_vma
);
51 /* The name of the dynamic interpreter. This is put in the .interp
54 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
56 #define MINUS_ONE ((bfd_vma) 0 - 1)
58 #define SH_PARTIAL32 TRUE
59 #define SH_SRC_MASK32 0xffffffff
60 #define SH_ELF_RELOC sh_elf_reloc
61 static reloc_howto_type sh_elf_howto_table
[] =
63 #include "elf32-sh-relocs.h"
66 #define SH_PARTIAL32 FALSE
67 #define SH_SRC_MASK32 0
68 #define SH_ELF_RELOC bfd_elf_generic_reloc
69 static reloc_howto_type sh_vxworks_howto_table
[] =
71 #include "elf32-sh-relocs.h"
74 /* Return true if OUTPUT_BFD is a VxWorks object. */
77 vxworks_object_p (bfd
*abfd
)
79 extern const bfd_target bfd_elf32_shlvxworks_vec
;
80 extern const bfd_target bfd_elf32_shvxworks_vec
;
82 return (abfd
->xvec
== &bfd_elf32_shlvxworks_vec
83 || abfd
->xvec
== &bfd_elf32_shvxworks_vec
);
86 /* Return the howto table for ABFD. */
88 static reloc_howto_type
*
89 get_howto_table (bfd
*abfd
)
91 if (vxworks_object_p (abfd
))
92 return sh_vxworks_howto_table
;
93 return sh_elf_howto_table
;
96 static bfd_reloc_status_type
97 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
98 asection
*input_section
, bfd_byte
*contents
,
99 bfd_vma addr
, asection
*symbol_section
,
100 bfd_vma start
, bfd_vma end
)
102 static bfd_vma last_addr
;
103 static asection
*last_symbol_section
;
104 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
109 /* Sanity check the address. */
110 if (addr
> bfd_get_section_limit (input_bfd
, input_section
))
111 return bfd_reloc_outofrange
;
113 /* We require the start and end relocations to be processed consecutively -
114 although we allow then to be processed forwards or backwards. */
118 last_symbol_section
= symbol_section
;
121 if (last_addr
!= addr
)
125 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
126 return bfd_reloc_outofrange
;
128 /* Get the symbol_section contents. */
129 if (symbol_section
!= input_section
)
131 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
132 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
135 if (!bfd_malloc_and_get_section (input_bfd
, symbol_section
,
138 if (contents
!= NULL
)
140 return bfd_reloc_outofrange
;
144 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
145 start_ptr
= contents
+ start
;
146 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
148 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
151 diff
= (last_ptr
- ptr
) >> 1;
152 cum_diff
+= diff
& 1;
155 /* Calculate the start / end values to load into rs / re minus four -
156 so that will cancel out the four we would otherwise have to add to
157 addr to get the value to subtract in order to get relative addressing. */
161 end
= (ptr
+ cum_diff
* 2) - contents
;
165 bfd_vma start0
= start
- 4;
167 while (start0
&& IS_PPI (contents
+ start0
))
169 start0
= start
- 2 - ((start
- start0
) & 2);
170 start
= start0
- cum_diff
- 2;
175 && elf_section_data (symbol_section
)->this_hdr
.contents
!= contents
)
178 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
180 x
= (insn
& 0x200 ? end
: start
) - addr
;
181 if (input_section
!= symbol_section
)
182 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
183 - (input_section
->output_section
->vma
184 + input_section
->output_offset
));
186 if (x
< -128 || x
> 127)
187 return bfd_reloc_overflow
;
189 x
= (insn
& ~0xff) | (x
& 0xff);
190 bfd_put_16 (input_bfd
, (bfd_vma
) x
, contents
+ addr
);
195 /* This function is used for normal relocs. This used to be like the COFF
196 function, and is almost certainly incorrect for other ELF targets. */
198 static bfd_reloc_status_type
199 sh_elf_reloc (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol_in
,
200 void *data
, asection
*input_section
, bfd
*output_bfd
,
201 char **error_message ATTRIBUTE_UNUSED
)
205 enum elf_sh_reloc_type r_type
;
206 bfd_vma addr
= reloc_entry
->address
;
207 bfd_byte
*hit_data
= addr
+ (bfd_byte
*) data
;
209 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
211 if (output_bfd
!= NULL
)
213 /* Partial linking--do nothing. */
214 reloc_entry
->address
+= input_section
->output_offset
;
218 /* Almost all relocs have to do with relaxing. If any work must be
219 done for them, it has been done in sh_relax_section. */
220 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
223 if (symbol_in
!= NULL
224 && bfd_is_und_section (symbol_in
->section
))
225 return bfd_reloc_undefined
;
227 if (bfd_is_com_section (symbol_in
->section
))
230 sym_value
= (symbol_in
->value
+
231 symbol_in
->section
->output_section
->vma
+
232 symbol_in
->section
->output_offset
);
237 insn
= bfd_get_32 (abfd
, hit_data
);
238 insn
+= sym_value
+ reloc_entry
->addend
;
239 bfd_put_32 (abfd
, (bfd_vma
) insn
, hit_data
);
242 insn
= bfd_get_16 (abfd
, hit_data
);
243 sym_value
+= reloc_entry
->addend
;
244 sym_value
-= (input_section
->output_section
->vma
245 + input_section
->output_offset
248 sym_value
+= (insn
& 0xfff) << 1;
251 insn
= (insn
& 0xf000) | (sym_value
& 0xfff);
252 bfd_put_16 (abfd
, (bfd_vma
) insn
, hit_data
);
253 if (sym_value
< (bfd_vma
) -0x1000 || sym_value
>= 0x1000)
254 return bfd_reloc_overflow
;
264 /* This function is used for relocs which are only used for relaxing,
265 which the linker should otherwise ignore. */
267 static bfd_reloc_status_type
268 sh_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
269 asymbol
*symbol ATTRIBUTE_UNUSED
,
270 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
272 char **error_message ATTRIBUTE_UNUSED
)
274 if (output_bfd
!= NULL
)
275 reloc_entry
->address
+= input_section
->output_offset
;
279 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
283 bfd_reloc_code_real_type bfd_reloc_val
;
284 unsigned char elf_reloc_val
;
287 /* An array mapping BFD reloc codes to SH ELF relocs. */
289 static const struct elf_reloc_map sh_reloc_map
[] =
291 { BFD_RELOC_NONE
, R_SH_NONE
},
292 { BFD_RELOC_32
, R_SH_DIR32
},
293 { BFD_RELOC_16
, R_SH_DIR16
},
294 { BFD_RELOC_8
, R_SH_DIR8
},
295 { BFD_RELOC_CTOR
, R_SH_DIR32
},
296 { BFD_RELOC_32_PCREL
, R_SH_REL32
},
297 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_DIR8WPN
},
298 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_IND12W
},
299 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_DIR8WPZ
},
300 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_DIR8WPL
},
301 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
302 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
303 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
304 { BFD_RELOC_SH_USES
, R_SH_USES
},
305 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
306 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
307 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
308 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
309 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
310 { BFD_RELOC_VTABLE_INHERIT
, R_SH_GNU_VTINHERIT
},
311 { BFD_RELOC_VTABLE_ENTRY
, R_SH_GNU_VTENTRY
},
312 { BFD_RELOC_SH_LOOP_START
, R_SH_LOOP_START
},
313 { BFD_RELOC_SH_LOOP_END
, R_SH_LOOP_END
},
314 { BFD_RELOC_SH_TLS_GD_32
, R_SH_TLS_GD_32
},
315 { BFD_RELOC_SH_TLS_LD_32
, R_SH_TLS_LD_32
},
316 { BFD_RELOC_SH_TLS_LDO_32
, R_SH_TLS_LDO_32
},
317 { BFD_RELOC_SH_TLS_IE_32
, R_SH_TLS_IE_32
},
318 { BFD_RELOC_SH_TLS_LE_32
, R_SH_TLS_LE_32
},
319 { BFD_RELOC_SH_TLS_DTPMOD32
, R_SH_TLS_DTPMOD32
},
320 { BFD_RELOC_SH_TLS_DTPOFF32
, R_SH_TLS_DTPOFF32
},
321 { BFD_RELOC_SH_TLS_TPOFF32
, R_SH_TLS_TPOFF32
},
322 { BFD_RELOC_32_GOT_PCREL
, R_SH_GOT32
},
323 { BFD_RELOC_32_PLT_PCREL
, R_SH_PLT32
},
324 { BFD_RELOC_SH_COPY
, R_SH_COPY
},
325 { BFD_RELOC_SH_GLOB_DAT
, R_SH_GLOB_DAT
},
326 { BFD_RELOC_SH_JMP_SLOT
, R_SH_JMP_SLOT
},
327 { BFD_RELOC_SH_RELATIVE
, R_SH_RELATIVE
},
328 { BFD_RELOC_32_GOTOFF
, R_SH_GOTOFF
},
329 { BFD_RELOC_SH_GOTPC
, R_SH_GOTPC
},
330 { BFD_RELOC_SH_GOTPLT32
, R_SH_GOTPLT32
},
331 #ifdef INCLUDE_SHMEDIA
332 { BFD_RELOC_SH_GOT_LOW16
, R_SH_GOT_LOW16
},
333 { BFD_RELOC_SH_GOT_MEDLOW16
, R_SH_GOT_MEDLOW16
},
334 { BFD_RELOC_SH_GOT_MEDHI16
, R_SH_GOT_MEDHI16
},
335 { BFD_RELOC_SH_GOT_HI16
, R_SH_GOT_HI16
},
336 { BFD_RELOC_SH_GOTPLT_LOW16
, R_SH_GOTPLT_LOW16
},
337 { BFD_RELOC_SH_GOTPLT_MEDLOW16
, R_SH_GOTPLT_MEDLOW16
},
338 { BFD_RELOC_SH_GOTPLT_MEDHI16
, R_SH_GOTPLT_MEDHI16
},
339 { BFD_RELOC_SH_GOTPLT_HI16
, R_SH_GOTPLT_HI16
},
340 { BFD_RELOC_SH_PLT_LOW16
, R_SH_PLT_LOW16
},
341 { BFD_RELOC_SH_PLT_MEDLOW16
, R_SH_PLT_MEDLOW16
},
342 { BFD_RELOC_SH_PLT_MEDHI16
, R_SH_PLT_MEDHI16
},
343 { BFD_RELOC_SH_PLT_HI16
, R_SH_PLT_HI16
},
344 { BFD_RELOC_SH_GOTOFF_LOW16
, R_SH_GOTOFF_LOW16
},
345 { BFD_RELOC_SH_GOTOFF_MEDLOW16
, R_SH_GOTOFF_MEDLOW16
},
346 { BFD_RELOC_SH_GOTOFF_MEDHI16
, R_SH_GOTOFF_MEDHI16
},
347 { BFD_RELOC_SH_GOTOFF_HI16
, R_SH_GOTOFF_HI16
},
348 { BFD_RELOC_SH_GOTPC_LOW16
, R_SH_GOTPC_LOW16
},
349 { BFD_RELOC_SH_GOTPC_MEDLOW16
, R_SH_GOTPC_MEDLOW16
},
350 { BFD_RELOC_SH_GOTPC_MEDHI16
, R_SH_GOTPC_MEDHI16
},
351 { BFD_RELOC_SH_GOTPC_HI16
, R_SH_GOTPC_HI16
},
352 { BFD_RELOC_SH_COPY64
, R_SH_COPY64
},
353 { BFD_RELOC_SH_GLOB_DAT64
, R_SH_GLOB_DAT64
},
354 { BFD_RELOC_SH_JMP_SLOT64
, R_SH_JMP_SLOT64
},
355 { BFD_RELOC_SH_RELATIVE64
, R_SH_RELATIVE64
},
356 { BFD_RELOC_SH_GOT10BY4
, R_SH_GOT10BY4
},
357 { BFD_RELOC_SH_GOT10BY8
, R_SH_GOT10BY8
},
358 { BFD_RELOC_SH_GOTPLT10BY4
, R_SH_GOTPLT10BY4
},
359 { BFD_RELOC_SH_GOTPLT10BY8
, R_SH_GOTPLT10BY8
},
360 { BFD_RELOC_SH_PT_16
, R_SH_PT_16
},
361 { BFD_RELOC_SH_SHMEDIA_CODE
, R_SH_SHMEDIA_CODE
},
362 { BFD_RELOC_SH_IMMU5
, R_SH_DIR5U
},
363 { BFD_RELOC_SH_IMMS6
, R_SH_DIR6S
},
364 { BFD_RELOC_SH_IMMU6
, R_SH_DIR6U
},
365 { BFD_RELOC_SH_IMMS10
, R_SH_DIR10S
},
366 { BFD_RELOC_SH_IMMS10BY2
, R_SH_DIR10SW
},
367 { BFD_RELOC_SH_IMMS10BY4
, R_SH_DIR10SL
},
368 { BFD_RELOC_SH_IMMS10BY8
, R_SH_DIR10SQ
},
369 { BFD_RELOC_SH_IMMS16
, R_SH_IMMS16
},
370 { BFD_RELOC_SH_IMMU16
, R_SH_IMMU16
},
371 { BFD_RELOC_SH_IMM_LOW16
, R_SH_IMM_LOW16
},
372 { BFD_RELOC_SH_IMM_LOW16_PCREL
, R_SH_IMM_LOW16_PCREL
},
373 { BFD_RELOC_SH_IMM_MEDLOW16
, R_SH_IMM_MEDLOW16
},
374 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL
, R_SH_IMM_MEDLOW16_PCREL
},
375 { BFD_RELOC_SH_IMM_MEDHI16
, R_SH_IMM_MEDHI16
},
376 { BFD_RELOC_SH_IMM_MEDHI16_PCREL
, R_SH_IMM_MEDHI16_PCREL
},
377 { BFD_RELOC_SH_IMM_HI16
, R_SH_IMM_HI16
},
378 { BFD_RELOC_SH_IMM_HI16_PCREL
, R_SH_IMM_HI16_PCREL
},
379 { BFD_RELOC_64
, R_SH_64
},
380 { BFD_RELOC_64_PCREL
, R_SH_64_PCREL
},
381 #endif /* not INCLUDE_SHMEDIA */
384 /* Given a BFD reloc code, return the howto structure for the
385 corresponding SH ELF reloc. */
387 static reloc_howto_type
*
388 sh_elf_reloc_type_lookup (bfd
*abfd
, bfd_reloc_code_real_type code
)
392 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
394 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
395 return get_howto_table (abfd
) + (int) sh_reloc_map
[i
].elf_reloc_val
;
401 /* Given an ELF reloc, fill in the howto field of a relent. */
404 sh_elf_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
, Elf_Internal_Rela
*dst
)
408 r
= ELF32_R_TYPE (dst
->r_info
);
410 BFD_ASSERT (r
< (unsigned int) R_SH_max
);
411 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC
|| r
> R_SH_LAST_INVALID_RELOC
);
412 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_2
|| r
> R_SH_LAST_INVALID_RELOC_2
);
413 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_3
|| r
> R_SH_LAST_INVALID_RELOC_3
);
414 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_4
|| r
> R_SH_LAST_INVALID_RELOC_4
);
415 BFD_ASSERT (r
< R_SH_FIRST_INVALID_RELOC_5
|| r
> R_SH_LAST_INVALID_RELOC_5
);
417 cache_ptr
->howto
= get_howto_table (abfd
) + r
;
420 /* This function handles relaxing for SH ELF. See the corresponding
421 function in coff-sh.c for a description of what this does. FIXME:
422 There is a lot of duplication here between this code and the COFF
423 specific code. The format of relocs and symbols is wound deeply
424 into this code, but it would still be better if the duplication
425 could be eliminated somehow. Note in particular that although both
426 functions use symbols like R_SH_CODE, those symbols have different
427 values; in coff-sh.c they come from include/coff/sh.h, whereas here
428 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
431 sh_elf_relax_section (bfd
*abfd
, asection
*sec
,
432 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
434 Elf_Internal_Shdr
*symtab_hdr
;
435 Elf_Internal_Rela
*internal_relocs
;
436 bfd_boolean have_code
;
437 Elf_Internal_Rela
*irel
, *irelend
;
438 bfd_byte
*contents
= NULL
;
439 Elf_Internal_Sym
*isymbuf
= NULL
;
443 if (link_info
->relocatable
444 || (sec
->flags
& SEC_RELOC
) == 0
445 || sec
->reloc_count
== 0)
448 #ifdef INCLUDE_SHMEDIA
449 if (elf_section_data (sec
)->this_hdr
.sh_flags
450 & (SHF_SH5_ISA32
| SHF_SH5_ISA32_MIXED
))
456 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
458 internal_relocs
= (_bfd_elf_link_read_relocs
459 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
460 link_info
->keep_memory
));
461 if (internal_relocs
== NULL
)
466 irelend
= internal_relocs
+ sec
->reloc_count
;
467 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
469 bfd_vma laddr
, paddr
, symval
;
471 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
474 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
477 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
480 /* Get the section contents. */
481 if (contents
== NULL
)
483 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
484 contents
= elf_section_data (sec
)->this_hdr
.contents
;
487 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
492 /* The r_addend field of the R_SH_USES reloc will point us to
493 the register load. The 4 is because the r_addend field is
494 computed as though it were a jump offset, which are based
495 from 4 bytes after the jump instruction. */
496 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
497 if (laddr
>= sec
->size
)
499 (*_bfd_error_handler
) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
501 (unsigned long) irel
->r_offset
);
504 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
506 /* If the instruction is not mov.l NN,rN, we don't know what to
508 if ((insn
& 0xf000) != 0xd000)
510 ((*_bfd_error_handler
)
511 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
512 abfd
, (unsigned long) irel
->r_offset
, insn
));
516 /* Get the address from which the register is being loaded. The
517 displacement in the mov.l instruction is quadrupled. It is a
518 displacement from four bytes after the movl instruction, but,
519 before adding in the PC address, two least significant bits
520 of the PC are cleared. We assume that the section is aligned
521 on a four byte boundary. */
524 paddr
+= (laddr
+ 4) &~ (bfd_vma
) 3;
525 if (paddr
>= sec
->size
)
527 ((*_bfd_error_handler
)
528 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
529 abfd
, (unsigned long) irel
->r_offset
));
533 /* Get the reloc for the address from which the register is
534 being loaded. This reloc will tell us which function is
535 actually being called. */
536 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
537 if (irelfn
->r_offset
== paddr
538 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
540 if (irelfn
>= irelend
)
542 ((*_bfd_error_handler
)
543 (_("%B: 0x%lx: warning: could not find expected reloc"),
544 abfd
, (unsigned long) paddr
));
548 /* Read this BFD's symbols if we haven't done so already. */
549 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
551 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
553 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
554 symtab_hdr
->sh_info
, 0,
560 /* Get the value of the symbol referred to by the reloc. */
561 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
563 /* A local symbol. */
564 Elf_Internal_Sym
*isym
;
566 isym
= isymbuf
+ ELF32_R_SYM (irelfn
->r_info
);
568 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd
, sec
))
570 ((*_bfd_error_handler
)
571 (_("%B: 0x%lx: warning: symbol in unexpected section"),
572 abfd
, (unsigned long) paddr
));
576 symval
= (isym
->st_value
577 + sec
->output_section
->vma
578 + sec
->output_offset
);
583 struct elf_link_hash_entry
*h
;
585 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
586 h
= elf_sym_hashes (abfd
)[indx
];
587 BFD_ASSERT (h
!= NULL
);
588 if (h
->root
.type
!= bfd_link_hash_defined
589 && h
->root
.type
!= bfd_link_hash_defweak
)
591 /* This appears to be a reference to an undefined
592 symbol. Just ignore it--it will be caught by the
593 regular reloc processing. */
597 symval
= (h
->root
.u
.def
.value
598 + h
->root
.u
.def
.section
->output_section
->vma
599 + h
->root
.u
.def
.section
->output_offset
);
602 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
603 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
605 symval
+= irelfn
->r_addend
;
607 /* See if this function call can be shortened. */
610 + sec
->output_section
->vma
613 if (foff
< -0x1000 || foff
>= 0x1000)
615 /* After all that work, we can't shorten this function call. */
619 /* Shorten the function call. */
621 /* For simplicity of coding, we are going to modify the section
622 contents, the section relocs, and the BFD symbol table. We
623 must tell the rest of the code not to free up this
624 information. It would be possible to instead create a table
625 of changes which have to be made, as is done in coff-mips.c;
626 that would be more work, but would require less memory when
627 the linker is run. */
629 elf_section_data (sec
)->relocs
= internal_relocs
;
630 elf_section_data (sec
)->this_hdr
.contents
= contents
;
631 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
633 /* Replace the jsr with a bsr. */
635 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
636 replace the jsr with a bsr. */
637 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
638 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
639 here, but that only checks if the symbol is an external symbol,
640 not if the symbol is in a different section. Besides, we need
641 a consistent meaning for the relocation, so we just assume here that
642 the value of the symbol is not available. */
644 /* We can't fully resolve this yet, because the external
645 symbol value may be changed by future relaxing. We let
646 the final link phase handle it. */
647 bfd_put_16 (abfd
, (bfd_vma
) 0xb000, contents
+ irel
->r_offset
);
651 /* See if there is another R_SH_USES reloc referring to the same
653 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
654 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
655 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
657 if (irelscan
< irelend
)
659 /* Some other function call depends upon this register load,
660 and we have not yet converted that function call.
661 Indeed, we may never be able to convert it. There is
662 nothing else we can do at this point. */
666 /* Look for a R_SH_COUNT reloc on the location where the
667 function address is stored. Do this before deleting any
668 bytes, to avoid confusion about the address. */
669 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
670 if (irelcount
->r_offset
== paddr
671 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
674 /* Delete the register load. */
675 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
678 /* That will change things, so, just in case it permits some
679 other function call to come within range, we should relax
680 again. Note that this is not required, and it may be slow. */
683 /* Now check whether we got a COUNT reloc. */
684 if (irelcount
>= irelend
)
686 ((*_bfd_error_handler
)
687 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
688 abfd
, (unsigned long) paddr
));
692 /* The number of uses is stored in the r_addend field. We've
694 if (irelcount
->r_addend
== 0)
696 ((*_bfd_error_handler
) (_("%B: 0x%lx: warning: bad count"),
698 (unsigned long) paddr
));
702 --irelcount
->r_addend
;
704 /* If there are no more uses, we can delete the address. Reload
705 the address from irelfn, in case it was changed by the
706 previous call to sh_elf_relax_delete_bytes. */
707 if (irelcount
->r_addend
== 0)
709 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
713 /* We've done all we can with that function call. */
716 /* Look for load and store instructions that we can align on four
718 if ((elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
) != EF_SH4
723 /* Get the section contents. */
724 if (contents
== NULL
)
726 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
727 contents
= elf_section_data (sec
)->this_hdr
.contents
;
730 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
735 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
741 elf_section_data (sec
)->relocs
= internal_relocs
;
742 elf_section_data (sec
)->this_hdr
.contents
= contents
;
743 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
748 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
750 if (! link_info
->keep_memory
)
754 /* Cache the symbols for elf_link_input_bfd. */
755 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
760 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
762 if (! link_info
->keep_memory
)
766 /* Cache the section contents for elf_link_input_bfd. */
767 elf_section_data (sec
)->this_hdr
.contents
= contents
;
771 if (internal_relocs
!= NULL
772 && elf_section_data (sec
)->relocs
!= internal_relocs
)
773 free (internal_relocs
);
779 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
782 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
784 if (internal_relocs
!= NULL
785 && elf_section_data (sec
)->relocs
!= internal_relocs
)
786 free (internal_relocs
);
791 /* Delete some bytes from a section while relaxing. FIXME: There is a
792 lot of duplication between this function and sh_relax_delete_bytes
796 sh_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
,
799 Elf_Internal_Shdr
*symtab_hdr
;
800 unsigned int sec_shndx
;
802 Elf_Internal_Rela
*irel
, *irelend
;
803 Elf_Internal_Rela
*irelalign
;
805 Elf_Internal_Sym
*isymbuf
, *isym
, *isymend
;
806 struct elf_link_hash_entry
**sym_hashes
;
807 struct elf_link_hash_entry
**end_hashes
;
808 unsigned int symcount
;
811 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
812 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
814 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
816 contents
= elf_section_data (sec
)->this_hdr
.contents
;
818 /* The deletion must stop at the next ALIGN reloc for an aligment
819 power larger than the number of bytes we are deleting. */
824 irel
= elf_section_data (sec
)->relocs
;
825 irelend
= irel
+ sec
->reloc_count
;
826 for (; irel
< irelend
; irel
++)
828 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
829 && irel
->r_offset
> addr
830 && count
< (1 << irel
->r_addend
))
833 toaddr
= irel
->r_offset
;
838 /* Actually delete the bytes. */
839 memmove (contents
+ addr
, contents
+ addr
+ count
,
840 (size_t) (toaddr
- addr
- count
));
841 if (irelalign
== NULL
)
847 #define NOP_OPCODE (0x0009)
849 BFD_ASSERT ((count
& 1) == 0);
850 for (i
= 0; i
< count
; i
+= 2)
851 bfd_put_16 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
854 /* Adjust all the relocs. */
855 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
857 bfd_vma nraddr
, stop
;
860 int off
, adjust
, oinsn
;
861 bfd_signed_vma voff
= 0;
862 bfd_boolean overflow
;
864 /* Get the new reloc address. */
865 nraddr
= irel
->r_offset
;
866 if ((irel
->r_offset
> addr
867 && irel
->r_offset
< toaddr
)
868 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
869 && irel
->r_offset
== toaddr
))
872 /* See if this reloc was for the bytes we have deleted, in which
873 case we no longer care about it. Don't delete relocs which
874 represent addresses, though. */
875 if (irel
->r_offset
>= addr
876 && irel
->r_offset
< addr
+ count
877 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
878 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
879 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
880 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
881 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
884 /* If this is a PC relative reloc, see if the range it covers
885 includes the bytes we have deleted. */
886 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
895 start
= irel
->r_offset
;
896 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
900 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
907 /* If this reloc is against a symbol defined in this
908 section, and the symbol will not be adjusted below, we
909 must check the addend to see it will put the value in
910 range to be adjusted, and hence must be changed. */
911 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
913 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
914 if (isym
->st_shndx
== sec_shndx
915 && (isym
->st_value
<= addr
916 || isym
->st_value
>= toaddr
))
920 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
922 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
923 val
+= isym
->st_value
;
924 if (val
> addr
&& val
< toaddr
)
925 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
929 val
= isym
->st_value
+ irel
->r_addend
;
930 if (val
> addr
&& val
< toaddr
)
931 irel
->r_addend
-= count
;
942 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
949 /* This has been made by previous relaxation. Since the
950 relocation will be against an external symbol, the
951 final relocation will just do the right thing. */
958 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
960 /* The addend will be against the section symbol, thus
961 for adjusting the addend, the relevant start is the
962 start of the section.
963 N.B. If we want to abandon in-place changes here and
964 test directly using symbol + addend, we have to take into
965 account that the addend has already been adjusted by -4. */
966 if (stop
> addr
&& stop
< toaddr
)
967 irel
->r_addend
-= count
;
973 stop
= start
+ 4 + off
* 2;
978 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
984 /* These relocs types represent
986 The r_addend field holds the difference between the reloc
987 address and L1. That is the start of the reloc, and
988 adding in the contents gives us the top. We must adjust
989 both the r_offset field and the section contents.
990 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
991 and the elf bfd r_offset is called r_vaddr. */
993 stop
= irel
->r_offset
;
994 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
998 && (stop
<= addr
|| stop
>= toaddr
))
999 irel
->r_addend
+= count
;
1000 else if (stop
> addr
1002 && (start
<= addr
|| start
>= toaddr
))
1003 irel
->r_addend
-= count
;
1005 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1006 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1007 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1008 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1010 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1011 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1016 start
= irel
->r_offset
;
1017 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1018 + (long) irel
->r_addend
1025 && (stop
<= addr
|| stop
>= toaddr
))
1027 else if (stop
> addr
1029 && (start
<= addr
|| start
>= toaddr
))
1038 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1047 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1049 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1054 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1056 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1060 BFD_ASSERT (adjust
== count
|| count
>= 4);
1065 if ((irel
->r_offset
& 3) == 0)
1068 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1070 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1075 if (voff
< 0 || voff
>= 0xff)
1077 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1082 if (voff
< - 0x8000 || voff
>= 0x8000)
1084 bfd_put_signed_16 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1089 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1093 irel
->r_addend
+= adjust
;
1099 ((*_bfd_error_handler
)
1100 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1101 abfd
, (unsigned long) irel
->r_offset
));
1102 bfd_set_error (bfd_error_bad_value
);
1107 irel
->r_offset
= nraddr
;
1110 /* Look through all the other sections. If there contain any IMM32
1111 relocs against internal symbols which we are not going to adjust
1112 below, we may need to adjust the addends. */
1113 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1115 Elf_Internal_Rela
*internal_relocs
;
1116 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1117 bfd_byte
*ocontents
;
1120 || (o
->flags
& SEC_RELOC
) == 0
1121 || o
->reloc_count
== 0)
1124 /* We always cache the relocs. Perhaps, if info->keep_memory is
1125 FALSE, we should free them, if we are permitted to, when we
1126 leave sh_coff_relax_section. */
1127 internal_relocs
= (_bfd_elf_link_read_relocs
1128 (abfd
, o
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
1129 if (internal_relocs
== NULL
)
1133 irelscanend
= internal_relocs
+ o
->reloc_count
;
1134 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1136 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1137 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1139 bfd_vma start
, stop
;
1140 bfd_signed_vma voff
;
1142 if (ocontents
== NULL
)
1144 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1145 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1148 /* We always cache the section contents.
1149 Perhaps, if info->keep_memory is FALSE, we
1150 should free them, if we are permitted to,
1151 when we leave sh_coff_relax_section. */
1152 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1154 if (ocontents
!= NULL
)
1159 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1163 stop
= irelscan
->r_offset
;
1165 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1167 /* STOP is in a different section, so it won't change. */
1168 if (start
> addr
&& start
< toaddr
)
1169 irelscan
->r_addend
+= count
;
1171 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1172 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1176 && (stop
<= addr
|| stop
>= toaddr
))
1177 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
+ count
,
1178 ocontents
+ irelscan
->r_offset
);
1179 else if (stop
> addr
1181 && (start
<= addr
|| start
>= toaddr
))
1182 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
- count
,
1183 ocontents
+ irelscan
->r_offset
);
1186 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1189 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1193 isym
= isymbuf
+ ELF32_R_SYM (irelscan
->r_info
);
1194 if (isym
->st_shndx
== sec_shndx
1195 && (isym
->st_value
<= addr
1196 || isym
->st_value
>= toaddr
))
1200 if (ocontents
== NULL
)
1202 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1203 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1206 /* We always cache the section contents.
1207 Perhaps, if info->keep_memory is FALSE, we
1208 should free them, if we are permitted to,
1209 when we leave sh_coff_relax_section. */
1210 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1212 if (ocontents
!= NULL
)
1217 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1221 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1222 val
+= isym
->st_value
;
1223 if (val
> addr
&& val
< toaddr
)
1224 bfd_put_32 (abfd
, val
- count
,
1225 ocontents
+ irelscan
->r_offset
);
1230 /* Adjust the local symbols defined in this section. */
1231 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1232 for (isym
= isymbuf
; isym
< isymend
; isym
++)
1234 if (isym
->st_shndx
== sec_shndx
1235 && isym
->st_value
> addr
1236 && isym
->st_value
< toaddr
)
1237 isym
->st_value
-= count
;
1240 /* Now adjust the global symbols defined in this section. */
1241 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1242 - symtab_hdr
->sh_info
);
1243 sym_hashes
= elf_sym_hashes (abfd
);
1244 end_hashes
= sym_hashes
+ symcount
;
1245 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1247 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1248 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1249 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1250 && sym_hash
->root
.u
.def
.section
== sec
1251 && sym_hash
->root
.u
.def
.value
> addr
1252 && sym_hash
->root
.u
.def
.value
< toaddr
)
1254 sym_hash
->root
.u
.def
.value
-= count
;
1258 /* See if we can move the ALIGN reloc forward. We have adjusted
1259 r_offset for it already. */
1260 if (irelalign
!= NULL
)
1262 bfd_vma alignto
, alignaddr
;
1264 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1265 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1266 1 << irelalign
->r_addend
);
1267 if (alignto
!= alignaddr
)
1269 /* Tail recursion. */
1270 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1271 (int) (alignto
- alignaddr
));
1278 /* Look for loads and stores which we can align to four byte
1279 boundaries. This is like sh_align_loads in coff-sh.c. */
1282 sh_elf_align_loads (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1283 Elf_Internal_Rela
*internal_relocs
,
1284 bfd_byte
*contents ATTRIBUTE_UNUSED
,
1285 bfd_boolean
*pswapped
)
1287 Elf_Internal_Rela
*irel
, *irelend
;
1288 bfd_vma
*labels
= NULL
;
1289 bfd_vma
*label
, *label_end
;
1294 irelend
= internal_relocs
+ sec
->reloc_count
;
1296 /* Get all the addresses with labels on them. */
1297 amt
= sec
->reloc_count
;
1298 amt
*= sizeof (bfd_vma
);
1299 labels
= (bfd_vma
*) bfd_malloc (amt
);
1303 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1305 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1307 *label_end
= irel
->r_offset
;
1312 /* Note that the assembler currently always outputs relocs in
1313 address order. If that ever changes, this code will need to sort
1314 the label values and the relocs. */
1318 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1320 bfd_vma start
, stop
;
1322 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1325 start
= irel
->r_offset
;
1327 for (irel
++; irel
< irelend
; irel
++)
1328 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1331 stop
= irel
->r_offset
;
1335 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1336 internal_relocs
, &label
,
1337 label_end
, start
, stop
, pswapped
))
1352 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1355 sh_elf_swap_insns (bfd
*abfd
, asection
*sec
, void *relocs
,
1356 bfd_byte
*contents
, bfd_vma addr
)
1358 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1359 unsigned short i1
, i2
;
1360 Elf_Internal_Rela
*irel
, *irelend
;
1362 /* Swap the instructions themselves. */
1363 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1364 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1365 bfd_put_16 (abfd
, (bfd_vma
) i2
, contents
+ addr
);
1366 bfd_put_16 (abfd
, (bfd_vma
) i1
, contents
+ addr
+ 2);
1368 /* Adjust all reloc addresses. */
1369 irelend
= internal_relocs
+ sec
->reloc_count
;
1370 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1372 enum elf_sh_reloc_type type
;
1375 /* There are a few special types of relocs that we don't want to
1376 adjust. These relocs do not apply to the instruction itself,
1377 but are only associated with the address. */
1378 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1379 if (type
== R_SH_ALIGN
1380 || type
== R_SH_CODE
1381 || type
== R_SH_DATA
1382 || type
== R_SH_LABEL
)
1385 /* If an R_SH_USES reloc points to one of the addresses being
1386 swapped, we must adjust it. It would be incorrect to do this
1387 for a jump, though, since we want to execute both
1388 instructions after the jump. (We have avoided swapping
1389 around a label, so the jump will not wind up executing an
1390 instruction it shouldn't). */
1391 if (type
== R_SH_USES
)
1395 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
1397 irel
->r_offset
+= 2;
1398 else if (off
== addr
+ 2)
1399 irel
->r_offset
-= 2;
1402 if (irel
->r_offset
== addr
)
1404 irel
->r_offset
+= 2;
1407 else if (irel
->r_offset
== addr
+ 2)
1409 irel
->r_offset
-= 2;
1418 unsigned short insn
, oinsn
;
1419 bfd_boolean overflow
;
1421 loc
= contents
+ irel
->r_offset
;
1430 insn
= bfd_get_16 (abfd
, loc
);
1433 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1435 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1439 insn
= bfd_get_16 (abfd
, loc
);
1442 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1444 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1448 /* This reloc ignores the least significant 3 bits of
1449 the program counter before adding in the offset.
1450 This means that if ADDR is at an even address, the
1451 swap will not affect the offset. If ADDR is an at an
1452 odd address, then the instruction will be crossing a
1453 four byte boundary, and must be adjusted. */
1454 if ((addr
& 3) != 0)
1456 insn
= bfd_get_16 (abfd
, loc
);
1459 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1461 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1469 ((*_bfd_error_handler
)
1470 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1471 abfd
, (unsigned long) irel
->r_offset
));
1472 bfd_set_error (bfd_error_bad_value
);
1480 #endif /* defined SH64_ELF */
1482 /* Describes one of the various PLT styles. */
1484 struct elf_sh_plt_info
1486 /* The template for the first PLT entry, or NULL if there is no special
1488 const bfd_byte
*plt0_entry
;
1490 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1491 bfd_vma plt0_entry_size
;
1493 /* Index I is the offset into PLT0_ENTRY of a pointer to
1494 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1495 if there is no such pointer. */
1496 bfd_vma plt0_got_fields
[3];
1498 /* The template for a symbol's PLT entry. */
1499 const bfd_byte
*symbol_entry
;
1501 /* The size of SYMBOL_ENTRY in bytes. */
1502 bfd_vma symbol_entry_size
;
1504 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1505 on all targets. The comments by each member indicate the value
1506 that the field must hold. */
1508 bfd_vma got_entry
; /* the address of the symbol's .got.plt entry */
1509 bfd_vma plt
; /* .plt (or a branch to .plt on VxWorks) */
1510 bfd_vma reloc_offset
; /* the offset of the symbol's JMP_SLOT reloc */
1513 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1514 bfd_vma symbol_resolve_offset
;
1517 #ifdef INCLUDE_SHMEDIA
1519 /* The size in bytes of an entry in the procedure linkage table. */
1521 #define ELF_PLT_ENTRY_SIZE 64
1523 /* First entry in an absolute procedure linkage table look like this. */
1525 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1527 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1528 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1529 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1530 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1531 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1532 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1533 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1534 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1535 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1536 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1537 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1538 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1539 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1540 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1541 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1542 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1545 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1547 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1548 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1549 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1550 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1551 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1552 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1553 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1554 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1555 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1556 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1557 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1558 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1559 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1560 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1561 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1562 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1565 /* Sebsequent entries in an absolute procedure linkage table look like
1568 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1570 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1571 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1572 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1573 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1574 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1575 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1576 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1577 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1578 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1579 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1580 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1581 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1582 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1583 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1584 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1585 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1588 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1590 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1591 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1592 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1593 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1594 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1595 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1596 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1597 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1598 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1599 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1600 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1601 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1602 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1603 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1604 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1605 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1608 /* Entries in a PIC procedure linkage table look like this. */
1610 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1612 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1613 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1614 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1615 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1616 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1617 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1618 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1619 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1620 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1621 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1622 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1623 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1624 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
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 */
1630 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1632 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1633 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1634 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1635 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1636 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1637 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1638 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1639 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1640 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1641 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1642 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1643 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1644 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
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 */
1650 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1653 /* Big-endian non-PIC. */
1654 elf_sh_plt0_entry_be
,
1656 { 0, MINUS_ONE
, MINUS_ONE
},
1657 elf_sh_plt_entry_be
,
1660 33 /* includes ISA encoding */
1663 /* Little-endian non-PIC. */
1664 elf_sh_plt0_entry_le
,
1666 { 0, MINUS_ONE
, MINUS_ONE
},
1667 elf_sh_plt_entry_le
,
1670 33 /* includes ISA encoding */
1675 /* Big-endian PIC. */
1676 elf_sh_plt0_entry_be
,
1678 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1679 elf_sh_pic_plt_entry_be
,
1681 { 0, MINUS_ONE
, 52 },
1682 33 /* includes ISA encoding */
1685 /* Little-endian PIC. */
1686 elf_sh_plt0_entry_le
,
1688 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1689 elf_sh_pic_plt_entry_le
,
1691 { 0, MINUS_ONE
, 52 },
1692 33 /* includes ISA encoding */
1697 /* Return offset of the linker in PLT0 entry. */
1698 #define elf_sh_plt0_gotplt_offset(info) 0
1700 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1701 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1704 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1707 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p
,
1708 unsigned long value
, bfd_byte
*addr
)
1711 bfd_put_32 (output_bfd
,
1712 bfd_get_32 (output_bfd
, addr
)
1713 | ((value
>> 6) & 0x3fffc00),
1715 bfd_put_32 (output_bfd
,
1716 bfd_get_32 (output_bfd
, addr
+ 4)
1717 | ((value
<< 10) & 0x3fffc00),
1721 /* Return the type of PLT associated with ABFD. PIC_P is true if
1722 the object is position-independent. */
1724 static const struct elf_sh_plt_info
*
1725 get_plt_info (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean pic_p
)
1727 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
1730 /* The size in bytes of an entry in the procedure linkage table. */
1732 #define ELF_PLT_ENTRY_SIZE 28
1734 /* First entry in an absolute procedure linkage table look like this. */
1736 /* Note - this code has been "optimised" not to use r2. r2 is used by
1737 GCC to return the address of large structures, so it should not be
1738 corrupted here. This does mean however, that this PLT does not conform
1739 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1740 and r2 contains the GOT id. This version stores the GOT id in r0 and
1741 ignores the type. Loaders can easily detect this difference however,
1742 since the type will always be 0 or 8, and the GOT ids will always be
1743 greater than or equal to 12. */
1744 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1746 0xd0, 0x05, /* mov.l 2f,r0 */
1747 0x60, 0x02, /* mov.l @r0,r0 */
1748 0x2f, 0x06, /* mov.l r0,@-r15 */
1749 0xd0, 0x03, /* mov.l 1f,r0 */
1750 0x60, 0x02, /* mov.l @r0,r0 */
1751 0x40, 0x2b, /* jmp @r0 */
1752 0x60, 0xf6, /* mov.l @r15+,r0 */
1753 0x00, 0x09, /* nop */
1754 0x00, 0x09, /* nop */
1755 0x00, 0x09, /* nop */
1756 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1757 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1760 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1762 0x05, 0xd0, /* mov.l 2f,r0 */
1763 0x02, 0x60, /* mov.l @r0,r0 */
1764 0x06, 0x2f, /* mov.l r0,@-r15 */
1765 0x03, 0xd0, /* mov.l 1f,r0 */
1766 0x02, 0x60, /* mov.l @r0,r0 */
1767 0x2b, 0x40, /* jmp @r0 */
1768 0xf6, 0x60, /* mov.l @r15+,r0 */
1769 0x09, 0x00, /* nop */
1770 0x09, 0x00, /* nop */
1771 0x09, 0x00, /* nop */
1772 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1773 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1776 /* Sebsequent entries in an absolute procedure linkage table look like
1779 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1781 0xd0, 0x04, /* mov.l 1f,r0 */
1782 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1783 0xd1, 0x02, /* mov.l 0f,r1 */
1784 0x40, 0x2b, /* jmp @r0 */
1785 0x60, 0x13, /* mov r1,r0 */
1786 0xd1, 0x03, /* mov.l 2f,r1 */
1787 0x40, 0x2b, /* jmp @r0 */
1788 0x00, 0x09, /* nop */
1789 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1790 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1791 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1794 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1796 0x04, 0xd0, /* mov.l 1f,r0 */
1797 0x02, 0x60, /* mov.l @r0,r0 */
1798 0x02, 0xd1, /* mov.l 0f,r1 */
1799 0x2b, 0x40, /* jmp @r0 */
1800 0x13, 0x60, /* mov r1,r0 */
1801 0x03, 0xd1, /* mov.l 2f,r1 */
1802 0x2b, 0x40, /* jmp @r0 */
1803 0x09, 0x00, /* nop */
1804 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1805 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1806 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1809 /* Entries in a PIC procedure linkage table look like this. */
1811 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1813 0xd0, 0x04, /* mov.l 1f,r0 */
1814 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1815 0x40, 0x2b, /* jmp @r0 */
1816 0x00, 0x09, /* nop */
1817 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1818 0xd1, 0x03, /* mov.l 2f,r1 */
1819 0x40, 0x2b, /* jmp @r0 */
1820 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1821 0x00, 0x09, /* nop */
1822 0x00, 0x09, /* nop */
1823 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1824 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1827 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1829 0x04, 0xd0, /* mov.l 1f,r0 */
1830 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1831 0x2b, 0x40, /* jmp @r0 */
1832 0x09, 0x00, /* nop */
1833 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1834 0x03, 0xd1, /* mov.l 2f,r1 */
1835 0x2b, 0x40, /* jmp @r0 */
1836 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1837 0x09, 0x00, /* nop */
1838 0x09, 0x00, /* nop */
1839 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1840 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1843 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1846 /* Big-endian non-PIC. */
1847 elf_sh_plt0_entry_be
,
1849 { MINUS_ONE
, 24, 20 },
1850 elf_sh_plt_entry_be
,
1856 /* Little-endian non-PIC. */
1857 elf_sh_plt0_entry_le
,
1859 { MINUS_ONE
, 24, 20 },
1860 elf_sh_plt_entry_le
,
1868 /* Big-endian PIC. */
1869 elf_sh_plt0_entry_be
,
1871 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1872 elf_sh_pic_plt_entry_be
,
1874 { 20, MINUS_ONE
, 24 },
1878 /* Little-endian PIC. */
1879 elf_sh_plt0_entry_le
,
1881 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1882 elf_sh_pic_plt_entry_le
,
1884 { 20, MINUS_ONE
, 24 },
1890 #define VXWORKS_PLT_HEADER_SIZE 12
1891 #define VXWORKS_PLT_ENTRY_SIZE 24
1893 static const bfd_byte vxworks_sh_plt0_entry_be
[VXWORKS_PLT_HEADER_SIZE
] =
1895 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1896 0x61, 0x12, /* mov.l @r1,r1 */
1897 0x41, 0x2b, /* jmp @r1 */
1898 0x00, 0x09, /* nop */
1899 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1902 static const bfd_byte vxworks_sh_plt0_entry_le
[VXWORKS_PLT_HEADER_SIZE
] =
1904 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1905 0x12, 0x61, /* mov.l @r1,r1 */
1906 0x2b, 0x41, /* jmp @r1 */
1907 0x09, 0x00, /* nop */
1908 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1911 static const bfd_byte vxworks_sh_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1913 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1914 0x60, 0x02, /* mov.l @r0,r0 */
1915 0x40, 0x2b, /* jmp @r0 */
1916 0x00, 0x09, /* nop */
1917 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1918 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1919 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1920 0x00, 0x09, /* nop */
1921 0x00, 0x09, /* nop */
1922 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1925 static const bfd_byte vxworks_sh_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1927 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1928 0x02, 0x60, /* mov.l @r0,r0 */
1929 0x2b, 0x40, /* jmp @r0 */
1930 0x09, 0x00, /* nop */
1931 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1932 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1933 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1934 0x09, 0x00, /* nop */
1935 0x09, 0x00, /* nop */
1936 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1939 static const bfd_byte vxworks_sh_pic_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1941 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1942 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1943 0x40, 0x2b, /* jmp @r0 */
1944 0x00, 0x09, /* nop */
1945 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1946 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1947 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1948 0x41, 0x2b, /* jmp @r1 */
1949 0x00, 0x09, /* nop */
1950 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1953 static const bfd_byte vxworks_sh_pic_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1955 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1956 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1957 0x2b, 0x40, /* jmp @r0 */
1958 0x09, 0x00, /* nop */
1959 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1960 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1961 0xc2, 0x51, /* mov.l @(8,r12),r1 */
1962 0x2b, 0x41, /* jmp @r1 */
1963 0x09, 0x00, /* nop */
1964 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1967 static const struct elf_sh_plt_info vxworks_sh_plts
[2][2] = {
1970 /* Big-endian non-PIC. */
1971 vxworks_sh_plt0_entry_be
,
1972 VXWORKS_PLT_HEADER_SIZE
,
1973 { MINUS_ONE
, MINUS_ONE
, 8 },
1974 vxworks_sh_plt_entry_be
,
1975 VXWORKS_PLT_ENTRY_SIZE
,
1980 /* Little-endian non-PIC. */
1981 vxworks_sh_plt0_entry_le
,
1982 VXWORKS_PLT_HEADER_SIZE
,
1983 { MINUS_ONE
, MINUS_ONE
, 8 },
1984 vxworks_sh_plt_entry_le
,
1985 VXWORKS_PLT_ENTRY_SIZE
,
1992 /* Big-endian PIC. */
1995 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1996 vxworks_sh_pic_plt_entry_be
,
1997 VXWORKS_PLT_ENTRY_SIZE
,
1998 { 8, MINUS_ONE
, 20 },
2002 /* Little-endian PIC. */
2005 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2006 vxworks_sh_pic_plt_entry_le
,
2007 VXWORKS_PLT_ENTRY_SIZE
,
2008 { 8, MINUS_ONE
, 20 },
2014 /* Return the type of PLT associated with ABFD. PIC_P is true if
2015 the object is position-independent. */
2017 static const struct elf_sh_plt_info
*
2018 get_plt_info (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean pic_p
)
2020 if (vxworks_object_p (abfd
))
2021 return &vxworks_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2022 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2025 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2026 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2030 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p ATTRIBUTE_UNUSED
,
2031 unsigned long value
, bfd_byte
*addr
)
2033 bfd_put_32 (output_bfd
, value
, addr
);
2037 /* Return the index of the PLT entry at byte offset OFFSET. */
2040 get_plt_index (const struct elf_sh_plt_info
*info
, bfd_vma offset
)
2042 return (offset
- info
->plt0_entry_size
) / info
->symbol_entry_size
;
2045 /* Do the inverse operation. */
2048 get_plt_offset (const struct elf_sh_plt_info
*info
, bfd_vma index
)
2050 return info
->plt0_entry_size
+ (index
* info
->symbol_entry_size
);
2053 /* The sh linker needs to keep track of the number of relocs that it
2054 decides to copy as dynamic relocs in check_relocs for each symbol.
2055 This is so that it can later discard them if they are found to be
2056 unnecessary. We store the information in a field extending the
2057 regular ELF linker hash table. */
2059 struct elf_sh_dyn_relocs
2061 struct elf_sh_dyn_relocs
*next
;
2063 /* The input section of the reloc. */
2066 /* Total number of relocs copied for the input section. */
2067 bfd_size_type count
;
2069 /* Number of pc-relative relocs copied for the input section. */
2070 bfd_size_type pc_count
;
2073 /* sh ELF linker hash entry. */
2075 struct elf_sh_link_hash_entry
2077 struct elf_link_hash_entry root
;
2079 #ifdef INCLUDE_SHMEDIA
2082 bfd_signed_vma refcount
;
2087 /* Track dynamic relocs copied for this symbol. */
2088 struct elf_sh_dyn_relocs
*dyn_relocs
;
2090 bfd_signed_vma gotplt_refcount
;
2093 GOT_UNKNOWN
= 0, GOT_NORMAL
, GOT_TLS_GD
, GOT_TLS_IE
2097 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2099 struct sh_elf_obj_tdata
2101 struct elf_obj_tdata root
;
2103 /* tls_type for each local got entry. */
2104 char *local_got_tls_type
;
2107 #define sh_elf_tdata(abfd) \
2108 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2110 #define sh_elf_local_got_tls_type(abfd) \
2111 (sh_elf_tdata (abfd)->local_got_tls_type)
2113 /* Override the generic function because we need to store sh_elf_obj_tdata
2114 as the specific tdata. */
2117 sh_elf_mkobject (bfd
*abfd
)
2119 if (abfd
->tdata
.any
== NULL
)
2121 bfd_size_type amt
= sizeof (struct sh_elf_obj_tdata
);
2122 abfd
->tdata
.any
= bfd_zalloc (abfd
, amt
);
2123 if (abfd
->tdata
.any
== NULL
)
2126 return bfd_elf_mkobject (abfd
);
2129 /* sh ELF linker hash table. */
2131 struct elf_sh_link_hash_table
2133 struct elf_link_hash_table root
;
2135 /* Short-cuts to get to dynamic linker sections. */
2144 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2147 /* Small local sym to section mapping cache. */
2148 struct sym_sec_cache sym_sec
;
2150 /* A counter or offset to track a TLS got entry. */
2153 bfd_signed_vma refcount
;
2157 /* The type of PLT to use. */
2158 const struct elf_sh_plt_info
*plt_info
;
2160 /* True if the target system is VxWorks. */
2161 bfd_boolean vxworks_p
;
2164 /* Traverse an sh ELF linker hash table. */
2166 #define sh_elf_link_hash_traverse(table, func, info) \
2167 (elf_link_hash_traverse \
2169 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2172 /* Get the sh ELF linker hash table from a link_info structure. */
2174 #define sh_elf_hash_table(p) \
2175 ((struct elf_sh_link_hash_table *) ((p)->hash))
2177 /* Create an entry in an sh ELF linker hash table. */
2179 static struct bfd_hash_entry
*
2180 sh_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
2181 struct bfd_hash_table
*table
,
2184 struct elf_sh_link_hash_entry
*ret
=
2185 (struct elf_sh_link_hash_entry
*) entry
;
2187 /* Allocate the structure if it has not already been allocated by a
2189 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2190 ret
= ((struct elf_sh_link_hash_entry
*)
2191 bfd_hash_allocate (table
,
2192 sizeof (struct elf_sh_link_hash_entry
)));
2193 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2194 return (struct bfd_hash_entry
*) ret
;
2196 /* Call the allocation method of the superclass. */
2197 ret
= ((struct elf_sh_link_hash_entry
*)
2198 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2200 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2202 ret
->dyn_relocs
= NULL
;
2203 ret
->gotplt_refcount
= 0;
2204 #ifdef INCLUDE_SHMEDIA
2205 ret
->datalabel_got
.refcount
= ret
->root
.got
.refcount
;
2207 ret
->tls_type
= GOT_UNKNOWN
;
2210 return (struct bfd_hash_entry
*) ret
;
2213 /* Create an sh ELF linker hash table. */
2215 static struct bfd_link_hash_table
*
2216 sh_elf_link_hash_table_create (bfd
*abfd
)
2218 struct elf_sh_link_hash_table
*ret
;
2219 bfd_size_type amt
= sizeof (struct elf_sh_link_hash_table
);
2221 ret
= (struct elf_sh_link_hash_table
*) bfd_malloc (amt
);
2222 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2225 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2226 sh_elf_link_hash_newfunc
,
2227 sizeof (struct elf_sh_link_hash_entry
)))
2234 ret
->sgotplt
= NULL
;
2235 ret
->srelgot
= NULL
;
2237 ret
->srelplt
= NULL
;
2238 ret
->sdynbss
= NULL
;
2239 ret
->srelbss
= NULL
;
2240 ret
->srelplt2
= NULL
;
2241 ret
->sym_sec
.abfd
= NULL
;
2242 ret
->tls_ldm_got
.refcount
= 0;
2243 ret
->plt_info
= NULL
;
2244 ret
->vxworks_p
= vxworks_object_p (abfd
);
2246 return &ret
->root
.root
;
2249 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2250 shortcuts to them in our hash table. */
2253 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
2255 struct elf_sh_link_hash_table
*htab
;
2257 if (! _bfd_elf_create_got_section (dynobj
, info
))
2260 htab
= sh_elf_hash_table (info
);
2261 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
2262 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
2263 if (! htab
->sgot
|| ! htab
->sgotplt
)
2266 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rela.got",
2267 (SEC_ALLOC
| SEC_LOAD
2270 | SEC_LINKER_CREATED
2272 if (htab
->srelgot
== NULL
2273 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 2))
2278 /* Create dynamic sections when linking against a dynamic object. */
2281 sh_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2283 struct elf_sh_link_hash_table
*htab
;
2284 flagword flags
, pltflags
;
2285 register asection
*s
;
2286 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2289 switch (bed
->s
->arch_size
)
2300 bfd_set_error (bfd_error_bad_value
);
2304 htab
= sh_elf_hash_table (info
);
2305 if (htab
->root
.dynamic_sections_created
)
2308 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2309 .rel[a].bss sections. */
2311 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2312 | SEC_LINKER_CREATED
);
2315 pltflags
|= SEC_CODE
;
2316 if (bed
->plt_not_loaded
)
2317 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2318 if (bed
->plt_readonly
)
2319 pltflags
|= SEC_READONLY
;
2321 s
= bfd_make_section_with_flags (abfd
, ".plt", pltflags
);
2324 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
2327 if (bed
->want_plt_sym
)
2329 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2331 struct elf_link_hash_entry
*h
;
2332 struct bfd_link_hash_entry
*bh
= NULL
;
2334 if (! (_bfd_generic_link_add_one_symbol
2335 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2336 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
2337 get_elf_backend_data (abfd
)->collect
, &bh
)))
2340 h
= (struct elf_link_hash_entry
*) bh
;
2342 h
->type
= STT_OBJECT
;
2343 htab
->root
.hplt
= h
;
2346 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2350 s
= bfd_make_section_with_flags (abfd
,
2351 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt",
2352 flags
| SEC_READONLY
);
2355 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2358 if (htab
->sgot
== NULL
2359 && !create_got_section (abfd
, info
))
2363 const char *secname
;
2368 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2370 secflags
= bfd_get_section_flags (abfd
, sec
);
2371 if ((secflags
& (SEC_DATA
| SEC_LINKER_CREATED
))
2372 || ((secflags
& SEC_HAS_CONTENTS
) != SEC_HAS_CONTENTS
))
2374 secname
= bfd_get_section_name (abfd
, sec
);
2375 relname
= (char *) bfd_malloc ((bfd_size_type
) strlen (secname
) + 6);
2376 strcpy (relname
, ".rela");
2377 strcat (relname
, secname
);
2378 if (bfd_get_section_by_name (abfd
, secname
))
2380 s
= bfd_make_section_with_flags (abfd
, relname
,
2381 flags
| SEC_READONLY
);
2383 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2388 if (bed
->want_dynbss
)
2390 /* The .dynbss section is a place to put symbols which are defined
2391 by dynamic objects, are referenced by regular objects, and are
2392 not functions. We must allocate space for them in the process
2393 image and use a R_*_COPY reloc to tell the dynamic linker to
2394 initialize them at run time. The linker script puts the .dynbss
2395 section into the .bss section of the final image. */
2396 s
= bfd_make_section_with_flags (abfd
, ".dynbss",
2397 SEC_ALLOC
| SEC_LINKER_CREATED
);
2402 /* The .rel[a].bss section holds copy relocs. This section is not
2403 normally needed. We need to create it here, though, so that the
2404 linker will map it to an output section. We can't just create it
2405 only if we need it, because we will not know whether we need it
2406 until we have seen all the input files, and the first time the
2407 main linker code calls BFD after examining all the input files
2408 (size_dynamic_sections) the input sections have already been
2409 mapped to the output sections. If the section turns out not to
2410 be needed, we can discard it later. We will never need this
2411 section when generating a shared object, since they do not use
2415 s
= bfd_make_section_with_flags (abfd
,
2416 (bed
->default_use_rela_p
2417 ? ".rela.bss" : ".rel.bss"),
2418 flags
| SEC_READONLY
);
2421 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2426 if (htab
->vxworks_p
)
2428 if (!elf_vxworks_create_dynamic_sections (abfd
, info
, &htab
->srelplt2
))
2435 /* Adjust a symbol defined by a dynamic object and referenced by a
2436 regular object. The current definition is in some section of the
2437 dynamic object, but we're not including those sections. We have to
2438 change the definition to something the rest of the link can
2442 sh_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2443 struct elf_link_hash_entry
*h
)
2445 struct elf_sh_link_hash_table
*htab
;
2446 struct elf_sh_link_hash_entry
*eh
;
2447 struct elf_sh_dyn_relocs
*p
;
2449 unsigned int power_of_two
;
2451 htab
= sh_elf_hash_table (info
);
2453 /* Make sure we know what is going on here. */
2454 BFD_ASSERT (htab
->root
.dynobj
!= NULL
2456 || h
->u
.weakdef
!= NULL
2459 && !h
->def_regular
)));
2461 /* If this is a function, put it in the procedure linkage table. We
2462 will fill in the contents of the procedure linkage table later,
2463 when we know the address of the .got section. */
2464 if (h
->type
== STT_FUNC
2467 if (h
->plt
.refcount
<= 0
2468 || SYMBOL_CALLS_LOCAL (info
, h
)
2469 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2470 && h
->root
.type
== bfd_link_hash_undefweak
))
2472 /* This case can occur if we saw a PLT reloc in an input
2473 file, but the symbol was never referred to by a dynamic
2474 object. In such a case, we don't actually need to build
2475 a procedure linkage table, and we can just do a REL32
2477 h
->plt
.offset
= (bfd_vma
) -1;
2484 h
->plt
.offset
= (bfd_vma
) -1;
2486 /* If this is a weak symbol, and there is a real definition, the
2487 processor independent code will have arranged for us to see the
2488 real definition first, and we can just use the same value. */
2489 if (h
->u
.weakdef
!= NULL
)
2491 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
2492 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
2493 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
2494 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
2495 if (info
->nocopyreloc
)
2496 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
2500 /* This is a reference to a symbol defined by a dynamic object which
2501 is not a function. */
2503 /* If we are creating a shared library, we must presume that the
2504 only references to the symbol are via the global offset table.
2505 For such cases we need not do anything here; the relocations will
2506 be handled correctly by relocate_section. */
2510 /* If there are no references to this symbol that do not use the
2511 GOT, we don't need to generate a copy reloc. */
2512 if (!h
->non_got_ref
)
2515 /* If -z nocopyreloc was given, we won't generate them either. */
2516 if (info
->nocopyreloc
)
2522 eh
= (struct elf_sh_link_hash_entry
*) h
;
2523 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2525 s
= p
->sec
->output_section
;
2526 if (s
!= NULL
&& (s
->flags
& (SEC_READONLY
| SEC_HAS_CONTENTS
)) != 0)
2530 /* If we didn't find any dynamic relocs in sections which needs the
2531 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2541 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
2542 h
->root
.root
.string
);
2546 /* We must allocate the symbol in our .dynbss section, which will
2547 become part of the .bss section of the executable. There will be
2548 an entry for this symbol in the .dynsym section. The dynamic
2549 object will contain position independent code, so all references
2550 from the dynamic object to this symbol will go through the global
2551 offset table. The dynamic linker will use the .dynsym entry to
2552 determine the address it must put in the global offset table, so
2553 both the dynamic object and the regular object will refer to the
2554 same memory location for the variable. */
2557 BFD_ASSERT (s
!= NULL
);
2559 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2560 copy the initial value out of the dynamic object and into the
2561 runtime process image. We need to remember the offset into the
2562 .rela.bss section we are going to use. */
2563 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
2567 srel
= htab
->srelbss
;
2568 BFD_ASSERT (srel
!= NULL
);
2569 srel
->size
+= sizeof (Elf32_External_Rela
);
2573 /* We need to figure out the alignment required for this symbol. I
2574 have no idea how ELF linkers handle this. */
2575 power_of_two
= bfd_log2 (h
->size
);
2576 if (power_of_two
> 3)
2579 /* Apply the required alignment. */
2580 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
) (1 << power_of_two
));
2581 if (power_of_two
> bfd_get_section_alignment (htab
->root
.dynobj
, s
))
2583 if (! bfd_set_section_alignment (htab
->root
.dynobj
, s
, power_of_two
))
2587 /* Define the symbol as being at this point in the section. */
2588 h
->root
.u
.def
.section
= s
;
2589 h
->root
.u
.def
.value
= s
->size
;
2591 /* Increment the section size to make room for the symbol. */
2597 /* Allocate space in .plt, .got and associated reloc sections for
2601 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2603 struct bfd_link_info
*info
;
2604 struct elf_sh_link_hash_table
*htab
;
2605 struct elf_sh_link_hash_entry
*eh
;
2606 struct elf_sh_dyn_relocs
*p
;
2608 if (h
->root
.type
== bfd_link_hash_indirect
)
2611 if (h
->root
.type
== bfd_link_hash_warning
)
2612 /* When warning symbols are created, they **replace** the "real"
2613 entry in the hash table, thus we never get to see the real
2614 symbol in a hash traversal. So look at it now. */
2615 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2617 info
= (struct bfd_link_info
*) inf
;
2618 htab
= sh_elf_hash_table (info
);
2620 eh
= (struct elf_sh_link_hash_entry
*) h
;
2621 if ((h
->got
.refcount
> 0
2623 && eh
->gotplt_refcount
> 0)
2625 /* The symbol has been forced local, or we have some direct got refs,
2626 so treat all the gotplt refs as got refs. */
2627 h
->got
.refcount
+= eh
->gotplt_refcount
;
2628 if (h
->plt
.refcount
>= eh
->gotplt_refcount
)
2629 h
->plt
.refcount
-= eh
->gotplt_refcount
;
2632 if (htab
->root
.dynamic_sections_created
2633 && h
->plt
.refcount
> 0
2634 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2635 || h
->root
.type
!= bfd_link_hash_undefweak
))
2637 /* Make sure this symbol is output as a dynamic symbol.
2638 Undefined weak syms won't yet be marked as dynamic. */
2639 if (h
->dynindx
== -1
2640 && !h
->forced_local
)
2642 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2647 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2649 asection
*s
= htab
->splt
;
2651 /* If this is the first .plt entry, make room for the special
2654 s
->size
+= htab
->plt_info
->plt0_entry_size
;
2656 h
->plt
.offset
= s
->size
;
2658 /* If this symbol is not defined in a regular file, and we are
2659 not generating a shared library, then set the symbol to this
2660 location in the .plt. This is required to make function
2661 pointers compare as equal between the normal executable and
2662 the shared library. */
2666 h
->root
.u
.def
.section
= s
;
2667 h
->root
.u
.def
.value
= h
->plt
.offset
;
2670 /* Make room for this entry. */
2671 s
->size
+= htab
->plt_info
->symbol_entry_size
;
2673 /* We also need to make an entry in the .got.plt section, which
2674 will be placed in the .got section by the linker script. */
2675 htab
->sgotplt
->size
+= 4;
2677 /* We also need to make an entry in the .rel.plt section. */
2678 htab
->srelplt
->size
+= sizeof (Elf32_External_Rela
);
2680 if (htab
->vxworks_p
&& !info
->shared
)
2682 /* VxWorks executables have a second set of relocations
2683 for each PLT entry. They go in a separate relocation
2684 section, which is processed by the kernel loader. */
2686 /* There is a relocation for the initial PLT entry:
2687 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2688 if (h
->plt
.offset
== htab
->plt_info
->plt0_entry_size
)
2689 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
);
2691 /* There are two extra relocations for each subsequent
2692 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2693 and an R_SH_DIR32 relocation for the PLT entry. */
2694 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
) * 2;
2699 h
->plt
.offset
= (bfd_vma
) -1;
2705 h
->plt
.offset
= (bfd_vma
) -1;
2709 if (h
->got
.refcount
> 0)
2713 int tls_type
= sh_elf_hash_entry (h
)->tls_type
;
2715 /* Make sure this symbol is output as a dynamic symbol.
2716 Undefined weak syms won't yet be marked as dynamic. */
2717 if (h
->dynindx
== -1
2718 && !h
->forced_local
)
2720 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2725 h
->got
.offset
= s
->size
;
2727 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2728 if (tls_type
== GOT_TLS_GD
)
2730 dyn
= htab
->root
.dynamic_sections_created
;
2731 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2732 R_SH_TLS_GD needs one if local symbol and two if global. */
2733 if ((tls_type
== GOT_TLS_GD
&& h
->dynindx
== -1)
2734 || (tls_type
== GOT_TLS_IE
&& dyn
))
2735 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2736 else if (tls_type
== GOT_TLS_GD
)
2737 htab
->srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
2738 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2739 || h
->root
.type
!= bfd_link_hash_undefweak
)
2741 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
2742 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2745 h
->got
.offset
= (bfd_vma
) -1;
2747 #ifdef INCLUDE_SHMEDIA
2748 if (eh
->datalabel_got
.refcount
> 0)
2753 /* Make sure this symbol is output as a dynamic symbol.
2754 Undefined weak syms won't yet be marked as dynamic. */
2755 if (h
->dynindx
== -1
2756 && !h
->forced_local
)
2758 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2763 eh
->datalabel_got
.offset
= s
->size
;
2765 dyn
= htab
->root
.dynamic_sections_created
;
2766 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
))
2767 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
2770 eh
->datalabel_got
.offset
= (bfd_vma
) -1;
2773 if (eh
->dyn_relocs
== NULL
)
2776 /* In the shared -Bsymbolic case, discard space allocated for
2777 dynamic pc-relative relocs against symbols which turn out to be
2778 defined in regular objects. For the normal shared case, discard
2779 space for pc-relative relocs that have become local due to symbol
2780 visibility changes. */
2784 if (SYMBOL_CALLS_LOCAL (info
, h
))
2786 struct elf_sh_dyn_relocs
**pp
;
2788 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2790 p
->count
-= p
->pc_count
;
2799 /* Also discard relocs on undefined weak syms with non-default
2801 if (eh
->dyn_relocs
!= NULL
2802 && h
->root
.type
== bfd_link_hash_undefweak
)
2804 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2805 eh
->dyn_relocs
= NULL
;
2807 /* Make sure undefined weak symbols are output as a dynamic
2809 else if (h
->dynindx
== -1
2810 && !h
->forced_local
)
2812 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2819 /* For the non-shared case, discard space for relocs against
2820 symbols which turn out to need copy relocs or are not
2826 || (htab
->root
.dynamic_sections_created
2827 && (h
->root
.type
== bfd_link_hash_undefweak
2828 || h
->root
.type
== bfd_link_hash_undefined
))))
2830 /* Make sure this symbol is output as a dynamic symbol.
2831 Undefined weak syms won't yet be marked as dynamic. */
2832 if (h
->dynindx
== -1
2833 && !h
->forced_local
)
2835 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2839 /* If that succeeded, we know we'll be keeping all the
2841 if (h
->dynindx
!= -1)
2845 eh
->dyn_relocs
= NULL
;
2850 /* Finally, allocate space. */
2851 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2853 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
2854 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
2860 /* Find any dynamic relocs that apply to read-only sections. */
2863 readonly_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2865 struct elf_sh_link_hash_entry
*eh
;
2866 struct elf_sh_dyn_relocs
*p
;
2868 if (h
->root
.type
== bfd_link_hash_warning
)
2869 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2871 eh
= (struct elf_sh_link_hash_entry
*) h
;
2872 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2874 asection
*s
= p
->sec
->output_section
;
2876 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2878 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
2880 info
->flags
|= DF_TEXTREL
;
2882 /* Not an error, just cut short the traversal. */
2889 /* This function is called after all the input files have been read,
2890 and the input sections have been assigned to output sections.
2891 It's a convenient place to determine the PLT style. */
2894 sh_elf_always_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
2896 sh_elf_hash_table (info
)->plt_info
= get_plt_info (output_bfd
, info
->shared
);
2900 /* Set the sizes of the dynamic sections. */
2903 sh_elf_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2904 struct bfd_link_info
*info
)
2906 struct elf_sh_link_hash_table
*htab
;
2912 htab
= sh_elf_hash_table (info
);
2913 dynobj
= htab
->root
.dynobj
;
2914 BFD_ASSERT (dynobj
!= NULL
);
2916 if (htab
->root
.dynamic_sections_created
)
2918 /* Set the contents of the .interp section to the interpreter. */
2919 if (info
->executable
)
2921 s
= bfd_get_section_by_name (dynobj
, ".interp");
2922 BFD_ASSERT (s
!= NULL
);
2923 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
2924 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2928 /* Set up .got offsets for local syms, and space for local dynamic
2930 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2932 bfd_signed_vma
*local_got
;
2933 bfd_signed_vma
*end_local_got
;
2934 char *local_tls_type
;
2935 bfd_size_type locsymcount
;
2936 Elf_Internal_Shdr
*symtab_hdr
;
2939 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
2942 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2944 struct elf_sh_dyn_relocs
*p
;
2946 for (p
= ((struct elf_sh_dyn_relocs
*)
2947 elf_section_data (s
)->local_dynrel
);
2951 if (! bfd_is_abs_section (p
->sec
)
2952 && bfd_is_abs_section (p
->sec
->output_section
))
2954 /* Input section has been discarded, either because
2955 it is a copy of a linkonce section or due to
2956 linker script /DISCARD/, so we'll be discarding
2959 else if (p
->count
!= 0)
2961 srel
= elf_section_data (p
->sec
)->sreloc
;
2962 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
2963 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
2964 info
->flags
|= DF_TEXTREL
;
2969 local_got
= elf_local_got_refcounts (ibfd
);
2973 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
2974 locsymcount
= symtab_hdr
->sh_info
;
2975 #ifdef INCLUDE_SHMEDIA
2976 /* Count datalabel local GOT. */
2979 end_local_got
= local_got
+ locsymcount
;
2980 local_tls_type
= sh_elf_local_got_tls_type (ibfd
);
2982 srel
= htab
->srelgot
;
2983 for (; local_got
< end_local_got
; ++local_got
)
2987 *local_got
= s
->size
;
2989 if (*local_tls_type
== GOT_TLS_GD
)
2992 srel
->size
+= sizeof (Elf32_External_Rela
);
2995 *local_got
= (bfd_vma
) -1;
3000 if (htab
->tls_ldm_got
.refcount
> 0)
3002 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3004 htab
->tls_ldm_got
.offset
= htab
->sgot
->size
;
3005 htab
->sgot
->size
+= 8;
3006 htab
->srelgot
->size
+= sizeof (Elf32_External_Rela
);
3009 htab
->tls_ldm_got
.offset
= -1;
3011 /* Allocate global sym .plt and .got entries, and space for global
3012 sym dynamic relocs. */
3013 elf_link_hash_traverse (&htab
->root
, allocate_dynrelocs
, info
);
3015 /* We now have determined the sizes of the various dynamic sections.
3016 Allocate memory for them. */
3018 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
3020 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
3025 || s
== htab
->sgotplt
3026 || s
== htab
->sdynbss
)
3028 /* Strip this section if we don't need it; see the
3031 else if (strncmp (bfd_get_section_name (dynobj
, s
), ".rela", 5) == 0)
3033 if (s
->size
!= 0 && s
!= htab
->srelplt
&& s
!= htab
->srelplt2
)
3036 /* We use the reloc_count field as a counter if we need
3037 to copy relocs into the output file. */
3042 /* It's not one of our sections, so don't allocate space. */
3048 /* If we don't need this section, strip it from the
3049 output file. This is mostly to handle .rela.bss and
3050 .rela.plt. We must create both sections in
3051 create_dynamic_sections, because they must be created
3052 before the linker maps input sections to output
3053 sections. The linker does that before
3054 adjust_dynamic_symbol is called, and it is that
3055 function which decides whether anything needs to go
3056 into these sections. */
3058 s
->flags
|= SEC_EXCLUDE
;
3062 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3065 /* Allocate memory for the section contents. We use bfd_zalloc
3066 here in case unused entries are not reclaimed before the
3067 section's contents are written out. This should not happen,
3068 but this way if it does, we get a R_SH_NONE reloc instead
3070 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
3071 if (s
->contents
== NULL
)
3075 if (htab
->root
.dynamic_sections_created
)
3077 /* Add some entries to the .dynamic section. We fill in the
3078 values later, in sh_elf_finish_dynamic_sections, but we
3079 must add the entries now so that we get the correct size for
3080 the .dynamic section. The DT_DEBUG entry is filled in by the
3081 dynamic linker and used by the debugger. */
3082 #define add_dynamic_entry(TAG, VAL) \
3083 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3085 if (info
->executable
)
3087 if (! add_dynamic_entry (DT_DEBUG
, 0))
3091 if (htab
->splt
->size
!= 0)
3093 if (! add_dynamic_entry (DT_PLTGOT
, 0)
3094 || ! add_dynamic_entry (DT_PLTRELSZ
, 0)
3095 || ! add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3096 || ! add_dynamic_entry (DT_JMPREL
, 0))
3102 if (! add_dynamic_entry (DT_RELA
, 0)
3103 || ! add_dynamic_entry (DT_RELASZ
, 0)
3104 || ! add_dynamic_entry (DT_RELAENT
,
3105 sizeof (Elf32_External_Rela
)))
3108 /* If any dynamic relocs apply to a read-only section,
3109 then we need a DT_TEXTREL entry. */
3110 if ((info
->flags
& DF_TEXTREL
) == 0)
3111 elf_link_hash_traverse (&htab
->root
, readonly_dynrelocs
, info
);
3113 if ((info
->flags
& DF_TEXTREL
) != 0)
3115 if (! add_dynamic_entry (DT_TEXTREL
, 0))
3120 #undef add_dynamic_entry
3125 /* Relocate an SH ELF section. */
3128 sh_elf_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
3129 bfd
*input_bfd
, asection
*input_section
,
3130 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
3131 Elf_Internal_Sym
*local_syms
,
3132 asection
**local_sections
)
3134 struct elf_sh_link_hash_table
*htab
;
3135 Elf_Internal_Shdr
*symtab_hdr
;
3136 struct elf_link_hash_entry
**sym_hashes
;
3137 Elf_Internal_Rela
*rel
, *relend
;
3139 bfd_vma
*local_got_offsets
;
3146 htab
= sh_elf_hash_table (info
);
3147 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3148 sym_hashes
= elf_sym_hashes (input_bfd
);
3149 dynobj
= htab
->root
.dynobj
;
3150 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3153 sgotplt
= htab
->sgotplt
;
3159 relend
= relocs
+ input_section
->reloc_count
;
3160 for (; rel
< relend
; rel
++)
3163 reloc_howto_type
*howto
;
3164 unsigned long r_symndx
;
3165 Elf_Internal_Sym
*sym
;
3167 struct elf_link_hash_entry
*h
;
3169 bfd_vma addend
= (bfd_vma
) 0;
3170 bfd_reloc_status_type r
;
3171 int seen_stt_datalabel
= 0;
3175 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3177 r_type
= ELF32_R_TYPE (rel
->r_info
);
3179 /* Many of the relocs are only used for relaxing, and are
3180 handled entirely by the relaxation code. */
3181 if (r_type
>= (int) R_SH_GNU_VTINHERIT
3182 && r_type
<= (int) R_SH_LABEL
)
3184 if (r_type
== (int) R_SH_NONE
)
3188 || r_type
>= R_SH_max
3189 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
3190 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
3191 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_3
3192 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_3
)
3193 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_4
3194 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_4
)
3195 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_5
3196 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_5
)
3197 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
3198 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
))
3200 bfd_set_error (bfd_error_bad_value
);
3204 howto
= get_howto_table (output_bfd
) + r_type
;
3206 /* For relocs that aren't partial_inplace, we get the addend from
3208 if (! howto
->partial_inplace
)
3209 addend
= rel
->r_addend
;
3214 if (r_symndx
< symtab_hdr
->sh_info
)
3216 sym
= local_syms
+ r_symndx
;
3217 sec
= local_sections
[r_symndx
];
3218 relocation
= (sec
->output_section
->vma
3219 + sec
->output_offset
3221 /* A local symbol never has STO_SH5_ISA32, so we don't need
3222 datalabel processing here. Make sure this does not change
3224 if ((sym
->st_other
& STO_SH5_ISA32
) != 0)
3225 ((*info
->callbacks
->reloc_dangerous
)
3227 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3228 input_bfd
, input_section
, rel
->r_offset
));
3229 if (info
->relocatable
)
3231 /* This is a relocatable link. We don't have to change
3232 anything, unless the reloc is against a section symbol,
3233 in which case we have to adjust according to where the
3234 section symbol winds up in the output section. */
3235 sym
= local_syms
+ r_symndx
;
3236 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3238 if (! howto
->partial_inplace
)
3240 /* For relocations with the addend in the
3241 relocation, we need just to update the addend.
3242 All real relocs are of type partial_inplace; this
3243 code is mostly for completeness. */
3244 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
3249 /* Relocs of type partial_inplace need to pick up the
3250 contents in the contents and add the offset resulting
3251 from the changed location of the section symbol.
3252 Using _bfd_final_link_relocate (e.g. goto
3253 final_link_relocate) here would be wrong, because
3254 relocations marked pc_relative would get the current
3255 location subtracted, and we must only do that at the
3257 r
= _bfd_relocate_contents (howto
, input_bfd
,
3260 contents
+ rel
->r_offset
);
3261 goto relocation_done
;
3266 else if (! howto
->partial_inplace
)
3268 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3269 addend
= rel
->r_addend
;
3271 else if ((sec
->flags
& SEC_MERGE
)
3272 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3276 if (howto
->rightshift
|| howto
->src_mask
!= 0xffffffff)
3278 (*_bfd_error_handler
)
3279 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3280 input_bfd
, input_section
,
3281 (long) rel
->r_offset
, howto
->name
);
3285 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3288 _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
)
3290 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
3291 bfd_put_32 (input_bfd
, addend
, contents
+ rel
->r_offset
);
3297 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3299 /* Section symbol are never (?) placed in the hash table, so
3300 we can just ignore hash relocations when creating a
3301 relocatable object file. */
3302 if (info
->relocatable
)
3305 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3306 while (h
->root
.type
== bfd_link_hash_indirect
3307 || h
->root
.type
== bfd_link_hash_warning
)
3309 #ifdef INCLUDE_SHMEDIA
3310 /* If the reference passes a symbol marked with
3311 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
3313 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
3315 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3317 if (h
->root
.type
== bfd_link_hash_defined
3318 || h
->root
.type
== bfd_link_hash_defweak
)
3322 dyn
= htab
->root
.dynamic_sections_created
;
3323 sec
= h
->root
.u
.def
.section
;
3324 /* In these cases, we don't need the relocation value.
3325 We check specially because in some obscure cases
3326 sec->output_section will be NULL. */
3327 if (r_type
== R_SH_GOTPC
3328 || r_type
== R_SH_GOTPC_LOW16
3329 || r_type
== R_SH_GOTPC_MEDLOW16
3330 || r_type
== R_SH_GOTPC_MEDHI16
3331 || r_type
== R_SH_GOTPC_HI16
3332 || ((r_type
== R_SH_PLT32
3333 || r_type
== R_SH_PLT_LOW16
3334 || r_type
== R_SH_PLT_MEDLOW16
3335 || r_type
== R_SH_PLT_MEDHI16
3336 || r_type
== R_SH_PLT_HI16
)
3337 && h
->plt
.offset
!= (bfd_vma
) -1)
3338 || ((r_type
== R_SH_GOT32
3339 || r_type
== R_SH_GOT_LOW16
3340 || r_type
== R_SH_GOT_MEDLOW16
3341 || r_type
== R_SH_GOT_MEDHI16
3342 || r_type
== R_SH_GOT_HI16
)
3343 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3345 || (! info
->symbolic
&& h
->dynindx
!= -1)
3346 || !h
->def_regular
))
3347 /* The cases above are those in which relocation is
3348 overwritten in the switch block below. The cases
3349 below are those in which we must defer relocation
3350 to run-time, because we can't resolve absolute
3351 addresses when creating a shared library. */
3353 && ((! info
->symbolic
&& h
->dynindx
!= -1)
3355 && ((r_type
== R_SH_DIR32
3356 && !h
->forced_local
)
3357 || (r_type
== R_SH_REL32
3358 && !SYMBOL_CALLS_LOCAL (info
, h
)))
3359 && ((input_section
->flags
& SEC_ALLOC
) != 0
3360 /* DWARF will emit R_SH_DIR32 relocations in its
3361 sections against symbols defined externally
3362 in shared libraries. We can't do anything
3364 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
3365 && h
->def_dynamic
)))
3366 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3367 sections because such sections are not SEC_ALLOC and
3368 thus ld.so will not process them. */
3369 || (sec
->output_section
== NULL
3370 && ((input_section
->flags
& SEC_DEBUGGING
) != 0
3372 || (sec
->output_section
== NULL
3373 && (sh_elf_hash_entry (h
)->tls_type
== GOT_TLS_IE
3374 || sh_elf_hash_entry (h
)->tls_type
== GOT_TLS_GD
)))
3376 else if (sec
->output_section
== NULL
)
3378 (*_bfd_error_handler
)
3379 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3382 (long) rel
->r_offset
,
3384 h
->root
.root
.string
);
3388 relocation
= ((h
->root
.u
.def
.value
3389 + sec
->output_section
->vma
3390 + sec
->output_offset
)
3391 /* A STO_SH5_ISA32 causes a "bitor 1" to the
3392 symbol value, unless we've seen
3393 STT_DATALABEL on the way to it. */
3394 | ((h
->other
& STO_SH5_ISA32
) != 0
3395 && ! seen_stt_datalabel
));
3397 else if (h
->root
.type
== bfd_link_hash_undefweak
)
3399 else if (info
->unresolved_syms_in_objects
== RM_IGNORE
3400 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3404 if (! info
->callbacks
->undefined_symbol
3405 (info
, h
->root
.root
.string
, input_bfd
,
3406 input_section
, rel
->r_offset
,
3407 (info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
3408 || ELF_ST_VISIBILITY (h
->other
))))
3414 switch ((int) r_type
)
3416 final_link_relocate
:
3417 /* COFF relocs don't use the addend. The addend is used for
3418 R_SH_DIR32 to be compatible with other compilers. */
3419 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3420 contents
, rel
->r_offset
,
3421 relocation
, addend
);
3425 goto final_link_relocate
;
3430 /* If the reloc is against the start of this section, then
3431 the assembler has already taken care of it and the reloc
3432 is here only to assist in relaxing. If the reloc is not
3433 against the start of this section, then it's against an
3434 external symbol and we must deal with it ourselves. */
3435 if (input_section
->output_section
->vma
+ input_section
->output_offset
3438 int disp
= (relocation
3439 - input_section
->output_section
->vma
3440 - input_section
->output_offset
3446 case R_SH_DIR8WPZ
: mask
= 1; break;
3447 case R_SH_DIR8WPL
: mask
= 3; break;
3448 default: mask
= 0; break;
3452 ((*_bfd_error_handler
)
3453 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
3454 input_section
->owner
,
3455 (unsigned long) rel
->r_offset
));
3456 bfd_set_error (bfd_error_bad_value
);
3460 goto final_link_relocate
;
3466 #ifdef INCLUDE_SHMEDIA
3467 if (shmedia_prepare_reloc (info
, input_bfd
, input_section
,
3468 contents
, rel
, &relocation
))
3469 goto final_link_relocate
;
3471 bfd_set_error (bfd_error_bad_value
);
3479 goto final_link_relocate
;
3485 ((*_bfd_error_handler
)
3486 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3487 input_section
->owner
,
3488 (unsigned long) rel
->r_offset
, howto
->name
,
3489 (unsigned long) relocation
));
3490 bfd_set_error (bfd_error_bad_value
);
3493 goto final_link_relocate
;
3500 ((*_bfd_error_handler
)
3501 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3502 input_section
->owner
,
3503 (unsigned long) rel
->r_offset
, howto
->name
,
3504 (unsigned long) relocation
));
3505 bfd_set_error (bfd_error_bad_value
);
3508 goto final_link_relocate
;
3511 if ((signed int)relocation
< -32
3512 || (signed int)relocation
> 32)
3514 ((*_bfd_error_handler
)
3515 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
3516 input_section
->owner
,
3517 (unsigned long) rel
->r_offset
,
3518 (unsigned long) relocation
));
3519 bfd_set_error (bfd_error_bad_value
);
3522 goto final_link_relocate
;
3525 if ((signed int)relocation
< -16
3526 || (signed int)relocation
> 16)
3528 ((*_bfd_error_handler
)
3529 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
3530 input_section
->owner
,
3531 (unsigned long) rel
->r_offset
,
3532 (unsigned long) relocation
));
3533 bfd_set_error (bfd_error_bad_value
);
3536 goto final_link_relocate
;
3540 #ifdef INCLUDE_SHMEDIA
3541 case R_SH_IMM_LOW16_PCREL
:
3542 case R_SH_IMM_MEDLOW16_PCREL
:
3543 case R_SH_IMM_MEDHI16_PCREL
:
3544 case R_SH_IMM_HI16_PCREL
:
3548 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3549 || h
->root
.type
!= bfd_link_hash_undefweak
)
3551 && (input_section
->flags
& SEC_ALLOC
) != 0
3552 && (r_type
== R_SH_DIR32
3553 || !SYMBOL_CALLS_LOCAL (info
, h
)))
3555 Elf_Internal_Rela outrel
;
3557 bfd_boolean skip
, relocate
;
3559 /* When generating a shared object, these relocations
3560 are copied into the output file to be resolved at run
3567 name
= (bfd_elf_string_from_elf_section
3569 elf_elfheader (input_bfd
)->e_shstrndx
,
3570 elf_section_data (input_section
)->rel_hdr
.sh_name
));
3574 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
3575 && strcmp (bfd_get_section_name (input_bfd
,
3579 sreloc
= bfd_get_section_by_name (dynobj
, name
);
3580 BFD_ASSERT (sreloc
!= NULL
);
3587 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3589 if (outrel
.r_offset
== (bfd_vma
) -1)
3591 else if (outrel
.r_offset
== (bfd_vma
) -2)
3592 skip
= TRUE
, relocate
= TRUE
;
3593 outrel
.r_offset
+= (input_section
->output_section
->vma
3594 + input_section
->output_offset
);
3597 memset (&outrel
, 0, sizeof outrel
);
3598 else if (r_type
== R_SH_REL32
)
3600 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3601 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
3603 = (howto
->partial_inplace
3604 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
3607 #ifdef INCLUDE_SHMEDIA
3608 else if (r_type
== R_SH_IMM_LOW16_PCREL
3609 || r_type
== R_SH_IMM_MEDLOW16_PCREL
3610 || r_type
== R_SH_IMM_MEDHI16_PCREL
3611 || r_type
== R_SH_IMM_HI16_PCREL
)
3613 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3614 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
3615 outrel
.r_addend
= addend
;
3620 /* h->dynindx may be -1 if this symbol was marked to
3623 || ((info
->symbolic
|| h
->dynindx
== -1)
3626 relocate
= howto
->partial_inplace
;
3627 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3631 BFD_ASSERT (h
->dynindx
!= -1);
3632 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
3634 outrel
.r_addend
= relocation
;
3636 += (howto
->partial_inplace
3637 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
3641 loc
= sreloc
->contents
;
3642 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3643 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3645 /* If this reloc is against an external symbol, we do
3646 not want to fiddle with the addend. Otherwise, we
3647 need to include the symbol value so that it becomes
3648 an addend for the dynamic reloc. */
3652 goto final_link_relocate
;
3655 #ifdef INCLUDE_SHMEDIA
3656 case R_SH_GOTPLT_LOW16
:
3657 case R_SH_GOTPLT_MEDLOW16
:
3658 case R_SH_GOTPLT_MEDHI16
:
3659 case R_SH_GOTPLT_HI16
:
3660 case R_SH_GOTPLT10BY4
:
3661 case R_SH_GOTPLT10BY8
:
3663 /* Relocation is to the entry for this symbol in the
3664 procedure linkage table. */
3671 || h
->plt
.offset
== (bfd_vma
) -1
3672 || h
->got
.offset
!= (bfd_vma
) -1)
3675 /* Relocation is to the entry for this symbol in the global
3676 offset table extension for the procedure linkage table. */
3678 BFD_ASSERT (sgotplt
!= NULL
);
3679 relocation
= (sgotplt
->output_offset
3680 + (get_plt_index (htab
->plt_info
, h
->plt
.offset
)
3684 relocation
-= GOT_BIAS
;
3687 goto final_link_relocate
;
3691 #ifdef INCLUDE_SHMEDIA
3692 case R_SH_GOT_LOW16
:
3693 case R_SH_GOT_MEDLOW16
:
3694 case R_SH_GOT_MEDHI16
:
3699 /* Relocation is to the entry for this symbol in the global
3702 BFD_ASSERT (sgot
!= NULL
);
3708 off
= h
->got
.offset
;
3709 #ifdef INCLUDE_SHMEDIA
3710 if (seen_stt_datalabel
)
3712 struct elf_sh_link_hash_entry
*hsh
;
3714 hsh
= (struct elf_sh_link_hash_entry
*)h
;
3715 off
= hsh
->datalabel_got
.offset
;
3718 BFD_ASSERT (off
!= (bfd_vma
) -1);
3720 dyn
= htab
->root
.dynamic_sections_created
;
3721 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3723 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3724 || (ELF_ST_VISIBILITY (h
->other
)
3725 && h
->root
.type
== bfd_link_hash_undefweak
))
3727 /* This is actually a static link, or it is a
3728 -Bsymbolic link and the symbol is defined
3729 locally, or the symbol was forced to be local
3730 because of a version file. We must initialize
3731 this entry in the global offset table. Since the
3732 offset must always be a multiple of 4, we use the
3733 least significant bit to record whether we have
3734 initialized it already.
3736 When doing a dynamic link, we create a .rela.got
3737 relocation entry to initialize the value. This
3738 is done in the finish_dynamic_symbol routine. */
3743 bfd_put_32 (output_bfd
, relocation
,
3744 sgot
->contents
+ off
);
3745 #ifdef INCLUDE_SHMEDIA
3746 if (seen_stt_datalabel
)
3748 struct elf_sh_link_hash_entry
*hsh
;
3750 hsh
= (struct elf_sh_link_hash_entry
*)h
;
3751 hsh
->datalabel_got
.offset
|= 1;
3759 relocation
= sgot
->output_offset
+ off
;
3763 #ifdef INCLUDE_SHMEDIA
3766 BFD_ASSERT (local_got_offsets
!= NULL
3767 && (local_got_offsets
[symtab_hdr
->sh_info
3771 off
= local_got_offsets
[symtab_hdr
->sh_info
3777 BFD_ASSERT (local_got_offsets
!= NULL
3778 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
3780 off
= local_got_offsets
[r_symndx
];
3781 #ifdef INCLUDE_SHMEDIA
3785 /* The offset must always be a multiple of 4. We use
3786 the least significant bit to record whether we have
3787 already generated the necessary reloc. */
3792 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
3796 Elf_Internal_Rela outrel
;
3799 if (srelgot
== NULL
)
3801 srelgot
= bfd_get_section_by_name (dynobj
,
3803 BFD_ASSERT (srelgot
!= NULL
);
3806 outrel
.r_offset
= (sgot
->output_section
->vma
3807 + sgot
->output_offset
3809 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
3810 outrel
.r_addend
= relocation
;
3811 loc
= srelgot
->contents
;
3812 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
3813 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
3816 #ifdef INCLUDE_SHMEDIA
3818 local_got_offsets
[symtab_hdr
->sh_info
+ r_symndx
] |= 1;
3821 local_got_offsets
[r_symndx
] |= 1;
3824 relocation
= sgot
->output_offset
+ off
;
3828 relocation
-= GOT_BIAS
;
3831 goto final_link_relocate
;
3834 #ifdef INCLUDE_SHMEDIA
3835 case R_SH_GOTOFF_LOW16
:
3836 case R_SH_GOTOFF_MEDLOW16
:
3837 case R_SH_GOTOFF_MEDHI16
:
3838 case R_SH_GOTOFF_HI16
:
3840 /* Relocation is relative to the start of the global offset
3843 BFD_ASSERT (sgot
!= NULL
);
3845 /* Note that sgot->output_offset is not involved in this
3846 calculation. We always want the start of .got. If we
3847 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3848 permitted by the ABI, we might have to change this
3850 relocation
-= sgot
->output_section
->vma
;
3853 relocation
-= GOT_BIAS
;
3856 addend
= rel
->r_addend
;
3858 goto final_link_relocate
;
3861 #ifdef INCLUDE_SHMEDIA
3862 case R_SH_GOTPC_LOW16
:
3863 case R_SH_GOTPC_MEDLOW16
:
3864 case R_SH_GOTPC_MEDHI16
:
3865 case R_SH_GOTPC_HI16
:
3867 /* Use global offset table as symbol value. */
3869 BFD_ASSERT (sgot
!= NULL
);
3870 relocation
= sgot
->output_section
->vma
;
3873 relocation
+= GOT_BIAS
;
3876 addend
= rel
->r_addend
;
3878 goto final_link_relocate
;
3881 #ifdef INCLUDE_SHMEDIA
3882 case R_SH_PLT_LOW16
:
3883 case R_SH_PLT_MEDLOW16
:
3884 case R_SH_PLT_MEDHI16
:
3887 /* Relocation is to the entry for this symbol in the
3888 procedure linkage table. */
3890 /* Resolve a PLT reloc against a local symbol directly,
3891 without using the procedure linkage table. */
3893 goto final_link_relocate
;
3895 if (h
->forced_local
)
3896 goto final_link_relocate
;
3898 if (h
->plt
.offset
== (bfd_vma
) -1)
3900 /* We didn't make a PLT entry for this symbol. This
3901 happens when statically linking PIC code, or when
3902 using -Bsymbolic. */
3903 goto final_link_relocate
;
3906 BFD_ASSERT (splt
!= NULL
);
3907 relocation
= (splt
->output_section
->vma
3908 + splt
->output_offset
3911 #ifdef INCLUDE_SHMEDIA
3915 addend
= rel
->r_addend
;
3917 goto final_link_relocate
;
3919 case R_SH_LOOP_START
:
3921 static bfd_vma start
, end
;
3923 start
= (relocation
+ rel
->r_addend
3924 - (sec
->output_section
->vma
+ sec
->output_offset
));
3925 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3926 rel
->r_offset
, sec
, start
, end
);
3930 end
= (relocation
+ rel
->r_addend
3931 - (sec
->output_section
->vma
+ sec
->output_offset
));
3932 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
3933 rel
->r_offset
, sec
, start
, end
);
3937 case R_SH_TLS_GD_32
:
3938 case R_SH_TLS_IE_32
:
3939 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
3940 tls_type
= GOT_UNKNOWN
;
3941 if (h
== NULL
&& local_got_offsets
)
3942 tls_type
= sh_elf_local_got_tls_type (input_bfd
) [r_symndx
];
3945 tls_type
= sh_elf_hash_entry (h
)->tls_type
;
3947 && (h
->dynindx
== -1
3949 r_type
= R_SH_TLS_LE_32
;
3952 if (r_type
== R_SH_TLS_GD_32
&& tls_type
== GOT_TLS_IE
)
3953 r_type
= R_SH_TLS_IE_32
;
3955 if (r_type
== R_SH_TLS_LE_32
)
3958 unsigned short insn
;
3960 if (ELF32_R_TYPE (rel
->r_info
) == R_SH_TLS_GD_32
)
3962 /* GD->LE transition:
3963 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
3964 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
3965 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
3967 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
3969 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
3971 offset
= rel
->r_offset
;
3972 BFD_ASSERT (offset
>= 16);
3973 /* Size of GD instructions is 16 or 18. */
3975 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
3976 if ((insn
& 0xff00) == 0xc700)
3978 BFD_ASSERT (offset
>= 2);
3980 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
3983 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
3984 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
3985 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
3986 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
3987 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
3988 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
3989 BFD_ASSERT (insn
== 0x310c);
3990 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
3991 BFD_ASSERT (insn
== 0x410b);
3992 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
3993 BFD_ASSERT (insn
== 0x34cc);
3995 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 2);
3996 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 4);
3997 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
3998 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
3999 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4005 /* IE->LE transition:
4006 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
4007 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
4009 mov.l .Ln,rM; stc gbr,rN; nop; ...;
4012 offset
= rel
->r_offset
;
4013 BFD_ASSERT (offset
>= 16);
4014 /* Size of IE instructions is 10 or 12. */
4016 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4017 if ((insn
& 0xf0ff) == 0x0012)
4019 BFD_ASSERT (offset
>= 2);
4021 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4024 BFD_ASSERT ((insn
& 0xff00) == 0xd000);
4025 index
= insn
& 0x00ff;
4026 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4027 BFD_ASSERT ((insn
& 0xf0ff) == 0x0012);
4028 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4029 BFD_ASSERT ((insn
& 0xf0ff) == 0x00ce);
4030 insn
= 0xd000 | (insn
& 0x0f00) | index
;
4031 bfd_put_16 (output_bfd
, insn
, contents
+ offset
+ 0);
4032 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4035 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4036 contents
+ rel
->r_offset
);
4045 off
= h
->got
.offset
;
4048 if (local_got_offsets
== NULL
)
4051 off
= local_got_offsets
[r_symndx
];
4054 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4055 if (r_type
== R_SH_TLS_IE_32
4056 && ! htab
->root
.dynamic_sections_created
)
4059 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4060 sgot
->contents
+ off
);
4061 bfd_put_32 (output_bfd
, sgot
->output_offset
+ off
,
4062 contents
+ rel
->r_offset
);
4070 Elf_Internal_Rela outrel
;
4074 if (srelgot
== NULL
)
4076 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
4077 BFD_ASSERT (srelgot
!= NULL
);
4080 outrel
.r_offset
= (sgot
->output_section
->vma
4081 + sgot
->output_offset
+ off
);
4083 if (h
== NULL
|| h
->dynindx
== -1)
4088 dr_type
= (r_type
== R_SH_TLS_GD_32
? R_SH_TLS_DTPMOD32
:
4090 if (dr_type
== R_SH_TLS_TPOFF32
&& indx
== 0)
4091 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4093 outrel
.r_addend
= 0;
4094 outrel
.r_info
= ELF32_R_INFO (indx
, dr_type
);
4095 loc
= srelgot
->contents
;
4096 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4097 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4099 if (r_type
== R_SH_TLS_GD_32
)
4103 bfd_put_32 (output_bfd
,
4104 relocation
- dtpoff_base (info
),
4105 sgot
->contents
+ off
+ 4);
4109 outrel
.r_info
= ELF32_R_INFO (indx
,
4111 outrel
.r_offset
+= 4;
4112 outrel
.r_addend
= 0;
4113 srelgot
->reloc_count
++;
4114 loc
+= sizeof (Elf32_External_Rela
);
4115 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4122 local_got_offsets
[r_symndx
] |= 1;
4125 if (off
>= (bfd_vma
) -2)
4128 if (r_type
== (int) ELF32_R_TYPE (rel
->r_info
))
4129 relocation
= sgot
->output_offset
+ off
;
4133 unsigned short insn
;
4135 /* GD->IE transition:
4136 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4137 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4138 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4140 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4141 nop; nop; bra 3f; nop; .align 2;
4142 1: .long x@TPOFF; 2:...; 3:. */
4144 offset
= rel
->r_offset
;
4145 BFD_ASSERT (offset
>= 16);
4146 /* Size of GD instructions is 16 or 18. */
4148 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4149 if ((insn
& 0xff00) == 0xc700)
4151 BFD_ASSERT (offset
>= 2);
4153 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4156 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4158 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4159 bfd_put_16 (output_bfd
, insn
& 0xf0ff, contents
+ offset
);
4161 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4162 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4163 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4164 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4165 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4166 BFD_ASSERT (insn
== 0x310c);
4167 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4168 BFD_ASSERT (insn
== 0x410b);
4169 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4170 BFD_ASSERT (insn
== 0x34cc);
4172 bfd_put_16 (output_bfd
, 0x0412, contents
+ offset
+ 2);
4173 bfd_put_16 (output_bfd
, 0x00ce, contents
+ offset
+ 4);
4174 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 6);
4175 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4176 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4178 bfd_put_32 (output_bfd
, sgot
->output_offset
+ off
,
4179 contents
+ rel
->r_offset
);
4184 addend
= rel
->r_addend
;
4186 goto final_link_relocate
;
4188 case R_SH_TLS_LD_32
:
4192 unsigned short insn
;
4194 /* LD->LE transition:
4195 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4196 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4197 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4199 stc gbr,r0; nop; nop; nop;
4200 nop; nop; bra 3f; ...; 3:. */
4202 offset
= rel
->r_offset
;
4203 BFD_ASSERT (offset
>= 16);
4204 /* Size of LD instructions is 16 or 18. */
4206 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4207 if ((insn
& 0xff00) == 0xc700)
4209 BFD_ASSERT (offset
>= 2);
4211 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4214 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4215 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4216 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4217 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4218 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4219 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4220 BFD_ASSERT (insn
== 0x310c);
4221 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4222 BFD_ASSERT (insn
== 0x410b);
4223 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4224 BFD_ASSERT (insn
== 0x34cc);
4226 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 0);
4227 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 2);
4228 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4229 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
4230 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4231 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4240 off
= htab
->tls_ldm_got
.offset
;
4245 Elf_Internal_Rela outrel
;
4248 srelgot
= htab
->srelgot
;
4249 if (srelgot
== NULL
)
4252 outrel
.r_offset
= (sgot
->output_section
->vma
4253 + sgot
->output_offset
+ off
);
4254 outrel
.r_addend
= 0;
4255 outrel
.r_info
= ELF32_R_INFO (0, R_SH_TLS_DTPMOD32
);
4256 loc
= srelgot
->contents
;
4257 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4258 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4259 htab
->tls_ldm_got
.offset
|= 1;
4262 relocation
= sgot
->output_offset
+ off
;
4263 addend
= rel
->r_addend
;
4265 goto final_link_relocate
;
4267 case R_SH_TLS_LDO_32
:
4269 relocation
= tpoff (info
, relocation
);
4271 relocation
-= dtpoff_base (info
);
4273 addend
= rel
->r_addend
;
4274 goto final_link_relocate
;
4276 case R_SH_TLS_LE_32
:
4279 Elf_Internal_Rela outrel
;
4284 relocation
= tpoff (info
, relocation
);
4285 addend
= rel
->r_addend
;
4286 goto final_link_relocate
;
4293 name
= (bfd_elf_string_from_elf_section
4295 elf_elfheader (input_bfd
)->e_shstrndx
,
4296 elf_section_data (input_section
)->rel_hdr
.sh_name
));
4300 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
4301 && strcmp (bfd_get_section_name (input_bfd
,
4305 sreloc
= bfd_get_section_by_name (dynobj
, name
);
4306 BFD_ASSERT (sreloc
!= NULL
);
4309 if (h
== NULL
|| h
->dynindx
== -1)
4314 outrel
.r_offset
= (input_section
->output_section
->vma
4315 + input_section
->output_offset
4317 outrel
.r_info
= ELF32_R_INFO (indx
, R_SH_TLS_TPOFF32
);
4319 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4321 outrel
.r_addend
= 0;
4323 loc
= sreloc
->contents
;
4324 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4325 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4331 if (r
!= bfd_reloc_ok
)
4336 case bfd_reloc_outofrange
:
4338 case bfd_reloc_overflow
:
4346 name
= (bfd_elf_string_from_elf_section
4347 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
4351 name
= bfd_section_name (input_bfd
, sec
);
4353 if (! ((*info
->callbacks
->reloc_overflow
)
4354 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
4355 (bfd_vma
) 0, input_bfd
, input_section
,
4367 /* This is a version of bfd_generic_get_relocated_section_contents
4368 which uses sh_elf_relocate_section. */
4371 sh_elf_get_relocated_section_contents (bfd
*output_bfd
,
4372 struct bfd_link_info
*link_info
,
4373 struct bfd_link_order
*link_order
,
4375 bfd_boolean relocatable
,
4378 Elf_Internal_Shdr
*symtab_hdr
;
4379 asection
*input_section
= link_order
->u
.indirect
.section
;
4380 bfd
*input_bfd
= input_section
->owner
;
4381 asection
**sections
= NULL
;
4382 Elf_Internal_Rela
*internal_relocs
= NULL
;
4383 Elf_Internal_Sym
*isymbuf
= NULL
;
4385 /* We only need to handle the case of relaxing, or of having a
4386 particular set of section contents, specially. */
4388 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
4389 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
4394 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
4396 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
4397 (size_t) input_section
->size
);
4399 if ((input_section
->flags
& SEC_RELOC
) != 0
4400 && input_section
->reloc_count
> 0)
4403 Elf_Internal_Sym
*isym
, *isymend
;
4406 internal_relocs
= (_bfd_elf_link_read_relocs
4407 (input_bfd
, input_section
, NULL
,
4408 (Elf_Internal_Rela
*) NULL
, FALSE
));
4409 if (internal_relocs
== NULL
)
4412 if (symtab_hdr
->sh_info
!= 0)
4414 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
4415 if (isymbuf
== NULL
)
4416 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
4417 symtab_hdr
->sh_info
, 0,
4419 if (isymbuf
== NULL
)
4423 amt
= symtab_hdr
->sh_info
;
4424 amt
*= sizeof (asection
*);
4425 sections
= (asection
**) bfd_malloc (amt
);
4426 if (sections
== NULL
&& amt
!= 0)
4429 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
4430 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
4434 if (isym
->st_shndx
== SHN_UNDEF
)
4435 isec
= bfd_und_section_ptr
;
4436 else if (isym
->st_shndx
== SHN_ABS
)
4437 isec
= bfd_abs_section_ptr
;
4438 else if (isym
->st_shndx
== SHN_COMMON
)
4439 isec
= bfd_com_section_ptr
;
4441 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
4446 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
4447 input_section
, data
, internal_relocs
,
4451 if (sections
!= NULL
)
4454 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4456 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
4457 free (internal_relocs
);
4463 if (sections
!= NULL
)
4466 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
4468 if (internal_relocs
!= NULL
4469 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
4470 free (internal_relocs
);
4474 /* Return the base VMA address which should be subtracted from real addresses
4475 when resolving @dtpoff relocation.
4476 This is PT_TLS segment p_vaddr. */
4479 dtpoff_base (struct bfd_link_info
*info
)
4481 /* If tls_sec is NULL, we should have signalled an error already. */
4482 if (elf_hash_table (info
)->tls_sec
== NULL
)
4484 return elf_hash_table (info
)->tls_sec
->vma
;
4487 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
4490 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
4492 /* If tls_sec is NULL, we should have signalled an error already. */
4493 if (elf_hash_table (info
)->tls_sec
== NULL
)
4495 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
4496 structure which has 2 pointer fields. */
4497 return (address
- elf_hash_table (info
)->tls_sec
->vma
4498 + align_power ((bfd_vma
) 8,
4499 elf_hash_table (info
)->tls_sec
->alignment_power
));
4503 sh_elf_gc_mark_hook (asection
*sec
,
4504 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
4505 Elf_Internal_Rela
*rel
, struct elf_link_hash_entry
*h
,
4506 Elf_Internal_Sym
*sym
)
4510 switch (ELF32_R_TYPE (rel
->r_info
))
4512 case R_SH_GNU_VTINHERIT
:
4513 case R_SH_GNU_VTENTRY
:
4517 #ifdef INCLUDE_SHMEDIA
4518 while (h
->root
.type
== bfd_link_hash_indirect
4519 && h
->root
.u
.i
.link
)
4520 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4522 switch (h
->root
.type
)
4524 case bfd_link_hash_defined
:
4525 case bfd_link_hash_defweak
:
4526 return h
->root
.u
.def
.section
;
4528 case bfd_link_hash_common
:
4529 return h
->root
.u
.c
.p
->section
;
4537 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
4542 /* Update the got entry reference counts for the section being removed. */
4545 sh_elf_gc_sweep_hook (bfd
*abfd
, struct bfd_link_info
*info
,
4546 asection
*sec
, const Elf_Internal_Rela
*relocs
)
4548 Elf_Internal_Shdr
*symtab_hdr
;
4549 struct elf_link_hash_entry
**sym_hashes
;
4550 bfd_signed_vma
*local_got_refcounts
;
4551 const Elf_Internal_Rela
*rel
, *relend
;
4553 elf_section_data (sec
)->local_dynrel
= NULL
;
4555 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4556 sym_hashes
= elf_sym_hashes (abfd
);
4557 local_got_refcounts
= elf_local_got_refcounts (abfd
);
4559 relend
= relocs
+ sec
->reloc_count
;
4560 for (rel
= relocs
; rel
< relend
; rel
++)
4562 unsigned long r_symndx
;
4563 unsigned int r_type
;
4564 struct elf_link_hash_entry
*h
= NULL
;
4565 #ifdef INCLUDE_SHMEDIA
4566 int seen_stt_datalabel
= 0;
4569 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4570 if (r_symndx
>= symtab_hdr
->sh_info
)
4572 struct elf_sh_link_hash_entry
*eh
;
4573 struct elf_sh_dyn_relocs
**pp
;
4574 struct elf_sh_dyn_relocs
*p
;
4576 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4577 while (h
->root
.type
== bfd_link_hash_indirect
4578 || h
->root
.type
== bfd_link_hash_warning
)
4580 #ifdef INCLUDE_SHMEDIA
4581 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
4583 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4585 eh
= (struct elf_sh_link_hash_entry
*) h
;
4586 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
4589 /* Everything must go for SEC. */
4595 r_type
= ELF32_R_TYPE (rel
->r_info
);
4596 switch (sh_elf_optimized_tls_reloc (info
, r_type
, h
!= NULL
))
4598 case R_SH_TLS_LD_32
:
4599 if (sh_elf_hash_table (info
)->tls_ldm_got
.refcount
> 0)
4600 sh_elf_hash_table (info
)->tls_ldm_got
.refcount
-= 1;
4606 #ifdef INCLUDE_SHMEDIA
4607 case R_SH_GOT_LOW16
:
4608 case R_SH_GOT_MEDLOW16
:
4609 case R_SH_GOT_MEDHI16
:
4613 case R_SH_GOTOFF_LOW16
:
4614 case R_SH_GOTOFF_MEDLOW16
:
4615 case R_SH_GOTOFF_MEDHI16
:
4616 case R_SH_GOTOFF_HI16
:
4617 case R_SH_GOTPC_LOW16
:
4618 case R_SH_GOTPC_MEDLOW16
:
4619 case R_SH_GOTPC_MEDHI16
:
4620 case R_SH_GOTPC_HI16
:
4622 case R_SH_TLS_GD_32
:
4623 case R_SH_TLS_IE_32
:
4626 #ifdef INCLUDE_SHMEDIA
4627 if (seen_stt_datalabel
)
4629 struct elf_sh_link_hash_entry
*eh
;
4630 eh
= (struct elf_sh_link_hash_entry
*) h
;
4631 if (eh
->datalabel_got
.refcount
> 0)
4632 eh
->datalabel_got
.refcount
-= 1;
4636 if (h
->got
.refcount
> 0)
4637 h
->got
.refcount
-= 1;
4639 else if (local_got_refcounts
!= NULL
)
4641 #ifdef INCLUDE_SHMEDIA
4642 if (rel
->r_addend
& 1)
4644 if (local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] > 0)
4645 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] -= 1;
4649 if (local_got_refcounts
[r_symndx
] > 0)
4650 local_got_refcounts
[r_symndx
] -= 1;
4661 #ifdef INCLUDE_SHMEDIA
4662 case R_SH_PLT_LOW16
:
4663 case R_SH_PLT_MEDLOW16
:
4664 case R_SH_PLT_MEDHI16
:
4669 if (h
->plt
.refcount
> 0)
4670 h
->plt
.refcount
-= 1;
4675 #ifdef INCLUDE_SHMEDIA
4676 case R_SH_GOTPLT_LOW16
:
4677 case R_SH_GOTPLT_MEDLOW16
:
4678 case R_SH_GOTPLT_MEDHI16
:
4679 case R_SH_GOTPLT_HI16
:
4680 case R_SH_GOTPLT10BY4
:
4681 case R_SH_GOTPLT10BY8
:
4685 struct elf_sh_link_hash_entry
*eh
;
4686 eh
= (struct elf_sh_link_hash_entry
*) h
;
4687 if (eh
->gotplt_refcount
> 0)
4689 eh
->gotplt_refcount
-= 1;
4690 if (h
->plt
.refcount
> 0)
4691 h
->plt
.refcount
-= 1;
4693 #ifdef INCLUDE_SHMEDIA
4694 else if (seen_stt_datalabel
)
4696 if (eh
->datalabel_got
.refcount
> 0)
4697 eh
->datalabel_got
.refcount
-= 1;
4700 else if (h
->got
.refcount
> 0)
4701 h
->got
.refcount
-= 1;
4703 else if (local_got_refcounts
!= NULL
)
4705 #ifdef INCLUDE_SHMEDIA
4706 if (rel
->r_addend
& 1)
4708 if (local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] > 0)
4709 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] -= 1;
4713 if (local_got_refcounts
[r_symndx
] > 0)
4714 local_got_refcounts
[r_symndx
] -= 1;
4726 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4729 sh_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
4730 struct elf_link_hash_entry
*dir
,
4731 struct elf_link_hash_entry
*ind
)
4733 struct elf_sh_link_hash_entry
*edir
, *eind
;
4735 edir
= (struct elf_sh_link_hash_entry
*) dir
;
4736 eind
= (struct elf_sh_link_hash_entry
*) ind
;
4738 if (eind
->dyn_relocs
!= NULL
)
4740 if (edir
->dyn_relocs
!= NULL
)
4742 struct elf_sh_dyn_relocs
**pp
;
4743 struct elf_sh_dyn_relocs
*p
;
4745 /* Add reloc counts against the indirect sym to the direct sym
4746 list. Merge any entries against the same section. */
4747 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
4749 struct elf_sh_dyn_relocs
*q
;
4751 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
4752 if (q
->sec
== p
->sec
)
4754 q
->pc_count
+= p
->pc_count
;
4755 q
->count
+= p
->count
;
4762 *pp
= edir
->dyn_relocs
;
4765 edir
->dyn_relocs
= eind
->dyn_relocs
;
4766 eind
->dyn_relocs
= NULL
;
4768 edir
->gotplt_refcount
= eind
->gotplt_refcount
;
4769 eind
->gotplt_refcount
= 0;
4770 #ifdef INCLUDE_SHMEDIA
4771 edir
->datalabel_got
.refcount
+= eind
->datalabel_got
.refcount
;
4772 eind
->datalabel_got
.refcount
= 0;
4775 if (ind
->root
.type
== bfd_link_hash_indirect
4776 && dir
->got
.refcount
<= 0)
4778 edir
->tls_type
= eind
->tls_type
;
4779 eind
->tls_type
= GOT_UNKNOWN
;
4782 if (ind
->root
.type
!= bfd_link_hash_indirect
4783 && dir
->dynamic_adjusted
)
4785 /* If called to transfer flags for a weakdef during processing
4786 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
4787 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4788 dir
->ref_dynamic
|= ind
->ref_dynamic
;
4789 dir
->ref_regular
|= ind
->ref_regular
;
4790 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
4791 dir
->needs_plt
|= ind
->needs_plt
;
4794 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
4798 sh_elf_optimized_tls_reloc (struct bfd_link_info
*info
, int r_type
,
4806 case R_SH_TLS_GD_32
:
4807 case R_SH_TLS_IE_32
:
4809 return R_SH_TLS_LE_32
;
4810 return R_SH_TLS_IE_32
;
4811 case R_SH_TLS_LD_32
:
4812 return R_SH_TLS_LE_32
;
4818 /* Look through the relocs for a section during the first phase.
4819 Since we don't do .gots or .plts, we just need to consider the
4820 virtual table relocs for gc. */
4823 sh_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
4824 const Elf_Internal_Rela
*relocs
)
4826 Elf_Internal_Shdr
*symtab_hdr
;
4827 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
4828 struct elf_sh_link_hash_table
*htab
;
4829 const Elf_Internal_Rela
*rel
;
4830 const Elf_Internal_Rela
*rel_end
;
4831 bfd_vma
*local_got_offsets
;
4835 unsigned int r_type
;
4836 int tls_type
, old_tls_type
;
4842 if (info
->relocatable
)
4845 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4846 sym_hashes
= elf_sym_hashes (abfd
);
4847 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
4848 if (!elf_bad_symtab (abfd
))
4849 sym_hashes_end
-= symtab_hdr
->sh_info
;
4851 htab
= sh_elf_hash_table (info
);
4852 local_got_offsets
= elf_local_got_offsets (abfd
);
4854 rel_end
= relocs
+ sec
->reloc_count
;
4855 for (rel
= relocs
; rel
< rel_end
; rel
++)
4857 struct elf_link_hash_entry
*h
;
4858 unsigned long r_symndx
;
4859 #ifdef INCLUDE_SHMEDIA
4860 int seen_stt_datalabel
= 0;
4863 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4864 r_type
= ELF32_R_TYPE (rel
->r_info
);
4866 if (r_symndx
< symtab_hdr
->sh_info
)
4870 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4871 while (h
->root
.type
== bfd_link_hash_indirect
4872 || h
->root
.type
== bfd_link_hash_warning
)
4874 #ifdef INCLUDE_SHMEDIA
4875 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
4877 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4881 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
4883 && r_type
== R_SH_TLS_IE_32
4885 && h
->root
.type
!= bfd_link_hash_undefined
4886 && h
->root
.type
!= bfd_link_hash_undefweak
4887 && (h
->dynindx
== -1
4889 r_type
= R_SH_TLS_LE_32
;
4891 /* Some relocs require a global offset table. */
4892 if (htab
->sgot
== NULL
)
4900 #ifdef INCLUDE_SHMEDIA
4901 case R_SH_GOTPLT_LOW16
:
4902 case R_SH_GOTPLT_MEDLOW16
:
4903 case R_SH_GOTPLT_MEDHI16
:
4904 case R_SH_GOTPLT_HI16
:
4905 case R_SH_GOTPLT10BY4
:
4906 case R_SH_GOTPLT10BY8
:
4907 case R_SH_GOT_LOW16
:
4908 case R_SH_GOT_MEDLOW16
:
4909 case R_SH_GOT_MEDHI16
:
4913 case R_SH_GOTOFF_LOW16
:
4914 case R_SH_GOTOFF_MEDLOW16
:
4915 case R_SH_GOTOFF_MEDHI16
:
4916 case R_SH_GOTOFF_HI16
:
4917 case R_SH_GOTPC_LOW16
:
4918 case R_SH_GOTPC_MEDLOW16
:
4919 case R_SH_GOTPC_MEDHI16
:
4920 case R_SH_GOTPC_HI16
:
4922 case R_SH_TLS_GD_32
:
4923 case R_SH_TLS_LD_32
:
4924 case R_SH_TLS_IE_32
:
4925 if (htab
->sgot
== NULL
)
4927 if (htab
->root
.dynobj
== NULL
)
4928 htab
->root
.dynobj
= abfd
;
4929 if (!create_got_section (htab
->root
.dynobj
, info
))
4941 /* This relocation describes the C++ object vtable hierarchy.
4942 Reconstruct it for later use during GC. */
4943 case R_SH_GNU_VTINHERIT
:
4944 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
4948 /* This relocation describes which C++ vtable entries are actually
4949 used. Record for later use during GC. */
4950 case R_SH_GNU_VTENTRY
:
4951 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
4955 case R_SH_TLS_IE_32
:
4957 info
->flags
|= DF_STATIC_TLS
;
4961 case R_SH_TLS_GD_32
:
4963 #ifdef INCLUDE_SHMEDIA
4964 case R_SH_GOT_LOW16
:
4965 case R_SH_GOT_MEDLOW16
:
4966 case R_SH_GOT_MEDHI16
:
4974 tls_type
= GOT_NORMAL
;
4976 case R_SH_TLS_GD_32
:
4977 tls_type
= GOT_TLS_GD
;
4979 case R_SH_TLS_IE_32
:
4980 tls_type
= GOT_TLS_IE
;
4986 #ifdef INCLUDE_SHMEDIA
4987 if (seen_stt_datalabel
)
4989 struct elf_sh_link_hash_entry
*eh
4990 = (struct elf_sh_link_hash_entry
*) h
;
4992 eh
->datalabel_got
.refcount
+= 1;
4996 h
->got
.refcount
+= 1;
4997 old_tls_type
= sh_elf_hash_entry (h
)->tls_type
;
5001 bfd_signed_vma
*local_got_refcounts
;
5003 /* This is a global offset table entry for a local
5005 local_got_refcounts
= elf_local_got_refcounts (abfd
);
5006 if (local_got_refcounts
== NULL
)
5010 size
= symtab_hdr
->sh_info
;
5011 size
*= sizeof (bfd_signed_vma
);
5012 #ifdef INCLUDE_SHMEDIA
5013 /* Reserve space for both the datalabel and
5014 codelabel local GOT offsets. */
5017 size
+= symtab_hdr
->sh_info
;
5018 local_got_refcounts
= ((bfd_signed_vma
*)
5019 bfd_zalloc (abfd
, size
));
5020 if (local_got_refcounts
== NULL
)
5022 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
5023 #ifdef INCLUDE_SHMEDIA
5024 /* Take care of both the datalabel and codelabel local
5026 sh_elf_local_got_tls_type (abfd
)
5027 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
5029 sh_elf_local_got_tls_type (abfd
)
5030 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
5033 #ifdef INCLUDE_SHMEDIA
5034 if (rel
->r_addend
& 1)
5035 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] += 1;
5038 local_got_refcounts
[r_symndx
] += 1;
5039 old_tls_type
= sh_elf_local_got_tls_type (abfd
) [r_symndx
];
5042 /* If a TLS symbol is accessed using IE at least once,
5043 there is no point to use dynamic model for it. */
5044 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
5045 && (old_tls_type
!= GOT_TLS_GD
|| tls_type
!= GOT_TLS_IE
))
5047 if (old_tls_type
== GOT_TLS_IE
&& tls_type
== GOT_TLS_GD
)
5048 tls_type
= GOT_TLS_IE
;
5051 (*_bfd_error_handler
)
5052 (_("%B: `%s' accessed both as normal and thread local symbol"),
5053 abfd
, h
->root
.root
.string
);
5058 if (old_tls_type
!= tls_type
)
5061 sh_elf_hash_entry (h
)->tls_type
= tls_type
;
5063 sh_elf_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
5068 case R_SH_TLS_LD_32
:
5069 sh_elf_hash_table(info
)->tls_ldm_got
.refcount
+= 1;
5073 #ifdef INCLUDE_SHMEDIA
5074 case R_SH_GOTPLT_LOW16
:
5075 case R_SH_GOTPLT_MEDLOW16
:
5076 case R_SH_GOTPLT_MEDHI16
:
5077 case R_SH_GOTPLT_HI16
:
5078 case R_SH_GOTPLT10BY4
:
5079 case R_SH_GOTPLT10BY8
:
5081 /* If this is a local symbol, we resolve it directly without
5082 creating a procedure linkage table entry. */
5088 || h
->dynindx
== -1)
5092 h
->plt
.refcount
+= 1;
5093 ((struct elf_sh_link_hash_entry
*) h
)->gotplt_refcount
+= 1;
5098 #ifdef INCLUDE_SHMEDIA
5099 case R_SH_PLT_LOW16
:
5100 case R_SH_PLT_MEDLOW16
:
5101 case R_SH_PLT_MEDHI16
:
5104 /* This symbol requires a procedure linkage table entry. We
5105 actually build the entry in adjust_dynamic_symbol,
5106 because this might be a case of linking PIC code which is
5107 never referenced by a dynamic object, in which case we
5108 don't need to generate a procedure linkage table entry
5111 /* If this is a local symbol, we resolve it directly without
5112 creating a procedure linkage table entry. */
5116 if (h
->forced_local
)
5120 h
->plt
.refcount
+= 1;
5125 #ifdef INCLUDE_SHMEDIA
5126 case R_SH_IMM_LOW16_PCREL
:
5127 case R_SH_IMM_MEDLOW16_PCREL
:
5128 case R_SH_IMM_MEDHI16_PCREL
:
5129 case R_SH_IMM_HI16_PCREL
:
5131 if (h
!= NULL
&& ! info
->shared
)
5134 h
->plt
.refcount
+= 1;
5137 /* If we are creating a shared library, and this is a reloc
5138 against a global symbol, or a non PC relative reloc
5139 against a local symbol, then we need to copy the reloc
5140 into the shared library. However, if we are linking with
5141 -Bsymbolic, we do not need to copy a reloc against a
5142 global symbol which is defined in an object we are
5143 including in the link (i.e., DEF_REGULAR is set). At
5144 this point we have not seen all the input files, so it is
5145 possible that DEF_REGULAR is not set now but will be set
5146 later (it is never cleared). We account for that
5147 possibility below by storing information in the
5148 dyn_relocs field of the hash table entry. A similar
5149 situation occurs when creating shared libraries and symbol
5150 visibility changes render the symbol local.
5152 If on the other hand, we are creating an executable, we
5153 may need to keep relocations for symbols satisfied by a
5154 dynamic library if we manage to avoid copy relocs for the
5157 && (sec
->flags
& SEC_ALLOC
) != 0
5158 && (r_type
!= R_SH_REL32
5160 && (! info
->symbolic
5161 || h
->root
.type
== bfd_link_hash_defweak
5162 || !h
->def_regular
))))
5164 && (sec
->flags
& SEC_ALLOC
) != 0
5166 && (h
->root
.type
== bfd_link_hash_defweak
5167 || !h
->def_regular
)))
5169 struct elf_sh_dyn_relocs
*p
;
5170 struct elf_sh_dyn_relocs
**head
;
5172 if (htab
->root
.dynobj
== NULL
)
5173 htab
->root
.dynobj
= abfd
;
5175 /* When creating a shared object, we must copy these
5176 reloc types into the output file. We create a reloc
5177 section in dynobj and make room for this reloc. */
5182 name
= (bfd_elf_string_from_elf_section
5184 elf_elfheader (abfd
)->e_shstrndx
,
5185 elf_section_data (sec
)->rel_hdr
.sh_name
));
5189 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
5190 && strcmp (bfd_get_section_name (abfd
, sec
),
5193 sreloc
= bfd_get_section_by_name (htab
->root
.dynobj
, name
);
5198 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
5199 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
5200 if ((sec
->flags
& SEC_ALLOC
) != 0)
5201 flags
|= SEC_ALLOC
| SEC_LOAD
;
5202 sreloc
= bfd_make_section_with_flags (htab
->root
.dynobj
,
5206 || ! bfd_set_section_alignment (htab
->root
.dynobj
,
5210 elf_section_data (sec
)->sreloc
= sreloc
;
5213 /* If this is a global symbol, we count the number of
5214 relocations we need for this symbol. */
5216 head
= &((struct elf_sh_link_hash_entry
*) h
)->dyn_relocs
;
5222 /* Track dynamic relocs needed for local syms too. */
5223 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
5228 vpp
= &elf_section_data (s
)->local_dynrel
;
5229 head
= (struct elf_sh_dyn_relocs
**) vpp
;
5233 if (p
== NULL
|| p
->sec
!= sec
)
5235 bfd_size_type amt
= sizeof (*p
);
5236 p
= bfd_alloc (htab
->root
.dynobj
, amt
);
5247 if (r_type
== R_SH_REL32
5248 #ifdef INCLUDE_SHMEDIA
5249 || r_type
== R_SH_IMM_LOW16_PCREL
5250 || r_type
== R_SH_IMM_MEDLOW16_PCREL
5251 || r_type
== R_SH_IMM_MEDHI16_PCREL
5252 || r_type
== R_SH_IMM_HI16_PCREL
5260 case R_SH_TLS_LE_32
:
5263 (*_bfd_error_handler
)
5264 (_("%B: TLS local exec code cannot be linked into shared objects"),
5271 case R_SH_TLS_LDO_32
:
5272 /* Nothing to do. */
5283 #ifndef sh_elf_set_mach_from_flags
5284 static unsigned int sh_ef_bfd_table
[] = { EF_SH_BFD_TABLE
};
5287 sh_elf_set_mach_from_flags (bfd
*abfd
)
5289 flagword flags
= elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
;
5291 if (flags
>= sizeof(sh_ef_bfd_table
))
5294 if (sh_ef_bfd_table
[flags
] == 0)
5297 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, sh_ef_bfd_table
[flags
]);
5303 /* Reverse table lookup for sh_ef_bfd_table[].
5304 Given a bfd MACH value from archures.c
5305 return the equivalent ELF flags from the table.
5306 Return -1 if no match is found. */
5309 sh_elf_get_flags_from_mach (unsigned long mach
)
5311 int i
= ARRAY_SIZE (sh_ef_bfd_table
) - 1;
5314 if (sh_ef_bfd_table
[i
] == mach
)
5317 /* shouldn't get here */
5322 #endif /* not sh_elf_set_mach_from_flags */
5324 #ifndef sh_elf_set_private_flags
5325 /* Function to keep SH specific file flags. */
5328 sh_elf_set_private_flags (bfd
*abfd
, flagword flags
)
5330 BFD_ASSERT (! elf_flags_init (abfd
)
5331 || elf_elfheader (abfd
)->e_flags
== flags
);
5333 elf_elfheader (abfd
)->e_flags
= flags
;
5334 elf_flags_init (abfd
) = TRUE
;
5335 return sh_elf_set_mach_from_flags (abfd
);
5337 #endif /* not sh_elf_set_private_flags */
5339 #ifndef sh_elf_copy_private_data
5340 /* Copy backend specific data from one object module to another */
5343 sh_elf_copy_private_data (bfd
* ibfd
, bfd
* obfd
)
5345 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5346 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5349 return sh_elf_set_private_flags (obfd
, elf_elfheader (ibfd
)->e_flags
);
5351 #endif /* not sh_elf_copy_private_data */
5353 #ifndef sh_elf_merge_private_data
5355 /* This function returns the ELF architecture number that
5356 corresponds to the given arch_sh* flags. */
5359 sh_find_elf_flags (unsigned int arch_set
)
5361 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
5362 unsigned long bfd_mach
= sh_get_bfd_mach_from_arch_set (arch_set
);
5364 return sh_elf_get_flags_from_mach (bfd_mach
);
5367 /* This routine initialises the elf flags when required and
5368 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
5371 sh_elf_merge_private_data (bfd
*ibfd
, bfd
*obfd
)
5373 extern bfd_boolean
sh_merge_bfd_arch (bfd
*, bfd
*);
5375 if ( bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5376 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5379 if (! elf_flags_init (obfd
))
5381 /* This happens when ld starts out with a 'blank' output file. */
5382 elf_flags_init (obfd
) = TRUE
;
5383 elf_elfheader (obfd
)->e_flags
= EF_SH1
;
5384 sh_elf_set_mach_from_flags (obfd
);
5387 if (! sh_merge_bfd_arch (ibfd
, obfd
))
5389 _bfd_error_handler ("%B: uses instructions which are incompatible "
5390 "with instructions used in previous modules",
5392 bfd_set_error (bfd_error_bad_value
);
5396 elf_elfheader (obfd
)->e_flags
=
5397 sh_elf_get_flags_from_mach (bfd_get_mach (obfd
));
5401 #endif /* not sh_elf_merge_private_data */
5403 /* Override the generic function because we need to store sh_elf_obj_tdata
5404 as the specific tdata. We set also the machine architecture from flags
5408 sh_elf_object_p (bfd
*abfd
)
5410 return sh_elf_set_mach_from_flags (abfd
);
5413 /* Finish up dynamic symbol handling. We set the contents of various
5414 dynamic sections here. */
5417 sh_elf_finish_dynamic_symbol (bfd
*output_bfd
, struct bfd_link_info
*info
,
5418 struct elf_link_hash_entry
*h
,
5419 Elf_Internal_Sym
*sym
)
5421 struct elf_sh_link_hash_table
*htab
;
5423 htab
= sh_elf_hash_table (info
);
5425 if (h
->plt
.offset
!= (bfd_vma
) -1)
5433 Elf_Internal_Rela rel
;
5436 /* This symbol has an entry in the procedure linkage table. Set
5439 BFD_ASSERT (h
->dynindx
!= -1);
5442 sgot
= htab
->sgotplt
;
5443 srel
= htab
->srelplt
;
5444 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
5446 /* Get the index in the procedure linkage table which
5447 corresponds to this symbol. This is the index of this symbol
5448 in all the symbols for which we are making plt entries. The
5449 first entry in the procedure linkage table is reserved. */
5450 plt_index
= get_plt_index (htab
->plt_info
, h
->plt
.offset
);
5452 /* Get the offset into the .got table of the entry that
5453 corresponds to this function. Each .got entry is 4 bytes.
5454 The first three are reserved. */
5455 got_offset
= (plt_index
+ 3) * 4;
5459 got_offset
-= GOT_BIAS
;
5462 /* Fill in the entry in the procedure linkage table. */
5463 memcpy (splt
->contents
+ h
->plt
.offset
,
5464 htab
->plt_info
->symbol_entry
,
5465 htab
->plt_info
->symbol_entry_size
);
5468 install_plt_field (output_bfd
, FALSE
, got_offset
,
5471 + htab
->plt_info
->symbol_fields
.got_entry
));
5474 install_plt_field (output_bfd
, FALSE
,
5475 (sgot
->output_section
->vma
5476 + sgot
->output_offset
5480 + htab
->plt_info
->symbol_fields
.got_entry
));
5481 if (htab
->vxworks_p
)
5483 unsigned int reachable_plts
, plts_per_4k
;
5486 /* Divide the PLT into groups. The first group contains
5487 REACHABLE_PLTS entries and the other groups contain
5488 PLTS_PER_4K entries. Entries in the first group can
5489 branch directly to .plt; those in later groups branch
5490 to the last element of the previous group. */
5491 /* ??? It would be better to create multiple copies of
5492 the common resolver stub. */
5493 reachable_plts
= ((4096
5494 - htab
->plt_info
->plt0_entry_size
5495 - (htab
->plt_info
->symbol_fields
.plt
+ 4))
5496 / htab
->plt_info
->symbol_entry_size
) + 1;
5497 plts_per_4k
= (4096 / htab
->plt_info
->symbol_entry_size
);
5498 if (plt_index
< reachable_plts
)
5499 distance
= -(h
->plt
.offset
5500 + htab
->plt_info
->symbol_fields
.plt
);
5502 distance
= -(((plt_index
- reachable_plts
) % plts_per_4k
+ 1)
5503 * htab
->plt_info
->symbol_entry_size
);
5505 /* Install the 'bra' with this offset. */
5506 bfd_put_16 (output_bfd
,
5507 0xa000 | (0x0fff & ((distance
- 4) / 2)),
5510 + htab
->plt_info
->symbol_fields
.plt
));
5513 install_plt_field (output_bfd
, TRUE
,
5514 splt
->output_section
->vma
+ splt
->output_offset
,
5517 + htab
->plt_info
->symbol_fields
.plt
));
5522 got_offset
+= GOT_BIAS
;
5525 install_plt_field (output_bfd
, FALSE
,
5526 plt_index
* sizeof (Elf32_External_Rela
),
5529 + htab
->plt_info
->symbol_fields
.reloc_offset
));
5531 /* Fill in the entry in the global offset table. */
5532 bfd_put_32 (output_bfd
,
5533 (splt
->output_section
->vma
5534 + splt
->output_offset
5536 + htab
->plt_info
->symbol_resolve_offset
),
5537 sgot
->contents
+ got_offset
);
5539 /* Fill in the entry in the .rela.plt section. */
5540 rel
.r_offset
= (sgot
->output_section
->vma
5541 + sgot
->output_offset
5543 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
5546 rel
.r_addend
= GOT_BIAS
;
5548 loc
= srel
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
5549 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5551 if (htab
->vxworks_p
&& !info
->shared
)
5553 /* Create the .rela.plt.unloaded relocations for this PLT entry.
5554 Begin by pointing LOC to the first such relocation. */
5555 loc
= (htab
->srelplt2
->contents
5556 + (plt_index
* 2 + 1) * sizeof (Elf32_External_Rela
));
5558 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
5559 for the PLT entry's pointer to the .got.plt entry. */
5560 rel
.r_offset
= (htab
->splt
->output_section
->vma
5561 + htab
->splt
->output_offset
5563 + htab
->plt_info
->symbol_fields
.got_entry
);
5564 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
5565 rel
.r_addend
= got_offset
;
5566 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5567 loc
+= sizeof (Elf32_External_Rela
);
5569 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
5570 the .got.plt entry, which initially points to .plt. */
5571 rel
.r_offset
= (htab
->sgotplt
->output_section
->vma
5572 + htab
->sgotplt
->output_offset
5574 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
, R_SH_DIR32
);
5576 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5579 if (!h
->def_regular
)
5581 /* Mark the symbol as undefined, rather than as defined in
5582 the .plt section. Leave the value alone. */
5583 sym
->st_shndx
= SHN_UNDEF
;
5587 if (h
->got
.offset
!= (bfd_vma
) -1
5588 && sh_elf_hash_entry (h
)->tls_type
!= GOT_TLS_GD
5589 && sh_elf_hash_entry (h
)->tls_type
!= GOT_TLS_IE
)
5593 Elf_Internal_Rela rel
;
5596 /* This symbol has an entry in the global offset table. Set it
5600 srel
= htab
->srelgot
;
5601 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
5603 rel
.r_offset
= (sgot
->output_section
->vma
5604 + sgot
->output_offset
5605 + (h
->got
.offset
&~ (bfd_vma
) 1));
5607 /* If this is a static link, or it is a -Bsymbolic link and the
5608 symbol is defined locally or was forced to be local because
5609 of a version file, we just want to emit a RELATIVE reloc.
5610 The entry in the global offset table will already have been
5611 initialized in the relocate_section function. */
5613 && SYMBOL_REFERENCES_LOCAL (info
, h
))
5615 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
5616 rel
.r_addend
= (h
->root
.u
.def
.value
5617 + h
->root
.u
.def
.section
->output_section
->vma
5618 + h
->root
.u
.def
.section
->output_offset
);
5622 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
5623 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
5627 loc
= srel
->contents
;
5628 loc
+= srel
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5629 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5632 #ifdef INCLUDE_SHMEDIA
5634 struct elf_sh_link_hash_entry
*eh
;
5636 eh
= (struct elf_sh_link_hash_entry
*) h
;
5637 if (eh
->datalabel_got
.offset
!= (bfd_vma
) -1)
5641 Elf_Internal_Rela rel
;
5644 /* This symbol has a datalabel entry in the global offset table.
5648 srel
= htab
->srelgot
;
5649 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
5651 rel
.r_offset
= (sgot
->output_section
->vma
5652 + sgot
->output_offset
5653 + (eh
->datalabel_got
.offset
&~ (bfd_vma
) 1));
5655 /* If this is a static link, or it is a -Bsymbolic link and the
5656 symbol is defined locally or was forced to be local because
5657 of a version file, we just want to emit a RELATIVE reloc.
5658 The entry in the global offset table will already have been
5659 initialized in the relocate_section function. */
5661 && SYMBOL_REFERENCES_LOCAL (info
, h
))
5663 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
5664 rel
.r_addend
= (h
->root
.u
.def
.value
5665 + h
->root
.u
.def
.section
->output_section
->vma
5666 + h
->root
.u
.def
.section
->output_offset
);
5670 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
5671 + eh
->datalabel_got
.offset
);
5672 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
5676 loc
= srel
->contents
;
5677 loc
+= srel
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5678 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5686 Elf_Internal_Rela rel
;
5689 /* This symbol needs a copy reloc. Set it up. */
5691 BFD_ASSERT (h
->dynindx
!= -1
5692 && (h
->root
.type
== bfd_link_hash_defined
5693 || h
->root
.type
== bfd_link_hash_defweak
));
5695 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
5697 BFD_ASSERT (s
!= NULL
);
5699 rel
.r_offset
= (h
->root
.u
.def
.value
5700 + h
->root
.u
.def
.section
->output_section
->vma
5701 + h
->root
.u
.def
.section
->output_offset
);
5702 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
5704 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5705 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5708 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
5709 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
5711 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
5712 || (!htab
->vxworks_p
&& h
== htab
->root
.hgot
))
5713 sym
->st_shndx
= SHN_ABS
;
5718 /* Finish up the dynamic sections. */
5721 sh_elf_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
5723 struct elf_sh_link_hash_table
*htab
;
5727 htab
= sh_elf_hash_table (info
);
5728 sgot
= htab
->sgotplt
;
5729 sdyn
= bfd_get_section_by_name (htab
->root
.dynobj
, ".dynamic");
5731 if (htab
->root
.dynamic_sections_created
)
5734 Elf32_External_Dyn
*dyncon
, *dynconend
;
5736 BFD_ASSERT (sgot
!= NULL
&& sdyn
!= NULL
);
5738 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
5739 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
5740 for (; dyncon
< dynconend
; dyncon
++)
5742 Elf_Internal_Dyn dyn
;
5744 #ifdef INCLUDE_SHMEDIA
5748 bfd_elf32_swap_dyn_in (htab
->root
.dynobj
, dyncon
, &dyn
);
5755 #ifdef INCLUDE_SHMEDIA
5757 name
= info
->init_function
;
5761 name
= info
->fini_function
;
5763 if (dyn
.d_un
.d_val
!= 0)
5765 struct elf_link_hash_entry
*h
;
5767 h
= elf_link_hash_lookup (&htab
->root
, name
,
5768 FALSE
, FALSE
, TRUE
);
5769 if (h
!= NULL
&& (h
->other
& STO_SH5_ISA32
))
5771 dyn
.d_un
.d_val
|= 1;
5772 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5779 s
= htab
->sgot
->output_section
;
5783 s
= htab
->srelplt
->output_section
;
5785 BFD_ASSERT (s
!= NULL
);
5786 dyn
.d_un
.d_ptr
= s
->vma
;
5787 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5791 s
= htab
->srelplt
->output_section
;
5792 BFD_ASSERT (s
!= NULL
);
5793 dyn
.d_un
.d_val
= s
->size
;
5794 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5798 /* My reading of the SVR4 ABI indicates that the
5799 procedure linkage table relocs (DT_JMPREL) should be
5800 included in the overall relocs (DT_RELA). This is
5801 what Solaris does. However, UnixWare can not handle
5802 that case. Therefore, we override the DT_RELASZ entry
5803 here to make it not include the JMPREL relocs. Since
5804 the linker script arranges for .rela.plt to follow all
5805 other relocation sections, we don't have to worry
5806 about changing the DT_RELA entry. */
5807 if (htab
->srelplt
!= NULL
)
5809 s
= htab
->srelplt
->output_section
;
5810 dyn
.d_un
.d_val
-= s
->size
;
5812 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
5817 /* Fill in the first entry in the procedure linkage table. */
5819 if (splt
&& splt
->size
> 0 && htab
->plt_info
->plt0_entry
)
5823 memcpy (splt
->contents
,
5824 htab
->plt_info
->plt0_entry
,
5825 htab
->plt_info
->plt0_entry_size
);
5826 for (i
= 0; i
< ARRAY_SIZE (htab
->plt_info
->plt0_got_fields
); i
++)
5827 if (htab
->plt_info
->plt0_got_fields
[i
] != MINUS_ONE
)
5828 install_plt_field (output_bfd
, FALSE
,
5829 (sgot
->output_section
->vma
5830 + sgot
->output_offset
5833 + htab
->plt_info
->plt0_got_fields
[i
]));
5835 if (htab
->vxworks_p
)
5837 /* Finalize the .rela.plt.unloaded contents. */
5838 Elf_Internal_Rela rel
;
5841 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
5842 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
5843 loc
= htab
->srelplt2
->contents
;
5844 rel
.r_offset
= (splt
->output_section
->vma
5845 + splt
->output_offset
5846 + htab
->plt_info
->plt0_got_fields
[2]);
5847 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
5849 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
5850 loc
+= sizeof (Elf32_External_Rela
);
5852 /* Fix up the remaining .rela.plt.unloaded relocations.
5853 They may have the wrong symbol index for _G_O_T_ or
5854 _P_L_T_ depending on the order in which symbols were
5856 while (loc
< htab
->srelplt2
->contents
+ htab
->srelplt2
->size
)
5858 /* The PLT entry's pointer to the .got.plt slot. */
5859 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
5860 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
,
5862 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5863 loc
+= sizeof (Elf32_External_Rela
);
5865 /* The .got.plt slot's pointer to .plt. */
5866 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
5867 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
,
5869 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
5870 loc
+= sizeof (Elf32_External_Rela
);
5874 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5875 really seem like the right value. */
5876 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
5880 /* Fill in the first three entries in the global offset table. */
5881 if (sgot
&& sgot
->size
> 0)
5884 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
5886 bfd_put_32 (output_bfd
,
5887 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
5889 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
5890 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
5892 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
5898 static enum elf_reloc_type_class
5899 sh_elf_reloc_type_class (const Elf_Internal_Rela
*rela
)
5901 switch ((int) ELF32_R_TYPE (rela
->r_info
))
5904 return reloc_class_relative
;
5906 return reloc_class_plt
;
5908 return reloc_class_copy
;
5910 return reloc_class_normal
;
5914 #if !defined SH_TARGET_ALREADY_DEFINED
5915 /* Support for Linux core dump NOTE sections. */
5918 elf32_shlin_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5923 switch (note
->descsz
)
5928 case 168: /* Linux/SH */
5930 elf_tdata (abfd
)->core_signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
5933 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
5942 /* Make a ".reg/999" section. */
5943 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5944 size
, note
->descpos
+ offset
);
5948 elf32_shlin_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5950 switch (note
->descsz
)
5955 case 124: /* Linux/SH elf_prpsinfo */
5956 elf_tdata (abfd
)->core_program
5957 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
5958 elf_tdata (abfd
)->core_command
5959 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
5962 /* Note that for some reason, a spurious space is tacked
5963 onto the end of the args in some (at least one anyway)
5964 implementations, so strip it off if it exists. */
5967 char *command
= elf_tdata (abfd
)->core_command
;
5968 int n
= strlen (command
);
5970 if (0 < n
&& command
[n
- 1] == ' ')
5971 command
[n
- 1] = '\0';
5976 #endif /* not SH_TARGET_ALREADY_DEFINED */
5979 /* Return address for Ith PLT stub in section PLT, for relocation REL
5980 or (bfd_vma) -1 if it should not be included. */
5983 sh_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
5984 const arelent
*rel ATTRIBUTE_UNUSED
)
5986 const struct elf_sh_plt_info
*plt_info
;
5988 plt_info
= get_plt_info (plt
->owner
, (plt
->owner
->flags
& DYNAMIC
) != 0);
5989 return plt
->vma
+ get_plt_offset (plt_info
, i
);
5992 #if !defined SH_TARGET_ALREADY_DEFINED
5993 #define TARGET_BIG_SYM bfd_elf32_sh_vec
5994 #define TARGET_BIG_NAME "elf32-sh"
5995 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
5996 #define TARGET_LITTLE_NAME "elf32-shl"
5999 #define ELF_ARCH bfd_arch_sh
6000 #define ELF_MACHINE_CODE EM_SH
6001 #ifdef __QNXTARGET__
6002 #define ELF_MAXPAGESIZE 0x1000
6004 #define ELF_MAXPAGESIZE 0x80
6007 #define elf_symbol_leading_char '_'
6009 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6010 #define elf_info_to_howto sh_elf_info_to_howto
6011 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
6012 #define elf_backend_relocate_section sh_elf_relocate_section
6013 #define bfd_elf32_bfd_get_relocated_section_contents \
6014 sh_elf_get_relocated_section_contents
6015 #define bfd_elf32_mkobject sh_elf_mkobject
6016 #define elf_backend_object_p sh_elf_object_p
6017 #define bfd_elf32_bfd_set_private_bfd_flags \
6018 sh_elf_set_private_flags
6019 #define bfd_elf32_bfd_copy_private_bfd_data \
6020 sh_elf_copy_private_data
6021 #define bfd_elf32_bfd_merge_private_bfd_data \
6022 sh_elf_merge_private_data
6024 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6025 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
6026 #define elf_backend_check_relocs sh_elf_check_relocs
6027 #define elf_backend_copy_indirect_symbol \
6028 sh_elf_copy_indirect_symbol
6029 #define elf_backend_create_dynamic_sections \
6030 sh_elf_create_dynamic_sections
6031 #define bfd_elf32_bfd_link_hash_table_create \
6032 sh_elf_link_hash_table_create
6033 #define elf_backend_adjust_dynamic_symbol \
6034 sh_elf_adjust_dynamic_symbol
6035 #define elf_backend_always_size_sections \
6036 sh_elf_always_size_sections
6037 #define elf_backend_size_dynamic_sections \
6038 sh_elf_size_dynamic_sections
6039 #define elf_backend_finish_dynamic_symbol \
6040 sh_elf_finish_dynamic_symbol
6041 #define elf_backend_finish_dynamic_sections \
6042 sh_elf_finish_dynamic_sections
6043 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
6044 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
6046 #define elf_backend_can_gc_sections 1
6047 #define elf_backend_can_refcount 1
6048 #define elf_backend_want_got_plt 1
6049 #define elf_backend_plt_readonly 1
6050 #define elf_backend_want_plt_sym 0
6051 #define elf_backend_got_header_size 12
6053 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
6055 #include "elf32-target.h"
6057 /* NetBSD support. */
6058 #undef TARGET_BIG_SYM
6059 #define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
6060 #undef TARGET_BIG_NAME
6061 #define TARGET_BIG_NAME "elf32-sh-nbsd"
6062 #undef TARGET_LITTLE_SYM
6063 #define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
6064 #undef TARGET_LITTLE_NAME
6065 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6066 #undef ELF_MAXPAGESIZE
6067 #define ELF_MAXPAGESIZE 0x10000
6068 #undef ELF_COMMONPAGESIZE
6069 #undef elf_symbol_leading_char
6070 #define elf_symbol_leading_char 0
6072 #define elf32_bed elf32_sh_nbsd_bed
6074 #include "elf32-target.h"
6077 /* Linux support. */
6078 #undef TARGET_BIG_SYM
6079 #define TARGET_BIG_SYM bfd_elf32_shblin_vec
6080 #undef TARGET_BIG_NAME
6081 #define TARGET_BIG_NAME "elf32-shbig-linux"
6082 #undef TARGET_LITTLE_SYM
6083 #define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
6084 #undef TARGET_LITTLE_NAME
6085 #define TARGET_LITTLE_NAME "elf32-sh-linux"
6086 #undef ELF_COMMONPAGESIZE
6087 #define ELF_COMMONPAGESIZE 0x1000
6089 #undef elf_backend_grok_prstatus
6090 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6091 #undef elf_backend_grok_psinfo
6092 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
6094 #define elf32_bed elf32_sh_lin_bed
6096 #include "elf32-target.h"
6098 #undef TARGET_BIG_SYM
6099 #define TARGET_BIG_SYM bfd_elf32_shvxworks_vec
6100 #undef TARGET_BIG_NAME
6101 #define TARGET_BIG_NAME "elf32-sh-vxworks"
6102 #undef TARGET_LITTLE_SYM
6103 #define TARGET_LITTLE_SYM bfd_elf32_shlvxworks_vec
6104 #undef TARGET_LITTLE_NAME
6105 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6107 #define elf32_bed elf32_sh_vxworks_bed
6109 #undef elf_backend_want_plt_sym
6110 #define elf_backend_want_plt_sym 1
6111 #undef elf_symbol_leading_char
6112 #define elf_symbol_leading_char '_'
6113 #define elf_backend_want_got_underscore 1
6114 #undef elf_backend_grok_prstatus
6115 #undef elf_backend_grok_psinfo
6116 #undef elf_backend_add_symbol_hook
6117 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6118 #undef elf_backend_link_output_symbol_hook
6119 #define elf_backend_link_output_symbol_hook \
6120 elf_vxworks_link_output_symbol_hook
6121 #undef elf_backend_emit_relocs
6122 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6123 #undef elf_backend_final_write_processing
6124 #define elf_backend_final_write_processing \
6125 elf_vxworks_final_write_processing
6126 #undef ELF_MAXPAGESIZE
6127 #define ELF_MAXPAGESIZE 0x1000
6128 #undef ELF_COMMONPAGESIZE
6130 #include "elf32-target.h"
6132 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */