Home | History | Annotate | Line # | Download | only in bfd
elf-m10300.c revision 1.1
      1  1.1  christos /* Matsushita 10300 specific support for 32-bit ELF
      2  1.1  christos    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
      3  1.1  christos    2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program; if not, write to the Free Software
     19  1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20  1.1  christos    MA 02110-1301, USA.  */
     21  1.1  christos 
     22  1.1  christos #include "sysdep.h"
     23  1.1  christos #include "bfd.h"
     24  1.1  christos #include "libbfd.h"
     25  1.1  christos #include "elf-bfd.h"
     26  1.1  christos #include "elf/mn10300.h"
     27  1.1  christos #include "libiberty.h"
     28  1.1  christos 
     29  1.1  christos /* The mn10300 linker needs to keep track of the number of relocs that
     30  1.1  christos    it decides to copy in check_relocs for each symbol.  This is so
     31  1.1  christos    that it can discard PC relative relocs if it doesn't need them when
     32  1.1  christos    linking with -Bsymbolic.  We store the information in a field
     33  1.1  christos    extending the regular ELF linker hash table.  */
     34  1.1  christos 
     35  1.1  christos struct elf32_mn10300_link_hash_entry
     36  1.1  christos {
     37  1.1  christos   /* The basic elf link hash table entry.  */
     38  1.1  christos   struct elf_link_hash_entry root;
     39  1.1  christos 
     40  1.1  christos   /* For function symbols, the number of times this function is
     41  1.1  christos      called directly (ie by name).  */
     42  1.1  christos   unsigned int direct_calls;
     43  1.1  christos 
     44  1.1  christos   /* For function symbols, the size of this function's stack
     45  1.1  christos      (if <= 255 bytes).  We stuff this into "call" instructions
     46  1.1  christos      to this target when it's valid and profitable to do so.
     47  1.1  christos 
     48  1.1  christos      This does not include stack allocated by movm!  */
     49  1.1  christos   unsigned char stack_size;
     50  1.1  christos 
     51  1.1  christos   /* For function symbols, arguments (if any) for movm instruction
     52  1.1  christos      in the prologue.  We stuff this value into "call" instructions
     53  1.1  christos      to the target when it's valid and profitable to do so.  */
     54  1.1  christos   unsigned char movm_args;
     55  1.1  christos 
     56  1.1  christos   /* For function symbols, the amount of stack space that would be allocated
     57  1.1  christos      by the movm instruction.  This is redundant with movm_args, but we
     58  1.1  christos      add it to the hash table to avoid computing it over and over.  */
     59  1.1  christos   unsigned char movm_stack_size;
     60  1.1  christos 
     61  1.1  christos /* When set, convert all "call" instructions to this target into "calls"
     62  1.1  christos    instructions.  */
     63  1.1  christos #define MN10300_CONVERT_CALL_TO_CALLS 0x1
     64  1.1  christos 
     65  1.1  christos /* Used to mark functions which have had redundant parts of their
     66  1.1  christos    prologue deleted.  */
     67  1.1  christos #define MN10300_DELETED_PROLOGUE_BYTES 0x2
     68  1.1  christos   unsigned char flags;
     69  1.1  christos 
     70  1.1  christos   /* Calculated value.  */
     71  1.1  christos   bfd_vma value;
     72  1.1  christos };
     73  1.1  christos 
     74  1.1  christos /* We derive a hash table from the main elf linker hash table so
     75  1.1  christos    we can store state variables and a secondary hash table without
     76  1.1  christos    resorting to global variables.  */
     77  1.1  christos struct elf32_mn10300_link_hash_table
     78  1.1  christos {
     79  1.1  christos   /* The main hash table.  */
     80  1.1  christos   struct elf_link_hash_table root;
     81  1.1  christos 
     82  1.1  christos   /* A hash table for static functions.  We could derive a new hash table
     83  1.1  christos      instead of using the full elf32_mn10300_link_hash_table if we wanted
     84  1.1  christos      to save some memory.  */
     85  1.1  christos   struct elf32_mn10300_link_hash_table *static_hash_table;
     86  1.1  christos 
     87  1.1  christos   /* Random linker state flags.  */
     88  1.1  christos #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
     89  1.1  christos   char flags;
     90  1.1  christos };
     91  1.1  christos 
     92  1.1  christos #ifndef streq
     93  1.1  christos #define streq(a, b) (strcmp ((a),(b)) == 0)
     94  1.1  christos #endif
     95  1.1  christos 
     96  1.1  christos /* For MN10300 linker hash table.  */
     97  1.1  christos 
     98  1.1  christos /* Get the MN10300 ELF linker hash table from a link_info structure.  */
     99  1.1  christos 
    100  1.1  christos #define elf32_mn10300_hash_table(p) \
    101  1.1  christos   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    102  1.1  christos   == MN10300_ELF_DATA ? ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) : NULL)
    103  1.1  christos 
    104  1.1  christos #define elf32_mn10300_link_hash_traverse(table, func, info)		\
    105  1.1  christos   (elf_link_hash_traverse						\
    106  1.1  christos    (&(table)->root,							\
    107  1.1  christos     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
    108  1.1  christos     (info)))
    109  1.1  christos 
    110  1.1  christos static reloc_howto_type elf_mn10300_howto_table[] =
    111  1.1  christos {
    112  1.1  christos   /* Dummy relocation.  Does nothing.  */
    113  1.1  christos   HOWTO (R_MN10300_NONE,
    114  1.1  christos 	 0,
    115  1.1  christos 	 2,
    116  1.1  christos 	 16,
    117  1.1  christos 	 FALSE,
    118  1.1  christos 	 0,
    119  1.1  christos 	 complain_overflow_bitfield,
    120  1.1  christos 	 bfd_elf_generic_reloc,
    121  1.1  christos 	 "R_MN10300_NONE",
    122  1.1  christos 	 FALSE,
    123  1.1  christos 	 0,
    124  1.1  christos 	 0,
    125  1.1  christos 	 FALSE),
    126  1.1  christos   /* Standard 32 bit reloc.  */
    127  1.1  christos   HOWTO (R_MN10300_32,
    128  1.1  christos 	 0,
    129  1.1  christos 	 2,
    130  1.1  christos 	 32,
    131  1.1  christos 	 FALSE,
    132  1.1  christos 	 0,
    133  1.1  christos 	 complain_overflow_bitfield,
    134  1.1  christos 	 bfd_elf_generic_reloc,
    135  1.1  christos 	 "R_MN10300_32",
    136  1.1  christos 	 FALSE,
    137  1.1  christos 	 0xffffffff,
    138  1.1  christos 	 0xffffffff,
    139  1.1  christos 	 FALSE),
    140  1.1  christos   /* Standard 16 bit reloc.  */
    141  1.1  christos   HOWTO (R_MN10300_16,
    142  1.1  christos 	 0,
    143  1.1  christos 	 1,
    144  1.1  christos 	 16,
    145  1.1  christos 	 FALSE,
    146  1.1  christos 	 0,
    147  1.1  christos 	 complain_overflow_bitfield,
    148  1.1  christos 	 bfd_elf_generic_reloc,
    149  1.1  christos 	 "R_MN10300_16",
    150  1.1  christos 	 FALSE,
    151  1.1  christos 	 0xffff,
    152  1.1  christos 	 0xffff,
    153  1.1  christos 	 FALSE),
    154  1.1  christos   /* Standard 8 bit reloc.  */
    155  1.1  christos   HOWTO (R_MN10300_8,
    156  1.1  christos 	 0,
    157  1.1  christos 	 0,
    158  1.1  christos 	 8,
    159  1.1  christos 	 FALSE,
    160  1.1  christos 	 0,
    161  1.1  christos 	 complain_overflow_bitfield,
    162  1.1  christos 	 bfd_elf_generic_reloc,
    163  1.1  christos 	 "R_MN10300_8",
    164  1.1  christos 	 FALSE,
    165  1.1  christos 	 0xff,
    166  1.1  christos 	 0xff,
    167  1.1  christos 	 FALSE),
    168  1.1  christos   /* Standard 32bit pc-relative reloc.  */
    169  1.1  christos   HOWTO (R_MN10300_PCREL32,
    170  1.1  christos 	 0,
    171  1.1  christos 	 2,
    172  1.1  christos 	 32,
    173  1.1  christos 	 TRUE,
    174  1.1  christos 	 0,
    175  1.1  christos 	 complain_overflow_bitfield,
    176  1.1  christos 	 bfd_elf_generic_reloc,
    177  1.1  christos 	 "R_MN10300_PCREL32",
    178  1.1  christos 	 FALSE,
    179  1.1  christos 	 0xffffffff,
    180  1.1  christos 	 0xffffffff,
    181  1.1  christos 	 TRUE),
    182  1.1  christos   /* Standard 16bit pc-relative reloc.  */
    183  1.1  christos   HOWTO (R_MN10300_PCREL16,
    184  1.1  christos 	 0,
    185  1.1  christos 	 1,
    186  1.1  christos 	 16,
    187  1.1  christos 	 TRUE,
    188  1.1  christos 	 0,
    189  1.1  christos 	 complain_overflow_bitfield,
    190  1.1  christos 	 bfd_elf_generic_reloc,
    191  1.1  christos 	 "R_MN10300_PCREL16",
    192  1.1  christos 	 FALSE,
    193  1.1  christos 	 0xffff,
    194  1.1  christos 	 0xffff,
    195  1.1  christos 	 TRUE),
    196  1.1  christos   /* Standard 8 pc-relative reloc.  */
    197  1.1  christos   HOWTO (R_MN10300_PCREL8,
    198  1.1  christos 	 0,
    199  1.1  christos 	 0,
    200  1.1  christos 	 8,
    201  1.1  christos 	 TRUE,
    202  1.1  christos 	 0,
    203  1.1  christos 	 complain_overflow_bitfield,
    204  1.1  christos 	 bfd_elf_generic_reloc,
    205  1.1  christos 	 "R_MN10300_PCREL8",
    206  1.1  christos 	 FALSE,
    207  1.1  christos 	 0xff,
    208  1.1  christos 	 0xff,
    209  1.1  christos 	 TRUE),
    210  1.1  christos 
    211  1.1  christos   /* GNU extension to record C++ vtable hierarchy.  */
    212  1.1  christos   HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
    213  1.1  christos 	 0,			/* rightshift */
    214  1.1  christos 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    215  1.1  christos 	 0,			/* bitsize */
    216  1.1  christos 	 FALSE,			/* pc_relative */
    217  1.1  christos 	 0,			/* bitpos */
    218  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    219  1.1  christos 	 NULL,			/* special_function */
    220  1.1  christos 	 "R_MN10300_GNU_VTINHERIT", /* name */
    221  1.1  christos 	 FALSE,			/* partial_inplace */
    222  1.1  christos 	 0,			/* src_mask */
    223  1.1  christos 	 0,			/* dst_mask */
    224  1.1  christos 	 FALSE),		/* pcrel_offset */
    225  1.1  christos 
    226  1.1  christos   /* GNU extension to record C++ vtable member usage */
    227  1.1  christos   HOWTO (R_MN10300_GNU_VTENTRY,	/* type */
    228  1.1  christos 	 0,			/* rightshift */
    229  1.1  christos 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    230  1.1  christos 	 0,			/* bitsize */
    231  1.1  christos 	 FALSE,			/* pc_relative */
    232  1.1  christos 	 0,			/* bitpos */
    233  1.1  christos 	 complain_overflow_dont, /* complain_on_overflow */
    234  1.1  christos 	 NULL,			/* special_function */
    235  1.1  christos 	 "R_MN10300_GNU_VTENTRY", /* name */
    236  1.1  christos 	 FALSE,			/* partial_inplace */
    237  1.1  christos 	 0,			/* src_mask */
    238  1.1  christos 	 0,			/* dst_mask */
    239  1.1  christos 	 FALSE),		/* pcrel_offset */
    240  1.1  christos 
    241  1.1  christos   /* Standard 24 bit reloc.  */
    242  1.1  christos   HOWTO (R_MN10300_24,
    243  1.1  christos 	 0,
    244  1.1  christos 	 2,
    245  1.1  christos 	 24,
    246  1.1  christos 	 FALSE,
    247  1.1  christos 	 0,
    248  1.1  christos 	 complain_overflow_bitfield,
    249  1.1  christos 	 bfd_elf_generic_reloc,
    250  1.1  christos 	 "R_MN10300_24",
    251  1.1  christos 	 FALSE,
    252  1.1  christos 	 0xffffff,
    253  1.1  christos 	 0xffffff,
    254  1.1  christos 	 FALSE),
    255  1.1  christos   HOWTO (R_MN10300_GOTPC32,	/* type */
    256  1.1  christos 	 0,			/* rightshift */
    257  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    258  1.1  christos 	 32,			/* bitsize */
    259  1.1  christos 	 TRUE,			/* pc_relative */
    260  1.1  christos 	 0,			/* bitpos */
    261  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    262  1.1  christos 	 bfd_elf_generic_reloc, /* */
    263  1.1  christos 	 "R_MN10300_GOTPC32",	/* name */
    264  1.1  christos 	 FALSE,			/* partial_inplace */
    265  1.1  christos 	 0xffffffff,		/* src_mask */
    266  1.1  christos 	 0xffffffff,		/* dst_mask */
    267  1.1  christos 	 TRUE),			/* pcrel_offset */
    268  1.1  christos 
    269  1.1  christos   HOWTO (R_MN10300_GOTPC16,	/* type */
    270  1.1  christos 	 0,			/* rightshift */
    271  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    272  1.1  christos 	 16,			/* bitsize */
    273  1.1  christos 	 TRUE,			/* pc_relative */
    274  1.1  christos 	 0,			/* bitpos */
    275  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    276  1.1  christos 	 bfd_elf_generic_reloc, /* */
    277  1.1  christos 	 "R_MN10300_GOTPC16",	/* name */
    278  1.1  christos 	 FALSE,			/* partial_inplace */
    279  1.1  christos 	 0xffff,		/* src_mask */
    280  1.1  christos 	 0xffff,		/* dst_mask */
    281  1.1  christos 	 TRUE),			/* pcrel_offset */
    282  1.1  christos 
    283  1.1  christos   HOWTO (R_MN10300_GOTOFF32,	/* type */
    284  1.1  christos 	 0,			/* rightshift */
    285  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    286  1.1  christos 	 32,			/* bitsize */
    287  1.1  christos 	 FALSE,			/* pc_relative */
    288  1.1  christos 	 0,			/* bitpos */
    289  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    290  1.1  christos 	 bfd_elf_generic_reloc, /* */
    291  1.1  christos 	 "R_MN10300_GOTOFF32",	/* name */
    292  1.1  christos 	 FALSE,			/* partial_inplace */
    293  1.1  christos 	 0xffffffff,		/* src_mask */
    294  1.1  christos 	 0xffffffff,		/* dst_mask */
    295  1.1  christos 	 FALSE),		/* pcrel_offset */
    296  1.1  christos 
    297  1.1  christos   HOWTO (R_MN10300_GOTOFF24,	/* type */
    298  1.1  christos 	 0,			/* rightshift */
    299  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    300  1.1  christos 	 24,			/* bitsize */
    301  1.1  christos 	 FALSE,			/* pc_relative */
    302  1.1  christos 	 0,			/* bitpos */
    303  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    304  1.1  christos 	 bfd_elf_generic_reloc, /* */
    305  1.1  christos 	 "R_MN10300_GOTOFF24",	/* name */
    306  1.1  christos 	 FALSE,			/* partial_inplace */
    307  1.1  christos 	 0xffffff,		/* src_mask */
    308  1.1  christos 	 0xffffff,		/* dst_mask */
    309  1.1  christos 	 FALSE),		/* pcrel_offset */
    310  1.1  christos 
    311  1.1  christos   HOWTO (R_MN10300_GOTOFF16,	/* type */
    312  1.1  christos 	 0,			/* rightshift */
    313  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    314  1.1  christos 	 16,			/* bitsize */
    315  1.1  christos 	 FALSE,			/* pc_relative */
    316  1.1  christos 	 0,			/* bitpos */
    317  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    318  1.1  christos 	 bfd_elf_generic_reloc, /* */
    319  1.1  christos 	 "R_MN10300_GOTOFF16",	/* name */
    320  1.1  christos 	 FALSE,			/* partial_inplace */
    321  1.1  christos 	 0xffff,		/* src_mask */
    322  1.1  christos 	 0xffff,		/* dst_mask */
    323  1.1  christos 	 FALSE),		/* pcrel_offset */
    324  1.1  christos 
    325  1.1  christos   HOWTO (R_MN10300_PLT32,	/* type */
    326  1.1  christos 	 0,			/* rightshift */
    327  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    328  1.1  christos 	 32,			/* bitsize */
    329  1.1  christos 	 TRUE,			/* pc_relative */
    330  1.1  christos 	 0,			/* bitpos */
    331  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    332  1.1  christos 	 bfd_elf_generic_reloc, /* */
    333  1.1  christos 	 "R_MN10300_PLT32",	/* name */
    334  1.1  christos 	 FALSE,			/* partial_inplace */
    335  1.1  christos 	 0xffffffff,		/* src_mask */
    336  1.1  christos 	 0xffffffff,		/* dst_mask */
    337  1.1  christos 	 TRUE),			/* pcrel_offset */
    338  1.1  christos 
    339  1.1  christos   HOWTO (R_MN10300_PLT16,	/* type */
    340  1.1  christos 	 0,			/* rightshift */
    341  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    342  1.1  christos 	 16,			/* bitsize */
    343  1.1  christos 	 TRUE,			/* pc_relative */
    344  1.1  christos 	 0,			/* bitpos */
    345  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    346  1.1  christos 	 bfd_elf_generic_reloc, /* */
    347  1.1  christos 	 "R_MN10300_PLT16",	/* name */
    348  1.1  christos 	 FALSE,			/* partial_inplace */
    349  1.1  christos 	 0xffff,		/* src_mask */
    350  1.1  christos 	 0xffff,		/* dst_mask */
    351  1.1  christos 	 TRUE),			/* pcrel_offset */
    352  1.1  christos 
    353  1.1  christos   HOWTO (R_MN10300_GOT32,	/* type */
    354  1.1  christos 	 0,			/* rightshift */
    355  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    356  1.1  christos 	 32,			/* bitsize */
    357  1.1  christos 	 FALSE,			/* pc_relative */
    358  1.1  christos 	 0,			/* bitpos */
    359  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    360  1.1  christos 	 bfd_elf_generic_reloc, /* */
    361  1.1  christos 	 "R_MN10300_GOT32",	/* name */
    362  1.1  christos 	 FALSE,			/* partial_inplace */
    363  1.1  christos 	 0xffffffff,		/* src_mask */
    364  1.1  christos 	 0xffffffff,		/* dst_mask */
    365  1.1  christos 	 FALSE),		/* pcrel_offset */
    366  1.1  christos 
    367  1.1  christos   HOWTO (R_MN10300_GOT24,	/* type */
    368  1.1  christos 	 0,			/* rightshift */
    369  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    370  1.1  christos 	 24,			/* bitsize */
    371  1.1  christos 	 FALSE,			/* pc_relative */
    372  1.1  christos 	 0,			/* bitpos */
    373  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    374  1.1  christos 	 bfd_elf_generic_reloc, /* */
    375  1.1  christos 	 "R_MN10300_GOT24",	/* name */
    376  1.1  christos 	 FALSE,			/* partial_inplace */
    377  1.1  christos 	 0xffffffff,		/* src_mask */
    378  1.1  christos 	 0xffffffff,		/* dst_mask */
    379  1.1  christos 	 FALSE),		/* pcrel_offset */
    380  1.1  christos 
    381  1.1  christos   HOWTO (R_MN10300_GOT16,	/* type */
    382  1.1  christos 	 0,			/* rightshift */
    383  1.1  christos 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    384  1.1  christos 	 16,			/* bitsize */
    385  1.1  christos 	 FALSE,			/* pc_relative */
    386  1.1  christos 	 0,			/* bitpos */
    387  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    388  1.1  christos 	 bfd_elf_generic_reloc, /* */
    389  1.1  christos 	 "R_MN10300_GOT16",	/* name */
    390  1.1  christos 	 FALSE,			/* partial_inplace */
    391  1.1  christos 	 0xffffffff,		/* src_mask */
    392  1.1  christos 	 0xffffffff,		/* dst_mask */
    393  1.1  christos 	 FALSE),		/* pcrel_offset */
    394  1.1  christos 
    395  1.1  christos   HOWTO (R_MN10300_COPY,	/* type */
    396  1.1  christos 	 0,			/* rightshift */
    397  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    398  1.1  christos 	 32,			/* bitsize */
    399  1.1  christos 	 FALSE,			/* pc_relative */
    400  1.1  christos 	 0,			/* bitpos */
    401  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    402  1.1  christos 	 bfd_elf_generic_reloc, /* */
    403  1.1  christos 	 "R_MN10300_COPY",		/* name */
    404  1.1  christos 	 FALSE,			/* partial_inplace */
    405  1.1  christos 	 0xffffffff,		/* src_mask */
    406  1.1  christos 	 0xffffffff,		/* dst_mask */
    407  1.1  christos 	 FALSE),		/* pcrel_offset */
    408  1.1  christos 
    409  1.1  christos   HOWTO (R_MN10300_GLOB_DAT,	/* type */
    410  1.1  christos 	 0,			/* rightshift */
    411  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    412  1.1  christos 	 32,			/* bitsize */
    413  1.1  christos 	 FALSE,			/* pc_relative */
    414  1.1  christos 	 0,			/* bitpos */
    415  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    416  1.1  christos 	 bfd_elf_generic_reloc, /* */
    417  1.1  christos 	 "R_MN10300_GLOB_DAT",	/* name */
    418  1.1  christos 	 FALSE,			/* partial_inplace */
    419  1.1  christos 	 0xffffffff,		/* src_mask */
    420  1.1  christos 	 0xffffffff,		/* dst_mask */
    421  1.1  christos 	 FALSE),		/* pcrel_offset */
    422  1.1  christos 
    423  1.1  christos   HOWTO (R_MN10300_JMP_SLOT,	/* type */
    424  1.1  christos 	 0,			/* rightshift */
    425  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    426  1.1  christos 	 32,			/* bitsize */
    427  1.1  christos 	 FALSE,			/* pc_relative */
    428  1.1  christos 	 0,			/* bitpos */
    429  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    430  1.1  christos 	 bfd_elf_generic_reloc, /* */
    431  1.1  christos 	 "R_MN10300_JMP_SLOT",	/* name */
    432  1.1  christos 	 FALSE,			/* partial_inplace */
    433  1.1  christos 	 0xffffffff,		/* src_mask */
    434  1.1  christos 	 0xffffffff,		/* dst_mask */
    435  1.1  christos 	 FALSE),		/* pcrel_offset */
    436  1.1  christos 
    437  1.1  christos   HOWTO (R_MN10300_RELATIVE,	/* type */
    438  1.1  christos 	 0,			/* rightshift */
    439  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    440  1.1  christos 	 32,			/* bitsize */
    441  1.1  christos 	 FALSE,			/* pc_relative */
    442  1.1  christos 	 0,			/* bitpos */
    443  1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    444  1.1  christos 	 bfd_elf_generic_reloc, /* */
    445  1.1  christos 	 "R_MN10300_RELATIVE",	/* name */
    446  1.1  christos 	 FALSE,			/* partial_inplace */
    447  1.1  christos 	 0xffffffff,		/* src_mask */
    448  1.1  christos 	 0xffffffff,		/* dst_mask */
    449  1.1  christos 	 FALSE),		/* pcrel_offset */
    450  1.1  christos 
    451  1.1  christos   EMPTY_HOWTO (24),
    452  1.1  christos   EMPTY_HOWTO (25),
    453  1.1  christos   EMPTY_HOWTO (26),
    454  1.1  christos   EMPTY_HOWTO (27),
    455  1.1  christos   EMPTY_HOWTO (28),
    456  1.1  christos   EMPTY_HOWTO (29),
    457  1.1  christos   EMPTY_HOWTO (30),
    458  1.1  christos   EMPTY_HOWTO (31),
    459  1.1  christos   EMPTY_HOWTO (32),
    460  1.1  christos 
    461  1.1  christos   HOWTO (R_MN10300_SYM_DIFF,	/* type */
    462  1.1  christos 	 0,			/* rightshift */
    463  1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    464  1.1  christos 	 32,			/* bitsize */
    465  1.1  christos 	 FALSE,			/* pc_relative */
    466  1.1  christos 	 0,			/* bitpos */
    467  1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
    468  1.1  christos 	 NULL, 			/* special handler.  */
    469  1.1  christos 	 "R_MN10300_SYM_DIFF",	/* name */
    470  1.1  christos 	 FALSE,			/* partial_inplace */
    471  1.1  christos 	 0xffffffff,		/* src_mask */
    472  1.1  christos 	 0xffffffff,		/* dst_mask */
    473  1.1  christos 	 FALSE),		/* pcrel_offset */
    474  1.1  christos 
    475  1.1  christos   HOWTO (R_MN10300_ALIGN,	/* type */
    476  1.1  christos 	 0,			/* rightshift */
    477  1.1  christos 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    478  1.1  christos 	 32,			/* bitsize */
    479  1.1  christos 	 FALSE,			/* pc_relative */
    480  1.1  christos 	 0,			/* bitpos */
    481  1.1  christos 	 complain_overflow_dont,/* complain_on_overflow */
    482  1.1  christos 	 NULL, 			/* special handler.  */
    483  1.1  christos 	 "R_MN10300_ALIGN",	/* name */
    484  1.1  christos 	 FALSE,			/* partial_inplace */
    485  1.1  christos 	 0,			/* src_mask */
    486  1.1  christos 	 0,			/* dst_mask */
    487  1.1  christos 	 FALSE)			/* pcrel_offset */
    488  1.1  christos };
    489  1.1  christos 
    490  1.1  christos struct mn10300_reloc_map
    491  1.1  christos {
    492  1.1  christos   bfd_reloc_code_real_type bfd_reloc_val;
    493  1.1  christos   unsigned char elf_reloc_val;
    494  1.1  christos };
    495  1.1  christos 
    496  1.1  christos static const struct mn10300_reloc_map mn10300_reloc_map[] =
    497  1.1  christos {
    498  1.1  christos   { BFD_RELOC_NONE, R_MN10300_NONE, },
    499  1.1  christos   { BFD_RELOC_32, R_MN10300_32, },
    500  1.1  christos   { BFD_RELOC_16, R_MN10300_16, },
    501  1.1  christos   { BFD_RELOC_8, R_MN10300_8, },
    502  1.1  christos   { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
    503  1.1  christos   { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
    504  1.1  christos   { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
    505  1.1  christos   { BFD_RELOC_24, R_MN10300_24, },
    506  1.1  christos   { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
    507  1.1  christos   { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
    508  1.1  christos   { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
    509  1.1  christos   { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
    510  1.1  christos   { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
    511  1.1  christos   { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
    512  1.1  christos   { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
    513  1.1  christos   { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
    514  1.1  christos   { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
    515  1.1  christos   { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
    516  1.1  christos   { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
    517  1.1  christos   { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
    518  1.1  christos   { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
    519  1.1  christos   { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
    520  1.1  christos   { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
    521  1.1  christos   { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
    522  1.1  christos   { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF },
    523  1.1  christos   { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN }
    524  1.1  christos };
    525  1.1  christos 
    526  1.1  christos /* Create the GOT section.  */
    527  1.1  christos 
    528  1.1  christos static bfd_boolean
    529  1.1  christos _bfd_mn10300_elf_create_got_section (bfd * abfd,
    530  1.1  christos 				     struct bfd_link_info * info)
    531  1.1  christos {
    532  1.1  christos   flagword   flags;
    533  1.1  christos   flagword   pltflags;
    534  1.1  christos   asection * s;
    535  1.1  christos   struct elf_link_hash_entry * h;
    536  1.1  christos   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
    537  1.1  christos   int ptralign;
    538  1.1  christos 
    539  1.1  christos   /* This function may be called more than once.  */
    540  1.1  christos   if (bfd_get_section_by_name (abfd, ".got") != NULL)
    541  1.1  christos     return TRUE;
    542  1.1  christos 
    543  1.1  christos   switch (bed->s->arch_size)
    544  1.1  christos     {
    545  1.1  christos     case 32:
    546  1.1  christos       ptralign = 2;
    547  1.1  christos       break;
    548  1.1  christos 
    549  1.1  christos     case 64:
    550  1.1  christos       ptralign = 3;
    551  1.1  christos       break;
    552  1.1  christos 
    553  1.1  christos     default:
    554  1.1  christos       bfd_set_error (bfd_error_bad_value);
    555  1.1  christos       return FALSE;
    556  1.1  christos     }
    557  1.1  christos 
    558  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
    559  1.1  christos 	   | SEC_LINKER_CREATED);
    560  1.1  christos 
    561  1.1  christos   pltflags = flags;
    562  1.1  christos   pltflags |= SEC_CODE;
    563  1.1  christos   if (bed->plt_not_loaded)
    564  1.1  christos     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
    565  1.1  christos   if (bed->plt_readonly)
    566  1.1  christos     pltflags |= SEC_READONLY;
    567  1.1  christos 
    568  1.1  christos   s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
    569  1.1  christos   if (s == NULL
    570  1.1  christos       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
    571  1.1  christos     return FALSE;
    572  1.1  christos 
    573  1.1  christos   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
    574  1.1  christos      .plt section.  */
    575  1.1  christos   if (bed->want_plt_sym)
    576  1.1  christos     {
    577  1.1  christos       h = _bfd_elf_define_linkage_sym (abfd, info, s,
    578  1.1  christos 				       "_PROCEDURE_LINKAGE_TABLE_");
    579  1.1  christos       elf_hash_table (info)->hplt = h;
    580  1.1  christos       if (h == NULL)
    581  1.1  christos 	return FALSE;
    582  1.1  christos     }
    583  1.1  christos 
    584  1.1  christos   s = bfd_make_section_with_flags (abfd, ".got", flags);
    585  1.1  christos   if (s == NULL
    586  1.1  christos       || ! bfd_set_section_alignment (abfd, s, ptralign))
    587  1.1  christos     return FALSE;
    588  1.1  christos 
    589  1.1  christos   if (bed->want_got_plt)
    590  1.1  christos     {
    591  1.1  christos       s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
    592  1.1  christos       if (s == NULL
    593  1.1  christos 	  || ! bfd_set_section_alignment (abfd, s, ptralign))
    594  1.1  christos 	return FALSE;
    595  1.1  christos     }
    596  1.1  christos 
    597  1.1  christos   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    598  1.1  christos      (or .got.plt) section.  We don't do this in the linker script
    599  1.1  christos      because we don't want to define the symbol if we are not creating
    600  1.1  christos      a global offset table.  */
    601  1.1  christos   h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
    602  1.1  christos   elf_hash_table (info)->hgot = h;
    603  1.1  christos   if (h == NULL)
    604  1.1  christos     return FALSE;
    605  1.1  christos 
    606  1.1  christos   /* The first bit of the global offset table is the header.  */
    607  1.1  christos   s->size += bed->got_header_size;
    608  1.1  christos 
    609  1.1  christos   return TRUE;
    610  1.1  christos }
    611  1.1  christos 
    612  1.1  christos static reloc_howto_type *
    613  1.1  christos bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    614  1.1  christos 				 bfd_reloc_code_real_type code)
    615  1.1  christos {
    616  1.1  christos   unsigned int i;
    617  1.1  christos 
    618  1.1  christos   for (i = ARRAY_SIZE (mn10300_reloc_map); i--;)
    619  1.1  christos     if (mn10300_reloc_map[i].bfd_reloc_val == code)
    620  1.1  christos       return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
    621  1.1  christos 
    622  1.1  christos   return NULL;
    623  1.1  christos }
    624  1.1  christos 
    625  1.1  christos static reloc_howto_type *
    626  1.1  christos bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    627  1.1  christos 				 const char *r_name)
    628  1.1  christos {
    629  1.1  christos   unsigned int i;
    630  1.1  christos 
    631  1.1  christos   for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;)
    632  1.1  christos     if (elf_mn10300_howto_table[i].name != NULL
    633  1.1  christos 	&& strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
    634  1.1  christos       return elf_mn10300_howto_table + i;
    635  1.1  christos 
    636  1.1  christos   return NULL;
    637  1.1  christos }
    638  1.1  christos 
    639  1.1  christos /* Set the howto pointer for an MN10300 ELF reloc.  */
    640  1.1  christos 
    641  1.1  christos static void
    642  1.1  christos mn10300_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
    643  1.1  christos 		       arelent *cache_ptr,
    644  1.1  christos 		       Elf_Internal_Rela *dst)
    645  1.1  christos {
    646  1.1  christos   unsigned int r_type;
    647  1.1  christos 
    648  1.1  christos   r_type = ELF32_R_TYPE (dst->r_info);
    649  1.1  christos   BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
    650  1.1  christos   cache_ptr->howto = elf_mn10300_howto_table + r_type;
    651  1.1  christos }
    652  1.1  christos 
    653  1.1  christos /* Look through the relocs for a section during the first phase.
    654  1.1  christos    Since we don't do .gots or .plts, we just need to consider the
    655  1.1  christos    virtual table relocs for gc.  */
    656  1.1  christos 
    657  1.1  christos static bfd_boolean
    658  1.1  christos mn10300_elf_check_relocs (bfd *abfd,
    659  1.1  christos 			  struct bfd_link_info *info,
    660  1.1  christos 			  asection *sec,
    661  1.1  christos 			  const Elf_Internal_Rela *relocs)
    662  1.1  christos {
    663  1.1  christos   bfd_boolean sym_diff_reloc_seen;
    664  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
    665  1.1  christos   Elf_Internal_Sym * isymbuf = NULL;
    666  1.1  christos   struct elf_link_hash_entry **sym_hashes;
    667  1.1  christos   const Elf_Internal_Rela *rel;
    668  1.1  christos   const Elf_Internal_Rela *rel_end;
    669  1.1  christos   bfd *      dynobj;
    670  1.1  christos   bfd_vma *  local_got_offsets;
    671  1.1  christos   asection * sgot;
    672  1.1  christos   asection * srelgot;
    673  1.1  christos   asection * sreloc;
    674  1.1  christos   bfd_boolean result = FALSE;
    675  1.1  christos 
    676  1.1  christos   sgot    = NULL;
    677  1.1  christos   srelgot = NULL;
    678  1.1  christos   sreloc  = NULL;
    679  1.1  christos 
    680  1.1  christos   if (info->relocatable)
    681  1.1  christos     return TRUE;
    682  1.1  christos 
    683  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    684  1.1  christos   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
    685  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
    686  1.1  christos 
    687  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
    688  1.1  christos   local_got_offsets = elf_local_got_offsets (abfd);
    689  1.1  christos   rel_end = relocs + sec->reloc_count;
    690  1.1  christos   sym_diff_reloc_seen = FALSE;
    691  1.1  christos 
    692  1.1  christos   for (rel = relocs; rel < rel_end; rel++)
    693  1.1  christos     {
    694  1.1  christos       struct elf_link_hash_entry *h;
    695  1.1  christos       unsigned long r_symndx;
    696  1.1  christos       unsigned int r_type;
    697  1.1  christos 
    698  1.1  christos       r_symndx = ELF32_R_SYM (rel->r_info);
    699  1.1  christos       if (r_symndx < symtab_hdr->sh_info)
    700  1.1  christos 	h = NULL;
    701  1.1  christos       else
    702  1.1  christos 	{
    703  1.1  christos 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
    704  1.1  christos 	  while (h->root.type == bfd_link_hash_indirect
    705  1.1  christos 		 || h->root.type == bfd_link_hash_warning)
    706  1.1  christos 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
    707  1.1  christos 	}
    708  1.1  christos 
    709  1.1  christos       r_type = ELF32_R_TYPE (rel->r_info);
    710  1.1  christos 
    711  1.1  christos       /* Some relocs require a global offset table.  */
    712  1.1  christos       if (dynobj == NULL)
    713  1.1  christos 	{
    714  1.1  christos 	  switch (r_type)
    715  1.1  christos 	    {
    716  1.1  christos 	    case R_MN10300_GOT32:
    717  1.1  christos 	    case R_MN10300_GOT24:
    718  1.1  christos 	    case R_MN10300_GOT16:
    719  1.1  christos 	    case R_MN10300_GOTOFF32:
    720  1.1  christos 	    case R_MN10300_GOTOFF24:
    721  1.1  christos 	    case R_MN10300_GOTOFF16:
    722  1.1  christos 	    case R_MN10300_GOTPC32:
    723  1.1  christos 	    case R_MN10300_GOTPC16:
    724  1.1  christos 	      elf_hash_table (info)->dynobj = dynobj = abfd;
    725  1.1  christos 	      if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
    726  1.1  christos 		goto fail;
    727  1.1  christos 	      break;
    728  1.1  christos 
    729  1.1  christos 	    default:
    730  1.1  christos 	      break;
    731  1.1  christos 	    }
    732  1.1  christos 	}
    733  1.1  christos 
    734  1.1  christos       switch (r_type)
    735  1.1  christos 	{
    736  1.1  christos 	/* This relocation describes the C++ object vtable hierarchy.
    737  1.1  christos 	   Reconstruct it for later use during GC.  */
    738  1.1  christos 	case R_MN10300_GNU_VTINHERIT:
    739  1.1  christos 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
    740  1.1  christos 	    goto fail;
    741  1.1  christos 	  break;
    742  1.1  christos 
    743  1.1  christos 	/* This relocation describes which C++ vtable entries are actually
    744  1.1  christos 	   used.  Record for later use during GC.  */
    745  1.1  christos 	case R_MN10300_GNU_VTENTRY:
    746  1.1  christos 	  BFD_ASSERT (h != NULL);
    747  1.1  christos 	  if (h != NULL
    748  1.1  christos 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
    749  1.1  christos 	    goto fail;
    750  1.1  christos 	  break;
    751  1.1  christos 
    752  1.1  christos 	case R_MN10300_GOT32:
    753  1.1  christos 	case R_MN10300_GOT24:
    754  1.1  christos 	case R_MN10300_GOT16:
    755  1.1  christos 	  /* This symbol requires a global offset table entry.  */
    756  1.1  christos 
    757  1.1  christos 	  if (sgot == NULL)
    758  1.1  christos 	    {
    759  1.1  christos 	      sgot = bfd_get_section_by_name (dynobj, ".got");
    760  1.1  christos 	      BFD_ASSERT (sgot != NULL);
    761  1.1  christos 	    }
    762  1.1  christos 
    763  1.1  christos 	  if (srelgot == NULL
    764  1.1  christos 	      && (h != NULL || info->shared))
    765  1.1  christos 	    {
    766  1.1  christos 	      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
    767  1.1  christos 	      if (srelgot == NULL)
    768  1.1  christos 		{
    769  1.1  christos 		  srelgot = bfd_make_section_with_flags (dynobj,
    770  1.1  christos 							 ".rela.got",
    771  1.1  christos 							 (SEC_ALLOC
    772  1.1  christos 							  | SEC_LOAD
    773  1.1  christos 							  | SEC_HAS_CONTENTS
    774  1.1  christos 							  | SEC_IN_MEMORY
    775  1.1  christos 							  | SEC_LINKER_CREATED
    776  1.1  christos 							  | SEC_READONLY));
    777  1.1  christos 		  if (srelgot == NULL
    778  1.1  christos 		      || ! bfd_set_section_alignment (dynobj, srelgot, 2))
    779  1.1  christos 		    goto fail;
    780  1.1  christos 		}
    781  1.1  christos 	    }
    782  1.1  christos 
    783  1.1  christos 	  if (h != NULL)
    784  1.1  christos 	    {
    785  1.1  christos 	      if (h->got.offset != (bfd_vma) -1)
    786  1.1  christos 		/* We have already allocated space in the .got.  */
    787  1.1  christos 		break;
    788  1.1  christos 
    789  1.1  christos 	      h->got.offset = sgot->size;
    790  1.1  christos 
    791  1.1  christos 	      /* Make sure this symbol is output as a dynamic symbol.  */
    792  1.1  christos 	      if (h->dynindx == -1)
    793  1.1  christos 		{
    794  1.1  christos 		  if (! bfd_elf_link_record_dynamic_symbol (info, h))
    795  1.1  christos 		    goto fail;
    796  1.1  christos 		}
    797  1.1  christos 
    798  1.1  christos 	      srelgot->size += sizeof (Elf32_External_Rela);
    799  1.1  christos 	    }
    800  1.1  christos 	  else
    801  1.1  christos 	    {
    802  1.1  christos 	      /* This is a global offset table entry for a local
    803  1.1  christos 		 symbol.  */
    804  1.1  christos 	      if (local_got_offsets == NULL)
    805  1.1  christos 		{
    806  1.1  christos 		  size_t       size;
    807  1.1  christos 		  unsigned int i;
    808  1.1  christos 
    809  1.1  christos 		  size = symtab_hdr->sh_info * sizeof (bfd_vma);
    810  1.1  christos 		  local_got_offsets = bfd_alloc (abfd, size);
    811  1.1  christos 
    812  1.1  christos 		  if (local_got_offsets == NULL)
    813  1.1  christos 		    goto fail;
    814  1.1  christos 
    815  1.1  christos 		  elf_local_got_offsets (abfd) = local_got_offsets;
    816  1.1  christos 
    817  1.1  christos 		  for (i = 0; i < symtab_hdr->sh_info; i++)
    818  1.1  christos 		    local_got_offsets[i] = (bfd_vma) -1;
    819  1.1  christos 		}
    820  1.1  christos 
    821  1.1  christos 	      if (local_got_offsets[r_symndx] != (bfd_vma) -1)
    822  1.1  christos 		/* We have already allocated space in the .got.  */
    823  1.1  christos 		break;
    824  1.1  christos 
    825  1.1  christos 	      local_got_offsets[r_symndx] = sgot->size;
    826  1.1  christos 
    827  1.1  christos 	      if (info->shared)
    828  1.1  christos 		/* If we are generating a shared object, we need to
    829  1.1  christos 		   output a R_MN10300_RELATIVE reloc so that the dynamic
    830  1.1  christos 		   linker can adjust this GOT entry.  */
    831  1.1  christos 		srelgot->size += sizeof (Elf32_External_Rela);
    832  1.1  christos 	    }
    833  1.1  christos 
    834  1.1  christos 	  sgot->size += 4;
    835  1.1  christos 	  break;
    836  1.1  christos 
    837  1.1  christos 	case R_MN10300_PLT32:
    838  1.1  christos 	case R_MN10300_PLT16:
    839  1.1  christos 	  /* This symbol requires a procedure linkage table entry.  We
    840  1.1  christos 	     actually build the entry in adjust_dynamic_symbol,
    841  1.1  christos 	     because this might be a case of linking PIC code which is
    842  1.1  christos 	     never referenced by a dynamic object, in which case we
    843  1.1  christos 	     don't need to generate a procedure linkage table entry
    844  1.1  christos 	     after all.  */
    845  1.1  christos 
    846  1.1  christos 	  /* If this is a local symbol, we resolve it directly without
    847  1.1  christos 	     creating a procedure linkage table entry.  */
    848  1.1  christos 	  if (h == NULL)
    849  1.1  christos 	    continue;
    850  1.1  christos 
    851  1.1  christos 	  if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
    852  1.1  christos 	      || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
    853  1.1  christos 	    break;
    854  1.1  christos 
    855  1.1  christos 	  h->needs_plt = 1;
    856  1.1  christos 	  break;
    857  1.1  christos 
    858  1.1  christos 	case R_MN10300_24:
    859  1.1  christos 	case R_MN10300_16:
    860  1.1  christos 	case R_MN10300_8:
    861  1.1  christos 	case R_MN10300_PCREL32:
    862  1.1  christos 	case R_MN10300_PCREL16:
    863  1.1  christos 	case R_MN10300_PCREL8:
    864  1.1  christos 	  if (h != NULL)
    865  1.1  christos 	    h->non_got_ref = 1;
    866  1.1  christos 	  break;
    867  1.1  christos 
    868  1.1  christos 	case R_MN10300_SYM_DIFF:
    869  1.1  christos 	  sym_diff_reloc_seen = TRUE;
    870  1.1  christos 	  break;
    871  1.1  christos 
    872  1.1  christos 	case R_MN10300_32:
    873  1.1  christos 	  if (h != NULL)
    874  1.1  christos 	    h->non_got_ref = 1;
    875  1.1  christos 
    876  1.1  christos 	  /* If we are creating a shared library, then we
    877  1.1  christos 	     need to copy the reloc into the shared library.  */
    878  1.1  christos 	  if (info->shared
    879  1.1  christos 	      && (sec->flags & SEC_ALLOC) != 0
    880  1.1  christos 	      /* Do not generate a dynamic reloc for a
    881  1.1  christos 		 reloc associated with a SYM_DIFF operation.  */
    882  1.1  christos 	      && ! sym_diff_reloc_seen)
    883  1.1  christos 	    {
    884  1.1  christos 	      asection * sym_section = NULL;
    885  1.1  christos 
    886  1.1  christos 	      /* Find the section containing the
    887  1.1  christos 		 symbol involved in the relocation.  */
    888  1.1  christos 	      if (h == NULL)
    889  1.1  christos 		{
    890  1.1  christos 		  Elf_Internal_Sym * isym;
    891  1.1  christos 
    892  1.1  christos 		  if (isymbuf == NULL)
    893  1.1  christos 		    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
    894  1.1  christos 						    symtab_hdr->sh_info, 0,
    895  1.1  christos 						    NULL, NULL, NULL);
    896  1.1  christos 		  if (isymbuf)
    897  1.1  christos 		    {
    898  1.1  christos 		      isym = isymbuf + r_symndx;
    899  1.1  christos 		      /* All we care about is whether this local symbol is absolute.  */
    900  1.1  christos 		      if (isym->st_shndx == SHN_ABS)
    901  1.1  christos 			sym_section = bfd_abs_section_ptr;
    902  1.1  christos 		    }
    903  1.1  christos 		}
    904  1.1  christos 	      else
    905  1.1  christos 		{
    906  1.1  christos 		  if (h->root.type == bfd_link_hash_defined
    907  1.1  christos 		      || h->root.type == bfd_link_hash_defweak)
    908  1.1  christos 		    sym_section = h->root.u.def.section;
    909  1.1  christos 		}
    910  1.1  christos 
    911  1.1  christos 	      /* If the symbol is absolute then the relocation can
    912  1.1  christos 		 be resolved during linking and there is no need for
    913  1.1  christos 		 a dynamic reloc.  */
    914  1.1  christos 	      if (sym_section != bfd_abs_section_ptr)
    915  1.1  christos 		{
    916  1.1  christos 		  /* When creating a shared object, we must copy these
    917  1.1  christos 		     reloc types into the output file.  We create a reloc
    918  1.1  christos 		     section in dynobj and make room for this reloc.  */
    919  1.1  christos 		  if (sreloc == NULL)
    920  1.1  christos 		    {
    921  1.1  christos 		      sreloc = _bfd_elf_make_dynamic_reloc_section
    922  1.1  christos 			(sec, dynobj, 2, abfd, /*rela?*/ TRUE);
    923  1.1  christos 		      if (sreloc == NULL)
    924  1.1  christos 			goto fail;
    925  1.1  christos 		    }
    926  1.1  christos 
    927  1.1  christos 		  sreloc->size += sizeof (Elf32_External_Rela);
    928  1.1  christos 		}
    929  1.1  christos 	    }
    930  1.1  christos 
    931  1.1  christos 	  break;
    932  1.1  christos 	}
    933  1.1  christos 
    934  1.1  christos       if (ELF32_R_TYPE (rel->r_info) != R_MN10300_SYM_DIFF)
    935  1.1  christos 	sym_diff_reloc_seen = FALSE;
    936  1.1  christos     }
    937  1.1  christos 
    938  1.1  christos   result = TRUE;
    939  1.1  christos  fail:
    940  1.1  christos   if (isymbuf != NULL)
    941  1.1  christos     free (isymbuf);
    942  1.1  christos 
    943  1.1  christos   return result;
    944  1.1  christos }
    945  1.1  christos 
    946  1.1  christos /* Return the section that should be marked against GC for a given
    947  1.1  christos    relocation.  */
    948  1.1  christos 
    949  1.1  christos static asection *
    950  1.1  christos mn10300_elf_gc_mark_hook (asection *sec,
    951  1.1  christos 			  struct bfd_link_info *info,
    952  1.1  christos 			  Elf_Internal_Rela *rel,
    953  1.1  christos 			  struct elf_link_hash_entry *h,
    954  1.1  christos 			  Elf_Internal_Sym *sym)
    955  1.1  christos {
    956  1.1  christos   if (h != NULL)
    957  1.1  christos     switch (ELF32_R_TYPE (rel->r_info))
    958  1.1  christos       {
    959  1.1  christos       case R_MN10300_GNU_VTINHERIT:
    960  1.1  christos       case R_MN10300_GNU_VTENTRY:
    961  1.1  christos 	return NULL;
    962  1.1  christos       }
    963  1.1  christos 
    964  1.1  christos   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
    965  1.1  christos }
    966  1.1  christos 
    967  1.1  christos /* Perform a relocation as part of a final link.  */
    968  1.1  christos 
    969  1.1  christos static bfd_reloc_status_type
    970  1.1  christos mn10300_elf_final_link_relocate (reloc_howto_type *howto,
    971  1.1  christos 				 bfd *input_bfd,
    972  1.1  christos 				 bfd *output_bfd ATTRIBUTE_UNUSED,
    973  1.1  christos 				 asection *input_section,
    974  1.1  christos 				 bfd_byte *contents,
    975  1.1  christos 				 bfd_vma offset,
    976  1.1  christos 				 bfd_vma value,
    977  1.1  christos 				 bfd_vma addend,
    978  1.1  christos 				 struct elf_link_hash_entry * h,
    979  1.1  christos 				 unsigned long symndx,
    980  1.1  christos 				 struct bfd_link_info *info,
    981  1.1  christos 				 asection *sym_sec ATTRIBUTE_UNUSED,
    982  1.1  christos 				 int is_local ATTRIBUTE_UNUSED)
    983  1.1  christos {
    984  1.1  christos   static asection *  sym_diff_section;
    985  1.1  christos   static bfd_vma     sym_diff_value;
    986  1.1  christos   bfd_boolean is_sym_diff_reloc;
    987  1.1  christos   unsigned long r_type = howto->type;
    988  1.1  christos   bfd_byte * hit_data = contents + offset;
    989  1.1  christos   bfd *      dynobj;
    990  1.1  christos   asection * sgot;
    991  1.1  christos   asection * splt;
    992  1.1  christos   asection * sreloc;
    993  1.1  christos 
    994  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
    995  1.1  christos   sgot   = NULL;
    996  1.1  christos   splt   = NULL;
    997  1.1  christos   sreloc = NULL;
    998  1.1  christos 
    999  1.1  christos   switch (r_type)
   1000  1.1  christos     {
   1001  1.1  christos     case R_MN10300_24:
   1002  1.1  christos     case R_MN10300_16:
   1003  1.1  christos     case R_MN10300_8:
   1004  1.1  christos     case R_MN10300_PCREL8:
   1005  1.1  christos     case R_MN10300_PCREL16:
   1006  1.1  christos     case R_MN10300_PCREL32:
   1007  1.1  christos     case R_MN10300_GOTOFF32:
   1008  1.1  christos     case R_MN10300_GOTOFF24:
   1009  1.1  christos     case R_MN10300_GOTOFF16:
   1010  1.1  christos       if (info->shared
   1011  1.1  christos 	  && (input_section->flags & SEC_ALLOC) != 0
   1012  1.1  christos 	  && h != NULL
   1013  1.1  christos 	  && ! SYMBOL_REFERENCES_LOCAL (info, h))
   1014  1.1  christos 	return bfd_reloc_dangerous;
   1015  1.1  christos     }
   1016  1.1  christos 
   1017  1.1  christos   is_sym_diff_reloc = FALSE;
   1018  1.1  christos   if (sym_diff_section != NULL)
   1019  1.1  christos     {
   1020  1.1  christos       BFD_ASSERT (sym_diff_section == input_section);
   1021  1.1  christos 
   1022  1.1  christos       switch (r_type)
   1023  1.1  christos 	{
   1024  1.1  christos 	case R_MN10300_32:
   1025  1.1  christos 	case R_MN10300_24:
   1026  1.1  christos 	case R_MN10300_16:
   1027  1.1  christos 	case R_MN10300_8:
   1028  1.1  christos 	  value -= sym_diff_value;
   1029  1.1  christos 	  /* If we are computing a 32-bit value for the location lists
   1030  1.1  christos 	     and the result is 0 then we add one to the value.  A zero
   1031  1.1  christos 	     value can result because of linker relaxation deleteing
   1032  1.1  christos 	     prologue instructions and using a value of 1 (for the begin
   1033  1.1  christos 	     and end offsets in the location list entry) results in a
   1034  1.1  christos 	     nul entry which does not prevent the following entries from
   1035  1.1  christos 	     being parsed.  */
   1036  1.1  christos 	  if (r_type == R_MN10300_32
   1037  1.1  christos 	      && value == 0
   1038  1.1  christos 	      && strcmp (input_section->name, ".debug_loc") == 0)
   1039  1.1  christos 	    value = 1;
   1040  1.1  christos 	  sym_diff_section = NULL;
   1041  1.1  christos 	  is_sym_diff_reloc = TRUE;
   1042  1.1  christos 	  break;
   1043  1.1  christos 
   1044  1.1  christos 	default:
   1045  1.1  christos 	  sym_diff_section = NULL;
   1046  1.1  christos 	  break;
   1047  1.1  christos 	}
   1048  1.1  christos     }
   1049  1.1  christos 
   1050  1.1  christos   switch (r_type)
   1051  1.1  christos     {
   1052  1.1  christos     case R_MN10300_SYM_DIFF:
   1053  1.1  christos       BFD_ASSERT (addend == 0);
   1054  1.1  christos       /* Cache the input section and value.
   1055  1.1  christos 	 The offset is unreliable, since relaxation may
   1056  1.1  christos 	 have reduced the following reloc's offset.  */
   1057  1.1  christos       sym_diff_section = input_section;
   1058  1.1  christos       sym_diff_value = value;
   1059  1.1  christos       return bfd_reloc_ok;
   1060  1.1  christos 
   1061  1.1  christos     case R_MN10300_ALIGN:
   1062  1.1  christos     case R_MN10300_NONE:
   1063  1.1  christos       return bfd_reloc_ok;
   1064  1.1  christos 
   1065  1.1  christos     case R_MN10300_32:
   1066  1.1  christos       if (info->shared
   1067  1.1  christos 	  /* Do not generate relocs when an R_MN10300_32 has been used
   1068  1.1  christos 	     with an R_MN10300_SYM_DIFF to compute a difference of two
   1069  1.1  christos 	     symbols.  */
   1070  1.1  christos 	  && is_sym_diff_reloc == FALSE
   1071  1.1  christos 	  /* Also, do not generate a reloc when the symbol associated
   1072  1.1  christos 	     with the R_MN10300_32 reloc is absolute - there is no
   1073  1.1  christos 	     need for a run time computation in this case.  */
   1074  1.1  christos 	  && sym_sec != bfd_abs_section_ptr
   1075  1.1  christos 	  /* If the section is not going to be allocated at load time
   1076  1.1  christos 	     then there is no need to generate relocs for it.  */
   1077  1.1  christos 	  && (input_section->flags & SEC_ALLOC) != 0)
   1078  1.1  christos 	{
   1079  1.1  christos 	  Elf_Internal_Rela outrel;
   1080  1.1  christos 	  bfd_boolean skip, relocate;
   1081  1.1  christos 
   1082  1.1  christos 	  /* When generating a shared object, these relocations are
   1083  1.1  christos 	     copied into the output file to be resolved at run
   1084  1.1  christos 	     time.  */
   1085  1.1  christos 	  if (sreloc == NULL)
   1086  1.1  christos 	    {
   1087  1.1  christos 	      sreloc = _bfd_elf_get_dynamic_reloc_section
   1088  1.1  christos 		(input_bfd, input_section, /*rela?*/ TRUE);
   1089  1.1  christos 	      if (sreloc == NULL)
   1090  1.1  christos 		return FALSE;
   1091  1.1  christos 	    }
   1092  1.1  christos 
   1093  1.1  christos 	  skip = FALSE;
   1094  1.1  christos 
   1095  1.1  christos 	  outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
   1096  1.1  christos 						     input_section, offset);
   1097  1.1  christos 	  if (outrel.r_offset == (bfd_vma) -1)
   1098  1.1  christos 	    skip = TRUE;
   1099  1.1  christos 
   1100  1.1  christos 	  outrel.r_offset += (input_section->output_section->vma
   1101  1.1  christos 			      + input_section->output_offset);
   1102  1.1  christos 
   1103  1.1  christos 	  if (skip)
   1104  1.1  christos 	    {
   1105  1.1  christos 	      memset (&outrel, 0, sizeof outrel);
   1106  1.1  christos 	      relocate = FALSE;
   1107  1.1  christos 	    }
   1108  1.1  christos 	  else
   1109  1.1  christos 	    {
   1110  1.1  christos 	      /* h->dynindx may be -1 if this symbol was marked to
   1111  1.1  christos 		 become local.  */
   1112  1.1  christos 	      if (h == NULL
   1113  1.1  christos 		  || SYMBOL_REFERENCES_LOCAL (info, h))
   1114  1.1  christos 		{
   1115  1.1  christos 		  relocate = TRUE;
   1116  1.1  christos 		  outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
   1117  1.1  christos 		  outrel.r_addend = value + addend;
   1118  1.1  christos 		}
   1119  1.1  christos 	      else
   1120  1.1  christos 		{
   1121  1.1  christos 		  BFD_ASSERT (h->dynindx != -1);
   1122  1.1  christos 		  relocate = FALSE;
   1123  1.1  christos 		  outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
   1124  1.1  christos 		  outrel.r_addend = value + addend;
   1125  1.1  christos 		}
   1126  1.1  christos 	    }
   1127  1.1  christos 
   1128  1.1  christos 	  bfd_elf32_swap_reloca_out (output_bfd, &outrel,
   1129  1.1  christos 				     (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
   1130  1.1  christos 						   + sreloc->reloc_count));
   1131  1.1  christos 	  ++sreloc->reloc_count;
   1132  1.1  christos 
   1133  1.1  christos 	  /* If this reloc is against an external symbol, we do
   1134  1.1  christos 	     not want to fiddle with the addend.  Otherwise, we
   1135  1.1  christos 	     need to include the symbol value so that it becomes
   1136  1.1  christos 	     an addend for the dynamic reloc.  */
   1137  1.1  christos 	  if (! relocate)
   1138  1.1  christos 	    return bfd_reloc_ok;
   1139  1.1  christos 	}
   1140  1.1  christos       value += addend;
   1141  1.1  christos       bfd_put_32 (input_bfd, value, hit_data);
   1142  1.1  christos       return bfd_reloc_ok;
   1143  1.1  christos 
   1144  1.1  christos     case R_MN10300_24:
   1145  1.1  christos       value += addend;
   1146  1.1  christos 
   1147  1.1  christos       if ((long) value > 0x7fffff || (long) value < -0x800000)
   1148  1.1  christos 	return bfd_reloc_overflow;
   1149  1.1  christos 
   1150  1.1  christos       bfd_put_8 (input_bfd, value & 0xff, hit_data);
   1151  1.1  christos       bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
   1152  1.1  christos       bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
   1153  1.1  christos       return bfd_reloc_ok;
   1154  1.1  christos 
   1155  1.1  christos     case R_MN10300_16:
   1156  1.1  christos       value += addend;
   1157  1.1  christos 
   1158  1.1  christos       if ((long) value > 0x7fff || (long) value < -0x8000)
   1159  1.1  christos 	return bfd_reloc_overflow;
   1160  1.1  christos 
   1161  1.1  christos       bfd_put_16 (input_bfd, value, hit_data);
   1162  1.1  christos       return bfd_reloc_ok;
   1163  1.1  christos 
   1164  1.1  christos     case R_MN10300_8:
   1165  1.1  christos       value += addend;
   1166  1.1  christos 
   1167  1.1  christos       if ((long) value > 0x7f || (long) value < -0x80)
   1168  1.1  christos 	return bfd_reloc_overflow;
   1169  1.1  christos 
   1170  1.1  christos       bfd_put_8 (input_bfd, value, hit_data);
   1171  1.1  christos       return bfd_reloc_ok;
   1172  1.1  christos 
   1173  1.1  christos     case R_MN10300_PCREL8:
   1174  1.1  christos       value -= (input_section->output_section->vma
   1175  1.1  christos 		+ input_section->output_offset);
   1176  1.1  christos       value -= offset;
   1177  1.1  christos       value += addend;
   1178  1.1  christos 
   1179  1.1  christos       if ((long) value > 0x7f || (long) value < -0x80)
   1180  1.1  christos 	return bfd_reloc_overflow;
   1181  1.1  christos 
   1182  1.1  christos       bfd_put_8 (input_bfd, value, hit_data);
   1183  1.1  christos       return bfd_reloc_ok;
   1184  1.1  christos 
   1185  1.1  christos     case R_MN10300_PCREL16:
   1186  1.1  christos       value -= (input_section->output_section->vma
   1187  1.1  christos 		+ input_section->output_offset);
   1188  1.1  christos       value -= offset;
   1189  1.1  christos       value += addend;
   1190  1.1  christos 
   1191  1.1  christos       if ((long) value > 0x7fff || (long) value < -0x8000)
   1192  1.1  christos 	return bfd_reloc_overflow;
   1193  1.1  christos 
   1194  1.1  christos       bfd_put_16 (input_bfd, value, hit_data);
   1195  1.1  christos       return bfd_reloc_ok;
   1196  1.1  christos 
   1197  1.1  christos     case R_MN10300_PCREL32:
   1198  1.1  christos       value -= (input_section->output_section->vma
   1199  1.1  christos 		+ input_section->output_offset);
   1200  1.1  christos       value -= offset;
   1201  1.1  christos       value += addend;
   1202  1.1  christos 
   1203  1.1  christos       bfd_put_32 (input_bfd, value, hit_data);
   1204  1.1  christos       return bfd_reloc_ok;
   1205  1.1  christos 
   1206  1.1  christos     case R_MN10300_GNU_VTINHERIT:
   1207  1.1  christos     case R_MN10300_GNU_VTENTRY:
   1208  1.1  christos       return bfd_reloc_ok;
   1209  1.1  christos 
   1210  1.1  christos     case R_MN10300_GOTPC32:
   1211  1.1  christos       /* Use global offset table as symbol value.  */
   1212  1.1  christos       value = bfd_get_section_by_name (dynobj,
   1213  1.1  christos 				       ".got")->output_section->vma;
   1214  1.1  christos       value -= (input_section->output_section->vma
   1215  1.1  christos 		+ input_section->output_offset);
   1216  1.1  christos       value -= offset;
   1217  1.1  christos       value += addend;
   1218  1.1  christos 
   1219  1.1  christos       bfd_put_32 (input_bfd, value, hit_data);
   1220  1.1  christos       return bfd_reloc_ok;
   1221  1.1  christos 
   1222  1.1  christos     case R_MN10300_GOTPC16:
   1223  1.1  christos       /* Use global offset table as symbol value.  */
   1224  1.1  christos       value = bfd_get_section_by_name (dynobj,
   1225  1.1  christos 				       ".got")->output_section->vma;
   1226  1.1  christos       value -= (input_section->output_section->vma
   1227  1.1  christos 		+ input_section->output_offset);
   1228  1.1  christos       value -= offset;
   1229  1.1  christos       value += addend;
   1230  1.1  christos 
   1231  1.1  christos       if ((long) value > 0x7fff || (long) value < -0x8000)
   1232  1.1  christos 	return bfd_reloc_overflow;
   1233  1.1  christos 
   1234  1.1  christos       bfd_put_16 (input_bfd, value, hit_data);
   1235  1.1  christos       return bfd_reloc_ok;
   1236  1.1  christos 
   1237  1.1  christos     case R_MN10300_GOTOFF32:
   1238  1.1  christos       value -= bfd_get_section_by_name (dynobj,
   1239  1.1  christos 					".got")->output_section->vma;
   1240  1.1  christos       value += addend;
   1241  1.1  christos 
   1242  1.1  christos       bfd_put_32 (input_bfd, value, hit_data);
   1243  1.1  christos       return bfd_reloc_ok;
   1244  1.1  christos 
   1245  1.1  christos     case R_MN10300_GOTOFF24:
   1246  1.1  christos       value -= bfd_get_section_by_name (dynobj,
   1247  1.1  christos 					".got")->output_section->vma;
   1248  1.1  christos       value += addend;
   1249  1.1  christos 
   1250  1.1  christos       if ((long) value > 0x7fffff || (long) value < -0x800000)
   1251  1.1  christos 	return bfd_reloc_overflow;
   1252  1.1  christos 
   1253  1.1  christos       bfd_put_8 (input_bfd, value, hit_data);
   1254  1.1  christos       bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
   1255  1.1  christos       bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
   1256  1.1  christos       return bfd_reloc_ok;
   1257  1.1  christos 
   1258  1.1  christos     case R_MN10300_GOTOFF16:
   1259  1.1  christos       value -= bfd_get_section_by_name (dynobj,
   1260  1.1  christos 					".got")->output_section->vma;
   1261  1.1  christos       value += addend;
   1262  1.1  christos 
   1263  1.1  christos       if ((long) value > 0x7fff || (long) value < -0x8000)
   1264  1.1  christos 	return bfd_reloc_overflow;
   1265  1.1  christos 
   1266  1.1  christos       bfd_put_16 (input_bfd, value, hit_data);
   1267  1.1  christos       return bfd_reloc_ok;
   1268  1.1  christos 
   1269  1.1  christos     case R_MN10300_PLT32:
   1270  1.1  christos       if (h != NULL
   1271  1.1  christos 	  && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   1272  1.1  christos 	  && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   1273  1.1  christos 	  && h->plt.offset != (bfd_vma) -1)
   1274  1.1  christos 	{
   1275  1.1  christos 	  splt = bfd_get_section_by_name (dynobj, ".plt");
   1276  1.1  christos 
   1277  1.1  christos 	  value = (splt->output_section->vma
   1278  1.1  christos 		   + splt->output_offset
   1279  1.1  christos 		   + h->plt.offset) - value;
   1280  1.1  christos 	}
   1281  1.1  christos 
   1282  1.1  christos       value -= (input_section->output_section->vma
   1283  1.1  christos 		+ input_section->output_offset);
   1284  1.1  christos       value -= offset;
   1285  1.1  christos       value += addend;
   1286  1.1  christos 
   1287  1.1  christos       bfd_put_32 (input_bfd, value, hit_data);
   1288  1.1  christos       return bfd_reloc_ok;
   1289  1.1  christos 
   1290  1.1  christos     case R_MN10300_PLT16:
   1291  1.1  christos       if (h != NULL
   1292  1.1  christos 	  && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
   1293  1.1  christos 	  && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
   1294  1.1  christos 	  && h->plt.offset != (bfd_vma) -1)
   1295  1.1  christos 	{
   1296  1.1  christos 	  splt = bfd_get_section_by_name (dynobj, ".plt");
   1297  1.1  christos 
   1298  1.1  christos 	  value = (splt->output_section->vma
   1299  1.1  christos 		   + splt->output_offset
   1300  1.1  christos 		   + h->plt.offset) - value;
   1301  1.1  christos 	}
   1302  1.1  christos 
   1303  1.1  christos       value -= (input_section->output_section->vma
   1304  1.1  christos 		+ input_section->output_offset);
   1305  1.1  christos       value -= offset;
   1306  1.1  christos       value += addend;
   1307  1.1  christos 
   1308  1.1  christos       if ((long) value > 0x7fff || (long) value < -0x8000)
   1309  1.1  christos 	return bfd_reloc_overflow;
   1310  1.1  christos 
   1311  1.1  christos       bfd_put_16 (input_bfd, value, hit_data);
   1312  1.1  christos       return bfd_reloc_ok;
   1313  1.1  christos 
   1314  1.1  christos     case R_MN10300_GOT32:
   1315  1.1  christos     case R_MN10300_GOT24:
   1316  1.1  christos     case R_MN10300_GOT16:
   1317  1.1  christos       {
   1318  1.1  christos 	sgot = bfd_get_section_by_name (dynobj, ".got");
   1319  1.1  christos 
   1320  1.1  christos 	  if (h != NULL)
   1321  1.1  christos 	    {
   1322  1.1  christos 	      bfd_vma off;
   1323  1.1  christos 
   1324  1.1  christos 	      off = h->got.offset;
   1325  1.1  christos 	      BFD_ASSERT (off != (bfd_vma) -1);
   1326  1.1  christos 
   1327  1.1  christos 	      if (! elf_hash_table (info)->dynamic_sections_created
   1328  1.1  christos 		  || SYMBOL_REFERENCES_LOCAL (info, h))
   1329  1.1  christos 		/* This is actually a static link, or it is a
   1330  1.1  christos 		   -Bsymbolic link and the symbol is defined
   1331  1.1  christos 		   locally, or the symbol was forced to be local
   1332  1.1  christos 		   because of a version file.  We must initialize
   1333  1.1  christos 		   this entry in the global offset table.
   1334  1.1  christos 
   1335  1.1  christos 		   When doing a dynamic link, we create a .rela.got
   1336  1.1  christos 		   relocation entry to initialize the value.  This
   1337  1.1  christos 		   is done in the finish_dynamic_symbol routine.  */
   1338  1.1  christos 		bfd_put_32 (output_bfd, value,
   1339  1.1  christos 			    sgot->contents + off);
   1340  1.1  christos 
   1341  1.1  christos 	      value = sgot->output_offset + off;
   1342  1.1  christos 	    }
   1343  1.1  christos 	  else
   1344  1.1  christos 	    {
   1345  1.1  christos 	      bfd_vma off;
   1346  1.1  christos 
   1347  1.1  christos 	      off = elf_local_got_offsets (input_bfd)[symndx];
   1348  1.1  christos 
   1349  1.1  christos 	      bfd_put_32 (output_bfd, value, sgot->contents + off);
   1350  1.1  christos 
   1351  1.1  christos 	      if (info->shared)
   1352  1.1  christos 		{
   1353  1.1  christos 		  asection * srelgot;
   1354  1.1  christos 		  Elf_Internal_Rela outrel;
   1355  1.1  christos 
   1356  1.1  christos 		  srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
   1357  1.1  christos 		  BFD_ASSERT (srelgot != NULL);
   1358  1.1  christos 
   1359  1.1  christos 		  outrel.r_offset = (sgot->output_section->vma
   1360  1.1  christos 				     + sgot->output_offset
   1361  1.1  christos 				     + off);
   1362  1.1  christos 		  outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
   1363  1.1  christos 		  outrel.r_addend = value;
   1364  1.1  christos 		  bfd_elf32_swap_reloca_out (output_bfd, &outrel,
   1365  1.1  christos 					     (bfd_byte *) (((Elf32_External_Rela *)
   1366  1.1  christos 							    srelgot->contents)
   1367  1.1  christos 							   + srelgot->reloc_count));
   1368  1.1  christos 		  ++ srelgot->reloc_count;
   1369  1.1  christos 		}
   1370  1.1  christos 
   1371  1.1  christos 	      value = sgot->output_offset + off;
   1372  1.1  christos 	    }
   1373  1.1  christos       }
   1374  1.1  christos 
   1375  1.1  christos       value += addend;
   1376  1.1  christos 
   1377  1.1  christos       if (r_type == R_MN10300_GOT32)
   1378  1.1  christos 	{
   1379  1.1  christos 	  bfd_put_32 (input_bfd, value, hit_data);
   1380  1.1  christos 	  return bfd_reloc_ok;
   1381  1.1  christos 	}
   1382  1.1  christos       else if (r_type == R_MN10300_GOT24)
   1383  1.1  christos 	{
   1384  1.1  christos 	  if ((long) value > 0x7fffff || (long) value < -0x800000)
   1385  1.1  christos 	    return bfd_reloc_overflow;
   1386  1.1  christos 
   1387  1.1  christos 	  bfd_put_8 (input_bfd, value & 0xff, hit_data);
   1388  1.1  christos 	  bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
   1389  1.1  christos 	  bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
   1390  1.1  christos 	  return bfd_reloc_ok;
   1391  1.1  christos 	}
   1392  1.1  christos       else if (r_type == R_MN10300_GOT16)
   1393  1.1  christos 	{
   1394  1.1  christos 	  if ((long) value > 0x7fff || (long) value < -0x8000)
   1395  1.1  christos 	    return bfd_reloc_overflow;
   1396  1.1  christos 
   1397  1.1  christos 	  bfd_put_16 (input_bfd, value, hit_data);
   1398  1.1  christos 	  return bfd_reloc_ok;
   1399  1.1  christos 	}
   1400  1.1  christos       /* Fall through.  */
   1401  1.1  christos 
   1402  1.1  christos     default:
   1403  1.1  christos       return bfd_reloc_notsupported;
   1404  1.1  christos     }
   1405  1.1  christos }
   1406  1.1  christos 
   1407  1.1  christos /* Relocate an MN10300 ELF section.  */
   1409  1.1  christos 
   1410  1.1  christos static bfd_boolean
   1411  1.1  christos mn10300_elf_relocate_section (bfd *output_bfd,
   1412  1.1  christos 			      struct bfd_link_info *info,
   1413  1.1  christos 			      bfd *input_bfd,
   1414  1.1  christos 			      asection *input_section,
   1415  1.1  christos 			      bfd_byte *contents,
   1416  1.1  christos 			      Elf_Internal_Rela *relocs,
   1417  1.1  christos 			      Elf_Internal_Sym *local_syms,
   1418  1.1  christos 			      asection **local_sections)
   1419  1.1  christos {
   1420  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1421  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   1422  1.1  christos   Elf_Internal_Rela *rel, *relend;
   1423  1.1  christos 
   1424  1.1  christos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   1425  1.1  christos   sym_hashes = elf_sym_hashes (input_bfd);
   1426  1.1  christos 
   1427  1.1  christos   rel = relocs;
   1428  1.1  christos   relend = relocs + input_section->reloc_count;
   1429  1.1  christos   for (; rel < relend; rel++)
   1430  1.1  christos     {
   1431  1.1  christos       int r_type;
   1432  1.1  christos       reloc_howto_type *howto;
   1433  1.1  christos       unsigned long r_symndx;
   1434  1.1  christos       Elf_Internal_Sym *sym;
   1435  1.1  christos       asection *sec;
   1436  1.1  christos       struct elf32_mn10300_link_hash_entry *h;
   1437  1.1  christos       bfd_vma relocation;
   1438  1.1  christos       bfd_reloc_status_type r;
   1439  1.1  christos 
   1440  1.1  christos       r_symndx = ELF32_R_SYM (rel->r_info);
   1441  1.1  christos       r_type = ELF32_R_TYPE (rel->r_info);
   1442  1.1  christos       howto = elf_mn10300_howto_table + r_type;
   1443  1.1  christos 
   1444  1.1  christos       /* Just skip the vtable gc relocs.  */
   1445  1.1  christos       if (r_type == R_MN10300_GNU_VTINHERIT
   1446  1.1  christos 	  || r_type == R_MN10300_GNU_VTENTRY)
   1447  1.1  christos 	continue;
   1448  1.1  christos 
   1449  1.1  christos       h = NULL;
   1450  1.1  christos       sym = NULL;
   1451  1.1  christos       sec = NULL;
   1452  1.1  christos       if (r_symndx < symtab_hdr->sh_info)
   1453  1.1  christos 	{
   1454  1.1  christos 	  sym = local_syms + r_symndx;
   1455  1.1  christos 	  sec = local_sections[r_symndx];
   1456  1.1  christos 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   1457  1.1  christos 	}
   1458  1.1  christos       else
   1459  1.1  christos 	{
   1460  1.1  christos 	  bfd_boolean unresolved_reloc;
   1461  1.1  christos 	  bfd_boolean warned;
   1462  1.1  christos 	  struct elf_link_hash_entry *hh;
   1463  1.1  christos 
   1464  1.1  christos 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   1465  1.1  christos 				   r_symndx, symtab_hdr, sym_hashes,
   1466  1.1  christos 				   hh, sec, relocation,
   1467  1.1  christos 				   unresolved_reloc, warned);
   1468  1.1  christos 
   1469  1.1  christos 	  h = (struct elf32_mn10300_link_hash_entry *) hh;
   1470  1.1  christos 
   1471  1.1  christos 	  if ((h->root.root.type == bfd_link_hash_defined
   1472  1.1  christos 	      || h->root.root.type == bfd_link_hash_defweak)
   1473  1.1  christos 	      && (   r_type == R_MN10300_GOTPC32
   1474  1.1  christos 		  || r_type == R_MN10300_GOTPC16
   1475  1.1  christos 		  || ((   r_type == R_MN10300_PLT32
   1476  1.1  christos 		       || r_type == R_MN10300_PLT16)
   1477  1.1  christos 		      && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
   1478  1.1  christos 		      && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
   1479  1.1  christos 		      && h->root.plt.offset != (bfd_vma) -1)
   1480  1.1  christos 		  || ((   r_type == R_MN10300_GOT32
   1481  1.1  christos 		       || r_type == R_MN10300_GOT24
   1482  1.1  christos 		       || r_type == R_MN10300_GOT16)
   1483  1.1  christos 		      && elf_hash_table (info)->dynamic_sections_created
   1484  1.1  christos 		      && !SYMBOL_REFERENCES_LOCAL (info, hh))
   1485  1.1  christos 		  || (r_type == R_MN10300_32
   1486  1.1  christos 		      /* _32 relocs in executables force _COPY relocs,
   1487  1.1  christos 			 such that the address of the symbol ends up
   1488  1.1  christos 			 being local.  */
   1489  1.1  christos 		      && !info->executable
   1490  1.1  christos 		      && !SYMBOL_REFERENCES_LOCAL (info, hh)
   1491  1.1  christos 		      && ((input_section->flags & SEC_ALLOC) != 0
   1492  1.1  christos 			  /* DWARF will emit R_MN10300_32 relocations
   1493  1.1  christos 			     in its sections against symbols defined
   1494  1.1  christos 			     externally in shared libraries.  We can't
   1495  1.1  christos 			     do anything with them here.  */
   1496  1.1  christos 			  || ((input_section->flags & SEC_DEBUGGING) != 0
   1497  1.1  christos 			      && h->root.def_dynamic)))))
   1498  1.1  christos 	    /* In these cases, we don't need the relocation
   1499  1.1  christos 	       value.  We check specially because in some
   1500  1.1  christos 	       obscure cases sec->output_section will be NULL.  */
   1501  1.1  christos 	    relocation = 0;
   1502  1.1  christos 
   1503  1.1  christos 	  else if (!info->relocatable && unresolved_reloc)
   1504  1.1  christos 	    (*_bfd_error_handler)
   1505  1.1  christos 	      (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
   1506  1.1  christos 	       input_bfd,
   1507  1.1  christos 	       input_section,
   1508  1.1  christos 	       (long) rel->r_offset,
   1509  1.1  christos 	       howto->name,
   1510  1.1  christos 	       h->root.root.root.string);
   1511  1.1  christos 	}
   1512  1.1  christos 
   1513  1.1  christos       if (sec != NULL && elf_discarded_section (sec))
   1514  1.1  christos 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   1515  1.1  christos 					 rel, relend, howto, contents);
   1516  1.1  christos 
   1517  1.1  christos       if (info->relocatable)
   1518  1.1  christos 	continue;
   1519  1.1  christos 
   1520  1.1  christos       r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
   1521  1.1  christos 					   input_section,
   1522  1.1  christos 					   contents, rel->r_offset,
   1523  1.1  christos 					   relocation, rel->r_addend,
   1524  1.1  christos 					   (struct elf_link_hash_entry *) h,
   1525  1.1  christos 					   r_symndx,
   1526  1.1  christos 					   info, sec, h == NULL);
   1527  1.1  christos 
   1528  1.1  christos       if (r != bfd_reloc_ok)
   1529  1.1  christos 	{
   1530  1.1  christos 	  const char *name;
   1531  1.1  christos 	  const char *msg = NULL;
   1532  1.1  christos 
   1533  1.1  christos 	  if (h != NULL)
   1534  1.1  christos 	    name = h->root.root.root.string;
   1535  1.1  christos 	  else
   1536  1.1  christos 	    {
   1537  1.1  christos 	      name = (bfd_elf_string_from_elf_section
   1538  1.1  christos 		      (input_bfd, symtab_hdr->sh_link, sym->st_name));
   1539  1.1  christos 	      if (name == NULL || *name == '\0')
   1540  1.1  christos 		name = bfd_section_name (input_bfd, sec);
   1541  1.1  christos 	    }
   1542  1.1  christos 
   1543  1.1  christos 	  switch (r)
   1544  1.1  christos 	    {
   1545  1.1  christos 	    case bfd_reloc_overflow:
   1546  1.1  christos 	      if (! ((*info->callbacks->reloc_overflow)
   1547  1.1  christos 		     (info, (h ? &h->root.root : NULL), name,
   1548  1.1  christos 		      howto->name, (bfd_vma) 0, input_bfd,
   1549  1.1  christos 		      input_section, rel->r_offset)))
   1550  1.1  christos 		return FALSE;
   1551  1.1  christos 	      break;
   1552  1.1  christos 
   1553  1.1  christos 	    case bfd_reloc_undefined:
   1554  1.1  christos 	      if (! ((*info->callbacks->undefined_symbol)
   1555  1.1  christos 		     (info, name, input_bfd, input_section,
   1556  1.1  christos 		      rel->r_offset, TRUE)))
   1557  1.1  christos 		return FALSE;
   1558  1.1  christos 	      break;
   1559  1.1  christos 
   1560  1.1  christos 	    case bfd_reloc_outofrange:
   1561  1.1  christos 	      msg = _("internal error: out of range error");
   1562  1.1  christos 	      goto common_error;
   1563  1.1  christos 
   1564  1.1  christos 	    case bfd_reloc_notsupported:
   1565  1.1  christos 	      msg = _("internal error: unsupported relocation error");
   1566  1.1  christos 	      goto common_error;
   1567  1.1  christos 
   1568  1.1  christos 	    case bfd_reloc_dangerous:
   1569  1.1  christos 	      if (r_type == R_MN10300_PCREL32)
   1570  1.1  christos 		msg = _("error: inappropriate relocation type for shared"
   1571  1.1  christos 			" library (did you forget -fpic?)");
   1572  1.1  christos 	      else
   1573  1.1  christos 		msg = _("internal error: suspicious relocation type used"
   1574  1.1  christos 			" in shared library");
   1575  1.1  christos 	      goto common_error;
   1576  1.1  christos 
   1577  1.1  christos 	    default:
   1578  1.1  christos 	      msg = _("internal error: unknown error");
   1579  1.1  christos 	      /* Fall through.  */
   1580  1.1  christos 
   1581  1.1  christos 	    common_error:
   1582  1.1  christos 	      if (!((*info->callbacks->warning)
   1583  1.1  christos 		    (info, msg, name, input_bfd, input_section,
   1584  1.1  christos 		     rel->r_offset)))
   1585  1.1  christos 		return FALSE;
   1586  1.1  christos 	      break;
   1587  1.1  christos 	    }
   1588  1.1  christos 	}
   1589  1.1  christos     }
   1590  1.1  christos 
   1591  1.1  christos   return TRUE;
   1592  1.1  christos }
   1593  1.1  christos 
   1594  1.1  christos /* Finish initializing one hash table entry.  */
   1595  1.1  christos 
   1596  1.1  christos static bfd_boolean
   1597  1.1  christos elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
   1598  1.1  christos 				       void * in_args)
   1599  1.1  christos {
   1600  1.1  christos   struct elf32_mn10300_link_hash_entry *entry;
   1601  1.1  christos   struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
   1602  1.1  christos   unsigned int byte_count = 0;
   1603  1.1  christos 
   1604  1.1  christos   entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
   1605  1.1  christos 
   1606  1.1  christos   if (entry->root.root.type == bfd_link_hash_warning)
   1607  1.1  christos     entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
   1608  1.1  christos 
   1609  1.1  christos   /* If we already know we want to convert "call" to "calls" for calls
   1610  1.1  christos      to this symbol, then return now.  */
   1611  1.1  christos   if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
   1612  1.1  christos     return TRUE;
   1613  1.1  christos 
   1614  1.1  christos   /* If there are no named calls to this symbol, or there's nothing we
   1615  1.1  christos      can move from the function itself into the "call" instruction,
   1616  1.1  christos      then note that all "call" instructions should be converted into
   1617  1.1  christos      "calls" instructions and return.  If a symbol is available for
   1618  1.1  christos      dynamic symbol resolution (overridable or overriding), avoid
   1619  1.1  christos      custom calling conventions.  */
   1620  1.1  christos   if (entry->direct_calls == 0
   1621  1.1  christos       || (entry->stack_size == 0 && entry->movm_args == 0)
   1622  1.1  christos       || (elf_hash_table (link_info)->dynamic_sections_created
   1623  1.1  christos 	  && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
   1624  1.1  christos 	  && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
   1625  1.1  christos     {
   1626  1.1  christos       /* Make a note that we should convert "call" instructions to "calls"
   1627  1.1  christos 	 instructions for calls to this symbol.  */
   1628  1.1  christos       entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
   1629  1.1  christos       return TRUE;
   1630  1.1  christos     }
   1631  1.1  christos 
   1632  1.1  christos   /* We may be able to move some instructions from the function itself into
   1633  1.1  christos      the "call" instruction.  Count how many bytes we might be able to
   1634  1.1  christos      eliminate in the function itself.  */
   1635  1.1  christos 
   1636  1.1  christos   /* A movm instruction is two bytes.  */
   1637  1.1  christos   if (entry->movm_args)
   1638  1.1  christos     byte_count += 2;
   1639  1.1  christos 
   1640  1.1  christos   /* Count the insn to allocate stack space too.  */
   1641  1.1  christos   if (entry->stack_size > 0)
   1642  1.1  christos     {
   1643  1.1  christos       if (entry->stack_size <= 128)
   1644  1.1  christos 	byte_count += 3;
   1645  1.1  christos       else
   1646  1.1  christos 	byte_count += 4;
   1647  1.1  christos     }
   1648  1.1  christos 
   1649  1.1  christos   /* If using "call" will result in larger code, then turn all
   1650  1.1  christos      the associated "call" instructions into "calls" instructions.  */
   1651  1.1  christos   if (byte_count < entry->direct_calls)
   1652  1.1  christos     entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
   1653  1.1  christos 
   1654  1.1  christos   /* This routine never fails.  */
   1655  1.1  christos   return TRUE;
   1656  1.1  christos }
   1657  1.1  christos 
   1658  1.1  christos /* Used to count hash table entries.  */
   1659  1.1  christos 
   1660  1.1  christos static bfd_boolean
   1661  1.1  christos elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
   1662  1.1  christos 					void * in_args)
   1663  1.1  christos {
   1664  1.1  christos   int *count = (int *) in_args;
   1665  1.1  christos 
   1666  1.1  christos   (*count) ++;
   1667  1.1  christos   return TRUE;
   1668  1.1  christos }
   1669  1.1  christos 
   1670  1.1  christos /* Used to enumerate hash table entries into a linear array.  */
   1671  1.1  christos 
   1672  1.1  christos static bfd_boolean
   1673  1.1  christos elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
   1674  1.1  christos 				       void * in_args)
   1675  1.1  christos {
   1676  1.1  christos   struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
   1677  1.1  christos 
   1678  1.1  christos   **ptr = gen_entry;
   1679  1.1  christos   (*ptr) ++;
   1680  1.1  christos   return TRUE;
   1681  1.1  christos }
   1682  1.1  christos 
   1683  1.1  christos /* Used to sort the array created by the above.  */
   1684  1.1  christos 
   1685  1.1  christos static int
   1686  1.1  christos sort_by_value (const void *va, const void *vb)
   1687  1.1  christos {
   1688  1.1  christos   struct elf32_mn10300_link_hash_entry *a
   1689  1.1  christos     = *(struct elf32_mn10300_link_hash_entry **) va;
   1690  1.1  christos   struct elf32_mn10300_link_hash_entry *b
   1691  1.1  christos     = *(struct elf32_mn10300_link_hash_entry **) vb;
   1692  1.1  christos 
   1693  1.1  christos   return a->value - b->value;
   1694  1.1  christos }
   1695  1.1  christos 
   1696  1.1  christos /* Compute the stack size and movm arguments for the function
   1697  1.1  christos    referred to by HASH at address ADDR in section with
   1698  1.1  christos    contents CONTENTS, store the information in the hash table.  */
   1699  1.1  christos 
   1700  1.1  christos static void
   1701  1.1  christos compute_function_info (bfd *abfd,
   1702  1.1  christos 		       struct elf32_mn10300_link_hash_entry *hash,
   1703  1.1  christos 		       bfd_vma addr,
   1704  1.1  christos 		       unsigned char *contents)
   1705  1.1  christos {
   1706  1.1  christos   unsigned char byte1, byte2;
   1707  1.1  christos   /* We only care about a very small subset of the possible prologue
   1708  1.1  christos      sequences here.  Basically we look for:
   1709  1.1  christos 
   1710  1.1  christos      movm [d2,d3,a2,a3],sp (optional)
   1711  1.1  christos      add <size>,sp (optional, and only for sizes which fit in an unsigned
   1712  1.1  christos 		    8 bit number)
   1713  1.1  christos 
   1714  1.1  christos      If we find anything else, we quit.  */
   1715  1.1  christos 
   1716  1.1  christos   /* Look for movm [regs],sp.  */
   1717  1.1  christos   byte1 = bfd_get_8 (abfd, contents + addr);
   1718  1.1  christos   byte2 = bfd_get_8 (abfd, contents + addr + 1);
   1719  1.1  christos 
   1720  1.1  christos   if (byte1 == 0xcf)
   1721  1.1  christos     {
   1722  1.1  christos       hash->movm_args = byte2;
   1723  1.1  christos       addr += 2;
   1724  1.1  christos       byte1 = bfd_get_8 (abfd, contents + addr);
   1725  1.1  christos       byte2 = bfd_get_8 (abfd, contents + addr + 1);
   1726  1.1  christos     }
   1727  1.1  christos 
   1728  1.1  christos   /* Now figure out how much stack space will be allocated by the movm
   1729  1.1  christos      instruction.  We need this kept separate from the function's normal
   1730  1.1  christos      stack space.  */
   1731  1.1  christos   if (hash->movm_args)
   1732  1.1  christos     {
   1733  1.1  christos       /* Space for d2.  */
   1734  1.1  christos       if (hash->movm_args & 0x80)
   1735  1.1  christos 	hash->movm_stack_size += 4;
   1736  1.1  christos 
   1737  1.1  christos       /* Space for d3.  */
   1738  1.1  christos       if (hash->movm_args & 0x40)
   1739  1.1  christos 	hash->movm_stack_size += 4;
   1740  1.1  christos 
   1741  1.1  christos       /* Space for a2.  */
   1742  1.1  christos       if (hash->movm_args & 0x20)
   1743  1.1  christos 	hash->movm_stack_size += 4;
   1744  1.1  christos 
   1745  1.1  christos       /* Space for a3.  */
   1746  1.1  christos       if (hash->movm_args & 0x10)
   1747  1.1  christos 	hash->movm_stack_size += 4;
   1748  1.1  christos 
   1749  1.1  christos       /* "other" space.  d0, d1, a0, a1, mdr, lir, lar, 4 byte pad.  */
   1750  1.1  christos       if (hash->movm_args & 0x08)
   1751  1.1  christos 	hash->movm_stack_size += 8 * 4;
   1752  1.1  christos 
   1753  1.1  christos       if (bfd_get_mach (abfd) == bfd_mach_am33
   1754  1.1  christos 	  || bfd_get_mach (abfd) == bfd_mach_am33_2)
   1755  1.1  christos 	{
   1756  1.1  christos 	  /* "exother" space.  e0, e1, mdrq, mcrh, mcrl, mcvf */
   1757  1.1  christos 	  if (hash->movm_args & 0x1)
   1758  1.1  christos 	    hash->movm_stack_size += 6 * 4;
   1759  1.1  christos 
   1760  1.1  christos 	  /* exreg1 space.  e4, e5, e6, e7 */
   1761  1.1  christos 	  if (hash->movm_args & 0x2)
   1762  1.1  christos 	    hash->movm_stack_size += 4 * 4;
   1763  1.1  christos 
   1764  1.1  christos 	  /* exreg0 space.  e2, e3  */
   1765  1.1  christos 	  if (hash->movm_args & 0x4)
   1766  1.1  christos 	    hash->movm_stack_size += 2 * 4;
   1767  1.1  christos 	}
   1768  1.1  christos     }
   1769  1.1  christos 
   1770  1.1  christos   /* Now look for the two stack adjustment variants.  */
   1771  1.1  christos   if (byte1 == 0xf8 && byte2 == 0xfe)
   1772  1.1  christos     {
   1773  1.1  christos       int temp = bfd_get_8 (abfd, contents + addr + 2);
   1774  1.1  christos       temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
   1775  1.1  christos 
   1776  1.1  christos       hash->stack_size = -temp;
   1777  1.1  christos     }
   1778  1.1  christos   else if (byte1 == 0xfa && byte2 == 0xfe)
   1779  1.1  christos     {
   1780  1.1  christos       int temp = bfd_get_16 (abfd, contents + addr + 2);
   1781  1.1  christos       temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
   1782  1.1  christos       temp = -temp;
   1783  1.1  christos 
   1784  1.1  christos       if (temp < 255)
   1785  1.1  christos 	hash->stack_size = temp;
   1786  1.1  christos     }
   1787  1.1  christos 
   1788  1.1  christos   /* If the total stack to be allocated by the call instruction is more
   1789  1.1  christos      than 255 bytes, then we can't remove the stack adjustment by using
   1790  1.1  christos      "call" (we might still be able to remove the "movm" instruction.  */
   1791  1.1  christos   if (hash->stack_size + hash->movm_stack_size > 255)
   1792  1.1  christos     hash->stack_size = 0;
   1793  1.1  christos }
   1794  1.1  christos 
   1795  1.1  christos /* Delete some bytes from a section while relaxing.  */
   1796  1.1  christos 
   1797  1.1  christos static bfd_boolean
   1798  1.1  christos mn10300_elf_relax_delete_bytes (bfd *abfd,
   1799  1.1  christos 				asection *sec,
   1800  1.1  christos 				bfd_vma addr,
   1801  1.1  christos 				int count)
   1802  1.1  christos {
   1803  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1804  1.1  christos   unsigned int sec_shndx;
   1805  1.1  christos   bfd_byte *contents;
   1806  1.1  christos   Elf_Internal_Rela *irel, *irelend;
   1807  1.1  christos   Elf_Internal_Rela *irelalign;
   1808  1.1  christos   bfd_vma toaddr;
   1809  1.1  christos   Elf_Internal_Sym *isym, *isymend;
   1810  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   1811  1.1  christos   struct elf_link_hash_entry **end_hashes;
   1812  1.1  christos   unsigned int symcount;
   1813  1.1  christos 
   1814  1.1  christos   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   1815  1.1  christos 
   1816  1.1  christos   contents = elf_section_data (sec)->this_hdr.contents;
   1817  1.1  christos 
   1818  1.1  christos   irelalign = NULL;
   1819  1.1  christos   toaddr = sec->size;
   1820  1.1  christos 
   1821  1.1  christos   irel = elf_section_data (sec)->relocs;
   1822  1.1  christos   irelend = irel + sec->reloc_count;
   1823  1.1  christos 
   1824  1.1  christos   if (sec->reloc_count > 0)
   1825  1.1  christos     {
   1826  1.1  christos       /* If there is an align reloc at the end of the section ignore it.
   1827  1.1  christos 	 GAS creates these relocs for reasons of its own, and they just
   1828  1.1  christos 	 serve to keep the section artifically inflated.  */
   1829  1.1  christos       if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
   1830  1.1  christos 	--irelend;
   1831  1.1  christos 
   1832  1.1  christos       /* The deletion must stop at the next ALIGN reloc for an aligment
   1833  1.1  christos 	 power larger than, or not a multiple of, the number of bytes we
   1834  1.1  christos 	 are deleting.  */
   1835  1.1  christos       for (; irel < irelend; irel++)
   1836  1.1  christos 	{
   1837  1.1  christos 	  int alignment = 1 << irel->r_addend;
   1838  1.1  christos 
   1839  1.1  christos 	  if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
   1840  1.1  christos 	      && irel->r_offset > addr
   1841  1.1  christos 	      && irel->r_offset < toaddr
   1842  1.1  christos 	      && (count < alignment
   1843  1.1  christos 		  || alignment % count != 0))
   1844  1.1  christos 	    {
   1845  1.1  christos 	      irelalign = irel;
   1846  1.1  christos 	      toaddr = irel->r_offset;
   1847  1.1  christos 	      break;
   1848  1.1  christos 	    }
   1849  1.1  christos 	}
   1850  1.1  christos     }
   1851  1.1  christos 
   1852  1.1  christos   /* Actually delete the bytes.  */
   1853  1.1  christos   memmove (contents + addr, contents + addr + count,
   1854  1.1  christos 	   (size_t) (toaddr - addr - count));
   1855  1.1  christos 
   1856  1.1  christos   /* Adjust the section's size if we are shrinking it, or else
   1857  1.1  christos      pad the bytes between the end of the shrunken region and
   1858  1.1  christos      the start of the next region with NOP codes.  */
   1859  1.1  christos   if (irelalign == NULL)
   1860  1.1  christos     {
   1861  1.1  christos       sec->size -= count;
   1862  1.1  christos       /* Include symbols at the end of the section, but
   1863  1.1  christos 	 not at the end of a sub-region of the section.  */
   1864  1.1  christos       toaddr ++;
   1865  1.1  christos     }
   1866  1.1  christos   else
   1867  1.1  christos     {
   1868  1.1  christos       int i;
   1869  1.1  christos 
   1870  1.1  christos #define NOP_OPCODE 0xcb
   1871  1.1  christos 
   1872  1.1  christos       for (i = 0; i < count; i ++)
   1873  1.1  christos 	bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
   1874  1.1  christos     }
   1875  1.1  christos 
   1876  1.1  christos   /* Adjust all the relocs.  */
   1877  1.1  christos   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
   1878  1.1  christos     {
   1879  1.1  christos       /* Get the new reloc address.  */
   1880  1.1  christos       if ((irel->r_offset > addr
   1881  1.1  christos 	   && irel->r_offset < toaddr)
   1882  1.1  christos 	  || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
   1883  1.1  christos 	      && irel->r_offset == toaddr))
   1884  1.1  christos 	irel->r_offset -= count;
   1885  1.1  christos     }
   1886  1.1  christos 
   1887  1.1  christos   /* Adjust the local symbols in the section, reducing their value
   1888  1.1  christos      by the number of bytes deleted.  Note - symbols within the deleted
   1889  1.1  christos      region are moved to the address of the start of the region, which
   1890  1.1  christos      actually means that they will address the byte beyond the end of
   1891  1.1  christos      the region once the deletion has been completed.  */
   1892  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1893  1.1  christos   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   1894  1.1  christos   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
   1895  1.1  christos     {
   1896  1.1  christos       if (isym->st_shndx == sec_shndx
   1897  1.1  christos 	  && isym->st_value > addr
   1898  1.1  christos 	  && isym->st_value < toaddr)
   1899  1.1  christos 	{
   1900  1.1  christos 	  if (isym->st_value < addr + count)
   1901  1.1  christos 	    isym->st_value = addr;
   1902  1.1  christos 	  else
   1903  1.1  christos 	    isym->st_value -= count;
   1904  1.1  christos 	}
   1905  1.1  christos       /* Adjust the function symbol's size as well.  */
   1906  1.1  christos       else if (isym->st_shndx == sec_shndx
   1907  1.1  christos 	       && ELF_ST_TYPE (isym->st_info) == STT_FUNC
   1908  1.1  christos 	       && isym->st_value + isym->st_size > addr
   1909  1.1  christos 	       && isym->st_value + isym->st_size < toaddr)
   1910  1.1  christos 	isym->st_size -= count;
   1911  1.1  christos     }
   1912  1.1  christos 
   1913  1.1  christos   /* Now adjust the global symbols defined in this section.  */
   1914  1.1  christos   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   1915  1.1  christos 	      - symtab_hdr->sh_info);
   1916  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   1917  1.1  christos   end_hashes = sym_hashes + symcount;
   1918  1.1  christos   for (; sym_hashes < end_hashes; sym_hashes++)
   1919  1.1  christos     {
   1920  1.1  christos       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   1921  1.1  christos 
   1922  1.1  christos       if ((sym_hash->root.type == bfd_link_hash_defined
   1923  1.1  christos 	   || sym_hash->root.type == bfd_link_hash_defweak)
   1924  1.1  christos 	  && sym_hash->root.u.def.section == sec
   1925  1.1  christos 	  && sym_hash->root.u.def.value > addr
   1926  1.1  christos 	  && sym_hash->root.u.def.value < toaddr)
   1927  1.1  christos 	{
   1928  1.1  christos 	  if (sym_hash->root.u.def.value < addr + count)
   1929  1.1  christos 	    sym_hash->root.u.def.value = addr;
   1930  1.1  christos 	  else
   1931  1.1  christos 	    sym_hash->root.u.def.value -= count;
   1932  1.1  christos 	}
   1933  1.1  christos       /* Adjust the function symbol's size as well.  */
   1934  1.1  christos       else if (sym_hash->root.type == bfd_link_hash_defined
   1935  1.1  christos 	       && sym_hash->root.u.def.section == sec
   1936  1.1  christos 	       && sym_hash->type == STT_FUNC
   1937  1.1  christos 	       && sym_hash->root.u.def.value + sym_hash->size > addr
   1938  1.1  christos 	       && sym_hash->root.u.def.value + sym_hash->size < toaddr)
   1939  1.1  christos 	sym_hash->size -= count;
   1940  1.1  christos     }
   1941  1.1  christos 
   1942  1.1  christos   /* See if we can move the ALIGN reloc forward.
   1943  1.1  christos      We have adjusted r_offset for it already.  */
   1944  1.1  christos   if (irelalign != NULL)
   1945  1.1  christos     {
   1946  1.1  christos       bfd_vma alignto, alignaddr;
   1947  1.1  christos 
   1948  1.1  christos       if ((int) irelalign->r_addend > 0)
   1949  1.1  christos 	{
   1950  1.1  christos 	  /* This is the old address.  */
   1951  1.1  christos 	  alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
   1952  1.1  christos 	  /* This is where the align points to now.  */
   1953  1.1  christos 	  alignaddr = BFD_ALIGN (irelalign->r_offset,
   1954  1.1  christos 				 1 << irelalign->r_addend);
   1955  1.1  christos 	  if (alignaddr < alignto)
   1956  1.1  christos 	    /* Tail recursion.  */
   1957  1.1  christos 	    return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
   1958  1.1  christos 						   (int) (alignto - alignaddr));
   1959  1.1  christos 	}
   1960  1.1  christos     }
   1961  1.1  christos 
   1962  1.1  christos   return TRUE;
   1963  1.1  christos }
   1964  1.1  christos 
   1965  1.1  christos /* Return TRUE if a symbol exists at the given address, else return
   1966  1.1  christos    FALSE.  */
   1967  1.1  christos 
   1968  1.1  christos static bfd_boolean
   1969  1.1  christos mn10300_elf_symbol_address_p (bfd *abfd,
   1970  1.1  christos 			      asection *sec,
   1971  1.1  christos 			      Elf_Internal_Sym *isym,
   1972  1.1  christos 			      bfd_vma addr)
   1973  1.1  christos {
   1974  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1975  1.1  christos   unsigned int sec_shndx;
   1976  1.1  christos   Elf_Internal_Sym *isymend;
   1977  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   1978  1.1  christos   struct elf_link_hash_entry **end_hashes;
   1979  1.1  christos   unsigned int symcount;
   1980  1.1  christos 
   1981  1.1  christos   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   1982  1.1  christos 
   1983  1.1  christos   /* Examine all the symbols.  */
   1984  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1985  1.1  christos   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
   1986  1.1  christos     if (isym->st_shndx == sec_shndx
   1987  1.1  christos 	&& isym->st_value == addr)
   1988  1.1  christos       return TRUE;
   1989  1.1  christos 
   1990  1.1  christos   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   1991  1.1  christos 	      - symtab_hdr->sh_info);
   1992  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   1993  1.1  christos   end_hashes = sym_hashes + symcount;
   1994  1.1  christos   for (; sym_hashes < end_hashes; sym_hashes++)
   1995  1.1  christos     {
   1996  1.1  christos       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   1997  1.1  christos 
   1998  1.1  christos       if ((sym_hash->root.type == bfd_link_hash_defined
   1999  1.1  christos 	   || sym_hash->root.type == bfd_link_hash_defweak)
   2000  1.1  christos 	  && sym_hash->root.u.def.section == sec
   2001  1.1  christos 	  && sym_hash->root.u.def.value == addr)
   2002  1.1  christos 	return TRUE;
   2003  1.1  christos     }
   2004  1.1  christos 
   2005  1.1  christos   return FALSE;
   2006  1.1  christos }
   2007  1.1  christos 
   2008  1.1  christos /* This function handles relaxing for the mn10300.
   2009  1.1  christos 
   2010  1.1  christos    There are quite a few relaxing opportunities available on the mn10300:
   2011  1.1  christos 
   2012  1.1  christos 	* calls:32 -> calls:16 					   2 bytes
   2013  1.1  christos 	* call:32  -> call:16					   2 bytes
   2014  1.1  christos 
   2015  1.1  christos 	* call:32 -> calls:32					   1 byte
   2016  1.1  christos 	* call:16 -> calls:16					   1 byte
   2017  1.1  christos 		* These are done anytime using "calls" would result
   2018  1.1  christos 		in smaller code, or when necessary to preserve the
   2019  1.1  christos 		meaning of the program.
   2020  1.1  christos 
   2021  1.1  christos 	* call:32						   varies
   2022  1.1  christos 	* call:16
   2023  1.1  christos 		* In some circumstances we can move instructions
   2024  1.1  christos 		from a function prologue into a "call" instruction.
   2025  1.1  christos 		This is only done if the resulting code is no larger
   2026  1.1  christos 		than the original code.
   2027  1.1  christos 
   2028  1.1  christos 	* jmp:32 -> jmp:16					   2 bytes
   2029  1.1  christos 	* jmp:16 -> bra:8					   1 byte
   2030  1.1  christos 
   2031  1.1  christos 		* If the previous instruction is a conditional branch
   2032  1.1  christos 		around the jump/bra, we may be able to reverse its condition
   2033  1.1  christos 		and change its target to the jump's target.  The jump/bra
   2034  1.1  christos 		can then be deleted.				   2 bytes
   2035  1.1  christos 
   2036  1.1  christos 	* mov abs32 -> mov abs16				   1 or 2 bytes
   2037  1.1  christos 
   2038  1.1  christos 	* Most instructions which accept imm32 can relax to imm16  1 or 2 bytes
   2039  1.1  christos 	- Most instructions which accept imm16 can relax to imm8   1 or 2 bytes
   2040  1.1  christos 
   2041  1.1  christos 	* Most instructions which accept d32 can relax to d16	   1 or 2 bytes
   2042  1.1  christos 	- Most instructions which accept d16 can relax to d8	   1 or 2 bytes
   2043  1.1  christos 
   2044  1.1  christos 	We don't handle imm16->imm8 or d16->d8 as they're very rare
   2045  1.1  christos 	and somewhat more difficult to support.  */
   2046  1.1  christos 
   2047  1.1  christos static bfd_boolean
   2048  1.1  christos mn10300_elf_relax_section (bfd *abfd,
   2049  1.1  christos 			   asection *sec,
   2050  1.1  christos 			   struct bfd_link_info *link_info,
   2051  1.1  christos 			   bfd_boolean *again)
   2052  1.1  christos {
   2053  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   2054  1.1  christos   Elf_Internal_Rela *internal_relocs = NULL;
   2055  1.1  christos   Elf_Internal_Rela *irel, *irelend;
   2056  1.1  christos   bfd_byte *contents = NULL;
   2057  1.1  christos   Elf_Internal_Sym *isymbuf = NULL;
   2058  1.1  christos   struct elf32_mn10300_link_hash_table *hash_table;
   2059  1.1  christos   asection *section = sec;
   2060  1.1  christos   bfd_vma align_gap_adjustment;
   2061  1.1  christos 
   2062  1.1  christos   if (link_info->relocatable)
   2063  1.1  christos     (*link_info->callbacks->einfo)
   2064  1.1  christos       (_("%P%F: --relax and -r may not be used together\n"));
   2065  1.1  christos 
   2066  1.1  christos   /* Assume nothing changes.  */
   2067  1.1  christos   *again = FALSE;
   2068  1.1  christos 
   2069  1.1  christos   /* We need a pointer to the mn10300 specific hash table.  */
   2070  1.1  christos   hash_table = elf32_mn10300_hash_table (link_info);
   2071  1.1  christos   if (hash_table == NULL)
   2072  1.1  christos     return FALSE;
   2073  1.1  christos 
   2074  1.1  christos   /* Initialize fields in each hash table entry the first time through.  */
   2075  1.1  christos   if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
   2076  1.1  christos     {
   2077  1.1  christos       bfd *input_bfd;
   2078  1.1  christos 
   2079  1.1  christos       /* Iterate over all the input bfds.  */
   2080  1.1  christos       for (input_bfd = link_info->input_bfds;
   2081  1.1  christos 	   input_bfd != NULL;
   2082  1.1  christos 	   input_bfd = input_bfd->link_next)
   2083  1.1  christos 	{
   2084  1.1  christos 	  /* We're going to need all the symbols for each bfd.  */
   2085  1.1  christos 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   2086  1.1  christos 	  if (symtab_hdr->sh_info != 0)
   2087  1.1  christos 	    {
   2088  1.1  christos 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   2089  1.1  christos 	      if (isymbuf == NULL)
   2090  1.1  christos 		isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   2091  1.1  christos 						symtab_hdr->sh_info, 0,
   2092  1.1  christos 						NULL, NULL, NULL);
   2093  1.1  christos 	      if (isymbuf == NULL)
   2094  1.1  christos 		goto error_return;
   2095  1.1  christos 	    }
   2096  1.1  christos 
   2097  1.1  christos 	  /* Iterate over each section in this bfd.  */
   2098  1.1  christos 	  for (section = input_bfd->sections;
   2099  1.1  christos 	       section != NULL;
   2100  1.1  christos 	       section = section->next)
   2101  1.1  christos 	    {
   2102  1.1  christos 	      struct elf32_mn10300_link_hash_entry *hash;
   2103  1.1  christos 	      asection *sym_sec = NULL;
   2104  1.1  christos 	      const char *sym_name;
   2105  1.1  christos 	      char *new_name;
   2106  1.1  christos 
   2107  1.1  christos 	      /* If there's nothing to do in this section, skip it.  */
   2108  1.1  christos 	      if (! ((section->flags & SEC_RELOC) != 0
   2109  1.1  christos 		     && section->reloc_count != 0))
   2110  1.1  christos 		continue;
   2111  1.1  christos 	      if ((section->flags & SEC_ALLOC) == 0)
   2112  1.1  christos 		continue;
   2113  1.1  christos 
   2114  1.1  christos 	      /* Get cached copy of section contents if it exists.  */
   2115  1.1  christos 	      if (elf_section_data (section)->this_hdr.contents != NULL)
   2116  1.1  christos 		contents = elf_section_data (section)->this_hdr.contents;
   2117  1.1  christos 	      else if (section->size != 0)
   2118  1.1  christos 		{
   2119  1.1  christos 		  /* Go get them off disk.  */
   2120  1.1  christos 		  if (!bfd_malloc_and_get_section (input_bfd, section,
   2121  1.1  christos 						   &contents))
   2122  1.1  christos 		    goto error_return;
   2123  1.1  christos 		}
   2124  1.1  christos 	      else
   2125  1.1  christos 		contents = NULL;
   2126  1.1  christos 
   2127  1.1  christos 	      /* If there aren't any relocs, then there's nothing to do.  */
   2128  1.1  christos 	      if ((section->flags & SEC_RELOC) != 0
   2129  1.1  christos 		  && section->reloc_count != 0)
   2130  1.1  christos 		{
   2131  1.1  christos 		  /* Get a copy of the native relocations.  */
   2132  1.1  christos 		  internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
   2133  1.1  christos 							       NULL, NULL,
   2134  1.1  christos 							       link_info->keep_memory);
   2135  1.1  christos 		  if (internal_relocs == NULL)
   2136  1.1  christos 		    goto error_return;
   2137  1.1  christos 
   2138  1.1  christos 		  /* Now examine each relocation.  */
   2139  1.1  christos 		  irel = internal_relocs;
   2140  1.1  christos 		  irelend = irel + section->reloc_count;
   2141  1.1  christos 		  for (; irel < irelend; irel++)
   2142  1.1  christos 		    {
   2143  1.1  christos 		      long r_type;
   2144  1.1  christos 		      unsigned long r_index;
   2145  1.1  christos 		      unsigned char code;
   2146  1.1  christos 
   2147  1.1  christos 		      r_type = ELF32_R_TYPE (irel->r_info);
   2148  1.1  christos 		      r_index = ELF32_R_SYM (irel->r_info);
   2149  1.1  christos 
   2150  1.1  christos 		      if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
   2151  1.1  christos 			goto error_return;
   2152  1.1  christos 
   2153  1.1  christos 		      /* We need the name and hash table entry of the target
   2154  1.1  christos 			 symbol!  */
   2155  1.1  christos 		      hash = NULL;
   2156  1.1  christos 		      sym_sec = NULL;
   2157  1.1  christos 
   2158  1.1  christos 		      if (r_index < symtab_hdr->sh_info)
   2159  1.1  christos 			{
   2160  1.1  christos 			  /* A local symbol.  */
   2161  1.1  christos 			  Elf_Internal_Sym *isym;
   2162  1.1  christos 			  struct elf_link_hash_table *elftab;
   2163  1.1  christos 			  bfd_size_type amt;
   2164  1.1  christos 
   2165  1.1  christos 			  isym = isymbuf + r_index;
   2166  1.1  christos 			  if (isym->st_shndx == SHN_UNDEF)
   2167  1.1  christos 			    sym_sec = bfd_und_section_ptr;
   2168  1.1  christos 			  else if (isym->st_shndx == SHN_ABS)
   2169  1.1  christos 			    sym_sec = bfd_abs_section_ptr;
   2170  1.1  christos 			  else if (isym->st_shndx == SHN_COMMON)
   2171  1.1  christos 			    sym_sec = bfd_com_section_ptr;
   2172  1.1  christos 			  else
   2173  1.1  christos 			    sym_sec
   2174  1.1  christos 			      = bfd_section_from_elf_index (input_bfd,
   2175  1.1  christos 							    isym->st_shndx);
   2176  1.1  christos 
   2177  1.1  christos 			  sym_name
   2178  1.1  christos 			    = bfd_elf_string_from_elf_section (input_bfd,
   2179  1.1  christos 							       (symtab_hdr
   2180  1.1  christos 								->sh_link),
   2181  1.1  christos 							       isym->st_name);
   2182  1.1  christos 
   2183  1.1  christos 			  /* If it isn't a function, then we don't care
   2184  1.1  christos 			     about it.  */
   2185  1.1  christos 			  if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
   2186  1.1  christos 			    continue;
   2187  1.1  christos 
   2188  1.1  christos 			  /* Tack on an ID so we can uniquely identify this
   2189  1.1  christos 			     local symbol in the global hash table.  */
   2190  1.1  christos 			  amt = strlen (sym_name) + 10;
   2191  1.1  christos 			  new_name = bfd_malloc (amt);
   2192  1.1  christos 			  if (new_name == NULL)
   2193  1.1  christos 			    goto error_return;
   2194  1.1  christos 
   2195  1.1  christos 			  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
   2196  1.1  christos 			  sym_name = new_name;
   2197  1.1  christos 
   2198  1.1  christos 			  elftab = &hash_table->static_hash_table->root;
   2199  1.1  christos 			  hash = ((struct elf32_mn10300_link_hash_entry *)
   2200  1.1  christos 				  elf_link_hash_lookup (elftab, sym_name,
   2201  1.1  christos 							TRUE, TRUE, FALSE));
   2202  1.1  christos 			  free (new_name);
   2203  1.1  christos 			}
   2204  1.1  christos 		      else
   2205  1.1  christos 			{
   2206  1.1  christos 			  r_index -= symtab_hdr->sh_info;
   2207  1.1  christos 			  hash = (struct elf32_mn10300_link_hash_entry *)
   2208  1.1  christos 				   elf_sym_hashes (input_bfd)[r_index];
   2209  1.1  christos 			}
   2210  1.1  christos 
   2211  1.1  christos 		      sym_name = hash->root.root.root.string;
   2212  1.1  christos 		      if ((section->flags & SEC_CODE) != 0)
   2213  1.1  christos 			{
   2214  1.1  christos 			  /* If this is not a "call" instruction, then we
   2215  1.1  christos 			     should convert "call" instructions to "calls"
   2216  1.1  christos 			     instructions.  */
   2217  1.1  christos 			  code = bfd_get_8 (input_bfd,
   2218  1.1  christos 					    contents + irel->r_offset - 1);
   2219  1.1  christos 			  if (code != 0xdd && code != 0xcd)
   2220  1.1  christos 			    hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
   2221  1.1  christos 			}
   2222  1.1  christos 
   2223  1.1  christos 		      /* If this is a jump/call, then bump the
   2224  1.1  christos 			 direct_calls counter.  Else force "call" to
   2225  1.1  christos 			 "calls" conversions.  */
   2226  1.1  christos 		      if (r_type == R_MN10300_PCREL32
   2227  1.1  christos 			  || r_type == R_MN10300_PLT32
   2228  1.1  christos 			  || r_type == R_MN10300_PLT16
   2229  1.1  christos 			  || r_type == R_MN10300_PCREL16)
   2230  1.1  christos 			hash->direct_calls++;
   2231  1.1  christos 		      else
   2232  1.1  christos 			hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
   2233  1.1  christos 		    }
   2234  1.1  christos 		}
   2235  1.1  christos 
   2236  1.1  christos 	      /* Now look at the actual contents to get the stack size,
   2237  1.1  christos 		 and a list of what registers were saved in the prologue
   2238  1.1  christos 		 (ie movm_args).  */
   2239  1.1  christos 	      if ((section->flags & SEC_CODE) != 0)
   2240  1.1  christos 		{
   2241  1.1  christos 		  Elf_Internal_Sym *isym, *isymend;
   2242  1.1  christos 		  unsigned int sec_shndx;
   2243  1.1  christos 		  struct elf_link_hash_entry **hashes;
   2244  1.1  christos 		  struct elf_link_hash_entry **end_hashes;
   2245  1.1  christos 		  unsigned int symcount;
   2246  1.1  christos 
   2247  1.1  christos 		  sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
   2248  1.1  christos 								 section);
   2249  1.1  christos 
   2250  1.1  christos 		  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   2251  1.1  christos 			      - symtab_hdr->sh_info);
   2252  1.1  christos 		  hashes = elf_sym_hashes (input_bfd);
   2253  1.1  christos 		  end_hashes = hashes + symcount;
   2254  1.1  christos 
   2255  1.1  christos 		  /* Look at each function defined in this section and
   2256  1.1  christos 		     update info for that function.  */
   2257  1.1  christos 		  isymend = isymbuf + symtab_hdr->sh_info;
   2258  1.1  christos 		  for (isym = isymbuf; isym < isymend; isym++)
   2259  1.1  christos 		    {
   2260  1.1  christos 		      if (isym->st_shndx == sec_shndx
   2261  1.1  christos 			  && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
   2262  1.1  christos 			{
   2263  1.1  christos 			  struct elf_link_hash_table *elftab;
   2264  1.1  christos 			  bfd_size_type amt;
   2265  1.1  christos 			  struct elf_link_hash_entry **lhashes = hashes;
   2266  1.1  christos 
   2267  1.1  christos 			  /* Skip a local symbol if it aliases a
   2268  1.1  christos 			     global one.  */
   2269  1.1  christos 			  for (; lhashes < end_hashes; lhashes++)
   2270  1.1  christos 			    {
   2271  1.1  christos 			      hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
   2272  1.1  christos 			      if ((hash->root.root.type == bfd_link_hash_defined
   2273  1.1  christos 				   || hash->root.root.type == bfd_link_hash_defweak)
   2274  1.1  christos 				  && hash->root.root.u.def.section == section
   2275  1.1  christos 				  && hash->root.type == STT_FUNC
   2276  1.1  christos 				  && hash->root.root.u.def.value == isym->st_value)
   2277  1.1  christos 				break;
   2278  1.1  christos 			    }
   2279  1.1  christos 			  if (lhashes != end_hashes)
   2280  1.1  christos 			    continue;
   2281  1.1  christos 
   2282  1.1  christos 			  if (isym->st_shndx == SHN_UNDEF)
   2283  1.1  christos 			    sym_sec = bfd_und_section_ptr;
   2284  1.1  christos 			  else if (isym->st_shndx == SHN_ABS)
   2285  1.1  christos 			    sym_sec = bfd_abs_section_ptr;
   2286  1.1  christos 			  else if (isym->st_shndx == SHN_COMMON)
   2287  1.1  christos 			    sym_sec = bfd_com_section_ptr;
   2288  1.1  christos 			  else
   2289  1.1  christos 			    sym_sec
   2290  1.1  christos 			      = bfd_section_from_elf_index (input_bfd,
   2291  1.1  christos 							    isym->st_shndx);
   2292  1.1  christos 
   2293  1.1  christos 			  sym_name = (bfd_elf_string_from_elf_section
   2294  1.1  christos 				      (input_bfd, symtab_hdr->sh_link,
   2295  1.1  christos 				       isym->st_name));
   2296  1.1  christos 
   2297  1.1  christos 			  /* Tack on an ID so we can uniquely identify this
   2298  1.1  christos 			     local symbol in the global hash table.  */
   2299  1.1  christos 			  amt = strlen (sym_name) + 10;
   2300  1.1  christos 			  new_name = bfd_malloc (amt);
   2301  1.1  christos 			  if (new_name == NULL)
   2302  1.1  christos 			    goto error_return;
   2303  1.1  christos 
   2304  1.1  christos 			  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
   2305  1.1  christos 			  sym_name = new_name;
   2306  1.1  christos 
   2307  1.1  christos 			  elftab = &hash_table->static_hash_table->root;
   2308  1.1  christos 			  hash = ((struct elf32_mn10300_link_hash_entry *)
   2309  1.1  christos 				  elf_link_hash_lookup (elftab, sym_name,
   2310  1.1  christos 							TRUE, TRUE, FALSE));
   2311  1.1  christos 			  free (new_name);
   2312  1.1  christos 			  compute_function_info (input_bfd, hash,
   2313  1.1  christos 						 isym->st_value, contents);
   2314  1.1  christos 			  hash->value = isym->st_value;
   2315  1.1  christos 			}
   2316  1.1  christos 		    }
   2317  1.1  christos 
   2318  1.1  christos 		  for (; hashes < end_hashes; hashes++)
   2319  1.1  christos 		    {
   2320  1.1  christos 		      hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
   2321  1.1  christos 		      if ((hash->root.root.type == bfd_link_hash_defined
   2322  1.1  christos 			   || hash->root.root.type == bfd_link_hash_defweak)
   2323  1.1  christos 			  && hash->root.root.u.def.section == section
   2324  1.1  christos 			  && hash->root.type == STT_FUNC)
   2325  1.1  christos 			compute_function_info (input_bfd, hash,
   2326  1.1  christos 					       (hash)->root.root.u.def.value,
   2327  1.1  christos 					       contents);
   2328  1.1  christos 		    }
   2329  1.1  christos 		}
   2330  1.1  christos 
   2331  1.1  christos 	      /* Cache or free any memory we allocated for the relocs.  */
   2332  1.1  christos 	      if (internal_relocs != NULL
   2333  1.1  christos 		  && elf_section_data (section)->relocs != internal_relocs)
   2334  1.1  christos 		free (internal_relocs);
   2335  1.1  christos 	      internal_relocs = NULL;
   2336  1.1  christos 
   2337  1.1  christos 	      /* Cache or free any memory we allocated for the contents.  */
   2338  1.1  christos 	      if (contents != NULL
   2339  1.1  christos 		  && elf_section_data (section)->this_hdr.contents != contents)
   2340  1.1  christos 		{
   2341  1.1  christos 		  if (! link_info->keep_memory)
   2342  1.1  christos 		    free (contents);
   2343  1.1  christos 		  else
   2344  1.1  christos 		    {
   2345  1.1  christos 		      /* Cache the section contents for elf_link_input_bfd.  */
   2346  1.1  christos 		      elf_section_data (section)->this_hdr.contents = contents;
   2347  1.1  christos 		    }
   2348  1.1  christos 		}
   2349  1.1  christos 	      contents = NULL;
   2350  1.1  christos 	    }
   2351  1.1  christos 
   2352  1.1  christos 	  /* Cache or free any memory we allocated for the symbols.  */
   2353  1.1  christos 	  if (isymbuf != NULL
   2354  1.1  christos 	      && symtab_hdr->contents != (unsigned char *) isymbuf)
   2355  1.1  christos 	    {
   2356  1.1  christos 	      if (! link_info->keep_memory)
   2357  1.1  christos 		free (isymbuf);
   2358  1.1  christos 	      else
   2359  1.1  christos 		{
   2360  1.1  christos 		  /* Cache the symbols for elf_link_input_bfd.  */
   2361  1.1  christos 		  symtab_hdr->contents = (unsigned char *) isymbuf;
   2362  1.1  christos 		}
   2363  1.1  christos 	    }
   2364  1.1  christos 	  isymbuf = NULL;
   2365  1.1  christos 	}
   2366  1.1  christos 
   2367  1.1  christos       /* Now iterate on each symbol in the hash table and perform
   2368  1.1  christos 	 the final initialization steps on each.  */
   2369  1.1  christos       elf32_mn10300_link_hash_traverse (hash_table,
   2370  1.1  christos 					elf32_mn10300_finish_hash_table_entry,
   2371  1.1  christos 					link_info);
   2372  1.1  christos       elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
   2373  1.1  christos 					elf32_mn10300_finish_hash_table_entry,
   2374  1.1  christos 					link_info);
   2375  1.1  christos 
   2376  1.1  christos       {
   2377  1.1  christos 	/* This section of code collects all our local symbols, sorts
   2378  1.1  christos 	   them by value, and looks for multiple symbols referring to
   2379  1.1  christos 	   the same address.  For those symbols, the flags are merged.
   2380  1.1  christos 	   At this point, the only flag that can be set is
   2381  1.1  christos 	   MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
   2382  1.1  christos 	   together.  */
   2383  1.1  christos 	int static_count = 0, i;
   2384  1.1  christos 	struct elf32_mn10300_link_hash_entry **entries;
   2385  1.1  christos 	struct elf32_mn10300_link_hash_entry **ptr;
   2386  1.1  christos 
   2387  1.1  christos 	elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
   2388  1.1  christos 					  elf32_mn10300_count_hash_table_entries,
   2389  1.1  christos 					  &static_count);
   2390  1.1  christos 
   2391  1.1  christos 	entries = bfd_malloc (static_count * sizeof (* ptr));
   2392  1.1  christos 
   2393  1.1  christos 	ptr = entries;
   2394  1.1  christos 	elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
   2395  1.1  christos 					  elf32_mn10300_list_hash_table_entries,
   2396  1.1  christos 					  & ptr);
   2397  1.1  christos 
   2398  1.1  christos 	qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
   2399  1.1  christos 
   2400  1.1  christos 	for (i = 0; i < static_count - 1; i++)
   2401  1.1  christos 	  if (entries[i]->value && entries[i]->value == entries[i+1]->value)
   2402  1.1  christos 	    {
   2403  1.1  christos 	      int v = entries[i]->flags;
   2404  1.1  christos 	      int j;
   2405  1.1  christos 
   2406  1.1  christos 	      for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
   2407  1.1  christos 		v |= entries[j]->flags;
   2408  1.1  christos 
   2409  1.1  christos 	      for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
   2410  1.1  christos 		entries[j]->flags = v;
   2411  1.1  christos 
   2412  1.1  christos 	      i = j - 1;
   2413  1.1  christos 	    }
   2414  1.1  christos       }
   2415  1.1  christos 
   2416  1.1  christos       /* All entries in the hash table are fully initialized.  */
   2417  1.1  christos       hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
   2418  1.1  christos 
   2419  1.1  christos       /* Now that everything has been initialized, go through each
   2420  1.1  christos 	 code section and delete any prologue insns which will be
   2421  1.1  christos 	 redundant because their operations will be performed by
   2422  1.1  christos 	 a "call" instruction.  */
   2423  1.1  christos       for (input_bfd = link_info->input_bfds;
   2424  1.1  christos 	   input_bfd != NULL;
   2425  1.1  christos 	   input_bfd = input_bfd->link_next)
   2426  1.1  christos 	{
   2427  1.1  christos 	  /* We're going to need all the local symbols for each bfd.  */
   2428  1.1  christos 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   2429  1.1  christos 	  if (symtab_hdr->sh_info != 0)
   2430  1.1  christos 	    {
   2431  1.1  christos 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   2432  1.1  christos 	      if (isymbuf == NULL)
   2433  1.1  christos 		isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   2434  1.1  christos 						symtab_hdr->sh_info, 0,
   2435  1.1  christos 						NULL, NULL, NULL);
   2436  1.1  christos 	      if (isymbuf == NULL)
   2437  1.1  christos 		goto error_return;
   2438  1.1  christos 	    }
   2439  1.1  christos 
   2440  1.1  christos 	  /* Walk over each section in this bfd.  */
   2441  1.1  christos 	  for (section = input_bfd->sections;
   2442  1.1  christos 	       section != NULL;
   2443  1.1  christos 	       section = section->next)
   2444  1.1  christos 	    {
   2445  1.1  christos 	      unsigned int sec_shndx;
   2446  1.1  christos 	      Elf_Internal_Sym *isym, *isymend;
   2447  1.1  christos 	      struct elf_link_hash_entry **hashes;
   2448  1.1  christos 	      struct elf_link_hash_entry **end_hashes;
   2449  1.1  christos 	      unsigned int symcount;
   2450  1.1  christos 
   2451  1.1  christos 	      /* Skip non-code sections and empty sections.  */
   2452  1.1  christos 	      if ((section->flags & SEC_CODE) == 0 || section->size == 0)
   2453  1.1  christos 		continue;
   2454  1.1  christos 
   2455  1.1  christos 	      if (section->reloc_count != 0)
   2456  1.1  christos 		{
   2457  1.1  christos 		  /* Get a copy of the native relocations.  */
   2458  1.1  christos 		  internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
   2459  1.1  christos 							       NULL, NULL,
   2460  1.1  christos 							       link_info->keep_memory);
   2461  1.1  christos 		  if (internal_relocs == NULL)
   2462  1.1  christos 		    goto error_return;
   2463  1.1  christos 		}
   2464  1.1  christos 
   2465  1.1  christos 	      /* Get cached copy of section contents if it exists.  */
   2466  1.1  christos 	      if (elf_section_data (section)->this_hdr.contents != NULL)
   2467  1.1  christos 		contents = elf_section_data (section)->this_hdr.contents;
   2468  1.1  christos 	      else
   2469  1.1  christos 		{
   2470  1.1  christos 		  /* Go get them off disk.  */
   2471  1.1  christos 		  if (!bfd_malloc_and_get_section (input_bfd, section,
   2472  1.1  christos 						   &contents))
   2473  1.1  christos 		    goto error_return;
   2474  1.1  christos 		}
   2475  1.1  christos 
   2476  1.1  christos 	      sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
   2477  1.1  christos 							     section);
   2478  1.1  christos 
   2479  1.1  christos 	      /* Now look for any function in this section which needs
   2480  1.1  christos 		 insns deleted from its prologue.  */
   2481  1.1  christos 	      isymend = isymbuf + symtab_hdr->sh_info;
   2482  1.1  christos 	      for (isym = isymbuf; isym < isymend; isym++)
   2483  1.1  christos 		{
   2484  1.1  christos 		  struct elf32_mn10300_link_hash_entry *sym_hash;
   2485  1.1  christos 		  asection *sym_sec = NULL;
   2486  1.1  christos 		  const char *sym_name;
   2487  1.1  christos 		  char *new_name;
   2488  1.1  christos 		  struct elf_link_hash_table *elftab;
   2489  1.1  christos 		  bfd_size_type amt;
   2490  1.1  christos 
   2491  1.1  christos 		  if (isym->st_shndx != sec_shndx)
   2492  1.1  christos 		    continue;
   2493  1.1  christos 
   2494  1.1  christos 		  if (isym->st_shndx == SHN_UNDEF)
   2495  1.1  christos 		    sym_sec = bfd_und_section_ptr;
   2496  1.1  christos 		  else if (isym->st_shndx == SHN_ABS)
   2497  1.1  christos 		    sym_sec = bfd_abs_section_ptr;
   2498  1.1  christos 		  else if (isym->st_shndx == SHN_COMMON)
   2499  1.1  christos 		    sym_sec = bfd_com_section_ptr;
   2500  1.1  christos 		  else
   2501  1.1  christos 		    sym_sec
   2502  1.1  christos 		      = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   2503  1.1  christos 
   2504  1.1  christos 		  sym_name
   2505  1.1  christos 		    = bfd_elf_string_from_elf_section (input_bfd,
   2506  1.1  christos 						       symtab_hdr->sh_link,
   2507  1.1  christos 						       isym->st_name);
   2508  1.1  christos 
   2509  1.1  christos 		  /* Tack on an ID so we can uniquely identify this
   2510  1.1  christos 		     local symbol in the global hash table.  */
   2511  1.1  christos 		  amt = strlen (sym_name) + 10;
   2512  1.1  christos 		  new_name = bfd_malloc (amt);
   2513  1.1  christos 		  if (new_name == NULL)
   2514  1.1  christos 		    goto error_return;
   2515  1.1  christos 		  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
   2516  1.1  christos 		  sym_name = new_name;
   2517  1.1  christos 
   2518  1.1  christos 		  elftab = & hash_table->static_hash_table->root;
   2519  1.1  christos 		  sym_hash = (struct elf32_mn10300_link_hash_entry *)
   2520  1.1  christos 		    elf_link_hash_lookup (elftab, sym_name,
   2521  1.1  christos 					  FALSE, FALSE, FALSE);
   2522  1.1  christos 
   2523  1.1  christos 		  free (new_name);
   2524  1.1  christos 		  if (sym_hash == NULL)
   2525  1.1  christos 		    continue;
   2526  1.1  christos 
   2527  1.1  christos 		  if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
   2528  1.1  christos 		      && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
   2529  1.1  christos 		    {
   2530  1.1  christos 		      int bytes = 0;
   2531  1.1  christos 
   2532  1.1  christos 		      /* Note that we've changed things.  */
   2533  1.1  christos 		      elf_section_data (section)->relocs = internal_relocs;
   2534  1.1  christos 		      elf_section_data (section)->this_hdr.contents = contents;
   2535  1.1  christos 		      symtab_hdr->contents = (unsigned char *) isymbuf;
   2536  1.1  christos 
   2537  1.1  christos 		      /* Count how many bytes we're going to delete.  */
   2538  1.1  christos 		      if (sym_hash->movm_args)
   2539  1.1  christos 			bytes += 2;
   2540  1.1  christos 
   2541  1.1  christos 		      if (sym_hash->stack_size > 0)
   2542  1.1  christos 			{
   2543  1.1  christos 			  if (sym_hash->stack_size <= 128)
   2544  1.1  christos 			    bytes += 3;
   2545  1.1  christos 			  else
   2546  1.1  christos 			    bytes += 4;
   2547  1.1  christos 			}
   2548  1.1  christos 
   2549  1.1  christos 		      /* Note that we've deleted prologue bytes for this
   2550  1.1  christos 			 function.  */
   2551  1.1  christos 		      sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
   2552  1.1  christos 
   2553  1.1  christos 		      /* Actually delete the bytes.  */
   2554  1.1  christos 		      if (!mn10300_elf_relax_delete_bytes (input_bfd,
   2555  1.1  christos 							   section,
   2556  1.1  christos 							   isym->st_value,
   2557  1.1  christos 							   bytes))
   2558  1.1  christos 			goto error_return;
   2559  1.1  christos 
   2560  1.1  christos 		      /* Something changed.  Not strictly necessary, but
   2561  1.1  christos 			 may lead to more relaxing opportunities.  */
   2562  1.1  christos 		      *again = TRUE;
   2563  1.1  christos 		    }
   2564  1.1  christos 		}
   2565  1.1  christos 
   2566  1.1  christos 	      /* Look for any global functions in this section which
   2567  1.1  christos 		 need insns deleted from their prologues.  */
   2568  1.1  christos 	      symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   2569  1.1  christos 			  - symtab_hdr->sh_info);
   2570  1.1  christos 	      hashes = elf_sym_hashes (input_bfd);
   2571  1.1  christos 	      end_hashes = hashes + symcount;
   2572  1.1  christos 	      for (; hashes < end_hashes; hashes++)
   2573  1.1  christos 		{
   2574  1.1  christos 		  struct elf32_mn10300_link_hash_entry *sym_hash;
   2575  1.1  christos 
   2576  1.1  christos 		  sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
   2577  1.1  christos 		  if ((sym_hash->root.root.type == bfd_link_hash_defined
   2578  1.1  christos 		       || sym_hash->root.root.type == bfd_link_hash_defweak)
   2579  1.1  christos 		      && sym_hash->root.root.u.def.section == section
   2580  1.1  christos 		      && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
   2581  1.1  christos 		      && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
   2582  1.1  christos 		    {
   2583  1.1  christos 		      int bytes = 0;
   2584  1.1  christos 		      bfd_vma symval;
   2585  1.1  christos 
   2586  1.1  christos 		      /* Note that we've changed things.  */
   2587  1.1  christos 		      elf_section_data (section)->relocs = internal_relocs;
   2588  1.1  christos 		      elf_section_data (section)->this_hdr.contents = contents;
   2589  1.1  christos 		      symtab_hdr->contents = (unsigned char *) isymbuf;
   2590  1.1  christos 
   2591  1.1  christos 		      /* Count how many bytes we're going to delete.  */
   2592  1.1  christos 		      if (sym_hash->movm_args)
   2593  1.1  christos 			bytes += 2;
   2594  1.1  christos 
   2595  1.1  christos 		      if (sym_hash->stack_size > 0)
   2596  1.1  christos 			{
   2597  1.1  christos 			  if (sym_hash->stack_size <= 128)
   2598  1.1  christos 			    bytes += 3;
   2599  1.1  christos 			  else
   2600  1.1  christos 			    bytes += 4;
   2601  1.1  christos 			}
   2602  1.1  christos 
   2603  1.1  christos 		      /* Note that we've deleted prologue bytes for this
   2604  1.1  christos 			 function.  */
   2605  1.1  christos 		      sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
   2606  1.1  christos 
   2607  1.1  christos 		      /* Actually delete the bytes.  */
   2608  1.1  christos 		      symval = sym_hash->root.root.u.def.value;
   2609  1.1  christos 		      if (!mn10300_elf_relax_delete_bytes (input_bfd,
   2610  1.1  christos 							   section,
   2611  1.1  christos 							   symval,
   2612  1.1  christos 							   bytes))
   2613  1.1  christos 			goto error_return;
   2614  1.1  christos 
   2615  1.1  christos 		      /* Something changed.  Not strictly necessary, but
   2616  1.1  christos 			 may lead to more relaxing opportunities.  */
   2617  1.1  christos 		      *again = TRUE;
   2618  1.1  christos 		    }
   2619  1.1  christos 		}
   2620  1.1  christos 
   2621  1.1  christos 	      /* Cache or free any memory we allocated for the relocs.  */
   2622  1.1  christos 	      if (internal_relocs != NULL
   2623  1.1  christos 		  && elf_section_data (section)->relocs != internal_relocs)
   2624  1.1  christos 		free (internal_relocs);
   2625  1.1  christos 	      internal_relocs = NULL;
   2626  1.1  christos 
   2627  1.1  christos 	      /* Cache or free any memory we allocated for the contents.  */
   2628  1.1  christos 	      if (contents != NULL
   2629  1.1  christos 		  && elf_section_data (section)->this_hdr.contents != contents)
   2630  1.1  christos 		{
   2631  1.1  christos 		  if (! link_info->keep_memory)
   2632  1.1  christos 		    free (contents);
   2633  1.1  christos 		  else
   2634  1.1  christos 		    /* Cache the section contents for elf_link_input_bfd.  */
   2635  1.1  christos 		    elf_section_data (section)->this_hdr.contents = contents;
   2636  1.1  christos 		}
   2637  1.1  christos 	      contents = NULL;
   2638  1.1  christos 	    }
   2639  1.1  christos 
   2640  1.1  christos 	  /* Cache or free any memory we allocated for the symbols.  */
   2641  1.1  christos 	  if (isymbuf != NULL
   2642  1.1  christos 	      && symtab_hdr->contents != (unsigned char *) isymbuf)
   2643  1.1  christos 	    {
   2644  1.1  christos 	      if (! link_info->keep_memory)
   2645  1.1  christos 		free (isymbuf);
   2646  1.1  christos 	      else
   2647  1.1  christos 		/* Cache the symbols for elf_link_input_bfd.  */
   2648  1.1  christos 		symtab_hdr->contents = (unsigned char *) isymbuf;
   2649  1.1  christos 	    }
   2650  1.1  christos 	  isymbuf = NULL;
   2651  1.1  christos 	}
   2652  1.1  christos     }
   2653  1.1  christos 
   2654  1.1  christos   /* (Re)initialize for the basic instruction shortening/relaxing pass.  */
   2655  1.1  christos   contents = NULL;
   2656  1.1  christos   internal_relocs = NULL;
   2657  1.1  christos   isymbuf = NULL;
   2658  1.1  christos   /* For error_return.  */
   2659  1.1  christos   section = sec;
   2660  1.1  christos 
   2661  1.1  christos   /* We don't have to do anything for a relocatable link, if
   2662  1.1  christos      this section does not have relocs, or if this is not a
   2663  1.1  christos      code section.  */
   2664  1.1  christos   if (link_info->relocatable
   2665  1.1  christos       || (sec->flags & SEC_RELOC) == 0
   2666  1.1  christos       || sec->reloc_count == 0
   2667  1.1  christos       || (sec->flags & SEC_CODE) == 0)
   2668  1.1  christos     return TRUE;
   2669  1.1  christos 
   2670  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2671  1.1  christos 
   2672  1.1  christos   /* Get a copy of the native relocations.  */
   2673  1.1  christos   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   2674  1.1  christos 					       link_info->keep_memory);
   2675  1.1  christos   if (internal_relocs == NULL)
   2676  1.1  christos     goto error_return;
   2677  1.1  christos 
   2678  1.1  christos   /* Scan for worst case alignment gap changes.  Note that this logic
   2679  1.1  christos      is not ideal; what we should do is run this scan for every
   2680  1.1  christos      opcode/address range and adjust accordingly, but that's
   2681  1.1  christos      expensive.  Worst case is that for an alignment of N bytes, we
   2682  1.1  christos      move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
   2683  1.1  christos      all before it.  Plus, this still doesn't cover cross-section
   2684  1.1  christos      jumps with section alignment.  */
   2685  1.1  christos   irelend = internal_relocs + sec->reloc_count;
   2686  1.1  christos   align_gap_adjustment = 0;
   2687  1.1  christos   for (irel = internal_relocs; irel < irelend; irel++)
   2688  1.1  christos     {
   2689  1.1  christos       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
   2690  1.1  christos 	{
   2691  1.1  christos 	  bfd_vma adj = 1 << irel->r_addend;
   2692  1.1  christos 	  bfd_vma aend = irel->r_offset;
   2693  1.1  christos 
   2694  1.1  christos 	  aend = BFD_ALIGN (aend, 1 << irel->r_addend);
   2695  1.1  christos 	  adj = 2 * adj - adj - 1;
   2696  1.1  christos 
   2697  1.1  christos 	  /* Record the biggest adjustmnet.  Skip any alignment at the
   2698  1.1  christos 	     end of our section.  */
   2699  1.1  christos 	  if (align_gap_adjustment < adj
   2700  1.1  christos 	      && aend < sec->output_section->vma + sec->output_offset + sec->size)
   2701  1.1  christos 	    align_gap_adjustment = adj;
   2702  1.1  christos 	}
   2703  1.1  christos     }
   2704  1.1  christos 
   2705  1.1  christos   /* Walk through them looking for relaxing opportunities.  */
   2706  1.1  christos   irelend = internal_relocs + sec->reloc_count;
   2707  1.1  christos   for (irel = internal_relocs; irel < irelend; irel++)
   2708  1.1  christos     {
   2709  1.1  christos       bfd_vma symval;
   2710  1.1  christos       bfd_signed_vma jump_offset;
   2711  1.1  christos       asection *sym_sec = NULL;
   2712  1.1  christos       struct elf32_mn10300_link_hash_entry *h = NULL;
   2713  1.1  christos 
   2714  1.1  christos       /* If this isn't something that can be relaxed, then ignore
   2715  1.1  christos 	 this reloc.  */
   2716  1.1  christos       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
   2717  1.1  christos 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
   2718  1.1  christos 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
   2719  1.1  christos 	continue;
   2720  1.1  christos 
   2721  1.1  christos       /* Get the section contents if we haven't done so already.  */
   2722  1.1  christos       if (contents == NULL)
   2723  1.1  christos 	{
   2724  1.1  christos 	  /* Get cached copy if it exists.  */
   2725  1.1  christos 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
   2726  1.1  christos 	    contents = elf_section_data (sec)->this_hdr.contents;
   2727  1.1  christos 	  else
   2728  1.1  christos 	    {
   2729  1.1  christos 	      /* Go get them off disk.  */
   2730  1.1  christos 	      if (!bfd_malloc_and_get_section (abfd, sec, &contents))
   2731  1.1  christos 		goto error_return;
   2732  1.1  christos 	    }
   2733  1.1  christos 	}
   2734  1.1  christos 
   2735  1.1  christos       /* Read this BFD's symbols if we haven't done so already.  */
   2736  1.1  christos       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   2737  1.1  christos 	{
   2738  1.1  christos 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   2739  1.1  christos 	  if (isymbuf == NULL)
   2740  1.1  christos 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   2741  1.1  christos 					    symtab_hdr->sh_info, 0,
   2742  1.1  christos 					    NULL, NULL, NULL);
   2743  1.1  christos 	  if (isymbuf == NULL)
   2744  1.1  christos 	    goto error_return;
   2745  1.1  christos 	}
   2746  1.1  christos 
   2747  1.1  christos       /* Get the value of the symbol referred to by the reloc.  */
   2748  1.1  christos       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
   2749  1.1  christos 	{
   2750  1.1  christos 	  Elf_Internal_Sym *isym;
   2751  1.1  christos 	  const char *sym_name;
   2752  1.1  christos 	  char *new_name;
   2753  1.1  christos 
   2754  1.1  christos 	  /* A local symbol.  */
   2755  1.1  christos 	  isym = isymbuf + ELF32_R_SYM (irel->r_info);
   2756  1.1  christos 	  if (isym->st_shndx == SHN_UNDEF)
   2757  1.1  christos 	    sym_sec = bfd_und_section_ptr;
   2758  1.1  christos 	  else if (isym->st_shndx == SHN_ABS)
   2759  1.1  christos 	    sym_sec = bfd_abs_section_ptr;
   2760  1.1  christos 	  else if (isym->st_shndx == SHN_COMMON)
   2761  1.1  christos 	    sym_sec = bfd_com_section_ptr;
   2762  1.1  christos 	  else
   2763  1.1  christos 	    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   2764  1.1  christos 
   2765  1.1  christos 	  sym_name = bfd_elf_string_from_elf_section (abfd,
   2766  1.1  christos 						      symtab_hdr->sh_link,
   2767  1.1  christos 						      isym->st_name);
   2768  1.1  christos 
   2769  1.1  christos 	  if ((sym_sec->flags & SEC_MERGE)
   2770  1.1  christos 	      && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
   2771  1.1  christos 	    {
   2772  1.1  christos 	      symval = isym->st_value;
   2773  1.1  christos 
   2774  1.1  christos 	      /* GAS may reduce relocations against symbols in SEC_MERGE
   2775  1.1  christos 		 sections to a relocation against the section symbol when
   2776  1.1  christos 		 the original addend was zero.  When the reloc is against
   2777  1.1  christos 		 a section symbol we should include the addend in the
   2778  1.1  christos 		 offset passed to _bfd_merged_section_offset, since the
   2779  1.1  christos 		 location of interest is the original symbol.  On the
   2780  1.1  christos 		 other hand, an access to "sym+addend" where "sym" is not
   2781  1.1  christos 		 a section symbol should not include the addend;  Such an
   2782  1.1  christos 		 access is presumed to be an offset from "sym";  The
   2783  1.1  christos 		 location of interest is just "sym".  */
   2784  1.1  christos 	      if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
   2785  1.1  christos 		symval += irel->r_addend;
   2786  1.1  christos 
   2787  1.1  christos 	      symval = _bfd_merged_section_offset (abfd, & sym_sec,
   2788  1.1  christos 						   elf_section_data (sym_sec)->sec_info,
   2789  1.1  christos 						   symval);
   2790  1.1  christos 
   2791  1.1  christos 	      if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   2792  1.1  christos 		symval += irel->r_addend;
   2793  1.1  christos 
   2794  1.1  christos 	      symval += sym_sec->output_section->vma
   2795  1.1  christos 		+ sym_sec->output_offset - irel->r_addend;
   2796  1.1  christos 	    }
   2797  1.1  christos 	  else
   2798  1.1  christos 	    symval = (isym->st_value
   2799  1.1  christos 		      + sym_sec->output_section->vma
   2800  1.1  christos 		      + sym_sec->output_offset);
   2801  1.1  christos 
   2802  1.1  christos 	  /* Tack on an ID so we can uniquely identify this
   2803  1.1  christos 	     local symbol in the global hash table.  */
   2804  1.1  christos 	  new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
   2805  1.1  christos 	  if (new_name == NULL)
   2806  1.1  christos 	    goto error_return;
   2807  1.1  christos 	  sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
   2808  1.1  christos 	  sym_name = new_name;
   2809  1.1  christos 
   2810  1.1  christos 	  h = (struct elf32_mn10300_link_hash_entry *)
   2811  1.1  christos 		elf_link_hash_lookup (&hash_table->static_hash_table->root,
   2812  1.1  christos 				      sym_name, FALSE, FALSE, FALSE);
   2813  1.1  christos 	  free (new_name);
   2814  1.1  christos 	}
   2815  1.1  christos       else
   2816  1.1  christos 	{
   2817  1.1  christos 	  unsigned long indx;
   2818  1.1  christos 
   2819  1.1  christos 	  /* An external symbol.  */
   2820  1.1  christos 	  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   2821  1.1  christos 	  h = (struct elf32_mn10300_link_hash_entry *)
   2822  1.1  christos 		(elf_sym_hashes (abfd)[indx]);
   2823  1.1  christos 	  BFD_ASSERT (h != NULL);
   2824  1.1  christos 	  if (h->root.root.type != bfd_link_hash_defined
   2825  1.1  christos 	      && h->root.root.type != bfd_link_hash_defweak)
   2826  1.1  christos 	    /* This appears to be a reference to an undefined
   2827  1.1  christos 	       symbol.  Just ignore it--it will be caught by the
   2828  1.1  christos 	       regular reloc processing.  */
   2829  1.1  christos 	    continue;
   2830  1.1  christos 
   2831  1.1  christos 	  /* Check for a reference to a discarded symbol and ignore it.  */
   2832  1.1  christos 	  if (h->root.root.u.def.section->output_section == NULL)
   2833  1.1  christos 	    continue;
   2834  1.1  christos 
   2835  1.1  christos 	  sym_sec = h->root.root.u.def.section->output_section;
   2836  1.1  christos 
   2837  1.1  christos 	  symval = (h->root.root.u.def.value
   2838  1.1  christos 		    + h->root.root.u.def.section->output_section->vma
   2839  1.1  christos 		    + h->root.root.u.def.section->output_offset);
   2840  1.1  christos 	}
   2841  1.1  christos 
   2842  1.1  christos       /* For simplicity of coding, we are going to modify the section
   2843  1.1  christos 	 contents, the section relocs, and the BFD symbol table.  We
   2844  1.1  christos 	 must tell the rest of the code not to free up this
   2845  1.1  christos 	 information.  It would be possible to instead create a table
   2846  1.1  christos 	 of changes which have to be made, as is done in coff-mips.c;
   2847  1.1  christos 	 that would be more work, but would require less memory when
   2848  1.1  christos 	 the linker is run.  */
   2849  1.1  christos 
   2850  1.1  christos       /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
   2851  1.1  christos 	 branch/call, also deal with "call" -> "calls" conversions and
   2852  1.1  christos 	 insertion of prologue data into "call" instructions.  */
   2853  1.1  christos       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
   2854  1.1  christos 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
   2855  1.1  christos 	{
   2856  1.1  christos 	  bfd_vma value = symval;
   2857  1.1  christos 
   2858  1.1  christos 	  if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
   2859  1.1  christos 	      && h != NULL
   2860  1.1  christos 	      && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
   2861  1.1  christos 	      && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
   2862  1.1  christos 	      && h->root.plt.offset != (bfd_vma) -1)
   2863  1.1  christos 	    {
   2864  1.1  christos 	      asection * splt;
   2865  1.1  christos 
   2866  1.1  christos 	      splt = bfd_get_section_by_name (elf_hash_table (link_info)
   2867  1.1  christos 					      ->dynobj, ".plt");
   2868  1.1  christos 
   2869  1.1  christos 	      value = ((splt->output_section->vma
   2870  1.1  christos 			+ splt->output_offset
   2871  1.1  christos 			+ h->root.plt.offset)
   2872  1.1  christos 		       - (sec->output_section->vma
   2873  1.1  christos 			  + sec->output_offset
   2874  1.1  christos 			  + irel->r_offset));
   2875  1.1  christos 	    }
   2876  1.1  christos 
   2877  1.1  christos 	  /* If we've got a "call" instruction that needs to be turned
   2878  1.1  christos 	     into a "calls" instruction, do so now.  It saves a byte.  */
   2879  1.1  christos 	  if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
   2880  1.1  christos 	    {
   2881  1.1  christos 	      unsigned char code;
   2882  1.1  christos 
   2883  1.1  christos 	      /* Get the opcode.  */
   2884  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   2885  1.1  christos 
   2886  1.1  christos 	      /* Make sure we're working with a "call" instruction!  */
   2887  1.1  christos 	      if (code == 0xdd)
   2888  1.1  christos 		{
   2889  1.1  christos 		  /* Note that we've changed the relocs, section contents,
   2890  1.1  christos 		     etc.  */
   2891  1.1  christos 		  elf_section_data (sec)->relocs = internal_relocs;
   2892  1.1  christos 		  elf_section_data (sec)->this_hdr.contents = contents;
   2893  1.1  christos 		  symtab_hdr->contents = (unsigned char *) isymbuf;
   2894  1.1  christos 
   2895  1.1  christos 		  /* Fix the opcode.  */
   2896  1.1  christos 		  bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
   2897  1.1  christos 		  bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
   2898  1.1  christos 
   2899  1.1  christos 		  /* Fix irel->r_offset and irel->r_addend.  */
   2900  1.1  christos 		  irel->r_offset += 1;
   2901  1.1  christos 		  irel->r_addend += 1;
   2902  1.1  christos 
   2903  1.1  christos 		  /* Delete one byte of data.  */
   2904  1.1  christos 		  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   2905  1.1  christos 						       irel->r_offset + 3, 1))
   2906  1.1  christos 		    goto error_return;
   2907  1.1  christos 
   2908  1.1  christos 		  /* That will change things, so, we should relax again.
   2909  1.1  christos 		     Note that this is not required, and it may be slow.  */
   2910  1.1  christos 		  *again = TRUE;
   2911  1.1  christos 		}
   2912  1.1  christos 	    }
   2913  1.1  christos 	  else if (h)
   2914  1.1  christos 	    {
   2915  1.1  christos 	      /* We've got a "call" instruction which needs some data
   2916  1.1  christos 		 from target function filled in.  */
   2917  1.1  christos 	      unsigned char code;
   2918  1.1  christos 
   2919  1.1  christos 	      /* Get the opcode.  */
   2920  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   2921  1.1  christos 
   2922  1.1  christos 	      /* Insert data from the target function into the "call"
   2923  1.1  christos 		 instruction if needed.  */
   2924  1.1  christos 	      if (code == 0xdd)
   2925  1.1  christos 		{
   2926  1.1  christos 		  bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
   2927  1.1  christos 		  bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
   2928  1.1  christos 			     contents + irel->r_offset + 5);
   2929  1.1  christos 		}
   2930  1.1  christos 	    }
   2931  1.1  christos 
   2932  1.1  christos 	  /* Deal with pc-relative gunk.  */
   2933  1.1  christos 	  value -= (sec->output_section->vma + sec->output_offset);
   2934  1.1  christos 	  value -= irel->r_offset;
   2935  1.1  christos 	  value += irel->r_addend;
   2936  1.1  christos 
   2937  1.1  christos 	  /* See if the value will fit in 16 bits, note the high value is
   2938  1.1  christos 	     0x7fff + 2 as the target will be two bytes closer if we are
   2939  1.1  christos 	     able to relax, if it's in the same section.  */
   2940  1.1  christos 	  if (sec->output_section == sym_sec->output_section)
   2941  1.1  christos 	    jump_offset = 0x8001;
   2942  1.1  christos 	  else
   2943  1.1  christos 	    jump_offset = 0x7fff;
   2944  1.1  christos 
   2945  1.1  christos 	  /* Account for jumps across alignment boundaries using
   2946  1.1  christos 	     align_gap_adjustment.  */
   2947  1.1  christos 	  if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
   2948  1.1  christos 	      && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
   2949  1.1  christos 	    {
   2950  1.1  christos 	      unsigned char code;
   2951  1.1  christos 
   2952  1.1  christos 	      /* Get the opcode.  */
   2953  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   2954  1.1  christos 
   2955  1.1  christos 	      if (code != 0xdc && code != 0xdd && code != 0xff)
   2956  1.1  christos 		continue;
   2957  1.1  christos 
   2958  1.1  christos 	      /* Note that we've changed the relocs, section contents, etc.  */
   2959  1.1  christos 	      elf_section_data (sec)->relocs = internal_relocs;
   2960  1.1  christos 	      elf_section_data (sec)->this_hdr.contents = contents;
   2961  1.1  christos 	      symtab_hdr->contents = (unsigned char *) isymbuf;
   2962  1.1  christos 
   2963  1.1  christos 	      /* Fix the opcode.  */
   2964  1.1  christos 	      if (code == 0xdc)
   2965  1.1  christos 		bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
   2966  1.1  christos 	      else if (code == 0xdd)
   2967  1.1  christos 		bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
   2968  1.1  christos 	      else if (code == 0xff)
   2969  1.1  christos 		bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
   2970  1.1  christos 
   2971  1.1  christos 	      /* Fix the relocation's type.  */
   2972  1.1  christos 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   2973  1.1  christos 					   (ELF32_R_TYPE (irel->r_info)
   2974  1.1  christos 					    == (int) R_MN10300_PLT32)
   2975  1.1  christos 					   ? R_MN10300_PLT16 :
   2976  1.1  christos 					   R_MN10300_PCREL16);
   2977  1.1  christos 
   2978  1.1  christos 	      /* Delete two bytes of data.  */
   2979  1.1  christos 	      if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   2980  1.1  christos 						   irel->r_offset + 1, 2))
   2981  1.1  christos 		goto error_return;
   2982  1.1  christos 
   2983  1.1  christos 	      /* That will change things, so, we should relax again.
   2984  1.1  christos 		 Note that this is not required, and it may be slow.  */
   2985  1.1  christos 	      *again = TRUE;
   2986  1.1  christos 	    }
   2987  1.1  christos 	}
   2988  1.1  christos 
   2989  1.1  christos       /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
   2990  1.1  christos 	 branch.  */
   2991  1.1  christos       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
   2992  1.1  christos 	{
   2993  1.1  christos 	  bfd_vma value = symval;
   2994  1.1  christos 
   2995  1.1  christos 	  /* If we've got a "call" instruction that needs to be turned
   2996  1.1  christos 	     into a "calls" instruction, do so now.  It saves a byte.  */
   2997  1.1  christos 	  if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
   2998  1.1  christos 	    {
   2999  1.1  christos 	      unsigned char code;
   3000  1.1  christos 
   3001  1.1  christos 	      /* Get the opcode.  */
   3002  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   3003  1.1  christos 
   3004  1.1  christos 	      /* Make sure we're working with a "call" instruction!  */
   3005  1.1  christos 	      if (code == 0xcd)
   3006  1.1  christos 		{
   3007  1.1  christos 		  /* Note that we've changed the relocs, section contents,
   3008  1.1  christos 		     etc.  */
   3009  1.1  christos 		  elf_section_data (sec)->relocs = internal_relocs;
   3010  1.1  christos 		  elf_section_data (sec)->this_hdr.contents = contents;
   3011  1.1  christos 		  symtab_hdr->contents = (unsigned char *) isymbuf;
   3012  1.1  christos 
   3013  1.1  christos 		  /* Fix the opcode.  */
   3014  1.1  christos 		  bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
   3015  1.1  christos 		  bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
   3016  1.1  christos 
   3017  1.1  christos 		  /* Fix irel->r_offset and irel->r_addend.  */
   3018  1.1  christos 		  irel->r_offset += 1;
   3019  1.1  christos 		  irel->r_addend += 1;
   3020  1.1  christos 
   3021  1.1  christos 		  /* Delete one byte of data.  */
   3022  1.1  christos 		  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3023  1.1  christos 						       irel->r_offset + 1, 1))
   3024  1.1  christos 		    goto error_return;
   3025  1.1  christos 
   3026  1.1  christos 		  /* That will change things, so, we should relax again.
   3027  1.1  christos 		     Note that this is not required, and it may be slow.  */
   3028  1.1  christos 		  *again = TRUE;
   3029  1.1  christos 		}
   3030  1.1  christos 	    }
   3031  1.1  christos 	  else if (h)
   3032  1.1  christos 	    {
   3033  1.1  christos 	      unsigned char code;
   3034  1.1  christos 
   3035  1.1  christos 	      /* Get the opcode.  */
   3036  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   3037  1.1  christos 
   3038  1.1  christos 	      /* Insert data from the target function into the "call"
   3039  1.1  christos 		 instruction if needed.  */
   3040  1.1  christos 	      if (code == 0xcd)
   3041  1.1  christos 		{
   3042  1.1  christos 		  bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
   3043  1.1  christos 		  bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
   3044  1.1  christos 			     contents + irel->r_offset + 3);
   3045  1.1  christos 		}
   3046  1.1  christos 	    }
   3047  1.1  christos 
   3048  1.1  christos 	  /* Deal with pc-relative gunk.  */
   3049  1.1  christos 	  value -= (sec->output_section->vma + sec->output_offset);
   3050  1.1  christos 	  value -= irel->r_offset;
   3051  1.1  christos 	  value += irel->r_addend;
   3052  1.1  christos 
   3053  1.1  christos 	  /* See if the value will fit in 8 bits, note the high value is
   3054  1.1  christos 	     0x7f + 1 as the target will be one bytes closer if we are
   3055  1.1  christos 	     able to relax.  */
   3056  1.1  christos 	  if ((long) value < 0x80 && (long) value > -0x80)
   3057  1.1  christos 	    {
   3058  1.1  christos 	      unsigned char code;
   3059  1.1  christos 
   3060  1.1  christos 	      /* Get the opcode.  */
   3061  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   3062  1.1  christos 
   3063  1.1  christos 	      if (code != 0xcc)
   3064  1.1  christos 		continue;
   3065  1.1  christos 
   3066  1.1  christos 	      /* Note that we've changed the relocs, section contents, etc.  */
   3067  1.1  christos 	      elf_section_data (sec)->relocs = internal_relocs;
   3068  1.1  christos 	      elf_section_data (sec)->this_hdr.contents = contents;
   3069  1.1  christos 	      symtab_hdr->contents = (unsigned char *) isymbuf;
   3070  1.1  christos 
   3071  1.1  christos 	      /* Fix the opcode.  */
   3072  1.1  christos 	      bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
   3073  1.1  christos 
   3074  1.1  christos 	      /* Fix the relocation's type.  */
   3075  1.1  christos 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3076  1.1  christos 					   R_MN10300_PCREL8);
   3077  1.1  christos 
   3078  1.1  christos 	      /* Delete one byte of data.  */
   3079  1.1  christos 	      if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3080  1.1  christos 						   irel->r_offset + 1, 1))
   3081  1.1  christos 		goto error_return;
   3082  1.1  christos 
   3083  1.1  christos 	      /* That will change things, so, we should relax again.
   3084  1.1  christos 		 Note that this is not required, and it may be slow.  */
   3085  1.1  christos 	      *again = TRUE;
   3086  1.1  christos 	    }
   3087  1.1  christos 	}
   3088  1.1  christos 
   3089  1.1  christos       /* Try to eliminate an unconditional 8 bit pc-relative branch
   3090  1.1  christos 	 which immediately follows a conditional 8 bit pc-relative
   3091  1.1  christos 	 branch around the unconditional branch.
   3092  1.1  christos 
   3093  1.1  christos 	    original:		new:
   3094  1.1  christos 	    bCC lab1		bCC' lab2
   3095  1.1  christos 	    bra lab2
   3096  1.1  christos 	   lab1:	       lab1:
   3097  1.1  christos 
   3098  1.1  christos 	 This happens when the bCC can't reach lab2 at assembly time,
   3099  1.1  christos 	 but due to other relaxations it can reach at link time.  */
   3100  1.1  christos       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
   3101  1.1  christos 	{
   3102  1.1  christos 	  Elf_Internal_Rela *nrel;
   3103  1.1  christos 	  bfd_vma value = symval;
   3104  1.1  christos 	  unsigned char code;
   3105  1.1  christos 
   3106  1.1  christos 	  /* Deal with pc-relative gunk.  */
   3107  1.1  christos 	  value -= (sec->output_section->vma + sec->output_offset);
   3108  1.1  christos 	  value -= irel->r_offset;
   3109  1.1  christos 	  value += irel->r_addend;
   3110  1.1  christos 
   3111  1.1  christos 	  /* Do nothing if this reloc is the last byte in the section.  */
   3112  1.1  christos 	  if (irel->r_offset == sec->size)
   3113  1.1  christos 	    continue;
   3114  1.1  christos 
   3115  1.1  christos 	  /* See if the next instruction is an unconditional pc-relative
   3116  1.1  christos 	     branch, more often than not this test will fail, so we
   3117  1.1  christos 	     test it first to speed things up.  */
   3118  1.1  christos 	  code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
   3119  1.1  christos 	  if (code != 0xca)
   3120  1.1  christos 	    continue;
   3121  1.1  christos 
   3122  1.1  christos 	  /* Also make sure the next relocation applies to the next
   3123  1.1  christos 	     instruction and that it's a pc-relative 8 bit branch.  */
   3124  1.1  christos 	  nrel = irel + 1;
   3125  1.1  christos 	  if (nrel == irelend
   3126  1.1  christos 	      || irel->r_offset + 2 != nrel->r_offset
   3127  1.1  christos 	      || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
   3128  1.1  christos 	    continue;
   3129  1.1  christos 
   3130  1.1  christos 	  /* Make sure our destination immediately follows the
   3131  1.1  christos 	     unconditional branch.  */
   3132  1.1  christos 	  if (symval != (sec->output_section->vma + sec->output_offset
   3133  1.1  christos 			 + irel->r_offset + 3))
   3134  1.1  christos 	    continue;
   3135  1.1  christos 
   3136  1.1  christos 	  /* Now make sure we are a conditional branch.  This may not
   3137  1.1  christos 	     be necessary, but why take the chance.
   3138  1.1  christos 
   3139  1.1  christos 	     Note these checks assume that R_MN10300_PCREL8 relocs
   3140  1.1  christos 	     only occur on bCC and bCCx insns.  If they occured
   3141  1.1  christos 	     elsewhere, we'd need to know the start of this insn
   3142  1.1  christos 	     for this check to be accurate.  */
   3143  1.1  christos 	  code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   3144  1.1  christos 	  if (code != 0xc0 && code != 0xc1 && code != 0xc2
   3145  1.1  christos 	      && code != 0xc3 && code != 0xc4 && code != 0xc5
   3146  1.1  christos 	      && code != 0xc6 && code != 0xc7 && code != 0xc8
   3147  1.1  christos 	      && code != 0xc9 && code != 0xe8 && code != 0xe9
   3148  1.1  christos 	      && code != 0xea && code != 0xeb)
   3149  1.1  christos 	    continue;
   3150  1.1  christos 
   3151  1.1  christos 	  /* We also have to be sure there is no symbol/label
   3152  1.1  christos 	     at the unconditional branch.  */
   3153  1.1  christos 	  if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
   3154  1.1  christos 					    irel->r_offset + 1))
   3155  1.1  christos 	    continue;
   3156  1.1  christos 
   3157  1.1  christos 	  /* Note that we've changed the relocs, section contents, etc.  */
   3158  1.1  christos 	  elf_section_data (sec)->relocs = internal_relocs;
   3159  1.1  christos 	  elf_section_data (sec)->this_hdr.contents = contents;
   3160  1.1  christos 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   3161  1.1  christos 
   3162  1.1  christos 	  /* Reverse the condition of the first branch.  */
   3163  1.1  christos 	  switch (code)
   3164  1.1  christos 	    {
   3165  1.1  christos 	    case 0xc8:
   3166  1.1  christos 	      code = 0xc9;
   3167  1.1  christos 	      break;
   3168  1.1  christos 	    case 0xc9:
   3169  1.1  christos 	      code = 0xc8;
   3170  1.1  christos 	      break;
   3171  1.1  christos 	    case 0xc0:
   3172  1.1  christos 	      code = 0xc2;
   3173  1.1  christos 	      break;
   3174  1.1  christos 	    case 0xc2:
   3175  1.1  christos 	      code = 0xc0;
   3176  1.1  christos 	      break;
   3177  1.1  christos 	    case 0xc3:
   3178  1.1  christos 	      code = 0xc1;
   3179  1.1  christos 	      break;
   3180  1.1  christos 	    case 0xc1:
   3181  1.1  christos 	      code = 0xc3;
   3182  1.1  christos 	      break;
   3183  1.1  christos 	    case 0xc4:
   3184  1.1  christos 	      code = 0xc6;
   3185  1.1  christos 	      break;
   3186  1.1  christos 	    case 0xc6:
   3187  1.1  christos 	      code = 0xc4;
   3188  1.1  christos 	      break;
   3189  1.1  christos 	    case 0xc7:
   3190  1.1  christos 	      code = 0xc5;
   3191  1.1  christos 	      break;
   3192  1.1  christos 	    case 0xc5:
   3193  1.1  christos 	      code = 0xc7;
   3194  1.1  christos 	      break;
   3195  1.1  christos 	    case 0xe8:
   3196  1.1  christos 	      code = 0xe9;
   3197  1.1  christos 	      break;
   3198  1.1  christos 	    case 0x9d:
   3199  1.1  christos 	      code = 0xe8;
   3200  1.1  christos 	      break;
   3201  1.1  christos 	    case 0xea:
   3202  1.1  christos 	      code = 0xeb;
   3203  1.1  christos 	      break;
   3204  1.1  christos 	    case 0xeb:
   3205  1.1  christos 	      code = 0xea;
   3206  1.1  christos 	      break;
   3207  1.1  christos 	    }
   3208  1.1  christos 	  bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
   3209  1.1  christos 
   3210  1.1  christos 	  /* Set the reloc type and symbol for the first branch
   3211  1.1  christos 	     from the second branch.  */
   3212  1.1  christos 	  irel->r_info = nrel->r_info;
   3213  1.1  christos 
   3214  1.1  christos 	  /* Make the reloc for the second branch a null reloc.  */
   3215  1.1  christos 	  nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
   3216  1.1  christos 				       R_MN10300_NONE);
   3217  1.1  christos 
   3218  1.1  christos 	  /* Delete two bytes of data.  */
   3219  1.1  christos 	  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3220  1.1  christos 					       irel->r_offset + 1, 2))
   3221  1.1  christos 	    goto error_return;
   3222  1.1  christos 
   3223  1.1  christos 	  /* That will change things, so, we should relax again.
   3224  1.1  christos 	     Note that this is not required, and it may be slow.  */
   3225  1.1  christos 	  *again = TRUE;
   3226  1.1  christos 	}
   3227  1.1  christos 
   3228  1.1  christos       /* Try to turn a 24 immediate, displacement or absolute address
   3229  1.1  christos 	 into a 8 immediate, displacement or absolute address.  */
   3230  1.1  christos       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
   3231  1.1  christos 	{
   3232  1.1  christos 	  bfd_vma value = symval;
   3233  1.1  christos 	  value += irel->r_addend;
   3234  1.1  christos 
   3235  1.1  christos 	  /* See if the value will fit in 8 bits.  */
   3236  1.1  christos 	  if ((long) value < 0x7f && (long) value > -0x80)
   3237  1.1  christos 	    {
   3238  1.1  christos 	      unsigned char code;
   3239  1.1  christos 
   3240  1.1  christos 	      /* AM33 insns which have 24 operands are 6 bytes long and
   3241  1.1  christos 		 will have 0xfd as the first byte.  */
   3242  1.1  christos 
   3243  1.1  christos 	      /* Get the first opcode.  */
   3244  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
   3245  1.1  christos 
   3246  1.1  christos 	      if (code == 0xfd)
   3247  1.1  christos 		{
   3248  1.1  christos 		  /* Get the second opcode.  */
   3249  1.1  christos 		  code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
   3250  1.1  christos 
   3251  1.1  christos 		  /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
   3252  1.1  christos 		     equivalent instructions exists.  */
   3253  1.1  christos 		  if (code != 0x6b && code != 0x7b
   3254  1.1  christos 		      && code != 0x8b && code != 0x9b
   3255  1.1  christos 		      && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
   3256  1.1  christos 			  || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
   3257  1.1  christos 			  || (code & 0x0f) == 0x0e))
   3258  1.1  christos 		    {
   3259  1.1  christos 		      /* Not safe if the high bit is on as relaxing may
   3260  1.1  christos 			 move the value out of high mem and thus not fit
   3261  1.1  christos 			 in a signed 8bit value.  This is currently over
   3262  1.1  christos 			 conservative.  */
   3263  1.1  christos 		      if ((value & 0x80) == 0)
   3264  1.1  christos 			{
   3265  1.1  christos 			  /* Note that we've changed the relocation contents,
   3266  1.1  christos 			     etc.  */
   3267  1.1  christos 			  elf_section_data (sec)->relocs = internal_relocs;
   3268  1.1  christos 			  elf_section_data (sec)->this_hdr.contents = contents;
   3269  1.1  christos 			  symtab_hdr->contents = (unsigned char *) isymbuf;
   3270  1.1  christos 
   3271  1.1  christos 			  /* Fix the opcode.  */
   3272  1.1  christos 			  bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
   3273  1.1  christos 			  bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
   3274  1.1  christos 
   3275  1.1  christos 			  /* Fix the relocation's type.  */
   3276  1.1  christos 			  irel->r_info =
   3277  1.1  christos 			    ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3278  1.1  christos 					  R_MN10300_8);
   3279  1.1  christos 
   3280  1.1  christos 			  /* Delete two bytes of data.  */
   3281  1.1  christos 			  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3282  1.1  christos 							       irel->r_offset + 1, 2))
   3283  1.1  christos 			    goto error_return;
   3284  1.1  christos 
   3285  1.1  christos 			  /* That will change things, so, we should relax
   3286  1.1  christos 			     again.  Note that this is not required, and it
   3287  1.1  christos 			     may be slow.  */
   3288  1.1  christos 			  *again = TRUE;
   3289  1.1  christos 			  break;
   3290  1.1  christos 			}
   3291  1.1  christos 		    }
   3292  1.1  christos 		}
   3293  1.1  christos 	    }
   3294  1.1  christos 	}
   3295  1.1  christos 
   3296  1.1  christos       /* Try to turn a 32bit immediate, displacement or absolute address
   3297  1.1  christos 	 into a 16bit immediate, displacement or absolute address.  */
   3298  1.1  christos       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
   3299  1.1  christos 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
   3300  1.1  christos 	  || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
   3301  1.1  christos 	{
   3302  1.1  christos 	  bfd_vma value = symval;
   3303  1.1  christos 
   3304  1.1  christos 	  if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
   3305  1.1  christos 	    {
   3306  1.1  christos 	      asection * sgot;
   3307  1.1  christos 
   3308  1.1  christos 	      sgot = bfd_get_section_by_name (elf_hash_table (link_info)
   3309  1.1  christos 					      ->dynobj, ".got");
   3310  1.1  christos 
   3311  1.1  christos 	      if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
   3312  1.1  christos 		{
   3313  1.1  christos 		  value = sgot->output_offset;
   3314  1.1  christos 
   3315  1.1  christos 		  if (h)
   3316  1.1  christos 		    value += h->root.got.offset;
   3317  1.1  christos 		  else
   3318  1.1  christos 		    value += (elf_local_got_offsets
   3319  1.1  christos 			      (abfd)[ELF32_R_SYM (irel->r_info)]);
   3320  1.1  christos 		}
   3321  1.1  christos 	      else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
   3322  1.1  christos 		value -= sgot->output_section->vma;
   3323  1.1  christos 	      else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
   3324  1.1  christos 		value = (sgot->output_section->vma
   3325  1.1  christos 			 - (sec->output_section->vma
   3326  1.1  christos 			    + sec->output_offset
   3327  1.1  christos 			    + irel->r_offset));
   3328  1.1  christos 	      else
   3329  1.1  christos 		abort ();
   3330  1.1  christos 	    }
   3331  1.1  christos 
   3332  1.1  christos 	  value += irel->r_addend;
   3333  1.1  christos 
   3334  1.1  christos 	  /* See if the value will fit in 24 bits.
   3335  1.1  christos 	     We allow any 16bit match here.  We prune those we can't
   3336  1.1  christos 	     handle below.  */
   3337  1.1  christos 	  if ((long) value < 0x7fffff && (long) value > -0x800000)
   3338  1.1  christos 	    {
   3339  1.1  christos 	      unsigned char code;
   3340  1.1  christos 
   3341  1.1  christos 	      /* AM33 insns which have 32bit operands are 7 bytes long and
   3342  1.1  christos 		 will have 0xfe as the first byte.  */
   3343  1.1  christos 
   3344  1.1  christos 	      /* Get the first opcode.  */
   3345  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
   3346  1.1  christos 
   3347  1.1  christos 	      if (code == 0xfe)
   3348  1.1  christos 		{
   3349  1.1  christos 		  /* Get the second opcode.  */
   3350  1.1  christos 		  code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
   3351  1.1  christos 
   3352  1.1  christos 		  /* All the am33 32 -> 24 relaxing possibilities.  */
   3353  1.1  christos 		  /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
   3354  1.1  christos 		     equivalent instructions exists.  */
   3355  1.1  christos 		  if (code != 0x6b && code != 0x7b
   3356  1.1  christos 		      && code != 0x8b && code != 0x9b
   3357  1.1  christos 		      && (ELF32_R_TYPE (irel->r_info)
   3358  1.1  christos 			  != (int) R_MN10300_GOTPC32)
   3359  1.1  christos 		      && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
   3360  1.1  christos 			  || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
   3361  1.1  christos 			  || (code & 0x0f) == 0x0e))
   3362  1.1  christos 		    {
   3363  1.1  christos 		      /* Not safe if the high bit is on as relaxing may
   3364  1.1  christos 			 move the value out of high mem and thus not fit
   3365  1.1  christos 			 in a signed 16bit value.  This is currently over
   3366  1.1  christos 			 conservative.  */
   3367  1.1  christos 		      if ((value & 0x8000) == 0)
   3368  1.1  christos 			{
   3369  1.1  christos 			  /* Note that we've changed the relocation contents,
   3370  1.1  christos 			     etc.  */
   3371  1.1  christos 			  elf_section_data (sec)->relocs = internal_relocs;
   3372  1.1  christos 			  elf_section_data (sec)->this_hdr.contents = contents;
   3373  1.1  christos 			  symtab_hdr->contents = (unsigned char *) isymbuf;
   3374  1.1  christos 
   3375  1.1  christos 			  /* Fix the opcode.  */
   3376  1.1  christos 			  bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
   3377  1.1  christos 			  bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
   3378  1.1  christos 
   3379  1.1  christos 			  /* Fix the relocation's type.  */
   3380  1.1  christos 			  irel->r_info =
   3381  1.1  christos 			    ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3382  1.1  christos 					  (ELF32_R_TYPE (irel->r_info)
   3383  1.1  christos 					   == (int) R_MN10300_GOTOFF32)
   3384  1.1  christos 					  ? R_MN10300_GOTOFF24
   3385  1.1  christos 					  : (ELF32_R_TYPE (irel->r_info)
   3386  1.1  christos 					     == (int) R_MN10300_GOT32)
   3387  1.1  christos 					  ? R_MN10300_GOT24 :
   3388  1.1  christos 					  R_MN10300_24);
   3389  1.1  christos 
   3390  1.1  christos 			  /* Delete one byte of data.  */
   3391  1.1  christos 			  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3392  1.1  christos 							       irel->r_offset + 3, 1))
   3393  1.1  christos 			    goto error_return;
   3394  1.1  christos 
   3395  1.1  christos 			  /* That will change things, so, we should relax
   3396  1.1  christos 			     again.  Note that this is not required, and it
   3397  1.1  christos 			     may be slow.  */
   3398  1.1  christos 			  *again = TRUE;
   3399  1.1  christos 			  break;
   3400  1.1  christos 			}
   3401  1.1  christos 		    }
   3402  1.1  christos 		}
   3403  1.1  christos 	    }
   3404  1.1  christos 
   3405  1.1  christos 	  /* See if the value will fit in 16 bits.
   3406  1.1  christos 	     We allow any 16bit match here.  We prune those we can't
   3407  1.1  christos 	     handle below.  */
   3408  1.1  christos 	  if ((long) value < 0x7fff && (long) value > -0x8000)
   3409  1.1  christos 	    {
   3410  1.1  christos 	      unsigned char code;
   3411  1.1  christos 
   3412  1.1  christos 	      /* Most insns which have 32bit operands are 6 bytes long;
   3413  1.1  christos 		 exceptions are pcrel insns and bit insns.
   3414  1.1  christos 
   3415  1.1  christos 		 We handle pcrel insns above.  We don't bother trying
   3416  1.1  christos 		 to handle the bit insns here.
   3417  1.1  christos 
   3418  1.1  christos 		 The first byte of the remaining insns will be 0xfc.  */
   3419  1.1  christos 
   3420  1.1  christos 	      /* Get the first opcode.  */
   3421  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
   3422  1.1  christos 
   3423  1.1  christos 	      if (code != 0xfc)
   3424  1.1  christos 		continue;
   3425  1.1  christos 
   3426  1.1  christos 	      /* Get the second opcode.  */
   3427  1.1  christos 	      code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
   3428  1.1  christos 
   3429  1.1  christos 	      if ((code & 0xf0) < 0x80)
   3430  1.1  christos 		switch (code & 0xf0)
   3431  1.1  christos 		  {
   3432  1.1  christos 		  /* mov (d32,am),dn   -> mov (d32,am),dn
   3433  1.1  christos 		     mov dm,(d32,am)   -> mov dn,(d32,am)
   3434  1.1  christos 		     mov (d32,am),an   -> mov (d32,am),an
   3435  1.1  christos 		     mov dm,(d32,am)   -> mov dn,(d32,am)
   3436  1.1  christos 		     movbu (d32,am),dn -> movbu (d32,am),dn
   3437  1.1  christos 		     movbu dm,(d32,am) -> movbu dn,(d32,am)
   3438  1.1  christos 		     movhu (d32,am),dn -> movhu (d32,am),dn
   3439  1.1  christos 		     movhu dm,(d32,am) -> movhu dn,(d32,am) */
   3440  1.1  christos 		  case 0x00:
   3441  1.1  christos 		  case 0x10:
   3442  1.1  christos 		  case 0x20:
   3443  1.1  christos 		  case 0x30:
   3444  1.1  christos 		  case 0x40:
   3445  1.1  christos 		  case 0x50:
   3446  1.1  christos 		  case 0x60:
   3447  1.1  christos 		  case 0x70:
   3448  1.1  christos 		    /* Not safe if the high bit is on as relaxing may
   3449  1.1  christos 		       move the value out of high mem and thus not fit
   3450  1.1  christos 		       in a signed 16bit value.  */
   3451  1.1  christos 		    if (code == 0xcc
   3452  1.1  christos 			&& (value & 0x8000))
   3453  1.1  christos 		      continue;
   3454  1.1  christos 
   3455  1.1  christos 		    /* Note that we've changed the relocation contents, etc.  */
   3456  1.1  christos 		    elf_section_data (sec)->relocs = internal_relocs;
   3457  1.1  christos 		    elf_section_data (sec)->this_hdr.contents = contents;
   3458  1.1  christos 		    symtab_hdr->contents = (unsigned char *) isymbuf;
   3459  1.1  christos 
   3460  1.1  christos 		    /* Fix the opcode.  */
   3461  1.1  christos 		    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
   3462  1.1  christos 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
   3463  1.1  christos 
   3464  1.1  christos 		    /* Fix the relocation's type.  */
   3465  1.1  christos 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3466  1.1  christos 						 (ELF32_R_TYPE (irel->r_info)
   3467  1.1  christos 						  == (int) R_MN10300_GOTOFF32)
   3468  1.1  christos 						 ? R_MN10300_GOTOFF16
   3469  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3470  1.1  christos 						    == (int) R_MN10300_GOT32)
   3471  1.1  christos 						 ? R_MN10300_GOT16
   3472  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3473  1.1  christos 						    == (int) R_MN10300_GOTPC32)
   3474  1.1  christos 						 ? R_MN10300_GOTPC16 :
   3475  1.1  christos 						 R_MN10300_16);
   3476  1.1  christos 
   3477  1.1  christos 		    /* Delete two bytes of data.  */
   3478  1.1  christos 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3479  1.1  christos 							 irel->r_offset + 2, 2))
   3480  1.1  christos 		      goto error_return;
   3481  1.1  christos 
   3482  1.1  christos 		    /* That will change things, so, we should relax again.
   3483  1.1  christos 		       Note that this is not required, and it may be slow.  */
   3484  1.1  christos 		    *again = TRUE;
   3485  1.1  christos 		    break;
   3486  1.1  christos 		  }
   3487  1.1  christos 	      else if ((code & 0xf0) == 0x80
   3488  1.1  christos 		       || (code & 0xf0) == 0x90)
   3489  1.1  christos 		switch (code & 0xf3)
   3490  1.1  christos 		  {
   3491  1.1  christos 		  /* mov dn,(abs32)   -> mov dn,(abs16)
   3492  1.1  christos 		     movbu dn,(abs32) -> movbu dn,(abs16)
   3493  1.1  christos 		     movhu dn,(abs32) -> movhu dn,(abs16)  */
   3494  1.1  christos 		  case 0x81:
   3495  1.1  christos 		  case 0x82:
   3496  1.1  christos 		  case 0x83:
   3497  1.1  christos 		    /* Note that we've changed the relocation contents, etc.  */
   3498  1.1  christos 		    elf_section_data (sec)->relocs = internal_relocs;
   3499  1.1  christos 		    elf_section_data (sec)->this_hdr.contents = contents;
   3500  1.1  christos 		    symtab_hdr->contents = (unsigned char *) isymbuf;
   3501  1.1  christos 
   3502  1.1  christos 		    if ((code & 0xf3) == 0x81)
   3503  1.1  christos 		      code = 0x01 + (code & 0x0c);
   3504  1.1  christos 		    else if ((code & 0xf3) == 0x82)
   3505  1.1  christos 		      code = 0x02 + (code & 0x0c);
   3506  1.1  christos 		    else if ((code & 0xf3) == 0x83)
   3507  1.1  christos 		      code = 0x03 + (code & 0x0c);
   3508  1.1  christos 		    else
   3509  1.1  christos 		      abort ();
   3510  1.1  christos 
   3511  1.1  christos 		    /* Fix the opcode.  */
   3512  1.1  christos 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
   3513  1.1  christos 
   3514  1.1  christos 		    /* Fix the relocation's type.  */
   3515  1.1  christos 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3516  1.1  christos 						 (ELF32_R_TYPE (irel->r_info)
   3517  1.1  christos 						  == (int) R_MN10300_GOTOFF32)
   3518  1.1  christos 						 ? R_MN10300_GOTOFF16
   3519  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3520  1.1  christos 						    == (int) R_MN10300_GOT32)
   3521  1.1  christos 						 ? R_MN10300_GOT16
   3522  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3523  1.1  christos 						    == (int) R_MN10300_GOTPC32)
   3524  1.1  christos 						 ? R_MN10300_GOTPC16 :
   3525  1.1  christos 						 R_MN10300_16);
   3526  1.1  christos 
   3527  1.1  christos 		    /* The opcode got shorter too, so we have to fix the
   3528  1.1  christos 		       addend and offset too!  */
   3529  1.1  christos 		    irel->r_offset -= 1;
   3530  1.1  christos 
   3531  1.1  christos 		    /* Delete three bytes of data.  */
   3532  1.1  christos 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3533  1.1  christos 							 irel->r_offset + 1, 3))
   3534  1.1  christos 		      goto error_return;
   3535  1.1  christos 
   3536  1.1  christos 		    /* That will change things, so, we should relax again.
   3537  1.1  christos 		       Note that this is not required, and it may be slow.  */
   3538  1.1  christos 		    *again = TRUE;
   3539  1.1  christos 		    break;
   3540  1.1  christos 
   3541  1.1  christos 		  /* mov am,(abs32)    -> mov am,(abs16)
   3542  1.1  christos 		     mov am,(d32,sp)   -> mov am,(d16,sp)
   3543  1.1  christos 		     mov dm,(d32,sp)   -> mov dm,(d32,sp)
   3544  1.1  christos 		     movbu dm,(d32,sp) -> movbu dm,(d32,sp)
   3545  1.1  christos 		     movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
   3546  1.1  christos 		  case 0x80:
   3547  1.1  christos 		  case 0x90:
   3548  1.1  christos 		  case 0x91:
   3549  1.1  christos 		  case 0x92:
   3550  1.1  christos 		  case 0x93:
   3551  1.1  christos 		    /* sp-based offsets are zero-extended.  */
   3552  1.1  christos 		    if (code >= 0x90 && code <= 0x93
   3553  1.1  christos 			&& (long) value < 0)
   3554  1.1  christos 		      continue;
   3555  1.1  christos 
   3556  1.1  christos 		    /* Note that we've changed the relocation contents, etc.  */
   3557  1.1  christos 		    elf_section_data (sec)->relocs = internal_relocs;
   3558  1.1  christos 		    elf_section_data (sec)->this_hdr.contents = contents;
   3559  1.1  christos 		    symtab_hdr->contents = (unsigned char *) isymbuf;
   3560  1.1  christos 
   3561  1.1  christos 		    /* Fix the opcode.  */
   3562  1.1  christos 		    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
   3563  1.1  christos 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
   3564  1.1  christos 
   3565  1.1  christos 		    /* Fix the relocation's type.  */
   3566  1.1  christos 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3567  1.1  christos 						 (ELF32_R_TYPE (irel->r_info)
   3568  1.1  christos 						  == (int) R_MN10300_GOTOFF32)
   3569  1.1  christos 						 ? R_MN10300_GOTOFF16
   3570  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3571  1.1  christos 						    == (int) R_MN10300_GOT32)
   3572  1.1  christos 						 ? R_MN10300_GOT16
   3573  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3574  1.1  christos 						    == (int) R_MN10300_GOTPC32)
   3575  1.1  christos 						 ? R_MN10300_GOTPC16 :
   3576  1.1  christos 						 R_MN10300_16);
   3577  1.1  christos 
   3578  1.1  christos 		    /* Delete two bytes of data.  */
   3579  1.1  christos 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3580  1.1  christos 							 irel->r_offset + 2, 2))
   3581  1.1  christos 		      goto error_return;
   3582  1.1  christos 
   3583  1.1  christos 		    /* That will change things, so, we should relax again.
   3584  1.1  christos 		       Note that this is not required, and it may be slow.  */
   3585  1.1  christos 		    *again = TRUE;
   3586  1.1  christos 		    break;
   3587  1.1  christos 		  }
   3588  1.1  christos 	      else if ((code & 0xf0) < 0xf0)
   3589  1.1  christos 		switch (code & 0xfc)
   3590  1.1  christos 		  {
   3591  1.1  christos 		  /* mov imm32,dn     -> mov imm16,dn
   3592  1.1  christos 		     mov imm32,an     -> mov imm16,an
   3593  1.1  christos 		     mov (abs32),dn   -> mov (abs16),dn
   3594  1.1  christos 		     movbu (abs32),dn -> movbu (abs16),dn
   3595  1.1  christos 		     movhu (abs32),dn -> movhu (abs16),dn  */
   3596  1.1  christos 		  case 0xcc:
   3597  1.1  christos 		  case 0xdc:
   3598  1.1  christos 		  case 0xa4:
   3599  1.1  christos 		  case 0xa8:
   3600  1.1  christos 		  case 0xac:
   3601  1.1  christos 		    /* Not safe if the high bit is on as relaxing may
   3602  1.1  christos 		       move the value out of high mem and thus not fit
   3603  1.1  christos 		       in a signed 16bit value.  */
   3604  1.1  christos 		    if (code == 0xcc
   3605  1.1  christos 			&& (value & 0x8000))
   3606  1.1  christos 		      continue;
   3607  1.1  christos 
   3608  1.1  christos 		    /* mov imm16, an zero-extends the immediate.  */
   3609  1.1  christos 		    if (code == 0xdc
   3610  1.1  christos 			&& (long) value < 0)
   3611  1.1  christos 		      continue;
   3612  1.1  christos 
   3613  1.1  christos 		    /* Note that we've changed the relocation contents, etc.  */
   3614  1.1  christos 		    elf_section_data (sec)->relocs = internal_relocs;
   3615  1.1  christos 		    elf_section_data (sec)->this_hdr.contents = contents;
   3616  1.1  christos 		    symtab_hdr->contents = (unsigned char *) isymbuf;
   3617  1.1  christos 
   3618  1.1  christos 		    if ((code & 0xfc) == 0xcc)
   3619  1.1  christos 		      code = 0x2c + (code & 0x03);
   3620  1.1  christos 		    else if ((code & 0xfc) == 0xdc)
   3621  1.1  christos 		      code = 0x24 + (code & 0x03);
   3622  1.1  christos 		    else if ((code & 0xfc) == 0xa4)
   3623  1.1  christos 		      code = 0x30 + (code & 0x03);
   3624  1.1  christos 		    else if ((code & 0xfc) == 0xa8)
   3625  1.1  christos 		      code = 0x34 + (code & 0x03);
   3626  1.1  christos 		    else if ((code & 0xfc) == 0xac)
   3627  1.1  christos 		      code = 0x38 + (code & 0x03);
   3628  1.1  christos 		    else
   3629  1.1  christos 		      abort ();
   3630  1.1  christos 
   3631  1.1  christos 		    /* Fix the opcode.  */
   3632  1.1  christos 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
   3633  1.1  christos 
   3634  1.1  christos 		    /* Fix the relocation's type.  */
   3635  1.1  christos 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3636  1.1  christos 						 (ELF32_R_TYPE (irel->r_info)
   3637  1.1  christos 						  == (int) R_MN10300_GOTOFF32)
   3638  1.1  christos 						 ? R_MN10300_GOTOFF16
   3639  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3640  1.1  christos 						    == (int) R_MN10300_GOT32)
   3641  1.1  christos 						 ? R_MN10300_GOT16
   3642  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3643  1.1  christos 						    == (int) R_MN10300_GOTPC32)
   3644  1.1  christos 						 ? R_MN10300_GOTPC16 :
   3645  1.1  christos 						 R_MN10300_16);
   3646  1.1  christos 
   3647  1.1  christos 		    /* The opcode got shorter too, so we have to fix the
   3648  1.1  christos 		       addend and offset too!  */
   3649  1.1  christos 		    irel->r_offset -= 1;
   3650  1.1  christos 
   3651  1.1  christos 		    /* Delete three bytes of data.  */
   3652  1.1  christos 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3653  1.1  christos 							 irel->r_offset + 1, 3))
   3654  1.1  christos 		      goto error_return;
   3655  1.1  christos 
   3656  1.1  christos 		    /* That will change things, so, we should relax again.
   3657  1.1  christos 		       Note that this is not required, and it may be slow.  */
   3658  1.1  christos 		    *again = TRUE;
   3659  1.1  christos 		    break;
   3660  1.1  christos 
   3661  1.1  christos 		  /* mov (abs32),an    -> mov (abs16),an
   3662  1.1  christos 		     mov (d32,sp),an   -> mov (d16,sp),an
   3663  1.1  christos 		     mov (d32,sp),dn   -> mov (d16,sp),dn
   3664  1.1  christos 		     movbu (d32,sp),dn -> movbu (d16,sp),dn
   3665  1.1  christos 		     movhu (d32,sp),dn -> movhu (d16,sp),dn
   3666  1.1  christos 		     add imm32,dn      -> add imm16,dn
   3667  1.1  christos 		     cmp imm32,dn      -> cmp imm16,dn
   3668  1.1  christos 		     add imm32,an      -> add imm16,an
   3669  1.1  christos 		     cmp imm32,an      -> cmp imm16,an
   3670  1.1  christos 		     and imm32,dn      -> and imm16,dn
   3671  1.1  christos 		     or imm32,dn       -> or imm16,dn
   3672  1.1  christos 		     xor imm32,dn      -> xor imm16,dn
   3673  1.1  christos 		     btst imm32,dn     -> btst imm16,dn */
   3674  1.1  christos 
   3675  1.1  christos 		  case 0xa0:
   3676  1.1  christos 		  case 0xb0:
   3677  1.1  christos 		  case 0xb1:
   3678  1.1  christos 		  case 0xb2:
   3679  1.1  christos 		  case 0xb3:
   3680  1.1  christos 		  case 0xc0:
   3681  1.1  christos 		  case 0xc8:
   3682  1.1  christos 
   3683  1.1  christos 		  case 0xd0:
   3684  1.1  christos 		  case 0xd8:
   3685  1.1  christos 		  case 0xe0:
   3686  1.1  christos 		  case 0xe1:
   3687  1.1  christos 		  case 0xe2:
   3688  1.1  christos 		  case 0xe3:
   3689  1.1  christos 		    /* cmp imm16, an zero-extends the immediate.  */
   3690  1.1  christos 		    if (code == 0xdc
   3691  1.1  christos 			&& (long) value < 0)
   3692  1.1  christos 		      continue;
   3693  1.1  christos 
   3694  1.1  christos 		    /* So do sp-based offsets.  */
   3695  1.1  christos 		    if (code >= 0xb0 && code <= 0xb3
   3696  1.1  christos 			&& (long) value < 0)
   3697  1.1  christos 		      continue;
   3698  1.1  christos 
   3699  1.1  christos 		    /* Note that we've changed the relocation contents, etc.  */
   3700  1.1  christos 		    elf_section_data (sec)->relocs = internal_relocs;
   3701  1.1  christos 		    elf_section_data (sec)->this_hdr.contents = contents;
   3702  1.1  christos 		    symtab_hdr->contents = (unsigned char *) isymbuf;
   3703  1.1  christos 
   3704  1.1  christos 		    /* Fix the opcode.  */
   3705  1.1  christos 		    bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
   3706  1.1  christos 		    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
   3707  1.1  christos 
   3708  1.1  christos 		    /* Fix the relocation's type.  */
   3709  1.1  christos 		    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3710  1.1  christos 						 (ELF32_R_TYPE (irel->r_info)
   3711  1.1  christos 						  == (int) R_MN10300_GOTOFF32)
   3712  1.1  christos 						 ? R_MN10300_GOTOFF16
   3713  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3714  1.1  christos 						    == (int) R_MN10300_GOT32)
   3715  1.1  christos 						 ? R_MN10300_GOT16
   3716  1.1  christos 						 : (ELF32_R_TYPE (irel->r_info)
   3717  1.1  christos 						    == (int) R_MN10300_GOTPC32)
   3718  1.1  christos 						 ? R_MN10300_GOTPC16 :
   3719  1.1  christos 						 R_MN10300_16);
   3720  1.1  christos 
   3721  1.1  christos 		    /* Delete two bytes of data.  */
   3722  1.1  christos 		    if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3723  1.1  christos 							 irel->r_offset + 2, 2))
   3724  1.1  christos 		      goto error_return;
   3725  1.1  christos 
   3726  1.1  christos 		    /* That will change things, so, we should relax again.
   3727  1.1  christos 		       Note that this is not required, and it may be slow.  */
   3728  1.1  christos 		    *again = TRUE;
   3729  1.1  christos 		    break;
   3730  1.1  christos 		  }
   3731  1.1  christos 	      else if (code == 0xfe)
   3732  1.1  christos 		{
   3733  1.1  christos 		  /* add imm32,sp -> add imm16,sp  */
   3734  1.1  christos 
   3735  1.1  christos 		  /* Note that we've changed the relocation contents, etc.  */
   3736  1.1  christos 		  elf_section_data (sec)->relocs = internal_relocs;
   3737  1.1  christos 		  elf_section_data (sec)->this_hdr.contents = contents;
   3738  1.1  christos 		  symtab_hdr->contents = (unsigned char *) isymbuf;
   3739  1.1  christos 
   3740  1.1  christos 		  /* Fix the opcode.  */
   3741  1.1  christos 		  bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
   3742  1.1  christos 		  bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
   3743  1.1  christos 
   3744  1.1  christos 		  /* Fix the relocation's type.  */
   3745  1.1  christos 		  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   3746  1.1  christos 					       (ELF32_R_TYPE (irel->r_info)
   3747  1.1  christos 						== (int) R_MN10300_GOT32)
   3748  1.1  christos 					       ? R_MN10300_GOT16
   3749  1.1  christos 					       : (ELF32_R_TYPE (irel->r_info)
   3750  1.1  christos 						  == (int) R_MN10300_GOTOFF32)
   3751  1.1  christos 					       ? R_MN10300_GOTOFF16
   3752  1.1  christos 					       : (ELF32_R_TYPE (irel->r_info)
   3753  1.1  christos 						  == (int) R_MN10300_GOTPC32)
   3754  1.1  christos 					       ? R_MN10300_GOTPC16 :
   3755  1.1  christos 					       R_MN10300_16);
   3756  1.1  christos 
   3757  1.1  christos 		  /* Delete two bytes of data.  */
   3758  1.1  christos 		  if (!mn10300_elf_relax_delete_bytes (abfd, sec,
   3759  1.1  christos 						       irel->r_offset + 2, 2))
   3760  1.1  christos 		    goto error_return;
   3761  1.1  christos 
   3762  1.1  christos 		  /* That will change things, so, we should relax again.
   3763  1.1  christos 		     Note that this is not required, and it may be slow.  */
   3764  1.1  christos 		  *again = TRUE;
   3765  1.1  christos 		  break;
   3766  1.1  christos 		}
   3767  1.1  christos 	    }
   3768  1.1  christos 	}
   3769  1.1  christos     }
   3770  1.1  christos 
   3771  1.1  christos   if (isymbuf != NULL
   3772  1.1  christos       && symtab_hdr->contents != (unsigned char *) isymbuf)
   3773  1.1  christos     {
   3774  1.1  christos       if (! link_info->keep_memory)
   3775  1.1  christos 	free (isymbuf);
   3776  1.1  christos       else
   3777  1.1  christos 	{
   3778  1.1  christos 	  /* Cache the symbols for elf_link_input_bfd.  */
   3779  1.1  christos 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   3780  1.1  christos 	}
   3781  1.1  christos     }
   3782  1.1  christos 
   3783  1.1  christos   if (contents != NULL
   3784  1.1  christos       && elf_section_data (sec)->this_hdr.contents != contents)
   3785  1.1  christos     {
   3786  1.1  christos       if (! link_info->keep_memory)
   3787  1.1  christos 	free (contents);
   3788  1.1  christos       else
   3789  1.1  christos 	{
   3790  1.1  christos 	  /* Cache the section contents for elf_link_input_bfd.  */
   3791  1.1  christos 	  elf_section_data (sec)->this_hdr.contents = contents;
   3792  1.1  christos 	}
   3793  1.1  christos     }
   3794  1.1  christos 
   3795  1.1  christos   if (internal_relocs != NULL
   3796  1.1  christos       && elf_section_data (sec)->relocs != internal_relocs)
   3797  1.1  christos     free (internal_relocs);
   3798  1.1  christos 
   3799  1.1  christos   return TRUE;
   3800  1.1  christos 
   3801  1.1  christos  error_return:
   3802  1.1  christos   if (isymbuf != NULL
   3803  1.1  christos       && symtab_hdr->contents != (unsigned char *) isymbuf)
   3804  1.1  christos     free (isymbuf);
   3805  1.1  christos   if (contents != NULL
   3806  1.1  christos       && elf_section_data (section)->this_hdr.contents != contents)
   3807  1.1  christos     free (contents);
   3808  1.1  christos   if (internal_relocs != NULL
   3809  1.1  christos       && elf_section_data (section)->relocs != internal_relocs)
   3810  1.1  christos     free (internal_relocs);
   3811  1.1  christos 
   3812  1.1  christos   return FALSE;
   3813  1.1  christos }
   3814  1.1  christos 
   3815  1.1  christos /* This is a version of bfd_generic_get_relocated_section_contents
   3816  1.1  christos    which uses mn10300_elf_relocate_section.  */
   3817  1.1  christos 
   3818  1.1  christos static bfd_byte *
   3819  1.1  christos mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
   3820  1.1  christos 					    struct bfd_link_info *link_info,
   3821  1.1  christos 					    struct bfd_link_order *link_order,
   3822  1.1  christos 					    bfd_byte *data,
   3823  1.1  christos 					    bfd_boolean relocatable,
   3824  1.1  christos 					    asymbol **symbols)
   3825  1.1  christos {
   3826  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   3827  1.1  christos   asection *input_section = link_order->u.indirect.section;
   3828  1.1  christos   bfd *input_bfd = input_section->owner;
   3829  1.1  christos   asection **sections = NULL;
   3830  1.1  christos   Elf_Internal_Rela *internal_relocs = NULL;
   3831  1.1  christos   Elf_Internal_Sym *isymbuf = NULL;
   3832  1.1  christos 
   3833  1.1  christos   /* We only need to handle the case of relaxing, or of having a
   3834  1.1  christos      particular set of section contents, specially.  */
   3835  1.1  christos   if (relocatable
   3836  1.1  christos       || elf_section_data (input_section)->this_hdr.contents == NULL)
   3837  1.1  christos     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
   3838  1.1  christos 						       link_order, data,
   3839  1.1  christos 						       relocatable,
   3840  1.1  christos 						       symbols);
   3841  1.1  christos 
   3842  1.1  christos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   3843  1.1  christos 
   3844  1.1  christos   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
   3845  1.1  christos 	  (size_t) input_section->size);
   3846  1.1  christos 
   3847  1.1  christos   if ((input_section->flags & SEC_RELOC) != 0
   3848  1.1  christos       && input_section->reloc_count > 0)
   3849  1.1  christos     {
   3850  1.1  christos       asection **secpp;
   3851  1.1  christos       Elf_Internal_Sym *isym, *isymend;
   3852  1.1  christos       bfd_size_type amt;
   3853  1.1  christos 
   3854  1.1  christos       internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
   3855  1.1  christos 						   NULL, NULL, FALSE);
   3856  1.1  christos       if (internal_relocs == NULL)
   3857  1.1  christos 	goto error_return;
   3858  1.1  christos 
   3859  1.1  christos       if (symtab_hdr->sh_info != 0)
   3860  1.1  christos 	{
   3861  1.1  christos 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   3862  1.1  christos 	  if (isymbuf == NULL)
   3863  1.1  christos 	    isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   3864  1.1  christos 					    symtab_hdr->sh_info, 0,
   3865  1.1  christos 					    NULL, NULL, NULL);
   3866  1.1  christos 	  if (isymbuf == NULL)
   3867  1.1  christos 	    goto error_return;
   3868  1.1  christos 	}
   3869  1.1  christos 
   3870  1.1  christos       amt = symtab_hdr->sh_info;
   3871  1.1  christos       amt *= sizeof (asection *);
   3872  1.1  christos       sections = bfd_malloc (amt);
   3873  1.1  christos       if (sections == NULL && amt != 0)
   3874  1.1  christos 	goto error_return;
   3875  1.1  christos 
   3876  1.1  christos       isymend = isymbuf + symtab_hdr->sh_info;
   3877  1.1  christos       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
   3878  1.1  christos 	{
   3879  1.1  christos 	  asection *isec;
   3880  1.1  christos 
   3881  1.1  christos 	  if (isym->st_shndx == SHN_UNDEF)
   3882  1.1  christos 	    isec = bfd_und_section_ptr;
   3883  1.1  christos 	  else if (isym->st_shndx == SHN_ABS)
   3884  1.1  christos 	    isec = bfd_abs_section_ptr;
   3885  1.1  christos 	  else if (isym->st_shndx == SHN_COMMON)
   3886  1.1  christos 	    isec = bfd_com_section_ptr;
   3887  1.1  christos 	  else
   3888  1.1  christos 	    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   3889  1.1  christos 
   3890  1.1  christos 	  *secpp = isec;
   3891  1.1  christos 	}
   3892  1.1  christos 
   3893  1.1  christos       if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
   3894  1.1  christos 					  input_section, data, internal_relocs,
   3895  1.1  christos 					  isymbuf, sections))
   3896  1.1  christos 	goto error_return;
   3897  1.1  christos 
   3898  1.1  christos       if (sections != NULL)
   3899  1.1  christos 	free (sections);
   3900  1.1  christos       if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
   3901  1.1  christos 	free (isymbuf);
   3902  1.1  christos       if (internal_relocs != elf_section_data (input_section)->relocs)
   3903  1.1  christos 	free (internal_relocs);
   3904  1.1  christos     }
   3905  1.1  christos 
   3906  1.1  christos   return data;
   3907  1.1  christos 
   3908  1.1  christos  error_return:
   3909  1.1  christos   if (sections != NULL)
   3910  1.1  christos     free (sections);
   3911  1.1  christos   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
   3912  1.1  christos     free (isymbuf);
   3913  1.1  christos   if (internal_relocs != NULL
   3914  1.1  christos       && internal_relocs != elf_section_data (input_section)->relocs)
   3915  1.1  christos     free (internal_relocs);
   3916  1.1  christos   return NULL;
   3917  1.1  christos }
   3918  1.1  christos 
   3919  1.1  christos /* Assorted hash table functions.  */
   3920  1.1  christos 
   3921  1.1  christos /* Initialize an entry in the link hash table.  */
   3922  1.1  christos 
   3923  1.1  christos /* Create an entry in an MN10300 ELF linker hash table.  */
   3924  1.1  christos 
   3925  1.1  christos static struct bfd_hash_entry *
   3926  1.1  christos elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
   3927  1.1  christos 				 struct bfd_hash_table *table,
   3928  1.1  christos 				 const char *string)
   3929  1.1  christos {
   3930  1.1  christos   struct elf32_mn10300_link_hash_entry *ret =
   3931  1.1  christos     (struct elf32_mn10300_link_hash_entry *) entry;
   3932  1.1  christos 
   3933  1.1  christos   /* Allocate the structure if it has not already been allocated by a
   3934  1.1  christos      subclass.  */
   3935  1.1  christos   if (ret == NULL)
   3936  1.1  christos     ret = (struct elf32_mn10300_link_hash_entry *)
   3937  1.1  christos 	   bfd_hash_allocate (table, sizeof (* ret));
   3938  1.1  christos   if (ret == NULL)
   3939  1.1  christos     return (struct bfd_hash_entry *) ret;
   3940  1.1  christos 
   3941  1.1  christos   /* Call the allocation method of the superclass.  */
   3942  1.1  christos   ret = (struct elf32_mn10300_link_hash_entry *)
   3943  1.1  christos 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   3944  1.1  christos 				     table, string);
   3945  1.1  christos   if (ret != NULL)
   3946  1.1  christos     {
   3947  1.1  christos       ret->direct_calls = 0;
   3948  1.1  christos       ret->stack_size = 0;
   3949  1.1  christos       ret->movm_args = 0;
   3950  1.1  christos       ret->movm_stack_size = 0;
   3951  1.1  christos       ret->flags = 0;
   3952  1.1  christos       ret->value = 0;
   3953  1.1  christos     }
   3954  1.1  christos 
   3955  1.1  christos   return (struct bfd_hash_entry *) ret;
   3956  1.1  christos }
   3957  1.1  christos 
   3958  1.1  christos /* Create an mn10300 ELF linker hash table.  */
   3959  1.1  christos 
   3960  1.1  christos static struct bfd_link_hash_table *
   3961  1.1  christos elf32_mn10300_link_hash_table_create (bfd *abfd)
   3962  1.1  christos {
   3963  1.1  christos   struct elf32_mn10300_link_hash_table *ret;
   3964  1.1  christos   bfd_size_type amt = sizeof (* ret);
   3965  1.1  christos 
   3966  1.1  christos   ret = bfd_malloc (amt);
   3967  1.1  christos   if (ret == NULL)
   3968  1.1  christos     return NULL;
   3969  1.1  christos 
   3970  1.1  christos   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
   3971  1.1  christos 				      elf32_mn10300_link_hash_newfunc,
   3972  1.1  christos 				      sizeof (struct elf32_mn10300_link_hash_entry),
   3973  1.1  christos 				      MN10300_ELF_DATA))
   3974  1.1  christos     {
   3975  1.1  christos       free (ret);
   3976  1.1  christos       return NULL;
   3977  1.1  christos     }
   3978  1.1  christos 
   3979  1.1  christos   ret->flags = 0;
   3980  1.1  christos   amt = sizeof (struct elf_link_hash_table);
   3981  1.1  christos   ret->static_hash_table = bfd_malloc (amt);
   3982  1.1  christos   if (ret->static_hash_table == NULL)
   3983  1.1  christos     {
   3984  1.1  christos       free (ret);
   3985  1.1  christos       return NULL;
   3986  1.1  christos     }
   3987  1.1  christos 
   3988  1.1  christos   if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
   3989  1.1  christos 				      elf32_mn10300_link_hash_newfunc,
   3990  1.1  christos 				      sizeof (struct elf32_mn10300_link_hash_entry),
   3991  1.1  christos 				      MN10300_ELF_DATA))
   3992  1.1  christos     {
   3993  1.1  christos       free (ret->static_hash_table);
   3994  1.1  christos       free (ret);
   3995  1.1  christos       return NULL;
   3996  1.1  christos     }
   3997  1.1  christos   return & ret->root.root;
   3998  1.1  christos }
   3999  1.1  christos 
   4000  1.1  christos /* Free an mn10300 ELF linker hash table.  */
   4001  1.1  christos 
   4002  1.1  christos static void
   4003  1.1  christos elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table *hash)
   4004  1.1  christos {
   4005  1.1  christos   struct elf32_mn10300_link_hash_table *ret
   4006  1.1  christos     = (struct elf32_mn10300_link_hash_table *) hash;
   4007  1.1  christos 
   4008  1.1  christos   _bfd_generic_link_hash_table_free
   4009  1.1  christos     ((struct bfd_link_hash_table *) ret->static_hash_table);
   4010  1.1  christos   _bfd_generic_link_hash_table_free
   4011  1.1  christos     ((struct bfd_link_hash_table *) ret);
   4012  1.1  christos }
   4013  1.1  christos 
   4014  1.1  christos static unsigned long
   4015  1.1  christos elf_mn10300_mach (flagword flags)
   4016  1.1  christos {
   4017  1.1  christos   switch (flags & EF_MN10300_MACH)
   4018  1.1  christos     {
   4019  1.1  christos     case E_MN10300_MACH_MN10300:
   4020  1.1  christos     default:
   4021  1.1  christos       return bfd_mach_mn10300;
   4022  1.1  christos 
   4023  1.1  christos     case E_MN10300_MACH_AM33:
   4024  1.1  christos       return bfd_mach_am33;
   4025  1.1  christos 
   4026  1.1  christos     case E_MN10300_MACH_AM33_2:
   4027  1.1  christos       return bfd_mach_am33_2;
   4028  1.1  christos     }
   4029  1.1  christos }
   4030  1.1  christos 
   4031  1.1  christos /* The final processing done just before writing out a MN10300 ELF object
   4032  1.1  christos    file.  This gets the MN10300 architecture right based on the machine
   4033  1.1  christos    number.  */
   4034  1.1  christos 
   4035  1.1  christos static void
   4036  1.1  christos _bfd_mn10300_elf_final_write_processing (bfd *abfd,
   4037  1.1  christos 					 bfd_boolean linker ATTRIBUTE_UNUSED)
   4038  1.1  christos {
   4039  1.1  christos   unsigned long val;
   4040  1.1  christos 
   4041  1.1  christos   switch (bfd_get_mach (abfd))
   4042  1.1  christos     {
   4043  1.1  christos     default:
   4044  1.1  christos     case bfd_mach_mn10300:
   4045  1.1  christos       val = E_MN10300_MACH_MN10300;
   4046  1.1  christos       break;
   4047  1.1  christos 
   4048  1.1  christos     case bfd_mach_am33:
   4049  1.1  christos       val = E_MN10300_MACH_AM33;
   4050  1.1  christos       break;
   4051  1.1  christos 
   4052  1.1  christos     case bfd_mach_am33_2:
   4053  1.1  christos       val = E_MN10300_MACH_AM33_2;
   4054  1.1  christos       break;
   4055  1.1  christos     }
   4056  1.1  christos 
   4057  1.1  christos   elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
   4058  1.1  christos   elf_elfheader (abfd)->e_flags |= val;
   4059  1.1  christos }
   4060  1.1  christos 
   4061  1.1  christos static bfd_boolean
   4062  1.1  christos _bfd_mn10300_elf_object_p (bfd *abfd)
   4063  1.1  christos {
   4064  1.1  christos   bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
   4065  1.1  christos 			     elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
   4066  1.1  christos   return TRUE;
   4067  1.1  christos }
   4068  1.1  christos 
   4069  1.1  christos /* Merge backend specific data from an object file to the output
   4070  1.1  christos    object file when linking.  */
   4071  1.1  christos 
   4072  1.1  christos static bfd_boolean
   4073  1.1  christos _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   4074  1.1  christos {
   4075  1.1  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   4076  1.1  christos       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   4077  1.1  christos     return TRUE;
   4078  1.1  christos 
   4079  1.1  christos   if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   4080  1.1  christos       && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
   4081  1.1  christos     {
   4082  1.1  christos       if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   4083  1.1  christos 			       bfd_get_mach (ibfd)))
   4084  1.1  christos 	return FALSE;
   4085  1.1  christos     }
   4086  1.1  christos 
   4087  1.1  christos   return TRUE;
   4088  1.1  christos }
   4089  1.1  christos 
   4090  1.1  christos #define PLT0_ENTRY_SIZE     15
   4091  1.1  christos #define PLT_ENTRY_SIZE      20
   4092  1.1  christos #define PIC_PLT_ENTRY_SIZE  24
   4093  1.1  christos 
   4094  1.1  christos static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
   4095  1.1  christos {
   4096  1.1  christos   0xfc, 0xa0, 0, 0, 0, 0,	/* mov	(.got+8),a0 */
   4097  1.1  christos   0xfe, 0xe, 0x10, 0, 0, 0, 0,	/* mov	(.got+4),r1 */
   4098  1.1  christos   0xf0, 0xf4,			/* jmp	(a0) */
   4099  1.1  christos };
   4100  1.1  christos 
   4101  1.1  christos static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
   4102  1.1  christos {
   4103  1.1  christos   0xfc, 0xa0, 0, 0, 0, 0,	/* mov	(nameN@GOT + .got),a0 */
   4104  1.1  christos   0xf0, 0xf4,			/* jmp	(a0) */
   4105  1.1  christos   0xfe, 8, 0, 0, 0, 0, 0,	/* mov	reloc-table-address,r0 */
   4106  1.1  christos   0xdc, 0, 0, 0, 0,		/* jmp	.plt0 */
   4107  1.1  christos };
   4108  1.1  christos 
   4109  1.1  christos static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
   4110  1.1  christos {
   4111  1.1  christos   0xfc, 0x22, 0, 0, 0, 0,	/* mov	(nameN@GOT,a2),a0 */
   4112  1.1  christos   0xf0, 0xf4,			/* jmp	(a0) */
   4113  1.1  christos   0xfe, 8, 0, 0, 0, 0, 0,	/* mov	reloc-table-address,r0 */
   4114  1.1  christos   0xf8, 0x22, 8,		/* mov	(8,a2),a0 */
   4115  1.1  christos   0xfb, 0xa, 0x1a, 4,		/* mov	(4,a2),r1 */
   4116  1.1  christos   0xf0, 0xf4,			/* jmp	(a0) */
   4117  1.1  christos };
   4118  1.1  christos 
   4119  1.1  christos /* Return size of the first PLT entry.  */
   4120  1.1  christos #define elf_mn10300_sizeof_plt0(info) \
   4121  1.1  christos   (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
   4122  1.1  christos 
   4123  1.1  christos /* Return size of a PLT entry.  */
   4124  1.1  christos #define elf_mn10300_sizeof_plt(info) \
   4125  1.1  christos   (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
   4126  1.1  christos 
   4127  1.1  christos /* Return offset of the PLT0 address in an absolute PLT entry.  */
   4128  1.1  christos #define elf_mn10300_plt_plt0_offset(info) 16
   4129  1.1  christos 
   4130  1.1  christos /* Return offset of the linker in PLT0 entry.  */
   4131  1.1  christos #define elf_mn10300_plt0_linker_offset(info) 2
   4132  1.1  christos 
   4133  1.1  christos /* Return offset of the GOT id in PLT0 entry.  */
   4134  1.1  christos #define elf_mn10300_plt0_gotid_offset(info) 9
   4135  1.1  christos 
   4136  1.1  christos /* Return offset of the temporary in PLT entry.  */
   4137  1.1  christos #define elf_mn10300_plt_temp_offset(info) 8
   4138  1.1  christos 
   4139  1.1  christos /* Return offset of the symbol in PLT entry.  */
   4140  1.1  christos #define elf_mn10300_plt_symbol_offset(info) 2
   4141  1.1  christos 
   4142  1.1  christos /* Return offset of the relocation in PLT entry.  */
   4143  1.1  christos #define elf_mn10300_plt_reloc_offset(info) 11
   4144  1.1  christos 
   4145  1.1  christos /* The name of the dynamic interpreter.  This is put in the .interp
   4146  1.1  christos    section.  */
   4147  1.1  christos 
   4148  1.1  christos #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
   4149  1.1  christos 
   4150  1.1  christos /* Create dynamic sections when linking against a dynamic object.  */
   4151  1.1  christos 
   4152  1.1  christos static bfd_boolean
   4153  1.1  christos _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   4154  1.1  christos {
   4155  1.1  christos   flagword   flags;
   4156  1.1  christos   asection * s;
   4157  1.1  christos   const struct elf_backend_data * bed = get_elf_backend_data (abfd);
   4158  1.1  christos   int ptralign = 0;
   4159  1.1  christos 
   4160  1.1  christos   switch (bed->s->arch_size)
   4161  1.1  christos     {
   4162  1.1  christos     case 32:
   4163  1.1  christos       ptralign = 2;
   4164  1.1  christos       break;
   4165  1.1  christos 
   4166  1.1  christos     case 64:
   4167  1.1  christos       ptralign = 3;
   4168  1.1  christos       break;
   4169  1.1  christos 
   4170  1.1  christos     default:
   4171  1.1  christos       bfd_set_error (bfd_error_bad_value);
   4172  1.1  christos       return FALSE;
   4173  1.1  christos     }
   4174  1.1  christos 
   4175  1.1  christos   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
   4176  1.1  christos      .rel[a].bss sections.  */
   4177  1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4178  1.1  christos 	   | SEC_LINKER_CREATED);
   4179  1.1  christos 
   4180  1.1  christos   s = bfd_make_section_with_flags (abfd,
   4181  1.1  christos 				   (bed->default_use_rela_p
   4182  1.1  christos 				    ? ".rela.plt" : ".rel.plt"),
   4183  1.1  christos 				   flags | SEC_READONLY);
   4184  1.1  christos   if (s == NULL
   4185  1.1  christos       || ! bfd_set_section_alignment (abfd, s, ptralign))
   4186  1.1  christos     return FALSE;
   4187  1.1  christos 
   4188  1.1  christos   if (! _bfd_mn10300_elf_create_got_section (abfd, info))
   4189  1.1  christos     return FALSE;
   4190  1.1  christos 
   4191  1.1  christos   {
   4192  1.1  christos     const char * secname;
   4193  1.1  christos     char *       relname;
   4194  1.1  christos     flagword     secflags;
   4195  1.1  christos     asection *   sec;
   4196  1.1  christos 
   4197  1.1  christos     for (sec = abfd->sections; sec; sec = sec->next)
   4198  1.1  christos       {
   4199  1.1  christos 	secflags = bfd_get_section_flags (abfd, sec);
   4200  1.1  christos 	if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
   4201  1.1  christos 	    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
   4202  1.1  christos 	  continue;
   4203  1.1  christos 
   4204  1.1  christos 	secname = bfd_get_section_name (abfd, sec);
   4205  1.1  christos 	relname = bfd_malloc (strlen (secname) + 6);
   4206  1.1  christos 	strcpy (relname, ".rela");
   4207  1.1  christos 	strcat (relname, secname);
   4208  1.1  christos 
   4209  1.1  christos 	s = bfd_make_section_with_flags (abfd, relname,
   4210  1.1  christos 					 flags | SEC_READONLY);
   4211  1.1  christos 	if (s == NULL
   4212  1.1  christos 	    || ! bfd_set_section_alignment (abfd, s, ptralign))
   4213  1.1  christos 	  return FALSE;
   4214  1.1  christos       }
   4215  1.1  christos   }
   4216  1.1  christos 
   4217  1.1  christos   if (bed->want_dynbss)
   4218  1.1  christos     {
   4219  1.1  christos       /* The .dynbss section is a place to put symbols which are defined
   4220  1.1  christos 	 by dynamic objects, are referenced by regular objects, and are
   4221  1.1  christos 	 not functions.  We must allocate space for them in the process
   4222  1.1  christos 	 image and use a R_*_COPY reloc to tell the dynamic linker to
   4223  1.1  christos 	 initialize them at run time.  The linker script puts the .dynbss
   4224  1.1  christos 	 section into the .bss section of the final image.  */
   4225  1.1  christos       s = bfd_make_section_with_flags (abfd, ".dynbss",
   4226  1.1  christos 				       SEC_ALLOC | SEC_LINKER_CREATED);
   4227  1.1  christos       if (s == NULL)
   4228  1.1  christos 	return FALSE;
   4229  1.1  christos 
   4230  1.1  christos       /* The .rel[a].bss section holds copy relocs.  This section is not
   4231  1.1  christos 	 normally needed.  We need to create it here, though, so that the
   4232  1.1  christos 	 linker will map it to an output section.  We can't just create it
   4233  1.1  christos 	 only if we need it, because we will not know whether we need it
   4234  1.1  christos 	 until we have seen all the input files, and the first time the
   4235  1.1  christos 	 main linker code calls BFD after examining all the input files
   4236  1.1  christos 	 (size_dynamic_sections) the input sections have already been
   4237  1.1  christos 	 mapped to the output sections.  If the section turns out not to
   4238  1.1  christos 	 be needed, we can discard it later.  We will never need this
   4239  1.1  christos 	 section when generating a shared object, since they do not use
   4240  1.1  christos 	 copy relocs.  */
   4241  1.1  christos       if (! info->shared)
   4242  1.1  christos 	{
   4243  1.1  christos 	  s = bfd_make_section_with_flags (abfd,
   4244  1.1  christos 					   (bed->default_use_rela_p
   4245  1.1  christos 					    ? ".rela.bss" : ".rel.bss"),
   4246  1.1  christos 					   flags | SEC_READONLY);
   4247  1.1  christos 	  if (s == NULL
   4248  1.1  christos 	      || ! bfd_set_section_alignment (abfd, s, ptralign))
   4249  1.1  christos 	    return FALSE;
   4250  1.1  christos 	}
   4251  1.1  christos     }
   4252  1.1  christos 
   4253  1.1  christos   return TRUE;
   4254  1.1  christos }
   4255  1.1  christos 
   4256  1.1  christos /* Adjust a symbol defined by a dynamic object and referenced by a
   4258  1.1  christos    regular object.  The current definition is in some section of the
   4259  1.1  christos    dynamic object, but we're not including those sections.  We have to
   4260  1.1  christos    change the definition to something the rest of the link can
   4261  1.1  christos    understand.  */
   4262  1.1  christos 
   4263  1.1  christos static bfd_boolean
   4264  1.1  christos _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
   4265  1.1  christos 					struct elf_link_hash_entry * h)
   4266  1.1  christos {
   4267  1.1  christos   bfd * dynobj;
   4268  1.1  christos   asection * s;
   4269  1.1  christos 
   4270  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   4271  1.1  christos 
   4272  1.1  christos   /* Make sure we know what is going on here.  */
   4273  1.1  christos   BFD_ASSERT (dynobj != NULL
   4274  1.1  christos 	      && (h->needs_plt
   4275  1.1  christos 		  || h->u.weakdef != NULL
   4276  1.1  christos 		  || (h->def_dynamic
   4277  1.1  christos 		      && h->ref_regular
   4278  1.1  christos 		      && !h->def_regular)));
   4279  1.1  christos 
   4280  1.1  christos   /* If this is a function, put it in the procedure linkage table.  We
   4281  1.1  christos      will fill in the contents of the procedure linkage table later,
   4282  1.1  christos      when we know the address of the .got section.  */
   4283  1.1  christos   if (h->type == STT_FUNC
   4284  1.1  christos       || h->needs_plt)
   4285  1.1  christos     {
   4286  1.1  christos       if (! info->shared
   4287  1.1  christos 	  && !h->def_dynamic
   4288  1.1  christos 	  && !h->ref_dynamic)
   4289  1.1  christos 	{
   4290  1.1  christos 	  /* This case can occur if we saw a PLT reloc in an input
   4291  1.1  christos 	     file, but the symbol was never referred to by a dynamic
   4292  1.1  christos 	     object.  In such a case, we don't actually need to build
   4293  1.1  christos 	     a procedure linkage table, and we can just do a REL32
   4294  1.1  christos 	     reloc instead.  */
   4295  1.1  christos 	  BFD_ASSERT (h->needs_plt);
   4296  1.1  christos 	  return TRUE;
   4297  1.1  christos 	}
   4298  1.1  christos 
   4299  1.1  christos       /* Make sure this symbol is output as a dynamic symbol.  */
   4300  1.1  christos       if (h->dynindx == -1)
   4301  1.1  christos 	{
   4302  1.1  christos 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   4303  1.1  christos 	    return FALSE;
   4304  1.1  christos 	}
   4305  1.1  christos 
   4306  1.1  christos       s = bfd_get_section_by_name (dynobj, ".plt");
   4307  1.1  christos       BFD_ASSERT (s != NULL);
   4308  1.1  christos 
   4309  1.1  christos       /* If this is the first .plt entry, make room for the special
   4310  1.1  christos 	 first entry.  */
   4311  1.1  christos       if (s->size == 0)
   4312  1.1  christos 	s->size += elf_mn10300_sizeof_plt0 (info);
   4313  1.1  christos 
   4314  1.1  christos       /* If this symbol is not defined in a regular file, and we are
   4315  1.1  christos 	 not generating a shared library, then set the symbol to this
   4316  1.1  christos 	 location in the .plt.  This is required to make function
   4317  1.1  christos 	 pointers compare as equal between the normal executable and
   4318  1.1  christos 	 the shared library.  */
   4319  1.1  christos       if (! info->shared
   4320  1.1  christos 	  && !h->def_regular)
   4321  1.1  christos 	{
   4322  1.1  christos 	  h->root.u.def.section = s;
   4323  1.1  christos 	  h->root.u.def.value = s->size;
   4324  1.1  christos 	}
   4325  1.1  christos 
   4326  1.1  christos       h->plt.offset = s->size;
   4327  1.1  christos 
   4328  1.1  christos       /* Make room for this entry.  */
   4329  1.1  christos       s->size += elf_mn10300_sizeof_plt (info);
   4330  1.1  christos 
   4331  1.1  christos       /* We also need to make an entry in the .got.plt section, which
   4332  1.1  christos 	 will be placed in the .got section by the linker script.  */
   4333  1.1  christos       s = bfd_get_section_by_name (dynobj, ".got.plt");
   4334  1.1  christos       BFD_ASSERT (s != NULL);
   4335  1.1  christos       s->size += 4;
   4336  1.1  christos 
   4337  1.1  christos       /* We also need to make an entry in the .rela.plt section.  */
   4338  1.1  christos       s = bfd_get_section_by_name (dynobj, ".rela.plt");
   4339  1.1  christos       BFD_ASSERT (s != NULL);
   4340  1.1  christos       s->size += sizeof (Elf32_External_Rela);
   4341  1.1  christos 
   4342  1.1  christos       return TRUE;
   4343  1.1  christos     }
   4344  1.1  christos 
   4345  1.1  christos   /* If this is a weak symbol, and there is a real definition, the
   4346  1.1  christos      processor independent code will have arranged for us to see the
   4347  1.1  christos      real definition first, and we can just use the same value.  */
   4348  1.1  christos   if (h->u.weakdef != NULL)
   4349  1.1  christos     {
   4350  1.1  christos       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   4351  1.1  christos 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   4352  1.1  christos       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   4353  1.1  christos       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   4354  1.1  christos       return TRUE;
   4355  1.1  christos     }
   4356  1.1  christos 
   4357  1.1  christos   /* This is a reference to a symbol defined by a dynamic object which
   4358  1.1  christos      is not a function.  */
   4359  1.1  christos 
   4360  1.1  christos   /* If we are creating a shared library, we must presume that the
   4361  1.1  christos      only references to the symbol are via the global offset table.
   4362  1.1  christos      For such cases we need not do anything here; the relocations will
   4363  1.1  christos      be handled correctly by relocate_section.  */
   4364  1.1  christos   if (info->shared)
   4365  1.1  christos     return TRUE;
   4366  1.1  christos 
   4367  1.1  christos   /* If there are no references to this symbol that do not use the
   4368  1.1  christos      GOT, we don't need to generate a copy reloc.  */
   4369  1.1  christos   if (!h->non_got_ref)
   4370  1.1  christos     return TRUE;
   4371  1.1  christos 
   4372  1.1  christos   if (h->size == 0)
   4373  1.1  christos     {
   4374  1.1  christos       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
   4375  1.1  christos 			     h->root.root.string);
   4376  1.1  christos       return TRUE;
   4377  1.1  christos     }
   4378  1.1  christos 
   4379  1.1  christos   /* We must allocate the symbol in our .dynbss section, which will
   4380  1.1  christos      become part of the .bss section of the executable.  There will be
   4381  1.1  christos      an entry for this symbol in the .dynsym section.  The dynamic
   4382  1.1  christos      object will contain position independent code, so all references
   4383  1.1  christos      from the dynamic object to this symbol will go through the global
   4384  1.1  christos      offset table.  The dynamic linker will use the .dynsym entry to
   4385  1.1  christos      determine the address it must put in the global offset table, so
   4386  1.1  christos      both the dynamic object and the regular object will refer to the
   4387  1.1  christos      same memory location for the variable.  */
   4388  1.1  christos 
   4389  1.1  christos   s = bfd_get_section_by_name (dynobj, ".dynbss");
   4390  1.1  christos   BFD_ASSERT (s != NULL);
   4391  1.1  christos 
   4392  1.1  christos   /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
   4393  1.1  christos      copy the initial value out of the dynamic object and into the
   4394  1.1  christos      runtime process image.  We need to remember the offset into the
   4395  1.1  christos      .rela.bss section we are going to use.  */
   4396  1.1  christos   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   4397  1.1  christos     {
   4398  1.1  christos       asection * srel;
   4399  1.1  christos 
   4400  1.1  christos       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
   4401  1.1  christos       BFD_ASSERT (srel != NULL);
   4402  1.1  christos       srel->size += sizeof (Elf32_External_Rela);
   4403  1.1  christos       h->needs_copy = 1;
   4404  1.1  christos     }
   4405  1.1  christos 
   4406  1.1  christos   return _bfd_elf_adjust_dynamic_copy (h, s);
   4407  1.1  christos }
   4408  1.1  christos 
   4409  1.1  christos /* Set the sizes of the dynamic sections.  */
   4410  1.1  christos 
   4411  1.1  christos static bfd_boolean
   4412  1.1  christos _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
   4413  1.1  christos 					struct bfd_link_info * info)
   4414  1.1  christos {
   4415  1.1  christos   bfd * dynobj;
   4416  1.1  christos   asection * s;
   4417  1.1  christos   bfd_boolean plt;
   4418  1.1  christos   bfd_boolean relocs;
   4419  1.1  christos   bfd_boolean reltext;
   4420  1.1  christos 
   4421  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   4422  1.1  christos   BFD_ASSERT (dynobj != NULL);
   4423  1.1  christos 
   4424  1.1  christos   if (elf_hash_table (info)->dynamic_sections_created)
   4425  1.1  christos     {
   4426  1.1  christos       /* Set the contents of the .interp section to the interpreter.  */
   4427  1.1  christos       if (info->executable)
   4428  1.1  christos 	{
   4429  1.1  christos 	  s = bfd_get_section_by_name (dynobj, ".interp");
   4430  1.1  christos 	  BFD_ASSERT (s != NULL);
   4431  1.1  christos 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   4432  1.1  christos 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   4433  1.1  christos 	}
   4434  1.1  christos     }
   4435  1.1  christos   else
   4436  1.1  christos     {
   4437  1.1  christos       /* We may have created entries in the .rela.got section.
   4438  1.1  christos 	 However, if we are not creating the dynamic sections, we will
   4439  1.1  christos 	 not actually use these entries.  Reset the size of .rela.got,
   4440  1.1  christos 	 which will cause it to get stripped from the output file
   4441  1.1  christos 	 below.  */
   4442  1.1  christos       s = bfd_get_section_by_name (dynobj, ".rela.got");
   4443  1.1  christos       if (s != NULL)
   4444  1.1  christos 	s->size = 0;
   4445  1.1  christos     }
   4446  1.1  christos 
   4447  1.1  christos   /* The check_relocs and adjust_dynamic_symbol entry points have
   4448  1.1  christos      determined the sizes of the various dynamic sections.  Allocate
   4449  1.1  christos      memory for them.  */
   4450  1.1  christos   plt = FALSE;
   4451  1.1  christos   relocs = FALSE;
   4452  1.1  christos   reltext = FALSE;
   4453  1.1  christos   for (s = dynobj->sections; s != NULL; s = s->next)
   4454  1.1  christos     {
   4455  1.1  christos       const char * name;
   4456  1.1  christos 
   4457  1.1  christos       if ((s->flags & SEC_LINKER_CREATED) == 0)
   4458  1.1  christos 	continue;
   4459  1.1  christos 
   4460  1.1  christos       /* It's OK to base decisions on the section name, because none
   4461  1.1  christos 	 of the dynobj section names depend upon the input files.  */
   4462  1.1  christos       name = bfd_get_section_name (dynobj, s);
   4463  1.1  christos 
   4464  1.1  christos       if (streq (name, ".plt"))
   4465  1.1  christos 	{
   4466  1.1  christos 	  /* Remember whether there is a PLT.  */
   4467  1.1  christos 	  plt = s->size != 0;
   4468  1.1  christos 	}
   4469  1.1  christos       else if (CONST_STRNEQ (name, ".rela"))
   4470  1.1  christos 	{
   4471  1.1  christos 	  if (s->size != 0)
   4472  1.1  christos 	    {
   4473  1.1  christos 	      asection * target;
   4474  1.1  christos 
   4475  1.1  christos 	      /* Remember whether there are any reloc sections other
   4476  1.1  christos 		 than .rela.plt.  */
   4477  1.1  christos 	      if (! streq (name, ".rela.plt"))
   4478  1.1  christos 		{
   4479  1.1  christos 		  const char * outname;
   4480  1.1  christos 
   4481  1.1  christos 		  relocs = TRUE;
   4482  1.1  christos 
   4483  1.1  christos 		  /* If this relocation section applies to a read only
   4484  1.1  christos 		     section, then we probably need a DT_TEXTREL
   4485  1.1  christos 		     entry.  The entries in the .rela.plt section
   4486  1.1  christos 		     really apply to the .got section, which we
   4487  1.1  christos 		     created ourselves and so know is not readonly.  */
   4488  1.1  christos 		  outname = bfd_get_section_name (output_bfd,
   4489  1.1  christos 						  s->output_section);
   4490  1.1  christos 		  target = bfd_get_section_by_name (output_bfd, outname + 5);
   4491  1.1  christos 		  if (target != NULL
   4492  1.1  christos 		      && (target->flags & SEC_READONLY) != 0
   4493  1.1  christos 		      && (target->flags & SEC_ALLOC) != 0)
   4494  1.1  christos 		    reltext = TRUE;
   4495  1.1  christos 		}
   4496  1.1  christos 
   4497  1.1  christos 	      /* We use the reloc_count field as a counter if we need
   4498  1.1  christos 		 to copy relocs into the output file.  */
   4499  1.1  christos 	      s->reloc_count = 0;
   4500  1.1  christos 	    }
   4501  1.1  christos 	}
   4502  1.1  christos       else if (! CONST_STRNEQ (name, ".got")
   4503  1.1  christos 	       && ! streq (name, ".dynbss"))
   4504  1.1  christos 	/* It's not one of our sections, so don't allocate space.  */
   4505  1.1  christos 	continue;
   4506  1.1  christos 
   4507  1.1  christos       if (s->size == 0)
   4508  1.1  christos 	{
   4509  1.1  christos 	  /* If we don't need this section, strip it from the
   4510  1.1  christos 	     output file.  This is mostly to handle .rela.bss and
   4511  1.1  christos 	     .rela.plt.  We must create both sections in
   4512  1.1  christos 	     create_dynamic_sections, because they must be created
   4513  1.1  christos 	     before the linker maps input sections to output
   4514  1.1  christos 	     sections.  The linker does that before
   4515  1.1  christos 	     adjust_dynamic_symbol is called, and it is that
   4516  1.1  christos 	     function which decides whether anything needs to go
   4517  1.1  christos 	     into these sections.  */
   4518  1.1  christos 	  s->flags |= SEC_EXCLUDE;
   4519  1.1  christos 	  continue;
   4520  1.1  christos 	}
   4521  1.1  christos 
   4522  1.1  christos 	if ((s->flags & SEC_HAS_CONTENTS) == 0)
   4523  1.1  christos 	  continue;
   4524  1.1  christos 
   4525  1.1  christos       /* Allocate memory for the section contents.  We use bfd_zalloc
   4526  1.1  christos 	 here in case unused entries are not reclaimed before the
   4527  1.1  christos 	 section's contents are written out.  This should not happen,
   4528  1.1  christos 	 but this way if it does, we get a R_MN10300_NONE reloc
   4529  1.1  christos 	 instead of garbage.  */
   4530  1.1  christos       s->contents = bfd_zalloc (dynobj, s->size);
   4531  1.1  christos       if (s->contents == NULL)
   4532  1.1  christos 	return FALSE;
   4533  1.1  christos     }
   4534  1.1  christos 
   4535  1.1  christos   if (elf_hash_table (info)->dynamic_sections_created)
   4536  1.1  christos     {
   4537  1.1  christos       /* Add some entries to the .dynamic section.  We fill in the
   4538  1.1  christos 	 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
   4539  1.1  christos 	 but we must add the entries now so that we get the correct
   4540  1.1  christos 	 size for the .dynamic section.  The DT_DEBUG entry is filled
   4541  1.1  christos 	 in by the dynamic linker and used by the debugger.  */
   4542  1.1  christos       if (! info->shared)
   4543  1.1  christos 	{
   4544  1.1  christos 	  if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
   4545  1.1  christos 	    return FALSE;
   4546  1.1  christos 	}
   4547  1.1  christos 
   4548  1.1  christos       if (plt)
   4549  1.1  christos 	{
   4550  1.1  christos 	  if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
   4551  1.1  christos 	      || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
   4552  1.1  christos 	      || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
   4553  1.1  christos 	      || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
   4554  1.1  christos 	    return FALSE;
   4555  1.1  christos 	}
   4556  1.1  christos 
   4557  1.1  christos       if (relocs)
   4558  1.1  christos 	{
   4559  1.1  christos 	  if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
   4560  1.1  christos 	      || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
   4561  1.1  christos 	      || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
   4562  1.1  christos 					      sizeof (Elf32_External_Rela)))
   4563  1.1  christos 	    return FALSE;
   4564  1.1  christos 	}
   4565  1.1  christos 
   4566  1.1  christos       if (reltext)
   4567  1.1  christos 	{
   4568  1.1  christos 	  if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
   4569  1.1  christos 	    return FALSE;
   4570  1.1  christos 	}
   4571  1.1  christos     }
   4572  1.1  christos 
   4573  1.1  christos   return TRUE;
   4574  1.1  christos }
   4575  1.1  christos 
   4576  1.1  christos /* Finish up dynamic symbol handling.  We set the contents of various
   4577  1.1  christos    dynamic sections here.  */
   4578  1.1  christos 
   4579  1.1  christos static bfd_boolean
   4580  1.1  christos _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
   4581  1.1  christos 					struct bfd_link_info * info,
   4582  1.1  christos 					struct elf_link_hash_entry * h,
   4583  1.1  christos 					Elf_Internal_Sym * sym)
   4584  1.1  christos {
   4585  1.1  christos   bfd * dynobj;
   4586  1.1  christos 
   4587  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   4588  1.1  christos 
   4589  1.1  christos   if (h->plt.offset != (bfd_vma) -1)
   4590  1.1  christos     {
   4591  1.1  christos       asection *        splt;
   4592  1.1  christos       asection *        sgot;
   4593  1.1  christos       asection *        srel;
   4594  1.1  christos       bfd_vma           plt_index;
   4595  1.1  christos       bfd_vma           got_offset;
   4596  1.1  christos       Elf_Internal_Rela rel;
   4597  1.1  christos 
   4598  1.1  christos       /* This symbol has an entry in the procedure linkage table.  Set
   4599  1.1  christos 	 it up.  */
   4600  1.1  christos 
   4601  1.1  christos       BFD_ASSERT (h->dynindx != -1);
   4602  1.1  christos 
   4603  1.1  christos       splt = bfd_get_section_by_name (dynobj, ".plt");
   4604  1.1  christos       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
   4605  1.1  christos       srel = bfd_get_section_by_name (dynobj, ".rela.plt");
   4606  1.1  christos       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
   4607  1.1  christos 
   4608  1.1  christos       /* Get the index in the procedure linkage table which
   4609  1.1  christos 	 corresponds to this symbol.  This is the index of this symbol
   4610  1.1  christos 	 in all the symbols for which we are making plt entries.  The
   4611  1.1  christos 	 first entry in the procedure linkage table is reserved.  */
   4612  1.1  christos       plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
   4613  1.1  christos 		   / elf_mn10300_sizeof_plt (info));
   4614  1.1  christos 
   4615  1.1  christos       /* Get the offset into the .got table of the entry that
   4616  1.1  christos 	 corresponds to this function.  Each .got entry is 4 bytes.
   4617  1.1  christos 	 The first three are reserved.  */
   4618  1.1  christos       got_offset = (plt_index + 3) * 4;
   4619  1.1  christos 
   4620  1.1  christos       /* Fill in the entry in the procedure linkage table.  */
   4621  1.1  christos       if (! info->shared)
   4622  1.1  christos 	{
   4623  1.1  christos 	  memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
   4624  1.1  christos 		  elf_mn10300_sizeof_plt (info));
   4625  1.1  christos 	  bfd_put_32 (output_bfd,
   4626  1.1  christos 		      (sgot->output_section->vma
   4627  1.1  christos 		       + sgot->output_offset
   4628  1.1  christos 		       + got_offset),
   4629  1.1  christos 		      (splt->contents + h->plt.offset
   4630  1.1  christos 		       + elf_mn10300_plt_symbol_offset (info)));
   4631  1.1  christos 
   4632  1.1  christos 	  bfd_put_32 (output_bfd,
   4633  1.1  christos 		      (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
   4634  1.1  christos 		      (splt->contents + h->plt.offset
   4635  1.1  christos 		       + elf_mn10300_plt_plt0_offset (info)));
   4636  1.1  christos 	}
   4637  1.1  christos       else
   4638  1.1  christos 	{
   4639  1.1  christos 	  memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
   4640  1.1  christos 		  elf_mn10300_sizeof_plt (info));
   4641  1.1  christos 
   4642  1.1  christos 	  bfd_put_32 (output_bfd, got_offset,
   4643  1.1  christos 		      (splt->contents + h->plt.offset
   4644  1.1  christos 		       + elf_mn10300_plt_symbol_offset (info)));
   4645  1.1  christos 	}
   4646  1.1  christos 
   4647  1.1  christos       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
   4648  1.1  christos 		  (splt->contents + h->plt.offset
   4649  1.1  christos 		   + elf_mn10300_plt_reloc_offset (info)));
   4650  1.1  christos 
   4651  1.1  christos       /* Fill in the entry in the global offset table.  */
   4652  1.1  christos       bfd_put_32 (output_bfd,
   4653  1.1  christos 		  (splt->output_section->vma
   4654  1.1  christos 		   + splt->output_offset
   4655  1.1  christos 		   + h->plt.offset
   4656  1.1  christos 		   + elf_mn10300_plt_temp_offset (info)),
   4657  1.1  christos 		  sgot->contents + got_offset);
   4658  1.1  christos 
   4659  1.1  christos       /* Fill in the entry in the .rela.plt section.  */
   4660  1.1  christos       rel.r_offset = (sgot->output_section->vma
   4661  1.1  christos 		      + sgot->output_offset
   4662  1.1  christos 		      + got_offset);
   4663  1.1  christos       rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
   4664  1.1  christos       rel.r_addend = 0;
   4665  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rel,
   4666  1.1  christos 				 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
   4667  1.1  christos 					       + plt_index));
   4668  1.1  christos 
   4669  1.1  christos       if (!h->def_regular)
   4670  1.1  christos 	/* Mark the symbol as undefined, rather than as defined in
   4671  1.1  christos 	   the .plt section.  Leave the value alone.  */
   4672  1.1  christos 	sym->st_shndx = SHN_UNDEF;
   4673  1.1  christos     }
   4674  1.1  christos 
   4675  1.1  christos   if (h->got.offset != (bfd_vma) -1)
   4676  1.1  christos     {
   4677  1.1  christos       asection *        sgot;
   4678  1.1  christos       asection *        srel;
   4679  1.1  christos       Elf_Internal_Rela rel;
   4680  1.1  christos 
   4681  1.1  christos       /* This symbol has an entry in the global offset table.  Set it up.  */
   4682  1.1  christos       sgot = bfd_get_section_by_name (dynobj, ".got");
   4683  1.1  christos       srel = bfd_get_section_by_name (dynobj, ".rela.got");
   4684  1.1  christos       BFD_ASSERT (sgot != NULL && srel != NULL);
   4685  1.1  christos 
   4686  1.1  christos       rel.r_offset = (sgot->output_section->vma
   4687  1.1  christos 		      + sgot->output_offset
   4688  1.1  christos 		      + (h->got.offset & ~1));
   4689  1.1  christos 
   4690  1.1  christos       /* If this is a -Bsymbolic link, and the symbol is defined
   4691  1.1  christos 	 locally, we just want to emit a RELATIVE reloc.  Likewise if
   4692  1.1  christos 	 the symbol was forced to be local because of a version file.
   4693  1.1  christos 	 The entry in the global offset table will already have been
   4694  1.1  christos 	 initialized in the relocate_section function.  */
   4695  1.1  christos       if (info->shared
   4696  1.1  christos 	  && (info->symbolic || h->dynindx == -1)
   4697  1.1  christos 	  && h->def_regular)
   4698  1.1  christos 	{
   4699  1.1  christos 	  rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
   4700  1.1  christos 	  rel.r_addend = (h->root.u.def.value
   4701  1.1  christos 			  + h->root.u.def.section->output_section->vma
   4702  1.1  christos 			  + h->root.u.def.section->output_offset);
   4703  1.1  christos 	}
   4704  1.1  christos       else
   4705  1.1  christos 	{
   4706  1.1  christos 	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
   4707  1.1  christos 	  rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
   4708  1.1  christos 	  rel.r_addend = 0;
   4709  1.1  christos 	}
   4710  1.1  christos 
   4711  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rel,
   4712  1.1  christos 				 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
   4713  1.1  christos 					       + srel->reloc_count));
   4714  1.1  christos       ++ srel->reloc_count;
   4715  1.1  christos     }
   4716  1.1  christos 
   4717  1.1  christos   if (h->needs_copy)
   4718  1.1  christos     {
   4719  1.1  christos       asection *        s;
   4720  1.1  christos       Elf_Internal_Rela rel;
   4721  1.1  christos 
   4722  1.1  christos       /* This symbol needs a copy reloc.  Set it up.  */
   4723  1.1  christos       BFD_ASSERT (h->dynindx != -1
   4724  1.1  christos 		  && (h->root.type == bfd_link_hash_defined
   4725  1.1  christos 		      || h->root.type == bfd_link_hash_defweak));
   4726  1.1  christos 
   4727  1.1  christos       s = bfd_get_section_by_name (h->root.u.def.section->owner,
   4728  1.1  christos 				   ".rela.bss");
   4729  1.1  christos       BFD_ASSERT (s != NULL);
   4730  1.1  christos 
   4731  1.1  christos       rel.r_offset = (h->root.u.def.value
   4732  1.1  christos 		      + h->root.u.def.section->output_section->vma
   4733  1.1  christos 		      + h->root.u.def.section->output_offset);
   4734  1.1  christos       rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
   4735  1.1  christos       rel.r_addend = 0;
   4736  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, & rel,
   4737  1.1  christos 				 (bfd_byte *) ((Elf32_External_Rela *) s->contents
   4738  1.1  christos 					       + s->reloc_count));
   4739  1.1  christos       ++ s->reloc_count;
   4740  1.1  christos     }
   4741  1.1  christos 
   4742  1.1  christos   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
   4743  1.1  christos   if (streq (h->root.root.string, "_DYNAMIC")
   4744  1.1  christos       || h == elf_hash_table (info)->hgot)
   4745  1.1  christos     sym->st_shndx = SHN_ABS;
   4746  1.1  christos 
   4747  1.1  christos   return TRUE;
   4748  1.1  christos }
   4749  1.1  christos 
   4750  1.1  christos /* Finish up the dynamic sections.  */
   4751  1.1  christos 
   4752  1.1  christos static bfd_boolean
   4753  1.1  christos _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
   4754  1.1  christos 					  struct bfd_link_info * info)
   4755  1.1  christos {
   4756  1.1  christos   bfd *      dynobj;
   4757  1.1  christos   asection * sgot;
   4758  1.1  christos   asection * sdyn;
   4759  1.1  christos 
   4760  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   4761  1.1  christos 
   4762  1.1  christos   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
   4763  1.1  christos   BFD_ASSERT (sgot != NULL);
   4764  1.1  christos   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
   4765  1.1  christos 
   4766  1.1  christos   if (elf_hash_table (info)->dynamic_sections_created)
   4767  1.1  christos     {
   4768  1.1  christos       asection *           splt;
   4769  1.1  christos       Elf32_External_Dyn * dyncon;
   4770  1.1  christos       Elf32_External_Dyn * dynconend;
   4771  1.1  christos 
   4772  1.1  christos       BFD_ASSERT (sdyn != NULL);
   4773  1.1  christos 
   4774  1.1  christos       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   4775  1.1  christos       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   4776  1.1  christos 
   4777  1.1  christos       for (; dyncon < dynconend; dyncon++)
   4778  1.1  christos 	{
   4779  1.1  christos 	  Elf_Internal_Dyn dyn;
   4780  1.1  christos 	  const char * name;
   4781  1.1  christos 	  asection * s;
   4782  1.1  christos 
   4783  1.1  christos 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   4784  1.1  christos 
   4785  1.1  christos 	  switch (dyn.d_tag)
   4786  1.1  christos 	    {
   4787  1.1  christos 	    default:
   4788  1.1  christos 	      break;
   4789  1.1  christos 
   4790  1.1  christos 	    case DT_PLTGOT:
   4791  1.1  christos 	      name = ".got";
   4792  1.1  christos 	      goto get_vma;
   4793  1.1  christos 
   4794  1.1  christos 	    case DT_JMPREL:
   4795  1.1  christos 	      name = ".rela.plt";
   4796  1.1  christos 	    get_vma:
   4797  1.1  christos 	      s = bfd_get_section_by_name (output_bfd, name);
   4798  1.1  christos 	      BFD_ASSERT (s != NULL);
   4799  1.1  christos 	      dyn.d_un.d_ptr = s->vma;
   4800  1.1  christos 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   4801  1.1  christos 	      break;
   4802  1.1  christos 
   4803  1.1  christos 	    case DT_PLTRELSZ:
   4804  1.1  christos 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
   4805  1.1  christos 	      BFD_ASSERT (s != NULL);
   4806  1.1  christos 	      dyn.d_un.d_val = s->size;
   4807  1.1  christos 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   4808  1.1  christos 	      break;
   4809  1.1  christos 
   4810  1.1  christos 	    case DT_RELASZ:
   4811  1.1  christos 	      /* My reading of the SVR4 ABI indicates that the
   4812  1.1  christos 		 procedure linkage table relocs (DT_JMPREL) should be
   4813  1.1  christos 		 included in the overall relocs (DT_RELA).  This is
   4814  1.1  christos 		 what Solaris does.  However, UnixWare can not handle
   4815  1.1  christos 		 that case.  Therefore, we override the DT_RELASZ entry
   4816  1.1  christos 		 here to make it not include the JMPREL relocs.  Since
   4817  1.1  christos 		 the linker script arranges for .rela.plt to follow all
   4818  1.1  christos 		 other relocation sections, we don't have to worry
   4819  1.1  christos 		 about changing the DT_RELA entry.  */
   4820  1.1  christos 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
   4821  1.1  christos 	      if (s != NULL)
   4822  1.1  christos 		dyn.d_un.d_val -= s->size;
   4823  1.1  christos 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   4824  1.1  christos 	      break;
   4825  1.1  christos 	    }
   4826  1.1  christos 	}
   4827  1.1  christos 
   4828  1.1  christos       /* Fill in the first entry in the procedure linkage table.  */
   4829  1.1  christos       splt = bfd_get_section_by_name (dynobj, ".plt");
   4830  1.1  christos       if (splt && splt->size > 0)
   4831  1.1  christos 	{
   4832  1.1  christos 	  if (info->shared)
   4833  1.1  christos 	    {
   4834  1.1  christos 	      memcpy (splt->contents, elf_mn10300_pic_plt_entry,
   4835  1.1  christos 		      elf_mn10300_sizeof_plt (info));
   4836  1.1  christos 	    }
   4837  1.1  christos 	  else
   4838  1.1  christos 	    {
   4839  1.1  christos 	      memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
   4840  1.1  christos 	      bfd_put_32 (output_bfd,
   4841  1.1  christos 			  sgot->output_section->vma + sgot->output_offset + 4,
   4842  1.1  christos 			  splt->contents + elf_mn10300_plt0_gotid_offset (info));
   4843  1.1  christos 	      bfd_put_32 (output_bfd,
   4844  1.1  christos 			  sgot->output_section->vma + sgot->output_offset + 8,
   4845  1.1  christos 			  splt->contents + elf_mn10300_plt0_linker_offset (info));
   4846  1.1  christos 	    }
   4847  1.1  christos 
   4848  1.1  christos 	  /* UnixWare sets the entsize of .plt to 4, although that doesn't
   4849  1.1  christos 	     really seem like the right value.  */
   4850  1.1  christos 	  elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
   4851  1.1  christos 	}
   4852  1.1  christos     }
   4853  1.1  christos 
   4854  1.1  christos   /* Fill in the first three entries in the global offset table.  */
   4855  1.1  christos   if (sgot->size > 0)
   4856  1.1  christos     {
   4857  1.1  christos       if (sdyn == NULL)
   4858  1.1  christos 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
   4859  1.1  christos       else
   4860  1.1  christos 	bfd_put_32 (output_bfd,
   4861  1.1  christos 		    sdyn->output_section->vma + sdyn->output_offset,
   4862  1.1  christos 		    sgot->contents);
   4863  1.1  christos       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
   4864  1.1  christos       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
   4865  1.1  christos     }
   4866  1.1  christos 
   4867  1.1  christos   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
   4868  1.1  christos 
   4869  1.1  christos   return TRUE;
   4870  1.1  christos }
   4871  1.1  christos 
   4872  1.1  christos /* Classify relocation types, such that combreloc can sort them
   4873  1.1  christos    properly.  */
   4874  1.1  christos 
   4875  1.1  christos static enum elf_reloc_type_class
   4876  1.1  christos _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
   4877  1.1  christos {
   4878  1.1  christos   switch ((int) ELF32_R_TYPE (rela->r_info))
   4879  1.1  christos     {
   4880  1.1  christos     case R_MN10300_RELATIVE:	return reloc_class_relative;
   4881  1.1  christos     case R_MN10300_JMP_SLOT:	return reloc_class_plt;
   4882  1.1  christos     case R_MN10300_COPY:	return reloc_class_copy;
   4883  1.1  christos     default:			return reloc_class_normal;
   4884  1.1  christos     }
   4885  1.1  christos }
   4886  1.1  christos 
   4887  1.1  christos #ifndef ELF_ARCH
   4888  1.1  christos #define TARGET_LITTLE_SYM	bfd_elf32_mn10300_vec
   4889  1.1  christos #define TARGET_LITTLE_NAME	"elf32-mn10300"
   4890  1.1  christos #define ELF_ARCH		bfd_arch_mn10300
   4891  1.1  christos #define ELF_TARGET_ID		MN10300_ELF_DATA
   4892  1.1  christos #define ELF_MACHINE_CODE	EM_MN10300
   4893  1.1  christos #define ELF_MACHINE_ALT1	EM_CYGNUS_MN10300
   4894  1.1  christos #define ELF_MAXPAGESIZE		0x1000
   4895  1.1  christos #endif
   4896  1.1  christos 
   4897  1.1  christos #define elf_info_to_howto		mn10300_info_to_howto
   4898  1.1  christos #define elf_info_to_howto_rel		0
   4899  1.1  christos #define elf_backend_can_gc_sections	1
   4900  1.1  christos #define elf_backend_rela_normal		1
   4901  1.1  christos #define elf_backend_check_relocs	mn10300_elf_check_relocs
   4902  1.1  christos #define elf_backend_gc_mark_hook	mn10300_elf_gc_mark_hook
   4903  1.1  christos #define elf_backend_relocate_section	mn10300_elf_relocate_section
   4904  1.1  christos #define bfd_elf32_bfd_relax_section	mn10300_elf_relax_section
   4905  1.1  christos #define bfd_elf32_bfd_get_relocated_section_contents \
   4906  1.1  christos 				mn10300_elf_get_relocated_section_contents
   4907  1.1  christos #define bfd_elf32_bfd_link_hash_table_create \
   4908  1.1  christos 				elf32_mn10300_link_hash_table_create
   4909  1.1  christos #define bfd_elf32_bfd_link_hash_table_free \
   4910  1.1  christos 				elf32_mn10300_link_hash_table_free
   4911  1.1  christos 
   4912  1.1  christos #ifndef elf_symbol_leading_char
   4913  1.1  christos #define elf_symbol_leading_char '_'
   4914  1.1  christos #endif
   4915  1.1  christos 
   4916  1.1  christos /* So we can set bits in e_flags.  */
   4917  1.1  christos #define elf_backend_final_write_processing \
   4918  1.1  christos 					_bfd_mn10300_elf_final_write_processing
   4919  1.1  christos #define elf_backend_object_p		_bfd_mn10300_elf_object_p
   4920  1.1  christos 
   4921  1.1  christos #define bfd_elf32_bfd_merge_private_bfd_data \
   4922  1.1  christos 					_bfd_mn10300_elf_merge_private_bfd_data
   4923  1.1  christos 
   4924  1.1  christos #define elf_backend_can_gc_sections	1
   4925  1.1  christos #define elf_backend_create_dynamic_sections \
   4926  1.1  christos   _bfd_mn10300_elf_create_dynamic_sections
   4927  1.1  christos #define elf_backend_adjust_dynamic_symbol \
   4928  1.1  christos   _bfd_mn10300_elf_adjust_dynamic_symbol
   4929  1.1  christos #define elf_backend_size_dynamic_sections \
   4930  1.1  christos   _bfd_mn10300_elf_size_dynamic_sections
   4931  1.1  christos #define elf_backend_omit_section_dynsym \
   4932  1.1  christos   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
   4933  1.1  christos #define elf_backend_finish_dynamic_symbol \
   4934  1.1  christos   _bfd_mn10300_elf_finish_dynamic_symbol
   4935  1.1  christos #define elf_backend_finish_dynamic_sections \
   4936  1.1  christos   _bfd_mn10300_elf_finish_dynamic_sections
   4937  1.1  christos 
   4938  1.1  christos #define elf_backend_reloc_type_class \
   4939  1.1  christos   _bfd_mn10300_elf_reloc_type_class
   4940  1.1  christos 
   4941  1.1  christos #define elf_backend_want_got_plt	1
   4942  1.1  christos #define elf_backend_plt_readonly	1
   4943  1.1  christos #define elf_backend_want_plt_sym	0
   4944  1.1  christos #define elf_backend_got_header_size	12
   4945                
   4946                #include "elf32-target.h"
   4947