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