Update my e-mail address.
[binutils-gdb.git] / bfd / elf32-sh.c
blob3960427bb4914fbb5f3ca22e1a569a988fcf852e
1 /* Renesas / SuperH SH specific support for 32-bit ELF
2 Copyright (C) 1996-2017 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. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf-vxworks.h"
28 #include "elf/sh.h"
29 #include "dwarf2.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 *);
41 #ifndef SH64_ELF
42 static bfd_boolean sh_elf_swap_insns
43 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
44 #endif
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 *);
49 static bfd_vma tpoff
50 (struct bfd_link_info *, bfd_vma);
52 /* The name of the dynamic interpreter. This is put in the .interp
53 section. */
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. */
87 static bfd_boolean
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);
96 #else
97 return FALSE;
98 #endif
101 /* Return true if OUTPUT_BFD is an FDPIC object. */
103 static bfd_boolean
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);
112 #else
113 return FALSE;
114 #endif
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;
136 int diff, cum_diff;
137 bfd_signed_vma x;
138 int insn;
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. */
146 if (! last_addr)
148 last_addr = addr;
149 last_symbol_section = symbol_section;
150 return bfd_reloc_ok;
152 if (last_addr != addr)
153 abort ();
154 last_addr = 0;
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;
164 else
166 if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
167 &contents))
169 if (contents != NULL)
170 free (contents);
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);)
180 ptr -= 2;
181 ptr += 2;
182 diff = (last_ptr - ptr) >> 1;
183 cum_diff += diff & 1;
184 cum_diff += diff;
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. */
189 if (cum_diff >= 0)
191 start -= 4;
192 end = (ptr + cum_diff * 2) - contents;
194 else
196 bfd_vma start0 = start - 4;
198 while (start0 && IS_PPI (contents + start0))
199 start0 -= 2;
200 start0 = start - 2 - ((start - start0) & 2);
201 start = start0 - cum_diff - 2;
202 end = start0;
205 if (contents != NULL
206 && elf_section_data (symbol_section)->this_hdr.contents != contents)
207 free (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));
216 x >>= 1;
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);
223 return bfd_reloc_ok;
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)
234 unsigned long insn;
235 bfd_vma sym_value;
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;
246 return bfd_reloc_ok;
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)
252 return bfd_reloc_ok;
254 if (symbol_in != NULL
255 && bfd_is_und_section (symbol_in->section))
256 return bfd_reloc_undefined;
258 /* PR 17512: file: 9891ca98. */
259 if (addr * bfd_octets_per_byte (abfd) + bfd_get_reloc_size (reloc_entry->howto)
260 > bfd_get_section_limit_octets (abfd, input_section))
261 return bfd_reloc_outofrange;
263 if (bfd_is_com_section (symbol_in->section))
264 sym_value = 0;
265 else
266 sym_value = (symbol_in->value +
267 symbol_in->section->output_section->vma +
268 symbol_in->section->output_offset);
270 switch (r_type)
272 case R_SH_DIR32:
273 insn = bfd_get_32 (abfd, hit_data);
274 insn += sym_value + reloc_entry->addend;
275 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
276 break;
277 case R_SH_IND12W:
278 insn = bfd_get_16 (abfd, hit_data);
279 sym_value += reloc_entry->addend;
280 sym_value -= (input_section->output_section->vma
281 + input_section->output_offset
282 + addr
283 + 4);
284 sym_value += (insn & 0xfff) << 1;
285 if (insn & 0x800)
286 sym_value -= 0x1000;
287 insn = (insn & 0xf000) | (sym_value & 0xfff);
288 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
289 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
290 return bfd_reloc_overflow;
291 break;
292 default:
293 abort ();
294 break;
297 return bfd_reloc_ok;
300 /* This function is used for relocs which are only used for relaxing,
301 which the linker should otherwise ignore. */
303 static bfd_reloc_status_type
304 sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
305 asymbol *symbol ATTRIBUTE_UNUSED,
306 void *data ATTRIBUTE_UNUSED, asection *input_section,
307 bfd *output_bfd,
308 char **error_message ATTRIBUTE_UNUSED)
310 if (output_bfd != NULL)
311 reloc_entry->address += input_section->output_offset;
312 return bfd_reloc_ok;
315 /* This structure is used to map BFD reloc codes to SH ELF relocs. */
317 struct elf_reloc_map
319 bfd_reloc_code_real_type bfd_reloc_val;
320 unsigned char elf_reloc_val;
323 /* An array mapping BFD reloc codes to SH ELF relocs. */
325 static const struct elf_reloc_map sh_reloc_map[] =
327 { BFD_RELOC_NONE, R_SH_NONE },
328 { BFD_RELOC_32, R_SH_DIR32 },
329 { BFD_RELOC_16, R_SH_DIR16 },
330 { BFD_RELOC_8, R_SH_DIR8 },
331 { BFD_RELOC_CTOR, R_SH_DIR32 },
332 { BFD_RELOC_32_PCREL, R_SH_REL32 },
333 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
334 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
335 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
336 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
337 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
338 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
339 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
340 { BFD_RELOC_SH_USES, R_SH_USES },
341 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
342 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
343 { BFD_RELOC_SH_CODE, R_SH_CODE },
344 { BFD_RELOC_SH_DATA, R_SH_DATA },
345 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
346 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
347 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
348 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
349 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
350 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
351 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
352 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
353 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
354 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
355 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
356 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
357 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
358 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
359 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
360 { BFD_RELOC_SH_COPY, R_SH_COPY },
361 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
362 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
363 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
364 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
365 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
366 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
367 { BFD_RELOC_SH_GOT20, R_SH_GOT20 },
368 { BFD_RELOC_SH_GOTOFF20, R_SH_GOTOFF20 },
369 { BFD_RELOC_SH_GOTFUNCDESC, R_SH_GOTFUNCDESC },
370 { BFD_RELOC_SH_GOTFUNCDESC20, R_SH_GOTFUNCDESC20 },
371 { BFD_RELOC_SH_GOTOFFFUNCDESC, R_SH_GOTOFFFUNCDESC },
372 { BFD_RELOC_SH_GOTOFFFUNCDESC20, R_SH_GOTOFFFUNCDESC20 },
373 { BFD_RELOC_SH_FUNCDESC, R_SH_FUNCDESC },
374 #ifdef INCLUDE_SHMEDIA
375 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
376 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
377 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
378 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
379 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
380 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
381 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
382 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
383 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
384 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
385 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
386 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
387 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
388 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
389 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
390 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
391 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
392 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
393 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
394 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
395 { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
396 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
397 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
398 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
399 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
400 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
401 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
402 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
403 { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
404 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
405 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
406 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
407 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
408 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
409 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
410 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
411 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
412 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
413 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
414 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
415 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
416 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
417 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
418 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
419 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
420 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
421 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
422 { BFD_RELOC_64, R_SH_64 },
423 { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
424 #endif /* not INCLUDE_SHMEDIA */
427 /* Given a BFD reloc code, return the howto structure for the
428 corresponding SH ELF reloc. */
430 static reloc_howto_type *
431 sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
433 unsigned int i;
435 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
437 if (sh_reloc_map[i].bfd_reloc_val == code)
438 return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
441 return NULL;
444 static reloc_howto_type *
445 sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
447 unsigned int i;
449 if (vxworks_object_p (abfd))
451 for (i = 0;
452 i < (sizeof (sh_vxworks_howto_table)
453 / sizeof (sh_vxworks_howto_table[0]));
454 i++)
455 if (sh_vxworks_howto_table[i].name != NULL
456 && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
457 return &sh_vxworks_howto_table[i];
459 else
461 for (i = 0;
462 i < (sizeof (sh_elf_howto_table)
463 / sizeof (sh_elf_howto_table[0]));
464 i++)
465 if (sh_elf_howto_table[i].name != NULL
466 && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
467 return &sh_elf_howto_table[i];
470 return NULL;
473 /* Given an ELF reloc, fill in the howto field of a relent. */
475 static void
476 sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
478 unsigned int r;
480 r = ELF32_R_TYPE (dst->r_info);
482 if (r >= R_SH_max
483 || (r >= R_SH_FIRST_INVALID_RELOC && r <= R_SH_LAST_INVALID_RELOC)
484 || (r >= R_SH_FIRST_INVALID_RELOC_2 && r <= R_SH_LAST_INVALID_RELOC_2)
485 || (r >= R_SH_FIRST_INVALID_RELOC_3 && r <= R_SH_LAST_INVALID_RELOC_3)
486 || (r >= R_SH_FIRST_INVALID_RELOC_4 && r <= R_SH_LAST_INVALID_RELOC_4)
487 || (r >= R_SH_FIRST_INVALID_RELOC_5 && r <= R_SH_LAST_INVALID_RELOC_5)
488 || (r >= R_SH_FIRST_INVALID_RELOC_6 && r <= R_SH_LAST_INVALID_RELOC_6))
490 /* xgettext:c-format */
491 _bfd_error_handler (_("%B: unrecognised SH reloc number: %d"),
492 abfd, r);
493 bfd_set_error (bfd_error_bad_value);
494 r = R_SH_NONE;
497 cache_ptr->howto = get_howto_table (abfd) + r;
500 /* This function handles relaxing for SH ELF. See the corresponding
501 function in coff-sh.c for a description of what this does. FIXME:
502 There is a lot of duplication here between this code and the COFF
503 specific code. The format of relocs and symbols is wound deeply
504 into this code, but it would still be better if the duplication
505 could be eliminated somehow. Note in particular that although both
506 functions use symbols like R_SH_CODE, those symbols have different
507 values; in coff-sh.c they come from include/coff/sh.h, whereas here
508 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
510 static bfd_boolean
511 sh_elf_relax_section (bfd *abfd, asection *sec,
512 struct bfd_link_info *link_info, bfd_boolean *again)
514 Elf_Internal_Shdr *symtab_hdr;
515 Elf_Internal_Rela *internal_relocs;
516 bfd_boolean have_code;
517 Elf_Internal_Rela *irel, *irelend;
518 bfd_byte *contents = NULL;
519 Elf_Internal_Sym *isymbuf = NULL;
521 *again = FALSE;
523 if (bfd_link_relocatable (link_info)
524 || (sec->flags & SEC_RELOC) == 0
525 || sec->reloc_count == 0)
526 return TRUE;
528 #ifdef INCLUDE_SHMEDIA
529 if (elf_section_data (sec)->this_hdr.sh_flags
530 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
532 return TRUE;
534 #endif
536 symtab_hdr = &elf_symtab_hdr (abfd);
538 internal_relocs = (_bfd_elf_link_read_relocs
539 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
540 link_info->keep_memory));
541 if (internal_relocs == NULL)
542 goto error_return;
544 have_code = FALSE;
546 irelend = internal_relocs + sec->reloc_count;
547 for (irel = internal_relocs; irel < irelend; irel++)
549 bfd_vma laddr, paddr, symval;
550 unsigned short insn;
551 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
552 bfd_signed_vma foff;
554 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
555 have_code = TRUE;
557 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
558 continue;
560 /* Get the section contents. */
561 if (contents == NULL)
563 if (elf_section_data (sec)->this_hdr.contents != NULL)
564 contents = elf_section_data (sec)->this_hdr.contents;
565 else
567 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
568 goto error_return;
572 /* The r_addend field of the R_SH_USES reloc will point us to
573 the register load. The 4 is because the r_addend field is
574 computed as though it were a jump offset, which are based
575 from 4 bytes after the jump instruction. */
576 laddr = irel->r_offset + 4 + irel->r_addend;
577 if (laddr >= sec->size)
579 /* xgettext:c-format */
580 _bfd_error_handler (_("%B: %#Lx: warning: bad R_SH_USES offset"),
581 abfd, irel->r_offset);
582 continue;
584 insn = bfd_get_16 (abfd, contents + laddr);
586 /* If the instruction is not mov.l NN,rN, we don't know what to
587 do. */
588 if ((insn & 0xf000) != 0xd000)
590 _bfd_error_handler
591 /* xgettext:c-format */
592 (_("%B: %#Lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
593 abfd, irel->r_offset, insn);
594 continue;
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. */
603 paddr = insn & 0xff;
604 paddr *= 4;
605 paddr += (laddr + 4) &~ (bfd_vma) 3;
606 if (paddr >= sec->size)
608 _bfd_error_handler
609 /* xgettext:c-format */
610 (_("%B: %#Lx: warning: bad R_SH_USES load offset"),
611 abfd, irel->r_offset);
612 continue;
615 /* Get the reloc for the address from which the register is
616 being loaded. This reloc will tell us which function is
617 actually being called. */
618 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
619 if (irelfn->r_offset == paddr
620 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
621 break;
622 if (irelfn >= irelend)
624 _bfd_error_handler
625 /* xgettext:c-format */
626 (_("%B: %#Lx: warning: could not find expected reloc"),
627 abfd, paddr);
628 continue;
631 /* Read this BFD's symbols if we haven't done so already. */
632 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
634 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
635 if (isymbuf == NULL)
636 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
637 symtab_hdr->sh_info, 0,
638 NULL, NULL, NULL);
639 if (isymbuf == NULL)
640 goto error_return;
643 /* Get the value of the symbol referred to by the reloc. */
644 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
646 /* A local symbol. */
647 Elf_Internal_Sym *isym;
649 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
650 if (isym->st_shndx
651 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
653 _bfd_error_handler
654 /* xgettext:c-format */
655 (_("%B: %#Lx: warning: symbol in unexpected section"),
656 abfd, paddr);
657 continue;
660 symval = (isym->st_value
661 + sec->output_section->vma
662 + sec->output_offset);
664 else
666 unsigned long indx;
667 struct elf_link_hash_entry *h;
669 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
670 h = elf_sym_hashes (abfd)[indx];
671 BFD_ASSERT (h != NULL);
672 if (h->root.type != bfd_link_hash_defined
673 && h->root.type != bfd_link_hash_defweak)
675 /* This appears to be a reference to an undefined
676 symbol. Just ignore it--it will be caught by the
677 regular reloc processing. */
678 continue;
681 symval = (h->root.u.def.value
682 + h->root.u.def.section->output_section->vma
683 + h->root.u.def.section->output_offset);
686 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
687 symval += bfd_get_32 (abfd, contents + paddr);
688 else
689 symval += irelfn->r_addend;
691 /* See if this function call can be shortened. */
692 foff = (symval
693 - (irel->r_offset
694 + sec->output_section->vma
695 + sec->output_offset
696 + 4));
697 /* A branch to an address beyond ours might be increased by an
698 .align that doesn't move when bytes behind us are deleted.
699 So, we add some slop in this calculation to allow for
700 that. */
701 if (foff < -0x1000 || foff >= 0x1000 - 8)
703 /* After all that work, we can't shorten this function call. */
704 continue;
707 /* Shorten the function call. */
709 /* For simplicity of coding, we are going to modify the section
710 contents, the section relocs, and the BFD symbol table. We
711 must tell the rest of the code not to free up this
712 information. It would be possible to instead create a table
713 of changes which have to be made, as is done in coff-mips.c;
714 that would be more work, but would require less memory when
715 the linker is run. */
717 elf_section_data (sec)->relocs = internal_relocs;
718 elf_section_data (sec)->this_hdr.contents = contents;
719 symtab_hdr->contents = (unsigned char *) isymbuf;
721 /* Replace the jmp/jsr with a bra/bsr. */
723 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
724 replace the jmp/jsr with a bra/bsr. */
725 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
726 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
727 here, but that only checks if the symbol is an external symbol,
728 not if the symbol is in a different section. Besides, we need
729 a consistent meaning for the relocation, so we just assume here that
730 the value of the symbol is not available. */
732 /* We can't fully resolve this yet, because the external
733 symbol value may be changed by future relaxing. We let
734 the final link phase handle it. */
735 if (bfd_get_16 (abfd, contents + irel->r_offset) & 0x0020)
736 bfd_put_16 (abfd, (bfd_vma) 0xa000, contents + irel->r_offset);
737 else
738 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
740 irel->r_addend = -4;
742 /* When we calculated the symbol "value" we had an offset in the
743 DIR32's word in memory (we read and add it above). However,
744 the jsr we create does NOT have this offset encoded, so we
745 have to add it to the addend to preserve it. */
746 irel->r_addend += bfd_get_32 (abfd, contents + paddr);
748 /* See if there is another R_SH_USES reloc referring to the same
749 register load. */
750 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
751 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
752 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
753 break;
754 if (irelscan < irelend)
756 /* Some other function call depends upon this register load,
757 and we have not yet converted that function call.
758 Indeed, we may never be able to convert it. There is
759 nothing else we can do at this point. */
760 continue;
763 /* Look for a R_SH_COUNT reloc on the location where the
764 function address is stored. Do this before deleting any
765 bytes, to avoid confusion about the address. */
766 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
767 if (irelcount->r_offset == paddr
768 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
769 break;
771 /* Delete the register load. */
772 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
773 goto error_return;
775 /* That will change things, so, just in case it permits some
776 other function call to come within range, we should relax
777 again. Note that this is not required, and it may be slow. */
778 *again = TRUE;
780 /* Now check whether we got a COUNT reloc. */
781 if (irelcount >= irelend)
783 _bfd_error_handler
784 /* xgettext:c-format */
785 (_("%B: %#Lx: warning: could not find expected COUNT reloc"),
786 abfd, paddr);
787 continue;
790 /* The number of uses is stored in the r_addend field. We've
791 just deleted one. */
792 if (irelcount->r_addend == 0)
794 /* xgettext:c-format */
795 _bfd_error_handler (_("%B: %#Lx: warning: bad count"),
796 abfd, paddr);
797 continue;
800 --irelcount->r_addend;
802 /* If there are no more uses, we can delete the address. Reload
803 the address from irelfn, in case it was changed by the
804 previous call to sh_elf_relax_delete_bytes. */
805 if (irelcount->r_addend == 0)
807 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
808 goto error_return;
811 /* We've done all we can with that function call. */
814 /* Look for load and store instructions that we can align on four
815 byte boundaries. */
816 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
817 && have_code)
819 bfd_boolean swapped;
821 /* Get the section contents. */
822 if (contents == NULL)
824 if (elf_section_data (sec)->this_hdr.contents != NULL)
825 contents = elf_section_data (sec)->this_hdr.contents;
826 else
828 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
829 goto error_return;
833 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
834 &swapped))
835 goto error_return;
837 if (swapped)
839 elf_section_data (sec)->relocs = internal_relocs;
840 elf_section_data (sec)->this_hdr.contents = contents;
841 symtab_hdr->contents = (unsigned char *) isymbuf;
845 if (isymbuf != NULL
846 && symtab_hdr->contents != (unsigned char *) isymbuf)
848 if (! link_info->keep_memory)
849 free (isymbuf);
850 else
852 /* Cache the symbols for elf_link_input_bfd. */
853 symtab_hdr->contents = (unsigned char *) isymbuf;
857 if (contents != NULL
858 && elf_section_data (sec)->this_hdr.contents != contents)
860 if (! link_info->keep_memory)
861 free (contents);
862 else
864 /* Cache the section contents for elf_link_input_bfd. */
865 elf_section_data (sec)->this_hdr.contents = contents;
869 if (internal_relocs != NULL
870 && elf_section_data (sec)->relocs != internal_relocs)
871 free (internal_relocs);
873 return TRUE;
875 error_return:
876 if (isymbuf != NULL
877 && symtab_hdr->contents != (unsigned char *) isymbuf)
878 free (isymbuf);
879 if (contents != NULL
880 && elf_section_data (sec)->this_hdr.contents != contents)
881 free (contents);
882 if (internal_relocs != NULL
883 && elf_section_data (sec)->relocs != internal_relocs)
884 free (internal_relocs);
886 return FALSE;
889 /* Delete some bytes from a section while relaxing. FIXME: There is a
890 lot of duplication between this function and sh_relax_delete_bytes
891 in coff-sh.c. */
893 static bfd_boolean
894 sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
895 int count)
897 Elf_Internal_Shdr *symtab_hdr;
898 unsigned int sec_shndx;
899 bfd_byte *contents;
900 Elf_Internal_Rela *irel, *irelend;
901 Elf_Internal_Rela *irelalign;
902 bfd_vma toaddr;
903 Elf_Internal_Sym *isymbuf, *isym, *isymend;
904 struct elf_link_hash_entry **sym_hashes;
905 struct elf_link_hash_entry **end_hashes;
906 unsigned int symcount;
907 asection *o;
909 symtab_hdr = &elf_symtab_hdr (abfd);
910 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
912 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
914 contents = elf_section_data (sec)->this_hdr.contents;
916 /* The deletion must stop at the next ALIGN reloc for an alignment
917 power larger than the number of bytes we are deleting. */
919 irelalign = NULL;
920 toaddr = sec->size;
922 irel = elf_section_data (sec)->relocs;
923 irelend = irel + sec->reloc_count;
924 for (; irel < irelend; irel++)
926 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
927 && irel->r_offset > addr
928 && count < (1 << irel->r_addend))
930 irelalign = irel;
931 toaddr = irel->r_offset;
932 break;
936 /* Actually delete the bytes. */
937 memmove (contents + addr, contents + addr + count,
938 (size_t) (toaddr - addr - count));
939 if (irelalign == NULL)
940 sec->size -= count;
941 else
943 int i;
945 #define NOP_OPCODE (0x0009)
947 BFD_ASSERT ((count & 1) == 0);
948 for (i = 0; i < count; i += 2)
949 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
952 /* Adjust all the relocs. */
953 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
955 bfd_vma nraddr, stop;
956 bfd_vma start = 0;
957 int insn = 0;
958 int off, adjust, oinsn;
959 bfd_signed_vma voff = 0;
960 bfd_boolean overflow;
962 /* Get the new reloc address. */
963 nraddr = irel->r_offset;
964 if ((irel->r_offset > addr
965 && irel->r_offset < toaddr)
966 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
967 && irel->r_offset == toaddr))
968 nraddr -= count;
970 /* See if this reloc was for the bytes we have deleted, in which
971 case we no longer care about it. Don't delete relocs which
972 represent addresses, though. */
973 if (irel->r_offset >= addr
974 && irel->r_offset < addr + count
975 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
976 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
977 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
978 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
979 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
980 (int) R_SH_NONE);
982 /* If this is a PC relative reloc, see if the range it covers
983 includes the bytes we have deleted. */
984 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
986 default:
987 break;
989 case R_SH_DIR8WPN:
990 case R_SH_IND12W:
991 case R_SH_DIR8WPZ:
992 case R_SH_DIR8WPL:
993 start = irel->r_offset;
994 insn = bfd_get_16 (abfd, contents + nraddr);
995 break;
998 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1000 default:
1001 start = stop = addr;
1002 break;
1004 case R_SH_DIR32:
1005 /* If this reloc is against a symbol defined in this
1006 section, and the symbol will not be adjusted below, we
1007 must check the addend to see it will put the value in
1008 range to be adjusted, and hence must be changed. */
1009 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1011 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1012 if (isym->st_shndx == sec_shndx
1013 && (isym->st_value <= addr
1014 || isym->st_value >= toaddr))
1016 bfd_vma val;
1018 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
1020 val = bfd_get_32 (abfd, contents + nraddr);
1021 val += isym->st_value;
1022 if (val > addr && val < toaddr)
1023 bfd_put_32 (abfd, val - count, contents + nraddr);
1025 else
1027 val = isym->st_value + irel->r_addend;
1028 if (val > addr && val < toaddr)
1029 irel->r_addend -= count;
1033 start = stop = addr;
1034 break;
1036 case R_SH_DIR8WPN:
1037 off = insn & 0xff;
1038 if (off & 0x80)
1039 off -= 0x100;
1040 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1041 break;
1043 case R_SH_IND12W:
1044 off = insn & 0xfff;
1045 if (! off)
1047 /* This has been made by previous relaxation. Since the
1048 relocation will be against an external symbol, the
1049 final relocation will just do the right thing. */
1050 start = stop = addr;
1052 else
1054 if (off & 0x800)
1055 off -= 0x1000;
1056 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
1058 /* The addend will be against the section symbol, thus
1059 for adjusting the addend, the relevant start is the
1060 start of the section.
1061 N.B. If we want to abandon in-place changes here and
1062 test directly using symbol + addend, we have to take into
1063 account that the addend has already been adjusted by -4. */
1064 if (stop > addr && stop < toaddr)
1065 irel->r_addend -= count;
1067 break;
1069 case R_SH_DIR8WPZ:
1070 off = insn & 0xff;
1071 stop = start + 4 + off * 2;
1072 break;
1074 case R_SH_DIR8WPL:
1075 off = insn & 0xff;
1076 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1077 break;
1079 case R_SH_SWITCH8:
1080 case R_SH_SWITCH16:
1081 case R_SH_SWITCH32:
1082 /* These relocs types represent
1083 .word L2-L1
1084 The r_addend field holds the difference between the reloc
1085 address and L1. That is the start of the reloc, and
1086 adding in the contents gives us the top. We must adjust
1087 both the r_offset field and the section contents.
1088 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1089 and the elf bfd r_offset is called r_vaddr. */
1091 stop = irel->r_offset;
1092 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1094 if (start > addr
1095 && start < toaddr
1096 && (stop <= addr || stop >= toaddr))
1097 irel->r_addend += count;
1098 else if (stop > addr
1099 && stop < toaddr
1100 && (start <= addr || start >= toaddr))
1101 irel->r_addend -= count;
1103 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1104 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1105 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1106 voff = bfd_get_8 (abfd, contents + nraddr);
1107 else
1108 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1109 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1111 break;
1113 case R_SH_USES:
1114 start = irel->r_offset;
1115 stop = (bfd_vma) ((bfd_signed_vma) start
1116 + (long) irel->r_addend
1117 + 4);
1118 break;
1121 if (start > addr
1122 && start < toaddr
1123 && (stop <= addr || stop >= toaddr))
1124 adjust = count;
1125 else if (stop > addr
1126 && stop < toaddr
1127 && (start <= addr || start >= toaddr))
1128 adjust = - count;
1129 else
1130 adjust = 0;
1132 if (adjust != 0)
1134 oinsn = insn;
1135 overflow = FALSE;
1136 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1138 default:
1139 abort ();
1140 break;
1142 case R_SH_DIR8WPN:
1143 case R_SH_DIR8WPZ:
1144 insn += adjust / 2;
1145 if ((oinsn & 0xff00) != (insn & 0xff00))
1146 overflow = TRUE;
1147 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1148 break;
1150 case R_SH_IND12W:
1151 insn += adjust / 2;
1152 if ((oinsn & 0xf000) != (insn & 0xf000))
1153 overflow = TRUE;
1154 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1155 break;
1157 case R_SH_DIR8WPL:
1158 BFD_ASSERT (adjust == count || count >= 4);
1159 if (count >= 4)
1160 insn += adjust / 4;
1161 else
1163 if ((irel->r_offset & 3) == 0)
1164 ++insn;
1166 if ((oinsn & 0xff00) != (insn & 0xff00))
1167 overflow = TRUE;
1168 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1169 break;
1171 case R_SH_SWITCH8:
1172 voff += adjust;
1173 if (voff < 0 || voff >= 0xff)
1174 overflow = TRUE;
1175 bfd_put_8 (abfd, voff, contents + nraddr);
1176 break;
1178 case R_SH_SWITCH16:
1179 voff += adjust;
1180 if (voff < - 0x8000 || voff >= 0x8000)
1181 overflow = TRUE;
1182 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1183 break;
1185 case R_SH_SWITCH32:
1186 voff += adjust;
1187 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1188 break;
1190 case R_SH_USES:
1191 irel->r_addend += adjust;
1192 break;
1195 if (overflow)
1197 _bfd_error_handler
1198 /* xgettext:c-format */
1199 (_("%B: %#Lx: fatal: reloc overflow while relaxing"),
1200 abfd, irel->r_offset);
1201 bfd_set_error (bfd_error_bad_value);
1202 return FALSE;
1206 irel->r_offset = nraddr;
1209 /* Look through all the other sections. If there contain any IMM32
1210 relocs against internal symbols which we are not going to adjust
1211 below, we may need to adjust the addends. */
1212 for (o = abfd->sections; o != NULL; o = o->next)
1214 Elf_Internal_Rela *internal_relocs;
1215 Elf_Internal_Rela *irelscan, *irelscanend;
1216 bfd_byte *ocontents;
1218 if (o == sec
1219 || (o->flags & SEC_RELOC) == 0
1220 || o->reloc_count == 0)
1221 continue;
1223 /* We always cache the relocs. Perhaps, if info->keep_memory is
1224 FALSE, we should free them, if we are permitted to, when we
1225 leave sh_coff_relax_section. */
1226 internal_relocs = (_bfd_elf_link_read_relocs
1227 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
1228 if (internal_relocs == NULL)
1229 return FALSE;
1231 ocontents = NULL;
1232 irelscanend = internal_relocs + o->reloc_count;
1233 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1235 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1236 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1238 bfd_vma start, stop;
1239 bfd_signed_vma voff;
1241 if (ocontents == NULL)
1243 if (elf_section_data (o)->this_hdr.contents != NULL)
1244 ocontents = elf_section_data (o)->this_hdr.contents;
1245 else
1247 /* We always cache the section contents.
1248 Perhaps, if info->keep_memory is FALSE, we
1249 should free them, if we are permitted to,
1250 when we leave sh_coff_relax_section. */
1251 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1253 if (ocontents != NULL)
1254 free (ocontents);
1255 return FALSE;
1258 elf_section_data (o)->this_hdr.contents = ocontents;
1262 stop = irelscan->r_offset;
1263 start
1264 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1266 /* STOP is in a different section, so it won't change. */
1267 if (start > addr && start < toaddr)
1268 irelscan->r_addend += count;
1270 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1271 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1273 if (start > addr
1274 && start < toaddr
1275 && (stop <= addr || stop >= toaddr))
1276 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1277 ocontents + irelscan->r_offset);
1278 else if (stop > addr
1279 && stop < toaddr
1280 && (start <= addr || start >= toaddr))
1281 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1282 ocontents + irelscan->r_offset);
1285 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1286 continue;
1288 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1289 continue;
1292 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1293 if (isym->st_shndx == sec_shndx
1294 && (isym->st_value <= addr
1295 || isym->st_value >= toaddr))
1297 bfd_vma val;
1299 if (ocontents == NULL)
1301 if (elf_section_data (o)->this_hdr.contents != NULL)
1302 ocontents = elf_section_data (o)->this_hdr.contents;
1303 else
1305 /* We always cache the section contents.
1306 Perhaps, if info->keep_memory is FALSE, we
1307 should free them, if we are permitted to,
1308 when we leave sh_coff_relax_section. */
1309 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1311 if (ocontents != NULL)
1312 free (ocontents);
1313 return FALSE;
1316 elf_section_data (o)->this_hdr.contents = ocontents;
1320 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1321 val += isym->st_value;
1322 if (val > addr && val < toaddr)
1323 bfd_put_32 (abfd, val - count,
1324 ocontents + irelscan->r_offset);
1329 /* Adjust the local symbols defined in this section. */
1330 isymend = isymbuf + symtab_hdr->sh_info;
1331 for (isym = isymbuf; isym < isymend; isym++)
1333 if (isym->st_shndx == sec_shndx
1334 && isym->st_value > addr
1335 && isym->st_value < toaddr)
1336 isym->st_value -= count;
1339 /* Now adjust the global symbols defined in this section. */
1340 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1341 - symtab_hdr->sh_info);
1342 sym_hashes = elf_sym_hashes (abfd);
1343 end_hashes = sym_hashes + symcount;
1344 for (; sym_hashes < end_hashes; sym_hashes++)
1346 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1347 if ((sym_hash->root.type == bfd_link_hash_defined
1348 || sym_hash->root.type == bfd_link_hash_defweak)
1349 && sym_hash->root.u.def.section == sec
1350 && sym_hash->root.u.def.value > addr
1351 && sym_hash->root.u.def.value < toaddr)
1353 sym_hash->root.u.def.value -= count;
1357 /* See if we can move the ALIGN reloc forward. We have adjusted
1358 r_offset for it already. */
1359 if (irelalign != NULL)
1361 bfd_vma alignto, alignaddr;
1363 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1364 alignaddr = BFD_ALIGN (irelalign->r_offset,
1365 1 << irelalign->r_addend);
1366 if (alignto != alignaddr)
1368 /* Tail recursion. */
1369 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1370 (int) (alignto - alignaddr));
1374 return TRUE;
1377 /* Look for loads and stores which we can align to four byte
1378 boundaries. This is like sh_align_loads in coff-sh.c. */
1380 static bfd_boolean
1381 sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1382 Elf_Internal_Rela *internal_relocs,
1383 bfd_byte *contents ATTRIBUTE_UNUSED,
1384 bfd_boolean *pswapped)
1386 Elf_Internal_Rela *irel, *irelend;
1387 bfd_vma *labels = NULL;
1388 bfd_vma *label, *label_end;
1389 bfd_size_type amt;
1391 *pswapped = FALSE;
1393 irelend = internal_relocs + sec->reloc_count;
1395 /* Get all the addresses with labels on them. */
1396 amt = sec->reloc_count;
1397 amt *= sizeof (bfd_vma);
1398 labels = (bfd_vma *) bfd_malloc (amt);
1399 if (labels == NULL)
1400 goto error_return;
1401 label_end = labels;
1402 for (irel = internal_relocs; irel < irelend; irel++)
1404 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1406 *label_end = irel->r_offset;
1407 ++label_end;
1411 /* Note that the assembler currently always outputs relocs in
1412 address order. If that ever changes, this code will need to sort
1413 the label values and the relocs. */
1415 label = labels;
1417 for (irel = internal_relocs; irel < irelend; irel++)
1419 bfd_vma start, stop;
1421 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1422 continue;
1424 start = irel->r_offset;
1426 for (irel++; irel < irelend; irel++)
1427 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1428 break;
1429 if (irel < irelend)
1430 stop = irel->r_offset;
1431 else
1432 stop = sec->size;
1434 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1435 internal_relocs, &label,
1436 label_end, start, stop, pswapped))
1437 goto error_return;
1440 free (labels);
1442 return TRUE;
1444 error_return:
1445 if (labels != NULL)
1446 free (labels);
1447 return FALSE;
1450 #ifndef SH64_ELF
1451 /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1453 static bfd_boolean
1454 sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1455 bfd_byte *contents, bfd_vma addr)
1457 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1458 unsigned short i1, i2;
1459 Elf_Internal_Rela *irel, *irelend;
1461 /* Swap the instructions themselves. */
1462 i1 = bfd_get_16 (abfd, contents + addr);
1463 i2 = bfd_get_16 (abfd, contents + addr + 2);
1464 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1465 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1467 /* Adjust all reloc addresses. */
1468 irelend = internal_relocs + sec->reloc_count;
1469 for (irel = internal_relocs; irel < irelend; irel++)
1471 enum elf_sh_reloc_type type;
1472 int add;
1474 /* There are a few special types of relocs that we don't want to
1475 adjust. These relocs do not apply to the instruction itself,
1476 but are only associated with the address. */
1477 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1478 if (type == R_SH_ALIGN
1479 || type == R_SH_CODE
1480 || type == R_SH_DATA
1481 || type == R_SH_LABEL)
1482 continue;
1484 /* If an R_SH_USES reloc points to one of the addresses being
1485 swapped, we must adjust it. It would be incorrect to do this
1486 for a jump, though, since we want to execute both
1487 instructions after the jump. (We have avoided swapping
1488 around a label, so the jump will not wind up executing an
1489 instruction it shouldn't). */
1490 if (type == R_SH_USES)
1492 bfd_vma off;
1494 off = irel->r_offset + 4 + irel->r_addend;
1495 if (off == addr)
1496 irel->r_offset += 2;
1497 else if (off == addr + 2)
1498 irel->r_offset -= 2;
1501 if (irel->r_offset == addr)
1503 irel->r_offset += 2;
1504 add = -2;
1506 else if (irel->r_offset == addr + 2)
1508 irel->r_offset -= 2;
1509 add = 2;
1511 else
1512 add = 0;
1514 if (add != 0)
1516 bfd_byte *loc;
1517 unsigned short insn, oinsn;
1518 bfd_boolean overflow;
1520 loc = contents + irel->r_offset;
1521 overflow = FALSE;
1522 switch (type)
1524 default:
1525 break;
1527 case R_SH_DIR8WPN:
1528 case R_SH_DIR8WPZ:
1529 insn = bfd_get_16 (abfd, loc);
1530 oinsn = insn;
1531 insn += add / 2;
1532 if ((oinsn & 0xff00) != (insn & 0xff00))
1533 overflow = TRUE;
1534 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1535 break;
1537 case R_SH_IND12W:
1538 insn = bfd_get_16 (abfd, loc);
1539 oinsn = insn;
1540 insn += add / 2;
1541 if ((oinsn & 0xf000) != (insn & 0xf000))
1542 overflow = TRUE;
1543 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1544 break;
1546 case R_SH_DIR8WPL:
1547 /* This reloc ignores the least significant 3 bits of
1548 the program counter before adding in the offset.
1549 This means that if ADDR is at an even address, the
1550 swap will not affect the offset. If ADDR is an at an
1551 odd address, then the instruction will be crossing a
1552 four byte boundary, and must be adjusted. */
1553 if ((addr & 3) != 0)
1555 insn = bfd_get_16 (abfd, loc);
1556 oinsn = insn;
1557 insn += add / 2;
1558 if ((oinsn & 0xff00) != (insn & 0xff00))
1559 overflow = TRUE;
1560 bfd_put_16 (abfd, (bfd_vma) insn, loc);
1563 break;
1566 if (overflow)
1568 _bfd_error_handler
1569 /* xgettext:c-format */
1570 (_("%B: %#Lx: fatal: reloc overflow while relaxing"),
1571 abfd, irel->r_offset);
1572 bfd_set_error (bfd_error_bad_value);
1573 return FALSE;
1578 return TRUE;
1580 #endif /* defined SH64_ELF */
1582 /* Describes one of the various PLT styles. */
1584 struct elf_sh_plt_info
1586 /* The template for the first PLT entry, or NULL if there is no special
1587 first entry. */
1588 const bfd_byte *plt0_entry;
1590 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1591 bfd_vma plt0_entry_size;
1593 /* Index I is the offset into PLT0_ENTRY of a pointer to
1594 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1595 if there is no such pointer. */
1596 bfd_vma plt0_got_fields[3];
1598 /* The template for a symbol's PLT entry. */
1599 const bfd_byte *symbol_entry;
1601 /* The size of SYMBOL_ENTRY in bytes. */
1602 bfd_vma symbol_entry_size;
1604 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1605 on all targets. The comments by each member indicate the value
1606 that the field must hold. */
1607 struct {
1608 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1609 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1610 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1611 bfd_boolean got20; /* TRUE if got_entry points to a movi20
1612 instruction (instead of a constant pool
1613 entry). */
1614 } symbol_fields;
1616 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1617 bfd_vma symbol_resolve_offset;
1619 /* A different PLT layout which can be used for the first
1620 MAX_SHORT_PLT entries. It must share the same plt0. NULL in
1621 other cases. */
1622 const struct elf_sh_plt_info *short_plt;
1625 #ifdef INCLUDE_SHMEDIA
1627 /* The size in bytes of an entry in the procedure linkage table. */
1629 #define ELF_PLT_ENTRY_SIZE 64
1631 /* First entry in an absolute procedure linkage table look like this. */
1633 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1635 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1636 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1637 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1638 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1639 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1640 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1641 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1642 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1643 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1644 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1645 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1646 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1647 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1648 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1649 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1650 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1653 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1655 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1656 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1657 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1658 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1659 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1660 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1661 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1662 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1663 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1664 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1665 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1666 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1667 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1668 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1669 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1670 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1673 /* Sebsequent entries in an absolute procedure linkage table look like
1674 this. */
1676 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1678 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1679 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1680 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1681 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1682 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1683 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1684 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1685 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1686 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1687 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1688 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1689 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1690 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1691 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1692 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1693 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1696 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1698 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1699 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1700 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1701 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1702 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1703 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1704 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1705 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1706 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1707 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1708 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1709 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1710 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1711 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1712 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1713 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1716 /* Entries in a PIC procedure linkage table look like this. */
1718 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1720 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1721 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1722 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1723 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1724 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1725 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1726 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1727 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1728 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
1729 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
1730 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1731 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1732 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1733 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1734 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1735 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1738 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1740 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1741 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1742 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1743 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1744 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1745 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1746 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1747 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1748 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
1749 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
1750 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1751 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1752 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1753 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1754 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1755 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1758 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1761 /* Big-endian non-PIC. */
1762 elf_sh_plt0_entry_be,
1763 ELF_PLT_ENTRY_SIZE,
1764 { 0, MINUS_ONE, MINUS_ONE },
1765 elf_sh_plt_entry_be,
1766 ELF_PLT_ENTRY_SIZE,
1767 { 0, 32, 48, FALSE },
1768 33, /* includes ISA encoding */
1769 NULL
1772 /* Little-endian non-PIC. */
1773 elf_sh_plt0_entry_le,
1774 ELF_PLT_ENTRY_SIZE,
1775 { 0, MINUS_ONE, MINUS_ONE },
1776 elf_sh_plt_entry_le,
1777 ELF_PLT_ENTRY_SIZE,
1778 { 0, 32, 48, FALSE },
1779 33, /* includes ISA encoding */
1780 NULL
1785 /* Big-endian PIC. */
1786 elf_sh_plt0_entry_be,
1787 ELF_PLT_ENTRY_SIZE,
1788 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1789 elf_sh_pic_plt_entry_be,
1790 ELF_PLT_ENTRY_SIZE,
1791 { 0, MINUS_ONE, 52, FALSE },
1792 33, /* includes ISA encoding */
1793 NULL
1796 /* Little-endian PIC. */
1797 elf_sh_plt0_entry_le,
1798 ELF_PLT_ENTRY_SIZE,
1799 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1800 elf_sh_pic_plt_entry_le,
1801 ELF_PLT_ENTRY_SIZE,
1802 { 0, MINUS_ONE, 52, FALSE },
1803 33, /* includes ISA encoding */
1804 NULL
1809 /* Return offset of the linker in PLT0 entry. */
1810 #define elf_sh_plt0_gotplt_offset(info) 0
1812 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1813 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1814 not data.
1816 On SH64, each 32-bit field is loaded by a movi/shori pair. */
1818 inline static void
1819 install_plt_field (bfd *output_bfd, bfd_boolean code_p,
1820 unsigned long value, bfd_byte *addr)
1822 value |= code_p;
1823 bfd_put_32 (output_bfd,
1824 bfd_get_32 (output_bfd, addr)
1825 | ((value >> 6) & 0x3fffc00),
1826 addr);
1827 bfd_put_32 (output_bfd,
1828 bfd_get_32 (output_bfd, addr + 4)
1829 | ((value << 10) & 0x3fffc00),
1830 addr + 4);
1833 /* Return the type of PLT associated with ABFD. PIC_P is true if
1834 the object is position-independent. */
1836 static const struct elf_sh_plt_info *
1837 get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
1839 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
1841 #else
1842 /* The size in bytes of an entry in the procedure linkage table. */
1844 #define ELF_PLT_ENTRY_SIZE 28
1846 /* First entry in an absolute procedure linkage table look like this. */
1848 /* Note - this code has been "optimised" not to use r2. r2 is used by
1849 GCC to return the address of large structures, so it should not be
1850 corrupted here. This does mean however, that this PLT does not conform
1851 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1852 and r2 contains the GOT id. This version stores the GOT id in r0 and
1853 ignores the type. Loaders can easily detect this difference however,
1854 since the type will always be 0 or 8, and the GOT ids will always be
1855 greater than or equal to 12. */
1856 static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1858 0xd0, 0x05, /* mov.l 2f,r0 */
1859 0x60, 0x02, /* mov.l @r0,r0 */
1860 0x2f, 0x06, /* mov.l r0,@-r15 */
1861 0xd0, 0x03, /* mov.l 1f,r0 */
1862 0x60, 0x02, /* mov.l @r0,r0 */
1863 0x40, 0x2b, /* jmp @r0 */
1864 0x60, 0xf6, /* mov.l @r15+,r0 */
1865 0x00, 0x09, /* nop */
1866 0x00, 0x09, /* nop */
1867 0x00, 0x09, /* nop */
1868 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1869 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1872 static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1874 0x05, 0xd0, /* mov.l 2f,r0 */
1875 0x02, 0x60, /* mov.l @r0,r0 */
1876 0x06, 0x2f, /* mov.l r0,@-r15 */
1877 0x03, 0xd0, /* mov.l 1f,r0 */
1878 0x02, 0x60, /* mov.l @r0,r0 */
1879 0x2b, 0x40, /* jmp @r0 */
1880 0xf6, 0x60, /* mov.l @r15+,r0 */
1881 0x09, 0x00, /* nop */
1882 0x09, 0x00, /* nop */
1883 0x09, 0x00, /* nop */
1884 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1885 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1888 /* Sebsequent entries in an absolute procedure linkage table look like
1889 this. */
1891 static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1893 0xd0, 0x04, /* mov.l 1f,r0 */
1894 0x60, 0x02, /* mov.l @(r0,r12),r0 */
1895 0xd1, 0x02, /* mov.l 0f,r1 */
1896 0x40, 0x2b, /* jmp @r0 */
1897 0x60, 0x13, /* mov r1,r0 */
1898 0xd1, 0x03, /* mov.l 2f,r1 */
1899 0x40, 0x2b, /* jmp @r0 */
1900 0x00, 0x09, /* nop */
1901 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1902 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1903 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1906 static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1908 0x04, 0xd0, /* mov.l 1f,r0 */
1909 0x02, 0x60, /* mov.l @r0,r0 */
1910 0x02, 0xd1, /* mov.l 0f,r1 */
1911 0x2b, 0x40, /* jmp @r0 */
1912 0x13, 0x60, /* mov r1,r0 */
1913 0x03, 0xd1, /* mov.l 2f,r1 */
1914 0x2b, 0x40, /* jmp @r0 */
1915 0x09, 0x00, /* nop */
1916 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1917 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1918 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1921 /* Entries in a PIC procedure linkage table look like this. */
1923 static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1925 0xd0, 0x04, /* mov.l 1f,r0 */
1926 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1927 0x40, 0x2b, /* jmp @r0 */
1928 0x00, 0x09, /* nop */
1929 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1930 0xd1, 0x03, /* mov.l 2f,r1 */
1931 0x40, 0x2b, /* jmp @r0 */
1932 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1933 0x00, 0x09, /* nop */
1934 0x00, 0x09, /* nop */
1935 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1936 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1939 static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1941 0x04, 0xd0, /* mov.l 1f,r0 */
1942 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1943 0x2b, 0x40, /* jmp @r0 */
1944 0x09, 0x00, /* nop */
1945 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1946 0x03, 0xd1, /* mov.l 2f,r1 */
1947 0x2b, 0x40, /* jmp @r0 */
1948 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1949 0x09, 0x00, /* nop */
1950 0x09, 0x00, /* nop */
1951 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1952 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1955 static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1958 /* Big-endian non-PIC. */
1959 elf_sh_plt0_entry_be,
1960 ELF_PLT_ENTRY_SIZE,
1961 { MINUS_ONE, 24, 20 },
1962 elf_sh_plt_entry_be,
1963 ELF_PLT_ENTRY_SIZE,
1964 { 20, 16, 24, FALSE },
1966 NULL
1969 /* Little-endian non-PIC. */
1970 elf_sh_plt0_entry_le,
1971 ELF_PLT_ENTRY_SIZE,
1972 { MINUS_ONE, 24, 20 },
1973 elf_sh_plt_entry_le,
1974 ELF_PLT_ENTRY_SIZE,
1975 { 20, 16, 24, FALSE },
1977 NULL
1982 /* Big-endian PIC. */
1983 elf_sh_plt0_entry_be,
1984 ELF_PLT_ENTRY_SIZE,
1985 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1986 elf_sh_pic_plt_entry_be,
1987 ELF_PLT_ENTRY_SIZE,
1988 { 20, MINUS_ONE, 24, FALSE },
1990 NULL
1993 /* Little-endian PIC. */
1994 elf_sh_plt0_entry_le,
1995 ELF_PLT_ENTRY_SIZE,
1996 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1997 elf_sh_pic_plt_entry_le,
1998 ELF_PLT_ENTRY_SIZE,
1999 { 20, MINUS_ONE, 24, FALSE },
2001 NULL
2006 #define VXWORKS_PLT_HEADER_SIZE 12
2007 #define VXWORKS_PLT_ENTRY_SIZE 24
2009 static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
2011 0xd1, 0x01, /* mov.l @(8,pc),r1 */
2012 0x61, 0x12, /* mov.l @r1,r1 */
2013 0x41, 0x2b, /* jmp @r1 */
2014 0x00, 0x09, /* nop */
2015 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2018 static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
2020 0x01, 0xd1, /* mov.l @(8,pc),r1 */
2021 0x12, 0x61, /* mov.l @r1,r1 */
2022 0x2b, 0x41, /* jmp @r1 */
2023 0x09, 0x00, /* nop */
2024 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
2027 static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2029 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2030 0x60, 0x02, /* mov.l @r0,r0 */
2031 0x40, 0x2b, /* jmp @r0 */
2032 0x00, 0x09, /* nop */
2033 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2034 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2035 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
2036 0x00, 0x09, /* nop */
2037 0x00, 0x09, /* nop */
2038 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2041 static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2043 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2044 0x02, 0x60, /* mov.l @r0,r0 */
2045 0x2b, 0x40, /* jmp @r0 */
2046 0x09, 0x00, /* nop */
2047 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
2048 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2049 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
2050 0x09, 0x00, /* nop */
2051 0x09, 0x00, /* nop */
2052 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2055 static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
2057 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2058 0x00, 0xce, /* mov.l @(r0,r12),r0 */
2059 0x40, 0x2b, /* jmp @r0 */
2060 0x00, 0x09, /* nop */
2061 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2062 0xd0, 0x01, /* mov.l @(8,pc),r0 */
2063 0x51, 0xc2, /* mov.l @(8,r12),r1 */
2064 0x41, 0x2b, /* jmp @r1 */
2065 0x00, 0x09, /* nop */
2066 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2069 static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
2071 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2072 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2073 0x2b, 0x40, /* jmp @r0 */
2074 0x09, 0x00, /* nop */
2075 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2076 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2077 0xc2, 0x51, /* mov.l @(8,r12),r1 */
2078 0x2b, 0x41, /* jmp @r1 */
2079 0x09, 0x00, /* nop */
2080 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2083 static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
2086 /* Big-endian non-PIC. */
2087 vxworks_sh_plt0_entry_be,
2088 VXWORKS_PLT_HEADER_SIZE,
2089 { MINUS_ONE, MINUS_ONE, 8 },
2090 vxworks_sh_plt_entry_be,
2091 VXWORKS_PLT_ENTRY_SIZE,
2092 { 8, 14, 20, FALSE },
2094 NULL
2097 /* Little-endian non-PIC. */
2098 vxworks_sh_plt0_entry_le,
2099 VXWORKS_PLT_HEADER_SIZE,
2100 { MINUS_ONE, MINUS_ONE, 8 },
2101 vxworks_sh_plt_entry_le,
2102 VXWORKS_PLT_ENTRY_SIZE,
2103 { 8, 14, 20, FALSE },
2105 NULL
2110 /* Big-endian PIC. */
2111 NULL,
2113 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2114 vxworks_sh_pic_plt_entry_be,
2115 VXWORKS_PLT_ENTRY_SIZE,
2116 { 8, MINUS_ONE, 20, FALSE },
2118 NULL
2121 /* Little-endian PIC. */
2122 NULL,
2124 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2125 vxworks_sh_pic_plt_entry_le,
2126 VXWORKS_PLT_ENTRY_SIZE,
2127 { 8, MINUS_ONE, 20, FALSE },
2129 NULL
2134 /* FDPIC PLT entries. Two unimplemented optimizations for lazy
2135 binding are to omit the lazy binding stub when linking with -z now
2136 and to move lazy binding stubs into a separate region for better
2137 cache behavior. */
2139 #define FDPIC_PLT_ENTRY_SIZE 28
2140 #define FDPIC_PLT_LAZY_OFFSET 20
2142 /* FIXME: The lazy binding stub requires a plt0 - which may need to be
2143 duplicated if it is out of range, or which can be inlined. So
2144 right now it is always inlined, which wastes a word per stub. It
2145 might be easier to handle the duplication if we put the lazy
2146 stubs separately. */
2148 static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] =
2150 0xd0, 0x02, /* mov.l @(12,pc),r0 */
2151 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2152 0x70, 0x04, /* add #4, r0 */
2153 0x41, 0x2b, /* jmp @r1 */
2154 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2155 0x00, 0x09, /* nop */
2156 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2157 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2158 0x60, 0xc2, /* mov.l @r12,r0 */
2159 0x40, 0x2b, /* jmp @r0 */
2160 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2161 0x00, 0x09, /* nop */
2164 static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] =
2166 0x02, 0xd0, /* mov.l @(12,pc),r0 */
2167 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2168 0x04, 0x70, /* add #4, r0 */
2169 0x2b, 0x41, /* jmp @r1 */
2170 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2171 0x09, 0x00, /* nop */
2172 0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
2173 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2174 0xc2, 0x60, /* mov.l @r12,r0 */
2175 0x2b, 0x40, /* jmp @r0 */
2176 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2177 0x09, 0x00, /* nop */
2180 static const struct elf_sh_plt_info fdpic_sh_plts[2] = {
2182 /* Big-endian PIC. */
2183 NULL,
2185 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2186 fdpic_sh_plt_entry_be,
2187 FDPIC_PLT_ENTRY_SIZE,
2188 { 12, MINUS_ONE, 16, FALSE },
2189 FDPIC_PLT_LAZY_OFFSET,
2190 NULL
2193 /* Little-endian PIC. */
2194 NULL,
2196 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2197 fdpic_sh_plt_entry_le,
2198 FDPIC_PLT_ENTRY_SIZE,
2199 { 12, MINUS_ONE, 16, FALSE },
2200 FDPIC_PLT_LAZY_OFFSET,
2201 NULL
2205 /* On SH2A, we can use the movi20 instruction to generate shorter PLT
2206 entries for the first 64K slots. We use the normal FDPIC PLT entry
2207 past that point; we could also use movi20s, which might be faster,
2208 but would not be any smaller. */
2210 #define FDPIC_SH2A_PLT_ENTRY_SIZE 24
2211 #define FDPIC_SH2A_PLT_LAZY_OFFSET 16
2213 static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2215 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2216 0x01, 0xce, /* mov.l @(r0,r12),r1 */
2217 0x70, 0x04, /* add #4, r0 */
2218 0x41, 0x2b, /* jmp @r1 */
2219 0x0c, 0xce, /* mov.l @(r0,r12),r12 */
2220 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2221 0x60, 0xc2, /* mov.l @r12,r0 */
2222 0x40, 0x2b, /* jmp @r0 */
2223 0x53, 0xc1, /* mov.l @(4,r12),r3 */
2224 0x00, 0x09, /* nop */
2227 static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] =
2229 0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
2230 0xce, 0x01, /* mov.l @(r0,r12),r1 */
2231 0x04, 0x70, /* add #4, r0 */
2232 0x2b, 0x41, /* jmp @r1 */
2233 0xce, 0x0c, /* mov.l @(r0,r12),r12 */
2234 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2235 0xc2, 0x60, /* mov.l @r12,r0 */
2236 0x2b, 0x40, /* jmp @r0 */
2237 0xc1, 0x53, /* mov.l @(4,r12),r3 */
2238 0x09, 0x00, /* nop */
2241 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = {
2242 /* Big-endian FDPIC, max index 64K. */
2243 NULL,
2245 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2246 fdpic_sh2a_plt_entry_be,
2247 FDPIC_SH2A_PLT_ENTRY_SIZE,
2248 { 0, MINUS_ONE, 12, TRUE },
2249 FDPIC_SH2A_PLT_LAZY_OFFSET,
2250 NULL
2253 static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = {
2254 /* Little-endian FDPIC, max index 64K. */
2255 NULL,
2257 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2258 fdpic_sh2a_plt_entry_le,
2259 FDPIC_SH2A_PLT_ENTRY_SIZE,
2260 { 0, MINUS_ONE, 12, TRUE },
2261 FDPIC_SH2A_PLT_LAZY_OFFSET,
2262 NULL
2265 static const struct elf_sh_plt_info fdpic_sh2a_plts[2] = {
2267 /* Big-endian PIC. */
2268 NULL,
2270 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2271 fdpic_sh_plt_entry_be,
2272 FDPIC_PLT_ENTRY_SIZE,
2273 { 12, MINUS_ONE, 16, FALSE },
2274 FDPIC_PLT_LAZY_OFFSET,
2275 &fdpic_sh2a_short_plt_be
2278 /* Little-endian PIC. */
2279 NULL,
2281 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2282 fdpic_sh_plt_entry_le,
2283 FDPIC_PLT_ENTRY_SIZE,
2284 { 12, MINUS_ONE, 16, FALSE },
2285 FDPIC_PLT_LAZY_OFFSET,
2286 &fdpic_sh2a_short_plt_le
2290 /* Return the type of PLT associated with ABFD. PIC_P is true if
2291 the object is position-independent. */
2293 static const struct elf_sh_plt_info *
2294 get_plt_info (bfd *abfd, bfd_boolean pic_p)
2296 if (fdpic_object_p (abfd))
2298 /* If any input file requires SH2A we can use a shorter PLT
2299 sequence. */
2300 if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base)
2301 return &fdpic_sh2a_plts[!bfd_big_endian (abfd)];
2302 else
2303 return &fdpic_sh_plts[!bfd_big_endian (abfd)];
2305 if (vxworks_object_p (abfd))
2306 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2307 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2310 /* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2311 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2312 not data. */
2314 inline static void
2315 install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2316 unsigned long value, bfd_byte *addr)
2318 bfd_put_32 (output_bfd, value, addr);
2320 #endif
2322 /* The number of PLT entries which can use a shorter PLT, if any.
2323 Currently always 64K, since only SH-2A FDPIC uses this; a
2324 20-bit movi20 can address that many function descriptors below
2325 _GLOBAL_OFFSET_TABLE_. */
2326 #define MAX_SHORT_PLT 65536
2328 /* Return the index of the PLT entry at byte offset OFFSET. */
2330 static bfd_vma
2331 get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2333 bfd_vma plt_index = 0;
2335 offset -= info->plt0_entry_size;
2336 if (info->short_plt != NULL)
2338 if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size)
2340 plt_index = MAX_SHORT_PLT;
2341 offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2343 else
2344 info = info->short_plt;
2346 return plt_index + offset / info->symbol_entry_size;
2349 /* Do the inverse operation. */
2351 static bfd_vma
2352 get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index)
2354 bfd_vma offset = 0;
2356 if (info->short_plt != NULL)
2358 if (plt_index > MAX_SHORT_PLT)
2360 offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2361 plt_index -= MAX_SHORT_PLT;
2363 else
2364 info = info->short_plt;
2366 return (offset + info->plt0_entry_size
2367 + (plt_index * info->symbol_entry_size));
2370 /* The sh linker needs to keep track of the number of relocs that it
2371 decides to copy as dynamic relocs in check_relocs for each symbol.
2372 This is so that it can later discard them if they are found to be
2373 unnecessary. We store the information in a field extending the
2374 regular ELF linker hash table. */
2376 struct elf_sh_dyn_relocs
2378 struct elf_sh_dyn_relocs *next;
2380 /* The input section of the reloc. */
2381 asection *sec;
2383 /* Total number of relocs copied for the input section. */
2384 bfd_size_type count;
2386 /* Number of pc-relative relocs copied for the input section. */
2387 bfd_size_type pc_count;
2390 union gotref
2392 bfd_signed_vma refcount;
2393 bfd_vma offset;
2396 /* sh ELF linker hash entry. */
2398 struct elf_sh_link_hash_entry
2400 struct elf_link_hash_entry root;
2402 #ifdef INCLUDE_SHMEDIA
2403 union
2405 bfd_signed_vma refcount;
2406 bfd_vma offset;
2407 } datalabel_got;
2408 #endif
2410 /* Track dynamic relocs copied for this symbol. */
2411 struct elf_sh_dyn_relocs *dyn_relocs;
2413 bfd_signed_vma gotplt_refcount;
2415 /* A local function descriptor, for FDPIC. The refcount counts
2416 R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2417 relocations; the PLT and GOT entry are accounted
2418 for separately. After adjust_dynamic_symbol, the offset is
2419 MINUS_ONE if there is no local descriptor (dynamic linker
2420 managed and no PLT entry, or undefined weak non-dynamic).
2421 During check_relocs we do not yet know whether the local
2422 descriptor will be canonical. */
2423 union gotref funcdesc;
2425 /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2426 and thus require fixups or relocations. */
2427 bfd_signed_vma abs_funcdesc_refcount;
2429 enum got_type {
2430 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC
2431 } got_type;
2434 #define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2436 struct sh_elf_obj_tdata
2438 struct elf_obj_tdata root;
2440 /* got_type for each local got entry. */
2441 char *local_got_type;
2443 /* Function descriptor refcount and offset for each local symbol. */
2444 union gotref *local_funcdesc;
2447 #define sh_elf_tdata(abfd) \
2448 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2450 #define sh_elf_local_got_type(abfd) \
2451 (sh_elf_tdata (abfd)->local_got_type)
2453 #define sh_elf_local_funcdesc(abfd) \
2454 (sh_elf_tdata (abfd)->local_funcdesc)
2456 #define is_sh_elf(bfd) \
2457 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2458 && elf_tdata (bfd) != NULL \
2459 && elf_object_id (bfd) == SH_ELF_DATA)
2461 /* Override the generic function because we need to store sh_elf_obj_tdata
2462 as the specific tdata. */
2464 static bfd_boolean
2465 sh_elf_mkobject (bfd *abfd)
2467 return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata),
2468 SH_ELF_DATA);
2471 /* sh ELF linker hash table. */
2473 struct elf_sh_link_hash_table
2475 struct elf_link_hash_table root;
2477 /* Short-cuts to get to dynamic linker sections. */
2478 asection *sdynbss;
2479 asection *srelbss;
2480 asection *sfuncdesc;
2481 asection *srelfuncdesc;
2482 asection *srofixup;
2484 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2485 asection *srelplt2;
2487 /* Small local sym cache. */
2488 struct sym_cache sym_cache;
2490 /* A counter or offset to track a TLS got entry. */
2491 union
2493 bfd_signed_vma refcount;
2494 bfd_vma offset;
2495 } tls_ldm_got;
2497 /* The type of PLT to use. */
2498 const struct elf_sh_plt_info *plt_info;
2500 /* True if the target system is VxWorks. */
2501 bfd_boolean vxworks_p;
2503 /* True if the target system uses FDPIC. */
2504 bfd_boolean fdpic_p;
2507 /* Traverse an sh ELF linker hash table. */
2509 #define sh_elf_link_hash_traverse(table, func, info) \
2510 (elf_link_hash_traverse \
2511 (&(table)->root, \
2512 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
2513 (info)))
2515 /* Get the sh ELF linker hash table from a link_info structure. */
2517 #define sh_elf_hash_table(p) \
2518 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2519 == SH_ELF_DATA ? ((struct elf_sh_link_hash_table *) ((p)->hash)) : NULL)
2521 /* Create an entry in an sh ELF linker hash table. */
2523 static struct bfd_hash_entry *
2524 sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2525 struct bfd_hash_table *table,
2526 const char *string)
2528 struct elf_sh_link_hash_entry *ret =
2529 (struct elf_sh_link_hash_entry *) entry;
2531 /* Allocate the structure if it has not already been allocated by a
2532 subclass. */
2533 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2534 ret = ((struct elf_sh_link_hash_entry *)
2535 bfd_hash_allocate (table,
2536 sizeof (struct elf_sh_link_hash_entry)));
2537 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2538 return (struct bfd_hash_entry *) ret;
2540 /* Call the allocation method of the superclass. */
2541 ret = ((struct elf_sh_link_hash_entry *)
2542 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2543 table, string));
2544 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2546 ret->dyn_relocs = NULL;
2547 ret->gotplt_refcount = 0;
2548 #ifdef INCLUDE_SHMEDIA
2549 ret->datalabel_got.refcount = ret->root.got.refcount;
2550 #endif
2551 ret->funcdesc.refcount = 0;
2552 ret->abs_funcdesc_refcount = 0;
2553 ret->got_type = GOT_UNKNOWN;
2556 return (struct bfd_hash_entry *) ret;
2559 /* Create an sh ELF linker hash table. */
2561 static struct bfd_link_hash_table *
2562 sh_elf_link_hash_table_create (bfd *abfd)
2564 struct elf_sh_link_hash_table *ret;
2565 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
2567 ret = (struct elf_sh_link_hash_table *) bfd_zmalloc (amt);
2568 if (ret == (struct elf_sh_link_hash_table *) NULL)
2569 return NULL;
2571 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2572 sh_elf_link_hash_newfunc,
2573 sizeof (struct elf_sh_link_hash_entry),
2574 SH_ELF_DATA))
2576 free (ret);
2577 return NULL;
2580 ret->vxworks_p = vxworks_object_p (abfd);
2581 ret->fdpic_p = fdpic_object_p (abfd);
2583 return &ret->root.root;
2586 static bfd_boolean
2587 sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2588 struct bfd_link_info *info, asection *p)
2590 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
2592 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2593 if (!htab->fdpic_p)
2594 return TRUE;
2596 /* We need dynamic symbols for every section, since segments can
2597 relocate independently. */
2598 switch (elf_section_data (p)->this_hdr.sh_type)
2600 case SHT_PROGBITS:
2601 case SHT_NOBITS:
2602 /* If sh_type is yet undecided, assume it could be
2603 SHT_PROGBITS/SHT_NOBITS. */
2604 case SHT_NULL:
2605 return FALSE;
2607 /* There shouldn't be section relative relocations
2608 against any other section. */
2609 default:
2610 return TRUE;
2614 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2615 shortcuts to them in our hash table. */
2617 static bfd_boolean
2618 create_got_section (bfd *dynobj, struct bfd_link_info *info)
2620 struct elf_sh_link_hash_table *htab;
2622 if (! _bfd_elf_create_got_section (dynobj, info))
2623 return FALSE;
2625 htab = sh_elf_hash_table (info);
2626 if (htab == NULL)
2627 return FALSE;
2629 htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
2630 (SEC_ALLOC | SEC_LOAD
2631 | SEC_HAS_CONTENTS
2632 | SEC_IN_MEMORY
2633 | SEC_LINKER_CREATED));
2634 if (htab->sfuncdesc == NULL
2635 || ! bfd_set_section_alignment (dynobj, htab->sfuncdesc, 2))
2636 return FALSE;
2638 htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
2639 ".rela.got.funcdesc",
2640 (SEC_ALLOC | SEC_LOAD
2641 | SEC_HAS_CONTENTS
2642 | SEC_IN_MEMORY
2643 | SEC_LINKER_CREATED
2644 | SEC_READONLY));
2645 if (htab->srelfuncdesc == NULL
2646 || ! bfd_set_section_alignment (dynobj, htab->srelfuncdesc, 2))
2647 return FALSE;
2649 /* Also create .rofixup. */
2650 htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
2651 (SEC_ALLOC | SEC_LOAD
2652 | SEC_HAS_CONTENTS
2653 | SEC_IN_MEMORY
2654 | SEC_LINKER_CREATED
2655 | SEC_READONLY));
2656 if (htab->srofixup == NULL
2657 || ! bfd_set_section_alignment (dynobj, htab->srofixup, 2))
2658 return FALSE;
2660 return TRUE;
2663 /* Create dynamic sections when linking against a dynamic object. */
2665 static bfd_boolean
2666 sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2668 struct elf_sh_link_hash_table *htab;
2669 flagword flags, pltflags;
2670 asection *s;
2671 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2672 int ptralign = 0;
2674 switch (bed->s->arch_size)
2676 case 32:
2677 ptralign = 2;
2678 break;
2680 case 64:
2681 ptralign = 3;
2682 break;
2684 default:
2685 bfd_set_error (bfd_error_bad_value);
2686 return FALSE;
2689 htab = sh_elf_hash_table (info);
2690 if (htab == NULL)
2691 return FALSE;
2693 if (htab->root.dynamic_sections_created)
2694 return TRUE;
2696 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2697 .rel[a].bss sections. */
2699 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2700 | SEC_LINKER_CREATED);
2702 pltflags = flags;
2703 pltflags |= SEC_CODE;
2704 if (bed->plt_not_loaded)
2705 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2706 if (bed->plt_readonly)
2707 pltflags |= SEC_READONLY;
2709 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
2710 htab->root.splt = s;
2711 if (s == NULL
2712 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2713 return FALSE;
2715 if (bed->want_plt_sym)
2717 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2718 .plt section. */
2719 struct elf_link_hash_entry *h;
2720 struct bfd_link_hash_entry *bh = NULL;
2722 if (! (_bfd_generic_link_add_one_symbol
2723 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2724 (bfd_vma) 0, (const char *) NULL, FALSE,
2725 get_elf_backend_data (abfd)->collect, &bh)))
2726 return FALSE;
2728 h = (struct elf_link_hash_entry *) bh;
2729 h->def_regular = 1;
2730 h->type = STT_OBJECT;
2731 htab->root.hplt = h;
2733 if (bfd_link_pic (info)
2734 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2735 return FALSE;
2738 s = bfd_make_section_anyway_with_flags (abfd,
2739 bed->default_use_rela_p
2740 ? ".rela.plt" : ".rel.plt",
2741 flags | SEC_READONLY);
2742 htab->root.srelplt = s;
2743 if (s == NULL
2744 || ! bfd_set_section_alignment (abfd, s, ptralign))
2745 return FALSE;
2747 if (htab->root.sgot == NULL
2748 && !create_got_section (abfd, info))
2749 return FALSE;
2751 if (bed->want_dynbss)
2753 /* The .dynbss section is a place to put symbols which are defined
2754 by dynamic objects, are referenced by regular objects, and are
2755 not functions. We must allocate space for them in the process
2756 image and use a R_*_COPY reloc to tell the dynamic linker to
2757 initialize them at run time. The linker script puts the .dynbss
2758 section into the .bss section of the final image. */
2759 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2760 SEC_ALLOC | SEC_LINKER_CREATED);
2761 htab->sdynbss = s;
2762 if (s == NULL)
2763 return FALSE;
2765 /* The .rel[a].bss section holds copy relocs. This section is not
2766 normally needed. We need to create it here, though, so that the
2767 linker will map it to an output section. We can't just create it
2768 only if we need it, because we will not know whether we need it
2769 until we have seen all the input files, and the first time the
2770 main linker code calls BFD after examining all the input files
2771 (size_dynamic_sections) the input sections have already been
2772 mapped to the output sections. If the section turns out not to
2773 be needed, we can discard it later. We will never need this
2774 section when generating a shared object, since they do not use
2775 copy relocs. */
2776 if (! bfd_link_pic (info))
2778 s = bfd_make_section_anyway_with_flags (abfd,
2779 (bed->default_use_rela_p
2780 ? ".rela.bss" : ".rel.bss"),
2781 flags | SEC_READONLY);
2782 htab->srelbss = s;
2783 if (s == NULL
2784 || ! bfd_set_section_alignment (abfd, s, ptralign))
2785 return FALSE;
2789 if (htab->vxworks_p)
2791 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2792 return FALSE;
2795 return TRUE;
2798 /* Adjust a symbol defined by a dynamic object and referenced by a
2799 regular object. The current definition is in some section of the
2800 dynamic object, but we're not including those sections. We have to
2801 change the definition to something the rest of the link can
2802 understand. */
2804 static bfd_boolean
2805 sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2806 struct elf_link_hash_entry *h)
2808 struct elf_sh_link_hash_table *htab;
2809 struct elf_sh_link_hash_entry *eh;
2810 struct elf_sh_dyn_relocs *p;
2811 asection *s;
2813 htab = sh_elf_hash_table (info);
2814 if (htab == NULL)
2815 return FALSE;
2817 /* Make sure we know what is going on here. */
2818 BFD_ASSERT (htab->root.dynobj != NULL
2819 && (h->needs_plt
2820 || h->u.weakdef != NULL
2821 || (h->def_dynamic
2822 && h->ref_regular
2823 && !h->def_regular)));
2825 /* If this is a function, put it in the procedure linkage table. We
2826 will fill in the contents of the procedure linkage table later,
2827 when we know the address of the .got section. */
2828 if (h->type == STT_FUNC
2829 || h->needs_plt)
2831 if (h->plt.refcount <= 0
2832 || SYMBOL_CALLS_LOCAL (info, h)
2833 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2834 && h->root.type == bfd_link_hash_undefweak))
2836 /* This case can occur if we saw a PLT reloc in an input
2837 file, but the symbol was never referred to by a dynamic
2838 object. In such a case, we don't actually need to build
2839 a procedure linkage table, and we can just do a REL32
2840 reloc instead. */
2841 h->plt.offset = (bfd_vma) -1;
2842 h->needs_plt = 0;
2845 return TRUE;
2847 else
2848 h->plt.offset = (bfd_vma) -1;
2850 /* If this is a weak symbol, and there is a real definition, the
2851 processor independent code will have arranged for us to see the
2852 real definition first, and we can just use the same value. */
2853 if (h->u.weakdef != NULL)
2855 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2856 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2857 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2858 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2859 if (info->nocopyreloc)
2860 h->non_got_ref = h->u.weakdef->non_got_ref;
2861 return TRUE;
2864 /* This is a reference to a symbol defined by a dynamic object which
2865 is not a function. */
2867 /* If we are creating a shared library, we must presume that the
2868 only references to the symbol are via the global offset table.
2869 For such cases we need not do anything here; the relocations will
2870 be handled correctly by relocate_section. */
2871 if (bfd_link_pic (info))
2872 return TRUE;
2874 /* If there are no references to this symbol that do not use the
2875 GOT, we don't need to generate a copy reloc. */
2876 if (!h->non_got_ref)
2877 return TRUE;
2879 /* If -z nocopyreloc was given, we won't generate them either. */
2880 if (info->nocopyreloc)
2882 h->non_got_ref = 0;
2883 return TRUE;
2886 eh = (struct elf_sh_link_hash_entry *) h;
2887 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2889 s = p->sec->output_section;
2890 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2891 break;
2894 /* If we didn't find any dynamic relocs in sections which needs the
2895 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2896 the copy reloc. */
2897 if (p == NULL)
2899 h->non_got_ref = 0;
2900 return TRUE;
2903 /* We must allocate the symbol in our .dynbss section, which will
2904 become part of the .bss section of the executable. There will be
2905 an entry for this symbol in the .dynsym section. The dynamic
2906 object will contain position independent code, so all references
2907 from the dynamic object to this symbol will go through the global
2908 offset table. The dynamic linker will use the .dynsym entry to
2909 determine the address it must put in the global offset table, so
2910 both the dynamic object and the regular object will refer to the
2911 same memory location for the variable. */
2913 s = htab->sdynbss;
2914 BFD_ASSERT (s != NULL);
2916 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2917 copy the initial value out of the dynamic object and into the
2918 runtime process image. We need to remember the offset into the
2919 .rela.bss section we are going to use. */
2920 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2922 asection *srel;
2924 srel = htab->srelbss;
2925 BFD_ASSERT (srel != NULL);
2926 srel->size += sizeof (Elf32_External_Rela);
2927 h->needs_copy = 1;
2930 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2933 /* Allocate space in .plt, .got and associated reloc sections for
2934 dynamic relocs. */
2936 static bfd_boolean
2937 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2939 struct bfd_link_info *info;
2940 struct elf_sh_link_hash_table *htab;
2941 struct elf_sh_link_hash_entry *eh;
2942 struct elf_sh_dyn_relocs *p;
2944 if (h->root.type == bfd_link_hash_indirect)
2945 return TRUE;
2947 info = (struct bfd_link_info *) inf;
2948 htab = sh_elf_hash_table (info);
2949 if (htab == NULL)
2950 return FALSE;
2952 eh = (struct elf_sh_link_hash_entry *) h;
2953 if ((h->got.refcount > 0
2954 || h->forced_local)
2955 && eh->gotplt_refcount > 0)
2957 /* The symbol has been forced local, or we have some direct got refs,
2958 so treat all the gotplt refs as got refs. */
2959 h->got.refcount += eh->gotplt_refcount;
2960 if (h->plt.refcount >= eh->gotplt_refcount)
2961 h->plt.refcount -= eh->gotplt_refcount;
2964 if (htab->root.dynamic_sections_created
2965 && h->plt.refcount > 0
2966 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2967 || h->root.type != bfd_link_hash_undefweak))
2969 /* Make sure this symbol is output as a dynamic symbol.
2970 Undefined weak syms won't yet be marked as dynamic. */
2971 if (h->dynindx == -1
2972 && !h->forced_local)
2974 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2975 return FALSE;
2978 if (bfd_link_pic (info)
2979 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2981 asection *s = htab->root.splt;
2982 const struct elf_sh_plt_info *plt_info;
2984 /* If this is the first .plt entry, make room for the special
2985 first entry. */
2986 if (s->size == 0)
2987 s->size += htab->plt_info->plt0_entry_size;
2989 h->plt.offset = s->size;
2991 /* If this symbol is not defined in a regular file, and we are
2992 not generating a shared library, then set the symbol to this
2993 location in the .plt. This is required to make function
2994 pointers compare as equal between the normal executable and
2995 the shared library. Skip this for FDPIC, since the
2996 function's address will be the address of the canonical
2997 function descriptor. */
2998 if (!htab->fdpic_p && !bfd_link_pic (info) && !h->def_regular)
3000 h->root.u.def.section = s;
3001 h->root.u.def.value = h->plt.offset;
3004 /* Make room for this entry. */
3005 plt_info = htab->plt_info;
3006 if (plt_info->short_plt != NULL
3007 && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
3008 plt_info = plt_info->short_plt;
3009 s->size += plt_info->symbol_entry_size;
3011 /* We also need to make an entry in the .got.plt section, which
3012 will be placed in the .got section by the linker script. */
3013 if (!htab->fdpic_p)
3014 htab->root.sgotplt->size += 4;
3015 else
3016 htab->root.sgotplt->size += 8;
3018 /* We also need to make an entry in the .rel.plt section. */
3019 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3021 if (htab->vxworks_p && !bfd_link_pic (info))
3023 /* VxWorks executables have a second set of relocations
3024 for each PLT entry. They go in a separate relocation
3025 section, which is processed by the kernel loader. */
3027 /* There is a relocation for the initial PLT entry:
3028 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
3029 if (h->plt.offset == htab->plt_info->plt0_entry_size)
3030 htab->srelplt2->size += sizeof (Elf32_External_Rela);
3032 /* There are two extra relocations for each subsequent
3033 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
3034 and an R_SH_DIR32 relocation for the PLT entry. */
3035 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
3038 else
3040 h->plt.offset = (bfd_vma) -1;
3041 h->needs_plt = 0;
3044 else
3046 h->plt.offset = (bfd_vma) -1;
3047 h->needs_plt = 0;
3050 if (h->got.refcount > 0)
3052 asection *s;
3053 bfd_boolean dyn;
3054 enum got_type got_type = sh_elf_hash_entry (h)->got_type;
3056 /* Make sure this symbol is output as a dynamic symbol.
3057 Undefined weak syms won't yet be marked as dynamic. */
3058 if (h->dynindx == -1
3059 && !h->forced_local)
3061 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3062 return FALSE;
3065 s = htab->root.sgot;
3066 h->got.offset = s->size;
3067 s->size += 4;
3068 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
3069 if (got_type == GOT_TLS_GD)
3070 s->size += 4;
3071 dyn = htab->root.dynamic_sections_created;
3072 if (!dyn)
3074 /* No dynamic relocations required. */
3075 if (htab->fdpic_p && !bfd_link_pic (info)
3076 && h->root.type != bfd_link_hash_undefweak
3077 && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
3078 htab->srofixup->size += 4;
3080 /* No dynamic relocations required when IE->LE conversion happens. */
3081 else if (got_type == GOT_TLS_IE
3082 && !h->def_dynamic
3083 && !bfd_link_pic (info))
3085 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3086 R_SH_TLS_GD needs one if local symbol and two if global. */
3087 else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
3088 || got_type == GOT_TLS_IE)
3089 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3090 else if (got_type == GOT_TLS_GD)
3091 htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
3092 else if (got_type == GOT_FUNCDESC)
3094 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
3095 htab->srofixup->size += 4;
3096 else
3097 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3099 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3100 || h->root.type != bfd_link_hash_undefweak)
3101 && (bfd_link_pic (info)
3102 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3103 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3104 else if (htab->fdpic_p
3105 && !bfd_link_pic (info)
3106 && got_type == GOT_NORMAL
3107 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3108 || h->root.type != bfd_link_hash_undefweak))
3109 htab->srofixup->size += 4;
3111 else
3112 h->got.offset = (bfd_vma) -1;
3114 #ifdef INCLUDE_SHMEDIA
3115 if (eh->datalabel_got.refcount > 0)
3117 asection *s;
3118 bfd_boolean dyn;
3120 /* Make sure this symbol is output as a dynamic symbol.
3121 Undefined weak syms won't yet be marked as dynamic. */
3122 if (h->dynindx == -1
3123 && !h->forced_local)
3125 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3126 return FALSE;
3129 s = htab->root.sgot;
3130 eh->datalabel_got.offset = s->size;
3131 s->size += 4;
3132 dyn = htab->root.dynamic_sections_created;
3133 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
3134 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3136 else
3137 eh->datalabel_got.offset = (bfd_vma) -1;
3138 #endif
3140 /* Allocate space for any dynamic relocations to function
3141 descriptors, canonical or otherwise. We need to relocate the
3142 reference unless it resolves to zero, which only happens for
3143 undefined weak symbols (either non-default visibility, or when
3144 static linking). Any GOT slot is accounted for elsewhere. */
3145 if (eh->abs_funcdesc_refcount > 0
3146 && (h->root.type != bfd_link_hash_undefweak
3147 || (htab->root.dynamic_sections_created
3148 && ! SYMBOL_CALLS_LOCAL (info, h))))
3150 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
3151 htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
3152 else
3153 htab->root.srelgot->size
3154 += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
3157 /* We must allocate a function descriptor if there are references to
3158 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
3159 the dynamic linker isn't going to allocate it. None of this
3160 applies if we already created one in .got.plt, but if the
3161 canonical function descriptor can be in this object, there
3162 won't be a PLT entry at all. */
3163 if ((eh->funcdesc.refcount > 0
3164 || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
3165 && h->root.type != bfd_link_hash_undefweak
3166 && SYMBOL_FUNCDESC_LOCAL (info, h))
3168 /* Make room for this function descriptor. */
3169 eh->funcdesc.offset = htab->sfuncdesc->size;
3170 htab->sfuncdesc->size += 8;
3172 /* We will need a relocation or two fixups to initialize the
3173 function descriptor, so allocate those too. */
3174 if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
3175 htab->srofixup->size += 8;
3176 else
3177 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3180 if (eh->dyn_relocs == NULL)
3181 return TRUE;
3183 /* In the shared -Bsymbolic case, discard space allocated for
3184 dynamic pc-relative relocs against symbols which turn out to be
3185 defined in regular objects. For the normal shared case, discard
3186 space for pc-relative relocs that have become local due to symbol
3187 visibility changes. */
3189 if (bfd_link_pic (info))
3191 if (SYMBOL_CALLS_LOCAL (info, h))
3193 struct elf_sh_dyn_relocs **pp;
3195 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3197 p->count -= p->pc_count;
3198 p->pc_count = 0;
3199 if (p->count == 0)
3200 *pp = p->next;
3201 else
3202 pp = &p->next;
3206 if (htab->vxworks_p)
3208 struct elf_sh_dyn_relocs **pp;
3210 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3212 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
3213 *pp = p->next;
3214 else
3215 pp = &p->next;
3219 /* Also discard relocs on undefined weak syms with non-default
3220 visibility. */
3221 if (eh->dyn_relocs != NULL
3222 && h->root.type == bfd_link_hash_undefweak)
3224 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3225 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
3226 eh->dyn_relocs = NULL;
3228 /* Make sure undefined weak symbols are output as a dynamic
3229 symbol in PIEs. */
3230 else if (h->dynindx == -1
3231 && !h->forced_local)
3233 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3234 return FALSE;
3238 else
3240 /* For the non-shared case, discard space for relocs against
3241 symbols which turn out to need copy relocs or are not
3242 dynamic. */
3244 if (!h->non_got_ref
3245 && ((h->def_dynamic
3246 && !h->def_regular)
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))
3257 return FALSE;
3260 /* If that succeeded, we know we'll be keeping all the
3261 relocs. */
3262 if (h->dynindx != -1)
3263 goto keep;
3266 eh->dyn_relocs = NULL;
3268 keep: ;
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 && !bfd_link_pic (info))
3279 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3282 return TRUE;
3285 /* Find any dynamic relocs that apply to read-only sections. */
3287 static bfd_boolean
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 info->callbacks->minfo (_("%B: dynamic relocation in read-only section `%A'\n"),
3305 p->sec->owner, p->sec);
3307 /* Not an error, just cut short the traversal. */
3308 return FALSE;
3311 return TRUE;
3314 /* This function is called after all the input files have been read,
3315 and the input sections have been assigned to output sections.
3316 It's a convenient place to determine the PLT style. */
3318 static bfd_boolean
3319 sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
3321 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd,
3322 bfd_link_pic (info));
3324 if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_relocatable (info)
3325 && !bfd_elf_stack_segment_size (output_bfd, info,
3326 "__stacksize", DEFAULT_STACK_SIZE))
3327 return FALSE;
3328 return TRUE;
3331 /* Set the sizes of the dynamic sections. */
3333 static bfd_boolean
3334 sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3335 struct bfd_link_info *info)
3337 struct elf_sh_link_hash_table *htab;
3338 bfd *dynobj;
3339 asection *s;
3340 bfd_boolean relocs;
3341 bfd *ibfd;
3343 htab = sh_elf_hash_table (info);
3344 if (htab == NULL)
3345 return FALSE;
3347 dynobj = htab->root.dynobj;
3348 BFD_ASSERT (dynobj != NULL);
3350 if (htab->root.dynamic_sections_created)
3352 /* Set the contents of the .interp section to the interpreter. */
3353 if (bfd_link_executable (info) && !info->nointerp)
3355 s = bfd_get_linker_section (dynobj, ".interp");
3356 BFD_ASSERT (s != NULL);
3357 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3358 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3362 /* Set up .got offsets for local syms, and space for local dynamic
3363 relocs. */
3364 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3366 bfd_signed_vma *local_got;
3367 bfd_signed_vma *end_local_got;
3368 union gotref *local_funcdesc, *end_local_funcdesc;
3369 char *local_got_type;
3370 bfd_size_type locsymcount;
3371 Elf_Internal_Shdr *symtab_hdr;
3372 asection *srel;
3374 if (! is_sh_elf (ibfd))
3375 continue;
3377 for (s = ibfd->sections; s != NULL; s = s->next)
3379 struct elf_sh_dyn_relocs *p;
3381 for (p = ((struct elf_sh_dyn_relocs *)
3382 elf_section_data (s)->local_dynrel);
3383 p != NULL;
3384 p = p->next)
3386 if (! bfd_is_abs_section (p->sec)
3387 && bfd_is_abs_section (p->sec->output_section))
3389 /* Input section has been discarded, either because
3390 it is a copy of a linkonce section or due to
3391 linker script /DISCARD/, so we'll be discarding
3392 the relocs too. */
3394 else if (htab->vxworks_p
3395 && strcmp (p->sec->output_section->name,
3396 ".tls_vars") == 0)
3398 /* Relocations in vxworks .tls_vars sections are
3399 handled specially by the loader. */
3401 else if (p->count != 0)
3403 srel = elf_section_data (p->sec)->sreloc;
3404 srel->size += p->count * sizeof (Elf32_External_Rela);
3405 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3407 info->flags |= DF_TEXTREL;
3408 info->callbacks->minfo (_("%B: dynamic relocation in read-only section `%A'\n"),
3409 p->sec->owner, p->sec);
3412 /* If we need relocations, we do not need fixups. */
3413 if (htab->fdpic_p && !bfd_link_pic (info))
3414 htab->srofixup->size -= 4 * (p->count - p->pc_count);
3419 symtab_hdr = &elf_symtab_hdr (ibfd);
3420 locsymcount = symtab_hdr->sh_info;
3421 #ifdef INCLUDE_SHMEDIA
3422 /* Count datalabel local GOT. */
3423 locsymcount *= 2;
3424 #endif
3425 s = htab->root.sgot;
3426 srel = htab->root.srelgot;
3428 local_got = elf_local_got_refcounts (ibfd);
3429 if (local_got)
3431 end_local_got = local_got + locsymcount;
3432 local_got_type = sh_elf_local_got_type (ibfd);
3433 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3434 for (; local_got < end_local_got; ++local_got)
3436 if (*local_got > 0)
3438 *local_got = s->size;
3439 s->size += 4;
3440 if (*local_got_type == GOT_TLS_GD)
3441 s->size += 4;
3442 if (bfd_link_pic (info))
3443 srel->size += sizeof (Elf32_External_Rela);
3444 else
3445 htab->srofixup->size += 4;
3447 if (*local_got_type == GOT_FUNCDESC)
3449 if (local_funcdesc == NULL)
3451 bfd_size_type size;
3453 size = locsymcount * sizeof (union gotref);
3454 local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
3455 size);
3456 if (local_funcdesc == NULL)
3457 return FALSE;
3458 sh_elf_local_funcdesc (ibfd) = local_funcdesc;
3459 local_funcdesc += (local_got
3460 - elf_local_got_refcounts (ibfd));
3462 local_funcdesc->refcount++;
3463 ++local_funcdesc;
3466 else
3467 *local_got = (bfd_vma) -1;
3468 ++local_got_type;
3472 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3473 if (local_funcdesc)
3475 end_local_funcdesc = local_funcdesc + locsymcount;
3477 for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
3479 if (local_funcdesc->refcount > 0)
3481 local_funcdesc->offset = htab->sfuncdesc->size;
3482 htab->sfuncdesc->size += 8;
3483 if (!bfd_link_pic (info))
3484 htab->srofixup->size += 8;
3485 else
3486 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3488 else
3489 local_funcdesc->offset = MINUS_ONE;
3495 if (htab->tls_ldm_got.refcount > 0)
3497 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3498 relocs. */
3499 htab->tls_ldm_got.offset = htab->root.sgot->size;
3500 htab->root.sgot->size += 8;
3501 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3503 else
3504 htab->tls_ldm_got.offset = -1;
3506 /* Only the reserved entries should be present. For FDPIC, they go at
3507 the end of .got.plt. */
3508 if (htab->fdpic_p)
3510 BFD_ASSERT (htab->root.sgotplt && htab->root.sgotplt->size == 12);
3511 htab->root.sgotplt->size = 0;
3514 /* Allocate global sym .plt and .got entries, and space for global
3515 sym dynamic relocs. */
3516 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
3518 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3519 end of the FDPIC .got.plt. */
3520 if (htab->fdpic_p)
3522 htab->root.hgot->root.u.def.value = htab->root.sgotplt->size;
3523 htab->root.sgotplt->size += 12;
3526 /* At the very end of the .rofixup section is a pointer to the GOT. */
3527 if (htab->fdpic_p && htab->srofixup != NULL)
3528 htab->srofixup->size += 4;
3530 /* We now have determined the sizes of the various dynamic sections.
3531 Allocate memory for them. */
3532 relocs = FALSE;
3533 for (s = dynobj->sections; s != NULL; s = s->next)
3535 if ((s->flags & SEC_LINKER_CREATED) == 0)
3536 continue;
3538 if (s == htab->root.splt
3539 || s == htab->root.sgot
3540 || s == htab->root.sgotplt
3541 || s == htab->sfuncdesc
3542 || s == htab->srofixup
3543 || s == htab->sdynbss)
3545 /* Strip this section if we don't need it; see the
3546 comment below. */
3548 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3550 if (s->size != 0 && s != htab->root.srelplt && s != htab->srelplt2)
3551 relocs = TRUE;
3553 /* We use the reloc_count field as a counter if we need
3554 to copy relocs into the output file. */
3555 s->reloc_count = 0;
3557 else
3559 /* It's not one of our sections, so don't allocate space. */
3560 continue;
3563 if (s->size == 0)
3565 /* If we don't need this section, strip it from the
3566 output file. This is mostly to handle .rela.bss and
3567 .rela.plt. We must create both sections in
3568 create_dynamic_sections, because they must be created
3569 before the linker maps input sections to output
3570 sections. The linker does that before
3571 adjust_dynamic_symbol is called, and it is that
3572 function which decides whether anything needs to go
3573 into these sections. */
3575 s->flags |= SEC_EXCLUDE;
3576 continue;
3579 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3580 continue;
3582 /* Allocate memory for the section contents. We use bfd_zalloc
3583 here in case unused entries are not reclaimed before the
3584 section's contents are written out. This should not happen,
3585 but this way if it does, we get a R_SH_NONE reloc instead
3586 of garbage. */
3587 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3588 if (s->contents == NULL)
3589 return FALSE;
3592 if (htab->root.dynamic_sections_created)
3594 /* Add some entries to the .dynamic section. We fill in the
3595 values later, in sh_elf_finish_dynamic_sections, but we
3596 must add the entries now so that we get the correct size for
3597 the .dynamic section. The DT_DEBUG entry is filled in by the
3598 dynamic linker and used by the debugger. */
3599 #define add_dynamic_entry(TAG, VAL) \
3600 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3602 if (bfd_link_executable (info))
3604 if (! add_dynamic_entry (DT_DEBUG, 0))
3605 return FALSE;
3608 if (htab->root.splt->size != 0)
3610 if (! add_dynamic_entry (DT_PLTGOT, 0)
3611 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3612 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3613 || ! add_dynamic_entry (DT_JMPREL, 0))
3614 return FALSE;
3616 else if ((elf_elfheader (output_bfd)->e_flags & EF_SH_FDPIC))
3618 if (! add_dynamic_entry (DT_PLTGOT, 0))
3619 return FALSE;
3622 if (relocs)
3624 if (! add_dynamic_entry (DT_RELA, 0)
3625 || ! add_dynamic_entry (DT_RELASZ, 0)
3626 || ! add_dynamic_entry (DT_RELAENT,
3627 sizeof (Elf32_External_Rela)))
3628 return FALSE;
3630 /* If any dynamic relocs apply to a read-only section,
3631 then we need a DT_TEXTREL entry. */
3632 if ((info->flags & DF_TEXTREL) == 0)
3633 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
3635 if ((info->flags & DF_TEXTREL) != 0)
3637 if (! add_dynamic_entry (DT_TEXTREL, 0))
3638 return FALSE;
3641 if (htab->vxworks_p
3642 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3643 return FALSE;
3645 #undef add_dynamic_entry
3647 return TRUE;
3650 /* Add a dynamic relocation to the SRELOC section. */
3652 inline static bfd_vma
3653 sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
3654 int reloc_type, long dynindx, bfd_vma addend)
3656 Elf_Internal_Rela outrel;
3657 bfd_vma reloc_offset;
3659 outrel.r_offset = offset;
3660 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
3661 outrel.r_addend = addend;
3663 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
3664 BFD_ASSERT (reloc_offset < sreloc->size);
3665 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3666 sreloc->contents + reloc_offset);
3667 sreloc->reloc_count++;
3669 return reloc_offset;
3672 /* Add an FDPIC read-only fixup. */
3674 inline static void
3675 sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3677 bfd_vma fixup_offset;
3679 fixup_offset = srofixup->reloc_count++ * 4;
3680 BFD_ASSERT (fixup_offset < srofixup->size);
3681 bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3684 /* Return the offset of the generated .got section from the
3685 _GLOBAL_OFFSET_TABLE_ symbol. */
3687 static bfd_signed_vma
3688 sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
3690 return (htab->root.sgot->output_offset - htab->root.sgotplt->output_offset
3691 - htab->root.hgot->root.u.def.value);
3694 /* Find the segment number in which OSEC, and output section, is
3695 located. */
3697 static unsigned
3698 sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
3700 Elf_Internal_Phdr *p = NULL;
3702 if (output_bfd->xvec->flavour == bfd_target_elf_flavour
3703 /* PR ld/17110: Do not look for output segments in an input bfd. */
3704 && output_bfd->direction != read_direction)
3705 p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
3707 /* FIXME: Nothing ever says what this index is relative to. The kernel
3708 supplies data in terms of the number of load segments but this is
3709 a phdr index and the first phdr may not be a load segment. */
3710 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
3713 static bfd_boolean
3714 sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
3716 unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
3718 return (seg != (unsigned) -1
3719 && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
3722 /* Generate the initial contents of a local function descriptor, along
3723 with any relocations or fixups required. */
3724 static bfd_boolean
3725 sh_elf_initialize_funcdesc (bfd *output_bfd,
3726 struct bfd_link_info *info,
3727 struct elf_link_hash_entry *h,
3728 bfd_vma offset,
3729 asection *section,
3730 bfd_vma value)
3732 struct elf_sh_link_hash_table *htab;
3733 int dynindx;
3734 bfd_vma addr, seg;
3736 htab = sh_elf_hash_table (info);
3738 /* FIXME: The ABI says that the offset to the function goes in the
3739 descriptor, along with the segment index. We're RELA, so it could
3740 go in the reloc instead... */
3742 if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
3744 section = h->root.u.def.section;
3745 value = h->root.u.def.value;
3748 if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
3750 dynindx = elf_section_data (section->output_section)->dynindx;
3751 addr = value + section->output_offset;
3752 seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
3754 else
3756 BFD_ASSERT (h->dynindx != -1);
3757 dynindx = h->dynindx;
3758 addr = seg = 0;
3761 if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
3763 if (h == NULL || h->root.type != bfd_link_hash_undefweak)
3765 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3766 offset
3767 + htab->sfuncdesc->output_section->vma
3768 + htab->sfuncdesc->output_offset);
3769 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3770 offset + 4
3771 + htab->sfuncdesc->output_section->vma
3772 + htab->sfuncdesc->output_offset);
3775 /* There are no dynamic relocations so fill in the final
3776 address and gp value (barring fixups). */
3777 addr += section->output_section->vma;
3778 seg = htab->root.hgot->root.u.def.value
3779 + htab->root.hgot->root.u.def.section->output_section->vma
3780 + htab->root.hgot->root.u.def.section->output_offset;
3782 else
3783 sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
3784 offset
3785 + htab->sfuncdesc->output_section->vma
3786 + htab->sfuncdesc->output_offset,
3787 R_SH_FUNCDESC_VALUE, dynindx, 0);
3789 bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
3790 bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
3792 return TRUE;
3795 /* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3796 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3797 otherwise. */
3799 static bfd_reloc_status_type
3800 install_movi20_field (bfd *output_bfd, unsigned long relocation,
3801 bfd *input_bfd, asection *input_section,
3802 bfd_byte *contents, bfd_vma offset)
3804 unsigned long cur_val;
3805 bfd_byte *addr;
3806 bfd_reloc_status_type r;
3808 if (offset > bfd_get_section_limit (input_bfd, input_section))
3809 return bfd_reloc_outofrange;
3811 r = bfd_check_overflow (complain_overflow_signed, 20, 0,
3812 bfd_arch_bits_per_address (input_bfd), relocation);
3813 if (r != bfd_reloc_ok)
3814 return r;
3816 addr = contents + offset;
3817 cur_val = bfd_get_16 (output_bfd, addr);
3818 bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
3819 bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
3821 return bfd_reloc_ok;
3824 /* Relocate an SH ELF section. */
3826 static bfd_boolean
3827 sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3828 bfd *input_bfd, asection *input_section,
3829 bfd_byte *contents, Elf_Internal_Rela *relocs,
3830 Elf_Internal_Sym *local_syms,
3831 asection **local_sections)
3833 struct elf_sh_link_hash_table *htab;
3834 Elf_Internal_Shdr *symtab_hdr;
3835 struct elf_link_hash_entry **sym_hashes;
3836 Elf_Internal_Rela *rel, *relend;
3837 bfd_vma *local_got_offsets;
3838 asection *sgot = NULL;
3839 asection *sgotplt = NULL;
3840 asection *splt = NULL;
3841 asection *sreloc = NULL;
3842 asection *srelgot = NULL;
3843 bfd_boolean is_vxworks_tls;
3844 unsigned isec_segment, got_segment, plt_segment, check_segment[2];
3845 bfd_boolean fdpic_p = FALSE;
3847 BFD_ASSERT (is_sh_elf (input_bfd));
3849 htab = sh_elf_hash_table (info);
3850 if (htab != NULL)
3852 sgot = htab->root.sgot;
3853 sgotplt = htab->root.sgotplt;
3854 srelgot = htab->root.srelgot;
3855 splt = htab->root.splt;
3856 fdpic_p = htab->fdpic_p;
3858 symtab_hdr = &elf_symtab_hdr (input_bfd);
3859 sym_hashes = elf_sym_hashes (input_bfd);
3860 local_got_offsets = elf_local_got_offsets (input_bfd);
3862 isec_segment = sh_elf_osec_to_segment (output_bfd,
3863 input_section->output_section);
3864 if (fdpic_p && sgot)
3865 got_segment = sh_elf_osec_to_segment (output_bfd,
3866 sgot->output_section);
3867 else
3868 got_segment = -1;
3869 if (fdpic_p && splt)
3870 plt_segment = sh_elf_osec_to_segment (output_bfd,
3871 splt->output_section);
3872 else
3873 plt_segment = -1;
3875 /* We have to handle relocations in vxworks .tls_vars sections
3876 specially, because the dynamic loader is 'weird'. */
3877 is_vxworks_tls = (htab && htab->vxworks_p && bfd_link_pic (info)
3878 && !strcmp (input_section->output_section->name,
3879 ".tls_vars"));
3881 rel = relocs;
3882 relend = relocs + input_section->reloc_count;
3883 for (; rel < relend; rel++)
3885 int r_type;
3886 reloc_howto_type *howto;
3887 unsigned long r_symndx;
3888 Elf_Internal_Sym *sym;
3889 asection *sec;
3890 struct elf_link_hash_entry *h;
3891 bfd_vma relocation;
3892 bfd_vma addend = (bfd_vma) 0;
3893 bfd_reloc_status_type r;
3894 int seen_stt_datalabel = 0;
3895 bfd_vma off;
3896 enum got_type got_type;
3897 const char *symname = NULL;
3898 bfd_boolean resolved_to_zero;
3900 r_symndx = ELF32_R_SYM (rel->r_info);
3902 r_type = ELF32_R_TYPE (rel->r_info);
3904 /* Many of the relocs are only used for relaxing, and are
3905 handled entirely by the relaxation code. */
3906 if (r_type >= (int) R_SH_GNU_VTINHERIT
3907 && r_type <= (int) R_SH_LABEL)
3908 continue;
3909 if (r_type == (int) R_SH_NONE)
3910 continue;
3912 if (r_type < 0
3913 || r_type >= R_SH_max
3914 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3915 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3916 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3917 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
3918 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3919 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3920 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3921 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3922 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3923 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
3924 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
3925 && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
3927 bfd_set_error (bfd_error_bad_value);
3928 return FALSE;
3931 howto = get_howto_table (output_bfd) + r_type;
3933 /* For relocs that aren't partial_inplace, we get the addend from
3934 the relocation. */
3935 if (! howto->partial_inplace)
3936 addend = rel->r_addend;
3938 resolved_to_zero = FALSE;
3939 h = NULL;
3940 sym = NULL;
3941 sec = NULL;
3942 check_segment[0] = -1;
3943 check_segment[1] = -1;
3944 if (r_symndx < symtab_hdr->sh_info)
3946 sym = local_syms + r_symndx;
3947 sec = local_sections[r_symndx];
3949 symname = bfd_elf_string_from_elf_section
3950 (input_bfd, symtab_hdr->sh_link, sym->st_name);
3951 if (symname == NULL || *symname == '\0')
3952 symname = bfd_section_name (input_bfd, sec);
3954 relocation = (sec->output_section->vma
3955 + sec->output_offset
3956 + sym->st_value);
3957 /* A local symbol never has STO_SH5_ISA32, so we don't need
3958 datalabel processing here. Make sure this does not change
3959 without notice. */
3960 if ((sym->st_other & STO_SH5_ISA32) != 0)
3961 (*info->callbacks->reloc_dangerous)
3962 (info,
3963 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3964 input_bfd, input_section, rel->r_offset);
3966 if (sec != NULL && discarded_section (sec))
3967 /* Handled below. */
3969 else if (bfd_link_relocatable (info))
3971 /* This is a relocatable link. We don't have to change
3972 anything, unless the reloc is against a section symbol,
3973 in which case we have to adjust according to where the
3974 section symbol winds up in the output section. */
3975 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3977 if (! howto->partial_inplace)
3979 /* For relocations with the addend in the
3980 relocation, we need just to update the addend.
3981 All real relocs are of type partial_inplace; this
3982 code is mostly for completeness. */
3983 rel->r_addend += sec->output_offset;
3985 continue;
3988 /* Relocs of type partial_inplace need to pick up the
3989 contents in the contents and add the offset resulting
3990 from the changed location of the section symbol.
3991 Using _bfd_final_link_relocate (e.g. goto
3992 final_link_relocate) here would be wrong, because
3993 relocations marked pc_relative would get the current
3994 location subtracted, and we must only do that at the
3995 final link. */
3996 r = _bfd_relocate_contents (howto, input_bfd,
3997 sec->output_offset
3998 + sym->st_value,
3999 contents + rel->r_offset);
4000 goto relocation_done;
4003 continue;
4005 else if (! howto->partial_inplace)
4007 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4008 addend = rel->r_addend;
4010 else if ((sec->flags & SEC_MERGE)
4011 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4013 asection *msec;
4015 if (howto->rightshift || howto->src_mask != 0xffffffff)
4017 _bfd_error_handler
4018 /* xgettext:c-format */
4019 (_("%B(%A+%#Lx): %s relocation against SEC_MERGE section"),
4020 input_bfd, input_section,
4021 rel->r_offset, howto->name);
4022 return FALSE;
4025 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4026 msec = sec;
4027 addend =
4028 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
4029 - relocation;
4030 addend += msec->output_section->vma + msec->output_offset;
4031 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
4032 addend = 0;
4035 else
4037 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
4039 relocation = 0;
4040 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4041 symname = h->root.root.string;
4042 while (h->root.type == bfd_link_hash_indirect
4043 || h->root.type == bfd_link_hash_warning)
4045 #ifdef INCLUDE_SHMEDIA
4046 /* If the reference passes a symbol marked with
4047 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
4048 doesn't count. */
4049 seen_stt_datalabel |= h->type == STT_DATALABEL;
4050 #endif
4051 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4053 if (h->root.type == bfd_link_hash_defined
4054 || h->root.type == bfd_link_hash_defweak)
4056 bfd_boolean dyn;
4058 dyn = htab ? htab->root.dynamic_sections_created : FALSE;
4059 sec = h->root.u.def.section;
4060 /* In these cases, we don't need the relocation value.
4061 We check specially because in some obscure cases
4062 sec->output_section will be NULL. */
4063 if (r_type == R_SH_GOTPC
4064 || r_type == R_SH_GOTPC_LOW16
4065 || r_type == R_SH_GOTPC_MEDLOW16
4066 || r_type == R_SH_GOTPC_MEDHI16
4067 || r_type == R_SH_GOTPC_HI16
4068 || ((r_type == R_SH_PLT32
4069 || r_type == R_SH_PLT_LOW16
4070 || r_type == R_SH_PLT_MEDLOW16
4071 || r_type == R_SH_PLT_MEDHI16
4072 || r_type == R_SH_PLT_HI16)
4073 && h->plt.offset != (bfd_vma) -1)
4074 || ((r_type == R_SH_GOT32
4075 || r_type == R_SH_GOT20
4076 || r_type == R_SH_GOTFUNCDESC
4077 || r_type == R_SH_GOTFUNCDESC20
4078 || r_type == R_SH_GOTOFFFUNCDESC
4079 || r_type == R_SH_GOTOFFFUNCDESC20
4080 || r_type == R_SH_FUNCDESC
4081 || r_type == R_SH_GOT_LOW16
4082 || r_type == R_SH_GOT_MEDLOW16
4083 || r_type == R_SH_GOT_MEDHI16
4084 || r_type == R_SH_GOT_HI16)
4085 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4086 bfd_link_pic (info),
4088 && (! bfd_link_pic (info)
4089 || (! info->symbolic && h->dynindx != -1)
4090 || !h->def_regular))
4091 /* The cases above are those in which relocation is
4092 overwritten in the switch block below. The cases
4093 below are those in which we must defer relocation
4094 to run-time, because we can't resolve absolute
4095 addresses when creating a shared library. */
4096 || (bfd_link_pic (info)
4097 && ((! info->symbolic && h->dynindx != -1)
4098 || !h->def_regular)
4099 && ((r_type == R_SH_DIR32
4100 && !h->forced_local)
4101 || (r_type == R_SH_REL32
4102 && !SYMBOL_CALLS_LOCAL (info, h)))
4103 && ((input_section->flags & SEC_ALLOC) != 0
4104 /* DWARF will emit R_SH_DIR32 relocations in its
4105 sections against symbols defined externally
4106 in shared libraries. We can't do anything
4107 with them here. */
4108 || ((input_section->flags & SEC_DEBUGGING) != 0
4109 && h->def_dynamic)))
4110 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4111 sections because such sections are not SEC_ALLOC and
4112 thus ld.so will not process them. */
4113 || (sec->output_section == NULL
4114 && ((input_section->flags & SEC_DEBUGGING) != 0
4115 && h->def_dynamic))
4116 || (sec->output_section == NULL
4117 && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
4118 || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
4120 else if (sec->output_section != NULL)
4121 relocation = ((h->root.u.def.value
4122 + sec->output_section->vma
4123 + sec->output_offset)
4124 /* A STO_SH5_ISA32 causes a "bitor 1" to the
4125 symbol value, unless we've seen
4126 STT_DATALABEL on the way to it. */
4127 | ((h->other & STO_SH5_ISA32) != 0
4128 && ! seen_stt_datalabel));
4129 else if (!bfd_link_relocatable (info)
4130 && (_bfd_elf_section_offset (output_bfd, info,
4131 input_section,
4132 rel->r_offset)
4133 != (bfd_vma) -1))
4135 _bfd_error_handler
4136 /* xgettext:c-format */
4137 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
4138 input_bfd,
4139 input_section,
4140 rel->r_offset,
4141 howto->name,
4142 h->root.root.string);
4143 return FALSE;
4146 else if (h->root.type == bfd_link_hash_undefweak)
4147 resolved_to_zero = UNDEFWEAK_NO_DYNAMIC_RELOC (info, h);
4148 else if (info->unresolved_syms_in_objects == RM_IGNORE
4149 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4151 else if (!bfd_link_relocatable (info))
4152 (*info->callbacks->undefined_symbol)
4153 (info, h->root.root.string, input_bfd,
4154 input_section, rel->r_offset,
4155 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
4156 || ELF_ST_VISIBILITY (h->other)));
4159 if (sec != NULL && discarded_section (sec))
4160 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4161 rel, 1, relend, howto, 0, contents);
4163 if (bfd_link_relocatable (info))
4164 continue;
4166 /* Check for inter-segment relocations in FDPIC files. Most
4167 relocations connect the relocation site to the location of
4168 the target symbol, but there are some exceptions below. */
4169 check_segment[0] = isec_segment;
4170 if (sec != NULL)
4171 check_segment[1] = sh_elf_osec_to_segment (output_bfd,
4172 sec->output_section);
4173 else
4174 check_segment[1] = -1;
4176 switch ((int) r_type)
4178 final_link_relocate:
4179 /* COFF relocs don't use the addend. The addend is used for
4180 R_SH_DIR32 to be compatible with other compilers. */
4181 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4182 contents, rel->r_offset,
4183 relocation, addend);
4184 break;
4186 case R_SH_IND12W:
4187 goto final_link_relocate;
4189 case R_SH_DIR8WPN:
4190 case R_SH_DIR8WPZ:
4191 case R_SH_DIR8WPL:
4192 /* If the reloc is against the start of this section, then
4193 the assembler has already taken care of it and the reloc
4194 is here only to assist in relaxing. If the reloc is not
4195 against the start of this section, then it's against an
4196 external symbol and we must deal with it ourselves. */
4197 if (input_section->output_section->vma + input_section->output_offset
4198 != relocation)
4200 int disp = (relocation
4201 - input_section->output_section->vma
4202 - input_section->output_offset
4203 - rel->r_offset);
4204 int mask = 0;
4205 switch (r_type)
4207 case R_SH_DIR8WPN:
4208 case R_SH_DIR8WPZ: mask = 1; break;
4209 case R_SH_DIR8WPL: mask = 3; break;
4210 default: mask = 0; break;
4212 if (disp & mask)
4214 _bfd_error_handler
4215 /* xgettext:c-format */
4216 (_("%B: %#Lx: fatal: unaligned branch target for relax-support relocation"),
4217 input_section->owner,
4218 rel->r_offset);
4219 bfd_set_error (bfd_error_bad_value);
4220 return FALSE;
4222 relocation -= 4;
4223 goto final_link_relocate;
4225 r = bfd_reloc_ok;
4226 break;
4228 default:
4229 #ifdef INCLUDE_SHMEDIA
4230 if (shmedia_prepare_reloc (info, input_bfd, input_section,
4231 contents, rel, &relocation))
4232 goto final_link_relocate;
4233 #endif
4234 bfd_set_error (bfd_error_bad_value);
4235 return FALSE;
4237 case R_SH_DIR16:
4238 case R_SH_DIR8:
4239 case R_SH_DIR8U:
4240 case R_SH_DIR8S:
4241 case R_SH_DIR4U:
4242 goto final_link_relocate;
4244 case R_SH_DIR8UL:
4245 case R_SH_DIR4UL:
4246 if (relocation & 3)
4248 _bfd_error_handler
4249 /* xgettext:c-format */
4250 (_("%B: %#Lx: fatal: unaligned %s relocation %#Lx"),
4251 input_section->owner,
4252 rel->r_offset, howto->name,
4253 relocation);
4254 bfd_set_error (bfd_error_bad_value);
4255 return FALSE;
4257 goto final_link_relocate;
4259 case R_SH_DIR8UW:
4260 case R_SH_DIR8SW:
4261 case R_SH_DIR4UW:
4262 if (relocation & 1)
4264 _bfd_error_handler
4265 /* xgettext:c-format */
4266 (_("%B: %#Lx: fatal: unaligned %s relocation %#Lx"),
4267 input_section->owner,
4268 rel->r_offset, howto->name,
4269 relocation);
4270 bfd_set_error (bfd_error_bad_value);
4271 return FALSE;
4273 goto final_link_relocate;
4275 case R_SH_PSHA:
4276 if ((signed int)relocation < -32
4277 || (signed int)relocation > 32)
4279 _bfd_error_handler
4280 /* xgettext:c-format */
4281 (_("%B: %#Lx: fatal: R_SH_PSHA relocation %Ld not in range -32..32"),
4282 input_section->owner,
4283 rel->r_offset,
4284 relocation);
4285 bfd_set_error (bfd_error_bad_value);
4286 return FALSE;
4288 goto final_link_relocate;
4290 case R_SH_PSHL:
4291 if ((signed int)relocation < -16
4292 || (signed int)relocation > 16)
4294 _bfd_error_handler
4295 /* xgettext:c-format */
4296 (_("%B: %#Lx: fatal: R_SH_PSHL relocation %Ld not in range -32..32"),
4297 input_section->owner,
4298 rel->r_offset,
4299 relocation);
4300 bfd_set_error (bfd_error_bad_value);
4301 return FALSE;
4303 goto final_link_relocate;
4305 case R_SH_DIR32:
4306 case R_SH_REL32:
4307 #ifdef INCLUDE_SHMEDIA
4308 case R_SH_IMM_LOW16_PCREL:
4309 case R_SH_IMM_MEDLOW16_PCREL:
4310 case R_SH_IMM_MEDHI16_PCREL:
4311 case R_SH_IMM_HI16_PCREL:
4312 #endif
4313 if (bfd_link_pic (info)
4314 && (h == NULL
4315 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4316 && !resolved_to_zero)
4317 || h->root.type != bfd_link_hash_undefweak)
4318 && r_symndx != STN_UNDEF
4319 && (input_section->flags & SEC_ALLOC) != 0
4320 && !is_vxworks_tls
4321 && (r_type == R_SH_DIR32
4322 || !SYMBOL_CALLS_LOCAL (info, h)))
4324 Elf_Internal_Rela outrel;
4325 bfd_byte *loc;
4326 bfd_boolean skip, relocate;
4328 /* When generating a shared object, these relocations
4329 are copied into the output file to be resolved at run
4330 time. */
4332 if (sreloc == NULL)
4334 sreloc = _bfd_elf_get_dynamic_reloc_section
4335 (input_bfd, input_section, /*rela?*/ TRUE);
4336 if (sreloc == NULL)
4337 return FALSE;
4340 skip = FALSE;
4341 relocate = FALSE;
4343 outrel.r_offset =
4344 _bfd_elf_section_offset (output_bfd, info, input_section,
4345 rel->r_offset);
4346 if (outrel.r_offset == (bfd_vma) -1)
4347 skip = TRUE;
4348 else if (outrel.r_offset == (bfd_vma) -2)
4349 skip = TRUE, relocate = TRUE;
4350 outrel.r_offset += (input_section->output_section->vma
4351 + input_section->output_offset);
4353 if (skip)
4354 memset (&outrel, 0, sizeof outrel);
4355 else if (r_type == R_SH_REL32)
4357 BFD_ASSERT (h != NULL && h->dynindx != -1);
4358 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
4359 outrel.r_addend
4360 = (howto->partial_inplace
4361 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4362 : addend);
4364 #ifdef INCLUDE_SHMEDIA
4365 else if (r_type == R_SH_IMM_LOW16_PCREL
4366 || r_type == R_SH_IMM_MEDLOW16_PCREL
4367 || r_type == R_SH_IMM_MEDHI16_PCREL
4368 || r_type == R_SH_IMM_HI16_PCREL)
4370 BFD_ASSERT (h != NULL && h->dynindx != -1);
4371 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4372 outrel.r_addend = addend;
4374 #endif
4375 else if (fdpic_p
4376 && (h == NULL
4377 || ((info->symbolic || h->dynindx == -1)
4378 && h->def_regular)))
4380 int dynindx;
4382 BFD_ASSERT (sec != NULL);
4383 BFD_ASSERT (sec->output_section != NULL);
4384 dynindx = elf_section_data (sec->output_section)->dynindx;
4385 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4386 outrel.r_addend = relocation;
4387 outrel.r_addend
4388 += (howto->partial_inplace
4389 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4390 : addend);
4391 outrel.r_addend -= sec->output_section->vma;
4393 else
4395 /* h->dynindx may be -1 if this symbol was marked to
4396 become local. */
4397 if (h == NULL
4398 || ((info->symbolic || h->dynindx == -1)
4399 && h->def_regular))
4401 relocate = howto->partial_inplace;
4402 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4404 else
4406 BFD_ASSERT (h->dynindx != -1);
4407 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
4409 outrel.r_addend = relocation;
4410 outrel.r_addend
4411 += (howto->partial_inplace
4412 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
4413 : addend);
4416 loc = sreloc->contents;
4417 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4418 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4420 check_segment[0] = check_segment[1] = -1;
4422 /* If this reloc is against an external symbol, we do
4423 not want to fiddle with the addend. Otherwise, we
4424 need to include the symbol value so that it becomes
4425 an addend for the dynamic reloc. */
4426 if (! relocate)
4427 continue;
4429 else if (fdpic_p && !bfd_link_pic (info)
4430 && r_type == R_SH_DIR32
4431 && (input_section->flags & SEC_ALLOC) != 0)
4433 bfd_vma offset;
4435 BFD_ASSERT (htab);
4437 if (sh_elf_osec_readonly_p (output_bfd,
4438 input_section->output_section))
4440 _bfd_error_handler
4441 /* xgettext:c-format */
4442 (_("%B(%A+%#Lx): cannot emit fixup to `%s' in read-only section"),
4443 input_bfd,
4444 input_section,
4445 rel->r_offset,
4446 symname);
4447 return FALSE;
4450 offset = _bfd_elf_section_offset (output_bfd, info,
4451 input_section, rel->r_offset);
4452 if (offset != (bfd_vma)-1)
4453 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4454 input_section->output_section->vma
4455 + input_section->output_offset
4456 + rel->r_offset);
4458 check_segment[0] = check_segment[1] = -1;
4460 /* We don't want warnings for non-NULL tests on undefined weak
4461 symbols. */
4462 else if (r_type == R_SH_REL32
4463 && h
4464 && h->root.type == bfd_link_hash_undefweak)
4465 check_segment[0] = check_segment[1] = -1;
4466 goto final_link_relocate;
4468 case R_SH_GOTPLT32:
4469 #ifdef INCLUDE_SHMEDIA
4470 case R_SH_GOTPLT_LOW16:
4471 case R_SH_GOTPLT_MEDLOW16:
4472 case R_SH_GOTPLT_MEDHI16:
4473 case R_SH_GOTPLT_HI16:
4474 case R_SH_GOTPLT10BY4:
4475 case R_SH_GOTPLT10BY8:
4476 #endif
4477 /* Relocation is to the entry for this symbol in the
4478 procedure linkage table. */
4480 if (h == NULL
4481 || h->forced_local
4482 || ! bfd_link_pic (info)
4483 || info->symbolic
4484 || h->dynindx == -1
4485 || h->plt.offset == (bfd_vma) -1
4486 || h->got.offset != (bfd_vma) -1)
4487 goto force_got;
4489 /* Relocation is to the entry for this symbol in the global
4490 offset table extension for the procedure linkage table. */
4492 BFD_ASSERT (htab);
4493 BFD_ASSERT (sgotplt != NULL);
4494 relocation = (sgotplt->output_offset
4495 + (get_plt_index (htab->plt_info, h->plt.offset)
4496 + 3) * 4);
4498 #ifdef GOT_BIAS
4499 relocation -= GOT_BIAS;
4500 #endif
4502 goto final_link_relocate;
4504 force_got:
4505 case R_SH_GOT32:
4506 case R_SH_GOT20:
4507 #ifdef INCLUDE_SHMEDIA
4508 case R_SH_GOT_LOW16:
4509 case R_SH_GOT_MEDLOW16:
4510 case R_SH_GOT_MEDHI16:
4511 case R_SH_GOT_HI16:
4512 case R_SH_GOT10BY4:
4513 case R_SH_GOT10BY8:
4514 #endif
4515 /* Relocation is to the entry for this symbol in the global
4516 offset table. */
4518 BFD_ASSERT (htab);
4519 BFD_ASSERT (sgot != NULL);
4520 check_segment[0] = check_segment[1] = -1;
4522 if (h != NULL)
4524 bfd_boolean dyn;
4526 off = h->got.offset;
4527 #ifdef INCLUDE_SHMEDIA
4528 if (seen_stt_datalabel)
4530 struct elf_sh_link_hash_entry *hsh;
4532 hsh = (struct elf_sh_link_hash_entry *)h;
4533 off = hsh->datalabel_got.offset;
4535 #endif
4536 BFD_ASSERT (off != (bfd_vma) -1);
4538 dyn = htab->root.dynamic_sections_created;
4539 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4540 bfd_link_pic (info),
4542 || (bfd_link_pic (info)
4543 && SYMBOL_REFERENCES_LOCAL (info, h))
4544 || ((ELF_ST_VISIBILITY (h->other)
4545 || resolved_to_zero)
4546 && h->root.type == bfd_link_hash_undefweak))
4548 /* This is actually a static link, or it is a
4549 -Bsymbolic link and the symbol is defined
4550 locally, or the symbol was forced to be local
4551 because of a version file. We must initialize
4552 this entry in the global offset table. Since the
4553 offset must always be a multiple of 4, we use the
4554 least significant bit to record whether we have
4555 initialized it already.
4557 When doing a dynamic link, we create a .rela.got
4558 relocation entry to initialize the value. This
4559 is done in the finish_dynamic_symbol routine. */
4560 if ((off & 1) != 0)
4561 off &= ~1;
4562 else
4564 bfd_put_32 (output_bfd, relocation,
4565 sgot->contents + off);
4566 #ifdef INCLUDE_SHMEDIA
4567 if (seen_stt_datalabel)
4569 struct elf_sh_link_hash_entry *hsh;
4571 hsh = (struct elf_sh_link_hash_entry *)h;
4572 hsh->datalabel_got.offset |= 1;
4574 else
4575 #endif
4576 h->got.offset |= 1;
4578 /* If we initialize the GOT entry here with a valid
4579 symbol address, also add a fixup. */
4580 if (fdpic_p && !bfd_link_pic (info)
4581 && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
4582 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4583 || h->root.type != bfd_link_hash_undefweak))
4584 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4585 sgot->output_section->vma
4586 + sgot->output_offset
4587 + off);
4591 relocation = sh_elf_got_offset (htab) + off;
4593 else
4595 #ifdef INCLUDE_SHMEDIA
4596 if (rel->r_addend)
4598 BFD_ASSERT (local_got_offsets != NULL
4599 && (local_got_offsets[symtab_hdr->sh_info
4600 + r_symndx]
4601 != (bfd_vma) -1));
4603 off = local_got_offsets[symtab_hdr->sh_info
4604 + r_symndx];
4606 else
4608 #endif
4609 BFD_ASSERT (local_got_offsets != NULL
4610 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4612 off = local_got_offsets[r_symndx];
4613 #ifdef INCLUDE_SHMEDIA
4615 #endif
4617 /* The offset must always be a multiple of 4. We use
4618 the least significant bit to record whether we have
4619 already generated the necessary reloc. */
4620 if ((off & 1) != 0)
4621 off &= ~1;
4622 else
4624 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4626 if (bfd_link_pic (info))
4628 Elf_Internal_Rela outrel;
4629 bfd_byte *loc;
4631 outrel.r_offset = (sgot->output_section->vma
4632 + sgot->output_offset
4633 + off);
4634 if (fdpic_p)
4636 int dynindx
4637 = elf_section_data (sec->output_section)->dynindx;
4638 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4639 outrel.r_addend = relocation;
4640 outrel.r_addend -= sec->output_section->vma;
4642 else
4644 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4645 outrel.r_addend = relocation;
4647 loc = srelgot->contents;
4648 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4649 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4651 else if (fdpic_p
4652 && (sh_elf_local_got_type (input_bfd) [r_symndx]
4653 == GOT_NORMAL))
4654 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4655 sgot->output_section->vma
4656 + sgot->output_offset
4657 + off);
4659 #ifdef INCLUDE_SHMEDIA
4660 if (rel->r_addend)
4661 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
4662 else
4663 #endif
4664 local_got_offsets[r_symndx] |= 1;
4667 relocation = sh_elf_got_offset (htab) + off;
4670 #ifdef GOT_BIAS
4671 relocation -= GOT_BIAS;
4672 #endif
4674 if (r_type == R_SH_GOT20)
4676 r = install_movi20_field (output_bfd, relocation + addend,
4677 input_bfd, input_section, contents,
4678 rel->r_offset);
4679 break;
4681 else
4682 goto final_link_relocate;
4684 case R_SH_GOTOFF:
4685 case R_SH_GOTOFF20:
4686 #ifdef INCLUDE_SHMEDIA
4687 case R_SH_GOTOFF_LOW16:
4688 case R_SH_GOTOFF_MEDLOW16:
4689 case R_SH_GOTOFF_MEDHI16:
4690 case R_SH_GOTOFF_HI16:
4691 #endif
4692 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4693 we place at the start of the .got.plt section. This is the same
4694 as the start of the output .got section, unless there are function
4695 descriptors in front of it. */
4696 BFD_ASSERT (htab);
4697 BFD_ASSERT (sgotplt != NULL);
4698 check_segment[0] = got_segment;
4699 relocation -= sgotplt->output_section->vma + sgotplt->output_offset
4700 + htab->root.hgot->root.u.def.value;
4702 #ifdef GOT_BIAS
4703 relocation -= GOT_BIAS;
4704 #endif
4706 addend = rel->r_addend;
4708 if (r_type == R_SH_GOTOFF20)
4710 r = install_movi20_field (output_bfd, relocation + addend,
4711 input_bfd, input_section, contents,
4712 rel->r_offset);
4713 break;
4715 else
4716 goto final_link_relocate;
4718 case R_SH_GOTPC:
4719 #ifdef INCLUDE_SHMEDIA
4720 case R_SH_GOTPC_LOW16:
4721 case R_SH_GOTPC_MEDLOW16:
4722 case R_SH_GOTPC_MEDHI16:
4723 case R_SH_GOTPC_HI16:
4724 #endif
4725 /* Use global offset table as symbol value. */
4727 BFD_ASSERT (sgotplt != NULL);
4728 relocation = sgotplt->output_section->vma + sgotplt->output_offset;
4730 #ifdef GOT_BIAS
4731 relocation += GOT_BIAS;
4732 #endif
4734 addend = rel->r_addend;
4736 goto final_link_relocate;
4738 case R_SH_PLT32:
4739 #ifdef INCLUDE_SHMEDIA
4740 case R_SH_PLT_LOW16:
4741 case R_SH_PLT_MEDLOW16:
4742 case R_SH_PLT_MEDHI16:
4743 case R_SH_PLT_HI16:
4744 #endif
4745 /* Relocation is to the entry for this symbol in the
4746 procedure linkage table. */
4748 /* Resolve a PLT reloc against a local symbol directly,
4749 without using the procedure linkage table. */
4750 if (h == NULL)
4751 goto final_link_relocate;
4753 /* We don't want to warn on calls to undefined weak symbols,
4754 as calls to them must be protected by non-NULL tests
4755 anyway, and unprotected calls would invoke undefined
4756 behavior. */
4757 if (h->root.type == bfd_link_hash_undefweak)
4758 check_segment[0] = check_segment[1] = -1;
4760 if (h->forced_local)
4761 goto final_link_relocate;
4763 if (h->plt.offset == (bfd_vma) -1)
4765 /* We didn't make a PLT entry for this symbol. This
4766 happens when statically linking PIC code, or when
4767 using -Bsymbolic. */
4768 goto final_link_relocate;
4771 BFD_ASSERT (splt != NULL);
4772 check_segment[1] = plt_segment;
4773 relocation = (splt->output_section->vma
4774 + splt->output_offset
4775 + h->plt.offset);
4777 #ifdef INCLUDE_SHMEDIA
4778 relocation++;
4779 #endif
4781 addend = rel->r_addend;
4783 goto final_link_relocate;
4785 /* Relocation is to the canonical function descriptor for this
4786 symbol, possibly via the GOT. Initialize the GOT
4787 entry and function descriptor if necessary. */
4788 case R_SH_GOTFUNCDESC:
4789 case R_SH_GOTFUNCDESC20:
4790 case R_SH_FUNCDESC:
4792 int dynindx = -1;
4793 asection *reloc_section;
4794 bfd_vma reloc_offset;
4795 int reloc_type = R_SH_FUNCDESC;
4797 BFD_ASSERT (htab);
4799 check_segment[0] = check_segment[1] = -1;
4801 /* FIXME: See what FRV does for global symbols in the
4802 executable, with --export-dynamic. Do they need ld.so
4803 to allocate official descriptors? See what this code
4804 does. */
4806 relocation = 0;
4807 addend = 0;
4809 if (r_type == R_SH_FUNCDESC)
4811 reloc_section = input_section;
4812 reloc_offset = rel->r_offset;
4814 else
4816 reloc_section = sgot;
4818 if (h != NULL)
4819 reloc_offset = h->got.offset;
4820 else
4822 BFD_ASSERT (local_got_offsets != NULL);
4823 reloc_offset = local_got_offsets[r_symndx];
4825 BFD_ASSERT (reloc_offset != MINUS_ONE);
4827 if (reloc_offset & 1)
4829 reloc_offset &= ~1;
4830 goto funcdesc_done_got;
4834 if (h && h->root.type == bfd_link_hash_undefweak
4835 && (SYMBOL_CALLS_LOCAL (info, h)
4836 || !htab->root.dynamic_sections_created))
4837 /* Undefined weak symbol which will not be dynamically
4838 resolved later; leave it at zero. */
4839 goto funcdesc_leave_zero;
4840 else if (SYMBOL_CALLS_LOCAL (info, h)
4841 && ! SYMBOL_FUNCDESC_LOCAL (info, h))
4843 /* If the symbol needs a non-local function descriptor
4844 but binds locally (i.e., its visibility is
4845 protected), emit a dynamic relocation decayed to
4846 section+offset. This is an optimization; the dynamic
4847 linker would resolve our function descriptor request
4848 to our copy of the function anyway. */
4849 dynindx = elf_section_data (h->root.u.def.section
4850 ->output_section)->dynindx;
4851 relocation += h->root.u.def.section->output_offset
4852 + h->root.u.def.value;
4854 else if (! SYMBOL_FUNCDESC_LOCAL (info, h))
4856 /* If the symbol is dynamic and there will be dynamic
4857 symbol resolution because we are or are linked with a
4858 shared library, emit a FUNCDESC relocation such that
4859 the dynamic linker will allocate the function
4860 descriptor. */
4861 BFD_ASSERT (h->dynindx != -1);
4862 dynindx = h->dynindx;
4864 else
4866 bfd_vma offset;
4868 /* Otherwise, we know we have a private function
4869 descriptor, so reference it directly. */
4870 reloc_type = R_SH_DIR32;
4871 dynindx = elf_section_data (htab->sfuncdesc
4872 ->output_section)->dynindx;
4874 if (h)
4876 offset = sh_elf_hash_entry (h)->funcdesc.offset;
4877 BFD_ASSERT (offset != MINUS_ONE);
4878 if ((offset & 1) == 0)
4880 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4881 offset, NULL, 0))
4882 return FALSE;
4883 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4886 else
4888 union gotref *local_funcdesc;
4890 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4891 offset = local_funcdesc[r_symndx].offset;
4892 BFD_ASSERT (offset != MINUS_ONE);
4893 if ((offset & 1) == 0)
4895 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4896 offset, sec,
4897 sym->st_value))
4898 return FALSE;
4899 local_funcdesc[r_symndx].offset |= 1;
4903 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4906 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
4908 bfd_vma offset;
4910 if (sh_elf_osec_readonly_p (output_bfd,
4911 reloc_section->output_section))
4913 _bfd_error_handler
4914 /* xgettext:c-format */
4915 (_("%B(%A+%#Lx): cannot emit fixup to `%s' in read-only section"),
4916 input_bfd,
4917 input_section,
4918 rel->r_offset,
4919 symname);
4920 return FALSE;
4923 offset = _bfd_elf_section_offset (output_bfd, info,
4924 reloc_section, reloc_offset);
4926 if (offset != (bfd_vma)-1)
4927 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4928 offset
4929 + reloc_section->output_section->vma
4930 + reloc_section->output_offset);
4932 else if ((reloc_section->output_section->flags
4933 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
4935 bfd_vma offset;
4937 if (sh_elf_osec_readonly_p (output_bfd,
4938 reloc_section->output_section))
4940 info->callbacks->warning
4941 (info,
4942 _("cannot emit dynamic relocations in read-only section"),
4943 symname, input_bfd, reloc_section, reloc_offset);
4944 return FALSE;
4947 offset = _bfd_elf_section_offset (output_bfd, info,
4948 reloc_section, reloc_offset);
4950 if (offset != (bfd_vma)-1)
4951 sh_elf_add_dyn_reloc (output_bfd, srelgot,
4952 offset
4953 + reloc_section->output_section->vma
4954 + reloc_section->output_offset,
4955 reloc_type, dynindx, relocation);
4957 if (r_type == R_SH_FUNCDESC)
4959 r = bfd_reloc_ok;
4960 break;
4962 else
4964 relocation = 0;
4965 goto funcdesc_leave_zero;
4969 if (SYMBOL_FUNCDESC_LOCAL (info, h))
4970 relocation += htab->sfuncdesc->output_section->vma;
4971 funcdesc_leave_zero:
4972 if (r_type != R_SH_FUNCDESC)
4974 bfd_put_32 (output_bfd, relocation,
4975 reloc_section->contents + reloc_offset);
4976 if (h != NULL)
4977 h->got.offset |= 1;
4978 else
4979 local_got_offsets[r_symndx] |= 1;
4981 funcdesc_done_got:
4983 relocation = sh_elf_got_offset (htab) + reloc_offset;
4984 #ifdef GOT_BIAS
4985 relocation -= GOT_BIAS;
4986 #endif
4988 if (r_type == R_SH_GOTFUNCDESC20)
4990 r = install_movi20_field (output_bfd, relocation + addend,
4991 input_bfd, input_section, contents,
4992 rel->r_offset);
4993 break;
4995 else
4996 goto final_link_relocate;
4998 break;
5000 case R_SH_GOTOFFFUNCDESC:
5001 case R_SH_GOTOFFFUNCDESC20:
5002 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
5003 executable and --export-dynamic. If such symbols get
5004 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
5005 for them. */
5006 BFD_ASSERT (htab);
5008 check_segment[0] = check_segment[1] = -1;
5009 relocation = 0;
5010 addend = rel->r_addend;
5012 if (h && (h->root.type == bfd_link_hash_undefweak
5013 || !SYMBOL_FUNCDESC_LOCAL (info, h)))
5015 _bfd_error_handler
5016 /* xgettext:c-format */
5017 (_("%B(%A+%#Lx): %s relocation against external symbol \"%s\""),
5018 input_bfd, input_section, rel->r_offset, howto->name,
5019 h->root.root.string);
5020 return FALSE;
5022 else
5024 bfd_vma offset;
5026 /* Otherwise, we know we have a private function
5027 descriptor, so reference it directly. */
5028 if (h)
5030 offset = sh_elf_hash_entry (h)->funcdesc.offset;
5031 BFD_ASSERT (offset != MINUS_ONE);
5032 if ((offset & 1) == 0)
5034 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
5035 offset, NULL, 0))
5036 return FALSE;
5037 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
5040 else
5042 union gotref *local_funcdesc;
5044 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
5045 offset = local_funcdesc[r_symndx].offset;
5046 BFD_ASSERT (offset != MINUS_ONE);
5047 if ((offset & 1) == 0)
5049 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
5050 offset, sec,
5051 sym->st_value))
5052 return FALSE;
5053 local_funcdesc[r_symndx].offset |= 1;
5057 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
5060 relocation -= (htab->root.hgot->root.u.def.value
5061 + sgotplt->output_offset);
5062 #ifdef GOT_BIAS
5063 relocation -= GOT_BIAS;
5064 #endif
5066 if (r_type == R_SH_GOTOFFFUNCDESC20)
5068 r = install_movi20_field (output_bfd, relocation + addend,
5069 input_bfd, input_section, contents,
5070 rel->r_offset);
5071 break;
5073 else
5074 goto final_link_relocate;
5076 case R_SH_LOOP_START:
5078 static bfd_vma start, end;
5080 start = (relocation + rel->r_addend
5081 - (sec->output_section->vma + sec->output_offset));
5082 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5083 rel->r_offset, sec, start, end);
5084 break;
5086 case R_SH_LOOP_END:
5087 end = (relocation + rel->r_addend
5088 - (sec->output_section->vma + sec->output_offset));
5089 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
5090 rel->r_offset, sec, start, end);
5091 break;
5094 case R_SH_TLS_GD_32:
5095 case R_SH_TLS_IE_32:
5096 BFD_ASSERT (htab);
5097 check_segment[0] = check_segment[1] = -1;
5098 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5099 got_type = GOT_UNKNOWN;
5100 if (h == NULL && local_got_offsets)
5101 got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
5102 else if (h != NULL)
5104 got_type = sh_elf_hash_entry (h)->got_type;
5105 if (! bfd_link_pic (info)
5106 && (h->dynindx == -1
5107 || h->def_regular))
5108 r_type = R_SH_TLS_LE_32;
5111 if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
5112 r_type = R_SH_TLS_IE_32;
5114 if (r_type == R_SH_TLS_LE_32)
5116 bfd_vma offset;
5117 unsigned short insn;
5119 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
5121 /* GD->LE transition:
5122 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5123 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5124 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5125 We change it into:
5126 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
5127 nop; nop; ...
5128 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
5130 offset = rel->r_offset;
5131 BFD_ASSERT (offset >= 16);
5132 /* Size of GD instructions is 16 or 18. */
5133 offset -= 16;
5134 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5135 if ((insn & 0xff00) == 0xc700)
5137 BFD_ASSERT (offset >= 2);
5138 offset -= 2;
5139 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5142 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5143 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5144 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5145 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5146 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5147 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5148 BFD_ASSERT (insn == 0x310c);
5149 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5150 BFD_ASSERT (insn == 0x410b);
5151 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5152 BFD_ASSERT (insn == 0x34cc);
5154 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
5155 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
5156 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5157 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5158 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5160 else
5162 int target;
5164 /* IE->LE transition:
5165 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
5166 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
5167 We change it into:
5168 mov.l .Ln,rM; stc gbr,rN; nop; ...;
5169 1: x@TPOFF; 2:. */
5171 offset = rel->r_offset;
5172 BFD_ASSERT (offset >= 16);
5173 /* Size of IE instructions is 10 or 12. */
5174 offset -= 10;
5175 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5176 if ((insn & 0xf0ff) == 0x0012)
5178 BFD_ASSERT (offset >= 2);
5179 offset -= 2;
5180 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5183 BFD_ASSERT ((insn & 0xff00) == 0xd000);
5184 target = insn & 0x00ff;
5185 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5186 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
5187 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5188 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
5189 insn = 0xd000 | (insn & 0x0f00) | target;
5190 bfd_put_16 (output_bfd, insn, contents + offset + 0);
5191 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5194 bfd_put_32 (output_bfd, tpoff (info, relocation),
5195 contents + rel->r_offset);
5196 continue;
5199 if (sgot == NULL || sgotplt == NULL)
5200 abort ();
5202 if (h != NULL)
5203 off = h->got.offset;
5204 else
5206 if (local_got_offsets == NULL)
5207 abort ();
5209 off = local_got_offsets[r_symndx];
5212 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
5213 if (r_type == R_SH_TLS_IE_32
5214 && ! htab->root.dynamic_sections_created)
5216 off &= ~1;
5217 bfd_put_32 (output_bfd, tpoff (info, relocation),
5218 sgot->contents + off);
5219 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5220 contents + rel->r_offset);
5221 continue;
5224 if ((off & 1) != 0)
5225 off &= ~1;
5226 else
5228 Elf_Internal_Rela outrel;
5229 bfd_byte *loc;
5230 int dr_type, indx;
5232 outrel.r_offset = (sgot->output_section->vma
5233 + sgot->output_offset + off);
5235 if (h == NULL || h->dynindx == -1)
5236 indx = 0;
5237 else
5238 indx = h->dynindx;
5240 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
5241 R_SH_TLS_TPOFF32);
5242 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
5243 outrel.r_addend = relocation - dtpoff_base (info);
5244 else
5245 outrel.r_addend = 0;
5246 outrel.r_info = ELF32_R_INFO (indx, dr_type);
5247 loc = srelgot->contents;
5248 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5249 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5251 if (r_type == R_SH_TLS_GD_32)
5253 if (indx == 0)
5255 bfd_put_32 (output_bfd,
5256 relocation - dtpoff_base (info),
5257 sgot->contents + off + 4);
5259 else
5261 outrel.r_info = ELF32_R_INFO (indx,
5262 R_SH_TLS_DTPOFF32);
5263 outrel.r_offset += 4;
5264 outrel.r_addend = 0;
5265 srelgot->reloc_count++;
5266 loc += sizeof (Elf32_External_Rela);
5267 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5271 if (h != NULL)
5272 h->got.offset |= 1;
5273 else
5274 local_got_offsets[r_symndx] |= 1;
5277 if (off >= (bfd_vma) -2)
5278 abort ();
5280 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
5281 relocation = sh_elf_got_offset (htab) + off;
5282 else
5284 bfd_vma offset;
5285 unsigned short insn;
5287 /* GD->IE transition:
5288 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5289 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5290 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
5291 We change it into:
5292 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
5293 nop; nop; bra 3f; nop; .align 2;
5294 1: .long x@TPOFF; 2:...; 3:. */
5296 offset = rel->r_offset;
5297 BFD_ASSERT (offset >= 16);
5298 /* Size of GD instructions is 16 or 18. */
5299 offset -= 16;
5300 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5301 if ((insn & 0xff00) == 0xc700)
5303 BFD_ASSERT (offset >= 2);
5304 offset -= 2;
5305 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5308 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5310 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
5311 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
5313 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5314 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5315 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5316 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5317 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5318 BFD_ASSERT (insn == 0x310c);
5319 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5320 BFD_ASSERT (insn == 0x410b);
5321 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5322 BFD_ASSERT (insn == 0x34cc);
5324 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
5325 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
5326 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
5327 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5328 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5330 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
5331 contents + rel->r_offset);
5333 continue;
5336 addend = rel->r_addend;
5338 goto final_link_relocate;
5340 case R_SH_TLS_LD_32:
5341 BFD_ASSERT (htab);
5342 check_segment[0] = check_segment[1] = -1;
5343 if (! bfd_link_pic (info))
5345 bfd_vma offset;
5346 unsigned short insn;
5348 /* LD->LE transition:
5349 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
5350 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
5351 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
5352 We change it into:
5353 stc gbr,r0; nop; nop; nop;
5354 nop; nop; bra 3f; ...; 3:. */
5356 offset = rel->r_offset;
5357 BFD_ASSERT (offset >= 16);
5358 /* Size of LD instructions is 16 or 18. */
5359 offset -= 16;
5360 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5361 if ((insn & 0xff00) == 0xc700)
5363 BFD_ASSERT (offset >= 2);
5364 offset -= 2;
5365 insn = bfd_get_16 (input_bfd, contents + offset + 0);
5368 BFD_ASSERT ((insn & 0xff00) == 0xd400);
5369 insn = bfd_get_16 (input_bfd, contents + offset + 2);
5370 BFD_ASSERT ((insn & 0xff00) == 0xc700);
5371 insn = bfd_get_16 (input_bfd, contents + offset + 4);
5372 BFD_ASSERT ((insn & 0xff00) == 0xd100);
5373 insn = bfd_get_16 (input_bfd, contents + offset + 6);
5374 BFD_ASSERT (insn == 0x310c);
5375 insn = bfd_get_16 (input_bfd, contents + offset + 8);
5376 BFD_ASSERT (insn == 0x410b);
5377 insn = bfd_get_16 (input_bfd, contents + offset + 10);
5378 BFD_ASSERT (insn == 0x34cc);
5380 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
5381 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
5382 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
5383 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
5384 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
5385 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
5387 continue;
5390 if (sgot == NULL || sgotplt == NULL)
5391 abort ();
5393 off = htab->tls_ldm_got.offset;
5394 if (off & 1)
5395 off &= ~1;
5396 else
5398 Elf_Internal_Rela outrel;
5399 bfd_byte *loc;
5401 outrel.r_offset = (sgot->output_section->vma
5402 + sgot->output_offset + off);
5403 outrel.r_addend = 0;
5404 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
5405 loc = srelgot->contents;
5406 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
5407 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5408 htab->tls_ldm_got.offset |= 1;
5411 relocation = sh_elf_got_offset (htab) + off;
5412 addend = rel->r_addend;
5414 goto final_link_relocate;
5416 case R_SH_TLS_LDO_32:
5417 check_segment[0] = check_segment[1] = -1;
5418 if (! bfd_link_pic (info))
5419 relocation = tpoff (info, relocation);
5420 else
5421 relocation -= dtpoff_base (info);
5423 addend = rel->r_addend;
5424 goto final_link_relocate;
5426 case R_SH_TLS_LE_32:
5428 int indx;
5429 Elf_Internal_Rela outrel;
5430 bfd_byte *loc;
5432 check_segment[0] = check_segment[1] = -1;
5434 if (!bfd_link_dll (info))
5436 relocation = tpoff (info, relocation);
5437 addend = rel->r_addend;
5438 goto final_link_relocate;
5441 if (sreloc == NULL)
5443 sreloc = _bfd_elf_get_dynamic_reloc_section
5444 (input_bfd, input_section, /*rela?*/ TRUE);
5445 if (sreloc == NULL)
5446 return FALSE;
5449 if (h == NULL || h->dynindx == -1)
5450 indx = 0;
5451 else
5452 indx = h->dynindx;
5454 outrel.r_offset = (input_section->output_section->vma
5455 + input_section->output_offset
5456 + rel->r_offset);
5457 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
5458 if (indx == 0)
5459 outrel.r_addend = relocation - dtpoff_base (info);
5460 else
5461 outrel.r_addend = 0;
5463 loc = sreloc->contents;
5464 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5465 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5466 continue;
5470 relocation_done:
5471 if (fdpic_p && check_segment[0] != (unsigned) -1
5472 && check_segment[0] != check_segment[1])
5474 /* We don't want duplicate errors for undefined symbols. */
5475 if (!h || h->root.type != bfd_link_hash_undefined)
5477 if (bfd_link_pic (info))
5479 info->callbacks->einfo
5480 /* xgettext:c-format */
5481 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5482 input_bfd, input_section, rel->r_offset, symname);
5483 return FALSE;
5485 else
5486 info->callbacks->einfo
5487 /* xgettext:c-format */
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)
5497 switch (r)
5499 default:
5500 case bfd_reloc_outofrange:
5501 abort ();
5502 case bfd_reloc_overflow:
5504 const char *name;
5506 if (h != NULL)
5507 name = NULL;
5508 else
5510 name = (bfd_elf_string_from_elf_section
5511 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5512 if (name == NULL)
5513 return FALSE;
5514 if (*name == '\0')
5515 name = bfd_section_name (input_bfd, sec);
5517 (*info->callbacks->reloc_overflow)
5518 (info, (h ? &h->root : NULL), name, howto->name,
5519 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5521 break;
5526 return TRUE;
5529 /* This is a version of bfd_generic_get_relocated_section_contents
5530 which uses sh_elf_relocate_section. */
5532 static bfd_byte *
5533 sh_elf_get_relocated_section_contents (bfd *output_bfd,
5534 struct bfd_link_info *link_info,
5535 struct bfd_link_order *link_order,
5536 bfd_byte *data,
5537 bfd_boolean relocatable,
5538 asymbol **symbols)
5540 Elf_Internal_Shdr *symtab_hdr;
5541 asection *input_section = link_order->u.indirect.section;
5542 bfd *input_bfd = input_section->owner;
5543 asection **sections = NULL;
5544 Elf_Internal_Rela *internal_relocs = NULL;
5545 Elf_Internal_Sym *isymbuf = NULL;
5547 /* We only need to handle the case of relaxing, or of having a
5548 particular set of section contents, specially. */
5549 if (relocatable
5550 || elf_section_data (input_section)->this_hdr.contents == NULL)
5551 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5552 link_order, data,
5553 relocatable,
5554 symbols);
5556 symtab_hdr = &elf_symtab_hdr (input_bfd);
5558 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5559 (size_t) input_section->size);
5561 if ((input_section->flags & SEC_RELOC) != 0
5562 && input_section->reloc_count > 0)
5564 asection **secpp;
5565 Elf_Internal_Sym *isym, *isymend;
5566 bfd_size_type amt;
5568 internal_relocs = (_bfd_elf_link_read_relocs
5569 (input_bfd, input_section, NULL,
5570 (Elf_Internal_Rela *) NULL, FALSE));
5571 if (internal_relocs == NULL)
5572 goto error_return;
5574 if (symtab_hdr->sh_info != 0)
5576 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5577 if (isymbuf == NULL)
5578 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5579 symtab_hdr->sh_info, 0,
5580 NULL, NULL, NULL);
5581 if (isymbuf == NULL)
5582 goto error_return;
5585 amt = symtab_hdr->sh_info;
5586 amt *= sizeof (asection *);
5587 sections = (asection **) bfd_malloc (amt);
5588 if (sections == NULL && amt != 0)
5589 goto error_return;
5591 isymend = isymbuf + symtab_hdr->sh_info;
5592 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5594 asection *isec;
5596 if (isym->st_shndx == SHN_UNDEF)
5597 isec = bfd_und_section_ptr;
5598 else if (isym->st_shndx == SHN_ABS)
5599 isec = bfd_abs_section_ptr;
5600 else if (isym->st_shndx == SHN_COMMON)
5601 isec = bfd_com_section_ptr;
5602 else
5603 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5605 *secpp = isec;
5608 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5609 input_section, data, internal_relocs,
5610 isymbuf, sections))
5611 goto error_return;
5613 if (sections != NULL)
5614 free (sections);
5615 if (isymbuf != NULL
5616 && symtab_hdr->contents != (unsigned char *) isymbuf)
5617 free (isymbuf);
5618 if (elf_section_data (input_section)->relocs != internal_relocs)
5619 free (internal_relocs);
5622 return data;
5624 error_return:
5625 if (sections != NULL)
5626 free (sections);
5627 if (isymbuf != NULL
5628 && symtab_hdr->contents != (unsigned char *) isymbuf)
5629 free (isymbuf);
5630 if (internal_relocs != NULL
5631 && elf_section_data (input_section)->relocs != internal_relocs)
5632 free (internal_relocs);
5633 return NULL;
5636 /* Return the base VMA address which should be subtracted from real addresses
5637 when resolving @dtpoff relocation.
5638 This is PT_TLS segment p_vaddr. */
5640 static bfd_vma
5641 dtpoff_base (struct bfd_link_info *info)
5643 /* If tls_sec is NULL, we should have signalled an error already. */
5644 if (elf_hash_table (info)->tls_sec == NULL)
5645 return 0;
5646 return elf_hash_table (info)->tls_sec->vma;
5649 /* Return the relocation value for R_SH_TLS_TPOFF32.. */
5651 static bfd_vma
5652 tpoff (struct bfd_link_info *info, bfd_vma address)
5654 /* If tls_sec is NULL, we should have signalled an error already. */
5655 if (elf_hash_table (info)->tls_sec == NULL)
5656 return 0;
5657 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5658 structure which has 2 pointer fields. */
5659 return (address - elf_hash_table (info)->tls_sec->vma
5660 + align_power ((bfd_vma) 8,
5661 elf_hash_table (info)->tls_sec->alignment_power));
5664 static asection *
5665 sh_elf_gc_mark_hook (asection *sec,
5666 struct bfd_link_info *info,
5667 Elf_Internal_Rela *rel,
5668 struct elf_link_hash_entry *h,
5669 Elf_Internal_Sym *sym)
5671 if (h != NULL)
5672 switch (ELF32_R_TYPE (rel->r_info))
5674 case R_SH_GNU_VTINHERIT:
5675 case R_SH_GNU_VTENTRY:
5676 return NULL;
5679 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5682 /* Copy the extra info we tack onto an elf_link_hash_entry. */
5684 static void
5685 sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
5686 struct elf_link_hash_entry *dir,
5687 struct elf_link_hash_entry *ind)
5689 struct elf_sh_link_hash_entry *edir, *eind;
5691 edir = (struct elf_sh_link_hash_entry *) dir;
5692 eind = (struct elf_sh_link_hash_entry *) ind;
5694 if (eind->dyn_relocs != NULL)
5696 if (edir->dyn_relocs != NULL)
5698 struct elf_sh_dyn_relocs **pp;
5699 struct elf_sh_dyn_relocs *p;
5701 /* Add reloc counts against the indirect sym to the direct sym
5702 list. Merge any entries against the same section. */
5703 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5705 struct elf_sh_dyn_relocs *q;
5707 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5708 if (q->sec == p->sec)
5710 q->pc_count += p->pc_count;
5711 q->count += p->count;
5712 *pp = p->next;
5713 break;
5715 if (q == NULL)
5716 pp = &p->next;
5718 *pp = edir->dyn_relocs;
5721 edir->dyn_relocs = eind->dyn_relocs;
5722 eind->dyn_relocs = NULL;
5724 edir->gotplt_refcount = eind->gotplt_refcount;
5725 eind->gotplt_refcount = 0;
5726 #ifdef INCLUDE_SHMEDIA
5727 edir->datalabel_got.refcount += eind->datalabel_got.refcount;
5728 eind->datalabel_got.refcount = 0;
5729 #endif
5730 edir->funcdesc.refcount += eind->funcdesc.refcount;
5731 eind->funcdesc.refcount = 0;
5732 edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
5733 eind->abs_funcdesc_refcount = 0;
5735 if (ind->root.type == bfd_link_hash_indirect
5736 && dir->got.refcount <= 0)
5738 edir->got_type = eind->got_type;
5739 eind->got_type = GOT_UNKNOWN;
5742 if (ind->root.type != bfd_link_hash_indirect
5743 && dir->dynamic_adjusted)
5745 /* If called to transfer flags for a weakdef during processing
5746 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5747 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
5748 if (dir->versioned != versioned_hidden)
5749 dir->ref_dynamic |= ind->ref_dynamic;
5750 dir->ref_regular |= ind->ref_regular;
5751 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
5752 dir->needs_plt |= ind->needs_plt;
5754 else
5755 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
5758 static int
5759 sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
5760 int is_local)
5762 if (bfd_link_pic (info))
5763 return r_type;
5765 switch (r_type)
5767 case R_SH_TLS_GD_32:
5768 case R_SH_TLS_IE_32:
5769 if (is_local)
5770 return R_SH_TLS_LE_32;
5771 return R_SH_TLS_IE_32;
5772 case R_SH_TLS_LD_32:
5773 return R_SH_TLS_LE_32;
5776 return r_type;
5779 /* Look through the relocs for a section during the first phase.
5780 Since we don't do .gots or .plts, we just need to consider the
5781 virtual table relocs for gc. */
5783 static bfd_boolean
5784 sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
5785 const Elf_Internal_Rela *relocs)
5787 Elf_Internal_Shdr *symtab_hdr;
5788 struct elf_link_hash_entry **sym_hashes;
5789 struct elf_sh_link_hash_table *htab;
5790 const Elf_Internal_Rela *rel;
5791 const Elf_Internal_Rela *rel_end;
5792 asection *sreloc;
5793 unsigned int r_type;
5794 enum got_type got_type, old_got_type;
5796 sreloc = NULL;
5798 if (bfd_link_relocatable (info))
5799 return TRUE;
5801 BFD_ASSERT (is_sh_elf (abfd));
5803 symtab_hdr = &elf_symtab_hdr (abfd);
5804 sym_hashes = elf_sym_hashes (abfd);
5806 htab = sh_elf_hash_table (info);
5807 if (htab == NULL)
5808 return FALSE;
5810 rel_end = relocs + sec->reloc_count;
5811 for (rel = relocs; rel < rel_end; rel++)
5813 struct elf_link_hash_entry *h;
5814 unsigned long r_symndx;
5815 #ifdef INCLUDE_SHMEDIA
5816 int seen_stt_datalabel = 0;
5817 #endif
5819 r_symndx = ELF32_R_SYM (rel->r_info);
5820 r_type = ELF32_R_TYPE (rel->r_info);
5822 if (r_symndx < symtab_hdr->sh_info)
5823 h = NULL;
5824 else
5826 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5827 while (h->root.type == bfd_link_hash_indirect
5828 || h->root.type == bfd_link_hash_warning)
5830 #ifdef INCLUDE_SHMEDIA
5831 seen_stt_datalabel |= h->type == STT_DATALABEL;
5832 #endif
5833 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5836 /* PR15323, ref flags aren't set for references in the same
5837 object. */
5838 h->root.non_ir_ref_regular = 1;
5841 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5842 if (! bfd_link_pic (info)
5843 && r_type == R_SH_TLS_IE_32
5844 && h != NULL
5845 && h->root.type != bfd_link_hash_undefined
5846 && h->root.type != bfd_link_hash_undefweak
5847 && (h->dynindx == -1
5848 || h->def_regular))
5849 r_type = R_SH_TLS_LE_32;
5851 if (htab->fdpic_p)
5852 switch (r_type)
5854 case R_SH_GOTOFFFUNCDESC:
5855 case R_SH_GOTOFFFUNCDESC20:
5856 case R_SH_FUNCDESC:
5857 case R_SH_GOTFUNCDESC:
5858 case R_SH_GOTFUNCDESC20:
5859 if (h != NULL)
5861 if (h->dynindx == -1)
5862 switch (ELF_ST_VISIBILITY (h->other))
5864 case STV_INTERNAL:
5865 case STV_HIDDEN:
5866 break;
5867 default:
5868 bfd_elf_link_record_dynamic_symbol (info, h);
5869 break;
5872 break;
5875 /* Some relocs require a global offset table. */
5876 if (htab->root.sgot == NULL)
5878 switch (r_type)
5880 case R_SH_DIR32:
5881 /* This may require an rofixup. */
5882 if (!htab->fdpic_p)
5883 break;
5884 /* Fall through. */
5885 case R_SH_GOTPLT32:
5886 case R_SH_GOT32:
5887 case R_SH_GOT20:
5888 case R_SH_GOTOFF:
5889 case R_SH_GOTOFF20:
5890 case R_SH_FUNCDESC:
5891 case R_SH_GOTFUNCDESC:
5892 case R_SH_GOTFUNCDESC20:
5893 case R_SH_GOTOFFFUNCDESC:
5894 case R_SH_GOTOFFFUNCDESC20:
5895 case R_SH_GOTPC:
5896 #ifdef INCLUDE_SHMEDIA
5897 case R_SH_GOTPLT_LOW16:
5898 case R_SH_GOTPLT_MEDLOW16:
5899 case R_SH_GOTPLT_MEDHI16:
5900 case R_SH_GOTPLT_HI16:
5901 case R_SH_GOTPLT10BY4:
5902 case R_SH_GOTPLT10BY8:
5903 case R_SH_GOT_LOW16:
5904 case R_SH_GOT_MEDLOW16:
5905 case R_SH_GOT_MEDHI16:
5906 case R_SH_GOT_HI16:
5907 case R_SH_GOT10BY4:
5908 case R_SH_GOT10BY8:
5909 case R_SH_GOTOFF_LOW16:
5910 case R_SH_GOTOFF_MEDLOW16:
5911 case R_SH_GOTOFF_MEDHI16:
5912 case R_SH_GOTOFF_HI16:
5913 case R_SH_GOTPC_LOW16:
5914 case R_SH_GOTPC_MEDLOW16:
5915 case R_SH_GOTPC_MEDHI16:
5916 case R_SH_GOTPC_HI16:
5917 #endif
5918 case R_SH_TLS_GD_32:
5919 case R_SH_TLS_LD_32:
5920 case R_SH_TLS_IE_32:
5921 if (htab->root.dynobj == NULL)
5922 htab->root.dynobj = abfd;
5923 if (!create_got_section (htab->root.dynobj, info))
5924 return FALSE;
5925 break;
5927 default:
5928 break;
5932 switch (r_type)
5934 /* This relocation describes the C++ object vtable hierarchy.
5935 Reconstruct it for later use during GC. */
5936 case R_SH_GNU_VTINHERIT:
5937 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5938 return FALSE;
5939 break;
5941 /* This relocation describes which C++ vtable entries are actually
5942 used. Record for later use during GC. */
5943 case R_SH_GNU_VTENTRY:
5944 BFD_ASSERT (h != NULL);
5945 if (h != NULL
5946 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5947 return FALSE;
5948 break;
5950 case R_SH_TLS_IE_32:
5951 if (bfd_link_pic (info))
5952 info->flags |= DF_STATIC_TLS;
5954 /* FALLTHROUGH */
5955 force_got:
5956 case R_SH_TLS_GD_32:
5957 case R_SH_GOT32:
5958 case R_SH_GOT20:
5959 #ifdef INCLUDE_SHMEDIA
5960 case R_SH_GOT_LOW16:
5961 case R_SH_GOT_MEDLOW16:
5962 case R_SH_GOT_MEDHI16:
5963 case R_SH_GOT_HI16:
5964 case R_SH_GOT10BY4:
5965 case R_SH_GOT10BY8:
5966 #endif
5967 case R_SH_GOTFUNCDESC:
5968 case R_SH_GOTFUNCDESC20:
5969 switch (r_type)
5971 default:
5972 got_type = GOT_NORMAL;
5973 break;
5974 case R_SH_TLS_GD_32:
5975 got_type = GOT_TLS_GD;
5976 break;
5977 case R_SH_TLS_IE_32:
5978 got_type = GOT_TLS_IE;
5979 break;
5980 case R_SH_GOTFUNCDESC:
5981 case R_SH_GOTFUNCDESC20:
5982 got_type = GOT_FUNCDESC;
5983 break;
5986 if (h != NULL)
5988 #ifdef INCLUDE_SHMEDIA
5989 if (seen_stt_datalabel)
5991 struct elf_sh_link_hash_entry *eh
5992 = (struct elf_sh_link_hash_entry *) h;
5994 eh->datalabel_got.refcount += 1;
5996 else
5997 #endif
5998 h->got.refcount += 1;
5999 old_got_type = sh_elf_hash_entry (h)->got_type;
6001 else
6003 bfd_signed_vma *local_got_refcounts;
6005 /* This is a global offset table entry for a local
6006 symbol. */
6007 local_got_refcounts = elf_local_got_refcounts (abfd);
6008 if (local_got_refcounts == NULL)
6010 bfd_size_type size;
6012 size = symtab_hdr->sh_info;
6013 size *= sizeof (bfd_signed_vma);
6014 #ifdef INCLUDE_SHMEDIA
6015 /* Reserve space for both the datalabel and
6016 codelabel local GOT offsets. */
6017 size *= 2;
6018 #endif
6019 size += symtab_hdr->sh_info;
6020 local_got_refcounts = ((bfd_signed_vma *)
6021 bfd_zalloc (abfd, size));
6022 if (local_got_refcounts == NULL)
6023 return FALSE;
6024 elf_local_got_refcounts (abfd) = local_got_refcounts;
6025 #ifdef INCLUDE_SHMEDIA
6026 /* Take care of both the datalabel and codelabel local
6027 GOT offsets. */
6028 sh_elf_local_got_type (abfd)
6029 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
6030 #else
6031 sh_elf_local_got_type (abfd)
6032 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
6033 #endif
6035 #ifdef INCLUDE_SHMEDIA
6036 if (rel->r_addend & 1)
6037 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
6038 else
6039 #endif
6040 local_got_refcounts[r_symndx] += 1;
6041 old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
6044 /* If a TLS symbol is accessed using IE at least once,
6045 there is no point to use dynamic model for it. */
6046 if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
6047 && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
6049 if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
6050 got_type = GOT_TLS_IE;
6051 else
6053 if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
6054 && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
6055 _bfd_error_handler
6056 /* xgettext:c-format */
6057 (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6058 abfd, h->root.root.string);
6059 else if (old_got_type == GOT_FUNCDESC
6060 || got_type == GOT_FUNCDESC)
6061 _bfd_error_handler
6062 /* xgettext:c-format */
6063 (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6064 abfd, h->root.root.string);
6065 else
6066 _bfd_error_handler
6067 /* xgettext:c-format */
6068 (_("%B: `%s' accessed both as normal and thread local symbol"),
6069 abfd, h->root.root.string);
6070 return FALSE;
6074 if (old_got_type != got_type)
6076 if (h != NULL)
6077 sh_elf_hash_entry (h)->got_type = got_type;
6078 else
6079 sh_elf_local_got_type (abfd) [r_symndx] = got_type;
6082 break;
6084 case R_SH_TLS_LD_32:
6085 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
6086 break;
6088 case R_SH_FUNCDESC:
6089 case R_SH_GOTOFFFUNCDESC:
6090 case R_SH_GOTOFFFUNCDESC20:
6091 if (rel->r_addend)
6093 _bfd_error_handler
6094 (_("%B: Function descriptor relocation with non-zero addend"),
6095 abfd);
6096 return FALSE;
6099 if (h == NULL)
6101 union gotref *local_funcdesc;
6103 /* We need a function descriptor for a local symbol. */
6104 local_funcdesc = sh_elf_local_funcdesc (abfd);
6105 if (local_funcdesc == NULL)
6107 bfd_size_type size;
6109 size = symtab_hdr->sh_info * sizeof (union gotref);
6110 #ifdef INCLUDE_SHMEDIA
6111 /* Count datalabel local GOT. */
6112 size *= 2;
6113 #endif
6114 local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
6115 if (local_funcdesc == NULL)
6116 return FALSE;
6117 sh_elf_local_funcdesc (abfd) = local_funcdesc;
6119 local_funcdesc[r_symndx].refcount += 1;
6121 if (r_type == R_SH_FUNCDESC)
6123 if (!bfd_link_pic (info))
6124 htab->srofixup->size += 4;
6125 else
6126 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
6129 else
6131 sh_elf_hash_entry (h)->funcdesc.refcount++;
6132 if (r_type == R_SH_FUNCDESC)
6133 sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
6135 /* If there is a function descriptor reference, then
6136 there should not be any non-FDPIC references. */
6137 old_got_type = sh_elf_hash_entry (h)->got_type;
6138 if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
6140 if (old_got_type == GOT_NORMAL)
6141 _bfd_error_handler
6142 /* xgettext:c-format */
6143 (_("%B: `%s' accessed both as normal and FDPIC symbol"),
6144 abfd, h->root.root.string);
6145 else
6146 _bfd_error_handler
6147 /* xgettext:c-format */
6148 (_("%B: `%s' accessed both as FDPIC and thread local symbol"),
6149 abfd, h->root.root.string);
6152 break;
6154 case R_SH_GOTPLT32:
6155 #ifdef INCLUDE_SHMEDIA
6156 case R_SH_GOTPLT_LOW16:
6157 case R_SH_GOTPLT_MEDLOW16:
6158 case R_SH_GOTPLT_MEDHI16:
6159 case R_SH_GOTPLT_HI16:
6160 case R_SH_GOTPLT10BY4:
6161 case R_SH_GOTPLT10BY8:
6162 #endif
6163 /* If this is a local symbol, we resolve it directly without
6164 creating a procedure linkage table entry. */
6166 if (h == NULL
6167 || h->forced_local
6168 || ! bfd_link_pic (info)
6169 || info->symbolic
6170 || h->dynindx == -1)
6171 goto force_got;
6173 h->needs_plt = 1;
6174 h->plt.refcount += 1;
6175 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
6177 break;
6179 case R_SH_PLT32:
6180 #ifdef INCLUDE_SHMEDIA
6181 case R_SH_PLT_LOW16:
6182 case R_SH_PLT_MEDLOW16:
6183 case R_SH_PLT_MEDHI16:
6184 case R_SH_PLT_HI16:
6185 #endif
6186 /* This symbol requires a procedure linkage table entry. We
6187 actually build the entry in adjust_dynamic_symbol,
6188 because this might be a case of linking PIC code which is
6189 never referenced by a dynamic object, in which case we
6190 don't need to generate a procedure linkage table entry
6191 after all. */
6193 /* If this is a local symbol, we resolve it directly without
6194 creating a procedure linkage table entry. */
6195 if (h == NULL)
6196 continue;
6198 if (h->forced_local)
6199 break;
6201 h->needs_plt = 1;
6202 h->plt.refcount += 1;
6203 break;
6205 case R_SH_DIR32:
6206 case R_SH_REL32:
6207 #ifdef INCLUDE_SHMEDIA
6208 case R_SH_IMM_LOW16_PCREL:
6209 case R_SH_IMM_MEDLOW16_PCREL:
6210 case R_SH_IMM_MEDHI16_PCREL:
6211 case R_SH_IMM_HI16_PCREL:
6212 #endif
6213 if (h != NULL && ! bfd_link_pic (info))
6215 h->non_got_ref = 1;
6216 h->plt.refcount += 1;
6219 /* If we are creating a shared library, and this is a reloc
6220 against a global symbol, or a non PC relative reloc
6221 against a local symbol, then we need to copy the reloc
6222 into the shared library. However, if we are linking with
6223 -Bsymbolic, we do not need to copy a reloc against a
6224 global symbol which is defined in an object we are
6225 including in the link (i.e., DEF_REGULAR is set). At
6226 this point we have not seen all the input files, so it is
6227 possible that DEF_REGULAR is not set now but will be set
6228 later (it is never cleared). We account for that
6229 possibility below by storing information in the
6230 dyn_relocs field of the hash table entry. A similar
6231 situation occurs when creating shared libraries and symbol
6232 visibility changes render the symbol local.
6234 If on the other hand, we are creating an executable, we
6235 may need to keep relocations for symbols satisfied by a
6236 dynamic library if we manage to avoid copy relocs for the
6237 symbol. */
6238 if ((bfd_link_pic (info)
6239 && (sec->flags & SEC_ALLOC) != 0
6240 && (r_type != R_SH_REL32
6241 || (h != NULL
6242 && (! info->symbolic
6243 || h->root.type == bfd_link_hash_defweak
6244 || !h->def_regular))))
6245 || (! bfd_link_pic (info)
6246 && (sec->flags & SEC_ALLOC) != 0
6247 && h != NULL
6248 && (h->root.type == bfd_link_hash_defweak
6249 || !h->def_regular)))
6251 struct elf_sh_dyn_relocs *p;
6252 struct elf_sh_dyn_relocs **head;
6254 if (htab->root.dynobj == NULL)
6255 htab->root.dynobj = abfd;
6257 /* When creating a shared object, we must copy these
6258 reloc types into the output file. We create a reloc
6259 section in dynobj and make room for this reloc. */
6260 if (sreloc == NULL)
6262 sreloc = _bfd_elf_make_dynamic_reloc_section
6263 (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE);
6265 if (sreloc == NULL)
6266 return FALSE;
6269 /* If this is a global symbol, we count the number of
6270 relocations we need for this symbol. */
6271 if (h != NULL)
6272 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
6273 else
6275 /* Track dynamic relocs needed for local syms too. */
6276 asection *s;
6277 void *vpp;
6278 Elf_Internal_Sym *isym;
6280 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6281 abfd, r_symndx);
6282 if (isym == NULL)
6283 return FALSE;
6285 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6286 if (s == NULL)
6287 s = sec;
6289 vpp = &elf_section_data (s)->local_dynrel;
6290 head = (struct elf_sh_dyn_relocs **) vpp;
6293 p = *head;
6294 if (p == NULL || p->sec != sec)
6296 bfd_size_type amt = sizeof (*p);
6297 p = bfd_alloc (htab->root.dynobj, amt);
6298 if (p == NULL)
6299 return FALSE;
6300 p->next = *head;
6301 *head = p;
6302 p->sec = sec;
6303 p->count = 0;
6304 p->pc_count = 0;
6307 p->count += 1;
6308 if (r_type == R_SH_REL32
6309 #ifdef INCLUDE_SHMEDIA
6310 || r_type == R_SH_IMM_LOW16_PCREL
6311 || r_type == R_SH_IMM_MEDLOW16_PCREL
6312 || r_type == R_SH_IMM_MEDHI16_PCREL
6313 || r_type == R_SH_IMM_HI16_PCREL
6314 #endif
6316 p->pc_count += 1;
6319 /* Allocate the fixup regardless of whether we need a relocation.
6320 If we end up generating the relocation, we'll unallocate the
6321 fixup. */
6322 if (htab->fdpic_p && !bfd_link_pic (info)
6323 && r_type == R_SH_DIR32
6324 && (sec->flags & SEC_ALLOC) != 0)
6325 htab->srofixup->size += 4;
6326 break;
6328 case R_SH_TLS_LE_32:
6329 if (bfd_link_dll (info))
6331 _bfd_error_handler
6332 (_("%B: TLS local exec code cannot be linked into shared objects"),
6333 abfd);
6334 return FALSE;
6337 break;
6339 case R_SH_TLS_LDO_32:
6340 /* Nothing to do. */
6341 break;
6343 default:
6344 break;
6348 return TRUE;
6351 #ifndef sh_elf_set_mach_from_flags
6352 static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
6354 static bfd_boolean
6355 sh_elf_set_mach_from_flags (bfd *abfd)
6357 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
6359 if (flags >= ARRAY_SIZE (sh_ef_bfd_table))
6360 return FALSE;
6362 if (sh_ef_bfd_table[flags] == 0)
6363 return FALSE;
6365 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
6367 return TRUE;
6371 /* Reverse table lookup for sh_ef_bfd_table[].
6372 Given a bfd MACH value from archures.c
6373 return the equivalent ELF flags from the table.
6374 Return -1 if no match is found. */
6377 sh_elf_get_flags_from_mach (unsigned long mach)
6379 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
6381 for (; i>0; i--)
6382 if (sh_ef_bfd_table[i] == mach)
6383 return i;
6385 /* shouldn't get here */
6386 BFD_FAIL();
6388 return -1;
6390 #endif /* not sh_elf_set_mach_from_flags */
6392 #ifndef sh_elf_copy_private_data
6393 /* Copy backend specific data from one object module to another */
6395 static bfd_boolean
6396 sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
6398 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6399 return TRUE;
6401 if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
6402 return FALSE;
6404 return sh_elf_set_mach_from_flags (obfd);
6406 #endif /* not sh_elf_copy_private_data */
6408 #ifndef sh_elf_merge_private_data
6410 /* This function returns the ELF architecture number that
6411 corresponds to the given arch_sh* flags. */
6414 sh_find_elf_flags (unsigned int arch_set)
6416 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
6417 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
6419 return sh_elf_get_flags_from_mach (bfd_mach);
6422 /* Merge the architecture type of two BFD files, such that the
6423 resultant architecture supports all the features required
6424 by the two input BFDs.
6425 If the input BFDs are multually incompatible - i.e. one uses
6426 DSP while the other uses FPU - or there is no known architecture
6427 that fits the requirements then an error is emitted. */
6429 static bfd_boolean
6430 sh_merge_bfd_arch (bfd *ibfd, struct bfd_link_info *info)
6432 bfd *obfd = info->output_bfd;
6433 unsigned int old_arch, new_arch, merged_arch;
6435 if (! _bfd_generic_verify_endian_match (ibfd, info))
6436 return FALSE;
6438 old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
6439 new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
6441 merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
6443 if (!SH_VALID_CO_ARCH_SET (merged_arch))
6445 _bfd_error_handler
6446 /* xgettext:c-format */
6447 (_("%B: uses %s instructions while previous modules "
6448 "use %s instructions"),
6449 ibfd,
6450 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
6451 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
6452 bfd_set_error (bfd_error_bad_value);
6453 return FALSE;
6455 else if (!SH_VALID_ARCH_SET (merged_arch))
6457 _bfd_error_handler
6458 /* xgettext:c-format */
6459 (_("internal error: merge of architecture '%s' with "
6460 "architecture '%s' produced unknown architecture"),
6461 bfd_printable_name (obfd),
6462 bfd_printable_name (ibfd));
6463 bfd_set_error (bfd_error_bad_value);
6464 return FALSE;
6467 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
6468 sh_get_bfd_mach_from_arch_set (merged_arch));
6470 return TRUE;
6473 /* This routine initialises the elf flags when required and
6474 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
6476 static bfd_boolean
6477 sh_elf_merge_private_data (bfd *ibfd, struct bfd_link_info *info)
6479 bfd *obfd = info->output_bfd;
6481 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
6482 return TRUE;
6484 if (! elf_flags_init (obfd))
6486 /* This happens when ld starts out with a 'blank' output file. */
6487 elf_flags_init (obfd) = TRUE;
6488 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6489 sh_elf_set_mach_from_flags (obfd);
6490 if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
6491 elf_elfheader (obfd)->e_flags &= ~EF_SH_PIC;
6494 if (! sh_merge_bfd_arch (ibfd, info))
6496 _bfd_error_handler (_("%B: uses instructions which are incompatible "
6497 "with instructions used in previous modules"),
6498 ibfd);
6499 bfd_set_error (bfd_error_bad_value);
6500 return FALSE;
6503 elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
6504 elf_elfheader (obfd)->e_flags |=
6505 sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
6507 if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
6509 _bfd_error_handler (_("%B: attempt to mix FDPIC and non-FDPIC objects"),
6510 ibfd);
6511 bfd_set_error (bfd_error_bad_value);
6512 return FALSE;
6515 return TRUE;
6517 #endif /* not sh_elf_merge_private_data */
6519 /* Override the generic function because we need to store sh_elf_obj_tdata
6520 as the specific tdata. We set also the machine architecture from flags
6521 here. */
6523 static bfd_boolean
6524 sh_elf_object_p (bfd *abfd)
6526 if (! sh_elf_set_mach_from_flags (abfd))
6527 return FALSE;
6529 return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
6530 == fdpic_object_p (abfd));
6533 /* Finish up dynamic symbol handling. We set the contents of various
6534 dynamic sections here. */
6536 static bfd_boolean
6537 sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6538 struct elf_link_hash_entry *h,
6539 Elf_Internal_Sym *sym)
6541 struct elf_sh_link_hash_table *htab;
6543 htab = sh_elf_hash_table (info);
6544 if (htab == NULL)
6545 return FALSE;
6547 if (h->plt.offset != (bfd_vma) -1)
6549 asection *splt;
6550 asection *sgotplt;
6551 asection *srelplt;
6553 bfd_vma plt_index;
6554 bfd_vma got_offset;
6555 Elf_Internal_Rela rel;
6556 bfd_byte *loc;
6557 const struct elf_sh_plt_info *plt_info;
6559 /* This symbol has an entry in the procedure linkage table. Set
6560 it up. */
6562 BFD_ASSERT (h->dynindx != -1);
6564 splt = htab->root.splt;
6565 sgotplt = htab->root.sgotplt;
6566 srelplt = htab->root.srelplt;
6567 BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
6569 /* Get the index in the procedure linkage table which
6570 corresponds to this symbol. This is the index of this symbol
6571 in all the symbols for which we are making plt entries. The
6572 first entry in the procedure linkage table is reserved. */
6573 plt_index = get_plt_index (htab->plt_info, h->plt.offset);
6575 plt_info = htab->plt_info;
6576 if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
6577 plt_info = plt_info->short_plt;
6579 /* Get the offset into the .got table of the entry that
6580 corresponds to this function. */
6581 if (htab->fdpic_p)
6582 /* The offset must be relative to the GOT symbol, twelve bytes
6583 before the end of .got.plt. Each descriptor is eight
6584 bytes. */
6585 got_offset = plt_index * 8 + 12 - sgotplt->size;
6586 else
6587 /* Each .got entry is 4 bytes. The first three are
6588 reserved. */
6589 got_offset = (plt_index + 3) * 4;
6591 #ifdef GOT_BIAS
6592 if (bfd_link_pic (info))
6593 got_offset -= GOT_BIAS;
6594 #endif
6596 /* Fill in the entry in the procedure linkage table. */
6597 memcpy (splt->contents + h->plt.offset,
6598 plt_info->symbol_entry,
6599 plt_info->symbol_entry_size);
6601 if (bfd_link_pic (info) || htab->fdpic_p)
6603 if (plt_info->symbol_fields.got20)
6605 bfd_reloc_status_type r;
6606 r = install_movi20_field (output_bfd, got_offset,
6607 splt->owner, splt, splt->contents,
6608 h->plt.offset
6609 + plt_info->symbol_fields.got_entry);
6610 BFD_ASSERT (r == bfd_reloc_ok);
6612 else
6613 install_plt_field (output_bfd, FALSE, got_offset,
6614 (splt->contents
6615 + h->plt.offset
6616 + plt_info->symbol_fields.got_entry));
6618 else
6620 BFD_ASSERT (!plt_info->symbol_fields.got20);
6622 install_plt_field (output_bfd, FALSE,
6623 (sgotplt->output_section->vma
6624 + sgotplt->output_offset
6625 + got_offset),
6626 (splt->contents
6627 + h->plt.offset
6628 + plt_info->symbol_fields.got_entry));
6629 if (htab->vxworks_p)
6631 unsigned int reachable_plts, plts_per_4k;
6632 int distance;
6634 /* Divide the PLT into groups. The first group contains
6635 REACHABLE_PLTS entries and the other groups contain
6636 PLTS_PER_4K entries. Entries in the first group can
6637 branch directly to .plt; those in later groups branch
6638 to the last element of the previous group. */
6639 /* ??? It would be better to create multiple copies of
6640 the common resolver stub. */
6641 reachable_plts = ((4096
6642 - plt_info->plt0_entry_size
6643 - (plt_info->symbol_fields.plt + 4))
6644 / plt_info->symbol_entry_size) + 1;
6645 plts_per_4k = (4096 / plt_info->symbol_entry_size);
6646 if (plt_index < reachable_plts)
6647 distance = -(h->plt.offset
6648 + plt_info->symbol_fields.plt);
6649 else
6650 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
6651 * plt_info->symbol_entry_size);
6653 /* Install the 'bra' with this offset. */
6654 bfd_put_16 (output_bfd,
6655 0xa000 | (0x0fff & ((distance - 4) / 2)),
6656 (splt->contents
6657 + h->plt.offset
6658 + plt_info->symbol_fields.plt));
6660 else
6661 install_plt_field (output_bfd, TRUE,
6662 splt->output_section->vma + splt->output_offset,
6663 (splt->contents
6664 + h->plt.offset
6665 + plt_info->symbol_fields.plt));
6668 /* Make got_offset relative to the start of .got.plt. */
6669 #ifdef GOT_BIAS
6670 if (bfd_link_pic (info))
6671 got_offset += GOT_BIAS;
6672 #endif
6673 if (htab->fdpic_p)
6674 got_offset = plt_index * 8;
6676 if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
6677 install_plt_field (output_bfd, FALSE,
6678 plt_index * sizeof (Elf32_External_Rela),
6679 (splt->contents
6680 + h->plt.offset
6681 + plt_info->symbol_fields.reloc_offset));
6683 /* Fill in the entry in the global offset table. */
6684 bfd_put_32 (output_bfd,
6685 (splt->output_section->vma
6686 + splt->output_offset
6687 + h->plt.offset
6688 + plt_info->symbol_resolve_offset),
6689 sgotplt->contents + got_offset);
6690 if (htab->fdpic_p)
6691 bfd_put_32 (output_bfd,
6692 sh_elf_osec_to_segment (output_bfd, splt->output_section),
6693 sgotplt->contents + got_offset + 4);
6695 /* Fill in the entry in the .rela.plt section. */
6696 rel.r_offset = (sgotplt->output_section->vma
6697 + sgotplt->output_offset
6698 + got_offset);
6699 if (htab->fdpic_p)
6700 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
6701 else
6702 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6703 rel.r_addend = 0;
6704 #ifdef GOT_BIAS
6705 rel.r_addend = GOT_BIAS;
6706 #endif
6707 loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
6708 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6710 if (htab->vxworks_p && !bfd_link_pic (info))
6712 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6713 Begin by pointing LOC to the first such relocation. */
6714 loc = (htab->srelplt2->contents
6715 + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
6717 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6718 for the PLT entry's pointer to the .got.plt entry. */
6719 rel.r_offset = (splt->output_section->vma
6720 + splt->output_offset
6721 + h->plt.offset
6722 + plt_info->symbol_fields.got_entry);
6723 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6724 rel.r_addend = got_offset;
6725 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6726 loc += sizeof (Elf32_External_Rela);
6728 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6729 the .got.plt entry, which initially points to .plt. */
6730 rel.r_offset = (sgotplt->output_section->vma
6731 + sgotplt->output_offset
6732 + got_offset);
6733 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
6734 rel.r_addend = 0;
6735 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6738 if (!h->def_regular)
6740 /* Mark the symbol as undefined, rather than as defined in
6741 the .plt section. Leave the value alone. */
6742 sym->st_shndx = SHN_UNDEF;
6746 if (h->got.offset != (bfd_vma) -1
6747 && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
6748 && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
6749 && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
6751 asection *sgot;
6752 asection *srelgot;
6753 Elf_Internal_Rela rel;
6754 bfd_byte *loc;
6756 /* This symbol has an entry in the global offset table. Set it
6757 up. */
6759 sgot = htab->root.sgot;
6760 srelgot = htab->root.srelgot;
6761 BFD_ASSERT (sgot != NULL && srelgot != NULL);
6763 rel.r_offset = (sgot->output_section->vma
6764 + sgot->output_offset
6765 + (h->got.offset &~ (bfd_vma) 1));
6767 /* If this is a static link, or it is a -Bsymbolic link and the
6768 symbol is defined locally or was forced to be local because
6769 of a version file, we just want to emit a RELATIVE reloc.
6770 The entry in the global offset table will already have been
6771 initialized in the relocate_section function. */
6772 if (bfd_link_pic (info)
6773 && SYMBOL_REFERENCES_LOCAL (info, h))
6775 if (htab->fdpic_p)
6777 asection *sec = h->root.u.def.section;
6778 int dynindx
6779 = elf_section_data (sec->output_section)->dynindx;
6781 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6782 rel.r_addend = (h->root.u.def.value
6783 + h->root.u.def.section->output_offset);
6785 else
6787 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6788 rel.r_addend = (h->root.u.def.value
6789 + h->root.u.def.section->output_section->vma
6790 + h->root.u.def.section->output_offset);
6793 else
6795 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6796 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6797 rel.r_addend = 0;
6800 loc = srelgot->contents;
6801 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6802 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6805 #ifdef INCLUDE_SHMEDIA
6807 struct elf_sh_link_hash_entry *eh;
6809 eh = (struct elf_sh_link_hash_entry *) h;
6810 if (eh->datalabel_got.offset != (bfd_vma) -1)
6812 asection *sgot;
6813 asection *srelgot;
6814 Elf_Internal_Rela rel;
6815 bfd_byte *loc;
6817 /* This symbol has a datalabel entry in the global offset table.
6818 Set it up. */
6820 sgot = htab->root.sgot;
6821 srelgot = htab->root.srelgot;
6822 BFD_ASSERT (sgot != NULL && srelgot != NULL);
6824 rel.r_offset = (sgot->output_section->vma
6825 + sgot->output_offset
6826 + (eh->datalabel_got.offset &~ (bfd_vma) 1));
6828 /* If this is a static link, or it is a -Bsymbolic link and the
6829 symbol is defined locally or was forced to be local because
6830 of a version file, we just want to emit a RELATIVE reloc.
6831 The entry in the global offset table will already have been
6832 initialized in the relocate_section function. */
6833 if (bfd_link_pic (info)
6834 && SYMBOL_REFERENCES_LOCAL (info, h))
6836 if (htab->fdpic_p)
6838 asection *sec = h->root.u.def.section;
6839 int dynindx
6840 = elf_section_data (sec->output_section)->dynindx;
6842 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6843 rel.r_addend = (h->root.u.def.value
6844 + h->root.u.def.section->output_offset);
6846 else
6848 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6849 rel.r_addend = (h->root.u.def.value
6850 + h->root.u.def.section->output_section->vma
6851 + h->root.u.def.section->output_offset);
6854 else
6856 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
6857 + eh->datalabel_got.offset);
6858 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6859 rel.r_addend = 0;
6862 loc = srelgot->contents;
6863 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6864 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6867 #endif
6869 if (h->needs_copy)
6871 asection *s;
6872 Elf_Internal_Rela rel;
6873 bfd_byte *loc;
6875 /* This symbol needs a copy reloc. Set it up. */
6877 BFD_ASSERT (h->dynindx != -1
6878 && (h->root.type == bfd_link_hash_defined
6879 || h->root.type == bfd_link_hash_defweak));
6881 s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
6882 BFD_ASSERT (s != NULL);
6884 rel.r_offset = (h->root.u.def.value
6885 + h->root.u.def.section->output_section->vma
6886 + h->root.u.def.section->output_offset);
6887 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
6888 rel.r_addend = 0;
6889 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6890 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6893 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
6894 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6895 ".got" section. */
6896 if (h == htab->root.hdynamic
6897 || (!htab->vxworks_p && h == htab->root.hgot))
6898 sym->st_shndx = SHN_ABS;
6900 return TRUE;
6903 /* Finish up the dynamic sections. */
6905 static bfd_boolean
6906 sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6908 struct elf_sh_link_hash_table *htab;
6909 asection *sgotplt;
6910 asection *sdyn;
6912 htab = sh_elf_hash_table (info);
6913 if (htab == NULL)
6914 return FALSE;
6916 sgotplt = htab->root.sgotplt;
6917 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
6919 if (htab->root.dynamic_sections_created)
6921 asection *splt;
6922 Elf32_External_Dyn *dyncon, *dynconend;
6924 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6926 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6927 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6928 for (; dyncon < dynconend; dyncon++)
6930 Elf_Internal_Dyn dyn;
6931 asection *s;
6932 #ifdef INCLUDE_SHMEDIA
6933 const char *name;
6934 #endif
6936 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
6938 switch (dyn.d_tag)
6940 default:
6941 if (htab->vxworks_p
6942 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
6943 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6944 break;
6946 #ifdef INCLUDE_SHMEDIA
6947 case DT_INIT:
6948 name = info->init_function;
6949 goto get_sym;
6951 case DT_FINI:
6952 name = info->fini_function;
6953 get_sym:
6954 if (dyn.d_un.d_val != 0)
6956 struct elf_link_hash_entry *h;
6958 h = elf_link_hash_lookup (&htab->root, name,
6959 FALSE, FALSE, TRUE);
6960 if (h != NULL && (h->other & STO_SH5_ISA32))
6962 dyn.d_un.d_val |= 1;
6963 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6966 break;
6967 #endif
6969 case DT_PLTGOT:
6970 BFD_ASSERT (htab->root.hgot != NULL);
6971 s = htab->root.hgot->root.u.def.section;
6972 dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
6973 + s->output_section->vma + s->output_offset;
6974 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6975 break;
6977 case DT_JMPREL:
6978 s = htab->root.srelplt->output_section;
6979 BFD_ASSERT (s != NULL);
6980 dyn.d_un.d_ptr = s->vma;
6981 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6982 break;
6984 case DT_PLTRELSZ:
6985 s = htab->root.srelplt->output_section;
6986 BFD_ASSERT (s != NULL);
6987 dyn.d_un.d_val = s->size;
6988 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6989 break;
6993 /* Fill in the first entry in the procedure linkage table. */
6994 splt = htab->root.splt;
6995 if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
6997 unsigned int i;
6999 memcpy (splt->contents,
7000 htab->plt_info->plt0_entry,
7001 htab->plt_info->plt0_entry_size);
7002 for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
7003 if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
7004 install_plt_field (output_bfd, FALSE,
7005 (sgotplt->output_section->vma
7006 + sgotplt->output_offset
7007 + (i * 4)),
7008 (splt->contents
7009 + htab->plt_info->plt0_got_fields[i]));
7011 if (htab->vxworks_p)
7013 /* Finalize the .rela.plt.unloaded contents. */
7014 Elf_Internal_Rela rel;
7015 bfd_byte *loc;
7017 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
7018 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
7019 loc = htab->srelplt2->contents;
7020 rel.r_offset = (splt->output_section->vma
7021 + splt->output_offset
7022 + htab->plt_info->plt0_got_fields[2]);
7023 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
7024 rel.r_addend = 8;
7025 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
7026 loc += sizeof (Elf32_External_Rela);
7028 /* Fix up the remaining .rela.plt.unloaded relocations.
7029 They may have the wrong symbol index for _G_O_T_ or
7030 _P_L_T_ depending on the order in which symbols were
7031 output. */
7032 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
7034 /* The PLT entry's pointer to the .got.plt slot. */
7035 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7036 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
7037 R_SH_DIR32);
7038 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7039 loc += sizeof (Elf32_External_Rela);
7041 /* The .got.plt slot's pointer to .plt. */
7042 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7043 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
7044 R_SH_DIR32);
7045 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7046 loc += sizeof (Elf32_External_Rela);
7050 /* UnixWare sets the entsize of .plt to 4, although that doesn't
7051 really seem like the right value. */
7052 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
7056 /* Fill in the first three entries in the global offset table. */
7057 if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
7059 if (sdyn == NULL)
7060 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
7061 else
7062 bfd_put_32 (output_bfd,
7063 sdyn->output_section->vma + sdyn->output_offset,
7064 sgotplt->contents);
7065 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
7066 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
7069 if (sgotplt && sgotplt->size > 0)
7070 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
7072 /* At the very end of the .rofixup section is a pointer to the GOT. */
7073 if (htab->fdpic_p && htab->srofixup != NULL)
7075 struct elf_link_hash_entry *hgot = htab->root.hgot;
7076 bfd_vma got_value = hgot->root.u.def.value
7077 + hgot->root.u.def.section->output_section->vma
7078 + hgot->root.u.def.section->output_offset;
7080 sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
7082 /* Make sure we allocated and generated the same number of fixups. */
7083 BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
7086 if (htab->srelfuncdesc)
7087 BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
7088 == htab->srelfuncdesc->size);
7090 if (htab->root.srelgot)
7091 BFD_ASSERT (htab->root.srelgot->reloc_count * sizeof (Elf32_External_Rela)
7092 == htab->root.srelgot->size);
7094 return TRUE;
7097 static enum elf_reloc_type_class
7098 sh_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
7099 const asection *rel_sec ATTRIBUTE_UNUSED,
7100 const Elf_Internal_Rela *rela)
7102 switch ((int) ELF32_R_TYPE (rela->r_info))
7104 case R_SH_RELATIVE:
7105 return reloc_class_relative;
7106 case R_SH_JMP_SLOT:
7107 return reloc_class_plt;
7108 case R_SH_COPY:
7109 return reloc_class_copy;
7110 default:
7111 return reloc_class_normal;
7115 #if !defined SH_TARGET_ALREADY_DEFINED
7116 /* Support for Linux core dump NOTE sections. */
7118 static bfd_boolean
7119 elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7121 int offset;
7122 unsigned int size;
7124 switch (note->descsz)
7126 default:
7127 return FALSE;
7129 case 168: /* Linux/SH */
7130 /* pr_cursig */
7131 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
7133 /* pr_pid */
7134 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
7136 /* pr_reg */
7137 offset = 72;
7138 size = 92;
7140 break;
7143 /* Make a ".reg/999" section. */
7144 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7145 size, note->descpos + offset);
7148 static bfd_boolean
7149 elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7151 switch (note->descsz)
7153 default:
7154 return FALSE;
7156 case 124: /* Linux/SH elf_prpsinfo */
7157 elf_tdata (abfd)->core->program
7158 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
7159 elf_tdata (abfd)->core->command
7160 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
7163 /* Note that for some reason, a spurious space is tacked
7164 onto the end of the args in some (at least one anyway)
7165 implementations, so strip it off if it exists. */
7168 char *command = elf_tdata (abfd)->core->command;
7169 int n = strlen (command);
7171 if (0 < n && command[n - 1] == ' ')
7172 command[n - 1] = '\0';
7175 return TRUE;
7177 #endif /* not SH_TARGET_ALREADY_DEFINED */
7180 /* Return address for Ith PLT stub in section PLT, for relocation REL
7181 or (bfd_vma) -1 if it should not be included. */
7183 static bfd_vma
7184 sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
7185 const arelent *rel ATTRIBUTE_UNUSED)
7187 const struct elf_sh_plt_info *plt_info;
7189 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
7190 return plt->vma + get_plt_offset (plt_info, i);
7193 /* Decide whether to attempt to turn absptr or lsda encodings in
7194 shared libraries into pcrel within the given input section. */
7196 static bfd_boolean
7197 sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
7198 struct bfd_link_info *info,
7199 asection *eh_frame_section ATTRIBUTE_UNUSED)
7201 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7203 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
7204 if (htab->fdpic_p)
7205 return FALSE;
7207 return TRUE;
7210 /* Adjust the contents of an eh_frame_hdr section before they're output. */
7212 static bfd_byte
7213 sh_elf_encode_eh_address (bfd *abfd,
7214 struct bfd_link_info *info,
7215 asection *osec, bfd_vma offset,
7216 asection *loc_sec, bfd_vma loc_offset,
7217 bfd_vma *encoded)
7219 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
7220 struct elf_link_hash_entry *h;
7222 if (!htab->fdpic_p)
7223 return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
7224 loc_offset, encoded);
7226 h = htab->root.hgot;
7227 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
7229 if (! h || (sh_elf_osec_to_segment (abfd, osec)
7230 == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
7231 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
7232 loc_sec, loc_offset, encoded);
7234 BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
7235 == (sh_elf_osec_to_segment
7236 (abfd, h->root.u.def.section->output_section)));
7238 *encoded = osec->vma + offset
7239 - (h->root.u.def.value
7240 + h->root.u.def.section->output_section->vma
7241 + h->root.u.def.section->output_offset);
7243 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
7246 #if !defined SH_TARGET_ALREADY_DEFINED
7247 #define TARGET_BIG_SYM sh_elf32_vec
7248 #define TARGET_BIG_NAME "elf32-sh"
7249 #define TARGET_LITTLE_SYM sh_elf32_le_vec
7250 #define TARGET_LITTLE_NAME "elf32-shl"
7251 #endif
7253 #define ELF_ARCH bfd_arch_sh
7254 #define ELF_TARGET_ID SH_ELF_DATA
7255 #define ELF_MACHINE_CODE EM_SH
7256 #ifdef __QNXTARGET__
7257 #define ELF_MAXPAGESIZE 0x1000
7258 #else
7259 #define ELF_MAXPAGESIZE 0x80
7260 #endif
7262 #define elf_symbol_leading_char '_'
7264 #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
7265 #define bfd_elf32_bfd_reloc_name_lookup \
7266 sh_elf_reloc_name_lookup
7267 #define elf_info_to_howto sh_elf_info_to_howto
7268 #define bfd_elf32_bfd_relax_section sh_elf_relax_section
7269 #define elf_backend_relocate_section sh_elf_relocate_section
7270 #define bfd_elf32_bfd_get_relocated_section_contents \
7271 sh_elf_get_relocated_section_contents
7272 #define bfd_elf32_mkobject sh_elf_mkobject
7273 #define elf_backend_object_p sh_elf_object_p
7274 #define bfd_elf32_bfd_copy_private_bfd_data \
7275 sh_elf_copy_private_data
7276 #define bfd_elf32_bfd_merge_private_bfd_data \
7277 sh_elf_merge_private_data
7279 #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
7280 #define elf_backend_check_relocs sh_elf_check_relocs
7281 #define elf_backend_copy_indirect_symbol \
7282 sh_elf_copy_indirect_symbol
7283 #define elf_backend_create_dynamic_sections \
7284 sh_elf_create_dynamic_sections
7285 #define bfd_elf32_bfd_link_hash_table_create \
7286 sh_elf_link_hash_table_create
7287 #define elf_backend_adjust_dynamic_symbol \
7288 sh_elf_adjust_dynamic_symbol
7289 #define elf_backend_always_size_sections \
7290 sh_elf_always_size_sections
7291 #define elf_backend_size_dynamic_sections \
7292 sh_elf_size_dynamic_sections
7293 #define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
7294 #define elf_backend_finish_dynamic_symbol \
7295 sh_elf_finish_dynamic_symbol
7296 #define elf_backend_finish_dynamic_sections \
7297 sh_elf_finish_dynamic_sections
7298 #define elf_backend_reloc_type_class sh_elf_reloc_type_class
7299 #define elf_backend_plt_sym_val sh_elf_plt_sym_val
7300 #define elf_backend_can_make_relative_eh_frame \
7301 sh_elf_use_relative_eh_frame
7302 #define elf_backend_can_make_lsda_relative_eh_frame \
7303 sh_elf_use_relative_eh_frame
7304 #define elf_backend_encode_eh_address \
7305 sh_elf_encode_eh_address
7307 #define elf_backend_stack_align 8
7308 #define elf_backend_can_gc_sections 1
7309 #define elf_backend_can_refcount 1
7310 #define elf_backend_want_got_plt 1
7311 #define elf_backend_plt_readonly 1
7312 #define elf_backend_want_plt_sym 0
7313 #define elf_backend_got_header_size 12
7314 #define elf_backend_dtrel_excludes_plt 1
7316 #define elf_backend_linux_prpsinfo32_ugid16 TRUE
7318 #if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
7320 #include "elf32-target.h"
7322 /* NetBSD support. */
7323 #undef TARGET_BIG_SYM
7324 #define TARGET_BIG_SYM sh_elf32_nbsd_vec
7325 #undef TARGET_BIG_NAME
7326 #define TARGET_BIG_NAME "elf32-sh-nbsd"
7327 #undef TARGET_LITTLE_SYM
7328 #define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
7329 #undef TARGET_LITTLE_NAME
7330 #define TARGET_LITTLE_NAME "elf32-shl-nbsd"
7331 #undef ELF_MAXPAGESIZE
7332 #define ELF_MAXPAGESIZE 0x10000
7333 #undef ELF_COMMONPAGESIZE
7334 #undef elf_symbol_leading_char
7335 #define elf_symbol_leading_char 0
7336 #undef elf32_bed
7337 #define elf32_bed elf32_sh_nbsd_bed
7339 #include "elf32-target.h"
7342 /* Linux support. */
7343 #undef TARGET_BIG_SYM
7344 #define TARGET_BIG_SYM sh_elf32_linux_be_vec
7345 #undef TARGET_BIG_NAME
7346 #define TARGET_BIG_NAME "elf32-shbig-linux"
7347 #undef TARGET_LITTLE_SYM
7348 #define TARGET_LITTLE_SYM sh_elf32_linux_vec
7349 #undef TARGET_LITTLE_NAME
7350 #define TARGET_LITTLE_NAME "elf32-sh-linux"
7351 #undef ELF_COMMONPAGESIZE
7352 #define ELF_COMMONPAGESIZE 0x1000
7354 #undef elf_backend_grok_prstatus
7355 #define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
7356 #undef elf_backend_grok_psinfo
7357 #define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
7358 #undef elf32_bed
7359 #define elf32_bed elf32_sh_lin_bed
7361 #include "elf32-target.h"
7364 /* FDPIC support. */
7365 #undef TARGET_BIG_SYM
7366 #define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
7367 #undef TARGET_BIG_NAME
7368 #define TARGET_BIG_NAME "elf32-shbig-fdpic"
7369 #undef TARGET_LITTLE_SYM
7370 #define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
7371 #undef TARGET_LITTLE_NAME
7372 #define TARGET_LITTLE_NAME "elf32-sh-fdpic"
7374 #undef elf32_bed
7375 #define elf32_bed elf32_sh_fd_bed
7377 #include "elf32-target.h"
7379 #undef elf_backend_modify_program_headers
7381 /* VxWorks support. */
7382 #undef TARGET_BIG_SYM
7383 #define TARGET_BIG_SYM sh_elf32_vxworks_vec
7384 #undef TARGET_BIG_NAME
7385 #define TARGET_BIG_NAME "elf32-sh-vxworks"
7386 #undef TARGET_LITTLE_SYM
7387 #define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
7388 #undef TARGET_LITTLE_NAME
7389 #define TARGET_LITTLE_NAME "elf32-shl-vxworks"
7390 #undef elf32_bed
7391 #define elf32_bed elf32_sh_vxworks_bed
7393 #undef elf_backend_want_plt_sym
7394 #define elf_backend_want_plt_sym 1
7395 #undef elf_symbol_leading_char
7396 #define elf_symbol_leading_char '_'
7397 #define elf_backend_want_got_underscore 1
7398 #undef elf_backend_grok_prstatus
7399 #undef elf_backend_grok_psinfo
7400 #undef elf_backend_add_symbol_hook
7401 #define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
7402 #undef elf_backend_link_output_symbol_hook
7403 #define elf_backend_link_output_symbol_hook \
7404 elf_vxworks_link_output_symbol_hook
7405 #undef elf_backend_emit_relocs
7406 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
7407 #undef elf_backend_final_write_processing
7408 #define elf_backend_final_write_processing \
7409 elf_vxworks_final_write_processing
7410 #undef ELF_MAXPAGESIZE
7411 #define ELF_MAXPAGESIZE 0x1000
7412 #undef ELF_COMMONPAGESIZE
7414 #include "elf32-target.h"
7416 #endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */