1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright (C) 1996-2020 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 #include "elf-vxworks.h"
30 #include "libiberty.h"
31 #include "../opcodes/sh-opc.h"
33 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
34 #define OCTETS_PER_BYTE(ABFD, SEC) 1
36 static bfd_reloc_status_type sh_elf_reloc
37 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
38 static bfd_reloc_status_type sh_elf_ignore_reloc
39 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
40 static bfd_boolean sh_elf_relax_delete_bytes
41 (bfd
*, asection
*, bfd_vma
, int);
42 static bfd_boolean sh_elf_align_loads
43 (bfd
*, asection
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_boolean
*);
44 static bfd_boolean sh_elf_swap_insns
45 (bfd
*, asection
*, void *, bfd_byte
*, bfd_vma
);
46 static int sh_elf_optimized_tls_reloc
47 (struct bfd_link_info
*, int, int);
48 static bfd_vma dtpoff_base
49 (struct bfd_link_info
*);
51 (struct bfd_link_info
*, bfd_vma
);
53 /* The name of the dynamic interpreter. This is put in the .interp
56 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
58 /* FDPIC binaries have a default 128K stack. */
59 #define DEFAULT_STACK_SIZE 0x20000
61 #define MINUS_ONE ((bfd_vma) 0 - 1)
63 /* Decide whether a reference to a symbol can be resolved locally or
64 not. If the symbol is protected, we want the local address, but
65 its function descriptor must be assigned by the dynamic linker. */
66 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
67 (SYMBOL_REFERENCES_LOCAL (INFO, H) \
68 || ! elf_hash_table (INFO)->dynamic_sections_created)
70 #define SH_PARTIAL32 TRUE
71 #define SH_SRC_MASK32 0xffffffff
72 #define SH_ELF_RELOC sh_elf_reloc
73 static reloc_howto_type sh_elf_howto_table
[] =
75 #include "elf32-sh-relocs.h"
78 #define SH_PARTIAL32 FALSE
79 #define SH_SRC_MASK32 0
80 #define SH_ELF_RELOC bfd_elf_generic_reloc
81 static reloc_howto_type sh_vxworks_howto_table
[] =
83 #include "elf32-sh-relocs.h"
86 /* Return true if OUTPUT_BFD is a VxWorks object. */
89 vxworks_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
91 #if !defined SH_TARGET_ALREADY_DEFINED
92 extern const bfd_target sh_elf32_vxworks_le_vec
;
93 extern const bfd_target sh_elf32_vxworks_vec
;
95 return (abfd
->xvec
== &sh_elf32_vxworks_le_vec
96 || abfd
->xvec
== &sh_elf32_vxworks_vec
);
102 /* Return true if OUTPUT_BFD is an FDPIC object. */
105 fdpic_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
107 #if !defined SH_TARGET_ALREADY_DEFINED
108 extern const bfd_target sh_elf32_fdpic_le_vec
;
109 extern const bfd_target sh_elf32_fdpic_be_vec
;
111 return (abfd
->xvec
== &sh_elf32_fdpic_le_vec
112 || abfd
->xvec
== &sh_elf32_fdpic_be_vec
);
118 /* Return the howto table for ABFD. */
120 static reloc_howto_type
*
121 get_howto_table (bfd
*abfd
)
123 if (vxworks_object_p (abfd
))
124 return sh_vxworks_howto_table
;
125 return sh_elf_howto_table
;
128 static bfd_reloc_status_type
129 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
130 asection
*input_section
, bfd_byte
*contents
,
131 bfd_vma addr
, asection
*symbol_section
,
132 bfd_vma start
, bfd_vma end
)
134 static bfd_vma last_addr
;
135 static asection
*last_symbol_section
;
136 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
141 /* Sanity check the address. */
142 if (addr
> bfd_get_section_limit (input_bfd
, input_section
))
143 return bfd_reloc_outofrange
;
145 /* We require the start and end relocations to be processed consecutively -
146 although we allow then to be processed forwards or backwards. */
150 last_symbol_section
= symbol_section
;
153 if (last_addr
!= addr
)
157 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
158 return bfd_reloc_outofrange
;
160 /* Get the symbol_section contents. */
161 if (symbol_section
!= input_section
)
163 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
164 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
167 if (!bfd_malloc_and_get_section (input_bfd
, symbol_section
,
171 return bfd_reloc_outofrange
;
175 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
176 start_ptr
= contents
+ start
;
177 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
179 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
182 diff
= (last_ptr
- ptr
) >> 1;
183 cum_diff
+= diff
& 1;
186 /* Calculate the start / end values to load into rs / re minus four -
187 so that will cancel out the four we would otherwise have to add to
188 addr to get the value to subtract in order to get relative addressing. */
192 end
= (ptr
+ cum_diff
* 2) - contents
;
196 bfd_vma start0
= start
- 4;
198 while (start0
&& IS_PPI (contents
+ start0
))
200 start0
= start
- 2 - ((start
- start0
) & 2);
201 start
= start0
- cum_diff
- 2;
205 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= contents
)
208 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
210 x
= (insn
& 0x200 ? end
: start
) - addr
;
211 if (input_section
!= symbol_section
)
212 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
213 - (input_section
->output_section
->vma
214 + input_section
->output_offset
));
216 if (x
< -128 || x
> 127)
217 return bfd_reloc_overflow
;
219 x
= (insn
& ~0xff) | (x
& 0xff);
220 bfd_put_16 (input_bfd
, (bfd_vma
) x
, contents
+ addr
);
225 /* This function is used for normal relocs. This used to be like the COFF
226 function, and is almost certainly incorrect for other ELF targets. */
228 static bfd_reloc_status_type
229 sh_elf_reloc (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol_in
,
230 void *data
, asection
*input_section
, bfd
*output_bfd
,
231 char **error_message ATTRIBUTE_UNUSED
)
235 enum elf_sh_reloc_type r_type
;
236 bfd_vma addr
= reloc_entry
->address
;
237 bfd_size_type octets
= addr
* OCTETS_PER_BYTE (abfd
, input_section
);
238 bfd_byte
*hit_data
= (bfd_byte
*) data
+ octets
;
240 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
242 if (output_bfd
!= NULL
)
244 /* Partial linking--do nothing. */
245 reloc_entry
->address
+= input_section
->output_offset
;
249 /* Almost all relocs have to do with relaxing. If any work must be
250 done for them, it has been done in sh_relax_section. */
251 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
254 if (symbol_in
!= NULL
255 && bfd_is_und_section (symbol_in
->section
))
256 return bfd_reloc_undefined
;
258 /* PR 17512: file: 9891ca98. */
259 if (octets
+ bfd_get_reloc_size (reloc_entry
->howto
)
260 > bfd_get_section_limit_octets (abfd
, input_section
))
261 return bfd_reloc_outofrange
;
263 if (bfd_is_com_section (symbol_in
->section
))
266 sym_value
= (symbol_in
->value
+
267 symbol_in
->section
->output_section
->vma
+
268 symbol_in
->section
->output_offset
);
273 insn
= bfd_get_32 (abfd
, hit_data
);
274 insn
+= sym_value
+ reloc_entry
->addend
;
275 bfd_put_32 (abfd
, insn
, hit_data
);
278 insn
= bfd_get_16 (abfd
, hit_data
);
279 sym_value
+= reloc_entry
->addend
;
280 sym_value
-= (input_section
->output_section
->vma
281 + input_section
->output_offset
284 sym_value
+= (((insn
& 0xfff) ^ 0x800) - 0x800) << 1;
285 insn
= (insn
& 0xf000) | ((sym_value
>> 1) & 0xfff);
286 bfd_put_16 (abfd
, insn
, hit_data
);
287 if (sym_value
+ 0x1000 >= 0x2000 || (sym_value
& 1) != 0)
288 return bfd_reloc_overflow
;
298 /* This function is used for relocs which are only used for relaxing,
299 which the linker should otherwise ignore. */
301 static bfd_reloc_status_type
302 sh_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
303 asymbol
*symbol ATTRIBUTE_UNUSED
,
304 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
306 char **error_message ATTRIBUTE_UNUSED
)
308 if (output_bfd
!= NULL
)
309 reloc_entry
->address
+= input_section
->output_offset
;
313 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
317 bfd_reloc_code_real_type bfd_reloc_val
;
318 unsigned char elf_reloc_val
;
321 /* An array mapping BFD reloc codes to SH ELF relocs. */
323 static const struct elf_reloc_map sh_reloc_map
[] =
325 { BFD_RELOC_NONE
, R_SH_NONE
},
326 { BFD_RELOC_32
, R_SH_DIR32
},
327 { BFD_RELOC_16
, R_SH_DIR16
},
328 { BFD_RELOC_8
, R_SH_DIR8
},
329 { BFD_RELOC_CTOR
, R_SH_DIR32
},
330 { BFD_RELOC_32_PCREL
, R_SH_REL32
},
331 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_DIR8WPN
},
332 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_IND12W
},
333 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_DIR8WPZ
},
334 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_DIR8WPL
},
335 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
336 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
337 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
338 { BFD_RELOC_SH_USES
, R_SH_USES
},
339 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
340 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
341 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
342 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
343 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
344 { BFD_RELOC_VTABLE_INHERIT
, R_SH_GNU_VTINHERIT
},
345 { BFD_RELOC_VTABLE_ENTRY
, R_SH_GNU_VTENTRY
},
346 { BFD_RELOC_SH_LOOP_START
, R_SH_LOOP_START
},
347 { BFD_RELOC_SH_LOOP_END
, R_SH_LOOP_END
},
348 { BFD_RELOC_SH_TLS_GD_32
, R_SH_TLS_GD_32
},
349 { BFD_RELOC_SH_TLS_LD_32
, R_SH_TLS_LD_32
},
350 { BFD_RELOC_SH_TLS_LDO_32
, R_SH_TLS_LDO_32
},
351 { BFD_RELOC_SH_TLS_IE_32
, R_SH_TLS_IE_32
},
352 { BFD_RELOC_SH_TLS_LE_32
, R_SH_TLS_LE_32
},
353 { BFD_RELOC_SH_TLS_DTPMOD32
, R_SH_TLS_DTPMOD32
},
354 { BFD_RELOC_SH_TLS_DTPOFF32
, R_SH_TLS_DTPOFF32
},
355 { BFD_RELOC_SH_TLS_TPOFF32
, R_SH_TLS_TPOFF32
},
356 { BFD_RELOC_32_GOT_PCREL
, R_SH_GOT32
},
357 { BFD_RELOC_32_PLT_PCREL
, R_SH_PLT32
},
358 { BFD_RELOC_SH_COPY
, R_SH_COPY
},
359 { BFD_RELOC_SH_GLOB_DAT
, R_SH_GLOB_DAT
},
360 { BFD_RELOC_SH_JMP_SLOT
, R_SH_JMP_SLOT
},
361 { BFD_RELOC_SH_RELATIVE
, R_SH_RELATIVE
},
362 { BFD_RELOC_32_GOTOFF
, R_SH_GOTOFF
},
363 { BFD_RELOC_SH_GOTPC
, R_SH_GOTPC
},
364 { BFD_RELOC_SH_GOTPLT32
, R_SH_GOTPLT32
},
365 { BFD_RELOC_SH_GOT20
, R_SH_GOT20
},
366 { BFD_RELOC_SH_GOTOFF20
, R_SH_GOTOFF20
},
367 { BFD_RELOC_SH_GOTFUNCDESC
, R_SH_GOTFUNCDESC
},
368 { BFD_RELOC_SH_GOTFUNCDESC20
, R_SH_GOTFUNCDESC20
},
369 { BFD_RELOC_SH_GOTOFFFUNCDESC
, R_SH_GOTOFFFUNCDESC
},
370 { BFD_RELOC_SH_GOTOFFFUNCDESC20
, R_SH_GOTOFFFUNCDESC20
},
371 { BFD_RELOC_SH_FUNCDESC
, R_SH_FUNCDESC
},
374 /* Given a BFD reloc code, return the howto structure for the
375 corresponding SH ELF reloc. */
377 static reloc_howto_type
*
378 sh_elf_reloc_type_lookup (bfd
*abfd
, bfd_reloc_code_real_type code
)
382 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
384 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
385 return get_howto_table (abfd
) + (int) sh_reloc_map
[i
].elf_reloc_val
;
391 static reloc_howto_type
*
392 sh_elf_reloc_name_lookup (bfd
*abfd
, const char *r_name
)
396 if (vxworks_object_p (abfd
))
399 i
< (sizeof (sh_vxworks_howto_table
)
400 / sizeof (sh_vxworks_howto_table
[0]));
402 if (sh_vxworks_howto_table
[i
].name
!= NULL
403 && strcasecmp (sh_vxworks_howto_table
[i
].name
, r_name
) == 0)
404 return &sh_vxworks_howto_table
[i
];
409 i
< (sizeof (sh_elf_howto_table
)
410 / sizeof (sh_elf_howto_table
[0]));
412 if (sh_elf_howto_table
[i
].name
!= NULL
413 && strcasecmp (sh_elf_howto_table
[i
].name
, r_name
) == 0)
414 return &sh_elf_howto_table
[i
];
420 /* Given an ELF reloc, fill in the howto field of a relent. */
423 sh_elf_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
, Elf_Internal_Rela
*dst
)
427 r
= ELF32_R_TYPE (dst
->r_info
);
430 || (r
>= R_SH_FIRST_INVALID_RELOC
&& r
<= R_SH_LAST_INVALID_RELOC
)
431 || (r
>= R_SH_FIRST_INVALID_RELOC_2
&& r
<= R_SH_LAST_INVALID_RELOC_2
)
432 || (r
>= R_SH_FIRST_INVALID_RELOC_3
&& r
<= R_SH_LAST_INVALID_RELOC_3
)
433 || (r
>= R_SH_FIRST_INVALID_RELOC_4
&& r
<= R_SH_LAST_INVALID_RELOC_4
)
434 || (r
>= R_SH_FIRST_INVALID_RELOC_5
&& r
<= R_SH_LAST_INVALID_RELOC_5
)
435 || (r
>= R_SH_FIRST_INVALID_RELOC_6
&& r
<= R_SH_LAST_INVALID_RELOC_6
))
437 /* xgettext:c-format */
438 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
440 bfd_set_error (bfd_error_bad_value
);
444 cache_ptr
->howto
= get_howto_table (abfd
) + r
;
448 /* This function handles relaxing for SH ELF. See the corresponding
449 function in coff-sh.c for a description of what this does. FIXME:
450 There is a lot of duplication here between this code and the COFF
451 specific code. The format of relocs and symbols is wound deeply
452 into this code, but it would still be better if the duplication
453 could be eliminated somehow. Note in particular that although both
454 functions use symbols like R_SH_CODE, those symbols have different
455 values; in coff-sh.c they come from include/coff/sh.h, whereas here
456 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
459 sh_elf_relax_section (bfd
*abfd
, asection
*sec
,
460 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
462 Elf_Internal_Shdr
*symtab_hdr
;
463 Elf_Internal_Rela
*internal_relocs
;
464 bfd_boolean have_code
;
465 Elf_Internal_Rela
*irel
, *irelend
;
466 bfd_byte
*contents
= NULL
;
467 Elf_Internal_Sym
*isymbuf
= NULL
;
471 if (bfd_link_relocatable (link_info
)
472 || (sec
->flags
& SEC_RELOC
) == 0
473 || sec
->reloc_count
== 0)
476 symtab_hdr
= &elf_symtab_hdr (abfd
);
478 internal_relocs
= (_bfd_elf_link_read_relocs
479 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
480 link_info
->keep_memory
));
481 if (internal_relocs
== NULL
)
486 irelend
= internal_relocs
+ sec
->reloc_count
;
487 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
489 bfd_vma laddr
, paddr
, symval
;
491 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
494 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
497 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
500 /* Get the section contents. */
501 if (contents
== NULL
)
503 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
504 contents
= elf_section_data (sec
)->this_hdr
.contents
;
507 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
512 /* The r_addend field of the R_SH_USES reloc will point us to
513 the register load. The 4 is because the r_addend field is
514 computed as though it were a jump offset, which are based
515 from 4 bytes after the jump instruction. */
516 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
517 if (laddr
>= sec
->size
)
519 /* xgettext:c-format */
521 (_("%pB: %#" PRIx64
": warning: bad R_SH_USES offset"),
522 abfd
, (uint64_t) irel
->r_offset
);
525 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
527 /* If the instruction is not mov.l NN,rN, we don't know what to
529 if ((insn
& 0xf000) != 0xd000)
532 /* xgettext:c-format */
533 (_("%pB: %#" PRIx64
": warning: "
534 "R_SH_USES points to unrecognized insn 0x%x"),
535 abfd
, (uint64_t) irel
->r_offset
, insn
);
539 /* Get the address from which the register is being loaded. The
540 displacement in the mov.l instruction is quadrupled. It is a
541 displacement from four bytes after the movl instruction, but,
542 before adding in the PC address, two least significant bits
543 of the PC are cleared. We assume that the section is aligned
544 on a four byte boundary. */
547 paddr
+= (laddr
+ 4) &~ (bfd_vma
) 3;
548 if (paddr
>= sec
->size
)
551 /* xgettext:c-format */
552 (_("%pB: %#" PRIx64
": warning: bad R_SH_USES load offset"),
553 abfd
, (uint64_t) irel
->r_offset
);
557 /* Get the reloc for the address from which the register is
558 being loaded. This reloc will tell us which function is
559 actually being called. */
560 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
561 if (irelfn
->r_offset
== paddr
562 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
564 if (irelfn
>= irelend
)
567 /* xgettext:c-format */
568 (_("%pB: %#" PRIx64
": warning: could not find expected reloc"),
569 abfd
, (uint64_t) paddr
);
573 /* Read this BFD's symbols if we haven't done so already. */
574 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
576 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
578 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
579 symtab_hdr
->sh_info
, 0,
585 /* Get the value of the symbol referred to by the reloc. */
586 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
588 /* A local symbol. */
589 Elf_Internal_Sym
*isym
;
591 isym
= isymbuf
+ ELF32_R_SYM (irelfn
->r_info
);
593 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd
, sec
))
596 /* xgettext:c-format */
597 (_("%pB: %#" PRIx64
": warning: symbol in unexpected section"),
598 abfd
, (uint64_t) paddr
);
602 symval
= (isym
->st_value
603 + sec
->output_section
->vma
604 + sec
->output_offset
);
609 struct elf_link_hash_entry
*h
;
611 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
612 h
= elf_sym_hashes (abfd
)[indx
];
613 BFD_ASSERT (h
!= NULL
);
614 if (h
->root
.type
!= bfd_link_hash_defined
615 && h
->root
.type
!= bfd_link_hash_defweak
)
617 /* This appears to be a reference to an undefined
618 symbol. Just ignore it--it will be caught by the
619 regular reloc processing. */
623 symval
= (h
->root
.u
.def
.value
624 + h
->root
.u
.def
.section
->output_section
->vma
625 + h
->root
.u
.def
.section
->output_offset
);
628 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
629 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
631 symval
+= irelfn
->r_addend
;
633 /* See if this function call can be shortened. */
636 + sec
->output_section
->vma
639 /* A branch to an address beyond ours might be increased by an
640 .align that doesn't move when bytes behind us are deleted.
641 So, we add some slop in this calculation to allow for
643 if (foff
< -0x1000 || foff
>= 0x1000 - 8)
645 /* After all that work, we can't shorten this function call. */
649 /* Shorten the function call. */
651 /* For simplicity of coding, we are going to modify the section
652 contents, the section relocs, and the BFD symbol table. We
653 must tell the rest of the code not to free up this
654 information. It would be possible to instead create a table
655 of changes which have to be made, as is done in coff-mips.c;
656 that would be more work, but would require less memory when
657 the linker is run. */
659 elf_section_data (sec
)->relocs
= internal_relocs
;
660 elf_section_data (sec
)->this_hdr
.contents
= contents
;
661 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
663 /* Replace the jmp/jsr with a bra/bsr. */
665 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
666 replace the jmp/jsr with a bra/bsr. */
667 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
668 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
669 here, but that only checks if the symbol is an external symbol,
670 not if the symbol is in a different section. Besides, we need
671 a consistent meaning for the relocation, so we just assume here that
672 the value of the symbol is not available. */
674 /* We can't fully resolve this yet, because the external
675 symbol value may be changed by future relaxing. We let
676 the final link phase handle it. */
677 if (bfd_get_16 (abfd
, contents
+ irel
->r_offset
) & 0x0020)
678 bfd_put_16 (abfd
, (bfd_vma
) 0xa000, contents
+ irel
->r_offset
);
680 bfd_put_16 (abfd
, (bfd_vma
) 0xb000, contents
+ irel
->r_offset
);
684 /* When we calculated the symbol "value" we had an offset in the
685 DIR32's word in memory (we read and add it above). However,
686 the jsr we create does NOT have this offset encoded, so we
687 have to add it to the addend to preserve it. */
688 irel
->r_addend
+= bfd_get_32 (abfd
, contents
+ paddr
);
690 /* See if there is another R_SH_USES reloc referring to the same
692 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
693 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
694 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
696 if (irelscan
< irelend
)
698 /* Some other function call depends upon this register load,
699 and we have not yet converted that function call.
700 Indeed, we may never be able to convert it. There is
701 nothing else we can do at this point. */
705 /* Look for a R_SH_COUNT reloc on the location where the
706 function address is stored. Do this before deleting any
707 bytes, to avoid confusion about the address. */
708 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
709 if (irelcount
->r_offset
== paddr
710 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
713 /* Delete the register load. */
714 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
717 /* That will change things, so, just in case it permits some
718 other function call to come within range, we should relax
719 again. Note that this is not required, and it may be slow. */
722 /* Now check whether we got a COUNT reloc. */
723 if (irelcount
>= irelend
)
726 /* xgettext:c-format */
727 (_("%pB: %#" PRIx64
": warning: "
728 "could not find expected COUNT reloc"),
729 abfd
, (uint64_t) paddr
);
733 /* The number of uses is stored in the r_addend field. We've
735 if (irelcount
->r_addend
== 0)
737 /* xgettext:c-format */
738 _bfd_error_handler (_("%pB: %#" PRIx64
": warning: bad count"),
739 abfd
, (uint64_t) paddr
);
743 --irelcount
->r_addend
;
745 /* If there are no more uses, we can delete the address. Reload
746 the address from irelfn, in case it was changed by the
747 previous call to sh_elf_relax_delete_bytes. */
748 if (irelcount
->r_addend
== 0)
750 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
754 /* We've done all we can with that function call. */
757 /* Look for load and store instructions that we can align on four
759 if ((elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
) != EF_SH4
764 /* Get the section contents. */
765 if (contents
== NULL
)
767 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
768 contents
= elf_section_data (sec
)->this_hdr
.contents
;
771 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
776 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
782 elf_section_data (sec
)->relocs
= internal_relocs
;
783 elf_section_data (sec
)->this_hdr
.contents
= contents
;
784 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
789 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
791 if (! link_info
->keep_memory
)
795 /* Cache the symbols for elf_link_input_bfd. */
796 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
801 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
803 if (! link_info
->keep_memory
)
807 /* Cache the section contents for elf_link_input_bfd. */
808 elf_section_data (sec
)->this_hdr
.contents
= contents
;
812 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
813 free (internal_relocs
);
818 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
820 if (elf_section_data (sec
)->this_hdr
.contents
!= contents
)
822 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
823 free (internal_relocs
);
828 /* Delete some bytes from a section while relaxing. FIXME: There is a
829 lot of duplication between this function and sh_relax_delete_bytes
833 sh_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
,
836 Elf_Internal_Shdr
*symtab_hdr
;
837 unsigned int sec_shndx
;
839 Elf_Internal_Rela
*irel
, *irelend
;
840 Elf_Internal_Rela
*irelalign
;
842 Elf_Internal_Sym
*isymbuf
, *isym
, *isymend
;
843 struct elf_link_hash_entry
**sym_hashes
;
844 struct elf_link_hash_entry
**end_hashes
;
845 unsigned int symcount
;
848 symtab_hdr
= &elf_symtab_hdr (abfd
);
849 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
851 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
853 contents
= elf_section_data (sec
)->this_hdr
.contents
;
855 /* The deletion must stop at the next ALIGN reloc for an alignment
856 power larger than the number of bytes we are deleting. */
861 irel
= elf_section_data (sec
)->relocs
;
862 irelend
= irel
+ sec
->reloc_count
;
863 for (; irel
< irelend
; irel
++)
865 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
866 && irel
->r_offset
> addr
867 && count
< (1 << irel
->r_addend
))
870 toaddr
= irel
->r_offset
;
875 /* Actually delete the bytes. */
876 memmove (contents
+ addr
, contents
+ addr
+ count
,
877 (size_t) (toaddr
- addr
- count
));
878 if (irelalign
== NULL
)
884 #define NOP_OPCODE (0x0009)
886 BFD_ASSERT ((count
& 1) == 0);
887 for (i
= 0; i
< count
; i
+= 2)
888 bfd_put_16 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
891 /* Adjust all the relocs. */
892 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
894 bfd_vma nraddr
, stop
;
897 int off
, adjust
, oinsn
;
898 bfd_signed_vma voff
= 0;
899 bfd_boolean overflow
;
901 /* Get the new reloc address. */
902 nraddr
= irel
->r_offset
;
903 if ((irel
->r_offset
> addr
904 && irel
->r_offset
< toaddr
)
905 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
906 && irel
->r_offset
== toaddr
))
909 /* See if this reloc was for the bytes we have deleted, in which
910 case we no longer care about it. Don't delete relocs which
911 represent addresses, though. */
912 if (irel
->r_offset
>= addr
913 && irel
->r_offset
< addr
+ count
914 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
915 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
916 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
917 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
918 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
921 /* If this is a PC relative reloc, see if the range it covers
922 includes the bytes we have deleted. */
923 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
932 start
= irel
->r_offset
;
933 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
937 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
944 /* If this reloc is against a symbol defined in this
945 section, and the symbol will not be adjusted below, we
946 must check the addend to see it will put the value in
947 range to be adjusted, and hence must be changed. */
948 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
950 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
951 if (isym
->st_shndx
== sec_shndx
952 && (isym
->st_value
<= addr
953 || isym
->st_value
>= toaddr
))
957 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
959 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
960 val
+= isym
->st_value
;
961 if (val
> addr
&& val
< toaddr
)
962 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
966 val
= isym
->st_value
+ irel
->r_addend
;
967 if (val
> addr
&& val
< toaddr
)
968 irel
->r_addend
-= count
;
979 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
986 /* This has been made by previous relaxation. Since the
987 relocation will be against an external symbol, the
988 final relocation will just do the right thing. */
995 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
997 /* The addend will be against the section symbol, thus
998 for adjusting the addend, the relevant start is the
999 start of the section.
1000 N.B. If we want to abandon in-place changes here and
1001 test directly using symbol + addend, we have to take into
1002 account that the addend has already been adjusted by -4. */
1003 if (stop
> addr
&& stop
< toaddr
)
1004 irel
->r_addend
-= count
;
1010 stop
= start
+ 4 + off
* 2;
1015 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
1021 /* These relocs types represent
1023 The r_addend field holds the difference between the reloc
1024 address and L1. That is the start of the reloc, and
1025 adding in the contents gives us the top. We must adjust
1026 both the r_offset field and the section contents.
1027 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1028 and the elf bfd r_offset is called r_vaddr. */
1030 stop
= irel
->r_offset
;
1031 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
1035 && (stop
<= addr
|| stop
>= toaddr
))
1036 irel
->r_addend
+= count
;
1037 else if (stop
> addr
1039 && (start
<= addr
|| start
>= toaddr
))
1040 irel
->r_addend
-= count
;
1042 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1043 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1044 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1045 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1047 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1048 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1053 start
= irel
->r_offset
;
1054 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1055 + (long) irel
->r_addend
1062 && (stop
<= addr
|| stop
>= toaddr
))
1064 else if (stop
> addr
1066 && (start
<= addr
|| start
>= toaddr
))
1075 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1084 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1086 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1091 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1093 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1097 BFD_ASSERT (adjust
== count
|| count
>= 4);
1102 if ((irel
->r_offset
& 3) == 0)
1105 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1107 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1112 if (voff
< 0 || voff
>= 0xff)
1114 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1119 if (voff
< - 0x8000 || voff
>= 0x8000)
1121 bfd_put_signed_16 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1126 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1130 irel
->r_addend
+= adjust
;
1137 /* xgettext:c-format */
1138 (_("%pB: %#" PRIx64
": fatal: reloc overflow while relaxing"),
1139 abfd
, (uint64_t) irel
->r_offset
);
1140 bfd_set_error (bfd_error_bad_value
);
1145 irel
->r_offset
= nraddr
;
1148 /* Look through all the other sections. If there contain any IMM32
1149 relocs against internal symbols which we are not going to adjust
1150 below, we may need to adjust the addends. */
1151 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1153 Elf_Internal_Rela
*internal_relocs
;
1154 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1155 bfd_byte
*ocontents
;
1158 || (o
->flags
& SEC_RELOC
) == 0
1159 || o
->reloc_count
== 0)
1162 /* We always cache the relocs. Perhaps, if info->keep_memory is
1163 FALSE, we should free them, if we are permitted to, when we
1164 leave sh_coff_relax_section. */
1165 internal_relocs
= (_bfd_elf_link_read_relocs
1166 (abfd
, o
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
1167 if (internal_relocs
== NULL
)
1171 irelscanend
= internal_relocs
+ o
->reloc_count
;
1172 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1174 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1175 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1177 bfd_vma start
, stop
;
1178 bfd_signed_vma voff
;
1180 if (ocontents
== NULL
)
1182 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1183 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1186 /* We always cache the section contents.
1187 Perhaps, if info->keep_memory is FALSE, we
1188 should free them, if we are permitted to,
1189 when we leave sh_coff_relax_section. */
1190 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1196 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1200 stop
= irelscan
->r_offset
;
1202 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1204 /* STOP is in a different section, so it won't change. */
1205 if (start
> addr
&& start
< toaddr
)
1206 irelscan
->r_addend
+= count
;
1208 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1209 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1213 && (stop
<= addr
|| stop
>= toaddr
))
1214 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
+ count
,
1215 ocontents
+ irelscan
->r_offset
);
1216 else if (stop
> addr
1218 && (start
<= addr
|| start
>= toaddr
))
1219 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
- count
,
1220 ocontents
+ irelscan
->r_offset
);
1223 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1226 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1230 isym
= isymbuf
+ ELF32_R_SYM (irelscan
->r_info
);
1231 if (isym
->st_shndx
== sec_shndx
1232 && (isym
->st_value
<= addr
1233 || isym
->st_value
>= toaddr
))
1237 if (ocontents
== NULL
)
1239 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1240 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1243 /* We always cache the section contents.
1244 Perhaps, if info->keep_memory is FALSE, we
1245 should free them, if we are permitted to,
1246 when we leave sh_coff_relax_section. */
1247 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1253 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1257 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1258 val
+= isym
->st_value
;
1259 if (val
> addr
&& val
< toaddr
)
1260 bfd_put_32 (abfd
, val
- count
,
1261 ocontents
+ irelscan
->r_offset
);
1266 /* Adjust the local symbols defined in this section. */
1267 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1268 for (isym
= isymbuf
; isym
< isymend
; isym
++)
1270 if (isym
->st_shndx
== sec_shndx
1271 && isym
->st_value
> addr
1272 && isym
->st_value
< toaddr
)
1273 isym
->st_value
-= count
;
1276 /* Now adjust the global symbols defined in this section. */
1277 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1278 - symtab_hdr
->sh_info
);
1279 sym_hashes
= elf_sym_hashes (abfd
);
1280 end_hashes
= sym_hashes
+ symcount
;
1281 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1283 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1284 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1285 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1286 && sym_hash
->root
.u
.def
.section
== sec
1287 && sym_hash
->root
.u
.def
.value
> addr
1288 && sym_hash
->root
.u
.def
.value
< toaddr
)
1290 sym_hash
->root
.u
.def
.value
-= count
;
1294 /* See if we can move the ALIGN reloc forward. We have adjusted
1295 r_offset for it already. */
1296 if (irelalign
!= NULL
)
1298 bfd_vma alignto
, alignaddr
;
1300 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1301 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1302 1 << irelalign
->r_addend
);
1303 if (alignto
!= alignaddr
)
1305 /* Tail recursion. */
1306 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1307 (int) (alignto
- alignaddr
));
1314 /* Look for loads and stores which we can align to four byte
1315 boundaries. This is like sh_align_loads in coff-sh.c. */
1318 sh_elf_align_loads (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1319 Elf_Internal_Rela
*internal_relocs
,
1320 bfd_byte
*contents ATTRIBUTE_UNUSED
,
1321 bfd_boolean
*pswapped
)
1323 Elf_Internal_Rela
*irel
, *irelend
;
1324 bfd_vma
*labels
= NULL
;
1325 bfd_vma
*label
, *label_end
;
1330 irelend
= internal_relocs
+ sec
->reloc_count
;
1332 /* Get all the addresses with labels on them. */
1333 amt
= sec
->reloc_count
;
1334 amt
*= sizeof (bfd_vma
);
1335 labels
= (bfd_vma
*) bfd_malloc (amt
);
1339 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1341 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1343 *label_end
= irel
->r_offset
;
1348 /* Note that the assembler currently always outputs relocs in
1349 address order. If that ever changes, this code will need to sort
1350 the label values and the relocs. */
1354 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1356 bfd_vma start
, stop
;
1358 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1361 start
= irel
->r_offset
;
1363 for (irel
++; irel
< irelend
; irel
++)
1364 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1367 stop
= irel
->r_offset
;
1371 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1372 internal_relocs
, &label
,
1373 label_end
, start
, stop
, pswapped
))
1386 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1389 sh_elf_swap_insns (bfd
*abfd
, asection
*sec
, void *relocs
,
1390 bfd_byte
*contents
, bfd_vma addr
)
1392 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1393 unsigned short i1
, i2
;
1394 Elf_Internal_Rela
*irel
, *irelend
;
1396 /* Swap the instructions themselves. */
1397 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1398 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1399 bfd_put_16 (abfd
, (bfd_vma
) i2
, contents
+ addr
);
1400 bfd_put_16 (abfd
, (bfd_vma
) i1
, contents
+ addr
+ 2);
1402 /* Adjust all reloc addresses. */
1403 irelend
= internal_relocs
+ sec
->reloc_count
;
1404 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1406 enum elf_sh_reloc_type type
;
1409 /* There are a few special types of relocs that we don't want to
1410 adjust. These relocs do not apply to the instruction itself,
1411 but are only associated with the address. */
1412 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1413 if (type
== R_SH_ALIGN
1414 || type
== R_SH_CODE
1415 || type
== R_SH_DATA
1416 || type
== R_SH_LABEL
)
1419 /* If an R_SH_USES reloc points to one of the addresses being
1420 swapped, we must adjust it. It would be incorrect to do this
1421 for a jump, though, since we want to execute both
1422 instructions after the jump. (We have avoided swapping
1423 around a label, so the jump will not wind up executing an
1424 instruction it shouldn't). */
1425 if (type
== R_SH_USES
)
1429 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
1431 irel
->r_offset
+= 2;
1432 else if (off
== addr
+ 2)
1433 irel
->r_offset
-= 2;
1436 if (irel
->r_offset
== addr
)
1438 irel
->r_offset
+= 2;
1441 else if (irel
->r_offset
== addr
+ 2)
1443 irel
->r_offset
-= 2;
1452 unsigned short insn
, oinsn
;
1453 bfd_boolean overflow
;
1455 loc
= contents
+ irel
->r_offset
;
1464 insn
= bfd_get_16 (abfd
, loc
);
1467 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1469 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1473 insn
= bfd_get_16 (abfd
, loc
);
1476 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1478 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1482 /* This reloc ignores the least significant 3 bits of
1483 the program counter before adding in the offset.
1484 This means that if ADDR is at an even address, the
1485 swap will not affect the offset. If ADDR is an at an
1486 odd address, then the instruction will be crossing a
1487 four byte boundary, and must be adjusted. */
1488 if ((addr
& 3) != 0)
1490 insn
= bfd_get_16 (abfd
, loc
);
1493 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1495 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1504 /* xgettext:c-format */
1505 (_("%pB: %#" PRIx64
": fatal: reloc overflow while relaxing"),
1506 abfd
, (uint64_t) irel
->r_offset
);
1507 bfd_set_error (bfd_error_bad_value
);
1516 /* Describes one of the various PLT styles. */
1518 struct elf_sh_plt_info
1520 /* The template for the first PLT entry, or NULL if there is no special
1522 const bfd_byte
*plt0_entry
;
1524 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1525 bfd_vma plt0_entry_size
;
1527 /* Index I is the offset into PLT0_ENTRY of a pointer to
1528 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1529 if there is no such pointer. */
1530 bfd_vma plt0_got_fields
[3];
1532 /* The template for a symbol's PLT entry. */
1533 const bfd_byte
*symbol_entry
;
1535 /* The size of SYMBOL_ENTRY in bytes. */
1536 bfd_vma symbol_entry_size
;
1538 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1539 on all targets. The comments by each member indicate the value
1540 that the field must hold. */
1542 bfd_vma got_entry
; /* the address of the symbol's .got.plt entry */
1543 bfd_vma plt
; /* .plt (or a branch to .plt on VxWorks) */
1544 bfd_vma reloc_offset
; /* the offset of the symbol's JMP_SLOT reloc */
1545 bfd_boolean got20
; /* TRUE if got_entry points to a movi20
1546 instruction (instead of a constant pool
1550 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1551 bfd_vma symbol_resolve_offset
;
1553 /* A different PLT layout which can be used for the first
1554 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1556 const struct elf_sh_plt_info
*short_plt
;
1559 /* The size in bytes of an entry in the procedure linkage table. */
1561 #define ELF_PLT_ENTRY_SIZE 28
1563 /* First entry in an absolute procedure linkage table look like this. */
1565 /* Note - this code has been "optimised" not to use r2. r2 is used by
1566 GCC to return the address of large structures, so it should not be
1567 corrupted here. This does mean however, that this PLT does not conform
1568 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1569 and r2 contains the GOT id. This version stores the GOT id in r0 and
1570 ignores the type. Loaders can easily detect this difference however,
1571 since the type will always be 0 or 8, and the GOT ids will always be
1572 greater than or equal to 12. */
1573 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1575 0xd0, 0x05, /* mov.l 2f,r0 */
1576 0x60, 0x02, /* mov.l @r0,r0 */
1577 0x2f, 0x06, /* mov.l r0,@-r15 */
1578 0xd0, 0x03, /* mov.l 1f,r0 */
1579 0x60, 0x02, /* mov.l @r0,r0 */
1580 0x40, 0x2b, /* jmp @r0 */
1581 0x60, 0xf6, /* mov.l @r15+,r0 */
1582 0x00, 0x09, /* nop */
1583 0x00, 0x09, /* nop */
1584 0x00, 0x09, /* nop */
1585 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1586 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1589 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1591 0x05, 0xd0, /* mov.l 2f,r0 */
1592 0x02, 0x60, /* mov.l @r0,r0 */
1593 0x06, 0x2f, /* mov.l r0,@-r15 */
1594 0x03, 0xd0, /* mov.l 1f,r0 */
1595 0x02, 0x60, /* mov.l @r0,r0 */
1596 0x2b, 0x40, /* jmp @r0 */
1597 0xf6, 0x60, /* mov.l @r15+,r0 */
1598 0x09, 0x00, /* nop */
1599 0x09, 0x00, /* nop */
1600 0x09, 0x00, /* nop */
1601 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1602 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1605 /* Sebsequent entries in an absolute procedure linkage table look like
1608 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1610 0xd0, 0x04, /* mov.l 1f,r0 */
1611 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1612 0xd1, 0x02, /* mov.l 0f,r1 */
1613 0x40, 0x2b, /* jmp @r0 */
1614 0x60, 0x13, /* mov r1,r0 */
1615 0xd1, 0x03, /* mov.l 2f,r1 */
1616 0x40, 0x2b, /* jmp @r0 */
1617 0x00, 0x09, /* nop */
1618 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1619 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1620 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1623 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1625 0x04, 0xd0, /* mov.l 1f,r0 */
1626 0x02, 0x60, /* mov.l @r0,r0 */
1627 0x02, 0xd1, /* mov.l 0f,r1 */
1628 0x2b, 0x40, /* jmp @r0 */
1629 0x13, 0x60, /* mov r1,r0 */
1630 0x03, 0xd1, /* mov.l 2f,r1 */
1631 0x2b, 0x40, /* jmp @r0 */
1632 0x09, 0x00, /* nop */
1633 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1634 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1635 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1638 /* Entries in a PIC procedure linkage table look like this. */
1640 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1642 0xd0, 0x04, /* mov.l 1f,r0 */
1643 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1644 0x40, 0x2b, /* jmp @r0 */
1645 0x00, 0x09, /* nop */
1646 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1647 0xd1, 0x03, /* mov.l 2f,r1 */
1648 0x40, 0x2b, /* jmp @r0 */
1649 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1650 0x00, 0x09, /* nop */
1651 0x00, 0x09, /* nop */
1652 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1653 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1656 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1658 0x04, 0xd0, /* mov.l 1f,r0 */
1659 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1660 0x2b, 0x40, /* jmp @r0 */
1661 0x09, 0x00, /* nop */
1662 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1663 0x03, 0xd1, /* mov.l 2f,r1 */
1664 0x2b, 0x40, /* jmp @r0 */
1665 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1666 0x09, 0x00, /* nop */
1667 0x09, 0x00, /* nop */
1668 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1669 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1672 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1675 /* Big-endian non-PIC. */
1676 elf_sh_plt0_entry_be
,
1678 { MINUS_ONE
, 24, 20 },
1679 elf_sh_plt_entry_be
,
1681 { 20, 16, 24, FALSE
},
1686 /* Little-endian non-PIC. */
1687 elf_sh_plt0_entry_le
,
1689 { MINUS_ONE
, 24, 20 },
1690 elf_sh_plt_entry_le
,
1692 { 20, 16, 24, FALSE
},
1699 /* Big-endian PIC. */
1700 elf_sh_plt0_entry_be
,
1702 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1703 elf_sh_pic_plt_entry_be
,
1705 { 20, MINUS_ONE
, 24, FALSE
},
1710 /* Little-endian PIC. */
1711 elf_sh_plt0_entry_le
,
1713 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1714 elf_sh_pic_plt_entry_le
,
1716 { 20, MINUS_ONE
, 24, FALSE
},
1723 #define VXWORKS_PLT_HEADER_SIZE 12
1724 #define VXWORKS_PLT_ENTRY_SIZE 24
1726 static const bfd_byte vxworks_sh_plt0_entry_be
[VXWORKS_PLT_HEADER_SIZE
] =
1728 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1729 0x61, 0x12, /* mov.l @r1,r1 */
1730 0x41, 0x2b, /* jmp @r1 */
1731 0x00, 0x09, /* nop */
1732 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1735 static const bfd_byte vxworks_sh_plt0_entry_le
[VXWORKS_PLT_HEADER_SIZE
] =
1737 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1738 0x12, 0x61, /* mov.l @r1,r1 */
1739 0x2b, 0x41, /* jmp @r1 */
1740 0x09, 0x00, /* nop */
1741 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1744 static const bfd_byte vxworks_sh_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1746 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1747 0x60, 0x02, /* mov.l @r0,r0 */
1748 0x40, 0x2b, /* jmp @r0 */
1749 0x00, 0x09, /* nop */
1750 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1751 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1752 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1753 0x00, 0x09, /* nop */
1754 0x00, 0x09, /* nop */
1755 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1758 static const bfd_byte vxworks_sh_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1760 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1761 0x02, 0x60, /* mov.l @r0,r0 */
1762 0x2b, 0x40, /* jmp @r0 */
1763 0x09, 0x00, /* nop */
1764 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1765 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1766 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1767 0x09, 0x00, /* nop */
1768 0x09, 0x00, /* nop */
1769 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1772 static const bfd_byte vxworks_sh_pic_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1774 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1775 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1776 0x40, 0x2b, /* jmp @r0 */
1777 0x00, 0x09, /* nop */
1778 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1779 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1780 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1781 0x41, 0x2b, /* jmp @r1 */
1782 0x00, 0x09, /* nop */
1783 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1786 static const bfd_byte vxworks_sh_pic_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1788 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1789 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1790 0x2b, 0x40, /* jmp @r0 */
1791 0x09, 0x00, /* nop */
1792 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1793 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1794 0xc2, 0x51, /* mov.l @(8,r12),r1 */
1795 0x2b, 0x41, /* jmp @r1 */
1796 0x09, 0x00, /* nop */
1797 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1800 static const struct elf_sh_plt_info vxworks_sh_plts
[2][2] = {
1803 /* Big-endian non-PIC. */
1804 vxworks_sh_plt0_entry_be
,
1805 VXWORKS_PLT_HEADER_SIZE
,
1806 { MINUS_ONE
, MINUS_ONE
, 8 },
1807 vxworks_sh_plt_entry_be
,
1808 VXWORKS_PLT_ENTRY_SIZE
,
1809 { 8, 14, 20, FALSE
},
1814 /* Little-endian non-PIC. */
1815 vxworks_sh_plt0_entry_le
,
1816 VXWORKS_PLT_HEADER_SIZE
,
1817 { MINUS_ONE
, MINUS_ONE
, 8 },
1818 vxworks_sh_plt_entry_le
,
1819 VXWORKS_PLT_ENTRY_SIZE
,
1820 { 8, 14, 20, FALSE
},
1827 /* Big-endian PIC. */
1830 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1831 vxworks_sh_pic_plt_entry_be
,
1832 VXWORKS_PLT_ENTRY_SIZE
,
1833 { 8, MINUS_ONE
, 20, FALSE
},
1838 /* Little-endian PIC. */
1841 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1842 vxworks_sh_pic_plt_entry_le
,
1843 VXWORKS_PLT_ENTRY_SIZE
,
1844 { 8, MINUS_ONE
, 20, FALSE
},
1851 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
1852 binding are to omit the lazy binding stub when linking with -z now
1853 and to move lazy binding stubs into a separate region for better
1856 #define FDPIC_PLT_ENTRY_SIZE 28
1857 #define FDPIC_PLT_LAZY_OFFSET 20
1859 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
1860 duplicated if it is out of range, or which can be inlined. So
1861 right now it is always inlined, which wastes a word per stub. It
1862 might be easier to handle the duplication if we put the lazy
1863 stubs separately. */
1865 static const bfd_byte fdpic_sh_plt_entry_be
[FDPIC_PLT_ENTRY_SIZE
] =
1867 0xd0, 0x02, /* mov.l @(12,pc),r0 */
1868 0x01, 0xce, /* mov.l @(r0,r12),r1 */
1869 0x70, 0x04, /* add #4, r0 */
1870 0x41, 0x2b, /* jmp @r1 */
1871 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1872 0x00, 0x09, /* nop */
1873 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1874 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1875 0x60, 0xc2, /* mov.l @r12,r0 */
1876 0x40, 0x2b, /* jmp @r0 */
1877 0x53, 0xc1, /* mov.l @(4,r12),r3 */
1878 0x00, 0x09, /* nop */
1881 static const bfd_byte fdpic_sh_plt_entry_le
[FDPIC_PLT_ENTRY_SIZE
] =
1883 0x02, 0xd0, /* mov.l @(12,pc),r0 */
1884 0xce, 0x01, /* mov.l @(r0,r12),r1 */
1885 0x04, 0x70, /* add #4, r0 */
1886 0x2b, 0x41, /* jmp @r1 */
1887 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1888 0x09, 0x00, /* nop */
1889 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1890 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1891 0xc2, 0x60, /* mov.l @r12,r0 */
1892 0x2b, 0x40, /* jmp @r0 */
1893 0xc1, 0x53, /* mov.l @(4,r12),r3 */
1894 0x09, 0x00, /* nop */
1897 static const struct elf_sh_plt_info fdpic_sh_plts
[2] = {
1899 /* Big-endian PIC. */
1902 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1903 fdpic_sh_plt_entry_be
,
1904 FDPIC_PLT_ENTRY_SIZE
,
1905 { 12, MINUS_ONE
, 16, FALSE
},
1906 FDPIC_PLT_LAZY_OFFSET
,
1910 /* Little-endian PIC. */
1913 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1914 fdpic_sh_plt_entry_le
,
1915 FDPIC_PLT_ENTRY_SIZE
,
1916 { 12, MINUS_ONE
, 16, FALSE
},
1917 FDPIC_PLT_LAZY_OFFSET
,
1922 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
1923 entries for the first 64K slots. We use the normal FDPIC PLT entry
1924 past that point; we could also use movi20s, which might be faster,
1925 but would not be any smaller. */
1927 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
1928 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
1930 static const bfd_byte fdpic_sh2a_plt_entry_be
[FDPIC_SH2A_PLT_ENTRY_SIZE
] =
1932 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1933 0x01, 0xce, /* mov.l @(r0,r12),r1 */
1934 0x70, 0x04, /* add #4, r0 */
1935 0x41, 0x2b, /* jmp @r1 */
1936 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1937 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1938 0x60, 0xc2, /* mov.l @r12,r0 */
1939 0x40, 0x2b, /* jmp @r0 */
1940 0x53, 0xc1, /* mov.l @(4,r12),r3 */
1941 0x00, 0x09, /* nop */
1944 static const bfd_byte fdpic_sh2a_plt_entry_le
[FDPIC_SH2A_PLT_ENTRY_SIZE
] =
1946 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1947 0xce, 0x01, /* mov.l @(r0,r12),r1 */
1948 0x04, 0x70, /* add #4, r0 */
1949 0x2b, 0x41, /* jmp @r1 */
1950 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1951 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1952 0xc2, 0x60, /* mov.l @r12,r0 */
1953 0x2b, 0x40, /* jmp @r0 */
1954 0xc1, 0x53, /* mov.l @(4,r12),r3 */
1955 0x09, 0x00, /* nop */
1958 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be
= {
1959 /* Big-endian FDPIC, max index 64K. */
1962 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1963 fdpic_sh2a_plt_entry_be
,
1964 FDPIC_SH2A_PLT_ENTRY_SIZE
,
1965 { 0, MINUS_ONE
, 12, TRUE
},
1966 FDPIC_SH2A_PLT_LAZY_OFFSET
,
1970 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le
= {
1971 /* Little-endian FDPIC, max index 64K. */
1974 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1975 fdpic_sh2a_plt_entry_le
,
1976 FDPIC_SH2A_PLT_ENTRY_SIZE
,
1977 { 0, MINUS_ONE
, 12, TRUE
},
1978 FDPIC_SH2A_PLT_LAZY_OFFSET
,
1982 static const struct elf_sh_plt_info fdpic_sh2a_plts
[2] = {
1984 /* Big-endian PIC. */
1987 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1988 fdpic_sh_plt_entry_be
,
1989 FDPIC_PLT_ENTRY_SIZE
,
1990 { 12, MINUS_ONE
, 16, FALSE
},
1991 FDPIC_PLT_LAZY_OFFSET
,
1992 &fdpic_sh2a_short_plt_be
1995 /* Little-endian PIC. */
1998 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1999 fdpic_sh_plt_entry_le
,
2000 FDPIC_PLT_ENTRY_SIZE
,
2001 { 12, MINUS_ONE
, 16, FALSE
},
2002 FDPIC_PLT_LAZY_OFFSET
,
2003 &fdpic_sh2a_short_plt_le
2007 /* Return the type of PLT associated with ABFD. PIC_P is true if
2008 the object is position-independent. */
2010 static const struct elf_sh_plt_info
*
2011 get_plt_info (bfd
*abfd
, bfd_boolean pic_p
)
2013 if (fdpic_object_p (abfd
))
2015 /* If any input file requires SH2A we can use a shorter PLT
2017 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd
)) & arch_sh2a_base
)
2018 return &fdpic_sh2a_plts
[!bfd_big_endian (abfd
)];
2020 return &fdpic_sh_plts
[!bfd_big_endian (abfd
)];
2022 if (vxworks_object_p (abfd
))
2023 return &vxworks_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2024 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2027 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2028 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2032 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p ATTRIBUTE_UNUSED
,
2033 unsigned long value
, bfd_byte
*addr
)
2035 bfd_put_32 (output_bfd
, value
, addr
);
2038 /* The number of PLT entries which can use a shorter PLT, if any.
2039 Currently always 64K, since only SH-2A FDPIC uses this; a
2040 20-bit movi20 can address that many function descriptors below
2041 _GLOBAL_OFFSET_TABLE_. */
2042 #define MAX_SHORT_PLT 65536
2044 /* Return the index of the PLT entry at byte offset OFFSET. */
2047 get_plt_index (const struct elf_sh_plt_info
*info
, bfd_vma offset
)
2049 bfd_vma plt_index
= 0;
2051 offset
-= info
->plt0_entry_size
;
2052 if (info
->short_plt
!= NULL
)
2054 if (offset
> MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
)
2056 plt_index
= MAX_SHORT_PLT
;
2057 offset
-= MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
;
2060 info
= info
->short_plt
;
2062 return plt_index
+ offset
/ info
->symbol_entry_size
;
2065 /* Do the inverse operation. */
2068 get_plt_offset (const struct elf_sh_plt_info
*info
, bfd_vma plt_index
)
2072 if (info
->short_plt
!= NULL
)
2074 if (plt_index
> MAX_SHORT_PLT
)
2076 offset
= MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
;
2077 plt_index
-= MAX_SHORT_PLT
;
2080 info
= info
->short_plt
;
2082 return (offset
+ info
->plt0_entry_size
2083 + (plt_index
* info
->symbol_entry_size
));
2088 bfd_signed_vma refcount
;
2092 /* sh ELF linker hash entry. */
2094 struct elf_sh_link_hash_entry
2096 struct elf_link_hash_entry root
;
2098 bfd_signed_vma gotplt_refcount
;
2100 /* A local function descriptor, for FDPIC. The refcount counts
2101 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2102 relocations; the PLT and GOT entry are accounted
2103 for separately. After adjust_dynamic_symbol, the offset is
2104 MINUS_ONE if there is no local descriptor (dynamic linker
2105 managed and no PLT entry, or undefined weak non-dynamic).
2106 During check_relocs we do not yet know whether the local
2107 descriptor will be canonical. */
2108 union gotref funcdesc
;
2110 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2111 and thus require fixups or relocations. */
2112 bfd_signed_vma abs_funcdesc_refcount
;
2115 GOT_UNKNOWN
= 0, GOT_NORMAL
, GOT_TLS_GD
, GOT_TLS_IE
, GOT_FUNCDESC
2119 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2121 struct sh_elf_obj_tdata
2123 struct elf_obj_tdata root
;
2125 /* got_type for each local got entry. */
2126 char *local_got_type
;
2128 /* Function descriptor refcount and offset for each local symbol. */
2129 union gotref
*local_funcdesc
;
2132 #define sh_elf_tdata(abfd) \
2133 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2135 #define sh_elf_local_got_type(abfd) \
2136 (sh_elf_tdata (abfd)->local_got_type)
2138 #define sh_elf_local_funcdesc(abfd) \
2139 (sh_elf_tdata (abfd)->local_funcdesc)
2141 #define is_sh_elf(bfd) \
2142 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2143 && elf_tdata (bfd) != NULL \
2144 && elf_object_id (bfd) == SH_ELF_DATA)
2146 /* Override the generic function because we need to store sh_elf_obj_tdata
2147 as the specific tdata. */
2150 sh_elf_mkobject (bfd
*abfd
)
2152 return bfd_elf_allocate_object (abfd
, sizeof (struct sh_elf_obj_tdata
),
2156 /* sh ELF linker hash table. */
2158 struct elf_sh_link_hash_table
2160 struct elf_link_hash_table root
;
2162 /* Short-cuts to get to dynamic linker sections. */
2165 asection
*sfuncdesc
;
2166 asection
*srelfuncdesc
;
2169 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2172 /* Small local sym cache. */
2173 struct sym_cache sym_cache
;
2175 /* A counter or offset to track a TLS got entry. */
2178 bfd_signed_vma refcount
;
2182 /* The type of PLT to use. */
2183 const struct elf_sh_plt_info
*plt_info
;
2185 /* True if the target system is VxWorks. */
2186 bfd_boolean vxworks_p
;
2188 /* True if the target system uses FDPIC. */
2189 bfd_boolean fdpic_p
;
2192 /* Traverse an sh ELF linker hash table. */
2194 #define sh_elf_link_hash_traverse(table, func, info) \
2195 (elf_link_hash_traverse \
2197 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2200 /* Get the sh ELF linker hash table from a link_info structure. */
2202 #define sh_elf_hash_table(p) \
2203 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2204 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2206 /* Create an entry in an sh ELF linker hash table. */
2208 static struct bfd_hash_entry
*
2209 sh_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
2210 struct bfd_hash_table
*table
,
2213 struct elf_sh_link_hash_entry
*ret
=
2214 (struct elf_sh_link_hash_entry
*) entry
;
2216 /* Allocate the structure if it has not already been allocated by a
2218 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2219 ret
= ((struct elf_sh_link_hash_entry
*)
2220 bfd_hash_allocate (table
,
2221 sizeof (struct elf_sh_link_hash_entry
)));
2222 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2223 return (struct bfd_hash_entry
*) ret
;
2225 /* Call the allocation method of the superclass. */
2226 ret
= ((struct elf_sh_link_hash_entry
*)
2227 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2229 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2231 ret
->gotplt_refcount
= 0;
2232 ret
->funcdesc
.refcount
= 0;
2233 ret
->abs_funcdesc_refcount
= 0;
2234 ret
->got_type
= GOT_UNKNOWN
;
2237 return (struct bfd_hash_entry
*) ret
;
2240 /* Create an sh ELF linker hash table. */
2242 static struct bfd_link_hash_table
*
2243 sh_elf_link_hash_table_create (bfd
*abfd
)
2245 struct elf_sh_link_hash_table
*ret
;
2246 size_t amt
= sizeof (struct elf_sh_link_hash_table
);
2248 ret
= (struct elf_sh_link_hash_table
*) bfd_zmalloc (amt
);
2249 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2252 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2253 sh_elf_link_hash_newfunc
,
2254 sizeof (struct elf_sh_link_hash_entry
),
2261 ret
->vxworks_p
= vxworks_object_p (abfd
);
2262 ret
->fdpic_p
= fdpic_object_p (abfd
);
2264 return &ret
->root
.root
;
2268 sh_elf_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2269 struct bfd_link_info
*info
, asection
*p
)
2271 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
2273 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2277 /* We need dynamic symbols for every section, since segments can
2278 relocate independently. */
2279 switch (elf_section_data (p
)->this_hdr
.sh_type
)
2283 /* If sh_type is yet undecided, assume it could be
2284 SHT_PROGBITS/SHT_NOBITS. */
2288 /* There shouldn't be section relative relocations
2289 against any other section. */
2295 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2296 shortcuts to them in our hash table. */
2299 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
2301 struct elf_sh_link_hash_table
*htab
;
2303 if (! _bfd_elf_create_got_section (dynobj
, info
))
2306 htab
= sh_elf_hash_table (info
);
2310 htab
->sfuncdesc
= bfd_make_section_anyway_with_flags (dynobj
, ".got.funcdesc",
2311 (SEC_ALLOC
| SEC_LOAD
2314 | SEC_LINKER_CREATED
));
2315 if (htab
->sfuncdesc
== NULL
2316 || !bfd_set_section_alignment (htab
->sfuncdesc
, 2))
2319 htab
->srelfuncdesc
= bfd_make_section_anyway_with_flags (dynobj
,
2320 ".rela.got.funcdesc",
2321 (SEC_ALLOC
| SEC_LOAD
2324 | SEC_LINKER_CREATED
2326 if (htab
->srelfuncdesc
== NULL
2327 || !bfd_set_section_alignment (htab
->srelfuncdesc
, 2))
2330 /* Also create .rofixup. */
2331 htab
->srofixup
= bfd_make_section_anyway_with_flags (dynobj
, ".rofixup",
2332 (SEC_ALLOC
| SEC_LOAD
2335 | SEC_LINKER_CREATED
2337 if (htab
->srofixup
== NULL
2338 || !bfd_set_section_alignment (htab
->srofixup
, 2))
2344 /* Create dynamic sections when linking against a dynamic object. */
2347 sh_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2349 struct elf_sh_link_hash_table
*htab
;
2350 flagword flags
, pltflags
;
2352 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2355 switch (bed
->s
->arch_size
)
2366 bfd_set_error (bfd_error_bad_value
);
2370 htab
= sh_elf_hash_table (info
);
2374 if (htab
->root
.dynamic_sections_created
)
2377 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2378 .rel[a].bss sections. */
2380 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2381 | SEC_LINKER_CREATED
);
2384 pltflags
|= SEC_CODE
;
2385 if (bed
->plt_not_loaded
)
2386 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2387 if (bed
->plt_readonly
)
2388 pltflags
|= SEC_READONLY
;
2390 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
2391 htab
->root
.splt
= s
;
2393 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
2396 if (bed
->want_plt_sym
)
2398 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2400 struct elf_link_hash_entry
*h
;
2401 struct bfd_link_hash_entry
*bh
= NULL
;
2403 if (! (_bfd_generic_link_add_one_symbol
2404 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2405 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
2406 get_elf_backend_data (abfd
)->collect
, &bh
)))
2409 h
= (struct elf_link_hash_entry
*) bh
;
2411 h
->type
= STT_OBJECT
;
2412 htab
->root
.hplt
= h
;
2414 if (bfd_link_pic (info
)
2415 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2419 s
= bfd_make_section_anyway_with_flags (abfd
,
2420 bed
->default_use_rela_p
2421 ? ".rela.plt" : ".rel.plt",
2422 flags
| SEC_READONLY
);
2423 htab
->root
.srelplt
= s
;
2425 || !bfd_set_section_alignment (s
, ptralign
))
2428 if (htab
->root
.sgot
== NULL
2429 && !create_got_section (abfd
, info
))
2432 if (bed
->want_dynbss
)
2434 /* The .dynbss section is a place to put symbols which are defined
2435 by dynamic objects, are referenced by regular objects, and are
2436 not functions. We must allocate space for them in the process
2437 image and use a R_*_COPY reloc to tell the dynamic linker to
2438 initialize them at run time. The linker script puts the .dynbss
2439 section into the .bss section of the final image. */
2440 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
2441 SEC_ALLOC
| SEC_LINKER_CREATED
);
2446 /* The .rel[a].bss section holds copy relocs. This section is not
2447 normally needed. We need to create it here, though, so that the
2448 linker will map it to an output section. We can't just create it
2449 only if we need it, because we will not know whether we need it
2450 until we have seen all the input files, and the first time the
2451 main linker code calls BFD after examining all the input files
2452 (size_dynamic_sections) the input sections have already been
2453 mapped to the output sections. If the section turns out not to
2454 be needed, we can discard it later. We will never need this
2455 section when generating a shared object, since they do not use
2457 if (! bfd_link_pic (info
))
2459 s
= bfd_make_section_anyway_with_flags (abfd
,
2460 (bed
->default_use_rela_p
2461 ? ".rela.bss" : ".rel.bss"),
2462 flags
| SEC_READONLY
);
2465 || !bfd_set_section_alignment (s
, ptralign
))
2470 if (htab
->vxworks_p
)
2472 if (!elf_vxworks_create_dynamic_sections (abfd
, info
, &htab
->srelplt2
))
2479 /* Find dynamic relocs for H that apply to read-only sections. */
2482 readonly_dynrelocs (struct elf_link_hash_entry
*h
)
2484 struct elf_dyn_relocs
*p
;
2486 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2488 asection
*s
= p
->sec
->output_section
;
2490 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2496 /* Adjust a symbol defined by a dynamic object and referenced by a
2497 regular object. The current definition is in some section of the
2498 dynamic object, but we're not including those sections. We have to
2499 change the definition to something the rest of the link can
2503 sh_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2504 struct elf_link_hash_entry
*h
)
2506 struct elf_sh_link_hash_table
*htab
;
2509 htab
= sh_elf_hash_table (info
);
2513 /* Make sure we know what is going on here. */
2514 BFD_ASSERT (htab
->root
.dynobj
!= NULL
2519 && !h
->def_regular
)));
2521 /* If this is a function, put it in the procedure linkage table. We
2522 will fill in the contents of the procedure linkage table later,
2523 when we know the address of the .got section. */
2524 if (h
->type
== STT_FUNC
2527 if (h
->plt
.refcount
<= 0
2528 || SYMBOL_CALLS_LOCAL (info
, h
)
2529 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2530 && h
->root
.type
== bfd_link_hash_undefweak
))
2532 /* This case can occur if we saw a PLT reloc in an input
2533 file, but the symbol was never referred to by a dynamic
2534 object. In such a case, we don't actually need to build
2535 a procedure linkage table, and we can just do a REL32
2537 h
->plt
.offset
= (bfd_vma
) -1;
2544 h
->plt
.offset
= (bfd_vma
) -1;
2546 /* If this is a weak symbol, and there is a real definition, the
2547 processor independent code will have arranged for us to see the
2548 real definition first, and we can just use the same value. */
2549 if (h
->is_weakalias
)
2551 struct elf_link_hash_entry
*def
= weakdef (h
);
2552 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
2553 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
2554 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
2555 if (info
->nocopyreloc
)
2556 h
->non_got_ref
= def
->non_got_ref
;
2560 /* This is a reference to a symbol defined by a dynamic object which
2561 is not a function. */
2563 /* If we are creating a shared library, we must presume that the
2564 only references to the symbol are via the global offset table.
2565 For such cases we need not do anything here; the relocations will
2566 be handled correctly by relocate_section. */
2567 if (bfd_link_pic (info
))
2570 /* If there are no references to this symbol that do not use the
2571 GOT, we don't need to generate a copy reloc. */
2572 if (!h
->non_got_ref
)
2575 /* If -z nocopyreloc was given, we won't generate them either. */
2576 if (0 && info
->nocopyreloc
)
2582 /* If we don't find any dynamic relocs in read-only sections, then
2583 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2584 if (0 && !readonly_dynrelocs (h
))
2590 /* We must allocate the symbol in our .dynbss section, which will
2591 become part of the .bss section of the executable. There will be
2592 an entry for this symbol in the .dynsym section. The dynamic
2593 object will contain position independent code, so all references
2594 from the dynamic object to this symbol will go through the global
2595 offset table. The dynamic linker will use the .dynsym entry to
2596 determine the address it must put in the global offset table, so
2597 both the dynamic object and the regular object will refer to the
2598 same memory location for the variable. */
2601 BFD_ASSERT (s
!= NULL
);
2603 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2604 copy the initial value out of the dynamic object and into the
2605 runtime process image. We need to remember the offset into the
2606 .rela.bss section we are going to use. */
2607 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
2611 srel
= htab
->srelbss
;
2612 BFD_ASSERT (srel
!= NULL
);
2613 srel
->size
+= sizeof (Elf32_External_Rela
);
2617 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
2620 /* Allocate space in .plt, .got and associated reloc sections for
2624 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2626 struct bfd_link_info
*info
;
2627 struct elf_sh_link_hash_table
*htab
;
2628 struct elf_sh_link_hash_entry
*eh
;
2629 struct elf_dyn_relocs
*p
;
2631 if (h
->root
.type
== bfd_link_hash_indirect
)
2634 info
= (struct bfd_link_info
*) inf
;
2635 htab
= sh_elf_hash_table (info
);
2639 eh
= (struct elf_sh_link_hash_entry
*) h
;
2640 if ((h
->got
.refcount
> 0
2642 && eh
->gotplt_refcount
> 0)
2644 /* The symbol has been forced local, or we have some direct got refs,
2645 so treat all the gotplt refs as got refs. */
2646 h
->got
.refcount
+= eh
->gotplt_refcount
;
2647 if (h
->plt
.refcount
>= eh
->gotplt_refcount
)
2648 h
->plt
.refcount
-= eh
->gotplt_refcount
;
2651 if (htab
->root
.dynamic_sections_created
2652 && h
->plt
.refcount
> 0
2653 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2654 || h
->root
.type
!= bfd_link_hash_undefweak
))
2656 /* Make sure this symbol is output as a dynamic symbol.
2657 Undefined weak syms won't yet be marked as dynamic. */
2658 if (h
->dynindx
== -1
2659 && !h
->forced_local
)
2661 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2665 if (bfd_link_pic (info
)
2666 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2668 asection
*s
= htab
->root
.splt
;
2669 const struct elf_sh_plt_info
*plt_info
;
2671 /* If this is the first .plt entry, make room for the special
2674 s
->size
+= htab
->plt_info
->plt0_entry_size
;
2676 h
->plt
.offset
= s
->size
;
2678 /* If this symbol is not defined in a regular file, and we are
2679 not generating a shared library, then set the symbol to this
2680 location in the .plt. This is required to make function
2681 pointers compare as equal between the normal executable and
2682 the shared library. Skip this for FDPIC, since the
2683 function's address will be the address of the canonical
2684 function descriptor. */
2685 if (!htab
->fdpic_p
&& !bfd_link_pic (info
) && !h
->def_regular
)
2687 h
->root
.u
.def
.section
= s
;
2688 h
->root
.u
.def
.value
= h
->plt
.offset
;
2691 /* Make room for this entry. */
2692 plt_info
= htab
->plt_info
;
2693 if (plt_info
->short_plt
!= NULL
2694 && (get_plt_index (plt_info
->short_plt
, s
->size
) < MAX_SHORT_PLT
))
2695 plt_info
= plt_info
->short_plt
;
2696 s
->size
+= plt_info
->symbol_entry_size
;
2698 /* We also need to make an entry in the .got.plt section, which
2699 will be placed in the .got section by the linker script. */
2701 htab
->root
.sgotplt
->size
+= 4;
2703 htab
->root
.sgotplt
->size
+= 8;
2705 /* We also need to make an entry in the .rel.plt section. */
2706 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
2708 if (htab
->vxworks_p
&& !bfd_link_pic (info
))
2710 /* VxWorks executables have a second set of relocations
2711 for each PLT entry. They go in a separate relocation
2712 section, which is processed by the kernel loader. */
2714 /* There is a relocation for the initial PLT entry:
2715 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2716 if (h
->plt
.offset
== htab
->plt_info
->plt0_entry_size
)
2717 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
);
2719 /* There are two extra relocations for each subsequent
2720 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2721 and an R_SH_DIR32 relocation for the PLT entry. */
2722 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
) * 2;
2727 h
->plt
.offset
= (bfd_vma
) -1;
2733 h
->plt
.offset
= (bfd_vma
) -1;
2737 if (h
->got
.refcount
> 0)
2741 enum got_type got_type
= sh_elf_hash_entry (h
)->got_type
;
2743 /* Make sure this symbol is output as a dynamic symbol.
2744 Undefined weak syms won't yet be marked as dynamic. */
2745 if (h
->dynindx
== -1
2746 && !h
->forced_local
)
2748 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2752 s
= htab
->root
.sgot
;
2753 h
->got
.offset
= s
->size
;
2755 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2756 if (got_type
== GOT_TLS_GD
)
2758 dyn
= htab
->root
.dynamic_sections_created
;
2761 /* No dynamic relocations required. */
2762 if (htab
->fdpic_p
&& !bfd_link_pic (info
)
2763 && h
->root
.type
!= bfd_link_hash_undefweak
2764 && (got_type
== GOT_NORMAL
|| got_type
== GOT_FUNCDESC
))
2765 htab
->srofixup
->size
+= 4;
2767 /* No dynamic relocations required when IE->LE conversion happens. */
2768 else if (got_type
== GOT_TLS_IE
2770 && !bfd_link_pic (info
))
2772 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2773 R_SH_TLS_GD needs one if local symbol and two if global. */
2774 else if ((got_type
== GOT_TLS_GD
&& h
->dynindx
== -1)
2775 || got_type
== GOT_TLS_IE
)
2776 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2777 else if (got_type
== GOT_TLS_GD
)
2778 htab
->root
.srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
2779 else if (got_type
== GOT_FUNCDESC
)
2781 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2782 htab
->srofixup
->size
+= 4;
2784 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2786 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2787 || h
->root
.type
!= bfd_link_hash_undefweak
)
2788 && (bfd_link_pic (info
)
2789 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
2790 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2791 else if (htab
->fdpic_p
2792 && !bfd_link_pic (info
)
2793 && got_type
== GOT_NORMAL
2794 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2795 || h
->root
.type
!= bfd_link_hash_undefweak
))
2796 htab
->srofixup
->size
+= 4;
2799 h
->got
.offset
= (bfd_vma
) -1;
2801 /* Allocate space for any dynamic relocations to function
2802 descriptors, canonical or otherwise. We need to relocate the
2803 reference unless it resolves to zero, which only happens for
2804 undefined weak symbols (either non-default visibility, or when
2805 static linking). Any GOT slot is accounted for elsewhere. */
2806 if (eh
->abs_funcdesc_refcount
> 0
2807 && (h
->root
.type
!= bfd_link_hash_undefweak
2808 || (htab
->root
.dynamic_sections_created
2809 && ! SYMBOL_CALLS_LOCAL (info
, h
))))
2811 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2812 htab
->srofixup
->size
+= eh
->abs_funcdesc_refcount
* 4;
2814 htab
->root
.srelgot
->size
2815 += eh
->abs_funcdesc_refcount
* sizeof (Elf32_External_Rela
);
2818 /* We must allocate a function descriptor if there are references to
2819 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
2820 the dynamic linker isn't going to allocate it. None of this
2821 applies if we already created one in .got.plt, but if the
2822 canonical function descriptor can be in this object, there
2823 won't be a PLT entry at all. */
2824 if ((eh
->funcdesc
.refcount
> 0
2825 || (h
->got
.offset
!= MINUS_ONE
&& eh
->got_type
== GOT_FUNCDESC
))
2826 && h
->root
.type
!= bfd_link_hash_undefweak
2827 && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2829 /* Make room for this function descriptor. */
2830 eh
->funcdesc
.offset
= htab
->sfuncdesc
->size
;
2831 htab
->sfuncdesc
->size
+= 8;
2833 /* We will need a relocation or two fixups to initialize the
2834 function descriptor, so allocate those too. */
2835 if (!bfd_link_pic (info
) && SYMBOL_CALLS_LOCAL (info
, h
))
2836 htab
->srofixup
->size
+= 8;
2838 htab
->srelfuncdesc
->size
+= sizeof (Elf32_External_Rela
);
2841 if (h
->dyn_relocs
== NULL
)
2844 /* In the shared -Bsymbolic case, discard space allocated for
2845 dynamic pc-relative relocs against symbols which turn out to be
2846 defined in regular objects. For the normal shared case, discard
2847 space for pc-relative relocs that have become local due to symbol
2848 visibility changes. */
2850 if (bfd_link_pic (info
))
2852 if (SYMBOL_CALLS_LOCAL (info
, h
))
2854 struct elf_dyn_relocs
**pp
;
2856 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
2858 p
->count
-= p
->pc_count
;
2867 if (htab
->vxworks_p
)
2869 struct elf_dyn_relocs
**pp
;
2871 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
2873 if (strcmp (p
->sec
->output_section
->name
, ".tls_vars") == 0)
2880 /* Also discard relocs on undefined weak syms with non-default
2882 if (h
->dyn_relocs
!= NULL
2883 && h
->root
.type
== bfd_link_hash_undefweak
)
2885 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2886 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2887 h
->dyn_relocs
= NULL
;
2889 /* Make sure undefined weak symbols are output as a dynamic
2891 else if (h
->dynindx
== -1
2892 && !h
->forced_local
)
2894 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2901 /* For the non-shared case, discard space for relocs against
2902 symbols which turn out to need copy relocs or are not
2908 || (htab
->root
.dynamic_sections_created
2909 && (h
->root
.type
== bfd_link_hash_undefweak
2910 || h
->root
.type
== bfd_link_hash_undefined
))))
2912 /* Make sure this symbol is output as a dynamic symbol.
2913 Undefined weak syms won't yet be marked as dynamic. */
2914 if (h
->dynindx
== -1
2915 && !h
->forced_local
)
2917 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2921 /* If that succeeded, we know we'll be keeping all the
2923 if (h
->dynindx
!= -1)
2927 h
->dyn_relocs
= NULL
;
2932 /* Finally, allocate space. */
2933 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2935 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
2936 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
2938 /* If we need relocations, we do not need fixups. */
2939 if (htab
->fdpic_p
&& !bfd_link_pic (info
))
2940 htab
->srofixup
->size
-= 4 * (p
->count
- p
->pc_count
);
2946 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2947 read-only sections. */
2950 maybe_set_textrel (struct elf_link_hash_entry
*h
, void *info_p
)
2954 if (h
->root
.type
== bfd_link_hash_indirect
)
2957 sec
= readonly_dynrelocs (h
);
2960 struct bfd_link_info
*info
= (struct bfd_link_info
*) info_p
;
2962 info
->flags
|= DF_TEXTREL
;
2963 info
->callbacks
->minfo
2964 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
2965 sec
->owner
, h
->root
.root
.string
, sec
);
2967 /* Not an error, just cut short the traversal. */
2973 /* This function is called after all the input files have been read,
2974 and the input sections have been assigned to output sections.
2975 It's a convenient place to determine the PLT style. */
2978 sh_elf_always_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
2980 sh_elf_hash_table (info
)->plt_info
= get_plt_info (output_bfd
,
2981 bfd_link_pic (info
));
2983 if (sh_elf_hash_table (info
)->fdpic_p
&& !bfd_link_relocatable (info
)
2984 && !bfd_elf_stack_segment_size (output_bfd
, info
,
2985 "__stacksize", DEFAULT_STACK_SIZE
))
2990 /* Set the sizes of the dynamic sections. */
2993 sh_elf_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2994 struct bfd_link_info
*info
)
2996 struct elf_sh_link_hash_table
*htab
;
3002 htab
= sh_elf_hash_table (info
);
3006 dynobj
= htab
->root
.dynobj
;
3007 BFD_ASSERT (dynobj
!= NULL
);
3009 if (htab
->root
.dynamic_sections_created
)
3011 /* Set the contents of the .interp section to the interpreter. */
3012 if (bfd_link_executable (info
) && !info
->nointerp
)
3014 s
= bfd_get_linker_section (dynobj
, ".interp");
3015 BFD_ASSERT (s
!= NULL
);
3016 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
3017 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
3021 /* Set up .got offsets for local syms, and space for local dynamic
3023 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
3025 bfd_signed_vma
*local_got
;
3026 bfd_signed_vma
*end_local_got
;
3027 union gotref
*local_funcdesc
, *end_local_funcdesc
;
3028 char *local_got_type
;
3029 bfd_size_type locsymcount
;
3030 Elf_Internal_Shdr
*symtab_hdr
;
3033 if (! is_sh_elf (ibfd
))
3036 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3038 struct elf_dyn_relocs
*p
;
3040 for (p
= ((struct elf_dyn_relocs
*)
3041 elf_section_data (s
)->local_dynrel
);
3045 if (! bfd_is_abs_section (p
->sec
)
3046 && bfd_is_abs_section (p
->sec
->output_section
))
3048 /* Input section has been discarded, either because
3049 it is a copy of a linkonce section or due to
3050 linker script /DISCARD/, so we'll be discarding
3053 else if (htab
->vxworks_p
3054 && strcmp (p
->sec
->output_section
->name
,
3057 /* Relocations in vxworks .tls_vars sections are
3058 handled specially by the loader. */
3060 else if (p
->count
!= 0)
3062 srel
= elf_section_data (p
->sec
)->sreloc
;
3063 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
3064 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
3066 info
->flags
|= DF_TEXTREL
;
3067 info
->callbacks
->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
3068 p
->sec
->owner
, p
->sec
);
3071 /* If we need relocations, we do not need fixups. */
3072 if (htab
->fdpic_p
&& !bfd_link_pic (info
))
3073 htab
->srofixup
->size
-= 4 * (p
->count
- p
->pc_count
);
3078 symtab_hdr
= &elf_symtab_hdr (ibfd
);
3079 locsymcount
= symtab_hdr
->sh_info
;
3080 s
= htab
->root
.sgot
;
3081 srel
= htab
->root
.srelgot
;
3083 local_got
= elf_local_got_refcounts (ibfd
);
3086 end_local_got
= local_got
+ locsymcount
;
3087 local_got_type
= sh_elf_local_got_type (ibfd
);
3088 local_funcdesc
= sh_elf_local_funcdesc (ibfd
);
3089 for (; local_got
< end_local_got
; ++local_got
)
3093 *local_got
= s
->size
;
3095 if (*local_got_type
== GOT_TLS_GD
)
3097 if (bfd_link_pic (info
))
3098 srel
->size
+= sizeof (Elf32_External_Rela
);
3100 htab
->srofixup
->size
+= 4;
3102 if (*local_got_type
== GOT_FUNCDESC
)
3104 if (local_funcdesc
== NULL
)
3108 size
= locsymcount
* sizeof (union gotref
);
3109 local_funcdesc
= (union gotref
*) bfd_zalloc (ibfd
,
3111 if (local_funcdesc
== NULL
)
3113 sh_elf_local_funcdesc (ibfd
) = local_funcdesc
;
3114 local_funcdesc
+= (local_got
3115 - elf_local_got_refcounts (ibfd
));
3117 local_funcdesc
->refcount
++;
3122 *local_got
= (bfd_vma
) -1;
3127 local_funcdesc
= sh_elf_local_funcdesc (ibfd
);
3130 end_local_funcdesc
= local_funcdesc
+ locsymcount
;
3132 for (; local_funcdesc
< end_local_funcdesc
; ++local_funcdesc
)
3134 if (local_funcdesc
->refcount
> 0)
3136 local_funcdesc
->offset
= htab
->sfuncdesc
->size
;
3137 htab
->sfuncdesc
->size
+= 8;
3138 if (!bfd_link_pic (info
))
3139 htab
->srofixup
->size
+= 8;
3141 htab
->srelfuncdesc
->size
+= sizeof (Elf32_External_Rela
);
3144 local_funcdesc
->offset
= MINUS_ONE
;
3150 if (htab
->tls_ldm_got
.refcount
> 0)
3152 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3154 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
3155 htab
->root
.sgot
->size
+= 8;
3156 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
3159 htab
->tls_ldm_got
.offset
= -1;
3161 /* Only the reserved entries should be present. For FDPIC, they go at
3162 the end of .got.plt. */
3165 BFD_ASSERT (htab
->root
.sgotplt
&& htab
->root
.sgotplt
->size
== 12);
3166 htab
->root
.sgotplt
->size
= 0;
3169 /* Allocate global sym .plt and .got entries, and space for global
3170 sym dynamic relocs. */
3171 elf_link_hash_traverse (&htab
->root
, allocate_dynrelocs
, info
);
3173 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3174 end of the FDPIC .got.plt. */
3177 htab
->root
.hgot
->root
.u
.def
.value
= htab
->root
.sgotplt
->size
;
3178 htab
->root
.sgotplt
->size
+= 12;
3181 /* At the very end of the .rofixup section is a pointer to the GOT. */
3182 if (htab
->fdpic_p
&& htab
->srofixup
!= NULL
)
3183 htab
->srofixup
->size
+= 4;
3185 /* We now have determined the sizes of the various dynamic sections.
3186 Allocate memory for them. */
3188 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
3190 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
3193 if (s
== htab
->root
.splt
3194 || s
== htab
->root
.sgot
3195 || s
== htab
->root
.sgotplt
3196 || s
== htab
->sfuncdesc
3197 || s
== htab
->srofixup
3198 || s
== htab
->sdynbss
)
3200 /* Strip this section if we don't need it; see the
3203 else if (CONST_STRNEQ (bfd_section_name (s
), ".rela"))
3205 if (s
->size
!= 0 && s
!= htab
->root
.srelplt
&& s
!= htab
->srelplt2
)
3208 /* We use the reloc_count field as a counter if we need
3209 to copy relocs into the output file. */
3214 /* It's not one of our sections, so don't allocate space. */
3220 /* If we don't need this section, strip it from the
3221 output file. This is mostly to handle .rela.bss and
3222 .rela.plt. We must create both sections in
3223 create_dynamic_sections, because they must be created
3224 before the linker maps input sections to output
3225 sections. The linker does that before
3226 adjust_dynamic_symbol is called, and it is that
3227 function which decides whether anything needs to go
3228 into these sections. */
3230 s
->flags
|= SEC_EXCLUDE
;
3234 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3237 /* Allocate memory for the section contents. We use bfd_zalloc
3238 here in case unused entries are not reclaimed before the
3239 section's contents are written out. This should not happen,
3240 but this way if it does, we get a R_SH_NONE reloc instead
3242 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
3243 if (s
->contents
== NULL
)
3247 if (htab
->root
.dynamic_sections_created
)
3249 /* Add some entries to the .dynamic section. We fill in the
3250 values later, in sh_elf_finish_dynamic_sections, but we
3251 must add the entries now so that we get the correct size for
3252 the .dynamic section. The DT_DEBUG entry is filled in by the
3253 dynamic linker and used by the debugger. */
3254 #define add_dynamic_entry(TAG, VAL) \
3255 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3257 if (bfd_link_executable (info
))
3259 if (! add_dynamic_entry (DT_DEBUG
, 0))
3263 if (htab
->root
.splt
->size
!= 0)
3265 if (! add_dynamic_entry (DT_PLTGOT
, 0)
3266 || ! add_dynamic_entry (DT_PLTRELSZ
, 0)
3267 || ! add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3268 || ! add_dynamic_entry (DT_JMPREL
, 0))
3271 else if ((elf_elfheader (output_bfd
)->e_flags
& EF_SH_FDPIC
))
3273 if (! add_dynamic_entry (DT_PLTGOT
, 0))
3279 if (! add_dynamic_entry (DT_RELA
, 0)
3280 || ! add_dynamic_entry (DT_RELASZ
, 0)
3281 || ! add_dynamic_entry (DT_RELAENT
,
3282 sizeof (Elf32_External_Rela
)))
3285 /* If any dynamic relocs apply to a read-only section,
3286 then we need a DT_TEXTREL entry. */
3287 if ((info
->flags
& DF_TEXTREL
) == 0)
3288 elf_link_hash_traverse (&htab
->root
, maybe_set_textrel
, info
);
3290 if ((info
->flags
& DF_TEXTREL
) != 0)
3292 if (! add_dynamic_entry (DT_TEXTREL
, 0))
3297 && !elf_vxworks_add_dynamic_entries (output_bfd
, info
))
3300 #undef add_dynamic_entry
3305 /* Add a dynamic relocation to the SRELOC section. */
3307 inline static bfd_vma
3308 sh_elf_add_dyn_reloc (bfd
*output_bfd
, asection
*sreloc
, bfd_vma offset
,
3309 int reloc_type
, long dynindx
, bfd_vma addend
)
3311 Elf_Internal_Rela outrel
;
3312 bfd_vma reloc_offset
;
3314 outrel
.r_offset
= offset
;
3315 outrel
.r_info
= ELF32_R_INFO (dynindx
, reloc_type
);
3316 outrel
.r_addend
= addend
;
3318 reloc_offset
= sreloc
->reloc_count
* sizeof (Elf32_External_Rela
);
3319 BFD_ASSERT (reloc_offset
< sreloc
->size
);
3320 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3321 sreloc
->contents
+ reloc_offset
);
3322 sreloc
->reloc_count
++;
3324 return reloc_offset
;
3327 /* Add an FDPIC read-only fixup. */
3330 sh_elf_add_rofixup (bfd
*output_bfd
, asection
*srofixup
, bfd_vma offset
)
3332 bfd_vma fixup_offset
;
3334 fixup_offset
= srofixup
->reloc_count
++ * 4;
3335 BFD_ASSERT (fixup_offset
< srofixup
->size
);
3336 bfd_put_32 (output_bfd
, offset
, srofixup
->contents
+ fixup_offset
);
3339 /* Return the offset of the generated .got section from the
3340 _GLOBAL_OFFSET_TABLE_ symbol. */
3342 static bfd_signed_vma
3343 sh_elf_got_offset (struct elf_sh_link_hash_table
*htab
)
3345 return (htab
->root
.sgot
->output_offset
- htab
->root
.sgotplt
->output_offset
3346 - htab
->root
.hgot
->root
.u
.def
.value
);
3349 /* Find the segment number in which OSEC, and output section, is
3353 sh_elf_osec_to_segment (bfd
*output_bfd
, asection
*osec
)
3355 Elf_Internal_Phdr
*p
= NULL
;
3357 if (output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
3358 /* PR ld/17110: Do not look for output segments in an input bfd. */
3359 && output_bfd
->direction
!= read_direction
)
3360 p
= _bfd_elf_find_segment_containing_section (output_bfd
, osec
);
3362 /* FIXME: Nothing ever says what this index is relative to. The kernel
3363 supplies data in terms of the number of load segments but this is
3364 a phdr index and the first phdr may not be a load segment. */
3365 return (p
!= NULL
) ? p
- elf_tdata (output_bfd
)->phdr
: -1;
3369 sh_elf_osec_readonly_p (bfd
*output_bfd
, asection
*osec
)
3371 unsigned seg
= sh_elf_osec_to_segment (output_bfd
, osec
);
3373 return (seg
!= (unsigned) -1
3374 && ! (elf_tdata (output_bfd
)->phdr
[seg
].p_flags
& PF_W
));
3377 /* Generate the initial contents of a local function descriptor, along
3378 with any relocations or fixups required. */
3380 sh_elf_initialize_funcdesc (bfd
*output_bfd
,
3381 struct bfd_link_info
*info
,
3382 struct elf_link_hash_entry
*h
,
3387 struct elf_sh_link_hash_table
*htab
;
3391 htab
= sh_elf_hash_table (info
);
3393 /* FIXME: The ABI says that the offset to the function goes in the
3394 descriptor, along with the segment index. We're RELA, so it could
3395 go in the reloc instead... */
3397 if (h
!= NULL
&& SYMBOL_CALLS_LOCAL (info
, h
))
3399 section
= h
->root
.u
.def
.section
;
3400 value
= h
->root
.u
.def
.value
;
3403 if (h
== NULL
|| SYMBOL_CALLS_LOCAL (info
, h
))
3405 dynindx
= elf_section_data (section
->output_section
)->dynindx
;
3406 addr
= value
+ section
->output_offset
;
3407 seg
= sh_elf_osec_to_segment (output_bfd
, section
->output_section
);
3411 BFD_ASSERT (h
->dynindx
!= -1);
3412 dynindx
= h
->dynindx
;
3416 if (!bfd_link_pic (info
) && SYMBOL_CALLS_LOCAL (info
, h
))
3418 if (h
== NULL
|| h
->root
.type
!= bfd_link_hash_undefweak
)
3420 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
3422 + htab
->sfuncdesc
->output_section
->vma
3423 + htab
->sfuncdesc
->output_offset
);
3424 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
3426 + htab
->sfuncdesc
->output_section
->vma
3427 + htab
->sfuncdesc
->output_offset
);
3430 /* There are no dynamic relocations so fill in the final
3431 address and gp value (barring fixups). */
3432 addr
+= section
->output_section
->vma
;
3433 seg
= htab
->root
.hgot
->root
.u
.def
.value
3434 + htab
->root
.hgot
->root
.u
.def
.section
->output_section
->vma
3435 + htab
->root
.hgot
->root
.u
.def
.section
->output_offset
;
3438 sh_elf_add_dyn_reloc (output_bfd
, htab
->srelfuncdesc
,
3440 + htab
->sfuncdesc
->output_section
->vma
3441 + htab
->sfuncdesc
->output_offset
,
3442 R_SH_FUNCDESC_VALUE
, dynindx
, 0);
3444 bfd_put_32 (output_bfd
, addr
, htab
->sfuncdesc
->contents
+ offset
);
3445 bfd_put_32 (output_bfd
, seg
, htab
->sfuncdesc
->contents
+ offset
+ 4);
3450 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3451 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3454 static bfd_reloc_status_type
3455 install_movi20_field (bfd
*output_bfd
, unsigned long relocation
,
3456 bfd
*input_bfd
, asection
*input_section
,
3457 bfd_byte
*contents
, bfd_vma offset
)
3459 unsigned long cur_val
;
3461 bfd_reloc_status_type r
;
3463 if (offset
> bfd_get_section_limit (input_bfd
, input_section
))
3464 return bfd_reloc_outofrange
;
3466 r
= bfd_check_overflow (complain_overflow_signed
, 20, 0,
3467 bfd_arch_bits_per_address (input_bfd
), relocation
);
3468 if (r
!= bfd_reloc_ok
)
3471 addr
= contents
+ offset
;
3472 cur_val
= bfd_get_16 (output_bfd
, addr
);
3473 bfd_put_16 (output_bfd
, cur_val
| ((relocation
& 0xf0000) >> 12), addr
);
3474 bfd_put_16 (output_bfd
, relocation
& 0xffff, addr
+ 2);
3476 return bfd_reloc_ok
;
3479 /* Relocate an SH ELF section. */
3482 sh_elf_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
3483 bfd
*input_bfd
, asection
*input_section
,
3484 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
3485 Elf_Internal_Sym
*local_syms
,
3486 asection
**local_sections
)
3488 struct elf_sh_link_hash_table
*htab
;
3489 Elf_Internal_Shdr
*symtab_hdr
;
3490 struct elf_link_hash_entry
**sym_hashes
;
3491 Elf_Internal_Rela
*rel
, *relend
;
3492 bfd_vma
*local_got_offsets
;
3493 asection
*sgot
= NULL
;
3494 asection
*sgotplt
= NULL
;
3495 asection
*splt
= NULL
;
3496 asection
*sreloc
= NULL
;
3497 asection
*srelgot
= NULL
;
3498 bfd_boolean is_vxworks_tls
;
3499 unsigned isec_segment
, got_segment
, plt_segment
, check_segment
[2];
3500 bfd_boolean fdpic_p
= FALSE
;
3502 if (!is_sh_elf (input_bfd
))
3504 bfd_set_error (bfd_error_wrong_format
);
3508 htab
= sh_elf_hash_table (info
);
3511 sgot
= htab
->root
.sgot
;
3512 sgotplt
= htab
->root
.sgotplt
;
3513 srelgot
= htab
->root
.srelgot
;
3514 splt
= htab
->root
.splt
;
3515 fdpic_p
= htab
->fdpic_p
;
3517 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
3518 sym_hashes
= elf_sym_hashes (input_bfd
);
3519 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3521 isec_segment
= sh_elf_osec_to_segment (output_bfd
,
3522 input_section
->output_section
);
3523 if (fdpic_p
&& sgot
)
3524 got_segment
= sh_elf_osec_to_segment (output_bfd
,
3525 sgot
->output_section
);
3528 if (fdpic_p
&& splt
)
3529 plt_segment
= sh_elf_osec_to_segment (output_bfd
,
3530 splt
->output_section
);
3534 /* We have to handle relocations in vxworks .tls_vars sections
3535 specially, because the dynamic loader is 'weird'. */
3536 is_vxworks_tls
= (htab
&& htab
->vxworks_p
&& bfd_link_pic (info
)
3537 && !strcmp (input_section
->output_section
->name
,
3541 relend
= relocs
+ input_section
->reloc_count
;
3542 for (; rel
< relend
; rel
++)
3545 reloc_howto_type
*howto
;
3546 unsigned long r_symndx
;
3547 Elf_Internal_Sym
*sym
;
3549 struct elf_link_hash_entry
*h
;
3551 bfd_vma addend
= (bfd_vma
) 0;
3552 bfd_reloc_status_type r
;
3554 enum got_type got_type
;
3555 const char *symname
= NULL
;
3556 bfd_boolean resolved_to_zero
;
3558 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3560 r_type
= ELF32_R_TYPE (rel
->r_info
);
3562 /* Many of the relocs are only used for relaxing, and are
3563 handled entirely by the relaxation code. */
3564 if (r_type
>= (int) R_SH_GNU_VTINHERIT
3565 && r_type
<= (int) R_SH_LABEL
)
3567 if (r_type
== (int) R_SH_NONE
)
3571 || r_type
>= R_SH_max
3572 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
3573 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
3574 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
3575 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
)
3576 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_3
3577 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_3
)
3578 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_4
3579 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_4
)
3580 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_5
3581 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_5
)
3582 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_6
3583 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_6
))
3585 bfd_set_error (bfd_error_bad_value
);
3589 howto
= get_howto_table (output_bfd
) + r_type
;
3591 /* For relocs that aren't partial_inplace, we get the addend from
3593 if (! howto
->partial_inplace
)
3594 addend
= rel
->r_addend
;
3596 resolved_to_zero
= FALSE
;
3600 check_segment
[0] = -1;
3601 check_segment
[1] = -1;
3602 if (r_symndx
< symtab_hdr
->sh_info
)
3604 sym
= local_syms
+ r_symndx
;
3605 sec
= local_sections
[r_symndx
];
3607 symname
= bfd_elf_string_from_elf_section
3608 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
3609 if (symname
== NULL
|| *symname
== '\0')
3610 symname
= bfd_section_name (sec
);
3612 relocation
= (sec
->output_section
->vma
3613 + sec
->output_offset
3616 if (sec
!= NULL
&& discarded_section (sec
))
3617 /* Handled below. */
3619 else if (bfd_link_relocatable (info
))
3621 /* This is a relocatable link. We don't have to change
3622 anything, unless the reloc is against a section symbol,
3623 in which case we have to adjust according to where the
3624 section symbol winds up in the output section. */
3625 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3627 if (! howto
->partial_inplace
)
3629 /* For relocations with the addend in the
3630 relocation, we need just to update the addend.
3631 All real relocs are of type partial_inplace; this
3632 code is mostly for completeness. */
3633 rel
->r_addend
+= sec
->output_offset
;
3638 /* Relocs of type partial_inplace need to pick up the
3639 contents in the contents and add the offset resulting
3640 from the changed location of the section symbol.
3641 Using _bfd_final_link_relocate (e.g. goto
3642 final_link_relocate) here would be wrong, because
3643 relocations marked pc_relative would get the current
3644 location subtracted, and we must only do that at the
3646 r
= _bfd_relocate_contents (howto
, input_bfd
,
3649 contents
+ rel
->r_offset
);
3650 goto relocation_done
;
3655 else if (! howto
->partial_inplace
)
3657 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3658 addend
= rel
->r_addend
;
3660 else if ((sec
->flags
& SEC_MERGE
)
3661 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3665 if (howto
->rightshift
|| howto
->src_mask
!= 0xffffffff)
3668 /* xgettext:c-format */
3669 (_("%pB(%pA+%#" PRIx64
"): "
3670 "%s relocation against SEC_MERGE section"),
3671 input_bfd
, input_section
,
3672 (uint64_t) rel
->r_offset
, howto
->name
);
3676 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3679 _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
)
3681 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
3682 bfd_put_32 (input_bfd
, addend
, contents
+ rel
->r_offset
);
3688 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3691 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3692 symname
= h
->root
.root
.string
;
3693 while (h
->root
.type
== bfd_link_hash_indirect
3694 || h
->root
.type
== bfd_link_hash_warning
)
3695 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3696 if (h
->root
.type
== bfd_link_hash_defined
3697 || h
->root
.type
== bfd_link_hash_defweak
)
3701 dyn
= htab
? htab
->root
.dynamic_sections_created
: FALSE
;
3702 sec
= h
->root
.u
.def
.section
;
3703 /* In these cases, we don't need the relocation value.
3704 We check specially because in some obscure cases
3705 sec->output_section will be NULL. */
3706 if (r_type
== R_SH_GOTPC
3707 || r_type
== R_SH_GOTPC_LOW16
3708 || r_type
== R_SH_GOTPC_MEDLOW16
3709 || r_type
== R_SH_GOTPC_MEDHI16
3710 || r_type
== R_SH_GOTPC_HI16
3711 || ((r_type
== R_SH_PLT32
3712 || r_type
== R_SH_PLT_LOW16
3713 || r_type
== R_SH_PLT_MEDLOW16
3714 || r_type
== R_SH_PLT_MEDHI16
3715 || r_type
== R_SH_PLT_HI16
)
3716 && h
->plt
.offset
!= (bfd_vma
) -1)
3717 || ((r_type
== R_SH_GOT32
3718 || r_type
== R_SH_GOT20
3719 || r_type
== R_SH_GOTFUNCDESC
3720 || r_type
== R_SH_GOTFUNCDESC20
3721 || r_type
== R_SH_GOTOFFFUNCDESC
3722 || r_type
== R_SH_GOTOFFFUNCDESC20
3723 || r_type
== R_SH_FUNCDESC
3724 || r_type
== R_SH_GOT_LOW16
3725 || r_type
== R_SH_GOT_MEDLOW16
3726 || r_type
== R_SH_GOT_MEDHI16
3727 || r_type
== R_SH_GOT_HI16
)
3728 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
3729 bfd_link_pic (info
),
3731 && (! bfd_link_pic (info
)
3732 || (! info
->symbolic
&& h
->dynindx
!= -1)
3733 || !h
->def_regular
))
3734 /* The cases above are those in which relocation is
3735 overwritten in the switch block below. The cases
3736 below are those in which we must defer relocation
3737 to run-time, because we can't resolve absolute
3738 addresses when creating a shared library. */
3739 || (bfd_link_pic (info
)
3740 && ((! info
->symbolic
&& h
->dynindx
!= -1)
3742 && ((r_type
== R_SH_DIR32
3743 && !h
->forced_local
)
3744 || (r_type
== R_SH_REL32
3745 && !SYMBOL_CALLS_LOCAL (info
, h
)))
3746 && ((input_section
->flags
& SEC_ALLOC
) != 0
3747 /* DWARF will emit R_SH_DIR32 relocations in its
3748 sections against symbols defined externally
3749 in shared libraries. We can't do anything
3751 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
3752 && h
->def_dynamic
)))
3753 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3754 sections because such sections are not SEC_ALLOC and
3755 thus ld.so will not process them. */
3756 || (sec
->output_section
== NULL
3757 && ((input_section
->flags
& SEC_DEBUGGING
) != 0
3759 || (sec
->output_section
== NULL
3760 && (sh_elf_hash_entry (h
)->got_type
== GOT_TLS_IE
3761 || sh_elf_hash_entry (h
)->got_type
== GOT_TLS_GD
)))
3763 else if (sec
->output_section
!= NULL
)
3764 relocation
= (h
->root
.u
.def
.value
3765 + sec
->output_section
->vma
3766 + sec
->output_offset
);
3767 else if (!bfd_link_relocatable (info
)
3768 && (_bfd_elf_section_offset (output_bfd
, info
,
3774 /* xgettext:c-format */
3775 (_("%pB(%pA+%#" PRIx64
"): "
3776 "unresolvable %s relocation against symbol `%s'"),
3779 (uint64_t) rel
->r_offset
,
3781 h
->root
.root
.string
);
3785 else if (h
->root
.type
== bfd_link_hash_undefweak
)
3786 resolved_to_zero
= UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
);
3787 else if (info
->unresolved_syms_in_objects
== RM_IGNORE
3788 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3790 else if (!bfd_link_relocatable (info
))
3791 info
->callbacks
->undefined_symbol
3792 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
3794 (info
->unresolved_syms_in_objects
== RM_DIAGNOSE
3795 && !info
->warn_unresolved_syms
)
3796 || ELF_ST_VISIBILITY (h
->other
));
3799 if (sec
!= NULL
&& discarded_section (sec
))
3800 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
3801 rel
, 1, relend
, howto
, 0, contents
);
3803 if (bfd_link_relocatable (info
))
3806 /* Check for inter-segment relocations in FDPIC files. Most
3807 relocations connect the relocation site to the location of
3808 the target symbol, but there are some exceptions below. */
3809 check_segment
[0] = isec_segment
;
3811 check_segment
[1] = sh_elf_osec_to_segment (output_bfd
,
3812 sec
->output_section
);
3814 check_segment
[1] = -1;
3816 switch ((int) r_type
)
3818 final_link_relocate
:
3819 /* COFF relocs don't use the addend. The addend is used for
3820 R_SH_DIR32 to be compatible with other compilers. */
3821 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3822 contents
, rel
->r_offset
,
3823 relocation
, addend
);
3827 goto final_link_relocate
;
3832 /* If the reloc is against the start of this section, then
3833 the assembler has already taken care of it and the reloc
3834 is here only to assist in relaxing. If the reloc is not
3835 against the start of this section, then it's against an
3836 external symbol and we must deal with it ourselves. */
3837 if (input_section
->output_section
->vma
+ input_section
->output_offset
3840 int disp
= (relocation
3841 - input_section
->output_section
->vma
3842 - input_section
->output_offset
3848 case R_SH_DIR8WPZ
: mask
= 1; break;
3849 case R_SH_DIR8WPL
: mask
= 3; break;
3850 default: mask
= 0; break;
3855 /* xgettext:c-format */
3856 (_("%pB: %#" PRIx64
": fatal: "
3857 "unaligned branch target for relax-support relocation"),
3858 input_section
->owner
,
3859 (uint64_t) rel
->r_offset
);
3860 bfd_set_error (bfd_error_bad_value
);
3864 goto final_link_relocate
;
3870 bfd_set_error (bfd_error_bad_value
);
3878 goto final_link_relocate
;
3885 /* xgettext:c-format */
3886 (_("%pB: %#" PRIx64
": fatal: "
3887 "unaligned %s relocation %#" PRIx64
),
3888 input_section
->owner
, (uint64_t) rel
->r_offset
,
3889 howto
->name
, (uint64_t) relocation
);
3890 bfd_set_error (bfd_error_bad_value
);
3893 goto final_link_relocate
;
3901 /* xgettext:c-format */
3902 (_("%pB: %#" PRIx64
": fatal: "
3903 "unaligned %s relocation %#" PRIx64
""),
3904 input_section
->owner
,
3905 (uint64_t) rel
->r_offset
, howto
->name
,
3906 (uint64_t) relocation
);
3907 bfd_set_error (bfd_error_bad_value
);
3910 goto final_link_relocate
;
3913 if ((signed int)relocation
< -32
3914 || (signed int)relocation
> 32)
3917 /* xgettext:c-format */
3918 (_("%pB: %#" PRIx64
": fatal: R_SH_PSHA relocation %" PRId64
3919 " not in range -32..32"),
3920 input_section
->owner
,
3921 (uint64_t) rel
->r_offset
,
3922 (int64_t) relocation
);
3923 bfd_set_error (bfd_error_bad_value
);
3926 goto final_link_relocate
;
3929 if ((signed int)relocation
< -16
3930 || (signed int)relocation
> 16)
3933 /* xgettext:c-format */
3934 (_("%pB: %#" PRIx64
": fatal: R_SH_PSHL relocation %" PRId64
3935 " not in range -32..32"),
3936 input_section
->owner
,
3937 (uint64_t) rel
->r_offset
,
3938 (int64_t) relocation
);
3939 bfd_set_error (bfd_error_bad_value
);
3942 goto final_link_relocate
;
3946 if (bfd_link_pic (info
)
3948 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3949 && !resolved_to_zero
)
3950 || h
->root
.type
!= bfd_link_hash_undefweak
)
3951 && r_symndx
!= STN_UNDEF
3952 && (input_section
->flags
& SEC_ALLOC
) != 0
3954 && (r_type
== R_SH_DIR32
3955 || !SYMBOL_CALLS_LOCAL (info
, h
)))
3957 Elf_Internal_Rela outrel
;
3959 bfd_boolean skip
, relocate
;
3961 /* When generating a shared object, these relocations
3962 are copied into the output file to be resolved at run
3967 sreloc
= _bfd_elf_get_dynamic_reloc_section
3968 (input_bfd
, input_section
, /*rela?*/ TRUE
);
3977 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3979 if (outrel
.r_offset
== (bfd_vma
) -1)
3981 else if (outrel
.r_offset
== (bfd_vma
) -2)
3982 skip
= TRUE
, relocate
= TRUE
;
3983 outrel
.r_offset
+= (input_section
->output_section
->vma
3984 + input_section
->output_offset
);
3987 memset (&outrel
, 0, sizeof outrel
);
3988 else if (r_type
== R_SH_REL32
)
3990 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
3991 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
3993 = (howto
->partial_inplace
3994 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
3999 || ((info
->symbolic
|| h
->dynindx
== -1)
4000 && h
->def_regular
)))
4004 BFD_ASSERT (sec
!= NULL
);
4005 BFD_ASSERT (sec
->output_section
!= NULL
);
4006 dynindx
= elf_section_data (sec
->output_section
)->dynindx
;
4007 outrel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
4008 outrel
.r_addend
= relocation
;
4010 += (howto
->partial_inplace
4011 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4013 outrel
.r_addend
-= sec
->output_section
->vma
;
4017 /* h->dynindx may be -1 if this symbol was marked to
4020 || ((info
->symbolic
|| h
->dynindx
== -1)
4023 relocate
= howto
->partial_inplace
;
4024 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4028 BFD_ASSERT (h
->dynindx
!= -1);
4029 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
4031 outrel
.r_addend
= relocation
;
4033 += (howto
->partial_inplace
4034 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4038 loc
= sreloc
->contents
;
4039 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4040 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4042 check_segment
[0] = check_segment
[1] = -1;
4044 /* If this reloc is against an external symbol, we do
4045 not want to fiddle with the addend. Otherwise, we
4046 need to include the symbol value so that it becomes
4047 an addend for the dynamic reloc. */
4051 else if (fdpic_p
&& !bfd_link_pic (info
)
4052 && r_type
== R_SH_DIR32
4053 && (input_section
->flags
& SEC_ALLOC
) != 0)
4059 if (sh_elf_osec_readonly_p (output_bfd
,
4060 input_section
->output_section
))
4063 /* xgettext:c-format */
4064 (_("%pB(%pA+%#" PRIx64
"): "
4065 "cannot emit fixup to `%s' in read-only section"),
4068 (uint64_t) rel
->r_offset
,
4073 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4074 input_section
, rel
->r_offset
);
4075 if (offset
!= (bfd_vma
)-1)
4076 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4077 input_section
->output_section
->vma
4078 + input_section
->output_offset
4081 check_segment
[0] = check_segment
[1] = -1;
4083 /* We don't want warnings for non-NULL tests on undefined weak
4085 else if (r_type
== R_SH_REL32
4087 && h
->root
.type
== bfd_link_hash_undefweak
)
4088 check_segment
[0] = check_segment
[1] = -1;
4089 goto final_link_relocate
;
4092 /* Relocation is to the entry for this symbol in the
4093 procedure linkage table. */
4097 || ! bfd_link_pic (info
)
4100 || h
->plt
.offset
== (bfd_vma
) -1
4101 || h
->got
.offset
!= (bfd_vma
) -1)
4104 /* Relocation is to the entry for this symbol in the global
4105 offset table extension for the procedure linkage table. */
4108 BFD_ASSERT (sgotplt
!= NULL
);
4109 relocation
= (sgotplt
->output_offset
4110 + (get_plt_index (htab
->plt_info
, h
->plt
.offset
)
4114 relocation
-= GOT_BIAS
;
4117 goto final_link_relocate
;
4122 /* Relocation is to the entry for this symbol in the global
4126 BFD_ASSERT (sgot
!= NULL
);
4127 check_segment
[0] = check_segment
[1] = -1;
4133 off
= h
->got
.offset
;
4134 BFD_ASSERT (off
!= (bfd_vma
) -1);
4136 dyn
= htab
->root
.dynamic_sections_created
;
4137 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
4138 bfd_link_pic (info
),
4140 || (bfd_link_pic (info
)
4141 && SYMBOL_REFERENCES_LOCAL (info
, h
))
4142 || ((ELF_ST_VISIBILITY (h
->other
)
4143 || resolved_to_zero
)
4144 && h
->root
.type
== bfd_link_hash_undefweak
))
4146 /* This is actually a static link, or it is a
4147 -Bsymbolic link and the symbol is defined
4148 locally, or the symbol was forced to be local
4149 because of a version file. We must initialize
4150 this entry in the global offset table. Since the
4151 offset must always be a multiple of 4, we use the
4152 least significant bit to record whether we have
4153 initialized it already.
4155 When doing a dynamic link, we create a .rela.got
4156 relocation entry to initialize the value. This
4157 is done in the finish_dynamic_symbol routine. */
4162 bfd_put_32 (output_bfd
, relocation
,
4163 sgot
->contents
+ off
);
4166 /* If we initialize the GOT entry here with a valid
4167 symbol address, also add a fixup. */
4168 if (fdpic_p
&& !bfd_link_pic (info
)
4169 && sh_elf_hash_entry (h
)->got_type
== GOT_NORMAL
4170 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4171 || h
->root
.type
!= bfd_link_hash_undefweak
))
4172 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4173 sgot
->output_section
->vma
4174 + sgot
->output_offset
4179 relocation
= sh_elf_got_offset (htab
) + off
;
4183 BFD_ASSERT (local_got_offsets
!= NULL
4184 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
4186 off
= local_got_offsets
[r_symndx
];
4188 /* The offset must always be a multiple of 4. We use
4189 the least significant bit to record whether we have
4190 already generated the necessary reloc. */
4195 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
4197 if (bfd_link_pic (info
))
4199 Elf_Internal_Rela outrel
;
4202 outrel
.r_offset
= (sgot
->output_section
->vma
4203 + sgot
->output_offset
4208 = elf_section_data (sec
->output_section
)->dynindx
;
4209 outrel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
4210 outrel
.r_addend
= relocation
;
4211 outrel
.r_addend
-= sec
->output_section
->vma
;
4215 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4216 outrel
.r_addend
= relocation
;
4218 loc
= srelgot
->contents
;
4219 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4220 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4223 && (sh_elf_local_got_type (input_bfd
) [r_symndx
]
4225 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4226 sgot
->output_section
->vma
4227 + sgot
->output_offset
4230 local_got_offsets
[r_symndx
] |= 1;
4233 relocation
= sh_elf_got_offset (htab
) + off
;
4237 relocation
-= GOT_BIAS
;
4240 if (r_type
== R_SH_GOT20
)
4242 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4243 input_bfd
, input_section
, contents
,
4248 goto final_link_relocate
;
4252 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4253 we place at the start of the .got.plt section. This is the same
4254 as the start of the output .got section, unless there are function
4255 descriptors in front of it. */
4257 BFD_ASSERT (sgotplt
!= NULL
);
4258 check_segment
[0] = got_segment
;
4259 relocation
-= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4260 + htab
->root
.hgot
->root
.u
.def
.value
;
4263 relocation
-= GOT_BIAS
;
4266 addend
= rel
->r_addend
;
4268 if (r_type
== R_SH_GOTOFF20
)
4270 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4271 input_bfd
, input_section
, contents
,
4276 goto final_link_relocate
;
4279 /* Use global offset table as symbol value. */
4281 BFD_ASSERT (sgotplt
!= NULL
);
4282 relocation
= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
;
4285 relocation
+= GOT_BIAS
;
4288 addend
= rel
->r_addend
;
4290 goto final_link_relocate
;
4293 /* Relocation is to the entry for this symbol in the
4294 procedure linkage table. */
4296 /* Resolve a PLT reloc against a local symbol directly,
4297 without using the procedure linkage table. */
4299 goto final_link_relocate
;
4301 /* We don't want to warn on calls to undefined weak symbols,
4302 as calls to them must be protected by non-NULL tests
4303 anyway, and unprotected calls would invoke undefined
4305 if (h
->root
.type
== bfd_link_hash_undefweak
)
4306 check_segment
[0] = check_segment
[1] = -1;
4308 if (h
->forced_local
)
4309 goto final_link_relocate
;
4311 if (h
->plt
.offset
== (bfd_vma
) -1)
4313 /* We didn't make a PLT entry for this symbol. This
4314 happens when statically linking PIC code, or when
4315 using -Bsymbolic. */
4316 goto final_link_relocate
;
4319 BFD_ASSERT (splt
!= NULL
);
4320 check_segment
[1] = plt_segment
;
4321 relocation
= (splt
->output_section
->vma
4322 + splt
->output_offset
4325 addend
= rel
->r_addend
;
4327 goto final_link_relocate
;
4329 /* Relocation is to the canonical function descriptor for this
4330 symbol, possibly via the GOT. Initialize the GOT
4331 entry and function descriptor if necessary. */
4332 case R_SH_GOTFUNCDESC
:
4333 case R_SH_GOTFUNCDESC20
:
4337 asection
*reloc_section
;
4338 bfd_vma reloc_offset
;
4339 int reloc_type
= R_SH_FUNCDESC
;
4343 check_segment
[0] = check_segment
[1] = -1;
4345 /* FIXME: See what FRV does for global symbols in the
4346 executable, with --export-dynamic. Do they need ld.so
4347 to allocate official descriptors? See what this code
4353 if (r_type
== R_SH_FUNCDESC
)
4355 reloc_section
= input_section
;
4356 reloc_offset
= rel
->r_offset
;
4360 reloc_section
= sgot
;
4363 reloc_offset
= h
->got
.offset
;
4366 BFD_ASSERT (local_got_offsets
!= NULL
);
4367 reloc_offset
= local_got_offsets
[r_symndx
];
4369 BFD_ASSERT (reloc_offset
!= MINUS_ONE
);
4371 if (reloc_offset
& 1)
4374 goto funcdesc_done_got
;
4378 if (h
&& h
->root
.type
== bfd_link_hash_undefweak
4379 && (SYMBOL_CALLS_LOCAL (info
, h
)
4380 || !htab
->root
.dynamic_sections_created
))
4381 /* Undefined weak symbol which will not be dynamically
4382 resolved later; leave it at zero. */
4383 goto funcdesc_leave_zero
;
4384 else if (SYMBOL_CALLS_LOCAL (info
, h
)
4385 && ! SYMBOL_FUNCDESC_LOCAL (info
, h
))
4387 /* If the symbol needs a non-local function descriptor
4388 but binds locally (i.e., its visibility is
4389 protected), emit a dynamic relocation decayed to
4390 section+offset. This is an optimization; the dynamic
4391 linker would resolve our function descriptor request
4392 to our copy of the function anyway. */
4393 dynindx
= elf_section_data (h
->root
.u
.def
.section
4394 ->output_section
)->dynindx
;
4395 relocation
+= h
->root
.u
.def
.section
->output_offset
4396 + h
->root
.u
.def
.value
;
4398 else if (! SYMBOL_FUNCDESC_LOCAL (info
, h
))
4400 /* If the symbol is dynamic and there will be dynamic
4401 symbol resolution because we are or are linked with a
4402 shared library, emit a FUNCDESC relocation such that
4403 the dynamic linker will allocate the function
4405 BFD_ASSERT (h
->dynindx
!= -1);
4406 dynindx
= h
->dynindx
;
4412 /* Otherwise, we know we have a private function
4413 descriptor, so reference it directly. */
4414 reloc_type
= R_SH_DIR32
;
4415 dynindx
= elf_section_data (htab
->sfuncdesc
4416 ->output_section
)->dynindx
;
4420 offset
= sh_elf_hash_entry (h
)->funcdesc
.offset
;
4421 BFD_ASSERT (offset
!= MINUS_ONE
);
4422 if ((offset
& 1) == 0)
4424 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, h
,
4427 sh_elf_hash_entry (h
)->funcdesc
.offset
|= 1;
4432 union gotref
*local_funcdesc
;
4434 local_funcdesc
= sh_elf_local_funcdesc (input_bfd
);
4435 offset
= local_funcdesc
[r_symndx
].offset
;
4436 BFD_ASSERT (offset
!= MINUS_ONE
);
4437 if ((offset
& 1) == 0)
4439 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, NULL
,
4443 local_funcdesc
[r_symndx
].offset
|= 1;
4447 relocation
= htab
->sfuncdesc
->output_offset
+ (offset
& ~1);
4450 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
4454 if (sh_elf_osec_readonly_p (output_bfd
,
4455 reloc_section
->output_section
))
4458 /* xgettext:c-format */
4459 (_("%pB(%pA+%#" PRIx64
"): "
4460 "cannot emit fixup to `%s' in read-only section"),
4463 (uint64_t) rel
->r_offset
,
4468 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4469 reloc_section
, reloc_offset
);
4471 if (offset
!= (bfd_vma
)-1)
4472 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4474 + reloc_section
->output_section
->vma
4475 + reloc_section
->output_offset
);
4477 else if ((reloc_section
->output_section
->flags
4478 & (SEC_ALLOC
| SEC_LOAD
)) == (SEC_ALLOC
| SEC_LOAD
))
4482 if (sh_elf_osec_readonly_p (output_bfd
,
4483 reloc_section
->output_section
))
4485 info
->callbacks
->warning
4487 _("cannot emit dynamic relocations in read-only section"),
4488 symname
, input_bfd
, reloc_section
, reloc_offset
);
4492 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4493 reloc_section
, reloc_offset
);
4495 if (offset
!= (bfd_vma
)-1)
4496 sh_elf_add_dyn_reloc (output_bfd
, srelgot
,
4498 + reloc_section
->output_section
->vma
4499 + reloc_section
->output_offset
,
4500 reloc_type
, dynindx
, relocation
);
4502 if (r_type
== R_SH_FUNCDESC
)
4510 goto funcdesc_leave_zero
;
4514 if (SYMBOL_FUNCDESC_LOCAL (info
, h
))
4515 relocation
+= htab
->sfuncdesc
->output_section
->vma
;
4516 funcdesc_leave_zero
:
4517 if (r_type
!= R_SH_FUNCDESC
)
4519 bfd_put_32 (output_bfd
, relocation
,
4520 reloc_section
->contents
+ reloc_offset
);
4524 local_got_offsets
[r_symndx
] |= 1;
4528 relocation
= sh_elf_got_offset (htab
) + reloc_offset
;
4530 relocation
-= GOT_BIAS
;
4533 if (r_type
== R_SH_GOTFUNCDESC20
)
4535 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4536 input_bfd
, input_section
, contents
,
4541 goto final_link_relocate
;
4545 case R_SH_GOTOFFFUNCDESC
:
4546 case R_SH_GOTOFFFUNCDESC20
:
4547 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4548 executable and --export-dynamic. If such symbols get
4549 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
4553 check_segment
[0] = check_segment
[1] = -1;
4555 addend
= rel
->r_addend
;
4557 if (h
&& (h
->root
.type
== bfd_link_hash_undefweak
4558 || !SYMBOL_FUNCDESC_LOCAL (info
, h
)))
4561 /* xgettext:c-format */
4562 (_("%pB(%pA+%#" PRIx64
"): "
4563 "%s relocation against external symbol \"%s\""),
4564 input_bfd
, input_section
, (uint64_t) rel
->r_offset
,
4565 howto
->name
, h
->root
.root
.string
);
4572 /* Otherwise, we know we have a private function
4573 descriptor, so reference it directly. */
4576 offset
= sh_elf_hash_entry (h
)->funcdesc
.offset
;
4577 BFD_ASSERT (offset
!= MINUS_ONE
);
4578 if ((offset
& 1) == 0)
4580 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, h
,
4583 sh_elf_hash_entry (h
)->funcdesc
.offset
|= 1;
4588 union gotref
*local_funcdesc
;
4590 local_funcdesc
= sh_elf_local_funcdesc (input_bfd
);
4591 offset
= local_funcdesc
[r_symndx
].offset
;
4592 BFD_ASSERT (offset
!= MINUS_ONE
);
4593 if ((offset
& 1) == 0)
4595 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, NULL
,
4599 local_funcdesc
[r_symndx
].offset
|= 1;
4603 relocation
= htab
->sfuncdesc
->output_offset
+ (offset
& ~1);
4606 relocation
-= (htab
->root
.hgot
->root
.u
.def
.value
4607 + sgotplt
->output_offset
);
4609 relocation
-= GOT_BIAS
;
4612 if (r_type
== R_SH_GOTOFFFUNCDESC20
)
4614 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4615 input_bfd
, input_section
, contents
,
4620 goto final_link_relocate
;
4622 case R_SH_LOOP_START
:
4624 static bfd_vma start
, end
;
4626 start
= (relocation
+ rel
->r_addend
4627 - (sec
->output_section
->vma
+ sec
->output_offset
));
4628 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
4629 rel
->r_offset
, sec
, start
, end
);
4633 end
= (relocation
+ rel
->r_addend
4634 - (sec
->output_section
->vma
+ sec
->output_offset
));
4635 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
4636 rel
->r_offset
, sec
, start
, end
);
4640 case R_SH_TLS_GD_32
:
4641 case R_SH_TLS_IE_32
:
4643 check_segment
[0] = check_segment
[1] = -1;
4644 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
4645 got_type
= GOT_UNKNOWN
;
4646 if (h
== NULL
&& local_got_offsets
)
4647 got_type
= sh_elf_local_got_type (input_bfd
) [r_symndx
];
4650 got_type
= sh_elf_hash_entry (h
)->got_type
;
4651 if (! bfd_link_pic (info
)
4652 && (h
->dynindx
== -1
4654 r_type
= R_SH_TLS_LE_32
;
4657 if (r_type
== R_SH_TLS_GD_32
&& got_type
== GOT_TLS_IE
)
4658 r_type
= R_SH_TLS_IE_32
;
4660 if (r_type
== R_SH_TLS_LE_32
)
4663 unsigned short insn
;
4665 if (ELF32_R_TYPE (rel
->r_info
) == R_SH_TLS_GD_32
)
4667 /* GD->LE transition:
4668 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4669 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4670 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4672 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
4674 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
4676 offset
= rel
->r_offset
;
4680 /* xgettext:c-format */
4681 (_("%pB(%pA): offset in relocation for GD->LE translation is too small: %#" PRIx64
),
4682 input_bfd
, input_section
, (uint64_t) offset
);
4686 /* Size of GD instructions is 16 or 18. */
4688 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4689 if ((insn
& 0xff00) == 0xc700)
4691 BFD_ASSERT (offset
>= 2);
4693 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4696 if ((insn
& 0xff00) != 0xd400)
4698 /* xgettext:c-format */ /* The backslash is to prevent bogus trigraph detection. */
4699 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd4?\?)"),
4700 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4702 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4704 if ((insn
& 0xff00) != 0xc700)
4706 /* xgettext:c-format */
4707 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xc7?\?)"),
4708 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4710 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4711 if ((insn
& 0xff00) != 0xd100)
4713 /* xgettext:c-format */
4714 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd1?\?)"),
4715 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4717 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4720 /* xgettext:c-format */
4721 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x310c)"),
4722 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4724 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4727 /* xgettext:c-format */
4728 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x410b)"),
4729 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4731 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4734 /* xgettext:c-format */
4735 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x34cc)"),
4736 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4738 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 2);
4739 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 4);
4740 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
4741 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4742 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4748 /* IE->LE transition:
4765 offset
= rel
->r_offset
;
4769 /* xgettext:c-format */
4770 (_("%pB(%pA): offset in relocation for IE->LE translation is too small: %#" PRIx64
),
4771 input_bfd
, input_section
, (uint64_t) offset
);
4775 /* Size of IE instructions is 10 or 12. */
4777 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4778 if ((insn
& 0xf0ff) == 0x0012)
4780 BFD_ASSERT (offset
>= 2);
4782 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4785 if ((insn
& 0xff00) != 0xd000)
4787 /* xgettext:c-format */
4788 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd0??: mov.l)"),
4789 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4791 target
= insn
& 0x00ff;
4793 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4794 if ((insn
& 0xf0ff) != 0x0012)
4796 /* xgettext:c-format */
4797 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x0?12: stc)"),
4798 input_bfd
, input_section
, (uint64_t) (offset
+ 2), (int) insn
);
4800 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4801 if ((insn
& 0xf0ff) != 0x00ce)
4803 /* xgettext:c-format */
4804 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x0?ce: mov.l)"),
4805 input_bfd
, input_section
, (uint64_t) (offset
+ 4), (int) insn
);
4807 insn
= 0xd000 | (insn
& 0x0f00) | target
;
4808 bfd_put_16 (output_bfd
, insn
, contents
+ offset
+ 0);
4809 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4812 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4813 contents
+ rel
->r_offset
);
4817 if (sgot
== NULL
|| sgotplt
== NULL
)
4821 off
= h
->got
.offset
;
4824 if (local_got_offsets
== NULL
)
4827 off
= local_got_offsets
[r_symndx
];
4830 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4831 if (r_type
== R_SH_TLS_IE_32
4832 && ! htab
->root
.dynamic_sections_created
)
4835 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4836 sgot
->contents
+ off
);
4837 bfd_put_32 (output_bfd
, sh_elf_got_offset (htab
) + off
,
4838 contents
+ rel
->r_offset
);
4846 Elf_Internal_Rela outrel
;
4850 outrel
.r_offset
= (sgot
->output_section
->vma
4851 + sgot
->output_offset
+ off
);
4853 if (h
== NULL
|| h
->dynindx
== -1)
4858 dr_type
= (r_type
== R_SH_TLS_GD_32
? R_SH_TLS_DTPMOD32
:
4860 if (dr_type
== R_SH_TLS_TPOFF32
&& indx
== 0)
4861 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4863 outrel
.r_addend
= 0;
4864 outrel
.r_info
= ELF32_R_INFO (indx
, dr_type
);
4865 loc
= srelgot
->contents
;
4866 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4867 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4869 if (r_type
== R_SH_TLS_GD_32
)
4873 bfd_put_32 (output_bfd
,
4874 relocation
- dtpoff_base (info
),
4875 sgot
->contents
+ off
+ 4);
4879 outrel
.r_info
= ELF32_R_INFO (indx
,
4881 outrel
.r_offset
+= 4;
4882 outrel
.r_addend
= 0;
4883 srelgot
->reloc_count
++;
4884 loc
+= sizeof (Elf32_External_Rela
);
4885 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4892 local_got_offsets
[r_symndx
] |= 1;
4895 if (off
>= (bfd_vma
) -2)
4898 if (r_type
== (int) ELF32_R_TYPE (rel
->r_info
))
4899 relocation
= sh_elf_got_offset (htab
) + off
;
4903 unsigned short insn
;
4905 /* GD->IE transition:
4906 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4907 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4908 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4910 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4911 nop; nop; bra 3f; nop; .align 2;
4912 1: .long x@TPOFF; 2:...; 3:. */
4914 offset
= rel
->r_offset
;
4918 /* xgettext:c-format */
4919 (_("%pB(%pA): offset in relocation for GD->IE translation is too small: %#" PRIx64
),
4920 input_bfd
, input_section
, (uint64_t) offset
);
4924 /* Size of GD instructions is 16 or 18. */
4926 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4927 if ((insn
& 0xff00) == 0xc700)
4929 BFD_ASSERT (offset
>= 2);
4931 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4934 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4936 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4937 bfd_put_16 (output_bfd
, insn
& 0xf0ff, contents
+ offset
);
4939 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4940 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4941 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4942 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4943 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4944 BFD_ASSERT (insn
== 0x310c);
4945 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4946 BFD_ASSERT (insn
== 0x410b);
4947 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4948 BFD_ASSERT (insn
== 0x34cc);
4950 bfd_put_16 (output_bfd
, 0x0412, contents
+ offset
+ 2);
4951 bfd_put_16 (output_bfd
, 0x00ce, contents
+ offset
+ 4);
4952 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 6);
4953 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4954 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4956 bfd_put_32 (output_bfd
, sh_elf_got_offset (htab
) + off
,
4957 contents
+ rel
->r_offset
);
4962 addend
= rel
->r_addend
;
4964 goto final_link_relocate
;
4966 case R_SH_TLS_LD_32
:
4968 check_segment
[0] = check_segment
[1] = -1;
4969 if (! bfd_link_pic (info
))
4972 unsigned short insn
;
4974 /* LD->LE transition:
4975 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4976 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4977 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4979 stc gbr,r0; nop; nop; nop;
4980 nop; nop; bra 3f; ...; 3:. */
4982 offset
= rel
->r_offset
;
4986 /* xgettext:c-format */
4987 (_("%pB(%pA): offset in relocation for LD->LE translation is too small: %#" PRIx64
),
4988 input_bfd
, input_section
, (uint64_t) offset
);
4992 /* Size of LD instructions is 16 or 18. */
4994 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4995 if ((insn
& 0xff00) == 0xc700)
4997 BFD_ASSERT (offset
>= 2);
4999 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5002 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
5003 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
5004 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
5005 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
5006 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
5007 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
5008 BFD_ASSERT (insn
== 0x310c);
5009 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
5010 BFD_ASSERT (insn
== 0x410b);
5011 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
5012 BFD_ASSERT (insn
== 0x34cc);
5014 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 0);
5015 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 2);
5016 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
5017 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
5018 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
5019 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
5024 if (sgot
== NULL
|| sgotplt
== NULL
)
5027 off
= htab
->tls_ldm_got
.offset
;
5032 Elf_Internal_Rela outrel
;
5035 outrel
.r_offset
= (sgot
->output_section
->vma
5036 + sgot
->output_offset
+ off
);
5037 outrel
.r_addend
= 0;
5038 outrel
.r_info
= ELF32_R_INFO (0, R_SH_TLS_DTPMOD32
);
5039 loc
= srelgot
->contents
;
5040 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5041 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5042 htab
->tls_ldm_got
.offset
|= 1;
5045 relocation
= sh_elf_got_offset (htab
) + off
;
5046 addend
= rel
->r_addend
;
5048 goto final_link_relocate
;
5050 case R_SH_TLS_LDO_32
:
5051 check_segment
[0] = check_segment
[1] = -1;
5052 if (! bfd_link_pic (info
))
5053 relocation
= tpoff (info
, relocation
);
5055 relocation
-= dtpoff_base (info
);
5057 addend
= rel
->r_addend
;
5058 goto final_link_relocate
;
5060 case R_SH_TLS_LE_32
:
5063 Elf_Internal_Rela outrel
;
5066 check_segment
[0] = check_segment
[1] = -1;
5068 if (!bfd_link_dll (info
))
5070 relocation
= tpoff (info
, relocation
);
5071 addend
= rel
->r_addend
;
5072 goto final_link_relocate
;
5077 sreloc
= _bfd_elf_get_dynamic_reloc_section
5078 (input_bfd
, input_section
, /*rela?*/ TRUE
);
5083 if (h
== NULL
|| h
->dynindx
== -1)
5088 outrel
.r_offset
= (input_section
->output_section
->vma
5089 + input_section
->output_offset
5091 outrel
.r_info
= ELF32_R_INFO (indx
, R_SH_TLS_TPOFF32
);
5093 outrel
.r_addend
= relocation
- dtpoff_base (info
);
5095 outrel
.r_addend
= 0;
5097 loc
= sreloc
->contents
;
5098 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5099 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5105 if (fdpic_p
&& check_segment
[0] != (unsigned) -1
5106 && check_segment
[0] != check_segment
[1])
5108 /* We don't want duplicate errors for undefined symbols. */
5109 if (!h
|| h
->root
.type
!= bfd_link_hash_undefined
)
5111 if (bfd_link_pic (info
))
5113 info
->callbacks
->einfo
5114 /* xgettext:c-format */
5115 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5116 input_bfd
, input_section
, rel
->r_offset
, symname
);
5120 info
->callbacks
->einfo
5121 /* xgettext:c-format */
5122 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5123 input_bfd
, input_section
, rel
->r_offset
, symname
);
5126 elf_elfheader (output_bfd
)->e_flags
|= EF_SH_PIC
;
5129 if (r
!= bfd_reloc_ok
)
5134 case bfd_reloc_outofrange
:
5136 case bfd_reloc_overflow
:
5144 name
= (bfd_elf_string_from_elf_section
5145 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
5149 name
= bfd_section_name (sec
);
5151 (*info
->callbacks
->reloc_overflow
)
5152 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
5153 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
5163 /* This is a version of bfd_generic_get_relocated_section_contents
5164 which uses sh_elf_relocate_section. */
5167 sh_elf_get_relocated_section_contents (bfd
*output_bfd
,
5168 struct bfd_link_info
*link_info
,
5169 struct bfd_link_order
*link_order
,
5171 bfd_boolean relocatable
,
5174 Elf_Internal_Shdr
*symtab_hdr
;
5175 asection
*input_section
= link_order
->u
.indirect
.section
;
5176 bfd
*input_bfd
= input_section
->owner
;
5177 asection
**sections
= NULL
;
5178 Elf_Internal_Rela
*internal_relocs
= NULL
;
5179 Elf_Internal_Sym
*isymbuf
= NULL
;
5181 /* We only need to handle the case of relaxing, or of having a
5182 particular set of section contents, specially. */
5184 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
5185 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
5190 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
5192 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
5193 (size_t) input_section
->size
);
5195 if ((input_section
->flags
& SEC_RELOC
) != 0
5196 && input_section
->reloc_count
> 0)
5199 Elf_Internal_Sym
*isym
, *isymend
;
5202 internal_relocs
= (_bfd_elf_link_read_relocs
5203 (input_bfd
, input_section
, NULL
,
5204 (Elf_Internal_Rela
*) NULL
, FALSE
));
5205 if (internal_relocs
== NULL
)
5208 if (symtab_hdr
->sh_info
!= 0)
5210 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
5211 if (isymbuf
== NULL
)
5212 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
5213 symtab_hdr
->sh_info
, 0,
5215 if (isymbuf
== NULL
)
5219 amt
= symtab_hdr
->sh_info
;
5220 amt
*= sizeof (asection
*);
5221 sections
= (asection
**) bfd_malloc (amt
);
5222 if (sections
== NULL
&& amt
!= 0)
5225 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
5226 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
5230 if (isym
->st_shndx
== SHN_UNDEF
)
5231 isec
= bfd_und_section_ptr
;
5232 else if (isym
->st_shndx
== SHN_ABS
)
5233 isec
= bfd_abs_section_ptr
;
5234 else if (isym
->st_shndx
== SHN_COMMON
)
5235 isec
= bfd_com_section_ptr
;
5237 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
5242 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
5243 input_section
, data
, internal_relocs
,
5248 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
5250 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
5251 free (internal_relocs
);
5258 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
5260 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
5261 free (internal_relocs
);
5265 /* Return the base VMA address which should be subtracted from real addresses
5266 when resolving @dtpoff relocation.
5267 This is PT_TLS segment p_vaddr. */
5270 dtpoff_base (struct bfd_link_info
*info
)
5272 /* If tls_sec is NULL, we should have signalled an error already. */
5273 if (elf_hash_table (info
)->tls_sec
== NULL
)
5275 return elf_hash_table (info
)->tls_sec
->vma
;
5278 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5281 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
5283 /* If tls_sec is NULL, we should have signalled an error already. */
5284 if (elf_hash_table (info
)->tls_sec
== NULL
)
5286 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5287 structure which has 2 pointer fields. */
5288 return (address
- elf_hash_table (info
)->tls_sec
->vma
5289 + align_power ((bfd_vma
) 8,
5290 elf_hash_table (info
)->tls_sec
->alignment_power
));
5294 sh_elf_gc_mark_hook (asection
*sec
,
5295 struct bfd_link_info
*info
,
5296 Elf_Internal_Rela
*rel
,
5297 struct elf_link_hash_entry
*h
,
5298 Elf_Internal_Sym
*sym
)
5301 switch (ELF32_R_TYPE (rel
->r_info
))
5303 case R_SH_GNU_VTINHERIT
:
5304 case R_SH_GNU_VTENTRY
:
5308 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
5311 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5314 sh_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
5315 struct elf_link_hash_entry
*dir
,
5316 struct elf_link_hash_entry
*ind
)
5318 struct elf_sh_link_hash_entry
*edir
, *eind
;
5320 edir
= (struct elf_sh_link_hash_entry
*) dir
;
5321 eind
= (struct elf_sh_link_hash_entry
*) ind
;
5323 if (ind
->dyn_relocs
!= NULL
)
5325 if (dir
->dyn_relocs
!= NULL
)
5327 struct elf_dyn_relocs
**pp
;
5328 struct elf_dyn_relocs
*p
;
5330 /* Add reloc counts against the indirect sym to the direct sym
5331 list. Merge any entries against the same section. */
5332 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
5334 struct elf_dyn_relocs
*q
;
5336 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
5337 if (q
->sec
== p
->sec
)
5339 q
->pc_count
+= p
->pc_count
;
5340 q
->count
+= p
->count
;
5347 *pp
= dir
->dyn_relocs
;
5350 dir
->dyn_relocs
= ind
->dyn_relocs
;
5351 ind
->dyn_relocs
= NULL
;
5353 edir
->gotplt_refcount
= eind
->gotplt_refcount
;
5354 eind
->gotplt_refcount
= 0;
5355 edir
->funcdesc
.refcount
+= eind
->funcdesc
.refcount
;
5356 eind
->funcdesc
.refcount
= 0;
5357 edir
->abs_funcdesc_refcount
+= eind
->abs_funcdesc_refcount
;
5358 eind
->abs_funcdesc_refcount
= 0;
5360 if (ind
->root
.type
== bfd_link_hash_indirect
5361 && dir
->got
.refcount
<= 0)
5363 edir
->got_type
= eind
->got_type
;
5364 eind
->got_type
= GOT_UNKNOWN
;
5367 if (ind
->root
.type
!= bfd_link_hash_indirect
5368 && dir
->dynamic_adjusted
)
5370 /* If called to transfer flags for a weakdef during processing
5371 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5372 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5373 if (dir
->versioned
!= versioned_hidden
)
5374 dir
->ref_dynamic
|= ind
->ref_dynamic
;
5375 dir
->ref_regular
|= ind
->ref_regular
;
5376 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
5377 dir
->needs_plt
|= ind
->needs_plt
;
5380 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
5384 sh_elf_optimized_tls_reloc (struct bfd_link_info
*info
, int r_type
,
5387 if (bfd_link_pic (info
))
5392 case R_SH_TLS_GD_32
:
5393 case R_SH_TLS_IE_32
:
5395 return R_SH_TLS_LE_32
;
5396 return R_SH_TLS_IE_32
;
5397 case R_SH_TLS_LD_32
:
5398 return R_SH_TLS_LE_32
;
5404 /* Look through the relocs for a section during the first phase.
5405 Since we don't do .gots or .plts, we just need to consider the
5406 virtual table relocs for gc. */
5409 sh_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
5410 const Elf_Internal_Rela
*relocs
)
5412 Elf_Internal_Shdr
*symtab_hdr
;
5413 struct elf_link_hash_entry
**sym_hashes
;
5414 struct elf_sh_link_hash_table
*htab
;
5415 const Elf_Internal_Rela
*rel
;
5416 const Elf_Internal_Rela
*rel_end
;
5418 unsigned int r_type
;
5419 enum got_type got_type
, old_got_type
;
5423 if (bfd_link_relocatable (info
))
5426 /* Don't do anything special with non-loaded, non-alloced sections.
5427 In particular, any relocs in such sections should not affect GOT
5428 and PLT reference counting (ie. we don't allow them to create GOT
5429 or PLT entries), there's no possibility or desire to optimize TLS
5430 relocs, and there's not much point in propagating relocs to shared
5431 libs that the dynamic linker won't relocate. */
5432 if ((sec
->flags
& SEC_ALLOC
) == 0)
5435 BFD_ASSERT (is_sh_elf (abfd
));
5437 symtab_hdr
= &elf_symtab_hdr (abfd
);
5438 sym_hashes
= elf_sym_hashes (abfd
);
5440 htab
= sh_elf_hash_table (info
);
5444 rel_end
= relocs
+ sec
->reloc_count
;
5445 for (rel
= relocs
; rel
< rel_end
; rel
++)
5447 struct elf_link_hash_entry
*h
;
5448 unsigned long r_symndx
;
5450 r_symndx
= ELF32_R_SYM (rel
->r_info
);
5451 r_type
= ELF32_R_TYPE (rel
->r_info
);
5453 if (r_symndx
< symtab_hdr
->sh_info
)
5457 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
5458 while (h
->root
.type
== bfd_link_hash_indirect
5459 || h
->root
.type
== bfd_link_hash_warning
)
5460 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5463 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
5464 if (! bfd_link_pic (info
)
5465 && r_type
== R_SH_TLS_IE_32
5467 && h
->root
.type
!= bfd_link_hash_undefined
5468 && h
->root
.type
!= bfd_link_hash_undefweak
5469 && (h
->dynindx
== -1
5471 r_type
= R_SH_TLS_LE_32
;
5476 case R_SH_GOTOFFFUNCDESC
:
5477 case R_SH_GOTOFFFUNCDESC20
:
5479 case R_SH_GOTFUNCDESC
:
5480 case R_SH_GOTFUNCDESC20
:
5483 if (h
->dynindx
== -1)
5484 switch (ELF_ST_VISIBILITY (h
->other
))
5490 bfd_elf_link_record_dynamic_symbol (info
, h
);
5497 /* Some relocs require a global offset table. */
5498 if (htab
->root
.sgot
== NULL
)
5503 /* This may require an rofixup. */
5513 case R_SH_GOTFUNCDESC
:
5514 case R_SH_GOTFUNCDESC20
:
5515 case R_SH_GOTOFFFUNCDESC
:
5516 case R_SH_GOTOFFFUNCDESC20
:
5518 case R_SH_TLS_GD_32
:
5519 case R_SH_TLS_LD_32
:
5520 case R_SH_TLS_IE_32
:
5521 if (htab
->root
.dynobj
== NULL
)
5522 htab
->root
.dynobj
= abfd
;
5523 if (!create_got_section (htab
->root
.dynobj
, info
))
5534 /* This relocation describes the C++ object vtable hierarchy.
5535 Reconstruct it for later use during GC. */
5536 case R_SH_GNU_VTINHERIT
:
5537 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
5541 /* This relocation describes which C++ vtable entries are actually
5542 used. Record for later use during GC. */
5543 case R_SH_GNU_VTENTRY
:
5544 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
5548 case R_SH_TLS_IE_32
:
5549 if (bfd_link_pic (info
))
5550 info
->flags
|= DF_STATIC_TLS
;
5554 case R_SH_TLS_GD_32
:
5557 case R_SH_GOTFUNCDESC
:
5558 case R_SH_GOTFUNCDESC20
:
5562 got_type
= GOT_NORMAL
;
5564 case R_SH_TLS_GD_32
:
5565 got_type
= GOT_TLS_GD
;
5567 case R_SH_TLS_IE_32
:
5568 got_type
= GOT_TLS_IE
;
5570 case R_SH_GOTFUNCDESC
:
5571 case R_SH_GOTFUNCDESC20
:
5572 got_type
= GOT_FUNCDESC
;
5578 h
->got
.refcount
+= 1;
5579 old_got_type
= sh_elf_hash_entry (h
)->got_type
;
5583 bfd_signed_vma
*local_got_refcounts
;
5585 /* This is a global offset table entry for a local
5587 local_got_refcounts
= elf_local_got_refcounts (abfd
);
5588 if (local_got_refcounts
== NULL
)
5592 size
= symtab_hdr
->sh_info
;
5593 size
*= sizeof (bfd_signed_vma
);
5594 size
+= symtab_hdr
->sh_info
;
5595 local_got_refcounts
= ((bfd_signed_vma
*)
5596 bfd_zalloc (abfd
, size
));
5597 if (local_got_refcounts
== NULL
)
5599 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
5600 sh_elf_local_got_type (abfd
)
5601 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
5603 local_got_refcounts
[r_symndx
] += 1;
5604 old_got_type
= sh_elf_local_got_type (abfd
) [r_symndx
];
5607 /* If a TLS symbol is accessed using IE at least once,
5608 there is no point to use dynamic model for it. */
5609 if (old_got_type
!= got_type
&& old_got_type
!= GOT_UNKNOWN
5610 && (old_got_type
!= GOT_TLS_GD
|| got_type
!= GOT_TLS_IE
))
5612 if (old_got_type
== GOT_TLS_IE
&& got_type
== GOT_TLS_GD
)
5613 got_type
= GOT_TLS_IE
;
5616 if ((old_got_type
== GOT_FUNCDESC
|| got_type
== GOT_FUNCDESC
)
5617 && (old_got_type
== GOT_NORMAL
|| got_type
== GOT_NORMAL
))
5619 /* xgettext:c-format */
5620 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5621 abfd
, h
->root
.root
.string
);
5622 else if (old_got_type
== GOT_FUNCDESC
5623 || got_type
== GOT_FUNCDESC
)
5625 /* xgettext:c-format */
5626 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5627 abfd
, h
->root
.root
.string
);
5630 /* xgettext:c-format */
5631 (_("%pB: `%s' accessed both as normal and thread local symbol"),
5632 abfd
, h
->root
.root
.string
);
5637 if (old_got_type
!= got_type
)
5640 sh_elf_hash_entry (h
)->got_type
= got_type
;
5642 sh_elf_local_got_type (abfd
) [r_symndx
] = got_type
;
5647 case R_SH_TLS_LD_32
:
5648 sh_elf_hash_table(info
)->tls_ldm_got
.refcount
+= 1;
5652 case R_SH_GOTOFFFUNCDESC
:
5653 case R_SH_GOTOFFFUNCDESC20
:
5657 (_("%pB: Function descriptor relocation with non-zero addend"),
5664 union gotref
*local_funcdesc
;
5666 /* We need a function descriptor for a local symbol. */
5667 local_funcdesc
= sh_elf_local_funcdesc (abfd
);
5668 if (local_funcdesc
== NULL
)
5672 size
= symtab_hdr
->sh_info
* sizeof (union gotref
);
5673 local_funcdesc
= (union gotref
*) bfd_zalloc (abfd
, size
);
5674 if (local_funcdesc
== NULL
)
5676 sh_elf_local_funcdesc (abfd
) = local_funcdesc
;
5678 local_funcdesc
[r_symndx
].refcount
+= 1;
5680 if (r_type
== R_SH_FUNCDESC
)
5682 if (!bfd_link_pic (info
))
5683 htab
->srofixup
->size
+= 4;
5685 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5690 sh_elf_hash_entry (h
)->funcdesc
.refcount
++;
5691 if (r_type
== R_SH_FUNCDESC
)
5692 sh_elf_hash_entry (h
)->abs_funcdesc_refcount
++;
5694 /* If there is a function descriptor reference, then
5695 there should not be any non-FDPIC references. */
5696 old_got_type
= sh_elf_hash_entry (h
)->got_type
;
5697 if (old_got_type
!= GOT_FUNCDESC
&& old_got_type
!= GOT_UNKNOWN
)
5699 if (old_got_type
== GOT_NORMAL
)
5701 /* xgettext:c-format */
5702 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5703 abfd
, h
->root
.root
.string
);
5706 /* xgettext:c-format */
5707 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5708 abfd
, h
->root
.root
.string
);
5714 /* If this is a local symbol, we resolve it directly without
5715 creating a procedure linkage table entry. */
5719 || ! bfd_link_pic (info
)
5721 || h
->dynindx
== -1)
5725 h
->plt
.refcount
+= 1;
5726 ((struct elf_sh_link_hash_entry
*) h
)->gotplt_refcount
+= 1;
5731 /* This symbol requires a procedure linkage table entry. We
5732 actually build the entry in adjust_dynamic_symbol,
5733 because this might be a case of linking PIC code which is
5734 never referenced by a dynamic object, in which case we
5735 don't need to generate a procedure linkage table entry
5738 /* If this is a local symbol, we resolve it directly without
5739 creating a procedure linkage table entry. */
5743 if (h
->forced_local
)
5747 h
->plt
.refcount
+= 1;
5752 if (h
!= NULL
&& ! bfd_link_pic (info
))
5755 h
->plt
.refcount
+= 1;
5758 /* If we are creating a shared library, and this is a reloc
5759 against a global symbol, or a non PC relative reloc
5760 against a local symbol, then we need to copy the reloc
5761 into the shared library. However, if we are linking with
5762 -Bsymbolic, we do not need to copy a reloc against a
5763 global symbol which is defined in an object we are
5764 including in the link (i.e., DEF_REGULAR is set). At
5765 this point we have not seen all the input files, so it is
5766 possible that DEF_REGULAR is not set now but will be set
5767 later (it is never cleared). We account for that
5768 possibility below by storing information in the
5769 dyn_relocs field of the hash table entry. A similar
5770 situation occurs when creating shared libraries and symbol
5771 visibility changes render the symbol local.
5773 If on the other hand, we are creating an executable, we
5774 may need to keep relocations for symbols satisfied by a
5775 dynamic library if we manage to avoid copy relocs for the
5777 if ((bfd_link_pic (info
)
5778 && (sec
->flags
& SEC_ALLOC
) != 0
5779 && (r_type
!= R_SH_REL32
5781 && (! info
->symbolic
5782 || h
->root
.type
== bfd_link_hash_defweak
5783 || !h
->def_regular
))))
5784 || (! bfd_link_pic (info
)
5785 && (sec
->flags
& SEC_ALLOC
) != 0
5787 && (h
->root
.type
== bfd_link_hash_defweak
5788 || !h
->def_regular
)))
5790 struct elf_dyn_relocs
*p
;
5791 struct elf_dyn_relocs
**head
;
5793 if (htab
->root
.dynobj
== NULL
)
5794 htab
->root
.dynobj
= abfd
;
5796 /* When creating a shared object, we must copy these
5797 reloc types into the output file. We create a reloc
5798 section in dynobj and make room for this reloc. */
5801 sreloc
= _bfd_elf_make_dynamic_reloc_section
5802 (sec
, htab
->root
.dynobj
, 2, abfd
, /*rela?*/ TRUE
);
5808 /* If this is a global symbol, we count the number of
5809 relocations we need for this symbol. */
5811 head
= &h
->dyn_relocs
;
5814 /* Track dynamic relocs needed for local syms too. */
5817 Elf_Internal_Sym
*isym
;
5819 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
5824 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
5828 vpp
= &elf_section_data (s
)->local_dynrel
;
5829 head
= (struct elf_dyn_relocs
**) vpp
;
5833 if (p
== NULL
|| p
->sec
!= sec
)
5835 size_t amt
= sizeof (*p
);
5836 p
= bfd_alloc (htab
->root
.dynobj
, amt
);
5847 if (r_type
== R_SH_REL32
)
5851 /* Allocate the fixup regardless of whether we need a relocation.
5852 If we end up generating the relocation, we'll unallocate the
5854 if (htab
->fdpic_p
&& !bfd_link_pic (info
)
5855 && r_type
== R_SH_DIR32
5856 && (sec
->flags
& SEC_ALLOC
) != 0)
5857 htab
->srofixup
->size
+= 4;
5860 case R_SH_TLS_LE_32
:
5861 if (bfd_link_dll (info
))
5864 (_("%pB: TLS local exec code cannot be linked into shared objects"),
5871 case R_SH_TLS_LDO_32
:
5872 /* Nothing to do. */
5883 #ifndef sh_elf_set_mach_from_flags
5884 static unsigned int sh_ef_bfd_table
[] = { EF_SH_BFD_TABLE
};
5887 sh_elf_set_mach_from_flags (bfd
*abfd
)
5889 flagword flags
= elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
;
5891 if (flags
>= ARRAY_SIZE (sh_ef_bfd_table
))
5894 if (sh_ef_bfd_table
[flags
] == 0)
5897 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, sh_ef_bfd_table
[flags
]);
5903 /* Reverse table lookup for sh_ef_bfd_table[].
5904 Given a bfd MACH value from archures.c
5905 return the equivalent ELF flags from the table.
5906 Return -1 if no match is found. */
5909 sh_elf_get_flags_from_mach (unsigned long mach
)
5911 int i
= ARRAY_SIZE (sh_ef_bfd_table
) - 1;
5914 if (sh_ef_bfd_table
[i
] == mach
)
5917 /* shouldn't get here */
5922 #endif /* not sh_elf_set_mach_from_flags */
5924 #ifndef sh_elf_copy_private_data
5925 /* Copy backend specific data from one object module to another */
5928 sh_elf_copy_private_data (bfd
* ibfd
, bfd
* obfd
)
5930 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
5933 if (! _bfd_elf_copy_private_bfd_data (ibfd
, obfd
))
5936 return sh_elf_set_mach_from_flags (obfd
);
5938 #endif /* not sh_elf_copy_private_data */
5940 #ifndef sh_elf_merge_private_data
5942 /* This function returns the ELF architecture number that
5943 corresponds to the given arch_sh* flags. */
5946 sh_find_elf_flags (unsigned int arch_set
)
5948 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
5949 unsigned long bfd_mach
= sh_get_bfd_mach_from_arch_set (arch_set
);
5951 return sh_elf_get_flags_from_mach (bfd_mach
);
5954 /* Merge the architecture type of two BFD files, such that the
5955 resultant architecture supports all the features required
5956 by the two input BFDs.
5957 If the input BFDs are multually incompatible - i.e. one uses
5958 DSP while the other uses FPU - or there is no known architecture
5959 that fits the requirements then an error is emitted. */
5962 sh_merge_bfd_arch (bfd
*ibfd
, struct bfd_link_info
*info
)
5964 bfd
*obfd
= info
->output_bfd
;
5965 unsigned int old_arch
, new_arch
, merged_arch
;
5967 if (! _bfd_generic_verify_endian_match (ibfd
, info
))
5970 old_arch
= sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd
));
5971 new_arch
= sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd
));
5973 merged_arch
= SH_MERGE_ARCH_SET (old_arch
, new_arch
);
5975 if (!SH_VALID_CO_ARCH_SET (merged_arch
))
5978 /* xgettext:c-format */
5979 (_("%pB: uses %s instructions while previous modules "
5980 "use %s instructions"),
5982 SH_ARCH_SET_HAS_DSP (new_arch
) ? "dsp" : "floating point",
5983 SH_ARCH_SET_HAS_DSP (new_arch
) ? "floating point" : "dsp");
5984 bfd_set_error (bfd_error_bad_value
);
5987 else if (!SH_VALID_ARCH_SET (merged_arch
))
5990 /* xgettext:c-format */
5991 (_("internal error: merge of architecture '%s' with "
5992 "architecture '%s' produced unknown architecture"),
5993 bfd_printable_name (obfd
),
5994 bfd_printable_name (ibfd
));
5995 bfd_set_error (bfd_error_bad_value
);
5999 bfd_default_set_arch_mach (obfd
, bfd_arch_sh
,
6000 sh_get_bfd_mach_from_arch_set (merged_arch
));
6005 /* This routine initialises the elf flags when required and
6006 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
6009 sh_elf_merge_private_data (bfd
*ibfd
, struct bfd_link_info
*info
)
6011 bfd
*obfd
= info
->output_bfd
;
6013 /* FIXME: What should be checked when linking shared libraries? */
6014 if ((ibfd
->flags
& DYNAMIC
) != 0)
6017 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
6020 if (! elf_flags_init (obfd
))
6022 /* This happens when ld starts out with a 'blank' output file. */
6023 elf_flags_init (obfd
) = TRUE
;
6024 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
6025 sh_elf_set_mach_from_flags (obfd
);
6026 if (elf_elfheader (obfd
)->e_flags
& EF_SH_FDPIC
)
6027 elf_elfheader (obfd
)->e_flags
&= ~EF_SH_PIC
;
6030 if (! sh_merge_bfd_arch (ibfd
, info
))
6032 _bfd_error_handler (_("%pB: uses instructions which are incompatible "
6033 "with instructions used in previous modules"),
6035 bfd_set_error (bfd_error_bad_value
);
6039 elf_elfheader (obfd
)->e_flags
&= ~EF_SH_MACH_MASK
;
6040 elf_elfheader (obfd
)->e_flags
|=
6041 sh_elf_get_flags_from_mach (bfd_get_mach (obfd
));
6043 if (fdpic_object_p (ibfd
) != fdpic_object_p (obfd
))
6045 _bfd_error_handler (_("%pB: attempt to mix FDPIC and non-FDPIC objects"),
6047 bfd_set_error (bfd_error_bad_value
);
6053 #endif /* not sh_elf_merge_private_data */
6055 /* Override the generic function because we need to store sh_elf_obj_tdata
6056 as the specific tdata. We set also the machine architecture from flags
6060 sh_elf_object_p (bfd
*abfd
)
6062 if (! sh_elf_set_mach_from_flags (abfd
))
6065 return (((elf_elfheader (abfd
)->e_flags
& EF_SH_FDPIC
) != 0)
6066 == fdpic_object_p (abfd
));
6069 /* Finish up dynamic symbol handling. We set the contents of various
6070 dynamic sections here. */
6073 sh_elf_finish_dynamic_symbol (bfd
*output_bfd
, struct bfd_link_info
*info
,
6074 struct elf_link_hash_entry
*h
,
6075 Elf_Internal_Sym
*sym
)
6077 struct elf_sh_link_hash_table
*htab
;
6079 htab
= sh_elf_hash_table (info
);
6083 if (h
->plt
.offset
!= (bfd_vma
) -1)
6091 Elf_Internal_Rela rel
;
6093 const struct elf_sh_plt_info
*plt_info
;
6095 /* This symbol has an entry in the procedure linkage table. Set
6098 BFD_ASSERT (h
->dynindx
!= -1);
6100 splt
= htab
->root
.splt
;
6101 sgotplt
= htab
->root
.sgotplt
;
6102 srelplt
= htab
->root
.srelplt
;
6103 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srelplt
!= NULL
);
6105 /* Get the index in the procedure linkage table which
6106 corresponds to this symbol. This is the index of this symbol
6107 in all the symbols for which we are making plt entries. The
6108 first entry in the procedure linkage table is reserved. */
6109 plt_index
= get_plt_index (htab
->plt_info
, h
->plt
.offset
);
6111 plt_info
= htab
->plt_info
;
6112 if (plt_info
->short_plt
!= NULL
&& plt_index
<= MAX_SHORT_PLT
)
6113 plt_info
= plt_info
->short_plt
;
6115 /* Get the offset into the .got table of the entry that
6116 corresponds to this function. */
6118 /* The offset must be relative to the GOT symbol, twelve bytes
6119 before the end of .got.plt. Each descriptor is eight
6121 got_offset
= plt_index
* 8 + 12 - sgotplt
->size
;
6123 /* Each .got entry is 4 bytes. The first three are
6125 got_offset
= (plt_index
+ 3) * 4;
6128 if (bfd_link_pic (info
))
6129 got_offset
-= GOT_BIAS
;
6132 /* Fill in the entry in the procedure linkage table. */
6133 memcpy (splt
->contents
+ h
->plt
.offset
,
6134 plt_info
->symbol_entry
,
6135 plt_info
->symbol_entry_size
);
6137 if (bfd_link_pic (info
) || htab
->fdpic_p
)
6139 if (plt_info
->symbol_fields
.got20
)
6141 bfd_reloc_status_type r
;
6142 r
= install_movi20_field (output_bfd
, got_offset
,
6143 splt
->owner
, splt
, splt
->contents
,
6145 + plt_info
->symbol_fields
.got_entry
);
6146 BFD_ASSERT (r
== bfd_reloc_ok
);
6149 install_plt_field (output_bfd
, FALSE
, got_offset
,
6152 + plt_info
->symbol_fields
.got_entry
));
6156 BFD_ASSERT (!plt_info
->symbol_fields
.got20
);
6158 install_plt_field (output_bfd
, FALSE
,
6159 (sgotplt
->output_section
->vma
6160 + sgotplt
->output_offset
6164 + plt_info
->symbol_fields
.got_entry
));
6165 if (htab
->vxworks_p
)
6167 unsigned int reachable_plts
, plts_per_4k
;
6170 /* Divide the PLT into groups. The first group contains
6171 REACHABLE_PLTS entries and the other groups contain
6172 PLTS_PER_4K entries. Entries in the first group can
6173 branch directly to .plt; those in later groups branch
6174 to the last element of the previous group. */
6175 /* ??? It would be better to create multiple copies of
6176 the common resolver stub. */
6177 reachable_plts
= ((4096
6178 - plt_info
->plt0_entry_size
6179 - (plt_info
->symbol_fields
.plt
+ 4))
6180 / plt_info
->symbol_entry_size
) + 1;
6181 plts_per_4k
= (4096 / plt_info
->symbol_entry_size
);
6182 if (plt_index
< reachable_plts
)
6183 distance
= -(h
->plt
.offset
6184 + plt_info
->symbol_fields
.plt
);
6186 distance
= -(((plt_index
- reachable_plts
) % plts_per_4k
+ 1)
6187 * plt_info
->symbol_entry_size
);
6189 /* Install the 'bra' with this offset. */
6190 bfd_put_16 (output_bfd
,
6191 0xa000 | (0x0fff & ((distance
- 4) / 2)),
6194 + plt_info
->symbol_fields
.plt
));
6197 install_plt_field (output_bfd
, TRUE
,
6198 splt
->output_section
->vma
+ splt
->output_offset
,
6201 + plt_info
->symbol_fields
.plt
));
6204 /* Make got_offset relative to the start of .got.plt. */
6206 if (bfd_link_pic (info
))
6207 got_offset
+= GOT_BIAS
;
6210 got_offset
= plt_index
* 8;
6212 if (plt_info
->symbol_fields
.reloc_offset
!= MINUS_ONE
)
6213 install_plt_field (output_bfd
, FALSE
,
6214 plt_index
* sizeof (Elf32_External_Rela
),
6217 + plt_info
->symbol_fields
.reloc_offset
));
6219 /* Fill in the entry in the global offset table. */
6220 bfd_put_32 (output_bfd
,
6221 (splt
->output_section
->vma
6222 + splt
->output_offset
6224 + plt_info
->symbol_resolve_offset
),
6225 sgotplt
->contents
+ got_offset
);
6227 bfd_put_32 (output_bfd
,
6228 sh_elf_osec_to_segment (output_bfd
, splt
->output_section
),
6229 sgotplt
->contents
+ got_offset
+ 4);
6231 /* Fill in the entry in the .rela.plt section. */
6232 rel
.r_offset
= (sgotplt
->output_section
->vma
6233 + sgotplt
->output_offset
6236 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_FUNCDESC_VALUE
);
6238 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
6241 rel
.r_addend
= GOT_BIAS
;
6243 loc
= srelplt
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
6244 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6246 if (htab
->vxworks_p
&& !bfd_link_pic (info
))
6248 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6249 Begin by pointing LOC to the first such relocation. */
6250 loc
= (htab
->srelplt2
->contents
6251 + (plt_index
* 2 + 1) * sizeof (Elf32_External_Rela
));
6253 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6254 for the PLT entry's pointer to the .got.plt entry. */
6255 rel
.r_offset
= (splt
->output_section
->vma
6256 + splt
->output_offset
6258 + plt_info
->symbol_fields
.got_entry
);
6259 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
6260 rel
.r_addend
= got_offset
;
6261 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6262 loc
+= sizeof (Elf32_External_Rela
);
6264 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6265 the .got.plt entry, which initially points to .plt. */
6266 rel
.r_offset
= (sgotplt
->output_section
->vma
6267 + sgotplt
->output_offset
6269 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
, R_SH_DIR32
);
6271 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6274 if (!h
->def_regular
)
6276 /* Mark the symbol as undefined, rather than as defined in
6277 the .plt section. Leave the value alone. */
6278 sym
->st_shndx
= SHN_UNDEF
;
6282 if (h
->got
.offset
!= (bfd_vma
) -1
6283 && sh_elf_hash_entry (h
)->got_type
!= GOT_TLS_GD
6284 && sh_elf_hash_entry (h
)->got_type
!= GOT_TLS_IE
6285 && sh_elf_hash_entry (h
)->got_type
!= GOT_FUNCDESC
)
6289 Elf_Internal_Rela rel
;
6292 /* This symbol has an entry in the global offset table. Set it
6295 sgot
= htab
->root
.sgot
;
6296 srelgot
= htab
->root
.srelgot
;
6297 BFD_ASSERT (sgot
!= NULL
&& srelgot
!= NULL
);
6299 rel
.r_offset
= (sgot
->output_section
->vma
6300 + sgot
->output_offset
6301 + (h
->got
.offset
&~ (bfd_vma
) 1));
6303 /* If this is a static link, or it is a -Bsymbolic link and the
6304 symbol is defined locally or was forced to be local because
6305 of a version file, we just want to emit a RELATIVE reloc.
6306 The entry in the global offset table will already have been
6307 initialized in the relocate_section function. */
6308 if (bfd_link_pic (info
)
6309 && SYMBOL_REFERENCES_LOCAL (info
, h
))
6313 asection
*sec
= h
->root
.u
.def
.section
;
6315 = elf_section_data (sec
->output_section
)->dynindx
;
6317 rel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
6318 rel
.r_addend
= (h
->root
.u
.def
.value
6319 + h
->root
.u
.def
.section
->output_offset
);
6323 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
6324 rel
.r_addend
= (h
->root
.u
.def
.value
6325 + h
->root
.u
.def
.section
->output_section
->vma
6326 + h
->root
.u
.def
.section
->output_offset
);
6331 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
6332 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
6336 loc
= srelgot
->contents
;
6337 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6338 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6344 Elf_Internal_Rela rel
;
6347 /* This symbol needs a copy reloc. Set it up. */
6349 BFD_ASSERT (h
->dynindx
!= -1
6350 && (h
->root
.type
== bfd_link_hash_defined
6351 || h
->root
.type
== bfd_link_hash_defweak
));
6353 s
= bfd_get_linker_section (htab
->root
.dynobj
, ".rela.bss");
6354 BFD_ASSERT (s
!= NULL
);
6356 rel
.r_offset
= (h
->root
.u
.def
.value
6357 + h
->root
.u
.def
.section
->output_section
->vma
6358 + h
->root
.u
.def
.section
->output_offset
);
6359 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
6361 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6362 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6365 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
6366 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6368 if (h
== htab
->root
.hdynamic
6369 || (!htab
->vxworks_p
&& h
== htab
->root
.hgot
))
6370 sym
->st_shndx
= SHN_ABS
;
6375 /* Finish up the dynamic sections. */
6378 sh_elf_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6380 struct elf_sh_link_hash_table
*htab
;
6384 htab
= sh_elf_hash_table (info
);
6388 sgotplt
= htab
->root
.sgotplt
;
6389 sdyn
= bfd_get_linker_section (htab
->root
.dynobj
, ".dynamic");
6391 if (htab
->root
.dynamic_sections_created
)
6394 Elf32_External_Dyn
*dyncon
, *dynconend
;
6396 BFD_ASSERT (sgotplt
!= NULL
&& sdyn
!= NULL
);
6398 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
6399 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
6400 for (; dyncon
< dynconend
; dyncon
++)
6402 Elf_Internal_Dyn dyn
;
6405 bfd_elf32_swap_dyn_in (htab
->root
.dynobj
, dyncon
, &dyn
);
6411 && elf_vxworks_finish_dynamic_entry (output_bfd
, &dyn
))
6412 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6416 BFD_ASSERT (htab
->root
.hgot
!= NULL
);
6417 s
= htab
->root
.hgot
->root
.u
.def
.section
;
6418 dyn
.d_un
.d_ptr
= htab
->root
.hgot
->root
.u
.def
.value
6419 + s
->output_section
->vma
+ s
->output_offset
;
6420 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6424 s
= htab
->root
.srelplt
->output_section
;
6425 BFD_ASSERT (s
!= NULL
);
6426 dyn
.d_un
.d_ptr
= s
->vma
;
6427 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6431 s
= htab
->root
.srelplt
->output_section
;
6432 BFD_ASSERT (s
!= NULL
);
6433 dyn
.d_un
.d_val
= s
->size
;
6434 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6439 /* Fill in the first entry in the procedure linkage table. */
6440 splt
= htab
->root
.splt
;
6441 if (splt
&& splt
->size
> 0 && htab
->plt_info
->plt0_entry
)
6445 memcpy (splt
->contents
,
6446 htab
->plt_info
->plt0_entry
,
6447 htab
->plt_info
->plt0_entry_size
);
6448 for (i
= 0; i
< ARRAY_SIZE (htab
->plt_info
->plt0_got_fields
); i
++)
6449 if (htab
->plt_info
->plt0_got_fields
[i
] != MINUS_ONE
)
6450 install_plt_field (output_bfd
, FALSE
,
6451 (sgotplt
->output_section
->vma
6452 + sgotplt
->output_offset
6455 + htab
->plt_info
->plt0_got_fields
[i
]));
6457 if (htab
->vxworks_p
)
6459 /* Finalize the .rela.plt.unloaded contents. */
6460 Elf_Internal_Rela rel
;
6463 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
6464 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
6465 loc
= htab
->srelplt2
->contents
;
6466 rel
.r_offset
= (splt
->output_section
->vma
6467 + splt
->output_offset
6468 + htab
->plt_info
->plt0_got_fields
[2]);
6469 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
6471 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6472 loc
+= sizeof (Elf32_External_Rela
);
6474 /* Fix up the remaining .rela.plt.unloaded relocations.
6475 They may have the wrong symbol index for _G_O_T_ or
6476 _P_L_T_ depending on the order in which symbols were
6478 while (loc
< htab
->srelplt2
->contents
+ htab
->srelplt2
->size
)
6480 /* The PLT entry's pointer to the .got.plt slot. */
6481 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
6482 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
,
6484 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6485 loc
+= sizeof (Elf32_External_Rela
);
6487 /* The .got.plt slot's pointer to .plt. */
6488 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
6489 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
,
6491 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6492 loc
+= sizeof (Elf32_External_Rela
);
6496 /* UnixWare sets the entsize of .plt to 4, although that doesn't
6497 really seem like the right value. */
6498 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
6502 /* Fill in the first three entries in the global offset table. */
6503 if (sgotplt
&& sgotplt
->size
> 0 && !htab
->fdpic_p
)
6506 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
6508 bfd_put_32 (output_bfd
,
6509 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
6511 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
6512 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
6515 if (sgotplt
&& sgotplt
->size
> 0)
6516 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
6518 /* At the very end of the .rofixup section is a pointer to the GOT. */
6519 if (htab
->fdpic_p
&& htab
->srofixup
!= NULL
)
6521 struct elf_link_hash_entry
*hgot
= htab
->root
.hgot
;
6522 bfd_vma got_value
= hgot
->root
.u
.def
.value
6523 + hgot
->root
.u
.def
.section
->output_section
->vma
6524 + hgot
->root
.u
.def
.section
->output_offset
;
6526 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
, got_value
);
6528 /* Make sure we allocated and generated the same number of fixups. */
6529 BFD_ASSERT (htab
->srofixup
->reloc_count
* 4 == htab
->srofixup
->size
);
6532 if (htab
->srelfuncdesc
)
6533 BFD_ASSERT (htab
->srelfuncdesc
->reloc_count
* sizeof (Elf32_External_Rela
)
6534 == htab
->srelfuncdesc
->size
);
6536 if (htab
->root
.srelgot
)
6537 BFD_ASSERT (htab
->root
.srelgot
->reloc_count
* sizeof (Elf32_External_Rela
)
6538 == htab
->root
.srelgot
->size
);
6543 static enum elf_reloc_type_class
6544 sh_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6545 const asection
*rel_sec ATTRIBUTE_UNUSED
,
6546 const Elf_Internal_Rela
*rela
)
6548 switch ((int) ELF32_R_TYPE (rela
->r_info
))
6551 return reloc_class_relative
;
6553 return reloc_class_plt
;
6555 return reloc_class_copy
;
6557 return reloc_class_normal
;
6561 #if !defined SH_TARGET_ALREADY_DEFINED
6562 /* Support for Linux core dump NOTE sections. */
6565 elf32_shlin_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6570 switch (note
->descsz
)
6575 case 168: /* Linux/SH */
6577 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
6580 elf_tdata (abfd
)->core
->lwpid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
6589 /* Make a ".reg/999" section. */
6590 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6591 size
, note
->descpos
+ offset
);
6595 elf32_shlin_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6597 switch (note
->descsz
)
6602 case 124: /* Linux/SH elf_prpsinfo */
6603 elf_tdata (abfd
)->core
->program
6604 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
6605 elf_tdata (abfd
)->core
->command
6606 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
6609 /* Note that for some reason, a spurious space is tacked
6610 onto the end of the args in some (at least one anyway)
6611 implementations, so strip it off if it exists. */
6614 char *command
= elf_tdata (abfd
)->core
->command
;
6615 int n
= strlen (command
);
6617 if (0 < n
&& command
[n
- 1] == ' ')
6618 command
[n
- 1] = '\0';
6623 #endif /* not SH_TARGET_ALREADY_DEFINED */
6626 /* Return address for Ith PLT stub in section PLT, for relocation REL
6627 or (bfd_vma) -1 if it should not be included. */
6630 sh_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
6631 const arelent
*rel ATTRIBUTE_UNUSED
)
6633 const struct elf_sh_plt_info
*plt_info
;
6635 plt_info
= get_plt_info (plt
->owner
, (plt
->owner
->flags
& DYNAMIC
) != 0);
6636 return plt
->vma
+ get_plt_offset (plt_info
, i
);
6639 /* Decide whether to attempt to turn absptr or lsda encodings in
6640 shared libraries into pcrel within the given input section. */
6643 sh_elf_use_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
6644 struct bfd_link_info
*info
,
6645 asection
*eh_frame_section ATTRIBUTE_UNUSED
)
6647 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
6649 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
6656 /* Adjust the contents of an eh_frame_hdr section before they're output. */
6659 sh_elf_encode_eh_address (bfd
*abfd
,
6660 struct bfd_link_info
*info
,
6661 asection
*osec
, bfd_vma offset
,
6662 asection
*loc_sec
, bfd_vma loc_offset
,
6665 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
6666 struct elf_link_hash_entry
*h
;
6669 return _bfd_elf_encode_eh_address (abfd
, info
, osec
, offset
, loc_sec
,
6670 loc_offset
, encoded
);
6672 h
= htab
->root
.hgot
;
6673 BFD_ASSERT (h
&& h
->root
.type
== bfd_link_hash_defined
);
6675 if (! h
|| (sh_elf_osec_to_segment (abfd
, osec
)
6676 == sh_elf_osec_to_segment (abfd
, loc_sec
->output_section
)))
6677 return _bfd_elf_encode_eh_address (abfd
, info
, osec
, offset
,
6678 loc_sec
, loc_offset
, encoded
);
6680 BFD_ASSERT (sh_elf_osec_to_segment (abfd
, osec
)
6681 == (sh_elf_osec_to_segment
6682 (abfd
, h
->root
.u
.def
.section
->output_section
)));
6684 *encoded
= osec
->vma
+ offset
6685 - (h
->root
.u
.def
.value
6686 + h
->root
.u
.def
.section
->output_section
->vma
6687 + h
->root
.u
.def
.section
->output_offset
);
6689 return DW_EH_PE_datarel
| DW_EH_PE_sdata4
;
6692 #if !defined SH_TARGET_ALREADY_DEFINED
6693 #define TARGET_BIG_SYM sh_elf32_vec
6694 #define TARGET_BIG_NAME "elf32-sh"
6695 #define TARGET_LITTLE_SYM sh_elf32_le_vec
6696 #define TARGET_LITTLE_NAME "elf32-shl"
6699 #define ELF_ARCH bfd_arch_sh
6700 #define ELF_TARGET_ID SH_ELF_DATA
6701 #define ELF_MACHINE_CODE EM_SH
6702 #ifdef __QNXTARGET__
6703 #define ELF_MAXPAGESIZE 0x1000
6705 #define ELF_MAXPAGESIZE 0x80
6708 #define elf_symbol_leading_char '_'
6710 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6711 #define bfd_elf32_bfd_reloc_name_lookup \
6712 sh_elf_reloc_name_lookup
6713 #define elf_info_to_howto sh_elf_info_to_howto
6714 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
6715 #define elf_backend_relocate_section sh_elf_relocate_section
6716 #define bfd_elf32_bfd_get_relocated_section_contents \
6717 sh_elf_get_relocated_section_contents
6718 #define bfd_elf32_mkobject sh_elf_mkobject
6719 #define elf_backend_object_p sh_elf_object_p
6720 #define bfd_elf32_bfd_copy_private_bfd_data \
6721 sh_elf_copy_private_data
6722 #define bfd_elf32_bfd_merge_private_bfd_data \
6723 sh_elf_merge_private_data
6725 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6726 #define elf_backend_check_relocs sh_elf_check_relocs
6727 #define elf_backend_copy_indirect_symbol \
6728 sh_elf_copy_indirect_symbol
6729 #define elf_backend_create_dynamic_sections \
6730 sh_elf_create_dynamic_sections
6731 #define bfd_elf32_bfd_link_hash_table_create \
6732 sh_elf_link_hash_table_create
6733 #define elf_backend_adjust_dynamic_symbol \
6734 sh_elf_adjust_dynamic_symbol
6735 #define elf_backend_always_size_sections \
6736 sh_elf_always_size_sections
6737 #define elf_backend_size_dynamic_sections \
6738 sh_elf_size_dynamic_sections
6739 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
6740 #define elf_backend_finish_dynamic_symbol \
6741 sh_elf_finish_dynamic_symbol
6742 #define elf_backend_finish_dynamic_sections \
6743 sh_elf_finish_dynamic_sections
6744 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
6745 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
6746 #define elf_backend_can_make_relative_eh_frame \
6747 sh_elf_use_relative_eh_frame
6748 #define elf_backend_can_make_lsda_relative_eh_frame \
6749 sh_elf_use_relative_eh_frame
6750 #define elf_backend_encode_eh_address \
6751 sh_elf_encode_eh_address
6753 #define elf_backend_stack_align 8
6754 #define elf_backend_can_gc_sections 1
6755 #define elf_backend_can_refcount 1
6756 #define elf_backend_want_got_plt 1
6757 #define elf_backend_plt_readonly 1
6758 #define elf_backend_want_plt_sym 0
6759 #define elf_backend_got_header_size 12
6760 #define elf_backend_dtrel_excludes_plt 1
6762 #define elf_backend_linux_prpsinfo32_ugid16 TRUE
6764 #if !defined SH_TARGET_ALREADY_DEFINED
6766 #include "elf32-target.h"
6768 /* NetBSD support. */
6769 #undef TARGET_BIG_SYM
6770 #define TARGET_BIG_SYM sh_elf32_nbsd_vec
6771 #undef TARGET_BIG_NAME
6772 #define TARGET_BIG_NAME "elf32-sh-nbsd"
6773 #undef TARGET_LITTLE_SYM
6774 #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
6775 #undef TARGET_LITTLE_NAME
6776 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6777 #undef ELF_MAXPAGESIZE
6778 #define ELF_MAXPAGESIZE 0x10000
6779 #undef ELF_COMMONPAGESIZE
6780 #undef elf_symbol_leading_char
6781 #define elf_symbol_leading_char 0
6783 #define elf32_bed elf32_sh_nbsd_bed
6785 #include "elf32-target.h"
6788 /* Linux support. */
6789 #undef TARGET_BIG_SYM
6790 #define TARGET_BIG_SYM sh_elf32_linux_be_vec
6791 #undef TARGET_BIG_NAME
6792 #define TARGET_BIG_NAME "elf32-shbig-linux"
6793 #undef TARGET_LITTLE_SYM
6794 #define TARGET_LITTLE_SYM sh_elf32_linux_vec
6795 #undef TARGET_LITTLE_NAME
6796 #define TARGET_LITTLE_NAME "elf32-sh-linux"
6797 #undef ELF_COMMONPAGESIZE
6798 #define ELF_COMMONPAGESIZE 0x1000
6800 #undef elf_backend_grok_prstatus
6801 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6802 #undef elf_backend_grok_psinfo
6803 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
6805 #define elf32_bed elf32_sh_lin_bed
6807 #include "elf32-target.h"
6810 /* FDPIC support. */
6811 #undef TARGET_BIG_SYM
6812 #define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
6813 #undef TARGET_BIG_NAME
6814 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
6815 #undef TARGET_LITTLE_SYM
6816 #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
6817 #undef TARGET_LITTLE_NAME
6818 #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
6821 #define elf32_bed elf32_sh_fd_bed
6823 #include "elf32-target.h"
6825 /* VxWorks support. */
6826 #undef TARGET_BIG_SYM
6827 #define TARGET_BIG_SYM sh_elf32_vxworks_vec
6828 #undef TARGET_BIG_NAME
6829 #define TARGET_BIG_NAME "elf32-sh-vxworks"
6830 #undef TARGET_LITTLE_SYM
6831 #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
6832 #undef TARGET_LITTLE_NAME
6833 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6835 #define elf32_bed elf32_sh_vxworks_bed
6837 #undef elf_backend_want_plt_sym
6838 #define elf_backend_want_plt_sym 1
6839 #undef elf_symbol_leading_char
6840 #define elf_symbol_leading_char '_'
6841 #define elf_backend_want_got_underscore 1
6842 #undef elf_backend_grok_prstatus
6843 #undef elf_backend_grok_psinfo
6844 #undef elf_backend_add_symbol_hook
6845 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6846 #undef elf_backend_link_output_symbol_hook
6847 #define elf_backend_link_output_symbol_hook \
6848 elf_vxworks_link_output_symbol_hook
6849 #undef elf_backend_emit_relocs
6850 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6851 #undef elf_backend_final_write_processing
6852 #define elf_backend_final_write_processing \
6853 elf_vxworks_final_write_processing
6854 #undef ELF_MAXPAGESIZE
6855 #define ELF_MAXPAGESIZE 0x1000
6856 #undef ELF_COMMONPAGESIZE
6858 #include "elf32-target.h"
6860 #endif /* not SH_TARGET_ALREADY_DEFINED */