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