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