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