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