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