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