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