* elf32-mn10200.c (mn10200_elf_relax_section): New function.
[deliverable/binutils-gdb.git] / bfd / elf32-mn10200.c
1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997 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 2 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 "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24
25 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
26 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
27 static void mn10200_info_to_howto
28 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
29 static boolean mn10200_elf_relax_delete_bytes
30 PARAMS ((bfd *, asection *, bfd_vma, int));
31 static boolean mn10200_elf_symbol_address_p
32 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
33
34 /* We have to use RELA instructions since md_apply_fix3 in the assembler
35 does absolutely nothing. */
36 #define USE_RELA
37
38 enum reloc_type
39 {
40 R_MN10200_NONE = 0,
41 R_MN10200_32,
42 R_MN10200_16,
43 R_MN10200_8,
44 R_MN10200_24,
45 R_MN10200_PCREL8,
46 R_MN10200_PCREL16,
47 R_MN10200_PCREL24,
48 R_MN10200_MAX
49 };
50
51 static reloc_howto_type elf_mn10200_howto_table[] =
52 {
53 /* Dummy relocation. Does nothing. */
54 HOWTO (R_MN10200_NONE,
55 0,
56 2,
57 16,
58 false,
59 0,
60 complain_overflow_bitfield,
61 bfd_elf_generic_reloc,
62 "R_MN10200_NONE",
63 false,
64 0,
65 0,
66 false),
67 /* Standard 32 bit reloc. */
68 HOWTO (R_MN10200_32,
69 0,
70 2,
71 32,
72 false,
73 0,
74 complain_overflow_bitfield,
75 bfd_elf_generic_reloc,
76 "R_MN10200_32",
77 false,
78 0xffffffff,
79 0xffffffff,
80 false),
81 /* Standard 16 bit reloc. */
82 HOWTO (R_MN10200_16,
83 0,
84 1,
85 16,
86 false,
87 0,
88 complain_overflow_bitfield,
89 bfd_elf_generic_reloc,
90 "R_MN10200_16",
91 false,
92 0xffff,
93 0xffff,
94 false),
95 /* Standard 8 bit reloc. */
96 HOWTO (R_MN10200_8,
97 0,
98 0,
99 8,
100 false,
101 0,
102 complain_overflow_bitfield,
103 bfd_elf_generic_reloc,
104 "R_MN10200_8",
105 false,
106 0xff,
107 0xff,
108 false),
109 /* Standard 24 bit reloc. */
110 HOWTO (R_MN10200_24,
111 0,
112 2,
113 24,
114 false,
115 0,
116 complain_overflow_bitfield,
117 bfd_elf_generic_reloc,
118 "R_MN10200_24",
119 false,
120 0xffffff,
121 0xffffff,
122 false),
123 /* Simple 8 pc-relative reloc. */
124 HOWTO (R_MN10200_PCREL8,
125 0,
126 0,
127 8,
128 true,
129 0,
130 complain_overflow_bitfield,
131 bfd_elf_generic_reloc,
132 "R_MN10200_PCREL8",
133 false,
134 0xff,
135 0xff,
136 true),
137 /* Simple 16 pc-relative reloc. */
138 HOWTO (R_MN10200_PCREL16,
139 0,
140 1,
141 16,
142 true,
143 0,
144 complain_overflow_bitfield,
145 bfd_elf_generic_reloc,
146 "R_MN10200_PCREL16",
147 false,
148 0xffff,
149 0xffff,
150 true),
151 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
152 to get the pc-relative offset correct. */
153 HOWTO (R_MN10200_PCREL24,
154 0,
155 2,
156 24,
157 true,
158 0,
159 complain_overflow_bitfield,
160 bfd_elf_generic_reloc,
161 "R_MN10200_PCREL24",
162 false,
163 0xffffff,
164 0xffffff,
165 true),
166 };
167
168 struct mn10200_reloc_map
169 {
170 unsigned char bfd_reloc_val;
171 unsigned char elf_reloc_val;
172 };
173
174 static const struct mn10200_reloc_map mn10200_reloc_map[] =
175 {
176 { BFD_RELOC_NONE, R_MN10200_NONE, },
177 { BFD_RELOC_32, R_MN10200_32, },
178 { BFD_RELOC_16, R_MN10200_16, },
179 { BFD_RELOC_8, R_MN10200_8, },
180 { BFD_RELOC_24, R_MN10200_24, },
181 { BFD_RELOC_8_PCREL, R_MN10200_PCREL8, },
182 { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
183 { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
184 };
185
186 static reloc_howto_type *
187 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
188 bfd *abfd;
189 bfd_reloc_code_real_type code;
190 {
191 unsigned int i;
192
193 for (i = 0;
194 i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
195 i++)
196 {
197 if (mn10200_reloc_map[i].bfd_reloc_val == code)
198 return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
199 }
200
201 return NULL;
202 }
203
204 /* Set the howto pointer for an MN10200 ELF reloc. */
205
206 static void
207 mn10200_info_to_howto (abfd, cache_ptr, dst)
208 bfd *abfd;
209 arelent *cache_ptr;
210 Elf32_Internal_Rela *dst;
211 {
212 unsigned int r_type;
213
214 r_type = ELF32_R_TYPE (dst->r_info);
215 BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
216 cache_ptr->howto = &elf_mn10200_howto_table[r_type];
217 }
218
219 /* Perform a relocation as part of a final link. */
220 static bfd_reloc_status_type
221 mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
222 input_section, contents, offset, value,
223 addend, info, sym_sec, is_local)
224 reloc_howto_type *howto;
225 bfd *input_bfd;
226 bfd *output_bfd;
227 asection *input_section;
228 bfd_byte *contents;
229 bfd_vma offset;
230 bfd_vma value;
231 bfd_vma addend;
232 struct bfd_link_info *info;
233 asection *sym_sec;
234 int is_local;
235 {
236 unsigned long r_type = howto->type;
237 bfd_byte *hit_data = contents + offset;
238
239 switch (r_type)
240 {
241
242 case R_MN10200_NONE:
243 return bfd_reloc_ok;
244
245 case R_MN10200_32:
246 value += bfd_get_32 (input_bfd, hit_data);
247 value += addend;
248 bfd_put_32 (input_bfd, value, hit_data);
249 return bfd_reloc_ok;
250
251 case R_MN10200_16:
252 value += (short)bfd_get_16 (input_bfd, hit_data);
253 value += addend;
254
255 if ((long)value > 0x7fff || (long)value < -0x8000)
256 return bfd_reloc_overflow;
257
258 bfd_put_16 (input_bfd, value, hit_data);
259 return bfd_reloc_ok;
260
261 case R_MN10200_8:
262 value += (char)bfd_get_8 (input_bfd, hit_data);
263 value += addend;
264
265 if ((long)value > 0x7fff || (long)value < -0x8000)
266 return bfd_reloc_overflow;
267
268 bfd_put_8 (input_bfd, value, hit_data);
269 return bfd_reloc_ok;
270
271 case R_MN10200_24:
272 value += (bfd_get_32 (input_bfd, hit_data) & 0xffffff);
273 value += addend;
274
275 if ((long)value > 0x7fffff || (long)value < -0x800000)
276 return bfd_reloc_overflow;
277
278 value &= 0xffffff;
279 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
280 bfd_put_32 (input_bfd, value, hit_data);
281 return bfd_reloc_ok;
282
283 case R_MN10200_PCREL8:
284 value -= (input_section->output_section->vma
285 + input_section->output_offset);
286 value -= offset;
287 value += addend;
288
289 if ((long)value > 0xff || (long)value < -0x100)
290 return bfd_reloc_overflow;
291
292 bfd_put_8 (input_bfd, value, hit_data);
293 return bfd_reloc_ok;
294
295 case R_MN10200_PCREL16:
296 value -= (input_section->output_section->vma
297 + input_section->output_offset);
298 value -= offset;
299 value += addend;
300
301 if ((long)value > 0xffff || (long)value < -0x10000)
302 return bfd_reloc_overflow;
303
304 bfd_put_16 (input_bfd, value, hit_data);
305 return bfd_reloc_ok;
306
307 case R_MN10200_PCREL24:
308 value -= (input_section->output_section->vma
309 + input_section->output_offset);
310 value -= offset;
311 value += addend;
312
313 if ((long)value > 0xffffff || (long)value < -0x1000000)
314 return bfd_reloc_overflow;
315
316 value &= 0xffffff;
317 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
318 bfd_put_32 (input_bfd, value, hit_data);
319 return bfd_reloc_ok;
320
321 default:
322 return bfd_reloc_notsupported;
323 }
324 }
325
326 \f
327 /* Relocate an MN10200 ELF section. */
328 static boolean
329 mn10200_elf_relocate_section (output_bfd, info, input_bfd, input_section,
330 contents, relocs, local_syms, local_sections)
331 bfd *output_bfd;
332 struct bfd_link_info *info;
333 bfd *input_bfd;
334 asection *input_section;
335 bfd_byte *contents;
336 Elf_Internal_Rela *relocs;
337 Elf_Internal_Sym *local_syms;
338 asection **local_sections;
339 {
340 Elf_Internal_Shdr *symtab_hdr;
341 struct elf_link_hash_entry **sym_hashes;
342 Elf_Internal_Rela *rel, *relend;
343
344 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
345 sym_hashes = elf_sym_hashes (input_bfd);
346
347 rel = relocs;
348 relend = relocs + input_section->reloc_count;
349 for (; rel < relend; rel++)
350 {
351 int r_type;
352 reloc_howto_type *howto;
353 unsigned long r_symndx;
354 Elf_Internal_Sym *sym;
355 asection *sec;
356 struct elf_link_hash_entry *h;
357 bfd_vma relocation;
358 bfd_reloc_status_type r;
359
360 r_symndx = ELF32_R_SYM (rel->r_info);
361 r_type = ELF32_R_TYPE (rel->r_info);
362 howto = elf_mn10200_howto_table + r_type;
363
364 if (info->relocateable)
365 {
366 /* This is a relocateable link. We don't have to change
367 anything, unless the reloc is against a section symbol,
368 in which case we have to adjust according to where the
369 section symbol winds up in the output section. */
370 if (r_symndx < symtab_hdr->sh_info)
371 {
372 sym = local_syms + r_symndx;
373 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
374 {
375 sec = local_sections[r_symndx];
376 rel->r_addend += sec->output_offset + sym->st_value;
377 }
378 }
379
380 continue;
381 }
382
383 /* This is a final link. */
384 h = NULL;
385 sym = NULL;
386 sec = NULL;
387 if (r_symndx < symtab_hdr->sh_info)
388 {
389 sym = local_syms + r_symndx;
390 sec = local_sections[r_symndx];
391 relocation = (sec->output_section->vma
392 + sec->output_offset
393 + sym->st_value);
394 }
395 else
396 {
397 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
398 while (h->root.type == bfd_link_hash_indirect
399 || h->root.type == bfd_link_hash_warning)
400 h = (struct elf_link_hash_entry *) h->root.u.i.link;
401 if (h->root.type == bfd_link_hash_defined
402 || h->root.type == bfd_link_hash_defweak)
403 {
404 sec = h->root.u.def.section;
405 relocation = (h->root.u.def.value
406 + sec->output_section->vma
407 + sec->output_offset);
408 }
409 else if (h->root.type == bfd_link_hash_undefweak)
410 relocation = 0;
411 else
412 {
413 if (! ((*info->callbacks->undefined_symbol)
414 (info, h->root.root.string, input_bfd,
415 input_section, rel->r_offset)))
416 return false;
417 relocation = 0;
418 }
419 }
420
421 r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
422 input_section,
423 contents, rel->r_offset,
424 relocation, rel->r_addend,
425 info, sec, h == NULL);
426
427 if (r != bfd_reloc_ok)
428 {
429 const char *name;
430 const char *msg = (const char *)0;
431
432 if (h != NULL)
433 name = h->root.root.string;
434 else
435 {
436 name = (bfd_elf_string_from_elf_section
437 (input_bfd, symtab_hdr->sh_link, sym->st_name));
438 if (name == NULL || *name == '\0')
439 name = bfd_section_name (input_bfd, sec);
440 }
441
442 switch (r)
443 {
444 case bfd_reloc_overflow:
445 if (! ((*info->callbacks->reloc_overflow)
446 (info, name, howto->name, (bfd_vma) 0,
447 input_bfd, input_section, rel->r_offset)))
448 return false;
449 break;
450
451 case bfd_reloc_undefined:
452 if (! ((*info->callbacks->undefined_symbol)
453 (info, name, input_bfd, input_section,
454 rel->r_offset)))
455 return false;
456 break;
457
458 case bfd_reloc_outofrange:
459 msg = "internal error: out of range error";
460 goto common_error;
461
462 case bfd_reloc_notsupported:
463 msg = "internal error: unsupported relocation error";
464 goto common_error;
465
466 case bfd_reloc_dangerous:
467 msg = "internal error: dangerous error";
468 goto common_error;
469
470 default:
471 msg = "internal error: unknown error";
472 /* fall through */
473
474 common_error:
475 if (!((*info->callbacks->warning)
476 (info, msg, name, input_bfd, input_section,
477 rel->r_offset)))
478 return false;
479 break;
480 }
481 }
482 }
483
484 return true;
485 }
486
487 /* This function handles relaxing for the mn10200.
488
489 There's quite a few relaxing opportunites available on the mn10200:
490
491 * jsr:24 -> jsr:16 2 bytes
492
493 * jmp:24 -> jmp:16 2 bytes
494 - jmp:16 -> bra:8 1 byte
495
496 - If the previous instruction is a conditional branch
497 around the jump/bra, we may be able to reverse its condition
498 and change its target to the jump's target. The jump/bra
499 can then be deleted. 1 byte
500
501 - mov abs24 -> mov abs16 2 byte savings
502
503 - Most instructions which accept imm24 can relax to imm16 2 bytes
504 - Most instructions which accept imm16 can relax to imm8 1 byte
505
506 - Most instructions which accept d24 can relax to d16 2 bytes
507 - Most instructions which accept d16 can relax to d8 1 byte
508
509
510 A '*' indicates a case this code can handle. */
511
512
513 static boolean
514 mn10200_elf_relax_section (abfd, sec, link_info, again)
515 bfd *abfd;
516 asection *sec;
517 struct bfd_link_info *link_info;
518 boolean *again;
519 {
520 Elf_Internal_Shdr *symtab_hdr;
521 Elf_Internal_Rela *internal_relocs;
522 Elf_Internal_Rela *free_relocs = NULL;
523 Elf_Internal_Rela *irel, *irelend;
524 bfd_byte *contents = NULL;
525 bfd_byte *free_contents = NULL;
526 Elf32_External_Sym *extsyms = NULL;
527 Elf32_External_Sym *free_extsyms = NULL;
528
529 /* Assume nothing changes. */
530 *again = false;
531
532 /* We don't have to do anything for a relocateable link, if
533 this section does not have relocs, or if this is not a
534 code section. */
535 if (link_info->relocateable
536 || (sec->flags & SEC_RELOC) == 0
537 || sec->reloc_count == 0
538 || (sec->flags & SEC_CODE) == 0)
539 return true;
540
541 /* If this is the first time we have been called for this section,
542 initialize the cooked size. */
543 if (sec->_cooked_size == 0)
544 sec->_cooked_size = sec->_raw_size;
545
546 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
547
548 /* Get a copy of the native relocations. */
549 internal_relocs = (_bfd_elf32_link_read_relocs
550 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
551 link_info->keep_memory));
552 if (internal_relocs == NULL)
553 goto error_return;
554 if (! link_info->keep_memory)
555 free_relocs = internal_relocs;
556
557 /* Walk through them looking for relaxing opportunities. */
558 irelend = internal_relocs + sec->reloc_count;
559 for (irel = internal_relocs; irel < irelend; irel++)
560 {
561 bfd_vma symval;
562
563 /* If this isn't something that can be relaxed, then ignore
564 this reloc. */
565 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
566 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
567 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
568 continue;
569
570 /* Get the section contents if we haven't done so already. */
571 if (contents == NULL)
572 {
573 /* Get cached copy if it exists. */
574 if (elf_section_data (sec)->this_hdr.contents != NULL)
575 contents = elf_section_data (sec)->this_hdr.contents;
576 else
577 {
578 /* Go get them off disk. */
579 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
580 if (contents == NULL)
581 goto error_return;
582 free_contents = contents;
583
584 if (! bfd_get_section_contents (abfd, sec, contents,
585 (file_ptr) 0, sec->_raw_size))
586 goto error_return;
587 }
588 }
589
590 /* Read the local symbols if we haven't done so already. */
591 if (extsyms == NULL)
592 {
593 /* Get cached copy if it exists. */
594 if (symtab_hdr->contents != NULL)
595 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
596 else
597 {
598 /* Go get them off disk. */
599 extsyms = ((Elf32_External_Sym *)
600 bfd_malloc (symtab_hdr->sh_info
601 * sizeof (Elf32_External_Sym)));
602 if (extsyms == NULL)
603 goto error_return;
604 free_extsyms = extsyms;
605 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
606 || (bfd_read (extsyms, sizeof (Elf32_External_Sym),
607 symtab_hdr->sh_info, abfd)
608 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
609 goto error_return;
610 }
611 }
612
613 /* Get the value of the symbol referred to by the reloc. */
614 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
615 {
616 Elf_Internal_Sym isym;
617
618 /* A local symbol. */
619 bfd_elf32_swap_symbol_in (abfd,
620 extsyms + ELF32_R_SYM (irel->r_info),
621 &isym);
622
623 #if 0
624 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
625 {
626 ((*_bfd_error_handler)
627 ("%s: 0x%lx: warning: symbol in unexpected section",
628 bfd_get_filename (abfd), (unsigned long) 0));
629 continue;
630 }
631 #endif
632
633 symval = (isym.st_value
634 + sec->output_section->vma
635 + sec->output_offset);
636 }
637 else
638 {
639 unsigned long indx;
640 struct elf_link_hash_entry *h;
641
642 /* An external symbol. */
643 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
644 h = elf_sym_hashes (abfd)[indx];
645 BFD_ASSERT (h != NULL);
646 if (h->root.type != bfd_link_hash_defined
647 && h->root.type != bfd_link_hash_defweak)
648 {
649 /* This appears to be a reference to an undefined
650 symbol. Just ignore it--it will be caught by the
651 regular reloc processing. */
652 continue;
653 }
654
655 symval = (h->root.u.def.value
656 + h->root.u.def.section->output_section->vma
657 + h->root.u.def.section->output_offset);
658 }
659
660 /* For simplicity of coding, we are going to modify the section
661 contents, the section relocs, and the BFD symbol table. We
662 must tell the rest of the code not to free up this
663 information. It would be possible to instead create a table
664 of changes which have to be made, as is done in coff-mips.c;
665 that would be more work, but would require less memory when
666 the linker is run. */
667
668
669 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
670 branch/call. */
671 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
672 {
673 bfd_vma value = symval;
674
675 /* Deal with pc-relative gunk. */
676 value -= (sec->output_section->vma + sec->output_offset);
677 value -= irel->r_offset;
678 value += irel->r_addend;
679
680 /* See if the value will fit in 16 bits, note the high value is
681 0x7fff + 2 as the target will be two bytes closer if we are
682 able to relax. */
683 if ((long)value < 0x8001 && (long)value > -0x8000)
684 {
685 unsigned char code;
686
687 /* Get the opcode. */
688 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
689
690 if (code != 0xe0 && code != 0xe1)
691 continue;
692
693 /* Note that we've changed the relocs, section contents, etc. */
694 elf_section_data (sec)->relocs = internal_relocs;
695 free_relocs = NULL;
696
697 elf_section_data (sec)->this_hdr.contents = contents;
698 free_contents = NULL;
699
700 symtab_hdr->contents = (bfd_byte *) extsyms;
701 free_extsyms = NULL;
702
703 /* Fix the opcode. */
704 if (code == 0xe0)
705 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
706 else if (code == 0xe1)
707 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
708
709 /* Fix the relocation's type. */
710 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
711 R_MN10200_PCREL16);
712
713 /* The opcode got shorter too, so we have to fix the
714 addend and offset too! */
715 irel->r_addend -= 1;
716 irel->r_offset -= 1;
717
718 /* Delete two bytes of data. */
719 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
720 irel->r_offset + 1, 2))
721 goto error_return;
722
723 /* That will change things, so, we should relax again.
724 Note that this is not required, and it may be slow. */
725 *again = true;
726 }
727 }
728
729 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
730 branch. */
731 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
732 {
733 bfd_vma value = symval;
734
735 /* Deal with pc-relative gunk. */
736 value -= (sec->output_section->vma + sec->output_offset);
737 value -= irel->r_offset;
738 value += irel->r_addend;
739
740 /* See if the value will fit in 8 bits, note the high value is
741 0x7f + 1 as the target will be one bytes closer if we are
742 able to relax. */
743 if ((long)value < 0x80 && (long)value > -0x80)
744 {
745 unsigned char code;
746
747 /* Get the opcode. */
748 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
749
750 if (code != 0xfc)
751 continue;
752
753 /* Note that we've changed the relocs, section contents, etc. */
754 elf_section_data (sec)->relocs = internal_relocs;
755 free_relocs = NULL;
756
757 elf_section_data (sec)->this_hdr.contents = contents;
758 free_contents = NULL;
759
760 symtab_hdr->contents = (bfd_byte *) extsyms;
761 free_extsyms = NULL;
762
763 /* Fix the opcode. */
764 bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
765
766 /* Fix the relocation's type. */
767 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
768 R_MN10200_PCREL8);
769
770 /* Delete one byte of data. */
771 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
772 irel->r_offset + 1, 1))
773 goto error_return;
774
775 /* That will change things, so, we should relax again.
776 Note that this is not required, and it may be slow. */
777 *again = true;
778 }
779 }
780
781 /* Try to eliminate an unconditional 8 bit pc-relative branch
782 which immediately follows a conditional 8 bit pc-relative
783 branch around the unconditional branch.
784
785 original: new:
786 bCC lab1 bCC' lab2
787 bra lab2
788 lab1: lab1:
789
790
791 This happens when the bCC can't reach lab2 at assembly time,
792 but due to other relaxations it can reach at link time. */
793 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
794 {
795 Elf_Internal_Rela *nrel;
796 bfd_vma value = symval;
797 unsigned char code;
798
799 /* Deal with pc-relative gunk. */
800 value -= (sec->output_section->vma + sec->output_offset);
801 value -= irel->r_offset;
802 value += irel->r_addend;
803
804 /* Do nothing if this reloc is the last byte in the section. */
805 if (irel->r_offset == sec->_cooked_size)
806 continue;
807
808 /* See if the next instruction is an unconditional pc-relative
809 branch, more often than not this test will fail, so we
810 test it first to speed things up. */
811 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
812 if (code != 0xea)
813 continue;
814
815 /* Also make sure the next relocation applies to the next
816 instruction and that it's a pc-relative 8 bit branch. */
817 nrel = irel + 1;
818 if (nrel == irelend
819 || irel->r_offset + 2 != nrel->r_offset
820 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
821 continue;
822
823 /* Make sure our destination immediately follows the
824 unconditional branch. */
825 if (symval != (sec->output_section->vma + sec->output_offset
826 + irel->r_offset + 3))
827 continue;
828
829 /* Now make sure we are a conditional branch. This may not
830 be necessary, but why take the chance.
831
832 Note these checks assume that R_MN10200_PCREL8 relocs
833 only occur on bCC and bCCx insns. If they occured
834 elsewhere, we'd need to know the start of this insn
835 for this check to be accurate. */
836 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
837 if (code != 0xe0 && code != 0xe1 && code != 0xe2
838 && code != 0xe3 && code != 0xe4 && code != 0xe5
839 && code != 0xe6 && code != 0xe7 && code != 0xe8
840 && code != 0xe9 && code != 0xec && code != 0xed
841 && code != 0xee && code != 0xef && code != 0xfc
842 && code != 0xfd && code != 0xfe && code != 0xff)
843 continue;
844
845 /* We also have to be sure there is no symbol/label
846 at the unconditional branch. */
847 if (mn10200_elf_symbol_address_p (abfd, sec, extsyms,
848 irel->r_offset + 1))
849 continue;
850
851 /* Note that we've changed the relocs, section contents, etc. */
852 elf_section_data (sec)->relocs = internal_relocs;
853 free_relocs = NULL;
854
855 elf_section_data (sec)->this_hdr.contents = contents;
856 free_contents = NULL;
857
858 symtab_hdr->contents = (bfd_byte *) extsyms;
859 free_extsyms = NULL;
860
861 /* Reverse the condition of the first branch. */
862 switch (code)
863 {
864 case 0xfc:
865 code = 0xfd;
866 break;
867 case 0xfd:
868 code = 0xfc;
869 break;
870 case 0xfe:
871 code = 0xff;
872 break;
873 case 0xff:
874 code = 0xfe;
875 break;
876 case 0xe8:
877 code = 0xe9;
878 break;
879 case 0xe9:
880 code = 0xe8;
881 break;
882 case 0xe0:
883 code = 0xe2;
884 break;
885 case 0xe2:
886 code = 0xe0;
887 break;
888 case 0xe3:
889 code = 0xe1;
890 break;
891 case 0xe1:
892 code = 0xe3;
893 break;
894 case 0xe4:
895 code = 0xe6;
896 break;
897 case 0xe6:
898 code = 0xe4;
899 break;
900 case 0xe7:
901 code = 0xe5;
902 break;
903 case 0xe5:
904 code = 0xe7;
905 break;
906 case 0xec:
907 code = 0xed;
908 break;
909 case 0xed:
910 code = 0xec;
911 break;
912 case 0xee:
913 code = 0xef;
914 break;
915 case 0xef:
916 code = 0xee;
917 break;
918 }
919 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
920
921 /* Set the reloc type and symbol for the first branch
922 from the second branch. */
923 irel->r_info = nrel->r_info;
924
925 /* Make the reloc for the second branch a null reloc. */
926 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
927 R_MN10200_NONE);
928
929 /* Delete two bytes of data. */
930 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
931 irel->r_offset + 1, 2))
932 goto error_return;
933
934 printf("yahoo\n");
935 /* That will change things, so, we should relax again.
936 Note that this is not required, and it may be slow. */
937 *again = true;
938 }
939
940 }
941
942 if (free_relocs != NULL)
943 {
944 free (free_relocs);
945 free_relocs = NULL;
946 }
947
948 if (free_contents != NULL)
949 {
950 if (! link_info->keep_memory)
951 free (free_contents);
952 else
953 {
954 /* Cache the section contents for elf_link_input_bfd. */
955 elf_section_data (sec)->this_hdr.contents = contents;
956 }
957 free_contents = NULL;
958 }
959
960 if (free_extsyms != NULL)
961 {
962 if (! link_info->keep_memory)
963 free (free_extsyms);
964 else
965 {
966 /* Cache the symbols for elf_link_input_bfd. */
967 symtab_hdr->contents = extsyms;
968 }
969 free_extsyms = NULL;
970 }
971
972 return true;
973
974 error_return:
975 if (free_relocs != NULL)
976 free (free_relocs);
977 if (free_contents != NULL)
978 free (free_contents);
979 if (free_extsyms != NULL)
980 free (free_extsyms);
981 return false;
982 }
983
984 /* Delete some bytes from a section while relaxing. */
985
986 static boolean
987 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
988 bfd *abfd;
989 asection *sec;
990 bfd_vma addr;
991 int count;
992 {
993 Elf_Internal_Shdr *symtab_hdr;
994 Elf32_External_Sym *extsyms;
995 int shndx;
996 bfd_byte *contents;
997 Elf_Internal_Rela *irel, *irelend;
998 Elf_Internal_Rela *irelalign;
999 bfd_vma toaddr;
1000 Elf32_External_Sym *esym, *esymend;
1001 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1002
1003 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1004 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1005
1006 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1007
1008 contents = elf_section_data (sec)->this_hdr.contents;
1009
1010 /* The deletion must stop at the next ALIGN reloc for an aligment
1011 power larger than the number of bytes we are deleting. */
1012
1013 irelalign = NULL;
1014 toaddr = sec->_cooked_size;
1015
1016 irel = elf_section_data (sec)->relocs;
1017 irelend = irel + sec->reloc_count;
1018
1019 /* Actually delete the bytes. */
1020 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1021 sec->_cooked_size -= count;
1022
1023 /* Adjust all the relocs. */
1024 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1025 {
1026 /* Get the new reloc address. */
1027 if ((irel->r_offset > addr
1028 && irel->r_offset < toaddr))
1029 irel->r_offset -= count;
1030 }
1031
1032 /* Adjust all the symbols. */
1033 esym = extsyms;
1034 esymend = esym + symtab_hdr->sh_info;
1035 for (; esym < esymend; esym++)
1036 {
1037 Elf_Internal_Sym isym;
1038
1039 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1040
1041 if (isym.st_shndx == shndx
1042 && isym.st_value > addr
1043 && isym.st_value < toaddr)
1044 {
1045 isym.st_value -= count;
1046 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1047 }
1048 }
1049
1050 sym_hash = elf_sym_hashes (abfd);
1051 sym_hash_end = (sym_hash
1052 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1053 - symtab_hdr->sh_info));
1054 for (; sym_hash < sym_hash_end; sym_hash++)
1055 {
1056 if (((*sym_hash)->root.type == bfd_link_hash_defined
1057 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1058 && (*sym_hash)->root.u.def.section == sec
1059 && (*sym_hash)->root.u.def.value > addr
1060 && (*sym_hash)->root.u.def.value < toaddr)
1061 {
1062 (*sym_hash)->root.u.def.value -= count;
1063 }
1064 }
1065
1066 return true;
1067 }
1068
1069 /* Return true if a symbol exists at the given address, else return
1070 false. */
1071 static boolean
1072 mn10200_elf_symbol_address_p (abfd, sec, extsyms, addr)
1073 bfd *abfd;
1074 asection *sec;
1075 Elf32_External_Sym *extsyms;
1076 bfd_vma addr;
1077 {
1078 Elf_Internal_Shdr *symtab_hdr;
1079 int shndx;
1080 Elf32_External_Sym *esym, *esymend;
1081 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1082
1083 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1084 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1085
1086 /* Examine all the symbols. */
1087 esym = extsyms;
1088 esymend = esym + symtab_hdr->sh_info;
1089 for (; esym < esymend; esym++)
1090 {
1091 Elf_Internal_Sym isym;
1092
1093 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1094
1095 if (isym.st_shndx == shndx
1096 && isym.st_value == addr)
1097 return true;
1098 }
1099
1100 sym_hash = elf_sym_hashes (abfd);
1101 sym_hash_end = (sym_hash
1102 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1103 - symtab_hdr->sh_info));
1104 for (; sym_hash < sym_hash_end; sym_hash++)
1105 {
1106 if (((*sym_hash)->root.type == bfd_link_hash_defined
1107 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1108 && (*sym_hash)->root.u.def.section == sec
1109 && (*sym_hash)->root.u.def.value == addr)
1110 return true;
1111 }
1112 return false;
1113 }
1114
1115 /* This is a version of bfd_generic_get_relocated_section_contents
1116 which uses mn10200_elf_relocate_section. */
1117
1118 static bfd_byte *
1119 mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1120 data, relocateable, symbols)
1121 bfd *output_bfd;
1122 struct bfd_link_info *link_info;
1123 struct bfd_link_order *link_order;
1124 bfd_byte *data;
1125 boolean relocateable;
1126 asymbol **symbols;
1127 {
1128 Elf_Internal_Shdr *symtab_hdr;
1129 asection *input_section = link_order->u.indirect.section;
1130 bfd *input_bfd = input_section->owner;
1131 asection **sections = NULL;
1132 Elf_Internal_Rela *internal_relocs = NULL;
1133 Elf32_External_Sym *external_syms = NULL;
1134 Elf_Internal_Sym *internal_syms = NULL;
1135
1136 /* We only need to handle the case of relaxing, or of having a
1137 particular set of section contents, specially. */
1138 if (relocateable
1139 || elf_section_data (input_section)->this_hdr.contents == NULL)
1140 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1141 link_order, data,
1142 relocateable,
1143 symbols);
1144
1145 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1146
1147 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1148 input_section->_raw_size);
1149
1150 if ((input_section->flags & SEC_RELOC) != 0
1151 && input_section->reloc_count > 0)
1152 {
1153 Elf_Internal_Sym *isymp;
1154 asection **secpp;
1155 Elf32_External_Sym *esym, *esymend;
1156
1157 if (symtab_hdr->contents != NULL)
1158 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1159 else
1160 {
1161 external_syms = ((Elf32_External_Sym *)
1162 bfd_malloc (symtab_hdr->sh_info
1163 * sizeof (Elf32_External_Sym)));
1164 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1165 goto error_return;
1166 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1167 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1168 symtab_hdr->sh_info, input_bfd)
1169 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1170 goto error_return;
1171 }
1172
1173 internal_relocs = (_bfd_elf32_link_read_relocs
1174 (input_bfd, input_section, (PTR) NULL,
1175 (Elf_Internal_Rela *) NULL, false));
1176 if (internal_relocs == NULL)
1177 goto error_return;
1178
1179 internal_syms = ((Elf_Internal_Sym *)
1180 bfd_malloc (symtab_hdr->sh_info
1181 * sizeof (Elf_Internal_Sym)));
1182 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1183 goto error_return;
1184
1185 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1186 * sizeof (asection *));
1187 if (sections == NULL && symtab_hdr->sh_info > 0)
1188 goto error_return;
1189
1190 isymp = internal_syms;
1191 secpp = sections;
1192 esym = external_syms;
1193 esymend = esym + symtab_hdr->sh_info;
1194 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1195 {
1196 asection *isec;
1197
1198 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1199
1200 if (isymp->st_shndx == SHN_UNDEF)
1201 isec = bfd_und_section_ptr;
1202 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1203 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1204 else if (isymp->st_shndx == SHN_ABS)
1205 isec = bfd_abs_section_ptr;
1206 else if (isymp->st_shndx == SHN_COMMON)
1207 isec = bfd_com_section_ptr;
1208 else
1209 {
1210 /* Who knows? */
1211 isec = NULL;
1212 }
1213
1214 *secpp = isec;
1215 }
1216
1217 if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1218 input_section, data, internal_relocs,
1219 internal_syms, sections))
1220 goto error_return;
1221
1222 if (sections != NULL)
1223 free (sections);
1224 sections = NULL;
1225 if (internal_syms != NULL)
1226 free (internal_syms);
1227 internal_syms = NULL;
1228 if (external_syms != NULL && symtab_hdr->contents == NULL)
1229 free (external_syms);
1230 external_syms = NULL;
1231 if (internal_relocs != elf_section_data (input_section)->relocs)
1232 free (internal_relocs);
1233 internal_relocs = NULL;
1234 }
1235
1236 return data;
1237
1238 error_return:
1239 if (internal_relocs != NULL
1240 && internal_relocs != elf_section_data (input_section)->relocs)
1241 free (internal_relocs);
1242 if (external_syms != NULL && symtab_hdr->contents == NULL)
1243 free (external_syms);
1244 if (internal_syms != NULL)
1245 free (internal_syms);
1246 if (sections != NULL)
1247 free (sections);
1248 return NULL;
1249 }
1250
1251
1252 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1253 #define TARGET_LITTLE_NAME "elf32-mn10200"
1254 #define ELF_ARCH bfd_arch_mn10200
1255 #define ELF_MACHINE_CODE EM_CYGNUS_MN10200
1256 #define ELF_MAXPAGESIZE 0x1000
1257
1258 #define elf_info_to_howto mn10200_info_to_howto
1259 #define elf_info_to_howto_rel 0
1260 #define elf_backend_relocate_section mn10200_elf_relocate_section
1261 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1262 #define bfd_elf32_bfd_get_relocated_section_contents \
1263 mn10200_elf_get_relocated_section_contents
1264
1265 #define elf_symbol_leading_char '_'
1266
1267 #include "elf32-target.h"
This page took 0.057261 seconds and 4 git commands to generate.