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