]>
Commit | Line | Data |
---|---|---|
1 | /* BFD back-end for Renesas H8/300 ELF binaries. | |
2 | Copyright 1993, 1995, 1998, 1999, 2001, 2002, 2003 | |
3 | Free Software Foundation, Inc. | |
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 "libbfd.h" | |
24 | #include "elf-bfd.h" | |
25 | #include "elf/h8.h" | |
26 | ||
27 | static reloc_howto_type *elf32_h8_reloc_type_lookup | |
28 | PARAMS ((bfd *abfd, bfd_reloc_code_real_type code)); | |
29 | static void elf32_h8_info_to_howto | |
30 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); | |
31 | static void elf32_h8_info_to_howto_rel | |
32 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); | |
33 | static unsigned long elf32_h8_mach | |
34 | PARAMS ((flagword)); | |
35 | static void elf32_h8_final_write_processing | |
36 | PARAMS ((bfd *, bfd_boolean)); | |
37 | static bfd_boolean elf32_h8_object_p | |
38 | PARAMS ((bfd *)); | |
39 | static bfd_boolean elf32_h8_merge_private_bfd_data | |
40 | PARAMS ((bfd *, bfd *)); | |
41 | static bfd_boolean elf32_h8_relax_section | |
42 | PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *)); | |
43 | static bfd_boolean elf32_h8_relax_delete_bytes | |
44 | PARAMS ((bfd *, asection *, bfd_vma, int)); | |
45 | static bfd_boolean elf32_h8_symbol_address_p | |
46 | PARAMS ((bfd *, asection *, bfd_vma)); | |
47 | static bfd_byte *elf32_h8_get_relocated_section_contents | |
48 | PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, | |
49 | bfd_byte *, bfd_boolean, asymbol **)); | |
50 | static bfd_reloc_status_type elf32_h8_final_link_relocate | |
51 | PARAMS ((unsigned long, bfd *, bfd *, asection *, | |
52 | bfd_byte *, bfd_vma, bfd_vma, bfd_vma, | |
53 | struct bfd_link_info *, asection *, int)); | |
54 | static bfd_boolean elf32_h8_relocate_section | |
55 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, | |
56 | bfd_byte *, Elf_Internal_Rela *, | |
57 | Elf_Internal_Sym *, asection **)); | |
58 | static bfd_reloc_status_type special | |
59 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
60 | ||
61 | /* This does not include any relocation information, but should be | |
62 | good enough for GDB or objdump to read the file. */ | |
63 | ||
64 | static reloc_howto_type h8_elf_howto_table[] = { | |
65 | #define R_H8_NONE_X 0 | |
66 | HOWTO (R_H8_NONE, /* type */ | |
67 | 0, /* rightshift */ | |
68 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
69 | 0, /* bitsize */ | |
70 | FALSE, /* pc_relative */ | |
71 | 0, /* bitpos */ | |
72 | complain_overflow_dont,/* complain_on_overflow */ | |
73 | special, /* special_function */ | |
74 | "R_H8_NONE", /* name */ | |
75 | FALSE, /* partial_inplace */ | |
76 | 0, /* src_mask */ | |
77 | 0, /* dst_mask */ | |
78 | FALSE), /* pcrel_offset */ | |
79 | #define R_H8_DIR32_X (R_H8_NONE_X + 1) | |
80 | HOWTO (R_H8_DIR32, /* type */ | |
81 | 0, /* rightshift */ | |
82 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
83 | 32, /* bitsize */ | |
84 | FALSE, /* pc_relative */ | |
85 | 0, /* bitpos */ | |
86 | complain_overflow_dont,/* complain_on_overflow */ | |
87 | special, /* special_function */ | |
88 | "R_H8_DIR32", /* name */ | |
89 | FALSE, /* partial_inplace */ | |
90 | 0, /* src_mask */ | |
91 | 0xffffffff, /* dst_mask */ | |
92 | FALSE), /* pcrel_offset */ | |
93 | #define R_H8_DIR16_X (R_H8_DIR32_X + 1) | |
94 | HOWTO (R_H8_DIR16, /* type */ | |
95 | 0, /* rightshift */ | |
96 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
97 | 16, /* bitsize */ | |
98 | FALSE, /* pc_relative */ | |
99 | 0, /* bitpos */ | |
100 | complain_overflow_dont,/* complain_on_overflow */ | |
101 | special, /* special_function */ | |
102 | "R_H8_DIR16", /* name */ | |
103 | FALSE, /* partial_inplace */ | |
104 | 0, /* src_mask */ | |
105 | 0x0000ffff, /* dst_mask */ | |
106 | FALSE), /* pcrel_offset */ | |
107 | #define R_H8_DIR8_X (R_H8_DIR16_X + 1) | |
108 | HOWTO (R_H8_DIR8, /* type */ | |
109 | 0, /* rightshift */ | |
110 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
111 | 8, /* bitsize */ | |
112 | FALSE, /* pc_relative */ | |
113 | 0, /* bitpos */ | |
114 | complain_overflow_dont,/* complain_on_overflow */ | |
115 | special, /* special_function */ | |
116 | "R_H8_DIR8", /* name */ | |
117 | FALSE, /* partial_inplace */ | |
118 | 0, /* src_mask */ | |
119 | 0x000000ff, /* dst_mask */ | |
120 | FALSE), /* pcrel_offset */ | |
121 | #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1) | |
122 | HOWTO (R_H8_DIR16A8, /* type */ | |
123 | 0, /* rightshift */ | |
124 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
125 | 16, /* bitsize */ | |
126 | FALSE, /* pc_relative */ | |
127 | 0, /* bitpos */ | |
128 | complain_overflow_bitfield, /* complain_on_overflow */ | |
129 | special, /* special_function */ | |
130 | "R_H8_DIR16A8", /* name */ | |
131 | FALSE, /* partial_inplace */ | |
132 | 0, /* src_mask */ | |
133 | 0x0000ffff, /* dst_mask */ | |
134 | FALSE), /* pcrel_offset */ | |
135 | #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1) | |
136 | HOWTO (R_H8_DIR16R8, /* type */ | |
137 | 0, /* rightshift */ | |
138 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
139 | 16, /* bitsize */ | |
140 | FALSE, /* pc_relative */ | |
141 | 0, /* bitpos */ | |
142 | complain_overflow_bitfield, /* complain_on_overflow */ | |
143 | special, /* special_function */ | |
144 | "R_H8_DIR16R8", /* name */ | |
145 | FALSE, /* partial_inplace */ | |
146 | 0, /* src_mask */ | |
147 | 0x0000ffff, /* dst_mask */ | |
148 | FALSE), /* pcrel_offset */ | |
149 | #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1) | |
150 | HOWTO (R_H8_DIR24A8, /* type */ | |
151 | 0, /* rightshift */ | |
152 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
153 | 24, /* bitsize */ | |
154 | FALSE, /* pc_relative */ | |
155 | 0, /* bitpos */ | |
156 | complain_overflow_bitfield, /* complain_on_overflow */ | |
157 | special, /* special_function */ | |
158 | "R_H8_DIR24A8", /* name */ | |
159 | TRUE, /* partial_inplace */ | |
160 | 0xff000000, /* src_mask */ | |
161 | 0x00ffffff, /* dst_mask */ | |
162 | FALSE), /* pcrel_offset */ | |
163 | #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1) | |
164 | HOWTO (R_H8_DIR24R8, /* type */ | |
165 | 0, /* rightshift */ | |
166 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
167 | 24, /* bitsize */ | |
168 | FALSE, /* pc_relative */ | |
169 | 0, /* bitpos */ | |
170 | complain_overflow_bitfield, /* complain_on_overflow */ | |
171 | special, /* special_function */ | |
172 | "R_H8_DIR24R8", /* name */ | |
173 | TRUE, /* partial_inplace */ | |
174 | 0xff000000, /* src_mask */ | |
175 | 0x00ffffff, /* dst_mask */ | |
176 | FALSE), /* pcrel_offset */ | |
177 | #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1) | |
178 | HOWTO (R_H8_DIR32A16, /* type */ | |
179 | 0, /* rightshift */ | |
180 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
181 | 32, /* bitsize */ | |
182 | FALSE, /* pc_relative */ | |
183 | 0, /* bitpos */ | |
184 | complain_overflow_dont,/* complain_on_overflow */ | |
185 | special, /* special_function */ | |
186 | "R_H8_DIR32", /* name */ | |
187 | FALSE, /* partial_inplace */ | |
188 | 0, /* src_mask */ | |
189 | 0xffffffff, /* dst_mask */ | |
190 | FALSE), /* pcrel_offset */ | |
191 | #define R_H8_PCREL16_X (R_H8_DIR32A16_X + 1) | |
192 | HOWTO (R_H8_PCREL16, /* type */ | |
193 | 0, /* rightshift */ | |
194 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
195 | 16, /* bitsize */ | |
196 | TRUE, /* pc_relative */ | |
197 | 0, /* bitpos */ | |
198 | complain_overflow_signed,/* complain_on_overflow */ | |
199 | special, /* special_function */ | |
200 | "R_H8_PCREL16", /* name */ | |
201 | FALSE, /* partial_inplace */ | |
202 | 0xffff, /* src_mask */ | |
203 | 0xffff, /* dst_mask */ | |
204 | TRUE), /* pcrel_offset */ | |
205 | #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1) | |
206 | HOWTO (R_H8_PCREL8, /* type */ | |
207 | 0, /* rightshift */ | |
208 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
209 | 8, /* bitsize */ | |
210 | TRUE, /* pc_relative */ | |
211 | 0, /* bitpos */ | |
212 | complain_overflow_signed,/* complain_on_overflow */ | |
213 | special, /* special_function */ | |
214 | "R_H8_PCREL8", /* name */ | |
215 | FALSE, /* partial_inplace */ | |
216 | 0xff, /* src_mask */ | |
217 | 0xff, /* dst_mask */ | |
218 | TRUE), /* pcrel_offset */ | |
219 | }; | |
220 | ||
221 | /* This structure is used to map BFD reloc codes to H8 ELF relocs. */ | |
222 | ||
223 | struct elf_reloc_map { | |
224 | bfd_reloc_code_real_type bfd_reloc_val; | |
225 | unsigned char howto_index; | |
226 | }; | |
227 | ||
228 | /* An array mapping BFD reloc codes to SH ELF relocs. */ | |
229 | ||
230 | static const struct elf_reloc_map h8_reloc_map[] = { | |
231 | { BFD_RELOC_NONE, R_H8_NONE_X }, | |
232 | { BFD_RELOC_32, R_H8_DIR32_X }, | |
233 | { BFD_RELOC_16, R_H8_DIR16_X }, | |
234 | { BFD_RELOC_8, R_H8_DIR8_X }, | |
235 | { BFD_RELOC_H8_DIR16A8, R_H8_DIR16A8_X }, | |
236 | { BFD_RELOC_H8_DIR16R8, R_H8_DIR16R8_X }, | |
237 | { BFD_RELOC_H8_DIR24A8, R_H8_DIR24A8_X }, | |
238 | { BFD_RELOC_H8_DIR24R8, R_H8_DIR24R8_X }, | |
239 | { BFD_RELOC_H8_DIR32A16, R_H8_DIR32A16_X }, | |
240 | { BFD_RELOC_16_PCREL, R_H8_PCREL16_X }, | |
241 | { BFD_RELOC_8_PCREL, R_H8_PCREL8_X }, | |
242 | }; | |
243 | ||
244 | ||
245 | static reloc_howto_type * | |
246 | elf32_h8_reloc_type_lookup (abfd, code) | |
247 | bfd *abfd ATTRIBUTE_UNUSED; | |
248 | bfd_reloc_code_real_type code; | |
249 | { | |
250 | unsigned int i; | |
251 | ||
252 | for (i = 0; i < sizeof (h8_reloc_map) / sizeof (struct elf_reloc_map); i++) | |
253 | { | |
254 | if (h8_reloc_map[i].bfd_reloc_val == code) | |
255 | return &h8_elf_howto_table[(int) h8_reloc_map[i].howto_index]; | |
256 | } | |
257 | return NULL; | |
258 | } | |
259 | ||
260 | static void | |
261 | elf32_h8_info_to_howto (abfd, bfd_reloc, elf_reloc) | |
262 | bfd *abfd ATTRIBUTE_UNUSED; | |
263 | arelent *bfd_reloc; | |
264 | Elf_Internal_Rela *elf_reloc; | |
265 | { | |
266 | unsigned int r; | |
267 | unsigned int i; | |
268 | ||
269 | r = ELF32_R_TYPE (elf_reloc->r_info); | |
270 | for (i = 0; i < sizeof (h8_elf_howto_table) / sizeof (reloc_howto_type); i++) | |
271 | if (h8_elf_howto_table[i].type == r) | |
272 | { | |
273 | bfd_reloc->howto = &h8_elf_howto_table[i]; | |
274 | return; | |
275 | } | |
276 | abort (); | |
277 | } | |
278 | ||
279 | static void | |
280 | elf32_h8_info_to_howto_rel (abfd, bfd_reloc, elf_reloc) | |
281 | bfd *abfd ATTRIBUTE_UNUSED; | |
282 | arelent *bfd_reloc; | |
283 | Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED; | |
284 | { | |
285 | unsigned int r; | |
286 | ||
287 | abort (); | |
288 | r = ELF32_R_TYPE (elf_reloc->r_info); | |
289 | bfd_reloc->howto = &h8_elf_howto_table[r]; | |
290 | } | |
291 | ||
292 | /* Special handling for H8/300 relocs. | |
293 | We only come here for pcrel stuff and return normally if not an -r link. | |
294 | When doing -r, we can't do any arithmetic for the pcrel stuff, because | |
295 | we support relaxing on the H8/300 series chips. */ | |
296 | static bfd_reloc_status_type | |
297 | special (abfd, reloc_entry, symbol, data, input_section, output_bfd, | |
298 | error_message) | |
299 | bfd *abfd ATTRIBUTE_UNUSED; | |
300 | arelent *reloc_entry ATTRIBUTE_UNUSED; | |
301 | asymbol *symbol ATTRIBUTE_UNUSED; | |
302 | PTR data ATTRIBUTE_UNUSED; | |
303 | asection *input_section ATTRIBUTE_UNUSED; | |
304 | bfd *output_bfd; | |
305 | char **error_message ATTRIBUTE_UNUSED; | |
306 | { | |
307 | if (output_bfd == (bfd *) NULL) | |
308 | return bfd_reloc_continue; | |
309 | ||
310 | /* Adjust the reloc address to that in the output section. */ | |
311 | reloc_entry->address += input_section->output_offset; | |
312 | return bfd_reloc_ok; | |
313 | } | |
314 | ||
315 | /* Perform a relocation as part of a final link. */ | |
316 | static bfd_reloc_status_type | |
317 | elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd, | |
318 | input_section, contents, offset, value, | |
319 | addend, info, sym_sec, is_local) | |
320 | unsigned long r_type; | |
321 | bfd *input_bfd; | |
322 | bfd *output_bfd ATTRIBUTE_UNUSED; | |
323 | asection *input_section ATTRIBUTE_UNUSED; | |
324 | bfd_byte *contents; | |
325 | bfd_vma offset; | |
326 | bfd_vma value; | |
327 | bfd_vma addend; | |
328 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
329 | asection *sym_sec ATTRIBUTE_UNUSED; | |
330 | int is_local ATTRIBUTE_UNUSED; | |
331 | { | |
332 | bfd_byte *hit_data = contents + offset; | |
333 | ||
334 | switch (r_type) | |
335 | { | |
336 | ||
337 | case R_H8_NONE: | |
338 | return bfd_reloc_ok; | |
339 | ||
340 | case R_H8_DIR32: | |
341 | case R_H8_DIR32A16: | |
342 | case R_H8_DIR24A8: | |
343 | value += addend; | |
344 | bfd_put_32 (input_bfd, value, hit_data); | |
345 | return bfd_reloc_ok; | |
346 | ||
347 | case R_H8_DIR16: | |
348 | case R_H8_DIR16A8: | |
349 | case R_H8_DIR16R8: | |
350 | value += addend; | |
351 | bfd_put_16 (input_bfd, value, hit_data); | |
352 | return bfd_reloc_ok; | |
353 | ||
354 | /* AKA R_RELBYTE */ | |
355 | case R_H8_DIR8: | |
356 | value += addend; | |
357 | ||
358 | bfd_put_8 (input_bfd, value, hit_data); | |
359 | return bfd_reloc_ok; | |
360 | ||
361 | case R_H8_DIR24R8: | |
362 | value += addend; | |
363 | ||
364 | /* HIT_DATA is the address for the first byte for the relocated | |
365 | value. Subtract 1 so that we can manipulate the data in 32bit | |
366 | hunks. */ | |
367 | hit_data--; | |
368 | ||
369 | /* Clear out the top byte in value. */ | |
370 | value &= 0xffffff; | |
371 | ||
372 | /* Retrieve the type byte for value from the section contents. */ | |
373 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000); | |
374 | ||
375 | /* Now scribble it out in one 32bit hunk. */ | |
376 | bfd_put_32 (input_bfd, value, hit_data); | |
377 | return bfd_reloc_ok; | |
378 | ||
379 | case R_H8_PCREL16: | |
380 | value -= (input_section->output_section->vma | |
381 | + input_section->output_offset); | |
382 | value -= offset; | |
383 | value += addend; | |
384 | ||
385 | /* The value is relative to the start of the instruction, | |
386 | not the relocation offset. Subtract 2 to account for | |
387 | this minor issue. */ | |
388 | value -= 2; | |
389 | ||
390 | bfd_put_16 (input_bfd, value, hit_data); | |
391 | return bfd_reloc_ok; | |
392 | ||
393 | case R_H8_PCREL8: | |
394 | value -= (input_section->output_section->vma | |
395 | + input_section->output_offset); | |
396 | value -= offset; | |
397 | value += addend; | |
398 | ||
399 | /* The value is relative to the start of the instruction, | |
400 | not the relocation offset. Subtract 1 to account for | |
401 | this minor issue. */ | |
402 | value -= 1; | |
403 | ||
404 | bfd_put_8 (input_bfd, value, hit_data); | |
405 | return bfd_reloc_ok; | |
406 | ||
407 | default: | |
408 | return bfd_reloc_notsupported; | |
409 | } | |
410 | } | |
411 | \f | |
412 | /* Relocate an H8 ELF section. */ | |
413 | static bfd_boolean | |
414 | elf32_h8_relocate_section (output_bfd, info, input_bfd, input_section, | |
415 | contents, relocs, local_syms, local_sections) | |
416 | bfd *output_bfd; | |
417 | struct bfd_link_info *info; | |
418 | bfd *input_bfd; | |
419 | asection *input_section; | |
420 | bfd_byte *contents; | |
421 | Elf_Internal_Rela *relocs; | |
422 | Elf_Internal_Sym *local_syms; | |
423 | asection **local_sections; | |
424 | { | |
425 | Elf_Internal_Shdr *symtab_hdr; | |
426 | struct elf_link_hash_entry **sym_hashes; | |
427 | Elf_Internal_Rela *rel, *relend; | |
428 | ||
429 | if (info->relocateable) | |
430 | return TRUE; | |
431 | ||
432 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
433 | sym_hashes = elf_sym_hashes (input_bfd); | |
434 | ||
435 | rel = relocs; | |
436 | relend = relocs + input_section->reloc_count; | |
437 | for (; rel < relend; rel++) | |
438 | { | |
439 | unsigned int r_type; | |
440 | unsigned long r_symndx; | |
441 | Elf_Internal_Sym *sym; | |
442 | asection *sec; | |
443 | struct elf_link_hash_entry *h; | |
444 | bfd_vma relocation; | |
445 | bfd_reloc_status_type r; | |
446 | ||
447 | /* This is a final link. */ | |
448 | r_symndx = ELF32_R_SYM (rel->r_info); | |
449 | r_type = ELF32_R_TYPE (rel->r_info); | |
450 | h = NULL; | |
451 | sym = NULL; | |
452 | sec = NULL; | |
453 | if (r_symndx < symtab_hdr->sh_info) | |
454 | { | |
455 | sym = local_syms + r_symndx; | |
456 | sec = local_sections[r_symndx]; | |
457 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel); | |
458 | } | |
459 | else | |
460 | { | |
461 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
462 | while (h->root.type == bfd_link_hash_indirect | |
463 | || h->root.type == bfd_link_hash_warning) | |
464 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
465 | if (h->root.type == bfd_link_hash_defined | |
466 | || h->root.type == bfd_link_hash_defweak) | |
467 | { | |
468 | sec = h->root.u.def.section; | |
469 | relocation = (h->root.u.def.value | |
470 | + sec->output_section->vma | |
471 | + sec->output_offset); | |
472 | } | |
473 | else if (h->root.type == bfd_link_hash_undefweak) | |
474 | relocation = 0; | |
475 | else | |
476 | { | |
477 | if (! ((*info->callbacks->undefined_symbol) | |
478 | (info, h->root.root.string, input_bfd, | |
479 | input_section, rel->r_offset, TRUE))) | |
480 | return FALSE; | |
481 | relocation = 0; | |
482 | } | |
483 | } | |
484 | ||
485 | r = elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd, | |
486 | input_section, | |
487 | contents, rel->r_offset, | |
488 | relocation, rel->r_addend, | |
489 | info, sec, h == NULL); | |
490 | ||
491 | if (r != bfd_reloc_ok) | |
492 | { | |
493 | const char *name; | |
494 | const char *msg = (const char *) 0; | |
495 | arelent bfd_reloc; | |
496 | reloc_howto_type *howto; | |
497 | ||
498 | elf32_h8_info_to_howto (input_bfd, &bfd_reloc, rel); | |
499 | howto = bfd_reloc.howto; | |
500 | ||
501 | if (h != NULL) | |
502 | name = h->root.root.string; | |
503 | else | |
504 | { | |
505 | name = (bfd_elf_string_from_elf_section | |
506 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); | |
507 | if (name == NULL || *name == '\0') | |
508 | name = bfd_section_name (input_bfd, sec); | |
509 | } | |
510 | ||
511 | switch (r) | |
512 | { | |
513 | case bfd_reloc_overflow: | |
514 | if (! ((*info->callbacks->reloc_overflow) | |
515 | (info, name, howto->name, (bfd_vma) 0, | |
516 | input_bfd, input_section, rel->r_offset))) | |
517 | return FALSE; | |
518 | break; | |
519 | ||
520 | case bfd_reloc_undefined: | |
521 | if (! ((*info->callbacks->undefined_symbol) | |
522 | (info, name, input_bfd, input_section, | |
523 | rel->r_offset, TRUE))) | |
524 | return FALSE; | |
525 | break; | |
526 | ||
527 | case bfd_reloc_outofrange: | |
528 | msg = _("internal error: out of range error"); | |
529 | goto common_error; | |
530 | ||
531 | case bfd_reloc_notsupported: | |
532 | msg = _("internal error: unsupported relocation error"); | |
533 | goto common_error; | |
534 | ||
535 | case bfd_reloc_dangerous: | |
536 | msg = _("internal error: dangerous error"); | |
537 | goto common_error; | |
538 | ||
539 | default: | |
540 | msg = _("internal error: unknown error"); | |
541 | /* fall through */ | |
542 | ||
543 | common_error: | |
544 | if (!((*info->callbacks->warning) | |
545 | (info, msg, name, input_bfd, input_section, | |
546 | rel->r_offset))) | |
547 | return FALSE; | |
548 | break; | |
549 | } | |
550 | } | |
551 | } | |
552 | ||
553 | return TRUE; | |
554 | } | |
555 | ||
556 | /* Object files encode the specific H8 model they were compiled | |
557 | for in the ELF flags field. | |
558 | ||
559 | Examine that field and return the proper BFD machine type for | |
560 | the object file. */ | |
561 | static unsigned long | |
562 | elf32_h8_mach (flags) | |
563 | flagword flags; | |
564 | { | |
565 | switch (flags & EF_H8_MACH) | |
566 | { | |
567 | case E_H8_MACH_H8300: | |
568 | default: | |
569 | return bfd_mach_h8300; | |
570 | ||
571 | case E_H8_MACH_H8300H: | |
572 | return bfd_mach_h8300h; | |
573 | ||
574 | case E_H8_MACH_H8300S: | |
575 | return bfd_mach_h8300s; | |
576 | ||
577 | case E_H8_MACH_H8300HN: | |
578 | return bfd_mach_h8300hn; | |
579 | ||
580 | case E_H8_MACH_H8300SN: | |
581 | return bfd_mach_h8300sn; | |
582 | ||
583 | case E_H8_MACH_H8300SX: | |
584 | return bfd_mach_h8300sx; | |
585 | } | |
586 | } | |
587 | ||
588 | /* The final processing done just before writing out a H8 ELF object | |
589 | file. We use this opportunity to encode the BFD machine type | |
590 | into the flags field in the object file. */ | |
591 | ||
592 | static void | |
593 | elf32_h8_final_write_processing (abfd, linker) | |
594 | bfd *abfd; | |
595 | bfd_boolean linker ATTRIBUTE_UNUSED; | |
596 | { | |
597 | unsigned long val; | |
598 | ||
599 | switch (bfd_get_mach (abfd)) | |
600 | { | |
601 | default: | |
602 | case bfd_mach_h8300: | |
603 | val = E_H8_MACH_H8300; | |
604 | break; | |
605 | ||
606 | case bfd_mach_h8300h: | |
607 | val = E_H8_MACH_H8300H; | |
608 | break; | |
609 | ||
610 | case bfd_mach_h8300s: | |
611 | val = E_H8_MACH_H8300S; | |
612 | break; | |
613 | ||
614 | case bfd_mach_h8300hn: | |
615 | val = E_H8_MACH_H8300HN; | |
616 | break; | |
617 | ||
618 | case bfd_mach_h8300sn: | |
619 | val = E_H8_MACH_H8300SN; | |
620 | break; | |
621 | ||
622 | case bfd_mach_h8300sx: | |
623 | val = E_H8_MACH_H8300SX; | |
624 | break; | |
625 | } | |
626 | ||
627 | elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH); | |
628 | elf_elfheader (abfd)->e_flags |= val; | |
629 | } | |
630 | ||
631 | /* Return nonzero if ABFD represents a valid H8 ELF object file; also | |
632 | record the encoded machine type found in the ELF flags. */ | |
633 | ||
634 | static bfd_boolean | |
635 | elf32_h8_object_p (abfd) | |
636 | bfd *abfd; | |
637 | { | |
638 | bfd_default_set_arch_mach (abfd, bfd_arch_h8300, | |
639 | elf32_h8_mach (elf_elfheader (abfd)->e_flags)); | |
640 | return TRUE; | |
641 | } | |
642 | ||
643 | /* Merge backend specific data from an object file to the output | |
644 | object file when linking. The only data we need to copy at this | |
645 | time is the architecture/machine information. */ | |
646 | ||
647 | static bfd_boolean | |
648 | elf32_h8_merge_private_bfd_data (ibfd, obfd) | |
649 | bfd *ibfd; | |
650 | bfd *obfd; | |
651 | { | |
652 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
653 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
654 | return TRUE; | |
655 | ||
656 | if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) | |
657 | && bfd_get_mach (obfd) < bfd_get_mach (ibfd)) | |
658 | { | |
659 | if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), | |
660 | bfd_get_mach (ibfd))) | |
661 | return FALSE; | |
662 | } | |
663 | ||
664 | return TRUE; | |
665 | } | |
666 | ||
667 | /* This function handles relaxing for the H8.. | |
668 | ||
669 | There's a few relaxing opportunites available on the H8: | |
670 | ||
671 | jmp/jsr:24 -> bra/bsr:8 2 bytes | |
672 | The jmp may be completely eliminated if the previous insn is a | |
673 | conditional branch to the insn after the jump. In that case | |
674 | we invert the branch and delete the jump and save 4 bytes. | |
675 | ||
676 | bCC:16 -> bCC:8 2 bytes | |
677 | bsr:16 -> bsr:8 2 bytes | |
678 | ||
679 | mov.b:16 -> mov.b:8 2 bytes | |
680 | mov.b:24/32 -> mov.b:8 4 bytes | |
681 | ||
682 | mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes */ | |
683 | ||
684 | static bfd_boolean | |
685 | elf32_h8_relax_section (abfd, sec, link_info, again) | |
686 | bfd *abfd; | |
687 | asection *sec; | |
688 | struct bfd_link_info *link_info; | |
689 | bfd_boolean *again; | |
690 | { | |
691 | Elf_Internal_Shdr *symtab_hdr; | |
692 | Elf_Internal_Rela *internal_relocs; | |
693 | Elf_Internal_Rela *irel, *irelend; | |
694 | bfd_byte *contents = NULL; | |
695 | Elf_Internal_Sym *isymbuf = NULL; | |
696 | static asection *last_input_section = NULL; | |
697 | static Elf_Internal_Rela *last_reloc = NULL; | |
698 | ||
699 | /* Assume nothing changes. */ | |
700 | *again = FALSE; | |
701 | ||
702 | /* We don't have to do anything for a relocateable link, if | |
703 | this section does not have relocs, or if this is not a | |
704 | code section. */ | |
705 | if (link_info->relocateable | |
706 | || (sec->flags & SEC_RELOC) == 0 | |
707 | || sec->reloc_count == 0 | |
708 | || (sec->flags & SEC_CODE) == 0) | |
709 | return TRUE; | |
710 | ||
711 | /* If this is the first time we have been called for this section, | |
712 | initialize the cooked size. */ | |
713 | if (sec->_cooked_size == 0) | |
714 | sec->_cooked_size = sec->_raw_size; | |
715 | ||
716 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
717 | ||
718 | /* Get a copy of the native relocations. */ | |
719 | internal_relocs = (_bfd_elf_link_read_relocs | |
720 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
721 | link_info->keep_memory)); | |
722 | if (internal_relocs == NULL) | |
723 | goto error_return; | |
724 | ||
725 | if (sec != last_input_section) | |
726 | last_reloc = NULL; | |
727 | ||
728 | last_input_section = sec; | |
729 | ||
730 | /* Walk through the relocs looking for relaxing opportunities. */ | |
731 | irelend = internal_relocs + sec->reloc_count; | |
732 | for (irel = internal_relocs; irel < irelend; irel++) | |
733 | { | |
734 | bfd_vma symval; | |
735 | ||
736 | /* Keep track of the previous reloc so that we can delete | |
737 | some long jumps created by the compiler. */ | |
738 | if (irel != internal_relocs) | |
739 | last_reloc = irel - 1; | |
740 | ||
741 | if (ELF32_R_TYPE (irel->r_info) != R_H8_DIR24R8 | |
742 | && ELF32_R_TYPE (irel->r_info) != R_H8_PCREL16 | |
743 | && ELF32_R_TYPE (irel->r_info) != R_H8_DIR16A8 | |
744 | && ELF32_R_TYPE (irel->r_info) != R_H8_DIR24A8 | |
745 | && ELF32_R_TYPE (irel->r_info) != R_H8_DIR32A16) | |
746 | continue; | |
747 | ||
748 | /* Get the section contents if we haven't done so already. */ | |
749 | if (contents == NULL) | |
750 | { | |
751 | /* Get cached copy if it exists. */ | |
752 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
753 | contents = elf_section_data (sec)->this_hdr.contents; | |
754 | else | |
755 | { | |
756 | /* Go get them off disk. */ | |
757 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
758 | if (contents == NULL) | |
759 | goto error_return; | |
760 | ||
761 | if (! bfd_get_section_contents (abfd, sec, contents, | |
762 | (file_ptr) 0, sec->_raw_size)) | |
763 | goto error_return; | |
764 | } | |
765 | } | |
766 | ||
767 | /* Read this BFD's local symbols if we haven't done so already. */ | |
768 | if (isymbuf == NULL && symtab_hdr->sh_info != 0) | |
769 | { | |
770 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
771 | if (isymbuf == NULL) | |
772 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
773 | symtab_hdr->sh_info, 0, | |
774 | NULL, NULL, NULL); | |
775 | if (isymbuf == NULL) | |
776 | goto error_return; | |
777 | } | |
778 | ||
779 | /* Get the value of the symbol referred to by the reloc. */ | |
780 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
781 | { | |
782 | /* A local symbol. */ | |
783 | Elf_Internal_Sym *isym; | |
784 | asection *sym_sec; | |
785 | ||
786 | isym = isymbuf + ELF32_R_SYM (irel->r_info); | |
787 | sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
788 | symval = isym->st_value; | |
789 | /* If the reloc is absolute, it will not have | |
790 | a symbol or section associated with it. */ | |
791 | if (sym_sec) | |
792 | symval += sym_sec->output_section->vma | |
793 | + sym_sec->output_offset; | |
794 | } | |
795 | else | |
796 | { | |
797 | unsigned long indx; | |
798 | struct elf_link_hash_entry *h; | |
799 | ||
800 | /* An external symbol. */ | |
801 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; | |
802 | h = elf_sym_hashes (abfd)[indx]; | |
803 | BFD_ASSERT (h != NULL); | |
804 | if (h->root.type != bfd_link_hash_defined | |
805 | && h->root.type != bfd_link_hash_defweak) | |
806 | { | |
807 | /* This appears to be a reference to an undefined | |
808 | symbol. Just ignore it--it will be caught by the | |
809 | regular reloc processing. */ | |
810 | continue; | |
811 | } | |
812 | ||
813 | symval = (h->root.u.def.value | |
814 | + h->root.u.def.section->output_section->vma | |
815 | + h->root.u.def.section->output_offset); | |
816 | } | |
817 | ||
818 | /* For simplicity of coding, we are going to modify the section | |
819 | contents, the section relocs, and the BFD symbol table. We | |
820 | must tell the rest of the code not to free up this | |
821 | information. It would be possible to instead create a table | |
822 | of changes which have to be made, as is done in coff-mips.c; | |
823 | that would be more work, but would require less memory when | |
824 | the linker is run. */ | |
825 | switch (ELF32_R_TYPE (irel->r_info)) | |
826 | { | |
827 | /* Try to turn a 24 bit absolute branch/call into an 8 bit | |
828 | pc-relative branch/call. */ | |
829 | case R_H8_DIR24R8: | |
830 | { | |
831 | bfd_vma value = symval + irel->r_addend; | |
832 | bfd_vma dot, gap; | |
833 | ||
834 | /* Get the address of this instruction. */ | |
835 | dot = (sec->output_section->vma | |
836 | + sec->output_offset + irel->r_offset - 1); | |
837 | ||
838 | /* Compute the distance from this insn to the branch target. */ | |
839 | gap = value - dot; | |
840 | ||
841 | /* If the distance is within -126..+130 inclusive, then we can | |
842 | relax this jump. +130 is valid since the target will move | |
843 | two bytes closer if we do relax this branch. */ | |
844 | if ((int) gap >= -126 && (int) gap <= 130) | |
845 | { | |
846 | unsigned char code; | |
847 | ||
848 | /* Note that we've changed the relocs, section contents, | |
849 | etc. */ | |
850 | elf_section_data (sec)->relocs = internal_relocs; | |
851 | elf_section_data (sec)->this_hdr.contents = contents; | |
852 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
853 | ||
854 | /* Get the instruction code being relaxed. */ | |
855 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
856 | ||
857 | /* If the previous instruction conditionally jumped around | |
858 | this instruction, we may be able to reverse the condition | |
859 | and redirect the previous instruction to the target of | |
860 | this instruction. | |
861 | ||
862 | Such sequences are used by the compiler to deal with | |
863 | long conditional branches. | |
864 | ||
865 | Only perform this optimisation for jumps (code 0x5a) not | |
866 | subroutine calls, as otherwise it could transform: | |
867 | ||
868 | mov.w r0,r0 | |
869 | beq .L1 | |
870 | jsr @_bar | |
871 | .L1: rts | |
872 | _bar: rts | |
873 | into: | |
874 | mov.w r0,r0 | |
875 | bne _bar | |
876 | rts | |
877 | _bar: rts | |
878 | ||
879 | which changes the call (jsr) into a branch (bne). */ | |
880 | if (code == 0x5a | |
881 | && (int) gap <= 130 | |
882 | && (int) gap >= -128 | |
883 | && last_reloc | |
884 | && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8 | |
885 | && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info) | |
886 | { | |
887 | bfd_vma last_value; | |
888 | asection *last_sym_sec; | |
889 | Elf_Internal_Sym *last_sym; | |
890 | ||
891 | /* We will need to examine the symbol used by the | |
892 | previous relocation. */ | |
893 | ||
894 | last_sym = isymbuf + ELF32_R_SYM (last_reloc->r_info); | |
895 | last_sym_sec | |
896 | = bfd_section_from_elf_index (abfd, last_sym->st_shndx); | |
897 | last_value = (last_sym->st_value | |
898 | + last_sym_sec->output_section->vma | |
899 | + last_sym_sec->output_offset); | |
900 | ||
901 | /* Verify that the previous relocation was for a | |
902 | branch around this instruction and that no symbol | |
903 | exists at the current location. */ | |
904 | if (last_value == dot + 4 | |
905 | && last_reloc->r_offset + 2 == irel->r_offset | |
906 | && ! elf32_h8_symbol_address_p (abfd, sec, dot)) | |
907 | { | |
908 | /* We can eliminate this jump. Twiddle the | |
909 | previous relocation as necessary. */ | |
910 | irel->r_info | |
911 | = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
912 | ELF32_R_TYPE (R_H8_NONE)); | |
913 | ||
914 | last_reloc->r_info | |
915 | = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
916 | ELF32_R_TYPE (R_H8_PCREL8)); | |
917 | last_reloc->r_addend = irel->r_addend; | |
918 | ||
919 | code = bfd_get_8 (abfd, | |
920 | contents + last_reloc->r_offset - 1); | |
921 | code ^= 1; | |
922 | bfd_put_8 (abfd, | |
923 | code, | |
924 | contents + last_reloc->r_offset - 1); | |
925 | ||
926 | /* Delete four bytes of data. */ | |
927 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
928 | irel->r_offset - 1, | |
929 | 4)) | |
930 | goto error_return; | |
931 | ||
932 | *again = TRUE; | |
933 | break; | |
934 | } | |
935 | } | |
936 | ||
937 | if (code == 0x5e) | |
938 | bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1); | |
939 | else if (code == 0x5a) | |
940 | bfd_put_8 (abfd, 0x40, contents + irel->r_offset - 1); | |
941 | else | |
942 | abort (); | |
943 | ||
944 | /* Fix the relocation's type. */ | |
945 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
946 | R_H8_PCREL8); | |
947 | ||
948 | /* Delete two bytes of data. */ | |
949 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
950 | irel->r_offset + 1, 2)) | |
951 | goto error_return; | |
952 | ||
953 | /* That will change things, so, we should relax again. | |
954 | Note that this is not required, and it may be slow. */ | |
955 | *again = TRUE; | |
956 | } | |
957 | break; | |
958 | } | |
959 | ||
960 | /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative | |
961 | branch. */ | |
962 | case R_H8_PCREL16: | |
963 | { | |
964 | bfd_vma value = symval + irel->r_addend; | |
965 | bfd_vma dot; | |
966 | bfd_vma gap; | |
967 | ||
968 | /* Get the address of this instruction. */ | |
969 | dot = (sec->output_section->vma | |
970 | + sec->output_offset | |
971 | + irel->r_offset - 2); | |
972 | ||
973 | gap = value - dot; | |
974 | ||
975 | /* If the distance is within -126..+130 inclusive, then we can | |
976 | relax this jump. +130 is valid since the target will move | |
977 | two bytes closer if we do relax this branch. */ | |
978 | if ((int) gap >= -126 && (int) gap <= 130) | |
979 | { | |
980 | unsigned char code; | |
981 | ||
982 | /* Note that we've changed the relocs, section contents, | |
983 | etc. */ | |
984 | elf_section_data (sec)->relocs = internal_relocs; | |
985 | elf_section_data (sec)->this_hdr.contents = contents; | |
986 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
987 | ||
988 | /* Get the opcode. */ | |
989 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
990 | ||
991 | if (code == 0x58) | |
992 | { | |
993 | /* bCC:16 -> bCC:8 */ | |
994 | /* Get the condition code from the original insn. */ | |
995 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
996 | code &= 0xf0; | |
997 | code >>= 4; | |
998 | code |= 0x40; | |
999 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); | |
1000 | } | |
1001 | else if (code == 0x5c) | |
1002 | bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2); | |
1003 | else | |
1004 | abort (); | |
1005 | ||
1006 | /* Fix the relocation's type. */ | |
1007 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1008 | R_H8_PCREL8); | |
1009 | irel->r_offset--; | |
1010 | ||
1011 | /* Delete two bytes of data. */ | |
1012 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
1013 | irel->r_offset + 1, 2)) | |
1014 | goto error_return; | |
1015 | ||
1016 | /* That will change things, so, we should relax again. | |
1017 | Note that this is not required, and it may be slow. */ | |
1018 | *again = TRUE; | |
1019 | } | |
1020 | break; | |
1021 | } | |
1022 | ||
1023 | /* This is a 16 bit absolute address in a "mov.b" insn, which may | |
1024 | become an 8 bit absolute address if its in the right range. */ | |
1025 | case R_H8_DIR16A8: | |
1026 | { | |
1027 | bfd_vma value = symval + irel->r_addend; | |
1028 | ||
1029 | if ((bfd_get_mach (abfd) == bfd_mach_h8300 | |
1030 | && value >= 0xff00 | |
1031 | && value <= 0xffff) | |
1032 | || ((bfd_get_mach (abfd) == bfd_mach_h8300h | |
1033 | /* FIXME: h8300hn? */ | |
1034 | || bfd_get_mach (abfd) == bfd_mach_h8300s | |
1035 | /* FIXME: h8300sn? */ | |
1036 | || bfd_get_mach (abfd) == bfd_mach_h8300sx) | |
1037 | && value >= 0xffff00 | |
1038 | && value <= 0xffffff)) | |
1039 | { | |
1040 | unsigned char code; | |
1041 | ||
1042 | /* Note that we've changed the relocs, section contents, | |
1043 | etc. */ | |
1044 | elf_section_data (sec)->relocs = internal_relocs; | |
1045 | elf_section_data (sec)->this_hdr.contents = contents; | |
1046 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
1047 | ||
1048 | /* Get the opcode. */ | |
1049 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
1050 | ||
1051 | /* Sanity check. */ | |
1052 | if (code != 0x6a) | |
1053 | abort (); | |
1054 | ||
1055 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1056 | ||
1057 | if ((code & 0xf0) == 0x00) | |
1058 | bfd_put_8 (abfd, | |
1059 | (code & 0xf) | 0x20, | |
1060 | contents + irel->r_offset - 2); | |
1061 | else if ((code & 0xf0) == 0x80) | |
1062 | bfd_put_8 (abfd, | |
1063 | (code & 0xf) | 0x30, | |
1064 | contents + irel->r_offset - 2); | |
1065 | else | |
1066 | abort (); | |
1067 | ||
1068 | /* Fix the relocation's type. */ | |
1069 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1070 | R_H8_DIR8); | |
1071 | ||
1072 | /* Delete two bytes of data. */ | |
1073 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
1074 | irel->r_offset + 1, 2)) | |
1075 | goto error_return; | |
1076 | ||
1077 | /* That will change things, so, we should relax again. | |
1078 | Note that this is not required, and it may be slow. */ | |
1079 | *again = TRUE; | |
1080 | } | |
1081 | break; | |
1082 | } | |
1083 | ||
1084 | /* This is a 24 bit absolute address in a "mov.b" insn, which may | |
1085 | become an 8 bit absolute address if its in the right range. */ | |
1086 | case R_H8_DIR24A8: | |
1087 | { | |
1088 | bfd_vma value = symval + irel->r_addend; | |
1089 | ||
1090 | if ((bfd_get_mach (abfd) == bfd_mach_h8300 | |
1091 | && value >= 0xff00 | |
1092 | && value <= 0xffff) | |
1093 | || ((bfd_get_mach (abfd) == bfd_mach_h8300h | |
1094 | /* FIXME: h8300hn? */ | |
1095 | || bfd_get_mach (abfd) == bfd_mach_h8300s | |
1096 | /* FIXME: h8300sn? */ | |
1097 | || bfd_get_mach (abfd) == bfd_mach_h8300sx) | |
1098 | && value >= 0xffff00 | |
1099 | && value <= 0xffffff)) | |
1100 | { | |
1101 | bfd_boolean skip = FALSE; | |
1102 | unsigned char code; | |
1103 | ||
1104 | /* Note that we've changed the relocs, section contents, | |
1105 | etc. */ | |
1106 | elf_section_data (sec)->relocs = internal_relocs; | |
1107 | elf_section_data (sec)->this_hdr.contents = contents; | |
1108 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
1109 | ||
1110 | /* Get the opcode. */ | |
1111 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); | |
1112 | ||
1113 | /* Sanity check. */ | |
1114 | if (code != 0x6a) | |
1115 | abort (); | |
1116 | ||
1117 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1118 | ||
1119 | switch (code & 0xf0) | |
1120 | { | |
1121 | case 0x00: | |
1122 | bfd_put_8 (abfd, (code & 0xf) | 0x20, | |
1123 | contents + irel->r_offset - 2); | |
1124 | break; | |
1125 | case 0x80: | |
1126 | bfd_put_8 (abfd, (code & 0xf) | 0x30, | |
1127 | contents + irel->r_offset - 2); | |
1128 | break; | |
1129 | case 0x20: | |
1130 | case 0xa0: | |
1131 | /* Skip 32bit versions. */ | |
1132 | skip = TRUE; | |
1133 | break; | |
1134 | default: | |
1135 | abort (); | |
1136 | } | |
1137 | ||
1138 | if (skip) | |
1139 | break; | |
1140 | ||
1141 | /* Fix the relocation's type. */ | |
1142 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1143 | R_H8_DIR8); | |
1144 | ||
1145 | /* Delete two bytes of data. */ | |
1146 | if (!elf32_h8_relax_delete_bytes (abfd, sec, irel->r_offset, 2)) | |
1147 | goto error_return; | |
1148 | ||
1149 | /* That will change things, so, we should relax again. | |
1150 | Note that this is not required, and it may be slow. */ | |
1151 | *again = TRUE; | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | /* FALLTHRU */ | |
1156 | ||
1157 | /* This is a 24/32bit absolute address in a "mov" insn, which may | |
1158 | become a 16bit absoulte address if it is in the right range. */ | |
1159 | case R_H8_DIR32A16: | |
1160 | { | |
1161 | bfd_vma value = symval + irel->r_addend; | |
1162 | ||
1163 | if (value <= 0x7fff || value >= 0xff8000) | |
1164 | { | |
1165 | unsigned char code; | |
1166 | ||
1167 | /* Note that we've changed the relocs, section contents, | |
1168 | etc. */ | |
1169 | elf_section_data (sec)->relocs = internal_relocs; | |
1170 | elf_section_data (sec)->this_hdr.contents = contents; | |
1171 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
1172 | ||
1173 | /* Get the opcode. */ | |
1174 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); | |
1175 | ||
1176 | /* We just need to turn off bit 0x20. */ | |
1177 | code &= ~0x20; | |
1178 | ||
1179 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); | |
1180 | ||
1181 | /* Fix the relocation's type. */ | |
1182 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
1183 | R_H8_DIR16A8); | |
1184 | ||
1185 | /* Delete two bytes of data. */ | |
1186 | if (!elf32_h8_relax_delete_bytes (abfd, sec, | |
1187 | irel->r_offset + 1, 2)) | |
1188 | goto error_return; | |
1189 | ||
1190 | /* That will change things, so, we should relax again. | |
1191 | Note that this is not required, and it may be slow. */ | |
1192 | *again = TRUE; | |
1193 | } | |
1194 | break; | |
1195 | } | |
1196 | ||
1197 | default: | |
1198 | break; | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | if (isymbuf != NULL | |
1203 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1204 | { | |
1205 | if (! link_info->keep_memory) | |
1206 | free (isymbuf); | |
1207 | else | |
1208 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
1209 | } | |
1210 | ||
1211 | if (contents != NULL | |
1212 | && elf_section_data (sec)->this_hdr.contents != contents) | |
1213 | { | |
1214 | if (! link_info->keep_memory) | |
1215 | free (contents); | |
1216 | else | |
1217 | { | |
1218 | /* Cache the section contents for elf_link_input_bfd. */ | |
1219 | elf_section_data (sec)->this_hdr.contents = contents; | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | if (internal_relocs != NULL | |
1224 | && elf_section_data (sec)->relocs != internal_relocs) | |
1225 | free (internal_relocs); | |
1226 | ||
1227 | return TRUE; | |
1228 | ||
1229 | error_return: | |
1230 | if (isymbuf != NULL | |
1231 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1232 | free (isymbuf); | |
1233 | if (contents != NULL | |
1234 | && elf_section_data (sec)->this_hdr.contents != contents) | |
1235 | free (contents); | |
1236 | if (internal_relocs != NULL | |
1237 | && elf_section_data (sec)->relocs != internal_relocs) | |
1238 | free (internal_relocs); | |
1239 | return FALSE; | |
1240 | } | |
1241 | ||
1242 | /* Delete some bytes from a section while relaxing. */ | |
1243 | ||
1244 | static bfd_boolean | |
1245 | elf32_h8_relax_delete_bytes (abfd, sec, addr, count) | |
1246 | bfd *abfd; | |
1247 | asection *sec; | |
1248 | bfd_vma addr; | |
1249 | int count; | |
1250 | { | |
1251 | Elf_Internal_Shdr *symtab_hdr; | |
1252 | unsigned int sec_shndx; | |
1253 | bfd_byte *contents; | |
1254 | Elf_Internal_Rela *irel, *irelend; | |
1255 | Elf_Internal_Rela *irelalign; | |
1256 | Elf_Internal_Sym *isym; | |
1257 | Elf_Internal_Sym *isymend; | |
1258 | bfd_vma toaddr; | |
1259 | struct elf_link_hash_entry **sym_hashes; | |
1260 | struct elf_link_hash_entry **end_hashes; | |
1261 | unsigned int symcount; | |
1262 | ||
1263 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
1264 | ||
1265 | contents = elf_section_data (sec)->this_hdr.contents; | |
1266 | ||
1267 | /* The deletion must stop at the next ALIGN reloc for an aligment | |
1268 | power larger than the number of bytes we are deleting. */ | |
1269 | ||
1270 | irelalign = NULL; | |
1271 | toaddr = sec->_cooked_size; | |
1272 | ||
1273 | irel = elf_section_data (sec)->relocs; | |
1274 | irelend = irel + sec->reloc_count; | |
1275 | ||
1276 | /* Actually delete the bytes. */ | |
1277 | memmove (contents + addr, contents + addr + count, | |
1278 | (size_t) (toaddr - addr - count)); | |
1279 | sec->_cooked_size -= count; | |
1280 | ||
1281 | /* Adjust all the relocs. */ | |
1282 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) | |
1283 | { | |
1284 | /* Get the new reloc address. */ | |
1285 | if ((irel->r_offset > addr | |
1286 | && irel->r_offset < toaddr)) | |
1287 | irel->r_offset -= count; | |
1288 | } | |
1289 | ||
1290 | /* Adjust the local symbols defined in this section. */ | |
1291 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1292 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1293 | isymend = isym + symtab_hdr->sh_info; | |
1294 | for (; isym < isymend; isym++) | |
1295 | { | |
1296 | if (isym->st_shndx == sec_shndx | |
1297 | && isym->st_value > addr | |
1298 | && isym->st_value < toaddr) | |
1299 | isym->st_value -= count; | |
1300 | } | |
1301 | ||
1302 | /* Now adjust the global symbols defined in this section. */ | |
1303 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) | |
1304 | - symtab_hdr->sh_info); | |
1305 | sym_hashes = elf_sym_hashes (abfd); | |
1306 | end_hashes = sym_hashes + symcount; | |
1307 | for (; sym_hashes < end_hashes; sym_hashes++) | |
1308 | { | |
1309 | struct elf_link_hash_entry *sym_hash = *sym_hashes; | |
1310 | if ((sym_hash->root.type == bfd_link_hash_defined | |
1311 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1312 | && sym_hash->root.u.def.section == sec | |
1313 | && sym_hash->root.u.def.value > addr | |
1314 | && sym_hash->root.u.def.value < toaddr) | |
1315 | { | |
1316 | sym_hash->root.u.def.value -= count; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | return TRUE; | |
1321 | } | |
1322 | ||
1323 | /* Return TRUE if a symbol exists at the given address, else return | |
1324 | FALSE. */ | |
1325 | static bfd_boolean | |
1326 | elf32_h8_symbol_address_p (abfd, sec, addr) | |
1327 | bfd *abfd; | |
1328 | asection *sec; | |
1329 | bfd_vma addr; | |
1330 | { | |
1331 | Elf_Internal_Shdr *symtab_hdr; | |
1332 | unsigned int sec_shndx; | |
1333 | Elf_Internal_Sym *isym; | |
1334 | Elf_Internal_Sym *isymend; | |
1335 | struct elf_link_hash_entry **sym_hashes; | |
1336 | struct elf_link_hash_entry **end_hashes; | |
1337 | unsigned int symcount; | |
1338 | ||
1339 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
1340 | ||
1341 | /* Examine all the symbols. */ | |
1342 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1343 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1344 | isymend = isym + symtab_hdr->sh_info; | |
1345 | for (; isym < isymend; isym++) | |
1346 | { | |
1347 | if (isym->st_shndx == sec_shndx | |
1348 | && isym->st_value == addr) | |
1349 | return TRUE; | |
1350 | } | |
1351 | ||
1352 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) | |
1353 | - symtab_hdr->sh_info); | |
1354 | sym_hashes = elf_sym_hashes (abfd); | |
1355 | end_hashes = sym_hashes + symcount; | |
1356 | for (; sym_hashes < end_hashes; sym_hashes++) | |
1357 | { | |
1358 | struct elf_link_hash_entry *sym_hash = *sym_hashes; | |
1359 | if ((sym_hash->root.type == bfd_link_hash_defined | |
1360 | || sym_hash->root.type == bfd_link_hash_defweak) | |
1361 | && sym_hash->root.u.def.section == sec | |
1362 | && sym_hash->root.u.def.value == addr) | |
1363 | return TRUE; | |
1364 | } | |
1365 | ||
1366 | return FALSE; | |
1367 | } | |
1368 | ||
1369 | /* This is a version of bfd_generic_get_relocated_section_contents | |
1370 | which uses elf32_h8_relocate_section. */ | |
1371 | ||
1372 | static bfd_byte * | |
1373 | elf32_h8_get_relocated_section_contents (output_bfd, link_info, link_order, | |
1374 | data, relocateable, symbols) | |
1375 | bfd *output_bfd; | |
1376 | struct bfd_link_info *link_info; | |
1377 | struct bfd_link_order *link_order; | |
1378 | bfd_byte *data; | |
1379 | bfd_boolean relocateable; | |
1380 | asymbol **symbols; | |
1381 | { | |
1382 | Elf_Internal_Shdr *symtab_hdr; | |
1383 | asection *input_section = link_order->u.indirect.section; | |
1384 | bfd *input_bfd = input_section->owner; | |
1385 | asection **sections = NULL; | |
1386 | Elf_Internal_Rela *internal_relocs = NULL; | |
1387 | Elf_Internal_Sym *isymbuf = NULL; | |
1388 | ||
1389 | /* We only need to handle the case of relaxing, or of having a | |
1390 | particular set of section contents, specially. */ | |
1391 | if (relocateable | |
1392 | || elf_section_data (input_section)->this_hdr.contents == NULL) | |
1393 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, | |
1394 | link_order, data, | |
1395 | relocateable, | |
1396 | symbols); | |
1397 | ||
1398 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1399 | ||
1400 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, | |
1401 | (size_t) input_section->_raw_size); | |
1402 | ||
1403 | if ((input_section->flags & SEC_RELOC) != 0 | |
1404 | && input_section->reloc_count > 0) | |
1405 | { | |
1406 | asection **secpp; | |
1407 | Elf_Internal_Sym *isym, *isymend; | |
1408 | bfd_size_type amt; | |
1409 | ||
1410 | internal_relocs = (_bfd_elf_link_read_relocs | |
1411 | (input_bfd, input_section, (PTR) NULL, | |
1412 | (Elf_Internal_Rela *) NULL, FALSE)); | |
1413 | if (internal_relocs == NULL) | |
1414 | goto error_return; | |
1415 | ||
1416 | if (symtab_hdr->sh_info != 0) | |
1417 | { | |
1418 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1419 | if (isymbuf == NULL) | |
1420 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, | |
1421 | symtab_hdr->sh_info, 0, | |
1422 | NULL, NULL, NULL); | |
1423 | if (isymbuf == NULL) | |
1424 | goto error_return; | |
1425 | } | |
1426 | ||
1427 | amt = symtab_hdr->sh_info; | |
1428 | amt *= sizeof (asection *); | |
1429 | sections = (asection **) bfd_malloc (amt); | |
1430 | if (sections == NULL && amt != 0) | |
1431 | goto error_return; | |
1432 | ||
1433 | isymend = isymbuf + symtab_hdr->sh_info; | |
1434 | for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) | |
1435 | { | |
1436 | asection *isec; | |
1437 | ||
1438 | if (isym->st_shndx == SHN_UNDEF) | |
1439 | isec = bfd_und_section_ptr; | |
1440 | else if (isym->st_shndx == SHN_ABS) | |
1441 | isec = bfd_abs_section_ptr; | |
1442 | else if (isym->st_shndx == SHN_COMMON) | |
1443 | isec = bfd_com_section_ptr; | |
1444 | else | |
1445 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
1446 | ||
1447 | *secpp = isec; | |
1448 | } | |
1449 | ||
1450 | if (! elf32_h8_relocate_section (output_bfd, link_info, input_bfd, | |
1451 | input_section, data, internal_relocs, | |
1452 | isymbuf, sections)) | |
1453 | goto error_return; | |
1454 | ||
1455 | if (sections != NULL) | |
1456 | free (sections); | |
1457 | if (isymbuf != NULL | |
1458 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1459 | free (isymbuf); | |
1460 | if (elf_section_data (input_section)->relocs != internal_relocs) | |
1461 | free (internal_relocs); | |
1462 | } | |
1463 | ||
1464 | return data; | |
1465 | ||
1466 | error_return: | |
1467 | if (sections != NULL) | |
1468 | free (sections); | |
1469 | if (isymbuf != NULL | |
1470 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1471 | free (isymbuf); | |
1472 | if (internal_relocs != NULL | |
1473 | && elf_section_data (input_section)->relocs != internal_relocs) | |
1474 | free (internal_relocs); | |
1475 | return NULL; | |
1476 | } | |
1477 | ||
1478 | ||
1479 | #define TARGET_BIG_SYM bfd_elf32_h8300_vec | |
1480 | #define TARGET_BIG_NAME "elf32-h8300" | |
1481 | #define ELF_ARCH bfd_arch_h8300 | |
1482 | #define ELF_MACHINE_CODE EM_H8_300 | |
1483 | #define ELF_MAXPAGESIZE 0x1 | |
1484 | #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup | |
1485 | #define elf_info_to_howto elf32_h8_info_to_howto | |
1486 | #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel | |
1487 | ||
1488 | /* So we can set/examine bits in e_flags to get the specific | |
1489 | H8 architecture in use. */ | |
1490 | #define elf_backend_final_write_processing \ | |
1491 | elf32_h8_final_write_processing | |
1492 | #define elf_backend_object_p \ | |
1493 | elf32_h8_object_p | |
1494 | #define bfd_elf32_bfd_merge_private_bfd_data \ | |
1495 | elf32_h8_merge_private_bfd_data | |
1496 | ||
1497 | /* ??? when elf_backend_relocate_section is not defined, elf32-target.h | |
1498 | defaults to using _bfd_generic_link_hash_table_create, but | |
1499 | elflink.h:bfd_elf32_size_dynamic_sections uses | |
1500 | dynobj = elf_hash_table (info)->dynobj; | |
1501 | and thus requires an elf hash table. */ | |
1502 | #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create | |
1503 | ||
1504 | /* Use an H8 specific linker, not the ELF generic linker. */ | |
1505 | #define elf_backend_relocate_section elf32_h8_relocate_section | |
1506 | #define elf_backend_rela_normal 1 | |
1507 | ||
1508 | /* And relaxing stuff. */ | |
1509 | #define bfd_elf32_bfd_relax_section elf32_h8_relax_section | |
1510 | #define bfd_elf32_bfd_get_relocated_section_contents \ | |
1511 | elf32_h8_get_relocated_section_contents | |
1512 | ||
1513 | ||
1514 | #include "elf32-target.h" |