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