Twiddle some comments to more accurately reflect reality.
[deliverable/binutils-gdb.git] / bfd / elf32-mn10200.c
CommitLineData
ae1b99e4 1/* Matsushita 10200 specific support for 32-bit ELF
b3ef3894 2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
efc2b064
JL
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, 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
25static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
26 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
e14af8fc
JL
27static void mn10200_info_to_howto
28 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
fd395661
JL
29static boolean mn10200_elf_relax_delete_bytes
30 PARAMS ((bfd *, asection *, bfd_vma, int));
31static boolean mn10200_elf_symbol_address_p
32 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
e14af8fc
JL
33
34/* We have to use RELA instructions since md_apply_fix3 in the assembler
35 does absolutely nothing. */
36#define USE_RELA
efc2b064
JL
37
38enum reloc_type
39{
ae1b99e4 40 R_MN10200_NONE = 0,
e14af8fc
JL
41 R_MN10200_32,
42 R_MN10200_16,
43 R_MN10200_8,
b3ef3894
JL
44 R_MN10200_24,
45 R_MN10200_PCREL8,
1a2faf1f 46 R_MN10200_PCREL16,
b3ef3894 47 R_MN10200_PCREL24,
ae1b99e4 48 R_MN10200_MAX
efc2b064
JL
49};
50
ae1b99e4 51static reloc_howto_type elf_mn10200_howto_table[] =
efc2b064 52{
e14af8fc 53 /* Dummy relocation. Does nothing. */
ae1b99e4 54 HOWTO (R_MN10200_NONE,
efc2b064
JL
55 0,
56 2,
57 16,
58 false,
59 0,
60 complain_overflow_bitfield,
61 bfd_elf_generic_reloc,
ae1b99e4 62 "R_MN10200_NONE",
efc2b064
JL
63 false,
64 0,
65 0,
66 false),
e14af8fc
JL
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),
b3ef3894
JL
109 /* Standard 24 bit reloc. */
110 HOWTO (R_MN10200_24,
e14af8fc
JL
111 0,
112 2,
b3ef3894
JL
113 24,
114 false,
e14af8fc
JL
115 0,
116 complain_overflow_bitfield,
b3ef3894
JL
117 bfd_elf_generic_reloc,
118 "R_MN10200_24",
119 false,
120 0xffffff,
121 0xffffff,
e14af8fc 122 false),
b3ef3894
JL
123 /* Simple 8 pc-relative reloc. */
124 HOWTO (R_MN10200_PCREL8,
e14af8fc
JL
125 0,
126 0,
127 8,
128 true,
129 0,
130 complain_overflow_bitfield,
40131692 131 bfd_elf_generic_reloc,
b3ef3894 132 "R_MN10200_PCREL8",
40131692 133 false,
e14af8fc
JL
134 0xff,
135 0xff,
136 true),
1a2faf1f
JL
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),
b3ef3894 151 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
e14af8fc 152 to get the pc-relative offset correct. */
b3ef3894 153 HOWTO (R_MN10200_PCREL24,
e14af8fc
JL
154 0,
155 2,
b3ef3894 156 24,
e14af8fc
JL
157 true,
158 0,
159 complain_overflow_bitfield,
40131692 160 bfd_elf_generic_reloc,
b3ef3894 161 "R_MN10200_PCREL24",
40131692 162 false,
b3ef3894
JL
163 0xffffff,
164 0xffffff,
483e1b91 165 true),
efc2b064
JL
166};
167
ae1b99e4 168struct mn10200_reloc_map
efc2b064
JL
169{
170 unsigned char bfd_reloc_val;
171 unsigned char elf_reloc_val;
172};
173
ae1b99e4 174static const struct mn10200_reloc_map mn10200_reloc_map[] =
efc2b064 175{
ae1b99e4 176 { BFD_RELOC_NONE, R_MN10200_NONE, },
e14af8fc
JL
177 { BFD_RELOC_32, R_MN10200_32, },
178 { BFD_RELOC_16, R_MN10200_16, },
179 { BFD_RELOC_8, R_MN10200_8, },
b3ef3894
JL
180 { BFD_RELOC_24, R_MN10200_24, },
181 { BFD_RELOC_8_PCREL, R_MN10200_PCREL8, },
1a2faf1f 182 { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
b3ef3894 183 { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
efc2b064
JL
184};
185
186static reloc_howto_type *
187bfd_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;
ae1b99e4 194 i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
efc2b064
JL
195 i++)
196 {
ae1b99e4
JL
197 if (mn10200_reloc_map[i].bfd_reloc_val == code)
198 return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
efc2b064
JL
199 }
200
201 return NULL;
202}
203
e14af8fc 204/* Set the howto pointer for an MN10200 ELF reloc. */
efc2b064
JL
205
206static void
e14af8fc 207mn10200_info_to_howto (abfd, cache_ptr, dst)
efc2b064
JL
208 bfd *abfd;
209 arelent *cache_ptr;
e14af8fc 210 Elf32_Internal_Rela *dst;
efc2b064
JL
211{
212 unsigned int r_type;
213
214 r_type = ELF32_R_TYPE (dst->r_info);
ae1b99e4
JL
215 BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
216 cache_ptr->howto = &elf_mn10200_howto_table[r_type];
efc2b064
JL
217}
218
64af6679
JL
219/* Perform a relocation as part of a final link. */
220static bfd_reloc_status_type
221mn10200_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{
64af6679
JL
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. */
328static boolean
329mn10200_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
fd395661
JL
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
7c49f021 494 * jmp:16 -> bra:8 1 byte
fd395661 495
7c49f021 496 * If the previous instruction is a conditional branch
fd395661
JL
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
7c49f021 499 can then be deleted. 2 bytes
fd395661
JL
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
7c49f021
JL
509 abs24, imm24, d24 all look the same at the reloc level. It
510 might make the code simpler if we had different relocs for
511 the various relaxable operand types.
fd395661
JL
512
513 A '*' indicates a case this code can handle. */
514
515
516static boolean
517mn10200_elf_relax_section (abfd, sec, link_info, again)
518 bfd *abfd;
519 asection *sec;
520 struct bfd_link_info *link_info;
521 boolean *again;
522{
523 Elf_Internal_Shdr *symtab_hdr;
524 Elf_Internal_Rela *internal_relocs;
525 Elf_Internal_Rela *free_relocs = NULL;
526 Elf_Internal_Rela *irel, *irelend;
527 bfd_byte *contents = NULL;
528 bfd_byte *free_contents = NULL;
529 Elf32_External_Sym *extsyms = NULL;
530 Elf32_External_Sym *free_extsyms = NULL;
531
532 /* Assume nothing changes. */
533 *again = false;
534
535 /* We don't have to do anything for a relocateable link, if
536 this section does not have relocs, or if this is not a
537 code section. */
538 if (link_info->relocateable
539 || (sec->flags & SEC_RELOC) == 0
540 || sec->reloc_count == 0
541 || (sec->flags & SEC_CODE) == 0)
542 return true;
543
544 /* If this is the first time we have been called for this section,
545 initialize the cooked size. */
546 if (sec->_cooked_size == 0)
547 sec->_cooked_size = sec->_raw_size;
548
549 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
550
551 /* Get a copy of the native relocations. */
552 internal_relocs = (_bfd_elf32_link_read_relocs
553 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
554 link_info->keep_memory));
555 if (internal_relocs == NULL)
556 goto error_return;
557 if (! link_info->keep_memory)
558 free_relocs = internal_relocs;
559
560 /* Walk through them looking for relaxing opportunities. */
561 irelend = internal_relocs + sec->reloc_count;
562 for (irel = internal_relocs; irel < irelend; irel++)
563 {
564 bfd_vma symval;
565
566 /* If this isn't something that can be relaxed, then ignore
567 this reloc. */
568 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
569 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
570 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
571 continue;
572
573 /* Get the section contents if we haven't done so already. */
574 if (contents == NULL)
575 {
576 /* Get cached copy if it exists. */
577 if (elf_section_data (sec)->this_hdr.contents != NULL)
578 contents = elf_section_data (sec)->this_hdr.contents;
579 else
580 {
581 /* Go get them off disk. */
582 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
583 if (contents == NULL)
584 goto error_return;
585 free_contents = contents;
586
587 if (! bfd_get_section_contents (abfd, sec, contents,
588 (file_ptr) 0, sec->_raw_size))
589 goto error_return;
590 }
591 }
592
593 /* Read the local symbols if we haven't done so already. */
594 if (extsyms == NULL)
595 {
596 /* Get cached copy if it exists. */
597 if (symtab_hdr->contents != NULL)
598 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
599 else
600 {
601 /* Go get them off disk. */
602 extsyms = ((Elf32_External_Sym *)
603 bfd_malloc (symtab_hdr->sh_info
604 * sizeof (Elf32_External_Sym)));
605 if (extsyms == NULL)
606 goto error_return;
607 free_extsyms = extsyms;
608 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
609 || (bfd_read (extsyms, sizeof (Elf32_External_Sym),
610 symtab_hdr->sh_info, abfd)
611 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
612 goto error_return;
613 }
614 }
615
616 /* Get the value of the symbol referred to by the reloc. */
617 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
618 {
619 Elf_Internal_Sym isym;
620
621 /* A local symbol. */
622 bfd_elf32_swap_symbol_in (abfd,
623 extsyms + ELF32_R_SYM (irel->r_info),
624 &isym);
625
626#if 0
627 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
628 {
629 ((*_bfd_error_handler)
630 ("%s: 0x%lx: warning: symbol in unexpected section",
631 bfd_get_filename (abfd), (unsigned long) 0));
632 continue;
633 }
634#endif
635
636 symval = (isym.st_value
637 + sec->output_section->vma
638 + sec->output_offset);
639 }
640 else
641 {
642 unsigned long indx;
643 struct elf_link_hash_entry *h;
644
645 /* An external symbol. */
646 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
647 h = elf_sym_hashes (abfd)[indx];
648 BFD_ASSERT (h != NULL);
649 if (h->root.type != bfd_link_hash_defined
650 && h->root.type != bfd_link_hash_defweak)
651 {
652 /* This appears to be a reference to an undefined
653 symbol. Just ignore it--it will be caught by the
654 regular reloc processing. */
655 continue;
656 }
657
658 symval = (h->root.u.def.value
659 + h->root.u.def.section->output_section->vma
660 + h->root.u.def.section->output_offset);
661 }
662
663 /* For simplicity of coding, we are going to modify the section
664 contents, the section relocs, and the BFD symbol table. We
665 must tell the rest of the code not to free up this
666 information. It would be possible to instead create a table
667 of changes which have to be made, as is done in coff-mips.c;
668 that would be more work, but would require less memory when
669 the linker is run. */
670
671
672 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
673 branch/call. */
674 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
675 {
676 bfd_vma value = symval;
677
678 /* Deal with pc-relative gunk. */
679 value -= (sec->output_section->vma + sec->output_offset);
680 value -= irel->r_offset;
681 value += irel->r_addend;
682
683 /* See if the value will fit in 16 bits, note the high value is
684 0x7fff + 2 as the target will be two bytes closer if we are
685 able to relax. */
686 if ((long)value < 0x8001 && (long)value > -0x8000)
687 {
688 unsigned char code;
689
690 /* Get the opcode. */
691 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
692
693 if (code != 0xe0 && code != 0xe1)
694 continue;
695
696 /* Note that we've changed the relocs, section contents, etc. */
697 elf_section_data (sec)->relocs = internal_relocs;
698 free_relocs = NULL;
699
700 elf_section_data (sec)->this_hdr.contents = contents;
701 free_contents = NULL;
702
703 symtab_hdr->contents = (bfd_byte *) extsyms;
704 free_extsyms = NULL;
705
706 /* Fix the opcode. */
707 if (code == 0xe0)
708 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
709 else if (code == 0xe1)
710 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
711
712 /* Fix the relocation's type. */
713 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
714 R_MN10200_PCREL16);
715
716 /* The opcode got shorter too, so we have to fix the
717 addend and offset too! */
718 irel->r_addend -= 1;
719 irel->r_offset -= 1;
720
721 /* Delete two bytes of data. */
722 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
723 irel->r_offset + 1, 2))
724 goto error_return;
725
726 /* That will change things, so, we should relax again.
727 Note that this is not required, and it may be slow. */
728 *again = true;
729 }
730 }
731
732 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
733 branch. */
734 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
735 {
736 bfd_vma value = symval;
737
738 /* Deal with pc-relative gunk. */
739 value -= (sec->output_section->vma + sec->output_offset);
740 value -= irel->r_offset;
741 value += irel->r_addend;
742
743 /* See if the value will fit in 8 bits, note the high value is
744 0x7f + 1 as the target will be one bytes closer if we are
745 able to relax. */
746 if ((long)value < 0x80 && (long)value > -0x80)
747 {
748 unsigned char code;
749
750 /* Get the opcode. */
751 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
752
753 if (code != 0xfc)
754 continue;
755
756 /* Note that we've changed the relocs, section contents, etc. */
757 elf_section_data (sec)->relocs = internal_relocs;
758 free_relocs = NULL;
759
760 elf_section_data (sec)->this_hdr.contents = contents;
761 free_contents = NULL;
762
763 symtab_hdr->contents = (bfd_byte *) extsyms;
764 free_extsyms = NULL;
765
766 /* Fix the opcode. */
767 bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
768
769 /* Fix the relocation's type. */
770 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
771 R_MN10200_PCREL8);
772
773 /* Delete one byte of data. */
774 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
775 irel->r_offset + 1, 1))
776 goto error_return;
777
778 /* That will change things, so, we should relax again.
779 Note that this is not required, and it may be slow. */
780 *again = true;
781 }
782 }
783
784 /* Try to eliminate an unconditional 8 bit pc-relative branch
785 which immediately follows a conditional 8 bit pc-relative
786 branch around the unconditional branch.
787
788 original: new:
789 bCC lab1 bCC' lab2
790 bra lab2
791 lab1: lab1:
792
793
794 This happens when the bCC can't reach lab2 at assembly time,
795 but due to other relaxations it can reach at link time. */
796 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
797 {
798 Elf_Internal_Rela *nrel;
799 bfd_vma value = symval;
800 unsigned char code;
801
802 /* Deal with pc-relative gunk. */
803 value -= (sec->output_section->vma + sec->output_offset);
804 value -= irel->r_offset;
805 value += irel->r_addend;
806
807 /* Do nothing if this reloc is the last byte in the section. */
808 if (irel->r_offset == sec->_cooked_size)
809 continue;
810
811 /* See if the next instruction is an unconditional pc-relative
812 branch, more often than not this test will fail, so we
813 test it first to speed things up. */
814 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
815 if (code != 0xea)
816 continue;
817
818 /* Also make sure the next relocation applies to the next
819 instruction and that it's a pc-relative 8 bit branch. */
820 nrel = irel + 1;
821 if (nrel == irelend
822 || irel->r_offset + 2 != nrel->r_offset
823 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
824 continue;
825
826 /* Make sure our destination immediately follows the
827 unconditional branch. */
828 if (symval != (sec->output_section->vma + sec->output_offset
829 + irel->r_offset + 3))
830 continue;
831
832 /* Now make sure we are a conditional branch. This may not
833 be necessary, but why take the chance.
834
835 Note these checks assume that R_MN10200_PCREL8 relocs
836 only occur on bCC and bCCx insns. If they occured
837 elsewhere, we'd need to know the start of this insn
838 for this check to be accurate. */
839 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
840 if (code != 0xe0 && code != 0xe1 && code != 0xe2
841 && code != 0xe3 && code != 0xe4 && code != 0xe5
842 && code != 0xe6 && code != 0xe7 && code != 0xe8
843 && code != 0xe9 && code != 0xec && code != 0xed
844 && code != 0xee && code != 0xef && code != 0xfc
845 && code != 0xfd && code != 0xfe && code != 0xff)
846 continue;
847
848 /* We also have to be sure there is no symbol/label
849 at the unconditional branch. */
850 if (mn10200_elf_symbol_address_p (abfd, sec, extsyms,
851 irel->r_offset + 1))
852 continue;
853
854 /* Note that we've changed the relocs, section contents, etc. */
855 elf_section_data (sec)->relocs = internal_relocs;
856 free_relocs = NULL;
857
858 elf_section_data (sec)->this_hdr.contents = contents;
859 free_contents = NULL;
860
861 symtab_hdr->contents = (bfd_byte *) extsyms;
862 free_extsyms = NULL;
863
864 /* Reverse the condition of the first branch. */
865 switch (code)
866 {
867 case 0xfc:
868 code = 0xfd;
869 break;
870 case 0xfd:
871 code = 0xfc;
872 break;
873 case 0xfe:
874 code = 0xff;
875 break;
876 case 0xff:
877 code = 0xfe;
878 break;
879 case 0xe8:
880 code = 0xe9;
881 break;
882 case 0xe9:
883 code = 0xe8;
884 break;
885 case 0xe0:
886 code = 0xe2;
887 break;
888 case 0xe2:
889 code = 0xe0;
890 break;
891 case 0xe3:
892 code = 0xe1;
893 break;
894 case 0xe1:
895 code = 0xe3;
896 break;
897 case 0xe4:
898 code = 0xe6;
899 break;
900 case 0xe6:
901 code = 0xe4;
902 break;
903 case 0xe7:
904 code = 0xe5;
905 break;
906 case 0xe5:
907 code = 0xe7;
908 break;
909 case 0xec:
910 code = 0xed;
911 break;
912 case 0xed:
913 code = 0xec;
914 break;
915 case 0xee:
916 code = 0xef;
917 break;
918 case 0xef:
919 code = 0xee;
920 break;
921 }
922 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
923
924 /* Set the reloc type and symbol for the first branch
925 from the second branch. */
926 irel->r_info = nrel->r_info;
927
928 /* Make the reloc for the second branch a null reloc. */
929 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
930 R_MN10200_NONE);
931
932 /* Delete two bytes of data. */
933 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
934 irel->r_offset + 1, 2))
935 goto error_return;
936
fd395661
JL
937 /* That will change things, so, we should relax again.
938 Note that this is not required, and it may be slow. */
939 *again = true;
940 }
941
942 }
943
944 if (free_relocs != NULL)
945 {
946 free (free_relocs);
947 free_relocs = NULL;
948 }
949
950 if (free_contents != NULL)
951 {
952 if (! link_info->keep_memory)
953 free (free_contents);
954 else
955 {
956 /* Cache the section contents for elf_link_input_bfd. */
957 elf_section_data (sec)->this_hdr.contents = contents;
958 }
959 free_contents = NULL;
960 }
961
962 if (free_extsyms != NULL)
963 {
964 if (! link_info->keep_memory)
965 free (free_extsyms);
966 else
967 {
968 /* Cache the symbols for elf_link_input_bfd. */
969 symtab_hdr->contents = extsyms;
970 }
971 free_extsyms = NULL;
972 }
973
974 return true;
975
976 error_return:
977 if (free_relocs != NULL)
978 free (free_relocs);
979 if (free_contents != NULL)
980 free (free_contents);
981 if (free_extsyms != NULL)
982 free (free_extsyms);
983 return false;
984}
985
986/* Delete some bytes from a section while relaxing. */
987
988static boolean
989mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
990 bfd *abfd;
991 asection *sec;
992 bfd_vma addr;
993 int count;
994{
995 Elf_Internal_Shdr *symtab_hdr;
996 Elf32_External_Sym *extsyms;
997 int shndx;
998 bfd_byte *contents;
999 Elf_Internal_Rela *irel, *irelend;
1000 Elf_Internal_Rela *irelalign;
1001 bfd_vma toaddr;
1002 Elf32_External_Sym *esym, *esymend;
1003 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1004
1005 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1006 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1007
1008 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1009
1010 contents = elf_section_data (sec)->this_hdr.contents;
1011
1012 /* The deletion must stop at the next ALIGN reloc for an aligment
1013 power larger than the number of bytes we are deleting. */
1014
1015 irelalign = NULL;
1016 toaddr = sec->_cooked_size;
1017
1018 irel = elf_section_data (sec)->relocs;
1019 irelend = irel + sec->reloc_count;
1020
1021 /* Actually delete the bytes. */
1022 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1023 sec->_cooked_size -= count;
1024
1025 /* Adjust all the relocs. */
1026 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1027 {
1028 /* Get the new reloc address. */
1029 if ((irel->r_offset > addr
1030 && irel->r_offset < toaddr))
1031 irel->r_offset -= count;
1032 }
1033
1034 /* Adjust all the symbols. */
1035 esym = extsyms;
1036 esymend = esym + symtab_hdr->sh_info;
1037 for (; esym < esymend; esym++)
1038 {
1039 Elf_Internal_Sym isym;
1040
1041 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1042
1043 if (isym.st_shndx == shndx
1044 && isym.st_value > addr
1045 && isym.st_value < toaddr)
1046 {
1047 isym.st_value -= count;
1048 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1049 }
1050 }
1051
1052 sym_hash = elf_sym_hashes (abfd);
1053 sym_hash_end = (sym_hash
1054 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1055 - symtab_hdr->sh_info));
1056 for (; sym_hash < sym_hash_end; sym_hash++)
1057 {
1058 if (((*sym_hash)->root.type == bfd_link_hash_defined
1059 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1060 && (*sym_hash)->root.u.def.section == sec
1061 && (*sym_hash)->root.u.def.value > addr
1062 && (*sym_hash)->root.u.def.value < toaddr)
1063 {
1064 (*sym_hash)->root.u.def.value -= count;
1065 }
1066 }
1067
1068 return true;
1069}
1070
1071/* Return true if a symbol exists at the given address, else return
1072 false. */
1073static boolean
1074mn10200_elf_symbol_address_p (abfd, sec, extsyms, addr)
1075 bfd *abfd;
1076 asection *sec;
1077 Elf32_External_Sym *extsyms;
1078 bfd_vma addr;
1079{
1080 Elf_Internal_Shdr *symtab_hdr;
1081 int shndx;
1082 Elf32_External_Sym *esym, *esymend;
1083 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1084
1085 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1086 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1087
1088 /* Examine all the symbols. */
1089 esym = extsyms;
1090 esymend = esym + symtab_hdr->sh_info;
1091 for (; esym < esymend; esym++)
1092 {
1093 Elf_Internal_Sym isym;
1094
1095 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1096
1097 if (isym.st_shndx == shndx
1098 && isym.st_value == addr)
1099 return true;
1100 }
1101
1102 sym_hash = elf_sym_hashes (abfd);
1103 sym_hash_end = (sym_hash
1104 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1105 - symtab_hdr->sh_info));
1106 for (; sym_hash < sym_hash_end; sym_hash++)
1107 {
1108 if (((*sym_hash)->root.type == bfd_link_hash_defined
1109 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1110 && (*sym_hash)->root.u.def.section == sec
1111 && (*sym_hash)->root.u.def.value == addr)
1112 return true;
1113 }
1114 return false;
1115}
1116
1117/* This is a version of bfd_generic_get_relocated_section_contents
1118 which uses mn10200_elf_relocate_section. */
1119
1120static bfd_byte *
1121mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1122 data, relocateable, symbols)
1123 bfd *output_bfd;
1124 struct bfd_link_info *link_info;
1125 struct bfd_link_order *link_order;
1126 bfd_byte *data;
1127 boolean relocateable;
1128 asymbol **symbols;
1129{
1130 Elf_Internal_Shdr *symtab_hdr;
1131 asection *input_section = link_order->u.indirect.section;
1132 bfd *input_bfd = input_section->owner;
1133 asection **sections = NULL;
1134 Elf_Internal_Rela *internal_relocs = NULL;
1135 Elf32_External_Sym *external_syms = NULL;
1136 Elf_Internal_Sym *internal_syms = NULL;
1137
1138 /* We only need to handle the case of relaxing, or of having a
1139 particular set of section contents, specially. */
1140 if (relocateable
1141 || elf_section_data (input_section)->this_hdr.contents == NULL)
1142 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1143 link_order, data,
1144 relocateable,
1145 symbols);
1146
1147 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1148
1149 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1150 input_section->_raw_size);
1151
1152 if ((input_section->flags & SEC_RELOC) != 0
1153 && input_section->reloc_count > 0)
1154 {
1155 Elf_Internal_Sym *isymp;
1156 asection **secpp;
1157 Elf32_External_Sym *esym, *esymend;
1158
1159 if (symtab_hdr->contents != NULL)
1160 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1161 else
1162 {
1163 external_syms = ((Elf32_External_Sym *)
1164 bfd_malloc (symtab_hdr->sh_info
1165 * sizeof (Elf32_External_Sym)));
1166 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1167 goto error_return;
1168 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1169 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1170 symtab_hdr->sh_info, input_bfd)
1171 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1172 goto error_return;
1173 }
1174
1175 internal_relocs = (_bfd_elf32_link_read_relocs
1176 (input_bfd, input_section, (PTR) NULL,
1177 (Elf_Internal_Rela *) NULL, false));
1178 if (internal_relocs == NULL)
1179 goto error_return;
1180
1181 internal_syms = ((Elf_Internal_Sym *)
1182 bfd_malloc (symtab_hdr->sh_info
1183 * sizeof (Elf_Internal_Sym)));
1184 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1185 goto error_return;
1186
1187 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1188 * sizeof (asection *));
1189 if (sections == NULL && symtab_hdr->sh_info > 0)
1190 goto error_return;
1191
1192 isymp = internal_syms;
1193 secpp = sections;
1194 esym = external_syms;
1195 esymend = esym + symtab_hdr->sh_info;
1196 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1197 {
1198 asection *isec;
1199
1200 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1201
1202 if (isymp->st_shndx == SHN_UNDEF)
1203 isec = bfd_und_section_ptr;
1204 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1205 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1206 else if (isymp->st_shndx == SHN_ABS)
1207 isec = bfd_abs_section_ptr;
1208 else if (isymp->st_shndx == SHN_COMMON)
1209 isec = bfd_com_section_ptr;
1210 else
1211 {
1212 /* Who knows? */
1213 isec = NULL;
1214 }
1215
1216 *secpp = isec;
1217 }
1218
1219 if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1220 input_section, data, internal_relocs,
1221 internal_syms, sections))
1222 goto error_return;
1223
1224 if (sections != NULL)
1225 free (sections);
1226 sections = NULL;
1227 if (internal_syms != NULL)
1228 free (internal_syms);
1229 internal_syms = NULL;
1230 if (external_syms != NULL && symtab_hdr->contents == NULL)
1231 free (external_syms);
1232 external_syms = NULL;
1233 if (internal_relocs != elf_section_data (input_section)->relocs)
1234 free (internal_relocs);
1235 internal_relocs = NULL;
1236 }
1237
1238 return data;
1239
1240 error_return:
1241 if (internal_relocs != NULL
1242 && internal_relocs != elf_section_data (input_section)->relocs)
1243 free (internal_relocs);
1244 if (external_syms != NULL && symtab_hdr->contents == NULL)
1245 free (external_syms);
1246 if (internal_syms != NULL)
1247 free (internal_syms);
1248 if (sections != NULL)
1249 free (sections);
1250 return NULL;
1251}
1252
1253
ae1b99e4
JL
1254#define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1255#define TARGET_LITTLE_NAME "elf32-mn10200"
1256#define ELF_ARCH bfd_arch_mn10200
1257#define ELF_MACHINE_CODE EM_CYGNUS_MN10200
efc2b064
JL
1258#define ELF_MAXPAGESIZE 0x1000
1259
e14af8fc
JL
1260#define elf_info_to_howto mn10200_info_to_howto
1261#define elf_info_to_howto_rel 0
64af6679 1262#define elf_backend_relocate_section mn10200_elf_relocate_section
fd395661
JL
1263#define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1264#define bfd_elf32_bfd_get_relocated_section_contents \
1265 mn10200_elf_get_relocated_section_contents
e14af8fc
JL
1266
1267#define elf_symbol_leading_char '_'
efc2b064
JL
1268
1269#include "elf32-target.h"
This page took 0.08947 seconds and 4 git commands to generate.