Add missing prototypes
[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 = (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               extsyms = ((Elf32_External_Sym *)
609                          bfd_malloc (symtab_hdr->sh_size));
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_read (extsyms, 1, symtab_hdr->sh_size, abfd)
615                       != symtab_hdr->sh_size))
616                 goto error_return;
617             }
618         }
619
620       /* Get the value of the symbol referred to by the reloc.  */
621       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
622         {
623           Elf_Internal_Sym isym;
624           asection *sym_sec;
625
626           /* A local symbol.  */
627           bfd_elf32_swap_symbol_in (abfd,
628                                     extsyms + ELF32_R_SYM (irel->r_info),
629                                     &isym);
630
631           sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
632           symval = (isym.st_value
633                     + sym_sec->output_section->vma
634                     + sym_sec->output_offset);
635         }
636       else
637         {
638           unsigned long indx;
639           struct elf_link_hash_entry *h;
640
641           /* An external symbol.  */
642           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
643           h = elf_sym_hashes (abfd)[indx];
644           BFD_ASSERT (h != NULL);
645           if (h->root.type != bfd_link_hash_defined
646               && h->root.type != bfd_link_hash_defweak)
647             {
648               /* This appears to be a reference to an undefined
649                  symbol.  Just ignore it--it will be caught by the
650                  regular reloc processing.  */
651               continue;
652             }
653
654           symval = (h->root.u.def.value
655                     + h->root.u.def.section->output_section->vma
656                     + h->root.u.def.section->output_offset);
657         }
658
659       /* For simplicity of coding, we are going to modify the section
660          contents, the section relocs, and the BFD symbol table.  We
661          must tell the rest of the code not to free up this
662          information.  It would be possible to instead create a table
663          of changes which have to be made, as is done in coff-mips.c;
664          that would be more work, but would require less memory when
665          the linker is run.  */
666
667       /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
668          branch/call.  */
669       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
670         {
671           bfd_vma value = symval;
672
673           /* Deal with pc-relative gunk.  */
674           value -= (sec->output_section->vma + sec->output_offset);
675           value -= (irel->r_offset + 3);
676           value += irel->r_addend;
677
678           /* See if the value will fit in 16 bits, note the high value is
679              0x7fff + 2 as the target will be two bytes closer if we are
680              able to relax.  */
681           if ((long) value < 0x8001 && (long) value > -0x8000)
682             {
683               unsigned char code;
684
685               /* Get the opcode.  */
686               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
687
688               if (code != 0xe0 && code != 0xe1)
689                 continue;
690
691               /* Note that we've changed the relocs, section contents, etc.  */
692               elf_section_data (sec)->relocs = internal_relocs;
693               free_relocs = NULL;
694
695               elf_section_data (sec)->this_hdr.contents = contents;
696               free_contents = NULL;
697
698               symtab_hdr->contents = (bfd_byte *) extsyms;
699               free_extsyms = NULL;
700
701               /* Fix the opcode.  */
702               if (code == 0xe0)
703                 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
704               else if (code == 0xe1)
705                 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
706
707               /* Fix the relocation's type.  */
708               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
709                                            R_MN10200_PCREL16);
710
711               /* The opcode got shorter too, so we have to fix the offset.  */
712               irel->r_offset -= 1;
713
714               /* Delete two bytes of data.  */
715               if (!mn10200_elf_relax_delete_bytes (abfd, sec,
716                                                    irel->r_offset + 1, 2))
717                 goto error_return;
718
719               /* That will change things, so, we should relax again.
720                  Note that this is not required, and it may be slow.  */
721               *again = true;
722             }
723         }
724
725       /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
726          branch.  */
727       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
728         {
729           bfd_vma value = symval;
730
731           /* Deal with pc-relative gunk.  */
732           value -= (sec->output_section->vma + sec->output_offset);
733           value -= (irel->r_offset + 2);
734           value += irel->r_addend;
735
736           /* See if the value will fit in 8 bits, note the high value is
737              0x7f + 1 as the target will be one bytes closer if we are
738              able to relax.  */
739           if ((long) value < 0x80 && (long) value > -0x80)
740             {
741               unsigned char code;
742
743               /* Get the opcode.  */
744               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
745
746               if (code != 0xfc)
747                 continue;
748
749               /* Note that we've changed the relocs, section contents, etc.  */
750               elf_section_data (sec)->relocs = internal_relocs;
751               free_relocs = NULL;
752
753               elf_section_data (sec)->this_hdr.contents = contents;
754               free_contents = NULL;
755
756               symtab_hdr->contents = (bfd_byte *) extsyms;
757               free_extsyms = NULL;
758
759               /* Fix the opcode.  */
760               bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
761
762               /* Fix the relocation's type.  */
763               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
764                                            R_MN10200_PCREL8);
765
766               /* Delete one byte of data.  */
767               if (!mn10200_elf_relax_delete_bytes (abfd, sec,
768                                                    irel->r_offset + 1, 1))
769                 goto error_return;
770
771               /* That will change things, so, we should relax again.
772                  Note that this is not required, and it may be slow.  */
773               *again = true;
774             }
775         }
776
777       /* Try to eliminate an unconditional 8 bit pc-relative branch
778          which immediately follows a conditional 8 bit pc-relative
779          branch around the unconditional branch.
780
781             original:           new:
782             bCC lab1            bCC' lab2
783             bra lab2
784            lab1:               lab1:
785
786          This happens when the bCC can't reach lab2 at assembly time,
787          but due to other relaxations it can reach at link time.  */
788       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
789         {
790           Elf_Internal_Rela *nrel;
791           bfd_vma value = symval;
792           unsigned char code;
793
794           /* Deal with pc-relative gunk.  */
795           value -= (sec->output_section->vma + sec->output_offset);
796           value -= (irel->r_offset + 1);
797           value += irel->r_addend;
798
799           /* Do nothing if this reloc is the last byte in the section.  */
800           if (irel->r_offset == sec->_cooked_size)
801             continue;
802
803           /* See if the next instruction is an unconditional pc-relative
804              branch, more often than not this test will fail, so we
805              test it first to speed things up.  */
806           code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
807           if (code != 0xea)
808             continue;
809
810           /* Also make sure the next relocation applies to the next
811              instruction and that it's a pc-relative 8 bit branch.  */
812           nrel = irel + 1;
813           if (nrel == irelend
814               || irel->r_offset + 2 != nrel->r_offset
815               || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
816             continue;
817
818           /* Make sure our destination immediately follows the
819              unconditional branch.  */
820           if (symval != (sec->output_section->vma + sec->output_offset
821                          + irel->r_offset + 3))
822             continue;
823
824           /* Now make sure we are a conditional branch.  This may not
825              be necessary, but why take the chance.
826
827              Note these checks assume that R_MN10200_PCREL8 relocs
828              only occur on bCC and bCCx insns.  If they occured
829              elsewhere, we'd need to know the start of this insn
830              for this check to be accurate.  */
831           code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
832           if (code != 0xe0 && code != 0xe1 && code != 0xe2
833               && code != 0xe3 && code != 0xe4 && code != 0xe5
834               && code != 0xe6 && code != 0xe7 && code != 0xe8
835               && code != 0xe9 && code != 0xec && code != 0xed
836               && code != 0xee && code != 0xef && code != 0xfc
837               && code != 0xfd && code != 0xfe && code != 0xff)
838             continue;
839
840           /* We also have to be sure there is no symbol/label
841              at the unconditional branch.  */
842           if (mn10200_elf_symbol_address_p (abfd, sec, extsyms,
843                                             irel->r_offset + 1))
844             continue;
845
846           /* Note that we've changed the relocs, section contents, etc.  */
847           elf_section_data (sec)->relocs = internal_relocs;
848           free_relocs = NULL;
849
850           elf_section_data (sec)->this_hdr.contents = contents;
851           free_contents = NULL;
852
853           symtab_hdr->contents = (bfd_byte *) extsyms;
854           free_extsyms = NULL;
855
856           /* Reverse the condition of the first branch.  */
857           switch (code)
858             {
859             case 0xfc:
860               code = 0xfd;
861               break;
862             case 0xfd:
863               code = 0xfc;
864               break;
865             case 0xfe:
866               code = 0xff;
867               break;
868             case 0xff:
869               code = 0xfe;
870               break;
871             case 0xe8:
872               code = 0xe9;
873               break;
874             case 0xe9:
875               code = 0xe8;
876               break;
877             case 0xe0:
878               code = 0xe2;
879               break;
880             case 0xe2:
881               code = 0xe0;
882               break;
883             case 0xe3:
884               code = 0xe1;
885               break;
886             case 0xe1:
887               code = 0xe3;
888               break;
889             case 0xe4:
890               code = 0xe6;
891               break;
892             case 0xe6:
893               code = 0xe4;
894               break;
895             case 0xe7:
896               code = 0xe5;
897               break;
898             case 0xe5:
899               code = 0xe7;
900               break;
901             case 0xec:
902               code = 0xed;
903               break;
904             case 0xed:
905               code = 0xec;
906               break;
907             case 0xee:
908               code = 0xef;
909               break;
910             case 0xef:
911               code = 0xee;
912               break;
913             }
914           bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
915
916           /* Set the reloc type and symbol for the first branch
917              from the second branch.  */
918           irel->r_info = nrel->r_info;
919
920           /* Make the reloc for the second branch a null reloc.  */
921           nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
922                                        R_MN10200_NONE);
923
924           /* Delete two bytes of data.  */
925           if (!mn10200_elf_relax_delete_bytes (abfd, sec,
926                                                irel->r_offset + 1, 2))
927             goto error_return;
928
929           /* That will change things, so, we should relax again.
930              Note that this is not required, and it may be slow.  */
931           *again = true;
932         }
933
934       /* Try to turn a 24bit immediate, displacement or absolute address
935          into a 16bit immediate, displacement or absolute address.  */
936       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
937         {
938           bfd_vma value = symval;
939
940           /* See if the value will fit in 16 bits.
941              We allow any 16bit match here.  We prune those we can't
942              handle below.  */
943           if ((long) value < 0x7fff && (long) value > -0x8000)
944             {
945               unsigned char code;
946
947               /* All insns which have 24bit operands are 5 bytes long,
948                  the first byte will always be 0xf4, but we double check
949                  it just in case.  */
950
951               /* Get the first opcode.  */
952               code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
953
954               if (code != 0xf4)
955                 continue;
956
957               /* Get the second opcode.  */
958               code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
959
960               switch (code & 0xfc)
961                 {
962                 /* mov imm24,dn -> mov imm16,dn */
963                 case 0x70:
964                   /* Not safe if the high bit is on as relaxing may
965                      move the value out of high mem and thus not fit
966                      in a signed 16bit value.  */
967                   if (value & 0x8000)
968                     continue;
969
970                   /* Note that we've changed the reldection contents, etc.  */
971                   elf_section_data (sec)->relocs = internal_relocs;
972                   free_relocs = NULL;
973
974                   elf_section_data (sec)->this_hdr.contents = contents;
975                   free_contents = NULL;
976
977                   symtab_hdr->contents = (bfd_byte *) extsyms;
978                   free_extsyms = NULL;
979
980                   /* Fix the opcode.  */
981                   bfd_put_8 (abfd, 0xf8 + (code & 0x03),
982                              contents + irel->r_offset - 2);
983
984                   /* Fix the relocation's type.  */
985                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
986                                                R_MN10200_16);
987
988                   /* The opcode got shorter too, so we have to fix the
989                      offset.  */
990                   irel->r_offset -= 1;
991
992                   /* Delete two bytes of data.  */
993                   if (!mn10200_elf_relax_delete_bytes (abfd, sec,
994                                                        irel->r_offset + 1, 2))
995                     goto error_return;
996
997                   /* That will change things, so, we should relax again.
998                      Note that this is not required, and it may be slow.  */
999                   *again = true;
1000                   break;
1001
1002                 /* mov imm24,an -> mov imm16,an
1003                    cmp imm24,an -> cmp imm16,an
1004                    mov (abs24),dn -> mov (abs16),dn
1005                    mov dn,(abs24) -> mov dn,(abs16)
1006                    movb dn,(abs24) -> movb dn,(abs16)
1007                    movbu (abs24),dn -> movbu (abs16),dn */
1008                 case 0x74:
1009                 case 0x7c:
1010                 case 0xc0:
1011                 case 0x40:
1012                 case 0x44:
1013                 case 0xc8:
1014                   /* Note that we've changed the reldection contents, etc.  */
1015                   elf_section_data (sec)->relocs = internal_relocs;
1016                   free_relocs = NULL;
1017
1018                   elf_section_data (sec)->this_hdr.contents = contents;
1019                   free_contents = NULL;
1020
1021                   symtab_hdr->contents = (bfd_byte *) extsyms;
1022                   free_extsyms = NULL;
1023
1024                   if ((code & 0xfc) == 0x74)
1025                     code = 0xdc + (code & 0x03);
1026                   else if ((code & 0xfc) == 0x7c)
1027                     code = 0xec + (code & 0x03);
1028                   else if ((code & 0xfc) == 0xc0)
1029                     code = 0xc8 + (code & 0x03);
1030                   else if ((code & 0xfc) == 0x40)
1031                     code = 0xc0 + (code & 0x03);
1032                   else if ((code & 0xfc) == 0x44)
1033                     code = 0xc4 + (code & 0x03);
1034                   else if ((code & 0xfc) == 0xc8)
1035                     code = 0xcc + (code & 0x03);
1036
1037                   /* Fix the opcode.  */
1038                   bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1039
1040                   /* Fix the relocation's type.  */
1041                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1042                                                R_MN10200_16);
1043
1044                   /* The opcode got shorter too, so we have to fix the
1045                      offset.  */
1046                   irel->r_offset -= 1;
1047
1048                   /* Delete two bytes of data.  */
1049                   if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1050                                                        irel->r_offset + 1, 2))
1051                     goto error_return;
1052
1053                   /* That will change things, so, we should relax again.
1054                      Note that this is not required, and it may be slow.  */
1055                   *again = true;
1056                   break;
1057
1058                 /* cmp imm24,dn -> cmp imm16,dn
1059                    mov (abs24),an -> mov (abs16),an
1060                    mov an,(abs24) -> mov an,(abs16)
1061                    add imm24,dn -> add imm16,dn
1062                    add imm24,an -> add imm16,an
1063                    sub imm24,dn -> sub imm16,dn
1064                    sub imm24,an -> sub imm16,an
1065                    And all d24->d16 in memory ops.  */
1066                 case 0x78:
1067                 case 0xd0:
1068                 case 0x50:
1069                 case 0x60:
1070                 case 0x64:
1071                 case 0x68:
1072                 case 0x6c:
1073                 case 0x80:
1074                 case 0xf0:
1075                 case 0x00:
1076                 case 0x10:
1077                 case 0xb0:
1078                 case 0x30:
1079                 case 0xa0:
1080                 case 0x20:
1081                 case 0x90:
1082                   /* Not safe if the high bit is on as relaxing may
1083                      move the value out of high mem and thus not fit
1084                      in a signed 16bit value.  */
1085                   if (((code & 0xfc) == 0x78
1086                        || (code & 0xfc) == 0x60
1087                        || (code & 0xfc) == 0x64
1088                        || (code & 0xfc) == 0x68
1089                        || (code & 0xfc) == 0x6c
1090                        || (code & 0xfc) == 0x80
1091                        || (code & 0xfc) == 0xf0
1092                        || (code & 0xfc) == 0x00
1093                        || (code & 0xfc) == 0x10
1094                        || (code & 0xfc) == 0xb0
1095                        || (code & 0xfc) == 0x30
1096                        || (code & 0xfc) == 0xa0
1097                        || (code & 0xfc) == 0x20
1098                        || (code & 0xfc) == 0x90)
1099                       && (value & 0x8000) != 0)
1100                     continue;
1101
1102                   /* Note that we've changed the reldection contents, etc.  */
1103                   elf_section_data (sec)->relocs = internal_relocs;
1104                   free_relocs = NULL;
1105
1106                   elf_section_data (sec)->this_hdr.contents = contents;
1107                   free_contents = NULL;
1108
1109                   symtab_hdr->contents = (bfd_byte *) extsyms;
1110                   free_extsyms = NULL;
1111
1112                   /* Fix the opcode.  */
1113                   bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
1114
1115                   if ((code & 0xfc) == 0x78)
1116                     code = 0x48 + (code & 0x03);
1117                   else if ((code & 0xfc) == 0xd0)
1118                     code = 0x30 + (code & 0x03);
1119                   else if ((code & 0xfc) == 0x50)
1120                     code = 0x20 + (code & 0x03);
1121                   else if ((code & 0xfc) == 0x60)
1122                     code = 0x18 + (code & 0x03);
1123                   else if ((code & 0xfc) == 0x64)
1124                     code = 0x08 + (code & 0x03);
1125                   else if ((code & 0xfc) == 0x68)
1126                     code = 0x1c + (code & 0x03);
1127                   else if ((code & 0xfc) == 0x6c)
1128                     code = 0x0c + (code & 0x03);
1129                   else if ((code & 0xfc) == 0x80)
1130                     code = 0xc0 + (code & 0x07);
1131                   else if ((code & 0xfc) == 0xf0)
1132                     code = 0xb0 + (code & 0x07);
1133                   else if ((code & 0xfc) == 0x00)
1134                     code = 0x80 + (code & 0x07);
1135                   else if ((code & 0xfc) == 0x10)
1136                     code = 0xa0 + (code & 0x07);
1137                   else if ((code & 0xfc) == 0xb0)
1138                     code = 0x70 + (code & 0x07);
1139                   else if ((code & 0xfc) == 0x30)
1140                     code = 0x60 + (code & 0x07);
1141                   else if ((code & 0xfc) == 0xa0)
1142                     code = 0xd0 + (code & 0x07);
1143                   else if ((code & 0xfc) == 0x20)
1144                     code = 0x90 + (code & 0x07);
1145                   else if ((code & 0xfc) == 0x90)
1146                     code = 0x50 + (code & 0x07);
1147
1148                   bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1149
1150                   /* Fix the relocation's type.  */
1151                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1152                                                R_MN10200_16);
1153
1154                   /* Delete one bytes of data.  */
1155                   if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1156                                                        irel->r_offset + 2, 1))
1157                     goto error_return;
1158
1159                   /* That will change things, so, we should relax again.
1160                      Note that this is not required, and it may be slow.  */
1161                   *again = true;
1162                   break;
1163
1164                 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1165                 case 0xc4:
1166                   /* Note that we've changed the reldection contents, etc.  */
1167                   elf_section_data (sec)->relocs = internal_relocs;
1168                   free_relocs = NULL;
1169
1170                   elf_section_data (sec)->this_hdr.contents = contents;
1171                   free_contents = NULL;
1172
1173                   symtab_hdr->contents = (bfd_byte *) extsyms;
1174                   free_extsyms = NULL;
1175
1176                   bfd_put_8 (abfd, 0xcc + (code & 0x03),
1177                              contents + irel->r_offset - 2);
1178
1179                   bfd_put_8 (abfd, 0xb8 + (code & 0x03),
1180                              contents + irel->r_offset - 1);
1181
1182                   /* Fix the relocation's type.  */
1183                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1184                                                R_MN10200_16);
1185
1186                   /* The reloc will be applied one byte in front of its
1187                      current location.  */
1188                   irel->r_offset -= 1;
1189
1190                   /* Delete one bytes of data.  */
1191                   if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1192                                                        irel->r_offset + 2, 1))
1193                     goto error_return;
1194
1195                   /* That will change things, so, we should relax again.
1196                      Note that this is not required, and it may be slow.  */
1197                   *again = true;
1198                   break;
1199                 }
1200             }
1201         }
1202     }
1203
1204   if (free_relocs != NULL)
1205     {
1206       free (free_relocs);
1207       free_relocs = NULL;
1208     }
1209
1210   if (free_contents != NULL)
1211     {
1212       if (! link_info->keep_memory)
1213         free (free_contents);
1214       else
1215         {
1216           /* Cache the section contents for elf_link_input_bfd.  */
1217           elf_section_data (sec)->this_hdr.contents = contents;
1218         }
1219       free_contents = NULL;
1220     }
1221
1222   if (free_extsyms != NULL)
1223     {
1224       if (! link_info->keep_memory)
1225         free (free_extsyms);
1226       else
1227         {
1228           /* Cache the symbols for elf_link_input_bfd.  */
1229           symtab_hdr->contents = extsyms;
1230         }
1231       free_extsyms = NULL;
1232     }
1233
1234   return true;
1235
1236  error_return:
1237   if (free_relocs != NULL)
1238     free (free_relocs);
1239   if (free_contents != NULL)
1240     free (free_contents);
1241   if (free_extsyms != NULL)
1242     free (free_extsyms);
1243   return false;
1244 }
1245
1246 /* Delete some bytes from a section while relaxing.  */
1247
1248 static boolean
1249 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
1250      bfd *abfd;
1251      asection *sec;
1252      bfd_vma addr;
1253      int count;
1254 {
1255   Elf_Internal_Shdr *symtab_hdr;
1256   Elf32_External_Sym *extsyms;
1257   int shndx, index;
1258   bfd_byte *contents;
1259   Elf_Internal_Rela *irel, *irelend;
1260   Elf_Internal_Rela *irelalign;
1261   bfd_vma toaddr;
1262   Elf32_External_Sym *esym, *esymend;
1263   struct elf_link_hash_entry *sym_hash;
1264
1265   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1266   extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1267
1268   shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1269
1270   contents = elf_section_data (sec)->this_hdr.contents;
1271
1272   /* The deletion must stop at the next ALIGN reloc for an aligment
1273      power larger than the number of bytes we are deleting.  */
1274
1275   irelalign = NULL;
1276   toaddr = sec->_cooked_size;
1277
1278   irel = elf_section_data (sec)->relocs;
1279   irelend = irel + sec->reloc_count;
1280
1281   /* Actually delete the bytes.  */
1282   memmove (contents + addr, contents + addr + count, 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           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
1423       if (symtab_hdr->contents != NULL)
1424         external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1425       else
1426         {
1427           external_syms = ((Elf32_External_Sym *)
1428                            bfd_malloc (symtab_hdr->sh_info
1429                                        * sizeof (Elf32_External_Sym)));
1430           if (external_syms == NULL && symtab_hdr->sh_info > 0)
1431             goto error_return;
1432           if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1433               || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1434                             symtab_hdr->sh_info, input_bfd)
1435                   != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1436             goto error_return;
1437         }
1438
1439       internal_relocs = (_bfd_elf32_link_read_relocs
1440                          (input_bfd, input_section, (PTR) NULL,
1441                           (Elf_Internal_Rela *) NULL, false));
1442       if (internal_relocs == NULL)
1443         goto error_return;
1444
1445       internal_syms = ((Elf_Internal_Sym *)
1446                        bfd_malloc (symtab_hdr->sh_info
1447                                    * sizeof (Elf_Internal_Sym)));
1448       if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1449         goto error_return;
1450
1451       sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1452                                            * sizeof (asection *));
1453       if (sections == NULL && symtab_hdr->sh_info > 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_CYGNUS_MN10200
1521 #define ELF_MAXPAGESIZE         0x1000
1522
1523 #define elf_info_to_howto       mn10200_info_to_howto
1524 #define elf_info_to_howto_rel   0
1525 #define elf_backend_relocate_section mn10200_elf_relocate_section
1526 #define bfd_elf32_bfd_relax_section     mn10200_elf_relax_section
1527 #define bfd_elf32_bfd_get_relocated_section_contents \
1528                                 mn10200_elf_get_relocated_section_contents
1529
1530 #define elf_symbol_leading_char '_'
1531
1532 #include "elf32-target.h"