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