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