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