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