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