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