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