]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-sh.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / elf32-sh.c
CommitLineData
ef230218 1/* Renesas / SuperH SH specific support for 32-bit ELF
250d07de 2 Copyright (C) 1996-2021 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. */
8e45593f
NC
2163 asection *sfuncdesc;
2164 asection *srelfuncdesc;
2165 asection *srofixup;
37c644f2 2166
55e6e397
RS
2167 /* The (unloaded but important) VxWorks .rela.plt.unloaded section. */
2168 asection *srelplt2;
2169
3376eaf5
KK
2170 /* A counter or offset to track a TLS got entry. */
2171 union
2172 {
2173 bfd_signed_vma refcount;
2174 bfd_vma offset;
2175 } tls_ldm_got;
55e6e397
RS
2176
2177 /* The type of PLT to use. */
2178 const struct elf_sh_plt_info *plt_info;
2179
8e45593f
NC
2180 /* True if the target system uses FDPIC. */
2181 bfd_boolean fdpic_p;
067653c5 2182};
37c644f2
AO
2183
2184/* Traverse an sh ELF linker hash table. */
2185
2186#define sh_elf_link_hash_traverse(table, func, info) \
2187 (elf_link_hash_traverse \
2188 (&(table)->root, \
09fd220b 2189 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
37c644f2
AO
2190 (info)))
2191
2192/* Get the sh ELF linker hash table from a link_info structure. */
2193
2194#define sh_elf_hash_table(p) \
0f55320b
AM
2195 ((is_elf_hash_table ((p)->hash) \
2196 && elf_hash_table_id (elf_hash_table (p)) == SH_ELF_DATA) \
2197 ? (struct elf_sh_link_hash_table *) (p)->hash : NULL)
37c644f2
AO
2198
2199/* Create an entry in an sh ELF linker hash table. */
2200
2201static struct bfd_hash_entry *
09fd220b
KK
2202sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2203 struct bfd_hash_table *table,
2204 const char *string)
37c644f2
AO
2205{
2206 struct elf_sh_link_hash_entry *ret =
2207 (struct elf_sh_link_hash_entry *) entry;
2208
2209 /* Allocate the structure if it has not already been allocated by a
2210 subclass. */
2211 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2212 ret = ((struct elf_sh_link_hash_entry *)
2213 bfd_hash_allocate (table,
2214 sizeof (struct elf_sh_link_hash_entry)));
2215 if (ret == (struct elf_sh_link_hash_entry *) NULL)
2216 return (struct bfd_hash_entry *) ret;
2217
2218 /* Call the allocation method of the superclass. */
2219 ret = ((struct elf_sh_link_hash_entry *)
2220 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2221 table, string));
2222 if (ret != (struct elf_sh_link_hash_entry *) NULL)
2223 {
396a6083 2224 ret->gotplt_refcount = 0;
8e45593f
NC
2225 ret->funcdesc.refcount = 0;
2226 ret->abs_funcdesc_refcount = 0;
2227 ret->got_type = GOT_UNKNOWN;
37c644f2
AO
2228 }
2229
2230 return (struct bfd_hash_entry *) ret;
2231}
2232
2233/* Create an sh ELF linker hash table. */
2234
2235static struct bfd_link_hash_table *
09fd220b 2236sh_elf_link_hash_table_create (bfd *abfd)
37c644f2
AO
2237{
2238 struct elf_sh_link_hash_table *ret;
986f0783 2239 size_t amt = sizeof (struct elf_sh_link_hash_table);
37c644f2 2240
0e5de31a 2241 ret = (struct elf_sh_link_hash_table *) bfd_zmalloc (amt);
37c644f2
AO
2242 if (ret == (struct elf_sh_link_hash_table *) NULL)
2243 return NULL;
2244
66eb6687
AM
2245 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2246 sh_elf_link_hash_newfunc,
4dfe6ac6
NC
2247 sizeof (struct elf_sh_link_hash_entry),
2248 SH_ELF_DATA))
37c644f2 2249 {
e2d34d7d 2250 free (ret);
37c644f2
AO
2251 return NULL;
2252 }
2253
3084d7a2
L
2254 if (fdpic_object_p (abfd))
2255 {
2256 ret->root.dt_pltgot_required = TRUE;
2257 ret->fdpic_p = TRUE;
2258 }
067653c5 2259
37c644f2
AO
2260 return &ret->root.root;
2261}
2262
8e45593f
NC
2263static bfd_boolean
2264sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2265 struct bfd_link_info *info, asection *p)
2266{
2267 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
2268
2269 /* Non-FDPIC binaries do not need dynamic symbols for sections. */
2270 if (!htab->fdpic_p)
2271 return TRUE;
2272
2273 /* We need dynamic symbols for every section, since segments can
2274 relocate independently. */
2275 switch (elf_section_data (p)->this_hdr.sh_type)
2276 {
2277 case SHT_PROGBITS:
2278 case SHT_NOBITS:
2279 /* If sh_type is yet undecided, assume it could be
2280 SHT_PROGBITS/SHT_NOBITS. */
2281 case SHT_NULL:
2282 return FALSE;
2283
2284 /* There shouldn't be section relative relocations
2285 against any other section. */
2286 default:
2287 return TRUE;
2288 }
2289}
2290
067653c5
AM
2291/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2292 shortcuts to them in our hash table. */
2293
b34976b6 2294static bfd_boolean
09fd220b 2295create_got_section (bfd *dynobj, struct bfd_link_info *info)
067653c5
AM
2296{
2297 struct elf_sh_link_hash_table *htab;
2298
2299 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 2300 return FALSE;
067653c5
AM
2301
2302 htab = sh_elf_hash_table (info);
4dfe6ac6
NC
2303 if (htab == NULL)
2304 return FALSE;
2305
3d4d4302
AM
2306 htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
2307 (SEC_ALLOC | SEC_LOAD
2308 | SEC_HAS_CONTENTS
2309 | SEC_IN_MEMORY
2310 | SEC_LINKER_CREATED));
8e45593f 2311 if (htab->sfuncdesc == NULL
fd361982 2312 || !bfd_set_section_alignment (htab->sfuncdesc, 2))
8e45593f
NC
2313 return FALSE;
2314
3d4d4302
AM
2315 htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
2316 ".rela.got.funcdesc",
2317 (SEC_ALLOC | SEC_LOAD
2318 | SEC_HAS_CONTENTS
2319 | SEC_IN_MEMORY
2320 | SEC_LINKER_CREATED
2321 | SEC_READONLY));
8e45593f 2322 if (htab->srelfuncdesc == NULL
fd361982 2323 || !bfd_set_section_alignment (htab->srelfuncdesc, 2))
8e45593f
NC
2324 return FALSE;
2325
2326 /* Also create .rofixup. */
3d4d4302
AM
2327 htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
2328 (SEC_ALLOC | SEC_LOAD
2329 | SEC_HAS_CONTENTS
2330 | SEC_IN_MEMORY
2331 | SEC_LINKER_CREATED
2332 | SEC_READONLY));
8e45593f 2333 if (htab->srofixup == NULL
fd361982 2334 || !bfd_set_section_alignment (htab->srofixup, 2))
8e45593f
NC
2335 return FALSE;
2336
b34976b6 2337 return TRUE;
067653c5
AM
2338}
2339
37c644f2
AO
2340/* Create dynamic sections when linking against a dynamic object. */
2341
b34976b6 2342static bfd_boolean
09fd220b 2343sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
37c644f2 2344{
067653c5 2345 struct elf_sh_link_hash_table *htab;
37c644f2 2346 flagword flags, pltflags;
91d6fa6a 2347 asection *s;
9c5bfbb7 2348 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
37c644f2
AO
2349 int ptralign = 0;
2350
2351 switch (bed->s->arch_size)
2352 {
2353 case 32:
2354 ptralign = 2;
2355 break;
2356
2357 case 64:
2358 ptralign = 3;
2359 break;
2360
2361 default:
2362 bfd_set_error (bfd_error_bad_value);
b34976b6 2363 return FALSE;
37c644f2
AO
2364 }
2365
067653c5 2366 htab = sh_elf_hash_table (info);
4dfe6ac6
NC
2367 if (htab == NULL)
2368 return FALSE;
2369
35cad4c4
KK
2370 if (htab->root.dynamic_sections_created)
2371 return TRUE;
067653c5 2372
37c644f2
AO
2373 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2374 .rel[a].bss sections. */
2375
2376 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2377 | SEC_LINKER_CREATED);
2378
2379 pltflags = flags;
2380 pltflags |= SEC_CODE;
2381 if (bed->plt_not_loaded)
2382 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2383 if (bed->plt_readonly)
2384 pltflags |= SEC_READONLY;
2385
3d4d4302 2386 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
ce558b89 2387 htab->root.splt = s;
35cad4c4 2388 if (s == NULL
fd361982 2389 || !bfd_set_section_alignment (s, bed->plt_alignment))
35cad4c4 2390 return FALSE;
37c644f2
AO
2391
2392 if (bed->want_plt_sym)
2393 {
2394 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2395 .plt section. */
14a793b2
AM
2396 struct elf_link_hash_entry *h;
2397 struct bfd_link_hash_entry *bh = NULL;
2398
37c644f2
AO
2399 if (! (_bfd_generic_link_add_one_symbol
2400 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
b34976b6 2401 (bfd_vma) 0, (const char *) NULL, FALSE,
14a793b2 2402 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 2403 return FALSE;
14a793b2
AM
2404
2405 h = (struct elf_link_hash_entry *) bh;
f5385ebf 2406 h->def_regular = 1;
37c644f2 2407 h->type = STT_OBJECT;
7325306f 2408 htab->root.hplt = h;
37c644f2 2409
0e1862bb 2410 if (bfd_link_pic (info)
c152c796 2411 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2412 return FALSE;
37c644f2
AO
2413 }
2414
3d4d4302
AM
2415 s = bfd_make_section_anyway_with_flags (abfd,
2416 bed->default_use_rela_p
2417 ? ".rela.plt" : ".rel.plt",
2418 flags | SEC_READONLY);
ce558b89 2419 htab->root.srelplt = s;
35cad4c4 2420 if (s == NULL
fd361982 2421 || !bfd_set_section_alignment (s, ptralign))
35cad4c4 2422 return FALSE;
37c644f2 2423
ce558b89 2424 if (htab->root.sgot == NULL
2293c499 2425 && !create_got_section (abfd, info))
b34976b6 2426 return FALSE;
37c644f2 2427
37c644f2
AO
2428 if (bed->want_dynbss)
2429 {
2430 /* The .dynbss section is a place to put symbols which are defined
2431 by dynamic objects, are referenced by regular objects, and are
2432 not functions. We must allocate space for them in the process
2433 image and use a R_*_COPY reloc to tell the dynamic linker to
2434 initialize them at run time. The linker script puts the .dynbss
2435 section into the .bss section of the final image. */
3d4d4302
AM
2436 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2437 SEC_ALLOC | SEC_LINKER_CREATED);
f1dfbfdb 2438 htab->root.sdynbss = s;
3496cb2a 2439 if (s == NULL)
35cad4c4 2440 return FALSE;
37c644f2
AO
2441
2442 /* The .rel[a].bss section holds copy relocs. This section is not
2443 normally needed. We need to create it here, though, so that the
2444 linker will map it to an output section. We can't just create it
2445 only if we need it, because we will not know whether we need it
2446 until we have seen all the input files, and the first time the
2447 main linker code calls BFD after examining all the input files
2448 (size_dynamic_sections) the input sections have already been
2449 mapped to the output sections. If the section turns out not to
2450 be needed, we can discard it later. We will never need this
2451 section when generating a shared object, since they do not use
2452 copy relocs. */
0e1862bb 2453 if (! bfd_link_pic (info))
37c644f2 2454 {
3d4d4302
AM
2455 s = bfd_make_section_anyway_with_flags (abfd,
2456 (bed->default_use_rela_p
2457 ? ".rela.bss" : ".rel.bss"),
2458 flags | SEC_READONLY);
f1dfbfdb 2459 htab->root.srelbss = s;
37c644f2 2460 if (s == NULL
fd361982 2461 || !bfd_set_section_alignment (s, ptralign))
b34976b6 2462 return FALSE;
37c644f2
AO
2463 }
2464 }
2465
90c14f0c 2466 if (htab->root.target_os == is_vxworks)
55e6e397
RS
2467 {
2468 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2469 return FALSE;
2470 }
2471
b34976b6 2472 return TRUE;
37c644f2
AO
2473}
2474\f
37c644f2
AO
2475/* Adjust a symbol defined by a dynamic object and referenced by a
2476 regular object. The current definition is in some section of the
2477 dynamic object, but we're not including those sections. We have to
2478 change the definition to something the rest of the link can
2479 understand. */
2480
b34976b6 2481static bfd_boolean
09fd220b
KK
2482sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2483 struct elf_link_hash_entry *h)
37c644f2 2484{
067653c5 2485 struct elf_sh_link_hash_table *htab;
37c644f2 2486 asection *s;
37c644f2 2487
2293c499 2488 htab = sh_elf_hash_table (info);
4dfe6ac6
NC
2489 if (htab == NULL)
2490 return FALSE;
37c644f2
AO
2491
2492 /* Make sure we know what is going on here. */
2293c499 2493 BFD_ASSERT (htab->root.dynobj != NULL
f5385ebf 2494 && (h->needs_plt
60d67dc8 2495 || h->is_weakalias
f5385ebf
AM
2496 || (h->def_dynamic
2497 && h->ref_regular
2498 && !h->def_regular)));
37c644f2
AO
2499
2500 /* If this is a function, put it in the procedure linkage table. We
2501 will fill in the contents of the procedure linkage table later,
2502 when we know the address of the .got section. */
2503 if (h->type == STT_FUNC
f5385ebf 2504 || h->needs_plt)
37c644f2 2505 {
067653c5 2506 if (h->plt.refcount <= 0
3d85aebe
KK
2507 || SYMBOL_CALLS_LOCAL (info, h)
2508 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2509 && h->root.type == bfd_link_hash_undefweak))
37c644f2
AO
2510 {
2511 /* This case can occur if we saw a PLT reloc in an input
2512 file, but the symbol was never referred to by a dynamic
2513 object. In such a case, we don't actually need to build
2514 a procedure linkage table, and we can just do a REL32
2515 reloc instead. */
067653c5 2516 h->plt.offset = (bfd_vma) -1;
f5385ebf 2517 h->needs_plt = 0;
37c644f2
AO
2518 }
2519
b34976b6 2520 return TRUE;
37c644f2 2521 }
067653c5
AM
2522 else
2523 h->plt.offset = (bfd_vma) -1;
37c644f2
AO
2524
2525 /* If this is a weak symbol, and there is a real definition, the
2526 processor independent code will have arranged for us to see the
2527 real definition first, and we can just use the same value. */
60d67dc8 2528 if (h->is_weakalias)
37c644f2 2529 {
60d67dc8
AM
2530 struct elf_link_hash_entry *def = weakdef (h);
2531 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2532 h->root.u.def.section = def->root.u.def.section;
2533 h->root.u.def.value = def->root.u.def.value;
04e534c3 2534 if (info->nocopyreloc)
60d67dc8 2535 h->non_got_ref = def->non_got_ref;
b34976b6 2536 return TRUE;
37c644f2
AO
2537 }
2538
2539 /* This is a reference to a symbol defined by a dynamic object which
2540 is not a function. */
2541
2542 /* If we are creating a shared library, we must presume that the
2543 only references to the symbol are via the global offset table.
2544 For such cases we need not do anything here; the relocations will
2545 be handled correctly by relocate_section. */
0e1862bb 2546 if (bfd_link_pic (info))
b34976b6 2547 return TRUE;
37c644f2
AO
2548
2549 /* If there are no references to this symbol that do not use the
2550 GOT, we don't need to generate a copy reloc. */
f5385ebf 2551 if (!h->non_got_ref)
b34976b6 2552 return TRUE;
37c644f2 2553
067653c5 2554 /* If -z nocopyreloc was given, we won't generate them either. */
3bf083ed 2555 if (0 && info->nocopyreloc)
067653c5 2556 {
f5385ebf 2557 h->non_got_ref = 0;
b34976b6 2558 return TRUE;
067653c5
AM
2559 }
2560
3bf083ed
AM
2561 /* If we don't find any dynamic relocs in read-only sections, then
2562 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5dbc8b37 2563 if (0 && !_bfd_elf_readonly_dynrelocs (h))
067653c5 2564 {
f5385ebf 2565 h->non_got_ref = 0;
b34976b6 2566 return TRUE;
067653c5
AM
2567 }
2568
37c644f2
AO
2569 /* We must allocate the symbol in our .dynbss section, which will
2570 become part of the .bss section of the executable. There will be
2571 an entry for this symbol in the .dynsym section. The dynamic
2572 object will contain position independent code, so all references
2573 from the dynamic object to this symbol will go through the global
2574 offset table. The dynamic linker will use the .dynsym entry to
2575 determine the address it must put in the global offset table, so
2576 both the dynamic object and the regular object will refer to the
2577 same memory location for the variable. */
2578
f1dfbfdb 2579 s = htab->root.sdynbss;
37c644f2
AO
2580 BFD_ASSERT (s != NULL);
2581
2582 /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2583 copy the initial value out of the dynamic object and into the
2584 runtime process image. We need to remember the offset into the
2585 .rela.bss section we are going to use. */
1d7e9d18 2586 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
37c644f2
AO
2587 {
2588 asection *srel;
2589
f1dfbfdb 2590 srel = htab->root.srelbss;
37c644f2 2591 BFD_ASSERT (srel != NULL);
eea6121a 2592 srel->size += sizeof (Elf32_External_Rela);
f5385ebf 2593 h->needs_copy = 1;
37c644f2
AO
2594 }
2595
6cabe1ea 2596 return _bfd_elf_adjust_dynamic_copy (info, h, s);
37c644f2
AO
2597}
2598
067653c5
AM
2599/* Allocate space in .plt, .got and associated reloc sections for
2600 dynamic relocs. */
2601
b34976b6 2602static bfd_boolean
09fd220b 2603allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
067653c5
AM
2604{
2605 struct bfd_link_info *info;
2606 struct elf_sh_link_hash_table *htab;
2607 struct elf_sh_link_hash_entry *eh;
3bf083ed 2608 struct elf_dyn_relocs *p;
067653c5
AM
2609
2610 if (h->root.type == bfd_link_hash_indirect)
b34976b6 2611 return TRUE;
067653c5 2612
067653c5
AM
2613 info = (struct bfd_link_info *) inf;
2614 htab = sh_elf_hash_table (info);
4dfe6ac6
NC
2615 if (htab == NULL)
2616 return FALSE;
067653c5 2617
4989d792
SC
2618 eh = (struct elf_sh_link_hash_entry *) h;
2619 if ((h->got.refcount > 0
f5385ebf 2620 || h->forced_local)
4989d792
SC
2621 && eh->gotplt_refcount > 0)
2622 {
2623 /* The symbol has been forced local, or we have some direct got refs,
26e41594 2624 so treat all the gotplt refs as got refs. */
4989d792
SC
2625 h->got.refcount += eh->gotplt_refcount;
2626 if (h->plt.refcount >= eh->gotplt_refcount)
2627 h->plt.refcount -= eh->gotplt_refcount;
2628 }
2629
067653c5 2630 if (htab->root.dynamic_sections_created
04e534c3
KK
2631 && h->plt.refcount > 0
2632 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2633 || h->root.type != bfd_link_hash_undefweak))
067653c5
AM
2634 {
2635 /* Make sure this symbol is output as a dynamic symbol.
2636 Undefined weak syms won't yet be marked as dynamic. */
2637 if (h->dynindx == -1
f5385ebf 2638 && !h->forced_local)
067653c5 2639 {
c152c796 2640 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2641 return FALSE;
067653c5
AM
2642 }
2643
0e1862bb 2644 if (bfd_link_pic (info)
267fb3c1 2645 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
067653c5 2646 {
ce558b89 2647 asection *s = htab->root.splt;
8e45593f 2648 const struct elf_sh_plt_info *plt_info;
067653c5
AM
2649
2650 /* If this is the first .plt entry, make room for the special
2651 first entry. */
eea6121a 2652 if (s->size == 0)
55e6e397 2653 s->size += htab->plt_info->plt0_entry_size;
067653c5 2654
eea6121a 2655 h->plt.offset = s->size;
067653c5
AM
2656
2657 /* If this symbol is not defined in a regular file, and we are
2658 not generating a shared library, then set the symbol to this
2659 location in the .plt. This is required to make function
2660 pointers compare as equal between the normal executable and
8e45593f
NC
2661 the shared library. Skip this for FDPIC, since the
2662 function's address will be the address of the canonical
2663 function descriptor. */
0e1862bb 2664 if (!htab->fdpic_p && !bfd_link_pic (info) && !h->def_regular)
067653c5
AM
2665 {
2666 h->root.u.def.section = s;
2667 h->root.u.def.value = h->plt.offset;
2668 }
2669
2670 /* Make room for this entry. */
8e45593f
NC
2671 plt_info = htab->plt_info;
2672 if (plt_info->short_plt != NULL
2673 && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
2674 plt_info = plt_info->short_plt;
2675 s->size += plt_info->symbol_entry_size;
067653c5
AM
2676
2677 /* We also need to make an entry in the .got.plt section, which
2678 will be placed in the .got section by the linker script. */
8e45593f 2679 if (!htab->fdpic_p)
ce558b89 2680 htab->root.sgotplt->size += 4;
8e45593f 2681 else
ce558b89 2682 htab->root.sgotplt->size += 8;
067653c5
AM
2683
2684 /* We also need to make an entry in the .rel.plt section. */
ce558b89 2685 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
55e6e397 2686
90c14f0c 2687 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
55e6e397
RS
2688 {
2689 /* VxWorks executables have a second set of relocations
2690 for each PLT entry. They go in a separate relocation
2691 section, which is processed by the kernel loader. */
2692
2693 /* There is a relocation for the initial PLT entry:
2694 an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_. */
2695 if (h->plt.offset == htab->plt_info->plt0_entry_size)
2696 htab->srelplt2->size += sizeof (Elf32_External_Rela);
2697
2698 /* There are two extra relocations for each subsequent
2699 PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2700 and an R_SH_DIR32 relocation for the PLT entry. */
2701 htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
2702 }
067653c5
AM
2703 }
2704 else
2705 {
2706 h->plt.offset = (bfd_vma) -1;
f5385ebf 2707 h->needs_plt = 0;
067653c5
AM
2708 }
2709 }
2710 else
2711 {
2712 h->plt.offset = (bfd_vma) -1;
f5385ebf 2713 h->needs_plt = 0;
067653c5
AM
2714 }
2715
2716 if (h->got.refcount > 0)
2717 {
2718 asection *s;
b34976b6 2719 bfd_boolean dyn;
85e02784 2720 enum got_type got_type = sh_elf_hash_entry (h)->got_type;
067653c5
AM
2721
2722 /* Make sure this symbol is output as a dynamic symbol.
2723 Undefined weak syms won't yet be marked as dynamic. */
2724 if (h->dynindx == -1
f5385ebf 2725 && !h->forced_local)
067653c5 2726 {
c152c796 2727 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2728 return FALSE;
067653c5
AM
2729 }
2730
ce558b89 2731 s = htab->root.sgot;
eea6121a
AM
2732 h->got.offset = s->size;
2733 s->size += 4;
3376eaf5 2734 /* R_SH_TLS_GD needs 2 consecutive GOT slots. */
8e45593f 2735 if (got_type == GOT_TLS_GD)
eea6121a 2736 s->size += 4;
067653c5 2737 dyn = htab->root.dynamic_sections_created;
8e45593f
NC
2738 if (!dyn)
2739 {
2740 /* No dynamic relocations required. */
0e1862bb 2741 if (htab->fdpic_p && !bfd_link_pic (info)
8e45593f
NC
2742 && h->root.type != bfd_link_hash_undefweak
2743 && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
2744 htab->srofixup->size += 4;
2745 }
87458ed2 2746 /* No dynamic relocations required when IE->LE conversion happens. */
0e1862bb
L
2747 else if (got_type == GOT_TLS_IE
2748 && !h->def_dynamic
2749 && !bfd_link_pic (info))
87458ed2 2750 ;
267fb3c1 2751 /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
3376eaf5 2752 R_SH_TLS_GD needs one if local symbol and two if global. */
8e45593f
NC
2753 else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
2754 || got_type == GOT_TLS_IE)
ce558b89 2755 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
8e45593f 2756 else if (got_type == GOT_TLS_GD)
ce558b89 2757 htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
8e45593f
NC
2758 else if (got_type == GOT_FUNCDESC)
2759 {
0e1862bb 2760 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
8e45593f
NC
2761 htab->srofixup->size += 4;
2762 else
ce558b89 2763 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
8e45593f 2764 }
04e534c3
KK
2765 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2766 || h->root.type != bfd_link_hash_undefweak)
0e1862bb 2767 && (bfd_link_pic (info)
04e534c3 2768 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
ce558b89 2769 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
0e1862bb
L
2770 else if (htab->fdpic_p
2771 && !bfd_link_pic (info)
2772 && got_type == GOT_NORMAL
8e45593f
NC
2773 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2774 || h->root.type != bfd_link_hash_undefweak))
2775 htab->srofixup->size += 4;
067653c5
AM
2776 }
2777 else
2778 h->got.offset = (bfd_vma) -1;
2779
8e45593f
NC
2780 /* Allocate space for any dynamic relocations to function
2781 descriptors, canonical or otherwise. We need to relocate the
2782 reference unless it resolves to zero, which only happens for
2783 undefined weak symbols (either non-default visibility, or when
2784 static linking). Any GOT slot is accounted for elsewhere. */
2785 if (eh->abs_funcdesc_refcount > 0
2786 && (h->root.type != bfd_link_hash_undefweak
2787 || (htab->root.dynamic_sections_created
2788 && ! SYMBOL_CALLS_LOCAL (info, h))))
2789 {
0e1862bb 2790 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
8e45593f
NC
2791 htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
2792 else
ce558b89 2793 htab->root.srelgot->size
8e45593f
NC
2794 += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
2795 }
2796
2797 /* We must allocate a function descriptor if there are references to
2798 a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
2799 the dynamic linker isn't going to allocate it. None of this
2800 applies if we already created one in .got.plt, but if the
2801 canonical function descriptor can be in this object, there
2802 won't be a PLT entry at all. */
2803 if ((eh->funcdesc.refcount > 0
2804 || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
2805 && h->root.type != bfd_link_hash_undefweak
2806 && SYMBOL_FUNCDESC_LOCAL (info, h))
2807 {
2808 /* Make room for this function descriptor. */
2809 eh->funcdesc.offset = htab->sfuncdesc->size;
2810 htab->sfuncdesc->size += 8;
2811
2812 /* We will need a relocation or two fixups to initialize the
2813 function descriptor, so allocate those too. */
0e1862bb 2814 if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
8e45593f
NC
2815 htab->srofixup->size += 8;
2816 else
2817 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
2818 }
2819
190eb1dd 2820 if (h->dyn_relocs == NULL)
b34976b6 2821 return TRUE;
067653c5
AM
2822
2823 /* In the shared -Bsymbolic case, discard space allocated for
2824 dynamic pc-relative relocs against symbols which turn out to be
2825 defined in regular objects. For the normal shared case, discard
2826 space for pc-relative relocs that have become local due to symbol
2827 visibility changes. */
2828
0e1862bb 2829 if (bfd_link_pic (info))
067653c5 2830 {
3d85aebe 2831 if (SYMBOL_CALLS_LOCAL (info, h))
067653c5 2832 {
3bf083ed 2833 struct elf_dyn_relocs **pp;
067653c5 2834
190eb1dd 2835 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
067653c5
AM
2836 {
2837 p->count -= p->pc_count;
2838 p->pc_count = 0;
2839 if (p->count == 0)
2840 *pp = p->next;
2841 else
2842 pp = &p->next;
2843 }
2844 }
04e534c3 2845
90c14f0c 2846 if (htab->root.target_os == is_vxworks)
3348747a 2847 {
3bf083ed 2848 struct elf_dyn_relocs **pp;
3348747a 2849
190eb1dd 2850 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
3348747a
NS
2851 {
2852 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2853 *pp = p->next;
2854 else
2855 pp = &p->next;
2856 }
2857 }
2858
04e534c3
KK
2859 /* Also discard relocs on undefined weak syms with non-default
2860 visibility. */
190eb1dd 2861 if (h->dyn_relocs != NULL
04e534c3 2862 && h->root.type == bfd_link_hash_undefweak)
22d606e9 2863 {
6ee6e05a
L
2864 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2865 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 2866 h->dyn_relocs = NULL;
22d606e9
AM
2867
2868 /* Make sure undefined weak symbols are output as a dynamic
2869 symbol in PIEs. */
2870 else if (h->dynindx == -1
2871 && !h->forced_local)
2872 {
2873 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2874 return FALSE;
2875 }
2876 }
067653c5
AM
2877 }
2878 else
2879 {
2880 /* For the non-shared case, discard space for relocs against
2881 symbols which turn out to need copy relocs or are not
2882 dynamic. */
2883
f5385ebf
AM
2884 if (!h->non_got_ref
2885 && ((h->def_dynamic
2886 && !h->def_regular)
067653c5
AM
2887 || (htab->root.dynamic_sections_created
2888 && (h->root.type == bfd_link_hash_undefweak
2889 || h->root.type == bfd_link_hash_undefined))))
2890 {
2891 /* Make sure this symbol is output as a dynamic symbol.
2892 Undefined weak syms won't yet be marked as dynamic. */
2893 if (h->dynindx == -1
f5385ebf 2894 && !h->forced_local)
067653c5 2895 {
c152c796 2896 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2897 return FALSE;
067653c5
AM
2898 }
2899
2900 /* If that succeeded, we know we'll be keeping all the
2901 relocs. */
2902 if (h->dynindx != -1)
2903 goto keep;
2904 }
2905
190eb1dd 2906 h->dyn_relocs = NULL;
067653c5
AM
2907
2908 keep: ;
2909 }
2910
2911 /* Finally, allocate space. */
190eb1dd 2912 for (p = h->dyn_relocs; p != NULL; p = p->next)
067653c5
AM
2913 {
2914 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 2915 sreloc->size += p->count * sizeof (Elf32_External_Rela);
8e45593f
NC
2916
2917 /* If we need relocations, we do not need fixups. */
0e1862bb 2918 if (htab->fdpic_p && !bfd_link_pic (info))
8e45593f 2919 htab->srofixup->size -= 4 * (p->count - p->pc_count);
067653c5
AM
2920 }
2921
b34976b6 2922 return TRUE;
067653c5
AM
2923}
2924
55e6e397
RS
2925/* This function is called after all the input files have been read,
2926 and the input sections have been assigned to output sections.
2927 It's a convenient place to determine the PLT style. */
2928
2929static bfd_boolean
2930sh_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
2931{
0e1862bb
L
2932 sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd,
2933 bfd_link_pic (info));
8e45593f 2934
0e1862bb 2935 if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_relocatable (info)
04c3a755
NS
2936 && !bfd_elf_stack_segment_size (output_bfd, info,
2937 "__stacksize", DEFAULT_STACK_SIZE))
2938 return FALSE;
55e6e397
RS
2939 return TRUE;
2940}
2941
37c644f2
AO
2942/* Set the sizes of the dynamic sections. */
2943
b34976b6 2944static bfd_boolean
09fd220b
KK
2945sh_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2946 struct bfd_link_info *info)
37c644f2 2947{
067653c5 2948 struct elf_sh_link_hash_table *htab;
37c644f2
AO
2949 bfd *dynobj;
2950 asection *s;
b34976b6 2951 bfd_boolean relocs;
067653c5 2952 bfd *ibfd;
37c644f2 2953
067653c5 2954 htab = sh_elf_hash_table (info);
4dfe6ac6
NC
2955 if (htab == NULL)
2956 return FALSE;
2957
067653c5 2958 dynobj = htab->root.dynobj;
37c644f2
AO
2959 BFD_ASSERT (dynobj != NULL);
2960
067653c5 2961 if (htab->root.dynamic_sections_created)
37c644f2
AO
2962 {
2963 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 2964 if (bfd_link_executable (info) && !info->nointerp)
37c644f2 2965 {
3d4d4302 2966 s = bfd_get_linker_section (dynobj, ".interp");
37c644f2 2967 BFD_ASSERT (s != NULL);
eea6121a 2968 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
37c644f2
AO
2969 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2970 }
2971 }
37c644f2 2972
067653c5
AM
2973 /* Set up .got offsets for local syms, and space for local dynamic
2974 relocs. */
c72f2fb2 2975 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
37c644f2 2976 {
067653c5
AM
2977 bfd_signed_vma *local_got;
2978 bfd_signed_vma *end_local_got;
8e45593f
NC
2979 union gotref *local_funcdesc, *end_local_funcdesc;
2980 char *local_got_type;
067653c5
AM
2981 bfd_size_type locsymcount;
2982 Elf_Internal_Shdr *symtab_hdr;
2983 asection *srel;
37c644f2 2984
0ffa91dd 2985 if (! is_sh_elf (ibfd))
37c644f2
AO
2986 continue;
2987
067653c5 2988 for (s = ibfd->sections; s != NULL; s = s->next)
37c644f2 2989 {
3bf083ed 2990 struct elf_dyn_relocs *p;
067653c5 2991
3bf083ed 2992 for (p = ((struct elf_dyn_relocs *)
067653c5
AM
2993 elf_section_data (s)->local_dynrel);
2994 p != NULL;
2995 p = p->next)
37c644f2 2996 {
067653c5
AM
2997 if (! bfd_is_abs_section (p->sec)
2998 && bfd_is_abs_section (p->sec->output_section))
2999 {
3000 /* Input section has been discarded, either because
3001 it is a copy of a linkonce section or due to
3002 linker script /DISCARD/, so we'll be discarding
3003 the relocs too. */
3004 }
90c14f0c 3005 else if (htab->root.target_os == is_vxworks
3348747a
NS
3006 && strcmp (p->sec->output_section->name,
3007 ".tls_vars") == 0)
3008 {
3009 /* Relocations in vxworks .tls_vars sections are
3010 handled specially by the loader. */
3011 }
067653c5
AM
3012 else if (p->count != 0)
3013 {
3014 srel = elf_section_data (p->sec)->sreloc;
eea6121a 3015 srel->size += p->count * sizeof (Elf32_External_Rela);
067653c5 3016 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
a72f95f7
L
3017 {
3018 info->flags |= DF_TEXTREL;
871b3ab2 3019 info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
a72f95f7
L
3020 p->sec->owner, p->sec);
3021 }
8e45593f
NC
3022
3023 /* If we need relocations, we do not need fixups. */
0e1862bb 3024 if (htab->fdpic_p && !bfd_link_pic (info))
8e45593f 3025 htab->srofixup->size -= 4 * (p->count - p->pc_count);
067653c5 3026 }
37c644f2
AO
3027 }
3028 }
067653c5 3029
0ffa91dd 3030 symtab_hdr = &elf_symtab_hdr (ibfd);
067653c5 3031 locsymcount = symtab_hdr->sh_info;
ce558b89
AM
3032 s = htab->root.sgot;
3033 srel = htab->root.srelgot;
8e45593f
NC
3034
3035 local_got = elf_local_got_refcounts (ibfd);
3036 if (local_got)
37c644f2 3037 {
8e45593f
NC
3038 end_local_got = local_got + locsymcount;
3039 local_got_type = sh_elf_local_got_type (ibfd);
3040 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3041 for (; local_got < end_local_got; ++local_got)
37c644f2 3042 {
8e45593f
NC
3043 if (*local_got > 0)
3044 {
3045 *local_got = s->size;
3046 s->size += 4;
3047 if (*local_got_type == GOT_TLS_GD)
3048 s->size += 4;
0e1862bb 3049 if (bfd_link_pic (info))
8e45593f
NC
3050 srel->size += sizeof (Elf32_External_Rela);
3051 else
3052 htab->srofixup->size += 4;
3053
3054 if (*local_got_type == GOT_FUNCDESC)
3055 {
3056 if (local_funcdesc == NULL)
3057 {
3058 bfd_size_type size;
3059
3060 size = locsymcount * sizeof (union gotref);
3061 local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
3062 size);
3063 if (local_funcdesc == NULL)
3064 return FALSE;
3065 sh_elf_local_funcdesc (ibfd) = local_funcdesc;
3066 local_funcdesc += (local_got
3067 - elf_local_got_refcounts (ibfd));
3068 }
3069 local_funcdesc->refcount++;
3070 ++local_funcdesc;
3071 }
3072 }
3073 else
3074 *local_got = (bfd_vma) -1;
3075 ++local_got_type;
3076 }
3077 }
3078
3079 local_funcdesc = sh_elf_local_funcdesc (ibfd);
3080 if (local_funcdesc)
3081 {
3082 end_local_funcdesc = local_funcdesc + locsymcount;
3083
3084 for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
3085 {
3086 if (local_funcdesc->refcount > 0)
3087 {
3088 local_funcdesc->offset = htab->sfuncdesc->size;
3089 htab->sfuncdesc->size += 8;
0e1862bb 3090 if (!bfd_link_pic (info))
8e45593f
NC
3091 htab->srofixup->size += 8;
3092 else
3093 htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3094 }
3095 else
3096 local_funcdesc->offset = MINUS_ONE;
37c644f2 3097 }
37c644f2 3098 }
8e45593f 3099
067653c5
AM
3100 }
3101
3376eaf5
KK
3102 if (htab->tls_ldm_got.refcount > 0)
3103 {
3104 /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3105 relocs. */
ce558b89
AM
3106 htab->tls_ldm_got.offset = htab->root.sgot->size;
3107 htab->root.sgot->size += 8;
3108 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3376eaf5
KK
3109 }
3110 else
3111 htab->tls_ldm_got.offset = -1;
3112
8e45593f
NC
3113 /* Only the reserved entries should be present. For FDPIC, they go at
3114 the end of .got.plt. */
3115 if (htab->fdpic_p)
3116 {
ce558b89
AM
3117 BFD_ASSERT (htab->root.sgotplt && htab->root.sgotplt->size == 12);
3118 htab->root.sgotplt->size = 0;
8e45593f
NC
3119 }
3120
067653c5
AM
3121 /* Allocate global sym .plt and .got entries, and space for global
3122 sym dynamic relocs. */
09fd220b 3123 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
067653c5 3124
8e45593f
NC
3125 /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3126 end of the FDPIC .got.plt. */
3127 if (htab->fdpic_p)
3128 {
ce558b89
AM
3129 htab->root.hgot->root.u.def.value = htab->root.sgotplt->size;
3130 htab->root.sgotplt->size += 12;
8e45593f
NC
3131 }
3132
3133 /* At the very end of the .rofixup section is a pointer to the GOT. */
3134 if (htab->fdpic_p && htab->srofixup != NULL)
3135 htab->srofixup->size += 4;
3136
067653c5
AM
3137 /* We now have determined the sizes of the various dynamic sections.
3138 Allocate memory for them. */
b34976b6 3139 relocs = FALSE;
067653c5
AM
3140 for (s = dynobj->sections; s != NULL; s = s->next)
3141 {
3142 if ((s->flags & SEC_LINKER_CREATED) == 0)
3143 continue;
3144
ce558b89
AM
3145 if (s == htab->root.splt
3146 || s == htab->root.sgot
3147 || s == htab->root.sgotplt
8e45593f
NC
3148 || s == htab->sfuncdesc
3149 || s == htab->srofixup
f1dfbfdb 3150 || s == htab->root.sdynbss)
067653c5
AM
3151 {
3152 /* Strip this section if we don't need it; see the
3153 comment below. */
3154 }
fd361982 3155 else if (CONST_STRNEQ (bfd_section_name (s), ".rela"))
067653c5 3156 {
ce558b89 3157 if (s->size != 0 && s != htab->root.srelplt && s != htab->srelplt2)
b34976b6 3158 relocs = TRUE;
067653c5
AM
3159
3160 /* We use the reloc_count field as a counter if we need
3161 to copy relocs into the output file. */
3162 s->reloc_count = 0;
3163 }
3164 else
37c644f2
AO
3165 {
3166 /* It's not one of our sections, so don't allocate space. */
3167 continue;
3168 }
3169
eea6121a 3170 if (s->size == 0)
37c644f2 3171 {
067653c5
AM
3172 /* If we don't need this section, strip it from the
3173 output file. This is mostly to handle .rela.bss and
3174 .rela.plt. We must create both sections in
3175 create_dynamic_sections, because they must be created
3176 before the linker maps input sections to output
3177 sections. The linker does that before
3178 adjust_dynamic_symbol is called, and it is that
3179 function which decides whether anything needs to go
3180 into these sections. */
3181
8423293d 3182 s->flags |= SEC_EXCLUDE;
37c644f2
AO
3183 continue;
3184 }
3185
c456f082
AM
3186 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3187 continue;
3188
067653c5
AM
3189 /* Allocate memory for the section contents. We use bfd_zalloc
3190 here in case unused entries are not reclaimed before the
3191 section's contents are written out. This should not happen,
3192 but this way if it does, we get a R_SH_NONE reloc instead
3193 of garbage. */
eea6121a 3194 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
067653c5 3195 if (s->contents == NULL)
b34976b6 3196 return FALSE;
37c644f2
AO
3197 }
3198
3084d7a2
L
3199 return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
3200 relocs);
37c644f2 3201}
37c644f2 3202\f
8e45593f 3203/* Add a dynamic relocation to the SRELOC section. */
37c644f2 3204
8e45593f
NC
3205inline static bfd_vma
3206sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
3207 int reloc_type, long dynindx, bfd_vma addend)
37c644f2 3208{
8e45593f
NC
3209 Elf_Internal_Rela outrel;
3210 bfd_vma reloc_offset;
37c644f2 3211
8e45593f
NC
3212 outrel.r_offset = offset;
3213 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
3214 outrel.r_addend = addend;
0ffa91dd 3215
8e45593f
NC
3216 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
3217 BFD_ASSERT (reloc_offset < sreloc->size);
3218 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3219 sreloc->contents + reloc_offset);
3220 sreloc->reloc_count++;
37c644f2 3221
8e45593f
NC
3222 return reloc_offset;
3223}
37c644f2 3224
8e45593f 3225/* Add an FDPIC read-only fixup. */
37c644f2 3226
8e45593f
NC
3227inline static void
3228sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3229{
3230 bfd_vma fixup_offset;
3231
3232 fixup_offset = srofixup->reloc_count++ * 4;
3233 BFD_ASSERT (fixup_offset < srofixup->size);
3234 bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3235}
3236
3237/* Return the offset of the generated .got section from the
3238 _GLOBAL_OFFSET_TABLE_ symbol. */
3239
3240static bfd_signed_vma
3241sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
3242{
ce558b89 3243 return (htab->root.sgot->output_offset - htab->root.sgotplt->output_offset
8e45593f
NC
3244 - htab->root.hgot->root.u.def.value);
3245}
3246
3247/* Find the segment number in which OSEC, and output section, is
3248 located. */
3249
3250static unsigned
3251sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
3252{
3d301fdc
AM
3253 Elf_Internal_Phdr *p = NULL;
3254
5bc8cb6f
NC
3255 if (output_bfd->xvec->flavour == bfd_target_elf_flavour
3256 /* PR ld/17110: Do not look for output segments in an input bfd. */
3257 && output_bfd->direction != read_direction)
3d301fdc 3258 p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
8e45593f
NC
3259
3260 /* FIXME: Nothing ever says what this index is relative to. The kernel
3261 supplies data in terms of the number of load segments but this is
3262 a phdr index and the first phdr may not be a load segment. */
3263 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
3264}
3265
3266static bfd_boolean
3267sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
3268{
3269 unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
3270
3d301fdc
AM
3271 return (seg != (unsigned) -1
3272 && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
8e45593f
NC
3273}
3274
3275/* Generate the initial contents of a local function descriptor, along
3276 with any relocations or fixups required. */
3277static bfd_boolean
3278sh_elf_initialize_funcdesc (bfd *output_bfd,
3279 struct bfd_link_info *info,
3280 struct elf_link_hash_entry *h,
3281 bfd_vma offset,
3282 asection *section,
3283 bfd_vma value)
3284{
3285 struct elf_sh_link_hash_table *htab;
3286 int dynindx;
3287 bfd_vma addr, seg;
3288
3289 htab = sh_elf_hash_table (info);
3290
3291 /* FIXME: The ABI says that the offset to the function goes in the
3292 descriptor, along with the segment index. We're RELA, so it could
3293 go in the reloc instead... */
3294
3295 if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
3296 {
3297 section = h->root.u.def.section;
3298 value = h->root.u.def.value;
3299 }
3300
3301 if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
3302 {
3303 dynindx = elf_section_data (section->output_section)->dynindx;
3304 addr = value + section->output_offset;
3305 seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
3306 }
3307 else
3308 {
3309 BFD_ASSERT (h->dynindx != -1);
3310 dynindx = h->dynindx;
3311 addr = seg = 0;
3312 }
3313
0e1862bb 3314 if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
8e45593f
NC
3315 {
3316 if (h == NULL || h->root.type != bfd_link_hash_undefweak)
3317 {
3318 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3319 offset
3320 + htab->sfuncdesc->output_section->vma
3321 + htab->sfuncdesc->output_offset);
3322 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3323 offset + 4
3324 + htab->sfuncdesc->output_section->vma
3325 + htab->sfuncdesc->output_offset);
3326 }
3327
3328 /* There are no dynamic relocations so fill in the final
3329 address and gp value (barring fixups). */
3330 addr += section->output_section->vma;
3331 seg = htab->root.hgot->root.u.def.value
3332 + htab->root.hgot->root.u.def.section->output_section->vma
3333 + htab->root.hgot->root.u.def.section->output_offset;
3334 }
3335 else
3336 sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
3337 offset
3338 + htab->sfuncdesc->output_section->vma
3339 + htab->sfuncdesc->output_offset,
3340 R_SH_FUNCDESC_VALUE, dynindx, 0);
3341
3342 bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
3343 bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
3344
3345 return TRUE;
3346}
3347
3348/* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3349 VALUE is the field's value. Return bfd_reloc_ok if successful or an error
3350 otherwise. */
3351
3352static bfd_reloc_status_type
3353install_movi20_field (bfd *output_bfd, unsigned long relocation,
3354 bfd *input_bfd, asection *input_section,
3355 bfd_byte *contents, bfd_vma offset)
3356{
3357 unsigned long cur_val;
3358 bfd_byte *addr;
3359 bfd_reloc_status_type r;
3360
3361 if (offset > bfd_get_section_limit (input_bfd, input_section))
3362 return bfd_reloc_outofrange;
3363
3364 r = bfd_check_overflow (complain_overflow_signed, 20, 0,
3365 bfd_arch_bits_per_address (input_bfd), relocation);
3366 if (r != bfd_reloc_ok)
3367 return r;
3368
3369 addr = contents + offset;
3370 cur_val = bfd_get_16 (output_bfd, addr);
3371 bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
3372 bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
3373
3374 return bfd_reloc_ok;
3375}
3376
3377/* Relocate an SH ELF section. */
3378
3379static bfd_boolean
3380sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3381 bfd *input_bfd, asection *input_section,
3382 bfd_byte *contents, Elf_Internal_Rela *relocs,
3383 Elf_Internal_Sym *local_syms,
3384 asection **local_sections)
3385{
3386 struct elf_sh_link_hash_table *htab;
3387 Elf_Internal_Shdr *symtab_hdr;
3388 struct elf_link_hash_entry **sym_hashes;
3389 Elf_Internal_Rela *rel, *relend;
8e45593f 3390 bfd_vma *local_got_offsets;
e0af1f53
MR
3391 asection *sgot = NULL;
3392 asection *sgotplt = NULL;
3393 asection *splt = NULL;
3394 asection *sreloc = NULL;
3395 asection *srelgot = NULL;
8e45593f
NC
3396 bfd_boolean is_vxworks_tls;
3397 unsigned isec_segment, got_segment, plt_segment, check_segment[2];
e0af1f53 3398 bfd_boolean fdpic_p = FALSE;
8e45593f 3399
7af5d5c4
AM
3400 if (!is_sh_elf (input_bfd))
3401 {
3402 bfd_set_error (bfd_error_wrong_format);
3403 return FALSE;
3404 }
8e45593f
NC
3405
3406 htab = sh_elf_hash_table (info);
e0af1f53
MR
3407 if (htab != NULL)
3408 {
ce558b89
AM
3409 sgot = htab->root.sgot;
3410 sgotplt = htab->root.sgotplt;
3411 srelgot = htab->root.srelgot;
3412 splt = htab->root.splt;
e0af1f53
MR
3413 fdpic_p = htab->fdpic_p;
3414 }
8e45593f
NC
3415 symtab_hdr = &elf_symtab_hdr (input_bfd);
3416 sym_hashes = elf_sym_hashes (input_bfd);
8e45593f
NC
3417 local_got_offsets = elf_local_got_offsets (input_bfd);
3418
3419 isec_segment = sh_elf_osec_to_segment (output_bfd,
3420 input_section->output_section);
e0af1f53 3421 if (fdpic_p && sgot)
8e45593f 3422 got_segment = sh_elf_osec_to_segment (output_bfd,
e0af1f53 3423 sgot->output_section);
8e45593f
NC
3424 else
3425 got_segment = -1;
e0af1f53 3426 if (fdpic_p && splt)
8e45593f 3427 plt_segment = sh_elf_osec_to_segment (output_bfd,
e0af1f53 3428 splt->output_section);
8e45593f
NC
3429 else
3430 plt_segment = -1;
3431
8e45593f
NC
3432 /* We have to handle relocations in vxworks .tls_vars sections
3433 specially, because the dynamic loader is 'weird'. */
90c14f0c 3434 is_vxworks_tls = (htab && htab->root.target_os == is_vxworks && bfd_link_pic (info)
8e45593f
NC
3435 && !strcmp (input_section->output_section->name,
3436 ".tls_vars"));
3437
3438 rel = relocs;
3439 relend = relocs + input_section->reloc_count;
3440 for (; rel < relend; rel++)
3441 {
3442 int r_type;
3443 reloc_howto_type *howto;
3444 unsigned long r_symndx;
3445 Elf_Internal_Sym *sym;
3446 asection *sec;
3447 struct elf_link_hash_entry *h;
3448 bfd_vma relocation;
3449 bfd_vma addend = (bfd_vma) 0;
3450 bfd_reloc_status_type r;
8e45593f 3451 bfd_vma off;
85e02784 3452 enum got_type got_type;
8e45593f 3453 const char *symname = NULL;
6ee6e05a 3454 bfd_boolean resolved_to_zero;
8e45593f
NC
3455
3456 r_symndx = ELF32_R_SYM (rel->r_info);
37c644f2 3457
37c644f2
AO
3458 r_type = ELF32_R_TYPE (rel->r_info);
3459
3460 /* Many of the relocs are only used for relaxing, and are
067653c5 3461 handled entirely by the relaxation code. */
d38eb334
DD
3462 if (r_type >= (int) R_SH_GNU_VTINHERIT
3463 && r_type <= (int) R_SH_LABEL)
37c644f2 3464 continue;
c5aeb40f
AO
3465 if (r_type == (int) R_SH_NONE)
3466 continue;
37c644f2
AO
3467
3468 if (r_type < 0
3469 || r_type >= R_SH_max
3470 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
06bb75c1 3471 && r_type <= (int) R_SH_LAST_INVALID_RELOC)
8e45593f
NC
3472 || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3473 && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
fbca6ad9
AO
3474 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3475 && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3476 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3477 && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3376eaf5
KK
3478 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3479 && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
8e45593f
NC
3480 || ( r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
3481 && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
37c644f2
AO
3482 {
3483 bfd_set_error (bfd_error_bad_value);
b34976b6 3484 return FALSE;
37c644f2
AO
3485 }
3486
55e6e397 3487 howto = get_howto_table (output_bfd) + r_type;
37c644f2 3488
146be91a 3489 /* For relocs that aren't partial_inplace, we get the addend from
067653c5 3490 the relocation. */
146be91a
HPN
3491 if (! howto->partial_inplace)
3492 addend = rel->r_addend;
3493
6ee6e05a 3494 resolved_to_zero = FALSE;
37c644f2
AO
3495 h = NULL;
3496 sym = NULL;
3497 sec = NULL;
8e45593f
NC
3498 check_segment[0] = -1;
3499 check_segment[1] = -1;
37c644f2
AO
3500 if (r_symndx < symtab_hdr->sh_info)
3501 {
3502 sym = local_syms + r_symndx;
3503 sec = local_sections[r_symndx];
8e45593f
NC
3504
3505 symname = bfd_elf_string_from_elf_section
3506 (input_bfd, symtab_hdr->sh_link, sym->st_name);
3507 if (symname == NULL || *symname == '\0')
fd361982 3508 symname = bfd_section_name (sec);
8e45593f 3509
37c644f2
AO
3510 relocation = (sec->output_section->vma
3511 + sec->output_offset
3512 + sym->st_value);
ab96bf03 3513
dbaa2011 3514 if (sec != NULL && discarded_section (sec))
ab96bf03
AM
3515 /* Handled below. */
3516 ;
0e1862bb 3517 else if (bfd_link_relocatable (info))
8a3975e6 3518 {
1049f94e 3519 /* This is a relocatable link. We don't have to change
8a3975e6
AO
3520 anything, unless the reloc is against a section symbol,
3521 in which case we have to adjust according to where the
3522 section symbol winds up in the output section. */
8a3975e6 3523 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
146be91a
HPN
3524 {
3525 if (! howto->partial_inplace)
3526 {
3527 /* For relocations with the addend in the
3528 relocation, we need just to update the addend.
3529 All real relocs are of type partial_inplace; this
3530 code is mostly for completeness. */
ab96bf03 3531 rel->r_addend += sec->output_offset;
146be91a
HPN
3532
3533 continue;
3534 }
3535
3536 /* Relocs of type partial_inplace need to pick up the
3537 contents in the contents and add the offset resulting
3538 from the changed location of the section symbol.
3539 Using _bfd_final_link_relocate (e.g. goto
3540 final_link_relocate) here would be wrong, because
3541 relocations marked pc_relative would get the current
3542 location subtracted, and we must only do that at the
3543 final link. */
3544 r = _bfd_relocate_contents (howto, input_bfd,
3545 sec->output_offset
3546 + sym->st_value,
3547 contents + rel->r_offset);
3548 goto relocation_done;
3549 }
8a3975e6
AO
3550
3551 continue;
3552 }
f8df10f4
JJ
3553 else if (! howto->partial_inplace)
3554 {
8517fae7 3555 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
f8df10f4
JJ
3556 addend = rel->r_addend;
3557 }
3558 else if ((sec->flags & SEC_MERGE)
3559 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3560 {
3561 asection *msec;
3562
3563 if (howto->rightshift || howto->src_mask != 0xffffffff)
3564 {
4eca0228 3565 _bfd_error_handler
695344c0 3566 /* xgettext:c-format */
2dcf00ce
AM
3567 (_("%pB(%pA+%#" PRIx64 "): "
3568 "%s relocation against SEC_MERGE section"),
d003868e 3569 input_bfd, input_section,
2dcf00ce 3570 (uint64_t) rel->r_offset, howto->name);
b34976b6 3571 return FALSE;
f8df10f4
JJ
3572 }
3573
067653c5
AM
3574 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
3575 msec = sec;
3576 addend =
c629eae0 3577 _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
f8df10f4
JJ
3578 - relocation;
3579 addend += msec->output_section->vma + msec->output_offset;
3580 bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
3581 addend = 0;
3582 }
37c644f2
AO
3583 }
3584 else
3585 {
560e09e9
NC
3586 /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro. */
3587
ab96bf03 3588 relocation = 0;
37c644f2 3589 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8e45593f 3590 symname = h->root.root.string;
37c644f2
AO
3591 while (h->root.type == bfd_link_hash_indirect
3592 || h->root.type == bfd_link_hash_warning)
211dc24b 3593 h = (struct elf_link_hash_entry *) h->root.u.i.link;
37c644f2
AO
3594 if (h->root.type == bfd_link_hash_defined
3595 || h->root.type == bfd_link_hash_defweak)
3596 {
b34976b6 3597 bfd_boolean dyn;
067653c5 3598
e0af1f53 3599 dyn = htab ? htab->root.dynamic_sections_created : FALSE;
37c644f2
AO
3600 sec = h->root.u.def.section;
3601 /* In these cases, we don't need the relocation value.
3602 We check specially because in some obscure cases
435b1e90 3603 sec->output_section will be NULL. */
37c644f2 3604 if (r_type == R_SH_GOTPC
fbca6ad9
AO
3605 || r_type == R_SH_GOTPC_LOW16
3606 || r_type == R_SH_GOTPC_MEDLOW16
3607 || r_type == R_SH_GOTPC_MEDHI16
3608 || r_type == R_SH_GOTPC_HI16
3609 || ((r_type == R_SH_PLT32
3610 || r_type == R_SH_PLT_LOW16
3611 || r_type == R_SH_PLT_MEDLOW16
3612 || r_type == R_SH_PLT_MEDHI16
3613 || r_type == R_SH_PLT_HI16)
37c644f2 3614 && h->plt.offset != (bfd_vma) -1)
fbca6ad9 3615 || ((r_type == R_SH_GOT32
8e45593f
NC
3616 || r_type == R_SH_GOT20
3617 || r_type == R_SH_GOTFUNCDESC
3618 || r_type == R_SH_GOTFUNCDESC20
3619 || r_type == R_SH_GOTOFFFUNCDESC
3620 || r_type == R_SH_GOTOFFFUNCDESC20
3621 || r_type == R_SH_FUNCDESC
fbca6ad9
AO
3622 || r_type == R_SH_GOT_LOW16
3623 || r_type == R_SH_GOT_MEDLOW16
3624 || r_type == R_SH_GOT_MEDHI16
3625 || r_type == R_SH_GOT_HI16)
0e1862bb
L
3626 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
3627 bfd_link_pic (info),
3628 h)
3629 && (! bfd_link_pic (info)
37c644f2 3630 || (! info->symbolic && h->dynindx != -1)
f5385ebf 3631 || !h->def_regular))
37c644f2
AO
3632 /* The cases above are those in which relocation is
3633 overwritten in the switch block below. The cases
3634 below are those in which we must defer relocation
3635 to run-time, because we can't resolve absolute
3636 addresses when creating a shared library. */
0e1862bb 3637 || (bfd_link_pic (info)
37c644f2 3638 && ((! info->symbolic && h->dynindx != -1)
f5385ebf 3639 || !h->def_regular)
37c644f2 3640 && ((r_type == R_SH_DIR32
f5385ebf 3641 && !h->forced_local)
f50ad433
KK
3642 || (r_type == R_SH_REL32
3643 && !SYMBOL_CALLS_LOCAL (info, h)))
37c644f2
AO
3644 && ((input_section->flags & SEC_ALLOC) != 0
3645 /* DWARF will emit R_SH_DIR32 relocations in its
3646 sections against symbols defined externally
3647 in shared libraries. We can't do anything
3648 with them here. */
067653c5 3649 || ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3650 && h->def_dynamic)))
1448fa32
KK
3651 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3652 sections because such sections are not SEC_ALLOC and
3653 thus ld.so will not process them. */
3654 || (sec->output_section == NULL
3655 && ((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3656 && h->def_dynamic))
3376eaf5 3657 || (sec->output_section == NULL
8e45593f
NC
3658 && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
3659 || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
ab96bf03
AM
3660 ;
3661 else if (sec->output_section != NULL)
fad3d2c1 3662 relocation = (h->root.u.def.value
ab96bf03 3663 + sec->output_section->vma
fad3d2c1 3664 + sec->output_offset);
0e1862bb 3665 else if (!bfd_link_relocatable (info)
1d5316ab
AM
3666 && (_bfd_elf_section_offset (output_bfd, info,
3667 input_section,
3668 rel->r_offset)
3669 != (bfd_vma) -1))
37c644f2 3670 {
4eca0228 3671 _bfd_error_handler
695344c0 3672 /* xgettext:c-format */
2dcf00ce
AM
3673 (_("%pB(%pA+%#" PRIx64 "): "
3674 "unresolvable %s relocation against symbol `%s'"),
843fe662
L
3675 input_bfd,
3676 input_section,
2dcf00ce 3677 (uint64_t) rel->r_offset,
843fe662
L
3678 howto->name,
3679 h->root.root.string);
b34976b6 3680 return FALSE;
37c644f2 3681 }
37c644f2
AO
3682 }
3683 else if (h->root.type == bfd_link_hash_undefweak)
6ee6e05a 3684 resolved_to_zero = UNDEFWEAK_NO_DYNAMIC_RELOC (info, h);
59c2e50f 3685 else if (info->unresolved_syms_in_objects == RM_IGNORE
067653c5 3686 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
ab96bf03 3687 ;
0e1862bb 3688 else if (!bfd_link_relocatable (info))
95a51568
FS
3689 info->callbacks->undefined_symbol
3690 (info, h->root.root.string, input_bfd, input_section,
3691 rel->r_offset,
3692 (info->unresolved_syms_in_objects == RM_DIAGNOSE
3693 && !info->warn_unresolved_syms)
3694 || ELF_ST_VISIBILITY (h->other));
3695 }
37c644f2 3696
dbaa2011 3697 if (sec != NULL && discarded_section (sec))
e4067dbb 3698 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 3699 rel, 1, relend, howto, 0, contents);
ab96bf03 3700
0e1862bb 3701 if (bfd_link_relocatable (info))
ab96bf03
AM
3702 continue;
3703
8e45593f
NC
3704 /* Check for inter-segment relocations in FDPIC files. Most
3705 relocations connect the relocation site to the location of
3706 the target symbol, but there are some exceptions below. */
3707 check_segment[0] = isec_segment;
3708 if (sec != NULL)
3709 check_segment[1] = sh_elf_osec_to_segment (output_bfd,
3710 sec->output_section);
3711 else
3712 check_segment[1] = -1;
3713
435b1e90 3714 switch ((int) r_type)
37c644f2
AO
3715 {
3716 final_link_relocate:
3717 /* COFF relocs don't use the addend. The addend is used for
435b1e90 3718 R_SH_DIR32 to be compatible with other compilers. */
37c644f2
AO
3719 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3720 contents, rel->r_offset,
3721 relocation, addend);
3722 break;
3723
3724 case R_SH_IND12W:
cd6ec716
DD
3725 goto final_link_relocate;
3726
37c644f2
AO
3727 case R_SH_DIR8WPN:
3728 case R_SH_DIR8WPZ:
3729 case R_SH_DIR8WPL:
cd6ec716
DD
3730 /* If the reloc is against the start of this section, then
3731 the assembler has already taken care of it and the reloc
3732 is here only to assist in relaxing. If the reloc is not
3733 against the start of this section, then it's against an
3734 external symbol and we must deal with it ourselves. */
3735 if (input_section->output_section->vma + input_section->output_offset
3736 != relocation)
3737 {
3738 int disp = (relocation
3739 - input_section->output_section->vma
3740 - input_section->output_offset
3741 - rel->r_offset);
3742 int mask = 0;
3743 switch (r_type)
3744 {
3745 case R_SH_DIR8WPN:
3746 case R_SH_DIR8WPZ: mask = 1; break;
3747 case R_SH_DIR8WPL: mask = 3; break;
3748 default: mask = 0; break;
3749 }
3750 if (disp & mask)
3751 {
4eca0228 3752 _bfd_error_handler
695344c0 3753 /* xgettext:c-format */
2dcf00ce
AM
3754 (_("%pB: %#" PRIx64 ": fatal: "
3755 "unaligned branch target for relax-support relocation"),
4eca0228 3756 input_section->owner,
2dcf00ce 3757 (uint64_t) rel->r_offset);
cd6ec716 3758 bfd_set_error (bfd_error_bad_value);
b34976b6 3759 return FALSE;
cd6ec716
DD
3760 }
3761 relocation -= 4;
3762 goto final_link_relocate;
3763 }
3764 r = bfd_reloc_ok;
3765 break;
37c644f2
AO
3766
3767 default:
3768 bfd_set_error (bfd_error_bad_value);
b34976b6 3769 return FALSE;
37c644f2 3770
d38eb334
DD
3771 case R_SH_DIR16:
3772 case R_SH_DIR8:
3773 case R_SH_DIR8U:
3774 case R_SH_DIR8S:
3775 case R_SH_DIR4U:
3776 goto final_link_relocate;
3777
3778 case R_SH_DIR8UL:
3779 case R_SH_DIR4UL:
3780 if (relocation & 3)
3781 {
4eca0228 3782 _bfd_error_handler
695344c0 3783 /* xgettext:c-format */
2dcf00ce
AM
3784 (_("%pB: %#" PRIx64 ": fatal: "
3785 "unaligned %s relocation %#" PRIx64),
3786 input_section->owner, (uint64_t) rel->r_offset,
3787 howto->name, (uint64_t) relocation);
d38eb334
DD
3788 bfd_set_error (bfd_error_bad_value);
3789 return FALSE;
3790 }
3791 goto final_link_relocate;
3792
3793 case R_SH_DIR8UW:
3794 case R_SH_DIR8SW:
3795 case R_SH_DIR4UW:
3796 if (relocation & 1)
3797 {
4eca0228 3798 _bfd_error_handler
695344c0 3799 /* xgettext:c-format */
2dcf00ce
AM
3800 (_("%pB: %#" PRIx64 ": fatal: "
3801 "unaligned %s relocation %#" PRIx64 ""),
4eca0228 3802 input_section->owner,
2dcf00ce
AM
3803 (uint64_t) rel->r_offset, howto->name,
3804 (uint64_t) relocation);
d38eb334
DD
3805 bfd_set_error (bfd_error_bad_value);
3806 return FALSE;
3807 }
3808 goto final_link_relocate;
3809
3810 case R_SH_PSHA:
3811 if ((signed int)relocation < -32
3812 || (signed int)relocation > 32)
3813 {
4eca0228 3814 _bfd_error_handler
695344c0 3815 /* xgettext:c-format */
2dcf00ce
AM
3816 (_("%pB: %#" PRIx64 ": fatal: R_SH_PSHA relocation %" PRId64
3817 " not in range -32..32"),
4eca0228 3818 input_section->owner,
2dcf00ce
AM
3819 (uint64_t) rel->r_offset,
3820 (int64_t) relocation);
d38eb334
DD
3821 bfd_set_error (bfd_error_bad_value);
3822 return FALSE;
3823 }
3824 goto final_link_relocate;
3825
3826 case R_SH_PSHL:
3827 if ((signed int)relocation < -16
3828 || (signed int)relocation > 16)
3829 {
4eca0228 3830 _bfd_error_handler
695344c0 3831 /* xgettext:c-format */
2dcf00ce
AM
3832 (_("%pB: %#" PRIx64 ": fatal: R_SH_PSHL relocation %" PRId64
3833 " not in range -32..32"),
4eca0228 3834 input_section->owner,
2dcf00ce
AM
3835 (uint64_t) rel->r_offset,
3836 (int64_t) relocation);
d38eb334
DD
3837 bfd_set_error (bfd_error_bad_value);
3838 return FALSE;
3839 }
3840 goto final_link_relocate;
3841
37c644f2
AO
3842 case R_SH_DIR32:
3843 case R_SH_REL32:
0e1862bb 3844 if (bfd_link_pic (info)
04e534c3 3845 && (h == NULL
6ee6e05a
L
3846 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3847 && !resolved_to_zero)
04e534c3 3848 || h->root.type != bfd_link_hash_undefweak)
cf35638d 3849 && r_symndx != STN_UNDEF
37c644f2 3850 && (input_section->flags & SEC_ALLOC) != 0
3348747a 3851 && !is_vxworks_tls
46e993b9 3852 && (r_type == R_SH_DIR32
3d85aebe 3853 || !SYMBOL_CALLS_LOCAL (info, h)))
37c644f2
AO
3854 {
3855 Elf_Internal_Rela outrel;
947216bf 3856 bfd_byte *loc;
b34976b6 3857 bfd_boolean skip, relocate;
37c644f2
AO
3858
3859 /* When generating a shared object, these relocations
3860 are copied into the output file to be resolved at run
3861 time. */
3862
3863 if (sreloc == NULL)
3864 {
83bac4b0
NC
3865 sreloc = _bfd_elf_get_dynamic_reloc_section
3866 (input_bfd, input_section, /*rela?*/ TRUE);
3867 if (sreloc == NULL)
b34976b6 3868 return FALSE;
37c644f2
AO
3869 }
3870
b34976b6
AM
3871 skip = FALSE;
3872 relocate = FALSE;
37c644f2 3873
c629eae0
JJ
3874 outrel.r_offset =
3875 _bfd_elf_section_offset (output_bfd, info, input_section,
3876 rel->r_offset);
3877 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3878 skip = TRUE;
0bb2d96a 3879 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3880 skip = TRUE, relocate = TRUE;
37c644f2
AO
3881 outrel.r_offset += (input_section->output_section->vma
3882 + input_section->output_offset);
3883
3884 if (skip)
0bb2d96a 3885 memset (&outrel, 0, sizeof outrel);
37c644f2
AO
3886 else if (r_type == R_SH_REL32)
3887 {
3888 BFD_ASSERT (h != NULL && h->dynindx != -1);
37c644f2 3889 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
146be91a 3890 outrel.r_addend
55e6e397
RS
3891 = (howto->partial_inplace
3892 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3893 : addend);
37c644f2 3894 }
e0af1f53 3895 else if (fdpic_p
8e45593f
NC
3896 && (h == NULL
3897 || ((info->symbolic || h->dynindx == -1)
3898 && h->def_regular)))
3899 {
3900 int dynindx;
3901
3902 BFD_ASSERT (sec != NULL);
3903 BFD_ASSERT (sec->output_section != NULL);
3904 dynindx = elf_section_data (sec->output_section)->dynindx;
3905 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
3906 outrel.r_addend = relocation;
3907 outrel.r_addend
3908 += (howto->partial_inplace
3909 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3910 : addend);
3911 outrel.r_addend -= sec->output_section->vma;
3912 }
37c644f2
AO
3913 else
3914 {
3915 /* h->dynindx may be -1 if this symbol was marked to
3916 become local. */
3917 if (h == NULL
3918 || ((info->symbolic || h->dynindx == -1)
f5385ebf 3919 && h->def_regular))
37c644f2 3920 {
55e6e397 3921 relocate = howto->partial_inplace;
37c644f2 3922 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
37c644f2
AO
3923 }
3924 else
3925 {
3926 BFD_ASSERT (h->dynindx != -1);
37c644f2 3927 outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
37c644f2 3928 }
55e6e397
RS
3929 outrel.r_addend = relocation;
3930 outrel.r_addend
3931 += (howto->partial_inplace
3932 ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3933 : addend);
37c644f2
AO
3934 }
3935
947216bf
AM
3936 loc = sreloc->contents;
3937 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3938 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
37c644f2 3939
8e45593f
NC
3940 check_segment[0] = check_segment[1] = -1;
3941
37c644f2
AO
3942 /* If this reloc is against an external symbol, we do
3943 not want to fiddle with the addend. Otherwise, we
3944 need to include the symbol value so that it becomes
3945 an addend for the dynamic reloc. */
3946 if (! relocate)
3947 continue;
3948 }
0e1862bb 3949 else if (fdpic_p && !bfd_link_pic (info)
8e45593f
NC
3950 && r_type == R_SH_DIR32
3951 && (input_section->flags & SEC_ALLOC) != 0)
3952 {
3953 bfd_vma offset;
3954
e0af1f53
MR
3955 BFD_ASSERT (htab);
3956
8e45593f
NC
3957 if (sh_elf_osec_readonly_p (output_bfd,
3958 input_section->output_section))
3959 {
4eca0228 3960 _bfd_error_handler
695344c0 3961 /* xgettext:c-format */
2dcf00ce
AM
3962 (_("%pB(%pA+%#" PRIx64 "): "
3963 "cannot emit fixup to `%s' in read-only section"),
8e45593f
NC
3964 input_bfd,
3965 input_section,
2dcf00ce 3966 (uint64_t) rel->r_offset,
8e45593f
NC
3967 symname);
3968 return FALSE;
3969 }
3970
3971 offset = _bfd_elf_section_offset (output_bfd, info,
3972 input_section, rel->r_offset);
3973 if (offset != (bfd_vma)-1)
3974 sh_elf_add_rofixup (output_bfd, htab->srofixup,
3975 input_section->output_section->vma
3976 + input_section->output_offset
3977 + rel->r_offset);
3978
3979 check_segment[0] = check_segment[1] = -1;
3980 }
a6f713fc
KK
3981 /* We don't want warnings for non-NULL tests on undefined weak
3982 symbols. */
3983 else if (r_type == R_SH_REL32
3984 && h
1b786873 3985 && h->root.type == bfd_link_hash_undefweak)
a6f713fc 3986 check_segment[0] = check_segment[1] = -1;
37c644f2
AO
3987 goto final_link_relocate;
3988
fbca6ad9 3989 case R_SH_GOTPLT32:
fbca6ad9
AO
3990 /* Relocation is to the entry for this symbol in the
3991 procedure linkage table. */
3992
3993 if (h == NULL
f5385ebf 3994 || h->forced_local
0e1862bb 3995 || ! bfd_link_pic (info)
fbca6ad9
AO
3996 || info->symbolic
3997 || h->dynindx == -1
3998 || h->plt.offset == (bfd_vma) -1
3999 || h->got.offset != (bfd_vma) -1)
4000 goto force_got;
4001
4002 /* Relocation is to the entry for this symbol in the global
4003 offset table extension for the procedure linkage table. */
fbca6ad9 4004
e0af1f53 4005 BFD_ASSERT (htab);
067653c5 4006 BFD_ASSERT (sgotplt != NULL);
fbca6ad9 4007 relocation = (sgotplt->output_offset
55e6e397
RS
4008 + (get_plt_index (htab->plt_info, h->plt.offset)
4009 + 3) * 4);
fbca6ad9
AO
4010
4011#ifdef GOT_BIAS
4012 relocation -= GOT_BIAS;
4013#endif
4014
4015 goto final_link_relocate;
4016
4017 force_got:
37c644f2 4018 case R_SH_GOT32:
8e45593f 4019 case R_SH_GOT20:
37c644f2
AO
4020 /* Relocation is to the entry for this symbol in the global
4021 offset table. */
067653c5 4022
e0af1f53 4023 BFD_ASSERT (htab);
067653c5 4024 BFD_ASSERT (sgot != NULL);
8e45593f 4025 check_segment[0] = check_segment[1] = -1;
37c644f2
AO
4026
4027 if (h != NULL)
4028 {
b34976b6 4029 bfd_boolean dyn;
37c644f2
AO
4030
4031 off = h->got.offset;
4032 BFD_ASSERT (off != (bfd_vma) -1);
4033
067653c5 4034 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
4035 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4036 bfd_link_pic (info),
4037 h)
4038 || (bfd_link_pic (info)
3d85aebe 4039 && SYMBOL_REFERENCES_LOCAL (info, h))
6ee6e05a
L
4040 || ((ELF_ST_VISIBILITY (h->other)
4041 || resolved_to_zero)
04e534c3 4042 && h->root.type == bfd_link_hash_undefweak))
37c644f2
AO
4043 {
4044 /* This is actually a static link, or it is a
4045 -Bsymbolic link and the symbol is defined
4046 locally, or the symbol was forced to be local
4047 because of a version file. We must initialize
4048 this entry in the global offset table. Since the
4049 offset must always be a multiple of 4, we use the
4050 least significant bit to record whether we have
4051 initialized it already.
4052
4053 When doing a dynamic link, we create a .rela.got
4054 relocation entry to initialize the value. This
4055 is done in the finish_dynamic_symbol routine. */
4056 if ((off & 1) != 0)
4057 off &= ~1;
4058 else
4059 {
4060 bfd_put_32 (output_bfd, relocation,
4061 sgot->contents + off);
211dc24b 4062 h->got.offset |= 1;
8e45593f
NC
4063
4064 /* If we initialize the GOT entry here with a valid
4065 symbol address, also add a fixup. */
0e1862bb 4066 if (fdpic_p && !bfd_link_pic (info)
8e45593f
NC
4067 && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
4068 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4069 || h->root.type != bfd_link_hash_undefweak))
4070 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4071 sgot->output_section->vma
4072 + sgot->output_offset
4073 + off);
37c644f2
AO
4074 }
4075 }
4076
8e45593f 4077 relocation = sh_elf_got_offset (htab) + off;
37c644f2
AO
4078 }
4079 else
4080 {
37c644f2
AO
4081 BFD_ASSERT (local_got_offsets != NULL
4082 && local_got_offsets[r_symndx] != (bfd_vma) -1);
4083
4084 off = local_got_offsets[r_symndx];
4085
4086 /* The offset must always be a multiple of 4. We use
4087 the least significant bit to record whether we have
4088 already generated the necessary reloc. */
4089 if ((off & 1) != 0)
4090 off &= ~1;
4091 else
4092 {
4093 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4094
0e1862bb 4095 if (bfd_link_pic (info))
37c644f2 4096 {
37c644f2 4097 Elf_Internal_Rela outrel;
947216bf 4098 bfd_byte *loc;
37c644f2 4099
37c644f2
AO
4100 outrel.r_offset = (sgot->output_section->vma
4101 + sgot->output_offset
4102 + off);
e0af1f53 4103 if (fdpic_p)
8e45593f
NC
4104 {
4105 int dynindx
4106 = elf_section_data (sec->output_section)->dynindx;
4107 outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4108 outrel.r_addend = relocation;
4109 outrel.r_addend -= sec->output_section->vma;
4110 }
4111 else
4112 {
4113 outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4114 outrel.r_addend = relocation;
4115 }
947216bf
AM
4116 loc = srelgot->contents;
4117 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4118 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
37c644f2 4119 }
e0af1f53 4120 else if (fdpic_p
8e45593f
NC
4121 && (sh_elf_local_got_type (input_bfd) [r_symndx]
4122 == GOT_NORMAL))
4123 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4124 sgot->output_section->vma
4125 + sgot->output_offset
4126 + off);
37c644f2 4127
211dc24b 4128 local_got_offsets[r_symndx] |= 1;
37c644f2
AO
4129 }
4130
8e45593f 4131 relocation = sh_elf_got_offset (htab) + off;
37c644f2
AO
4132 }
4133
fbca6ad9 4134#ifdef GOT_BIAS
067653c5 4135 relocation -= GOT_BIAS;
fbca6ad9
AO
4136#endif
4137
8e45593f
NC
4138 if (r_type == R_SH_GOT20)
4139 {
4140 r = install_movi20_field (output_bfd, relocation + addend,
4141 input_bfd, input_section, contents,
4142 rel->r_offset);
4143 break;
4144 }
4145 else
4146 goto final_link_relocate;
37c644f2
AO
4147
4148 case R_SH_GOTOFF:
8e45593f 4149 case R_SH_GOTOFF20:
8e45593f
NC
4150 /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4151 we place at the start of the .got.plt section. This is the same
4152 as the start of the output .got section, unless there are function
4153 descriptors in front of it. */
e0af1f53 4154 BFD_ASSERT (htab);
8e45593f
NC
4155 BFD_ASSERT (sgotplt != NULL);
4156 check_segment[0] = got_segment;
4157 relocation -= sgotplt->output_section->vma + sgotplt->output_offset
4158 + htab->root.hgot->root.u.def.value;
37c644f2 4159
fbca6ad9
AO
4160#ifdef GOT_BIAS
4161 relocation -= GOT_BIAS;
4162#endif
4163
4164 addend = rel->r_addend;
4165
8e45593f
NC
4166 if (r_type == R_SH_GOTOFF20)
4167 {
4168 r = install_movi20_field (output_bfd, relocation + addend,
4169 input_bfd, input_section, contents,
4170 rel->r_offset);
4171 break;
4172 }
4173 else
4174 goto final_link_relocate;
37c644f2
AO
4175
4176 case R_SH_GOTPC:
4177 /* Use global offset table as symbol value. */
4178
8e45593f
NC
4179 BFD_ASSERT (sgotplt != NULL);
4180 relocation = sgotplt->output_section->vma + sgotplt->output_offset;
37c644f2 4181
fbca6ad9
AO
4182#ifdef GOT_BIAS
4183 relocation += GOT_BIAS;
4184#endif
4185
4186 addend = rel->r_addend;
4187
37c644f2
AO
4188 goto final_link_relocate;
4189
4190 case R_SH_PLT32:
4191 /* Relocation is to the entry for this symbol in the
4192 procedure linkage table. */
4193
4194 /* Resolve a PLT reloc against a local symbol directly,
4195 without using the procedure linkage table. */
4196 if (h == NULL)
4197 goto final_link_relocate;
4198
8e45593f
NC
4199 /* We don't want to warn on calls to undefined weak symbols,
4200 as calls to them must be protected by non-NULL tests
4201 anyway, and unprotected calls would invoke undefined
4202 behavior. */
4203 if (h->root.type == bfd_link_hash_undefweak)
4204 check_segment[0] = check_segment[1] = -1;
4205
f5385ebf 4206 if (h->forced_local)
37c644f2
AO
4207 goto final_link_relocate;
4208
4209 if (h->plt.offset == (bfd_vma) -1)
4210 {
4211 /* We didn't make a PLT entry for this symbol. This
4212 happens when statically linking PIC code, or when
4213 using -Bsymbolic. */
4214 goto final_link_relocate;
4215 }
4216
067653c5 4217 BFD_ASSERT (splt != NULL);
8e45593f 4218 check_segment[1] = plt_segment;
37c644f2
AO
4219 relocation = (splt->output_section->vma
4220 + splt->output_offset
4221 + h->plt.offset);
4222
fbca6ad9
AO
4223 addend = rel->r_addend;
4224
37c644f2
AO
4225 goto final_link_relocate;
4226
8e45593f
NC
4227 /* Relocation is to the canonical function descriptor for this
4228 symbol, possibly via the GOT. Initialize the GOT
4229 entry and function descriptor if necessary. */
4230 case R_SH_GOTFUNCDESC:
4231 case R_SH_GOTFUNCDESC20:
4232 case R_SH_FUNCDESC:
4233 {
4234 int dynindx = -1;
4235 asection *reloc_section;
4236 bfd_vma reloc_offset;
4237 int reloc_type = R_SH_FUNCDESC;
4238
e0af1f53
MR
4239 BFD_ASSERT (htab);
4240
8e45593f
NC
4241 check_segment[0] = check_segment[1] = -1;
4242
4243 /* FIXME: See what FRV does for global symbols in the
4244 executable, with --export-dynamic. Do they need ld.so
4245 to allocate official descriptors? See what this code
4246 does. */
4247
4248 relocation = 0;
4249 addend = 0;
4250
4251 if (r_type == R_SH_FUNCDESC)
4252 {
4253 reloc_section = input_section;
4254 reloc_offset = rel->r_offset;
4255 }
4256 else
4257 {
e0af1f53 4258 reloc_section = sgot;
8e45593f
NC
4259
4260 if (h != NULL)
4261 reloc_offset = h->got.offset;
4262 else
4263 {
4264 BFD_ASSERT (local_got_offsets != NULL);
4265 reloc_offset = local_got_offsets[r_symndx];
4266 }
4267 BFD_ASSERT (reloc_offset != MINUS_ONE);
4268
4269 if (reloc_offset & 1)
4270 {
4271 reloc_offset &= ~1;
4272 goto funcdesc_done_got;
4273 }
4274 }
4275
4276 if (h && h->root.type == bfd_link_hash_undefweak
4277 && (SYMBOL_CALLS_LOCAL (info, h)
4278 || !htab->root.dynamic_sections_created))
4279 /* Undefined weak symbol which will not be dynamically
4280 resolved later; leave it at zero. */
4281 goto funcdesc_leave_zero;
4282 else if (SYMBOL_CALLS_LOCAL (info, h)
4283 && ! SYMBOL_FUNCDESC_LOCAL (info, h))
4284 {
4285 /* If the symbol needs a non-local function descriptor
4286 but binds locally (i.e., its visibility is
4287 protected), emit a dynamic relocation decayed to
4288 section+offset. This is an optimization; the dynamic
4289 linker would resolve our function descriptor request
4290 to our copy of the function anyway. */
4291 dynindx = elf_section_data (h->root.u.def.section
4292 ->output_section)->dynindx;
4293 relocation += h->root.u.def.section->output_offset
4294 + h->root.u.def.value;
4295 }
4296 else if (! SYMBOL_FUNCDESC_LOCAL (info, h))
4297 {
4298 /* If the symbol is dynamic and there will be dynamic
4299 symbol resolution because we are or are linked with a
4300 shared library, emit a FUNCDESC relocation such that
4301 the dynamic linker will allocate the function
4302 descriptor. */
4303 BFD_ASSERT (h->dynindx != -1);
4304 dynindx = h->dynindx;
4305 }
4306 else
4307 {
4308 bfd_vma offset;
4309
4310 /* Otherwise, we know we have a private function
4311 descriptor, so reference it directly. */
4312 reloc_type = R_SH_DIR32;
4313 dynindx = elf_section_data (htab->sfuncdesc
4314 ->output_section)->dynindx;
4315
4316 if (h)
4317 {
4318 offset = sh_elf_hash_entry (h)->funcdesc.offset;
4319 BFD_ASSERT (offset != MINUS_ONE);
4320 if ((offset & 1) == 0)
4321 {
4322 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4323 offset, NULL, 0))
4324 return FALSE;
4325 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4326 }
4327 }
4328 else
4329 {
4330 union gotref *local_funcdesc;
4331
4332 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4333 offset = local_funcdesc[r_symndx].offset;
4334 BFD_ASSERT (offset != MINUS_ONE);
4335 if ((offset & 1) == 0)
4336 {
4337 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4338 offset, sec,
4339 sym->st_value))
4340 return FALSE;
4341 local_funcdesc[r_symndx].offset |= 1;
4342 }
4343 }
4344
4345 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4346 }
4347
0e1862bb 4348 if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
8e45593f
NC
4349 {
4350 bfd_vma offset;
4351
4352 if (sh_elf_osec_readonly_p (output_bfd,
4353 reloc_section->output_section))
4354 {
4eca0228 4355 _bfd_error_handler
695344c0 4356 /* xgettext:c-format */
2dcf00ce
AM
4357 (_("%pB(%pA+%#" PRIx64 "): "
4358 "cannot emit fixup to `%s' in read-only section"),
8e45593f
NC
4359 input_bfd,
4360 input_section,
2dcf00ce 4361 (uint64_t) rel->r_offset,
8e45593f
NC
4362 symname);
4363 return FALSE;
4364 }
4365
4366 offset = _bfd_elf_section_offset (output_bfd, info,
4367 reloc_section, reloc_offset);
4368
4369 if (offset != (bfd_vma)-1)
4370 sh_elf_add_rofixup (output_bfd, htab->srofixup,
4371 offset
4372 + reloc_section->output_section->vma
4373 + reloc_section->output_offset);
4374 }
4375 else if ((reloc_section->output_section->flags
4376 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
4377 {
4378 bfd_vma offset;
4379
4380 if (sh_elf_osec_readonly_p (output_bfd,
4381 reloc_section->output_section))
4382 {
4383 info->callbacks->warning
4384 (info,
4385 _("cannot emit dynamic relocations in read-only section"),
4386 symname, input_bfd, reloc_section, reloc_offset);
4387 return FALSE;
4388 }
4389
8e45593f
NC
4390 offset = _bfd_elf_section_offset (output_bfd, info,
4391 reloc_section, reloc_offset);
4392
4393 if (offset != (bfd_vma)-1)
4394 sh_elf_add_dyn_reloc (output_bfd, srelgot,
4395 offset
4396 + reloc_section->output_section->vma
4397 + reloc_section->output_offset,
4398 reloc_type, dynindx, relocation);
4399
4400 if (r_type == R_SH_FUNCDESC)
4401 {
4402 r = bfd_reloc_ok;
4403 break;
4404 }
4405 else
4406 {
4407 relocation = 0;
4408 goto funcdesc_leave_zero;
4409 }
4410 }
4411
4412 if (SYMBOL_FUNCDESC_LOCAL (info, h))
4413 relocation += htab->sfuncdesc->output_section->vma;
4414 funcdesc_leave_zero:
4415 if (r_type != R_SH_FUNCDESC)
4416 {
4417 bfd_put_32 (output_bfd, relocation,
4418 reloc_section->contents + reloc_offset);
4419 if (h != NULL)
4420 h->got.offset |= 1;
4421 else
4422 local_got_offsets[r_symndx] |= 1;
4423
4424 funcdesc_done_got:
4425
4426 relocation = sh_elf_got_offset (htab) + reloc_offset;
4427#ifdef GOT_BIAS
4428 relocation -= GOT_BIAS;
4429#endif
4430 }
4431 if (r_type == R_SH_GOTFUNCDESC20)
4432 {
4433 r = install_movi20_field (output_bfd, relocation + addend,
4434 input_bfd, input_section, contents,
4435 rel->r_offset);
4436 break;
4437 }
4438 else
4439 goto final_link_relocate;
4440 }
4441 break;
4442
4443 case R_SH_GOTOFFFUNCDESC:
4444 case R_SH_GOTOFFFUNCDESC20:
4445 /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4446 executable and --export-dynamic. If such symbols get
4447 ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
4448 for them. */
e0af1f53 4449 BFD_ASSERT (htab);
8e45593f
NC
4450
4451 check_segment[0] = check_segment[1] = -1;
4452 relocation = 0;
4453 addend = rel->r_addend;
4454
4455 if (h && (h->root.type == bfd_link_hash_undefweak
4456 || !SYMBOL_FUNCDESC_LOCAL (info, h)))
4457 {
4458 _bfd_error_handler
695344c0 4459 /* xgettext:c-format */
2dcf00ce
AM
4460 (_("%pB(%pA+%#" PRIx64 "): "
4461 "%s relocation against external symbol \"%s\""),
4462 input_bfd, input_section, (uint64_t) rel->r_offset,
4463 howto->name, h->root.root.string);
8e45593f
NC
4464 return FALSE;
4465 }
4466 else
4467 {
4468 bfd_vma offset;
4469
4470 /* Otherwise, we know we have a private function
4471 descriptor, so reference it directly. */
4472 if (h)
4473 {
4474 offset = sh_elf_hash_entry (h)->funcdesc.offset;
4475 BFD_ASSERT (offset != MINUS_ONE);
4476 if ((offset & 1) == 0)
4477 {
4478 if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4479 offset, NULL, 0))
4480 return FALSE;
4481 sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4482 }
4483 }
4484 else
4485 {
4486 union gotref *local_funcdesc;
4487
4488 local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4489 offset = local_funcdesc[r_symndx].offset;
4490 BFD_ASSERT (offset != MINUS_ONE);
4491 if ((offset & 1) == 0)
4492 {
4493 if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4494 offset, sec,
4495 sym->st_value))
4496 return FALSE;
4497 local_funcdesc[r_symndx].offset |= 1;
4498 }
4499 }
4500
4501 relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4502 }
4503
e0af1f53
MR
4504 relocation -= (htab->root.hgot->root.u.def.value
4505 + sgotplt->output_offset);
8e45593f
NC
4506#ifdef GOT_BIAS
4507 relocation -= GOT_BIAS;
4508#endif
4509
4510 if (r_type == R_SH_GOTOFFFUNCDESC20)
4511 {
4512 r = install_movi20_field (output_bfd, relocation + addend,
4513 input_bfd, input_section, contents,
4514 rel->r_offset);
4515 break;
4516 }
4517 else
4518 goto final_link_relocate;
4519
37c644f2
AO
4520 case R_SH_LOOP_START:
4521 {
4522 static bfd_vma start, end;
4523
4524 start = (relocation + rel->r_addend
4525 - (sec->output_section->vma + sec->output_offset));
4526 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
4527 rel->r_offset, sec, start, end);
4528 break;
4529
4530 case R_SH_LOOP_END:
4531 end = (relocation + rel->r_addend
4532 - (sec->output_section->vma + sec->output_offset));
4533 r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
4534 rel->r_offset, sec, start, end);
4535 break;
4536 }
3376eaf5
KK
4537
4538 case R_SH_TLS_GD_32:
4539 case R_SH_TLS_IE_32:
e0af1f53 4540 BFD_ASSERT (htab);
8e45593f 4541 check_segment[0] = check_segment[1] = -1;
3376eaf5 4542 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
8e45593f 4543 got_type = GOT_UNKNOWN;
3376eaf5 4544 if (h == NULL && local_got_offsets)
8e45593f 4545 got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
3376eaf5
KK
4546 else if (h != NULL)
4547 {
8e45593f 4548 got_type = sh_elf_hash_entry (h)->got_type;
0e1862bb 4549 if (! bfd_link_pic (info)
3376eaf5 4550 && (h->dynindx == -1
f5385ebf 4551 || h->def_regular))
267fb3c1 4552 r_type = R_SH_TLS_LE_32;
3376eaf5
KK
4553 }
4554
8e45593f 4555 if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
3376eaf5
KK
4556 r_type = R_SH_TLS_IE_32;
4557
4558 if (r_type == R_SH_TLS_LE_32)
4559 {
4560 bfd_vma offset;
4561 unsigned short insn;
3376eaf5
KK
4562
4563 if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
4564 {
4565 /* GD->LE transition:
4566 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
b34976b6 4567 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
3376eaf5
KK
4568 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4569 We change it into:
4570 mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
4571 nop; nop; ...
4572 1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:. */
4573
4574 offset = rel->r_offset;
a05b9f5e
NC
4575 if (offset < 16)
4576 {
4577 _bfd_error_handler
4578 /* xgettext:c-format */
4579 (_("%pB(%pA): offset in relocation for GD->LE translation is too small: %#" PRIx64),
4580 input_bfd, input_section, (uint64_t) offset);
4581 return FALSE;
4582 }
4583
3376eaf5
KK
4584 /* Size of GD instructions is 16 or 18. */
4585 offset -= 16;
4586 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4587 if ((insn & 0xff00) == 0xc700)
4588 {
4589 BFD_ASSERT (offset >= 2);
4590 offset -= 2;
4591 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4592 }
4593
a05b9f5e
NC
4594 if ((insn & 0xff00) != 0xd400)
4595 _bfd_error_handler
4596 /* xgettext:c-format */ /* The backslash is to prevent bogus trigraph detection. */
4597 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd4?\?)"),
4598 input_bfd, input_section, (uint64_t) offset, (int) insn);
4599
3376eaf5 4600 insn = bfd_get_16 (input_bfd, contents + offset + 2);
a05b9f5e
NC
4601
4602 if ((insn & 0xff00) != 0xc700)
4603 _bfd_error_handler
4604 /* xgettext:c-format */
4605 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xc7?\?)"),
4606 input_bfd, input_section, (uint64_t) offset, (int) insn);
4607
3376eaf5 4608 insn = bfd_get_16 (input_bfd, contents + offset + 4);
a05b9f5e
NC
4609 if ((insn & 0xff00) != 0xd100)
4610 _bfd_error_handler
4611 /* xgettext:c-format */
4612 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd1?\?)"),
4613 input_bfd, input_section, (uint64_t) offset, (int) insn);
4614
3376eaf5 4615 insn = bfd_get_16 (input_bfd, contents + offset + 6);
a05b9f5e
NC
4616 if (insn != 0x310c)
4617 _bfd_error_handler
4618 /* xgettext:c-format */
4619 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x310c)"),
4620 input_bfd, input_section, (uint64_t) offset, (int) insn);
4621
3376eaf5 4622 insn = bfd_get_16 (input_bfd, contents + offset + 8);
a05b9f5e
NC
4623 if (insn != 0x410b)
4624 _bfd_error_handler
4625 /* xgettext:c-format */
4626 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x410b)"),
4627 input_bfd, input_section, (uint64_t) offset, (int) insn);
4628
3376eaf5 4629 insn = bfd_get_16 (input_bfd, contents + offset + 10);
a05b9f5e
NC
4630 if (insn != 0x34cc)
4631 _bfd_error_handler
4632 /* xgettext:c-format */
4633 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x34cc)"),
4634 input_bfd, input_section, (uint64_t) offset, (int) insn);
3376eaf5
KK
4635
4636 bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
4637 bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
4638 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4639 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4640 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4641 }
4642 else
4643 {
91d6fa6a 4644 int target;
3376eaf5
KK
4645
4646 /* IE->LE transition:
a05b9f5e
NC
4647 mov.l 1f,r0;
4648 stc gbr,rN;
4649 mov.l @(r0,r12),rM;
4650 bra 2f;
4651 add ...;
4652 .align 2;
4653 1: x@GOTTPOFF;
4654 2:
3376eaf5 4655 We change it into:
a05b9f5e
NC
4656 mov.l .Ln,rM;
4657 stc gbr,rN;
4658 nop;
4659 ...;
4660 1: x@TPOFF;
4661 2:. */
3376eaf5
KK
4662
4663 offset = rel->r_offset;
a05b9f5e
NC
4664 if (offset < 16)
4665 {
4666 _bfd_error_handler
4667 /* xgettext:c-format */
4668 (_("%pB(%pA): offset in relocation for IE->LE translation is too small: %#" PRIx64),
4669 input_bfd, input_section, (uint64_t) offset);
4670 return FALSE;
4671 }
4672
3376eaf5
KK
4673 /* Size of IE instructions is 10 or 12. */
4674 offset -= 10;
4675 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4676 if ((insn & 0xf0ff) == 0x0012)
4677 {
4678 BFD_ASSERT (offset >= 2);
4679 offset -= 2;
4680 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4681 }
4682
a05b9f5e
NC
4683 if ((insn & 0xff00) != 0xd000)
4684 _bfd_error_handler
4685 /* xgettext:c-format */
4686 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd0??: mov.l)"),
4687 input_bfd, input_section, (uint64_t) offset, (int) insn);
4688
91d6fa6a 4689 target = insn & 0x00ff;
a05b9f5e 4690
3376eaf5 4691 insn = bfd_get_16 (input_bfd, contents + offset + 2);
a05b9f5e
NC
4692 if ((insn & 0xf0ff) != 0x0012)
4693 _bfd_error_handler
4694 /* xgettext:c-format */
4695 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x0?12: stc)"),
4696 input_bfd, input_section, (uint64_t) (offset + 2), (int) insn);
4697
3376eaf5 4698 insn = bfd_get_16 (input_bfd, contents + offset + 4);
a05b9f5e
NC
4699 if ((insn & 0xf0ff) != 0x00ce)
4700 _bfd_error_handler
4701 /* xgettext:c-format */
4702 (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x0?ce: mov.l)"),
4703 input_bfd, input_section, (uint64_t) (offset + 4), (int) insn);
4704
91d6fa6a 4705 insn = 0xd000 | (insn & 0x0f00) | target;
3376eaf5
KK
4706 bfd_put_16 (output_bfd, insn, contents + offset + 0);
4707 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4708 }
4709
267fb3c1
KK
4710 bfd_put_32 (output_bfd, tpoff (info, relocation),
4711 contents + rel->r_offset);
3376eaf5
KK
4712 continue;
4713 }
4714
8e45593f 4715 if (sgot == NULL || sgotplt == NULL)
3376eaf5
KK
4716 abort ();
4717
4718 if (h != NULL)
4719 off = h->got.offset;
4720 else
4721 {
4722 if (local_got_offsets == NULL)
4723 abort ();
4724
4725 off = local_got_offsets[r_symndx];
4726 }
4727
267fb3c1
KK
4728 /* Relocate R_SH_TLS_IE_32 directly when statically linking. */
4729 if (r_type == R_SH_TLS_IE_32
4730 && ! htab->root.dynamic_sections_created)
4731 {
4732 off &= ~1;
4733 bfd_put_32 (output_bfd, tpoff (info, relocation),
4734 sgot->contents + off);
8e45593f 4735 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
267fb3c1
KK
4736 contents + rel->r_offset);
4737 continue;
4738 }
4739
3376eaf5
KK
4740 if ((off & 1) != 0)
4741 off &= ~1;
26e41594 4742 else
3376eaf5
KK
4743 {
4744 Elf_Internal_Rela outrel;
947216bf 4745 bfd_byte *loc;
3376eaf5
KK
4746 int dr_type, indx;
4747
3376eaf5
KK
4748 outrel.r_offset = (sgot->output_section->vma
4749 + sgot->output_offset + off);
4750
267fb3c1 4751 if (h == NULL || h->dynindx == -1)
c95b8a7a
KK
4752 indx = 0;
4753 else
4754 indx = h->dynindx;
267fb3c1 4755
3376eaf5
KK
4756 dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
4757 R_SH_TLS_TPOFF32);
4758 if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
4759 outrel.r_addend = relocation - dtpoff_base (info);
4760 else
4761 outrel.r_addend = 0;
4762 outrel.r_info = ELF32_R_INFO (indx, dr_type);
947216bf
AM
4763 loc = srelgot->contents;
4764 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3376eaf5
KK
4765 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4766
4767 if (r_type == R_SH_TLS_GD_32)
4768 {
4769 if (indx == 0)
4770 {
4771 bfd_put_32 (output_bfd,
4772 relocation - dtpoff_base (info),
4773 sgot->contents + off + 4);
4774 }
4775 else
4776 {
4777 outrel.r_info = ELF32_R_INFO (indx,
4778 R_SH_TLS_DTPOFF32);
4779 outrel.r_offset += 4;
4780 outrel.r_addend = 0;
4781 srelgot->reloc_count++;
947216bf
AM
4782 loc += sizeof (Elf32_External_Rela);
4783 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3376eaf5
KK
4784 }
4785 }
4786
4787 if (h != NULL)
4788 h->got.offset |= 1;
4789 else
4790 local_got_offsets[r_symndx] |= 1;
4791 }
4792
4793 if (off >= (bfd_vma) -2)
4794 abort ();
4795
4796 if (r_type == (int) ELF32_R_TYPE (rel->r_info))
8e45593f 4797 relocation = sh_elf_got_offset (htab) + off;
3376eaf5
KK
4798 else
4799 {
4800 bfd_vma offset;
4801 unsigned short insn;
4802
4803 /* GD->IE transition:
4804 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4805 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4806 1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4807 We change it into:
4808 mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4809 nop; nop; bra 3f; nop; .align 2;
4810 1: .long x@TPOFF; 2:...; 3:. */
4811
4812 offset = rel->r_offset;
a05b9f5e
NC
4813 if (offset < 16)
4814 {
4815 _bfd_error_handler
4816 /* xgettext:c-format */
4817 (_("%pB(%pA): offset in relocation for GD->IE translation is too small: %#" PRIx64),
4818 input_bfd, input_section, (uint64_t) offset);
4819 return FALSE;
4820 }
4821
3376eaf5
KK
4822 /* Size of GD instructions is 16 or 18. */
4823 offset -= 16;
4824 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4825 if ((insn & 0xff00) == 0xc700)
4826 {
4827 BFD_ASSERT (offset >= 2);
4828 offset -= 2;
4829 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4830 }
4831
4832 BFD_ASSERT ((insn & 0xff00) == 0xd400);
4833
4834 /* Replace mov.l 1f,R4 with mov.l 1f,r0. */
4835 bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
4836
4837 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4838 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4839 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4840 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4841 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4842 BFD_ASSERT (insn == 0x310c);
4843 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4844 BFD_ASSERT (insn == 0x410b);
4845 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4846 BFD_ASSERT (insn == 0x34cc);
4847
4848 bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
4849 bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
4850 bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
4851 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4852 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4853
8e45593f 4854 bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
3376eaf5
KK
4855 contents + rel->r_offset);
4856
4857 continue;
4858 }
4859
4860 addend = rel->r_addend;
4861
4862 goto final_link_relocate;
4863
4864 case R_SH_TLS_LD_32:
e0af1f53 4865 BFD_ASSERT (htab);
8e45593f 4866 check_segment[0] = check_segment[1] = -1;
0e1862bb 4867 if (! bfd_link_pic (info))
3376eaf5
KK
4868 {
4869 bfd_vma offset;
4870 unsigned short insn;
4871
4872 /* LD->LE transition:
4873 mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4874 jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4875 1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4876 We change it into:
4877 stc gbr,r0; nop; nop; nop;
4878 nop; nop; bra 3f; ...; 3:. */
4879
4880 offset = rel->r_offset;
a05b9f5e
NC
4881 if (offset < 16)
4882 {
4883 _bfd_error_handler
4884 /* xgettext:c-format */
4885 (_("%pB(%pA): offset in relocation for LD->LE translation is too small: %#" PRIx64),
4886 input_bfd, input_section, (uint64_t) offset);
4887 return FALSE;
4888 }
4889
3376eaf5
KK
4890 /* Size of LD instructions is 16 or 18. */
4891 offset -= 16;
4892 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4893 if ((insn & 0xff00) == 0xc700)
4894 {
4895 BFD_ASSERT (offset >= 2);
4896 offset -= 2;
4897 insn = bfd_get_16 (input_bfd, contents + offset + 0);
4898 }
4899
4900 BFD_ASSERT ((insn & 0xff00) == 0xd400);
4901 insn = bfd_get_16 (input_bfd, contents + offset + 2);
4902 BFD_ASSERT ((insn & 0xff00) == 0xc700);
4903 insn = bfd_get_16 (input_bfd, contents + offset + 4);
4904 BFD_ASSERT ((insn & 0xff00) == 0xd100);
4905 insn = bfd_get_16 (input_bfd, contents + offset + 6);
4906 BFD_ASSERT (insn == 0x310c);
4907 insn = bfd_get_16 (input_bfd, contents + offset + 8);
4908 BFD_ASSERT (insn == 0x410b);
4909 insn = bfd_get_16 (input_bfd, contents + offset + 10);
4910 BFD_ASSERT (insn == 0x34cc);
4911
4912 bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
4913 bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
4914 bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4915 bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4916 bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4917 bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4918
4919 continue;
4920 }
4921
8e45593f 4922 if (sgot == NULL || sgotplt == NULL)
3376eaf5
KK
4923 abort ();
4924
4925 off = htab->tls_ldm_got.offset;
4926 if (off & 1)
4927 off &= ~1;
4928 else
4929 {
4930 Elf_Internal_Rela outrel;
947216bf 4931 bfd_byte *loc;
3376eaf5 4932
3376eaf5
KK
4933 outrel.r_offset = (sgot->output_section->vma
4934 + sgot->output_offset + off);
4935 outrel.r_addend = 0;
4936 outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
947216bf
AM
4937 loc = srelgot->contents;
4938 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3376eaf5
KK
4939 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4940 htab->tls_ldm_got.offset |= 1;
4941 }
4942
8e45593f 4943 relocation = sh_elf_got_offset (htab) + off;
3376eaf5
KK
4944 addend = rel->r_addend;
4945
4946 goto final_link_relocate;
4947
4948 case R_SH_TLS_LDO_32:
8e45593f 4949 check_segment[0] = check_segment[1] = -1;
0e1862bb 4950 if (! bfd_link_pic (info))
267fb3c1 4951 relocation = tpoff (info, relocation);
3376eaf5
KK
4952 else
4953 relocation -= dtpoff_base (info);
4954
4955 addend = rel->r_addend;
4956 goto final_link_relocate;
4957
4958 case R_SH_TLS_LE_32:
4959 {
4960 int indx;
4961 Elf_Internal_Rela outrel;
947216bf 4962 bfd_byte *loc;
3376eaf5 4963
8e45593f
NC
4964 check_segment[0] = check_segment[1] = -1;
4965
3cbc1e5e 4966 if (!bfd_link_dll (info))
267fb3c1
KK
4967 {
4968 relocation = tpoff (info, relocation);
4969 addend = rel->r_addend;
4970 goto final_link_relocate;
4971 }
4972
3376eaf5
KK
4973 if (sreloc == NULL)
4974 {
83bac4b0
NC
4975 sreloc = _bfd_elf_get_dynamic_reloc_section
4976 (input_bfd, input_section, /*rela?*/ TRUE);
4977 if (sreloc == NULL)
b34976b6 4978 return FALSE;
3376eaf5
KK
4979 }
4980
267fb3c1
KK
4981 if (h == NULL || h->dynindx == -1)
4982 indx = 0;
4983 else
4984 indx = h->dynindx;
4985
3376eaf5
KK
4986 outrel.r_offset = (input_section->output_section->vma
4987 + input_section->output_offset
4988 + rel->r_offset);
4989 outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
4990 if (indx == 0)
4991 outrel.r_addend = relocation - dtpoff_base (info);
4992 else
4993 outrel.r_addend = 0;
3376eaf5 4994
947216bf
AM
4995 loc = sreloc->contents;
4996 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4997 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3376eaf5
KK
4998 continue;
4999 }
37c644f2
AO
5000 }
5001
146be91a 5002 relocation_done:
e0af1f53 5003 if (fdpic_p && check_segment[0] != (unsigned) -1
8e45593f
NC
5004 && check_segment[0] != check_segment[1])
5005 {
5006 /* We don't want duplicate errors for undefined symbols. */
5007 if (!h || h->root.type != bfd_link_hash_undefined)
5008 {
0e1862bb 5009 if (bfd_link_pic (info))
8e45593f
NC
5010 {
5011 info->callbacks->einfo
695344c0 5012 /* xgettext:c-format */
8e45593f
NC
5013 (_("%X%C: relocation to \"%s\" references a different segment\n"),
5014 input_bfd, input_section, rel->r_offset, symname);
5015 return FALSE;
5016 }
5017 else
5018 info->callbacks->einfo
695344c0 5019 /* xgettext:c-format */
8e45593f
NC
5020 (_("%C: warning: relocation to \"%s\" references a different segment\n"),
5021 input_bfd, input_section, rel->r_offset, symname);
5022 }
5023
fbedb42d 5024 elf_elfheader (output_bfd)->e_flags |= EF_SH_PIC;
8e45593f
NC
5025 }
5026
37c644f2
AO
5027 if (r != bfd_reloc_ok)
5028 {
5029 switch (r)
5030 {
5031 default:
5032 case bfd_reloc_outofrange:
5033 abort ();
5034 case bfd_reloc_overflow:
5035 {
5036 const char *name;
5037
5038 if (h != NULL)
dfeffb9f 5039 name = NULL;
37c644f2
AO
5040 else
5041 {
252b5132
RH
5042 name = (bfd_elf_string_from_elf_section
5043 (input_bfd, symtab_hdr->sh_link, sym->st_name));
5044 if (name == NULL)
b34976b6 5045 return FALSE;
252b5132 5046 if (*name == '\0')
fd361982 5047 name = bfd_section_name (sec);
252b5132 5048 }
1a72702b
AM
5049 (*info->callbacks->reloc_overflow)
5050 (info, (h ? &h->root : NULL), name, howto->name,
5051 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
252b5132
RH
5052 }
5053 break;
5054 }
5055 }
5056 }
5057
b34976b6 5058 return TRUE;
252b5132
RH
5059}
5060
5061/* This is a version of bfd_generic_get_relocated_section_contents
5062 which uses sh_elf_relocate_section. */
5063
5064static bfd_byte *
09fd220b
KK
5065sh_elf_get_relocated_section_contents (bfd *output_bfd,
5066 struct bfd_link_info *link_info,
5067 struct bfd_link_order *link_order,
5068 bfd_byte *data,
5069 bfd_boolean relocatable,
5070 asymbol **symbols)
252b5132
RH
5071{
5072 Elf_Internal_Shdr *symtab_hdr;
5073 asection *input_section = link_order->u.indirect.section;
5074 bfd *input_bfd = input_section->owner;
5075 asection **sections = NULL;
5076 Elf_Internal_Rela *internal_relocs = NULL;
6cdc0ccc 5077 Elf_Internal_Sym *isymbuf = NULL;
252b5132
RH
5078
5079 /* We only need to handle the case of relaxing, or of having a
5080 particular set of section contents, specially. */
1049f94e 5081 if (relocatable
252b5132
RH
5082 || elf_section_data (input_section)->this_hdr.contents == NULL)
5083 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5084 link_order, data,
1049f94e 5085 relocatable,
252b5132
RH
5086 symbols);
5087
0ffa91dd 5088 symtab_hdr = &elf_symtab_hdr (input_bfd);
252b5132
RH
5089
5090 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
eea6121a 5091 (size_t) input_section->size);
252b5132
RH
5092
5093 if ((input_section->flags & SEC_RELOC) != 0
5094 && input_section->reloc_count > 0)
5095 {
252b5132 5096 asection **secpp;
6cdc0ccc 5097 Elf_Internal_Sym *isym, *isymend;
9ad5cbcf 5098 bfd_size_type amt;
252b5132 5099
45d6a902 5100 internal_relocs = (_bfd_elf_link_read_relocs
09fd220b 5101 (input_bfd, input_section, NULL,
b34976b6 5102 (Elf_Internal_Rela *) NULL, FALSE));
252b5132
RH
5103 if (internal_relocs == NULL)
5104 goto error_return;
5105
6cdc0ccc
AM
5106 if (symtab_hdr->sh_info != 0)
5107 {
5108 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5109 if (isymbuf == NULL)
5110 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5111 symtab_hdr->sh_info, 0,
5112 NULL, NULL, NULL);
5113 if (isymbuf == NULL)
5114 goto error_return;
5115 }
252b5132 5116
9ad5cbcf
AM
5117 amt = symtab_hdr->sh_info;
5118 amt *= sizeof (asection *);
5119 sections = (asection **) bfd_malloc (amt);
5120 if (sections == NULL && amt != 0)
252b5132
RH
5121 goto error_return;
5122
6cdc0ccc
AM
5123 isymend = isymbuf + symtab_hdr->sh_info;
5124 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
252b5132
RH
5125 {
5126 asection *isec;
5127
6cdc0ccc 5128 if (isym->st_shndx == SHN_UNDEF)
252b5132 5129 isec = bfd_und_section_ptr;
6cdc0ccc 5130 else if (isym->st_shndx == SHN_ABS)
252b5132 5131 isec = bfd_abs_section_ptr;
6cdc0ccc 5132 else if (isym->st_shndx == SHN_COMMON)
252b5132
RH
5133 isec = bfd_com_section_ptr;
5134 else
6cdc0ccc 5135 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
252b5132
RH
5136
5137 *secpp = isec;
5138 }
5139
5140 if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5141 input_section, data, internal_relocs,
6cdc0ccc 5142 isymbuf, sections))
252b5132
RH
5143 goto error_return;
5144
c9594989
AM
5145 free (sections);
5146 if (symtab_hdr->contents != (unsigned char *) isymbuf)
6cdc0ccc
AM
5147 free (isymbuf);
5148 if (elf_section_data (input_section)->relocs != internal_relocs)
252b5132 5149 free (internal_relocs);
252b5132
RH
5150 }
5151
5152 return data;
5153
5154 error_return:
c9594989
AM
5155 free (sections);
5156 if (symtab_hdr->contents != (unsigned char *) isymbuf)
6cdc0ccc 5157 free (isymbuf);
c9594989 5158 if (elf_section_data (input_section)->relocs != internal_relocs)
6cdc0ccc 5159 free (internal_relocs);
252b5132
RH
5160 return NULL;
5161}
917583ad 5162
3376eaf5
KK
5163/* Return the base VMA address which should be subtracted from real addresses
5164 when resolving @dtpoff relocation.
5165 This is PT_TLS segment p_vaddr. */
5166
5167static bfd_vma
09fd220b 5168dtpoff_base (struct bfd_link_info *info)
3376eaf5 5169{
e1918d23
AM
5170 /* If tls_sec is NULL, we should have signalled an error already. */
5171 if (elf_hash_table (info)->tls_sec == NULL)
267fb3c1 5172 return 0;
e1918d23 5173 return elf_hash_table (info)->tls_sec->vma;
3376eaf5
KK
5174}
5175
267fb3c1
KK
5176/* Return the relocation value for R_SH_TLS_TPOFF32.. */
5177
5178static bfd_vma
09fd220b 5179tpoff (struct bfd_link_info *info, bfd_vma address)
267fb3c1 5180{
e1918d23
AM
5181 /* If tls_sec is NULL, we should have signalled an error already. */
5182 if (elf_hash_table (info)->tls_sec == NULL)
267fb3c1
KK
5183 return 0;
5184 /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5185 structure which has 2 pointer fields. */
044ad7e3
KK
5186 return (address - elf_hash_table (info)->tls_sec->vma
5187 + align_power ((bfd_vma) 8,
5188 elf_hash_table (info)->tls_sec->alignment_power));
267fb3c1
KK
5189}
5190
252b5132 5191static asection *
09fd220b 5192sh_elf_gc_mark_hook (asection *sec,
07adf181
AM
5193 struct bfd_link_info *info,
5194 Elf_Internal_Rela *rel,
5195 struct elf_link_hash_entry *h,
09fd220b 5196 Elf_Internal_Sym *sym)
252b5132
RH
5197{
5198 if (h != NULL)
07adf181
AM
5199 switch (ELF32_R_TYPE (rel->r_info))
5200 {
5201 case R_SH_GNU_VTINHERIT:
5202 case R_SH_GNU_VTENTRY:
5203 return NULL;
5204 }
9ad5cbcf 5205
07adf181 5206 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
252b5132
RH
5207}
5208
067653c5
AM
5209/* Copy the extra info we tack onto an elf_link_hash_entry. */
5210
5211static void
fcfa13d2 5212sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
09fd220b
KK
5213 struct elf_link_hash_entry *dir,
5214 struct elf_link_hash_entry *ind)
067653c5
AM
5215{
5216 struct elf_sh_link_hash_entry *edir, *eind;
5217
5218 edir = (struct elf_sh_link_hash_entry *) dir;
5219 eind = (struct elf_sh_link_hash_entry *) ind;
5220
4989d792
SC
5221 edir->gotplt_refcount = eind->gotplt_refcount;
5222 eind->gotplt_refcount = 0;
8e45593f 5223 edir->funcdesc.refcount += eind->funcdesc.refcount;
68ffbac6 5224 eind->funcdesc.refcount = 0;
8e45593f 5225 edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
68ffbac6 5226 eind->abs_funcdesc_refcount = 0;
067653c5 5227
3376eaf5
KK
5228 if (ind->root.type == bfd_link_hash_indirect
5229 && dir->got.refcount <= 0)
5230 {
8e45593f
NC
5231 edir->got_type = eind->got_type;
5232 eind->got_type = GOT_UNKNOWN;
3376eaf5
KK
5233 }
5234
04e534c3 5235 if (ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
5236 && dir->dynamic_adjusted)
5237 {
5238 /* If called to transfer flags for a weakdef during processing
5239 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5240 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
e81830c5
AM
5241 if (dir->versioned != versioned_hidden)
5242 dir->ref_dynamic |= ind->ref_dynamic;
f5385ebf
AM
5243 dir->ref_regular |= ind->ref_regular;
5244 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
5245 dir->needs_plt |= ind->needs_plt;
5246 }
04e534c3 5247 else
fcfa13d2 5248 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
067653c5
AM
5249}
5250
3376eaf5 5251static int
09fd220b
KK
5252sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
5253 int is_local)
3376eaf5 5254{
0e1862bb 5255 if (bfd_link_pic (info))
3376eaf5
KK
5256 return r_type;
5257
5258 switch (r_type)
5259 {
5260 case R_SH_TLS_GD_32:
5261 case R_SH_TLS_IE_32:
5262 if (is_local)
5263 return R_SH_TLS_LE_32;
5264 return R_SH_TLS_IE_32;
5265 case R_SH_TLS_LD_32:
5266 return R_SH_TLS_LE_32;
5267 }
5268
5269 return r_type;
5270}
5271
252b5132
RH
5272/* Look through the relocs for a section during the first phase.
5273 Since we don't do .gots or .plts, we just need to consider the
5274 virtual table relocs for gc. */
435b1e90 5275
b34976b6 5276static bfd_boolean
09fd220b
KK
5277sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
5278 const Elf_Internal_Rela *relocs)
252b5132
RH
5279{
5280 Elf_Internal_Shdr *symtab_hdr;
5582a088 5281 struct elf_link_hash_entry **sym_hashes;
067653c5 5282 struct elf_sh_link_hash_table *htab;
252b5132
RH
5283 const Elf_Internal_Rela *rel;
5284 const Elf_Internal_Rela *rel_end;
37c644f2 5285 asection *sreloc;
3376eaf5 5286 unsigned int r_type;
85e02784 5287 enum got_type got_type, old_got_type;
435b1e90 5288
37c644f2
AO
5289 sreloc = NULL;
5290
0e1862bb 5291 if (bfd_link_relocatable (info))
b34976b6 5292 return TRUE;
435b1e90 5293
0ffa91dd
NC
5294 BFD_ASSERT (is_sh_elf (abfd));
5295
5296 symtab_hdr = &elf_symtab_hdr (abfd);
252b5132 5297 sym_hashes = elf_sym_hashes (abfd);
435b1e90 5298
067653c5 5299 htab = sh_elf_hash_table (info);
4dfe6ac6
NC
5300 if (htab == NULL)
5301 return FALSE;
5302
252b5132
RH
5303 rel_end = relocs + sec->reloc_count;
5304 for (rel = relocs; rel < rel_end; rel++)
5305 {
5306 struct elf_link_hash_entry *h;
5307 unsigned long r_symndx;
435b1e90 5308
252b5132 5309 r_symndx = ELF32_R_SYM (rel->r_info);
3376eaf5
KK
5310 r_type = ELF32_R_TYPE (rel->r_info);
5311
252b5132 5312 if (r_symndx < symtab_hdr->sh_info)
435b1e90 5313 h = NULL;
252b5132 5314 else
396a6083
SC
5315 {
5316 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
396a6083
SC
5317 while (h->root.type == bfd_link_hash_indirect
5318 || h->root.type == bfd_link_hash_warning)
211dc24b 5319 h = (struct elf_link_hash_entry *) h->root.u.i.link;
396a6083 5320 }
435b1e90 5321
3376eaf5 5322 r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
0e1862bb 5323 if (! bfd_link_pic (info)
3376eaf5
KK
5324 && r_type == R_SH_TLS_IE_32
5325 && h != NULL
5326 && h->root.type != bfd_link_hash_undefined
5327 && h->root.type != bfd_link_hash_undefweak
5328 && (h->dynindx == -1
f5385ebf 5329 || h->def_regular))
3376eaf5
KK
5330 r_type = R_SH_TLS_LE_32;
5331
8e45593f
NC
5332 if (htab->fdpic_p)
5333 switch (r_type)
5334 {
5335 case R_SH_GOTOFFFUNCDESC:
5336 case R_SH_GOTOFFFUNCDESC20:
5337 case R_SH_FUNCDESC:
5338 case R_SH_GOTFUNCDESC:
5339 case R_SH_GOTFUNCDESC20:
5340 if (h != NULL)
5341 {
5342 if (h->dynindx == -1)
5343 switch (ELF_ST_VISIBILITY (h->other))
5344 {
5345 case STV_INTERNAL:
5346 case STV_HIDDEN:
5347 break;
5348 default:
5349 bfd_elf_link_record_dynamic_symbol (info, h);
5350 break;
5351 }
5352 }
5353 break;
5354 }
5355
37c644f2 5356 /* Some relocs require a global offset table. */
ce558b89 5357 if (htab->root.sgot == NULL)
37c644f2 5358 {
3376eaf5 5359 switch (r_type)
37c644f2 5360 {
8e45593f
NC
5361 case R_SH_DIR32:
5362 /* This may require an rofixup. */
5363 if (!htab->fdpic_p)
5364 break;
1a0670f3 5365 /* Fall through. */
fbca6ad9 5366 case R_SH_GOTPLT32:
37c644f2 5367 case R_SH_GOT32:
8e45593f 5368 case R_SH_GOT20:
37c644f2 5369 case R_SH_GOTOFF:
8e45593f
NC
5370 case R_SH_GOTOFF20:
5371 case R_SH_FUNCDESC:
5372 case R_SH_GOTFUNCDESC:
5373 case R_SH_GOTFUNCDESC20:
5374 case R_SH_GOTOFFFUNCDESC:
5375 case R_SH_GOTOFFFUNCDESC20:
37c644f2 5376 case R_SH_GOTPC:
3376eaf5
KK
5377 case R_SH_TLS_GD_32:
5378 case R_SH_TLS_LD_32:
5379 case R_SH_TLS_IE_32:
8e45593f
NC
5380 if (htab->root.dynobj == NULL)
5381 htab->root.dynobj = abfd;
5382 if (!create_got_section (htab->root.dynobj, info))
5383 return FALSE;
37c644f2
AO
5384 break;
5385
5386 default:
5387 break;
5388 }
5389 }
5390
3376eaf5 5391 switch (r_type)
067653c5
AM
5392 {
5393 /* This relocation describes the C++ object vtable hierarchy.
5394 Reconstruct it for later use during GC. */
435b1e90 5395 case R_SH_GNU_VTINHERIT:
c152c796 5396 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5397 return FALSE;
435b1e90
KH
5398 break;
5399
067653c5
AM
5400 /* This relocation describes which C++ vtable entries are actually
5401 used. Record for later use during GC. */
435b1e90 5402 case R_SH_GNU_VTENTRY:
a0ea3a14 5403 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5404 return FALSE;
435b1e90 5405 break;
37c644f2 5406
3376eaf5 5407 case R_SH_TLS_IE_32:
0e1862bb 5408 if (bfd_link_pic (info))
3376eaf5
KK
5409 info->flags |= DF_STATIC_TLS;
5410
5411 /* FALLTHROUGH */
267fb3c1 5412 force_got:
3376eaf5 5413 case R_SH_TLS_GD_32:
37c644f2 5414 case R_SH_GOT32:
8e45593f 5415 case R_SH_GOT20:
8e45593f
NC
5416 case R_SH_GOTFUNCDESC:
5417 case R_SH_GOTFUNCDESC20:
3376eaf5
KK
5418 switch (r_type)
5419 {
5420 default:
8e45593f 5421 got_type = GOT_NORMAL;
3376eaf5
KK
5422 break;
5423 case R_SH_TLS_GD_32:
8e45593f 5424 got_type = GOT_TLS_GD;
3376eaf5
KK
5425 break;
5426 case R_SH_TLS_IE_32:
8e45593f
NC
5427 got_type = GOT_TLS_IE;
5428 break;
5429 case R_SH_GOTFUNCDESC:
5430 case R_SH_GOTFUNCDESC20:
5431 got_type = GOT_FUNCDESC;
3376eaf5
KK
5432 break;
5433 }
5434
37c644f2 5435 if (h != NULL)
3376eaf5 5436 {
211dc24b 5437 h->got.refcount += 1;
8e45593f 5438 old_got_type = sh_elf_hash_entry (h)->got_type;
3376eaf5 5439 }
37c644f2
AO
5440 else
5441 {
067653c5
AM
5442 bfd_signed_vma *local_got_refcounts;
5443
435b1e90 5444 /* This is a global offset table entry for a local
067653c5
AM
5445 symbol. */
5446 local_got_refcounts = elf_local_got_refcounts (abfd);
5447 if (local_got_refcounts == NULL)
37c644f2 5448 {
dc810e39 5449 bfd_size_type size;
37c644f2 5450
dc810e39 5451 size = symtab_hdr->sh_info;
067653c5 5452 size *= sizeof (bfd_signed_vma);
3376eaf5 5453 size += symtab_hdr->sh_info;
067653c5
AM
5454 local_got_refcounts = ((bfd_signed_vma *)
5455 bfd_zalloc (abfd, size));
5456 if (local_got_refcounts == NULL)
b34976b6 5457 return FALSE;
067653c5 5458 elf_local_got_refcounts (abfd) = local_got_refcounts;
8e45593f 5459 sh_elf_local_got_type (abfd)
3376eaf5 5460 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
37c644f2 5461 }
211dc24b 5462 local_got_refcounts[r_symndx] += 1;
8e45593f 5463 old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
3376eaf5
KK
5464 }
5465
5466 /* If a TLS symbol is accessed using IE at least once,
5467 there is no point to use dynamic model for it. */
8e45593f
NC
5468 if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
5469 && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
3376eaf5 5470 {
8e45593f
NC
5471 if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
5472 got_type = GOT_TLS_IE;
3376eaf5
KK
5473 else
5474 {
8e45593f
NC
5475 if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
5476 && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
4eca0228 5477 _bfd_error_handler
695344c0 5478 /* xgettext:c-format */
871b3ab2 5479 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
8e45593f
NC
5480 abfd, h->root.root.string);
5481 else if (old_got_type == GOT_FUNCDESC
5482 || got_type == GOT_FUNCDESC)
4eca0228 5483 _bfd_error_handler
695344c0 5484 /* xgettext:c-format */
871b3ab2 5485 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
8e45593f
NC
5486 abfd, h->root.root.string);
5487 else
4eca0228 5488 _bfd_error_handler
695344c0 5489 /* xgettext:c-format */
871b3ab2 5490 (_("%pB: `%s' accessed both as normal and thread local symbol"),
4eca0228 5491 abfd, h->root.root.string);
b34976b6 5492 return FALSE;
3376eaf5
KK
5493 }
5494 }
5495
8e45593f 5496 if (old_got_type != got_type)
3376eaf5
KK
5497 {
5498 if (h != NULL)
8e45593f 5499 sh_elf_hash_entry (h)->got_type = got_type;
3376eaf5 5500 else
8e45593f 5501 sh_elf_local_got_type (abfd) [r_symndx] = got_type;
37c644f2 5502 }
3376eaf5
KK
5503
5504 break;
5505
5506 case R_SH_TLS_LD_32:
5507 sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
37c644f2
AO
5508 break;
5509
8e45593f
NC
5510 case R_SH_FUNCDESC:
5511 case R_SH_GOTOFFFUNCDESC:
5512 case R_SH_GOTOFFFUNCDESC20:
5513 if (rel->r_addend)
5514 {
4eca0228 5515 _bfd_error_handler
871b3ab2 5516 (_("%pB: Function descriptor relocation with non-zero addend"),
8e45593f
NC
5517 abfd);
5518 return FALSE;
5519 }
5520
5521 if (h == NULL)
5522 {
5523 union gotref *local_funcdesc;
5524
5525 /* We need a function descriptor for a local symbol. */
5526 local_funcdesc = sh_elf_local_funcdesc (abfd);
5527 if (local_funcdesc == NULL)
5528 {
5529 bfd_size_type size;
5530
5531 size = symtab_hdr->sh_info * sizeof (union gotref);
8e45593f
NC
5532 local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
5533 if (local_funcdesc == NULL)
5534 return FALSE;
5535 sh_elf_local_funcdesc (abfd) = local_funcdesc;
5536 }
5537 local_funcdesc[r_symndx].refcount += 1;
5538
5539 if (r_type == R_SH_FUNCDESC)
5540 {
0e1862bb 5541 if (!bfd_link_pic (info))
8e45593f
NC
5542 htab->srofixup->size += 4;
5543 else
ce558b89 5544 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
8e45593f
NC
5545 }
5546 }
5547 else
5548 {
5549 sh_elf_hash_entry (h)->funcdesc.refcount++;
5550 if (r_type == R_SH_FUNCDESC)
5551 sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
5552
5553 /* If there is a function descriptor reference, then
5554 there should not be any non-FDPIC references. */
5555 old_got_type = sh_elf_hash_entry (h)->got_type;
5556 if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
5557 {
5558 if (old_got_type == GOT_NORMAL)
4eca0228 5559 _bfd_error_handler
695344c0 5560 /* xgettext:c-format */
871b3ab2 5561 (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
8e45593f
NC
5562 abfd, h->root.root.string);
5563 else
4eca0228 5564 _bfd_error_handler
695344c0 5565 /* xgettext:c-format */
871b3ab2 5566 (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
8e45593f
NC
5567 abfd, h->root.root.string);
5568 }
5569 }
5570 break;
5571
fbca6ad9 5572 case R_SH_GOTPLT32:
fbca6ad9
AO
5573 /* If this is a local symbol, we resolve it directly without
5574 creating a procedure linkage table entry. */
5575
5576 if (h == NULL
f5385ebf 5577 || h->forced_local
0e1862bb 5578 || ! bfd_link_pic (info)
fbca6ad9 5579 || info->symbolic
067653c5 5580 || h->dynindx == -1)
fbca6ad9
AO
5581 goto force_got;
5582
f5385ebf 5583 h->needs_plt = 1;
067653c5 5584 h->plt.refcount += 1;
4989d792 5585 ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
fbca6ad9
AO
5586
5587 break;
5588
37c644f2
AO
5589 case R_SH_PLT32:
5590 /* This symbol requires a procedure linkage table entry. We
5591 actually build the entry in adjust_dynamic_symbol,
5592 because this might be a case of linking PIC code which is
5593 never referenced by a dynamic object, in which case we
5594 don't need to generate a procedure linkage table entry
5595 after all. */
5596
5597 /* If this is a local symbol, we resolve it directly without
5598 creating a procedure linkage table entry. */
5599 if (h == NULL)
5600 continue;
5601
f5385ebf 5602 if (h->forced_local)
37c644f2
AO
5603 break;
5604
f5385ebf 5605 h->needs_plt = 1;
067653c5 5606 h->plt.refcount += 1;
37c644f2
AO
5607 break;
5608
5609 case R_SH_DIR32:
5610 case R_SH_REL32:
0e1862bb 5611 if (h != NULL && ! bfd_link_pic (info))
067653c5 5612 {
f5385ebf 5613 h->non_got_ref = 1;
067653c5
AM
5614 h->plt.refcount += 1;
5615 }
37c644f2
AO
5616
5617 /* If we are creating a shared library, and this is a reloc
5618 against a global symbol, or a non PC relative reloc
5619 against a local symbol, then we need to copy the reloc
5620 into the shared library. However, if we are linking with
5621 -Bsymbolic, we do not need to copy a reloc against a
5622 global symbol which is defined in an object we are
5623 including in the link (i.e., DEF_REGULAR is set). At
5624 this point we have not seen all the input files, so it is
5625 possible that DEF_REGULAR is not set now but will be set
5626 later (it is never cleared). We account for that
5627 possibility below by storing information in the
067653c5
AM
5628 dyn_relocs field of the hash table entry. A similar
5629 situation occurs when creating shared libraries and symbol
5630 visibility changes render the symbol local.
5631
5632 If on the other hand, we are creating an executable, we
5633 may need to keep relocations for symbols satisfied by a
5634 dynamic library if we manage to avoid copy relocs for the
5635 symbol. */
0e1862bb 5636 if ((bfd_link_pic (info)
067653c5 5637 && (sec->flags & SEC_ALLOC) != 0
3376eaf5 5638 && (r_type != R_SH_REL32
067653c5
AM
5639 || (h != NULL
5640 && (! info->symbolic
5641 || h->root.type == bfd_link_hash_defweak
f5385ebf 5642 || !h->def_regular))))
0e1862bb 5643 || (! bfd_link_pic (info)
067653c5
AM
5644 && (sec->flags & SEC_ALLOC) != 0
5645 && h != NULL
5646 && (h->root.type == bfd_link_hash_defweak
f5385ebf 5647 || !h->def_regular)))
37c644f2 5648 {
3bf083ed
AM
5649 struct elf_dyn_relocs *p;
5650 struct elf_dyn_relocs **head;
067653c5 5651
2293c499
AM
5652 if (htab->root.dynobj == NULL)
5653 htab->root.dynobj = abfd;
067653c5 5654
37c644f2
AO
5655 /* When creating a shared object, we must copy these
5656 reloc types into the output file. We create a reloc
5657 section in dynobj and make room for this reloc. */
5658 if (sreloc == NULL)
5659 {
83bac4b0
NC
5660 sreloc = _bfd_elf_make_dynamic_reloc_section
5661 (sec, htab->root.dynobj, 2, abfd, /*rela?*/ TRUE);
37c644f2 5662
37c644f2 5663 if (sreloc == NULL)
83bac4b0 5664 return FALSE;
37c644f2
AO
5665 }
5666
067653c5
AM
5667 /* If this is a global symbol, we count the number of
5668 relocations we need for this symbol. */
5669 if (h != NULL)
190eb1dd 5670 head = &h->dyn_relocs;
067653c5 5671 else
37c644f2 5672 {
87d72d41 5673 /* Track dynamic relocs needed for local syms too. */
067653c5 5674 asection *s;
6edfbbad 5675 void *vpp;
87d72d41 5676 Elf_Internal_Sym *isym;
37c644f2 5677
f1dfbfdb 5678 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
87d72d41
AM
5679 abfd, r_symndx);
5680 if (isym == NULL)
b34976b6 5681 return FALSE;
37c644f2 5682
87d72d41
AM
5683 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5684 if (s == NULL)
5685 s = sec;
5686
6edfbbad 5687 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 5688 head = (struct elf_dyn_relocs **) vpp;
067653c5 5689 }
37c644f2 5690
067653c5
AM
5691 p = *head;
5692 if (p == NULL || p->sec != sec)
5693 {
986f0783 5694 size_t amt = sizeof (*p);
2293c499 5695 p = bfd_alloc (htab->root.dynobj, amt);
37c644f2 5696 if (p == NULL)
b34976b6 5697 return FALSE;
067653c5
AM
5698 p->next = *head;
5699 *head = p;
5700 p->sec = sec;
5701 p->count = 0;
5702 p->pc_count = 0;
37c644f2 5703 }
067653c5
AM
5704
5705 p->count += 1;
211dc24b 5706 if (r_type == R_SH_REL32)
067653c5 5707 p->pc_count += 1;
37c644f2
AO
5708 }
5709
8e45593f
NC
5710 /* Allocate the fixup regardless of whether we need a relocation.
5711 If we end up generating the relocation, we'll unallocate the
5712 fixup. */
0e1862bb 5713 if (htab->fdpic_p && !bfd_link_pic (info)
8e45593f
NC
5714 && r_type == R_SH_DIR32
5715 && (sec->flags & SEC_ALLOC) != 0)
5716 htab->srofixup->size += 4;
37c644f2 5717 break;
3376eaf5
KK
5718
5719 case R_SH_TLS_LE_32:
3cbc1e5e 5720 if (bfd_link_dll (info))
3376eaf5 5721 {
4eca0228 5722 _bfd_error_handler
871b3ab2 5723 (_("%pB: TLS local exec code cannot be linked into shared objects"),
d003868e 5724 abfd);
b34976b6 5725 return FALSE;
3376eaf5
KK
5726 }
5727
267fb3c1 5728 break;
3376eaf5 5729
3376eaf5 5730 case R_SH_TLS_LDO_32:
267fb3c1 5731 /* Nothing to do. */
3376eaf5
KK
5732 break;
5733
5734 default:
5735 break;
435b1e90 5736 }
252b5132 5737 }
435b1e90 5738
b34976b6 5739 return TRUE;
252b5132
RH
5740}
5741
fbca6ad9 5742#ifndef sh_elf_set_mach_from_flags
f6f9408f
JR
5743static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
5744
b34976b6 5745static bfd_boolean
09fd220b 5746sh_elf_set_mach_from_flags (bfd *abfd)
d4845d57 5747{
f6f9408f
JR
5748 flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
5749
f461bbd8 5750 if (flags >= ARRAY_SIZE (sh_ef_bfd_table))
f6f9408f
JR
5751 return FALSE;
5752
5753 if (sh_ef_bfd_table[flags] == 0)
5754 return FALSE;
68ffbac6 5755
f6f9408f 5756 bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
d4845d57 5757
b34976b6 5758 return TRUE;
d4845d57 5759}
f6f9408f
JR
5760
5761
5762/* Reverse table lookup for sh_ef_bfd_table[].
5763 Given a bfd MACH value from archures.c
5764 return the equivalent ELF flags from the table.
5765 Return -1 if no match is found. */
5766
5767int
5768sh_elf_get_flags_from_mach (unsigned long mach)
5769{
4195f552 5770 int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
68ffbac6 5771
f6f9408f
JR
5772 for (; i>0; i--)
5773 if (sh_ef_bfd_table[i] == mach)
5774 return i;
68ffbac6 5775
f6f9408f
JR
5776 /* shouldn't get here */
5777 BFD_FAIL();
5778
5779 return -1;
5780}
fbca6ad9 5781#endif /* not sh_elf_set_mach_from_flags */
d4845d57 5782
fbca6ad9 5783#ifndef sh_elf_copy_private_data
d4845d57 5784/* Copy backend specific data from one object module to another */
435b1e90 5785
b34976b6 5786static bfd_boolean
09fd220b 5787sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
d4845d57 5788{
0ffa91dd
NC
5789 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
5790 return TRUE;
5791
e2349352
L
5792 if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
5793 return FALSE;
5794
5795 return sh_elf_set_mach_from_flags (obfd);
d4845d57 5796}
fbca6ad9 5797#endif /* not sh_elf_copy_private_data */
d4845d57 5798
fbca6ad9 5799#ifndef sh_elf_merge_private_data
d4845d57 5800
f6f9408f
JR
5801/* This function returns the ELF architecture number that
5802 corresponds to the given arch_sh* flags. */
85fbca6a 5803
f6f9408f
JR
5804int
5805sh_find_elf_flags (unsigned int arch_set)
d4845d57 5806{
85fbca6a 5807 extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
f6f9408f 5808 unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
d4845d57 5809
f6f9408f
JR
5810 return sh_elf_get_flags_from_mach (bfd_mach);
5811}
f6f9408f 5812
1047201f
AM
5813/* Merge the architecture type of two BFD files, such that the
5814 resultant architecture supports all the features required
5815 by the two input BFDs.
5816 If the input BFDs are multually incompatible - i.e. one uses
5817 DSP while the other uses FPU - or there is no known architecture
5818 that fits the requirements then an error is emitted. */
5819
5820static bfd_boolean
50e03d47 5821sh_merge_bfd_arch (bfd *ibfd, struct bfd_link_info *info)
1047201f 5822{
50e03d47 5823 bfd *obfd = info->output_bfd;
1047201f
AM
5824 unsigned int old_arch, new_arch, merged_arch;
5825
50e03d47 5826 if (! _bfd_generic_verify_endian_match (ibfd, info))
1047201f
AM
5827 return FALSE;
5828
5829 old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
5830 new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
5831
5832 merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
5833
5834 if (!SH_VALID_CO_ARCH_SET (merged_arch))
5835 {
5836 _bfd_error_handler
695344c0 5837 /* xgettext:c-format */
871b3ab2 5838 (_("%pB: uses %s instructions while previous modules "
695344c0 5839 "use %s instructions"),
1047201f
AM
5840 ibfd,
5841 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
5842 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
5843 bfd_set_error (bfd_error_bad_value);
5844 return FALSE;
5845 }
5846 else if (!SH_VALID_ARCH_SET (merged_arch))
5847 {
5848 _bfd_error_handler
695344c0
NC
5849 /* xgettext:c-format */
5850 (_("internal error: merge of architecture '%s' with "
5851 "architecture '%s' produced unknown architecture"),
1047201f
AM
5852 bfd_printable_name (obfd),
5853 bfd_printable_name (ibfd));
5854 bfd_set_error (bfd_error_bad_value);
5855 return FALSE;
5856 }
5857
5858 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
5859 sh_get_bfd_mach_from_arch_set (merged_arch));
5860
5861 return TRUE;
5862}
5863
f6f9408f
JR
5864/* This routine initialises the elf flags when required and
5865 calls sh_merge_bfd_arch() to check dsp/fpu compatibility. */
d4845d57 5866
f6f9408f 5867static bfd_boolean
50e03d47 5868sh_elf_merge_private_data (bfd *ibfd, struct bfd_link_info *info)
f6f9408f 5869{
50e03d47 5870 bfd *obfd = info->output_bfd;
85fbca6a 5871
6b728d32
AM
5872 /* FIXME: What should be checked when linking shared libraries? */
5873 if ((ibfd->flags & DYNAMIC) != 0)
5874 return TRUE;
5875
0ffa91dd 5876 if (! is_sh_elf (ibfd) || ! is_sh_elf (obfd))
b34976b6 5877 return TRUE;
d4845d57
JR
5878
5879 if (! elf_flags_init (obfd))
5880 {
a39b79b9 5881 /* This happens when ld starts out with a 'blank' output file. */
b34976b6 5882 elf_flags_init (obfd) = TRUE;
8e45593f 5883 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
f6f9408f 5884 sh_elf_set_mach_from_flags (obfd);
8e45593f 5885 if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
fbedb42d 5886 elf_elfheader (obfd)->e_flags &= ~EF_SH_PIC;
d4845d57 5887 }
d4845d57 5888
50e03d47 5889 if (! sh_merge_bfd_arch (ibfd, info))
1d70c7fb 5890 {
871b3ab2 5891 _bfd_error_handler (_("%pB: uses instructions which are incompatible "
695344c0 5892 "with instructions used in previous modules"),
d003868e 5893 ibfd);
1d70c7fb
AO
5894 bfd_set_error (bfd_error_bad_value);
5895 return FALSE;
5896 }
f6f9408f 5897
8e45593f
NC
5898 elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
5899 elf_elfheader (obfd)->e_flags |=
f6f9408f 5900 sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
8e45593f
NC
5901
5902 if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
5903 {
871b3ab2 5904 _bfd_error_handler (_("%pB: attempt to mix FDPIC and non-FDPIC objects"),
8e45593f
NC
5905 ibfd);
5906 bfd_set_error (bfd_error_bad_value);
5907 return FALSE;
5908 }
5909
f6f9408f 5910 return TRUE;
d4845d57 5911}
fbca6ad9 5912#endif /* not sh_elf_merge_private_data */
d4845d57 5913
3376eaf5
KK
5914/* Override the generic function because we need to store sh_elf_obj_tdata
5915 as the specific tdata. We set also the machine architecture from flags
5916 here. */
5917
b34976b6 5918static bfd_boolean
09fd220b 5919sh_elf_object_p (bfd *abfd)
3376eaf5 5920{
8e45593f
NC
5921 if (! sh_elf_set_mach_from_flags (abfd))
5922 return FALSE;
5923
5924 return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
5925 == fdpic_object_p (abfd));
3376eaf5
KK
5926}
5927
37c644f2
AO
5928/* Finish up dynamic symbol handling. We set the contents of various
5929 dynamic sections here. */
5930
b34976b6 5931static bfd_boolean
09fd220b
KK
5932sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5933 struct elf_link_hash_entry *h,
5934 Elf_Internal_Sym *sym)
37c644f2 5935{
067653c5 5936 struct elf_sh_link_hash_table *htab;
37c644f2 5937
067653c5 5938 htab = sh_elf_hash_table (info);
4dfe6ac6
NC
5939 if (htab == NULL)
5940 return FALSE;
37c644f2
AO
5941
5942 if (h->plt.offset != (bfd_vma) -1)
5943 {
5944 asection *splt;
8e45593f
NC
5945 asection *sgotplt;
5946 asection *srelplt;
37c644f2
AO
5947
5948 bfd_vma plt_index;
5949 bfd_vma got_offset;
5950 Elf_Internal_Rela rel;
947216bf 5951 bfd_byte *loc;
8e45593f 5952 const struct elf_sh_plt_info *plt_info;
37c644f2
AO
5953
5954 /* This symbol has an entry in the procedure linkage table. Set
5955 it up. */
5956
5957 BFD_ASSERT (h->dynindx != -1);
5958
ce558b89
AM
5959 splt = htab->root.splt;
5960 sgotplt = htab->root.sgotplt;
5961 srelplt = htab->root.srelplt;
8e45593f 5962 BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
37c644f2
AO
5963
5964 /* Get the index in the procedure linkage table which
5965 corresponds to this symbol. This is the index of this symbol
5966 in all the symbols for which we are making plt entries. The
5967 first entry in the procedure linkage table is reserved. */
55e6e397 5968 plt_index = get_plt_index (htab->plt_info, h->plt.offset);
37c644f2 5969
8e45593f
NC
5970 plt_info = htab->plt_info;
5971 if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
5972 plt_info = plt_info->short_plt;
5973
37c644f2 5974 /* Get the offset into the .got table of the entry that
8e45593f
NC
5975 corresponds to this function. */
5976 if (htab->fdpic_p)
5977 /* The offset must be relative to the GOT symbol, twelve bytes
5978 before the end of .got.plt. Each descriptor is eight
5979 bytes. */
5980 got_offset = plt_index * 8 + 12 - sgotplt->size;
5981 else
5982 /* Each .got entry is 4 bytes. The first three are
5983 reserved. */
5984 got_offset = (plt_index + 3) * 4;
37c644f2 5985
fbca6ad9 5986#ifdef GOT_BIAS
0e1862bb 5987 if (bfd_link_pic (info))
c8614e8e 5988 got_offset -= GOT_BIAS;
fbca6ad9
AO
5989#endif
5990
37c644f2 5991 /* Fill in the entry in the procedure linkage table. */
55e6e397 5992 memcpy (splt->contents + h->plt.offset,
8e45593f
NC
5993 plt_info->symbol_entry,
5994 plt_info->symbol_entry_size);
55e6e397 5995
0e1862bb 5996 if (bfd_link_pic (info) || htab->fdpic_p)
8e45593f
NC
5997 {
5998 if (plt_info->symbol_fields.got20)
5999 {
6000 bfd_reloc_status_type r;
6001 r = install_movi20_field (output_bfd, got_offset,
6002 splt->owner, splt, splt->contents,
6003 h->plt.offset
6004 + plt_info->symbol_fields.got_entry);
6005 BFD_ASSERT (r == bfd_reloc_ok);
6006 }
6007 else
6008 install_plt_field (output_bfd, FALSE, got_offset,
6009 (splt->contents
6010 + h->plt.offset
6011 + plt_info->symbol_fields.got_entry));
6012 }
55e6e397 6013 else
37c644f2 6014 {
8e45593f
NC
6015 BFD_ASSERT (!plt_info->symbol_fields.got20);
6016
55e6e397 6017 install_plt_field (output_bfd, FALSE,
8e45593f
NC
6018 (sgotplt->output_section->vma
6019 + sgotplt->output_offset
fbca6ad9 6020 + got_offset),
55e6e397
RS
6021 (splt->contents
6022 + h->plt.offset
8e45593f 6023 + plt_info->symbol_fields.got_entry));
90c14f0c 6024 if (htab->root.target_os == is_vxworks)
37c644f2 6025 {
55e6e397
RS
6026 unsigned int reachable_plts, plts_per_4k;
6027 int distance;
6028
6029 /* Divide the PLT into groups. The first group contains
6030 REACHABLE_PLTS entries and the other groups contain
6031 PLTS_PER_4K entries. Entries in the first group can
6032 branch directly to .plt; those in later groups branch
6033 to the last element of the previous group. */
6034 /* ??? It would be better to create multiple copies of
6035 the common resolver stub. */
6036 reachable_plts = ((4096
8e45593f
NC
6037 - plt_info->plt0_entry_size
6038 - (plt_info->symbol_fields.plt + 4))
6039 / plt_info->symbol_entry_size) + 1;
6040 plts_per_4k = (4096 / plt_info->symbol_entry_size);
55e6e397
RS
6041 if (plt_index < reachable_plts)
6042 distance = -(h->plt.offset
8e45593f 6043 + plt_info->symbol_fields.plt);
55e6e397
RS
6044 else
6045 distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
8e45593f 6046 * plt_info->symbol_entry_size);
55e6e397
RS
6047
6048 /* Install the 'bra' with this offset. */
6049 bfd_put_16 (output_bfd,
6050 0xa000 | (0x0fff & ((distance - 4) / 2)),
6051 (splt->contents
6052 + h->plt.offset
8e45593f 6053 + plt_info->symbol_fields.plt));
37c644f2 6054 }
55e6e397
RS
6055 else
6056 install_plt_field (output_bfd, TRUE,
6057 splt->output_section->vma + splt->output_offset,
6058 (splt->contents
6059 + h->plt.offset
8e45593f 6060 + plt_info->symbol_fields.plt));
37c644f2
AO
6061 }
6062
8e45593f 6063 /* Make got_offset relative to the start of .got.plt. */
fbca6ad9 6064#ifdef GOT_BIAS
0e1862bb 6065 if (bfd_link_pic (info))
c8614e8e 6066 got_offset += GOT_BIAS;
fbca6ad9 6067#endif
8e45593f
NC
6068 if (htab->fdpic_p)
6069 got_offset = plt_index * 8;
fbca6ad9 6070
8e45593f
NC
6071 if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
6072 install_plt_field (output_bfd, FALSE,
6073 plt_index * sizeof (Elf32_External_Rela),
6074 (splt->contents
6075 + h->plt.offset
6076 + plt_info->symbol_fields.reloc_offset));
37c644f2
AO
6077
6078 /* Fill in the entry in the global offset table. */
6079 bfd_put_32 (output_bfd,
6080 (splt->output_section->vma
6081 + splt->output_offset
6082 + h->plt.offset
8e45593f
NC
6083 + plt_info->symbol_resolve_offset),
6084 sgotplt->contents + got_offset);
6085 if (htab->fdpic_p)
6086 bfd_put_32 (output_bfd,
ce558b89 6087 sh_elf_osec_to_segment (output_bfd, splt->output_section),
8e45593f 6088 sgotplt->contents + got_offset + 4);
37c644f2
AO
6089
6090 /* Fill in the entry in the .rela.plt section. */
8e45593f
NC
6091 rel.r_offset = (sgotplt->output_section->vma
6092 + sgotplt->output_offset
37c644f2 6093 + got_offset);
8e45593f
NC
6094 if (htab->fdpic_p)
6095 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
6096 else
6097 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
37c644f2 6098 rel.r_addend = 0;
fbca6ad9
AO
6099#ifdef GOT_BIAS
6100 rel.r_addend = GOT_BIAS;
6101#endif
8e45593f 6102 loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
947216bf 6103 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2 6104
90c14f0c 6105 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
55e6e397
RS
6106 {
6107 /* Create the .rela.plt.unloaded relocations for this PLT entry.
6108 Begin by pointing LOC to the first such relocation. */
6109 loc = (htab->srelplt2->contents
6110 + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
6111
6112 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6113 for the PLT entry's pointer to the .got.plt entry. */
ce558b89
AM
6114 rel.r_offset = (splt->output_section->vma
6115 + splt->output_offset
55e6e397 6116 + h->plt.offset
8e45593f 6117 + plt_info->symbol_fields.got_entry);
55e6e397
RS
6118 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6119 rel.r_addend = got_offset;
6120 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6121 loc += sizeof (Elf32_External_Rela);
6122
6123 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6124 the .got.plt entry, which initially points to .plt. */
8e45593f
NC
6125 rel.r_offset = (sgotplt->output_section->vma
6126 + sgotplt->output_offset
55e6e397
RS
6127 + got_offset);
6128 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
6129 rel.r_addend = 0;
6130 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6131 }
6132
f5385ebf 6133 if (!h->def_regular)
37c644f2
AO
6134 {
6135 /* Mark the symbol as undefined, rather than as defined in
6136 the .plt section. Leave the value alone. */
6137 sym->st_shndx = SHN_UNDEF;
6138 }
6139 }
6140
3376eaf5 6141 if (h->got.offset != (bfd_vma) -1
8e45593f
NC
6142 && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
6143 && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
6144 && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
37c644f2
AO
6145 {
6146 asection *sgot;
8e45593f 6147 asection *srelgot;
37c644f2 6148 Elf_Internal_Rela rel;
947216bf 6149 bfd_byte *loc;
37c644f2
AO
6150
6151 /* This symbol has an entry in the global offset table. Set it
6152 up. */
6153
ce558b89
AM
6154 sgot = htab->root.sgot;
6155 srelgot = htab->root.srelgot;
8e45593f 6156 BFD_ASSERT (sgot != NULL && srelgot != NULL);
37c644f2
AO
6157
6158 rel.r_offset = (sgot->output_section->vma
6159 + sgot->output_offset
dc810e39 6160 + (h->got.offset &~ (bfd_vma) 1));
37c644f2 6161
067653c5
AM
6162 /* If this is a static link, or it is a -Bsymbolic link and the
6163 symbol is defined locally or was forced to be local because
6164 of a version file, we just want to emit a RELATIVE reloc.
37c644f2
AO
6165 The entry in the global offset table will already have been
6166 initialized in the relocate_section function. */
0e1862bb 6167 if (bfd_link_pic (info)
3d85aebe 6168 && SYMBOL_REFERENCES_LOCAL (info, h))
37c644f2 6169 {
8e45593f
NC
6170 if (htab->fdpic_p)
6171 {
6172 asection *sec = h->root.u.def.section;
6173 int dynindx
6174 = elf_section_data (sec->output_section)->dynindx;
6175
6176 rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6177 rel.r_addend = (h->root.u.def.value
6178 + h->root.u.def.section->output_offset);
6179 }
6180 else
6181 {
6182 rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6183 rel.r_addend = (h->root.u.def.value
6184 + h->root.u.def.section->output_section->vma
6185 + h->root.u.def.section->output_offset);
6186 }
37c644f2
AO
6187 }
6188 else
6189 {
6190 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6191 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6192 rel.r_addend = 0;
6193 }
6194
8e45593f
NC
6195 loc = srelgot->contents;
6196 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
947216bf 6197 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2
AO
6198 }
6199
f5385ebf 6200 if (h->needs_copy)
37c644f2
AO
6201 {
6202 asection *s;
6203 Elf_Internal_Rela rel;
947216bf 6204 bfd_byte *loc;
37c644f2
AO
6205
6206 /* This symbol needs a copy reloc. Set it up. */
6207
6208 BFD_ASSERT (h->dynindx != -1
6209 && (h->root.type == bfd_link_hash_defined
6210 || h->root.type == bfd_link_hash_defweak));
6211
3d4d4302 6212 s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
37c644f2
AO
6213 BFD_ASSERT (s != NULL);
6214
6215 rel.r_offset = (h->root.u.def.value
6216 + h->root.u.def.section->output_section->vma
6217 + h->root.u.def.section->output_offset);
6218 rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
6219 rel.r_addend = 0;
947216bf
AM
6220 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6221 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
37c644f2
AO
6222 }
6223
55e6e397
RS
6224 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. On VxWorks,
6225 _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6226 ".got" section. */
9637f6ef 6227 if (h == htab->root.hdynamic
90c14f0c 6228 || (htab->root.target_os != is_vxworks && h == htab->root.hgot))
37c644f2
AO
6229 sym->st_shndx = SHN_ABS;
6230
b34976b6 6231 return TRUE;
37c644f2
AO
6232}
6233
6234/* Finish up the dynamic sections. */
6235
b34976b6 6236static bfd_boolean
09fd220b 6237sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
37c644f2 6238{
067653c5 6239 struct elf_sh_link_hash_table *htab;
8e45593f 6240 asection *sgotplt;
37c644f2
AO
6241 asection *sdyn;
6242
067653c5 6243 htab = sh_elf_hash_table (info);
4dfe6ac6
NC
6244 if (htab == NULL)
6245 return FALSE;
6246
ce558b89 6247 sgotplt = htab->root.sgotplt;
3d4d4302 6248 sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
37c644f2 6249
067653c5 6250 if (htab->root.dynamic_sections_created)
37c644f2
AO
6251 {
6252 asection *splt;
6253 Elf32_External_Dyn *dyncon, *dynconend;
6254
8e45593f 6255 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
37c644f2
AO
6256
6257 dyncon = (Elf32_External_Dyn *) sdyn->contents;
eea6121a 6258 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
37c644f2
AO
6259 for (; dyncon < dynconend; dyncon++)
6260 {
6261 Elf_Internal_Dyn dyn;
37c644f2
AO
6262 asection *s;
6263
2293c499 6264 bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
37c644f2
AO
6265
6266 switch (dyn.d_tag)
6267 {
6268 default:
90c14f0c 6269 if (htab->root.target_os == is_vxworks
7a2b07ff
NS
6270 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
6271 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
37c644f2
AO
6272 break;
6273
6274 case DT_PLTGOT:
8e45593f
NC
6275 BFD_ASSERT (htab->root.hgot != NULL);
6276 s = htab->root.hgot->root.u.def.section;
6277 dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
6278 + s->output_section->vma + s->output_offset;
6279 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6280 break;
37c644f2
AO
6281
6282 case DT_JMPREL:
ce558b89 6283 s = htab->root.srelplt->output_section;
37c644f2
AO
6284 BFD_ASSERT (s != NULL);
6285 dyn.d_un.d_ptr = s->vma;
6286 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6287 break;
6288
6289 case DT_PLTRELSZ:
ce558b89 6290 s = htab->root.srelplt->output_section;
37c644f2 6291 BFD_ASSERT (s != NULL);
eea6121a 6292 dyn.d_un.d_val = s->size;
37c644f2
AO
6293 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6294 break;
37c644f2
AO
6295 }
6296 }
6297
6298 /* Fill in the first entry in the procedure linkage table. */
ce558b89 6299 splt = htab->root.splt;
55e6e397 6300 if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
37c644f2 6301 {
55e6e397
RS
6302 unsigned int i;
6303
6304 memcpy (splt->contents,
6305 htab->plt_info->plt0_entry,
6306 htab->plt_info->plt0_entry_size);
6307 for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
6308 if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
6309 install_plt_field (output_bfd, FALSE,
8e45593f
NC
6310 (sgotplt->output_section->vma
6311 + sgotplt->output_offset
55e6e397
RS
6312 + (i * 4)),
6313 (splt->contents
6314 + htab->plt_info->plt0_got_fields[i]));
6315
90c14f0c 6316 if (htab->root.target_os == is_vxworks)
37c644f2 6317 {
55e6e397
RS
6318 /* Finalize the .rela.plt.unloaded contents. */
6319 Elf_Internal_Rela rel;
6320 bfd_byte *loc;
6321
6322 /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
6323 first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8. */
6324 loc = htab->srelplt2->contents;
6325 rel.r_offset = (splt->output_section->vma
6326 + splt->output_offset
6327 + htab->plt_info->plt0_got_fields[2]);
6328 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6329 rel.r_addend = 8;
6330 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6331 loc += sizeof (Elf32_External_Rela);
6332
6333 /* Fix up the remaining .rela.plt.unloaded relocations.
6334 They may have the wrong symbol index for _G_O_T_ or
6335 _P_L_T_ depending on the order in which symbols were
6336 output. */
6337 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
37c644f2 6338 {
55e6e397
RS
6339 /* The PLT entry's pointer to the .got.plt slot. */
6340 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
6341 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
6342 R_SH_DIR32);
6343 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6344 loc += sizeof (Elf32_External_Rela);
6345
6346 /* The .got.plt slot's pointer to .plt. */
6347 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
6348 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
6349 R_SH_DIR32);
6350 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6351 loc += sizeof (Elf32_External_Rela);
37c644f2 6352 }
37c644f2
AO
6353 }
6354
6355 /* UnixWare sets the entsize of .plt to 4, although that doesn't
6356 really seem like the right value. */
6357 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
6358 }
6359 }
6360
6361 /* Fill in the first three entries in the global offset table. */
8e45593f 6362 if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
37c644f2
AO
6363 {
6364 if (sdyn == NULL)
8e45593f 6365 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
37c644f2
AO
6366 else
6367 bfd_put_32 (output_bfd,
6368 sdyn->output_section->vma + sdyn->output_offset,
8e45593f
NC
6369 sgotplt->contents);
6370 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6371 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6372 }
37c644f2 6373
8e45593f
NC
6374 if (sgotplt && sgotplt->size > 0)
6375 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
68ffbac6 6376
8e45593f
NC
6377 /* At the very end of the .rofixup section is a pointer to the GOT. */
6378 if (htab->fdpic_p && htab->srofixup != NULL)
6379 {
6380 struct elf_link_hash_entry *hgot = htab->root.hgot;
6381 bfd_vma got_value = hgot->root.u.def.value
6382 + hgot->root.u.def.section->output_section->vma
6383 + hgot->root.u.def.section->output_offset;
6384
6385 sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
6386
6387 /* Make sure we allocated and generated the same number of fixups. */
6388 BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
067653c5 6389 }
37c644f2 6390
8e45593f
NC
6391 if (htab->srelfuncdesc)
6392 BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
6393 == htab->srelfuncdesc->size);
6394
ce558b89
AM
6395 if (htab->root.srelgot)
6396 BFD_ASSERT (htab->root.srelgot->reloc_count * sizeof (Elf32_External_Rela)
6397 == htab->root.srelgot->size);
8e45593f 6398
b34976b6 6399 return TRUE;
37c644f2
AO
6400}
6401
99e4ae17 6402static enum elf_reloc_type_class
7e612e98
AM
6403sh_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
6404 const asection *rel_sec ATTRIBUTE_UNUSED,
6405 const Elf_Internal_Rela *rela)
99e4ae17 6406{
f51e552e 6407 switch ((int) ELF32_R_TYPE (rela->r_info))
99e4ae17
AJ
6408 {
6409 case R_SH_RELATIVE:
6410 return reloc_class_relative;
6411 case R_SH_JMP_SLOT:
6412 return reloc_class_plt;
6413 case R_SH_COPY:
6414 return reloc_class_copy;
6415 default:
6416 return reloc_class_normal;
6417 }
6418}
6419
85fbca6a 6420#if !defined SH_TARGET_ALREADY_DEFINED
571fe01f 6421/* Support for Linux core dump NOTE sections. */
85fbca6a 6422
b34976b6 6423static bfd_boolean
09fd220b 6424elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2bc3c89a
AM
6425{
6426 int offset;
eea6121a 6427 unsigned int size;
2bc3c89a
AM
6428
6429 switch (note->descsz)
6430 {
6431 default:
b34976b6 6432 return FALSE;
2bc3c89a
AM
6433
6434 case 168: /* Linux/SH */
6435 /* pr_cursig */
228e534f 6436 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2bc3c89a
AM
6437
6438 /* pr_pid */
228e534f 6439 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
2bc3c89a
AM
6440
6441 /* pr_reg */
6442 offset = 72;
eea6121a 6443 size = 92;
2bc3c89a
AM
6444
6445 break;
6446 }
6447
6448 /* Make a ".reg/999" section. */
6449 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 6450 size, note->descpos + offset);
2bc3c89a
AM
6451}
6452
b34976b6 6453static bfd_boolean
09fd220b 6454elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2bc3c89a
AM
6455{
6456 switch (note->descsz)
6457 {
6458 default:
b34976b6 6459 return FALSE;
2bc3c89a
AM
6460
6461 case 124: /* Linux/SH elf_prpsinfo */
228e534f 6462 elf_tdata (abfd)->core->program
2bc3c89a 6463 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 6464 elf_tdata (abfd)->core->command
2bc3c89a
AM
6465 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
6466 }
6467
6468 /* Note that for some reason, a spurious space is tacked
6469 onto the end of the args in some (at least one anyway)
6470 implementations, so strip it off if it exists. */
6471
6472 {
228e534f 6473 char *command = elf_tdata (abfd)->core->command;
2bc3c89a
AM
6474 int n = strlen (command);
6475
6476 if (0 < n && command[n - 1] == ' ')
6477 command[n - 1] = '\0';
6478 }
6479
b34976b6 6480 return TRUE;
2bc3c89a 6481}
85fbca6a 6482#endif /* not SH_TARGET_ALREADY_DEFINED */
2bc3c89a 6483
68ffbac6 6484
1f1799d5
KK
6485/* Return address for Ith PLT stub in section PLT, for relocation REL
6486 or (bfd_vma) -1 if it should not be included. */
6487
6488static bfd_vma
6489sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
6490 const arelent *rel ATTRIBUTE_UNUSED)
6491{
55e6e397
RS
6492 const struct elf_sh_plt_info *plt_info;
6493
6494 plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
6495 return plt->vma + get_plt_offset (plt_info, i);
1f1799d5
KK
6496}
6497
8e45593f
NC
6498/* Decide whether to attempt to turn absptr or lsda encodings in
6499 shared libraries into pcrel within the given input section. */
6500
6501static bfd_boolean
6502sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
6503 struct bfd_link_info *info,
6504 asection *eh_frame_section ATTRIBUTE_UNUSED)
6505{
6506 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
6507
6508 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
6509 if (htab->fdpic_p)
6510 return FALSE;
6511
6512 return TRUE;
6513}
6514
6515/* Adjust the contents of an eh_frame_hdr section before they're output. */
6516
6517static bfd_byte
6518sh_elf_encode_eh_address (bfd *abfd,
6519 struct bfd_link_info *info,
6520 asection *osec, bfd_vma offset,
6521 asection *loc_sec, bfd_vma loc_offset,
6522 bfd_vma *encoded)
6523{
6524 struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
6525 struct elf_link_hash_entry *h;
6526
6527 if (!htab->fdpic_p)
6528 return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
6529 loc_offset, encoded);
6530
6531 h = htab->root.hgot;
6532 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
6533
6534 if (! h || (sh_elf_osec_to_segment (abfd, osec)
6535 == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
6536 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
6537 loc_sec, loc_offset, encoded);
6538
6539 BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
6540 == (sh_elf_osec_to_segment
6541 (abfd, h->root.u.def.section->output_section)));
6542
6543 *encoded = osec->vma + offset
6544 - (h->root.u.def.value
6545 + h->root.u.def.section->output_section->vma
6546 + h->root.u.def.section->output_offset);
6547
6548 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
6549}
6550
85fbca6a 6551#if !defined SH_TARGET_ALREADY_DEFINED
6d00b590 6552#define TARGET_BIG_SYM sh_elf32_vec
252b5132 6553#define TARGET_BIG_NAME "elf32-sh"
6d00b590 6554#define TARGET_LITTLE_SYM sh_elf32_le_vec
252b5132 6555#define TARGET_LITTLE_NAME "elf32-shl"
85fbca6a
NC
6556#endif
6557
252b5132 6558#define ELF_ARCH bfd_arch_sh
ae95ffa6 6559#define ELF_TARGET_ID SH_ELF_DATA
252b5132 6560#define ELF_MACHINE_CODE EM_SH
d0facd1b
NC
6561#ifdef __QNXTARGET__
6562#define ELF_MAXPAGESIZE 0x1000
6563#else
6564#define ELF_MAXPAGESIZE 0x80
6565#endif
252b5132
RH
6566
6567#define elf_symbol_leading_char '_'
6568
6569#define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
157090f7
AM
6570#define bfd_elf32_bfd_reloc_name_lookup \
6571 sh_elf_reloc_name_lookup
252b5132
RH
6572#define elf_info_to_howto sh_elf_info_to_howto
6573#define bfd_elf32_bfd_relax_section sh_elf_relax_section
6574#define elf_backend_relocate_section sh_elf_relocate_section
6575#define bfd_elf32_bfd_get_relocated_section_contents \
6576 sh_elf_get_relocated_section_contents
3376eaf5
KK
6577#define bfd_elf32_mkobject sh_elf_mkobject
6578#define elf_backend_object_p sh_elf_object_p
d4845d57
JR
6579#define bfd_elf32_bfd_copy_private_bfd_data \
6580 sh_elf_copy_private_data
875f7f69 6581#define bfd_elf32_bfd_merge_private_bfd_data \
d4845d57 6582 sh_elf_merge_private_data
252b5132 6583
067653c5 6584#define elf_backend_gc_mark_hook sh_elf_gc_mark_hook
067653c5
AM
6585#define elf_backend_check_relocs sh_elf_check_relocs
6586#define elf_backend_copy_indirect_symbol \
6587 sh_elf_copy_indirect_symbol
37c644f2
AO
6588#define elf_backend_create_dynamic_sections \
6589 sh_elf_create_dynamic_sections
6590#define bfd_elf32_bfd_link_hash_table_create \
6591 sh_elf_link_hash_table_create
6592#define elf_backend_adjust_dynamic_symbol \
6593 sh_elf_adjust_dynamic_symbol
55e6e397
RS
6594#define elf_backend_always_size_sections \
6595 sh_elf_always_size_sections
37c644f2
AO
6596#define elf_backend_size_dynamic_sections \
6597 sh_elf_size_dynamic_sections
8e45593f 6598#define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
37c644f2
AO
6599#define elf_backend_finish_dynamic_symbol \
6600 sh_elf_finish_dynamic_symbol
6601#define elf_backend_finish_dynamic_sections \
6602 sh_elf_finish_dynamic_sections
99e4ae17 6603#define elf_backend_reloc_type_class sh_elf_reloc_type_class
1f1799d5 6604#define elf_backend_plt_sym_val sh_elf_plt_sym_val
8e45593f
NC
6605#define elf_backend_can_make_relative_eh_frame \
6606 sh_elf_use_relative_eh_frame
6607#define elf_backend_can_make_lsda_relative_eh_frame \
6608 sh_elf_use_relative_eh_frame
6609#define elf_backend_encode_eh_address \
6610 sh_elf_encode_eh_address
37c644f2 6611
04c3a755 6612#define elf_backend_stack_align 8
067653c5
AM
6613#define elf_backend_can_gc_sections 1
6614#define elf_backend_can_refcount 1
37c644f2
AO
6615#define elf_backend_want_got_plt 1
6616#define elf_backend_plt_readonly 1
6617#define elf_backend_want_plt_sym 0
6618#define elf_backend_got_header_size 12
64f52338 6619#define elf_backend_dtrel_excludes_plt 1
ed71e111 6620
a2f63b2e
MR
6621#define elf_backend_linux_prpsinfo32_ugid16 TRUE
6622
211dc24b 6623#if !defined SH_TARGET_ALREADY_DEFINED
2bc3c89a 6624
252b5132 6625#include "elf32-target.h"
ed71e111 6626
2bc3c89a
AM
6627/* NetBSD support. */
6628#undef TARGET_BIG_SYM
6d00b590 6629#define TARGET_BIG_SYM sh_elf32_nbsd_vec
2bc3c89a
AM
6630#undef TARGET_BIG_NAME
6631#define TARGET_BIG_NAME "elf32-sh-nbsd"
6632#undef TARGET_LITTLE_SYM
6d00b590 6633#define TARGET_LITTLE_SYM sh_elf32_nbsd_le_vec
2bc3c89a
AM
6634#undef TARGET_LITTLE_NAME
6635#define TARGET_LITTLE_NAME "elf32-shl-nbsd"
6636#undef ELF_MAXPAGESIZE
6637#define ELF_MAXPAGESIZE 0x10000
24718e3b 6638#undef ELF_COMMONPAGESIZE
2bc3c89a
AM
6639#undef elf_symbol_leading_char
6640#define elf_symbol_leading_char 0
571fe01f 6641#undef elf32_bed
2bc3c89a
AM
6642#define elf32_bed elf32_sh_nbsd_bed
6643
6644#include "elf32-target.h"
6645
2bc3c89a
AM
6646
6647/* Linux support. */
6648#undef TARGET_BIG_SYM
6d00b590 6649#define TARGET_BIG_SYM sh_elf32_linux_be_vec
2bc3c89a
AM
6650#undef TARGET_BIG_NAME
6651#define TARGET_BIG_NAME "elf32-shbig-linux"
6652#undef TARGET_LITTLE_SYM
6d00b590 6653#define TARGET_LITTLE_SYM sh_elf32_linux_vec
2bc3c89a
AM
6654#undef TARGET_LITTLE_NAME
6655#define TARGET_LITTLE_NAME "elf32-sh-linux"
24718e3b
L
6656#undef ELF_COMMONPAGESIZE
6657#define ELF_COMMONPAGESIZE 0x1000
2bc3c89a
AM
6658
6659#undef elf_backend_grok_prstatus
6660#define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6661#undef elf_backend_grok_psinfo
6662#define elf_backend_grok_psinfo elf32_shlin_grok_psinfo
571fe01f 6663#undef elf32_bed
2bc3c89a
AM
6664#define elf32_bed elf32_sh_lin_bed
6665
6666#include "elf32-target.h"
6667
8e45593f
NC
6668
6669/* FDPIC support. */
6670#undef TARGET_BIG_SYM
6d00b590 6671#define TARGET_BIG_SYM sh_elf32_fdpic_be_vec
8e45593f
NC
6672#undef TARGET_BIG_NAME
6673#define TARGET_BIG_NAME "elf32-shbig-fdpic"
6674#undef TARGET_LITTLE_SYM
6d00b590 6675#define TARGET_LITTLE_SYM sh_elf32_fdpic_le_vec
8e45593f
NC
6676#undef TARGET_LITTLE_NAME
6677#define TARGET_LITTLE_NAME "elf32-sh-fdpic"
8e45593f
NC
6678
6679#undef elf32_bed
6680#define elf32_bed elf32_sh_fd_bed
6681
6682#include "elf32-target.h"
6683
8e45593f 6684/* VxWorks support. */
55e6e397 6685#undef TARGET_BIG_SYM
6d00b590 6686#define TARGET_BIG_SYM sh_elf32_vxworks_vec
55e6e397
RS
6687#undef TARGET_BIG_NAME
6688#define TARGET_BIG_NAME "elf32-sh-vxworks"
6689#undef TARGET_LITTLE_SYM
6d00b590 6690#define TARGET_LITTLE_SYM sh_elf32_vxworks_le_vec
55e6e397
RS
6691#undef TARGET_LITTLE_NAME
6692#define TARGET_LITTLE_NAME "elf32-shl-vxworks"
6693#undef elf32_bed
6694#define elf32_bed elf32_sh_vxworks_bed
6695
6696#undef elf_backend_want_plt_sym
6697#define elf_backend_want_plt_sym 1
6698#undef elf_symbol_leading_char
6699#define elf_symbol_leading_char '_'
6700#define elf_backend_want_got_underscore 1
6701#undef elf_backend_grok_prstatus
6702#undef elf_backend_grok_psinfo
6703#undef elf_backend_add_symbol_hook
6704#define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6705#undef elf_backend_link_output_symbol_hook
6706#define elf_backend_link_output_symbol_hook \
6707 elf_vxworks_link_output_symbol_hook
6708#undef elf_backend_emit_relocs
6709#define elf_backend_emit_relocs elf_vxworks_emit_relocs
6710#undef elf_backend_final_write_processing
6711#define elf_backend_final_write_processing \
6712 elf_vxworks_final_write_processing
6713#undef ELF_MAXPAGESIZE
6714#define ELF_MAXPAGESIZE 0x1000
6715#undef ELF_COMMONPAGESIZE
6716
90c14f0c
L
6717#undef ELF_TARGET_OS
6718#define ELF_TARGET_OS is_vxworks
6719
55e6e397
RS
6720#include "elf32-target.h"
6721
211dc24b 6722#endif /* not SH_TARGET_ALREADY_DEFINED */