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