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