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