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