1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright (C) 1996-2019 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 static bfd_reloc_status_type sh_elf_reloc
34 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
35 static bfd_reloc_status_type sh_elf_ignore_reloc
36 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
37 static bfd_boolean sh_elf_relax_delete_bytes
38 (bfd
*, asection
*, bfd_vma
, int);
39 static bfd_boolean sh_elf_align_loads
40 (bfd
*, asection
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_boolean
*);
41 static bfd_boolean sh_elf_swap_insns
42 (bfd
*, asection
*, void *, bfd_byte
*, bfd_vma
);
43 static int sh_elf_optimized_tls_reloc
44 (struct bfd_link_info
*, int, int);
45 static bfd_vma dtpoff_base
46 (struct bfd_link_info
*);
48 (struct bfd_link_info
*, bfd_vma
);
50 /* The name of the dynamic interpreter. This is put in the .interp
53 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
55 /* FDPIC binaries have a default 128K stack. */
56 #define DEFAULT_STACK_SIZE 0x20000
58 #define MINUS_ONE ((bfd_vma) 0 - 1)
60 /* Decide whether a reference to a symbol can be resolved locally or
61 not. If the symbol is protected, we want the local address, but
62 its function descriptor must be assigned by the dynamic linker. */
63 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
64 (SYMBOL_REFERENCES_LOCAL (INFO, H) \
65 || ! elf_hash_table (INFO)->dynamic_sections_created)
67 #define SH_PARTIAL32 TRUE
68 #define SH_SRC_MASK32 0xffffffff
69 #define SH_ELF_RELOC sh_elf_reloc
70 static reloc_howto_type sh_elf_howto_table
[] =
72 #include "elf32-sh-relocs.h"
75 #define SH_PARTIAL32 FALSE
76 #define SH_SRC_MASK32 0
77 #define SH_ELF_RELOC bfd_elf_generic_reloc
78 static reloc_howto_type sh_vxworks_howto_table
[] =
80 #include "elf32-sh-relocs.h"
83 /* Return true if OUTPUT_BFD is a VxWorks object. */
86 vxworks_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
88 #if !defined SH_TARGET_ALREADY_DEFINED
89 extern const bfd_target sh_elf32_vxworks_le_vec
;
90 extern const bfd_target sh_elf32_vxworks_vec
;
92 return (abfd
->xvec
== &sh_elf32_vxworks_le_vec
93 || abfd
->xvec
== &sh_elf32_vxworks_vec
);
99 /* Return true if OUTPUT_BFD is an FDPIC object. */
102 fdpic_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
104 #if !defined SH_TARGET_ALREADY_DEFINED
105 extern const bfd_target sh_elf32_fdpic_le_vec
;
106 extern const bfd_target sh_elf32_fdpic_be_vec
;
108 return (abfd
->xvec
== &sh_elf32_fdpic_le_vec
109 || abfd
->xvec
== &sh_elf32_fdpic_be_vec
);
115 /* Return the howto table for ABFD. */
117 static reloc_howto_type
*
118 get_howto_table (bfd
*abfd
)
120 if (vxworks_object_p (abfd
))
121 return sh_vxworks_howto_table
;
122 return sh_elf_howto_table
;
125 static bfd_reloc_status_type
126 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
127 asection
*input_section
, bfd_byte
*contents
,
128 bfd_vma addr
, asection
*symbol_section
,
129 bfd_vma start
, bfd_vma end
)
131 static bfd_vma last_addr
;
132 static asection
*last_symbol_section
;
133 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
138 /* Sanity check the address. */
139 if (addr
> bfd_get_section_limit (input_bfd
, input_section
))
140 return bfd_reloc_outofrange
;
142 /* We require the start and end relocations to be processed consecutively -
143 although we allow then to be processed forwards or backwards. */
147 last_symbol_section
= symbol_section
;
150 if (last_addr
!= addr
)
154 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
155 return bfd_reloc_outofrange
;
157 /* Get the symbol_section contents. */
158 if (symbol_section
!= input_section
)
160 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
161 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
164 if (!bfd_malloc_and_get_section (input_bfd
, symbol_section
,
167 if (contents
!= NULL
)
169 return bfd_reloc_outofrange
;
173 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
174 start_ptr
= contents
+ start
;
175 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
177 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
180 diff
= (last_ptr
- ptr
) >> 1;
181 cum_diff
+= diff
& 1;
184 /* Calculate the start / end values to load into rs / re minus four -
185 so that will cancel out the four we would otherwise have to add to
186 addr to get the value to subtract in order to get relative addressing. */
190 end
= (ptr
+ cum_diff
* 2) - contents
;
194 bfd_vma start0
= start
- 4;
196 while (start0
&& IS_PPI (contents
+ start0
))
198 start0
= start
- 2 - ((start
- start0
) & 2);
199 start
= start0
- cum_diff
- 2;
204 && elf_section_data (symbol_section
)->this_hdr
.contents
!= contents
)
207 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
209 x
= (insn
& 0x200 ? end
: start
) - addr
;
210 if (input_section
!= symbol_section
)
211 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
212 - (input_section
->output_section
->vma
213 + input_section
->output_offset
));
215 if (x
< -128 || x
> 127)
216 return bfd_reloc_overflow
;
218 x
= (insn
& ~0xff) | (x
& 0xff);
219 bfd_put_16 (input_bfd
, (bfd_vma
) x
, contents
+ addr
);
224 /* This function is used for normal relocs. This used to be like the COFF
225 function, and is almost certainly incorrect for other ELF targets. */
227 static bfd_reloc_status_type
228 sh_elf_reloc (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol_in
,
229 void *data
, asection
*input_section
, bfd
*output_bfd
,
230 char **error_message ATTRIBUTE_UNUSED
)
234 enum elf_sh_reloc_type r_type
;
235 bfd_vma addr
= reloc_entry
->address
;
236 bfd_byte
*hit_data
= addr
+ (bfd_byte
*) data
;
238 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
240 if (output_bfd
!= NULL
)
242 /* Partial linking--do nothing. */
243 reloc_entry
->address
+= input_section
->output_offset
;
247 /* Almost all relocs have to do with relaxing. If any work must be
248 done for them, it has been done in sh_relax_section. */
249 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
252 if (symbol_in
!= NULL
253 && bfd_is_und_section (symbol_in
->section
))
254 return bfd_reloc_undefined
;
256 /* PR 17512: file: 9891ca98. */
257 if ((addr
* bfd_octets_per_byte (abfd
, NULL
)
258 + bfd_get_reloc_size (reloc_entry
->howto
))
259 > bfd_get_section_limit_octets (abfd
, input_section
))
260 return bfd_reloc_outofrange
;
262 if (bfd_is_com_section (symbol_in
->section
))
265 sym_value
= (symbol_in
->value
+
266 symbol_in
->section
->output_section
->vma
+
267 symbol_in
->section
->output_offset
);
272 insn
= bfd_get_32 (abfd
, hit_data
);
273 insn
+= sym_value
+ reloc_entry
->addend
;
274 bfd_put_32 (abfd
, (bfd_vma
) insn
, hit_data
);
277 insn
= bfd_get_16 (abfd
, hit_data
);
278 sym_value
+= reloc_entry
->addend
;
279 sym_value
-= (input_section
->output_section
->vma
280 + input_section
->output_offset
283 sym_value
+= (insn
& 0xfff) << 1;
286 insn
= (insn
& 0xf000) | (sym_value
& 0xfff);
287 bfd_put_16 (abfd
, (bfd_vma
) insn
, hit_data
);
288 if (sym_value
< (bfd_vma
) -0x1000 || sym_value
>= 0x1000)
289 return bfd_reloc_overflow
;
299 /* This function is used for relocs which are only used for relaxing,
300 which the linker should otherwise ignore. */
302 static bfd_reloc_status_type
303 sh_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
304 asymbol
*symbol ATTRIBUTE_UNUSED
,
305 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
307 char **error_message ATTRIBUTE_UNUSED
)
309 if (output_bfd
!= NULL
)
310 reloc_entry
->address
+= input_section
->output_offset
;
314 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
318 bfd_reloc_code_real_type bfd_reloc_val
;
319 unsigned char elf_reloc_val
;
322 /* An array mapping BFD reloc codes to SH ELF relocs. */
324 static const struct elf_reloc_map sh_reloc_map
[] =
326 { BFD_RELOC_NONE
, R_SH_NONE
},
327 { BFD_RELOC_32
, R_SH_DIR32
},
328 { BFD_RELOC_16
, R_SH_DIR16
},
329 { BFD_RELOC_8
, R_SH_DIR8
},
330 { BFD_RELOC_CTOR
, R_SH_DIR32
},
331 { BFD_RELOC_32_PCREL
, R_SH_REL32
},
332 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_DIR8WPN
},
333 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_IND12W
},
334 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_DIR8WPZ
},
335 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_DIR8WPL
},
336 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
337 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
338 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
339 { BFD_RELOC_SH_USES
, R_SH_USES
},
340 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
341 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
342 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
343 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
344 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
345 { BFD_RELOC_VTABLE_INHERIT
, R_SH_GNU_VTINHERIT
},
346 { BFD_RELOC_VTABLE_ENTRY
, R_SH_GNU_VTENTRY
},
347 { BFD_RELOC_SH_LOOP_START
, R_SH_LOOP_START
},
348 { BFD_RELOC_SH_LOOP_END
, R_SH_LOOP_END
},
349 { BFD_RELOC_SH_TLS_GD_32
, R_SH_TLS_GD_32
},
350 { BFD_RELOC_SH_TLS_LD_32
, R_SH_TLS_LD_32
},
351 { BFD_RELOC_SH_TLS_LDO_32
, R_SH_TLS_LDO_32
},
352 { BFD_RELOC_SH_TLS_IE_32
, R_SH_TLS_IE_32
},
353 { BFD_RELOC_SH_TLS_LE_32
, R_SH_TLS_LE_32
},
354 { BFD_RELOC_SH_TLS_DTPMOD32
, R_SH_TLS_DTPMOD32
},
355 { BFD_RELOC_SH_TLS_DTPOFF32
, R_SH_TLS_DTPOFF32
},
356 { BFD_RELOC_SH_TLS_TPOFF32
, R_SH_TLS_TPOFF32
},
357 { BFD_RELOC_32_GOT_PCREL
, R_SH_GOT32
},
358 { BFD_RELOC_32_PLT_PCREL
, R_SH_PLT32
},
359 { BFD_RELOC_SH_COPY
, R_SH_COPY
},
360 { BFD_RELOC_SH_GLOB_DAT
, R_SH_GLOB_DAT
},
361 { BFD_RELOC_SH_JMP_SLOT
, R_SH_JMP_SLOT
},
362 { BFD_RELOC_SH_RELATIVE
, R_SH_RELATIVE
},
363 { BFD_RELOC_32_GOTOFF
, R_SH_GOTOFF
},
364 { BFD_RELOC_SH_GOTPC
, R_SH_GOTPC
},
365 { BFD_RELOC_SH_GOTPLT32
, R_SH_GOTPLT32
},
366 { BFD_RELOC_SH_GOT20
, R_SH_GOT20
},
367 { BFD_RELOC_SH_GOTOFF20
, R_SH_GOTOFF20
},
368 { BFD_RELOC_SH_GOTFUNCDESC
, R_SH_GOTFUNCDESC
},
369 { BFD_RELOC_SH_GOTFUNCDESC20
, R_SH_GOTFUNCDESC20
},
370 { BFD_RELOC_SH_GOTOFFFUNCDESC
, R_SH_GOTOFFFUNCDESC
},
371 { BFD_RELOC_SH_GOTOFFFUNCDESC20
, R_SH_GOTOFFFUNCDESC20
},
372 { BFD_RELOC_SH_FUNCDESC
, R_SH_FUNCDESC
},
375 /* Given a BFD reloc code, return the howto structure for the
376 corresponding SH ELF reloc. */
378 static reloc_howto_type
*
379 sh_elf_reloc_type_lookup (bfd
*abfd
, bfd_reloc_code_real_type code
)
383 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
385 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
386 return get_howto_table (abfd
) + (int) sh_reloc_map
[i
].elf_reloc_val
;
392 static reloc_howto_type
*
393 sh_elf_reloc_name_lookup (bfd
*abfd
, const char *r_name
)
397 if (vxworks_object_p (abfd
))
400 i
< (sizeof (sh_vxworks_howto_table
)
401 / sizeof (sh_vxworks_howto_table
[0]));
403 if (sh_vxworks_howto_table
[i
].name
!= NULL
404 && strcasecmp (sh_vxworks_howto_table
[i
].name
, r_name
) == 0)
405 return &sh_vxworks_howto_table
[i
];
410 i
< (sizeof (sh_elf_howto_table
)
411 / sizeof (sh_elf_howto_table
[0]));
413 if (sh_elf_howto_table
[i
].name
!= NULL
414 && strcasecmp (sh_elf_howto_table
[i
].name
, r_name
) == 0)
415 return &sh_elf_howto_table
[i
];
421 /* Given an ELF reloc, fill in the howto field of a relent. */
424 sh_elf_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
, Elf_Internal_Rela
*dst
)
428 r
= ELF32_R_TYPE (dst
->r_info
);
431 || (r
>= R_SH_FIRST_INVALID_RELOC
&& r
<= R_SH_LAST_INVALID_RELOC
)
432 || (r
>= R_SH_FIRST_INVALID_RELOC_2
&& r
<= R_SH_LAST_INVALID_RELOC_2
)
433 || (r
>= R_SH_FIRST_INVALID_RELOC_3
&& r
<= R_SH_LAST_INVALID_RELOC_3
)
434 || (r
>= R_SH_FIRST_INVALID_RELOC_4
&& r
<= R_SH_LAST_INVALID_RELOC_4
)
435 || (r
>= R_SH_FIRST_INVALID_RELOC_5
&& r
<= R_SH_LAST_INVALID_RELOC_5
)
436 || (r
>= R_SH_FIRST_INVALID_RELOC_6
&& r
<= R_SH_LAST_INVALID_RELOC_6
))
438 /* xgettext:c-format */
439 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
441 bfd_set_error (bfd_error_bad_value
);
445 cache_ptr
->howto
= get_howto_table (abfd
) + r
;
449 /* This function handles relaxing for SH ELF. See the corresponding
450 function in coff-sh.c for a description of what this does. FIXME:
451 There is a lot of duplication here between this code and the COFF
452 specific code. The format of relocs and symbols is wound deeply
453 into this code, but it would still be better if the duplication
454 could be eliminated somehow. Note in particular that although both
455 functions use symbols like R_SH_CODE, those symbols have different
456 values; in coff-sh.c they come from include/coff/sh.h, whereas here
457 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
460 sh_elf_relax_section (bfd
*abfd
, asection
*sec
,
461 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
463 Elf_Internal_Shdr
*symtab_hdr
;
464 Elf_Internal_Rela
*internal_relocs
;
465 bfd_boolean have_code
;
466 Elf_Internal_Rela
*irel
, *irelend
;
467 bfd_byte
*contents
= NULL
;
468 Elf_Internal_Sym
*isymbuf
= NULL
;
472 if (bfd_link_relocatable (link_info
)
473 || (sec
->flags
& SEC_RELOC
) == 0
474 || sec
->reloc_count
== 0)
477 symtab_hdr
= &elf_symtab_hdr (abfd
);
479 internal_relocs
= (_bfd_elf_link_read_relocs
480 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
481 link_info
->keep_memory
));
482 if (internal_relocs
== NULL
)
487 irelend
= internal_relocs
+ sec
->reloc_count
;
488 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
490 bfd_vma laddr
, paddr
, symval
;
492 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
495 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
498 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
501 /* Get the section contents. */
502 if (contents
== NULL
)
504 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
505 contents
= elf_section_data (sec
)->this_hdr
.contents
;
508 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
513 /* The r_addend field of the R_SH_USES reloc will point us to
514 the register load. The 4 is because the r_addend field is
515 computed as though it were a jump offset, which are based
516 from 4 bytes after the jump instruction. */
517 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
518 if (laddr
>= sec
->size
)
520 /* xgettext:c-format */
522 (_("%pB: %#" PRIx64
": warning: bad R_SH_USES offset"),
523 abfd
, (uint64_t) irel
->r_offset
);
526 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
528 /* If the instruction is not mov.l NN,rN, we don't know what to
530 if ((insn
& 0xf000) != 0xd000)
533 /* xgettext:c-format */
534 (_("%pB: %#" PRIx64
": warning: "
535 "R_SH_USES points to unrecognized insn 0x%x"),
536 abfd
, (uint64_t) irel
->r_offset
, insn
);
540 /* Get the address from which the register is being loaded. The
541 displacement in the mov.l instruction is quadrupled. It is a
542 displacement from four bytes after the movl instruction, but,
543 before adding in the PC address, two least significant bits
544 of the PC are cleared. We assume that the section is aligned
545 on a four byte boundary. */
548 paddr
+= (laddr
+ 4) &~ (bfd_vma
) 3;
549 if (paddr
>= sec
->size
)
552 /* xgettext:c-format */
553 (_("%pB: %#" PRIx64
": warning: bad R_SH_USES load offset"),
554 abfd
, (uint64_t) irel
->r_offset
);
558 /* Get the reloc for the address from which the register is
559 being loaded. This reloc will tell us which function is
560 actually being called. */
561 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
562 if (irelfn
->r_offset
== paddr
563 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
565 if (irelfn
>= irelend
)
568 /* xgettext:c-format */
569 (_("%pB: %#" PRIx64
": warning: could not find expected reloc"),
570 abfd
, (uint64_t) paddr
);
574 /* Read this BFD's symbols if we haven't done so already. */
575 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
577 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
579 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
580 symtab_hdr
->sh_info
, 0,
586 /* Get the value of the symbol referred to by the reloc. */
587 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
589 /* A local symbol. */
590 Elf_Internal_Sym
*isym
;
592 isym
= isymbuf
+ ELF32_R_SYM (irelfn
->r_info
);
594 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd
, sec
))
597 /* xgettext:c-format */
598 (_("%pB: %#" PRIx64
": warning: symbol in unexpected section"),
599 abfd
, (uint64_t) paddr
);
603 symval
= (isym
->st_value
604 + sec
->output_section
->vma
605 + sec
->output_offset
);
610 struct elf_link_hash_entry
*h
;
612 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
613 h
= elf_sym_hashes (abfd
)[indx
];
614 BFD_ASSERT (h
!= NULL
);
615 if (h
->root
.type
!= bfd_link_hash_defined
616 && h
->root
.type
!= bfd_link_hash_defweak
)
618 /* This appears to be a reference to an undefined
619 symbol. Just ignore it--it will be caught by the
620 regular reloc processing. */
624 symval
= (h
->root
.u
.def
.value
625 + h
->root
.u
.def
.section
->output_section
->vma
626 + h
->root
.u
.def
.section
->output_offset
);
629 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
630 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
632 symval
+= irelfn
->r_addend
;
634 /* See if this function call can be shortened. */
637 + sec
->output_section
->vma
640 /* A branch to an address beyond ours might be increased by an
641 .align that doesn't move when bytes behind us are deleted.
642 So, we add some slop in this calculation to allow for
644 if (foff
< -0x1000 || foff
>= 0x1000 - 8)
646 /* After all that work, we can't shorten this function call. */
650 /* Shorten the function call. */
652 /* For simplicity of coding, we are going to modify the section
653 contents, the section relocs, and the BFD symbol table. We
654 must tell the rest of the code not to free up this
655 information. It would be possible to instead create a table
656 of changes which have to be made, as is done in coff-mips.c;
657 that would be more work, but would require less memory when
658 the linker is run. */
660 elf_section_data (sec
)->relocs
= internal_relocs
;
661 elf_section_data (sec
)->this_hdr
.contents
= contents
;
662 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
664 /* Replace the jmp/jsr with a bra/bsr. */
666 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
667 replace the jmp/jsr with a bra/bsr. */
668 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
669 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
670 here, but that only checks if the symbol is an external symbol,
671 not if the symbol is in a different section. Besides, we need
672 a consistent meaning for the relocation, so we just assume here that
673 the value of the symbol is not available. */
675 /* We can't fully resolve this yet, because the external
676 symbol value may be changed by future relaxing. We let
677 the final link phase handle it. */
678 if (bfd_get_16 (abfd
, contents
+ irel
->r_offset
) & 0x0020)
679 bfd_put_16 (abfd
, (bfd_vma
) 0xa000, contents
+ irel
->r_offset
);
681 bfd_put_16 (abfd
, (bfd_vma
) 0xb000, contents
+ irel
->r_offset
);
685 /* When we calculated the symbol "value" we had an offset in the
686 DIR32's word in memory (we read and add it above). However,
687 the jsr we create does NOT have this offset encoded, so we
688 have to add it to the addend to preserve it. */
689 irel
->r_addend
+= bfd_get_32 (abfd
, contents
+ paddr
);
691 /* See if there is another R_SH_USES reloc referring to the same
693 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
694 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
695 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
697 if (irelscan
< irelend
)
699 /* Some other function call depends upon this register load,
700 and we have not yet converted that function call.
701 Indeed, we may never be able to convert it. There is
702 nothing else we can do at this point. */
706 /* Look for a R_SH_COUNT reloc on the location where the
707 function address is stored. Do this before deleting any
708 bytes, to avoid confusion about the address. */
709 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
710 if (irelcount
->r_offset
== paddr
711 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
714 /* Delete the register load. */
715 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
718 /* That will change things, so, just in case it permits some
719 other function call to come within range, we should relax
720 again. Note that this is not required, and it may be slow. */
723 /* Now check whether we got a COUNT reloc. */
724 if (irelcount
>= irelend
)
727 /* xgettext:c-format */
728 (_("%pB: %#" PRIx64
": warning: "
729 "could not find expected COUNT reloc"),
730 abfd
, (uint64_t) paddr
);
734 /* The number of uses is stored in the r_addend field. We've
736 if (irelcount
->r_addend
== 0)
738 /* xgettext:c-format */
739 _bfd_error_handler (_("%pB: %#" PRIx64
": warning: bad count"),
740 abfd
, (uint64_t) paddr
);
744 --irelcount
->r_addend
;
746 /* If there are no more uses, we can delete the address. Reload
747 the address from irelfn, in case it was changed by the
748 previous call to sh_elf_relax_delete_bytes. */
749 if (irelcount
->r_addend
== 0)
751 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
755 /* We've done all we can with that function call. */
758 /* Look for load and store instructions that we can align on four
760 if ((elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
) != EF_SH4
765 /* Get the section contents. */
766 if (contents
== NULL
)
768 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
769 contents
= elf_section_data (sec
)->this_hdr
.contents
;
772 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
777 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
783 elf_section_data (sec
)->relocs
= internal_relocs
;
784 elf_section_data (sec
)->this_hdr
.contents
= contents
;
785 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
790 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
792 if (! link_info
->keep_memory
)
796 /* Cache the symbols for elf_link_input_bfd. */
797 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
802 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
804 if (! link_info
->keep_memory
)
808 /* Cache the section contents for elf_link_input_bfd. */
809 elf_section_data (sec
)->this_hdr
.contents
= contents
;
813 if (internal_relocs
!= NULL
814 && elf_section_data (sec
)->relocs
!= internal_relocs
)
815 free (internal_relocs
);
821 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
824 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
826 if (internal_relocs
!= NULL
827 && elf_section_data (sec
)->relocs
!= internal_relocs
)
828 free (internal_relocs
);
833 /* Delete some bytes from a section while relaxing. FIXME: There is a
834 lot of duplication between this function and sh_relax_delete_bytes
838 sh_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
,
841 Elf_Internal_Shdr
*symtab_hdr
;
842 unsigned int sec_shndx
;
844 Elf_Internal_Rela
*irel
, *irelend
;
845 Elf_Internal_Rela
*irelalign
;
847 Elf_Internal_Sym
*isymbuf
, *isym
, *isymend
;
848 struct elf_link_hash_entry
**sym_hashes
;
849 struct elf_link_hash_entry
**end_hashes
;
850 unsigned int symcount
;
853 symtab_hdr
= &elf_symtab_hdr (abfd
);
854 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
856 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
858 contents
= elf_section_data (sec
)->this_hdr
.contents
;
860 /* The deletion must stop at the next ALIGN reloc for an alignment
861 power larger than the number of bytes we are deleting. */
866 irel
= elf_section_data (sec
)->relocs
;
867 irelend
= irel
+ sec
->reloc_count
;
868 for (; irel
< irelend
; irel
++)
870 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
871 && irel
->r_offset
> addr
872 && count
< (1 << irel
->r_addend
))
875 toaddr
= irel
->r_offset
;
880 /* Actually delete the bytes. */
881 memmove (contents
+ addr
, contents
+ addr
+ count
,
882 (size_t) (toaddr
- addr
- count
));
883 if (irelalign
== NULL
)
889 #define NOP_OPCODE (0x0009)
891 BFD_ASSERT ((count
& 1) == 0);
892 for (i
= 0; i
< count
; i
+= 2)
893 bfd_put_16 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
896 /* Adjust all the relocs. */
897 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
899 bfd_vma nraddr
, stop
;
902 int off
, adjust
, oinsn
;
903 bfd_signed_vma voff
= 0;
904 bfd_boolean overflow
;
906 /* Get the new reloc address. */
907 nraddr
= irel
->r_offset
;
908 if ((irel
->r_offset
> addr
909 && irel
->r_offset
< toaddr
)
910 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
911 && irel
->r_offset
== toaddr
))
914 /* See if this reloc was for the bytes we have deleted, in which
915 case we no longer care about it. Don't delete relocs which
916 represent addresses, though. */
917 if (irel
->r_offset
>= addr
918 && irel
->r_offset
< addr
+ count
919 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
920 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
921 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
922 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
923 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
926 /* If this is a PC relative reloc, see if the range it covers
927 includes the bytes we have deleted. */
928 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
937 start
= irel
->r_offset
;
938 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
942 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
949 /* If this reloc is against a symbol defined in this
950 section, and the symbol will not be adjusted below, we
951 must check the addend to see it will put the value in
952 range to be adjusted, and hence must be changed. */
953 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
955 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
956 if (isym
->st_shndx
== sec_shndx
957 && (isym
->st_value
<= addr
958 || isym
->st_value
>= toaddr
))
962 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
964 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
965 val
+= isym
->st_value
;
966 if (val
> addr
&& val
< toaddr
)
967 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
971 val
= isym
->st_value
+ irel
->r_addend
;
972 if (val
> addr
&& val
< toaddr
)
973 irel
->r_addend
-= count
;
984 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
991 /* This has been made by previous relaxation. Since the
992 relocation will be against an external symbol, the
993 final relocation will just do the right thing. */
1000 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1002 /* The addend will be against the section symbol, thus
1003 for adjusting the addend, the relevant start is the
1004 start of the section.
1005 N.B. If we want to abandon in-place changes here and
1006 test directly using symbol + addend, we have to take into
1007 account that the addend has already been adjusted by -4. */
1008 if (stop
> addr
&& stop
< toaddr
)
1009 irel
->r_addend
-= count
;
1015 stop
= start
+ 4 + off
* 2;
1020 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
1026 /* These relocs types represent
1028 The r_addend field holds the difference between the reloc
1029 address and L1. That is the start of the reloc, and
1030 adding in the contents gives us the top. We must adjust
1031 both the r_offset field and the section contents.
1032 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1033 and the elf bfd r_offset is called r_vaddr. */
1035 stop
= irel
->r_offset
;
1036 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
1040 && (stop
<= addr
|| stop
>= toaddr
))
1041 irel
->r_addend
+= count
;
1042 else if (stop
> addr
1044 && (start
<= addr
|| start
>= toaddr
))
1045 irel
->r_addend
-= count
;
1047 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1048 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1049 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1050 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1052 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1053 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1058 start
= irel
->r_offset
;
1059 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1060 + (long) irel
->r_addend
1067 && (stop
<= addr
|| stop
>= toaddr
))
1069 else if (stop
> addr
1071 && (start
<= addr
|| start
>= toaddr
))
1080 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1089 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1091 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1096 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1098 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1102 BFD_ASSERT (adjust
== count
|| count
>= 4);
1107 if ((irel
->r_offset
& 3) == 0)
1110 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1112 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1117 if (voff
< 0 || voff
>= 0xff)
1119 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1124 if (voff
< - 0x8000 || voff
>= 0x8000)
1126 bfd_put_signed_16 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1131 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1135 irel
->r_addend
+= adjust
;
1142 /* xgettext:c-format */
1143 (_("%pB: %#" PRIx64
": fatal: reloc overflow while relaxing"),
1144 abfd
, (uint64_t) irel
->r_offset
);
1145 bfd_set_error (bfd_error_bad_value
);
1150 irel
->r_offset
= nraddr
;
1153 /* Look through all the other sections. If there contain any IMM32
1154 relocs against internal symbols which we are not going to adjust
1155 below, we may need to adjust the addends. */
1156 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1158 Elf_Internal_Rela
*internal_relocs
;
1159 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1160 bfd_byte
*ocontents
;
1163 || (o
->flags
& SEC_RELOC
) == 0
1164 || o
->reloc_count
== 0)
1167 /* We always cache the relocs. Perhaps, if info->keep_memory is
1168 FALSE, we should free them, if we are permitted to, when we
1169 leave sh_coff_relax_section. */
1170 internal_relocs
= (_bfd_elf_link_read_relocs
1171 (abfd
, o
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
1172 if (internal_relocs
== NULL
)
1176 irelscanend
= internal_relocs
+ o
->reloc_count
;
1177 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1179 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1180 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1182 bfd_vma start
, stop
;
1183 bfd_signed_vma voff
;
1185 if (ocontents
== NULL
)
1187 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1188 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1191 /* We always cache the section contents.
1192 Perhaps, if info->keep_memory is FALSE, we
1193 should free them, if we are permitted to,
1194 when we leave sh_coff_relax_section. */
1195 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1197 if (ocontents
!= NULL
)
1202 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1206 stop
= irelscan
->r_offset
;
1208 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1210 /* STOP is in a different section, so it won't change. */
1211 if (start
> addr
&& start
< toaddr
)
1212 irelscan
->r_addend
+= count
;
1214 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1215 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1219 && (stop
<= addr
|| stop
>= toaddr
))
1220 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
+ count
,
1221 ocontents
+ irelscan
->r_offset
);
1222 else if (stop
> addr
1224 && (start
<= addr
|| start
>= toaddr
))
1225 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
- count
,
1226 ocontents
+ irelscan
->r_offset
);
1229 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1232 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1236 isym
= isymbuf
+ ELF32_R_SYM (irelscan
->r_info
);
1237 if (isym
->st_shndx
== sec_shndx
1238 && (isym
->st_value
<= addr
1239 || isym
->st_value
>= toaddr
))
1243 if (ocontents
== NULL
)
1245 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1246 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1249 /* We always cache the section contents.
1250 Perhaps, if info->keep_memory is FALSE, we
1251 should free them, if we are permitted to,
1252 when we leave sh_coff_relax_section. */
1253 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1255 if (ocontents
!= NULL
)
1260 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1264 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1265 val
+= isym
->st_value
;
1266 if (val
> addr
&& val
< toaddr
)
1267 bfd_put_32 (abfd
, val
- count
,
1268 ocontents
+ irelscan
->r_offset
);
1273 /* Adjust the local symbols defined in this section. */
1274 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1275 for (isym
= isymbuf
; isym
< isymend
; isym
++)
1277 if (isym
->st_shndx
== sec_shndx
1278 && isym
->st_value
> addr
1279 && isym
->st_value
< toaddr
)
1280 isym
->st_value
-= count
;
1283 /* Now adjust the global symbols defined in this section. */
1284 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1285 - symtab_hdr
->sh_info
);
1286 sym_hashes
= elf_sym_hashes (abfd
);
1287 end_hashes
= sym_hashes
+ symcount
;
1288 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1290 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1291 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1292 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1293 && sym_hash
->root
.u
.def
.section
== sec
1294 && sym_hash
->root
.u
.def
.value
> addr
1295 && sym_hash
->root
.u
.def
.value
< toaddr
)
1297 sym_hash
->root
.u
.def
.value
-= count
;
1301 /* See if we can move the ALIGN reloc forward. We have adjusted
1302 r_offset for it already. */
1303 if (irelalign
!= NULL
)
1305 bfd_vma alignto
, alignaddr
;
1307 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1308 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1309 1 << irelalign
->r_addend
);
1310 if (alignto
!= alignaddr
)
1312 /* Tail recursion. */
1313 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1314 (int) (alignto
- alignaddr
));
1321 /* Look for loads and stores which we can align to four byte
1322 boundaries. This is like sh_align_loads in coff-sh.c. */
1325 sh_elf_align_loads (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1326 Elf_Internal_Rela
*internal_relocs
,
1327 bfd_byte
*contents ATTRIBUTE_UNUSED
,
1328 bfd_boolean
*pswapped
)
1330 Elf_Internal_Rela
*irel
, *irelend
;
1331 bfd_vma
*labels
= NULL
;
1332 bfd_vma
*label
, *label_end
;
1337 irelend
= internal_relocs
+ sec
->reloc_count
;
1339 /* Get all the addresses with labels on them. */
1340 amt
= sec
->reloc_count
;
1341 amt
*= sizeof (bfd_vma
);
1342 labels
= (bfd_vma
*) bfd_malloc (amt
);
1346 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1348 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1350 *label_end
= irel
->r_offset
;
1355 /* Note that the assembler currently always outputs relocs in
1356 address order. If that ever changes, this code will need to sort
1357 the label values and the relocs. */
1361 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1363 bfd_vma start
, stop
;
1365 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1368 start
= irel
->r_offset
;
1370 for (irel
++; irel
< irelend
; irel
++)
1371 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1374 stop
= irel
->r_offset
;
1378 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1379 internal_relocs
, &label
,
1380 label_end
, start
, stop
, pswapped
))
1394 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1397 sh_elf_swap_insns (bfd
*abfd
, asection
*sec
, void *relocs
,
1398 bfd_byte
*contents
, bfd_vma addr
)
1400 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1401 unsigned short i1
, i2
;
1402 Elf_Internal_Rela
*irel
, *irelend
;
1404 /* Swap the instructions themselves. */
1405 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1406 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1407 bfd_put_16 (abfd
, (bfd_vma
) i2
, contents
+ addr
);
1408 bfd_put_16 (abfd
, (bfd_vma
) i1
, contents
+ addr
+ 2);
1410 /* Adjust all reloc addresses. */
1411 irelend
= internal_relocs
+ sec
->reloc_count
;
1412 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1414 enum elf_sh_reloc_type type
;
1417 /* There are a few special types of relocs that we don't want to
1418 adjust. These relocs do not apply to the instruction itself,
1419 but are only associated with the address. */
1420 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1421 if (type
== R_SH_ALIGN
1422 || type
== R_SH_CODE
1423 || type
== R_SH_DATA
1424 || type
== R_SH_LABEL
)
1427 /* If an R_SH_USES reloc points to one of the addresses being
1428 swapped, we must adjust it. It would be incorrect to do this
1429 for a jump, though, since we want to execute both
1430 instructions after the jump. (We have avoided swapping
1431 around a label, so the jump will not wind up executing an
1432 instruction it shouldn't). */
1433 if (type
== R_SH_USES
)
1437 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
1439 irel
->r_offset
+= 2;
1440 else if (off
== addr
+ 2)
1441 irel
->r_offset
-= 2;
1444 if (irel
->r_offset
== addr
)
1446 irel
->r_offset
+= 2;
1449 else if (irel
->r_offset
== addr
+ 2)
1451 irel
->r_offset
-= 2;
1460 unsigned short insn
, oinsn
;
1461 bfd_boolean overflow
;
1463 loc
= contents
+ irel
->r_offset
;
1472 insn
= bfd_get_16 (abfd
, loc
);
1475 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1477 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1481 insn
= bfd_get_16 (abfd
, loc
);
1484 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1486 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1490 /* This reloc ignores the least significant 3 bits of
1491 the program counter before adding in the offset.
1492 This means that if ADDR is at an even address, the
1493 swap will not affect the offset. If ADDR is an at an
1494 odd address, then the instruction will be crossing a
1495 four byte boundary, and must be adjusted. */
1496 if ((addr
& 3) != 0)
1498 insn
= bfd_get_16 (abfd
, loc
);
1501 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1503 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1512 /* xgettext:c-format */
1513 (_("%pB: %#" PRIx64
": fatal: reloc overflow while relaxing"),
1514 abfd
, (uint64_t) irel
->r_offset
);
1515 bfd_set_error (bfd_error_bad_value
);
1524 /* Describes one of the various PLT styles. */
1526 struct elf_sh_plt_info
1528 /* The template for the first PLT entry, or NULL if there is no special
1530 const bfd_byte
*plt0_entry
;
1532 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1533 bfd_vma plt0_entry_size
;
1535 /* Index I is the offset into PLT0_ENTRY of a pointer to
1536 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1537 if there is no such pointer. */
1538 bfd_vma plt0_got_fields
[3];
1540 /* The template for a symbol's PLT entry. */
1541 const bfd_byte
*symbol_entry
;
1543 /* The size of SYMBOL_ENTRY in bytes. */
1544 bfd_vma symbol_entry_size
;
1546 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1547 on all targets. The comments by each member indicate the value
1548 that the field must hold. */
1550 bfd_vma got_entry
; /* the address of the symbol's .got.plt entry */
1551 bfd_vma plt
; /* .plt (or a branch to .plt on VxWorks) */
1552 bfd_vma reloc_offset
; /* the offset of the symbol's JMP_SLOT reloc */
1553 bfd_boolean got20
; /* TRUE if got_entry points to a movi20
1554 instruction (instead of a constant pool
1558 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1559 bfd_vma symbol_resolve_offset
;
1561 /* A different PLT layout which can be used for the first
1562 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1564 const struct elf_sh_plt_info
*short_plt
;
1567 /* The size in bytes of an entry in the procedure linkage table. */
1569 #define ELF_PLT_ENTRY_SIZE 28
1571 /* First entry in an absolute procedure linkage table look like this. */
1573 /* Note - this code has been "optimised" not to use r2. r2 is used by
1574 GCC to return the address of large structures, so it should not be
1575 corrupted here. This does mean however, that this PLT does not conform
1576 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1577 and r2 contains the GOT id. This version stores the GOT id in r0 and
1578 ignores the type. Loaders can easily detect this difference however,
1579 since the type will always be 0 or 8, and the GOT ids will always be
1580 greater than or equal to 12. */
1581 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1583 0xd0, 0x05, /* mov.l 2f,r0 */
1584 0x60, 0x02, /* mov.l @r0,r0 */
1585 0x2f, 0x06, /* mov.l r0,@-r15 */
1586 0xd0, 0x03, /* mov.l 1f,r0 */
1587 0x60, 0x02, /* mov.l @r0,r0 */
1588 0x40, 0x2b, /* jmp @r0 */
1589 0x60, 0xf6, /* mov.l @r15+,r0 */
1590 0x00, 0x09, /* nop */
1591 0x00, 0x09, /* nop */
1592 0x00, 0x09, /* nop */
1593 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1594 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1597 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1599 0x05, 0xd0, /* mov.l 2f,r0 */
1600 0x02, 0x60, /* mov.l @r0,r0 */
1601 0x06, 0x2f, /* mov.l r0,@-r15 */
1602 0x03, 0xd0, /* mov.l 1f,r0 */
1603 0x02, 0x60, /* mov.l @r0,r0 */
1604 0x2b, 0x40, /* jmp @r0 */
1605 0xf6, 0x60, /* mov.l @r15+,r0 */
1606 0x09, 0x00, /* nop */
1607 0x09, 0x00, /* nop */
1608 0x09, 0x00, /* nop */
1609 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1610 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1613 /* Sebsequent entries in an absolute procedure linkage table look like
1616 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1618 0xd0, 0x04, /* mov.l 1f,r0 */
1619 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1620 0xd1, 0x02, /* mov.l 0f,r1 */
1621 0x40, 0x2b, /* jmp @r0 */
1622 0x60, 0x13, /* mov r1,r0 */
1623 0xd1, 0x03, /* mov.l 2f,r1 */
1624 0x40, 0x2b, /* jmp @r0 */
1625 0x00, 0x09, /* nop */
1626 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1627 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1628 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1631 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1633 0x04, 0xd0, /* mov.l 1f,r0 */
1634 0x02, 0x60, /* mov.l @r0,r0 */
1635 0x02, 0xd1, /* mov.l 0f,r1 */
1636 0x2b, 0x40, /* jmp @r0 */
1637 0x13, 0x60, /* mov r1,r0 */
1638 0x03, 0xd1, /* mov.l 2f,r1 */
1639 0x2b, 0x40, /* jmp @r0 */
1640 0x09, 0x00, /* nop */
1641 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1642 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1643 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1646 /* Entries in a PIC procedure linkage table look like this. */
1648 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1650 0xd0, 0x04, /* mov.l 1f,r0 */
1651 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1652 0x40, 0x2b, /* jmp @r0 */
1653 0x00, 0x09, /* nop */
1654 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1655 0xd1, 0x03, /* mov.l 2f,r1 */
1656 0x40, 0x2b, /* jmp @r0 */
1657 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1658 0x00, 0x09, /* nop */
1659 0x00, 0x09, /* nop */
1660 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1661 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1664 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1666 0x04, 0xd0, /* mov.l 1f,r0 */
1667 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1668 0x2b, 0x40, /* jmp @r0 */
1669 0x09, 0x00, /* nop */
1670 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1671 0x03, 0xd1, /* mov.l 2f,r1 */
1672 0x2b, 0x40, /* jmp @r0 */
1673 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1674 0x09, 0x00, /* nop */
1675 0x09, 0x00, /* nop */
1676 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1677 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1680 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1683 /* Big-endian non-PIC. */
1684 elf_sh_plt0_entry_be
,
1686 { MINUS_ONE
, 24, 20 },
1687 elf_sh_plt_entry_be
,
1689 { 20, 16, 24, FALSE
},
1694 /* Little-endian non-PIC. */
1695 elf_sh_plt0_entry_le
,
1697 { MINUS_ONE
, 24, 20 },
1698 elf_sh_plt_entry_le
,
1700 { 20, 16, 24, FALSE
},
1707 /* Big-endian PIC. */
1708 elf_sh_plt0_entry_be
,
1710 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1711 elf_sh_pic_plt_entry_be
,
1713 { 20, MINUS_ONE
, 24, FALSE
},
1718 /* Little-endian PIC. */
1719 elf_sh_plt0_entry_le
,
1721 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1722 elf_sh_pic_plt_entry_le
,
1724 { 20, MINUS_ONE
, 24, FALSE
},
1731 #define VXWORKS_PLT_HEADER_SIZE 12
1732 #define VXWORKS_PLT_ENTRY_SIZE 24
1734 static const bfd_byte vxworks_sh_plt0_entry_be
[VXWORKS_PLT_HEADER_SIZE
] =
1736 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1737 0x61, 0x12, /* mov.l @r1,r1 */
1738 0x41, 0x2b, /* jmp @r1 */
1739 0x00, 0x09, /* nop */
1740 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1743 static const bfd_byte vxworks_sh_plt0_entry_le
[VXWORKS_PLT_HEADER_SIZE
] =
1745 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1746 0x12, 0x61, /* mov.l @r1,r1 */
1747 0x2b, 0x41, /* jmp @r1 */
1748 0x09, 0x00, /* nop */
1749 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1752 static const bfd_byte vxworks_sh_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1754 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1755 0x60, 0x02, /* mov.l @r0,r0 */
1756 0x40, 0x2b, /* jmp @r0 */
1757 0x00, 0x09, /* nop */
1758 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1759 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1760 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1761 0x00, 0x09, /* nop */
1762 0x00, 0x09, /* nop */
1763 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1766 static const bfd_byte vxworks_sh_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1768 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1769 0x02, 0x60, /* mov.l @r0,r0 */
1770 0x2b, 0x40, /* jmp @r0 */
1771 0x09, 0x00, /* nop */
1772 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1773 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1774 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1775 0x09, 0x00, /* nop */
1776 0x09, 0x00, /* nop */
1777 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1780 static const bfd_byte vxworks_sh_pic_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1782 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1783 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1784 0x40, 0x2b, /* jmp @r0 */
1785 0x00, 0x09, /* nop */
1786 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1787 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1788 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1789 0x41, 0x2b, /* jmp @r1 */
1790 0x00, 0x09, /* nop */
1791 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1794 static const bfd_byte vxworks_sh_pic_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1796 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1797 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1798 0x2b, 0x40, /* jmp @r0 */
1799 0x09, 0x00, /* nop */
1800 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1801 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1802 0xc2, 0x51, /* mov.l @(8,r12),r1 */
1803 0x2b, 0x41, /* jmp @r1 */
1804 0x09, 0x00, /* nop */
1805 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1808 static const struct elf_sh_plt_info vxworks_sh_plts
[2][2] = {
1811 /* Big-endian non-PIC. */
1812 vxworks_sh_plt0_entry_be
,
1813 VXWORKS_PLT_HEADER_SIZE
,
1814 { MINUS_ONE
, MINUS_ONE
, 8 },
1815 vxworks_sh_plt_entry_be
,
1816 VXWORKS_PLT_ENTRY_SIZE
,
1817 { 8, 14, 20, FALSE
},
1822 /* Little-endian non-PIC. */
1823 vxworks_sh_plt0_entry_le
,
1824 VXWORKS_PLT_HEADER_SIZE
,
1825 { MINUS_ONE
, MINUS_ONE
, 8 },
1826 vxworks_sh_plt_entry_le
,
1827 VXWORKS_PLT_ENTRY_SIZE
,
1828 { 8, 14, 20, FALSE
},
1835 /* Big-endian PIC. */
1838 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1839 vxworks_sh_pic_plt_entry_be
,
1840 VXWORKS_PLT_ENTRY_SIZE
,
1841 { 8, MINUS_ONE
, 20, FALSE
},
1846 /* Little-endian PIC. */
1849 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1850 vxworks_sh_pic_plt_entry_le
,
1851 VXWORKS_PLT_ENTRY_SIZE
,
1852 { 8, MINUS_ONE
, 20, FALSE
},
1859 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
1860 binding are to omit the lazy binding stub when linking with -z now
1861 and to move lazy binding stubs into a separate region for better
1864 #define FDPIC_PLT_ENTRY_SIZE 28
1865 #define FDPIC_PLT_LAZY_OFFSET 20
1867 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
1868 duplicated if it is out of range, or which can be inlined. So
1869 right now it is always inlined, which wastes a word per stub. It
1870 might be easier to handle the duplication if we put the lazy
1871 stubs separately. */
1873 static const bfd_byte fdpic_sh_plt_entry_be
[FDPIC_PLT_ENTRY_SIZE
] =
1875 0xd0, 0x02, /* mov.l @(12,pc),r0 */
1876 0x01, 0xce, /* mov.l @(r0,r12),r1 */
1877 0x70, 0x04, /* add #4, r0 */
1878 0x41, 0x2b, /* jmp @r1 */
1879 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1880 0x00, 0x09, /* nop */
1881 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1882 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1883 0x60, 0xc2, /* mov.l @r12,r0 */
1884 0x40, 0x2b, /* jmp @r0 */
1885 0x53, 0xc1, /* mov.l @(4,r12),r3 */
1886 0x00, 0x09, /* nop */
1889 static const bfd_byte fdpic_sh_plt_entry_le
[FDPIC_PLT_ENTRY_SIZE
] =
1891 0x02, 0xd0, /* mov.l @(12,pc),r0 */
1892 0xce, 0x01, /* mov.l @(r0,r12),r1 */
1893 0x04, 0x70, /* add #4, r0 */
1894 0x2b, 0x41, /* jmp @r1 */
1895 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1896 0x09, 0x00, /* nop */
1897 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1898 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1899 0xc2, 0x60, /* mov.l @r12,r0 */
1900 0x2b, 0x40, /* jmp @r0 */
1901 0xc1, 0x53, /* mov.l @(4,r12),r3 */
1902 0x09, 0x00, /* nop */
1905 static const struct elf_sh_plt_info fdpic_sh_plts
[2] = {
1907 /* Big-endian PIC. */
1910 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1911 fdpic_sh_plt_entry_be
,
1912 FDPIC_PLT_ENTRY_SIZE
,
1913 { 12, MINUS_ONE
, 16, FALSE
},
1914 FDPIC_PLT_LAZY_OFFSET
,
1918 /* Little-endian PIC. */
1921 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1922 fdpic_sh_plt_entry_le
,
1923 FDPIC_PLT_ENTRY_SIZE
,
1924 { 12, MINUS_ONE
, 16, FALSE
},
1925 FDPIC_PLT_LAZY_OFFSET
,
1930 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
1931 entries for the first 64K slots. We use the normal FDPIC PLT entry
1932 past that point; we could also use movi20s, which might be faster,
1933 but would not be any smaller. */
1935 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
1936 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
1938 static const bfd_byte fdpic_sh2a_plt_entry_be
[FDPIC_SH2A_PLT_ENTRY_SIZE
] =
1940 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1941 0x01, 0xce, /* mov.l @(r0,r12),r1 */
1942 0x70, 0x04, /* add #4, r0 */
1943 0x41, 0x2b, /* jmp @r1 */
1944 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1945 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1946 0x60, 0xc2, /* mov.l @r12,r0 */
1947 0x40, 0x2b, /* jmp @r0 */
1948 0x53, 0xc1, /* mov.l @(4,r12),r3 */
1949 0x00, 0x09, /* nop */
1952 static const bfd_byte fdpic_sh2a_plt_entry_le
[FDPIC_SH2A_PLT_ENTRY_SIZE
] =
1954 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1955 0xce, 0x01, /* mov.l @(r0,r12),r1 */
1956 0x04, 0x70, /* add #4, r0 */
1957 0x2b, 0x41, /* jmp @r1 */
1958 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1959 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1960 0xc2, 0x60, /* mov.l @r12,r0 */
1961 0x2b, 0x40, /* jmp @r0 */
1962 0xc1, 0x53, /* mov.l @(4,r12),r3 */
1963 0x09, 0x00, /* nop */
1966 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be
= {
1967 /* Big-endian FDPIC, max index 64K. */
1970 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1971 fdpic_sh2a_plt_entry_be
,
1972 FDPIC_SH2A_PLT_ENTRY_SIZE
,
1973 { 0, MINUS_ONE
, 12, TRUE
},
1974 FDPIC_SH2A_PLT_LAZY_OFFSET
,
1978 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le
= {
1979 /* Little-endian FDPIC, max index 64K. */
1982 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1983 fdpic_sh2a_plt_entry_le
,
1984 FDPIC_SH2A_PLT_ENTRY_SIZE
,
1985 { 0, MINUS_ONE
, 12, TRUE
},
1986 FDPIC_SH2A_PLT_LAZY_OFFSET
,
1990 static const struct elf_sh_plt_info fdpic_sh2a_plts
[2] = {
1992 /* Big-endian PIC. */
1995 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1996 fdpic_sh_plt_entry_be
,
1997 FDPIC_PLT_ENTRY_SIZE
,
1998 { 12, MINUS_ONE
, 16, FALSE
},
1999 FDPIC_PLT_LAZY_OFFSET
,
2000 &fdpic_sh2a_short_plt_be
2003 /* Little-endian PIC. */
2006 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2007 fdpic_sh_plt_entry_le
,
2008 FDPIC_PLT_ENTRY_SIZE
,
2009 { 12, MINUS_ONE
, 16, FALSE
},
2010 FDPIC_PLT_LAZY_OFFSET
,
2011 &fdpic_sh2a_short_plt_le
2015 /* Return the type of PLT associated with ABFD. PIC_P is true if
2016 the object is position-independent. */
2018 static const struct elf_sh_plt_info
*
2019 get_plt_info (bfd
*abfd
, bfd_boolean pic_p
)
2021 if (fdpic_object_p (abfd
))
2023 /* If any input file requires SH2A we can use a shorter PLT
2025 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd
)) & arch_sh2a_base
)
2026 return &fdpic_sh2a_plts
[!bfd_big_endian (abfd
)];
2028 return &fdpic_sh_plts
[!bfd_big_endian (abfd
)];
2030 if (vxworks_object_p (abfd
))
2031 return &vxworks_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2032 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2035 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2036 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2040 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p ATTRIBUTE_UNUSED
,
2041 unsigned long value
, bfd_byte
*addr
)
2043 bfd_put_32 (output_bfd
, value
, addr
);
2046 /* The number of PLT entries which can use a shorter PLT, if any.
2047 Currently always 64K, since only SH-2A FDPIC uses this; a
2048 20-bit movi20 can address that many function descriptors below
2049 _GLOBAL_OFFSET_TABLE_. */
2050 #define MAX_SHORT_PLT 65536
2052 /* Return the index of the PLT entry at byte offset OFFSET. */
2055 get_plt_index (const struct elf_sh_plt_info
*info
, bfd_vma offset
)
2057 bfd_vma plt_index
= 0;
2059 offset
-= info
->plt0_entry_size
;
2060 if (info
->short_plt
!= NULL
)
2062 if (offset
> MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
)
2064 plt_index
= MAX_SHORT_PLT
;
2065 offset
-= MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
;
2068 info
= info
->short_plt
;
2070 return plt_index
+ offset
/ info
->symbol_entry_size
;
2073 /* Do the inverse operation. */
2076 get_plt_offset (const struct elf_sh_plt_info
*info
, bfd_vma plt_index
)
2080 if (info
->short_plt
!= NULL
)
2082 if (plt_index
> MAX_SHORT_PLT
)
2084 offset
= MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
;
2085 plt_index
-= MAX_SHORT_PLT
;
2088 info
= info
->short_plt
;
2090 return (offset
+ info
->plt0_entry_size
2091 + (plt_index
* info
->symbol_entry_size
));
2096 bfd_signed_vma refcount
;
2100 /* sh ELF linker hash entry. */
2102 struct elf_sh_link_hash_entry
2104 struct elf_link_hash_entry root
;
2106 /* Track dynamic relocs copied for this symbol. */
2107 struct elf_dyn_relocs
*dyn_relocs
;
2109 bfd_signed_vma gotplt_refcount
;
2111 /* A local function descriptor, for FDPIC. The refcount counts
2112 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2113 relocations; the PLT and GOT entry are accounted
2114 for separately. After adjust_dynamic_symbol, the offset is
2115 MINUS_ONE if there is no local descriptor (dynamic linker
2116 managed and no PLT entry, or undefined weak non-dynamic).
2117 During check_relocs we do not yet know whether the local
2118 descriptor will be canonical. */
2119 union gotref funcdesc
;
2121 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2122 and thus require fixups or relocations. */
2123 bfd_signed_vma abs_funcdesc_refcount
;
2126 GOT_UNKNOWN
= 0, GOT_NORMAL
, GOT_TLS_GD
, GOT_TLS_IE
, GOT_FUNCDESC
2130 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2132 struct sh_elf_obj_tdata
2134 struct elf_obj_tdata root
;
2136 /* got_type for each local got entry. */
2137 char *local_got_type
;
2139 /* Function descriptor refcount and offset for each local symbol. */
2140 union gotref
*local_funcdesc
;
2143 #define sh_elf_tdata(abfd) \
2144 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2146 #define sh_elf_local_got_type(abfd) \
2147 (sh_elf_tdata (abfd)->local_got_type)
2149 #define sh_elf_local_funcdesc(abfd) \
2150 (sh_elf_tdata (abfd)->local_funcdesc)
2152 #define is_sh_elf(bfd) \
2153 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2154 && elf_tdata (bfd) != NULL \
2155 && elf_object_id (bfd) == SH_ELF_DATA)
2157 /* Override the generic function because we need to store sh_elf_obj_tdata
2158 as the specific tdata. */
2161 sh_elf_mkobject (bfd
*abfd
)
2163 return bfd_elf_allocate_object (abfd
, sizeof (struct sh_elf_obj_tdata
),
2167 /* sh ELF linker hash table. */
2169 struct elf_sh_link_hash_table
2171 struct elf_link_hash_table root
;
2173 /* Short-cuts to get to dynamic linker sections. */
2176 asection
*sfuncdesc
;
2177 asection
*srelfuncdesc
;
2180 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2183 /* Small local sym cache. */
2184 struct sym_cache sym_cache
;
2186 /* A counter or offset to track a TLS got entry. */
2189 bfd_signed_vma refcount
;
2193 /* The type of PLT to use. */
2194 const struct elf_sh_plt_info
*plt_info
;
2196 /* True if the target system is VxWorks. */
2197 bfd_boolean vxworks_p
;
2199 /* True if the target system uses FDPIC. */
2200 bfd_boolean fdpic_p
;
2203 /* Traverse an sh ELF linker hash table. */
2205 #define sh_elf_link_hash_traverse(table, func, info) \
2206 (elf_link_hash_traverse \
2208 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2211 /* Get the sh ELF linker hash table from a link_info structure. */
2213 #define sh_elf_hash_table(p) \
2214 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2215 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2217 /* Create an entry in an sh ELF linker hash table. */
2219 static struct bfd_hash_entry
*
2220 sh_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
2221 struct bfd_hash_table
*table
,
2224 struct elf_sh_link_hash_entry
*ret
=
2225 (struct elf_sh_link_hash_entry
*) entry
;
2227 /* Allocate the structure if it has not already been allocated by a
2229 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2230 ret
= ((struct elf_sh_link_hash_entry
*)
2231 bfd_hash_allocate (table
,
2232 sizeof (struct elf_sh_link_hash_entry
)));
2233 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2234 return (struct bfd_hash_entry
*) ret
;
2236 /* Call the allocation method of the superclass. */
2237 ret
= ((struct elf_sh_link_hash_entry
*)
2238 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2240 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2242 ret
->dyn_relocs
= NULL
;
2243 ret
->gotplt_refcount
= 0;
2244 ret
->funcdesc
.refcount
= 0;
2245 ret
->abs_funcdesc_refcount
= 0;
2246 ret
->got_type
= GOT_UNKNOWN
;
2249 return (struct bfd_hash_entry
*) ret
;
2252 /* Create an sh ELF linker hash table. */
2254 static struct bfd_link_hash_table
*
2255 sh_elf_link_hash_table_create (bfd
*abfd
)
2257 struct elf_sh_link_hash_table
*ret
;
2258 bfd_size_type amt
= sizeof (struct elf_sh_link_hash_table
);
2260 ret
= (struct elf_sh_link_hash_table
*) bfd_zmalloc (amt
);
2261 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2264 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2265 sh_elf_link_hash_newfunc
,
2266 sizeof (struct elf_sh_link_hash_entry
),
2273 ret
->vxworks_p
= vxworks_object_p (abfd
);
2274 ret
->fdpic_p
= fdpic_object_p (abfd
);
2276 return &ret
->root
.root
;
2280 sh_elf_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2281 struct bfd_link_info
*info
, asection
*p
)
2283 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
2285 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2289 /* We need dynamic symbols for every section, since segments can
2290 relocate independently. */
2291 switch (elf_section_data (p
)->this_hdr
.sh_type
)
2295 /* If sh_type is yet undecided, assume it could be
2296 SHT_PROGBITS/SHT_NOBITS. */
2300 /* There shouldn't be section relative relocations
2301 against any other section. */
2307 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2308 shortcuts to them in our hash table. */
2311 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
2313 struct elf_sh_link_hash_table
*htab
;
2315 if (! _bfd_elf_create_got_section (dynobj
, info
))
2318 htab
= sh_elf_hash_table (info
);
2322 htab
->sfuncdesc
= bfd_make_section_anyway_with_flags (dynobj
, ".got.funcdesc",
2323 (SEC_ALLOC
| SEC_LOAD
2326 | SEC_LINKER_CREATED
));
2327 if (htab
->sfuncdesc
== NULL
2328 || !bfd_set_section_alignment (htab
->sfuncdesc
, 2))
2331 htab
->srelfuncdesc
= bfd_make_section_anyway_with_flags (dynobj
,
2332 ".rela.got.funcdesc",
2333 (SEC_ALLOC
| SEC_LOAD
2336 | SEC_LINKER_CREATED
2338 if (htab
->srelfuncdesc
== NULL
2339 || !bfd_set_section_alignment (htab
->srelfuncdesc
, 2))
2342 /* Also create .rofixup. */
2343 htab
->srofixup
= bfd_make_section_anyway_with_flags (dynobj
, ".rofixup",
2344 (SEC_ALLOC
| SEC_LOAD
2347 | SEC_LINKER_CREATED
2349 if (htab
->srofixup
== NULL
2350 || !bfd_set_section_alignment (htab
->srofixup
, 2))
2356 /* Create dynamic sections when linking against a dynamic object. */
2359 sh_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2361 struct elf_sh_link_hash_table
*htab
;
2362 flagword flags
, pltflags
;
2364 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2367 switch (bed
->s
->arch_size
)
2378 bfd_set_error (bfd_error_bad_value
);
2382 htab
= sh_elf_hash_table (info
);
2386 if (htab
->root
.dynamic_sections_created
)
2389 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2390 .rel[a].bss sections. */
2392 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2393 | SEC_LINKER_CREATED
);
2396 pltflags
|= SEC_CODE
;
2397 if (bed
->plt_not_loaded
)
2398 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2399 if (bed
->plt_readonly
)
2400 pltflags
|= SEC_READONLY
;
2402 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
2403 htab
->root
.splt
= s
;
2405 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
2408 if (bed
->want_plt_sym
)
2410 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2412 struct elf_link_hash_entry
*h
;
2413 struct bfd_link_hash_entry
*bh
= NULL
;
2415 if (! (_bfd_generic_link_add_one_symbol
2416 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2417 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
2418 get_elf_backend_data (abfd
)->collect
, &bh
)))
2421 h
= (struct elf_link_hash_entry
*) bh
;
2423 h
->type
= STT_OBJECT
;
2424 htab
->root
.hplt
= h
;
2426 if (bfd_link_pic (info
)
2427 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2431 s
= bfd_make_section_anyway_with_flags (abfd
,
2432 bed
->default_use_rela_p
2433 ? ".rela.plt" : ".rel.plt",
2434 flags
| SEC_READONLY
);
2435 htab
->root
.srelplt
= s
;
2437 || !bfd_set_section_alignment (s
, ptralign
))
2440 if (htab
->root
.sgot
== NULL
2441 && !create_got_section (abfd
, info
))
2444 if (bed
->want_dynbss
)
2446 /* The .dynbss section is a place to put symbols which are defined
2447 by dynamic objects, are referenced by regular objects, and are
2448 not functions. We must allocate space for them in the process
2449 image and use a R_*_COPY reloc to tell the dynamic linker to
2450 initialize them at run time. The linker script puts the .dynbss
2451 section into the .bss section of the final image. */
2452 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
2453 SEC_ALLOC
| SEC_LINKER_CREATED
);
2458 /* The .rel[a].bss section holds copy relocs. This section is not
2459 normally needed. We need to create it here, though, so that the
2460 linker will map it to an output section. We can't just create it
2461 only if we need it, because we will not know whether we need it
2462 until we have seen all the input files, and the first time the
2463 main linker code calls BFD after examining all the input files
2464 (size_dynamic_sections) the input sections have already been
2465 mapped to the output sections. If the section turns out not to
2466 be needed, we can discard it later. We will never need this
2467 section when generating a shared object, since they do not use
2469 if (! bfd_link_pic (info
))
2471 s
= bfd_make_section_anyway_with_flags (abfd
,
2472 (bed
->default_use_rela_p
2473 ? ".rela.bss" : ".rel.bss"),
2474 flags
| SEC_READONLY
);
2477 || !bfd_set_section_alignment (s
, ptralign
))
2482 if (htab
->vxworks_p
)
2484 if (!elf_vxworks_create_dynamic_sections (abfd
, info
, &htab
->srelplt2
))
2491 /* Find dynamic relocs for H that apply to read-only sections. */
2494 readonly_dynrelocs (struct elf_link_hash_entry
*h
)
2496 struct elf_dyn_relocs
*p
;
2498 for (p
= sh_elf_hash_entry (h
)->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2500 asection
*s
= p
->sec
->output_section
;
2502 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2508 /* Adjust a symbol defined by a dynamic object and referenced by a
2509 regular object. The current definition is in some section of the
2510 dynamic object, but we're not including those sections. We have to
2511 change the definition to something the rest of the link can
2515 sh_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2516 struct elf_link_hash_entry
*h
)
2518 struct elf_sh_link_hash_table
*htab
;
2521 htab
= sh_elf_hash_table (info
);
2525 /* Make sure we know what is going on here. */
2526 BFD_ASSERT (htab
->root
.dynobj
!= NULL
2531 && !h
->def_regular
)));
2533 /* If this is a function, put it in the procedure linkage table. We
2534 will fill in the contents of the procedure linkage table later,
2535 when we know the address of the .got section. */
2536 if (h
->type
== STT_FUNC
2539 if (h
->plt
.refcount
<= 0
2540 || SYMBOL_CALLS_LOCAL (info
, h
)
2541 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2542 && h
->root
.type
== bfd_link_hash_undefweak
))
2544 /* This case can occur if we saw a PLT reloc in an input
2545 file, but the symbol was never referred to by a dynamic
2546 object. In such a case, we don't actually need to build
2547 a procedure linkage table, and we can just do a REL32
2549 h
->plt
.offset
= (bfd_vma
) -1;
2556 h
->plt
.offset
= (bfd_vma
) -1;
2558 /* If this is a weak symbol, and there is a real definition, the
2559 processor independent code will have arranged for us to see the
2560 real definition first, and we can just use the same value. */
2561 if (h
->is_weakalias
)
2563 struct elf_link_hash_entry
*def
= weakdef (h
);
2564 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
2565 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
2566 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
2567 if (info
->nocopyreloc
)
2568 h
->non_got_ref
= def
->non_got_ref
;
2572 /* This is a reference to a symbol defined by a dynamic object which
2573 is not a function. */
2575 /* If we are creating a shared library, we must presume that the
2576 only references to the symbol are via the global offset table.
2577 For such cases we need not do anything here; the relocations will
2578 be handled correctly by relocate_section. */
2579 if (bfd_link_pic (info
))
2582 /* If there are no references to this symbol that do not use the
2583 GOT, we don't need to generate a copy reloc. */
2584 if (!h
->non_got_ref
)
2587 /* If -z nocopyreloc was given, we won't generate them either. */
2588 if (0 && info
->nocopyreloc
)
2594 /* If we don't find any dynamic relocs in read-only sections, then
2595 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2596 if (0 && !readonly_dynrelocs (h
))
2602 /* We must allocate the symbol in our .dynbss section, which will
2603 become part of the .bss section of the executable. There will be
2604 an entry for this symbol in the .dynsym section. The dynamic
2605 object will contain position independent code, so all references
2606 from the dynamic object to this symbol will go through the global
2607 offset table. The dynamic linker will use the .dynsym entry to
2608 determine the address it must put in the global offset table, so
2609 both the dynamic object and the regular object will refer to the
2610 same memory location for the variable. */
2613 BFD_ASSERT (s
!= NULL
);
2615 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2616 copy the initial value out of the dynamic object and into the
2617 runtime process image. We need to remember the offset into the
2618 .rela.bss section we are going to use. */
2619 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
2623 srel
= htab
->srelbss
;
2624 BFD_ASSERT (srel
!= NULL
);
2625 srel
->size
+= sizeof (Elf32_External_Rela
);
2629 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
2632 /* Allocate space in .plt, .got and associated reloc sections for
2636 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2638 struct bfd_link_info
*info
;
2639 struct elf_sh_link_hash_table
*htab
;
2640 struct elf_sh_link_hash_entry
*eh
;
2641 struct elf_dyn_relocs
*p
;
2643 if (h
->root
.type
== bfd_link_hash_indirect
)
2646 info
= (struct bfd_link_info
*) inf
;
2647 htab
= sh_elf_hash_table (info
);
2651 eh
= (struct elf_sh_link_hash_entry
*) h
;
2652 if ((h
->got
.refcount
> 0
2654 && eh
->gotplt_refcount
> 0)
2656 /* The symbol has been forced local, or we have some direct got refs,
2657 so treat all the gotplt refs as got refs. */
2658 h
->got
.refcount
+= eh
->gotplt_refcount
;
2659 if (h
->plt
.refcount
>= eh
->gotplt_refcount
)
2660 h
->plt
.refcount
-= eh
->gotplt_refcount
;
2663 if (htab
->root
.dynamic_sections_created
2664 && h
->plt
.refcount
> 0
2665 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2666 || h
->root
.type
!= bfd_link_hash_undefweak
))
2668 /* Make sure this symbol is output as a dynamic symbol.
2669 Undefined weak syms won't yet be marked as dynamic. */
2670 if (h
->dynindx
== -1
2671 && !h
->forced_local
)
2673 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2677 if (bfd_link_pic (info
)
2678 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2680 asection
*s
= htab
->root
.splt
;
2681 const struct elf_sh_plt_info
*plt_info
;
2683 /* If this is the first .plt entry, make room for the special
2686 s
->size
+= htab
->plt_info
->plt0_entry_size
;
2688 h
->plt
.offset
= s
->size
;
2690 /* If this symbol is not defined in a regular file, and we are
2691 not generating a shared library, then set the symbol to this
2692 location in the .plt. This is required to make function
2693 pointers compare as equal between the normal executable and
2694 the shared library. Skip this for FDPIC, since the
2695 function's address will be the address of the canonical
2696 function descriptor. */
2697 if (!htab
->fdpic_p
&& !bfd_link_pic (info
) && !h
->def_regular
)
2699 h
->root
.u
.def
.section
= s
;
2700 h
->root
.u
.def
.value
= h
->plt
.offset
;
2703 /* Make room for this entry. */
2704 plt_info
= htab
->plt_info
;
2705 if (plt_info
->short_plt
!= NULL
2706 && (get_plt_index (plt_info
->short_plt
, s
->size
) < MAX_SHORT_PLT
))
2707 plt_info
= plt_info
->short_plt
;
2708 s
->size
+= plt_info
->symbol_entry_size
;
2710 /* We also need to make an entry in the .got.plt section, which
2711 will be placed in the .got section by the linker script. */
2713 htab
->root
.sgotplt
->size
+= 4;
2715 htab
->root
.sgotplt
->size
+= 8;
2717 /* We also need to make an entry in the .rel.plt section. */
2718 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
2720 if (htab
->vxworks_p
&& !bfd_link_pic (info
))
2722 /* VxWorks executables have a second set of relocations
2723 for each PLT entry. They go in a separate relocation
2724 section, which is processed by the kernel loader. */
2726 /* There is a relocation for the initial PLT entry:
2727 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2728 if (h
->plt
.offset
== htab
->plt_info
->plt0_entry_size
)
2729 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
);
2731 /* There are two extra relocations for each subsequent
2732 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2733 and an R_SH_DIR32 relocation for the PLT entry. */
2734 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
) * 2;
2739 h
->plt
.offset
= (bfd_vma
) -1;
2745 h
->plt
.offset
= (bfd_vma
) -1;
2749 if (h
->got
.refcount
> 0)
2753 enum got_type got_type
= sh_elf_hash_entry (h
)->got_type
;
2755 /* Make sure this symbol is output as a dynamic symbol.
2756 Undefined weak syms won't yet be marked as dynamic. */
2757 if (h
->dynindx
== -1
2758 && !h
->forced_local
)
2760 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2764 s
= htab
->root
.sgot
;
2765 h
->got
.offset
= s
->size
;
2767 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2768 if (got_type
== GOT_TLS_GD
)
2770 dyn
= htab
->root
.dynamic_sections_created
;
2773 /* No dynamic relocations required. */
2774 if (htab
->fdpic_p
&& !bfd_link_pic (info
)
2775 && h
->root
.type
!= bfd_link_hash_undefweak
2776 && (got_type
== GOT_NORMAL
|| got_type
== GOT_FUNCDESC
))
2777 htab
->srofixup
->size
+= 4;
2779 /* No dynamic relocations required when IE->LE conversion happens. */
2780 else if (got_type
== GOT_TLS_IE
2782 && !bfd_link_pic (info
))
2784 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2785 R_SH_TLS_GD needs one if local symbol and two if global. */
2786 else if ((got_type
== GOT_TLS_GD
&& h
->dynindx
== -1)
2787 || got_type
== GOT_TLS_IE
)
2788 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2789 else if (got_type
== GOT_TLS_GD
)
2790 htab
->root
.srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
2791 else if (got_type
== GOT_FUNCDESC
)
2793 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2794 htab
->srofixup
->size
+= 4;
2796 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2798 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2799 || h
->root
.type
!= bfd_link_hash_undefweak
)
2800 && (bfd_link_pic (info
)
2801 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
2802 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2803 else if (htab
->fdpic_p
2804 && !bfd_link_pic (info
)
2805 && got_type
== GOT_NORMAL
2806 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2807 || h
->root
.type
!= bfd_link_hash_undefweak
))
2808 htab
->srofixup
->size
+= 4;
2811 h
->got
.offset
= (bfd_vma
) -1;
2813 /* Allocate space for any dynamic relocations to function
2814 descriptors, canonical or otherwise. We need to relocate the
2815 reference unless it resolves to zero, which only happens for
2816 undefined weak symbols (either non-default visibility, or when
2817 static linking). Any GOT slot is accounted for elsewhere. */
2818 if (eh
->abs_funcdesc_refcount
> 0
2819 && (h
->root
.type
!= bfd_link_hash_undefweak
2820 || (htab
->root
.dynamic_sections_created
2821 && ! SYMBOL_CALLS_LOCAL (info
, h
))))
2823 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2824 htab
->srofixup
->size
+= eh
->abs_funcdesc_refcount
* 4;
2826 htab
->root
.srelgot
->size
2827 += eh
->abs_funcdesc_refcount
* sizeof (Elf32_External_Rela
);
2830 /* We must allocate a function descriptor if there are references to
2831 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
2832 the dynamic linker isn't going to allocate it. None of this
2833 applies if we already created one in .got.plt, but if the
2834 canonical function descriptor can be in this object, there
2835 won't be a PLT entry at all. */
2836 if ((eh
->funcdesc
.refcount
> 0
2837 || (h
->got
.offset
!= MINUS_ONE
&& eh
->got_type
== GOT_FUNCDESC
))
2838 && h
->root
.type
!= bfd_link_hash_undefweak
2839 && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2841 /* Make room for this function descriptor. */
2842 eh
->funcdesc
.offset
= htab
->sfuncdesc
->size
;
2843 htab
->sfuncdesc
->size
+= 8;
2845 /* We will need a relocation or two fixups to initialize the
2846 function descriptor, so allocate those too. */
2847 if (!bfd_link_pic (info
) && SYMBOL_CALLS_LOCAL (info
, h
))
2848 htab
->srofixup
->size
+= 8;
2850 htab
->srelfuncdesc
->size
+= sizeof (Elf32_External_Rela
);
2853 if (eh
->dyn_relocs
== NULL
)
2856 /* In the shared -Bsymbolic case, discard space allocated for
2857 dynamic pc-relative relocs against symbols which turn out to be
2858 defined in regular objects. For the normal shared case, discard
2859 space for pc-relative relocs that have become local due to symbol
2860 visibility changes. */
2862 if (bfd_link_pic (info
))
2864 if (SYMBOL_CALLS_LOCAL (info
, h
))
2866 struct elf_dyn_relocs
**pp
;
2868 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2870 p
->count
-= p
->pc_count
;
2879 if (htab
->vxworks_p
)
2881 struct elf_dyn_relocs
**pp
;
2883 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2885 if (strcmp (p
->sec
->output_section
->name
, ".tls_vars") == 0)
2892 /* Also discard relocs on undefined weak syms with non-default
2894 if (eh
->dyn_relocs
!= NULL
2895 && h
->root
.type
== bfd_link_hash_undefweak
)
2897 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2898 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2899 eh
->dyn_relocs
= NULL
;
2901 /* Make sure undefined weak symbols are output as a dynamic
2903 else if (h
->dynindx
== -1
2904 && !h
->forced_local
)
2906 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2913 /* For the non-shared case, discard space for relocs against
2914 symbols which turn out to need copy relocs or are not
2920 || (htab
->root
.dynamic_sections_created
2921 && (h
->root
.type
== bfd_link_hash_undefweak
2922 || h
->root
.type
== bfd_link_hash_undefined
))))
2924 /* Make sure this symbol is output as a dynamic symbol.
2925 Undefined weak syms won't yet be marked as dynamic. */
2926 if (h
->dynindx
== -1
2927 && !h
->forced_local
)
2929 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2933 /* If that succeeded, we know we'll be keeping all the
2935 if (h
->dynindx
!= -1)
2939 eh
->dyn_relocs
= NULL
;
2944 /* Finally, allocate space. */
2945 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2947 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
2948 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
2950 /* If we need relocations, we do not need fixups. */
2951 if (htab
->fdpic_p
&& !bfd_link_pic (info
))
2952 htab
->srofixup
->size
-= 4 * (p
->count
- p
->pc_count
);
2958 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2959 read-only sections. */
2962 maybe_set_textrel (struct elf_link_hash_entry
*h
, void *info_p
)
2966 if (h
->root
.type
== bfd_link_hash_indirect
)
2969 sec
= readonly_dynrelocs (h
);
2972 struct bfd_link_info
*info
= (struct bfd_link_info
*) info_p
;
2974 info
->flags
|= DF_TEXTREL
;
2975 info
->callbacks
->minfo
2976 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
2977 sec
->owner
, h
->root
.root
.string
, sec
);
2979 /* Not an error, just cut short the traversal. */
2985 /* This function is called after all the input files have been read,
2986 and the input sections have been assigned to output sections.
2987 It's a convenient place to determine the PLT style. */
2990 sh_elf_always_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
2992 sh_elf_hash_table (info
)->plt_info
= get_plt_info (output_bfd
,
2993 bfd_link_pic (info
));
2995 if (sh_elf_hash_table (info
)->fdpic_p
&& !bfd_link_relocatable (info
)
2996 && !bfd_elf_stack_segment_size (output_bfd
, info
,
2997 "__stacksize", DEFAULT_STACK_SIZE
))
3002 /* Set the sizes of the dynamic sections. */
3005 sh_elf_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
3006 struct bfd_link_info
*info
)
3008 struct elf_sh_link_hash_table
*htab
;
3014 htab
= sh_elf_hash_table (info
);
3018 dynobj
= htab
->root
.dynobj
;
3019 BFD_ASSERT (dynobj
!= NULL
);
3021 if (htab
->root
.dynamic_sections_created
)
3023 /* Set the contents of the .interp section to the interpreter. */
3024 if (bfd_link_executable (info
) && !info
->nointerp
)
3026 s
= bfd_get_linker_section (dynobj
, ".interp");
3027 BFD_ASSERT (s
!= NULL
);
3028 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
3029 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
3033 /* Set up .got offsets for local syms, and space for local dynamic
3035 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
3037 bfd_signed_vma
*local_got
;
3038 bfd_signed_vma
*end_local_got
;
3039 union gotref
*local_funcdesc
, *end_local_funcdesc
;
3040 char *local_got_type
;
3041 bfd_size_type locsymcount
;
3042 Elf_Internal_Shdr
*symtab_hdr
;
3045 if (! is_sh_elf (ibfd
))
3048 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3050 struct elf_dyn_relocs
*p
;
3052 for (p
= ((struct elf_dyn_relocs
*)
3053 elf_section_data (s
)->local_dynrel
);
3057 if (! bfd_is_abs_section (p
->sec
)
3058 && bfd_is_abs_section (p
->sec
->output_section
))
3060 /* Input section has been discarded, either because
3061 it is a copy of a linkonce section or due to
3062 linker script /DISCARD/, so we'll be discarding
3065 else if (htab
->vxworks_p
3066 && strcmp (p
->sec
->output_section
->name
,
3069 /* Relocations in vxworks .tls_vars sections are
3070 handled specially by the loader. */
3072 else if (p
->count
!= 0)
3074 srel
= elf_section_data (p
->sec
)->sreloc
;
3075 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
3076 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
3078 info
->flags
|= DF_TEXTREL
;
3079 info
->callbacks
->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
3080 p
->sec
->owner
, p
->sec
);
3083 /* If we need relocations, we do not need fixups. */
3084 if (htab
->fdpic_p
&& !bfd_link_pic (info
))
3085 htab
->srofixup
->size
-= 4 * (p
->count
- p
->pc_count
);
3090 symtab_hdr
= &elf_symtab_hdr (ibfd
);
3091 locsymcount
= symtab_hdr
->sh_info
;
3092 s
= htab
->root
.sgot
;
3093 srel
= htab
->root
.srelgot
;
3095 local_got
= elf_local_got_refcounts (ibfd
);
3098 end_local_got
= local_got
+ locsymcount
;
3099 local_got_type
= sh_elf_local_got_type (ibfd
);
3100 local_funcdesc
= sh_elf_local_funcdesc (ibfd
);
3101 for (; local_got
< end_local_got
; ++local_got
)
3105 *local_got
= s
->size
;
3107 if (*local_got_type
== GOT_TLS_GD
)
3109 if (bfd_link_pic (info
))
3110 srel
->size
+= sizeof (Elf32_External_Rela
);
3112 htab
->srofixup
->size
+= 4;
3114 if (*local_got_type
== GOT_FUNCDESC
)
3116 if (local_funcdesc
== NULL
)
3120 size
= locsymcount
* sizeof (union gotref
);
3121 local_funcdesc
= (union gotref
*) bfd_zalloc (ibfd
,
3123 if (local_funcdesc
== NULL
)
3125 sh_elf_local_funcdesc (ibfd
) = local_funcdesc
;
3126 local_funcdesc
+= (local_got
3127 - elf_local_got_refcounts (ibfd
));
3129 local_funcdesc
->refcount
++;
3134 *local_got
= (bfd_vma
) -1;
3139 local_funcdesc
= sh_elf_local_funcdesc (ibfd
);
3142 end_local_funcdesc
= local_funcdesc
+ locsymcount
;
3144 for (; local_funcdesc
< end_local_funcdesc
; ++local_funcdesc
)
3146 if (local_funcdesc
->refcount
> 0)
3148 local_funcdesc
->offset
= htab
->sfuncdesc
->size
;
3149 htab
->sfuncdesc
->size
+= 8;
3150 if (!bfd_link_pic (info
))
3151 htab
->srofixup
->size
+= 8;
3153 htab
->srelfuncdesc
->size
+= sizeof (Elf32_External_Rela
);
3156 local_funcdesc
->offset
= MINUS_ONE
;
3162 if (htab
->tls_ldm_got
.refcount
> 0)
3164 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3166 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
3167 htab
->root
.sgot
->size
+= 8;
3168 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
3171 htab
->tls_ldm_got
.offset
= -1;
3173 /* Only the reserved entries should be present. For FDPIC, they go at
3174 the end of .got.plt. */
3177 BFD_ASSERT (htab
->root
.sgotplt
&& htab
->root
.sgotplt
->size
== 12);
3178 htab
->root
.sgotplt
->size
= 0;
3181 /* Allocate global sym .plt and .got entries, and space for global
3182 sym dynamic relocs. */
3183 elf_link_hash_traverse (&htab
->root
, allocate_dynrelocs
, info
);
3185 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3186 end of the FDPIC .got.plt. */
3189 htab
->root
.hgot
->root
.u
.def
.value
= htab
->root
.sgotplt
->size
;
3190 htab
->root
.sgotplt
->size
+= 12;
3193 /* At the very end of the .rofixup section is a pointer to the GOT. */
3194 if (htab
->fdpic_p
&& htab
->srofixup
!= NULL
)
3195 htab
->srofixup
->size
+= 4;
3197 /* We now have determined the sizes of the various dynamic sections.
3198 Allocate memory for them. */
3200 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
3202 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
3205 if (s
== htab
->root
.splt
3206 || s
== htab
->root
.sgot
3207 || s
== htab
->root
.sgotplt
3208 || s
== htab
->sfuncdesc
3209 || s
== htab
->srofixup
3210 || s
== htab
->sdynbss
)
3212 /* Strip this section if we don't need it; see the
3215 else if (CONST_STRNEQ (bfd_section_name (s
), ".rela"))
3217 if (s
->size
!= 0 && s
!= htab
->root
.srelplt
&& s
!= htab
->srelplt2
)
3220 /* We use the reloc_count field as a counter if we need
3221 to copy relocs into the output file. */
3226 /* It's not one of our sections, so don't allocate space. */
3232 /* If we don't need this section, strip it from the
3233 output file. This is mostly to handle .rela.bss and
3234 .rela.plt. We must create both sections in
3235 create_dynamic_sections, because they must be created
3236 before the linker maps input sections to output
3237 sections. The linker does that before
3238 adjust_dynamic_symbol is called, and it is that
3239 function which decides whether anything needs to go
3240 into these sections. */
3242 s
->flags
|= SEC_EXCLUDE
;
3246 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3249 /* Allocate memory for the section contents. We use bfd_zalloc
3250 here in case unused entries are not reclaimed before the
3251 section's contents are written out. This should not happen,
3252 but this way if it does, we get a R_SH_NONE reloc instead
3254 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
3255 if (s
->contents
== NULL
)
3259 if (htab
->root
.dynamic_sections_created
)
3261 /* Add some entries to the .dynamic section. We fill in the
3262 values later, in sh_elf_finish_dynamic_sections, but we
3263 must add the entries now so that we get the correct size for
3264 the .dynamic section. The DT_DEBUG entry is filled in by the
3265 dynamic linker and used by the debugger. */
3266 #define add_dynamic_entry(TAG, VAL) \
3267 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3269 if (bfd_link_executable (info
))
3271 if (! add_dynamic_entry (DT_DEBUG
, 0))
3275 if (htab
->root
.splt
->size
!= 0)
3277 if (! add_dynamic_entry (DT_PLTGOT
, 0)
3278 || ! add_dynamic_entry (DT_PLTRELSZ
, 0)
3279 || ! add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3280 || ! add_dynamic_entry (DT_JMPREL
, 0))
3283 else if ((elf_elfheader (output_bfd
)->e_flags
& EF_SH_FDPIC
))
3285 if (! add_dynamic_entry (DT_PLTGOT
, 0))
3291 if (! add_dynamic_entry (DT_RELA
, 0)
3292 || ! add_dynamic_entry (DT_RELASZ
, 0)
3293 || ! add_dynamic_entry (DT_RELAENT
,
3294 sizeof (Elf32_External_Rela
)))
3297 /* If any dynamic relocs apply to a read-only section,
3298 then we need a DT_TEXTREL entry. */
3299 if ((info
->flags
& DF_TEXTREL
) == 0)
3300 elf_link_hash_traverse (&htab
->root
, maybe_set_textrel
, info
);
3302 if ((info
->flags
& DF_TEXTREL
) != 0)
3304 if (! add_dynamic_entry (DT_TEXTREL
, 0))
3309 && !elf_vxworks_add_dynamic_entries (output_bfd
, info
))
3312 #undef add_dynamic_entry
3317 /* Add a dynamic relocation to the SRELOC section. */
3319 inline static bfd_vma
3320 sh_elf_add_dyn_reloc (bfd
*output_bfd
, asection
*sreloc
, bfd_vma offset
,
3321 int reloc_type
, long dynindx
, bfd_vma addend
)
3323 Elf_Internal_Rela outrel
;
3324 bfd_vma reloc_offset
;
3326 outrel
.r_offset
= offset
;
3327 outrel
.r_info
= ELF32_R_INFO (dynindx
, reloc_type
);
3328 outrel
.r_addend
= addend
;
3330 reloc_offset
= sreloc
->reloc_count
* sizeof (Elf32_External_Rela
);
3331 BFD_ASSERT (reloc_offset
< sreloc
->size
);
3332 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3333 sreloc
->contents
+ reloc_offset
);
3334 sreloc
->reloc_count
++;
3336 return reloc_offset
;
3339 /* Add an FDPIC read-only fixup. */
3342 sh_elf_add_rofixup (bfd
*output_bfd
, asection
*srofixup
, bfd_vma offset
)
3344 bfd_vma fixup_offset
;
3346 fixup_offset
= srofixup
->reloc_count
++ * 4;
3347 BFD_ASSERT (fixup_offset
< srofixup
->size
);
3348 bfd_put_32 (output_bfd
, offset
, srofixup
->contents
+ fixup_offset
);
3351 /* Return the offset of the generated .got section from the
3352 _GLOBAL_OFFSET_TABLE_ symbol. */
3354 static bfd_signed_vma
3355 sh_elf_got_offset (struct elf_sh_link_hash_table
*htab
)
3357 return (htab
->root
.sgot
->output_offset
- htab
->root
.sgotplt
->output_offset
3358 - htab
->root
.hgot
->root
.u
.def
.value
);
3361 /* Find the segment number in which OSEC, and output section, is
3365 sh_elf_osec_to_segment (bfd
*output_bfd
, asection
*osec
)
3367 Elf_Internal_Phdr
*p
= NULL
;
3369 if (output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
3370 /* PR ld/17110: Do not look for output segments in an input bfd. */
3371 && output_bfd
->direction
!= read_direction
)
3372 p
= _bfd_elf_find_segment_containing_section (output_bfd
, osec
);
3374 /* FIXME: Nothing ever says what this index is relative to. The kernel
3375 supplies data in terms of the number of load segments but this is
3376 a phdr index and the first phdr may not be a load segment. */
3377 return (p
!= NULL
) ? p
- elf_tdata (output_bfd
)->phdr
: -1;
3381 sh_elf_osec_readonly_p (bfd
*output_bfd
, asection
*osec
)
3383 unsigned seg
= sh_elf_osec_to_segment (output_bfd
, osec
);
3385 return (seg
!= (unsigned) -1
3386 && ! (elf_tdata (output_bfd
)->phdr
[seg
].p_flags
& PF_W
));
3389 /* Generate the initial contents of a local function descriptor, along
3390 with any relocations or fixups required. */
3392 sh_elf_initialize_funcdesc (bfd
*output_bfd
,
3393 struct bfd_link_info
*info
,
3394 struct elf_link_hash_entry
*h
,
3399 struct elf_sh_link_hash_table
*htab
;
3403 htab
= sh_elf_hash_table (info
);
3405 /* FIXME: The ABI says that the offset to the function goes in the
3406 descriptor, along with the segment index. We're RELA, so it could
3407 go in the reloc instead... */
3409 if (h
!= NULL
&& SYMBOL_CALLS_LOCAL (info
, h
))
3411 section
= h
->root
.u
.def
.section
;
3412 value
= h
->root
.u
.def
.value
;
3415 if (h
== NULL
|| SYMBOL_CALLS_LOCAL (info
, h
))
3417 dynindx
= elf_section_data (section
->output_section
)->dynindx
;
3418 addr
= value
+ section
->output_offset
;
3419 seg
= sh_elf_osec_to_segment (output_bfd
, section
->output_section
);
3423 BFD_ASSERT (h
->dynindx
!= -1);
3424 dynindx
= h
->dynindx
;
3428 if (!bfd_link_pic (info
) && SYMBOL_CALLS_LOCAL (info
, h
))
3430 if (h
== NULL
|| h
->root
.type
!= bfd_link_hash_undefweak
)
3432 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
3434 + htab
->sfuncdesc
->output_section
->vma
3435 + htab
->sfuncdesc
->output_offset
);
3436 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
3438 + htab
->sfuncdesc
->output_section
->vma
3439 + htab
->sfuncdesc
->output_offset
);
3442 /* There are no dynamic relocations so fill in the final
3443 address and gp value (barring fixups). */
3444 addr
+= section
->output_section
->vma
;
3445 seg
= htab
->root
.hgot
->root
.u
.def
.value
3446 + htab
->root
.hgot
->root
.u
.def
.section
->output_section
->vma
3447 + htab
->root
.hgot
->root
.u
.def
.section
->output_offset
;
3450 sh_elf_add_dyn_reloc (output_bfd
, htab
->srelfuncdesc
,
3452 + htab
->sfuncdesc
->output_section
->vma
3453 + htab
->sfuncdesc
->output_offset
,
3454 R_SH_FUNCDESC_VALUE
, dynindx
, 0);
3456 bfd_put_32 (output_bfd
, addr
, htab
->sfuncdesc
->contents
+ offset
);
3457 bfd_put_32 (output_bfd
, seg
, htab
->sfuncdesc
->contents
+ offset
+ 4);
3462 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3463 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3466 static bfd_reloc_status_type
3467 install_movi20_field (bfd
*output_bfd
, unsigned long relocation
,
3468 bfd
*input_bfd
, asection
*input_section
,
3469 bfd_byte
*contents
, bfd_vma offset
)
3471 unsigned long cur_val
;
3473 bfd_reloc_status_type r
;
3475 if (offset
> bfd_get_section_limit (input_bfd
, input_section
))
3476 return bfd_reloc_outofrange
;
3478 r
= bfd_check_overflow (complain_overflow_signed
, 20, 0,
3479 bfd_arch_bits_per_address (input_bfd
), relocation
);
3480 if (r
!= bfd_reloc_ok
)
3483 addr
= contents
+ offset
;
3484 cur_val
= bfd_get_16 (output_bfd
, addr
);
3485 bfd_put_16 (output_bfd
, cur_val
| ((relocation
& 0xf0000) >> 12), addr
);
3486 bfd_put_16 (output_bfd
, relocation
& 0xffff, addr
+ 2);
3488 return bfd_reloc_ok
;
3491 /* Relocate an SH ELF section. */
3494 sh_elf_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
3495 bfd
*input_bfd
, asection
*input_section
,
3496 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
3497 Elf_Internal_Sym
*local_syms
,
3498 asection
**local_sections
)
3500 struct elf_sh_link_hash_table
*htab
;
3501 Elf_Internal_Shdr
*symtab_hdr
;
3502 struct elf_link_hash_entry
**sym_hashes
;
3503 Elf_Internal_Rela
*rel
, *relend
;
3504 bfd_vma
*local_got_offsets
;
3505 asection
*sgot
= NULL
;
3506 asection
*sgotplt
= NULL
;
3507 asection
*splt
= NULL
;
3508 asection
*sreloc
= NULL
;
3509 asection
*srelgot
= NULL
;
3510 bfd_boolean is_vxworks_tls
;
3511 unsigned isec_segment
, got_segment
, plt_segment
, check_segment
[2];
3512 bfd_boolean fdpic_p
= FALSE
;
3514 if (!is_sh_elf (input_bfd
))
3516 bfd_set_error (bfd_error_wrong_format
);
3520 htab
= sh_elf_hash_table (info
);
3523 sgot
= htab
->root
.sgot
;
3524 sgotplt
= htab
->root
.sgotplt
;
3525 srelgot
= htab
->root
.srelgot
;
3526 splt
= htab
->root
.splt
;
3527 fdpic_p
= htab
->fdpic_p
;
3529 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
3530 sym_hashes
= elf_sym_hashes (input_bfd
);
3531 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3533 isec_segment
= sh_elf_osec_to_segment (output_bfd
,
3534 input_section
->output_section
);
3535 if (fdpic_p
&& sgot
)
3536 got_segment
= sh_elf_osec_to_segment (output_bfd
,
3537 sgot
->output_section
);
3540 if (fdpic_p
&& splt
)
3541 plt_segment
= sh_elf_osec_to_segment (output_bfd
,
3542 splt
->output_section
);
3546 /* We have to handle relocations in vxworks .tls_vars sections
3547 specially, because the dynamic loader is 'weird'. */
3548 is_vxworks_tls
= (htab
&& htab
->vxworks_p
&& bfd_link_pic (info
)
3549 && !strcmp (input_section
->output_section
->name
,
3553 relend
= relocs
+ input_section
->reloc_count
;
3554 for (; rel
< relend
; rel
++)
3557 reloc_howto_type
*howto
;
3558 unsigned long r_symndx
;
3559 Elf_Internal_Sym
*sym
;
3561 struct elf_link_hash_entry
*h
;
3563 bfd_vma addend
= (bfd_vma
) 0;
3564 bfd_reloc_status_type r
;
3565 int seen_stt_datalabel
= 0;
3567 enum got_type got_type
;
3568 const char *symname
= NULL
;
3569 bfd_boolean resolved_to_zero
;
3571 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3573 r_type
= ELF32_R_TYPE (rel
->r_info
);
3575 /* Many of the relocs are only used for relaxing, and are
3576 handled entirely by the relaxation code. */
3577 if (r_type
>= (int) R_SH_GNU_VTINHERIT
3578 && r_type
<= (int) R_SH_LABEL
)
3580 if (r_type
== (int) R_SH_NONE
)
3584 || r_type
>= R_SH_max
3585 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
3586 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
3587 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
3588 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
)
3589 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_3
3590 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_3
)
3591 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_4
3592 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_4
)
3593 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_5
3594 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_5
)
3595 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_6
3596 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_6
))
3598 bfd_set_error (bfd_error_bad_value
);
3602 howto
= get_howto_table (output_bfd
) + r_type
;
3604 /* For relocs that aren't partial_inplace, we get the addend from
3606 if (! howto
->partial_inplace
)
3607 addend
= rel
->r_addend
;
3609 resolved_to_zero
= FALSE
;
3613 check_segment
[0] = -1;
3614 check_segment
[1] = -1;
3615 if (r_symndx
< symtab_hdr
->sh_info
)
3617 sym
= local_syms
+ r_symndx
;
3618 sec
= local_sections
[r_symndx
];
3620 symname
= bfd_elf_string_from_elf_section
3621 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
3622 if (symname
== NULL
|| *symname
== '\0')
3623 symname
= bfd_section_name (sec
);
3625 relocation
= (sec
->output_section
->vma
3626 + sec
->output_offset
3628 /* A local symbol never has STO_SH5_ISA32, so we don't need
3629 datalabel processing here. Make sure this does not change
3631 if ((sym
->st_other
& STO_SH5_ISA32
) != 0)
3632 (*info
->callbacks
->reloc_dangerous
)
3634 _("unexpected STO_SH5_ISA32 on local symbol is not handled"),
3635 input_bfd
, input_section
, rel
->r_offset
);
3637 if (sec
!= NULL
&& discarded_section (sec
))
3638 /* Handled below. */
3640 else if (bfd_link_relocatable (info
))
3642 /* This is a relocatable link. We don't have to change
3643 anything, unless the reloc is against a section symbol,
3644 in which case we have to adjust according to where the
3645 section symbol winds up in the output section. */
3646 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3648 if (! howto
->partial_inplace
)
3650 /* For relocations with the addend in the
3651 relocation, we need just to update the addend.
3652 All real relocs are of type partial_inplace; this
3653 code is mostly for completeness. */
3654 rel
->r_addend
+= sec
->output_offset
;
3659 /* Relocs of type partial_inplace need to pick up the
3660 contents in the contents and add the offset resulting
3661 from the changed location of the section symbol.
3662 Using _bfd_final_link_relocate (e.g. goto
3663 final_link_relocate) here would be wrong, because
3664 relocations marked pc_relative would get the current
3665 location subtracted, and we must only do that at the
3667 r
= _bfd_relocate_contents (howto
, input_bfd
,
3670 contents
+ rel
->r_offset
);
3671 goto relocation_done
;
3676 else if (! howto
->partial_inplace
)
3678 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3679 addend
= rel
->r_addend
;
3681 else if ((sec
->flags
& SEC_MERGE
)
3682 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3686 if (howto
->rightshift
|| howto
->src_mask
!= 0xffffffff)
3689 /* xgettext:c-format */
3690 (_("%pB(%pA+%#" PRIx64
"): "
3691 "%s relocation against SEC_MERGE section"),
3692 input_bfd
, input_section
,
3693 (uint64_t) rel
->r_offset
, howto
->name
);
3697 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3700 _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
)
3702 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
3703 bfd_put_32 (input_bfd
, addend
, contents
+ rel
->r_offset
);
3709 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3712 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3713 symname
= h
->root
.root
.string
;
3714 while (h
->root
.type
== bfd_link_hash_indirect
3715 || h
->root
.type
== bfd_link_hash_warning
)
3716 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3717 if (h
->root
.type
== bfd_link_hash_defined
3718 || h
->root
.type
== bfd_link_hash_defweak
)
3722 dyn
= htab
? htab
->root
.dynamic_sections_created
: FALSE
;
3723 sec
= h
->root
.u
.def
.section
;
3724 /* In these cases, we don't need the relocation value.
3725 We check specially because in some obscure cases
3726 sec->output_section will be NULL. */
3727 if (r_type
== R_SH_GOTPC
3728 || r_type
== R_SH_GOTPC_LOW16
3729 || r_type
== R_SH_GOTPC_MEDLOW16
3730 || r_type
== R_SH_GOTPC_MEDHI16
3731 || r_type
== R_SH_GOTPC_HI16
3732 || ((r_type
== R_SH_PLT32
3733 || r_type
== R_SH_PLT_LOW16
3734 || r_type
== R_SH_PLT_MEDLOW16
3735 || r_type
== R_SH_PLT_MEDHI16
3736 || r_type
== R_SH_PLT_HI16
)
3737 && h
->plt
.offset
!= (bfd_vma
) -1)
3738 || ((r_type
== R_SH_GOT32
3739 || r_type
== R_SH_GOT20
3740 || r_type
== R_SH_GOTFUNCDESC
3741 || r_type
== R_SH_GOTFUNCDESC20
3742 || r_type
== R_SH_GOTOFFFUNCDESC
3743 || r_type
== R_SH_GOTOFFFUNCDESC20
3744 || r_type
== R_SH_FUNCDESC
3745 || r_type
== R_SH_GOT_LOW16
3746 || r_type
== R_SH_GOT_MEDLOW16
3747 || r_type
== R_SH_GOT_MEDHI16
3748 || r_type
== R_SH_GOT_HI16
)
3749 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
3750 bfd_link_pic (info
),
3752 && (! bfd_link_pic (info
)
3753 || (! info
->symbolic
&& h
->dynindx
!= -1)
3754 || !h
->def_regular
))
3755 /* The cases above are those in which relocation is
3756 overwritten in the switch block below. The cases
3757 below are those in which we must defer relocation
3758 to run-time, because we can't resolve absolute
3759 addresses when creating a shared library. */
3760 || (bfd_link_pic (info
)
3761 && ((! info
->symbolic
&& h
->dynindx
!= -1)
3763 && ((r_type
== R_SH_DIR32
3764 && !h
->forced_local
)
3765 || (r_type
== R_SH_REL32
3766 && !SYMBOL_CALLS_LOCAL (info
, h
)))
3767 && ((input_section
->flags
& SEC_ALLOC
) != 0
3768 /* DWARF will emit R_SH_DIR32 relocations in its
3769 sections against symbols defined externally
3770 in shared libraries. We can't do anything
3772 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
3773 && h
->def_dynamic
)))
3774 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3775 sections because such sections are not SEC_ALLOC and
3776 thus ld.so will not process them. */
3777 || (sec
->output_section
== NULL
3778 && ((input_section
->flags
& SEC_DEBUGGING
) != 0
3780 || (sec
->output_section
== NULL
3781 && (sh_elf_hash_entry (h
)->got_type
== GOT_TLS_IE
3782 || sh_elf_hash_entry (h
)->got_type
== GOT_TLS_GD
)))
3784 else if (sec
->output_section
!= NULL
)
3785 relocation
= ((h
->root
.u
.def
.value
3786 + sec
->output_section
->vma
3787 + sec
->output_offset
)
3788 /* A STO_SH5_ISA32 causes a "bitor 1" to the
3789 symbol value, unless we've seen
3790 STT_DATALABEL on the way to it. */
3791 | ((h
->other
& STO_SH5_ISA32
) != 0
3792 && ! seen_stt_datalabel
));
3793 else if (!bfd_link_relocatable (info
)
3794 && (_bfd_elf_section_offset (output_bfd
, info
,
3800 /* xgettext:c-format */
3801 (_("%pB(%pA+%#" PRIx64
"): "
3802 "unresolvable %s relocation against symbol `%s'"),
3805 (uint64_t) rel
->r_offset
,
3807 h
->root
.root
.string
);
3811 else if (h
->root
.type
== bfd_link_hash_undefweak
)
3812 resolved_to_zero
= UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
);
3813 else if (info
->unresolved_syms_in_objects
== RM_IGNORE
3814 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3816 else if (!bfd_link_relocatable (info
))
3817 (*info
->callbacks
->undefined_symbol
)
3818 (info
, h
->root
.root
.string
, input_bfd
,
3819 input_section
, rel
->r_offset
,
3820 (info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
3821 || ELF_ST_VISIBILITY (h
->other
)));
3824 if (sec
!= NULL
&& discarded_section (sec
))
3825 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
3826 rel
, 1, relend
, howto
, 0, contents
);
3828 if (bfd_link_relocatable (info
))
3831 /* Check for inter-segment relocations in FDPIC files. Most
3832 relocations connect the relocation site to the location of
3833 the target symbol, but there are some exceptions below. */
3834 check_segment
[0] = isec_segment
;
3836 check_segment
[1] = sh_elf_osec_to_segment (output_bfd
,
3837 sec
->output_section
);
3839 check_segment
[1] = -1;
3841 switch ((int) r_type
)
3843 final_link_relocate
:
3844 /* COFF relocs don't use the addend. The addend is used for
3845 R_SH_DIR32 to be compatible with other compilers. */
3846 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3847 contents
, rel
->r_offset
,
3848 relocation
, addend
);
3852 goto final_link_relocate
;
3857 /* If the reloc is against the start of this section, then
3858 the assembler has already taken care of it and the reloc
3859 is here only to assist in relaxing. If the reloc is not
3860 against the start of this section, then it's against an
3861 external symbol and we must deal with it ourselves. */
3862 if (input_section
->output_section
->vma
+ input_section
->output_offset
3865 int disp
= (relocation
3866 - input_section
->output_section
->vma
3867 - input_section
->output_offset
3873 case R_SH_DIR8WPZ
: mask
= 1; break;
3874 case R_SH_DIR8WPL
: mask
= 3; break;
3875 default: mask
= 0; break;
3880 /* xgettext:c-format */
3881 (_("%pB: %#" PRIx64
": fatal: "
3882 "unaligned branch target for relax-support relocation"),
3883 input_section
->owner
,
3884 (uint64_t) rel
->r_offset
);
3885 bfd_set_error (bfd_error_bad_value
);
3889 goto final_link_relocate
;
3895 bfd_set_error (bfd_error_bad_value
);
3903 goto final_link_relocate
;
3910 /* xgettext:c-format */
3911 (_("%pB: %#" PRIx64
": fatal: "
3912 "unaligned %s relocation %#" PRIx64
),
3913 input_section
->owner
, (uint64_t) rel
->r_offset
,
3914 howto
->name
, (uint64_t) relocation
);
3915 bfd_set_error (bfd_error_bad_value
);
3918 goto final_link_relocate
;
3926 /* xgettext:c-format */
3927 (_("%pB: %#" PRIx64
": fatal: "
3928 "unaligned %s relocation %#" PRIx64
""),
3929 input_section
->owner
,
3930 (uint64_t) rel
->r_offset
, howto
->name
,
3931 (uint64_t) relocation
);
3932 bfd_set_error (bfd_error_bad_value
);
3935 goto final_link_relocate
;
3938 if ((signed int)relocation
< -32
3939 || (signed int)relocation
> 32)
3942 /* xgettext:c-format */
3943 (_("%pB: %#" PRIx64
": fatal: R_SH_PSHA relocation %" PRId64
3944 " not in range -32..32"),
3945 input_section
->owner
,
3946 (uint64_t) rel
->r_offset
,
3947 (int64_t) relocation
);
3948 bfd_set_error (bfd_error_bad_value
);
3951 goto final_link_relocate
;
3954 if ((signed int)relocation
< -16
3955 || (signed int)relocation
> 16)
3958 /* xgettext:c-format */
3959 (_("%pB: %#" PRIx64
": fatal: R_SH_PSHL relocation %" PRId64
3960 " not in range -32..32"),
3961 input_section
->owner
,
3962 (uint64_t) rel
->r_offset
,
3963 (int64_t) relocation
);
3964 bfd_set_error (bfd_error_bad_value
);
3967 goto final_link_relocate
;
3971 if (bfd_link_pic (info
)
3973 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3974 && !resolved_to_zero
)
3975 || h
->root
.type
!= bfd_link_hash_undefweak
)
3976 && r_symndx
!= STN_UNDEF
3977 && (input_section
->flags
& SEC_ALLOC
) != 0
3979 && (r_type
== R_SH_DIR32
3980 || !SYMBOL_CALLS_LOCAL (info
, h
)))
3982 Elf_Internal_Rela outrel
;
3984 bfd_boolean skip
, relocate
;
3986 /* When generating a shared object, these relocations
3987 are copied into the output file to be resolved at run
3992 sreloc
= _bfd_elf_get_dynamic_reloc_section
3993 (input_bfd
, input_section
, /*rela?*/ TRUE
);
4002 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
4004 if (outrel
.r_offset
== (bfd_vma
) -1)
4006 else if (outrel
.r_offset
== (bfd_vma
) -2)
4007 skip
= TRUE
, relocate
= TRUE
;
4008 outrel
.r_offset
+= (input_section
->output_section
->vma
4009 + input_section
->output_offset
);
4012 memset (&outrel
, 0, sizeof outrel
);
4013 else if (r_type
== R_SH_REL32
)
4015 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
4016 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
4018 = (howto
->partial_inplace
4019 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4024 || ((info
->symbolic
|| h
->dynindx
== -1)
4025 && h
->def_regular
)))
4029 BFD_ASSERT (sec
!= NULL
);
4030 BFD_ASSERT (sec
->output_section
!= NULL
);
4031 dynindx
= elf_section_data (sec
->output_section
)->dynindx
;
4032 outrel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
4033 outrel
.r_addend
= relocation
;
4035 += (howto
->partial_inplace
4036 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4038 outrel
.r_addend
-= sec
->output_section
->vma
;
4042 /* h->dynindx may be -1 if this symbol was marked to
4045 || ((info
->symbolic
|| h
->dynindx
== -1)
4048 relocate
= howto
->partial_inplace
;
4049 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4053 BFD_ASSERT (h
->dynindx
!= -1);
4054 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
4056 outrel
.r_addend
= relocation
;
4058 += (howto
->partial_inplace
4059 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4063 loc
= sreloc
->contents
;
4064 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4065 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4067 check_segment
[0] = check_segment
[1] = -1;
4069 /* If this reloc is against an external symbol, we do
4070 not want to fiddle with the addend. Otherwise, we
4071 need to include the symbol value so that it becomes
4072 an addend for the dynamic reloc. */
4076 else if (fdpic_p
&& !bfd_link_pic (info
)
4077 && r_type
== R_SH_DIR32
4078 && (input_section
->flags
& SEC_ALLOC
) != 0)
4084 if (sh_elf_osec_readonly_p (output_bfd
,
4085 input_section
->output_section
))
4088 /* xgettext:c-format */
4089 (_("%pB(%pA+%#" PRIx64
"): "
4090 "cannot emit fixup to `%s' in read-only section"),
4093 (uint64_t) rel
->r_offset
,
4098 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4099 input_section
, rel
->r_offset
);
4100 if (offset
!= (bfd_vma
)-1)
4101 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4102 input_section
->output_section
->vma
4103 + input_section
->output_offset
4106 check_segment
[0] = check_segment
[1] = -1;
4108 /* We don't want warnings for non-NULL tests on undefined weak
4110 else if (r_type
== R_SH_REL32
4112 && h
->root
.type
== bfd_link_hash_undefweak
)
4113 check_segment
[0] = check_segment
[1] = -1;
4114 goto final_link_relocate
;
4117 /* Relocation is to the entry for this symbol in the
4118 procedure linkage table. */
4122 || ! bfd_link_pic (info
)
4125 || h
->plt
.offset
== (bfd_vma
) -1
4126 || h
->got
.offset
!= (bfd_vma
) -1)
4129 /* Relocation is to the entry for this symbol in the global
4130 offset table extension for the procedure linkage table. */
4133 BFD_ASSERT (sgotplt
!= NULL
);
4134 relocation
= (sgotplt
->output_offset
4135 + (get_plt_index (htab
->plt_info
, h
->plt
.offset
)
4139 relocation
-= GOT_BIAS
;
4142 goto final_link_relocate
;
4147 /* Relocation is to the entry for this symbol in the global
4151 BFD_ASSERT (sgot
!= NULL
);
4152 check_segment
[0] = check_segment
[1] = -1;
4158 off
= h
->got
.offset
;
4159 BFD_ASSERT (off
!= (bfd_vma
) -1);
4161 dyn
= htab
->root
.dynamic_sections_created
;
4162 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
4163 bfd_link_pic (info
),
4165 || (bfd_link_pic (info
)
4166 && SYMBOL_REFERENCES_LOCAL (info
, h
))
4167 || ((ELF_ST_VISIBILITY (h
->other
)
4168 || resolved_to_zero
)
4169 && h
->root
.type
== bfd_link_hash_undefweak
))
4171 /* This is actually a static link, or it is a
4172 -Bsymbolic link and the symbol is defined
4173 locally, or the symbol was forced to be local
4174 because of a version file. We must initialize
4175 this entry in the global offset table. Since the
4176 offset must always be a multiple of 4, we use the
4177 least significant bit to record whether we have
4178 initialized it already.
4180 When doing a dynamic link, we create a .rela.got
4181 relocation entry to initialize the value. This
4182 is done in the finish_dynamic_symbol routine. */
4187 bfd_put_32 (output_bfd
, relocation
,
4188 sgot
->contents
+ off
);
4191 /* If we initialize the GOT entry here with a valid
4192 symbol address, also add a fixup. */
4193 if (fdpic_p
&& !bfd_link_pic (info
)
4194 && sh_elf_hash_entry (h
)->got_type
== GOT_NORMAL
4195 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4196 || h
->root
.type
!= bfd_link_hash_undefweak
))
4197 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4198 sgot
->output_section
->vma
4199 + sgot
->output_offset
4204 relocation
= sh_elf_got_offset (htab
) + off
;
4208 BFD_ASSERT (local_got_offsets
!= NULL
4209 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
4211 off
= local_got_offsets
[r_symndx
];
4213 /* The offset must always be a multiple of 4. We use
4214 the least significant bit to record whether we have
4215 already generated the necessary reloc. */
4220 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
4222 if (bfd_link_pic (info
))
4224 Elf_Internal_Rela outrel
;
4227 outrel
.r_offset
= (sgot
->output_section
->vma
4228 + sgot
->output_offset
4233 = elf_section_data (sec
->output_section
)->dynindx
;
4234 outrel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
4235 outrel
.r_addend
= relocation
;
4236 outrel
.r_addend
-= sec
->output_section
->vma
;
4240 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4241 outrel
.r_addend
= relocation
;
4243 loc
= srelgot
->contents
;
4244 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4245 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4248 && (sh_elf_local_got_type (input_bfd
) [r_symndx
]
4250 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4251 sgot
->output_section
->vma
4252 + sgot
->output_offset
4255 local_got_offsets
[r_symndx
] |= 1;
4258 relocation
= sh_elf_got_offset (htab
) + off
;
4262 relocation
-= GOT_BIAS
;
4265 if (r_type
== R_SH_GOT20
)
4267 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4268 input_bfd
, input_section
, contents
,
4273 goto final_link_relocate
;
4277 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4278 we place at the start of the .got.plt section. This is the same
4279 as the start of the output .got section, unless there are function
4280 descriptors in front of it. */
4282 BFD_ASSERT (sgotplt
!= NULL
);
4283 check_segment
[0] = got_segment
;
4284 relocation
-= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4285 + htab
->root
.hgot
->root
.u
.def
.value
;
4288 relocation
-= GOT_BIAS
;
4291 addend
= rel
->r_addend
;
4293 if (r_type
== R_SH_GOTOFF20
)
4295 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4296 input_bfd
, input_section
, contents
,
4301 goto final_link_relocate
;
4304 /* Use global offset table as symbol value. */
4306 BFD_ASSERT (sgotplt
!= NULL
);
4307 relocation
= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
;
4310 relocation
+= GOT_BIAS
;
4313 addend
= rel
->r_addend
;
4315 goto final_link_relocate
;
4318 /* Relocation is to the entry for this symbol in the
4319 procedure linkage table. */
4321 /* Resolve a PLT reloc against a local symbol directly,
4322 without using the procedure linkage table. */
4324 goto final_link_relocate
;
4326 /* We don't want to warn on calls to undefined weak symbols,
4327 as calls to them must be protected by non-NULL tests
4328 anyway, and unprotected calls would invoke undefined
4330 if (h
->root
.type
== bfd_link_hash_undefweak
)
4331 check_segment
[0] = check_segment
[1] = -1;
4333 if (h
->forced_local
)
4334 goto final_link_relocate
;
4336 if (h
->plt
.offset
== (bfd_vma
) -1)
4338 /* We didn't make a PLT entry for this symbol. This
4339 happens when statically linking PIC code, or when
4340 using -Bsymbolic. */
4341 goto final_link_relocate
;
4344 BFD_ASSERT (splt
!= NULL
);
4345 check_segment
[1] = plt_segment
;
4346 relocation
= (splt
->output_section
->vma
4347 + splt
->output_offset
4350 addend
= rel
->r_addend
;
4352 goto final_link_relocate
;
4354 /* Relocation is to the canonical function descriptor for this
4355 symbol, possibly via the GOT. Initialize the GOT
4356 entry and function descriptor if necessary. */
4357 case R_SH_GOTFUNCDESC
:
4358 case R_SH_GOTFUNCDESC20
:
4362 asection
*reloc_section
;
4363 bfd_vma reloc_offset
;
4364 int reloc_type
= R_SH_FUNCDESC
;
4368 check_segment
[0] = check_segment
[1] = -1;
4370 /* FIXME: See what FRV does for global symbols in the
4371 executable, with --export-dynamic. Do they need ld.so
4372 to allocate official descriptors? See what this code
4378 if (r_type
== R_SH_FUNCDESC
)
4380 reloc_section
= input_section
;
4381 reloc_offset
= rel
->r_offset
;
4385 reloc_section
= sgot
;
4388 reloc_offset
= h
->got
.offset
;
4391 BFD_ASSERT (local_got_offsets
!= NULL
);
4392 reloc_offset
= local_got_offsets
[r_symndx
];
4394 BFD_ASSERT (reloc_offset
!= MINUS_ONE
);
4396 if (reloc_offset
& 1)
4399 goto funcdesc_done_got
;
4403 if (h
&& h
->root
.type
== bfd_link_hash_undefweak
4404 && (SYMBOL_CALLS_LOCAL (info
, h
)
4405 || !htab
->root
.dynamic_sections_created
))
4406 /* Undefined weak symbol which will not be dynamically
4407 resolved later; leave it at zero. */
4408 goto funcdesc_leave_zero
;
4409 else if (SYMBOL_CALLS_LOCAL (info
, h
)
4410 && ! SYMBOL_FUNCDESC_LOCAL (info
, h
))
4412 /* If the symbol needs a non-local function descriptor
4413 but binds locally (i.e., its visibility is
4414 protected), emit a dynamic relocation decayed to
4415 section+offset. This is an optimization; the dynamic
4416 linker would resolve our function descriptor request
4417 to our copy of the function anyway. */
4418 dynindx
= elf_section_data (h
->root
.u
.def
.section
4419 ->output_section
)->dynindx
;
4420 relocation
+= h
->root
.u
.def
.section
->output_offset
4421 + h
->root
.u
.def
.value
;
4423 else if (! SYMBOL_FUNCDESC_LOCAL (info
, h
))
4425 /* If the symbol is dynamic and there will be dynamic
4426 symbol resolution because we are or are linked with a
4427 shared library, emit a FUNCDESC relocation such that
4428 the dynamic linker will allocate the function
4430 BFD_ASSERT (h
->dynindx
!= -1);
4431 dynindx
= h
->dynindx
;
4437 /* Otherwise, we know we have a private function
4438 descriptor, so reference it directly. */
4439 reloc_type
= R_SH_DIR32
;
4440 dynindx
= elf_section_data (htab
->sfuncdesc
4441 ->output_section
)->dynindx
;
4445 offset
= sh_elf_hash_entry (h
)->funcdesc
.offset
;
4446 BFD_ASSERT (offset
!= MINUS_ONE
);
4447 if ((offset
& 1) == 0)
4449 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, h
,
4452 sh_elf_hash_entry (h
)->funcdesc
.offset
|= 1;
4457 union gotref
*local_funcdesc
;
4459 local_funcdesc
= sh_elf_local_funcdesc (input_bfd
);
4460 offset
= local_funcdesc
[r_symndx
].offset
;
4461 BFD_ASSERT (offset
!= MINUS_ONE
);
4462 if ((offset
& 1) == 0)
4464 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, NULL
,
4468 local_funcdesc
[r_symndx
].offset
|= 1;
4472 relocation
= htab
->sfuncdesc
->output_offset
+ (offset
& ~1);
4475 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
4479 if (sh_elf_osec_readonly_p (output_bfd
,
4480 reloc_section
->output_section
))
4483 /* xgettext:c-format */
4484 (_("%pB(%pA+%#" PRIx64
"): "
4485 "cannot emit fixup to `%s' in read-only section"),
4488 (uint64_t) rel
->r_offset
,
4493 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4494 reloc_section
, reloc_offset
);
4496 if (offset
!= (bfd_vma
)-1)
4497 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4499 + reloc_section
->output_section
->vma
4500 + reloc_section
->output_offset
);
4502 else if ((reloc_section
->output_section
->flags
4503 & (SEC_ALLOC
| SEC_LOAD
)) == (SEC_ALLOC
| SEC_LOAD
))
4507 if (sh_elf_osec_readonly_p (output_bfd
,
4508 reloc_section
->output_section
))
4510 info
->callbacks
->warning
4512 _("cannot emit dynamic relocations in read-only section"),
4513 symname
, input_bfd
, reloc_section
, reloc_offset
);
4517 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4518 reloc_section
, reloc_offset
);
4520 if (offset
!= (bfd_vma
)-1)
4521 sh_elf_add_dyn_reloc (output_bfd
, srelgot
,
4523 + reloc_section
->output_section
->vma
4524 + reloc_section
->output_offset
,
4525 reloc_type
, dynindx
, relocation
);
4527 if (r_type
== R_SH_FUNCDESC
)
4535 goto funcdesc_leave_zero
;
4539 if (SYMBOL_FUNCDESC_LOCAL (info
, h
))
4540 relocation
+= htab
->sfuncdesc
->output_section
->vma
;
4541 funcdesc_leave_zero
:
4542 if (r_type
!= R_SH_FUNCDESC
)
4544 bfd_put_32 (output_bfd
, relocation
,
4545 reloc_section
->contents
+ reloc_offset
);
4549 local_got_offsets
[r_symndx
] |= 1;
4553 relocation
= sh_elf_got_offset (htab
) + reloc_offset
;
4555 relocation
-= GOT_BIAS
;
4558 if (r_type
== R_SH_GOTFUNCDESC20
)
4560 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4561 input_bfd
, input_section
, contents
,
4566 goto final_link_relocate
;
4570 case R_SH_GOTOFFFUNCDESC
:
4571 case R_SH_GOTOFFFUNCDESC20
:
4572 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4573 executable and --export-dynamic. If such symbols get
4574 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
4578 check_segment
[0] = check_segment
[1] = -1;
4580 addend
= rel
->r_addend
;
4582 if (h
&& (h
->root
.type
== bfd_link_hash_undefweak
4583 || !SYMBOL_FUNCDESC_LOCAL (info
, h
)))
4586 /* xgettext:c-format */
4587 (_("%pB(%pA+%#" PRIx64
"): "
4588 "%s relocation against external symbol \"%s\""),
4589 input_bfd
, input_section
, (uint64_t) rel
->r_offset
,
4590 howto
->name
, h
->root
.root
.string
);
4597 /* Otherwise, we know we have a private function
4598 descriptor, so reference it directly. */
4601 offset
= sh_elf_hash_entry (h
)->funcdesc
.offset
;
4602 BFD_ASSERT (offset
!= MINUS_ONE
);
4603 if ((offset
& 1) == 0)
4605 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, h
,
4608 sh_elf_hash_entry (h
)->funcdesc
.offset
|= 1;
4613 union gotref
*local_funcdesc
;
4615 local_funcdesc
= sh_elf_local_funcdesc (input_bfd
);
4616 offset
= local_funcdesc
[r_symndx
].offset
;
4617 BFD_ASSERT (offset
!= MINUS_ONE
);
4618 if ((offset
& 1) == 0)
4620 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, NULL
,
4624 local_funcdesc
[r_symndx
].offset
|= 1;
4628 relocation
= htab
->sfuncdesc
->output_offset
+ (offset
& ~1);
4631 relocation
-= (htab
->root
.hgot
->root
.u
.def
.value
4632 + sgotplt
->output_offset
);
4634 relocation
-= GOT_BIAS
;
4637 if (r_type
== R_SH_GOTOFFFUNCDESC20
)
4639 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4640 input_bfd
, input_section
, contents
,
4645 goto final_link_relocate
;
4647 case R_SH_LOOP_START
:
4649 static bfd_vma start
, end
;
4651 start
= (relocation
+ rel
->r_addend
4652 - (sec
->output_section
->vma
+ sec
->output_offset
));
4653 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
4654 rel
->r_offset
, sec
, start
, end
);
4658 end
= (relocation
+ rel
->r_addend
4659 - (sec
->output_section
->vma
+ sec
->output_offset
));
4660 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
4661 rel
->r_offset
, sec
, start
, end
);
4665 case R_SH_TLS_GD_32
:
4666 case R_SH_TLS_IE_32
:
4668 check_segment
[0] = check_segment
[1] = -1;
4669 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
4670 got_type
= GOT_UNKNOWN
;
4671 if (h
== NULL
&& local_got_offsets
)
4672 got_type
= sh_elf_local_got_type (input_bfd
) [r_symndx
];
4675 got_type
= sh_elf_hash_entry (h
)->got_type
;
4676 if (! bfd_link_pic (info
)
4677 && (h
->dynindx
== -1
4679 r_type
= R_SH_TLS_LE_32
;
4682 if (r_type
== R_SH_TLS_GD_32
&& got_type
== GOT_TLS_IE
)
4683 r_type
= R_SH_TLS_IE_32
;
4685 if (r_type
== R_SH_TLS_LE_32
)
4688 unsigned short insn
;
4690 if (ELF32_R_TYPE (rel
->r_info
) == R_SH_TLS_GD_32
)
4692 /* GD->LE transition:
4693 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4694 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4695 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4697 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
4699 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
4701 offset
= rel
->r_offset
;
4705 /* xgettext:c-format */
4706 (_("%pB(%pA): offset in relocation for GD->LE translation is too small: %#" PRIx64
),
4707 input_bfd
, input_section
, (uint64_t) offset
);
4711 /* Size of GD instructions is 16 or 18. */
4713 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4714 if ((insn
& 0xff00) == 0xc700)
4716 BFD_ASSERT (offset
>= 2);
4718 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4721 if ((insn
& 0xff00) != 0xd400)
4723 /* xgettext:c-format */ /* The backslash is to prevent bogus trigraph detection. */
4724 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd4?\?)"),
4725 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4727 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4729 if ((insn
& 0xff00) != 0xc700)
4731 /* xgettext:c-format */
4732 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xc7?\?)"),
4733 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4735 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4736 if ((insn
& 0xff00) != 0xd100)
4738 /* xgettext:c-format */
4739 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd1?\?)"),
4740 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4742 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4745 /* xgettext:c-format */
4746 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x310c)"),
4747 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4749 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4752 /* xgettext:c-format */
4753 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x410b)"),
4754 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4756 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4759 /* xgettext:c-format */
4760 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x34cc)"),
4761 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4763 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 2);
4764 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 4);
4765 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
4766 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4767 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4773 /* IE->LE transition:
4790 offset
= rel
->r_offset
;
4794 /* xgettext:c-format */
4795 (_("%pB(%pA): offset in relocation for IE->LE translation is too small: %#" PRIx64
),
4796 input_bfd
, input_section
, (uint64_t) offset
);
4800 /* Size of IE instructions is 10 or 12. */
4802 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4803 if ((insn
& 0xf0ff) == 0x0012)
4805 BFD_ASSERT (offset
>= 2);
4807 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4810 if ((insn
& 0xff00) != 0xd000)
4812 /* xgettext:c-format */
4813 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd0??: mov.l)"),
4814 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4816 target
= insn
& 0x00ff;
4818 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4819 if ((insn
& 0xf0ff) != 0x0012)
4821 /* xgettext:c-format */
4822 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x0?12: stc)"),
4823 input_bfd
, input_section
, (uint64_t) (offset
+ 2), (int) insn
);
4825 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4826 if ((insn
& 0xf0ff) != 0x00ce)
4828 /* xgettext:c-format */
4829 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x0?ce: mov.l)"),
4830 input_bfd
, input_section
, (uint64_t) (offset
+ 4), (int) insn
);
4832 insn
= 0xd000 | (insn
& 0x0f00) | target
;
4833 bfd_put_16 (output_bfd
, insn
, contents
+ offset
+ 0);
4834 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4837 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4838 contents
+ rel
->r_offset
);
4842 if (sgot
== NULL
|| sgotplt
== NULL
)
4846 off
= h
->got
.offset
;
4849 if (local_got_offsets
== NULL
)
4852 off
= local_got_offsets
[r_symndx
];
4855 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4856 if (r_type
== R_SH_TLS_IE_32
4857 && ! htab
->root
.dynamic_sections_created
)
4860 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4861 sgot
->contents
+ off
);
4862 bfd_put_32 (output_bfd
, sh_elf_got_offset (htab
) + off
,
4863 contents
+ rel
->r_offset
);
4871 Elf_Internal_Rela outrel
;
4875 outrel
.r_offset
= (sgot
->output_section
->vma
4876 + sgot
->output_offset
+ off
);
4878 if (h
== NULL
|| h
->dynindx
== -1)
4883 dr_type
= (r_type
== R_SH_TLS_GD_32
? R_SH_TLS_DTPMOD32
:
4885 if (dr_type
== R_SH_TLS_TPOFF32
&& indx
== 0)
4886 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4888 outrel
.r_addend
= 0;
4889 outrel
.r_info
= ELF32_R_INFO (indx
, dr_type
);
4890 loc
= srelgot
->contents
;
4891 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4892 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4894 if (r_type
== R_SH_TLS_GD_32
)
4898 bfd_put_32 (output_bfd
,
4899 relocation
- dtpoff_base (info
),
4900 sgot
->contents
+ off
+ 4);
4904 outrel
.r_info
= ELF32_R_INFO (indx
,
4906 outrel
.r_offset
+= 4;
4907 outrel
.r_addend
= 0;
4908 srelgot
->reloc_count
++;
4909 loc
+= sizeof (Elf32_External_Rela
);
4910 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4917 local_got_offsets
[r_symndx
] |= 1;
4920 if (off
>= (bfd_vma
) -2)
4923 if (r_type
== (int) ELF32_R_TYPE (rel
->r_info
))
4924 relocation
= sh_elf_got_offset (htab
) + off
;
4928 unsigned short insn
;
4930 /* GD->IE transition:
4931 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4932 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4933 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4935 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4936 nop; nop; bra 3f; nop; .align 2;
4937 1: .long x@TPOFF; 2:...; 3:. */
4939 offset
= rel
->r_offset
;
4943 /* xgettext:c-format */
4944 (_("%pB(%pA): offset in relocation for GD->IE translation is too small: %#" PRIx64
),
4945 input_bfd
, input_section
, (uint64_t) offset
);
4949 /* Size of GD instructions is 16 or 18. */
4951 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4952 if ((insn
& 0xff00) == 0xc700)
4954 BFD_ASSERT (offset
>= 2);
4956 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4959 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4961 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4962 bfd_put_16 (output_bfd
, insn
& 0xf0ff, contents
+ offset
);
4964 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4965 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4966 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4967 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4968 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4969 BFD_ASSERT (insn
== 0x310c);
4970 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4971 BFD_ASSERT (insn
== 0x410b);
4972 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4973 BFD_ASSERT (insn
== 0x34cc);
4975 bfd_put_16 (output_bfd
, 0x0412, contents
+ offset
+ 2);
4976 bfd_put_16 (output_bfd
, 0x00ce, contents
+ offset
+ 4);
4977 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 6);
4978 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4979 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4981 bfd_put_32 (output_bfd
, sh_elf_got_offset (htab
) + off
,
4982 contents
+ rel
->r_offset
);
4987 addend
= rel
->r_addend
;
4989 goto final_link_relocate
;
4991 case R_SH_TLS_LD_32
:
4993 check_segment
[0] = check_segment
[1] = -1;
4994 if (! bfd_link_pic (info
))
4997 unsigned short insn
;
4999 /* LD->LE transition:
5000 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5001 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5002 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5004 stc gbr,r0; nop; nop; nop;
5005 nop; nop; bra 3f; ...; 3:. */
5007 offset
= rel
->r_offset
;
5011 /* xgettext:c-format */
5012 (_("%pB(%pA): offset in relocation for LD->LE translation is too small: %#" PRIx64
),
5013 input_bfd
, input_section
, (uint64_t) offset
);
5017 /* Size of LD instructions is 16 or 18. */
5019 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5020 if ((insn
& 0xff00) == 0xc700)
5022 BFD_ASSERT (offset
>= 2);
5024 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5027 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
5028 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
5029 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
5030 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
5031 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
5032 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
5033 BFD_ASSERT (insn
== 0x310c);
5034 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
5035 BFD_ASSERT (insn
== 0x410b);
5036 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
5037 BFD_ASSERT (insn
== 0x34cc);
5039 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 0);
5040 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 2);
5041 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
5042 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
5043 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
5044 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
5049 if (sgot
== NULL
|| sgotplt
== NULL
)
5052 off
= htab
->tls_ldm_got
.offset
;
5057 Elf_Internal_Rela outrel
;
5060 outrel
.r_offset
= (sgot
->output_section
->vma
5061 + sgot
->output_offset
+ off
);
5062 outrel
.r_addend
= 0;
5063 outrel
.r_info
= ELF32_R_INFO (0, R_SH_TLS_DTPMOD32
);
5064 loc
= srelgot
->contents
;
5065 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5066 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5067 htab
->tls_ldm_got
.offset
|= 1;
5070 relocation
= sh_elf_got_offset (htab
) + off
;
5071 addend
= rel
->r_addend
;
5073 goto final_link_relocate
;
5075 case R_SH_TLS_LDO_32
:
5076 check_segment
[0] = check_segment
[1] = -1;
5077 if (! bfd_link_pic (info
))
5078 relocation
= tpoff (info
, relocation
);
5080 relocation
-= dtpoff_base (info
);
5082 addend
= rel
->r_addend
;
5083 goto final_link_relocate
;
5085 case R_SH_TLS_LE_32
:
5088 Elf_Internal_Rela outrel
;
5091 check_segment
[0] = check_segment
[1] = -1;
5093 if (!bfd_link_dll (info
))
5095 relocation
= tpoff (info
, relocation
);
5096 addend
= rel
->r_addend
;
5097 goto final_link_relocate
;
5102 sreloc
= _bfd_elf_get_dynamic_reloc_section
5103 (input_bfd
, input_section
, /*rela?*/ TRUE
);
5108 if (h
== NULL
|| h
->dynindx
== -1)
5113 outrel
.r_offset
= (input_section
->output_section
->vma
5114 + input_section
->output_offset
5116 outrel
.r_info
= ELF32_R_INFO (indx
, R_SH_TLS_TPOFF32
);
5118 outrel
.r_addend
= relocation
- dtpoff_base (info
);
5120 outrel
.r_addend
= 0;
5122 loc
= sreloc
->contents
;
5123 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5124 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5130 if (fdpic_p
&& check_segment
[0] != (unsigned) -1
5131 && check_segment
[0] != check_segment
[1])
5133 /* We don't want duplicate errors for undefined symbols. */
5134 if (!h
|| h
->root
.type
!= bfd_link_hash_undefined
)
5136 if (bfd_link_pic (info
))
5138 info
->callbacks
->einfo
5139 /* xgettext:c-format */
5140 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5141 input_bfd
, input_section
, rel
->r_offset
, symname
);
5145 info
->callbacks
->einfo
5146 /* xgettext:c-format */
5147 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5148 input_bfd
, input_section
, rel
->r_offset
, symname
);
5151 elf_elfheader (output_bfd
)->e_flags
|= EF_SH_PIC
;
5154 if (r
!= bfd_reloc_ok
)
5159 case bfd_reloc_outofrange
:
5161 case bfd_reloc_overflow
:
5169 name
= (bfd_elf_string_from_elf_section
5170 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
5174 name
= bfd_section_name (sec
);
5176 (*info
->callbacks
->reloc_overflow
)
5177 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
5178 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
5188 /* This is a version of bfd_generic_get_relocated_section_contents
5189 which uses sh_elf_relocate_section. */
5192 sh_elf_get_relocated_section_contents (bfd
*output_bfd
,
5193 struct bfd_link_info
*link_info
,
5194 struct bfd_link_order
*link_order
,
5196 bfd_boolean relocatable
,
5199 Elf_Internal_Shdr
*symtab_hdr
;
5200 asection
*input_section
= link_order
->u
.indirect
.section
;
5201 bfd
*input_bfd
= input_section
->owner
;
5202 asection
**sections
= NULL
;
5203 Elf_Internal_Rela
*internal_relocs
= NULL
;
5204 Elf_Internal_Sym
*isymbuf
= NULL
;
5206 /* We only need to handle the case of relaxing, or of having a
5207 particular set of section contents, specially. */
5209 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
5210 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
5215 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
5217 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
5218 (size_t) input_section
->size
);
5220 if ((input_section
->flags
& SEC_RELOC
) != 0
5221 && input_section
->reloc_count
> 0)
5224 Elf_Internal_Sym
*isym
, *isymend
;
5227 internal_relocs
= (_bfd_elf_link_read_relocs
5228 (input_bfd
, input_section
, NULL
,
5229 (Elf_Internal_Rela
*) NULL
, FALSE
));
5230 if (internal_relocs
== NULL
)
5233 if (symtab_hdr
->sh_info
!= 0)
5235 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
5236 if (isymbuf
== NULL
)
5237 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
5238 symtab_hdr
->sh_info
, 0,
5240 if (isymbuf
== NULL
)
5244 amt
= symtab_hdr
->sh_info
;
5245 amt
*= sizeof (asection
*);
5246 sections
= (asection
**) bfd_malloc (amt
);
5247 if (sections
== NULL
&& amt
!= 0)
5250 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
5251 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
5255 if (isym
->st_shndx
== SHN_UNDEF
)
5256 isec
= bfd_und_section_ptr
;
5257 else if (isym
->st_shndx
== SHN_ABS
)
5258 isec
= bfd_abs_section_ptr
;
5259 else if (isym
->st_shndx
== SHN_COMMON
)
5260 isec
= bfd_com_section_ptr
;
5262 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
5267 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
5268 input_section
, data
, internal_relocs
,
5272 if (sections
!= NULL
)
5275 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
5277 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
5278 free (internal_relocs
);
5284 if (sections
!= NULL
)
5287 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
5289 if (internal_relocs
!= NULL
5290 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
5291 free (internal_relocs
);
5295 /* Return the base VMA address which should be subtracted from real addresses
5296 when resolving @dtpoff relocation.
5297 This is PT_TLS segment p_vaddr. */
5300 dtpoff_base (struct bfd_link_info
*info
)
5302 /* If tls_sec is NULL, we should have signalled an error already. */
5303 if (elf_hash_table (info
)->tls_sec
== NULL
)
5305 return elf_hash_table (info
)->tls_sec
->vma
;
5308 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5311 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
5313 /* If tls_sec is NULL, we should have signalled an error already. */
5314 if (elf_hash_table (info
)->tls_sec
== NULL
)
5316 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5317 structure which has 2 pointer fields. */
5318 return (address
- elf_hash_table (info
)->tls_sec
->vma
5319 + align_power ((bfd_vma
) 8,
5320 elf_hash_table (info
)->tls_sec
->alignment_power
));
5324 sh_elf_gc_mark_hook (asection
*sec
,
5325 struct bfd_link_info
*info
,
5326 Elf_Internal_Rela
*rel
,
5327 struct elf_link_hash_entry
*h
,
5328 Elf_Internal_Sym
*sym
)
5331 switch (ELF32_R_TYPE (rel
->r_info
))
5333 case R_SH_GNU_VTINHERIT
:
5334 case R_SH_GNU_VTENTRY
:
5338 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
5341 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5344 sh_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
5345 struct elf_link_hash_entry
*dir
,
5346 struct elf_link_hash_entry
*ind
)
5348 struct elf_sh_link_hash_entry
*edir
, *eind
;
5350 edir
= (struct elf_sh_link_hash_entry
*) dir
;
5351 eind
= (struct elf_sh_link_hash_entry
*) ind
;
5353 if (eind
->dyn_relocs
!= NULL
)
5355 if (edir
->dyn_relocs
!= NULL
)
5357 struct elf_dyn_relocs
**pp
;
5358 struct elf_dyn_relocs
*p
;
5360 /* Add reloc counts against the indirect sym to the direct sym
5361 list. Merge any entries against the same section. */
5362 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
5364 struct elf_dyn_relocs
*q
;
5366 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
5367 if (q
->sec
== p
->sec
)
5369 q
->pc_count
+= p
->pc_count
;
5370 q
->count
+= p
->count
;
5377 *pp
= edir
->dyn_relocs
;
5380 edir
->dyn_relocs
= eind
->dyn_relocs
;
5381 eind
->dyn_relocs
= NULL
;
5383 edir
->gotplt_refcount
= eind
->gotplt_refcount
;
5384 eind
->gotplt_refcount
= 0;
5385 edir
->funcdesc
.refcount
+= eind
->funcdesc
.refcount
;
5386 eind
->funcdesc
.refcount
= 0;
5387 edir
->abs_funcdesc_refcount
+= eind
->abs_funcdesc_refcount
;
5388 eind
->abs_funcdesc_refcount
= 0;
5390 if (ind
->root
.type
== bfd_link_hash_indirect
5391 && dir
->got
.refcount
<= 0)
5393 edir
->got_type
= eind
->got_type
;
5394 eind
->got_type
= GOT_UNKNOWN
;
5397 if (ind
->root
.type
!= bfd_link_hash_indirect
5398 && dir
->dynamic_adjusted
)
5400 /* If called to transfer flags for a weakdef during processing
5401 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5402 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5403 if (dir
->versioned
!= versioned_hidden
)
5404 dir
->ref_dynamic
|= ind
->ref_dynamic
;
5405 dir
->ref_regular
|= ind
->ref_regular
;
5406 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
5407 dir
->needs_plt
|= ind
->needs_plt
;
5410 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
5414 sh_elf_optimized_tls_reloc (struct bfd_link_info
*info
, int r_type
,
5417 if (bfd_link_pic (info
))
5422 case R_SH_TLS_GD_32
:
5423 case R_SH_TLS_IE_32
:
5425 return R_SH_TLS_LE_32
;
5426 return R_SH_TLS_IE_32
;
5427 case R_SH_TLS_LD_32
:
5428 return R_SH_TLS_LE_32
;
5434 /* Look through the relocs for a section during the first phase.
5435 Since we don't do .gots or .plts, we just need to consider the
5436 virtual table relocs for gc. */
5439 sh_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
5440 const Elf_Internal_Rela
*relocs
)
5442 Elf_Internal_Shdr
*symtab_hdr
;
5443 struct elf_link_hash_entry
**sym_hashes
;
5444 struct elf_sh_link_hash_table
*htab
;
5445 const Elf_Internal_Rela
*rel
;
5446 const Elf_Internal_Rela
*rel_end
;
5448 unsigned int r_type
;
5449 enum got_type got_type
, old_got_type
;
5453 if (bfd_link_relocatable (info
))
5456 /* Don't do anything special with non-loaded, non-alloced sections.
5457 In particular, any relocs in such sections should not affect GOT
5458 and PLT reference counting (ie. we don't allow them to create GOT
5459 or PLT entries), there's no possibility or desire to optimize TLS
5460 relocs, and there's not much point in propagating relocs to shared
5461 libs that the dynamic linker won't relocate. */
5462 if ((sec
->flags
& SEC_ALLOC
) == 0)
5465 BFD_ASSERT (is_sh_elf (abfd
));
5467 symtab_hdr
= &elf_symtab_hdr (abfd
);
5468 sym_hashes
= elf_sym_hashes (abfd
);
5470 htab
= sh_elf_hash_table (info
);
5474 rel_end
= relocs
+ sec
->reloc_count
;
5475 for (rel
= relocs
; rel
< rel_end
; rel
++)
5477 struct elf_link_hash_entry
*h
;
5478 unsigned long r_symndx
;
5480 r_symndx
= ELF32_R_SYM (rel
->r_info
);
5481 r_type
= ELF32_R_TYPE (rel
->r_info
);
5483 if (r_symndx
< symtab_hdr
->sh_info
)
5487 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
5488 while (h
->root
.type
== bfd_link_hash_indirect
5489 || h
->root
.type
== bfd_link_hash_warning
)
5490 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5493 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
5494 if (! bfd_link_pic (info
)
5495 && r_type
== R_SH_TLS_IE_32
5497 && h
->root
.type
!= bfd_link_hash_undefined
5498 && h
->root
.type
!= bfd_link_hash_undefweak
5499 && (h
->dynindx
== -1
5501 r_type
= R_SH_TLS_LE_32
;
5506 case R_SH_GOTOFFFUNCDESC
:
5507 case R_SH_GOTOFFFUNCDESC20
:
5509 case R_SH_GOTFUNCDESC
:
5510 case R_SH_GOTFUNCDESC20
:
5513 if (h
->dynindx
== -1)
5514 switch (ELF_ST_VISIBILITY (h
->other
))
5520 bfd_elf_link_record_dynamic_symbol (info
, h
);
5527 /* Some relocs require a global offset table. */
5528 if (htab
->root
.sgot
== NULL
)
5533 /* This may require an rofixup. */
5543 case R_SH_GOTFUNCDESC
:
5544 case R_SH_GOTFUNCDESC20
:
5545 case R_SH_GOTOFFFUNCDESC
:
5546 case R_SH_GOTOFFFUNCDESC20
:
5548 case R_SH_TLS_GD_32
:
5549 case R_SH_TLS_LD_32
:
5550 case R_SH_TLS_IE_32
:
5551 if (htab
->root
.dynobj
== NULL
)
5552 htab
->root
.dynobj
= abfd
;
5553 if (!create_got_section (htab
->root
.dynobj
, info
))
5564 /* This relocation describes the C++ object vtable hierarchy.
5565 Reconstruct it for later use during GC. */
5566 case R_SH_GNU_VTINHERIT
:
5567 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
5571 /* This relocation describes which C++ vtable entries are actually
5572 used. Record for later use during GC. */
5573 case R_SH_GNU_VTENTRY
:
5574 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
5578 case R_SH_TLS_IE_32
:
5579 if (bfd_link_pic (info
))
5580 info
->flags
|= DF_STATIC_TLS
;
5584 case R_SH_TLS_GD_32
:
5587 case R_SH_GOTFUNCDESC
:
5588 case R_SH_GOTFUNCDESC20
:
5592 got_type
= GOT_NORMAL
;
5594 case R_SH_TLS_GD_32
:
5595 got_type
= GOT_TLS_GD
;
5597 case R_SH_TLS_IE_32
:
5598 got_type
= GOT_TLS_IE
;
5600 case R_SH_GOTFUNCDESC
:
5601 case R_SH_GOTFUNCDESC20
:
5602 got_type
= GOT_FUNCDESC
;
5608 h
->got
.refcount
+= 1;
5609 old_got_type
= sh_elf_hash_entry (h
)->got_type
;
5613 bfd_signed_vma
*local_got_refcounts
;
5615 /* This is a global offset table entry for a local
5617 local_got_refcounts
= elf_local_got_refcounts (abfd
);
5618 if (local_got_refcounts
== NULL
)
5622 size
= symtab_hdr
->sh_info
;
5623 size
*= sizeof (bfd_signed_vma
);
5624 size
+= symtab_hdr
->sh_info
;
5625 local_got_refcounts
= ((bfd_signed_vma
*)
5626 bfd_zalloc (abfd
, size
));
5627 if (local_got_refcounts
== NULL
)
5629 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
5630 sh_elf_local_got_type (abfd
)
5631 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
5633 local_got_refcounts
[r_symndx
] += 1;
5634 old_got_type
= sh_elf_local_got_type (abfd
) [r_symndx
];
5637 /* If a TLS symbol is accessed using IE at least once,
5638 there is no point to use dynamic model for it. */
5639 if (old_got_type
!= got_type
&& old_got_type
!= GOT_UNKNOWN
5640 && (old_got_type
!= GOT_TLS_GD
|| got_type
!= GOT_TLS_IE
))
5642 if (old_got_type
== GOT_TLS_IE
&& got_type
== GOT_TLS_GD
)
5643 got_type
= GOT_TLS_IE
;
5646 if ((old_got_type
== GOT_FUNCDESC
|| got_type
== GOT_FUNCDESC
)
5647 && (old_got_type
== GOT_NORMAL
|| got_type
== GOT_NORMAL
))
5649 /* xgettext:c-format */
5650 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5651 abfd
, h
->root
.root
.string
);
5652 else if (old_got_type
== GOT_FUNCDESC
5653 || got_type
== GOT_FUNCDESC
)
5655 /* xgettext:c-format */
5656 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5657 abfd
, h
->root
.root
.string
);
5660 /* xgettext:c-format */
5661 (_("%pB: `%s' accessed both as normal and thread local symbol"),
5662 abfd
, h
->root
.root
.string
);
5667 if (old_got_type
!= got_type
)
5670 sh_elf_hash_entry (h
)->got_type
= got_type
;
5672 sh_elf_local_got_type (abfd
) [r_symndx
] = got_type
;
5677 case R_SH_TLS_LD_32
:
5678 sh_elf_hash_table(info
)->tls_ldm_got
.refcount
+= 1;
5682 case R_SH_GOTOFFFUNCDESC
:
5683 case R_SH_GOTOFFFUNCDESC20
:
5687 (_("%pB: Function descriptor relocation with non-zero addend"),
5694 union gotref
*local_funcdesc
;
5696 /* We need a function descriptor for a local symbol. */
5697 local_funcdesc
= sh_elf_local_funcdesc (abfd
);
5698 if (local_funcdesc
== NULL
)
5702 size
= symtab_hdr
->sh_info
* sizeof (union gotref
);
5703 local_funcdesc
= (union gotref
*) bfd_zalloc (abfd
, size
);
5704 if (local_funcdesc
== NULL
)
5706 sh_elf_local_funcdesc (abfd
) = local_funcdesc
;
5708 local_funcdesc
[r_symndx
].refcount
+= 1;
5710 if (r_type
== R_SH_FUNCDESC
)
5712 if (!bfd_link_pic (info
))
5713 htab
->srofixup
->size
+= 4;
5715 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5720 sh_elf_hash_entry (h
)->funcdesc
.refcount
++;
5721 if (r_type
== R_SH_FUNCDESC
)
5722 sh_elf_hash_entry (h
)->abs_funcdesc_refcount
++;
5724 /* If there is a function descriptor reference, then
5725 there should not be any non-FDPIC references. */
5726 old_got_type
= sh_elf_hash_entry (h
)->got_type
;
5727 if (old_got_type
!= GOT_FUNCDESC
&& old_got_type
!= GOT_UNKNOWN
)
5729 if (old_got_type
== GOT_NORMAL
)
5731 /* xgettext:c-format */
5732 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5733 abfd
, h
->root
.root
.string
);
5736 /* xgettext:c-format */
5737 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5738 abfd
, h
->root
.root
.string
);
5744 /* If this is a local symbol, we resolve it directly without
5745 creating a procedure linkage table entry. */
5749 || ! bfd_link_pic (info
)
5751 || h
->dynindx
== -1)
5755 h
->plt
.refcount
+= 1;
5756 ((struct elf_sh_link_hash_entry
*) h
)->gotplt_refcount
+= 1;
5761 /* This symbol requires a procedure linkage table entry. We
5762 actually build the entry in adjust_dynamic_symbol,
5763 because this might be a case of linking PIC code which is
5764 never referenced by a dynamic object, in which case we
5765 don't need to generate a procedure linkage table entry
5768 /* If this is a local symbol, we resolve it directly without
5769 creating a procedure linkage table entry. */
5773 if (h
->forced_local
)
5777 h
->plt
.refcount
+= 1;
5782 if (h
!= NULL
&& ! bfd_link_pic (info
))
5785 h
->plt
.refcount
+= 1;
5788 /* If we are creating a shared library, and this is a reloc
5789 against a global symbol, or a non PC relative reloc
5790 against a local symbol, then we need to copy the reloc
5791 into the shared library. However, if we are linking with
5792 -Bsymbolic, we do not need to copy a reloc against a
5793 global symbol which is defined in an object we are
5794 including in the link (i.e., DEF_REGULAR is set). At
5795 this point we have not seen all the input files, so it is
5796 possible that DEF_REGULAR is not set now but will be set
5797 later (it is never cleared). We account for that
5798 possibility below by storing information in the
5799 dyn_relocs field of the hash table entry. A similar
5800 situation occurs when creating shared libraries and symbol
5801 visibility changes render the symbol local.
5803 If on the other hand, we are creating an executable, we
5804 may need to keep relocations for symbols satisfied by a
5805 dynamic library if we manage to avoid copy relocs for the
5807 if ((bfd_link_pic (info
)
5808 && (sec
->flags
& SEC_ALLOC
) != 0
5809 && (r_type
!= R_SH_REL32
5811 && (! info
->symbolic
5812 || h
->root
.type
== bfd_link_hash_defweak
5813 || !h
->def_regular
))))
5814 || (! bfd_link_pic (info
)
5815 && (sec
->flags
& SEC_ALLOC
) != 0
5817 && (h
->root
.type
== bfd_link_hash_defweak
5818 || !h
->def_regular
)))
5820 struct elf_dyn_relocs
*p
;
5821 struct elf_dyn_relocs
**head
;
5823 if (htab
->root
.dynobj
== NULL
)
5824 htab
->root
.dynobj
= abfd
;
5826 /* When creating a shared object, we must copy these
5827 reloc types into the output file. We create a reloc
5828 section in dynobj and make room for this reloc. */
5831 sreloc
= _bfd_elf_make_dynamic_reloc_section
5832 (sec
, htab
->root
.dynobj
, 2, abfd
, /*rela?*/ TRUE
);
5838 /* If this is a global symbol, we count the number of
5839 relocations we need for this symbol. */
5841 head
= &((struct elf_sh_link_hash_entry
*) h
)->dyn_relocs
;
5844 /* Track dynamic relocs needed for local syms too. */
5847 Elf_Internal_Sym
*isym
;
5849 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
5854 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
5858 vpp
= &elf_section_data (s
)->local_dynrel
;
5859 head
= (struct elf_dyn_relocs
**) vpp
;
5863 if (p
== NULL
|| p
->sec
!= sec
)
5865 bfd_size_type amt
= sizeof (*p
);
5866 p
= bfd_alloc (htab
->root
.dynobj
, amt
);
5877 if (r_type
== R_SH_REL32
)
5881 /* Allocate the fixup regardless of whether we need a relocation.
5882 If we end up generating the relocation, we'll unallocate the
5884 if (htab
->fdpic_p
&& !bfd_link_pic (info
)
5885 && r_type
== R_SH_DIR32
5886 && (sec
->flags
& SEC_ALLOC
) != 0)
5887 htab
->srofixup
->size
+= 4;
5890 case R_SH_TLS_LE_32
:
5891 if (bfd_link_dll (info
))
5894 (_("%pB: TLS local exec code cannot be linked into shared objects"),
5901 case R_SH_TLS_LDO_32
:
5902 /* Nothing to do. */
5913 #ifndef sh_elf_set_mach_from_flags
5914 static unsigned int sh_ef_bfd_table
[] = { EF_SH_BFD_TABLE
};
5917 sh_elf_set_mach_from_flags (bfd
*abfd
)
5919 flagword flags
= elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
;
5921 if (flags
>= ARRAY_SIZE (sh_ef_bfd_table
))
5924 if (sh_ef_bfd_table
[flags
] == 0)
5927 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, sh_ef_bfd_table
[flags
]);
5933 /* Reverse table lookup for sh_ef_bfd_table[].
5934 Given a bfd MACH value from archures.c
5935 return the equivalent ELF flags from the table.
5936 Return -1 if no match is found. */
5939 sh_elf_get_flags_from_mach (unsigned long mach
)
5941 int i
= ARRAY_SIZE (sh_ef_bfd_table
) - 1;
5944 if (sh_ef_bfd_table
[i
] == mach
)
5947 /* shouldn't get here */
5952 #endif /* not sh_elf_set_mach_from_flags */
5954 #ifndef sh_elf_copy_private_data
5955 /* Copy backend specific data from one object module to another */
5958 sh_elf_copy_private_data (bfd
* ibfd
, bfd
* obfd
)
5960 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
5963 if (! _bfd_elf_copy_private_bfd_data (ibfd
, obfd
))
5966 return sh_elf_set_mach_from_flags (obfd
);
5968 #endif /* not sh_elf_copy_private_data */
5970 #ifndef sh_elf_merge_private_data
5972 /* This function returns the ELF architecture number that
5973 corresponds to the given arch_sh* flags. */
5976 sh_find_elf_flags (unsigned int arch_set
)
5978 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
5979 unsigned long bfd_mach
= sh_get_bfd_mach_from_arch_set (arch_set
);
5981 return sh_elf_get_flags_from_mach (bfd_mach
);
5984 /* Merge the architecture type of two BFD files, such that the
5985 resultant architecture supports all the features required
5986 by the two input BFDs.
5987 If the input BFDs are multually incompatible - i.e. one uses
5988 DSP while the other uses FPU - or there is no known architecture
5989 that fits the requirements then an error is emitted. */
5992 sh_merge_bfd_arch (bfd
*ibfd
, struct bfd_link_info
*info
)
5994 bfd
*obfd
= info
->output_bfd
;
5995 unsigned int old_arch
, new_arch
, merged_arch
;
5997 if (! _bfd_generic_verify_endian_match (ibfd
, info
))
6000 old_arch
= sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd
));
6001 new_arch
= sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd
));
6003 merged_arch
= SH_MERGE_ARCH_SET (old_arch
, new_arch
);
6005 if (!SH_VALID_CO_ARCH_SET (merged_arch
))
6008 /* xgettext:c-format */
6009 (_("%pB: uses %s instructions while previous modules "
6010 "use %s instructions"),
6012 SH_ARCH_SET_HAS_DSP (new_arch
) ? "dsp" : "floating point",
6013 SH_ARCH_SET_HAS_DSP (new_arch
) ? "floating point" : "dsp");
6014 bfd_set_error (bfd_error_bad_value
);
6017 else if (!SH_VALID_ARCH_SET (merged_arch
))
6020 /* xgettext:c-format */
6021 (_("internal error: merge of architecture '%s' with "
6022 "architecture '%s' produced unknown architecture"),
6023 bfd_printable_name (obfd
),
6024 bfd_printable_name (ibfd
));
6025 bfd_set_error (bfd_error_bad_value
);
6029 bfd_default_set_arch_mach (obfd
, bfd_arch_sh
,
6030 sh_get_bfd_mach_from_arch_set (merged_arch
));
6035 /* This routine initialises the elf flags when required and
6036 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
6039 sh_elf_merge_private_data (bfd
*ibfd
, struct bfd_link_info
*info
)
6041 bfd
*obfd
= info
->output_bfd
;
6043 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
6046 if (! elf_flags_init (obfd
))
6048 /* This happens when ld starts out with a 'blank' output file. */
6049 elf_flags_init (obfd
) = TRUE
;
6050 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
6051 sh_elf_set_mach_from_flags (obfd
);
6052 if (elf_elfheader (obfd
)->e_flags
& EF_SH_FDPIC
)
6053 elf_elfheader (obfd
)->e_flags
&= ~EF_SH_PIC
;
6056 if (! sh_merge_bfd_arch (ibfd
, info
))
6058 _bfd_error_handler (_("%pB: uses instructions which are incompatible "
6059 "with instructions used in previous modules"),
6061 bfd_set_error (bfd_error_bad_value
);
6065 elf_elfheader (obfd
)->e_flags
&= ~EF_SH_MACH_MASK
;
6066 elf_elfheader (obfd
)->e_flags
|=
6067 sh_elf_get_flags_from_mach (bfd_get_mach (obfd
));
6069 if (fdpic_object_p (ibfd
) != fdpic_object_p (obfd
))
6071 _bfd_error_handler (_("%pB: attempt to mix FDPIC and non-FDPIC objects"),
6073 bfd_set_error (bfd_error_bad_value
);
6079 #endif /* not sh_elf_merge_private_data */
6081 /* Override the generic function because we need to store sh_elf_obj_tdata
6082 as the specific tdata. We set also the machine architecture from flags
6086 sh_elf_object_p (bfd
*abfd
)
6088 if (! sh_elf_set_mach_from_flags (abfd
))
6091 return (((elf_elfheader (abfd
)->e_flags
& EF_SH_FDPIC
) != 0)
6092 == fdpic_object_p (abfd
));
6095 /* Finish up dynamic symbol handling. We set the contents of various
6096 dynamic sections here. */
6099 sh_elf_finish_dynamic_symbol (bfd
*output_bfd
, struct bfd_link_info
*info
,
6100 struct elf_link_hash_entry
*h
,
6101 Elf_Internal_Sym
*sym
)
6103 struct elf_sh_link_hash_table
*htab
;
6105 htab
= sh_elf_hash_table (info
);
6109 if (h
->plt
.offset
!= (bfd_vma
) -1)
6117 Elf_Internal_Rela rel
;
6119 const struct elf_sh_plt_info
*plt_info
;
6121 /* This symbol has an entry in the procedure linkage table. Set
6124 BFD_ASSERT (h
->dynindx
!= -1);
6126 splt
= htab
->root
.splt
;
6127 sgotplt
= htab
->root
.sgotplt
;
6128 srelplt
= htab
->root
.srelplt
;
6129 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srelplt
!= NULL
);
6131 /* Get the index in the procedure linkage table which
6132 corresponds to this symbol. This is the index of this symbol
6133 in all the symbols for which we are making plt entries. The
6134 first entry in the procedure linkage table is reserved. */
6135 plt_index
= get_plt_index (htab
->plt_info
, h
->plt
.offset
);
6137 plt_info
= htab
->plt_info
;
6138 if (plt_info
->short_plt
!= NULL
&& plt_index
<= MAX_SHORT_PLT
)
6139 plt_info
= plt_info
->short_plt
;
6141 /* Get the offset into the .got table of the entry that
6142 corresponds to this function. */
6144 /* The offset must be relative to the GOT symbol, twelve bytes
6145 before the end of .got.plt. Each descriptor is eight
6147 got_offset
= plt_index
* 8 + 12 - sgotplt
->size
;
6149 /* Each .got entry is 4 bytes. The first three are
6151 got_offset
= (plt_index
+ 3) * 4;
6154 if (bfd_link_pic (info
))
6155 got_offset
-= GOT_BIAS
;
6158 /* Fill in the entry in the procedure linkage table. */
6159 memcpy (splt
->contents
+ h
->plt
.offset
,
6160 plt_info
->symbol_entry
,
6161 plt_info
->symbol_entry_size
);
6163 if (bfd_link_pic (info
) || htab
->fdpic_p
)
6165 if (plt_info
->symbol_fields
.got20
)
6167 bfd_reloc_status_type r
;
6168 r
= install_movi20_field (output_bfd
, got_offset
,
6169 splt
->owner
, splt
, splt
->contents
,
6171 + plt_info
->symbol_fields
.got_entry
);
6172 BFD_ASSERT (r
== bfd_reloc_ok
);
6175 install_plt_field (output_bfd
, FALSE
, got_offset
,
6178 + plt_info
->symbol_fields
.got_entry
));
6182 BFD_ASSERT (!plt_info
->symbol_fields
.got20
);
6184 install_plt_field (output_bfd
, FALSE
,
6185 (sgotplt
->output_section
->vma
6186 + sgotplt
->output_offset
6190 + plt_info
->symbol_fields
.got_entry
));
6191 if (htab
->vxworks_p
)
6193 unsigned int reachable_plts
, plts_per_4k
;
6196 /* Divide the PLT into groups. The first group contains
6197 REACHABLE_PLTS entries and the other groups contain
6198 PLTS_PER_4K entries. Entries in the first group can
6199 branch directly to .plt; those in later groups branch
6200 to the last element of the previous group. */
6201 /* ??? It would be better to create multiple copies of
6202 the common resolver stub. */
6203 reachable_plts
= ((4096
6204 - plt_info
->plt0_entry_size
6205 - (plt_info
->symbol_fields
.plt
+ 4))
6206 / plt_info
->symbol_entry_size
) + 1;
6207 plts_per_4k
= (4096 / plt_info
->symbol_entry_size
);
6208 if (plt_index
< reachable_plts
)
6209 distance
= -(h
->plt
.offset
6210 + plt_info
->symbol_fields
.plt
);
6212 distance
= -(((plt_index
- reachable_plts
) % plts_per_4k
+ 1)
6213 * plt_info
->symbol_entry_size
);
6215 /* Install the 'bra' with this offset. */
6216 bfd_put_16 (output_bfd
,
6217 0xa000 | (0x0fff & ((distance
- 4) / 2)),
6220 + plt_info
->symbol_fields
.plt
));
6223 install_plt_field (output_bfd
, TRUE
,
6224 splt
->output_section
->vma
+ splt
->output_offset
,
6227 + plt_info
->symbol_fields
.plt
));
6230 /* Make got_offset relative to the start of .got.plt. */
6232 if (bfd_link_pic (info
))
6233 got_offset
+= GOT_BIAS
;
6236 got_offset
= plt_index
* 8;
6238 if (plt_info
->symbol_fields
.reloc_offset
!= MINUS_ONE
)
6239 install_plt_field (output_bfd
, FALSE
,
6240 plt_index
* sizeof (Elf32_External_Rela
),
6243 + plt_info
->symbol_fields
.reloc_offset
));
6245 /* Fill in the entry in the global offset table. */
6246 bfd_put_32 (output_bfd
,
6247 (splt
->output_section
->vma
6248 + splt
->output_offset
6250 + plt_info
->symbol_resolve_offset
),
6251 sgotplt
->contents
+ got_offset
);
6253 bfd_put_32 (output_bfd
,
6254 sh_elf_osec_to_segment (output_bfd
, splt
->output_section
),
6255 sgotplt
->contents
+ got_offset
+ 4);
6257 /* Fill in the entry in the .rela.plt section. */
6258 rel
.r_offset
= (sgotplt
->output_section
->vma
6259 + sgotplt
->output_offset
6262 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_FUNCDESC_VALUE
);
6264 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
6267 rel
.r_addend
= GOT_BIAS
;
6269 loc
= srelplt
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
6270 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6272 if (htab
->vxworks_p
&& !bfd_link_pic (info
))
6274 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6275 Begin by pointing LOC to the first such relocation. */
6276 loc
= (htab
->srelplt2
->contents
6277 + (plt_index
* 2 + 1) * sizeof (Elf32_External_Rela
));
6279 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6280 for the PLT entry's pointer to the .got.plt entry. */
6281 rel
.r_offset
= (splt
->output_section
->vma
6282 + splt
->output_offset
6284 + plt_info
->symbol_fields
.got_entry
);
6285 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
6286 rel
.r_addend
= got_offset
;
6287 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6288 loc
+= sizeof (Elf32_External_Rela
);
6290 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6291 the .got.plt entry, which initially points to .plt. */
6292 rel
.r_offset
= (sgotplt
->output_section
->vma
6293 + sgotplt
->output_offset
6295 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
, R_SH_DIR32
);
6297 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6300 if (!h
->def_regular
)
6302 /* Mark the symbol as undefined, rather than as defined in
6303 the .plt section. Leave the value alone. */
6304 sym
->st_shndx
= SHN_UNDEF
;
6308 if (h
->got
.offset
!= (bfd_vma
) -1
6309 && sh_elf_hash_entry (h
)->got_type
!= GOT_TLS_GD
6310 && sh_elf_hash_entry (h
)->got_type
!= GOT_TLS_IE
6311 && sh_elf_hash_entry (h
)->got_type
!= GOT_FUNCDESC
)
6315 Elf_Internal_Rela rel
;
6318 /* This symbol has an entry in the global offset table. Set it
6321 sgot
= htab
->root
.sgot
;
6322 srelgot
= htab
->root
.srelgot
;
6323 BFD_ASSERT (sgot
!= NULL
&& srelgot
!= NULL
);
6325 rel
.r_offset
= (sgot
->output_section
->vma
6326 + sgot
->output_offset
6327 + (h
->got
.offset
&~ (bfd_vma
) 1));
6329 /* If this is a static link, or it is a -Bsymbolic link and the
6330 symbol is defined locally or was forced to be local because
6331 of a version file, we just want to emit a RELATIVE reloc.
6332 The entry in the global offset table will already have been
6333 initialized in the relocate_section function. */
6334 if (bfd_link_pic (info
)
6335 && SYMBOL_REFERENCES_LOCAL (info
, h
))
6339 asection
*sec
= h
->root
.u
.def
.section
;
6341 = elf_section_data (sec
->output_section
)->dynindx
;
6343 rel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
6344 rel
.r_addend
= (h
->root
.u
.def
.value
6345 + h
->root
.u
.def
.section
->output_offset
);
6349 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
6350 rel
.r_addend
= (h
->root
.u
.def
.value
6351 + h
->root
.u
.def
.section
->output_section
->vma
6352 + h
->root
.u
.def
.section
->output_offset
);
6357 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
6358 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
6362 loc
= srelgot
->contents
;
6363 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6364 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6370 Elf_Internal_Rela rel
;
6373 /* This symbol needs a copy reloc. Set it up. */
6375 BFD_ASSERT (h
->dynindx
!= -1
6376 && (h
->root
.type
== bfd_link_hash_defined
6377 || h
->root
.type
== bfd_link_hash_defweak
));
6379 s
= bfd_get_linker_section (htab
->root
.dynobj
, ".rela.bss");
6380 BFD_ASSERT (s
!= NULL
);
6382 rel
.r_offset
= (h
->root
.u
.def
.value
6383 + h
->root
.u
.def
.section
->output_section
->vma
6384 + h
->root
.u
.def
.section
->output_offset
);
6385 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
6387 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6388 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6391 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
6392 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6394 if (h
== htab
->root
.hdynamic
6395 || (!htab
->vxworks_p
&& h
== htab
->root
.hgot
))
6396 sym
->st_shndx
= SHN_ABS
;
6401 /* Finish up the dynamic sections. */
6404 sh_elf_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6406 struct elf_sh_link_hash_table
*htab
;
6410 htab
= sh_elf_hash_table (info
);
6414 sgotplt
= htab
->root
.sgotplt
;
6415 sdyn
= bfd_get_linker_section (htab
->root
.dynobj
, ".dynamic");
6417 if (htab
->root
.dynamic_sections_created
)
6420 Elf32_External_Dyn
*dyncon
, *dynconend
;
6422 BFD_ASSERT (sgotplt
!= NULL
&& sdyn
!= NULL
);
6424 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
6425 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
6426 for (; dyncon
< dynconend
; dyncon
++)
6428 Elf_Internal_Dyn dyn
;
6431 bfd_elf32_swap_dyn_in (htab
->root
.dynobj
, dyncon
, &dyn
);
6437 && elf_vxworks_finish_dynamic_entry (output_bfd
, &dyn
))
6438 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6442 BFD_ASSERT (htab
->root
.hgot
!= NULL
);
6443 s
= htab
->root
.hgot
->root
.u
.def
.section
;
6444 dyn
.d_un
.d_ptr
= htab
->root
.hgot
->root
.u
.def
.value
6445 + s
->output_section
->vma
+ s
->output_offset
;
6446 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6450 s
= htab
->root
.srelplt
->output_section
;
6451 BFD_ASSERT (s
!= NULL
);
6452 dyn
.d_un
.d_ptr
= s
->vma
;
6453 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6457 s
= htab
->root
.srelplt
->output_section
;
6458 BFD_ASSERT (s
!= NULL
);
6459 dyn
.d_un
.d_val
= s
->size
;
6460 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6465 /* Fill in the first entry in the procedure linkage table. */
6466 splt
= htab
->root
.splt
;
6467 if (splt
&& splt
->size
> 0 && htab
->plt_info
->plt0_entry
)
6471 memcpy (splt
->contents
,
6472 htab
->plt_info
->plt0_entry
,
6473 htab
->plt_info
->plt0_entry_size
);
6474 for (i
= 0; i
< ARRAY_SIZE (htab
->plt_info
->plt0_got_fields
); i
++)
6475 if (htab
->plt_info
->plt0_got_fields
[i
] != MINUS_ONE
)
6476 install_plt_field (output_bfd
, FALSE
,
6477 (sgotplt
->output_section
->vma
6478 + sgotplt
->output_offset
6481 + htab
->plt_info
->plt0_got_fields
[i
]));
6483 if (htab
->vxworks_p
)
6485 /* Finalize the .rela.plt.unloaded contents. */
6486 Elf_Internal_Rela rel
;
6489 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
6490 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
6491 loc
= htab
->srelplt2
->contents
;
6492 rel
.r_offset
= (splt
->output_section
->vma
6493 + splt
->output_offset
6494 + htab
->plt_info
->plt0_got_fields
[2]);
6495 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
6497 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6498 loc
+= sizeof (Elf32_External_Rela
);
6500 /* Fix up the remaining .rela.plt.unloaded relocations.
6501 They may have the wrong symbol index for _G_O_T_ or
6502 _P_L_T_ depending on the order in which symbols were
6504 while (loc
< htab
->srelplt2
->contents
+ htab
->srelplt2
->size
)
6506 /* The PLT entry's pointer to the .got.plt slot. */
6507 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
6508 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
,
6510 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6511 loc
+= sizeof (Elf32_External_Rela
);
6513 /* The .got.plt slot's pointer to .plt. */
6514 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
6515 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
,
6517 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6518 loc
+= sizeof (Elf32_External_Rela
);
6522 /* UnixWare sets the entsize of .plt to 4, although that doesn't
6523 really seem like the right value. */
6524 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
6528 /* Fill in the first three entries in the global offset table. */
6529 if (sgotplt
&& sgotplt
->size
> 0 && !htab
->fdpic_p
)
6532 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
6534 bfd_put_32 (output_bfd
,
6535 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
6537 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
6538 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
6541 if (sgotplt
&& sgotplt
->size
> 0)
6542 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
6544 /* At the very end of the .rofixup section is a pointer to the GOT. */
6545 if (htab
->fdpic_p
&& htab
->srofixup
!= NULL
)
6547 struct elf_link_hash_entry
*hgot
= htab
->root
.hgot
;
6548 bfd_vma got_value
= hgot
->root
.u
.def
.value
6549 + hgot
->root
.u
.def
.section
->output_section
->vma
6550 + hgot
->root
.u
.def
.section
->output_offset
;
6552 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
, got_value
);
6554 /* Make sure we allocated and generated the same number of fixups. */
6555 BFD_ASSERT (htab
->srofixup
->reloc_count
* 4 == htab
->srofixup
->size
);
6558 if (htab
->srelfuncdesc
)
6559 BFD_ASSERT (htab
->srelfuncdesc
->reloc_count
* sizeof (Elf32_External_Rela
)
6560 == htab
->srelfuncdesc
->size
);
6562 if (htab
->root
.srelgot
)
6563 BFD_ASSERT (htab
->root
.srelgot
->reloc_count
* sizeof (Elf32_External_Rela
)
6564 == htab
->root
.srelgot
->size
);
6569 static enum elf_reloc_type_class
6570 sh_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6571 const asection
*rel_sec ATTRIBUTE_UNUSED
,
6572 const Elf_Internal_Rela
*rela
)
6574 switch ((int) ELF32_R_TYPE (rela
->r_info
))
6577 return reloc_class_relative
;
6579 return reloc_class_plt
;
6581 return reloc_class_copy
;
6583 return reloc_class_normal
;
6587 #if !defined SH_TARGET_ALREADY_DEFINED
6588 /* Support for Linux core dump NOTE sections. */
6591 elf32_shlin_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6596 switch (note
->descsz
)
6601 case 168: /* Linux/SH */
6603 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
6606 elf_tdata (abfd
)->core
->lwpid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
6615 /* Make a ".reg/999" section. */
6616 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6617 size
, note
->descpos
+ offset
);
6621 elf32_shlin_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6623 switch (note
->descsz
)
6628 case 124: /* Linux/SH elf_prpsinfo */
6629 elf_tdata (abfd
)->core
->program
6630 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
6631 elf_tdata (abfd
)->core
->command
6632 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
6635 /* Note that for some reason, a spurious space is tacked
6636 onto the end of the args in some (at least one anyway)
6637 implementations, so strip it off if it exists. */
6640 char *command
= elf_tdata (abfd
)->core
->command
;
6641 int n
= strlen (command
);
6643 if (0 < n
&& command
[n
- 1] == ' ')
6644 command
[n
- 1] = '\0';
6649 #endif /* not SH_TARGET_ALREADY_DEFINED */
6652 /* Return address for Ith PLT stub in section PLT, for relocation REL
6653 or (bfd_vma) -1 if it should not be included. */
6656 sh_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
6657 const arelent
*rel ATTRIBUTE_UNUSED
)
6659 const struct elf_sh_plt_info
*plt_info
;
6661 plt_info
= get_plt_info (plt
->owner
, (plt
->owner
->flags
& DYNAMIC
) != 0);
6662 return plt
->vma
+ get_plt_offset (plt_info
, i
);
6665 /* Decide whether to attempt to turn absptr or lsda encodings in
6666 shared libraries into pcrel within the given input section. */
6669 sh_elf_use_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
6670 struct bfd_link_info
*info
,
6671 asection
*eh_frame_section ATTRIBUTE_UNUSED
)
6673 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
6675 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
6682 /* Adjust the contents of an eh_frame_hdr section before they're output. */
6685 sh_elf_encode_eh_address (bfd
*abfd
,
6686 struct bfd_link_info
*info
,
6687 asection
*osec
, bfd_vma offset
,
6688 asection
*loc_sec
, bfd_vma loc_offset
,
6691 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
6692 struct elf_link_hash_entry
*h
;
6695 return _bfd_elf_encode_eh_address (abfd
, info
, osec
, offset
, loc_sec
,
6696 loc_offset
, encoded
);
6698 h
= htab
->root
.hgot
;
6699 BFD_ASSERT (h
&& h
->root
.type
== bfd_link_hash_defined
);
6701 if (! h
|| (sh_elf_osec_to_segment (abfd
, osec
)
6702 == sh_elf_osec_to_segment (abfd
, loc_sec
->output_section
)))
6703 return _bfd_elf_encode_eh_address (abfd
, info
, osec
, offset
,
6704 loc_sec
, loc_offset
, encoded
);
6706 BFD_ASSERT (sh_elf_osec_to_segment (abfd
, osec
)
6707 == (sh_elf_osec_to_segment
6708 (abfd
, h
->root
.u
.def
.section
->output_section
)));
6710 *encoded
= osec
->vma
+ offset
6711 - (h
->root
.u
.def
.value
6712 + h
->root
.u
.def
.section
->output_section
->vma
6713 + h
->root
.u
.def
.section
->output_offset
);
6715 return DW_EH_PE_datarel
| DW_EH_PE_sdata4
;
6718 #if !defined SH_TARGET_ALREADY_DEFINED
6719 #define TARGET_BIG_SYM sh_elf32_vec
6720 #define TARGET_BIG_NAME "elf32-sh"
6721 #define TARGET_LITTLE_SYM sh_elf32_le_vec
6722 #define TARGET_LITTLE_NAME "elf32-shl"
6725 #define ELF_ARCH bfd_arch_sh
6726 #define ELF_TARGET_ID SH_ELF_DATA
6727 #define ELF_MACHINE_CODE EM_SH
6728 #ifdef __QNXTARGET__
6729 #define ELF_MAXPAGESIZE 0x1000
6731 #define ELF_MAXPAGESIZE 0x80
6734 #define elf_symbol_leading_char '_'
6736 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6737 #define bfd_elf32_bfd_reloc_name_lookup \
6738 sh_elf_reloc_name_lookup
6739 #define elf_info_to_howto sh_elf_info_to_howto
6740 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
6741 #define elf_backend_relocate_section sh_elf_relocate_section
6742 #define bfd_elf32_bfd_get_relocated_section_contents \
6743 sh_elf_get_relocated_section_contents
6744 #define bfd_elf32_mkobject sh_elf_mkobject
6745 #define elf_backend_object_p sh_elf_object_p
6746 #define bfd_elf32_bfd_copy_private_bfd_data \
6747 sh_elf_copy_private_data
6748 #define bfd_elf32_bfd_merge_private_bfd_data \
6749 sh_elf_merge_private_data
6751 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6752 #define elf_backend_check_relocs sh_elf_check_relocs
6753 #define elf_backend_copy_indirect_symbol \
6754 sh_elf_copy_indirect_symbol
6755 #define elf_backend_create_dynamic_sections \
6756 sh_elf_create_dynamic_sections
6757 #define bfd_elf32_bfd_link_hash_table_create \
6758 sh_elf_link_hash_table_create
6759 #define elf_backend_adjust_dynamic_symbol \
6760 sh_elf_adjust_dynamic_symbol
6761 #define elf_backend_always_size_sections \
6762 sh_elf_always_size_sections
6763 #define elf_backend_size_dynamic_sections \
6764 sh_elf_size_dynamic_sections
6765 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
6766 #define elf_backend_finish_dynamic_symbol \
6767 sh_elf_finish_dynamic_symbol
6768 #define elf_backend_finish_dynamic_sections \
6769 sh_elf_finish_dynamic_sections
6770 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
6771 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
6772 #define elf_backend_can_make_relative_eh_frame \
6773 sh_elf_use_relative_eh_frame
6774 #define elf_backend_can_make_lsda_relative_eh_frame \
6775 sh_elf_use_relative_eh_frame
6776 #define elf_backend_encode_eh_address \
6777 sh_elf_encode_eh_address
6779 #define elf_backend_stack_align 8
6780 #define elf_backend_can_gc_sections 1
6781 #define elf_backend_can_refcount 1
6782 #define elf_backend_want_got_plt 1
6783 #define elf_backend_plt_readonly 1
6784 #define elf_backend_want_plt_sym 0
6785 #define elf_backend_got_header_size 12
6786 #define elf_backend_dtrel_excludes_plt 1
6788 #define elf_backend_linux_prpsinfo32_ugid16 TRUE
6790 #if !defined SH_TARGET_ALREADY_DEFINED
6792 #include "elf32-target.h"
6794 /* NetBSD support. */
6795 #undef TARGET_BIG_SYM
6796 #define TARGET_BIG_SYM sh_elf32_nbsd_vec
6797 #undef TARGET_BIG_NAME
6798 #define TARGET_BIG_NAME "elf32-sh-nbsd"
6799 #undef TARGET_LITTLE_SYM
6800 #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
6801 #undef TARGET_LITTLE_NAME
6802 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6803 #undef ELF_MAXPAGESIZE
6804 #define ELF_MAXPAGESIZE 0x10000
6805 #undef ELF_COMMONPAGESIZE
6806 #undef elf_symbol_leading_char
6807 #define elf_symbol_leading_char 0
6809 #define elf32_bed elf32_sh_nbsd_bed
6811 #include "elf32-target.h"
6814 /* Linux support. */
6815 #undef TARGET_BIG_SYM
6816 #define TARGET_BIG_SYM sh_elf32_linux_be_vec
6817 #undef TARGET_BIG_NAME
6818 #define TARGET_BIG_NAME "elf32-shbig-linux"
6819 #undef TARGET_LITTLE_SYM
6820 #define TARGET_LITTLE_SYM sh_elf32_linux_vec
6821 #undef TARGET_LITTLE_NAME
6822 #define TARGET_LITTLE_NAME "elf32-sh-linux"
6823 #undef ELF_COMMONPAGESIZE
6824 #define ELF_COMMONPAGESIZE 0x1000
6826 #undef elf_backend_grok_prstatus
6827 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6828 #undef elf_backend_grok_psinfo
6829 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
6831 #define elf32_bed elf32_sh_lin_bed
6833 #include "elf32-target.h"
6836 /* FDPIC support. */
6837 #undef TARGET_BIG_SYM
6838 #define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
6839 #undef TARGET_BIG_NAME
6840 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
6841 #undef TARGET_LITTLE_SYM
6842 #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
6843 #undef TARGET_LITTLE_NAME
6844 #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
6847 #define elf32_bed elf32_sh_fd_bed
6849 #include "elf32-target.h"
6851 /* VxWorks support. */
6852 #undef TARGET_BIG_SYM
6853 #define TARGET_BIG_SYM sh_elf32_vxworks_vec
6854 #undef TARGET_BIG_NAME
6855 #define TARGET_BIG_NAME "elf32-sh-vxworks"
6856 #undef TARGET_LITTLE_SYM
6857 #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
6858 #undef TARGET_LITTLE_NAME
6859 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6861 #define elf32_bed elf32_sh_vxworks_bed
6863 #undef elf_backend_want_plt_sym
6864 #define elf_backend_want_plt_sym 1
6865 #undef elf_symbol_leading_char
6866 #define elf_symbol_leading_char '_'
6867 #define elf_backend_want_got_underscore 1
6868 #undef elf_backend_grok_prstatus
6869 #undef elf_backend_grok_psinfo
6870 #undef elf_backend_add_symbol_hook
6871 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6872 #undef elf_backend_link_output_symbol_hook
6873 #define elf_backend_link_output_symbol_hook \
6874 elf_vxworks_link_output_symbol_hook
6875 #undef elf_backend_emit_relocs
6876 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6877 #undef elf_backend_final_write_processing
6878 #define elf_backend_final_write_processing \
6879 elf_vxworks_final_write_processing
6880 #undef ELF_MAXPAGESIZE
6881 #define ELF_MAXPAGESIZE 0x1000
6882 #undef ELF_COMMONPAGESIZE
6884 #include "elf32-target.h"
6886 #endif /* not SH_TARGET_ALREADY_DEFINED */