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