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