* elf.c (_bfd_elf_rela_local_sym): New.
[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 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 = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
398         }
399       else
400         {
401           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
402           while (h->root.type == bfd_link_hash_indirect
403                  || h->root.type == bfd_link_hash_warning)
404             h = (struct elf_link_hash_entry *) h->root.u.i.link;
405           if (h->root.type == bfd_link_hash_defined
406               || h->root.type == bfd_link_hash_defweak)
407             {
408               sec = h->root.u.def.section;
409               relocation = (h->root.u.def.value
410                             + sec->output_section->vma
411                             + sec->output_offset);
412             }
413           else if (h->root.type == bfd_link_hash_undefweak)
414             relocation = 0;
415           else
416             {
417               if (! ((*info->callbacks->undefined_symbol)
418                      (info, h->root.root.string, input_bfd,
419                       input_section, rel->r_offset, true)))
420                 return false;
421               relocation = 0;
422             }
423         }
424
425       r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
426                                            input_section,
427                                            contents, rel->r_offset,
428                                            relocation, rel->r_addend,
429                                            info, sec, h == NULL);
430
431       if (r != bfd_reloc_ok)
432         {
433           const char *name;
434           const char *msg = (const char *) 0;
435
436           if (h != NULL)
437             name = h->root.root.string;
438           else
439             {
440               name = (bfd_elf_string_from_elf_section
441                       (input_bfd, symtab_hdr->sh_link, sym->st_name));
442               if (name == NULL || *name == '\0')
443                 name = bfd_section_name (input_bfd, sec);
444             }
445
446           switch (r)
447             {
448             case bfd_reloc_overflow:
449               if (! ((*info->callbacks->reloc_overflow)
450                      (info, name, howto->name, (bfd_vma) 0,
451                       input_bfd, input_section, rel->r_offset)))
452                 return false;
453               break;
454
455             case bfd_reloc_undefined:
456               if (! ((*info->callbacks->undefined_symbol)
457                      (info, name, input_bfd, input_section,
458                       rel->r_offset, true)))
459                 return false;
460               break;
461
462             case bfd_reloc_outofrange:
463               msg = _("internal error: out of range error");
464               goto common_error;
465
466             case bfd_reloc_notsupported:
467               msg = _("internal error: unsupported relocation error");
468               goto common_error;
469
470             case bfd_reloc_dangerous:
471               msg = _("internal error: dangerous error");
472               goto common_error;
473
474             default:
475               msg = _("internal error: unknown error");
476               /* fall through */
477
478             common_error:
479               if (!((*info->callbacks->warning)
480                     (info, msg, name, input_bfd, input_section,
481                      rel->r_offset)))
482                 return false;
483               break;
484             }
485         }
486     }
487
488   return true;
489 }
490
491 /* This function handles relaxing for the mn10200.
492
493    There's quite a few relaxing opportunites available on the mn10200:
494
495         * jsr:24 -> jsr:16                                         2 bytes
496
497         * jmp:24 -> jmp:16                                         2 bytes
498         * jmp:16 -> bra:8                                          1 byte
499
500                 * If the previous instruction is a conditional branch
501                 around the jump/bra, we may be able to reverse its condition
502                 and change its target to the jump's target.  The jump/bra
503                 can then be deleted.                               2 bytes
504
505         * mov abs24 -> mov abs16        2 byte savings
506
507         * Most instructions which accept imm24 can relax to imm16  2 bytes
508         - Most instructions which accept imm16 can relax to imm8   1 byte
509
510         * Most instructions which accept d24 can relax to d16      2 bytes
511         - Most instructions which accept d16 can relax to d8       1 byte
512
513         abs24, imm24, d24 all look the same at the reloc level.  It
514         might make the code simpler if we had different relocs for
515         the various relaxable operand types.
516
517         We don't handle imm16->imm8 or d16->d8 as they're very rare
518         and somewhat more difficult to support.  */
519
520 static boolean
521 mn10200_elf_relax_section (abfd, sec, link_info, again)
522      bfd *abfd;
523      asection *sec;
524      struct bfd_link_info *link_info;
525      boolean *again;
526 {
527   Elf_Internal_Shdr *symtab_hdr;
528   Elf_Internal_Rela *internal_relocs;
529   Elf_Internal_Rela *free_relocs = NULL;
530   Elf_Internal_Rela *irel, *irelend;
531   bfd_byte *contents = NULL;
532   bfd_byte *free_contents = NULL;
533   Elf32_External_Sym *extsyms = NULL;
534   Elf32_External_Sym *free_extsyms = NULL;
535
536   /* Assume nothing changes.  */
537   *again = false;
538
539   /* We don't have to do anything for a relocateable link, if
540      this section does not have relocs, or if this is not a
541      code section.  */
542   if (link_info->relocateable
543       || (sec->flags & SEC_RELOC) == 0
544       || sec->reloc_count == 0
545       || (sec->flags & SEC_CODE) == 0)
546     return true;
547
548   /* If this is the first time we have been called for this section,
549      initialize the cooked size.  */
550   if (sec->_cooked_size == 0)
551     sec->_cooked_size = sec->_raw_size;
552
553   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
554
555   /* Get a copy of the native relocations.  */
556   internal_relocs = (_bfd_elf32_link_read_relocs
557                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
558                       link_info->keep_memory));
559   if (internal_relocs == NULL)
560     goto error_return;
561   if (! link_info->keep_memory)
562     free_relocs = internal_relocs;
563
564   /* Walk through them looking for relaxing opportunities.  */
565   irelend = internal_relocs + sec->reloc_count;
566   for (irel = internal_relocs; irel < irelend; irel++)
567     {
568       bfd_vma symval;
569
570       /* If this isn't something that can be relaxed, then ignore
571          this reloc.  */
572       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
573           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
574           || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
575         continue;
576
577       /* Get the section contents if we haven't done so already.  */
578       if (contents == NULL)
579         {
580           /* Get cached copy if it exists.  */
581           if (elf_section_data (sec)->this_hdr.contents != NULL)
582             contents = elf_section_data (sec)->this_hdr.contents;
583           else
584             {
585               /* Go get them off disk.  */
586               contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
587               if (contents == NULL)
588                 goto error_return;
589               free_contents = contents;
590
591               if (! bfd_get_section_contents (abfd, sec, contents,
592                                               (file_ptr) 0, sec->_raw_size))
593                 goto error_return;
594             }
595         }
596
597       /* Read this BFD's symbols if we haven't done so already.  */
598       if (extsyms == NULL)
599         {
600           /* Get cached copy if it exists.  */
601           if (symtab_hdr->contents != NULL)
602             extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
603           else
604             {
605               /* Go get them off disk.  */
606               bfd_size_type amt = symtab_hdr->sh_size;
607               extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
608               if (extsyms == NULL)
609                 goto error_return;
610               free_extsyms = extsyms;
611               if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
612                   || bfd_bread (extsyms, amt, abfd) != amt)
613                 goto error_return;
614             }
615         }
616
617       /* Get the value of the symbol referred to by the reloc.  */
618       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
619         {
620           Elf_Internal_Sym isym;
621           asection *sym_sec;
622
623           /* A local symbol.  */
624           bfd_elf32_swap_symbol_in (abfd,
625                                     extsyms + ELF32_R_SYM (irel->r_info),
626                                     &isym);
627
628           sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
629           symval = (isym.st_value
630                     + sym_sec->output_section->vma
631                     + sym_sec->output_offset);
632         }
633       else
634         {
635           unsigned long indx;
636           struct elf_link_hash_entry *h;
637
638           /* An external symbol.  */
639           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
640           h = elf_sym_hashes (abfd)[indx];
641           BFD_ASSERT (h != NULL);
642           if (h->root.type != bfd_link_hash_defined
643               && h->root.type != bfd_link_hash_defweak)
644             {
645               /* This appears to be a reference to an undefined
646                  symbol.  Just ignore it--it will be caught by the
647                  regular reloc processing.  */
648               continue;
649             }
650
651           symval = (h->root.u.def.value
652                     + h->root.u.def.section->output_section->vma
653                     + h->root.u.def.section->output_offset);
654         }
655
656       /* For simplicity of coding, we are going to modify the section
657          contents, the section relocs, and the BFD symbol table.  We
658          must tell the rest of the code not to free up this
659          information.  It would be possible to instead create a table
660          of changes which have to be made, as is done in coff-mips.c;
661          that would be more work, but would require less memory when
662          the linker is run.  */
663
664       /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
665          branch/call.  */
666       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
667         {
668           bfd_vma value = symval;
669
670           /* Deal with pc-relative gunk.  */
671           value -= (sec->output_section->vma + sec->output_offset);
672           value -= (irel->r_offset + 3);
673           value += irel->r_addend;
674
675           /* See if the value will fit in 16 bits, note the high value is
676              0x7fff + 2 as the target will be two bytes closer if we are
677              able to relax.  */
678           if ((long) value < 0x8001 && (long) value > -0x8000)
679             {
680               unsigned char code;
681
682               /* Get the opcode.  */
683               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
684
685               if (code != 0xe0 && code != 0xe1)
686                 continue;
687
688               /* Note that we've changed the relocs, section contents, etc.  */
689               elf_section_data (sec)->relocs = internal_relocs;
690               free_relocs = NULL;
691
692               elf_section_data (sec)->this_hdr.contents = contents;
693               free_contents = NULL;
694
695               symtab_hdr->contents = (bfd_byte *) extsyms;
696               free_extsyms = NULL;
697
698               /* Fix the opcode.  */
699               if (code == 0xe0)
700                 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
701               else if (code == 0xe1)
702                 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
703
704               /* Fix the relocation's type.  */
705               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
706                                            R_MN10200_PCREL16);
707
708               /* The opcode got shorter too, so we have to fix the offset.  */
709               irel->r_offset -= 1;
710
711               /* Delete two bytes of data.  */
712               if (!mn10200_elf_relax_delete_bytes (abfd, sec,
713                                                    irel->r_offset + 1, 2))
714                 goto error_return;
715
716               /* That will change things, so, we should relax again.
717                  Note that this is not required, and it may be slow.  */
718               *again = true;
719             }
720         }
721
722       /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
723          branch.  */
724       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
725         {
726           bfd_vma value = symval;
727
728           /* Deal with pc-relative gunk.  */
729           value -= (sec->output_section->vma + sec->output_offset);
730           value -= (irel->r_offset + 2);
731           value += irel->r_addend;
732
733           /* See if the value will fit in 8 bits, note the high value is
734              0x7f + 1 as the target will be one bytes closer if we are
735              able to relax.  */
736           if ((long) value < 0x80 && (long) value > -0x80)
737             {
738               unsigned char code;
739
740               /* Get the opcode.  */
741               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
742
743               if (code != 0xfc)
744                 continue;
745
746               /* Note that we've changed the relocs, section contents, etc.  */
747               elf_section_data (sec)->relocs = internal_relocs;
748               free_relocs = NULL;
749
750               elf_section_data (sec)->this_hdr.contents = contents;
751               free_contents = NULL;
752
753               symtab_hdr->contents = (bfd_byte *) extsyms;
754               free_extsyms = NULL;
755
756               /* Fix the opcode.  */
757               bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
758
759               /* Fix the relocation's type.  */
760               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
761                                            R_MN10200_PCREL8);
762
763               /* Delete one byte of data.  */
764               if (!mn10200_elf_relax_delete_bytes (abfd, sec,
765                                                    irel->r_offset + 1, 1))
766                 goto error_return;
767
768               /* That will change things, so, we should relax again.
769                  Note that this is not required, and it may be slow.  */
770               *again = true;
771             }
772         }
773
774       /* Try to eliminate an unconditional 8 bit pc-relative branch
775          which immediately follows a conditional 8 bit pc-relative
776          branch around the unconditional branch.
777
778             original:           new:
779             bCC lab1            bCC' lab2
780             bra lab2
781            lab1:               lab1:
782
783          This happens when the bCC can't reach lab2 at assembly time,
784          but due to other relaxations it can reach at link time.  */
785       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
786         {
787           Elf_Internal_Rela *nrel;
788           bfd_vma value = symval;
789           unsigned char code;
790
791           /* Deal with pc-relative gunk.  */
792           value -= (sec->output_section->vma + sec->output_offset);
793           value -= (irel->r_offset + 1);
794           value += irel->r_addend;
795
796           /* Do nothing if this reloc is the last byte in the section.  */
797           if (irel->r_offset == sec->_cooked_size)
798             continue;
799
800           /* See if the next instruction is an unconditional pc-relative
801              branch, more often than not this test will fail, so we
802              test it first to speed things up.  */
803           code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
804           if (code != 0xea)
805             continue;
806
807           /* Also make sure the next relocation applies to the next
808              instruction and that it's a pc-relative 8 bit branch.  */
809           nrel = irel + 1;
810           if (nrel == irelend
811               || irel->r_offset + 2 != nrel->r_offset
812               || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
813             continue;
814
815           /* Make sure our destination immediately follows the
816              unconditional branch.  */
817           if (symval != (sec->output_section->vma + sec->output_offset
818                          + irel->r_offset + 3))
819             continue;
820
821           /* Now make sure we are a conditional branch.  This may not
822              be necessary, but why take the chance.
823
824              Note these checks assume that R_MN10200_PCREL8 relocs
825              only occur on bCC and bCCx insns.  If they occured
826              elsewhere, we'd need to know the start of this insn
827              for this check to be accurate.  */
828           code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
829           if (code != 0xe0 && code != 0xe1 && code != 0xe2
830               && code != 0xe3 && code != 0xe4 && code != 0xe5
831               && code != 0xe6 && code != 0xe7 && code != 0xe8
832               && code != 0xe9 && code != 0xec && code != 0xed
833               && code != 0xee && code != 0xef && code != 0xfc
834               && code != 0xfd && code != 0xfe && code != 0xff)
835             continue;
836
837           /* We also have to be sure there is no symbol/label
838              at the unconditional branch.  */
839           if (mn10200_elf_symbol_address_p (abfd, sec, extsyms,
840                                             irel->r_offset + 1))
841             continue;
842
843           /* Note that we've changed the relocs, section contents, etc.  */
844           elf_section_data (sec)->relocs = internal_relocs;
845           free_relocs = NULL;
846
847           elf_section_data (sec)->this_hdr.contents = contents;
848           free_contents = NULL;
849
850           symtab_hdr->contents = (bfd_byte *) extsyms;
851           free_extsyms = NULL;
852
853           /* Reverse the condition of the first branch.  */
854           switch (code)
855             {
856             case 0xfc:
857               code = 0xfd;
858               break;
859             case 0xfd:
860               code = 0xfc;
861               break;
862             case 0xfe:
863               code = 0xff;
864               break;
865             case 0xff:
866               code = 0xfe;
867               break;
868             case 0xe8:
869               code = 0xe9;
870               break;
871             case 0xe9:
872               code = 0xe8;
873               break;
874             case 0xe0:
875               code = 0xe2;
876               break;
877             case 0xe2:
878               code = 0xe0;
879               break;
880             case 0xe3:
881               code = 0xe1;
882               break;
883             case 0xe1:
884               code = 0xe3;
885               break;
886             case 0xe4:
887               code = 0xe6;
888               break;
889             case 0xe6:
890               code = 0xe4;
891               break;
892             case 0xe7:
893               code = 0xe5;
894               break;
895             case 0xe5:
896               code = 0xe7;
897               break;
898             case 0xec:
899               code = 0xed;
900               break;
901             case 0xed:
902               code = 0xec;
903               break;
904             case 0xee:
905               code = 0xef;
906               break;
907             case 0xef:
908               code = 0xee;
909               break;
910             }
911           bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
912
913           /* Set the reloc type and symbol for the first branch
914              from the second branch.  */
915           irel->r_info = nrel->r_info;
916
917           /* Make the reloc for the second branch a null reloc.  */
918           nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
919                                        R_MN10200_NONE);
920
921           /* Delete two bytes of data.  */
922           if (!mn10200_elf_relax_delete_bytes (abfd, sec,
923                                                irel->r_offset + 1, 2))
924             goto error_return;
925
926           /* That will change things, so, we should relax again.
927              Note that this is not required, and it may be slow.  */
928           *again = true;
929         }
930
931       /* Try to turn a 24bit immediate, displacement or absolute address
932          into a 16bit immediate, displacement or absolute address.  */
933       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
934         {
935           bfd_vma value = symval;
936
937           /* See if the value will fit in 16 bits.
938              We allow any 16bit match here.  We prune those we can't
939              handle below.  */
940           if ((long) value < 0x7fff && (long) value > -0x8000)
941             {
942               unsigned char code;
943
944               /* All insns which have 24bit operands are 5 bytes long,
945                  the first byte will always be 0xf4, but we double check
946                  it just in case.  */
947
948               /* Get the first opcode.  */
949               code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
950
951               if (code != 0xf4)
952                 continue;
953
954               /* Get the second opcode.  */
955               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
956
957               switch (code & 0xfc)
958                 {
959                 /* mov imm24,dn -> mov imm16,dn */
960                 case 0x70:
961                   /* Not safe if the high bit is on as relaxing may
962                      move the value out of high mem and thus not fit
963                      in a signed 16bit value.  */
964                   if (value & 0x8000)
965                     continue;
966
967                   /* Note that we've changed the reldection contents, etc.  */
968                   elf_section_data (sec)->relocs = internal_relocs;
969                   free_relocs = NULL;
970
971                   elf_section_data (sec)->this_hdr.contents = contents;
972                   free_contents = NULL;
973
974                   symtab_hdr->contents = (bfd_byte *) extsyms;
975                   free_extsyms = NULL;
976
977                   /* Fix the opcode.  */
978                   bfd_put_8 (abfd, 0xf8 + (code & 0x03),
979                              contents + irel->r_offset - 2);
980
981                   /* Fix the relocation's type.  */
982                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
983                                                R_MN10200_16);
984
985                   /* The opcode got shorter too, so we have to fix the
986                      offset.  */
987                   irel->r_offset -= 1;
988
989                   /* Delete two bytes of data.  */
990                   if (!mn10200_elf_relax_delete_bytes (abfd, sec,
991                                                        irel->r_offset + 1, 2))
992                     goto error_return;
993
994                   /* That will change things, so, we should relax again.
995                      Note that this is not required, and it may be slow.  */
996                   *again = true;
997                   break;
998
999                 /* mov imm24,an -> mov imm16,an
1000                    cmp imm24,an -> cmp imm16,an
1001                    mov (abs24),dn -> mov (abs16),dn
1002                    mov dn,(abs24) -> mov dn,(abs16)
1003                    movb dn,(abs24) -> movb dn,(abs16)
1004                    movbu (abs24),dn -> movbu (abs16),dn */
1005                 case 0x74:
1006                 case 0x7c:
1007                 case 0xc0:
1008                 case 0x40:
1009                 case 0x44:
1010                 case 0xc8:
1011                   /* Note that we've changed the reldection contents, etc.  */
1012                   elf_section_data (sec)->relocs = internal_relocs;
1013                   free_relocs = NULL;
1014
1015                   elf_section_data (sec)->this_hdr.contents = contents;
1016                   free_contents = NULL;
1017
1018                   symtab_hdr->contents = (bfd_byte *) extsyms;
1019                   free_extsyms = NULL;
1020
1021                   if ((code & 0xfc) == 0x74)
1022                     code = 0xdc + (code & 0x03);
1023                   else if ((code & 0xfc) == 0x7c)
1024                     code = 0xec + (code & 0x03);
1025                   else if ((code & 0xfc) == 0xc0)
1026                     code = 0xc8 + (code & 0x03);
1027                   else if ((code & 0xfc) == 0x40)
1028                     code = 0xc0 + (code & 0x03);
1029                   else if ((code & 0xfc) == 0x44)
1030                     code = 0xc4 + (code & 0x03);
1031                   else if ((code & 0xfc) == 0xc8)
1032                     code = 0xcc + (code & 0x03);
1033
1034                   /* Fix the opcode.  */
1035                   bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1036
1037                   /* Fix the relocation's type.  */
1038                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1039                                                R_MN10200_16);
1040
1041                   /* The opcode got shorter too, so we have to fix the
1042                      offset.  */
1043                   irel->r_offset -= 1;
1044
1045                   /* Delete two bytes of data.  */
1046                   if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1047                                                        irel->r_offset + 1, 2))
1048                     goto error_return;
1049
1050                   /* That will change things, so, we should relax again.
1051                      Note that this is not required, and it may be slow.  */
1052                   *again = true;
1053                   break;
1054
1055                 /* cmp imm24,dn -> cmp imm16,dn
1056                    mov (abs24),an -> mov (abs16),an
1057                    mov an,(abs24) -> mov an,(abs16)
1058                    add imm24,dn -> add imm16,dn
1059                    add imm24,an -> add imm16,an
1060                    sub imm24,dn -> sub imm16,dn
1061                    sub imm24,an -> sub imm16,an
1062                    And all d24->d16 in memory ops.  */
1063                 case 0x78:
1064                 case 0xd0:
1065                 case 0x50:
1066                 case 0x60:
1067                 case 0x64:
1068                 case 0x68:
1069                 case 0x6c:
1070                 case 0x80:
1071                 case 0xf0:
1072                 case 0x00:
1073                 case 0x10:
1074                 case 0xb0:
1075                 case 0x30:
1076                 case 0xa0:
1077                 case 0x20:
1078                 case 0x90:
1079                   /* Not safe if the high bit is on as relaxing may
1080                      move the value out of high mem and thus not fit
1081                      in a signed 16bit value.  */
1082                   if (((code & 0xfc) == 0x78
1083                        || (code & 0xfc) == 0x60
1084                        || (code & 0xfc) == 0x64
1085                        || (code & 0xfc) == 0x68
1086                        || (code & 0xfc) == 0x6c
1087                        || (code & 0xfc) == 0x80
1088                        || (code & 0xfc) == 0xf0
1089                        || (code & 0xfc) == 0x00
1090                        || (code & 0xfc) == 0x10
1091                        || (code & 0xfc) == 0xb0
1092                        || (code & 0xfc) == 0x30
1093                        || (code & 0xfc) == 0xa0
1094                        || (code & 0xfc) == 0x20
1095                        || (code & 0xfc) == 0x90)
1096                       && (value & 0x8000) != 0)
1097                     continue;
1098
1099                   /* Note that we've changed the reldection contents, etc.  */
1100                   elf_section_data (sec)->relocs = internal_relocs;
1101                   free_relocs = NULL;
1102
1103                   elf_section_data (sec)->this_hdr.contents = contents;
1104                   free_contents = NULL;
1105
1106                   symtab_hdr->contents = (bfd_byte *) extsyms;
1107                   free_extsyms = NULL;
1108
1109                   /* Fix the opcode.  */
1110                   bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
1111
1112                   if ((code & 0xfc) == 0x78)
1113                     code = 0x48 + (code & 0x03);
1114                   else if ((code & 0xfc) == 0xd0)
1115                     code = 0x30 + (code & 0x03);
1116                   else if ((code & 0xfc) == 0x50)
1117                     code = 0x20 + (code & 0x03);
1118                   else if ((code & 0xfc) == 0x60)
1119                     code = 0x18 + (code & 0x03);
1120                   else if ((code & 0xfc) == 0x64)
1121                     code = 0x08 + (code & 0x03);
1122                   else if ((code & 0xfc) == 0x68)
1123                     code = 0x1c + (code & 0x03);
1124                   else if ((code & 0xfc) == 0x6c)
1125                     code = 0x0c + (code & 0x03);
1126                   else if ((code & 0xfc) == 0x80)
1127                     code = 0xc0 + (code & 0x07);
1128                   else if ((code & 0xfc) == 0xf0)
1129                     code = 0xb0 + (code & 0x07);
1130                   else if ((code & 0xfc) == 0x00)
1131                     code = 0x80 + (code & 0x07);
1132                   else if ((code & 0xfc) == 0x10)
1133                     code = 0xa0 + (code & 0x07);
1134                   else if ((code & 0xfc) == 0xb0)
1135                     code = 0x70 + (code & 0x07);
1136                   else if ((code & 0xfc) == 0x30)
1137                     code = 0x60 + (code & 0x07);
1138                   else if ((code & 0xfc) == 0xa0)
1139                     code = 0xd0 + (code & 0x07);
1140                   else if ((code & 0xfc) == 0x20)
1141                     code = 0x90 + (code & 0x07);
1142                   else if ((code & 0xfc) == 0x90)
1143                     code = 0x50 + (code & 0x07);
1144
1145                   bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1146
1147                   /* Fix the relocation's type.  */
1148                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1149                                                R_MN10200_16);
1150
1151                   /* Delete one bytes of data.  */
1152                   if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1153                                                        irel->r_offset + 2, 1))
1154                     goto error_return;
1155
1156                   /* That will change things, so, we should relax again.
1157                      Note that this is not required, and it may be slow.  */
1158                   *again = true;
1159                   break;
1160
1161                 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1162                 case 0xc4:
1163                   /* Note that we've changed the reldection contents, etc.  */
1164                   elf_section_data (sec)->relocs = internal_relocs;
1165                   free_relocs = NULL;
1166
1167                   elf_section_data (sec)->this_hdr.contents = contents;
1168                   free_contents = NULL;
1169
1170                   symtab_hdr->contents = (bfd_byte *) extsyms;
1171                   free_extsyms = NULL;
1172
1173                   bfd_put_8 (abfd, 0xcc + (code & 0x03),
1174                              contents + irel->r_offset - 2);
1175
1176                   bfd_put_8 (abfd, 0xb8 + (code & 0x03),
1177                              contents + irel->r_offset - 1);
1178
1179                   /* Fix the relocation's type.  */
1180                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1181                                                R_MN10200_16);
1182
1183                   /* The reloc will be applied one byte in front of its
1184                      current location.  */
1185                   irel->r_offset -= 1;
1186
1187                   /* Delete one bytes of data.  */
1188                   if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1189                                                        irel->r_offset + 2, 1))
1190                     goto error_return;
1191
1192                   /* That will change things, so, we should relax again.
1193                      Note that this is not required, and it may be slow.  */
1194                   *again = true;
1195                   break;
1196                 }
1197             }
1198         }
1199     }
1200
1201   if (free_relocs != NULL)
1202     {
1203       free (free_relocs);
1204       free_relocs = NULL;
1205     }
1206
1207   if (free_contents != NULL)
1208     {
1209       if (! link_info->keep_memory)
1210         free (free_contents);
1211       else
1212         {
1213           /* Cache the section contents for elf_link_input_bfd.  */
1214           elf_section_data (sec)->this_hdr.contents = contents;
1215         }
1216       free_contents = NULL;
1217     }
1218
1219   if (free_extsyms != NULL)
1220     {
1221       if (! link_info->keep_memory)
1222         free (free_extsyms);
1223       else
1224         {
1225           /* Cache the symbols for elf_link_input_bfd.  */
1226           symtab_hdr->contents = (unsigned char *) extsyms;
1227         }
1228       free_extsyms = NULL;
1229     }
1230
1231   return true;
1232
1233  error_return:
1234   if (free_relocs != NULL)
1235     free (free_relocs);
1236   if (free_contents != NULL)
1237     free (free_contents);
1238   if (free_extsyms != NULL)
1239     free (free_extsyms);
1240   return false;
1241 }
1242
1243 /* Delete some bytes from a section while relaxing.  */
1244
1245 static boolean
1246 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
1247      bfd *abfd;
1248      asection *sec;
1249      bfd_vma addr;
1250      int count;
1251 {
1252   Elf_Internal_Shdr *symtab_hdr;
1253   Elf32_External_Sym *extsyms;
1254   int shndx, index;
1255   bfd_byte *contents;
1256   Elf_Internal_Rela *irel, *irelend;
1257   Elf_Internal_Rela *irelalign;
1258   bfd_vma toaddr;
1259   Elf32_External_Sym *esym, *esymend;
1260   struct elf_link_hash_entry *sym_hash;
1261
1262   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1263   extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1264
1265   shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1266
1267   contents = elf_section_data (sec)->this_hdr.contents;
1268
1269   /* The deletion must stop at the next ALIGN reloc for an aligment
1270      power larger than the number of bytes we are deleting.  */
1271
1272   irelalign = NULL;
1273   toaddr = sec->_cooked_size;
1274
1275   irel = elf_section_data (sec)->relocs;
1276   irelend = irel + sec->reloc_count;
1277
1278   /* Actually delete the bytes.  */
1279   memmove (contents + addr, contents + addr + count,
1280            (size_t) (toaddr - addr - count));
1281   sec->_cooked_size -= count;
1282
1283   /* Adjust all the relocs.  */
1284   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1285     {
1286       /* Get the new reloc address.  */
1287       if ((irel->r_offset > addr
1288            && irel->r_offset < toaddr))
1289         irel->r_offset -= count;
1290     }
1291
1292   /* Adjust the local symbols defined in this section.  */
1293   esym = extsyms;
1294   esymend = esym + symtab_hdr->sh_info;
1295   for (; esym < esymend; esym++)
1296     {
1297       Elf_Internal_Sym isym;
1298
1299       bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1300
1301       if (isym.st_shndx == shndx
1302           && isym.st_value > addr
1303           && isym.st_value < toaddr)
1304         {
1305           isym.st_value -= count;
1306           bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1307         }
1308     }
1309
1310   /* Now adjust the global symbols defined in this section.  */
1311   esym = extsyms + symtab_hdr->sh_info;
1312   esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1313   for (index = 0; esym < esymend; esym++, index++)
1314     {
1315       Elf_Internal_Sym isym;
1316
1317       bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1318       sym_hash = elf_sym_hashes (abfd)[index];
1319       if (isym.st_shndx == shndx
1320           && ((sym_hash)->root.type == bfd_link_hash_defined
1321               || (sym_hash)->root.type == bfd_link_hash_defweak)
1322           && (sym_hash)->root.u.def.section == sec
1323           && (sym_hash)->root.u.def.value > addr
1324           && (sym_hash)->root.u.def.value < toaddr)
1325         {
1326           (sym_hash)->root.u.def.value -= count;
1327         }
1328     }
1329
1330   return true;
1331 }
1332
1333 /* Return true if a symbol exists at the given address, else return
1334    false.  */
1335 static boolean
1336 mn10200_elf_symbol_address_p (abfd, sec, extsyms, addr)
1337      bfd *abfd;
1338      asection *sec;
1339      Elf32_External_Sym *extsyms;
1340      bfd_vma addr;
1341 {
1342   Elf_Internal_Shdr *symtab_hdr;
1343   int shndx;
1344   Elf32_External_Sym *esym, *esymend;
1345   struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1346
1347   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1348   shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1349
1350   /* Examine all the symbols.  */
1351   esym = extsyms;
1352   esymend = esym + symtab_hdr->sh_info;
1353   for (; esym < esymend; esym++)
1354     {
1355       Elf_Internal_Sym isym;
1356
1357       bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1358
1359       if (isym.st_shndx == shndx
1360           && isym.st_value == addr)
1361         return true;
1362     }
1363
1364   sym_hash = elf_sym_hashes (abfd);
1365   sym_hash_end = (sym_hash
1366                   + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1367                      - symtab_hdr->sh_info));
1368   for (; sym_hash < sym_hash_end; sym_hash++)
1369     {
1370       if (((*sym_hash)->root.type == bfd_link_hash_defined
1371            || (*sym_hash)->root.type == bfd_link_hash_defweak)
1372           && (*sym_hash)->root.u.def.section == sec
1373           && (*sym_hash)->root.u.def.value == addr)
1374         return true;
1375     }
1376   return false;
1377 }
1378
1379 /* This is a version of bfd_generic_get_relocated_section_contents
1380    which uses mn10200_elf_relocate_section.  */
1381
1382 static bfd_byte *
1383 mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1384                                             data, relocateable, symbols)
1385      bfd *output_bfd;
1386      struct bfd_link_info *link_info;
1387      struct bfd_link_order *link_order;
1388      bfd_byte *data;
1389      boolean relocateable;
1390      asymbol **symbols;
1391 {
1392   Elf_Internal_Shdr *symtab_hdr;
1393   asection *input_section = link_order->u.indirect.section;
1394   bfd *input_bfd = input_section->owner;
1395   asection **sections = NULL;
1396   Elf_Internal_Rela *internal_relocs = NULL;
1397   Elf32_External_Sym *external_syms = NULL;
1398   Elf_Internal_Sym *internal_syms = NULL;
1399
1400   /* We only need to handle the case of relaxing, or of having a
1401      particular set of section contents, specially.  */
1402   if (relocateable
1403       || elf_section_data (input_section)->this_hdr.contents == NULL)
1404     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1405                                                        link_order, data,
1406                                                        relocateable,
1407                                                        symbols);
1408
1409   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1410
1411   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1412           (size_t) input_section->_raw_size);
1413
1414   if ((input_section->flags & SEC_RELOC) != 0
1415       && input_section->reloc_count > 0)
1416     {
1417       Elf_Internal_Sym *isymp;
1418       asection **secpp;
1419       Elf32_External_Sym *esym, *esymend;
1420       bfd_size_type size;
1421
1422       if (symtab_hdr->contents != NULL)
1423         external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1424       else
1425         {
1426           size = symtab_hdr->sh_info;
1427           size *= sizeof (Elf32_External_Sym);
1428           external_syms = (Elf32_External_Sym *) bfd_malloc (size);
1429           if (external_syms == NULL && size != 0)
1430             goto error_return;
1431           if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1432               || bfd_bread (external_syms, size, input_bfd) != size)
1433             goto error_return;
1434         }
1435
1436       internal_relocs = (_bfd_elf32_link_read_relocs
1437                          (input_bfd, input_section, (PTR) NULL,
1438                           (Elf_Internal_Rela *) NULL, false));
1439       if (internal_relocs == NULL)
1440         goto error_return;
1441
1442       size = symtab_hdr->sh_info;
1443       size *= sizeof (Elf_Internal_Sym);
1444       internal_syms = (Elf_Internal_Sym *) bfd_malloc (size);
1445       if (internal_syms == NULL && size != 0)
1446         goto error_return;
1447
1448       size = symtab_hdr->sh_info;
1449       size *= sizeof (asection *);
1450       sections = (asection **) bfd_malloc (size);
1451       if (sections == NULL && size != 0)
1452         goto error_return;
1453
1454       isymp = internal_syms;
1455       secpp = sections;
1456       esym = external_syms;
1457       esymend = esym + symtab_hdr->sh_info;
1458       for (; esym < esymend; ++esym, ++isymp, ++secpp)
1459         {
1460           asection *isec;
1461
1462           bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1463
1464           if (isymp->st_shndx == SHN_UNDEF)
1465             isec = bfd_und_section_ptr;
1466           else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1467             isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1468           else if (isymp->st_shndx == SHN_ABS)
1469             isec = bfd_abs_section_ptr;
1470           else if (isymp->st_shndx == SHN_COMMON)
1471             isec = bfd_com_section_ptr;
1472           else
1473             {
1474               /* Who knows?  */
1475               isec = NULL;
1476             }
1477
1478           *secpp = isec;
1479         }
1480
1481       if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1482                                      input_section, data, internal_relocs,
1483                                      internal_syms, sections))
1484         goto error_return;
1485
1486       if (sections != NULL)
1487         free (sections);
1488       sections = NULL;
1489       if (internal_syms != NULL)
1490         free (internal_syms);
1491       internal_syms = NULL;
1492       if (external_syms != NULL && symtab_hdr->contents == NULL)
1493         free (external_syms);
1494       external_syms = NULL;
1495       if (internal_relocs != elf_section_data (input_section)->relocs)
1496         free (internal_relocs);
1497       internal_relocs = NULL;
1498     }
1499
1500   return data;
1501
1502  error_return:
1503   if (internal_relocs != NULL
1504       && internal_relocs != elf_section_data (input_section)->relocs)
1505     free (internal_relocs);
1506   if (external_syms != NULL && symtab_hdr->contents == NULL)
1507     free (external_syms);
1508   if (internal_syms != NULL)
1509     free (internal_syms);
1510   if (sections != NULL)
1511     free (sections);
1512   return NULL;
1513 }
1514
1515 #define TARGET_LITTLE_SYM       bfd_elf32_mn10200_vec
1516 #define TARGET_LITTLE_NAME      "elf32-mn10200"
1517 #define ELF_ARCH                bfd_arch_mn10200
1518 #define ELF_MACHINE_CODE        EM_MN10200
1519 #define ELF_MACHINE_ALT1        EM_CYGNUS_MN10200
1520 #define ELF_MAXPAGESIZE         0x1000
1521
1522 #define elf_info_to_howto       mn10200_info_to_howto
1523 #define elf_info_to_howto_rel   0
1524 #define elf_backend_relocate_section mn10200_elf_relocate_section
1525 #define bfd_elf32_bfd_relax_section     mn10200_elf_relax_section
1526 #define bfd_elf32_bfd_get_relocated_section_contents \
1527                                 mn10200_elf_get_relocated_section_contents
1528
1529 #define elf_symbol_leading_char '_'
1530
1531 #include "elf32-target.h"