1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 #include "elf-vxworks.h"
30 #include "libiberty.h"
31 #include "../opcodes/sh-opc.h"
33 static bfd_reloc_status_type sh_elf_reloc
34 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
35 static bfd_reloc_status_type sh_elf_ignore_reloc
36 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
37 static bfd_boolean sh_elf_relax_delete_bytes
38 (bfd
*, asection
*, bfd_vma
, int);
39 static bfd_boolean sh_elf_align_loads
40 (bfd
*, asection
*, Elf_Internal_Rela
*, bfd_byte
*, bfd_boolean
*);
42 static bfd_boolean sh_elf_swap_insns
43 (bfd
*, asection
*, void *, bfd_byte
*, bfd_vma
);
45 static int sh_elf_optimized_tls_reloc
46 (struct bfd_link_info
*, int, int);
47 static bfd_vma dtpoff_base
48 (struct bfd_link_info
*);
50 (struct bfd_link_info
*, bfd_vma
);
52 /* The name of the dynamic interpreter. This is put in the .interp
55 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
57 /* FDPIC binaries have a default 128K stack. */
58 #define DEFAULT_STACK_SIZE 0x20000
60 #define MINUS_ONE ((bfd_vma) 0 - 1)
62 /* Decide whether a reference to a symbol can be resolved locally or
63 not. If the symbol is protected, we want the local address, but
64 its function descriptor must be assigned by the dynamic linker. */
65 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
66 (SYMBOL_REFERENCES_LOCAL (INFO, H) \
67 || ! elf_hash_table (INFO)->dynamic_sections_created)
69 #define SH_PARTIAL32 TRUE
70 #define SH_SRC_MASK32 0xffffffff
71 #define SH_ELF_RELOC sh_elf_reloc
72 static reloc_howto_type sh_elf_howto_table
[] =
74 #include "elf32-sh-relocs.h"
77 #define SH_PARTIAL32 FALSE
78 #define SH_SRC_MASK32 0
79 #define SH_ELF_RELOC bfd_elf_generic_reloc
80 static reloc_howto_type sh_vxworks_howto_table
[] =
82 #include "elf32-sh-relocs.h"
85 /* Return true if OUTPUT_BFD is a VxWorks object. */
88 vxworks_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
90 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
91 extern const bfd_target sh_elf32_vxworks_le_vec
;
92 extern const bfd_target sh_elf32_vxworks_vec
;
94 return (abfd
->xvec
== &sh_elf32_vxworks_le_vec
95 || abfd
->xvec
== &sh_elf32_vxworks_vec
);
101 /* Return true if OUTPUT_BFD is an FDPIC object. */
104 fdpic_object_p (bfd
*abfd ATTRIBUTE_UNUSED
)
106 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
107 extern const bfd_target sh_elf32_fdpic_le_vec
;
108 extern const bfd_target sh_elf32_fdpic_be_vec
;
110 return (abfd
->xvec
== &sh_elf32_fdpic_le_vec
111 || abfd
->xvec
== &sh_elf32_fdpic_be_vec
);
117 /* Return the howto table for ABFD. */
119 static reloc_howto_type
*
120 get_howto_table (bfd
*abfd
)
122 if (vxworks_object_p (abfd
))
123 return sh_vxworks_howto_table
;
124 return sh_elf_howto_table
;
127 static bfd_reloc_status_type
128 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED
, bfd
*input_bfd
,
129 asection
*input_section
, bfd_byte
*contents
,
130 bfd_vma addr
, asection
*symbol_section
,
131 bfd_vma start
, bfd_vma end
)
133 static bfd_vma last_addr
;
134 static asection
*last_symbol_section
;
135 bfd_byte
*start_ptr
, *ptr
, *last_ptr
;
140 /* Sanity check the address. */
141 if (addr
> bfd_get_section_limit (input_bfd
, input_section
))
142 return bfd_reloc_outofrange
;
144 /* We require the start and end relocations to be processed consecutively -
145 although we allow then to be processed forwards or backwards. */
149 last_symbol_section
= symbol_section
;
152 if (last_addr
!= addr
)
156 if (! symbol_section
|| last_symbol_section
!= symbol_section
|| end
< start
)
157 return bfd_reloc_outofrange
;
159 /* Get the symbol_section contents. */
160 if (symbol_section
!= input_section
)
162 if (elf_section_data (symbol_section
)->this_hdr
.contents
!= NULL
)
163 contents
= elf_section_data (symbol_section
)->this_hdr
.contents
;
166 if (!bfd_malloc_and_get_section (input_bfd
, symbol_section
,
169 if (contents
!= NULL
)
171 return bfd_reloc_outofrange
;
175 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
176 start_ptr
= contents
+ start
;
177 for (cum_diff
= -6, ptr
= contents
+ end
; cum_diff
< 0 && ptr
> start_ptr
;)
179 for (last_ptr
= ptr
, ptr
-= 4; ptr
>= start_ptr
&& IS_PPI (ptr
);)
182 diff
= (last_ptr
- ptr
) >> 1;
183 cum_diff
+= diff
& 1;
186 /* Calculate the start / end values to load into rs / re minus four -
187 so that will cancel out the four we would otherwise have to add to
188 addr to get the value to subtract in order to get relative addressing. */
192 end
= (ptr
+ cum_diff
* 2) - contents
;
196 bfd_vma start0
= start
- 4;
198 while (start0
&& IS_PPI (contents
+ start0
))
200 start0
= start
- 2 - ((start
- start0
) & 2);
201 start
= start0
- cum_diff
- 2;
206 && elf_section_data (symbol_section
)->this_hdr
.contents
!= contents
)
209 insn
= bfd_get_16 (input_bfd
, contents
+ addr
);
211 x
= (insn
& 0x200 ? end
: start
) - addr
;
212 if (input_section
!= symbol_section
)
213 x
+= ((symbol_section
->output_section
->vma
+ symbol_section
->output_offset
)
214 - (input_section
->output_section
->vma
215 + input_section
->output_offset
));
217 if (x
< -128 || x
> 127)
218 return bfd_reloc_overflow
;
220 x
= (insn
& ~0xff) | (x
& 0xff);
221 bfd_put_16 (input_bfd
, (bfd_vma
) x
, contents
+ addr
);
226 /* This function is used for normal relocs. This used to be like the COFF
227 function, and is almost certainly incorrect for other ELF targets. */
229 static bfd_reloc_status_type
230 sh_elf_reloc (bfd
*abfd
, arelent
*reloc_entry
, asymbol
*symbol_in
,
231 void *data
, asection
*input_section
, bfd
*output_bfd
,
232 char **error_message ATTRIBUTE_UNUSED
)
236 enum elf_sh_reloc_type r_type
;
237 bfd_vma addr
= reloc_entry
->address
;
238 bfd_byte
*hit_data
= addr
+ (bfd_byte
*) data
;
240 r_type
= (enum elf_sh_reloc_type
) reloc_entry
->howto
->type
;
242 if (output_bfd
!= NULL
)
244 /* Partial linking--do nothing. */
245 reloc_entry
->address
+= input_section
->output_offset
;
249 /* Almost all relocs have to do with relaxing. If any work must be
250 done for them, it has been done in sh_relax_section. */
251 if (r_type
== R_SH_IND12W
&& (symbol_in
->flags
& BSF_LOCAL
) != 0)
254 if (symbol_in
!= NULL
255 && bfd_is_und_section (symbol_in
->section
))
256 return bfd_reloc_undefined
;
258 /* PR 17512: file: 9891ca98. */
259 if (addr
* bfd_octets_per_byte (abfd
) + bfd_get_reloc_size (reloc_entry
->howto
)
260 > bfd_get_section_limit_octets (abfd
, input_section
))
261 return bfd_reloc_outofrange
;
263 if (bfd_is_com_section (symbol_in
->section
))
266 sym_value
= (symbol_in
->value
+
267 symbol_in
->section
->output_section
->vma
+
268 symbol_in
->section
->output_offset
);
273 insn
= bfd_get_32 (abfd
, hit_data
);
274 insn
+= sym_value
+ reloc_entry
->addend
;
275 bfd_put_32 (abfd
, (bfd_vma
) insn
, hit_data
);
278 insn
= bfd_get_16 (abfd
, hit_data
);
279 sym_value
+= reloc_entry
->addend
;
280 sym_value
-= (input_section
->output_section
->vma
281 + input_section
->output_offset
284 sym_value
+= (insn
& 0xfff) << 1;
287 insn
= (insn
& 0xf000) | (sym_value
& 0xfff);
288 bfd_put_16 (abfd
, (bfd_vma
) insn
, hit_data
);
289 if (sym_value
< (bfd_vma
) -0x1000 || sym_value
>= 0x1000)
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
},
374 #ifdef INCLUDE_SHMEDIA
375 { BFD_RELOC_SH_GOT_LOW16
, R_SH_GOT_LOW16
},
376 { BFD_RELOC_SH_GOT_MEDLOW16
, R_SH_GOT_MEDLOW16
},
377 { BFD_RELOC_SH_GOT_MEDHI16
, R_SH_GOT_MEDHI16
},
378 { BFD_RELOC_SH_GOT_HI16
, R_SH_GOT_HI16
},
379 { BFD_RELOC_SH_GOTPLT_LOW16
, R_SH_GOTPLT_LOW16
},
380 { BFD_RELOC_SH_GOTPLT_MEDLOW16
, R_SH_GOTPLT_MEDLOW16
},
381 { BFD_RELOC_SH_GOTPLT_MEDHI16
, R_SH_GOTPLT_MEDHI16
},
382 { BFD_RELOC_SH_GOTPLT_HI16
, R_SH_GOTPLT_HI16
},
383 { BFD_RELOC_SH_PLT_LOW16
, R_SH_PLT_LOW16
},
384 { BFD_RELOC_SH_PLT_MEDLOW16
, R_SH_PLT_MEDLOW16
},
385 { BFD_RELOC_SH_PLT_MEDHI16
, R_SH_PLT_MEDHI16
},
386 { BFD_RELOC_SH_PLT_HI16
, R_SH_PLT_HI16
},
387 { BFD_RELOC_SH_GOTOFF_LOW16
, R_SH_GOTOFF_LOW16
},
388 { BFD_RELOC_SH_GOTOFF_MEDLOW16
, R_SH_GOTOFF_MEDLOW16
},
389 { BFD_RELOC_SH_GOTOFF_MEDHI16
, R_SH_GOTOFF_MEDHI16
},
390 { BFD_RELOC_SH_GOTOFF_HI16
, R_SH_GOTOFF_HI16
},
391 { BFD_RELOC_SH_GOTPC_LOW16
, R_SH_GOTPC_LOW16
},
392 { BFD_RELOC_SH_GOTPC_MEDLOW16
, R_SH_GOTPC_MEDLOW16
},
393 { BFD_RELOC_SH_GOTPC_MEDHI16
, R_SH_GOTPC_MEDHI16
},
394 { BFD_RELOC_SH_GOTPC_HI16
, R_SH_GOTPC_HI16
},
395 { BFD_RELOC_SH_COPY64
, R_SH_COPY64
},
396 { BFD_RELOC_SH_GLOB_DAT64
, R_SH_GLOB_DAT64
},
397 { BFD_RELOC_SH_JMP_SLOT64
, R_SH_JMP_SLOT64
},
398 { BFD_RELOC_SH_RELATIVE64
, R_SH_RELATIVE64
},
399 { BFD_RELOC_SH_GOT10BY4
, R_SH_GOT10BY4
},
400 { BFD_RELOC_SH_GOT10BY8
, R_SH_GOT10BY8
},
401 { BFD_RELOC_SH_GOTPLT10BY4
, R_SH_GOTPLT10BY4
},
402 { BFD_RELOC_SH_GOTPLT10BY8
, R_SH_GOTPLT10BY8
},
403 { BFD_RELOC_SH_PT_16
, R_SH_PT_16
},
404 { BFD_RELOC_SH_SHMEDIA_CODE
, R_SH_SHMEDIA_CODE
},
405 { BFD_RELOC_SH_IMMU5
, R_SH_DIR5U
},
406 { BFD_RELOC_SH_IMMS6
, R_SH_DIR6S
},
407 { BFD_RELOC_SH_IMMU6
, R_SH_DIR6U
},
408 { BFD_RELOC_SH_IMMS10
, R_SH_DIR10S
},
409 { BFD_RELOC_SH_IMMS10BY2
, R_SH_DIR10SW
},
410 { BFD_RELOC_SH_IMMS10BY4
, R_SH_DIR10SL
},
411 { BFD_RELOC_SH_IMMS10BY8
, R_SH_DIR10SQ
},
412 { BFD_RELOC_SH_IMMS16
, R_SH_IMMS16
},
413 { BFD_RELOC_SH_IMMU16
, R_SH_IMMU16
},
414 { BFD_RELOC_SH_IMM_LOW16
, R_SH_IMM_LOW16
},
415 { BFD_RELOC_SH_IMM_LOW16_PCREL
, R_SH_IMM_LOW16_PCREL
},
416 { BFD_RELOC_SH_IMM_MEDLOW16
, R_SH_IMM_MEDLOW16
},
417 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL
, R_SH_IMM_MEDLOW16_PCREL
},
418 { BFD_RELOC_SH_IMM_MEDHI16
, R_SH_IMM_MEDHI16
},
419 { BFD_RELOC_SH_IMM_MEDHI16_PCREL
, R_SH_IMM_MEDHI16_PCREL
},
420 { BFD_RELOC_SH_IMM_HI16
, R_SH_IMM_HI16
},
421 { BFD_RELOC_SH_IMM_HI16_PCREL
, R_SH_IMM_HI16_PCREL
},
422 { BFD_RELOC_64
, R_SH_64
},
423 { BFD_RELOC_64_PCREL
, R_SH_64_PCREL
},
424 #endif /* not INCLUDE_SHMEDIA */
427 /* Given a BFD reloc code, return the howto structure for the
428 corresponding SH ELF reloc. */
430 static reloc_howto_type
*
431 sh_elf_reloc_type_lookup (bfd
*abfd
, bfd_reloc_code_real_type code
)
435 for (i
= 0; i
< sizeof (sh_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
437 if (sh_reloc_map
[i
].bfd_reloc_val
== code
)
438 return get_howto_table (abfd
) + (int) sh_reloc_map
[i
].elf_reloc_val
;
444 static reloc_howto_type
*
445 sh_elf_reloc_name_lookup (bfd
*abfd
, const char *r_name
)
449 if (vxworks_object_p (abfd
))
452 i
< (sizeof (sh_vxworks_howto_table
)
453 / sizeof (sh_vxworks_howto_table
[0]));
455 if (sh_vxworks_howto_table
[i
].name
!= NULL
456 && strcasecmp (sh_vxworks_howto_table
[i
].name
, r_name
) == 0)
457 return &sh_vxworks_howto_table
[i
];
462 i
< (sizeof (sh_elf_howto_table
)
463 / sizeof (sh_elf_howto_table
[0]));
465 if (sh_elf_howto_table
[i
].name
!= NULL
466 && strcasecmp (sh_elf_howto_table
[i
].name
, r_name
) == 0)
467 return &sh_elf_howto_table
[i
];
473 /* Given an ELF reloc, fill in the howto field of a relent. */
476 sh_elf_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
, Elf_Internal_Rela
*dst
)
480 r
= ELF32_R_TYPE (dst
->r_info
);
483 || (r
>= R_SH_FIRST_INVALID_RELOC
&& r
<= R_SH_LAST_INVALID_RELOC
)
484 || (r
>= R_SH_FIRST_INVALID_RELOC_2
&& r
<= R_SH_LAST_INVALID_RELOC_2
)
485 || (r
>= R_SH_FIRST_INVALID_RELOC_3
&& r
<= R_SH_LAST_INVALID_RELOC_3
)
486 || (r
>= R_SH_FIRST_INVALID_RELOC_4
&& r
<= R_SH_LAST_INVALID_RELOC_4
)
487 || (r
>= R_SH_FIRST_INVALID_RELOC_5
&& r
<= R_SH_LAST_INVALID_RELOC_5
)
488 || (r
>= R_SH_FIRST_INVALID_RELOC_6
&& r
<= R_SH_LAST_INVALID_RELOC_6
))
490 /* xgettext:c-format */
491 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
493 bfd_set_error (bfd_error_bad_value
);
497 cache_ptr
->howto
= get_howto_table (abfd
) + r
;
501 /* This function handles relaxing for SH ELF. See the corresponding
502 function in coff-sh.c for a description of what this does. FIXME:
503 There is a lot of duplication here between this code and the COFF
504 specific code. The format of relocs and symbols is wound deeply
505 into this code, but it would still be better if the duplication
506 could be eliminated somehow. Note in particular that although both
507 functions use symbols like R_SH_CODE, those symbols have different
508 values; in coff-sh.c they come from include/coff/sh.h, whereas here
509 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
512 sh_elf_relax_section (bfd
*abfd
, asection
*sec
,
513 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
515 Elf_Internal_Shdr
*symtab_hdr
;
516 Elf_Internal_Rela
*internal_relocs
;
517 bfd_boolean have_code
;
518 Elf_Internal_Rela
*irel
, *irelend
;
519 bfd_byte
*contents
= NULL
;
520 Elf_Internal_Sym
*isymbuf
= NULL
;
524 if (bfd_link_relocatable (link_info
)
525 || (sec
->flags
& SEC_RELOC
) == 0
526 || sec
->reloc_count
== 0)
529 #ifdef INCLUDE_SHMEDIA
530 if (elf_section_data (sec
)->this_hdr
.sh_flags
531 & (SHF_SH5_ISA32
| SHF_SH5_ISA32_MIXED
))
537 symtab_hdr
= &elf_symtab_hdr (abfd
);
539 internal_relocs
= (_bfd_elf_link_read_relocs
540 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
541 link_info
->keep_memory
));
542 if (internal_relocs
== NULL
)
547 irelend
= internal_relocs
+ sec
->reloc_count
;
548 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
550 bfd_vma laddr
, paddr
, symval
;
552 Elf_Internal_Rela
*irelfn
, *irelscan
, *irelcount
;
555 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_CODE
)
558 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_USES
)
561 /* Get the section contents. */
562 if (contents
== NULL
)
564 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
565 contents
= elf_section_data (sec
)->this_hdr
.contents
;
568 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
573 /* The r_addend field of the R_SH_USES reloc will point us to
574 the register load. The 4 is because the r_addend field is
575 computed as though it were a jump offset, which are based
576 from 4 bytes after the jump instruction. */
577 laddr
= irel
->r_offset
+ 4 + irel
->r_addend
;
578 if (laddr
>= sec
->size
)
580 /* xgettext:c-format */
582 (_("%pB: %#" PRIx64
": warning: bad R_SH_USES offset"),
583 abfd
, (uint64_t) irel
->r_offset
);
586 insn
= bfd_get_16 (abfd
, contents
+ laddr
);
588 /* If the instruction is not mov.l NN,rN, we don't know what to
590 if ((insn
& 0xf000) != 0xd000)
593 /* xgettext:c-format */
594 (_("%pB: %#" PRIx64
": warning: "
595 "R_SH_USES points to unrecognized insn 0x%x"),
596 abfd
, (uint64_t) irel
->r_offset
, insn
);
600 /* Get the address from which the register is being loaded. The
601 displacement in the mov.l instruction is quadrupled. It is a
602 displacement from four bytes after the movl instruction, but,
603 before adding in the PC address, two least significant bits
604 of the PC are cleared. We assume that the section is aligned
605 on a four byte boundary. */
608 paddr
+= (laddr
+ 4) &~ (bfd_vma
) 3;
609 if (paddr
>= sec
->size
)
612 /* xgettext:c-format */
613 (_("%pB: %#" PRIx64
": warning: bad R_SH_USES load offset"),
614 abfd
, (uint64_t) irel
->r_offset
);
618 /* Get the reloc for the address from which the register is
619 being loaded. This reloc will tell us which function is
620 actually being called. */
621 for (irelfn
= internal_relocs
; irelfn
< irelend
; irelfn
++)
622 if (irelfn
->r_offset
== paddr
623 && ELF32_R_TYPE (irelfn
->r_info
) == (int) R_SH_DIR32
)
625 if (irelfn
>= irelend
)
628 /* xgettext:c-format */
629 (_("%pB: %#" PRIx64
": warning: could not find expected reloc"),
630 abfd
, (uint64_t) paddr
);
634 /* Read this BFD's symbols if we haven't done so already. */
635 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
637 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
639 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
640 symtab_hdr
->sh_info
, 0,
646 /* Get the value of the symbol referred to by the reloc. */
647 if (ELF32_R_SYM (irelfn
->r_info
) < symtab_hdr
->sh_info
)
649 /* A local symbol. */
650 Elf_Internal_Sym
*isym
;
652 isym
= isymbuf
+ ELF32_R_SYM (irelfn
->r_info
);
654 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd
, sec
))
657 /* xgettext:c-format */
658 (_("%pB: %#" PRIx64
": warning: symbol in unexpected section"),
659 abfd
, (uint64_t) paddr
);
663 symval
= (isym
->st_value
664 + sec
->output_section
->vma
665 + sec
->output_offset
);
670 struct elf_link_hash_entry
*h
;
672 indx
= ELF32_R_SYM (irelfn
->r_info
) - symtab_hdr
->sh_info
;
673 h
= elf_sym_hashes (abfd
)[indx
];
674 BFD_ASSERT (h
!= NULL
);
675 if (h
->root
.type
!= bfd_link_hash_defined
676 && h
->root
.type
!= bfd_link_hash_defweak
)
678 /* This appears to be a reference to an undefined
679 symbol. Just ignore it--it will be caught by the
680 regular reloc processing. */
684 symval
= (h
->root
.u
.def
.value
685 + h
->root
.u
.def
.section
->output_section
->vma
686 + h
->root
.u
.def
.section
->output_offset
);
689 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
690 symval
+= bfd_get_32 (abfd
, contents
+ paddr
);
692 symval
+= irelfn
->r_addend
;
694 /* See if this function call can be shortened. */
697 + sec
->output_section
->vma
700 /* A branch to an address beyond ours might be increased by an
701 .align that doesn't move when bytes behind us are deleted.
702 So, we add some slop in this calculation to allow for
704 if (foff
< -0x1000 || foff
>= 0x1000 - 8)
706 /* After all that work, we can't shorten this function call. */
710 /* Shorten the function call. */
712 /* For simplicity of coding, we are going to modify the section
713 contents, the section relocs, and the BFD symbol table. We
714 must tell the rest of the code not to free up this
715 information. It would be possible to instead create a table
716 of changes which have to be made, as is done in coff-mips.c;
717 that would be more work, but would require less memory when
718 the linker is run. */
720 elf_section_data (sec
)->relocs
= internal_relocs
;
721 elf_section_data (sec
)->this_hdr
.contents
= contents
;
722 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
724 /* Replace the jmp/jsr with a bra/bsr. */
726 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
727 replace the jmp/jsr with a bra/bsr. */
728 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irelfn
->r_info
), R_SH_IND12W
);
729 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
730 here, but that only checks if the symbol is an external symbol,
731 not if the symbol is in a different section. Besides, we need
732 a consistent meaning for the relocation, so we just assume here that
733 the value of the symbol is not available. */
735 /* We can't fully resolve this yet, because the external
736 symbol value may be changed by future relaxing. We let
737 the final link phase handle it. */
738 if (bfd_get_16 (abfd
, contents
+ irel
->r_offset
) & 0x0020)
739 bfd_put_16 (abfd
, (bfd_vma
) 0xa000, contents
+ irel
->r_offset
);
741 bfd_put_16 (abfd
, (bfd_vma
) 0xb000, contents
+ irel
->r_offset
);
745 /* When we calculated the symbol "value" we had an offset in the
746 DIR32's word in memory (we read and add it above). However,
747 the jsr we create does NOT have this offset encoded, so we
748 have to add it to the addend to preserve it. */
749 irel
->r_addend
+= bfd_get_32 (abfd
, contents
+ paddr
);
751 /* See if there is another R_SH_USES reloc referring to the same
753 for (irelscan
= internal_relocs
; irelscan
< irelend
; irelscan
++)
754 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_USES
755 && laddr
== irelscan
->r_offset
+ 4 + irelscan
->r_addend
)
757 if (irelscan
< irelend
)
759 /* Some other function call depends upon this register load,
760 and we have not yet converted that function call.
761 Indeed, we may never be able to convert it. There is
762 nothing else we can do at this point. */
766 /* Look for a R_SH_COUNT reloc on the location where the
767 function address is stored. Do this before deleting any
768 bytes, to avoid confusion about the address. */
769 for (irelcount
= internal_relocs
; irelcount
< irelend
; irelcount
++)
770 if (irelcount
->r_offset
== paddr
771 && ELF32_R_TYPE (irelcount
->r_info
) == (int) R_SH_COUNT
)
774 /* Delete the register load. */
775 if (! sh_elf_relax_delete_bytes (abfd
, sec
, laddr
, 2))
778 /* That will change things, so, just in case it permits some
779 other function call to come within range, we should relax
780 again. Note that this is not required, and it may be slow. */
783 /* Now check whether we got a COUNT reloc. */
784 if (irelcount
>= irelend
)
787 /* xgettext:c-format */
788 (_("%pB: %#" PRIx64
": warning: "
789 "could not find expected COUNT reloc"),
790 abfd
, (uint64_t) paddr
);
794 /* The number of uses is stored in the r_addend field. We've
796 if (irelcount
->r_addend
== 0)
798 /* xgettext:c-format */
799 _bfd_error_handler (_("%pB: %#" PRIx64
": warning: bad count"),
800 abfd
, (uint64_t) paddr
);
804 --irelcount
->r_addend
;
806 /* If there are no more uses, we can delete the address. Reload
807 the address from irelfn, in case it was changed by the
808 previous call to sh_elf_relax_delete_bytes. */
809 if (irelcount
->r_addend
== 0)
811 if (! sh_elf_relax_delete_bytes (abfd
, sec
, irelfn
->r_offset
, 4))
815 /* We've done all we can with that function call. */
818 /* Look for load and store instructions that we can align on four
820 if ((elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
) != EF_SH4
825 /* Get the section contents. */
826 if (contents
== NULL
)
828 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
829 contents
= elf_section_data (sec
)->this_hdr
.contents
;
832 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
837 if (! sh_elf_align_loads (abfd
, sec
, internal_relocs
, contents
,
843 elf_section_data (sec
)->relocs
= internal_relocs
;
844 elf_section_data (sec
)->this_hdr
.contents
= contents
;
845 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
850 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
852 if (! link_info
->keep_memory
)
856 /* Cache the symbols for elf_link_input_bfd. */
857 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
862 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
864 if (! link_info
->keep_memory
)
868 /* Cache the section contents for elf_link_input_bfd. */
869 elf_section_data (sec
)->this_hdr
.contents
= contents
;
873 if (internal_relocs
!= NULL
874 && elf_section_data (sec
)->relocs
!= internal_relocs
)
875 free (internal_relocs
);
881 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
884 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
886 if (internal_relocs
!= NULL
887 && elf_section_data (sec
)->relocs
!= internal_relocs
)
888 free (internal_relocs
);
893 /* Delete some bytes from a section while relaxing. FIXME: There is a
894 lot of duplication between this function and sh_relax_delete_bytes
898 sh_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
,
901 Elf_Internal_Shdr
*symtab_hdr
;
902 unsigned int sec_shndx
;
904 Elf_Internal_Rela
*irel
, *irelend
;
905 Elf_Internal_Rela
*irelalign
;
907 Elf_Internal_Sym
*isymbuf
, *isym
, *isymend
;
908 struct elf_link_hash_entry
**sym_hashes
;
909 struct elf_link_hash_entry
**end_hashes
;
910 unsigned int symcount
;
913 symtab_hdr
= &elf_symtab_hdr (abfd
);
914 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
916 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
918 contents
= elf_section_data (sec
)->this_hdr
.contents
;
920 /* The deletion must stop at the next ALIGN reloc for an alignment
921 power larger than the number of bytes we are deleting. */
926 irel
= elf_section_data (sec
)->relocs
;
927 irelend
= irel
+ sec
->reloc_count
;
928 for (; irel
< irelend
; irel
++)
930 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
931 && irel
->r_offset
> addr
932 && count
< (1 << irel
->r_addend
))
935 toaddr
= irel
->r_offset
;
940 /* Actually delete the bytes. */
941 memmove (contents
+ addr
, contents
+ addr
+ count
,
942 (size_t) (toaddr
- addr
- count
));
943 if (irelalign
== NULL
)
949 #define NOP_OPCODE (0x0009)
951 BFD_ASSERT ((count
& 1) == 0);
952 for (i
= 0; i
< count
; i
+= 2)
953 bfd_put_16 (abfd
, (bfd_vma
) NOP_OPCODE
, contents
+ toaddr
- count
+ i
);
956 /* Adjust all the relocs. */
957 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
959 bfd_vma nraddr
, stop
;
962 int off
, adjust
, oinsn
;
963 bfd_signed_vma voff
= 0;
964 bfd_boolean overflow
;
966 /* Get the new reloc address. */
967 nraddr
= irel
->r_offset
;
968 if ((irel
->r_offset
> addr
969 && irel
->r_offset
< toaddr
)
970 || (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_ALIGN
971 && irel
->r_offset
== toaddr
))
974 /* See if this reloc was for the bytes we have deleted, in which
975 case we no longer care about it. Don't delete relocs which
976 represent addresses, though. */
977 if (irel
->r_offset
>= addr
978 && irel
->r_offset
< addr
+ count
979 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_ALIGN
980 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
981 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_DATA
982 && ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_LABEL
)
983 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
986 /* If this is a PC relative reloc, see if the range it covers
987 includes the bytes we have deleted. */
988 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
997 start
= irel
->r_offset
;
998 insn
= bfd_get_16 (abfd
, contents
+ nraddr
);
1002 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1005 start
= stop
= addr
;
1009 /* If this reloc is against a symbol defined in this
1010 section, and the symbol will not be adjusted below, we
1011 must check the addend to see it will put the value in
1012 range to be adjusted, and hence must be changed. */
1013 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1015 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1016 if (isym
->st_shndx
== sec_shndx
1017 && (isym
->st_value
<= addr
1018 || isym
->st_value
>= toaddr
))
1022 if (get_howto_table (abfd
)[R_SH_DIR32
].partial_inplace
)
1024 val
= bfd_get_32 (abfd
, contents
+ nraddr
);
1025 val
+= isym
->st_value
;
1026 if (val
> addr
&& val
< toaddr
)
1027 bfd_put_32 (abfd
, val
- count
, contents
+ nraddr
);
1031 val
= isym
->st_value
+ irel
->r_addend
;
1032 if (val
> addr
&& val
< toaddr
)
1033 irel
->r_addend
-= count
;
1037 start
= stop
= addr
;
1044 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1051 /* This has been made by previous relaxation. Since the
1052 relocation will be against an external symbol, the
1053 final relocation will just do the right thing. */
1054 start
= stop
= addr
;
1060 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ 4 + off
* 2);
1062 /* The addend will be against the section symbol, thus
1063 for adjusting the addend, the relevant start is the
1064 start of the section.
1065 N.B. If we want to abandon in-place changes here and
1066 test directly using symbol + addend, we have to take into
1067 account that the addend has already been adjusted by -4. */
1068 if (stop
> addr
&& stop
< toaddr
)
1069 irel
->r_addend
-= count
;
1075 stop
= start
+ 4 + off
* 2;
1080 stop
= (start
& ~(bfd_vma
) 3) + 4 + off
* 4;
1086 /* These relocs types represent
1088 The r_addend field holds the difference between the reloc
1089 address and L1. That is the start of the reloc, and
1090 adding in the contents gives us the top. We must adjust
1091 both the r_offset field and the section contents.
1092 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1093 and the elf bfd r_offset is called r_vaddr. */
1095 stop
= irel
->r_offset
;
1096 start
= (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irel
->r_addend
);
1100 && (stop
<= addr
|| stop
>= toaddr
))
1101 irel
->r_addend
+= count
;
1102 else if (stop
> addr
1104 && (start
<= addr
|| start
>= toaddr
))
1105 irel
->r_addend
-= count
;
1107 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH16
)
1108 voff
= bfd_get_signed_16 (abfd
, contents
+ nraddr
);
1109 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_SWITCH8
)
1110 voff
= bfd_get_8 (abfd
, contents
+ nraddr
);
1112 voff
= bfd_get_signed_32 (abfd
, contents
+ nraddr
);
1113 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1118 start
= irel
->r_offset
;
1119 stop
= (bfd_vma
) ((bfd_signed_vma
) start
1120 + (long) irel
->r_addend
1127 && (stop
<= addr
|| stop
>= toaddr
))
1129 else if (stop
> addr
1131 && (start
<= addr
|| start
>= toaddr
))
1140 switch ((enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
))
1149 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1151 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1156 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1158 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1162 BFD_ASSERT (adjust
== count
|| count
>= 4);
1167 if ((irel
->r_offset
& 3) == 0)
1170 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1172 bfd_put_16 (abfd
, (bfd_vma
) insn
, contents
+ nraddr
);
1177 if (voff
< 0 || voff
>= 0xff)
1179 bfd_put_8 (abfd
, voff
, contents
+ nraddr
);
1184 if (voff
< - 0x8000 || voff
>= 0x8000)
1186 bfd_put_signed_16 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1191 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
, contents
+ nraddr
);
1195 irel
->r_addend
+= adjust
;
1202 /* xgettext:c-format */
1203 (_("%pB: %#" PRIx64
": fatal: reloc overflow while relaxing"),
1204 abfd
, (uint64_t) irel
->r_offset
);
1205 bfd_set_error (bfd_error_bad_value
);
1210 irel
->r_offset
= nraddr
;
1213 /* Look through all the other sections. If there contain any IMM32
1214 relocs against internal symbols which we are not going to adjust
1215 below, we may need to adjust the addends. */
1216 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1218 Elf_Internal_Rela
*internal_relocs
;
1219 Elf_Internal_Rela
*irelscan
, *irelscanend
;
1220 bfd_byte
*ocontents
;
1223 || (o
->flags
& SEC_RELOC
) == 0
1224 || o
->reloc_count
== 0)
1227 /* We always cache the relocs. Perhaps, if info->keep_memory is
1228 FALSE, we should free them, if we are permitted to, when we
1229 leave sh_coff_relax_section. */
1230 internal_relocs
= (_bfd_elf_link_read_relocs
1231 (abfd
, o
, NULL
, (Elf_Internal_Rela
*) NULL
, TRUE
));
1232 if (internal_relocs
== NULL
)
1236 irelscanend
= internal_relocs
+ o
->reloc_count
;
1237 for (irelscan
= internal_relocs
; irelscan
< irelscanend
; irelscan
++)
1239 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1240 if (ELF32_R_TYPE (irelscan
->r_info
) == (int) R_SH_SWITCH32
)
1242 bfd_vma start
, stop
;
1243 bfd_signed_vma voff
;
1245 if (ocontents
== NULL
)
1247 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1248 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1251 /* We always cache the section contents.
1252 Perhaps, if info->keep_memory is FALSE, we
1253 should free them, if we are permitted to,
1254 when we leave sh_coff_relax_section. */
1255 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1257 if (ocontents
!= NULL
)
1262 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1266 stop
= irelscan
->r_offset
;
1268 = (bfd_vma
) ((bfd_signed_vma
) stop
- (long) irelscan
->r_addend
);
1270 /* STOP is in a different section, so it won't change. */
1271 if (start
> addr
&& start
< toaddr
)
1272 irelscan
->r_addend
+= count
;
1274 voff
= bfd_get_signed_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1275 stop
= (bfd_vma
) ((bfd_signed_vma
) start
+ voff
);
1279 && (stop
<= addr
|| stop
>= toaddr
))
1280 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
+ count
,
1281 ocontents
+ irelscan
->r_offset
);
1282 else if (stop
> addr
1284 && (start
<= addr
|| start
>= toaddr
))
1285 bfd_put_signed_32 (abfd
, (bfd_vma
) voff
- count
,
1286 ocontents
+ irelscan
->r_offset
);
1289 if (ELF32_R_TYPE (irelscan
->r_info
) != (int) R_SH_DIR32
)
1292 if (ELF32_R_SYM (irelscan
->r_info
) >= symtab_hdr
->sh_info
)
1296 isym
= isymbuf
+ ELF32_R_SYM (irelscan
->r_info
);
1297 if (isym
->st_shndx
== sec_shndx
1298 && (isym
->st_value
<= addr
1299 || isym
->st_value
>= toaddr
))
1303 if (ocontents
== NULL
)
1305 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
1306 ocontents
= elf_section_data (o
)->this_hdr
.contents
;
1309 /* We always cache the section contents.
1310 Perhaps, if info->keep_memory is FALSE, we
1311 should free them, if we are permitted to,
1312 when we leave sh_coff_relax_section. */
1313 if (!bfd_malloc_and_get_section (abfd
, o
, &ocontents
))
1315 if (ocontents
!= NULL
)
1320 elf_section_data (o
)->this_hdr
.contents
= ocontents
;
1324 val
= bfd_get_32 (abfd
, ocontents
+ irelscan
->r_offset
);
1325 val
+= isym
->st_value
;
1326 if (val
> addr
&& val
< toaddr
)
1327 bfd_put_32 (abfd
, val
- count
,
1328 ocontents
+ irelscan
->r_offset
);
1333 /* Adjust the local symbols defined in this section. */
1334 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1335 for (isym
= isymbuf
; isym
< isymend
; isym
++)
1337 if (isym
->st_shndx
== sec_shndx
1338 && isym
->st_value
> addr
1339 && isym
->st_value
< toaddr
)
1340 isym
->st_value
-= count
;
1343 /* Now adjust the global symbols defined in this section. */
1344 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1345 - symtab_hdr
->sh_info
);
1346 sym_hashes
= elf_sym_hashes (abfd
);
1347 end_hashes
= sym_hashes
+ symcount
;
1348 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1350 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1351 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1352 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1353 && sym_hash
->root
.u
.def
.section
== sec
1354 && sym_hash
->root
.u
.def
.value
> addr
1355 && sym_hash
->root
.u
.def
.value
< toaddr
)
1357 sym_hash
->root
.u
.def
.value
-= count
;
1361 /* See if we can move the ALIGN reloc forward. We have adjusted
1362 r_offset for it already. */
1363 if (irelalign
!= NULL
)
1365 bfd_vma alignto
, alignaddr
;
1367 alignto
= BFD_ALIGN (toaddr
, 1 << irelalign
->r_addend
);
1368 alignaddr
= BFD_ALIGN (irelalign
->r_offset
,
1369 1 << irelalign
->r_addend
);
1370 if (alignto
!= alignaddr
)
1372 /* Tail recursion. */
1373 return sh_elf_relax_delete_bytes (abfd
, sec
, alignaddr
,
1374 (int) (alignto
- alignaddr
));
1381 /* Look for loads and stores which we can align to four byte
1382 boundaries. This is like sh_align_loads in coff-sh.c. */
1385 sh_elf_align_loads (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
,
1386 Elf_Internal_Rela
*internal_relocs
,
1387 bfd_byte
*contents ATTRIBUTE_UNUSED
,
1388 bfd_boolean
*pswapped
)
1390 Elf_Internal_Rela
*irel
, *irelend
;
1391 bfd_vma
*labels
= NULL
;
1392 bfd_vma
*label
, *label_end
;
1397 irelend
= internal_relocs
+ sec
->reloc_count
;
1399 /* Get all the addresses with labels on them. */
1400 amt
= sec
->reloc_count
;
1401 amt
*= sizeof (bfd_vma
);
1402 labels
= (bfd_vma
*) bfd_malloc (amt
);
1406 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1408 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_LABEL
)
1410 *label_end
= irel
->r_offset
;
1415 /* Note that the assembler currently always outputs relocs in
1416 address order. If that ever changes, this code will need to sort
1417 the label values and the relocs. */
1421 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1423 bfd_vma start
, stop
;
1425 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_SH_CODE
)
1428 start
= irel
->r_offset
;
1430 for (irel
++; irel
< irelend
; irel
++)
1431 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_SH_DATA
)
1434 stop
= irel
->r_offset
;
1438 if (! _bfd_sh_align_load_span (abfd
, sec
, contents
, sh_elf_swap_insns
,
1439 internal_relocs
, &label
,
1440 label_end
, start
, stop
, pswapped
))
1455 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1458 sh_elf_swap_insns (bfd
*abfd
, asection
*sec
, void *relocs
,
1459 bfd_byte
*contents
, bfd_vma addr
)
1461 Elf_Internal_Rela
*internal_relocs
= (Elf_Internal_Rela
*) relocs
;
1462 unsigned short i1
, i2
;
1463 Elf_Internal_Rela
*irel
, *irelend
;
1465 /* Swap the instructions themselves. */
1466 i1
= bfd_get_16 (abfd
, contents
+ addr
);
1467 i2
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
1468 bfd_put_16 (abfd
, (bfd_vma
) i2
, contents
+ addr
);
1469 bfd_put_16 (abfd
, (bfd_vma
) i1
, contents
+ addr
+ 2);
1471 /* Adjust all reloc addresses. */
1472 irelend
= internal_relocs
+ sec
->reloc_count
;
1473 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1475 enum elf_sh_reloc_type type
;
1478 /* There are a few special types of relocs that we don't want to
1479 adjust. These relocs do not apply to the instruction itself,
1480 but are only associated with the address. */
1481 type
= (enum elf_sh_reloc_type
) ELF32_R_TYPE (irel
->r_info
);
1482 if (type
== R_SH_ALIGN
1483 || type
== R_SH_CODE
1484 || type
== R_SH_DATA
1485 || type
== R_SH_LABEL
)
1488 /* If an R_SH_USES reloc points to one of the addresses being
1489 swapped, we must adjust it. It would be incorrect to do this
1490 for a jump, though, since we want to execute both
1491 instructions after the jump. (We have avoided swapping
1492 around a label, so the jump will not wind up executing an
1493 instruction it shouldn't). */
1494 if (type
== R_SH_USES
)
1498 off
= irel
->r_offset
+ 4 + irel
->r_addend
;
1500 irel
->r_offset
+= 2;
1501 else if (off
== addr
+ 2)
1502 irel
->r_offset
-= 2;
1505 if (irel
->r_offset
== addr
)
1507 irel
->r_offset
+= 2;
1510 else if (irel
->r_offset
== addr
+ 2)
1512 irel
->r_offset
-= 2;
1521 unsigned short insn
, oinsn
;
1522 bfd_boolean overflow
;
1524 loc
= contents
+ irel
->r_offset
;
1533 insn
= bfd_get_16 (abfd
, loc
);
1536 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1538 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1542 insn
= bfd_get_16 (abfd
, loc
);
1545 if ((oinsn
& 0xf000) != (insn
& 0xf000))
1547 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1551 /* This reloc ignores the least significant 3 bits of
1552 the program counter before adding in the offset.
1553 This means that if ADDR is at an even address, the
1554 swap will not affect the offset. If ADDR is an at an
1555 odd address, then the instruction will be crossing a
1556 four byte boundary, and must be adjusted. */
1557 if ((addr
& 3) != 0)
1559 insn
= bfd_get_16 (abfd
, loc
);
1562 if ((oinsn
& 0xff00) != (insn
& 0xff00))
1564 bfd_put_16 (abfd
, (bfd_vma
) insn
, loc
);
1573 /* xgettext:c-format */
1574 (_("%pB: %#" PRIx64
": fatal: reloc overflow while relaxing"),
1575 abfd
, (uint64_t) irel
->r_offset
);
1576 bfd_set_error (bfd_error_bad_value
);
1584 #endif /* defined SH64_ELF */
1586 /* Describes one of the various PLT styles. */
1588 struct elf_sh_plt_info
1590 /* The template for the first PLT entry, or NULL if there is no special
1592 const bfd_byte
*plt0_entry
;
1594 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1595 bfd_vma plt0_entry_size
;
1597 /* Index I is the offset into PLT0_ENTRY of a pointer to
1598 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1599 if there is no such pointer. */
1600 bfd_vma plt0_got_fields
[3];
1602 /* The template for a symbol's PLT entry. */
1603 const bfd_byte
*symbol_entry
;
1605 /* The size of SYMBOL_ENTRY in bytes. */
1606 bfd_vma symbol_entry_size
;
1608 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1609 on all targets. The comments by each member indicate the value
1610 that the field must hold. */
1612 bfd_vma got_entry
; /* the address of the symbol's .got.plt entry */
1613 bfd_vma plt
; /* .plt (or a branch to .plt on VxWorks) */
1614 bfd_vma reloc_offset
; /* the offset of the symbol's JMP_SLOT reloc */
1615 bfd_boolean got20
; /* TRUE if got_entry points to a movi20
1616 instruction (instead of a constant pool
1620 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1621 bfd_vma symbol_resolve_offset
;
1623 /* A different PLT layout which can be used for the first
1624 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1626 const struct elf_sh_plt_info
*short_plt
;
1629 #ifdef INCLUDE_SHMEDIA
1631 /* The size in bytes of an entry in the procedure linkage table. */
1633 #define ELF_PLT_ENTRY_SIZE 64
1635 /* First entry in an absolute procedure linkage table look like this. */
1637 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1639 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1640 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1641 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1642 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1643 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1644 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1645 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1646 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1647 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1648 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1649 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1650 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1651 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1652 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1653 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1654 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1657 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1659 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1660 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1661 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1662 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1663 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1664 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1665 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1666 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1667 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1668 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1669 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1670 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1671 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1672 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1673 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1674 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1677 /* Sebsequent entries in an absolute procedure linkage table look like
1680 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1682 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1683 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1684 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1685 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1686 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1687 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1688 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1689 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1690 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1691 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1692 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1693 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1694 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1695 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1696 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1697 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1700 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1702 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1703 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1704 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1705 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1706 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1707 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1708 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1709 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1710 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1711 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1712 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1713 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1714 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1715 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1716 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1717 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1720 /* Entries in a PIC procedure linkage table look like this. */
1722 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1724 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1725 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1726 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1727 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1728 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1729 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1730 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1731 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1732 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1733 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1734 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1735 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1736 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1737 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1738 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1739 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1742 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1744 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1745 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1746 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1747 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1748 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1749 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1750 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1751 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1752 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1753 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1754 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1755 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1756 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1757 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1758 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1759 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1762 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1765 /* Big-endian non-PIC. */
1766 elf_sh_plt0_entry_be
,
1768 { 0, MINUS_ONE
, MINUS_ONE
},
1769 elf_sh_plt_entry_be
,
1771 { 0, 32, 48, FALSE
},
1772 33, /* includes ISA encoding */
1776 /* Little-endian non-PIC. */
1777 elf_sh_plt0_entry_le
,
1779 { 0, MINUS_ONE
, MINUS_ONE
},
1780 elf_sh_plt_entry_le
,
1782 { 0, 32, 48, FALSE
},
1783 33, /* includes ISA encoding */
1789 /* Big-endian PIC. */
1790 elf_sh_plt0_entry_be
,
1792 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1793 elf_sh_pic_plt_entry_be
,
1795 { 0, MINUS_ONE
, 52, FALSE
},
1796 33, /* includes ISA encoding */
1800 /* Little-endian PIC. */
1801 elf_sh_plt0_entry_le
,
1803 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1804 elf_sh_pic_plt_entry_le
,
1806 { 0, MINUS_ONE
, 52, FALSE
},
1807 33, /* includes ISA encoding */
1813 /* Return offset of the linker in PLT0 entry. */
1814 #define elf_sh_plt0_gotplt_offset(info) 0
1816 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1817 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1820 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1823 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p
,
1824 unsigned long value
, bfd_byte
*addr
)
1827 bfd_put_32 (output_bfd
,
1828 bfd_get_32 (output_bfd
, addr
)
1829 | ((value
>> 6) & 0x3fffc00),
1831 bfd_put_32 (output_bfd
,
1832 bfd_get_32 (output_bfd
, addr
+ 4)
1833 | ((value
<< 10) & 0x3fffc00),
1837 /* Return the type of PLT associated with ABFD. PIC_P is true if
1838 the object is position-independent. */
1840 static const struct elf_sh_plt_info
*
1841 get_plt_info (bfd
*abfd ATTRIBUTE_UNUSED
, bfd_boolean pic_p
)
1843 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
1846 /* The size in bytes of an entry in the procedure linkage table. */
1848 #define ELF_PLT_ENTRY_SIZE 28
1850 /* First entry in an absolute procedure linkage table look like this. */
1852 /* Note - this code has been "optimised" not to use r2. r2 is used by
1853 GCC to return the address of large structures, so it should not be
1854 corrupted here. This does mean however, that this PLT does not conform
1855 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1856 and r2 contains the GOT id. This version stores the GOT id in r0 and
1857 ignores the type. Loaders can easily detect this difference however,
1858 since the type will always be 0 or 8, and the GOT ids will always be
1859 greater than or equal to 12. */
1860 static const bfd_byte elf_sh_plt0_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1862 0xd0, 0x05, /* mov.l 2f,r0 */
1863 0x60, 0x02, /* mov.l @r0,r0 */
1864 0x2f, 0x06, /* mov.l r0,@-r15 */
1865 0xd0, 0x03, /* mov.l 1f,r0 */
1866 0x60, 0x02, /* mov.l @r0,r0 */
1867 0x40, 0x2b, /* jmp @r0 */
1868 0x60, 0xf6, /* mov.l @r15+,r0 */
1869 0x00, 0x09, /* nop */
1870 0x00, 0x09, /* nop */
1871 0x00, 0x09, /* nop */
1872 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1873 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1876 static const bfd_byte elf_sh_plt0_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1878 0x05, 0xd0, /* mov.l 2f,r0 */
1879 0x02, 0x60, /* mov.l @r0,r0 */
1880 0x06, 0x2f, /* mov.l r0,@-r15 */
1881 0x03, 0xd0, /* mov.l 1f,r0 */
1882 0x02, 0x60, /* mov.l @r0,r0 */
1883 0x2b, 0x40, /* jmp @r0 */
1884 0xf6, 0x60, /* mov.l @r15+,r0 */
1885 0x09, 0x00, /* nop */
1886 0x09, 0x00, /* nop */
1887 0x09, 0x00, /* nop */
1888 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1889 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1892 /* Sebsequent entries in an absolute procedure linkage table look like
1895 static const bfd_byte elf_sh_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1897 0xd0, 0x04, /* mov.l 1f,r0 */
1898 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1899 0xd1, 0x02, /* mov.l 0f,r1 */
1900 0x40, 0x2b, /* jmp @r0 */
1901 0x60, 0x13, /* mov r1,r0 */
1902 0xd1, 0x03, /* mov.l 2f,r1 */
1903 0x40, 0x2b, /* jmp @r0 */
1904 0x00, 0x09, /* nop */
1905 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1906 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1907 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1910 static const bfd_byte elf_sh_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1912 0x04, 0xd0, /* mov.l 1f,r0 */
1913 0x02, 0x60, /* mov.l @r0,r0 */
1914 0x02, 0xd1, /* mov.l 0f,r1 */
1915 0x2b, 0x40, /* jmp @r0 */
1916 0x13, 0x60, /* mov r1,r0 */
1917 0x03, 0xd1, /* mov.l 2f,r1 */
1918 0x2b, 0x40, /* jmp @r0 */
1919 0x09, 0x00, /* nop */
1920 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1921 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1922 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1925 /* Entries in a PIC procedure linkage table look like this. */
1927 static const bfd_byte elf_sh_pic_plt_entry_be
[ELF_PLT_ENTRY_SIZE
] =
1929 0xd0, 0x04, /* mov.l 1f,r0 */
1930 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1931 0x40, 0x2b, /* jmp @r0 */
1932 0x00, 0x09, /* nop */
1933 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1934 0xd1, 0x03, /* mov.l 2f,r1 */
1935 0x40, 0x2b, /* jmp @r0 */
1936 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1937 0x00, 0x09, /* nop */
1938 0x00, 0x09, /* nop */
1939 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1940 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1943 static const bfd_byte elf_sh_pic_plt_entry_le
[ELF_PLT_ENTRY_SIZE
] =
1945 0x04, 0xd0, /* mov.l 1f,r0 */
1946 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1947 0x2b, 0x40, /* jmp @r0 */
1948 0x09, 0x00, /* nop */
1949 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1950 0x03, 0xd1, /* mov.l 2f,r1 */
1951 0x2b, 0x40, /* jmp @r0 */
1952 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1953 0x09, 0x00, /* nop */
1954 0x09, 0x00, /* nop */
1955 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1956 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1959 static const struct elf_sh_plt_info elf_sh_plts
[2][2] = {
1962 /* Big-endian non-PIC. */
1963 elf_sh_plt0_entry_be
,
1965 { MINUS_ONE
, 24, 20 },
1966 elf_sh_plt_entry_be
,
1968 { 20, 16, 24, FALSE
},
1973 /* Little-endian non-PIC. */
1974 elf_sh_plt0_entry_le
,
1976 { MINUS_ONE
, 24, 20 },
1977 elf_sh_plt_entry_le
,
1979 { 20, 16, 24, FALSE
},
1986 /* Big-endian PIC. */
1987 elf_sh_plt0_entry_be
,
1989 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
1990 elf_sh_pic_plt_entry_be
,
1992 { 20, MINUS_ONE
, 24, FALSE
},
1997 /* Little-endian PIC. */
1998 elf_sh_plt0_entry_le
,
2000 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2001 elf_sh_pic_plt_entry_le
,
2003 { 20, MINUS_ONE
, 24, FALSE
},
2010 #define VXWORKS_PLT_HEADER_SIZE 12
2011 #define VXWORKS_PLT_ENTRY_SIZE 24
2013 static const bfd_byte vxworks_sh_plt0_entry_be
[VXWORKS_PLT_HEADER_SIZE
] =
2015 0xd1, 0x01, /* mov.l @(8,pc),r1 */
2016 0x61, 0x12, /* mov.l @r1,r1 */
2017 0x41, 0x2b, /* jmp @r1 */
2018 0x00, 0x09, /* nop */
2019 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2022 static const bfd_byte vxworks_sh_plt0_entry_le
[VXWORKS_PLT_HEADER_SIZE
] =
2024 0x01, 0xd1, /* mov.l @(8,pc),r1 */
2025 0x12, 0x61, /* mov.l @r1,r1 */
2026 0x2b, 0x41, /* jmp @r1 */
2027 0x09, 0x00, /* nop */
2028 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2031 static const bfd_byte vxworks_sh_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
2033 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2034 0x60, 0x02, /* mov.l @r0,r0 */
2035 0x40, 0x2b, /* jmp @r0 */
2036 0x00, 0x09, /* nop */
2037 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2038 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2039 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
2040 0x00, 0x09, /* nop */
2041 0x00, 0x09, /* nop */
2042 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2045 static const bfd_byte vxworks_sh_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
2047 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2048 0x02, 0x60, /* mov.l @r0,r0 */
2049 0x2b, 0x40, /* jmp @r0 */
2050 0x09, 0x00, /* nop */
2051 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2052 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2053 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
2054 0x09, 0x00, /* nop */
2055 0x09, 0x00, /* nop */
2056 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2059 static const bfd_byte vxworks_sh_pic_plt_entry_be
[VXWORKS_PLT_ENTRY_SIZE
] =
2061 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2062 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2063 0x40, 0x2b, /* jmp @r0 */
2064 0x00, 0x09, /* nop */
2065 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2066 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2067 0x51, 0xc2, /* mov.l @(8,r12),r1 */
2068 0x41, 0x2b, /* jmp @r1 */
2069 0x00, 0x09, /* nop */
2070 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2073 static const bfd_byte vxworks_sh_pic_plt_entry_le
[VXWORKS_PLT_ENTRY_SIZE
] =
2075 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2076 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2077 0x2b, 0x40, /* jmp @r0 */
2078 0x09, 0x00, /* nop */
2079 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2080 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2081 0xc2, 0x51, /* mov.l @(8,r12),r1 */
2082 0x2b, 0x41, /* jmp @r1 */
2083 0x09, 0x00, /* nop */
2084 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2087 static const struct elf_sh_plt_info vxworks_sh_plts
[2][2] = {
2090 /* Big-endian non-PIC. */
2091 vxworks_sh_plt0_entry_be
,
2092 VXWORKS_PLT_HEADER_SIZE
,
2093 { MINUS_ONE
, MINUS_ONE
, 8 },
2094 vxworks_sh_plt_entry_be
,
2095 VXWORKS_PLT_ENTRY_SIZE
,
2096 { 8, 14, 20, FALSE
},
2101 /* Little-endian non-PIC. */
2102 vxworks_sh_plt0_entry_le
,
2103 VXWORKS_PLT_HEADER_SIZE
,
2104 { MINUS_ONE
, MINUS_ONE
, 8 },
2105 vxworks_sh_plt_entry_le
,
2106 VXWORKS_PLT_ENTRY_SIZE
,
2107 { 8, 14, 20, FALSE
},
2114 /* Big-endian PIC. */
2117 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2118 vxworks_sh_pic_plt_entry_be
,
2119 VXWORKS_PLT_ENTRY_SIZE
,
2120 { 8, MINUS_ONE
, 20, FALSE
},
2125 /* Little-endian PIC. */
2128 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2129 vxworks_sh_pic_plt_entry_le
,
2130 VXWORKS_PLT_ENTRY_SIZE
,
2131 { 8, MINUS_ONE
, 20, FALSE
},
2138 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
2139 binding are to omit the lazy binding stub when linking with -z now
2140 and to move lazy binding stubs into a separate region for better
2143 #define FDPIC_PLT_ENTRY_SIZE 28
2144 #define FDPIC_PLT_LAZY_OFFSET 20
2146 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
2147 duplicated if it is out of range, or which can be inlined. So
2148 right now it is always inlined, which wastes a word per stub. It
2149 might be easier to handle the duplication if we put the lazy
2150 stubs separately. */
2152 static const bfd_byte fdpic_sh_plt_entry_be
[FDPIC_PLT_ENTRY_SIZE
] =
2154 0xd0, 0x02, /* mov.l @(12,pc),r0 */
2155 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2156 0x70, 0x04, /* add #4, r0 */
2157 0x41, 0x2b, /* jmp @r1 */
2158 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2159 0x00, 0x09, /* nop */
2160 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2161 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2162 0x60, 0xc2, /* mov.l @r12,r0 */
2163 0x40, 0x2b, /* jmp @r0 */
2164 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2165 0x00, 0x09, /* nop */
2168 static const bfd_byte fdpic_sh_plt_entry_le
[FDPIC_PLT_ENTRY_SIZE
] =
2170 0x02, 0xd0, /* mov.l @(12,pc),r0 */
2171 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2172 0x04, 0x70, /* add #4, r0 */
2173 0x2b, 0x41, /* jmp @r1 */
2174 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2175 0x09, 0x00, /* nop */
2176 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2177 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2178 0xc2, 0x60, /* mov.l @r12,r0 */
2179 0x2b, 0x40, /* jmp @r0 */
2180 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2181 0x09, 0x00, /* nop */
2184 static const struct elf_sh_plt_info fdpic_sh_plts
[2] = {
2186 /* Big-endian PIC. */
2189 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2190 fdpic_sh_plt_entry_be
,
2191 FDPIC_PLT_ENTRY_SIZE
,
2192 { 12, MINUS_ONE
, 16, FALSE
},
2193 FDPIC_PLT_LAZY_OFFSET
,
2197 /* Little-endian PIC. */
2200 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2201 fdpic_sh_plt_entry_le
,
2202 FDPIC_PLT_ENTRY_SIZE
,
2203 { 12, MINUS_ONE
, 16, FALSE
},
2204 FDPIC_PLT_LAZY_OFFSET
,
2209 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
2210 entries for the first 64K slots. We use the normal FDPIC PLT entry
2211 past that point; we could also use movi20s, which might be faster,
2212 but would not be any smaller. */
2214 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
2215 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
2217 static const bfd_byte fdpic_sh2a_plt_entry_be
[FDPIC_SH2A_PLT_ENTRY_SIZE
] =
2219 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2220 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2221 0x70, 0x04, /* add #4, r0 */
2222 0x41, 0x2b, /* jmp @r1 */
2223 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2224 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2225 0x60, 0xc2, /* mov.l @r12,r0 */
2226 0x40, 0x2b, /* jmp @r0 */
2227 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2228 0x00, 0x09, /* nop */
2231 static const bfd_byte fdpic_sh2a_plt_entry_le
[FDPIC_SH2A_PLT_ENTRY_SIZE
] =
2233 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2234 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2235 0x04, 0x70, /* add #4, r0 */
2236 0x2b, 0x41, /* jmp @r1 */
2237 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2238 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2239 0xc2, 0x60, /* mov.l @r12,r0 */
2240 0x2b, 0x40, /* jmp @r0 */
2241 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2242 0x09, 0x00, /* nop */
2245 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be
= {
2246 /* Big-endian FDPIC, max index 64K. */
2249 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2250 fdpic_sh2a_plt_entry_be
,
2251 FDPIC_SH2A_PLT_ENTRY_SIZE
,
2252 { 0, MINUS_ONE
, 12, TRUE
},
2253 FDPIC_SH2A_PLT_LAZY_OFFSET
,
2257 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le
= {
2258 /* Little-endian FDPIC, max index 64K. */
2261 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2262 fdpic_sh2a_plt_entry_le
,
2263 FDPIC_SH2A_PLT_ENTRY_SIZE
,
2264 { 0, MINUS_ONE
, 12, TRUE
},
2265 FDPIC_SH2A_PLT_LAZY_OFFSET
,
2269 static const struct elf_sh_plt_info fdpic_sh2a_plts
[2] = {
2271 /* Big-endian PIC. */
2274 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2275 fdpic_sh_plt_entry_be
,
2276 FDPIC_PLT_ENTRY_SIZE
,
2277 { 12, MINUS_ONE
, 16, FALSE
},
2278 FDPIC_PLT_LAZY_OFFSET
,
2279 &fdpic_sh2a_short_plt_be
2282 /* Little-endian PIC. */
2285 { MINUS_ONE
, MINUS_ONE
, MINUS_ONE
},
2286 fdpic_sh_plt_entry_le
,
2287 FDPIC_PLT_ENTRY_SIZE
,
2288 { 12, MINUS_ONE
, 16, FALSE
},
2289 FDPIC_PLT_LAZY_OFFSET
,
2290 &fdpic_sh2a_short_plt_le
2294 /* Return the type of PLT associated with ABFD. PIC_P is true if
2295 the object is position-independent. */
2297 static const struct elf_sh_plt_info
*
2298 get_plt_info (bfd
*abfd
, bfd_boolean pic_p
)
2300 if (fdpic_object_p (abfd
))
2302 /* If any input file requires SH2A we can use a shorter PLT
2304 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd
)) & arch_sh2a_base
)
2305 return &fdpic_sh2a_plts
[!bfd_big_endian (abfd
)];
2307 return &fdpic_sh_plts
[!bfd_big_endian (abfd
)];
2309 if (vxworks_object_p (abfd
))
2310 return &vxworks_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2311 return &elf_sh_plts
[pic_p
][!bfd_big_endian (abfd
)];
2314 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2315 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2319 install_plt_field (bfd
*output_bfd
, bfd_boolean code_p ATTRIBUTE_UNUSED
,
2320 unsigned long value
, bfd_byte
*addr
)
2322 bfd_put_32 (output_bfd
, value
, addr
);
2326 /* The number of PLT entries which can use a shorter PLT, if any.
2327 Currently always 64K, since only SH-2A FDPIC uses this; a
2328 20-bit movi20 can address that many function descriptors below
2329 _GLOBAL_OFFSET_TABLE_. */
2330 #define MAX_SHORT_PLT 65536
2332 /* Return the index of the PLT entry at byte offset OFFSET. */
2335 get_plt_index (const struct elf_sh_plt_info
*info
, bfd_vma offset
)
2337 bfd_vma plt_index
= 0;
2339 offset
-= info
->plt0_entry_size
;
2340 if (info
->short_plt
!= NULL
)
2342 if (offset
> MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
)
2344 plt_index
= MAX_SHORT_PLT
;
2345 offset
-= MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
;
2348 info
= info
->short_plt
;
2350 return plt_index
+ offset
/ info
->symbol_entry_size
;
2353 /* Do the inverse operation. */
2356 get_plt_offset (const struct elf_sh_plt_info
*info
, bfd_vma plt_index
)
2360 if (info
->short_plt
!= NULL
)
2362 if (plt_index
> MAX_SHORT_PLT
)
2364 offset
= MAX_SHORT_PLT
* info
->short_plt
->symbol_entry_size
;
2365 plt_index
-= MAX_SHORT_PLT
;
2368 info
= info
->short_plt
;
2370 return (offset
+ info
->plt0_entry_size
2371 + (plt_index
* info
->symbol_entry_size
));
2376 bfd_signed_vma refcount
;
2380 /* sh ELF linker hash entry. */
2382 struct elf_sh_link_hash_entry
2384 struct elf_link_hash_entry root
;
2386 #ifdef INCLUDE_SHMEDIA
2389 bfd_signed_vma refcount
;
2394 /* Track dynamic relocs copied for this symbol. */
2395 struct elf_dyn_relocs
*dyn_relocs
;
2397 bfd_signed_vma gotplt_refcount
;
2399 /* A local function descriptor, for FDPIC. The refcount counts
2400 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2401 relocations; the PLT and GOT entry are accounted
2402 for separately. After adjust_dynamic_symbol, the offset is
2403 MINUS_ONE if there is no local descriptor (dynamic linker
2404 managed and no PLT entry, or undefined weak non-dynamic).
2405 During check_relocs we do not yet know whether the local
2406 descriptor will be canonical. */
2407 union gotref funcdesc
;
2409 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2410 and thus require fixups or relocations. */
2411 bfd_signed_vma abs_funcdesc_refcount
;
2414 GOT_UNKNOWN
= 0, GOT_NORMAL
, GOT_TLS_GD
, GOT_TLS_IE
, GOT_FUNCDESC
2418 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2420 struct sh_elf_obj_tdata
2422 struct elf_obj_tdata root
;
2424 /* got_type for each local got entry. */
2425 char *local_got_type
;
2427 /* Function descriptor refcount and offset for each local symbol. */
2428 union gotref
*local_funcdesc
;
2431 #define sh_elf_tdata(abfd) \
2432 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2434 #define sh_elf_local_got_type(abfd) \
2435 (sh_elf_tdata (abfd)->local_got_type)
2437 #define sh_elf_local_funcdesc(abfd) \
2438 (sh_elf_tdata (abfd)->local_funcdesc)
2440 #define is_sh_elf(bfd) \
2441 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2442 && elf_tdata (bfd) != NULL \
2443 && elf_object_id (bfd) == SH_ELF_DATA)
2445 /* Override the generic function because we need to store sh_elf_obj_tdata
2446 as the specific tdata. */
2449 sh_elf_mkobject (bfd
*abfd
)
2451 return bfd_elf_allocate_object (abfd
, sizeof (struct sh_elf_obj_tdata
),
2455 /* sh ELF linker hash table. */
2457 struct elf_sh_link_hash_table
2459 struct elf_link_hash_table root
;
2461 /* Short-cuts to get to dynamic linker sections. */
2464 asection
*sfuncdesc
;
2465 asection
*srelfuncdesc
;
2468 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2471 /* Small local sym cache. */
2472 struct sym_cache sym_cache
;
2474 /* A counter or offset to track a TLS got entry. */
2477 bfd_signed_vma refcount
;
2481 /* The type of PLT to use. */
2482 const struct elf_sh_plt_info
*plt_info
;
2484 /* True if the target system is VxWorks. */
2485 bfd_boolean vxworks_p
;
2487 /* True if the target system uses FDPIC. */
2488 bfd_boolean fdpic_p
;
2491 /* Traverse an sh ELF linker hash table. */
2493 #define sh_elf_link_hash_traverse(table, func, info) \
2494 (elf_link_hash_traverse \
2496 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2499 /* Get the sh ELF linker hash table from a link_info structure. */
2501 #define sh_elf_hash_table(p) \
2502 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2503 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2505 /* Create an entry in an sh ELF linker hash table. */
2507 static struct bfd_hash_entry
*
2508 sh_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
2509 struct bfd_hash_table
*table
,
2512 struct elf_sh_link_hash_entry
*ret
=
2513 (struct elf_sh_link_hash_entry
*) entry
;
2515 /* Allocate the structure if it has not already been allocated by a
2517 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2518 ret
= ((struct elf_sh_link_hash_entry
*)
2519 bfd_hash_allocate (table
,
2520 sizeof (struct elf_sh_link_hash_entry
)));
2521 if (ret
== (struct elf_sh_link_hash_entry
*) NULL
)
2522 return (struct bfd_hash_entry
*) ret
;
2524 /* Call the allocation method of the superclass. */
2525 ret
= ((struct elf_sh_link_hash_entry
*)
2526 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2528 if (ret
!= (struct elf_sh_link_hash_entry
*) NULL
)
2530 ret
->dyn_relocs
= NULL
;
2531 ret
->gotplt_refcount
= 0;
2532 #ifdef INCLUDE_SHMEDIA
2533 ret
->datalabel_got
.refcount
= ret
->root
.got
.refcount
;
2535 ret
->funcdesc
.refcount
= 0;
2536 ret
->abs_funcdesc_refcount
= 0;
2537 ret
->got_type
= GOT_UNKNOWN
;
2540 return (struct bfd_hash_entry
*) ret
;
2543 /* Create an sh ELF linker hash table. */
2545 static struct bfd_link_hash_table
*
2546 sh_elf_link_hash_table_create (bfd
*abfd
)
2548 struct elf_sh_link_hash_table
*ret
;
2549 bfd_size_type amt
= sizeof (struct elf_sh_link_hash_table
);
2551 ret
= (struct elf_sh_link_hash_table
*) bfd_zmalloc (amt
);
2552 if (ret
== (struct elf_sh_link_hash_table
*) NULL
)
2555 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2556 sh_elf_link_hash_newfunc
,
2557 sizeof (struct elf_sh_link_hash_entry
),
2564 ret
->vxworks_p
= vxworks_object_p (abfd
);
2565 ret
->fdpic_p
= fdpic_object_p (abfd
);
2567 return &ret
->root
.root
;
2571 sh_elf_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2572 struct bfd_link_info
*info
, asection
*p
)
2574 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
2576 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2580 /* We need dynamic symbols for every section, since segments can
2581 relocate independently. */
2582 switch (elf_section_data (p
)->this_hdr
.sh_type
)
2586 /* If sh_type is yet undecided, assume it could be
2587 SHT_PROGBITS/SHT_NOBITS. */
2591 /* There shouldn't be section relative relocations
2592 against any other section. */
2598 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2599 shortcuts to them in our hash table. */
2602 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
2604 struct elf_sh_link_hash_table
*htab
;
2606 if (! _bfd_elf_create_got_section (dynobj
, info
))
2609 htab
= sh_elf_hash_table (info
);
2613 htab
->sfuncdesc
= bfd_make_section_anyway_with_flags (dynobj
, ".got.funcdesc",
2614 (SEC_ALLOC
| SEC_LOAD
2617 | SEC_LINKER_CREATED
));
2618 if (htab
->sfuncdesc
== NULL
2619 || ! bfd_set_section_alignment (dynobj
, htab
->sfuncdesc
, 2))
2622 htab
->srelfuncdesc
= bfd_make_section_anyway_with_flags (dynobj
,
2623 ".rela.got.funcdesc",
2624 (SEC_ALLOC
| SEC_LOAD
2627 | SEC_LINKER_CREATED
2629 if (htab
->srelfuncdesc
== NULL
2630 || ! bfd_set_section_alignment (dynobj
, htab
->srelfuncdesc
, 2))
2633 /* Also create .rofixup. */
2634 htab
->srofixup
= bfd_make_section_anyway_with_flags (dynobj
, ".rofixup",
2635 (SEC_ALLOC
| SEC_LOAD
2638 | SEC_LINKER_CREATED
2640 if (htab
->srofixup
== NULL
2641 || ! bfd_set_section_alignment (dynobj
, htab
->srofixup
, 2))
2647 /* Create dynamic sections when linking against a dynamic object. */
2650 sh_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
2652 struct elf_sh_link_hash_table
*htab
;
2653 flagword flags
, pltflags
;
2655 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2658 switch (bed
->s
->arch_size
)
2669 bfd_set_error (bfd_error_bad_value
);
2673 htab
= sh_elf_hash_table (info
);
2677 if (htab
->root
.dynamic_sections_created
)
2680 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2681 .rel[a].bss sections. */
2683 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
2684 | SEC_LINKER_CREATED
);
2687 pltflags
|= SEC_CODE
;
2688 if (bed
->plt_not_loaded
)
2689 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
2690 if (bed
->plt_readonly
)
2691 pltflags
|= SEC_READONLY
;
2693 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
2694 htab
->root
.splt
= s
;
2696 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
2699 if (bed
->want_plt_sym
)
2701 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2703 struct elf_link_hash_entry
*h
;
2704 struct bfd_link_hash_entry
*bh
= NULL
;
2706 if (! (_bfd_generic_link_add_one_symbol
2707 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
2708 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
2709 get_elf_backend_data (abfd
)->collect
, &bh
)))
2712 h
= (struct elf_link_hash_entry
*) bh
;
2714 h
->type
= STT_OBJECT
;
2715 htab
->root
.hplt
= h
;
2717 if (bfd_link_pic (info
)
2718 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2722 s
= bfd_make_section_anyway_with_flags (abfd
,
2723 bed
->default_use_rela_p
2724 ? ".rela.plt" : ".rel.plt",
2725 flags
| SEC_READONLY
);
2726 htab
->root
.srelplt
= s
;
2728 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2731 if (htab
->root
.sgot
== NULL
2732 && !create_got_section (abfd
, info
))
2735 if (bed
->want_dynbss
)
2737 /* The .dynbss section is a place to put symbols which are defined
2738 by dynamic objects, are referenced by regular objects, and are
2739 not functions. We must allocate space for them in the process
2740 image and use a R_*_COPY reloc to tell the dynamic linker to
2741 initialize them at run time. The linker script puts the .dynbss
2742 section into the .bss section of the final image. */
2743 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
2744 SEC_ALLOC
| SEC_LINKER_CREATED
);
2749 /* The .rel[a].bss section holds copy relocs. This section is not
2750 normally needed. We need to create it here, though, so that the
2751 linker will map it to an output section. We can't just create it
2752 only if we need it, because we will not know whether we need it
2753 until we have seen all the input files, and the first time the
2754 main linker code calls BFD after examining all the input files
2755 (size_dynamic_sections) the input sections have already been
2756 mapped to the output sections. If the section turns out not to
2757 be needed, we can discard it later. We will never need this
2758 section when generating a shared object, since they do not use
2760 if (! bfd_link_pic (info
))
2762 s
= bfd_make_section_anyway_with_flags (abfd
,
2763 (bed
->default_use_rela_p
2764 ? ".rela.bss" : ".rel.bss"),
2765 flags
| SEC_READONLY
);
2768 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
2773 if (htab
->vxworks_p
)
2775 if (!elf_vxworks_create_dynamic_sections (abfd
, info
, &htab
->srelplt2
))
2782 /* Find dynamic relocs for H that apply to read-only sections. */
2785 readonly_dynrelocs (struct elf_link_hash_entry
*h
)
2787 struct elf_dyn_relocs
*p
;
2789 for (p
= sh_elf_hash_entry (h
)->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2791 asection
*s
= p
->sec
->output_section
;
2793 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2799 /* Adjust a symbol defined by a dynamic object and referenced by a
2800 regular object. The current definition is in some section of the
2801 dynamic object, but we're not including those sections. We have to
2802 change the definition to something the rest of the link can
2806 sh_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
2807 struct elf_link_hash_entry
*h
)
2809 struct elf_sh_link_hash_table
*htab
;
2812 htab
= sh_elf_hash_table (info
);
2816 /* Make sure we know what is going on here. */
2817 BFD_ASSERT (htab
->root
.dynobj
!= NULL
2822 && !h
->def_regular
)));
2824 /* If this is a function, put it in the procedure linkage table. We
2825 will fill in the contents of the procedure linkage table later,
2826 when we know the address of the .got section. */
2827 if (h
->type
== STT_FUNC
2830 if (h
->plt
.refcount
<= 0
2831 || SYMBOL_CALLS_LOCAL (info
, h
)
2832 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2833 && h
->root
.type
== bfd_link_hash_undefweak
))
2835 /* This case can occur if we saw a PLT reloc in an input
2836 file, but the symbol was never referred to by a dynamic
2837 object. In such a case, we don't actually need to build
2838 a procedure linkage table, and we can just do a REL32
2840 h
->plt
.offset
= (bfd_vma
) -1;
2847 h
->plt
.offset
= (bfd_vma
) -1;
2849 /* If this is a weak symbol, and there is a real definition, the
2850 processor independent code will have arranged for us to see the
2851 real definition first, and we can just use the same value. */
2852 if (h
->is_weakalias
)
2854 struct elf_link_hash_entry
*def
= weakdef (h
);
2855 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
2856 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
2857 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
2858 if (info
->nocopyreloc
)
2859 h
->non_got_ref
= def
->non_got_ref
;
2863 /* This is a reference to a symbol defined by a dynamic object which
2864 is not a function. */
2866 /* If we are creating a shared library, we must presume that the
2867 only references to the symbol are via the global offset table.
2868 For such cases we need not do anything here; the relocations will
2869 be handled correctly by relocate_section. */
2870 if (bfd_link_pic (info
))
2873 /* If there are no references to this symbol that do not use the
2874 GOT, we don't need to generate a copy reloc. */
2875 if (!h
->non_got_ref
)
2878 /* If -z nocopyreloc was given, we won't generate them either. */
2879 if (0 && info
->nocopyreloc
)
2885 /* If we don't find any dynamic relocs in read-only sections, then
2886 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2887 if (0 && !readonly_dynrelocs (h
))
2893 /* We must allocate the symbol in our .dynbss section, which will
2894 become part of the .bss section of the executable. There will be
2895 an entry for this symbol in the .dynsym section. The dynamic
2896 object will contain position independent code, so all references
2897 from the dynamic object to this symbol will go through the global
2898 offset table. The dynamic linker will use the .dynsym entry to
2899 determine the address it must put in the global offset table, so
2900 both the dynamic object and the regular object will refer to the
2901 same memory location for the variable. */
2904 BFD_ASSERT (s
!= NULL
);
2906 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2907 copy the initial value out of the dynamic object and into the
2908 runtime process image. We need to remember the offset into the
2909 .rela.bss section we are going to use. */
2910 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
2914 srel
= htab
->srelbss
;
2915 BFD_ASSERT (srel
!= NULL
);
2916 srel
->size
+= sizeof (Elf32_External_Rela
);
2920 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
2923 /* Allocate space in .plt, .got and associated reloc sections for
2927 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
2929 struct bfd_link_info
*info
;
2930 struct elf_sh_link_hash_table
*htab
;
2931 struct elf_sh_link_hash_entry
*eh
;
2932 struct elf_dyn_relocs
*p
;
2934 if (h
->root
.type
== bfd_link_hash_indirect
)
2937 info
= (struct bfd_link_info
*) inf
;
2938 htab
= sh_elf_hash_table (info
);
2942 eh
= (struct elf_sh_link_hash_entry
*) h
;
2943 if ((h
->got
.refcount
> 0
2945 && eh
->gotplt_refcount
> 0)
2947 /* The symbol has been forced local, or we have some direct got refs,
2948 so treat all the gotplt refs as got refs. */
2949 h
->got
.refcount
+= eh
->gotplt_refcount
;
2950 if (h
->plt
.refcount
>= eh
->gotplt_refcount
)
2951 h
->plt
.refcount
-= eh
->gotplt_refcount
;
2954 if (htab
->root
.dynamic_sections_created
2955 && h
->plt
.refcount
> 0
2956 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2957 || h
->root
.type
!= bfd_link_hash_undefweak
))
2959 /* Make sure this symbol is output as a dynamic symbol.
2960 Undefined weak syms won't yet be marked as dynamic. */
2961 if (h
->dynindx
== -1
2962 && !h
->forced_local
)
2964 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2968 if (bfd_link_pic (info
)
2969 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2971 asection
*s
= htab
->root
.splt
;
2972 const struct elf_sh_plt_info
*plt_info
;
2974 /* If this is the first .plt entry, make room for the special
2977 s
->size
+= htab
->plt_info
->plt0_entry_size
;
2979 h
->plt
.offset
= s
->size
;
2981 /* If this symbol is not defined in a regular file, and we are
2982 not generating a shared library, then set the symbol to this
2983 location in the .plt. This is required to make function
2984 pointers compare as equal between the normal executable and
2985 the shared library. Skip this for FDPIC, since the
2986 function's address will be the address of the canonical
2987 function descriptor. */
2988 if (!htab
->fdpic_p
&& !bfd_link_pic (info
) && !h
->def_regular
)
2990 h
->root
.u
.def
.section
= s
;
2991 h
->root
.u
.def
.value
= h
->plt
.offset
;
2994 /* Make room for this entry. */
2995 plt_info
= htab
->plt_info
;
2996 if (plt_info
->short_plt
!= NULL
2997 && (get_plt_index (plt_info
->short_plt
, s
->size
) < MAX_SHORT_PLT
))
2998 plt_info
= plt_info
->short_plt
;
2999 s
->size
+= plt_info
->symbol_entry_size
;
3001 /* We also need to make an entry in the .got.plt section, which
3002 will be placed in the .got section by the linker script. */
3004 htab
->root
.sgotplt
->size
+= 4;
3006 htab
->root
.sgotplt
->size
+= 8;
3008 /* We also need to make an entry in the .rel.plt section. */
3009 htab
->root
.srelplt
->size
+= sizeof (Elf32_External_Rela
);
3011 if (htab
->vxworks_p
&& !bfd_link_pic (info
))
3013 /* VxWorks executables have a second set of relocations
3014 for each PLT entry. They go in a separate relocation
3015 section, which is processed by the kernel loader. */
3017 /* There is a relocation for the initial PLT entry:
3018 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
3019 if (h
->plt
.offset
== htab
->plt_info
->plt0_entry_size
)
3020 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
);
3022 /* There are two extra relocations for each subsequent
3023 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
3024 and an R_SH_DIR32 relocation for the PLT entry. */
3025 htab
->srelplt2
->size
+= sizeof (Elf32_External_Rela
) * 2;
3030 h
->plt
.offset
= (bfd_vma
) -1;
3036 h
->plt
.offset
= (bfd_vma
) -1;
3040 if (h
->got
.refcount
> 0)
3044 enum got_type got_type
= sh_elf_hash_entry (h
)->got_type
;
3046 /* Make sure this symbol is output as a dynamic symbol.
3047 Undefined weak syms won't yet be marked as dynamic. */
3048 if (h
->dynindx
== -1
3049 && !h
->forced_local
)
3051 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3055 s
= htab
->root
.sgot
;
3056 h
->got
.offset
= s
->size
;
3058 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
3059 if (got_type
== GOT_TLS_GD
)
3061 dyn
= htab
->root
.dynamic_sections_created
;
3064 /* No dynamic relocations required. */
3065 if (htab
->fdpic_p
&& !bfd_link_pic (info
)
3066 && h
->root
.type
!= bfd_link_hash_undefweak
3067 && (got_type
== GOT_NORMAL
|| got_type
== GOT_FUNCDESC
))
3068 htab
->srofixup
->size
+= 4;
3070 /* No dynamic relocations required when IE->LE conversion happens. */
3071 else if (got_type
== GOT_TLS_IE
3073 && !bfd_link_pic (info
))
3075 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3076 R_SH_TLS_GD needs one if local symbol and two if global. */
3077 else if ((got_type
== GOT_TLS_GD
&& h
->dynindx
== -1)
3078 || got_type
== GOT_TLS_IE
)
3079 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
3080 else if (got_type
== GOT_TLS_GD
)
3081 htab
->root
.srelgot
->size
+= 2 * sizeof (Elf32_External_Rela
);
3082 else if (got_type
== GOT_FUNCDESC
)
3084 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
3085 htab
->srofixup
->size
+= 4;
3087 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
3089 else if ((ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3090 || h
->root
.type
!= bfd_link_hash_undefweak
)
3091 && (bfd_link_pic (info
)
3092 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
3093 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
3094 else if (htab
->fdpic_p
3095 && !bfd_link_pic (info
)
3096 && got_type
== GOT_NORMAL
3097 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3098 || h
->root
.type
!= bfd_link_hash_undefweak
))
3099 htab
->srofixup
->size
+= 4;
3102 h
->got
.offset
= (bfd_vma
) -1;
3104 #ifdef INCLUDE_SHMEDIA
3105 if (eh
->datalabel_got
.refcount
> 0)
3110 /* Make sure this symbol is output as a dynamic symbol.
3111 Undefined weak syms won't yet be marked as dynamic. */
3112 if (h
->dynindx
== -1
3113 && !h
->forced_local
)
3115 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3119 s
= htab
->root
.sgot
;
3120 eh
->datalabel_got
.offset
= s
->size
;
3122 dyn
= htab
->root
.dynamic_sections_created
;
3123 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
))
3124 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
3127 eh
->datalabel_got
.offset
= (bfd_vma
) -1;
3130 /* Allocate space for any dynamic relocations to function
3131 descriptors, canonical or otherwise. We need to relocate the
3132 reference unless it resolves to zero, which only happens for
3133 undefined weak symbols (either non-default visibility, or when
3134 static linking). Any GOT slot is accounted for elsewhere. */
3135 if (eh
->abs_funcdesc_refcount
> 0
3136 && (h
->root
.type
!= bfd_link_hash_undefweak
3137 || (htab
->root
.dynamic_sections_created
3138 && ! SYMBOL_CALLS_LOCAL (info
, h
))))
3140 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
3141 htab
->srofixup
->size
+= eh
->abs_funcdesc_refcount
* 4;
3143 htab
->root
.srelgot
->size
3144 += eh
->abs_funcdesc_refcount
* sizeof (Elf32_External_Rela
);
3147 /* We must allocate a function descriptor if there are references to
3148 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
3149 the dynamic linker isn't going to allocate it. None of this
3150 applies if we already created one in .got.plt, but if the
3151 canonical function descriptor can be in this object, there
3152 won't be a PLT entry at all. */
3153 if ((eh
->funcdesc
.refcount
> 0
3154 || (h
->got
.offset
!= MINUS_ONE
&& eh
->got_type
== GOT_FUNCDESC
))
3155 && h
->root
.type
!= bfd_link_hash_undefweak
3156 && SYMBOL_FUNCDESC_LOCAL (info
, h
))
3158 /* Make room for this function descriptor. */
3159 eh
->funcdesc
.offset
= htab
->sfuncdesc
->size
;
3160 htab
->sfuncdesc
->size
+= 8;
3162 /* We will need a relocation or two fixups to initialize the
3163 function descriptor, so allocate those too. */
3164 if (!bfd_link_pic (info
) && SYMBOL_CALLS_LOCAL (info
, h
))
3165 htab
->srofixup
->size
+= 8;
3167 htab
->srelfuncdesc
->size
+= sizeof (Elf32_External_Rela
);
3170 if (eh
->dyn_relocs
== NULL
)
3173 /* In the shared -Bsymbolic case, discard space allocated for
3174 dynamic pc-relative relocs against symbols which turn out to be
3175 defined in regular objects. For the normal shared case, discard
3176 space for pc-relative relocs that have become local due to symbol
3177 visibility changes. */
3179 if (bfd_link_pic (info
))
3181 if (SYMBOL_CALLS_LOCAL (info
, h
))
3183 struct elf_dyn_relocs
**pp
;
3185 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
3187 p
->count
-= p
->pc_count
;
3196 if (htab
->vxworks_p
)
3198 struct elf_dyn_relocs
**pp
;
3200 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
3202 if (strcmp (p
->sec
->output_section
->name
, ".tls_vars") == 0)
3209 /* Also discard relocs on undefined weak syms with non-default
3211 if (eh
->dyn_relocs
!= NULL
3212 && h
->root
.type
== bfd_link_hash_undefweak
)
3214 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
3215 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
3216 eh
->dyn_relocs
= NULL
;
3218 /* Make sure undefined weak symbols are output as a dynamic
3220 else if (h
->dynindx
== -1
3221 && !h
->forced_local
)
3223 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3230 /* For the non-shared case, discard space for relocs against
3231 symbols which turn out to need copy relocs or are not
3237 || (htab
->root
.dynamic_sections_created
3238 && (h
->root
.type
== bfd_link_hash_undefweak
3239 || h
->root
.type
== bfd_link_hash_undefined
))))
3241 /* Make sure this symbol is output as a dynamic symbol.
3242 Undefined weak syms won't yet be marked as dynamic. */
3243 if (h
->dynindx
== -1
3244 && !h
->forced_local
)
3246 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3250 /* If that succeeded, we know we'll be keeping all the
3252 if (h
->dynindx
!= -1)
3256 eh
->dyn_relocs
= NULL
;
3261 /* Finally, allocate space. */
3262 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
3264 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
3265 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
3267 /* If we need relocations, we do not need fixups. */
3268 if (htab
->fdpic_p
&& !bfd_link_pic (info
))
3269 htab
->srofixup
->size
-= 4 * (p
->count
- p
->pc_count
);
3275 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
3276 read-only sections. */
3279 maybe_set_textrel (struct elf_link_hash_entry
*h
, void *info_p
)
3283 if (h
->root
.type
== bfd_link_hash_indirect
)
3286 sec
= readonly_dynrelocs (h
);
3289 struct bfd_link_info
*info
= (struct bfd_link_info
*) info_p
;
3291 info
->flags
|= DF_TEXTREL
;
3292 info
->callbacks
->minfo
3293 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
3294 sec
->owner
, h
->root
.root
.string
, sec
);
3296 /* Not an error, just cut short the traversal. */
3302 /* This function is called after all the input files have been read,
3303 and the input sections have been assigned to output sections.
3304 It's a convenient place to determine the PLT style. */
3307 sh_elf_always_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
3309 sh_elf_hash_table (info
)->plt_info
= get_plt_info (output_bfd
,
3310 bfd_link_pic (info
));
3312 if (sh_elf_hash_table (info
)->fdpic_p
&& !bfd_link_relocatable (info
)
3313 && !bfd_elf_stack_segment_size (output_bfd
, info
,
3314 "__stacksize", DEFAULT_STACK_SIZE
))
3319 /* Set the sizes of the dynamic sections. */
3322 sh_elf_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
3323 struct bfd_link_info
*info
)
3325 struct elf_sh_link_hash_table
*htab
;
3331 htab
= sh_elf_hash_table (info
);
3335 dynobj
= htab
->root
.dynobj
;
3336 BFD_ASSERT (dynobj
!= NULL
);
3338 if (htab
->root
.dynamic_sections_created
)
3340 /* Set the contents of the .interp section to the interpreter. */
3341 if (bfd_link_executable (info
) && !info
->nointerp
)
3343 s
= bfd_get_linker_section (dynobj
, ".interp");
3344 BFD_ASSERT (s
!= NULL
);
3345 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
3346 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
3350 /* Set up .got offsets for local syms, and space for local dynamic
3352 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
3354 bfd_signed_vma
*local_got
;
3355 bfd_signed_vma
*end_local_got
;
3356 union gotref
*local_funcdesc
, *end_local_funcdesc
;
3357 char *local_got_type
;
3358 bfd_size_type locsymcount
;
3359 Elf_Internal_Shdr
*symtab_hdr
;
3362 if (! is_sh_elf (ibfd
))
3365 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3367 struct elf_dyn_relocs
*p
;
3369 for (p
= ((struct elf_dyn_relocs
*)
3370 elf_section_data (s
)->local_dynrel
);
3374 if (! bfd_is_abs_section (p
->sec
)
3375 && bfd_is_abs_section (p
->sec
->output_section
))
3377 /* Input section has been discarded, either because
3378 it is a copy of a linkonce section or due to
3379 linker script /DISCARD/, so we'll be discarding
3382 else if (htab
->vxworks_p
3383 && strcmp (p
->sec
->output_section
->name
,
3386 /* Relocations in vxworks .tls_vars sections are
3387 handled specially by the loader. */
3389 else if (p
->count
!= 0)
3391 srel
= elf_section_data (p
->sec
)->sreloc
;
3392 srel
->size
+= p
->count
* sizeof (Elf32_External_Rela
);
3393 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
3395 info
->flags
|= DF_TEXTREL
;
3396 info
->callbacks
->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
3397 p
->sec
->owner
, p
->sec
);
3400 /* If we need relocations, we do not need fixups. */
3401 if (htab
->fdpic_p
&& !bfd_link_pic (info
))
3402 htab
->srofixup
->size
-= 4 * (p
->count
- p
->pc_count
);
3407 symtab_hdr
= &elf_symtab_hdr (ibfd
);
3408 locsymcount
= symtab_hdr
->sh_info
;
3409 #ifdef INCLUDE_SHMEDIA
3410 /* Count datalabel local GOT. */
3413 s
= htab
->root
.sgot
;
3414 srel
= htab
->root
.srelgot
;
3416 local_got
= elf_local_got_refcounts (ibfd
);
3419 end_local_got
= local_got
+ locsymcount
;
3420 local_got_type
= sh_elf_local_got_type (ibfd
);
3421 local_funcdesc
= sh_elf_local_funcdesc (ibfd
);
3422 for (; local_got
< end_local_got
; ++local_got
)
3426 *local_got
= s
->size
;
3428 if (*local_got_type
== GOT_TLS_GD
)
3430 if (bfd_link_pic (info
))
3431 srel
->size
+= sizeof (Elf32_External_Rela
);
3433 htab
->srofixup
->size
+= 4;
3435 if (*local_got_type
== GOT_FUNCDESC
)
3437 if (local_funcdesc
== NULL
)
3441 size
= locsymcount
* sizeof (union gotref
);
3442 local_funcdesc
= (union gotref
*) bfd_zalloc (ibfd
,
3444 if (local_funcdesc
== NULL
)
3446 sh_elf_local_funcdesc (ibfd
) = local_funcdesc
;
3447 local_funcdesc
+= (local_got
3448 - elf_local_got_refcounts (ibfd
));
3450 local_funcdesc
->refcount
++;
3455 *local_got
= (bfd_vma
) -1;
3460 local_funcdesc
= sh_elf_local_funcdesc (ibfd
);
3463 end_local_funcdesc
= local_funcdesc
+ locsymcount
;
3465 for (; local_funcdesc
< end_local_funcdesc
; ++local_funcdesc
)
3467 if (local_funcdesc
->refcount
> 0)
3469 local_funcdesc
->offset
= htab
->sfuncdesc
->size
;
3470 htab
->sfuncdesc
->size
+= 8;
3471 if (!bfd_link_pic (info
))
3472 htab
->srofixup
->size
+= 8;
3474 htab
->srelfuncdesc
->size
+= sizeof (Elf32_External_Rela
);
3477 local_funcdesc
->offset
= MINUS_ONE
;
3483 if (htab
->tls_ldm_got
.refcount
> 0)
3485 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3487 htab
->tls_ldm_got
.offset
= htab
->root
.sgot
->size
;
3488 htab
->root
.sgot
->size
+= 8;
3489 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
3492 htab
->tls_ldm_got
.offset
= -1;
3494 /* Only the reserved entries should be present. For FDPIC, they go at
3495 the end of .got.plt. */
3498 BFD_ASSERT (htab
->root
.sgotplt
&& htab
->root
.sgotplt
->size
== 12);
3499 htab
->root
.sgotplt
->size
= 0;
3502 /* Allocate global sym .plt and .got entries, and space for global
3503 sym dynamic relocs. */
3504 elf_link_hash_traverse (&htab
->root
, allocate_dynrelocs
, info
);
3506 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3507 end of the FDPIC .got.plt. */
3510 htab
->root
.hgot
->root
.u
.def
.value
= htab
->root
.sgotplt
->size
;
3511 htab
->root
.sgotplt
->size
+= 12;
3514 /* At the very end of the .rofixup section is a pointer to the GOT. */
3515 if (htab
->fdpic_p
&& htab
->srofixup
!= NULL
)
3516 htab
->srofixup
->size
+= 4;
3518 /* We now have determined the sizes of the various dynamic sections.
3519 Allocate memory for them. */
3521 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
3523 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
3526 if (s
== htab
->root
.splt
3527 || s
== htab
->root
.sgot
3528 || s
== htab
->root
.sgotplt
3529 || s
== htab
->sfuncdesc
3530 || s
== htab
->srofixup
3531 || s
== htab
->sdynbss
)
3533 /* Strip this section if we don't need it; see the
3536 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
3538 if (s
->size
!= 0 && s
!= htab
->root
.srelplt
&& s
!= htab
->srelplt2
)
3541 /* We use the reloc_count field as a counter if we need
3542 to copy relocs into the output file. */
3547 /* It's not one of our sections, so don't allocate space. */
3553 /* If we don't need this section, strip it from the
3554 output file. This is mostly to handle .rela.bss and
3555 .rela.plt. We must create both sections in
3556 create_dynamic_sections, because they must be created
3557 before the linker maps input sections to output
3558 sections. The linker does that before
3559 adjust_dynamic_symbol is called, and it is that
3560 function which decides whether anything needs to go
3561 into these sections. */
3563 s
->flags
|= SEC_EXCLUDE
;
3567 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
3570 /* Allocate memory for the section contents. We use bfd_zalloc
3571 here in case unused entries are not reclaimed before the
3572 section's contents are written out. This should not happen,
3573 but this way if it does, we get a R_SH_NONE reloc instead
3575 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
3576 if (s
->contents
== NULL
)
3580 if (htab
->root
.dynamic_sections_created
)
3582 /* Add some entries to the .dynamic section. We fill in the
3583 values later, in sh_elf_finish_dynamic_sections, but we
3584 must add the entries now so that we get the correct size for
3585 the .dynamic section. The DT_DEBUG entry is filled in by the
3586 dynamic linker and used by the debugger. */
3587 #define add_dynamic_entry(TAG, VAL) \
3588 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3590 if (bfd_link_executable (info
))
3592 if (! add_dynamic_entry (DT_DEBUG
, 0))
3596 if (htab
->root
.splt
->size
!= 0)
3598 if (! add_dynamic_entry (DT_PLTGOT
, 0)
3599 || ! add_dynamic_entry (DT_PLTRELSZ
, 0)
3600 || ! add_dynamic_entry (DT_PLTREL
, DT_RELA
)
3601 || ! add_dynamic_entry (DT_JMPREL
, 0))
3604 else if ((elf_elfheader (output_bfd
)->e_flags
& EF_SH_FDPIC
))
3606 if (! add_dynamic_entry (DT_PLTGOT
, 0))
3612 if (! add_dynamic_entry (DT_RELA
, 0)
3613 || ! add_dynamic_entry (DT_RELASZ
, 0)
3614 || ! add_dynamic_entry (DT_RELAENT
,
3615 sizeof (Elf32_External_Rela
)))
3618 /* If any dynamic relocs apply to a read-only section,
3619 then we need a DT_TEXTREL entry. */
3620 if ((info
->flags
& DF_TEXTREL
) == 0)
3621 elf_link_hash_traverse (&htab
->root
, maybe_set_textrel
, info
);
3623 if ((info
->flags
& DF_TEXTREL
) != 0)
3625 if (! add_dynamic_entry (DT_TEXTREL
, 0))
3630 && !elf_vxworks_add_dynamic_entries (output_bfd
, info
))
3633 #undef add_dynamic_entry
3638 /* Add a dynamic relocation to the SRELOC section. */
3640 inline static bfd_vma
3641 sh_elf_add_dyn_reloc (bfd
*output_bfd
, asection
*sreloc
, bfd_vma offset
,
3642 int reloc_type
, long dynindx
, bfd_vma addend
)
3644 Elf_Internal_Rela outrel
;
3645 bfd_vma reloc_offset
;
3647 outrel
.r_offset
= offset
;
3648 outrel
.r_info
= ELF32_R_INFO (dynindx
, reloc_type
);
3649 outrel
.r_addend
= addend
;
3651 reloc_offset
= sreloc
->reloc_count
* sizeof (Elf32_External_Rela
);
3652 BFD_ASSERT (reloc_offset
< sreloc
->size
);
3653 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
3654 sreloc
->contents
+ reloc_offset
);
3655 sreloc
->reloc_count
++;
3657 return reloc_offset
;
3660 /* Add an FDPIC read-only fixup. */
3663 sh_elf_add_rofixup (bfd
*output_bfd
, asection
*srofixup
, bfd_vma offset
)
3665 bfd_vma fixup_offset
;
3667 fixup_offset
= srofixup
->reloc_count
++ * 4;
3668 BFD_ASSERT (fixup_offset
< srofixup
->size
);
3669 bfd_put_32 (output_bfd
, offset
, srofixup
->contents
+ fixup_offset
);
3672 /* Return the offset of the generated .got section from the
3673 _GLOBAL_OFFSET_TABLE_ symbol. */
3675 static bfd_signed_vma
3676 sh_elf_got_offset (struct elf_sh_link_hash_table
*htab
)
3678 return (htab
->root
.sgot
->output_offset
- htab
->root
.sgotplt
->output_offset
3679 - htab
->root
.hgot
->root
.u
.def
.value
);
3682 /* Find the segment number in which OSEC, and output section, is
3686 sh_elf_osec_to_segment (bfd
*output_bfd
, asection
*osec
)
3688 Elf_Internal_Phdr
*p
= NULL
;
3690 if (output_bfd
->xvec
->flavour
== bfd_target_elf_flavour
3691 /* PR ld/17110: Do not look for output segments in an input bfd. */
3692 && output_bfd
->direction
!= read_direction
)
3693 p
= _bfd_elf_find_segment_containing_section (output_bfd
, osec
);
3695 /* FIXME: Nothing ever says what this index is relative to. The kernel
3696 supplies data in terms of the number of load segments but this is
3697 a phdr index and the first phdr may not be a load segment. */
3698 return (p
!= NULL
) ? p
- elf_tdata (output_bfd
)->phdr
: -1;
3702 sh_elf_osec_readonly_p (bfd
*output_bfd
, asection
*osec
)
3704 unsigned seg
= sh_elf_osec_to_segment (output_bfd
, osec
);
3706 return (seg
!= (unsigned) -1
3707 && ! (elf_tdata (output_bfd
)->phdr
[seg
].p_flags
& PF_W
));
3710 /* Generate the initial contents of a local function descriptor, along
3711 with any relocations or fixups required. */
3713 sh_elf_initialize_funcdesc (bfd
*output_bfd
,
3714 struct bfd_link_info
*info
,
3715 struct elf_link_hash_entry
*h
,
3720 struct elf_sh_link_hash_table
*htab
;
3724 htab
= sh_elf_hash_table (info
);
3726 /* FIXME: The ABI says that the offset to the function goes in the
3727 descriptor, along with the segment index. We're RELA, so it could
3728 go in the reloc instead... */
3730 if (h
!= NULL
&& SYMBOL_CALLS_LOCAL (info
, h
))
3732 section
= h
->root
.u
.def
.section
;
3733 value
= h
->root
.u
.def
.value
;
3736 if (h
== NULL
|| SYMBOL_CALLS_LOCAL (info
, h
))
3738 dynindx
= elf_section_data (section
->output_section
)->dynindx
;
3739 addr
= value
+ section
->output_offset
;
3740 seg
= sh_elf_osec_to_segment (output_bfd
, section
->output_section
);
3744 BFD_ASSERT (h
->dynindx
!= -1);
3745 dynindx
= h
->dynindx
;
3749 if (!bfd_link_pic (info
) && SYMBOL_CALLS_LOCAL (info
, h
))
3751 if (h
== NULL
|| h
->root
.type
!= bfd_link_hash_undefweak
)
3753 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
3755 + htab
->sfuncdesc
->output_section
->vma
3756 + htab
->sfuncdesc
->output_offset
);
3757 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
3759 + htab
->sfuncdesc
->output_section
->vma
3760 + htab
->sfuncdesc
->output_offset
);
3763 /* There are no dynamic relocations so fill in the final
3764 address and gp value (barring fixups). */
3765 addr
+= section
->output_section
->vma
;
3766 seg
= htab
->root
.hgot
->root
.u
.def
.value
3767 + htab
->root
.hgot
->root
.u
.def
.section
->output_section
->vma
3768 + htab
->root
.hgot
->root
.u
.def
.section
->output_offset
;
3771 sh_elf_add_dyn_reloc (output_bfd
, htab
->srelfuncdesc
,
3773 + htab
->sfuncdesc
->output_section
->vma
3774 + htab
->sfuncdesc
->output_offset
,
3775 R_SH_FUNCDESC_VALUE
, dynindx
, 0);
3777 bfd_put_32 (output_bfd
, addr
, htab
->sfuncdesc
->contents
+ offset
);
3778 bfd_put_32 (output_bfd
, seg
, htab
->sfuncdesc
->contents
+ offset
+ 4);
3783 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3784 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3787 static bfd_reloc_status_type
3788 install_movi20_field (bfd
*output_bfd
, unsigned long relocation
,
3789 bfd
*input_bfd
, asection
*input_section
,
3790 bfd_byte
*contents
, bfd_vma offset
)
3792 unsigned long cur_val
;
3794 bfd_reloc_status_type r
;
3796 if (offset
> bfd_get_section_limit (input_bfd
, input_section
))
3797 return bfd_reloc_outofrange
;
3799 r
= bfd_check_overflow (complain_overflow_signed
, 20, 0,
3800 bfd_arch_bits_per_address (input_bfd
), relocation
);
3801 if (r
!= bfd_reloc_ok
)
3804 addr
= contents
+ offset
;
3805 cur_val
= bfd_get_16 (output_bfd
, addr
);
3806 bfd_put_16 (output_bfd
, cur_val
| ((relocation
& 0xf0000) >> 12), addr
);
3807 bfd_put_16 (output_bfd
, relocation
& 0xffff, addr
+ 2);
3809 return bfd_reloc_ok
;
3812 /* Relocate an SH ELF section. */
3815 sh_elf_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
3816 bfd
*input_bfd
, asection
*input_section
,
3817 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
3818 Elf_Internal_Sym
*local_syms
,
3819 asection
**local_sections
)
3821 struct elf_sh_link_hash_table
*htab
;
3822 Elf_Internal_Shdr
*symtab_hdr
;
3823 struct elf_link_hash_entry
**sym_hashes
;
3824 Elf_Internal_Rela
*rel
, *relend
;
3825 bfd_vma
*local_got_offsets
;
3826 asection
*sgot
= NULL
;
3827 asection
*sgotplt
= NULL
;
3828 asection
*splt
= NULL
;
3829 asection
*sreloc
= NULL
;
3830 asection
*srelgot
= NULL
;
3831 bfd_boolean is_vxworks_tls
;
3832 unsigned isec_segment
, got_segment
, plt_segment
, check_segment
[2];
3833 bfd_boolean fdpic_p
= FALSE
;
3835 BFD_ASSERT (is_sh_elf (input_bfd
));
3837 htab
= sh_elf_hash_table (info
);
3840 sgot
= htab
->root
.sgot
;
3841 sgotplt
= htab
->root
.sgotplt
;
3842 srelgot
= htab
->root
.srelgot
;
3843 splt
= htab
->root
.splt
;
3844 fdpic_p
= htab
->fdpic_p
;
3846 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
3847 sym_hashes
= elf_sym_hashes (input_bfd
);
3848 local_got_offsets
= elf_local_got_offsets (input_bfd
);
3850 isec_segment
= sh_elf_osec_to_segment (output_bfd
,
3851 input_section
->output_section
);
3852 if (fdpic_p
&& sgot
)
3853 got_segment
= sh_elf_osec_to_segment (output_bfd
,
3854 sgot
->output_section
);
3857 if (fdpic_p
&& splt
)
3858 plt_segment
= sh_elf_osec_to_segment (output_bfd
,
3859 splt
->output_section
);
3863 /* We have to handle relocations in vxworks .tls_vars sections
3864 specially, because the dynamic loader is 'weird'. */
3865 is_vxworks_tls
= (htab
&& htab
->vxworks_p
&& bfd_link_pic (info
)
3866 && !strcmp (input_section
->output_section
->name
,
3870 relend
= relocs
+ input_section
->reloc_count
;
3871 for (; rel
< relend
; rel
++)
3874 reloc_howto_type
*howto
;
3875 unsigned long r_symndx
;
3876 Elf_Internal_Sym
*sym
;
3878 struct elf_link_hash_entry
*h
;
3880 bfd_vma addend
= (bfd_vma
) 0;
3881 bfd_reloc_status_type r
;
3882 int seen_stt_datalabel
= 0;
3884 enum got_type got_type
;
3885 const char *symname
= NULL
;
3886 bfd_boolean resolved_to_zero
;
3888 r_symndx
= ELF32_R_SYM (rel
->r_info
);
3890 r_type
= ELF32_R_TYPE (rel
->r_info
);
3892 /* Many of the relocs are only used for relaxing, and are
3893 handled entirely by the relaxation code. */
3894 if (r_type
>= (int) R_SH_GNU_VTINHERIT
3895 && r_type
<= (int) R_SH_LABEL
)
3897 if (r_type
== (int) R_SH_NONE
)
3901 || r_type
>= R_SH_max
3902 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC
3903 && r_type
<= (int) R_SH_LAST_INVALID_RELOC
)
3904 || (r_type
>= (int) R_SH_FIRST_INVALID_RELOC_2
3905 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_2
)
3906 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_3
3907 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_3
)
3908 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_4
3909 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_4
)
3910 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_5
3911 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_5
)
3912 || ( r_type
>= (int) R_SH_FIRST_INVALID_RELOC_6
3913 && r_type
<= (int) R_SH_LAST_INVALID_RELOC_6
))
3915 bfd_set_error (bfd_error_bad_value
);
3919 howto
= get_howto_table (output_bfd
) + r_type
;
3921 /* For relocs that aren't partial_inplace, we get the addend from
3923 if (! howto
->partial_inplace
)
3924 addend
= rel
->r_addend
;
3926 resolved_to_zero
= FALSE
;
3930 check_segment
[0] = -1;
3931 check_segment
[1] = -1;
3932 if (r_symndx
< symtab_hdr
->sh_info
)
3934 sym
= local_syms
+ r_symndx
;
3935 sec
= local_sections
[r_symndx
];
3937 symname
= bfd_elf_string_from_elf_section
3938 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
3939 if (symname
== NULL
|| *symname
== '\0')
3940 symname
= bfd_section_name (input_bfd
, sec
);
3942 relocation
= (sec
->output_section
->vma
3943 + sec
->output_offset
3945 /* A local symbol never has STO_SH5_ISA32, so we don't need
3946 datalabel processing here. Make sure this does not change
3948 if ((sym
->st_other
& STO_SH5_ISA32
) != 0)
3949 (*info
->callbacks
->reloc_dangerous
)
3951 _("unexpected STO_SH5_ISA32 on local symbol is not handled"),
3952 input_bfd
, input_section
, rel
->r_offset
);
3954 if (sec
!= NULL
&& discarded_section (sec
))
3955 /* Handled below. */
3957 else if (bfd_link_relocatable (info
))
3959 /* This is a relocatable link. We don't have to change
3960 anything, unless the reloc is against a section symbol,
3961 in which case we have to adjust according to where the
3962 section symbol winds up in the output section. */
3963 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
3965 if (! howto
->partial_inplace
)
3967 /* For relocations with the addend in the
3968 relocation, we need just to update the addend.
3969 All real relocs are of type partial_inplace; this
3970 code is mostly for completeness. */
3971 rel
->r_addend
+= sec
->output_offset
;
3976 /* Relocs of type partial_inplace need to pick up the
3977 contents in the contents and add the offset resulting
3978 from the changed location of the section symbol.
3979 Using _bfd_final_link_relocate (e.g. goto
3980 final_link_relocate) here would be wrong, because
3981 relocations marked pc_relative would get the current
3982 location subtracted, and we must only do that at the
3984 r
= _bfd_relocate_contents (howto
, input_bfd
,
3987 contents
+ rel
->r_offset
);
3988 goto relocation_done
;
3993 else if (! howto
->partial_inplace
)
3995 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
3996 addend
= rel
->r_addend
;
3998 else if ((sec
->flags
& SEC_MERGE
)
3999 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
4003 if (howto
->rightshift
|| howto
->src_mask
!= 0xffffffff)
4006 /* xgettext:c-format */
4007 (_("%pB(%pA+%#" PRIx64
"): "
4008 "%s relocation against SEC_MERGE section"),
4009 input_bfd
, input_section
,
4010 (uint64_t) rel
->r_offset
, howto
->name
);
4014 addend
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
4017 _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
)
4019 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
4020 bfd_put_32 (input_bfd
, addend
, contents
+ rel
->r_offset
);
4026 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
4029 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
4030 symname
= h
->root
.root
.string
;
4031 while (h
->root
.type
== bfd_link_hash_indirect
4032 || h
->root
.type
== bfd_link_hash_warning
)
4034 #ifdef INCLUDE_SHMEDIA
4035 /* If the reference passes a symbol marked with
4036 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4038 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
4040 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4042 if (h
->root
.type
== bfd_link_hash_defined
4043 || h
->root
.type
== bfd_link_hash_defweak
)
4047 dyn
= htab
? htab
->root
.dynamic_sections_created
: FALSE
;
4048 sec
= h
->root
.u
.def
.section
;
4049 /* In these cases, we don't need the relocation value.
4050 We check specially because in some obscure cases
4051 sec->output_section will be NULL. */
4052 if (r_type
== R_SH_GOTPC
4053 || r_type
== R_SH_GOTPC_LOW16
4054 || r_type
== R_SH_GOTPC_MEDLOW16
4055 || r_type
== R_SH_GOTPC_MEDHI16
4056 || r_type
== R_SH_GOTPC_HI16
4057 || ((r_type
== R_SH_PLT32
4058 || r_type
== R_SH_PLT_LOW16
4059 || r_type
== R_SH_PLT_MEDLOW16
4060 || r_type
== R_SH_PLT_MEDHI16
4061 || r_type
== R_SH_PLT_HI16
)
4062 && h
->plt
.offset
!= (bfd_vma
) -1)
4063 || ((r_type
== R_SH_GOT32
4064 || r_type
== R_SH_GOT20
4065 || r_type
== R_SH_GOTFUNCDESC
4066 || r_type
== R_SH_GOTFUNCDESC20
4067 || r_type
== R_SH_GOTOFFFUNCDESC
4068 || r_type
== R_SH_GOTOFFFUNCDESC20
4069 || r_type
== R_SH_FUNCDESC
4070 || r_type
== R_SH_GOT_LOW16
4071 || r_type
== R_SH_GOT_MEDLOW16
4072 || r_type
== R_SH_GOT_MEDHI16
4073 || r_type
== R_SH_GOT_HI16
)
4074 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
4075 bfd_link_pic (info
),
4077 && (! bfd_link_pic (info
)
4078 || (! info
->symbolic
&& h
->dynindx
!= -1)
4079 || !h
->def_regular
))
4080 /* The cases above are those in which relocation is
4081 overwritten in the switch block below. The cases
4082 below are those in which we must defer relocation
4083 to run-time, because we can't resolve absolute
4084 addresses when creating a shared library. */
4085 || (bfd_link_pic (info
)
4086 && ((! info
->symbolic
&& h
->dynindx
!= -1)
4088 && ((r_type
== R_SH_DIR32
4089 && !h
->forced_local
)
4090 || (r_type
== R_SH_REL32
4091 && !SYMBOL_CALLS_LOCAL (info
, h
)))
4092 && ((input_section
->flags
& SEC_ALLOC
) != 0
4093 /* DWARF will emit R_SH_DIR32 relocations in its
4094 sections against symbols defined externally
4095 in shared libraries. We can't do anything
4097 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
4098 && h
->def_dynamic
)))
4099 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4100 sections because such sections are not SEC_ALLOC and
4101 thus ld.so will not process them. */
4102 || (sec
->output_section
== NULL
4103 && ((input_section
->flags
& SEC_DEBUGGING
) != 0
4105 || (sec
->output_section
== NULL
4106 && (sh_elf_hash_entry (h
)->got_type
== GOT_TLS_IE
4107 || sh_elf_hash_entry (h
)->got_type
== GOT_TLS_GD
)))
4109 else if (sec
->output_section
!= NULL
)
4110 relocation
= ((h
->root
.u
.def
.value
4111 + sec
->output_section
->vma
4112 + sec
->output_offset
)
4113 /* A STO_SH5_ISA32 causes a "bitor 1" to the
4114 symbol value, unless we've seen
4115 STT_DATALABEL on the way to it. */
4116 | ((h
->other
& STO_SH5_ISA32
) != 0
4117 && ! seen_stt_datalabel
));
4118 else if (!bfd_link_relocatable (info
)
4119 && (_bfd_elf_section_offset (output_bfd
, info
,
4125 /* xgettext:c-format */
4126 (_("%pB(%pA+%#" PRIx64
"): "
4127 "unresolvable %s relocation against symbol `%s'"),
4130 (uint64_t) rel
->r_offset
,
4132 h
->root
.root
.string
);
4136 else if (h
->root
.type
== bfd_link_hash_undefweak
)
4137 resolved_to_zero
= UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
);
4138 else if (info
->unresolved_syms_in_objects
== RM_IGNORE
4139 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
4141 else if (!bfd_link_relocatable (info
))
4142 (*info
->callbacks
->undefined_symbol
)
4143 (info
, h
->root
.root
.string
, input_bfd
,
4144 input_section
, rel
->r_offset
,
4145 (info
->unresolved_syms_in_objects
== RM_GENERATE_ERROR
4146 || ELF_ST_VISIBILITY (h
->other
)));
4149 if (sec
!= NULL
&& discarded_section (sec
))
4150 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
4151 rel
, 1, relend
, howto
, 0, contents
);
4153 if (bfd_link_relocatable (info
))
4156 /* Check for inter-segment relocations in FDPIC files. Most
4157 relocations connect the relocation site to the location of
4158 the target symbol, but there are some exceptions below. */
4159 check_segment
[0] = isec_segment
;
4161 check_segment
[1] = sh_elf_osec_to_segment (output_bfd
,
4162 sec
->output_section
);
4164 check_segment
[1] = -1;
4166 switch ((int) r_type
)
4168 final_link_relocate
:
4169 /* COFF relocs don't use the addend. The addend is used for
4170 R_SH_DIR32 to be compatible with other compilers. */
4171 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4172 contents
, rel
->r_offset
,
4173 relocation
, addend
);
4177 goto final_link_relocate
;
4182 /* If the reloc is against the start of this section, then
4183 the assembler has already taken care of it and the reloc
4184 is here only to assist in relaxing. If the reloc is not
4185 against the start of this section, then it's against an
4186 external symbol and we must deal with it ourselves. */
4187 if (input_section
->output_section
->vma
+ input_section
->output_offset
4190 int disp
= (relocation
4191 - input_section
->output_section
->vma
4192 - input_section
->output_offset
4198 case R_SH_DIR8WPZ
: mask
= 1; break;
4199 case R_SH_DIR8WPL
: mask
= 3; break;
4200 default: mask
= 0; break;
4205 /* xgettext:c-format */
4206 (_("%pB: %#" PRIx64
": fatal: "
4207 "unaligned branch target for relax-support relocation"),
4208 input_section
->owner
,
4209 (uint64_t) rel
->r_offset
);
4210 bfd_set_error (bfd_error_bad_value
);
4214 goto final_link_relocate
;
4220 #ifdef INCLUDE_SHMEDIA
4221 if (shmedia_prepare_reloc (info
, input_bfd
, input_section
,
4222 contents
, rel
, &relocation
))
4223 goto final_link_relocate
;
4225 bfd_set_error (bfd_error_bad_value
);
4233 goto final_link_relocate
;
4240 /* xgettext:c-format */
4241 (_("%pB: %#" PRIx64
": fatal: "
4242 "unaligned %s relocation %#" PRIx64
),
4243 input_section
->owner
, (uint64_t) rel
->r_offset
,
4244 howto
->name
, (uint64_t) relocation
);
4245 bfd_set_error (bfd_error_bad_value
);
4248 goto final_link_relocate
;
4256 /* xgettext:c-format */
4257 (_("%pB: %#" PRIx64
": fatal: "
4258 "unaligned %s relocation %#" PRIx64
""),
4259 input_section
->owner
,
4260 (uint64_t) rel
->r_offset
, howto
->name
,
4261 (uint64_t) relocation
);
4262 bfd_set_error (bfd_error_bad_value
);
4265 goto final_link_relocate
;
4268 if ((signed int)relocation
< -32
4269 || (signed int)relocation
> 32)
4272 /* xgettext:c-format */
4273 (_("%pB: %#" PRIx64
": fatal: R_SH_PSHA relocation %" PRId64
4274 " not in range -32..32"),
4275 input_section
->owner
,
4276 (uint64_t) rel
->r_offset
,
4277 (int64_t) relocation
);
4278 bfd_set_error (bfd_error_bad_value
);
4281 goto final_link_relocate
;
4284 if ((signed int)relocation
< -16
4285 || (signed int)relocation
> 16)
4288 /* xgettext:c-format */
4289 (_("%pB: %#" PRIx64
": fatal: R_SH_PSHL relocation %" PRId64
4290 " not in range -32..32"),
4291 input_section
->owner
,
4292 (uint64_t) rel
->r_offset
,
4293 (int64_t) relocation
);
4294 bfd_set_error (bfd_error_bad_value
);
4297 goto final_link_relocate
;
4301 #ifdef INCLUDE_SHMEDIA
4302 case R_SH_IMM_LOW16_PCREL
:
4303 case R_SH_IMM_MEDLOW16_PCREL
:
4304 case R_SH_IMM_MEDHI16_PCREL
:
4305 case R_SH_IMM_HI16_PCREL
:
4307 if (bfd_link_pic (info
)
4309 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4310 && !resolved_to_zero
)
4311 || h
->root
.type
!= bfd_link_hash_undefweak
)
4312 && r_symndx
!= STN_UNDEF
4313 && (input_section
->flags
& SEC_ALLOC
) != 0
4315 && (r_type
== R_SH_DIR32
4316 || !SYMBOL_CALLS_LOCAL (info
, h
)))
4318 Elf_Internal_Rela outrel
;
4320 bfd_boolean skip
, relocate
;
4322 /* When generating a shared object, these relocations
4323 are copied into the output file to be resolved at run
4328 sreloc
= _bfd_elf_get_dynamic_reloc_section
4329 (input_bfd
, input_section
, /*rela?*/ TRUE
);
4338 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
4340 if (outrel
.r_offset
== (bfd_vma
) -1)
4342 else if (outrel
.r_offset
== (bfd_vma
) -2)
4343 skip
= TRUE
, relocate
= TRUE
;
4344 outrel
.r_offset
+= (input_section
->output_section
->vma
4345 + input_section
->output_offset
);
4348 memset (&outrel
, 0, sizeof outrel
);
4349 else if (r_type
== R_SH_REL32
)
4351 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
4352 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_REL32
);
4354 = (howto
->partial_inplace
4355 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4358 #ifdef INCLUDE_SHMEDIA
4359 else if (r_type
== R_SH_IMM_LOW16_PCREL
4360 || r_type
== R_SH_IMM_MEDLOW16_PCREL
4361 || r_type
== R_SH_IMM_MEDHI16_PCREL
4362 || r_type
== R_SH_IMM_HI16_PCREL
)
4364 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
4365 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
4366 outrel
.r_addend
= addend
;
4371 || ((info
->symbolic
|| h
->dynindx
== -1)
4372 && h
->def_regular
)))
4376 BFD_ASSERT (sec
!= NULL
);
4377 BFD_ASSERT (sec
->output_section
!= NULL
);
4378 dynindx
= elf_section_data (sec
->output_section
)->dynindx
;
4379 outrel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
4380 outrel
.r_addend
= relocation
;
4382 += (howto
->partial_inplace
4383 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4385 outrel
.r_addend
-= sec
->output_section
->vma
;
4389 /* h->dynindx may be -1 if this symbol was marked to
4392 || ((info
->symbolic
|| h
->dynindx
== -1)
4395 relocate
= howto
->partial_inplace
;
4396 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4400 BFD_ASSERT (h
->dynindx
!= -1);
4401 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_DIR32
);
4403 outrel
.r_addend
= relocation
;
4405 += (howto
->partial_inplace
4406 ? bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
)
4410 loc
= sreloc
->contents
;
4411 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4412 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4414 check_segment
[0] = check_segment
[1] = -1;
4416 /* If this reloc is against an external symbol, we do
4417 not want to fiddle with the addend. Otherwise, we
4418 need to include the symbol value so that it becomes
4419 an addend for the dynamic reloc. */
4423 else if (fdpic_p
&& !bfd_link_pic (info
)
4424 && r_type
== R_SH_DIR32
4425 && (input_section
->flags
& SEC_ALLOC
) != 0)
4431 if (sh_elf_osec_readonly_p (output_bfd
,
4432 input_section
->output_section
))
4435 /* xgettext:c-format */
4436 (_("%pB(%pA+%#" PRIx64
"): "
4437 "cannot emit fixup to `%s' in read-only section"),
4440 (uint64_t) rel
->r_offset
,
4445 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4446 input_section
, rel
->r_offset
);
4447 if (offset
!= (bfd_vma
)-1)
4448 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4449 input_section
->output_section
->vma
4450 + input_section
->output_offset
4453 check_segment
[0] = check_segment
[1] = -1;
4455 /* We don't want warnings for non-NULL tests on undefined weak
4457 else if (r_type
== R_SH_REL32
4459 && h
->root
.type
== bfd_link_hash_undefweak
)
4460 check_segment
[0] = check_segment
[1] = -1;
4461 goto final_link_relocate
;
4464 #ifdef INCLUDE_SHMEDIA
4465 case R_SH_GOTPLT_LOW16
:
4466 case R_SH_GOTPLT_MEDLOW16
:
4467 case R_SH_GOTPLT_MEDHI16
:
4468 case R_SH_GOTPLT_HI16
:
4469 case R_SH_GOTPLT10BY4
:
4470 case R_SH_GOTPLT10BY8
:
4472 /* Relocation is to the entry for this symbol in the
4473 procedure linkage table. */
4477 || ! bfd_link_pic (info
)
4480 || h
->plt
.offset
== (bfd_vma
) -1
4481 || h
->got
.offset
!= (bfd_vma
) -1)
4484 /* Relocation is to the entry for this symbol in the global
4485 offset table extension for the procedure linkage table. */
4488 BFD_ASSERT (sgotplt
!= NULL
);
4489 relocation
= (sgotplt
->output_offset
4490 + (get_plt_index (htab
->plt_info
, h
->plt
.offset
)
4494 relocation
-= GOT_BIAS
;
4497 goto final_link_relocate
;
4502 #ifdef INCLUDE_SHMEDIA
4503 case R_SH_GOT_LOW16
:
4504 case R_SH_GOT_MEDLOW16
:
4505 case R_SH_GOT_MEDHI16
:
4510 /* Relocation is to the entry for this symbol in the global
4514 BFD_ASSERT (sgot
!= NULL
);
4515 check_segment
[0] = check_segment
[1] = -1;
4521 off
= h
->got
.offset
;
4522 #ifdef INCLUDE_SHMEDIA
4523 if (seen_stt_datalabel
)
4525 struct elf_sh_link_hash_entry
*hsh
;
4527 hsh
= (struct elf_sh_link_hash_entry
*)h
;
4528 off
= hsh
->datalabel_got
.offset
;
4531 BFD_ASSERT (off
!= (bfd_vma
) -1);
4533 dyn
= htab
->root
.dynamic_sections_created
;
4534 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
,
4535 bfd_link_pic (info
),
4537 || (bfd_link_pic (info
)
4538 && SYMBOL_REFERENCES_LOCAL (info
, h
))
4539 || ((ELF_ST_VISIBILITY (h
->other
)
4540 || resolved_to_zero
)
4541 && h
->root
.type
== bfd_link_hash_undefweak
))
4543 /* This is actually a static link, or it is a
4544 -Bsymbolic link and the symbol is defined
4545 locally, or the symbol was forced to be local
4546 because of a version file. We must initialize
4547 this entry in the global offset table. Since the
4548 offset must always be a multiple of 4, we use the
4549 least significant bit to record whether we have
4550 initialized it already.
4552 When doing a dynamic link, we create a .rela.got
4553 relocation entry to initialize the value. This
4554 is done in the finish_dynamic_symbol routine. */
4559 bfd_put_32 (output_bfd
, relocation
,
4560 sgot
->contents
+ off
);
4561 #ifdef INCLUDE_SHMEDIA
4562 if (seen_stt_datalabel
)
4564 struct elf_sh_link_hash_entry
*hsh
;
4566 hsh
= (struct elf_sh_link_hash_entry
*)h
;
4567 hsh
->datalabel_got
.offset
|= 1;
4573 /* If we initialize the GOT entry here with a valid
4574 symbol address, also add a fixup. */
4575 if (fdpic_p
&& !bfd_link_pic (info
)
4576 && sh_elf_hash_entry (h
)->got_type
== GOT_NORMAL
4577 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
4578 || h
->root
.type
!= bfd_link_hash_undefweak
))
4579 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4580 sgot
->output_section
->vma
4581 + sgot
->output_offset
4586 relocation
= sh_elf_got_offset (htab
) + off
;
4590 #ifdef INCLUDE_SHMEDIA
4593 BFD_ASSERT (local_got_offsets
!= NULL
4594 && (local_got_offsets
[symtab_hdr
->sh_info
4598 off
= local_got_offsets
[symtab_hdr
->sh_info
4604 BFD_ASSERT (local_got_offsets
!= NULL
4605 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
4607 off
= local_got_offsets
[r_symndx
];
4608 #ifdef INCLUDE_SHMEDIA
4612 /* The offset must always be a multiple of 4. We use
4613 the least significant bit to record whether we have
4614 already generated the necessary reloc. */
4619 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
4621 if (bfd_link_pic (info
))
4623 Elf_Internal_Rela outrel
;
4626 outrel
.r_offset
= (sgot
->output_section
->vma
4627 + sgot
->output_offset
4632 = elf_section_data (sec
->output_section
)->dynindx
;
4633 outrel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
4634 outrel
.r_addend
= relocation
;
4635 outrel
.r_addend
-= sec
->output_section
->vma
;
4639 outrel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
4640 outrel
.r_addend
= relocation
;
4642 loc
= srelgot
->contents
;
4643 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
4644 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
4647 && (sh_elf_local_got_type (input_bfd
) [r_symndx
]
4649 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4650 sgot
->output_section
->vma
4651 + sgot
->output_offset
4654 #ifdef INCLUDE_SHMEDIA
4656 local_got_offsets
[symtab_hdr
->sh_info
+ r_symndx
] |= 1;
4659 local_got_offsets
[r_symndx
] |= 1;
4662 relocation
= sh_elf_got_offset (htab
) + off
;
4666 relocation
-= GOT_BIAS
;
4669 if (r_type
== R_SH_GOT20
)
4671 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4672 input_bfd
, input_section
, contents
,
4677 goto final_link_relocate
;
4681 #ifdef INCLUDE_SHMEDIA
4682 case R_SH_GOTOFF_LOW16
:
4683 case R_SH_GOTOFF_MEDLOW16
:
4684 case R_SH_GOTOFF_MEDHI16
:
4685 case R_SH_GOTOFF_HI16
:
4687 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4688 we place at the start of the .got.plt section. This is the same
4689 as the start of the output .got section, unless there are function
4690 descriptors in front of it. */
4692 BFD_ASSERT (sgotplt
!= NULL
);
4693 check_segment
[0] = got_segment
;
4694 relocation
-= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
4695 + htab
->root
.hgot
->root
.u
.def
.value
;
4698 relocation
-= GOT_BIAS
;
4701 addend
= rel
->r_addend
;
4703 if (r_type
== R_SH_GOTOFF20
)
4705 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4706 input_bfd
, input_section
, contents
,
4711 goto final_link_relocate
;
4714 #ifdef INCLUDE_SHMEDIA
4715 case R_SH_GOTPC_LOW16
:
4716 case R_SH_GOTPC_MEDLOW16
:
4717 case R_SH_GOTPC_MEDHI16
:
4718 case R_SH_GOTPC_HI16
:
4720 /* Use global offset table as symbol value. */
4722 BFD_ASSERT (sgotplt
!= NULL
);
4723 relocation
= sgotplt
->output_section
->vma
+ sgotplt
->output_offset
;
4726 relocation
+= GOT_BIAS
;
4729 addend
= rel
->r_addend
;
4731 goto final_link_relocate
;
4734 #ifdef INCLUDE_SHMEDIA
4735 case R_SH_PLT_LOW16
:
4736 case R_SH_PLT_MEDLOW16
:
4737 case R_SH_PLT_MEDHI16
:
4740 /* Relocation is to the entry for this symbol in the
4741 procedure linkage table. */
4743 /* Resolve a PLT reloc against a local symbol directly,
4744 without using the procedure linkage table. */
4746 goto final_link_relocate
;
4748 /* We don't want to warn on calls to undefined weak symbols,
4749 as calls to them must be protected by non-NULL tests
4750 anyway, and unprotected calls would invoke undefined
4752 if (h
->root
.type
== bfd_link_hash_undefweak
)
4753 check_segment
[0] = check_segment
[1] = -1;
4755 if (h
->forced_local
)
4756 goto final_link_relocate
;
4758 if (h
->plt
.offset
== (bfd_vma
) -1)
4760 /* We didn't make a PLT entry for this symbol. This
4761 happens when statically linking PIC code, or when
4762 using -Bsymbolic. */
4763 goto final_link_relocate
;
4766 BFD_ASSERT (splt
!= NULL
);
4767 check_segment
[1] = plt_segment
;
4768 relocation
= (splt
->output_section
->vma
4769 + splt
->output_offset
4772 #ifdef INCLUDE_SHMEDIA
4776 addend
= rel
->r_addend
;
4778 goto final_link_relocate
;
4780 /* Relocation is to the canonical function descriptor for this
4781 symbol, possibly via the GOT. Initialize the GOT
4782 entry and function descriptor if necessary. */
4783 case R_SH_GOTFUNCDESC
:
4784 case R_SH_GOTFUNCDESC20
:
4788 asection
*reloc_section
;
4789 bfd_vma reloc_offset
;
4790 int reloc_type
= R_SH_FUNCDESC
;
4794 check_segment
[0] = check_segment
[1] = -1;
4796 /* FIXME: See what FRV does for global symbols in the
4797 executable, with --export-dynamic. Do they need ld.so
4798 to allocate official descriptors? See what this code
4804 if (r_type
== R_SH_FUNCDESC
)
4806 reloc_section
= input_section
;
4807 reloc_offset
= rel
->r_offset
;
4811 reloc_section
= sgot
;
4814 reloc_offset
= h
->got
.offset
;
4817 BFD_ASSERT (local_got_offsets
!= NULL
);
4818 reloc_offset
= local_got_offsets
[r_symndx
];
4820 BFD_ASSERT (reloc_offset
!= MINUS_ONE
);
4822 if (reloc_offset
& 1)
4825 goto funcdesc_done_got
;
4829 if (h
&& h
->root
.type
== bfd_link_hash_undefweak
4830 && (SYMBOL_CALLS_LOCAL (info
, h
)
4831 || !htab
->root
.dynamic_sections_created
))
4832 /* Undefined weak symbol which will not be dynamically
4833 resolved later; leave it at zero. */
4834 goto funcdesc_leave_zero
;
4835 else if (SYMBOL_CALLS_LOCAL (info
, h
)
4836 && ! SYMBOL_FUNCDESC_LOCAL (info
, h
))
4838 /* If the symbol needs a non-local function descriptor
4839 but binds locally (i.e., its visibility is
4840 protected), emit a dynamic relocation decayed to
4841 section+offset. This is an optimization; the dynamic
4842 linker would resolve our function descriptor request
4843 to our copy of the function anyway. */
4844 dynindx
= elf_section_data (h
->root
.u
.def
.section
4845 ->output_section
)->dynindx
;
4846 relocation
+= h
->root
.u
.def
.section
->output_offset
4847 + h
->root
.u
.def
.value
;
4849 else if (! SYMBOL_FUNCDESC_LOCAL (info
, h
))
4851 /* If the symbol is dynamic and there will be dynamic
4852 symbol resolution because we are or are linked with a
4853 shared library, emit a FUNCDESC relocation such that
4854 the dynamic linker will allocate the function
4856 BFD_ASSERT (h
->dynindx
!= -1);
4857 dynindx
= h
->dynindx
;
4863 /* Otherwise, we know we have a private function
4864 descriptor, so reference it directly. */
4865 reloc_type
= R_SH_DIR32
;
4866 dynindx
= elf_section_data (htab
->sfuncdesc
4867 ->output_section
)->dynindx
;
4871 offset
= sh_elf_hash_entry (h
)->funcdesc
.offset
;
4872 BFD_ASSERT (offset
!= MINUS_ONE
);
4873 if ((offset
& 1) == 0)
4875 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, h
,
4878 sh_elf_hash_entry (h
)->funcdesc
.offset
|= 1;
4883 union gotref
*local_funcdesc
;
4885 local_funcdesc
= sh_elf_local_funcdesc (input_bfd
);
4886 offset
= local_funcdesc
[r_symndx
].offset
;
4887 BFD_ASSERT (offset
!= MINUS_ONE
);
4888 if ((offset
& 1) == 0)
4890 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, NULL
,
4894 local_funcdesc
[r_symndx
].offset
|= 1;
4898 relocation
= htab
->sfuncdesc
->output_offset
+ (offset
& ~1);
4901 if (!bfd_link_pic (info
) && SYMBOL_FUNCDESC_LOCAL (info
, h
))
4905 if (sh_elf_osec_readonly_p (output_bfd
,
4906 reloc_section
->output_section
))
4909 /* xgettext:c-format */
4910 (_("%pB(%pA+%#" PRIx64
"): "
4911 "cannot emit fixup to `%s' in read-only section"),
4914 (uint64_t) rel
->r_offset
,
4919 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4920 reloc_section
, reloc_offset
);
4922 if (offset
!= (bfd_vma
)-1)
4923 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
,
4925 + reloc_section
->output_section
->vma
4926 + reloc_section
->output_offset
);
4928 else if ((reloc_section
->output_section
->flags
4929 & (SEC_ALLOC
| SEC_LOAD
)) == (SEC_ALLOC
| SEC_LOAD
))
4933 if (sh_elf_osec_readonly_p (output_bfd
,
4934 reloc_section
->output_section
))
4936 info
->callbacks
->warning
4938 _("cannot emit dynamic relocations in read-only section"),
4939 symname
, input_bfd
, reloc_section
, reloc_offset
);
4943 offset
= _bfd_elf_section_offset (output_bfd
, info
,
4944 reloc_section
, reloc_offset
);
4946 if (offset
!= (bfd_vma
)-1)
4947 sh_elf_add_dyn_reloc (output_bfd
, srelgot
,
4949 + reloc_section
->output_section
->vma
4950 + reloc_section
->output_offset
,
4951 reloc_type
, dynindx
, relocation
);
4953 if (r_type
== R_SH_FUNCDESC
)
4961 goto funcdesc_leave_zero
;
4965 if (SYMBOL_FUNCDESC_LOCAL (info
, h
))
4966 relocation
+= htab
->sfuncdesc
->output_section
->vma
;
4967 funcdesc_leave_zero
:
4968 if (r_type
!= R_SH_FUNCDESC
)
4970 bfd_put_32 (output_bfd
, relocation
,
4971 reloc_section
->contents
+ reloc_offset
);
4975 local_got_offsets
[r_symndx
] |= 1;
4979 relocation
= sh_elf_got_offset (htab
) + reloc_offset
;
4981 relocation
-= GOT_BIAS
;
4984 if (r_type
== R_SH_GOTFUNCDESC20
)
4986 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
4987 input_bfd
, input_section
, contents
,
4992 goto final_link_relocate
;
4996 case R_SH_GOTOFFFUNCDESC
:
4997 case R_SH_GOTOFFFUNCDESC20
:
4998 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4999 executable and --export-dynamic. If such symbols get
5000 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
5004 check_segment
[0] = check_segment
[1] = -1;
5006 addend
= rel
->r_addend
;
5008 if (h
&& (h
->root
.type
== bfd_link_hash_undefweak
5009 || !SYMBOL_FUNCDESC_LOCAL (info
, h
)))
5012 /* xgettext:c-format */
5013 (_("%pB(%pA+%#" PRIx64
"): "
5014 "%s relocation against external symbol \"%s\""),
5015 input_bfd
, input_section
, (uint64_t) rel
->r_offset
,
5016 howto
->name
, h
->root
.root
.string
);
5023 /* Otherwise, we know we have a private function
5024 descriptor, so reference it directly. */
5027 offset
= sh_elf_hash_entry (h
)->funcdesc
.offset
;
5028 BFD_ASSERT (offset
!= MINUS_ONE
);
5029 if ((offset
& 1) == 0)
5031 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, h
,
5034 sh_elf_hash_entry (h
)->funcdesc
.offset
|= 1;
5039 union gotref
*local_funcdesc
;
5041 local_funcdesc
= sh_elf_local_funcdesc (input_bfd
);
5042 offset
= local_funcdesc
[r_symndx
].offset
;
5043 BFD_ASSERT (offset
!= MINUS_ONE
);
5044 if ((offset
& 1) == 0)
5046 if (!sh_elf_initialize_funcdesc (output_bfd
, info
, NULL
,
5050 local_funcdesc
[r_symndx
].offset
|= 1;
5054 relocation
= htab
->sfuncdesc
->output_offset
+ (offset
& ~1);
5057 relocation
-= (htab
->root
.hgot
->root
.u
.def
.value
5058 + sgotplt
->output_offset
);
5060 relocation
-= GOT_BIAS
;
5063 if (r_type
== R_SH_GOTOFFFUNCDESC20
)
5065 r
= install_movi20_field (output_bfd
, relocation
+ addend
,
5066 input_bfd
, input_section
, contents
,
5071 goto final_link_relocate
;
5073 case R_SH_LOOP_START
:
5075 static bfd_vma start
, end
;
5077 start
= (relocation
+ rel
->r_addend
5078 - (sec
->output_section
->vma
+ sec
->output_offset
));
5079 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
5080 rel
->r_offset
, sec
, start
, end
);
5084 end
= (relocation
+ rel
->r_addend
5085 - (sec
->output_section
->vma
+ sec
->output_offset
));
5086 r
= sh_elf_reloc_loop (r_type
, input_bfd
, input_section
, contents
,
5087 rel
->r_offset
, sec
, start
, end
);
5091 case R_SH_TLS_GD_32
:
5092 case R_SH_TLS_IE_32
:
5094 check_segment
[0] = check_segment
[1] = -1;
5095 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
5096 got_type
= GOT_UNKNOWN
;
5097 if (h
== NULL
&& local_got_offsets
)
5098 got_type
= sh_elf_local_got_type (input_bfd
) [r_symndx
];
5101 got_type
= sh_elf_hash_entry (h
)->got_type
;
5102 if (! bfd_link_pic (info
)
5103 && (h
->dynindx
== -1
5105 r_type
= R_SH_TLS_LE_32
;
5108 if (r_type
== R_SH_TLS_GD_32
&& got_type
== GOT_TLS_IE
)
5109 r_type
= R_SH_TLS_IE_32
;
5111 if (r_type
== R_SH_TLS_LE_32
)
5114 unsigned short insn
;
5116 if (ELF32_R_TYPE (rel
->r_info
) == R_SH_TLS_GD_32
)
5118 /* GD->LE transition:
5119 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5120 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5121 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5123 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5125 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
5127 offset
= rel
->r_offset
;
5128 BFD_ASSERT (offset
>= 16);
5129 /* Size of GD instructions is 16 or 18. */
5131 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5132 if ((insn
& 0xff00) == 0xc700)
5134 BFD_ASSERT (offset
>= 2);
5136 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5139 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
5140 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
5141 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
5142 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
5143 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
5144 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
5145 BFD_ASSERT (insn
== 0x310c);
5146 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
5147 BFD_ASSERT (insn
== 0x410b);
5148 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
5149 BFD_ASSERT (insn
== 0x34cc);
5151 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 2);
5152 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 4);
5153 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
5154 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
5155 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
5161 /* IE->LE transition:
5162 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5163 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5165 mov.l .Ln,rM; stc gbr,rN; nop; ...;
5168 offset
= rel
->r_offset
;
5169 BFD_ASSERT (offset
>= 16);
5170 /* Size of IE instructions is 10 or 12. */
5172 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5173 if ((insn
& 0xf0ff) == 0x0012)
5175 BFD_ASSERT (offset
>= 2);
5177 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5180 BFD_ASSERT ((insn
& 0xff00) == 0xd000);
5181 target
= insn
& 0x00ff;
5182 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
5183 BFD_ASSERT ((insn
& 0xf0ff) == 0x0012);
5184 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
5185 BFD_ASSERT ((insn
& 0xf0ff) == 0x00ce);
5186 insn
= 0xd000 | (insn
& 0x0f00) | target
;
5187 bfd_put_16 (output_bfd
, insn
, contents
+ offset
+ 0);
5188 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
5191 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
5192 contents
+ rel
->r_offset
);
5196 if (sgot
== NULL
|| sgotplt
== NULL
)
5200 off
= h
->got
.offset
;
5203 if (local_got_offsets
== NULL
)
5206 off
= local_got_offsets
[r_symndx
];
5209 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
5210 if (r_type
== R_SH_TLS_IE_32
5211 && ! htab
->root
.dynamic_sections_created
)
5214 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
5215 sgot
->contents
+ off
);
5216 bfd_put_32 (output_bfd
, sh_elf_got_offset (htab
) + off
,
5217 contents
+ rel
->r_offset
);
5225 Elf_Internal_Rela outrel
;
5229 outrel
.r_offset
= (sgot
->output_section
->vma
5230 + sgot
->output_offset
+ off
);
5232 if (h
== NULL
|| h
->dynindx
== -1)
5237 dr_type
= (r_type
== R_SH_TLS_GD_32
? R_SH_TLS_DTPMOD32
:
5239 if (dr_type
== R_SH_TLS_TPOFF32
&& indx
== 0)
5240 outrel
.r_addend
= relocation
- dtpoff_base (info
);
5242 outrel
.r_addend
= 0;
5243 outrel
.r_info
= ELF32_R_INFO (indx
, dr_type
);
5244 loc
= srelgot
->contents
;
5245 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5246 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5248 if (r_type
== R_SH_TLS_GD_32
)
5252 bfd_put_32 (output_bfd
,
5253 relocation
- dtpoff_base (info
),
5254 sgot
->contents
+ off
+ 4);
5258 outrel
.r_info
= ELF32_R_INFO (indx
,
5260 outrel
.r_offset
+= 4;
5261 outrel
.r_addend
= 0;
5262 srelgot
->reloc_count
++;
5263 loc
+= sizeof (Elf32_External_Rela
);
5264 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5271 local_got_offsets
[r_symndx
] |= 1;
5274 if (off
>= (bfd_vma
) -2)
5277 if (r_type
== (int) ELF32_R_TYPE (rel
->r_info
))
5278 relocation
= sh_elf_got_offset (htab
) + off
;
5282 unsigned short insn
;
5284 /* GD->IE transition:
5285 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5286 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5287 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5289 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5290 nop; nop; bra 3f; nop; .align 2;
5291 1: .long x@TPOFF; 2:...; 3:. */
5293 offset
= rel
->r_offset
;
5294 BFD_ASSERT (offset
>= 16);
5295 /* Size of GD instructions is 16 or 18. */
5297 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5298 if ((insn
& 0xff00) == 0xc700)
5300 BFD_ASSERT (offset
>= 2);
5302 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5305 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
5307 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
5308 bfd_put_16 (output_bfd
, insn
& 0xf0ff, contents
+ offset
);
5310 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
5311 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
5312 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
5313 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
5314 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
5315 BFD_ASSERT (insn
== 0x310c);
5316 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
5317 BFD_ASSERT (insn
== 0x410b);
5318 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
5319 BFD_ASSERT (insn
== 0x34cc);
5321 bfd_put_16 (output_bfd
, 0x0412, contents
+ offset
+ 2);
5322 bfd_put_16 (output_bfd
, 0x00ce, contents
+ offset
+ 4);
5323 bfd_put_16 (output_bfd
, 0x304c, contents
+ offset
+ 6);
5324 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
5325 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
5327 bfd_put_32 (output_bfd
, sh_elf_got_offset (htab
) + off
,
5328 contents
+ rel
->r_offset
);
5333 addend
= rel
->r_addend
;
5335 goto final_link_relocate
;
5337 case R_SH_TLS_LD_32
:
5339 check_segment
[0] = check_segment
[1] = -1;
5340 if (! bfd_link_pic (info
))
5343 unsigned short insn
;
5345 /* LD->LE transition:
5346 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5347 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5348 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5350 stc gbr,r0; nop; nop; nop;
5351 nop; nop; bra 3f; ...; 3:. */
5353 offset
= rel
->r_offset
;
5354 BFD_ASSERT (offset
>= 16);
5355 /* Size of LD instructions is 16 or 18. */
5357 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5358 if ((insn
& 0xff00) == 0xc700)
5360 BFD_ASSERT (offset
>= 2);
5362 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 0);
5365 BFD_ASSERT ((insn
& 0xff00) == 0xd400);
5366 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 2);
5367 BFD_ASSERT ((insn
& 0xff00) == 0xc700);
5368 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 4);
5369 BFD_ASSERT ((insn
& 0xff00) == 0xd100);
5370 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 6);
5371 BFD_ASSERT (insn
== 0x310c);
5372 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 8);
5373 BFD_ASSERT (insn
== 0x410b);
5374 insn
= bfd_get_16 (input_bfd
, contents
+ offset
+ 10);
5375 BFD_ASSERT (insn
== 0x34cc);
5377 bfd_put_16 (output_bfd
, 0x0012, contents
+ offset
+ 0);
5378 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 2);
5379 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 4);
5380 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 6);
5381 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 8);
5382 bfd_put_16 (output_bfd
, 0x0009, contents
+ offset
+ 10);
5387 if (sgot
== NULL
|| sgotplt
== NULL
)
5390 off
= htab
->tls_ldm_got
.offset
;
5395 Elf_Internal_Rela outrel
;
5398 outrel
.r_offset
= (sgot
->output_section
->vma
5399 + sgot
->output_offset
+ off
);
5400 outrel
.r_addend
= 0;
5401 outrel
.r_info
= ELF32_R_INFO (0, R_SH_TLS_DTPMOD32
);
5402 loc
= srelgot
->contents
;
5403 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5404 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5405 htab
->tls_ldm_got
.offset
|= 1;
5408 relocation
= sh_elf_got_offset (htab
) + off
;
5409 addend
= rel
->r_addend
;
5411 goto final_link_relocate
;
5413 case R_SH_TLS_LDO_32
:
5414 check_segment
[0] = check_segment
[1] = -1;
5415 if (! bfd_link_pic (info
))
5416 relocation
= tpoff (info
, relocation
);
5418 relocation
-= dtpoff_base (info
);
5420 addend
= rel
->r_addend
;
5421 goto final_link_relocate
;
5423 case R_SH_TLS_LE_32
:
5426 Elf_Internal_Rela outrel
;
5429 check_segment
[0] = check_segment
[1] = -1;
5431 if (!bfd_link_dll (info
))
5433 relocation
= tpoff (info
, relocation
);
5434 addend
= rel
->r_addend
;
5435 goto final_link_relocate
;
5440 sreloc
= _bfd_elf_get_dynamic_reloc_section
5441 (input_bfd
, input_section
, /*rela?*/ TRUE
);
5446 if (h
== NULL
|| h
->dynindx
== -1)
5451 outrel
.r_offset
= (input_section
->output_section
->vma
5452 + input_section
->output_offset
5454 outrel
.r_info
= ELF32_R_INFO (indx
, R_SH_TLS_TPOFF32
);
5456 outrel
.r_addend
= relocation
- dtpoff_base (info
);
5458 outrel
.r_addend
= 0;
5460 loc
= sreloc
->contents
;
5461 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rela
);
5462 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
, loc
);
5468 if (fdpic_p
&& check_segment
[0] != (unsigned) -1
5469 && check_segment
[0] != check_segment
[1])
5471 /* We don't want duplicate errors for undefined symbols. */
5472 if (!h
|| h
->root
.type
!= bfd_link_hash_undefined
)
5474 if (bfd_link_pic (info
))
5476 info
->callbacks
->einfo
5477 /* xgettext:c-format */
5478 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5479 input_bfd
, input_section
, rel
->r_offset
, symname
);
5483 info
->callbacks
->einfo
5484 /* xgettext:c-format */
5485 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5486 input_bfd
, input_section
, rel
->r_offset
, symname
);
5489 elf_elfheader (output_bfd
)->e_flags
|= EF_SH_PIC
;
5492 if (r
!= bfd_reloc_ok
)
5497 case bfd_reloc_outofrange
:
5499 case bfd_reloc_overflow
:
5507 name
= (bfd_elf_string_from_elf_section
5508 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
5512 name
= bfd_section_name (input_bfd
, sec
);
5514 (*info
->callbacks
->reloc_overflow
)
5515 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
5516 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
5526 /* This is a version of bfd_generic_get_relocated_section_contents
5527 which uses sh_elf_relocate_section. */
5530 sh_elf_get_relocated_section_contents (bfd
*output_bfd
,
5531 struct bfd_link_info
*link_info
,
5532 struct bfd_link_order
*link_order
,
5534 bfd_boolean relocatable
,
5537 Elf_Internal_Shdr
*symtab_hdr
;
5538 asection
*input_section
= link_order
->u
.indirect
.section
;
5539 bfd
*input_bfd
= input_section
->owner
;
5540 asection
**sections
= NULL
;
5541 Elf_Internal_Rela
*internal_relocs
= NULL
;
5542 Elf_Internal_Sym
*isymbuf
= NULL
;
5544 /* We only need to handle the case of relaxing, or of having a
5545 particular set of section contents, specially. */
5547 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
5548 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
5553 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
5555 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
5556 (size_t) input_section
->size
);
5558 if ((input_section
->flags
& SEC_RELOC
) != 0
5559 && input_section
->reloc_count
> 0)
5562 Elf_Internal_Sym
*isym
, *isymend
;
5565 internal_relocs
= (_bfd_elf_link_read_relocs
5566 (input_bfd
, input_section
, NULL
,
5567 (Elf_Internal_Rela
*) NULL
, FALSE
));
5568 if (internal_relocs
== NULL
)
5571 if (symtab_hdr
->sh_info
!= 0)
5573 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
5574 if (isymbuf
== NULL
)
5575 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
5576 symtab_hdr
->sh_info
, 0,
5578 if (isymbuf
== NULL
)
5582 amt
= symtab_hdr
->sh_info
;
5583 amt
*= sizeof (asection
*);
5584 sections
= (asection
**) bfd_malloc (amt
);
5585 if (sections
== NULL
&& amt
!= 0)
5588 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
5589 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
5593 if (isym
->st_shndx
== SHN_UNDEF
)
5594 isec
= bfd_und_section_ptr
;
5595 else if (isym
->st_shndx
== SHN_ABS
)
5596 isec
= bfd_abs_section_ptr
;
5597 else if (isym
->st_shndx
== SHN_COMMON
)
5598 isec
= bfd_com_section_ptr
;
5600 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
5605 if (! sh_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
5606 input_section
, data
, internal_relocs
,
5610 if (sections
!= NULL
)
5613 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
5615 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
5616 free (internal_relocs
);
5622 if (sections
!= NULL
)
5625 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
5627 if (internal_relocs
!= NULL
5628 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
5629 free (internal_relocs
);
5633 /* Return the base VMA address which should be subtracted from real addresses
5634 when resolving @dtpoff relocation.
5635 This is PT_TLS segment p_vaddr. */
5638 dtpoff_base (struct bfd_link_info
*info
)
5640 /* If tls_sec is NULL, we should have signalled an error already. */
5641 if (elf_hash_table (info
)->tls_sec
== NULL
)
5643 return elf_hash_table (info
)->tls_sec
->vma
;
5646 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5649 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
5651 /* If tls_sec is NULL, we should have signalled an error already. */
5652 if (elf_hash_table (info
)->tls_sec
== NULL
)
5654 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5655 structure which has 2 pointer fields. */
5656 return (address
- elf_hash_table (info
)->tls_sec
->vma
5657 + align_power ((bfd_vma
) 8,
5658 elf_hash_table (info
)->tls_sec
->alignment_power
));
5662 sh_elf_gc_mark_hook (asection
*sec
,
5663 struct bfd_link_info
*info
,
5664 Elf_Internal_Rela
*rel
,
5665 struct elf_link_hash_entry
*h
,
5666 Elf_Internal_Sym
*sym
)
5669 switch (ELF32_R_TYPE (rel
->r_info
))
5671 case R_SH_GNU_VTINHERIT
:
5672 case R_SH_GNU_VTENTRY
:
5676 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
5679 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5682 sh_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
5683 struct elf_link_hash_entry
*dir
,
5684 struct elf_link_hash_entry
*ind
)
5686 struct elf_sh_link_hash_entry
*edir
, *eind
;
5688 edir
= (struct elf_sh_link_hash_entry
*) dir
;
5689 eind
= (struct elf_sh_link_hash_entry
*) ind
;
5691 if (eind
->dyn_relocs
!= NULL
)
5693 if (edir
->dyn_relocs
!= NULL
)
5695 struct elf_dyn_relocs
**pp
;
5696 struct elf_dyn_relocs
*p
;
5698 /* Add reloc counts against the indirect sym to the direct sym
5699 list. Merge any entries against the same section. */
5700 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
5702 struct elf_dyn_relocs
*q
;
5704 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
5705 if (q
->sec
== p
->sec
)
5707 q
->pc_count
+= p
->pc_count
;
5708 q
->count
+= p
->count
;
5715 *pp
= edir
->dyn_relocs
;
5718 edir
->dyn_relocs
= eind
->dyn_relocs
;
5719 eind
->dyn_relocs
= NULL
;
5721 edir
->gotplt_refcount
= eind
->gotplt_refcount
;
5722 eind
->gotplt_refcount
= 0;
5723 #ifdef INCLUDE_SHMEDIA
5724 edir
->datalabel_got
.refcount
+= eind
->datalabel_got
.refcount
;
5725 eind
->datalabel_got
.refcount
= 0;
5727 edir
->funcdesc
.refcount
+= eind
->funcdesc
.refcount
;
5728 eind
->funcdesc
.refcount
= 0;
5729 edir
->abs_funcdesc_refcount
+= eind
->abs_funcdesc_refcount
;
5730 eind
->abs_funcdesc_refcount
= 0;
5732 if (ind
->root
.type
== bfd_link_hash_indirect
5733 && dir
->got
.refcount
<= 0)
5735 edir
->got_type
= eind
->got_type
;
5736 eind
->got_type
= GOT_UNKNOWN
;
5739 if (ind
->root
.type
!= bfd_link_hash_indirect
5740 && dir
->dynamic_adjusted
)
5742 /* If called to transfer flags for a weakdef during processing
5743 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5744 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5745 if (dir
->versioned
!= versioned_hidden
)
5746 dir
->ref_dynamic
|= ind
->ref_dynamic
;
5747 dir
->ref_regular
|= ind
->ref_regular
;
5748 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
5749 dir
->needs_plt
|= ind
->needs_plt
;
5752 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
5756 sh_elf_optimized_tls_reloc (struct bfd_link_info
*info
, int r_type
,
5759 if (bfd_link_pic (info
))
5764 case R_SH_TLS_GD_32
:
5765 case R_SH_TLS_IE_32
:
5767 return R_SH_TLS_LE_32
;
5768 return R_SH_TLS_IE_32
;
5769 case R_SH_TLS_LD_32
:
5770 return R_SH_TLS_LE_32
;
5776 /* Look through the relocs for a section during the first phase.
5777 Since we don't do .gots or .plts, we just need to consider the
5778 virtual table relocs for gc. */
5781 sh_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
5782 const Elf_Internal_Rela
*relocs
)
5784 Elf_Internal_Shdr
*symtab_hdr
;
5785 struct elf_link_hash_entry
**sym_hashes
;
5786 struct elf_sh_link_hash_table
*htab
;
5787 const Elf_Internal_Rela
*rel
;
5788 const Elf_Internal_Rela
*rel_end
;
5790 unsigned int r_type
;
5791 enum got_type got_type
, old_got_type
;
5795 if (bfd_link_relocatable (info
))
5798 /* Don't do anything special with non-loaded, non-alloced sections.
5799 In particular, any relocs in such sections should not affect GOT
5800 and PLT reference counting (ie. we don't allow them to create GOT
5801 or PLT entries), there's no possibility or desire to optimize TLS
5802 relocs, and there's not much point in propagating relocs to shared
5803 libs that the dynamic linker won't relocate. */
5804 if ((sec
->flags
& SEC_ALLOC
) == 0)
5807 BFD_ASSERT (is_sh_elf (abfd
));
5809 symtab_hdr
= &elf_symtab_hdr (abfd
);
5810 sym_hashes
= elf_sym_hashes (abfd
);
5812 htab
= sh_elf_hash_table (info
);
5816 rel_end
= relocs
+ sec
->reloc_count
;
5817 for (rel
= relocs
; rel
< rel_end
; rel
++)
5819 struct elf_link_hash_entry
*h
;
5820 unsigned long r_symndx
;
5821 #ifdef INCLUDE_SHMEDIA
5822 int seen_stt_datalabel
= 0;
5825 r_symndx
= ELF32_R_SYM (rel
->r_info
);
5826 r_type
= ELF32_R_TYPE (rel
->r_info
);
5828 if (r_symndx
< symtab_hdr
->sh_info
)
5832 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
5833 while (h
->root
.type
== bfd_link_hash_indirect
5834 || h
->root
.type
== bfd_link_hash_warning
)
5836 #ifdef INCLUDE_SHMEDIA
5837 seen_stt_datalabel
|= h
->type
== STT_DATALABEL
;
5839 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5843 r_type
= sh_elf_optimized_tls_reloc (info
, r_type
, h
== NULL
);
5844 if (! bfd_link_pic (info
)
5845 && r_type
== R_SH_TLS_IE_32
5847 && h
->root
.type
!= bfd_link_hash_undefined
5848 && h
->root
.type
!= bfd_link_hash_undefweak
5849 && (h
->dynindx
== -1
5851 r_type
= R_SH_TLS_LE_32
;
5856 case R_SH_GOTOFFFUNCDESC
:
5857 case R_SH_GOTOFFFUNCDESC20
:
5859 case R_SH_GOTFUNCDESC
:
5860 case R_SH_GOTFUNCDESC20
:
5863 if (h
->dynindx
== -1)
5864 switch (ELF_ST_VISIBILITY (h
->other
))
5870 bfd_elf_link_record_dynamic_symbol (info
, h
);
5877 /* Some relocs require a global offset table. */
5878 if (htab
->root
.sgot
== NULL
)
5883 /* This may require an rofixup. */
5893 case R_SH_GOTFUNCDESC
:
5894 case R_SH_GOTFUNCDESC20
:
5895 case R_SH_GOTOFFFUNCDESC
:
5896 case R_SH_GOTOFFFUNCDESC20
:
5898 #ifdef INCLUDE_SHMEDIA
5899 case R_SH_GOTPLT_LOW16
:
5900 case R_SH_GOTPLT_MEDLOW16
:
5901 case R_SH_GOTPLT_MEDHI16
:
5902 case R_SH_GOTPLT_HI16
:
5903 case R_SH_GOTPLT10BY4
:
5904 case R_SH_GOTPLT10BY8
:
5905 case R_SH_GOT_LOW16
:
5906 case R_SH_GOT_MEDLOW16
:
5907 case R_SH_GOT_MEDHI16
:
5911 case R_SH_GOTOFF_LOW16
:
5912 case R_SH_GOTOFF_MEDLOW16
:
5913 case R_SH_GOTOFF_MEDHI16
:
5914 case R_SH_GOTOFF_HI16
:
5915 case R_SH_GOTPC_LOW16
:
5916 case R_SH_GOTPC_MEDLOW16
:
5917 case R_SH_GOTPC_MEDHI16
:
5918 case R_SH_GOTPC_HI16
:
5920 case R_SH_TLS_GD_32
:
5921 case R_SH_TLS_LD_32
:
5922 case R_SH_TLS_IE_32
:
5923 if (htab
->root
.dynobj
== NULL
)
5924 htab
->root
.dynobj
= abfd
;
5925 if (!create_got_section (htab
->root
.dynobj
, info
))
5936 /* This relocation describes the C++ object vtable hierarchy.
5937 Reconstruct it for later use during GC. */
5938 case R_SH_GNU_VTINHERIT
:
5939 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
5943 /* This relocation describes which C++ vtable entries are actually
5944 used. Record for later use during GC. */
5945 case R_SH_GNU_VTENTRY
:
5946 BFD_ASSERT (h
!= NULL
);
5948 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
5952 case R_SH_TLS_IE_32
:
5953 if (bfd_link_pic (info
))
5954 info
->flags
|= DF_STATIC_TLS
;
5958 case R_SH_TLS_GD_32
:
5961 #ifdef INCLUDE_SHMEDIA
5962 case R_SH_GOT_LOW16
:
5963 case R_SH_GOT_MEDLOW16
:
5964 case R_SH_GOT_MEDHI16
:
5969 case R_SH_GOTFUNCDESC
:
5970 case R_SH_GOTFUNCDESC20
:
5974 got_type
= GOT_NORMAL
;
5976 case R_SH_TLS_GD_32
:
5977 got_type
= GOT_TLS_GD
;
5979 case R_SH_TLS_IE_32
:
5980 got_type
= GOT_TLS_IE
;
5982 case R_SH_GOTFUNCDESC
:
5983 case R_SH_GOTFUNCDESC20
:
5984 got_type
= GOT_FUNCDESC
;
5990 #ifdef INCLUDE_SHMEDIA
5991 if (seen_stt_datalabel
)
5993 struct elf_sh_link_hash_entry
*eh
5994 = (struct elf_sh_link_hash_entry
*) h
;
5996 eh
->datalabel_got
.refcount
+= 1;
6000 h
->got
.refcount
+= 1;
6001 old_got_type
= sh_elf_hash_entry (h
)->got_type
;
6005 bfd_signed_vma
*local_got_refcounts
;
6007 /* This is a global offset table entry for a local
6009 local_got_refcounts
= elf_local_got_refcounts (abfd
);
6010 if (local_got_refcounts
== NULL
)
6014 size
= symtab_hdr
->sh_info
;
6015 size
*= sizeof (bfd_signed_vma
);
6016 #ifdef INCLUDE_SHMEDIA
6017 /* Reserve space for both the datalabel and
6018 codelabel local GOT offsets. */
6021 size
+= symtab_hdr
->sh_info
;
6022 local_got_refcounts
= ((bfd_signed_vma
*)
6023 bfd_zalloc (abfd
, size
));
6024 if (local_got_refcounts
== NULL
)
6026 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
6027 #ifdef INCLUDE_SHMEDIA
6028 /* Take care of both the datalabel and codelabel local
6030 sh_elf_local_got_type (abfd
)
6031 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
6033 sh_elf_local_got_type (abfd
)
6034 = (char *) (local_got_refcounts
+ symtab_hdr
->sh_info
);
6037 #ifdef INCLUDE_SHMEDIA
6038 if (rel
->r_addend
& 1)
6039 local_got_refcounts
[symtab_hdr
->sh_info
+ r_symndx
] += 1;
6042 local_got_refcounts
[r_symndx
] += 1;
6043 old_got_type
= sh_elf_local_got_type (abfd
) [r_symndx
];
6046 /* If a TLS symbol is accessed using IE at least once,
6047 there is no point to use dynamic model for it. */
6048 if (old_got_type
!= got_type
&& old_got_type
!= GOT_UNKNOWN
6049 && (old_got_type
!= GOT_TLS_GD
|| got_type
!= GOT_TLS_IE
))
6051 if (old_got_type
== GOT_TLS_IE
&& got_type
== GOT_TLS_GD
)
6052 got_type
= GOT_TLS_IE
;
6055 if ((old_got_type
== GOT_FUNCDESC
|| got_type
== GOT_FUNCDESC
)
6056 && (old_got_type
== GOT_NORMAL
|| got_type
== GOT_NORMAL
))
6058 /* xgettext:c-format */
6059 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
6060 abfd
, h
->root
.root
.string
);
6061 else if (old_got_type
== GOT_FUNCDESC
6062 || got_type
== GOT_FUNCDESC
)
6064 /* xgettext:c-format */
6065 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
6066 abfd
, h
->root
.root
.string
);
6069 /* xgettext:c-format */
6070 (_("%pB: `%s' accessed both as normal and thread local symbol"),
6071 abfd
, h
->root
.root
.string
);
6076 if (old_got_type
!= got_type
)
6079 sh_elf_hash_entry (h
)->got_type
= got_type
;
6081 sh_elf_local_got_type (abfd
) [r_symndx
] = got_type
;
6086 case R_SH_TLS_LD_32
:
6087 sh_elf_hash_table(info
)->tls_ldm_got
.refcount
+= 1;
6091 case R_SH_GOTOFFFUNCDESC
:
6092 case R_SH_GOTOFFFUNCDESC20
:
6096 (_("%pB: Function descriptor relocation with non-zero addend"),
6103 union gotref
*local_funcdesc
;
6105 /* We need a function descriptor for a local symbol. */
6106 local_funcdesc
= sh_elf_local_funcdesc (abfd
);
6107 if (local_funcdesc
== NULL
)
6111 size
= symtab_hdr
->sh_info
* sizeof (union gotref
);
6112 #ifdef INCLUDE_SHMEDIA
6113 /* Count datalabel local GOT. */
6116 local_funcdesc
= (union gotref
*) bfd_zalloc (abfd
, size
);
6117 if (local_funcdesc
== NULL
)
6119 sh_elf_local_funcdesc (abfd
) = local_funcdesc
;
6121 local_funcdesc
[r_symndx
].refcount
+= 1;
6123 if (r_type
== R_SH_FUNCDESC
)
6125 if (!bfd_link_pic (info
))
6126 htab
->srofixup
->size
+= 4;
6128 htab
->root
.srelgot
->size
+= sizeof (Elf32_External_Rela
);
6133 sh_elf_hash_entry (h
)->funcdesc
.refcount
++;
6134 if (r_type
== R_SH_FUNCDESC
)
6135 sh_elf_hash_entry (h
)->abs_funcdesc_refcount
++;
6137 /* If there is a function descriptor reference, then
6138 there should not be any non-FDPIC references. */
6139 old_got_type
= sh_elf_hash_entry (h
)->got_type
;
6140 if (old_got_type
!= GOT_FUNCDESC
&& old_got_type
!= GOT_UNKNOWN
)
6142 if (old_got_type
== GOT_NORMAL
)
6144 /* xgettext:c-format */
6145 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
6146 abfd
, h
->root
.root
.string
);
6149 /* xgettext:c-format */
6150 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
6151 abfd
, h
->root
.root
.string
);
6157 #ifdef INCLUDE_SHMEDIA
6158 case R_SH_GOTPLT_LOW16
:
6159 case R_SH_GOTPLT_MEDLOW16
:
6160 case R_SH_GOTPLT_MEDHI16
:
6161 case R_SH_GOTPLT_HI16
:
6162 case R_SH_GOTPLT10BY4
:
6163 case R_SH_GOTPLT10BY8
:
6165 /* If this is a local symbol, we resolve it directly without
6166 creating a procedure linkage table entry. */
6170 || ! bfd_link_pic (info
)
6172 || h
->dynindx
== -1)
6176 h
->plt
.refcount
+= 1;
6177 ((struct elf_sh_link_hash_entry
*) h
)->gotplt_refcount
+= 1;
6182 #ifdef INCLUDE_SHMEDIA
6183 case R_SH_PLT_LOW16
:
6184 case R_SH_PLT_MEDLOW16
:
6185 case R_SH_PLT_MEDHI16
:
6188 /* This symbol requires a procedure linkage table entry. We
6189 actually build the entry in adjust_dynamic_symbol,
6190 because this might be a case of linking PIC code which is
6191 never referenced by a dynamic object, in which case we
6192 don't need to generate a procedure linkage table entry
6195 /* If this is a local symbol, we resolve it directly without
6196 creating a procedure linkage table entry. */
6200 if (h
->forced_local
)
6204 h
->plt
.refcount
+= 1;
6209 #ifdef INCLUDE_SHMEDIA
6210 case R_SH_IMM_LOW16_PCREL
:
6211 case R_SH_IMM_MEDLOW16_PCREL
:
6212 case R_SH_IMM_MEDHI16_PCREL
:
6213 case R_SH_IMM_HI16_PCREL
:
6215 if (h
!= NULL
&& ! bfd_link_pic (info
))
6218 h
->plt
.refcount
+= 1;
6221 /* If we are creating a shared library, and this is a reloc
6222 against a global symbol, or a non PC relative reloc
6223 against a local symbol, then we need to copy the reloc
6224 into the shared library. However, if we are linking with
6225 -Bsymbolic, we do not need to copy a reloc against a
6226 global symbol which is defined in an object we are
6227 including in the link (i.e., DEF_REGULAR is set). At
6228 this point we have not seen all the input files, so it is
6229 possible that DEF_REGULAR is not set now but will be set
6230 later (it is never cleared). We account for that
6231 possibility below by storing information in the
6232 dyn_relocs field of the hash table entry. A similar
6233 situation occurs when creating shared libraries and symbol
6234 visibility changes render the symbol local.
6236 If on the other hand, we are creating an executable, we
6237 may need to keep relocations for symbols satisfied by a
6238 dynamic library if we manage to avoid copy relocs for the
6240 if ((bfd_link_pic (info
)
6241 && (sec
->flags
& SEC_ALLOC
) != 0
6242 && (r_type
!= R_SH_REL32
6244 && (! info
->symbolic
6245 || h
->root
.type
== bfd_link_hash_defweak
6246 || !h
->def_regular
))))
6247 || (! bfd_link_pic (info
)
6248 && (sec
->flags
& SEC_ALLOC
) != 0
6250 && (h
->root
.type
== bfd_link_hash_defweak
6251 || !h
->def_regular
)))
6253 struct elf_dyn_relocs
*p
;
6254 struct elf_dyn_relocs
**head
;
6256 if (htab
->root
.dynobj
== NULL
)
6257 htab
->root
.dynobj
= abfd
;
6259 /* When creating a shared object, we must copy these
6260 reloc types into the output file. We create a reloc
6261 section in dynobj and make room for this reloc. */
6264 sreloc
= _bfd_elf_make_dynamic_reloc_section
6265 (sec
, htab
->root
.dynobj
, 2, abfd
, /*rela?*/ TRUE
);
6271 /* If this is a global symbol, we count the number of
6272 relocations we need for this symbol. */
6274 head
= &((struct elf_sh_link_hash_entry
*) h
)->dyn_relocs
;
6277 /* Track dynamic relocs needed for local syms too. */
6280 Elf_Internal_Sym
*isym
;
6282 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
6287 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
6291 vpp
= &elf_section_data (s
)->local_dynrel
;
6292 head
= (struct elf_dyn_relocs
**) vpp
;
6296 if (p
== NULL
|| p
->sec
!= sec
)
6298 bfd_size_type amt
= sizeof (*p
);
6299 p
= bfd_alloc (htab
->root
.dynobj
, amt
);
6310 if (r_type
== R_SH_REL32
6311 #ifdef INCLUDE_SHMEDIA
6312 || r_type
== R_SH_IMM_LOW16_PCREL
6313 || r_type
== R_SH_IMM_MEDLOW16_PCREL
6314 || r_type
== R_SH_IMM_MEDHI16_PCREL
6315 || r_type
== R_SH_IMM_HI16_PCREL
6321 /* Allocate the fixup regardless of whether we need a relocation.
6322 If we end up generating the relocation, we'll unallocate the
6324 if (htab
->fdpic_p
&& !bfd_link_pic (info
)
6325 && r_type
== R_SH_DIR32
6326 && (sec
->flags
& SEC_ALLOC
) != 0)
6327 htab
->srofixup
->size
+= 4;
6330 case R_SH_TLS_LE_32
:
6331 if (bfd_link_dll (info
))
6334 (_("%pB: TLS local exec code cannot be linked into shared objects"),
6341 case R_SH_TLS_LDO_32
:
6342 /* Nothing to do. */
6353 #ifndef sh_elf_set_mach_from_flags
6354 static unsigned int sh_ef_bfd_table
[] = { EF_SH_BFD_TABLE
};
6357 sh_elf_set_mach_from_flags (bfd
*abfd
)
6359 flagword flags
= elf_elfheader (abfd
)->e_flags
& EF_SH_MACH_MASK
;
6361 if (flags
>= ARRAY_SIZE (sh_ef_bfd_table
))
6364 if (sh_ef_bfd_table
[flags
] == 0)
6367 bfd_default_set_arch_mach (abfd
, bfd_arch_sh
, sh_ef_bfd_table
[flags
]);
6373 /* Reverse table lookup for sh_ef_bfd_table[].
6374 Given a bfd MACH value from archures.c
6375 return the equivalent ELF flags from the table.
6376 Return -1 if no match is found. */
6379 sh_elf_get_flags_from_mach (unsigned long mach
)
6381 int i
= ARRAY_SIZE (sh_ef_bfd_table
) - 1;
6384 if (sh_ef_bfd_table
[i
] == mach
)
6387 /* shouldn't get here */
6392 #endif /* not sh_elf_set_mach_from_flags */
6394 #ifndef sh_elf_copy_private_data
6395 /* Copy backend specific data from one object module to another */
6398 sh_elf_copy_private_data (bfd
* ibfd
, bfd
* obfd
)
6400 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
6403 if (! _bfd_elf_copy_private_bfd_data (ibfd
, obfd
))
6406 return sh_elf_set_mach_from_flags (obfd
);
6408 #endif /* not sh_elf_copy_private_data */
6410 #ifndef sh_elf_merge_private_data
6412 /* This function returns the ELF architecture number that
6413 corresponds to the given arch_sh* flags. */
6416 sh_find_elf_flags (unsigned int arch_set
)
6418 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
6419 unsigned long bfd_mach
= sh_get_bfd_mach_from_arch_set (arch_set
);
6421 return sh_elf_get_flags_from_mach (bfd_mach
);
6424 /* Merge the architecture type of two BFD files, such that the
6425 resultant architecture supports all the features required
6426 by the two input BFDs.
6427 If the input BFDs are multually incompatible - i.e. one uses
6428 DSP while the other uses FPU - or there is no known architecture
6429 that fits the requirements then an error is emitted. */
6432 sh_merge_bfd_arch (bfd
*ibfd
, struct bfd_link_info
*info
)
6434 bfd
*obfd
= info
->output_bfd
;
6435 unsigned int old_arch
, new_arch
, merged_arch
;
6437 if (! _bfd_generic_verify_endian_match (ibfd
, info
))
6440 old_arch
= sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd
));
6441 new_arch
= sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd
));
6443 merged_arch
= SH_MERGE_ARCH_SET (old_arch
, new_arch
);
6445 if (!SH_VALID_CO_ARCH_SET (merged_arch
))
6448 /* xgettext:c-format */
6449 (_("%pB: uses %s instructions while previous modules "
6450 "use %s instructions"),
6452 SH_ARCH_SET_HAS_DSP (new_arch
) ? "dsp" : "floating point",
6453 SH_ARCH_SET_HAS_DSP (new_arch
) ? "floating point" : "dsp");
6454 bfd_set_error (bfd_error_bad_value
);
6457 else if (!SH_VALID_ARCH_SET (merged_arch
))
6460 /* xgettext:c-format */
6461 (_("internal error: merge of architecture '%s' with "
6462 "architecture '%s' produced unknown architecture"),
6463 bfd_printable_name (obfd
),
6464 bfd_printable_name (ibfd
));
6465 bfd_set_error (bfd_error_bad_value
);
6469 bfd_default_set_arch_mach (obfd
, bfd_arch_sh
,
6470 sh_get_bfd_mach_from_arch_set (merged_arch
));
6475 /* This routine initialises the elf flags when required and
6476 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
6479 sh_elf_merge_private_data (bfd
*ibfd
, struct bfd_link_info
*info
)
6481 bfd
*obfd
= info
->output_bfd
;
6483 if (! is_sh_elf (ibfd
) || ! is_sh_elf (obfd
))
6486 if (! elf_flags_init (obfd
))
6488 /* This happens when ld starts out with a 'blank' output file. */
6489 elf_flags_init (obfd
) = TRUE
;
6490 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
6491 sh_elf_set_mach_from_flags (obfd
);
6492 if (elf_elfheader (obfd
)->e_flags
& EF_SH_FDPIC
)
6493 elf_elfheader (obfd
)->e_flags
&= ~EF_SH_PIC
;
6496 if (! sh_merge_bfd_arch (ibfd
, info
))
6498 _bfd_error_handler (_("%pB: uses instructions which are incompatible "
6499 "with instructions used in previous modules"),
6501 bfd_set_error (bfd_error_bad_value
);
6505 elf_elfheader (obfd
)->e_flags
&= ~EF_SH_MACH_MASK
;
6506 elf_elfheader (obfd
)->e_flags
|=
6507 sh_elf_get_flags_from_mach (bfd_get_mach (obfd
));
6509 if (fdpic_object_p (ibfd
) != fdpic_object_p (obfd
))
6511 _bfd_error_handler (_("%pB: attempt to mix FDPIC and non-FDPIC objects"),
6513 bfd_set_error (bfd_error_bad_value
);
6519 #endif /* not sh_elf_merge_private_data */
6521 /* Override the generic function because we need to store sh_elf_obj_tdata
6522 as the specific tdata. We set also the machine architecture from flags
6526 sh_elf_object_p (bfd
*abfd
)
6528 if (! sh_elf_set_mach_from_flags (abfd
))
6531 return (((elf_elfheader (abfd
)->e_flags
& EF_SH_FDPIC
) != 0)
6532 == fdpic_object_p (abfd
));
6535 /* Finish up dynamic symbol handling. We set the contents of various
6536 dynamic sections here. */
6539 sh_elf_finish_dynamic_symbol (bfd
*output_bfd
, struct bfd_link_info
*info
,
6540 struct elf_link_hash_entry
*h
,
6541 Elf_Internal_Sym
*sym
)
6543 struct elf_sh_link_hash_table
*htab
;
6545 htab
= sh_elf_hash_table (info
);
6549 if (h
->plt
.offset
!= (bfd_vma
) -1)
6557 Elf_Internal_Rela rel
;
6559 const struct elf_sh_plt_info
*plt_info
;
6561 /* This symbol has an entry in the procedure linkage table. Set
6564 BFD_ASSERT (h
->dynindx
!= -1);
6566 splt
= htab
->root
.splt
;
6567 sgotplt
= htab
->root
.sgotplt
;
6568 srelplt
= htab
->root
.srelplt
;
6569 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srelplt
!= NULL
);
6571 /* Get the index in the procedure linkage table which
6572 corresponds to this symbol. This is the index of this symbol
6573 in all the symbols for which we are making plt entries. The
6574 first entry in the procedure linkage table is reserved. */
6575 plt_index
= get_plt_index (htab
->plt_info
, h
->plt
.offset
);
6577 plt_info
= htab
->plt_info
;
6578 if (plt_info
->short_plt
!= NULL
&& plt_index
<= MAX_SHORT_PLT
)
6579 plt_info
= plt_info
->short_plt
;
6581 /* Get the offset into the .got table of the entry that
6582 corresponds to this function. */
6584 /* The offset must be relative to the GOT symbol, twelve bytes
6585 before the end of .got.plt. Each descriptor is eight
6587 got_offset
= plt_index
* 8 + 12 - sgotplt
->size
;
6589 /* Each .got entry is 4 bytes. The first three are
6591 got_offset
= (plt_index
+ 3) * 4;
6594 if (bfd_link_pic (info
))
6595 got_offset
-= GOT_BIAS
;
6598 /* Fill in the entry in the procedure linkage table. */
6599 memcpy (splt
->contents
+ h
->plt
.offset
,
6600 plt_info
->symbol_entry
,
6601 plt_info
->symbol_entry_size
);
6603 if (bfd_link_pic (info
) || htab
->fdpic_p
)
6605 if (plt_info
->symbol_fields
.got20
)
6607 bfd_reloc_status_type r
;
6608 r
= install_movi20_field (output_bfd
, got_offset
,
6609 splt
->owner
, splt
, splt
->contents
,
6611 + plt_info
->symbol_fields
.got_entry
);
6612 BFD_ASSERT (r
== bfd_reloc_ok
);
6615 install_plt_field (output_bfd
, FALSE
, got_offset
,
6618 + plt_info
->symbol_fields
.got_entry
));
6622 BFD_ASSERT (!plt_info
->symbol_fields
.got20
);
6624 install_plt_field (output_bfd
, FALSE
,
6625 (sgotplt
->output_section
->vma
6626 + sgotplt
->output_offset
6630 + plt_info
->symbol_fields
.got_entry
));
6631 if (htab
->vxworks_p
)
6633 unsigned int reachable_plts
, plts_per_4k
;
6636 /* Divide the PLT into groups. The first group contains
6637 REACHABLE_PLTS entries and the other groups contain
6638 PLTS_PER_4K entries. Entries in the first group can
6639 branch directly to .plt; those in later groups branch
6640 to the last element of the previous group. */
6641 /* ??? It would be better to create multiple copies of
6642 the common resolver stub. */
6643 reachable_plts
= ((4096
6644 - plt_info
->plt0_entry_size
6645 - (plt_info
->symbol_fields
.plt
+ 4))
6646 / plt_info
->symbol_entry_size
) + 1;
6647 plts_per_4k
= (4096 / plt_info
->symbol_entry_size
);
6648 if (plt_index
< reachable_plts
)
6649 distance
= -(h
->plt
.offset
6650 + plt_info
->symbol_fields
.plt
);
6652 distance
= -(((plt_index
- reachable_plts
) % plts_per_4k
+ 1)
6653 * plt_info
->symbol_entry_size
);
6655 /* Install the 'bra' with this offset. */
6656 bfd_put_16 (output_bfd
,
6657 0xa000 | (0x0fff & ((distance
- 4) / 2)),
6660 + plt_info
->symbol_fields
.plt
));
6663 install_plt_field (output_bfd
, TRUE
,
6664 splt
->output_section
->vma
+ splt
->output_offset
,
6667 + plt_info
->symbol_fields
.plt
));
6670 /* Make got_offset relative to the start of .got.plt. */
6672 if (bfd_link_pic (info
))
6673 got_offset
+= GOT_BIAS
;
6676 got_offset
= plt_index
* 8;
6678 if (plt_info
->symbol_fields
.reloc_offset
!= MINUS_ONE
)
6679 install_plt_field (output_bfd
, FALSE
,
6680 plt_index
* sizeof (Elf32_External_Rela
),
6683 + plt_info
->symbol_fields
.reloc_offset
));
6685 /* Fill in the entry in the global offset table. */
6686 bfd_put_32 (output_bfd
,
6687 (splt
->output_section
->vma
6688 + splt
->output_offset
6690 + plt_info
->symbol_resolve_offset
),
6691 sgotplt
->contents
+ got_offset
);
6693 bfd_put_32 (output_bfd
,
6694 sh_elf_osec_to_segment (output_bfd
, splt
->output_section
),
6695 sgotplt
->contents
+ got_offset
+ 4);
6697 /* Fill in the entry in the .rela.plt section. */
6698 rel
.r_offset
= (sgotplt
->output_section
->vma
6699 + sgotplt
->output_offset
6702 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_FUNCDESC_VALUE
);
6704 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_JMP_SLOT
);
6707 rel
.r_addend
= GOT_BIAS
;
6709 loc
= srelplt
->contents
+ plt_index
* sizeof (Elf32_External_Rela
);
6710 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6712 if (htab
->vxworks_p
&& !bfd_link_pic (info
))
6714 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6715 Begin by pointing LOC to the first such relocation. */
6716 loc
= (htab
->srelplt2
->contents
6717 + (plt_index
* 2 + 1) * sizeof (Elf32_External_Rela
));
6719 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6720 for the PLT entry's pointer to the .got.plt entry. */
6721 rel
.r_offset
= (splt
->output_section
->vma
6722 + splt
->output_offset
6724 + plt_info
->symbol_fields
.got_entry
);
6725 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
6726 rel
.r_addend
= got_offset
;
6727 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6728 loc
+= sizeof (Elf32_External_Rela
);
6730 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6731 the .got.plt entry, which initially points to .plt. */
6732 rel
.r_offset
= (sgotplt
->output_section
->vma
6733 + sgotplt
->output_offset
6735 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
, R_SH_DIR32
);
6737 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
6740 if (!h
->def_regular
)
6742 /* Mark the symbol as undefined, rather than as defined in
6743 the .plt section. Leave the value alone. */
6744 sym
->st_shndx
= SHN_UNDEF
;
6748 if (h
->got
.offset
!= (bfd_vma
) -1
6749 && sh_elf_hash_entry (h
)->got_type
!= GOT_TLS_GD
6750 && sh_elf_hash_entry (h
)->got_type
!= GOT_TLS_IE
6751 && sh_elf_hash_entry (h
)->got_type
!= GOT_FUNCDESC
)
6755 Elf_Internal_Rela rel
;
6758 /* This symbol has an entry in the global offset table. Set it
6761 sgot
= htab
->root
.sgot
;
6762 srelgot
= htab
->root
.srelgot
;
6763 BFD_ASSERT (sgot
!= NULL
&& srelgot
!= NULL
);
6765 rel
.r_offset
= (sgot
->output_section
->vma
6766 + sgot
->output_offset
6767 + (h
->got
.offset
&~ (bfd_vma
) 1));
6769 /* If this is a static link, or it is a -Bsymbolic link and the
6770 symbol is defined locally or was forced to be local because
6771 of a version file, we just want to emit a RELATIVE reloc.
6772 The entry in the global offset table will already have been
6773 initialized in the relocate_section function. */
6774 if (bfd_link_pic (info
)
6775 && SYMBOL_REFERENCES_LOCAL (info
, h
))
6779 asection
*sec
= h
->root
.u
.def
.section
;
6781 = elf_section_data (sec
->output_section
)->dynindx
;
6783 rel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
6784 rel
.r_addend
= (h
->root
.u
.def
.value
6785 + h
->root
.u
.def
.section
->output_offset
);
6789 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
6790 rel
.r_addend
= (h
->root
.u
.def
.value
6791 + h
->root
.u
.def
.section
->output_section
->vma
6792 + h
->root
.u
.def
.section
->output_offset
);
6797 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
6798 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
6802 loc
= srelgot
->contents
;
6803 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6804 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6807 #ifdef INCLUDE_SHMEDIA
6809 struct elf_sh_link_hash_entry
*eh
;
6811 eh
= (struct elf_sh_link_hash_entry
*) h
;
6812 if (eh
->datalabel_got
.offset
!= (bfd_vma
) -1)
6816 Elf_Internal_Rela rel
;
6819 /* This symbol has a datalabel entry in the global offset table.
6822 sgot
= htab
->root
.sgot
;
6823 srelgot
= htab
->root
.srelgot
;
6824 BFD_ASSERT (sgot
!= NULL
&& srelgot
!= NULL
);
6826 rel
.r_offset
= (sgot
->output_section
->vma
6827 + sgot
->output_offset
6828 + (eh
->datalabel_got
.offset
&~ (bfd_vma
) 1));
6830 /* If this is a static link, or it is a -Bsymbolic link and the
6831 symbol is defined locally or was forced to be local because
6832 of a version file, we just want to emit a RELATIVE reloc.
6833 The entry in the global offset table will already have been
6834 initialized in the relocate_section function. */
6835 if (bfd_link_pic (info
)
6836 && SYMBOL_REFERENCES_LOCAL (info
, h
))
6840 asection
*sec
= h
->root
.u
.def
.section
;
6842 = elf_section_data (sec
->output_section
)->dynindx
;
6844 rel
.r_info
= ELF32_R_INFO (dynindx
, R_SH_DIR32
);
6845 rel
.r_addend
= (h
->root
.u
.def
.value
6846 + h
->root
.u
.def
.section
->output_offset
);
6850 rel
.r_info
= ELF32_R_INFO (0, R_SH_RELATIVE
);
6851 rel
.r_addend
= (h
->root
.u
.def
.value
6852 + h
->root
.u
.def
.section
->output_section
->vma
6853 + h
->root
.u
.def
.section
->output_offset
);
6858 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
6859 + eh
->datalabel_got
.offset
);
6860 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_GLOB_DAT
);
6864 loc
= srelgot
->contents
;
6865 loc
+= srelgot
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6866 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6874 Elf_Internal_Rela rel
;
6877 /* This symbol needs a copy reloc. Set it up. */
6879 BFD_ASSERT (h
->dynindx
!= -1
6880 && (h
->root
.type
== bfd_link_hash_defined
6881 || h
->root
.type
== bfd_link_hash_defweak
));
6883 s
= bfd_get_linker_section (htab
->root
.dynobj
, ".rela.bss");
6884 BFD_ASSERT (s
!= NULL
);
6886 rel
.r_offset
= (h
->root
.u
.def
.value
6887 + h
->root
.u
.def
.section
->output_section
->vma
6888 + h
->root
.u
.def
.section
->output_offset
);
6889 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_SH_COPY
);
6891 loc
= s
->contents
+ s
->reloc_count
++ * sizeof (Elf32_External_Rela
);
6892 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
6895 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
6896 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6898 if (h
== htab
->root
.hdynamic
6899 || (!htab
->vxworks_p
&& h
== htab
->root
.hgot
))
6900 sym
->st_shndx
= SHN_ABS
;
6905 /* Finish up the dynamic sections. */
6908 sh_elf_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6910 struct elf_sh_link_hash_table
*htab
;
6914 htab
= sh_elf_hash_table (info
);
6918 sgotplt
= htab
->root
.sgotplt
;
6919 sdyn
= bfd_get_linker_section (htab
->root
.dynobj
, ".dynamic");
6921 if (htab
->root
.dynamic_sections_created
)
6924 Elf32_External_Dyn
*dyncon
, *dynconend
;
6926 BFD_ASSERT (sgotplt
!= NULL
&& sdyn
!= NULL
);
6928 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
6929 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
6930 for (; dyncon
< dynconend
; dyncon
++)
6932 Elf_Internal_Dyn dyn
;
6934 #ifdef INCLUDE_SHMEDIA
6938 bfd_elf32_swap_dyn_in (htab
->root
.dynobj
, dyncon
, &dyn
);
6944 && elf_vxworks_finish_dynamic_entry (output_bfd
, &dyn
))
6945 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6948 #ifdef INCLUDE_SHMEDIA
6950 name
= info
->init_function
;
6954 name
= info
->fini_function
;
6956 if (dyn
.d_un
.d_val
!= 0)
6958 struct elf_link_hash_entry
*h
;
6960 h
= elf_link_hash_lookup (&htab
->root
, name
,
6961 FALSE
, FALSE
, TRUE
);
6962 if (h
!= NULL
&& (h
->other
& STO_SH5_ISA32
))
6964 dyn
.d_un
.d_val
|= 1;
6965 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6972 BFD_ASSERT (htab
->root
.hgot
!= NULL
);
6973 s
= htab
->root
.hgot
->root
.u
.def
.section
;
6974 dyn
.d_un
.d_ptr
= htab
->root
.hgot
->root
.u
.def
.value
6975 + s
->output_section
->vma
+ s
->output_offset
;
6976 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6980 s
= htab
->root
.srelplt
->output_section
;
6981 BFD_ASSERT (s
!= NULL
);
6982 dyn
.d_un
.d_ptr
= s
->vma
;
6983 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6987 s
= htab
->root
.srelplt
->output_section
;
6988 BFD_ASSERT (s
!= NULL
);
6989 dyn
.d_un
.d_val
= s
->size
;
6990 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
6995 /* Fill in the first entry in the procedure linkage table. */
6996 splt
= htab
->root
.splt
;
6997 if (splt
&& splt
->size
> 0 && htab
->plt_info
->plt0_entry
)
7001 memcpy (splt
->contents
,
7002 htab
->plt_info
->plt0_entry
,
7003 htab
->plt_info
->plt0_entry_size
);
7004 for (i
= 0; i
< ARRAY_SIZE (htab
->plt_info
->plt0_got_fields
); i
++)
7005 if (htab
->plt_info
->plt0_got_fields
[i
] != MINUS_ONE
)
7006 install_plt_field (output_bfd
, FALSE
,
7007 (sgotplt
->output_section
->vma
7008 + sgotplt
->output_offset
7011 + htab
->plt_info
->plt0_got_fields
[i
]));
7013 if (htab
->vxworks_p
)
7015 /* Finalize the .rela.plt.unloaded contents. */
7016 Elf_Internal_Rela rel
;
7019 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
7020 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
7021 loc
= htab
->srelplt2
->contents
;
7022 rel
.r_offset
= (splt
->output_section
->vma
7023 + splt
->output_offset
7024 + htab
->plt_info
->plt0_got_fields
[2]);
7025 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
, R_SH_DIR32
);
7027 bfd_elf32_swap_reloca_out (output_bfd
, &rel
, loc
);
7028 loc
+= sizeof (Elf32_External_Rela
);
7030 /* Fix up the remaining .rela.plt.unloaded relocations.
7031 They may have the wrong symbol index for _G_O_T_ or
7032 _P_L_T_ depending on the order in which symbols were
7034 while (loc
< htab
->srelplt2
->contents
+ htab
->srelplt2
->size
)
7036 /* The PLT entry's pointer to the .got.plt slot. */
7037 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
7038 rel
.r_info
= ELF32_R_INFO (htab
->root
.hgot
->indx
,
7040 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
7041 loc
+= sizeof (Elf32_External_Rela
);
7043 /* The .got.plt slot's pointer to .plt. */
7044 bfd_elf32_swap_reloc_in (output_bfd
, loc
, &rel
);
7045 rel
.r_info
= ELF32_R_INFO (htab
->root
.hplt
->indx
,
7047 bfd_elf32_swap_reloc_out (output_bfd
, &rel
, loc
);
7048 loc
+= sizeof (Elf32_External_Rela
);
7052 /* UnixWare sets the entsize of .plt to 4, although that doesn't
7053 really seem like the right value. */
7054 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
7058 /* Fill in the first three entries in the global offset table. */
7059 if (sgotplt
&& sgotplt
->size
> 0 && !htab
->fdpic_p
)
7062 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
);
7064 bfd_put_32 (output_bfd
,
7065 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
7067 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 4);
7068 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgotplt
->contents
+ 8);
7071 if (sgotplt
&& sgotplt
->size
> 0)
7072 elf_section_data (sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
7074 /* At the very end of the .rofixup section is a pointer to the GOT. */
7075 if (htab
->fdpic_p
&& htab
->srofixup
!= NULL
)
7077 struct elf_link_hash_entry
*hgot
= htab
->root
.hgot
;
7078 bfd_vma got_value
= hgot
->root
.u
.def
.value
7079 + hgot
->root
.u
.def
.section
->output_section
->vma
7080 + hgot
->root
.u
.def
.section
->output_offset
;
7082 sh_elf_add_rofixup (output_bfd
, htab
->srofixup
, got_value
);
7084 /* Make sure we allocated and generated the same number of fixups. */
7085 BFD_ASSERT (htab
->srofixup
->reloc_count
* 4 == htab
->srofixup
->size
);
7088 if (htab
->srelfuncdesc
)
7089 BFD_ASSERT (htab
->srelfuncdesc
->reloc_count
* sizeof (Elf32_External_Rela
)
7090 == htab
->srelfuncdesc
->size
);
7092 if (htab
->root
.srelgot
)
7093 BFD_ASSERT (htab
->root
.srelgot
->reloc_count
* sizeof (Elf32_External_Rela
)
7094 == htab
->root
.srelgot
->size
);
7099 static enum elf_reloc_type_class
7100 sh_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
7101 const asection
*rel_sec ATTRIBUTE_UNUSED
,
7102 const Elf_Internal_Rela
*rela
)
7104 switch ((int) ELF32_R_TYPE (rela
->r_info
))
7107 return reloc_class_relative
;
7109 return reloc_class_plt
;
7111 return reloc_class_copy
;
7113 return reloc_class_normal
;
7117 #if !defined SH_TARGET_ALREADY_DEFINED
7118 /* Support for Linux core dump NOTE sections. */
7121 elf32_shlin_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7126 switch (note
->descsz
)
7131 case 168: /* Linux/SH */
7133 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
7136 elf_tdata (abfd
)->core
->lwpid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
7145 /* Make a ".reg/999" section. */
7146 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
7147 size
, note
->descpos
+ offset
);
7151 elf32_shlin_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7153 switch (note
->descsz
)
7158 case 124: /* Linux/SH elf_prpsinfo */
7159 elf_tdata (abfd
)->core
->program
7160 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
7161 elf_tdata (abfd
)->core
->command
7162 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
7165 /* Note that for some reason, a spurious space is tacked
7166 onto the end of the args in some (at least one anyway)
7167 implementations, so strip it off if it exists. */
7170 char *command
= elf_tdata (abfd
)->core
->command
;
7171 int n
= strlen (command
);
7173 if (0 < n
&& command
[n
- 1] == ' ')
7174 command
[n
- 1] = '\0';
7179 #endif /* not SH_TARGET_ALREADY_DEFINED */
7182 /* Return address for Ith PLT stub in section PLT, for relocation REL
7183 or (bfd_vma) -1 if it should not be included. */
7186 sh_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
7187 const arelent
*rel ATTRIBUTE_UNUSED
)
7189 const struct elf_sh_plt_info
*plt_info
;
7191 plt_info
= get_plt_info (plt
->owner
, (plt
->owner
->flags
& DYNAMIC
) != 0);
7192 return plt
->vma
+ get_plt_offset (plt_info
, i
);
7195 /* Decide whether to attempt to turn absptr or lsda encodings in
7196 shared libraries into pcrel within the given input section. */
7199 sh_elf_use_relative_eh_frame (bfd
*input_bfd ATTRIBUTE_UNUSED
,
7200 struct bfd_link_info
*info
,
7201 asection
*eh_frame_section ATTRIBUTE_UNUSED
)
7203 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
7205 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
7212 /* Adjust the contents of an eh_frame_hdr section before they're output. */
7215 sh_elf_encode_eh_address (bfd
*abfd
,
7216 struct bfd_link_info
*info
,
7217 asection
*osec
, bfd_vma offset
,
7218 asection
*loc_sec
, bfd_vma loc_offset
,
7221 struct elf_sh_link_hash_table
*htab
= sh_elf_hash_table (info
);
7222 struct elf_link_hash_entry
*h
;
7225 return _bfd_elf_encode_eh_address (abfd
, info
, osec
, offset
, loc_sec
,
7226 loc_offset
, encoded
);
7228 h
= htab
->root
.hgot
;
7229 BFD_ASSERT (h
&& h
->root
.type
== bfd_link_hash_defined
);
7231 if (! h
|| (sh_elf_osec_to_segment (abfd
, osec
)
7232 == sh_elf_osec_to_segment (abfd
, loc_sec
->output_section
)))
7233 return _bfd_elf_encode_eh_address (abfd
, info
, osec
, offset
,
7234 loc_sec
, loc_offset
, encoded
);
7236 BFD_ASSERT (sh_elf_osec_to_segment (abfd
, osec
)
7237 == (sh_elf_osec_to_segment
7238 (abfd
, h
->root
.u
.def
.section
->output_section
)));
7240 *encoded
= osec
->vma
+ offset
7241 - (h
->root
.u
.def
.value
7242 + h
->root
.u
.def
.section
->output_section
->vma
7243 + h
->root
.u
.def
.section
->output_offset
);
7245 return DW_EH_PE_datarel
| DW_EH_PE_sdata4
;
7248 #if !defined SH_TARGET_ALREADY_DEFINED
7249 #define TARGET_BIG_SYM sh_elf32_vec
7250 #define TARGET_BIG_NAME "elf32-sh"
7251 #define TARGET_LITTLE_SYM sh_elf32_le_vec
7252 #define TARGET_LITTLE_NAME "elf32-shl"
7255 #define ELF_ARCH bfd_arch_sh
7256 #define ELF_TARGET_ID SH_ELF_DATA
7257 #define ELF_MACHINE_CODE EM_SH
7258 #ifdef __QNXTARGET__
7259 #define ELF_MAXPAGESIZE 0x1000
7261 #define ELF_MAXPAGESIZE 0x80
7264 #define elf_symbol_leading_char '_'
7266 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7267 #define bfd_elf32_bfd_reloc_name_lookup \
7268 sh_elf_reloc_name_lookup
7269 #define elf_info_to_howto sh_elf_info_to_howto
7270 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
7271 #define elf_backend_relocate_section sh_elf_relocate_section
7272 #define bfd_elf32_bfd_get_relocated_section_contents \
7273 sh_elf_get_relocated_section_contents
7274 #define bfd_elf32_mkobject sh_elf_mkobject
7275 #define elf_backend_object_p sh_elf_object_p
7276 #define bfd_elf32_bfd_copy_private_bfd_data \
7277 sh_elf_copy_private_data
7278 #define bfd_elf32_bfd_merge_private_bfd_data \
7279 sh_elf_merge_private_data
7281 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
7282 #define elf_backend_check_relocs sh_elf_check_relocs
7283 #define elf_backend_copy_indirect_symbol \
7284 sh_elf_copy_indirect_symbol
7285 #define elf_backend_create_dynamic_sections \
7286 sh_elf_create_dynamic_sections
7287 #define bfd_elf32_bfd_link_hash_table_create \
7288 sh_elf_link_hash_table_create
7289 #define elf_backend_adjust_dynamic_symbol \
7290 sh_elf_adjust_dynamic_symbol
7291 #define elf_backend_always_size_sections \
7292 sh_elf_always_size_sections
7293 #define elf_backend_size_dynamic_sections \
7294 sh_elf_size_dynamic_sections
7295 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
7296 #define elf_backend_finish_dynamic_symbol \
7297 sh_elf_finish_dynamic_symbol
7298 #define elf_backend_finish_dynamic_sections \
7299 sh_elf_finish_dynamic_sections
7300 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
7301 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
7302 #define elf_backend_can_make_relative_eh_frame \
7303 sh_elf_use_relative_eh_frame
7304 #define elf_backend_can_make_lsda_relative_eh_frame \
7305 sh_elf_use_relative_eh_frame
7306 #define elf_backend_encode_eh_address \
7307 sh_elf_encode_eh_address
7309 #define elf_backend_stack_align 8
7310 #define elf_backend_can_gc_sections 1
7311 #define elf_backend_can_refcount 1
7312 #define elf_backend_want_got_plt 1
7313 #define elf_backend_plt_readonly 1
7314 #define elf_backend_want_plt_sym 0
7315 #define elf_backend_got_header_size 12
7316 #define elf_backend_dtrel_excludes_plt 1
7318 #define elf_backend_linux_prpsinfo32_ugid16 TRUE
7320 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
7322 #include "elf32-target.h"
7324 /* NetBSD support. */
7325 #undef TARGET_BIG_SYM
7326 #define TARGET_BIG_SYM sh_elf32_nbsd_vec
7327 #undef TARGET_BIG_NAME
7328 #define TARGET_BIG_NAME "elf32-sh-nbsd"
7329 #undef TARGET_LITTLE_SYM
7330 #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
7331 #undef TARGET_LITTLE_NAME
7332 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
7333 #undef ELF_MAXPAGESIZE
7334 #define ELF_MAXPAGESIZE 0x10000
7335 #undef ELF_COMMONPAGESIZE
7336 #undef elf_symbol_leading_char
7337 #define elf_symbol_leading_char 0
7339 #define elf32_bed elf32_sh_nbsd_bed
7341 #include "elf32-target.h"
7344 /* Linux support. */
7345 #undef TARGET_BIG_SYM
7346 #define TARGET_BIG_SYM sh_elf32_linux_be_vec
7347 #undef TARGET_BIG_NAME
7348 #define TARGET_BIG_NAME "elf32-shbig-linux"
7349 #undef TARGET_LITTLE_SYM
7350 #define TARGET_LITTLE_SYM sh_elf32_linux_vec
7351 #undef TARGET_LITTLE_NAME
7352 #define TARGET_LITTLE_NAME "elf32-sh-linux"
7353 #undef ELF_COMMONPAGESIZE
7354 #define ELF_COMMONPAGESIZE 0x1000
7356 #undef elf_backend_grok_prstatus
7357 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
7358 #undef elf_backend_grok_psinfo
7359 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
7361 #define elf32_bed elf32_sh_lin_bed
7363 #include "elf32-target.h"
7366 /* FDPIC support. */
7367 #undef TARGET_BIG_SYM
7368 #define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
7369 #undef TARGET_BIG_NAME
7370 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
7371 #undef TARGET_LITTLE_SYM
7372 #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
7373 #undef TARGET_LITTLE_NAME
7374 #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
7377 #define elf32_bed elf32_sh_fd_bed
7379 #include "elf32-target.h"
7381 #undef elf_backend_modify_program_headers
7383 /* VxWorks support. */
7384 #undef TARGET_BIG_SYM
7385 #define TARGET_BIG_SYM sh_elf32_vxworks_vec
7386 #undef TARGET_BIG_NAME
7387 #define TARGET_BIG_NAME "elf32-sh-vxworks"
7388 #undef TARGET_LITTLE_SYM
7389 #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
7390 #undef TARGET_LITTLE_NAME
7391 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
7393 #define elf32_bed elf32_sh_vxworks_bed
7395 #undef elf_backend_want_plt_sym
7396 #define elf_backend_want_plt_sym 1
7397 #undef elf_symbol_leading_char
7398 #define elf_symbol_leading_char '_'
7399 #define elf_backend_want_got_underscore 1
7400 #undef elf_backend_grok_prstatus
7401 #undef elf_backend_grok_psinfo
7402 #undef elf_backend_add_symbol_hook
7403 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
7404 #undef elf_backend_link_output_symbol_hook
7405 #define elf_backend_link_output_symbol_hook \
7406 elf_vxworks_link_output_symbol_hook
7407 #undef elf_backend_emit_relocs
7408 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
7409 #undef elf_backend_final_write_processing
7410 #define elf_backend_final_write_processing \
7411 elf_vxworks_final_write_processing
7412 #undef ELF_MAXPAGESIZE
7413 #define ELF_MAXPAGESIZE 0x1000
7414 #undef ELF_COMMONPAGESIZE
7416 #include "elf32-target.h"
7418 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */