Revert the value change of DT_ENCODING from 2003-01-17. Luckily (and
[deliverable/binutils-gdb.git] / bfd / elf32-sh.c
CommitLineData
ef230218 1/* Renesas / SuperH SH specific support for 32-bit ELF
22d606e9 2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
ab96bf03 3 2006, 2007 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by Ian Lance Taylor, Cygnus Support.
5
571fe01f 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
571fe01f
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
252b5132 12
571fe01f
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
571fe01f
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
3e110533 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132
RH
21
22#include "bfd.h"
23#include "sysdep.h"
24#include "bfdlink.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
55e6e397 27#include "elf-vxworks.h"
252b5132 28#include "elf/sh.h"
f6f9408f 29#include "libiberty.h"
871ec896 30#include "../opcodes/sh-opc.h"
252b5132
RH
31
32static bfd_reloc_status_type sh_elf_reloc
09fd220b 33 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
252b5132 34static bfd_reloc_status_type sh_elf_ignore_reloc
09fd220b 35 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
b34976b6 36static bfd_boolean sh_elf_relax_delete_bytes
09fd220b 37 (bfd *, asection *, bfd_vma, int);
b34976b6 38static bfd_boolean sh_elf_align_loads
09fd220b 39 (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_boolean *);
85fbca6a 40#ifndef SH64_ELF
b34976b6 41static bfd_boolean sh_elf_swap_insns
09fd220b 42 (bfd *, asection *, void *, bfd_byte *, bfd_vma);
85fbca6a 43#endif
3376eaf5 44static int sh_elf_optimized_tls_reloc
09fd220b 45 (struct bfd_link_info *, int, int);
3376eaf5 46static bfd_vma dtpoff_base
09fd220b 47 (struct bfd_link_info *);
267fb3c1 48static bfd_vma tpoff
09fd220b 49 (struct bfd_link_info *, bfd_vma);
37c644f2
AO
50
51/* The name of the dynamic interpreter. This is put in the .interp
52 section. */
53
54#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
252b5132 55
55e6e397
RS
56#define MINUS_ONE ((bfd_vma) 0 - 1)
57\f
58#define SH_PARTIAL32 TRUE
59#define SH_SRC_MASK32 0xffffffff
60#define SH_ELF_RELOC sh_elf_reloc
38b1a46c
NC
61static reloc_howto_type sh_elf_howto_table[] =
62{
55e6e397
RS
63#include "elf32-sh-relocs.h"
64};
fbca6ad9 65
55e6e397
RS
66#define SH_PARTIAL32 FALSE
67#define SH_SRC_MASK32 0
68#define SH_ELF_RELOC bfd_elf_generic_reloc
69static reloc_howto_type sh_vxworks_howto_table[] =
70{
71#include "elf32-sh-relocs.h"
72};
73\f
74/* Return true if OUTPUT_BFD is a VxWorks object. */
fbca6ad9 75
55e6e397 76static bfd_boolean
527a23b8 77vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
55e6e397 78{
527a23b8 79#if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
55e6e397
RS
80 extern const bfd_target bfd_elf32_shlvxworks_vec;
81 extern const bfd_target bfd_elf32_shvxworks_vec;
fbca6ad9 82
55e6e397
RS
83 return (abfd->xvec == &bfd_elf32_shlvxworks_vec
84 || abfd->xvec == &bfd_elf32_shvxworks_vec);
527a23b8
NC
85#else
86 return FALSE;
87#endif
55e6e397 88}
fbca6ad9 89
55e6e397 90/* Return the howto table for ABFD. */
fbca6ad9 91
55e6e397
RS
92static reloc_howto_type *
93get_howto_table (bfd *abfd)
94{
95 if (vxworks_object_p (abfd))
96 return sh_vxworks_howto_table;
97 return sh_elf_howto_table;
98}
252b5132 99
015551fc 100static bfd_reloc_status_type
09fd220b
KK
101sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
102 asection *input_section, bfd_byte *contents,
103 bfd_vma addr, asection *symbol_section,
104 bfd_vma start, bfd_vma end)
015551fc
JR
105{
106 static bfd_vma last_addr;
00fdaf47 107 static asection *last_symbol_section;
015551fc
JR
108 bfd_byte *start_ptr, *ptr, *last_ptr;
109 int diff, cum_diff;
110 bfd_signed_vma x;
111 int insn;
112
113 /* Sanity check the address. */
07515404 114 if (addr > bfd_get_section_limit (input_bfd, input_section))
015551fc
JR
115 return bfd_reloc_outofrange;
116
117 /* We require the start and end relocations to be processed consecutively -
118 although we allow then to be processed forwards or backwards. */
119 if (! last_addr)
120 {
121 last_addr = addr;
122 last_symbol_section = symbol_section;
123 return bfd_reloc_ok;
124 }
125 if (last_addr != addr)
126 abort ();
127 last_addr = 0;
128
129 if (! symbol_section || last_symbol_section != symbol_section || end < start)
130 return bfd_reloc_outofrange;
131
132 /* Get the symbol_section contents. */
133 if (symbol_section != input_section)
134 {
135 if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
136 contents = elf_section_data (symbol_section)->this_hdr.contents;
137 else
138 {
eea6121a
AM
139 if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
140 &contents))
015551fc 141 {
eea6121a
AM
142 if (contents != NULL)
143 free (contents);
015551fc
JR
144 return bfd_reloc_outofrange;
145 }
146 }
147 }
148#define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
149 start_ptr = contents + start;
150 for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
151 {
152 for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
153 ptr -= 2;
154 ptr += 2;
61ff1804 155 diff = (last_ptr - ptr) >> 1;
015551fc
JR
156 cum_diff += diff & 1;
157 cum_diff += diff;
158 }
159 /* Calculate the start / end values to load into rs / re minus four -
160 so that will cancel out the four we would otherwise have to add to
161 addr to get the value to subtract in order to get relative addressing. */
162 if (cum_diff >= 0)
163 {
164 start -= 4;
165 end = (ptr + cum_diff * 2) - contents;
166 }
167 else
168 {
169 bfd_vma start0 = start - 4;
170
a0fc8ba1 171 while (start0 && IS_PPI (contents + start0))
015551fc
JR
172 start0 -= 2;
173 start0 = start - 2 - ((start - start0) & 2);
174 start = start0 - cum_diff - 2;
175 end = start0;
176 }
177
6cdc0ccc
AM
178 if (contents != NULL
179 && elf_section_data (symbol_section)->this_hdr.contents != contents)
180 free (contents);
015551fc
JR
181
182 insn = bfd_get_16 (input_bfd, contents + addr);
183
184 x = (insn & 0x200 ? end : start) - addr;
185 if (input_section != symbol_section)
186 x += ((symbol_section->output_section->vma + symbol_section->output_offset)
187 - (input_section->output_section->vma
188 + input_section->output_offset));
189 x >>= 1;
190 if (x < -128 || x > 127)
191 return bfd_reloc_overflow;
192
61ff1804 193 x = (insn & ~0xff) | (x & 0xff);
dc810e39 194 bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
015551fc
JR
195
196 return bfd_reloc_ok;
197}
198
199/* This function is used for normal relocs. This used to be like the COFF
252b5132
RH
200 function, and is almost certainly incorrect for other ELF targets. */
201
202static bfd_reloc_status_type
09fd220b
KK
203sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
204 void *data, asection *input_section, bfd *output_bfd,
205 char **error_message ATTRIBUTE_UNUSED)
252b5132
RH
206{
207 unsigned long insn;
208 bfd_vma sym_value;
209 enum elf_sh_reloc_type r_type;
210 bfd_vma addr = reloc_entry->address;
211 bfd_byte *hit_data = addr + (bfd_byte *) data;
212
213 r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
214
215 if (output_bfd != NULL)
216 {
217 /* Partial linking--do nothing. */
218 reloc_entry->address += input_section->output_offset;
219 return bfd_reloc_ok;
220 }
221
222 /* Almost all relocs have to do with relaxing. If any work must be
223 done for them, it has been done in sh_relax_section. */
015551fc 224 if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
252b5132
RH
225 return bfd_reloc_ok;
226
227 if (symbol_in != NULL
228 && bfd_is_und_section (symbol_in->section))
229 return bfd_reloc_undefined;
230
231 if (bfd_is_com_section (symbol_in->section))
435b1e90
KH
232 sym_value = 0;
233 else
252b5132
RH
234 sym_value = (symbol_in->value +
235 symbol_in->section->output_section->vma +
236 symbol_in->section->output_offset);
237
238 switch (r_type)
239 {
240 case R_SH_DIR32:
241 insn = bfd_get_32 (abfd, hit_data);
242 insn += sym_value + reloc_entry->addend;
dc810e39 243 bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
252b5132
RH
244 break;
245 case R_SH_IND12W:
246 insn = bfd_get_16 (abfd, hit_data);
247 sym_value += reloc_entry->addend;
248 sym_value -= (input_section->output_section->vma
249 + input_section->output_offset
250 + addr
251 + 4);
252 sym_value += (insn & 0xfff) << 1;
253 if (insn & 0x800)
254 sym_value -= 0x1000;
255 insn = (insn & 0xf000) | (sym_value & 0xfff);
dc810e39 256 bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
252b5132
RH
257 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
258 return bfd_reloc_overflow;
259 break;
260 default:
261 abort ();
262 break;
263 }
264
265 return bfd_reloc_ok;
266}
267
268/* This function is used for relocs which are only used for relaxing,
269 which the linker should otherwise ignore. */
270
271static bfd_reloc_status_type
09fd220b
KK
272sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
273 asymbol *symbol ATTRIBUTE_UNUSED,
274 void *data ATTRIBUTE_UNUSED, asection *input_section,
275 bfd *output_bfd,
276 char **error_message ATTRIBUTE_UNUSED)
252b5132
RH
277{
278 if (output_bfd != NULL)
279 reloc_entry->address += input_section->output_offset;
280 return bfd_reloc_ok;
281}
282
283/* This structure is used to map BFD reloc codes to SH ELF relocs. */
284
38b1a46c
NC
285struct elf_reloc_map
286{
252b5132
RH
287 bfd_reloc_code_real_type bfd_reloc_val;
288 unsigned char elf_reloc_val;
289};
290
291/* An array mapping BFD reloc codes to SH ELF relocs. */
292
38b1a46c
NC
293static const struct elf_reloc_map sh_reloc_map[] =
294{
252b5132
RH
295 { BFD_RELOC_NONE, R_SH_NONE },
296 { BFD_RELOC_32, R_SH_DIR32 },
d38eb334
DD
297 { BFD_RELOC_16, R_SH_DIR16 },
298 { BFD_RELOC_8, R_SH_DIR8 },
252b5132
RH
299 { BFD_RELOC_CTOR, R_SH_DIR32 },
300 { BFD_RELOC_32_PCREL, R_SH_REL32 },
301 { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
302 { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
303 { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
304 { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
305 { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
306 { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
307 { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
308 { BFD_RELOC_SH_USES, R_SH_USES },
309 { BFD_RELOC_SH_COUNT, R_SH_COUNT },
310 { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
311 { BFD_RELOC_SH_CODE, R_SH_CODE },
312 { BFD_RELOC_SH_DATA, R_SH_DATA },
313 { BFD_RELOC_SH_LABEL, R_SH_LABEL },
314 { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
315 { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
015551fc
JR
316 { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
317 { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
3376eaf5
KK
318 { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
319 { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
320 { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
321 { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
322 { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
323 { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
324 { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
325 { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
37c644f2
AO
326 { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
327 { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
328 { BFD_RELOC_SH_COPY, R_SH_COPY },
329 { BFD_RELOC_SH_GLOB_DAT, R_SH_GLOB_DAT },
330 { BFD_RELOC_SH_JMP_SLOT, R_SH_JMP_SLOT },
331 { BFD_RELOC_SH_RELATIVE, R_SH_RELATIVE },
332 { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
333 { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
fbca6ad9
AO
334 { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
335#ifdef INCLUDE_SHMEDIA
336 { BFD_RELOC_SH_GOT_LOW16, R_SH_GOT_LOW16 },
337 { BFD_RELOC_SH_GOT_MEDLOW16, R_SH_GOT_MEDLOW16 },
338 { BFD_RELOC_SH_GOT_MEDHI16, R_SH_GOT_MEDHI16 },
339 { BFD_RELOC_SH_GOT_HI16, R_SH_GOT_HI16 },
340 { BFD_RELOC_SH_GOTPLT_LOW16, R_SH_GOTPLT_LOW16 },
341 { BFD_RELOC_SH_GOTPLT_MEDLOW16, R_SH_GOTPLT_MEDLOW16 },
342 { BFD_RELOC_SH_GOTPLT_MEDHI16, R_SH_GOTPLT_MEDHI16 },
343 { BFD_RELOC_SH_GOTPLT_HI16, R_SH_GOTPLT_HI16 },
344 { BFD_RELOC_SH_PLT_LOW16, R_SH_PLT_LOW16 },
345 { BFD_RELOC_SH_PLT_MEDLOW16, R_SH_PLT_MEDLOW16 },
346 { BFD_RELOC_SH_PLT_MEDHI16, R_SH_PLT_MEDHI16 },
347 { BFD_RELOC_SH_PLT_HI16, R_SH_PLT_HI16 },
348 { BFD_RELOC_SH_GOTOFF_LOW16, R_SH_GOTOFF_LOW16 },
349 { BFD_RELOC_SH_GOTOFF_MEDLOW16, R_SH_GOTOFF_MEDLOW16 },
350 { BFD_RELOC_SH_GOTOFF_MEDHI16, R_SH_GOTOFF_MEDHI16 },
351 { BFD_RELOC_SH_GOTOFF_HI16, R_SH_GOTOFF_HI16 },
352 { BFD_RELOC_SH_GOTPC_LOW16, R_SH_GOTPC_LOW16 },
353 { BFD_RELOC_SH_GOTPC_MEDLOW16, R_SH_GOTPC_MEDLOW16 },
354 { BFD_RELOC_SH_GOTPC_MEDHI16, R_SH_GOTPC_MEDHI16 },
355 { BFD_RELOC_SH_GOTPC_HI16, R_SH_GOTPC_HI16 },
356 { BFD_RELOC_SH_COPY64, R_SH_COPY64 },
357 { BFD_RELOC_SH_GLOB_DAT64, R_SH_GLOB_DAT64 },
358 { BFD_RELOC_SH_JMP_SLOT64, R_SH_JMP_SLOT64 },
359 { BFD_RELOC_SH_RELATIVE64, R_SH_RELATIVE64 },
360 { BFD_RELOC_SH_GOT10BY4, R_SH_GOT10BY4 },
361 { BFD_RELOC_SH_GOT10BY8, R_SH_GOT10BY8 },
362 { BFD_RELOC_SH_GOTPLT10BY4, R_SH_GOTPLT10BY4 },
363 { BFD_RELOC_SH_GOTPLT10BY8, R_SH_GOTPLT10BY8 },
364 { BFD_RELOC_SH_PT_16, R_SH_PT_16 },
365 { BFD_RELOC_SH_SHMEDIA_CODE, R_SH_SHMEDIA_CODE },
366 { BFD_RELOC_SH_IMMU5, R_SH_DIR5U },
367 { BFD_RELOC_SH_IMMS6, R_SH_DIR6S },
368 { BFD_RELOC_SH_IMMU6, R_SH_DIR6U },
369 { BFD_RELOC_SH_IMMS10, R_SH_DIR10S },
370 { BFD_RELOC_SH_IMMS10BY2, R_SH_DIR10SW },
371 { BFD_RELOC_SH_IMMS10BY4, R_SH_DIR10SL },
372 { BFD_RELOC_SH_IMMS10BY8, R_SH_DIR10SQ },
373 { BFD_RELOC_SH_IMMS16, R_SH_IMMS16 },
374 { BFD_RELOC_SH_IMMU16, R_SH_IMMU16 },
375 { BFD_RELOC_SH_IMM_LOW16, R_SH_IMM_LOW16 },
376 { BFD_RELOC_SH_IMM_LOW16_PCREL, R_SH_IMM_LOW16_PCREL },
377 { BFD_RELOC_SH_IMM_MEDLOW16, R_SH_IMM_MEDLOW16 },
378 { BFD_RELOC_SH_IMM_MEDLOW16_PCREL, R_SH_IMM_MEDLOW16_PCREL },
379 { BFD_RELOC_SH_IMM_MEDHI16, R_SH_IMM_MEDHI16 },
380 { BFD_RELOC_SH_IMM_MEDHI16_PCREL, R_SH_IMM_MEDHI16_PCREL },
381 { BFD_RELOC_SH_IMM_HI16, R_SH_IMM_HI16 },
382 { BFD_RELOC_SH_IMM_HI16_PCREL, R_SH_IMM_HI16_PCREL },
383 { BFD_RELOC_64, R_SH_64 },
384 { BFD_RELOC_64_PCREL, R_SH_64_PCREL },
385#endif /* not INCLUDE_SHMEDIA */
252b5132
RH
386};
387
388/* Given a BFD reloc code, return the howto structure for the
55e6e397 389 corresponding SH ELF reloc. */
252b5132
RH
390
391static reloc_howto_type *
55e6e397 392sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
252b5132
RH
393{
394 unsigned int i;
395
396 for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
397 {
398 if (sh_reloc_map[i].bfd_reloc_val == code)
55e6e397 399 return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
252b5132
RH
400 }
401
402 return NULL;
403}
404
157090f7
AM
405static reloc_howto_type *
406sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
407{
408 unsigned int i;
409
410 if (vxworks_object_p (abfd))
411 {
412 for (i = 0;
413 i < (sizeof (sh_vxworks_howto_table)
414 / sizeof (sh_vxworks_howto_table[0]));
415 i++)
416 if (sh_vxworks_howto_table[i].name != NULL
417 && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
418 return &sh_vxworks_howto_table[i];
419 }
420 else
421 {
422 for (i = 0;
423 i < (sizeof (sh_elf_howto_table)
424 / sizeof (sh_elf_howto_table[0]));
425 i++)
426 if (sh_elf_howto_table[i].name != NULL
427 && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
428 return &sh_elf_howto_table[i];
429 }
430
431 return NULL;
432}
433
252b5132
RH
434/* Given an ELF reloc, fill in the howto field of a relent. */
435
436static void
55e6e397 437sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
252b5132
RH
438{
439 unsigned int r;
440
441 r = ELF32_R_TYPE (dst->r_info);
442
443 BFD_ASSERT (r < (unsigned int) R_SH_max);
444 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC);
06bb75c1 445 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_2 || r > R_SH_LAST_INVALID_RELOC_2);
fbca6ad9
AO
446 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_3 || r > R_SH_LAST_INVALID_RELOC_3);
447 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_4 || r > R_SH_LAST_INVALID_RELOC_4);
3376eaf5 448 BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC_5 || r > R_SH_LAST_INVALID_RELOC_5);
252b5132 449
55e6e397 450 cache_ptr->howto = get_howto_table (abfd) + r;
252b5132
RH
451}
452\f
453/* This function handles relaxing for SH ELF. See the corresponding
454 function in coff-sh.c for a description of what this does. FIXME:
455 There is a lot of duplication here between this code and the COFF
456 specific code. The format of relocs and symbols is wound deeply
457 into this code, but it would still be better if the duplication
458 could be eliminated somehow. Note in particular that although both
459 functions use symbols like R_SH_CODE, those symbols have different
460 values; in coff-sh.c they come from include/coff/sh.h, whereas here
461 they come from enum elf_sh_reloc_type in include/elf/sh.h. */
462
b34976b6 463static bfd_boolean
09fd220b
KK
464sh_elf_relax_section (bfd *abfd, asection *sec,
465 struct bfd_link_info *link_info, bfd_boolean *again)
252b5132
RH
466{
467 Elf_Internal_Shdr *symtab_hdr;
468 Elf_Internal_Rela *internal_relocs;
b34976b6 469 bfd_boolean have_code;
252b5132
RH
470 Elf_Internal_Rela *irel, *irelend;
471 bfd_byte *contents = NULL;
6cdc0ccc 472 Elf_Internal_Sym *isymbuf = NULL;
252b5132 473
b34976b6 474 *again = FALSE;
252b5132 475
1049f94e 476 if (link_info->relocatable
252b5132
RH
477 || (sec->flags & SEC_RELOC) == 0
478 || sec->reloc_count == 0)
b34976b6 479 return TRUE;
252b5132 480
fbca6ad9
AO
481#ifdef INCLUDE_SHMEDIA
482 if (elf_section_data (sec)->this_hdr.sh_flags
483 & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
484 {
b34976b6 485 return TRUE;
fbca6ad9
AO
486 }
487#endif
488
252b5132
RH
489 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
490
45d6a902 491 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 492 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
252b5132
RH
493 link_info->keep_memory));
494 if (internal_relocs == NULL)
495 goto error_return;
252b5132 496
b34976b6 497 have_code = FALSE;
252b5132
RH
498
499 irelend = internal_relocs + sec->reloc_count;
500 for (irel = internal_relocs; irel < irelend; irel++)
501 {
502 bfd_vma laddr, paddr, symval;
503 unsigned short insn;
504 Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
505 bfd_signed_vma foff;
506
507 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
b34976b6 508 have_code = TRUE;
252b5132
RH
509
510 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
511 continue;
512
513 /* Get the section contents. */
514 if (contents == NULL)
515 {
516 if (elf_section_data (sec)->this_hdr.contents != NULL)
517 contents = elf_section_data (sec)->this_hdr.contents;
518 else
519 {
eea6121a 520 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
252b5132
RH
521 goto error_return;
522 }
523 }
524
525 /* The r_addend field of the R_SH_USES reloc will point us to
067653c5
AM
526 the register load. The 4 is because the r_addend field is
527 computed as though it were a jump offset, which are based
528 from 4 bytes after the jump instruction. */
252b5132 529 laddr = irel->r_offset + 4 + irel->r_addend;
eea6121a 530 if (laddr >= sec->size)
252b5132 531 {
d003868e
AM
532 (*_bfd_error_handler) (_("%B: 0x%lx: warning: bad R_SH_USES offset"),
533 abfd,
252b5132
RH
534 (unsigned long) irel->r_offset);
535 continue;
536 }
537 insn = bfd_get_16 (abfd, contents + laddr);
538
539 /* If the instruction is not mov.l NN,rN, we don't know what to
067653c5 540 do. */
252b5132
RH
541 if ((insn & 0xf000) != 0xd000)
542 {
543 ((*_bfd_error_handler)
d003868e
AM
544 (_("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"),
545 abfd, (unsigned long) irel->r_offset, insn));
252b5132
RH
546 continue;
547 }
548
549 /* Get the address from which the register is being loaded. The
99e4ae17
AJ
550 displacement in the mov.l instruction is quadrupled. It is a
551 displacement from four bytes after the movl instruction, but,
552 before adding in the PC address, two least significant bits
553 of the PC are cleared. We assume that the section is aligned
554 on a four byte boundary. */
252b5132
RH
555 paddr = insn & 0xff;
556 paddr *= 4;
dc810e39 557 paddr += (laddr + 4) &~ (bfd_vma) 3;
eea6121a 558 if (paddr >= sec->size)
252b5132
RH
559 {
560 ((*_bfd_error_handler)
d003868e
AM
561 (_("%B: 0x%lx: warning: bad R_SH_USES load offset"),
562 abfd, (unsigned long) irel->r_offset));
252b5132
RH
563 continue;
564 }
565
566 /* Get the reloc for the address from which the register is
067653c5
AM
567 being loaded. This reloc will tell us which function is
568 actually being called. */
252b5132
RH
569 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
570 if (irelfn->r_offset == paddr
571 && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
572 break;
573 if (irelfn >= irelend)
574 {
575 ((*_bfd_error_handler)
d003868e
AM
576 (_("%B: 0x%lx: warning: could not find expected reloc"),
577 abfd, (unsigned long) paddr));
252b5132
RH
578 continue;
579 }
580
581 /* Read this BFD's symbols if we haven't done so already. */
6cdc0ccc 582 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
252b5132 583 {
6cdc0ccc
AM
584 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
585 if (isymbuf == NULL)
586 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
587 symtab_hdr->sh_info, 0,
588 NULL, NULL, NULL);
589 if (isymbuf == NULL)
590 goto error_return;
252b5132
RH
591 }
592
593 /* Get the value of the symbol referred to by the reloc. */
594 if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
595 {
9ad5cbcf 596 /* A local symbol. */
6cdc0ccc 597 Elf_Internal_Sym *isym;
252b5132 598
6cdc0ccc
AM
599 isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
600 if (isym->st_shndx
d426c6b0 601 != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
252b5132
RH
602 {
603 ((*_bfd_error_handler)
d003868e
AM
604 (_("%B: 0x%lx: warning: symbol in unexpected section"),
605 abfd, (unsigned long) paddr));
252b5132
RH
606 continue;
607 }
608
6cdc0ccc 609 symval = (isym->st_value
252b5132
RH
610 + sec->output_section->vma
611 + sec->output_offset);
612 }
613 else
614 {
615 unsigned long indx;
616 struct elf_link_hash_entry *h;
617
618 indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
619 h = elf_sym_hashes (abfd)[indx];
620 BFD_ASSERT (h != NULL);
621 if (h->root.type != bfd_link_hash_defined
622 && h->root.type != bfd_link_hash_defweak)
623 {
624 /* This appears to be a reference to an undefined
067653c5
AM
625 symbol. Just ignore it--it will be caught by the
626 regular reloc processing. */
252b5132
RH
627 continue;
628 }
629
630 symval = (h->root.u.def.value
631 + h->root.u.def.section->output_section->vma
632 + h->root.u.def.section->output_offset);
633 }
634
55e6e397
RS
635 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
636 symval += bfd_get_32 (abfd, contents + paddr);
637 else
638 symval += irelfn->r_addend;
252b5132
RH
639
640 /* See if this function call can be shortened. */
641 foff = (symval
642 - (irel->r_offset
643 + sec->output_section->vma
644 + sec->output_offset
645 + 4));
f725025b
DD
646 /* A branch to an address beyond ours might be increased by an
647 .align that doesn't move when bytes behind us are deleted.
648 So, we add some slop in this calculation to allow for
649 that. */
650 if (foff < -0x1000 || foff >= 0x1000 - 8)
252b5132
RH
651 {
652 /* After all that work, we can't shorten this function call. */
653 continue;
654 }
655
656 /* Shorten the function call. */
657
658 /* For simplicity of coding, we are going to modify the section
659 contents, the section relocs, and the BFD symbol table. We
660 must tell the rest of the code not to free up this
661 information. It would be possible to instead create a table
662 of changes which have to be made, as is done in coff-mips.c;
663 that would be more work, but would require less memory when
664 the linker is run. */
665
666 elf_section_data (sec)->relocs = internal_relocs;
252b5132 667 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 668 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132
RH
669
670 /* Replace the jsr with a bsr. */
671
672 /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
067653c5 673 replace the jsr with a bsr. */
252b5132 674 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
bdfaef52
JR
675 /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
676 here, but that only checks if the symbol is an external symbol,
677 not if the symbol is in a different section. Besides, we need
678 a consistent meaning for the relocation, so we just assume here that
679 the value of the symbol is not available. */
0e71e495
BE
680
681 /* We can't fully resolve this yet, because the external
682 symbol value may be changed by future relaxing. We let
683 the final link phase handle it. */
684 bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
685
bdfaef52 686 irel->r_addend = -4;
252b5132 687
f725025b
DD
688 /* When we calculated the symbol "value" we had an offset in the
689 DIR32's word in memory (we read and add it above). However,
690 the jsr we create does NOT have this offset encoded, so we
691 have to add it to the addend to preserve it. */
692 irel->r_addend += bfd_get_32 (abfd, contents + paddr);
693
252b5132 694 /* See if there is another R_SH_USES reloc referring to the same
067653c5 695 register load. */
252b5132
RH
696 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
697 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
698 && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
699 break;
700 if (irelscan < irelend)
701 {
702 /* Some other function call depends upon this register load,
703 and we have not yet converted that function call.
704 Indeed, we may never be able to convert it. There is
705 nothing else we can do at this point. */
706 continue;
707 }
708
709 /* Look for a R_SH_COUNT reloc on the location where the
067653c5
AM
710 function address is stored. Do this before deleting any
711 bytes, to avoid confusion about the address. */
252b5132
RH
712 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
713 if (irelcount->r_offset == paddr
714 && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
715 break;
716
717 /* Delete the register load. */
718 if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
719 goto error_return;
720
721 /* That will change things, so, just in case it permits some
067653c5
AM
722 other function call to come within range, we should relax
723 again. Note that this is not required, and it may be slow. */
b34976b6 724 *again = TRUE;
252b5132
RH
725
726 /* Now check whether we got a COUNT reloc. */
727 if (irelcount >= irelend)
728 {
729 ((*_bfd_error_handler)
d003868e
AM
730 (_("%B: 0x%lx: warning: could not find expected COUNT reloc"),
731 abfd, (unsigned long) paddr));
252b5132
RH
732 continue;
733 }
734
735 /* The number of uses is stored in the r_addend field. We've
067653c5 736 just deleted one. */
252b5132
RH
737 if (irelcount->r_addend == 0)
738 {
d003868e
AM
739 ((*_bfd_error_handler) (_("%B: 0x%lx: warning: bad count"),
740 abfd,
252b5132
RH
741 (unsigned long) paddr));
742 continue;
743 }
744
745 --irelcount->r_addend;
746
747 /* If there are no more uses, we can delete the address. Reload
067653c5
AM
748 the address from irelfn, in case it was changed by the
749 previous call to sh_elf_relax_delete_bytes. */
252b5132
RH
750 if (irelcount->r_addend == 0)
751 {
752 if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
753 goto error_return;
754 }
755
756 /* We've done all we can with that function call. */
757 }
758
759 /* Look for load and store instructions that we can align on four
760 byte boundaries. */
bdfaef52
JR
761 if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
762 && have_code)
252b5132 763 {
b34976b6 764 bfd_boolean swapped;
252b5132
RH
765
766 /* Get the section contents. */
767 if (contents == NULL)
768 {
769 if (elf_section_data (sec)->this_hdr.contents != NULL)
770 contents = elf_section_data (sec)->this_hdr.contents;
771 else
772 {
eea6121a 773 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
252b5132
RH
774 goto error_return;
775 }
776 }
777
778 if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
779 &swapped))
780 goto error_return;
781
782 if (swapped)
783 {
784 elf_section_data (sec)->relocs = internal_relocs;
252b5132 785 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 786 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132
RH
787 }
788 }
789
6cdc0ccc
AM
790 if (isymbuf != NULL
791 && symtab_hdr->contents != (unsigned char *) isymbuf)
252b5132
RH
792 {
793 if (! link_info->keep_memory)
6cdc0ccc 794 free (isymbuf);
252b5132
RH
795 else
796 {
6cdc0ccc
AM
797 /* Cache the symbols for elf_link_input_bfd. */
798 symtab_hdr->contents = (unsigned char *) isymbuf;
252b5132 799 }
9ad5cbcf
AM
800 }
801
6cdc0ccc
AM
802 if (contents != NULL
803 && elf_section_data (sec)->this_hdr.contents != contents)
252b5132
RH
804 {
805 if (! link_info->keep_memory)
6cdc0ccc
AM
806 free (contents);
807 else
252b5132 808 {
6cdc0ccc
AM
809 /* Cache the section contents for elf_link_input_bfd. */
810 elf_section_data (sec)->this_hdr.contents = contents;
252b5132 811 }
252b5132
RH
812 }
813
6cdc0ccc
AM
814 if (internal_relocs != NULL
815 && elf_section_data (sec)->relocs != internal_relocs)
816 free (internal_relocs);
817
b34976b6 818 return TRUE;
252b5132
RH
819
820 error_return:
6cdc0ccc
AM
821 if (isymbuf != NULL
822 && symtab_hdr->contents != (unsigned char *) isymbuf)
823 free (isymbuf);
824 if (contents != NULL
825 && elf_section_data (sec)->this_hdr.contents != contents)
826 free (contents);
827 if (internal_relocs != NULL
828 && elf_section_data (sec)->relocs != internal_relocs)
829 free (internal_relocs);
9ad5cbcf 830
b34976b6 831 return FALSE;
252b5132
RH
832}
833
834/* Delete some bytes from a section while relaxing. FIXME: There is a
835 lot of duplication between this function and sh_relax_delete_bytes
836 in coff-sh.c. */
837
b34976b6 838static bfd_boolean
09fd220b
KK
839sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
840 int count)
252b5132
RH
841{
842 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 843 unsigned int sec_shndx;
252b5132
RH
844 bfd_byte *contents;
845 Elf_Internal_Rela *irel, *irelend;
846 Elf_Internal_Rela *irelalign;
847 bfd_vma toaddr;
6cdc0ccc 848 Elf_Internal_Sym *isymbuf, *isym, *isymend;
9ad5cbcf
AM
849 struct elf_link_hash_entry **sym_hashes;
850 struct elf_link_hash_entry **end_hashes;
851 unsigned int symcount;
252b5132
RH
852 asection *o;
853
854 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc 855 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9ad5cbcf
AM
856
857 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132
RH
858
859 contents = elf_section_data (sec)->this_hdr.contents;
860
861 /* The deletion must stop at the next ALIGN reloc for an aligment
862 power larger than the number of bytes we are deleting. */
863
864 irelalign = NULL;
eea6121a 865 toaddr = sec->size;
252b5132
RH
866
867 irel = elf_section_data (sec)->relocs;
868 irelend = irel + sec->reloc_count;
869 for (; irel < irelend; irel++)
870 {
871 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
872 && irel->r_offset > addr
873 && count < (1 << irel->r_addend))
874 {
875 irelalign = irel;
876 toaddr = irel->r_offset;
877 break;
878 }
879 }
880
881 /* Actually delete the bytes. */
dc810e39
AM
882 memmove (contents + addr, contents + addr + count,
883 (size_t) (toaddr - addr - count));
252b5132 884 if (irelalign == NULL)
eea6121a 885 sec->size -= count;
252b5132
RH
886 else
887 {
888 int i;
889
890#define NOP_OPCODE (0x0009)
891
892 BFD_ASSERT ((count & 1) == 0);
893 for (i = 0; i < count; i += 2)
dc810e39 894 bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
252b5132
RH
895 }
896
897 /* Adjust all the relocs. */
898 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
899 {
900 bfd_vma nraddr, stop;
901 bfd_vma start = 0;
902 int insn = 0;
252b5132
RH
903 int off, adjust, oinsn;
904 bfd_signed_vma voff = 0;
b34976b6 905 bfd_boolean overflow;
252b5132
RH
906
907 /* Get the new reloc address. */
908 nraddr = irel->r_offset;
909 if ((irel->r_offset > addr
910 && irel->r_offset < toaddr)
911 || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
912 && irel->r_offset == toaddr))
913 nraddr -= count;
914
915 /* See if this reloc was for the bytes we have deleted, in which
916 case we no longer care about it. Don't delete relocs which
917 represent addresses, though. */
918 if (irel->r_offset >= addr
919 && irel->r_offset < addr + count
920 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
921 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
922 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
923 && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
924 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
925 (int) R_SH_NONE);
926
927 /* If this is a PC relative reloc, see if the range it covers
067653c5 928 includes the bytes we have deleted. */
252b5132
RH
929 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
930 {
931 default:
932 break;
933
934 case R_SH_DIR8WPN:
935 case R_SH_IND12W:
936 case R_SH_DIR8WPZ:
937 case R_SH_DIR8WPL:
938 start = irel->r_offset;
939 insn = bfd_get_16 (abfd, contents + nraddr);
940 break;
941 }
942
943 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
944 {
945 default:
946 start = stop = addr;
947 break;
948
949 case R_SH_DIR32:
950 /* If this reloc is against a symbol defined in this
067653c5
AM
951 section, and the symbol will not be adjusted below, we
952 must check the addend to see it will put the value in
953 range to be adjusted, and hence must be changed. */
252b5132
RH
954 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
955 {
6cdc0ccc
AM
956 isym = isymbuf + ELF32_R_SYM (irel->r_info);
957 if (isym->st_shndx == sec_shndx
958 && (isym->st_value <= addr
959 || isym->st_value >= toaddr))
252b5132
RH
960 {
961 bfd_vma val;
962
55e6e397
RS
963 if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
964 {
965 val = bfd_get_32 (abfd, contents + nraddr);
966 val += isym->st_value;
967 if (val > addr && val < toaddr)
968 bfd_put_32 (abfd, val - count, contents + nraddr);
969 }
970 else
971 {
972 val = isym->st_value + irel->r_addend;
973 if (val > addr && val < toaddr)
974 irel->r_addend -= count;
975 }
252b5132
RH
976 }
977 }
978 start = stop = addr;
979 break;
980
981 case R_SH_DIR8WPN:
982 off = insn & 0xff;
983 if (off & 0x80)
984 off -= 0x100;
985 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
986 break;
987
988 case R_SH_IND12W:
bdfaef52
JR
989 off = insn & 0xfff;
990 if (! off)
991 {
992 /* This has been made by previous relaxation. Since the
993 relocation will be against an external symbol, the
994 final relocation will just do the right thing. */
995 start = stop = addr;
996 }
252b5132
RH
997 else
998 {
252b5132
RH
999 if (off & 0x800)
1000 off -= 0x1000;
1001 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
bdfaef52
JR
1002
1003 /* The addend will be against the section symbol, thus
1004 for adjusting the addend, the relevant start is the
1005 start of the section.
4cc11e76 1006 N.B. If we want to abandon in-place changes here and
bdfaef52
JR
1007 test directly using symbol + addend, we have to take into
1008 account that the addend has already been adjusted by -4. */
1009 if (stop > addr && stop < toaddr)
1010 irel->r_addend -= count;
252b5132
RH
1011 }
1012 break;
1013
1014 case R_SH_DIR8WPZ:
1015 off = insn & 0xff;
1016 stop = start + 4 + off * 2;
1017 break;
1018
1019 case R_SH_DIR8WPL:
1020 off = insn & 0xff;
435b1e90 1021 stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
252b5132
RH
1022 break;
1023
1024 case R_SH_SWITCH8:
1025 case R_SH_SWITCH16:
1026 case R_SH_SWITCH32:
1027 /* These relocs types represent
1028 .word L2-L1
06e1ba78 1029 The r_addend field holds the difference between the reloc
252b5132
RH
1030 address and L1. That is the start of the reloc, and
1031 adding in the contents gives us the top. We must adjust
06e1ba78
JR
1032 both the r_offset field and the section contents.
1033 N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1034 and the elf bfd r_offset is called r_vaddr. */
252b5132 1035
06e1ba78
JR
1036 stop = irel->r_offset;
1037 start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
252b5132
RH
1038
1039 if (start > addr
1040 && start < toaddr
1041 && (stop <= addr || stop >= toaddr))
1042 irel->r_addend += count;
1043 else if (stop > addr
1044 && stop < toaddr
1045 && (start <= addr || start >= toaddr))
1046 irel->r_addend -= count;
1047
252b5132
RH
1048 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1049 voff = bfd_get_signed_16 (abfd, contents + nraddr);
1050 else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1051 voff = bfd_get_8 (abfd, contents + nraddr);
1052 else
1053 voff = bfd_get_signed_32 (abfd, contents + nraddr);
1054 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1055
1056 break;
1057
1058 case R_SH_USES:
1059 start = irel->r_offset;
1060 stop = (bfd_vma) ((bfd_signed_vma) start
1061 + (long) irel->r_addend
1062 + 4);
1063 break;
1064 }
1065
1066 if (start > addr
1067 && start < toaddr
1068 && (stop <= addr || stop >= toaddr))
1069 adjust = count;
1070 else if (stop > addr
1071 && stop < toaddr
1072 && (start <= addr || start >= toaddr))
1073 adjust = - count;
1074 else
1075 adjust = 0;
1076
1077 if (adjust != 0)
1078 {
1079 oinsn = insn;
b34976b6 1080 overflow = FALSE;
252b5132
RH
1081 switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1082 {
1083 default:
1084 abort ();
1085 break;
1086
1087 case R_SH_DIR8WPN:
1088 case R_SH_DIR8WPZ:
1089 insn += adjust / 2;
1090 if ((oinsn & 0xff00) != (insn & 0xff00))
b34976b6 1091 overflow = TRUE;
dc810e39 1092 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
1093 break;
1094
1095 case R_SH_IND12W:
1096 insn += adjust / 2;
1097 if ((oinsn & 0xf000) != (insn & 0xf000))
b34976b6 1098 overflow = TRUE;
dc810e39 1099 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
1100 break;
1101
1102 case R_SH_DIR8WPL:
1103 BFD_ASSERT (adjust == count || count >= 4);
1104 if (count >= 4)
1105 insn += adjust / 4;
1106 else
1107 {
1108 if ((irel->r_offset & 3) == 0)
1109 ++insn;
1110 }
1111 if ((oinsn & 0xff00) != (insn & 0xff00))
b34976b6 1112 overflow = TRUE;
dc810e39 1113 bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
252b5132
RH
1114 break;
1115
851cde10
JR
1116 case R_SH_SWITCH8:
1117 voff += adjust;
1118 if (voff < 0 || voff >= 0xff)
b34976b6 1119 overflow = TRUE;
851cde10
JR
1120 bfd_put_8 (abfd, voff, contents + nraddr);
1121 break;
1122
252b5132
RH
1123 case R_SH_SWITCH16:
1124 voff += adjust;
1125 if (voff < - 0x8000 || voff >= 0x8000)
b34976b6 1126 overflow = TRUE;
dc810e39 1127 bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
252b5132
RH
1128 break;
1129
1130 case R_SH_SWITCH32:
1131 voff += adjust;
dc810e39 1132 bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
252b5132
RH
1133 break;
1134
1135 case R_SH_USES:
1136 irel->r_addend += adjust;
1137 break;
1138 }
1139
1140 if (overflow)
1141 {
1142 ((*_bfd_error_handler)
d003868e
AM
1143 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1144 abfd, (unsigned long) irel->r_offset));
252b5132 1145 bfd_set_error (bfd_error_bad_value);
b34976b6 1146 return FALSE;
252b5132
RH
1147 }
1148 }
1149
1150 irel->r_offset = nraddr;
1151 }
1152
1153 /* Look through all the other sections. If there contain any IMM32
1154 relocs against internal symbols which we are not going to adjust
1155 below, we may need to adjust the addends. */
1156 for (o = abfd->sections; o != NULL; o = o->next)
1157 {
1158 Elf_Internal_Rela *internal_relocs;
1159 Elf_Internal_Rela *irelscan, *irelscanend;
1160 bfd_byte *ocontents;
1161
1162 if (o == sec
1163 || (o->flags & SEC_RELOC) == 0
1164 || o->reloc_count == 0)
1165 continue;
1166
1167 /* We always cache the relocs. Perhaps, if info->keep_memory is
b34976b6 1168 FALSE, we should free them, if we are permitted to, when we
067653c5 1169 leave sh_coff_relax_section. */
45d6a902 1170 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 1171 (abfd, o, NULL, (Elf_Internal_Rela *) NULL, TRUE));
252b5132 1172 if (internal_relocs == NULL)
b34976b6 1173 return FALSE;
252b5132
RH
1174
1175 ocontents = NULL;
1176 irelscanend = internal_relocs + o->reloc_count;
1177 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1178 {
084aa3aa
JR
1179 /* Dwarf line numbers use R_SH_SWITCH32 relocs. */
1180 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1181 {
1182 bfd_vma start, stop;
1183 bfd_signed_vma voff;
1184
1185 if (ocontents == NULL)
1186 {
1187 if (elf_section_data (o)->this_hdr.contents != NULL)
1188 ocontents = elf_section_data (o)->this_hdr.contents;
1189 else
1190 {
1191 /* We always cache the section contents.
b34976b6 1192 Perhaps, if info->keep_memory is FALSE, we
067653c5
AM
1193 should free them, if we are permitted to,
1194 when we leave sh_coff_relax_section. */
eea6121a
AM
1195 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1196 {
1197 if (ocontents != NULL)
1198 free (ocontents);
1199 return FALSE;
1200 }
1201
084aa3aa
JR
1202 elf_section_data (o)->this_hdr.contents = ocontents;
1203 }
1204 }
1205
1206 stop = irelscan->r_offset;
1207 start
1208 = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1209
1210 /* STOP is in a different section, so it won't change. */
1211 if (start > addr && start < toaddr)
1212 irelscan->r_addend += count;
1213
1214 voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1215 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1216
1217 if (start > addr
1218 && start < toaddr
1219 && (stop <= addr || stop >= toaddr))
dc810e39 1220 bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
084aa3aa
JR
1221 ocontents + irelscan->r_offset);
1222 else if (stop > addr
1223 && stop < toaddr
1224 && (start <= addr || start >= toaddr))
dc810e39 1225 bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
084aa3aa
JR
1226 ocontents + irelscan->r_offset);
1227 }
1228
252b5132
RH
1229 if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1230 continue;
1231
1232 if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1233 continue;
1234
252b5132 1235
6cdc0ccc
AM
1236 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1237 if (isym->st_shndx == sec_shndx
1238 && (isym->st_value <= addr
1239 || isym->st_value >= toaddr))
252b5132
RH
1240 {
1241 bfd_vma val;
1242
1243 if (ocontents == NULL)
1244 {
1245 if (elf_section_data (o)->this_hdr.contents != NULL)
1246 ocontents = elf_section_data (o)->this_hdr.contents;
1247 else
1248 {
1249 /* We always cache the section contents.
b34976b6 1250 Perhaps, if info->keep_memory is FALSE, we
067653c5
AM
1251 should free them, if we are permitted to,
1252 when we leave sh_coff_relax_section. */
eea6121a
AM
1253 if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1254 {
1255 if (ocontents != NULL)
1256 free (ocontents);
1257 return FALSE;
1258 }
1259
252b5132
RH
1260 elf_section_data (o)->this_hdr.contents = ocontents;
1261 }
1262 }
1263
1264 val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
6cdc0ccc 1265 val += isym->st_value;
252b5132
RH
1266 if (val > addr && val < toaddr)
1267 bfd_put_32 (abfd, val - count,
1268 ocontents + irelscan->r_offset);
1269 }
1270 }
1271 }
1272
1273 /* Adjust the local symbols defined in this section. */
6cdc0ccc
AM
1274 isymend = isymbuf + symtab_hdr->sh_info;
1275 for (isym = isymbuf; isym < isymend; isym++)
252b5132 1276 {
6cdc0ccc
AM
1277 if (isym->st_shndx == sec_shndx
1278 && isym->st_value > addr
1279 && isym->st_value < toaddr)
1280 isym->st_value -= count;
252b5132
RH
1281 }
1282
1283 /* Now adjust the global symbols defined in this section. */
9ad5cbcf
AM
1284 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1285 - symtab_hdr->sh_info);
1286 sym_hashes = elf_sym_hashes (abfd);
1287 end_hashes = sym_hashes + symcount;
1288 for (; sym_hashes < end_hashes; sym_hashes++)
252b5132 1289 {
9ad5cbcf
AM
1290 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1291 if ((sym_hash->root.type == bfd_link_hash_defined
1292 || sym_hash->root.type == bfd_link_hash_defweak)
1293 && sym_hash->root.u.def.section == sec
1294 && sym_hash->root.u.def.value > addr
1295 && sym_hash->root.u.def.value < toaddr)
252b5132 1296 {
9ad5cbcf 1297 sym_hash->root.u.def.value -= count;
252b5132
RH
1298 }
1299 }
1300
1301 /* See if we can move the ALIGN reloc forward. We have adjusted
1302 r_offset for it already. */
1303 if (irelalign != NULL)
1304 {
1305 bfd_vma alignto, alignaddr;
1306
1307 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1308 alignaddr = BFD_ALIGN (irelalign->r_offset,
1309 1 << irelalign->r_addend);
1310 if (alignto != alignaddr)
1311 {
1312 /* Tail recursion. */
1313 return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
dc810e39 1314 (int) (alignto - alignaddr));
252b5132
RH
1315 }
1316 }
1317
b34976b6 1318 return TRUE;
252b5132
RH
1319}
1320
1321/* Look for loads and stores which we can align to four byte
1322 boundaries. This is like sh_align_loads in coff-sh.c. */
1323
b34976b6 1324static bfd_boolean
09fd220b
KK
1325sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1326 Elf_Internal_Rela *internal_relocs,
1327 bfd_byte *contents ATTRIBUTE_UNUSED,
1328 bfd_boolean *pswapped)
252b5132
RH
1329{
1330 Elf_Internal_Rela *irel, *irelend;
1331 bfd_vma *labels = NULL;
1332 bfd_vma *label, *label_end;
dc810e39 1333 bfd_size_type amt;
252b5132 1334
b34976b6 1335 *pswapped = FALSE;
252b5132
RH
1336
1337 irelend = internal_relocs + sec->reloc_count;
1338
1339 /* Get all the addresses with labels on them. */
dc810e39
AM
1340 amt = sec->reloc_count;
1341 amt *= sizeof (bfd_vma);
1342 labels = (bfd_vma *) bfd_malloc (amt);
252b5132
RH
1343 if (labels == NULL)
1344 goto error_return;
1345 label_end = labels;
1346 for (irel = internal_relocs; irel < irelend; irel++)
1347 {
1348 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1349 {
1350 *label_end = irel->r_offset;
1351 ++label_end;
1352 }
1353 }
1354
1355 /* Note that the assembler currently always outputs relocs in
1356 address order. If that ever changes, this code will need to sort
1357 the label values and the relocs. */
1358
1359 label = labels;
1360
1361 for (irel = internal_relocs; irel < irelend; irel++)
1362 {
1363 bfd_vma start, stop;
1364
1365 if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1366 continue;
1367
1368 start = irel->r_offset;
1369
1370 for (irel++; irel < irelend; irel++)
1371 if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1372 break;
1373 if (irel < irelend)
1374 stop = irel->r_offset;
1375 else
eea6121a 1376 stop = sec->size;
252b5132
RH
1377
1378 if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
09fd220b 1379 internal_relocs, &label,
252b5132
RH
1380 label_end, start, stop, pswapped))
1381 goto error_return;
1382 }
1383
1384 free (labels);
1385
b34976b6 1386 return TRUE;
252b5132
RH
1387
1388 error_return:
1389 if (labels != NULL)
1390 free (labels);
b34976b6 1391 return FALSE;
252b5132
RH
1392}
1393
85fbca6a 1394#ifndef SH64_ELF
252b5132
RH
1395/* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */
1396
b34976b6 1397static bfd_boolean
09fd220b
KK
1398sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1399 bfd_byte *contents, bfd_vma addr)
252b5132
RH
1400{
1401 Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1402 unsigned short i1, i2;
1403 Elf_Internal_Rela *irel, *irelend;
1404
1405 /* Swap the instructions themselves. */
1406 i1 = bfd_get_16 (abfd, contents + addr);
1407 i2 = bfd_get_16 (abfd, contents + addr + 2);
dc810e39
AM
1408 bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1409 bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
252b5132
RH
1410
1411 /* Adjust all reloc addresses. */
1412 irelend = internal_relocs + sec->reloc_count;
1413 for (irel = internal_relocs; irel < irelend; irel++)
1414 {
1415 enum elf_sh_reloc_type type;
1416 int add;
1417
1418 /* There are a few special types of relocs that we don't want to
067653c5
AM
1419 adjust. These relocs do not apply to the instruction itself,
1420 but are only associated with the address. */
252b5132
RH
1421 type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1422 if (type == R_SH_ALIGN
1423 || type == R_SH_CODE
1424 || type == R_SH_DATA
1425 || type == R_SH_LABEL)
1426 continue;
1427
1428 /* If an R_SH_USES reloc points to one of the addresses being
067653c5
AM
1429 swapped, we must adjust it. It would be incorrect to do this
1430 for a jump, though, since we want to execute both
1431 instructions after the jump. (We have avoided swapping
1432 around a label, so the jump will not wind up executing an
1433 instruction it shouldn't). */
252b5132
RH
1434 if (type == R_SH_USES)
1435 {
1436 bfd_vma off;
1437
1438 off = irel->r_offset + 4 + irel->r_addend;
1439 if (off == addr)
1440 irel->r_offset += 2;
1441 else if (off == addr + 2)
1442 irel->r_offset -= 2;
1443 }
1444
1445 if (irel->r_offset == addr)
1446 {
1447 irel->r_offset += 2;
1448 add = -2;
1449 }
1450 else if (irel->r_offset == addr + 2)
1451 {
1452 irel->r_offset -= 2;
1453 add = 2;
1454 }
1455 else
1456 add = 0;
1457
1458 if (add != 0)
1459 {
1460 bfd_byte *loc;
1461 unsigned short insn, oinsn;
b34976b6 1462 bfd_boolean overflow;
252b5132
RH
1463
1464 loc = contents + irel->r_offset;
b34976b6 1465 overflow = FALSE;
252b5132
RH
1466 switch (type)
1467 {
1468 default:
1469 break;
1470
1471 case R_SH_DIR8WPN:
1472 case R_SH_DIR8WPZ:
1473 insn = bfd_get_16 (abfd, loc);
1474 oinsn = insn;
1475 insn += add / 2;
1476 if ((oinsn & 0xff00) != (insn & 0xff00))
b34976b6 1477 overflow = TRUE;
dc810e39 1478 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
1479 break;
1480
1481 case R_SH_IND12W:
1482 insn = bfd_get_16 (abfd, loc);
1483 oinsn = insn;
1484 insn += add / 2;
1485 if ((oinsn & 0xf000) != (insn & 0xf000))
b34976b6 1486 overflow = TRUE;
dc810e39 1487 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
1488 break;
1489
1490 case R_SH_DIR8WPL:
1491 /* This reloc ignores the least significant 3 bits of
067653c5
AM
1492 the program counter before adding in the offset.
1493 This means that if ADDR is at an even address, the
1494 swap will not affect the offset. If ADDR is an at an
1495 odd address, then the instruction will be crossing a
1496 four byte boundary, and must be adjusted. */
252b5132
RH
1497 if ((addr & 3) != 0)
1498 {
1499 insn = bfd_get_16 (abfd, loc);
1500 oinsn = insn;
1501 insn += add / 2;
1502 if ((oinsn & 0xff00) != (insn & 0xff00))
b34976b6 1503 overflow = TRUE;
dc810e39 1504 bfd_put_16 (abfd, (bfd_vma) insn, loc);
252b5132
RH
1505 }
1506
1507 break;
1508 }
1509
1510 if (overflow)
1511 {
1512 ((*_bfd_error_handler)
d003868e
AM
1513 (_("%B: 0x%lx: fatal: reloc overflow while relaxing"),
1514 abfd, (unsigned long) irel->r_offset));
252b5132 1515 bfd_set_error (bfd_error_bad_value);
b34976b6 1516 return FALSE;
252b5132
RH
1517 }
1518 }
1519 }
1520
b34976b6 1521 return TRUE;
252b5132 1522}
85fbca6a 1523#endif /* defined SH64_ELF */
252b5132 1524\f
55e6e397
RS
1525/* Describes one of the various PLT styles. */
1526
1527struct elf_sh_plt_info
1528{
1529 /* The template for the first PLT entry, or NULL if there is no special
1530 first entry. */
1531 const bfd_byte *plt0_entry;
1532
1533 /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL. */
1534 bfd_vma plt0_entry_size;
1535
1536 /* Index I is the offset into PLT0_ENTRY of a pointer to
1537 _GLOBAL_OFFSET_TABLE_ + I * 4. The value is MINUS_ONE
1538 if there is no such pointer. */
1539 bfd_vma plt0_got_fields[3];
1540
1541 /* The template for a symbol's PLT entry. */
1542 const bfd_byte *symbol_entry;
1543
1544 /* The size of SYMBOL_ENTRY in bytes. */
1545 bfd_vma symbol_entry_size;
1546
1547 /* Byte offsets of fields in SYMBOL_ENTRY. Not all fields are used
1548 on all targets. The comments by each member indicate the value
1549 that the field must hold. */
1550 struct {
1551 bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1552 bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1553 bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1554 } symbol_fields;
1555
1556 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
1557 bfd_vma symbol_resolve_offset;
1558};
1559
fbca6ad9
AO
1560#ifdef INCLUDE_SHMEDIA
1561
1562/* The size in bytes of an entry in the procedure linkage table. */
1563
55e6e397 1564#define ELF_PLT_ENTRY_SIZE 64
fbca6ad9
AO
1565
1566/* First entry in an absolute procedure linkage table look like this. */
1567
55e6e397 1568static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1569{
1570 0xcc, 0x00, 0x01, 0x10, /* movi .got.plt >> 16, r17 */
1571 0xc8, 0x00, 0x01, 0x10, /* shori .got.plt & 65535, r17 */
1572 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
c8614e8e 1573 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
fbca6ad9
AO
1574 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1575 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1576 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1577 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1578 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1579 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1580 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1581 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1582 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1583 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1584 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1585 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1586};
1587
55e6e397 1588static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1589{
1590 0x10, 0x01, 0x00, 0xcc, /* movi .got.plt >> 16, r17 */
1591 0x10, 0x01, 0x00, 0xc8, /* shori .got.plt & 65535, r17 */
1592 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
c8614e8e 1593 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
fbca6ad9
AO
1594 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1595 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1596 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1597 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1598 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1599 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1600 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1601 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1602 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1603 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1604 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1605 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1606};
1607
1608/* Sebsequent entries in an absolute procedure linkage table look like
1609 this. */
1610
55e6e397 1611static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1612{
1613 0xcc, 0x00, 0x01, 0x90, /* movi nameN-in-GOT >> 16, r25 */
1614 0xc8, 0x00, 0x01, 0x90, /* shori nameN-in-GOT & 65535, r25 */
1615 0x89, 0x90, 0x01, 0x90, /* ld.l r25, 0, r25 */
1616 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1617 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1618 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1619 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1620 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1621 0xcc, 0x00, 0x01, 0x90, /* movi .PLT0 >> 16, r25 */
1622 0xc8, 0x00, 0x01, 0x90, /* shori .PLT0 & 65535, r25 */
1623 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1624 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1625 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1626 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1627 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1628 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1629};
1630
55e6e397 1631static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1632{
1633 0x90, 0x01, 0x00, 0xcc, /* movi nameN-in-GOT >> 16, r25 */
1634 0x90, 0x01, 0x00, 0xc8, /* shori nameN-in-GOT & 65535, r25 */
1635 0x90, 0x01, 0x90, 0x89, /* ld.l r25, 0, r25 */
1636 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1637 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1638 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1639 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1640 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1641 0x90, 0x01, 0x00, 0xcc, /* movi .PLT0 >> 16, r25 */
1642 0x90, 0x01, 0x00, 0xc8, /* shori .PLT0 & 65535, r25 */
1643 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1644 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1645 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1646 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1647 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1648 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1649};
1650
1651/* Entries in a PIC procedure linkage table look like this. */
1652
55e6e397 1653static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1654{
1655 0xcc, 0x00, 0x01, 0x90, /* movi nameN@GOT >> 16, r25 */
1656 0xc8, 0x00, 0x01, 0x90, /* shori nameN@GOT & 65535, r25 */
1657 0x40, 0xc2, 0x65, 0x90, /* ldx.l r12, r25, r25 */
1658 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1659 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1660 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1661 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1662 0x6f, 0xf0, 0xff, 0xf0, /* nop */
1663 0xce, 0x00, 0x01, 0x10, /* movi -GOT_BIAS, r17 */
0a4ef3f4 1664 0x00, 0xc8, 0x45, 0x10, /* add.l r12, r17, r17 */
fbca6ad9
AO
1665 0x89, 0x10, 0x09, 0x90, /* ld.l r17, 8, r25 */
1666 0x6b, 0xf1, 0x66, 0x00, /* ptabs r25, tr0 */
1667 0x89, 0x10, 0x05, 0x10, /* ld.l r17, 4, r17 */
1668 0xcc, 0x00, 0x01, 0x50, /* movi reloc-offset >> 16, r21 */
1669 0xc8, 0x00, 0x01, 0x50, /* shori reloc-offset & 65535, r21 */
1670 0x44, 0x01, 0xff, 0xf0, /* blink tr0, r63 */
1671};
1672
55e6e397 1673static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
fbca6ad9
AO
1674{
1675 0x90, 0x01, 0x00, 0xcc, /* movi nameN@GOT >> 16, r25 */
1676 0x90, 0x01, 0x00, 0xc8, /* shori nameN@GOT & 65535, r25 */
1677 0x90, 0x65, 0xc2, 0x40, /* ldx.l r12, r25, r25 */
1678 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1679 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1680 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1681 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1682 0xf0, 0xff, 0xf0, 0x6f, /* nop */
1683 0x10, 0x01, 0x00, 0xce, /* movi -GOT_BIAS, r17 */
0a4ef3f4 1684 0x10, 0x45, 0xc8, 0x00, /* add.l r12, r17, r17 */
fbca6ad9
AO
1685 0x90, 0x09, 0x10, 0x89, /* ld.l r17, 8, r25 */
1686 0x00, 0x66, 0xf1, 0x6b, /* ptabs r25, tr0 */
1687 0x10, 0x05, 0x10, 0x89, /* ld.l r17, 4, r17 */
1688 0x50, 0x01, 0x00, 0xcc, /* movi reloc-offset >> 16, r21 */
1689 0x50, 0x01, 0x00, 0xc8, /* shori reloc-offset & 65535, r21 */
1690 0xf0, 0xff, 0x01, 0x44, /* blink tr0, r63 */
1691};
1692
55e6e397
RS
1693static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1694 {
1695 {
1696 /* Big-endian non-PIC. */
1697 elf_sh_plt0_entry_be,
1698 ELF_PLT_ENTRY_SIZE,
1699 { 0, MINUS_ONE, MINUS_ONE },
1700 elf_sh_plt_entry_be,
1701 ELF_PLT_ENTRY_SIZE,
1702 { 0, 32, 48 },
1703 33 /* includes ISA encoding */
1704 },
1705 {
1706 /* Little-endian non-PIC. */
1707 elf_sh_plt0_entry_le,
1708 ELF_PLT_ENTRY_SIZE,
1709 { 0, MINUS_ONE, MINUS_ONE },
1710 elf_sh_plt_entry_le,
1711 ELF_PLT_ENTRY_SIZE,
1712 { 0, 32, 48 },
1713 33 /* includes ISA encoding */
1714 },
1715 },
1716 {
1717 {
1718 /* Big-endian PIC. */
1719 elf_sh_plt0_entry_be,
1720 ELF_PLT_ENTRY_SIZE,
1721 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1722 elf_sh_pic_plt_entry_be,
1723 ELF_PLT_ENTRY_SIZE,
1724 { 0, MINUS_ONE, 52 },
1725 33 /* includes ISA encoding */
1726 },
1727 {
1728 /* Little-endian PIC. */
1729 elf_sh_plt0_entry_le,
1730 ELF_PLT_ENTRY_SIZE,
1731 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1732 elf_sh_pic_plt_entry_le,
1733 ELF_PLT_ENTRY_SIZE,
1734 { 0, MINUS_ONE, 52 },
1735 33 /* includes ISA encoding */
1736 },
1737 }
1738};
fbca6ad9
AO
1739
1740/* Return offset of the linker in PLT0 entry. */
1741#define elf_sh_plt0_gotplt_offset(info) 0
1742
55e6e397
RS
1743/* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
1744 VALUE is the field's value and CODE_P is true if VALUE refers to code,
1745 not data.
fbca6ad9 1746
55e6e397 1747 On SH64, each 32-bit field is loaded by a movi/shori pair. */
fbca6ad9
AO
1748
1749inline static void
55e6e397
RS
1750install_plt_field (bfd *output_bfd, bfd_boolean code_p,
1751 unsigned long value, bfd_byte *addr)
fbca6ad9 1752{
55e6e397 1753 value |= code_p;
fbca6ad9
AO
1754 bfd_put_32 (output_bfd,
1755 bfd_get_32 (output_bfd, addr)
1756 | ((value >> 6) & 0x3fffc00),
1757 addr);
1758 bfd_put_32 (output_bfd,
1759 bfd_get_32 (output_bfd, addr + 4)
1760 | ((value << 10) & 0x3fffc00),
1761 addr + 4);
1762}
1763
55e6e397
RS
1764/* Return the type of PLT associated with ABFD. PIC_P is true if
1765 the object is position-independent. */
1766
1767static const struct elf_sh_plt_info *
1768get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
1769{
1770 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
1771}
fbca6ad9 1772#else
37c644f2 1773/* The size in bytes of an entry in the procedure linkage table. */
252b5132 1774
55e6e397 1775#define ELF_PLT_ENTRY_SIZE 28
37c644f2
AO
1776
1777/* First entry in an absolute procedure linkage table look like this. */
1778
6c426cf3 1779/* Note - this code has been "optimised" not to use r2. r2 is used by
4cc11e76 1780 GCC to return the address of large structures, so it should not be
6c426cf3
NC
1781 corrupted here. This does mean however, that this PLT does not conform
1782 to the SH PIC ABI. That spec says that r0 contains the type of the PLT
1783 and r2 contains the GOT id. This version stores the GOT id in r0 and
1784 ignores the type. Loaders can easily detect this difference however,
1785 since the type will always be 0 or 8, and the GOT ids will always be
1786 greater than or equal to 12. */
55e6e397 1787static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1788{
1789 0xd0, 0x05, /* mov.l 2f,r0 */
1790 0x60, 0x02, /* mov.l @r0,r0 */
1791 0x2f, 0x06, /* mov.l r0,@-r15 */
1792 0xd0, 0x03, /* mov.l 1f,r0 */
1793 0x60, 0x02, /* mov.l @r0,r0 */
1794 0x40, 0x2b, /* jmp @r0 */
1795 0x60, 0xf6, /* mov.l @r15+,r0 */
1796 0x00, 0x09, /* nop */
1797 0x00, 0x09, /* nop */
1798 0x00, 0x09, /* nop */
1799 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1800 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1801};
1802
55e6e397 1803static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1804{
1805 0x05, 0xd0, /* mov.l 2f,r0 */
1806 0x02, 0x60, /* mov.l @r0,r0 */
1807 0x06, 0x2f, /* mov.l r0,@-r15 */
1808 0x03, 0xd0, /* mov.l 1f,r0 */
1809 0x02, 0x60, /* mov.l @r0,r0 */
1810 0x2b, 0x40, /* jmp @r0 */
1811 0xf6, 0x60, /* mov.l @r15+,r0 */
1812 0x09, 0x00, /* nop */
1813 0x09, 0x00, /* nop */
1814 0x09, 0x00, /* nop */
1815 0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8. */
1816 0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4. */
1817};
1818
1819/* Sebsequent entries in an absolute procedure linkage table look like
1820 this. */
1821
55e6e397 1822static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1823{
1824 0xd0, 0x04, /* mov.l 1f,r0 */
55e6e397 1825 0x60, 0x02, /* mov.l @(r0,r12),r0 */
6c426cf3
NC
1826 0xd1, 0x02, /* mov.l 0f,r1 */
1827 0x40, 0x2b, /* jmp @r0 */
1828 0x60, 0x13, /* mov r1,r0 */
1829 0xd1, 0x03, /* mov.l 2f,r1 */
1830 0x40, 0x2b, /* jmp @r0 */
1831 0x00, 0x09, /* nop */
1832 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1833 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1834 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1835};
1836
55e6e397 1837static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1838{
1839 0x04, 0xd0, /* mov.l 1f,r0 */
1840 0x02, 0x60, /* mov.l @r0,r0 */
1841 0x02, 0xd1, /* mov.l 0f,r1 */
1842 0x2b, 0x40, /* jmp @r0 */
1843 0x13, 0x60, /* mov r1,r0 */
1844 0x03, 0xd1, /* mov.l 2f,r1 */
1845 0x2b, 0x40, /* jmp @r0 */
1846 0x09, 0x00, /* nop */
1847 0, 0, 0, 0, /* 0: replaced with address of .PLT0. */
1848 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1849 0, 0, 0, 0, /* 2: replaced with offset into relocation table. */
1850};
1851
1852/* Entries in a PIC procedure linkage table look like this. */
1853
55e6e397 1854static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1855{
1856 0xd0, 0x04, /* mov.l 1f,r0 */
1857 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1858 0x40, 0x2b, /* jmp @r0 */
1859 0x00, 0x09, /* nop */
1860 0x50, 0xc2, /* mov.l @(8,r12),r0 */
1861 0xd1, 0x03, /* mov.l 2f,r1 */
1862 0x40, 0x2b, /* jmp @r0 */
1863 0x50, 0xc1, /* mov.l @(4,r12),r0 */
1864 0x00, 0x09, /* nop */
1865 0x00, 0x09, /* nop */
1866 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1867 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1868};
1869
55e6e397 1870static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
6c426cf3
NC
1871{
1872 0x04, 0xd0, /* mov.l 1f,r0 */
1873 0xce, 0x00, /* mov.l @(r0,r12),r0 */
1874 0x2b, 0x40, /* jmp @r0 */
1875 0x09, 0x00, /* nop */
1876 0xc2, 0x50, /* mov.l @(8,r12),r0 */
1877 0x03, 0xd1, /* mov.l 2f,r1 */
1878 0x2b, 0x40, /* jmp @r0 */
1879 0xc1, 0x50, /* mov.l @(4,r12),r0 */
1880 0x09, 0x00, /* nop */
1881 0x09, 0x00, /* nop */
1882 0, 0, 0, 0, /* 1: replaced with address of this symbol in .got. */
1883 0, 0, 0, 0 /* 2: replaced with offset into relocation table. */
1884};
1885
55e6e397
RS
1886static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1887 {
1888 {
1889 /* Big-endian non-PIC. */
1890 elf_sh_plt0_entry_be,
1891 ELF_PLT_ENTRY_SIZE,
1892 { MINUS_ONE, 24, 20 },
1893 elf_sh_plt_entry_be,
1894 ELF_PLT_ENTRY_SIZE,
1895 { 20, 16, 24 },
1896 8
1897 },
1898 {
1899 /* Little-endian non-PIC. */
1900 elf_sh_plt0_entry_le,
1901 ELF_PLT_ENTRY_SIZE,
1902 { MINUS_ONE, 24, 20 },
1903 elf_sh_plt_entry_le,
1904 ELF_PLT_ENTRY_SIZE,
1905 { 20, 16, 24 },
1906 8
1907 },
1908 },
1909 {
1910 {
1911 /* Big-endian PIC. */
1912 elf_sh_plt0_entry_be,
1913 ELF_PLT_ENTRY_SIZE,
1914 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1915 elf_sh_pic_plt_entry_be,
1916 ELF_PLT_ENTRY_SIZE,
1917 { 20, MINUS_ONE, 24 },
1918 8
1919 },
1920 {
1921 /* Little-endian PIC. */
1922 elf_sh_plt0_entry_le,
1923 ELF_PLT_ENTRY_SIZE,
1924 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1925 elf_sh_pic_plt_entry_le,
1926 ELF_PLT_ENTRY_SIZE,
1927 { 20, MINUS_ONE, 24 },
1928 8
1929 },
1930 }
1931};
252b5132 1932
55e6e397
RS
1933#define VXWORKS_PLT_HEADER_SIZE 12
1934#define VXWORKS_PLT_ENTRY_SIZE 24
252b5132 1935
55e6e397
RS
1936static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
1937{
1938 0xd1, 0x01, /* mov.l @(8,pc),r1 */
1939 0x61, 0x12, /* mov.l @r1,r1 */
1940 0x41, 0x2b, /* jmp @r1 */
1941 0x00, 0x09, /* nop */
1942 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1943};
252b5132 1944
55e6e397
RS
1945static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
1946{
1947 0x01, 0xd1, /* mov.l @(8,pc),r1 */
1948 0x12, 0x61, /* mov.l @r1,r1 */
1949 0x2b, 0x41, /* jmp @r1 */
1950 0x09, 0x00, /* nop */
1951 0, 0, 0, 0 /* 0: replaced with _GLOBAL_OFFSET_TABLE+8. */
1952};
1953
1954static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1955{
1956 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1957 0x60, 0x02, /* mov.l @r0,r0 */
1958 0x40, 0x2b, /* jmp @r0 */
1959 0x00, 0x09, /* nop */
1960 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1961 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1962 0xa0, 0x00, /* bra PLT (We need to fix the offset.) */
1963 0x00, 0x09, /* nop */
1964 0x00, 0x09, /* nop */
1965 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1966};
1967
1968static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1969{
1970 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1971 0x02, 0x60, /* mov.l @r0,r0 */
1972 0x2b, 0x40, /* jmp @r0 */
1973 0x09, 0x00, /* nop */
1974 0, 0, 0, 0, /* 0: replaced with address of this symbol in .got. */
1975 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1976 0x00, 0xa0, /* bra PLT (We need to fix the offset.) */
1977 0x09, 0x00, /* nop */
1978 0x09, 0x00, /* nop */
1979 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1980};
1981
1982static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1983{
1984 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1985 0x00, 0xce, /* mov.l @(r0,r12),r0 */
1986 0x40, 0x2b, /* jmp @r0 */
1987 0x00, 0x09, /* nop */
1988 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
1989 0xd0, 0x01, /* mov.l @(8,pc),r0 */
1990 0x51, 0xc2, /* mov.l @(8,r12),r1 */
1991 0x41, 0x2b, /* jmp @r1 */
1992 0x00, 0x09, /* nop */
1993 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
1994};
1995
1996static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1997{
1998 0x01, 0xd0, /* mov.l @(8,pc),r0 */
1999 0xce, 0x00, /* mov.l @(r0,r12),r0 */
2000 0x2b, 0x40, /* jmp @r0 */
2001 0x09, 0x00, /* nop */
2002 0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got. */
2003 0x01, 0xd0, /* mov.l @(8,pc),r0 */
2004 0xc2, 0x51, /* mov.l @(8,r12),r1 */
2005 0x2b, 0x41, /* jmp @r1 */
2006 0x09, 0x00, /* nop */
2007 0, 0, 0, 0, /* 1: replaced with offset into relocation table. */
2008};
2009
2010static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
2011 {
2012 {
2013 /* Big-endian non-PIC. */
2014 vxworks_sh_plt0_entry_be,
2015 VXWORKS_PLT_HEADER_SIZE,
2016 { MINUS_ONE, MINUS_ONE, 8 },
2017 vxworks_sh_plt_entry_be,
2018 VXWORKS_PLT_ENTRY_SIZE,
2019 { 8, 14, 20 },
2020 12
2021 },
2022 {
2023 /* Little-endian non-PIC. */
2024 vxworks_sh_plt0_entry_le,
2025 VXWORKS_PLT_HEADER_SIZE,
2026 { MINUS_ONE, MINUS_ONE, 8 },
2027 vxworks_sh_plt_entry_le,
2028 VXWORKS_PLT_ENTRY_SIZE,
2029 { 8, 14, 20 },
2030 12
2031 },
2032 },
2033 {
2034 {
2035 /* Big-endian PIC. */
2036 NULL,
2037 0,
2038 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2039 vxworks_sh_pic_plt_entry_be,
2040 VXWORKS_PLT_ENTRY_SIZE,
2041 { 8, MINUS_ONE, 20 },
2042 12
2043 },
2044 {
2045 /* Little-endian PIC. */
2046 NULL,
2047 0,
2048 { MINUS_ONE, MINUS_ONE, MINUS_ONE },
2049 vxworks_sh_pic_plt_entry_le,
2050 VXWORKS_PLT_ENTRY_SIZE,
2051 { 8, MINUS_ONE, 20 },
2052 12
2053 },
2054 }
2055};
9bdafcce 2056
55e6e397
RS
2057/* Return the type of PLT associated with ABFD. PIC_P is true if
2058 the object is position-independent. */
9bdafcce 2059
55e6e397
RS
2060static const struct elf_sh_plt_info *
2061get_plt_info (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean pic_p)
2062{
2063 if (vxworks_object_p (abfd))
2064 return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2065 return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2066}
9bdafcce 2067
55e6e397
RS
2068/* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2069 VALUE is the field's value and CODE_P is true if VALUE refers to code,
2070 not data. */
9bdafcce 2071
55e6e397
RS
2072inline static void
2073install_plt_field (bfd *output_bfd, bfd_boolean code_p ATTRIBUTE_UNUSED,
2074 unsigned long value, bfd_byte *addr)
2075{
2076 bfd_put_32 (output_bfd, value, addr);
2077}
fbca6ad9 2078#endif
015551fc 2079
55e6e397
RS
2080/* Return the index of the PLT entry at byte offset OFFSET. */
2081
2082static bfd_vma
2083get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2084{
2085 return (offset - info->plt0_entry_size) / info->symbol_entry_size;
2086}
2087
2088/* Do the inverse operation. */
2089
2090static bfd_vma
2091get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma index)
2092{
2093 return info->plt0_entry_size + (index * info->symbol_entry_size);
2094}
2095
37c644f2 2096/* The sh linker needs to keep track of the number of relocs that it
067653c5
AM
2097 decides to copy as dynamic relocs in check_relocs for each symbol.
2098 This is so that it can later discard them if they are found to be
2099 unnecessary. We store the information in a field extending the
2100 regular ELF linker hash table. */
252b5132 2101
067653c5 2102struct elf_sh_dyn_relocs
38b1a46c 2103{
067653c5
AM
2104 struct elf_sh_dyn_relocs *next;
2105
2106 /* The input section of the reloc. */
2107 asection *sec;
2108
2109 /* Total number of relocs copied for the input section. */
37c644f2 2110 bfd_size_type count;
067653c5
AM
2111
2112 /* Number of pc-relative relocs copied for the input section. */
2113 bfd_size_type pc_count;
37c644f2 2114};
252b5132 2115
37c644f2
AO
2116/* sh ELF linker hash entry. */
2117
38b1a46c
NC
2118struct elf_sh_link_hash_entry
2119{
37c644f2
AO
2120 struct elf_link_hash_entry root;
2121
fbca6ad9 2122#ifdef INCLUDE_SHMEDIA
396a6083
SC
2123 union
2124 {
2125 bfd_signed_vma refcount;
2126 bfd_vma offset;
2127 } datalabel_got;
fbca6ad9
AO
2128#endif
2129
067653c5
AM
2130 /* Track dynamic relocs copied for this symbol. */
2131 struct elf_sh_dyn_relocs *dyn_relocs;
4989d792
SC
2132
2133 bfd_signed_vma gotplt_refcount;
3376eaf5
KK
2134
2135 enum {
2136 GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE
2137 } tls_type;
3376eaf5
KK
2138};
2139
2140#define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2141
2142struct sh_elf_obj_tdata
2143{
2144 struct elf_obj_tdata root;
2145
2146 /* tls_type for each local got entry. */
2147 char *local_got_tls_type;
37c644f2
AO
2148};
2149
3376eaf5
KK
2150#define sh_elf_tdata(abfd) \
2151 ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2152
2153#define sh_elf_local_got_tls_type(abfd) \
2154 (sh_elf_tdata (abfd)->local_got_tls_type)
2155
2156/* Override the generic function because we need to store sh_elf_obj_tdata
2157 as the specific tdata. */
2158
b34976b6 2159static bfd_boolean
09fd220b 2160sh_elf_mkobject (bfd *abfd)
3376eaf5 2161{
3376eaf5 2162 if (abfd->tdata.any == NULL)
62d7a5f6
AM
2163 {
2164 bfd_size_type amt = sizeof (struct sh_elf_obj_tdata);
2165 abfd->tdata.any = bfd_zalloc (abfd, amt);
2166 if (abfd->tdata.any == NULL)
2167 return FALSE;
2168 }
2169 return bfd_elf_mkobject (abfd);
3376eaf5 2170}
b34976b6 2171
37c644f2
AO
2172/* sh ELF linker hash table. */
2173
38b1a46c
NC
2174struct elf_sh_link_hash_table
2175{
37c644f2 2176 struct elf_link_hash_table root;
37c644f2 2177
067653c5
AM
2178 /* Short-cuts to get to dynamic linker sections. */
2179 asection *sgot;
2180 asection *sgotplt;
2181 asection *srelgot;
2182 asection *splt;
2183 asection *srelplt;
2184 asection *sdynbss;
2185 asection *srelbss;
37c644f2 2186
55e6e397
RS
2187 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2188 asection *srelplt2;
2189
067653c5
AM
2190 /* Small local sym to section mapping cache. */
2191 struct sym_sec_cache sym_sec;
3376eaf5
KK
2192
2193 /* A counter or offset to track a TLS got entry. */
2194 union
2195 {
2196 bfd_signed_vma refcount;
2197 bfd_vma offset;
2198 } tls_ldm_got;
55e6e397
RS
2199
2200 /* The type of PLT to use. */
2201 const struct elf_sh_plt_info *plt_info;
2202
2203 /* True if the target system is VxWorks. */
2204 bfd_boolean vxworks_p;
067653c5 2205};
37c644f2
AO
2206
2207/* Traverse an sh ELF linker hash table. */
2208
2209#define sh_elf_link_hash_traverse(table, func, info) \
2210 (elf_link_hash_traverse \
2211 (&(table)->root, \
09fd220b 2212 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
37c644f2
AO
2213 (info)))
2214
2215/* Get the sh ELF linker hash table from a link_info structure. */
2216
2217#define sh_elf_hash_table(p) \
2218 ((struct elf_sh_link_hash_table *) ((p)->hash))
2219
2220/* Create an entry in an sh ELF linker hash table. */
2221
2222static struct bfd_hash_entry *
09fd220b
KK
2223sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2224 struct bfd_hash_table *table,
2225 const char *string)
37c644f2
AO
2226{
2227 struct elf_sh_link_hash_entry *ret =
2228 (struct elf_sh_link_hash_entry *) entry;
2229
2230 /* Allocate the structure if it has not already been allocated by a
2231 subclass. */
2232 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2233 ret = ((struct elf_sh_link_hash_entry *)
2234 bfd_hash_allocate (table,
2235 sizeof (struct elf_sh_link_hash_entry)));
2236 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2237 return (struct bfd_hash_entry *) ret;
2238
2239 /* Call the allocation method of the superclass. */
2240 ret = ((struct elf_sh_link_hash_entry *)
2241 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2242 table, string));
2243 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2244 {
396a6083
SC
2245 ret->dyn_relocs = NULL;
2246 ret->gotplt_refcount = 0;
fbca6ad9 2247#ifdef INCLUDE_SHMEDIA
396a6083 2248 ret->datalabel_got.refcount = ret->root.got.refcount;
fbca6ad9 2249#endif
3376eaf5 2250 ret->tls_type = GOT_UNKNOWN;
37c644f2
AO
2251 }
2252
2253 return (struct bfd_hash_entry *) ret;
2254}
2255
2256/* Create an sh ELF linker hash table. */
2257
2258static struct bfd_link_hash_table *
09fd220b 2259sh_elf_link_hash_table_create (bfd *abfd)
37c644f2
AO
2260{
2261 struct elf_sh_link_hash_table *ret;
dc810e39 2262 bfd_size_type amt = sizeof (struct elf_sh_link_hash_table);
37c644f2 2263
e2d34d7d 2264 ret = (struct elf_sh_link_hash_table *) bfd_malloc (amt);
37c644f2
AO
2265 if (ret == (struct elf_sh_link_hash_table *) NULL)
2266 return NULL;
2267
66eb6687
AM
2268 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2269 sh_elf_link_hash_newfunc,
2270 sizeof (struct elf_sh_link_hash_entry)))
37c644f2 2271 {
e2d34d7d 2272 free (ret);
37c644f2
AO
2273 return NULL;
2274 }
2275
067653c5
AM
2276 ret->sgot = NULL;
2277 ret->sgotplt = NULL;
2278 ret->srelgot = NULL;
2279 ret->splt = NULL;
2280 ret->srelplt = NULL;
2281 ret->sdynbss = NULL;
2282 ret->srelbss = NULL;
55e6e397 2283 ret->srelplt2 = NULL;
067653c5 2284 ret->sym_sec.abfd = NULL;
3376eaf5 2285 ret->tls_ldm_got.refcount = 0;
55e6e397
RS
2286 ret->plt_info = NULL;
2287 ret->vxworks_p = vxworks_object_p (abfd);
067653c5 2288
37c644f2
AO
2289 return &ret->root.root;
2290}
2291
067653c5
AM
2292/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2293 shortcuts to them in our hash table. */
2294
b34976b6 2295static bfd_boolean
09fd220b 2296create_got_section (bfd *dynobj, struct bfd_link_info *info)
067653c5
AM
2297{
2298 struct elf_sh_link_hash_table *htab;
2299
2300 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 2301 return FALSE;
067653c5
AM
2302
2303 htab = sh_elf_hash_table (info);
2304 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2305 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
2306 if (! htab->sgot || ! htab->sgotplt)
2307 abort ();
2308
3496cb2a
L
2309 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
2310 (SEC_ALLOC | SEC_LOAD
2311 | SEC_HAS_CONTENTS
2312 | SEC_IN_MEMORY
2313 | SEC_LINKER_CREATED
2314 | SEC_READONLY));
067653c5 2315 if (htab->srelgot == NULL
067653c5 2316 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
b34976b6
AM
2317 return FALSE;
2318 return TRUE;
067653c5
AM
2319}
2320
37c644f2
AO
2321/* Create dynamic sections when linking against a dynamic object. */
2322
b34976b6 2323static bfd_boolean
09fd220b 2324sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
37c644f2 2325{
067653c5 2326 struct elf_sh_link_hash_table *htab;
37c644f2
AO
2327 flagword flags, pltflags;
2328 register asection *s;
9c5bfbb7 2329 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
37c644f2
AO
2330 int ptralign = 0;
2331
2332 switch (bed->s->arch_size)
2333 {
2334 case 32:
2335 ptralign = 2;
2336 break;
2337
2338 case 64:
2339 ptralign = 3;
2340 break;
2341
2342 default:
2343 bfd_set_error (bfd_error_bad_value);
b34976b6 2344 return FALSE;
37c644f2
AO
2345 }
2346
067653c5 2347 htab = sh_elf_hash_table (info);
35cad4c4
KK
2348 if (htab->root.dynamic_sections_created)
2349 return TRUE;
067653c5 2350
37c644f2
AO
2351 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2352 .rel[a].bss sections. */
2353
2354 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2355 | SEC_LINKER_CREATED);
2356
2357 pltflags = flags;
2358 pltflags |= SEC_CODE;
2359 if (bed->plt_not_loaded)
2360 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2361 if (bed->plt_readonly)
2362 pltflags |= SEC_READONLY;
2363
3496cb2a 2364 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
35cad4c4
KK
2365 htab->splt = s;
2366 if (s == NULL
35cad4c4
KK
2367 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2368 return FALSE;
37c644f2
AO
2369
2370 if (bed->want_plt_sym)
2371 {
2372 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2373 .plt section. */
14a793b2
AM
2374 struct elf_link_hash_entry *h;
2375 struct bfd_link_hash_entry *bh = NULL;
2376
37c644f2
AO
2377 if (! (_bfd_generic_link_add_one_symbol
2378 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
b34976b6 2379 (bfd_vma) 0, (const char *) NULL, FALSE,
14a793b2 2380 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 2381 return FALSE;
14a793b2
AM
2382
2383 h = (struct elf_link_hash_entry *) bh;
f5385ebf 2384 h->def_regular = 1;
37c644f2 2385 h->type = STT_OBJECT;
7325306f 2386 htab->root.hplt = h;
37c644f2
AO
2387
2388 if (info->shared
c152c796 2389 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2390 return FALSE;
37c644f2
AO
2391 }
2392
3496cb2a
L
2393 s = bfd_make_section_with_flags (abfd,
2394 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt",
2395 flags | SEC_READONLY);
35cad4c4
KK
2396 htab->srelplt = s;
2397 if (s == NULL
35cad4c4
KK
2398 || ! bfd_set_section_alignment (abfd, s, ptralign))
2399 return FALSE;
37c644f2 2400
2293c499
AM
2401 if (htab->sgot == NULL
2402 && !create_got_section (abfd, info))
b34976b6 2403 return FALSE;
37c644f2
AO
2404
2405 {
2406 const char *secname;
2407 char *relname;
2408 flagword secflags;
2409 asection *sec;
2410
2411 for (sec = abfd->sections; sec; sec = sec->next)
2412 {
2413 secflags = bfd_get_section_flags (abfd, sec);
2414 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
2415 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
2416 continue;
2417 secname = bfd_get_section_name (abfd, sec);
dc810e39 2418 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
37c644f2
AO
2419 strcpy (relname, ".rela");
2420 strcat (relname, secname);
03151a5a
KK
2421 if (bfd_get_section_by_name (abfd, secname))
2422 continue;
3496cb2a
L
2423 s = bfd_make_section_with_flags (abfd, relname,
2424 flags | SEC_READONLY);
37c644f2 2425 if (s == NULL
37c644f2 2426 || ! bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 2427 return FALSE;
37c644f2
AO
2428 }
2429 }
2430
2431 if (bed->want_dynbss)
2432 {
2433 /* The .dynbss section is a place to put symbols which are defined
2434 by dynamic objects, are referenced by regular objects, and are
2435 not functions. We must allocate space for them in the process
2436 image and use a R_*_COPY reloc to tell the dynamic linker to
2437 initialize them at run time. The linker script puts the .dynbss
2438 section into the .bss section of the final image. */
3496cb2a
L
2439 s = bfd_make_section_with_flags (abfd, ".dynbss",
2440 SEC_ALLOC | SEC_LINKER_CREATED);
35cad4c4 2441 htab->sdynbss = s;
3496cb2a 2442 if (s == NULL)
35cad4c4 2443 return FALSE;
37c644f2
AO
2444
2445 /* The .rel[a].bss section holds copy relocs. This section is not
2446 normally needed. We need to create it here, though, so that the
2447 linker will map it to an output section. We can't just create it
2448 only if we need it, because we will not know whether we need it
2449 until we have seen all the input files, and the first time the
2450 main linker code calls BFD after examining all the input files
2451 (size_dynamic_sections) the input sections have already been
2452 mapped to the output sections. If the section turns out not to
2453 be needed, we can discard it later. We will never need this
2454 section when generating a shared object, since they do not use
2455 copy relocs. */
35cad4c4 2456 if (! info->shared)
37c644f2 2457 {
3496cb2a
L
2458 s = bfd_make_section_with_flags (abfd,
2459 (bed->default_use_rela_p
2460 ? ".rela.bss" : ".rel.bss"),
2461 flags | SEC_READONLY);
067653c5 2462 htab->srelbss = s;
37c644f2 2463 if (s == NULL
37c644f2 2464 || ! bfd_set_section_alignment (abfd, s, ptralign))
b34976b6 2465 return FALSE;
37c644f2
AO
2466 }
2467 }
2468
55e6e397
RS
2469 if (htab->vxworks_p)
2470 {
2471 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2472 return FALSE;
2473 }
2474
b34976b6 2475 return TRUE;
37c644f2
AO
2476}
2477\f
37c644f2
AO
2478/* Adjust a symbol defined by a dynamic object and referenced by a
2479 regular object. The current definition is in some section of the
2480 dynamic object, but we're not including those sections. We have to
2481 change the definition to something the rest of the link can
2482 understand. */
2483
b34976b6 2484static bfd_boolean
09fd220b
KK
2485sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2486 struct elf_link_hash_entry *h)
37c644f2 2487{
067653c5
AM
2488 struct elf_sh_link_hash_table *htab;
2489 struct elf_sh_link_hash_entry *eh;
2490 struct elf_sh_dyn_relocs *p;
37c644f2
AO
2491 asection *s;
2492 unsigned int power_of_two;
2493
2293c499 2494 htab = sh_elf_hash_table (info);
37c644f2
AO
2495
2496 /* Make sure we know what is going on here. */
2293c499 2497 BFD_ASSERT (htab->root.dynobj != NULL
f5385ebf 2498 && (h->needs_plt
f6e332e6 2499 || h->u.weakdef != NULL
f5385ebf
AM
2500 || (h->def_dynamic
2501 && h->ref_regular
2502 && !h->def_regular)));
37c644f2
AO
2503
2504 /* If this is a function, put it in the procedure linkage table. We
2505 will fill in the contents of the procedure linkage table later,
2506 when we know the address of the .got section. */
2507 if (h->type == STT_FUNC
f5385ebf 2508 || h->needs_plt)
37c644f2 2509 {
067653c5 2510 if (h->plt.refcount <= 0
3d85aebe
KK
2511 || SYMBOL_CALLS_LOCAL (info, h)
2512 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2513 && h->root.type == bfd_link_hash_undefweak))
37c644f2
AO
2514 {
2515 /* This case can occur if we saw a PLT reloc in an input
2516 file, but the symbol was never referred to by a dynamic
2517 object. In such a case, we don't actually need to build
2518 a procedure linkage table, and we can just do a REL32
2519 reloc instead. */
067653c5 2520 h->plt.offset = (bfd_vma) -1;
f5385ebf 2521 h->needs_plt = 0;
37c644f2
AO
2522 }
2523
b34976b6 2524 return TRUE;
37c644f2 2525 }
067653c5
AM
2526 else
2527 h->plt.offset = (bfd_vma) -1;
37c644f2
AO
2528
2529 /* If this is a weak symbol, and there is a real definition, the
2530 processor independent code will have arranged for us to see the
2531 real definition first, and we can just use the same value. */
f6e332e6 2532 if (h->u.weakdef != NULL)
37c644f2 2533 {
f6e332e6
AM
2534 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2535 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2536 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2537 h->root.u.def.value = h->u.weakdef->root.u.def.value;
04e534c3 2538 if (info->nocopyreloc)
f6e332e6 2539 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 2540 return TRUE;
37c644f2
AO
2541 }
2542
2543 /* This is a reference to a symbol defined by a dynamic object which
2544 is not a function. */
2545
2546 /* If we are creating a shared library, we must presume that the
2547 only references to the symbol are via the global offset table.
2548 For such cases we need not do anything here; the relocations will
2549 be handled correctly by relocate_section. */
2550 if (info->shared)
b34976b6 2551 return TRUE;
37c644f2
AO
2552
2553 /* If there are no references to this symbol that do not use the
2554 GOT, we don't need to generate a copy reloc. */
f5385ebf 2555 if (!h->non_got_ref)
b34976b6 2556 return TRUE;
37c644f2 2557
067653c5
AM
2558 /* If -z nocopyreloc was given, we won't generate them either. */
2559 if (info->nocopyreloc)
2560 {
f5385ebf 2561 h->non_got_ref = 0;
b34976b6 2562 return TRUE;
067653c5
AM
2563 }
2564
2565 eh = (struct elf_sh_link_hash_entry *) h;
2566 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2567 {
2568 s = p->sec->output_section;
2569 if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
2570 break;
2571 }
2572
2573 /* If we didn't find any dynamic relocs in sections which needs the
2574 copy reloc, then we'll be keeping the dynamic relocs and avoiding
2575 the copy reloc. */
2576 if (p == NULL)
2577 {
f5385ebf 2578 h->non_got_ref = 0;
b34976b6 2579 return TRUE;
067653c5
AM
2580 }
2581
909272ee
AM
2582 if (h->size == 0)
2583 {
2584 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2585 h->root.root.string);
2586 return TRUE;
2587 }
2588
37c644f2
AO
2589 /* We must allocate the symbol in our .dynbss section, which will
2590 become part of the .bss section of the executable. There will be
2591 an entry for this symbol in the .dynsym section. The dynamic
2592 object will contain position independent code, so all references
2593 from the dynamic object to this symbol will go through the global
2594 offset table. The dynamic linker will use the .dynsym entry to
2595 determine the address it must put in the global offset table, so
2596 both the dynamic object and the regular object will refer to the
2597 same memory location for the variable. */
2598
067653c5 2599 s = htab->sdynbss;
37c644f2
AO
2600 BFD_ASSERT (s != NULL);
2601
2602 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2603 copy the initial value out of the dynamic object and into the
2604 runtime process image. We need to remember the offset into the
2605 .rela.bss section we are going to use. */
2606 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2607 {
2608 asection *srel;
2609
067653c5 2610 srel = htab->srelbss;
37c644f2 2611 BFD_ASSERT (srel != NULL);
eea6121a 2612 srel->size += sizeof (Elf32_External_Rela);
f5385ebf 2613 h->needs_copy = 1;
37c644f2
AO
2614 }
2615
2616 /* We need to figure out the alignment required for this symbol. I
2617 have no idea how ELF linkers handle this. */
2618 power_of_two = bfd_log2 (h->size);
2619 if (power_of_two > 3)
2620 power_of_two = 3;
2621
2622 /* Apply the required alignment. */
eea6121a 2623 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2293c499 2624 if (power_of_two > bfd_get_section_alignment (htab->root.dynobj, s))
37c644f2 2625 {
2293c499 2626 if (! bfd_set_section_alignment (htab->root.dynobj, s, power_of_two))
b34976b6 2627 return FALSE;
37c644f2
AO
2628 }
2629
2630 /* Define the symbol as being at this point in the section. */
2631 h->root.u.def.section = s;
eea6121a 2632 h->root.u.def.value = s->size;
37c644f2
AO
2633
2634 /* Increment the section size to make room for the symbol. */
eea6121a 2635 s->size += h->size;
37c644f2 2636
b34976b6 2637 return TRUE;
37c644f2
AO
2638}
2639
067653c5
AM
2640/* Allocate space in .plt, .got and associated reloc sections for
2641 dynamic relocs. */
2642
b34976b6 2643static bfd_boolean
09fd220b 2644allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
067653c5
AM
2645{
2646 struct bfd_link_info *info;
2647 struct elf_sh_link_hash_table *htab;
2648 struct elf_sh_link_hash_entry *eh;
2649 struct elf_sh_dyn_relocs *p;
2650
2651 if (h->root.type == bfd_link_hash_indirect)
b34976b6 2652 return TRUE;
067653c5
AM
2653
2654 if (h->root.type == bfd_link_hash_warning)
2655 /* When warning symbols are created, they **replace** the "real"
2656 entry in the hash table, thus we never get to see the real
2657 symbol in a hash traversal. So look at it now. */
2658 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2659
2660 info = (struct bfd_link_info *) inf;
2661 htab = sh_elf_hash_table (info);
2662
4989d792
SC
2663 eh = (struct elf_sh_link_hash_entry *) h;
2664 if ((h->got.refcount > 0
f5385ebf 2665 || h->forced_local)
4989d792
SC
2666 && eh->gotplt_refcount > 0)
2667 {
2668 /* The symbol has been forced local, or we have some direct got refs,
26e41594 2669 so treat all the gotplt refs as got refs. */
4989d792
SC
2670 h->got.refcount += eh->gotplt_refcount;
2671 if (h->plt.refcount >= eh->gotplt_refcount)
2672 h->plt.refcount -= eh->gotplt_refcount;
2673 }
2674
067653c5 2675 if (htab->root.dynamic_sections_created
04e534c3
KK
2676 && h->plt.refcount > 0
2677 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2678 || h->root.type != bfd_link_hash_undefweak))
067653c5
AM
2679 {
2680 /* Make sure this symbol is output as a dynamic symbol.
2681 Undefined weak syms won't yet be marked as dynamic. */
2682 if (h->dynindx == -1
f5385ebf 2683 && !h->forced_local)
067653c5 2684 {
c152c796 2685 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2686 return FALSE;
067653c5
AM
2687 }
2688
267fb3c1
KK
2689 if (info->shared
2690 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
067653c5
AM
2691 {
2692 asection *s = htab->splt;
2693
2694 /* If this is the first .plt entry, make room for the special
2695 first entry. */
eea6121a 2696 if (s->size == 0)
55e6e397 2697 s->size += htab->plt_info->plt0_entry_size;
067653c5 2698
eea6121a 2699 h->plt.offset = s->size;
067653c5
AM
2700
2701 /* If this symbol is not defined in a regular file, and we are
2702 not generating a shared library, then set the symbol to this
2703 location in the .plt. This is required to make function
2704 pointers compare as equal between the normal executable and
2705 the shared library. */
2706 if (! info->shared
f5385ebf 2707 && !h->def_regular)
067653c5
AM
2708 {
2709 h->root.u.def.section = s;
2710 h->root.u.def.value = h->plt.offset;
2711 }
2712
2713 /* Make room for this entry. */
55e6e397 2714 s->size += htab->plt_info->symbol_entry_size;
067653c5
AM
2715
2716 /* We also need to make an entry in the .got.plt section, which
2717 will be placed in the .got section by the linker script. */
eea6121a 2718 htab->sgotplt->size += 4;
067653c5
AM
2719
2720 /* We also need to make an entry in the .rel.plt section. */
eea6121a 2721 htab->srelplt->size += sizeof (Elf32_External_Rela);
55e6e397
RS
2722
2723 if (htab->vxworks_p && !info->shared)
2724 {
2725 /* VxWorks executables have a second set of relocations
2726 for each PLT entry. They go in a separate relocation
2727 section, which is processed by the kernel loader. */
2728
2729 /* There is a relocation for the initial PLT entry:
2730 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2731 if (h->plt.offset == htab->plt_info->plt0_entry_size)
2732 htab->srelplt2->size += sizeof (Elf32_External_Rela);
2733
2734 /* There are two extra relocations for each subsequent
2735 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2736 and an R_SH_DIR32 relocation for the PLT entry. */
2737 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
2738 }
067653c5
AM
2739 }
2740 else
2741 {
2742 h->plt.offset = (bfd_vma) -1;
f5385ebf 2743 h->needs_plt = 0;
067653c5
AM
2744 }
2745 }
2746 else
2747 {
2748 h->plt.offset = (bfd_vma) -1;
f5385ebf 2749 h->needs_plt = 0;
067653c5
AM
2750 }
2751
2752 if (h->got.refcount > 0)
2753 {
2754 asection *s;
b34976b6 2755 bfd_boolean dyn;
3376eaf5 2756 int tls_type = sh_elf_hash_entry (h)->tls_type;
067653c5
AM
2757
2758 /* Make sure this symbol is output as a dynamic symbol.
2759 Undefined weak syms won't yet be marked as dynamic. */
2760 if (h->dynindx == -1
f5385ebf 2761 && !h->forced_local)
067653c5 2762 {
c152c796 2763 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2764 return FALSE;
067653c5
AM
2765 }
2766
2767 s = htab->sgot;
eea6121a
AM
2768 h->got.offset = s->size;
2769 s->size += 4;
3376eaf5
KK
2770 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
2771 if (tls_type == GOT_TLS_GD)
eea6121a 2772 s->size += 4;
067653c5 2773 dyn = htab->root.dynamic_sections_created;
267fb3c1 2774 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3376eaf5
KK
2775 R_SH_TLS_GD needs one if local symbol and two if global. */
2776 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
267fb3c1 2777 || (tls_type == GOT_TLS_IE && dyn))
eea6121a 2778 htab->srelgot->size += sizeof (Elf32_External_Rela);
3376eaf5 2779 else if (tls_type == GOT_TLS_GD)
eea6121a 2780 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
04e534c3
KK
2781 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2782 || h->root.type != bfd_link_hash_undefweak)
2783 && (info->shared
2784 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 2785 htab->srelgot->size += sizeof (Elf32_External_Rela);
067653c5
AM
2786 }
2787 else
2788 h->got.offset = (bfd_vma) -1;
2789
396a6083
SC
2790#ifdef INCLUDE_SHMEDIA
2791 if (eh->datalabel_got.refcount > 0)
2792 {
2793 asection *s;
b34976b6 2794 bfd_boolean dyn;
396a6083
SC
2795
2796 /* Make sure this symbol is output as a dynamic symbol.
2797 Undefined weak syms won't yet be marked as dynamic. */
2798 if (h->dynindx == -1
f5385ebf 2799 && !h->forced_local)
396a6083 2800 {
c152c796 2801 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2802 return FALSE;
396a6083
SC
2803 }
2804
2805 s = htab->sgot;
eea6121a
AM
2806 eh->datalabel_got.offset = s->size;
2807 s->size += 4;
396a6083 2808 dyn = htab->root.dynamic_sections_created;
267fb3c1 2809 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
eea6121a 2810 htab->srelgot->size += sizeof (Elf32_External_Rela);
396a6083
SC
2811 }
2812 else
2813 eh->datalabel_got.offset = (bfd_vma) -1;
2814#endif
2815
067653c5 2816 if (eh->dyn_relocs == NULL)
b34976b6 2817 return TRUE;
067653c5
AM
2818
2819 /* In the shared -Bsymbolic case, discard space allocated for
2820 dynamic pc-relative relocs against symbols which turn out to be
2821 defined in regular objects. For the normal shared case, discard
2822 space for pc-relative relocs that have become local due to symbol
2823 visibility changes. */
2824
2825 if (info->shared)
2826 {
3d85aebe 2827 if (SYMBOL_CALLS_LOCAL (info, h))
067653c5
AM
2828 {
2829 struct elf_sh_dyn_relocs **pp;
2830
2831 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2832 {
2833 p->count -= p->pc_count;
2834 p->pc_count = 0;
2835 if (p->count == 0)
2836 *pp = p->next;
2837 else
2838 pp = &p->next;
2839 }
2840 }
04e534c3
KK
2841
2842 /* Also discard relocs on undefined weak syms with non-default
2843 visibility. */
22d606e9 2844 if (eh->dyn_relocs != NULL
04e534c3 2845 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
2846 {
2847 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2848 eh->dyn_relocs = NULL;
2849
2850 /* Make sure undefined weak symbols are output as a dynamic
2851 symbol in PIEs. */
2852 else if (h->dynindx == -1
2853 && !h->forced_local)
2854 {
2855 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2856 return FALSE;
2857 }
2858 }
067653c5
AM
2859 }
2860 else
2861 {
2862 /* For the non-shared case, discard space for relocs against
2863 symbols which turn out to need copy relocs or are not
2864 dynamic. */
2865
f5385ebf
AM
2866 if (!h->non_got_ref
2867 && ((h->def_dynamic
2868 && !h->def_regular)
067653c5
AM
2869 || (htab->root.dynamic_sections_created
2870 && (h->root.type == bfd_link_hash_undefweak
2871 || h->root.type == bfd_link_hash_undefined))))
2872 {
2873 /* Make sure this symbol is output as a dynamic symbol.
2874 Undefined weak syms won't yet be marked as dynamic. */
2875 if (h->dynindx == -1
f5385ebf 2876 && !h->forced_local)
067653c5 2877 {
c152c796 2878 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2879 return FALSE;
067653c5
AM
2880 }
2881
2882 /* If that succeeded, we know we'll be keeping all the
2883 relocs. */
2884 if (h->dynindx != -1)
2885 goto keep;
2886 }
2887
2888 eh->dyn_relocs = NULL;
2889
2890 keep: ;
2891 }
2892
2893 /* Finally, allocate space. */
2894 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2895 {
2896 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 2897 sreloc->size += p->count * sizeof (Elf32_External_Rela);
067653c5
AM
2898 }
2899
b34976b6 2900 return TRUE;
067653c5
AM
2901}
2902
2903/* Find any dynamic relocs that apply to read-only sections. */
2904
b34976b6 2905static bfd_boolean
09fd220b 2906readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
067653c5
AM
2907{
2908 struct elf_sh_link_hash_entry *eh;
2909 struct elf_sh_dyn_relocs *p;
2910
2911 if (h->root.type == bfd_link_hash_warning)
2912 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2913
2914 eh = (struct elf_sh_link_hash_entry *) h;
2915 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2916 {
2917 asection *s = p->sec->output_section;
2918
2919 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2920 {
2921 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2922
2923 info->flags |= DF_TEXTREL;
2924
2925 /* Not an error, just cut short the traversal. */
b34976b6 2926 return FALSE;
067653c5
AM
2927 }
2928 }
b34976b6 2929 return TRUE;
067653c5
AM
2930}
2931
55e6e397
RS
2932/* This function is called after all the input files have been read,
2933 and the input sections have been assigned to output sections.
2934 It's a convenient place to determine the PLT style. */
2935
2936static bfd_boolean
2937sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
2938{
2939 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd, info->shared);
2940 return TRUE;
2941}
2942
37c644f2
AO
2943/* Set the sizes of the dynamic sections. */
2944
b34976b6 2945static bfd_boolean
09fd220b
KK
2946sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2947 struct bfd_link_info *info)
37c644f2 2948{
067653c5 2949 struct elf_sh_link_hash_table *htab;
37c644f2
AO
2950 bfd *dynobj;
2951 asection *s;
b34976b6 2952 bfd_boolean relocs;
067653c5 2953 bfd *ibfd;
37c644f2 2954
067653c5
AM
2955 htab = sh_elf_hash_table (info);
2956 dynobj = htab->root.dynobj;
37c644f2
AO
2957 BFD_ASSERT (dynobj != NULL);
2958
067653c5 2959 if (htab->root.dynamic_sections_created)
37c644f2
AO
2960 {
2961 /* Set the contents of the .interp section to the interpreter. */
2558b9a9 2962 if (info->executable)
37c644f2
AO
2963 {
2964 s = bfd_get_section_by_name (dynobj, ".interp");
2965 BFD_ASSERT (s != NULL);
eea6121a 2966 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
37c644f2
AO
2967 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2968 }
2969 }
37c644f2 2970
067653c5
AM
2971 /* Set up .got offsets for local syms, and space for local dynamic
2972 relocs. */
2973 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
37c644f2 2974 {
067653c5
AM
2975 bfd_signed_vma *local_got;
2976 bfd_signed_vma *end_local_got;
3376eaf5 2977 char *local_tls_type;
067653c5
AM
2978 bfd_size_type locsymcount;
2979 Elf_Internal_Shdr *symtab_hdr;
2980 asection *srel;
37c644f2 2981
067653c5 2982 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
37c644f2
AO
2983 continue;
2984
067653c5 2985 for (s = ibfd->sections; s != NULL; s = s->next)
37c644f2 2986 {
067653c5
AM
2987 struct elf_sh_dyn_relocs *p;
2988
2989 for (p = ((struct elf_sh_dyn_relocs *)
2990 elf_section_data (s)->local_dynrel);
2991 p != NULL;
2992 p = p->next)
37c644f2 2993 {
067653c5
AM
2994 if (! bfd_is_abs_section (p->sec)
2995 && bfd_is_abs_section (p->sec->output_section))
2996 {
2997 /* Input section has been discarded, either because
2998 it is a copy of a linkonce section or due to
2999 linker script /DISCARD/, so we'll be discarding
3000 the relocs too. */
3001 }
3002 else if (p->count != 0)
3003 {
3004 srel = elf_section_data (p->sec)->sreloc;
eea6121a 3005 srel->size += p->count * sizeof (Elf32_External_Rela);
067653c5
AM
3006 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3007 info->flags |= DF_TEXTREL;
3008 }
37c644f2
AO
3009 }
3010 }
067653c5
AM
3011
3012 local_got = elf_local_got_refcounts (ibfd);
3013 if (!local_got)
3014 continue;
3015
3016 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3017 locsymcount = symtab_hdr->sh_info;
3018#ifdef INCLUDE_SHMEDIA
3019 /* Count datalabel local GOT. */
3020 locsymcount *= 2;
3021#endif
3022 end_local_got = local_got + locsymcount;
3376eaf5 3023 local_tls_type = sh_elf_local_got_tls_type (ibfd);
067653c5
AM
3024 s = htab->sgot;
3025 srel = htab->srelgot;
3026 for (; local_got < end_local_got; ++local_got)
37c644f2 3027 {
067653c5 3028 if (*local_got > 0)
37c644f2 3029 {
eea6121a
AM
3030 *local_got = s->size;
3031 s->size += 4;
3376eaf5 3032 if (*local_tls_type == GOT_TLS_GD)
eea6121a 3033 s->size += 4;
067653c5 3034 if (info->shared)
eea6121a 3035 srel->size += sizeof (Elf32_External_Rela);
37c644f2
AO
3036 }
3037 else
067653c5 3038 *local_got = (bfd_vma) -1;
3376eaf5 3039 ++local_tls_type;
37c644f2 3040 }
067653c5
AM
3041 }
3042
3376eaf5
KK
3043 if (htab->tls_ldm_got.refcount > 0)
3044 {
3045 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3046 relocs. */
eea6121a
AM
3047 htab->tls_ldm_got.offset = htab->sgot->size;
3048 htab->sgot->size += 8;
3049 htab->srelgot->size += sizeof (Elf32_External_Rela);
3376eaf5
KK
3050 }
3051 else
3052 htab->tls_ldm_got.offset = -1;
3053
067653c5
AM
3054 /* Allocate global sym .plt and .got entries, and space for global
3055 sym dynamic relocs. */
09fd220b 3056 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
067653c5
AM
3057
3058 /* We now have determined the sizes of the various dynamic sections.
3059 Allocate memory for them. */
b34976b6 3060 relocs = FALSE;
067653c5
AM
3061 for (s = dynobj->sections; s != NULL; s = s->next)
3062 {
3063 if ((s->flags & SEC_LINKER_CREATED) == 0)
3064 continue;
3065
3066 if (s == htab->splt
3067 || s == htab->sgot
c456f082
AM
3068 || s == htab->sgotplt
3069 || s == htab->sdynbss)
067653c5
AM
3070 {
3071 /* Strip this section if we don't need it; see the
3072 comment below. */
3073 }
0112cd26 3074 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
067653c5 3075 {
55e6e397 3076 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
b34976b6 3077 relocs = TRUE;
067653c5
AM
3078
3079 /* We use the reloc_count field as a counter if we need
3080 to copy relocs into the output file. */
3081 s->reloc_count = 0;
3082 }
3083 else
37c644f2
AO
3084 {
3085 /* It's not one of our sections, so don't allocate space. */
3086 continue;
3087 }
3088
eea6121a 3089 if (s->size == 0)
37c644f2 3090 {
067653c5
AM
3091 /* If we don't need this section, strip it from the
3092 output file. This is mostly to handle .rela.bss and
3093 .rela.plt. We must create both sections in
3094 create_dynamic_sections, because they must be created
3095 before the linker maps input sections to output
3096 sections. The linker does that before
3097 adjust_dynamic_symbol is called, and it is that
3098 function which decides whether anything needs to go
3099 into these sections. */
3100
8423293d 3101 s->flags |= SEC_EXCLUDE;
37c644f2
AO
3102 continue;
3103 }
3104
c456f082
AM
3105 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3106 continue;
3107
067653c5
AM
3108 /* Allocate memory for the section contents. We use bfd_zalloc
3109 here in case unused entries are not reclaimed before the
3110 section's contents are written out. This should not happen,
3111 but this way if it does, we get a R_SH_NONE reloc instead
3112 of garbage. */
eea6121a 3113 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
067653c5 3114 if (s->contents == NULL)
b34976b6 3115 return FALSE;
37c644f2
AO
3116 }
3117
067653c5 3118 if (htab->root.dynamic_sections_created)
37c644f2
AO
3119 {
3120 /* Add some entries to the .dynamic section. We fill in the
3121 values later, in sh_elf_finish_dynamic_sections, but we
3122 must add the entries now so that we get the correct size for
3123 the .dynamic section. The DT_DEBUG entry is filled in by the
3124 dynamic linker and used by the debugger. */
dc810e39 3125#define add_dynamic_entry(TAG, VAL) \
5a580b3a 3126 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 3127
2558b9a9 3128 if (info->executable)
37c644f2 3129 {
067653c5 3130 if (! add_dynamic_entry (DT_DEBUG, 0))
b34976b6 3131 return FALSE;
37c644f2
AO
3132 }
3133
eea6121a 3134 if (htab->splt->size != 0)
37c644f2 3135 {
067653c5
AM
3136 if (! add_dynamic_entry (DT_PLTGOT, 0)
3137 || ! add_dynamic_entry (DT_PLTRELSZ, 0)
3138 || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
3139 || ! add_dynamic_entry (DT_JMPREL, 0))
b34976b6 3140 return FALSE;
37c644f2
AO
3141 }
3142
3143 if (relocs)
3144 {
067653c5
AM
3145 if (! add_dynamic_entry (DT_RELA, 0)
3146 || ! add_dynamic_entry (DT_RELASZ, 0)
3147 || ! add_dynamic_entry (DT_RELAENT,
3148 sizeof (Elf32_External_Rela)))
b34976b6 3149 return FALSE;
37c644f2 3150
067653c5
AM
3151 /* If any dynamic relocs apply to a read-only section,
3152 then we need a DT_TEXTREL entry. */
3153 if ((info->flags & DF_TEXTREL) == 0)
09fd220b 3154 elf_link_hash_traverse (&htab->root, readonly_dynrelocs, info);
067653c5
AM
3155
3156 if ((info->flags & DF_TEXTREL) != 0)
3157 {
3158 if (! add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3159 return FALSE;
067653c5 3160 }
37c644f2
AO
3161 }
3162 }
dc810e39 3163#undef add_dynamic_entry
37c644f2 3164
b34976b6 3165 return TRUE;
37c644f2 3166}
37c644f2
AO
3167\f
3168/* Relocate an SH ELF section. */
3169
b34976b6 3170static bfd_boolean
09fd220b
KK
3171sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3172 bfd *input_bfd, asection *input_section,
3173 bfd_byte *contents, Elf_Internal_Rela *relocs,
3174 Elf_Internal_Sym *local_syms,
3175 asection **local_sections)
37c644f2 3176{
067653c5 3177 struct elf_sh_link_hash_table *htab;
37c644f2
AO
3178 Elf_Internal_Shdr *symtab_hdr;
3179 struct elf_link_hash_entry **sym_hashes;
3180 Elf_Internal_Rela *rel, *relend;
3181 bfd *dynobj;
3182 bfd_vma *local_got_offsets;
3183 asection *sgot;
fbca6ad9 3184 asection *sgotplt;
37c644f2
AO
3185 asection *splt;
3186 asection *sreloc;
3376eaf5 3187 asection *srelgot;
37c644f2 3188
067653c5 3189 htab = sh_elf_hash_table (info);
37c644f2
AO
3190 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3191 sym_hashes = elf_sym_hashes (input_bfd);
067653c5 3192 dynobj = htab->root.dynobj;
37c644f2
AO
3193 local_got_offsets = elf_local_got_offsets (input_bfd);
3194
067653c5
AM
3195 sgot = htab->sgot;
3196 sgotplt = htab->sgotplt;
3197 splt = htab->splt;
37c644f2 3198 sreloc = NULL;
3376eaf5 3199 srelgot = NULL;
37c644f2
AO
3200
3201 rel = relocs;
3202 relend = relocs + input_section->reloc_count;
3203 for (; rel < relend; rel++)
3204 {
3205 int r_type;
3206 reloc_howto_type *howto;
3207 unsigned long r_symndx;
3208 Elf_Internal_Sym *sym;
3209 asection *sec;
3210 struct elf_link_hash_entry *h;
3211 bfd_vma relocation;
435b1e90 3212 bfd_vma addend = (bfd_vma) 0;
37c644f2 3213 bfd_reloc_status_type r;
fbca6ad9 3214 int seen_stt_datalabel = 0;
3376eaf5
KK
3215 bfd_vma off;
3216 int tls_type;
37c644f2
AO
3217
3218 r_symndx = ELF32_R_SYM (rel->r_info);
3219
37c644f2
AO
3220 r_type = ELF32_R_TYPE (rel->r_info);
3221
3222 /* Many of the relocs are only used for relaxing, and are
067653c5 3223 handled entirely by the relaxation code. */
d38eb334
DD
3224 if (r_type >= (int) R_SH_GNU_VTINHERIT
3225 && r_type <= (int) R_SH_LABEL)
37c644f2 3226 continue;
c5aeb40f
AO
3227 if (r_type == (int) R_SH_NONE)
3228 continue;
37c644f2
AO
3229
3230 if (r_type < 0
3231 || r_type >= R_SH_max
3232 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
06bb75c1 3233 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
fbca6ad9
AO
3234 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3235 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3236 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3237 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3376eaf5
KK
3238 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3239 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
06bb75c1
AO
3240 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3241 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2))
37c644f2
AO
3242 {
3243 bfd_set_error (bfd_error_bad_value);
b34976b6 3244 return FALSE;
37c644f2
AO
3245 }
3246
55e6e397 3247 howto = get_howto_table (output_bfd) + r_type;
37c644f2 3248
146be91a 3249 /* For relocs that aren't partial_inplace, we get the addend from
067653c5 3250 the relocation. */
146be91a
HPN
3251 if (! howto->partial_inplace)
3252 addend = rel->r_addend;
3253
37c644f2
AO
3254 h = NULL;
3255 sym = NULL;
3256 sec = NULL;
3257 if (r_symndx < symtab_hdr->sh_info)
3258 {
3259 sym = local_syms + r_symndx;
3260 sec = local_sections[r_symndx];
3261 relocation = (sec->output_section->vma
3262 + sec->output_offset
3263 + sym->st_value);
fbca6ad9
AO
3264 /* A local symbol never has STO_SH5_ISA32, so we don't need
3265 datalabel processing here. Make sure this does not change
3266 without notice. */
3267 if ((sym->st_other & STO_SH5_ISA32) != 0)
3268 ((*info->callbacks->reloc_dangerous)
3269 (info,
3270 _("Unexpected STO_SH5_ISA32 on local symbol is not handled"),
3271 input_bfd, input_section, rel->r_offset));
ab96bf03
AM
3272
3273 if (sec != NULL && elf_discarded_section (sec))
3274 /* Handled below. */
3275 ;
3276 else if (info->relocatable)
8a3975e6 3277 {
1049f94e 3278 /* This is a relocatable link. We don't have to change
8a3975e6
AO
3279 anything, unless the reloc is against a section symbol,
3280 in which case we have to adjust according to where the
3281 section symbol winds up in the output section. */
8a3975e6 3282 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
146be91a
HPN
3283 {
3284 if (! howto->partial_inplace)
3285 {
3286 /* For relocations with the addend in the
3287 relocation, we need just to update the addend.
3288 All real relocs are of type partial_inplace; this
3289 code is mostly for completeness. */
ab96bf03 3290 rel->r_addend += sec->output_offset;
146be91a
HPN
3291
3292 continue;
3293 }
3294
3295 /* Relocs of type partial_inplace need to pick up the
3296 contents in the contents and add the offset resulting
3297 from the changed location of the section symbol.
3298 Using _bfd_final_link_relocate (e.g. goto
3299 final_link_relocate) here would be wrong, because
3300 relocations marked pc_relative would get the current
3301 location subtracted, and we must only do that at the
3302 final link. */
3303 r = _bfd_relocate_contents (howto, input_bfd,
3304 sec->output_offset
3305 + sym->st_value,
3306 contents + rel->r_offset);
3307 goto relocation_done;
3308 }
8a3975e6
AO
3309
3310 continue;
3311 }
f8df10f4
JJ
3312 else if (! howto->partial_inplace)
3313 {
8517fae7 3314 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
f8df10f4
JJ
3315 addend = rel->r_addend;
3316 }
3317 else if ((sec->flags & SEC_MERGE)
3318 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3319 {
3320 asection *msec;
3321
3322 if (howto->rightshift || howto->src_mask != 0xffffffff)
3323 {
3324 (*_bfd_error_handler)
d003868e
AM
3325 (_("%B(%A+0x%lx): %s relocation against SEC_MERGE section"),
3326 input_bfd, input_section,
f8df10f4 3327 (long) rel->r_offset, howto->name);
b34976b6 3328 return FALSE;
f8df10f4
JJ
3329 }
3330
067653c5
AM
3331 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
3332 msec = sec;
3333 addend =
c629eae0 3334 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
f8df10f4
JJ
3335 - relocation;
3336 addend += msec->output_section->vma + msec->output_offset;
3337 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
3338 addend = 0;
3339 }
37c644f2
AO
3340 }
3341 else
3342 {
560e09e9
NC
3343 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3344
ab96bf03 3345 relocation = 0;
37c644f2
AO
3346 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3347 while (h->root.type == bfd_link_hash_indirect
3348 || h->root.type == bfd_link_hash_warning)
fbca6ad9
AO
3349 {
3350#ifdef INCLUDE_SHMEDIA
3351 /* If the reference passes a symbol marked with
3352 STT_DATALABEL, then any STO_SH5_ISA32 on the final value
3353 doesn't count. */
3354 seen_stt_datalabel |= h->type == STT_DATALABEL;
3355#endif
3356 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3357 }
37c644f2
AO
3358 if (h->root.type == bfd_link_hash_defined
3359 || h->root.type == bfd_link_hash_defweak)
3360 {
b34976b6 3361 bfd_boolean dyn;
067653c5
AM
3362
3363 dyn = htab->root.dynamic_sections_created;
37c644f2
AO
3364 sec = h->root.u.def.section;
3365 /* In these cases, we don't need the relocation value.
3366 We check specially because in some obscure cases
435b1e90 3367 sec->output_section will be NULL. */
37c644f2 3368 if (r_type == R_SH_GOTPC
fbca6ad9
AO
3369 || r_type == R_SH_GOTPC_LOW16
3370 || r_type == R_SH_GOTPC_MEDLOW16
3371 || r_type == R_SH_GOTPC_MEDHI16
3372 || r_type == R_SH_GOTPC_HI16
3373 || ((r_type == R_SH_PLT32
3374 || r_type == R_SH_PLT_LOW16
3375 || r_type == R_SH_PLT_MEDLOW16
3376 || r_type == R_SH_PLT_MEDHI16
3377 || r_type == R_SH_PLT_HI16)
37c644f2 3378 && h->plt.offset != (bfd_vma) -1)
fbca6ad9
AO
3379 || ((r_type == R_SH_GOT32
3380 || r_type == R_SH_GOT_LOW16
3381 || r_type == R_SH_GOT_MEDLOW16
3382 || r_type == R_SH_GOT_MEDHI16
3383 || r_type == R_SH_GOT_HI16)
267fb3c1 3384 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
37c644f2
AO
3385 && (! info->shared
3386 || (! info->symbolic && h->dynindx != -1)
f5385ebf 3387 || !h->def_regular))
37c644f2
AO
3388 /* The cases above are those in which relocation is
3389 overwritten in the switch block below. The cases
3390 below are those in which we must defer relocation
3391 to run-time, because we can't resolve absolute
3392 addresses when creating a shared library. */
3393 || (info->shared
3394 && ((! info->symbolic && h->dynindx != -1)
f5385ebf 3395 || !h->def_regular)
37c644f2 3396 && ((r_type == R_SH_DIR32
f5385ebf 3397 && !h->forced_local)
f50ad433
KK
3398 || (r_type == R_SH_REL32
3399 && !SYMBOL_CALLS_LOCAL (info, h)))
37c644f2
AO
3400 && ((input_section->flags & SEC_ALLOC) != 0
3401 /* DWARF will emit R_SH_DIR32 relocations in its
3402 sections against symbols defined externally
3403 in shared libraries. We can't do anything
3404 with them here. */
067653c5 3405 || ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3406 && h->def_dynamic)))
1448fa32
KK
3407 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3408 sections because such sections are not SEC_ALLOC and
3409 thus ld.so will not process them. */
3410 || (sec->output_section == NULL
3411 && ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3412 && h->def_dynamic))
3376eaf5
KK
3413 || (sec->output_section == NULL
3414 && (sh_elf_hash_entry (h)->tls_type == GOT_TLS_IE
3415 || sh_elf_hash_entry (h)->tls_type == GOT_TLS_GD)))
ab96bf03
AM
3416 ;
3417 else if (sec->output_section != NULL)
3418 relocation = ((h->root.u.def.value
3419 + sec->output_section->vma
3420 + sec->output_offset)
3421 /* A STO_SH5_ISA32 causes a "bitor 1" to the
3422 symbol value, unless we've seen
3423 STT_DATALABEL on the way to it. */
3424 | ((h->other & STO_SH5_ISA32) != 0
3425 && ! seen_stt_datalabel));
3426 else if (!info->relocatable)
37c644f2
AO
3427 {
3428 (*_bfd_error_handler)
843fe662
L
3429 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3430 input_bfd,
3431 input_section,
3432 (long) rel->r_offset,
3433 howto->name,
3434 h->root.root.string);
b34976b6 3435 return FALSE;
37c644f2 3436 }
37c644f2
AO
3437 }
3438 else if (h->root.type == bfd_link_hash_undefweak)
ab96bf03 3439 ;
59c2e50f 3440 else if (info->unresolved_syms_in_objects == RM_IGNORE
067653c5 3441 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
ab96bf03
AM
3442 ;
3443 else if (!info->relocatable)
37c644f2 3444 {
560e09e9
NC
3445 if (! info->callbacks->undefined_symbol
3446 (info, h->root.root.string, input_bfd,
3447 input_section, rel->r_offset,
59c2e50f 3448 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
560e09e9 3449 || ELF_ST_VISIBILITY (h->other))))
b34976b6 3450 return FALSE;
37c644f2
AO
3451 }
3452 }
3453
ab96bf03
AM
3454 if (sec != NULL && elf_discarded_section (sec))
3455 {
3456 /* For relocs against symbols from removed linkonce sections,
3457 or sections discarded by a linker script, we just want the
3458 section contents zeroed. Avoid any special processing. */
3459 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
3460 rel->r_info = 0;
3461 rel->r_addend = 0;
3462 continue;
3463 }
3464
3465 if (info->relocatable)
3466 continue;
3467
435b1e90 3468 switch ((int) r_type)
37c644f2
AO
3469 {
3470 final_link_relocate:
3471 /* COFF relocs don't use the addend. The addend is used for
435b1e90 3472 R_SH_DIR32 to be compatible with other compilers. */
37c644f2
AO
3473 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3474 contents, rel->r_offset,
3475 relocation, addend);
3476 break;
3477
3478 case R_SH_IND12W:
cd6ec716
DD
3479 goto final_link_relocate;
3480
37c644f2
AO
3481 case R_SH_DIR8WPN:
3482 case R_SH_DIR8WPZ:
3483 case R_SH_DIR8WPL:
cd6ec716
DD
3484 /* If the reloc is against the start of this section, then
3485 the assembler has already taken care of it and the reloc
3486 is here only to assist in relaxing. If the reloc is not
3487 against the start of this section, then it's against an
3488 external symbol and we must deal with it ourselves. */
3489 if (input_section->output_section->vma + input_section->output_offset
3490 != relocation)
3491 {
3492 int disp = (relocation
3493 - input_section->output_section->vma
3494 - input_section->output_offset
3495 - rel->r_offset);
3496 int mask = 0;
3497 switch (r_type)
3498 {
3499 case R_SH_DIR8WPN:
3500 case R_SH_DIR8WPZ: mask = 1; break;
3501 case R_SH_DIR8WPL: mask = 3; break;
3502 default: mask = 0; break;
3503 }
3504 if (disp & mask)
3505 {
3506 ((*_bfd_error_handler)
d003868e
AM
3507 (_("%B: 0x%lx: fatal: unaligned branch target for relax-support relocation"),
3508 input_section->owner,
cd6ec716
DD
3509 (unsigned long) rel->r_offset));
3510 bfd_set_error (bfd_error_bad_value);
b34976b6 3511 return FALSE;
cd6ec716
DD
3512 }
3513 relocation -= 4;
3514 goto final_link_relocate;
3515 }
3516 r = bfd_reloc_ok;
3517 break;
37c644f2
AO
3518
3519 default:
fbca6ad9
AO
3520#ifdef INCLUDE_SHMEDIA
3521 if (shmedia_prepare_reloc (info, input_bfd, input_section,
3522 contents, rel, &relocation))
3523 goto final_link_relocate;
3524#endif
37c644f2 3525 bfd_set_error (bfd_error_bad_value);
b34976b6 3526 return FALSE;
37c644f2 3527
d38eb334
DD
3528 case R_SH_DIR16:
3529 case R_SH_DIR8:
3530 case R_SH_DIR8U:
3531 case R_SH_DIR8S:
3532 case R_SH_DIR4U:
3533 goto final_link_relocate;
3534
3535 case R_SH_DIR8UL:
3536 case R_SH_DIR4UL:
3537 if (relocation & 3)
3538 {
3539 ((*_bfd_error_handler)
d003868e
AM
3540 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3541 input_section->owner,
d38eb334 3542 (unsigned long) rel->r_offset, howto->name,
d003868e 3543 (unsigned long) relocation));
d38eb334
DD
3544 bfd_set_error (bfd_error_bad_value);
3545 return FALSE;
3546 }
3547 goto final_link_relocate;
3548
3549 case R_SH_DIR8UW:
3550 case R_SH_DIR8SW:
3551 case R_SH_DIR4UW:
3552 if (relocation & 1)
3553 {
3554 ((*_bfd_error_handler)
d003868e
AM
3555 (_("%B: 0x%lx: fatal: unaligned %s relocation 0x%lx"),
3556 input_section->owner,
d38eb334 3557 (unsigned long) rel->r_offset, howto->name,
d003868e 3558 (unsigned long) relocation));
d38eb334
DD
3559 bfd_set_error (bfd_error_bad_value);
3560 return FALSE;
3561 }
3562 goto final_link_relocate;
3563
3564 case R_SH_PSHA:
3565 if ((signed int)relocation < -32
3566 || (signed int)relocation > 32)
3567 {
3568 ((*_bfd_error_handler)
d003868e
AM
3569 (_("%B: 0x%lx: fatal: R_SH_PSHA relocation %d not in range -32..32"),
3570 input_section->owner,
d38eb334 3571 (unsigned long) rel->r_offset,
d003868e 3572 (unsigned long) relocation));
d38eb334
DD
3573 bfd_set_error (bfd_error_bad_value);
3574 return FALSE;
3575 }
3576 goto final_link_relocate;
3577
3578 case R_SH_PSHL:
3579 if ((signed int)relocation < -16
3580 || (signed int)relocation > 16)
3581 {
3582 ((*_bfd_error_handler)
d003868e
AM
3583 (_("%B: 0x%lx: fatal: R_SH_PSHL relocation %d not in range -32..32"),
3584 input_section->owner,
d38eb334 3585 (unsigned long) rel->r_offset,
d003868e 3586 (unsigned long) relocation));
d38eb334
DD
3587 bfd_set_error (bfd_error_bad_value);
3588 return FALSE;
3589 }
3590 goto final_link_relocate;
3591
37c644f2
AO
3592 case R_SH_DIR32:
3593 case R_SH_REL32:
46e993b9
KK
3594#ifdef INCLUDE_SHMEDIA
3595 case R_SH_IMM_LOW16_PCREL:
3596 case R_SH_IMM_MEDLOW16_PCREL:
3597 case R_SH_IMM_MEDHI16_PCREL:
3598 case R_SH_IMM_HI16_PCREL:
3599#endif
37c644f2 3600 if (info->shared
04e534c3
KK
3601 && (h == NULL
3602 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3603 || h->root.type != bfd_link_hash_undefweak)
ec338859 3604 && r_symndx != 0
37c644f2 3605 && (input_section->flags & SEC_ALLOC) != 0
46e993b9 3606 && (r_type == R_SH_DIR32
3d85aebe 3607 || !SYMBOL_CALLS_LOCAL (info, h)))
37c644f2
AO
3608 {
3609 Elf_Internal_Rela outrel;
947216bf 3610 bfd_byte *loc;
b34976b6 3611 bfd_boolean skip, relocate;
37c644f2
AO
3612
3613 /* When generating a shared object, these relocations
3614 are copied into the output file to be resolved at run
3615 time. */
3616
3617 if (sreloc == NULL)
3618 {
3619 const char *name;
3620
3621 name = (bfd_elf_string_from_elf_section
3622 (input_bfd,
3623 elf_elfheader (input_bfd)->e_shstrndx,
3624 elf_section_data (input_section)->rel_hdr.sh_name));
3625 if (name == NULL)
b34976b6 3626 return FALSE;
37c644f2 3627
0112cd26 3628 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
37c644f2
AO
3629 && strcmp (bfd_get_section_name (input_bfd,
3630 input_section),
3631 name + 5) == 0);
3632
3633 sreloc = bfd_get_section_by_name (dynobj, name);
3634 BFD_ASSERT (sreloc != NULL);
3635 }
3636
b34976b6
AM
3637 skip = FALSE;
3638 relocate = FALSE;
37c644f2 3639
c629eae0
JJ
3640 outrel.r_offset =
3641 _bfd_elf_section_offset (output_bfd, info, input_section,
3642 rel->r_offset);
3643 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3644 skip = TRUE;
0bb2d96a 3645 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3646 skip = TRUE, relocate = TRUE;
37c644f2
AO
3647 outrel.r_offset += (input_section->output_section->vma
3648 + input_section->output_offset);
3649
3650 if (skip)
0bb2d96a 3651 memset (&outrel, 0, sizeof outrel);
37c644f2
AO
3652 else if (r_type == R_SH_REL32)
3653 {
3654 BFD_ASSERT (h != NULL && h->dynindx != -1);
37c644f2 3655 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
146be91a 3656 outrel.r_addend
55e6e397
RS
3657 = (howto->partial_inplace
3658 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3659 : addend);
37c644f2 3660 }
46e993b9
KK
3661#ifdef INCLUDE_SHMEDIA
3662 else if (r_type == R_SH_IMM_LOW16_PCREL
3663 || r_type == R_SH_IMM_MEDLOW16_PCREL
3664 || r_type == R_SH_IMM_MEDHI16_PCREL
3665 || r_type == R_SH_IMM_HI16_PCREL)
3666 {
3667 BFD_ASSERT (h != NULL && h->dynindx != -1);
3668 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3669 outrel.r_addend = addend;
3670 }
3671#endif
37c644f2
AO
3672 else
3673 {
3674 /* h->dynindx may be -1 if this symbol was marked to
3675 become local. */
3676 if (h == NULL
3677 || ((info->symbolic || h->dynindx == -1)
f5385ebf 3678 && h->def_regular))
37c644f2 3679 {
55e6e397 3680 relocate = howto->partial_inplace;
37c644f2 3681 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
37c644f2
AO
3682 }
3683 else
3684 {
3685 BFD_ASSERT (h->dynindx != -1);
37c644f2 3686 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
37c644f2 3687 }
55e6e397
RS
3688 outrel.r_addend = relocation;
3689 outrel.r_addend
3690 += (howto->partial_inplace
3691 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3692 : addend);
37c644f2
AO
3693 }
3694
947216bf
AM
3695 loc = sreloc->contents;
3696 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3697 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
37c644f2
AO
3698
3699 /* If this reloc is against an external symbol, we do
3700 not want to fiddle with the addend. Otherwise, we
3701 need to include the symbol value so that it becomes
3702 an addend for the dynamic reloc. */
3703 if (! relocate)
3704 continue;
3705 }
37c644f2
AO
3706 goto final_link_relocate;
3707
fbca6ad9
AO
3708 case R_SH_GOTPLT32:
3709#ifdef INCLUDE_SHMEDIA
3710 case R_SH_GOTPLT_LOW16:
3711 case R_SH_GOTPLT_MEDLOW16:
3712 case R_SH_GOTPLT_MEDHI16:
3713 case R_SH_GOTPLT_HI16:
3714 case R_SH_GOTPLT10BY4:
3715 case R_SH_GOTPLT10BY8:
3716#endif
3717 /* Relocation is to the entry for this symbol in the
3718 procedure linkage table. */
3719
3720 if (h == NULL
f5385ebf 3721 || h->forced_local
fbca6ad9
AO
3722 || ! info->shared
3723 || info->symbolic
3724 || h->dynindx == -1
3725 || h->plt.offset == (bfd_vma) -1
3726 || h->got.offset != (bfd_vma) -1)
3727 goto force_got;
3728
3729 /* Relocation is to the entry for this symbol in the global
3730 offset table extension for the procedure linkage table. */
fbca6ad9 3731
067653c5 3732 BFD_ASSERT (sgotplt != NULL);
fbca6ad9 3733 relocation = (sgotplt->output_offset
55e6e397
RS
3734 + (get_plt_index (htab->plt_info, h->plt.offset)
3735 + 3) * 4);
fbca6ad9
AO
3736
3737#ifdef GOT_BIAS
3738 relocation -= GOT_BIAS;
3739#endif
3740
3741 goto final_link_relocate;
3742
3743 force_got:
37c644f2 3744 case R_SH_GOT32:
fbca6ad9
AO
3745#ifdef INCLUDE_SHMEDIA
3746 case R_SH_GOT_LOW16:
3747 case R_SH_GOT_MEDLOW16:
3748 case R_SH_GOT_MEDHI16:
3749 case R_SH_GOT_HI16:
3750 case R_SH_GOT10BY4:
3751 case R_SH_GOT10BY8:
3752#endif
37c644f2
AO
3753 /* Relocation is to the entry for this symbol in the global
3754 offset table. */
067653c5
AM
3755
3756 BFD_ASSERT (sgot != NULL);
37c644f2
AO
3757
3758 if (h != NULL)
3759 {
b34976b6 3760 bfd_boolean dyn;
37c644f2
AO
3761
3762 off = h->got.offset;
fbca6ad9
AO
3763#ifdef INCLUDE_SHMEDIA
3764 if (seen_stt_datalabel)
3765 {
3766 struct elf_sh_link_hash_entry *hsh;
3767
3768 hsh = (struct elf_sh_link_hash_entry *)h;
396a6083 3769 off = hsh->datalabel_got.offset;
fbca6ad9
AO
3770 }
3771#endif
37c644f2
AO
3772 BFD_ASSERT (off != (bfd_vma) -1);
3773
067653c5 3774 dyn = htab->root.dynamic_sections_created;
267fb3c1 3775 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
37c644f2 3776 || (info->shared
3d85aebe 3777 && SYMBOL_REFERENCES_LOCAL (info, h))
04e534c3
KK
3778 || (ELF_ST_VISIBILITY (h->other)
3779 && h->root.type == bfd_link_hash_undefweak))
37c644f2
AO
3780 {
3781 /* This is actually a static link, or it is a
3782 -Bsymbolic link and the symbol is defined
3783 locally, or the symbol was forced to be local
3784 because of a version file. We must initialize
3785 this entry in the global offset table. Since the
3786 offset must always be a multiple of 4, we use the
3787 least significant bit to record whether we have
3788 initialized it already.
3789
3790 When doing a dynamic link, we create a .rela.got
3791 relocation entry to initialize the value. This
3792 is done in the finish_dynamic_symbol routine. */
3793 if ((off & 1) != 0)
3794 off &= ~1;
3795 else
3796 {
3797 bfd_put_32 (output_bfd, relocation,
3798 sgot->contents + off);
fbca6ad9
AO
3799#ifdef INCLUDE_SHMEDIA
3800 if (seen_stt_datalabel)
3801 {
3802 struct elf_sh_link_hash_entry *hsh;
3803
3804 hsh = (struct elf_sh_link_hash_entry *)h;
396a6083 3805 hsh->datalabel_got.offset |= 1;
fbca6ad9
AO
3806 }
3807 else
3808#endif
3809 h->got.offset |= 1;
37c644f2
AO
3810 }
3811 }
3812
3813 relocation = sgot->output_offset + off;
3814 }
3815 else
3816 {
fbca6ad9
AO
3817#ifdef INCLUDE_SHMEDIA
3818 if (rel->r_addend)
3819 {
3820 BFD_ASSERT (local_got_offsets != NULL
3821 && (local_got_offsets[symtab_hdr->sh_info
3822 + r_symndx]
3823 != (bfd_vma) -1));
3824
3825 off = local_got_offsets[symtab_hdr->sh_info
3826 + r_symndx];
3827 }
3828 else
3829 {
3830#endif
37c644f2
AO
3831 BFD_ASSERT (local_got_offsets != NULL
3832 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3833
3834 off = local_got_offsets[r_symndx];
fbca6ad9
AO
3835#ifdef INCLUDE_SHMEDIA
3836 }
3837#endif
37c644f2
AO
3838
3839 /* The offset must always be a multiple of 4. We use
3840 the least significant bit to record whether we have
3841 already generated the necessary reloc. */
3842 if ((off & 1) != 0)
3843 off &= ~1;
3844 else
3845 {
3846 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3847
3848 if (info->shared)
3849 {
37c644f2 3850 Elf_Internal_Rela outrel;
947216bf 3851 bfd_byte *loc;
37c644f2 3852
3376eaf5
KK
3853 if (srelgot == NULL)
3854 {
3855 srelgot = bfd_get_section_by_name (dynobj,
3856 ".rela.got");
3857 BFD_ASSERT (srelgot != NULL);
3858 }
37c644f2
AO
3859
3860 outrel.r_offset = (sgot->output_section->vma
3861 + sgot->output_offset
3862 + off);
3863 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3864 outrel.r_addend = relocation;
947216bf
AM
3865 loc = srelgot->contents;
3866 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3867 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
37c644f2
AO
3868 }
3869
fbca6ad9
AO
3870#ifdef INCLUDE_SHMEDIA
3871 if (rel->r_addend)
3872 local_got_offsets[symtab_hdr->sh_info + r_symndx] |= 1;
3873 else
3874#endif
3875 local_got_offsets[r_symndx] |= 1;
37c644f2
AO
3876 }
3877
3878 relocation = sgot->output_offset + off;
3879 }
3880
fbca6ad9 3881#ifdef GOT_BIAS
067653c5 3882 relocation -= GOT_BIAS;
fbca6ad9
AO
3883#endif
3884
37c644f2
AO
3885 goto final_link_relocate;
3886
3887 case R_SH_GOTOFF:
fbca6ad9
AO
3888#ifdef INCLUDE_SHMEDIA
3889 case R_SH_GOTOFF_LOW16:
3890 case R_SH_GOTOFF_MEDLOW16:
3891 case R_SH_GOTOFF_MEDHI16:
3892 case R_SH_GOTOFF_HI16:
3893#endif
37c644f2
AO
3894 /* Relocation is relative to the start of the global offset
3895 table. */
3896
067653c5 3897 BFD_ASSERT (sgot != NULL);
37c644f2
AO
3898
3899 /* Note that sgot->output_offset is not involved in this
3900 calculation. We always want the start of .got. If we
3901 defined _GLOBAL_OFFSET_TABLE in a different way, as is
3902 permitted by the ABI, we might have to change this
3903 calculation. */
3904 relocation -= sgot->output_section->vma;
3905
fbca6ad9
AO
3906#ifdef GOT_BIAS
3907 relocation -= GOT_BIAS;
3908#endif
3909
3910 addend = rel->r_addend;
3911
37c644f2
AO
3912 goto final_link_relocate;
3913
3914 case R_SH_GOTPC:
fbca6ad9
AO
3915#ifdef INCLUDE_SHMEDIA
3916 case R_SH_GOTPC_LOW16:
3917 case R_SH_GOTPC_MEDLOW16:
3918 case R_SH_GOTPC_MEDHI16:
3919 case R_SH_GOTPC_HI16:
3920#endif
37c644f2
AO
3921 /* Use global offset table as symbol value. */
3922
067653c5 3923 BFD_ASSERT (sgot != NULL);
37c644f2
AO
3924 relocation = sgot->output_section->vma;
3925
fbca6ad9
AO
3926#ifdef GOT_BIAS
3927 relocation += GOT_BIAS;
3928#endif
3929
3930 addend = rel->r_addend;
3931
37c644f2
AO
3932 goto final_link_relocate;
3933
3934 case R_SH_PLT32:
fbca6ad9
AO
3935#ifdef INCLUDE_SHMEDIA
3936 case R_SH_PLT_LOW16:
3937 case R_SH_PLT_MEDLOW16:
3938 case R_SH_PLT_MEDHI16:
3939 case R_SH_PLT_HI16:
3940#endif
37c644f2
AO
3941 /* Relocation is to the entry for this symbol in the
3942 procedure linkage table. */
3943
3944 /* Resolve a PLT reloc against a local symbol directly,
3945 without using the procedure linkage table. */
3946 if (h == NULL)
3947 goto final_link_relocate;
3948
f5385ebf 3949 if (h->forced_local)
37c644f2
AO
3950 goto final_link_relocate;
3951
3952 if (h->plt.offset == (bfd_vma) -1)
3953 {
3954 /* We didn't make a PLT entry for this symbol. This
3955 happens when statically linking PIC code, or when
3956 using -Bsymbolic. */
3957 goto final_link_relocate;
3958 }
3959
067653c5 3960 BFD_ASSERT (splt != NULL);
37c644f2
AO
3961 relocation = (splt->output_section->vma
3962 + splt->output_offset
3963 + h->plt.offset);
3964
fbca6ad9
AO
3965#ifdef INCLUDE_SHMEDIA
3966 relocation++;
3967#endif
3968
3969 addend = rel->r_addend;
3970
37c644f2
AO
3971 goto final_link_relocate;
3972
3973 case R_SH_LOOP_START:
3974 {
3975 static bfd_vma start, end;
3976
3977 start = (relocation + rel->r_addend
3978 - (sec->output_section->vma + sec->output_offset));
3979 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3980 rel->r_offset, sec, start, end);
3981 break;
3982
3983 case R_SH_LOOP_END:
3984 end = (relocation + rel->r_addend
3985 - (sec->output_section->vma + sec->output_offset));
3986 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
3987 rel->r_offset, sec, start, end);
3988 break;
3989 }
3376eaf5
KK
3990
3991 case R_SH_TLS_GD_32:
3992 case R_SH_TLS_IE_32:
3993 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
3994 tls_type = GOT_UNKNOWN;
3995 if (h == NULL && local_got_offsets)
3996 tls_type = sh_elf_local_got_tls_type (input_bfd) [r_symndx];
3997 else if (h != NULL)
3998 {
3999 tls_type = sh_elf_hash_entry (h)->tls_type;
4000 if (! info->shared
4001 && (h->dynindx == -1
f5385ebf 4002 || h->def_regular))
267fb3c1 4003 r_type = R_SH_TLS_LE_32;
3376eaf5
KK
4004 }
4005
4006 if (r_type == R_SH_TLS_GD_32 && tls_type == GOT_TLS_IE)
4007 r_type = R_SH_TLS_IE_32;
4008
4009 if (r_type == R_SH_TLS_LE_32)
4010 {
4011 bfd_vma offset;
4012 unsigned short insn;
3376eaf5
KK
4013
4014 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
4015 {
4016 /* GD->LE transition:
4017 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
b34976b6 4018 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
3376eaf5
KK
4019 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4020 We change it into:
4021 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
4022 nop; nop; ...
4023 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
4024
4025 offset = rel->r_offset;
4026 BFD_ASSERT (offset >= 16);
4027 /* Size of GD instructions is 16 or 18. */
4028 offset -= 16;
4029 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4030 if ((insn & 0xff00) == 0xc700)
4031 {
4032 BFD_ASSERT (offset >= 2);
4033 offset -= 2;
4034 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4035 }
4036
4037 BFD_ASSERT ((insn & 0xff00) == 0xd400);
4038 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4039 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4040 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4041 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4042 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4043 BFD_ASSERT (insn == 0x310c);
4044 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4045 BFD_ASSERT (insn == 0x410b);
4046 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4047 BFD_ASSERT (insn == 0x34cc);
4048
4049 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
4050 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
4051 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4052 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4053 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4054 }
4055 else
4056 {
4057 int index;
4058
4059 /* IE->LE transition:
4060 mov.l 1f,r0; stc gbr,rN; mov.l @(r0,r12),rM;
4061 bra 2f; add ...; .align 2; 1: x@GOTTPOFF; 2:
4062 We change it into:
4063 mov.l .Ln,rM; stc gbr,rN; nop; ...;
4064 1: x@TPOFF; 2:. */
4065
4066 offset = rel->r_offset;
4067 BFD_ASSERT (offset >= 16);
4068 /* Size of IE instructions is 10 or 12. */
4069 offset -= 10;
4070 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4071 if ((insn & 0xf0ff) == 0x0012)
4072 {
4073 BFD_ASSERT (offset >= 2);
4074 offset -= 2;
4075 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4076 }
4077
4078 BFD_ASSERT ((insn & 0xff00) == 0xd000);
4079 index = insn & 0x00ff;
4080 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4081 BFD_ASSERT ((insn & 0xf0ff) == 0x0012);
4082 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4083 BFD_ASSERT ((insn & 0xf0ff) == 0x00ce);
4084 insn = 0xd000 | (insn & 0x0f00) | index;
4085 bfd_put_16 (output_bfd, insn, contents + offset + 0);
4086 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4087 }
4088
267fb3c1
KK
4089 bfd_put_32 (output_bfd, tpoff (info, relocation),
4090 contents + rel->r_offset);
3376eaf5
KK
4091 continue;
4092 }
4093
4094 sgot = htab->sgot;
4095 if (sgot == NULL)
4096 abort ();
4097
4098 if (h != NULL)
4099 off = h->got.offset;
4100 else
4101 {
4102 if (local_got_offsets == NULL)
4103 abort ();
4104
4105 off = local_got_offsets[r_symndx];
4106 }
4107
267fb3c1
KK
4108 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4109 if (r_type == R_SH_TLS_IE_32
4110 && ! htab->root.dynamic_sections_created)
4111 {
4112 off &= ~1;
4113 bfd_put_32 (output_bfd, tpoff (info, relocation),
4114 sgot->contents + off);
4115 bfd_put_32 (output_bfd, sgot->output_offset + off,
4116 contents + rel->r_offset);
4117 continue;
4118 }
4119
3376eaf5
KK
4120 if ((off & 1) != 0)
4121 off &= ~1;
26e41594 4122 else
3376eaf5
KK
4123 {
4124 Elf_Internal_Rela outrel;
947216bf 4125 bfd_byte *loc;
3376eaf5
KK
4126 int dr_type, indx;
4127
4128 if (srelgot == NULL)
4129 {
4130 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4131 BFD_ASSERT (srelgot != NULL);
4132 }
4133
4134 outrel.r_offset = (sgot->output_section->vma
4135 + sgot->output_offset + off);
4136
267fb3c1 4137 if (h == NULL || h->dynindx == -1)
c95b8a7a
KK
4138 indx = 0;
4139 else
4140 indx = h->dynindx;
267fb3c1 4141
3376eaf5
KK
4142 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
4143 R_SH_TLS_TPOFF32);
4144 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
4145 outrel.r_addend = relocation - dtpoff_base (info);
4146 else
4147 outrel.r_addend = 0;
4148 outrel.r_info = ELF32_R_INFO (indx, dr_type);
947216bf
AM
4149 loc = srelgot->contents;
4150 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3376eaf5
KK
4151 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4152
4153 if (r_type == R_SH_TLS_GD_32)
4154 {
4155 if (indx == 0)
4156 {
4157 bfd_put_32 (output_bfd,
4158 relocation - dtpoff_base (info),
4159 sgot->contents + off + 4);
4160 }
4161 else
4162 {
4163 outrel.r_info = ELF32_R_INFO (indx,
4164 R_SH_TLS_DTPOFF32);
4165 outrel.r_offset += 4;
4166 outrel.r_addend = 0;
4167 srelgot->reloc_count++;
947216bf
AM
4168 loc += sizeof (Elf32_External_Rela);
4169 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3376eaf5
KK
4170 }
4171 }
4172
4173 if (h != NULL)
4174 h->got.offset |= 1;
4175 else
4176 local_got_offsets[r_symndx] |= 1;
4177 }
4178
4179 if (off >= (bfd_vma) -2)
4180 abort ();
4181
4182 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
4183 relocation = sgot->output_offset + off;
4184 else
4185 {
4186 bfd_vma offset;
4187 unsigned short insn;
4188
4189 /* GD->IE transition:
4190 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4191 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4192 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4193 We change it into:
4194 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4195 nop; nop; bra 3f; nop; .align 2;
4196 1: .long x@TPOFF; 2:...; 3:. */
4197
4198 offset = rel->r_offset;
4199 BFD_ASSERT (offset >= 16);
4200 /* Size of GD instructions is 16 or 18. */
4201 offset -= 16;
4202 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4203 if ((insn & 0xff00) == 0xc700)
4204 {
4205 BFD_ASSERT (offset >= 2);
4206 offset -= 2;
4207 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4208 }
4209
4210 BFD_ASSERT ((insn & 0xff00) == 0xd400);
4211
4212 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4213 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
4214
4215 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4216 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4217 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4218 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4219 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4220 BFD_ASSERT (insn == 0x310c);
4221 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4222 BFD_ASSERT (insn == 0x410b);
4223 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4224 BFD_ASSERT (insn == 0x34cc);
4225
4226 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
4227 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
4228 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
4229 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4230 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4231
4232 bfd_put_32 (output_bfd, sgot->output_offset + off,
4233 contents + rel->r_offset);
4234
4235 continue;
4236 }
4237
4238 addend = rel->r_addend;
4239
4240 goto final_link_relocate;
4241
4242 case R_SH_TLS_LD_32:
4243 if (! info->shared)
4244 {
4245 bfd_vma offset;
4246 unsigned short insn;
4247
4248 /* LD->LE transition:
4249 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4250 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4251 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4252 We change it into:
4253 stc gbr,r0; nop; nop; nop;
4254 nop; nop; bra 3f; ...; 3:. */
4255
4256 offset = rel->r_offset;
4257 BFD_ASSERT (offset >= 16);
4258 /* Size of LD instructions is 16 or 18. */
4259 offset -= 16;
4260 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4261 if ((insn & 0xff00) == 0xc700)
4262 {
4263 BFD_ASSERT (offset >= 2);
4264 offset -= 2;
4265 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4266 }
4267
4268 BFD_ASSERT ((insn & 0xff00) == 0xd400);
4269 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4270 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4271 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4272 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4273 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4274 BFD_ASSERT (insn == 0x310c);
4275 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4276 BFD_ASSERT (insn == 0x410b);
4277 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4278 BFD_ASSERT (insn == 0x34cc);
4279
4280 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
4281 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
4282 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4283 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4284 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4285 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4286
4287 continue;
4288 }
4289
4290 sgot = htab->sgot;
4291 if (sgot == NULL)
4292 abort ();
4293
4294 off = htab->tls_ldm_got.offset;
4295 if (off & 1)
4296 off &= ~1;
4297 else
4298 {
4299 Elf_Internal_Rela outrel;
947216bf 4300 bfd_byte *loc;
3376eaf5
KK
4301
4302 srelgot = htab->srelgot;
4303 if (srelgot == NULL)
4304 abort ();
4305
4306 outrel.r_offset = (sgot->output_section->vma
4307 + sgot->output_offset + off);
4308 outrel.r_addend = 0;
4309 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
947216bf
AM
4310 loc = srelgot->contents;
4311 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3376eaf5
KK
4312 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4313 htab->tls_ldm_got.offset |= 1;
4314 }
4315
4316 relocation = sgot->output_offset + off;
4317 addend = rel->r_addend;
4318
4319 goto final_link_relocate;
4320
4321 case R_SH_TLS_LDO_32:
4322 if (! info->shared)
267fb3c1 4323 relocation = tpoff (info, relocation);
3376eaf5
KK
4324 else
4325 relocation -= dtpoff_base (info);
4326
4327 addend = rel->r_addend;
4328 goto final_link_relocate;
4329
4330 case R_SH_TLS_LE_32:
4331 {
4332 int indx;
4333 Elf_Internal_Rela outrel;
947216bf 4334 bfd_byte *loc;
3376eaf5 4335
267fb3c1
KK
4336 if (! info->shared)
4337 {
4338 relocation = tpoff (info, relocation);
4339 addend = rel->r_addend;
4340 goto final_link_relocate;
4341 }
4342
3376eaf5
KK
4343 if (sreloc == NULL)
4344 {
4345 const char *name;
4346
4347 name = (bfd_elf_string_from_elf_section
4348 (input_bfd,
4349 elf_elfheader (input_bfd)->e_shstrndx,
4350 elf_section_data (input_section)->rel_hdr.sh_name));
4351 if (name == NULL)
b34976b6 4352 return FALSE;
3376eaf5 4353
0112cd26 4354 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
3376eaf5
KK
4355 && strcmp (bfd_get_section_name (input_bfd,
4356 input_section),
4357 name + 5) == 0);
4358
4359 sreloc = bfd_get_section_by_name (dynobj, name);
4360 BFD_ASSERT (sreloc != NULL);
4361 }
4362
267fb3c1
KK
4363 if (h == NULL || h->dynindx == -1)
4364 indx = 0;
4365 else
4366 indx = h->dynindx;
4367
3376eaf5
KK
4368 outrel.r_offset = (input_section->output_section->vma
4369 + input_section->output_offset
4370 + rel->r_offset);
4371 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
4372 if (indx == 0)
4373 outrel.r_addend = relocation - dtpoff_base (info);
4374 else
4375 outrel.r_addend = 0;
3376eaf5 4376
947216bf
AM
4377 loc = sreloc->contents;
4378 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4379 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3376eaf5
KK
4380 continue;
4381 }
37c644f2
AO
4382 }
4383
146be91a 4384 relocation_done:
37c644f2
AO
4385 if (r != bfd_reloc_ok)
4386 {
4387 switch (r)
4388 {
4389 default:
4390 case bfd_reloc_outofrange:
4391 abort ();
4392 case bfd_reloc_overflow:
4393 {
4394 const char *name;
4395
4396 if (h != NULL)
dfeffb9f 4397 name = NULL;
37c644f2
AO
4398 else
4399 {
252b5132
RH
4400 name = (bfd_elf_string_from_elf_section
4401 (input_bfd, symtab_hdr->sh_link, sym->st_name));
4402 if (name == NULL)
b34976b6 4403 return FALSE;
252b5132
RH
4404 if (*name == '\0')
4405 name = bfd_section_name (input_bfd, sec);
4406 }
4407 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
4408 (info, (h ? &h->root : NULL), name, howto->name,
4409 (bfd_vma) 0, input_bfd, input_section,
4410 rel->r_offset)))
b34976b6 4411 return FALSE;
252b5132
RH
4412 }
4413 break;
4414 }
4415 }
4416 }
4417
b34976b6 4418 return TRUE;
252b5132
RH
4419}
4420
4421/* This is a version of bfd_generic_get_relocated_section_contents
4422 which uses sh_elf_relocate_section. */
4423
4424static bfd_byte *
09fd220b
KK
4425sh_elf_get_relocated_section_contents (bfd *output_bfd,
4426 struct bfd_link_info *link_info,
4427 struct bfd_link_order *link_order,
4428 bfd_byte *data,
4429 bfd_boolean relocatable,
4430 asymbol **symbols)
252b5132
RH
4431{
4432 Elf_Internal_Shdr *symtab_hdr;
4433 asection *input_section = link_order->u.indirect.section;
4434 bfd *input_bfd = input_section->owner;
4435 asection **sections = NULL;
4436 Elf_Internal_Rela *internal_relocs = NULL;
6cdc0ccc 4437 Elf_Internal_Sym *isymbuf = NULL;
252b5132
RH
4438
4439 /* We only need to handle the case of relaxing, or of having a
4440 particular set of section contents, specially. */
1049f94e 4441 if (relocatable
252b5132
RH
4442 || elf_section_data (input_section)->this_hdr.contents == NULL)
4443 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4444 link_order, data,
1049f94e 4445 relocatable,
252b5132
RH
4446 symbols);
4447
4448 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4449
4450 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
eea6121a 4451 (size_t) input_section->size);
252b5132
RH
4452
4453 if ((input_section->flags & SEC_RELOC) != 0
4454 && input_section->reloc_count > 0)
4455 {
252b5132 4456 asection **secpp;
6cdc0ccc 4457 Elf_Internal_Sym *isym, *isymend;
9ad5cbcf 4458 bfd_size_type amt;
252b5132 4459
45d6a902 4460 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 4461 (input_bfd, input_section, NULL,
b34976b6 4462 (Elf_Internal_Rela *) NULL, FALSE));
252b5132
RH
4463 if (internal_relocs == NULL)
4464 goto error_return;
4465
6cdc0ccc
AM
4466 if (symtab_hdr->sh_info != 0)
4467 {
4468 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4469 if (isymbuf == NULL)
4470 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4471 symtab_hdr->sh_info, 0,
4472 NULL, NULL, NULL);
4473 if (isymbuf == NULL)
4474 goto error_return;
4475 }
252b5132 4476
9ad5cbcf
AM
4477 amt = symtab_hdr->sh_info;
4478 amt *= sizeof (asection *);
4479 sections = (asection **) bfd_malloc (amt);
4480 if (sections == NULL && amt != 0)
252b5132
RH
4481 goto error_return;
4482
6cdc0ccc
AM
4483 isymend = isymbuf + symtab_hdr->sh_info;
4484 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
252b5132
RH
4485 {
4486 asection *isec;
4487
6cdc0ccc 4488 if (isym->st_shndx == SHN_UNDEF)
252b5132 4489 isec = bfd_und_section_ptr;
6cdc0ccc 4490 else if (isym->st_shndx == SHN_ABS)
252b5132 4491 isec = bfd_abs_section_ptr;
6cdc0ccc 4492 else if (isym->st_shndx == SHN_COMMON)
252b5132
RH
4493 isec = bfd_com_section_ptr;
4494 else
6cdc0ccc 4495 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
252b5132
RH
4496
4497 *secpp = isec;
4498 }
4499
4500 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
4501 input_section, data, internal_relocs,
6cdc0ccc 4502 isymbuf, sections))
252b5132
RH
4503 goto error_return;
4504
4505 if (sections != NULL)
4506 free (sections);
6cdc0ccc
AM
4507 if (isymbuf != NULL
4508 && symtab_hdr->contents != (unsigned char *) isymbuf)
4509 free (isymbuf);
4510 if (elf_section_data (input_section)->relocs != internal_relocs)
252b5132 4511 free (internal_relocs);
252b5132
RH
4512 }
4513
4514 return data;
4515
4516 error_return:
252b5132
RH
4517 if (sections != NULL)
4518 free (sections);
6cdc0ccc
AM
4519 if (isymbuf != NULL
4520 && symtab_hdr->contents != (unsigned char *) isymbuf)
4521 free (isymbuf);
4522 if (internal_relocs != NULL
4523 && elf_section_data (input_section)->relocs != internal_relocs)
4524 free (internal_relocs);
252b5132
RH
4525 return NULL;
4526}
917583ad 4527
3376eaf5
KK
4528/* Return the base VMA address which should be subtracted from real addresses
4529 when resolving @dtpoff relocation.
4530 This is PT_TLS segment p_vaddr. */
4531
4532static bfd_vma
09fd220b 4533dtpoff_base (struct bfd_link_info *info)
3376eaf5 4534{
e1918d23
AM
4535 /* If tls_sec is NULL, we should have signalled an error already. */
4536 if (elf_hash_table (info)->tls_sec == NULL)
267fb3c1 4537 return 0;
e1918d23 4538 return elf_hash_table (info)->tls_sec->vma;
3376eaf5
KK
4539}
4540
267fb3c1
KK
4541/* Return the relocation value for R_SH_TLS_TPOFF32.. */
4542
4543static bfd_vma
09fd220b 4544tpoff (struct bfd_link_info *info, bfd_vma address)
267fb3c1 4545{
e1918d23
AM
4546 /* If tls_sec is NULL, we should have signalled an error already. */
4547 if (elf_hash_table (info)->tls_sec == NULL)
267fb3c1
KK
4548 return 0;
4549 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
4550 structure which has 2 pointer fields. */
044ad7e3
KK
4551 return (address - elf_hash_table (info)->tls_sec->vma
4552 + align_power ((bfd_vma) 8,
4553 elf_hash_table (info)->tls_sec->alignment_power));
267fb3c1
KK
4554}
4555
252b5132 4556static asection *
09fd220b 4557sh_elf_gc_mark_hook (asection *sec,
07adf181
AM
4558 struct bfd_link_info *info,
4559 Elf_Internal_Rela *rel,
4560 struct elf_link_hash_entry *h,
09fd220b 4561 Elf_Internal_Sym *sym)
252b5132
RH
4562{
4563 if (h != NULL)
07adf181
AM
4564 switch (ELF32_R_TYPE (rel->r_info))
4565 {
4566 case R_SH_GNU_VTINHERIT:
4567 case R_SH_GNU_VTENTRY:
4568 return NULL;
4569 }
9ad5cbcf 4570
07adf181 4571 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
252b5132
RH
4572}
4573
37c644f2
AO
4574/* Update the got entry reference counts for the section being removed. */
4575
b34976b6 4576static bfd_boolean
09fd220b
KK
4577sh_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
4578 asection *sec, const Elf_Internal_Rela *relocs)
252b5132 4579{
067653c5
AM
4580 Elf_Internal_Shdr *symtab_hdr;
4581 struct elf_link_hash_entry **sym_hashes;
4582 bfd_signed_vma *local_got_refcounts;
4583 const Elf_Internal_Rela *rel, *relend;
067653c5
AM
4584
4585 elf_section_data (sec)->local_dynrel = NULL;
4586
4587 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4588 sym_hashes = elf_sym_hashes (abfd);
4589 local_got_refcounts = elf_local_got_refcounts (abfd);
4590
4591 relend = relocs + sec->reloc_count;
4592 for (rel = relocs; rel < relend; rel++)
396a6083 4593 {
26e41594
AM
4594 unsigned long r_symndx;
4595 unsigned int r_type;
4596 struct elf_link_hash_entry *h = NULL;
396a6083
SC
4597#ifdef INCLUDE_SHMEDIA
4598 int seen_stt_datalabel = 0;
4599#endif
3376eaf5 4600
396a6083 4601 r_symndx = ELF32_R_SYM (rel->r_info);
26e41594 4602 if (r_symndx >= symtab_hdr->sh_info)
396a6083 4603 {
26e41594
AM
4604 struct elf_sh_link_hash_entry *eh;
4605 struct elf_sh_dyn_relocs **pp;
4606 struct elf_sh_dyn_relocs *p;
4607
396a6083 4608 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
396a6083
SC
4609 while (h->root.type == bfd_link_hash_indirect
4610 || h->root.type == bfd_link_hash_warning)
4611 {
3eb128b2 4612#ifdef INCLUDE_SHMEDIA
396a6083 4613 seen_stt_datalabel |= h->type == STT_DATALABEL;
3eb128b2 4614#endif
396a6083
SC
4615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4616 }
26e41594
AM
4617 eh = (struct elf_sh_link_hash_entry *) h;
4618 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4619 if (p->sec == sec)
4620 {
4621 /* Everything must go for SEC. */
4622 *pp = p->next;
4623 break;
4624 }
396a6083 4625 }
067653c5 4626
26e41594
AM
4627 r_type = ELF32_R_TYPE (rel->r_info);
4628 switch (sh_elf_optimized_tls_reloc (info, r_type, h != NULL))
396a6083
SC
4629 {
4630 case R_SH_TLS_LD_32:
4631 if (sh_elf_hash_table (info)->tls_ldm_got.refcount > 0)
4632 sh_elf_hash_table (info)->tls_ldm_got.refcount -= 1;
4633 break;
4634
4635 case R_SH_GOT32:
4636 case R_SH_GOTOFF:
4637 case R_SH_GOTPC:
4638#ifdef INCLUDE_SHMEDIA
4639 case R_SH_GOT_LOW16:
4640 case R_SH_GOT_MEDLOW16:
4641 case R_SH_GOT_MEDHI16:
4642 case R_SH_GOT_HI16:
4643 case R_SH_GOT10BY4:
4644 case R_SH_GOT10BY8:
4645 case R_SH_GOTOFF_LOW16:
4646 case R_SH_GOTOFF_MEDLOW16:
4647 case R_SH_GOTOFF_MEDHI16:
4648 case R_SH_GOTOFF_HI16:
4649 case R_SH_GOTPC_LOW16:
4650 case R_SH_GOTPC_MEDLOW16:
4651 case R_SH_GOTPC_MEDHI16:
4652 case R_SH_GOTPC_HI16:
4653#endif
4654 case R_SH_TLS_GD_32:
4655 case R_SH_TLS_IE_32:
4656 if (h != NULL)
4657 {
4658#ifdef INCLUDE_SHMEDIA
4659 if (seen_stt_datalabel)
4660 {
26e41594
AM
4661 struct elf_sh_link_hash_entry *eh;
4662 eh = (struct elf_sh_link_hash_entry *) h;
396a6083
SC
4663 if (eh->datalabel_got.refcount > 0)
4664 eh->datalabel_got.refcount -= 1;
4665 }
4666 else
4667#endif
4668 if (h->got.refcount > 0)
4669 h->got.refcount -= 1;
4670 }
4671 else if (local_got_refcounts != NULL)
4672 {
4673#ifdef INCLUDE_SHMEDIA
4674 if (rel->r_addend & 1)
4675 {
4676 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
4677 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
4678 }
4679 else
4680#endif
4681 if (local_got_refcounts[r_symndx] > 0)
4682 local_got_refcounts[r_symndx] -= 1;
4683 }
4684 break;
4685
4686 case R_SH_DIR32:
4687 case R_SH_REL32:
26e41594
AM
4688 if (info->shared)
4689 break;
4690 /* Fall thru */
396a6083
SC
4691
4692 case R_SH_PLT32:
4693#ifdef INCLUDE_SHMEDIA
4694 case R_SH_PLT_LOW16:
4695 case R_SH_PLT_MEDLOW16:
4696 case R_SH_PLT_MEDHI16:
4697 case R_SH_PLT_HI16:
4698#endif
4699 if (h != NULL)
4700 {
4701 if (h->plt.refcount > 0)
4702 h->plt.refcount -= 1;
4703 }
4704 break;
067653c5 4705
396a6083
SC
4706 case R_SH_GOTPLT32:
4707#ifdef INCLUDE_SHMEDIA
4708 case R_SH_GOTPLT_LOW16:
4709 case R_SH_GOTPLT_MEDLOW16:
4710 case R_SH_GOTPLT_MEDHI16:
4711 case R_SH_GOTPLT_HI16:
4712 case R_SH_GOTPLT10BY4:
4713 case R_SH_GOTPLT10BY8:
4714#endif
4715 if (h != NULL)
4716 {
26e41594
AM
4717 struct elf_sh_link_hash_entry *eh;
4718 eh = (struct elf_sh_link_hash_entry *) h;
396a6083 4719 if (eh->gotplt_refcount > 0)
067653c5 4720 {
396a6083
SC
4721 eh->gotplt_refcount -= 1;
4722 if (h->plt.refcount > 0)
4723 h->plt.refcount -= 1;
067653c5 4724 }
067653c5 4725#ifdef INCLUDE_SHMEDIA
396a6083
SC
4726 else if (seen_stt_datalabel)
4727 {
4728 if (eh->datalabel_got.refcount > 0)
4729 eh->datalabel_got.refcount -= 1;
4730 }
067653c5 4731#endif
396a6083
SC
4732 else if (h->got.refcount > 0)
4733 h->got.refcount -= 1;
4734 }
4735 else if (local_got_refcounts != NULL)
4736 {
067653c5 4737#ifdef INCLUDE_SHMEDIA
396a6083
SC
4738 if (rel->r_addend & 1)
4739 {
4740 if (local_got_refcounts[symtab_hdr->sh_info + r_symndx] > 0)
4741 local_got_refcounts[symtab_hdr->sh_info + r_symndx] -= 1;
4742 }
4743 else
067653c5 4744#endif
396a6083
SC
4745 if (local_got_refcounts[r_symndx] > 0)
4746 local_got_refcounts[r_symndx] -= 1;
4747 }
4748 break;
067653c5 4749
396a6083
SC
4750 default:
4751 break;
4752 }
4753 }
067653c5 4754
b34976b6 4755 return TRUE;
252b5132
RH
4756}
4757
067653c5
AM
4758/* Copy the extra info we tack onto an elf_link_hash_entry. */
4759
4760static void
fcfa13d2 4761sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
09fd220b
KK
4762 struct elf_link_hash_entry *dir,
4763 struct elf_link_hash_entry *ind)
067653c5
AM
4764{
4765 struct elf_sh_link_hash_entry *edir, *eind;
4766
4767 edir = (struct elf_sh_link_hash_entry *) dir;
4768 eind = (struct elf_sh_link_hash_entry *) ind;
4769
4770 if (eind->dyn_relocs != NULL)
4771 {
4772 if (edir->dyn_relocs != NULL)
4773 {
4774 struct elf_sh_dyn_relocs **pp;
4775 struct elf_sh_dyn_relocs *p;
4776
fcfa13d2 4777 /* Add reloc counts against the indirect sym to the direct sym
067653c5
AM
4778 list. Merge any entries against the same section. */
4779 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4780 {
4781 struct elf_sh_dyn_relocs *q;
4782
4783 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4784 if (q->sec == p->sec)
4785 {
4786 q->pc_count += p->pc_count;
4787 q->count += p->count;
4788 *pp = p->next;
4789 break;
4790 }
4791 if (q == NULL)
4792 pp = &p->next;
4793 }
4794 *pp = edir->dyn_relocs;
4795 }
4796
4797 edir->dyn_relocs = eind->dyn_relocs;
4798 eind->dyn_relocs = NULL;
4799 }
4989d792
SC
4800 edir->gotplt_refcount = eind->gotplt_refcount;
4801 eind->gotplt_refcount = 0;
396a6083 4802#ifdef INCLUDE_SHMEDIA
fcfa13d2
AM
4803 edir->datalabel_got.refcount += eind->datalabel_got.refcount;
4804 eind->datalabel_got.refcount = 0;
396a6083 4805#endif
067653c5 4806
3376eaf5
KK
4807 if (ind->root.type == bfd_link_hash_indirect
4808 && dir->got.refcount <= 0)
4809 {
4810 edir->tls_type = eind->tls_type;
4811 eind->tls_type = GOT_UNKNOWN;
4812 }
4813
04e534c3 4814 if (ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
4815 && dir->dynamic_adjusted)
4816 {
4817 /* If called to transfer flags for a weakdef during processing
4818 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
4819 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4820 dir->ref_dynamic |= ind->ref_dynamic;
4821 dir->ref_regular |= ind->ref_regular;
4822 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
4823 dir->needs_plt |= ind->needs_plt;
4824 }
04e534c3 4825 else
fcfa13d2 4826 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
067653c5
AM
4827}
4828
3376eaf5 4829static int
09fd220b
KK
4830sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
4831 int is_local)
3376eaf5
KK
4832{
4833 if (info->shared)
4834 return r_type;
4835
4836 switch (r_type)
4837 {
4838 case R_SH_TLS_GD_32:
4839 case R_SH_TLS_IE_32:
4840 if (is_local)
4841 return R_SH_TLS_LE_32;
4842 return R_SH_TLS_IE_32;
4843 case R_SH_TLS_LD_32:
4844 return R_SH_TLS_LE_32;
4845 }
4846
4847 return r_type;
4848}
4849
252b5132
RH
4850/* Look through the relocs for a section during the first phase.
4851 Since we don't do .gots or .plts, we just need to consider the
4852 virtual table relocs for gc. */
435b1e90 4853
b34976b6 4854static bfd_boolean
09fd220b
KK
4855sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
4856 const Elf_Internal_Rela *relocs)
252b5132
RH
4857{
4858 Elf_Internal_Shdr *symtab_hdr;
4859 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
067653c5 4860 struct elf_sh_link_hash_table *htab;
252b5132
RH
4861 const Elf_Internal_Rela *rel;
4862 const Elf_Internal_Rela *rel_end;
37c644f2
AO
4863 bfd_vma *local_got_offsets;
4864 asection *sgot;
4865 asection *srelgot;
4866 asection *sreloc;
3376eaf5
KK
4867 unsigned int r_type;
4868 int tls_type, old_tls_type;
435b1e90 4869
37c644f2
AO
4870 sgot = NULL;
4871 srelgot = NULL;
4872 sreloc = NULL;
4873
1049f94e 4874 if (info->relocatable)
b34976b6 4875 return TRUE;
435b1e90 4876
252b5132
RH
4877 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4878 sym_hashes = elf_sym_hashes (abfd);
435b1e90 4879 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
252b5132
RH
4880 if (!elf_bad_symtab (abfd))
4881 sym_hashes_end -= symtab_hdr->sh_info;
435b1e90 4882
067653c5 4883 htab = sh_elf_hash_table (info);
37c644f2
AO
4884 local_got_offsets = elf_local_got_offsets (abfd);
4885
252b5132
RH
4886 rel_end = relocs + sec->reloc_count;
4887 for (rel = relocs; rel < rel_end; rel++)
4888 {
4889 struct elf_link_hash_entry *h;
4890 unsigned long r_symndx;
396a6083
SC
4891#ifdef INCLUDE_SHMEDIA
4892 int seen_stt_datalabel = 0;
4893#endif
435b1e90 4894
252b5132 4895 r_symndx = ELF32_R_SYM (rel->r_info);
3376eaf5
KK
4896 r_type = ELF32_R_TYPE (rel->r_info);
4897
252b5132 4898 if (r_symndx < symtab_hdr->sh_info)
435b1e90 4899 h = NULL;
252b5132 4900 else
396a6083
SC
4901 {
4902 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
396a6083
SC
4903 while (h->root.type == bfd_link_hash_indirect
4904 || h->root.type == bfd_link_hash_warning)
4905 {
973a3492 4906#ifdef INCLUDE_SHMEDIA
396a6083 4907 seen_stt_datalabel |= h->type == STT_DATALABEL;
973a3492 4908#endif
396a6083
SC
4909 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4910 }
396a6083 4911 }
435b1e90 4912
3376eaf5
KK
4913 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
4914 if (! info->shared
4915 && r_type == R_SH_TLS_IE_32
4916 && h != NULL
4917 && h->root.type != bfd_link_hash_undefined
4918 && h->root.type != bfd_link_hash_undefweak
4919 && (h->dynindx == -1
f5385ebf 4920 || h->def_regular))
3376eaf5
KK
4921 r_type = R_SH_TLS_LE_32;
4922
37c644f2 4923 /* Some relocs require a global offset table. */
067653c5 4924 if (htab->sgot == NULL)
37c644f2 4925 {
3376eaf5 4926 switch (r_type)
37c644f2 4927 {
fbca6ad9 4928 case R_SH_GOTPLT32:
37c644f2
AO
4929 case R_SH_GOT32:
4930 case R_SH_GOTOFF:
4931 case R_SH_GOTPC:
fbca6ad9
AO
4932#ifdef INCLUDE_SHMEDIA
4933 case R_SH_GOTPLT_LOW16:
4934 case R_SH_GOTPLT_MEDLOW16:
4935 case R_SH_GOTPLT_MEDHI16:
4936 case R_SH_GOTPLT_HI16:
4937 case R_SH_GOTPLT10BY4:
4938 case R_SH_GOTPLT10BY8:
4939 case R_SH_GOT_LOW16:
4940 case R_SH_GOT_MEDLOW16:
4941 case R_SH_GOT_MEDHI16:
4942 case R_SH_GOT_HI16:
4943 case R_SH_GOT10BY4:
4944 case R_SH_GOT10BY8:
4945 case R_SH_GOTOFF_LOW16:
4946 case R_SH_GOTOFF_MEDLOW16:
4947 case R_SH_GOTOFF_MEDHI16:
4948 case R_SH_GOTOFF_HI16:
4949 case R_SH_GOTPC_LOW16:
4950 case R_SH_GOTPC_MEDLOW16:
4951 case R_SH_GOTPC_MEDHI16:
4952 case R_SH_GOTPC_HI16:
4953#endif
3376eaf5
KK
4954 case R_SH_TLS_GD_32:
4955 case R_SH_TLS_LD_32:
4956 case R_SH_TLS_IE_32:
2293c499
AM
4957 if (htab->sgot == NULL)
4958 {
4959 if (htab->root.dynobj == NULL)
4960 htab->root.dynobj = abfd;
4961 if (!create_got_section (htab->root.dynobj, info))
4962 return FALSE;
4963 }
37c644f2
AO
4964 break;
4965
4966 default:
4967 break;
4968 }
4969 }
4970
3376eaf5 4971 switch (r_type)
067653c5
AM
4972 {
4973 /* This relocation describes the C++ object vtable hierarchy.
4974 Reconstruct it for later use during GC. */
435b1e90 4975 case R_SH_GNU_VTINHERIT:
c152c796 4976 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4977 return FALSE;
435b1e90
KH
4978 break;
4979
067653c5
AM
4980 /* This relocation describes which C++ vtable entries are actually
4981 used. Record for later use during GC. */
435b1e90 4982 case R_SH_GNU_VTENTRY:
c152c796 4983 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4984 return FALSE;
435b1e90 4985 break;
37c644f2 4986
3376eaf5
KK
4987 case R_SH_TLS_IE_32:
4988 if (info->shared)
4989 info->flags |= DF_STATIC_TLS;
4990
4991 /* FALLTHROUGH */
267fb3c1 4992 force_got:
3376eaf5 4993 case R_SH_TLS_GD_32:
37c644f2 4994 case R_SH_GOT32:
fbca6ad9
AO
4995#ifdef INCLUDE_SHMEDIA
4996 case R_SH_GOT_LOW16:
4997 case R_SH_GOT_MEDLOW16:
4998 case R_SH_GOT_MEDHI16:
4999 case R_SH_GOT_HI16:
5000 case R_SH_GOT10BY4:
5001 case R_SH_GOT10BY8:
5002#endif
3376eaf5
KK
5003 switch (r_type)
5004 {
5005 default:
5006 tls_type = GOT_NORMAL;
5007 break;
5008 case R_SH_TLS_GD_32:
5009 tls_type = GOT_TLS_GD;
5010 break;
5011 case R_SH_TLS_IE_32:
5012 tls_type = GOT_TLS_IE;
5013 break;
5014 }
5015
37c644f2 5016 if (h != NULL)
3376eaf5 5017 {
396a6083
SC
5018#ifdef INCLUDE_SHMEDIA
5019 if (seen_stt_datalabel)
5020 {
b34976b6
AM
5021 struct elf_sh_link_hash_entry *eh
5022 = (struct elf_sh_link_hash_entry *) h;
396a6083
SC
5023
5024 eh->datalabel_got.refcount += 1;
5025 }
5026 else
5027#endif
5028 h->got.refcount += 1;
3376eaf5
KK
5029 old_tls_type = sh_elf_hash_entry (h)->tls_type;
5030 }
37c644f2
AO
5031 else
5032 {
067653c5
AM
5033 bfd_signed_vma *local_got_refcounts;
5034
435b1e90 5035 /* This is a global offset table entry for a local
067653c5
AM
5036 symbol. */
5037 local_got_refcounts = elf_local_got_refcounts (abfd);
5038 if (local_got_refcounts == NULL)
37c644f2 5039 {
dc810e39 5040 bfd_size_type size;
37c644f2 5041
dc810e39 5042 size = symtab_hdr->sh_info;
067653c5 5043 size *= sizeof (bfd_signed_vma);
fbca6ad9
AO
5044#ifdef INCLUDE_SHMEDIA
5045 /* Reserve space for both the datalabel and
5046 codelabel local GOT offsets. */
5047 size *= 2;
5048#endif
3376eaf5 5049 size += symtab_hdr->sh_info;
067653c5
AM
5050 local_got_refcounts = ((bfd_signed_vma *)
5051 bfd_zalloc (abfd, size));
5052 if (local_got_refcounts == NULL)
b34976b6 5053 return FALSE;
067653c5 5054 elf_local_got_refcounts (abfd) = local_got_refcounts;
3376eaf5
KK
5055#ifdef INCLUDE_SHMEDIA
5056 /* Take care of both the datalabel and codelabel local
5057 GOT offsets. */
5058 sh_elf_local_got_tls_type (abfd)
5059 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
5060#else
5061 sh_elf_local_got_tls_type (abfd)
5062 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
5063#endif
37c644f2 5064 }
396a6083
SC
5065#ifdef INCLUDE_SHMEDIA
5066 if (rel->r_addend & 1)
5067 local_got_refcounts[symtab_hdr->sh_info + r_symndx] += 1;
5068 else
5069#endif
5070 local_got_refcounts[r_symndx] += 1;
3376eaf5
KK
5071 old_tls_type = sh_elf_local_got_tls_type (abfd) [r_symndx];
5072 }
5073
5074 /* If a TLS symbol is accessed using IE at least once,
5075 there is no point to use dynamic model for it. */
5076 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
5077 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
5078 {
5079 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
5080 tls_type = GOT_TLS_IE;
5081 else
5082 {
5083 (*_bfd_error_handler)
d003868e
AM
5084 (_("%B: `%s' accessed both as normal and thread local symbol"),
5085 abfd, h->root.root.string);
b34976b6 5086 return FALSE;
3376eaf5
KK
5087 }
5088 }
5089
5090 if (old_tls_type != tls_type)
5091 {
5092 if (h != NULL)
5093 sh_elf_hash_entry (h)->tls_type = tls_type;
5094 else
5095 sh_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
37c644f2 5096 }
3376eaf5
KK
5097
5098 break;
5099
5100 case R_SH_TLS_LD_32:
5101 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
37c644f2
AO
5102 break;
5103
fbca6ad9
AO
5104 case R_SH_GOTPLT32:
5105#ifdef INCLUDE_SHMEDIA
5106 case R_SH_GOTPLT_LOW16:
5107 case R_SH_GOTPLT_MEDLOW16:
5108 case R_SH_GOTPLT_MEDHI16:
5109 case R_SH_GOTPLT_HI16:
5110 case R_SH_GOTPLT10BY4:
5111 case R_SH_GOTPLT10BY8:
5112#endif
5113 /* If this is a local symbol, we resolve it directly without
5114 creating a procedure linkage table entry. */
5115
5116 if (h == NULL
f5385ebf 5117 || h->forced_local
fbca6ad9
AO
5118 || ! info->shared
5119 || info->symbolic
067653c5 5120 || h->dynindx == -1)
fbca6ad9
AO
5121 goto force_got;
5122
f5385ebf 5123 h->needs_plt = 1;
067653c5 5124 h->plt.refcount += 1;
4989d792 5125 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
fbca6ad9
AO
5126
5127 break;
5128
37c644f2 5129 case R_SH_PLT32:
fbca6ad9
AO
5130#ifdef INCLUDE_SHMEDIA
5131 case R_SH_PLT_LOW16:
5132 case R_SH_PLT_MEDLOW16:
5133 case R_SH_PLT_MEDHI16:
5134 case R_SH_PLT_HI16:
5135#endif
37c644f2
AO
5136 /* This symbol requires a procedure linkage table entry. We
5137 actually build the entry in adjust_dynamic_symbol,
5138 because this might be a case of linking PIC code which is
5139 never referenced by a dynamic object, in which case we
5140 don't need to generate a procedure linkage table entry
5141 after all. */
5142
5143 /* If this is a local symbol, we resolve it directly without
5144 creating a procedure linkage table entry. */
5145 if (h == NULL)
5146 continue;
5147
f5385ebf 5148 if (h->forced_local)
37c644f2
AO
5149 break;
5150
f5385ebf 5151 h->needs_plt = 1;
067653c5 5152 h->plt.refcount += 1;
37c644f2
AO
5153 break;
5154
5155 case R_SH_DIR32:
5156 case R_SH_REL32:
46e993b9
KK
5157#ifdef INCLUDE_SHMEDIA
5158 case R_SH_IMM_LOW16_PCREL:
5159 case R_SH_IMM_MEDLOW16_PCREL:
5160 case R_SH_IMM_MEDHI16_PCREL:
5161 case R_SH_IMM_HI16_PCREL:
5162#endif
067653c5
AM
5163 if (h != NULL && ! info->shared)
5164 {
f5385ebf 5165 h->non_got_ref = 1;
067653c5
AM
5166 h->plt.refcount += 1;
5167 }
37c644f2
AO
5168
5169 /* If we are creating a shared library, and this is a reloc
5170 against a global symbol, or a non PC relative reloc
5171 against a local symbol, then we need to copy the reloc
5172 into the shared library. However, if we are linking with
5173 -Bsymbolic, we do not need to copy a reloc against a
5174 global symbol which is defined in an object we are
5175 including in the link (i.e., DEF_REGULAR is set). At
5176 this point we have not seen all the input files, so it is
5177 possible that DEF_REGULAR is not set now but will be set
5178 later (it is never cleared). We account for that
5179 possibility below by storing information in the
067653c5
AM
5180 dyn_relocs field of the hash table entry. A similar
5181 situation occurs when creating shared libraries and symbol
5182 visibility changes render the symbol local.
5183
5184 If on the other hand, we are creating an executable, we
5185 may need to keep relocations for symbols satisfied by a
5186 dynamic library if we manage to avoid copy relocs for the
5187 symbol. */
5188 if ((info->shared
5189 && (sec->flags & SEC_ALLOC) != 0
3376eaf5 5190 && (r_type != R_SH_REL32
067653c5
AM
5191 || (h != NULL
5192 && (! info->symbolic
5193 || h->root.type == bfd_link_hash_defweak
f5385ebf 5194 || !h->def_regular))))
067653c5
AM
5195 || (! info->shared
5196 && (sec->flags & SEC_ALLOC) != 0
5197 && h != NULL
5198 && (h->root.type == bfd_link_hash_defweak
f5385ebf 5199 || !h->def_regular)))
37c644f2 5200 {
067653c5
AM
5201 struct elf_sh_dyn_relocs *p;
5202 struct elf_sh_dyn_relocs **head;
5203
2293c499
AM
5204 if (htab->root.dynobj == NULL)
5205 htab->root.dynobj = abfd;
067653c5 5206
37c644f2
AO
5207 /* When creating a shared object, we must copy these
5208 reloc types into the output file. We create a reloc
5209 section in dynobj and make room for this reloc. */
5210 if (sreloc == NULL)
5211 {
5212 const char *name;
5213
5214 name = (bfd_elf_string_from_elf_section
5215 (abfd,
5216 elf_elfheader (abfd)->e_shstrndx,
5217 elf_section_data (sec)->rel_hdr.sh_name));
5218 if (name == NULL)
b34976b6 5219 return FALSE;
37c644f2 5220
0112cd26 5221 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
37c644f2
AO
5222 && strcmp (bfd_get_section_name (abfd, sec),
5223 name + 5) == 0);
5224
2293c499 5225 sreloc = bfd_get_section_by_name (htab->root.dynobj, name);
37c644f2
AO
5226 if (sreloc == NULL)
5227 {
5228 flagword flags;
5229
37c644f2
AO
5230 flags = (SEC_HAS_CONTENTS | SEC_READONLY
5231 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
5232 if ((sec->flags & SEC_ALLOC) != 0)
5233 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
5234 sreloc = bfd_make_section_with_flags (htab->root.dynobj,
5235 name,
5236 flags);
37c644f2 5237 if (sreloc == NULL
2293c499
AM
5238 || ! bfd_set_section_alignment (htab->root.dynobj,
5239 sreloc, 2))
b34976b6 5240 return FALSE;
37c644f2 5241 }
067653c5 5242 elf_section_data (sec)->sreloc = sreloc;
37c644f2
AO
5243 }
5244
067653c5
AM
5245 /* If this is a global symbol, we count the number of
5246 relocations we need for this symbol. */
5247 if (h != NULL)
5248 head = &((struct elf_sh_link_hash_entry *) h)->dyn_relocs;
5249 else
37c644f2 5250 {
067653c5 5251 asection *s;
6edfbbad 5252 void *vpp;
37c644f2 5253
067653c5
AM
5254 /* Track dynamic relocs needed for local syms too. */
5255 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
5256 sec, r_symndx);
5257 if (s == NULL)
b34976b6 5258 return FALSE;
37c644f2 5259
6edfbbad
DJ
5260 vpp = &elf_section_data (s)->local_dynrel;
5261 head = (struct elf_sh_dyn_relocs **) vpp;
067653c5 5262 }
37c644f2 5263
067653c5
AM
5264 p = *head;
5265 if (p == NULL || p->sec != sec)
5266 {
5267 bfd_size_type amt = sizeof (*p);
2293c499 5268 p = bfd_alloc (htab->root.dynobj, amt);
37c644f2 5269 if (p == NULL)
b34976b6 5270 return FALSE;
067653c5
AM
5271 p->next = *head;
5272 *head = p;
5273 p->sec = sec;
5274 p->count = 0;
5275 p->pc_count = 0;
37c644f2 5276 }
067653c5
AM
5277
5278 p->count += 1;
46e993b9
KK
5279 if (r_type == R_SH_REL32
5280#ifdef INCLUDE_SHMEDIA
5281 || r_type == R_SH_IMM_LOW16_PCREL
5282 || r_type == R_SH_IMM_MEDLOW16_PCREL
5283 || r_type == R_SH_IMM_MEDHI16_PCREL
5284 || r_type == R_SH_IMM_HI16_PCREL
5285#endif
5286 )
067653c5 5287 p->pc_count += 1;
37c644f2
AO
5288 }
5289
5290 break;
3376eaf5
KK
5291
5292 case R_SH_TLS_LE_32:
5293 if (info->shared)
5294 {
d003868e
AM
5295 (*_bfd_error_handler)
5296 (_("%B: TLS local exec code cannot be linked into shared objects"),
5297 abfd);
b34976b6 5298 return FALSE;
3376eaf5
KK
5299 }
5300
267fb3c1 5301 break;
3376eaf5 5302
3376eaf5 5303 case R_SH_TLS_LDO_32:
267fb3c1 5304 /* Nothing to do. */
3376eaf5
KK
5305 break;
5306
5307 default:
5308 break;
435b1e90 5309 }
252b5132 5310 }
435b1e90 5311
b34976b6 5312 return TRUE;
252b5132
RH
5313}
5314
fbca6ad9 5315#ifndef sh_elf_set_mach_from_flags
f6f9408f
JR
5316static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
5317
b34976b6 5318static bfd_boolean
09fd220b 5319sh_elf_set_mach_from_flags (bfd *abfd)
d4845d57 5320{
f6f9408f
JR
5321 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
5322
5323 if (flags >= sizeof(sh_ef_bfd_table))
5324 return FALSE;
5325
5326 if (sh_ef_bfd_table[flags] == 0)
5327 return FALSE;
5328
5329 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
d4845d57 5330
b34976b6 5331 return TRUE;
d4845d57 5332}
f6f9408f
JR
5333
5334
5335/* Reverse table lookup for sh_ef_bfd_table[].
5336 Given a bfd MACH value from archures.c
5337 return the equivalent ELF flags from the table.
5338 Return -1 if no match is found. */
5339
5340int
5341sh_elf_get_flags_from_mach (unsigned long mach)
5342{
4195f552 5343 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
f6f9408f
JR
5344
5345 for (; i>0; i--)
5346 if (sh_ef_bfd_table[i] == mach)
5347 return i;
5348
5349 /* shouldn't get here */
5350 BFD_FAIL();
5351
5352 return -1;
5353}
fbca6ad9 5354#endif /* not sh_elf_set_mach_from_flags */
d4845d57 5355
fbca6ad9 5356#ifndef sh_elf_set_private_flags
435b1e90
KH
5357/* Function to keep SH specific file flags. */
5358
b34976b6 5359static bfd_boolean
09fd220b 5360sh_elf_set_private_flags (bfd *abfd, flagword flags)
d4845d57
JR
5361{
5362 BFD_ASSERT (! elf_flags_init (abfd)
5363 || elf_elfheader (abfd)->e_flags == flags);
5364
5365 elf_elfheader (abfd)->e_flags = flags;
b34976b6 5366 elf_flags_init (abfd) = TRUE;
d4845d57
JR
5367 return sh_elf_set_mach_from_flags (abfd);
5368}
fbca6ad9 5369#endif /* not sh_elf_set_private_flags */
d4845d57 5370
fbca6ad9 5371#ifndef sh_elf_copy_private_data
d4845d57 5372/* Copy backend specific data from one object module to another */
435b1e90 5373
b34976b6 5374static bfd_boolean
09fd220b 5375sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
d4845d57 5376{
38b1a46c 5377 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
d4845d57 5378 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 5379 return TRUE;
d4845d57
JR
5380
5381 return sh_elf_set_private_flags (obfd, elf_elfheader (ibfd)->e_flags);
5382}
fbca6ad9 5383#endif /* not sh_elf_copy_private_data */
d4845d57 5384
fbca6ad9 5385#ifndef sh_elf_merge_private_data
d4845d57 5386
f6f9408f
JR
5387/* This function returns the ELF architecture number that
5388 corresponds to the given arch_sh* flags. */
85fbca6a 5389
f6f9408f
JR
5390int
5391sh_find_elf_flags (unsigned int arch_set)
d4845d57 5392{
85fbca6a 5393 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
f6f9408f 5394 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
d4845d57 5395
f6f9408f
JR
5396 return sh_elf_get_flags_from_mach (bfd_mach);
5397}
f6f9408f
JR
5398
5399/* This routine initialises the elf flags when required and
5400 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
d4845d57 5401
f6f9408f
JR
5402static bfd_boolean
5403sh_elf_merge_private_data (bfd *ibfd, bfd *obfd)
5404{
85fbca6a
NC
5405 extern bfd_boolean sh_merge_bfd_arch (bfd *, bfd *);
5406
d4845d57
JR
5407 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5408 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 5409 return TRUE;
d4845d57
JR
5410
5411 if (! elf_flags_init (obfd))
5412 {
a39b79b9 5413 /* This happens when ld starts out with a 'blank' output file. */
b34976b6 5414 elf_flags_init (obfd) = TRUE;
a39b79b9 5415 elf_elfheader (obfd)->e_flags = EF_SH1;
f6f9408f 5416 sh_elf_set_mach_from_flags (obfd);
d4845d57 5417 }
d4845d57 5418
85fbca6a 5419 if (! sh_merge_bfd_arch (ibfd, obfd))
1d70c7fb 5420 {
d003868e 5421 _bfd_error_handler ("%B: uses instructions which are incompatible "
1d70c7fb 5422 "with instructions used in previous modules",
d003868e 5423 ibfd);
1d70c7fb
AO
5424 bfd_set_error (bfd_error_bad_value);
5425 return FALSE;
5426 }
f6f9408f
JR
5427
5428 elf_elfheader (obfd)->e_flags =
5429 sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
5430
5431 return TRUE;
d4845d57 5432}
fbca6ad9 5433#endif /* not sh_elf_merge_private_data */
d4845d57 5434
3376eaf5
KK
5435/* Override the generic function because we need to store sh_elf_obj_tdata
5436 as the specific tdata. We set also the machine architecture from flags
5437 here. */
5438
b34976b6 5439static bfd_boolean
09fd220b 5440sh_elf_object_p (bfd *abfd)
3376eaf5 5441{
66becf32 5442 return sh_elf_set_mach_from_flags (abfd);
3376eaf5
KK
5443}
5444
37c644f2
AO
5445/* Finish up dynamic symbol handling. We set the contents of various
5446 dynamic sections here. */
5447
b34976b6 5448static bfd_boolean
09fd220b
KK
5449sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5450 struct elf_link_hash_entry *h,
5451 Elf_Internal_Sym *sym)
37c644f2 5452{
067653c5 5453 struct elf_sh_link_hash_table *htab;
37c644f2 5454
067653c5 5455 htab = sh_elf_hash_table (info);
37c644f2
AO
5456
5457 if (h->plt.offset != (bfd_vma) -1)
5458 {
5459 asection *splt;
5460 asection *sgot;
5461 asection *srel;
5462
5463 bfd_vma plt_index;
5464 bfd_vma got_offset;
5465 Elf_Internal_Rela rel;
947216bf 5466 bfd_byte *loc;
37c644f2
AO
5467
5468 /* This symbol has an entry in the procedure linkage table. Set
5469 it up. */
5470
5471 BFD_ASSERT (h->dynindx != -1);
5472
067653c5
AM
5473 splt = htab->splt;
5474 sgot = htab->sgotplt;
5475 srel = htab->srelplt;
37c644f2
AO
5476 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
5477
5478 /* Get the index in the procedure linkage table which
5479 corresponds to this symbol. This is the index of this symbol
5480 in all the symbols for which we are making plt entries. The
5481 first entry in the procedure linkage table is reserved. */
55e6e397 5482 plt_index = get_plt_index (htab->plt_info, h->plt.offset);
37c644f2
AO
5483
5484 /* Get the offset into the .got table of the entry that
5485 corresponds to this function. Each .got entry is 4 bytes.
5486 The first three are reserved. */
5487 got_offset = (plt_index + 3) * 4;
5488
fbca6ad9 5489#ifdef GOT_BIAS
c8614e8e
AM
5490 if (info->shared)
5491 got_offset -= GOT_BIAS;
fbca6ad9
AO
5492#endif
5493
37c644f2 5494 /* Fill in the entry in the procedure linkage table. */
55e6e397
RS
5495 memcpy (splt->contents + h->plt.offset,
5496 htab->plt_info->symbol_entry,
5497 htab->plt_info->symbol_entry_size);
5498
5499 if (info->shared)
5500 install_plt_field (output_bfd, FALSE, got_offset,
5501 (splt->contents
5502 + h->plt.offset
5503 + htab->plt_info->symbol_fields.got_entry));
5504 else
37c644f2 5505 {
55e6e397 5506 install_plt_field (output_bfd, FALSE,
fbca6ad9
AO
5507 (sgot->output_section->vma
5508 + sgot->output_offset
5509 + got_offset),
55e6e397
RS
5510 (splt->contents
5511 + h->plt.offset
5512 + htab->plt_info->symbol_fields.got_entry));
5513 if (htab->vxworks_p)
37c644f2 5514 {
55e6e397
RS
5515 unsigned int reachable_plts, plts_per_4k;
5516 int distance;
5517
5518 /* Divide the PLT into groups. The first group contains
5519 REACHABLE_PLTS entries and the other groups contain
5520 PLTS_PER_4K entries. Entries in the first group can
5521 branch directly to .plt; those in later groups branch
5522 to the last element of the previous group. */
5523 /* ??? It would be better to create multiple copies of
5524 the common resolver stub. */
5525 reachable_plts = ((4096
5526 - htab->plt_info->plt0_entry_size
5527 - (htab->plt_info->symbol_fields.plt + 4))
5528 / htab->plt_info->symbol_entry_size) + 1;
5529 plts_per_4k = (4096 / htab->plt_info->symbol_entry_size);
5530 if (plt_index < reachable_plts)
5531 distance = -(h->plt.offset
5532 + htab->plt_info->symbol_fields.plt);
5533 else
5534 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
5535 * htab->plt_info->symbol_entry_size);
5536
5537 /* Install the 'bra' with this offset. */
5538 bfd_put_16 (output_bfd,
5539 0xa000 | (0x0fff & ((distance - 4) / 2)),
5540 (splt->contents
5541 + h->plt.offset
5542 + htab->plt_info->symbol_fields.plt));
37c644f2 5543 }
55e6e397
RS
5544 else
5545 install_plt_field (output_bfd, TRUE,
5546 splt->output_section->vma + splt->output_offset,
5547 (splt->contents
5548 + h->plt.offset
5549 + htab->plt_info->symbol_fields.plt));
37c644f2
AO
5550 }
5551
fbca6ad9 5552#ifdef GOT_BIAS
c8614e8e
AM
5553 if (info->shared)
5554 got_offset += GOT_BIAS;
fbca6ad9
AO
5555#endif
5556
55e6e397 5557 install_plt_field (output_bfd, FALSE,
fbca6ad9 5558 plt_index * sizeof (Elf32_External_Rela),
55e6e397
RS
5559 (splt->contents
5560 + h->plt.offset
5561 + htab->plt_info->symbol_fields.reloc_offset));
37c644f2
AO
5562
5563 /* Fill in the entry in the global offset table. */
5564 bfd_put_32 (output_bfd,
5565 (splt->output_section->vma
5566 + splt->output_offset
5567 + h->plt.offset
55e6e397 5568 + htab->plt_info->symbol_resolve_offset),
37c644f2
AO
5569 sgot->contents + got_offset);
5570
5571 /* Fill in the entry in the .rela.plt section. */
5572 rel.r_offset = (sgot->output_section->vma
5573 + sgot->output_offset
5574 + got_offset);
5575 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
5576 rel.r_addend = 0;
fbca6ad9
AO
5577#ifdef GOT_BIAS
5578 rel.r_addend = GOT_BIAS;
5579#endif
947216bf
AM
5580 loc = srel->contents + plt_index * sizeof (Elf32_External_Rela);
5581 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2 5582
55e6e397
RS
5583 if (htab->vxworks_p && !info->shared)
5584 {
5585 /* Create the .rela.plt.unloaded relocations for this PLT entry.
5586 Begin by pointing LOC to the first such relocation. */
5587 loc = (htab->srelplt2->contents
5588 + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
5589
5590 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
5591 for the PLT entry's pointer to the .got.plt entry. */
5592 rel.r_offset = (htab->splt->output_section->vma
5593 + htab->splt->output_offset
5594 + h->plt.offset
5595 + htab->plt_info->symbol_fields.got_entry);
5596 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
5597 rel.r_addend = got_offset;
5598 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
5599 loc += sizeof (Elf32_External_Rela);
5600
5601 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
5602 the .got.plt entry, which initially points to .plt. */
5603 rel.r_offset = (htab->sgotplt->output_section->vma
5604 + htab->sgotplt->output_offset
5605 + got_offset);
5606 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
5607 rel.r_addend = 0;
5608 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5609 }
5610
f5385ebf 5611 if (!h->def_regular)
37c644f2
AO
5612 {
5613 /* Mark the symbol as undefined, rather than as defined in
5614 the .plt section. Leave the value alone. */
5615 sym->st_shndx = SHN_UNDEF;
5616 }
5617 }
5618
3376eaf5
KK
5619 if (h->got.offset != (bfd_vma) -1
5620 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_GD
5621 && sh_elf_hash_entry (h)->tls_type != GOT_TLS_IE)
37c644f2
AO
5622 {
5623 asection *sgot;
5624 asection *srel;
5625 Elf_Internal_Rela rel;
947216bf 5626 bfd_byte *loc;
37c644f2
AO
5627
5628 /* This symbol has an entry in the global offset table. Set it
5629 up. */
5630
067653c5
AM
5631 sgot = htab->sgot;
5632 srel = htab->srelgot;
37c644f2
AO
5633 BFD_ASSERT (sgot != NULL && srel != NULL);
5634
5635 rel.r_offset = (sgot->output_section->vma
5636 + sgot->output_offset
dc810e39 5637 + (h->got.offset &~ (bfd_vma) 1));
37c644f2 5638
067653c5
AM
5639 /* If this is a static link, or it is a -Bsymbolic link and the
5640 symbol is defined locally or was forced to be local because
5641 of a version file, we just want to emit a RELATIVE reloc.
37c644f2
AO
5642 The entry in the global offset table will already have been
5643 initialized in the relocate_section function. */
5644 if (info->shared
3d85aebe 5645 && SYMBOL_REFERENCES_LOCAL (info, h))
37c644f2
AO
5646 {
5647 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
5648 rel.r_addend = (h->root.u.def.value
5649 + h->root.u.def.section->output_section->vma
5650 + h->root.u.def.section->output_offset);
5651 }
5652 else
5653 {
5654 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5655 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
5656 rel.r_addend = 0;
5657 }
5658
947216bf
AM
5659 loc = srel->contents;
5660 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
5661 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2
AO
5662 }
5663
396a6083
SC
5664#ifdef INCLUDE_SHMEDIA
5665 {
5666 struct elf_sh_link_hash_entry *eh;
5667
5668 eh = (struct elf_sh_link_hash_entry *) h;
5669 if (eh->datalabel_got.offset != (bfd_vma) -1)
5670 {
5671 asection *sgot;
5672 asection *srel;
5673 Elf_Internal_Rela rel;
947216bf 5674 bfd_byte *loc;
396a6083
SC
5675
5676 /* This symbol has a datalabel entry in the global offset table.
5677 Set it up. */
5678
5679 sgot = htab->sgot;
5680 srel = htab->srelgot;
5681 BFD_ASSERT (sgot != NULL && srel != NULL);
5682
5683 rel.r_offset = (sgot->output_section->vma
5684 + sgot->output_offset
5685 + (eh->datalabel_got.offset &~ (bfd_vma) 1));
5686
5687 /* If this is a static link, or it is a -Bsymbolic link and the
5688 symbol is defined locally or was forced to be local because
5689 of a version file, we just want to emit a RELATIVE reloc.
5690 The entry in the global offset table will already have been
5691 initialized in the relocate_section function. */
5692 if (info->shared
3d85aebe 5693 && SYMBOL_REFERENCES_LOCAL (info, h))
396a6083
SC
5694 {
5695 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
5696 rel.r_addend = (h->root.u.def.value
5697 + h->root.u.def.section->output_section->vma
5698 + h->root.u.def.section->output_offset);
5699 }
5700 else
5701 {
5702 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents
5703 + eh->datalabel_got.offset);
5704 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
5705 rel.r_addend = 0;
5706 }
5707
947216bf
AM
5708 loc = srel->contents;
5709 loc += srel->reloc_count++ * sizeof (Elf32_External_Rela);
5710 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
396a6083
SC
5711 }
5712 }
5713#endif
5714
f5385ebf 5715 if (h->needs_copy)
37c644f2
AO
5716 {
5717 asection *s;
5718 Elf_Internal_Rela rel;
947216bf 5719 bfd_byte *loc;
37c644f2
AO
5720
5721 /* This symbol needs a copy reloc. Set it up. */
5722
5723 BFD_ASSERT (h->dynindx != -1
5724 && (h->root.type == bfd_link_hash_defined
5725 || h->root.type == bfd_link_hash_defweak));
5726
5727 s = bfd_get_section_by_name (h->root.u.def.section->owner,
5728 ".rela.bss");
5729 BFD_ASSERT (s != NULL);
5730
5731 rel.r_offset = (h->root.u.def.value
5732 + h->root.u.def.section->output_section->vma
5733 + h->root.u.def.section->output_offset);
5734 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
5735 rel.r_addend = 0;
947216bf
AM
5736 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5737 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2
AO
5738 }
5739
55e6e397
RS
5740 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
5741 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
5742 ".got" section. */
37c644f2 5743 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
55e6e397 5744 || (!htab->vxworks_p && h == htab->root.hgot))
37c644f2
AO
5745 sym->st_shndx = SHN_ABS;
5746
b34976b6 5747 return TRUE;
37c644f2
AO
5748}
5749
5750/* Finish up the dynamic sections. */
5751
b34976b6 5752static bfd_boolean
09fd220b 5753sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
37c644f2 5754{
067653c5 5755 struct elf_sh_link_hash_table *htab;
37c644f2
AO
5756 asection *sgot;
5757 asection *sdyn;
5758
067653c5 5759 htab = sh_elf_hash_table (info);
067653c5 5760 sgot = htab->sgotplt;
2293c499 5761 sdyn = bfd_get_section_by_name (htab->root.dynobj, ".dynamic");
37c644f2 5762
067653c5 5763 if (htab->root.dynamic_sections_created)
37c644f2
AO
5764 {
5765 asection *splt;
5766 Elf32_External_Dyn *dyncon, *dynconend;
5767
067653c5 5768 BFD_ASSERT (sgot != NULL && sdyn != NULL);
37c644f2
AO
5769
5770 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 5771 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
37c644f2
AO
5772 for (; dyncon < dynconend; dyncon++)
5773 {
5774 Elf_Internal_Dyn dyn;
37c644f2 5775 asection *s;
067653c5
AM
5776#ifdef INCLUDE_SHMEDIA
5777 const char *name;
5778#endif
37c644f2 5779
2293c499 5780 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
37c644f2
AO
5781
5782 switch (dyn.d_tag)
5783 {
5784 default:
5785 break;
5786
3b587c71
AM
5787#ifdef INCLUDE_SHMEDIA
5788 case DT_INIT:
5789 name = info->init_function;
5790 goto get_sym;
5791
5792 case DT_FINI:
5793 name = info->fini_function;
5794 get_sym:
5795 if (dyn.d_un.d_val != 0)
5796 {
067653c5
AM
5797 struct elf_link_hash_entry *h;
5798
5799 h = elf_link_hash_lookup (&htab->root, name,
b34976b6 5800 FALSE, FALSE, TRUE);
3b587c71
AM
5801 if (h != NULL && (h->other & STO_SH5_ISA32))
5802 {
5803 dyn.d_un.d_val |= 1;
5804 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5805 }
5806 }
5807 break;
5808#endif
5809
37c644f2 5810 case DT_PLTGOT:
067653c5 5811 s = htab->sgot->output_section;
37c644f2
AO
5812 goto get_vma;
5813
5814 case DT_JMPREL:
067653c5 5815 s = htab->srelplt->output_section;
37c644f2 5816 get_vma:
37c644f2
AO
5817 BFD_ASSERT (s != NULL);
5818 dyn.d_un.d_ptr = s->vma;
5819 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5820 break;
5821
5822 case DT_PLTRELSZ:
067653c5 5823 s = htab->srelplt->output_section;
37c644f2 5824 BFD_ASSERT (s != NULL);
eea6121a 5825 dyn.d_un.d_val = s->size;
37c644f2
AO
5826 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5827 break;
5828
5829 case DT_RELASZ:
5830 /* My reading of the SVR4 ABI indicates that the
5831 procedure linkage table relocs (DT_JMPREL) should be
5832 included in the overall relocs (DT_RELA). This is
5833 what Solaris does. However, UnixWare can not handle
5834 that case. Therefore, we override the DT_RELASZ entry
5835 here to make it not include the JMPREL relocs. Since
5836 the linker script arranges for .rela.plt to follow all
5837 other relocation sections, we don't have to worry
5838 about changing the DT_RELA entry. */
067653c5 5839 if (htab->srelplt != NULL)
37c644f2 5840 {
067653c5 5841 s = htab->srelplt->output_section;
eea6121a 5842 dyn.d_un.d_val -= s->size;
37c644f2
AO
5843 }
5844 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5845 break;
5846 }
5847 }
5848
5849 /* Fill in the first entry in the procedure linkage table. */
067653c5 5850 splt = htab->splt;
55e6e397 5851 if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
37c644f2 5852 {
55e6e397
RS
5853 unsigned int i;
5854
5855 memcpy (splt->contents,
5856 htab->plt_info->plt0_entry,
5857 htab->plt_info->plt0_entry_size);
5858 for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
5859 if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
5860 install_plt_field (output_bfd, FALSE,
5861 (sgot->output_section->vma
5862 + sgot->output_offset
5863 + (i * 4)),
5864 (splt->contents
5865 + htab->plt_info->plt0_got_fields[i]));
5866
5867 if (htab->vxworks_p)
37c644f2 5868 {
55e6e397
RS
5869 /* Finalize the .rela.plt.unloaded contents. */
5870 Elf_Internal_Rela rel;
5871 bfd_byte *loc;
5872
5873 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
5874 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
5875 loc = htab->srelplt2->contents;
5876 rel.r_offset = (splt->output_section->vma
5877 + splt->output_offset
5878 + htab->plt_info->plt0_got_fields[2]);
5879 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
5880 rel.r_addend = 8;
5881 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
5882 loc += sizeof (Elf32_External_Rela);
5883
5884 /* Fix up the remaining .rela.plt.unloaded relocations.
5885 They may have the wrong symbol index for _G_O_T_ or
5886 _P_L_T_ depending on the order in which symbols were
5887 output. */
5888 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
37c644f2 5889 {
55e6e397
RS
5890 /* The PLT entry's pointer to the .got.plt slot. */
5891 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
5892 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
5893 R_SH_DIR32);
5894 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5895 loc += sizeof (Elf32_External_Rela);
5896
5897 /* The .got.plt slot's pointer to .plt. */
5898 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
5899 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
5900 R_SH_DIR32);
5901 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5902 loc += sizeof (Elf32_External_Rela);
37c644f2 5903 }
37c644f2
AO
5904 }
5905
5906 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5907 really seem like the right value. */
5908 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
5909 }
5910 }
5911
5912 /* Fill in the first three entries in the global offset table. */
eea6121a 5913 if (sgot && sgot->size > 0)
37c644f2
AO
5914 {
5915 if (sdyn == NULL)
5916 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5917 else
5918 bfd_put_32 (output_bfd,
5919 sdyn->output_section->vma + sdyn->output_offset,
5920 sgot->contents);
5921 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5922 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
37c644f2 5923
067653c5
AM
5924 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5925 }
37c644f2 5926
b34976b6 5927 return TRUE;
37c644f2
AO
5928}
5929
99e4ae17 5930static enum elf_reloc_type_class
09fd220b 5931sh_elf_reloc_type_class (const Elf_Internal_Rela *rela)
99e4ae17 5932{
f51e552e 5933 switch ((int) ELF32_R_TYPE (rela->r_info))
99e4ae17
AJ
5934 {
5935 case R_SH_RELATIVE:
5936 return reloc_class_relative;
5937 case R_SH_JMP_SLOT:
5938 return reloc_class_plt;
5939 case R_SH_COPY:
5940 return reloc_class_copy;
5941 default:
5942 return reloc_class_normal;
5943 }
5944}
5945
85fbca6a 5946#if !defined SH_TARGET_ALREADY_DEFINED
571fe01f 5947/* Support for Linux core dump NOTE sections. */
85fbca6a 5948
b34976b6 5949static bfd_boolean
09fd220b 5950elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2bc3c89a
AM
5951{
5952 int offset;
eea6121a 5953 unsigned int size;
2bc3c89a
AM
5954
5955 switch (note->descsz)
5956 {
5957 default:
b34976b6 5958 return FALSE;
2bc3c89a
AM
5959
5960 case 168: /* Linux/SH */
5961 /* pr_cursig */
5962 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5963
5964 /* pr_pid */
5965 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5966
5967 /* pr_reg */
5968 offset = 72;
eea6121a 5969 size = 92;
2bc3c89a
AM
5970
5971 break;
5972 }
5973
5974 /* Make a ".reg/999" section. */
5975 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 5976 size, note->descpos + offset);
2bc3c89a
AM
5977}
5978
b34976b6 5979static bfd_boolean
09fd220b 5980elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2bc3c89a
AM
5981{
5982 switch (note->descsz)
5983 {
5984 default:
b34976b6 5985 return FALSE;
2bc3c89a
AM
5986
5987 case 124: /* Linux/SH elf_prpsinfo */
5988 elf_tdata (abfd)->core_program
5989 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
5990 elf_tdata (abfd)->core_command
5991 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
5992 }
5993
5994 /* Note that for some reason, a spurious space is tacked
5995 onto the end of the args in some (at least one anyway)
5996 implementations, so strip it off if it exists. */
5997
5998 {
5999 char *command = elf_tdata (abfd)->core_command;
6000 int n = strlen (command);
6001
6002 if (0 < n && command[n - 1] == ' ')
6003 command[n - 1] = '\0';
6004 }
6005
b34976b6 6006 return TRUE;
2bc3c89a 6007}
85fbca6a 6008#endif /* not SH_TARGET_ALREADY_DEFINED */
2bc3c89a 6009
1f1799d5
KK
6010
6011/* Return address for Ith PLT stub in section PLT, for relocation REL
6012 or (bfd_vma) -1 if it should not be included. */
6013
6014static bfd_vma
6015sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
6016 const arelent *rel ATTRIBUTE_UNUSED)
6017{
55e6e397
RS
6018 const struct elf_sh_plt_info *plt_info;
6019
6020 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
6021 return plt->vma + get_plt_offset (plt_info, i);
1f1799d5
KK
6022}
6023
85fbca6a 6024#if !defined SH_TARGET_ALREADY_DEFINED
252b5132
RH
6025#define TARGET_BIG_SYM bfd_elf32_sh_vec
6026#define TARGET_BIG_NAME "elf32-sh"
6027#define TARGET_LITTLE_SYM bfd_elf32_shl_vec
6028#define TARGET_LITTLE_NAME "elf32-shl"
85fbca6a
NC
6029#endif
6030
252b5132
RH
6031#define ELF_ARCH bfd_arch_sh
6032#define ELF_MACHINE_CODE EM_SH
d0facd1b
NC
6033#ifdef __QNXTARGET__
6034#define ELF_MAXPAGESIZE 0x1000
6035#else
6036#define ELF_MAXPAGESIZE 0x80
6037#endif
252b5132
RH
6038
6039#define elf_symbol_leading_char '_'
6040
6041#define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
157090f7
AM
6042#define bfd_elf32_bfd_reloc_name_lookup \
6043 sh_elf_reloc_name_lookup
252b5132
RH
6044#define elf_info_to_howto sh_elf_info_to_howto
6045#define bfd_elf32_bfd_relax_section sh_elf_relax_section
6046#define elf_backend_relocate_section sh_elf_relocate_section
6047#define bfd_elf32_bfd_get_relocated_section_contents \
6048 sh_elf_get_relocated_section_contents
3376eaf5
KK
6049#define bfd_elf32_mkobject sh_elf_mkobject
6050#define elf_backend_object_p sh_elf_object_p
d4845d57
JR
6051#define bfd_elf32_bfd_set_private_bfd_flags \
6052 sh_elf_set_private_flags
6053#define bfd_elf32_bfd_copy_private_bfd_data \
6054 sh_elf_copy_private_data
875f7f69 6055#define bfd_elf32_bfd_merge_private_bfd_data \
d4845d57 6056 sh_elf_merge_private_data
252b5132 6057
067653c5
AM
6058#define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
6059#define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook
6060#define elf_backend_check_relocs sh_elf_check_relocs
6061#define elf_backend_copy_indirect_symbol \
6062 sh_elf_copy_indirect_symbol
37c644f2
AO
6063#define elf_backend_create_dynamic_sections \
6064 sh_elf_create_dynamic_sections
6065#define bfd_elf32_bfd_link_hash_table_create \
6066 sh_elf_link_hash_table_create
6067#define elf_backend_adjust_dynamic_symbol \
6068 sh_elf_adjust_dynamic_symbol
55e6e397
RS
6069#define elf_backend_always_size_sections \
6070 sh_elf_always_size_sections
37c644f2
AO
6071#define elf_backend_size_dynamic_sections \
6072 sh_elf_size_dynamic_sections
74541ad4
AM
6073#define elf_backend_omit_section_dynsym \
6074 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
37c644f2
AO
6075#define elf_backend_finish_dynamic_symbol \
6076 sh_elf_finish_dynamic_symbol
6077#define elf_backend_finish_dynamic_sections \
6078 sh_elf_finish_dynamic_sections
99e4ae17 6079#define elf_backend_reloc_type_class sh_elf_reloc_type_class
1f1799d5 6080#define elf_backend_plt_sym_val sh_elf_plt_sym_val
37c644f2 6081
067653c5
AM
6082#define elf_backend_can_gc_sections 1
6083#define elf_backend_can_refcount 1
37c644f2
AO
6084#define elf_backend_want_got_plt 1
6085#define elf_backend_plt_readonly 1
6086#define elf_backend_want_plt_sym 0
6087#define elf_backend_got_header_size 12
ed71e111 6088
85fbca6a 6089#if !defined INCLUDE_SHMEDIA && !defined SH_TARGET_ALREADY_DEFINED
2bc3c89a 6090
252b5132 6091#include "elf32-target.h"
ed71e111 6092
2bc3c89a
AM
6093/* NetBSD support. */
6094#undef TARGET_BIG_SYM
6095#define TARGET_BIG_SYM bfd_elf32_shnbsd_vec
6096#undef TARGET_BIG_NAME
6097#define TARGET_BIG_NAME "elf32-sh-nbsd"
6098#undef TARGET_LITTLE_SYM
6099#define TARGET_LITTLE_SYM bfd_elf32_shlnbsd_vec
6100#undef TARGET_LITTLE_NAME
6101#define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6102#undef ELF_MAXPAGESIZE
6103#define ELF_MAXPAGESIZE 0x10000
24718e3b 6104#undef ELF_COMMONPAGESIZE
2bc3c89a
AM
6105#undef elf_symbol_leading_char
6106#define elf_symbol_leading_char 0
571fe01f 6107#undef elf32_bed
2bc3c89a
AM
6108#define elf32_bed elf32_sh_nbsd_bed
6109
6110#include "elf32-target.h"
6111
2bc3c89a
AM
6112
6113/* Linux support. */
6114#undef TARGET_BIG_SYM
6115#define TARGET_BIG_SYM bfd_elf32_shblin_vec
6116#undef TARGET_BIG_NAME
6117#define TARGET_BIG_NAME "elf32-shbig-linux"
6118#undef TARGET_LITTLE_SYM
6119#define TARGET_LITTLE_SYM bfd_elf32_shlin_vec
6120#undef TARGET_LITTLE_NAME
6121#define TARGET_LITTLE_NAME "elf32-sh-linux"
24718e3b
L
6122#undef ELF_COMMONPAGESIZE
6123#define ELF_COMMONPAGESIZE 0x1000
2bc3c89a
AM
6124
6125#undef elf_backend_grok_prstatus
6126#define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6127#undef elf_backend_grok_psinfo
6128#define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
571fe01f 6129#undef elf32_bed
2bc3c89a
AM
6130#define elf32_bed elf32_sh_lin_bed
6131
6132#include "elf32-target.h"
6133
55e6e397
RS
6134#undef TARGET_BIG_SYM
6135#define TARGET_BIG_SYM bfd_elf32_shvxworks_vec
6136#undef TARGET_BIG_NAME
6137#define TARGET_BIG_NAME "elf32-sh-vxworks"
6138#undef TARGET_LITTLE_SYM
6139#define TARGET_LITTLE_SYM bfd_elf32_shlvxworks_vec
6140#undef TARGET_LITTLE_NAME
6141#define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6142#undef elf32_bed
6143#define elf32_bed elf32_sh_vxworks_bed
6144
6145#undef elf_backend_want_plt_sym
6146#define elf_backend_want_plt_sym 1
6147#undef elf_symbol_leading_char
6148#define elf_symbol_leading_char '_'
6149#define elf_backend_want_got_underscore 1
6150#undef elf_backend_grok_prstatus
6151#undef elf_backend_grok_psinfo
6152#undef elf_backend_add_symbol_hook
6153#define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6154#undef elf_backend_link_output_symbol_hook
6155#define elf_backend_link_output_symbol_hook \
6156 elf_vxworks_link_output_symbol_hook
6157#undef elf_backend_emit_relocs
6158#define elf_backend_emit_relocs elf_vxworks_emit_relocs
6159#undef elf_backend_final_write_processing
6160#define elf_backend_final_write_processing \
6161 elf_vxworks_final_write_processing
6162#undef ELF_MAXPAGESIZE
6163#define ELF_MAXPAGESIZE 0x1000
6164#undef ELF_COMMONPAGESIZE
6165
6166#include "elf32-target.h"
6167
85fbca6a 6168#endif /* neither INCLUDE_SHMEDIA nor SH_TARGET_ALREADY_DEFINED */
This page took 0.75167 seconds and 4 git commands to generate.