]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-h8300.c
2002-11-15 Klee Dienes <kdienes@apple.com>
[thirdparty/binutils-gdb.git] / bfd / elf32-h8300.c
CommitLineData
e01b0e69 1/* Generic support for 32-bit ELF
73ff0d56
AM
2 Copyright 1993, 1995, 1998, 1999, 2001, 2002
3 Free Software Foundation, Inc.
e01b0e69
JR
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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
JL
31static void elf32_h8_info_to_howto_rel
32 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
dc810e39 33static unsigned long elf32_h8_mach
5e47149d 34 PARAMS ((flagword));
dc810e39
AM
35static void elf32_h8_final_write_processing
36 PARAMS ((bfd *, boolean));
37static boolean elf32_h8_object_p
38 PARAMS ((bfd *));
39static boolean elf32_h8_merge_private_bfd_data
40 PARAMS ((bfd *, bfd *));
41static boolean elf32_h8_relax_section
42 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
43static boolean elf32_h8_relax_delete_bytes
44 PARAMS ((bfd *, asection *, bfd_vma, int));
45static 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 *,
49 bfd_byte *, 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));
54static boolean elf32_h8_relocate_section
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 */
70 false, /* pc_relative */
71 0, /* bitpos */
72 complain_overflow_dont, /* complain_on_overflow */
a00c9dbc 73 special, /* special_function */
e01b0e69
JR
74 "R_H8_NONE", /* name */
75 false, /* partial_inplace */
76 0, /* src_mask */
77 0, /* dst_mask */
78 false), /* pcrel_offset */
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 */
84 false, /* pc_relative */
85 0, /* bitpos */
86 complain_overflow_dont, /* complain_on_overflow */
a00c9dbc 87 special, /* special_function */
e01b0e69
JR
88 "R_H8_DIR32", /* name */
89 false, /* partial_inplace */
90 0, /* src_mask */
91 0xffffffff, /* dst_mask */
92 false), /* pcrel_offset */
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 */
98 false, /* pc_relative */
99 0, /* bitpos */
100 complain_overflow_dont, /* complain_on_overflow */
a00c9dbc 101 special, /* special_function */
e01b0e69
JR
102 "R_H8_DIR16", /* name */
103 false, /* partial_inplace */
104 0, /* src_mask */
105 0x0000ffff, /* dst_mask */
106 false), /* pcrel_offset */
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 */
112 false, /* pc_relative */
113 0, /* bitpos */
114 complain_overflow_dont, /* complain_on_overflow */
a00c9dbc 115 special, /* special_function */
e01b0e69
JR
116 "R_H8_DIR16", /* name */
117 false, /* partial_inplace */
118 0, /* src_mask */
119 0x000000ff, /* dst_mask */
120 false), /* pcrel_offset */
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 */
126 false, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_bitfield, /* complain_on_overflow */
a00c9dbc 129 special, /* special_function */
e01b0e69
JR
130 "R_H8_DIR16A8", /* name */
131 false, /* partial_inplace */
132 0, /* src_mask */
133 0x0000ffff, /* dst_mask */
134 false), /* pcrel_offset */
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 */
140 false, /* pc_relative */
141 0, /* bitpos */
142 complain_overflow_bitfield, /* complain_on_overflow */
a00c9dbc 143 special, /* special_function */
e01b0e69
JR
144 "R_H8_DIR16R8", /* name */
145 false, /* partial_inplace */
146 0, /* src_mask */
147 0x0000ffff, /* dst_mask */
148 false), /* pcrel_offset */
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 */
154 false, /* pc_relative */
155 0, /* bitpos */
156 complain_overflow_bitfield, /* complain_on_overflow */
a00c9dbc 157 special, /* special_function */
e01b0e69
JR
158 "R_H8_DIR24A8", /* name */
159 true, /* partial_inplace */
160 0xff000000, /* src_mask */
161 0x00ffffff, /* dst_mask */
162 false), /* pcrel_offset */
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 */
168 false, /* pc_relative */
169 0, /* bitpos */
170 complain_overflow_bitfield, /* complain_on_overflow */
a00c9dbc 171 special, /* special_function */
e01b0e69
JR
172 "R_H8_DIR24R8", /* name */
173 true, /* partial_inplace */
174 0xff000000, /* src_mask */
175 0x00ffffff, /* dst_mask */
176 false), /* pcrel_offset */
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 */
182 false, /* pc_relative */
183 0, /* bitpos */
184 complain_overflow_dont, /* complain_on_overflow */
a00c9dbc 185 special, /* special_function */
e01b0e69
JR
186 "R_H8_DIR32", /* name */
187 false, /* partial_inplace */
188 0, /* src_mask */
189 0xffffffff, /* dst_mask */
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 */
196 true, /* pc_relative */
197 0, /* bitpos */
198 complain_overflow_signed, /* complain_on_overflow */
a00c9dbc 199 special, /* special_function */
f2352488
JL
200 "R_H8_PCREL16", /* name */
201 false, /* partial_inplace */
202 0xffff, /* src_mask */
203 0xffff, /* dst_mask */
204 true), /* pcrel_offset */
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 */
210 true, /* pc_relative */
211 0, /* bitpos */
212 complain_overflow_signed, /* complain_on_overflow */
a00c9dbc 213 special, /* special_function */
f2352488
JL
214 "R_H8_PCREL8", /* name */
215 false, /* partial_inplace */
216 0xff, /* src_mask */
217 0xff, /* dst_mask */
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;
5e47149d 264 Elf32_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;
283 Elf32_Internal_Rel *elf_reloc ATTRIBUTE_UNUSED;
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. */
413static boolean
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
f0fe0e16
AM
429 if (info->relocateable)
430 return true;
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,
479 input_section, rel->r_offset, true)))
480 return false;
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)))
517 return false;
518 break;
519
520 case bfd_reloc_undefined:
521 if (! ((*info->callbacks->undefined_symbol)
522 (info, name, input_bfd, input_section,
523 rel->r_offset, true)))
524 return false;
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)))
547 return false;
548 break;
549 }
550 }
551 }
552
553 return true;
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;
576 }
577}
578
579/* The final processing done just before writing out a H8 ELF object
580 file. We use this opportunity to encode the BFD machine type
581 into the flags field in the object file. */
582
dc810e39 583static void
0a83638b
JL
584elf32_h8_final_write_processing (abfd, linker)
585 bfd *abfd;
586 boolean linker ATTRIBUTE_UNUSED;
587{
588 unsigned long val;
589
590 switch (bfd_get_mach (abfd))
591 {
592 default:
593 case bfd_mach_h8300:
594 val = E_H8_MACH_H8300;
595 break;
596
597 case bfd_mach_h8300h:
598 val = E_H8_MACH_H8300H;
599 break;
600
601 case bfd_mach_h8300s:
602 val = E_H8_MACH_H8300S;
603 break;
604 }
605
606 elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH);
607 elf_elfheader (abfd)->e_flags |= val;
608}
609
610/* Return nonzero if ABFD represents a valid H8 ELF object file; also
611 record the encoded machine type found in the ELF flags. */
612
dc810e39 613static boolean
0a83638b
JL
614elf32_h8_object_p (abfd)
615 bfd *abfd;
616{
617 bfd_default_set_arch_mach (abfd, bfd_arch_h8300,
618 elf32_h8_mach (elf_elfheader (abfd)->e_flags));
619 return true;
620}
621
622/* Merge backend specific data from an object file to the output
623 object file when linking. The only data we need to copy at this
624 time is the architecture/machine information. */
625
dc810e39 626static boolean
0a83638b
JL
627elf32_h8_merge_private_bfd_data (ibfd, obfd)
628 bfd *ibfd;
629 bfd *obfd;
630{
631 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
632 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
633 return true;
634
635 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
636 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
637 {
638 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
639 bfd_get_mach (ibfd)))
640 return false;
641 }
642
643 return true;
644}
645
5907e628
JL
646/* This function handles relaxing for the H8..
647
648 There's a few relaxing opportunites available on the H8:
649
650 jmp/jsr:24 -> bra/bsr:8 2 bytes
651 The jmp may be completely eliminated if the previous insn is a
652 conditional branch to the insn after the jump. In that case
653 we invert the branch and delete the jump and save 4 bytes.
654
655 bCC:16 -> bCC:8 2 bytes
656 bsr:16 -> bsr:8 2 bytes
657
658 mov.b:16 -> mov.b:8 2 bytes
659 mov.b:24/32 -> mov.b:8 4 bytes
660
76f99c63 661 mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes */
5907e628
JL
662
663static boolean
664elf32_h8_relax_section (abfd, sec, link_info, again)
665 bfd *abfd;
666 asection *sec;
667 struct bfd_link_info *link_info;
668 boolean *again;
669{
670 Elf_Internal_Shdr *symtab_hdr;
671 Elf_Internal_Rela *internal_relocs;
5907e628
JL
672 Elf_Internal_Rela *irel, *irelend;
673 bfd_byte *contents = NULL;
6cdc0ccc 674 Elf_Internal_Sym *isymbuf = NULL;
5907e628
JL
675 static asection *last_input_section = NULL;
676 static Elf_Internal_Rela *last_reloc = NULL;
677
678 /* Assume nothing changes. */
679 *again = false;
680
681 /* We don't have to do anything for a relocateable link, if
682 this section does not have relocs, or if this is not a
683 code section. */
684 if (link_info->relocateable
685 || (sec->flags & SEC_RELOC) == 0
686 || sec->reloc_count == 0
687 || (sec->flags & SEC_CODE) == 0)
688 return true;
689
690 /* If this is the first time we have been called for this section,
691 initialize the cooked size. */
692 if (sec->_cooked_size == 0)
693 sec->_cooked_size = sec->_raw_size;
694
695 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
696
697 /* Get a copy of the native relocations. */
698 internal_relocs = (_bfd_elf32_link_read_relocs
699 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
700 link_info->keep_memory));
701 if (internal_relocs == NULL)
702 goto error_return;
5907e628
JL
703
704 if (sec != last_input_section)
705 last_reloc = NULL;
706
707 last_input_section = sec;
708
709 /* Walk through the relocs looking for relaxing opportunities. */
710 irelend = internal_relocs + sec->reloc_count;
711 for (irel = internal_relocs; irel < irelend; irel++)
712 {
713 bfd_vma symval;
714
715 /* Keep track of the previous reloc so that we can delete
716 some long jumps created by the compiler. */
717 if (irel != internal_relocs)
718 last_reloc = irel - 1;
719
76f99c63
JL
720 if (ELF32_R_TYPE (irel->r_info) != R_H8_DIR24R8
721 && ELF32_R_TYPE (irel->r_info) != R_H8_PCREL16
722 && ELF32_R_TYPE (irel->r_info) != R_H8_DIR16A8
723 && ELF32_R_TYPE (irel->r_info) != R_H8_DIR24A8
724 && ELF32_R_TYPE (irel->r_info) != R_H8_DIR32A16)
725 continue;
726
5907e628
JL
727 /* Get the section contents if we haven't done so already. */
728 if (contents == NULL)
729 {
730 /* Get cached copy if it exists. */
731 if (elf_section_data (sec)->this_hdr.contents != NULL)
732 contents = elf_section_data (sec)->this_hdr.contents;
733 else
734 {
735 /* Go get them off disk. */
736 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
737 if (contents == NULL)
738 goto error_return;
5907e628
JL
739
740 if (! bfd_get_section_contents (abfd, sec, contents,
741 (file_ptr) 0, sec->_raw_size))
742 goto error_return;
743 }
744 }
745
9ad5cbcf 746 /* Read this BFD's local symbols if we haven't done so already. */
6cdc0ccc 747 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
5907e628 748 {
6cdc0ccc
AM
749 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
750 if (isymbuf == NULL)
751 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
752 symtab_hdr->sh_info, 0,
753 NULL, NULL, NULL);
754 if (isymbuf == NULL)
755 goto error_return;
5907e628
JL
756 }
757
758 /* Get the value of the symbol referred to by the reloc. */
759 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
760 {
5907e628 761 /* A local symbol. */
6cdc0ccc
AM
762 Elf_Internal_Sym *isym;
763 asection *sym_sec;
5907e628 764
32ac2c9a 765 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6cdc0ccc
AM
766 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
767 symval = (isym->st_value
5907e628
JL
768 + sym_sec->output_section->vma
769 + sym_sec->output_offset);
770 }
771 else
772 {
773 unsigned long indx;
774 struct elf_link_hash_entry *h;
775
776 /* An external symbol. */
777 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
778 h = elf_sym_hashes (abfd)[indx];
779 BFD_ASSERT (h != NULL);
780 if (h->root.type != bfd_link_hash_defined
781 && h->root.type != bfd_link_hash_defweak)
782 {
783 /* This appears to be a reference to an undefined
784 symbol. Just ignore it--it will be caught by the
785 regular reloc processing. */
786 continue;
787 }
788
789 symval = (h->root.u.def.value
790 + h->root.u.def.section->output_section->vma
791 + h->root.u.def.section->output_offset);
792 }
793
794 /* For simplicity of coding, we are going to modify the section
795 contents, the section relocs, and the BFD symbol table. We
796 must tell the rest of the code not to free up this
797 information. It would be possible to instead create a table
798 of changes which have to be made, as is done in coff-mips.c;
799 that would be more work, but would require less memory when
800 the linker is run. */
801 switch (ELF32_R_TYPE (irel->r_info))
802 {
803 /* Try to turn a 24 bit absolute branch/call into an 8 bit
804 pc-relative branch/call. */
805 case R_H8_DIR24R8:
806 {
807 bfd_vma value = symval + irel->r_addend;
808 bfd_vma dot, gap;
809
810 /* Get the address of this instruction. */
811 dot = (sec->output_section->vma
812 + sec->output_offset + irel->r_offset - 1);
813
814 /* Compute the distance from this insn to the branch target. */
815 gap = value - dot;
816
817 /* If the distance is within -126..+130 inclusive, then we can
818 relax this jump. +130 is valid since the target will move
819 two bytes closer if we do relax this branch. */
dc810e39 820 if ((int) gap >= -126 && (int) gap <= 130)
5907e628
JL
821 {
822 unsigned char code;
823
824 /* Note that we've changed the relocs, section contents,
825 etc. */
826 elf_section_data (sec)->relocs = internal_relocs;
5907e628 827 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 828 symtab_hdr->contents = (unsigned char *) isymbuf;
5907e628
JL
829
830 /* If the previous instruction conditionally jumped around
831 this instruction, we may be able to reverse the condition
832 and redirect the previous instruction to the target of
833 this instruction.
834
835 Such sequences are used by the compiler to deal with
836 long conditional branches. */
dc810e39
AM
837 if ((int) gap <= 130
838 && (int) gap >= -128
5907e628
JL
839 && last_reloc
840 && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8
841 && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info)
842 {
843 bfd_vma last_value;
844 asection *last_sym_sec;
6cdc0ccc 845 Elf_Internal_Sym *last_sym;
5907e628
JL
846
847 /* We will need to examine the symbol used by the
848 previous relocation. */
dc810e39 849
6cdc0ccc 850 last_sym = isymbuf + ELF32_R_SYM (last_reloc->r_info);
5907e628 851 last_sym_sec
6cdc0ccc
AM
852 = bfd_section_from_elf_index (abfd, last_sym->st_shndx);
853 last_value = (last_sym->st_value
5907e628
JL
854 + last_sym_sec->output_section->vma
855 + last_sym_sec->output_offset);
856
857 /* Verify that the previous relocation was for a
858 branch around this instruction and that no symbol
859 exists at the current location. */
860 if (last_value == dot + 4
861 && last_reloc->r_offset + 2 == irel->r_offset
9ad5cbcf 862 && ! elf32_h8_symbol_address_p (abfd, sec, dot))
5907e628
JL
863 {
864 /* We can eliminate this jump. Twiddle the
865 previous relocation as necessary. */
866 irel->r_info
867 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
868 ELF32_R_TYPE (R_H8_NONE));
869
bc7eab72 870 last_reloc->r_info
5907e628 871 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
bc7eab72
KH
872 ELF32_R_TYPE (R_H8_PCREL8));
873 last_reloc->r_addend = irel->r_addend;
5907e628
JL
874
875 code = bfd_get_8 (abfd,
876 contents + last_reloc->r_offset - 1);
877 code ^= 1;
878 bfd_put_8 (abfd,
879 code,
880 contents + last_reloc->r_offset - 1);
881
882 /* Delete four bytes of data. */
883 if (!elf32_h8_relax_delete_bytes (abfd, sec,
884 irel->r_offset - 1,
885 4))
886 goto error_return;
887
888 *again = true;
889 break;
890 }
891 }
892
893 /* We could not eliminate this jump, so just shorten it. */
894 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
895
896 if (code == 0x5e)
897 bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1);
898 else if (code == 0x5a)
899 bfd_put_8 (abfd, 0x40, contents + irel->r_offset - 1);
900 else
901 abort ();
902
903 /* Fix the relocation's type. */
904 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
905 R_H8_PCREL8);
906
907 /* Delete two bytes of data. */
908 if (!elf32_h8_relax_delete_bytes (abfd, sec,
909 irel->r_offset + 1, 2))
910 goto error_return;
911
912 /* That will change things, so, we should relax again.
913 Note that this is not required, and it may be slow. */
914 *again = true;
915 }
916 break;
917 }
918
919 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
920 branch. */
921 case R_H8_PCREL16:
922 {
923 bfd_vma value = symval + irel->r_addend;
924 bfd_vma dot;
925 bfd_vma gap;
926
927 /* Get the address of this instruction. */
928 dot = (sec->output_section->vma
929 + sec->output_offset
930 + irel->r_offset - 2);
dc810e39 931
5907e628
JL
932 gap = value - dot;
933
934 /* If the distance is within -126..+130 inclusive, then we can
935 relax this jump. +130 is valid since the target will move
936 two bytes closer if we do relax this branch. */
bc7eab72 937 if ((int) gap >= -126 && (int) gap <= 130)
5907e628 938 {
bc7eab72 939 unsigned char code;
5907e628 940
bc7eab72 941 /* Note that we've changed the relocs, section contents,
5907e628 942 etc. */
bc7eab72
KH
943 elf_section_data (sec)->relocs = internal_relocs;
944 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 945 symtab_hdr->contents = (unsigned char *) isymbuf;
5907e628 946
bc7eab72
KH
947 /* Get the opcode. */
948 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
5907e628
JL
949
950 if (code == 0x58)
951 {
952 /* bCC:16 -> bCC:8 */
953 /* Get the condition code from the original insn. */
954 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
955 code &= 0xf0;
956 code >>= 4;
957 code |= 0x40;
958 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
959 }
960 else if (code == 0x5c)
961 bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2);
962 else
963 abort ();
964
965 /* Fix the relocation's type. */
bc7eab72 966 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
5907e628 967 R_H8_PCREL8);
bc7eab72 968 irel->r_offset--;
5907e628 969
bc7eab72
KH
970 /* Delete two bytes of data. */
971 if (!elf32_h8_relax_delete_bytes (abfd, sec,
5907e628
JL
972 irel->r_offset + 1, 2))
973 goto error_return;
974
bc7eab72 975 /* That will change things, so, we should relax again.
5907e628 976 Note that this is not required, and it may be slow. */
bc7eab72 977 *again = true;
5907e628
JL
978 }
979 break;
980 }
981
982 /* This is a 16 bit absolute address in a "mov.b" insn, which may
983 become an 8 bit absolute address if its in the right range. */
984 case R_H8_DIR16A8:
985 {
986 bfd_vma value = symval + irel->r_addend;
987
988 if ((bfd_get_mach (abfd) == bfd_mach_h8300
bc7eab72
KH
989 && value >= 0xff00
990 && value <= 0xffff)
991 || ((bfd_get_mach (abfd) == bfd_mach_h8300h
5907e628
JL
992 || bfd_get_mach (abfd) == bfd_mach_h8300s)
993 && value >= 0xffff00
994 && value <= 0xffffff))
995 {
bc7eab72 996 unsigned char code;
5907e628 997
bc7eab72 998 /* Note that we've changed the relocs, section contents,
5907e628 999 etc. */
bc7eab72
KH
1000 elf_section_data (sec)->relocs = internal_relocs;
1001 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 1002 symtab_hdr->contents = (unsigned char *) isymbuf;
5907e628 1003
bc7eab72
KH
1004 /* Get the opcode. */
1005 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
5907e628 1006
bc7eab72
KH
1007 /* Sanity check. */
1008 if (code != 0x6a)
5907e628
JL
1009 abort ();
1010
bc7eab72 1011 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
5907e628 1012
bc7eab72 1013 if ((code & 0xf0) == 0x00)
5907e628
JL
1014 bfd_put_8 (abfd,
1015 (code & 0xf) | 0x20,
bc7eab72
KH
1016 contents + irel->r_offset - 2);
1017 else if ((code & 0xf0) == 0x80)
5907e628
JL
1018 bfd_put_8 (abfd,
1019 (code & 0xf) | 0x30,
bc7eab72
KH
1020 contents + irel->r_offset - 2);
1021 else
5907e628
JL
1022 abort ();
1023
bc7eab72
KH
1024 /* Fix the relocation's type. */
1025 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
5907e628
JL
1026 R_H8_DIR8);
1027
bc7eab72
KH
1028 /* Delete two bytes of data. */
1029 if (!elf32_h8_relax_delete_bytes (abfd, sec,
5907e628
JL
1030 irel->r_offset + 1, 2))
1031 goto error_return;
1032
bc7eab72 1033 /* That will change things, so, we should relax again.
5907e628 1034 Note that this is not required, and it may be slow. */
bc7eab72 1035 *again = true;
5907e628
JL
1036 }
1037 break;
1038 }
1039
1040 /* This is a 24 bit absolute address in a "mov.b" insn, which may
1041 become an 8 bit absolute address if its in the right range. */
1042 case R_H8_DIR24A8:
1043 {
1044 bfd_vma value = symval + irel->r_addend;
1045
1046 if ((bfd_get_mach (abfd) == bfd_mach_h8300
bc7eab72
KH
1047 && value >= 0xff00
1048 && value <= 0xffff)
1049 || ((bfd_get_mach (abfd) == bfd_mach_h8300h
5907e628
JL
1050 || bfd_get_mach (abfd) == bfd_mach_h8300s)
1051 && value >= 0xffff00
1052 && value <= 0xffffff))
1053 {
bc7eab72 1054 unsigned char code;
5907e628 1055
bc7eab72 1056 /* Note that we've changed the relocs, section contents,
5907e628 1057 etc. */
bc7eab72
KH
1058 elf_section_data (sec)->relocs = internal_relocs;
1059 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 1060 symtab_hdr->contents = (unsigned char *) isymbuf;
5907e628 1061
bc7eab72
KH
1062 /* Get the opcode. */
1063 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
5907e628 1064
bc7eab72
KH
1065 /* Sanity check. */
1066 if (code != 0x6a)
5907e628
JL
1067 abort ();
1068
bc7eab72 1069 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
5907e628 1070
bc7eab72 1071 if ((code & 0xf0) == 0x00)
5907e628
JL
1072 bfd_put_8 (abfd,
1073 (code & 0xf) | 0x20,
bc7eab72
KH
1074 contents + irel->r_offset - 2);
1075 else if ((code & 0xf0) == 0x80)
5907e628
JL
1076 bfd_put_8 (abfd,
1077 (code & 0xf) | 0x30,
bc7eab72
KH
1078 contents + irel->r_offset - 2);
1079 else
5907e628
JL
1080 abort ();
1081
bc7eab72
KH
1082 /* Fix the relocation's type. */
1083 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
5907e628
JL
1084 R_H8_DIR8);
1085
bc7eab72
KH
1086 /* Delete two bytes of data. */
1087 if (!elf32_h8_relax_delete_bytes (abfd, sec, irel->r_offset, 2))
5907e628
JL
1088 goto error_return;
1089
bc7eab72 1090 /* That will change things, so, we should relax again.
5907e628 1091 Note that this is not required, and it may be slow. */
bc7eab72 1092 *again = true;
5907e628
JL
1093 }
1094 }
1095
1096 /* FALLTHRU */
1097
1098 /* This is a 24/32bit absolute address in a "mov" insn, which may
1099 become a 16bit absoulte address if it is in the right range. */
1100 case R_H8_DIR32A16:
1101 {
1102 bfd_vma value = symval + irel->r_addend;
1103
1104 if (value <= 0x7fff || value >= 0xff8000)
1105 {
bc7eab72 1106 unsigned char code;
5907e628 1107
bc7eab72 1108 /* Note that we've changed the relocs, section contents,
5907e628 1109 etc. */
bc7eab72
KH
1110 elf_section_data (sec)->relocs = internal_relocs;
1111 elf_section_data (sec)->this_hdr.contents = contents;
6cdc0ccc 1112 symtab_hdr->contents = (unsigned char *) isymbuf;
5907e628 1113
bc7eab72
KH
1114 /* Get the opcode. */
1115 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
5907e628 1116
bc7eab72
KH
1117 /* We just need to turn off bit 0x20. */
1118 code &= ~0x20;
5907e628 1119
bc7eab72 1120 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
5907e628 1121
bc7eab72
KH
1122 /* Fix the relocation's type. */
1123 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
5907e628
JL
1124 R_H8_DIR16A8);
1125
bc7eab72
KH
1126 /* Delete two bytes of data. */
1127 if (!elf32_h8_relax_delete_bytes (abfd, sec,
5907e628
JL
1128 irel->r_offset + 1, 2))
1129 goto error_return;
1130
bc7eab72 1131 /* That will change things, so, we should relax again.
5907e628 1132 Note that this is not required, and it may be slow. */
bc7eab72 1133 *again = true;
5907e628
JL
1134 }
1135 break;
1136 }
1137
1138 default:
1139 break;
1140 }
1141 }
1142
6cdc0ccc
AM
1143 if (isymbuf != NULL
1144 && symtab_hdr->contents != (unsigned char *) isymbuf)
5907e628 1145 {
6cdc0ccc
AM
1146 if (! link_info->keep_memory)
1147 free (isymbuf);
1148 else
1149 symtab_hdr->contents = (unsigned char *) isymbuf;
5907e628
JL
1150 }
1151
6cdc0ccc
AM
1152 if (contents != NULL
1153 && elf_section_data (sec)->this_hdr.contents != contents)
5907e628
JL
1154 {
1155 if (! link_info->keep_memory)
6cdc0ccc 1156 free (contents);
5907e628
JL
1157 else
1158 {
1159 /* Cache the section contents for elf_link_input_bfd. */
1160 elf_section_data (sec)->this_hdr.contents = contents;
1161 }
9ad5cbcf
AM
1162 }
1163
6cdc0ccc
AM
1164 if (internal_relocs != NULL
1165 && elf_section_data (sec)->relocs != internal_relocs)
1166 free (internal_relocs);
5907e628
JL
1167
1168 return true;
1169
1170 error_return:
6cdc0ccc
AM
1171 if (isymbuf != NULL
1172 && symtab_hdr->contents != (unsigned char *) isymbuf)
1173 free (isymbuf);
1174 if (contents != NULL
1175 && elf_section_data (sec)->this_hdr.contents != contents)
1176 free (contents);
1177 if (internal_relocs != NULL
1178 && elf_section_data (sec)->relocs != internal_relocs)
1179 free (internal_relocs);
5907e628
JL
1180 return false;
1181}
1182
1183/* Delete some bytes from a section while relaxing. */
1184
1185static boolean
1186elf32_h8_relax_delete_bytes (abfd, sec, addr, count)
1187 bfd *abfd;
1188 asection *sec;
1189 bfd_vma addr;
1190 int count;
1191{
1192 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 1193 unsigned int sec_shndx;
5907e628
JL
1194 bfd_byte *contents;
1195 Elf_Internal_Rela *irel, *irelend;
1196 Elf_Internal_Rela *irelalign;
6cdc0ccc
AM
1197 Elf_Internal_Sym *isym;
1198 Elf_Internal_Sym *isymend;
5907e628 1199 bfd_vma toaddr;
9ad5cbcf
AM
1200 struct elf_link_hash_entry **sym_hashes;
1201 struct elf_link_hash_entry **end_hashes;
1202 unsigned int symcount;
5907e628 1203
9ad5cbcf 1204 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5907e628
JL
1205
1206 contents = elf_section_data (sec)->this_hdr.contents;
1207
1208 /* The deletion must stop at the next ALIGN reloc for an aligment
1209 power larger than the number of bytes we are deleting. */
1210
1211 irelalign = NULL;
1212 toaddr = sec->_cooked_size;
1213
1214 irel = elf_section_data (sec)->relocs;
1215 irelend = irel + sec->reloc_count;
1216
1217 /* Actually delete the bytes. */
dc810e39
AM
1218 memmove (contents + addr, contents + addr + count,
1219 (size_t) (toaddr - addr - count));
5907e628
JL
1220 sec->_cooked_size -= count;
1221
1222 /* Adjust all the relocs. */
1223 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1224 {
1225 /* Get the new reloc address. */
1226 if ((irel->r_offset > addr
1227 && irel->r_offset < toaddr))
1228 irel->r_offset -= count;
1229 }
1230
1231 /* Adjust the local symbols defined in this section. */
6cdc0ccc
AM
1232 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1233 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1234 isymend = isym + symtab_hdr->sh_info;
1235 for (; isym < isymend; isym++)
5907e628 1236 {
6cdc0ccc
AM
1237 if (isym->st_shndx == sec_shndx
1238 && isym->st_value > addr
1239 && isym->st_value < toaddr)
1240 isym->st_value -= count;
5907e628
JL
1241 }
1242
1243 /* Now adjust the global symbols defined in this section. */
9ad5cbcf
AM
1244 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1245 - symtab_hdr->sh_info);
1246 sym_hashes = elf_sym_hashes (abfd);
1247 end_hashes = sym_hashes + symcount;
1248 for (; sym_hashes < end_hashes; sym_hashes++)
5907e628 1249 {
9ad5cbcf
AM
1250 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1251 if ((sym_hash->root.type == bfd_link_hash_defined
1252 || sym_hash->root.type == bfd_link_hash_defweak)
1253 && sym_hash->root.u.def.section == sec
1254 && sym_hash->root.u.def.value > addr
1255 && sym_hash->root.u.def.value < toaddr)
5907e628 1256 {
9ad5cbcf 1257 sym_hash->root.u.def.value -= count;
5907e628
JL
1258 }
1259 }
1260
1261 return true;
1262}
1263
1264/* Return true if a symbol exists at the given address, else return
1265 false. */
1266static boolean
9ad5cbcf 1267elf32_h8_symbol_address_p (abfd, sec, addr)
5907e628
JL
1268 bfd *abfd;
1269 asection *sec;
5907e628
JL
1270 bfd_vma addr;
1271{
1272 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 1273 unsigned int sec_shndx;
6cdc0ccc
AM
1274 Elf_Internal_Sym *isym;
1275 Elf_Internal_Sym *isymend;
9ad5cbcf
AM
1276 struct elf_link_hash_entry **sym_hashes;
1277 struct elf_link_hash_entry **end_hashes;
1278 unsigned int symcount;
5907e628 1279
9ad5cbcf 1280 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5907e628
JL
1281
1282 /* Examine all the symbols. */
9ad5cbcf 1283 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
1284 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1285 isymend = isym + symtab_hdr->sh_info;
1286 for (; isym < isymend; isym++)
5907e628 1287 {
6cdc0ccc
AM
1288 if (isym->st_shndx == sec_shndx
1289 && isym->st_value == addr)
5907e628
JL
1290 return true;
1291 }
1292
9ad5cbcf
AM
1293 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1294 - symtab_hdr->sh_info);
1295 sym_hashes = elf_sym_hashes (abfd);
1296 end_hashes = sym_hashes + symcount;
1297 for (; sym_hashes < end_hashes; sym_hashes++)
5907e628 1298 {
9ad5cbcf
AM
1299 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1300 if ((sym_hash->root.type == bfd_link_hash_defined
1301 || sym_hash->root.type == bfd_link_hash_defweak)
1302 && sym_hash->root.u.def.section == sec
1303 && sym_hash->root.u.def.value == addr)
5907e628
JL
1304 return true;
1305 }
9ad5cbcf 1306
5907e628
JL
1307 return false;
1308}
1309
1310/* This is a version of bfd_generic_get_relocated_section_contents
1311 which uses elf32_h8_relocate_section. */
1312
1313static bfd_byte *
1314elf32_h8_get_relocated_section_contents (output_bfd, link_info, link_order,
1315 data, relocateable, symbols)
1316 bfd *output_bfd;
1317 struct bfd_link_info *link_info;
1318 struct bfd_link_order *link_order;
1319 bfd_byte *data;
1320 boolean relocateable;
1321 asymbol **symbols;
1322{
1323 Elf_Internal_Shdr *symtab_hdr;
1324 asection *input_section = link_order->u.indirect.section;
1325 bfd *input_bfd = input_section->owner;
1326 asection **sections = NULL;
1327 Elf_Internal_Rela *internal_relocs = NULL;
6cdc0ccc 1328 Elf_Internal_Sym *isymbuf = NULL;
5907e628
JL
1329
1330 /* We only need to handle the case of relaxing, or of having a
1331 particular set of section contents, specially. */
1332 if (relocateable
1333 || elf_section_data (input_section)->this_hdr.contents == NULL)
1334 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1335 link_order, data,
1336 relocateable,
1337 symbols);
1338
1339 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1340
1341 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
dc810e39 1342 (size_t) input_section->_raw_size);
5907e628
JL
1343
1344 if ((input_section->flags & SEC_RELOC) != 0
1345 && input_section->reloc_count > 0)
1346 {
5907e628 1347 asection **secpp;
6cdc0ccc 1348 Elf_Internal_Sym *isym, *isymend;
9ad5cbcf 1349 bfd_size_type amt;
5907e628 1350
5907e628
JL
1351 internal_relocs = (_bfd_elf32_link_read_relocs
1352 (input_bfd, input_section, (PTR) NULL,
1353 (Elf_Internal_Rela *) NULL, false));
1354 if (internal_relocs == NULL)
1355 goto error_return;
1356
6cdc0ccc
AM
1357 if (symtab_hdr->sh_info != 0)
1358 {
1359 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1360 if (isymbuf == NULL)
1361 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1362 symtab_hdr->sh_info, 0,
1363 NULL, NULL, NULL);
1364 if (isymbuf == NULL)
1365 goto error_return;
1366 }
5907e628 1367
9ad5cbcf
AM
1368 amt = symtab_hdr->sh_info;
1369 amt *= sizeof (asection *);
1370 sections = (asection **) bfd_malloc (amt);
1371 if (sections == NULL && amt != 0)
5907e628
JL
1372 goto error_return;
1373
6cdc0ccc
AM
1374 isymend = isymbuf + symtab_hdr->sh_info;
1375 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5907e628
JL
1376 {
1377 asection *isec;
1378
6cdc0ccc 1379 if (isym->st_shndx == SHN_UNDEF)
5907e628 1380 isec = bfd_und_section_ptr;
6cdc0ccc 1381 else if (isym->st_shndx == SHN_ABS)
5907e628 1382 isec = bfd_abs_section_ptr;
6cdc0ccc 1383 else if (isym->st_shndx == SHN_COMMON)
5907e628
JL
1384 isec = bfd_com_section_ptr;
1385 else
6cdc0ccc 1386 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5907e628
JL
1387
1388 *secpp = isec;
1389 }
1390
1391 if (! elf32_h8_relocate_section (output_bfd, link_info, input_bfd,
1392 input_section, data, internal_relocs,
6cdc0ccc 1393 isymbuf, sections))
5907e628
JL
1394 goto error_return;
1395
1396 if (sections != NULL)
1397 free (sections);
6cdc0ccc
AM
1398 if (isymbuf != NULL
1399 && symtab_hdr->contents != (unsigned char *) isymbuf)
1400 free (isymbuf);
1401 if (elf_section_data (input_section)->relocs != internal_relocs)
5907e628 1402 free (internal_relocs);
5907e628
JL
1403 }
1404
1405 return data;
1406
1407 error_return:
5907e628
JL
1408 if (sections != NULL)
1409 free (sections);
6cdc0ccc
AM
1410 if (isymbuf != NULL
1411 && symtab_hdr->contents != (unsigned char *) isymbuf)
1412 free (isymbuf);
1413 if (internal_relocs != NULL
1414 && elf_section_data (input_section)->relocs != internal_relocs)
1415 free (internal_relocs);
5907e628
JL
1416 return NULL;
1417}
1418
0a83638b 1419
e01b0e69
JR
1420#define TARGET_BIG_SYM bfd_elf32_h8300_vec
1421#define TARGET_BIG_NAME "elf32-h8300"
1422#define ELF_ARCH bfd_arch_h8300
1423#define ELF_MACHINE_CODE EM_H8_300
1424#define ELF_MAXPAGESIZE 0x1
1425#define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
1426#define elf_info_to_howto elf32_h8_info_to_howto
1427#define elf_info_to_howto_rel elf32_h8_info_to_howto_rel
1428
0a83638b
JL
1429/* So we can set/examine bits in e_flags to get the specific
1430 H8 architecture in use. */
1431#define elf_backend_final_write_processing \
1432 elf32_h8_final_write_processing
1433#define elf_backend_object_p \
1434 elf32_h8_object_p
1435#define bfd_elf32_bfd_merge_private_bfd_data \
1436 elf32_h8_merge_private_bfd_data
1437
e01b0e69
JR
1438/* ??? when elf_backend_relocate_section is not defined, elf32-target.h
1439 defaults to using _bfd_generic_link_hash_table_create, but
1440 elflink.h:bfd_elf32_size_dynamic_sections uses
1441 dynobj = elf_hash_table (info)->dynobj;
1442 and thus requires an elf hash table. */
1443#define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
1444
5e47149d
JL
1445/* Use an H8 specific linker, not the ELF generic linker. */
1446#define elf_backend_relocate_section elf32_h8_relocate_section
f0fe0e16 1447#define elf_backend_rela_normal 1
5e47149d 1448
5907e628
JL
1449/* And relaxing stuff. */
1450#define bfd_elf32_bfd_relax_section elf32_h8_relax_section
1451#define bfd_elf32_bfd_get_relocated_section_contents \
1452 elf32_h8_get_relocated_section_contents
1453
1454
e01b0e69 1455#include "elf32-target.h"