* gas/config/tc-m68k.c (find_cf_chip): Merge into ...
[binutils.git] / bfd / elf32-m32c.c
blobf56efd2e4da026e02e70749e619e4a8f8645cf1e
1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/m32c.h"
26 #include "libiberty.h"
28 /* Forward declarations. */
29 static reloc_howto_type * m32c_reloc_type_lookup
30 (bfd *, bfd_reloc_code_real_type);
31 static void m32c_info_to_howto_rela
32 (bfd *, arelent *, Elf_Internal_Rela *);
33 static bfd_boolean m32c_elf_relocate_section
34 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
35 static bfd_boolean m32c_elf_gc_sweep_hook
36 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
37 static asection * m32c_elf_gc_mark_hook
38 (asection *, struct bfd_link_info *, Elf_Internal_Rela *, struct elf_link_hash_entry *, Elf_Internal_Sym *);
39 static bfd_boolean m32c_elf_check_relocs
40 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
41 static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
42 #ifdef DEBUG
43 static char * m32c_get_reloc (long reloc);
44 #endif
45 static bfd_boolean m32c_elf_relax_section
46 (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
49 static reloc_howto_type m32c_elf_howto_table [] =
51 /* This reloc does nothing. */
52 HOWTO (R_M32C_NONE, /* type */
53 0, /* rightshift */
54 0, /* size (0 = byte, 1 = short, 2 = long) */
55 32, /* bitsize */
56 FALSE, /* pc_relative */
57 0, /* bitpos */
58 complain_overflow_bitfield, /* complain_on_overflow */
59 bfd_elf_generic_reloc, /* special_function */
60 "R_M32C_NONE", /* name */
61 FALSE, /* partial_inplace */
62 0, /* src_mask */
63 0, /* dst_mask */
64 FALSE), /* pcrel_offset */
66 HOWTO (R_M32C_16, /* type */
67 0, /* rightshift */
68 1, /* size (0 = byte, 1 = short, 2 = long) */
69 16, /* bitsize */
70 FALSE, /* pc_relative */
71 0, /* bitpos */
72 complain_overflow_bitfield, /* complain_on_overflow */
73 bfd_elf_generic_reloc, /* special_function */
74 "R_M32C_16", /* name */
75 FALSE, /* partial_inplace */
76 0, /* src_mask */
77 0xffff, /* dst_mask */
78 FALSE), /* pcrel_offset */
80 HOWTO (R_M32C_24, /* type */
81 0, /* rightshift */
82 2, /* size (0 = byte, 1 = short, 2 = long) */
83 24, /* bitsize */
84 FALSE, /* pc_relative */
85 0, /* bitpos */
86 complain_overflow_bitfield, /* complain_on_overflow */
87 bfd_elf_generic_reloc, /* special_function */
88 "R_M32C_24", /* name */
89 FALSE, /* partial_inplace */
90 0, /* src_mask */
91 0xffffff, /* dst_mask */
92 FALSE), /* pcrel_offset */
94 HOWTO (R_M32C_32, /* type */
95 0, /* rightshift */
96 2, /* size (0 = byte, 1 = short, 2 = long) */
97 32, /* bitsize */
98 FALSE, /* pc_relative */
99 0, /* bitpos */
100 complain_overflow_bitfield, /* complain_on_overflow */
101 bfd_elf_generic_reloc, /* special_function */
102 "R_M32C_32", /* name */
103 FALSE, /* partial_inplace */
104 0, /* src_mask */
105 0xffffffff, /* dst_mask */
106 FALSE), /* pcrel_offset */
108 HOWTO (R_M32C_8_PCREL, /* type */
109 0, /* rightshift */
110 0, /* size (0 = byte, 1 = short, 2 = long) */
111 8, /* bitsize */
112 TRUE, /* pc_relative */
113 0, /* bitpos */
114 complain_overflow_signed, /* complain_on_overflow */
115 bfd_elf_generic_reloc, /* special_function */
116 "R_M32C_8_PCREL", /* name */
117 FALSE, /* partial_inplace */
118 0, /* src_mask */
119 0xff, /* dst_mask */
120 TRUE), /* pcrel_offset */
122 HOWTO (R_M32C_16_PCREL, /* type */
123 0, /* rightshift */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
125 16, /* bitsize */
126 TRUE, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_signed, /* complain_on_overflow */
129 bfd_elf_generic_reloc, /* special_function */
130 "R_M32C_16_PCREL", /* name */
131 FALSE, /* partial_inplace */
132 0, /* src_mask */
133 0xffff, /* dst_mask */
134 TRUE), /* pcrel_offset */
136 HOWTO (R_M32C_8, /* type */
137 0, /* rightshift */
138 0, /* size (0 = byte, 1 = short, 2 = long) */
139 8, /* bitsize */
140 FALSE, /* pc_relative */
141 0, /* bitpos */
142 complain_overflow_unsigned, /* complain_on_overflow */
143 bfd_elf_generic_reloc, /* special_function */
144 "R_M32C_8", /* name */
145 FALSE, /* partial_inplace */
146 0, /* src_mask */
147 0xff, /* dst_mask */
148 FALSE), /* pcrel_offset */
150 HOWTO (R_M32C_LO16, /* type */
151 0, /* rightshift */
152 1, /* size (0 = byte, 1 = short, 2 = long) */
153 16, /* bitsize */
154 FALSE, /* pc_relative */
155 0, /* bitpos */
156 complain_overflow_dont, /* complain_on_overflow */
157 bfd_elf_generic_reloc, /* special_function */
158 "R_M32C_LO16", /* name */
159 FALSE, /* partial_inplace */
160 0, /* src_mask */
161 0xffff, /* dst_mask */
162 FALSE), /* pcrel_offset */
164 HOWTO (R_M32C_HI8, /* type */
165 0, /* rightshift */
166 0, /* size (0 = byte, 1 = short, 2 = long) */
167 8, /* bitsize */
168 FALSE, /* pc_relative */
169 0, /* bitpos */
170 complain_overflow_dont, /* complain_on_overflow */
171 bfd_elf_generic_reloc, /* special_function */
172 "R_M32C_HI8", /* name */
173 FALSE, /* partial_inplace */
174 0, /* src_mask */
175 0xff, /* dst_mask */
176 FALSE), /* pcrel_offset */
178 HOWTO (R_M32C_HI16, /* type */
179 0, /* rightshift */
180 1, /* size (0 = byte, 1 = short, 2 = long) */
181 16, /* bitsize */
182 FALSE, /* pc_relative */
183 0, /* bitpos */
184 complain_overflow_dont, /* complain_on_overflow */
185 bfd_elf_generic_reloc, /* special_function */
186 "R_M32C_HI16", /* name */
187 FALSE, /* partial_inplace */
188 0, /* src_mask */
189 0xffff, /* dst_mask */
190 FALSE), /* pcrel_offset */
192 HOWTO (R_M32C_RL_JUMP, /* type */
193 0, /* rightshift */
194 0, /* size (0 = byte, 1 = short, 2 = long) */
195 0, /* bitsize */
196 FALSE, /* pc_relative */
197 0, /* bitpos */
198 complain_overflow_signed, /* complain_on_overflow */
199 bfd_elf_generic_reloc, /* special_function */
200 "R_M32C_RL_JUMP", /* name */
201 FALSE, /* partial_inplace */
202 0, /* src_mask */
203 0, /* dst_mask */
204 FALSE), /* pcrel_offset */
206 HOWTO (R_M32C_RL_1ADDR, /* type */
207 0, /* rightshift */
208 0, /* size (0 = byte, 1 = short, 2 = long) */
209 0, /* bitsize */
210 FALSE, /* pc_relative */
211 0, /* bitpos */
212 complain_overflow_signed, /* complain_on_overflow */
213 bfd_elf_generic_reloc, /* special_function */
214 "R_M32C_RL_1ADDR", /* name */
215 FALSE, /* partial_inplace */
216 0, /* src_mask */
217 0, /* dst_mask */
218 FALSE), /* pcrel_offset */
220 HOWTO (R_M32C_RL_2ADDR, /* type */
221 0, /* rightshift */
222 0, /* size (0 = byte, 1 = short, 2 = long) */
223 0, /* bitsize */
224 FALSE, /* pc_relative */
225 0, /* bitpos */
226 complain_overflow_signed, /* complain_on_overflow */
227 bfd_elf_generic_reloc, /* special_function */
228 "R_M32C_RL_2ADDR", /* name */
229 FALSE, /* partial_inplace */
230 0, /* src_mask */
231 0, /* dst_mask */
232 FALSE), /* pcrel_offset */
236 /* Map BFD reloc types to M32C ELF reloc types. */
238 struct m32c_reloc_map
240 bfd_reloc_code_real_type bfd_reloc_val;
241 unsigned int m32c_reloc_val;
244 static const struct m32c_reloc_map m32c_reloc_map [] =
246 { BFD_RELOC_NONE, R_M32C_NONE },
247 { BFD_RELOC_16, R_M32C_16 },
248 { BFD_RELOC_24, R_M32C_24 },
249 { BFD_RELOC_32, R_M32C_32 },
250 { BFD_RELOC_8_PCREL, R_M32C_8_PCREL },
251 { BFD_RELOC_16_PCREL, R_M32C_16_PCREL },
252 { BFD_RELOC_8, R_M32C_8 },
253 { BFD_RELOC_LO16, R_M32C_LO16 },
254 { BFD_RELOC_HI16, R_M32C_HI16 },
255 { BFD_RELOC_M32C_HI8, R_M32C_HI8 },
256 { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
257 { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR },
258 { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR }
261 static reloc_howto_type *
262 m32c_reloc_type_lookup
263 (bfd * abfd ATTRIBUTE_UNUSED,
264 bfd_reloc_code_real_type code)
266 unsigned int i;
268 for (i = ARRAY_SIZE (m32c_reloc_map); --i;)
269 if (m32c_reloc_map [i].bfd_reloc_val == code)
270 return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
272 return NULL;
275 /* Set the howto pointer for an M32C ELF reloc. */
277 static void
278 m32c_info_to_howto_rela
279 (bfd * abfd ATTRIBUTE_UNUSED,
280 arelent * cache_ptr,
281 Elf_Internal_Rela * dst)
283 unsigned int r_type;
285 r_type = ELF32_R_TYPE (dst->r_info);
286 BFD_ASSERT (r_type < (unsigned int) R_M32C_max);
287 cache_ptr->howto = & m32c_elf_howto_table [r_type];
292 /* Relocate an M32C ELF section.
293 There is some attempt to make this function usable for many architectures,
294 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
295 if only to serve as a learning tool.
297 The RELOCATE_SECTION function is called by the new ELF backend linker
298 to handle the relocations for a section.
300 The relocs are always passed as Rela structures; if the section
301 actually uses Rel structures, the r_addend field will always be
302 zero.
304 This function is responsible for adjusting the section contents as
305 necessary, and (if using Rela relocs and generating a relocatable
306 output file) adjusting the reloc addend as necessary.
308 This function does not have to worry about setting the reloc
309 address or the reloc symbol index.
311 LOCAL_SYMS is a pointer to the swapped in local symbols.
313 LOCAL_SECTIONS is an array giving the section in the input file
314 corresponding to the st_shndx field of each local symbol.
316 The global hash table entry for the global symbols can be found
317 via elf_sym_hashes (input_bfd).
319 When generating relocatable output, this function must handle
320 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
321 going to be the section symbol corresponding to the output
322 section, which means that the addend must be adjusted
323 accordingly. */
325 static bfd_boolean
326 m32c_elf_relocate_section
327 (bfd * output_bfd ATTRIBUTE_UNUSED,
328 struct bfd_link_info * info,
329 bfd * input_bfd,
330 asection * input_section,
331 bfd_byte * contents,
332 Elf_Internal_Rela * relocs,
333 Elf_Internal_Sym * local_syms,
334 asection ** local_sections)
336 Elf_Internal_Shdr * symtab_hdr;
337 struct elf_link_hash_entry ** sym_hashes;
338 Elf_Internal_Rela * rel;
339 Elf_Internal_Rela * relend;
340 bfd *dynobj;
341 asection *splt;
343 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
344 sym_hashes = elf_sym_hashes (input_bfd);
345 relend = relocs + input_section->reloc_count;
347 dynobj = elf_hash_table (info)->dynobj;
348 splt = NULL;
349 if (dynobj != NULL)
350 splt = bfd_get_section_by_name (dynobj, ".plt");
352 for (rel = relocs; rel < relend; rel ++)
354 reloc_howto_type * howto;
355 unsigned long r_symndx;
356 Elf_Internal_Sym * sym;
357 asection * sec;
358 struct elf_link_hash_entry * h;
359 bfd_vma relocation;
360 bfd_reloc_status_type r;
361 const char * name = NULL;
362 int r_type;
364 r_type = ELF32_R_TYPE (rel->r_info);
366 /* These are only used for relaxing; we don't actually relocate
367 anything with them, so skip them. */
368 if (r_type == R_M32C_RL_JUMP
369 || r_type == R_M32C_RL_1ADDR
370 || r_type == R_M32C_RL_2ADDR)
371 continue;
373 r_symndx = ELF32_R_SYM (rel->r_info);
375 if (info->relocatable)
377 /* This is a relocatable link. We don't have to change
378 anything, unless the reloc is against a section symbol,
379 in which case we have to adjust according to where the
380 section symbol winds up in the output section. */
381 if (r_symndx < symtab_hdr->sh_info)
383 sym = local_syms + r_symndx;
385 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
387 sec = local_sections [r_symndx];
388 rel->r_addend += sec->output_offset + sym->st_value;
392 continue;
395 /* This is a final link. */
396 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
397 h = NULL;
398 sym = NULL;
399 sec = NULL;
401 if (r_symndx < symtab_hdr->sh_info)
403 sym = local_syms + r_symndx;
404 sec = local_sections [r_symndx];
405 relocation = (sec->output_section->vma
406 + sec->output_offset
407 + sym->st_value);
409 name = bfd_elf_string_from_elf_section
410 (input_bfd, symtab_hdr->sh_link, sym->st_name);
411 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
413 else
415 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
417 while (h->root.type == bfd_link_hash_indirect
418 || h->root.type == bfd_link_hash_warning)
419 h = (struct elf_link_hash_entry *) h->root.u.i.link;
421 name = h->root.root.string;
423 if (h->root.type == bfd_link_hash_defined
424 || h->root.type == bfd_link_hash_defweak)
426 sec = h->root.u.def.section;
427 relocation = (h->root.u.def.value
428 + sec->output_section->vma
429 + sec->output_offset);
431 else if (h->root.type == bfd_link_hash_undefweak)
433 relocation = 0;
435 else
437 if (! ((*info->callbacks->undefined_symbol)
438 (info, h->root.root.string, input_bfd,
439 input_section, rel->r_offset, TRUE)))
440 return FALSE;
441 relocation = 0;
445 switch (ELF32_R_TYPE (rel->r_info))
447 case R_M32C_16:
449 bfd_vma *plt_offset;
451 if (h != NULL)
452 plt_offset = &h->plt.offset;
453 else
454 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
456 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
457 relocation, *plt_offset);*/
458 if (relocation <= 0xffff)
460 /* If the symbol is in range for a 16-bit address, we should
461 have deallocated the plt entry in relax_section. */
462 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
464 else
466 /* If the symbol is out of range for a 16-bit address,
467 we must have allocated a plt entry. */
468 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
470 /* If this is the first time we've processed this symbol,
471 fill in the plt entry with the correct symbol address. */
472 if ((*plt_offset & 1) == 0)
474 unsigned int x;
476 x = 0x000000fc; /* jmpf */
477 x |= (relocation << 8) & 0xffffff00;
478 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
479 *plt_offset |= 1;
482 relocation = (splt->output_section->vma
483 + splt->output_offset
484 + (*plt_offset & -2));
487 break;
489 case R_M32C_HI8:
490 case R_M32C_HI16:
491 relocation >>= 16;
492 break;
495 #if 0
496 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
497 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
498 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
499 relocation, rel->r_addend);
501 int i;
502 for (i=0; i<4; i++)
503 printf (" %02x", contents[rel->r_offset+i]);
504 printf ("\n");
506 #endif
507 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
508 contents, rel->r_offset, relocation,
509 rel->r_addend);
511 if (r != bfd_reloc_ok)
513 const char * msg = (const char *) NULL;
515 switch (r)
517 case bfd_reloc_overflow:
518 r = info->callbacks->reloc_overflow
519 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
520 input_bfd, input_section, rel->r_offset);
521 break;
523 case bfd_reloc_undefined:
524 r = info->callbacks->undefined_symbol
525 (info, name, input_bfd, input_section, rel->r_offset,
526 TRUE);
527 break;
529 case bfd_reloc_outofrange:
530 msg = _("internal error: out of range error");
531 break;
533 case bfd_reloc_notsupported:
534 msg = _("internal error: unsupported relocation error");
535 break;
537 case bfd_reloc_dangerous:
538 msg = _("internal error: dangerous relocation");
539 break;
541 default:
542 msg = _("internal error: unknown error");
543 break;
546 if (msg)
547 r = info->callbacks->warning
548 (info, msg, name, input_bfd, input_section, rel->r_offset);
550 if (! r)
551 return FALSE;
555 return TRUE;
558 /* Return the section that should be marked against GC for a given
559 relocation. */
561 static asection *
562 m32c_elf_gc_mark_hook
563 (asection * sec,
564 struct bfd_link_info * info ATTRIBUTE_UNUSED,
565 Elf_Internal_Rela * rel,
566 struct elf_link_hash_entry * h,
567 Elf_Internal_Sym * sym)
569 if (h != NULL)
571 switch (ELF32_R_TYPE (rel->r_info))
573 default:
574 switch (h->root.type)
576 case bfd_link_hash_defined:
577 case bfd_link_hash_defweak:
578 return h->root.u.def.section;
580 case bfd_link_hash_common:
581 return h->root.u.c.p->section;
583 default:
584 break;
588 else
590 if (!(elf_bad_symtab (sec->owner)
591 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
592 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
593 && sym->st_shndx != SHN_COMMON))
595 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
599 return NULL;
602 /* Update the got entry reference counts for the section being removed. */
604 static bfd_boolean
605 m32c_elf_gc_sweep_hook
606 (bfd * abfd ATTRIBUTE_UNUSED,
607 struct bfd_link_info * info ATTRIBUTE_UNUSED,
608 asection * sec ATTRIBUTE_UNUSED,
609 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
611 return TRUE;
614 /* We support 16-bit pointers to code above 64k by generating a thunk
615 below 64k containing a JMP instruction to the final address. */
617 static bfd_boolean
618 m32c_elf_check_relocs
619 (bfd * abfd,
620 struct bfd_link_info * info,
621 asection * sec,
622 const Elf_Internal_Rela * relocs)
624 Elf_Internal_Shdr * symtab_hdr;
625 struct elf_link_hash_entry ** sym_hashes;
626 struct elf_link_hash_entry ** sym_hashes_end;
627 const Elf_Internal_Rela * rel;
628 const Elf_Internal_Rela * rel_end;
629 bfd_vma *local_plt_offsets;
630 asection *splt;
631 bfd *dynobj;
633 if (info->relocatable)
634 return TRUE;
636 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
637 sym_hashes = elf_sym_hashes (abfd);
638 local_plt_offsets = elf_local_got_offsets (abfd);
639 splt = NULL;
640 dynobj = elf_hash_table(info)->dynobj;
642 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
643 if (!elf_bad_symtab (abfd))
644 sym_hashes_end -= symtab_hdr->sh_info;
646 rel_end = relocs + sec->reloc_count;
647 for (rel = relocs; rel < rel_end; rel++)
649 struct elf_link_hash_entry *h;
650 unsigned long r_symndx;
651 bfd_vma *offset;
653 r_symndx = ELF32_R_SYM (rel->r_info);
654 if (r_symndx < symtab_hdr->sh_info)
655 h = NULL;
656 else
658 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
659 while (h->root.type == bfd_link_hash_indirect
660 || h->root.type == bfd_link_hash_warning)
661 h = (struct elf_link_hash_entry *) h->root.u.i.link;
664 switch (ELF32_R_TYPE (rel->r_info))
666 /* This relocation describes a 16-bit pointer to a function.
667 We may need to allocate a thunk in low memory; reserve memory
668 for it now. */
669 case R_M32C_16:
670 if (dynobj == NULL)
671 elf_hash_table (info)->dynobj = dynobj = abfd;
672 if (splt == NULL)
674 splt = bfd_get_section_by_name (dynobj, ".plt");
675 if (splt == NULL)
677 splt = bfd_make_section (dynobj, ".plt");
678 if (splt == NULL
679 || ! bfd_set_section_flags (dynobj, splt,
680 (SEC_ALLOC
681 | SEC_LOAD
682 | SEC_HAS_CONTENTS
683 | SEC_IN_MEMORY
684 | SEC_LINKER_CREATED
685 | SEC_READONLY
686 | SEC_CODE))
687 || ! bfd_set_section_alignment (dynobj, splt, 1))
688 return FALSE;
692 if (h != NULL)
693 offset = &h->plt.offset;
694 else
696 if (local_plt_offsets == NULL)
698 size_t size;
699 unsigned int i;
701 size = symtab_hdr->sh_info * sizeof (bfd_vma);
702 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
703 if (local_plt_offsets == NULL)
704 return FALSE;
705 elf_local_got_offsets (abfd) = local_plt_offsets;
707 for (i = 0; i < symtab_hdr->sh_info; i++)
708 local_plt_offsets[i] = (bfd_vma) -1;
710 offset = &local_plt_offsets[r_symndx];
713 if (*offset == (bfd_vma) -1)
715 *offset = splt->size;
716 splt->size += 4;
718 break;
722 return TRUE;
725 /* This must exist if dynobj is ever set. */
727 static bfd_boolean
728 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
729 struct bfd_link_info *info)
731 bfd *dynobj;
732 asection *splt;
734 /* As an extra sanity check, verify that all plt entries have
735 been filled in. */
737 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
738 && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL)
740 bfd_byte *contents = splt->contents;
741 unsigned int i, size = splt->size;
742 for (i = 0; i < size; i += 4)
744 unsigned int x = bfd_get_32 (dynobj, contents + i);
745 BFD_ASSERT (x != 0);
749 return TRUE;
752 static bfd_boolean
753 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
754 struct bfd_link_info *info)
756 bfd *dynobj;
757 asection *splt;
759 if (info->relocatable)
760 return TRUE;
762 dynobj = elf_hash_table (info)->dynobj;
763 if (dynobj == NULL)
764 return TRUE;
766 splt = bfd_get_section_by_name (dynobj, ".plt");
767 BFD_ASSERT (splt != NULL);
769 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
770 if (splt->contents == NULL)
771 return FALSE;
773 return TRUE;
776 /* Function to set the ELF flag bits. */
778 static bfd_boolean
779 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
781 elf_elfheader (abfd)->e_flags = flags;
782 elf_flags_init (abfd) = TRUE;
783 return TRUE;
786 /* Merge backend specific data from an object file to the output
787 object file when linking. */
789 static bfd_boolean
790 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
792 flagword old_flags, old_partial;
793 flagword new_flags, new_partial;
794 bfd_boolean error = FALSE;
795 char new_opt[80];
796 char old_opt[80];
798 new_opt[0] = old_opt[0] = '\0';
799 new_flags = elf_elfheader (ibfd)->e_flags;
800 old_flags = elf_elfheader (obfd)->e_flags;
802 #ifdef DEBUG
803 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
804 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
805 bfd_get_filename (ibfd));
806 #endif
808 if (!elf_flags_init (obfd))
810 /* First call, no flags set. */
811 elf_flags_init (obfd) = TRUE;
812 elf_elfheader (obfd)->e_flags = new_flags;
815 else if (new_flags == old_flags)
816 /* Compatible flags are ok. */
819 else /* Possibly incompatible flags. */
821 /* Warn if different cpu is used (allow a specific cpu to override
822 the generic cpu). */
823 new_partial = (new_flags & EF_M32C_CPU_MASK);
824 old_partial = (old_flags & EF_M32C_CPU_MASK);
825 if (new_partial == old_partial)
828 else
830 switch (new_partial)
832 default: strcat (new_opt, " -m16c"); break;
833 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
834 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
837 switch (old_partial)
839 default: strcat (old_opt, " -m16c"); break;
840 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
841 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
845 /* Print out any mismatches from above. */
846 if (new_opt[0])
848 error = TRUE;
849 (*_bfd_error_handler)
850 (_("%s: compiled with %s and linked with modules compiled with %s"),
851 bfd_get_filename (ibfd), new_opt, old_opt);
854 new_flags &= ~ EF_M32C_ALL_FLAGS;
855 old_flags &= ~ EF_M32C_ALL_FLAGS;
857 /* Warn about any other mismatches. */
858 if (new_flags != old_flags)
860 error = TRUE;
861 (*_bfd_error_handler)
862 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
863 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
867 if (error)
868 bfd_set_error (bfd_error_bad_value);
870 return !error;
874 static bfd_boolean
875 m32c_elf_print_private_bfd_data (bfd *abfd, PTR ptr)
877 FILE *file = (FILE *) ptr;
878 flagword flags;
880 BFD_ASSERT (abfd != NULL && ptr != NULL);
882 /* Print normal ELF private data. */
883 _bfd_elf_print_private_bfd_data (abfd, ptr);
885 flags = elf_elfheader (abfd)->e_flags;
886 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
888 switch (flags & EF_M32C_CPU_MASK)
890 default: break;
891 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
892 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
895 fputc ('\n', file);
896 return TRUE;
899 /* Return the MACH for an e_flags value. */
901 static int
902 elf32_m32c_machine (bfd *abfd)
904 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
906 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
907 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
910 return bfd_mach_m16c;
913 static bfd_boolean
914 m32c_elf_object_p (bfd *abfd)
916 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
917 elf32_m32c_machine (abfd));
918 return TRUE;
922 #ifdef DEBUG
923 static void
924 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
926 size_t locsymcount;
927 Elf_Internal_Sym *isymbuf;
928 Elf_Internal_Sym *isymend;
929 Elf_Internal_Sym *isym;
930 Elf_Internal_Shdr *symtab_hdr;
931 bfd_boolean free_internal = 0, free_external = 0;
932 char * st_info_str;
933 char * st_info_stb_str;
934 char * st_other_str;
935 char * st_shndx_str;
937 if (! internal_syms)
939 internal_syms = bfd_malloc (1000);
940 free_internal = 1;
942 if (! external_syms)
944 external_syms = bfd_malloc (1000);
945 free_external = 1;
948 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
949 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
950 if (free_internal)
951 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
952 symtab_hdr->sh_info, 0,
953 internal_syms, external_syms, NULL);
954 else
955 isymbuf = internal_syms;
956 isymend = isymbuf + locsymcount;
958 for (isym = isymbuf ; isym < isymend ; isym++)
960 switch (ELF_ST_TYPE (isym->st_info))
962 case STT_FUNC: st_info_str = "STT_FUNC";
963 case STT_SECTION: st_info_str = "STT_SECTION";
964 case STT_SRELC: st_info_str = "STT_SRELC";
965 case STT_FILE: st_info_str = "STT_FILE";
966 case STT_OBJECT: st_info_str = "STT_OBJECT";
967 case STT_TLS: st_info_str = "STT_TLS";
968 default: st_info_str = "";
970 switch (ELF_ST_BIND (isym->st_info))
972 case STB_LOCAL: st_info_stb_str = "STB_LOCAL";
973 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL";
974 default: st_info_stb_str = "";
976 switch (ELF_ST_VISIBILITY (isym->st_other))
978 case STV_DEFAULT: st_other_str = "STV_DEFAULT";
979 case STV_INTERNAL: st_other_str = "STV_INTERNAL";
980 case STV_PROTECTED: st_other_str = "STV_PROTECTED";
981 default: st_other_str = "";
983 switch (isym->st_shndx)
985 case SHN_ABS: st_shndx_str = "SHN_ABS";
986 case SHN_COMMON: st_shndx_str = "SHN_COMMON";
987 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF";
988 default: st_shndx_str = "";
991 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
992 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
993 isym,
994 (unsigned long) isym->st_value,
995 (unsigned long) isym->st_size,
996 isym->st_name,
997 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
998 isym->st_name),
999 isym->st_info, st_info_str, st_info_stb_str,
1000 isym->st_other, st_other_str,
1001 isym->st_shndx, st_shndx_str);
1003 if (free_internal)
1004 free (internal_syms);
1005 if (free_external)
1006 free (external_syms);
1009 static char *
1010 m32c_get_reloc (long reloc)
1012 if (0 <= reloc && reloc < R_M32C_max)
1013 return m32c_elf_howto_table[reloc].name;
1014 else
1015 return "";
1017 #endif /* DEBUG */
1019 /* Handle relaxing. */
1021 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1022 is within the low 64k, remove any entry for it in the plt. */
1024 struct relax_plt_data
1026 asection *splt;
1027 bfd_boolean *again;
1030 static bfd_boolean
1031 m32c_relax_plt_check (struct elf_link_hash_entry *h,
1032 PTR xdata)
1034 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1036 if (h->root.type == bfd_link_hash_warning)
1037 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1039 if (h->plt.offset != (bfd_vma) -1)
1041 bfd_vma address;
1043 if (h->root.type == bfd_link_hash_undefined
1044 || h->root.type == bfd_link_hash_undefweak)
1045 address = 0;
1046 else
1047 address = (h->root.u.def.section->output_section->vma
1048 + h->root.u.def.section->output_offset
1049 + h->root.u.def.value);
1051 if (address <= 0xffff)
1053 h->plt.offset = -1;
1054 data->splt->size -= 4;
1055 *data->again = TRUE;
1059 return TRUE;
1062 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1063 previously had a plt entry, give it a new entry offset. */
1065 static bfd_boolean
1066 m32c_relax_plt_realloc (struct elf_link_hash_entry *h,
1067 PTR xdata)
1069 bfd_vma *entry = (bfd_vma *) xdata;
1071 if (h->root.type == bfd_link_hash_warning)
1072 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1074 if (h->plt.offset != (bfd_vma) -1)
1076 h->plt.offset = *entry;
1077 *entry += 4;
1080 return TRUE;
1083 static bfd_boolean
1084 m32c_elf_relax_plt_section (bfd *dynobj,
1085 asection *splt,
1086 struct bfd_link_info *info,
1087 bfd_boolean *again)
1089 struct relax_plt_data relax_plt_data;
1090 bfd *ibfd;
1092 /* Assume nothing changes. */
1093 *again = FALSE;
1095 if (info->relocatable)
1096 return TRUE;
1098 /* We only relax the .plt section at the moment. */
1099 if (dynobj != elf_hash_table (info)->dynobj
1100 || strcmp (splt->name, ".plt") != 0)
1101 return TRUE;
1103 /* Quick check for an empty plt. */
1104 if (splt->size == 0)
1105 return TRUE;
1107 /* Map across all global symbols; see which ones happen to
1108 fall in the low 64k. */
1109 relax_plt_data.splt = splt;
1110 relax_plt_data.again = again;
1111 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1112 &relax_plt_data);
1114 /* Likewise for local symbols, though that's somewhat less convenient
1115 as we have to walk the list of input bfds and swap in symbol data. */
1116 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1118 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1119 Elf_Internal_Shdr *symtab_hdr;
1120 Elf_Internal_Sym *isymbuf = NULL;
1121 unsigned int idx;
1123 if (! local_plt_offsets)
1124 continue;
1126 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1127 if (symtab_hdr->sh_info != 0)
1129 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1130 if (isymbuf == NULL)
1131 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1132 symtab_hdr->sh_info, 0,
1133 NULL, NULL, NULL);
1134 if (isymbuf == NULL)
1135 return FALSE;
1138 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1140 Elf_Internal_Sym *isym;
1141 asection *tsec;
1142 bfd_vma address;
1144 if (local_plt_offsets[idx] == (bfd_vma) -1)
1145 continue;
1147 isym = &isymbuf[idx];
1148 if (isym->st_shndx == SHN_UNDEF)
1149 continue;
1150 else if (isym->st_shndx == SHN_ABS)
1151 tsec = bfd_abs_section_ptr;
1152 else if (isym->st_shndx == SHN_COMMON)
1153 tsec = bfd_com_section_ptr;
1154 else
1155 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1157 address = (tsec->output_section->vma
1158 + tsec->output_offset
1159 + isym->st_value);
1160 if (address <= 0xffff)
1162 local_plt_offsets[idx] = -1;
1163 splt->size -= 4;
1164 *again = TRUE;
1168 if (isymbuf != NULL
1169 && symtab_hdr->contents != (unsigned char *) isymbuf)
1171 if (! info->keep_memory)
1172 free (isymbuf);
1173 else
1175 /* Cache the symbols for elf_link_input_bfd. */
1176 symtab_hdr->contents = (unsigned char *) isymbuf;
1181 /* If we changed anything, walk the symbols again to reallocate
1182 .plt entry addresses. */
1183 if (*again && splt->size > 0)
1185 bfd_vma entry = 0;
1187 elf_link_hash_traverse (elf_hash_table (info),
1188 m32c_relax_plt_realloc, &entry);
1190 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1192 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1193 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1194 unsigned int idx;
1196 if (! local_plt_offsets)
1197 continue;
1199 for (idx = 0; idx < nlocals; ++idx)
1200 if (local_plt_offsets[idx] != (bfd_vma) -1)
1202 local_plt_offsets[idx] = entry;
1203 entry += 4;
1208 return TRUE;
1211 static int
1212 compare_reloc (const void *e1, const void *e2)
1214 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1215 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1217 if (i1->r_offset == i2->r_offset)
1218 return 0;
1219 else
1220 return i1->r_offset < i2->r_offset ? -1 : 1;
1223 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1224 static bfd_vma
1225 m32c_offset_for_reloc (bfd *abfd,
1226 Elf_Internal_Rela *rel,
1227 Elf_Internal_Shdr *symtab_hdr,
1228 Elf_External_Sym_Shndx *shndx_buf,
1229 Elf_Internal_Sym *intsyms)
1231 bfd_vma symval;
1233 /* Get the value of the symbol referred to by the reloc. */
1234 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1236 /* A local symbol. */
1237 Elf_Internal_Sym *isym;
1238 Elf_External_Sym_Shndx *shndx;
1239 asection *ssec;
1242 isym = intsyms + ELF32_R_SYM (rel->r_info);
1243 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1244 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (rel->r_info) : 0);
1246 symval = isym->st_value;
1247 if (ssec)
1248 symval += ssec->output_section->vma
1249 + ssec->output_offset;
1251 else
1253 unsigned long indx;
1254 struct elf_link_hash_entry *h;
1256 /* An external symbol. */
1257 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1258 h = elf_sym_hashes (abfd)[indx];
1259 BFD_ASSERT (h != NULL);
1261 if (h->root.type != bfd_link_hash_defined
1262 && h->root.type != bfd_link_hash_defweak)
1263 /* This appears to be a reference to an undefined
1264 symbol. Just ignore it--it will be caught by the
1265 regular reloc processing. */
1266 return 0;
1268 symval = (h->root.u.def.value
1269 + h->root.u.def.section->output_section->vma
1270 + h->root.u.def.section->output_offset);
1272 return symval;
1275 static int bytes_saved = 0;
1277 static int bytes_to_reloc[] = {
1278 R_M32C_NONE,
1279 R_M32C_8,
1280 R_M32C_16,
1281 R_M32C_24,
1282 R_M32C_32
1285 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1287 /* Mask for the number of relocs associated with this insn. */
1288 #define RLA_RELOCS 0x0000000f
1289 /* Number of bytes gas emitted (before gas's relaxing) */
1290 #define RLA_NBYTES 0x00000ff0
1292 /* If the displacement is within the given range and the new encoding
1293 differs from the old encoding (the index), then the insn can be
1294 relaxed to the new encoding. */
1295 typedef struct {
1296 int bytes;
1297 unsigned int max_disp;
1298 unsigned char new_encoding;
1299 } EncodingTable;
1301 static EncodingTable m16c_addr_encodings[] = {
1302 { 0, 0, 0 }, /* R0 */
1303 { 0, 0, 1 }, /* R1 */
1304 { 0, 0, 2 }, /* R2 */
1305 { 0, 0, 3 }, /* R3 */
1306 { 0, 0, 4 }, /* A0 */
1307 { 0, 0, 5 }, /* A1 */
1308 { 0, 0, 6 }, /* [A0] */
1309 { 0, 0, 7 }, /* [A1] */
1310 { 1, 0, 6 }, /* udsp:8[A0] */
1311 { 1, 0, 7 }, /* udsp:8[A1] */
1312 { 1, 0, 10 }, /* udsp:8[SB] */
1313 { 1, 0, 11 }, /* sdsp:8[FB] */
1314 { 2, 255, 8 }, /* udsp:16[A0] */
1315 { 2, 255, 9 }, /* udsp:16[A1] */
1316 { 2, 255, 10 }, /* udsp:16[SB] */
1317 { 2, 0, 15 }, /* abs:16 */
1320 static EncodingTable m16c_jmpaddr_encodings[] = {
1321 { 0, 0, 0 }, /* R0 */
1322 { 0, 0, 1 }, /* R1 */
1323 { 0, 0, 2 }, /* R2 */
1324 { 0, 0, 3 }, /* R3 */
1325 { 0, 0, 4 }, /* A0 */
1326 { 0, 0, 5 }, /* A1 */
1327 { 0, 0, 6 }, /* [A0] */
1328 { 0, 0, 7 }, /* [A1] */
1329 { 1, 0, 6 }, /* udsp:8[A0] */
1330 { 1, 0, 7 }, /* udsp:8[A1] */
1331 { 1, 0, 10 }, /* udsp:8[SB] */
1332 { 1, 0, 11 }, /* sdsp:8[FB] */
1333 { 3, 255, 8 }, /* udsp:20[A0] */
1334 { 3, 255, 9 }, /* udsp:20[A1] */
1335 { 2, 255, 10 }, /* udsp:16[SB] */
1336 { 2, 0, 15 }, /* abs:16 */
1339 static EncodingTable m32c_addr_encodings[] = {
1340 { 0, 0, 0 }, /* [A0] */
1341 { 0, 0, 1 }, /* [A1] */
1342 { 0, 0, 2 }, /* A0 */
1343 { 0, 0, 3 }, /* A1 */
1344 { 1, 0, 0 }, /* udsp:8[A0] */
1345 { 1, 0, 1 }, /* udsp:8[A1] */
1346 { 1, 0, 6 }, /* udsp:8[SB] */
1347 { 1, 0, 7 }, /* sdsp:8[FB] */
1348 { 2, 255, 4 }, /* udsp:16[A0] */
1349 { 2, 255, 5 }, /* udsp:16[A1] */
1350 { 2, 255, 6 }, /* udsp:16[SB] */
1351 { 2, 127, 7 }, /* sdsp:16[FB] */
1352 { 3, 65535, 8 }, /* udsp:24[A0] */
1353 { 3, 65535, 9 }, /* udsp:24[A1] */
1354 { 3, 65535, 15 }, /* abs24 */
1355 { 2, 0, 15 }, /* abs16 */
1356 { 0, 0, 16 }, /* R2 */
1357 { 0, 0, 17 }, /* R3 */
1358 { 0, 0, 18 }, /* R0 */
1359 { 0, 0, 19 }, /* R1 */
1360 { 0, 0, 20 }, /* */
1361 { 0, 0, 21 }, /* */
1362 { 0, 0, 22 }, /* */
1363 { 0, 0, 23 }, /* */
1364 { 0, 0, 24 }, /* */
1365 { 0, 0, 25 }, /* */
1366 { 0, 0, 26 }, /* */
1367 { 0, 0, 27 }, /* */
1368 { 0, 0, 28 }, /* */
1369 { 0, 0, 29 }, /* */
1370 { 0, 0, 30 }, /* */
1371 { 0, 0, 31 }, /* */
1374 static bfd_boolean
1375 m32c_elf_relax_section
1376 (bfd * abfd,
1377 asection * sec,
1378 struct bfd_link_info * link_info,
1379 bfd_boolean * again)
1381 Elf_Internal_Shdr *symtab_hdr;
1382 Elf_Internal_Shdr *shndx_hdr;
1383 Elf_Internal_Rela *internal_relocs;
1384 Elf_Internal_Rela *free_relocs = NULL;
1385 Elf_Internal_Rela *irel, *irelend, *srel;
1386 bfd_byte * contents = NULL;
1387 bfd_byte * free_contents = NULL;
1388 Elf_Internal_Sym *intsyms = NULL;
1389 Elf_Internal_Sym *free_intsyms = NULL;
1390 Elf_External_Sym_Shndx *shndx_buf = NULL;
1391 int machine;
1393 if (abfd == elf_hash_table (link_info)->dynobj
1394 && strcmp (sec->name, ".plt") == 0)
1395 return m32c_elf_relax_plt_section (abfd, sec, link_info, again);
1397 /* Assume nothing changes. */
1398 *again = FALSE;
1400 machine = elf32_m32c_machine (abfd);
1402 /* We don't have to do anything for a relocatable link, if
1403 this section does not have relocs, or if this is not a
1404 code section. */
1405 if (link_info->relocatable
1406 || (sec->flags & SEC_RELOC) == 0
1407 || sec->reloc_count == 0
1408 || (sec->flags & SEC_CODE) == 0)
1409 return TRUE;
1411 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1412 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1414 /* Get the section contents. */
1415 if (elf_section_data (sec)->this_hdr.contents != NULL)
1416 contents = elf_section_data (sec)->this_hdr.contents;
1417 /* Go get them off disk. */
1418 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1419 goto error_return;
1421 /* Read this BFD's symbols. */
1422 /* Get cached copy if it exists. */
1423 if (symtab_hdr->contents != NULL)
1425 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1427 else
1429 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1430 symtab_hdr->contents = (bfd_byte *) intsyms;
1433 if (shndx_hdr->sh_size != 0)
1435 bfd_size_type amt;
1437 amt = symtab_hdr->sh_info;
1438 amt *= sizeof (Elf_External_Sym_Shndx);
1439 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1440 if (shndx_buf == NULL)
1441 goto error_return;
1442 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1443 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1444 goto error_return;
1445 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1448 /* Get a copy of the native relocations. */
1449 internal_relocs = (_bfd_elf_link_read_relocs
1450 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1451 link_info->keep_memory));
1452 if (internal_relocs == NULL)
1453 goto error_return;
1454 if (! link_info->keep_memory)
1455 free_relocs = internal_relocs;
1457 /* The RL_ relocs must be just before the operand relocs they go
1458 with, so we must sort them to guarantee this. */
1459 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1460 compare_reloc);
1462 /* Walk through them looking for relaxing opportunities. */
1463 irelend = internal_relocs + sec->reloc_count;
1465 for (irel = internal_relocs; irel < irelend; irel++)
1467 bfd_vma symval;
1468 unsigned char *insn, *gap, *einsn;
1469 bfd_vma pc;
1470 bfd_signed_vma pcrel;
1471 int relax_relocs;
1472 int gap_size;
1473 int new_type;
1474 int posn;
1475 int enc;
1476 EncodingTable *enctbl;
1477 EncodingTable *e;
1479 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1480 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1481 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1482 continue;
1484 srel = irel;
1486 /* There will always be room for the relaxed insn, since it is smaller
1487 than the one it would replace. */
1488 BFD_ASSERT (irel->r_offset < sec->size);
1490 insn = contents + irel->r_offset;
1491 relax_relocs = irel->r_addend % 16;
1493 /* Ok, we only have three relocs we care about, and they're all
1494 fake. The lower four bits of the addend is always the number
1495 of following relocs (hence the qsort above) that are assigned
1496 to this opcode. The next 8 bits of the addend indicates the
1497 number of bytes in the insn. We use the rest of them
1498 ourselves as flags for the more expensive operations (defines
1499 above). The three relocs are:
1501 RL_JUMP: This marks all direct jump insns. We check the
1502 displacement and replace them with shorter jumps if
1503 they're in range. We also use this to find JMP.S
1504 insns and manually shorten them when we delete bytes.
1505 We have to decode these insns to figure out what to
1508 RL_1ADDR: This is a :G or :Q insn, which has a single
1509 "standard" operand. We have to extract the type
1510 field, see if it's a wide displacement, then figure
1511 out if we can replace it with a narrow displacement.
1512 We don't have to decode these insns.
1514 RL_2ADDR: Similarly, but two "standard" operands. Note that
1515 r_addend may still be 1, as standard operands don't
1516 always have displacements. Gas shouldn't give us one
1517 with zero operands, but since we don't know which one
1518 has the displacement, we check them both anyway.
1520 These all point to the beginning of the insn itself, not the
1521 operands.
1523 Note that we only relax one step at a time, relying on the
1524 linker to call us repeatedly. Thus, there is no code for
1525 JMP.A->JMP.B although that will happen in two steps.
1526 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1529 /* Get the value of the symbol referred to by the reloc. Just
1530 in case this is the last reloc in the list, use the RL's
1531 addend to choose between this reloc (no addend) or the next
1532 (yes addend, which means at least one following reloc). */
1533 srel = irel + (relax_relocs ? 1 : 0);
1534 symval = OFFSET_FOR_RELOC (srel);
1536 /* Setting gap_size nonzero is the flag which means "something
1537 shrunk". */
1538 gap_size = 0;
1539 gap = NULL;
1540 new_type = ELF32_R_TYPE(srel->r_info);
1542 pc = sec->output_section->vma + sec->output_offset
1543 + srel->r_offset;
1544 pcrel = symval - pc + srel->r_addend;
1546 if (machine == bfd_mach_m16c)
1548 /* R8C / M16C */
1550 switch (ELF32_R_TYPE(irel->r_info))
1553 case R_M32C_RL_JUMP:
1554 switch (insn[0])
1556 case 0xfe: /* jmp.b */
1557 if (pcrel >= 2 && pcrel <= 9)
1559 /* Relax JMP.B -> JMP.S. We need to get rid of
1560 the following reloc though. */
1561 insn[0] = 0x60 | (pcrel - 2);
1562 new_type = R_M32C_NONE;
1563 irel->r_addend = 0x10;
1564 gap_size = 1;
1565 gap = insn + 1;
1567 break;
1569 case 0xf4: /* jmp.w */
1570 /* 128 is allowed because it will be one byte closer
1571 after relaxing. Likewise for all other pc-rel
1572 jumps. */
1573 if (pcrel <= 128 && pcrel >= -128)
1575 /* Relax JMP.W -> JMP.B */
1576 insn[0] = 0xfe;
1577 insn[1] = 0;
1578 new_type = R_M32C_8_PCREL;
1579 gap_size = 1;
1580 gap = insn + 2;
1582 break;
1584 case 0xfc: /* jmp.a */
1585 if (pcrel <= 32768 && pcrel >= -32768)
1587 /* Relax JMP.A -> JMP.W */
1588 insn[0] = 0xf4;
1589 insn[1] = 0;
1590 insn[2] = 0;
1591 new_type = R_M32C_16_PCREL;
1592 gap_size = 1;
1593 gap = insn + 3;
1595 break;
1597 case 0xfd: /* jsr.a */
1598 if (pcrel <= 32768 && pcrel >= -32768)
1600 /* Relax JSR.A -> JSR.W */
1601 insn[0] = 0xf5;
1602 insn[1] = 0;
1603 insn[2] = 0;
1604 new_type = R_M32C_16_PCREL;
1605 gap_size = 1;
1606 gap = insn + 3;
1608 break;
1610 break;
1612 case R_M32C_RL_2ADDR:
1613 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1615 enctbl = m16c_addr_encodings;
1616 posn = 2;
1617 enc = (insn[1] >> 4) & 0x0f;
1618 e = & enctbl[enc];
1620 if (srel->r_offset == irel->r_offset + posn
1621 && e->new_encoding != enc
1622 && symval <= e->max_disp)
1624 insn[1] &= 0x0f;
1625 insn[1] |= e->new_encoding << 4;
1626 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1627 gap = insn + posn + enctbl[e->new_encoding].bytes;
1628 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1629 break;
1631 if (relax_relocs == 2)
1632 srel ++;
1633 posn += e->bytes;
1635 goto try_1addr_16;
1637 case R_M32C_RL_1ADDR:
1638 /* xxxx xxxx xxxx dest [disp] */
1640 enctbl = m16c_addr_encodings;
1641 posn = 2;
1643 /* Check the opcode for jumps. We know it's safe to
1644 do this because all 2ADDR insns are at least two
1645 bytes long. */
1646 enc = insn[0] * 256 + insn[1];
1647 enc &= 0xfff0;
1648 if (enc == 0x7d20
1649 || enc == 0x7d00
1650 || enc == 0x7d30
1651 || enc == 0x7d10)
1653 enctbl = m16c_jmpaddr_encodings;
1656 try_1addr_16:
1657 /* srel, posn, and enc must be set here. */
1659 symval = OFFSET_FOR_RELOC (srel);
1660 enc = insn[1] & 0x0f;
1661 e = & enctbl[enc];
1663 if (srel->r_offset == irel->r_offset + posn
1664 && e->new_encoding != enc
1665 && symval <= e->max_disp)
1667 insn[1] &= 0xf0;
1668 insn[1] |= e->new_encoding;
1669 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1670 gap = insn + posn + enctbl[e->new_encoding].bytes;
1671 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1672 break;
1675 break;
1677 } /* Ends switch (reloc type) for m16c. */
1679 else /* machine == bfd_mach_m32c */
1681 /* M32CM / M32C */
1683 switch (ELF32_R_TYPE(irel->r_info))
1686 case R_M32C_RL_JUMP:
1687 switch (insn[0])
1689 case 0xbb: /* jmp.b */
1690 if (pcrel >= 2 && pcrel <= 9)
1692 int p = pcrel - 2;
1693 /* Relax JMP.B -> JMP.S. We need to get rid of
1694 the following reloc though. */
1695 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1696 new_type = R_M32C_NONE;
1697 irel->r_addend = 0x10;
1698 gap_size = 1;
1699 gap = insn + 1;
1701 break;
1703 case 0xce: /* jmp.w */
1704 if (pcrel <= 128 && pcrel >= -128)
1706 /* Relax JMP.W -> JMP.B */
1707 insn[0] = 0xbb;
1708 insn[1] = 0;
1709 new_type = R_M32C_8_PCREL;
1710 gap_size = 1;
1711 gap = insn + 2;
1713 break;
1715 case 0xcc: /* jmp.a */
1716 if (pcrel <= 32768 && pcrel >= -32768)
1718 /* Relax JMP.A -> JMP.W */
1719 insn[0] = 0xce;
1720 insn[1] = 0;
1721 insn[2] = 0;
1722 new_type = R_M32C_16_PCREL;
1723 gap_size = 1;
1724 gap = insn + 3;
1726 break;
1728 case 0xcd: /* jsr.a */
1729 if (pcrel <= 32768 && pcrel >= -32768)
1731 /* Relax JSR.A -> JSR.W */
1732 insn[0] = 0xcf;
1733 insn[1] = 0;
1734 insn[2] = 0;
1735 new_type = R_M32C_16_PCREL;
1736 gap_size = 1;
1737 gap = insn + 3;
1739 break;
1741 break;
1743 case R_M32C_RL_2ADDR:
1744 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1746 einsn = insn;
1747 posn = 2;
1748 if (einsn[0] == 1)
1750 /* prefix; remove it as far as the RL reloc is concerned. */
1751 einsn ++;
1752 posn ++;
1755 enctbl = m32c_addr_encodings;
1756 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1757 e = & enctbl[enc];
1759 if (srel->r_offset == irel->r_offset + posn
1760 && e->new_encoding != enc
1761 && symval <= e->max_disp)
1763 einsn[0] &= 0x8f;
1764 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1765 einsn[1] &= 0xcf;
1766 einsn[1] |= (e->new_encoding & 0x03) << 4;
1767 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1768 gap = insn + posn + enctbl[e->new_encoding].bytes;
1769 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1770 break;
1772 if (relax_relocs == 2)
1773 srel ++;
1774 posn += e->bytes;
1776 goto try_1addr_32;
1778 case R_M32C_RL_1ADDR:
1779 /* xxxx DDDx DDxx xxxx [disp] */
1781 einsn = insn;
1782 posn = 2;
1783 if (einsn[0] == 1)
1785 /* prefix; remove it as far as the RL reloc is concerned. */
1786 einsn ++;
1787 posn ++;
1790 enctbl = m32c_addr_encodings;
1792 try_1addr_32:
1793 /* srel, posn, and enc must be set here. */
1795 symval = OFFSET_FOR_RELOC (srel);
1796 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1797 e = & enctbl[enc];
1799 if (srel->r_offset == irel->r_offset + posn
1800 && e->new_encoding != enc
1801 && symval <= e->max_disp)
1803 einsn[0] &= 0xf1;
1804 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1805 einsn[1] &= 0x3f;
1806 einsn[1] |= (e->new_encoding & 0x03) << 6;
1807 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1808 gap = insn + posn + enctbl[e->new_encoding].bytes;
1809 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1810 break;
1813 break;
1815 } /* Ends switch (reloc type) for m32c. */
1818 if (gap_size == 0)
1819 continue;
1821 *again = TRUE;
1823 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1825 /* Note that we've changed the relocs, section contents, etc. */
1826 elf_section_data (sec)->relocs = internal_relocs;
1827 free_relocs = NULL;
1829 elf_section_data (sec)->this_hdr.contents = contents;
1830 free_contents = NULL;
1832 symtab_hdr->contents = (bfd_byte *) intsyms;
1833 free_intsyms = NULL;
1835 bytes_saved += gap_size;
1837 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1838 goto error_return;
1840 } /* next relocation */
1842 if (free_relocs != NULL)
1844 free (free_relocs);
1845 free_relocs = NULL;
1848 if (free_contents != NULL)
1850 if (! link_info->keep_memory)
1851 free (free_contents);
1852 /* Cache the section contents for elf_link_input_bfd. */
1853 else
1854 elf_section_data (sec)->this_hdr.contents = contents;
1856 free_contents = NULL;
1859 if (shndx_buf != NULL)
1861 shndx_hdr->contents = NULL;
1862 free (shndx_buf);
1865 if (free_intsyms != NULL)
1867 if (! link_info->keep_memory)
1868 free (free_intsyms);
1869 /* Cache the symbols for elf_link_input_bfd. */
1870 else
1872 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1875 free_intsyms = NULL;
1878 return TRUE;
1880 error_return:
1881 if (free_relocs != NULL)
1882 free (free_relocs);
1883 if (free_contents != NULL)
1884 free (free_contents);
1885 if (shndx_buf != NULL)
1887 shndx_hdr->contents = NULL;
1888 free (shndx_buf);
1890 if (free_intsyms != NULL)
1891 free (free_intsyms);
1892 return FALSE;
1895 /* Delete some bytes from a section while relaxing. */
1897 static bfd_boolean
1898 m32c_elf_relax_delete_bytes
1899 (bfd * abfd,
1900 asection * sec,
1901 bfd_vma addr,
1902 int count)
1904 Elf_Internal_Shdr *symtab_hdr;
1905 Elf_Internal_Shdr *shndx_hdr;
1906 int sec_shndx;
1907 bfd_byte *contents;
1908 Elf_Internal_Rela *irel;
1909 Elf_Internal_Rela *irelend;
1910 Elf_Internal_Rela *irelalign;
1911 bfd_vma toaddr;
1912 Elf_Internal_Sym *isym;
1913 Elf_Internal_Sym *isymend;
1914 Elf_Internal_Sym *intsyms;
1915 Elf_External_Sym_Shndx *shndx_buf;
1916 Elf_External_Sym_Shndx *shndx;
1917 struct elf_link_hash_entry ** sym_hashes;
1918 struct elf_link_hash_entry ** end_hashes;
1919 unsigned int symcount;
1921 contents = elf_section_data (sec)->this_hdr.contents;
1923 /* The deletion must stop at the next ALIGN reloc for an aligment
1924 power larger than the number of bytes we are deleting. */
1925 irelalign = NULL;
1926 toaddr = sec->size;
1928 irel = elf_section_data (sec)->relocs;
1929 irelend = irel + sec->reloc_count;
1931 /* Actually delete the bytes. */
1932 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1933 sec->size -= count;
1935 /* Adjust all the relocs. */
1936 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1938 /* Get the new reloc address. */
1939 if (irel->r_offset > addr && irel->r_offset < toaddr)
1940 irel->r_offset -= count;
1942 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1943 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1944 && irel->r_offset + 1 < addr
1945 && irel->r_offset + 7 > addr)
1947 bfd_vma disp;
1948 unsigned char *insn = &contents[irel->r_offset];
1949 disp = *insn;
1950 /* This is a JMP.S, which we have to manually update. */
1951 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1953 if ((*insn & 0xf8) != 0x60)
1954 continue;
1955 disp = (disp & 7);
1957 else
1959 if ((*insn & 0xce) != 0x4a)
1960 continue;
1961 disp = ((disp & 0x30) >> 3) | (disp & 1);
1963 if (irel->r_offset + disp + 2 >= addr+count)
1965 disp -= count;
1966 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1968 *insn = (*insn & 0xf8) | disp;
1970 else
1972 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
1978 /* Adjust the local symbols defined in this section. */
1979 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1980 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1981 isym = intsyms;
1982 isymend = isym + symtab_hdr->sh_info;
1984 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1985 shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr;
1986 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1987 shndx = shndx_buf;
1989 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
1992 if ((int) isym->st_shndx == sec_shndx
1993 && isym->st_value > addr
1994 && isym->st_value < toaddr)
1996 isym->st_value -= count;
2000 /* Now adjust the global symbols defined in this section. */
2001 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2002 - symtab_hdr->sh_info);
2003 sym_hashes = elf_sym_hashes (abfd);
2004 // sym_hashes += symtab_hdr->sh_info;
2005 end_hashes = sym_hashes + symcount;
2007 for (; sym_hashes < end_hashes; sym_hashes ++)
2009 struct elf_link_hash_entry * sym_hash = * sym_hashes;
2011 if (sym_hash &&
2012 ( sym_hash->root.type == bfd_link_hash_defined
2013 || sym_hash->root.type == bfd_link_hash_defweak)
2014 && sym_hash->root.u.def.section == sec
2015 && sym_hash->root.u.def.value > addr
2016 && sym_hash->root.u.def.value < toaddr)
2018 sym_hash->root.u.def.value -= count;
2022 return TRUE;
2026 #define ELF_ARCH bfd_arch_m32c
2027 #define ELF_MACHINE_CODE EM_M32C
2028 #define ELF_MAXPAGESIZE 0x1000
2030 #if 0
2031 #define TARGET_BIG_SYM bfd_elf32_m32c_vec
2032 #define TARGET_BIG_NAME "elf32-m32c"
2033 #else
2034 #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
2035 #define TARGET_LITTLE_NAME "elf32-m32c"
2036 #endif
2038 #define elf_info_to_howto_rel NULL
2039 #define elf_info_to_howto m32c_info_to_howto_rela
2040 #define elf_backend_object_p m32c_elf_object_p
2041 #define elf_backend_relocate_section m32c_elf_relocate_section
2042 #define elf_backend_gc_mark_hook m32c_elf_gc_mark_hook
2043 #define elf_backend_gc_sweep_hook m32c_elf_gc_sweep_hook
2044 #define elf_backend_check_relocs m32c_elf_check_relocs
2045 #define elf_backend_object_p m32c_elf_object_p
2046 #define elf_symbol_leading_char ('_')
2047 #define elf_backend_always_size_sections \
2048 m32c_elf_always_size_sections
2049 #define elf_backend_finish_dynamic_sections \
2050 m32c_elf_finish_dynamic_sections
2052 #define elf_backend_can_gc_sections 1
2054 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2055 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2056 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2057 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2058 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2060 #include "elf32-target.h"