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