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