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