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