1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 #include "elf-vxworks.h"
30 #include "libiberty.h"
31 #include "../opcodes/sh-opc.h"
33 static bfd_reloc_status_type sh_elf_reloc
34 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
35 static bfd_reloc_status_type sh_elf_ignore_reloc
36 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
37 static bfd_boolean sh_elf_relax_delete_bytes
38 (bfd *, asection *, bfd_vma, int);
39 static bfd_boolean sh_elf_align_loads
40 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
42 static bfd_boolean sh_elf_swap_insns
43 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
45 static int sh_elf_optimized_tls_reloc
46 (struct bfd_link_info *, int, int);
47 static bfd_vma dtpoff_base
48 (struct bfd_link_info *);
50 (struct bfd_link_info *, bfd_vma);
52 /* The name of the dynamic interpreter. This is put in the .interp
55 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
57 /* FDPIC binaries have a default 128K stack. */
58 #define DEFAULT_STACK_SIZE 0x20000
60 #define MINUS_ONE ((bfd_vma) 0 - 1)
62 /* Decide whether a reference to a symbol can be resolved locally or
63 not. If the symbol is protected, we want the local address, but
64 its function descriptor must be assigned by the dynamic linker. */
65 #define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
66 (SYMBOL_REFERENCES_LOCAL (INFO, H) \
67 || ! elf_hash_table (INFO)->dynamic_sections_created)
69 #define SH_PARTIAL32 TRUE
70 #define SH_SRC_MASK32 0xffffffff
71 #define SH_ELF_RELOC sh_elf_reloc
72 static reloc_howto_type sh_elf_howto_table[] =
74 #include "elf32-sh-relocs.h"
77 #define SH_PARTIAL32 FALSE
78 #define SH_SRC_MASK32 0
79 #define SH_ELF_RELOC bfd_elf_generic_reloc
80 static reloc_howto_type sh_vxworks_howto_table[] =
82 #include "elf32-sh-relocs.h"
85 /* Return true if OUTPUT_BFD is a VxWorks object. */
88 vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
90 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
91 extern const bfd_target sh_elf32_vxworks_le_vec;
92 extern const bfd_target sh_elf32_vxworks_vec;
94 return (abfd->xvec == &sh_elf32_vxworks_le_vec
95 || abfd->xvec == &sh_elf32_vxworks_vec);
101 /* Return true if OUTPUT_BFD is an FDPIC object. */
104 fdpic_object_p (bfd *abfd ATTRIBUTE_UNUSED)
106 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
107 extern const bfd_target sh_elf32_fdpic_le_vec;
108 extern const bfd_target sh_elf32_fdpic_be_vec;
110 return (abfd->xvec == &sh_elf32_fdpic_le_vec
111 || abfd->xvec == &sh_elf32_fdpic_be_vec);
117 /* Return the howto table for ABFD. */
119 static reloc_howto_type *
120 get_howto_table (bfd *abfd)
122 if (vxworks_object_p (abfd))
123 return sh_vxworks_howto_table;
124 return sh_elf_howto_table;
127 static bfd_reloc_status_type
128 sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
129 asection *input_section, bfd_byte *contents,
130 bfd_vma addr, asection *symbol_section,
131 bfd_vma start, bfd_vma end)
133 static bfd_vma last_addr;
134 static asection *last_symbol_section;
135 bfd_byte *start_ptr, *ptr, *last_ptr;
140 /* Sanity check the address. */
141 if (addr > bfd_get_section_limit (input_bfd, input_section))
142 return bfd_reloc_outofrange;
144 /* We require the start and end relocations to be processed consecutively -
145 although we allow then to be processed forwards or backwards. */
149 last_symbol_section = symbol_section;
152 if (last_addr != addr)
156 if (! symbol_section || last_symbol_section != symbol_section || end < start)
157 return bfd_reloc_outofrange;
159 /* Get the symbol_section contents. */
160 if (symbol_section != input_section)
162 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
163 contents = elf_section_data (symbol_section)->this_hdr.contents;
166 if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
169 if (contents != NULL)
171 return bfd_reloc_outofrange;
175 #define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
176 start_ptr = contents + start;
177 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
179 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
182 diff = (last_ptr - ptr) >> 1;
183 cum_diff += diff & 1;
186 /* Calculate the start / end values to load into rs / re minus four -
187 so that will cancel out the four we would otherwise have to add to
188 addr to get the value to subtract in order to get relative addressing. */
192 end = (ptr + cum_diff * 2) - contents;
196 bfd_vma start0 = start - 4;
198 while (start0 && IS_PPI (contents + start0))
200 start0 = start - 2 - ((start - start0) & 2);
201 start = start0 - cum_diff - 2;
206 && elf_section_data (symbol_section)->this_hdr.contents != contents)
209 insn = bfd_get_16 (input_bfd, contents + addr);
211 x = (insn & 0x200 ? end : start) - addr;
212 if (input_section != symbol_section)
213 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
214 - (input_section->output_section->vma
215 + input_section->output_offset));
217 if (x < -128 || x > 127)
218 return bfd_reloc_overflow;
220 x = (insn & ~0xff) | (x & 0xff);
221 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
226 /* This function is used for normal relocs. This used to be like the COFF
227 function, and is almost certainly incorrect for other ELF targets. */
229 static bfd_reloc_status_type
230 sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
231 void *data, asection *input_section, bfd *output_bfd,
232 char **error_message ATTRIBUTE_UNUSED)
236 enum elf_sh_reloc_type r_type;
237 bfd_vma addr = reloc_entry->address;
238 bfd_byte *hit_data = addr + (bfd_byte *) data;
240 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
242 if (output_bfd != NULL)
244 /* Partial linking--do nothing. */
245 reloc_entry->address += input_section->output_offset;
249 /* Almost all relocs have to do with relaxing. If any work must be
250 done for them, it has been done in sh_relax_section. */
251 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
254 if (symbol_in != NULL
255 && bfd_is_und_section (symbol_in->section))
256 return bfd_reloc_undefined;
258 /* PR 17512: file: 9891ca98. */
259 if (addr > bfd_get_section_limit (abfd, input_section)
260 - bfd_get_reloc_size (reloc_entry->howto)
261 || bfd_get_reloc_size (reloc_entry->howto)
262 > bfd_get_section_limit (abfd, input_section))
263 return bfd_reloc_outofrange;
265 if (bfd_is_com_section (symbol_in->section))
268 sym_value = (symbol_in->value +
269 symbol_in->section->output_section->vma +
270 symbol_in->section->output_offset);
275 insn = bfd_get_32 (abfd, hit_data);
276 insn += sym_value + reloc_entry->addend;
277 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
280 insn = bfd_get_16 (abfd, hit_data);
281 sym_value += reloc_entry->addend;
282 sym_value -= (input_section->output_section->vma
283 + input_section->output_offset
286 sym_value += (insn & 0xfff) << 1;
289 insn = (insn & 0xf000) | (sym_value & 0xfff);
290 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
291 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
292 return bfd_reloc_overflow;
302 /* This function is used for relocs which are only used for relaxing,
303 which the linker should otherwise ignore. */
305 static bfd_reloc_status_type
306 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
307 asymbol *symbol ATTRIBUTE_UNUSED,
308 void *data ATTRIBUTE_UNUSED, asection *input_section,
310 char **error_message ATTRIBUTE_UNUSED)
312 if (output_bfd != NULL)
313 reloc_entry->address += input_section->output_offset;
317 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
321 bfd_reloc_code_real_type bfd_reloc_val;
322 unsigned char elf_reloc_val;
325 /* An array mapping BFD reloc codes to SH ELF relocs. */
327 static const struct elf_reloc_map sh_reloc_map[] =
329 { BFD_RELOC_NONE, R_SH_NONE },
330 { BFD_RELOC_32, R_SH_DIR32 },
331 { BFD_RELOC_16, R_SH_DIR16 },
332 { BFD_RELOC_8, R_SH_DIR8 },
333 { BFD_RELOC_CTOR, R_SH_DIR32 },
334 { BFD_RELOC_32_PCREL, R_SH_REL32 },
335 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
336 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
337 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
338 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
339 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
340 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
341 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
342 { BFD_RELOC_SH_USES, R_SH_USES },
343 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
344 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
345 { BFD_RELOC_SH_CODE, R_SH_CODE },
346 { BFD_RELOC_SH_DATA, R_SH_DATA },
347 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
348 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
349 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
350 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
351 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
352 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
353 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
354 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
355 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
356 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
357 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
358 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
359 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
360 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
361 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
362 { BFD_RELOC_SH_COPY, R_SH_COPY },
363 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
364 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
365 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
366 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
367 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
368 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
369 { BFD_RELOC_SH_GOT20, R_SH_GOT20 },
370 { BFD_RELOC_SH_GOTOFF20, R_SH_GOTOFF20 },
371 { BFD_RELOC_SH_GOTFUNCDESC, R_SH_GOTFUNCDESC },
372 { BFD_RELOC_SH_GOTFUNCDESC20, R_SH_GOTFUNCDESC20 },
373 { BFD_RELOC_SH_GOTOFFFUNCDESC, R_SH_GOTOFFFUNCDESC },
374 { BFD_RELOC_SH_GOTOFFFUNCDESC20, R_SH_GOTOFFFUNCDESC20 },
375 { BFD_RELOC_SH_FUNCDESC, R_SH_FUNCDESC },
376 #ifdef INCLUDE_SHMEDIA
377 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
378 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
379 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
380 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
381 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
382 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
383 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
384 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
385 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
386 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
387 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
388 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
389 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
390 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
391 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
392 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
393 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
394 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
395 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
396 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
397 { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
398 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
399 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
400 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
401 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
402 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
403 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
404 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
405 { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
406 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
407 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
408 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
409 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
410 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
411 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
412 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
413 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
414 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
415 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
416 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
417 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
418 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
419 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
420 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
421 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
422 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
423 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
424 { BFD_RELOC_64, R_SH_64 },
425 { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
426 #endif /* not INCLUDE_SHMEDIA */
429 /* Given a BFD reloc code, return the howto structure for the
430 corresponding SH ELF reloc. */
432 static reloc_howto_type *
433 sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
437 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
439 if (sh_reloc_map[i].bfd_reloc_val == code)
440 return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
446 static reloc_howto_type *
447 sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
451 if (vxworks_object_p (abfd))
454 i < (sizeof (sh_vxworks_howto_table)
455 / sizeof (sh_vxworks_howto_table[0]));
457 if (sh_vxworks_howto_table[i].name != NULL
458 && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
459 return &sh_vxworks_howto_table[i];
464 i < (sizeof (sh_elf_howto_table)
465 / sizeof (sh_elf_howto_table[0]));
467 if (sh_elf_howto_table[i].name != NULL
468 && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
469 return &sh_elf_howto_table[i];
475 /* Given an ELF reloc, fill in the howto field of a relent. */
478 sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
482 r = ELF32_R_TYPE (dst->r_info);
485 || (r >= R_SH_FIRST_INVALID_RELOC && r <= R_SH_LAST_INVALID_RELOC)
486 || (r >= R_SH_FIRST_INVALID_RELOC_2 && r <= R_SH_LAST_INVALID_RELOC_2)
487 || (r >= R_SH_FIRST_INVALID_RELOC_3 && r <= R_SH_LAST_INVALID_RELOC_3)
488 || (r >= R_SH_FIRST_INVALID_RELOC_4 && r <= R_SH_LAST_INVALID_RELOC_4)
489 || (r >= R_SH_FIRST_INVALID_RELOC_5 && r <= R_SH_LAST_INVALID_RELOC_5)
490 || (r >= R_SH_FIRST_INVALID_RELOC_6 && r <= R_SH_LAST_INVALID_RELOC_6))
492 (*_bfd_error_handler) (_("%A: unrecognised SH reloc number: %d"),
494 bfd_set_error (bfd_error_bad_value);
498 cache_ptr->howto = get_howto_table (abfd) + r;
501 /* This function handles relaxing for SH ELF. See the corresponding
502 function in coff-sh.c for a description of what this does. FIXME:
503 There is a lot of duplication here between this code and the COFF
504 specific code. The format of relocs and symbols is wound deeply
505 into this code, but it would still be better if the duplication
506 could be eliminated somehow. Note in particular that although both
507 functions use symbols like R_SH_CODE, those symbols have different
508 values; in coff-sh.c they come from include/coff/sh.h, whereas here
509 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
512 sh_elf_relax_section (bfd *abfd, asection *sec,
513 struct bfd_link_info *link_info, bfd_boolean *again)
515 Elf_Internal_Shdr *symtab_hdr;
516 Elf_Internal_Rela *internal_relocs;
517 bfd_boolean have_code;
518 Elf_Internal_Rela *irel, *irelend;
519 bfd_byte *contents = NULL;
520 Elf_Internal_Sym *isymbuf = NULL;
524 if (link_info->relocatable
525 || (sec->flags & SEC_RELOC) == 0
526 || sec->reloc_count == 0)
529 #ifdef INCLUDE_SHMEDIA
530 if (elf_section_data (sec)->this_hdr.sh_flags
531 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
537 symtab_hdr = &elf_symtab_hdr (abfd);
539 internal_relocs = (_bfd_elf_link_read_relocs
540 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
541 link_info->keep_memory));
542 if (internal_relocs == NULL)
547 irelend = internal_relocs + sec->reloc_count;
548 for (irel = internal_relocs; irel < irelend; irel++)
550 bfd_vma laddr, paddr, symval;
552 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
555 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
558 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
561 /* Get the section contents. */
562 if (contents == NULL)
564 if (elf_section_data (sec)->this_hdr.contents != NULL)
565 contents = elf_section_data (sec)->this_hdr.contents;
568 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
573 /* The r_addend field of the R_SH_USES reloc will point us to
574 the register load. The 4 is because the r_addend field is
575 computed as though it were a jump offset, which are based
576 from 4 bytes after the jump instruction. */
577 laddr = irel->r_offset + 4 + irel->r_addend;
578 if (laddr >= sec->size)
580 (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
582 (unsigned long) irel->r_offset);
585 insn = bfd_get_16 (abfd, contents + laddr);
587 /* If the instruction is not mov.l NN,rN, we don't know what to
589 if ((insn & 0xf000) != 0xd000)
591 ((*_bfd_error_handler)
592 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
593 abfd, (unsigned long) irel->r_offset, insn));
597 /* Get the address from which the register is being loaded. The
598 displacement in the mov.l instruction is quadrupled. It is a
599 displacement from four bytes after the movl instruction, but,
600 before adding in the PC address, two least significant bits
601 of the PC are cleared. We assume that the section is aligned
602 on a four byte boundary. */
605 paddr += (laddr + 4) &~ (bfd_vma) 3;
606 if (paddr >= sec->size)
608 ((*_bfd_error_handler)
609 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
610 abfd, (unsigned long) irel->r_offset));
614 /* Get the reloc for the address from which the register is
615 being loaded. This reloc will tell us which function is
616 actually being called. */
617 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
618 if (irelfn->r_offset == paddr
619 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
621 if (irelfn >= irelend)
623 ((*_bfd_error_handler)
624 (_("%B: 0x%lx: warning: could not find expected reloc"),
625 abfd, (unsigned long) paddr));
629 /* Read this BFD's symbols if we haven't done so already. */
630 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
632 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
634 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
635 symtab_hdr->sh_info, 0,
641 /* Get the value of the symbol referred to by the reloc. */
642 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
644 /* A local symbol. */
645 Elf_Internal_Sym *isym;
647 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
649 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
651 ((*_bfd_error_handler)
652 (_("%B: 0x%lx: warning: symbol in unexpected section"),
653 abfd, (unsigned long) paddr));
657 symval = (isym->st_value
658 + sec->output_section->vma
659 + sec->output_offset);
664 struct elf_link_hash_entry *h;
666 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
667 h = elf_sym_hashes (abfd)[indx];
668 BFD_ASSERT (h != NULL);
669 if (h->root.type != bfd_link_hash_defined
670 && h->root.type != bfd_link_hash_defweak)
672 /* This appears to be a reference to an undefined
673 symbol. Just ignore it--it will be caught by the
674 regular reloc processing. */
678 symval = (h->root.u.def.value
679 + h->root.u.def.section->output_section->vma
680 + h->root.u.def.section->output_offset);
683 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
684 symval += bfd_get_32 (abfd, contents + paddr);
686 symval += irelfn->r_addend;
688 /* See if this function call can be shortened. */
691 + sec->output_section->vma
694 /* A branch to an address beyond ours might be increased by an
695 .align that doesn't move when bytes behind us are deleted.
696 So, we add some slop in this calculation to allow for
698 if (foff < -0x1000 || foff >= 0x1000 - 8)
700 /* After all that work, we can't shorten this function call. */
704 /* Shorten the function call. */
706 /* For simplicity of coding, we are going to modify the section
707 contents, the section relocs, and the BFD symbol table. We
708 must tell the rest of the code not to free up this
709 information. It would be possible to instead create a table
710 of changes which have to be made, as is done in coff-mips.c;
711 that would be more work, but would require less memory when
712 the linker is run. */
714 elf_section_data (sec)->relocs = internal_relocs;
715 elf_section_data (sec)->this_hdr.contents = contents;
716 symtab_hdr->contents = (unsigned char *) isymbuf;
718 /* Replace the jmp/jsr with a bra/bsr. */
720 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
721 replace the jmp/jsr with a bra/bsr. */
722 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
723 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
724 here, but that only checks if the symbol is an external symbol,
725 not if the symbol is in a different section. Besides, we need
726 a consistent meaning for the relocation, so we just assume here that
727 the value of the symbol is not available. */
729 /* We can't fully resolve this yet, because the external
730 symbol value may be changed by future relaxing. We let
731 the final link phase handle it. */
732 if (bfd_get_16 (abfd, contents + irel->r_offset) & 0x0020)
733 bfd_put_16 (abfd, (bfd_vma) 0xa000, contents + irel->r_offset);
735 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
739 /* When we calculated the symbol "value" we had an offset in the
740 DIR32's word in memory (we read and add it above). However,
741 the jsr we create does NOT have this offset encoded, so we
742 have to add it to the addend to preserve it. */
743 irel->r_addend += bfd_get_32 (abfd, contents + paddr);
745 /* See if there is another R_SH_USES reloc referring to the same
747 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
748 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
749 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
751 if (irelscan < irelend)
753 /* Some other function call depends upon this register load,
754 and we have not yet converted that function call.
755 Indeed, we may never be able to convert it. There is
756 nothing else we can do at this point. */
760 /* Look for a R_SH_COUNT reloc on the location where the
761 function address is stored. Do this before deleting any
762 bytes, to avoid confusion about the address. */
763 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
764 if (irelcount->r_offset == paddr
765 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
768 /* Delete the register load. */
769 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
772 /* That will change things, so, just in case it permits some
773 other function call to come within range, we should relax
774 again. Note that this is not required, and it may be slow. */
777 /* Now check whether we got a COUNT reloc. */
778 if (irelcount >= irelend)
780 ((*_bfd_error_handler)
781 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
782 abfd, (unsigned long) paddr));
786 /* The number of uses is stored in the r_addend field. We've
788 if (irelcount->r_addend == 0)
790 ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"),
792 (unsigned long) paddr));
796 --irelcount->r_addend;
798 /* If there are no more uses, we can delete the address. Reload
799 the address from irelfn, in case it was changed by the
800 previous call to sh_elf_relax_delete_bytes. */
801 if (irelcount->r_addend == 0)
803 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
807 /* We've done all we can with that function call. */
810 /* Look for load and store instructions that we can align on four
812 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
817 /* Get the section contents. */
818 if (contents == NULL)
820 if (elf_section_data (sec)->this_hdr.contents != NULL)
821 contents = elf_section_data (sec)->this_hdr.contents;
824 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
829 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
835 elf_section_data (sec)->relocs = internal_relocs;
836 elf_section_data (sec)->this_hdr.contents = contents;
837 symtab_hdr->contents = (unsigned char *) isymbuf;
842 && symtab_hdr->contents != (unsigned char *) isymbuf)
844 if (! link_info->keep_memory)
848 /* Cache the symbols for elf_link_input_bfd. */
849 symtab_hdr->contents = (unsigned char *) isymbuf;
854 && elf_section_data (sec)->this_hdr.contents != contents)
856 if (! link_info->keep_memory)
860 /* Cache the section contents for elf_link_input_bfd. */
861 elf_section_data (sec)->this_hdr.contents = contents;
865 if (internal_relocs != NULL
866 && elf_section_data (sec)->relocs != internal_relocs)
867 free (internal_relocs);
873 && symtab_hdr->contents != (unsigned char *) isymbuf)
876 && elf_section_data (sec)->this_hdr.contents != contents)
878 if (internal_relocs != NULL
879 && elf_section_data (sec)->relocs != internal_relocs)
880 free (internal_relocs);
885 /* Delete some bytes from a section while relaxing. FIXME: There is a
886 lot of duplication between this function and sh_relax_delete_bytes
890 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
893 Elf_Internal_Shdr *symtab_hdr;
894 unsigned int sec_shndx;
896 Elf_Internal_Rela *irel, *irelend;
897 Elf_Internal_Rela *irelalign;
899 Elf_Internal_Sym *isymbuf, *isym, *isymend;
900 struct elf_link_hash_entry **sym_hashes;
901 struct elf_link_hash_entry **end_hashes;
902 unsigned int symcount;
905 symtab_hdr = &elf_symtab_hdr (abfd);
906 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
908 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
910 contents = elf_section_data (sec)->this_hdr.contents;
912 /* The deletion must stop at the next ALIGN reloc for an aligment
913 power larger than the number of bytes we are deleting. */
918 irel = elf_section_data (sec)->relocs;
919 irelend = irel + sec->reloc_count;
920 for (; irel < irelend; irel++)
922 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
923 && irel->r_offset > addr
924 && count < (1 << irel->r_addend))
927 toaddr = irel->r_offset;
932 /* Actually delete the bytes. */
933 memmove (contents + addr, contents + addr + count,
934 (size_t) (toaddr - addr - count));
935 if (irelalign == NULL)
941 #define NOP_OPCODE (0x0009)
943 BFD_ASSERT ((count & 1) == 0);
944 for (i = 0; i < count; i += 2)
945 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
948 /* Adjust all the relocs. */
949 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
951 bfd_vma nraddr, stop;
954 int off, adjust, oinsn;
955 bfd_signed_vma voff = 0;
956 bfd_boolean overflow;
958 /* Get the new reloc address. */
959 nraddr = irel->r_offset;
960 if ((irel->r_offset > addr
961 && irel->r_offset < toaddr)
962 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
963 && irel->r_offset == toaddr))
966 /* See if this reloc was for the bytes we have deleted, in which
967 case we no longer care about it. Don't delete relocs which
968 represent addresses, though. */
969 if (irel->r_offset >= addr
970 && irel->r_offset < addr + count
971 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
972 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
973 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
974 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
975 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
978 /* If this is a PC relative reloc, see if the range it covers
979 includes the bytes we have deleted. */
980 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
989 start = irel->r_offset;
990 insn = bfd_get_16 (abfd, contents + nraddr);
994 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1001 /* If this reloc is against a symbol defined in this
1002 section, and the symbol will not be adjusted below, we
1003 must check the addend to see it will put the value in
1004 range to be adjusted, and hence must be changed. */
1005 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1007 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1008 if (isym->st_shndx == sec_shndx
1009 && (isym->st_value <= addr
1010 || isym->st_value >= toaddr))
1014 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
1016 val = bfd_get_32 (abfd, contents + nraddr);
1017 val += isym->st_value;
1018 if (val > addr && val < toaddr)
1019 bfd_put_32 (abfd, val - count, contents + nraddr);
1023 val = isym->st_value + irel->r_addend;
1024 if (val > addr && val < toaddr)
1025 irel->r_addend -= count;
1029 start = stop = addr;
1036 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1043 /* This has been made by previous relaxation. Since the
1044 relocation will be against an external symbol, the
1045 final relocation will just do the right thing. */
1046 start = stop = addr;
1052 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1054 /* The addend will be against the section symbol, thus
1055 for adjusting the addend, the relevant start is the
1056 start of the section.
1057 N.B. If we want to abandon in-place changes here and
1058 test directly using symbol + addend, we have to take into
1059 account that the addend has already been adjusted by -4. */
1060 if (stop > addr && stop < toaddr)
1061 irel->r_addend -= count;
1067 stop = start + 4 + off * 2;
1072 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1078 /* These relocs types represent
1080 The r_addend field holds the difference between the reloc
1081 address and L1. That is the start of the reloc, and
1082 adding in the contents gives us the top. We must adjust
1083 both the r_offset field and the section contents.
1084 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1085 and the elf bfd r_offset is called r_vaddr. */
1087 stop = irel->r_offset;
1088 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1092 && (stop <= addr || stop >= toaddr))
1093 irel->r_addend += count;
1094 else if (stop > addr
1096 && (start <= addr || start >= toaddr))
1097 irel->r_addend -= count;
1099 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1100 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1101 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1102 voff = bfd_get_8 (abfd, contents + nraddr);
1104 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1105 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1110 start = irel->r_offset;
1111 stop = (bfd_vma) ((bfd_signed_vma) start
1112 + (long) irel->r_addend
1119 && (stop <= addr || stop >= toaddr))
1121 else if (stop > addr
1123 && (start <= addr || start >= toaddr))
1132 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1141 if ((oinsn & 0xff00) != (insn & 0xff00))
1143 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1148 if ((oinsn & 0xf000) != (insn & 0xf000))
1150 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1154 BFD_ASSERT (adjust == count || count >= 4);
1159 if ((irel->r_offset & 3) == 0)
1162 if ((oinsn & 0xff00) != (insn & 0xff00))
1164 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1169 if (voff < 0 || voff >= 0xff)
1171 bfd_put_8 (abfd, voff, contents + nraddr);
1176 if (voff < - 0x8000 || voff >= 0x8000)
1178 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1183 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1187 irel->r_addend += adjust;
1193 ((*_bfd_error_handler)
1194 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1195 abfd, (unsigned long) irel->r_offset));
1196 bfd_set_error (bfd_error_bad_value);
1201 irel->r_offset = nraddr;
1204 /* Look through all the other sections. If there contain any IMM32
1205 relocs against internal symbols which we are not going to adjust
1206 below, we may need to adjust the addends. */
1207 for (o = abfd->sections; o != NULL; o = o->next)
1209 Elf_Internal_Rela *internal_relocs;
1210 Elf_Internal_Rela *irelscan, *irelscanend;
1211 bfd_byte *ocontents;
1214 || (o->flags & SEC_RELOC) == 0
1215 || o->reloc_count == 0)
1218 /* We always cache the relocs. Perhaps, if info->keep_memory is
1219 FALSE, we should free them, if we are permitted to, when we
1220 leave sh_coff_relax_section. */
1221 internal_relocs = (_bfd_elf_link_read_relocs
1222 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
1223 if (internal_relocs == NULL)
1227 irelscanend = internal_relocs + o->reloc_count;
1228 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1230 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1231 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1233 bfd_vma start, stop;
1234 bfd_signed_vma voff;
1236 if (ocontents == NULL)
1238 if (elf_section_data (o)->this_hdr.contents != NULL)
1239 ocontents = elf_section_data (o)->this_hdr.contents;
1242 /* We always cache the section contents.
1243 Perhaps, if info->keep_memory is FALSE, we
1244 should free them, if we are permitted to,
1245 when we leave sh_coff_relax_section. */
1246 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1248 if (ocontents != NULL)
1253 elf_section_data (o)->this_hdr.contents = ocontents;
1257 stop = irelscan->r_offset;
1259 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1261 /* STOP is in a different section, so it won't change. */
1262 if (start > addr && start < toaddr)
1263 irelscan->r_addend += count;
1265 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1266 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1270 && (stop <= addr || stop >= toaddr))
1271 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1272 ocontents + irelscan->r_offset);
1273 else if (stop > addr
1275 && (start <= addr || start >= toaddr))
1276 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1277 ocontents + irelscan->r_offset);
1280 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1283 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1287 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1288 if (isym->st_shndx == sec_shndx
1289 && (isym->st_value <= addr
1290 || isym->st_value >= toaddr))
1294 if (ocontents == NULL)
1296 if (elf_section_data (o)->this_hdr.contents != NULL)
1297 ocontents = elf_section_data (o)->this_hdr.contents;
1300 /* We always cache the section contents.
1301 Perhaps, if info->keep_memory is FALSE, we
1302 should free them, if we are permitted to,
1303 when we leave sh_coff_relax_section. */
1304 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1306 if (ocontents != NULL)
1311 elf_section_data (o)->this_hdr.contents = ocontents;
1315 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1316 val += isym->st_value;
1317 if (val > addr && val < toaddr)
1318 bfd_put_32 (abfd, val - count,
1319 ocontents + irelscan->r_offset);
1324 /* Adjust the local symbols defined in this section. */
1325 isymend = isymbuf + symtab_hdr->sh_info;
1326 for (isym = isymbuf; isym < isymend; isym++)
1328 if (isym->st_shndx == sec_shndx
1329 && isym->st_value > addr
1330 && isym->st_value < toaddr)
1331 isym->st_value -= count;
1334 /* Now adjust the global symbols defined in this section. */
1335 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1336 - symtab_hdr->sh_info);
1337 sym_hashes = elf_sym_hashes (abfd);
1338 end_hashes = sym_hashes + symcount;
1339 for (; sym_hashes < end_hashes; sym_hashes++)
1341 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1342 if ((sym_hash->root.type == bfd_link_hash_defined
1343 || sym_hash->root.type == bfd_link_hash_defweak)
1344 && sym_hash->root.u.def.section == sec
1345 && sym_hash->root.u.def.value > addr
1346 && sym_hash->root.u.def.value < toaddr)
1348 sym_hash->root.u.def.value -= count;
1352 /* See if we can move the ALIGN reloc forward. We have adjusted
1353 r_offset for it already. */
1354 if (irelalign != NULL)
1356 bfd_vma alignto, alignaddr;
1358 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1359 alignaddr = BFD_ALIGN (irelalign->r_offset,
1360 1 << irelalign->r_addend);
1361 if (alignto != alignaddr)
1363 /* Tail recursion. */
1364 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1365 (int) (alignto - alignaddr));
1372 /* Look for loads and stores which we can align to four byte
1373 boundaries. This is like sh_align_loads in coff-sh.c. */
1376 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1377 Elf_Internal_Rela *internal_relocs,
1378 bfd_byte *contents ATTRIBUTE_UNUSED,
1379 bfd_boolean *pswapped)
1381 Elf_Internal_Rela *irel, *irelend;
1382 bfd_vma *labels = NULL;
1383 bfd_vma *label, *label_end;
1388 irelend = internal_relocs + sec->reloc_count;
1390 /* Get all the addresses with labels on them. */
1391 amt = sec->reloc_count;
1392 amt *= sizeof (bfd_vma);
1393 labels = (bfd_vma *) bfd_malloc (amt);
1397 for (irel = internal_relocs; irel < irelend; irel++)
1399 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1401 *label_end = irel->r_offset;
1406 /* Note that the assembler currently always outputs relocs in
1407 address order. If that ever changes, this code will need to sort
1408 the label values and the relocs. */
1412 for (irel = internal_relocs; irel < irelend; irel++)
1414 bfd_vma start, stop;
1416 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1419 start = irel->r_offset;
1421 for (irel++; irel < irelend; irel++)
1422 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1425 stop = irel->r_offset;
1429 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1430 internal_relocs, &label,
1431 label_end, start, stop, pswapped))
1446 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1449 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1450 bfd_byte *contents, bfd_vma addr)
1452 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1453 unsigned short i1, i2;
1454 Elf_Internal_Rela *irel, *irelend;
1456 /* Swap the instructions themselves. */
1457 i1 = bfd_get_16 (abfd, contents + addr);
1458 i2 = bfd_get_16 (abfd, contents + addr + 2);
1459 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1460 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1462 /* Adjust all reloc addresses. */
1463 irelend = internal_relocs + sec->reloc_count;
1464 for (irel = internal_relocs; irel < irelend; irel++)
1466 enum elf_sh_reloc_type type;
1469 /* There are a few special types of relocs that we don't want to
1470 adjust. These relocs do not apply to the instruction itself,
1471 but are only associated with the address. */
1472 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1473 if (type == R_SH_ALIGN
1474 || type == R_SH_CODE
1475 || type == R_SH_DATA
1476 || type == R_SH_LABEL)
1479 /* If an R_SH_USES reloc points to one of the addresses being
1480 swapped, we must adjust it. It would be incorrect to do this
1481 for a jump, though, since we want to execute both
1482 instructions after the jump. (We have avoided swapping
1483 around a label, so the jump will not wind up executing an
1484 instruction it shouldn't). */
1485 if (type == R_SH_USES)
1489 off = irel->r_offset + 4 + irel->r_addend;
1491 irel->r_offset += 2;
1492 else if (off == addr + 2)
1493 irel->r_offset -= 2;
1496 if (irel->r_offset == addr)
1498 irel->r_offset += 2;
1501 else if (irel->r_offset == addr + 2)
1503 irel->r_offset -= 2;
1512 unsigned short insn, oinsn;
1513 bfd_boolean overflow;
1515 loc = contents + irel->r_offset;
1524 insn = bfd_get_16 (abfd, loc);
1527 if ((oinsn & 0xff00) != (insn & 0xff00))
1529 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1533 insn = bfd_get_16 (abfd, loc);
1536 if ((oinsn & 0xf000) != (insn & 0xf000))
1538 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1542 /* This reloc ignores the least significant 3 bits of
1543 the program counter before adding in the offset.
1544 This means that if ADDR is at an even address, the
1545 swap will not affect the offset. If ADDR is an at an
1546 odd address, then the instruction will be crossing a
1547 four byte boundary, and must be adjusted. */
1548 if ((addr & 3) != 0)
1550 insn = bfd_get_16 (abfd, loc);
1553 if ((oinsn & 0xff00) != (insn & 0xff00))
1555 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1563 ((*_bfd_error_handler)
1564 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1565 abfd, (unsigned long) irel->r_offset));
1566 bfd_set_error (bfd_error_bad_value);
1574 #endif /* defined SH64_ELF */
1576 /* Describes one of the various PLT styles. */
1578 struct elf_sh_plt_info
1580 /* The template for the first PLT entry, or NULL if there is no special
1582 const bfd_byte *plt0_entry;
1584 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1585 bfd_vma plt0_entry_size;
1587 /* Index I is the offset into PLT0_ENTRY of a pointer to
1588 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1589 if there is no such pointer. */
1590 bfd_vma plt0_got_fields[3];
1592 /* The template for a symbol's PLT entry. */
1593 const bfd_byte *symbol_entry;
1595 /* The size of SYMBOL_ENTRY in bytes. */
1596 bfd_vma symbol_entry_size;
1598 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1599 on all targets. The comments by each member indicate the value
1600 that the field must hold. */
1602 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1603 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1604 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1605 bfd_boolean got20; /* TRUE if got_entry points to a movi20
1606 instruction (instead of a constant pool
1610 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1611 bfd_vma symbol_resolve_offset;
1613 /* A different PLT layout which can be used for the first
1614 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1616 const struct elf_sh_plt_info *short_plt;
1619 #ifdef INCLUDE_SHMEDIA
1621 /* The size in bytes of an entry in the procedure linkage table. */
1623 #define ELF_PLT_ENTRY_SIZE 64
1625 /* First entry in an absolute procedure linkage table look like this. */
1627 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1629 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1630 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1631 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1632 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1633 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1634 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1635 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1636 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1637 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1638 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1639 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1640 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1641 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1642 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1643 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1644 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1647 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1649 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1650 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1651 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1652 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1653 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1654 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1655 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1656 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1657 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1658 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1659 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1660 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1661 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1662 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1663 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1664 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1667 /* Sebsequent entries in an absolute procedure linkage table look like
1670 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1672 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1673 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1674 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1675 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1676 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1677 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1678 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1679 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1680 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1681 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1682 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1683 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1684 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1685 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1686 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1687 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1690 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1692 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1693 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1694 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1695 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1696 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1697 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1698 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1699 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1700 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1701 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1702 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1703 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1704 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1705 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1706 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1707 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1710 /* Entries in a PIC procedure linkage table look like this. */
1712 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1714 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1715 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1716 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1717 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1718 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1719 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1720 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1721 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1722 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1723 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1724 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1725 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1726 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1727 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1728 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1729 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1732 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1734 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1735 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1736 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1737 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1738 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1739 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1740 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1741 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1742 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1743 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1744 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1745 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1746 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1747 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1748 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1749 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1752 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1755 /* Big-endian non-PIC. */
1756 elf_sh_plt0_entry_be,
1758 { 0, MINUS_ONE, MINUS_ONE },
1759 elf_sh_plt_entry_be,
1761 { 0, 32, 48, FALSE },
1762 33, /* includes ISA encoding */
1766 /* Little-endian non-PIC. */
1767 elf_sh_plt0_entry_le,
1769 { 0, MINUS_ONE, MINUS_ONE },
1770 elf_sh_plt_entry_le,
1772 { 0, 32, 48, FALSE },
1773 33, /* includes ISA encoding */
1779 /* Big-endian PIC. */
1780 elf_sh_plt0_entry_be,
1782 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1783 elf_sh_pic_plt_entry_be,
1785 { 0, MINUS_ONE, 52, FALSE },
1786 33, /* includes ISA encoding */
1790 /* Little-endian PIC. */
1791 elf_sh_plt0_entry_le,
1793 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1794 elf_sh_pic_plt_entry_le,
1796 { 0, MINUS_ONE, 52, FALSE },
1797 33, /* includes ISA encoding */
1803 /* Return offset of the linker in PLT0 entry. */
1804 #define elf_sh_plt0_gotplt_offset(info) 0
1806 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1807 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1810 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1813 install_plt_field (bfd *output_bfd, bfd_boolean code_p,
1814 unsigned long value, bfd_byte *addr)
1817 bfd_put_32 (output_bfd,
1818 bfd_get_32 (output_bfd, addr)
1819 | ((value >> 6) & 0x3fffc00),
1821 bfd_put_32 (output_bfd,
1822 bfd_get_32 (output_bfd, addr + 4)
1823 | ((value << 10) & 0x3fffc00),
1827 /* Return the type of PLT associated with ABFD. PIC_P is true if
1828 the object is position-independent. */
1830 static const struct elf_sh_plt_info *
1831 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
1833 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
1836 /* The size in bytes of an entry in the procedure linkage table. */
1838 #define ELF_PLT_ENTRY_SIZE 28
1840 /* First entry in an absolute procedure linkage table look like this. */
1842 /* Note - this code has been "optimised" not to use r2. r2 is used by
1843 GCC to return the address of large structures, so it should not be
1844 corrupted here. This does mean however, that this PLT does not conform
1845 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1846 and r2 contains the GOT id. This version stores the GOT id in r0 and
1847 ignores the type. Loaders can easily detect this difference however,
1848 since the type will always be 0 or 8, and the GOT ids will always be
1849 greater than or equal to 12. */
1850 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1852 0xd0, 0x05, /* mov.l 2f,r0 */
1853 0x60, 0x02, /* mov.l @r0,r0 */
1854 0x2f, 0x06, /* mov.l r0,@-r15 */
1855 0xd0, 0x03, /* mov.l 1f,r0 */
1856 0x60, 0x02, /* mov.l @r0,r0 */
1857 0x40, 0x2b, /* jmp @r0 */
1858 0x60, 0xf6, /* mov.l @r15+,r0 */
1859 0x00, 0x09, /* nop */
1860 0x00, 0x09, /* nop */
1861 0x00, 0x09, /* nop */
1862 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1863 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1866 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1868 0x05, 0xd0, /* mov.l 2f,r0 */
1869 0x02, 0x60, /* mov.l @r0,r0 */
1870 0x06, 0x2f, /* mov.l r0,@-r15 */
1871 0x03, 0xd0, /* mov.l 1f,r0 */
1872 0x02, 0x60, /* mov.l @r0,r0 */
1873 0x2b, 0x40, /* jmp @r0 */
1874 0xf6, 0x60, /* mov.l @r15+,r0 */
1875 0x09, 0x00, /* nop */
1876 0x09, 0x00, /* nop */
1877 0x09, 0x00, /* nop */
1878 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1879 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1882 /* Sebsequent entries in an absolute procedure linkage table look like
1885 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1887 0xd0, 0x04, /* mov.l 1f,r0 */
1888 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1889 0xd1, 0x02, /* mov.l 0f,r1 */
1890 0x40, 0x2b, /* jmp @r0 */
1891 0x60, 0x13, /* mov r1,r0 */
1892 0xd1, 0x03, /* mov.l 2f,r1 */
1893 0x40, 0x2b, /* jmp @r0 */
1894 0x00, 0x09, /* nop */
1895 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1896 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1897 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1900 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1902 0x04, 0xd0, /* mov.l 1f,r0 */
1903 0x02, 0x60, /* mov.l @r0,r0 */
1904 0x02, 0xd1, /* mov.l 0f,r1 */
1905 0x2b, 0x40, /* jmp @r0 */
1906 0x13, 0x60, /* mov r1,r0 */
1907 0x03, 0xd1, /* mov.l 2f,r1 */
1908 0x2b, 0x40, /* jmp @r0 */
1909 0x09, 0x00, /* nop */
1910 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1911 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1912 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1915 /* Entries in a PIC procedure linkage table look like this. */
1917 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1919 0xd0, 0x04, /* mov.l 1f,r0 */
1920 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1921 0x40, 0x2b, /* jmp @r0 */
1922 0x00, 0x09, /* nop */
1923 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1924 0xd1, 0x03, /* mov.l 2f,r1 */
1925 0x40, 0x2b, /* jmp @r0 */
1926 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1927 0x00, 0x09, /* nop */
1928 0x00, 0x09, /* nop */
1929 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1930 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1933 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1935 0x04, 0xd0, /* mov.l 1f,r0 */
1936 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1937 0x2b, 0x40, /* jmp @r0 */
1938 0x09, 0x00, /* nop */
1939 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1940 0x03, 0xd1, /* mov.l 2f,r1 */
1941 0x2b, 0x40, /* jmp @r0 */
1942 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1943 0x09, 0x00, /* nop */
1944 0x09, 0x00, /* nop */
1945 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1946 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1949 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1952 /* Big-endian non-PIC. */
1953 elf_sh_plt0_entry_be,
1955 { MINUS_ONE, 24, 20 },
1956 elf_sh_plt_entry_be,
1958 { 20, 16, 24, FALSE },
1963 /* Little-endian non-PIC. */
1964 elf_sh_plt0_entry_le,
1966 { MINUS_ONE, 24, 20 },
1967 elf_sh_plt_entry_le,
1969 { 20, 16, 24, FALSE },
1976 /* Big-endian PIC. */
1977 elf_sh_plt0_entry_be,
1979 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1980 elf_sh_pic_plt_entry_be,
1982 { 20, MINUS_ONE, 24, FALSE },
1987 /* Little-endian PIC. */
1988 elf_sh_plt0_entry_le,
1990 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1991 elf_sh_pic_plt_entry_le,
1993 { 20, MINUS_ONE, 24, FALSE },
2000 #define VXWORKS_PLT_HEADER_SIZE 12
2001 #define VXWORKS_PLT_ENTRY_SIZE 24
2003 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
2005 0xd1, 0x01, /* mov.l @(8,pc),r1 */
2006 0x61, 0x12, /* mov.l @r1,r1 */
2007 0x41, 0x2b, /* jmp @r1 */
2008 0x00, 0x09, /* nop */
2009 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2012 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
2014 0x01, 0xd1, /* mov.l @(8,pc),r1 */
2015 0x12, 0x61, /* mov.l @r1,r1 */
2016 0x2b, 0x41, /* jmp @r1 */
2017 0x09, 0x00, /* nop */
2018 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2021 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2023 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2024 0x60, 0x02, /* mov.l @r0,r0 */
2025 0x40, 0x2b, /* jmp @r0 */
2026 0x00, 0x09, /* nop */
2027 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2028 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2029 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
2030 0x00, 0x09, /* nop */
2031 0x00, 0x09, /* nop */
2032 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2035 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2037 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2038 0x02, 0x60, /* mov.l @r0,r0 */
2039 0x2b, 0x40, /* jmp @r0 */
2040 0x09, 0x00, /* nop */
2041 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2042 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2043 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
2044 0x09, 0x00, /* nop */
2045 0x09, 0x00, /* nop */
2046 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2049 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2051 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2052 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2053 0x40, 0x2b, /* jmp @r0 */
2054 0x00, 0x09, /* nop */
2055 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2056 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2057 0x51, 0xc2, /* mov.l @(8,r12),r1 */
2058 0x41, 0x2b, /* jmp @r1 */
2059 0x00, 0x09, /* nop */
2060 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2063 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2065 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2066 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2067 0x2b, 0x40, /* jmp @r0 */
2068 0x09, 0x00, /* nop */
2069 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2070 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2071 0xc2, 0x51, /* mov.l @(8,r12),r1 */
2072 0x2b, 0x41, /* jmp @r1 */
2073 0x09, 0x00, /* nop */
2074 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2077 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
2080 /* Big-endian non-PIC. */
2081 vxworks_sh_plt0_entry_be,
2082 VXWORKS_PLT_HEADER_SIZE,
2083 { MINUS_ONE, MINUS_ONE, 8 },
2084 vxworks_sh_plt_entry_be,
2085 VXWORKS_PLT_ENTRY_SIZE,
2086 { 8, 14, 20, FALSE },
2091 /* Little-endian non-PIC. */
2092 vxworks_sh_plt0_entry_le,
2093 VXWORKS_PLT_HEADER_SIZE,
2094 { MINUS_ONE, MINUS_ONE, 8 },
2095 vxworks_sh_plt_entry_le,
2096 VXWORKS_PLT_ENTRY_SIZE,
2097 { 8, 14, 20, FALSE },
2104 /* Big-endian PIC. */
2107 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2108 vxworks_sh_pic_plt_entry_be,
2109 VXWORKS_PLT_ENTRY_SIZE,
2110 { 8, MINUS_ONE, 20, FALSE },
2115 /* Little-endian PIC. */
2118 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2119 vxworks_sh_pic_plt_entry_le,
2120 VXWORKS_PLT_ENTRY_SIZE,
2121 { 8, MINUS_ONE, 20, FALSE },
2128 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
2129 binding are to omit the lazy binding stub when linking with -z now
2130 and to move lazy binding stubs into a separate region for better
2133 #define FDPIC_PLT_ENTRY_SIZE 28
2134 #define FDPIC_PLT_LAZY_OFFSET 20
2136 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
2137 duplicated if it is out of range, or which can be inlined. So
2138 right now it is always inlined, which wastes a word per stub. It
2139 might be easier to handle the duplication if we put the lazy
2140 stubs separately. */
2142 static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] =
2144 0xd0, 0x02, /* mov.l @(12,pc),r0 */
2145 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2146 0x70, 0x04, /* add #4, r0 */
2147 0x41, 0x2b, /* jmp @r1 */
2148 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2149 0x00, 0x09, /* nop */
2150 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2151 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2152 0x60, 0xc2, /* mov.l @r12,r0 */
2153 0x40, 0x2b, /* jmp @r0 */
2154 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2155 0x00, 0x09, /* nop */
2158 static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] =
2160 0x02, 0xd0, /* mov.l @(12,pc),r0 */
2161 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2162 0x04, 0x70, /* add #4, r0 */
2163 0x2b, 0x41, /* jmp @r1 */
2164 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2165 0x09, 0x00, /* nop */
2166 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2167 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2168 0xc2, 0x60, /* mov.l @r12,r0 */
2169 0x2b, 0x40, /* jmp @r0 */
2170 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2171 0x09, 0x00, /* nop */
2174 static const struct elf_sh_plt_info fdpic_sh_plts[2] = {
2176 /* Big-endian PIC. */
2179 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2180 fdpic_sh_plt_entry_be,
2181 FDPIC_PLT_ENTRY_SIZE,
2182 { 12, MINUS_ONE, 16, FALSE },
2183 FDPIC_PLT_LAZY_OFFSET,
2187 /* Little-endian PIC. */
2190 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2191 fdpic_sh_plt_entry_le,
2192 FDPIC_PLT_ENTRY_SIZE,
2193 { 12, MINUS_ONE, 16, FALSE },
2194 FDPIC_PLT_LAZY_OFFSET,
2199 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
2200 entries for the first 64K slots. We use the normal FDPIC PLT entry
2201 past that point; we could also use movi20s, which might be faster,
2202 but would not be any smaller. */
2204 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
2205 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
2207 static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2209 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2210 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2211 0x70, 0x04, /* add #4, r0 */
2212 0x41, 0x2b, /* jmp @r1 */
2213 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2214 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2215 0x60, 0xc2, /* mov.l @r12,r0 */
2216 0x40, 0x2b, /* jmp @r0 */
2217 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2218 0x00, 0x09, /* nop */
2221 static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2223 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2224 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2225 0x04, 0x70, /* add #4, r0 */
2226 0x2b, 0x41, /* jmp @r1 */
2227 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2228 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2229 0xc2, 0x60, /* mov.l @r12,r0 */
2230 0x2b, 0x40, /* jmp @r0 */
2231 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2232 0x09, 0x00, /* nop */
2235 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = {
2236 /* Big-endian FDPIC, max index 64K. */
2239 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2240 fdpic_sh2a_plt_entry_be,
2241 FDPIC_SH2A_PLT_ENTRY_SIZE,
2242 { 0, MINUS_ONE, 12, TRUE },
2243 FDPIC_SH2A_PLT_LAZY_OFFSET,
2247 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = {
2248 /* Little-endian FDPIC, max index 64K. */
2251 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2252 fdpic_sh2a_plt_entry_le,
2253 FDPIC_SH2A_PLT_ENTRY_SIZE,
2254 { 0, MINUS_ONE, 12, TRUE },
2255 FDPIC_SH2A_PLT_LAZY_OFFSET,
2259 static const struct elf_sh_plt_info fdpic_sh2a_plts[2] = {
2261 /* Big-endian PIC. */
2264 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2265 fdpic_sh_plt_entry_be,
2266 FDPIC_PLT_ENTRY_SIZE,
2267 { 12, MINUS_ONE, 16, FALSE },
2268 FDPIC_PLT_LAZY_OFFSET,
2269 &fdpic_sh2a_short_plt_be
2272 /* Little-endian PIC. */
2275 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2276 fdpic_sh_plt_entry_le,
2277 FDPIC_PLT_ENTRY_SIZE,
2278 { 12, MINUS_ONE, 16, FALSE },
2279 FDPIC_PLT_LAZY_OFFSET,
2280 &fdpic_sh2a_short_plt_le
2284 /* Return the type of PLT associated with ABFD. PIC_P is true if
2285 the object is position-independent. */
2287 static const struct elf_sh_plt_info *
2288 get_plt_info (bfd *abfd, bfd_boolean pic_p)
2290 if (fdpic_object_p (abfd))
2292 /* If any input file requires SH2A we can use a shorter PLT
2294 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base)
2295 return &fdpic_sh2a_plts[!bfd_big_endian (abfd)];
2297 return &fdpic_sh_plts[!bfd_big_endian (abfd)];
2299 if (vxworks_object_p (abfd))
2300 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2301 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2304 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2305 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2309 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2310 unsigned long value, bfd_byte *addr)
2312 bfd_put_32 (output_bfd, value, addr);
2316 /* The number of PLT entries which can use a shorter PLT, if any.
2317 Currently always 64K, since only SH-2A FDPIC uses this; a
2318 20-bit movi20 can address that many function descriptors below
2319 _GLOBAL_OFFSET_TABLE_. */
2320 #define MAX_SHORT_PLT 65536
2322 /* Return the index of the PLT entry at byte offset OFFSET. */
2325 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2327 bfd_vma plt_index = 0;
2329 offset -= info->plt0_entry_size;
2330 if (info->short_plt != NULL)
2332 if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size)
2334 plt_index = MAX_SHORT_PLT;
2335 offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2338 info = info->short_plt;
2340 return plt_index + offset / info->symbol_entry_size;
2343 /* Do the inverse operation. */
2346 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index)
2350 if (info->short_plt != NULL)
2352 if (plt_index > MAX_SHORT_PLT)
2354 offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2355 plt_index -= MAX_SHORT_PLT;
2358 info = info->short_plt;
2360 return (offset + info->plt0_entry_size
2361 + (plt_index * info->symbol_entry_size));
2364 /* The sh linker needs to keep track of the number of relocs that it
2365 decides to copy as dynamic relocs in check_relocs for each symbol.
2366 This is so that it can later discard them if they are found to be
2367 unnecessary. We store the information in a field extending the
2368 regular ELF linker hash table. */
2370 struct elf_sh_dyn_relocs
2372 struct elf_sh_dyn_relocs *next;
2374 /* The input section of the reloc. */
2377 /* Total number of relocs copied for the input section. */
2378 bfd_size_type count;
2380 /* Number of pc-relative relocs copied for the input section. */
2381 bfd_size_type pc_count;
2386 bfd_signed_vma refcount;
2390 /* sh ELF linker hash entry. */
2392 struct elf_sh_link_hash_entry
2394 struct elf_link_hash_entry root;
2396 #ifdef INCLUDE_SHMEDIA
2399 bfd_signed_vma refcount;
2404 /* Track dynamic relocs copied for this symbol. */
2405 struct elf_sh_dyn_relocs *dyn_relocs;
2407 bfd_signed_vma gotplt_refcount;
2409 /* A local function descriptor, for FDPIC. The refcount counts
2410 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2411 relocations; the PLT and GOT entry are accounted
2412 for separately. After adjust_dynamic_symbol, the offset is
2413 MINUS_ONE if there is no local descriptor (dynamic linker
2414 managed and no PLT entry, or undefined weak non-dynamic).
2415 During check_relocs we do not yet know whether the local
2416 descriptor will be canonical. */
2417 union gotref funcdesc;
2419 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2420 and thus require fixups or relocations. */
2421 bfd_signed_vma abs_funcdesc_refcount;
2424 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC
2428 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2430 struct sh_elf_obj_tdata
2432 struct elf_obj_tdata root;
2434 /* got_type for each local got entry. */
2435 char *local_got_type;
2437 /* Function descriptor refcount and offset for each local symbol. */
2438 union gotref *local_funcdesc;
2441 #define sh_elf_tdata(abfd) \
2442 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2444 #define sh_elf_local_got_type(abfd) \
2445 (sh_elf_tdata (abfd)->local_got_type)
2447 #define sh_elf_local_funcdesc(abfd) \
2448 (sh_elf_tdata (abfd)->local_funcdesc)
2450 #define is_sh_elf(bfd) \
2451 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2452 && elf_tdata (bfd) != NULL \
2453 && elf_object_id (bfd) == SH_ELF_DATA)
2455 /* Override the generic function because we need to store sh_elf_obj_tdata
2456 as the specific tdata. */
2459 sh_elf_mkobject (bfd *abfd)
2461 return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata),
2465 /* sh ELF linker hash table. */
2467 struct elf_sh_link_hash_table
2469 struct elf_link_hash_table root;
2471 /* Short-cuts to get to dynamic linker sections. */
2479 asection *sfuncdesc;
2480 asection *srelfuncdesc;
2483 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2486 /* Small local sym cache. */
2487 struct sym_cache sym_cache;
2489 /* A counter or offset to track a TLS got entry. */
2492 bfd_signed_vma refcount;
2496 /* The type of PLT to use. */
2497 const struct elf_sh_plt_info *plt_info;
2499 /* True if the target system is VxWorks. */
2500 bfd_boolean vxworks_p;
2502 /* True if the target system uses FDPIC. */
2503 bfd_boolean fdpic_p;
2506 /* Traverse an sh ELF linker hash table. */
2508 #define sh_elf_link_hash_traverse(table, func, info) \
2509 (elf_link_hash_traverse \
2511 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2514 /* Get the sh ELF linker hash table from a link_info structure. */
2516 #define sh_elf_hash_table(p) \
2517 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2518 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2520 /* Create an entry in an sh ELF linker hash table. */
2522 static struct bfd_hash_entry *
2523 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2524 struct bfd_hash_table *table,
2527 struct elf_sh_link_hash_entry *ret =
2528 (struct elf_sh_link_hash_entry *) entry;
2530 /* Allocate the structure if it has not already been allocated by a
2532 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2533 ret = ((struct elf_sh_link_hash_entry *)
2534 bfd_hash_allocate (table,
2535 sizeof (struct elf_sh_link_hash_entry)));
2536 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2537 return (struct bfd_hash_entry *) ret;
2539 /* Call the allocation method of the superclass. */
2540 ret = ((struct elf_sh_link_hash_entry *)
2541 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2543 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2545 ret->dyn_relocs = NULL;
2546 ret->gotplt_refcount = 0;
2547 #ifdef INCLUDE_SHMEDIA
2548 ret->datalabel_got.refcount = ret->root.got.refcount;
2550 ret->funcdesc.refcount = 0;
2551 ret->abs_funcdesc_refcount = 0;
2552 ret->got_type = GOT_UNKNOWN;
2555 return (struct bfd_hash_entry *) ret;
2558 /* Create an sh ELF linker hash table. */
2560 static struct bfd_link_hash_table *
2561 sh_elf_link_hash_table_create (bfd *abfd)
2563 struct elf_sh_link_hash_table *ret;
2564 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
2566 ret = (struct elf_sh_link_hash_table *) bfd_zmalloc (amt);
2567 if (ret == (struct elf_sh_link_hash_table *) NULL)
2570 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2571 sh_elf_link_hash_newfunc,
2572 sizeof (struct elf_sh_link_hash_entry),
2579 ret->vxworks_p = vxworks_object_p (abfd);
2580 ret->fdpic_p = fdpic_object_p (abfd);
2582 return &ret->root.root;
2586 sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2587 struct bfd_link_info *info, asection *p)
2589 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
2591 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2595 /* We need dynamic symbols for every section, since segments can
2596 relocate independently. */
2597 switch (elf_section_data (p)->this_hdr.sh_type)
2601 /* If sh_type is yet undecided, assume it could be
2602 SHT_PROGBITS/SHT_NOBITS. */
2606 /* There shouldn't be section relative relocations
2607 against any other section. */
2613 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2614 shortcuts to them in our hash table. */
2617 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2619 struct elf_sh_link_hash_table *htab;
2621 if (! _bfd_elf_create_got_section (dynobj, info))
2624 htab = sh_elf_hash_table (info);
2628 htab->sgot = bfd_get_linker_section (dynobj, ".got");
2629 htab->sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
2630 htab->srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2631 if (! htab->sgot || ! htab->sgotplt || ! htab->srelgot)
2634 htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
2635 (SEC_ALLOC | SEC_LOAD
2638 | SEC_LINKER_CREATED));
2639 if (htab->sfuncdesc == NULL
2640 || ! bfd_set_section_alignment (dynobj, htab->sfuncdesc, 2))
2643 htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
2644 ".rela.got.funcdesc",
2645 (SEC_ALLOC | SEC_LOAD
2648 | SEC_LINKER_CREATED
2650 if (htab->srelfuncdesc == NULL
2651 || ! bfd_set_section_alignment (dynobj, htab->srelfuncdesc, 2))
2654 /* Also create .rofixup. */
2655 htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
2656 (SEC_ALLOC | SEC_LOAD
2659 | SEC_LINKER_CREATED
2661 if (htab->srofixup == NULL
2662 || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2))
2668 /* Create dynamic sections when linking against a dynamic object. */
2671 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2673 struct elf_sh_link_hash_table *htab;
2674 flagword flags, pltflags;
2676 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2679 switch (bed->s->arch_size)
2690 bfd_set_error (bfd_error_bad_value);
2694 htab = sh_elf_hash_table (info);
2698 if (htab->root.dynamic_sections_created)
2701 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2702 .rel[a].bss sections. */
2704 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2705 | SEC_LINKER_CREATED);
2708 pltflags |= SEC_CODE;
2709 if (bed->plt_not_loaded)
2710 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2711 if (bed->plt_readonly)
2712 pltflags |= SEC_READONLY;
2714 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
2717 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2720 if (bed->want_plt_sym)
2722 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2724 struct elf_link_hash_entry *h;
2725 struct bfd_link_hash_entry *bh = NULL;
2727 if (! (_bfd_generic_link_add_one_symbol
2728 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2729 (bfd_vma) 0, (const char *) NULL, FALSE,
2730 get_elf_backend_data (abfd)->collect, &bh)))
2733 h = (struct elf_link_hash_entry *) bh;
2735 h->type = STT_OBJECT;
2736 htab->root.hplt = h;
2739 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2743 s = bfd_make_section_anyway_with_flags (abfd,
2744 bed->default_use_rela_p
2745 ? ".rela.plt" : ".rel.plt",
2746 flags | SEC_READONLY);
2749 || ! bfd_set_section_alignment (abfd, s, ptralign))
2752 if (htab->sgot == NULL
2753 && !create_got_section (abfd, info))
2756 if (bed->want_dynbss)
2758 /* The .dynbss section is a place to put symbols which are defined
2759 by dynamic objects, are referenced by regular objects, and are
2760 not functions. We must allocate space for them in the process
2761 image and use a R_*_COPY reloc to tell the dynamic linker to
2762 initialize them at run time. The linker script puts the .dynbss
2763 section into the .bss section of the final image. */
2764 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2765 SEC_ALLOC | SEC_LINKER_CREATED);
2770 /* The .rel[a].bss section holds copy relocs. This section is not
2771 normally needed. We need to create it here, though, so that the
2772 linker will map it to an output section. We can't just create it
2773 only if we need it, because we will not know whether we need it
2774 until we have seen all the input files, and the first time the
2775 main linker code calls BFD after examining all the input files
2776 (size_dynamic_sections) the input sections have already been
2777 mapped to the output sections. If the section turns out not to
2778 be needed, we can discard it later. We will never need this
2779 section when generating a shared object, since they do not use
2783 s = bfd_make_section_anyway_with_flags (abfd,
2784 (bed->default_use_rela_p
2785 ? ".rela.bss" : ".rel.bss"),
2786 flags | SEC_READONLY);
2789 || ! bfd_set_section_alignment (abfd, s, ptralign))
2794 if (htab->vxworks_p)
2796 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2803 /* Adjust a symbol defined by a dynamic object and referenced by a
2804 regular object. The current definition is in some section of the
2805 dynamic object, but we're not including those sections. We have to
2806 change the definition to something the rest of the link can
2810 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2811 struct elf_link_hash_entry *h)
2813 struct elf_sh_link_hash_table *htab;
2814 struct elf_sh_link_hash_entry *eh;
2815 struct elf_sh_dyn_relocs *p;
2818 htab = sh_elf_hash_table (info);
2822 /* Make sure we know what is going on here. */
2823 BFD_ASSERT (htab->root.dynobj != NULL
2825 || h->u.weakdef != NULL
2828 && !h->def_regular)));
2830 /* If this is a function, put it in the procedure linkage table. We
2831 will fill in the contents of the procedure linkage table later,
2832 when we know the address of the .got section. */
2833 if (h->type == STT_FUNC
2836 if (h->plt.refcount <= 0
2837 || SYMBOL_CALLS_LOCAL (info, h)
2838 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2839 && h->root.type == bfd_link_hash_undefweak))
2841 /* This case can occur if we saw a PLT reloc in an input
2842 file, but the symbol was never referred to by a dynamic
2843 object. In such a case, we don't actually need to build
2844 a procedure linkage table, and we can just do a REL32
2846 h->plt.offset = (bfd_vma) -1;
2853 h->plt.offset = (bfd_vma) -1;
2855 /* If this is a weak symbol, and there is a real definition, the
2856 processor independent code will have arranged for us to see the
2857 real definition first, and we can just use the same value. */
2858 if (h->u.weakdef != NULL)
2860 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2861 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2862 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2863 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2864 if (info->nocopyreloc)
2865 h->non_got_ref = h->u.weakdef->non_got_ref;
2869 /* This is a reference to a symbol defined by a dynamic object which
2870 is not a function. */
2872 /* If we are creating a shared library, we must presume that the
2873 only references to the symbol are via the global offset table.
2874 For such cases we need not do anything here; the relocations will
2875 be handled correctly by relocate_section. */
2879 /* If there are no references to this symbol that do not use the
2880 GOT, we don't need to generate a copy reloc. */
2881 if (!h->non_got_ref)
2884 /* If -z nocopyreloc was given, we won't generate them either. */
2885 if (info->nocopyreloc)
2891 eh = (struct elf_sh_link_hash_entry *) h;
2892 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2894 s = p->sec->output_section;
2895 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2899 /* If we didn't find any dynamic relocs in sections which needs the
2900 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2908 /* We must allocate the symbol in our .dynbss section, which will
2909 become part of the .bss section of the executable. There will be
2910 an entry for this symbol in the .dynsym section. The dynamic
2911 object will contain position independent code, so all references
2912 from the dynamic object to this symbol will go through the global
2913 offset table. The dynamic linker will use the .dynsym entry to
2914 determine the address it must put in the global offset table, so
2915 both the dynamic object and the regular object will refer to the
2916 same memory location for the variable. */
2919 BFD_ASSERT (s != NULL);
2921 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2922 copy the initial value out of the dynamic object and into the
2923 runtime process image. We need to remember the offset into the
2924 .rela.bss section we are going to use. */
2925 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2929 srel = htab->srelbss;
2930 BFD_ASSERT (srel != NULL);
2931 srel->size += sizeof (Elf32_External_Rela);
2935 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2938 /* Allocate space in .plt, .got and associated reloc sections for
2942 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2944 struct bfd_link_info *info;
2945 struct elf_sh_link_hash_table *htab;
2946 struct elf_sh_link_hash_entry *eh;
2947 struct elf_sh_dyn_relocs *p;
2949 if (h->root.type == bfd_link_hash_indirect)
2952 info = (struct bfd_link_info *) inf;
2953 htab = sh_elf_hash_table (info);
2957 eh = (struct elf_sh_link_hash_entry *) h;
2958 if ((h->got.refcount > 0
2960 && eh->gotplt_refcount > 0)
2962 /* The symbol has been forced local, or we have some direct got refs,
2963 so treat all the gotplt refs as got refs. */
2964 h->got.refcount += eh->gotplt_refcount;
2965 if (h->plt.refcount >= eh->gotplt_refcount)
2966 h->plt.refcount -= eh->gotplt_refcount;
2969 if (htab->root.dynamic_sections_created
2970 && h->plt.refcount > 0
2971 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2972 || h->root.type != bfd_link_hash_undefweak))
2974 /* Make sure this symbol is output as a dynamic symbol.
2975 Undefined weak syms won't yet be marked as dynamic. */
2976 if (h->dynindx == -1
2977 && !h->forced_local)
2979 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2984 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2986 asection *s = htab->splt;
2987 const struct elf_sh_plt_info *plt_info;
2989 /* If this is the first .plt entry, make room for the special
2992 s->size += htab->plt_info->plt0_entry_size;
2994 h->plt.offset = s->size;
2996 /* If this symbol is not defined in a regular file, and we are
2997 not generating a shared library, then set the symbol to this
2998 location in the .plt. This is required to make function
2999 pointers compare as equal between the normal executable and
3000 the shared library. Skip this for FDPIC, since the
3001 function's address will be the address of the canonical
3002 function descriptor. */
3003 if (!htab->fdpic_p && !info->shared && !h->def_regular)
3005 h->root.u.def.section = s;
3006 h->root.u.def.value = h->plt.offset;
3009 /* Make room for this entry. */
3010 plt_info = htab->plt_info;
3011 if (plt_info->short_plt != NULL
3012 && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
3013 plt_info = plt_info->short_plt;
3014 s->size += plt_info->symbol_entry_size;
3016 /* We also need to make an entry in the .got.plt section, which
3017 will be placed in the .got section by the linker script. */
3019 htab->sgotplt->size += 4;
3021 htab->sgotplt->size += 8;
3023 /* We also need to make an entry in the .rel.plt section. */
3024 htab->srelplt->size += sizeof (Elf32_External_Rela);
3026 if (htab->vxworks_p && !info->shared)
3028 /* VxWorks executables have a second set of relocations
3029 for each PLT entry. They go in a separate relocation
3030 section, which is processed by the kernel loader. */
3032 /* There is a relocation for the initial PLT entry:
3033 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
3034 if (h->plt.offset == htab->plt_info->plt0_entry_size)
3035 htab->srelplt2->size += sizeof (Elf32_External_Rela);
3037 /* There are two extra relocations for each subsequent
3038 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
3039 and an R_SH_DIR32 relocation for the PLT entry. */
3040 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
3045 h->plt.offset = (bfd_vma) -1;
3051 h->plt.offset = (bfd_vma) -1;
3055 if (h->got.refcount > 0)
3059 enum got_type got_type = sh_elf_hash_entry (h)->got_type;
3061 /* Make sure this symbol is output as a dynamic symbol.
3062 Undefined weak syms won't yet be marked as dynamic. */
3063 if (h->dynindx == -1
3064 && !h->forced_local)
3066 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3071 h->got.offset = s->size;
3073 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
3074 if (got_type == GOT_TLS_GD)
3076 dyn = htab->root.dynamic_sections_created;
3079 /* No dynamic relocations required. */
3080 if (htab->fdpic_p && !info->shared
3081 && h->root.type != bfd_link_hash_undefweak
3082 && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
3083 htab->srofixup->size += 4;
3085 /* No dynamic relocations required when IE->LE conversion happens. */
3086 else if (got_type == GOT_TLS_IE && !h->def_dynamic && !info->shared)
3088 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3089 R_SH_TLS_GD needs one if local symbol and two if global. */
3090 else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
3091 || got_type == GOT_TLS_IE)
3092 htab->srelgot->size += sizeof (Elf32_External_Rela);
3093 else if (got_type == GOT_TLS_GD)
3094 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
3095 else if (got_type == GOT_FUNCDESC)
3097 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h))
3098 htab->srofixup->size += 4;
3100 htab->srelgot->size += sizeof (Elf32_External_Rela);
3102 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3103 || h->root.type != bfd_link_hash_undefweak)
3105 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3106 htab->srelgot->size += sizeof (Elf32_External_Rela);
3107 else if (htab->fdpic_p && !info->shared && got_type == GOT_NORMAL
3108 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3109 || h->root.type != bfd_link_hash_undefweak))
3110 htab->srofixup->size += 4;
3113 h->got.offset = (bfd_vma) -1;
3115 #ifdef INCLUDE_SHMEDIA
3116 if (eh->datalabel_got.refcount > 0)
3121 /* Make sure this symbol is output as a dynamic symbol.
3122 Undefined weak syms won't yet be marked as dynamic. */
3123 if (h->dynindx == -1
3124 && !h->forced_local)
3126 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3131 eh->datalabel_got.offset = s->size;
3133 dyn = htab->root.dynamic_sections_created;
3134 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
3135 htab->srelgot->size += sizeof (Elf32_External_Rela);
3138 eh->datalabel_got.offset = (bfd_vma) -1;
3141 /* Allocate space for any dynamic relocations to function
3142 descriptors, canonical or otherwise. We need to relocate the
3143 reference unless it resolves to zero, which only happens for
3144 undefined weak symbols (either non-default visibility, or when
3145 static linking). Any GOT slot is accounted for elsewhere. */
3146 if (eh->abs_funcdesc_refcount > 0
3147 && (h->root.type != bfd_link_hash_undefweak
3148 || (htab->root.dynamic_sections_created
3149 && ! SYMBOL_CALLS_LOCAL (info, h))))
3151 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h))
3152 htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
3155 += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
3158 /* We must allocate a function descriptor if there are references to
3159 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
3160 the dynamic linker isn't going to allocate it. None of this
3161 applies if we already created one in .got.plt, but if the
3162 canonical function descriptor can be in this object, there
3163 won't be a PLT entry at all. */
3164 if ((eh->funcdesc.refcount > 0
3165 || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
3166 && h->root.type != bfd_link_hash_undefweak
3167 && SYMBOL_FUNCDESC_LOCAL (info, h))
3169 /* Make room for this function descriptor. */
3170 eh->funcdesc.offset = htab->sfuncdesc->size;
3171 htab->sfuncdesc->size += 8;
3173 /* We will need a relocation or two fixups to initialize the
3174 function descriptor, so allocate those too. */
3175 if (!info->shared && SYMBOL_CALLS_LOCAL (info, h))
3176 htab->srofixup->size += 8;
3178 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3181 if (eh->dyn_relocs == NULL)
3184 /* In the shared -Bsymbolic case, discard space allocated for
3185 dynamic pc-relative relocs against symbols which turn out to be
3186 defined in regular objects. For the normal shared case, discard
3187 space for pc-relative relocs that have become local due to symbol
3188 visibility changes. */
3192 if (SYMBOL_CALLS_LOCAL (info, h))
3194 struct elf_sh_dyn_relocs **pp;
3196 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3198 p->count -= p->pc_count;
3207 if (htab->vxworks_p)
3209 struct elf_sh_dyn_relocs **pp;
3211 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3213 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
3220 /* Also discard relocs on undefined weak syms with non-default
3222 if (eh->dyn_relocs != NULL
3223 && h->root.type == bfd_link_hash_undefweak)
3225 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3226 eh->dyn_relocs = NULL;
3228 /* Make sure undefined weak symbols are output as a dynamic
3230 else if (h->dynindx == -1
3231 && !h->forced_local)
3233 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3240 /* For the non-shared case, discard space for relocs against
3241 symbols which turn out to need copy relocs or are not
3247 || (htab->root.dynamic_sections_created
3248 && (h->root.type == bfd_link_hash_undefweak
3249 || h->root.type == bfd_link_hash_undefined))))
3251 /* Make sure this symbol is output as a dynamic symbol.
3252 Undefined weak syms won't yet be marked as dynamic. */
3253 if (h->dynindx == -1
3254 && !h->forced_local)
3256 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3260 /* If that succeeded, we know we'll be keeping all the
3262 if (h->dynindx != -1)
3266 eh->dyn_relocs = NULL;
3271 /* Finally, allocate space. */
3272 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3274 asection *sreloc = elf_section_data (p->sec)->sreloc;
3275 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3277 /* If we need relocations, we do not need fixups. */
3278 if (htab->fdpic_p && !info->shared)
3279 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3285 /* Find any dynamic relocs that apply to read-only sections. */
3288 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3290 struct elf_sh_link_hash_entry *eh;
3291 struct elf_sh_dyn_relocs *p;
3293 eh = (struct elf_sh_link_hash_entry *) h;
3294 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3296 asection *s = p->sec->output_section;
3298 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3300 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3302 info->flags |= DF_TEXTREL;
3304 /* Not an error, just cut short the traversal. */
3311 /* This function is called after all the input files have been read,
3312 and the input sections have been assigned to output sections.
3313 It's a convenient place to determine the PLT style. */
3316 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
3318 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd, info->shared);
3320 if (sh_elf_hash_table (info)->fdpic_p && !info->relocatable
3321 && !bfd_elf_stack_segment_size (output_bfd, info,
3322 "__stacksize", DEFAULT_STACK_SIZE))
3327 /* Set the sizes of the dynamic sections. */
3330 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3331 struct bfd_link_info *info)
3333 struct elf_sh_link_hash_table *htab;
3339 htab = sh_elf_hash_table (info);
3343 dynobj = htab->root.dynobj;
3344 BFD_ASSERT (dynobj != NULL);
3346 if (htab->root.dynamic_sections_created)
3348 /* Set the contents of the .interp section to the interpreter. */
3349 if (info->executable)
3351 s = bfd_get_linker_section (dynobj, ".interp");
3352 BFD_ASSERT (s != NULL);
3353 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3354 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3358 /* Set up .got offsets for local syms, and space for local dynamic
3360 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3362 bfd_signed_vma *local_got;
3363 bfd_signed_vma *end_local_got;
3364 union gotref *local_funcdesc, *end_local_funcdesc;
3365 char *local_got_type;
3366 bfd_size_type locsymcount;
3367 Elf_Internal_Shdr *symtab_hdr;
3370 if (! is_sh_elf (ibfd))
3373 for (s = ibfd->sections; s != NULL; s = s->next)
3375 struct elf_sh_dyn_relocs *p;
3377 for (p = ((struct elf_sh_dyn_relocs *)
3378 elf_section_data (s)->local_dynrel);
3382 if (! bfd_is_abs_section (p->sec)
3383 && bfd_is_abs_section (p->sec->output_section))
3385 /* Input section has been discarded, either because
3386 it is a copy of a linkonce section or due to
3387 linker script /DISCARD/, so we'll be discarding
3390 else if (htab->vxworks_p
3391 && strcmp (p->sec->output_section->name,
3394 /* Relocations in vxworks .tls_vars sections are
3395 handled specially by the loader. */
3397 else if (p->count != 0)
3399 srel = elf_section_data (p->sec)->sreloc;
3400 srel->size += p->count * sizeof (Elf32_External_Rela);
3401 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3402 info->flags |= DF_TEXTREL;
3404 /* If we need relocations, we do not need fixups. */
3405 if (htab->fdpic_p && !info->shared)
3406 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3411 symtab_hdr = &elf_symtab_hdr (ibfd);
3412 locsymcount = symtab_hdr->sh_info;
3413 #ifdef INCLUDE_SHMEDIA
3414 /* Count datalabel local GOT. */
3418 srel = htab->srelgot;
3420 local_got = elf_local_got_refcounts (ibfd);
3423 end_local_got = local_got + locsymcount;
3424 local_got_type = sh_elf_local_got_type (ibfd);
3425 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3426 for (; local_got < end_local_got; ++local_got)
3430 *local_got = s->size;
3432 if (*local_got_type == GOT_TLS_GD)
3435 srel->size += sizeof (Elf32_External_Rela);
3437 htab->srofixup->size += 4;
3439 if (*local_got_type == GOT_FUNCDESC)
3441 if (local_funcdesc == NULL)
3445 size = locsymcount * sizeof (union gotref);
3446 local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
3448 if (local_funcdesc == NULL)
3450 sh_elf_local_funcdesc (ibfd) = local_funcdesc;
3451 local_funcdesc += (local_got
3452 - elf_local_got_refcounts (ibfd));
3454 local_funcdesc->refcount++;
3459 *local_got = (bfd_vma) -1;
3464 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3467 end_local_funcdesc = local_funcdesc + locsymcount;
3469 for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
3471 if (local_funcdesc->refcount > 0)
3473 local_funcdesc->offset = htab->sfuncdesc->size;
3474 htab->sfuncdesc->size += 8;
3476 htab->srofixup->size += 8;
3478 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3481 local_funcdesc->offset = MINUS_ONE;
3487 if (htab->tls_ldm_got.refcount > 0)
3489 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3491 htab->tls_ldm_got.offset = htab->sgot->size;
3492 htab->sgot->size += 8;
3493 htab->srelgot->size += sizeof (Elf32_External_Rela);
3496 htab->tls_ldm_got.offset = -1;
3498 /* Only the reserved entries should be present. For FDPIC, they go at
3499 the end of .got.plt. */
3502 BFD_ASSERT (htab->sgotplt && htab->sgotplt->size == 12);
3503 htab->sgotplt->size = 0;
3506 /* Allocate global sym .plt and .got entries, and space for global
3507 sym dynamic relocs. */
3508 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
3510 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3511 end of the FDPIC .got.plt. */
3514 htab->root.hgot->root.u.def.value = htab->sgotplt->size;
3515 htab->sgotplt->size += 12;
3518 /* At the very end of the .rofixup section is a pointer to the GOT. */
3519 if (htab->fdpic_p && htab->srofixup != NULL)
3520 htab->srofixup->size += 4;
3522 /* We now have determined the sizes of the various dynamic sections.
3523 Allocate memory for them. */
3525 for (s = dynobj->sections; s != NULL; s = s->next)
3527 if ((s->flags & SEC_LINKER_CREATED) == 0)
3532 || s == htab->sgotplt
3533 || s == htab->sfuncdesc
3534 || s == htab->srofixup
3535 || s == htab->sdynbss)
3537 /* Strip this section if we don't need it; see the
3540 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3542 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
3545 /* We use the reloc_count field as a counter if we need
3546 to copy relocs into the output file. */
3551 /* It's not one of our sections, so don't allocate space. */
3557 /* If we don't need this section, strip it from the
3558 output file. This is mostly to handle .rela.bss and
3559 .rela.plt. We must create both sections in
3560 create_dynamic_sections, because they must be created
3561 before the linker maps input sections to output
3562 sections. The linker does that before
3563 adjust_dynamic_symbol is called, and it is that
3564 function which decides whether anything needs to go
3565 into these sections. */
3567 s->flags |= SEC_EXCLUDE;
3571 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3574 /* Allocate memory for the section contents. We use bfd_zalloc
3575 here in case unused entries are not reclaimed before the
3576 section's contents are written out. This should not happen,
3577 but this way if it does, we get a R_SH_NONE reloc instead
3579 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3580 if (s->contents == NULL)
3584 if (htab->root.dynamic_sections_created)
3586 /* Add some entries to the .dynamic section. We fill in the
3587 values later, in sh_elf_finish_dynamic_sections, but we
3588 must add the entries now so that we get the correct size for
3589 the .dynamic section. The DT_DEBUG entry is filled in by the
3590 dynamic linker and used by the debugger. */
3591 #define add_dynamic_entry(TAG, VAL) \
3592 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3594 if (info->executable)
3596 if (! add_dynamic_entry (DT_DEBUG, 0))
3600 if (htab->splt->size != 0)
3602 if (! add_dynamic_entry (DT_PLTGOT, 0)
3603 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3604 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3605 || ! add_dynamic_entry (DT_JMPREL, 0))
3608 else if ((elf_elfheader (output_bfd)->e_flags & EF_SH_FDPIC)
3609 && htab->sgot->size != 0)
3611 if (! add_dynamic_entry (DT_PLTGOT, 0))
3617 if (! add_dynamic_entry (DT_RELA, 0)
3618 || ! add_dynamic_entry (DT_RELASZ, 0)
3619 || ! add_dynamic_entry (DT_RELAENT,
3620 sizeof (Elf32_External_Rela)))
3623 /* If any dynamic relocs apply to a read-only section,
3624 then we need a DT_TEXTREL entry. */
3625 if ((info->flags & DF_TEXTREL) == 0)
3626 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
3628 if ((info->flags & DF_TEXTREL) != 0)
3630 if (! add_dynamic_entry (DT_TEXTREL, 0))
3635 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3638 #undef add_dynamic_entry
3643 /* Add a dynamic relocation to the SRELOC section. */
3645 inline static bfd_vma
3646 sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
3647 int reloc_type, long dynindx, bfd_vma addend)
3649 Elf_Internal_Rela outrel;
3650 bfd_vma reloc_offset;
3652 outrel.r_offset = offset;
3653 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
3654 outrel.r_addend = addend;
3656 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
3657 BFD_ASSERT (reloc_offset < sreloc->size);
3658 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3659 sreloc->contents + reloc_offset);
3660 sreloc->reloc_count++;
3662 return reloc_offset;
3665 /* Add an FDPIC read-only fixup. */
3668 sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3670 bfd_vma fixup_offset;
3672 fixup_offset = srofixup->reloc_count++ * 4;
3673 BFD_ASSERT (fixup_offset < srofixup->size);
3674 bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3677 /* Return the offset of the generated .got section from the
3678 _GLOBAL_OFFSET_TABLE_ symbol. */
3680 static bfd_signed_vma
3681 sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
3683 return (htab->sgot->output_offset - htab->sgotplt->output_offset
3684 - htab->root.hgot->root.u.def.value);
3687 /* Find the segment number in which OSEC, and output section, is
3691 sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
3693 Elf_Internal_Phdr *p = NULL;
3695 if (output_bfd->xvec->flavour == bfd_target_elf_flavour
3696 /* PR ld/17110: Do not look for output segments in an input bfd. */
3697 && output_bfd->direction != read_direction)
3698 p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
3700 /* FIXME: Nothing ever says what this index is relative to. The kernel
3701 supplies data in terms of the number of load segments but this is
3702 a phdr index and the first phdr may not be a load segment. */
3703 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
3707 sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
3709 unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
3711 return (seg != (unsigned) -1
3712 && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
3715 /* Generate the initial contents of a local function descriptor, along
3716 with any relocations or fixups required. */
3718 sh_elf_initialize_funcdesc (bfd *output_bfd,
3719 struct bfd_link_info *info,
3720 struct elf_link_hash_entry *h,
3725 struct elf_sh_link_hash_table *htab;
3729 htab = sh_elf_hash_table (info);
3731 /* FIXME: The ABI says that the offset to the function goes in the
3732 descriptor, along with the segment index. We're RELA, so it could
3733 go in the reloc instead... */
3735 if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
3737 section = h->root.u.def.section;
3738 value = h->root.u.def.value;
3741 if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
3743 dynindx = elf_section_data (section->output_section)->dynindx;
3744 addr = value + section->output_offset;
3745 seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
3749 BFD_ASSERT (h->dynindx != -1);
3750 dynindx = h->dynindx;
3754 if (!info->shared && SYMBOL_CALLS_LOCAL (info, h))
3756 if (h == NULL || h->root.type != bfd_link_hash_undefweak)
3758 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3760 + htab->sfuncdesc->output_section->vma
3761 + htab->sfuncdesc->output_offset);
3762 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3764 + htab->sfuncdesc->output_section->vma
3765 + htab->sfuncdesc->output_offset);
3768 /* There are no dynamic relocations so fill in the final
3769 address and gp value (barring fixups). */
3770 addr += section->output_section->vma;
3771 seg = htab->root.hgot->root.u.def.value
3772 + htab->root.hgot->root.u.def.section->output_section->vma
3773 + htab->root.hgot->root.u.def.section->output_offset;
3776 sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
3778 + htab->sfuncdesc->output_section->vma
3779 + htab->sfuncdesc->output_offset,
3780 R_SH_FUNCDESC_VALUE, dynindx, 0);
3782 bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
3783 bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
3788 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3789 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3792 static bfd_reloc_status_type
3793 install_movi20_field (bfd *output_bfd, unsigned long relocation,
3794 bfd *input_bfd, asection *input_section,
3795 bfd_byte *contents, bfd_vma offset)
3797 unsigned long cur_val;
3799 bfd_reloc_status_type r;
3801 if (offset > bfd_get_section_limit (input_bfd, input_section))
3802 return bfd_reloc_outofrange;
3804 r = bfd_check_overflow (complain_overflow_signed, 20, 0,
3805 bfd_arch_bits_per_address (input_bfd), relocation);
3806 if (r != bfd_reloc_ok)
3809 addr = contents + offset;
3810 cur_val = bfd_get_16 (output_bfd, addr);
3811 bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
3812 bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
3814 return bfd_reloc_ok;
3817 /* Relocate an SH ELF section. */
3820 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3821 bfd *input_bfd, asection *input_section,
3822 bfd_byte *contents, Elf_Internal_Rela *relocs,
3823 Elf_Internal_Sym *local_syms,
3824 asection **local_sections)
3826 struct elf_sh_link_hash_table *htab;
3827 Elf_Internal_Shdr *symtab_hdr;
3828 struct elf_link_hash_entry **sym_hashes;
3829 Elf_Internal_Rela *rel, *relend;
3831 bfd_vma *local_got_offsets;
3832 asection *sgot = NULL;
3833 asection *sgotplt = NULL;
3834 asection *splt = NULL;
3835 asection *sreloc = NULL;
3836 asection *srelgot = NULL;
3837 bfd_boolean is_vxworks_tls;
3838 unsigned isec_segment, got_segment, plt_segment, check_segment[2];
3839 bfd_boolean fdpic_p = FALSE;
3841 BFD_ASSERT (is_sh_elf (input_bfd));
3843 htab = sh_elf_hash_table (info);
3846 dynobj = htab->root.dynobj;
3848 sgotplt = htab->sgotplt;
3850 fdpic_p = htab->fdpic_p;
3852 symtab_hdr = &elf_symtab_hdr (input_bfd);
3853 sym_hashes = elf_sym_hashes (input_bfd);
3854 local_got_offsets = elf_local_got_offsets (input_bfd);
3856 isec_segment = sh_elf_osec_to_segment (output_bfd,
3857 input_section->output_section);
3858 if (fdpic_p && sgot)
3859 got_segment = sh_elf_osec_to_segment (output_bfd,
3860 sgot->output_section);
3863 if (fdpic_p && splt)
3864 plt_segment = sh_elf_osec_to_segment (output_bfd,
3865 splt->output_section);
3869 /* We have to handle relocations in vxworks .tls_vars sections
3870 specially, because the dynamic loader is 'weird'. */
3871 is_vxworks_tls = (htab && htab->vxworks_p && info->shared
3872 && !strcmp (input_section->output_section->name,
3876 relend = relocs + input_section->reloc_count;
3877 for (; rel < relend; rel++)
3880 reloc_howto_type *howto;
3881 unsigned long r_symndx;
3882 Elf_Internal_Sym *sym;
3884 struct elf_link_hash_entry *h;
3886 bfd_vma addend = (bfd_vma) 0;
3887 bfd_reloc_status_type r;
3888 int seen_stt_datalabel = 0;
3890 enum got_type got_type;
3891 const char *symname = NULL;
3893 r_symndx = ELF32_R_SYM (rel->r_info);
3895 r_type = ELF32_R_TYPE (rel->r_info);
3897 /* Many of the relocs are only used for relaxing, and are
3898 handled entirely by the relaxation code. */
3899 if (r_type >= (int) R_SH_GNU_VTINHERIT
3900 && r_type <= (int) R_SH_LABEL)
3902 if (r_type == (int) R_SH_NONE)
3906 || r_type >= R_SH_max
3907 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3908 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3909 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3910 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
3911 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3912 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3913 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3914 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3915 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3916 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
3917 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
3918 && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
3920 bfd_set_error (bfd_error_bad_value);
3924 howto = get_howto_table (output_bfd) + r_type;
3926 /* For relocs that aren't partial_inplace, we get the addend from
3928 if (! howto->partial_inplace)
3929 addend = rel->r_addend;
3934 check_segment[0] = -1;
3935 check_segment[1] = -1;
3936 if (r_symndx < symtab_hdr->sh_info)
3938 sym = local_syms + r_symndx;
3939 sec = local_sections[r_symndx];
3941 symname = bfd_elf_string_from_elf_section
3942 (input_bfd, symtab_hdr->sh_link, sym->st_name);
3943 if (symname == NULL || *symname == '\0')
3944 symname = bfd_section_name (input_bfd, sec);
3946 relocation = (sec->output_section->vma
3947 + sec->output_offset
3949 /* A local symbol never has STO_SH5_ISA32, so we don't need
3950 datalabel processing here. Make sure this does not change
3952 if ((sym->st_other & STO_SH5_ISA32) != 0)
3953 ((*info->callbacks->reloc_dangerous)
3955 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3956 input_bfd, input_section, rel->r_offset));
3958 if (sec != NULL && discarded_section (sec))
3959 /* Handled below. */
3961 else if (info->relocatable)
3963 /* This is a relocatable link. We don't have to change
3964 anything, unless the reloc is against a section symbol,
3965 in which case we have to adjust according to where the
3966 section symbol winds up in the output section. */
3967 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3969 if (! howto->partial_inplace)
3971 /* For relocations with the addend in the
3972 relocation, we need just to update the addend.
3973 All real relocs are of type partial_inplace; this
3974 code is mostly for completeness. */
3975 rel->r_addend += sec->output_offset;
3980 /* Relocs of type partial_inplace need to pick up the
3981 contents in the contents and add the offset resulting
3982 from the changed location of the section symbol.
3983 Using _bfd_final_link_relocate (e.g. goto
3984 final_link_relocate) here would be wrong, because
3985 relocations marked pc_relative would get the current
3986 location subtracted, and we must only do that at the
3988 r = _bfd_relocate_contents (howto, input_bfd,
3991 contents + rel->r_offset);
3992 goto relocation_done;
3997 else if (! howto->partial_inplace)
3999 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4000 addend = rel->r_addend;
4002 else if ((sec->flags & SEC_MERGE)
4003 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4007 if (howto->rightshift || howto->src_mask != 0xffffffff)
4009 (*_bfd_error_handler)
4010 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
4011 input_bfd, input_section,
4012 (long) rel->r_offset, howto->name);
4016 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4019 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4021 addend += msec->output_section->vma + msec->output_offset;
4022 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4028 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
4031 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4032 symname = h->root.root.string;
4033 while (h->root.type == bfd_link_hash_indirect
4034 || h->root.type == bfd_link_hash_warning)
4036 #ifdef INCLUDE_SHMEDIA
4037 /* If the reference passes a symbol marked with
4038 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4040 seen_stt_datalabel |= h->type == STT_DATALABEL;
4042 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4044 if (h->root.type == bfd_link_hash_defined
4045 || h->root.type == bfd_link_hash_defweak)
4049 dyn = htab ? htab->root.dynamic_sections_created : FALSE;
4050 sec = h->root.u.def.section;
4051 /* In these cases, we don't need the relocation value.
4052 We check specially because in some obscure cases
4053 sec->output_section will be NULL. */
4054 if (r_type == R_SH_GOTPC
4055 || r_type == R_SH_GOTPC_LOW16
4056 || r_type == R_SH_GOTPC_MEDLOW16
4057 || r_type == R_SH_GOTPC_MEDHI16
4058 || r_type == R_SH_GOTPC_HI16
4059 || ((r_type == R_SH_PLT32
4060 || r_type == R_SH_PLT_LOW16
4061 || r_type == R_SH_PLT_MEDLOW16
4062 || r_type == R_SH_PLT_MEDHI16
4063 || r_type == R_SH_PLT_HI16)
4064 && h->plt.offset != (bfd_vma) -1)
4065 || ((r_type == R_SH_GOT32
4066 || r_type == R_SH_GOT20
4067 || r_type == R_SH_GOTFUNCDESC
4068 || r_type == R_SH_GOTFUNCDESC20
4069 || r_type == R_SH_GOTOFFFUNCDESC
4070 || r_type == R_SH_GOTOFFFUNCDESC20
4071 || r_type == R_SH_FUNCDESC
4072 || r_type == R_SH_GOT_LOW16
4073 || r_type == R_SH_GOT_MEDLOW16
4074 || r_type == R_SH_GOT_MEDHI16
4075 || r_type == R_SH_GOT_HI16)
4076 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4078 || (! info->symbolic && h->dynindx != -1)
4079 || !h->def_regular))
4080 /* The cases above are those in which relocation is
4081 overwritten in the switch block below. The cases
4082 below are those in which we must defer relocation
4083 to run-time, because we can't resolve absolute
4084 addresses when creating a shared library. */
4086 && ((! info->symbolic && h->dynindx != -1)
4088 && ((r_type == R_SH_DIR32
4089 && !h->forced_local)
4090 || (r_type == R_SH_REL32
4091 && !SYMBOL_CALLS_LOCAL (info, h)))
4092 && ((input_section->flags & SEC_ALLOC) != 0
4093 /* DWARF will emit R_SH_DIR32 relocations in its
4094 sections against symbols defined externally
4095 in shared libraries. We can't do anything
4097 || ((input_section->flags & SEC_DEBUGGING) != 0
4098 && h->def_dynamic)))
4099 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4100 sections because such sections are not SEC_ALLOC and
4101 thus ld.so will not process them. */
4102 || (sec->output_section == NULL
4103 && ((input_section->flags & SEC_DEBUGGING) != 0
4105 || (sec->output_section == NULL
4106 && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
4107 || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
4109 else if (sec->output_section != NULL)
4110 relocation = ((h->root.u.def.value
4111 + sec->output_section->vma
4112 + sec->output_offset)
4113 /* A STO_SH5_ISA32 causes a "bitor 1" to the
4114 symbol value, unless we've seen
4115 STT_DATALABEL on the way to it. */
4116 | ((h->other & STO_SH5_ISA32) != 0
4117 && ! seen_stt_datalabel));
4118 else if (!info->relocatable
4119 && (_bfd_elf_section_offset (output_bfd, info,
4124 (*_bfd_error_handler)
4125 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4128 (long) rel->r_offset,
4130 h->root.root.string);
4134 else if (h->root.type == bfd_link_hash_undefweak)
4136 else if (info->unresolved_syms_in_objects == RM_IGNORE
4137 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4139 else if (!info->relocatable)
4141 if (! info->callbacks->undefined_symbol
4142 (info, h->root.root.string, input_bfd,
4143 input_section, rel->r_offset,
4144 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
4145 || ELF_ST_VISIBILITY (h->other))))
4150 if (sec != NULL && discarded_section (sec))
4151 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4152 rel, 1, relend, howto, 0, contents);
4154 if (info->relocatable)
4157 /* Check for inter-segment relocations in FDPIC files. Most
4158 relocations connect the relocation site to the location of
4159 the target symbol, but there are some exceptions below. */
4160 check_segment[0] = isec_segment;
4162 check_segment[1] = sh_elf_osec_to_segment (output_bfd,
4163 sec->output_section);
4165 check_segment[1] = -1;
4167 switch ((int) r_type)
4169 final_link_relocate:
4170 /* COFF relocs don't use the addend. The addend is used for
4171 R_SH_DIR32 to be compatible with other compilers. */
4172 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4173 contents, rel->r_offset,
4174 relocation, addend);
4178 goto final_link_relocate;
4183 /* If the reloc is against the start of this section, then
4184 the assembler has already taken care of it and the reloc
4185 is here only to assist in relaxing. If the reloc is not
4186 against the start of this section, then it's against an
4187 external symbol and we must deal with it ourselves. */
4188 if (input_section->output_section->vma + input_section->output_offset
4191 int disp = (relocation
4192 - input_section->output_section->vma
4193 - input_section->output_offset
4199 case R_SH_DIR8WPZ: mask = 1; break;
4200 case R_SH_DIR8WPL: mask = 3; break;
4201 default: mask = 0; break;
4205 ((*_bfd_error_handler)
4206 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
4207 input_section->owner,
4208 (unsigned long) rel->r_offset));
4209 bfd_set_error (bfd_error_bad_value);
4213 goto final_link_relocate;
4219 #ifdef INCLUDE_SHMEDIA
4220 if (shmedia_prepare_reloc (info, input_bfd, input_section,
4221 contents, rel, &relocation))
4222 goto final_link_relocate;
4224 bfd_set_error (bfd_error_bad_value);
4232 goto final_link_relocate;
4238 ((*_bfd_error_handler)
4239 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4240 input_section->owner,
4241 (unsigned long) rel->r_offset, howto->name,
4242 (unsigned long) relocation));
4243 bfd_set_error (bfd_error_bad_value);
4246 goto final_link_relocate;
4253 ((*_bfd_error_handler)
4254 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
4255 input_section->owner,
4256 (unsigned long) rel->r_offset, howto->name,
4257 (unsigned long) relocation));
4258 bfd_set_error (bfd_error_bad_value);
4261 goto final_link_relocate;
4264 if ((signed int)relocation < -32
4265 || (signed int)relocation > 32)
4267 ((*_bfd_error_handler)
4268 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
4269 input_section->owner,
4270 (unsigned long) rel->r_offset,
4271 (unsigned long) relocation));
4272 bfd_set_error (bfd_error_bad_value);
4275 goto final_link_relocate;
4278 if ((signed int)relocation < -16
4279 || (signed int)relocation > 16)
4281 ((*_bfd_error_handler)
4282 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
4283 input_section->owner,
4284 (unsigned long) rel->r_offset,
4285 (unsigned long) relocation));
4286 bfd_set_error (bfd_error_bad_value);
4289 goto final_link_relocate;
4293 #ifdef INCLUDE_SHMEDIA
4294 case R_SH_IMM_LOW16_PCREL:
4295 case R_SH_IMM_MEDLOW16_PCREL:
4296 case R_SH_IMM_MEDHI16_PCREL:
4297 case R_SH_IMM_HI16_PCREL:
4301 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4302 || h->root.type != bfd_link_hash_undefweak)
4303 && r_symndx != STN_UNDEF
4304 && (input_section->flags & SEC_ALLOC) != 0
4306 && (r_type == R_SH_DIR32
4307 || !SYMBOL_CALLS_LOCAL (info, h)))
4309 Elf_Internal_Rela outrel;
4311 bfd_boolean skip, relocate;
4313 /* When generating a shared object, these relocations
4314 are copied into the output file to be resolved at run
4319 sreloc = _bfd_elf_get_dynamic_reloc_section
4320 (input_bfd, input_section, /*rela?*/ TRUE);
4329 _bfd_elf_section_offset (output_bfd, info, input_section,
4331 if (outrel.r_offset == (bfd_vma) -1)
4333 else if (outrel.r_offset == (bfd_vma) -2)
4334 skip = TRUE, relocate = TRUE;
4335 outrel.r_offset += (input_section->output_section->vma
4336 + input_section->output_offset);
4339 memset (&outrel, 0, sizeof outrel);
4340 else if (r_type == R_SH_REL32)
4342 BFD_ASSERT (h != NULL && h->dynindx != -1);
4343 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4345 = (howto->partial_inplace
4346 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4349 #ifdef INCLUDE_SHMEDIA
4350 else if (r_type == R_SH_IMM_LOW16_PCREL
4351 || r_type == R_SH_IMM_MEDLOW16_PCREL
4352 || r_type == R_SH_IMM_MEDHI16_PCREL
4353 || r_type == R_SH_IMM_HI16_PCREL)
4355 BFD_ASSERT (h != NULL && h->dynindx != -1);
4356 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4357 outrel.r_addend = addend;
4362 || ((info->symbolic || h->dynindx == -1)
4363 && h->def_regular)))
4367 BFD_ASSERT (sec != NULL);
4368 BFD_ASSERT (sec->output_section != NULL);
4369 dynindx = elf_section_data (sec->output_section)->dynindx;
4370 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4371 outrel.r_addend = relocation;
4373 += (howto->partial_inplace
4374 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4376 outrel.r_addend -= sec->output_section->vma;
4380 /* h->dynindx may be -1 if this symbol was marked to
4383 || ((info->symbolic || h->dynindx == -1)
4386 relocate = howto->partial_inplace;
4387 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4391 BFD_ASSERT (h->dynindx != -1);
4392 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4394 outrel.r_addend = relocation;
4396 += (howto->partial_inplace
4397 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4401 loc = sreloc->contents;
4402 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4403 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4405 check_segment[0] = check_segment[1] = -1;
4407 /* If this reloc is against an external symbol, we do
4408 not want to fiddle with the addend. Otherwise, we
4409 need to include the symbol value so that it becomes
4410 an addend for the dynamic reloc. */
4414 else if (fdpic_p && !info->shared
4415 && r_type == R_SH_DIR32
4416 && (input_section->flags & SEC_ALLOC) != 0)
4422 if (sh_elf_osec_readonly_p (output_bfd,
4423 input_section->output_section))
4425 (*_bfd_error_handler)
4426 (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"),
4429 (long) rel->r_offset,
4434 offset = _bfd_elf_section_offset (output_bfd, info,
4435 input_section, rel->r_offset);
4436 if (offset != (bfd_vma)-1)
4437 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4438 input_section->output_section->vma
4439 + input_section->output_offset
4442 check_segment[0] = check_segment[1] = -1;
4444 /* We don't want warnings for non-NULL tests on undefined weak
4446 else if (r_type == R_SH_REL32
4448 && h->root.type == bfd_link_hash_undefweak)
4449 check_segment[0] = check_segment[1] = -1;
4450 goto final_link_relocate;
4453 #ifdef INCLUDE_SHMEDIA
4454 case R_SH_GOTPLT_LOW16:
4455 case R_SH_GOTPLT_MEDLOW16:
4456 case R_SH_GOTPLT_MEDHI16:
4457 case R_SH_GOTPLT_HI16:
4458 case R_SH_GOTPLT10BY4:
4459 case R_SH_GOTPLT10BY8:
4461 /* Relocation is to the entry for this symbol in the
4462 procedure linkage table. */
4469 || h->plt.offset == (bfd_vma) -1
4470 || h->got.offset != (bfd_vma) -1)
4473 /* Relocation is to the entry for this symbol in the global
4474 offset table extension for the procedure linkage table. */
4477 BFD_ASSERT (sgotplt != NULL);
4478 relocation = (sgotplt->output_offset
4479 + (get_plt_index (htab->plt_info, h->plt.offset)
4483 relocation -= GOT_BIAS;
4486 goto final_link_relocate;
4491 #ifdef INCLUDE_SHMEDIA
4492 case R_SH_GOT_LOW16:
4493 case R_SH_GOT_MEDLOW16:
4494 case R_SH_GOT_MEDHI16:
4499 /* Relocation is to the entry for this symbol in the global
4503 BFD_ASSERT (sgot != NULL);
4504 check_segment[0] = check_segment[1] = -1;
4510 off = h->got.offset;
4511 #ifdef INCLUDE_SHMEDIA
4512 if (seen_stt_datalabel)
4514 struct elf_sh_link_hash_entry *hsh;
4516 hsh = (struct elf_sh_link_hash_entry *)h;
4517 off = hsh->datalabel_got.offset;
4520 BFD_ASSERT (off != (bfd_vma) -1);
4522 dyn = htab->root.dynamic_sections_created;
4523 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4525 && SYMBOL_REFERENCES_LOCAL (info, h))
4526 || (ELF_ST_VISIBILITY (h->other)
4527 && h->root.type == bfd_link_hash_undefweak))
4529 /* This is actually a static link, or it is a
4530 -Bsymbolic link and the symbol is defined
4531 locally, or the symbol was forced to be local
4532 because of a version file. We must initialize
4533 this entry in the global offset table. Since the
4534 offset must always be a multiple of 4, we use the
4535 least significant bit to record whether we have
4536 initialized it already.
4538 When doing a dynamic link, we create a .rela.got
4539 relocation entry to initialize the value. This
4540 is done in the finish_dynamic_symbol routine. */
4545 bfd_put_32 (output_bfd, relocation,
4546 sgot->contents + off);
4547 #ifdef INCLUDE_SHMEDIA
4548 if (seen_stt_datalabel)
4550 struct elf_sh_link_hash_entry *hsh;
4552 hsh = (struct elf_sh_link_hash_entry *)h;
4553 hsh->datalabel_got.offset |= 1;
4559 /* If we initialize the GOT entry here with a valid
4560 symbol address, also add a fixup. */
4561 if (fdpic_p && !info->shared
4562 && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
4563 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4564 || h->root.type != bfd_link_hash_undefweak))
4565 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4566 sgot->output_section->vma
4567 + sgot->output_offset
4572 relocation = sh_elf_got_offset (htab) + off;
4576 #ifdef INCLUDE_SHMEDIA
4579 BFD_ASSERT (local_got_offsets != NULL
4580 && (local_got_offsets[symtab_hdr->sh_info
4584 off = local_got_offsets[symtab_hdr->sh_info
4590 BFD_ASSERT (local_got_offsets != NULL
4591 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4593 off = local_got_offsets[r_symndx];
4594 #ifdef INCLUDE_SHMEDIA
4598 /* The offset must always be a multiple of 4. We use
4599 the least significant bit to record whether we have
4600 already generated the necessary reloc. */
4605 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4609 Elf_Internal_Rela outrel;
4612 if (srelgot == NULL)
4614 srelgot = bfd_get_linker_section (dynobj,
4616 BFD_ASSERT (srelgot != NULL);
4619 outrel.r_offset = (sgot->output_section->vma
4620 + sgot->output_offset
4625 = elf_section_data (sec->output_section)->dynindx;
4626 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4627 outrel.r_addend = relocation;
4628 outrel.r_addend -= sec->output_section->vma;
4632 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4633 outrel.r_addend = relocation;
4635 loc = srelgot->contents;
4636 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4637 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4640 && (sh_elf_local_got_type (input_bfd) [r_symndx]
4642 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4643 sgot->output_section->vma
4644 + sgot->output_offset
4647 #ifdef INCLUDE_SHMEDIA
4649 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
4652 local_got_offsets[r_symndx] |= 1;
4655 relocation = sh_elf_got_offset (htab) + off;
4659 relocation -= GOT_BIAS;
4662 if (r_type == R_SH_GOT20)
4664 r = install_movi20_field (output_bfd, relocation + addend,
4665 input_bfd, input_section, contents,
4670 goto final_link_relocate;
4674 #ifdef INCLUDE_SHMEDIA
4675 case R_SH_GOTOFF_LOW16:
4676 case R_SH_GOTOFF_MEDLOW16:
4677 case R_SH_GOTOFF_MEDHI16:
4678 case R_SH_GOTOFF_HI16:
4680 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4681 we place at the start of the .got.plt section. This is the same
4682 as the start of the output .got section, unless there are function
4683 descriptors in front of it. */
4685 BFD_ASSERT (sgotplt != NULL);
4686 check_segment[0] = got_segment;
4687 relocation -= sgotplt->output_section->vma + sgotplt->output_offset
4688 + htab->root.hgot->root.u.def.value;
4691 relocation -= GOT_BIAS;
4694 addend = rel->r_addend;
4696 if (r_type == R_SH_GOTOFF20)
4698 r = install_movi20_field (output_bfd, relocation + addend,
4699 input_bfd, input_section, contents,
4704 goto final_link_relocate;
4707 #ifdef INCLUDE_SHMEDIA
4708 case R_SH_GOTPC_LOW16:
4709 case R_SH_GOTPC_MEDLOW16:
4710 case R_SH_GOTPC_MEDHI16:
4711 case R_SH_GOTPC_HI16:
4713 /* Use global offset table as symbol value. */
4715 BFD_ASSERT (sgotplt != NULL);
4716 relocation = sgotplt->output_section->vma + sgotplt->output_offset;
4719 relocation += GOT_BIAS;
4722 addend = rel->r_addend;
4724 goto final_link_relocate;
4727 #ifdef INCLUDE_SHMEDIA
4728 case R_SH_PLT_LOW16:
4729 case R_SH_PLT_MEDLOW16:
4730 case R_SH_PLT_MEDHI16:
4733 /* Relocation is to the entry for this symbol in the
4734 procedure linkage table. */
4736 /* Resolve a PLT reloc against a local symbol directly,
4737 without using the procedure linkage table. */
4739 goto final_link_relocate;
4741 /* We don't want to warn on calls to undefined weak symbols,
4742 as calls to them must be protected by non-NULL tests
4743 anyway, and unprotected calls would invoke undefined
4745 if (h->root.type == bfd_link_hash_undefweak)
4746 check_segment[0] = check_segment[1] = -1;
4748 if (h->forced_local)
4749 goto final_link_relocate;
4751 if (h->plt.offset == (bfd_vma) -1)
4753 /* We didn't make a PLT entry for this symbol. This
4754 happens when statically linking PIC code, or when
4755 using -Bsymbolic. */
4756 goto final_link_relocate;
4759 BFD_ASSERT (splt != NULL);
4760 check_segment[1] = plt_segment;
4761 relocation = (splt->output_section->vma
4762 + splt->output_offset
4765 #ifdef INCLUDE_SHMEDIA
4769 addend = rel->r_addend;
4771 goto final_link_relocate;
4773 /* Relocation is to the canonical function descriptor for this
4774 symbol, possibly via the GOT. Initialize the GOT
4775 entry and function descriptor if necessary. */
4776 case R_SH_GOTFUNCDESC:
4777 case R_SH_GOTFUNCDESC20:
4781 asection *reloc_section;
4782 bfd_vma reloc_offset;
4783 int reloc_type = R_SH_FUNCDESC;
4787 check_segment[0] = check_segment[1] = -1;
4789 /* FIXME: See what FRV does for global symbols in the
4790 executable, with --export-dynamic. Do they need ld.so
4791 to allocate official descriptors? See what this code
4797 if (r_type == R_SH_FUNCDESC)
4799 reloc_section = input_section;
4800 reloc_offset = rel->r_offset;
4804 reloc_section = sgot;
4807 reloc_offset = h->got.offset;
4810 BFD_ASSERT (local_got_offsets != NULL);
4811 reloc_offset = local_got_offsets[r_symndx];
4813 BFD_ASSERT (reloc_offset != MINUS_ONE);
4815 if (reloc_offset & 1)
4818 goto funcdesc_done_got;
4822 if (h && h->root.type == bfd_link_hash_undefweak
4823 && (SYMBOL_CALLS_LOCAL (info, h)
4824 || !htab->root.dynamic_sections_created))
4825 /* Undefined weak symbol which will not be dynamically
4826 resolved later; leave it at zero. */
4827 goto funcdesc_leave_zero;
4828 else if (SYMBOL_CALLS_LOCAL (info, h)
4829 && ! SYMBOL_FUNCDESC_LOCAL (info, h))
4831 /* If the symbol needs a non-local function descriptor
4832 but binds locally (i.e., its visibility is
4833 protected), emit a dynamic relocation decayed to
4834 section+offset. This is an optimization; the dynamic
4835 linker would resolve our function descriptor request
4836 to our copy of the function anyway. */
4837 dynindx = elf_section_data (h->root.u.def.section
4838 ->output_section)->dynindx;
4839 relocation += h->root.u.def.section->output_offset
4840 + h->root.u.def.value;
4842 else if (! SYMBOL_FUNCDESC_LOCAL (info, h))
4844 /* If the symbol is dynamic and there will be dynamic
4845 symbol resolution because we are or are linked with a
4846 shared library, emit a FUNCDESC relocation such that
4847 the dynamic linker will allocate the function
4849 BFD_ASSERT (h->dynindx != -1);
4850 dynindx = h->dynindx;
4856 /* Otherwise, we know we have a private function
4857 descriptor, so reference it directly. */
4858 reloc_type = R_SH_DIR32;
4859 dynindx = elf_section_data (htab->sfuncdesc
4860 ->output_section)->dynindx;
4864 offset = sh_elf_hash_entry (h)->funcdesc.offset;
4865 BFD_ASSERT (offset != MINUS_ONE);
4866 if ((offset & 1) == 0)
4868 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4871 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4876 union gotref *local_funcdesc;
4878 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4879 offset = local_funcdesc[r_symndx].offset;
4880 BFD_ASSERT (offset != MINUS_ONE);
4881 if ((offset & 1) == 0)
4883 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4887 local_funcdesc[r_symndx].offset |= 1;
4891 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4894 if (!info->shared && SYMBOL_FUNCDESC_LOCAL (info, h))
4898 if (sh_elf_osec_readonly_p (output_bfd,
4899 reloc_section->output_section))
4901 (*_bfd_error_handler)
4902 (_("%B(%A+0x%lx): cannot emit fixup to `%s' in read-only section"),
4905 (long) rel->r_offset,
4910 offset = _bfd_elf_section_offset (output_bfd, info,
4911 reloc_section, reloc_offset);
4913 if (offset != (bfd_vma)-1)
4914 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4916 + reloc_section->output_section->vma
4917 + reloc_section->output_offset);
4919 else if ((reloc_section->output_section->flags
4920 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
4924 if (sh_elf_osec_readonly_p (output_bfd,
4925 reloc_section->output_section))
4927 info->callbacks->warning
4929 _("cannot emit dynamic relocations in read-only section"),
4930 symname, input_bfd, reloc_section, reloc_offset);
4934 if (srelgot == NULL)
4936 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
4937 BFD_ASSERT (srelgot != NULL);
4940 offset = _bfd_elf_section_offset (output_bfd, info,
4941 reloc_section, reloc_offset);
4943 if (offset != (bfd_vma)-1)
4944 sh_elf_add_dyn_reloc (output_bfd, srelgot,
4946 + reloc_section->output_section->vma
4947 + reloc_section->output_offset,
4948 reloc_type, dynindx, relocation);
4950 if (r_type == R_SH_FUNCDESC)
4958 goto funcdesc_leave_zero;
4962 if (SYMBOL_FUNCDESC_LOCAL (info, h))
4963 relocation += htab->sfuncdesc->output_section->vma;
4964 funcdesc_leave_zero:
4965 if (r_type != R_SH_FUNCDESC)
4967 bfd_put_32 (output_bfd, relocation,
4968 reloc_section->contents + reloc_offset);
4972 local_got_offsets[r_symndx] |= 1;
4976 relocation = sh_elf_got_offset (htab) + reloc_offset;
4978 relocation -= GOT_BIAS;
4981 if (r_type == R_SH_GOTFUNCDESC20)
4983 r = install_movi20_field (output_bfd, relocation + addend,
4984 input_bfd, input_section, contents,
4989 goto final_link_relocate;
4993 case R_SH_GOTOFFFUNCDESC:
4994 case R_SH_GOTOFFFUNCDESC20:
4995 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4996 executable and --export-dynamic. If such symbols get
4997 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
5001 check_segment[0] = check_segment[1] = -1;
5003 addend = rel->r_addend;
5005 if (h && (h->root.type == bfd_link_hash_undefweak
5006 || !SYMBOL_FUNCDESC_LOCAL (info, h)))
5009 (_("%B(%A+0x%lx): %s relocation against external symbol \"%s\""),
5010 input_bfd, input_section, (long) rel->r_offset, howto->name,
5011 h->root.root.string);
5018 /* Otherwise, we know we have a private function
5019 descriptor, so reference it directly. */
5022 offset = sh_elf_hash_entry (h)->funcdesc.offset;
5023 BFD_ASSERT (offset != MINUS_ONE);
5024 if ((offset & 1) == 0)
5026 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
5029 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
5034 union gotref *local_funcdesc;
5036 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
5037 offset = local_funcdesc[r_symndx].offset;
5038 BFD_ASSERT (offset != MINUS_ONE);
5039 if ((offset & 1) == 0)
5041 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
5045 local_funcdesc[r_symndx].offset |= 1;
5049 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
5052 relocation -= (htab->root.hgot->root.u.def.value
5053 + sgotplt->output_offset);
5055 relocation -= GOT_BIAS;
5058 if (r_type == R_SH_GOTOFFFUNCDESC20)
5060 r = install_movi20_field (output_bfd, relocation + addend,
5061 input_bfd, input_section, contents,
5066 goto final_link_relocate;
5068 case R_SH_LOOP_START:
5070 static bfd_vma start, end;
5072 start = (relocation + rel->r_addend
5073 - (sec->output_section->vma + sec->output_offset));
5074 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5075 rel->r_offset, sec, start, end);
5079 end = (relocation + rel->r_addend
5080 - (sec->output_section->vma + sec->output_offset));
5081 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5082 rel->r_offset, sec, start, end);
5086 case R_SH_TLS_GD_32:
5087 case R_SH_TLS_IE_32:
5089 check_segment[0] = check_segment[1] = -1;
5090 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5091 got_type = GOT_UNKNOWN;
5092 if (h == NULL && local_got_offsets)
5093 got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
5096 got_type = sh_elf_hash_entry (h)->got_type;
5098 && (h->dynindx == -1
5100 r_type = R_SH_TLS_LE_32;
5103 if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
5104 r_type = R_SH_TLS_IE_32;
5106 if (r_type == R_SH_TLS_LE_32)
5109 unsigned short insn;
5111 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5113 /* GD->LE transition:
5114 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5115 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5116 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5118 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5120 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
5122 offset = rel->r_offset;
5123 BFD_ASSERT (offset >= 16);
5124 /* Size of GD instructions is 16 or 18. */
5126 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5127 if ((insn & 0xff00) == 0xc700)
5129 BFD_ASSERT (offset >= 2);
5131 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5134 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5135 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5136 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5137 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5138 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5139 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5140 BFD_ASSERT (insn == 0x310c);
5141 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5142 BFD_ASSERT (insn == 0x410b);
5143 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5144 BFD_ASSERT (insn == 0x34cc);
5146 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5147 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5148 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5149 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5150 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5156 /* IE->LE transition:
5157 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5158 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5160 mov.l .Ln,rM; stc gbr,rN; nop; ...;
5163 offset = rel->r_offset;
5164 BFD_ASSERT (offset >= 16);
5165 /* Size of IE instructions is 10 or 12. */
5167 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5168 if ((insn & 0xf0ff) == 0x0012)
5170 BFD_ASSERT (offset >= 2);
5172 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5175 BFD_ASSERT ((insn & 0xff00) == 0xd000);
5176 target = insn & 0x00ff;
5177 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5178 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5179 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5180 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5181 insn = 0xd000 | (insn & 0x0f00) | target;
5182 bfd_put_16 (output_bfd, insn, contents + offset + 0);
5183 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5186 bfd_put_32 (output_bfd, tpoff (info, relocation),
5187 contents + rel->r_offset);
5191 if (sgot == NULL || sgotplt == NULL)
5195 off = h->got.offset;
5198 if (local_got_offsets == NULL)
5201 off = local_got_offsets[r_symndx];
5204 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
5205 if (r_type == R_SH_TLS_IE_32
5206 && ! htab->root.dynamic_sections_created)
5209 bfd_put_32 (output_bfd, tpoff (info, relocation),
5210 sgot->contents + off);
5211 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5212 contents + rel->r_offset);
5220 Elf_Internal_Rela outrel;
5224 if (srelgot == NULL)
5226 srelgot = bfd_get_linker_section (dynobj, ".rela.got");
5227 BFD_ASSERT (srelgot != NULL);
5230 outrel.r_offset = (sgot->output_section->vma
5231 + sgot->output_offset + off);
5233 if (h == NULL || h->dynindx == -1)
5238 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5240 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5241 outrel.r_addend = relocation - dtpoff_base (info);
5243 outrel.r_addend = 0;
5244 outrel.r_info = ELF32_R_INFO (indx, dr_type);
5245 loc = srelgot->contents;
5246 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5247 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5249 if (r_type == R_SH_TLS_GD_32)
5253 bfd_put_32 (output_bfd,
5254 relocation - dtpoff_base (info),
5255 sgot->contents + off + 4);
5259 outrel.r_info = ELF32_R_INFO (indx,
5261 outrel.r_offset += 4;
5262 outrel.r_addend = 0;
5263 srelgot->reloc_count++;
5264 loc += sizeof (Elf32_External_Rela);
5265 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5272 local_got_offsets[r_symndx] |= 1;
5275 if (off >= (bfd_vma) -2)
5278 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5279 relocation = sh_elf_got_offset (htab) + off;
5283 unsigned short insn;
5285 /* GD->IE transition:
5286 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5287 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5288 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5290 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5291 nop; nop; bra 3f; nop; .align 2;
5292 1: .long x@TPOFF; 2:...; 3:. */
5294 offset = rel->r_offset;
5295 BFD_ASSERT (offset >= 16);
5296 /* Size of GD instructions is 16 or 18. */
5298 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5299 if ((insn & 0xff00) == 0xc700)
5301 BFD_ASSERT (offset >= 2);
5303 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5306 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5308 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
5309 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5311 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5312 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5313 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5314 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5315 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5316 BFD_ASSERT (insn == 0x310c);
5317 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5318 BFD_ASSERT (insn == 0x410b);
5319 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5320 BFD_ASSERT (insn == 0x34cc);
5322 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5323 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5324 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5325 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5326 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5328 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5329 contents + rel->r_offset);
5334 addend = rel->r_addend;
5336 goto final_link_relocate;
5338 case R_SH_TLS_LD_32:
5340 check_segment[0] = check_segment[1] = -1;
5344 unsigned short insn;
5346 /* LD->LE transition:
5347 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5348 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5349 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5351 stc gbr,r0; nop; nop; nop;
5352 nop; nop; bra 3f; ...; 3:. */
5354 offset = rel->r_offset;
5355 BFD_ASSERT (offset >= 16);
5356 /* Size of LD instructions is 16 or 18. */
5358 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5359 if ((insn & 0xff00) == 0xc700)
5361 BFD_ASSERT (offset >= 2);
5363 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5366 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5367 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5368 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5369 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5370 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5371 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5372 BFD_ASSERT (insn == 0x310c);
5373 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5374 BFD_ASSERT (insn == 0x410b);
5375 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5376 BFD_ASSERT (insn == 0x34cc);
5378 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5379 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5380 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5381 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5382 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5383 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5388 if (sgot == NULL || sgotplt == NULL)
5391 off = htab->tls_ldm_got.offset;
5396 Elf_Internal_Rela outrel;
5399 srelgot = htab->srelgot;
5400 if (srelgot == NULL)
5403 outrel.r_offset = (sgot->output_section->vma
5404 + sgot->output_offset + off);
5405 outrel.r_addend = 0;
5406 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5407 loc = srelgot->contents;
5408 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5409 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5410 htab->tls_ldm_got.offset |= 1;
5413 relocation = sh_elf_got_offset (htab) + off;
5414 addend = rel->r_addend;
5416 goto final_link_relocate;
5418 case R_SH_TLS_LDO_32:
5419 check_segment[0] = check_segment[1] = -1;
5421 relocation = tpoff (info, relocation);
5423 relocation -= dtpoff_base (info);
5425 addend = rel->r_addend;
5426 goto final_link_relocate;
5428 case R_SH_TLS_LE_32:
5431 Elf_Internal_Rela outrel;
5434 check_segment[0] = check_segment[1] = -1;
5436 if (! info->shared || info->pie)
5438 relocation = tpoff (info, relocation);
5439 addend = rel->r_addend;
5440 goto final_link_relocate;
5445 sreloc = _bfd_elf_get_dynamic_reloc_section
5446 (input_bfd, input_section, /*rela?*/ TRUE);
5451 if (h == NULL || h->dynindx == -1)
5456 outrel.r_offset = (input_section->output_section->vma
5457 + input_section->output_offset
5459 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5461 outrel.r_addend = relocation - dtpoff_base (info);
5463 outrel.r_addend = 0;
5465 loc = sreloc->contents;
5466 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5467 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5473 if (fdpic_p && check_segment[0] != (unsigned) -1
5474 && check_segment[0] != check_segment[1])
5476 /* We don't want duplicate errors for undefined symbols. */
5477 if (!h || h->root.type != bfd_link_hash_undefined)
5481 info->callbacks->einfo
5482 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5483 input_bfd, input_section, rel->r_offset, symname);
5487 info->callbacks->einfo
5488 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5489 input_bfd, input_section, rel->r_offset, symname);
5492 elf_elfheader (output_bfd)->e_flags &= ~EF_SH_PIC;
5495 if (r != bfd_reloc_ok)
5500 case bfd_reloc_outofrange:
5502 case bfd_reloc_overflow:
5510 name = (bfd_elf_string_from_elf_section
5511 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5515 name = bfd_section_name (input_bfd, sec);
5517 if (! ((*info->callbacks->reloc_overflow)
5518 (info, (h ? &h->root : NULL), name, howto->name,
5519 (bfd_vma) 0, input_bfd, input_section,
5531 /* This is a version of bfd_generic_get_relocated_section_contents
5532 which uses sh_elf_relocate_section. */
5535 sh_elf_get_relocated_section_contents (bfd *output_bfd,
5536 struct bfd_link_info *link_info,
5537 struct bfd_link_order *link_order,
5539 bfd_boolean relocatable,
5542 Elf_Internal_Shdr *symtab_hdr;
5543 asection *input_section = link_order->u.indirect.section;
5544 bfd *input_bfd = input_section->owner;
5545 asection **sections = NULL;
5546 Elf_Internal_Rela *internal_relocs = NULL;
5547 Elf_Internal_Sym *isymbuf = NULL;
5549 /* We only need to handle the case of relaxing, or of having a
5550 particular set of section contents, specially. */
5552 || elf_section_data (input_section)->this_hdr.contents == NULL)
5553 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5558 symtab_hdr = &elf_symtab_hdr (input_bfd);
5560 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5561 (size_t) input_section->size);
5563 if ((input_section->flags & SEC_RELOC) != 0
5564 && input_section->reloc_count > 0)
5567 Elf_Internal_Sym *isym, *isymend;
5570 internal_relocs = (_bfd_elf_link_read_relocs
5571 (input_bfd, input_section, NULL,
5572 (Elf_Internal_Rela *) NULL, FALSE));
5573 if (internal_relocs == NULL)
5576 if (symtab_hdr->sh_info != 0)
5578 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5579 if (isymbuf == NULL)
5580 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5581 symtab_hdr->sh_info, 0,
5583 if (isymbuf == NULL)
5587 amt = symtab_hdr->sh_info;
5588 amt *= sizeof (asection *);
5589 sections = (asection **) bfd_malloc (amt);
5590 if (sections == NULL && amt != 0)
5593 isymend = isymbuf + symtab_hdr->sh_info;
5594 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5598 if (isym->st_shndx == SHN_UNDEF)
5599 isec = bfd_und_section_ptr;
5600 else if (isym->st_shndx == SHN_ABS)
5601 isec = bfd_abs_section_ptr;
5602 else if (isym->st_shndx == SHN_COMMON)
5603 isec = bfd_com_section_ptr;
5605 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5610 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5611 input_section, data, internal_relocs,
5615 if (sections != NULL)
5618 && symtab_hdr->contents != (unsigned char *) isymbuf)
5620 if (elf_section_data (input_section)->relocs != internal_relocs)
5621 free (internal_relocs);
5627 if (sections != NULL)
5630 && symtab_hdr->contents != (unsigned char *) isymbuf)
5632 if (internal_relocs != NULL
5633 && elf_section_data (input_section)->relocs != internal_relocs)
5634 free (internal_relocs);
5638 /* Return the base VMA address which should be subtracted from real addresses
5639 when resolving @dtpoff relocation.
5640 This is PT_TLS segment p_vaddr. */
5643 dtpoff_base (struct bfd_link_info *info)
5645 /* If tls_sec is NULL, we should have signalled an error already. */
5646 if (elf_hash_table (info)->tls_sec == NULL)
5648 return elf_hash_table (info)->tls_sec->vma;
5651 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5654 tpoff (struct bfd_link_info *info, bfd_vma address)
5656 /* If tls_sec is NULL, we should have signalled an error already. */
5657 if (elf_hash_table (info)->tls_sec == NULL)
5659 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5660 structure which has 2 pointer fields. */
5661 return (address - elf_hash_table (info)->tls_sec->vma
5662 + align_power ((bfd_vma) 8,
5663 elf_hash_table (info)->tls_sec->alignment_power));
5667 sh_elf_gc_mark_hook (asection *sec,
5668 struct bfd_link_info *info,
5669 Elf_Internal_Rela *rel,
5670 struct elf_link_hash_entry *h,
5671 Elf_Internal_Sym *sym)
5674 switch (ELF32_R_TYPE (rel->r_info))
5676 case R_SH_GNU_VTINHERIT:
5677 case R_SH_GNU_VTENTRY:
5681 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5684 /* Update the got entry reference counts for the section being removed. */
5687 sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5688 asection *sec, const Elf_Internal_Rela *relocs)
5690 Elf_Internal_Shdr *symtab_hdr;
5691 struct elf_link_hash_entry **sym_hashes;
5692 bfd_signed_vma *local_got_refcounts;
5693 union gotref *local_funcdesc;
5694 const Elf_Internal_Rela *rel, *relend;
5696 if (info->relocatable)
5699 elf_section_data (sec)->local_dynrel = NULL;
5701 symtab_hdr = &elf_symtab_hdr (abfd);
5702 sym_hashes = elf_sym_hashes (abfd);
5703 local_got_refcounts = elf_local_got_refcounts (abfd);
5704 local_funcdesc = sh_elf_local_funcdesc (abfd);
5706 relend = relocs + sec->reloc_count;
5707 for (rel = relocs; rel < relend; rel++)
5709 unsigned long r_symndx;
5710 unsigned int r_type;
5711 struct elf_link_hash_entry *h = NULL;
5712 #ifdef INCLUDE_SHMEDIA
5713 int seen_stt_datalabel = 0;
5716 r_symndx = ELF32_R_SYM (rel->r_info);
5717 if (r_symndx >= symtab_hdr->sh_info)
5719 struct elf_sh_link_hash_entry *eh;
5720 struct elf_sh_dyn_relocs **pp;
5721 struct elf_sh_dyn_relocs *p;
5723 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5724 while (h->root.type == bfd_link_hash_indirect
5725 || h->root.type == bfd_link_hash_warning)
5727 #ifdef INCLUDE_SHMEDIA
5728 seen_stt_datalabel |= h->type == STT_DATALABEL;
5730 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5732 eh = (struct elf_sh_link_hash_entry *) h;
5733 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5736 /* Everything must go for SEC. */
5742 r_type = ELF32_R_TYPE (rel->r_info);
5743 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
5745 case R_SH_TLS_LD_32:
5746 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
5747 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
5755 #ifdef INCLUDE_SHMEDIA
5756 case R_SH_GOT_LOW16:
5757 case R_SH_GOT_MEDLOW16:
5758 case R_SH_GOT_MEDHI16:
5762 case R_SH_GOTOFF_LOW16:
5763 case R_SH_GOTOFF_MEDLOW16:
5764 case R_SH_GOTOFF_MEDHI16:
5765 case R_SH_GOTOFF_HI16:
5766 case R_SH_GOTPC_LOW16:
5767 case R_SH_GOTPC_MEDLOW16:
5768 case R_SH_GOTPC_MEDHI16:
5769 case R_SH_GOTPC_HI16:
5771 case R_SH_TLS_GD_32:
5772 case R_SH_TLS_IE_32:
5773 case R_SH_GOTFUNCDESC:
5774 case R_SH_GOTFUNCDESC20:
5777 #ifdef INCLUDE_SHMEDIA
5778 if (seen_stt_datalabel)
5780 struct elf_sh_link_hash_entry *eh;
5781 eh = (struct elf_sh_link_hash_entry *) h;
5782 if (eh->datalabel_got.refcount > 0)
5783 eh->datalabel_got.refcount -= 1;
5787 if (h->got.refcount > 0)
5788 h->got.refcount -= 1;
5790 else if (local_got_refcounts != NULL)
5792 #ifdef INCLUDE_SHMEDIA
5793 if (rel->r_addend & 1)
5795 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5796 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5800 if (local_got_refcounts[r_symndx] > 0)
5801 local_got_refcounts[r_symndx] -= 1;
5807 sh_elf_hash_entry (h)->abs_funcdesc_refcount -= 1;
5808 else if (sh_elf_hash_table (info)->fdpic_p && !info->shared)
5809 sh_elf_hash_table (info)->srofixup->size -= 4;
5813 case R_SH_GOTOFFFUNCDESC:
5814 case R_SH_GOTOFFFUNCDESC20:
5816 sh_elf_hash_entry (h)->funcdesc.refcount -= 1;
5818 local_funcdesc[r_symndx].refcount -= 1;
5822 if (sh_elf_hash_table (info)->fdpic_p && !info->shared
5823 && (sec->flags & SEC_ALLOC) != 0)
5824 sh_elf_hash_table (info)->srofixup->size -= 4;
5833 #ifdef INCLUDE_SHMEDIA
5834 case R_SH_PLT_LOW16:
5835 case R_SH_PLT_MEDLOW16:
5836 case R_SH_PLT_MEDHI16:
5841 if (h->plt.refcount > 0)
5842 h->plt.refcount -= 1;
5847 #ifdef INCLUDE_SHMEDIA
5848 case R_SH_GOTPLT_LOW16:
5849 case R_SH_GOTPLT_MEDLOW16:
5850 case R_SH_GOTPLT_MEDHI16:
5851 case R_SH_GOTPLT_HI16:
5852 case R_SH_GOTPLT10BY4:
5853 case R_SH_GOTPLT10BY8:
5857 struct elf_sh_link_hash_entry *eh;
5858 eh = (struct elf_sh_link_hash_entry *) h;
5859 if (eh->gotplt_refcount > 0)
5861 eh->gotplt_refcount -= 1;
5862 if (h->plt.refcount > 0)
5863 h->plt.refcount -= 1;
5865 #ifdef INCLUDE_SHMEDIA
5866 else if (seen_stt_datalabel)
5868 if (eh->datalabel_got.refcount > 0)
5869 eh->datalabel_got.refcount -= 1;
5872 else if (h->got.refcount > 0)
5873 h->got.refcount -= 1;
5875 else if (local_got_refcounts != NULL)
5877 #ifdef INCLUDE_SHMEDIA
5878 if (rel->r_addend & 1)
5880 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
5881 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
5885 if (local_got_refcounts[r_symndx] > 0)
5886 local_got_refcounts[r_symndx] -= 1;
5898 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5901 sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
5902 struct elf_link_hash_entry *dir,
5903 struct elf_link_hash_entry *ind)
5905 struct elf_sh_link_hash_entry *edir, *eind;
5907 edir = (struct elf_sh_link_hash_entry *) dir;
5908 eind = (struct elf_sh_link_hash_entry *) ind;
5910 if (eind->dyn_relocs != NULL)
5912 if (edir->dyn_relocs != NULL)
5914 struct elf_sh_dyn_relocs **pp;
5915 struct elf_sh_dyn_relocs *p;
5917 /* Add reloc counts against the indirect sym to the direct sym
5918 list. Merge any entries against the same section. */
5919 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5921 struct elf_sh_dyn_relocs *q;
5923 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5924 if (q->sec == p->sec)
5926 q->pc_count += p->pc_count;
5927 q->count += p->count;
5934 *pp = edir->dyn_relocs;
5937 edir->dyn_relocs = eind->dyn_relocs;
5938 eind->dyn_relocs = NULL;
5940 edir->gotplt_refcount = eind->gotplt_refcount;
5941 eind->gotplt_refcount = 0;
5942 #ifdef INCLUDE_SHMEDIA
5943 edir->datalabel_got.refcount += eind->datalabel_got.refcount;
5944 eind->datalabel_got.refcount = 0;
5946 edir->funcdesc.refcount += eind->funcdesc.refcount;
5947 eind->funcdesc.refcount = 0;
5948 edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
5949 eind->abs_funcdesc_refcount = 0;
5951 if (ind->root.type == bfd_link_hash_indirect
5952 && dir->got.refcount <= 0)
5954 edir->got_type = eind->got_type;
5955 eind->got_type = GOT_UNKNOWN;
5958 if (ind->root.type != bfd_link_hash_indirect
5959 && dir->dynamic_adjusted)
5961 /* If called to transfer flags for a weakdef during processing
5962 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5963 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5964 dir->ref_dynamic |= ind->ref_dynamic;
5965 dir->ref_regular |= ind->ref_regular;
5966 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
5967 dir->needs_plt |= ind->needs_plt;
5970 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
5974 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
5982 case R_SH_TLS_GD_32:
5983 case R_SH_TLS_IE_32:
5985 return R_SH_TLS_LE_32;
5986 return R_SH_TLS_IE_32;
5987 case R_SH_TLS_LD_32:
5988 return R_SH_TLS_LE_32;
5994 /* Look through the relocs for a section during the first phase.
5995 Since we don't do .gots or .plts, we just need to consider the
5996 virtual table relocs for gc. */
5999 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
6000 const Elf_Internal_Rela *relocs)
6002 Elf_Internal_Shdr *symtab_hdr;
6003 struct elf_link_hash_entry **sym_hashes;
6004 struct elf_sh_link_hash_table *htab;
6005 const Elf_Internal_Rela *rel;
6006 const Elf_Internal_Rela *rel_end;
6008 unsigned int r_type;
6009 enum got_type got_type, old_got_type;
6013 if (info->relocatable)
6016 BFD_ASSERT (is_sh_elf (abfd));
6018 symtab_hdr = &elf_symtab_hdr (abfd);
6019 sym_hashes = elf_sym_hashes (abfd);
6021 htab = sh_elf_hash_table (info);
6025 rel_end = relocs + sec->reloc_count;
6026 for (rel = relocs; rel < rel_end; rel++)
6028 struct elf_link_hash_entry *h;
6029 unsigned long r_symndx;
6030 #ifdef INCLUDE_SHMEDIA
6031 int seen_stt_datalabel = 0;
6034 r_symndx = ELF32_R_SYM (rel->r_info);
6035 r_type = ELF32_R_TYPE (rel->r_info);
6037 if (r_symndx < symtab_hdr->sh_info)
6041 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6042 while (h->root.type == bfd_link_hash_indirect
6043 || h->root.type == bfd_link_hash_warning)
6045 #ifdef INCLUDE_SHMEDIA
6046 seen_stt_datalabel |= h->type == STT_DATALABEL;
6048 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6051 /* PR15323, ref flags aren't set for references in the same
6053 h->root.non_ir_ref = 1;
6056 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
6058 && r_type == R_SH_TLS_IE_32
6060 && h->root.type != bfd_link_hash_undefined
6061 && h->root.type != bfd_link_hash_undefweak
6062 && (h->dynindx == -1
6064 r_type = R_SH_TLS_LE_32;
6069 case R_SH_GOTOFFFUNCDESC:
6070 case R_SH_GOTOFFFUNCDESC20:
6072 case R_SH_GOTFUNCDESC:
6073 case R_SH_GOTFUNCDESC20:
6076 if (h->dynindx == -1)
6077 switch (ELF_ST_VISIBILITY (h->other))
6083 bfd_elf_link_record_dynamic_symbol (info, h);
6090 /* Some relocs require a global offset table. */
6091 if (htab->sgot == NULL)
6096 /* This may require an rofixup. */
6105 case R_SH_GOTFUNCDESC:
6106 case R_SH_GOTFUNCDESC20:
6107 case R_SH_GOTOFFFUNCDESC:
6108 case R_SH_GOTOFFFUNCDESC20:
6110 #ifdef INCLUDE_SHMEDIA
6111 case R_SH_GOTPLT_LOW16:
6112 case R_SH_GOTPLT_MEDLOW16:
6113 case R_SH_GOTPLT_MEDHI16:
6114 case R_SH_GOTPLT_HI16:
6115 case R_SH_GOTPLT10BY4:
6116 case R_SH_GOTPLT10BY8:
6117 case R_SH_GOT_LOW16:
6118 case R_SH_GOT_MEDLOW16:
6119 case R_SH_GOT_MEDHI16:
6123 case R_SH_GOTOFF_LOW16:
6124 case R_SH_GOTOFF_MEDLOW16:
6125 case R_SH_GOTOFF_MEDHI16:
6126 case R_SH_GOTOFF_HI16:
6127 case R_SH_GOTPC_LOW16:
6128 case R_SH_GOTPC_MEDLOW16:
6129 case R_SH_GOTPC_MEDHI16:
6130 case R_SH_GOTPC_HI16:
6132 case R_SH_TLS_GD_32:
6133 case R_SH_TLS_LD_32:
6134 case R_SH_TLS_IE_32:
6135 if (htab->root.dynobj == NULL)
6136 htab->root.dynobj = abfd;
6137 if (!create_got_section (htab->root.dynobj, info))
6148 /* This relocation describes the C++ object vtable hierarchy.
6149 Reconstruct it for later use during GC. */
6150 case R_SH_GNU_VTINHERIT:
6151 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6155 /* This relocation describes which C++ vtable entries are actually
6156 used. Record for later use during GC. */
6157 case R_SH_GNU_VTENTRY:
6158 BFD_ASSERT (h != NULL);
6160 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6164 case R_SH_TLS_IE_32:
6166 info->flags |= DF_STATIC_TLS;
6170 case R_SH_TLS_GD_32:
6173 #ifdef INCLUDE_SHMEDIA
6174 case R_SH_GOT_LOW16:
6175 case R_SH_GOT_MEDLOW16:
6176 case R_SH_GOT_MEDHI16:
6181 case R_SH_GOTFUNCDESC:
6182 case R_SH_GOTFUNCDESC20:
6186 got_type = GOT_NORMAL;
6188 case R_SH_TLS_GD_32:
6189 got_type = GOT_TLS_GD;
6191 case R_SH_TLS_IE_32:
6192 got_type = GOT_TLS_IE;
6194 case R_SH_GOTFUNCDESC:
6195 case R_SH_GOTFUNCDESC20:
6196 got_type = GOT_FUNCDESC;
6202 #ifdef INCLUDE_SHMEDIA
6203 if (seen_stt_datalabel)
6205 struct elf_sh_link_hash_entry *eh
6206 = (struct elf_sh_link_hash_entry *) h;
6208 eh->datalabel_got.refcount += 1;
6212 h->got.refcount += 1;
6213 old_got_type = sh_elf_hash_entry (h)->got_type;
6217 bfd_signed_vma *local_got_refcounts;
6219 /* This is a global offset table entry for a local
6221 local_got_refcounts = elf_local_got_refcounts (abfd);
6222 if (local_got_refcounts == NULL)
6226 size = symtab_hdr->sh_info;
6227 size *= sizeof (bfd_signed_vma);
6228 #ifdef INCLUDE_SHMEDIA
6229 /* Reserve space for both the datalabel and
6230 codelabel local GOT offsets. */
6233 size += symtab_hdr->sh_info;
6234 local_got_refcounts = ((bfd_signed_vma *)
6235 bfd_zalloc (abfd, size));
6236 if (local_got_refcounts == NULL)
6238 elf_local_got_refcounts (abfd) = local_got_refcounts;
6239 #ifdef INCLUDE_SHMEDIA
6240 /* Take care of both the datalabel and codelabel local
6242 sh_elf_local_got_type (abfd)
6243 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6245 sh_elf_local_got_type (abfd)
6246 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6249 #ifdef INCLUDE_SHMEDIA
6250 if (rel->r_addend & 1)
6251 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6254 local_got_refcounts[r_symndx] += 1;
6255 old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
6258 /* If a TLS symbol is accessed using IE at least once,
6259 there is no point to use dynamic model for it. */
6260 if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
6261 && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
6263 if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
6264 got_type = GOT_TLS_IE;
6267 if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
6268 && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
6269 (*_bfd_error_handler)
6270 (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6271 abfd, h->root.root.string);
6272 else if (old_got_type == GOT_FUNCDESC
6273 || got_type == GOT_FUNCDESC)
6274 (*_bfd_error_handler)
6275 (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6276 abfd, h->root.root.string);
6278 (*_bfd_error_handler)
6279 (_("%B: `%s' accessed both as normal and thread local symbol"),
6280 abfd, h->root.root.string);
6285 if (old_got_type != got_type)
6288 sh_elf_hash_entry (h)->got_type = got_type;
6290 sh_elf_local_got_type (abfd) [r_symndx] = got_type;
6295 case R_SH_TLS_LD_32:
6296 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6300 case R_SH_GOTOFFFUNCDESC:
6301 case R_SH_GOTOFFFUNCDESC20:
6304 (*_bfd_error_handler)
6305 (_("%B: Function descriptor relocation with non-zero addend"),
6312 union gotref *local_funcdesc;
6314 /* We need a function descriptor for a local symbol. */
6315 local_funcdesc = sh_elf_local_funcdesc (abfd);
6316 if (local_funcdesc == NULL)
6320 size = symtab_hdr->sh_info * sizeof (union gotref);
6321 #ifdef INCLUDE_SHMEDIA
6322 /* Count datalabel local GOT. */
6325 local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
6326 if (local_funcdesc == NULL)
6328 sh_elf_local_funcdesc (abfd) = local_funcdesc;
6330 local_funcdesc[r_symndx].refcount += 1;
6332 if (r_type == R_SH_FUNCDESC)
6335 htab->srofixup->size += 4;
6337 htab->srelgot->size += sizeof (Elf32_External_Rela);
6342 sh_elf_hash_entry (h)->funcdesc.refcount++;
6343 if (r_type == R_SH_FUNCDESC)
6344 sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
6346 /* If there is a function descriptor reference, then
6347 there should not be any non-FDPIC references. */
6348 old_got_type = sh_elf_hash_entry (h)->got_type;
6349 if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
6351 if (old_got_type == GOT_NORMAL)
6352 (*_bfd_error_handler)
6353 (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6354 abfd, h->root.root.string);
6356 (*_bfd_error_handler)
6357 (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6358 abfd, h->root.root.string);
6364 #ifdef INCLUDE_SHMEDIA
6365 case R_SH_GOTPLT_LOW16:
6366 case R_SH_GOTPLT_MEDLOW16:
6367 case R_SH_GOTPLT_MEDHI16:
6368 case R_SH_GOTPLT_HI16:
6369 case R_SH_GOTPLT10BY4:
6370 case R_SH_GOTPLT10BY8:
6372 /* If this is a local symbol, we resolve it directly without
6373 creating a procedure linkage table entry. */
6379 || h->dynindx == -1)
6383 h->plt.refcount += 1;
6384 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6389 #ifdef INCLUDE_SHMEDIA
6390 case R_SH_PLT_LOW16:
6391 case R_SH_PLT_MEDLOW16:
6392 case R_SH_PLT_MEDHI16:
6395 /* This symbol requires a procedure linkage table entry. We
6396 actually build the entry in adjust_dynamic_symbol,
6397 because this might be a case of linking PIC code which is
6398 never referenced by a dynamic object, in which case we
6399 don't need to generate a procedure linkage table entry
6402 /* If this is a local symbol, we resolve it directly without
6403 creating a procedure linkage table entry. */
6407 if (h->forced_local)
6411 h->plt.refcount += 1;
6416 #ifdef INCLUDE_SHMEDIA
6417 case R_SH_IMM_LOW16_PCREL:
6418 case R_SH_IMM_MEDLOW16_PCREL:
6419 case R_SH_IMM_MEDHI16_PCREL:
6420 case R_SH_IMM_HI16_PCREL:
6422 if (h != NULL && ! info->shared)
6425 h->plt.refcount += 1;
6428 /* If we are creating a shared library, and this is a reloc
6429 against a global symbol, or a non PC relative reloc
6430 against a local symbol, then we need to copy the reloc
6431 into the shared library. However, if we are linking with
6432 -Bsymbolic, we do not need to copy a reloc against a
6433 global symbol which is defined in an object we are
6434 including in the link (i.e., DEF_REGULAR is set). At
6435 this point we have not seen all the input files, so it is
6436 possible that DEF_REGULAR is not set now but will be set
6437 later (it is never cleared). We account for that
6438 possibility below by storing information in the
6439 dyn_relocs field of the hash table entry. A similar
6440 situation occurs when creating shared libraries and symbol
6441 visibility changes render the symbol local.
6443 If on the other hand, we are creating an executable, we
6444 may need to keep relocations for symbols satisfied by a
6445 dynamic library if we manage to avoid copy relocs for the
6448 && (sec->flags & SEC_ALLOC) != 0
6449 && (r_type != R_SH_REL32
6451 && (! info->symbolic
6452 || h->root.type == bfd_link_hash_defweak
6453 || !h->def_regular))))
6455 && (sec->flags & SEC_ALLOC) != 0
6457 && (h->root.type == bfd_link_hash_defweak
6458 || !h->def_regular)))
6460 struct elf_sh_dyn_relocs *p;
6461 struct elf_sh_dyn_relocs **head;
6463 if (htab->root.dynobj == NULL)
6464 htab->root.dynobj = abfd;
6466 /* When creating a shared object, we must copy these
6467 reloc types into the output file. We create a reloc
6468 section in dynobj and make room for this reloc. */
6471 sreloc = _bfd_elf_make_dynamic_reloc_section
6472 (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE);
6478 /* If this is a global symbol, we count the number of
6479 relocations we need for this symbol. */
6481 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6484 /* Track dynamic relocs needed for local syms too. */
6487 Elf_Internal_Sym *isym;
6489 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6494 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6498 vpp = &elf_section_data (s)->local_dynrel;
6499 head = (struct elf_sh_dyn_relocs **) vpp;
6503 if (p == NULL || p->sec != sec)
6505 bfd_size_type amt = sizeof (*p);
6506 p = bfd_alloc (htab->root.dynobj, amt);
6517 if (r_type == R_SH_REL32
6518 #ifdef INCLUDE_SHMEDIA
6519 || r_type == R_SH_IMM_LOW16_PCREL
6520 || r_type == R_SH_IMM_MEDLOW16_PCREL
6521 || r_type == R_SH_IMM_MEDHI16_PCREL
6522 || r_type == R_SH_IMM_HI16_PCREL
6528 /* Allocate the fixup regardless of whether we need a relocation.
6529 If we end up generating the relocation, we'll unallocate the
6531 if (htab->fdpic_p && !info->shared
6532 && r_type == R_SH_DIR32
6533 && (sec->flags & SEC_ALLOC) != 0)
6534 htab->srofixup->size += 4;
6537 case R_SH_TLS_LE_32:
6538 if (info->shared && !info->pie)
6540 (*_bfd_error_handler)
6541 (_("%B: TLS local exec code cannot be linked into shared objects"),
6548 case R_SH_TLS_LDO_32:
6549 /* Nothing to do. */
6560 #ifndef sh_elf_set_mach_from_flags
6561 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
6564 sh_elf_set_mach_from_flags (bfd *abfd)
6566 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
6568 if (flags >= sizeof(sh_ef_bfd_table))
6571 if (sh_ef_bfd_table[flags] == 0)
6574 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
6580 /* Reverse table lookup for sh_ef_bfd_table[].
6581 Given a bfd MACH value from archures.c
6582 return the equivalent ELF flags from the table.
6583 Return -1 if no match is found. */
6586 sh_elf_get_flags_from_mach (unsigned long mach)
6588 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
6591 if (sh_ef_bfd_table[i] == mach)
6594 /* shouldn't get here */
6599 #endif /* not sh_elf_set_mach_from_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 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6610 if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
6613 return sh_elf_set_mach_from_flags (obfd);
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 struct bfd_link_info *info ATTRIBUTE_UNUSED,
7276 const asection *rel_sec ATTRIBUTE_UNUSED,
7277 const Elf_Internal_Rela *rela)
7279 switch ((int) ELF32_R_TYPE (rela->r_info))
7282 return reloc_class_relative;
7284 return reloc_class_plt;
7286 return reloc_class_copy;
7288 return reloc_class_normal;
7292 #if !defined SH_TARGET_ALREADY_DEFINED
7293 /* Support for Linux core dump NOTE sections. */
7296 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7301 switch (note->descsz)
7306 case 168: /* Linux/SH */
7308 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
7311 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
7320 /* Make a ".reg/999" section. */
7321 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7322 size, note->descpos + offset);
7326 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7328 switch (note->descsz)
7333 case 124: /* Linux/SH elf_prpsinfo */
7334 elf_tdata (abfd)->core->program
7335 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7336 elf_tdata (abfd)->core->command
7337 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7340 /* Note that for some reason, a spurious space is tacked
7341 onto the end of the args in some (at least one anyway)
7342 implementations, so strip it off if it exists. */
7345 char *command = elf_tdata (abfd)->core->command;
7346 int n = strlen (command);
7348 if (0 < n && command[n - 1] == ' ')
7349 command[n - 1] = '\0';
7354 #endif /* not SH_TARGET_ALREADY_DEFINED */
7357 /* Return address for Ith PLT stub in section PLT, for relocation REL
7358 or (bfd_vma) -1 if it should not be included. */
7361 sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
7362 const arelent *rel ATTRIBUTE_UNUSED)
7364 const struct elf_sh_plt_info *plt_info;
7366 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
7367 return plt->vma + get_plt_offset (plt_info, i);
7370 /* Decide whether to attempt to turn absptr or lsda encodings in
7371 shared libraries into pcrel within the given input section. */
7374 sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
7375 struct bfd_link_info *info,
7376 asection *eh_frame_section ATTRIBUTE_UNUSED)
7378 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7380 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
7387 /* Adjust the contents of an eh_frame_hdr section before they're output. */
7390 sh_elf_encode_eh_address (bfd *abfd,
7391 struct bfd_link_info *info,
7392 asection *osec, bfd_vma offset,
7393 asection *loc_sec, bfd_vma loc_offset,
7396 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7397 struct elf_link_hash_entry *h;
7400 return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
7401 loc_offset, encoded);
7403 h = htab->root.hgot;
7404 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
7406 if (! h || (sh_elf_osec_to_segment (abfd, osec)
7407 == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
7408 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
7409 loc_sec, loc_offset, encoded);
7411 BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
7412 == (sh_elf_osec_to_segment
7413 (abfd, h->root.u.def.section->output_section)));
7415 *encoded = osec->vma + offset
7416 - (h->root.u.def.value
7417 + h->root.u.def.section->output_section->vma
7418 + h->root.u.def.section->output_offset);
7420 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
7423 #if !defined SH_TARGET_ALREADY_DEFINED
7424 #define TARGET_BIG_SYM sh_elf32_vec
7425 #define TARGET_BIG_NAME "elf32-sh"
7426 #define TARGET_LITTLE_SYM sh_elf32_le_vec
7427 #define TARGET_LITTLE_NAME "elf32-shl"
7430 #define ELF_ARCH bfd_arch_sh
7431 #define ELF_TARGET_ID SH_ELF_DATA
7432 #define ELF_MACHINE_CODE EM_SH
7433 #ifdef __QNXTARGET__
7434 #define ELF_MAXPAGESIZE 0x1000
7436 #define ELF_MAXPAGESIZE 0x80
7439 #define elf_symbol_leading_char '_'
7441 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7442 #define bfd_elf32_bfd_reloc_name_lookup \
7443 sh_elf_reloc_name_lookup
7444 #define elf_info_to_howto sh_elf_info_to_howto
7445 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
7446 #define elf_backend_relocate_section sh_elf_relocate_section
7447 #define bfd_elf32_bfd_get_relocated_section_contents \
7448 sh_elf_get_relocated_section_contents
7449 #define bfd_elf32_mkobject sh_elf_mkobject
7450 #define elf_backend_object_p sh_elf_object_p
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 sh_elf32_nbsd_vec
7500 #undef TARGET_BIG_NAME
7501 #define TARGET_BIG_NAME "elf32-sh-nbsd"
7502 #undef TARGET_LITTLE_SYM
7503 #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_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 sh_elf32_linux_be_vec
7520 #undef TARGET_BIG_NAME
7521 #define TARGET_BIG_NAME "elf32-shbig-linux"
7522 #undef TARGET_LITTLE_SYM
7523 #define TARGET_LITTLE_SYM sh_elf32_linux_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 sh_elf32_fdpic_be_vec
7542 #undef TARGET_BIG_NAME
7543 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
7544 #undef TARGET_LITTLE_SYM
7545 #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_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 sh_elf32_vxworks_vec
7559 #undef TARGET_BIG_NAME
7560 #define TARGET_BIG_NAME "elf32-sh-vxworks"
7561 #undef TARGET_LITTLE_SYM
7562 #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_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 */