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