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