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