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