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