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