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