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