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