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