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