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