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