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