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