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