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