* elf32-xtensa.c (build_reloc_opcodes): New.
[deliverable/binutils-gdb.git] / bfd / elf32-m32c.c
CommitLineData
49f58d10 1/* M16C/M32C specific support for 32-bit ELF.
6772dd07 2 Copyright (C) 2005, 2006
49f58d10
JB
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24#include "elf-bfd.h"
25#include "elf/m32c.h"
26#include "libiberty.h"
27
28/* Forward declarations. */
29static reloc_howto_type * m32c_reloc_type_lookup
30 (bfd *, bfd_reloc_code_real_type);
31static void m32c_info_to_howto_rela
32 (bfd *, arelent *, Elf_Internal_Rela *);
33static bfd_boolean m32c_elf_relocate_section
34 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
35static bfd_boolean m32c_elf_gc_sweep_hook
36 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
37static asection * m32c_elf_gc_mark_hook
38 (asection *, struct bfd_link_info *, Elf_Internal_Rela *, struct elf_link_hash_entry *, Elf_Internal_Sym *);
39static bfd_boolean m32c_elf_check_relocs
40 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
41static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
42#ifdef DEBUG
43static char * m32c_get_reloc (long reloc);
44#endif
45static bfd_boolean m32c_elf_relax_section
46(bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
47
48
49static reloc_howto_type m32c_elf_howto_table [] =
50{
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 */
65
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 */
6772dd07 77 0xffff, /* dst_mask */
49f58d10
JB
78 FALSE), /* pcrel_offset */
79
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 */
6772dd07 91 0xffffff, /* dst_mask */
49f58d10
JB
92 FALSE), /* pcrel_offset */
93
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 */
107
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 */
6772dd07 119 0xff, /* dst_mask */
49f58d10
JB
120 TRUE), /* pcrel_offset */
121
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 */
6772dd07 133 0xffff, /* dst_mask */
49f58d10 134 TRUE), /* pcrel_offset */
fd54057a
DD
135
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 */
6772dd07 147 0xff, /* dst_mask */
fd54057a
DD
148 FALSE), /* pcrel_offset */
149
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 */
6772dd07 161 0xffff, /* dst_mask */
fd54057a
DD
162 FALSE), /* pcrel_offset */
163
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 */
6772dd07 175 0xff, /* dst_mask */
fd54057a
DD
176 FALSE), /* pcrel_offset */
177
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 */
6772dd07 189 0xffff, /* dst_mask */
fd54057a 190 FALSE), /* pcrel_offset */
6772dd07
DD
191
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 */
205
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 */
219
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 */
233
49f58d10
JB
234};
235\f
236/* Map BFD reloc types to M32C ELF reloc types. */
237
238struct m32c_reloc_map
239{
240 bfd_reloc_code_real_type bfd_reloc_val;
241 unsigned int m32c_reloc_val;
242};
243
244static const struct m32c_reloc_map m32c_reloc_map [] =
245{
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 },
fd54057a
DD
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 },
6772dd07
DD
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 }
49f58d10
JB
259};
260
261static reloc_howto_type *
262m32c_reloc_type_lookup
263 (bfd * abfd ATTRIBUTE_UNUSED,
264 bfd_reloc_code_real_type code)
265{
266 unsigned int i;
267
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];
271
272 return NULL;
273}
274
275/* Set the howto pointer for an M32C ELF reloc. */
276
277static void
278m32c_info_to_howto_rela
279 (bfd * abfd ATTRIBUTE_UNUSED,
280 arelent * cache_ptr,
281 Elf_Internal_Rela * dst)
282{
283 unsigned int r_type;
284
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];
288}
289
290\f
291
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.
296
297 The RELOCATE_SECTION function is called by the new ELF backend linker
298 to handle the relocations for a section.
299
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.
303
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.
307
308 This function does not have to worry about setting the reloc
309 address or the reloc symbol index.
310
311 LOCAL_SYMS is a pointer to the swapped in local symbols.
312
313 LOCAL_SECTIONS is an array giving the section in the input file
314 corresponding to the st_shndx field of each local symbol.
315
316 The global hash table entry for the global symbols can be found
317 via elf_sym_hashes (input_bfd).
318
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. */
324
325static bfd_boolean
326m32c_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)
335{
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;
342
343 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
344 sym_hashes = elf_sym_hashes (input_bfd);
345 relend = relocs + input_section->reloc_count;
346
347 dynobj = elf_hash_table (info)->dynobj;
348 splt = NULL;
349 if (dynobj != NULL)
350 splt = bfd_get_section_by_name (dynobj, ".plt");
351
352 for (rel = relocs; rel < relend; rel ++)
353 {
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;
363
364 r_type = ELF32_R_TYPE (rel->r_info);
6772dd07
DD
365
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;
49f58d10
JB
372
373 r_symndx = ELF32_R_SYM (rel->r_info);
374
375 if (info->relocatable)
376 {
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)
382 {
383 sym = local_syms + r_symndx;
384
385 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
386 {
387 sec = local_sections [r_symndx];
388 rel->r_addend += sec->output_offset + sym->st_value;
389 }
390 }
391
392 continue;
393 }
394
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;
6772dd07 400
49f58d10
JB
401 if (r_symndx < symtab_hdr->sh_info)
402 {
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);
408
409 name = bfd_elf_string_from_elf_section
410 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6772dd07 411 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
49f58d10
JB
412 }
413 else
414 {
415 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
416
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;
420
421 name = h->root.root.string;
422
423 if (h->root.type == bfd_link_hash_defined
424 || h->root.type == bfd_link_hash_defweak)
425 {
426 sec = h->root.u.def.section;
427 relocation = (h->root.u.def.value
428 + sec->output_section->vma
429 + sec->output_offset);
430 }
431 else if (h->root.type == bfd_link_hash_undefweak)
432 {
433 relocation = 0;
434 }
435 else
436 {
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;
442 }
443 }
444
445 switch (ELF32_R_TYPE (rel->r_info))
446 {
447 case R_M32C_16:
448 {
449 bfd_vma *plt_offset;
450
451 if (h != NULL)
452 plt_offset = &h->plt.offset;
453 else
454 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
455
456 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
457 relocation, *plt_offset);*/
458 if (relocation <= 0xffff)
459 {
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);
463 }
464 else
465 {
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);
469
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)
473 {
474 unsigned int x;
475
476 x = 0x000000fc; /* jmpf */
477 x |= (relocation << 8) & 0xffffff00;
478 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
479 *plt_offset |= 1;
480 }
481
482 relocation = (splt->output_section->vma
483 + splt->output_offset
484 + (*plt_offset & -2));
35520cb7
DD
485 if (name)
486 {
487 char *newname = bfd_malloc (strlen(name)+5);
488 strcpy (newname, name);
489 strcat(newname, ".plt");
490 _bfd_generic_link_add_one_symbol (info,
491 input_bfd,
492 newname,
493 BSF_FUNCTION | BSF_WEAK,
494 splt,
495 (*plt_offset & -2),
496 0,
497 1,
498 0,
499 0);
500 }
49f58d10
JB
501 }
502 }
503 break;
fd54057a
DD
504
505 case R_M32C_HI8:
506 case R_M32C_HI16:
507 relocation >>= 16;
508 break;
49f58d10
JB
509 }
510
6772dd07 511#if 0
a1013894
DD
512 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
513 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
514 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
515 relocation, rel->r_addend);
6772dd07
DD
516 {
517 int i;
518 for (i=0; i<4; i++)
a1013894
DD
519 printf (" %02x", contents[rel->r_offset+i]);
520 printf ("\n");
6772dd07
DD
521 }
522#endif
49f58d10
JB
523 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
524 contents, rel->r_offset, relocation,
525 rel->r_addend);
526
527 if (r != bfd_reloc_ok)
528 {
529 const char * msg = (const char *) NULL;
530
531 switch (r)
532 {
533 case bfd_reloc_overflow:
534 r = info->callbacks->reloc_overflow
535 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
536 input_bfd, input_section, rel->r_offset);
537 break;
538
539 case bfd_reloc_undefined:
540 r = info->callbacks->undefined_symbol
541 (info, name, input_bfd, input_section, rel->r_offset,
542 TRUE);
543 break;
544
545 case bfd_reloc_outofrange:
546 msg = _("internal error: out of range error");
547 break;
548
549 case bfd_reloc_notsupported:
550 msg = _("internal error: unsupported relocation error");
551 break;
552
553 case bfd_reloc_dangerous:
554 msg = _("internal error: dangerous relocation");
555 break;
556
557 default:
558 msg = _("internal error: unknown error");
559 break;
560 }
561
562 if (msg)
563 r = info->callbacks->warning
564 (info, msg, name, input_bfd, input_section, rel->r_offset);
565
566 if (! r)
567 return FALSE;
568 }
569 }
570
571 return TRUE;
572}
573\f
574/* Return the section that should be marked against GC for a given
575 relocation. */
576
577static asection *
578m32c_elf_gc_mark_hook
579 (asection * sec,
580 struct bfd_link_info * info ATTRIBUTE_UNUSED,
581 Elf_Internal_Rela * rel,
582 struct elf_link_hash_entry * h,
583 Elf_Internal_Sym * sym)
584{
585 if (h != NULL)
586 {
587 switch (ELF32_R_TYPE (rel->r_info))
588 {
589 default:
590 switch (h->root.type)
591 {
592 case bfd_link_hash_defined:
593 case bfd_link_hash_defweak:
594 return h->root.u.def.section;
595
596 case bfd_link_hash_common:
597 return h->root.u.c.p->section;
598
599 default:
600 break;
601 }
602 }
603 }
604 else
605 {
606 if (!(elf_bad_symtab (sec->owner)
607 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
608 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
609 && sym->st_shndx != SHN_COMMON))
610 {
611 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
612 }
613 }
614
615 return NULL;
616}
617
618/* Update the got entry reference counts for the section being removed. */
619
620static bfd_boolean
621m32c_elf_gc_sweep_hook
622 (bfd * abfd ATTRIBUTE_UNUSED,
623 struct bfd_link_info * info ATTRIBUTE_UNUSED,
624 asection * sec ATTRIBUTE_UNUSED,
625 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
626{
627 return TRUE;
628}
629
630/* We support 16-bit pointers to code above 64k by generating a thunk
631 below 64k containing a JMP instruction to the final address. */
632
633static bfd_boolean
634m32c_elf_check_relocs
635 (bfd * abfd,
636 struct bfd_link_info * info,
637 asection * sec,
638 const Elf_Internal_Rela * relocs)
639{
640 Elf_Internal_Shdr * symtab_hdr;
641 struct elf_link_hash_entry ** sym_hashes;
642 struct elf_link_hash_entry ** sym_hashes_end;
643 const Elf_Internal_Rela * rel;
644 const Elf_Internal_Rela * rel_end;
645 bfd_vma *local_plt_offsets;
646 asection *splt;
647 bfd *dynobj;
648
649 if (info->relocatable)
650 return TRUE;
651
652 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
653 sym_hashes = elf_sym_hashes (abfd);
654 local_plt_offsets = elf_local_got_offsets (abfd);
655 splt = NULL;
656 dynobj = elf_hash_table(info)->dynobj;
657
658 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
659 if (!elf_bad_symtab (abfd))
660 sym_hashes_end -= symtab_hdr->sh_info;
661
662 rel_end = relocs + sec->reloc_count;
663 for (rel = relocs; rel < rel_end; rel++)
664 {
665 struct elf_link_hash_entry *h;
666 unsigned long r_symndx;
667 bfd_vma *offset;
668
669 r_symndx = ELF32_R_SYM (rel->r_info);
670 if (r_symndx < symtab_hdr->sh_info)
671 h = NULL;
672 else
673 {
674 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
675 while (h->root.type == bfd_link_hash_indirect
676 || h->root.type == bfd_link_hash_warning)
677 h = (struct elf_link_hash_entry *) h->root.u.i.link;
678 }
679
680 switch (ELF32_R_TYPE (rel->r_info))
681 {
682 /* This relocation describes a 16-bit pointer to a function.
683 We may need to allocate a thunk in low memory; reserve memory
684 for it now. */
685 case R_M32C_16:
686 if (dynobj == NULL)
687 elf_hash_table (info)->dynobj = dynobj = abfd;
688 if (splt == NULL)
689 {
690 splt = bfd_get_section_by_name (dynobj, ".plt");
691 if (splt == NULL)
692 {
693 splt = bfd_make_section (dynobj, ".plt");
694 if (splt == NULL
695 || ! bfd_set_section_flags (dynobj, splt,
696 (SEC_ALLOC
697 | SEC_LOAD
698 | SEC_HAS_CONTENTS
699 | SEC_IN_MEMORY
700 | SEC_LINKER_CREATED
701 | SEC_READONLY
702 | SEC_CODE))
703 || ! bfd_set_section_alignment (dynobj, splt, 1))
704 return FALSE;
705 }
706 }
707
708 if (h != NULL)
709 offset = &h->plt.offset;
710 else
711 {
712 if (local_plt_offsets == NULL)
713 {
714 size_t size;
715 unsigned int i;
716
717 size = symtab_hdr->sh_info * sizeof (bfd_vma);
718 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
719 if (local_plt_offsets == NULL)
720 return FALSE;
721 elf_local_got_offsets (abfd) = local_plt_offsets;
722
723 for (i = 0; i < symtab_hdr->sh_info; i++)
724 local_plt_offsets[i] = (bfd_vma) -1;
725 }
726 offset = &local_plt_offsets[r_symndx];
727 }
728
729 if (*offset == (bfd_vma) -1)
730 {
731 *offset = splt->size;
732 splt->size += 4;
733 }
734 break;
735 }
736 }
737
738 return TRUE;
739}
740
741/* This must exist if dynobj is ever set. */
742
743static bfd_boolean
744m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
745 struct bfd_link_info *info)
746{
747 bfd *dynobj;
748 asection *splt;
749
750 /* As an extra sanity check, verify that all plt entries have
751 been filled in. */
752
753 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
754 && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL)
755 {
756 bfd_byte *contents = splt->contents;
757 unsigned int i, size = splt->size;
758 for (i = 0; i < size; i += 4)
759 {
760 unsigned int x = bfd_get_32 (dynobj, contents + i);
761 BFD_ASSERT (x != 0);
762 }
763 }
764
765 return TRUE;
766}
767
768static bfd_boolean
769m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
770 struct bfd_link_info *info)
771{
772 bfd *dynobj;
773 asection *splt;
774
775 if (info->relocatable)
776 return TRUE;
777
778 dynobj = elf_hash_table (info)->dynobj;
779 if (dynobj == NULL)
780 return TRUE;
781
782 splt = bfd_get_section_by_name (dynobj, ".plt");
783 BFD_ASSERT (splt != NULL);
784
785 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
786 if (splt->contents == NULL)
787 return FALSE;
788
789 return TRUE;
790}
791\f
792/* Function to set the ELF flag bits. */
793
794static bfd_boolean
795m32c_elf_set_private_flags (bfd *abfd, flagword flags)
796{
797 elf_elfheader (abfd)->e_flags = flags;
798 elf_flags_init (abfd) = TRUE;
799 return TRUE;
800}
801
802/* Merge backend specific data from an object file to the output
803 object file when linking. */
804
805static bfd_boolean
806m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
807{
808 flagword old_flags, old_partial;
809 flagword new_flags, new_partial;
810 bfd_boolean error = FALSE;
811 char new_opt[80];
812 char old_opt[80];
813
814 new_opt[0] = old_opt[0] = '\0';
815 new_flags = elf_elfheader (ibfd)->e_flags;
816 old_flags = elf_elfheader (obfd)->e_flags;
817
818#ifdef DEBUG
819 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
820 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
821 bfd_get_filename (ibfd));
822#endif
823
824 if (!elf_flags_init (obfd))
825 {
826 /* First call, no flags set. */
827 elf_flags_init (obfd) = TRUE;
828 elf_elfheader (obfd)->e_flags = new_flags;
829 }
830
831 else if (new_flags == old_flags)
832 /* Compatible flags are ok. */
833 ;
834
835 else /* Possibly incompatible flags. */
836 {
837 /* Warn if different cpu is used (allow a specific cpu to override
838 the generic cpu). */
839 new_partial = (new_flags & EF_M32C_CPU_MASK);
840 old_partial = (old_flags & EF_M32C_CPU_MASK);
841 if (new_partial == old_partial)
842 ;
843
844 else
845 {
846 switch (new_partial)
847 {
848 default: strcat (new_opt, " -m16c"); break;
849 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
850 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
851 }
852
853 switch (old_partial)
854 {
855 default: strcat (old_opt, " -m16c"); break;
856 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
857 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
858 }
859 }
860
861 /* Print out any mismatches from above. */
862 if (new_opt[0])
863 {
864 error = TRUE;
865 (*_bfd_error_handler)
866 (_("%s: compiled with %s and linked with modules compiled with %s"),
867 bfd_get_filename (ibfd), new_opt, old_opt);
868 }
869
870 new_flags &= ~ EF_M32C_ALL_FLAGS;
871 old_flags &= ~ EF_M32C_ALL_FLAGS;
872
873 /* Warn about any other mismatches. */
874 if (new_flags != old_flags)
875 {
876 error = TRUE;
877 (*_bfd_error_handler)
878 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
879 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
880 }
881 }
882
883 if (error)
884 bfd_set_error (bfd_error_bad_value);
885
886 return !error;
887}
888
889\f
890static bfd_boolean
891m32c_elf_print_private_bfd_data (bfd *abfd, PTR ptr)
892{
893 FILE *file = (FILE *) ptr;
894 flagword flags;
895
896 BFD_ASSERT (abfd != NULL && ptr != NULL);
897
898 /* Print normal ELF private data. */
899 _bfd_elf_print_private_bfd_data (abfd, ptr);
900
901 flags = elf_elfheader (abfd)->e_flags;
902 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
903
904 switch (flags & EF_M32C_CPU_MASK)
905 {
906 default: break;
907 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
908 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
909 }
910
911 fputc ('\n', file);
912 return TRUE;
913}
914
915/* Return the MACH for an e_flags value. */
916
917static int
918elf32_m32c_machine (bfd *abfd)
919{
920 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
921 {
922 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
923 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
924 }
925
926 return bfd_mach_m16c;
927}
928
929static bfd_boolean
930m32c_elf_object_p (bfd *abfd)
931{
932 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
933 elf32_m32c_machine (abfd));
934 return TRUE;
935}
936 \f
937
938#ifdef DEBUG
939static void
940dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
941{
942 size_t locsymcount;
943 Elf_Internal_Sym *isymbuf;
944 Elf_Internal_Sym *isymend;
945 Elf_Internal_Sym *isym;
946 Elf_Internal_Shdr *symtab_hdr;
947 bfd_boolean free_internal = 0, free_external = 0;
948 char * st_info_str;
949 char * st_info_stb_str;
950 char * st_other_str;
951 char * st_shndx_str;
952
953 if (! internal_syms)
954 {
955 internal_syms = bfd_malloc (1000);
956 free_internal = 1;
957 }
958 if (! external_syms)
959 {
960 external_syms = bfd_malloc (1000);
961 free_external = 1;
962 }
963
964 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
965 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
966 if (free_internal)
967 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
968 symtab_hdr->sh_info, 0,
969 internal_syms, external_syms, NULL);
970 else
971 isymbuf = internal_syms;
972 isymend = isymbuf + locsymcount;
973
974 for (isym = isymbuf ; isym < isymend ; isym++)
975 {
976 switch (ELF_ST_TYPE (isym->st_info))
977 {
978 case STT_FUNC: st_info_str = "STT_FUNC";
979 case STT_SECTION: st_info_str = "STT_SECTION";
980 case STT_SRELC: st_info_str = "STT_SRELC";
981 case STT_FILE: st_info_str = "STT_FILE";
982 case STT_OBJECT: st_info_str = "STT_OBJECT";
983 case STT_TLS: st_info_str = "STT_TLS";
984 default: st_info_str = "";
985 }
986 switch (ELF_ST_BIND (isym->st_info))
987 {
988 case STB_LOCAL: st_info_stb_str = "STB_LOCAL";
989 case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL";
990 default: st_info_stb_str = "";
991 }
992 switch (ELF_ST_VISIBILITY (isym->st_other))
993 {
994 case STV_DEFAULT: st_other_str = "STV_DEFAULT";
995 case STV_INTERNAL: st_other_str = "STV_INTERNAL";
996 case STV_PROTECTED: st_other_str = "STV_PROTECTED";
997 default: st_other_str = "";
998 }
999 switch (isym->st_shndx)
1000 {
1001 case SHN_ABS: st_shndx_str = "SHN_ABS";
1002 case SHN_COMMON: st_shndx_str = "SHN_COMMON";
1003 case SHN_UNDEF: st_shndx_str = "SHN_UNDEF";
1004 default: st_shndx_str = "";
1005 }
1006
1007 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
1008 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1009 isym,
1010 (unsigned long) isym->st_value,
1011 (unsigned long) isym->st_size,
1012 isym->st_name,
1013 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
1014 isym->st_name),
1015 isym->st_info, st_info_str, st_info_stb_str,
1016 isym->st_other, st_other_str,
1017 isym->st_shndx, st_shndx_str);
1018 }
1019 if (free_internal)
1020 free (internal_syms);
1021 if (free_external)
1022 free (external_syms);
1023}
1024
1025static char *
1026m32c_get_reloc (long reloc)
1027{
1028 if (0 <= reloc && reloc < R_M32C_max)
1029 return m32c_elf_howto_table[reloc].name;
1030 else
1031 return "";
1032}
1033#endif /* DEBUG */
1034
1035/* Handle relaxing. */
1036
1037/* A subroutine of m32c_elf_relax_section. If the global symbol H
1038 is within the low 64k, remove any entry for it in the plt. */
1039
1040struct relax_plt_data
1041{
1042 asection *splt;
1043 bfd_boolean *again;
1044};
1045
1046static bfd_boolean
1047m32c_relax_plt_check (struct elf_link_hash_entry *h,
1048 PTR xdata)
1049{
1050 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1051
1052 if (h->root.type == bfd_link_hash_warning)
1053 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1054
1055 if (h->plt.offset != (bfd_vma) -1)
1056 {
1057 bfd_vma address;
1058
1059 if (h->root.type == bfd_link_hash_undefined
1060 || h->root.type == bfd_link_hash_undefweak)
1061 address = 0;
1062 else
1063 address = (h->root.u.def.section->output_section->vma
1064 + h->root.u.def.section->output_offset
1065 + h->root.u.def.value);
1066
1067 if (address <= 0xffff)
1068 {
1069 h->plt.offset = -1;
1070 data->splt->size -= 4;
1071 *data->again = TRUE;
1072 }
1073 }
1074
1075 return TRUE;
1076}
1077
1078/* A subroutine of m32c_elf_relax_section. If the global symbol H
1079 previously had a plt entry, give it a new entry offset. */
1080
1081static bfd_boolean
1082m32c_relax_plt_realloc (struct elf_link_hash_entry *h,
1083 PTR xdata)
1084{
1085 bfd_vma *entry = (bfd_vma *) xdata;
1086
1087 if (h->root.type == bfd_link_hash_warning)
1088 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1089
1090 if (h->plt.offset != (bfd_vma) -1)
1091 {
1092 h->plt.offset = *entry;
1093 *entry += 4;
1094 }
1095
1096 return TRUE;
1097}
1098
1099static bfd_boolean
1100m32c_elf_relax_plt_section (bfd *dynobj,
1101 asection *splt,
1102 struct bfd_link_info *info,
1103 bfd_boolean *again)
1104{
1105 struct relax_plt_data relax_plt_data;
1106 bfd *ibfd;
1107
1108 /* Assume nothing changes. */
1109 *again = FALSE;
1110
1111 if (info->relocatable)
1112 return TRUE;
1113
1114 /* We only relax the .plt section at the moment. */
1115 if (dynobj != elf_hash_table (info)->dynobj
1116 || strcmp (splt->name, ".plt") != 0)
1117 return TRUE;
1118
1119 /* Quick check for an empty plt. */
1120 if (splt->size == 0)
1121 return TRUE;
1122
1123 /* Map across all global symbols; see which ones happen to
1124 fall in the low 64k. */
1125 relax_plt_data.splt = splt;
1126 relax_plt_data.again = again;
1127 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1128 &relax_plt_data);
1129
1130 /* Likewise for local symbols, though that's somewhat less convenient
1131 as we have to walk the list of input bfds and swap in symbol data. */
1132 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1133 {
1134 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1135 Elf_Internal_Shdr *symtab_hdr;
1136 Elf_Internal_Sym *isymbuf = NULL;
1137 unsigned int idx;
1138
1139 if (! local_plt_offsets)
1140 continue;
1141
1142 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1143 if (symtab_hdr->sh_info != 0)
1144 {
1145 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1146 if (isymbuf == NULL)
1147 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1148 symtab_hdr->sh_info, 0,
1149 NULL, NULL, NULL);
1150 if (isymbuf == NULL)
1151 return FALSE;
1152 }
1153
1154 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1155 {
1156 Elf_Internal_Sym *isym;
1157 asection *tsec;
1158 bfd_vma address;
1159
1160 if (local_plt_offsets[idx] == (bfd_vma) -1)
1161 continue;
1162
1163 isym = &isymbuf[idx];
1164 if (isym->st_shndx == SHN_UNDEF)
1165 continue;
1166 else if (isym->st_shndx == SHN_ABS)
1167 tsec = bfd_abs_section_ptr;
1168 else if (isym->st_shndx == SHN_COMMON)
1169 tsec = bfd_com_section_ptr;
1170 else
1171 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1172
1173 address = (tsec->output_section->vma
1174 + tsec->output_offset
1175 + isym->st_value);
1176 if (address <= 0xffff)
1177 {
1178 local_plt_offsets[idx] = -1;
1179 splt->size -= 4;
1180 *again = TRUE;
1181 }
1182 }
1183
1184 if (isymbuf != NULL
1185 && symtab_hdr->contents != (unsigned char *) isymbuf)
1186 {
1187 if (! info->keep_memory)
1188 free (isymbuf);
1189 else
1190 {
1191 /* Cache the symbols for elf_link_input_bfd. */
1192 symtab_hdr->contents = (unsigned char *) isymbuf;
1193 }
1194 }
1195 }
1196
1197 /* If we changed anything, walk the symbols again to reallocate
1198 .plt entry addresses. */
1199 if (*again && splt->size > 0)
1200 {
1201 bfd_vma entry = 0;
1202
1203 elf_link_hash_traverse (elf_hash_table (info),
1204 m32c_relax_plt_realloc, &entry);
1205
1206 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1207 {
1208 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1209 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1210 unsigned int idx;
1211
1212 if (! local_plt_offsets)
1213 continue;
1214
1215 for (idx = 0; idx < nlocals; ++idx)
1216 if (local_plt_offsets[idx] != (bfd_vma) -1)
1217 {
1218 local_plt_offsets[idx] = entry;
1219 entry += 4;
1220 }
1221 }
1222 }
1223
1224 return TRUE;
1225}
1226
6772dd07
DD
1227static int
1228compare_reloc (const void *e1, const void *e2)
49f58d10 1229{
6772dd07
DD
1230 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1231 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1232
1233 if (i1->r_offset == i2->r_offset)
1234 return 0;
1235 else
1236 return i1->r_offset < i2->r_offset ? -1 : 1;
1237}
1238
a1013894 1239#define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
6772dd07
DD
1240static bfd_vma
1241m32c_offset_for_reloc (bfd *abfd,
6772dd07
DD
1242 Elf_Internal_Rela *rel,
1243 Elf_Internal_Shdr *symtab_hdr,
1244 Elf_External_Sym_Shndx *shndx_buf,
1245 Elf_Internal_Sym *intsyms)
1246{
1247 bfd_vma symval;
1248
1249 /* Get the value of the symbol referred to by the reloc. */
1250 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
49f58d10 1251 {
6772dd07
DD
1252 /* A local symbol. */
1253 Elf_Internal_Sym *isym;
1254 Elf_External_Sym_Shndx *shndx;
a1013894
DD
1255 asection *ssec;
1256
6772dd07
DD
1257
1258 isym = intsyms + ELF32_R_SYM (rel->r_info);
a1013894 1259 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6772dd07
DD
1260 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (rel->r_info) : 0);
1261
a1013894
DD
1262 symval = isym->st_value;
1263 if (ssec)
1264 symval += ssec->output_section->vma
1265 + ssec->output_offset;
6772dd07
DD
1266 }
1267 else
49f58d10 1268 {
6772dd07
DD
1269 unsigned long indx;
1270 struct elf_link_hash_entry *h;
1271
1272 /* An external symbol. */
1273 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1274 h = elf_sym_hashes (abfd)[indx];
1275 BFD_ASSERT (h != NULL);
1276
1277 if (h->root.type != bfd_link_hash_defined
1278 && h->root.type != bfd_link_hash_defweak)
1279 /* This appears to be a reference to an undefined
1280 symbol. Just ignore it--it will be caught by the
1281 regular reloc processing. */
1282 return 0;
1283
1284 symval = (h->root.u.def.value
1285 + h->root.u.def.section->output_section->vma
1286 + h->root.u.def.section->output_offset);
49f58d10 1287 }
6772dd07
DD
1288 return symval;
1289}
1290
1291static int bytes_saved = 0;
1292
1293static int bytes_to_reloc[] = {
1294 R_M32C_NONE,
1295 R_M32C_8,
1296 R_M32C_16,
1297 R_M32C_24,
1298 R_M32C_32
1299};
1300
1301/* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1302
1303/* Mask for the number of relocs associated with this insn. */
1304#define RLA_RELOCS 0x0000000f
1305/* Number of bytes gas emitted (before gas's relaxing) */
1306#define RLA_NBYTES 0x00000ff0
1307
1308/* If the displacement is within the given range and the new encoding
1309 differs from the old encoding (the index), then the insn can be
1310 relaxed to the new encoding. */
1311typedef struct {
1312 int bytes;
1313 unsigned int max_disp;
1314 unsigned char new_encoding;
1315} EncodingTable;
1316
1317static EncodingTable m16c_addr_encodings[] = {
1318 { 0, 0, 0 }, /* R0 */
1319 { 0, 0, 1 }, /* R1 */
1320 { 0, 0, 2 }, /* R2 */
1321 { 0, 0, 3 }, /* R3 */
1322 { 0, 0, 4 }, /* A0 */
1323 { 0, 0, 5 }, /* A1 */
1324 { 0, 0, 6 }, /* [A0] */
1325 { 0, 0, 7 }, /* [A1] */
1326 { 1, 0, 6 }, /* udsp:8[A0] */
1327 { 1, 0, 7 }, /* udsp:8[A1] */
1328 { 1, 0, 10 }, /* udsp:8[SB] */
1329 { 1, 0, 11 }, /* sdsp:8[FB] */
1330 { 2, 255, 8 }, /* udsp:16[A0] */
1331 { 2, 255, 9 }, /* udsp:16[A1] */
1332 { 2, 255, 10 }, /* udsp:16[SB] */
1333 { 2, 0, 15 }, /* abs:16 */
1334};
1335
1336static EncodingTable m16c_jmpaddr_encodings[] = {
1337 { 0, 0, 0 }, /* R0 */
1338 { 0, 0, 1 }, /* R1 */
1339 { 0, 0, 2 }, /* R2 */
1340 { 0, 0, 3 }, /* R3 */
1341 { 0, 0, 4 }, /* A0 */
1342 { 0, 0, 5 }, /* A1 */
1343 { 0, 0, 6 }, /* [A0] */
1344 { 0, 0, 7 }, /* [A1] */
1345 { 1, 0, 6 }, /* udsp:8[A0] */
1346 { 1, 0, 7 }, /* udsp:8[A1] */
1347 { 1, 0, 10 }, /* udsp:8[SB] */
1348 { 1, 0, 11 }, /* sdsp:8[FB] */
1349 { 3, 255, 8 }, /* udsp:20[A0] */
1350 { 3, 255, 9 }, /* udsp:20[A1] */
1351 { 2, 255, 10 }, /* udsp:16[SB] */
1352 { 2, 0, 15 }, /* abs:16 */
1353};
1354
1355static EncodingTable m32c_addr_encodings[] = {
1356 { 0, 0, 0 }, /* [A0] */
1357 { 0, 0, 1 }, /* [A1] */
1358 { 0, 0, 2 }, /* A0 */
1359 { 0, 0, 3 }, /* A1 */
1360 { 1, 0, 0 }, /* udsp:8[A0] */
1361 { 1, 0, 1 }, /* udsp:8[A1] */
1362 { 1, 0, 6 }, /* udsp:8[SB] */
1363 { 1, 0, 7 }, /* sdsp:8[FB] */
1364 { 2, 255, 4 }, /* udsp:16[A0] */
1365 { 2, 255, 5 }, /* udsp:16[A1] */
1366 { 2, 255, 6 }, /* udsp:16[SB] */
1367 { 2, 127, 7 }, /* sdsp:16[FB] */
1368 { 3, 65535, 8 }, /* udsp:24[A0] */
1369 { 3, 65535, 9 }, /* udsp:24[A1] */
1370 { 3, 65535, 15 }, /* abs24 */
1371 { 2, 0, 15 }, /* abs16 */
1372 { 0, 0, 16 }, /* R2 */
1373 { 0, 0, 17 }, /* R3 */
1374 { 0, 0, 18 }, /* R0 */
1375 { 0, 0, 19 }, /* R1 */
1376 { 0, 0, 20 }, /* */
1377 { 0, 0, 21 }, /* */
1378 { 0, 0, 22 }, /* */
1379 { 0, 0, 23 }, /* */
1380 { 0, 0, 24 }, /* */
1381 { 0, 0, 25 }, /* */
1382 { 0, 0, 26 }, /* */
1383 { 0, 0, 27 }, /* */
1384 { 0, 0, 28 }, /* */
1385 { 0, 0, 29 }, /* */
1386 { 0, 0, 30 }, /* */
1387 { 0, 0, 31 }, /* */
1388};
1389
49f58d10
JB
1390static bfd_boolean
1391m32c_elf_relax_section
1392 (bfd * abfd,
1393 asection * sec,
1394 struct bfd_link_info * link_info,
1395 bfd_boolean * again)
1396{
1397 Elf_Internal_Shdr *symtab_hdr;
1398 Elf_Internal_Shdr *shndx_hdr;
1399 Elf_Internal_Rela *internal_relocs;
1400 Elf_Internal_Rela *free_relocs = NULL;
6772dd07 1401 Elf_Internal_Rela *irel, *irelend, *srel;
49f58d10
JB
1402 bfd_byte * contents = NULL;
1403 bfd_byte * free_contents = NULL;
6772dd07
DD
1404 Elf_Internal_Sym *intsyms = NULL;
1405 Elf_Internal_Sym *free_intsyms = NULL;
49f58d10
JB
1406 Elf_External_Sym_Shndx *shndx_buf = NULL;
1407 int machine;
1408
1409 if (abfd == elf_hash_table (link_info)->dynobj
1410 && strcmp (sec->name, ".plt") == 0)
1411 return m32c_elf_relax_plt_section (abfd, sec, link_info, again);
1412
1413 /* Assume nothing changes. */
1414 *again = FALSE;
1415
1416 machine = elf32_m32c_machine (abfd);
1417
1418 /* We don't have to do anything for a relocatable link, if
1419 this section does not have relocs, or if this is not a
1420 code section. */
1421 if (link_info->relocatable
1422 || (sec->flags & SEC_RELOC) == 0
1423 || sec->reloc_count == 0
1424 || (sec->flags & SEC_CODE) == 0)
1425 return TRUE;
1426
49f58d10
JB
1427 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1428 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1429
6772dd07
DD
1430 /* Get the section contents. */
1431 if (elf_section_data (sec)->this_hdr.contents != NULL)
1432 contents = elf_section_data (sec)->this_hdr.contents;
1433 /* Go get them off disk. */
1434 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1435 goto error_return;
1436
1437 /* Read this BFD's symbols. */
1438 /* Get cached copy if it exists. */
1439 if (symtab_hdr->contents != NULL)
1440 {
1441 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1442 }
1443 else
1444 {
1445 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1446 symtab_hdr->contents = (bfd_byte *) intsyms;
1447 }
1448
1449 if (shndx_hdr->sh_size != 0)
1450 {
1451 bfd_size_type amt;
1452
1453 amt = symtab_hdr->sh_info;
1454 amt *= sizeof (Elf_External_Sym_Shndx);
1455 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1456 if (shndx_buf == NULL)
1457 goto error_return;
1458 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1459 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1460 goto error_return;
1461 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1462 }
1463
49f58d10
JB
1464 /* Get a copy of the native relocations. */
1465 internal_relocs = (_bfd_elf_link_read_relocs
1466 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1467 link_info->keep_memory));
1468 if (internal_relocs == NULL)
1469 goto error_return;
1470 if (! link_info->keep_memory)
1471 free_relocs = internal_relocs;
1472
6772dd07
DD
1473 /* The RL_ relocs must be just before the operand relocs they go
1474 with, so we must sort them to guarantee this. */
1475 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1476 compare_reloc);
1477
49f58d10
JB
1478 /* Walk through them looking for relaxing opportunities. */
1479 irelend = internal_relocs + sec->reloc_count;
1480
1481 for (irel = internal_relocs; irel < irelend; irel++)
1482 {
1483 bfd_vma symval;
6772dd07 1484 unsigned char *insn, *gap, *einsn;
49f58d10 1485 bfd_vma pc;
6772dd07
DD
1486 bfd_signed_vma pcrel;
1487 int relax_relocs;
1488 int gap_size;
1489 int new_type;
1490 int posn;
1491 int enc;
1492 EncodingTable *enctbl;
1493 EncodingTable *e;
1494
1495 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1496 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1497 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1498 continue;
49f58d10 1499
6772dd07 1500 srel = irel;
49f58d10 1501
6772dd07
DD
1502 /* There will always be room for the relaxed insn, since it is smaller
1503 than the one it would replace. */
1504 BFD_ASSERT (irel->r_offset < sec->size);
1505
1506 insn = contents + irel->r_offset;
1507 relax_relocs = irel->r_addend % 16;
1508
1509 /* Ok, we only have three relocs we care about, and they're all
1510 fake. The lower four bits of the addend is always the number
1511 of following relocs (hence the qsort above) that are assigned
1512 to this opcode. The next 8 bits of the addend indicates the
1513 number of bytes in the insn. We use the rest of them
1514 ourselves as flags for the more expensive operations (defines
1515 above). The three relocs are:
1516
1517 RL_JUMP: This marks all direct jump insns. We check the
1518 displacement and replace them with shorter jumps if
1519 they're in range. We also use this to find JMP.S
1520 insns and manually shorten them when we delete bytes.
1521 We have to decode these insns to figure out what to
1522 do.
1523
1524 RL_1ADDR: This is a :G or :Q insn, which has a single
1525 "standard" operand. We have to extract the type
1526 field, see if it's a wide displacement, then figure
1527 out if we can replace it with a narrow displacement.
1528 We don't have to decode these insns.
1529
1530 RL_2ADDR: Similarly, but two "standard" operands. Note that
1531 r_addend may still be 1, as standard operands don't
1532 always have displacements. Gas shouldn't give us one
1533 with zero operands, but since we don't know which one
1534 has the displacement, we check them both anyway.
1535
1536 These all point to the beginning of the insn itself, not the
1537 operands.
1538
1539 Note that we only relax one step at a time, relying on the
1540 linker to call us repeatedly. Thus, there is no code for
1541 JMP.A->JMP.B although that will happen in two steps.
1542 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1543 */
1544
1545 /* Get the value of the symbol referred to by the reloc. Just
1546 in case this is the last reloc in the list, use the RL's
1547 addend to choose between this reloc (no addend) or the next
1548 (yes addend, which means at least one following reloc). */
1549 srel = irel + (relax_relocs ? 1 : 0);
1550 symval = OFFSET_FOR_RELOC (srel);
1551
1552 /* Setting gap_size nonzero is the flag which means "something
1553 shrunk". */
1554 gap_size = 0;
e970df31 1555 gap = NULL;
6772dd07
DD
1556 new_type = ELF32_R_TYPE(srel->r_info);
1557
1558 pc = sec->output_section->vma + sec->output_offset
1559 + srel->r_offset;
1560 pcrel = symval - pc + srel->r_addend;
1561
1562 if (machine == bfd_mach_m16c)
49f58d10 1563 {
6772dd07 1564 /* R8C / M16C */
49f58d10 1565
6772dd07 1566 switch (ELF32_R_TYPE(irel->r_info))
49f58d10 1567 {
49f58d10 1568
6772dd07
DD
1569 case R_M32C_RL_JUMP:
1570 switch (insn[0])
1571 {
1572 case 0xfe: /* jmp.b */
1573 if (pcrel >= 2 && pcrel <= 9)
1574 {
1575 /* Relax JMP.B -> JMP.S. We need to get rid of
1576 the following reloc though. */
1577 insn[0] = 0x60 | (pcrel - 2);
1578 new_type = R_M32C_NONE;
1579 irel->r_addend = 0x10;
1580 gap_size = 1;
1581 gap = insn + 1;
1582 }
1583 break;
1584
1585 case 0xf4: /* jmp.w */
1586 /* 128 is allowed because it will be one byte closer
1587 after relaxing. Likewise for all other pc-rel
1588 jumps. */
1589 if (pcrel <= 128 && pcrel >= -128)
1590 {
1591 /* Relax JMP.W -> JMP.B */
1592 insn[0] = 0xfe;
1593 insn[1] = 0;
1594 new_type = R_M32C_8_PCREL;
1595 gap_size = 1;
1596 gap = insn + 2;
1597 }
1598 break;
1599
1600 case 0xfc: /* jmp.a */
1601 if (pcrel <= 32768 && pcrel >= -32768)
1602 {
1603 /* Relax JMP.A -> JMP.W */
1604 insn[0] = 0xf4;
1605 insn[1] = 0;
1606 insn[2] = 0;
1607 new_type = R_M32C_16_PCREL;
1608 gap_size = 1;
1609 gap = insn + 3;
1610 }
1611 break;
1612
1613 case 0xfd: /* jsr.a */
1614 if (pcrel <= 32768 && pcrel >= -32768)
1615 {
1616 /* Relax JSR.A -> JSR.W */
1617 insn[0] = 0xf5;
1618 insn[1] = 0;
1619 insn[2] = 0;
1620 new_type = R_M32C_16_PCREL;
1621 gap_size = 1;
1622 gap = insn + 3;
1623 }
1624 break;
1625 }
1626 break;
49f58d10 1627
6772dd07
DD
1628 case R_M32C_RL_2ADDR:
1629 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
49f58d10 1630
6772dd07
DD
1631 enctbl = m16c_addr_encodings;
1632 posn = 2;
1633 enc = (insn[1] >> 4) & 0x0f;
1634 e = & enctbl[enc];
1635
1636 if (srel->r_offset == irel->r_offset + posn
1637 && e->new_encoding != enc
1638 && symval <= e->max_disp)
1639 {
1640 insn[1] &= 0x0f;
1641 insn[1] |= e->new_encoding << 4;
1642 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1643 gap = insn + posn + enctbl[e->new_encoding].bytes;
1644 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1645 break;
1646 }
1647 if (relax_relocs == 2)
1648 srel ++;
1649 posn += e->bytes;
1650
1651 goto try_1addr_16;
49f58d10 1652
6772dd07
DD
1653 case R_M32C_RL_1ADDR:
1654 /* xxxx xxxx xxxx dest [disp] */
49f58d10 1655
6772dd07
DD
1656 enctbl = m16c_addr_encodings;
1657 posn = 2;
1658
1659 /* Check the opcode for jumps. We know it's safe to
1660 do this because all 2ADDR insns are at least two
1661 bytes long. */
1662 enc = insn[0] * 256 + insn[1];
1663 enc &= 0xfff0;
1664 if (enc == 0x7d20
1665 || enc == 0x7d00
1666 || enc == 0x7d30
1667 || enc == 0x7d10)
1668 {
1669 enctbl = m16c_jmpaddr_encodings;
1670 }
1671
1672 try_1addr_16:
1673 /* srel, posn, and enc must be set here. */
1674
1675 symval = OFFSET_FOR_RELOC (srel);
1676 enc = insn[1] & 0x0f;
1677 e = & enctbl[enc];
1678
1679 if (srel->r_offset == irel->r_offset + posn
1680 && e->new_encoding != enc
1681 && symval <= e->max_disp)
1682 {
1683 insn[1] &= 0xf0;
1684 insn[1] |= e->new_encoding;
1685 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1686 gap = insn + posn + enctbl[e->new_encoding].bytes;
1687 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1688 break;
1689 }
1690
1691 break;
1692
1693 } /* Ends switch (reloc type) for m16c. */
1694 }
1695 else /* machine == bfd_mach_m32c */
49f58d10 1696 {
6772dd07 1697 /* M32CM / M32C */
49f58d10 1698
6772dd07 1699 switch (ELF32_R_TYPE(irel->r_info))
49f58d10 1700 {
49f58d10 1701
6772dd07
DD
1702 case R_M32C_RL_JUMP:
1703 switch (insn[0])
1704 {
1705 case 0xbb: /* jmp.b */
1706 if (pcrel >= 2 && pcrel <= 9)
1707 {
1708 int p = pcrel - 2;
1709 /* Relax JMP.B -> JMP.S. We need to get rid of
1710 the following reloc though. */
1711 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1712 new_type = R_M32C_NONE;
1713 irel->r_addend = 0x10;
1714 gap_size = 1;
1715 gap = insn + 1;
1716 }
1717 break;
1718
1719 case 0xce: /* jmp.w */
1720 if (pcrel <= 128 && pcrel >= -128)
1721 {
1722 /* Relax JMP.W -> JMP.B */
1723 insn[0] = 0xbb;
1724 insn[1] = 0;
1725 new_type = R_M32C_8_PCREL;
1726 gap_size = 1;
1727 gap = insn + 2;
1728 }
1729 break;
1730
1731 case 0xcc: /* jmp.a */
1732 if (pcrel <= 32768 && pcrel >= -32768)
1733 {
1734 /* Relax JMP.A -> JMP.W */
1735 insn[0] = 0xce;
1736 insn[1] = 0;
1737 insn[2] = 0;
1738 new_type = R_M32C_16_PCREL;
1739 gap_size = 1;
1740 gap = insn + 3;
1741 }
1742 break;
1743
1744 case 0xcd: /* jsr.a */
1745 if (pcrel <= 32768 && pcrel >= -32768)
1746 {
1747 /* Relax JSR.A -> JSR.W */
1748 insn[0] = 0xcf;
1749 insn[1] = 0;
1750 insn[2] = 0;
1751 new_type = R_M32C_16_PCREL;
1752 gap_size = 1;
1753 gap = insn + 3;
1754 }
1755 break;
1756 }
1757 break;
49f58d10 1758
6772dd07
DD
1759 case R_M32C_RL_2ADDR:
1760 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
49f58d10 1761
6772dd07
DD
1762 einsn = insn;
1763 posn = 2;
1764 if (einsn[0] == 1)
1765 {
1766 /* prefix; remove it as far as the RL reloc is concerned. */
1767 einsn ++;
1768 posn ++;
1769 }
49f58d10 1770
6772dd07
DD
1771 enctbl = m32c_addr_encodings;
1772 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1773 e = & enctbl[enc];
49f58d10 1774
6772dd07
DD
1775 if (srel->r_offset == irel->r_offset + posn
1776 && e->new_encoding != enc
1777 && symval <= e->max_disp)
1778 {
1779 einsn[0] &= 0x8f;
1780 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1781 einsn[1] &= 0xcf;
1782 einsn[1] |= (e->new_encoding & 0x03) << 4;
1783 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1784 gap = insn + posn + enctbl[e->new_encoding].bytes;
1785 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1786 break;
1787 }
1788 if (relax_relocs == 2)
1789 srel ++;
1790 posn += e->bytes;
1791
1792 goto try_1addr_32;
1793
1794 case R_M32C_RL_1ADDR:
1795 /* xxxx DDDx DDxx xxxx [disp] */
1796
1797 einsn = insn;
1798 posn = 2;
1799 if (einsn[0] == 1)
1800 {
1801 /* prefix; remove it as far as the RL reloc is concerned. */
1802 einsn ++;
1803 posn ++;
1804 }
1805
1806 enctbl = m32c_addr_encodings;
49f58d10 1807
6772dd07
DD
1808 try_1addr_32:
1809 /* srel, posn, and enc must be set here. */
49f58d10 1810
6772dd07
DD
1811 symval = OFFSET_FOR_RELOC (srel);
1812 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1813 e = & enctbl[enc];
49f58d10 1814
6772dd07
DD
1815 if (srel->r_offset == irel->r_offset + posn
1816 && e->new_encoding != enc
1817 && symval <= e->max_disp)
1818 {
1819 einsn[0] &= 0xf1;
1820 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1821 einsn[1] &= 0x3f;
1822 einsn[1] |= (e->new_encoding & 0x03) << 6;
1823 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1824 gap = insn + posn + enctbl[e->new_encoding].bytes;
1825 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1826 break;
1827 }
1828
1829 break;
1830
1831 } /* Ends switch (reloc type) for m32c. */
1832 }
1833
1834 if (gap_size == 0)
1835 continue;
1836
1837 *again = TRUE;
1838
1839 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1840
1841 /* Note that we've changed the relocs, section contents, etc. */
1842 elf_section_data (sec)->relocs = internal_relocs;
1843 free_relocs = NULL;
1844
1845 elf_section_data (sec)->this_hdr.contents = contents;
1846 free_contents = NULL;
1847
1848 symtab_hdr->contents = (bfd_byte *) intsyms;
1849 free_intsyms = NULL;
1850
1851 bytes_saved += gap_size;
1852
1853 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1854 goto error_return;
49f58d10 1855
49f58d10
JB
1856 } /* next relocation */
1857
1858 if (free_relocs != NULL)
1859 {
1860 free (free_relocs);
1861 free_relocs = NULL;
1862 }
1863
1864 if (free_contents != NULL)
1865 {
1866 if (! link_info->keep_memory)
1867 free (free_contents);
1868 /* Cache the section contents for elf_link_input_bfd. */
1869 else
1870 elf_section_data (sec)->this_hdr.contents = contents;
1871
1872 free_contents = NULL;
1873 }
1874
1875 if (shndx_buf != NULL)
1876 {
1877 shndx_hdr->contents = NULL;
1878 free (shndx_buf);
1879 }
1880
6772dd07 1881 if (free_intsyms != NULL)
49f58d10
JB
1882 {
1883 if (! link_info->keep_memory)
6772dd07 1884 free (free_intsyms);
49f58d10
JB
1885 /* Cache the symbols for elf_link_input_bfd. */
1886 else
6772dd07
DD
1887 {
1888 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1889 }
49f58d10 1890
6772dd07 1891 free_intsyms = NULL;
49f58d10 1892 }
49f58d10
JB
1893
1894 return TRUE;
1895
1896 error_return:
1897 if (free_relocs != NULL)
1898 free (free_relocs);
1899 if (free_contents != NULL)
1900 free (free_contents);
1901 if (shndx_buf != NULL)
1902 {
1903 shndx_hdr->contents = NULL;
1904 free (shndx_buf);
1905 }
6772dd07
DD
1906 if (free_intsyms != NULL)
1907 free (free_intsyms);
49f58d10
JB
1908 return FALSE;
1909}
1910
1911/* Delete some bytes from a section while relaxing. */
1912
1913static bfd_boolean
1914m32c_elf_relax_delete_bytes
1915 (bfd * abfd,
1916 asection * sec,
1917 bfd_vma addr,
1918 int count)
1919{
1920 Elf_Internal_Shdr *symtab_hdr;
1921 Elf_Internal_Shdr *shndx_hdr;
1922 int sec_shndx;
1923 bfd_byte *contents;
1924 Elf_Internal_Rela *irel;
1925 Elf_Internal_Rela *irelend;
1926 Elf_Internal_Rela *irelalign;
1927 bfd_vma toaddr;
6772dd07
DD
1928 Elf_Internal_Sym *isym;
1929 Elf_Internal_Sym *isymend;
1930 Elf_Internal_Sym *intsyms;
49f58d10
JB
1931 Elf_External_Sym_Shndx *shndx_buf;
1932 Elf_External_Sym_Shndx *shndx;
1933 struct elf_link_hash_entry ** sym_hashes;
1934 struct elf_link_hash_entry ** end_hashes;
1935 unsigned int symcount;
6772dd07 1936
49f58d10
JB
1937 contents = elf_section_data (sec)->this_hdr.contents;
1938
1939 /* The deletion must stop at the next ALIGN reloc for an aligment
1940 power larger than the number of bytes we are deleting. */
1941 irelalign = NULL;
1942 toaddr = sec->size;
1943
1944 irel = elf_section_data (sec)->relocs;
1945 irelend = irel + sec->reloc_count;
1946
1947 /* Actually delete the bytes. */
1948 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1949 sec->size -= count;
1950
1951 /* Adjust all the relocs. */
1952 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1953 {
1954 /* Get the new reloc address. */
1955 if (irel->r_offset > addr && irel->r_offset < toaddr)
1956 irel->r_offset -= count;
6772dd07
DD
1957
1958 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1959 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1960 && irel->r_offset + 1 < addr
1961 && irel->r_offset + 7 > addr)
1962 {
1963 bfd_vma disp;
1964 unsigned char *insn = &contents[irel->r_offset];
1965 disp = *insn;
1966 /* This is a JMP.S, which we have to manually update. */
1967 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1968 {
1969 if ((*insn & 0xf8) != 0x60)
1970 continue;
1971 disp = (disp & 7);
1972 }
1973 else
1974 {
1975 if ((*insn & 0xce) != 0x4a)
1976 continue;
1977 disp = ((disp & 0x30) >> 3) | (disp & 1);
1978 }
1979 if (irel->r_offset + disp + 2 >= addr+count)
1980 {
1981 disp -= count;
1982 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1983 {
1984 *insn = (*insn & 0xf8) | disp;
1985 }
1986 else
1987 {
1988 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
1989 }
1990 }
1991 }
49f58d10
JB
1992 }
1993
1994 /* Adjust the local symbols defined in this section. */
6772dd07
DD
1995 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1996 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1997 isym = intsyms;
1998 isymend = isym + symtab_hdr->sh_info;
1999
2000 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2001 shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr;
2002 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
49f58d10 2003 shndx = shndx_buf;
49f58d10 2004
6772dd07
DD
2005 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
2006 {
49f58d10 2007
6772dd07
DD
2008 if ((int) isym->st_shndx == sec_shndx
2009 && isym->st_value > addr
2010 && isym->st_value < toaddr)
49f58d10 2011 {
6772dd07 2012 isym->st_value -= count;
49f58d10
JB
2013 }
2014 }
2015
2016 /* Now adjust the global symbols defined in this section. */
2017 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2018 - symtab_hdr->sh_info);
2019 sym_hashes = elf_sym_hashes (abfd);
2020 // sym_hashes += symtab_hdr->sh_info;
2021 end_hashes = sym_hashes + symcount;
2022
2023 for (; sym_hashes < end_hashes; sym_hashes ++)
2024 {
2025 struct elf_link_hash_entry * sym_hash = * sym_hashes;
2026
2027 if (sym_hash &&
2028 ( sym_hash->root.type == bfd_link_hash_defined
2029 || sym_hash->root.type == bfd_link_hash_defweak)
2030 && sym_hash->root.u.def.section == sec
2031 && sym_hash->root.u.def.value > addr
2032 && sym_hash->root.u.def.value < toaddr)
6772dd07
DD
2033 {
2034 sym_hash->root.u.def.value -= count;
2035 }
49f58d10
JB
2036 }
2037
2038 return TRUE;
2039}
2040\f
2041
2042#define ELF_ARCH bfd_arch_m32c
2043#define ELF_MACHINE_CODE EM_M32C
2044#define ELF_MAXPAGESIZE 0x1000
2045
2046#if 0
2047#define TARGET_BIG_SYM bfd_elf32_m32c_vec
2048#define TARGET_BIG_NAME "elf32-m32c"
2049#else
2050#define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
2051#define TARGET_LITTLE_NAME "elf32-m32c"
2052#endif
2053
2054#define elf_info_to_howto_rel NULL
2055#define elf_info_to_howto m32c_info_to_howto_rela
2056#define elf_backend_object_p m32c_elf_object_p
2057#define elf_backend_relocate_section m32c_elf_relocate_section
2058#define elf_backend_gc_mark_hook m32c_elf_gc_mark_hook
2059#define elf_backend_gc_sweep_hook m32c_elf_gc_sweep_hook
2060#define elf_backend_check_relocs m32c_elf_check_relocs
2061#define elf_backend_object_p m32c_elf_object_p
2062#define elf_symbol_leading_char ('_')
2063#define elf_backend_always_size_sections \
2064 m32c_elf_always_size_sections
2065#define elf_backend_finish_dynamic_sections \
2066 m32c_elf_finish_dynamic_sections
2067
2068#define elf_backend_can_gc_sections 1
2069
2070#define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2071#define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2072#define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2073#define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2074#define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2075
2076#include "elf32-target.h"
This page took 0.128815 seconds and 4 git commands to generate.