2002-05-11 Ralf Corsepius <corsepiu@faw.uni-ulm.de>
[binutils.git] / bfd / elf-m10200.c
blob4e8de60df94afc4e21269799c2d0984692c03e36
1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
26 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
27 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
28 static void mn10200_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
30 static boolean mn10200_elf_relax_delete_bytes
31 PARAMS ((bfd *, asection *, bfd_vma, int));
32 static boolean mn10200_elf_symbol_address_p
33 PARAMS ((bfd *, asection *, bfd_vma));
34 static bfd_reloc_status_type mn10200_elf_final_link_relocate
35 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *,
36 bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
37 struct bfd_link_info *, asection *, int));
38 static boolean mn10200_elf_relocate_section
39 PARAMS (( bfd *, struct bfd_link_info *, bfd *, asection *,
40 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *,
41 asection **));
42 static boolean mn10200_elf_relax_section
43 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
44 static bfd_byte * mn10200_elf_get_relocated_section_contents
45 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
46 bfd_byte *, boolean, asymbol **));
48 /* We have to use RELA instructions since md_apply_fix3 in the assembler
49 does absolutely nothing. */
50 #define USE_RELA
52 enum reloc_type {
53 R_MN10200_NONE = 0,
54 R_MN10200_32,
55 R_MN10200_16,
56 R_MN10200_8,
57 R_MN10200_24,
58 R_MN10200_PCREL8,
59 R_MN10200_PCREL16,
60 R_MN10200_PCREL24,
61 R_MN10200_MAX
64 static reloc_howto_type elf_mn10200_howto_table[] = {
65 /* Dummy relocation. Does nothing. */
66 HOWTO (R_MN10200_NONE,
69 16,
70 false,
72 complain_overflow_bitfield,
73 bfd_elf_generic_reloc,
74 "R_MN10200_NONE",
75 false,
78 false),
79 /* Standard 32 bit reloc. */
80 HOWTO (R_MN10200_32,
83 32,
84 false,
86 complain_overflow_bitfield,
87 bfd_elf_generic_reloc,
88 "R_MN10200_32",
89 false,
90 0xffffffff,
91 0xffffffff,
92 false),
93 /* Standard 16 bit reloc. */
94 HOWTO (R_MN10200_16,
97 16,
98 false,
100 complain_overflow_bitfield,
101 bfd_elf_generic_reloc,
102 "R_MN10200_16",
103 false,
104 0xffff,
105 0xffff,
106 false),
107 /* Standard 8 bit reloc. */
108 HOWTO (R_MN10200_8,
112 false,
114 complain_overflow_bitfield,
115 bfd_elf_generic_reloc,
116 "R_MN10200_8",
117 false,
118 0xff,
119 0xff,
120 false),
121 /* Standard 24 bit reloc. */
122 HOWTO (R_MN10200_24,
126 false,
128 complain_overflow_bitfield,
129 bfd_elf_generic_reloc,
130 "R_MN10200_24",
131 false,
132 0xffffff,
133 0xffffff,
134 false),
135 /* Simple 8 pc-relative reloc. */
136 HOWTO (R_MN10200_PCREL8,
140 true,
142 complain_overflow_bitfield,
143 bfd_elf_generic_reloc,
144 "R_MN10200_PCREL8",
145 false,
146 0xff,
147 0xff,
148 true),
149 /* Simple 16 pc-relative reloc. */
150 HOWTO (R_MN10200_PCREL16,
154 true,
156 complain_overflow_bitfield,
157 bfd_elf_generic_reloc,
158 "R_MN10200_PCREL16",
159 false,
160 0xffff,
161 0xffff,
162 true),
163 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
164 to get the pc-relative offset correct. */
165 HOWTO (R_MN10200_PCREL24,
169 true,
171 complain_overflow_bitfield,
172 bfd_elf_generic_reloc,
173 "R_MN10200_PCREL24",
174 false,
175 0xffffff,
176 0xffffff,
177 true),
180 struct mn10200_reloc_map {
181 bfd_reloc_code_real_type bfd_reloc_val;
182 unsigned char elf_reloc_val;
185 static const struct mn10200_reloc_map mn10200_reloc_map[] = {
186 { BFD_RELOC_NONE , R_MN10200_NONE , },
187 { BFD_RELOC_32 , R_MN10200_32 , },
188 { BFD_RELOC_16 , R_MN10200_16 , },
189 { BFD_RELOC_8 , R_MN10200_8 , },
190 { BFD_RELOC_24 , R_MN10200_24 , },
191 { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , },
192 { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
193 { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
196 static reloc_howto_type *
197 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
198 bfd *abfd ATTRIBUTE_UNUSED;
199 bfd_reloc_code_real_type code;
201 unsigned int i;
203 for (i = 0;
204 i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
205 i++)
207 if (mn10200_reloc_map[i].bfd_reloc_val == code)
208 return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
211 return NULL;
214 /* Set the howto pointer for an MN10200 ELF reloc. */
216 static void
217 mn10200_info_to_howto (abfd, cache_ptr, dst)
218 bfd *abfd ATTRIBUTE_UNUSED;
219 arelent *cache_ptr;
220 Elf32_Internal_Rela *dst;
222 unsigned int r_type;
224 r_type = ELF32_R_TYPE (dst->r_info);
225 BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
226 cache_ptr->howto = &elf_mn10200_howto_table[r_type];
229 /* Perform a relocation as part of a final link. */
231 static bfd_reloc_status_type
232 mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
233 input_section, contents, offset, value,
234 addend, info, sym_sec, is_local)
235 reloc_howto_type *howto;
236 bfd *input_bfd;
237 bfd *output_bfd ATTRIBUTE_UNUSED;
238 asection *input_section;
239 bfd_byte *contents;
240 bfd_vma offset;
241 bfd_vma value;
242 bfd_vma addend;
243 struct bfd_link_info *info ATTRIBUTE_UNUSED;
244 asection *sym_sec ATTRIBUTE_UNUSED;
245 int is_local ATTRIBUTE_UNUSED;
247 unsigned long r_type = howto->type;
248 bfd_byte *hit_data = contents + offset;
250 switch (r_type)
253 case R_MN10200_NONE:
254 return bfd_reloc_ok;
256 case R_MN10200_32:
257 value += addend;
258 bfd_put_32 (input_bfd, value, hit_data);
259 return bfd_reloc_ok;
261 case R_MN10200_16:
262 value += addend;
264 if ((long) value > 0x7fff || (long) value < -0x8000)
265 return bfd_reloc_overflow;
267 bfd_put_16 (input_bfd, value, hit_data);
268 return bfd_reloc_ok;
270 case R_MN10200_8:
271 value += addend;
273 if ((long) value > 0x7f || (long) value < -0x80)
274 return bfd_reloc_overflow;
276 bfd_put_8 (input_bfd, value, hit_data);
277 return bfd_reloc_ok;
279 case R_MN10200_24:
280 value += addend;
282 if ((long) value > 0x7fffff || (long) value < -0x800000)
283 return bfd_reloc_overflow;
285 value &= 0xffffff;
286 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
287 bfd_put_32 (input_bfd, value, hit_data);
288 return bfd_reloc_ok;
290 case R_MN10200_PCREL8:
291 value -= (input_section->output_section->vma
292 + input_section->output_offset);
293 value -= (offset + 1);
294 value += addend;
296 if ((long) value > 0xff || (long) value < -0x100)
297 return bfd_reloc_overflow;
299 bfd_put_8 (input_bfd, value, hit_data);
300 return bfd_reloc_ok;
302 case R_MN10200_PCREL16:
303 value -= (input_section->output_section->vma
304 + input_section->output_offset);
305 value -= (offset + 2);
306 value += addend;
308 if ((long) value > 0xffff || (long) value < -0x10000)
309 return bfd_reloc_overflow;
311 bfd_put_16 (input_bfd, value, hit_data);
312 return bfd_reloc_ok;
314 case R_MN10200_PCREL24:
315 value -= (input_section->output_section->vma
316 + input_section->output_offset);
317 value -= (offset + 3);
318 value += addend;
320 if ((long) value > 0xffffff || (long) value < -0x1000000)
321 return bfd_reloc_overflow;
323 value &= 0xffffff;
324 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
325 bfd_put_32 (input_bfd, value, hit_data);
326 return bfd_reloc_ok;
328 default:
329 return bfd_reloc_notsupported;
333 /* Relocate an MN10200 ELF section. */
334 static boolean
335 mn10200_elf_relocate_section (output_bfd, info, input_bfd, input_section,
336 contents, relocs, local_syms, local_sections)
337 bfd *output_bfd;
338 struct bfd_link_info *info;
339 bfd *input_bfd;
340 asection *input_section;
341 bfd_byte *contents;
342 Elf_Internal_Rela *relocs;
343 Elf_Internal_Sym *local_syms;
344 asection **local_sections;
346 Elf_Internal_Shdr *symtab_hdr;
347 struct elf_link_hash_entry **sym_hashes;
348 Elf_Internal_Rela *rel, *relend;
350 if (info->relocateable)
351 return true;
353 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
354 sym_hashes = elf_sym_hashes (input_bfd);
356 rel = relocs;
357 relend = relocs + input_section->reloc_count;
358 for (; rel < relend; rel++)
360 int r_type;
361 reloc_howto_type *howto;
362 unsigned long r_symndx;
363 Elf_Internal_Sym *sym;
364 asection *sec;
365 struct elf_link_hash_entry *h;
366 bfd_vma relocation;
367 bfd_reloc_status_type r;
369 r_symndx = ELF32_R_SYM (rel->r_info);
370 r_type = ELF32_R_TYPE (rel->r_info);
371 howto = elf_mn10200_howto_table + r_type;
373 h = NULL;
374 sym = NULL;
375 sec = NULL;
376 if (r_symndx < symtab_hdr->sh_info)
378 sym = local_syms + r_symndx;
379 sec = local_sections[r_symndx];
380 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
382 else
384 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
385 while (h->root.type == bfd_link_hash_indirect
386 || h->root.type == bfd_link_hash_warning)
387 h = (struct elf_link_hash_entry *) h->root.u.i.link;
388 if (h->root.type == bfd_link_hash_defined
389 || h->root.type == bfd_link_hash_defweak)
391 sec = h->root.u.def.section;
392 relocation = (h->root.u.def.value
393 + sec->output_section->vma
394 + sec->output_offset);
396 else if (h->root.type == bfd_link_hash_undefweak)
397 relocation = 0;
398 else
400 if (! ((*info->callbacks->undefined_symbol)
401 (info, h->root.root.string, input_bfd,
402 input_section, rel->r_offset, true)))
403 return false;
404 relocation = 0;
408 r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
409 input_section,
410 contents, rel->r_offset,
411 relocation, rel->r_addend,
412 info, sec, h == NULL);
414 if (r != bfd_reloc_ok)
416 const char *name;
417 const char *msg = (const char *) 0;
419 if (h != NULL)
420 name = h->root.root.string;
421 else
423 name = (bfd_elf_string_from_elf_section
424 (input_bfd, symtab_hdr->sh_link, sym->st_name));
425 if (name == NULL || *name == '\0')
426 name = bfd_section_name (input_bfd, sec);
429 switch (r)
431 case bfd_reloc_overflow:
432 if (! ((*info->callbacks->reloc_overflow)
433 (info, name, howto->name, (bfd_vma) 0,
434 input_bfd, input_section, rel->r_offset)))
435 return false;
436 break;
438 case bfd_reloc_undefined:
439 if (! ((*info->callbacks->undefined_symbol)
440 (info, name, input_bfd, input_section,
441 rel->r_offset, true)))
442 return false;
443 break;
445 case bfd_reloc_outofrange:
446 msg = _("internal error: out of range error");
447 goto common_error;
449 case bfd_reloc_notsupported:
450 msg = _("internal error: unsupported relocation error");
451 goto common_error;
453 case bfd_reloc_dangerous:
454 msg = _("internal error: dangerous error");
455 goto common_error;
457 default:
458 msg = _("internal error: unknown error");
459 /* fall through */
461 common_error:
462 if (!((*info->callbacks->warning)
463 (info, msg, name, input_bfd, input_section,
464 rel->r_offset)))
465 return false;
466 break;
471 return true;
474 /* This function handles relaxing for the mn10200.
476 There's quite a few relaxing opportunites available on the mn10200:
478 * jsr:24 -> jsr:16 2 bytes
480 * jmp:24 -> jmp:16 2 bytes
481 * jmp:16 -> bra:8 1 byte
483 * If the previous instruction is a conditional branch
484 around the jump/bra, we may be able to reverse its condition
485 and change its target to the jump's target. The jump/bra
486 can then be deleted. 2 bytes
488 * mov abs24 -> mov abs16 2 byte savings
490 * Most instructions which accept imm24 can relax to imm16 2 bytes
491 - Most instructions which accept imm16 can relax to imm8 1 byte
493 * Most instructions which accept d24 can relax to d16 2 bytes
494 - Most instructions which accept d16 can relax to d8 1 byte
496 abs24, imm24, d24 all look the same at the reloc level. It
497 might make the code simpler if we had different relocs for
498 the various relaxable operand types.
500 We don't handle imm16->imm8 or d16->d8 as they're very rare
501 and somewhat more difficult to support. */
503 static boolean
504 mn10200_elf_relax_section (abfd, sec, link_info, again)
505 bfd *abfd;
506 asection *sec;
507 struct bfd_link_info *link_info;
508 boolean *again;
510 Elf_Internal_Shdr *symtab_hdr;
511 Elf_Internal_Shdr *shndx_hdr;
512 Elf_Internal_Rela *internal_relocs;
513 Elf_Internal_Rela *free_relocs = NULL;
514 Elf_Internal_Rela *irel, *irelend;
515 bfd_byte *contents = NULL;
516 bfd_byte *free_contents = NULL;
517 Elf32_External_Sym *extsyms = NULL;
518 Elf32_External_Sym *free_extsyms = NULL;
519 Elf_External_Sym_Shndx *shndx_buf = NULL;
521 /* Assume nothing changes. */
522 *again = false;
524 /* We don't have to do anything for a relocateable link, if
525 this section does not have relocs, or if this is not a
526 code section. */
527 if (link_info->relocateable
528 || (sec->flags & SEC_RELOC) == 0
529 || sec->reloc_count == 0
530 || (sec->flags & SEC_CODE) == 0)
531 return true;
533 /* If this is the first time we have been called for this section,
534 initialize the cooked size. */
535 if (sec->_cooked_size == 0)
536 sec->_cooked_size = sec->_raw_size;
538 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
539 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
541 /* Get a copy of the native relocations. */
542 internal_relocs = (_bfd_elf32_link_read_relocs
543 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
544 link_info->keep_memory));
545 if (internal_relocs == NULL)
546 goto error_return;
547 if (! link_info->keep_memory)
548 free_relocs = internal_relocs;
550 /* Walk through them looking for relaxing opportunities. */
551 irelend = internal_relocs + sec->reloc_count;
552 for (irel = internal_relocs; irel < irelend; irel++)
554 bfd_vma symval;
556 /* If this isn't something that can be relaxed, then ignore
557 this reloc. */
558 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
559 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
560 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
561 continue;
563 /* Get the section contents if we haven't done so already. */
564 if (contents == NULL)
566 /* Get cached copy if it exists. */
567 if (elf_section_data (sec)->this_hdr.contents != NULL)
568 contents = elf_section_data (sec)->this_hdr.contents;
569 else
571 /* Go get them off disk. */
572 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
573 if (contents == NULL)
574 goto error_return;
575 free_contents = contents;
577 if (! bfd_get_section_contents (abfd, sec, contents,
578 (file_ptr) 0, sec->_raw_size))
579 goto error_return;
583 /* Read this BFD's symbols if we haven't done so already. */
584 if (extsyms == NULL)
586 /* Get cached copy if it exists. */
587 if (symtab_hdr->contents != NULL)
588 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
589 else
591 /* Go get them off disk. */
592 bfd_size_type amt;
594 amt = symtab_hdr->sh_info;
595 amt *= sizeof (Elf32_External_Sym);
596 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
597 if (extsyms == NULL)
598 goto error_return;
599 free_extsyms = extsyms;
600 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
601 || bfd_bread ((PTR) extsyms, amt, abfd) != amt)
602 goto error_return;
603 symtab_hdr->contents = (bfd_byte *) extsyms;
606 if (shndx_hdr->sh_size != 0)
608 bfd_size_type amt;
610 amt = symtab_hdr->sh_info;
611 amt *= sizeof (Elf_External_Sym_Shndx);
612 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
613 if (shndx_buf == NULL)
614 goto error_return;
615 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
616 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
617 goto error_return;
618 shndx_hdr->contents = (bfd_byte *) shndx_buf;
622 /* Get the value of the symbol referred to by the reloc. */
623 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
625 /* A local symbol. */
626 Elf32_External_Sym *esym;
627 Elf_External_Sym_Shndx *shndx;
628 Elf_Internal_Sym isym;
629 asection *sym_sec;
631 esym = extsyms + ELF32_R_SYM (irel->r_info);
632 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0);
633 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
635 if (isym.st_shndx == SHN_UNDEF)
636 sym_sec = bfd_und_section_ptr;
637 else if (isym.st_shndx == SHN_ABS)
638 sym_sec = bfd_abs_section_ptr;
639 else if (isym.st_shndx == SHN_COMMON)
640 sym_sec = bfd_com_section_ptr;
641 else
642 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
643 symval = (isym.st_value
644 + sym_sec->output_section->vma
645 + sym_sec->output_offset);
647 else
649 unsigned long indx;
650 struct elf_link_hash_entry *h;
652 /* An external symbol. */
653 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
654 h = elf_sym_hashes (abfd)[indx];
655 BFD_ASSERT (h != NULL);
656 if (h->root.type != bfd_link_hash_defined
657 && h->root.type != bfd_link_hash_defweak)
659 /* This appears to be a reference to an undefined
660 symbol. Just ignore it--it will be caught by the
661 regular reloc processing. */
662 continue;
665 symval = (h->root.u.def.value
666 + h->root.u.def.section->output_section->vma
667 + h->root.u.def.section->output_offset);
670 /* For simplicity of coding, we are going to modify the section
671 contents, the section relocs, and the BFD symbol table. We
672 must tell the rest of the code not to free up this
673 information. It would be possible to instead create a table
674 of changes which have to be made, as is done in coff-mips.c;
675 that would be more work, but would require less memory when
676 the linker is run. */
678 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
679 branch/call. */
680 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
682 bfd_vma value = symval;
684 /* Deal with pc-relative gunk. */
685 value -= (sec->output_section->vma + sec->output_offset);
686 value -= (irel->r_offset + 3);
687 value += irel->r_addend;
689 /* See if the value will fit in 16 bits, note the high value is
690 0x7fff + 2 as the target will be two bytes closer if we are
691 able to relax. */
692 if ((long) value < 0x8001 && (long) value > -0x8000)
694 unsigned char code;
696 /* Get the opcode. */
697 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
699 if (code != 0xe0 && code != 0xe1)
700 continue;
702 /* Note that we've changed the relocs, section contents, etc. */
703 elf_section_data (sec)->relocs = internal_relocs;
704 free_relocs = NULL;
706 elf_section_data (sec)->this_hdr.contents = contents;
707 free_contents = NULL;
709 free_extsyms = NULL;
711 /* Fix the opcode. */
712 if (code == 0xe0)
713 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
714 else if (code == 0xe1)
715 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
717 /* Fix the relocation's type. */
718 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
719 R_MN10200_PCREL16);
721 /* The opcode got shorter too, so we have to fix the offset. */
722 irel->r_offset -= 1;
724 /* Delete two bytes of data. */
725 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
726 irel->r_offset + 1, 2))
727 goto error_return;
729 /* That will change things, so, we should relax again.
730 Note that this is not required, and it may be slow. */
731 *again = true;
735 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
736 branch. */
737 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
739 bfd_vma value = symval;
741 /* Deal with pc-relative gunk. */
742 value -= (sec->output_section->vma + sec->output_offset);
743 value -= (irel->r_offset + 2);
744 value += irel->r_addend;
746 /* See if the value will fit in 8 bits, note the high value is
747 0x7f + 1 as the target will be one bytes closer if we are
748 able to relax. */
749 if ((long) value < 0x80 && (long) value > -0x80)
751 unsigned char code;
753 /* Get the opcode. */
754 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
756 if (code != 0xfc)
757 continue;
759 /* Note that we've changed the relocs, section contents, etc. */
760 elf_section_data (sec)->relocs = internal_relocs;
761 free_relocs = NULL;
763 elf_section_data (sec)->this_hdr.contents = contents;
764 free_contents = NULL;
766 free_extsyms = NULL;
768 /* Fix the opcode. */
769 bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
771 /* Fix the relocation's type. */
772 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
773 R_MN10200_PCREL8);
775 /* Delete one byte of data. */
776 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
777 irel->r_offset + 1, 1))
778 goto error_return;
780 /* That will change things, so, we should relax again.
781 Note that this is not required, and it may be slow. */
782 *again = true;
786 /* Try to eliminate an unconditional 8 bit pc-relative branch
787 which immediately follows a conditional 8 bit pc-relative
788 branch around the unconditional branch.
790 original: new:
791 bCC lab1 bCC' lab2
792 bra lab2
793 lab1: lab1:
795 This happens when the bCC can't reach lab2 at assembly time,
796 but due to other relaxations it can reach at link time. */
797 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
799 Elf_Internal_Rela *nrel;
800 bfd_vma value = symval;
801 unsigned char code;
803 /* Deal with pc-relative gunk. */
804 value -= (sec->output_section->vma + sec->output_offset);
805 value -= (irel->r_offset + 1);
806 value += irel->r_addend;
808 /* Do nothing if this reloc is the last byte in the section. */
809 if (irel->r_offset == sec->_cooked_size)
810 continue;
812 /* See if the next instruction is an unconditional pc-relative
813 branch, more often than not this test will fail, so we
814 test it first to speed things up. */
815 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
816 if (code != 0xea)
817 continue;
819 /* Also make sure the next relocation applies to the next
820 instruction and that it's a pc-relative 8 bit branch. */
821 nrel = irel + 1;
822 if (nrel == irelend
823 || irel->r_offset + 2 != nrel->r_offset
824 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
825 continue;
827 /* Make sure our destination immediately follows the
828 unconditional branch. */
829 if (symval != (sec->output_section->vma + sec->output_offset
830 + irel->r_offset + 3))
831 continue;
833 /* Now make sure we are a conditional branch. This may not
834 be necessary, but why take the chance.
836 Note these checks assume that R_MN10200_PCREL8 relocs
837 only occur on bCC and bCCx insns. If they occured
838 elsewhere, we'd need to know the start of this insn
839 for this check to be accurate. */
840 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
841 if (code != 0xe0 && code != 0xe1 && code != 0xe2
842 && code != 0xe3 && code != 0xe4 && code != 0xe5
843 && code != 0xe6 && code != 0xe7 && code != 0xe8
844 && code != 0xe9 && code != 0xec && code != 0xed
845 && code != 0xee && code != 0xef && code != 0xfc
846 && code != 0xfd && code != 0xfe && code != 0xff)
847 continue;
849 /* We also have to be sure there is no symbol/label
850 at the unconditional branch. */
851 if (mn10200_elf_symbol_address_p (abfd, sec, irel->r_offset + 1))
852 continue;
854 /* Note that we've changed the relocs, section contents, etc. */
855 elf_section_data (sec)->relocs = internal_relocs;
856 free_relocs = NULL;
858 elf_section_data (sec)->this_hdr.contents = contents;
859 free_contents = NULL;
861 free_extsyms = NULL;
863 /* Reverse the condition of the first branch. */
864 switch (code)
866 case 0xfc:
867 code = 0xfd;
868 break;
869 case 0xfd:
870 code = 0xfc;
871 break;
872 case 0xfe:
873 code = 0xff;
874 break;
875 case 0xff:
876 code = 0xfe;
877 break;
878 case 0xe8:
879 code = 0xe9;
880 break;
881 case 0xe9:
882 code = 0xe8;
883 break;
884 case 0xe0:
885 code = 0xe2;
886 break;
887 case 0xe2:
888 code = 0xe0;
889 break;
890 case 0xe3:
891 code = 0xe1;
892 break;
893 case 0xe1:
894 code = 0xe3;
895 break;
896 case 0xe4:
897 code = 0xe6;
898 break;
899 case 0xe6:
900 code = 0xe4;
901 break;
902 case 0xe7:
903 code = 0xe5;
904 break;
905 case 0xe5:
906 code = 0xe7;
907 break;
908 case 0xec:
909 code = 0xed;
910 break;
911 case 0xed:
912 code = 0xec;
913 break;
914 case 0xee:
915 code = 0xef;
916 break;
917 case 0xef:
918 code = 0xee;
919 break;
921 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
923 /* Set the reloc type and symbol for the first branch
924 from the second branch. */
925 irel->r_info = nrel->r_info;
927 /* Make the reloc for the second branch a null reloc. */
928 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
929 R_MN10200_NONE);
931 /* Delete two bytes of data. */
932 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
933 irel->r_offset + 1, 2))
934 goto error_return;
936 /* That will change things, so, we should relax again.
937 Note that this is not required, and it may be slow. */
938 *again = true;
941 /* Try to turn a 24bit immediate, displacement or absolute address
942 into a 16bit immediate, displacement or absolute address. */
943 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
945 bfd_vma value = symval;
947 /* See if the value will fit in 16 bits.
948 We allow any 16bit match here. We prune those we can't
949 handle below. */
950 if ((long) value < 0x7fff && (long) value > -0x8000)
952 unsigned char code;
954 /* All insns which have 24bit operands are 5 bytes long,
955 the first byte will always be 0xf4, but we double check
956 it just in case. */
958 /* Get the first opcode. */
959 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
961 if (code != 0xf4)
962 continue;
964 /* Get the second opcode. */
965 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
967 switch (code & 0xfc)
969 /* mov imm24,dn -> mov imm16,dn */
970 case 0x70:
971 /* Not safe if the high bit is on as relaxing may
972 move the value out of high mem and thus not fit
973 in a signed 16bit value. */
974 if (value & 0x8000)
975 continue;
977 /* Note that we've changed the reldection contents, etc. */
978 elf_section_data (sec)->relocs = internal_relocs;
979 free_relocs = NULL;
981 elf_section_data (sec)->this_hdr.contents = contents;
982 free_contents = NULL;
984 free_extsyms = NULL;
986 /* Fix the opcode. */
987 bfd_put_8 (abfd, 0xf8 + (code & 0x03),
988 contents + irel->r_offset - 2);
990 /* Fix the relocation's type. */
991 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
992 R_MN10200_16);
994 /* The opcode got shorter too, so we have to fix the
995 offset. */
996 irel->r_offset -= 1;
998 /* Delete two bytes of data. */
999 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1000 irel->r_offset + 1, 2))
1001 goto error_return;
1003 /* That will change things, so, we should relax again.
1004 Note that this is not required, and it may be slow. */
1005 *again = true;
1006 break;
1008 /* mov imm24,an -> mov imm16,an
1009 cmp imm24,an -> cmp imm16,an
1010 mov (abs24),dn -> mov (abs16),dn
1011 mov dn,(abs24) -> mov dn,(abs16)
1012 movb dn,(abs24) -> movb dn,(abs16)
1013 movbu (abs24),dn -> movbu (abs16),dn */
1014 case 0x74:
1015 case 0x7c:
1016 case 0xc0:
1017 case 0x40:
1018 case 0x44:
1019 case 0xc8:
1020 /* Note that we've changed the reldection contents, etc. */
1021 elf_section_data (sec)->relocs = internal_relocs;
1022 free_relocs = NULL;
1024 elf_section_data (sec)->this_hdr.contents = contents;
1025 free_contents = NULL;
1027 free_extsyms = NULL;
1029 if ((code & 0xfc) == 0x74)
1030 code = 0xdc + (code & 0x03);
1031 else if ((code & 0xfc) == 0x7c)
1032 code = 0xec + (code & 0x03);
1033 else if ((code & 0xfc) == 0xc0)
1034 code = 0xc8 + (code & 0x03);
1035 else if ((code & 0xfc) == 0x40)
1036 code = 0xc0 + (code & 0x03);
1037 else if ((code & 0xfc) == 0x44)
1038 code = 0xc4 + (code & 0x03);
1039 else if ((code & 0xfc) == 0xc8)
1040 code = 0xcc + (code & 0x03);
1042 /* Fix the opcode. */
1043 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1045 /* Fix the relocation's type. */
1046 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1047 R_MN10200_16);
1049 /* The opcode got shorter too, so we have to fix the
1050 offset. */
1051 irel->r_offset -= 1;
1053 /* Delete two bytes of data. */
1054 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1055 irel->r_offset + 1, 2))
1056 goto error_return;
1058 /* That will change things, so, we should relax again.
1059 Note that this is not required, and it may be slow. */
1060 *again = true;
1061 break;
1063 /* cmp imm24,dn -> cmp imm16,dn
1064 mov (abs24),an -> mov (abs16),an
1065 mov an,(abs24) -> mov an,(abs16)
1066 add imm24,dn -> add imm16,dn
1067 add imm24,an -> add imm16,an
1068 sub imm24,dn -> sub imm16,dn
1069 sub imm24,an -> sub imm16,an
1070 And all d24->d16 in memory ops. */
1071 case 0x78:
1072 case 0xd0:
1073 case 0x50:
1074 case 0x60:
1075 case 0x64:
1076 case 0x68:
1077 case 0x6c:
1078 case 0x80:
1079 case 0xf0:
1080 case 0x00:
1081 case 0x10:
1082 case 0xb0:
1083 case 0x30:
1084 case 0xa0:
1085 case 0x20:
1086 case 0x90:
1087 /* Not safe if the high bit is on as relaxing may
1088 move the value out of high mem and thus not fit
1089 in a signed 16bit value. */
1090 if (((code & 0xfc) == 0x78
1091 || (code & 0xfc) == 0x60
1092 || (code & 0xfc) == 0x64
1093 || (code & 0xfc) == 0x68
1094 || (code & 0xfc) == 0x6c
1095 || (code & 0xfc) == 0x80
1096 || (code & 0xfc) == 0xf0
1097 || (code & 0xfc) == 0x00
1098 || (code & 0xfc) == 0x10
1099 || (code & 0xfc) == 0xb0
1100 || (code & 0xfc) == 0x30
1101 || (code & 0xfc) == 0xa0
1102 || (code & 0xfc) == 0x20
1103 || (code & 0xfc) == 0x90)
1104 && (value & 0x8000) != 0)
1105 continue;
1107 /* Note that we've changed the reldection contents, etc. */
1108 elf_section_data (sec)->relocs = internal_relocs;
1109 free_relocs = NULL;
1111 elf_section_data (sec)->this_hdr.contents = contents;
1112 free_contents = NULL;
1114 free_extsyms = NULL;
1116 /* Fix the opcode. */
1117 bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
1119 if ((code & 0xfc) == 0x78)
1120 code = 0x48 + (code & 0x03);
1121 else if ((code & 0xfc) == 0xd0)
1122 code = 0x30 + (code & 0x03);
1123 else if ((code & 0xfc) == 0x50)
1124 code = 0x20 + (code & 0x03);
1125 else if ((code & 0xfc) == 0x60)
1126 code = 0x18 + (code & 0x03);
1127 else if ((code & 0xfc) == 0x64)
1128 code = 0x08 + (code & 0x03);
1129 else if ((code & 0xfc) == 0x68)
1130 code = 0x1c + (code & 0x03);
1131 else if ((code & 0xfc) == 0x6c)
1132 code = 0x0c + (code & 0x03);
1133 else if ((code & 0xfc) == 0x80)
1134 code = 0xc0 + (code & 0x07);
1135 else if ((code & 0xfc) == 0xf0)
1136 code = 0xb0 + (code & 0x07);
1137 else if ((code & 0xfc) == 0x00)
1138 code = 0x80 + (code & 0x07);
1139 else if ((code & 0xfc) == 0x10)
1140 code = 0xa0 + (code & 0x07);
1141 else if ((code & 0xfc) == 0xb0)
1142 code = 0x70 + (code & 0x07);
1143 else if ((code & 0xfc) == 0x30)
1144 code = 0x60 + (code & 0x07);
1145 else if ((code & 0xfc) == 0xa0)
1146 code = 0xd0 + (code & 0x07);
1147 else if ((code & 0xfc) == 0x20)
1148 code = 0x90 + (code & 0x07);
1149 else if ((code & 0xfc) == 0x90)
1150 code = 0x50 + (code & 0x07);
1152 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1154 /* Fix the relocation's type. */
1155 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1156 R_MN10200_16);
1158 /* Delete one bytes of data. */
1159 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1160 irel->r_offset + 2, 1))
1161 goto error_return;
1163 /* That will change things, so, we should relax again.
1164 Note that this is not required, and it may be slow. */
1165 *again = true;
1166 break;
1168 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1169 case 0xc4:
1170 /* Note that we've changed the reldection contents, etc. */
1171 elf_section_data (sec)->relocs = internal_relocs;
1172 free_relocs = NULL;
1174 elf_section_data (sec)->this_hdr.contents = contents;
1175 free_contents = NULL;
1177 free_extsyms = NULL;
1179 bfd_put_8 (abfd, 0xcc + (code & 0x03),
1180 contents + irel->r_offset - 2);
1182 bfd_put_8 (abfd, 0xb8 + (code & 0x03),
1183 contents + irel->r_offset - 1);
1185 /* Fix the relocation's type. */
1186 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1187 R_MN10200_16);
1189 /* The reloc will be applied one byte in front of its
1190 current location. */
1191 irel->r_offset -= 1;
1193 /* Delete one bytes of data. */
1194 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1195 irel->r_offset + 2, 1))
1196 goto error_return;
1198 /* That will change things, so, we should relax again.
1199 Note that this is not required, and it may be slow. */
1200 *again = true;
1201 break;
1207 if (free_relocs != NULL)
1208 free (free_relocs);
1210 if (free_contents != NULL)
1212 if (! link_info->keep_memory)
1213 free (free_contents);
1214 else
1216 /* Cache the section contents for elf_link_input_bfd. */
1217 elf_section_data (sec)->this_hdr.contents = contents;
1221 if (shndx_buf != NULL)
1223 shndx_hdr->contents = NULL;
1224 free (shndx_buf);
1227 if (free_extsyms != NULL)
1229 if (! link_info->keep_memory)
1231 symtab_hdr->contents = NULL;
1232 free (free_extsyms);
1236 return true;
1238 error_return:
1239 if (free_relocs != NULL)
1240 free (free_relocs);
1241 if (free_contents != NULL)
1242 free (free_contents);
1243 if (shndx_buf != NULL)
1245 shndx_hdr->contents = NULL;
1246 free (shndx_buf);
1248 if (free_extsyms != NULL)
1250 symtab_hdr->contents = NULL;
1251 free (free_extsyms);
1254 return false;
1257 /* Delete some bytes from a section while relaxing. */
1259 static boolean
1260 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
1261 bfd *abfd;
1262 asection *sec;
1263 bfd_vma addr;
1264 int count;
1266 Elf_Internal_Shdr *symtab_hdr;
1267 Elf_Internal_Shdr *shndx_hdr;
1268 Elf32_External_Sym *extsyms;
1269 unsigned int sec_shndx;
1270 bfd_byte *contents;
1271 Elf_Internal_Rela *irel, *irelend;
1272 Elf_Internal_Rela *irelalign;
1273 bfd_vma toaddr;
1274 Elf32_External_Sym *esym, *esymend;
1275 Elf_External_Sym_Shndx *shndx;
1276 struct elf_link_hash_entry **sym_hashes;
1277 struct elf_link_hash_entry **end_hashes;
1278 unsigned int symcount;
1280 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1281 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1283 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1285 contents = elf_section_data (sec)->this_hdr.contents;
1287 /* The deletion must stop at the next ALIGN reloc for an aligment
1288 power larger than the number of bytes we are deleting. */
1290 irelalign = NULL;
1291 toaddr = sec->_cooked_size;
1293 irel = elf_section_data (sec)->relocs;
1294 irelend = irel + sec->reloc_count;
1296 /* Actually delete the bytes. */
1297 memmove (contents + addr, contents + addr + count,
1298 (size_t) (toaddr - addr - count));
1299 sec->_cooked_size -= count;
1301 /* Adjust all the relocs. */
1302 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1304 /* Get the new reloc address. */
1305 if ((irel->r_offset > addr
1306 && irel->r_offset < toaddr))
1307 irel->r_offset -= count;
1310 /* Adjust the local symbols defined in this section. */
1311 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1312 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1313 esym = extsyms;
1314 esymend = esym + symtab_hdr->sh_info;
1315 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
1317 Elf_Internal_Sym isym;
1318 Elf_External_Sym_Shndx dummy;
1320 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
1322 if (isym.st_shndx == sec_shndx
1323 && isym.st_value > addr
1324 && isym.st_value < toaddr)
1326 isym.st_value -= count;
1327 bfd_elf32_swap_symbol_out (abfd, &isym, (PTR) esym, (PTR) &dummy);
1331 /* Now adjust the global symbols defined in this section. */
1332 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1333 - symtab_hdr->sh_info);
1334 sym_hashes = elf_sym_hashes (abfd);
1335 end_hashes = sym_hashes + symcount;
1336 for (; sym_hashes < end_hashes; sym_hashes++)
1338 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1339 if ((sym_hash->root.type == bfd_link_hash_defined
1340 || sym_hash->root.type == bfd_link_hash_defweak)
1341 && sym_hash->root.u.def.section == sec
1342 && sym_hash->root.u.def.value > addr
1343 && sym_hash->root.u.def.value < toaddr)
1345 sym_hash->root.u.def.value -= count;
1349 return true;
1352 /* Return true if a symbol exists at the given address, else return
1353 false. */
1354 static boolean
1355 mn10200_elf_symbol_address_p (abfd, sec, addr)
1356 bfd *abfd;
1357 asection *sec;
1358 bfd_vma addr;
1360 Elf_Internal_Shdr *symtab_hdr;
1361 Elf_Internal_Shdr *shndx_hdr;
1362 unsigned int sec_shndx;
1363 Elf32_External_Sym *esym, *esymend;
1364 Elf_External_Sym_Shndx *shndx;
1365 struct elf_link_hash_entry **sym_hashes;
1366 struct elf_link_hash_entry **end_hashes;
1367 unsigned int symcount;
1369 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1371 /* Examine all the symbols. */
1372 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1373 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1374 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1375 esym = (Elf32_External_Sym *) symtab_hdr->contents;
1376 esymend = esym + symtab_hdr->sh_info;
1377 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
1379 Elf_Internal_Sym isym;
1381 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
1383 if (isym.st_shndx == sec_shndx
1384 && isym.st_value == addr)
1385 return true;
1388 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1389 - symtab_hdr->sh_info);
1390 sym_hashes = elf_sym_hashes (abfd);
1391 end_hashes = sym_hashes + symcount;
1392 for (; sym_hashes < end_hashes; sym_hashes++)
1394 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1395 if ((sym_hash->root.type == bfd_link_hash_defined
1396 || sym_hash->root.type == bfd_link_hash_defweak)
1397 && sym_hash->root.u.def.section == sec
1398 && sym_hash->root.u.def.value == addr)
1399 return true;
1402 return false;
1405 /* This is a version of bfd_generic_get_relocated_section_contents
1406 which uses mn10200_elf_relocate_section. */
1408 static bfd_byte *
1409 mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1410 data, relocateable, symbols)
1411 bfd *output_bfd;
1412 struct bfd_link_info *link_info;
1413 struct bfd_link_order *link_order;
1414 bfd_byte *data;
1415 boolean relocateable;
1416 asymbol **symbols;
1418 Elf_Internal_Shdr *symtab_hdr;
1419 Elf_Internal_Shdr *shndx_hdr;
1420 asection *input_section = link_order->u.indirect.section;
1421 bfd *input_bfd = input_section->owner;
1422 asection **sections = NULL;
1423 Elf_Internal_Rela *internal_relocs = NULL;
1424 Elf32_External_Sym *external_syms = NULL;
1425 Elf_External_Sym_Shndx *shndx_buf = NULL;
1426 Elf_External_Sym_Shndx *shndx;
1427 Elf_Internal_Sym *internal_syms = NULL;
1429 /* We only need to handle the case of relaxing, or of having a
1430 particular set of section contents, specially. */
1431 if (relocateable
1432 || elf_section_data (input_section)->this_hdr.contents == NULL)
1433 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1434 link_order, data,
1435 relocateable,
1436 symbols);
1438 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1439 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
1441 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1442 (size_t) input_section->_raw_size);
1444 if ((input_section->flags & SEC_RELOC) != 0
1445 && input_section->reloc_count > 0)
1447 Elf_Internal_Sym *isymp;
1448 asection **secpp;
1449 Elf32_External_Sym *esym, *esymend;
1450 bfd_size_type amt;
1452 if (symtab_hdr->contents != NULL)
1453 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1454 else if (symtab_hdr->sh_info != 0)
1456 amt = symtab_hdr->sh_info;
1457 amt *= sizeof (Elf32_External_Sym);
1458 external_syms = (Elf32_External_Sym *) bfd_malloc (amt);
1459 if (external_syms == NULL)
1460 goto error_return;
1461 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1462 || bfd_bread ((PTR) external_syms, amt, input_bfd) != amt)
1463 goto error_return;
1466 if (symtab_hdr->sh_info != 0 && shndx_hdr->sh_size != 0)
1468 amt = symtab_hdr->sh_info;
1469 amt *= sizeof (Elf_External_Sym_Shndx);
1470 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1471 if (shndx_buf == NULL)
1472 goto error_return;
1473 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1474 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
1475 goto error_return;
1478 internal_relocs = (_bfd_elf32_link_read_relocs
1479 (input_bfd, input_section, (PTR) NULL,
1480 (Elf_Internal_Rela *) NULL, false));
1481 if (internal_relocs == NULL)
1482 goto error_return;
1484 amt = symtab_hdr->sh_info;
1485 amt *= sizeof (Elf_Internal_Sym);
1486 internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
1487 if (internal_syms == NULL && amt != 0)
1488 goto error_return;
1490 amt = symtab_hdr->sh_info;
1491 amt *= sizeof (asection *);
1492 sections = (asection **) bfd_malloc (amt);
1493 if (sections == NULL && amt != 0)
1494 goto error_return;
1496 for (isymp = internal_syms, secpp = sections, shndx = shndx_buf,
1497 esym = external_syms, esymend = esym + symtab_hdr->sh_info;
1498 esym < esymend;
1499 ++esym, ++isymp, ++secpp, shndx = (shndx ? shndx + 1 : NULL))
1501 asection *isec;
1503 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx, isymp);
1505 if (isymp->st_shndx == SHN_UNDEF)
1506 isec = bfd_und_section_ptr;
1507 else if (isymp->st_shndx == SHN_ABS)
1508 isec = bfd_abs_section_ptr;
1509 else if (isymp->st_shndx == SHN_COMMON)
1510 isec = bfd_com_section_ptr;
1511 else
1512 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1514 *secpp = isec;
1517 if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1518 input_section, data, internal_relocs,
1519 internal_syms, sections))
1520 goto error_return;
1522 if (sections != NULL)
1523 free (sections);
1524 if (internal_syms != NULL)
1525 free (internal_syms);
1526 if (shndx_buf != NULL)
1527 free (shndx_buf);
1528 if (external_syms != NULL && symtab_hdr->contents == NULL)
1529 free (external_syms);
1530 if (internal_relocs != elf_section_data (input_section)->relocs)
1531 free (internal_relocs);
1534 return data;
1536 error_return:
1537 if (internal_relocs != NULL
1538 && internal_relocs != elf_section_data (input_section)->relocs)
1539 free (internal_relocs);
1540 if (shndx_buf != NULL)
1541 free (shndx_buf);
1542 if (external_syms != NULL && symtab_hdr->contents == NULL)
1543 free (external_syms);
1544 if (internal_syms != NULL)
1545 free (internal_syms);
1546 if (sections != NULL)
1547 free (sections);
1548 return NULL;
1551 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1552 #define TARGET_LITTLE_NAME "elf32-mn10200"
1553 #define ELF_ARCH bfd_arch_mn10200
1554 #define ELF_MACHINE_CODE EM_MN10200
1555 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1556 #define ELF_MAXPAGESIZE 0x1000
1558 #define elf_backend_rela_normal 1
1559 #define elf_info_to_howto mn10200_info_to_howto
1560 #define elf_info_to_howto_rel 0
1561 #define elf_backend_relocate_section mn10200_elf_relocate_section
1562 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1563 #define bfd_elf32_bfd_get_relocated_section_contents \
1564 mn10200_elf_get_relocated_section_contents
1566 #define elf_symbol_leading_char '_'
1568 #include "elf32-target.h"