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