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