Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* Hitachi SH specific support for 32-bit ELF |
2 | Copyright 1996, 1997, 1998 Free Software Foundation, Inc. | |
3 | Contributed by Ian Lance Taylor, Cygnus Support. | |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "bfdlink.h" | |
24 | #include "libbfd.h" | |
25 | #include "elf-bfd.h" | |
26 | #include "elf/sh.h" | |
27 | ||
28 | static bfd_reloc_status_type sh_elf_reloc | |
29 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
30 | static bfd_reloc_status_type sh_elf_ignore_reloc | |
31 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
32 | static reloc_howto_type *sh_elf_reloc_type_lookup | |
33 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
34 | static void sh_elf_info_to_howto | |
35 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); | |
36 | static boolean sh_elf_relax_section | |
37 | PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *)); | |
38 | static boolean sh_elf_relax_delete_bytes | |
39 | PARAMS ((bfd *, asection *, bfd_vma, int)); | |
40 | static boolean sh_elf_align_loads | |
41 | PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, boolean *)); | |
42 | static boolean sh_elf_swap_insns | |
43 | PARAMS ((bfd *, asection *, PTR, bfd_byte *, bfd_vma)); | |
44 | static boolean sh_elf_relocate_section | |
45 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
46 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
47 | static bfd_byte *sh_elf_get_relocated_section_contents | |
48 | PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, | |
49 | bfd_byte *, boolean, asymbol **)); | |
50 | ||
51 | static reloc_howto_type sh_elf_howto_table[] = | |
52 | { | |
53 | /* No relocation. */ | |
54 | HOWTO (R_SH_NONE, /* type */ | |
55 | 0, /* rightshift */ | |
56 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
57 | 0, /* bitsize */ | |
58 | false, /* pc_relative */ | |
59 | 0, /* bitpos */ | |
60 | complain_overflow_dont, /* complain_on_overflow */ | |
61 | sh_elf_reloc, /* special_function */ | |
62 | "R_SH_NONE", /* name */ | |
63 | false, /* partial_inplace */ | |
64 | 0, /* src_mask */ | |
65 | 0, /* dst_mask */ | |
66 | false), /* pcrel_offset */ | |
67 | ||
68 | /* 32 bit absolute relocation. Setting partial_inplace to true and | |
69 | src_mask to a non-zero value is similar to the COFF toolchain. */ | |
70 | HOWTO (R_SH_DIR32, /* type */ | |
71 | 0, /* rightshift */ | |
72 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
73 | 32, /* bitsize */ | |
74 | false, /* pc_relative */ | |
75 | 0, /* bitpos */ | |
76 | complain_overflow_bitfield, /* complain_on_overflow */ | |
77 | sh_elf_reloc, /* special_function */ | |
78 | "R_SH_DIR32", /* name */ | |
79 | true, /* partial_inplace */ | |
80 | 0xffffffff, /* src_mask */ | |
81 | 0xffffffff, /* dst_mask */ | |
82 | false), /* pcrel_offset */ | |
83 | ||
84 | /* 32 bit PC relative relocation. */ | |
85 | HOWTO (R_SH_REL32, /* type */ | |
86 | 0, /* rightshift */ | |
87 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
88 | 32, /* bitsize */ | |
89 | true, /* pc_relative */ | |
90 | 0, /* bitpos */ | |
91 | complain_overflow_signed, /* complain_on_overflow */ | |
92 | sh_elf_reloc, /* special_function */ | |
93 | "R_SH_REL32", /* name */ | |
94 | false, /* partial_inplace */ | |
95 | 0, /* src_mask */ | |
96 | 0xffffffff, /* dst_mask */ | |
97 | true), /* pcrel_offset */ | |
98 | ||
99 | /* 8 bit PC relative branch divided by 2. */ | |
100 | HOWTO (R_SH_DIR8WPN, /* type */ | |
101 | 1, /* rightshift */ | |
102 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
103 | 8, /* bitsize */ | |
104 | true, /* pc_relative */ | |
105 | 0, /* bitpos */ | |
106 | complain_overflow_signed, /* complain_on_overflow */ | |
107 | sh_elf_reloc, /* special_function */ | |
108 | "R_SH_DIR8WPN", /* name */ | |
109 | true, /* partial_inplace */ | |
110 | 0xff, /* src_mask */ | |
111 | 0xff, /* dst_mask */ | |
112 | true), /* pcrel_offset */ | |
113 | ||
114 | /* 12 bit PC relative branch divided by 2. */ | |
115 | HOWTO (R_SH_IND12W, /* type */ | |
116 | 1, /* rightshift */ | |
117 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
118 | 12, /* bitsize */ | |
119 | true, /* pc_relative */ | |
120 | 0, /* bitpos */ | |
121 | complain_overflow_signed, /* complain_on_overflow */ | |
122 | sh_elf_reloc, /* special_function */ | |
123 | "R_SH_IND12W", /* name */ | |
124 | true, /* partial_inplace */ | |
125 | 0xfff, /* src_mask */ | |
126 | 0xfff, /* dst_mask */ | |
127 | true), /* pcrel_offset */ | |
128 | ||
129 | /* 8 bit unsigned PC relative divided by 4. */ | |
130 | HOWTO (R_SH_DIR8WPL, /* type */ | |
131 | 2, /* rightshift */ | |
132 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
133 | 8, /* bitsize */ | |
134 | true, /* pc_relative */ | |
135 | 0, /* bitpos */ | |
136 | complain_overflow_unsigned, /* complain_on_overflow */ | |
137 | sh_elf_reloc, /* special_function */ | |
138 | "R_SH_DIR8WPL", /* name */ | |
139 | true, /* partial_inplace */ | |
140 | 0xff, /* src_mask */ | |
141 | 0xff, /* dst_mask */ | |
142 | true), /* pcrel_offset */ | |
143 | ||
144 | /* 8 bit unsigned PC relative divided by 2. */ | |
145 | HOWTO (R_SH_DIR8WPZ, /* type */ | |
146 | 1, /* rightshift */ | |
147 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
148 | 8, /* bitsize */ | |
149 | true, /* pc_relative */ | |
150 | 0, /* bitpos */ | |
151 | complain_overflow_unsigned, /* complain_on_overflow */ | |
152 | sh_elf_reloc, /* special_function */ | |
153 | "R_SH_DIR8WPZ", /* name */ | |
154 | true, /* partial_inplace */ | |
155 | 0xff, /* src_mask */ | |
156 | 0xff, /* dst_mask */ | |
157 | true), /* pcrel_offset */ | |
158 | ||
159 | /* 8 bit GBR relative. FIXME: This only makes sense if we have some | |
160 | special symbol for the GBR relative area, and that is not | |
161 | implemented. */ | |
162 | HOWTO (R_SH_DIR8BP, /* type */ | |
163 | 0, /* rightshift */ | |
164 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
165 | 8, /* bitsize */ | |
166 | false, /* pc_relative */ | |
167 | 0, /* bitpos */ | |
168 | complain_overflow_unsigned, /* complain_on_overflow */ | |
169 | sh_elf_reloc, /* special_function */ | |
170 | "R_SH_DIR8BP", /* name */ | |
171 | false, /* partial_inplace */ | |
172 | 0, /* src_mask */ | |
173 | 0xff, /* dst_mask */ | |
174 | true), /* pcrel_offset */ | |
175 | ||
176 | /* 8 bit GBR relative divided by 2. FIXME: This only makes sense if | |
177 | we have some special symbol for the GBR relative area, and that | |
178 | is not implemented. */ | |
179 | HOWTO (R_SH_DIR8W, /* type */ | |
180 | 1, /* rightshift */ | |
181 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
182 | 8, /* bitsize */ | |
183 | false, /* pc_relative */ | |
184 | 0, /* bitpos */ | |
185 | complain_overflow_unsigned, /* complain_on_overflow */ | |
186 | sh_elf_reloc, /* special_function */ | |
187 | "R_SH_DIR8W", /* name */ | |
188 | false, /* partial_inplace */ | |
189 | 0, /* src_mask */ | |
190 | 0xff, /* dst_mask */ | |
191 | true), /* pcrel_offset */ | |
192 | ||
193 | /* 8 bit GBR relative divided by 4. FIXME: This only makes sense if | |
194 | we have some special symbol for the GBR relative area, and that | |
195 | is not implemented. */ | |
196 | HOWTO (R_SH_DIR8L, /* type */ | |
197 | 2, /* rightshift */ | |
198 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
199 | 8, /* bitsize */ | |
200 | false, /* pc_relative */ | |
201 | 0, /* bitpos */ | |
202 | complain_overflow_unsigned, /* complain_on_overflow */ | |
203 | sh_elf_reloc, /* special_function */ | |
204 | "R_SH_DIR8L", /* name */ | |
205 | false, /* partial_inplace */ | |
206 | 0, /* src_mask */ | |
207 | 0xff, /* dst_mask */ | |
208 | true), /* pcrel_offset */ | |
209 | ||
210 | { 10 }, | |
211 | { 11 }, | |
212 | { 12 }, | |
213 | { 13 }, | |
214 | { 14 }, | |
215 | { 15 }, | |
216 | { 16 }, | |
217 | { 17 }, | |
218 | { 18 }, | |
219 | { 19 }, | |
220 | { 20 }, | |
221 | { 21 }, | |
222 | { 22 }, | |
223 | { 23 }, | |
224 | { 24 }, | |
225 | ||
226 | /* The remaining relocs are a GNU extension used for relaxing. The | |
227 | final pass of the linker never needs to do anything with any of | |
228 | these relocs. Any required operations are handled by the | |
229 | relaxation code. */ | |
230 | ||
231 | /* A 16 bit switch table entry. This is generated for an expression | |
232 | such as ``.word L1 - L2''. The offset holds the difference | |
233 | between the reloc address and L2. */ | |
234 | HOWTO (R_SH_SWITCH16, /* type */ | |
235 | 0, /* rightshift */ | |
236 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
237 | 16, /* bitsize */ | |
238 | false, /* pc_relative */ | |
239 | 0, /* bitpos */ | |
240 | complain_overflow_unsigned, /* complain_on_overflow */ | |
241 | sh_elf_ignore_reloc, /* special_function */ | |
242 | "R_SH_SWITCH16", /* name */ | |
243 | false, /* partial_inplace */ | |
244 | 0, /* src_mask */ | |
245 | 0, /* dst_mask */ | |
246 | true), /* pcrel_offset */ | |
247 | ||
248 | /* A 32 bit switch table entry. This is generated for an expression | |
249 | such as ``.long L1 - L2''. The offset holds the difference | |
250 | between the reloc address and L2. */ | |
251 | HOWTO (R_SH_SWITCH32, /* type */ | |
252 | 0, /* rightshift */ | |
253 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
254 | 32, /* bitsize */ | |
255 | false, /* pc_relative */ | |
256 | 0, /* bitpos */ | |
257 | complain_overflow_unsigned, /* complain_on_overflow */ | |
258 | sh_elf_ignore_reloc, /* special_function */ | |
259 | "R_SH_SWITCH32", /* name */ | |
260 | false, /* partial_inplace */ | |
261 | 0, /* src_mask */ | |
262 | 0, /* dst_mask */ | |
263 | true), /* pcrel_offset */ | |
264 | ||
265 | /* Indicates a .uses pseudo-op. The compiler will generate .uses | |
266 | pseudo-ops when it finds a function call which can be relaxed. | |
267 | The offset field holds the PC relative offset to the instruction | |
268 | which loads the register used in the function call. */ | |
269 | HOWTO (R_SH_USES, /* type */ | |
270 | 0, /* rightshift */ | |
271 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
272 | 0, /* bitsize */ | |
273 | false, /* pc_relative */ | |
274 | 0, /* bitpos */ | |
275 | complain_overflow_unsigned, /* complain_on_overflow */ | |
276 | sh_elf_ignore_reloc, /* special_function */ | |
277 | "R_SH_USES", /* name */ | |
278 | false, /* partial_inplace */ | |
279 | 0, /* src_mask */ | |
280 | 0, /* dst_mask */ | |
281 | true), /* pcrel_offset */ | |
282 | ||
283 | /* The assembler will generate this reloc for addresses referred to | |
284 | by the register loads associated with USES relocs. The offset | |
285 | field holds the number of times the address is referenced in the | |
286 | object file. */ | |
287 | HOWTO (R_SH_COUNT, /* type */ | |
288 | 0, /* rightshift */ | |
289 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
290 | 0, /* bitsize */ | |
291 | false, /* pc_relative */ | |
292 | 0, /* bitpos */ | |
293 | complain_overflow_unsigned, /* complain_on_overflow */ | |
294 | sh_elf_ignore_reloc, /* special_function */ | |
295 | "R_SH_COUNT", /* name */ | |
296 | false, /* partial_inplace */ | |
297 | 0, /* src_mask */ | |
298 | 0, /* dst_mask */ | |
299 | true), /* pcrel_offset */ | |
300 | ||
301 | /* Indicates an alignment statement. The offset field is the power | |
302 | of 2 to which subsequent portions of the object file must be | |
303 | aligned. */ | |
304 | HOWTO (R_SH_ALIGN, /* type */ | |
305 | 0, /* rightshift */ | |
306 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
307 | 0, /* bitsize */ | |
308 | false, /* pc_relative */ | |
309 | 0, /* bitpos */ | |
310 | complain_overflow_unsigned, /* complain_on_overflow */ | |
311 | sh_elf_ignore_reloc, /* special_function */ | |
312 | "R_SH_ALIGN", /* name */ | |
313 | false, /* partial_inplace */ | |
314 | 0, /* src_mask */ | |
315 | 0, /* dst_mask */ | |
316 | true), /* pcrel_offset */ | |
317 | ||
318 | /* The assembler will generate this reloc before a block of | |
319 | instructions. A section should be processed as assumining it | |
320 | contains data, unless this reloc is seen. */ | |
321 | HOWTO (R_SH_CODE, /* type */ | |
322 | 0, /* rightshift */ | |
323 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
324 | 0, /* bitsize */ | |
325 | false, /* pc_relative */ | |
326 | 0, /* bitpos */ | |
327 | complain_overflow_unsigned, /* complain_on_overflow */ | |
328 | sh_elf_ignore_reloc, /* special_function */ | |
329 | "R_SH_CODE", /* name */ | |
330 | false, /* partial_inplace */ | |
331 | 0, /* src_mask */ | |
332 | 0, /* dst_mask */ | |
333 | true), /* pcrel_offset */ | |
334 | ||
335 | /* The assembler will generate this reloc after a block of | |
336 | instructions when it sees data that is not instructions. */ | |
337 | HOWTO (R_SH_DATA, /* type */ | |
338 | 0, /* rightshift */ | |
339 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
340 | 0, /* bitsize */ | |
341 | false, /* pc_relative */ | |
342 | 0, /* bitpos */ | |
343 | complain_overflow_unsigned, /* complain_on_overflow */ | |
344 | sh_elf_ignore_reloc, /* special_function */ | |
345 | "R_SH_DATA", /* name */ | |
346 | false, /* partial_inplace */ | |
347 | 0, /* src_mask */ | |
348 | 0, /* dst_mask */ | |
349 | true), /* pcrel_offset */ | |
350 | ||
351 | /* The assembler generates this reloc for each label within a block | |
352 | of instructions. This permits the linker to avoid swapping | |
353 | instructions which are the targets of branches. */ | |
354 | HOWTO (R_SH_LABEL, /* type */ | |
355 | 0, /* rightshift */ | |
356 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
357 | 0, /* bitsize */ | |
358 | false, /* pc_relative */ | |
359 | 0, /* bitpos */ | |
360 | complain_overflow_unsigned, /* complain_on_overflow */ | |
361 | sh_elf_ignore_reloc, /* special_function */ | |
362 | "R_SH_LABEL", /* name */ | |
363 | false, /* partial_inplace */ | |
364 | 0, /* src_mask */ | |
365 | 0, /* dst_mask */ | |
366 | true), /* pcrel_offset */ | |
367 | ||
368 | /* An 8 bit switch table entry. This is generated for an expression | |
369 | such as ``.word L1 - L2''. The offset holds the difference | |
370 | between the reloc address and L2. */ | |
371 | HOWTO (R_SH_SWITCH8, /* type */ | |
372 | 0, /* rightshift */ | |
373 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
374 | 8, /* bitsize */ | |
375 | false, /* pc_relative */ | |
376 | 0, /* bitpos */ | |
377 | complain_overflow_unsigned, /* complain_on_overflow */ | |
378 | sh_elf_ignore_reloc, /* special_function */ | |
379 | "R_SH_SWITCH8", /* name */ | |
380 | false, /* partial_inplace */ | |
381 | 0, /* src_mask */ | |
382 | 0, /* dst_mask */ | |
383 | true), /* pcrel_offset */ | |
384 | ||
385 | /* GNU extension to record C++ vtable hierarchy */ | |
386 | HOWTO (R_SH_GNU_VTINHERIT, /* type */ | |
387 | 0, /* rightshift */ | |
388 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
389 | 0, /* bitsize */ | |
390 | false, /* pc_relative */ | |
391 | 0, /* bitpos */ | |
392 | complain_overflow_dont, /* complain_on_overflow */ | |
393 | NULL, /* special_function */ | |
394 | "R_SH_GNU_VTINHERIT", /* name */ | |
395 | false, /* partial_inplace */ | |
396 | 0, /* src_mask */ | |
397 | 0, /* dst_mask */ | |
398 | false), /* pcrel_offset */ | |
399 | ||
400 | /* GNU extension to record C++ vtable member usage */ | |
401 | HOWTO (R_SH_GNU_VTENTRY, /* type */ | |
402 | 0, /* rightshift */ | |
403 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
404 | 0, /* bitsize */ | |
405 | false, /* pc_relative */ | |
406 | 0, /* bitpos */ | |
407 | complain_overflow_dont, /* complain_on_overflow */ | |
408 | _bfd_elf_rel_vtable_reloc_fn, /* special_function */ | |
409 | "R_SH_GNU_VTENTRY", /* name */ | |
410 | false, /* partial_inplace */ | |
411 | 0, /* src_mask */ | |
412 | 0, /* dst_mask */ | |
413 | false), /* pcrel_offset */ | |
414 | ||
415 | }; | |
416 | ||
417 | /* This function is used for normal relocs. This is like the COFF | |
418 | function, and is almost certainly incorrect for other ELF targets. */ | |
419 | ||
420 | static bfd_reloc_status_type | |
421 | sh_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd, | |
422 | error_message) | |
423 | bfd *abfd; | |
424 | arelent *reloc_entry; | |
425 | asymbol *symbol_in; | |
426 | PTR data; | |
427 | asection *input_section; | |
428 | bfd *output_bfd; | |
429 | char **error_message; | |
430 | { | |
431 | unsigned long insn; | |
432 | bfd_vma sym_value; | |
433 | enum elf_sh_reloc_type r_type; | |
434 | bfd_vma addr = reloc_entry->address; | |
435 | bfd_byte *hit_data = addr + (bfd_byte *) data; | |
436 | ||
437 | r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type; | |
438 | ||
439 | if (output_bfd != NULL) | |
440 | { | |
441 | /* Partial linking--do nothing. */ | |
442 | reloc_entry->address += input_section->output_offset; | |
443 | return bfd_reloc_ok; | |
444 | } | |
445 | ||
446 | /* Almost all relocs have to do with relaxing. If any work must be | |
447 | done for them, it has been done in sh_relax_section. */ | |
448 | if (r_type != R_SH_DIR32 | |
449 | && (r_type != R_SH_IND12W | |
450 | || (symbol_in->flags & BSF_LOCAL) != 0)) | |
451 | return bfd_reloc_ok; | |
452 | ||
453 | if (symbol_in != NULL | |
454 | && bfd_is_und_section (symbol_in->section)) | |
455 | return bfd_reloc_undefined; | |
456 | ||
457 | if (bfd_is_com_section (symbol_in->section)) | |
458 | sym_value = 0; | |
459 | else | |
460 | sym_value = (symbol_in->value + | |
461 | symbol_in->section->output_section->vma + | |
462 | symbol_in->section->output_offset); | |
463 | ||
464 | switch (r_type) | |
465 | { | |
466 | case R_SH_DIR32: | |
467 | insn = bfd_get_32 (abfd, hit_data); | |
468 | insn += sym_value + reloc_entry->addend; | |
469 | bfd_put_32 (abfd, insn, hit_data); | |
470 | break; | |
471 | case R_SH_IND12W: | |
472 | insn = bfd_get_16 (abfd, hit_data); | |
473 | sym_value += reloc_entry->addend; | |
474 | sym_value -= (input_section->output_section->vma | |
475 | + input_section->output_offset | |
476 | + addr | |
477 | + 4); | |
478 | sym_value += (insn & 0xfff) << 1; | |
479 | if (insn & 0x800) | |
480 | sym_value -= 0x1000; | |
481 | insn = (insn & 0xf000) | (sym_value & 0xfff); | |
482 | bfd_put_16 (abfd, insn, hit_data); | |
483 | if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000) | |
484 | return bfd_reloc_overflow; | |
485 | break; | |
486 | default: | |
487 | abort (); | |
488 | break; | |
489 | } | |
490 | ||
491 | return bfd_reloc_ok; | |
492 | } | |
493 | ||
494 | /* This function is used for relocs which are only used for relaxing, | |
495 | which the linker should otherwise ignore. */ | |
496 | ||
497 | static bfd_reloc_status_type | |
498 | sh_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section, | |
499 | output_bfd, error_message) | |
500 | bfd *abfd; | |
501 | arelent *reloc_entry; | |
502 | asymbol *symbol; | |
503 | PTR data; | |
504 | asection *input_section; | |
505 | bfd *output_bfd; | |
506 | char **error_message; | |
507 | { | |
508 | if (output_bfd != NULL) | |
509 | reloc_entry->address += input_section->output_offset; | |
510 | return bfd_reloc_ok; | |
511 | } | |
512 | ||
513 | /* This structure is used to map BFD reloc codes to SH ELF relocs. */ | |
514 | ||
515 | struct elf_reloc_map | |
516 | { | |
517 | bfd_reloc_code_real_type bfd_reloc_val; | |
518 | unsigned char elf_reloc_val; | |
519 | }; | |
520 | ||
521 | /* An array mapping BFD reloc codes to SH ELF relocs. */ | |
522 | ||
523 | static const struct elf_reloc_map sh_reloc_map[] = | |
524 | { | |
525 | { BFD_RELOC_NONE, R_SH_NONE }, | |
526 | { BFD_RELOC_32, R_SH_DIR32 }, | |
527 | { BFD_RELOC_CTOR, R_SH_DIR32 }, | |
528 | { BFD_RELOC_32_PCREL, R_SH_REL32 }, | |
529 | { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN }, | |
530 | { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W }, | |
531 | { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ }, | |
532 | { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL }, | |
533 | { BFD_RELOC_8_PCREL, R_SH_SWITCH8 }, | |
534 | { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 }, | |
535 | { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 }, | |
536 | { BFD_RELOC_SH_USES, R_SH_USES }, | |
537 | { BFD_RELOC_SH_COUNT, R_SH_COUNT }, | |
538 | { BFD_RELOC_SH_ALIGN, R_SH_ALIGN }, | |
539 | { BFD_RELOC_SH_CODE, R_SH_CODE }, | |
540 | { BFD_RELOC_SH_DATA, R_SH_DATA }, | |
541 | { BFD_RELOC_SH_LABEL, R_SH_LABEL }, | |
542 | { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT }, | |
543 | { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY }, | |
544 | }; | |
545 | ||
546 | /* Given a BFD reloc code, return the howto structure for the | |
547 | corresponding SH ELf reloc. */ | |
548 | ||
549 | static reloc_howto_type * | |
550 | sh_elf_reloc_type_lookup (abfd, code) | |
551 | bfd *abfd; | |
552 | bfd_reloc_code_real_type code; | |
553 | { | |
554 | unsigned int i; | |
555 | ||
556 | for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++) | |
557 | { | |
558 | if (sh_reloc_map[i].bfd_reloc_val == code) | |
559 | return &sh_elf_howto_table[(int) sh_reloc_map[i].elf_reloc_val]; | |
560 | } | |
561 | ||
562 | return NULL; | |
563 | } | |
564 | ||
565 | /* Given an ELF reloc, fill in the howto field of a relent. */ | |
566 | ||
567 | static void | |
568 | sh_elf_info_to_howto (abfd, cache_ptr, dst) | |
569 | bfd *abfd; | |
570 | arelent *cache_ptr; | |
571 | Elf_Internal_Rela *dst; | |
572 | { | |
573 | unsigned int r; | |
574 | ||
575 | r = ELF32_R_TYPE (dst->r_info); | |
576 | ||
577 | BFD_ASSERT (r < (unsigned int) R_SH_max); | |
578 | BFD_ASSERT (r < R_SH_FIRST_INVALID_RELOC || r > R_SH_LAST_INVALID_RELOC); | |
579 | ||
580 | cache_ptr->howto = &sh_elf_howto_table[r]; | |
581 | } | |
582 | \f | |
583 | /* This function handles relaxing for SH ELF. See the corresponding | |
584 | function in coff-sh.c for a description of what this does. FIXME: | |
585 | There is a lot of duplication here between this code and the COFF | |
586 | specific code. The format of relocs and symbols is wound deeply | |
587 | into this code, but it would still be better if the duplication | |
588 | could be eliminated somehow. Note in particular that although both | |
589 | functions use symbols like R_SH_CODE, those symbols have different | |
590 | values; in coff-sh.c they come from include/coff/sh.h, whereas here | |
591 | they come from enum elf_sh_reloc_type in include/elf/sh.h. */ | |
592 | ||
593 | static boolean | |
594 | sh_elf_relax_section (abfd, sec, link_info, again) | |
595 | bfd *abfd; | |
596 | asection *sec; | |
597 | struct bfd_link_info *link_info; | |
598 | boolean *again; | |
599 | { | |
600 | Elf_Internal_Shdr *symtab_hdr; | |
601 | Elf_Internal_Rela *internal_relocs; | |
602 | Elf_Internal_Rela *free_relocs = NULL; | |
603 | boolean have_code; | |
604 | Elf_Internal_Rela *irel, *irelend; | |
605 | bfd_byte *contents = NULL; | |
606 | bfd_byte *free_contents = NULL; | |
607 | Elf32_External_Sym *extsyms = NULL; | |
608 | Elf32_External_Sym *free_extsyms = NULL; | |
609 | ||
610 | *again = false; | |
611 | ||
612 | if (link_info->relocateable | |
613 | || (sec->flags & SEC_RELOC) == 0 | |
614 | || sec->reloc_count == 0) | |
615 | return true; | |
616 | ||
617 | /* If this is the first time we have been called for this section, | |
618 | initialize the cooked size. */ | |
619 | if (sec->_cooked_size == 0) | |
620 | sec->_cooked_size = sec->_raw_size; | |
621 | ||
622 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
623 | ||
624 | internal_relocs = (_bfd_elf32_link_read_relocs | |
625 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
626 | link_info->keep_memory)); | |
627 | if (internal_relocs == NULL) | |
628 | goto error_return; | |
629 | if (! link_info->keep_memory) | |
630 | free_relocs = internal_relocs; | |
631 | ||
632 | have_code = false; | |
633 | ||
634 | irelend = internal_relocs + sec->reloc_count; | |
635 | for (irel = internal_relocs; irel < irelend; irel++) | |
636 | { | |
637 | bfd_vma laddr, paddr, symval; | |
638 | unsigned short insn; | |
639 | Elf_Internal_Rela *irelfn, *irelscan, *irelcount; | |
640 | bfd_signed_vma foff; | |
641 | ||
642 | if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE) | |
643 | have_code = true; | |
644 | ||
645 | if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES) | |
646 | continue; | |
647 | ||
648 | /* Get the section contents. */ | |
649 | if (contents == NULL) | |
650 | { | |
651 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
652 | contents = elf_section_data (sec)->this_hdr.contents; | |
653 | else | |
654 | { | |
655 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
656 | if (contents == NULL) | |
657 | goto error_return; | |
658 | free_contents = contents; | |
659 | ||
660 | if (! bfd_get_section_contents (abfd, sec, contents, | |
661 | (file_ptr) 0, sec->_raw_size)) | |
662 | goto error_return; | |
663 | } | |
664 | } | |
665 | ||
666 | /* The r_addend field of the R_SH_USES reloc will point us to | |
667 | the register load. The 4 is because the r_addend field is | |
668 | computed as though it were a jump offset, which are based | |
669 | from 4 bytes after the jump instruction. */ | |
670 | laddr = irel->r_offset + 4 + irel->r_addend; | |
671 | if (laddr >= sec->_raw_size) | |
672 | { | |
673 | (*_bfd_error_handler) (_("%s: 0x%lx: warning: bad R_SH_USES offset"), | |
674 | bfd_get_filename (abfd), | |
675 | (unsigned long) irel->r_offset); | |
676 | continue; | |
677 | } | |
678 | insn = bfd_get_16 (abfd, contents + laddr); | |
679 | ||
680 | /* If the instruction is not mov.l NN,rN, we don't know what to | |
681 | do. */ | |
682 | if ((insn & 0xf000) != 0xd000) | |
683 | { | |
684 | ((*_bfd_error_handler) | |
685 | (_("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x"), | |
686 | bfd_get_filename (abfd), (unsigned long) irel->r_offset, insn)); | |
687 | continue; | |
688 | } | |
689 | ||
690 | /* Get the address from which the register is being loaded. The | |
691 | displacement in the mov.l instruction is quadrupled. It is a | |
692 | displacement from four bytes after the movl instruction, but, | |
693 | before adding in the PC address, two least significant bits | |
694 | of the PC are cleared. We assume that the section is aligned | |
695 | on a four byte boundary. */ | |
696 | paddr = insn & 0xff; | |
697 | paddr *= 4; | |
698 | paddr += (laddr + 4) &~ 3; | |
699 | if (paddr >= sec->_raw_size) | |
700 | { | |
701 | ((*_bfd_error_handler) | |
702 | (_("%s: 0x%lx: warning: bad R_SH_USES load offset"), | |
703 | bfd_get_filename (abfd), (unsigned long) irel->r_offset)); | |
704 | continue; | |
705 | } | |
706 | ||
707 | /* Get the reloc for the address from which the register is | |
708 | being loaded. This reloc will tell us which function is | |
709 | actually being called. */ | |
710 | for (irelfn = internal_relocs; irelfn < irelend; irelfn++) | |
711 | if (irelfn->r_offset == paddr | |
712 | && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32) | |
713 | break; | |
714 | if (irelfn >= irelend) | |
715 | { | |
716 | ((*_bfd_error_handler) | |
717 | (_("%s: 0x%lx: warning: could not find expected reloc"), | |
718 | bfd_get_filename (abfd), (unsigned long) paddr)); | |
719 | continue; | |
720 | } | |
721 | ||
722 | /* Read this BFD's symbols if we haven't done so already. */ | |
723 | if (extsyms == NULL) | |
724 | { | |
725 | if (symtab_hdr->contents != NULL) | |
726 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
727 | else | |
728 | { | |
729 | extsyms = ((Elf32_External_Sym *) | |
730 | bfd_malloc (symtab_hdr->sh_size)); | |
731 | if (extsyms == NULL) | |
732 | goto error_return; | |
733 | free_extsyms = extsyms; | |
734 | if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
735 | || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd) | |
736 | != symtab_hdr->sh_size)) | |
737 | goto error_return; | |
738 | } | |
739 | } | |
740 | ||
741 | /* Get the value of the symbol referred to by the reloc. */ | |
742 | if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info) | |
743 | { | |
744 | Elf_Internal_Sym isym; | |
745 | ||
746 | /* A local symbol. */ | |
747 | bfd_elf32_swap_symbol_in (abfd, | |
748 | extsyms + ELF32_R_SYM (irelfn->r_info), | |
749 | &isym); | |
750 | ||
751 | if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec)) | |
752 | { | |
753 | ((*_bfd_error_handler) | |
754 | (_("%s: 0x%lx: warning: symbol in unexpected section"), | |
755 | bfd_get_filename (abfd), (unsigned long) paddr)); | |
756 | continue; | |
757 | } | |
758 | ||
759 | symval = (isym.st_value | |
760 | + sec->output_section->vma | |
761 | + sec->output_offset); | |
762 | } | |
763 | else | |
764 | { | |
765 | unsigned long indx; | |
766 | struct elf_link_hash_entry *h; | |
767 | ||
768 | indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info; | |
769 | h = elf_sym_hashes (abfd)[indx]; | |
770 | BFD_ASSERT (h != NULL); | |
771 | if (h->root.type != bfd_link_hash_defined | |
772 | && h->root.type != bfd_link_hash_defweak) | |
773 | { | |
774 | /* This appears to be a reference to an undefined | |
775 | symbol. Just ignore it--it will be caught by the | |
776 | regular reloc processing. */ | |
777 | continue; | |
778 | } | |
779 | ||
780 | symval = (h->root.u.def.value | |
781 | + h->root.u.def.section->output_section->vma | |
782 | + h->root.u.def.section->output_offset); | |
783 | } | |
784 | ||
785 | symval += bfd_get_32 (abfd, contents + paddr); | |
786 | ||
787 | /* See if this function call can be shortened. */ | |
788 | foff = (symval | |
789 | - (irel->r_offset | |
790 | + sec->output_section->vma | |
791 | + sec->output_offset | |
792 | + 4)); | |
793 | if (foff < -0x1000 || foff >= 0x1000) | |
794 | { | |
795 | /* After all that work, we can't shorten this function call. */ | |
796 | continue; | |
797 | } | |
798 | ||
799 | /* Shorten the function call. */ | |
800 | ||
801 | /* For simplicity of coding, we are going to modify the section | |
802 | contents, the section relocs, and the BFD symbol table. We | |
803 | must tell the rest of the code not to free up this | |
804 | information. It would be possible to instead create a table | |
805 | of changes which have to be made, as is done in coff-mips.c; | |
806 | that would be more work, but would require less memory when | |
807 | the linker is run. */ | |
808 | ||
809 | elf_section_data (sec)->relocs = internal_relocs; | |
810 | free_relocs = NULL; | |
811 | ||
812 | elf_section_data (sec)->this_hdr.contents = contents; | |
813 | free_contents = NULL; | |
814 | ||
815 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
816 | free_extsyms = NULL; | |
817 | ||
818 | /* Replace the jsr with a bsr. */ | |
819 | ||
820 | /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and | |
821 | replace the jsr with a bsr. */ | |
822 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W); | |
823 | if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info) | |
824 | { | |
825 | /* If this needs to be changed because of future relaxing, | |
826 | it will be handled here like other internal IND12W | |
827 | relocs. */ | |
828 | bfd_put_16 (abfd, | |
829 | 0xb000 | ((foff >> 1) & 0xfff), | |
830 | contents + irel->r_offset); | |
831 | } | |
832 | else | |
833 | { | |
834 | /* We can't fully resolve this yet, because the external | |
835 | symbol value may be changed by future relaxing. We let | |
836 | the final link phase handle it. */ | |
837 | bfd_put_16 (abfd, 0xb000, contents + irel->r_offset); | |
838 | } | |
839 | ||
840 | /* See if there is another R_SH_USES reloc referring to the same | |
841 | register load. */ | |
842 | for (irelscan = internal_relocs; irelscan < irelend; irelscan++) | |
843 | if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES | |
844 | && laddr == irelscan->r_offset + 4 + irelscan->r_addend) | |
845 | break; | |
846 | if (irelscan < irelend) | |
847 | { | |
848 | /* Some other function call depends upon this register load, | |
849 | and we have not yet converted that function call. | |
850 | Indeed, we may never be able to convert it. There is | |
851 | nothing else we can do at this point. */ | |
852 | continue; | |
853 | } | |
854 | ||
855 | /* Look for a R_SH_COUNT reloc on the location where the | |
856 | function address is stored. Do this before deleting any | |
857 | bytes, to avoid confusion about the address. */ | |
858 | for (irelcount = internal_relocs; irelcount < irelend; irelcount++) | |
859 | if (irelcount->r_offset == paddr | |
860 | && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT) | |
861 | break; | |
862 | ||
863 | /* Delete the register load. */ | |
864 | if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2)) | |
865 | goto error_return; | |
866 | ||
867 | /* That will change things, so, just in case it permits some | |
868 | other function call to come within range, we should relax | |
869 | again. Note that this is not required, and it may be slow. */ | |
870 | *again = true; | |
871 | ||
872 | /* Now check whether we got a COUNT reloc. */ | |
873 | if (irelcount >= irelend) | |
874 | { | |
875 | ((*_bfd_error_handler) | |
876 | (_("%s: 0x%lx: warning: could not find expected COUNT reloc"), | |
877 | bfd_get_filename (abfd), (unsigned long) paddr)); | |
878 | continue; | |
879 | } | |
880 | ||
881 | /* The number of uses is stored in the r_addend field. We've | |
882 | just deleted one. */ | |
883 | if (irelcount->r_addend == 0) | |
884 | { | |
885 | ((*_bfd_error_handler) (_("%s: 0x%lx: warning: bad count"), | |
886 | bfd_get_filename (abfd), | |
887 | (unsigned long) paddr)); | |
888 | continue; | |
889 | } | |
890 | ||
891 | --irelcount->r_addend; | |
892 | ||
893 | /* If there are no more uses, we can delete the address. Reload | |
894 | the address from irelfn, in case it was changed by the | |
895 | previous call to sh_elf_relax_delete_bytes. */ | |
896 | if (irelcount->r_addend == 0) | |
897 | { | |
898 | if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4)) | |
899 | goto error_return; | |
900 | } | |
901 | ||
902 | /* We've done all we can with that function call. */ | |
903 | } | |
904 | ||
905 | /* Look for load and store instructions that we can align on four | |
906 | byte boundaries. */ | |
907 | if (have_code) | |
908 | { | |
909 | boolean swapped; | |
910 | ||
911 | /* Get the section contents. */ | |
912 | if (contents == NULL) | |
913 | { | |
914 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
915 | contents = elf_section_data (sec)->this_hdr.contents; | |
916 | else | |
917 | { | |
918 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
919 | if (contents == NULL) | |
920 | goto error_return; | |
921 | free_contents = contents; | |
922 | ||
923 | if (! bfd_get_section_contents (abfd, sec, contents, | |
924 | (file_ptr) 0, sec->_raw_size)) | |
925 | goto error_return; | |
926 | } | |
927 | } | |
928 | ||
929 | if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents, | |
930 | &swapped)) | |
931 | goto error_return; | |
932 | ||
933 | if (swapped) | |
934 | { | |
935 | elf_section_data (sec)->relocs = internal_relocs; | |
936 | free_relocs = NULL; | |
937 | ||
938 | elf_section_data (sec)->this_hdr.contents = contents; | |
939 | free_contents = NULL; | |
940 | ||
941 | symtab_hdr->contents = (bfd_byte *) extsyms; | |
942 | free_extsyms = NULL; | |
943 | } | |
944 | } | |
945 | ||
946 | if (free_relocs != NULL) | |
947 | { | |
948 | free (free_relocs); | |
949 | free_relocs = NULL; | |
950 | } | |
951 | ||
952 | if (free_contents != NULL) | |
953 | { | |
954 | if (! link_info->keep_memory) | |
955 | free (free_contents); | |
956 | else | |
957 | { | |
958 | /* Cache the section contents for elf_link_input_bfd. */ | |
959 | elf_section_data (sec)->this_hdr.contents = contents; | |
960 | } | |
961 | free_contents = NULL; | |
962 | } | |
963 | ||
964 | if (free_extsyms != NULL) | |
965 | { | |
966 | if (! link_info->keep_memory) | |
967 | free (free_extsyms); | |
968 | else | |
969 | { | |
970 | /* Cache the symbols for elf_link_input_bfd. */ | |
971 | symtab_hdr->contents = extsyms; | |
972 | } | |
973 | free_extsyms = NULL; | |
974 | } | |
975 | ||
976 | return true; | |
977 | ||
978 | error_return: | |
979 | if (free_relocs != NULL) | |
980 | free (free_relocs); | |
981 | if (free_contents != NULL) | |
982 | free (free_contents); | |
983 | if (free_extsyms != NULL) | |
984 | free (free_extsyms); | |
985 | return false; | |
986 | } | |
987 | ||
988 | /* Delete some bytes from a section while relaxing. FIXME: There is a | |
989 | lot of duplication between this function and sh_relax_delete_bytes | |
990 | in coff-sh.c. */ | |
991 | ||
992 | static boolean | |
993 | sh_elf_relax_delete_bytes (abfd, sec, addr, count) | |
994 | bfd *abfd; | |
995 | asection *sec; | |
996 | bfd_vma addr; | |
997 | int count; | |
998 | { | |
999 | Elf_Internal_Shdr *symtab_hdr; | |
1000 | Elf32_External_Sym *extsyms; | |
1001 | int shndx, index; | |
1002 | bfd_byte *contents; | |
1003 | Elf_Internal_Rela *irel, *irelend; | |
1004 | Elf_Internal_Rela *irelalign; | |
1005 | bfd_vma toaddr; | |
1006 | Elf32_External_Sym *esym, *esymend; | |
1007 | struct elf_link_hash_entry *sym_hash; | |
1008 | asection *o; | |
1009 | ||
1010 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1011 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
1012 | ||
1013 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
1014 | ||
1015 | contents = elf_section_data (sec)->this_hdr.contents; | |
1016 | ||
1017 | /* The deletion must stop at the next ALIGN reloc for an aligment | |
1018 | power larger than the number of bytes we are deleting. */ | |
1019 | ||
1020 | irelalign = NULL; | |
1021 | toaddr = sec->_cooked_size; | |
1022 | ||
1023 | irel = elf_section_data (sec)->relocs; | |
1024 | irelend = irel + sec->reloc_count; | |
1025 | for (; irel < irelend; irel++) | |
1026 | { | |
1027 | if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN | |
1028 | && irel->r_offset > addr | |
1029 | && count < (1 << irel->r_addend)) | |
1030 | { | |
1031 | irelalign = irel; | |
1032 | toaddr = irel->r_offset; | |
1033 | break; | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | /* Actually delete the bytes. */ | |
1038 | memmove (contents + addr, contents + addr + count, toaddr - addr - count); | |
1039 | if (irelalign == NULL) | |
1040 | sec->_cooked_size -= count; | |
1041 | else | |
1042 | { | |
1043 | int i; | |
1044 | ||
1045 | #define NOP_OPCODE (0x0009) | |
1046 | ||
1047 | BFD_ASSERT ((count & 1) == 0); | |
1048 | for (i = 0; i < count; i += 2) | |
1049 | bfd_put_16 (abfd, NOP_OPCODE, contents + toaddr - count + i); | |
1050 | } | |
1051 | ||
1052 | /* Adjust all the relocs. */ | |
1053 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) | |
1054 | { | |
1055 | bfd_vma nraddr, stop; | |
1056 | bfd_vma start = 0; | |
1057 | int insn = 0; | |
1058 | Elf_Internal_Sym sym; | |
1059 | int off, adjust, oinsn; | |
1060 | bfd_signed_vma voff = 0; | |
1061 | boolean overflow; | |
1062 | ||
1063 | /* Get the new reloc address. */ | |
1064 | nraddr = irel->r_offset; | |
1065 | if ((irel->r_offset > addr | |
1066 | && irel->r_offset < toaddr) | |
1067 | || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN | |
1068 | && irel->r_offset == toaddr)) | |
1069 | nraddr -= count; | |
1070 | ||
1071 | /* See if this reloc was for the bytes we have deleted, in which | |
1072 | case we no longer care about it. Don't delete relocs which | |
1073 | represent addresses, though. */ | |
1074 | if (irel->r_offset >= addr | |
1075 | && irel->r_offset < addr + count | |
1076 | && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN | |
1077 | && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE | |
1078 | && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA | |
1079 | && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL) | |
1080 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1081 | (int) R_SH_NONE); | |
1082 | ||
1083 | /* If this is a PC relative reloc, see if the range it covers | |
1084 | includes the bytes we have deleted. */ | |
1085 | switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info)) | |
1086 | { | |
1087 | default: | |
1088 | break; | |
1089 | ||
1090 | case R_SH_DIR8WPN: | |
1091 | case R_SH_IND12W: | |
1092 | case R_SH_DIR8WPZ: | |
1093 | case R_SH_DIR8WPL: | |
1094 | start = irel->r_offset; | |
1095 | insn = bfd_get_16 (abfd, contents + nraddr); | |
1096 | break; | |
1097 | } | |
1098 | ||
1099 | switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info)) | |
1100 | { | |
1101 | default: | |
1102 | start = stop = addr; | |
1103 | break; | |
1104 | ||
1105 | case R_SH_DIR32: | |
1106 | /* If this reloc is against a symbol defined in this | |
1107 | section, and the symbol will not be adjusted below, we | |
1108 | must check the addend to see it will put the value in | |
1109 | range to be adjusted, and hence must be changed. */ | |
1110 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
1111 | { | |
1112 | bfd_elf32_swap_symbol_in (abfd, | |
1113 | extsyms + ELF32_R_SYM (irel->r_info), | |
1114 | &sym); | |
1115 | if (sym.st_shndx == shndx | |
1116 | && (sym.st_value <= addr | |
1117 | || sym.st_value >= toaddr)) | |
1118 | { | |
1119 | bfd_vma val; | |
1120 | ||
1121 | val = bfd_get_32 (abfd, contents + nraddr); | |
1122 | val += sym.st_value; | |
1123 | if (val > addr && val < toaddr) | |
1124 | bfd_put_32 (abfd, val - count, contents + nraddr); | |
1125 | } | |
1126 | } | |
1127 | start = stop = addr; | |
1128 | break; | |
1129 | ||
1130 | case R_SH_DIR8WPN: | |
1131 | off = insn & 0xff; | |
1132 | if (off & 0x80) | |
1133 | off -= 0x100; | |
1134 | stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2); | |
1135 | break; | |
1136 | ||
1137 | case R_SH_IND12W: | |
1138 | if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info) | |
1139 | start = stop = addr; | |
1140 | else | |
1141 | { | |
1142 | off = insn & 0xfff; | |
1143 | if (off & 0x800) | |
1144 | off -= 0x1000; | |
1145 | stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2); | |
1146 | } | |
1147 | break; | |
1148 | ||
1149 | case R_SH_DIR8WPZ: | |
1150 | off = insn & 0xff; | |
1151 | stop = start + 4 + off * 2; | |
1152 | break; | |
1153 | ||
1154 | case R_SH_DIR8WPL: | |
1155 | off = insn & 0xff; | |
1156 | stop = (start &~ (bfd_vma) 3) + 4 + off * 4; | |
1157 | break; | |
1158 | ||
1159 | case R_SH_SWITCH8: | |
1160 | case R_SH_SWITCH16: | |
1161 | case R_SH_SWITCH32: | |
1162 | /* These relocs types represent | |
1163 | .word L2-L1 | |
1164 | The r_offset field holds the difference between the reloc | |
1165 | address and L1. That is the start of the reloc, and | |
1166 | adding in the contents gives us the top. We must adjust | |
1167 | both the r_offset field and the section contents. */ | |
1168 | ||
1169 | start = irel->r_offset; | |
1170 | stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_addend); | |
1171 | ||
1172 | if (start > addr | |
1173 | && start < toaddr | |
1174 | && (stop <= addr || stop >= toaddr)) | |
1175 | irel->r_addend += count; | |
1176 | else if (stop > addr | |
1177 | && stop < toaddr | |
1178 | && (start <= addr || start >= toaddr)) | |
1179 | irel->r_addend -= count; | |
1180 | ||
1181 | start = stop; | |
1182 | ||
1183 | if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16) | |
1184 | voff = bfd_get_signed_16 (abfd, contents + nraddr); | |
1185 | else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8) | |
1186 | voff = bfd_get_8 (abfd, contents + nraddr); | |
1187 | else | |
1188 | voff = bfd_get_signed_32 (abfd, contents + nraddr); | |
1189 | stop = (bfd_vma) ((bfd_signed_vma) start + voff); | |
1190 | ||
1191 | break; | |
1192 | ||
1193 | case R_SH_USES: | |
1194 | start = irel->r_offset; | |
1195 | stop = (bfd_vma) ((bfd_signed_vma) start | |
1196 | + (long) irel->r_addend | |
1197 | + 4); | |
1198 | break; | |
1199 | } | |
1200 | ||
1201 | if (start > addr | |
1202 | && start < toaddr | |
1203 | && (stop <= addr || stop >= toaddr)) | |
1204 | adjust = count; | |
1205 | else if (stop > addr | |
1206 | && stop < toaddr | |
1207 | && (start <= addr || start >= toaddr)) | |
1208 | adjust = - count; | |
1209 | else | |
1210 | adjust = 0; | |
1211 | ||
1212 | if (adjust != 0) | |
1213 | { | |
1214 | oinsn = insn; | |
1215 | overflow = false; | |
1216 | switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info)) | |
1217 | { | |
1218 | default: | |
1219 | abort (); | |
1220 | break; | |
1221 | ||
1222 | case R_SH_DIR8WPN: | |
1223 | case R_SH_DIR8WPZ: | |
1224 | insn += adjust / 2; | |
1225 | if ((oinsn & 0xff00) != (insn & 0xff00)) | |
1226 | overflow = true; | |
1227 | bfd_put_16 (abfd, insn, contents + nraddr); | |
1228 | break; | |
1229 | ||
1230 | case R_SH_IND12W: | |
1231 | insn += adjust / 2; | |
1232 | if ((oinsn & 0xf000) != (insn & 0xf000)) | |
1233 | overflow = true; | |
1234 | bfd_put_16 (abfd, insn, contents + nraddr); | |
1235 | break; | |
1236 | ||
1237 | case R_SH_DIR8WPL: | |
1238 | BFD_ASSERT (adjust == count || count >= 4); | |
1239 | if (count >= 4) | |
1240 | insn += adjust / 4; | |
1241 | else | |
1242 | { | |
1243 | if ((irel->r_offset & 3) == 0) | |
1244 | ++insn; | |
1245 | } | |
1246 | if ((oinsn & 0xff00) != (insn & 0xff00)) | |
1247 | overflow = true; | |
1248 | bfd_put_16 (abfd, insn, contents + nraddr); | |
1249 | break; | |
1250 | ||
1251 | case R_SH_SWITCH16: | |
1252 | voff += adjust; | |
1253 | if (voff < - 0x8000 || voff >= 0x8000) | |
1254 | overflow = true; | |
1255 | bfd_put_signed_16 (abfd, voff, contents + nraddr); | |
1256 | break; | |
1257 | ||
1258 | case R_SH_SWITCH32: | |
1259 | voff += adjust; | |
1260 | bfd_put_signed_32 (abfd, voff, contents + nraddr); | |
1261 | break; | |
1262 | ||
1263 | case R_SH_USES: | |
1264 | irel->r_addend += adjust; | |
1265 | break; | |
1266 | } | |
1267 | ||
1268 | if (overflow) | |
1269 | { | |
1270 | ((*_bfd_error_handler) | |
1271 | (_("%s: 0x%lx: fatal: reloc overflow while relaxing"), | |
1272 | bfd_get_filename (abfd), (unsigned long) irel->r_offset)); | |
1273 | bfd_set_error (bfd_error_bad_value); | |
1274 | return false; | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | irel->r_offset = nraddr; | |
1279 | } | |
1280 | ||
1281 | /* Look through all the other sections. If there contain any IMM32 | |
1282 | relocs against internal symbols which we are not going to adjust | |
1283 | below, we may need to adjust the addends. */ | |
1284 | for (o = abfd->sections; o != NULL; o = o->next) | |
1285 | { | |
1286 | Elf_Internal_Rela *internal_relocs; | |
1287 | Elf_Internal_Rela *irelscan, *irelscanend; | |
1288 | bfd_byte *ocontents; | |
1289 | ||
1290 | if (o == sec | |
1291 | || (o->flags & SEC_RELOC) == 0 | |
1292 | || o->reloc_count == 0) | |
1293 | continue; | |
1294 | ||
1295 | /* We always cache the relocs. Perhaps, if info->keep_memory is | |
1296 | false, we should free them, if we are permitted to, when we | |
1297 | leave sh_coff_relax_section. */ | |
1298 | internal_relocs = (_bfd_elf32_link_read_relocs | |
1299 | (abfd, o, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
1300 | true)); | |
1301 | if (internal_relocs == NULL) | |
1302 | return false; | |
1303 | ||
1304 | ocontents = NULL; | |
1305 | irelscanend = internal_relocs + o->reloc_count; | |
1306 | for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++) | |
1307 | { | |
1308 | Elf_Internal_Sym sym; | |
1309 | ||
1310 | if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32) | |
1311 | continue; | |
1312 | ||
1313 | if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info) | |
1314 | continue; | |
1315 | ||
1316 | bfd_elf32_swap_symbol_in (abfd, | |
1317 | extsyms + ELF32_R_SYM (irelscan->r_info), | |
1318 | &sym); | |
1319 | ||
1320 | if (sym.st_shndx == shndx | |
1321 | && (sym.st_value <= addr | |
1322 | || sym.st_value >= toaddr)) | |
1323 | { | |
1324 | bfd_vma val; | |
1325 | ||
1326 | if (ocontents == NULL) | |
1327 | { | |
1328 | if (elf_section_data (o)->this_hdr.contents != NULL) | |
1329 | ocontents = elf_section_data (o)->this_hdr.contents; | |
1330 | else | |
1331 | { | |
1332 | /* We always cache the section contents. | |
1333 | Perhaps, if info->keep_memory is false, we | |
1334 | should free them, if we are permitted to, | |
1335 | when we leave sh_coff_relax_section. */ | |
1336 | ocontents = (bfd_byte *) bfd_malloc (o->_raw_size); | |
1337 | if (ocontents == NULL) | |
1338 | return false; | |
1339 | if (! bfd_get_section_contents (abfd, o, ocontents, | |
1340 | (file_ptr) 0, | |
1341 | o->_raw_size)) | |
1342 | return false; | |
1343 | elf_section_data (o)->this_hdr.contents = ocontents; | |
1344 | } | |
1345 | } | |
1346 | ||
1347 | val = bfd_get_32 (abfd, ocontents + irelscan->r_offset); | |
1348 | val += sym.st_value; | |
1349 | if (val > addr && val < toaddr) | |
1350 | bfd_put_32 (abfd, val - count, | |
1351 | ocontents + irelscan->r_offset); | |
1352 | } | |
1353 | } | |
1354 | } | |
1355 | ||
1356 | /* Adjust the local symbols defined in this section. */ | |
1357 | esym = extsyms; | |
1358 | esymend = esym + symtab_hdr->sh_info; | |
1359 | for (; esym < esymend; esym++) | |
1360 | { | |
1361 | Elf_Internal_Sym isym; | |
1362 | ||
1363 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); | |
1364 | ||
1365 | if (isym.st_shndx == shndx | |
1366 | && isym.st_value > addr | |
1367 | && isym.st_value < toaddr) | |
1368 | { | |
1369 | isym.st_value -= count; | |
1370 | bfd_elf32_swap_symbol_out (abfd, &isym, esym); | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | /* Now adjust the global symbols defined in this section. */ | |
1375 | esym = extsyms + symtab_hdr->sh_info; | |
1376 | esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)); | |
1377 | for (index = 0; esym < esymend; esym++, index++) | |
1378 | { | |
1379 | Elf_Internal_Sym isym; | |
1380 | ||
1381 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); | |
1382 | sym_hash = elf_sym_hashes (abfd)[index]; | |
1383 | if (isym.st_shndx == shndx | |
1384 | && ((sym_hash)->root.type == bfd_link_hash_defined | |
1385 | || (sym_hash)->root.type == bfd_link_hash_defweak) | |
1386 | && (sym_hash)->root.u.def.section == sec | |
1387 | && (sym_hash)->root.u.def.value > addr | |
1388 | && (sym_hash)->root.u.def.value < toaddr) | |
1389 | { | |
1390 | (sym_hash)->root.u.def.value -= count; | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | /* See if we can move the ALIGN reloc forward. We have adjusted | |
1395 | r_offset for it already. */ | |
1396 | if (irelalign != NULL) | |
1397 | { | |
1398 | bfd_vma alignto, alignaddr; | |
1399 | ||
1400 | alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend); | |
1401 | alignaddr = BFD_ALIGN (irelalign->r_offset, | |
1402 | 1 << irelalign->r_addend); | |
1403 | if (alignto != alignaddr) | |
1404 | { | |
1405 | /* Tail recursion. */ | |
1406 | return sh_elf_relax_delete_bytes (abfd, sec, alignaddr, | |
1407 | alignto - alignaddr); | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | return true; | |
1412 | } | |
1413 | ||
1414 | /* Look for loads and stores which we can align to four byte | |
1415 | boundaries. This is like sh_align_loads in coff-sh.c. */ | |
1416 | ||
1417 | static boolean | |
1418 | sh_elf_align_loads (abfd, sec, internal_relocs, contents, pswapped) | |
1419 | bfd *abfd; | |
1420 | asection *sec; | |
1421 | Elf_Internal_Rela *internal_relocs; | |
1422 | bfd_byte *contents; | |
1423 | boolean *pswapped; | |
1424 | { | |
1425 | Elf_Internal_Rela *irel, *irelend; | |
1426 | bfd_vma *labels = NULL; | |
1427 | bfd_vma *label, *label_end; | |
1428 | ||
1429 | *pswapped = false; | |
1430 | ||
1431 | irelend = internal_relocs + sec->reloc_count; | |
1432 | ||
1433 | /* Get all the addresses with labels on them. */ | |
1434 | labels = (bfd_vma *) bfd_malloc (sec->reloc_count * sizeof (bfd_vma)); | |
1435 | if (labels == NULL) | |
1436 | goto error_return; | |
1437 | label_end = labels; | |
1438 | for (irel = internal_relocs; irel < irelend; irel++) | |
1439 | { | |
1440 | if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL) | |
1441 | { | |
1442 | *label_end = irel->r_offset; | |
1443 | ++label_end; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | /* Note that the assembler currently always outputs relocs in | |
1448 | address order. If that ever changes, this code will need to sort | |
1449 | the label values and the relocs. */ | |
1450 | ||
1451 | label = labels; | |
1452 | ||
1453 | for (irel = internal_relocs; irel < irelend; irel++) | |
1454 | { | |
1455 | bfd_vma start, stop; | |
1456 | ||
1457 | if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE) | |
1458 | continue; | |
1459 | ||
1460 | start = irel->r_offset; | |
1461 | ||
1462 | for (irel++; irel < irelend; irel++) | |
1463 | if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA) | |
1464 | break; | |
1465 | if (irel < irelend) | |
1466 | stop = irel->r_offset; | |
1467 | else | |
1468 | stop = sec->_cooked_size; | |
1469 | ||
1470 | if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns, | |
1471 | (PTR) internal_relocs, &label, | |
1472 | label_end, start, stop, pswapped)) | |
1473 | goto error_return; | |
1474 | } | |
1475 | ||
1476 | free (labels); | |
1477 | ||
1478 | return true; | |
1479 | ||
1480 | error_return: | |
1481 | if (labels != NULL) | |
1482 | free (labels); | |
1483 | return false; | |
1484 | } | |
1485 | ||
1486 | /* Swap two SH instructions. This is like sh_swap_insns in coff-sh.c. */ | |
1487 | ||
1488 | static boolean | |
1489 | sh_elf_swap_insns (abfd, sec, relocs, contents, addr) | |
1490 | bfd *abfd; | |
1491 | asection *sec; | |
1492 | PTR relocs; | |
1493 | bfd_byte *contents; | |
1494 | bfd_vma addr; | |
1495 | { | |
1496 | Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs; | |
1497 | unsigned short i1, i2; | |
1498 | Elf_Internal_Rela *irel, *irelend; | |
1499 | ||
1500 | /* Swap the instructions themselves. */ | |
1501 | i1 = bfd_get_16 (abfd, contents + addr); | |
1502 | i2 = bfd_get_16 (abfd, contents + addr + 2); | |
1503 | bfd_put_16 (abfd, i2, contents + addr); | |
1504 | bfd_put_16 (abfd, i1, contents + addr + 2); | |
1505 | ||
1506 | /* Adjust all reloc addresses. */ | |
1507 | irelend = internal_relocs + sec->reloc_count; | |
1508 | for (irel = internal_relocs; irel < irelend; irel++) | |
1509 | { | |
1510 | enum elf_sh_reloc_type type; | |
1511 | int add; | |
1512 | ||
1513 | /* There are a few special types of relocs that we don't want to | |
1514 | adjust. These relocs do not apply to the instruction itself, | |
1515 | but are only associated with the address. */ | |
1516 | type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info); | |
1517 | if (type == R_SH_ALIGN | |
1518 | || type == R_SH_CODE | |
1519 | || type == R_SH_DATA | |
1520 | || type == R_SH_LABEL) | |
1521 | continue; | |
1522 | ||
1523 | /* If an R_SH_USES reloc points to one of the addresses being | |
1524 | swapped, we must adjust it. It would be incorrect to do this | |
1525 | for a jump, though, since we want to execute both | |
1526 | instructions after the jump. (We have avoided swapping | |
1527 | around a label, so the jump will not wind up executing an | |
1528 | instruction it shouldn't). */ | |
1529 | if (type == R_SH_USES) | |
1530 | { | |
1531 | bfd_vma off; | |
1532 | ||
1533 | off = irel->r_offset + 4 + irel->r_addend; | |
1534 | if (off == addr) | |
1535 | irel->r_offset += 2; | |
1536 | else if (off == addr + 2) | |
1537 | irel->r_offset -= 2; | |
1538 | } | |
1539 | ||
1540 | if (irel->r_offset == addr) | |
1541 | { | |
1542 | irel->r_offset += 2; | |
1543 | add = -2; | |
1544 | } | |
1545 | else if (irel->r_offset == addr + 2) | |
1546 | { | |
1547 | irel->r_offset -= 2; | |
1548 | add = 2; | |
1549 | } | |
1550 | else | |
1551 | add = 0; | |
1552 | ||
1553 | if (add != 0) | |
1554 | { | |
1555 | bfd_byte *loc; | |
1556 | unsigned short insn, oinsn; | |
1557 | boolean overflow; | |
1558 | ||
1559 | loc = contents + irel->r_offset; | |
1560 | overflow = false; | |
1561 | switch (type) | |
1562 | { | |
1563 | default: | |
1564 | break; | |
1565 | ||
1566 | case R_SH_DIR8WPN: | |
1567 | case R_SH_DIR8WPZ: | |
1568 | insn = bfd_get_16 (abfd, loc); | |
1569 | oinsn = insn; | |
1570 | insn += add / 2; | |
1571 | if ((oinsn & 0xff00) != (insn & 0xff00)) | |
1572 | overflow = true; | |
1573 | bfd_put_16 (abfd, insn, loc); | |
1574 | break; | |
1575 | ||
1576 | case R_SH_IND12W: | |
1577 | insn = bfd_get_16 (abfd, loc); | |
1578 | oinsn = insn; | |
1579 | insn += add / 2; | |
1580 | if ((oinsn & 0xf000) != (insn & 0xf000)) | |
1581 | overflow = true; | |
1582 | bfd_put_16 (abfd, insn, loc); | |
1583 | break; | |
1584 | ||
1585 | case R_SH_DIR8WPL: | |
1586 | /* This reloc ignores the least significant 3 bits of | |
1587 | the program counter before adding in the offset. | |
1588 | This means that if ADDR is at an even address, the | |
1589 | swap will not affect the offset. If ADDR is an at an | |
1590 | odd address, then the instruction will be crossing a | |
1591 | four byte boundary, and must be adjusted. */ | |
1592 | if ((addr & 3) != 0) | |
1593 | { | |
1594 | insn = bfd_get_16 (abfd, loc); | |
1595 | oinsn = insn; | |
1596 | insn += add / 2; | |
1597 | if ((oinsn & 0xff00) != (insn & 0xff00)) | |
1598 | overflow = true; | |
1599 | bfd_put_16 (abfd, insn, loc); | |
1600 | } | |
1601 | ||
1602 | break; | |
1603 | } | |
1604 | ||
1605 | if (overflow) | |
1606 | { | |
1607 | ((*_bfd_error_handler) | |
1608 | (_("%s: 0x%lx: fatal: reloc overflow while relaxing"), | |
1609 | bfd_get_filename (abfd), (unsigned long) irel->r_offset)); | |
1610 | bfd_set_error (bfd_error_bad_value); | |
1611 | return false; | |
1612 | } | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | return true; | |
1617 | } | |
1618 | \f | |
1619 | /* Relocate an SH ELF section. */ | |
1620 | ||
1621 | static boolean | |
1622 | sh_elf_relocate_section (output_bfd, info, input_bfd, input_section, | |
1623 | contents, relocs, local_syms, local_sections) | |
1624 | bfd *output_bfd; | |
1625 | struct bfd_link_info *info; | |
1626 | bfd *input_bfd; | |
1627 | asection *input_section; | |
1628 | bfd_byte *contents; | |
1629 | Elf_Internal_Rela *relocs; | |
1630 | Elf_Internal_Sym *local_syms; | |
1631 | asection **local_sections; | |
1632 | { | |
1633 | Elf_Internal_Shdr *symtab_hdr; | |
1634 | struct elf_link_hash_entry **sym_hashes; | |
1635 | Elf_Internal_Rela *rel, *relend; | |
1636 | ||
1637 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1638 | sym_hashes = elf_sym_hashes (input_bfd); | |
1639 | ||
1640 | rel = relocs; | |
1641 | relend = relocs + input_section->reloc_count; | |
1642 | for (; rel < relend; rel++) | |
1643 | { | |
1644 | int r_type; | |
1645 | reloc_howto_type *howto; | |
1646 | unsigned long r_symndx; | |
1647 | Elf_Internal_Sym *sym; | |
1648 | asection *sec; | |
1649 | struct elf_link_hash_entry *h; | |
1650 | bfd_vma relocation; | |
1651 | bfd_vma addend = (bfd_vma)0; | |
1652 | bfd_reloc_status_type r; | |
1653 | ||
1654 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1655 | ||
1656 | if (info->relocateable) | |
1657 | { | |
1658 | /* This is a relocateable link. We don't have to change | |
1659 | anything, unless the reloc is against a section symbol, | |
1660 | in which case we have to adjust according to where the | |
1661 | section symbol winds up in the output section. */ | |
1662 | if (r_symndx < symtab_hdr->sh_info) | |
1663 | { | |
1664 | sym = local_syms + r_symndx; | |
1665 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
1666 | { | |
1667 | sec = local_sections[r_symndx]; | |
1668 | rel->r_addend += sec->output_offset + sym->st_value; | |
1669 | } | |
1670 | } | |
1671 | ||
1672 | continue; | |
1673 | } | |
1674 | ||
1675 | r_type = ELF32_R_TYPE (rel->r_info); | |
1676 | ||
1677 | /* Many of the relocs are only used for relaxing, and are | |
1678 | handled entirely by the relaxation code. */ | |
1679 | if (r_type > (int) R_SH_LAST_INVALID_RELOC) | |
1680 | continue; | |
1681 | ||
1682 | if (r_type < 0 | |
1683 | || r_type >= (int) R_SH_FIRST_INVALID_RELOC) | |
1684 | { | |
1685 | bfd_set_error (bfd_error_bad_value); | |
1686 | return false; | |
1687 | } | |
1688 | ||
1689 | /* FIXME: This is certainly incorrect. However, it is how the | |
1690 | COFF linker works. */ | |
1691 | if (r_type != (int) R_SH_DIR32 | |
1692 | && r_type != (int) R_SH_IND12W) | |
1693 | continue; | |
1694 | ||
1695 | howto = sh_elf_howto_table + r_type; | |
1696 | ||
1697 | /* This is a final link. */ | |
1698 | h = NULL; | |
1699 | sym = NULL; | |
1700 | sec = NULL; | |
1701 | if (r_symndx < symtab_hdr->sh_info) | |
1702 | { | |
1703 | /* There is nothing to be done for an internal IND12W | |
1704 | relocation. FIXME: This is probably wrong, but it's how | |
1705 | the COFF relocations work. */ | |
1706 | if (r_type == (int) R_SH_IND12W) | |
1707 | continue; | |
1708 | sym = local_syms + r_symndx; | |
1709 | sec = local_sections[r_symndx]; | |
1710 | relocation = (sec->output_section->vma | |
1711 | + sec->output_offset | |
1712 | + sym->st_value); | |
1713 | } | |
1714 | else | |
1715 | { | |
1716 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1717 | while (h->root.type == bfd_link_hash_indirect | |
1718 | || h->root.type == bfd_link_hash_warning) | |
1719 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1720 | if (h->root.type == bfd_link_hash_defined | |
1721 | || h->root.type == bfd_link_hash_defweak) | |
1722 | { | |
1723 | sec = h->root.u.def.section; | |
1724 | relocation = (h->root.u.def.value | |
1725 | + sec->output_section->vma | |
1726 | + sec->output_offset); | |
1727 | } | |
1728 | else if (h->root.type == bfd_link_hash_undefweak) | |
1729 | relocation = 0; | |
1730 | else | |
1731 | { | |
1732 | if (! ((*info->callbacks->undefined_symbol) | |
1733 | (info, h->root.root.string, input_bfd, | |
1734 | input_section, rel->r_offset))) | |
1735 | return false; | |
1736 | relocation = 0; | |
1737 | } | |
1738 | } | |
1739 | ||
1740 | /* FIXME: This is how the COFF relocations work. */ | |
1741 | if (r_type == (int) R_SH_IND12W) | |
1742 | relocation -= 4; | |
1743 | ||
1744 | switch ((int)r_type) | |
1745 | { | |
1746 | case (int)R_SH_DIR32: | |
1747 | addend = rel->r_addend; | |
1748 | break; | |
1749 | } | |
1750 | ||
1751 | /* COFF relocs don't use the addend. The addend is used for R_SH_DIR32 | |
1752 | to be compatible with other compilers. */ | |
1753 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
1754 | contents, rel->r_offset, | |
1755 | relocation, addend); | |
1756 | ||
1757 | if (r != bfd_reloc_ok) | |
1758 | { | |
1759 | switch (r) | |
1760 | { | |
1761 | default: | |
1762 | case bfd_reloc_outofrange: | |
1763 | abort (); | |
1764 | case bfd_reloc_overflow: | |
1765 | { | |
1766 | const char *name; | |
1767 | ||
1768 | if (h != NULL) | |
1769 | name = h->root.root.string; | |
1770 | else | |
1771 | { | |
1772 | name = (bfd_elf_string_from_elf_section | |
1773 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); | |
1774 | if (name == NULL) | |
1775 | return false; | |
1776 | if (*name == '\0') | |
1777 | name = bfd_section_name (input_bfd, sec); | |
1778 | } | |
1779 | if (! ((*info->callbacks->reloc_overflow) | |
1780 | (info, name, howto->name, (bfd_vma) 0, | |
1781 | input_bfd, input_section, rel->r_offset))) | |
1782 | return false; | |
1783 | } | |
1784 | break; | |
1785 | } | |
1786 | } | |
1787 | } | |
1788 | ||
1789 | return true; | |
1790 | } | |
1791 | ||
1792 | /* This is a version of bfd_generic_get_relocated_section_contents | |
1793 | which uses sh_elf_relocate_section. */ | |
1794 | ||
1795 | static bfd_byte * | |
1796 | sh_elf_get_relocated_section_contents (output_bfd, link_info, link_order, | |
1797 | data, relocateable, symbols) | |
1798 | bfd *output_bfd; | |
1799 | struct bfd_link_info *link_info; | |
1800 | struct bfd_link_order *link_order; | |
1801 | bfd_byte *data; | |
1802 | boolean relocateable; | |
1803 | asymbol **symbols; | |
1804 | { | |
1805 | Elf_Internal_Shdr *symtab_hdr; | |
1806 | asection *input_section = link_order->u.indirect.section; | |
1807 | bfd *input_bfd = input_section->owner; | |
1808 | asection **sections = NULL; | |
1809 | Elf_Internal_Rela *internal_relocs = NULL; | |
1810 | Elf32_External_Sym *external_syms = NULL; | |
1811 | Elf_Internal_Sym *internal_syms = NULL; | |
1812 | ||
1813 | /* We only need to handle the case of relaxing, or of having a | |
1814 | particular set of section contents, specially. */ | |
1815 | if (relocateable | |
1816 | || elf_section_data (input_section)->this_hdr.contents == NULL) | |
1817 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, | |
1818 | link_order, data, | |
1819 | relocateable, | |
1820 | symbols); | |
1821 | ||
1822 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1823 | ||
1824 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, | |
1825 | input_section->_raw_size); | |
1826 | ||
1827 | if ((input_section->flags & SEC_RELOC) != 0 | |
1828 | && input_section->reloc_count > 0) | |
1829 | { | |
1830 | Elf_Internal_Sym *isymp; | |
1831 | asection **secpp; | |
1832 | Elf32_External_Sym *esym, *esymend; | |
1833 | ||
1834 | if (symtab_hdr->contents != NULL) | |
1835 | external_syms = (Elf32_External_Sym *) symtab_hdr->contents; | |
1836 | else | |
1837 | { | |
1838 | external_syms = ((Elf32_External_Sym *) | |
1839 | bfd_malloc (symtab_hdr->sh_info | |
1840 | * sizeof (Elf32_External_Sym))); | |
1841 | if (external_syms == NULL && symtab_hdr->sh_info > 0) | |
1842 | goto error_return; | |
1843 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
1844 | || (bfd_read (external_syms, sizeof (Elf32_External_Sym), | |
1845 | symtab_hdr->sh_info, input_bfd) | |
1846 | != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)))) | |
1847 | goto error_return; | |
1848 | } | |
1849 | ||
1850 | internal_relocs = (_bfd_elf32_link_read_relocs | |
1851 | (input_bfd, input_section, (PTR) NULL, | |
1852 | (Elf_Internal_Rela *) NULL, false)); | |
1853 | if (internal_relocs == NULL) | |
1854 | goto error_return; | |
1855 | ||
1856 | internal_syms = ((Elf_Internal_Sym *) | |
1857 | bfd_malloc (symtab_hdr->sh_info | |
1858 | * sizeof (Elf_Internal_Sym))); | |
1859 | if (internal_syms == NULL && symtab_hdr->sh_info > 0) | |
1860 | goto error_return; | |
1861 | ||
1862 | sections = (asection **) bfd_malloc (symtab_hdr->sh_info | |
1863 | * sizeof (asection *)); | |
1864 | if (sections == NULL && symtab_hdr->sh_info > 0) | |
1865 | goto error_return; | |
1866 | ||
1867 | isymp = internal_syms; | |
1868 | secpp = sections; | |
1869 | esym = external_syms; | |
1870 | esymend = esym + symtab_hdr->sh_info; | |
1871 | for (; esym < esymend; ++esym, ++isymp, ++secpp) | |
1872 | { | |
1873 | asection *isec; | |
1874 | ||
1875 | bfd_elf32_swap_symbol_in (input_bfd, esym, isymp); | |
1876 | ||
1877 | if (isymp->st_shndx == SHN_UNDEF) | |
1878 | isec = bfd_und_section_ptr; | |
1879 | else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE) | |
1880 | isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx); | |
1881 | else if (isymp->st_shndx == SHN_ABS) | |
1882 | isec = bfd_abs_section_ptr; | |
1883 | else if (isymp->st_shndx == SHN_COMMON) | |
1884 | isec = bfd_com_section_ptr; | |
1885 | else | |
1886 | { | |
1887 | /* Who knows? */ | |
1888 | isec = NULL; | |
1889 | } | |
1890 | ||
1891 | *secpp = isec; | |
1892 | } | |
1893 | ||
1894 | if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd, | |
1895 | input_section, data, internal_relocs, | |
1896 | internal_syms, sections)) | |
1897 | goto error_return; | |
1898 | ||
1899 | if (sections != NULL) | |
1900 | free (sections); | |
1901 | sections = NULL; | |
1902 | if (internal_syms != NULL) | |
1903 | free (internal_syms); | |
1904 | internal_syms = NULL; | |
1905 | if (external_syms != NULL && symtab_hdr->contents == NULL) | |
1906 | free (external_syms); | |
1907 | external_syms = NULL; | |
1908 | if (internal_relocs != elf_section_data (input_section)->relocs) | |
1909 | free (internal_relocs); | |
1910 | internal_relocs = NULL; | |
1911 | } | |
1912 | ||
1913 | return data; | |
1914 | ||
1915 | error_return: | |
1916 | if (internal_relocs != NULL | |
1917 | && internal_relocs != elf_section_data (input_section)->relocs) | |
1918 | free (internal_relocs); | |
1919 | if (external_syms != NULL && symtab_hdr->contents == NULL) | |
1920 | free (external_syms); | |
1921 | if (internal_syms != NULL) | |
1922 | free (internal_syms); | |
1923 | if (sections != NULL) | |
1924 | free (sections); | |
1925 | return NULL; | |
1926 | } | |
1927 | static asection * | |
1928 | sh_elf_gc_mark_hook (abfd, info, rel, h, sym) | |
1929 | bfd *abfd; | |
1930 | struct bfd_link_info *info; | |
1931 | Elf_Internal_Rela *rel; | |
1932 | struct elf_link_hash_entry *h; | |
1933 | Elf_Internal_Sym *sym; | |
1934 | { | |
1935 | if (h != NULL) | |
1936 | { | |
1937 | switch (ELF32_R_TYPE (rel->r_info)) | |
1938 | { | |
1939 | case R_SH_GNU_VTINHERIT: | |
1940 | case R_SH_GNU_VTENTRY: | |
1941 | break; | |
1942 | ||
1943 | default: | |
1944 | switch (h->root.type) | |
1945 | { | |
1946 | case bfd_link_hash_defined: | |
1947 | case bfd_link_hash_defweak: | |
1948 | return h->root.u.def.section; | |
1949 | ||
1950 | case bfd_link_hash_common: | |
1951 | return h->root.u.c.p->section; | |
1952 | } | |
1953 | } | |
1954 | } | |
1955 | else | |
1956 | { | |
1957 | if (!(elf_bad_symtab (abfd) | |
1958 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
1959 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) | |
1960 | && sym->st_shndx != SHN_COMMON)) | |
1961 | { | |
1962 | return bfd_section_from_elf_index (abfd, sym->st_shndx); | |
1963 | } | |
1964 | } | |
1965 | return NULL; | |
1966 | } | |
1967 | ||
1968 | static boolean | |
1969 | sh_elf_gc_sweep_hook (abfd, info, sec, relocs) | |
1970 | bfd *abfd; | |
1971 | struct bfd_link_info *info; | |
1972 | asection *sec; | |
1973 | const Elf_Internal_Rela *relocs; | |
1974 | { | |
1975 | /* we don't use got and plt entries for sh. */ | |
1976 | return true; | |
1977 | } | |
1978 | ||
1979 | /* Look through the relocs for a section during the first phase. | |
1980 | Since we don't do .gots or .plts, we just need to consider the | |
1981 | virtual table relocs for gc. */ | |
1982 | ||
1983 | static boolean | |
1984 | sh_elf_check_relocs (abfd, info, sec, relocs) | |
1985 | bfd *abfd; | |
1986 | struct bfd_link_info *info; | |
1987 | asection *sec; | |
1988 | const Elf_Internal_Rela *relocs; | |
1989 | { | |
1990 | Elf_Internal_Shdr *symtab_hdr; | |
1991 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
1992 | const Elf_Internal_Rela *rel; | |
1993 | const Elf_Internal_Rela *rel_end; | |
1994 | ||
1995 | if (info->relocateable) | |
1996 | return true; | |
1997 | ||
1998 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1999 | sym_hashes = elf_sym_hashes (abfd); | |
2000 | sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym); | |
2001 | if (!elf_bad_symtab (abfd)) | |
2002 | sym_hashes_end -= symtab_hdr->sh_info; | |
2003 | ||
2004 | rel_end = relocs + sec->reloc_count; | |
2005 | for (rel = relocs; rel < rel_end; rel++) | |
2006 | { | |
2007 | struct elf_link_hash_entry *h; | |
2008 | unsigned long r_symndx; | |
2009 | ||
2010 | r_symndx = ELF32_R_SYM (rel->r_info); | |
2011 | if (r_symndx < symtab_hdr->sh_info) | |
2012 | h = NULL; | |
2013 | else | |
2014 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
2015 | ||
2016 | switch (ELF32_R_TYPE (rel->r_info)) | |
2017 | { | |
2018 | /* This relocation describes the C++ object vtable hierarchy. | |
2019 | Reconstruct it for later use during GC. */ | |
2020 | case R_SH_GNU_VTINHERIT: | |
2021 | if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
2022 | return false; | |
2023 | break; | |
2024 | ||
2025 | /* This relocation describes which C++ vtable entries are actually | |
2026 | used. Record for later use during GC. */ | |
2027 | case R_SH_GNU_VTENTRY: | |
2028 | if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend)) | |
2029 | return false; | |
2030 | break; | |
2031 | } | |
2032 | } | |
2033 | ||
2034 | return true; | |
2035 | } | |
2036 | ||
2037 | #define TARGET_BIG_SYM bfd_elf32_sh_vec | |
2038 | #define TARGET_BIG_NAME "elf32-sh" | |
2039 | #define TARGET_LITTLE_SYM bfd_elf32_shl_vec | |
2040 | #define TARGET_LITTLE_NAME "elf32-shl" | |
2041 | #define ELF_ARCH bfd_arch_sh | |
2042 | #define ELF_MACHINE_CODE EM_SH | |
2043 | #define ELF_MAXPAGESIZE 0x1 | |
2044 | ||
2045 | #define elf_symbol_leading_char '_' | |
2046 | ||
2047 | #define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup | |
2048 | #define elf_info_to_howto sh_elf_info_to_howto | |
2049 | #define bfd_elf32_bfd_relax_section sh_elf_relax_section | |
2050 | #define elf_backend_relocate_section sh_elf_relocate_section | |
2051 | #define bfd_elf32_bfd_get_relocated_section_contents \ | |
2052 | sh_elf_get_relocated_section_contents | |
2053 | ||
2054 | #define elf_backend_gc_mark_hook sh_elf_gc_mark_hook | |
2055 | #define elf_backend_gc_sweep_hook sh_elf_gc_sweep_hook | |
2056 | #define elf_backend_check_relocs sh_elf_check_relocs | |
2057 | ||
2058 | #define elf_backend_can_gc_sections 1 | |
2059 | #include "elf32-target.h" |