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