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