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