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