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