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