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