Back to index

cell-binutils  2.17cvs20070401
elf-m10300.c
Go to the documentation of this file.
00001 /* Matsushita 10300 specific support for 32-bit ELF
00002    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00003    2006, 2007 Free Software Foundation, Inc.
00004 
00005    This file is part of BFD, the Binary File Descriptor library.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00020 
00021 #include "bfd.h"
00022 #include "sysdep.h"
00023 #include "libbfd.h"
00024 #include "elf-bfd.h"
00025 #include "elf/mn10300.h"
00026 
00027 static bfd_reloc_status_type mn10300_elf_final_link_relocate
00028   PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
00029           bfd_vma, bfd_vma, bfd_vma,
00030           struct elf_link_hash_entry *, unsigned long, struct bfd_link_info *,
00031           asection *, int));
00032 static bfd_boolean mn10300_elf_relocate_section
00033   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
00034           Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
00035 static bfd_boolean mn10300_elf_relax_section
00036   PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
00037 static bfd_byte * mn10300_elf_get_relocated_section_contents
00038   PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
00039           bfd_byte *, bfd_boolean, asymbol **));
00040 static unsigned long elf_mn10300_mach
00041   PARAMS ((flagword));
00042 void _bfd_mn10300_elf_final_write_processing
00043   PARAMS ((bfd *, bfd_boolean));
00044 bfd_boolean _bfd_mn10300_elf_object_p
00045   PARAMS ((bfd *));
00046 bfd_boolean _bfd_mn10300_elf_merge_private_bfd_data
00047   PARAMS ((bfd *,bfd *));
00048 
00049 /* The mn10300 linker needs to keep track of the number of relocs that
00050    it decides to copy in check_relocs for each symbol.  This is so
00051    that it can discard PC relative relocs if it doesn't need them when
00052    linking with -Bsymbolic.  We store the information in a field
00053    extending the regular ELF linker hash table.  */
00054 
00055 struct elf32_mn10300_link_hash_entry {
00056   /* The basic elf link hash table entry.  */
00057   struct elf_link_hash_entry root;
00058 
00059   /* For function symbols, the number of times this function is
00060      called directly (ie by name).  */
00061   unsigned int direct_calls;
00062 
00063   /* For function symbols, the size of this function's stack
00064      (if <= 255 bytes).  We stuff this into "call" instructions
00065      to this target when it's valid and profitable to do so.
00066 
00067      This does not include stack allocated by movm!  */
00068   unsigned char stack_size;
00069 
00070   /* For function symbols, arguments (if any) for movm instruction
00071      in the prologue.  We stuff this value into "call" instructions
00072      to the target when it's valid and profitable to do so.  */
00073   unsigned char movm_args;
00074 
00075   /* For function symbols, the amount of stack space that would be allocated
00076      by the movm instruction.  This is redundant with movm_args, but we
00077      add it to the hash table to avoid computing it over and over.  */
00078   unsigned char movm_stack_size;
00079 
00080 /* When set, convert all "call" instructions to this target into "calls"
00081    instructions.  */
00082 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
00083 
00084 /* Used to mark functions which have had redundant parts of their
00085    prologue deleted.  */
00086 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
00087   unsigned char flags;
00088 
00089   /* Calculated value.  */
00090   bfd_vma value;
00091 };
00092 
00093 /* We derive a hash table from the main elf linker hash table so
00094    we can store state variables and a secondary hash table without
00095    resorting to global variables.  */
00096 struct elf32_mn10300_link_hash_table {
00097   /* The main hash table.  */
00098   struct elf_link_hash_table root;
00099 
00100   /* A hash table for static functions.  We could derive a new hash table
00101      instead of using the full elf32_mn10300_link_hash_table if we wanted
00102      to save some memory.  */
00103   struct elf32_mn10300_link_hash_table *static_hash_table;
00104 
00105   /* Random linker state flags.  */
00106 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
00107   char flags;
00108 };
00109 
00110 /* For MN10300 linker hash table.  */
00111 
00112 /* Get the MN10300 ELF linker hash table from a link_info structure.  */
00113 
00114 #define elf32_mn10300_hash_table(p) \
00115   ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
00116 
00117 #define elf32_mn10300_link_hash_traverse(table, func, info)           \
00118   (elf_link_hash_traverse                                      \
00119    (&(table)->root,                                            \
00120     (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
00121     (info)))
00122 
00123 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
00124   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
00125 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
00126   PARAMS ((bfd *));
00127 static void elf32_mn10300_link_hash_table_free
00128   PARAMS ((struct bfd_link_hash_table *));
00129 
00130 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
00131   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
00132 static void mn10300_info_to_howto
00133   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
00134 static bfd_boolean mn10300_elf_check_relocs
00135   PARAMS ((bfd *, struct bfd_link_info *, asection *,
00136           const Elf_Internal_Rela *));
00137 static bfd_boolean mn10300_elf_relax_delete_bytes
00138   PARAMS ((bfd *, asection *, bfd_vma, int));
00139 static bfd_boolean mn10300_elf_symbol_address_p
00140   PARAMS ((bfd *, asection *, Elf_Internal_Sym *, bfd_vma));
00141 static bfd_boolean elf32_mn10300_finish_hash_table_entry
00142   PARAMS ((struct bfd_hash_entry *, PTR));
00143 static void compute_function_info
00144   PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
00145           bfd_vma, unsigned char *));
00146 
00147 static bfd_boolean _bfd_mn10300_elf_create_got_section
00148   PARAMS ((bfd *, struct bfd_link_info *));
00149 static bfd_boolean _bfd_mn10300_elf_create_dynamic_sections
00150   PARAMS ((bfd *, struct bfd_link_info *));
00151 static bfd_boolean _bfd_mn10300_elf_adjust_dynamic_symbol
00152   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
00153 static bfd_boolean _bfd_mn10300_elf_size_dynamic_sections
00154   PARAMS ((bfd *, struct bfd_link_info *));
00155 static bfd_boolean _bfd_mn10300_elf_finish_dynamic_symbol
00156   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
00157           Elf_Internal_Sym *));
00158 static bfd_boolean _bfd_mn10300_elf_finish_dynamic_sections
00159   PARAMS ((bfd *, struct bfd_link_info *));
00160 
00161 static reloc_howto_type elf_mn10300_howto_table[] = {
00162   /* Dummy relocation.  Does nothing.  */
00163   HOWTO (R_MN10300_NONE,
00164         0,
00165         2,
00166         16,
00167         FALSE,
00168         0,
00169         complain_overflow_bitfield,
00170         bfd_elf_generic_reloc,
00171         "R_MN10300_NONE",
00172         FALSE,
00173         0,
00174         0,
00175         FALSE),
00176   /* Standard 32 bit reloc.  */
00177   HOWTO (R_MN10300_32,
00178         0,
00179         2,
00180         32,
00181         FALSE,
00182         0,
00183         complain_overflow_bitfield,
00184         bfd_elf_generic_reloc,
00185         "R_MN10300_32",
00186         FALSE,
00187         0xffffffff,
00188         0xffffffff,
00189         FALSE),
00190   /* Standard 16 bit reloc.  */
00191   HOWTO (R_MN10300_16,
00192         0,
00193         1,
00194         16,
00195         FALSE,
00196         0,
00197         complain_overflow_bitfield,
00198         bfd_elf_generic_reloc,
00199         "R_MN10300_16",
00200         FALSE,
00201         0xffff,
00202         0xffff,
00203         FALSE),
00204   /* Standard 8 bit reloc.  */
00205   HOWTO (R_MN10300_8,
00206         0,
00207         0,
00208         8,
00209         FALSE,
00210         0,
00211         complain_overflow_bitfield,
00212         bfd_elf_generic_reloc,
00213         "R_MN10300_8",
00214         FALSE,
00215         0xff,
00216         0xff,
00217         FALSE),
00218   /* Standard 32bit pc-relative reloc.  */
00219   HOWTO (R_MN10300_PCREL32,
00220         0,
00221         2,
00222         32,
00223         TRUE,
00224         0,
00225         complain_overflow_bitfield,
00226         bfd_elf_generic_reloc,
00227         "R_MN10300_PCREL32",
00228         FALSE,
00229         0xffffffff,
00230         0xffffffff,
00231         TRUE),
00232   /* Standard 16bit pc-relative reloc.  */
00233   HOWTO (R_MN10300_PCREL16,
00234         0,
00235         1,
00236         16,
00237         TRUE,
00238         0,
00239         complain_overflow_bitfield,
00240         bfd_elf_generic_reloc,
00241         "R_MN10300_PCREL16",
00242         FALSE,
00243         0xffff,
00244         0xffff,
00245         TRUE),
00246   /* Standard 8 pc-relative reloc.  */
00247   HOWTO (R_MN10300_PCREL8,
00248         0,
00249         0,
00250         8,
00251         TRUE,
00252         0,
00253         complain_overflow_bitfield,
00254         bfd_elf_generic_reloc,
00255         "R_MN10300_PCREL8",
00256         FALSE,
00257         0xff,
00258         0xff,
00259         TRUE),
00260 
00261   /* GNU extension to record C++ vtable hierarchy */
00262   HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
00263         0,                  /* rightshift */
00264         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00265         0,                  /* bitsize */
00266         FALSE,                     /* pc_relative */
00267         0,                  /* bitpos */
00268         complain_overflow_dont, /* complain_on_overflow */
00269         NULL,               /* special_function */
00270         "R_MN10300_GNU_VTINHERIT", /* name */
00271         FALSE,                     /* partial_inplace */
00272         0,                  /* src_mask */
00273         0,                  /* dst_mask */
00274         FALSE),             /* pcrel_offset */
00275 
00276   /* GNU extension to record C++ vtable member usage */
00277   HOWTO (R_MN10300_GNU_VTENTRY,    /* type */
00278         0,                  /* rightshift */
00279         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00280         0,                  /* bitsize */
00281         FALSE,                     /* pc_relative */
00282         0,                  /* bitpos */
00283         complain_overflow_dont, /* complain_on_overflow */
00284         NULL,               /* special_function */
00285         "R_MN10300_GNU_VTENTRY", /* name */
00286         FALSE,                     /* partial_inplace */
00287         0,                  /* src_mask */
00288         0,                  /* dst_mask */
00289         FALSE),             /* pcrel_offset */
00290 
00291   /* Standard 24 bit reloc.  */
00292   HOWTO (R_MN10300_24,
00293         0,
00294         2,
00295         24,
00296         FALSE,
00297         0,
00298         complain_overflow_bitfield,
00299         bfd_elf_generic_reloc,
00300         "R_MN10300_24",
00301         FALSE,
00302         0xffffff,
00303         0xffffff,
00304         FALSE),
00305   HOWTO (R_MN10300_GOTPC32, /* type */
00306         0,                  /* rightshift */
00307         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00308         32,                 /* bitsize */
00309         TRUE,               /* pc_relative */
00310         0,                  /* bitpos */
00311         complain_overflow_bitfield, /* complain_on_overflow */
00312         bfd_elf_generic_reloc, /* */
00313         "R_MN10300_GOTPC32",       /* name */
00314         FALSE,                     /* partial_inplace */
00315         0xffffffff,         /* src_mask */
00316         0xffffffff,         /* dst_mask */
00317         TRUE),                     /* pcrel_offset */
00318 
00319   HOWTO (R_MN10300_GOTPC16, /* type */
00320         0,                  /* rightshift */
00321         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00322         16,                 /* bitsize */
00323         TRUE,               /* pc_relative */
00324         0,                  /* bitpos */
00325         complain_overflow_bitfield, /* complain_on_overflow */
00326         bfd_elf_generic_reloc, /* */
00327         "R_MN10300_GOTPC16",       /* name */
00328         FALSE,                     /* partial_inplace */
00329         0xffff,             /* src_mask */
00330         0xffff,             /* dst_mask */
00331         TRUE),                     /* pcrel_offset */
00332 
00333   HOWTO (R_MN10300_GOTOFF32,       /* type */
00334         0,                  /* rightshift */
00335         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00336         32,                 /* bitsize */
00337         FALSE,                     /* pc_relative */
00338         0,                  /* bitpos */
00339         complain_overflow_bitfield, /* complain_on_overflow */
00340         bfd_elf_generic_reloc, /* */
00341         "R_MN10300_GOTOFF32",      /* name */
00342         FALSE,                     /* partial_inplace */
00343         0xffffffff,         /* src_mask */
00344         0xffffffff,         /* dst_mask */
00345         FALSE),             /* pcrel_offset */
00346 
00347   HOWTO (R_MN10300_GOTOFF24,       /* type */
00348         0,                  /* rightshift */
00349         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00350         24,                 /* bitsize */
00351         FALSE,                     /* pc_relative */
00352         0,                  /* bitpos */
00353         complain_overflow_bitfield, /* complain_on_overflow */
00354         bfd_elf_generic_reloc, /* */
00355         "R_MN10300_GOTOFF24",      /* name */
00356         FALSE,                     /* partial_inplace */
00357         0xffffff,           /* src_mask */
00358         0xffffff,           /* dst_mask */
00359         FALSE),             /* pcrel_offset */
00360 
00361   HOWTO (R_MN10300_GOTOFF16,       /* type */
00362         0,                  /* rightshift */
00363         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00364         16,                 /* bitsize */
00365         FALSE,                     /* pc_relative */
00366         0,                  /* bitpos */
00367         complain_overflow_bitfield, /* complain_on_overflow */
00368         bfd_elf_generic_reloc, /* */
00369         "R_MN10300_GOTOFF16",      /* name */
00370         FALSE,                     /* partial_inplace */
00371         0xffff,             /* src_mask */
00372         0xffff,             /* dst_mask */
00373         FALSE),             /* pcrel_offset */
00374 
00375   HOWTO (R_MN10300_PLT32,   /* type */
00376         0,                  /* rightshift */
00377         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00378         32,                 /* bitsize */
00379         TRUE,               /* pc_relative */
00380         0,                  /* bitpos */
00381         complain_overflow_bitfield, /* complain_on_overflow */
00382         bfd_elf_generic_reloc, /* */
00383         "R_MN10300_PLT32",  /* name */
00384         FALSE,                     /* partial_inplace */
00385         0xffffffff,         /* src_mask */
00386         0xffffffff,         /* dst_mask */
00387         TRUE),                     /* pcrel_offset */
00388 
00389   HOWTO (R_MN10300_PLT16,   /* type */
00390         0,                  /* rightshift */
00391         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00392         16,                 /* bitsize */
00393         TRUE,               /* pc_relative */
00394         0,                  /* bitpos */
00395         complain_overflow_bitfield, /* complain_on_overflow */
00396         bfd_elf_generic_reloc, /* */
00397         "R_MN10300_PLT16",  /* name */
00398         FALSE,                     /* partial_inplace */
00399         0xffff,             /* src_mask */
00400         0xffff,             /* dst_mask */
00401         TRUE),                     /* pcrel_offset */
00402 
00403   HOWTO (R_MN10300_GOT32,   /* type */
00404         0,                  /* rightshift */
00405         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00406         32,                 /* bitsize */
00407         FALSE,                     /* pc_relative */
00408         0,                  /* bitpos */
00409         complain_overflow_bitfield, /* complain_on_overflow */
00410         bfd_elf_generic_reloc, /* */
00411         "R_MN10300_GOT32",  /* name */
00412         FALSE,                     /* partial_inplace */
00413         0xffffffff,         /* src_mask */
00414         0xffffffff,         /* dst_mask */
00415         FALSE),             /* pcrel_offset */
00416 
00417   HOWTO (R_MN10300_GOT24,   /* type */
00418         0,                  /* rightshift */
00419         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00420         24,                 /* bitsize */
00421         FALSE,                     /* pc_relative */
00422         0,                  /* bitpos */
00423         complain_overflow_bitfield, /* complain_on_overflow */
00424         bfd_elf_generic_reloc, /* */
00425         "R_MN10300_GOT24",  /* name */
00426         FALSE,                     /* partial_inplace */
00427         0xffffffff,         /* src_mask */
00428         0xffffffff,         /* dst_mask */
00429         FALSE),             /* pcrel_offset */
00430 
00431   HOWTO (R_MN10300_GOT16,   /* type */
00432         0,                  /* rightshift */
00433         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00434         16,                 /* bitsize */
00435         FALSE,                     /* pc_relative */
00436         0,                  /* bitpos */
00437         complain_overflow_bitfield, /* complain_on_overflow */
00438         bfd_elf_generic_reloc, /* */
00439         "R_MN10300_GOT16",  /* name */
00440         FALSE,                     /* partial_inplace */
00441         0xffffffff,         /* src_mask */
00442         0xffffffff,         /* dst_mask */
00443         FALSE),             /* pcrel_offset */
00444 
00445   HOWTO (R_MN10300_COPY,    /* type */
00446         0,                  /* rightshift */
00447         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00448         32,                 /* bitsize */
00449         FALSE,                     /* pc_relative */
00450         0,                  /* bitpos */
00451         complain_overflow_bitfield, /* complain_on_overflow */
00452         bfd_elf_generic_reloc, /* */
00453         "R_MN10300_COPY",          /* name */
00454         FALSE,                     /* partial_inplace */
00455         0xffffffff,         /* src_mask */
00456         0xffffffff,         /* dst_mask */
00457         FALSE),             /* pcrel_offset */
00458 
00459   HOWTO (R_MN10300_GLOB_DAT,       /* type */
00460         0,                  /* rightshift */
00461         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00462         32,                 /* bitsize */
00463         FALSE,                     /* pc_relative */
00464         0,                  /* bitpos */
00465         complain_overflow_bitfield, /* complain_on_overflow */
00466         bfd_elf_generic_reloc, /* */
00467         "R_MN10300_GLOB_DAT",      /* name */
00468         FALSE,                     /* partial_inplace */
00469         0xffffffff,         /* src_mask */
00470         0xffffffff,         /* dst_mask */
00471         FALSE),             /* pcrel_offset */
00472 
00473   HOWTO (R_MN10300_JMP_SLOT,       /* type */
00474         0,                  /* rightshift */
00475         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00476         32,                 /* bitsize */
00477         FALSE,                     /* pc_relative */
00478         0,                  /* bitpos */
00479         complain_overflow_bitfield, /* complain_on_overflow */
00480         bfd_elf_generic_reloc, /* */
00481         "R_MN10300_JMP_SLOT",      /* name */
00482         FALSE,                     /* partial_inplace */
00483         0xffffffff,         /* src_mask */
00484         0xffffffff,         /* dst_mask */
00485         FALSE),             /* pcrel_offset */
00486 
00487   HOWTO (R_MN10300_RELATIVE,       /* type */
00488         0,                  /* rightshift */
00489         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00490         32,                 /* bitsize */
00491         FALSE,                     /* pc_relative */
00492         0,                  /* bitpos */
00493         complain_overflow_bitfield, /* complain_on_overflow */
00494         bfd_elf_generic_reloc, /* */
00495         "R_MN10300_RELATIVE",      /* name */
00496         FALSE,                     /* partial_inplace */
00497         0xffffffff,         /* src_mask */
00498         0xffffffff,         /* dst_mask */
00499         FALSE),             /* pcrel_offset */
00500 
00501 };
00502 
00503 struct mn10300_reloc_map {
00504   bfd_reloc_code_real_type bfd_reloc_val;
00505   unsigned char elf_reloc_val;
00506 };
00507 
00508 static const struct mn10300_reloc_map mn10300_reloc_map[] = {
00509   { BFD_RELOC_NONE, R_MN10300_NONE, },
00510   { BFD_RELOC_32, R_MN10300_32, },
00511   { BFD_RELOC_16, R_MN10300_16, },
00512   { BFD_RELOC_8, R_MN10300_8, },
00513   { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
00514   { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
00515   { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
00516   { BFD_RELOC_24, R_MN10300_24, },
00517   { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
00518   { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
00519   { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
00520   { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
00521   { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
00522   { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
00523   { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
00524   { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
00525   { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
00526   { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
00527   { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
00528   { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
00529   { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
00530   { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
00531   { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
00532   { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
00533 };
00534 
00535 /* Create the GOT section.  */
00536 
00537 static bfd_boolean
00538 _bfd_mn10300_elf_create_got_section (abfd, info)
00539      bfd * abfd;
00540      struct bfd_link_info * info;
00541 {
00542   flagword   flags;
00543   flagword   pltflags;
00544   asection * s;
00545   struct elf_link_hash_entry * h;
00546   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
00547   int ptralign;
00548 
00549   /* This function may be called more than once.  */
00550   if (bfd_get_section_by_name (abfd, ".got") != NULL)
00551     return TRUE;
00552 
00553   switch (bed->s->arch_size)
00554     {
00555     case 32:
00556       ptralign = 2;
00557       break;
00558 
00559     case 64:
00560       ptralign = 3;
00561       break;
00562 
00563     default:
00564       bfd_set_error (bfd_error_bad_value);
00565       return FALSE;
00566     }
00567 
00568   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
00569           | SEC_LINKER_CREATED);
00570 
00571   pltflags = flags;
00572   pltflags |= SEC_CODE;
00573   if (bed->plt_not_loaded)
00574     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
00575   if (bed->plt_readonly)
00576     pltflags |= SEC_READONLY;
00577 
00578   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
00579   if (s == NULL
00580       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
00581     return FALSE;
00582 
00583   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
00584      .plt section.  */
00585   if (bed->want_plt_sym)
00586     {
00587       h = _bfd_elf_define_linkage_sym (abfd, info, s,
00588                                    "_PROCEDURE_LINKAGE_TABLE_");
00589       elf_hash_table (info)->hplt = h;
00590       if (h == NULL)
00591        return FALSE;
00592     }
00593 
00594   s = bfd_make_section_with_flags (abfd, ".got", flags);
00595   if (s == NULL
00596       || ! bfd_set_section_alignment (abfd, s, ptralign))
00597     return FALSE;
00598 
00599   if (bed->want_got_plt)
00600     {
00601       s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
00602       if (s == NULL
00603          || ! bfd_set_section_alignment (abfd, s, ptralign))
00604        return FALSE;
00605     }
00606 
00607   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
00608      (or .got.plt) section.  We don't do this in the linker script
00609      because we don't want to define the symbol if we are not creating
00610      a global offset table.  */
00611   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
00612   elf_hash_table (info)->hgot = h;
00613   if (h == NULL)
00614     return FALSE;
00615 
00616   /* The first bit of the global offset table is the header.  */
00617   s->size += bed->got_header_size;
00618 
00619   return TRUE;
00620 }
00621 
00622 static reloc_howto_type *
00623 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
00624      bfd *abfd ATTRIBUTE_UNUSED;
00625      bfd_reloc_code_real_type code;
00626 {
00627   unsigned int i;
00628 
00629   for (i = 0;
00630        i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
00631        i++)
00632     {
00633       if (mn10300_reloc_map[i].bfd_reloc_val == code)
00634        return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
00635     }
00636 
00637   return NULL;
00638 }
00639 
00640 static reloc_howto_type *
00641 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00642                              const char *r_name)
00643 {
00644   unsigned int i;
00645 
00646   for (i = 0;
00647        i < (sizeof (elf_mn10300_howto_table)
00648            / sizeof (elf_mn10300_howto_table[0]));
00649        i++)
00650     if (elf_mn10300_howto_table[i].name != NULL
00651        && strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
00652       return &elf_mn10300_howto_table[i];
00653 
00654   return NULL;
00655 }
00656 
00657 /* Set the howto pointer for an MN10300 ELF reloc.  */
00658 
00659 static void
00660 mn10300_info_to_howto (abfd, cache_ptr, dst)
00661      bfd *abfd ATTRIBUTE_UNUSED;
00662      arelent *cache_ptr;
00663      Elf_Internal_Rela *dst;
00664 {
00665   unsigned int r_type;
00666 
00667   r_type = ELF32_R_TYPE (dst->r_info);
00668   BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
00669   cache_ptr->howto = &elf_mn10300_howto_table[r_type];
00670 }
00671 
00672 /* Look through the relocs for a section during the first phase.
00673    Since we don't do .gots or .plts, we just need to consider the
00674    virtual table relocs for gc.  */
00675 
00676 static bfd_boolean
00677 mn10300_elf_check_relocs (abfd, info, sec, relocs)
00678      bfd *abfd;
00679      struct bfd_link_info *info;
00680      asection *sec;
00681      const Elf_Internal_Rela *relocs;
00682 {
00683   Elf_Internal_Shdr *symtab_hdr;
00684   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
00685   const Elf_Internal_Rela *rel;
00686   const Elf_Internal_Rela *rel_end;
00687   bfd *      dynobj;
00688   bfd_vma *  local_got_offsets;
00689   asection * sgot;
00690   asection * srelgot;
00691   asection * sreloc;
00692 
00693   sgot    = NULL;
00694   srelgot = NULL;
00695   sreloc  = NULL;
00696 
00697   if (info->relocatable)
00698     return TRUE;
00699 
00700   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00701   sym_hashes = elf_sym_hashes (abfd);
00702   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
00703   if (!elf_bad_symtab (abfd))
00704     sym_hashes_end -= symtab_hdr->sh_info;
00705 
00706   dynobj = elf_hash_table (info)->dynobj;
00707   local_got_offsets = elf_local_got_offsets (abfd);
00708   rel_end = relocs + sec->reloc_count;
00709   for (rel = relocs; rel < rel_end; rel++)
00710     {
00711       struct elf_link_hash_entry *h;
00712       unsigned long r_symndx;
00713 
00714       r_symndx = ELF32_R_SYM (rel->r_info);
00715       if (r_symndx < symtab_hdr->sh_info)
00716        h = NULL;
00717       else
00718        {
00719          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
00720          while (h->root.type == bfd_link_hash_indirect
00721                || h->root.type == bfd_link_hash_warning)
00722            h = (struct elf_link_hash_entry *) h->root.u.i.link;
00723        }
00724 
00725       /* Some relocs require a global offset table.  */
00726       if (dynobj == NULL)
00727        {
00728          switch (ELF32_R_TYPE (rel->r_info))
00729            {
00730            case R_MN10300_GOT32:
00731            case R_MN10300_GOT24:
00732            case R_MN10300_GOT16:
00733            case R_MN10300_GOTOFF32:
00734            case R_MN10300_GOTOFF24:
00735            case R_MN10300_GOTOFF16:
00736            case R_MN10300_GOTPC32:
00737            case R_MN10300_GOTPC16:
00738              elf_hash_table (info)->dynobj = dynobj = abfd;
00739              if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
00740               return FALSE;
00741              break;
00742 
00743            default:
00744              break;
00745            }
00746        }
00747 
00748       switch (ELF32_R_TYPE (rel->r_info))
00749        {
00750        /* This relocation describes the C++ object vtable hierarchy.
00751           Reconstruct it for later use during GC.  */
00752        case R_MN10300_GNU_VTINHERIT:
00753          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
00754            return FALSE;
00755          break;
00756 
00757        /* This relocation describes which C++ vtable entries are actually
00758           used.  Record for later use during GC.  */
00759        case R_MN10300_GNU_VTENTRY:
00760          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
00761            return FALSE;
00762          break;
00763        case R_MN10300_GOT32:
00764        case R_MN10300_GOT24:
00765        case R_MN10300_GOT16:
00766          /* This symbol requires a global offset table entry.  */
00767 
00768          if (sgot == NULL)
00769            {
00770              sgot = bfd_get_section_by_name (dynobj, ".got");
00771              BFD_ASSERT (sgot != NULL);
00772            }
00773 
00774          if (srelgot == NULL
00775              && (h != NULL || info->shared))
00776            {
00777              srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
00778              if (srelgot == NULL)
00779               {
00780                 srelgot = bfd_make_section_with_flags (dynobj,
00781                                                   ".rela.got",
00782                                                   (SEC_ALLOC
00783                                                    | SEC_LOAD
00784                                                    | SEC_HAS_CONTENTS
00785                                                    | SEC_IN_MEMORY
00786                                                    | SEC_LINKER_CREATED
00787                                                    | SEC_READONLY));
00788                 if (srelgot == NULL
00789                     || ! bfd_set_section_alignment (dynobj, srelgot, 2))
00790                   return FALSE;
00791               }
00792            }
00793 
00794          if (h != NULL)
00795            {
00796              if (h->got.offset != (bfd_vma) -1)
00797               /* We have already allocated space in the .got.  */
00798               break;
00799 
00800              h->got.offset = sgot->size;
00801 
00802              /* Make sure this symbol is output as a dynamic symbol.  */
00803              if (h->dynindx == -1)
00804               {
00805                 if (! bfd_elf_link_record_dynamic_symbol (info, h))
00806                   return FALSE;
00807               }
00808 
00809              srelgot->size += sizeof (Elf32_External_Rela);
00810            }
00811          else
00812            {
00813              /* This is a global offset table entry for a local
00814                symbol.  */
00815              if (local_got_offsets == NULL)
00816               {
00817                 size_t       size;
00818                 unsigned int i;
00819 
00820                 size = symtab_hdr->sh_info * sizeof (bfd_vma);
00821                 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
00822 
00823                 if (local_got_offsets == NULL)
00824                   return FALSE;
00825                 elf_local_got_offsets (abfd) = local_got_offsets;
00826 
00827                 for (i = 0; i < symtab_hdr->sh_info; i++)
00828                   local_got_offsets[i] = (bfd_vma) -1;
00829               }
00830 
00831              if (local_got_offsets[r_symndx] != (bfd_vma) -1)
00832               /* We have already allocated space in the .got.  */
00833               break;
00834 
00835              local_got_offsets[r_symndx] = sgot->size;
00836 
00837              if (info->shared)
00838               /* If we are generating a shared object, we need to
00839                  output a R_MN10300_RELATIVE reloc so that the dynamic
00840                  linker can adjust this GOT entry.  */
00841               srelgot->size += sizeof (Elf32_External_Rela);
00842            }
00843 
00844          sgot->size += 4;
00845 
00846          break;
00847 
00848        case R_MN10300_PLT32:
00849        case R_MN10300_PLT16:
00850          /* This symbol requires a procedure linkage table entry.  We
00851             actually build the entry in adjust_dynamic_symbol,
00852             because this might be a case of linking PIC code which is
00853             never referenced by a dynamic object, in which case we
00854             don't need to generate a procedure linkage table entry
00855             after all.  */
00856 
00857          /* If this is a local symbol, we resolve it directly without
00858             creating a procedure linkage table entry.  */
00859          if (h == NULL)
00860            continue;
00861 
00862          if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
00863              || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
00864            break;
00865 
00866          h->needs_plt = 1;
00867 
00868          break;
00869 
00870        case R_MN10300_24:
00871        case R_MN10300_16:
00872        case R_MN10300_8:
00873        case R_MN10300_PCREL32:
00874        case R_MN10300_PCREL16:
00875        case R_MN10300_PCREL8:
00876          if (h != NULL)
00877            h->non_got_ref = 1;
00878          break;
00879 
00880        case R_MN10300_32:
00881          if (h != NULL)
00882            h->non_got_ref = 1;
00883 
00884          /* If we are creating a shared library, then we need to copy
00885             the reloc into the shared library.  */
00886          if (info->shared
00887              && (sec->flags & SEC_ALLOC) != 0)
00888            {
00889              /* When creating a shared object, we must copy these
00890                reloc types into the output file.  We create a reloc
00891                section in dynobj and make room for this reloc.  */
00892              if (sreloc == NULL)
00893               {
00894                 const char * name;
00895 
00896                 name = (bfd_elf_string_from_elf_section
00897                        (abfd,
00898                         elf_elfheader (abfd)->e_shstrndx,
00899                         elf_section_data (sec)->rel_hdr.sh_name));
00900                 if (name == NULL)
00901                   return FALSE;
00902 
00903                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
00904                            && strcmp (bfd_get_section_name (abfd, sec),
00905                                     name + 5) == 0);
00906 
00907                 sreloc = bfd_get_section_by_name (dynobj, name);
00908                 if (sreloc == NULL)
00909                   {
00910                     flagword flags;
00911 
00912                     flags = (SEC_HAS_CONTENTS | SEC_READONLY
00913                             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
00914                     if ((sec->flags & SEC_ALLOC) != 0)
00915                      flags |= SEC_ALLOC | SEC_LOAD;
00916                     sreloc = bfd_make_section_with_flags (dynobj,
00917                                                      name,
00918                                                      flags);
00919                     if (sreloc == NULL
00920                        || ! bfd_set_section_alignment (dynobj, sreloc, 2))
00921                      return FALSE;
00922                   }
00923               }
00924 
00925              sreloc->size += sizeof (Elf32_External_Rela);
00926            }
00927 
00928          break;
00929        }
00930     }
00931 
00932   return TRUE;
00933 }
00934 
00935 /* Return the section that should be marked against GC for a given
00936    relocation.  */
00937 
00938 static asection *
00939 mn10300_elf_gc_mark_hook (asection *sec,
00940                        struct bfd_link_info *info,
00941                        Elf_Internal_Rela *rel,
00942                        struct elf_link_hash_entry *h,
00943                        Elf_Internal_Sym *sym)
00944 {
00945   if (h != NULL)
00946     switch (ELF32_R_TYPE (rel->r_info))
00947       {
00948       case R_MN10300_GNU_VTINHERIT:
00949       case R_MN10300_GNU_VTENTRY:
00950        return NULL;
00951       }
00952 
00953   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
00954 }
00955 
00956 /* Perform a relocation as part of a final link.  */
00957 static bfd_reloc_status_type
00958 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
00959                              input_section, contents, offset, value,
00960                              addend, h, symndx, info, sym_sec, is_local)
00961      reloc_howto_type *howto;
00962      bfd *input_bfd;
00963      bfd *output_bfd ATTRIBUTE_UNUSED;
00964      asection *input_section;
00965      bfd_byte *contents;
00966      bfd_vma offset;
00967      bfd_vma value;
00968      bfd_vma addend;
00969      struct elf_link_hash_entry * h;
00970      unsigned long symndx;
00971      struct bfd_link_info *info;
00972      asection *sym_sec ATTRIBUTE_UNUSED;
00973      int is_local ATTRIBUTE_UNUSED;
00974 {
00975   unsigned long r_type = howto->type;
00976   bfd_byte *hit_data = contents + offset;
00977   bfd *      dynobj;
00978   bfd_vma *  local_got_offsets;
00979   asection * sgot;
00980   asection * splt;
00981   asection * sreloc;
00982 
00983   dynobj = elf_hash_table (info)->dynobj;
00984   local_got_offsets = elf_local_got_offsets (input_bfd);
00985 
00986   sgot   = NULL;
00987   splt   = NULL;
00988   sreloc = NULL;
00989 
00990   switch (r_type)
00991     {
00992     case R_MN10300_24:
00993     case R_MN10300_16:
00994     case R_MN10300_8:
00995     case R_MN10300_PCREL8:
00996     case R_MN10300_PCREL16:
00997     case R_MN10300_PCREL32:
00998     case R_MN10300_GOTOFF32:
00999     case R_MN10300_GOTOFF24:
01000     case R_MN10300_GOTOFF16:
01001       if (info->shared
01002          && (input_section->flags & SEC_ALLOC) != 0
01003          && h != NULL
01004          && ! SYMBOL_REFERENCES_LOCAL (info, h))
01005        return bfd_reloc_dangerous;
01006     }
01007 
01008   switch (r_type)
01009     {
01010     case R_MN10300_NONE:
01011       return bfd_reloc_ok;
01012 
01013     case R_MN10300_32:
01014       if (info->shared
01015          && (input_section->flags & SEC_ALLOC) != 0)
01016        {
01017          Elf_Internal_Rela outrel;
01018          bfd_boolean skip, relocate;
01019 
01020          /* When generating a shared object, these relocations are
01021             copied into the output file to be resolved at run
01022             time.  */
01023          if (sreloc == NULL)
01024            {
01025              const char * name;
01026 
01027              name = (bfd_elf_string_from_elf_section
01028                     (input_bfd,
01029                      elf_elfheader (input_bfd)->e_shstrndx,
01030                      elf_section_data (input_section)->rel_hdr.sh_name));
01031              if (name == NULL)
01032               return FALSE;
01033 
01034              BFD_ASSERT (CONST_STRNEQ (name, ".rela")
01035                        && strcmp (bfd_get_section_name (input_bfd,
01036                                                     input_section),
01037                                  name + 5) == 0);
01038 
01039              sreloc = bfd_get_section_by_name (dynobj, name);
01040              BFD_ASSERT (sreloc != NULL);
01041            }
01042 
01043          skip = FALSE;
01044 
01045          outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
01046                                                input_section, offset);
01047          if (outrel.r_offset == (bfd_vma) -1)
01048            skip = TRUE;
01049 
01050          outrel.r_offset += (input_section->output_section->vma
01051                            + input_section->output_offset);
01052 
01053          if (skip)
01054            {
01055              memset (&outrel, 0, sizeof outrel);
01056              relocate = FALSE;
01057            }
01058          else
01059            {
01060              /* h->dynindx may be -1 if this symbol was marked to
01061                become local.  */
01062              if (h == NULL
01063                 || SYMBOL_REFERENCES_LOCAL (info, h))
01064               {
01065                 relocate = TRUE;
01066                 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
01067                 outrel.r_addend = value + addend;
01068               }
01069              else
01070               {
01071                 BFD_ASSERT (h->dynindx != -1);
01072                 relocate = FALSE;
01073                 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
01074                 outrel.r_addend = value + addend;
01075               }
01076            }
01077 
01078          bfd_elf32_swap_reloca_out (output_bfd, &outrel,
01079                                  (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
01080                                              + sreloc->reloc_count));
01081          ++sreloc->reloc_count;
01082 
01083          /* If this reloc is against an external symbol, we do
01084             not want to fiddle with the addend.  Otherwise, we
01085             need to include the symbol value so that it becomes
01086             an addend for the dynamic reloc.  */
01087          if (! relocate)
01088            return bfd_reloc_ok;
01089        }
01090       value += addend;
01091       bfd_put_32 (input_bfd, value, hit_data);
01092       return bfd_reloc_ok;
01093 
01094     case R_MN10300_24:
01095       value += addend;
01096 
01097       if ((long) value > 0x7fffff || (long) value < -0x800000)
01098        return bfd_reloc_overflow;
01099 
01100       bfd_put_8 (input_bfd, value & 0xff, hit_data);
01101       bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
01102       bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
01103       return bfd_reloc_ok;
01104 
01105     case R_MN10300_16:
01106       value += addend;
01107 
01108       if ((long) value > 0x7fff || (long) value < -0x8000)
01109        return bfd_reloc_overflow;
01110 
01111       bfd_put_16 (input_bfd, value, hit_data);
01112       return bfd_reloc_ok;
01113 
01114     case R_MN10300_8:
01115       value += addend;
01116 
01117       if ((long) value > 0x7f || (long) value < -0x80)
01118        return bfd_reloc_overflow;
01119 
01120       bfd_put_8 (input_bfd, value, hit_data);
01121       return bfd_reloc_ok;
01122 
01123     case R_MN10300_PCREL8:
01124       value -= (input_section->output_section->vma
01125               + input_section->output_offset);
01126       value -= offset;
01127       value += addend;
01128 
01129       if ((long) value > 0xff || (long) value < -0x100)
01130        return bfd_reloc_overflow;
01131 
01132       bfd_put_8 (input_bfd, value, hit_data);
01133       return bfd_reloc_ok;
01134 
01135     case R_MN10300_PCREL16:
01136       value -= (input_section->output_section->vma
01137               + input_section->output_offset);
01138       value -= offset;
01139       value += addend;
01140 
01141       if ((long) value > 0xffff || (long) value < -0x10000)
01142        return bfd_reloc_overflow;
01143 
01144       bfd_put_16 (input_bfd, value, hit_data);
01145       return bfd_reloc_ok;
01146 
01147     case R_MN10300_PCREL32:
01148       value -= (input_section->output_section->vma
01149               + input_section->output_offset);
01150       value -= offset;
01151       value += addend;
01152 
01153       bfd_put_32 (input_bfd, value, hit_data);
01154       return bfd_reloc_ok;
01155 
01156     case R_MN10300_GNU_VTINHERIT:
01157     case R_MN10300_GNU_VTENTRY:
01158       return bfd_reloc_ok;
01159 
01160     case R_MN10300_GOTPC32:
01161       /* Use global offset table as symbol value.  */
01162 
01163       value = bfd_get_section_by_name (dynobj,
01164                                    ".got")->output_section->vma;
01165       value -= (input_section->output_section->vma
01166               + input_section->output_offset);
01167       value -= offset;
01168       value += addend;
01169 
01170       bfd_put_32 (input_bfd, value, hit_data);
01171       return bfd_reloc_ok;
01172 
01173     case R_MN10300_GOTPC16:
01174       /* Use global offset table as symbol value.  */
01175 
01176       value = bfd_get_section_by_name (dynobj,
01177                                    ".got")->output_section->vma;
01178       value -= (input_section->output_section->vma
01179               + input_section->output_offset);
01180       value -= offset;
01181       value += addend;
01182 
01183       if ((long) value > 0xffff || (long) value < -0x10000)
01184        return bfd_reloc_overflow;
01185 
01186       bfd_put_16 (input_bfd, value, hit_data);
01187       return bfd_reloc_ok;
01188 
01189     case R_MN10300_GOTOFF32:
01190       value -= bfd_get_section_by_name (dynobj,
01191                                    ".got")->output_section->vma;
01192       value += addend;
01193 
01194       bfd_put_32 (input_bfd, value, hit_data);
01195       return bfd_reloc_ok;
01196 
01197     case R_MN10300_GOTOFF24:
01198       value -= bfd_get_section_by_name (dynobj,
01199                                    ".got")->output_section->vma;
01200       value += addend;
01201 
01202       if ((long) value > 0x7fffff || (long) value < -0x800000)
01203        return bfd_reloc_overflow;
01204 
01205       bfd_put_8 (input_bfd, value, hit_data);
01206       bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
01207       bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
01208       return bfd_reloc_ok;
01209 
01210     case R_MN10300_GOTOFF16:
01211       value -= bfd_get_section_by_name (dynobj,
01212                                    ".got")->output_section->vma;
01213       value += addend;
01214 
01215       if ((long) value > 0xffff || (long) value < -0x10000)
01216        return bfd_reloc_overflow;
01217 
01218       bfd_put_16 (input_bfd, value, hit_data);
01219       return bfd_reloc_ok;
01220 
01221     case R_MN10300_PLT32:
01222       if (h != NULL
01223          && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
01224          && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
01225          && h->plt.offset != (bfd_vma) -1)
01226        {
01227          asection * splt;
01228 
01229          splt = bfd_get_section_by_name (dynobj, ".plt");
01230 
01231          value = (splt->output_section->vma
01232                  + splt->output_offset
01233                  + h->plt.offset) - value;
01234        }
01235 
01236       value -= (input_section->output_section->vma
01237               + input_section->output_offset);
01238       value -= offset;
01239       value += addend;
01240 
01241       bfd_put_32 (input_bfd, value, hit_data);
01242       return bfd_reloc_ok;
01243 
01244     case R_MN10300_PLT16:
01245       if (h != NULL
01246          && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
01247          && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
01248          && h->plt.offset != (bfd_vma) -1)
01249        {
01250          asection * splt;
01251 
01252          splt = bfd_get_section_by_name (dynobj, ".plt");
01253 
01254          value = (splt->output_section->vma
01255                  + splt->output_offset
01256                  + h->plt.offset) - value;
01257        }
01258 
01259       value -= (input_section->output_section->vma
01260               + input_section->output_offset);
01261       value -= offset;
01262       value += addend;
01263 
01264       if ((long) value > 0xffff || (long) value < -0x10000)
01265        return bfd_reloc_overflow;
01266 
01267       bfd_put_16 (input_bfd, value, hit_data);
01268       return bfd_reloc_ok;
01269 
01270     case R_MN10300_GOT32:
01271     case R_MN10300_GOT24:
01272     case R_MN10300_GOT16:
01273       {
01274        asection * sgot;
01275 
01276        sgot = bfd_get_section_by_name (dynobj, ".got");
01277 
01278          if (h != NULL)
01279            {
01280              bfd_vma off;
01281 
01282              off = h->got.offset;
01283              BFD_ASSERT (off != (bfd_vma) -1);
01284 
01285              if (! elf_hash_table (info)->dynamic_sections_created
01286                 || SYMBOL_REFERENCES_LOCAL (info, h))
01287               /* This is actually a static link, or it is a
01288                  -Bsymbolic link and the symbol is defined
01289                  locally, or the symbol was forced to be local
01290                  because of a version file.  We must initialize
01291                  this entry in the global offset table.
01292 
01293                  When doing a dynamic link, we create a .rela.got
01294                  relocation entry to initialize the value.  This
01295                  is done in the finish_dynamic_symbol routine.  */
01296               bfd_put_32 (output_bfd, value,
01297                          sgot->contents + off);
01298 
01299              value = sgot->output_offset + off;
01300            }
01301          else
01302            {
01303              bfd_vma off;
01304 
01305              off = elf_local_got_offsets (input_bfd)[symndx];
01306 
01307              bfd_put_32 (output_bfd, value, sgot->contents + off);
01308 
01309              if (info->shared)
01310               {
01311                 asection * srelgot;
01312                 Elf_Internal_Rela outrel;
01313 
01314                 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
01315                 BFD_ASSERT (srelgot != NULL);
01316 
01317                 outrel.r_offset = (sgot->output_section->vma
01318                                  + sgot->output_offset
01319                                  + off);
01320                 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
01321                 outrel.r_addend = value;
01322                 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
01323                                         (bfd_byte *) (((Elf32_External_Rela *)
01324                                                      srelgot->contents)
01325                                                     + srelgot->reloc_count));
01326                 ++ srelgot->reloc_count;
01327               }
01328 
01329              value = sgot->output_offset + off;
01330            }
01331       }
01332 
01333       value += addend;
01334 
01335       if (r_type == R_MN10300_GOT32)
01336        {
01337          bfd_put_32 (input_bfd, value, hit_data);
01338          return bfd_reloc_ok;
01339        }
01340       else if (r_type == R_MN10300_GOT24)
01341        {
01342          if ((long) value > 0x7fffff || (long) value < -0x800000)
01343            return bfd_reloc_overflow;
01344 
01345          bfd_put_8 (input_bfd, value & 0xff, hit_data);
01346          bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
01347          bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
01348          return bfd_reloc_ok;
01349        }
01350       else if (r_type == R_MN10300_GOT16)
01351        {
01352          if ((long) value > 0xffff || (long) value < -0x10000)
01353            return bfd_reloc_overflow;
01354 
01355          bfd_put_16 (input_bfd, value, hit_data);
01356          return bfd_reloc_ok;
01357        }
01358       /* Fall through.  */
01359 
01360     default:
01361       return bfd_reloc_notsupported;
01362     }
01363 }
01364 
01365 /* Relocate an MN10300 ELF section.  */
01366 static bfd_boolean
01367 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
01368                            contents, relocs, local_syms, local_sections)
01369      bfd *output_bfd;
01370      struct bfd_link_info *info;
01371      bfd *input_bfd;
01372      asection *input_section;
01373      bfd_byte *contents;
01374      Elf_Internal_Rela *relocs;
01375      Elf_Internal_Sym *local_syms;
01376      asection **local_sections;
01377 {
01378   Elf_Internal_Shdr *symtab_hdr;
01379   struct elf_link_hash_entry **sym_hashes;
01380   Elf_Internal_Rela *rel, *relend;
01381 
01382   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
01383   sym_hashes = elf_sym_hashes (input_bfd);
01384 
01385   rel = relocs;
01386   relend = relocs + input_section->reloc_count;
01387   for (; rel < relend; rel++)
01388     {
01389       int r_type;
01390       reloc_howto_type *howto;
01391       unsigned long r_symndx;
01392       Elf_Internal_Sym *sym;
01393       asection *sec;
01394       struct elf32_mn10300_link_hash_entry *h;
01395       bfd_vma relocation;
01396       bfd_reloc_status_type r;
01397 
01398       r_symndx = ELF32_R_SYM (rel->r_info);
01399       r_type = ELF32_R_TYPE (rel->r_info);
01400       howto = elf_mn10300_howto_table + r_type;
01401 
01402       /* Just skip the vtable gc relocs.  */
01403       if (r_type == R_MN10300_GNU_VTINHERIT
01404          || r_type == R_MN10300_GNU_VTENTRY)
01405        continue;
01406 
01407       h = NULL;
01408       sym = NULL;
01409       sec = NULL;
01410       if (r_symndx < symtab_hdr->sh_info)
01411        {
01412          sym = local_syms + r_symndx;
01413          sec = local_sections[r_symndx];
01414          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
01415        }
01416       else
01417        {
01418          bfd_boolean unresolved_reloc;
01419          bfd_boolean warned;
01420          struct elf_link_hash_entry *hh;
01421 
01422          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
01423                                r_symndx, symtab_hdr, sym_hashes,
01424                                hh, sec, relocation,
01425                                unresolved_reloc, warned);
01426 
01427          h = (struct elf32_mn10300_link_hash_entry *) hh;
01428 
01429          if ((h->root.root.type == bfd_link_hash_defined
01430              || h->root.root.type == bfd_link_hash_defweak)
01431              && (   r_type == R_MN10300_GOTPC32
01432                 || r_type == R_MN10300_GOTPC16
01433                 || ((   r_type == R_MN10300_PLT32
01434                      || r_type == R_MN10300_PLT16)
01435                     && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
01436                     && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
01437                     && h->root.plt.offset != (bfd_vma) -1)
01438                 || ((   r_type == R_MN10300_GOT32
01439                      || r_type == R_MN10300_GOT24
01440                      || r_type == R_MN10300_GOT16)
01441                     && elf_hash_table (info)->dynamic_sections_created
01442                     && !SYMBOL_REFERENCES_LOCAL (info, hh))
01443                 || (r_type == R_MN10300_32
01444                     && !SYMBOL_REFERENCES_LOCAL (info, hh)
01445                     && ((input_section->flags & SEC_ALLOC) != 0
01446                        /* DWARF will emit R_MN10300_32 relocations
01447                           in its sections against symbols defined
01448                           externally in shared libraries.  We can't
01449                           do anything with them here.  */
01450                        || ((input_section->flags & SEC_DEBUGGING) != 0
01451                            && h->root.def_dynamic)))))
01452            /* In these cases, we don't need the relocation
01453               value.  We check specially because in some
01454               obscure cases sec->output_section will be NULL.  */
01455            relocation = 0;
01456 
01457          else if (!info->relocatable && unresolved_reloc)
01458            (*_bfd_error_handler)
01459              (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
01460               input_bfd,
01461               input_section,
01462               (long) rel->r_offset,
01463               howto->name,
01464               h->root.root.root.string);
01465        }
01466 
01467       if (sec != NULL && elf_discarded_section (sec))
01468        {
01469          /* For relocs against symbols from removed linkonce sections,
01470             or sections discarded by a linker script, we just want the
01471             section contents zeroed.  Avoid any special processing.  */
01472          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
01473          rel->r_info = 0;
01474          rel->r_addend = 0;
01475          continue;
01476        }
01477 
01478       if (info->relocatable)
01479        continue;
01480 
01481       r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
01482                                       input_section,
01483                                       contents, rel->r_offset,
01484                                       relocation, rel->r_addend,
01485                                       (struct elf_link_hash_entry *)h,
01486                                       r_symndx,
01487                                       info, sec, h == NULL);
01488 
01489       if (r != bfd_reloc_ok)
01490        {
01491          const char *name;
01492          const char *msg = (const char *) 0;
01493 
01494          if (h != NULL)
01495            name = h->root.root.root.string;
01496          else
01497            {
01498              name = (bfd_elf_string_from_elf_section
01499                     (input_bfd, symtab_hdr->sh_link, sym->st_name));
01500              if (name == NULL || *name == '\0')
01501               name = bfd_section_name (input_bfd, sec);
01502            }
01503 
01504          switch (r)
01505            {
01506            case bfd_reloc_overflow:
01507              if (! ((*info->callbacks->reloc_overflow)
01508                    (info, (h ? &h->root.root : NULL), name,
01509                     howto->name, (bfd_vma) 0, input_bfd,
01510                     input_section, rel->r_offset)))
01511               return FALSE;
01512              break;
01513 
01514            case bfd_reloc_undefined:
01515              if (! ((*info->callbacks->undefined_symbol)
01516                    (info, name, input_bfd, input_section,
01517                     rel->r_offset, TRUE)))
01518               return FALSE;
01519              break;
01520 
01521            case bfd_reloc_outofrange:
01522              msg = _("internal error: out of range error");
01523              goto common_error;
01524 
01525            case bfd_reloc_notsupported:
01526              msg = _("internal error: unsupported relocation error");
01527              goto common_error;
01528 
01529            case bfd_reloc_dangerous:
01530              if (r_type == R_MN10300_PCREL32)
01531               msg = _("error: inappropriate relocation type for shared"
01532                      " library (did you forget -fpic?)");
01533              else
01534               msg = _("internal error: suspicious relocation type used"
01535                      " in shared library");
01536              goto common_error;
01537 
01538            default:
01539              msg = _("internal error: unknown error");
01540              /* fall through */
01541 
01542            common_error:
01543              if (!((*info->callbacks->warning)
01544                   (info, msg, name, input_bfd, input_section,
01545                    rel->r_offset)))
01546               return FALSE;
01547              break;
01548            }
01549        }
01550     }
01551 
01552   return TRUE;
01553 }
01554 
01555 /* Finish initializing one hash table entry.  */
01556 static bfd_boolean
01557 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
01558      struct bfd_hash_entry *gen_entry;
01559      PTR in_args;
01560 {
01561   struct elf32_mn10300_link_hash_entry *entry;
01562   struct bfd_link_info *link_info = (struct bfd_link_info *)in_args;
01563   unsigned int byte_count = 0;
01564 
01565   entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
01566 
01567   if (entry->root.root.type == bfd_link_hash_warning)
01568     entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
01569 
01570   /* If we already know we want to convert "call" to "calls" for calls
01571      to this symbol, then return now.  */
01572   if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
01573     return TRUE;
01574 
01575   /* If there are no named calls to this symbol, or there's nothing we
01576      can move from the function itself into the "call" instruction,
01577      then note that all "call" instructions should be converted into
01578      "calls" instructions and return.  If a symbol is available for
01579      dynamic symbol resolution (overridable or overriding), avoid
01580      custom calling conventions.  */
01581   if (entry->direct_calls == 0
01582       || (entry->stack_size == 0 && entry->movm_args == 0)
01583       || (elf_hash_table (link_info)->dynamic_sections_created
01584          && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
01585          && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
01586     {
01587       /* Make a note that we should convert "call" instructions to "calls"
01588         instructions for calls to this symbol.  */
01589       entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
01590       return TRUE;
01591     }
01592 
01593   /* We may be able to move some instructions from the function itself into
01594      the "call" instruction.  Count how many bytes we might be able to
01595      eliminate in the function itself.  */
01596 
01597   /* A movm instruction is two bytes.  */
01598   if (entry->movm_args)
01599     byte_count += 2;
01600 
01601   /* Count the insn to allocate stack space too.  */
01602   if (entry->stack_size > 0)
01603     {
01604       if (entry->stack_size <= 128)
01605        byte_count += 3;
01606       else
01607        byte_count += 4;
01608     }
01609 
01610   /* If using "call" will result in larger code, then turn all
01611      the associated "call" instructions into "calls" instructions.  */
01612   if (byte_count < entry->direct_calls)
01613     entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
01614 
01615   /* This routine never fails.  */
01616   return TRUE;
01617 }
01618 
01619 /* Used to count hash table entries.  */
01620 static bfd_boolean
01621 elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
01622                                    PTR in_args)
01623 {
01624   int *count = (int *)in_args;
01625 
01626   (*count) ++;
01627   return TRUE;
01628 }
01629 
01630 /* Used to enumerate hash table entries into a linear array.  */
01631 static bfd_boolean
01632 elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
01633                                    PTR in_args)
01634 {
01635   struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
01636 
01637   **ptr = gen_entry;
01638   (*ptr) ++;
01639   return TRUE;
01640 }
01641 
01642 /* Used to sort the array created by the above.  */
01643 static int
01644 sort_by_value (const void *va, const void *vb)
01645 {
01646   struct elf32_mn10300_link_hash_entry *a
01647     = *(struct elf32_mn10300_link_hash_entry **)va;
01648   struct elf32_mn10300_link_hash_entry *b
01649     = *(struct elf32_mn10300_link_hash_entry **)vb;
01650 
01651   return a->value - b->value;
01652 }
01653 
01654 
01655 /* This function handles relaxing for the mn10300.
01656 
01657    There are quite a few relaxing opportunities available on the mn10300:
01658 
01659        * calls:32 -> calls:16                                     2 bytes
01660        * call:32  -> call:16                                      2 bytes
01661 
01662        * call:32 -> calls:32                                      1 byte
01663        * call:16 -> calls:16                                      1 byte
01664               * These are done anytime using "calls" would result
01665               in smaller code, or when necessary to preserve the
01666               meaning of the program.
01667 
01668        * call:32                                           varies
01669        * call:16
01670               * In some circumstances we can move instructions
01671               from a function prologue into a "call" instruction.
01672               This is only done if the resulting code is no larger
01673               than the original code.
01674 
01675        * jmp:32 -> jmp:16                                  2 bytes
01676        * jmp:16 -> bra:8                                   1 byte
01677 
01678               * If the previous instruction is a conditional branch
01679               around the jump/bra, we may be able to reverse its condition
01680               and change its target to the jump's target.  The jump/bra
01681               can then be deleted.                         2 bytes
01682 
01683        * mov abs32 -> mov abs16                            1 or 2 bytes
01684 
01685        * Most instructions which accept imm32 can relax to imm16  1 or 2 bytes
01686        - Most instructions which accept imm16 can relax to imm8   1 or 2 bytes
01687 
01688        * Most instructions which accept d32 can relax to d16      1 or 2 bytes
01689        - Most instructions which accept d16 can relax to d8       1 or 2 bytes
01690 
01691        We don't handle imm16->imm8 or d16->d8 as they're very rare
01692        and somewhat more difficult to support.  */
01693 
01694 static bfd_boolean
01695 mn10300_elf_relax_section (abfd, sec, link_info, again)
01696      bfd *abfd;
01697      asection *sec;
01698      struct bfd_link_info *link_info;
01699      bfd_boolean *again;
01700 {
01701   Elf_Internal_Shdr *symtab_hdr;
01702   Elf_Internal_Rela *internal_relocs = NULL;
01703   Elf_Internal_Rela *irel, *irelend;
01704   bfd_byte *contents = NULL;
01705   Elf_Internal_Sym *isymbuf = NULL;
01706   struct elf32_mn10300_link_hash_table *hash_table;
01707   asection *section = sec;
01708 
01709   /* Assume nothing changes.  */
01710   *again = FALSE;
01711 
01712   /* We need a pointer to the mn10300 specific hash table.  */
01713   hash_table = elf32_mn10300_hash_table (link_info);
01714 
01715   /* Initialize fields in each hash table entry the first time through.  */
01716   if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
01717     {
01718       bfd *input_bfd;
01719 
01720       /* Iterate over all the input bfds.  */
01721       for (input_bfd = link_info->input_bfds;
01722           input_bfd != NULL;
01723           input_bfd = input_bfd->link_next)
01724        {
01725          /* We're going to need all the symbols for each bfd.  */
01726          symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
01727          if (symtab_hdr->sh_info != 0)
01728            {
01729              isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
01730              if (isymbuf == NULL)
01731               isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
01732                                           symtab_hdr->sh_info, 0,
01733                                           NULL, NULL, NULL);
01734              if (isymbuf == NULL)
01735               goto error_return;
01736            }
01737 
01738          /* Iterate over each section in this bfd.  */
01739          for (section = input_bfd->sections;
01740               section != NULL;
01741               section = section->next)
01742            {
01743              struct elf32_mn10300_link_hash_entry *hash;
01744              Elf_Internal_Sym *sym;
01745              asection *sym_sec = NULL;
01746              const char *sym_name;
01747              char *new_name;
01748 
01749              /* If there's nothing to do in this section, skip it.  */
01750              if (! ((section->flags & SEC_RELOC) != 0
01751                    && section->reloc_count != 0))
01752               continue;
01753              if ((section->flags & SEC_ALLOC) == 0)
01754               continue;
01755 
01756              /* Get cached copy of section contents if it exists.  */
01757              if (elf_section_data (section)->this_hdr.contents != NULL)
01758               contents = elf_section_data (section)->this_hdr.contents;
01759              else if (section->size != 0)
01760               {
01761                 /* Go get them off disk.  */
01762                 if (!bfd_malloc_and_get_section (input_bfd, section,
01763                                              &contents))
01764                   goto error_return;
01765               }
01766              else
01767               contents = NULL;
01768 
01769              /* If there aren't any relocs, then there's nothing to do.  */
01770              if ((section->flags & SEC_RELOC) != 0
01771                 && section->reloc_count != 0)
01772               {
01773 
01774                 /* Get a copy of the native relocations.  */
01775                 internal_relocs = (_bfd_elf_link_read_relocs
01776                                  (input_bfd, section, (PTR) NULL,
01777                                   (Elf_Internal_Rela *) NULL,
01778                                   link_info->keep_memory));
01779                 if (internal_relocs == NULL)
01780                   goto error_return;
01781 
01782                 /* Now examine each relocation.  */
01783                 irel = internal_relocs;
01784                 irelend = irel + section->reloc_count;
01785                 for (; irel < irelend; irel++)
01786                   {
01787                     long r_type;
01788                     unsigned long r_index;
01789                     unsigned char code;
01790 
01791                     r_type = ELF32_R_TYPE (irel->r_info);
01792                     r_index = ELF32_R_SYM (irel->r_info);
01793 
01794                     if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
01795                      goto error_return;
01796 
01797                     /* We need the name and hash table entry of the target
01798                       symbol!  */
01799                     hash = NULL;
01800                     sym = NULL;
01801                     sym_sec = NULL;
01802 
01803                     if (r_index < symtab_hdr->sh_info)
01804                      {
01805                        /* A local symbol.  */
01806                        Elf_Internal_Sym *isym;
01807                        struct elf_link_hash_table *elftab;
01808                        bfd_size_type amt;
01809 
01810                        isym = isymbuf + r_index;
01811                        if (isym->st_shndx == SHN_UNDEF)
01812                          sym_sec = bfd_und_section_ptr;
01813                        else if (isym->st_shndx == SHN_ABS)
01814                          sym_sec = bfd_abs_section_ptr;
01815                        else if (isym->st_shndx == SHN_COMMON)
01816                          sym_sec = bfd_com_section_ptr;
01817                        else
01818                          sym_sec
01819                            = bfd_section_from_elf_index (input_bfd,
01820                                                      isym->st_shndx);
01821 
01822                        sym_name
01823                          = bfd_elf_string_from_elf_section (input_bfd,
01824                                                         (symtab_hdr
01825                                                         ->sh_link),
01826                                                         isym->st_name);
01827 
01828                        /* If it isn't a function, then we don't care
01829                           about it.  */
01830                        if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
01831                          continue;
01832 
01833                        /* Tack on an ID so we can uniquely identify this
01834                           local symbol in the global hash table.  */
01835                        amt = strlen (sym_name) + 10;
01836                        new_name = bfd_malloc (amt);
01837                        if (new_name == 0)
01838                          goto error_return;
01839 
01840                        sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
01841                        sym_name = new_name;
01842 
01843                        elftab = &hash_table->static_hash_table->root;
01844                        hash = ((struct elf32_mn10300_link_hash_entry *)
01845                               elf_link_hash_lookup (elftab, sym_name,
01846                                                  TRUE, TRUE, FALSE));
01847                        free (new_name);
01848                      }
01849                     else
01850                      {
01851                        r_index -= symtab_hdr->sh_info;
01852                        hash = (struct elf32_mn10300_link_hash_entry *)
01853                                elf_sym_hashes (input_bfd)[r_index];
01854                      }
01855 
01856                     sym_name = hash->root.root.root.string;
01857                     if ((section->flags & SEC_CODE) != 0)
01858                      {
01859                        /* If this is not a "call" instruction, then we
01860                           should convert "call" instructions to "calls"
01861                           instructions.  */
01862                        code = bfd_get_8 (input_bfd,
01863                                        contents + irel->r_offset - 1);
01864                        if (code != 0xdd && code != 0xcd)
01865                          hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
01866                      }
01867 
01868                     /* If this is a jump/call, then bump the
01869                       direct_calls counter.  Else force "call" to
01870                       "calls" conversions.  */
01871                     if (r_type == R_MN10300_PCREL32
01872                        || r_type == R_MN10300_PLT32
01873                        || r_type == R_MN10300_PLT16
01874                        || r_type == R_MN10300_PCREL16)
01875                      hash->direct_calls++;
01876                     else
01877                      hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
01878                   }
01879               }
01880 
01881              /* Now look at the actual contents to get the stack size,
01882                and a list of what registers were saved in the prologue
01883                (ie movm_args).  */
01884              if ((section->flags & SEC_CODE) != 0)
01885               {
01886                 Elf_Internal_Sym *isym, *isymend;
01887                 unsigned int sec_shndx;
01888                 struct elf_link_hash_entry **hashes;
01889                 struct elf_link_hash_entry **end_hashes;
01890                 unsigned int symcount;
01891 
01892                 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
01893                                                          section);
01894 
01895                 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
01896                            - symtab_hdr->sh_info);
01897                 hashes = elf_sym_hashes (input_bfd);
01898                 end_hashes = hashes + symcount;
01899 
01900                 /* Look at each function defined in this section and
01901                    update info for that function.  */
01902                 isymend = isymbuf + symtab_hdr->sh_info;
01903                 for (isym = isymbuf; isym < isymend; isym++)
01904                   {
01905                     if (isym->st_shndx == sec_shndx
01906                        && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
01907                      {
01908                        struct elf_link_hash_table *elftab;
01909                        bfd_size_type amt;
01910                        struct elf_link_hash_entry **lhashes = hashes;
01911 
01912                        /* Skip a local symbol if it aliases a
01913                           global one.  */
01914                        for (; lhashes < end_hashes; lhashes++)
01915                          {
01916                            hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
01917                            if ((hash->root.root.type == bfd_link_hash_defined
01918                                || hash->root.root.type == bfd_link_hash_defweak)
01919                               && hash->root.root.u.def.section == section
01920                               && hash->root.type == STT_FUNC
01921                               && hash->root.root.u.def.value == isym->st_value)
01922                             break;
01923                          }
01924                        if (lhashes != end_hashes)
01925                          continue;
01926 
01927                        if (isym->st_shndx == SHN_UNDEF)
01928                          sym_sec = bfd_und_section_ptr;
01929                        else if (isym->st_shndx == SHN_ABS)
01930                          sym_sec = bfd_abs_section_ptr;
01931                        else if (isym->st_shndx == SHN_COMMON)
01932                          sym_sec = bfd_com_section_ptr;
01933                        else
01934                          sym_sec
01935                            = bfd_section_from_elf_index (input_bfd,
01936                                                      isym->st_shndx);
01937 
01938                        sym_name = (bfd_elf_string_from_elf_section
01939                                   (input_bfd, symtab_hdr->sh_link,
01940                                    isym->st_name));
01941 
01942                        /* Tack on an ID so we can uniquely identify this
01943                           local symbol in the global hash table.  */
01944                        amt = strlen (sym_name) + 10;
01945                        new_name = bfd_malloc (amt);
01946                        if (new_name == 0)
01947                          goto error_return;
01948 
01949                        sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
01950                        sym_name = new_name;
01951 
01952                        elftab = &hash_table->static_hash_table->root;
01953                        hash = ((struct elf32_mn10300_link_hash_entry *)
01954                               elf_link_hash_lookup (elftab, sym_name,
01955                                                  TRUE, TRUE, FALSE));
01956                        free (new_name);
01957                        compute_function_info (input_bfd, hash,
01958                                            isym->st_value, contents);
01959                        hash->value = isym->st_value;
01960                      }
01961                   }
01962 
01963                 for (; hashes < end_hashes; hashes++)
01964                   {
01965                     hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
01966                     if ((hash->root.root.type == bfd_link_hash_defined
01967                         || hash->root.root.type == bfd_link_hash_defweak)
01968                        && hash->root.root.u.def.section == section
01969                        && hash->root.type == STT_FUNC)
01970                      compute_function_info (input_bfd, hash,
01971                                           (hash)->root.root.u.def.value,
01972                                           contents);
01973                   }
01974               }
01975 
01976              /* Cache or free any memory we allocated for the relocs.  */
01977              if (internal_relocs != NULL
01978                 && elf_section_data (section)->relocs != internal_relocs)
01979               free (internal_relocs);
01980              internal_relocs = NULL;
01981 
01982              /* Cache or free any memory we allocated for the contents.  */
01983              if (contents != NULL
01984                 && elf_section_data (section)->this_hdr.contents != contents)
01985               {
01986                 if (! link_info->keep_memory)
01987                   free (contents);
01988                 else
01989                   {
01990                     /* Cache the section contents for elf_link_input_bfd.  */
01991                     elf_section_data (section)->this_hdr.contents = contents;
01992                   }
01993               }
01994              contents = NULL;
01995            }
01996 
01997          /* Cache or free any memory we allocated for the symbols.  */
01998          if (isymbuf != NULL
01999              && symtab_hdr->contents != (unsigned char *) isymbuf)
02000            {
02001              if (! link_info->keep_memory)
02002               free (isymbuf);
02003              else
02004               {
02005                 /* Cache the symbols for elf_link_input_bfd.  */
02006                 symtab_hdr->contents = (unsigned char *) isymbuf;
02007               }
02008            }
02009          isymbuf = NULL;
02010        }
02011 
02012       /* Now iterate on each symbol in the hash table and perform
02013         the final initialization steps on each.  */
02014       elf32_mn10300_link_hash_traverse (hash_table,
02015                                    elf32_mn10300_finish_hash_table_entry,
02016                                    link_info);
02017       elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
02018                                    elf32_mn10300_finish_hash_table_entry,
02019                                    link_info);
02020 
02021       {
02022        /* This section of code collects all our local symbols, sorts
02023           them by value, and looks for multiple symbols referring to
02024           the same address.  For those symbols, the flags are merged.
02025           At this point, the only flag that can be set is
02026           MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
02027           together.  */
02028        int static_count = 0, i;
02029        struct elf32_mn10300_link_hash_entry **entries;
02030        struct elf32_mn10300_link_hash_entry **ptr;
02031 
02032        elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
02033                                      elf32_mn10300_count_hash_table_entries,
02034                                      &static_count);
02035 
02036        entries = (struct elf32_mn10300_link_hash_entry **)
02037          bfd_malloc (static_count * sizeof (struct elf32_mn10300_link_hash_entry *));
02038 
02039        ptr = entries;
02040        elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
02041                                      elf32_mn10300_list_hash_table_entries,
02042                                      &ptr);
02043 
02044        qsort (entries, static_count, sizeof(entries[0]), sort_by_value);
02045 
02046        for (i=0; i<static_count-1; i++)
02047          if (entries[i]->value && entries[i]->value == entries[i+1]->value)
02048            {
02049              int v = entries[i]->flags;
02050              int j;
02051              for (j=i+1; j<static_count && entries[j]->value == entries[i]->value; j++)
02052               v |= entries[j]->flags;
02053              for (j=i; j<static_count && entries[j]->value == entries[i]->value; j++)
02054               entries[j]->flags = v;
02055              i = j-1;
02056            }
02057       }
02058 
02059       /* All entries in the hash table are fully initialized.  */
02060       hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
02061 
02062       /* Now that everything has been initialized, go through each
02063         code section and delete any prologue insns which will be
02064         redundant because their operations will be performed by
02065         a "call" instruction.  */
02066       for (input_bfd = link_info->input_bfds;
02067           input_bfd != NULL;
02068           input_bfd = input_bfd->link_next)
02069        {
02070          /* We're going to need all the local symbols for each bfd.  */
02071          symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02072          if (symtab_hdr->sh_info != 0)
02073            {
02074              isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
02075              if (isymbuf == NULL)
02076               isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
02077                                           symtab_hdr->sh_info, 0,
02078                                           NULL, NULL, NULL);
02079              if (isymbuf == NULL)
02080               goto error_return;
02081            }
02082 
02083          /* Walk over each section in this bfd.  */
02084          for (section = input_bfd->sections;
02085               section != NULL;
02086               section = section->next)
02087            {
02088              unsigned int sec_shndx;
02089              Elf_Internal_Sym *isym, *isymend;
02090              struct elf_link_hash_entry **hashes;
02091              struct elf_link_hash_entry **end_hashes;
02092              unsigned int symcount;
02093 
02094              /* Skip non-code sections and empty sections.  */
02095              if ((section->flags & SEC_CODE) == 0 || section->size == 0)
02096               continue;
02097 
02098              if (section->reloc_count != 0)
02099               {
02100                 /* Get a copy of the native relocations.  */
02101                 internal_relocs = (_bfd_elf_link_read_relocs
02102                                  (input_bfd, section, (PTR) NULL,
02103                                   (Elf_Internal_Rela *) NULL,
02104                                   link_info->keep_memory));
02105                 if (internal_relocs == NULL)
02106                   goto error_return;
02107               }
02108 
02109              /* Get cached copy of section contents if it exists.  */
02110              if (elf_section_data (section)->this_hdr.contents != NULL)
02111               contents = elf_section_data (section)->this_hdr.contents;
02112              else
02113               {
02114                 /* Go get them off disk.  */
02115                 if (!bfd_malloc_and_get_section (input_bfd, section,
02116                                              &contents))
02117                   goto error_return;
02118               }
02119 
02120              sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
02121                                                       section);
02122 
02123              /* Now look for any function in this section which needs
02124                insns deleted from its prologue.  */
02125              isymend = isymbuf + symtab_hdr->sh_info;
02126              for (isym = isymbuf; isym < isymend; isym++)
02127               {
02128                 struct elf32_mn10300_link_hash_entry *sym_hash;
02129                 asection *sym_sec = NULL;
02130                 const char *sym_name;
02131                 char *new_name;
02132                 struct elf_link_hash_table *elftab;
02133                 bfd_size_type amt;
02134 
02135                 if (isym->st_shndx != sec_shndx)
02136                   continue;
02137 
02138                 if (isym->st_shndx == SHN_UNDEF)
02139                   sym_sec = bfd_und_section_ptr;
02140                 else if (isym->st_shndx == SHN_ABS)
02141                   sym_sec = bfd_abs_section_ptr;
02142                 else if (isym->st_shndx == SHN_COMMON)
02143                   sym_sec = bfd_com_section_ptr;
02144                 else
02145                   sym_sec
02146                     = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
02147 
02148                 sym_name
02149                   = bfd_elf_string_from_elf_section (input_bfd,
02150                                                  symtab_hdr->sh_link,
02151                                                  isym->st_name);
02152 
02153                 /* Tack on an ID so we can uniquely identify this
02154                    local symbol in the global hash table.  */
02155                 amt = strlen (sym_name) + 10;
02156                 new_name = bfd_malloc (amt);
02157                 if (new_name == 0)
02158                   goto error_return;
02159                 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
02160                 sym_name = new_name;
02161 
02162                 elftab = &hash_table->static_hash_table->root;
02163                 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
02164                            elf_link_hash_lookup (elftab, sym_name,
02165                                               FALSE, FALSE, FALSE));
02166 
02167                 free (new_name);
02168                 if (sym_hash == NULL)
02169                   continue;
02170 
02171                 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
02172                     && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
02173                   {
02174                     int bytes = 0;
02175 
02176                     /* Note that we've changed things.  */
02177                     elf_section_data (section)->relocs = internal_relocs;
02178                     elf_section_data (section)->this_hdr.contents = contents;
02179                     symtab_hdr->contents = (unsigned char *) isymbuf;
02180 
02181                     /* Count how many bytes we're going to delete.  */
02182                     if (sym_hash->movm_args)
02183                      bytes += 2;
02184 
02185                     if (sym_hash->stack_size > 0)
02186                      {
02187                        if (sym_hash->stack_size <= 128)
02188                          bytes += 3;
02189                        else
02190                          bytes += 4;
02191                      }
02192 
02193                     /* Note that we've deleted prologue bytes for this
02194                       function.  */
02195                     sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
02196 
02197                     /* Actually delete the bytes.  */
02198                     if (!mn10300_elf_relax_delete_bytes (input_bfd,
02199                                                     section,
02200                                                     isym->st_value,
02201                                                     bytes))
02202                      goto error_return;
02203 
02204                     /* Something changed.  Not strictly necessary, but
02205                       may lead to more relaxing opportunities.  */
02206                     *again = TRUE;
02207                   }
02208               }
02209 
02210              /* Look for any global functions in this section which
02211                need insns deleted from their prologues.  */
02212              symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
02213                        - symtab_hdr->sh_info);
02214              hashes = elf_sym_hashes (input_bfd);
02215              end_hashes = hashes + symcount;
02216              for (; hashes < end_hashes; hashes++)
02217               {
02218                 struct elf32_mn10300_link_hash_entry *sym_hash;
02219 
02220                 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
02221                 if ((sym_hash->root.root.type == bfd_link_hash_defined
02222                      || sym_hash->root.root.type == bfd_link_hash_defweak)
02223                     && sym_hash->root.root.u.def.section == section
02224                     && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
02225                     && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
02226                   {
02227                     int bytes = 0;
02228                     bfd_vma symval;
02229 
02230                     /* Note that we've changed things.  */
02231                     elf_section_data (section)->relocs = internal_relocs;
02232                     elf_section_data (section)->this_hdr.contents = contents;
02233                     symtab_hdr->contents = (unsigned char *) isymbuf;
02234 
02235                     /* Count how many bytes we're going to delete.  */
02236                     if (sym_hash->movm_args)
02237                      bytes += 2;
02238 
02239                     if (sym_hash->stack_size > 0)
02240                      {
02241                        if (sym_hash->stack_size <= 128)
02242                          bytes += 3;
02243                        else
02244                          bytes += 4;
02245                      }
02246 
02247                     /* Note that we've deleted prologue bytes for this
02248                       function.  */
02249                     sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
02250 
02251                     /* Actually delete the bytes.  */
02252                     symval = sym_hash->root.root.u.def.value;
02253                     if (!mn10300_elf_relax_delete_bytes (input_bfd,
02254                                                     section,
02255                                                     symval,
02256                                                     bytes))
02257                      goto error_return;
02258 
02259                     /* Something changed.  Not strictly necessary, but
02260                       may lead to more relaxing opportunities.  */
02261                     *again = TRUE;
02262                   }
02263               }
02264 
02265              /* Cache or free any memory we allocated for the relocs.  */
02266              if (internal_relocs != NULL
02267                 && elf_section_data (section)->relocs != internal_relocs)
02268               free (internal_relocs);
02269              internal_relocs = NULL;
02270 
02271              /* Cache or free any memory we allocated for the contents.  */
02272              if (contents != NULL
02273                 && elf_section_data (section)->this_hdr.contents != contents)
02274               {
02275                 if (! link_info->keep_memory)
02276                   free (contents);
02277                 else
02278                   {
02279                     /* Cache the section contents for elf_link_input_bfd.  */
02280                     elf_section_data (section)->this_hdr.contents = contents;
02281                   }
02282               }
02283              contents = NULL;
02284            }
02285 
02286          /* Cache or free any memory we allocated for the symbols.  */
02287          if (isymbuf != NULL
02288              && symtab_hdr->contents != (unsigned char *) isymbuf)
02289            {
02290              if (! link_info->keep_memory)
02291               free (isymbuf);
02292              else
02293               {
02294                 /* Cache the symbols for elf_link_input_bfd.  */
02295                 symtab_hdr->contents = (unsigned char *) isymbuf;
02296               }
02297            }
02298          isymbuf = NULL;
02299        }
02300     }
02301 
02302   /* (Re)initialize for the basic instruction shortening/relaxing pass.  */
02303   contents = NULL;
02304   internal_relocs = NULL;
02305   isymbuf = NULL;
02306   /* For error_return.  */
02307   section = sec;
02308 
02309   /* We don't have to do anything for a relocatable link, if
02310      this section does not have relocs, or if this is not a
02311      code section.  */
02312   if (link_info->relocatable
02313       || (sec->flags & SEC_RELOC) == 0
02314       || sec->reloc_count == 0
02315       || (sec->flags & SEC_CODE) == 0)
02316     return TRUE;
02317 
02318   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
02319 
02320   /* Get a copy of the native relocations.  */
02321   internal_relocs = (_bfd_elf_link_read_relocs
02322                    (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
02323                     link_info->keep_memory));
02324   if (internal_relocs == NULL)
02325     goto error_return;
02326 
02327   /* Walk through them looking for relaxing opportunities.  */
02328   irelend = internal_relocs + sec->reloc_count;
02329   for (irel = internal_relocs; irel < irelend; irel++)
02330     {
02331       bfd_vma symval;
02332       struct elf32_mn10300_link_hash_entry *h = NULL;
02333 
02334       /* If this isn't something that can be relaxed, then ignore
02335         this reloc.  */
02336       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
02337          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
02338          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
02339        continue;
02340 
02341       /* Get the section contents if we haven't done so already.  */
02342       if (contents == NULL)
02343        {
02344          /* Get cached copy if it exists.  */
02345          if (elf_section_data (sec)->this_hdr.contents != NULL)
02346            contents = elf_section_data (sec)->this_hdr.contents;
02347          else
02348            {
02349              /* Go get them off disk.  */
02350              if (!bfd_malloc_and_get_section (abfd, sec, &contents))
02351               goto error_return;
02352            }
02353        }
02354 
02355       /* Read this BFD's symbols if we haven't done so already.  */
02356       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
02357        {
02358          isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
02359          if (isymbuf == NULL)
02360            isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
02361                                        symtab_hdr->sh_info, 0,
02362                                        NULL, NULL, NULL);
02363          if (isymbuf == NULL)
02364            goto error_return;
02365        }
02366 
02367       /* Get the value of the symbol referred to by the reloc.  */
02368       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
02369        {
02370          Elf_Internal_Sym *isym;
02371          asection *sym_sec = NULL;
02372          const char *sym_name;
02373          char *new_name;
02374          bfd_vma saved_addend;
02375 
02376          /* A local symbol.  */
02377          isym = isymbuf + ELF32_R_SYM (irel->r_info);
02378          if (isym->st_shndx == SHN_UNDEF)
02379            sym_sec = bfd_und_section_ptr;
02380          else if (isym->st_shndx == SHN_ABS)
02381            sym_sec = bfd_abs_section_ptr;
02382          else if (isym->st_shndx == SHN_COMMON)
02383            sym_sec = bfd_com_section_ptr;
02384          else
02385            sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
02386 
02387          sym_name = bfd_elf_string_from_elf_section (abfd,
02388                                                 symtab_hdr->sh_link,
02389                                                 isym->st_name);
02390 
02391          if ((sym_sec->flags & SEC_MERGE)
02392              && ELF_ST_TYPE (isym->st_info) == STT_SECTION
02393              && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
02394            {
02395              saved_addend = irel->r_addend;
02396              symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
02397              symval += irel->r_addend;
02398              irel->r_addend = saved_addend;
02399            }
02400          else
02401            {
02402              symval = (isym->st_value
02403                      + sym_sec->output_section->vma
02404                      + sym_sec->output_offset);
02405            }
02406          /* Tack on an ID so we can uniquely identify this
02407             local symbol in the global hash table.  */
02408          new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
02409          if (new_name == 0)
02410            goto error_return;
02411          sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
02412          sym_name = new_name;
02413 
02414          h = (struct elf32_mn10300_link_hash_entry *)
02415               elf_link_hash_lookup (&hash_table->static_hash_table->root,
02416                                   sym_name, FALSE, FALSE, FALSE);
02417          free (new_name);
02418        }
02419       else
02420        {
02421          unsigned long indx;
02422 
02423          /* An external symbol.  */
02424          indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
02425          h = (struct elf32_mn10300_link_hash_entry *)
02426               (elf_sym_hashes (abfd)[indx]);
02427          BFD_ASSERT (h != NULL);
02428          if (h->root.root.type != bfd_link_hash_defined
02429              && h->root.root.type != bfd_link_hash_defweak)
02430            {
02431              /* This appears to be a reference to an undefined
02432               symbol.  Just ignore it--it will be caught by the
02433               regular reloc processing.  */
02434              continue;
02435            }
02436 
02437          symval = (h->root.root.u.def.value
02438                   + h->root.root.u.def.section->output_section->vma
02439                   + h->root.root.u.def.section->output_offset);
02440        }
02441 
02442       /* For simplicity of coding, we are going to modify the section
02443         contents, the section relocs, and the BFD symbol table.  We
02444         must tell the rest of the code not to free up this
02445         information.  It would be possible to instead create a table
02446         of changes which have to be made, as is done in coff-mips.c;
02447         that would be more work, but would require less memory when
02448         the linker is run.  */
02449 
02450       /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
02451         branch/call, also deal with "call" -> "calls" conversions and
02452         insertion of prologue data into "call" instructions.  */
02453       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
02454          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
02455        {
02456          bfd_vma value = symval;
02457 
02458          if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
02459              && h != NULL
02460              && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
02461              && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
02462              && h->root.plt.offset != (bfd_vma) -1)
02463            {
02464              asection * splt;
02465 
02466              splt = bfd_get_section_by_name (elf_hash_table (link_info)
02467                                          ->dynobj, ".plt");
02468 
02469              value = ((splt->output_section->vma
02470                      + splt->output_offset
02471                      + h->root.plt.offset)
02472                      - (sec->output_section->vma
02473                        + sec->output_offset
02474                        + irel->r_offset));
02475            }
02476 
02477          /* If we've got a "call" instruction that needs to be turned
02478             into a "calls" instruction, do so now.  It saves a byte.  */
02479          if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
02480            {
02481              unsigned char code;
02482 
02483              /* Get the opcode.  */
02484              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
02485 
02486              /* Make sure we're working with a "call" instruction!  */
02487              if (code == 0xdd)
02488               {
02489                 /* Note that we've changed the relocs, section contents,
02490                    etc.  */
02491                 elf_section_data (sec)->relocs = internal_relocs;
02492                 elf_section_data (sec)->this_hdr.contents = contents;
02493                 symtab_hdr->contents = (unsigned char *) isymbuf;
02494 
02495                 /* Fix the opcode.  */
02496                 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
02497                 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
02498 
02499                 /* Fix irel->r_offset and irel->r_addend.  */
02500                 irel->r_offset += 1;
02501                 irel->r_addend += 1;
02502 
02503                 /* Delete one byte of data.  */
02504                 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
02505                                                  irel->r_offset + 3, 1))
02506                   goto error_return;
02507 
02508                 /* That will change things, so, we should relax again.
02509                    Note that this is not required, and it may be slow.  */
02510                 *again = TRUE;
02511               }
02512            }
02513          else if (h)
02514            {
02515              /* We've got a "call" instruction which needs some data
02516                from target function filled in.  */
02517              unsigned char code;
02518 
02519              /* Get the opcode.  */
02520              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
02521 
02522              /* Insert data from the target function into the "call"
02523                instruction if needed.  */
02524              if (code == 0xdd)
02525               {
02526                 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
02527                 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
02528                           contents + irel->r_offset + 5);
02529               }
02530            }
02531 
02532          /* Deal with pc-relative gunk.  */
02533          value -= (sec->output_section->vma + sec->output_offset);
02534          value -= irel->r_offset;
02535          value += irel->r_addend;
02536 
02537          /* See if the value will fit in 16 bits, note the high value is
02538             0x7fff + 2 as the target will be two bytes closer if we are
02539             able to relax.  */
02540          if ((long) value < 0x8001 && (long) value > -0x8000)
02541            {
02542              unsigned char code;
02543 
02544              /* Get the opcode.  */
02545              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
02546 
02547              if (code != 0xdc && code != 0xdd && code != 0xff)
02548               continue;
02549 
02550              /* Note that we've changed the relocs, section contents, etc.  */
02551              elf_section_data (sec)->relocs = internal_relocs;
02552              elf_section_data (sec)->this_hdr.contents = contents;
02553              symtab_hdr->contents = (unsigned char *) isymbuf;
02554 
02555              /* Fix the opcode.  */
02556              if (code == 0xdc)
02557               bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
02558              else if (code == 0xdd)
02559               bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
02560              else if (code == 0xff)
02561               bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
02562 
02563              /* Fix the relocation's type.  */
02564              irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
02565                                       (ELF32_R_TYPE (irel->r_info)
02566                                        == (int) R_MN10300_PLT32)
02567                                       ? R_MN10300_PLT16 :
02568                                       R_MN10300_PCREL16);
02569 
02570              /* Delete two bytes of data.  */
02571              if (!mn10300_elf_relax_delete_bytes (abfd, sec,
02572                                              irel->r_offset + 1, 2))
02573               goto error_return;
02574 
02575              /* That will change things, so, we should relax again.
02576                Note that this is not required, and it may be slow.  */
02577              *again = TRUE;
02578            }
02579        }
02580 
02581       /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
02582         branch.  */
02583       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
02584        {
02585          bfd_vma value = symval;
02586 
02587          /* If we've got a "call" instruction that needs to be turned
02588             into a "calls" instruction, do so now.  It saves a byte.  */
02589          if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
02590            {
02591              unsigned char code;
02592 
02593              /* Get the opcode.  */
02594              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
02595 
02596              /* Make sure we're working with a "call" instruction!  */
02597              if (code == 0xcd)
02598               {
02599                 /* Note that we've changed the relocs, section contents,
02600                    etc.  */
02601                 elf_section_data (sec)->relocs = internal_relocs;
02602                 elf_section_data (sec)->this_hdr.contents = contents;
02603                 symtab_hdr->contents = (unsigned char *) isymbuf;
02604 
02605                 /* Fix the opcode.  */
02606                 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
02607                 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
02608 
02609                 /* Fix irel->r_offset and irel->r_addend.  */
02610                 irel->r_offset += 1;
02611                 irel->r_addend += 1;
02612 
02613                 /* Delete one byte of data.  */
02614                 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
02615                                                  irel->r_offset + 1, 1))
02616                   goto error_return;
02617 
02618                 /* That will change things, so, we should relax again.
02619                    Note that this is not required, and it may be slow.  */
02620                 *again = TRUE;
02621               }
02622            }
02623          else if (h)
02624            {
02625              unsigned char code;
02626 
02627              /* Get the opcode.  */
02628              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
02629 
02630              /* Insert data from the target function into the "call"
02631                instruction if needed.  */
02632              if (code == 0xcd)
02633               {
02634                 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
02635                 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
02636                           contents + irel->r_offset + 3);
02637               }
02638            }
02639 
02640          /* Deal with pc-relative gunk.  */
02641          value -= (sec->output_section->vma + sec->output_offset);
02642          value -= irel->r_offset;
02643          value += irel->r_addend;
02644 
02645          /* See if the value will fit in 8 bits, note the high value is
02646             0x7f + 1 as the target will be one bytes closer if we are
02647             able to relax.  */
02648          if ((long) value < 0x80 && (long) value > -0x80)
02649            {
02650              unsigned char code;
02651 
02652              /* Get the opcode.  */
02653              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
02654 
02655              if (code != 0xcc)
02656               continue;
02657 
02658              /* Note that we've changed the relocs, section contents, etc.  */
02659              elf_section_data (sec)->relocs = internal_relocs;
02660              elf_section_data (sec)->this_hdr.contents = contents;
02661              symtab_hdr->contents = (unsigned char *) isymbuf;
02662 
02663              /* Fix the opcode.  */
02664              bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
02665 
02666              /* Fix the relocation's type.  */
02667              irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
02668                                       R_MN10300_PCREL8);
02669 
02670              /* Delete one byte of data.  */
02671              if (!mn10300_elf_relax_delete_bytes (abfd, sec,
02672                                              irel->r_offset + 1, 1))
02673               goto error_return;
02674 
02675              /* That will change things, so, we should relax again.
02676                Note that this is not required, and it may be slow.  */
02677              *again = TRUE;
02678            }
02679        }
02680 
02681       /* Try to eliminate an unconditional 8 bit pc-relative branch
02682         which immediately follows a conditional 8 bit pc-relative
02683         branch around the unconditional branch.
02684 
02685            original:        new:
02686            bCC lab1         bCC' lab2
02687            bra lab2
02688           lab1:             lab1:
02689 
02690         This happens when the bCC can't reach lab2 at assembly time,
02691         but due to other relaxations it can reach at link time.  */
02692       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
02693        {
02694          Elf_Internal_Rela *nrel;
02695          bfd_vma value = symval;
02696          unsigned char code;
02697 
02698          /* Deal with pc-relative gunk.  */
02699          value -= (sec->output_section->vma + sec->output_offset);
02700          value -= irel->r_offset;
02701          value += irel->r_addend;
02702 
02703          /* Do nothing if this reloc is the last byte in the section.  */
02704          if (irel->r_offset == sec->size)
02705            continue;
02706 
02707          /* See if the next instruction is an unconditional pc-relative
02708             branch, more often than not this test will fail, so we
02709             test it first to speed things up.  */
02710          code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
02711          if (code != 0xca)
02712            continue;
02713 
02714          /* Also make sure the next relocation applies to the next
02715             instruction and that it's a pc-relative 8 bit branch.  */
02716          nrel = irel + 1;
02717          if (nrel == irelend
02718              || irel->r_offset + 2 != nrel->r_offset
02719              || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
02720            continue;
02721 
02722          /* Make sure our destination immediately follows the
02723             unconditional branch.  */
02724          if (symval != (sec->output_section->vma + sec->output_offset
02725                       + irel->r_offset + 3))
02726            continue;
02727 
02728          /* Now make sure we are a conditional branch.  This may not
02729             be necessary, but why take the chance.
02730 
02731             Note these checks assume that R_MN10300_PCREL8 relocs
02732             only occur on bCC and bCCx insns.  If they occured
02733             elsewhere, we'd need to know the start of this insn
02734             for this check to be accurate.  */
02735          code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
02736          if (code != 0xc0 && code != 0xc1 && code != 0xc2
02737              && code != 0xc3 && code != 0xc4 && code != 0xc5
02738              && code != 0xc6 && code != 0xc7 && code != 0xc8
02739              && code != 0xc9 && code != 0xe8 && code != 0xe9
02740              && code != 0xea && code != 0xeb)
02741            continue;
02742 
02743          /* We also have to be sure there is no symbol/label
02744             at the unconditional branch.  */
02745          if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
02746                                        irel->r_offset + 1))
02747            continue;
02748 
02749          /* Note that we've changed the relocs, section contents, etc.  */
02750          elf_section_data (sec)->relocs = internal_relocs;
02751          elf_section_data (sec)->this_hdr.contents = contents;
02752          symtab_hdr->contents = (unsigned char *) isymbuf;
02753 
02754          /* Reverse the condition of the first branch.  */
02755          switch (code)
02756            {
02757            case 0xc8:
02758              code = 0xc9;
02759              break;
02760            case 0xc9:
02761              code = 0xc8;
02762              break;
02763            case 0xc0:
02764              code = 0xc2;
02765              break;
02766            case 0xc2:
02767              code = 0xc0;
02768              break;
02769            case 0xc3:
02770              code = 0xc1;
02771              break;
02772            case 0xc1:
02773              code = 0xc3;
02774              break;
02775            case 0xc4:
02776              code = 0xc6;
02777              break;
02778            case 0xc6:
02779              code = 0xc4;
02780              break;
02781            case 0xc7:
02782              code = 0xc5;
02783              break;
02784            case 0xc5:
02785              code = 0xc7;
02786              break;
02787            case 0xe8:
02788              code = 0xe9;
02789              break;
02790            case 0x9d:
02791              code = 0xe8;
02792              break;
02793            case 0xea:
02794              code = 0xeb;
02795              break;
02796            case 0xeb:
02797              code = 0xea;
02798              break;
02799            }
02800          bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
02801 
02802          /* Set the reloc type and symbol for the first branch
02803             from the second branch.  */
02804          irel->r_info = nrel->r_info;
02805 
02806          /* Make the reloc for the second branch a null reloc.  */
02807          nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
02808                                    R_MN10300_NONE);
02809 
02810          /* Delete two bytes of data.  */
02811          if (!mn10300_elf_relax_delete_bytes (abfd, sec,
02812                                           irel->r_offset + 1, 2))
02813            goto error_return;
02814 
02815          /* That will change things, so, we should relax again.
02816             Note that this is not required, and it may be slow.  */
02817          *again = TRUE;
02818        }
02819 
02820       /* Try to turn a 24 immediate, displacement or absolute address
02821         into a 8 immediate, displacement or absolute address.  */
02822       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
02823        {
02824          bfd_vma value = symval;
02825          value += irel->r_addend;
02826 
02827          /* See if the value will fit in 8 bits.  */
02828          if ((long) value < 0x7f && (long) value > -0x80)
02829            {
02830              unsigned char code;
02831 
02832              /* AM33 insns which have 24 operands are 6 bytes long and
02833                will have 0xfd as the first byte.  */
02834 
02835              /* Get the first opcode.  */
02836              code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
02837 
02838              if (code == 0xfd)
02839               {
02840                 /* Get the second opcode.  */
02841                 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
02842 
02843                 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
02844                    equivalent instructions exists.  */
02845                 if (code != 0x6b && code != 0x7b
02846                     && code != 0x8b && code != 0x9b
02847                     && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
02848                        || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
02849                        || (code & 0x0f) == 0x0e))
02850                   {
02851                     /* Not safe if the high bit is on as relaxing may
02852                       move the value out of high mem and thus not fit
02853                       in a signed 8bit value.  This is currently over
02854                       conservative.  */
02855                     if ((value & 0x80) == 0)
02856                      {
02857                        /* Note that we've changed the relocation contents,
02858                           etc.  */
02859                        elf_section_data (sec)->relocs = internal_relocs;
02860                        elf_section_data (sec)->this_hdr.contents = contents;
02861                        symtab_hdr->contents = (unsigned char *) isymbuf;
02862 
02863                        /* Fix the opcode.  */
02864                        bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
02865                        bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
02866 
02867                        /* Fix the relocation's type.  */
02868                        irel->r_info =
02869                          ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
02870                                      R_MN10300_8);
02871 
02872                        /* Delete two bytes of data.  */
02873                        if (!mn10300_elf_relax_delete_bytes (abfd, sec,
02874                                                         irel->r_offset + 1, 2))
02875                          goto error_return;
02876 
02877                        /* That will change things, so, we should relax
02878                           again.  Note that this is not required, and it
02879                           may be slow.  */
02880                        *again = TRUE;
02881                        break;
02882                      }
02883                   }
02884               }
02885            }
02886        }
02887 
02888       /* Try to turn a 32bit immediate, displacement or absolute address
02889         into a 16bit immediate, displacement or absolute address.  */
02890       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
02891          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
02892          || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
02893        {
02894          bfd_vma value = symval;
02895 
02896          if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
02897            {
02898              asection * sgot;
02899 
02900              sgot = bfd_get_section_by_name (elf_hash_table (link_info)
02901                                          ->dynobj, ".got");
02902 
02903              if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
02904               {
02905                 value = sgot->output_offset;
02906 
02907                 if (h)
02908                   value += h->root.got.offset;
02909                 else
02910                   value += (elf_local_got_offsets
02911                            (abfd)[ELF32_R_SYM (irel->r_info)]);
02912               }
02913              else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
02914               value -= sgot->output_section->vma;
02915              else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
02916               value = (sgot->output_section->vma
02917                       - (sec->output_section->vma
02918                          + sec->output_offset
02919                          + irel->r_offset));
02920              else
02921               abort ();
02922            }
02923 
02924          value += irel->r_addend;
02925 
02926          /* See if the value will fit in 24 bits.
02927             We allow any 16bit match here.  We prune those we can't
02928             handle below.  */
02929          if ((long) value < 0x7fffff && (long) value > -0x800000)
02930            {
02931              unsigned char code;
02932 
02933              /* AM33 insns which have 32bit operands are 7 bytes long and
02934                will have 0xfe as the first byte.  */
02935 
02936              /* Get the first opcode.  */
02937              code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
02938 
02939              if (code == 0xfe)
02940               {
02941                 /* Get the second opcode.  */
02942                 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
02943 
02944                 /* All the am33 32 -> 24 relaxing possibilities.  */
02945                 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
02946                    equivalent instructions exists.  */
02947                 if (code != 0x6b && code != 0x7b
02948                     && code != 0x8b && code != 0x9b
02949                     && (ELF32_R_TYPE (irel->r_info)
02950                        != (int) R_MN10300_GOTPC32)
02951                     && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
02952                        || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
02953                        || (code & 0x0f) == 0x0e))
02954                   {
02955                     /* Not safe if the high bit is on as relaxing may
02956                       move the value out of high mem and thus not fit
02957                       in a signed 16bit value.  This is currently over
02958                       conservative.  */
02959                     if ((value & 0x8000) == 0)
02960                      {
02961                        /* Note that we've changed the relocation contents,
02962                           etc.  */
02963                        elf_section_data (sec)->relocs = internal_relocs;
02964                        elf_section_data (sec)->this_hdr.contents = contents;
02965                        symtab_hdr->contents = (unsigned char *) isymbuf;
02966 
02967                        /* Fix the opcode.  */
02968                        bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
02969                        bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
02970 
02971                        /* Fix the relocation's type.  */
02972                        irel->r_info =
02973                          ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
02974                                      (ELF32_R_TYPE (irel->r_info)
02975                                       == (int) R_MN10300_GOTOFF32)
02976                                      ? R_MN10300_GOTOFF24
02977                                      : (ELF32_R_TYPE (irel->r_info)
02978                                         == (int) R_MN10300_GOT32)
02979                                      ? R_MN10300_GOT24 :
02980                                      R_MN10300_24);
02981 
02982                        /* Delete one byte of data.  */
02983                        if (!mn10300_elf_relax_delete_bytes (abfd, sec,
02984                                                         irel->r_offset + 3, 1))
02985                          goto error_return;
02986 
02987                        /* That will change things, so, we should relax
02988                           again.  Note that this is not required, and it
02989                           may be slow.  */
02990                        *again = TRUE;
02991                        break;
02992                      }
02993                   }
02994               }
02995            }
02996 
02997          /* See if the value will fit in 16 bits.
02998             We allow any 16bit match here.  We prune those we can't
02999             handle below.  */
03000          if ((long) value < 0x7fff && (long) value > -0x8000)
03001            {
03002              unsigned char code;
03003 
03004              /* Most insns which have 32bit operands are 6 bytes long;
03005                exceptions are pcrel insns and bit insns.
03006 
03007                We handle pcrel insns above.  We don't bother trying
03008                to handle the bit insns here.
03009 
03010                The first byte of the remaining insns will be 0xfc.  */
03011 
03012              /* Get the first opcode.  */
03013              code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
03014 
03015              if (code != 0xfc)
03016               continue;
03017 
03018              /* Get the second opcode.  */
03019              code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
03020 
03021              if ((code & 0xf0) < 0x80)
03022               switch (code & 0xf0)
03023                 {
03024                 /* mov (d32,am),dn   -> mov (d32,am),dn
03025                    mov dm,(d32,am)   -> mov dn,(d32,am)
03026                    mov (d32,am),an   -> mov (d32,am),an
03027                    mov dm,(d32,am)   -> mov dn,(d32,am)
03028                    movbu (d32,am),dn -> movbu (d32,am),dn
03029                    movbu dm,(d32,am) -> movbu dn,(d32,am)
03030                    movhu (d32,am),dn -> movhu (d32,am),dn
03031                    movhu dm,(d32,am) -> movhu dn,(d32,am) */
03032                 case 0x00:
03033                 case 0x10:
03034                 case 0x20:
03035                 case 0x30:
03036                 case 0x40:
03037                 case 0x50:
03038                 case 0x60:
03039                 case 0x70:
03040                   /* Not safe if the high bit is on as relaxing may
03041                      move the value out of high mem and thus not fit
03042                      in a signed 16bit value.  */
03043                   if (code == 0xcc
03044                      && (value & 0x8000))
03045                     continue;
03046 
03047                   /* Note that we've changed the relocation contents, etc.  */
03048                   elf_section_data (sec)->relocs = internal_relocs;
03049                   elf_section_data (sec)->this_hdr.contents = contents;
03050                   symtab_hdr->contents = (unsigned char *) isymbuf;
03051 
03052                   /* Fix the opcode.  */
03053                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
03054                   bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
03055 
03056                   /* Fix the relocation's type.  */
03057                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
03058                                            (ELF32_R_TYPE (irel->r_info)
03059                                             == (int) R_MN10300_GOTOFF32)
03060                                            ? R_MN10300_GOTOFF16
03061                                            : (ELF32_R_TYPE (irel->r_info)
03062                                               == (int) R_MN10300_GOT32)
03063                                            ? R_MN10300_GOT16
03064                                            : (ELF32_R_TYPE (irel->r_info)
03065                                               == (int) R_MN10300_GOTPC32)
03066                                            ? R_MN10300_GOTPC16 :
03067                                            R_MN10300_16);
03068 
03069                   /* Delete two bytes of data.  */
03070                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
03071                                                   irel->r_offset + 2, 2))
03072                     goto error_return;
03073 
03074                   /* That will change things, so, we should relax again.
03075                      Note that this is not required, and it may be slow.  */
03076                   *again = TRUE;
03077                   break;
03078                 }
03079              else if ((code & 0xf0) == 0x80
03080                      || (code & 0xf0) == 0x90)
03081               switch (code & 0xf3)
03082                 {
03083                 /* mov dn,(abs32)   -> mov dn,(abs16)
03084                    movbu dn,(abs32) -> movbu dn,(abs16)
03085                    movhu dn,(abs32) -> movhu dn,(abs16)  */
03086                 case 0x81:
03087                 case 0x82:
03088                 case 0x83:
03089                   /* Note that we've changed the relocation contents, etc.  */
03090                   elf_section_data (sec)->relocs = internal_relocs;
03091                   elf_section_data (sec)->this_hdr.contents = contents;
03092                   symtab_hdr->contents = (unsigned char *) isymbuf;
03093 
03094                   if ((code & 0xf3) == 0x81)
03095                     code = 0x01 + (code & 0x0c);
03096                   else if ((code & 0xf3) == 0x82)
03097                     code = 0x02 + (code & 0x0c);
03098                   else if ((code & 0xf3) == 0x83)
03099                     code = 0x03 + (code & 0x0c);
03100                   else
03101                     abort ();
03102 
03103                   /* Fix the opcode.  */
03104                   bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
03105 
03106                   /* Fix the relocation's type.  */
03107                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
03108                                            (ELF32_R_TYPE (irel->r_info)
03109                                             == (int) R_MN10300_GOTOFF32)
03110                                            ? R_MN10300_GOTOFF16
03111                                            : (ELF32_R_TYPE (irel->r_info)
03112                                               == (int) R_MN10300_GOT32)
03113                                            ? R_MN10300_GOT16
03114                                            : (ELF32_R_TYPE (irel->r_info)
03115                                               == (int) R_MN10300_GOTPC32)
03116                                            ? R_MN10300_GOTPC16 :
03117                                            R_MN10300_16);
03118 
03119                   /* The opcode got shorter too, so we have to fix the
03120                      addend and offset too!  */
03121                   irel->r_offset -= 1;
03122 
03123                   /* Delete three bytes of data.  */
03124                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
03125                                                   irel->r_offset + 1, 3))
03126                     goto error_return;
03127 
03128                   /* That will change things, so, we should relax again.
03129                      Note that this is not required, and it may be slow.  */
03130                   *again = TRUE;
03131                   break;
03132 
03133                 /* mov am,(abs32)    -> mov am,(abs16)
03134                    mov am,(d32,sp)   -> mov am,(d16,sp)
03135                    mov dm,(d32,sp)   -> mov dm,(d32,sp)
03136                    movbu dm,(d32,sp) -> movbu dm,(d32,sp)
03137                    movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
03138                 case 0x80:
03139                 case 0x90:
03140                 case 0x91:
03141                 case 0x92:
03142                 case 0x93:
03143                   /* sp-based offsets are zero-extended.  */
03144                   if (code >= 0x90 && code <= 0x93
03145                      && (long)value < 0)
03146                     continue;
03147 
03148                   /* Note that we've changed the relocation contents, etc.  */
03149                   elf_section_data (sec)->relocs = internal_relocs;
03150                   elf_section_data (sec)->this_hdr.contents = contents;
03151                   symtab_hdr->contents = (unsigned char *) isymbuf;
03152 
03153                   /* Fix the opcode.  */
03154                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
03155                   bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
03156 
03157                   /* Fix the relocation's type.  */
03158                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
03159                                            (ELF32_R_TYPE (irel->r_info)
03160                                             == (int) R_MN10300_GOTOFF32)
03161                                            ? R_MN10300_GOTOFF16
03162                                            : (ELF32_R_TYPE (irel->r_info)
03163                                               == (int) R_MN10300_GOT32)
03164                                            ? R_MN10300_GOT16
03165                                            : (ELF32_R_TYPE (irel->r_info)
03166                                               == (int) R_MN10300_GOTPC32)
03167                                            ? R_MN10300_GOTPC16 :
03168                                            R_MN10300_16);
03169 
03170                   /* Delete two bytes of data.  */
03171                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
03172                                                   irel->r_offset + 2, 2))
03173                     goto error_return;
03174 
03175                   /* That will change things, so, we should relax again.
03176                      Note that this is not required, and it may be slow.  */
03177                   *again = TRUE;
03178                   break;
03179                 }
03180              else if ((code & 0xf0) < 0xf0)
03181               switch (code & 0xfc)
03182                 {
03183                 /* mov imm32,dn     -> mov imm16,dn
03184                    mov imm32,an     -> mov imm16,an
03185                    mov (abs32),dn   -> mov (abs16),dn
03186                    movbu (abs32),dn -> movbu (abs16),dn
03187                    movhu (abs32),dn -> movhu (abs16),dn  */
03188                 case 0xcc:
03189                 case 0xdc:
03190                 case 0xa4:
03191                 case 0xa8:
03192                 case 0xac:
03193                   /* Not safe if the high bit is on as relaxing may
03194                      move the value out of high mem and thus not fit
03195                      in a signed 16bit value.  */
03196                   if (code == 0xcc
03197                      && (value & 0x8000))
03198                     continue;
03199 
03200                   /* mov imm16, an zero-extends the immediate.  */
03201                   if (code == 0xdc
03202                      && (long)value < 0)
03203                     continue;
03204 
03205                   /* Note that we've changed the relocation contents, etc.  */
03206                   elf_section_data (sec)->relocs = internal_relocs;
03207                   elf_section_data (sec)->this_hdr.contents = contents;
03208                   symtab_hdr->contents = (unsigned char *) isymbuf;
03209 
03210                   if ((code & 0xfc) == 0xcc)
03211                     code = 0x2c + (code & 0x03);
03212                   else if ((code & 0xfc) == 0xdc)
03213                     code = 0x24 + (code & 0x03);
03214                   else if ((code & 0xfc) == 0xa4)
03215                     code = 0x30 + (code & 0x03);
03216                   else if ((code & 0xfc) == 0xa8)
03217                     code = 0x34 + (code & 0x03);
03218                   else if ((code & 0xfc) == 0xac)
03219                     code = 0x38 + (code & 0x03);
03220                   else
03221                     abort ();
03222 
03223                   /* Fix the opcode.  */
03224                   bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
03225 
03226                   /* Fix the relocation's type.  */
03227                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
03228                                            (ELF32_R_TYPE (irel->r_info)
03229                                             == (int) R_MN10300_GOTOFF32)
03230                                            ? R_MN10300_GOTOFF16
03231                                            : (ELF32_R_TYPE (irel->r_info)
03232                                               == (int) R_MN10300_GOT32)
03233                                            ? R_MN10300_GOT16
03234                                            : (ELF32_R_TYPE (irel->r_info)
03235                                               == (int) R_MN10300_GOTPC32)
03236                                            ? R_MN10300_GOTPC16 :
03237                                            R_MN10300_16);
03238 
03239                   /* The opcode got shorter too, so we have to fix the
03240                      addend and offset too!  */
03241                   irel->r_offset -= 1;
03242 
03243                   /* Delete three bytes of data.  */
03244                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
03245                                                   irel->r_offset + 1, 3))
03246                     goto error_return;
03247 
03248                   /* That will change things, so, we should relax again.
03249                      Note that this is not required, and it may be slow.  */
03250                   *again = TRUE;
03251                   break;
03252 
03253                 /* mov (abs32),an    -> mov (abs16),an
03254                    mov (d32,sp),an   -> mov (d16,sp),an
03255                    mov (d32,sp),dn   -> mov (d16,sp),dn
03256                    movbu (d32,sp),dn -> movbu (d16,sp),dn
03257                    movhu (d32,sp),dn -> movhu (d16,sp),dn
03258                    add imm32,dn      -> add imm16,dn
03259                    cmp imm32,dn      -> cmp imm16,dn
03260                    add imm32,an      -> add imm16,an
03261                    cmp imm32,an      -> cmp imm16,an
03262                    and imm32,dn      -> and imm16,dn
03263                    or imm32,dn       -> or imm16,dn
03264                    xor imm32,dn      -> xor imm16,dn
03265                    btst imm32,dn     -> btst imm16,dn */
03266 
03267                 case 0xa0:
03268                 case 0xb0:
03269                 case 0xb1:
03270                 case 0xb2:
03271                 case 0xb3:
03272                 case 0xc0:
03273                 case 0xc8:
03274 
03275                 case 0xd0:
03276                 case 0xd8:
03277                 case 0xe0:
03278                 case 0xe1:
03279                 case 0xe2:
03280                 case 0xe3:
03281                   /* cmp imm16, an zero-extends the immediate.  */
03282                   if (code == 0xdc
03283                      && (long)value < 0)
03284                     continue;
03285 
03286                   /* So do sp-based offsets.  */
03287                   if (code >= 0xb0 && code <= 0xb3
03288                      && (long)value < 0)
03289                     continue;
03290 
03291                   /* Note that we've changed the relocation contents, etc.  */
03292                   elf_section_data (sec)->relocs = internal_relocs;
03293                   elf_section_data (sec)->this_hdr.contents = contents;
03294                   symtab_hdr->contents = (unsigned char *) isymbuf;
03295 
03296                   /* Fix the opcode.  */
03297                   bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
03298                   bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
03299 
03300                   /* Fix the relocation's type.  */
03301                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
03302                                            (ELF32_R_TYPE (irel->r_info)
03303                                             == (int) R_MN10300_GOTOFF32)
03304                                            ? R_MN10300_GOTOFF16
03305                                            : (ELF32_R_TYPE (irel->r_info)
03306                                               == (int) R_MN10300_GOT32)
03307                                            ? R_MN10300_GOT16
03308                                            : (ELF32_R_TYPE (irel->r_info)
03309                                               == (int) R_MN10300_GOTPC32)
03310                                            ? R_MN10300_GOTPC16 :
03311                                            R_MN10300_16);
03312 
03313                   /* Delete two bytes of data.  */
03314                   if (!mn10300_elf_relax_delete_bytes (abfd, sec,
03315                                                   irel->r_offset + 2, 2))
03316                     goto error_return;
03317 
03318                   /* That will change things, so, we should relax again.
03319                      Note that this is not required, and it may be slow.  */
03320                   *again = TRUE;
03321                   break;
03322                 }
03323              else if (code == 0xfe)
03324               {
03325                 /* add imm32,sp -> add imm16,sp  */
03326 
03327                 /* Note that we've changed the relocation contents, etc.  */
03328                 elf_section_data (sec)->relocs = internal_relocs;
03329                 elf_section_data (sec)->this_hdr.contents = contents;
03330                 symtab_hdr->contents = (unsigned char *) isymbuf;
03331 
03332                 /* Fix the opcode.  */
03333                 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
03334                 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
03335 
03336                 /* Fix the relocation's type.  */
03337                 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
03338                                           (ELF32_R_TYPE (irel->r_info)
03339                                           == (int) R_MN10300_GOT32)
03340                                           ? R_MN10300_GOT16
03341                                           : (ELF32_R_TYPE (irel->r_info)
03342                                             == (int) R_MN10300_GOTOFF32)
03343                                           ? R_MN10300_GOTOFF16
03344                                           : (ELF32_R_TYPE (irel->r_info)
03345                                             == (int) R_MN10300_GOTPC32)
03346                                           ? R_MN10300_GOTPC16 :
03347                                           R_MN10300_16);
03348 
03349                 /* Delete two bytes of data.  */
03350                 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
03351                                                  irel->r_offset + 2, 2))
03352                   goto error_return;
03353 
03354                 /* That will change things, so, we should relax again.
03355                    Note that this is not required, and it may be slow.  */
03356                 *again = TRUE;
03357                 break;
03358               }
03359            }
03360        }
03361     }
03362 
03363   if (isymbuf != NULL
03364       && symtab_hdr->contents != (unsigned char *) isymbuf)
03365     {
03366       if (! link_info->keep_memory)
03367        free (isymbuf);
03368       else
03369        {
03370          /* Cache the symbols for elf_link_input_bfd.  */
03371          symtab_hdr->contents = (unsigned char *) isymbuf;
03372        }
03373     }
03374 
03375   if (contents != NULL
03376       && elf_section_data (sec)->this_hdr.contents != contents)
03377     {
03378       if (! link_info->keep_memory)
03379        free (contents);
03380       else
03381        {
03382          /* Cache the section contents for elf_link_input_bfd.  */
03383          elf_section_data (sec)->this_hdr.contents = contents;
03384        }
03385     }
03386 
03387   if (internal_relocs != NULL
03388       && elf_section_data (sec)->relocs != internal_relocs)
03389     free (internal_relocs);
03390 
03391   return TRUE;
03392 
03393  error_return:
03394   if (isymbuf != NULL
03395       && symtab_hdr->contents != (unsigned char *) isymbuf)
03396     free (isymbuf);
03397   if (contents != NULL
03398       && elf_section_data (section)->this_hdr.contents != contents)
03399     free (contents);
03400   if (internal_relocs != NULL
03401       && elf_section_data (section)->relocs != internal_relocs)
03402     free (internal_relocs);
03403 
03404   return FALSE;
03405 }
03406 
03407 /* Compute the stack size and movm arguments for the function
03408    referred to by HASH at address ADDR in section with
03409    contents CONTENTS, store the information in the hash table.  */
03410 static void
03411 compute_function_info (abfd, hash, addr, contents)
03412      bfd *abfd;
03413      struct elf32_mn10300_link_hash_entry *hash;
03414      bfd_vma addr;
03415      unsigned char *contents;
03416 {
03417   unsigned char byte1, byte2;
03418   /* We only care about a very small subset of the possible prologue
03419      sequences here.  Basically we look for:
03420 
03421      movm [d2,d3,a2,a3],sp (optional)
03422      add <size>,sp (optional, and only for sizes which fit in an unsigned
03423                   8 bit number)
03424 
03425      If we find anything else, we quit.  */
03426 
03427   /* Look for movm [regs],sp */
03428   byte1 = bfd_get_8 (abfd, contents + addr);
03429   byte2 = bfd_get_8 (abfd, contents + addr + 1);
03430 
03431   if (byte1 == 0xcf)
03432     {
03433       hash->movm_args = byte2;
03434       addr += 2;
03435       byte1 = bfd_get_8 (abfd, contents + addr);
03436       byte2 = bfd_get_8 (abfd, contents + addr + 1);
03437     }
03438 
03439   /* Now figure out how much stack space will be allocated by the movm
03440      instruction.  We need this kept separate from the function's normal
03441      stack space.  */
03442   if (hash->movm_args)
03443     {
03444       /* Space for d2.  */
03445       if (hash->movm_args & 0x80)
03446        hash->movm_stack_size += 4;
03447 
03448       /* Space for d3.  */
03449       if (hash->movm_args & 0x40)
03450        hash->movm_stack_size += 4;
03451 
03452       /* Space for a2.  */
03453       if (hash->movm_args & 0x20)
03454        hash->movm_stack_size += 4;
03455 
03456       /* Space for a3.  */
03457       if (hash->movm_args & 0x10)
03458        hash->movm_stack_size += 4;
03459 
03460       /* "other" space.  d0, d1, a0, a1, mdr, lir, lar, 4 byte pad.  */
03461       if (hash->movm_args & 0x08)
03462        hash->movm_stack_size += 8 * 4;
03463 
03464       if (bfd_get_mach (abfd) == bfd_mach_am33
03465          || bfd_get_mach (abfd) == bfd_mach_am33_2)
03466        {
03467          /* "exother" space.  e0, e1, mdrq, mcrh, mcrl, mcvf */
03468          if (hash->movm_args & 0x1)
03469            hash->movm_stack_size += 6 * 4;
03470 
03471          /* exreg1 space.  e4, e5, e6, e7 */
03472          if (hash->movm_args & 0x2)
03473            hash->movm_stack_size += 4 * 4;
03474 
03475          /* exreg0 space.  e2, e3  */
03476          if (hash->movm_args & 0x4)
03477            hash->movm_stack_size += 2 * 4;
03478        }
03479     }
03480 
03481   /* Now look for the two stack adjustment variants.  */
03482   if (byte1 == 0xf8 && byte2 == 0xfe)
03483     {
03484       int temp = bfd_get_8 (abfd, contents + addr + 2);
03485       temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
03486 
03487       hash->stack_size = -temp;
03488     }
03489   else if (byte1 == 0xfa && byte2 == 0xfe)
03490     {
03491       int temp = bfd_get_16 (abfd, contents + addr + 2);
03492       temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
03493       temp = -temp;
03494 
03495       if (temp < 255)
03496        hash->stack_size = temp;
03497     }
03498 
03499   /* If the total stack to be allocated by the call instruction is more
03500      than 255 bytes, then we can't remove the stack adjustment by using
03501      "call" (we might still be able to remove the "movm" instruction.  */
03502   if (hash->stack_size + hash->movm_stack_size > 255)
03503     hash->stack_size = 0;
03504 
03505   return;
03506 }
03507 
03508 /* Delete some bytes from a section while relaxing.  */
03509 
03510 static bfd_boolean
03511 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
03512      bfd *abfd;
03513      asection *sec;
03514      bfd_vma addr;
03515      int count;
03516 {
03517   Elf_Internal_Shdr *symtab_hdr;
03518   unsigned int sec_shndx;
03519   bfd_byte *contents;
03520   Elf_Internal_Rela *irel, *irelend;
03521   Elf_Internal_Rela *irelalign;
03522   bfd_vma toaddr;
03523   Elf_Internal_Sym *isym, *isymend;
03524   struct elf_link_hash_entry **sym_hashes;
03525   struct elf_link_hash_entry **end_hashes;
03526   unsigned int symcount;
03527 
03528   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
03529 
03530   contents = elf_section_data (sec)->this_hdr.contents;
03531 
03532   /* The deletion must stop at the next ALIGN reloc for an aligment
03533      power larger than the number of bytes we are deleting.  */
03534 
03535   irelalign = NULL;
03536   toaddr = sec->size;
03537 
03538   irel = elf_section_data (sec)->relocs;
03539   irelend = irel + sec->reloc_count;
03540 
03541   /* Actually delete the bytes.  */
03542   memmove (contents + addr, contents + addr + count,
03543           (size_t) (toaddr - addr - count));
03544   sec->size -= count;
03545 
03546   /* Adjust all the relocs.  */
03547   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
03548     {
03549       /* Get the new reloc address.  */
03550       if ((irel->r_offset > addr
03551           && irel->r_offset < toaddr))
03552        irel->r_offset -= count;
03553     }
03554 
03555   /* Adjust the local symbols defined in this section.  */
03556   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
03557   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
03558   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
03559     {
03560       if (isym->st_shndx == sec_shndx
03561          && isym->st_value > addr
03562          && isym->st_value < toaddr)
03563        isym->st_value -= count;
03564     }
03565 
03566   /* Now adjust the global symbols defined in this section.  */
03567   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
03568              - symtab_hdr->sh_info);
03569   sym_hashes = elf_sym_hashes (abfd);
03570   end_hashes = sym_hashes + symcount;
03571   for (; sym_hashes < end_hashes; sym_hashes++)
03572     {
03573       struct elf_link_hash_entry *sym_hash = *sym_hashes;
03574       if ((sym_hash->root.type == bfd_link_hash_defined
03575           || sym_hash->root.type == bfd_link_hash_defweak)
03576          && sym_hash->root.u.def.section == sec
03577          && sym_hash->root.u.def.value > addr
03578          && sym_hash->root.u.def.value < toaddr)
03579        {
03580          sym_hash->root.u.def.value -= count;
03581        }
03582     }
03583 
03584   return TRUE;
03585 }
03586 
03587 /* Return TRUE if a symbol exists at the given address, else return
03588    FALSE.  */
03589 static bfd_boolean
03590 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
03591      bfd *abfd;
03592      asection *sec;
03593      Elf_Internal_Sym *isym;
03594      bfd_vma addr;
03595 {
03596   Elf_Internal_Shdr *symtab_hdr;
03597   unsigned int sec_shndx;
03598   Elf_Internal_Sym *isymend;
03599   struct elf_link_hash_entry **sym_hashes;
03600   struct elf_link_hash_entry **end_hashes;
03601   unsigned int symcount;
03602 
03603   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
03604 
03605   /* Examine all the symbols.  */
03606   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
03607   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
03608     {
03609       if (isym->st_shndx == sec_shndx
03610          && isym->st_value == addr)
03611        return TRUE;
03612     }
03613 
03614   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
03615              - symtab_hdr->sh_info);
03616   sym_hashes = elf_sym_hashes (abfd);
03617   end_hashes = sym_hashes + symcount;
03618   for (; sym_hashes < end_hashes; sym_hashes++)
03619     {
03620       struct elf_link_hash_entry *sym_hash = *sym_hashes;
03621       if ((sym_hash->root.type == bfd_link_hash_defined
03622           || sym_hash->root.type == bfd_link_hash_defweak)
03623          && sym_hash->root.u.def.section == sec
03624          && sym_hash->root.u.def.value == addr)
03625        return TRUE;
03626     }
03627 
03628   return FALSE;
03629 }
03630 
03631 /* This is a version of bfd_generic_get_relocated_section_contents
03632    which uses mn10300_elf_relocate_section.  */
03633 
03634 static bfd_byte *
03635 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
03636                                        data, relocatable, symbols)
03637      bfd *output_bfd;
03638      struct bfd_link_info *link_info;
03639      struct bfd_link_order *link_order;
03640      bfd_byte *data;
03641      bfd_boolean relocatable;
03642      asymbol **symbols;
03643 {
03644   Elf_Internal_Shdr *symtab_hdr;
03645   asection *input_section = link_order->u.indirect.section;
03646   bfd *input_bfd = input_section->owner;
03647   asection **sections = NULL;
03648   Elf_Internal_Rela *internal_relocs = NULL;
03649   Elf_Internal_Sym *isymbuf = NULL;
03650 
03651   /* We only need to handle the case of relaxing, or of having a
03652      particular set of section contents, specially.  */
03653   if (relocatable
03654       || elf_section_data (input_section)->this_hdr.contents == NULL)
03655     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
03656                                                  link_order, data,
03657                                                  relocatable,
03658                                                  symbols);
03659 
03660   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
03661 
03662   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
03663          (size_t) input_section->size);
03664 
03665   if ((input_section->flags & SEC_RELOC) != 0
03666       && input_section->reloc_count > 0)
03667     {
03668       asection **secpp;
03669       Elf_Internal_Sym *isym, *isymend;
03670       bfd_size_type amt;
03671 
03672       internal_relocs = (_bfd_elf_link_read_relocs
03673                       (input_bfd, input_section, (PTR) NULL,
03674                        (Elf_Internal_Rela *) NULL, FALSE));
03675       if (internal_relocs == NULL)
03676        goto error_return;
03677 
03678       if (symtab_hdr->sh_info != 0)
03679        {
03680          isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
03681          if (isymbuf == NULL)
03682            isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
03683                                        symtab_hdr->sh_info, 0,
03684                                        NULL, NULL, NULL);
03685          if (isymbuf == NULL)
03686            goto error_return;
03687        }
03688 
03689       amt = symtab_hdr->sh_info;
03690       amt *= sizeof (asection *);
03691       sections = (asection **) bfd_malloc (amt);
03692       if (sections == NULL && amt != 0)
03693        goto error_return;
03694 
03695       isymend = isymbuf + symtab_hdr->sh_info;
03696       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
03697        {
03698          asection *isec;
03699 
03700          if (isym->st_shndx == SHN_UNDEF)
03701            isec = bfd_und_section_ptr;
03702          else if (isym->st_shndx == SHN_ABS)
03703            isec = bfd_abs_section_ptr;
03704          else if (isym->st_shndx == SHN_COMMON)
03705            isec = bfd_com_section_ptr;
03706          else
03707            isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
03708 
03709          *secpp = isec;
03710        }
03711 
03712       if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
03713                                  input_section, data, internal_relocs,
03714                                  isymbuf, sections))
03715        goto error_return;
03716 
03717       if (sections != NULL)
03718        free (sections);
03719       if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
03720        free (isymbuf);
03721       if (internal_relocs != elf_section_data (input_section)->relocs)
03722        free (internal_relocs);
03723     }
03724 
03725   return data;
03726 
03727  error_return:
03728   if (sections != NULL)
03729     free (sections);
03730   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
03731     free (isymbuf);
03732   if (internal_relocs != NULL
03733       && internal_relocs != elf_section_data (input_section)->relocs)
03734     free (internal_relocs);
03735   return NULL;
03736 }
03737 
03738 /* Assorted hash table functions.  */
03739 
03740 /* Initialize an entry in the link hash table.  */
03741 
03742 /* Create an entry in an MN10300 ELF linker hash table.  */
03743 
03744 static struct bfd_hash_entry *
03745 elf32_mn10300_link_hash_newfunc (entry, table, string)
03746      struct bfd_hash_entry *entry;
03747      struct bfd_hash_table *table;
03748      const char *string;
03749 {
03750   struct elf32_mn10300_link_hash_entry *ret =
03751     (struct elf32_mn10300_link_hash_entry *) entry;
03752 
03753   /* Allocate the structure if it has not already been allocated by a
03754      subclass.  */
03755   if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
03756     ret = ((struct elf32_mn10300_link_hash_entry *)
03757           bfd_hash_allocate (table,
03758                            sizeof (struct elf32_mn10300_link_hash_entry)));
03759   if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
03760     return (struct bfd_hash_entry *) ret;
03761 
03762   /* Call the allocation method of the superclass.  */
03763   ret = ((struct elf32_mn10300_link_hash_entry *)
03764         _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
03765                                  table, string));
03766   if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
03767     {
03768       ret->direct_calls = 0;
03769       ret->stack_size = 0;
03770       ret->movm_args = 0;
03771       ret->movm_stack_size = 0;
03772       ret->flags = 0;
03773       ret->value = 0;
03774     }
03775 
03776   return (struct bfd_hash_entry *) ret;
03777 }
03778 
03779 /* Create an mn10300 ELF linker hash table.  */
03780 
03781 static struct bfd_link_hash_table *
03782 elf32_mn10300_link_hash_table_create (abfd)
03783      bfd *abfd;
03784 {
03785   struct elf32_mn10300_link_hash_table *ret;
03786   bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
03787 
03788   ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
03789   if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
03790     return NULL;
03791 
03792   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
03793                                   elf32_mn10300_link_hash_newfunc,
03794                                   sizeof (struct elf32_mn10300_link_hash_entry)))
03795     {
03796       free (ret);
03797       return NULL;
03798     }
03799 
03800   ret->flags = 0;
03801   amt = sizeof (struct elf_link_hash_table);
03802   ret->static_hash_table
03803     = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
03804   if (ret->static_hash_table == NULL)
03805     {
03806       free (ret);
03807       return NULL;
03808     }
03809 
03810   if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
03811                                   elf32_mn10300_link_hash_newfunc,
03812                                   sizeof (struct elf32_mn10300_link_hash_entry)))
03813     {
03814       free (ret->static_hash_table);
03815       free (ret);
03816       return NULL;
03817     }
03818   return &ret->root.root;
03819 }
03820 
03821 /* Free an mn10300 ELF linker hash table.  */
03822 
03823 static void
03824 elf32_mn10300_link_hash_table_free (hash)
03825      struct bfd_link_hash_table *hash;
03826 {
03827   struct elf32_mn10300_link_hash_table *ret
03828     = (struct elf32_mn10300_link_hash_table *) hash;
03829 
03830   _bfd_generic_link_hash_table_free
03831     ((struct bfd_link_hash_table *) ret->static_hash_table);
03832   _bfd_generic_link_hash_table_free
03833     ((struct bfd_link_hash_table *) ret);
03834 }
03835 
03836 static unsigned long
03837 elf_mn10300_mach (flags)
03838      flagword flags;
03839 {
03840   switch (flags & EF_MN10300_MACH)
03841     {
03842     case E_MN10300_MACH_MN10300:
03843     default:
03844       return bfd_mach_mn10300;
03845 
03846     case E_MN10300_MACH_AM33:
03847       return bfd_mach_am33;
03848 
03849     case E_MN10300_MACH_AM33_2:
03850       return bfd_mach_am33_2;
03851     }
03852 }
03853 
03854 /* The final processing done just before writing out a MN10300 ELF object
03855    file.  This gets the MN10300 architecture right based on the machine
03856    number.  */
03857 
03858 void
03859 _bfd_mn10300_elf_final_write_processing (abfd, linker)
03860      bfd *abfd;
03861      bfd_boolean linker ATTRIBUTE_UNUSED;
03862 {
03863   unsigned long val;
03864 
03865   switch (bfd_get_mach (abfd))
03866     {
03867     default:
03868     case bfd_mach_mn10300:
03869       val = E_MN10300_MACH_MN10300;
03870       break;
03871 
03872     case bfd_mach_am33:
03873       val = E_MN10300_MACH_AM33;
03874       break;
03875 
03876     case bfd_mach_am33_2:
03877       val = E_MN10300_MACH_AM33_2;
03878       break;
03879     }
03880 
03881   elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
03882   elf_elfheader (abfd)->e_flags |= val;
03883 }
03884 
03885 bfd_boolean
03886 _bfd_mn10300_elf_object_p (abfd)
03887      bfd *abfd;
03888 {
03889   bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
03890                           elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
03891   return TRUE;
03892 }
03893 
03894 /* Merge backend specific data from an object file to the output
03895    object file when linking.  */
03896 
03897 bfd_boolean
03898 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
03899      bfd *ibfd;
03900      bfd *obfd;
03901 {
03902   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
03903       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
03904     return TRUE;
03905 
03906   if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
03907       && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
03908     {
03909       if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
03910                             bfd_get_mach (ibfd)))
03911        return FALSE;
03912     }
03913 
03914   return TRUE;
03915 }
03916 
03917 #define PLT0_ENTRY_SIZE 15
03918 #define PLT_ENTRY_SIZE 20
03919 #define PIC_PLT_ENTRY_SIZE 24
03920 
03921 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
03922 {
03923   0xfc, 0xa0, 0, 0, 0, 0,   /* mov (.got+8),a0 */
03924   0xfe, 0xe, 0x10, 0, 0, 0, 0,     /* mov (.got+4),r1 */
03925   0xf0, 0xf4,               /* jmp (a0) */
03926 };
03927 
03928 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
03929 {
03930   0xfc, 0xa0, 0, 0, 0, 0,   /* mov (nameN@GOT + .got),a0 */
03931   0xf0, 0xf4,               /* jmp (a0) */
03932   0xfe, 8, 0, 0, 0, 0, 0,   /* mov reloc-table-address,r0 */
03933   0xdc, 0, 0, 0, 0,         /* jmp .plt0 */
03934 };
03935 
03936 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
03937 {
03938   0xfc, 0x22, 0, 0, 0, 0,   /* mov (nameN@GOT,a2),a0 */
03939   0xf0, 0xf4,               /* jmp (a0) */
03940   0xfe, 8, 0, 0, 0, 0, 0,   /* mov reloc-table-address,r0 */
03941   0xf8, 0x22, 8,            /* mov (8,a2),a0 */
03942   0xfb, 0xa, 0x1a, 4,              /* mov (4,a2),r1 */
03943   0xf0, 0xf4,               /* jmp (a0) */
03944 };
03945 
03946 /* Return size of the first PLT entry.  */
03947 #define elf_mn10300_sizeof_plt0(info) \
03948   (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
03949 
03950 /* Return size of a PLT entry.  */
03951 #define elf_mn10300_sizeof_plt(info) \
03952   (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
03953 
03954 /* Return offset of the PLT0 address in an absolute PLT entry.  */
03955 #define elf_mn10300_plt_plt0_offset(info) 16
03956 
03957 /* Return offset of the linker in PLT0 entry.  */
03958 #define elf_mn10300_plt0_linker_offset(info) 2
03959 
03960 /* Return offset of the GOT id in PLT0 entry.  */
03961 #define elf_mn10300_plt0_gotid_offset(info) 9
03962 
03963 /* Return offset of the temporary in PLT entry */
03964 #define elf_mn10300_plt_temp_offset(info) 8
03965 
03966 /* Return offset of the symbol in PLT entry.  */
03967 #define elf_mn10300_plt_symbol_offset(info) 2
03968 
03969 /* Return offset of the relocation in PLT entry.  */
03970 #define elf_mn10300_plt_reloc_offset(info) 11
03971 
03972 /* The name of the dynamic interpreter.  This is put in the .interp
03973    section.  */
03974 
03975 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
03976 
03977 /* Create dynamic sections when linking against a dynamic object.  */
03978 
03979 static bfd_boolean
03980 _bfd_mn10300_elf_create_dynamic_sections (abfd, info)
03981      bfd *abfd;
03982      struct bfd_link_info *info;
03983 {
03984   flagword   flags;
03985   asection * s;
03986   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
03987   int ptralign = 0;
03988 
03989   switch (bed->s->arch_size)
03990     {
03991     case 32:
03992       ptralign = 2;
03993       break;
03994 
03995     case 64:
03996       ptralign = 3;
03997       break;
03998 
03999     default:
04000       bfd_set_error (bfd_error_bad_value);
04001       return FALSE;
04002     }
04003 
04004   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
04005      .rel[a].bss sections.  */
04006 
04007   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
04008           | SEC_LINKER_CREATED);
04009 
04010   s = bfd_make_section_with_flags (abfd,
04011                                (bed->default_use_rela_p
04012                                 ? ".rela.plt" : ".rel.plt"),
04013                                flags | SEC_READONLY);
04014   if (s == NULL
04015       || ! bfd_set_section_alignment (abfd, s, ptralign))
04016     return FALSE;
04017 
04018   if (! _bfd_mn10300_elf_create_got_section (abfd, info))
04019     return FALSE;
04020 
04021   {
04022     const char * secname;
04023     char *       relname;
04024     flagword     secflags;
04025     asection *   sec;
04026 
04027     for (sec = abfd->sections; sec; sec = sec->next)
04028       {
04029        secflags = bfd_get_section_flags (abfd, sec);
04030        if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
04031            || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
04032          continue;
04033 
04034        secname = bfd_get_section_name (abfd, sec);
04035        relname = (char *) bfd_malloc (strlen (secname) + 6);
04036        strcpy (relname, ".rela");
04037        strcat (relname, secname);
04038 
04039        s = bfd_make_section_with_flags (abfd, relname,
04040                                     flags | SEC_READONLY);
04041        if (s == NULL
04042            || ! bfd_set_section_alignment (abfd, s, ptralign))
04043          return FALSE;
04044       }
04045   }
04046 
04047   if (bed->want_dynbss)
04048     {
04049       /* The .dynbss section is a place to put symbols which are defined
04050         by dynamic objects, are referenced by regular objects, and are
04051         not functions.  We must allocate space for them in the process
04052         image and use a R_*_COPY reloc to tell the dynamic linker to
04053         initialize them at run time.  The linker script puts the .dynbss
04054         section into the .bss section of the final image.  */
04055       s = bfd_make_section_with_flags (abfd, ".dynbss",
04056                                    SEC_ALLOC | SEC_LINKER_CREATED);
04057       if (s == NULL)
04058        return FALSE;
04059 
04060       /* The .rel[a].bss section holds copy relocs.  This section is not
04061         normally needed.  We need to create it here, though, so that the
04062         linker will map it to an output section.  We can't just create it
04063         only if we need it, because we will not know whether we need it
04064         until we have seen all the input files, and the first time the
04065         main linker code calls BFD after examining all the input files
04066         (size_dynamic_sections) the input sections have already been
04067         mapped to the output sections.  If the section turns out not to
04068         be needed, we can discard it later.  We will never need this
04069         section when generating a shared object, since they do not use
04070         copy relocs.  */
04071       if (! info->shared)
04072        {
04073          s = bfd_make_section_with_flags (abfd,
04074                                       (bed->default_use_rela_p
04075                                        ? ".rela.bss" : ".rel.bss"),
04076                                       flags | SEC_READONLY);
04077          if (s == NULL
04078              || ! bfd_set_section_alignment (abfd, s, ptralign))
04079            return FALSE;
04080        }
04081     }
04082 
04083   return TRUE;
04084 }
04085 
04086 /* Adjust a symbol defined by a dynamic object and referenced by a
04087    regular object.  The current definition is in some section of the
04088    dynamic object, but we're not including those sections.  We have to
04089    change the definition to something the rest of the link can
04090    understand.  */
04091 
04092 static bfd_boolean
04093 _bfd_mn10300_elf_adjust_dynamic_symbol (info, h)
04094      struct bfd_link_info * info;
04095      struct elf_link_hash_entry * h;
04096 {
04097   bfd * dynobj;
04098   asection * s;
04099   unsigned int power_of_two;
04100 
04101   dynobj = elf_hash_table (info)->dynobj;
04102 
04103   /* Make sure we know what is going on here.  */
04104   BFD_ASSERT (dynobj != NULL
04105              && (h->needs_plt
04106                 || h->u.weakdef != NULL
04107                 || (h->def_dynamic
04108                     && h->ref_regular
04109                     && !h->def_regular)));
04110 
04111   /* If this is a function, put it in the procedure linkage table.  We
04112      will fill in the contents of the procedure linkage table later,
04113      when we know the address of the .got section.  */
04114   if (h->type == STT_FUNC
04115       || h->needs_plt)
04116     {
04117       if (! info->shared
04118          && !h->def_dynamic
04119          && !h->ref_dynamic)
04120        {
04121          /* This case can occur if we saw a PLT reloc in an input
04122             file, but the symbol was never referred to by a dynamic
04123             object.  In such a case, we don't actually need to build
04124             a procedure linkage table, and we can just do a REL32
04125             reloc instead.  */
04126          BFD_ASSERT (h->needs_plt);
04127          return TRUE;
04128        }
04129 
04130       /* Make sure this symbol is output as a dynamic symbol.  */
04131       if (h->dynindx == -1)
04132        {
04133          if (! bfd_elf_link_record_dynamic_symbol (info, h))
04134            return FALSE;
04135        }
04136 
04137       s = bfd_get_section_by_name (dynobj, ".plt");
04138       BFD_ASSERT (s != NULL);
04139 
04140       /* If this is the first .plt entry, make room for the special
04141         first entry.  */
04142       if (s->size == 0)
04143        s->size += elf_mn10300_sizeof_plt0 (info);
04144 
04145       /* If this symbol is not defined in a regular file, and we are
04146         not generating a shared library, then set the symbol to this
04147         location in the .plt.  This is required to make function
04148         pointers compare as equal between the normal executable and
04149         the shared library.  */
04150       if (! info->shared
04151          && !h->def_regular)
04152        {
04153          h->root.u.def.section = s;
04154          h->root.u.def.value = s->size;
04155        }
04156 
04157       h->plt.offset = s->size;
04158 
04159       /* Make room for this entry.  */
04160       s->size += elf_mn10300_sizeof_plt (info);
04161 
04162       /* We also need to make an entry in the .got.plt section, which
04163         will be placed in the .got section by the linker script.  */
04164 
04165       s = bfd_get_section_by_name (dynobj, ".got.plt");
04166       BFD_ASSERT (s != NULL);
04167       s->size += 4;
04168 
04169       /* We also need to make an entry in the .rela.plt section.  */
04170 
04171       s = bfd_get_section_by_name (dynobj, ".rela.plt");
04172       BFD_ASSERT (s != NULL);
04173       s->size += sizeof (Elf32_External_Rela);
04174 
04175       return TRUE;
04176     }
04177 
04178   /* If this is a weak symbol, and there is a real definition, the
04179      processor independent code will have arranged for us to see the
04180      real definition first, and we can just use the same value.  */
04181   if (h->u.weakdef != NULL)
04182     {
04183       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
04184                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
04185       h->root.u.def.section = h->u.weakdef->root.u.def.section;
04186       h->root.u.def.value = h->u.weakdef->root.u.def.value;
04187       return TRUE;
04188     }
04189 
04190   /* This is a reference to a symbol defined by a dynamic object which
04191      is not a function.  */
04192 
04193   /* If we are creating a shared library, we must presume that the
04194      only references to the symbol are via the global offset table.
04195      For such cases we need not do anything here; the relocations will
04196      be handled correctly by relocate_section.  */
04197   if (info->shared)
04198     return TRUE;
04199 
04200   /* If there are no references to this symbol that do not use the
04201      GOT, we don't need to generate a copy reloc.  */
04202   if (!h->non_got_ref)
04203     return TRUE;
04204 
04205   if (h->size == 0)
04206     {
04207       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
04208                           h->root.root.string);
04209       return TRUE;
04210     }
04211 
04212   /* We must allocate the symbol in our .dynbss section, which will
04213      become part of the .bss section of the executable.  There will be
04214      an entry for this symbol in the .dynsym section.  The dynamic
04215      object will contain position independent code, so all references
04216      from the dynamic object to this symbol will go through the global
04217      offset table.  The dynamic linker will use the .dynsym entry to
04218      determine the address it must put in the global offset table, so
04219      both the dynamic object and the regular object will refer to the
04220      same memory location for the variable.  */
04221 
04222   s = bfd_get_section_by_name (dynobj, ".dynbss");
04223   BFD_ASSERT (s != NULL);
04224 
04225   /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
04226      copy the initial value out of the dynamic object and into the
04227      runtime process image.  We need to remember the offset into the
04228      .rela.bss section we are going to use.  */
04229   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
04230     {
04231       asection * srel;
04232 
04233       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
04234       BFD_ASSERT (srel != NULL);
04235       srel->size += sizeof (Elf32_External_Rela);
04236       h->needs_copy = 1;
04237     }
04238 
04239   /* We need to figure out the alignment required for this symbol.  I
04240      have no idea how ELF linkers handle this.  */
04241   power_of_two = bfd_log2 (h->size);
04242   if (power_of_two > 3)
04243     power_of_two = 3;
04244 
04245   /* Apply the required alignment.  */
04246   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
04247   if (power_of_two > bfd_get_section_alignment (dynobj, s))
04248     {
04249       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
04250        return FALSE;
04251     }
04252 
04253   /* Define the symbol as being at this point in the section.  */
04254   h->root.u.def.section = s;
04255   h->root.u.def.value = s->size;
04256 
04257   /* Increment the section size to make room for the symbol.  */
04258   s->size += h->size;
04259 
04260   return TRUE;
04261 }
04262 
04263 /* Set the sizes of the dynamic sections.  */
04264 
04265 static bfd_boolean
04266 _bfd_mn10300_elf_size_dynamic_sections (output_bfd, info)
04267      bfd * output_bfd;
04268      struct bfd_link_info * info;
04269 {
04270   bfd * dynobj;
04271   asection * s;
04272   bfd_boolean plt;
04273   bfd_boolean relocs;
04274   bfd_boolean reltext;
04275 
04276   dynobj = elf_hash_table (info)->dynobj;
04277   BFD_ASSERT (dynobj != NULL);
04278 
04279   if (elf_hash_table (info)->dynamic_sections_created)
04280     {
04281       /* Set the contents of the .interp section to the interpreter.  */
04282       if (info->executable)
04283        {
04284          s = bfd_get_section_by_name (dynobj, ".interp");
04285          BFD_ASSERT (s != NULL);
04286          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
04287          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
04288        }
04289     }
04290   else
04291     {
04292       /* We may have created entries in the .rela.got section.
04293         However, if we are not creating the dynamic sections, we will
04294         not actually use these entries.  Reset the size of .rela.got,
04295         which will cause it to get stripped from the output file
04296         below.  */
04297       s = bfd_get_section_by_name (dynobj, ".rela.got");
04298       if (s != NULL)
04299        s->size = 0;
04300     }
04301 
04302   /* The check_relocs and adjust_dynamic_symbol entry points have
04303      determined the sizes of the various dynamic sections.  Allocate
04304      memory for them.  */
04305   plt = FALSE;
04306   relocs = FALSE;
04307   reltext = FALSE;
04308   for (s = dynobj->sections; s != NULL; s = s->next)
04309     {
04310       const char * name;
04311 
04312       if ((s->flags & SEC_LINKER_CREATED) == 0)
04313        continue;
04314 
04315       /* It's OK to base decisions on the section name, because none
04316         of the dynobj section names depend upon the input files.  */
04317       name = bfd_get_section_name (dynobj, s);
04318 
04319       if (strcmp (name, ".plt") == 0)
04320        {
04321          /* Remember whether there is a PLT.  */
04322          plt = s->size != 0;
04323        }
04324       else if (CONST_STRNEQ (name, ".rela"))
04325        {
04326          if (s->size != 0)
04327            {
04328              asection * target;
04329 
04330              /* Remember whether there are any reloc sections other
04331                than .rela.plt.  */
04332              if (strcmp (name, ".rela.plt") != 0)
04333               {
04334                 const char * outname;
04335 
04336                 relocs = TRUE;
04337 
04338                 /* If this relocation section applies to a read only
04339                    section, then we probably need a DT_TEXTREL
04340                    entry.  The entries in the .rela.plt section
04341                    really apply to the .got section, which we
04342                    created ourselves and so know is not readonly.  */
04343                 outname = bfd_get_section_name (output_bfd,
04344                                             s->output_section);
04345                 target = bfd_get_section_by_name (output_bfd, outname + 5);
04346                 if (target != NULL
04347                     && (target->flags & SEC_READONLY) != 0
04348                     && (target->flags & SEC_ALLOC) != 0)
04349                   reltext = TRUE;
04350               }
04351 
04352              /* We use the reloc_count field as a counter if we need
04353                to copy relocs into the output file.  */
04354              s->reloc_count = 0;
04355            }
04356        }
04357       else if (! CONST_STRNEQ (name, ".got")
04358               && strcmp (name, ".dynbss") != 0)
04359        /* It's not one of our sections, so don't allocate space.  */
04360        continue;
04361 
04362       if (s->size == 0)
04363        {
04364          /* If we don't need this section, strip it from the
04365             output file.  This is mostly to handle .rela.bss and
04366             .rela.plt.  We must create both sections in
04367             create_dynamic_sections, because they must be created
04368             before the linker maps input sections to output
04369             sections.  The linker does that before
04370             adjust_dynamic_symbol is called, and it is that
04371             function which decides whether anything needs to go
04372             into these sections.  */
04373          s->flags |= SEC_EXCLUDE;
04374          continue;
04375        }
04376 
04377        if ((s->flags & SEC_HAS_CONTENTS) == 0)
04378          continue;
04379 
04380       /* Allocate memory for the section contents.  We use bfd_zalloc
04381         here in case unused entries are not reclaimed before the
04382         section's contents are written out.  This should not happen,
04383         but this way if it does, we get a R_MN10300_NONE reloc
04384         instead of garbage.  */
04385       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
04386       if (s->contents == NULL)
04387        return FALSE;
04388     }
04389 
04390   if (elf_hash_table (info)->dynamic_sections_created)
04391     {
04392       /* Add some entries to the .dynamic section.  We fill in the
04393         values later, in _bfd_mn10300_elf_finish_dynamic_sections,
04394         but we must add the entries now so that we get the correct
04395         size for the .dynamic section.  The DT_DEBUG entry is filled
04396         in by the dynamic linker and used by the debugger.  */
04397       if (! info->shared)
04398        {
04399          if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
04400            return FALSE;
04401        }
04402 
04403       if (plt)
04404        {
04405          if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
04406              || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
04407              || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
04408              || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
04409            return FALSE;
04410        }
04411 
04412       if (relocs)
04413        {
04414          if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
04415              || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
04416              || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
04417                                          sizeof (Elf32_External_Rela)))
04418            return FALSE;
04419        }
04420 
04421       if (reltext)
04422        {
04423          if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
04424            return FALSE;
04425        }
04426     }
04427 
04428   return TRUE;
04429 }
04430 
04431 /* Finish up dynamic symbol handling.  We set the contents of various
04432    dynamic sections here.  */
04433 
04434 static bfd_boolean
04435 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
04436      bfd * output_bfd;
04437      struct bfd_link_info * info;
04438      struct elf_link_hash_entry * h;
04439      Elf_Internal_Sym * sym;
04440 {
04441   bfd * dynobj;
04442 
04443   dynobj = elf_hash_table (info)->dynobj;
04444 
04445   if (h->plt.offset != (bfd_vma) -1)
04446     {
04447       asection *        splt;
04448       asection *        sgot;
04449       asection *        srel;
04450       bfd_vma           plt_index;
04451       bfd_vma           got_offset;
04452       Elf_Internal_Rela rel;
04453 
04454       /* This symbol has an entry in the procedure linkage table.  Set
04455         it up.  */
04456 
04457       BFD_ASSERT (h->dynindx != -1);
04458 
04459       splt = bfd_get_section_by_name (dynobj, ".plt");
04460       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
04461       srel = bfd_get_section_by_name (dynobj, ".rela.plt");
04462       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
04463 
04464       /* Get the index in the procedure linkage table which
04465         corresponds to this symbol.  This is the index of this symbol
04466         in all the symbols for which we are making plt entries.  The
04467         first entry in the procedure linkage table is reserved.  */
04468       plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
04469                  / elf_mn10300_sizeof_plt (info));
04470 
04471       /* Get the offset into the .got table of the entry that
04472         corresponds to this function.  Each .got entry is 4 bytes.
04473         The first three are reserved.  */
04474       got_offset = (plt_index + 3) * 4;
04475 
04476       /* Fill in the entry in the procedure linkage table.  */
04477       if (! info->shared)
04478        {
04479          memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
04480                 elf_mn10300_sizeof_plt (info));
04481          bfd_put_32 (output_bfd,
04482                     (sgot->output_section->vma
04483                      + sgot->output_offset
04484                      + got_offset),
04485                     (splt->contents + h->plt.offset
04486                      + elf_mn10300_plt_symbol_offset (info)));
04487 
04488          bfd_put_32 (output_bfd,
04489                     (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
04490                     (splt->contents + h->plt.offset
04491                      + elf_mn10300_plt_plt0_offset (info)));
04492        }
04493       else
04494        {
04495          memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
04496                 elf_mn10300_sizeof_plt (info));
04497 
04498          bfd_put_32 (output_bfd, got_offset,
04499                     (splt->contents + h->plt.offset
04500                      + elf_mn10300_plt_symbol_offset (info)));
04501        }
04502 
04503       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
04504                 (splt->contents + h->plt.offset
04505                  + elf_mn10300_plt_reloc_offset (info)));
04506 
04507       /* Fill in the entry in the global offset table.  */
04508       bfd_put_32 (output_bfd,
04509                 (splt->output_section->vma
04510                  + splt->output_offset
04511                  + h->plt.offset
04512                  + elf_mn10300_plt_temp_offset (info)),
04513                 sgot->contents + got_offset);
04514 
04515       /* Fill in the entry in the .rela.plt section.  */
04516       rel.r_offset = (sgot->output_section->vma
04517                     + sgot->output_offset
04518                     + got_offset);
04519       rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
04520       rel.r_addend = 0;
04521       bfd_elf32_swap_reloca_out (output_bfd, &rel,
04522                              (bfd_byte *) ((Elf32_External_Rela *) srel->contents
04523                                           + plt_index));
04524 
04525       if (!h->def_regular)
04526        /* Mark the symbol as undefined, rather than as defined in
04527           the .plt section.  Leave the value alone.  */
04528        sym->st_shndx = SHN_UNDEF;
04529     }
04530 
04531   if (h->got.offset != (bfd_vma) -1)
04532     {
04533       asection *        sgot;
04534       asection *        srel;
04535       Elf_Internal_Rela rel;
04536 
04537       /* This symbol has an entry in the global offset table.  Set it up.  */
04538 
04539       sgot = bfd_get_section_by_name (dynobj, ".got");
04540       srel = bfd_get_section_by_name (dynobj, ".rela.got");
04541       BFD_ASSERT (sgot != NULL && srel != NULL);
04542 
04543       rel.r_offset = (sgot->output_section->vma
04544                     + sgot->output_offset
04545                     + (h->got.offset &~ 1));
04546 
04547       /* If this is a -Bsymbolic link, and the symbol is defined
04548         locally, we just want to emit a RELATIVE reloc.  Likewise if
04549         the symbol was forced to be local because of a version file.
04550         The entry in the global offset table will already have been
04551         initialized in the relocate_section function.  */
04552       if (info->shared
04553          && (info->symbolic || h->dynindx == -1)
04554          && h->def_regular)
04555        {
04556          rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
04557          rel.r_addend = (h->root.u.def.value
04558                        + h->root.u.def.section->output_section->vma
04559                        + h->root.u.def.section->output_offset);
04560        }
04561       else
04562        {
04563          bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
04564          rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
04565          rel.r_addend = 0;
04566        }
04567 
04568       bfd_elf32_swap_reloca_out (output_bfd, &rel,
04569                              (bfd_byte *) ((Elf32_External_Rela *) srel->contents
04570                                           + srel->reloc_count));
04571       ++ srel->reloc_count;
04572     }
04573 
04574   if (h->needs_copy)
04575     {
04576       asection *        s;
04577       Elf_Internal_Rela rel;
04578 
04579       /* This symbol needs a copy reloc.  Set it up.  */
04580       BFD_ASSERT (h->dynindx != -1
04581                 && (h->root.type == bfd_link_hash_defined
04582                     || h->root.type == bfd_link_hash_defweak));
04583 
04584       s = bfd_get_section_by_name (h->root.u.def.section->owner,
04585                                ".rela.bss");
04586       BFD_ASSERT (s != NULL);
04587 
04588       rel.r_offset = (h->root.u.def.value
04589                     + h->root.u.def.section->output_section->vma
04590                     + h->root.u.def.section->output_offset);
04591       rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
04592       rel.r_addend = 0;
04593       bfd_elf32_swap_reloca_out (output_bfd, &rel,
04594                              (bfd_byte *) ((Elf32_External_Rela *) s->contents
04595                                           + s->reloc_count));
04596       ++ s->reloc_count;
04597     }
04598 
04599   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
04600   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
04601       || h == elf_hash_table (info)->hgot)
04602     sym->st_shndx = SHN_ABS;
04603 
04604   return TRUE;
04605 }
04606 
04607 /* Finish up the dynamic sections.  */
04608 
04609 static bfd_boolean
04610 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd, info)
04611      bfd * output_bfd;
04612      struct bfd_link_info * info;
04613 {
04614   bfd *      dynobj;
04615   asection * sgot;
04616   asection * sdyn;
04617 
04618   dynobj = elf_hash_table (info)->dynobj;
04619 
04620   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
04621   BFD_ASSERT (sgot != NULL);
04622   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
04623 
04624   if (elf_hash_table (info)->dynamic_sections_created)
04625     {
04626       asection *           splt;
04627       Elf32_External_Dyn * dyncon;
04628       Elf32_External_Dyn * dynconend;
04629 
04630       BFD_ASSERT (sdyn != NULL);
04631 
04632       dyncon = (Elf32_External_Dyn *) sdyn->contents;
04633       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
04634 
04635       for (; dyncon < dynconend; dyncon++)
04636        {
04637          Elf_Internal_Dyn dyn;
04638          const char * name;
04639          asection * s;
04640 
04641          bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
04642 
04643          switch (dyn.d_tag)
04644            {
04645            default:
04646              break;
04647 
04648            case DT_PLTGOT:
04649              name = ".got";
04650              goto get_vma;
04651 
04652            case DT_JMPREL:
04653              name = ".rela.plt";
04654            get_vma:
04655              s = bfd_get_section_by_name (output_bfd, name);
04656              BFD_ASSERT (s != NULL);
04657              dyn.d_un.d_ptr = s->vma;
04658              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
04659              break;
04660 
04661            case DT_PLTRELSZ:
04662              s = bfd_get_section_by_name (output_bfd, ".rela.plt");
04663              BFD_ASSERT (s != NULL);
04664              dyn.d_un.d_val = s->size;
04665              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
04666              break;
04667 
04668            case DT_RELASZ:
04669              /* My reading of the SVR4 ABI indicates that the
04670                procedure linkage table relocs (DT_JMPREL) should be
04671                included in the overall relocs (DT_RELA).  This is
04672                what Solaris does.  However, UnixWare can not handle
04673                that case.  Therefore, we override the DT_RELASZ entry
04674                here to make it not include the JMPREL relocs.  Since
04675                the linker script arranges for .rela.plt to follow all
04676                other relocation sections, we don't have to worry
04677                about changing the DT_RELA entry.  */
04678              s = bfd_get_section_by_name (output_bfd, ".rela.plt");
04679              if (s != NULL)
04680               dyn.d_un.d_val -= s->size;
04681              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
04682              break;
04683            }
04684        }
04685 
04686       /* Fill in the first entry in the procedure linkage table.  */
04687       splt = bfd_get_section_by_name (dynobj, ".plt");
04688       if (splt && splt->size > 0)
04689        {
04690          if (info->shared)
04691            {
04692              memcpy (splt->contents, elf_mn10300_pic_plt_entry,
04693                     elf_mn10300_sizeof_plt (info));
04694            }
04695          else
04696            {
04697              memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
04698              bfd_put_32 (output_bfd,
04699                        sgot->output_section->vma + sgot->output_offset + 4,
04700                        splt->contents + elf_mn10300_plt0_gotid_offset (info));
04701              bfd_put_32 (output_bfd,
04702                        sgot->output_section->vma + sgot->output_offset + 8,
04703                        splt->contents + elf_mn10300_plt0_linker_offset (info));
04704            }
04705 
04706          /* UnixWare sets the entsize of .plt to 4, although that doesn't
04707             really seem like the right value.  */
04708          elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
04709        }
04710     }
04711 
04712   /* Fill in the first three entries in the global offset table.  */
04713   if (sgot->size > 0)
04714     {
04715       if (sdyn == NULL)
04716        bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
04717       else
04718        bfd_put_32 (output_bfd,
04719                   sdyn->output_section->vma + sdyn->output_offset,
04720                   sgot->contents);
04721       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
04722       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
04723     }
04724 
04725   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
04726 
04727   return TRUE;
04728 }
04729 
04730 /* Classify relocation types, such that combreloc can sort them
04731    properly.  */
04732 
04733 static enum elf_reloc_type_class
04734 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
04735 {
04736   switch ((int) ELF32_R_TYPE (rela->r_info))
04737     {
04738     case R_MN10300_RELATIVE:
04739       return reloc_class_relative;
04740     case R_MN10300_JMP_SLOT:
04741       return reloc_class_plt;
04742     case R_MN10300_COPY:
04743       return reloc_class_copy;
04744     default:
04745       return reloc_class_normal;
04746     }
04747 }
04748 
04749 #ifndef ELF_ARCH
04750 #define TARGET_LITTLE_SYM   bfd_elf32_mn10300_vec
04751 #define TARGET_LITTLE_NAME  "elf32-mn10300"
04752 #define ELF_ARCH            bfd_arch_mn10300
04753 #define ELF_MACHINE_CODE    EM_MN10300
04754 #define ELF_MACHINE_ALT1    EM_CYGNUS_MN10300
04755 #define ELF_MAXPAGESIZE            0x1000
04756 #endif
04757 
04758 #define elf_info_to_howto          mn10300_info_to_howto
04759 #define elf_info_to_howto_rel             0
04760 #define elf_backend_can_gc_sections       1
04761 #define elf_backend_rela_normal           1
04762 #define elf_backend_check_relocs   mn10300_elf_check_relocs
04763 #define elf_backend_gc_mark_hook   mn10300_elf_gc_mark_hook
04764 #define elf_backend_relocate_section      mn10300_elf_relocate_section
04765 #define bfd_elf32_bfd_relax_section       mn10300_elf_relax_section
04766 #define bfd_elf32_bfd_get_relocated_section_contents \
04767                             mn10300_elf_get_relocated_section_contents
04768 #define bfd_elf32_bfd_link_hash_table_create \
04769                             elf32_mn10300_link_hash_table_create
04770 #define bfd_elf32_bfd_link_hash_table_free \
04771                             elf32_mn10300_link_hash_table_free
04772 
04773 #ifndef elf_symbol_leading_char
04774 #define elf_symbol_leading_char '_'
04775 #endif
04776 
04777 /* So we can set bits in e_flags.  */
04778 #define elf_backend_final_write_processing \
04779                                    _bfd_mn10300_elf_final_write_processing
04780 #define elf_backend_object_p              _bfd_mn10300_elf_object_p
04781 
04782 #define bfd_elf32_bfd_merge_private_bfd_data \
04783                                    _bfd_mn10300_elf_merge_private_bfd_data
04784 
04785 #define elf_backend_can_gc_sections       1
04786 #define elf_backend_create_dynamic_sections \
04787   _bfd_mn10300_elf_create_dynamic_sections
04788 #define elf_backend_adjust_dynamic_symbol \
04789   _bfd_mn10300_elf_adjust_dynamic_symbol
04790 #define elf_backend_size_dynamic_sections \
04791   _bfd_mn10300_elf_size_dynamic_sections
04792 #define elf_backend_omit_section_dynsym \
04793   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
04794 #define elf_backend_finish_dynamic_symbol \
04795   _bfd_mn10300_elf_finish_dynamic_symbol
04796 #define elf_backend_finish_dynamic_sections \
04797   _bfd_mn10300_elf_finish_dynamic_sections
04798 
04799 #define elf_backend_reloc_type_class \
04800   _bfd_mn10300_elf_reloc_type_class
04801 
04802 #define elf_backend_want_got_plt   1
04803 #define elf_backend_plt_readonly   1
04804 #define elf_backend_want_plt_sym   0
04805 #define elf_backend_got_header_size       12
04806 
04807 #include "elf32-target.h"