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