1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright (C) 1996-2020 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 #include "elf-vxworks.h"
30 #include "libiberty.h"
31 #include "../opcodes/sh-opc.h"
33 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
34 #define OCTETS_PER_BYTE(ABFD, SEC) 1
36 static bfd_reloc_status_type sh_elf_reloc
37 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
38 static bfd_reloc_status_type sh_elf_ignore_reloc
39 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
40 static bfd_boolean sh_elf_relax_delete_bytes
41 (bfd
*, asection
*, bfd_vma
, int);
42 static bfd_boolean sh_elf_align_loads
43 (bfd
*, asection
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_boolean
*);
44 static bfd_boolean sh_elf_swap_insns
45 (bfd
*, asection
*, void *, bfd_byte
*, bfd_vma
);
46 static int sh_elf_optimized_tls_reloc
47 (struct bfd_link_info
*, int, int);
48 static bfd_vma dtpoff_base
49 (struct bfd_link_info
*);
51 (struct bfd_link_info
*, bfd_vma
);
53 /* The name of the dynamic interpreter. This is put in the .interp
56 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
58 /* FDPIC binaries have a default 128K stack. */
59 #define DEFAULT_STACK_SIZE 0x20000
61 #define MINUS_ONE ((bfd_vma) 0 - 1)
63 /* Decide whether a reference to a symbol can be resolved locally or
64 not. If the symbol is protected, we want the local address, but
65 its function descriptor must be assigned by the dynamic linker. */
66 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
67 (SYMBOL_REFERENCES_LOCAL (INFO, H) \
68 || ! elf_hash_table (INFO)->dynamic_sections_created)
70 #define SH_PARTIAL32 TRUE
71 #define SH_SRC_MASK32 0xffffffff
72 #define SH_ELF_RELOC sh_elf_reloc
73 static reloc_howto_type sh_elf_howto_table
[] =
75 #include "elf32-sh-relocs.h"
78 #define SH_PARTIAL32 FALSE
79 #define SH_SRC_MASK32 0
80 #define SH_ELF_RELOC bfd_elf_generic_reloc
81 static reloc_howto_type sh_vxworks_howto_table
[] =
83 #include "elf32-sh-relocs.h"
86 /* Return true if OUTPUT_BFD is a VxWorks object. */
89 vxworks_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
91 #if !defined SH_TARGET_ALREADY_DEFINED
92 extern const bfd_target sh_elf32_vxworks_le_vec
;
93 extern const bfd_target sh_elf32_vxworks_vec
;
95 return (abfd
->xvec
== &sh_elf32_vxworks_le_vec
96 || abfd
->xvec
== &sh_elf32_vxworks_vec
);
102 /* Return true if OUTPUT_BFD is an FDPIC object. */
105 fdpic_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
107 #if !defined SH_TARGET_ALREADY_DEFINED
108 extern const bfd_target sh_elf32_fdpic_le_vec
;
109 extern const bfd_target sh_elf32_fdpic_be_vec
;
111 return (abfd
->xvec
== &sh_elf32_fdpic_le_vec
112 || abfd
->xvec
== &sh_elf32_fdpic_be_vec
);
118 /* Return the howto table for ABFD. */
120 static reloc_howto_type
*
121 get_howto_table (bfd
*abfd
)
123 if (vxworks_object_p (abfd
))
124 return sh_vxworks_howto_table
;
125 return sh_elf_howto_table
;
128 static bfd_reloc_status_type
129 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
130 asection
*input_section
, bfd_byte
*contents
,
131 bfd_vma addr
, asection
*symbol_section
,
132 bfd_vma start
, bfd_vma end
)
134 static bfd_vma last_addr
;
135 static asection
*last_symbol_section
;
136 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
141 /* Sanity check the address. */
142 if (addr
> bfd_get_section_limit (input_bfd
, input_section
))
143 return bfd_reloc_outofrange
;
145 /* We require the start and end relocations to be processed consecutively -
146 although we allow then to be processed forwards or backwards. */
150 last_symbol_section
= symbol_section
;
153 if (last_addr
!= addr
)
157 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
158 return bfd_reloc_outofrange
;
160 /* Get the symbol_section contents. */
161 if (symbol_section
!= input_section
)
163 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
164 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
167 if (!bfd_malloc_and_get_section (input_bfd
, symbol_section
,
170 if (contents
!= NULL
)
172 return bfd_reloc_outofrange
;
176 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
177 start_ptr
= contents
+ start
;
178 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
180 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
183 diff
= (last_ptr
- ptr
) >> 1;
184 cum_diff
+= diff
& 1;
187 /* Calculate the start / end values to load into rs / re minus four -
188 so that will cancel out the four we would otherwise have to add to
189 addr to get the value to subtract in order to get relative addressing. */
193 end
= (ptr
+ cum_diff
* 2) - contents
;
197 bfd_vma start0
= start
- 4;
199 while (start0
&& IS_PPI (contents
+ start0
))
201 start0
= start
- 2 - ((start
- start0
) & 2);
202 start
= start0
- cum_diff
- 2;
207 && elf_section_data (symbol_section
)->this_hdr
.contents
!= contents
)
210 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
212 x
= (insn
& 0x200 ? end
: start
) - addr
;
213 if (input_section
!= symbol_section
)
214 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
215 - (input_section
->output_section
->vma
216 + input_section
->output_offset
));
218 if (x
< -128 || x
> 127)
219 return bfd_reloc_overflow
;
221 x
= (insn
& ~0xff) | (x
& 0xff);
222 bfd_put_16 (input_bfd
, (bfd_vma
) x
, contents
+ addr
);
227 /* This function is used for normal relocs. This used to be like the COFF
228 function, and is almost certainly incorrect for other ELF targets. */
230 static bfd_reloc_status_type
231 sh_elf_reloc (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol_in
,
232 void *data
, asection
*input_section
, bfd
*output_bfd
,
233 char **error_message ATTRIBUTE_UNUSED
)
237 enum elf_sh_reloc_type r_type
;
238 bfd_vma addr
= reloc_entry
->address
;
239 bfd_size_type octets
= addr
* OCTETS_PER_BYTE (abfd
, input_section
);
240 bfd_byte
*hit_data
= (bfd_byte
*) data
+ octets
;
242 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
244 if (output_bfd
!= NULL
)
246 /* Partial linking--do nothing. */
247 reloc_entry
->address
+= input_section
->output_offset
;
251 /* Almost all relocs have to do with relaxing. If any work must be
252 done for them, it has been done in sh_relax_section. */
253 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
256 if (symbol_in
!= NULL
257 && bfd_is_und_section (symbol_in
->section
))
258 return bfd_reloc_undefined
;
260 /* PR 17512: file: 9891ca98. */
261 if (octets
+ bfd_get_reloc_size (reloc_entry
->howto
)
262 > bfd_get_section_limit_octets (abfd
, input_section
))
263 return bfd_reloc_outofrange
;
265 if (bfd_is_com_section (symbol_in
->section
))
268 sym_value
= (symbol_in
->value
+
269 symbol_in
->section
->output_section
->vma
+
270 symbol_in
->section
->output_offset
);
275 insn
= bfd_get_32 (abfd
, hit_data
);
276 insn
+= sym_value
+ reloc_entry
->addend
;
277 bfd_put_32 (abfd
, insn
, hit_data
);
280 insn
= bfd_get_16 (abfd
, hit_data
);
281 sym_value
+= reloc_entry
->addend
;
282 sym_value
-= (input_section
->output_section
->vma
283 + input_section
->output_offset
286 sym_value
+= (((insn
& 0xfff) ^ 0x800) - 0x800) << 1;
287 insn
= (insn
& 0xf000) | ((sym_value
>> 1) & 0xfff);
288 bfd_put_16 (abfd
, insn
, hit_data
);
289 if (sym_value
+ 0x1000 >= 0x2000 || (sym_value
& 1) != 0)
290 return bfd_reloc_overflow
;
300 /* This function is used for relocs which are only used for relaxing,
301 which the linker should otherwise ignore. */
303 static bfd_reloc_status_type
304 sh_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*reloc_entry
,
305 asymbol
*symbol ATTRIBUTE_UNUSED
,
306 void *data ATTRIBUTE_UNUSED
, asection
*input_section
,
308 char **error_message ATTRIBUTE_UNUSED
)
310 if (output_bfd
!= NULL
)
311 reloc_entry
->address
+= input_section
->output_offset
;
315 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
319 bfd_reloc_code_real_type bfd_reloc_val
;
320 unsigned char elf_reloc_val
;
323 /* An array mapping BFD reloc codes to SH ELF relocs. */
325 static const struct elf_reloc_map sh_reloc_map
[] =
327 { BFD_RELOC_NONE
, R_SH_NONE
},
328 { BFD_RELOC_32
, R_SH_DIR32
},
329 { BFD_RELOC_16
, R_SH_DIR16
},
330 { BFD_RELOC_8
, R_SH_DIR8
},
331 { BFD_RELOC_CTOR
, R_SH_DIR32
},
332 { BFD_RELOC_32_PCREL
, R_SH_REL32
},
333 { BFD_RELOC_SH_PCDISP8BY2
, R_SH_DIR8WPN
},
334 { BFD_RELOC_SH_PCDISP12BY2
, R_SH_IND12W
},
335 { BFD_RELOC_SH_PCRELIMM8BY2
, R_SH_DIR8WPZ
},
336 { BFD_RELOC_SH_PCRELIMM8BY4
, R_SH_DIR8WPL
},
337 { BFD_RELOC_8_PCREL
, R_SH_SWITCH8
},
338 { BFD_RELOC_SH_SWITCH16
, R_SH_SWITCH16
},
339 { BFD_RELOC_SH_SWITCH32
, R_SH_SWITCH32
},
340 { BFD_RELOC_SH_USES
, R_SH_USES
},
341 { BFD_RELOC_SH_COUNT
, R_SH_COUNT
},
342 { BFD_RELOC_SH_ALIGN
, R_SH_ALIGN
},
343 { BFD_RELOC_SH_CODE
, R_SH_CODE
},
344 { BFD_RELOC_SH_DATA
, R_SH_DATA
},
345 { BFD_RELOC_SH_LABEL
, R_SH_LABEL
},
346 { BFD_RELOC_VTABLE_INHERIT
, R_SH_GNU_VTINHERIT
},
347 { BFD_RELOC_VTABLE_ENTRY
, R_SH_GNU_VTENTRY
},
348 { BFD_RELOC_SH_LOOP_START
, R_SH_LOOP_START
},
349 { BFD_RELOC_SH_LOOP_END
, R_SH_LOOP_END
},
350 { BFD_RELOC_SH_TLS_GD_32
, R_SH_TLS_GD_32
},
351 { BFD_RELOC_SH_TLS_LD_32
, R_SH_TLS_LD_32
},
352 { BFD_RELOC_SH_TLS_LDO_32
, R_SH_TLS_LDO_32
},
353 { BFD_RELOC_SH_TLS_IE_32
, R_SH_TLS_IE_32
},
354 { BFD_RELOC_SH_TLS_LE_32
, R_SH_TLS_LE_32
},
355 { BFD_RELOC_SH_TLS_DTPMOD32
, R_SH_TLS_DTPMOD32
},
356 { BFD_RELOC_SH_TLS_DTPOFF32
, R_SH_TLS_DTPOFF32
},
357 { BFD_RELOC_SH_TLS_TPOFF32
, R_SH_TLS_TPOFF32
},
358 { BFD_RELOC_32_GOT_PCREL
, R_SH_GOT32
},
359 { BFD_RELOC_32_PLT_PCREL
, R_SH_PLT32
},
360 { BFD_RELOC_SH_COPY
, R_SH_COPY
},
361 { BFD_RELOC_SH_GLOB_DAT
, R_SH_GLOB_DAT
},
362 { BFD_RELOC_SH_JMP_SLOT
, R_SH_JMP_SLOT
},
363 { BFD_RELOC_SH_RELATIVE
, R_SH_RELATIVE
},
364 { BFD_RELOC_32_GOTOFF
, R_SH_GOTOFF
},
365 { BFD_RELOC_SH_GOTPC
, R_SH_GOTPC
},
366 { BFD_RELOC_SH_GOTPLT32
, R_SH_GOTPLT32
},
367 { BFD_RELOC_SH_GOT20
, R_SH_GOT20
},
368 { BFD_RELOC_SH_GOTOFF20
, R_SH_GOTOFF20
},
369 { BFD_RELOC_SH_GOTFUNCDESC
, R_SH_GOTFUNCDESC
},
370 { BFD_RELOC_SH_GOTFUNCDESC20
, R_SH_GOTFUNCDESC20
},
371 { BFD_RELOC_SH_GOTOFFFUNCDESC
, R_SH_GOTOFFFUNCDESC
},
372 { BFD_RELOC_SH_GOTOFFFUNCDESC20
, R_SH_GOTOFFFUNCDESC20
},
373 { BFD_RELOC_SH_FUNCDESC
, R_SH_FUNCDESC
},
376 /* Given a BFD reloc code, return the howto structure for the
377 corresponding SH ELF reloc. */
379 static reloc_howto_type
*
380 sh_elf_reloc_type_lookup (bfd
*abfd
, bfd_reloc_code_real_type code
)
384 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
386 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
387 return get_howto_table (abfd
) + (int) sh_reloc_map
[i
].elf_reloc_val
;
393 static reloc_howto_type
*
394 sh_elf_reloc_name_lookup (bfd
*abfd
, const char *r_name
)
398 if (vxworks_object_p (abfd
))
401 i
< (sizeof (sh_vxworks_howto_table
)
402 / sizeof (sh_vxworks_howto_table
[0]));
404 if (sh_vxworks_howto_table
[i
].name
!= NULL
405 && strcasecmp (sh_vxworks_howto_table
[i
].name
, r_name
) == 0)
406 return &sh_vxworks_howto_table
[i
];
411 i
< (sizeof (sh_elf_howto_table
)
412 / sizeof (sh_elf_howto_table
[0]));
414 if (sh_elf_howto_table
[i
].name
!= NULL
415 && strcasecmp (sh_elf_howto_table
[i
].name
, r_name
) == 0)
416 return &sh_elf_howto_table
[i
];
422 /* Given an ELF reloc, fill in the howto field of a relent. */
425 sh_elf_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
, Elf_Internal_Rela
*dst
)
429 r
= ELF32_R_TYPE (dst
->r_info
);
432 || (r
>= R_SH_FIRST_INVALID_RELOC
&& r
<= R_SH_LAST_INVALID_RELOC
)
433 || (r
>= R_SH_FIRST_INVALID_RELOC_2
&& r
<= R_SH_LAST_INVALID_RELOC_2
)
434 || (r
>= R_SH_FIRST_INVALID_RELOC_3
&& r
<= R_SH_LAST_INVALID_RELOC_3
)
435 || (r
>= R_SH_FIRST_INVALID_RELOC_4
&& r
<= R_SH_LAST_INVALID_RELOC_4
)
436 || (r
>= R_SH_FIRST_INVALID_RELOC_5
&& r
<= R_SH_LAST_INVALID_RELOC_5
)
437 || (r
>= R_SH_FIRST_INVALID_RELOC_6
&& r
<= R_SH_LAST_INVALID_RELOC_6
))
439 /* xgettext:c-format */
440 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
442 bfd_set_error (bfd_error_bad_value
);
446 cache_ptr
->howto
= get_howto_table (abfd
) + r
;
450 /* This function handles relaxing for SH ELF. See the corresponding
451 function in coff-sh.c for a description of what this does. FIXME:
452 There is a lot of duplication here between this code and the COFF
453 specific code. The format of relocs and symbols is wound deeply
454 into this code, but it would still be better if the duplication
455 could be eliminated somehow. Note in particular that although both
456 functions use symbols like R_SH_CODE, those symbols have different
457 values; in coff-sh.c they come from include/coff/sh.h, whereas here
458 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
461 sh_elf_relax_section (bfd
*abfd
, asection
*sec
,
462 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
464 Elf_Internal_Shdr
*symtab_hdr
;
465 Elf_Internal_Rela
*internal_relocs
;
466 bfd_boolean have_code
;
467 Elf_Internal_Rela
*irel
, *irelend
;
468 bfd_byte
*contents
= NULL
;
469 Elf_Internal_Sym
*isymbuf
= NULL
;
473 if (bfd_link_relocatable (link_info
)
474 || (sec
->flags
& SEC_RELOC
) == 0
475 || sec
->reloc_count
== 0)
478 symtab_hdr
= &elf_symtab_hdr (abfd
);
480 internal_relocs
= (_bfd_elf_link_read_relocs
481 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
482 link_info
->keep_memory
));
483 if (internal_relocs
== NULL
)
488 irelend
= internal_relocs
+ sec
->reloc_count
;
489 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
491 bfd_vma laddr
, paddr
, symval
;
493 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
496 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
499 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
502 /* Get the section contents. */
503 if (contents
== NULL
)
505 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
506 contents
= elf_section_data (sec
)->this_hdr
.contents
;
509 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
514 /* The r_addend field of the R_SH_USES reloc will point us to
515 the register load. The 4 is because the r_addend field is
516 computed as though it were a jump offset, which are based
517 from 4 bytes after the jump instruction. */
518 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
519 if (laddr
>= sec
->size
)
521 /* xgettext:c-format */
523 (_("%pB: %#" PRIx64
": warning: bad R_SH_USES offset"),
524 abfd
, (uint64_t) irel
->r_offset
);
527 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
529 /* If the instruction is not mov.l NN,rN, we don't know what to
531 if ((insn
& 0xf000) != 0xd000)
534 /* xgettext:c-format */
535 (_("%pB: %#" PRIx64
": warning: "
536 "R_SH_USES points to unrecognized insn 0x%x"),
537 abfd
, (uint64_t) irel
->r_offset
, insn
);
541 /* Get the address from which the register is being loaded. The
542 displacement in the mov.l instruction is quadrupled. It is a
543 displacement from four bytes after the movl instruction, but,
544 before adding in the PC address, two least significant bits
545 of the PC are cleared. We assume that the section is aligned
546 on a four byte boundary. */
549 paddr
+= (laddr
+ 4) &~ (bfd_vma
) 3;
550 if (paddr
>= sec
->size
)
553 /* xgettext:c-format */
554 (_("%pB: %#" PRIx64
": warning: bad R_SH_USES load offset"),
555 abfd
, (uint64_t) irel
->r_offset
);
559 /* Get the reloc for the address from which the register is
560 being loaded. This reloc will tell us which function is
561 actually being called. */
562 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
563 if (irelfn
->r_offset
== paddr
564 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
566 if (irelfn
>= irelend
)
569 /* xgettext:c-format */
570 (_("%pB: %#" PRIx64
": warning: could not find expected reloc"),
571 abfd
, (uint64_t) paddr
);
575 /* Read this BFD's symbols if we haven't done so already. */
576 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
578 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
580 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
581 symtab_hdr
->sh_info
, 0,
587 /* Get the value of the symbol referred to by the reloc. */
588 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
590 /* A local symbol. */
591 Elf_Internal_Sym
*isym
;
593 isym
= isymbuf
+ ELF32_R_SYM (irelfn
->r_info
);
595 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd
, sec
))
598 /* xgettext:c-format */
599 (_("%pB: %#" PRIx64
": warning: symbol in unexpected section"),
600 abfd
, (uint64_t) paddr
);
604 symval
= (isym
->st_value
605 + sec
->output_section
->vma
606 + sec
->output_offset
);
611 struct elf_link_hash_entry
*h
;
613 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
614 h
= elf_sym_hashes (abfd
)[indx
];
615 BFD_ASSERT (h
!= NULL
);
616 if (h
->root
.type
!= bfd_link_hash_defined
617 && h
->root
.type
!= bfd_link_hash_defweak
)
619 /* This appears to be a reference to an undefined
620 symbol. Just ignore it--it will be caught by the
621 regular reloc processing. */
625 symval
= (h
->root
.u
.def
.value
626 + h
->root
.u
.def
.section
->output_section
->vma
627 + h
->root
.u
.def
.section
->output_offset
);
630 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
631 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
633 symval
+= irelfn
->r_addend
;
635 /* See if this function call can be shortened. */
638 + sec
->output_section
->vma
641 /* A branch to an address beyond ours might be increased by an
642 .align that doesn't move when bytes behind us are deleted.
643 So, we add some slop in this calculation to allow for
645 if (foff
< -0x1000 || foff
>= 0x1000 - 8)
647 /* After all that work, we can't shorten this function call. */
651 /* Shorten the function call. */
653 /* For simplicity of coding, we are going to modify the section
654 contents, the section relocs, and the BFD symbol table. We
655 must tell the rest of the code not to free up this
656 information. It would be possible to instead create a table
657 of changes which have to be made, as is done in coff-mips.c;
658 that would be more work, but would require less memory when
659 the linker is run. */
661 elf_section_data (sec
)->relocs
= internal_relocs
;
662 elf_section_data (sec
)->this_hdr
.contents
= contents
;
663 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
665 /* Replace the jmp/jsr with a bra/bsr. */
667 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
668 replace the jmp/jsr with a bra/bsr. */
669 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
670 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
671 here, but that only checks if the symbol is an external symbol,
672 not if the symbol is in a different section. Besides, we need
673 a consistent meaning for the relocation, so we just assume here that
674 the value of the symbol is not available. */
676 /* We can't fully resolve this yet, because the external
677 symbol value may be changed by future relaxing. We let
678 the final link phase handle it. */
679 if (bfd_get_16 (abfd
, contents
+ irel
->r_offset
) & 0x0020)
680 bfd_put_16 (abfd
, (bfd_vma
) 0xa000, contents
+ irel
->r_offset
);
682 bfd_put_16 (abfd
, (bfd_vma
) 0xb000, contents
+ irel
->r_offset
);
686 /* When we calculated the symbol "value" we had an offset in the
687 DIR32's word in memory (we read and add it above). However,
688 the jsr we create does NOT have this offset encoded, so we
689 have to add it to the addend to preserve it. */
690 irel
->r_addend
+= bfd_get_32 (abfd
, contents
+ paddr
);
692 /* See if there is another R_SH_USES reloc referring to the same
694 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
695 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
696 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
698 if (irelscan
< irelend
)
700 /* Some other function call depends upon this register load,
701 and we have not yet converted that function call.
702 Indeed, we may never be able to convert it. There is
703 nothing else we can do at this point. */
707 /* Look for a R_SH_COUNT reloc on the location where the
708 function address is stored. Do this before deleting any
709 bytes, to avoid confusion about the address. */
710 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
711 if (irelcount
->r_offset
== paddr
712 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
715 /* Delete the register load. */
716 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
719 /* That will change things, so, just in case it permits some
720 other function call to come within range, we should relax
721 again. Note that this is not required, and it may be slow. */
724 /* Now check whether we got a COUNT reloc. */
725 if (irelcount
>= irelend
)
728 /* xgettext:c-format */
729 (_("%pB: %#" PRIx64
": warning: "
730 "could not find expected COUNT reloc"),
731 abfd
, (uint64_t) paddr
);
735 /* The number of uses is stored in the r_addend field. We've
737 if (irelcount
->r_addend
== 0)
739 /* xgettext:c-format */
740 _bfd_error_handler (_("%pB: %#" PRIx64
": warning: bad count"),
741 abfd
, (uint64_t) paddr
);
745 --irelcount
->r_addend
;
747 /* If there are no more uses, we can delete the address. Reload
748 the address from irelfn, in case it was changed by the
749 previous call to sh_elf_relax_delete_bytes. */
750 if (irelcount
->r_addend
== 0)
752 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
756 /* We've done all we can with that function call. */
759 /* Look for load and store instructions that we can align on four
761 if ((elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
) != EF_SH4
766 /* Get the section contents. */
767 if (contents
== NULL
)
769 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
770 contents
= elf_section_data (sec
)->this_hdr
.contents
;
773 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
778 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
784 elf_section_data (sec
)->relocs
= internal_relocs
;
785 elf_section_data (sec
)->this_hdr
.contents
= contents
;
786 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
791 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
793 if (! link_info
->keep_memory
)
797 /* Cache the symbols for elf_link_input_bfd. */
798 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
803 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
805 if (! link_info
->keep_memory
)
809 /* Cache the section contents for elf_link_input_bfd. */
810 elf_section_data (sec
)->this_hdr
.contents
= contents
;
814 if (internal_relocs
!= NULL
815 && elf_section_data (sec
)->relocs
!= internal_relocs
)
816 free (internal_relocs
);
822 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
825 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
827 if (internal_relocs
!= NULL
828 && elf_section_data (sec
)->relocs
!= internal_relocs
)
829 free (internal_relocs
);
834 /* Delete some bytes from a section while relaxing. FIXME: There is a
835 lot of duplication between this function and sh_relax_delete_bytes
839 sh_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
,
842 Elf_Internal_Shdr
*symtab_hdr
;
843 unsigned int sec_shndx
;
845 Elf_Internal_Rela
*irel
, *irelend
;
846 Elf_Internal_Rela
*irelalign
;
848 Elf_Internal_Sym
*isymbuf
, *isym
, *isymend
;
849 struct elf_link_hash_entry
**sym_hashes
;
850 struct elf_link_hash_entry
**end_hashes
;
851 unsigned int symcount
;
854 symtab_hdr
= &elf_symtab_hdr (abfd
);
855 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
857 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
859 contents
= elf_section_data (sec
)->this_hdr
.contents
;
861 /* The deletion must stop at the next ALIGN reloc for an alignment
862 power larger than the number of bytes we are deleting. */
867 irel
= elf_section_data (sec
)->relocs
;
868 irelend
= irel
+ sec
->reloc_count
;
869 for (; irel
< irelend
; irel
++)
871 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
872 && irel
->r_offset
> addr
873 && count
< (1 << irel
->r_addend
))
876 toaddr
= irel
->r_offset
;
881 /* Actually delete the bytes. */
882 memmove (contents
+ addr
, contents
+ addr
+ count
,
883 (size_t) (toaddr
- addr
- count
));
884 if (irelalign
== NULL
)
890 #define NOP_OPCODE (0x0009)
892 BFD_ASSERT ((count
& 1) == 0);
893 for (i
= 0; i
< count
; i
+= 2)
894 bfd_put_16 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
897 /* Adjust all the relocs. */
898 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
900 bfd_vma nraddr
, stop
;
903 int off
, adjust
, oinsn
;
904 bfd_signed_vma voff
= 0;
905 bfd_boolean overflow
;
907 /* Get the new reloc address. */
908 nraddr
= irel
->r_offset
;
909 if ((irel
->r_offset
> addr
910 && irel
->r_offset
< toaddr
)
911 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
912 && irel
->r_offset
== toaddr
))
915 /* See if this reloc was for the bytes we have deleted, in which
916 case we no longer care about it. Don't delete relocs which
917 represent addresses, though. */
918 if (irel
->r_offset
>= addr
919 && irel
->r_offset
< addr
+ count
920 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
921 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
922 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
923 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
924 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
927 /* If this is a PC relative reloc, see if the range it covers
928 includes the bytes we have deleted. */
929 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
938 start
= irel
->r_offset
;
939 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
943 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
950 /* If this reloc is against a symbol defined in this
951 section, and the symbol will not be adjusted below, we
952 must check the addend to see it will put the value in
953 range to be adjusted, and hence must be changed. */
954 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
956 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
957 if (isym
->st_shndx
== sec_shndx
958 && (isym
->st_value
<= addr
959 || isym
->st_value
>= toaddr
))
963 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
965 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
966 val
+= isym
->st_value
;
967 if (val
> addr
&& val
< toaddr
)
968 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
972 val
= isym
->st_value
+ irel
->r_addend
;
973 if (val
> addr
&& val
< toaddr
)
974 irel
->r_addend
-= count
;
985 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
992 /* This has been made by previous relaxation. Since the
993 relocation will be against an external symbol, the
994 final relocation will just do the right thing. */
1001 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1003 /* The addend will be against the section symbol, thus
1004 for adjusting the addend, the relevant start is the
1005 start of the section.
1006 N.B. If we want to abandon in-place changes here and
1007 test directly using symbol + addend, we have to take into
1008 account that the addend has already been adjusted by -4. */
1009 if (stop
> addr
&& stop
< toaddr
)
1010 irel
->r_addend
-= count
;
1016 stop
= start
+ 4 + off
* 2;
1021 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
1027 /* These relocs types represent
1029 The r_addend field holds the difference between the reloc
1030 address and L1. That is the start of the reloc, and
1031 adding in the contents gives us the top. We must adjust
1032 both the r_offset field and the section contents.
1033 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1034 and the elf bfd r_offset is called r_vaddr. */
1036 stop
= irel
->r_offset
;
1037 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
1041 && (stop
<= addr
|| stop
>= toaddr
))
1042 irel
->r_addend
+= count
;
1043 else if (stop
> addr
1045 && (start
<= addr
|| start
>= toaddr
))
1046 irel
->r_addend
-= count
;
1048 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1049 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1050 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1051 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1053 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1054 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1059 start
= irel
->r_offset
;
1060 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1061 + (long) irel
->r_addend
1068 && (stop
<= addr
|| stop
>= toaddr
))
1070 else if (stop
> addr
1072 && (start
<= addr
|| start
>= toaddr
))
1081 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1090 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1092 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1097 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1099 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1103 BFD_ASSERT (adjust
== count
|| count
>= 4);
1108 if ((irel
->r_offset
& 3) == 0)
1111 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1113 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1118 if (voff
< 0 || voff
>= 0xff)
1120 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1125 if (voff
< - 0x8000 || voff
>= 0x8000)
1127 bfd_put_signed_16 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1132 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1136 irel
->r_addend
+= adjust
;
1143 /* xgettext:c-format */
1144 (_("%pB: %#" PRIx64
": fatal: reloc overflow while relaxing"),
1145 abfd
, (uint64_t) irel
->r_offset
);
1146 bfd_set_error (bfd_error_bad_value
);
1151 irel
->r_offset
= nraddr
;
1154 /* Look through all the other sections. If there contain any IMM32
1155 relocs against internal symbols which we are not going to adjust
1156 below, we may need to adjust the addends. */
1157 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1159 Elf_Internal_Rela
*internal_relocs
;
1160 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1161 bfd_byte
*ocontents
;
1164 || (o
->flags
& SEC_RELOC
) == 0
1165 || o
->reloc_count
== 0)
1168 /* We always cache the relocs. Perhaps, if info->keep_memory is
1169 FALSE, we should free them, if we are permitted to, when we
1170 leave sh_coff_relax_section. */
1171 internal_relocs
= (_bfd_elf_link_read_relocs
1172 (abfd
, o
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
1173 if (internal_relocs
== NULL
)
1177 irelscanend
= internal_relocs
+ o
->reloc_count
;
1178 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1180 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1181 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1183 bfd_vma start
, stop
;
1184 bfd_signed_vma voff
;
1186 if (ocontents
== NULL
)
1188 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1189 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1192 /* We always cache the section contents.
1193 Perhaps, if info->keep_memory is FALSE, we
1194 should free them, if we are permitted to,
1195 when we leave sh_coff_relax_section. */
1196 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1198 if (ocontents
!= NULL
)
1203 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1207 stop
= irelscan
->r_offset
;
1209 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1211 /* STOP is in a different section, so it won't change. */
1212 if (start
> addr
&& start
< toaddr
)
1213 irelscan
->r_addend
+= count
;
1215 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1216 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1220 && (stop
<= addr
|| stop
>= toaddr
))
1221 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
+ count
,
1222 ocontents
+ irelscan
->r_offset
);
1223 else if (stop
> addr
1225 && (start
<= addr
|| start
>= toaddr
))
1226 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
- count
,
1227 ocontents
+ irelscan
->r_offset
);
1230 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1233 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1237 isym
= isymbuf
+ ELF32_R_SYM (irelscan
->r_info
);
1238 if (isym
->st_shndx
== sec_shndx
1239 && (isym
->st_value
<= addr
1240 || isym
->st_value
>= toaddr
))
1244 if (ocontents
== NULL
)
1246 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1247 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1250 /* We always cache the section contents.
1251 Perhaps, if info->keep_memory is FALSE, we
1252 should free them, if we are permitted to,
1253 when we leave sh_coff_relax_section. */
1254 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1256 if (ocontents
!= NULL
)
1261 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1265 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1266 val
+= isym
->st_value
;
1267 if (val
> addr
&& val
< toaddr
)
1268 bfd_put_32 (abfd
, val
- count
,
1269 ocontents
+ irelscan
->r_offset
);
1274 /* Adjust the local symbols defined in this section. */
1275 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1276 for (isym
= isymbuf
; isym
< isymend
; isym
++)
1278 if (isym
->st_shndx
== sec_shndx
1279 && isym
->st_value
> addr
1280 && isym
->st_value
< toaddr
)
1281 isym
->st_value
-= count
;
1284 /* Now adjust the global symbols defined in this section. */
1285 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1286 - symtab_hdr
->sh_info
);
1287 sym_hashes
= elf_sym_hashes (abfd
);
1288 end_hashes
= sym_hashes
+ symcount
;
1289 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1291 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1292 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1293 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1294 && sym_hash
->root
.u
.def
.section
== sec
1295 && sym_hash
->root
.u
.def
.value
> addr
1296 && sym_hash
->root
.u
.def
.value
< toaddr
)
1298 sym_hash
->root
.u
.def
.value
-= count
;
1302 /* See if we can move the ALIGN reloc forward. We have adjusted
1303 r_offset for it already. */
1304 if (irelalign
!= NULL
)
1306 bfd_vma alignto
, alignaddr
;
1308 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1309 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1310 1 << irelalign
->r_addend
);
1311 if (alignto
!= alignaddr
)
1313 /* Tail recursion. */
1314 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1315 (int) (alignto
- alignaddr
));
1322 /* Look for loads and stores which we can align to four byte
1323 boundaries. This is like sh_align_loads in coff-sh.c. */
1326 sh_elf_align_loads (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1327 Elf_Internal_Rela
*internal_relocs
,
1328 bfd_byte
*contents ATTRIBUTE_UNUSED
,
1329 bfd_boolean
*pswapped
)
1331 Elf_Internal_Rela
*irel
, *irelend
;
1332 bfd_vma
*labels
= NULL
;
1333 bfd_vma
*label
, *label_end
;
1338 irelend
= internal_relocs
+ sec
->reloc_count
;
1340 /* Get all the addresses with labels on them. */
1341 amt
= sec
->reloc_count
;
1342 amt
*= sizeof (bfd_vma
);
1343 labels
= (bfd_vma
*) bfd_malloc (amt
);
1347 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1349 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1351 *label_end
= irel
->r_offset
;
1356 /* Note that the assembler currently always outputs relocs in
1357 address order. If that ever changes, this code will need to sort
1358 the label values and the relocs. */
1362 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1364 bfd_vma start
, stop
;
1366 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1369 start
= irel
->r_offset
;
1371 for (irel
++; irel
< irelend
; irel
++)
1372 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1375 stop
= irel
->r_offset
;
1379 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1380 internal_relocs
, &label
,
1381 label_end
, start
, stop
, pswapped
))
1395 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1398 sh_elf_swap_insns (bfd
*abfd
, asection
*sec
, void *relocs
,
1399 bfd_byte
*contents
, bfd_vma addr
)
1401 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1402 unsigned short i1
, i2
;
1403 Elf_Internal_Rela
*irel
, *irelend
;
1405 /* Swap the instructions themselves. */
1406 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1407 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1408 bfd_put_16 (abfd
, (bfd_vma
) i2
, contents
+ addr
);
1409 bfd_put_16 (abfd
, (bfd_vma
) i1
, contents
+ addr
+ 2);
1411 /* Adjust all reloc addresses. */
1412 irelend
= internal_relocs
+ sec
->reloc_count
;
1413 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1415 enum elf_sh_reloc_type type
;
1418 /* There are a few special types of relocs that we don't want to
1419 adjust. These relocs do not apply to the instruction itself,
1420 but are only associated with the address. */
1421 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1422 if (type
== R_SH_ALIGN
1423 || type
== R_SH_CODE
1424 || type
== R_SH_DATA
1425 || type
== R_SH_LABEL
)
1428 /* If an R_SH_USES reloc points to one of the addresses being
1429 swapped, we must adjust it. It would be incorrect to do this
1430 for a jump, though, since we want to execute both
1431 instructions after the jump. (We have avoided swapping
1432 around a label, so the jump will not wind up executing an
1433 instruction it shouldn't). */
1434 if (type
== R_SH_USES
)
1438 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
1440 irel
->r_offset
+= 2;
1441 else if (off
== addr
+ 2)
1442 irel
->r_offset
-= 2;
1445 if (irel
->r_offset
== addr
)
1447 irel
->r_offset
+= 2;
1450 else if (irel
->r_offset
== addr
+ 2)
1452 irel
->r_offset
-= 2;
1461 unsigned short insn
, oinsn
;
1462 bfd_boolean overflow
;
1464 loc
= contents
+ irel
->r_offset
;
1473 insn
= bfd_get_16 (abfd
, loc
);
1476 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1478 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1482 insn
= bfd_get_16 (abfd
, loc
);
1485 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1487 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1491 /* This reloc ignores the least significant 3 bits of
1492 the program counter before adding in the offset.
1493 This means that if ADDR is at an even address, the
1494 swap will not affect the offset. If ADDR is an at an
1495 odd address, then the instruction will be crossing a
1496 four byte boundary, and must be adjusted. */
1497 if ((addr
& 3) != 0)
1499 insn
= bfd_get_16 (abfd
, loc
);
1502 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1504 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1513 /* xgettext:c-format */
1514 (_("%pB: %#" PRIx64
": fatal: reloc overflow while relaxing"),
1515 abfd
, (uint64_t) irel
->r_offset
);
1516 bfd_set_error (bfd_error_bad_value
);
1525 /* Describes one of the various PLT styles. */
1527 struct elf_sh_plt_info
1529 /* The template for the first PLT entry, or NULL if there is no special
1531 const bfd_byte
*plt0_entry
;
1533 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1534 bfd_vma plt0_entry_size
;
1536 /* Index I is the offset into PLT0_ENTRY of a pointer to
1537 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1538 if there is no such pointer. */
1539 bfd_vma plt0_got_fields
[3];
1541 /* The template for a symbol's PLT entry. */
1542 const bfd_byte
*symbol_entry
;
1544 /* The size of SYMBOL_ENTRY in bytes. */
1545 bfd_vma symbol_entry_size
;
1547 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1548 on all targets. The comments by each member indicate the value
1549 that the field must hold. */
1551 bfd_vma got_entry
; /* the address of the symbol's .got.plt entry */
1552 bfd_vma plt
; /* .plt (or a branch to .plt on VxWorks) */
1553 bfd_vma reloc_offset
; /* the offset of the symbol's JMP_SLOT reloc */
1554 bfd_boolean got20
; /* TRUE if got_entry points to a movi20
1555 instruction (instead of a constant pool
1559 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1560 bfd_vma symbol_resolve_offset
;
1562 /* A different PLT layout which can be used for the first
1563 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1565 const struct elf_sh_plt_info
*short_plt
;
1568 /* The size in bytes of an entry in the procedure linkage table. */
1570 #define ELF_PLT_ENTRY_SIZE 28
1572 /* First entry in an absolute procedure linkage table look like this. */
1574 /* Note - this code has been "optimised" not to use r2. r2 is used by
1575 GCC to return the address of large structures, so it should not be
1576 corrupted here. This does mean however, that this PLT does not conform
1577 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1578 and r2 contains the GOT id. This version stores the GOT id in r0 and
1579 ignores the type. Loaders can easily detect this difference however,
1580 since the type will always be 0 or 8, and the GOT ids will always be
1581 greater than or equal to 12. */
1582 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1584 0xd0, 0x05, /* mov.l 2f,r0 */
1585 0x60, 0x02, /* mov.l @r0,r0 */
1586 0x2f, 0x06, /* mov.l r0,@-r15 */
1587 0xd0, 0x03, /* mov.l 1f,r0 */
1588 0x60, 0x02, /* mov.l @r0,r0 */
1589 0x40, 0x2b, /* jmp @r0 */
1590 0x60, 0xf6, /* mov.l @r15+,r0 */
1591 0x00, 0x09, /* nop */
1592 0x00, 0x09, /* nop */
1593 0x00, 0x09, /* nop */
1594 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1595 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1598 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1600 0x05, 0xd0, /* mov.l 2f,r0 */
1601 0x02, 0x60, /* mov.l @r0,r0 */
1602 0x06, 0x2f, /* mov.l r0,@-r15 */
1603 0x03, 0xd0, /* mov.l 1f,r0 */
1604 0x02, 0x60, /* mov.l @r0,r0 */
1605 0x2b, 0x40, /* jmp @r0 */
1606 0xf6, 0x60, /* mov.l @r15+,r0 */
1607 0x09, 0x00, /* nop */
1608 0x09, 0x00, /* nop */
1609 0x09, 0x00, /* nop */
1610 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1611 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1614 /* Sebsequent entries in an absolute procedure linkage table look like
1617 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1619 0xd0, 0x04, /* mov.l 1f,r0 */
1620 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1621 0xd1, 0x02, /* mov.l 0f,r1 */
1622 0x40, 0x2b, /* jmp @r0 */
1623 0x60, 0x13, /* mov r1,r0 */
1624 0xd1, 0x03, /* mov.l 2f,r1 */
1625 0x40, 0x2b, /* jmp @r0 */
1626 0x00, 0x09, /* nop */
1627 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1628 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1629 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1632 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1634 0x04, 0xd0, /* mov.l 1f,r0 */
1635 0x02, 0x60, /* mov.l @r0,r0 */
1636 0x02, 0xd1, /* mov.l 0f,r1 */
1637 0x2b, 0x40, /* jmp @r0 */
1638 0x13, 0x60, /* mov r1,r0 */
1639 0x03, 0xd1, /* mov.l 2f,r1 */
1640 0x2b, 0x40, /* jmp @r0 */
1641 0x09, 0x00, /* nop */
1642 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1643 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1644 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1647 /* Entries in a PIC procedure linkage table look like this. */
1649 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1651 0xd0, 0x04, /* mov.l 1f,r0 */
1652 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1653 0x40, 0x2b, /* jmp @r0 */
1654 0x00, 0x09, /* nop */
1655 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1656 0xd1, 0x03, /* mov.l 2f,r1 */
1657 0x40, 0x2b, /* jmp @r0 */
1658 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1659 0x00, 0x09, /* nop */
1660 0x00, 0x09, /* nop */
1661 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1662 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1665 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1667 0x04, 0xd0, /* mov.l 1f,r0 */
1668 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1669 0x2b, 0x40, /* jmp @r0 */
1670 0x09, 0x00, /* nop */
1671 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1672 0x03, 0xd1, /* mov.l 2f,r1 */
1673 0x2b, 0x40, /* jmp @r0 */
1674 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1675 0x09, 0x00, /* nop */
1676 0x09, 0x00, /* nop */
1677 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1678 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1681 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1684 /* Big-endian non-PIC. */
1685 elf_sh_plt0_entry_be
,
1687 { MINUS_ONE
, 24, 20 },
1688 elf_sh_plt_entry_be
,
1690 { 20, 16, 24, FALSE
},
1695 /* Little-endian non-PIC. */
1696 elf_sh_plt0_entry_le
,
1698 { MINUS_ONE
, 24, 20 },
1699 elf_sh_plt_entry_le
,
1701 { 20, 16, 24, FALSE
},
1708 /* Big-endian PIC. */
1709 elf_sh_plt0_entry_be
,
1711 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1712 elf_sh_pic_plt_entry_be
,
1714 { 20, MINUS_ONE
, 24, FALSE
},
1719 /* Little-endian PIC. */
1720 elf_sh_plt0_entry_le
,
1722 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1723 elf_sh_pic_plt_entry_le
,
1725 { 20, MINUS_ONE
, 24, FALSE
},
1732 #define VXWORKS_PLT_HEADER_SIZE 12
1733 #define VXWORKS_PLT_ENTRY_SIZE 24
1735 static const bfd_byte vxworks_sh_plt0_entry_be
[VXWORKS_PLT_HEADER_SIZE
] =
1737 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1738 0x61, 0x12, /* mov.l @r1,r1 */
1739 0x41, 0x2b, /* jmp @r1 */
1740 0x00, 0x09, /* nop */
1741 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1744 static const bfd_byte vxworks_sh_plt0_entry_le
[VXWORKS_PLT_HEADER_SIZE
] =
1746 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1747 0x12, 0x61, /* mov.l @r1,r1 */
1748 0x2b, 0x41, /* jmp @r1 */
1749 0x09, 0x00, /* nop */
1750 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1753 static const bfd_byte vxworks_sh_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1755 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1756 0x60, 0x02, /* mov.l @r0,r0 */
1757 0x40, 0x2b, /* jmp @r0 */
1758 0x00, 0x09, /* nop */
1759 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1760 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1761 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1762 0x00, 0x09, /* nop */
1763 0x00, 0x09, /* nop */
1764 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1767 static const bfd_byte vxworks_sh_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1769 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1770 0x02, 0x60, /* mov.l @r0,r0 */
1771 0x2b, 0x40, /* jmp @r0 */
1772 0x09, 0x00, /* nop */
1773 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1774 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1775 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1776 0x09, 0x00, /* nop */
1777 0x09, 0x00, /* nop */
1778 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1781 static const bfd_byte vxworks_sh_pic_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
1783 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1784 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1785 0x40, 0x2b, /* jmp @r0 */
1786 0x00, 0x09, /* nop */
1787 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1788 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1789 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1790 0x41, 0x2b, /* jmp @r1 */
1791 0x00, 0x09, /* nop */
1792 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1795 static const bfd_byte vxworks_sh_pic_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
1797 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1798 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1799 0x2b, 0x40, /* jmp @r0 */
1800 0x09, 0x00, /* nop */
1801 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1802 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1803 0xc2, 0x51, /* mov.l @(8,r12),r1 */
1804 0x2b, 0x41, /* jmp @r1 */
1805 0x09, 0x00, /* nop */
1806 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1809 static const struct elf_sh_plt_info vxworks_sh_plts
[2][2] = {
1812 /* Big-endian non-PIC. */
1813 vxworks_sh_plt0_entry_be
,
1814 VXWORKS_PLT_HEADER_SIZE
,
1815 { MINUS_ONE
, MINUS_ONE
, 8 },
1816 vxworks_sh_plt_entry_be
,
1817 VXWORKS_PLT_ENTRY_SIZE
,
1818 { 8, 14, 20, FALSE
},
1823 /* Little-endian non-PIC. */
1824 vxworks_sh_plt0_entry_le
,
1825 VXWORKS_PLT_HEADER_SIZE
,
1826 { MINUS_ONE
, MINUS_ONE
, 8 },
1827 vxworks_sh_plt_entry_le
,
1828 VXWORKS_PLT_ENTRY_SIZE
,
1829 { 8, 14, 20, FALSE
},
1836 /* Big-endian PIC. */
1839 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1840 vxworks_sh_pic_plt_entry_be
,
1841 VXWORKS_PLT_ENTRY_SIZE
,
1842 { 8, MINUS_ONE
, 20, FALSE
},
1847 /* Little-endian PIC. */
1850 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1851 vxworks_sh_pic_plt_entry_le
,
1852 VXWORKS_PLT_ENTRY_SIZE
,
1853 { 8, MINUS_ONE
, 20, FALSE
},
1860 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
1861 binding are to omit the lazy binding stub when linking with -z now
1862 and to move lazy binding stubs into a separate region for better
1865 #define FDPIC_PLT_ENTRY_SIZE 28
1866 #define FDPIC_PLT_LAZY_OFFSET 20
1868 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
1869 duplicated if it is out of range, or which can be inlined. So
1870 right now it is always inlined, which wastes a word per stub. It
1871 might be easier to handle the duplication if we put the lazy
1872 stubs separately. */
1874 static const bfd_byte fdpic_sh_plt_entry_be
[FDPIC_PLT_ENTRY_SIZE
] =
1876 0xd0, 0x02, /* mov.l @(12,pc),r0 */
1877 0x01, 0xce, /* mov.l @(r0,r12),r1 */
1878 0x70, 0x04, /* add #4, r0 */
1879 0x41, 0x2b, /* jmp @r1 */
1880 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1881 0x00, 0x09, /* nop */
1882 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1883 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1884 0x60, 0xc2, /* mov.l @r12,r0 */
1885 0x40, 0x2b, /* jmp @r0 */
1886 0x53, 0xc1, /* mov.l @(4,r12),r3 */
1887 0x00, 0x09, /* nop */
1890 static const bfd_byte fdpic_sh_plt_entry_le
[FDPIC_PLT_ENTRY_SIZE
] =
1892 0x02, 0xd0, /* mov.l @(12,pc),r0 */
1893 0xce, 0x01, /* mov.l @(r0,r12),r1 */
1894 0x04, 0x70, /* add #4, r0 */
1895 0x2b, 0x41, /* jmp @r1 */
1896 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1897 0x09, 0x00, /* nop */
1898 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1899 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1900 0xc2, 0x60, /* mov.l @r12,r0 */
1901 0x2b, 0x40, /* jmp @r0 */
1902 0xc1, 0x53, /* mov.l @(4,r12),r3 */
1903 0x09, 0x00, /* nop */
1906 static const struct elf_sh_plt_info fdpic_sh_plts
[2] = {
1908 /* Big-endian PIC. */
1911 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1912 fdpic_sh_plt_entry_be
,
1913 FDPIC_PLT_ENTRY_SIZE
,
1914 { 12, MINUS_ONE
, 16, FALSE
},
1915 FDPIC_PLT_LAZY_OFFSET
,
1919 /* Little-endian PIC. */
1922 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1923 fdpic_sh_plt_entry_le
,
1924 FDPIC_PLT_ENTRY_SIZE
,
1925 { 12, MINUS_ONE
, 16, FALSE
},
1926 FDPIC_PLT_LAZY_OFFSET
,
1931 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
1932 entries for the first 64K slots. We use the normal FDPIC PLT entry
1933 past that point; we could also use movi20s, which might be faster,
1934 but would not be any smaller. */
1936 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
1937 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
1939 static const bfd_byte fdpic_sh2a_plt_entry_be
[FDPIC_SH2A_PLT_ENTRY_SIZE
] =
1941 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1942 0x01, 0xce, /* mov.l @(r0,r12),r1 */
1943 0x70, 0x04, /* add #4, r0 */
1944 0x41, 0x2b, /* jmp @r1 */
1945 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1946 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1947 0x60, 0xc2, /* mov.l @r12,r0 */
1948 0x40, 0x2b, /* jmp @r0 */
1949 0x53, 0xc1, /* mov.l @(4,r12),r3 */
1950 0x00, 0x09, /* nop */
1953 static const bfd_byte fdpic_sh2a_plt_entry_le
[FDPIC_SH2A_PLT_ENTRY_SIZE
] =
1955 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1956 0xce, 0x01, /* mov.l @(r0,r12),r1 */
1957 0x04, 0x70, /* add #4, r0 */
1958 0x2b, 0x41, /* jmp @r1 */
1959 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1960 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1961 0xc2, 0x60, /* mov.l @r12,r0 */
1962 0x2b, 0x40, /* jmp @r0 */
1963 0xc1, 0x53, /* mov.l @(4,r12),r3 */
1964 0x09, 0x00, /* nop */
1967 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be
= {
1968 /* Big-endian FDPIC, max index 64K. */
1971 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1972 fdpic_sh2a_plt_entry_be
,
1973 FDPIC_SH2A_PLT_ENTRY_SIZE
,
1974 { 0, MINUS_ONE
, 12, TRUE
},
1975 FDPIC_SH2A_PLT_LAZY_OFFSET
,
1979 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le
= {
1980 /* Little-endian FDPIC, max index 64K. */
1983 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1984 fdpic_sh2a_plt_entry_le
,
1985 FDPIC_SH2A_PLT_ENTRY_SIZE
,
1986 { 0, MINUS_ONE
, 12, TRUE
},
1987 FDPIC_SH2A_PLT_LAZY_OFFSET
,
1991 static const struct elf_sh_plt_info fdpic_sh2a_plts
[2] = {
1993 /* Big-endian PIC. */
1996 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1997 fdpic_sh_plt_entry_be
,
1998 FDPIC_PLT_ENTRY_SIZE
,
1999 { 12, MINUS_ONE
, 16, FALSE
},
2000 FDPIC_PLT_LAZY_OFFSET
,
2001 &fdpic_sh2a_short_plt_be
2004 /* Little-endian PIC. */
2007 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2008 fdpic_sh_plt_entry_le
,
2009 FDPIC_PLT_ENTRY_SIZE
,
2010 { 12, MINUS_ONE
, 16, FALSE
},
2011 FDPIC_PLT_LAZY_OFFSET
,
2012 &fdpic_sh2a_short_plt_le
2016 /* Return the type of PLT associated with ABFD. PIC_P is true if
2017 the object is position-independent. */
2019 static const struct elf_sh_plt_info
*
2020 get_plt_info (bfd
*abfd
, bfd_boolean pic_p
)
2022 if (fdpic_object_p (abfd
))
2024 /* If any input file requires SH2A we can use a shorter PLT
2026 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd
)) & arch_sh2a_base
)
2027 return &fdpic_sh2a_plts
[!bfd_big_endian (abfd
)];
2029 return &fdpic_sh_plts
[!bfd_big_endian (abfd
)];
2031 if (vxworks_object_p (abfd
))
2032 return &vxworks_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2033 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2036 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2037 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2041 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p ATTRIBUTE_UNUSED
,
2042 unsigned long value
, bfd_byte
*addr
)
2044 bfd_put_32 (output_bfd
, value
, addr
);
2047 /* The number of PLT entries which can use a shorter PLT, if any.
2048 Currently always 64K, since only SH-2A FDPIC uses this; a
2049 20-bit movi20 can address that many function descriptors below
2050 _GLOBAL_OFFSET_TABLE_. */
2051 #define MAX_SHORT_PLT 65536
2053 /* Return the index of the PLT entry at byte offset OFFSET. */
2056 get_plt_index (const struct elf_sh_plt_info
*info
, bfd_vma offset
)
2058 bfd_vma plt_index
= 0;
2060 offset
-= info
->plt0_entry_size
;
2061 if (info
->short_plt
!= NULL
)
2063 if (offset
> MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
)
2065 plt_index
= MAX_SHORT_PLT
;
2066 offset
-= MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
;
2069 info
= info
->short_plt
;
2071 return plt_index
+ offset
/ info
->symbol_entry_size
;
2074 /* Do the inverse operation. */
2077 get_plt_offset (const struct elf_sh_plt_info
*info
, bfd_vma plt_index
)
2081 if (info
->short_plt
!= NULL
)
2083 if (plt_index
> MAX_SHORT_PLT
)
2085 offset
= MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
;
2086 plt_index
-= MAX_SHORT_PLT
;
2089 info
= info
->short_plt
;
2091 return (offset
+ info
->plt0_entry_size
2092 + (plt_index
* info
->symbol_entry_size
));
2097 bfd_signed_vma refcount
;
2101 /* sh ELF linker hash entry. */
2103 struct elf_sh_link_hash_entry
2105 struct elf_link_hash_entry root
;
2107 /* Track dynamic relocs copied for this symbol. */
2108 struct elf_dyn_relocs
*dyn_relocs
;
2110 bfd_signed_vma gotplt_refcount
;
2112 /* A local function descriptor, for FDPIC. The refcount counts
2113 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2114 relocations; the PLT and GOT entry are accounted
2115 for separately. After adjust_dynamic_symbol, the offset is
2116 MINUS_ONE if there is no local descriptor (dynamic linker
2117 managed and no PLT entry, or undefined weak non-dynamic).
2118 During check_relocs we do not yet know whether the local
2119 descriptor will be canonical. */
2120 union gotref funcdesc
;
2122 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2123 and thus require fixups or relocations. */
2124 bfd_signed_vma abs_funcdesc_refcount
;
2127 GOT_UNKNOWN
= 0, GOT_NORMAL
, GOT_TLS_GD
, GOT_TLS_IE
, GOT_FUNCDESC
2131 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2133 struct sh_elf_obj_tdata
2135 struct elf_obj_tdata root
;
2137 /* got_type for each local got entry. */
2138 char *local_got_type
;
2140 /* Function descriptor refcount and offset for each local symbol. */
2141 union gotref
*local_funcdesc
;
2144 #define sh_elf_tdata(abfd) \
2145 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2147 #define sh_elf_local_got_type(abfd) \
2148 (sh_elf_tdata (abfd)->local_got_type)
2150 #define sh_elf_local_funcdesc(abfd) \
2151 (sh_elf_tdata (abfd)->local_funcdesc)
2153 #define is_sh_elf(bfd) \
2154 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2155 && elf_tdata (bfd) != NULL \
2156 && elf_object_id (bfd) == SH_ELF_DATA)
2158 /* Override the generic function because we need to store sh_elf_obj_tdata
2159 as the specific tdata. */
2162 sh_elf_mkobject (bfd
*abfd
)
2164 return bfd_elf_allocate_object (abfd
, sizeof (struct sh_elf_obj_tdata
),
2168 /* sh ELF linker hash table. */
2170 struct elf_sh_link_hash_table
2172 struct elf_link_hash_table root
;
2174 /* Short-cuts to get to dynamic linker sections. */
2177 asection
*sfuncdesc
;
2178 asection
*srelfuncdesc
;
2181 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2184 /* Small local sym cache. */
2185 struct sym_cache sym_cache
;
2187 /* A counter or offset to track a TLS got entry. */
2190 bfd_signed_vma refcount
;
2194 /* The type of PLT to use. */
2195 const struct elf_sh_plt_info
*plt_info
;
2197 /* True if the target system is VxWorks. */
2198 bfd_boolean vxworks_p
;
2200 /* True if the target system uses FDPIC. */
2201 bfd_boolean fdpic_p
;
2204 /* Traverse an sh ELF linker hash table. */
2206 #define sh_elf_link_hash_traverse(table, func, info) \
2207 (elf_link_hash_traverse \
2209 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2212 /* Get the sh ELF linker hash table from a link_info structure. */
2214 #define sh_elf_hash_table(p) \
2215 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2216 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2218 /* Create an entry in an sh ELF linker hash table. */
2220 static struct bfd_hash_entry
*
2221 sh_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
2222 struct bfd_hash_table
*table
,
2225 struct elf_sh_link_hash_entry
*ret
=
2226 (struct elf_sh_link_hash_entry
*) entry
;
2228 /* Allocate the structure if it has not already been allocated by a
2230 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2231 ret
= ((struct elf_sh_link_hash_entry
*)
2232 bfd_hash_allocate (table
,
2233 sizeof (struct elf_sh_link_hash_entry
)));
2234 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2235 return (struct bfd_hash_entry
*) ret
;
2237 /* Call the allocation method of the superclass. */
2238 ret
= ((struct elf_sh_link_hash_entry
*)
2239 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2241 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2243 ret
->dyn_relocs
= NULL
;
2244 ret
->gotplt_refcount
= 0;
2245 ret
->funcdesc
.refcount
= 0;
2246 ret
->abs_funcdesc_refcount
= 0;
2247 ret
->got_type
= GOT_UNKNOWN
;
2250 return (struct bfd_hash_entry
*) ret
;
2253 /* Create an sh ELF linker hash table. */
2255 static struct bfd_link_hash_table
*
2256 sh_elf_link_hash_table_create (bfd
*abfd
)
2258 struct elf_sh_link_hash_table
*ret
;
2259 size_t amt
= sizeof (struct elf_sh_link_hash_table
);
2261 ret
= (struct elf_sh_link_hash_table
*) bfd_zmalloc (amt
);
2262 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2265 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2266 sh_elf_link_hash_newfunc
,
2267 sizeof (struct elf_sh_link_hash_entry
),
2274 ret
->vxworks_p
= vxworks_object_p (abfd
);
2275 ret
->fdpic_p
= fdpic_object_p (abfd
);
2277 return &ret
->root
.root
;
2281 sh_elf_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2282 struct bfd_link_info
*info
, asection
*p
)
2284 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
2286 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2290 /* We need dynamic symbols for every section, since segments can
2291 relocate independently. */
2292 switch (elf_section_data (p
)->this_hdr
.sh_type
)
2296 /* If sh_type is yet undecided, assume it could be
2297 SHT_PROGBITS/SHT_NOBITS. */
2301 /* There shouldn't be section relative relocations
2302 against any other section. */
2308 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2309 shortcuts to them in our hash table. */
2312 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
2314 struct elf_sh_link_hash_table
*htab
;
2316 if (! _bfd_elf_create_got_section (dynobj
, info
))
2319 htab
= sh_elf_hash_table (info
);
2323 htab
->sfuncdesc
= bfd_make_section_anyway_with_flags (dynobj
, ".got.funcdesc",
2324 (SEC_ALLOC
| SEC_LOAD
2327 | SEC_LINKER_CREATED
));
2328 if (htab
->sfuncdesc
== NULL
2329 || !bfd_set_section_alignment (htab
->sfuncdesc
, 2))
2332 htab
->srelfuncdesc
= bfd_make_section_anyway_with_flags (dynobj
,
2333 ".rela.got.funcdesc",
2334 (SEC_ALLOC
| SEC_LOAD
2337 | SEC_LINKER_CREATED
2339 if (htab
->srelfuncdesc
== NULL
2340 || !bfd_set_section_alignment (htab
->srelfuncdesc
, 2))
2343 /* Also create .rofixup. */
2344 htab
->srofixup
= bfd_make_section_anyway_with_flags (dynobj
, ".rofixup",
2345 (SEC_ALLOC
| SEC_LOAD
2348 | SEC_LINKER_CREATED
2350 if (htab
->srofixup
== NULL
2351 || !bfd_set_section_alignment (htab
->srofixup
, 2))
2357 /* Create dynamic sections when linking against a dynamic object. */
2360 sh_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2362 struct elf_sh_link_hash_table
*htab
;
2363 flagword flags
, pltflags
;
2365 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2368 switch (bed
->s
->arch_size
)
2379 bfd_set_error (bfd_error_bad_value
);
2383 htab
= sh_elf_hash_table (info
);
2387 if (htab
->root
.dynamic_sections_created
)
2390 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2391 .rel[a].bss sections. */
2393 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2394 | SEC_LINKER_CREATED
);
2397 pltflags
|= SEC_CODE
;
2398 if (bed
->plt_not_loaded
)
2399 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2400 if (bed
->plt_readonly
)
2401 pltflags
|= SEC_READONLY
;
2403 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
2404 htab
->root
.splt
= s
;
2406 || !bfd_set_section_alignment (s
, bed
->plt_alignment
))
2409 if (bed
->want_plt_sym
)
2411 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2413 struct elf_link_hash_entry
*h
;
2414 struct bfd_link_hash_entry
*bh
= NULL
;
2416 if (! (_bfd_generic_link_add_one_symbol
2417 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2418 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
2419 get_elf_backend_data (abfd
)->collect
, &bh
)))
2422 h
= (struct elf_link_hash_entry
*) bh
;
2424 h
->type
= STT_OBJECT
;
2425 htab
->root
.hplt
= h
;
2427 if (bfd_link_pic (info
)
2428 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2432 s
= bfd_make_section_anyway_with_flags (abfd
,
2433 bed
->default_use_rela_p
2434 ? ".rela.plt" : ".rel.plt",
2435 flags
| SEC_READONLY
);
2436 htab
->root
.srelplt
= s
;
2438 || !bfd_set_section_alignment (s
, ptralign
))
2441 if (htab
->root
.sgot
== NULL
2442 && !create_got_section (abfd
, info
))
2445 if (bed
->want_dynbss
)
2447 /* The .dynbss section is a place to put symbols which are defined
2448 by dynamic objects, are referenced by regular objects, and are
2449 not functions. We must allocate space for them in the process
2450 image and use a R_*_COPY reloc to tell the dynamic linker to
2451 initialize them at run time. The linker script puts the .dynbss
2452 section into the .bss section of the final image. */
2453 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
2454 SEC_ALLOC
| SEC_LINKER_CREATED
);
2459 /* The .rel[a].bss section holds copy relocs. This section is not
2460 normally needed. We need to create it here, though, so that the
2461 linker will map it to an output section. We can't just create it
2462 only if we need it, because we will not know whether we need it
2463 until we have seen all the input files, and the first time the
2464 main linker code calls BFD after examining all the input files
2465 (size_dynamic_sections) the input sections have already been
2466 mapped to the output sections. If the section turns out not to
2467 be needed, we can discard it later. We will never need this
2468 section when generating a shared object, since they do not use
2470 if (! bfd_link_pic (info
))
2472 s
= bfd_make_section_anyway_with_flags (abfd
,
2473 (bed
->default_use_rela_p
2474 ? ".rela.bss" : ".rel.bss"),
2475 flags
| SEC_READONLY
);
2478 || !bfd_set_section_alignment (s
, ptralign
))
2483 if (htab
->vxworks_p
)
2485 if (!elf_vxworks_create_dynamic_sections (abfd
, info
, &htab
->srelplt2
))
2492 /* Find dynamic relocs for H that apply to read-only sections. */
2495 readonly_dynrelocs (struct elf_link_hash_entry
*h
)
2497 struct elf_dyn_relocs
*p
;
2499 for (p
= sh_elf_hash_entry (h
)->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2501 asection
*s
= p
->sec
->output_section
;
2503 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2509 /* Adjust a symbol defined by a dynamic object and referenced by a
2510 regular object. The current definition is in some section of the
2511 dynamic object, but we're not including those sections. We have to
2512 change the definition to something the rest of the link can
2516 sh_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2517 struct elf_link_hash_entry
*h
)
2519 struct elf_sh_link_hash_table
*htab
;
2522 htab
= sh_elf_hash_table (info
);
2526 /* Make sure we know what is going on here. */
2527 BFD_ASSERT (htab
->root
.dynobj
!= NULL
2532 && !h
->def_regular
)));
2534 /* If this is a function, put it in the procedure linkage table. We
2535 will fill in the contents of the procedure linkage table later,
2536 when we know the address of the .got section. */
2537 if (h
->type
== STT_FUNC
2540 if (h
->plt
.refcount
<= 0
2541 || SYMBOL_CALLS_LOCAL (info
, h
)
2542 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2543 && h
->root
.type
== bfd_link_hash_undefweak
))
2545 /* This case can occur if we saw a PLT reloc in an input
2546 file, but the symbol was never referred to by a dynamic
2547 object. In such a case, we don't actually need to build
2548 a procedure linkage table, and we can just do a REL32
2550 h
->plt
.offset
= (bfd_vma
) -1;
2557 h
->plt
.offset
= (bfd_vma
) -1;
2559 /* If this is a weak symbol, and there is a real definition, the
2560 processor independent code will have arranged for us to see the
2561 real definition first, and we can just use the same value. */
2562 if (h
->is_weakalias
)
2564 struct elf_link_hash_entry
*def
= weakdef (h
);
2565 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
2566 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
2567 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
2568 if (info
->nocopyreloc
)
2569 h
->non_got_ref
= def
->non_got_ref
;
2573 /* This is a reference to a symbol defined by a dynamic object which
2574 is not a function. */
2576 /* If we are creating a shared library, we must presume that the
2577 only references to the symbol are via the global offset table.
2578 For such cases we need not do anything here; the relocations will
2579 be handled correctly by relocate_section. */
2580 if (bfd_link_pic (info
))
2583 /* If there are no references to this symbol that do not use the
2584 GOT, we don't need to generate a copy reloc. */
2585 if (!h
->non_got_ref
)
2588 /* If -z nocopyreloc was given, we won't generate them either. */
2589 if (0 && info
->nocopyreloc
)
2595 /* If we don't find any dynamic relocs in read-only sections, then
2596 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2597 if (0 && !readonly_dynrelocs (h
))
2603 /* We must allocate the symbol in our .dynbss section, which will
2604 become part of the .bss section of the executable. There will be
2605 an entry for this symbol in the .dynsym section. The dynamic
2606 object will contain position independent code, so all references
2607 from the dynamic object to this symbol will go through the global
2608 offset table. The dynamic linker will use the .dynsym entry to
2609 determine the address it must put in the global offset table, so
2610 both the dynamic object and the regular object will refer to the
2611 same memory location for the variable. */
2614 BFD_ASSERT (s
!= NULL
);
2616 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2617 copy the initial value out of the dynamic object and into the
2618 runtime process image. We need to remember the offset into the
2619 .rela.bss section we are going to use. */
2620 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
2624 srel
= htab
->srelbss
;
2625 BFD_ASSERT (srel
!= NULL
);
2626 srel
->size
+= sizeof (Elf32_External_Rela
);
2630 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
2633 /* Allocate space in .plt, .got and associated reloc sections for
2637 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2639 struct bfd_link_info
*info
;
2640 struct elf_sh_link_hash_table
*htab
;
2641 struct elf_sh_link_hash_entry
*eh
;
2642 struct elf_dyn_relocs
*p
;
2644 if (h
->root
.type
== bfd_link_hash_indirect
)
2647 info
= (struct bfd_link_info
*) inf
;
2648 htab
= sh_elf_hash_table (info
);
2652 eh
= (struct elf_sh_link_hash_entry
*) h
;
2653 if ((h
->got
.refcount
> 0
2655 && eh
->gotplt_refcount
> 0)
2657 /* The symbol has been forced local, or we have some direct got refs,
2658 so treat all the gotplt refs as got refs. */
2659 h
->got
.refcount
+= eh
->gotplt_refcount
;
2660 if (h
->plt
.refcount
>= eh
->gotplt_refcount
)
2661 h
->plt
.refcount
-= eh
->gotplt_refcount
;
2664 if (htab
->root
.dynamic_sections_created
2665 && h
->plt
.refcount
> 0
2666 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2667 || h
->root
.type
!= bfd_link_hash_undefweak
))
2669 /* Make sure this symbol is output as a dynamic symbol.
2670 Undefined weak syms won't yet be marked as dynamic. */
2671 if (h
->dynindx
== -1
2672 && !h
->forced_local
)
2674 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2678 if (bfd_link_pic (info
)
2679 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2681 asection
*s
= htab
->root
.splt
;
2682 const struct elf_sh_plt_info
*plt_info
;
2684 /* If this is the first .plt entry, make room for the special
2687 s
->size
+= htab
->plt_info
->plt0_entry_size
;
2689 h
->plt
.offset
= s
->size
;
2691 /* If this symbol is not defined in a regular file, and we are
2692 not generating a shared library, then set the symbol to this
2693 location in the .plt. This is required to make function
2694 pointers compare as equal between the normal executable and
2695 the shared library. Skip this for FDPIC, since the
2696 function's address will be the address of the canonical
2697 function descriptor. */
2698 if (!htab
->fdpic_p
&& !bfd_link_pic (info
) && !h
->def_regular
)
2700 h
->root
.u
.def
.section
= s
;
2701 h
->root
.u
.def
.value
= h
->plt
.offset
;
2704 /* Make room for this entry. */
2705 plt_info
= htab
->plt_info
;
2706 if (plt_info
->short_plt
!= NULL
2707 && (get_plt_index (plt_info
->short_plt
, s
->size
) < MAX_SHORT_PLT
))
2708 plt_info
= plt_info
->short_plt
;
2709 s
->size
+= plt_info
->symbol_entry_size
;
2711 /* We also need to make an entry in the .got.plt section, which
2712 will be placed in the .got section by the linker script. */
2714 htab
->root
.sgotplt
->size
+= 4;
2716 htab
->root
.sgotplt
->size
+= 8;
2718 /* We also need to make an entry in the .rel.plt section. */
2719 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
2721 if (htab
->vxworks_p
&& !bfd_link_pic (info
))
2723 /* VxWorks executables have a second set of relocations
2724 for each PLT entry. They go in a separate relocation
2725 section, which is processed by the kernel loader. */
2727 /* There is a relocation for the initial PLT entry:
2728 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2729 if (h
->plt
.offset
== htab
->plt_info
->plt0_entry_size
)
2730 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
);
2732 /* There are two extra relocations for each subsequent
2733 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2734 and an R_SH_DIR32 relocation for the PLT entry. */
2735 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
) * 2;
2740 h
->plt
.offset
= (bfd_vma
) -1;
2746 h
->plt
.offset
= (bfd_vma
) -1;
2750 if (h
->got
.refcount
> 0)
2754 enum got_type got_type
= sh_elf_hash_entry (h
)->got_type
;
2756 /* Make sure this symbol is output as a dynamic symbol.
2757 Undefined weak syms won't yet be marked as dynamic. */
2758 if (h
->dynindx
== -1
2759 && !h
->forced_local
)
2761 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2765 s
= htab
->root
.sgot
;
2766 h
->got
.offset
= s
->size
;
2768 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2769 if (got_type
== GOT_TLS_GD
)
2771 dyn
= htab
->root
.dynamic_sections_created
;
2774 /* No dynamic relocations required. */
2775 if (htab
->fdpic_p
&& !bfd_link_pic (info
)
2776 && h
->root
.type
!= bfd_link_hash_undefweak
2777 && (got_type
== GOT_NORMAL
|| got_type
== GOT_FUNCDESC
))
2778 htab
->srofixup
->size
+= 4;
2780 /* No dynamic relocations required when IE->LE conversion happens. */
2781 else if (got_type
== GOT_TLS_IE
2783 && !bfd_link_pic (info
))
2785 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2786 R_SH_TLS_GD needs one if local symbol and two if global. */
2787 else if ((got_type
== GOT_TLS_GD
&& h
->dynindx
== -1)
2788 || got_type
== GOT_TLS_IE
)
2789 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2790 else if (got_type
== GOT_TLS_GD
)
2791 htab
->root
.srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
2792 else if (got_type
== GOT_FUNCDESC
)
2794 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2795 htab
->srofixup
->size
+= 4;
2797 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2799 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2800 || h
->root
.type
!= bfd_link_hash_undefweak
)
2801 && (bfd_link_pic (info
)
2802 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
2803 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
2804 else if (htab
->fdpic_p
2805 && !bfd_link_pic (info
)
2806 && got_type
== GOT_NORMAL
2807 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2808 || h
->root
.type
!= bfd_link_hash_undefweak
))
2809 htab
->srofixup
->size
+= 4;
2812 h
->got
.offset
= (bfd_vma
) -1;
2814 /* Allocate space for any dynamic relocations to function
2815 descriptors, canonical or otherwise. We need to relocate the
2816 reference unless it resolves to zero, which only happens for
2817 undefined weak symbols (either non-default visibility, or when
2818 static linking). Any GOT slot is accounted for elsewhere. */
2819 if (eh
->abs_funcdesc_refcount
> 0
2820 && (h
->root
.type
!= bfd_link_hash_undefweak
2821 || (htab
->root
.dynamic_sections_created
2822 && ! SYMBOL_CALLS_LOCAL (info
, h
))))
2824 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2825 htab
->srofixup
->size
+= eh
->abs_funcdesc_refcount
* 4;
2827 htab
->root
.srelgot
->size
2828 += eh
->abs_funcdesc_refcount
* sizeof (Elf32_External_Rela
);
2831 /* We must allocate a function descriptor if there are references to
2832 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
2833 the dynamic linker isn't going to allocate it. None of this
2834 applies if we already created one in .got.plt, but if the
2835 canonical function descriptor can be in this object, there
2836 won't be a PLT entry at all. */
2837 if ((eh
->funcdesc
.refcount
> 0
2838 || (h
->got
.offset
!= MINUS_ONE
&& eh
->got_type
== GOT_FUNCDESC
))
2839 && h
->root
.type
!= bfd_link_hash_undefweak
2840 && SYMBOL_FUNCDESC_LOCAL (info
, h
))
2842 /* Make room for this function descriptor. */
2843 eh
->funcdesc
.offset
= htab
->sfuncdesc
->size
;
2844 htab
->sfuncdesc
->size
+= 8;
2846 /* We will need a relocation or two fixups to initialize the
2847 function descriptor, so allocate those too. */
2848 if (!bfd_link_pic (info
) && SYMBOL_CALLS_LOCAL (info
, h
))
2849 htab
->srofixup
->size
+= 8;
2851 htab
->srelfuncdesc
->size
+= sizeof (Elf32_External_Rela
);
2854 if (eh
->dyn_relocs
== NULL
)
2857 /* In the shared -Bsymbolic case, discard space allocated for
2858 dynamic pc-relative relocs against symbols which turn out to be
2859 defined in regular objects. For the normal shared case, discard
2860 space for pc-relative relocs that have become local due to symbol
2861 visibility changes. */
2863 if (bfd_link_pic (info
))
2865 if (SYMBOL_CALLS_LOCAL (info
, h
))
2867 struct elf_dyn_relocs
**pp
;
2869 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2871 p
->count
-= p
->pc_count
;
2880 if (htab
->vxworks_p
)
2882 struct elf_dyn_relocs
**pp
;
2884 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2886 if (strcmp (p
->sec
->output_section
->name
, ".tls_vars") == 0)
2893 /* Also discard relocs on undefined weak syms with non-default
2895 if (eh
->dyn_relocs
!= NULL
2896 && h
->root
.type
== bfd_link_hash_undefweak
)
2898 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2899 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2900 eh
->dyn_relocs
= NULL
;
2902 /* Make sure undefined weak symbols are output as a dynamic
2904 else if (h
->dynindx
== -1
2905 && !h
->forced_local
)
2907 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2914 /* For the non-shared case, discard space for relocs against
2915 symbols which turn out to need copy relocs or are not
2921 || (htab
->root
.dynamic_sections_created
2922 && (h
->root
.type
== bfd_link_hash_undefweak
2923 || h
->root
.type
== bfd_link_hash_undefined
))))
2925 /* Make sure this symbol is output as a dynamic symbol.
2926 Undefined weak syms won't yet be marked as dynamic. */
2927 if (h
->dynindx
== -1
2928 && !h
->forced_local
)
2930 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2934 /* If that succeeded, we know we'll be keeping all the
2936 if (h
->dynindx
!= -1)
2940 eh
->dyn_relocs
= NULL
;
2945 /* Finally, allocate space. */
2946 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2948 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
2949 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
2951 /* If we need relocations, we do not need fixups. */
2952 if (htab
->fdpic_p
&& !bfd_link_pic (info
))
2953 htab
->srofixup
->size
-= 4 * (p
->count
- p
->pc_count
);
2959 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
2960 read-only sections. */
2963 maybe_set_textrel (struct elf_link_hash_entry
*h
, void *info_p
)
2967 if (h
->root
.type
== bfd_link_hash_indirect
)
2970 sec
= readonly_dynrelocs (h
);
2973 struct bfd_link_info
*info
= (struct bfd_link_info
*) info_p
;
2975 info
->flags
|= DF_TEXTREL
;
2976 info
->callbacks
->minfo
2977 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
2978 sec
->owner
, h
->root
.root
.string
, sec
);
2980 /* Not an error, just cut short the traversal. */
2986 /* This function is called after all the input files have been read,
2987 and the input sections have been assigned to output sections.
2988 It's a convenient place to determine the PLT style. */
2991 sh_elf_always_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
2993 sh_elf_hash_table (info
)->plt_info
= get_plt_info (output_bfd
,
2994 bfd_link_pic (info
));
2996 if (sh_elf_hash_table (info
)->fdpic_p
&& !bfd_link_relocatable (info
)
2997 && !bfd_elf_stack_segment_size (output_bfd
, info
,
2998 "__stacksize", DEFAULT_STACK_SIZE
))
3003 /* Set the sizes of the dynamic sections. */
3006 sh_elf_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
3007 struct bfd_link_info
*info
)
3009 struct elf_sh_link_hash_table
*htab
;
3015 htab
= sh_elf_hash_table (info
);
3019 dynobj
= htab
->root
.dynobj
;
3020 BFD_ASSERT (dynobj
!= NULL
);
3022 if (htab
->root
.dynamic_sections_created
)
3024 /* Set the contents of the .interp section to the interpreter. */
3025 if (bfd_link_executable (info
) && !info
->nointerp
)
3027 s
= bfd_get_linker_section (dynobj
, ".interp");
3028 BFD_ASSERT (s
!= NULL
);
3029 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
3030 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
3034 /* Set up .got offsets for local syms, and space for local dynamic
3036 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
3038 bfd_signed_vma
*local_got
;
3039 bfd_signed_vma
*end_local_got
;
3040 union gotref
*local_funcdesc
, *end_local_funcdesc
;
3041 char *local_got_type
;
3042 bfd_size_type locsymcount
;
3043 Elf_Internal_Shdr
*symtab_hdr
;
3046 if (! is_sh_elf (ibfd
))
3049 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3051 struct elf_dyn_relocs
*p
;
3053 for (p
= ((struct elf_dyn_relocs
*)
3054 elf_section_data (s
)->local_dynrel
);
3058 if (! bfd_is_abs_section (p
->sec
)
3059 && bfd_is_abs_section (p
->sec
->output_section
))
3061 /* Input section has been discarded, either because
3062 it is a copy of a linkonce section or due to
3063 linker script /DISCARD/, so we'll be discarding
3066 else if (htab
->vxworks_p
3067 && strcmp (p
->sec
->output_section
->name
,
3070 /* Relocations in vxworks .tls_vars sections are
3071 handled specially by the loader. */
3073 else if (p
->count
!= 0)
3075 srel
= elf_section_data (p
->sec
)->sreloc
;
3076 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
3077 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
3079 info
->flags
|= DF_TEXTREL
;
3080 info
->callbacks
->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
3081 p
->sec
->owner
, p
->sec
);
3084 /* If we need relocations, we do not need fixups. */
3085 if (htab
->fdpic_p
&& !bfd_link_pic (info
))
3086 htab
->srofixup
->size
-= 4 * (p
->count
- p
->pc_count
);
3091 symtab_hdr
= &elf_symtab_hdr (ibfd
);
3092 locsymcount
= symtab_hdr
->sh_info
;
3093 s
= htab
->root
.sgot
;
3094 srel
= htab
->root
.srelgot
;
3096 local_got
= elf_local_got_refcounts (ibfd
);
3099 end_local_got
= local_got
+ locsymcount
;
3100 local_got_type
= sh_elf_local_got_type (ibfd
);
3101 local_funcdesc
= sh_elf_local_funcdesc (ibfd
);
3102 for (; local_got
< end_local_got
; ++local_got
)
3106 *local_got
= s
->size
;
3108 if (*local_got_type
== GOT_TLS_GD
)
3110 if (bfd_link_pic (info
))
3111 srel
->size
+= sizeof (Elf32_External_Rela
);
3113 htab
->srofixup
->size
+= 4;
3115 if (*local_got_type
== GOT_FUNCDESC
)
3117 if (local_funcdesc
== NULL
)
3121 size
= locsymcount
* sizeof (union gotref
);
3122 local_funcdesc
= (union gotref
*) bfd_zalloc (ibfd
,
3124 if (local_funcdesc
== NULL
)
3126 sh_elf_local_funcdesc (ibfd
) = local_funcdesc
;
3127 local_funcdesc
+= (local_got
3128 - elf_local_got_refcounts (ibfd
));
3130 local_funcdesc
->refcount
++;
3135 *local_got
= (bfd_vma
) -1;
3140 local_funcdesc
= sh_elf_local_funcdesc (ibfd
);
3143 end_local_funcdesc
= local_funcdesc
+ locsymcount
;
3145 for (; local_funcdesc
< end_local_funcdesc
; ++local_funcdesc
)
3147 if (local_funcdesc
->refcount
> 0)
3149 local_funcdesc
->offset
= htab
->sfuncdesc
->size
;
3150 htab
->sfuncdesc
->size
+= 8;
3151 if (!bfd_link_pic (info
))
3152 htab
->srofixup
->size
+= 8;
3154 htab
->srelfuncdesc
->size
+= sizeof (Elf32_External_Rela
);
3157 local_funcdesc
->offset
= MINUS_ONE
;
3163 if (htab
->tls_ldm_got
.refcount
> 0)
3165 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3167 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
3168 htab
->root
.sgot
->size
+= 8;
3169 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
3172 htab
->tls_ldm_got
.offset
= -1;
3174 /* Only the reserved entries should be present. For FDPIC, they go at
3175 the end of .got.plt. */
3178 BFD_ASSERT (htab
->root
.sgotplt
&& htab
->root
.sgotplt
->size
== 12);
3179 htab
->root
.sgotplt
->size
= 0;
3182 /* Allocate global sym .plt and .got entries, and space for global
3183 sym dynamic relocs. */
3184 elf_link_hash_traverse (&htab
->root
, allocate_dynrelocs
, info
);
3186 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3187 end of the FDPIC .got.plt. */
3190 htab
->root
.hgot
->root
.u
.def
.value
= htab
->root
.sgotplt
->size
;
3191 htab
->root
.sgotplt
->size
+= 12;
3194 /* At the very end of the .rofixup section is a pointer to the GOT. */
3195 if (htab
->fdpic_p
&& htab
->srofixup
!= NULL
)
3196 htab
->srofixup
->size
+= 4;
3198 /* We now have determined the sizes of the various dynamic sections.
3199 Allocate memory for them. */
3201 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
3203 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
3206 if (s
== htab
->root
.splt
3207 || s
== htab
->root
.sgot
3208 || s
== htab
->root
.sgotplt
3209 || s
== htab
->sfuncdesc
3210 || s
== htab
->srofixup
3211 || s
== htab
->sdynbss
)
3213 /* Strip this section if we don't need it; see the
3216 else if (CONST_STRNEQ (bfd_section_name (s
), ".rela"))
3218 if (s
->size
!= 0 && s
!= htab
->root
.srelplt
&& s
!= htab
->srelplt2
)
3221 /* We use the reloc_count field as a counter if we need
3222 to copy relocs into the output file. */
3227 /* It's not one of our sections, so don't allocate space. */
3233 /* If we don't need this section, strip it from the
3234 output file. This is mostly to handle .rela.bss and
3235 .rela.plt. We must create both sections in
3236 create_dynamic_sections, because they must be created
3237 before the linker maps input sections to output
3238 sections. The linker does that before
3239 adjust_dynamic_symbol is called, and it is that
3240 function which decides whether anything needs to go
3241 into these sections. */
3243 s
->flags
|= SEC_EXCLUDE
;
3247 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3250 /* Allocate memory for the section contents. We use bfd_zalloc
3251 here in case unused entries are not reclaimed before the
3252 section's contents are written out. This should not happen,
3253 but this way if it does, we get a R_SH_NONE reloc instead
3255 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
3256 if (s
->contents
== NULL
)
3260 if (htab
->root
.dynamic_sections_created
)
3262 /* Add some entries to the .dynamic section. We fill in the
3263 values later, in sh_elf_finish_dynamic_sections, but we
3264 must add the entries now so that we get the correct size for
3265 the .dynamic section. The DT_DEBUG entry is filled in by the
3266 dynamic linker and used by the debugger. */
3267 #define add_dynamic_entry(TAG, VAL) \
3268 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3270 if (bfd_link_executable (info
))
3272 if (! add_dynamic_entry (DT_DEBUG
, 0))
3276 if (htab
->root
.splt
->size
!= 0)
3278 if (! add_dynamic_entry (DT_PLTGOT
, 0)
3279 || ! add_dynamic_entry (DT_PLTRELSZ
, 0)
3280 || ! add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3281 || ! add_dynamic_entry (DT_JMPREL
, 0))
3284 else if ((elf_elfheader (output_bfd
)->e_flags
& EF_SH_FDPIC
))
3286 if (! add_dynamic_entry (DT_PLTGOT
, 0))
3292 if (! add_dynamic_entry (DT_RELA
, 0)
3293 || ! add_dynamic_entry (DT_RELASZ
, 0)
3294 || ! add_dynamic_entry (DT_RELAENT
,
3295 sizeof (Elf32_External_Rela
)))
3298 /* If any dynamic relocs apply to a read-only section,
3299 then we need a DT_TEXTREL entry. */
3300 if ((info
->flags
& DF_TEXTREL
) == 0)
3301 elf_link_hash_traverse (&htab
->root
, maybe_set_textrel
, info
);
3303 if ((info
->flags
& DF_TEXTREL
) != 0)
3305 if (! add_dynamic_entry (DT_TEXTREL
, 0))
3310 && !elf_vxworks_add_dynamic_entries (output_bfd
, info
))
3313 #undef add_dynamic_entry
3318 /* Add a dynamic relocation to the SRELOC section. */
3320 inline static bfd_vma
3321 sh_elf_add_dyn_reloc (bfd
*output_bfd
, asection
*sreloc
, bfd_vma offset
,
3322 int reloc_type
, long dynindx
, bfd_vma addend
)
3324 Elf_Internal_Rela outrel
;
3325 bfd_vma reloc_offset
;
3327 outrel
.r_offset
= offset
;
3328 outrel
.r_info
= ELF32_R_INFO (dynindx
, reloc_type
);
3329 outrel
.r_addend
= addend
;
3331 reloc_offset
= sreloc
->reloc_count
* sizeof (Elf32_External_Rela
);
3332 BFD_ASSERT (reloc_offset
< sreloc
->size
);
3333 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3334 sreloc
->contents
+ reloc_offset
);
3335 sreloc
->reloc_count
++;
3337 return reloc_offset
;
3340 /* Add an FDPIC read-only fixup. */
3343 sh_elf_add_rofixup (bfd
*output_bfd
, asection
*srofixup
, bfd_vma offset
)
3345 bfd_vma fixup_offset
;
3347 fixup_offset
= srofixup
->reloc_count
++ * 4;
3348 BFD_ASSERT (fixup_offset
< srofixup
->size
);
3349 bfd_put_32 (output_bfd
, offset
, srofixup
->contents
+ fixup_offset
);
3352 /* Return the offset of the generated .got section from the
3353 _GLOBAL_OFFSET_TABLE_ symbol. */
3355 static bfd_signed_vma
3356 sh_elf_got_offset (struct elf_sh_link_hash_table
*htab
)
3358 return (htab
->root
.sgot
->output_offset
- htab
->root
.sgotplt
->output_offset
3359 - htab
->root
.hgot
->root
.u
.def
.value
);
3362 /* Find the segment number in which OSEC, and output section, is
3366 sh_elf_osec_to_segment (bfd
*output_bfd
, asection
*osec
)
3368 Elf_Internal_Phdr
*p
= NULL
;
3370 if (output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
3371 /* PR ld/17110: Do not look for output segments in an input bfd. */
3372 && output_bfd
->direction
!= read_direction
)
3373 p
= _bfd_elf_find_segment_containing_section (output_bfd
, osec
);
3375 /* FIXME: Nothing ever says what this index is relative to. The kernel
3376 supplies data in terms of the number of load segments but this is
3377 a phdr index and the first phdr may not be a load segment. */
3378 return (p
!= NULL
) ? p
- elf_tdata (output_bfd
)->phdr
: -1;
3382 sh_elf_osec_readonly_p (bfd
*output_bfd
, asection
*osec
)
3384 unsigned seg
= sh_elf_osec_to_segment (output_bfd
, osec
);
3386 return (seg
!= (unsigned) -1
3387 && ! (elf_tdata (output_bfd
)->phdr
[seg
].p_flags
& PF_W
));
3390 /* Generate the initial contents of a local function descriptor, along
3391 with any relocations or fixups required. */
3393 sh_elf_initialize_funcdesc (bfd
*output_bfd
,
3394 struct bfd_link_info
*info
,
3395 struct elf_link_hash_entry
*h
,
3400 struct elf_sh_link_hash_table
*htab
;
3404 htab
= sh_elf_hash_table (info
);
3406 /* FIXME: The ABI says that the offset to the function goes in the
3407 descriptor, along with the segment index. We're RELA, so it could
3408 go in the reloc instead... */
3410 if (h
!= NULL
&& SYMBOL_CALLS_LOCAL (info
, h
))
3412 section
= h
->root
.u
.def
.section
;
3413 value
= h
->root
.u
.def
.value
;
3416 if (h
== NULL
|| SYMBOL_CALLS_LOCAL (info
, h
))
3418 dynindx
= elf_section_data (section
->output_section
)->dynindx
;
3419 addr
= value
+ section
->output_offset
;
3420 seg
= sh_elf_osec_to_segment (output_bfd
, section
->output_section
);
3424 BFD_ASSERT (h
->dynindx
!= -1);
3425 dynindx
= h
->dynindx
;
3429 if (!bfd_link_pic (info
) && SYMBOL_CALLS_LOCAL (info
, h
))
3431 if (h
== NULL
|| h
->root
.type
!= bfd_link_hash_undefweak
)
3433 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
3435 + htab
->sfuncdesc
->output_section
->vma
3436 + htab
->sfuncdesc
->output_offset
);
3437 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
3439 + htab
->sfuncdesc
->output_section
->vma
3440 + htab
->sfuncdesc
->output_offset
);
3443 /* There are no dynamic relocations so fill in the final
3444 address and gp value (barring fixups). */
3445 addr
+= section
->output_section
->vma
;
3446 seg
= htab
->root
.hgot
->root
.u
.def
.value
3447 + htab
->root
.hgot
->root
.u
.def
.section
->output_section
->vma
3448 + htab
->root
.hgot
->root
.u
.def
.section
->output_offset
;
3451 sh_elf_add_dyn_reloc (output_bfd
, htab
->srelfuncdesc
,
3453 + htab
->sfuncdesc
->output_section
->vma
3454 + htab
->sfuncdesc
->output_offset
,
3455 R_SH_FUNCDESC_VALUE
, dynindx
, 0);
3457 bfd_put_32 (output_bfd
, addr
, htab
->sfuncdesc
->contents
+ offset
);
3458 bfd_put_32 (output_bfd
, seg
, htab
->sfuncdesc
->contents
+ offset
+ 4);
3463 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3464 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3467 static bfd_reloc_status_type
3468 install_movi20_field (bfd
*output_bfd
, unsigned long relocation
,
3469 bfd
*input_bfd
, asection
*input_section
,
3470 bfd_byte
*contents
, bfd_vma offset
)
3472 unsigned long cur_val
;
3474 bfd_reloc_status_type r
;
3476 if (offset
> bfd_get_section_limit (input_bfd
, input_section
))
3477 return bfd_reloc_outofrange
;
3479 r
= bfd_check_overflow (complain_overflow_signed
, 20, 0,
3480 bfd_arch_bits_per_address (input_bfd
), relocation
);
3481 if (r
!= bfd_reloc_ok
)
3484 addr
= contents
+ offset
;
3485 cur_val
= bfd_get_16 (output_bfd
, addr
);
3486 bfd_put_16 (output_bfd
, cur_val
| ((relocation
& 0xf0000) >> 12), addr
);
3487 bfd_put_16 (output_bfd
, relocation
& 0xffff, addr
+ 2);
3489 return bfd_reloc_ok
;
3492 /* Relocate an SH ELF section. */
3495 sh_elf_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
3496 bfd
*input_bfd
, asection
*input_section
,
3497 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
3498 Elf_Internal_Sym
*local_syms
,
3499 asection
**local_sections
)
3501 struct elf_sh_link_hash_table
*htab
;
3502 Elf_Internal_Shdr
*symtab_hdr
;
3503 struct elf_link_hash_entry
**sym_hashes
;
3504 Elf_Internal_Rela
*rel
, *relend
;
3505 bfd_vma
*local_got_offsets
;
3506 asection
*sgot
= NULL
;
3507 asection
*sgotplt
= NULL
;
3508 asection
*splt
= NULL
;
3509 asection
*sreloc
= NULL
;
3510 asection
*srelgot
= NULL
;
3511 bfd_boolean is_vxworks_tls
;
3512 unsigned isec_segment
, got_segment
, plt_segment
, check_segment
[2];
3513 bfd_boolean fdpic_p
= FALSE
;
3515 if (!is_sh_elf (input_bfd
))
3517 bfd_set_error (bfd_error_wrong_format
);
3521 htab
= sh_elf_hash_table (info
);
3524 sgot
= htab
->root
.sgot
;
3525 sgotplt
= htab
->root
.sgotplt
;
3526 srelgot
= htab
->root
.srelgot
;
3527 splt
= htab
->root
.splt
;
3528 fdpic_p
= htab
->fdpic_p
;
3530 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
3531 sym_hashes
= elf_sym_hashes (input_bfd
);
3532 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3534 isec_segment
= sh_elf_osec_to_segment (output_bfd
,
3535 input_section
->output_section
);
3536 if (fdpic_p
&& sgot
)
3537 got_segment
= sh_elf_osec_to_segment (output_bfd
,
3538 sgot
->output_section
);
3541 if (fdpic_p
&& splt
)
3542 plt_segment
= sh_elf_osec_to_segment (output_bfd
,
3543 splt
->output_section
);
3547 /* We have to handle relocations in vxworks .tls_vars sections
3548 specially, because the dynamic loader is 'weird'. */
3549 is_vxworks_tls
= (htab
&& htab
->vxworks_p
&& bfd_link_pic (info
)
3550 && !strcmp (input_section
->output_section
->name
,
3554 relend
= relocs
+ input_section
->reloc_count
;
3555 for (; rel
< relend
; rel
++)
3558 reloc_howto_type
*howto
;
3559 unsigned long r_symndx
;
3560 Elf_Internal_Sym
*sym
;
3562 struct elf_link_hash_entry
*h
;
3564 bfd_vma addend
= (bfd_vma
) 0;
3565 bfd_reloc_status_type r
;
3566 int seen_stt_datalabel
= 0;
3568 enum got_type got_type
;
3569 const char *symname
= NULL
;
3570 bfd_boolean resolved_to_zero
;
3572 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3574 r_type
= ELF32_R_TYPE (rel
->r_info
);
3576 /* Many of the relocs are only used for relaxing, and are
3577 handled entirely by the relaxation code. */
3578 if (r_type
>= (int) R_SH_GNU_VTINHERIT
3579 && r_type
<= (int) R_SH_LABEL
)
3581 if (r_type
== (int) R_SH_NONE
)
3585 || r_type
>= R_SH_max
3586 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
3587 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
3588 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
3589 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
)
3590 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_3
3591 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_3
)
3592 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_4
3593 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_4
)
3594 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_5
3595 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_5
)
3596 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_6
3597 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_6
))
3599 bfd_set_error (bfd_error_bad_value
);
3603 howto
= get_howto_table (output_bfd
) + r_type
;
3605 /* For relocs that aren't partial_inplace, we get the addend from
3607 if (! howto
->partial_inplace
)
3608 addend
= rel
->r_addend
;
3610 resolved_to_zero
= FALSE
;
3614 check_segment
[0] = -1;
3615 check_segment
[1] = -1;
3616 if (r_symndx
< symtab_hdr
->sh_info
)
3618 sym
= local_syms
+ r_symndx
;
3619 sec
= local_sections
[r_symndx
];
3621 symname
= bfd_elf_string_from_elf_section
3622 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
3623 if (symname
== NULL
|| *symname
== '\0')
3624 symname
= bfd_section_name (sec
);
3626 relocation
= (sec
->output_section
->vma
3627 + sec
->output_offset
3629 /* A local symbol never has STO_SH5_ISA32, so we don't need
3630 datalabel processing here. Make sure this does not change
3632 if ((sym
->st_other
& STO_SH5_ISA32
) != 0)
3633 (*info
->callbacks
->reloc_dangerous
)
3635 _("unexpected STO_SH5_ISA32 on local symbol is not handled"),
3636 input_bfd
, input_section
, rel
->r_offset
);
3638 if (sec
!= NULL
&& discarded_section (sec
))
3639 /* Handled below. */
3641 else if (bfd_link_relocatable (info
))
3643 /* This is a relocatable link. We don't have to change
3644 anything, unless the reloc is against a section symbol,
3645 in which case we have to adjust according to where the
3646 section symbol winds up in the output section. */
3647 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3649 if (! howto
->partial_inplace
)
3651 /* For relocations with the addend in the
3652 relocation, we need just to update the addend.
3653 All real relocs are of type partial_inplace; this
3654 code is mostly for completeness. */
3655 rel
->r_addend
+= sec
->output_offset
;
3660 /* Relocs of type partial_inplace need to pick up the
3661 contents in the contents and add the offset resulting
3662 from the changed location of the section symbol.
3663 Using _bfd_final_link_relocate (e.g. goto
3664 final_link_relocate) here would be wrong, because
3665 relocations marked pc_relative would get the current
3666 location subtracted, and we must only do that at the
3668 r
= _bfd_relocate_contents (howto
, input_bfd
,
3671 contents
+ rel
->r_offset
);
3672 goto relocation_done
;
3677 else if (! howto
->partial_inplace
)
3679 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3680 addend
= rel
->r_addend
;
3682 else if ((sec
->flags
& SEC_MERGE
)
3683 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3687 if (howto
->rightshift
|| howto
->src_mask
!= 0xffffffff)
3690 /* xgettext:c-format */
3691 (_("%pB(%pA+%#" PRIx64
"): "
3692 "%s relocation against SEC_MERGE section"),
3693 input_bfd
, input_section
,
3694 (uint64_t) rel
->r_offset
, howto
->name
);
3698 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
3701 _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
)
3703 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
3704 bfd_put_32 (input_bfd
, addend
, contents
+ rel
->r_offset
);
3710 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3713 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
3714 symname
= h
->root
.root
.string
;
3715 while (h
->root
.type
== bfd_link_hash_indirect
3716 || h
->root
.type
== bfd_link_hash_warning
)
3717 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3718 if (h
->root
.type
== bfd_link_hash_defined
3719 || h
->root
.type
== bfd_link_hash_defweak
)
3723 dyn
= htab
? htab
->root
.dynamic_sections_created
: FALSE
;
3724 sec
= h
->root
.u
.def
.section
;
3725 /* In these cases, we don't need the relocation value.
3726 We check specially because in some obscure cases
3727 sec->output_section will be NULL. */
3728 if (r_type
== R_SH_GOTPC
3729 || r_type
== R_SH_GOTPC_LOW16
3730 || r_type
== R_SH_GOTPC_MEDLOW16
3731 || r_type
== R_SH_GOTPC_MEDHI16
3732 || r_type
== R_SH_GOTPC_HI16
3733 || ((r_type
== R_SH_PLT32
3734 || r_type
== R_SH_PLT_LOW16
3735 || r_type
== R_SH_PLT_MEDLOW16
3736 || r_type
== R_SH_PLT_MEDHI16
3737 || r_type
== R_SH_PLT_HI16
)
3738 && h
->plt
.offset
!= (bfd_vma
) -1)
3739 || ((r_type
== R_SH_GOT32
3740 || r_type
== R_SH_GOT20
3741 || r_type
== R_SH_GOTFUNCDESC
3742 || r_type
== R_SH_GOTFUNCDESC20
3743 || r_type
== R_SH_GOTOFFFUNCDESC
3744 || r_type
== R_SH_GOTOFFFUNCDESC20
3745 || r_type
== R_SH_FUNCDESC
3746 || r_type
== R_SH_GOT_LOW16
3747 || r_type
== R_SH_GOT_MEDLOW16
3748 || r_type
== R_SH_GOT_MEDHI16
3749 || r_type
== R_SH_GOT_HI16
)
3750 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
3751 bfd_link_pic (info
),
3753 && (! bfd_link_pic (info
)
3754 || (! info
->symbolic
&& h
->dynindx
!= -1)
3755 || !h
->def_regular
))
3756 /* The cases above are those in which relocation is
3757 overwritten in the switch block below. The cases
3758 below are those in which we must defer relocation
3759 to run-time, because we can't resolve absolute
3760 addresses when creating a shared library. */
3761 || (bfd_link_pic (info
)
3762 && ((! info
->symbolic
&& h
->dynindx
!= -1)
3764 && ((r_type
== R_SH_DIR32
3765 && !h
->forced_local
)
3766 || (r_type
== R_SH_REL32
3767 && !SYMBOL_CALLS_LOCAL (info
, h
)))
3768 && ((input_section
->flags
& SEC_ALLOC
) != 0
3769 /* DWARF will emit R_SH_DIR32 relocations in its
3770 sections against symbols defined externally
3771 in shared libraries. We can't do anything
3773 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
3774 && h
->def_dynamic
)))
3775 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3776 sections because such sections are not SEC_ALLOC and
3777 thus ld.so will not process them. */
3778 || (sec
->output_section
== NULL
3779 && ((input_section
->flags
& SEC_DEBUGGING
) != 0
3781 || (sec
->output_section
== NULL
3782 && (sh_elf_hash_entry (h
)->got_type
== GOT_TLS_IE
3783 || sh_elf_hash_entry (h
)->got_type
== GOT_TLS_GD
)))
3785 else if (sec
->output_section
!= NULL
)
3786 relocation
= ((h
->root
.u
.def
.value
3787 + sec
->output_section
->vma
3788 + sec
->output_offset
)
3789 /* A STO_SH5_ISA32 causes a "bitor 1" to the
3790 symbol value, unless we've seen
3791 STT_DATALABEL on the way to it. */
3792 | ((h
->other
& STO_SH5_ISA32
) != 0
3793 && ! seen_stt_datalabel
));
3794 else if (!bfd_link_relocatable (info
)
3795 && (_bfd_elf_section_offset (output_bfd
, info
,
3801 /* xgettext:c-format */
3802 (_("%pB(%pA+%#" PRIx64
"): "
3803 "unresolvable %s relocation against symbol `%s'"),
3806 (uint64_t) rel
->r_offset
,
3808 h
->root
.root
.string
);
3812 else if (h
->root
.type
== bfd_link_hash_undefweak
)
3813 resolved_to_zero
= UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
);
3814 else if (info
->unresolved_syms_in_objects
== RM_IGNORE
3815 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3817 else if (!bfd_link_relocatable (info
))
3818 info
->callbacks
->undefined_symbol
3819 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
3821 (info
->unresolved_syms_in_objects
== RM_DIAGNOSE
3822 && !info
->warn_unresolved_syms
)
3823 || ELF_ST_VISIBILITY (h
->other
));
3826 if (sec
!= NULL
&& discarded_section (sec
))
3827 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
3828 rel
, 1, relend
, howto
, 0, contents
);
3830 if (bfd_link_relocatable (info
))
3833 /* Check for inter-segment relocations in FDPIC files. Most
3834 relocations connect the relocation site to the location of
3835 the target symbol, but there are some exceptions below. */
3836 check_segment
[0] = isec_segment
;
3838 check_segment
[1] = sh_elf_osec_to_segment (output_bfd
,
3839 sec
->output_section
);
3841 check_segment
[1] = -1;
3843 switch ((int) r_type
)
3845 final_link_relocate
:
3846 /* COFF relocs don't use the addend. The addend is used for
3847 R_SH_DIR32 to be compatible with other compilers. */
3848 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3849 contents
, rel
->r_offset
,
3850 relocation
, addend
);
3854 goto final_link_relocate
;
3859 /* If the reloc is against the start of this section, then
3860 the assembler has already taken care of it and the reloc
3861 is here only to assist in relaxing. If the reloc is not
3862 against the start of this section, then it's against an
3863 external symbol and we must deal with it ourselves. */
3864 if (input_section
->output_section
->vma
+ input_section
->output_offset
3867 int disp
= (relocation
3868 - input_section
->output_section
->vma
3869 - input_section
->output_offset
3875 case R_SH_DIR8WPZ
: mask
= 1; break;
3876 case R_SH_DIR8WPL
: mask
= 3; break;
3877 default: mask
= 0; break;
3882 /* xgettext:c-format */
3883 (_("%pB: %#" PRIx64
": fatal: "
3884 "unaligned branch target for relax-support relocation"),
3885 input_section
->owner
,
3886 (uint64_t) rel
->r_offset
);
3887 bfd_set_error (bfd_error_bad_value
);
3891 goto final_link_relocate
;
3897 bfd_set_error (bfd_error_bad_value
);
3905 goto final_link_relocate
;
3912 /* xgettext:c-format */
3913 (_("%pB: %#" PRIx64
": fatal: "
3914 "unaligned %s relocation %#" PRIx64
),
3915 input_section
->owner
, (uint64_t) rel
->r_offset
,
3916 howto
->name
, (uint64_t) relocation
);
3917 bfd_set_error (bfd_error_bad_value
);
3920 goto final_link_relocate
;
3928 /* xgettext:c-format */
3929 (_("%pB: %#" PRIx64
": fatal: "
3930 "unaligned %s relocation %#" PRIx64
""),
3931 input_section
->owner
,
3932 (uint64_t) rel
->r_offset
, howto
->name
,
3933 (uint64_t) relocation
);
3934 bfd_set_error (bfd_error_bad_value
);
3937 goto final_link_relocate
;
3940 if ((signed int)relocation
< -32
3941 || (signed int)relocation
> 32)
3944 /* xgettext:c-format */
3945 (_("%pB: %#" PRIx64
": fatal: R_SH_PSHA relocation %" PRId64
3946 " not in range -32..32"),
3947 input_section
->owner
,
3948 (uint64_t) rel
->r_offset
,
3949 (int64_t) relocation
);
3950 bfd_set_error (bfd_error_bad_value
);
3953 goto final_link_relocate
;
3956 if ((signed int)relocation
< -16
3957 || (signed int)relocation
> 16)
3960 /* xgettext:c-format */
3961 (_("%pB: %#" PRIx64
": fatal: R_SH_PSHL relocation %" PRId64
3962 " not in range -32..32"),
3963 input_section
->owner
,
3964 (uint64_t) rel
->r_offset
,
3965 (int64_t) relocation
);
3966 bfd_set_error (bfd_error_bad_value
);
3969 goto final_link_relocate
;
3973 if (bfd_link_pic (info
)
3975 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3976 && !resolved_to_zero
)
3977 || h
->root
.type
!= bfd_link_hash_undefweak
)
3978 && r_symndx
!= STN_UNDEF
3979 && (input_section
->flags
& SEC_ALLOC
) != 0
3981 && (r_type
== R_SH_DIR32
3982 || !SYMBOL_CALLS_LOCAL (info
, h
)))
3984 Elf_Internal_Rela outrel
;
3986 bfd_boolean skip
, relocate
;
3988 /* When generating a shared object, these relocations
3989 are copied into the output file to be resolved at run
3994 sreloc
= _bfd_elf_get_dynamic_reloc_section
3995 (input_bfd
, input_section
, /*rela?*/ TRUE
);
4004 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
4006 if (outrel
.r_offset
== (bfd_vma
) -1)
4008 else if (outrel
.r_offset
== (bfd_vma
) -2)
4009 skip
= TRUE
, relocate
= TRUE
;
4010 outrel
.r_offset
+= (input_section
->output_section
->vma
4011 + input_section
->output_offset
);
4014 memset (&outrel
, 0, sizeof outrel
);
4015 else if (r_type
== R_SH_REL32
)
4017 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
4018 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
4020 = (howto
->partial_inplace
4021 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4026 || ((info
->symbolic
|| h
->dynindx
== -1)
4027 && h
->def_regular
)))
4031 BFD_ASSERT (sec
!= NULL
);
4032 BFD_ASSERT (sec
->output_section
!= NULL
);
4033 dynindx
= elf_section_data (sec
->output_section
)->dynindx
;
4034 outrel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
4035 outrel
.r_addend
= relocation
;
4037 += (howto
->partial_inplace
4038 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4040 outrel
.r_addend
-= sec
->output_section
->vma
;
4044 /* h->dynindx may be -1 if this symbol was marked to
4047 || ((info
->symbolic
|| h
->dynindx
== -1)
4050 relocate
= howto
->partial_inplace
;
4051 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4055 BFD_ASSERT (h
->dynindx
!= -1);
4056 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
4058 outrel
.r_addend
= relocation
;
4060 += (howto
->partial_inplace
4061 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4065 loc
= sreloc
->contents
;
4066 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4067 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4069 check_segment
[0] = check_segment
[1] = -1;
4071 /* If this reloc is against an external symbol, we do
4072 not want to fiddle with the addend. Otherwise, we
4073 need to include the symbol value so that it becomes
4074 an addend for the dynamic reloc. */
4078 else if (fdpic_p
&& !bfd_link_pic (info
)
4079 && r_type
== R_SH_DIR32
4080 && (input_section
->flags
& SEC_ALLOC
) != 0)
4086 if (sh_elf_osec_readonly_p (output_bfd
,
4087 input_section
->output_section
))
4090 /* xgettext:c-format */
4091 (_("%pB(%pA+%#" PRIx64
"): "
4092 "cannot emit fixup to `%s' in read-only section"),
4095 (uint64_t) rel
->r_offset
,
4100 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4101 input_section
, rel
->r_offset
);
4102 if (offset
!= (bfd_vma
)-1)
4103 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4104 input_section
->output_section
->vma
4105 + input_section
->output_offset
4108 check_segment
[0] = check_segment
[1] = -1;
4110 /* We don't want warnings for non-NULL tests on undefined weak
4112 else if (r_type
== R_SH_REL32
4114 && h
->root
.type
== bfd_link_hash_undefweak
)
4115 check_segment
[0] = check_segment
[1] = -1;
4116 goto final_link_relocate
;
4119 /* Relocation is to the entry for this symbol in the
4120 procedure linkage table. */
4124 || ! bfd_link_pic (info
)
4127 || h
->plt
.offset
== (bfd_vma
) -1
4128 || h
->got
.offset
!= (bfd_vma
) -1)
4131 /* Relocation is to the entry for this symbol in the global
4132 offset table extension for the procedure linkage table. */
4135 BFD_ASSERT (sgotplt
!= NULL
);
4136 relocation
= (sgotplt
->output_offset
4137 + (get_plt_index (htab
->plt_info
, h
->plt
.offset
)
4141 relocation
-= GOT_BIAS
;
4144 goto final_link_relocate
;
4149 /* Relocation is to the entry for this symbol in the global
4153 BFD_ASSERT (sgot
!= NULL
);
4154 check_segment
[0] = check_segment
[1] = -1;
4160 off
= h
->got
.offset
;
4161 BFD_ASSERT (off
!= (bfd_vma
) -1);
4163 dyn
= htab
->root
.dynamic_sections_created
;
4164 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
4165 bfd_link_pic (info
),
4167 || (bfd_link_pic (info
)
4168 && SYMBOL_REFERENCES_LOCAL (info
, h
))
4169 || ((ELF_ST_VISIBILITY (h
->other
)
4170 || resolved_to_zero
)
4171 && h
->root
.type
== bfd_link_hash_undefweak
))
4173 /* This is actually a static link, or it is a
4174 -Bsymbolic link and the symbol is defined
4175 locally, or the symbol was forced to be local
4176 because of a version file. We must initialize
4177 this entry in the global offset table. Since the
4178 offset must always be a multiple of 4, we use the
4179 least significant bit to record whether we have
4180 initialized it already.
4182 When doing a dynamic link, we create a .rela.got
4183 relocation entry to initialize the value. This
4184 is done in the finish_dynamic_symbol routine. */
4189 bfd_put_32 (output_bfd
, relocation
,
4190 sgot
->contents
+ off
);
4193 /* If we initialize the GOT entry here with a valid
4194 symbol address, also add a fixup. */
4195 if (fdpic_p
&& !bfd_link_pic (info
)
4196 && sh_elf_hash_entry (h
)->got_type
== GOT_NORMAL
4197 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4198 || h
->root
.type
!= bfd_link_hash_undefweak
))
4199 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4200 sgot
->output_section
->vma
4201 + sgot
->output_offset
4206 relocation
= sh_elf_got_offset (htab
) + off
;
4210 BFD_ASSERT (local_got_offsets
!= NULL
4211 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
4213 off
= local_got_offsets
[r_symndx
];
4215 /* The offset must always be a multiple of 4. We use
4216 the least significant bit to record whether we have
4217 already generated the necessary reloc. */
4222 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
4224 if (bfd_link_pic (info
))
4226 Elf_Internal_Rela outrel
;
4229 outrel
.r_offset
= (sgot
->output_section
->vma
4230 + sgot
->output_offset
4235 = elf_section_data (sec
->output_section
)->dynindx
;
4236 outrel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
4237 outrel
.r_addend
= relocation
;
4238 outrel
.r_addend
-= sec
->output_section
->vma
;
4242 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4243 outrel
.r_addend
= relocation
;
4245 loc
= srelgot
->contents
;
4246 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4247 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4250 && (sh_elf_local_got_type (input_bfd
) [r_symndx
]
4252 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4253 sgot
->output_section
->vma
4254 + sgot
->output_offset
4257 local_got_offsets
[r_symndx
] |= 1;
4260 relocation
= sh_elf_got_offset (htab
) + off
;
4264 relocation
-= GOT_BIAS
;
4267 if (r_type
== R_SH_GOT20
)
4269 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4270 input_bfd
, input_section
, contents
,
4275 goto final_link_relocate
;
4279 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4280 we place at the start of the .got.plt section. This is the same
4281 as the start of the output .got section, unless there are function
4282 descriptors in front of it. */
4284 BFD_ASSERT (sgotplt
!= NULL
);
4285 check_segment
[0] = got_segment
;
4286 relocation
-= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4287 + htab
->root
.hgot
->root
.u
.def
.value
;
4290 relocation
-= GOT_BIAS
;
4293 addend
= rel
->r_addend
;
4295 if (r_type
== R_SH_GOTOFF20
)
4297 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4298 input_bfd
, input_section
, contents
,
4303 goto final_link_relocate
;
4306 /* Use global offset table as symbol value. */
4308 BFD_ASSERT (sgotplt
!= NULL
);
4309 relocation
= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
;
4312 relocation
+= GOT_BIAS
;
4315 addend
= rel
->r_addend
;
4317 goto final_link_relocate
;
4320 /* Relocation is to the entry for this symbol in the
4321 procedure linkage table. */
4323 /* Resolve a PLT reloc against a local symbol directly,
4324 without using the procedure linkage table. */
4326 goto final_link_relocate
;
4328 /* We don't want to warn on calls to undefined weak symbols,
4329 as calls to them must be protected by non-NULL tests
4330 anyway, and unprotected calls would invoke undefined
4332 if (h
->root
.type
== bfd_link_hash_undefweak
)
4333 check_segment
[0] = check_segment
[1] = -1;
4335 if (h
->forced_local
)
4336 goto final_link_relocate
;
4338 if (h
->plt
.offset
== (bfd_vma
) -1)
4340 /* We didn't make a PLT entry for this symbol. This
4341 happens when statically linking PIC code, or when
4342 using -Bsymbolic. */
4343 goto final_link_relocate
;
4346 BFD_ASSERT (splt
!= NULL
);
4347 check_segment
[1] = plt_segment
;
4348 relocation
= (splt
->output_section
->vma
4349 + splt
->output_offset
4352 addend
= rel
->r_addend
;
4354 goto final_link_relocate
;
4356 /* Relocation is to the canonical function descriptor for this
4357 symbol, possibly via the GOT. Initialize the GOT
4358 entry and function descriptor if necessary. */
4359 case R_SH_GOTFUNCDESC
:
4360 case R_SH_GOTFUNCDESC20
:
4364 asection
*reloc_section
;
4365 bfd_vma reloc_offset
;
4366 int reloc_type
= R_SH_FUNCDESC
;
4370 check_segment
[0] = check_segment
[1] = -1;
4372 /* FIXME: See what FRV does for global symbols in the
4373 executable, with --export-dynamic. Do they need ld.so
4374 to allocate official descriptors? See what this code
4380 if (r_type
== R_SH_FUNCDESC
)
4382 reloc_section
= input_section
;
4383 reloc_offset
= rel
->r_offset
;
4387 reloc_section
= sgot
;
4390 reloc_offset
= h
->got
.offset
;
4393 BFD_ASSERT (local_got_offsets
!= NULL
);
4394 reloc_offset
= local_got_offsets
[r_symndx
];
4396 BFD_ASSERT (reloc_offset
!= MINUS_ONE
);
4398 if (reloc_offset
& 1)
4401 goto funcdesc_done_got
;
4405 if (h
&& h
->root
.type
== bfd_link_hash_undefweak
4406 && (SYMBOL_CALLS_LOCAL (info
, h
)
4407 || !htab
->root
.dynamic_sections_created
))
4408 /* Undefined weak symbol which will not be dynamically
4409 resolved later; leave it at zero. */
4410 goto funcdesc_leave_zero
;
4411 else if (SYMBOL_CALLS_LOCAL (info
, h
)
4412 && ! SYMBOL_FUNCDESC_LOCAL (info
, h
))
4414 /* If the symbol needs a non-local function descriptor
4415 but binds locally (i.e., its visibility is
4416 protected), emit a dynamic relocation decayed to
4417 section+offset. This is an optimization; the dynamic
4418 linker would resolve our function descriptor request
4419 to our copy of the function anyway. */
4420 dynindx
= elf_section_data (h
->root
.u
.def
.section
4421 ->output_section
)->dynindx
;
4422 relocation
+= h
->root
.u
.def
.section
->output_offset
4423 + h
->root
.u
.def
.value
;
4425 else if (! SYMBOL_FUNCDESC_LOCAL (info
, h
))
4427 /* If the symbol is dynamic and there will be dynamic
4428 symbol resolution because we are or are linked with a
4429 shared library, emit a FUNCDESC relocation such that
4430 the dynamic linker will allocate the function
4432 BFD_ASSERT (h
->dynindx
!= -1);
4433 dynindx
= h
->dynindx
;
4439 /* Otherwise, we know we have a private function
4440 descriptor, so reference it directly. */
4441 reloc_type
= R_SH_DIR32
;
4442 dynindx
= elf_section_data (htab
->sfuncdesc
4443 ->output_section
)->dynindx
;
4447 offset
= sh_elf_hash_entry (h
)->funcdesc
.offset
;
4448 BFD_ASSERT (offset
!= MINUS_ONE
);
4449 if ((offset
& 1) == 0)
4451 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, h
,
4454 sh_elf_hash_entry (h
)->funcdesc
.offset
|= 1;
4459 union gotref
*local_funcdesc
;
4461 local_funcdesc
= sh_elf_local_funcdesc (input_bfd
);
4462 offset
= local_funcdesc
[r_symndx
].offset
;
4463 BFD_ASSERT (offset
!= MINUS_ONE
);
4464 if ((offset
& 1) == 0)
4466 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, NULL
,
4470 local_funcdesc
[r_symndx
].offset
|= 1;
4474 relocation
= htab
->sfuncdesc
->output_offset
+ (offset
& ~1);
4477 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
4481 if (sh_elf_osec_readonly_p (output_bfd
,
4482 reloc_section
->output_section
))
4485 /* xgettext:c-format */
4486 (_("%pB(%pA+%#" PRIx64
"): "
4487 "cannot emit fixup to `%s' in read-only section"),
4490 (uint64_t) rel
->r_offset
,
4495 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4496 reloc_section
, reloc_offset
);
4498 if (offset
!= (bfd_vma
)-1)
4499 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4501 + reloc_section
->output_section
->vma
4502 + reloc_section
->output_offset
);
4504 else if ((reloc_section
->output_section
->flags
4505 & (SEC_ALLOC
| SEC_LOAD
)) == (SEC_ALLOC
| SEC_LOAD
))
4509 if (sh_elf_osec_readonly_p (output_bfd
,
4510 reloc_section
->output_section
))
4512 info
->callbacks
->warning
4514 _("cannot emit dynamic relocations in read-only section"),
4515 symname
, input_bfd
, reloc_section
, reloc_offset
);
4519 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4520 reloc_section
, reloc_offset
);
4522 if (offset
!= (bfd_vma
)-1)
4523 sh_elf_add_dyn_reloc (output_bfd
, srelgot
,
4525 + reloc_section
->output_section
->vma
4526 + reloc_section
->output_offset
,
4527 reloc_type
, dynindx
, relocation
);
4529 if (r_type
== R_SH_FUNCDESC
)
4537 goto funcdesc_leave_zero
;
4541 if (SYMBOL_FUNCDESC_LOCAL (info
, h
))
4542 relocation
+= htab
->sfuncdesc
->output_section
->vma
;
4543 funcdesc_leave_zero
:
4544 if (r_type
!= R_SH_FUNCDESC
)
4546 bfd_put_32 (output_bfd
, relocation
,
4547 reloc_section
->contents
+ reloc_offset
);
4551 local_got_offsets
[r_symndx
] |= 1;
4555 relocation
= sh_elf_got_offset (htab
) + reloc_offset
;
4557 relocation
-= GOT_BIAS
;
4560 if (r_type
== R_SH_GOTFUNCDESC20
)
4562 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4563 input_bfd
, input_section
, contents
,
4568 goto final_link_relocate
;
4572 case R_SH_GOTOFFFUNCDESC
:
4573 case R_SH_GOTOFFFUNCDESC20
:
4574 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4575 executable and --export-dynamic. If such symbols get
4576 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
4580 check_segment
[0] = check_segment
[1] = -1;
4582 addend
= rel
->r_addend
;
4584 if (h
&& (h
->root
.type
== bfd_link_hash_undefweak
4585 || !SYMBOL_FUNCDESC_LOCAL (info
, h
)))
4588 /* xgettext:c-format */
4589 (_("%pB(%pA+%#" PRIx64
"): "
4590 "%s relocation against external symbol \"%s\""),
4591 input_bfd
, input_section
, (uint64_t) rel
->r_offset
,
4592 howto
->name
, h
->root
.root
.string
);
4599 /* Otherwise, we know we have a private function
4600 descriptor, so reference it directly. */
4603 offset
= sh_elf_hash_entry (h
)->funcdesc
.offset
;
4604 BFD_ASSERT (offset
!= MINUS_ONE
);
4605 if ((offset
& 1) == 0)
4607 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, h
,
4610 sh_elf_hash_entry (h
)->funcdesc
.offset
|= 1;
4615 union gotref
*local_funcdesc
;
4617 local_funcdesc
= sh_elf_local_funcdesc (input_bfd
);
4618 offset
= local_funcdesc
[r_symndx
].offset
;
4619 BFD_ASSERT (offset
!= MINUS_ONE
);
4620 if ((offset
& 1) == 0)
4622 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, NULL
,
4626 local_funcdesc
[r_symndx
].offset
|= 1;
4630 relocation
= htab
->sfuncdesc
->output_offset
+ (offset
& ~1);
4633 relocation
-= (htab
->root
.hgot
->root
.u
.def
.value
4634 + sgotplt
->output_offset
);
4636 relocation
-= GOT_BIAS
;
4639 if (r_type
== R_SH_GOTOFFFUNCDESC20
)
4641 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4642 input_bfd
, input_section
, contents
,
4647 goto final_link_relocate
;
4649 case R_SH_LOOP_START
:
4651 static bfd_vma start
, end
;
4653 start
= (relocation
+ rel
->r_addend
4654 - (sec
->output_section
->vma
+ sec
->output_offset
));
4655 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
4656 rel
->r_offset
, sec
, start
, end
);
4660 end
= (relocation
+ rel
->r_addend
4661 - (sec
->output_section
->vma
+ sec
->output_offset
));
4662 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
4663 rel
->r_offset
, sec
, start
, end
);
4667 case R_SH_TLS_GD_32
:
4668 case R_SH_TLS_IE_32
:
4670 check_segment
[0] = check_segment
[1] = -1;
4671 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
4672 got_type
= GOT_UNKNOWN
;
4673 if (h
== NULL
&& local_got_offsets
)
4674 got_type
= sh_elf_local_got_type (input_bfd
) [r_symndx
];
4677 got_type
= sh_elf_hash_entry (h
)->got_type
;
4678 if (! bfd_link_pic (info
)
4679 && (h
->dynindx
== -1
4681 r_type
= R_SH_TLS_LE_32
;
4684 if (r_type
== R_SH_TLS_GD_32
&& got_type
== GOT_TLS_IE
)
4685 r_type
= R_SH_TLS_IE_32
;
4687 if (r_type
== R_SH_TLS_LE_32
)
4690 unsigned short insn
;
4692 if (ELF32_R_TYPE (rel
->r_info
) == R_SH_TLS_GD_32
)
4694 /* GD->LE transition:
4695 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4696 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4697 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4699 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
4701 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
4703 offset
= rel
->r_offset
;
4707 /* xgettext:c-format */
4708 (_("%pB(%pA): offset in relocation for GD->LE translation is too small: %#" PRIx64
),
4709 input_bfd
, input_section
, (uint64_t) offset
);
4713 /* Size of GD instructions is 16 or 18. */
4715 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4716 if ((insn
& 0xff00) == 0xc700)
4718 BFD_ASSERT (offset
>= 2);
4720 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4723 if ((insn
& 0xff00) != 0xd400)
4725 /* xgettext:c-format */ /* The backslash is to prevent bogus trigraph detection. */
4726 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd4?\?)"),
4727 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4729 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4731 if ((insn
& 0xff00) != 0xc700)
4733 /* xgettext:c-format */
4734 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xc7?\?)"),
4735 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4737 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4738 if ((insn
& 0xff00) != 0xd100)
4740 /* xgettext:c-format */
4741 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd1?\?)"),
4742 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4744 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4747 /* xgettext:c-format */
4748 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x310c)"),
4749 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4751 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4754 /* xgettext:c-format */
4755 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x410b)"),
4756 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4758 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4761 /* xgettext:c-format */
4762 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x34cc)"),
4763 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4765 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 2);
4766 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 4);
4767 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
4768 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4769 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4775 /* IE->LE transition:
4792 offset
= rel
->r_offset
;
4796 /* xgettext:c-format */
4797 (_("%pB(%pA): offset in relocation for IE->LE translation is too small: %#" PRIx64
),
4798 input_bfd
, input_section
, (uint64_t) offset
);
4802 /* Size of IE instructions is 10 or 12. */
4804 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4805 if ((insn
& 0xf0ff) == 0x0012)
4807 BFD_ASSERT (offset
>= 2);
4809 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4812 if ((insn
& 0xff00) != 0xd000)
4814 /* xgettext:c-format */
4815 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0xd0??: mov.l)"),
4816 input_bfd
, input_section
, (uint64_t) offset
, (int) insn
);
4818 target
= insn
& 0x00ff;
4820 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4821 if ((insn
& 0xf0ff) != 0x0012)
4823 /* xgettext:c-format */
4824 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x0?12: stc)"),
4825 input_bfd
, input_section
, (uint64_t) (offset
+ 2), (int) insn
);
4827 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4828 if ((insn
& 0xf0ff) != 0x00ce)
4830 /* xgettext:c-format */
4831 (_("%pB(%pA+%#" PRIx64
"): unexpected instruction %#04X (expected 0x0?ce: mov.l)"),
4832 input_bfd
, input_section
, (uint64_t) (offset
+ 4), (int) insn
);
4834 insn
= 0xd000 | (insn
& 0x0f00) | target
;
4835 bfd_put_16 (output_bfd
, insn
, contents
+ offset
+ 0);
4836 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
4839 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4840 contents
+ rel
->r_offset
);
4844 if (sgot
== NULL
|| sgotplt
== NULL
)
4848 off
= h
->got
.offset
;
4851 if (local_got_offsets
== NULL
)
4854 off
= local_got_offsets
[r_symndx
];
4857 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4858 if (r_type
== R_SH_TLS_IE_32
4859 && ! htab
->root
.dynamic_sections_created
)
4862 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
4863 sgot
->contents
+ off
);
4864 bfd_put_32 (output_bfd
, sh_elf_got_offset (htab
) + off
,
4865 contents
+ rel
->r_offset
);
4873 Elf_Internal_Rela outrel
;
4877 outrel
.r_offset
= (sgot
->output_section
->vma
4878 + sgot
->output_offset
+ off
);
4880 if (h
== NULL
|| h
->dynindx
== -1)
4885 dr_type
= (r_type
== R_SH_TLS_GD_32
? R_SH_TLS_DTPMOD32
:
4887 if (dr_type
== R_SH_TLS_TPOFF32
&& indx
== 0)
4888 outrel
.r_addend
= relocation
- dtpoff_base (info
);
4890 outrel
.r_addend
= 0;
4891 outrel
.r_info
= ELF32_R_INFO (indx
, dr_type
);
4892 loc
= srelgot
->contents
;
4893 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4894 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4896 if (r_type
== R_SH_TLS_GD_32
)
4900 bfd_put_32 (output_bfd
,
4901 relocation
- dtpoff_base (info
),
4902 sgot
->contents
+ off
+ 4);
4906 outrel
.r_info
= ELF32_R_INFO (indx
,
4908 outrel
.r_offset
+= 4;
4909 outrel
.r_addend
= 0;
4910 srelgot
->reloc_count
++;
4911 loc
+= sizeof (Elf32_External_Rela
);
4912 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4919 local_got_offsets
[r_symndx
] |= 1;
4922 if (off
>= (bfd_vma
) -2)
4925 if (r_type
== (int) ELF32_R_TYPE (rel
->r_info
))
4926 relocation
= sh_elf_got_offset (htab
) + off
;
4930 unsigned short insn
;
4932 /* GD->IE transition:
4933 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4934 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4935 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4937 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4938 nop; nop; bra 3f; nop; .align 2;
4939 1: .long x@TPOFF; 2:...; 3:. */
4941 offset
= rel
->r_offset
;
4945 /* xgettext:c-format */
4946 (_("%pB(%pA): offset in relocation for GD->IE translation is too small: %#" PRIx64
),
4947 input_bfd
, input_section
, (uint64_t) offset
);
4951 /* Size of GD instructions is 16 or 18. */
4953 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4954 if ((insn
& 0xff00) == 0xc700)
4956 BFD_ASSERT (offset
>= 2);
4958 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
4961 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
4963 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4964 bfd_put_16 (output_bfd
, insn
& 0xf0ff, contents
+ offset
);
4966 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
4967 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
4968 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
4969 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
4970 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
4971 BFD_ASSERT (insn
== 0x310c);
4972 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
4973 BFD_ASSERT (insn
== 0x410b);
4974 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
4975 BFD_ASSERT (insn
== 0x34cc);
4977 bfd_put_16 (output_bfd
, 0x0412, contents
+ offset
+ 2);
4978 bfd_put_16 (output_bfd
, 0x00ce, contents
+ offset
+ 4);
4979 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 6);
4980 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
4981 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
4983 bfd_put_32 (output_bfd
, sh_elf_got_offset (htab
) + off
,
4984 contents
+ rel
->r_offset
);
4989 addend
= rel
->r_addend
;
4991 goto final_link_relocate
;
4993 case R_SH_TLS_LD_32
:
4995 check_segment
[0] = check_segment
[1] = -1;
4996 if (! bfd_link_pic (info
))
4999 unsigned short insn
;
5001 /* LD->LE transition:
5002 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5003 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5004 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5006 stc gbr,r0; nop; nop; nop;
5007 nop; nop; bra 3f; ...; 3:. */
5009 offset
= rel
->r_offset
;
5013 /* xgettext:c-format */
5014 (_("%pB(%pA): offset in relocation for LD->LE translation is too small: %#" PRIx64
),
5015 input_bfd
, input_section
, (uint64_t) offset
);
5019 /* Size of LD instructions is 16 or 18. */
5021 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5022 if ((insn
& 0xff00) == 0xc700)
5024 BFD_ASSERT (offset
>= 2);
5026 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5029 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
5030 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
5031 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
5032 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
5033 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
5034 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
5035 BFD_ASSERT (insn
== 0x310c);
5036 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
5037 BFD_ASSERT (insn
== 0x410b);
5038 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
5039 BFD_ASSERT (insn
== 0x34cc);
5041 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 0);
5042 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 2);
5043 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
5044 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
5045 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
5046 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
5051 if (sgot
== NULL
|| sgotplt
== NULL
)
5054 off
= htab
->tls_ldm_got
.offset
;
5059 Elf_Internal_Rela outrel
;
5062 outrel
.r_offset
= (sgot
->output_section
->vma
5063 + sgot
->output_offset
+ off
);
5064 outrel
.r_addend
= 0;
5065 outrel
.r_info
= ELF32_R_INFO (0, R_SH_TLS_DTPMOD32
);
5066 loc
= srelgot
->contents
;
5067 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5068 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5069 htab
->tls_ldm_got
.offset
|= 1;
5072 relocation
= sh_elf_got_offset (htab
) + off
;
5073 addend
= rel
->r_addend
;
5075 goto final_link_relocate
;
5077 case R_SH_TLS_LDO_32
:
5078 check_segment
[0] = check_segment
[1] = -1;
5079 if (! bfd_link_pic (info
))
5080 relocation
= tpoff (info
, relocation
);
5082 relocation
-= dtpoff_base (info
);
5084 addend
= rel
->r_addend
;
5085 goto final_link_relocate
;
5087 case R_SH_TLS_LE_32
:
5090 Elf_Internal_Rela outrel
;
5093 check_segment
[0] = check_segment
[1] = -1;
5095 if (!bfd_link_dll (info
))
5097 relocation
= tpoff (info
, relocation
);
5098 addend
= rel
->r_addend
;
5099 goto final_link_relocate
;
5104 sreloc
= _bfd_elf_get_dynamic_reloc_section
5105 (input_bfd
, input_section
, /*rela?*/ TRUE
);
5110 if (h
== NULL
|| h
->dynindx
== -1)
5115 outrel
.r_offset
= (input_section
->output_section
->vma
5116 + input_section
->output_offset
5118 outrel
.r_info
= ELF32_R_INFO (indx
, R_SH_TLS_TPOFF32
);
5120 outrel
.r_addend
= relocation
- dtpoff_base (info
);
5122 outrel
.r_addend
= 0;
5124 loc
= sreloc
->contents
;
5125 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5126 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5132 if (fdpic_p
&& check_segment
[0] != (unsigned) -1
5133 && check_segment
[0] != check_segment
[1])
5135 /* We don't want duplicate errors for undefined symbols. */
5136 if (!h
|| h
->root
.type
!= bfd_link_hash_undefined
)
5138 if (bfd_link_pic (info
))
5140 info
->callbacks
->einfo
5141 /* xgettext:c-format */
5142 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5143 input_bfd
, input_section
, rel
->r_offset
, symname
);
5147 info
->callbacks
->einfo
5148 /* xgettext:c-format */
5149 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5150 input_bfd
, input_section
, rel
->r_offset
, symname
);
5153 elf_elfheader (output_bfd
)->e_flags
|= EF_SH_PIC
;
5156 if (r
!= bfd_reloc_ok
)
5161 case bfd_reloc_outofrange
:
5163 case bfd_reloc_overflow
:
5171 name
= (bfd_elf_string_from_elf_section
5172 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
5176 name
= bfd_section_name (sec
);
5178 (*info
->callbacks
->reloc_overflow
)
5179 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
5180 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
5190 /* This is a version of bfd_generic_get_relocated_section_contents
5191 which uses sh_elf_relocate_section. */
5194 sh_elf_get_relocated_section_contents (bfd
*output_bfd
,
5195 struct bfd_link_info
*link_info
,
5196 struct bfd_link_order
*link_order
,
5198 bfd_boolean relocatable
,
5201 Elf_Internal_Shdr
*symtab_hdr
;
5202 asection
*input_section
= link_order
->u
.indirect
.section
;
5203 bfd
*input_bfd
= input_section
->owner
;
5204 asection
**sections
= NULL
;
5205 Elf_Internal_Rela
*internal_relocs
= NULL
;
5206 Elf_Internal_Sym
*isymbuf
= NULL
;
5208 /* We only need to handle the case of relaxing, or of having a
5209 particular set of section contents, specially. */
5211 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
5212 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
5217 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
5219 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
5220 (size_t) input_section
->size
);
5222 if ((input_section
->flags
& SEC_RELOC
) != 0
5223 && input_section
->reloc_count
> 0)
5226 Elf_Internal_Sym
*isym
, *isymend
;
5229 internal_relocs
= (_bfd_elf_link_read_relocs
5230 (input_bfd
, input_section
, NULL
,
5231 (Elf_Internal_Rela
*) NULL
, FALSE
));
5232 if (internal_relocs
== NULL
)
5235 if (symtab_hdr
->sh_info
!= 0)
5237 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
5238 if (isymbuf
== NULL
)
5239 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
5240 symtab_hdr
->sh_info
, 0,
5242 if (isymbuf
== NULL
)
5246 amt
= symtab_hdr
->sh_info
;
5247 amt
*= sizeof (asection
*);
5248 sections
= (asection
**) bfd_malloc (amt
);
5249 if (sections
== NULL
&& amt
!= 0)
5252 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
5253 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
5257 if (isym
->st_shndx
== SHN_UNDEF
)
5258 isec
= bfd_und_section_ptr
;
5259 else if (isym
->st_shndx
== SHN_ABS
)
5260 isec
= bfd_abs_section_ptr
;
5261 else if (isym
->st_shndx
== SHN_COMMON
)
5262 isec
= bfd_com_section_ptr
;
5264 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
5269 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
5270 input_section
, data
, internal_relocs
,
5274 if (sections
!= NULL
)
5277 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
5279 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
5280 free (internal_relocs
);
5286 if (sections
!= NULL
)
5289 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
5291 if (internal_relocs
!= NULL
5292 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
5293 free (internal_relocs
);
5297 /* Return the base VMA address which should be subtracted from real addresses
5298 when resolving @dtpoff relocation.
5299 This is PT_TLS segment p_vaddr. */
5302 dtpoff_base (struct bfd_link_info
*info
)
5304 /* If tls_sec is NULL, we should have signalled an error already. */
5305 if (elf_hash_table (info
)->tls_sec
== NULL
)
5307 return elf_hash_table (info
)->tls_sec
->vma
;
5310 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5313 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
5315 /* If tls_sec is NULL, we should have signalled an error already. */
5316 if (elf_hash_table (info
)->tls_sec
== NULL
)
5318 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5319 structure which has 2 pointer fields. */
5320 return (address
- elf_hash_table (info
)->tls_sec
->vma
5321 + align_power ((bfd_vma
) 8,
5322 elf_hash_table (info
)->tls_sec
->alignment_power
));
5326 sh_elf_gc_mark_hook (asection
*sec
,
5327 struct bfd_link_info
*info
,
5328 Elf_Internal_Rela
*rel
,
5329 struct elf_link_hash_entry
*h
,
5330 Elf_Internal_Sym
*sym
)
5333 switch (ELF32_R_TYPE (rel
->r_info
))
5335 case R_SH_GNU_VTINHERIT
:
5336 case R_SH_GNU_VTENTRY
:
5340 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
5343 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5346 sh_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
5347 struct elf_link_hash_entry
*dir
,
5348 struct elf_link_hash_entry
*ind
)
5350 struct elf_sh_link_hash_entry
*edir
, *eind
;
5352 edir
= (struct elf_sh_link_hash_entry
*) dir
;
5353 eind
= (struct elf_sh_link_hash_entry
*) ind
;
5355 if (eind
->dyn_relocs
!= NULL
)
5357 if (edir
->dyn_relocs
!= NULL
)
5359 struct elf_dyn_relocs
**pp
;
5360 struct elf_dyn_relocs
*p
;
5362 /* Add reloc counts against the indirect sym to the direct sym
5363 list. Merge any entries against the same section. */
5364 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
5366 struct elf_dyn_relocs
*q
;
5368 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
5369 if (q
->sec
== p
->sec
)
5371 q
->pc_count
+= p
->pc_count
;
5372 q
->count
+= p
->count
;
5379 *pp
= edir
->dyn_relocs
;
5382 edir
->dyn_relocs
= eind
->dyn_relocs
;
5383 eind
->dyn_relocs
= NULL
;
5385 edir
->gotplt_refcount
= eind
->gotplt_refcount
;
5386 eind
->gotplt_refcount
= 0;
5387 edir
->funcdesc
.refcount
+= eind
->funcdesc
.refcount
;
5388 eind
->funcdesc
.refcount
= 0;
5389 edir
->abs_funcdesc_refcount
+= eind
->abs_funcdesc_refcount
;
5390 eind
->abs_funcdesc_refcount
= 0;
5392 if (ind
->root
.type
== bfd_link_hash_indirect
5393 && dir
->got
.refcount
<= 0)
5395 edir
->got_type
= eind
->got_type
;
5396 eind
->got_type
= GOT_UNKNOWN
;
5399 if (ind
->root
.type
!= bfd_link_hash_indirect
5400 && dir
->dynamic_adjusted
)
5402 /* If called to transfer flags for a weakdef during processing
5403 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5404 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5405 if (dir
->versioned
!= versioned_hidden
)
5406 dir
->ref_dynamic
|= ind
->ref_dynamic
;
5407 dir
->ref_regular
|= ind
->ref_regular
;
5408 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
5409 dir
->needs_plt
|= ind
->needs_plt
;
5412 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
5416 sh_elf_optimized_tls_reloc (struct bfd_link_info
*info
, int r_type
,
5419 if (bfd_link_pic (info
))
5424 case R_SH_TLS_GD_32
:
5425 case R_SH_TLS_IE_32
:
5427 return R_SH_TLS_LE_32
;
5428 return R_SH_TLS_IE_32
;
5429 case R_SH_TLS_LD_32
:
5430 return R_SH_TLS_LE_32
;
5436 /* Look through the relocs for a section during the first phase.
5437 Since we don't do .gots or .plts, we just need to consider the
5438 virtual table relocs for gc. */
5441 sh_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
5442 const Elf_Internal_Rela
*relocs
)
5444 Elf_Internal_Shdr
*symtab_hdr
;
5445 struct elf_link_hash_entry
**sym_hashes
;
5446 struct elf_sh_link_hash_table
*htab
;
5447 const Elf_Internal_Rela
*rel
;
5448 const Elf_Internal_Rela
*rel_end
;
5450 unsigned int r_type
;
5451 enum got_type got_type
, old_got_type
;
5455 if (bfd_link_relocatable (info
))
5458 /* Don't do anything special with non-loaded, non-alloced sections.
5459 In particular, any relocs in such sections should not affect GOT
5460 and PLT reference counting (ie. we don't allow them to create GOT
5461 or PLT entries), there's no possibility or desire to optimize TLS
5462 relocs, and there's not much point in propagating relocs to shared
5463 libs that the dynamic linker won't relocate. */
5464 if ((sec
->flags
& SEC_ALLOC
) == 0)
5467 BFD_ASSERT (is_sh_elf (abfd
));
5469 symtab_hdr
= &elf_symtab_hdr (abfd
);
5470 sym_hashes
= elf_sym_hashes (abfd
);
5472 htab
= sh_elf_hash_table (info
);
5476 rel_end
= relocs
+ sec
->reloc_count
;
5477 for (rel
= relocs
; rel
< rel_end
; rel
++)
5479 struct elf_link_hash_entry
*h
;
5480 unsigned long r_symndx
;
5482 r_symndx
= ELF32_R_SYM (rel
->r_info
);
5483 r_type
= ELF32_R_TYPE (rel
->r_info
);
5485 if (r_symndx
< symtab_hdr
->sh_info
)
5489 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
5490 while (h
->root
.type
== bfd_link_hash_indirect
5491 || h
->root
.type
== bfd_link_hash_warning
)
5492 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5495 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
5496 if (! bfd_link_pic (info
)
5497 && r_type
== R_SH_TLS_IE_32
5499 && h
->root
.type
!= bfd_link_hash_undefined
5500 && h
->root
.type
!= bfd_link_hash_undefweak
5501 && (h
->dynindx
== -1
5503 r_type
= R_SH_TLS_LE_32
;
5508 case R_SH_GOTOFFFUNCDESC
:
5509 case R_SH_GOTOFFFUNCDESC20
:
5511 case R_SH_GOTFUNCDESC
:
5512 case R_SH_GOTFUNCDESC20
:
5515 if (h
->dynindx
== -1)
5516 switch (ELF_ST_VISIBILITY (h
->other
))
5522 bfd_elf_link_record_dynamic_symbol (info
, h
);
5529 /* Some relocs require a global offset table. */
5530 if (htab
->root
.sgot
== NULL
)
5535 /* This may require an rofixup. */
5545 case R_SH_GOTFUNCDESC
:
5546 case R_SH_GOTFUNCDESC20
:
5547 case R_SH_GOTOFFFUNCDESC
:
5548 case R_SH_GOTOFFFUNCDESC20
:
5550 case R_SH_TLS_GD_32
:
5551 case R_SH_TLS_LD_32
:
5552 case R_SH_TLS_IE_32
:
5553 if (htab
->root
.dynobj
== NULL
)
5554 htab
->root
.dynobj
= abfd
;
5555 if (!create_got_section (htab
->root
.dynobj
, info
))
5566 /* This relocation describes the C++ object vtable hierarchy.
5567 Reconstruct it for later use during GC. */
5568 case R_SH_GNU_VTINHERIT
:
5569 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
5573 /* This relocation describes which C++ vtable entries are actually
5574 used. Record for later use during GC. */
5575 case R_SH_GNU_VTENTRY
:
5576 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
5580 case R_SH_TLS_IE_32
:
5581 if (bfd_link_pic (info
))
5582 info
->flags
|= DF_STATIC_TLS
;
5586 case R_SH_TLS_GD_32
:
5589 case R_SH_GOTFUNCDESC
:
5590 case R_SH_GOTFUNCDESC20
:
5594 got_type
= GOT_NORMAL
;
5596 case R_SH_TLS_GD_32
:
5597 got_type
= GOT_TLS_GD
;
5599 case R_SH_TLS_IE_32
:
5600 got_type
= GOT_TLS_IE
;
5602 case R_SH_GOTFUNCDESC
:
5603 case R_SH_GOTFUNCDESC20
:
5604 got_type
= GOT_FUNCDESC
;
5610 h
->got
.refcount
+= 1;
5611 old_got_type
= sh_elf_hash_entry (h
)->got_type
;
5615 bfd_signed_vma
*local_got_refcounts
;
5617 /* This is a global offset table entry for a local
5619 local_got_refcounts
= elf_local_got_refcounts (abfd
);
5620 if (local_got_refcounts
== NULL
)
5624 size
= symtab_hdr
->sh_info
;
5625 size
*= sizeof (bfd_signed_vma
);
5626 size
+= symtab_hdr
->sh_info
;
5627 local_got_refcounts
= ((bfd_signed_vma
*)
5628 bfd_zalloc (abfd
, size
));
5629 if (local_got_refcounts
== NULL
)
5631 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
5632 sh_elf_local_got_type (abfd
)
5633 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
5635 local_got_refcounts
[r_symndx
] += 1;
5636 old_got_type
= sh_elf_local_got_type (abfd
) [r_symndx
];
5639 /* If a TLS symbol is accessed using IE at least once,
5640 there is no point to use dynamic model for it. */
5641 if (old_got_type
!= got_type
&& old_got_type
!= GOT_UNKNOWN
5642 && (old_got_type
!= GOT_TLS_GD
|| got_type
!= GOT_TLS_IE
))
5644 if (old_got_type
== GOT_TLS_IE
&& got_type
== GOT_TLS_GD
)
5645 got_type
= GOT_TLS_IE
;
5648 if ((old_got_type
== GOT_FUNCDESC
|| got_type
== GOT_FUNCDESC
)
5649 && (old_got_type
== GOT_NORMAL
|| got_type
== GOT_NORMAL
))
5651 /* xgettext:c-format */
5652 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5653 abfd
, h
->root
.root
.string
);
5654 else if (old_got_type
== GOT_FUNCDESC
5655 || got_type
== GOT_FUNCDESC
)
5657 /* xgettext:c-format */
5658 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5659 abfd
, h
->root
.root
.string
);
5662 /* xgettext:c-format */
5663 (_("%pB: `%s' accessed both as normal and thread local symbol"),
5664 abfd
, h
->root
.root
.string
);
5669 if (old_got_type
!= got_type
)
5672 sh_elf_hash_entry (h
)->got_type
= got_type
;
5674 sh_elf_local_got_type (abfd
) [r_symndx
] = got_type
;
5679 case R_SH_TLS_LD_32
:
5680 sh_elf_hash_table(info
)->tls_ldm_got
.refcount
+= 1;
5684 case R_SH_GOTOFFFUNCDESC
:
5685 case R_SH_GOTOFFFUNCDESC20
:
5689 (_("%pB: Function descriptor relocation with non-zero addend"),
5696 union gotref
*local_funcdesc
;
5698 /* We need a function descriptor for a local symbol. */
5699 local_funcdesc
= sh_elf_local_funcdesc (abfd
);
5700 if (local_funcdesc
== NULL
)
5704 size
= symtab_hdr
->sh_info
* sizeof (union gotref
);
5705 local_funcdesc
= (union gotref
*) bfd_zalloc (abfd
, size
);
5706 if (local_funcdesc
== NULL
)
5708 sh_elf_local_funcdesc (abfd
) = local_funcdesc
;
5710 local_funcdesc
[r_symndx
].refcount
+= 1;
5712 if (r_type
== R_SH_FUNCDESC
)
5714 if (!bfd_link_pic (info
))
5715 htab
->srofixup
->size
+= 4;
5717 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
5722 sh_elf_hash_entry (h
)->funcdesc
.refcount
++;
5723 if (r_type
== R_SH_FUNCDESC
)
5724 sh_elf_hash_entry (h
)->abs_funcdesc_refcount
++;
5726 /* If there is a function descriptor reference, then
5727 there should not be any non-FDPIC references. */
5728 old_got_type
= sh_elf_hash_entry (h
)->got_type
;
5729 if (old_got_type
!= GOT_FUNCDESC
&& old_got_type
!= GOT_UNKNOWN
)
5731 if (old_got_type
== GOT_NORMAL
)
5733 /* xgettext:c-format */
5734 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5735 abfd
, h
->root
.root
.string
);
5738 /* xgettext:c-format */
5739 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5740 abfd
, h
->root
.root
.string
);
5746 /* If this is a local symbol, we resolve it directly without
5747 creating a procedure linkage table entry. */
5751 || ! bfd_link_pic (info
)
5753 || h
->dynindx
== -1)
5757 h
->plt
.refcount
+= 1;
5758 ((struct elf_sh_link_hash_entry
*) h
)->gotplt_refcount
+= 1;
5763 /* This symbol requires a procedure linkage table entry. We
5764 actually build the entry in adjust_dynamic_symbol,
5765 because this might be a case of linking PIC code which is
5766 never referenced by a dynamic object, in which case we
5767 don't need to generate a procedure linkage table entry
5770 /* If this is a local symbol, we resolve it directly without
5771 creating a procedure linkage table entry. */
5775 if (h
->forced_local
)
5779 h
->plt
.refcount
+= 1;
5784 if (h
!= NULL
&& ! bfd_link_pic (info
))
5787 h
->plt
.refcount
+= 1;
5790 /* If we are creating a shared library, and this is a reloc
5791 against a global symbol, or a non PC relative reloc
5792 against a local symbol, then we need to copy the reloc
5793 into the shared library. However, if we are linking with
5794 -Bsymbolic, we do not need to copy a reloc against a
5795 global symbol which is defined in an object we are
5796 including in the link (i.e., DEF_REGULAR is set). At
5797 this point we have not seen all the input files, so it is
5798 possible that DEF_REGULAR is not set now but will be set
5799 later (it is never cleared). We account for that
5800 possibility below by storing information in the
5801 dyn_relocs field of the hash table entry. A similar
5802 situation occurs when creating shared libraries and symbol
5803 visibility changes render the symbol local.
5805 If on the other hand, we are creating an executable, we
5806 may need to keep relocations for symbols satisfied by a
5807 dynamic library if we manage to avoid copy relocs for the
5809 if ((bfd_link_pic (info
)
5810 && (sec
->flags
& SEC_ALLOC
) != 0
5811 && (r_type
!= R_SH_REL32
5813 && (! info
->symbolic
5814 || h
->root
.type
== bfd_link_hash_defweak
5815 || !h
->def_regular
))))
5816 || (! bfd_link_pic (info
)
5817 && (sec
->flags
& SEC_ALLOC
) != 0
5819 && (h
->root
.type
== bfd_link_hash_defweak
5820 || !h
->def_regular
)))
5822 struct elf_dyn_relocs
*p
;
5823 struct elf_dyn_relocs
**head
;
5825 if (htab
->root
.dynobj
== NULL
)
5826 htab
->root
.dynobj
= abfd
;
5828 /* When creating a shared object, we must copy these
5829 reloc types into the output file. We create a reloc
5830 section in dynobj and make room for this reloc. */
5833 sreloc
= _bfd_elf_make_dynamic_reloc_section
5834 (sec
, htab
->root
.dynobj
, 2, abfd
, /*rela?*/ TRUE
);
5840 /* If this is a global symbol, we count the number of
5841 relocations we need for this symbol. */
5843 head
= &((struct elf_sh_link_hash_entry
*) h
)->dyn_relocs
;
5846 /* Track dynamic relocs needed for local syms too. */
5849 Elf_Internal_Sym
*isym
;
5851 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
5856 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
5860 vpp
= &elf_section_data (s
)->local_dynrel
;
5861 head
= (struct elf_dyn_relocs
**) vpp
;
5865 if (p
== NULL
|| p
->sec
!= sec
)
5867 size_t amt
= sizeof (*p
);
5868 p
= bfd_alloc (htab
->root
.dynobj
, amt
);
5879 if (r_type
== R_SH_REL32
)
5883 /* Allocate the fixup regardless of whether we need a relocation.
5884 If we end up generating the relocation, we'll unallocate the
5886 if (htab
->fdpic_p
&& !bfd_link_pic (info
)
5887 && r_type
== R_SH_DIR32
5888 && (sec
->flags
& SEC_ALLOC
) != 0)
5889 htab
->srofixup
->size
+= 4;
5892 case R_SH_TLS_LE_32
:
5893 if (bfd_link_dll (info
))
5896 (_("%pB: TLS local exec code cannot be linked into shared objects"),
5903 case R_SH_TLS_LDO_32
:
5904 /* Nothing to do. */
5915 #ifndef sh_elf_set_mach_from_flags
5916 static unsigned int sh_ef_bfd_table
[] = { EF_SH_BFD_TABLE
};
5919 sh_elf_set_mach_from_flags (bfd
*abfd
)
5921 flagword flags
= elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
;
5923 if (flags
>= ARRAY_SIZE (sh_ef_bfd_table
))
5926 if (sh_ef_bfd_table
[flags
] == 0)
5929 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, sh_ef_bfd_table
[flags
]);
5935 /* Reverse table lookup for sh_ef_bfd_table[].
5936 Given a bfd MACH value from archures.c
5937 return the equivalent ELF flags from the table.
5938 Return -1 if no match is found. */
5941 sh_elf_get_flags_from_mach (unsigned long mach
)
5943 int i
= ARRAY_SIZE (sh_ef_bfd_table
) - 1;
5946 if (sh_ef_bfd_table
[i
] == mach
)
5949 /* shouldn't get here */
5954 #endif /* not sh_elf_set_mach_from_flags */
5956 #ifndef sh_elf_copy_private_data
5957 /* Copy backend specific data from one object module to another */
5960 sh_elf_copy_private_data (bfd
* ibfd
, bfd
* obfd
)
5962 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
5965 if (! _bfd_elf_copy_private_bfd_data (ibfd
, obfd
))
5968 return sh_elf_set_mach_from_flags (obfd
);
5970 #endif /* not sh_elf_copy_private_data */
5972 #ifndef sh_elf_merge_private_data
5974 /* This function returns the ELF architecture number that
5975 corresponds to the given arch_sh* flags. */
5978 sh_find_elf_flags (unsigned int arch_set
)
5980 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
5981 unsigned long bfd_mach
= sh_get_bfd_mach_from_arch_set (arch_set
);
5983 return sh_elf_get_flags_from_mach (bfd_mach
);
5986 /* Merge the architecture type of two BFD files, such that the
5987 resultant architecture supports all the features required
5988 by the two input BFDs.
5989 If the input BFDs are multually incompatible - i.e. one uses
5990 DSP while the other uses FPU - or there is no known architecture
5991 that fits the requirements then an error is emitted. */
5994 sh_merge_bfd_arch (bfd
*ibfd
, struct bfd_link_info
*info
)
5996 bfd
*obfd
= info
->output_bfd
;
5997 unsigned int old_arch
, new_arch
, merged_arch
;
5999 if (! _bfd_generic_verify_endian_match (ibfd
, info
))
6002 old_arch
= sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd
));
6003 new_arch
= sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd
));
6005 merged_arch
= SH_MERGE_ARCH_SET (old_arch
, new_arch
);
6007 if (!SH_VALID_CO_ARCH_SET (merged_arch
))
6010 /* xgettext:c-format */
6011 (_("%pB: uses %s instructions while previous modules "
6012 "use %s instructions"),
6014 SH_ARCH_SET_HAS_DSP (new_arch
) ? "dsp" : "floating point",
6015 SH_ARCH_SET_HAS_DSP (new_arch
) ? "floating point" : "dsp");
6016 bfd_set_error (bfd_error_bad_value
);
6019 else if (!SH_VALID_ARCH_SET (merged_arch
))
6022 /* xgettext:c-format */
6023 (_("internal error: merge of architecture '%s' with "
6024 "architecture '%s' produced unknown architecture"),
6025 bfd_printable_name (obfd
),
6026 bfd_printable_name (ibfd
));
6027 bfd_set_error (bfd_error_bad_value
);
6031 bfd_default_set_arch_mach (obfd
, bfd_arch_sh
,
6032 sh_get_bfd_mach_from_arch_set (merged_arch
));
6037 /* This routine initialises the elf flags when required and
6038 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
6041 sh_elf_merge_private_data (bfd
*ibfd
, struct bfd_link_info
*info
)
6043 bfd
*obfd
= info
->output_bfd
;
6045 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
6048 if (! elf_flags_init (obfd
))
6050 /* This happens when ld starts out with a 'blank' output file. */
6051 elf_flags_init (obfd
) = TRUE
;
6052 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
6053 sh_elf_set_mach_from_flags (obfd
);
6054 if (elf_elfheader (obfd
)->e_flags
& EF_SH_FDPIC
)
6055 elf_elfheader (obfd
)->e_flags
&= ~EF_SH_PIC
;
6058 if (! sh_merge_bfd_arch (ibfd
, info
))
6060 _bfd_error_handler (_("%pB: uses instructions which are incompatible "
6061 "with instructions used in previous modules"),
6063 bfd_set_error (bfd_error_bad_value
);
6067 elf_elfheader (obfd
)->e_flags
&= ~EF_SH_MACH_MASK
;
6068 elf_elfheader (obfd
)->e_flags
|=
6069 sh_elf_get_flags_from_mach (bfd_get_mach (obfd
));
6071 if (fdpic_object_p (ibfd
) != fdpic_object_p (obfd
))
6073 _bfd_error_handler (_("%pB: attempt to mix FDPIC and non-FDPIC objects"),
6075 bfd_set_error (bfd_error_bad_value
);
6081 #endif /* not sh_elf_merge_private_data */
6083 /* Override the generic function because we need to store sh_elf_obj_tdata
6084 as the specific tdata. We set also the machine architecture from flags
6088 sh_elf_object_p (bfd
*abfd
)
6090 if (! sh_elf_set_mach_from_flags (abfd
))
6093 return (((elf_elfheader (abfd
)->e_flags
& EF_SH_FDPIC
) != 0)
6094 == fdpic_object_p (abfd
));
6097 /* Finish up dynamic symbol handling. We set the contents of various
6098 dynamic sections here. */
6101 sh_elf_finish_dynamic_symbol (bfd
*output_bfd
, struct bfd_link_info
*info
,
6102 struct elf_link_hash_entry
*h
,
6103 Elf_Internal_Sym
*sym
)
6105 struct elf_sh_link_hash_table
*htab
;
6107 htab
= sh_elf_hash_table (info
);
6111 if (h
->plt
.offset
!= (bfd_vma
) -1)
6119 Elf_Internal_Rela rel
;
6121 const struct elf_sh_plt_info
*plt_info
;
6123 /* This symbol has an entry in the procedure linkage table. Set
6126 BFD_ASSERT (h
->dynindx
!= -1);
6128 splt
= htab
->root
.splt
;
6129 sgotplt
= htab
->root
.sgotplt
;
6130 srelplt
= htab
->root
.srelplt
;
6131 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srelplt
!= NULL
);
6133 /* Get the index in the procedure linkage table which
6134 corresponds to this symbol. This is the index of this symbol
6135 in all the symbols for which we are making plt entries. The
6136 first entry in the procedure linkage table is reserved. */
6137 plt_index
= get_plt_index (htab
->plt_info
, h
->plt
.offset
);
6139 plt_info
= htab
->plt_info
;
6140 if (plt_info
->short_plt
!= NULL
&& plt_index
<= MAX_SHORT_PLT
)
6141 plt_info
= plt_info
->short_plt
;
6143 /* Get the offset into the .got table of the entry that
6144 corresponds to this function. */
6146 /* The offset must be relative to the GOT symbol, twelve bytes
6147 before the end of .got.plt. Each descriptor is eight
6149 got_offset
= plt_index
* 8 + 12 - sgotplt
->size
;
6151 /* Each .got entry is 4 bytes. The first three are
6153 got_offset
= (plt_index
+ 3) * 4;
6156 if (bfd_link_pic (info
))
6157 got_offset
-= GOT_BIAS
;
6160 /* Fill in the entry in the procedure linkage table. */
6161 memcpy (splt
->contents
+ h
->plt
.offset
,
6162 plt_info
->symbol_entry
,
6163 plt_info
->symbol_entry_size
);
6165 if (bfd_link_pic (info
) || htab
->fdpic_p
)
6167 if (plt_info
->symbol_fields
.got20
)
6169 bfd_reloc_status_type r
;
6170 r
= install_movi20_field (output_bfd
, got_offset
,
6171 splt
->owner
, splt
, splt
->contents
,
6173 + plt_info
->symbol_fields
.got_entry
);
6174 BFD_ASSERT (r
== bfd_reloc_ok
);
6177 install_plt_field (output_bfd
, FALSE
, got_offset
,
6180 + plt_info
->symbol_fields
.got_entry
));
6184 BFD_ASSERT (!plt_info
->symbol_fields
.got20
);
6186 install_plt_field (output_bfd
, FALSE
,
6187 (sgotplt
->output_section
->vma
6188 + sgotplt
->output_offset
6192 + plt_info
->symbol_fields
.got_entry
));
6193 if (htab
->vxworks_p
)
6195 unsigned int reachable_plts
, plts_per_4k
;
6198 /* Divide the PLT into groups. The first group contains
6199 REACHABLE_PLTS entries and the other groups contain
6200 PLTS_PER_4K entries. Entries in the first group can
6201 branch directly to .plt; those in later groups branch
6202 to the last element of the previous group. */
6203 /* ??? It would be better to create multiple copies of
6204 the common resolver stub. */
6205 reachable_plts
= ((4096
6206 - plt_info
->plt0_entry_size
6207 - (plt_info
->symbol_fields
.plt
+ 4))
6208 / plt_info
->symbol_entry_size
) + 1;
6209 plts_per_4k
= (4096 / plt_info
->symbol_entry_size
);
6210 if (plt_index
< reachable_plts
)
6211 distance
= -(h
->plt
.offset
6212 + plt_info
->symbol_fields
.plt
);
6214 distance
= -(((plt_index
- reachable_plts
) % plts_per_4k
+ 1)
6215 * plt_info
->symbol_entry_size
);
6217 /* Install the 'bra' with this offset. */
6218 bfd_put_16 (output_bfd
,
6219 0xa000 | (0x0fff & ((distance
- 4) / 2)),
6222 + plt_info
->symbol_fields
.plt
));
6225 install_plt_field (output_bfd
, TRUE
,
6226 splt
->output_section
->vma
+ splt
->output_offset
,
6229 + plt_info
->symbol_fields
.plt
));
6232 /* Make got_offset relative to the start of .got.plt. */
6234 if (bfd_link_pic (info
))
6235 got_offset
+= GOT_BIAS
;
6238 got_offset
= plt_index
* 8;
6240 if (plt_info
->symbol_fields
.reloc_offset
!= MINUS_ONE
)
6241 install_plt_field (output_bfd
, FALSE
,
6242 plt_index
* sizeof (Elf32_External_Rela
),
6245 + plt_info
->symbol_fields
.reloc_offset
));
6247 /* Fill in the entry in the global offset table. */
6248 bfd_put_32 (output_bfd
,
6249 (splt
->output_section
->vma
6250 + splt
->output_offset
6252 + plt_info
->symbol_resolve_offset
),
6253 sgotplt
->contents
+ got_offset
);
6255 bfd_put_32 (output_bfd
,
6256 sh_elf_osec_to_segment (output_bfd
, splt
->output_section
),
6257 sgotplt
->contents
+ got_offset
+ 4);
6259 /* Fill in the entry in the .rela.plt section. */
6260 rel
.r_offset
= (sgotplt
->output_section
->vma
6261 + sgotplt
->output_offset
6264 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_FUNCDESC_VALUE
);
6266 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
6269 rel
.r_addend
= GOT_BIAS
;
6271 loc
= srelplt
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
6272 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6274 if (htab
->vxworks_p
&& !bfd_link_pic (info
))
6276 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6277 Begin by pointing LOC to the first such relocation. */
6278 loc
= (htab
->srelplt2
->contents
6279 + (plt_index
* 2 + 1) * sizeof (Elf32_External_Rela
));
6281 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6282 for the PLT entry's pointer to the .got.plt entry. */
6283 rel
.r_offset
= (splt
->output_section
->vma
6284 + splt
->output_offset
6286 + plt_info
->symbol_fields
.got_entry
);
6287 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
6288 rel
.r_addend
= got_offset
;
6289 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6290 loc
+= sizeof (Elf32_External_Rela
);
6292 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6293 the .got.plt entry, which initially points to .plt. */
6294 rel
.r_offset
= (sgotplt
->output_section
->vma
6295 + sgotplt
->output_offset
6297 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
, R_SH_DIR32
);
6299 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6302 if (!h
->def_regular
)
6304 /* Mark the symbol as undefined, rather than as defined in
6305 the .plt section. Leave the value alone. */
6306 sym
->st_shndx
= SHN_UNDEF
;
6310 if (h
->got
.offset
!= (bfd_vma
) -1
6311 && sh_elf_hash_entry (h
)->got_type
!= GOT_TLS_GD
6312 && sh_elf_hash_entry (h
)->got_type
!= GOT_TLS_IE
6313 && sh_elf_hash_entry (h
)->got_type
!= GOT_FUNCDESC
)
6317 Elf_Internal_Rela rel
;
6320 /* This symbol has an entry in the global offset table. Set it
6323 sgot
= htab
->root
.sgot
;
6324 srelgot
= htab
->root
.srelgot
;
6325 BFD_ASSERT (sgot
!= NULL
&& srelgot
!= NULL
);
6327 rel
.r_offset
= (sgot
->output_section
->vma
6328 + sgot
->output_offset
6329 + (h
->got
.offset
&~ (bfd_vma
) 1));
6331 /* If this is a static link, or it is a -Bsymbolic link and the
6332 symbol is defined locally or was forced to be local because
6333 of a version file, we just want to emit a RELATIVE reloc.
6334 The entry in the global offset table will already have been
6335 initialized in the relocate_section function. */
6336 if (bfd_link_pic (info
)
6337 && SYMBOL_REFERENCES_LOCAL (info
, h
))
6341 asection
*sec
= h
->root
.u
.def
.section
;
6343 = elf_section_data (sec
->output_section
)->dynindx
;
6345 rel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
6346 rel
.r_addend
= (h
->root
.u
.def
.value
6347 + h
->root
.u
.def
.section
->output_offset
);
6351 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
6352 rel
.r_addend
= (h
->root
.u
.def
.value
6353 + h
->root
.u
.def
.section
->output_section
->vma
6354 + h
->root
.u
.def
.section
->output_offset
);
6359 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
6360 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
6364 loc
= srelgot
->contents
;
6365 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6366 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6372 Elf_Internal_Rela rel
;
6375 /* This symbol needs a copy reloc. Set it up. */
6377 BFD_ASSERT (h
->dynindx
!= -1
6378 && (h
->root
.type
== bfd_link_hash_defined
6379 || h
->root
.type
== bfd_link_hash_defweak
));
6381 s
= bfd_get_linker_section (htab
->root
.dynobj
, ".rela.bss");
6382 BFD_ASSERT (s
!= NULL
);
6384 rel
.r_offset
= (h
->root
.u
.def
.value
6385 + h
->root
.u
.def
.section
->output_section
->vma
6386 + h
->root
.u
.def
.section
->output_offset
);
6387 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
6389 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6390 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6393 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
6394 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6396 if (h
== htab
->root
.hdynamic
6397 || (!htab
->vxworks_p
&& h
== htab
->root
.hgot
))
6398 sym
->st_shndx
= SHN_ABS
;
6403 /* Finish up the dynamic sections. */
6406 sh_elf_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6408 struct elf_sh_link_hash_table
*htab
;
6412 htab
= sh_elf_hash_table (info
);
6416 sgotplt
= htab
->root
.sgotplt
;
6417 sdyn
= bfd_get_linker_section (htab
->root
.dynobj
, ".dynamic");
6419 if (htab
->root
.dynamic_sections_created
)
6422 Elf32_External_Dyn
*dyncon
, *dynconend
;
6424 BFD_ASSERT (sgotplt
!= NULL
&& sdyn
!= NULL
);
6426 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
6427 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
6428 for (; dyncon
< dynconend
; dyncon
++)
6430 Elf_Internal_Dyn dyn
;
6433 bfd_elf32_swap_dyn_in (htab
->root
.dynobj
, dyncon
, &dyn
);
6439 && elf_vxworks_finish_dynamic_entry (output_bfd
, &dyn
))
6440 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6444 BFD_ASSERT (htab
->root
.hgot
!= NULL
);
6445 s
= htab
->root
.hgot
->root
.u
.def
.section
;
6446 dyn
.d_un
.d_ptr
= htab
->root
.hgot
->root
.u
.def
.value
6447 + s
->output_section
->vma
+ s
->output_offset
;
6448 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6452 s
= htab
->root
.srelplt
->output_section
;
6453 BFD_ASSERT (s
!= NULL
);
6454 dyn
.d_un
.d_ptr
= s
->vma
;
6455 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6459 s
= htab
->root
.srelplt
->output_section
;
6460 BFD_ASSERT (s
!= NULL
);
6461 dyn
.d_un
.d_val
= s
->size
;
6462 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6467 /* Fill in the first entry in the procedure linkage table. */
6468 splt
= htab
->root
.splt
;
6469 if (splt
&& splt
->size
> 0 && htab
->plt_info
->plt0_entry
)
6473 memcpy (splt
->contents
,
6474 htab
->plt_info
->plt0_entry
,
6475 htab
->plt_info
->plt0_entry_size
);
6476 for (i
= 0; i
< ARRAY_SIZE (htab
->plt_info
->plt0_got_fields
); i
++)
6477 if (htab
->plt_info
->plt0_got_fields
[i
] != MINUS_ONE
)
6478 install_plt_field (output_bfd
, FALSE
,
6479 (sgotplt
->output_section
->vma
6480 + sgotplt
->output_offset
6483 + htab
->plt_info
->plt0_got_fields
[i
]));
6485 if (htab
->vxworks_p
)
6487 /* Finalize the .rela.plt.unloaded contents. */
6488 Elf_Internal_Rela rel
;
6491 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
6492 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
6493 loc
= htab
->srelplt2
->contents
;
6494 rel
.r_offset
= (splt
->output_section
->vma
6495 + splt
->output_offset
6496 + htab
->plt_info
->plt0_got_fields
[2]);
6497 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
6499 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6500 loc
+= sizeof (Elf32_External_Rela
);
6502 /* Fix up the remaining .rela.plt.unloaded relocations.
6503 They may have the wrong symbol index for _G_O_T_ or
6504 _P_L_T_ depending on the order in which symbols were
6506 while (loc
< htab
->srelplt2
->contents
+ htab
->srelplt2
->size
)
6508 /* The PLT entry's pointer to the .got.plt slot. */
6509 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
6510 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
,
6512 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6513 loc
+= sizeof (Elf32_External_Rela
);
6515 /* The .got.plt slot's pointer to .plt. */
6516 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
6517 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
,
6519 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6520 loc
+= sizeof (Elf32_External_Rela
);
6524 /* UnixWare sets the entsize of .plt to 4, although that doesn't
6525 really seem like the right value. */
6526 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
6530 /* Fill in the first three entries in the global offset table. */
6531 if (sgotplt
&& sgotplt
->size
> 0 && !htab
->fdpic_p
)
6534 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
6536 bfd_put_32 (output_bfd
,
6537 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
6539 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
6540 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
6543 if (sgotplt
&& sgotplt
->size
> 0)
6544 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
6546 /* At the very end of the .rofixup section is a pointer to the GOT. */
6547 if (htab
->fdpic_p
&& htab
->srofixup
!= NULL
)
6549 struct elf_link_hash_entry
*hgot
= htab
->root
.hgot
;
6550 bfd_vma got_value
= hgot
->root
.u
.def
.value
6551 + hgot
->root
.u
.def
.section
->output_section
->vma
6552 + hgot
->root
.u
.def
.section
->output_offset
;
6554 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
, got_value
);
6556 /* Make sure we allocated and generated the same number of fixups. */
6557 BFD_ASSERT (htab
->srofixup
->reloc_count
* 4 == htab
->srofixup
->size
);
6560 if (htab
->srelfuncdesc
)
6561 BFD_ASSERT (htab
->srelfuncdesc
->reloc_count
* sizeof (Elf32_External_Rela
)
6562 == htab
->srelfuncdesc
->size
);
6564 if (htab
->root
.srelgot
)
6565 BFD_ASSERT (htab
->root
.srelgot
->reloc_count
* sizeof (Elf32_External_Rela
)
6566 == htab
->root
.srelgot
->size
);
6571 static enum elf_reloc_type_class
6572 sh_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
6573 const asection
*rel_sec ATTRIBUTE_UNUSED
,
6574 const Elf_Internal_Rela
*rela
)
6576 switch ((int) ELF32_R_TYPE (rela
->r_info
))
6579 return reloc_class_relative
;
6581 return reloc_class_plt
;
6583 return reloc_class_copy
;
6585 return reloc_class_normal
;
6589 #if !defined SH_TARGET_ALREADY_DEFINED
6590 /* Support for Linux core dump NOTE sections. */
6593 elf32_shlin_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
6598 switch (note
->descsz
)
6603 case 168: /* Linux/SH */
6605 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
6608 elf_tdata (abfd
)->core
->lwpid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
6617 /* Make a ".reg/999" section. */
6618 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6619 size
, note
->descpos
+ offset
);
6623 elf32_shlin_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
6625 switch (note
->descsz
)
6630 case 124: /* Linux/SH elf_prpsinfo */
6631 elf_tdata (abfd
)->core
->program
6632 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
6633 elf_tdata (abfd
)->core
->command
6634 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
6637 /* Note that for some reason, a spurious space is tacked
6638 onto the end of the args in some (at least one anyway)
6639 implementations, so strip it off if it exists. */
6642 char *command
= elf_tdata (abfd
)->core
->command
;
6643 int n
= strlen (command
);
6645 if (0 < n
&& command
[n
- 1] == ' ')
6646 command
[n
- 1] = '\0';
6651 #endif /* not SH_TARGET_ALREADY_DEFINED */
6654 /* Return address for Ith PLT stub in section PLT, for relocation REL
6655 or (bfd_vma) -1 if it should not be included. */
6658 sh_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
6659 const arelent
*rel ATTRIBUTE_UNUSED
)
6661 const struct elf_sh_plt_info
*plt_info
;
6663 plt_info
= get_plt_info (plt
->owner
, (plt
->owner
->flags
& DYNAMIC
) != 0);
6664 return plt
->vma
+ get_plt_offset (plt_info
, i
);
6667 /* Decide whether to attempt to turn absptr or lsda encodings in
6668 shared libraries into pcrel within the given input section. */
6671 sh_elf_use_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
6672 struct bfd_link_info
*info
,
6673 asection
*eh_frame_section ATTRIBUTE_UNUSED
)
6675 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
6677 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
6684 /* Adjust the contents of an eh_frame_hdr section before they're output. */
6687 sh_elf_encode_eh_address (bfd
*abfd
,
6688 struct bfd_link_info
*info
,
6689 asection
*osec
, bfd_vma offset
,
6690 asection
*loc_sec
, bfd_vma loc_offset
,
6693 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
6694 struct elf_link_hash_entry
*h
;
6697 return _bfd_elf_encode_eh_address (abfd
, info
, osec
, offset
, loc_sec
,
6698 loc_offset
, encoded
);
6700 h
= htab
->root
.hgot
;
6701 BFD_ASSERT (h
&& h
->root
.type
== bfd_link_hash_defined
);
6703 if (! h
|| (sh_elf_osec_to_segment (abfd
, osec
)
6704 == sh_elf_osec_to_segment (abfd
, loc_sec
->output_section
)))
6705 return _bfd_elf_encode_eh_address (abfd
, info
, osec
, offset
,
6706 loc_sec
, loc_offset
, encoded
);
6708 BFD_ASSERT (sh_elf_osec_to_segment (abfd
, osec
)
6709 == (sh_elf_osec_to_segment
6710 (abfd
, h
->root
.u
.def
.section
->output_section
)));
6712 *encoded
= osec
->vma
+ offset
6713 - (h
->root
.u
.def
.value
6714 + h
->root
.u
.def
.section
->output_section
->vma
6715 + h
->root
.u
.def
.section
->output_offset
);
6717 return DW_EH_PE_datarel
| DW_EH_PE_sdata4
;
6720 #if !defined SH_TARGET_ALREADY_DEFINED
6721 #define TARGET_BIG_SYM sh_elf32_vec
6722 #define TARGET_BIG_NAME "elf32-sh"
6723 #define TARGET_LITTLE_SYM sh_elf32_le_vec
6724 #define TARGET_LITTLE_NAME "elf32-shl"
6727 #define ELF_ARCH bfd_arch_sh
6728 #define ELF_TARGET_ID SH_ELF_DATA
6729 #define ELF_MACHINE_CODE EM_SH
6730 #ifdef __QNXTARGET__
6731 #define ELF_MAXPAGESIZE 0x1000
6733 #define ELF_MAXPAGESIZE 0x80
6736 #define elf_symbol_leading_char '_'
6738 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6739 #define bfd_elf32_bfd_reloc_name_lookup \
6740 sh_elf_reloc_name_lookup
6741 #define elf_info_to_howto sh_elf_info_to_howto
6742 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
6743 #define elf_backend_relocate_section sh_elf_relocate_section
6744 #define bfd_elf32_bfd_get_relocated_section_contents \
6745 sh_elf_get_relocated_section_contents
6746 #define bfd_elf32_mkobject sh_elf_mkobject
6747 #define elf_backend_object_p sh_elf_object_p
6748 #define bfd_elf32_bfd_copy_private_bfd_data \
6749 sh_elf_copy_private_data
6750 #define bfd_elf32_bfd_merge_private_bfd_data \
6751 sh_elf_merge_private_data
6753 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6754 #define elf_backend_check_relocs sh_elf_check_relocs
6755 #define elf_backend_copy_indirect_symbol \
6756 sh_elf_copy_indirect_symbol
6757 #define elf_backend_create_dynamic_sections \
6758 sh_elf_create_dynamic_sections
6759 #define bfd_elf32_bfd_link_hash_table_create \
6760 sh_elf_link_hash_table_create
6761 #define elf_backend_adjust_dynamic_symbol \
6762 sh_elf_adjust_dynamic_symbol
6763 #define elf_backend_always_size_sections \
6764 sh_elf_always_size_sections
6765 #define elf_backend_size_dynamic_sections \
6766 sh_elf_size_dynamic_sections
6767 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
6768 #define elf_backend_finish_dynamic_symbol \
6769 sh_elf_finish_dynamic_symbol
6770 #define elf_backend_finish_dynamic_sections \
6771 sh_elf_finish_dynamic_sections
6772 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
6773 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
6774 #define elf_backend_can_make_relative_eh_frame \
6775 sh_elf_use_relative_eh_frame
6776 #define elf_backend_can_make_lsda_relative_eh_frame \
6777 sh_elf_use_relative_eh_frame
6778 #define elf_backend_encode_eh_address \
6779 sh_elf_encode_eh_address
6781 #define elf_backend_stack_align 8
6782 #define elf_backend_can_gc_sections 1
6783 #define elf_backend_can_refcount 1
6784 #define elf_backend_want_got_plt 1
6785 #define elf_backend_plt_readonly 1
6786 #define elf_backend_want_plt_sym 0
6787 #define elf_backend_got_header_size 12
6788 #define elf_backend_dtrel_excludes_plt 1
6790 #define elf_backend_linux_prpsinfo32_ugid16 TRUE
6792 #if !defined SH_TARGET_ALREADY_DEFINED
6794 #include "elf32-target.h"
6796 /* NetBSD support. */
6797 #undef TARGET_BIG_SYM
6798 #define TARGET_BIG_SYM sh_elf32_nbsd_vec
6799 #undef TARGET_BIG_NAME
6800 #define TARGET_BIG_NAME "elf32-sh-nbsd"
6801 #undef TARGET_LITTLE_SYM
6802 #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
6803 #undef TARGET_LITTLE_NAME
6804 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6805 #undef ELF_MAXPAGESIZE
6806 #define ELF_MAXPAGESIZE 0x10000
6807 #undef ELF_COMMONPAGESIZE
6808 #undef elf_symbol_leading_char
6809 #define elf_symbol_leading_char 0
6811 #define elf32_bed elf32_sh_nbsd_bed
6813 #include "elf32-target.h"
6816 /* Linux support. */
6817 #undef TARGET_BIG_SYM
6818 #define TARGET_BIG_SYM sh_elf32_linux_be_vec
6819 #undef TARGET_BIG_NAME
6820 #define TARGET_BIG_NAME "elf32-shbig-linux"
6821 #undef TARGET_LITTLE_SYM
6822 #define TARGET_LITTLE_SYM sh_elf32_linux_vec
6823 #undef TARGET_LITTLE_NAME
6824 #define TARGET_LITTLE_NAME "elf32-sh-linux"
6825 #undef ELF_COMMONPAGESIZE
6826 #define ELF_COMMONPAGESIZE 0x1000
6828 #undef elf_backend_grok_prstatus
6829 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6830 #undef elf_backend_grok_psinfo
6831 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
6833 #define elf32_bed elf32_sh_lin_bed
6835 #include "elf32-target.h"
6838 /* FDPIC support. */
6839 #undef TARGET_BIG_SYM
6840 #define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
6841 #undef TARGET_BIG_NAME
6842 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
6843 #undef TARGET_LITTLE_SYM
6844 #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
6845 #undef TARGET_LITTLE_NAME
6846 #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
6849 #define elf32_bed elf32_sh_fd_bed
6851 #include "elf32-target.h"
6853 /* VxWorks support. */
6854 #undef TARGET_BIG_SYM
6855 #define TARGET_BIG_SYM sh_elf32_vxworks_vec
6856 #undef TARGET_BIG_NAME
6857 #define TARGET_BIG_NAME "elf32-sh-vxworks"
6858 #undef TARGET_LITTLE_SYM
6859 #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
6860 #undef TARGET_LITTLE_NAME
6861 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6863 #define elf32_bed elf32_sh_vxworks_bed
6865 #undef elf_backend_want_plt_sym
6866 #define elf_backend_want_plt_sym 1
6867 #undef elf_symbol_leading_char
6868 #define elf_symbol_leading_char '_'
6869 #define elf_backend_want_got_underscore 1
6870 #undef elf_backend_grok_prstatus
6871 #undef elf_backend_grok_psinfo
6872 #undef elf_backend_add_symbol_hook
6873 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6874 #undef elf_backend_link_output_symbol_hook
6875 #define elf_backend_link_output_symbol_hook \
6876 elf_vxworks_link_output_symbol_hook
6877 #undef elf_backend_emit_relocs
6878 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6879 #undef elf_backend_final_write_processing
6880 #define elf_backend_final_write_processing \
6881 elf_vxworks_final_write_processing
6882 #undef ELF_MAXPAGESIZE
6883 #define ELF_MAXPAGESIZE 0x1000
6884 #undef ELF_COMMONPAGESIZE
6886 #include "elf32-target.h"
6888 #endif /* not SH_TARGET_ALREADY_DEFINED */