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