From Andrew Chatham: Make File_read::View::data_ a const pointer.
[binutils.git] / bfd / elf32-m32c.c
blob6c9a82502cd2c20ef633d7f002070d0042771ea9
1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005, 2006, 2007
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 3 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 "sysdep.h"
22 #include "bfd.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_check_relocs
36 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
37 static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
38 #ifdef DEBUG
39 char * m32c_get_reloc (long reloc);
40 void dump_symtab (bfd *, void *, void *);
41 #endif
42 static bfd_boolean m32c_elf_relax_section
43 (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
46 static reloc_howto_type m32c_elf_howto_table [] =
48 /* This reloc does nothing. */
49 HOWTO (R_M32C_NONE, /* type */
50 0, /* rightshift */
51 0, /* size (0 = byte, 1 = short, 2 = long) */
52 32, /* bitsize */
53 FALSE, /* pc_relative */
54 0, /* bitpos */
55 complain_overflow_bitfield, /* complain_on_overflow */
56 bfd_elf_generic_reloc, /* special_function */
57 "R_M32C_NONE", /* name */
58 FALSE, /* partial_inplace */
59 0, /* src_mask */
60 0, /* dst_mask */
61 FALSE), /* pcrel_offset */
63 /* GCC intentionally overflows these next two in order to work
64 around limitations in the addressing modes, so don't complain
65 about overflow. */
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_dont, /* 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_dont, /* 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 static reloc_howto_type *
276 m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
278 unsigned int i;
280 for (i = 0;
281 i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]);
282 i++)
283 if (m32c_elf_howto_table[i].name != NULL
284 && strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0)
285 return &m32c_elf_howto_table[i];
287 return NULL;
290 /* Set the howto pointer for an M32C ELF reloc. */
292 static void
293 m32c_info_to_howto_rela
294 (bfd * abfd ATTRIBUTE_UNUSED,
295 arelent * cache_ptr,
296 Elf_Internal_Rela * dst)
298 unsigned int r_type;
300 r_type = ELF32_R_TYPE (dst->r_info);
301 BFD_ASSERT (r_type < (unsigned int) R_M32C_max);
302 cache_ptr->howto = & m32c_elf_howto_table [r_type];
307 /* Relocate an M32C ELF section.
308 There is some attempt to make this function usable for many architectures,
309 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
310 if only to serve as a learning tool.
312 The RELOCATE_SECTION function is called by the new ELF backend linker
313 to handle the relocations for a section.
315 The relocs are always passed as Rela structures; if the section
316 actually uses Rel structures, the r_addend field will always be
317 zero.
319 This function is responsible for adjusting the section contents as
320 necessary, and (if using Rela relocs and generating a relocatable
321 output file) adjusting the reloc addend as necessary.
323 This function does not have to worry about setting the reloc
324 address or the reloc symbol index.
326 LOCAL_SYMS is a pointer to the swapped in local symbols.
328 LOCAL_SECTIONS is an array giving the section in the input file
329 corresponding to the st_shndx field of each local symbol.
331 The global hash table entry for the global symbols can be found
332 via elf_sym_hashes (input_bfd).
334 When generating relocatable output, this function must handle
335 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
336 going to be the section symbol corresponding to the output
337 section, which means that the addend must be adjusted
338 accordingly. */
340 static bfd_boolean
341 m32c_elf_relocate_section
342 (bfd * output_bfd ATTRIBUTE_UNUSED,
343 struct bfd_link_info * info,
344 bfd * input_bfd,
345 asection * input_section,
346 bfd_byte * contents,
347 Elf_Internal_Rela * relocs,
348 Elf_Internal_Sym * local_syms,
349 asection ** local_sections)
351 Elf_Internal_Shdr * symtab_hdr;
352 struct elf_link_hash_entry ** sym_hashes;
353 Elf_Internal_Rela * rel;
354 Elf_Internal_Rela * relend;
355 bfd *dynobj;
356 asection *splt;
358 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
359 sym_hashes = elf_sym_hashes (input_bfd);
360 relend = relocs + input_section->reloc_count;
362 dynobj = elf_hash_table (info)->dynobj;
363 splt = NULL;
364 if (dynobj != NULL)
365 splt = bfd_get_section_by_name (dynobj, ".plt");
367 for (rel = relocs; rel < relend; rel ++)
369 reloc_howto_type * howto;
370 unsigned long r_symndx;
371 Elf_Internal_Sym * sym;
372 asection * sec;
373 struct elf_link_hash_entry * h;
374 bfd_vma relocation;
375 bfd_reloc_status_type r;
376 const char * name = NULL;
377 int r_type;
379 r_type = ELF32_R_TYPE (rel->r_info);
381 /* These are only used for relaxing; we don't actually relocate
382 anything with them, so skip them. */
383 if (r_type == R_M32C_RL_JUMP
384 || r_type == R_M32C_RL_1ADDR
385 || r_type == R_M32C_RL_2ADDR)
386 continue;
388 r_symndx = ELF32_R_SYM (rel->r_info);
390 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
391 h = NULL;
392 sym = NULL;
393 sec = NULL;
394 relocation = 0;
396 if (r_symndx < symtab_hdr->sh_info)
398 sym = local_syms + r_symndx;
399 sec = local_sections [r_symndx];
400 relocation = (sec->output_section->vma
401 + sec->output_offset
402 + sym->st_value);
404 name = bfd_elf_string_from_elf_section
405 (input_bfd, symtab_hdr->sh_link, sym->st_name);
406 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
408 else
410 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
412 while (h->root.type == bfd_link_hash_indirect
413 || h->root.type == bfd_link_hash_warning)
414 h = (struct elf_link_hash_entry *) h->root.u.i.link;
416 name = h->root.root.string;
418 if (h->root.type == bfd_link_hash_defined
419 || h->root.type == bfd_link_hash_defweak)
421 sec = h->root.u.def.section;
422 relocation = (h->root.u.def.value
423 + sec->output_section->vma
424 + sec->output_offset);
426 else if (h->root.type == bfd_link_hash_undefweak)
428 else if (!info->relocatable)
430 if (! ((*info->callbacks->undefined_symbol)
431 (info, h->root.root.string, input_bfd,
432 input_section, rel->r_offset, TRUE)))
433 return FALSE;
437 if (sec != NULL && elf_discarded_section (sec))
439 /* For relocs against symbols from removed linkonce sections,
440 or sections discarded by a linker script, we just want the
441 section contents zeroed. Avoid any special processing. */
442 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
443 rel->r_info = 0;
444 rel->r_addend = 0;
445 continue;
448 if (info->relocatable)
450 /* This is a relocatable link. We don't have to change
451 anything, unless the reloc is against a section symbol,
452 in which case we have to adjust according to where the
453 section symbol winds up in the output section. */
454 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
455 rel->r_addend += sec->output_offset;
456 continue;
459 switch (ELF32_R_TYPE (rel->r_info))
461 case R_M32C_16:
463 bfd_vma *plt_offset;
465 if (h != NULL)
466 plt_offset = &h->plt.offset;
467 else
468 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
470 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
471 relocation, *plt_offset);*/
472 if (relocation <= 0xffff)
474 /* If the symbol is in range for a 16-bit address, we should
475 have deallocated the plt entry in relax_section. */
476 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
478 else
480 /* If the symbol is out of range for a 16-bit address,
481 we must have allocated a plt entry. */
482 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
484 /* If this is the first time we've processed this symbol,
485 fill in the plt entry with the correct symbol address. */
486 if ((*plt_offset & 1) == 0)
488 unsigned int x;
490 x = 0x000000fc; /* jmpf */
491 x |= (relocation << 8) & 0xffffff00;
492 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
493 *plt_offset |= 1;
496 relocation = (splt->output_section->vma
497 + splt->output_offset
498 + (*plt_offset & -2));
499 if (name)
501 char *newname = bfd_malloc (strlen(name)+5);
502 strcpy (newname, name);
503 strcat(newname, ".plt");
504 _bfd_generic_link_add_one_symbol (info,
505 input_bfd,
506 newname,
507 BSF_FUNCTION | BSF_WEAK,
508 splt,
509 (*plt_offset & -2),
517 break;
519 case R_M32C_HI8:
520 case R_M32C_HI16:
521 relocation >>= 16;
522 break;
525 #if 0
526 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
527 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
528 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
529 relocation, rel->r_addend);
531 int i;
532 for (i=0; i<4; i++)
533 printf (" %02x", contents[rel->r_offset+i]);
534 printf ("\n");
536 #endif
537 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
538 contents, rel->r_offset, relocation,
539 rel->r_addend);
541 if (r != bfd_reloc_ok)
543 const char * msg = (const char *) NULL;
545 switch (r)
547 case bfd_reloc_overflow:
548 r = info->callbacks->reloc_overflow
549 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
550 input_bfd, input_section, rel->r_offset);
551 break;
553 case bfd_reloc_undefined:
554 r = info->callbacks->undefined_symbol
555 (info, name, input_bfd, input_section, rel->r_offset,
556 TRUE);
557 break;
559 case bfd_reloc_outofrange:
560 msg = _("internal error: out of range error");
561 break;
563 case bfd_reloc_notsupported:
564 msg = _("internal error: unsupported relocation error");
565 break;
567 case bfd_reloc_dangerous:
568 msg = _("internal error: dangerous relocation");
569 break;
571 default:
572 msg = _("internal error: unknown error");
573 break;
576 if (msg)
577 r = info->callbacks->warning
578 (info, msg, name, input_bfd, input_section, rel->r_offset);
580 if (! r)
581 return FALSE;
585 return TRUE;
588 /* We support 16-bit pointers to code above 64k by generating a thunk
589 below 64k containing a JMP instruction to the final address. */
591 static bfd_boolean
592 m32c_elf_check_relocs
593 (bfd * abfd,
594 struct bfd_link_info * info,
595 asection * sec,
596 const Elf_Internal_Rela * relocs)
598 Elf_Internal_Shdr * symtab_hdr;
599 struct elf_link_hash_entry ** sym_hashes;
600 struct elf_link_hash_entry ** sym_hashes_end;
601 const Elf_Internal_Rela * rel;
602 const Elf_Internal_Rela * rel_end;
603 bfd_vma *local_plt_offsets;
604 asection *splt;
605 bfd *dynobj;
607 if (info->relocatable)
608 return TRUE;
610 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
611 sym_hashes = elf_sym_hashes (abfd);
612 local_plt_offsets = elf_local_got_offsets (abfd);
613 splt = NULL;
614 dynobj = elf_hash_table(info)->dynobj;
616 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
617 if (!elf_bad_symtab (abfd))
618 sym_hashes_end -= symtab_hdr->sh_info;
620 rel_end = relocs + sec->reloc_count;
621 for (rel = relocs; rel < rel_end; rel++)
623 struct elf_link_hash_entry *h;
624 unsigned long r_symndx;
625 bfd_vma *offset;
627 r_symndx = ELF32_R_SYM (rel->r_info);
628 if (r_symndx < symtab_hdr->sh_info)
629 h = NULL;
630 else
632 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
633 while (h->root.type == bfd_link_hash_indirect
634 || h->root.type == bfd_link_hash_warning)
635 h = (struct elf_link_hash_entry *) h->root.u.i.link;
638 switch (ELF32_R_TYPE (rel->r_info))
640 /* This relocation describes a 16-bit pointer to a function.
641 We may need to allocate a thunk in low memory; reserve memory
642 for it now. */
643 case R_M32C_16:
644 if (dynobj == NULL)
645 elf_hash_table (info)->dynobj = dynobj = abfd;
646 if (splt == NULL)
648 splt = bfd_get_section_by_name (dynobj, ".plt");
649 if (splt == NULL)
651 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
652 | SEC_IN_MEMORY | SEC_LINKER_CREATED
653 | SEC_READONLY | SEC_CODE);
654 splt = bfd_make_section_with_flags (dynobj, ".plt", flags);
655 if (splt == NULL
656 || ! bfd_set_section_alignment (dynobj, splt, 1))
657 return FALSE;
661 if (h != NULL)
662 offset = &h->plt.offset;
663 else
665 if (local_plt_offsets == NULL)
667 size_t size;
668 unsigned int i;
670 size = symtab_hdr->sh_info * sizeof (bfd_vma);
671 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
672 if (local_plt_offsets == NULL)
673 return FALSE;
674 elf_local_got_offsets (abfd) = local_plt_offsets;
676 for (i = 0; i < symtab_hdr->sh_info; i++)
677 local_plt_offsets[i] = (bfd_vma) -1;
679 offset = &local_plt_offsets[r_symndx];
682 if (*offset == (bfd_vma) -1)
684 *offset = splt->size;
685 splt->size += 4;
687 break;
691 return TRUE;
694 /* This must exist if dynobj is ever set. */
696 static bfd_boolean
697 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
698 struct bfd_link_info *info)
700 bfd *dynobj;
701 asection *splt;
703 /* As an extra sanity check, verify that all plt entries have
704 been filled in. */
706 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
707 && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL)
709 bfd_byte *contents = splt->contents;
710 unsigned int i, size = splt->size;
711 for (i = 0; i < size; i += 4)
713 unsigned int x = bfd_get_32 (dynobj, contents + i);
714 BFD_ASSERT (x != 0);
718 return TRUE;
721 static bfd_boolean
722 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
723 struct bfd_link_info *info)
725 bfd *dynobj;
726 asection *splt;
728 if (info->relocatable)
729 return TRUE;
731 dynobj = elf_hash_table (info)->dynobj;
732 if (dynobj == NULL)
733 return TRUE;
735 splt = bfd_get_section_by_name (dynobj, ".plt");
736 BFD_ASSERT (splt != NULL);
738 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
739 if (splt->contents == NULL)
740 return FALSE;
742 return TRUE;
745 /* Function to set the ELF flag bits. */
747 static bfd_boolean
748 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
750 elf_elfheader (abfd)->e_flags = flags;
751 elf_flags_init (abfd) = TRUE;
752 return TRUE;
755 /* Merge backend specific data from an object file to the output
756 object file when linking. */
758 static bfd_boolean
759 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
761 flagword old_flags, old_partial;
762 flagword new_flags, new_partial;
763 bfd_boolean error = FALSE;
764 char new_opt[80];
765 char old_opt[80];
767 new_opt[0] = old_opt[0] = '\0';
768 new_flags = elf_elfheader (ibfd)->e_flags;
769 old_flags = elf_elfheader (obfd)->e_flags;
771 #ifdef DEBUG
772 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
773 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
774 bfd_get_filename (ibfd));
775 #endif
777 if (!elf_flags_init (obfd))
779 /* First call, no flags set. */
780 elf_flags_init (obfd) = TRUE;
781 elf_elfheader (obfd)->e_flags = new_flags;
784 else if (new_flags == old_flags)
785 /* Compatible flags are ok. */
788 else /* Possibly incompatible flags. */
790 /* Warn if different cpu is used (allow a specific cpu to override
791 the generic cpu). */
792 new_partial = (new_flags & EF_M32C_CPU_MASK);
793 old_partial = (old_flags & EF_M32C_CPU_MASK);
794 if (new_partial == old_partial)
797 else
799 switch (new_partial)
801 default: strcat (new_opt, " -m16c"); break;
802 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
803 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
806 switch (old_partial)
808 default: strcat (old_opt, " -m16c"); break;
809 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
810 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
814 /* Print out any mismatches from above. */
815 if (new_opt[0])
817 error = TRUE;
818 (*_bfd_error_handler)
819 (_("%s: compiled with %s and linked with modules compiled with %s"),
820 bfd_get_filename (ibfd), new_opt, old_opt);
823 new_flags &= ~ EF_M32C_ALL_FLAGS;
824 old_flags &= ~ EF_M32C_ALL_FLAGS;
826 /* Warn about any other mismatches. */
827 if (new_flags != old_flags)
829 error = TRUE;
830 (*_bfd_error_handler)
831 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
832 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
836 if (error)
837 bfd_set_error (bfd_error_bad_value);
839 return !error;
843 static bfd_boolean
844 m32c_elf_print_private_bfd_data (bfd *abfd, PTR ptr)
846 FILE *file = (FILE *) ptr;
847 flagword flags;
849 BFD_ASSERT (abfd != NULL && ptr != NULL);
851 /* Print normal ELF private data. */
852 _bfd_elf_print_private_bfd_data (abfd, ptr);
854 flags = elf_elfheader (abfd)->e_flags;
855 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
857 switch (flags & EF_M32C_CPU_MASK)
859 default: break;
860 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
861 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
864 fputc ('\n', file);
865 return TRUE;
868 /* Return the MACH for an e_flags value. */
870 static int
871 elf32_m32c_machine (bfd *abfd)
873 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
875 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
876 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
879 return bfd_mach_m16c;
882 static bfd_boolean
883 m32c_elf_object_p (bfd *abfd)
885 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
886 elf32_m32c_machine (abfd));
887 return TRUE;
891 #ifdef DEBUG
892 void
893 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
895 size_t locsymcount;
896 Elf_Internal_Sym *isymbuf;
897 Elf_Internal_Sym *isymend;
898 Elf_Internal_Sym *isym;
899 Elf_Internal_Shdr *symtab_hdr;
900 bfd_boolean free_internal = 0, free_external = 0;
901 char * st_info_str;
902 char * st_info_stb_str;
903 char * st_other_str;
904 char * st_shndx_str;
906 if (! internal_syms)
908 internal_syms = bfd_malloc (1000);
909 free_internal = 1;
911 if (! external_syms)
913 external_syms = bfd_malloc (1000);
914 free_external = 1;
917 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
918 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
919 if (free_internal)
920 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
921 symtab_hdr->sh_info, 0,
922 internal_syms, external_syms, NULL);
923 else
924 isymbuf = internal_syms;
925 isymend = isymbuf + locsymcount;
927 for (isym = isymbuf ; isym < isymend ; isym++)
929 switch (ELF_ST_TYPE (isym->st_info))
931 case STT_FUNC: st_info_str = "STT_FUNC";
932 case STT_SECTION: st_info_str = "STT_SECTION";
933 case STT_FILE: st_info_str = "STT_FILE";
934 case STT_OBJECT: st_info_str = "STT_OBJECT";
935 case STT_TLS: st_info_str = "STT_TLS";
936 default: st_info_str = "";
938 switch (ELF_ST_BIND (isym->st_info))
940 case STB_LOCAL: st_info_stb_str = "STB_LOCAL";
941 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL";
942 default: st_info_stb_str = "";
944 switch (ELF_ST_VISIBILITY (isym->st_other))
946 case STV_DEFAULT: st_other_str = "STV_DEFAULT";
947 case STV_INTERNAL: st_other_str = "STV_INTERNAL";
948 case STV_PROTECTED: st_other_str = "STV_PROTECTED";
949 default: st_other_str = "";
951 switch (isym->st_shndx)
953 case SHN_ABS: st_shndx_str = "SHN_ABS";
954 case SHN_COMMON: st_shndx_str = "SHN_COMMON";
955 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF";
956 default: st_shndx_str = "";
959 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
960 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
961 isym,
962 (unsigned long) isym->st_value,
963 (unsigned long) isym->st_size,
964 isym->st_name,
965 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
966 isym->st_name),
967 isym->st_info, st_info_str, st_info_stb_str,
968 isym->st_other, st_other_str,
969 isym->st_shndx, st_shndx_str);
971 if (free_internal)
972 free (internal_syms);
973 if (free_external)
974 free (external_syms);
977 char *
978 m32c_get_reloc (long reloc)
980 if (0 <= reloc && reloc < R_M32C_max)
981 return m32c_elf_howto_table[reloc].name;
982 else
983 return "";
985 #endif /* DEBUG */
987 /* Handle relaxing. */
989 /* A subroutine of m32c_elf_relax_section. If the global symbol H
990 is within the low 64k, remove any entry for it in the plt. */
992 struct relax_plt_data
994 asection *splt;
995 bfd_boolean *again;
998 static bfd_boolean
999 m32c_relax_plt_check (struct elf_link_hash_entry *h,
1000 PTR xdata)
1002 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1004 if (h->root.type == bfd_link_hash_warning)
1005 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1007 if (h->plt.offset != (bfd_vma) -1)
1009 bfd_vma address;
1011 if (h->root.type == bfd_link_hash_undefined
1012 || h->root.type == bfd_link_hash_undefweak)
1013 address = 0;
1014 else
1015 address = (h->root.u.def.section->output_section->vma
1016 + h->root.u.def.section->output_offset
1017 + h->root.u.def.value);
1019 if (address <= 0xffff)
1021 h->plt.offset = -1;
1022 data->splt->size -= 4;
1023 *data->again = TRUE;
1027 return TRUE;
1030 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1031 previously had a plt entry, give it a new entry offset. */
1033 static bfd_boolean
1034 m32c_relax_plt_realloc (struct elf_link_hash_entry *h,
1035 PTR xdata)
1037 bfd_vma *entry = (bfd_vma *) xdata;
1039 if (h->root.type == bfd_link_hash_warning)
1040 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1042 if (h->plt.offset != (bfd_vma) -1)
1044 h->plt.offset = *entry;
1045 *entry += 4;
1048 return TRUE;
1051 static bfd_boolean
1052 m32c_elf_relax_plt_section (bfd *dynobj,
1053 asection *splt,
1054 struct bfd_link_info *info,
1055 bfd_boolean *again)
1057 struct relax_plt_data relax_plt_data;
1058 bfd *ibfd;
1060 /* Assume nothing changes. */
1061 *again = FALSE;
1063 if (info->relocatable)
1064 return TRUE;
1066 /* We only relax the .plt section at the moment. */
1067 if (dynobj != elf_hash_table (info)->dynobj
1068 || strcmp (splt->name, ".plt") != 0)
1069 return TRUE;
1071 /* Quick check for an empty plt. */
1072 if (splt->size == 0)
1073 return TRUE;
1075 /* Map across all global symbols; see which ones happen to
1076 fall in the low 64k. */
1077 relax_plt_data.splt = splt;
1078 relax_plt_data.again = again;
1079 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1080 &relax_plt_data);
1082 /* Likewise for local symbols, though that's somewhat less convenient
1083 as we have to walk the list of input bfds and swap in symbol data. */
1084 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1086 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1087 Elf_Internal_Shdr *symtab_hdr;
1088 Elf_Internal_Sym *isymbuf = NULL;
1089 unsigned int idx;
1091 if (! local_plt_offsets)
1092 continue;
1094 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1095 if (symtab_hdr->sh_info != 0)
1097 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1098 if (isymbuf == NULL)
1099 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1100 symtab_hdr->sh_info, 0,
1101 NULL, NULL, NULL);
1102 if (isymbuf == NULL)
1103 return FALSE;
1106 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1108 Elf_Internal_Sym *isym;
1109 asection *tsec;
1110 bfd_vma address;
1112 if (local_plt_offsets[idx] == (bfd_vma) -1)
1113 continue;
1115 isym = &isymbuf[idx];
1116 if (isym->st_shndx == SHN_UNDEF)
1117 continue;
1118 else if (isym->st_shndx == SHN_ABS)
1119 tsec = bfd_abs_section_ptr;
1120 else if (isym->st_shndx == SHN_COMMON)
1121 tsec = bfd_com_section_ptr;
1122 else
1123 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1125 address = (tsec->output_section->vma
1126 + tsec->output_offset
1127 + isym->st_value);
1128 if (address <= 0xffff)
1130 local_plt_offsets[idx] = -1;
1131 splt->size -= 4;
1132 *again = TRUE;
1136 if (isymbuf != NULL
1137 && symtab_hdr->contents != (unsigned char *) isymbuf)
1139 if (! info->keep_memory)
1140 free (isymbuf);
1141 else
1143 /* Cache the symbols for elf_link_input_bfd. */
1144 symtab_hdr->contents = (unsigned char *) isymbuf;
1149 /* If we changed anything, walk the symbols again to reallocate
1150 .plt entry addresses. */
1151 if (*again && splt->size > 0)
1153 bfd_vma entry = 0;
1155 elf_link_hash_traverse (elf_hash_table (info),
1156 m32c_relax_plt_realloc, &entry);
1158 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1160 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1161 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1162 unsigned int idx;
1164 if (! local_plt_offsets)
1165 continue;
1167 for (idx = 0; idx < nlocals; ++idx)
1168 if (local_plt_offsets[idx] != (bfd_vma) -1)
1170 local_plt_offsets[idx] = entry;
1171 entry += 4;
1176 return TRUE;
1179 static int
1180 compare_reloc (const void *e1, const void *e2)
1182 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1183 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1185 if (i1->r_offset == i2->r_offset)
1186 return 0;
1187 else
1188 return i1->r_offset < i2->r_offset ? -1 : 1;
1191 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1192 static bfd_vma
1193 m32c_offset_for_reloc (bfd *abfd,
1194 Elf_Internal_Rela *rel,
1195 Elf_Internal_Shdr *symtab_hdr,
1196 Elf_External_Sym_Shndx *shndx_buf,
1197 Elf_Internal_Sym *intsyms)
1199 bfd_vma symval;
1201 /* Get the value of the symbol referred to by the reloc. */
1202 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1204 /* A local symbol. */
1205 Elf_Internal_Sym *isym;
1206 Elf_External_Sym_Shndx *shndx;
1207 asection *ssec;
1210 isym = intsyms + ELF32_R_SYM (rel->r_info);
1211 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1212 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (rel->r_info) : 0);
1214 symval = isym->st_value;
1215 if (ssec)
1216 symval += ssec->output_section->vma
1217 + ssec->output_offset;
1219 else
1221 unsigned long indx;
1222 struct elf_link_hash_entry *h;
1224 /* An external symbol. */
1225 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1226 h = elf_sym_hashes (abfd)[indx];
1227 BFD_ASSERT (h != NULL);
1229 if (h->root.type != bfd_link_hash_defined
1230 && h->root.type != bfd_link_hash_defweak)
1231 /* This appears to be a reference to an undefined
1232 symbol. Just ignore it--it will be caught by the
1233 regular reloc processing. */
1234 return 0;
1236 symval = (h->root.u.def.value
1237 + h->root.u.def.section->output_section->vma
1238 + h->root.u.def.section->output_offset);
1240 return symval;
1243 static int bytes_saved = 0;
1245 static int bytes_to_reloc[] = {
1246 R_M32C_NONE,
1247 R_M32C_8,
1248 R_M32C_16,
1249 R_M32C_24,
1250 R_M32C_32
1253 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1255 /* Mask for the number of relocs associated with this insn. */
1256 #define RLA_RELOCS 0x0000000f
1257 /* Number of bytes gas emitted (before gas's relaxing) */
1258 #define RLA_NBYTES 0x00000ff0
1260 /* If the displacement is within the given range and the new encoding
1261 differs from the old encoding (the index), then the insn can be
1262 relaxed to the new encoding. */
1263 typedef struct {
1264 int bytes;
1265 unsigned int max_disp;
1266 unsigned char new_encoding;
1267 } EncodingTable;
1269 static EncodingTable m16c_addr_encodings[] = {
1270 { 0, 0, 0 }, /* R0 */
1271 { 0, 0, 1 }, /* R1 */
1272 { 0, 0, 2 }, /* R2 */
1273 { 0, 0, 3 }, /* R3 */
1274 { 0, 0, 4 }, /* A0 */
1275 { 0, 0, 5 }, /* A1 */
1276 { 0, 0, 6 }, /* [A0] */
1277 { 0, 0, 7 }, /* [A1] */
1278 { 1, 0, 6 }, /* udsp:8[A0] */
1279 { 1, 0, 7 }, /* udsp:8[A1] */
1280 { 1, 0, 10 }, /* udsp:8[SB] */
1281 { 1, 0, 11 }, /* sdsp:8[FB] */
1282 { 2, 255, 8 }, /* udsp:16[A0] */
1283 { 2, 255, 9 }, /* udsp:16[A1] */
1284 { 2, 255, 10 }, /* udsp:16[SB] */
1285 { 2, 0, 15 }, /* abs:16 */
1288 static EncodingTable m16c_jmpaddr_encodings[] = {
1289 { 0, 0, 0 }, /* R0 */
1290 { 0, 0, 1 }, /* R1 */
1291 { 0, 0, 2 }, /* R2 */
1292 { 0, 0, 3 }, /* R3 */
1293 { 0, 0, 4 }, /* A0 */
1294 { 0, 0, 5 }, /* A1 */
1295 { 0, 0, 6 }, /* [A0] */
1296 { 0, 0, 7 }, /* [A1] */
1297 { 1, 0, 6 }, /* udsp:8[A0] */
1298 { 1, 0, 7 }, /* udsp:8[A1] */
1299 { 1, 0, 10 }, /* udsp:8[SB] */
1300 { 1, 0, 11 }, /* sdsp:8[FB] */
1301 { 3, 255, 8 }, /* udsp:20[A0] */
1302 { 3, 255, 9 }, /* udsp:20[A1] */
1303 { 2, 255, 10 }, /* udsp:16[SB] */
1304 { 2, 0, 15 }, /* abs:16 */
1307 static EncodingTable m32c_addr_encodings[] = {
1308 { 0, 0, 0 }, /* [A0] */
1309 { 0, 0, 1 }, /* [A1] */
1310 { 0, 0, 2 }, /* A0 */
1311 { 0, 0, 3 }, /* A1 */
1312 { 1, 0, 0 }, /* udsp:8[A0] */
1313 { 1, 0, 1 }, /* udsp:8[A1] */
1314 { 1, 0, 6 }, /* udsp:8[SB] */
1315 { 1, 0, 7 }, /* sdsp:8[FB] */
1316 { 2, 255, 4 }, /* udsp:16[A0] */
1317 { 2, 255, 5 }, /* udsp:16[A1] */
1318 { 2, 255, 6 }, /* udsp:16[SB] */
1319 { 2, 127, 7 }, /* sdsp:16[FB] */
1320 { 3, 65535, 8 }, /* udsp:24[A0] */
1321 { 3, 65535, 9 }, /* udsp:24[A1] */
1322 { 3, 65535, 15 }, /* abs24 */
1323 { 2, 0, 15 }, /* abs16 */
1324 { 0, 0, 16 }, /* R2 */
1325 { 0, 0, 17 }, /* R3 */
1326 { 0, 0, 18 }, /* R0 */
1327 { 0, 0, 19 }, /* R1 */
1328 { 0, 0, 20 }, /* */
1329 { 0, 0, 21 }, /* */
1330 { 0, 0, 22 }, /* */
1331 { 0, 0, 23 }, /* */
1332 { 0, 0, 24 }, /* */
1333 { 0, 0, 25 }, /* */
1334 { 0, 0, 26 }, /* */
1335 { 0, 0, 27 }, /* */
1336 { 0, 0, 28 }, /* */
1337 { 0, 0, 29 }, /* */
1338 { 0, 0, 30 }, /* */
1339 { 0, 0, 31 }, /* */
1342 static bfd_boolean
1343 m32c_elf_relax_section
1344 (bfd * abfd,
1345 asection * sec,
1346 struct bfd_link_info * link_info,
1347 bfd_boolean * again)
1349 Elf_Internal_Shdr *symtab_hdr;
1350 Elf_Internal_Shdr *shndx_hdr;
1351 Elf_Internal_Rela *internal_relocs;
1352 Elf_Internal_Rela *free_relocs = NULL;
1353 Elf_Internal_Rela *irel, *irelend, *srel;
1354 bfd_byte * contents = NULL;
1355 bfd_byte * free_contents = NULL;
1356 Elf_Internal_Sym *intsyms = NULL;
1357 Elf_Internal_Sym *free_intsyms = NULL;
1358 Elf_External_Sym_Shndx *shndx_buf = NULL;
1359 int machine;
1361 if (abfd == elf_hash_table (link_info)->dynobj
1362 && strcmp (sec->name, ".plt") == 0)
1363 return m32c_elf_relax_plt_section (abfd, sec, link_info, again);
1365 /* Assume nothing changes. */
1366 *again = FALSE;
1368 machine = elf32_m32c_machine (abfd);
1370 /* We don't have to do anything for a relocatable link, if
1371 this section does not have relocs, or if this is not a
1372 code section. */
1373 if (link_info->relocatable
1374 || (sec->flags & SEC_RELOC) == 0
1375 || sec->reloc_count == 0
1376 || (sec->flags & SEC_CODE) == 0)
1377 return TRUE;
1379 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1380 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1382 /* Get the section contents. */
1383 if (elf_section_data (sec)->this_hdr.contents != NULL)
1384 contents = elf_section_data (sec)->this_hdr.contents;
1385 /* Go get them off disk. */
1386 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1387 goto error_return;
1389 /* Read this BFD's symbols. */
1390 /* Get cached copy if it exists. */
1391 if (symtab_hdr->contents != NULL)
1393 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1395 else
1397 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1398 symtab_hdr->contents = (bfd_byte *) intsyms;
1401 if (shndx_hdr->sh_size != 0)
1403 bfd_size_type amt;
1405 amt = symtab_hdr->sh_info;
1406 amt *= sizeof (Elf_External_Sym_Shndx);
1407 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1408 if (shndx_buf == NULL)
1409 goto error_return;
1410 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1411 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1412 goto error_return;
1413 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1416 /* Get a copy of the native relocations. */
1417 internal_relocs = (_bfd_elf_link_read_relocs
1418 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1419 link_info->keep_memory));
1420 if (internal_relocs == NULL)
1421 goto error_return;
1422 if (! link_info->keep_memory)
1423 free_relocs = internal_relocs;
1425 /* The RL_ relocs must be just before the operand relocs they go
1426 with, so we must sort them to guarantee this. */
1427 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1428 compare_reloc);
1430 /* Walk through them looking for relaxing opportunities. */
1431 irelend = internal_relocs + sec->reloc_count;
1433 for (irel = internal_relocs; irel < irelend; irel++)
1435 bfd_vma symval;
1436 unsigned char *insn, *gap, *einsn;
1437 bfd_vma pc;
1438 bfd_signed_vma pcrel;
1439 int relax_relocs;
1440 int gap_size;
1441 int new_type;
1442 int posn;
1443 int enc;
1444 EncodingTable *enctbl;
1445 EncodingTable *e;
1447 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1448 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1449 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1450 continue;
1452 srel = irel;
1454 /* There will always be room for the relaxed insn, since it is smaller
1455 than the one it would replace. */
1456 BFD_ASSERT (irel->r_offset < sec->size);
1458 insn = contents + irel->r_offset;
1459 relax_relocs = irel->r_addend % 16;
1461 /* Ok, we only have three relocs we care about, and they're all
1462 fake. The lower four bits of the addend is always the number
1463 of following relocs (hence the qsort above) that are assigned
1464 to this opcode. The next 8 bits of the addend indicates the
1465 number of bytes in the insn. We use the rest of them
1466 ourselves as flags for the more expensive operations (defines
1467 above). The three relocs are:
1469 RL_JUMP: This marks all direct jump insns. We check the
1470 displacement and replace them with shorter jumps if
1471 they're in range. We also use this to find JMP.S
1472 insns and manually shorten them when we delete bytes.
1473 We have to decode these insns to figure out what to
1476 RL_1ADDR: This is a :G or :Q insn, which has a single
1477 "standard" operand. We have to extract the type
1478 field, see if it's a wide displacement, then figure
1479 out if we can replace it with a narrow displacement.
1480 We don't have to decode these insns.
1482 RL_2ADDR: Similarly, but two "standard" operands. Note that
1483 r_addend may still be 1, as standard operands don't
1484 always have displacements. Gas shouldn't give us one
1485 with zero operands, but since we don't know which one
1486 has the displacement, we check them both anyway.
1488 These all point to the beginning of the insn itself, not the
1489 operands.
1491 Note that we only relax one step at a time, relying on the
1492 linker to call us repeatedly. Thus, there is no code for
1493 JMP.A->JMP.B although that will happen in two steps.
1494 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1497 /* Get the value of the symbol referred to by the reloc. Just
1498 in case this is the last reloc in the list, use the RL's
1499 addend to choose between this reloc (no addend) or the next
1500 (yes addend, which means at least one following reloc). */
1501 srel = irel + (relax_relocs ? 1 : 0);
1502 symval = OFFSET_FOR_RELOC (srel);
1504 /* Setting gap_size nonzero is the flag which means "something
1505 shrunk". */
1506 gap_size = 0;
1507 gap = NULL;
1508 new_type = ELF32_R_TYPE(srel->r_info);
1510 pc = sec->output_section->vma + sec->output_offset
1511 + srel->r_offset;
1512 pcrel = symval - pc + srel->r_addend;
1514 if (machine == bfd_mach_m16c)
1516 /* R8C / M16C */
1518 switch (ELF32_R_TYPE(irel->r_info))
1521 case R_M32C_RL_JUMP:
1522 switch (insn[0])
1524 case 0xfe: /* jmp.b */
1525 if (pcrel >= 2 && pcrel <= 9)
1527 /* Relax JMP.B -> JMP.S. We need to get rid of
1528 the following reloc though. */
1529 insn[0] = 0x60 | (pcrel - 2);
1530 new_type = R_M32C_NONE;
1531 irel->r_addend = 0x10;
1532 gap_size = 1;
1533 gap = insn + 1;
1535 break;
1537 case 0xf4: /* jmp.w */
1538 /* 128 is allowed because it will be one byte closer
1539 after relaxing. Likewise for all other pc-rel
1540 jumps. */
1541 if (pcrel <= 128 && pcrel >= -128)
1543 /* Relax JMP.W -> JMP.B */
1544 insn[0] = 0xfe;
1545 insn[1] = 0;
1546 new_type = R_M32C_8_PCREL;
1547 gap_size = 1;
1548 gap = insn + 2;
1550 break;
1552 case 0xfc: /* jmp.a */
1553 if (pcrel <= 32768 && pcrel >= -32768)
1555 /* Relax JMP.A -> JMP.W */
1556 insn[0] = 0xf4;
1557 insn[1] = 0;
1558 insn[2] = 0;
1559 new_type = R_M32C_16_PCREL;
1560 gap_size = 1;
1561 gap = insn + 3;
1563 break;
1565 case 0xfd: /* jsr.a */
1566 if (pcrel <= 32768 && pcrel >= -32768)
1568 /* Relax JSR.A -> JSR.W */
1569 insn[0] = 0xf5;
1570 insn[1] = 0;
1571 insn[2] = 0;
1572 new_type = R_M32C_16_PCREL;
1573 gap_size = 1;
1574 gap = insn + 3;
1576 break;
1578 break;
1580 case R_M32C_RL_2ADDR:
1581 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1583 enctbl = m16c_addr_encodings;
1584 posn = 2;
1585 enc = (insn[1] >> 4) & 0x0f;
1586 e = & enctbl[enc];
1588 if (srel->r_offset == irel->r_offset + posn
1589 && e->new_encoding != enc
1590 && symval <= e->max_disp)
1592 insn[1] &= 0x0f;
1593 insn[1] |= e->new_encoding << 4;
1594 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1595 gap = insn + posn + enctbl[e->new_encoding].bytes;
1596 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1597 break;
1599 if (relax_relocs == 2)
1600 srel ++;
1601 posn += e->bytes;
1603 goto try_1addr_16;
1605 case R_M32C_RL_1ADDR:
1606 /* xxxx xxxx xxxx dest [disp] */
1608 enctbl = m16c_addr_encodings;
1609 posn = 2;
1611 /* Check the opcode for jumps. We know it's safe to
1612 do this because all 2ADDR insns are at least two
1613 bytes long. */
1614 enc = insn[0] * 256 + insn[1];
1615 enc &= 0xfff0;
1616 if (enc == 0x7d20
1617 || enc == 0x7d00
1618 || enc == 0x7d30
1619 || enc == 0x7d10)
1621 enctbl = m16c_jmpaddr_encodings;
1624 try_1addr_16:
1625 /* srel, posn, and enc must be set here. */
1627 symval = OFFSET_FOR_RELOC (srel);
1628 enc = insn[1] & 0x0f;
1629 e = & enctbl[enc];
1631 if (srel->r_offset == irel->r_offset + posn
1632 && e->new_encoding != enc
1633 && symval <= e->max_disp)
1635 insn[1] &= 0xf0;
1636 insn[1] |= e->new_encoding;
1637 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1638 gap = insn + posn + enctbl[e->new_encoding].bytes;
1639 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1640 break;
1643 break;
1645 } /* Ends switch (reloc type) for m16c. */
1647 else /* machine == bfd_mach_m32c */
1649 /* M32CM / M32C */
1651 switch (ELF32_R_TYPE(irel->r_info))
1654 case R_M32C_RL_JUMP:
1655 switch (insn[0])
1657 case 0xbb: /* jmp.b */
1658 if (pcrel >= 2 && pcrel <= 9)
1660 int p = pcrel - 2;
1661 /* Relax JMP.B -> JMP.S. We need to get rid of
1662 the following reloc though. */
1663 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1664 new_type = R_M32C_NONE;
1665 irel->r_addend = 0x10;
1666 gap_size = 1;
1667 gap = insn + 1;
1669 break;
1671 case 0xce: /* jmp.w */
1672 if (pcrel <= 128 && pcrel >= -128)
1674 /* Relax JMP.W -> JMP.B */
1675 insn[0] = 0xbb;
1676 insn[1] = 0;
1677 new_type = R_M32C_8_PCREL;
1678 gap_size = 1;
1679 gap = insn + 2;
1681 break;
1683 case 0xcc: /* jmp.a */
1684 if (pcrel <= 32768 && pcrel >= -32768)
1686 /* Relax JMP.A -> JMP.W */
1687 insn[0] = 0xce;
1688 insn[1] = 0;
1689 insn[2] = 0;
1690 new_type = R_M32C_16_PCREL;
1691 gap_size = 1;
1692 gap = insn + 3;
1694 break;
1696 case 0xcd: /* jsr.a */
1697 if (pcrel <= 32768 && pcrel >= -32768)
1699 /* Relax JSR.A -> JSR.W */
1700 insn[0] = 0xcf;
1701 insn[1] = 0;
1702 insn[2] = 0;
1703 new_type = R_M32C_16_PCREL;
1704 gap_size = 1;
1705 gap = insn + 3;
1707 break;
1709 break;
1711 case R_M32C_RL_2ADDR:
1712 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1714 einsn = insn;
1715 posn = 2;
1716 if (einsn[0] == 1)
1718 /* prefix; remove it as far as the RL reloc is concerned. */
1719 einsn ++;
1720 posn ++;
1723 enctbl = m32c_addr_encodings;
1724 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1725 e = & enctbl[enc];
1727 if (srel->r_offset == irel->r_offset + posn
1728 && e->new_encoding != enc
1729 && symval <= e->max_disp)
1731 einsn[0] &= 0x8f;
1732 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1733 einsn[1] &= 0xcf;
1734 einsn[1] |= (e->new_encoding & 0x03) << 4;
1735 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1736 gap = insn + posn + enctbl[e->new_encoding].bytes;
1737 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1738 break;
1740 if (relax_relocs == 2)
1741 srel ++;
1742 posn += e->bytes;
1744 goto try_1addr_32;
1746 case R_M32C_RL_1ADDR:
1747 /* xxxx DDDx DDxx xxxx [disp] */
1749 einsn = insn;
1750 posn = 2;
1751 if (einsn[0] == 1)
1753 /* prefix; remove it as far as the RL reloc is concerned. */
1754 einsn ++;
1755 posn ++;
1758 enctbl = m32c_addr_encodings;
1760 try_1addr_32:
1761 /* srel, posn, and enc must be set here. */
1763 symval = OFFSET_FOR_RELOC (srel);
1764 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1765 e = & enctbl[enc];
1767 if (srel->r_offset == irel->r_offset + posn
1768 && e->new_encoding != enc
1769 && symval <= e->max_disp)
1771 einsn[0] &= 0xf1;
1772 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1773 einsn[1] &= 0x3f;
1774 einsn[1] |= (e->new_encoding & 0x03) << 6;
1775 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1776 gap = insn + posn + enctbl[e->new_encoding].bytes;
1777 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1778 break;
1781 break;
1783 } /* Ends switch (reloc type) for m32c. */
1786 if (gap_size == 0)
1787 continue;
1789 *again = TRUE;
1791 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1793 /* Note that we've changed the relocs, section contents, etc. */
1794 elf_section_data (sec)->relocs = internal_relocs;
1795 free_relocs = NULL;
1797 elf_section_data (sec)->this_hdr.contents = contents;
1798 free_contents = NULL;
1800 symtab_hdr->contents = (bfd_byte *) intsyms;
1801 free_intsyms = NULL;
1803 bytes_saved += gap_size;
1805 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1806 goto error_return;
1808 } /* next relocation */
1810 if (free_relocs != NULL)
1812 free (free_relocs);
1813 free_relocs = NULL;
1816 if (free_contents != NULL)
1818 if (! link_info->keep_memory)
1819 free (free_contents);
1820 /* Cache the section contents for elf_link_input_bfd. */
1821 else
1822 elf_section_data (sec)->this_hdr.contents = contents;
1824 free_contents = NULL;
1827 if (shndx_buf != NULL)
1829 shndx_hdr->contents = NULL;
1830 free (shndx_buf);
1833 if (free_intsyms != NULL)
1835 if (! link_info->keep_memory)
1836 free (free_intsyms);
1837 /* Cache the symbols for elf_link_input_bfd. */
1838 else
1840 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1843 free_intsyms = NULL;
1846 return TRUE;
1848 error_return:
1849 if (free_relocs != NULL)
1850 free (free_relocs);
1851 if (free_contents != NULL)
1852 free (free_contents);
1853 if (shndx_buf != NULL)
1855 shndx_hdr->contents = NULL;
1856 free (shndx_buf);
1858 if (free_intsyms != NULL)
1859 free (free_intsyms);
1860 return FALSE;
1863 /* Delete some bytes from a section while relaxing. */
1865 static bfd_boolean
1866 m32c_elf_relax_delete_bytes
1867 (bfd * abfd,
1868 asection * sec,
1869 bfd_vma addr,
1870 int count)
1872 Elf_Internal_Shdr *symtab_hdr;
1873 Elf_Internal_Shdr *shndx_hdr;
1874 int sec_shndx;
1875 bfd_byte *contents;
1876 Elf_Internal_Rela *irel;
1877 Elf_Internal_Rela *irelend;
1878 Elf_Internal_Rela *irelalign;
1879 bfd_vma toaddr;
1880 Elf_Internal_Sym *isym;
1881 Elf_Internal_Sym *isymend;
1882 Elf_Internal_Sym *intsyms;
1883 Elf_External_Sym_Shndx *shndx_buf;
1884 Elf_External_Sym_Shndx *shndx;
1885 struct elf_link_hash_entry ** sym_hashes;
1886 struct elf_link_hash_entry ** end_hashes;
1887 unsigned int symcount;
1889 contents = elf_section_data (sec)->this_hdr.contents;
1891 /* The deletion must stop at the next ALIGN reloc for an aligment
1892 power larger than the number of bytes we are deleting. */
1893 irelalign = NULL;
1894 toaddr = sec->size;
1896 irel = elf_section_data (sec)->relocs;
1897 irelend = irel + sec->reloc_count;
1899 /* Actually delete the bytes. */
1900 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1901 sec->size -= count;
1903 /* Adjust all the relocs. */
1904 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1906 /* Get the new reloc address. */
1907 if (irel->r_offset > addr && irel->r_offset < toaddr)
1908 irel->r_offset -= count;
1910 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1911 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1912 && irel->r_offset + 1 < addr
1913 && irel->r_offset + 7 > addr)
1915 bfd_vma disp;
1916 unsigned char *insn = &contents[irel->r_offset];
1917 disp = *insn;
1918 /* This is a JMP.S, which we have to manually update. */
1919 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1921 if ((*insn & 0xf8) != 0x60)
1922 continue;
1923 disp = (disp & 7);
1925 else
1927 if ((*insn & 0xce) != 0x4a)
1928 continue;
1929 disp = ((disp & 0x30) >> 3) | (disp & 1);
1931 if (irel->r_offset + disp + 2 >= addr+count)
1933 disp -= count;
1934 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1936 *insn = (*insn & 0xf8) | disp;
1938 else
1940 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
1946 /* Adjust the local symbols defined in this section. */
1947 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1948 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1949 isym = intsyms;
1950 isymend = isym + symtab_hdr->sh_info;
1952 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1953 shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr;
1954 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1955 shndx = shndx_buf;
1957 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
1960 if ((int) isym->st_shndx == sec_shndx
1961 && isym->st_value > addr
1962 && isym->st_value < toaddr)
1964 isym->st_value -= count;
1968 /* Now adjust the global symbols defined in this section. */
1969 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1970 - symtab_hdr->sh_info);
1971 sym_hashes = elf_sym_hashes (abfd);
1972 // sym_hashes += symtab_hdr->sh_info;
1973 end_hashes = sym_hashes + symcount;
1975 for (; sym_hashes < end_hashes; sym_hashes ++)
1977 struct elf_link_hash_entry * sym_hash = * sym_hashes;
1979 if (sym_hash &&
1980 ( sym_hash->root.type == bfd_link_hash_defined
1981 || sym_hash->root.type == bfd_link_hash_defweak)
1982 && sym_hash->root.u.def.section == sec
1983 && sym_hash->root.u.def.value > addr
1984 && sym_hash->root.u.def.value < toaddr)
1986 sym_hash->root.u.def.value -= count;
1990 return TRUE;
1994 #define ELF_ARCH bfd_arch_m32c
1995 #define ELF_MACHINE_CODE EM_M32C
1996 #define ELF_MAXPAGESIZE 0x1000
1998 #if 0
1999 #define TARGET_BIG_SYM bfd_elf32_m32c_vec
2000 #define TARGET_BIG_NAME "elf32-m32c"
2001 #else
2002 #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
2003 #define TARGET_LITTLE_NAME "elf32-m32c"
2004 #endif
2006 #define elf_info_to_howto_rel NULL
2007 #define elf_info_to_howto m32c_info_to_howto_rela
2008 #define elf_backend_object_p m32c_elf_object_p
2009 #define elf_backend_relocate_section m32c_elf_relocate_section
2010 #define elf_backend_check_relocs m32c_elf_check_relocs
2011 #define elf_backend_object_p m32c_elf_object_p
2012 #define elf_symbol_leading_char ('_')
2013 #define elf_backend_always_size_sections \
2014 m32c_elf_always_size_sections
2015 #define elf_backend_finish_dynamic_sections \
2016 m32c_elf_finish_dynamic_sections
2018 #define elf_backend_can_gc_sections 1
2020 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2021 #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2022 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2023 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2024 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2025 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2027 #include "elf32-target.h"