1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright (C) 1996-2015 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 (*_bfd_error_handler) (_("%B: unrecognised SH reloc number: %d"),
492 bfd_set_error (bfd_error_bad_value);
496 cache_ptr->howto = get_howto_table (abfd) + r;
499 /* This function handles relaxing for SH ELF. See the corresponding
500 function in coff-sh.c for a description of what this does. FIXME:
501 There is a lot of duplication here between this code and the COFF
502 specific code. The format of relocs and symbols is wound deeply
503 into this code, but it would still be better if the duplication
504 could be eliminated somehow. Note in particular that although both
505 functions use symbols like R_SH_CODE, those symbols have different
506 values; in coff-sh.c they come from include/coff/sh.h, whereas here
507 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
510 sh_elf_relax_section (bfd *abfd, asection *sec,
511 struct bfd_link_info *link_info, bfd_boolean *again)
513 Elf_Internal_Shdr *symtab_hdr;
514 Elf_Internal_Rela *internal_relocs;
515 bfd_boolean have_code;
516 Elf_Internal_Rela *irel, *irelend;
517 bfd_byte *contents = NULL;
518 Elf_Internal_Sym *isymbuf = NULL;
522 if (bfd_link_relocatable (link_info)
523 || (sec->flags & SEC_RELOC) == 0
524 || sec->reloc_count == 0)
527 #ifdef INCLUDE_SHMEDIA
528 if (elf_section_data (sec)->this_hdr.sh_flags
529 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
535 symtab_hdr = &elf_symtab_hdr (abfd);
537 internal_relocs = (_bfd_elf_link_read_relocs
538 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
539 link_info->keep_memory));
540 if (internal_relocs == NULL)
545 irelend = internal_relocs + sec->reloc_count;
546 for (irel = internal_relocs; irel < irelend; irel++)
548 bfd_vma laddr, paddr, symval;
550 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
553 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
556 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
559 /* Get the section contents. */
560 if (contents == NULL)
562 if (elf_section_data (sec)->this_hdr.contents != NULL)
563 contents = elf_section_data (sec)->this_hdr.contents;
566 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
571 /* The r_addend field of the R_SH_USES reloc will point us to
572 the register load. The 4 is because the r_addend field is
573 computed as though it were a jump offset, which are based
574 from 4 bytes after the jump instruction. */
575 laddr = irel->r_offset + 4 + irel->r_addend;
576 if (laddr >= sec->size)
578 (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
580 (unsigned long) irel->r_offset);
583 insn = bfd_get_16 (abfd, contents + laddr);
585 /* If the instruction is not mov.l NN,rN, we don't know what to
587 if ((insn & 0xf000) != 0xd000)
589 ((*_bfd_error_handler)
590 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
591 abfd, (unsigned long) irel->r_offset, insn));
595 /* Get the address from which the register is being loaded. The
596 displacement in the mov.l instruction is quadrupled. It is a
597 displacement from four bytes after the movl instruction, but,
598 before adding in the PC address, two least significant bits
599 of the PC are cleared. We assume that the section is aligned
600 on a four byte boundary. */
603 paddr += (laddr + 4) &~ (bfd_vma) 3;
604 if (paddr >= sec->size)
606 ((*_bfd_error_handler)
607 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
608 abfd, (unsigned long) irel->r_offset));
612 /* Get the reloc for the address from which the register is
613 being loaded. This reloc will tell us which function is
614 actually being called. */
615 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
616 if (irelfn->r_offset == paddr
617 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
619 if (irelfn >= irelend)
621 ((*_bfd_error_handler)
622 (_("%B: 0x%lx: warning: could not find expected reloc"),
623 abfd, (unsigned long) paddr));
627 /* Read this BFD's symbols if we haven't done so already. */
628 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
630 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
632 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
633 symtab_hdr->sh_info, 0,
639 /* Get the value of the symbol referred to by the reloc. */
640 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
642 /* A local symbol. */
643 Elf_Internal_Sym *isym;
645 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
647 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
649 ((*_bfd_error_handler)
650 (_("%B: 0x%lx: warning: symbol in unexpected section"),
651 abfd, (unsigned long) paddr));
655 symval = (isym->st_value
656 + sec->output_section->vma
657 + sec->output_offset);
662 struct elf_link_hash_entry *h;
664 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
665 h = elf_sym_hashes (abfd)[indx];
666 BFD_ASSERT (h != NULL);
667 if (h->root.type != bfd_link_hash_defined
668 && h->root.type != bfd_link_hash_defweak)
670 /* This appears to be a reference to an undefined
671 symbol. Just ignore it--it will be caught by the
672 regular reloc processing. */
676 symval = (h->root.u.def.value
677 + h->root.u.def.section->output_section->vma
678 + h->root.u.def.section->output_offset);
681 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
682 symval += bfd_get_32 (abfd, contents + paddr);
684 symval += irelfn->r_addend;
686 /* See if this function call can be shortened. */
689 + sec->output_section->vma
692 /* A branch to an address beyond ours might be increased by an
693 .align that doesn't move when bytes behind us are deleted.
694 So, we add some slop in this calculation to allow for
696 if (foff < -0x1000 || foff >= 0x1000 - 8)
698 /* After all that work, we can't shorten this function call. */
702 /* Shorten the function call. */
704 /* For simplicity of coding, we are going to modify the section
705 contents, the section relocs, and the BFD symbol table. We
706 must tell the rest of the code not to free up this
707 information. It would be possible to instead create a table
708 of changes which have to be made, as is done in coff-mips.c;
709 that would be more work, but would require less memory when
710 the linker is run. */
712 elf_section_data (sec)->relocs = internal_relocs;
713 elf_section_data (sec)->this_hdr.contents = contents;
714 symtab_hdr->contents = (unsigned char *) isymbuf;
716 /* Replace the jmp/jsr with a bra/bsr. */
718 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
719 replace the jmp/jsr with a bra/bsr. */
720 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
721 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
722 here, but that only checks if the symbol is an external symbol,
723 not if the symbol is in a different section. Besides, we need
724 a consistent meaning for the relocation, so we just assume here that
725 the value of the symbol is not available. */
727 /* We can't fully resolve this yet, because the external
728 symbol value may be changed by future relaxing. We let
729 the final link phase handle it. */
730 if (bfd_get_16 (abfd, contents + irel->r_offset) & 0x0020)
731 bfd_put_16 (abfd, (bfd_vma) 0xa000, contents + irel->r_offset);
733 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
737 /* When we calculated the symbol "value" we had an offset in the
738 DIR32's word in memory (we read and add it above). However,
739 the jsr we create does NOT have this offset encoded, so we
740 have to add it to the addend to preserve it. */
741 irel->r_addend += bfd_get_32 (abfd, contents + paddr);
743 /* See if there is another R_SH_USES reloc referring to the same
745 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
746 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
747 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
749 if (irelscan < irelend)
751 /* Some other function call depends upon this register load,
752 and we have not yet converted that function call.
753 Indeed, we may never be able to convert it. There is
754 nothing else we can do at this point. */
758 /* Look for a R_SH_COUNT reloc on the location where the
759 function address is stored. Do this before deleting any
760 bytes, to avoid confusion about the address. */
761 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
762 if (irelcount->r_offset == paddr
763 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
766 /* Delete the register load. */
767 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
770 /* That will change things, so, just in case it permits some
771 other function call to come within range, we should relax
772 again. Note that this is not required, and it may be slow. */
775 /* Now check whether we got a COUNT reloc. */
776 if (irelcount >= irelend)
778 ((*_bfd_error_handler)
779 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
780 abfd, (unsigned long) paddr));
784 /* The number of uses is stored in the r_addend field. We've
786 if (irelcount->r_addend == 0)
788 ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"),
790 (unsigned long) paddr));
794 --irelcount->r_addend;
796 /* If there are no more uses, we can delete the address. Reload
797 the address from irelfn, in case it was changed by the
798 previous call to sh_elf_relax_delete_bytes. */
799 if (irelcount->r_addend == 0)
801 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
805 /* We've done all we can with that function call. */
808 /* Look for load and store instructions that we can align on four
810 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
815 /* Get the section contents. */
816 if (contents == NULL)
818 if (elf_section_data (sec)->this_hdr.contents != NULL)
819 contents = elf_section_data (sec)->this_hdr.contents;
822 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
827 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
833 elf_section_data (sec)->relocs = internal_relocs;
834 elf_section_data (sec)->this_hdr.contents = contents;
835 symtab_hdr->contents = (unsigned char *) isymbuf;
840 && symtab_hdr->contents != (unsigned char *) isymbuf)
842 if (! link_info->keep_memory)
846 /* Cache the symbols for elf_link_input_bfd. */
847 symtab_hdr->contents = (unsigned char *) isymbuf;
852 && elf_section_data (sec)->this_hdr.contents != contents)
854 if (! link_info->keep_memory)
858 /* Cache the section contents for elf_link_input_bfd. */
859 elf_section_data (sec)->this_hdr.contents = contents;
863 if (internal_relocs != NULL
864 && elf_section_data (sec)->relocs != internal_relocs)
865 free (internal_relocs);
871 && symtab_hdr->contents != (unsigned char *) isymbuf)
874 && elf_section_data (sec)->this_hdr.contents != contents)
876 if (internal_relocs != NULL
877 && elf_section_data (sec)->relocs != internal_relocs)
878 free (internal_relocs);
883 /* Delete some bytes from a section while relaxing. FIXME: There is a
884 lot of duplication between this function and sh_relax_delete_bytes
888 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
891 Elf_Internal_Shdr *symtab_hdr;
892 unsigned int sec_shndx;
894 Elf_Internal_Rela *irel, *irelend;
895 Elf_Internal_Rela *irelalign;
897 Elf_Internal_Sym *isymbuf, *isym, *isymend;
898 struct elf_link_hash_entry **sym_hashes;
899 struct elf_link_hash_entry **end_hashes;
900 unsigned int symcount;
903 symtab_hdr = &elf_symtab_hdr (abfd);
904 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
906 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
908 contents = elf_section_data (sec)->this_hdr.contents;
910 /* The deletion must stop at the next ALIGN reloc for an aligment
911 power larger than the number of bytes we are deleting. */
916 irel = elf_section_data (sec)->relocs;
917 irelend = irel + sec->reloc_count;
918 for (; irel < irelend; irel++)
920 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
921 && irel->r_offset > addr
922 && count < (1 << irel->r_addend))
925 toaddr = irel->r_offset;
930 /* Actually delete the bytes. */
931 memmove (contents + addr, contents + addr + count,
932 (size_t) (toaddr - addr - count));
933 if (irelalign == NULL)
939 #define NOP_OPCODE (0x0009)
941 BFD_ASSERT ((count & 1) == 0);
942 for (i = 0; i < count; i += 2)
943 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
946 /* Adjust all the relocs. */
947 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
949 bfd_vma nraddr, stop;
952 int off, adjust, oinsn;
953 bfd_signed_vma voff = 0;
954 bfd_boolean overflow;
956 /* Get the new reloc address. */
957 nraddr = irel->r_offset;
958 if ((irel->r_offset > addr
959 && irel->r_offset < toaddr)
960 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
961 && irel->r_offset == toaddr))
964 /* See if this reloc was for the bytes we have deleted, in which
965 case we no longer care about it. Don't delete relocs which
966 represent addresses, though. */
967 if (irel->r_offset >= addr
968 && irel->r_offset < addr + count
969 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
970 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
971 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
972 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
973 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
976 /* If this is a PC relative reloc, see if the range it covers
977 includes the bytes we have deleted. */
978 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
987 start = irel->r_offset;
988 insn = bfd_get_16 (abfd, contents + nraddr);
992 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
999 /* If this reloc is against a symbol defined in this
1000 section, and the symbol will not be adjusted below, we
1001 must check the addend to see it will put the value in
1002 range to be adjusted, and hence must be changed. */
1003 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1005 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1006 if (isym->st_shndx == sec_shndx
1007 && (isym->st_value <= addr
1008 || isym->st_value >= toaddr))
1012 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
1014 val = bfd_get_32 (abfd, contents + nraddr);
1015 val += isym->st_value;
1016 if (val > addr && val < toaddr)
1017 bfd_put_32 (abfd, val - count, contents + nraddr);
1021 val = isym->st_value + irel->r_addend;
1022 if (val > addr && val < toaddr)
1023 irel->r_addend -= count;
1027 start = stop = addr;
1034 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1041 /* This has been made by previous relaxation. Since the
1042 relocation will be against an external symbol, the
1043 final relocation will just do the right thing. */
1044 start = stop = addr;
1050 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1052 /* The addend will be against the section symbol, thus
1053 for adjusting the addend, the relevant start is the
1054 start of the section.
1055 N.B. If we want to abandon in-place changes here and
1056 test directly using symbol + addend, we have to take into
1057 account that the addend has already been adjusted by -4. */
1058 if (stop > addr && stop < toaddr)
1059 irel->r_addend -= count;
1065 stop = start + 4 + off * 2;
1070 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1076 /* These relocs types represent
1078 The r_addend field holds the difference between the reloc
1079 address and L1. That is the start of the reloc, and
1080 adding in the contents gives us the top. We must adjust
1081 both the r_offset field and the section contents.
1082 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1083 and the elf bfd r_offset is called r_vaddr. */
1085 stop = irel->r_offset;
1086 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1090 && (stop <= addr || stop >= toaddr))
1091 irel->r_addend += count;
1092 else if (stop > addr
1094 && (start <= addr || start >= toaddr))
1095 irel->r_addend -= count;
1097 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1098 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1099 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1100 voff = bfd_get_8 (abfd, contents + nraddr);
1102 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1103 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1108 start = irel->r_offset;
1109 stop = (bfd_vma) ((bfd_signed_vma) start
1110 + (long) irel->r_addend
1117 && (stop <= addr || stop >= toaddr))
1119 else if (stop > addr
1121 && (start <= addr || start >= toaddr))
1130 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1139 if ((oinsn & 0xff00) != (insn & 0xff00))
1141 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1146 if ((oinsn & 0xf000) != (insn & 0xf000))
1148 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1152 BFD_ASSERT (adjust == count || count >= 4);
1157 if ((irel->r_offset & 3) == 0)
1160 if ((oinsn & 0xff00) != (insn & 0xff00))
1162 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1167 if (voff < 0 || voff >= 0xff)
1169 bfd_put_8 (abfd, voff, contents + nraddr);
1174 if (voff < - 0x8000 || voff >= 0x8000)
1176 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1181 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1185 irel->r_addend += adjust;
1191 ((*_bfd_error_handler)
1192 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1193 abfd, (unsigned long) irel->r_offset));
1194 bfd_set_error (bfd_error_bad_value);
1199 irel->r_offset = nraddr;
1202 /* Look through all the other sections. If there contain any IMM32
1203 relocs against internal symbols which we are not going to adjust
1204 below, we may need to adjust the addends. */
1205 for (o = abfd->sections; o != NULL; o = o->next)
1207 Elf_Internal_Rela *internal_relocs;
1208 Elf_Internal_Rela *irelscan, *irelscanend;
1209 bfd_byte *ocontents;
1212 || (o->flags & SEC_RELOC) == 0
1213 || o->reloc_count == 0)
1216 /* We always cache the relocs. Perhaps, if info->keep_memory is
1217 FALSE, we should free them, if we are permitted to, when we
1218 leave sh_coff_relax_section. */
1219 internal_relocs = (_bfd_elf_link_read_relocs
1220 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
1221 if (internal_relocs == NULL)
1225 irelscanend = internal_relocs + o->reloc_count;
1226 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1228 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1229 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1231 bfd_vma start, stop;
1232 bfd_signed_vma voff;
1234 if (ocontents == NULL)
1236 if (elf_section_data (o)->this_hdr.contents != NULL)
1237 ocontents = elf_section_data (o)->this_hdr.contents;
1240 /* We always cache the section contents.
1241 Perhaps, if info->keep_memory is FALSE, we
1242 should free them, if we are permitted to,
1243 when we leave sh_coff_relax_section. */
1244 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1246 if (ocontents != NULL)
1251 elf_section_data (o)->this_hdr.contents = ocontents;
1255 stop = irelscan->r_offset;
1257 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1259 /* STOP is in a different section, so it won't change. */
1260 if (start > addr && start < toaddr)
1261 irelscan->r_addend += count;
1263 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1264 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1268 && (stop <= addr || stop >= toaddr))
1269 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1270 ocontents + irelscan->r_offset);
1271 else if (stop > addr
1273 && (start <= addr || start >= toaddr))
1274 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1275 ocontents + irelscan->r_offset);
1278 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1281 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1285 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1286 if (isym->st_shndx == sec_shndx
1287 && (isym->st_value <= addr
1288 || isym->st_value >= toaddr))
1292 if (ocontents == NULL)
1294 if (elf_section_data (o)->this_hdr.contents != NULL)
1295 ocontents = elf_section_data (o)->this_hdr.contents;
1298 /* We always cache the section contents.
1299 Perhaps, if info->keep_memory is FALSE, we
1300 should free them, if we are permitted to,
1301 when we leave sh_coff_relax_section. */
1302 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1304 if (ocontents != NULL)
1309 elf_section_data (o)->this_hdr.contents = ocontents;
1313 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1314 val += isym->st_value;
1315 if (val > addr && val < toaddr)
1316 bfd_put_32 (abfd, val - count,
1317 ocontents + irelscan->r_offset);
1322 /* Adjust the local symbols defined in this section. */
1323 isymend = isymbuf + symtab_hdr->sh_info;
1324 for (isym = isymbuf; isym < isymend; isym++)
1326 if (isym->st_shndx == sec_shndx
1327 && isym->st_value > addr
1328 && isym->st_value < toaddr)
1329 isym->st_value -= count;
1332 /* Now adjust the global symbols defined in this section. */
1333 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1334 - symtab_hdr->sh_info);
1335 sym_hashes = elf_sym_hashes (abfd);
1336 end_hashes = sym_hashes + symcount;
1337 for (; sym_hashes < end_hashes; sym_hashes++)
1339 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1340 if ((sym_hash->root.type == bfd_link_hash_defined
1341 || sym_hash->root.type == bfd_link_hash_defweak)
1342 && sym_hash->root.u.def.section == sec
1343 && sym_hash->root.u.def.value > addr
1344 && sym_hash->root.u.def.value < toaddr)
1346 sym_hash->root.u.def.value -= count;
1350 /* See if we can move the ALIGN reloc forward. We have adjusted
1351 r_offset for it already. */
1352 if (irelalign != NULL)
1354 bfd_vma alignto, alignaddr;
1356 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1357 alignaddr = BFD_ALIGN (irelalign->r_offset,
1358 1 << irelalign->r_addend);
1359 if (alignto != alignaddr)
1361 /* Tail recursion. */
1362 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1363 (int) (alignto - alignaddr));
1370 /* Look for loads and stores which we can align to four byte
1371 boundaries. This is like sh_align_loads in coff-sh.c. */
1374 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1375 Elf_Internal_Rela *internal_relocs,
1376 bfd_byte *contents ATTRIBUTE_UNUSED,
1377 bfd_boolean *pswapped)
1379 Elf_Internal_Rela *irel, *irelend;
1380 bfd_vma *labels = NULL;
1381 bfd_vma *label, *label_end;
1386 irelend = internal_relocs + sec->reloc_count;
1388 /* Get all the addresses with labels on them. */
1389 amt = sec->reloc_count;
1390 amt *= sizeof (bfd_vma);
1391 labels = (bfd_vma *) bfd_malloc (amt);
1395 for (irel = internal_relocs; irel < irelend; irel++)
1397 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1399 *label_end = irel->r_offset;
1404 /* Note that the assembler currently always outputs relocs in
1405 address order. If that ever changes, this code will need to sort
1406 the label values and the relocs. */
1410 for (irel = internal_relocs; irel < irelend; irel++)
1412 bfd_vma start, stop;
1414 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1417 start = irel->r_offset;
1419 for (irel++; irel < irelend; irel++)
1420 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1423 stop = irel->r_offset;
1427 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1428 internal_relocs, &label,
1429 label_end, start, stop, pswapped))
1444 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1447 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1448 bfd_byte *contents, bfd_vma addr)
1450 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1451 unsigned short i1, i2;
1452 Elf_Internal_Rela *irel, *irelend;
1454 /* Swap the instructions themselves. */
1455 i1 = bfd_get_16 (abfd, contents + addr);
1456 i2 = bfd_get_16 (abfd, contents + addr + 2);
1457 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1458 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1460 /* Adjust all reloc addresses. */
1461 irelend = internal_relocs + sec->reloc_count;
1462 for (irel = internal_relocs; irel < irelend; irel++)
1464 enum elf_sh_reloc_type type;
1467 /* There are a few special types of relocs that we don't want to
1468 adjust. These relocs do not apply to the instruction itself,
1469 but are only associated with the address. */
1470 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1471 if (type == R_SH_ALIGN
1472 || type == R_SH_CODE
1473 || type == R_SH_DATA
1474 || type == R_SH_LABEL)
1477 /* If an R_SH_USES reloc points to one of the addresses being
1478 swapped, we must adjust it. It would be incorrect to do this
1479 for a jump, though, since we want to execute both
1480 instructions after the jump. (We have avoided swapping
1481 around a label, so the jump will not wind up executing an
1482 instruction it shouldn't). */
1483 if (type == R_SH_USES)
1487 off = irel->r_offset + 4 + irel->r_addend;
1489 irel->r_offset += 2;
1490 else if (off == addr + 2)
1491 irel->r_offset -= 2;
1494 if (irel->r_offset == addr)
1496 irel->r_offset += 2;
1499 else if (irel->r_offset == addr + 2)
1501 irel->r_offset -= 2;
1510 unsigned short insn, oinsn;
1511 bfd_boolean overflow;
1513 loc = contents + irel->r_offset;
1522 insn = bfd_get_16 (abfd, loc);
1525 if ((oinsn & 0xff00) != (insn & 0xff00))
1527 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1531 insn = bfd_get_16 (abfd, loc);
1534 if ((oinsn & 0xf000) != (insn & 0xf000))
1536 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1540 /* This reloc ignores the least significant 3 bits of
1541 the program counter before adding in the offset.
1542 This means that if ADDR is at an even address, the
1543 swap will not affect the offset. If ADDR is an at an
1544 odd address, then the instruction will be crossing a
1545 four byte boundary, and must be adjusted. */
1546 if ((addr & 3) != 0)
1548 insn = bfd_get_16 (abfd, loc);
1551 if ((oinsn & 0xff00) != (insn & 0xff00))
1553 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1561 ((*_bfd_error_handler)
1562 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1563 abfd, (unsigned long) irel->r_offset));
1564 bfd_set_error (bfd_error_bad_value);
1572 #endif /* defined SH64_ELF */
1574 /* Describes one of the various PLT styles. */
1576 struct elf_sh_plt_info
1578 /* The template for the first PLT entry, or NULL if there is no special
1580 const bfd_byte *plt0_entry;
1582 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1583 bfd_vma plt0_entry_size;
1585 /* Index I is the offset into PLT0_ENTRY of a pointer to
1586 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1587 if there is no such pointer. */
1588 bfd_vma plt0_got_fields[3];
1590 /* The template for a symbol's PLT entry. */
1591 const bfd_byte *symbol_entry;
1593 /* The size of SYMBOL_ENTRY in bytes. */
1594 bfd_vma symbol_entry_size;
1596 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1597 on all targets. The comments by each member indicate the value
1598 that the field must hold. */
1600 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1601 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1602 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1603 bfd_boolean got20; /* TRUE if got_entry points to a movi20
1604 instruction (instead of a constant pool
1608 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1609 bfd_vma symbol_resolve_offset;
1611 /* A different PLT layout which can be used for the first
1612 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1614 const struct elf_sh_plt_info *short_plt;
1617 #ifdef INCLUDE_SHMEDIA
1619 /* The size in bytes of an entry in the procedure linkage table. */
1621 #define ELF_PLT_ENTRY_SIZE 64
1623 /* First entry in an absolute procedure linkage table look like this. */
1625 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1627 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1628 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1629 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1630 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1631 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1632 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1633 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1634 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1635 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1636 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1637 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1638 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1639 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1640 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1641 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1642 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1645 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1647 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1648 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1649 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1650 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1651 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1652 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1653 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1654 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1655 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1656 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1657 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1658 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1659 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1660 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1661 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1662 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1665 /* Sebsequent entries in an absolute procedure linkage table look like
1668 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1670 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1671 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1672 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1673 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1674 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1675 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1676 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1677 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1678 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1679 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1680 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1681 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1682 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1683 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1684 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1685 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1688 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1690 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1691 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1692 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1693 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1694 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1695 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1696 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1697 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1698 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1699 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1700 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1701 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1702 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1703 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1704 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1705 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1708 /* Entries in a PIC procedure linkage table look like this. */
1710 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1712 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1713 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1714 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1715 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1716 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1717 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1718 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1719 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1720 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1721 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1722 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1723 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1724 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1725 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1726 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1727 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1730 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1732 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1733 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1734 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1735 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1736 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1737 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1738 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1739 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1740 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1741 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1742 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1743 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1744 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1745 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1746 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1747 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1750 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1753 /* Big-endian non-PIC. */
1754 elf_sh_plt0_entry_be,
1756 { 0, MINUS_ONE, MINUS_ONE },
1757 elf_sh_plt_entry_be,
1759 { 0, 32, 48, FALSE },
1760 33, /* includes ISA encoding */
1764 /* Little-endian non-PIC. */
1765 elf_sh_plt0_entry_le,
1767 { 0, MINUS_ONE, MINUS_ONE },
1768 elf_sh_plt_entry_le,
1770 { 0, 32, 48, FALSE },
1771 33, /* includes ISA encoding */
1777 /* Big-endian PIC. */
1778 elf_sh_plt0_entry_be,
1780 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1781 elf_sh_pic_plt_entry_be,
1783 { 0, MINUS_ONE, 52, FALSE },
1784 33, /* includes ISA encoding */
1788 /* Little-endian PIC. */
1789 elf_sh_plt0_entry_le,
1791 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1792 elf_sh_pic_plt_entry_le,
1794 { 0, MINUS_ONE, 52, FALSE },
1795 33, /* includes ISA encoding */
1801 /* Return offset of the linker in PLT0 entry. */
1802 #define elf_sh_plt0_gotplt_offset(info) 0
1804 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1805 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1808 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1811 install_plt_field (bfd *output_bfd, bfd_boolean code_p,
1812 unsigned long value, bfd_byte *addr)
1815 bfd_put_32 (output_bfd,
1816 bfd_get_32 (output_bfd, addr)
1817 | ((value >> 6) & 0x3fffc00),
1819 bfd_put_32 (output_bfd,
1820 bfd_get_32 (output_bfd, addr + 4)
1821 | ((value << 10) & 0x3fffc00),
1825 /* Return the type of PLT associated with ABFD. PIC_P is true if
1826 the object is position-independent. */
1828 static const struct elf_sh_plt_info *
1829 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
1831 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
1834 /* The size in bytes of an entry in the procedure linkage table. */
1836 #define ELF_PLT_ENTRY_SIZE 28
1838 /* First entry in an absolute procedure linkage table look like this. */
1840 /* Note - this code has been "optimised" not to use r2. r2 is used by
1841 GCC to return the address of large structures, so it should not be
1842 corrupted here. This does mean however, that this PLT does not conform
1843 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1844 and r2 contains the GOT id. This version stores the GOT id in r0 and
1845 ignores the type. Loaders can easily detect this difference however,
1846 since the type will always be 0 or 8, and the GOT ids will always be
1847 greater than or equal to 12. */
1848 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1850 0xd0, 0x05, /* mov.l 2f,r0 */
1851 0x60, 0x02, /* mov.l @r0,r0 */
1852 0x2f, 0x06, /* mov.l r0,@-r15 */
1853 0xd0, 0x03, /* mov.l 1f,r0 */
1854 0x60, 0x02, /* mov.l @r0,r0 */
1855 0x40, 0x2b, /* jmp @r0 */
1856 0x60, 0xf6, /* mov.l @r15+,r0 */
1857 0x00, 0x09, /* nop */
1858 0x00, 0x09, /* nop */
1859 0x00, 0x09, /* nop */
1860 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1861 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1864 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1866 0x05, 0xd0, /* mov.l 2f,r0 */
1867 0x02, 0x60, /* mov.l @r0,r0 */
1868 0x06, 0x2f, /* mov.l r0,@-r15 */
1869 0x03, 0xd0, /* mov.l 1f,r0 */
1870 0x02, 0x60, /* mov.l @r0,r0 */
1871 0x2b, 0x40, /* jmp @r0 */
1872 0xf6, 0x60, /* mov.l @r15+,r0 */
1873 0x09, 0x00, /* nop */
1874 0x09, 0x00, /* nop */
1875 0x09, 0x00, /* nop */
1876 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1877 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1880 /* Sebsequent entries in an absolute procedure linkage table look like
1883 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1885 0xd0, 0x04, /* mov.l 1f,r0 */
1886 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1887 0xd1, 0x02, /* mov.l 0f,r1 */
1888 0x40, 0x2b, /* jmp @r0 */
1889 0x60, 0x13, /* mov r1,r0 */
1890 0xd1, 0x03, /* mov.l 2f,r1 */
1891 0x40, 0x2b, /* jmp @r0 */
1892 0x00, 0x09, /* nop */
1893 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1894 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1895 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1898 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1900 0x04, 0xd0, /* mov.l 1f,r0 */
1901 0x02, 0x60, /* mov.l @r0,r0 */
1902 0x02, 0xd1, /* mov.l 0f,r1 */
1903 0x2b, 0x40, /* jmp @r0 */
1904 0x13, 0x60, /* mov r1,r0 */
1905 0x03, 0xd1, /* mov.l 2f,r1 */
1906 0x2b, 0x40, /* jmp @r0 */
1907 0x09, 0x00, /* nop */
1908 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1909 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1910 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1913 /* Entries in a PIC procedure linkage table look like this. */
1915 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1917 0xd0, 0x04, /* mov.l 1f,r0 */
1918 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1919 0x40, 0x2b, /* jmp @r0 */
1920 0x00, 0x09, /* nop */
1921 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1922 0xd1, 0x03, /* mov.l 2f,r1 */
1923 0x40, 0x2b, /* jmp @r0 */
1924 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1925 0x00, 0x09, /* nop */
1926 0x00, 0x09, /* nop */
1927 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1928 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1931 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1933 0x04, 0xd0, /* mov.l 1f,r0 */
1934 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1935 0x2b, 0x40, /* jmp @r0 */
1936 0x09, 0x00, /* nop */
1937 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1938 0x03, 0xd1, /* mov.l 2f,r1 */
1939 0x2b, 0x40, /* jmp @r0 */
1940 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1941 0x09, 0x00, /* nop */
1942 0x09, 0x00, /* nop */
1943 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1944 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1947 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1950 /* Big-endian non-PIC. */
1951 elf_sh_plt0_entry_be,
1953 { MINUS_ONE, 24, 20 },
1954 elf_sh_plt_entry_be,
1956 { 20, 16, 24, FALSE },
1961 /* Little-endian non-PIC. */
1962 elf_sh_plt0_entry_le,
1964 { MINUS_ONE, 24, 20 },
1965 elf_sh_plt_entry_le,
1967 { 20, 16, 24, FALSE },
1974 /* Big-endian PIC. */
1975 elf_sh_plt0_entry_be,
1977 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1978 elf_sh_pic_plt_entry_be,
1980 { 20, MINUS_ONE, 24, FALSE },
1985 /* Little-endian PIC. */
1986 elf_sh_plt0_entry_le,
1988 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1989 elf_sh_pic_plt_entry_le,
1991 { 20, MINUS_ONE, 24, FALSE },
1998 #define VXWORKS_PLT_HEADER_SIZE 12
1999 #define VXWORKS_PLT_ENTRY_SIZE 24
2001 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
2003 0xd1, 0x01, /* mov.l @(8,pc),r1 */
2004 0x61, 0x12, /* mov.l @r1,r1 */
2005 0x41, 0x2b, /* jmp @r1 */
2006 0x00, 0x09, /* nop */
2007 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2010 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
2012 0x01, 0xd1, /* mov.l @(8,pc),r1 */
2013 0x12, 0x61, /* mov.l @r1,r1 */
2014 0x2b, 0x41, /* jmp @r1 */
2015 0x09, 0x00, /* nop */
2016 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2019 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2021 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2022 0x60, 0x02, /* mov.l @r0,r0 */
2023 0x40, 0x2b, /* jmp @r0 */
2024 0x00, 0x09, /* nop */
2025 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2026 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2027 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
2028 0x00, 0x09, /* nop */
2029 0x00, 0x09, /* nop */
2030 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2033 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2035 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2036 0x02, 0x60, /* mov.l @r0,r0 */
2037 0x2b, 0x40, /* jmp @r0 */
2038 0x09, 0x00, /* nop */
2039 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2040 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2041 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
2042 0x09, 0x00, /* nop */
2043 0x09, 0x00, /* nop */
2044 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2047 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2049 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2050 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2051 0x40, 0x2b, /* jmp @r0 */
2052 0x00, 0x09, /* nop */
2053 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2054 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2055 0x51, 0xc2, /* mov.l @(8,r12),r1 */
2056 0x41, 0x2b, /* jmp @r1 */
2057 0x00, 0x09, /* nop */
2058 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2061 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2063 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2064 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2065 0x2b, 0x40, /* jmp @r0 */
2066 0x09, 0x00, /* nop */
2067 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2068 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2069 0xc2, 0x51, /* mov.l @(8,r12),r1 */
2070 0x2b, 0x41, /* jmp @r1 */
2071 0x09, 0x00, /* nop */
2072 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2075 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
2078 /* Big-endian non-PIC. */
2079 vxworks_sh_plt0_entry_be,
2080 VXWORKS_PLT_HEADER_SIZE,
2081 { MINUS_ONE, MINUS_ONE, 8 },
2082 vxworks_sh_plt_entry_be,
2083 VXWORKS_PLT_ENTRY_SIZE,
2084 { 8, 14, 20, FALSE },
2089 /* Little-endian non-PIC. */
2090 vxworks_sh_plt0_entry_le,
2091 VXWORKS_PLT_HEADER_SIZE,
2092 { MINUS_ONE, MINUS_ONE, 8 },
2093 vxworks_sh_plt_entry_le,
2094 VXWORKS_PLT_ENTRY_SIZE,
2095 { 8, 14, 20, FALSE },
2102 /* Big-endian PIC. */
2105 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2106 vxworks_sh_pic_plt_entry_be,
2107 VXWORKS_PLT_ENTRY_SIZE,
2108 { 8, MINUS_ONE, 20, FALSE },
2113 /* Little-endian PIC. */
2116 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2117 vxworks_sh_pic_plt_entry_le,
2118 VXWORKS_PLT_ENTRY_SIZE,
2119 { 8, MINUS_ONE, 20, FALSE },
2126 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
2127 binding are to omit the lazy binding stub when linking with -z now
2128 and to move lazy binding stubs into a separate region for better
2131 #define FDPIC_PLT_ENTRY_SIZE 28
2132 #define FDPIC_PLT_LAZY_OFFSET 20
2134 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
2135 duplicated if it is out of range, or which can be inlined. So
2136 right now it is always inlined, which wastes a word per stub. It
2137 might be easier to handle the duplication if we put the lazy
2138 stubs separately. */
2140 static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] =
2142 0xd0, 0x02, /* mov.l @(12,pc),r0 */
2143 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2144 0x70, 0x04, /* add #4, r0 */
2145 0x41, 0x2b, /* jmp @r1 */
2146 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2147 0x00, 0x09, /* nop */
2148 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2149 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2150 0x60, 0xc2, /* mov.l @r12,r0 */
2151 0x40, 0x2b, /* jmp @r0 */
2152 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2153 0x00, 0x09, /* nop */
2156 static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] =
2158 0x02, 0xd0, /* mov.l @(12,pc),r0 */
2159 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2160 0x04, 0x70, /* add #4, r0 */
2161 0x2b, 0x41, /* jmp @r1 */
2162 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2163 0x09, 0x00, /* nop */
2164 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2165 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2166 0xc2, 0x60, /* mov.l @r12,r0 */
2167 0x2b, 0x40, /* jmp @r0 */
2168 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2169 0x09, 0x00, /* nop */
2172 static const struct elf_sh_plt_info fdpic_sh_plts[2] = {
2174 /* Big-endian PIC. */
2177 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2178 fdpic_sh_plt_entry_be,
2179 FDPIC_PLT_ENTRY_SIZE,
2180 { 12, MINUS_ONE, 16, FALSE },
2181 FDPIC_PLT_LAZY_OFFSET,
2185 /* Little-endian PIC. */
2188 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2189 fdpic_sh_plt_entry_le,
2190 FDPIC_PLT_ENTRY_SIZE,
2191 { 12, MINUS_ONE, 16, FALSE },
2192 FDPIC_PLT_LAZY_OFFSET,
2197 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
2198 entries for the first 64K slots. We use the normal FDPIC PLT entry
2199 past that point; we could also use movi20s, which might be faster,
2200 but would not be any smaller. */
2202 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
2203 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
2205 static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2207 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2208 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2209 0x70, 0x04, /* add #4, r0 */
2210 0x41, 0x2b, /* jmp @r1 */
2211 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2212 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2213 0x60, 0xc2, /* mov.l @r12,r0 */
2214 0x40, 0x2b, /* jmp @r0 */
2215 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2216 0x00, 0x09, /* nop */
2219 static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2221 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2222 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2223 0x04, 0x70, /* add #4, r0 */
2224 0x2b, 0x41, /* jmp @r1 */
2225 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2226 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2227 0xc2, 0x60, /* mov.l @r12,r0 */
2228 0x2b, 0x40, /* jmp @r0 */
2229 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2230 0x09, 0x00, /* nop */
2233 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = {
2234 /* Big-endian FDPIC, max index 64K. */
2237 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2238 fdpic_sh2a_plt_entry_be,
2239 FDPIC_SH2A_PLT_ENTRY_SIZE,
2240 { 0, MINUS_ONE, 12, TRUE },
2241 FDPIC_SH2A_PLT_LAZY_OFFSET,
2245 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = {
2246 /* Little-endian FDPIC, max index 64K. */
2249 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2250 fdpic_sh2a_plt_entry_le,
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_plts[2] = {
2259 /* Big-endian PIC. */
2262 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2263 fdpic_sh_plt_entry_be,
2264 FDPIC_PLT_ENTRY_SIZE,
2265 { 12, MINUS_ONE, 16, FALSE },
2266 FDPIC_PLT_LAZY_OFFSET,
2267 &fdpic_sh2a_short_plt_be
2270 /* Little-endian PIC. */
2273 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2274 fdpic_sh_plt_entry_le,
2275 FDPIC_PLT_ENTRY_SIZE,
2276 { 12, MINUS_ONE, 16, FALSE },
2277 FDPIC_PLT_LAZY_OFFSET,
2278 &fdpic_sh2a_short_plt_le
2282 /* Return the type of PLT associated with ABFD. PIC_P is true if
2283 the object is position-independent. */
2285 static const struct elf_sh_plt_info *
2286 get_plt_info (bfd *abfd, bfd_boolean pic_p)
2288 if (fdpic_object_p (abfd))
2290 /* If any input file requires SH2A we can use a shorter PLT
2292 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base)
2293 return &fdpic_sh2a_plts[!bfd_big_endian (abfd)];
2295 return &fdpic_sh_plts[!bfd_big_endian (abfd)];
2297 if (vxworks_object_p (abfd))
2298 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2299 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2302 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2303 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2307 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2308 unsigned long value, bfd_byte *addr)
2310 bfd_put_32 (output_bfd, value, addr);
2314 /* The number of PLT entries which can use a shorter PLT, if any.
2315 Currently always 64K, since only SH-2A FDPIC uses this; a
2316 20-bit movi20 can address that many function descriptors below
2317 _GLOBAL_OFFSET_TABLE_. */
2318 #define MAX_SHORT_PLT 65536
2320 /* Return the index of the PLT entry at byte offset OFFSET. */
2323 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2325 bfd_vma plt_index = 0;
2327 offset -= info->plt0_entry_size;
2328 if (info->short_plt != NULL)
2330 if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size)
2332 plt_index = MAX_SHORT_PLT;
2333 offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2336 info = info->short_plt;
2338 return plt_index + offset / info->symbol_entry_size;
2341 /* Do the inverse operation. */
2344 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index)
2348 if (info->short_plt != NULL)
2350 if (plt_index > MAX_SHORT_PLT)
2352 offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2353 plt_index -= MAX_SHORT_PLT;
2356 info = info->short_plt;
2358 return (offset + info->plt0_entry_size
2359 + (plt_index * info->symbol_entry_size));
2362 /* The sh linker needs to keep track of the number of relocs that it
2363 decides to copy as dynamic relocs in check_relocs for each symbol.
2364 This is so that it can later discard them if they are found to be
2365 unnecessary. We store the information in a field extending the
2366 regular ELF linker hash table. */
2368 struct elf_sh_dyn_relocs
2370 struct elf_sh_dyn_relocs *next;
2372 /* The input section of the reloc. */
2375 /* Total number of relocs copied for the input section. */
2376 bfd_size_type count;
2378 /* Number of pc-relative relocs copied for the input section. */
2379 bfd_size_type pc_count;
2384 bfd_signed_vma refcount;
2388 /* sh ELF linker hash entry. */
2390 struct elf_sh_link_hash_entry
2392 struct elf_link_hash_entry root;
2394 #ifdef INCLUDE_SHMEDIA
2397 bfd_signed_vma refcount;
2402 /* Track dynamic relocs copied for this symbol. */
2403 struct elf_sh_dyn_relocs *dyn_relocs;
2405 bfd_signed_vma gotplt_refcount;
2407 /* A local function descriptor, for FDPIC. The refcount counts
2408 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2409 relocations; the PLT and GOT entry are accounted
2410 for separately. After adjust_dynamic_symbol, the offset is
2411 MINUS_ONE if there is no local descriptor (dynamic linker
2412 managed and no PLT entry, or undefined weak non-dynamic).
2413 During check_relocs we do not yet know whether the local
2414 descriptor will be canonical. */
2415 union gotref funcdesc;
2417 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2418 and thus require fixups or relocations. */
2419 bfd_signed_vma abs_funcdesc_refcount;
2422 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC
2426 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2428 struct sh_elf_obj_tdata
2430 struct elf_obj_tdata root;
2432 /* got_type for each local got entry. */
2433 char *local_got_type;
2435 /* Function descriptor refcount and offset for each local symbol. */
2436 union gotref *local_funcdesc;
2439 #define sh_elf_tdata(abfd) \
2440 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2442 #define sh_elf_local_got_type(abfd) \
2443 (sh_elf_tdata (abfd)->local_got_type)
2445 #define sh_elf_local_funcdesc(abfd) \
2446 (sh_elf_tdata (abfd)->local_funcdesc)
2448 #define is_sh_elf(bfd) \
2449 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2450 && elf_tdata (bfd) != NULL \
2451 && elf_object_id (bfd) == SH_ELF_DATA)
2453 /* Override the generic function because we need to store sh_elf_obj_tdata
2454 as the specific tdata. */
2457 sh_elf_mkobject (bfd *abfd)
2459 return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata),
2463 /* sh ELF linker hash table. */
2465 struct elf_sh_link_hash_table
2467 struct elf_link_hash_table root;
2469 /* Short-cuts to get to dynamic linker sections. */
2477 asection *sfuncdesc;
2478 asection *srelfuncdesc;
2481 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2484 /* Small local sym cache. */
2485 struct sym_cache sym_cache;
2487 /* A counter or offset to track a TLS got entry. */
2490 bfd_signed_vma refcount;
2494 /* The type of PLT to use. */
2495 const struct elf_sh_plt_info *plt_info;
2497 /* True if the target system is VxWorks. */
2498 bfd_boolean vxworks_p;
2500 /* True if the target system uses FDPIC. */
2501 bfd_boolean fdpic_p;
2504 /* Traverse an sh ELF linker hash table. */
2506 #define sh_elf_link_hash_traverse(table, func, info) \
2507 (elf_link_hash_traverse \
2509 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2512 /* Get the sh ELF linker hash table from a link_info structure. */
2514 #define sh_elf_hash_table(p) \
2515 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2516 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2518 /* Create an entry in an sh ELF linker hash table. */
2520 static struct bfd_hash_entry *
2521 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2522 struct bfd_hash_table *table,
2525 struct elf_sh_link_hash_entry *ret =
2526 (struct elf_sh_link_hash_entry *) entry;
2528 /* Allocate the structure if it has not already been allocated by a
2530 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2531 ret = ((struct elf_sh_link_hash_entry *)
2532 bfd_hash_allocate (table,
2533 sizeof (struct elf_sh_link_hash_entry)));
2534 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2535 return (struct bfd_hash_entry *) ret;
2537 /* Call the allocation method of the superclass. */
2538 ret = ((struct elf_sh_link_hash_entry *)
2539 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2541 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2543 ret->dyn_relocs = NULL;
2544 ret->gotplt_refcount = 0;
2545 #ifdef INCLUDE_SHMEDIA
2546 ret->datalabel_got.refcount = ret->root.got.refcount;
2548 ret->funcdesc.refcount = 0;
2549 ret->abs_funcdesc_refcount = 0;
2550 ret->got_type = GOT_UNKNOWN;
2553 return (struct bfd_hash_entry *) ret;
2556 /* Create an sh ELF linker hash table. */
2558 static struct bfd_link_hash_table *
2559 sh_elf_link_hash_table_create (bfd *abfd)
2561 struct elf_sh_link_hash_table *ret;
2562 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
2564 ret = (struct elf_sh_link_hash_table *) bfd_zmalloc (amt);
2565 if (ret == (struct elf_sh_link_hash_table *) NULL)
2568 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2569 sh_elf_link_hash_newfunc,
2570 sizeof (struct elf_sh_link_hash_entry),
2577 ret->vxworks_p = vxworks_object_p (abfd);
2578 ret->fdpic_p = fdpic_object_p (abfd);
2580 return &ret->root.root;
2584 sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2585 struct bfd_link_info *info, asection *p)
2587 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
2589 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2593 /* We need dynamic symbols for every section, since segments can
2594 relocate independently. */
2595 switch (elf_section_data (p)->this_hdr.sh_type)
2599 /* If sh_type is yet undecided, assume it could be
2600 SHT_PROGBITS/SHT_NOBITS. */
2604 /* There shouldn't be section relative relocations
2605 against any other section. */
2611 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2612 shortcuts to them in our hash table. */
2615 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2617 struct elf_sh_link_hash_table *htab;
2619 if (! _bfd_elf_create_got_section (dynobj, info))
2622 htab = sh_elf_hash_table (info);
2626 htab->sgot = bfd_get_linker_section (dynobj, ".got");
2627 htab->sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
2628 htab->srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2629 if (! htab->sgot || ! htab->sgotplt || ! htab->srelgot)
2632 htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
2633 (SEC_ALLOC | SEC_LOAD
2636 | SEC_LINKER_CREATED));
2637 if (htab->sfuncdesc == NULL
2638 || ! bfd_set_section_alignment (dynobj, htab->sfuncdesc, 2))
2641 htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
2642 ".rela.got.funcdesc",
2643 (SEC_ALLOC | SEC_LOAD
2646 | SEC_LINKER_CREATED
2648 if (htab->srelfuncdesc == NULL
2649 || ! bfd_set_section_alignment (dynobj, htab->srelfuncdesc, 2))
2652 /* Also create .rofixup. */
2653 htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
2654 (SEC_ALLOC | SEC_LOAD
2657 | SEC_LINKER_CREATED
2659 if (htab->srofixup == NULL
2660 || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2))
2666 /* Create dynamic sections when linking against a dynamic object. */
2669 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2671 struct elf_sh_link_hash_table *htab;
2672 flagword flags, pltflags;
2674 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2677 switch (bed->s->arch_size)
2688 bfd_set_error (bfd_error_bad_value);
2692 htab = sh_elf_hash_table (info);
2696 if (htab->root.dynamic_sections_created)
2699 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2700 .rel[a].bss sections. */
2702 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2703 | SEC_LINKER_CREATED);
2706 pltflags |= SEC_CODE;
2707 if (bed->plt_not_loaded)
2708 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2709 if (bed->plt_readonly)
2710 pltflags |= SEC_READONLY;
2712 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
2715 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2718 if (bed->want_plt_sym)
2720 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2722 struct elf_link_hash_entry *h;
2723 struct bfd_link_hash_entry *bh = NULL;
2725 if (! (_bfd_generic_link_add_one_symbol
2726 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2727 (bfd_vma) 0, (const char *) NULL, FALSE,
2728 get_elf_backend_data (abfd)->collect, &bh)))
2731 h = (struct elf_link_hash_entry *) bh;
2733 h->type = STT_OBJECT;
2734 htab->root.hplt = h;
2736 if (bfd_link_pic (info)
2737 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2741 s = bfd_make_section_anyway_with_flags (abfd,
2742 bed->default_use_rela_p
2743 ? ".rela.plt" : ".rel.plt",
2744 flags | SEC_READONLY);
2747 || ! bfd_set_section_alignment (abfd, s, ptralign))
2750 if (htab->sgot == NULL
2751 && !create_got_section (abfd, info))
2754 if (bed->want_dynbss)
2756 /* The .dynbss section is a place to put symbols which are defined
2757 by dynamic objects, are referenced by regular objects, and are
2758 not functions. We must allocate space for them in the process
2759 image and use a R_*_COPY reloc to tell the dynamic linker to
2760 initialize them at run time. The linker script puts the .dynbss
2761 section into the .bss section of the final image. */
2762 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2763 SEC_ALLOC | SEC_LINKER_CREATED);
2768 /* The .rel[a].bss section holds copy relocs. This section is not
2769 normally needed. We need to create it here, though, so that the
2770 linker will map it to an output section. We can't just create it
2771 only if we need it, because we will not know whether we need it
2772 until we have seen all the input files, and the first time the
2773 main linker code calls BFD after examining all the input files
2774 (size_dynamic_sections) the input sections have already been
2775 mapped to the output sections. If the section turns out not to
2776 be needed, we can discard it later. We will never need this
2777 section when generating a shared object, since they do not use
2779 if (! bfd_link_pic (info))
2781 s = bfd_make_section_anyway_with_flags (abfd,
2782 (bed->default_use_rela_p
2783 ? ".rela.bss" : ".rel.bss"),
2784 flags | SEC_READONLY);
2787 || ! bfd_set_section_alignment (abfd, s, ptralign))
2792 if (htab->vxworks_p)
2794 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2801 /* Adjust a symbol defined by a dynamic object and referenced by a
2802 regular object. The current definition is in some section of the
2803 dynamic object, but we're not including those sections. We have to
2804 change the definition to something the rest of the link can
2808 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2809 struct elf_link_hash_entry *h)
2811 struct elf_sh_link_hash_table *htab;
2812 struct elf_sh_link_hash_entry *eh;
2813 struct elf_sh_dyn_relocs *p;
2816 htab = sh_elf_hash_table (info);
2820 /* Make sure we know what is going on here. */
2821 BFD_ASSERT (htab->root.dynobj != NULL
2823 || h->u.weakdef != NULL
2826 && !h->def_regular)));
2828 /* If this is a function, put it in the procedure linkage table. We
2829 will fill in the contents of the procedure linkage table later,
2830 when we know the address of the .got section. */
2831 if (h->type == STT_FUNC
2834 if (h->plt.refcount <= 0
2835 || SYMBOL_CALLS_LOCAL (info, h)
2836 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2837 && h->root.type == bfd_link_hash_undefweak))
2839 /* This case can occur if we saw a PLT reloc in an input
2840 file, but the symbol was never referred to by a dynamic
2841 object. In such a case, we don't actually need to build
2842 a procedure linkage table, and we can just do a REL32
2844 h->plt.offset = (bfd_vma) -1;
2851 h->plt.offset = (bfd_vma) -1;
2853 /* If this is a weak symbol, and there is a real definition, the
2854 processor independent code will have arranged for us to see the
2855 real definition first, and we can just use the same value. */
2856 if (h->u.weakdef != NULL)
2858 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2859 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2860 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2861 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2862 if (info->nocopyreloc)
2863 h->non_got_ref = h->u.weakdef->non_got_ref;
2867 /* This is a reference to a symbol defined by a dynamic object which
2868 is not a function. */
2870 /* If we are creating a shared library, we must presume that the
2871 only references to the symbol are via the global offset table.
2872 For such cases we need not do anything here; the relocations will
2873 be handled correctly by relocate_section. */
2874 if (bfd_link_pic (info))
2877 /* If there are no references to this symbol that do not use the
2878 GOT, we don't need to generate a copy reloc. */
2879 if (!h->non_got_ref)
2882 /* If -z nocopyreloc was given, we won't generate them either. */
2883 if (info->nocopyreloc)
2889 eh = (struct elf_sh_link_hash_entry *) h;
2890 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2892 s = p->sec->output_section;
2893 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2897 /* If we didn't find any dynamic relocs in sections which needs the
2898 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2906 /* We must allocate the symbol in our .dynbss section, which will
2907 become part of the .bss section of the executable. There will be
2908 an entry for this symbol in the .dynsym section. The dynamic
2909 object will contain position independent code, so all references
2910 from the dynamic object to this symbol will go through the global
2911 offset table. The dynamic linker will use the .dynsym entry to
2912 determine the address it must put in the global offset table, so
2913 both the dynamic object and the regular object will refer to the
2914 same memory location for the variable. */
2917 BFD_ASSERT (s != NULL);
2919 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2920 copy the initial value out of the dynamic object and into the
2921 runtime process image. We need to remember the offset into the
2922 .rela.bss section we are going to use. */
2923 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2927 srel = htab->srelbss;
2928 BFD_ASSERT (srel != NULL);
2929 srel->size += sizeof (Elf32_External_Rela);
2933 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2936 /* Allocate space in .plt, .got and associated reloc sections for
2940 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2942 struct bfd_link_info *info;
2943 struct elf_sh_link_hash_table *htab;
2944 struct elf_sh_link_hash_entry *eh;
2945 struct elf_sh_dyn_relocs *p;
2947 if (h->root.type == bfd_link_hash_indirect)
2950 info = (struct bfd_link_info *) inf;
2951 htab = sh_elf_hash_table (info);
2955 eh = (struct elf_sh_link_hash_entry *) h;
2956 if ((h->got.refcount > 0
2958 && eh->gotplt_refcount > 0)
2960 /* The symbol has been forced local, or we have some direct got refs,
2961 so treat all the gotplt refs as got refs. */
2962 h->got.refcount += eh->gotplt_refcount;
2963 if (h->plt.refcount >= eh->gotplt_refcount)
2964 h->plt.refcount -= eh->gotplt_refcount;
2967 if (htab->root.dynamic_sections_created
2968 && h->plt.refcount > 0
2969 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2970 || h->root.type != bfd_link_hash_undefweak))
2972 /* Make sure this symbol is output as a dynamic symbol.
2973 Undefined weak syms won't yet be marked as dynamic. */
2974 if (h->dynindx == -1
2975 && !h->forced_local)
2977 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2981 if (bfd_link_pic (info)
2982 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2984 asection *s = htab->splt;
2985 const struct elf_sh_plt_info *plt_info;
2987 /* If this is the first .plt entry, make room for the special
2990 s->size += htab->plt_info->plt0_entry_size;
2992 h->plt.offset = s->size;
2994 /* If this symbol is not defined in a regular file, and we are
2995 not generating a shared library, then set the symbol to this
2996 location in the .plt. This is required to make function
2997 pointers compare as equal between the normal executable and
2998 the shared library. Skip this for FDPIC, since the
2999 function's address will be the address of the canonical
3000 function descriptor. */
3001 if (!htab->fdpic_p && !bfd_link_pic (info) && !h->def_regular)
3003 h->root.u.def.section = s;
3004 h->root.u.def.value = h->plt.offset;
3007 /* Make room for this entry. */
3008 plt_info = htab->plt_info;
3009 if (plt_info->short_plt != NULL
3010 && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
3011 plt_info = plt_info->short_plt;
3012 s->size += plt_info->symbol_entry_size;
3014 /* We also need to make an entry in the .got.plt section, which
3015 will be placed in the .got section by the linker script. */
3017 htab->sgotplt->size += 4;
3019 htab->sgotplt->size += 8;
3021 /* We also need to make an entry in the .rel.plt section. */
3022 htab->srelplt->size += sizeof (Elf32_External_Rela);
3024 if (htab->vxworks_p && !bfd_link_pic (info))
3026 /* VxWorks executables have a second set of relocations
3027 for each PLT entry. They go in a separate relocation
3028 section, which is processed by the kernel loader. */
3030 /* There is a relocation for the initial PLT entry:
3031 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
3032 if (h->plt.offset == htab->plt_info->plt0_entry_size)
3033 htab->srelplt2->size += sizeof (Elf32_External_Rela);
3035 /* There are two extra relocations for each subsequent
3036 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
3037 and an R_SH_DIR32 relocation for the PLT entry. */
3038 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
3043 h->plt.offset = (bfd_vma) -1;
3049 h->plt.offset = (bfd_vma) -1;
3053 if (h->got.refcount > 0)
3057 enum got_type got_type = sh_elf_hash_entry (h)->got_type;
3059 /* Make sure this symbol is output as a dynamic symbol.
3060 Undefined weak syms won't yet be marked as dynamic. */
3061 if (h->dynindx == -1
3062 && !h->forced_local)
3064 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3069 h->got.offset = s->size;
3071 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
3072 if (got_type == GOT_TLS_GD)
3074 dyn = htab->root.dynamic_sections_created;
3077 /* No dynamic relocations required. */
3078 if (htab->fdpic_p && !bfd_link_pic (info)
3079 && h->root.type != bfd_link_hash_undefweak
3080 && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
3081 htab->srofixup->size += 4;
3083 /* No dynamic relocations required when IE->LE conversion happens. */
3084 else if (got_type == GOT_TLS_IE
3086 && !bfd_link_pic (info))
3088 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3089 R_SH_TLS_GD needs one if local symbol and two if global. */
3090 else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
3091 || got_type == GOT_TLS_IE)
3092 htab->srelgot->size += sizeof (Elf32_External_Rela);
3093 else if (got_type == GOT_TLS_GD)
3094 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
3095 else if (got_type == GOT_FUNCDESC)
3097 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
3098 htab->srofixup->size += 4;
3100 htab->srelgot->size += sizeof (Elf32_External_Rela);
3102 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3103 || h->root.type != bfd_link_hash_undefweak)
3104 && (bfd_link_pic (info)
3105 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3106 htab->srelgot->size += sizeof (Elf32_External_Rela);
3107 else if (htab->fdpic_p
3108 && !bfd_link_pic (info)
3109 && got_type == GOT_NORMAL
3110 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3111 || h->root.type != bfd_link_hash_undefweak))
3112 htab->srofixup->size += 4;
3115 h->got.offset = (bfd_vma) -1;
3117 #ifdef INCLUDE_SHMEDIA
3118 if (eh->datalabel_got.refcount > 0)
3123 /* Make sure this symbol is output as a dynamic symbol.
3124 Undefined weak syms won't yet be marked as dynamic. */
3125 if (h->dynindx == -1
3126 && !h->forced_local)
3128 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3133 eh->datalabel_got.offset = s->size;
3135 dyn = htab->root.dynamic_sections_created;
3136 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
3137 htab->srelgot->size += sizeof (Elf32_External_Rela);
3140 eh->datalabel_got.offset = (bfd_vma) -1;
3143 /* Allocate space for any dynamic relocations to function
3144 descriptors, canonical or otherwise. We need to relocate the
3145 reference unless it resolves to zero, which only happens for
3146 undefined weak symbols (either non-default visibility, or when
3147 static linking). Any GOT slot is accounted for elsewhere. */
3148 if (eh->abs_funcdesc_refcount > 0
3149 && (h->root.type != bfd_link_hash_undefweak
3150 || (htab->root.dynamic_sections_created
3151 && ! SYMBOL_CALLS_LOCAL (info, h))))
3153 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
3154 htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
3157 += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
3160 /* We must allocate a function descriptor if there are references to
3161 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
3162 the dynamic linker isn't going to allocate it. None of this
3163 applies if we already created one in .got.plt, but if the
3164 canonical function descriptor can be in this object, there
3165 won't be a PLT entry at all. */
3166 if ((eh->funcdesc.refcount > 0
3167 || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
3168 && h->root.type != bfd_link_hash_undefweak
3169 && SYMBOL_FUNCDESC_LOCAL (info, h))
3171 /* Make room for this function descriptor. */
3172 eh->funcdesc.offset = htab->sfuncdesc->size;
3173 htab->sfuncdesc->size += 8;
3175 /* We will need a relocation or two fixups to initialize the
3176 function descriptor, so allocate those too. */
3177 if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
3178 htab->srofixup->size += 8;
3180 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3183 if (eh->dyn_relocs == NULL)
3186 /* In the shared -Bsymbolic case, discard space allocated for
3187 dynamic pc-relative relocs against symbols which turn out to be
3188 defined in regular objects. For the normal shared case, discard
3189 space for pc-relative relocs that have become local due to symbol
3190 visibility changes. */
3192 if (bfd_link_pic (info))
3194 if (SYMBOL_CALLS_LOCAL (info, h))
3196 struct elf_sh_dyn_relocs **pp;
3198 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3200 p->count -= p->pc_count;
3209 if (htab->vxworks_p)
3211 struct elf_sh_dyn_relocs **pp;
3213 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3215 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
3222 /* Also discard relocs on undefined weak syms with non-default
3224 if (eh->dyn_relocs != NULL
3225 && h->root.type == bfd_link_hash_undefweak)
3227 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3228 eh->dyn_relocs = NULL;
3230 /* Make sure undefined weak symbols are output as a dynamic
3232 else if (h->dynindx == -1
3233 && !h->forced_local)
3235 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3242 /* For the non-shared case, discard space for relocs against
3243 symbols which turn out to need copy relocs or are not
3249 || (htab->root.dynamic_sections_created
3250 && (h->root.type == bfd_link_hash_undefweak
3251 || h->root.type == bfd_link_hash_undefined))))
3253 /* Make sure this symbol is output as a dynamic symbol.
3254 Undefined weak syms won't yet be marked as dynamic. */
3255 if (h->dynindx == -1
3256 && !h->forced_local)
3258 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3262 /* If that succeeded, we know we'll be keeping all the
3264 if (h->dynindx != -1)
3268 eh->dyn_relocs = NULL;
3273 /* Finally, allocate space. */
3274 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3276 asection *sreloc = elf_section_data (p->sec)->sreloc;
3277 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3279 /* If we need relocations, we do not need fixups. */
3280 if (htab->fdpic_p && !bfd_link_pic (info))
3281 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3287 /* Find any dynamic relocs that apply to read-only sections. */
3290 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3292 struct elf_sh_link_hash_entry *eh;
3293 struct elf_sh_dyn_relocs *p;
3295 eh = (struct elf_sh_link_hash_entry *) h;
3296 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3298 asection *s = p->sec->output_section;
3300 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3302 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3304 info->flags |= DF_TEXTREL;
3306 /* Not an error, just cut short the traversal. */
3313 /* This function is called after all the input files have been read,
3314 and the input sections have been assigned to output sections.
3315 It's a convenient place to determine the PLT style. */
3318 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
3320 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd,
3321 bfd_link_pic (info));
3323 if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_relocatable (info)
3324 && !bfd_elf_stack_segment_size (output_bfd, info,
3325 "__stacksize", DEFAULT_STACK_SIZE))
3330 /* Set the sizes of the dynamic sections. */
3333 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3334 struct bfd_link_info *info)
3336 struct elf_sh_link_hash_table *htab;
3342 htab = sh_elf_hash_table (info);
3346 dynobj = htab->root.dynobj;
3347 BFD_ASSERT (dynobj != NULL);
3349 if (htab->root.dynamic_sections_created)
3351 /* Set the contents of the .interp section to the interpreter. */
3352 if (bfd_link_executable (info) && !info->nointerp)
3354 s = bfd_get_linker_section (dynobj, ".interp");
3355 BFD_ASSERT (s != NULL);
3356 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3357 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3361 /* Set up .got offsets for local syms, and space for local dynamic
3363 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3365 bfd_signed_vma *local_got;
3366 bfd_signed_vma *end_local_got;
3367 union gotref *local_funcdesc, *end_local_funcdesc;
3368 char *local_got_type;
3369 bfd_size_type locsymcount;
3370 Elf_Internal_Shdr *symtab_hdr;
3373 if (! is_sh_elf (ibfd))
3376 for (s = ibfd->sections; s != NULL; s = s->next)
3378 struct elf_sh_dyn_relocs *p;
3380 for (p = ((struct elf_sh_dyn_relocs *)
3381 elf_section_data (s)->local_dynrel);
3385 if (! bfd_is_abs_section (p->sec)
3386 && bfd_is_abs_section (p->sec->output_section))
3388 /* Input section has been discarded, either because
3389 it is a copy of a linkonce section or due to
3390 linker script /DISCARD/, so we'll be discarding
3393 else if (htab->vxworks_p
3394 && strcmp (p->sec->output_section->name,
3397 /* Relocations in vxworks .tls_vars sections are
3398 handled specially by the loader. */
3400 else if (p->count != 0)
3402 srel = elf_section_data (p->sec)->sreloc;
3403 srel->size += p->count * sizeof (Elf32_External_Rela);
3404 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3405 info->flags |= DF_TEXTREL;
3407 /* If we need relocations, we do not need fixups. */
3408 if (htab->fdpic_p && !bfd_link_pic (info))
3409 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3414 symtab_hdr = &elf_symtab_hdr (ibfd);
3415 locsymcount = symtab_hdr->sh_info;
3416 #ifdef INCLUDE_SHMEDIA
3417 /* Count datalabel local GOT. */
3421 srel = htab->srelgot;
3423 local_got = elf_local_got_refcounts (ibfd);
3426 end_local_got = local_got + locsymcount;
3427 local_got_type = sh_elf_local_got_type (ibfd);
3428 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3429 for (; local_got < end_local_got; ++local_got)
3433 *local_got = s->size;
3435 if (*local_got_type == GOT_TLS_GD)
3437 if (bfd_link_pic (info))
3438 srel->size += sizeof (Elf32_External_Rela);
3440 htab->srofixup->size += 4;
3442 if (*local_got_type == GOT_FUNCDESC)
3444 if (local_funcdesc == NULL)
3448 size = locsymcount * sizeof (union gotref);
3449 local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
3451 if (local_funcdesc == NULL)
3453 sh_elf_local_funcdesc (ibfd) = local_funcdesc;
3454 local_funcdesc += (local_got
3455 - elf_local_got_refcounts (ibfd));
3457 local_funcdesc->refcount++;
3462 *local_got = (bfd_vma) -1;
3467 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3470 end_local_funcdesc = local_funcdesc + locsymcount;
3472 for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
3474 if (local_funcdesc->refcount > 0)
3476 local_funcdesc->offset = htab->sfuncdesc->size;
3477 htab->sfuncdesc->size += 8;
3478 if (!bfd_link_pic (info))
3479 htab->srofixup->size += 8;
3481 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3484 local_funcdesc->offset = MINUS_ONE;
3490 if (htab->tls_ldm_got.refcount > 0)
3492 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3494 htab->tls_ldm_got.offset = htab->sgot->size;
3495 htab->sgot->size += 8;
3496 htab->srelgot->size += sizeof (Elf32_External_Rela);
3499 htab->tls_ldm_got.offset = -1;
3501 /* Only the reserved entries should be present. For FDPIC, they go at
3502 the end of .got.plt. */
3505 BFD_ASSERT (htab->sgotplt && htab->sgotplt->size == 12);
3506 htab->sgotplt->size = 0;
3509 /* Allocate global sym .plt and .got entries, and space for global
3510 sym dynamic relocs. */
3511 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
3513 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3514 end of the FDPIC .got.plt. */
3517 htab->root.hgot->root.u.def.value = htab->sgotplt->size;
3518 htab->sgotplt->size += 12;
3521 /* At the very end of the .rofixup section is a pointer to the GOT. */
3522 if (htab->fdpic_p && htab->srofixup != NULL)
3523 htab->srofixup->size += 4;
3525 /* We now have determined the sizes of the various dynamic sections.
3526 Allocate memory for them. */
3528 for (s = dynobj->sections; s != NULL; s = s->next)
3530 if ((s->flags & SEC_LINKER_CREATED) == 0)
3535 || s == htab->sgotplt
3536 || s == htab->sfuncdesc
3537 || s == htab->srofixup
3538 || s == htab->sdynbss)
3540 /* Strip this section if we don't need it; see the
3543 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3545 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
3548 /* We use the reloc_count field as a counter if we need
3549 to copy relocs into the output file. */
3554 /* It's not one of our sections, so don't allocate space. */
3560 /* If we don't need this section, strip it from the
3561 output file. This is mostly to handle .rela.bss and
3562 .rela.plt. We must create both sections in
3563 create_dynamic_sections, because they must be created
3564 before the linker maps input sections to output
3565 sections. The linker does that before
3566 adjust_dynamic_symbol is called, and it is that
3567 function which decides whether anything needs to go
3568 into these sections. */
3570 s->flags |= SEC_EXCLUDE;
3574 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3577 /* Allocate memory for the section contents. We use bfd_zalloc
3578 here in case unused entries are not reclaimed before the
3579 section's contents are written out. This should not happen,
3580 but this way if it does, we get a R_SH_NONE reloc instead
3582 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3583 if (s->contents == NULL)
3587 if (htab->root.dynamic_sections_created)
3589 /* Add some entries to the .dynamic section. We fill in the
3590 values later, in sh_elf_finish_dynamic_sections, but we
3591 must add the entries now so that we get the correct size for
3592 the .dynamic section. The DT_DEBUG entry is filled in by the
3593 dynamic linker and used by the debugger. */
3594 #define add_dynamic_entry(TAG, VAL) \
3595 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3597 if (bfd_link_executable (info))
3599 if (! add_dynamic_entry (DT_DEBUG, 0))
3603 if (htab->splt->size != 0)
3605 if (! add_dynamic_entry (DT_PLTGOT, 0)
3606 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3607 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3608 || ! add_dynamic_entry (DT_JMPREL, 0))
3611 else if ((elf_elfheader (output_bfd)->e_flags & EF_SH_FDPIC)
3612 && htab->sgot->size != 0)
3614 if (! add_dynamic_entry (DT_PLTGOT, 0))
3620 if (! add_dynamic_entry (DT_RELA, 0)
3621 || ! add_dynamic_entry (DT_RELASZ, 0)
3622 || ! add_dynamic_entry (DT_RELAENT,
3623 sizeof (Elf32_External_Rela)))
3626 /* If any dynamic relocs apply to a read-only section,
3627 then we need a DT_TEXTREL entry. */
3628 if ((info->flags & DF_TEXTREL) == 0)
3629 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
3631 if ((info->flags & DF_TEXTREL) != 0)
3633 if (! add_dynamic_entry (DT_TEXTREL, 0))
3638 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3641 #undef add_dynamic_entry
3646 /* Add a dynamic relocation to the SRELOC section. */
3648 inline static bfd_vma
3649 sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
3650 int reloc_type, long dynindx, bfd_vma addend)
3652 Elf_Internal_Rela outrel;
3653 bfd_vma reloc_offset;
3655 outrel.r_offset = offset;
3656 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
3657 outrel.r_addend = addend;
3659 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
3660 BFD_ASSERT (reloc_offset < sreloc->size);
3661 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3662 sreloc->contents + reloc_offset);
3663 sreloc->reloc_count++;
3665 return reloc_offset;
3668 /* Add an FDPIC read-only fixup. */
3671 sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3673 bfd_vma fixup_offset;
3675 fixup_offset = srofixup->reloc_count++ * 4;
3676 BFD_ASSERT (fixup_offset < srofixup->size);
3677 bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3680 /* Return the offset of the generated .got section from the
3681 _GLOBAL_OFFSET_TABLE_ symbol. */
3683 static bfd_signed_vma
3684 sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
3686 return (htab->sgot->output_offset - htab->sgotplt->output_offset
3687 - htab->root.hgot->root.u.def.value);
3690 /* Find the segment number in which OSEC, and output section, is
3694 sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
3696 Elf_Internal_Phdr *p = NULL;
3698 if (output_bfd->xvec->flavour == bfd_target_elf_flavour
3699 /* PR ld/17110: Do not look for output segments in an input bfd. */
3700 && output_bfd->direction != read_direction)
3701 p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
3703 /* FIXME: Nothing ever says what this index is relative to. The kernel
3704 supplies data in terms of the number of load segments but this is
3705 a phdr index and the first phdr may not be a load segment. */
3706 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
3710 sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
3712 unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
3714 return (seg != (unsigned) -1
3715 && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
3718 /* Generate the initial contents of a local function descriptor, along
3719 with any relocations or fixups required. */
3721 sh_elf_initialize_funcdesc (bfd *output_bfd,
3722 struct bfd_link_info *info,
3723 struct elf_link_hash_entry *h,
3728 struct elf_sh_link_hash_table *htab;
3732 htab = sh_elf_hash_table (info);
3734 /* FIXME: The ABI says that the offset to the function goes in the
3735 descriptor, along with the segment index. We're RELA, so it could
3736 go in the reloc instead... */
3738 if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
3740 section = h->root.u.def.section;
3741 value = h->root.u.def.value;
3744 if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
3746 dynindx = elf_section_data (section->output_section)->dynindx;
3747 addr = value + section->output_offset;
3748 seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
3752 BFD_ASSERT (h->dynindx != -1);
3753 dynindx = h->dynindx;
3757 if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
3759 if (h == NULL || h->root.type != bfd_link_hash_undefweak)
3761 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3763 + htab->sfuncdesc->output_section->vma
3764 + htab->sfuncdesc->output_offset);
3765 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3767 + htab->sfuncdesc->output_section->vma
3768 + htab->sfuncdesc->output_offset);
3771 /* There are no dynamic relocations so fill in the final
3772 address and gp value (barring fixups). */
3773 addr += section->output_section->vma;
3774 seg = htab->root.hgot->root.u.def.value
3775 + htab->root.hgot->root.u.def.section->output_section->vma
3776 + htab->root.hgot->root.u.def.section->output_offset;
3779 sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
3781 + htab->sfuncdesc->output_section->vma
3782 + htab->sfuncdesc->output_offset,
3783 R_SH_FUNCDESC_VALUE, dynindx, 0);
3785 bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
3786 bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
3791 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3792 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3795 static bfd_reloc_status_type
3796 install_movi20_field (bfd *output_bfd, unsigned long relocation,
3797 bfd *input_bfd, asection *input_section,
3798 bfd_byte *contents, bfd_vma offset)
3800 unsigned long cur_val;
3802 bfd_reloc_status_type r;
3804 if (offset > bfd_get_section_limit (input_bfd, input_section))
3805 return bfd_reloc_outofrange;
3807 r = bfd_check_overflow (complain_overflow_signed, 20, 0,
3808 bfd_arch_bits_per_address (input_bfd), relocation);
3809 if (r != bfd_reloc_ok)
3812 addr = contents + offset;
3813 cur_val = bfd_get_16 (output_bfd, addr);
3814 bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
3815 bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
3817 return bfd_reloc_ok;
3820 /* Relocate an SH ELF section. */
3823 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3824 bfd *input_bfd, asection *input_section,
3825 bfd_byte *contents, Elf_Internal_Rela *relocs,
3826 Elf_Internal_Sym *local_syms,
3827 asection **local_sections)
3829 struct elf_sh_link_hash_table *htab;
3830 Elf_Internal_Shdr *symtab_hdr;
3831 struct elf_link_hash_entry **sym_hashes;
3832 Elf_Internal_Rela *rel, *relend;
3834 bfd_vma *local_got_offsets;
3835 asection *sgot = NULL;
3836 asection *sgotplt = NULL;
3837 asection *splt = NULL;
3838 asection *sreloc = NULL;
3839 asection *srelgot = NULL;
3840 bfd_boolean is_vxworks_tls;
3841 unsigned isec_segment, got_segment, plt_segment, check_segment[2];
3842 bfd_boolean fdpic_p = FALSE;
3844 BFD_ASSERT (is_sh_elf (input_bfd));
3846 htab = sh_elf_hash_table (info);
3849 dynobj = htab->root.dynobj;
3851 sgotplt = htab->sgotplt;
3853 fdpic_p = htab->fdpic_p;
3855 symtab_hdr = &elf_symtab_hdr (input_bfd);
3856 sym_hashes = elf_sym_hashes (input_bfd);
3857 local_got_offsets = elf_local_got_offsets (input_bfd);
3859 isec_segment = sh_elf_osec_to_segment (output_bfd,
3860 input_section->output_section);
3861 if (fdpic_p && sgot)
3862 got_segment = sh_elf_osec_to_segment (output_bfd,
3863 sgot->output_section);
3866 if (fdpic_p && splt)
3867 plt_segment = sh_elf_osec_to_segment (output_bfd,
3868 splt->output_section);
3872 /* We have to handle relocations in vxworks .tls_vars sections
3873 specially, because the dynamic loader is 'weird'. */
3874 is_vxworks_tls = (htab && htab->vxworks_p && bfd_link_pic (info)
3875 && !strcmp (input_section->output_section->name,
3879 relend = relocs + input_section->reloc_count;
3880 for (; rel < relend; rel++)
3883 reloc_howto_type *howto;
3884 unsigned long r_symndx;
3885 Elf_Internal_Sym *sym;
3887 struct elf_link_hash_entry *h;
3889 bfd_vma addend = (bfd_vma) 0;
3890 bfd_reloc_status_type r;
3891 int seen_stt_datalabel = 0;
3893 enum got_type got_type;
3894 const char *symname = NULL;
3896 r_symndx = ELF32_R_SYM (rel->r_info);
3898 r_type = ELF32_R_TYPE (rel->r_info);
3900 /* Many of the relocs are only used for relaxing, and are
3901 handled entirely by the relaxation code. */
3902 if (r_type >= (int) R_SH_GNU_VTINHERIT
3903 && r_type <= (int) R_SH_LABEL)
3905 if (r_type == (int) R_SH_NONE)
3909 || r_type >= R_SH_max
3910 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3911 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3912 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3913 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
3914 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3915 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3916 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3917 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3918 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3919 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
3920 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
3921 && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
3923 bfd_set_error (bfd_error_bad_value);
3927 howto = get_howto_table (output_bfd) + r_type;
3929 /* For relocs that aren't partial_inplace, we get the addend from
3931 if (! howto->partial_inplace)
3932 addend = rel->r_addend;
3937 check_segment[0] = -1;
3938 check_segment[1] = -1;
3939 if (r_symndx < symtab_hdr->sh_info)
3941 sym = local_syms + r_symndx;
3942 sec = local_sections[r_symndx];
3944 symname = bfd_elf_string_from_elf_section
3945 (input_bfd, symtab_hdr->sh_link, sym->st_name);
3946 if (symname == NULL || *symname == '\0')
3947 symname = bfd_section_name (input_bfd, sec);
3949 relocation = (sec->output_section->vma
3950 + sec->output_offset
3952 /* A local symbol never has STO_SH5_ISA32, so we don't need
3953 datalabel processing here. Make sure this does not change
3955 if ((sym->st_other & STO_SH5_ISA32) != 0)
3956 ((*info->callbacks->reloc_dangerous)
3958 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3959 input_bfd, input_section, rel->r_offset));
3961 if (sec != NULL && discarded_section (sec))
3962 /* Handled below. */
3964 else if (bfd_link_relocatable (info))
3966 /* This is a relocatable link. We don't have to change
3967 anything, unless the reloc is against a section symbol,
3968 in which case we have to adjust according to where the
3969 section symbol winds up in the output section. */
3970 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3972 if (! howto->partial_inplace)
3974 /* For relocations with the addend in the
3975 relocation, we need just to update the addend.
3976 All real relocs are of type partial_inplace; this
3977 code is mostly for completeness. */
3978 rel->r_addend += sec->output_offset;
3983 /* Relocs of type partial_inplace need to pick up the
3984 contents in the contents and add the offset resulting
3985 from the changed location of the section symbol.
3986 Using _bfd_final_link_relocate (e.g. goto
3987 final_link_relocate) here would be wrong, because
3988 relocations marked pc_relative would get the current
3989 location subtracted, and we must only do that at the
3991 r = _bfd_relocate_contents (howto, input_bfd,
3994 contents + rel->r_offset);
3995 goto relocation_done;
4000 else if (! howto->partial_inplace)
4002 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4003 addend = rel->r_addend;
4005 else if ((sec->flags & SEC_MERGE)
4006 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4010 if (howto->rightshift || howto->src_mask != 0xffffffff)
4012 (*_bfd_error_handler)
4013 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
4014 input_bfd, input_section,
4015 (long) rel->r_offset, howto->name);
4019 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4022 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4024 addend += msec->output_section->vma + msec->output_offset;
4025 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4031 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
4034 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4035 symname = h->root.root.string;
4036 while (h->root.type == bfd_link_hash_indirect
4037 || h->root.type == bfd_link_hash_warning)
4039 #ifdef INCLUDE_SHMEDIA
4040 /* If the reference passes a symbol marked with
4041 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4043 seen_stt_datalabel |= h->type == STT_DATALABEL;
4045 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4047 if (h->root.type == bfd_link_hash_defined
4048 || h->root.type == bfd_link_hash_defweak)
4052 dyn = htab ? htab->root.dynamic_sections_created : FALSE;
4053 sec = h->root.u.def.section;
4054 /* In these cases, we don't need the relocation value.
4055 We check specially because in some obscure cases
4056 sec->output_section will be NULL. */
4057 if (r_type == R_SH_GOTPC
4058 || r_type == R_SH_GOTPC_LOW16
4059 || r_type == R_SH_GOTPC_MEDLOW16
4060 || r_type == R_SH_GOTPC_MEDHI16
4061 || r_type == R_SH_GOTPC_HI16
4062 || ((r_type == R_SH_PLT32
4063 || r_type == R_SH_PLT_LOW16
4064 || r_type == R_SH_PLT_MEDLOW16
4065 || r_type == R_SH_PLT_MEDHI16
4066 || r_type == R_SH_PLT_HI16)
4067 && h->plt.offset != (bfd_vma) -1)
4068 || ((r_type == R_SH_GOT32
4069 || r_type == R_SH_GOT20
4070 || r_type == R_SH_GOTFUNCDESC
4071 || r_type == R_SH_GOTFUNCDESC20
4072 || r_type == R_SH_GOTOFFFUNCDESC
4073 || r_type == R_SH_GOTOFFFUNCDESC20
4074 || r_type == R_SH_FUNCDESC
4075 || r_type == R_SH_GOT_LOW16
4076 || r_type == R_SH_GOT_MEDLOW16
4077 || r_type == R_SH_GOT_MEDHI16
4078 || r_type == R_SH_GOT_HI16)
4079 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4080 bfd_link_pic (info),
4082 && (! bfd_link_pic (info)
4083 || (! info->symbolic && h->dynindx != -1)
4084 || !h->def_regular))
4085 /* The cases above are those in which relocation is
4086 overwritten in the switch block below. The cases
4087 below are those in which we must defer relocation
4088 to run-time, because we can't resolve absolute
4089 addresses when creating a shared library. */
4090 || (bfd_link_pic (info)
4091 && ((! info->symbolic && h->dynindx != -1)
4093 && ((r_type == R_SH_DIR32
4094 && !h->forced_local)
4095 || (r_type == R_SH_REL32
4096 && !SYMBOL_CALLS_LOCAL (info, h)))
4097 && ((input_section->flags & SEC_ALLOC) != 0
4098 /* DWARF will emit R_SH_DIR32 relocations in its
4099 sections against symbols defined externally
4100 in shared libraries. We can't do anything
4102 || ((input_section->flags & SEC_DEBUGGING) != 0
4103 && h->def_dynamic)))
4104 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4105 sections because such sections are not SEC_ALLOC and
4106 thus ld.so will not process them. */
4107 || (sec->output_section == NULL
4108 && ((input_section->flags & SEC_DEBUGGING) != 0
4110 || (sec->output_section == NULL
4111 && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
4112 || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
4114 else if (sec->output_section != NULL)
4115 relocation = ((h->root.u.def.value
4116 + sec->output_section->vma
4117 + sec->output_offset)
4118 /* A STO_SH5_ISA32 causes a "bitor 1" to the
4119 symbol value, unless we've seen
4120 STT_DATALABEL on the way to it. */
4121 | ((h->other & STO_SH5_ISA32) != 0
4122 && ! seen_stt_datalabel));
4123 else if (!bfd_link_relocatable (info)
4124 && (_bfd_elf_section_offset (output_bfd, info,
4129 (*_bfd_error_handler)
4130 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4133 (long) rel->r_offset,
4135 h->root.root.string);
4139 else if (h->root.type == bfd_link_hash_undefweak)
4141 else if (info->unresolved_syms_in_objects == RM_IGNORE
4142 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4144 else if (!bfd_link_relocatable (info))
4146 if (! info->callbacks->undefined_symbol
4147 (info, h->root.root.string, input_bfd,
4148 input_section, rel->r_offset,
4149 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
4150 || ELF_ST_VISIBILITY (h->other))))
4155 if (sec != NULL && discarded_section (sec))
4156 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4157 rel, 1, relend, howto, 0, contents);
4159 if (bfd_link_relocatable (info))
4162 /* Check for inter-segment relocations in FDPIC files. Most
4163 relocations connect the relocation site to the location of
4164 the target symbol, but there are some exceptions below. */
4165 check_segment[0] = isec_segment;
4167 check_segment[1] = sh_elf_osec_to_segment (output_bfd,
4168 sec->output_section);
4170 check_segment[1] = -1;
4172 switch ((int) r_type)
4174 final_link_relocate:
4175 /* COFF relocs don't use the addend. The addend is used for
4176 R_SH_DIR32 to be compatible with other compilers. */
4177 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4178 contents, rel->r_offset,
4179 relocation, addend);
4183 goto final_link_relocate;
4188 /* If the reloc is against the start of this section, then
4189 the assembler has already taken care of it and the reloc
4190 is here only to assist in relaxing. If the reloc is not
4191 against the start of this section, then it's against an
4192 external symbol and we must deal with it ourselves. */
4193 if (input_section->output_section->vma + input_section->output_offset
4196 int disp = (relocation
4197 - input_section->output_section->vma
4198 - input_section->output_offset
4204 case R_SH_DIR8WPZ: mask = 1; break;
4205 case R_SH_DIR8WPL: mask = 3; break;
4206 default: mask = 0; break;
4210 ((*_bfd_error_handler)
4211 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4212 input_section->owner,
4213 (unsigned long) rel->r_offset));
4214 bfd_set_error (bfd_error_bad_value);
4218 goto final_link_relocate;
4224 #ifdef INCLUDE_SHMEDIA
4225 if (shmedia_prepare_reloc (info, input_bfd, input_section,
4226 contents, rel, &relocation))
4227 goto final_link_relocate;
4229 bfd_set_error (bfd_error_bad_value);
4237 goto final_link_relocate;
4243 ((*_bfd_error_handler)
4244 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4245 input_section->owner,
4246 (unsigned long) rel->r_offset, howto->name,
4247 (unsigned long) relocation));
4248 bfd_set_error (bfd_error_bad_value);
4251 goto final_link_relocate;
4258 ((*_bfd_error_handler)
4259 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4260 input_section->owner,
4261 (unsigned long) rel->r_offset, howto->name,
4262 (unsigned long) relocation));
4263 bfd_set_error (bfd_error_bad_value);
4266 goto final_link_relocate;
4269 if ((signed int)relocation < -32
4270 || (signed int)relocation > 32)
4272 ((*_bfd_error_handler)
4273 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
4274 input_section->owner,
4275 (unsigned long) rel->r_offset,
4276 (unsigned long) relocation));
4277 bfd_set_error (bfd_error_bad_value);
4280 goto final_link_relocate;
4283 if ((signed int)relocation < -16
4284 || (signed int)relocation > 16)
4286 ((*_bfd_error_handler)
4287 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
4288 input_section->owner,
4289 (unsigned long) rel->r_offset,
4290 (unsigned long) relocation));
4291 bfd_set_error (bfd_error_bad_value);
4294 goto final_link_relocate;
4298 #ifdef INCLUDE_SHMEDIA
4299 case R_SH_IMM_LOW16_PCREL:
4300 case R_SH_IMM_MEDLOW16_PCREL:
4301 case R_SH_IMM_MEDHI16_PCREL:
4302 case R_SH_IMM_HI16_PCREL:
4304 if (bfd_link_pic (info)
4306 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4307 || h->root.type != bfd_link_hash_undefweak)
4308 && r_symndx != STN_UNDEF
4309 && (input_section->flags & SEC_ALLOC) != 0
4311 && (r_type == R_SH_DIR32
4312 || !SYMBOL_CALLS_LOCAL (info, h)))
4314 Elf_Internal_Rela outrel;
4316 bfd_boolean skip, relocate;
4318 /* When generating a shared object, these relocations
4319 are copied into the output file to be resolved at run
4324 sreloc = _bfd_elf_get_dynamic_reloc_section
4325 (input_bfd, input_section, /*rela?*/ TRUE);
4334 _bfd_elf_section_offset (output_bfd, info, input_section,
4336 if (outrel.r_offset == (bfd_vma) -1)
4338 else if (outrel.r_offset == (bfd_vma) -2)
4339 skip = TRUE, relocate = TRUE;
4340 outrel.r_offset += (input_section->output_section->vma
4341 + input_section->output_offset);
4344 memset (&outrel, 0, sizeof outrel);
4345 else if (r_type == R_SH_REL32)
4347 BFD_ASSERT (h != NULL && h->dynindx != -1);
4348 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4350 = (howto->partial_inplace
4351 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4354 #ifdef INCLUDE_SHMEDIA
4355 else if (r_type == R_SH_IMM_LOW16_PCREL
4356 || r_type == R_SH_IMM_MEDLOW16_PCREL
4357 || r_type == R_SH_IMM_MEDHI16_PCREL
4358 || r_type == R_SH_IMM_HI16_PCREL)
4360 BFD_ASSERT (h != NULL && h->dynindx != -1);
4361 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4362 outrel.r_addend = addend;
4367 || ((info->symbolic || h->dynindx == -1)
4368 && h->def_regular)))
4372 BFD_ASSERT (sec != NULL);
4373 BFD_ASSERT (sec->output_section != NULL);
4374 dynindx = elf_section_data (sec->output_section)->dynindx;
4375 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4376 outrel.r_addend = relocation;
4378 += (howto->partial_inplace
4379 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4381 outrel.r_addend -= sec->output_section->vma;
4385 /* h->dynindx may be -1 if this symbol was marked to
4388 || ((info->symbolic || h->dynindx == -1)
4391 relocate = howto->partial_inplace;
4392 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4396 BFD_ASSERT (h->dynindx != -1);
4397 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4399 outrel.r_addend = relocation;
4401 += (howto->partial_inplace
4402 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4406 loc = sreloc->contents;
4407 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4408 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4410 check_segment[0] = check_segment[1] = -1;
4412 /* If this reloc is against an external symbol, we do
4413 not want to fiddle with the addend. Otherwise, we
4414 need to include the symbol value so that it becomes
4415 an addend for the dynamic reloc. */
4419 else if (fdpic_p && !bfd_link_pic (info)
4420 && r_type == R_SH_DIR32
4421 && (input_section->flags & SEC_ALLOC) != 0)
4427 if (sh_elf_osec_readonly_p (output_bfd,
4428 input_section->output_section))
4430 (*_bfd_error_handler)
4431 (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"),
4434 (long) rel->r_offset,
4439 offset = _bfd_elf_section_offset (output_bfd, info,
4440 input_section, rel->r_offset);
4441 if (offset != (bfd_vma)-1)
4442 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4443 input_section->output_section->vma
4444 + input_section->output_offset
4447 check_segment[0] = check_segment[1] = -1;
4449 /* We don't want warnings for non-NULL tests on undefined weak
4451 else if (r_type == R_SH_REL32
4453 && h->root.type == bfd_link_hash_undefweak)
4454 check_segment[0] = check_segment[1] = -1;
4455 goto final_link_relocate;
4458 #ifdef INCLUDE_SHMEDIA
4459 case R_SH_GOTPLT_LOW16:
4460 case R_SH_GOTPLT_MEDLOW16:
4461 case R_SH_GOTPLT_MEDHI16:
4462 case R_SH_GOTPLT_HI16:
4463 case R_SH_GOTPLT10BY4:
4464 case R_SH_GOTPLT10BY8:
4466 /* Relocation is to the entry for this symbol in the
4467 procedure linkage table. */
4471 || ! bfd_link_pic (info)
4474 || h->plt.offset == (bfd_vma) -1
4475 || h->got.offset != (bfd_vma) -1)
4478 /* Relocation is to the entry for this symbol in the global
4479 offset table extension for the procedure linkage table. */
4482 BFD_ASSERT (sgotplt != NULL);
4483 relocation = (sgotplt->output_offset
4484 + (get_plt_index (htab->plt_info, h->plt.offset)
4488 relocation -= GOT_BIAS;
4491 goto final_link_relocate;
4496 #ifdef INCLUDE_SHMEDIA
4497 case R_SH_GOT_LOW16:
4498 case R_SH_GOT_MEDLOW16:
4499 case R_SH_GOT_MEDHI16:
4504 /* Relocation is to the entry for this symbol in the global
4508 BFD_ASSERT (sgot != NULL);
4509 check_segment[0] = check_segment[1] = -1;
4515 off = h->got.offset;
4516 #ifdef INCLUDE_SHMEDIA
4517 if (seen_stt_datalabel)
4519 struct elf_sh_link_hash_entry *hsh;
4521 hsh = (struct elf_sh_link_hash_entry *)h;
4522 off = hsh->datalabel_got.offset;
4525 BFD_ASSERT (off != (bfd_vma) -1);
4527 dyn = htab->root.dynamic_sections_created;
4528 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4529 bfd_link_pic (info),
4531 || (bfd_link_pic (info)
4532 && SYMBOL_REFERENCES_LOCAL (info, h))
4533 || (ELF_ST_VISIBILITY (h->other)
4534 && h->root.type == bfd_link_hash_undefweak))
4536 /* This is actually a static link, or it is a
4537 -Bsymbolic link and the symbol is defined
4538 locally, or the symbol was forced to be local
4539 because of a version file. We must initialize
4540 this entry in the global offset table. Since the
4541 offset must always be a multiple of 4, we use the
4542 least significant bit to record whether we have
4543 initialized it already.
4545 When doing a dynamic link, we create a .rela.got
4546 relocation entry to initialize the value. This
4547 is done in the finish_dynamic_symbol routine. */
4552 bfd_put_32 (output_bfd, relocation,
4553 sgot->contents + off);
4554 #ifdef INCLUDE_SHMEDIA
4555 if (seen_stt_datalabel)
4557 struct elf_sh_link_hash_entry *hsh;
4559 hsh = (struct elf_sh_link_hash_entry *)h;
4560 hsh->datalabel_got.offset |= 1;
4566 /* If we initialize the GOT entry here with a valid
4567 symbol address, also add a fixup. */
4568 if (fdpic_p && !bfd_link_pic (info)
4569 && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
4570 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4571 || h->root.type != bfd_link_hash_undefweak))
4572 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4573 sgot->output_section->vma
4574 + sgot->output_offset
4579 relocation = sh_elf_got_offset (htab) + off;
4583 #ifdef INCLUDE_SHMEDIA
4586 BFD_ASSERT (local_got_offsets != NULL
4587 && (local_got_offsets[symtab_hdr->sh_info
4591 off = local_got_offsets[symtab_hdr->sh_info
4597 BFD_ASSERT (local_got_offsets != NULL
4598 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4600 off = local_got_offsets[r_symndx];
4601 #ifdef INCLUDE_SHMEDIA
4605 /* The offset must always be a multiple of 4. We use
4606 the least significant bit to record whether we have
4607 already generated the necessary reloc. */
4612 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4614 if (bfd_link_pic (info))
4616 Elf_Internal_Rela outrel;
4619 if (srelgot == NULL)
4621 srelgot = bfd_get_linker_section (dynobj,
4623 BFD_ASSERT (srelgot != NULL);
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))
4908 (*_bfd_error_handler)
4909 (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"),
4912 (long) rel->r_offset,
4917 offset = _bfd_elf_section_offset (output_bfd, info,
4918 reloc_section, reloc_offset);
4920 if (offset != (bfd_vma)-1)
4921 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4923 + reloc_section->output_section->vma
4924 + reloc_section->output_offset);
4926 else if ((reloc_section->output_section->flags
4927 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
4931 if (sh_elf_osec_readonly_p (output_bfd,
4932 reloc_section->output_section))
4934 info->callbacks->warning
4936 _("cannot emit dynamic relocations in read-only section"),
4937 symname, input_bfd, reloc_section, reloc_offset);
4941 if (srelgot == NULL)
4943 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
4944 BFD_ASSERT (srelgot != NULL);
4947 offset = _bfd_elf_section_offset (output_bfd, info,
4948 reloc_section, reloc_offset);
4950 if (offset != (bfd_vma)-1)
4951 sh_elf_add_dyn_reloc (output_bfd, srelgot,
4953 + reloc_section->output_section->vma
4954 + reloc_section->output_offset,
4955 reloc_type, dynindx, relocation);
4957 if (r_type == R_SH_FUNCDESC)
4965 goto funcdesc_leave_zero;
4969 if (SYMBOL_FUNCDESC_LOCAL (info, h))
4970 relocation += htab->sfuncdesc->output_section->vma;
4971 funcdesc_leave_zero:
4972 if (r_type != R_SH_FUNCDESC)
4974 bfd_put_32 (output_bfd, relocation,
4975 reloc_section->contents + reloc_offset);
4979 local_got_offsets[r_symndx] |= 1;
4983 relocation = sh_elf_got_offset (htab) + reloc_offset;
4985 relocation -= GOT_BIAS;
4988 if (r_type == R_SH_GOTFUNCDESC20)
4990 r = install_movi20_field (output_bfd, relocation + addend,
4991 input_bfd, input_section, contents,
4996 goto final_link_relocate;
5000 case R_SH_GOTOFFFUNCDESC:
5001 case R_SH_GOTOFFFUNCDESC20:
5002 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
5003 executable and --export-dynamic. If such symbols get
5004 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
5008 check_segment[0] = check_segment[1] = -1;
5010 addend = rel->r_addend;
5012 if (h && (h->root.type == bfd_link_hash_undefweak
5013 || !SYMBOL_FUNCDESC_LOCAL (info, h)))
5016 (_("%B(%A+0x%lx): %s relocation against external symbol \"%s\""),
5017 input_bfd, input_section, (long) rel->r_offset, howto->name,
5018 h->root.root.string);
5025 /* Otherwise, we know we have a private function
5026 descriptor, so reference it directly. */
5029 offset = sh_elf_hash_entry (h)->funcdesc.offset;
5030 BFD_ASSERT (offset != MINUS_ONE);
5031 if ((offset & 1) == 0)
5033 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
5036 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
5041 union gotref *local_funcdesc;
5043 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
5044 offset = local_funcdesc[r_symndx].offset;
5045 BFD_ASSERT (offset != MINUS_ONE);
5046 if ((offset & 1) == 0)
5048 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
5052 local_funcdesc[r_symndx].offset |= 1;
5056 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
5059 relocation -= (htab->root.hgot->root.u.def.value
5060 + sgotplt->output_offset);
5062 relocation -= GOT_BIAS;
5065 if (r_type == R_SH_GOTOFFFUNCDESC20)
5067 r = install_movi20_field (output_bfd, relocation + addend,
5068 input_bfd, input_section, contents,
5073 goto final_link_relocate;
5075 case R_SH_LOOP_START:
5077 static bfd_vma start, end;
5079 start = (relocation + rel->r_addend
5080 - (sec->output_section->vma + sec->output_offset));
5081 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5082 rel->r_offset, sec, start, end);
5086 end = (relocation + rel->r_addend
5087 - (sec->output_section->vma + sec->output_offset));
5088 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5089 rel->r_offset, sec, start, end);
5093 case R_SH_TLS_GD_32:
5094 case R_SH_TLS_IE_32:
5096 check_segment[0] = check_segment[1] = -1;
5097 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5098 got_type = GOT_UNKNOWN;
5099 if (h == NULL && local_got_offsets)
5100 got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
5103 got_type = sh_elf_hash_entry (h)->got_type;
5104 if (! bfd_link_pic (info)
5105 && (h->dynindx == -1
5107 r_type = R_SH_TLS_LE_32;
5110 if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
5111 r_type = R_SH_TLS_IE_32;
5113 if (r_type == R_SH_TLS_LE_32)
5116 unsigned short insn;
5118 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5120 /* GD->LE transition:
5121 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5122 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5123 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5125 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5127 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
5129 offset = rel->r_offset;
5130 BFD_ASSERT (offset >= 16);
5131 /* Size of GD instructions is 16 or 18. */
5133 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5134 if ((insn & 0xff00) == 0xc700)
5136 BFD_ASSERT (offset >= 2);
5138 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5141 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5142 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5143 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5144 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5145 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5146 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5147 BFD_ASSERT (insn == 0x310c);
5148 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5149 BFD_ASSERT (insn == 0x410b);
5150 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5151 BFD_ASSERT (insn == 0x34cc);
5153 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5154 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5155 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5156 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5157 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5163 /* IE->LE transition:
5164 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5165 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5167 mov.l .Ln,rM; stc gbr,rN; nop; ...;
5170 offset = rel->r_offset;
5171 BFD_ASSERT (offset >= 16);
5172 /* Size of IE instructions is 10 or 12. */
5174 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5175 if ((insn & 0xf0ff) == 0x0012)
5177 BFD_ASSERT (offset >= 2);
5179 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5182 BFD_ASSERT ((insn & 0xff00) == 0xd000);
5183 target = insn & 0x00ff;
5184 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5185 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5186 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5187 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5188 insn = 0xd000 | (insn & 0x0f00) | target;
5189 bfd_put_16 (output_bfd, insn, contents + offset + 0);
5190 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5193 bfd_put_32 (output_bfd, tpoff (info, relocation),
5194 contents + rel->r_offset);
5198 if (sgot == NULL || sgotplt == NULL)
5202 off = h->got.offset;
5205 if (local_got_offsets == NULL)
5208 off = local_got_offsets[r_symndx];
5211 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
5212 if (r_type == R_SH_TLS_IE_32
5213 && ! htab->root.dynamic_sections_created)
5216 bfd_put_32 (output_bfd, tpoff (info, relocation),
5217 sgot->contents + off);
5218 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5219 contents + rel->r_offset);
5227 Elf_Internal_Rela outrel;
5231 if (srelgot == NULL)
5233 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
5234 BFD_ASSERT (srelgot != NULL);
5237 outrel.r_offset = (sgot->output_section->vma
5238 + sgot->output_offset + off);
5240 if (h == NULL || h->dynindx == -1)
5245 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5247 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5248 outrel.r_addend = relocation - dtpoff_base (info);
5250 outrel.r_addend = 0;
5251 outrel.r_info = ELF32_R_INFO (indx, dr_type);
5252 loc = srelgot->contents;
5253 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5254 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5256 if (r_type == R_SH_TLS_GD_32)
5260 bfd_put_32 (output_bfd,
5261 relocation - dtpoff_base (info),
5262 sgot->contents + off + 4);
5266 outrel.r_info = ELF32_R_INFO (indx,
5268 outrel.r_offset += 4;
5269 outrel.r_addend = 0;
5270 srelgot->reloc_count++;
5271 loc += sizeof (Elf32_External_Rela);
5272 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5279 local_got_offsets[r_symndx] |= 1;
5282 if (off >= (bfd_vma) -2)
5285 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5286 relocation = sh_elf_got_offset (htab) + off;
5290 unsigned short insn;
5292 /* GD->IE transition:
5293 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5294 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5295 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5297 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5298 nop; nop; bra 3f; nop; .align 2;
5299 1: .long x@TPOFF; 2:...; 3:. */
5301 offset = rel->r_offset;
5302 BFD_ASSERT (offset >= 16);
5303 /* Size of GD instructions is 16 or 18. */
5305 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5306 if ((insn & 0xff00) == 0xc700)
5308 BFD_ASSERT (offset >= 2);
5310 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5313 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5315 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
5316 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5318 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5319 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5320 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5321 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5322 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5323 BFD_ASSERT (insn == 0x310c);
5324 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5325 BFD_ASSERT (insn == 0x410b);
5326 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5327 BFD_ASSERT (insn == 0x34cc);
5329 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5330 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5331 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5332 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5333 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5335 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5336 contents + rel->r_offset);
5341 addend = rel->r_addend;
5343 goto final_link_relocate;
5345 case R_SH_TLS_LD_32:
5347 check_segment[0] = check_segment[1] = -1;
5348 if (! bfd_link_pic (info))
5351 unsigned short insn;
5353 /* LD->LE transition:
5354 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5355 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5356 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5358 stc gbr,r0; nop; nop; nop;
5359 nop; nop; bra 3f; ...; 3:. */
5361 offset = rel->r_offset;
5362 BFD_ASSERT (offset >= 16);
5363 /* Size of LD instructions is 16 or 18. */
5365 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5366 if ((insn & 0xff00) == 0xc700)
5368 BFD_ASSERT (offset >= 2);
5370 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5373 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5374 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5375 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5376 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5377 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5378 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5379 BFD_ASSERT (insn == 0x310c);
5380 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5381 BFD_ASSERT (insn == 0x410b);
5382 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5383 BFD_ASSERT (insn == 0x34cc);
5385 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5386 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5387 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5388 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5389 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5390 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5395 if (sgot == NULL || sgotplt == NULL)
5398 off = htab->tls_ldm_got.offset;
5403 Elf_Internal_Rela outrel;
5406 srelgot = htab->srelgot;
5407 if (srelgot == NULL)
5410 outrel.r_offset = (sgot->output_section->vma
5411 + sgot->output_offset + off);
5412 outrel.r_addend = 0;
5413 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5414 loc = srelgot->contents;
5415 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5416 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5417 htab->tls_ldm_got.offset |= 1;
5420 relocation = sh_elf_got_offset (htab) + off;
5421 addend = rel->r_addend;
5423 goto final_link_relocate;
5425 case R_SH_TLS_LDO_32:
5426 check_segment[0] = check_segment[1] = -1;
5427 if (! bfd_link_pic (info))
5428 relocation = tpoff (info, relocation);
5430 relocation -= dtpoff_base (info);
5432 addend = rel->r_addend;
5433 goto final_link_relocate;
5435 case R_SH_TLS_LE_32:
5438 Elf_Internal_Rela outrel;
5441 check_segment[0] = check_segment[1] = -1;
5443 if (!bfd_link_dll (info))
5445 relocation = tpoff (info, relocation);
5446 addend = rel->r_addend;
5447 goto final_link_relocate;
5452 sreloc = _bfd_elf_get_dynamic_reloc_section
5453 (input_bfd, input_section, /*rela?*/ TRUE);
5458 if (h == NULL || h->dynindx == -1)
5463 outrel.r_offset = (input_section->output_section->vma
5464 + input_section->output_offset
5466 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5468 outrel.r_addend = relocation - dtpoff_base (info);
5470 outrel.r_addend = 0;
5472 loc = sreloc->contents;
5473 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5474 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5480 if (fdpic_p && check_segment[0] != (unsigned) -1
5481 && check_segment[0] != check_segment[1])
5483 /* We don't want duplicate errors for undefined symbols. */
5484 if (!h || h->root.type != bfd_link_hash_undefined)
5486 if (bfd_link_pic (info))
5488 info->callbacks->einfo
5489 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5490 input_bfd, input_section, rel->r_offset, symname);
5494 info->callbacks->einfo
5495 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5496 input_bfd, input_section, rel->r_offset, symname);
5499 elf_elfheader (output_bfd)->e_flags |= EF_SH_PIC;
5502 if (r != bfd_reloc_ok)
5507 case bfd_reloc_outofrange:
5509 case bfd_reloc_overflow:
5517 name = (bfd_elf_string_from_elf_section
5518 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5522 name = bfd_section_name (input_bfd, sec);
5524 if (! ((*info->callbacks->reloc_overflow)
5525 (info, (h ? &h->root : NULL), name, howto->name,
5526 (bfd_vma) 0, input_bfd, input_section,
5538 /* This is a version of bfd_generic_get_relocated_section_contents
5539 which uses sh_elf_relocate_section. */
5542 sh_elf_get_relocated_section_contents (bfd *output_bfd,
5543 struct bfd_link_info *link_info,
5544 struct bfd_link_order *link_order,
5546 bfd_boolean relocatable,
5549 Elf_Internal_Shdr *symtab_hdr;
5550 asection *input_section = link_order->u.indirect.section;
5551 bfd *input_bfd = input_section->owner;
5552 asection **sections = NULL;
5553 Elf_Internal_Rela *internal_relocs = NULL;
5554 Elf_Internal_Sym *isymbuf = NULL;
5556 /* We only need to handle the case of relaxing, or of having a
5557 particular set of section contents, specially. */
5559 || elf_section_data (input_section)->this_hdr.contents == NULL)
5560 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5565 symtab_hdr = &elf_symtab_hdr (input_bfd);
5567 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5568 (size_t) input_section->size);
5570 if ((input_section->flags & SEC_RELOC) != 0
5571 && input_section->reloc_count > 0)
5574 Elf_Internal_Sym *isym, *isymend;
5577 internal_relocs = (_bfd_elf_link_read_relocs
5578 (input_bfd, input_section, NULL,
5579 (Elf_Internal_Rela *) NULL, FALSE));
5580 if (internal_relocs == NULL)
5583 if (symtab_hdr->sh_info != 0)
5585 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5586 if (isymbuf == NULL)
5587 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5588 symtab_hdr->sh_info, 0,
5590 if (isymbuf == NULL)
5594 amt = symtab_hdr->sh_info;
5595 amt *= sizeof (asection *);
5596 sections = (asection **) bfd_malloc (amt);
5597 if (sections == NULL && amt != 0)
5600 isymend = isymbuf + symtab_hdr->sh_info;
5601 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5605 if (isym->st_shndx == SHN_UNDEF)
5606 isec = bfd_und_section_ptr;
5607 else if (isym->st_shndx == SHN_ABS)
5608 isec = bfd_abs_section_ptr;
5609 else if (isym->st_shndx == SHN_COMMON)
5610 isec = bfd_com_section_ptr;
5612 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5617 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5618 input_section, data, internal_relocs,
5622 if (sections != NULL)
5625 && symtab_hdr->contents != (unsigned char *) isymbuf)
5627 if (elf_section_data (input_section)->relocs != internal_relocs)
5628 free (internal_relocs);
5634 if (sections != NULL)
5637 && symtab_hdr->contents != (unsigned char *) isymbuf)
5639 if (internal_relocs != NULL
5640 && elf_section_data (input_section)->relocs != internal_relocs)
5641 free (internal_relocs);
5645 /* Return the base VMA address which should be subtracted from real addresses
5646 when resolving @dtpoff relocation.
5647 This is PT_TLS segment p_vaddr. */
5650 dtpoff_base (struct bfd_link_info *info)
5652 /* If tls_sec is NULL, we should have signalled an error already. */
5653 if (elf_hash_table (info)->tls_sec == NULL)
5655 return elf_hash_table (info)->tls_sec->vma;
5658 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5661 tpoff (struct bfd_link_info *info, bfd_vma address)
5663 /* If tls_sec is NULL, we should have signalled an error already. */
5664 if (elf_hash_table (info)->tls_sec == NULL)
5666 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5667 structure which has 2 pointer fields. */
5668 return (address - elf_hash_table (info)->tls_sec->vma
5669 + align_power ((bfd_vma) 8,
5670 elf_hash_table (info)->tls_sec->alignment_power));
5674 sh_elf_gc_mark_hook (asection *sec,
5675 struct bfd_link_info *info,
5676 Elf_Internal_Rela *rel,
5677 struct elf_link_hash_entry *h,
5678 Elf_Internal_Sym *sym)
5681 switch (ELF32_R_TYPE (rel->r_info))
5683 case R_SH_GNU_VTINHERIT:
5684 case R_SH_GNU_VTENTRY:
5688 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5691 /* Update the got entry reference counts for the section being removed. */
5694 sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5695 asection *sec, const Elf_Internal_Rela *relocs)
5697 Elf_Internal_Shdr *symtab_hdr;
5698 struct elf_link_hash_entry **sym_hashes;
5699 bfd_signed_vma *local_got_refcounts;
5700 union gotref *local_funcdesc;
5701 const Elf_Internal_Rela *rel, *relend;
5703 if (bfd_link_relocatable (info))
5706 elf_section_data (sec)->local_dynrel = NULL;
5708 symtab_hdr = &elf_symtab_hdr (abfd);
5709 sym_hashes = elf_sym_hashes (abfd);
5710 local_got_refcounts = elf_local_got_refcounts (abfd);
5711 local_funcdesc = sh_elf_local_funcdesc (abfd);
5713 relend = relocs + sec->reloc_count;
5714 for (rel = relocs; rel < relend; rel++)
5716 unsigned long r_symndx;
5717 unsigned int r_type;
5718 struct elf_link_hash_entry *h = NULL;
5719 #ifdef INCLUDE_SHMEDIA
5720 int seen_stt_datalabel = 0;
5723 r_symndx = ELF32_R_SYM (rel->r_info);
5724 if (r_symndx >= symtab_hdr->sh_info)
5726 struct elf_sh_link_hash_entry *eh;
5727 struct elf_sh_dyn_relocs **pp;
5728 struct elf_sh_dyn_relocs *p;
5730 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5731 while (h->root.type == bfd_link_hash_indirect
5732 || h->root.type == bfd_link_hash_warning)
5734 #ifdef INCLUDE_SHMEDIA
5735 seen_stt_datalabel |= h->type == STT_DATALABEL;
5737 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5739 eh = (struct elf_sh_link_hash_entry *) h;
5740 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5743 /* Everything must go for SEC. */
5749 r_type = ELF32_R_TYPE (rel->r_info);
5750 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
5752 case R_SH_TLS_LD_32:
5753 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
5754 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
5762 #ifdef INCLUDE_SHMEDIA
5763 case R_SH_GOT_LOW16:
5764 case R_SH_GOT_MEDLOW16:
5765 case R_SH_GOT_MEDHI16:
5769 case R_SH_GOTOFF_LOW16:
5770 case R_SH_GOTOFF_MEDLOW16:
5771 case R_SH_GOTOFF_MEDHI16:
5772 case R_SH_GOTOFF_HI16:
5773 case R_SH_GOTPC_LOW16:
5774 case R_SH_GOTPC_MEDLOW16:
5775 case R_SH_GOTPC_MEDHI16:
5776 case R_SH_GOTPC_HI16:
5778 case R_SH_TLS_GD_32:
5779 case R_SH_TLS_IE_32:
5780 case R_SH_GOTFUNCDESC:
5781 case R_SH_GOTFUNCDESC20:
5784 #ifdef INCLUDE_SHMEDIA
5785 if (seen_stt_datalabel)
5787 struct elf_sh_link_hash_entry *eh;
5788 eh = (struct elf_sh_link_hash_entry *) h;
5789 if (eh->datalabel_got.refcount > 0)
5790 eh->datalabel_got.refcount -= 1;
5794 if (h->got.refcount > 0)
5795 h->got.refcount -= 1;
5797 else if (local_got_refcounts != NULL)
5799 #ifdef INCLUDE_SHMEDIA
5800 if (rel->r_addend & 1)
5802 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5803 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5807 if (local_got_refcounts[r_symndx] > 0)
5808 local_got_refcounts[r_symndx] -= 1;
5814 sh_elf_hash_entry (h)->abs_funcdesc_refcount -= 1;
5815 else if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_pic (info))
5816 sh_elf_hash_table (info)->srofixup->size -= 4;
5820 case R_SH_GOTOFFFUNCDESC:
5821 case R_SH_GOTOFFFUNCDESC20:
5823 sh_elf_hash_entry (h)->funcdesc.refcount -= 1;
5825 local_funcdesc[r_symndx].refcount -= 1;
5829 if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_pic (info)
5830 && (sec->flags & SEC_ALLOC) != 0)
5831 sh_elf_hash_table (info)->srofixup->size -= 4;
5835 if (bfd_link_pic (info))
5840 #ifdef INCLUDE_SHMEDIA
5841 case R_SH_PLT_LOW16:
5842 case R_SH_PLT_MEDLOW16:
5843 case R_SH_PLT_MEDHI16:
5848 if (h->plt.refcount > 0)
5849 h->plt.refcount -= 1;
5854 #ifdef INCLUDE_SHMEDIA
5855 case R_SH_GOTPLT_LOW16:
5856 case R_SH_GOTPLT_MEDLOW16:
5857 case R_SH_GOTPLT_MEDHI16:
5858 case R_SH_GOTPLT_HI16:
5859 case R_SH_GOTPLT10BY4:
5860 case R_SH_GOTPLT10BY8:
5864 struct elf_sh_link_hash_entry *eh;
5865 eh = (struct elf_sh_link_hash_entry *) h;
5866 if (eh->gotplt_refcount > 0)
5868 eh->gotplt_refcount -= 1;
5869 if (h->plt.refcount > 0)
5870 h->plt.refcount -= 1;
5872 #ifdef INCLUDE_SHMEDIA
5873 else if (seen_stt_datalabel)
5875 if (eh->datalabel_got.refcount > 0)
5876 eh->datalabel_got.refcount -= 1;
5879 else if (h->got.refcount > 0)
5880 h->got.refcount -= 1;
5882 else if (local_got_refcounts != NULL)
5884 #ifdef INCLUDE_SHMEDIA
5885 if (rel->r_addend & 1)
5887 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5888 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5892 if (local_got_refcounts[r_symndx] > 0)
5893 local_got_refcounts[r_symndx] -= 1;
5905 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5908 sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
5909 struct elf_link_hash_entry *dir,
5910 struct elf_link_hash_entry *ind)
5912 struct elf_sh_link_hash_entry *edir, *eind;
5914 edir = (struct elf_sh_link_hash_entry *) dir;
5915 eind = (struct elf_sh_link_hash_entry *) ind;
5917 if (eind->dyn_relocs != NULL)
5919 if (edir->dyn_relocs != NULL)
5921 struct elf_sh_dyn_relocs **pp;
5922 struct elf_sh_dyn_relocs *p;
5924 /* Add reloc counts against the indirect sym to the direct sym
5925 list. Merge any entries against the same section. */
5926 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5928 struct elf_sh_dyn_relocs *q;
5930 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5931 if (q->sec == p->sec)
5933 q->pc_count += p->pc_count;
5934 q->count += p->count;
5941 *pp = edir->dyn_relocs;
5944 edir->dyn_relocs = eind->dyn_relocs;
5945 eind->dyn_relocs = NULL;
5947 edir->gotplt_refcount = eind->gotplt_refcount;
5948 eind->gotplt_refcount = 0;
5949 #ifdef INCLUDE_SHMEDIA
5950 edir->datalabel_got.refcount += eind->datalabel_got.refcount;
5951 eind->datalabel_got.refcount = 0;
5953 edir->funcdesc.refcount += eind->funcdesc.refcount;
5954 eind->funcdesc.refcount = 0;
5955 edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
5956 eind->abs_funcdesc_refcount = 0;
5958 if (ind->root.type == bfd_link_hash_indirect
5959 && dir->got.refcount <= 0)
5961 edir->got_type = eind->got_type;
5962 eind->got_type = GOT_UNKNOWN;
5965 if (ind->root.type != bfd_link_hash_indirect
5966 && dir->dynamic_adjusted)
5968 /* If called to transfer flags for a weakdef during processing
5969 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5970 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5971 dir->ref_dynamic |= ind->ref_dynamic;
5972 dir->ref_regular |= ind->ref_regular;
5973 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
5974 dir->needs_plt |= ind->needs_plt;
5977 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
5981 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
5984 if (bfd_link_pic (info))
5989 case R_SH_TLS_GD_32:
5990 case R_SH_TLS_IE_32:
5992 return R_SH_TLS_LE_32;
5993 return R_SH_TLS_IE_32;
5994 case R_SH_TLS_LD_32:
5995 return R_SH_TLS_LE_32;
6001 /* Look through the relocs for a section during the first phase.
6002 Since we don't do .gots or .plts, we just need to consider the
6003 virtual table relocs for gc. */
6006 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
6007 const Elf_Internal_Rela *relocs)
6009 Elf_Internal_Shdr *symtab_hdr;
6010 struct elf_link_hash_entry **sym_hashes;
6011 struct elf_sh_link_hash_table *htab;
6012 const Elf_Internal_Rela *rel;
6013 const Elf_Internal_Rela *rel_end;
6015 unsigned int r_type;
6016 enum got_type got_type, old_got_type;
6020 if (bfd_link_relocatable (info))
6023 BFD_ASSERT (is_sh_elf (abfd));
6025 symtab_hdr = &elf_symtab_hdr (abfd);
6026 sym_hashes = elf_sym_hashes (abfd);
6028 htab = sh_elf_hash_table (info);
6032 rel_end = relocs + sec->reloc_count;
6033 for (rel = relocs; rel < rel_end; rel++)
6035 struct elf_link_hash_entry *h;
6036 unsigned long r_symndx;
6037 #ifdef INCLUDE_SHMEDIA
6038 int seen_stt_datalabel = 0;
6041 r_symndx = ELF32_R_SYM (rel->r_info);
6042 r_type = ELF32_R_TYPE (rel->r_info);
6044 if (r_symndx < symtab_hdr->sh_info)
6048 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6049 while (h->root.type == bfd_link_hash_indirect
6050 || h->root.type == bfd_link_hash_warning)
6052 #ifdef INCLUDE_SHMEDIA
6053 seen_stt_datalabel |= h->type == STT_DATALABEL;
6055 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6058 /* PR15323, ref flags aren't set for references in the same
6060 h->root.non_ir_ref = 1;
6063 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
6064 if (! bfd_link_pic (info)
6065 && r_type == R_SH_TLS_IE_32
6067 && h->root.type != bfd_link_hash_undefined
6068 && h->root.type != bfd_link_hash_undefweak
6069 && (h->dynindx == -1
6071 r_type = R_SH_TLS_LE_32;
6076 case R_SH_GOTOFFFUNCDESC:
6077 case R_SH_GOTOFFFUNCDESC20:
6079 case R_SH_GOTFUNCDESC:
6080 case R_SH_GOTFUNCDESC20:
6083 if (h->dynindx == -1)
6084 switch (ELF_ST_VISIBILITY (h->other))
6090 bfd_elf_link_record_dynamic_symbol (info, h);
6097 /* Some relocs require a global offset table. */
6098 if (htab->sgot == NULL)
6103 /* This may require an rofixup. */
6112 case R_SH_GOTFUNCDESC:
6113 case R_SH_GOTFUNCDESC20:
6114 case R_SH_GOTOFFFUNCDESC:
6115 case R_SH_GOTOFFFUNCDESC20:
6117 #ifdef INCLUDE_SHMEDIA
6118 case R_SH_GOTPLT_LOW16:
6119 case R_SH_GOTPLT_MEDLOW16:
6120 case R_SH_GOTPLT_MEDHI16:
6121 case R_SH_GOTPLT_HI16:
6122 case R_SH_GOTPLT10BY4:
6123 case R_SH_GOTPLT10BY8:
6124 case R_SH_GOT_LOW16:
6125 case R_SH_GOT_MEDLOW16:
6126 case R_SH_GOT_MEDHI16:
6130 case R_SH_GOTOFF_LOW16:
6131 case R_SH_GOTOFF_MEDLOW16:
6132 case R_SH_GOTOFF_MEDHI16:
6133 case R_SH_GOTOFF_HI16:
6134 case R_SH_GOTPC_LOW16:
6135 case R_SH_GOTPC_MEDLOW16:
6136 case R_SH_GOTPC_MEDHI16:
6137 case R_SH_GOTPC_HI16:
6139 case R_SH_TLS_GD_32:
6140 case R_SH_TLS_LD_32:
6141 case R_SH_TLS_IE_32:
6142 if (htab->root.dynobj == NULL)
6143 htab->root.dynobj = abfd;
6144 if (!create_got_section (htab->root.dynobj, info))
6155 /* This relocation describes the C++ object vtable hierarchy.
6156 Reconstruct it for later use during GC. */
6157 case R_SH_GNU_VTINHERIT:
6158 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6162 /* This relocation describes which C++ vtable entries are actually
6163 used. Record for later use during GC. */
6164 case R_SH_GNU_VTENTRY:
6165 BFD_ASSERT (h != NULL);
6167 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6171 case R_SH_TLS_IE_32:
6172 if (bfd_link_pic (info))
6173 info->flags |= DF_STATIC_TLS;
6177 case R_SH_TLS_GD_32:
6180 #ifdef INCLUDE_SHMEDIA
6181 case R_SH_GOT_LOW16:
6182 case R_SH_GOT_MEDLOW16:
6183 case R_SH_GOT_MEDHI16:
6188 case R_SH_GOTFUNCDESC:
6189 case R_SH_GOTFUNCDESC20:
6193 got_type = GOT_NORMAL;
6195 case R_SH_TLS_GD_32:
6196 got_type = GOT_TLS_GD;
6198 case R_SH_TLS_IE_32:
6199 got_type = GOT_TLS_IE;
6201 case R_SH_GOTFUNCDESC:
6202 case R_SH_GOTFUNCDESC20:
6203 got_type = GOT_FUNCDESC;
6209 #ifdef INCLUDE_SHMEDIA
6210 if (seen_stt_datalabel)
6212 struct elf_sh_link_hash_entry *eh
6213 = (struct elf_sh_link_hash_entry *) h;
6215 eh->datalabel_got.refcount += 1;
6219 h->got.refcount += 1;
6220 old_got_type = sh_elf_hash_entry (h)->got_type;
6224 bfd_signed_vma *local_got_refcounts;
6226 /* This is a global offset table entry for a local
6228 local_got_refcounts = elf_local_got_refcounts (abfd);
6229 if (local_got_refcounts == NULL)
6233 size = symtab_hdr->sh_info;
6234 size *= sizeof (bfd_signed_vma);
6235 #ifdef INCLUDE_SHMEDIA
6236 /* Reserve space for both the datalabel and
6237 codelabel local GOT offsets. */
6240 size += symtab_hdr->sh_info;
6241 local_got_refcounts = ((bfd_signed_vma *)
6242 bfd_zalloc (abfd, size));
6243 if (local_got_refcounts == NULL)
6245 elf_local_got_refcounts (abfd) = local_got_refcounts;
6246 #ifdef INCLUDE_SHMEDIA
6247 /* Take care of both the datalabel and codelabel local
6249 sh_elf_local_got_type (abfd)
6250 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6252 sh_elf_local_got_type (abfd)
6253 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6256 #ifdef INCLUDE_SHMEDIA
6257 if (rel->r_addend & 1)
6258 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6261 local_got_refcounts[r_symndx] += 1;
6262 old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
6265 /* If a TLS symbol is accessed using IE at least once,
6266 there is no point to use dynamic model for it. */
6267 if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
6268 && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
6270 if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
6271 got_type = GOT_TLS_IE;
6274 if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
6275 && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
6276 (*_bfd_error_handler)
6277 (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6278 abfd, h->root.root.string);
6279 else if (old_got_type == GOT_FUNCDESC
6280 || got_type == GOT_FUNCDESC)
6281 (*_bfd_error_handler)
6282 (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6283 abfd, h->root.root.string);
6285 (*_bfd_error_handler)
6286 (_("%B: `%s' accessed both as normal and thread local symbol"),
6287 abfd, h->root.root.string);
6292 if (old_got_type != got_type)
6295 sh_elf_hash_entry (h)->got_type = got_type;
6297 sh_elf_local_got_type (abfd) [r_symndx] = got_type;
6302 case R_SH_TLS_LD_32:
6303 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6307 case R_SH_GOTOFFFUNCDESC:
6308 case R_SH_GOTOFFFUNCDESC20:
6311 (*_bfd_error_handler)
6312 (_("%B: Function descriptor relocation with non-zero addend"),
6319 union gotref *local_funcdesc;
6321 /* We need a function descriptor for a local symbol. */
6322 local_funcdesc = sh_elf_local_funcdesc (abfd);
6323 if (local_funcdesc == NULL)
6327 size = symtab_hdr->sh_info * sizeof (union gotref);
6328 #ifdef INCLUDE_SHMEDIA
6329 /* Count datalabel local GOT. */
6332 local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
6333 if (local_funcdesc == NULL)
6335 sh_elf_local_funcdesc (abfd) = local_funcdesc;
6337 local_funcdesc[r_symndx].refcount += 1;
6339 if (r_type == R_SH_FUNCDESC)
6341 if (!bfd_link_pic (info))
6342 htab->srofixup->size += 4;
6344 htab->srelgot->size += sizeof (Elf32_External_Rela);
6349 sh_elf_hash_entry (h)->funcdesc.refcount++;
6350 if (r_type == R_SH_FUNCDESC)
6351 sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
6353 /* If there is a function descriptor reference, then
6354 there should not be any non-FDPIC references. */
6355 old_got_type = sh_elf_hash_entry (h)->got_type;
6356 if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
6358 if (old_got_type == GOT_NORMAL)
6359 (*_bfd_error_handler)
6360 (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6361 abfd, h->root.root.string);
6363 (*_bfd_error_handler)
6364 (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6365 abfd, h->root.root.string);
6371 #ifdef INCLUDE_SHMEDIA
6372 case R_SH_GOTPLT_LOW16:
6373 case R_SH_GOTPLT_MEDLOW16:
6374 case R_SH_GOTPLT_MEDHI16:
6375 case R_SH_GOTPLT_HI16:
6376 case R_SH_GOTPLT10BY4:
6377 case R_SH_GOTPLT10BY8:
6379 /* If this is a local symbol, we resolve it directly without
6380 creating a procedure linkage table entry. */
6384 || ! bfd_link_pic (info)
6386 || h->dynindx == -1)
6390 h->plt.refcount += 1;
6391 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6396 #ifdef INCLUDE_SHMEDIA
6397 case R_SH_PLT_LOW16:
6398 case R_SH_PLT_MEDLOW16:
6399 case R_SH_PLT_MEDHI16:
6402 /* This symbol requires a procedure linkage table entry. We
6403 actually build the entry in adjust_dynamic_symbol,
6404 because this might be a case of linking PIC code which is
6405 never referenced by a dynamic object, in which case we
6406 don't need to generate a procedure linkage table entry
6409 /* If this is a local symbol, we resolve it directly without
6410 creating a procedure linkage table entry. */
6414 if (h->forced_local)
6418 h->plt.refcount += 1;
6423 #ifdef INCLUDE_SHMEDIA
6424 case R_SH_IMM_LOW16_PCREL:
6425 case R_SH_IMM_MEDLOW16_PCREL:
6426 case R_SH_IMM_MEDHI16_PCREL:
6427 case R_SH_IMM_HI16_PCREL:
6429 if (h != NULL && ! bfd_link_pic (info))
6432 h->plt.refcount += 1;
6435 /* If we are creating a shared library, and this is a reloc
6436 against a global symbol, or a non PC relative reloc
6437 against a local symbol, then we need to copy the reloc
6438 into the shared library. However, if we are linking with
6439 -Bsymbolic, we do not need to copy a reloc against a
6440 global symbol which is defined in an object we are
6441 including in the link (i.e., DEF_REGULAR is set). At
6442 this point we have not seen all the input files, so it is
6443 possible that DEF_REGULAR is not set now but will be set
6444 later (it is never cleared). We account for that
6445 possibility below by storing information in the
6446 dyn_relocs field of the hash table entry. A similar
6447 situation occurs when creating shared libraries and symbol
6448 visibility changes render the symbol local.
6450 If on the other hand, we are creating an executable, we
6451 may need to keep relocations for symbols satisfied by a
6452 dynamic library if we manage to avoid copy relocs for the
6454 if ((bfd_link_pic (info)
6455 && (sec->flags & SEC_ALLOC) != 0
6456 && (r_type != R_SH_REL32
6458 && (! info->symbolic
6459 || h->root.type == bfd_link_hash_defweak
6460 || !h->def_regular))))
6461 || (! bfd_link_pic (info)
6462 && (sec->flags & SEC_ALLOC) != 0
6464 && (h->root.type == bfd_link_hash_defweak
6465 || !h->def_regular)))
6467 struct elf_sh_dyn_relocs *p;
6468 struct elf_sh_dyn_relocs **head;
6470 if (htab->root.dynobj == NULL)
6471 htab->root.dynobj = abfd;
6473 /* When creating a shared object, we must copy these
6474 reloc types into the output file. We create a reloc
6475 section in dynobj and make room for this reloc. */
6478 sreloc = _bfd_elf_make_dynamic_reloc_section
6479 (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE);
6485 /* If this is a global symbol, we count the number of
6486 relocations we need for this symbol. */
6488 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6491 /* Track dynamic relocs needed for local syms too. */
6494 Elf_Internal_Sym *isym;
6496 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6501 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6505 vpp = &elf_section_data (s)->local_dynrel;
6506 head = (struct elf_sh_dyn_relocs **) vpp;
6510 if (p == NULL || p->sec != sec)
6512 bfd_size_type amt = sizeof (*p);
6513 p = bfd_alloc (htab->root.dynobj, amt);
6524 if (r_type == R_SH_REL32
6525 #ifdef INCLUDE_SHMEDIA
6526 || r_type == R_SH_IMM_LOW16_PCREL
6527 || r_type == R_SH_IMM_MEDLOW16_PCREL
6528 || r_type == R_SH_IMM_MEDHI16_PCREL
6529 || r_type == R_SH_IMM_HI16_PCREL
6535 /* Allocate the fixup regardless of whether we need a relocation.
6536 If we end up generating the relocation, we'll unallocate the
6538 if (htab->fdpic_p && !bfd_link_pic (info)
6539 && r_type == R_SH_DIR32
6540 && (sec->flags & SEC_ALLOC) != 0)
6541 htab->srofixup->size += 4;
6544 case R_SH_TLS_LE_32:
6545 if (bfd_link_dll (info))
6547 (*_bfd_error_handler)
6548 (_("%B: TLS local exec code cannot be linked into shared objects"),
6555 case R_SH_TLS_LDO_32:
6556 /* Nothing to do. */
6567 #ifndef sh_elf_set_mach_from_flags
6568 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
6571 sh_elf_set_mach_from_flags (bfd *abfd)
6573 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
6575 if (flags >= sizeof(sh_ef_bfd_table))
6578 if (sh_ef_bfd_table[flags] == 0)
6581 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
6587 /* Reverse table lookup for sh_ef_bfd_table[].
6588 Given a bfd MACH value from archures.c
6589 return the equivalent ELF flags from the table.
6590 Return -1 if no match is found. */
6593 sh_elf_get_flags_from_mach (unsigned long mach)
6595 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
6598 if (sh_ef_bfd_table[i] == mach)
6601 /* shouldn't get here */
6606 #endif /* not sh_elf_set_mach_from_flags */
6608 #ifndef sh_elf_copy_private_data
6609 /* Copy backend specific data from one object module to another */
6612 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
6614 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6617 if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
6620 return sh_elf_set_mach_from_flags (obfd);
6622 #endif /* not sh_elf_copy_private_data */
6624 #ifndef sh_elf_merge_private_data
6626 /* This function returns the ELF architecture number that
6627 corresponds to the given arch_sh* flags. */
6630 sh_find_elf_flags (unsigned int arch_set)
6632 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
6633 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
6635 return sh_elf_get_flags_from_mach (bfd_mach);
6638 /* This routine initialises the elf flags when required and
6639 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
6642 sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
6644 extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *);
6646 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6649 if (! elf_flags_init (obfd))
6651 /* This happens when ld starts out with a 'blank' output file. */
6652 elf_flags_init (obfd) = TRUE;
6653 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6654 sh_elf_set_mach_from_flags (obfd);
6655 if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
6656 elf_elfheader (obfd)->e_flags &= ~EF_SH_PIC;
6659 if (! sh_merge_bfd_arch (ibfd, obfd))
6661 _bfd_error_handler ("%B: uses instructions which are incompatible "
6662 "with instructions used in previous modules",
6664 bfd_set_error (bfd_error_bad_value);
6668 elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
6669 elf_elfheader (obfd)->e_flags |=
6670 sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
6672 if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
6674 _bfd_error_handler ("%B: attempt to mix FDPIC and non-FDPIC objects",
6676 bfd_set_error (bfd_error_bad_value);
6682 #endif /* not sh_elf_merge_private_data */
6684 /* Override the generic function because we need to store sh_elf_obj_tdata
6685 as the specific tdata. We set also the machine architecture from flags
6689 sh_elf_object_p (bfd *abfd)
6691 if (! sh_elf_set_mach_from_flags (abfd))
6694 return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
6695 == fdpic_object_p (abfd));
6698 /* Finish up dynamic symbol handling. We set the contents of various
6699 dynamic sections here. */
6702 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6703 struct elf_link_hash_entry *h,
6704 Elf_Internal_Sym *sym)
6706 struct elf_sh_link_hash_table *htab;
6708 htab = sh_elf_hash_table (info);
6712 if (h->plt.offset != (bfd_vma) -1)
6720 Elf_Internal_Rela rel;
6722 const struct elf_sh_plt_info *plt_info;
6724 /* This symbol has an entry in the procedure linkage table. Set
6727 BFD_ASSERT (h->dynindx != -1);
6730 sgotplt = htab->sgotplt;
6731 srelplt = htab->srelplt;
6732 BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
6734 /* Get the index in the procedure linkage table which
6735 corresponds to this symbol. This is the index of this symbol
6736 in all the symbols for which we are making plt entries. The
6737 first entry in the procedure linkage table is reserved. */
6738 plt_index = get_plt_index (htab->plt_info, h->plt.offset);
6740 plt_info = htab->plt_info;
6741 if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
6742 plt_info = plt_info->short_plt;
6744 /* Get the offset into the .got table of the entry that
6745 corresponds to this function. */
6747 /* The offset must be relative to the GOT symbol, twelve bytes
6748 before the end of .got.plt. Each descriptor is eight
6750 got_offset = plt_index * 8 + 12 - sgotplt->size;
6752 /* Each .got entry is 4 bytes. The first three are
6754 got_offset = (plt_index + 3) * 4;
6757 if (bfd_link_pic (info))
6758 got_offset -= GOT_BIAS;
6761 /* Fill in the entry in the procedure linkage table. */
6762 memcpy (splt->contents + h->plt.offset,
6763 plt_info->symbol_entry,
6764 plt_info->symbol_entry_size);
6766 if (bfd_link_pic (info) || htab->fdpic_p)
6768 if (plt_info->symbol_fields.got20)
6770 bfd_reloc_status_type r;
6771 r = install_movi20_field (output_bfd, got_offset,
6772 splt->owner, splt, splt->contents,
6774 + plt_info->symbol_fields.got_entry);
6775 BFD_ASSERT (r == bfd_reloc_ok);
6778 install_plt_field (output_bfd, FALSE, got_offset,
6781 + plt_info->symbol_fields.got_entry));
6785 BFD_ASSERT (!plt_info->symbol_fields.got20);
6787 install_plt_field (output_bfd, FALSE,
6788 (sgotplt->output_section->vma
6789 + sgotplt->output_offset
6793 + plt_info->symbol_fields.got_entry));
6794 if (htab->vxworks_p)
6796 unsigned int reachable_plts, plts_per_4k;
6799 /* Divide the PLT into groups. The first group contains
6800 REACHABLE_PLTS entries and the other groups contain
6801 PLTS_PER_4K entries. Entries in the first group can
6802 branch directly to .plt; those in later groups branch
6803 to the last element of the previous group. */
6804 /* ??? It would be better to create multiple copies of
6805 the common resolver stub. */
6806 reachable_plts = ((4096
6807 - plt_info->plt0_entry_size
6808 - (plt_info->symbol_fields.plt + 4))
6809 / plt_info->symbol_entry_size) + 1;
6810 plts_per_4k = (4096 / plt_info->symbol_entry_size);
6811 if (plt_index < reachable_plts)
6812 distance = -(h->plt.offset
6813 + plt_info->symbol_fields.plt);
6815 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
6816 * plt_info->symbol_entry_size);
6818 /* Install the 'bra' with this offset. */
6819 bfd_put_16 (output_bfd,
6820 0xa000 | (0x0fff & ((distance - 4) / 2)),
6823 + plt_info->symbol_fields.plt));
6826 install_plt_field (output_bfd, TRUE,
6827 splt->output_section->vma + splt->output_offset,
6830 + plt_info->symbol_fields.plt));
6833 /* Make got_offset relative to the start of .got.plt. */
6835 if (bfd_link_pic (info))
6836 got_offset += GOT_BIAS;
6839 got_offset = plt_index * 8;
6841 if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
6842 install_plt_field (output_bfd, FALSE,
6843 plt_index * sizeof (Elf32_External_Rela),
6846 + plt_info->symbol_fields.reloc_offset));
6848 /* Fill in the entry in the global offset table. */
6849 bfd_put_32 (output_bfd,
6850 (splt->output_section->vma
6851 + splt->output_offset
6853 + plt_info->symbol_resolve_offset),
6854 sgotplt->contents + got_offset);
6856 bfd_put_32 (output_bfd,
6857 sh_elf_osec_to_segment (output_bfd,
6858 htab->splt->output_section),
6859 sgotplt->contents + got_offset + 4);
6861 /* Fill in the entry in the .rela.plt section. */
6862 rel.r_offset = (sgotplt->output_section->vma
6863 + sgotplt->output_offset
6866 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
6868 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6871 rel.r_addend = GOT_BIAS;
6873 loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
6874 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6876 if (htab->vxworks_p && !bfd_link_pic (info))
6878 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6879 Begin by pointing LOC to the first such relocation. */
6880 loc = (htab->srelplt2->contents
6881 + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
6883 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6884 for the PLT entry's pointer to the .got.plt entry. */
6885 rel.r_offset = (htab->splt->output_section->vma
6886 + htab->splt->output_offset
6888 + plt_info->symbol_fields.got_entry);
6889 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6890 rel.r_addend = got_offset;
6891 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6892 loc += sizeof (Elf32_External_Rela);
6894 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6895 the .got.plt entry, which initially points to .plt. */
6896 rel.r_offset = (sgotplt->output_section->vma
6897 + sgotplt->output_offset
6899 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
6901 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6904 if (!h->def_regular)
6906 /* Mark the symbol as undefined, rather than as defined in
6907 the .plt section. Leave the value alone. */
6908 sym->st_shndx = SHN_UNDEF;
6912 if (h->got.offset != (bfd_vma) -1
6913 && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
6914 && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
6915 && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
6919 Elf_Internal_Rela rel;
6922 /* This symbol has an entry in the global offset table. Set it
6926 srelgot = htab->srelgot;
6927 BFD_ASSERT (sgot != NULL && srelgot != NULL);
6929 rel.r_offset = (sgot->output_section->vma
6930 + sgot->output_offset
6931 + (h->got.offset &~ (bfd_vma) 1));
6933 /* If this is a static link, or it is a -Bsymbolic link and the
6934 symbol is defined locally or was forced to be local because
6935 of a version file, we just want to emit a RELATIVE reloc.
6936 The entry in the global offset table will already have been
6937 initialized in the relocate_section function. */
6938 if (bfd_link_pic (info)
6939 && SYMBOL_REFERENCES_LOCAL (info, h))
6943 asection *sec = h->root.u.def.section;
6945 = elf_section_data (sec->output_section)->dynindx;
6947 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6948 rel.r_addend = (h->root.u.def.value
6949 + h->root.u.def.section->output_offset);
6953 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6954 rel.r_addend = (h->root.u.def.value
6955 + h->root.u.def.section->output_section->vma
6956 + h->root.u.def.section->output_offset);
6961 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6962 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6966 loc = srelgot->contents;
6967 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6968 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6971 #ifdef INCLUDE_SHMEDIA
6973 struct elf_sh_link_hash_entry *eh;
6975 eh = (struct elf_sh_link_hash_entry *) h;
6976 if (eh->datalabel_got.offset != (bfd_vma) -1)
6980 Elf_Internal_Rela rel;
6983 /* This symbol has a datalabel entry in the global offset table.
6987 srelgot = htab->srelgot;
6988 BFD_ASSERT (sgot != NULL && srelgot != NULL);
6990 rel.r_offset = (sgot->output_section->vma
6991 + sgot->output_offset
6992 + (eh->datalabel_got.offset &~ (bfd_vma) 1));
6994 /* If this is a static link, or it is a -Bsymbolic link and the
6995 symbol is defined locally or was forced to be local because
6996 of a version file, we just want to emit a RELATIVE reloc.
6997 The entry in the global offset table will already have been
6998 initialized in the relocate_section function. */
6999 if (bfd_link_pic (info)
7000 && SYMBOL_REFERENCES_LOCAL (info, h))
7004 asection *sec = h->root.u.def.section;
7006 = elf_section_data (sec->output_section)->dynindx;
7008 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
7009 rel.r_addend = (h->root.u.def.value
7010 + h->root.u.def.section->output_offset);
7014 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
7015 rel.r_addend = (h->root.u.def.value
7016 + h->root.u.def.section->output_section->vma
7017 + h->root.u.def.section->output_offset);
7022 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
7023 + eh->datalabel_got.offset);
7024 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
7028 loc = srelgot->contents;
7029 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
7030 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7038 Elf_Internal_Rela rel;
7041 /* This symbol needs a copy reloc. Set it up. */
7043 BFD_ASSERT (h->dynindx != -1
7044 && (h->root.type == bfd_link_hash_defined
7045 || h->root.type == bfd_link_hash_defweak));
7047 s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
7048 BFD_ASSERT (s != NULL);
7050 rel.r_offset = (h->root.u.def.value
7051 + h->root.u.def.section->output_section->vma
7052 + h->root.u.def.section->output_offset);
7053 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
7055 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
7056 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7059 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
7060 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
7062 if (h == htab->root.hdynamic
7063 || (!htab->vxworks_p && h == htab->root.hgot))
7064 sym->st_shndx = SHN_ABS;
7069 /* Finish up the dynamic sections. */
7072 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
7074 struct elf_sh_link_hash_table *htab;
7078 htab = sh_elf_hash_table (info);
7082 sgotplt = htab->sgotplt;
7083 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
7085 if (htab->root.dynamic_sections_created)
7088 Elf32_External_Dyn *dyncon, *dynconend;
7090 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
7092 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7093 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
7094 for (; dyncon < dynconend; dyncon++)
7096 Elf_Internal_Dyn dyn;
7098 #ifdef INCLUDE_SHMEDIA
7102 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
7108 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
7109 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7112 #ifdef INCLUDE_SHMEDIA
7114 name = info->init_function;
7118 name = info->fini_function;
7120 if (dyn.d_un.d_val != 0)
7122 struct elf_link_hash_entry *h;
7124 h = elf_link_hash_lookup (&htab->root, name,
7125 FALSE, FALSE, TRUE);
7126 if (h != NULL && (h->other & STO_SH5_ISA32))
7128 dyn.d_un.d_val |= 1;
7129 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7136 BFD_ASSERT (htab->root.hgot != NULL);
7137 s = htab->root.hgot->root.u.def.section;
7138 dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
7139 + s->output_section->vma + s->output_offset;
7140 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7144 s = htab->srelplt->output_section;
7145 BFD_ASSERT (s != NULL);
7146 dyn.d_un.d_ptr = s->vma;
7147 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7151 s = htab->srelplt->output_section;
7152 BFD_ASSERT (s != NULL);
7153 dyn.d_un.d_val = s->size;
7154 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7158 /* My reading of the SVR4 ABI indicates that the
7159 procedure linkage table relocs (DT_JMPREL) should be
7160 included in the overall relocs (DT_RELA). This is
7161 what Solaris does. However, UnixWare can not handle
7162 that case. Therefore, we override the DT_RELASZ entry
7163 here to make it not include the JMPREL relocs. Since
7164 the linker script arranges for .rela.plt to follow all
7165 other relocation sections, we don't have to worry
7166 about changing the DT_RELA entry. */
7167 if (htab->srelplt != NULL)
7169 s = htab->srelplt->output_section;
7170 dyn.d_un.d_val -= s->size;
7172 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7177 /* Fill in the first entry in the procedure linkage table. */
7179 if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
7183 memcpy (splt->contents,
7184 htab->plt_info->plt0_entry,
7185 htab->plt_info->plt0_entry_size);
7186 for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
7187 if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
7188 install_plt_field (output_bfd, FALSE,
7189 (sgotplt->output_section->vma
7190 + sgotplt->output_offset
7193 + htab->plt_info->plt0_got_fields[i]));
7195 if (htab->vxworks_p)
7197 /* Finalize the .rela.plt.unloaded contents. */
7198 Elf_Internal_Rela rel;
7201 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
7202 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
7203 loc = htab->srelplt2->contents;
7204 rel.r_offset = (splt->output_section->vma
7205 + splt->output_offset
7206 + htab->plt_info->plt0_got_fields[2]);
7207 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
7209 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7210 loc += sizeof (Elf32_External_Rela);
7212 /* Fix up the remaining .rela.plt.unloaded relocations.
7213 They may have the wrong symbol index for _G_O_T_ or
7214 _P_L_T_ depending on the order in which symbols were
7216 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
7218 /* The PLT entry's pointer to the .got.plt slot. */
7219 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7220 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
7222 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7223 loc += sizeof (Elf32_External_Rela);
7225 /* The .got.plt slot's pointer to .plt. */
7226 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7227 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
7229 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7230 loc += sizeof (Elf32_External_Rela);
7234 /* UnixWare sets the entsize of .plt to 4, although that doesn't
7235 really seem like the right value. */
7236 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7240 /* Fill in the first three entries in the global offset table. */
7241 if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
7244 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
7246 bfd_put_32 (output_bfd,
7247 sdyn->output_section->vma + sdyn->output_offset,
7249 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
7250 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
7253 if (sgotplt && sgotplt->size > 0)
7254 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
7256 /* At the very end of the .rofixup section is a pointer to the GOT. */
7257 if (htab->fdpic_p && htab->srofixup != NULL)
7259 struct elf_link_hash_entry *hgot = htab->root.hgot;
7260 bfd_vma got_value = hgot->root.u.def.value
7261 + hgot->root.u.def.section->output_section->vma
7262 + hgot->root.u.def.section->output_offset;
7264 sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
7266 /* Make sure we allocated and generated the same number of fixups. */
7267 BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
7270 if (htab->srelfuncdesc)
7271 BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
7272 == htab->srelfuncdesc->size);
7275 BFD_ASSERT (htab->srelgot->reloc_count * sizeof (Elf32_External_Rela)
7276 == htab->srelgot->size);
7281 static enum elf_reloc_type_class
7282 sh_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
7283 const asection *rel_sec ATTRIBUTE_UNUSED,
7284 const Elf_Internal_Rela *rela)
7286 switch ((int) ELF32_R_TYPE (rela->r_info))
7289 return reloc_class_relative;
7291 return reloc_class_plt;
7293 return reloc_class_copy;
7295 return reloc_class_normal;
7299 #if !defined SH_TARGET_ALREADY_DEFINED
7300 /* Support for Linux core dump NOTE sections. */
7303 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7308 switch (note->descsz)
7313 case 168: /* Linux/SH */
7315 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
7318 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
7327 /* Make a ".reg/999" section. */
7328 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7329 size, note->descpos + offset);
7333 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7335 switch (note->descsz)
7340 case 124: /* Linux/SH elf_prpsinfo */
7341 elf_tdata (abfd)->core->program
7342 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7343 elf_tdata (abfd)->core->command
7344 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7347 /* Note that for some reason, a spurious space is tacked
7348 onto the end of the args in some (at least one anyway)
7349 implementations, so strip it off if it exists. */
7352 char *command = elf_tdata (abfd)->core->command;
7353 int n = strlen (command);
7355 if (0 < n && command[n - 1] == ' ')
7356 command[n - 1] = '\0';
7361 #endif /* not SH_TARGET_ALREADY_DEFINED */
7364 /* Return address for Ith PLT stub in section PLT, for relocation REL
7365 or (bfd_vma) -1 if it should not be included. */
7368 sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
7369 const arelent *rel ATTRIBUTE_UNUSED)
7371 const struct elf_sh_plt_info *plt_info;
7373 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
7374 return plt->vma + get_plt_offset (plt_info, i);
7377 /* Decide whether to attempt to turn absptr or lsda encodings in
7378 shared libraries into pcrel within the given input section. */
7381 sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
7382 struct bfd_link_info *info,
7383 asection *eh_frame_section ATTRIBUTE_UNUSED)
7385 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7387 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
7394 /* Adjust the contents of an eh_frame_hdr section before they're output. */
7397 sh_elf_encode_eh_address (bfd *abfd,
7398 struct bfd_link_info *info,
7399 asection *osec, bfd_vma offset,
7400 asection *loc_sec, bfd_vma loc_offset,
7403 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7404 struct elf_link_hash_entry *h;
7407 return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
7408 loc_offset, encoded);
7410 h = htab->root.hgot;
7411 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
7413 if (! h || (sh_elf_osec_to_segment (abfd, osec)
7414 == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
7415 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
7416 loc_sec, loc_offset, encoded);
7418 BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
7419 == (sh_elf_osec_to_segment
7420 (abfd, h->root.u.def.section->output_section)));
7422 *encoded = osec->vma + offset
7423 - (h->root.u.def.value
7424 + h->root.u.def.section->output_section->vma
7425 + h->root.u.def.section->output_offset);
7427 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
7430 #if !defined SH_TARGET_ALREADY_DEFINED
7431 #define TARGET_BIG_SYM sh_elf32_vec
7432 #define TARGET_BIG_NAME "elf32-sh"
7433 #define TARGET_LITTLE_SYM sh_elf32_le_vec
7434 #define TARGET_LITTLE_NAME "elf32-shl"
7437 #define ELF_ARCH bfd_arch_sh
7438 #define ELF_TARGET_ID SH_ELF_DATA
7439 #define ELF_MACHINE_CODE EM_SH
7440 #ifdef __QNXTARGET__
7441 #define ELF_MAXPAGESIZE 0x1000
7443 #define ELF_MAXPAGESIZE 0x80
7446 #define elf_symbol_leading_char '_'
7448 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7449 #define bfd_elf32_bfd_reloc_name_lookup \
7450 sh_elf_reloc_name_lookup
7451 #define elf_info_to_howto sh_elf_info_to_howto
7452 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
7453 #define elf_backend_relocate_section sh_elf_relocate_section
7454 #define bfd_elf32_bfd_get_relocated_section_contents \
7455 sh_elf_get_relocated_section_contents
7456 #define bfd_elf32_mkobject sh_elf_mkobject
7457 #define elf_backend_object_p sh_elf_object_p
7458 #define bfd_elf32_bfd_copy_private_bfd_data \
7459 sh_elf_copy_private_data
7460 #define bfd_elf32_bfd_merge_private_bfd_data \
7461 sh_elf_merge_private_data
7463 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
7464 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
7465 #define elf_backend_check_relocs sh_elf_check_relocs
7466 #define elf_backend_copy_indirect_symbol \
7467 sh_elf_copy_indirect_symbol
7468 #define elf_backend_create_dynamic_sections \
7469 sh_elf_create_dynamic_sections
7470 #define bfd_elf32_bfd_link_hash_table_create \
7471 sh_elf_link_hash_table_create
7472 #define elf_backend_adjust_dynamic_symbol \
7473 sh_elf_adjust_dynamic_symbol
7474 #define elf_backend_always_size_sections \
7475 sh_elf_always_size_sections
7476 #define elf_backend_size_dynamic_sections \
7477 sh_elf_size_dynamic_sections
7478 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
7479 #define elf_backend_finish_dynamic_symbol \
7480 sh_elf_finish_dynamic_symbol
7481 #define elf_backend_finish_dynamic_sections \
7482 sh_elf_finish_dynamic_sections
7483 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
7484 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
7485 #define elf_backend_can_make_relative_eh_frame \
7486 sh_elf_use_relative_eh_frame
7487 #define elf_backend_can_make_lsda_relative_eh_frame \
7488 sh_elf_use_relative_eh_frame
7489 #define elf_backend_encode_eh_address \
7490 sh_elf_encode_eh_address
7492 #define elf_backend_stack_align 8
7493 #define elf_backend_can_gc_sections 1
7494 #define elf_backend_can_refcount 1
7495 #define elf_backend_want_got_plt 1
7496 #define elf_backend_plt_readonly 1
7497 #define elf_backend_want_plt_sym 0
7498 #define elf_backend_got_header_size 12
7500 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
7502 #include "elf32-target.h"
7504 /* NetBSD support. */
7505 #undef TARGET_BIG_SYM
7506 #define TARGET_BIG_SYM sh_elf32_nbsd_vec
7507 #undef TARGET_BIG_NAME
7508 #define TARGET_BIG_NAME "elf32-sh-nbsd"
7509 #undef TARGET_LITTLE_SYM
7510 #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
7511 #undef TARGET_LITTLE_NAME
7512 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
7513 #undef ELF_MAXPAGESIZE
7514 #define ELF_MAXPAGESIZE 0x10000
7515 #undef ELF_COMMONPAGESIZE
7516 #undef elf_symbol_leading_char
7517 #define elf_symbol_leading_char 0
7519 #define elf32_bed elf32_sh_nbsd_bed
7521 #include "elf32-target.h"
7524 /* Linux support. */
7525 #undef TARGET_BIG_SYM
7526 #define TARGET_BIG_SYM sh_elf32_linux_be_vec
7527 #undef TARGET_BIG_NAME
7528 #define TARGET_BIG_NAME "elf32-shbig-linux"
7529 #undef TARGET_LITTLE_SYM
7530 #define TARGET_LITTLE_SYM sh_elf32_linux_vec
7531 #undef TARGET_LITTLE_NAME
7532 #define TARGET_LITTLE_NAME "elf32-sh-linux"
7533 #undef ELF_COMMONPAGESIZE
7534 #define ELF_COMMONPAGESIZE 0x1000
7536 #undef elf_backend_grok_prstatus
7537 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
7538 #undef elf_backend_grok_psinfo
7539 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
7541 #define elf32_bed elf32_sh_lin_bed
7543 #include "elf32-target.h"
7546 /* FDPIC support. */
7547 #undef TARGET_BIG_SYM
7548 #define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
7549 #undef TARGET_BIG_NAME
7550 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
7551 #undef TARGET_LITTLE_SYM
7552 #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
7553 #undef TARGET_LITTLE_NAME
7554 #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
7557 #define elf32_bed elf32_sh_fd_bed
7559 #include "elf32-target.h"
7561 #undef elf_backend_modify_program_headers
7563 /* VxWorks support. */
7564 #undef TARGET_BIG_SYM
7565 #define TARGET_BIG_SYM sh_elf32_vxworks_vec
7566 #undef TARGET_BIG_NAME
7567 #define TARGET_BIG_NAME "elf32-sh-vxworks"
7568 #undef TARGET_LITTLE_SYM
7569 #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
7570 #undef TARGET_LITTLE_NAME
7571 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
7573 #define elf32_bed elf32_sh_vxworks_bed
7575 #undef elf_backend_want_plt_sym
7576 #define elf_backend_want_plt_sym 1
7577 #undef elf_symbol_leading_char
7578 #define elf_symbol_leading_char '_'
7579 #define elf_backend_want_got_underscore 1
7580 #undef elf_backend_grok_prstatus
7581 #undef elf_backend_grok_psinfo
7582 #undef elf_backend_add_symbol_hook
7583 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
7584 #undef elf_backend_link_output_symbol_hook
7585 #define elf_backend_link_output_symbol_hook \
7586 elf_vxworks_link_output_symbol_hook
7587 #undef elf_backend_emit_relocs
7588 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
7589 #undef elf_backend_final_write_processing
7590 #define elf_backend_final_write_processing \
7591 elf_vxworks_final_write_processing
7592 #undef ELF_MAXPAGESIZE
7593 #define ELF_MAXPAGESIZE 0x1000
7594 #undef ELF_COMMONPAGESIZE
7596 #include "elf32-target.h"
7598 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */