1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by Ian Lance Taylor, Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
29 #include "elf-vxworks.h"
32 #include "libiberty.h"
33 #include "../opcodes/sh-opc.h"
35 static bfd_reloc_status_type sh_elf_reloc
36 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
37 static bfd_reloc_status_type sh_elf_ignore_reloc
38 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
39 static bfd_boolean sh_elf_relax_delete_bytes
40 (bfd *, asection *, bfd_vma, int);
41 static bfd_boolean sh_elf_align_loads
42 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
44 static bfd_boolean sh_elf_swap_insns
45 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
47 static int sh_elf_optimized_tls_reloc
48 (struct bfd_link_info *, int, int);
49 static bfd_vma dtpoff_base
50 (struct bfd_link_info *);
52 (struct bfd_link_info *, bfd_vma);
54 /* The name of the dynamic interpreter. This is put in the .interp
57 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
59 /* FDPIC binaries have a default 128K stack. */
60 #define DEFAULT_STACK_SIZE 0x20000
62 #define MINUS_ONE ((bfd_vma) 0 - 1)
64 /* Decide whether a reference to a symbol can be resolved locally or
65 not. If the symbol is protected, we want the local address, but
66 its function descriptor must be assigned by the dynamic linker. */
67 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
68 (SYMBOL_REFERENCES_LOCAL (INFO, H) \
69 || ! elf_hash_table (INFO)->dynamic_sections_created)
71 #define SH_PARTIAL32 TRUE
72 #define SH_SRC_MASK32 0xffffffff
73 #define SH_ELF_RELOC sh_elf_reloc
74 static reloc_howto_type sh_elf_howto_table[] =
76 #include "elf32-sh-relocs.h"
79 #define SH_PARTIAL32 FALSE
80 #define SH_SRC_MASK32 0
81 #define SH_ELF_RELOC bfd_elf_generic_reloc
82 static reloc_howto_type sh_vxworks_howto_table[] =
84 #include "elf32-sh-relocs.h"
87 /* Return true if OUTPUT_BFD is a VxWorks object. */
90 vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
92 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
93 extern const bfd_target bfd_elf32_shlvxworks_vec;
94 extern const bfd_target bfd_elf32_shvxworks_vec;
96 return (abfd->xvec == &bfd_elf32_shlvxworks_vec
97 || abfd->xvec == &bfd_elf32_shvxworks_vec);
103 /* Return true if OUTPUT_BFD is an FDPIC object. */
106 fdpic_object_p (bfd *abfd ATTRIBUTE_UNUSED)
108 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
109 extern const bfd_target bfd_elf32_shfd_vec;
110 extern const bfd_target bfd_elf32_shbfd_vec;
112 return (abfd->xvec == &bfd_elf32_shfd_vec
113 || abfd->xvec == &bfd_elf32_shbfd_vec);
119 /* Return the howto table for ABFD. */
121 static reloc_howto_type *
122 get_howto_table (bfd *abfd)
124 if (vxworks_object_p (abfd))
125 return sh_vxworks_howto_table;
126 return sh_elf_howto_table;
129 static bfd_reloc_status_type
130 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
131 asection *input_section, bfd_byte *contents,
132 bfd_vma addr, asection *symbol_section,
133 bfd_vma start, bfd_vma end)
135 static bfd_vma last_addr;
136 static asection *last_symbol_section;
137 bfd_byte *start_ptr, *ptr, *last_ptr;
142 /* Sanity check the address. */
143 if (addr > bfd_get_section_limit (input_bfd, input_section))
144 return bfd_reloc_outofrange;
146 /* We require the start and end relocations to be processed consecutively -
147 although we allow then to be processed forwards or backwards. */
151 last_symbol_section = symbol_section;
154 if (last_addr != addr)
158 if (! symbol_section || last_symbol_section != symbol_section || end < start)
159 return bfd_reloc_outofrange;
161 /* Get the symbol_section contents. */
162 if (symbol_section != input_section)
164 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
165 contents = elf_section_data (symbol_section)->this_hdr.contents;
168 if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
171 if (contents != NULL)
173 return bfd_reloc_outofrange;
177 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
178 start_ptr = contents + start;
179 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
181 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
184 diff = (last_ptr - ptr) >> 1;
185 cum_diff += diff & 1;
188 /* Calculate the start / end values to load into rs / re minus four -
189 so that will cancel out the four we would otherwise have to add to
190 addr to get the value to subtract in order to get relative addressing. */
194 end = (ptr + cum_diff * 2) - contents;
198 bfd_vma start0 = start - 4;
200 while (start0 && IS_PPI (contents + start0))
202 start0 = start - 2 - ((start - start0) & 2);
203 start = start0 - cum_diff - 2;
208 && elf_section_data (symbol_section)->this_hdr.contents != contents)
211 insn = bfd_get_16 (input_bfd, contents + addr);
213 x = (insn & 0x200 ? end : start) - addr;
214 if (input_section != symbol_section)
215 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
216 - (input_section->output_section->vma
217 + input_section->output_offset));
219 if (x < -128 || x > 127)
220 return bfd_reloc_overflow;
222 x = (insn & ~0xff) | (x & 0xff);
223 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
228 /* This function is used for normal relocs. This used to be like the COFF
229 function, and is almost certainly incorrect for other ELF targets. */
231 static bfd_reloc_status_type
232 sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
233 void *data, asection *input_section, bfd *output_bfd,
234 char **error_message ATTRIBUTE_UNUSED)
238 enum elf_sh_reloc_type r_type;
239 bfd_vma addr = reloc_entry->address;
240 bfd_byte *hit_data = addr + (bfd_byte *) data;
242 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
244 if (output_bfd != NULL)
246 /* Partial linking--do nothing. */
247 reloc_entry->address += input_section->output_offset;
251 /* Almost all relocs have to do with relaxing. If any work must be
252 done for them, it has been done in sh_relax_section. */
253 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
256 if (symbol_in != NULL
257 && bfd_is_und_section (symbol_in->section))
258 return bfd_reloc_undefined;
260 if (bfd_is_com_section (symbol_in->section))
263 sym_value = (symbol_in->value +
264 symbol_in->section->output_section->vma +
265 symbol_in->section->output_offset);
270 insn = bfd_get_32 (abfd, hit_data);
271 insn += sym_value + reloc_entry->addend;
272 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
275 insn = bfd_get_16 (abfd, hit_data);
276 sym_value += reloc_entry->addend;
277 sym_value -= (input_section->output_section->vma
278 + input_section->output_offset
281 sym_value += (insn & 0xfff) << 1;
284 insn = (insn & 0xf000) | (sym_value & 0xfff);
285 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
286 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
287 return bfd_reloc_overflow;
297 /* This function is used for relocs which are only used for relaxing,
298 which the linker should otherwise ignore. */
300 static bfd_reloc_status_type
301 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
302 asymbol *symbol ATTRIBUTE_UNUSED,
303 void *data ATTRIBUTE_UNUSED, asection *input_section,
305 char **error_message ATTRIBUTE_UNUSED)
307 if (output_bfd != NULL)
308 reloc_entry->address += input_section->output_offset;
312 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
316 bfd_reloc_code_real_type bfd_reloc_val;
317 unsigned char elf_reloc_val;
320 /* An array mapping BFD reloc codes to SH ELF relocs. */
322 static const struct elf_reloc_map sh_reloc_map[] =
324 { BFD_RELOC_NONE, R_SH_NONE },
325 { BFD_RELOC_32, R_SH_DIR32 },
326 { BFD_RELOC_16, R_SH_DIR16 },
327 { BFD_RELOC_8, R_SH_DIR8 },
328 { BFD_RELOC_CTOR, R_SH_DIR32 },
329 { BFD_RELOC_32_PCREL, R_SH_REL32 },
330 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
331 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
332 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
333 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
334 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
335 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
336 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
337 { BFD_RELOC_SH_USES, R_SH_USES },
338 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
339 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
340 { BFD_RELOC_SH_CODE, R_SH_CODE },
341 { BFD_RELOC_SH_DATA, R_SH_DATA },
342 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
343 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
344 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
345 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
346 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
347 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
348 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
349 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
350 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
351 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
352 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
353 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
354 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
355 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
356 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
357 { BFD_RELOC_SH_COPY, R_SH_COPY },
358 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
359 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
360 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
361 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
362 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
363 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
364 { BFD_RELOC_SH_GOT20, R_SH_GOT20 },
365 { BFD_RELOC_SH_GOTOFF20, R_SH_GOTOFF20 },
366 { BFD_RELOC_SH_GOTFUNCDESC, R_SH_GOTFUNCDESC },
367 { BFD_RELOC_SH_GOTFUNCDESC20, R_SH_GOTFUNCDESC20 },
368 { BFD_RELOC_SH_GOTOFFFUNCDESC, R_SH_GOTOFFFUNCDESC },
369 { BFD_RELOC_SH_GOTOFFFUNCDESC20, R_SH_GOTOFFFUNCDESC20 },
370 { BFD_RELOC_SH_FUNCDESC, R_SH_FUNCDESC },
371 #ifdef INCLUDE_SHMEDIA
372 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
373 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
374 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
375 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
376 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
377 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
378 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
379 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
380 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
381 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
382 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
383 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
384 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
385 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
386 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
387 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
388 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
389 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
390 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
391 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
392 { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
393 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
394 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
395 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
396 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
397 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
398 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
399 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
400 { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
401 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
402 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
403 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
404 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
405 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
406 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
407 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
408 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
409 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
410 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
411 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
412 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
413 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
414 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
415 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
416 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
417 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
418 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
419 { BFD_RELOC_64, R_SH_64 },
420 { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
421 #endif /* not INCLUDE_SHMEDIA */
424 /* Given a BFD reloc code, return the howto structure for the
425 corresponding SH ELF reloc. */
427 static reloc_howto_type *
428 sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
432 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
434 if (sh_reloc_map[i].bfd_reloc_val == code)
435 return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
441 static reloc_howto_type *
442 sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
446 if (vxworks_object_p (abfd))
449 i < (sizeof (sh_vxworks_howto_table)
450 / sizeof (sh_vxworks_howto_table[0]));
452 if (sh_vxworks_howto_table[i].name != NULL
453 && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
454 return &sh_vxworks_howto_table[i];
459 i < (sizeof (sh_elf_howto_table)
460 / sizeof (sh_elf_howto_table[0]));
462 if (sh_elf_howto_table[i].name != NULL
463 && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
464 return &sh_elf_howto_table[i];
470 /* Given an ELF reloc, fill in the howto field of a relent. */
473 sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
477 r = ELF32_R_TYPE (dst->r_info);
479 BFD_ASSERT (r < (unsigned int) R_SH_max);
480 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
481 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
482 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_LAST_INVALID_RELOC_3);
483 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4);
484 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5);
485 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_6 || r > R_SH_LAST_INVALID_RELOC_6);
487 cache_ptr->howto = get_howto_table (abfd) + r;
490 /* This function handles relaxing for SH ELF. See the corresponding
491 function in coff-sh.c for a description of what this does. FIXME:
492 There is a lot of duplication here between this code and the COFF
493 specific code. The format of relocs and symbols is wound deeply
494 into this code, but it would still be better if the duplication
495 could be eliminated somehow. Note in particular that although both
496 functions use symbols like R_SH_CODE, those symbols have different
497 values; in coff-sh.c they come from include/coff/sh.h, whereas here
498 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
501 sh_elf_relax_section (bfd *abfd, asection *sec,
502 struct bfd_link_info *link_info, bfd_boolean *again)
504 Elf_Internal_Shdr *symtab_hdr;
505 Elf_Internal_Rela *internal_relocs;
506 bfd_boolean have_code;
507 Elf_Internal_Rela *irel, *irelend;
508 bfd_byte *contents = NULL;
509 Elf_Internal_Sym *isymbuf = NULL;
513 if (link_info->relocatable
514 || (sec->flags & SEC_RELOC) == 0
515 || sec->reloc_count == 0)
518 #ifdef INCLUDE_SHMEDIA
519 if (elf_section_data (sec)->this_hdr.sh_flags
520 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
526 symtab_hdr = &elf_symtab_hdr (abfd);
528 internal_relocs = (_bfd_elf_link_read_relocs
529 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
530 link_info->keep_memory));
531 if (internal_relocs == NULL)
536 irelend = internal_relocs + sec->reloc_count;
537 for (irel = internal_relocs; irel < irelend; irel++)
539 bfd_vma laddr, paddr, symval;
541 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
544 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
547 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
550 /* Get the section contents. */
551 if (contents == NULL)
553 if (elf_section_data (sec)->this_hdr.contents != NULL)
554 contents = elf_section_data (sec)->this_hdr.contents;
557 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
562 /* The r_addend field of the R_SH_USES reloc will point us to
563 the register load. The 4 is because the r_addend field is
564 computed as though it were a jump offset, which are based
565 from 4 bytes after the jump instruction. */
566 laddr = irel->r_offset + 4 + irel->r_addend;
567 if (laddr >= sec->size)
569 (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
571 (unsigned long) irel->r_offset);
574 insn = bfd_get_16 (abfd, contents + laddr);
576 /* If the instruction is not mov.l NN,rN, we don't know what to
578 if ((insn & 0xf000) != 0xd000)
580 ((*_bfd_error_handler)
581 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
582 abfd, (unsigned long) irel->r_offset, insn));
586 /* Get the address from which the register is being loaded. The
587 displacement in the mov.l instruction is quadrupled. It is a
588 displacement from four bytes after the movl instruction, but,
589 before adding in the PC address, two least significant bits
590 of the PC are cleared. We assume that the section is aligned
591 on a four byte boundary. */
594 paddr += (laddr + 4) &~ (bfd_vma) 3;
595 if (paddr >= sec->size)
597 ((*_bfd_error_handler)
598 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
599 abfd, (unsigned long) irel->r_offset));
603 /* Get the reloc for the address from which the register is
604 being loaded. This reloc will tell us which function is
605 actually being called. */
606 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
607 if (irelfn->r_offset == paddr
608 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
610 if (irelfn >= irelend)
612 ((*_bfd_error_handler)
613 (_("%B: 0x%lx: warning: could not find expected reloc"),
614 abfd, (unsigned long) paddr));
618 /* Read this BFD's symbols if we haven't done so already. */
619 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
621 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
623 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
624 symtab_hdr->sh_info, 0,
630 /* Get the value of the symbol referred to by the reloc. */
631 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
633 /* A local symbol. */
634 Elf_Internal_Sym *isym;
636 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
638 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
640 ((*_bfd_error_handler)
641 (_("%B: 0x%lx: warning: symbol in unexpected section"),
642 abfd, (unsigned long) paddr));
646 symval = (isym->st_value
647 + sec->output_section->vma
648 + sec->output_offset);
653 struct elf_link_hash_entry *h;
655 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
656 h = elf_sym_hashes (abfd)[indx];
657 BFD_ASSERT (h != NULL);
658 if (h->root.type != bfd_link_hash_defined
659 && h->root.type != bfd_link_hash_defweak)
661 /* This appears to be a reference to an undefined
662 symbol. Just ignore it--it will be caught by the
663 regular reloc processing. */
667 symval = (h->root.u.def.value
668 + h->root.u.def.section->output_section->vma
669 + h->root.u.def.section->output_offset);
672 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
673 symval += bfd_get_32 (abfd, contents + paddr);
675 symval += irelfn->r_addend;
677 /* See if this function call can be shortened. */
680 + sec->output_section->vma
683 /* A branch to an address beyond ours might be increased by an
684 .align that doesn't move when bytes behind us are deleted.
685 So, we add some slop in this calculation to allow for
687 if (foff < -0x1000 || foff >= 0x1000 - 8)
689 /* After all that work, we can't shorten this function call. */
693 /* Shorten the function call. */
695 /* For simplicity of coding, we are going to modify the section
696 contents, the section relocs, and the BFD symbol table. We
697 must tell the rest of the code not to free up this
698 information. It would be possible to instead create a table
699 of changes which have to be made, as is done in coff-mips.c;
700 that would be more work, but would require less memory when
701 the linker is run. */
703 elf_section_data (sec)->relocs = internal_relocs;
704 elf_section_data (sec)->this_hdr.contents = contents;
705 symtab_hdr->contents = (unsigned char *) isymbuf;
707 /* Replace the jsr with a bsr. */
709 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
710 replace the jsr with a bsr. */
711 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
712 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
713 here, but that only checks if the symbol is an external symbol,
714 not if the symbol is in a different section. Besides, we need
715 a consistent meaning for the relocation, so we just assume here that
716 the value of the symbol is not available. */
718 /* We can't fully resolve this yet, because the external
719 symbol value may be changed by future relaxing. We let
720 the final link phase handle it. */
721 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
725 /* When we calculated the symbol "value" we had an offset in the
726 DIR32's word in memory (we read and add it above). However,
727 the jsr we create does NOT have this offset encoded, so we
728 have to add it to the addend to preserve it. */
729 irel->r_addend += bfd_get_32 (abfd, contents + paddr);
731 /* See if there is another R_SH_USES reloc referring to the same
733 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
734 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
735 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
737 if (irelscan < irelend)
739 /* Some other function call depends upon this register load,
740 and we have not yet converted that function call.
741 Indeed, we may never be able to convert it. There is
742 nothing else we can do at this point. */
746 /* Look for a R_SH_COUNT reloc on the location where the
747 function address is stored. Do this before deleting any
748 bytes, to avoid confusion about the address. */
749 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
750 if (irelcount->r_offset == paddr
751 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
754 /* Delete the register load. */
755 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
758 /* That will change things, so, just in case it permits some
759 other function call to come within range, we should relax
760 again. Note that this is not required, and it may be slow. */
763 /* Now check whether we got a COUNT reloc. */
764 if (irelcount >= irelend)
766 ((*_bfd_error_handler)
767 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
768 abfd, (unsigned long) paddr));
772 /* The number of uses is stored in the r_addend field. We've
774 if (irelcount->r_addend == 0)
776 ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"),
778 (unsigned long) paddr));
782 --irelcount->r_addend;
784 /* If there are no more uses, we can delete the address. Reload
785 the address from irelfn, in case it was changed by the
786 previous call to sh_elf_relax_delete_bytes. */
787 if (irelcount->r_addend == 0)
789 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
793 /* We've done all we can with that function call. */
796 /* Look for load and store instructions that we can align on four
798 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
803 /* Get the section contents. */
804 if (contents == NULL)
806 if (elf_section_data (sec)->this_hdr.contents != NULL)
807 contents = elf_section_data (sec)->this_hdr.contents;
810 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
815 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
821 elf_section_data (sec)->relocs = internal_relocs;
822 elf_section_data (sec)->this_hdr.contents = contents;
823 symtab_hdr->contents = (unsigned char *) isymbuf;
828 && symtab_hdr->contents != (unsigned char *) isymbuf)
830 if (! link_info->keep_memory)
834 /* Cache the symbols for elf_link_input_bfd. */
835 symtab_hdr->contents = (unsigned char *) isymbuf;
840 && elf_section_data (sec)->this_hdr.contents != contents)
842 if (! link_info->keep_memory)
846 /* Cache the section contents for elf_link_input_bfd. */
847 elf_section_data (sec)->this_hdr.contents = contents;
851 if (internal_relocs != NULL
852 && elf_section_data (sec)->relocs != internal_relocs)
853 free (internal_relocs);
859 && symtab_hdr->contents != (unsigned char *) isymbuf)
862 && elf_section_data (sec)->this_hdr.contents != contents)
864 if (internal_relocs != NULL
865 && elf_section_data (sec)->relocs != internal_relocs)
866 free (internal_relocs);
871 /* Delete some bytes from a section while relaxing. FIXME: There is a
872 lot of duplication between this function and sh_relax_delete_bytes
876 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
879 Elf_Internal_Shdr *symtab_hdr;
880 unsigned int sec_shndx;
882 Elf_Internal_Rela *irel, *irelend;
883 Elf_Internal_Rela *irelalign;
885 Elf_Internal_Sym *isymbuf, *isym, *isymend;
886 struct elf_link_hash_entry **sym_hashes;
887 struct elf_link_hash_entry **end_hashes;
888 unsigned int symcount;
891 symtab_hdr = &elf_symtab_hdr (abfd);
892 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
894 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
896 contents = elf_section_data (sec)->this_hdr.contents;
898 /* The deletion must stop at the next ALIGN reloc for an aligment
899 power larger than the number of bytes we are deleting. */
904 irel = elf_section_data (sec)->relocs;
905 irelend = irel + sec->reloc_count;
906 for (; irel < irelend; irel++)
908 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
909 && irel->r_offset > addr
910 && count < (1 << irel->r_addend))
913 toaddr = irel->r_offset;
918 /* Actually delete the bytes. */
919 memmove (contents + addr, contents + addr + count,
920 (size_t) (toaddr - addr - count));
921 if (irelalign == NULL)
927 #define NOP_OPCODE (0x0009)
929 BFD_ASSERT ((count & 1) == 0);
930 for (i = 0; i < count; i += 2)
931 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
934 /* Adjust all the relocs. */
935 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
937 bfd_vma nraddr, stop;
940 int off, adjust, oinsn;
941 bfd_signed_vma voff = 0;
942 bfd_boolean overflow;
944 /* Get the new reloc address. */
945 nraddr = irel->r_offset;
946 if ((irel->r_offset > addr
947 && irel->r_offset < toaddr)
948 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
949 && irel->r_offset == toaddr))
952 /* See if this reloc was for the bytes we have deleted, in which
953 case we no longer care about it. Don't delete relocs which
954 represent addresses, though. */
955 if (irel->r_offset >= addr
956 && irel->r_offset < addr + count
957 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
958 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
959 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
960 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
961 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
964 /* If this is a PC relative reloc, see if the range it covers
965 includes the bytes we have deleted. */
966 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
975 start = irel->r_offset;
976 insn = bfd_get_16 (abfd, contents + nraddr);
980 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
987 /* If this reloc is against a symbol defined in this
988 section, and the symbol will not be adjusted below, we
989 must check the addend to see it will put the value in
990 range to be adjusted, and hence must be changed. */
991 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
993 isym = isymbuf + ELF32_R_SYM (irel->r_info);
994 if (isym->st_shndx == sec_shndx
995 && (isym->st_value <= addr
996 || isym->st_value >= toaddr))
1000 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
1002 val = bfd_get_32 (abfd, contents + nraddr);
1003 val += isym->st_value;
1004 if (val > addr && val < toaddr)
1005 bfd_put_32 (abfd, val - count, contents + nraddr);
1009 val = isym->st_value + irel->r_addend;
1010 if (val > addr && val < toaddr)
1011 irel->r_addend -= count;
1015 start = stop = addr;
1022 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1029 /* This has been made by previous relaxation. Since the
1030 relocation will be against an external symbol, the
1031 final relocation will just do the right thing. */
1032 start = stop = addr;
1038 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1040 /* The addend will be against the section symbol, thus
1041 for adjusting the addend, the relevant start is the
1042 start of the section.
1043 N.B. If we want to abandon in-place changes here and
1044 test directly using symbol + addend, we have to take into
1045 account that the addend has already been adjusted by -4. */
1046 if (stop > addr && stop < toaddr)
1047 irel->r_addend -= count;
1053 stop = start + 4 + off * 2;
1058 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1064 /* These relocs types represent
1066 The r_addend field holds the difference between the reloc
1067 address and L1. That is the start of the reloc, and
1068 adding in the contents gives us the top. We must adjust
1069 both the r_offset field and the section contents.
1070 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1071 and the elf bfd r_offset is called r_vaddr. */
1073 stop = irel->r_offset;
1074 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1078 && (stop <= addr || stop >= toaddr))
1079 irel->r_addend += count;
1080 else if (stop > addr
1082 && (start <= addr || start >= toaddr))
1083 irel->r_addend -= count;
1085 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1086 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1087 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1088 voff = bfd_get_8 (abfd, contents + nraddr);
1090 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1091 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1096 start = irel->r_offset;
1097 stop = (bfd_vma) ((bfd_signed_vma) start
1098 + (long) irel->r_addend
1105 && (stop <= addr || stop >= toaddr))
1107 else if (stop > addr
1109 && (start <= addr || start >= toaddr))
1118 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1127 if ((oinsn & 0xff00) != (insn & 0xff00))
1129 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1134 if ((oinsn & 0xf000) != (insn & 0xf000))
1136 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1140 BFD_ASSERT (adjust == count || count >= 4);
1145 if ((irel->r_offset & 3) == 0)
1148 if ((oinsn & 0xff00) != (insn & 0xff00))
1150 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1155 if (voff < 0 || voff >= 0xff)
1157 bfd_put_8 (abfd, voff, contents + nraddr);
1162 if (voff < - 0x8000 || voff >= 0x8000)
1164 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1169 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1173 irel->r_addend += adjust;
1179 ((*_bfd_error_handler)
1180 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1181 abfd, (unsigned long) irel->r_offset));
1182 bfd_set_error (bfd_error_bad_value);
1187 irel->r_offset = nraddr;
1190 /* Look through all the other sections. If there contain any IMM32
1191 relocs against internal symbols which we are not going to adjust
1192 below, we may need to adjust the addends. */
1193 for (o = abfd->sections; o != NULL; o = o->next)
1195 Elf_Internal_Rela *internal_relocs;
1196 Elf_Internal_Rela *irelscan, *irelscanend;
1197 bfd_byte *ocontents;
1200 || (o->flags & SEC_RELOC) == 0
1201 || o->reloc_count == 0)
1204 /* We always cache the relocs. Perhaps, if info->keep_memory is
1205 FALSE, we should free them, if we are permitted to, when we
1206 leave sh_coff_relax_section. */
1207 internal_relocs = (_bfd_elf_link_read_relocs
1208 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
1209 if (internal_relocs == NULL)
1213 irelscanend = internal_relocs + o->reloc_count;
1214 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1216 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1217 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1219 bfd_vma start, stop;
1220 bfd_signed_vma voff;
1222 if (ocontents == NULL)
1224 if (elf_section_data (o)->this_hdr.contents != NULL)
1225 ocontents = elf_section_data (o)->this_hdr.contents;
1228 /* We always cache the section contents.
1229 Perhaps, if info->keep_memory is FALSE, we
1230 should free them, if we are permitted to,
1231 when we leave sh_coff_relax_section. */
1232 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1234 if (ocontents != NULL)
1239 elf_section_data (o)->this_hdr.contents = ocontents;
1243 stop = irelscan->r_offset;
1245 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1247 /* STOP is in a different section, so it won't change. */
1248 if (start > addr && start < toaddr)
1249 irelscan->r_addend += count;
1251 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1252 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1256 && (stop <= addr || stop >= toaddr))
1257 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1258 ocontents + irelscan->r_offset);
1259 else if (stop > addr
1261 && (start <= addr || start >= toaddr))
1262 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1263 ocontents + irelscan->r_offset);
1266 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1269 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1273 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1274 if (isym->st_shndx == sec_shndx
1275 && (isym->st_value <= addr
1276 || isym->st_value >= toaddr))
1280 if (ocontents == NULL)
1282 if (elf_section_data (o)->this_hdr.contents != NULL)
1283 ocontents = elf_section_data (o)->this_hdr.contents;
1286 /* We always cache the section contents.
1287 Perhaps, if info->keep_memory is FALSE, we
1288 should free them, if we are permitted to,
1289 when we leave sh_coff_relax_section. */
1290 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1292 if (ocontents != NULL)
1297 elf_section_data (o)->this_hdr.contents = ocontents;
1301 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1302 val += isym->st_value;
1303 if (val > addr && val < toaddr)
1304 bfd_put_32 (abfd, val - count,
1305 ocontents + irelscan->r_offset);
1310 /* Adjust the local symbols defined in this section. */
1311 isymend = isymbuf + symtab_hdr->sh_info;
1312 for (isym = isymbuf; isym < isymend; isym++)
1314 if (isym->st_shndx == sec_shndx
1315 && isym->st_value > addr
1316 && isym->st_value < toaddr)
1317 isym->st_value -= count;
1320 /* Now adjust the global symbols defined in this section. */
1321 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1322 - symtab_hdr->sh_info);
1323 sym_hashes = elf_sym_hashes (abfd);
1324 end_hashes = sym_hashes + symcount;
1325 for (; sym_hashes < end_hashes; sym_hashes++)
1327 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1328 if ((sym_hash->root.type == bfd_link_hash_defined
1329 || sym_hash->root.type == bfd_link_hash_defweak)
1330 && sym_hash->root.u.def.section == sec
1331 && sym_hash->root.u.def.value > addr
1332 && sym_hash->root.u.def.value < toaddr)
1334 sym_hash->root.u.def.value -= count;
1338 /* See if we can move the ALIGN reloc forward. We have adjusted
1339 r_offset for it already. */
1340 if (irelalign != NULL)
1342 bfd_vma alignto, alignaddr;
1344 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1345 alignaddr = BFD_ALIGN (irelalign->r_offset,
1346 1 << irelalign->r_addend);
1347 if (alignto != alignaddr)
1349 /* Tail recursion. */
1350 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1351 (int) (alignto - alignaddr));
1358 /* Look for loads and stores which we can align to four byte
1359 boundaries. This is like sh_align_loads in coff-sh.c. */
1362 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1363 Elf_Internal_Rela *internal_relocs,
1364 bfd_byte *contents ATTRIBUTE_UNUSED,
1365 bfd_boolean *pswapped)
1367 Elf_Internal_Rela *irel, *irelend;
1368 bfd_vma *labels = NULL;
1369 bfd_vma *label, *label_end;
1374 irelend = internal_relocs + sec->reloc_count;
1376 /* Get all the addresses with labels on them. */
1377 amt = sec->reloc_count;
1378 amt *= sizeof (bfd_vma);
1379 labels = (bfd_vma *) bfd_malloc (amt);
1383 for (irel = internal_relocs; irel < irelend; irel++)
1385 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1387 *label_end = irel->r_offset;
1392 /* Note that the assembler currently always outputs relocs in
1393 address order. If that ever changes, this code will need to sort
1394 the label values and the relocs. */
1398 for (irel = internal_relocs; irel < irelend; irel++)
1400 bfd_vma start, stop;
1402 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1405 start = irel->r_offset;
1407 for (irel++; irel < irelend; irel++)
1408 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1411 stop = irel->r_offset;
1415 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1416 internal_relocs, &label,
1417 label_end, start, stop, pswapped))
1432 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1435 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1436 bfd_byte *contents, bfd_vma addr)
1438 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1439 unsigned short i1, i2;
1440 Elf_Internal_Rela *irel, *irelend;
1442 /* Swap the instructions themselves. */
1443 i1 = bfd_get_16 (abfd, contents + addr);
1444 i2 = bfd_get_16 (abfd, contents + addr + 2);
1445 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1446 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1448 /* Adjust all reloc addresses. */
1449 irelend = internal_relocs + sec->reloc_count;
1450 for (irel = internal_relocs; irel < irelend; irel++)
1452 enum elf_sh_reloc_type type;
1455 /* There are a few special types of relocs that we don't want to
1456 adjust. These relocs do not apply to the instruction itself,
1457 but are only associated with the address. */
1458 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1459 if (type == R_SH_ALIGN
1460 || type == R_SH_CODE
1461 || type == R_SH_DATA
1462 || type == R_SH_LABEL)
1465 /* If an R_SH_USES reloc points to one of the addresses being
1466 swapped, we must adjust it. It would be incorrect to do this
1467 for a jump, though, since we want to execute both
1468 instructions after the jump. (We have avoided swapping
1469 around a label, so the jump will not wind up executing an
1470 instruction it shouldn't). */
1471 if (type == R_SH_USES)
1475 off = irel->r_offset + 4 + irel->r_addend;
1477 irel->r_offset += 2;
1478 else if (off == addr + 2)
1479 irel->r_offset -= 2;
1482 if (irel->r_offset == addr)
1484 irel->r_offset += 2;
1487 else if (irel->r_offset == addr + 2)
1489 irel->r_offset -= 2;
1498 unsigned short insn, oinsn;
1499 bfd_boolean overflow;
1501 loc = contents + irel->r_offset;
1510 insn = bfd_get_16 (abfd, loc);
1513 if ((oinsn & 0xff00) != (insn & 0xff00))
1515 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1519 insn = bfd_get_16 (abfd, loc);
1522 if ((oinsn & 0xf000) != (insn & 0xf000))
1524 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1528 /* This reloc ignores the least significant 3 bits of
1529 the program counter before adding in the offset.
1530 This means that if ADDR is at an even address, the
1531 swap will not affect the offset. If ADDR is an at an
1532 odd address, then the instruction will be crossing a
1533 four byte boundary, and must be adjusted. */
1534 if ((addr & 3) != 0)
1536 insn = bfd_get_16 (abfd, loc);
1539 if ((oinsn & 0xff00) != (insn & 0xff00))
1541 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1549 ((*_bfd_error_handler)
1550 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1551 abfd, (unsigned long) irel->r_offset));
1552 bfd_set_error (bfd_error_bad_value);
1560 #endif /* defined SH64_ELF */
1562 /* Describes one of the various PLT styles. */
1564 struct elf_sh_plt_info
1566 /* The template for the first PLT entry, or NULL if there is no special
1568 const bfd_byte *plt0_entry;
1570 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1571 bfd_vma plt0_entry_size;
1573 /* Index I is the offset into PLT0_ENTRY of a pointer to
1574 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1575 if there is no such pointer. */
1576 bfd_vma plt0_got_fields[3];
1578 /* The template for a symbol's PLT entry. */
1579 const bfd_byte *symbol_entry;
1581 /* The size of SYMBOL_ENTRY in bytes. */
1582 bfd_vma symbol_entry_size;
1584 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1585 on all targets. The comments by each member indicate the value
1586 that the field must hold. */
1588 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1589 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1590 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1591 bfd_boolean got20; /* TRUE if got_entry points to a movi20
1592 instruction (instead of a constant pool
1596 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1597 bfd_vma symbol_resolve_offset;
1599 /* A different PLT layout which can be used for the first
1600 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1602 const struct elf_sh_plt_info *short_plt;
1605 #ifdef INCLUDE_SHMEDIA
1607 /* The size in bytes of an entry in the procedure linkage table. */
1609 #define ELF_PLT_ENTRY_SIZE 64
1611 /* First entry in an absolute procedure linkage table look like this. */
1613 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1615 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1616 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1617 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1618 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1619 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1620 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1621 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1622 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1623 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1624 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1625 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1626 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1627 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1628 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1629 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1630 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1633 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1635 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1636 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1637 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1638 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1639 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1640 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1641 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1642 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1643 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1644 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1645 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1646 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1647 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1648 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1649 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1650 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1653 /* Sebsequent entries in an absolute procedure linkage table look like
1656 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1658 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1659 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1660 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1661 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1662 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1663 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1664 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1665 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1666 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1667 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1668 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1669 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1670 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1671 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1672 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1673 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1676 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1678 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1679 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1680 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1681 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1682 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1683 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1684 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1685 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1686 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1687 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1688 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1689 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1690 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1691 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1692 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1693 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1696 /* Entries in a PIC procedure linkage table look like this. */
1698 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1700 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1701 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1702 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1703 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1704 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1705 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1706 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1707 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1708 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1709 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1710 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1711 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1712 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1713 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1714 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1715 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1718 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1720 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1721 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1722 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1723 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1724 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1725 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1726 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1727 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1728 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1729 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1730 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1731 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1732 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1733 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1734 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1735 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1738 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1741 /* Big-endian non-PIC. */
1742 elf_sh_plt0_entry_be,
1744 { 0, MINUS_ONE, MINUS_ONE },
1745 elf_sh_plt_entry_be,
1747 { 0, 32, 48, FALSE },
1748 33, /* includes ISA encoding */
1752 /* Little-endian non-PIC. */
1753 elf_sh_plt0_entry_le,
1755 { 0, MINUS_ONE, MINUS_ONE },
1756 elf_sh_plt_entry_le,
1758 { 0, 32, 48, FALSE },
1759 33, /* includes ISA encoding */
1765 /* Big-endian PIC. */
1766 elf_sh_plt0_entry_be,
1768 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1769 elf_sh_pic_plt_entry_be,
1771 { 0, MINUS_ONE, 52, FALSE },
1772 33, /* includes ISA encoding */
1776 /* Little-endian PIC. */
1777 elf_sh_plt0_entry_le,
1779 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1780 elf_sh_pic_plt_entry_le,
1782 { 0, MINUS_ONE, 52, FALSE },
1783 33, /* includes ISA encoding */
1789 /* Return offset of the linker in PLT0 entry. */
1790 #define elf_sh_plt0_gotplt_offset(info) 0
1792 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1793 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1796 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1799 install_plt_field (bfd *output_bfd, bfd_boolean code_p,
1800 unsigned long value, bfd_byte *addr)
1803 bfd_put_32 (output_bfd,
1804 bfd_get_32 (output_bfd, addr)
1805 | ((value >> 6) & 0x3fffc00),
1807 bfd_put_32 (output_bfd,
1808 bfd_get_32 (output_bfd, addr + 4)
1809 | ((value << 10) & 0x3fffc00),
1813 /* Return the type of PLT associated with ABFD. PIC_P is true if
1814 the object is position-independent. */
1816 static const struct elf_sh_plt_info *
1817 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
1819 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
1822 /* The size in bytes of an entry in the procedure linkage table. */
1824 #define ELF_PLT_ENTRY_SIZE 28
1826 /* First entry in an absolute procedure linkage table look like this. */
1828 /* Note - this code has been "optimised" not to use r2. r2 is used by
1829 GCC to return the address of large structures, so it should not be
1830 corrupted here. This does mean however, that this PLT does not conform
1831 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1832 and r2 contains the GOT id. This version stores the GOT id in r0 and
1833 ignores the type. Loaders can easily detect this difference however,
1834 since the type will always be 0 or 8, and the GOT ids will always be
1835 greater than or equal to 12. */
1836 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1838 0xd0, 0x05, /* mov.l 2f,r0 */
1839 0x60, 0x02, /* mov.l @r0,r0 */
1840 0x2f, 0x06, /* mov.l r0,@-r15 */
1841 0xd0, 0x03, /* mov.l 1f,r0 */
1842 0x60, 0x02, /* mov.l @r0,r0 */
1843 0x40, 0x2b, /* jmp @r0 */
1844 0x60, 0xf6, /* mov.l @r15+,r0 */
1845 0x00, 0x09, /* nop */
1846 0x00, 0x09, /* nop */
1847 0x00, 0x09, /* nop */
1848 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1849 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1852 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1854 0x05, 0xd0, /* mov.l 2f,r0 */
1855 0x02, 0x60, /* mov.l @r0,r0 */
1856 0x06, 0x2f, /* mov.l r0,@-r15 */
1857 0x03, 0xd0, /* mov.l 1f,r0 */
1858 0x02, 0x60, /* mov.l @r0,r0 */
1859 0x2b, 0x40, /* jmp @r0 */
1860 0xf6, 0x60, /* mov.l @r15+,r0 */
1861 0x09, 0x00, /* nop */
1862 0x09, 0x00, /* nop */
1863 0x09, 0x00, /* nop */
1864 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1865 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1868 /* Sebsequent entries in an absolute procedure linkage table look like
1871 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1873 0xd0, 0x04, /* mov.l 1f,r0 */
1874 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1875 0xd1, 0x02, /* mov.l 0f,r1 */
1876 0x40, 0x2b, /* jmp @r0 */
1877 0x60, 0x13, /* mov r1,r0 */
1878 0xd1, 0x03, /* mov.l 2f,r1 */
1879 0x40, 0x2b, /* jmp @r0 */
1880 0x00, 0x09, /* nop */
1881 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1882 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1883 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1886 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1888 0x04, 0xd0, /* mov.l 1f,r0 */
1889 0x02, 0x60, /* mov.l @r0,r0 */
1890 0x02, 0xd1, /* mov.l 0f,r1 */
1891 0x2b, 0x40, /* jmp @r0 */
1892 0x13, 0x60, /* mov r1,r0 */
1893 0x03, 0xd1, /* mov.l 2f,r1 */
1894 0x2b, 0x40, /* jmp @r0 */
1895 0x09, 0x00, /* nop */
1896 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1897 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1898 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1901 /* Entries in a PIC procedure linkage table look like this. */
1903 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1905 0xd0, 0x04, /* mov.l 1f,r0 */
1906 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1907 0x40, 0x2b, /* jmp @r0 */
1908 0x00, 0x09, /* nop */
1909 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1910 0xd1, 0x03, /* mov.l 2f,r1 */
1911 0x40, 0x2b, /* jmp @r0 */
1912 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1913 0x00, 0x09, /* nop */
1914 0x00, 0x09, /* nop */
1915 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1916 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1919 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1921 0x04, 0xd0, /* mov.l 1f,r0 */
1922 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1923 0x2b, 0x40, /* jmp @r0 */
1924 0x09, 0x00, /* nop */
1925 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1926 0x03, 0xd1, /* mov.l 2f,r1 */
1927 0x2b, 0x40, /* jmp @r0 */
1928 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1929 0x09, 0x00, /* nop */
1930 0x09, 0x00, /* nop */
1931 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1932 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1935 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1938 /* Big-endian non-PIC. */
1939 elf_sh_plt0_entry_be,
1941 { MINUS_ONE, 24, 20 },
1942 elf_sh_plt_entry_be,
1944 { 20, 16, 24, FALSE },
1949 /* Little-endian non-PIC. */
1950 elf_sh_plt0_entry_le,
1952 { MINUS_ONE, 24, 20 },
1953 elf_sh_plt_entry_le,
1955 { 20, 16, 24, FALSE },
1962 /* Big-endian PIC. */
1963 elf_sh_plt0_entry_be,
1965 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1966 elf_sh_pic_plt_entry_be,
1968 { 20, MINUS_ONE, 24, FALSE },
1973 /* Little-endian PIC. */
1974 elf_sh_plt0_entry_le,
1976 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1977 elf_sh_pic_plt_entry_le,
1979 { 20, MINUS_ONE, 24, FALSE },
1986 #define VXWORKS_PLT_HEADER_SIZE 12
1987 #define VXWORKS_PLT_ENTRY_SIZE 24
1989 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
1991 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1992 0x61, 0x12, /* mov.l @r1,r1 */
1993 0x41, 0x2b, /* jmp @r1 */
1994 0x00, 0x09, /* nop */
1995 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1998 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
2000 0x01, 0xd1, /* mov.l @(8,pc),r1 */
2001 0x12, 0x61, /* mov.l @r1,r1 */
2002 0x2b, 0x41, /* jmp @r1 */
2003 0x09, 0x00, /* nop */
2004 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2007 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2009 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2010 0x60, 0x02, /* mov.l @r0,r0 */
2011 0x40, 0x2b, /* jmp @r0 */
2012 0x00, 0x09, /* nop */
2013 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2014 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2015 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
2016 0x00, 0x09, /* nop */
2017 0x00, 0x09, /* nop */
2018 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2021 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2023 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2024 0x02, 0x60, /* mov.l @r0,r0 */
2025 0x2b, 0x40, /* jmp @r0 */
2026 0x09, 0x00, /* nop */
2027 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2028 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2029 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
2030 0x09, 0x00, /* nop */
2031 0x09, 0x00, /* nop */
2032 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2035 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2037 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2038 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2039 0x40, 0x2b, /* jmp @r0 */
2040 0x00, 0x09, /* nop */
2041 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2042 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2043 0x51, 0xc2, /* mov.l @(8,r12),r1 */
2044 0x41, 0x2b, /* jmp @r1 */
2045 0x00, 0x09, /* nop */
2046 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2049 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2051 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2052 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2053 0x2b, 0x40, /* jmp @r0 */
2054 0x09, 0x00, /* nop */
2055 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2056 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2057 0xc2, 0x51, /* mov.l @(8,r12),r1 */
2058 0x2b, 0x41, /* jmp @r1 */
2059 0x09, 0x00, /* nop */
2060 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2063 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
2066 /* Big-endian non-PIC. */
2067 vxworks_sh_plt0_entry_be,
2068 VXWORKS_PLT_HEADER_SIZE,
2069 { MINUS_ONE, MINUS_ONE, 8 },
2070 vxworks_sh_plt_entry_be,
2071 VXWORKS_PLT_ENTRY_SIZE,
2072 { 8, 14, 20, FALSE },
2077 /* Little-endian non-PIC. */
2078 vxworks_sh_plt0_entry_le,
2079 VXWORKS_PLT_HEADER_SIZE,
2080 { MINUS_ONE, MINUS_ONE, 8 },
2081 vxworks_sh_plt_entry_le,
2082 VXWORKS_PLT_ENTRY_SIZE,
2083 { 8, 14, 20, FALSE },
2090 /* Big-endian PIC. */
2093 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2094 vxworks_sh_pic_plt_entry_be,
2095 VXWORKS_PLT_ENTRY_SIZE,
2096 { 8, MINUS_ONE, 20, FALSE },
2101 /* Little-endian PIC. */
2104 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2105 vxworks_sh_pic_plt_entry_le,
2106 VXWORKS_PLT_ENTRY_SIZE,
2107 { 8, MINUS_ONE, 20, FALSE },
2114 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
2115 binding are to omit the lazy binding stub when linking with -z now
2116 and to move lazy binding stubs into a separate region for better
2119 #define FDPIC_PLT_ENTRY_SIZE 28
2120 #define FDPIC_PLT_LAZY_OFFSET 20
2122 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
2123 duplicated if it is out of range, or which can be inlined. So
2124 right now it is always inlined, which wastes a word per stub. It
2125 might be easier to handle the duplication if we put the lazy
2126 stubs separately. */
2128 static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] =
2130 0xd0, 0x02, /* mov.l @(12,pc),r0 */
2131 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2132 0x70, 0x04, /* add #4, r0 */
2133 0x41, 0x2b, /* jmp @r1 */
2134 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2135 0x00, 0x09, /* nop */
2136 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2137 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2138 0x60, 0xc2, /* mov.l @r12,r0 */
2139 0x40, 0x2b, /* jmp @r0 */
2140 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2141 0x00, 0x09, /* nop */
2144 static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] =
2146 0x02, 0xd0, /* mov.l @(12,pc),r0 */
2147 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2148 0x04, 0x70, /* add #4, r0 */
2149 0x2b, 0x41, /* jmp @r1 */
2150 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2151 0x09, 0x00, /* nop */
2152 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2153 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2154 0xc2, 0x60, /* mov.l @r12,r0 */
2155 0x2b, 0x40, /* jmp @r0 */
2156 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2157 0x09, 0x00, /* nop */
2160 static const struct elf_sh_plt_info fdpic_sh_plts[2] = {
2162 /* Big-endian PIC. */
2165 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2166 fdpic_sh_plt_entry_be,
2167 FDPIC_PLT_ENTRY_SIZE,
2168 { 12, MINUS_ONE, 16, FALSE },
2169 FDPIC_PLT_LAZY_OFFSET,
2173 /* Little-endian PIC. */
2176 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2177 fdpic_sh_plt_entry_le,
2178 FDPIC_PLT_ENTRY_SIZE,
2179 { 12, MINUS_ONE, 16, FALSE },
2180 FDPIC_PLT_LAZY_OFFSET,
2185 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
2186 entries for the first 64K slots. We use the normal FDPIC PLT entry
2187 past that point; we could also use movi20s, which might be faster,
2188 but would not be any smaller. */
2190 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
2191 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
2193 static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2195 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2196 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2197 0x70, 0x04, /* add #4, r0 */
2198 0x41, 0x2b, /* jmp @r1 */
2199 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2200 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2201 0x60, 0xc2, /* mov.l @r12,r0 */
2202 0x40, 0x2b, /* jmp @r0 */
2203 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2204 0x00, 0x09, /* nop */
2207 static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2209 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2210 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2211 0x04, 0x70, /* add #4, r0 */
2212 0x2b, 0x41, /* jmp @r1 */
2213 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2214 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2215 0xc2, 0x60, /* mov.l @r12,r0 */
2216 0x2b, 0x40, /* jmp @r0 */
2217 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2218 0x09, 0x00, /* nop */
2221 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = {
2222 /* Big-endian FDPIC, max index 64K. */
2225 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2226 fdpic_sh2a_plt_entry_be,
2227 FDPIC_SH2A_PLT_ENTRY_SIZE,
2228 { 0, MINUS_ONE, 12, TRUE },
2229 FDPIC_SH2A_PLT_LAZY_OFFSET,
2233 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = {
2234 /* Little-endian FDPIC, max index 64K. */
2237 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2238 fdpic_sh2a_plt_entry_le,
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_plts[2] = {
2247 /* Big-endian PIC. */
2250 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2251 fdpic_sh_plt_entry_be,
2252 FDPIC_PLT_ENTRY_SIZE,
2253 { 12, MINUS_ONE, 16, FALSE },
2254 FDPIC_PLT_LAZY_OFFSET,
2255 &fdpic_sh2a_short_plt_be
2258 /* Little-endian PIC. */
2261 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2262 fdpic_sh_plt_entry_le,
2263 FDPIC_PLT_ENTRY_SIZE,
2264 { 12, MINUS_ONE, 16, FALSE },
2265 FDPIC_PLT_LAZY_OFFSET,
2266 &fdpic_sh2a_short_plt_le
2270 /* Return the type of PLT associated with ABFD. PIC_P is true if
2271 the object is position-independent. */
2273 static const struct elf_sh_plt_info *
2274 get_plt_info (bfd *abfd, bfd_boolean pic_p)
2276 if (fdpic_object_p (abfd))
2278 /* If any input file requires SH2A we can use a shorter PLT
2280 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base)
2281 return &fdpic_sh2a_plts[!bfd_big_endian (abfd)];
2283 return &fdpic_sh_plts[!bfd_big_endian (abfd)];
2285 if (vxworks_object_p (abfd))
2286 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2287 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2290 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2291 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2295 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2296 unsigned long value, bfd_byte *addr)
2298 bfd_put_32 (output_bfd, value, addr);
2302 /* The number of PLT entries which can use a shorter PLT, if any.
2303 Currently always 64K, since only SH-2A FDPIC uses this; a
2304 20-bit movi20 can address that many function descriptors below
2305 _GLOBAL_OFFSET_TABLE_. */
2306 #define MAX_SHORT_PLT 65536
2308 /* Return the index of the PLT entry at byte offset OFFSET. */
2311 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2313 bfd_vma plt_index = 0;
2315 offset -= info->plt0_entry_size;
2316 if (info->short_plt != NULL)
2318 if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size)
2320 plt_index = MAX_SHORT_PLT;
2321 offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2324 info = info->short_plt;
2326 return plt_index + offset / info->symbol_entry_size;
2329 /* Do the inverse operation. */
2332 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index)
2336 if (info->short_plt != NULL)
2338 if (plt_index > MAX_SHORT_PLT)
2340 offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2341 plt_index -= MAX_SHORT_PLT;
2344 info = info->short_plt;
2346 return (offset + info->plt0_entry_size
2347 + (plt_index * info->symbol_entry_size));
2350 /* The sh linker needs to keep track of the number of relocs that it
2351 decides to copy as dynamic relocs in check_relocs for each symbol.
2352 This is so that it can later discard them if they are found to be
2353 unnecessary. We store the information in a field extending the
2354 regular ELF linker hash table. */
2356 struct elf_sh_dyn_relocs
2358 struct elf_sh_dyn_relocs *next;
2360 /* The input section of the reloc. */
2363 /* Total number of relocs copied for the input section. */
2364 bfd_size_type count;
2366 /* Number of pc-relative relocs copied for the input section. */
2367 bfd_size_type pc_count;
2372 bfd_signed_vma refcount;
2376 /* sh ELF linker hash entry. */
2378 struct elf_sh_link_hash_entry
2380 struct elf_link_hash_entry root;
2382 #ifdef INCLUDE_SHMEDIA
2385 bfd_signed_vma refcount;
2390 /* Track dynamic relocs copied for this symbol. */
2391 struct elf_sh_dyn_relocs *dyn_relocs;
2393 bfd_signed_vma gotplt_refcount;
2395 /* A local function descriptor, for FDPIC. The refcount counts
2396 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2397 relocations; the PLT and GOT entry are accounted
2398 for separately. After adjust_dynamic_symbol, the offset is
2399 MINUS_ONE if there is no local descriptor (dynamic linker
2400 managed and no PLT entry, or undefined weak non-dynamic).
2401 During check_relocs we do not yet know whether the local
2402 descriptor will be canonical. */
2403 union gotref funcdesc;
2405 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2406 and thus require fixups or relocations. */
2407 bfd_signed_vma abs_funcdesc_refcount;
2410 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC
2414 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2416 struct sh_elf_obj_tdata
2418 struct elf_obj_tdata root;
2420 /* got_type for each local got entry. */
2421 char *local_got_type;
2423 /* Function descriptor refcount and offset for each local symbol. */
2424 union gotref *local_funcdesc;
2427 #define sh_elf_tdata(abfd) \
2428 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2430 #define sh_elf_local_got_type(abfd) \
2431 (sh_elf_tdata (abfd)->local_got_type)
2433 #define sh_elf_local_funcdesc(abfd) \
2434 (sh_elf_tdata (abfd)->local_funcdesc)
2436 #define is_sh_elf(bfd) \
2437 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2438 && elf_tdata (bfd) != NULL \
2439 && elf_object_id (bfd) == SH_ELF_DATA)
2441 /* Override the generic function because we need to store sh_elf_obj_tdata
2442 as the specific tdata. */
2445 sh_elf_mkobject (bfd *abfd)
2447 return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata),
2451 /* sh ELF linker hash table. */
2453 struct elf_sh_link_hash_table
2455 struct elf_link_hash_table root;
2457 /* Short-cuts to get to dynamic linker sections. */
2465 asection *sfuncdesc;
2466 asection *srelfuncdesc;
2469 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2472 /* Small local sym cache. */
2473 struct sym_cache sym_cache;
2475 /* A counter or offset to track a TLS got entry. */
2478 bfd_signed_vma refcount;
2482 /* The type of PLT to use. */
2483 const struct elf_sh_plt_info *plt_info;
2485 /* True if the target system is VxWorks. */
2486 bfd_boolean vxworks_p;
2488 /* True if the target system uses FDPIC. */
2489 bfd_boolean fdpic_p;
2492 /* Traverse an sh ELF linker hash table. */
2494 #define sh_elf_link_hash_traverse(table, func, info) \
2495 (elf_link_hash_traverse \
2497 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2500 /* Get the sh ELF linker hash table from a link_info structure. */
2502 #define sh_elf_hash_table(p) \
2503 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2504 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2506 /* Create an entry in an sh ELF linker hash table. */
2508 static struct bfd_hash_entry *
2509 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2510 struct bfd_hash_table *table,
2513 struct elf_sh_link_hash_entry *ret =
2514 (struct elf_sh_link_hash_entry *) entry;
2516 /* Allocate the structure if it has not already been allocated by a
2518 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2519 ret = ((struct elf_sh_link_hash_entry *)
2520 bfd_hash_allocate (table,
2521 sizeof (struct elf_sh_link_hash_entry)));
2522 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2523 return (struct bfd_hash_entry *) ret;
2525 /* Call the allocation method of the superclass. */
2526 ret = ((struct elf_sh_link_hash_entry *)
2527 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2529 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2531 ret->dyn_relocs = NULL;
2532 ret->gotplt_refcount = 0;
2533 #ifdef INCLUDE_SHMEDIA
2534 ret->datalabel_got.refcount = ret->root.got.refcount;
2536 ret->funcdesc.refcount = 0;
2537 ret->abs_funcdesc_refcount = 0;
2538 ret->got_type = GOT_UNKNOWN;
2541 return (struct bfd_hash_entry *) ret;
2544 /* Create an sh ELF linker hash table. */
2546 static struct bfd_link_hash_table *
2547 sh_elf_link_hash_table_create (bfd *abfd)
2549 struct elf_sh_link_hash_table *ret;
2550 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
2552 ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
2553 if (ret == (struct elf_sh_link_hash_table *) NULL)
2556 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2557 sh_elf_link_hash_newfunc,
2558 sizeof (struct elf_sh_link_hash_entry),
2566 ret->sgotplt = NULL;
2567 ret->srelgot = NULL;
2569 ret->srelplt = NULL;
2570 ret->sdynbss = NULL;
2571 ret->srelbss = NULL;
2572 ret->srelplt2 = NULL;
2573 ret->sym_cache.abfd = NULL;
2574 ret->tls_ldm_got.refcount = 0;
2575 ret->plt_info = NULL;
2576 ret->vxworks_p = vxworks_object_p (abfd);
2577 ret->fdpic_p = fdpic_object_p (abfd);
2579 return &ret->root.root;
2583 sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2584 struct bfd_link_info *info, asection *p)
2586 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
2588 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2592 /* We need dynamic symbols for every section, since segments can
2593 relocate independently. */
2594 switch (elf_section_data (p)->this_hdr.sh_type)
2598 /* If sh_type is yet undecided, assume it could be
2599 SHT_PROGBITS/SHT_NOBITS. */
2603 /* There shouldn't be section relative relocations
2604 against any other section. */
2610 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2611 shortcuts to them in our hash table. */
2614 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2616 struct elf_sh_link_hash_table *htab;
2618 if (! _bfd_elf_create_got_section (dynobj, info))
2621 htab = sh_elf_hash_table (info);
2625 htab->sgot = bfd_get_linker_section (dynobj, ".got");
2626 htab->sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
2627 htab->srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2628 if (! htab->sgot || ! htab->sgotplt || ! htab->srelgot)
2631 htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
2632 (SEC_ALLOC | SEC_LOAD
2635 | SEC_LINKER_CREATED));
2636 if (htab->sfuncdesc == NULL
2637 || ! bfd_set_section_alignment (dynobj, htab->sfuncdesc, 2))
2640 htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
2641 ".rela.got.funcdesc",
2642 (SEC_ALLOC | SEC_LOAD
2645 | SEC_LINKER_CREATED
2647 if (htab->srelfuncdesc == NULL
2648 || ! bfd_set_section_alignment (dynobj, htab->srelfuncdesc, 2))
2651 /* Also create .rofixup. */
2652 htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
2653 (SEC_ALLOC | SEC_LOAD
2656 | SEC_LINKER_CREATED
2658 if (htab->srofixup == NULL
2659 || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2))
2665 /* Create dynamic sections when linking against a dynamic object. */
2668 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2670 struct elf_sh_link_hash_table *htab;
2671 flagword flags, pltflags;
2673 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2676 switch (bed->s->arch_size)
2687 bfd_set_error (bfd_error_bad_value);
2691 htab = sh_elf_hash_table (info);
2695 if (htab->root.dynamic_sections_created)
2698 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2699 .rel[a].bss sections. */
2701 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2702 | SEC_LINKER_CREATED);
2705 pltflags |= SEC_CODE;
2706 if (bed->plt_not_loaded)
2707 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2708 if (bed->plt_readonly)
2709 pltflags |= SEC_READONLY;
2711 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
2714 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2717 if (bed->want_plt_sym)
2719 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2721 struct elf_link_hash_entry *h;
2722 struct bfd_link_hash_entry *bh = NULL;
2724 if (! (_bfd_generic_link_add_one_symbol
2725 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2726 (bfd_vma) 0, (const char *) NULL, FALSE,
2727 get_elf_backend_data (abfd)->collect, &bh)))
2730 h = (struct elf_link_hash_entry *) bh;
2732 h->type = STT_OBJECT;
2733 htab->root.hplt = h;
2736 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2740 s = bfd_make_section_anyway_with_flags (abfd,
2741 bed->default_use_rela_p
2742 ? ".rela.plt" : ".rel.plt",
2743 flags | SEC_READONLY);
2746 || ! bfd_set_section_alignment (abfd, s, ptralign))
2749 if (htab->sgot == NULL
2750 && !create_got_section (abfd, info))
2753 if (bed->want_dynbss)
2755 /* The .dynbss section is a place to put symbols which are defined
2756 by dynamic objects, are referenced by regular objects, and are
2757 not functions. We must allocate space for them in the process
2758 image and use a R_*_COPY reloc to tell the dynamic linker to
2759 initialize them at run time. The linker script puts the .dynbss
2760 section into the .bss section of the final image. */
2761 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2762 SEC_ALLOC | SEC_LINKER_CREATED);
2767 /* The .rel[a].bss section holds copy relocs. This section is not
2768 normally needed. We need to create it here, though, so that the
2769 linker will map it to an output section. We can't just create it
2770 only if we need it, because we will not know whether we need it
2771 until we have seen all the input files, and the first time the
2772 main linker code calls BFD after examining all the input files
2773 (size_dynamic_sections) the input sections have already been
2774 mapped to the output sections. If the section turns out not to
2775 be needed, we can discard it later. We will never need this
2776 section when generating a shared object, since they do not use
2780 s = bfd_make_section_anyway_with_flags (abfd,
2781 (bed->default_use_rela_p
2782 ? ".rela.bss" : ".rel.bss"),
2783 flags | SEC_READONLY);
2786 || ! bfd_set_section_alignment (abfd, s, ptralign))
2791 if (htab->vxworks_p)
2793 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2800 /* Adjust a symbol defined by a dynamic object and referenced by a
2801 regular object. The current definition is in some section of the
2802 dynamic object, but we're not including those sections. We have to
2803 change the definition to something the rest of the link can
2807 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2808 struct elf_link_hash_entry *h)
2810 struct elf_sh_link_hash_table *htab;
2811 struct elf_sh_link_hash_entry *eh;
2812 struct elf_sh_dyn_relocs *p;
2815 htab = sh_elf_hash_table (info);
2819 /* Make sure we know what is going on here. */
2820 BFD_ASSERT (htab->root.dynobj != NULL
2822 || h->u.weakdef != NULL
2825 && !h->def_regular)));
2827 /* If this is a function, put it in the procedure linkage table. We
2828 will fill in the contents of the procedure linkage table later,
2829 when we know the address of the .got section. */
2830 if (h->type == STT_FUNC
2833 if (h->plt.refcount <= 0
2834 || SYMBOL_CALLS_LOCAL (info, h)
2835 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2836 && h->root.type == bfd_link_hash_undefweak))
2838 /* This case can occur if we saw a PLT reloc in an input
2839 file, but the symbol was never referred to by a dynamic
2840 object. In such a case, we don't actually need to build
2841 a procedure linkage table, and we can just do a REL32
2843 h->plt.offset = (bfd_vma) -1;
2850 h->plt.offset = (bfd_vma) -1;
2852 /* If this is a weak symbol, and there is a real definition, the
2853 processor independent code will have arranged for us to see the
2854 real definition first, and we can just use the same value. */
2855 if (h->u.weakdef != NULL)
2857 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2858 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2859 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2860 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2861 if (info->nocopyreloc)
2862 h->non_got_ref = h->u.weakdef->non_got_ref;
2866 /* This is a reference to a symbol defined by a dynamic object which
2867 is not a function. */
2869 /* If we are creating a shared library, we must presume that the
2870 only references to the symbol are via the global offset table.
2871 For such cases we need not do anything here; the relocations will
2872 be handled correctly by relocate_section. */
2876 /* If there are no references to this symbol that do not use the
2877 GOT, we don't need to generate a copy reloc. */
2878 if (!h->non_got_ref)
2881 /* If -z nocopyreloc was given, we won't generate them either. */
2882 if (info->nocopyreloc)
2888 eh = (struct elf_sh_link_hash_entry *) h;
2889 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2891 s = p->sec->output_section;
2892 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2896 /* If we didn't find any dynamic relocs in sections which needs the
2897 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2905 /* We must allocate the symbol in our .dynbss section, which will
2906 become part of the .bss section of the executable. There will be
2907 an entry for this symbol in the .dynsym section. The dynamic
2908 object will contain position independent code, so all references
2909 from the dynamic object to this symbol will go through the global
2910 offset table. The dynamic linker will use the .dynsym entry to
2911 determine the address it must put in the global offset table, so
2912 both the dynamic object and the regular object will refer to the
2913 same memory location for the variable. */
2916 BFD_ASSERT (s != NULL);
2918 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2919 copy the initial value out of the dynamic object and into the
2920 runtime process image. We need to remember the offset into the
2921 .rela.bss section we are going to use. */
2922 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2926 srel = htab->srelbss;
2927 BFD_ASSERT (srel != NULL);
2928 srel->size += sizeof (Elf32_External_Rela);
2932 return _bfd_elf_adjust_dynamic_copy (h, s);
2935 /* Allocate space in .plt, .got and associated reloc sections for
2939 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2941 struct bfd_link_info *info;
2942 struct elf_sh_link_hash_table *htab;
2943 struct elf_sh_link_hash_entry *eh;
2944 struct elf_sh_dyn_relocs *p;
2946 if (h->root.type == bfd_link_hash_indirect)
2949 info = (struct bfd_link_info *) inf;
2950 htab = sh_elf_hash_table (info);
2954 eh = (struct elf_sh_link_hash_entry *) h;
2955 if ((h->got.refcount > 0
2957 && eh->gotplt_refcount > 0)
2959 /* The symbol has been forced local, or we have some direct got refs,
2960 so treat all the gotplt refs as got refs. */
2961 h->got.refcount += eh->gotplt_refcount;
2962 if (h->plt.refcount >= eh->gotplt_refcount)
2963 h->plt.refcount -= eh->gotplt_refcount;
2966 if (htab->root.dynamic_sections_created
2967 && h->plt.refcount > 0
2968 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2969 || h->root.type != bfd_link_hash_undefweak))
2971 /* Make sure this symbol is output as a dynamic symbol.
2972 Undefined weak syms won't yet be marked as dynamic. */
2973 if (h->dynindx == -1
2974 && !h->forced_local)
2976 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2981 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2983 asection *s = htab->splt;
2984 const struct elf_sh_plt_info *plt_info;
2986 /* If this is the first .plt entry, make room for the special
2989 s->size += htab->plt_info->plt0_entry_size;
2991 h->plt.offset = s->size;
2993 /* If this symbol is not defined in a regular file, and we are
2994 not generating a shared library, then set the symbol to this
2995 location in the .plt. This is required to make function
2996 pointers compare as equal between the normal executable and
2997 the shared library. Skip this for FDPIC, since the
2998 function's address will be the address of the canonical
2999 function descriptor. */
3000 if (!htab->fdpic_p && !info->shared && !h->def_regular)
3002 h->root.u.def.section = s;
3003 h->root.u.def.value = h->plt.offset;
3006 /* Make room for this entry. */
3007 plt_info = htab->plt_info;
3008 if (plt_info->short_plt != NULL
3009 && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
3010 plt_info = plt_info->short_plt;
3011 s->size += plt_info->symbol_entry_size;
3013 /* We also need to make an entry in the .got.plt section, which
3014 will be placed in the .got section by the linker script. */
3016 htab->sgotplt->size += 4;
3018 htab->sgotplt->size += 8;
3020 /* We also need to make an entry in the .rel.plt section. */
3021 htab->srelplt->size += sizeof (Elf32_External_Rela);
3023 if (htab->vxworks_p && !info->shared)
3025 /* VxWorks executables have a second set of relocations
3026 for each PLT entry. They go in a separate relocation
3027 section, which is processed by the kernel loader. */
3029 /* There is a relocation for the initial PLT entry:
3030 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
3031 if (h->plt.offset == htab->plt_info->plt0_entry_size)
3032 htab->srelplt2->size += sizeof (Elf32_External_Rela);
3034 /* There are two extra relocations for each subsequent
3035 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
3036 and an R_SH_DIR32 relocation for the PLT entry. */
3037 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
3042 h->plt.offset = (bfd_vma) -1;
3048 h->plt.offset = (bfd_vma) -1;
3052 if (h->got.refcount > 0)
3056 enum got_type got_type = sh_elf_hash_entry (h)->got_type;
3058 /* Make sure this symbol is output as a dynamic symbol.
3059 Undefined weak syms won't yet be marked as dynamic. */
3060 if (h->dynindx == -1
3061 && !h->forced_local)
3063 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3068 h->got.offset = s->size;
3070 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
3071 if (got_type == GOT_TLS_GD)
3073 dyn = htab->root.dynamic_sections_created;
3076 /* No dynamic relocations required. */
3077 if (htab->fdpic_p && !info->shared
3078 && h->root.type != bfd_link_hash_undefweak
3079 && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
3080 htab->srofixup->size += 4;
3082 /* No dynamic relocations required when IE->LE conversion happens. */
3083 else if (got_type == GOT_TLS_IE && !h->def_dynamic && !info->shared)
3085 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3086 R_SH_TLS_GD needs one if local symbol and two if global. */
3087 else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
3088 || got_type == GOT_TLS_IE)
3089 htab->srelgot->size += sizeof (Elf32_External_Rela);
3090 else if (got_type == GOT_TLS_GD)
3091 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
3092 else if (got_type == GOT_FUNCDESC)
3094 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h))
3095 htab->srofixup->size += 4;
3097 htab->srelgot->size += sizeof (Elf32_External_Rela);
3099 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3100 || h->root.type != bfd_link_hash_undefweak)
3102 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3103 htab->srelgot->size += sizeof (Elf32_External_Rela);
3104 else if (htab->fdpic_p && !info->shared && got_type == GOT_NORMAL
3105 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3106 || h->root.type != bfd_link_hash_undefweak))
3107 htab->srofixup->size += 4;
3110 h->got.offset = (bfd_vma) -1;
3112 #ifdef INCLUDE_SHMEDIA
3113 if (eh->datalabel_got.refcount > 0)
3118 /* Make sure this symbol is output as a dynamic symbol.
3119 Undefined weak syms won't yet be marked as dynamic. */
3120 if (h->dynindx == -1
3121 && !h->forced_local)
3123 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3128 eh->datalabel_got.offset = s->size;
3130 dyn = htab->root.dynamic_sections_created;
3131 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
3132 htab->srelgot->size += sizeof (Elf32_External_Rela);
3135 eh->datalabel_got.offset = (bfd_vma) -1;
3138 /* Allocate space for any dynamic relocations to function
3139 descriptors, canonical or otherwise. We need to relocate the
3140 reference unless it resolves to zero, which only happens for
3141 undefined weak symbols (either non-default visibility, or when
3142 static linking). Any GOT slot is accounted for elsewhere. */
3143 if (eh->abs_funcdesc_refcount > 0
3144 && (h->root.type != bfd_link_hash_undefweak
3145 || (htab->root.dynamic_sections_created
3146 && ! SYMBOL_CALLS_LOCAL (info, h))))
3148 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h))
3149 htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
3152 += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
3155 /* We must allocate a function descriptor if there are references to
3156 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
3157 the dynamic linker isn't going to allocate it. None of this
3158 applies if we already created one in .got.plt, but if the
3159 canonical function descriptor can be in this object, there
3160 won't be a PLT entry at all. */
3161 if ((eh->funcdesc.refcount > 0
3162 || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
3163 && h->root.type != bfd_link_hash_undefweak
3164 && SYMBOL_FUNCDESC_LOCAL (info, h))
3166 /* Make room for this function descriptor. */
3167 eh->funcdesc.offset = htab->sfuncdesc->size;
3168 htab->sfuncdesc->size += 8;
3170 /* We will need a relocation or two fixups to initialize the
3171 function descriptor, so allocate those too. */
3172 if (!info->shared && SYMBOL_CALLS_LOCAL (info, h))
3173 htab->srofixup->size += 8;
3175 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3178 if (eh->dyn_relocs == NULL)
3181 /* In the shared -Bsymbolic case, discard space allocated for
3182 dynamic pc-relative relocs against symbols which turn out to be
3183 defined in regular objects. For the normal shared case, discard
3184 space for pc-relative relocs that have become local due to symbol
3185 visibility changes. */
3189 if (SYMBOL_CALLS_LOCAL (info, h))
3191 struct elf_sh_dyn_relocs **pp;
3193 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3195 p->count -= p->pc_count;
3204 if (htab->vxworks_p)
3206 struct elf_sh_dyn_relocs **pp;
3208 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3210 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
3217 /* Also discard relocs on undefined weak syms with non-default
3219 if (eh->dyn_relocs != NULL
3220 && h->root.type == bfd_link_hash_undefweak)
3222 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3223 eh->dyn_relocs = NULL;
3225 /* Make sure undefined weak symbols are output as a dynamic
3227 else if (h->dynindx == -1
3228 && !h->forced_local)
3230 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3237 /* For the non-shared case, discard space for relocs against
3238 symbols which turn out to need copy relocs or are not
3244 || (htab->root.dynamic_sections_created
3245 && (h->root.type == bfd_link_hash_undefweak
3246 || h->root.type == bfd_link_hash_undefined))))
3248 /* Make sure this symbol is output as a dynamic symbol.
3249 Undefined weak syms won't yet be marked as dynamic. */
3250 if (h->dynindx == -1
3251 && !h->forced_local)
3253 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3257 /* If that succeeded, we know we'll be keeping all the
3259 if (h->dynindx != -1)
3263 eh->dyn_relocs = NULL;
3268 /* Finally, allocate space. */
3269 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3271 asection *sreloc = elf_section_data (p->sec)->sreloc;
3272 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3274 /* If we need relocations, we do not need fixups. */
3275 if (htab->fdpic_p && !info->shared)
3276 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3282 /* Find any dynamic relocs that apply to read-only sections. */
3285 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3287 struct elf_sh_link_hash_entry *eh;
3288 struct elf_sh_dyn_relocs *p;
3290 eh = (struct elf_sh_link_hash_entry *) h;
3291 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3293 asection *s = p->sec->output_section;
3295 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3297 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3299 info->flags |= DF_TEXTREL;
3301 /* Not an error, just cut short the traversal. */
3308 /* This function is called after all the input files have been read,
3309 and the input sections have been assigned to output sections.
3310 It's a convenient place to determine the PLT style. */
3313 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
3315 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd, info->shared);
3317 if (sh_elf_hash_table (info)->fdpic_p && !info->relocatable
3318 && !bfd_elf_stack_segment_size (output_bfd, info,
3319 "__stacksize", DEFAULT_STACK_SIZE))
3324 /* Set the sizes of the dynamic sections. */
3327 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3328 struct bfd_link_info *info)
3330 struct elf_sh_link_hash_table *htab;
3336 htab = sh_elf_hash_table (info);
3340 dynobj = htab->root.dynobj;
3341 BFD_ASSERT (dynobj != NULL);
3343 if (htab->root.dynamic_sections_created)
3345 /* Set the contents of the .interp section to the interpreter. */
3346 if (info->executable)
3348 s = bfd_get_linker_section (dynobj, ".interp");
3349 BFD_ASSERT (s != NULL);
3350 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3351 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3355 /* Set up .got offsets for local syms, and space for local dynamic
3357 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3359 bfd_signed_vma *local_got;
3360 bfd_signed_vma *end_local_got;
3361 union gotref *local_funcdesc, *end_local_funcdesc;
3362 char *local_got_type;
3363 bfd_size_type locsymcount;
3364 Elf_Internal_Shdr *symtab_hdr;
3367 if (! is_sh_elf (ibfd))
3370 for (s = ibfd->sections; s != NULL; s = s->next)
3372 struct elf_sh_dyn_relocs *p;
3374 for (p = ((struct elf_sh_dyn_relocs *)
3375 elf_section_data (s)->local_dynrel);
3379 if (! bfd_is_abs_section (p->sec)
3380 && bfd_is_abs_section (p->sec->output_section))
3382 /* Input section has been discarded, either because
3383 it is a copy of a linkonce section or due to
3384 linker script /DISCARD/, so we'll be discarding
3387 else if (htab->vxworks_p
3388 && strcmp (p->sec->output_section->name,
3391 /* Relocations in vxworks .tls_vars sections are
3392 handled specially by the loader. */
3394 else if (p->count != 0)
3396 srel = elf_section_data (p->sec)->sreloc;
3397 srel->size += p->count * sizeof (Elf32_External_Rela);
3398 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3399 info->flags |= DF_TEXTREL;
3401 /* If we need relocations, we do not need fixups. */
3402 if (htab->fdpic_p && !info->shared)
3403 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3408 symtab_hdr = &elf_symtab_hdr (ibfd);
3409 locsymcount = symtab_hdr->sh_info;
3410 #ifdef INCLUDE_SHMEDIA
3411 /* Count datalabel local GOT. */
3415 srel = htab->srelgot;
3417 local_got = elf_local_got_refcounts (ibfd);
3420 end_local_got = local_got + locsymcount;
3421 local_got_type = sh_elf_local_got_type (ibfd);
3422 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3423 for (; local_got < end_local_got; ++local_got)
3427 *local_got = s->size;
3429 if (*local_got_type == GOT_TLS_GD)
3432 srel->size += sizeof (Elf32_External_Rela);
3434 htab->srofixup->size += 4;
3436 if (*local_got_type == GOT_FUNCDESC)
3438 if (local_funcdesc == NULL)
3442 size = locsymcount * sizeof (union gotref);
3443 local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
3445 if (local_funcdesc == NULL)
3447 sh_elf_local_funcdesc (ibfd) = local_funcdesc;
3448 local_funcdesc += (local_got
3449 - elf_local_got_refcounts (ibfd));
3451 local_funcdesc->refcount++;
3456 *local_got = (bfd_vma) -1;
3461 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3464 end_local_funcdesc = local_funcdesc + locsymcount;
3466 for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
3468 if (local_funcdesc->refcount > 0)
3470 local_funcdesc->offset = htab->sfuncdesc->size;
3471 htab->sfuncdesc->size += 8;
3473 htab->srofixup->size += 8;
3475 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3478 local_funcdesc->offset = MINUS_ONE;
3484 if (htab->tls_ldm_got.refcount > 0)
3486 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3488 htab->tls_ldm_got.offset = htab->sgot->size;
3489 htab->sgot->size += 8;
3490 htab->srelgot->size += sizeof (Elf32_External_Rela);
3493 htab->tls_ldm_got.offset = -1;
3495 /* Only the reserved entries should be present. For FDPIC, they go at
3496 the end of .got.plt. */
3499 BFD_ASSERT (htab->sgotplt && htab->sgotplt->size == 12);
3500 htab->sgotplt->size = 0;
3503 /* Allocate global sym .plt and .got entries, and space for global
3504 sym dynamic relocs. */
3505 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
3507 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3508 end of the FDPIC .got.plt. */
3511 htab->root.hgot->root.u.def.value = htab->sgotplt->size;
3512 htab->sgotplt->size += 12;
3515 /* At the very end of the .rofixup section is a pointer to the GOT. */
3516 if (htab->fdpic_p && htab->srofixup != NULL)
3517 htab->srofixup->size += 4;
3519 /* We now have determined the sizes of the various dynamic sections.
3520 Allocate memory for them. */
3522 for (s = dynobj->sections; s != NULL; s = s->next)
3524 if ((s->flags & SEC_LINKER_CREATED) == 0)
3529 || s == htab->sgotplt
3530 || s == htab->sfuncdesc
3531 || s == htab->srofixup
3532 || s == htab->sdynbss)
3534 /* Strip this section if we don't need it; see the
3537 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3539 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
3542 /* We use the reloc_count field as a counter if we need
3543 to copy relocs into the output file. */
3548 /* It's not one of our sections, so don't allocate space. */
3554 /* If we don't need this section, strip it from the
3555 output file. This is mostly to handle .rela.bss and
3556 .rela.plt. We must create both sections in
3557 create_dynamic_sections, because they must be created
3558 before the linker maps input sections to output
3559 sections. The linker does that before
3560 adjust_dynamic_symbol is called, and it is that
3561 function which decides whether anything needs to go
3562 into these sections. */
3564 s->flags |= SEC_EXCLUDE;
3568 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3571 /* Allocate memory for the section contents. We use bfd_zalloc
3572 here in case unused entries are not reclaimed before the
3573 section's contents are written out. This should not happen,
3574 but this way if it does, we get a R_SH_NONE reloc instead
3576 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3577 if (s->contents == NULL)
3581 if (htab->root.dynamic_sections_created)
3583 /* Add some entries to the .dynamic section. We fill in the
3584 values later, in sh_elf_finish_dynamic_sections, but we
3585 must add the entries now so that we get the correct size for
3586 the .dynamic section. The DT_DEBUG entry is filled in by the
3587 dynamic linker and used by the debugger. */
3588 #define add_dynamic_entry(TAG, VAL) \
3589 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3591 if (info->executable)
3593 if (! add_dynamic_entry (DT_DEBUG, 0))
3597 if (htab->splt->size != 0)
3599 if (! add_dynamic_entry (DT_PLTGOT, 0)
3600 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3601 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3602 || ! add_dynamic_entry (DT_JMPREL, 0))
3605 else if ((elf_elfheader (output_bfd)->e_flags & EF_SH_FDPIC)
3606 && htab->sgot->size != 0)
3608 if (! add_dynamic_entry (DT_PLTGOT, 0))
3614 if (! add_dynamic_entry (DT_RELA, 0)
3615 || ! add_dynamic_entry (DT_RELASZ, 0)
3616 || ! add_dynamic_entry (DT_RELAENT,
3617 sizeof (Elf32_External_Rela)))
3620 /* If any dynamic relocs apply to a read-only section,
3621 then we need a DT_TEXTREL entry. */
3622 if ((info->flags & DF_TEXTREL) == 0)
3623 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
3625 if ((info->flags & DF_TEXTREL) != 0)
3627 if (! add_dynamic_entry (DT_TEXTREL, 0))
3632 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3635 #undef add_dynamic_entry
3640 /* Add a dynamic relocation to the SRELOC section. */
3642 inline static bfd_vma
3643 sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
3644 int reloc_type, long dynindx, bfd_vma addend)
3646 Elf_Internal_Rela outrel;
3647 bfd_vma reloc_offset;
3649 outrel.r_offset = offset;
3650 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
3651 outrel.r_addend = addend;
3653 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
3654 BFD_ASSERT (reloc_offset < sreloc->size);
3655 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3656 sreloc->contents + reloc_offset);
3657 sreloc->reloc_count++;
3659 return reloc_offset;
3662 /* Add an FDPIC read-only fixup. */
3665 sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3667 bfd_vma fixup_offset;
3669 fixup_offset = srofixup->reloc_count++ * 4;
3670 BFD_ASSERT (fixup_offset < srofixup->size);
3671 bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3674 /* Return the offset of the generated .got section from the
3675 _GLOBAL_OFFSET_TABLE_ symbol. */
3677 static bfd_signed_vma
3678 sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
3680 return (htab->sgot->output_offset - htab->sgotplt->output_offset
3681 - htab->root.hgot->root.u.def.value);
3684 /* Find the segment number in which OSEC, and output section, is
3688 sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
3690 Elf_Internal_Phdr *p = NULL;
3692 if (output_bfd->xvec->flavour == bfd_target_elf_flavour)
3693 p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
3695 /* FIXME: Nothing ever says what this index is relative to. The kernel
3696 supplies data in terms of the number of load segments but this is
3697 a phdr index and the first phdr may not be a load segment. */
3698 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
3702 sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
3704 unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
3706 return (seg != (unsigned) -1
3707 && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
3710 /* Generate the initial contents of a local function descriptor, along
3711 with any relocations or fixups required. */
3713 sh_elf_initialize_funcdesc (bfd *output_bfd,
3714 struct bfd_link_info *info,
3715 struct elf_link_hash_entry *h,
3720 struct elf_sh_link_hash_table *htab;
3724 htab = sh_elf_hash_table (info);
3726 /* FIXME: The ABI says that the offset to the function goes in the
3727 descriptor, along with the segment index. We're RELA, so it could
3728 go in the reloc instead... */
3730 if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
3732 section = h->root.u.def.section;
3733 value = h->root.u.def.value;
3736 if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
3738 dynindx = elf_section_data (section->output_section)->dynindx;
3739 addr = value + section->output_offset;
3740 seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
3744 BFD_ASSERT (h->dynindx != -1);
3745 dynindx = h->dynindx;
3749 if (!info->shared && SYMBOL_CALLS_LOCAL (info, h))
3751 if (h == NULL || h->root.type != bfd_link_hash_undefweak)
3753 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3755 + htab->sfuncdesc->output_section->vma
3756 + htab->sfuncdesc->output_offset);
3757 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3759 + htab->sfuncdesc->output_section->vma
3760 + htab->sfuncdesc->output_offset);
3763 /* There are no dynamic relocations so fill in the final
3764 address and gp value (barring fixups). */
3765 addr += section->output_section->vma;
3766 seg = htab->root.hgot->root.u.def.value
3767 + htab->root.hgot->root.u.def.section->output_section->vma
3768 + htab->root.hgot->root.u.def.section->output_offset;
3771 sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
3773 + htab->sfuncdesc->output_section->vma
3774 + htab->sfuncdesc->output_offset,
3775 R_SH_FUNCDESC_VALUE, dynindx, 0);
3777 bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
3778 bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
3783 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3784 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3787 static bfd_reloc_status_type
3788 install_movi20_field (bfd *output_bfd, unsigned long relocation,
3789 bfd *input_bfd, asection *input_section,
3790 bfd_byte *contents, bfd_vma offset)
3792 unsigned long cur_val;
3794 bfd_reloc_status_type r;
3796 if (offset > bfd_get_section_limit (input_bfd, input_section))
3797 return bfd_reloc_outofrange;
3799 r = bfd_check_overflow (complain_overflow_signed, 20, 0,
3800 bfd_arch_bits_per_address (input_bfd), relocation);
3801 if (r != bfd_reloc_ok)
3804 addr = contents + offset;
3805 cur_val = bfd_get_16 (output_bfd, addr);
3806 bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
3807 bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
3809 return bfd_reloc_ok;
3812 /* Relocate an SH ELF section. */
3815 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3816 bfd *input_bfd, asection *input_section,
3817 bfd_byte *contents, Elf_Internal_Rela *relocs,
3818 Elf_Internal_Sym *local_syms,
3819 asection **local_sections)
3821 struct elf_sh_link_hash_table *htab;
3822 Elf_Internal_Shdr *symtab_hdr;
3823 struct elf_link_hash_entry **sym_hashes;
3824 Elf_Internal_Rela *rel, *relend;
3826 bfd_vma *local_got_offsets;
3827 asection *sgot = NULL;
3828 asection *sgotplt = NULL;
3829 asection *splt = NULL;
3830 asection *sreloc = NULL;
3831 asection *srelgot = NULL;
3832 bfd_boolean is_vxworks_tls;
3833 unsigned isec_segment, got_segment, plt_segment, check_segment[2];
3834 bfd_boolean fdpic_p = FALSE;
3836 BFD_ASSERT (is_sh_elf (input_bfd));
3838 htab = sh_elf_hash_table (info);
3841 dynobj = htab->root.dynobj;
3843 sgotplt = htab->sgotplt;
3845 fdpic_p = htab->fdpic_p;
3847 symtab_hdr = &elf_symtab_hdr (input_bfd);
3848 sym_hashes = elf_sym_hashes (input_bfd);
3849 local_got_offsets = elf_local_got_offsets (input_bfd);
3851 isec_segment = sh_elf_osec_to_segment (output_bfd,
3852 input_section->output_section);
3853 if (fdpic_p && sgot)
3854 got_segment = sh_elf_osec_to_segment (output_bfd,
3855 sgot->output_section);
3858 if (fdpic_p && splt)
3859 plt_segment = sh_elf_osec_to_segment (output_bfd,
3860 splt->output_section);
3864 /* We have to handle relocations in vxworks .tls_vars sections
3865 specially, because the dynamic loader is 'weird'. */
3866 is_vxworks_tls = (htab && htab->vxworks_p && info->shared
3867 && !strcmp (input_section->output_section->name,
3871 relend = relocs + input_section->reloc_count;
3872 for (; rel < relend; rel++)
3875 reloc_howto_type *howto;
3876 unsigned long r_symndx;
3877 Elf_Internal_Sym *sym;
3879 struct elf_link_hash_entry *h;
3881 bfd_vma addend = (bfd_vma) 0;
3882 bfd_reloc_status_type r;
3883 int seen_stt_datalabel = 0;
3885 enum got_type got_type;
3886 const char *symname = NULL;
3888 r_symndx = ELF32_R_SYM (rel->r_info);
3890 r_type = ELF32_R_TYPE (rel->r_info);
3892 /* Many of the relocs are only used for relaxing, and are
3893 handled entirely by the relaxation code. */
3894 if (r_type >= (int) R_SH_GNU_VTINHERIT
3895 && r_type <= (int) R_SH_LABEL)
3897 if (r_type == (int) R_SH_NONE)
3901 || r_type >= R_SH_max
3902 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3903 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3904 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3905 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
3906 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3907 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3908 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3909 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3910 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3911 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
3912 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
3913 && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
3915 bfd_set_error (bfd_error_bad_value);
3919 howto = get_howto_table (output_bfd) + r_type;
3921 /* For relocs that aren't partial_inplace, we get the addend from
3923 if (! howto->partial_inplace)
3924 addend = rel->r_addend;
3929 check_segment[0] = -1;
3930 check_segment[1] = -1;
3931 if (r_symndx < symtab_hdr->sh_info)
3933 sym = local_syms + r_symndx;
3934 sec = local_sections[r_symndx];
3936 symname = bfd_elf_string_from_elf_section
3937 (input_bfd, symtab_hdr->sh_link, sym->st_name);
3938 if (symname == NULL || *symname == '\0')
3939 symname = bfd_section_name (input_bfd, sec);
3941 relocation = (sec->output_section->vma
3942 + sec->output_offset
3944 /* A local symbol never has STO_SH5_ISA32, so we don't need
3945 datalabel processing here. Make sure this does not change
3947 if ((sym->st_other & STO_SH5_ISA32) != 0)
3948 ((*info->callbacks->reloc_dangerous)
3950 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3951 input_bfd, input_section, rel->r_offset));
3953 if (sec != NULL && discarded_section (sec))
3954 /* Handled below. */
3956 else if (info->relocatable)
3958 /* This is a relocatable link. We don't have to change
3959 anything, unless the reloc is against a section symbol,
3960 in which case we have to adjust according to where the
3961 section symbol winds up in the output section. */
3962 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3964 if (! howto->partial_inplace)
3966 /* For relocations with the addend in the
3967 relocation, we need just to update the addend.
3968 All real relocs are of type partial_inplace; this
3969 code is mostly for completeness. */
3970 rel->r_addend += sec->output_offset;
3975 /* Relocs of type partial_inplace need to pick up the
3976 contents in the contents and add the offset resulting
3977 from the changed location of the section symbol.
3978 Using _bfd_final_link_relocate (e.g. goto
3979 final_link_relocate) here would be wrong, because
3980 relocations marked pc_relative would get the current
3981 location subtracted, and we must only do that at the
3983 r = _bfd_relocate_contents (howto, input_bfd,
3986 contents + rel->r_offset);
3987 goto relocation_done;
3992 else if (! howto->partial_inplace)
3994 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3995 addend = rel->r_addend;
3997 else if ((sec->flags & SEC_MERGE)
3998 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4002 if (howto->rightshift || howto->src_mask != 0xffffffff)
4004 (*_bfd_error_handler)
4005 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
4006 input_bfd, input_section,
4007 (long) rel->r_offset, howto->name);
4011 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4014 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4016 addend += msec->output_section->vma + msec->output_offset;
4017 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4023 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
4026 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4027 symname = h->root.root.string;
4028 while (h->root.type == bfd_link_hash_indirect
4029 || h->root.type == bfd_link_hash_warning)
4031 #ifdef INCLUDE_SHMEDIA
4032 /* If the reference passes a symbol marked with
4033 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4035 seen_stt_datalabel |= h->type == STT_DATALABEL;
4037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4039 if (h->root.type == bfd_link_hash_defined
4040 || h->root.type == bfd_link_hash_defweak)
4044 dyn = htab ? htab->root.dynamic_sections_created : FALSE;
4045 sec = h->root.u.def.section;
4046 /* In these cases, we don't need the relocation value.
4047 We check specially because in some obscure cases
4048 sec->output_section will be NULL. */
4049 if (r_type == R_SH_GOTPC
4050 || r_type == R_SH_GOTPC_LOW16
4051 || r_type == R_SH_GOTPC_MEDLOW16
4052 || r_type == R_SH_GOTPC_MEDHI16
4053 || r_type == R_SH_GOTPC_HI16
4054 || ((r_type == R_SH_PLT32
4055 || r_type == R_SH_PLT_LOW16
4056 || r_type == R_SH_PLT_MEDLOW16
4057 || r_type == R_SH_PLT_MEDHI16
4058 || r_type == R_SH_PLT_HI16)
4059 && h->plt.offset != (bfd_vma) -1)
4060 || ((r_type == R_SH_GOT32
4061 || r_type == R_SH_GOT20
4062 || r_type == R_SH_GOTFUNCDESC
4063 || r_type == R_SH_GOTFUNCDESC20
4064 || r_type == R_SH_GOTOFFFUNCDESC
4065 || r_type == R_SH_GOTOFFFUNCDESC20
4066 || r_type == R_SH_FUNCDESC
4067 || r_type == R_SH_GOT_LOW16
4068 || r_type == R_SH_GOT_MEDLOW16
4069 || r_type == R_SH_GOT_MEDHI16
4070 || r_type == R_SH_GOT_HI16)
4071 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4073 || (! info->symbolic && h->dynindx != -1)
4074 || !h->def_regular))
4075 /* The cases above are those in which relocation is
4076 overwritten in the switch block below. The cases
4077 below are those in which we must defer relocation
4078 to run-time, because we can't resolve absolute
4079 addresses when creating a shared library. */
4081 && ((! info->symbolic && h->dynindx != -1)
4083 && ((r_type == R_SH_DIR32
4084 && !h->forced_local)
4085 || (r_type == R_SH_REL32
4086 && !SYMBOL_CALLS_LOCAL (info, h)))
4087 && ((input_section->flags & SEC_ALLOC) != 0
4088 /* DWARF will emit R_SH_DIR32 relocations in its
4089 sections against symbols defined externally
4090 in shared libraries. We can't do anything
4092 || ((input_section->flags & SEC_DEBUGGING) != 0
4093 && h->def_dynamic)))
4094 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4095 sections because such sections are not SEC_ALLOC and
4096 thus ld.so will not process them. */
4097 || (sec->output_section == NULL
4098 && ((input_section->flags & SEC_DEBUGGING) != 0
4100 || (sec->output_section == NULL
4101 && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
4102 || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
4104 else if (sec->output_section != NULL)
4105 relocation = ((h->root.u.def.value
4106 + sec->output_section->vma
4107 + sec->output_offset)
4108 /* A STO_SH5_ISA32 causes a "bitor 1" to the
4109 symbol value, unless we've seen
4110 STT_DATALABEL on the way to it. */
4111 | ((h->other & STO_SH5_ISA32) != 0
4112 && ! seen_stt_datalabel));
4113 else if (!info->relocatable
4114 && (_bfd_elf_section_offset (output_bfd, info,
4119 (*_bfd_error_handler)
4120 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4123 (long) rel->r_offset,
4125 h->root.root.string);
4129 else if (h->root.type == bfd_link_hash_undefweak)
4131 else if (info->unresolved_syms_in_objects == RM_IGNORE
4132 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4134 else if (!info->relocatable)
4136 if (! info->callbacks->undefined_symbol
4137 (info, h->root.root.string, input_bfd,
4138 input_section, rel->r_offset,
4139 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
4140 || ELF_ST_VISIBILITY (h->other))))
4145 if (sec != NULL && discarded_section (sec))
4146 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4147 rel, 1, relend, howto, 0, contents);
4149 if (info->relocatable)
4152 /* Check for inter-segment relocations in FDPIC files. Most
4153 relocations connect the relocation site to the location of
4154 the target symbol, but there are some exceptions below. */
4155 check_segment[0] = isec_segment;
4157 check_segment[1] = sh_elf_osec_to_segment (output_bfd,
4158 sec->output_section);
4160 check_segment[1] = -1;
4162 switch ((int) r_type)
4164 final_link_relocate:
4165 /* COFF relocs don't use the addend. The addend is used for
4166 R_SH_DIR32 to be compatible with other compilers. */
4167 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4168 contents, rel->r_offset,
4169 relocation, addend);
4173 goto final_link_relocate;
4178 /* If the reloc is against the start of this section, then
4179 the assembler has already taken care of it and the reloc
4180 is here only to assist in relaxing. If the reloc is not
4181 against the start of this section, then it's against an
4182 external symbol and we must deal with it ourselves. */
4183 if (input_section->output_section->vma + input_section->output_offset
4186 int disp = (relocation
4187 - input_section->output_section->vma
4188 - input_section->output_offset
4194 case R_SH_DIR8WPZ: mask = 1; break;
4195 case R_SH_DIR8WPL: mask = 3; break;
4196 default: mask = 0; break;
4200 ((*_bfd_error_handler)
4201 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4202 input_section->owner,
4203 (unsigned long) rel->r_offset));
4204 bfd_set_error (bfd_error_bad_value);
4208 goto final_link_relocate;
4214 #ifdef INCLUDE_SHMEDIA
4215 if (shmedia_prepare_reloc (info, input_bfd, input_section,
4216 contents, rel, &relocation))
4217 goto final_link_relocate;
4219 bfd_set_error (bfd_error_bad_value);
4227 goto final_link_relocate;
4233 ((*_bfd_error_handler)
4234 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4235 input_section->owner,
4236 (unsigned long) rel->r_offset, howto->name,
4237 (unsigned long) relocation));
4238 bfd_set_error (bfd_error_bad_value);
4241 goto final_link_relocate;
4248 ((*_bfd_error_handler)
4249 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4250 input_section->owner,
4251 (unsigned long) rel->r_offset, howto->name,
4252 (unsigned long) relocation));
4253 bfd_set_error (bfd_error_bad_value);
4256 goto final_link_relocate;
4259 if ((signed int)relocation < -32
4260 || (signed int)relocation > 32)
4262 ((*_bfd_error_handler)
4263 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
4264 input_section->owner,
4265 (unsigned long) rel->r_offset,
4266 (unsigned long) relocation));
4267 bfd_set_error (bfd_error_bad_value);
4270 goto final_link_relocate;
4273 if ((signed int)relocation < -16
4274 || (signed int)relocation > 16)
4276 ((*_bfd_error_handler)
4277 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
4278 input_section->owner,
4279 (unsigned long) rel->r_offset,
4280 (unsigned long) relocation));
4281 bfd_set_error (bfd_error_bad_value);
4284 goto final_link_relocate;
4288 #ifdef INCLUDE_SHMEDIA
4289 case R_SH_IMM_LOW16_PCREL:
4290 case R_SH_IMM_MEDLOW16_PCREL:
4291 case R_SH_IMM_MEDHI16_PCREL:
4292 case R_SH_IMM_HI16_PCREL:
4296 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4297 || h->root.type != bfd_link_hash_undefweak)
4298 && r_symndx != STN_UNDEF
4299 && (input_section->flags & SEC_ALLOC) != 0
4301 && (r_type == R_SH_DIR32
4302 || !SYMBOL_CALLS_LOCAL (info, h)))
4304 Elf_Internal_Rela outrel;
4306 bfd_boolean skip, relocate;
4308 /* When generating a shared object, these relocations
4309 are copied into the output file to be resolved at run
4314 sreloc = _bfd_elf_get_dynamic_reloc_section
4315 (input_bfd, input_section, /*rela?*/ TRUE);
4324 _bfd_elf_section_offset (output_bfd, info, input_section,
4326 if (outrel.r_offset == (bfd_vma) -1)
4328 else if (outrel.r_offset == (bfd_vma) -2)
4329 skip = TRUE, relocate = TRUE;
4330 outrel.r_offset += (input_section->output_section->vma
4331 + input_section->output_offset);
4334 memset (&outrel, 0, sizeof outrel);
4335 else if (r_type == R_SH_REL32)
4337 BFD_ASSERT (h != NULL && h->dynindx != -1);
4338 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4340 = (howto->partial_inplace
4341 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4344 #ifdef INCLUDE_SHMEDIA
4345 else if (r_type == R_SH_IMM_LOW16_PCREL
4346 || r_type == R_SH_IMM_MEDLOW16_PCREL
4347 || r_type == R_SH_IMM_MEDHI16_PCREL
4348 || r_type == R_SH_IMM_HI16_PCREL)
4350 BFD_ASSERT (h != NULL && h->dynindx != -1);
4351 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4352 outrel.r_addend = addend;
4357 || ((info->symbolic || h->dynindx == -1)
4358 && h->def_regular)))
4362 BFD_ASSERT (sec != NULL);
4363 BFD_ASSERT (sec->output_section != NULL);
4364 dynindx = elf_section_data (sec->output_section)->dynindx;
4365 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4366 outrel.r_addend = relocation;
4368 += (howto->partial_inplace
4369 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4371 outrel.r_addend -= sec->output_section->vma;
4375 /* h->dynindx may be -1 if this symbol was marked to
4378 || ((info->symbolic || h->dynindx == -1)
4381 relocate = howto->partial_inplace;
4382 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4386 BFD_ASSERT (h->dynindx != -1);
4387 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4389 outrel.r_addend = relocation;
4391 += (howto->partial_inplace
4392 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4396 loc = sreloc->contents;
4397 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4398 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4400 check_segment[0] = check_segment[1] = -1;
4402 /* If this reloc is against an external symbol, we do
4403 not want to fiddle with the addend. Otherwise, we
4404 need to include the symbol value so that it becomes
4405 an addend for the dynamic reloc. */
4409 else if (fdpic_p && !info->shared
4410 && r_type == R_SH_DIR32
4411 && (input_section->flags & SEC_ALLOC) != 0)
4417 if (sh_elf_osec_readonly_p (output_bfd,
4418 input_section->output_section))
4420 (*_bfd_error_handler)
4421 (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"),
4424 (long) rel->r_offset,
4429 offset = _bfd_elf_section_offset (output_bfd, info,
4430 input_section, rel->r_offset);
4431 if (offset != (bfd_vma)-1)
4432 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4433 input_section->output_section->vma
4434 + input_section->output_offset
4437 check_segment[0] = check_segment[1] = -1;
4439 goto final_link_relocate;
4442 #ifdef INCLUDE_SHMEDIA
4443 case R_SH_GOTPLT_LOW16:
4444 case R_SH_GOTPLT_MEDLOW16:
4445 case R_SH_GOTPLT_MEDHI16:
4446 case R_SH_GOTPLT_HI16:
4447 case R_SH_GOTPLT10BY4:
4448 case R_SH_GOTPLT10BY8:
4450 /* Relocation is to the entry for this symbol in the
4451 procedure linkage table. */
4458 || h->plt.offset == (bfd_vma) -1
4459 || h->got.offset != (bfd_vma) -1)
4462 /* Relocation is to the entry for this symbol in the global
4463 offset table extension for the procedure linkage table. */
4466 BFD_ASSERT (sgotplt != NULL);
4467 relocation = (sgotplt->output_offset
4468 + (get_plt_index (htab->plt_info, h->plt.offset)
4472 relocation -= GOT_BIAS;
4475 goto final_link_relocate;
4480 #ifdef INCLUDE_SHMEDIA
4481 case R_SH_GOT_LOW16:
4482 case R_SH_GOT_MEDLOW16:
4483 case R_SH_GOT_MEDHI16:
4488 /* Relocation is to the entry for this symbol in the global
4492 BFD_ASSERT (sgot != NULL);
4493 check_segment[0] = check_segment[1] = -1;
4499 off = h->got.offset;
4500 #ifdef INCLUDE_SHMEDIA
4501 if (seen_stt_datalabel)
4503 struct elf_sh_link_hash_entry *hsh;
4505 hsh = (struct elf_sh_link_hash_entry *)h;
4506 off = hsh->datalabel_got.offset;
4509 BFD_ASSERT (off != (bfd_vma) -1);
4511 dyn = htab->root.dynamic_sections_created;
4512 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4514 && SYMBOL_REFERENCES_LOCAL (info, h))
4515 || (ELF_ST_VISIBILITY (h->other)
4516 && h->root.type == bfd_link_hash_undefweak))
4518 /* This is actually a static link, or it is a
4519 -Bsymbolic link and the symbol is defined
4520 locally, or the symbol was forced to be local
4521 because of a version file. We must initialize
4522 this entry in the global offset table. Since the
4523 offset must always be a multiple of 4, we use the
4524 least significant bit to record whether we have
4525 initialized it already.
4527 When doing a dynamic link, we create a .rela.got
4528 relocation entry to initialize the value. This
4529 is done in the finish_dynamic_symbol routine. */
4534 bfd_put_32 (output_bfd, relocation,
4535 sgot->contents + off);
4536 #ifdef INCLUDE_SHMEDIA
4537 if (seen_stt_datalabel)
4539 struct elf_sh_link_hash_entry *hsh;
4541 hsh = (struct elf_sh_link_hash_entry *)h;
4542 hsh->datalabel_got.offset |= 1;
4548 /* If we initialize the GOT entry here with a valid
4549 symbol address, also add a fixup. */
4550 if (fdpic_p && !info->shared
4551 && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
4552 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4553 || h->root.type != bfd_link_hash_undefweak))
4554 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4555 sgot->output_section->vma
4556 + sgot->output_offset
4561 relocation = sh_elf_got_offset (htab) + off;
4565 #ifdef INCLUDE_SHMEDIA
4568 BFD_ASSERT (local_got_offsets != NULL
4569 && (local_got_offsets[symtab_hdr->sh_info
4573 off = local_got_offsets[symtab_hdr->sh_info
4579 BFD_ASSERT (local_got_offsets != NULL
4580 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4582 off = local_got_offsets[r_symndx];
4583 #ifdef INCLUDE_SHMEDIA
4587 /* The offset must always be a multiple of 4. We use
4588 the least significant bit to record whether we have
4589 already generated the necessary reloc. */
4594 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4598 Elf_Internal_Rela outrel;
4601 if (srelgot == NULL)
4603 srelgot = bfd_get_linker_section (dynobj,
4605 BFD_ASSERT (srelgot != NULL);
4608 outrel.r_offset = (sgot->output_section->vma
4609 + sgot->output_offset
4614 = elf_section_data (sec->output_section)->dynindx;
4615 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4616 outrel.r_addend = relocation;
4617 outrel.r_addend -= sec->output_section->vma;
4621 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4622 outrel.r_addend = relocation;
4624 loc = srelgot->contents;
4625 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4626 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4629 && (sh_elf_local_got_type (input_bfd) [r_symndx]
4631 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4632 sgot->output_section->vma
4633 + sgot->output_offset
4636 #ifdef INCLUDE_SHMEDIA
4638 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
4641 local_got_offsets[r_symndx] |= 1;
4644 relocation = sh_elf_got_offset (htab) + off;
4648 relocation -= GOT_BIAS;
4651 if (r_type == R_SH_GOT20)
4653 r = install_movi20_field (output_bfd, relocation + addend,
4654 input_bfd, input_section, contents,
4659 goto final_link_relocate;
4663 #ifdef INCLUDE_SHMEDIA
4664 case R_SH_GOTOFF_LOW16:
4665 case R_SH_GOTOFF_MEDLOW16:
4666 case R_SH_GOTOFF_MEDHI16:
4667 case R_SH_GOTOFF_HI16:
4669 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4670 we place at the start of the .got.plt section. This is the same
4671 as the start of the output .got section, unless there are function
4672 descriptors in front of it. */
4674 BFD_ASSERT (sgotplt != NULL);
4675 check_segment[0] = got_segment;
4676 relocation -= sgotplt->output_section->vma + sgotplt->output_offset
4677 + htab->root.hgot->root.u.def.value;
4680 relocation -= GOT_BIAS;
4683 addend = rel->r_addend;
4685 if (r_type == R_SH_GOTOFF20)
4687 r = install_movi20_field (output_bfd, relocation + addend,
4688 input_bfd, input_section, contents,
4693 goto final_link_relocate;
4696 #ifdef INCLUDE_SHMEDIA
4697 case R_SH_GOTPC_LOW16:
4698 case R_SH_GOTPC_MEDLOW16:
4699 case R_SH_GOTPC_MEDHI16:
4700 case R_SH_GOTPC_HI16:
4702 /* Use global offset table as symbol value. */
4704 BFD_ASSERT (sgotplt != NULL);
4705 relocation = sgotplt->output_section->vma + sgotplt->output_offset;
4708 relocation += GOT_BIAS;
4711 addend = rel->r_addend;
4713 goto final_link_relocate;
4716 #ifdef INCLUDE_SHMEDIA
4717 case R_SH_PLT_LOW16:
4718 case R_SH_PLT_MEDLOW16:
4719 case R_SH_PLT_MEDHI16:
4722 /* Relocation is to the entry for this symbol in the
4723 procedure linkage table. */
4725 /* Resolve a PLT reloc against a local symbol directly,
4726 without using the procedure linkage table. */
4728 goto final_link_relocate;
4730 /* We don't want to warn on calls to undefined weak symbols,
4731 as calls to them must be protected by non-NULL tests
4732 anyway, and unprotected calls would invoke undefined
4734 if (h->root.type == bfd_link_hash_undefweak)
4735 check_segment[0] = check_segment[1] = -1;
4737 if (h->forced_local)
4738 goto final_link_relocate;
4740 if (h->plt.offset == (bfd_vma) -1)
4742 /* We didn't make a PLT entry for this symbol. This
4743 happens when statically linking PIC code, or when
4744 using -Bsymbolic. */
4745 goto final_link_relocate;
4748 BFD_ASSERT (splt != NULL);
4749 check_segment[1] = plt_segment;
4750 relocation = (splt->output_section->vma
4751 + splt->output_offset
4754 #ifdef INCLUDE_SHMEDIA
4758 addend = rel->r_addend;
4760 goto final_link_relocate;
4762 /* Relocation is to the canonical function descriptor for this
4763 symbol, possibly via the GOT. Initialize the GOT
4764 entry and function descriptor if necessary. */
4765 case R_SH_GOTFUNCDESC:
4766 case R_SH_GOTFUNCDESC20:
4770 asection *reloc_section;
4771 bfd_vma reloc_offset;
4772 int reloc_type = R_SH_FUNCDESC;
4776 check_segment[0] = check_segment[1] = -1;
4778 /* FIXME: See what FRV does for global symbols in the
4779 executable, with --export-dynamic. Do they need ld.so
4780 to allocate official descriptors? See what this code
4786 if (r_type == R_SH_FUNCDESC)
4788 reloc_section = input_section;
4789 reloc_offset = rel->r_offset;
4793 reloc_section = sgot;
4796 reloc_offset = h->got.offset;
4799 BFD_ASSERT (local_got_offsets != NULL);
4800 reloc_offset = local_got_offsets[r_symndx];
4802 BFD_ASSERT (reloc_offset != MINUS_ONE);
4804 if (reloc_offset & 1)
4807 goto funcdesc_done_got;
4811 if (h && h->root.type == bfd_link_hash_undefweak
4812 && (SYMBOL_CALLS_LOCAL (info, h)
4813 || !htab->root.dynamic_sections_created))
4814 /* Undefined weak symbol which will not be dynamically
4815 resolved later; leave it at zero. */
4816 goto funcdesc_leave_zero;
4817 else if (SYMBOL_CALLS_LOCAL (info, h)
4818 && ! SYMBOL_FUNCDESC_LOCAL (info, h))
4820 /* If the symbol needs a non-local function descriptor
4821 but binds locally (i.e., its visibility is
4822 protected), emit a dynamic relocation decayed to
4823 section+offset. This is an optimization; the dynamic
4824 linker would resolve our function descriptor request
4825 to our copy of the function anyway. */
4826 dynindx = elf_section_data (h->root.u.def.section
4827 ->output_section)->dynindx;
4828 relocation += h->root.u.def.section->output_offset
4829 + h->root.u.def.value;
4831 else if (! SYMBOL_FUNCDESC_LOCAL (info, h))
4833 /* If the symbol is dynamic and there will be dynamic
4834 symbol resolution because we are or are linked with a
4835 shared library, emit a FUNCDESC relocation such that
4836 the dynamic linker will allocate the function
4838 BFD_ASSERT (h->dynindx != -1);
4839 dynindx = h->dynindx;
4845 /* Otherwise, we know we have a private function
4846 descriptor, so reference it directly. */
4847 reloc_type = R_SH_DIR32;
4848 dynindx = elf_section_data (htab->sfuncdesc
4849 ->output_section)->dynindx;
4853 offset = sh_elf_hash_entry (h)->funcdesc.offset;
4854 BFD_ASSERT (offset != MINUS_ONE);
4855 if ((offset & 1) == 0)
4857 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4860 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4865 union gotref *local_funcdesc;
4867 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4868 offset = local_funcdesc[r_symndx].offset;
4869 BFD_ASSERT (offset != MINUS_ONE);
4870 if ((offset & 1) == 0)
4872 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4876 local_funcdesc[r_symndx].offset |= 1;
4880 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4883 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h))
4887 if (sh_elf_osec_readonly_p (output_bfd,
4888 reloc_section->output_section))
4890 (*_bfd_error_handler)
4891 (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"),
4894 (long) rel->r_offset,
4899 offset = _bfd_elf_section_offset (output_bfd, info,
4900 reloc_section, reloc_offset);
4902 if (offset != (bfd_vma)-1)
4903 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4905 + reloc_section->output_section->vma
4906 + reloc_section->output_offset);
4908 else if ((reloc_section->output_section->flags
4909 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
4913 if (sh_elf_osec_readonly_p (output_bfd,
4914 reloc_section->output_section))
4916 info->callbacks->warning
4918 _("cannot emit dynamic relocations in read-only section"),
4919 symname, input_bfd, reloc_section, reloc_offset);
4923 if (srelgot == NULL)
4925 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
4926 BFD_ASSERT (srelgot != NULL);
4929 offset = _bfd_elf_section_offset (output_bfd, info,
4930 reloc_section, reloc_offset);
4932 if (offset != (bfd_vma)-1)
4933 sh_elf_add_dyn_reloc (output_bfd, srelgot,
4935 + reloc_section->output_section->vma
4936 + reloc_section->output_offset,
4937 reloc_type, dynindx, relocation);
4939 if (r_type == R_SH_FUNCDESC)
4947 goto funcdesc_leave_zero;
4951 if (SYMBOL_FUNCDESC_LOCAL (info, h))
4952 relocation += htab->sfuncdesc->output_section->vma;
4953 funcdesc_leave_zero:
4954 if (r_type != R_SH_FUNCDESC)
4956 bfd_put_32 (output_bfd, relocation,
4957 reloc_section->contents + reloc_offset);
4961 local_got_offsets[r_symndx] |= 1;
4965 relocation = sh_elf_got_offset (htab) + reloc_offset;
4967 relocation -= GOT_BIAS;
4970 if (r_type == R_SH_GOTFUNCDESC20)
4972 r = install_movi20_field (output_bfd, relocation + addend,
4973 input_bfd, input_section, contents,
4978 goto final_link_relocate;
4982 case R_SH_GOTOFFFUNCDESC:
4983 case R_SH_GOTOFFFUNCDESC20:
4984 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4985 executable and --export-dynamic. If such symbols get
4986 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
4990 check_segment[0] = check_segment[1] = -1;
4992 addend = rel->r_addend;
4994 if (h && (h->root.type == bfd_link_hash_undefweak
4995 || !SYMBOL_FUNCDESC_LOCAL (info, h)))
4998 (_("%B(%A+0x%lx): %s relocation against external symbol \"%s\""),
4999 input_bfd, input_section, (long) rel->r_offset, howto->name,
5000 h->root.root.string);
5007 /* Otherwise, we know we have a private function
5008 descriptor, so reference it directly. */
5011 offset = sh_elf_hash_entry (h)->funcdesc.offset;
5012 BFD_ASSERT (offset != MINUS_ONE);
5013 if ((offset & 1) == 0)
5015 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
5018 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
5023 union gotref *local_funcdesc;
5025 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
5026 offset = local_funcdesc[r_symndx].offset;
5027 BFD_ASSERT (offset != MINUS_ONE);
5028 if ((offset & 1) == 0)
5030 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
5034 local_funcdesc[r_symndx].offset |= 1;
5038 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
5041 relocation -= (htab->root.hgot->root.u.def.value
5042 + sgotplt->output_offset);
5044 relocation -= GOT_BIAS;
5047 if (r_type == R_SH_GOTOFFFUNCDESC20)
5049 r = install_movi20_field (output_bfd, relocation + addend,
5050 input_bfd, input_section, contents,
5055 goto final_link_relocate;
5057 case R_SH_LOOP_START:
5059 static bfd_vma start, end;
5061 start = (relocation + rel->r_addend
5062 - (sec->output_section->vma + sec->output_offset));
5063 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5064 rel->r_offset, sec, start, end);
5068 end = (relocation + rel->r_addend
5069 - (sec->output_section->vma + sec->output_offset));
5070 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5071 rel->r_offset, sec, start, end);
5075 case R_SH_TLS_GD_32:
5076 case R_SH_TLS_IE_32:
5078 check_segment[0] = check_segment[1] = -1;
5079 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5080 got_type = GOT_UNKNOWN;
5081 if (h == NULL && local_got_offsets)
5082 got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
5085 got_type = sh_elf_hash_entry (h)->got_type;
5087 && (h->dynindx == -1
5089 r_type = R_SH_TLS_LE_32;
5092 if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
5093 r_type = R_SH_TLS_IE_32;
5095 if (r_type == R_SH_TLS_LE_32)
5098 unsigned short insn;
5100 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5102 /* GD->LE transition:
5103 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5104 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5105 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5107 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5109 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
5111 offset = rel->r_offset;
5112 BFD_ASSERT (offset >= 16);
5113 /* Size of GD instructions is 16 or 18. */
5115 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5116 if ((insn & 0xff00) == 0xc700)
5118 BFD_ASSERT (offset >= 2);
5120 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5123 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5124 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5125 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5126 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5127 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5128 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5129 BFD_ASSERT (insn == 0x310c);
5130 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5131 BFD_ASSERT (insn == 0x410b);
5132 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5133 BFD_ASSERT (insn == 0x34cc);
5135 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5136 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5137 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5138 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5139 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5145 /* IE->LE transition:
5146 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5147 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5149 mov.l .Ln,rM; stc gbr,rN; nop; ...;
5152 offset = rel->r_offset;
5153 BFD_ASSERT (offset >= 16);
5154 /* Size of IE instructions is 10 or 12. */
5156 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5157 if ((insn & 0xf0ff) == 0x0012)
5159 BFD_ASSERT (offset >= 2);
5161 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5164 BFD_ASSERT ((insn & 0xff00) == 0xd000);
5165 target = insn & 0x00ff;
5166 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5167 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5168 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5169 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5170 insn = 0xd000 | (insn & 0x0f00) | target;
5171 bfd_put_16 (output_bfd, insn, contents + offset + 0);
5172 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5175 bfd_put_32 (output_bfd, tpoff (info, relocation),
5176 contents + rel->r_offset);
5180 if (sgot == NULL || sgotplt == NULL)
5184 off = h->got.offset;
5187 if (local_got_offsets == NULL)
5190 off = local_got_offsets[r_symndx];
5193 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
5194 if (r_type == R_SH_TLS_IE_32
5195 && ! htab->root.dynamic_sections_created)
5198 bfd_put_32 (output_bfd, tpoff (info, relocation),
5199 sgot->contents + off);
5200 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5201 contents + rel->r_offset);
5209 Elf_Internal_Rela outrel;
5213 if (srelgot == NULL)
5215 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
5216 BFD_ASSERT (srelgot != NULL);
5219 outrel.r_offset = (sgot->output_section->vma
5220 + sgot->output_offset + off);
5222 if (h == NULL || h->dynindx == -1)
5227 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5229 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5230 outrel.r_addend = relocation - dtpoff_base (info);
5232 outrel.r_addend = 0;
5233 outrel.r_info = ELF32_R_INFO (indx, dr_type);
5234 loc = srelgot->contents;
5235 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5236 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5238 if (r_type == R_SH_TLS_GD_32)
5242 bfd_put_32 (output_bfd,
5243 relocation - dtpoff_base (info),
5244 sgot->contents + off + 4);
5248 outrel.r_info = ELF32_R_INFO (indx,
5250 outrel.r_offset += 4;
5251 outrel.r_addend = 0;
5252 srelgot->reloc_count++;
5253 loc += sizeof (Elf32_External_Rela);
5254 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5261 local_got_offsets[r_symndx] |= 1;
5264 if (off >= (bfd_vma) -2)
5267 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5268 relocation = sh_elf_got_offset (htab) + off;
5272 unsigned short insn;
5274 /* GD->IE transition:
5275 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5276 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5277 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5279 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5280 nop; nop; bra 3f; nop; .align 2;
5281 1: .long x@TPOFF; 2:...; 3:. */
5283 offset = rel->r_offset;
5284 BFD_ASSERT (offset >= 16);
5285 /* Size of GD instructions is 16 or 18. */
5287 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5288 if ((insn & 0xff00) == 0xc700)
5290 BFD_ASSERT (offset >= 2);
5292 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5295 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5297 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
5298 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5300 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5301 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5302 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5303 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5304 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5305 BFD_ASSERT (insn == 0x310c);
5306 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5307 BFD_ASSERT (insn == 0x410b);
5308 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5309 BFD_ASSERT (insn == 0x34cc);
5311 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5312 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5313 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5314 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5315 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5317 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5318 contents + rel->r_offset);
5323 addend = rel->r_addend;
5325 goto final_link_relocate;
5327 case R_SH_TLS_LD_32:
5329 check_segment[0] = check_segment[1] = -1;
5333 unsigned short insn;
5335 /* LD->LE transition:
5336 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5337 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5338 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5340 stc gbr,r0; nop; nop; nop;
5341 nop; nop; bra 3f; ...; 3:. */
5343 offset = rel->r_offset;
5344 BFD_ASSERT (offset >= 16);
5345 /* Size of LD instructions is 16 or 18. */
5347 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5348 if ((insn & 0xff00) == 0xc700)
5350 BFD_ASSERT (offset >= 2);
5352 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5355 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5356 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5357 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5358 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5359 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5360 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5361 BFD_ASSERT (insn == 0x310c);
5362 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5363 BFD_ASSERT (insn == 0x410b);
5364 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5365 BFD_ASSERT (insn == 0x34cc);
5367 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5368 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5369 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5370 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5371 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5372 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5377 if (sgot == NULL || sgotplt == NULL)
5380 off = htab->tls_ldm_got.offset;
5385 Elf_Internal_Rela outrel;
5388 srelgot = htab->srelgot;
5389 if (srelgot == NULL)
5392 outrel.r_offset = (sgot->output_section->vma
5393 + sgot->output_offset + off);
5394 outrel.r_addend = 0;
5395 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5396 loc = srelgot->contents;
5397 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5398 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5399 htab->tls_ldm_got.offset |= 1;
5402 relocation = sh_elf_got_offset (htab) + off;
5403 addend = rel->r_addend;
5405 goto final_link_relocate;
5407 case R_SH_TLS_LDO_32:
5408 check_segment[0] = check_segment[1] = -1;
5410 relocation = tpoff (info, relocation);
5412 relocation -= dtpoff_base (info);
5414 addend = rel->r_addend;
5415 goto final_link_relocate;
5417 case R_SH_TLS_LE_32:
5420 Elf_Internal_Rela outrel;
5423 check_segment[0] = check_segment[1] = -1;
5425 if (! info->shared || info->pie)
5427 relocation = tpoff (info, relocation);
5428 addend = rel->r_addend;
5429 goto final_link_relocate;
5434 sreloc = _bfd_elf_get_dynamic_reloc_section
5435 (input_bfd, input_section, /*rela?*/ TRUE);
5440 if (h == NULL || h->dynindx == -1)
5445 outrel.r_offset = (input_section->output_section->vma
5446 + input_section->output_offset
5448 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5450 outrel.r_addend = relocation - dtpoff_base (info);
5452 outrel.r_addend = 0;
5454 loc = sreloc->contents;
5455 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5456 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5462 if (fdpic_p && check_segment[0] != (unsigned) -1
5463 && check_segment[0] != check_segment[1])
5465 /* We don't want duplicate errors for undefined symbols. */
5466 if (!h || h->root.type != bfd_link_hash_undefined)
5470 info->callbacks->einfo
5471 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5472 input_bfd, input_section, rel->r_offset, symname);
5476 info->callbacks->einfo
5477 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5478 input_bfd, input_section, rel->r_offset, symname);
5481 elf_elfheader (output_bfd)->e_flags &= ~EF_SH_PIC;
5484 if (r != bfd_reloc_ok)
5489 case bfd_reloc_outofrange:
5491 case bfd_reloc_overflow:
5499 name = (bfd_elf_string_from_elf_section
5500 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5504 name = bfd_section_name (input_bfd, sec);
5506 if (! ((*info->callbacks->reloc_overflow)
5507 (info, (h ? &h->root : NULL), name, howto->name,
5508 (bfd_vma) 0, input_bfd, input_section,
5520 /* This is a version of bfd_generic_get_relocated_section_contents
5521 which uses sh_elf_relocate_section. */
5524 sh_elf_get_relocated_section_contents (bfd *output_bfd,
5525 struct bfd_link_info *link_info,
5526 struct bfd_link_order *link_order,
5528 bfd_boolean relocatable,
5531 Elf_Internal_Shdr *symtab_hdr;
5532 asection *input_section = link_order->u.indirect.section;
5533 bfd *input_bfd = input_section->owner;
5534 asection **sections = NULL;
5535 Elf_Internal_Rela *internal_relocs = NULL;
5536 Elf_Internal_Sym *isymbuf = NULL;
5538 /* We only need to handle the case of relaxing, or of having a
5539 particular set of section contents, specially. */
5541 || elf_section_data (input_section)->this_hdr.contents == NULL)
5542 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5547 symtab_hdr = &elf_symtab_hdr (input_bfd);
5549 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5550 (size_t) input_section->size);
5552 if ((input_section->flags & SEC_RELOC) != 0
5553 && input_section->reloc_count > 0)
5556 Elf_Internal_Sym *isym, *isymend;
5559 internal_relocs = (_bfd_elf_link_read_relocs
5560 (input_bfd, input_section, NULL,
5561 (Elf_Internal_Rela *) NULL, FALSE));
5562 if (internal_relocs == NULL)
5565 if (symtab_hdr->sh_info != 0)
5567 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5568 if (isymbuf == NULL)
5569 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5570 symtab_hdr->sh_info, 0,
5572 if (isymbuf == NULL)
5576 amt = symtab_hdr->sh_info;
5577 amt *= sizeof (asection *);
5578 sections = (asection **) bfd_malloc (amt);
5579 if (sections == NULL && amt != 0)
5582 isymend = isymbuf + symtab_hdr->sh_info;
5583 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5587 if (isym->st_shndx == SHN_UNDEF)
5588 isec = bfd_und_section_ptr;
5589 else if (isym->st_shndx == SHN_ABS)
5590 isec = bfd_abs_section_ptr;
5591 else if (isym->st_shndx == SHN_COMMON)
5592 isec = bfd_com_section_ptr;
5594 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5599 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5600 input_section, data, internal_relocs,
5604 if (sections != NULL)
5607 && symtab_hdr->contents != (unsigned char *) isymbuf)
5609 if (elf_section_data (input_section)->relocs != internal_relocs)
5610 free (internal_relocs);
5616 if (sections != NULL)
5619 && symtab_hdr->contents != (unsigned char *) isymbuf)
5621 if (internal_relocs != NULL
5622 && elf_section_data (input_section)->relocs != internal_relocs)
5623 free (internal_relocs);
5627 /* Return the base VMA address which should be subtracted from real addresses
5628 when resolving @dtpoff relocation.
5629 This is PT_TLS segment p_vaddr. */
5632 dtpoff_base (struct bfd_link_info *info)
5634 /* If tls_sec is NULL, we should have signalled an error already. */
5635 if (elf_hash_table (info)->tls_sec == NULL)
5637 return elf_hash_table (info)->tls_sec->vma;
5640 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5643 tpoff (struct bfd_link_info *info, bfd_vma address)
5645 /* If tls_sec is NULL, we should have signalled an error already. */
5646 if (elf_hash_table (info)->tls_sec == NULL)
5648 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5649 structure which has 2 pointer fields. */
5650 return (address - elf_hash_table (info)->tls_sec->vma
5651 + align_power ((bfd_vma) 8,
5652 elf_hash_table (info)->tls_sec->alignment_power));
5656 sh_elf_gc_mark_hook (asection *sec,
5657 struct bfd_link_info *info,
5658 Elf_Internal_Rela *rel,
5659 struct elf_link_hash_entry *h,
5660 Elf_Internal_Sym *sym)
5663 switch (ELF32_R_TYPE (rel->r_info))
5665 case R_SH_GNU_VTINHERIT:
5666 case R_SH_GNU_VTENTRY:
5670 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5673 /* Update the got entry reference counts for the section being removed. */
5676 sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5677 asection *sec, const Elf_Internal_Rela *relocs)
5679 Elf_Internal_Shdr *symtab_hdr;
5680 struct elf_link_hash_entry **sym_hashes;
5681 bfd_signed_vma *local_got_refcounts;
5682 union gotref *local_funcdesc;
5683 const Elf_Internal_Rela *rel, *relend;
5685 if (info->relocatable)
5688 elf_section_data (sec)->local_dynrel = NULL;
5690 symtab_hdr = &elf_symtab_hdr (abfd);
5691 sym_hashes = elf_sym_hashes (abfd);
5692 local_got_refcounts = elf_local_got_refcounts (abfd);
5693 local_funcdesc = sh_elf_local_funcdesc (abfd);
5695 relend = relocs + sec->reloc_count;
5696 for (rel = relocs; rel < relend; rel++)
5698 unsigned long r_symndx;
5699 unsigned int r_type;
5700 struct elf_link_hash_entry *h = NULL;
5701 #ifdef INCLUDE_SHMEDIA
5702 int seen_stt_datalabel = 0;
5705 r_symndx = ELF32_R_SYM (rel->r_info);
5706 if (r_symndx >= symtab_hdr->sh_info)
5708 struct elf_sh_link_hash_entry *eh;
5709 struct elf_sh_dyn_relocs **pp;
5710 struct elf_sh_dyn_relocs *p;
5712 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5713 while (h->root.type == bfd_link_hash_indirect
5714 || h->root.type == bfd_link_hash_warning)
5716 #ifdef INCLUDE_SHMEDIA
5717 seen_stt_datalabel |= h->type == STT_DATALABEL;
5719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5721 eh = (struct elf_sh_link_hash_entry *) h;
5722 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5725 /* Everything must go for SEC. */
5731 r_type = ELF32_R_TYPE (rel->r_info);
5732 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
5734 case R_SH_TLS_LD_32:
5735 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
5736 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
5744 #ifdef INCLUDE_SHMEDIA
5745 case R_SH_GOT_LOW16:
5746 case R_SH_GOT_MEDLOW16:
5747 case R_SH_GOT_MEDHI16:
5751 case R_SH_GOTOFF_LOW16:
5752 case R_SH_GOTOFF_MEDLOW16:
5753 case R_SH_GOTOFF_MEDHI16:
5754 case R_SH_GOTOFF_HI16:
5755 case R_SH_GOTPC_LOW16:
5756 case R_SH_GOTPC_MEDLOW16:
5757 case R_SH_GOTPC_MEDHI16:
5758 case R_SH_GOTPC_HI16:
5760 case R_SH_TLS_GD_32:
5761 case R_SH_TLS_IE_32:
5762 case R_SH_GOTFUNCDESC:
5763 case R_SH_GOTFUNCDESC20:
5766 #ifdef INCLUDE_SHMEDIA
5767 if (seen_stt_datalabel)
5769 struct elf_sh_link_hash_entry *eh;
5770 eh = (struct elf_sh_link_hash_entry *) h;
5771 if (eh->datalabel_got.refcount > 0)
5772 eh->datalabel_got.refcount -= 1;
5776 if (h->got.refcount > 0)
5777 h->got.refcount -= 1;
5779 else if (local_got_refcounts != NULL)
5781 #ifdef INCLUDE_SHMEDIA
5782 if (rel->r_addend & 1)
5784 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5785 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5789 if (local_got_refcounts[r_symndx] > 0)
5790 local_got_refcounts[r_symndx] -= 1;
5796 sh_elf_hash_entry (h)->abs_funcdesc_refcount -= 1;
5797 else if (sh_elf_hash_table (info)->fdpic_p && !info->shared)
5798 sh_elf_hash_table (info)->srofixup->size -= 4;
5802 case R_SH_GOTOFFFUNCDESC:
5803 case R_SH_GOTOFFFUNCDESC20:
5805 sh_elf_hash_entry (h)->funcdesc.refcount -= 1;
5807 local_funcdesc[r_symndx].refcount -= 1;
5811 if (sh_elf_hash_table (info)->fdpic_p && !info->shared
5812 && (sec->flags & SEC_ALLOC) != 0)
5813 sh_elf_hash_table (info)->srofixup->size -= 4;
5822 #ifdef INCLUDE_SHMEDIA
5823 case R_SH_PLT_LOW16:
5824 case R_SH_PLT_MEDLOW16:
5825 case R_SH_PLT_MEDHI16:
5830 if (h->plt.refcount > 0)
5831 h->plt.refcount -= 1;
5836 #ifdef INCLUDE_SHMEDIA
5837 case R_SH_GOTPLT_LOW16:
5838 case R_SH_GOTPLT_MEDLOW16:
5839 case R_SH_GOTPLT_MEDHI16:
5840 case R_SH_GOTPLT_HI16:
5841 case R_SH_GOTPLT10BY4:
5842 case R_SH_GOTPLT10BY8:
5846 struct elf_sh_link_hash_entry *eh;
5847 eh = (struct elf_sh_link_hash_entry *) h;
5848 if (eh->gotplt_refcount > 0)
5850 eh->gotplt_refcount -= 1;
5851 if (h->plt.refcount > 0)
5852 h->plt.refcount -= 1;
5854 #ifdef INCLUDE_SHMEDIA
5855 else if (seen_stt_datalabel)
5857 if (eh->datalabel_got.refcount > 0)
5858 eh->datalabel_got.refcount -= 1;
5861 else if (h->got.refcount > 0)
5862 h->got.refcount -= 1;
5864 else if (local_got_refcounts != NULL)
5866 #ifdef INCLUDE_SHMEDIA
5867 if (rel->r_addend & 1)
5869 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5870 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5874 if (local_got_refcounts[r_symndx] > 0)
5875 local_got_refcounts[r_symndx] -= 1;
5887 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5890 sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
5891 struct elf_link_hash_entry *dir,
5892 struct elf_link_hash_entry *ind)
5894 struct elf_sh_link_hash_entry *edir, *eind;
5896 edir = (struct elf_sh_link_hash_entry *) dir;
5897 eind = (struct elf_sh_link_hash_entry *) ind;
5899 if (eind->dyn_relocs != NULL)
5901 if (edir->dyn_relocs != NULL)
5903 struct elf_sh_dyn_relocs **pp;
5904 struct elf_sh_dyn_relocs *p;
5906 /* Add reloc counts against the indirect sym to the direct sym
5907 list. Merge any entries against the same section. */
5908 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5910 struct elf_sh_dyn_relocs *q;
5912 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5913 if (q->sec == p->sec)
5915 q->pc_count += p->pc_count;
5916 q->count += p->count;
5923 *pp = edir->dyn_relocs;
5926 edir->dyn_relocs = eind->dyn_relocs;
5927 eind->dyn_relocs = NULL;
5929 edir->gotplt_refcount = eind->gotplt_refcount;
5930 eind->gotplt_refcount = 0;
5931 #ifdef INCLUDE_SHMEDIA
5932 edir->datalabel_got.refcount += eind->datalabel_got.refcount;
5933 eind->datalabel_got.refcount = 0;
5935 edir->funcdesc.refcount += eind->funcdesc.refcount;
5936 eind->funcdesc.refcount = 0;
5937 edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
5938 eind->abs_funcdesc_refcount = 0;
5940 if (ind->root.type == bfd_link_hash_indirect
5941 && dir->got.refcount <= 0)
5943 edir->got_type = eind->got_type;
5944 eind->got_type = GOT_UNKNOWN;
5947 if (ind->root.type != bfd_link_hash_indirect
5948 && dir->dynamic_adjusted)
5950 /* If called to transfer flags for a weakdef during processing
5951 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5952 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5953 dir->ref_dynamic |= ind->ref_dynamic;
5954 dir->ref_regular |= ind->ref_regular;
5955 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
5956 dir->needs_plt |= ind->needs_plt;
5959 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
5963 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
5971 case R_SH_TLS_GD_32:
5972 case R_SH_TLS_IE_32:
5974 return R_SH_TLS_LE_32;
5975 return R_SH_TLS_IE_32;
5976 case R_SH_TLS_LD_32:
5977 return R_SH_TLS_LE_32;
5983 /* Look through the relocs for a section during the first phase.
5984 Since we don't do .gots or .plts, we just need to consider the
5985 virtual table relocs for gc. */
5988 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
5989 const Elf_Internal_Rela *relocs)
5991 Elf_Internal_Shdr *symtab_hdr;
5992 struct elf_link_hash_entry **sym_hashes;
5993 struct elf_sh_link_hash_table *htab;
5994 const Elf_Internal_Rela *rel;
5995 const Elf_Internal_Rela *rel_end;
5997 unsigned int r_type;
5998 enum got_type got_type, old_got_type;
6002 if (info->relocatable)
6005 BFD_ASSERT (is_sh_elf (abfd));
6007 symtab_hdr = &elf_symtab_hdr (abfd);
6008 sym_hashes = elf_sym_hashes (abfd);
6010 htab = sh_elf_hash_table (info);
6014 rel_end = relocs + sec->reloc_count;
6015 for (rel = relocs; rel < rel_end; rel++)
6017 struct elf_link_hash_entry *h;
6018 unsigned long r_symndx;
6019 #ifdef INCLUDE_SHMEDIA
6020 int seen_stt_datalabel = 0;
6023 r_symndx = ELF32_R_SYM (rel->r_info);
6024 r_type = ELF32_R_TYPE (rel->r_info);
6026 if (r_symndx < symtab_hdr->sh_info)
6030 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6031 while (h->root.type == bfd_link_hash_indirect
6032 || h->root.type == bfd_link_hash_warning)
6034 #ifdef INCLUDE_SHMEDIA
6035 seen_stt_datalabel |= h->type == STT_DATALABEL;
6037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6041 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
6043 && r_type == R_SH_TLS_IE_32
6045 && h->root.type != bfd_link_hash_undefined
6046 && h->root.type != bfd_link_hash_undefweak
6047 && (h->dynindx == -1
6049 r_type = R_SH_TLS_LE_32;
6054 case R_SH_GOTOFFFUNCDESC:
6055 case R_SH_GOTOFFFUNCDESC20:
6057 case R_SH_GOTFUNCDESC:
6058 case R_SH_GOTFUNCDESC20:
6061 if (h->dynindx == -1)
6062 switch (ELF_ST_VISIBILITY (h->other))
6068 bfd_elf_link_record_dynamic_symbol (info, h);
6075 /* Some relocs require a global offset table. */
6076 if (htab->sgot == NULL)
6081 /* This may require an rofixup. */
6090 case R_SH_GOTFUNCDESC:
6091 case R_SH_GOTFUNCDESC20:
6092 case R_SH_GOTOFFFUNCDESC:
6093 case R_SH_GOTOFFFUNCDESC20:
6095 #ifdef INCLUDE_SHMEDIA
6096 case R_SH_GOTPLT_LOW16:
6097 case R_SH_GOTPLT_MEDLOW16:
6098 case R_SH_GOTPLT_MEDHI16:
6099 case R_SH_GOTPLT_HI16:
6100 case R_SH_GOTPLT10BY4:
6101 case R_SH_GOTPLT10BY8:
6102 case R_SH_GOT_LOW16:
6103 case R_SH_GOT_MEDLOW16:
6104 case R_SH_GOT_MEDHI16:
6108 case R_SH_GOTOFF_LOW16:
6109 case R_SH_GOTOFF_MEDLOW16:
6110 case R_SH_GOTOFF_MEDHI16:
6111 case R_SH_GOTOFF_HI16:
6112 case R_SH_GOTPC_LOW16:
6113 case R_SH_GOTPC_MEDLOW16:
6114 case R_SH_GOTPC_MEDHI16:
6115 case R_SH_GOTPC_HI16:
6117 case R_SH_TLS_GD_32:
6118 case R_SH_TLS_LD_32:
6119 case R_SH_TLS_IE_32:
6120 if (htab->root.dynobj == NULL)
6121 htab->root.dynobj = abfd;
6122 if (!create_got_section (htab->root.dynobj, info))
6133 /* This relocation describes the C++ object vtable hierarchy.
6134 Reconstruct it for later use during GC. */
6135 case R_SH_GNU_VTINHERIT:
6136 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6140 /* This relocation describes which C++ vtable entries are actually
6141 used. Record for later use during GC. */
6142 case R_SH_GNU_VTENTRY:
6143 BFD_ASSERT (h != NULL);
6145 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6149 case R_SH_TLS_IE_32:
6151 info->flags |= DF_STATIC_TLS;
6155 case R_SH_TLS_GD_32:
6158 #ifdef INCLUDE_SHMEDIA
6159 case R_SH_GOT_LOW16:
6160 case R_SH_GOT_MEDLOW16:
6161 case R_SH_GOT_MEDHI16:
6166 case R_SH_GOTFUNCDESC:
6167 case R_SH_GOTFUNCDESC20:
6171 got_type = GOT_NORMAL;
6173 case R_SH_TLS_GD_32:
6174 got_type = GOT_TLS_GD;
6176 case R_SH_TLS_IE_32:
6177 got_type = GOT_TLS_IE;
6179 case R_SH_GOTFUNCDESC:
6180 case R_SH_GOTFUNCDESC20:
6181 got_type = GOT_FUNCDESC;
6187 #ifdef INCLUDE_SHMEDIA
6188 if (seen_stt_datalabel)
6190 struct elf_sh_link_hash_entry *eh
6191 = (struct elf_sh_link_hash_entry *) h;
6193 eh->datalabel_got.refcount += 1;
6197 h->got.refcount += 1;
6198 old_got_type = sh_elf_hash_entry (h)->got_type;
6202 bfd_signed_vma *local_got_refcounts;
6204 /* This is a global offset table entry for a local
6206 local_got_refcounts = elf_local_got_refcounts (abfd);
6207 if (local_got_refcounts == NULL)
6211 size = symtab_hdr->sh_info;
6212 size *= sizeof (bfd_signed_vma);
6213 #ifdef INCLUDE_SHMEDIA
6214 /* Reserve space for both the datalabel and
6215 codelabel local GOT offsets. */
6218 size += symtab_hdr->sh_info;
6219 local_got_refcounts = ((bfd_signed_vma *)
6220 bfd_zalloc (abfd, size));
6221 if (local_got_refcounts == NULL)
6223 elf_local_got_refcounts (abfd) = local_got_refcounts;
6224 #ifdef INCLUDE_SHMEDIA
6225 /* Take care of both the datalabel and codelabel local
6227 sh_elf_local_got_type (abfd)
6228 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6230 sh_elf_local_got_type (abfd)
6231 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6234 #ifdef INCLUDE_SHMEDIA
6235 if (rel->r_addend & 1)
6236 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6239 local_got_refcounts[r_symndx] += 1;
6240 old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
6243 /* If a TLS symbol is accessed using IE at least once,
6244 there is no point to use dynamic model for it. */
6245 if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
6246 && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
6248 if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
6249 got_type = GOT_TLS_IE;
6252 if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
6253 && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
6254 (*_bfd_error_handler)
6255 (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6256 abfd, h->root.root.string);
6257 else if (old_got_type == GOT_FUNCDESC
6258 || got_type == GOT_FUNCDESC)
6259 (*_bfd_error_handler)
6260 (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6261 abfd, h->root.root.string);
6263 (*_bfd_error_handler)
6264 (_("%B: `%s' accessed both as normal and thread local symbol"),
6265 abfd, h->root.root.string);
6270 if (old_got_type != got_type)
6273 sh_elf_hash_entry (h)->got_type = got_type;
6275 sh_elf_local_got_type (abfd) [r_symndx] = got_type;
6280 case R_SH_TLS_LD_32:
6281 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6285 case R_SH_GOTOFFFUNCDESC:
6286 case R_SH_GOTOFFFUNCDESC20:
6289 (*_bfd_error_handler)
6290 (_("%B: Function descriptor relocation with non-zero addend"),
6297 union gotref *local_funcdesc;
6299 /* We need a function descriptor for a local symbol. */
6300 local_funcdesc = sh_elf_local_funcdesc (abfd);
6301 if (local_funcdesc == NULL)
6305 size = symtab_hdr->sh_info * sizeof (union gotref);
6306 #ifdef INCLUDE_SHMEDIA
6307 /* Count datalabel local GOT. */
6310 local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
6311 if (local_funcdesc == NULL)
6313 sh_elf_local_funcdesc (abfd) = local_funcdesc;
6315 local_funcdesc[r_symndx].refcount += 1;
6317 if (r_type == R_SH_FUNCDESC)
6320 htab->srofixup->size += 4;
6322 htab->srelgot->size += sizeof (Elf32_External_Rela);
6327 sh_elf_hash_entry (h)->funcdesc.refcount++;
6328 if (r_type == R_SH_FUNCDESC)
6329 sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
6331 /* If there is a function descriptor reference, then
6332 there should not be any non-FDPIC references. */
6333 old_got_type = sh_elf_hash_entry (h)->got_type;
6334 if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
6336 if (old_got_type == GOT_NORMAL)
6337 (*_bfd_error_handler)
6338 (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6339 abfd, h->root.root.string);
6341 (*_bfd_error_handler)
6342 (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6343 abfd, h->root.root.string);
6349 #ifdef INCLUDE_SHMEDIA
6350 case R_SH_GOTPLT_LOW16:
6351 case R_SH_GOTPLT_MEDLOW16:
6352 case R_SH_GOTPLT_MEDHI16:
6353 case R_SH_GOTPLT_HI16:
6354 case R_SH_GOTPLT10BY4:
6355 case R_SH_GOTPLT10BY8:
6357 /* If this is a local symbol, we resolve it directly without
6358 creating a procedure linkage table entry. */
6364 || h->dynindx == -1)
6368 h->plt.refcount += 1;
6369 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6374 #ifdef INCLUDE_SHMEDIA
6375 case R_SH_PLT_LOW16:
6376 case R_SH_PLT_MEDLOW16:
6377 case R_SH_PLT_MEDHI16:
6380 /* This symbol requires a procedure linkage table entry. We
6381 actually build the entry in adjust_dynamic_symbol,
6382 because this might be a case of linking PIC code which is
6383 never referenced by a dynamic object, in which case we
6384 don't need to generate a procedure linkage table entry
6387 /* If this is a local symbol, we resolve it directly without
6388 creating a procedure linkage table entry. */
6392 if (h->forced_local)
6396 h->plt.refcount += 1;
6401 #ifdef INCLUDE_SHMEDIA
6402 case R_SH_IMM_LOW16_PCREL:
6403 case R_SH_IMM_MEDLOW16_PCREL:
6404 case R_SH_IMM_MEDHI16_PCREL:
6405 case R_SH_IMM_HI16_PCREL:
6407 if (h != NULL && ! info->shared)
6410 h->plt.refcount += 1;
6413 /* If we are creating a shared library, and this is a reloc
6414 against a global symbol, or a non PC relative reloc
6415 against a local symbol, then we need to copy the reloc
6416 into the shared library. However, if we are linking with
6417 -Bsymbolic, we do not need to copy a reloc against a
6418 global symbol which is defined in an object we are
6419 including in the link (i.e., DEF_REGULAR is set). At
6420 this point we have not seen all the input files, so it is
6421 possible that DEF_REGULAR is not set now but will be set
6422 later (it is never cleared). We account for that
6423 possibility below by storing information in the
6424 dyn_relocs field of the hash table entry. A similar
6425 situation occurs when creating shared libraries and symbol
6426 visibility changes render the symbol local.
6428 If on the other hand, we are creating an executable, we
6429 may need to keep relocations for symbols satisfied by a
6430 dynamic library if we manage to avoid copy relocs for the
6433 && (sec->flags & SEC_ALLOC) != 0
6434 && (r_type != R_SH_REL32
6436 && (! info->symbolic
6437 || h->root.type == bfd_link_hash_defweak
6438 || !h->def_regular))))
6440 && (sec->flags & SEC_ALLOC) != 0
6442 && (h->root.type == bfd_link_hash_defweak
6443 || !h->def_regular)))
6445 struct elf_sh_dyn_relocs *p;
6446 struct elf_sh_dyn_relocs **head;
6448 if (htab->root.dynobj == NULL)
6449 htab->root.dynobj = abfd;
6451 /* When creating a shared object, we must copy these
6452 reloc types into the output file. We create a reloc
6453 section in dynobj and make room for this reloc. */
6456 sreloc = _bfd_elf_make_dynamic_reloc_section
6457 (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE);
6463 /* If this is a global symbol, we count the number of
6464 relocations we need for this symbol. */
6466 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6469 /* Track dynamic relocs needed for local syms too. */
6472 Elf_Internal_Sym *isym;
6474 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6479 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6483 vpp = &elf_section_data (s)->local_dynrel;
6484 head = (struct elf_sh_dyn_relocs **) vpp;
6488 if (p == NULL || p->sec != sec)
6490 bfd_size_type amt = sizeof (*p);
6491 p = bfd_alloc (htab->root.dynobj, amt);
6502 if (r_type == R_SH_REL32
6503 #ifdef INCLUDE_SHMEDIA
6504 || r_type == R_SH_IMM_LOW16_PCREL
6505 || r_type == R_SH_IMM_MEDLOW16_PCREL
6506 || r_type == R_SH_IMM_MEDHI16_PCREL
6507 || r_type == R_SH_IMM_HI16_PCREL
6513 /* Allocate the fixup regardless of whether we need a relocation.
6514 If we end up generating the relocation, we'll unallocate the
6516 if (htab->fdpic_p && !info->shared
6517 && r_type == R_SH_DIR32
6518 && (sec->flags & SEC_ALLOC) != 0)
6519 htab->srofixup->size += 4;
6522 case R_SH_TLS_LE_32:
6523 if (info->shared && !info->pie)
6525 (*_bfd_error_handler)
6526 (_("%B: TLS local exec code cannot be linked into shared objects"),
6533 case R_SH_TLS_LDO_32:
6534 /* Nothing to do. */
6545 #ifndef sh_elf_set_mach_from_flags
6546 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
6549 sh_elf_set_mach_from_flags (bfd *abfd)
6551 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
6553 if (flags >= sizeof(sh_ef_bfd_table))
6556 if (sh_ef_bfd_table[flags] == 0)
6559 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
6565 /* Reverse table lookup for sh_ef_bfd_table[].
6566 Given a bfd MACH value from archures.c
6567 return the equivalent ELF flags from the table.
6568 Return -1 if no match is found. */
6571 sh_elf_get_flags_from_mach (unsigned long mach)
6573 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
6576 if (sh_ef_bfd_table[i] == mach)
6579 /* shouldn't get here */
6584 #endif /* not sh_elf_set_mach_from_flags */
6586 #ifndef sh_elf_set_private_flags
6587 /* Function to keep SH specific file flags. */
6590 sh_elf_set_private_flags (bfd *abfd, flagword flags)
6592 BFD_ASSERT (! elf_flags_init (abfd)
6593 || elf_elfheader (abfd)->e_flags == flags);
6595 elf_elfheader (abfd)->e_flags = flags;
6596 elf_flags_init (abfd) = TRUE;
6597 return sh_elf_set_mach_from_flags (abfd);
6599 #endif /* not sh_elf_set_private_flags */
6601 #ifndef sh_elf_copy_private_data
6602 /* Copy backend specific data from one object module to another */
6605 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
6607 /* Copy object attributes. */
6608 _bfd_elf_copy_obj_attributes (ibfd, obfd);
6610 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6613 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
6615 #endif /* not sh_elf_copy_private_data */
6617 #ifndef sh_elf_merge_private_data
6619 /* This function returns the ELF architecture number that
6620 corresponds to the given arch_sh* flags. */
6623 sh_find_elf_flags (unsigned int arch_set)
6625 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
6626 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
6628 return sh_elf_get_flags_from_mach (bfd_mach);
6631 /* This routine initialises the elf flags when required and
6632 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
6635 sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
6637 extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *);
6639 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6642 if (! elf_flags_init (obfd))
6644 /* This happens when ld starts out with a 'blank' output file. */
6645 elf_flags_init (obfd) = TRUE;
6646 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6647 sh_elf_set_mach_from_flags (obfd);
6648 if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
6649 elf_elfheader (obfd)->e_flags |= EF_SH_PIC;
6652 if (! sh_merge_bfd_arch (ibfd, obfd))
6654 _bfd_error_handler ("%B: uses instructions which are incompatible "
6655 "with instructions used in previous modules",
6657 bfd_set_error (bfd_error_bad_value);
6661 elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
6662 elf_elfheader (obfd)->e_flags |=
6663 sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
6665 if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
6667 _bfd_error_handler ("%B: attempt to mix FDPIC and non-FDPIC objects",
6669 bfd_set_error (bfd_error_bad_value);
6675 #endif /* not sh_elf_merge_private_data */
6677 /* Override the generic function because we need to store sh_elf_obj_tdata
6678 as the specific tdata. We set also the machine architecture from flags
6682 sh_elf_object_p (bfd *abfd)
6684 if (! sh_elf_set_mach_from_flags (abfd))
6687 return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
6688 == fdpic_object_p (abfd));
6691 /* Finish up dynamic symbol handling. We set the contents of various
6692 dynamic sections here. */
6695 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6696 struct elf_link_hash_entry *h,
6697 Elf_Internal_Sym *sym)
6699 struct elf_sh_link_hash_table *htab;
6701 htab = sh_elf_hash_table (info);
6705 if (h->plt.offset != (bfd_vma) -1)
6713 Elf_Internal_Rela rel;
6715 const struct elf_sh_plt_info *plt_info;
6717 /* This symbol has an entry in the procedure linkage table. Set
6720 BFD_ASSERT (h->dynindx != -1);
6723 sgotplt = htab->sgotplt;
6724 srelplt = htab->srelplt;
6725 BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
6727 /* Get the index in the procedure linkage table which
6728 corresponds to this symbol. This is the index of this symbol
6729 in all the symbols for which we are making plt entries. The
6730 first entry in the procedure linkage table is reserved. */
6731 plt_index = get_plt_index (htab->plt_info, h->plt.offset);
6733 plt_info = htab->plt_info;
6734 if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
6735 plt_info = plt_info->short_plt;
6737 /* Get the offset into the .got table of the entry that
6738 corresponds to this function. */
6740 /* The offset must be relative to the GOT symbol, twelve bytes
6741 before the end of .got.plt. Each descriptor is eight
6743 got_offset = plt_index * 8 + 12 - sgotplt->size;
6745 /* Each .got entry is 4 bytes. The first three are
6747 got_offset = (plt_index + 3) * 4;
6751 got_offset -= GOT_BIAS;
6754 /* Fill in the entry in the procedure linkage table. */
6755 memcpy (splt->contents + h->plt.offset,
6756 plt_info->symbol_entry,
6757 plt_info->symbol_entry_size);
6759 if (info->shared || htab->fdpic_p)
6761 if (plt_info->symbol_fields.got20)
6763 bfd_reloc_status_type r;
6764 r = install_movi20_field (output_bfd, got_offset,
6765 splt->owner, splt, splt->contents,
6767 + plt_info->symbol_fields.got_entry);
6768 BFD_ASSERT (r == bfd_reloc_ok);
6771 install_plt_field (output_bfd, FALSE, got_offset,
6774 + plt_info->symbol_fields.got_entry));
6778 BFD_ASSERT (!plt_info->symbol_fields.got20);
6780 install_plt_field (output_bfd, FALSE,
6781 (sgotplt->output_section->vma
6782 + sgotplt->output_offset
6786 + plt_info->symbol_fields.got_entry));
6787 if (htab->vxworks_p)
6789 unsigned int reachable_plts, plts_per_4k;
6792 /* Divide the PLT into groups. The first group contains
6793 REACHABLE_PLTS entries and the other groups contain
6794 PLTS_PER_4K entries. Entries in the first group can
6795 branch directly to .plt; those in later groups branch
6796 to the last element of the previous group. */
6797 /* ??? It would be better to create multiple copies of
6798 the common resolver stub. */
6799 reachable_plts = ((4096
6800 - plt_info->plt0_entry_size
6801 - (plt_info->symbol_fields.plt + 4))
6802 / plt_info->symbol_entry_size) + 1;
6803 plts_per_4k = (4096 / plt_info->symbol_entry_size);
6804 if (plt_index < reachable_plts)
6805 distance = -(h->plt.offset
6806 + plt_info->symbol_fields.plt);
6808 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
6809 * plt_info->symbol_entry_size);
6811 /* Install the 'bra' with this offset. */
6812 bfd_put_16 (output_bfd,
6813 0xa000 | (0x0fff & ((distance - 4) / 2)),
6816 + plt_info->symbol_fields.plt));
6819 install_plt_field (output_bfd, TRUE,
6820 splt->output_section->vma + splt->output_offset,
6823 + plt_info->symbol_fields.plt));
6826 /* Make got_offset relative to the start of .got.plt. */
6829 got_offset += GOT_BIAS;
6832 got_offset = plt_index * 8;
6834 if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
6835 install_plt_field (output_bfd, FALSE,
6836 plt_index * sizeof (Elf32_External_Rela),
6839 + plt_info->symbol_fields.reloc_offset));
6841 /* Fill in the entry in the global offset table. */
6842 bfd_put_32 (output_bfd,
6843 (splt->output_section->vma
6844 + splt->output_offset
6846 + plt_info->symbol_resolve_offset),
6847 sgotplt->contents + got_offset);
6849 bfd_put_32 (output_bfd,
6850 sh_elf_osec_to_segment (output_bfd,
6851 htab->splt->output_section),
6852 sgotplt->contents + got_offset + 4);
6854 /* Fill in the entry in the .rela.plt section. */
6855 rel.r_offset = (sgotplt->output_section->vma
6856 + sgotplt->output_offset
6859 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
6861 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6864 rel.r_addend = GOT_BIAS;
6866 loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
6867 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6869 if (htab->vxworks_p && !info->shared)
6871 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6872 Begin by pointing LOC to the first such relocation. */
6873 loc = (htab->srelplt2->contents
6874 + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
6876 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6877 for the PLT entry's pointer to the .got.plt entry. */
6878 rel.r_offset = (htab->splt->output_section->vma
6879 + htab->splt->output_offset
6881 + plt_info->symbol_fields.got_entry);
6882 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6883 rel.r_addend = got_offset;
6884 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6885 loc += sizeof (Elf32_External_Rela);
6887 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6888 the .got.plt entry, which initially points to .plt. */
6889 rel.r_offset = (sgotplt->output_section->vma
6890 + sgotplt->output_offset
6892 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
6894 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6897 if (!h->def_regular)
6899 /* Mark the symbol as undefined, rather than as defined in
6900 the .plt section. Leave the value alone. */
6901 sym->st_shndx = SHN_UNDEF;
6905 if (h->got.offset != (bfd_vma) -1
6906 && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
6907 && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
6908 && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
6912 Elf_Internal_Rela rel;
6915 /* This symbol has an entry in the global offset table. Set it
6919 srelgot = htab->srelgot;
6920 BFD_ASSERT (sgot != NULL && srelgot != NULL);
6922 rel.r_offset = (sgot->output_section->vma
6923 + sgot->output_offset
6924 + (h->got.offset &~ (bfd_vma) 1));
6926 /* If this is a static link, or it is a -Bsymbolic link and the
6927 symbol is defined locally or was forced to be local because
6928 of a version file, we just want to emit a RELATIVE reloc.
6929 The entry in the global offset table will already have been
6930 initialized in the relocate_section function. */
6932 && SYMBOL_REFERENCES_LOCAL (info, h))
6936 asection *sec = h->root.u.def.section;
6938 = elf_section_data (sec->output_section)->dynindx;
6940 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6941 rel.r_addend = (h->root.u.def.value
6942 + h->root.u.def.section->output_offset);
6946 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6947 rel.r_addend = (h->root.u.def.value
6948 + h->root.u.def.section->output_section->vma
6949 + h->root.u.def.section->output_offset);
6954 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6955 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6959 loc = srelgot->contents;
6960 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6961 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6964 #ifdef INCLUDE_SHMEDIA
6966 struct elf_sh_link_hash_entry *eh;
6968 eh = (struct elf_sh_link_hash_entry *) h;
6969 if (eh->datalabel_got.offset != (bfd_vma) -1)
6973 Elf_Internal_Rela rel;
6976 /* This symbol has a datalabel entry in the global offset table.
6980 srelgot = htab->srelgot;
6981 BFD_ASSERT (sgot != NULL && srelgot != NULL);
6983 rel.r_offset = (sgot->output_section->vma
6984 + sgot->output_offset
6985 + (eh->datalabel_got.offset &~ (bfd_vma) 1));
6987 /* If this is a static link, or it is a -Bsymbolic link and the
6988 symbol is defined locally or was forced to be local because
6989 of a version file, we just want to emit a RELATIVE reloc.
6990 The entry in the global offset table will already have been
6991 initialized in the relocate_section function. */
6993 && SYMBOL_REFERENCES_LOCAL (info, h))
6997 asection *sec = h->root.u.def.section;
6999 = elf_section_data (sec->output_section)->dynindx;
7001 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
7002 rel.r_addend = (h->root.u.def.value
7003 + h->root.u.def.section->output_offset);
7007 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
7008 rel.r_addend = (h->root.u.def.value
7009 + h->root.u.def.section->output_section->vma
7010 + h->root.u.def.section->output_offset);
7015 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
7016 + eh->datalabel_got.offset);
7017 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
7021 loc = srelgot->contents;
7022 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
7023 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7031 Elf_Internal_Rela rel;
7034 /* This symbol needs a copy reloc. Set it up. */
7036 BFD_ASSERT (h->dynindx != -1
7037 && (h->root.type == bfd_link_hash_defined
7038 || h->root.type == bfd_link_hash_defweak));
7040 s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
7041 BFD_ASSERT (s != NULL);
7043 rel.r_offset = (h->root.u.def.value
7044 + h->root.u.def.section->output_section->vma
7045 + h->root.u.def.section->output_offset);
7046 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
7048 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
7049 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7052 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
7053 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
7055 if (h == htab->root.hdynamic
7056 || (!htab->vxworks_p && h == htab->root.hgot))
7057 sym->st_shndx = SHN_ABS;
7062 /* Finish up the dynamic sections. */
7065 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
7067 struct elf_sh_link_hash_table *htab;
7071 htab = sh_elf_hash_table (info);
7075 sgotplt = htab->sgotplt;
7076 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
7078 if (htab->root.dynamic_sections_created)
7081 Elf32_External_Dyn *dyncon, *dynconend;
7083 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
7085 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7086 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
7087 for (; dyncon < dynconend; dyncon++)
7089 Elf_Internal_Dyn dyn;
7091 #ifdef INCLUDE_SHMEDIA
7095 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
7101 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
7102 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7105 #ifdef INCLUDE_SHMEDIA
7107 name = info->init_function;
7111 name = info->fini_function;
7113 if (dyn.d_un.d_val != 0)
7115 struct elf_link_hash_entry *h;
7117 h = elf_link_hash_lookup (&htab->root, name,
7118 FALSE, FALSE, TRUE);
7119 if (h != NULL && (h->other & STO_SH5_ISA32))
7121 dyn.d_un.d_val |= 1;
7122 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7129 BFD_ASSERT (htab->root.hgot != NULL);
7130 s = htab->root.hgot->root.u.def.section;
7131 dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
7132 + s->output_section->vma + s->output_offset;
7133 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7137 s = htab->srelplt->output_section;
7138 BFD_ASSERT (s != NULL);
7139 dyn.d_un.d_ptr = s->vma;
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_val = s->size;
7147 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7151 /* My reading of the SVR4 ABI indicates that the
7152 procedure linkage table relocs (DT_JMPREL) should be
7153 included in the overall relocs (DT_RELA). This is
7154 what Solaris does. However, UnixWare can not handle
7155 that case. Therefore, we override the DT_RELASZ entry
7156 here to make it not include the JMPREL relocs. Since
7157 the linker script arranges for .rela.plt to follow all
7158 other relocation sections, we don't have to worry
7159 about changing the DT_RELA entry. */
7160 if (htab->srelplt != NULL)
7162 s = htab->srelplt->output_section;
7163 dyn.d_un.d_val -= s->size;
7165 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7170 /* Fill in the first entry in the procedure linkage table. */
7172 if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
7176 memcpy (splt->contents,
7177 htab->plt_info->plt0_entry,
7178 htab->plt_info->plt0_entry_size);
7179 for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
7180 if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
7181 install_plt_field (output_bfd, FALSE,
7182 (sgotplt->output_section->vma
7183 + sgotplt->output_offset
7186 + htab->plt_info->plt0_got_fields[i]));
7188 if (htab->vxworks_p)
7190 /* Finalize the .rela.plt.unloaded contents. */
7191 Elf_Internal_Rela rel;
7194 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
7195 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
7196 loc = htab->srelplt2->contents;
7197 rel.r_offset = (splt->output_section->vma
7198 + splt->output_offset
7199 + htab->plt_info->plt0_got_fields[2]);
7200 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
7202 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7203 loc += sizeof (Elf32_External_Rela);
7205 /* Fix up the remaining .rela.plt.unloaded relocations.
7206 They may have the wrong symbol index for _G_O_T_ or
7207 _P_L_T_ depending on the order in which symbols were
7209 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
7211 /* The PLT entry's pointer to the .got.plt slot. */
7212 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7213 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
7215 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7216 loc += sizeof (Elf32_External_Rela);
7218 /* The .got.plt slot's pointer to .plt. */
7219 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7220 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
7222 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7223 loc += sizeof (Elf32_External_Rela);
7227 /* UnixWare sets the entsize of .plt to 4, although that doesn't
7228 really seem like the right value. */
7229 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7233 /* Fill in the first three entries in the global offset table. */
7234 if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
7237 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
7239 bfd_put_32 (output_bfd,
7240 sdyn->output_section->vma + sdyn->output_offset,
7242 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
7243 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
7246 if (sgotplt && sgotplt->size > 0)
7247 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
7249 /* At the very end of the .rofixup section is a pointer to the GOT. */
7250 if (htab->fdpic_p && htab->srofixup != NULL)
7252 struct elf_link_hash_entry *hgot = htab->root.hgot;
7253 bfd_vma got_value = hgot->root.u.def.value
7254 + hgot->root.u.def.section->output_section->vma
7255 + hgot->root.u.def.section->output_offset;
7257 sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
7259 /* Make sure we allocated and generated the same number of fixups. */
7260 BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
7263 if (htab->srelfuncdesc)
7264 BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
7265 == htab->srelfuncdesc->size);
7268 BFD_ASSERT (htab->srelgot->reloc_count * sizeof (Elf32_External_Rela)
7269 == htab->srelgot->size);
7274 static enum elf_reloc_type_class
7275 sh_elf_reloc_type_class (const Elf_Internal_Rela *rela)
7277 switch ((int) ELF32_R_TYPE (rela->r_info))
7280 return reloc_class_relative;
7282 return reloc_class_plt;
7284 return reloc_class_copy;
7286 return reloc_class_normal;
7290 #if !defined SH_TARGET_ALREADY_DEFINED
7291 /* Support for Linux core dump NOTE sections. */
7294 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7299 switch (note->descsz)
7304 case 168: /* Linux/SH */
7306 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
7309 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 24);
7318 /* Make a ".reg/999" section. */
7319 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7320 size, note->descpos + offset);
7324 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7326 switch (note->descsz)
7331 case 124: /* Linux/SH elf_prpsinfo */
7332 elf_tdata (abfd)->core_program
7333 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7334 elf_tdata (abfd)->core_command
7335 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7338 /* Note that for some reason, a spurious space is tacked
7339 onto the end of the args in some (at least one anyway)
7340 implementations, so strip it off if it exists. */
7343 char *command = elf_tdata (abfd)->core_command;
7344 int n = strlen (command);
7346 if (0 < n && command[n - 1] == ' ')
7347 command[n - 1] = '\0';
7352 #endif /* not SH_TARGET_ALREADY_DEFINED */
7355 /* Return address for Ith PLT stub in section PLT, for relocation REL
7356 or (bfd_vma) -1 if it should not be included. */
7359 sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
7360 const arelent *rel ATTRIBUTE_UNUSED)
7362 const struct elf_sh_plt_info *plt_info;
7364 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
7365 return plt->vma + get_plt_offset (plt_info, i);
7368 /* Decide whether to attempt to turn absptr or lsda encodings in
7369 shared libraries into pcrel within the given input section. */
7372 sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
7373 struct bfd_link_info *info,
7374 asection *eh_frame_section ATTRIBUTE_UNUSED)
7376 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7378 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
7385 /* Adjust the contents of an eh_frame_hdr section before they're output. */
7388 sh_elf_encode_eh_address (bfd *abfd,
7389 struct bfd_link_info *info,
7390 asection *osec, bfd_vma offset,
7391 asection *loc_sec, bfd_vma loc_offset,
7394 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7395 struct elf_link_hash_entry *h;
7398 return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
7399 loc_offset, encoded);
7401 h = htab->root.hgot;
7402 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
7404 if (! h || (sh_elf_osec_to_segment (abfd, osec)
7405 == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
7406 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
7407 loc_sec, loc_offset, encoded);
7409 BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
7410 == (sh_elf_osec_to_segment
7411 (abfd, h->root.u.def.section->output_section)));
7413 *encoded = osec->vma + offset
7414 - (h->root.u.def.value
7415 + h->root.u.def.section->output_section->vma
7416 + h->root.u.def.section->output_offset);
7418 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
7421 #if !defined SH_TARGET_ALREADY_DEFINED
7422 #define TARGET_BIG_SYM bfd_elf32_sh_vec
7423 #define TARGET_BIG_NAME "elf32-sh"
7424 #define TARGET_LITTLE_SYM bfd_elf32_shl_vec
7425 #define TARGET_LITTLE_NAME "elf32-shl"
7428 #define ELF_ARCH bfd_arch_sh
7429 #define ELF_TARGET_ID SH_ELF_DATA
7430 #define ELF_MACHINE_CODE EM_SH
7431 #ifdef __QNXTARGET__
7432 #define ELF_MAXPAGESIZE 0x1000
7434 #define ELF_MAXPAGESIZE 0x80
7437 #define elf_symbol_leading_char '_'
7439 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7440 #define bfd_elf32_bfd_reloc_name_lookup \
7441 sh_elf_reloc_name_lookup
7442 #define elf_info_to_howto sh_elf_info_to_howto
7443 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
7444 #define elf_backend_relocate_section sh_elf_relocate_section
7445 #define bfd_elf32_bfd_get_relocated_section_contents \
7446 sh_elf_get_relocated_section_contents
7447 #define bfd_elf32_mkobject sh_elf_mkobject
7448 #define elf_backend_object_p sh_elf_object_p
7449 #define bfd_elf32_bfd_set_private_bfd_flags \
7450 sh_elf_set_private_flags
7451 #define bfd_elf32_bfd_copy_private_bfd_data \
7452 sh_elf_copy_private_data
7453 #define bfd_elf32_bfd_merge_private_bfd_data \
7454 sh_elf_merge_private_data
7456 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
7457 #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
7458 #define elf_backend_check_relocs sh_elf_check_relocs
7459 #define elf_backend_copy_indirect_symbol \
7460 sh_elf_copy_indirect_symbol
7461 #define elf_backend_create_dynamic_sections \
7462 sh_elf_create_dynamic_sections
7463 #define bfd_elf32_bfd_link_hash_table_create \
7464 sh_elf_link_hash_table_create
7465 #define elf_backend_adjust_dynamic_symbol \
7466 sh_elf_adjust_dynamic_symbol
7467 #define elf_backend_always_size_sections \
7468 sh_elf_always_size_sections
7469 #define elf_backend_size_dynamic_sections \
7470 sh_elf_size_dynamic_sections
7471 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
7472 #define elf_backend_finish_dynamic_symbol \
7473 sh_elf_finish_dynamic_symbol
7474 #define elf_backend_finish_dynamic_sections \
7475 sh_elf_finish_dynamic_sections
7476 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
7477 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
7478 #define elf_backend_can_make_relative_eh_frame \
7479 sh_elf_use_relative_eh_frame
7480 #define elf_backend_can_make_lsda_relative_eh_frame \
7481 sh_elf_use_relative_eh_frame
7482 #define elf_backend_encode_eh_address \
7483 sh_elf_encode_eh_address
7485 #define elf_backend_stack_align 8
7486 #define elf_backend_can_gc_sections 1
7487 #define elf_backend_can_refcount 1
7488 #define elf_backend_want_got_plt 1
7489 #define elf_backend_plt_readonly 1
7490 #define elf_backend_want_plt_sym 0
7491 #define elf_backend_got_header_size 12
7493 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
7495 #include "elf32-target.h"
7497 /* NetBSD support. */
7498 #undef TARGET_BIG_SYM
7499 #define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
7500 #undef TARGET_BIG_NAME
7501 #define TARGET_BIG_NAME "elf32-sh-nbsd"
7502 #undef TARGET_LITTLE_SYM
7503 #define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
7504 #undef TARGET_LITTLE_NAME
7505 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
7506 #undef ELF_MAXPAGESIZE
7507 #define ELF_MAXPAGESIZE 0x10000
7508 #undef ELF_COMMONPAGESIZE
7509 #undef elf_symbol_leading_char
7510 #define elf_symbol_leading_char 0
7512 #define elf32_bed elf32_sh_nbsd_bed
7514 #include "elf32-target.h"
7517 /* Linux support. */
7518 #undef TARGET_BIG_SYM
7519 #define TARGET_BIG_SYM bfd_elf32_shblin_vec
7520 #undef TARGET_BIG_NAME
7521 #define TARGET_BIG_NAME "elf32-shbig-linux"
7522 #undef TARGET_LITTLE_SYM
7523 #define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
7524 #undef TARGET_LITTLE_NAME
7525 #define TARGET_LITTLE_NAME "elf32-sh-linux"
7526 #undef ELF_COMMONPAGESIZE
7527 #define ELF_COMMONPAGESIZE 0x1000
7529 #undef elf_backend_grok_prstatus
7530 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
7531 #undef elf_backend_grok_psinfo
7532 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
7534 #define elf32_bed elf32_sh_lin_bed
7536 #include "elf32-target.h"
7539 /* FDPIC support. */
7540 #undef TARGET_BIG_SYM
7541 #define TARGET_BIG_SYM bfd_elf32_shbfd_vec
7542 #undef TARGET_BIG_NAME
7543 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
7544 #undef TARGET_LITTLE_SYM
7545 #define TARGET_LITTLE_SYM bfd_elf32_shfd_vec
7546 #undef TARGET_LITTLE_NAME
7547 #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
7550 #define elf32_bed elf32_sh_fd_bed
7552 #include "elf32-target.h"
7554 #undef elf_backend_modify_program_headers
7556 /* VxWorks support. */
7557 #undef TARGET_BIG_SYM
7558 #define TARGET_BIG_SYM bfd_elf32_shvxworks_vec
7559 #undef TARGET_BIG_NAME
7560 #define TARGET_BIG_NAME "elf32-sh-vxworks"
7561 #undef TARGET_LITTLE_SYM
7562 #define TARGET_LITTLE_SYM bfd_elf32_shlvxworks_vec
7563 #undef TARGET_LITTLE_NAME
7564 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
7566 #define elf32_bed elf32_sh_vxworks_bed
7568 #undef elf_backend_want_plt_sym
7569 #define elf_backend_want_plt_sym 1
7570 #undef elf_symbol_leading_char
7571 #define elf_symbol_leading_char '_'
7572 #define elf_backend_want_got_underscore 1
7573 #undef elf_backend_grok_prstatus
7574 #undef elf_backend_grok_psinfo
7575 #undef elf_backend_add_symbol_hook
7576 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
7577 #undef elf_backend_link_output_symbol_hook
7578 #define elf_backend_link_output_symbol_hook \
7579 elf_vxworks_link_output_symbol_hook
7580 #undef elf_backend_emit_relocs
7581 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
7582 #undef elf_backend_final_write_processing
7583 #define elf_backend_final_write_processing \
7584 elf_vxworks_final_write_processing
7585 #undef ELF_MAXPAGESIZE
7586 #define ELF_MAXPAGESIZE 0x1000
7587 #undef ELF_COMMONPAGESIZE
7589 #include "elf32-target.h"
7591 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */