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