Home | History | Annotate | Line # | Download | only in bfd
elf32-lm32.c revision 1.1.1.6
      1      1.1  christos /* Lattice Mico32-specific support for 32-bit ELF
      2  1.1.1.5  christos    Copyright (C) 2008-2018 Free Software Foundation, Inc.
      3      1.1  christos    Contributed by Jon Beniston <jon (at) beniston.com>
      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.1.2  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/lm32.h"
     27      1.1  christos 
     28      1.1  christos #define DEFAULT_STACK_SIZE 0x20000
     29      1.1  christos 
     30      1.1  christos #define PLT_ENTRY_SIZE 20
     31      1.1  christos 
     32      1.1  christos #define PLT0_ENTRY_WORD0  0
     33      1.1  christos #define PLT0_ENTRY_WORD1  0
     34      1.1  christos #define PLT0_ENTRY_WORD2  0
     35      1.1  christos #define PLT0_ENTRY_WORD3  0
     36      1.1  christos #define PLT0_ENTRY_WORD4  0
     37      1.1  christos 
     38      1.1  christos #define PLT0_PIC_ENTRY_WORD0  0
     39      1.1  christos #define PLT0_PIC_ENTRY_WORD1  0
     40      1.1  christos #define PLT0_PIC_ENTRY_WORD2  0
     41      1.1  christos #define PLT0_PIC_ENTRY_WORD3  0
     42      1.1  christos #define PLT0_PIC_ENTRY_WORD4  0
     43      1.1  christos 
     44      1.1  christos #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
     45      1.1  christos 
     46  1.1.1.3  christos extern const bfd_target lm32_elf32_fdpic_vec;
     47      1.1  christos 
     48  1.1.1.3  christos #define IS_FDPIC(bfd) ((bfd)->xvec == &lm32_elf32_fdpic_vec)
     49      1.1  christos 
     50      1.1  christos static bfd_reloc_status_type lm32_elf_gprel_reloc
     51      1.1  christos   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     52      1.1  christos 
     53      1.1  christos /* lm32 ELF linker hash entry.  */
     54      1.1  christos 
     55      1.1  christos struct elf_lm32_link_hash_entry
     56      1.1  christos {
     57      1.1  christos   struct elf_link_hash_entry root;
     58      1.1  christos 
     59      1.1  christos   /* Track dynamic relocs copied for this symbol.  */
     60  1.1.1.5  christos   struct elf_dyn_relocs *dyn_relocs;
     61      1.1  christos };
     62      1.1  christos 
     63      1.1  christos /* lm32 ELF linker hash table.  */
     64      1.1  christos 
     65      1.1  christos struct elf_lm32_link_hash_table
     66      1.1  christos {
     67      1.1  christos   struct elf_link_hash_table root;
     68      1.1  christos 
     69      1.1  christos   /* Short-cuts to get to dynamic linker sections.  */
     70      1.1  christos   asection *sfixup32;
     71      1.1  christos   asection *sdynbss;
     72      1.1  christos   asection *srelbss;
     73      1.1  christos 
     74      1.1  christos   int relocs32;
     75      1.1  christos };
     76      1.1  christos 
     77      1.1  christos /* Get the lm32 ELF linker hash table from a link_info structure.  */
     78      1.1  christos 
     79      1.1  christos #define lm32_elf_hash_table(p) \
     80      1.1  christos   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
     81      1.1  christos   == LM32_ELF_DATA ? ((struct elf_lm32_link_hash_table *) ((p)->hash)) : NULL)
     82      1.1  christos 
     83      1.1  christos #define lm32fdpic_got_section(info) \
     84  1.1.1.5  christos   (lm32_elf_hash_table (info)->root.sgot)
     85      1.1  christos #define lm32fdpic_gotrel_section(info) \
     86  1.1.1.5  christos   (lm32_elf_hash_table (info)->root.srelgot)
     87      1.1  christos #define lm32fdpic_fixup32_section(info) \
     88      1.1  christos   (lm32_elf_hash_table (info)->sfixup32)
     89      1.1  christos 
     90      1.1  christos struct weak_symbol_list
     91      1.1  christos {
     92      1.1  christos   const char *name;
     93      1.1  christos   struct weak_symbol_list *next;
     94      1.1  christos };
     95      1.1  christos 
     96      1.1  christos /* Create an entry in an lm32 ELF linker hash table.  */
     97      1.1  christos 
     98      1.1  christos static struct bfd_hash_entry *
     99      1.1  christos lm32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
    100      1.1  christos 			    struct bfd_hash_table *table,
    101      1.1  christos 			    const char *string)
    102      1.1  christos {
    103      1.1  christos   struct elf_lm32_link_hash_entry *ret =
    104      1.1  christos     (struct elf_lm32_link_hash_entry *) entry;
    105      1.1  christos 
    106      1.1  christos   /* Allocate the structure if it has not already been allocated by a
    107      1.1  christos      subclass.  */
    108      1.1  christos   if (ret == NULL)
    109      1.1  christos     ret = bfd_hash_allocate (table,
    110      1.1  christos 			     sizeof (struct elf_lm32_link_hash_entry));
    111      1.1  christos   if (ret == NULL)
    112      1.1  christos     return NULL;
    113      1.1  christos 
    114      1.1  christos   /* Call the allocation method of the superclass.  */
    115      1.1  christos   ret = ((struct elf_lm32_link_hash_entry *)
    116  1.1.1.5  christos 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
    117  1.1.1.5  christos 				     table, string));
    118      1.1  christos   if (ret != NULL)
    119      1.1  christos     {
    120      1.1  christos       struct elf_lm32_link_hash_entry *eh;
    121      1.1  christos 
    122      1.1  christos       eh = (struct elf_lm32_link_hash_entry *) ret;
    123      1.1  christos       eh->dyn_relocs = NULL;
    124      1.1  christos     }
    125      1.1  christos 
    126      1.1  christos   return (struct bfd_hash_entry *) ret;
    127      1.1  christos }
    128      1.1  christos 
    129      1.1  christos /* Create an lm32 ELF linker hash table.  */
    130      1.1  christos 
    131      1.1  christos static struct bfd_link_hash_table *
    132      1.1  christos lm32_elf_link_hash_table_create (bfd *abfd)
    133      1.1  christos {
    134      1.1  christos   struct elf_lm32_link_hash_table *ret;
    135      1.1  christos   bfd_size_type amt = sizeof (struct elf_lm32_link_hash_table);
    136      1.1  christos 
    137  1.1.1.3  christos   ret = bfd_zmalloc (amt);
    138      1.1  christos   if (ret == NULL)
    139      1.1  christos     return NULL;
    140      1.1  christos 
    141      1.1  christos   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
    142      1.1  christos 				      lm32_elf_link_hash_newfunc,
    143      1.1  christos 				      sizeof (struct elf_lm32_link_hash_entry),
    144      1.1  christos 				      LM32_ELF_DATA))
    145      1.1  christos     {
    146      1.1  christos       free (ret);
    147      1.1  christos       return NULL;
    148      1.1  christos     }
    149      1.1  christos 
    150      1.1  christos   return &ret->root.root;
    151      1.1  christos }
    152      1.1  christos 
    153      1.1  christos /* Add a fixup to the ROFIXUP section.  */
    154      1.1  christos 
    155      1.1  christos static bfd_vma
    156      1.1  christos _lm32fdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma relocation)
    157      1.1  christos {
    158      1.1  christos   bfd_vma fixup_offset;
    159      1.1  christos 
    160      1.1  christos   if (rofixup->flags & SEC_EXCLUDE)
    161      1.1  christos     return -1;
    162      1.1  christos 
    163      1.1  christos   fixup_offset = rofixup->reloc_count * 4;
    164      1.1  christos   if (rofixup->contents)
    165      1.1  christos     {
    166      1.1  christos       BFD_ASSERT (fixup_offset < rofixup->size);
    167      1.1  christos       if (fixup_offset < rofixup->size)
    168      1.1  christos       bfd_put_32 (output_bfd, relocation, rofixup->contents + fixup_offset);
    169      1.1  christos     }
    170      1.1  christos   rofixup->reloc_count++;
    171      1.1  christos 
    172      1.1  christos   return fixup_offset;
    173      1.1  christos }
    174      1.1  christos 
    175      1.1  christos /* Create .rofixup sections in DYNOBJ, and set up
    176      1.1  christos    shortcuts to them in our hash table.  */
    177      1.1  christos 
    178      1.1  christos static bfd_boolean
    179      1.1  christos create_rofixup_section (bfd *dynobj, struct bfd_link_info *info)
    180      1.1  christos {
    181      1.1  christos   struct elf_lm32_link_hash_table *htab;
    182      1.1  christos   htab = lm32_elf_hash_table (info);
    183      1.1  christos 
    184      1.1  christos   if (htab == NULL)
    185      1.1  christos     return FALSE;
    186      1.1  christos 
    187      1.1  christos   /* Fixup section for R_LM32_32 relocs.  */
    188  1.1.1.2  christos   lm32fdpic_fixup32_section (info)
    189  1.1.1.2  christos     = bfd_make_section_anyway_with_flags (dynobj,
    190  1.1.1.2  christos 					  ".rofixup",
    191  1.1.1.2  christos 					  (SEC_ALLOC
    192  1.1.1.2  christos 					   | SEC_LOAD
    193  1.1.1.2  christos 					   | SEC_HAS_CONTENTS
    194  1.1.1.2  christos 					   | SEC_IN_MEMORY
    195  1.1.1.2  christos 					   | SEC_LINKER_CREATED
    196  1.1.1.2  christos 					   | SEC_READONLY));
    197      1.1  christos   if (lm32fdpic_fixup32_section (info) == NULL
    198  1.1.1.2  christos       || ! bfd_set_section_alignment (dynobj,
    199  1.1.1.2  christos 				      lm32fdpic_fixup32_section (info), 2))
    200      1.1  christos     return FALSE;
    201      1.1  christos 
    202      1.1  christos   return TRUE;
    203      1.1  christos }
    204      1.1  christos 
    205      1.1  christos static reloc_howto_type lm32_elf_howto_table [] =
    206      1.1  christos {
    207      1.1  christos   /* This reloc does nothing.  */
    208  1.1.1.5  christos   HOWTO (R_LM32_NONE,		    /* type */
    209  1.1.1.5  christos 	 0,			    /* rightshift */
    210  1.1.1.5  christos 	 3,			    /* size (0 = byte, 1 = short, 2 = long) */
    211  1.1.1.5  christos 	 0,			    /* bitsize */
    212  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    213  1.1.1.5  christos 	 0,			    /* bitpos */
    214  1.1.1.5  christos 	 complain_overflow_dont,    /* complain_on_overflow */
    215  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    216  1.1.1.5  christos 	 "R_LM32_NONE",		    /* name */
    217  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    218  1.1.1.5  christos 	 0,			    /* src_mask */
    219  1.1.1.5  christos 	 0,			    /* dst_mask */
    220  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    221      1.1  christos 
    222      1.1  christos   /* An 8 bit absolute relocation.  */
    223  1.1.1.5  christos   HOWTO (R_LM32_8,		    /* type */
    224  1.1.1.5  christos 	 0,			    /* rightshift */
    225  1.1.1.5  christos 	 0,			    /* size (0 = byte, 1 = short, 2 = long) */
    226  1.1.1.5  christos 	 8,			    /* bitsize */
    227  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    228  1.1.1.5  christos 	 0,			    /* bitpos */
    229  1.1.1.5  christos 	 complain_overflow_bitfield,/* complain_on_overflow */
    230  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    231  1.1.1.5  christos 	 "R_LM32_8",		    /* name */
    232  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    233  1.1.1.5  christos 	 0,			    /* src_mask */
    234  1.1.1.5  christos 	 0xff,			    /* dst_mask */
    235  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    236      1.1  christos 
    237      1.1  christos   /* A 16 bit absolute relocation.  */
    238  1.1.1.5  christos   HOWTO (R_LM32_16,		    /* type */
    239  1.1.1.5  christos 	 0,			    /* rightshift */
    240  1.1.1.5  christos 	 1,			    /* size (0 = byte, 1 = short, 2 = long) */
    241  1.1.1.5  christos 	 16,			    /* bitsize */
    242  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    243  1.1.1.5  christos 	 0,			    /* bitpos */
    244  1.1.1.5  christos 	 complain_overflow_bitfield,/* complain_on_overflow */
    245  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    246  1.1.1.5  christos 	 "R_LM32_16",		    /* name */
    247  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    248  1.1.1.5  christos 	 0,			    /* src_mask */
    249  1.1.1.5  christos 	 0xffff,		    /* dst_mask */
    250  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    251      1.1  christos 
    252      1.1  christos   /* A 32 bit absolute relocation.  */
    253  1.1.1.5  christos   HOWTO (R_LM32_32,		    /* type */
    254  1.1.1.5  christos 	 0,			    /* rightshift */
    255  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    256  1.1.1.5  christos 	 32,			    /* bitsize */
    257  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    258  1.1.1.5  christos 	 0,			    /* bitpos */
    259  1.1.1.5  christos 	 complain_overflow_bitfield,/* complain_on_overflow */
    260  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    261  1.1.1.5  christos 	 "R_LM32_32",		    /* name */
    262  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    263  1.1.1.5  christos 	 0,			    /* src_mask */
    264  1.1.1.5  christos 	 0xffffffff,		    /* dst_mask */
    265  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    266  1.1.1.5  christos 
    267  1.1.1.5  christos   HOWTO (R_LM32_HI16,		    /* type */
    268  1.1.1.5  christos 	 16,			    /* rightshift */
    269  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    270  1.1.1.5  christos 	 16,			    /* bitsize */
    271  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    272  1.1.1.5  christos 	 0,			    /* bitpos */
    273  1.1.1.5  christos 	 complain_overflow_bitfield,/* complain_on_overflow */
    274  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    275  1.1.1.5  christos 	 "R_LM32_HI16",		    /* name */
    276  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    277  1.1.1.5  christos 	 0,			    /* src_mask */
    278  1.1.1.5  christos 	 0xffff,		    /* dst_mask */
    279  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    280  1.1.1.5  christos 
    281  1.1.1.5  christos   HOWTO (R_LM32_LO16,		    /* type */
    282  1.1.1.5  christos 	 0,			    /* rightshift */
    283  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    284  1.1.1.5  christos 	 16,			    /* bitsize */
    285  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    286  1.1.1.5  christos 	 0,			    /* bitpos */
    287  1.1.1.5  christos 	 complain_overflow_dont,    /* complain_on_overflow */
    288  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    289  1.1.1.5  christos 	 "R_LM32_LO16",		    /* name */
    290  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    291  1.1.1.5  christos 	 0,			    /* src_mask */
    292  1.1.1.5  christos 	 0xffff,		    /* dst_mask */
    293  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    294  1.1.1.5  christos 
    295  1.1.1.5  christos   HOWTO (R_LM32_GPREL16,	    /* type */
    296  1.1.1.5  christos 	 0,			    /* rightshift */
    297  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    298  1.1.1.5  christos 	 16,			    /* bitsize */
    299  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    300  1.1.1.5  christos 	 0,			    /* bitpos */
    301  1.1.1.5  christos 	 complain_overflow_dont,    /* complain_on_overflow */
    302  1.1.1.5  christos 	 lm32_elf_gprel_reloc,	    /* special_function */
    303  1.1.1.5  christos 	 "R_LM32_GPREL16",	    /* name */
    304  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    305  1.1.1.5  christos 	 0,			    /* src_mask */
    306  1.1.1.5  christos 	 0xffff,		    /* dst_mask */
    307  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    308  1.1.1.5  christos 
    309  1.1.1.5  christos   HOWTO (R_LM32_CALL,		    /* type */
    310  1.1.1.5  christos 	 2,			    /* rightshift */
    311  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    312  1.1.1.5  christos 	 26,			    /* bitsize */
    313  1.1.1.5  christos 	 TRUE,			    /* pc_relative */
    314  1.1.1.5  christos 	 0,			    /* bitpos */
    315  1.1.1.5  christos 	 complain_overflow_signed,  /* complain_on_overflow */
    316  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    317  1.1.1.5  christos 	 "R_LM32_CALL",		    /* name */
    318  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    319  1.1.1.5  christos 	 0,			    /* src_mask */
    320  1.1.1.5  christos 	 0x3ffffff,		    /* dst_mask */
    321  1.1.1.5  christos 	 TRUE),			    /* pcrel_offset */
    322  1.1.1.5  christos 
    323  1.1.1.5  christos   HOWTO (R_LM32_BRANCH,		    /* type */
    324  1.1.1.5  christos 	 2,			    /* rightshift */
    325  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    326  1.1.1.5  christos 	 16,			    /* bitsize */
    327  1.1.1.5  christos 	 TRUE,			    /* pc_relative */
    328  1.1.1.5  christos 	 0,			    /* bitpos */
    329  1.1.1.5  christos 	 complain_overflow_signed,  /* complain_on_overflow */
    330  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    331  1.1.1.5  christos 	 "R_LM32_BRANCH",	    /* name */
    332  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    333  1.1.1.5  christos 	 0,			    /* src_mask */
    334  1.1.1.5  christos 	 0xffff,		    /* dst_mask */
    335  1.1.1.5  christos 	 TRUE),			    /* pcrel_offset */
    336      1.1  christos 
    337      1.1  christos   /* GNU extension to record C++ vtable hierarchy.  */
    338  1.1.1.5  christos   HOWTO (R_LM32_GNU_VTINHERIT,	    /* type */
    339  1.1.1.5  christos 	 0,			    /* rightshift */
    340  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    341  1.1.1.5  christos 	 0,			    /* bitsize */
    342  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    343  1.1.1.5  christos 	 0,			    /* bitpos */
    344  1.1.1.5  christos 	 complain_overflow_dont,    /* complain_on_overflow */
    345  1.1.1.5  christos 	 NULL,			    /* special_function */
    346  1.1.1.5  christos 	 "R_LM32_GNU_VTINHERIT",    /* name */
    347  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    348  1.1.1.5  christos 	 0,			    /* src_mask */
    349  1.1.1.5  christos 	 0,			    /* dst_mask */
    350  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    351      1.1  christos 
    352      1.1  christos   /* GNU extension to record C++ vtable member usage.  */
    353  1.1.1.5  christos   HOWTO (R_LM32_GNU_VTENTRY,	    /* type */
    354  1.1.1.5  christos 	 0,			    /* rightshift */
    355  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    356  1.1.1.5  christos 	 0,			    /* bitsize */
    357  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    358  1.1.1.5  christos 	 0,			    /* bitpos */
    359  1.1.1.5  christos 	 complain_overflow_dont,    /* complain_on_overflow */
    360  1.1.1.5  christos 	 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
    361  1.1.1.5  christos 	 "R_LM32_GNU_VTENTRY",	    /* name */
    362  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    363  1.1.1.5  christos 	 0,			    /* src_mask */
    364  1.1.1.5  christos 	 0,			    /* dst_mask */
    365  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    366  1.1.1.5  christos 
    367  1.1.1.5  christos   HOWTO (R_LM32_16_GOT,		    /* type */
    368  1.1.1.5  christos 	 0,			    /* rightshift */
    369  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    370  1.1.1.5  christos 	 16,			    /* bitsize */
    371  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    372  1.1.1.5  christos 	 0,			    /* bitpos */
    373  1.1.1.5  christos 	 complain_overflow_signed,  /* complain_on_overflow */
    374  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    375  1.1.1.5  christos 	 "R_LM32_16_GOT",	    /* name */
    376  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    377  1.1.1.5  christos 	 0,			    /* src_mask */
    378  1.1.1.5  christos 	 0xffff,		    /* dst_mask */
    379  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    380  1.1.1.5  christos 
    381  1.1.1.5  christos   HOWTO (R_LM32_GOTOFF_HI16,	    /* type */
    382  1.1.1.5  christos 	 16,			    /* rightshift */
    383  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    384  1.1.1.5  christos 	 16,			    /* bitsize */
    385  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    386  1.1.1.5  christos 	 0,			    /* bitpos */
    387  1.1.1.5  christos 	 complain_overflow_dont,    /* complain_on_overflow */
    388  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    389  1.1.1.5  christos 	 "R_LM32_GOTOFF_HI16",	    /* name */
    390  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    391  1.1.1.5  christos 	 0xffff,		    /* src_mask */
    392  1.1.1.5  christos 	 0xffff,		    /* dst_mask */
    393  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    394  1.1.1.5  christos 
    395  1.1.1.5  christos   HOWTO (R_LM32_GOTOFF_LO16,	    /* type */
    396  1.1.1.5  christos 	 0,			    /* rightshift */
    397  1.1.1.5  christos 	 2,			    /* size (0 = byte, 1 = short, 2 = long) */
    398  1.1.1.5  christos 	 16,			    /* bitsize */
    399  1.1.1.5  christos 	 FALSE,			    /* pc_relative */
    400  1.1.1.5  christos 	 0,			    /* bitpos */
    401  1.1.1.5  christos 	 complain_overflow_dont,    /* complain_on_overflow */
    402  1.1.1.5  christos 	 bfd_elf_generic_reloc,	    /* special_function */
    403  1.1.1.5  christos 	 "R_LM32_GOTOFF_LO16",	    /* name */
    404  1.1.1.5  christos 	 FALSE,			    /* partial_inplace */
    405  1.1.1.5  christos 	 0xffff,		    /* src_mask */
    406  1.1.1.5  christos 	 0xffff,		    /* dst_mask */
    407  1.1.1.5  christos 	 FALSE),		    /* pcrel_offset */
    408      1.1  christos 
    409      1.1  christos   HOWTO (R_LM32_COPY,		/* 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, /* special_function */
    417      1.1  christos 	 "R_LM32_COPY",		/* 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_LM32_GLOB_DAT,	/* 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, /* special_function */
    431      1.1  christos 	 "R_LM32_GLOB_DAT",	/* 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_LM32_JMP_SLOT,	/* 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, /* special_function */
    445      1.1  christos 	 "R_LM32_JMP_SLOT",	/* 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   HOWTO (R_LM32_RELATIVE,	/* type */
    452      1.1  christos 	 0,			/* rightshift */
    453      1.1  christos 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    454      1.1  christos 	 32,			/* bitsize */
    455      1.1  christos 	 FALSE,			/* pc_relative */
    456      1.1  christos 	 0,			/* bitpos */
    457      1.1  christos 	 complain_overflow_bitfield, /* complain_on_overflow */
    458      1.1  christos 	 bfd_elf_generic_reloc, /* special_function */
    459      1.1  christos 	 "R_LM32_RELATIVE",	/* name */
    460      1.1  christos 	 FALSE,			/* partial_inplace */
    461      1.1  christos 	 0xffffffff,		/* src_mask */
    462      1.1  christos 	 0xffffffff,		/* dst_mask */
    463      1.1  christos 	 FALSE),		/* pcrel_offset */
    464      1.1  christos 
    465      1.1  christos };
    466      1.1  christos 
    467      1.1  christos /* Map BFD reloc types to lm32 ELF reloc types. */
    468      1.1  christos 
    469      1.1  christos struct lm32_reloc_map
    470      1.1  christos {
    471      1.1  christos     bfd_reloc_code_real_type bfd_reloc_val;
    472      1.1  christos     unsigned char elf_reloc_val;
    473      1.1  christos };
    474      1.1  christos 
    475      1.1  christos static const struct lm32_reloc_map lm32_reloc_map[] =
    476      1.1  christos {
    477  1.1.1.5  christos   { BFD_RELOC_NONE,		R_LM32_NONE },
    478  1.1.1.5  christos   { BFD_RELOC_8,		R_LM32_8 },
    479  1.1.1.5  christos   { BFD_RELOC_16,		R_LM32_16 },
    480  1.1.1.5  christos   { BFD_RELOC_32,		R_LM32_32 },
    481  1.1.1.5  christos   { BFD_RELOC_HI16,		R_LM32_HI16 },
    482  1.1.1.5  christos   { BFD_RELOC_LO16,		R_LM32_LO16 },
    483  1.1.1.5  christos   { BFD_RELOC_GPREL16,		R_LM32_GPREL16 },
    484  1.1.1.5  christos   { BFD_RELOC_LM32_CALL,	R_LM32_CALL },
    485  1.1.1.5  christos   { BFD_RELOC_LM32_BRANCH,	R_LM32_BRANCH },
    486  1.1.1.5  christos   { BFD_RELOC_VTABLE_INHERIT,	R_LM32_GNU_VTINHERIT },
    487  1.1.1.5  christos   { BFD_RELOC_VTABLE_ENTRY,	R_LM32_GNU_VTENTRY },
    488  1.1.1.5  christos   { BFD_RELOC_LM32_16_GOT,	R_LM32_16_GOT },
    489      1.1  christos   { BFD_RELOC_LM32_GOTOFF_HI16, R_LM32_GOTOFF_HI16 },
    490      1.1  christos   { BFD_RELOC_LM32_GOTOFF_LO16, R_LM32_GOTOFF_LO16 },
    491  1.1.1.5  christos   { BFD_RELOC_LM32_COPY,	R_LM32_COPY },
    492  1.1.1.5  christos   { BFD_RELOC_LM32_GLOB_DAT,	R_LM32_GLOB_DAT },
    493  1.1.1.5  christos   { BFD_RELOC_LM32_JMP_SLOT,	R_LM32_JMP_SLOT },
    494  1.1.1.5  christos   { BFD_RELOC_LM32_RELATIVE,	R_LM32_RELATIVE },
    495      1.1  christos };
    496      1.1  christos 
    497      1.1  christos static reloc_howto_type *
    498      1.1  christos lm32_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    499  1.1.1.5  christos 			bfd_reloc_code_real_type code)
    500      1.1  christos {
    501      1.1  christos   unsigned int i;
    502      1.1  christos 
    503      1.1  christos   for (i = 0; i < sizeof (lm32_reloc_map) / sizeof (lm32_reloc_map[0]); i++)
    504      1.1  christos     if (lm32_reloc_map[i].bfd_reloc_val == code)
    505      1.1  christos       return &lm32_elf_howto_table[lm32_reloc_map[i].elf_reloc_val];
    506      1.1  christos   return NULL;
    507      1.1  christos }
    508      1.1  christos 
    509      1.1  christos static reloc_howto_type *
    510      1.1  christos lm32_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    511      1.1  christos 			const char *r_name)
    512      1.1  christos {
    513      1.1  christos   unsigned int i;
    514      1.1  christos 
    515      1.1  christos   for (i = 0;
    516      1.1  christos        i < sizeof (lm32_elf_howto_table) / sizeof (lm32_elf_howto_table[0]);
    517      1.1  christos        i++)
    518      1.1  christos     if (lm32_elf_howto_table[i].name != NULL
    519      1.1  christos 	&& strcasecmp (lm32_elf_howto_table[i].name, r_name) == 0)
    520      1.1  christos       return &lm32_elf_howto_table[i];
    521      1.1  christos 
    522      1.1  christos   return NULL;
    523      1.1  christos }
    524      1.1  christos 
    525      1.1  christos 
    526      1.1  christos /* Set the howto pointer for an Lattice Mico32 ELF reloc.  */
    527      1.1  christos 
    528  1.1.1.6  christos static bfd_boolean
    529  1.1.1.6  christos lm32_info_to_howto_rela (bfd *abfd,
    530  1.1.1.5  christos 			 arelent *cache_ptr,
    531  1.1.1.5  christos 			 Elf_Internal_Rela *dst)
    532      1.1  christos {
    533      1.1  christos   unsigned int r_type;
    534      1.1  christos 
    535      1.1  christos   r_type = ELF32_R_TYPE (dst->r_info);
    536  1.1.1.3  christos   if (r_type >= (unsigned int) R_LM32_max)
    537  1.1.1.3  christos     {
    538  1.1.1.5  christos       /* xgettext:c-format */
    539  1.1.1.6  christos       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
    540  1.1.1.6  christos 			  abfd, r_type);
    541  1.1.1.6  christos       bfd_set_error (bfd_error_bad_value);
    542  1.1.1.6  christos       return FALSE;
    543  1.1.1.3  christos     }
    544      1.1  christos   cache_ptr->howto = &lm32_elf_howto_table[r_type];
    545  1.1.1.6  christos   return TRUE;
    546      1.1  christos }
    547      1.1  christos 
    548      1.1  christos /* Set the right machine number for an Lattice Mico32 ELF file. */
    549      1.1  christos 
    550      1.1  christos static bfd_boolean
    551      1.1  christos lm32_elf_object_p (bfd *abfd)
    552      1.1  christos {
    553      1.1  christos   return bfd_default_set_arch_mach (abfd, bfd_arch_lm32, bfd_mach_lm32);
    554      1.1  christos }
    555      1.1  christos 
    556      1.1  christos /* Set machine type flags just before file is written out. */
    557      1.1  christos 
    558      1.1  christos static void
    559      1.1  christos lm32_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
    560      1.1  christos {
    561      1.1  christos   elf_elfheader (abfd)->e_machine = EM_LATTICEMICO32;
    562      1.1  christos   elf_elfheader (abfd)->e_flags &=~ EF_LM32_MACH;
    563      1.1  christos   switch (bfd_get_mach (abfd))
    564      1.1  christos     {
    565      1.1  christos       case bfd_mach_lm32:
    566  1.1.1.5  christos 	elf_elfheader (abfd)->e_flags |= E_LM32_MACH;
    567  1.1.1.5  christos 	break;
    568      1.1  christos       default:
    569  1.1.1.5  christos 	abort ();
    570      1.1  christos     }
    571      1.1  christos }
    572      1.1  christos 
    573      1.1  christos /* Set the GP value for OUTPUT_BFD.  Returns FALSE if this is a
    574      1.1  christos    dangerous relocation.  */
    575      1.1  christos 
    576      1.1  christos static bfd_boolean
    577      1.1  christos lm32_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp)
    578      1.1  christos {
    579      1.1  christos   unsigned int count;
    580      1.1  christos   asymbol **sym;
    581      1.1  christos   unsigned int i;
    582      1.1  christos 
    583      1.1  christos   /* If we've already figured out what GP will be, just return it. */
    584      1.1  christos   *pgp = _bfd_get_gp_value (output_bfd);
    585      1.1  christos   if (*pgp)
    586      1.1  christos     return TRUE;
    587      1.1  christos 
    588      1.1  christos   count = bfd_get_symcount (output_bfd);
    589      1.1  christos   sym = bfd_get_outsymbols (output_bfd);
    590      1.1  christos 
    591      1.1  christos   /* The linker script will have created a symbol named `_gp' with the
    592      1.1  christos      appropriate value.  */
    593      1.1  christos   if (sym == NULL)
    594      1.1  christos     i = count;
    595      1.1  christos   else
    596      1.1  christos     {
    597      1.1  christos       for (i = 0; i < count; i++, sym++)
    598      1.1  christos 	{
    599      1.1  christos 	  const char *name;
    600      1.1  christos 
    601      1.1  christos 	  name = bfd_asymbol_name (*sym);
    602      1.1  christos 	  if (*name == '_' && strcmp (name, "_gp") == 0)
    603      1.1  christos 	    {
    604      1.1  christos 	      *pgp = bfd_asymbol_value (*sym);
    605      1.1  christos 	      _bfd_set_gp_value (output_bfd, *pgp);
    606      1.1  christos 	      break;
    607      1.1  christos 	    }
    608      1.1  christos 	}
    609      1.1  christos     }
    610      1.1  christos 
    611      1.1  christos   if (i >= count)
    612      1.1  christos     {
    613      1.1  christos       /* Only get the error once.  */
    614      1.1  christos       *pgp = 4;
    615      1.1  christos       _bfd_set_gp_value (output_bfd, *pgp);
    616      1.1  christos       return FALSE;
    617      1.1  christos     }
    618      1.1  christos 
    619      1.1  christos   return TRUE;
    620      1.1  christos }
    621      1.1  christos 
    622      1.1  christos /* We have to figure out the gp value, so that we can adjust the
    623      1.1  christos    symbol value correctly.  We look up the symbol _gp in the output
    624      1.1  christos    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
    625      1.1  christos    target data.  We don't need to adjust the symbol value for an
    626      1.1  christos    external symbol if we are producing relocatable output.  */
    627      1.1  christos 
    628      1.1  christos static bfd_reloc_status_type
    629      1.1  christos lm32_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
    630  1.1.1.5  christos 		    char **error_message, bfd_vma *pgp)
    631      1.1  christos {
    632      1.1  christos   if (bfd_is_und_section (symbol->section) && !relocatable)
    633      1.1  christos     {
    634      1.1  christos       *pgp = 0;
    635      1.1  christos       return bfd_reloc_undefined;
    636      1.1  christos     }
    637      1.1  christos 
    638      1.1  christos   *pgp = _bfd_get_gp_value (output_bfd);
    639      1.1  christos   if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0))
    640      1.1  christos     {
    641      1.1  christos       if (relocatable)
    642      1.1  christos 	{
    643      1.1  christos 	  /* Make up a value.  */
    644      1.1  christos 	  *pgp = symbol->section->output_section->vma + 0x4000;
    645      1.1  christos 	  _bfd_set_gp_value (output_bfd, *pgp);
    646      1.1  christos 	}
    647      1.1  christos       else if (!lm32_elf_assign_gp (output_bfd, pgp))
    648      1.1  christos 	{
    649      1.1  christos 	  *error_message =
    650      1.1  christos 	    (char *)
    651      1.1  christos 	    _("global pointer relative relocation when _gp not defined");
    652      1.1  christos 	  return bfd_reloc_dangerous;
    653      1.1  christos 	}
    654      1.1  christos     }
    655      1.1  christos 
    656      1.1  christos   return bfd_reloc_ok;
    657      1.1  christos }
    658      1.1  christos 
    659      1.1  christos static bfd_reloc_status_type
    660      1.1  christos lm32_elf_do_gprel_relocate (bfd *abfd,
    661      1.1  christos 			    reloc_howto_type *howto,
    662      1.1  christos 			    asection *input_section ATTRIBUTE_UNUSED,
    663      1.1  christos 			    bfd_byte *data,
    664      1.1  christos 			    bfd_vma offset,
    665      1.1  christos 			    bfd_vma symbol_value,
    666      1.1  christos 			    bfd_vma addend)
    667      1.1  christos {
    668      1.1  christos   return _bfd_final_link_relocate (howto, abfd, input_section,
    669      1.1  christos 				   data, offset, symbol_value, addend);
    670      1.1  christos }
    671      1.1  christos 
    672      1.1  christos static bfd_reloc_status_type
    673      1.1  christos lm32_elf_gprel_reloc (bfd *abfd,
    674      1.1  christos 		      arelent *reloc_entry,
    675      1.1  christos 		      asymbol *symbol,
    676      1.1  christos 		      void *data,
    677      1.1  christos 		      asection *input_section,
    678      1.1  christos 		      bfd *output_bfd,
    679      1.1  christos 		      char **msg)
    680      1.1  christos {
    681      1.1  christos   bfd_vma relocation;
    682      1.1  christos   bfd_vma gp;
    683      1.1  christos   bfd_reloc_status_type r;
    684      1.1  christos 
    685      1.1  christos   if (output_bfd != (bfd *) NULL
    686      1.1  christos       && (symbol->flags & BSF_SECTION_SYM) == 0
    687      1.1  christos       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
    688      1.1  christos     {
    689      1.1  christos       reloc_entry->address += input_section->output_offset;
    690      1.1  christos       return bfd_reloc_ok;
    691      1.1  christos     }
    692      1.1  christos 
    693      1.1  christos   if (output_bfd != NULL)
    694      1.1  christos     return bfd_reloc_ok;
    695      1.1  christos 
    696      1.1  christos   relocation = symbol->value
    697      1.1  christos     + symbol->section->output_section->vma + symbol->section->output_offset;
    698      1.1  christos 
    699      1.1  christos   if ((r =
    700      1.1  christos        lm32_elf_final_gp (abfd, symbol, FALSE, msg, &gp)) == bfd_reloc_ok)
    701      1.1  christos     {
    702      1.1  christos       relocation = relocation + reloc_entry->addend - gp;
    703      1.1  christos       reloc_entry->addend = 0;
    704      1.1  christos       if ((signed) relocation < -32768 || (signed) relocation > 32767)
    705      1.1  christos 	{
    706      1.1  christos 	  *msg = _("global pointer relative address out of range");
    707      1.1  christos 	  r = bfd_reloc_outofrange;
    708      1.1  christos 	}
    709      1.1  christos       else
    710      1.1  christos 	{
    711      1.1  christos 	  r = lm32_elf_do_gprel_relocate (abfd, reloc_entry->howto,
    712      1.1  christos 					     input_section,
    713      1.1  christos 					     data, reloc_entry->address,
    714      1.1  christos 					     relocation, reloc_entry->addend);
    715      1.1  christos 	}
    716      1.1  christos     }
    717      1.1  christos 
    718      1.1  christos   return r;
    719      1.1  christos }
    720      1.1  christos 
    721      1.1  christos /* Find the segment number in which OSEC, and output section, is
    722      1.1  christos    located.  */
    723      1.1  christos 
    724      1.1  christos static unsigned
    725      1.1  christos _lm32fdpic_osec_to_segment (bfd *output_bfd, asection *osec)
    726      1.1  christos {
    727      1.1  christos   struct elf_segment_map *m;
    728      1.1  christos   Elf_Internal_Phdr *p;
    729      1.1  christos 
    730      1.1  christos   /* Find the segment that contains the output_section.  */
    731  1.1.1.3  christos   for (m = elf_seg_map (output_bfd), p = elf_tdata (output_bfd)->phdr;
    732      1.1  christos        m != NULL;
    733      1.1  christos        m = m->next, p++)
    734      1.1  christos     {
    735      1.1  christos       int i;
    736      1.1  christos 
    737      1.1  christos       for (i = m->count - 1; i >= 0; i--)
    738      1.1  christos 	if (m->sections[i] == osec)
    739      1.1  christos 	  break;
    740      1.1  christos 
    741      1.1  christos       if (i >= 0)
    742      1.1  christos 	break;
    743      1.1  christos     }
    744      1.1  christos 
    745      1.1  christos   return p - elf_tdata (output_bfd)->phdr;
    746      1.1  christos }
    747      1.1  christos 
    748      1.1  christos /* Determine if an output section is read-only.  */
    749      1.1  christos 
    750      1.1  christos inline static bfd_boolean
    751      1.1  christos _lm32fdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
    752      1.1  christos {
    753      1.1  christos   unsigned seg = _lm32fdpic_osec_to_segment (output_bfd, osec);
    754      1.1  christos 
    755      1.1  christos   return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
    756      1.1  christos }
    757      1.1  christos 
    758      1.1  christos /* Relocate a section */
    759      1.1  christos 
    760      1.1  christos static bfd_boolean
    761      1.1  christos lm32_elf_relocate_section (bfd *output_bfd,
    762  1.1.1.5  christos 			   struct bfd_link_info *info,
    763  1.1.1.5  christos 			   bfd *input_bfd,
    764  1.1.1.5  christos 			   asection *input_section,
    765  1.1.1.5  christos 			   bfd_byte *contents,
    766  1.1.1.5  christos 			   Elf_Internal_Rela *relocs,
    767  1.1.1.5  christos 			   Elf_Internal_Sym *local_syms,
    768  1.1.1.5  christos 			   asection **local_sections)
    769      1.1  christos {
    770      1.1  christos   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
    771      1.1  christos   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
    772      1.1  christos   Elf_Internal_Rela *rel, *relend;
    773      1.1  christos   struct elf_lm32_link_hash_table *htab = lm32_elf_hash_table (info);
    774      1.1  christos   bfd_vma *local_got_offsets;
    775      1.1  christos   asection *sgot;
    776      1.1  christos 
    777      1.1  christos   if (htab == NULL)
    778      1.1  christos     return FALSE;
    779      1.1  christos 
    780      1.1  christos   local_got_offsets = elf_local_got_offsets (input_bfd);
    781      1.1  christos 
    782  1.1.1.5  christos   sgot = htab->root.sgot;
    783      1.1  christos 
    784      1.1  christos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
    785      1.1  christos   sym_hashes = elf_sym_hashes (input_bfd);
    786      1.1  christos 
    787      1.1  christos   rel = relocs;
    788      1.1  christos   relend = relocs + input_section->reloc_count;
    789      1.1  christos   for (; rel < relend; rel++)
    790      1.1  christos     {
    791      1.1  christos       reloc_howto_type *howto;
    792      1.1  christos       unsigned int r_type;
    793      1.1  christos       unsigned long r_symndx;
    794      1.1  christos       Elf_Internal_Sym *sym;
    795      1.1  christos       asection *sec;
    796      1.1  christos       struct elf_link_hash_entry *h;
    797      1.1  christos       bfd_vma relocation;
    798      1.1  christos       bfd_vma gp;
    799      1.1  christos       bfd_reloc_status_type r;
    800      1.1  christos       const char *name = NULL;
    801      1.1  christos 
    802      1.1  christos       r_symndx = ELF32_R_SYM (rel->r_info);
    803      1.1  christos       r_type = ELF32_R_TYPE (rel->r_info);
    804      1.1  christos 
    805      1.1  christos       if (r_type == R_LM32_GNU_VTENTRY
    806  1.1.1.5  christos 	  || r_type == R_LM32_GNU_VTINHERIT )
    807  1.1.1.5  christos 	continue;
    808      1.1  christos 
    809      1.1  christos       h = NULL;
    810      1.1  christos       sym = NULL;
    811      1.1  christos       sec = NULL;
    812      1.1  christos 
    813      1.1  christos       howto = lm32_elf_howto_table + r_type;
    814      1.1  christos 
    815      1.1  christos       if (r_symndx < symtab_hdr->sh_info)
    816  1.1.1.5  christos 	{
    817  1.1.1.5  christos 	  /* It's a local symbol.  */
    818  1.1.1.5  christos 	  sym = local_syms + r_symndx;
    819  1.1.1.5  christos 	  sec = local_sections[r_symndx];
    820  1.1.1.5  christos 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
    821  1.1.1.5  christos 	  name = bfd_elf_string_from_elf_section
    822      1.1  christos 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
    823      1.1  christos 	  name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
    824  1.1.1.5  christos 	}
    825      1.1  christos       else
    826  1.1.1.5  christos 	{
    827  1.1.1.5  christos 	  /* It's a global symbol.  */
    828  1.1.1.5  christos 	  bfd_boolean unresolved_reloc;
    829  1.1.1.3  christos 	  bfd_boolean warned, ignored;
    830      1.1  christos 
    831      1.1  christos 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
    832      1.1  christos 				   r_symndx, symtab_hdr, sym_hashes,
    833      1.1  christos 				   h, sec, relocation,
    834  1.1.1.3  christos 				   unresolved_reloc, warned, ignored);
    835      1.1  christos 	  name = h->root.root.string;
    836  1.1.1.5  christos 	}
    837      1.1  christos 
    838  1.1.1.2  christos       if (sec != NULL && discarded_section (sec))
    839      1.1  christos 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
    840  1.1.1.2  christos 					 rel, 1, relend, howto, 0, contents);
    841      1.1  christos 
    842  1.1.1.3  christos       if (bfd_link_relocatable (info))
    843  1.1.1.5  christos 	{
    844      1.1  christos 	  /* This is a relocatable link.  We don't have to change
    845      1.1  christos 	     anything, unless the reloc is against a section symbol,
    846      1.1  christos 	     in which case we have to adjust according to where the
    847      1.1  christos 	     section symbol winds up in the output section.  */
    848      1.1  christos 	  if (sym == NULL || ELF_ST_TYPE (sym->st_info) != STT_SECTION)
    849      1.1  christos 	    continue;
    850      1.1  christos 
    851      1.1  christos 	  /* If partial_inplace, we need to store any additional addend
    852      1.1  christos 	     back in the section.  */
    853      1.1  christos 	  if (! howto->partial_inplace)
    854      1.1  christos 	    continue;
    855      1.1  christos 
    856  1.1.1.5  christos 	  /* Shouldn't reach here.  */
    857      1.1  christos 	  abort ();
    858      1.1  christos 	  r = bfd_reloc_ok;
    859  1.1.1.5  christos 	}
    860      1.1  christos       else
    861  1.1.1.5  christos 	{
    862  1.1.1.5  christos 	  switch (howto->type)
    863  1.1.1.5  christos 	    {
    864  1.1.1.5  christos 	    case R_LM32_GPREL16:
    865  1.1.1.5  christos 	      if (!lm32_elf_assign_gp (output_bfd, &gp))
    866  1.1.1.5  christos 		r = bfd_reloc_dangerous;
    867  1.1.1.5  christos 	      else
    868  1.1.1.5  christos 		{
    869  1.1.1.5  christos 		  relocation = relocation + rel->r_addend - gp;
    870  1.1.1.5  christos 		  rel->r_addend = 0;
    871  1.1.1.5  christos 		  if ((signed)relocation < -32768 || (signed)relocation > 32767)
    872  1.1.1.5  christos 		    r = bfd_reloc_outofrange;
    873  1.1.1.5  christos 		  else
    874  1.1.1.5  christos 		    {
    875  1.1.1.5  christos 		      r = _bfd_final_link_relocate (howto, input_bfd,
    876  1.1.1.5  christos 						    input_section, contents,
    877  1.1.1.5  christos 						    rel->r_offset, relocation,
    878  1.1.1.5  christos 						    rel->r_addend);
    879  1.1.1.5  christos 		    }
    880  1.1.1.5  christos 		}
    881  1.1.1.5  christos 	      break;
    882  1.1.1.5  christos 	    case R_LM32_16_GOT:
    883  1.1.1.5  christos 	      /* Relocation is to the entry for this symbol in the global
    884  1.1.1.5  christos 		 offset table.  */
    885  1.1.1.5  christos 	      BFD_ASSERT (sgot != NULL);
    886  1.1.1.5  christos 	      if (h != NULL)
    887  1.1.1.5  christos 		{
    888  1.1.1.5  christos 		  bfd_boolean dyn;
    889  1.1.1.5  christos 		  bfd_vma off;
    890      1.1  christos 
    891  1.1.1.5  christos 		  off = h->got.offset;
    892  1.1.1.5  christos 		  BFD_ASSERT (off != (bfd_vma) -1);
    893      1.1  christos 
    894  1.1.1.5  christos 		  dyn = htab->root.dynamic_sections_created;
    895  1.1.1.5  christos 		  if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
    896  1.1.1.3  christos 							 bfd_link_pic (info),
    897  1.1.1.3  christos 							 h)
    898  1.1.1.5  christos 		      || (bfd_link_pic (info)
    899  1.1.1.5  christos 			  && (info->symbolic
    900  1.1.1.5  christos 			      || h->dynindx == -1
    901  1.1.1.5  christos 			      || h->forced_local)
    902  1.1.1.5  christos 			  && h->def_regular))
    903  1.1.1.5  christos 		    {
    904  1.1.1.5  christos 		      /* This is actually a static link, or it is a
    905  1.1.1.5  christos 			 -Bsymbolic link and the symbol is defined
    906  1.1.1.5  christos 			 locally, or the symbol was forced to be local
    907  1.1.1.5  christos 			 because of a version file.  We must initialize
    908  1.1.1.5  christos 			 this entry in the global offset table.  Since the
    909  1.1.1.5  christos 			 offset must always be a multiple of 4, we use the
    910  1.1.1.5  christos 			 least significant bit to record whether we have
    911  1.1.1.5  christos 			 initialized it already.
    912  1.1.1.5  christos 
    913  1.1.1.5  christos 			 When doing a dynamic link, we create a .rela.got
    914  1.1.1.5  christos 			 relocation entry to initialize the value.  This
    915  1.1.1.5  christos 			 is done in the finish_dynamic_symbol routine.  */
    916  1.1.1.5  christos 		      if ((off & 1) != 0)
    917  1.1.1.5  christos 			off &= ~1;
    918  1.1.1.5  christos 		      else
    919  1.1.1.5  christos 			{
    920  1.1.1.5  christos 			  /* Write entry in GOT */
    921  1.1.1.5  christos 			  bfd_put_32 (output_bfd, relocation,
    922  1.1.1.5  christos 				      sgot->contents + off);
    923  1.1.1.5  christos 			  /* Create entry in .rofixup pointing to GOT entry.  */
    924  1.1.1.5  christos 			   if (IS_FDPIC (output_bfd) && h->root.type != bfd_link_hash_undefweak)
    925  1.1.1.5  christos 			     {
    926  1.1.1.5  christos 			       _lm32fdpic_add_rofixup (output_bfd,
    927  1.1.1.5  christos 						       lm32fdpic_fixup32_section
    928  1.1.1.5  christos 							(info),
    929  1.1.1.5  christos 						       sgot->output_section->vma
    930  1.1.1.5  christos 							+ sgot->output_offset
    931  1.1.1.5  christos 							+ off);
    932  1.1.1.5  christos 			     }
    933  1.1.1.5  christos 			  /* Mark GOT entry as having been written.  */
    934  1.1.1.5  christos 			  h->got.offset |= 1;
    935  1.1.1.5  christos 			}
    936  1.1.1.5  christos 		    }
    937  1.1.1.5  christos 
    938  1.1.1.5  christos 		  relocation = sgot->output_offset + off;
    939  1.1.1.5  christos 		}
    940  1.1.1.5  christos 	      else
    941  1.1.1.5  christos 		{
    942  1.1.1.5  christos 		  bfd_vma off;
    943  1.1.1.5  christos 		  bfd_byte *loc;
    944  1.1.1.5  christos 
    945  1.1.1.5  christos 		  BFD_ASSERT (local_got_offsets != NULL
    946  1.1.1.5  christos 			      && local_got_offsets[r_symndx] != (bfd_vma) -1);
    947  1.1.1.5  christos 
    948  1.1.1.5  christos 		  /* Get offset into GOT table.  */
    949  1.1.1.5  christos 		  off = local_got_offsets[r_symndx];
    950  1.1.1.5  christos 
    951  1.1.1.5  christos 		  /* The offset must always be a multiple of 4.  We use
    952  1.1.1.5  christos 		     the least significant bit to record whether we have
    953  1.1.1.5  christos 		     already processed this entry.  */
    954  1.1.1.5  christos 		  if ((off & 1) != 0)
    955  1.1.1.5  christos 		    off &= ~1;
    956  1.1.1.5  christos 		  else
    957  1.1.1.5  christos 		    {
    958  1.1.1.5  christos 		      /* Write entry in GOT.  */
    959  1.1.1.5  christos 		      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
    960  1.1.1.5  christos 		      /* Create entry in .rofixup pointing to GOT entry.  */
    961  1.1.1.5  christos 		      if (IS_FDPIC (output_bfd))
    962  1.1.1.5  christos 			{
    963  1.1.1.5  christos 			  _lm32fdpic_add_rofixup (output_bfd,
    964  1.1.1.5  christos 						  lm32fdpic_fixup32_section
    965  1.1.1.5  christos 						   (info),
    966  1.1.1.5  christos 						  sgot->output_section->vma
    967  1.1.1.5  christos 						   + sgot->output_offset
    968  1.1.1.5  christos 						   + off);
    969  1.1.1.5  christos 			}
    970  1.1.1.5  christos 
    971  1.1.1.5  christos 		      if (bfd_link_pic (info))
    972  1.1.1.5  christos 			{
    973  1.1.1.5  christos 			  asection *srelgot;
    974  1.1.1.5  christos 			  Elf_Internal_Rela outrel;
    975  1.1.1.5  christos 
    976  1.1.1.5  christos 			  /* We need to generate a R_LM32_RELATIVE reloc
    977  1.1.1.5  christos 			     for the dynamic linker.  */
    978  1.1.1.5  christos 			  srelgot = htab->root.srelgot;
    979  1.1.1.5  christos 			  BFD_ASSERT (srelgot != NULL);
    980  1.1.1.5  christos 
    981  1.1.1.5  christos 			  outrel.r_offset = (sgot->output_section->vma
    982  1.1.1.5  christos 					     + sgot->output_offset
    983  1.1.1.5  christos 					     + off);
    984  1.1.1.5  christos 			  outrel.r_info = ELF32_R_INFO (0, R_LM32_RELATIVE);
    985  1.1.1.5  christos 			  outrel.r_addend = relocation;
    986  1.1.1.5  christos 			  loc = srelgot->contents;
    987  1.1.1.5  christos 			  loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
    988  1.1.1.5  christos 			  bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
    989  1.1.1.5  christos 			  ++srelgot->reloc_count;
    990  1.1.1.5  christos 			}
    991  1.1.1.5  christos 
    992  1.1.1.5  christos 		      local_got_offsets[r_symndx] |= 1;
    993  1.1.1.5  christos 		    }
    994  1.1.1.5  christos 
    995  1.1.1.5  christos 
    996  1.1.1.5  christos 		  relocation = sgot->output_offset + off;
    997  1.1.1.5  christos 		}
    998  1.1.1.5  christos 
    999  1.1.1.5  christos 	      /* Addend should be zero.  */
   1000  1.1.1.5  christos 	      if (rel->r_addend != 0)
   1001  1.1.1.6  christos 		_bfd_error_handler
   1002  1.1.1.6  christos 		  (_("internal error: addend should be zero for %s"),
   1003  1.1.1.6  christos 		   "R_LM32_16_GOT");
   1004  1.1.1.5  christos 
   1005  1.1.1.5  christos 	      r = _bfd_final_link_relocate (howto,
   1006  1.1.1.5  christos 					    input_bfd,
   1007  1.1.1.5  christos 					    input_section,
   1008  1.1.1.5  christos 					    contents,
   1009  1.1.1.5  christos 					    rel->r_offset,
   1010  1.1.1.5  christos 					    relocation,
   1011  1.1.1.5  christos 					    rel->r_addend);
   1012  1.1.1.5  christos 	      break;
   1013  1.1.1.5  christos 
   1014  1.1.1.5  christos 	    case R_LM32_GOTOFF_LO16:
   1015  1.1.1.5  christos 	    case R_LM32_GOTOFF_HI16:
   1016  1.1.1.5  christos 	      /* Relocation is offset from GOT.  */
   1017      1.1  christos 	      BFD_ASSERT (sgot != NULL);
   1018      1.1  christos 	      relocation -= sgot->output_section->vma;
   1019      1.1  christos 	      /* Account for sign-extension.  */
   1020  1.1.1.5  christos 	      if ((r_type == R_LM32_GOTOFF_HI16)
   1021  1.1.1.5  christos 		  && ((relocation + rel->r_addend) & 0x8000))
   1022  1.1.1.5  christos 		rel->r_addend += 0x10000;
   1023  1.1.1.5  christos 	      r = _bfd_final_link_relocate (howto,
   1024  1.1.1.5  christos 					    input_bfd,
   1025  1.1.1.5  christos 					    input_section,
   1026  1.1.1.5  christos 					    contents,
   1027  1.1.1.5  christos 					    rel->r_offset,
   1028  1.1.1.5  christos 					    relocation,
   1029  1.1.1.5  christos 					    rel->r_addend);
   1030  1.1.1.5  christos 	      break;
   1031  1.1.1.5  christos 
   1032  1.1.1.5  christos 	    case R_LM32_32:
   1033  1.1.1.5  christos 	      if (IS_FDPIC (output_bfd))
   1034  1.1.1.5  christos 		{
   1035  1.1.1.5  christos 		  if ((!h) || (h && h->root.type != bfd_link_hash_undefweak))
   1036  1.1.1.5  christos 		    {
   1037  1.1.1.5  christos 		      /* Only create .rofixup entries for relocs in loadable sections.  */
   1038  1.1.1.5  christos 		      if ((bfd_get_section_flags (output_bfd, input_section->output_section)
   1039  1.1.1.5  christos 			  & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
   1040  1.1.1.5  christos 
   1041  1.1.1.5  christos 			{
   1042  1.1.1.5  christos 			  /* Check address to be modified is writable.  */
   1043  1.1.1.5  christos 			  if (_lm32fdpic_osec_readonly_p (output_bfd,
   1044  1.1.1.5  christos 							  input_section
   1045  1.1.1.5  christos 							   ->output_section))
   1046  1.1.1.5  christos 			    {
   1047  1.1.1.5  christos 			      info->callbacks->warning
   1048  1.1.1.5  christos 				(info,
   1049  1.1.1.5  christos 				 _("cannot emit dynamic relocations in read-only section"),
   1050  1.1.1.5  christos 				 name, input_bfd, input_section, rel->r_offset);
   1051  1.1.1.5  christos 			       return FALSE;
   1052  1.1.1.5  christos 			    }
   1053  1.1.1.5  christos 			  /* Create entry in .rofixup section.  */
   1054  1.1.1.5  christos 			  _lm32fdpic_add_rofixup (output_bfd,
   1055  1.1.1.5  christos 						  lm32fdpic_fixup32_section (info),
   1056  1.1.1.5  christos 						  input_section->output_section->vma
   1057  1.1.1.5  christos 						   + input_section->output_offset
   1058  1.1.1.5  christos 						   + rel->r_offset);
   1059  1.1.1.5  christos 			}
   1060  1.1.1.5  christos 		    }
   1061  1.1.1.5  christos 		}
   1062  1.1.1.5  christos 	      /* Fall through.  */
   1063  1.1.1.5  christos 
   1064  1.1.1.5  christos 	    default:
   1065  1.1.1.5  christos 	      r = _bfd_final_link_relocate (howto,
   1066  1.1.1.5  christos 					    input_bfd,
   1067  1.1.1.5  christos 					    input_section,
   1068  1.1.1.5  christos 					    contents,
   1069  1.1.1.5  christos 					    rel->r_offset,
   1070  1.1.1.5  christos 					    relocation,
   1071  1.1.1.5  christos 					    rel->r_addend);
   1072  1.1.1.5  christos 	      break;
   1073  1.1.1.5  christos 	    }
   1074  1.1.1.5  christos 	}
   1075      1.1  christos 
   1076      1.1  christos       if (r != bfd_reloc_ok)
   1077  1.1.1.5  christos 	{
   1078  1.1.1.5  christos 	  const char *msg = NULL;
   1079  1.1.1.5  christos 	  arelent bfd_reloc;
   1080  1.1.1.5  christos 
   1081  1.1.1.6  christos 	  if (! lm32_info_to_howto_rela (input_bfd, &bfd_reloc, rel))
   1082  1.1.1.6  christos 	    continue;
   1083  1.1.1.5  christos 	  howto = bfd_reloc.howto;
   1084  1.1.1.5  christos 
   1085  1.1.1.5  christos 	  if (h != NULL)
   1086  1.1.1.5  christos 	    name = h->root.root.string;
   1087  1.1.1.5  christos 	  else
   1088  1.1.1.5  christos 	    {
   1089  1.1.1.5  christos 	      name = (bfd_elf_string_from_elf_section
   1090  1.1.1.5  christos 		      (input_bfd, symtab_hdr->sh_link, sym->st_name));
   1091  1.1.1.5  christos 	      if (name == NULL || *name == '\0')
   1092  1.1.1.5  christos 		name = bfd_section_name (input_bfd, sec);
   1093  1.1.1.5  christos 	    }
   1094      1.1  christos 
   1095  1.1.1.5  christos 	  switch (r)
   1096  1.1.1.5  christos 	    {
   1097      1.1  christos 	    case bfd_reloc_overflow:
   1098      1.1  christos 	      if ((h != NULL)
   1099  1.1.1.5  christos 		 && (h->root.type == bfd_link_hash_undefweak))
   1100  1.1.1.5  christos 		break;
   1101  1.1.1.4  christos 	      (*info->callbacks->reloc_overflow)
   1102  1.1.1.4  christos 		(info, (h ? &h->root : NULL), name, howto->name,
   1103  1.1.1.4  christos 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
   1104      1.1  christos 	      break;
   1105      1.1  christos 
   1106  1.1.1.5  christos 	    case bfd_reloc_undefined:
   1107  1.1.1.4  christos 	      (*info->callbacks->undefined_symbol)
   1108  1.1.1.4  christos 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
   1109  1.1.1.5  christos 	      break;
   1110      1.1  christos 
   1111  1.1.1.5  christos 	    case bfd_reloc_outofrange:
   1112  1.1.1.5  christos 	      msg = _("internal error: out of range error");
   1113  1.1.1.5  christos 	      goto common_error;
   1114  1.1.1.5  christos 
   1115  1.1.1.5  christos 	    case bfd_reloc_notsupported:
   1116  1.1.1.5  christos 	      msg = _("internal error: unsupported relocation error");
   1117  1.1.1.5  christos 	      goto common_error;
   1118  1.1.1.5  christos 
   1119  1.1.1.5  christos 	    case bfd_reloc_dangerous:
   1120  1.1.1.5  christos 	      msg = _("internal error: dangerous error");
   1121  1.1.1.5  christos 	      goto common_error;
   1122  1.1.1.5  christos 
   1123  1.1.1.5  christos 	    default:
   1124  1.1.1.5  christos 	      msg = _("internal error: unknown error");
   1125  1.1.1.5  christos 	      /* fall through */
   1126      1.1  christos 
   1127  1.1.1.5  christos 	    common_error:
   1128  1.1.1.4  christos 	      (*info->callbacks->warning) (info, msg, name, input_bfd,
   1129  1.1.1.4  christos 					   input_section, rel->r_offset);
   1130  1.1.1.5  christos 	      break;
   1131  1.1.1.5  christos 	    }
   1132  1.1.1.5  christos 	}
   1133      1.1  christos     }
   1134      1.1  christos 
   1135      1.1  christos   return TRUE;
   1136      1.1  christos }
   1137      1.1  christos 
   1138      1.1  christos static asection *
   1139      1.1  christos lm32_elf_gc_mark_hook (asection *sec,
   1140  1.1.1.5  christos 		       struct bfd_link_info *info,
   1141  1.1.1.5  christos 		       Elf_Internal_Rela *rel,
   1142  1.1.1.5  christos 		       struct elf_link_hash_entry *h,
   1143  1.1.1.5  christos 		       Elf_Internal_Sym *sym)
   1144      1.1  christos {
   1145      1.1  christos   if (h != NULL)
   1146      1.1  christos     switch (ELF32_R_TYPE (rel->r_info))
   1147      1.1  christos       {
   1148      1.1  christos       case R_LM32_GNU_VTINHERIT:
   1149      1.1  christos       case R_LM32_GNU_VTENTRY:
   1150      1.1  christos 	return NULL;
   1151      1.1  christos       }
   1152      1.1  christos 
   1153      1.1  christos   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   1154      1.1  christos }
   1155      1.1  christos 
   1156      1.1  christos /* Look through the relocs for a section during the first phase.  */
   1157      1.1  christos 
   1158      1.1  christos static bfd_boolean
   1159      1.1  christos lm32_elf_check_relocs (bfd *abfd,
   1160  1.1.1.5  christos 		       struct bfd_link_info *info,
   1161  1.1.1.5  christos 		       asection *sec,
   1162  1.1.1.5  christos 		       const Elf_Internal_Rela *relocs)
   1163      1.1  christos {
   1164      1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1165      1.1  christos   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   1166      1.1  christos   const Elf_Internal_Rela *rel;
   1167      1.1  christos   const Elf_Internal_Rela *rel_end;
   1168      1.1  christos   struct elf_lm32_link_hash_table *htab;
   1169      1.1  christos   bfd *dynobj;
   1170      1.1  christos 
   1171  1.1.1.3  christos   if (bfd_link_relocatable (info))
   1172      1.1  christos     return TRUE;
   1173      1.1  christos 
   1174  1.1.1.5  christos   /* Don't do anything special with non-loaded, non-alloced sections.
   1175  1.1.1.5  christos      In particular, any relocs in such sections should not affect GOT
   1176  1.1.1.5  christos      and PLT reference counting (ie. we don't allow them to create GOT
   1177  1.1.1.5  christos      or PLT entries), there's no possibility or desire to optimize TLS
   1178  1.1.1.5  christos      relocs, and there's not much point in propagating relocs to shared
   1179  1.1.1.5  christos      libs that the dynamic linker won't relocate.  */
   1180  1.1.1.5  christos   if ((sec->flags & SEC_ALLOC) == 0)
   1181  1.1.1.5  christos     return TRUE;
   1182  1.1.1.5  christos 
   1183      1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1184      1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   1185      1.1  christos   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
   1186      1.1  christos   if (!elf_bad_symtab (abfd))
   1187      1.1  christos     sym_hashes_end -= symtab_hdr->sh_info;
   1188      1.1  christos 
   1189      1.1  christos   htab = lm32_elf_hash_table (info);
   1190      1.1  christos   if (htab == NULL)
   1191      1.1  christos     return FALSE;
   1192      1.1  christos 
   1193      1.1  christos   dynobj = htab->root.dynobj;
   1194      1.1  christos 
   1195      1.1  christos   rel_end = relocs + sec->reloc_count;
   1196      1.1  christos   for (rel = relocs; rel < rel_end; rel++)
   1197      1.1  christos     {
   1198      1.1  christos       int r_type;
   1199      1.1  christos       struct elf_link_hash_entry *h;
   1200      1.1  christos       unsigned long r_symndx;
   1201      1.1  christos 
   1202      1.1  christos       r_symndx = ELF32_R_SYM (rel->r_info);
   1203      1.1  christos       r_type = ELF32_R_TYPE (rel->r_info);
   1204      1.1  christos       if (r_symndx < symtab_hdr->sh_info)
   1205  1.1.1.5  christos 	h = NULL;
   1206      1.1  christos       else
   1207      1.1  christos 	{
   1208      1.1  christos 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   1209      1.1  christos 	  while (h->root.type == bfd_link_hash_indirect
   1210      1.1  christos 		 || h->root.type == bfd_link_hash_warning)
   1211      1.1  christos 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1212      1.1  christos 	}
   1213      1.1  christos 
   1214      1.1  christos       /* Some relocs require a global offset table.  */
   1215  1.1.1.5  christos       if (htab->root.sgot == NULL)
   1216  1.1.1.5  christos 	{
   1217  1.1.1.5  christos 	  switch (r_type)
   1218  1.1.1.5  christos 	    {
   1219  1.1.1.5  christos 	    case R_LM32_16_GOT:
   1220  1.1.1.5  christos 	    case R_LM32_GOTOFF_HI16:
   1221  1.1.1.5  christos 	    case R_LM32_GOTOFF_LO16:
   1222  1.1.1.5  christos 	      if (dynobj == NULL)
   1223  1.1.1.5  christos 		htab->root.dynobj = dynobj = abfd;
   1224  1.1.1.5  christos 	      if (!_bfd_elf_create_got_section (dynobj, info))
   1225  1.1.1.5  christos 		return FALSE;
   1226  1.1.1.5  christos 	      break;
   1227  1.1.1.5  christos 	    }
   1228  1.1.1.5  christos 	}
   1229      1.1  christos 
   1230      1.1  christos       /* Some relocs require a rofixup table. */
   1231      1.1  christos       if (IS_FDPIC (abfd))
   1232  1.1.1.5  christos 	{
   1233  1.1.1.5  christos 	  switch (r_type)
   1234  1.1.1.5  christos 	    {
   1235  1.1.1.5  christos 	    case R_LM32_32:
   1236  1.1.1.5  christos 	      /* FDPIC requires a GOT if there is a .rofixup section
   1237  1.1.1.5  christos 		 (Normal ELF doesn't). */
   1238  1.1.1.5  christos 	      if (dynobj == NULL)
   1239  1.1.1.5  christos 		htab->root.dynobj = dynobj = abfd;
   1240  1.1.1.5  christos 	      if (!_bfd_elf_create_got_section (dynobj, info))
   1241  1.1.1.5  christos 		return FALSE;
   1242  1.1.1.5  christos 	      /* Create .rofixup section */
   1243  1.1.1.5  christos 	      if (htab->sfixup32 == NULL)
   1244  1.1.1.5  christos 		{
   1245  1.1.1.5  christos 		  if (! create_rofixup_section (dynobj, info))
   1246  1.1.1.5  christos 		    return FALSE;
   1247  1.1.1.5  christos 		}
   1248  1.1.1.5  christos 	      break;
   1249  1.1.1.5  christos 	    case R_LM32_16_GOT:
   1250  1.1.1.5  christos 	    case R_LM32_GOTOFF_HI16:
   1251  1.1.1.5  christos 	    case R_LM32_GOTOFF_LO16:
   1252  1.1.1.5  christos 	      /* Create .rofixup section.  */
   1253  1.1.1.5  christos 	      if (htab->sfixup32 == NULL)
   1254  1.1.1.5  christos 		{
   1255  1.1.1.2  christos 		  if (dynobj == NULL)
   1256  1.1.1.2  christos 		    htab->root.dynobj = dynobj = abfd;
   1257  1.1.1.5  christos 		  if (! create_rofixup_section (dynobj, info))
   1258  1.1.1.5  christos 		    return FALSE;
   1259  1.1.1.5  christos 		}
   1260  1.1.1.5  christos 	      break;
   1261  1.1.1.5  christos 	    }
   1262  1.1.1.5  christos 	}
   1263      1.1  christos 
   1264      1.1  christos       switch (r_type)
   1265      1.1  christos 	{
   1266      1.1  christos 	case R_LM32_16_GOT:
   1267  1.1.1.5  christos 	  if (h != NULL)
   1268  1.1.1.5  christos 	    h->got.refcount += 1;
   1269  1.1.1.5  christos 	  else
   1270  1.1.1.5  christos 	    {
   1271  1.1.1.5  christos 	      bfd_signed_vma *local_got_refcounts;
   1272  1.1.1.5  christos 
   1273  1.1.1.5  christos 	      /* This is a global offset table entry for a local symbol.  */
   1274  1.1.1.5  christos 	      local_got_refcounts = elf_local_got_refcounts (abfd);
   1275  1.1.1.5  christos 	      if (local_got_refcounts == NULL)
   1276  1.1.1.5  christos 		{
   1277  1.1.1.5  christos 		  bfd_size_type size;
   1278  1.1.1.5  christos 
   1279  1.1.1.5  christos 		  size = symtab_hdr->sh_info;
   1280  1.1.1.5  christos 		  size *= sizeof (bfd_signed_vma);
   1281  1.1.1.5  christos 		  local_got_refcounts = bfd_zalloc (abfd, size);
   1282  1.1.1.5  christos 		  if (local_got_refcounts == NULL)
   1283  1.1.1.5  christos 		    return FALSE;
   1284  1.1.1.5  christos 		  elf_local_got_refcounts (abfd) = local_got_refcounts;
   1285  1.1.1.5  christos 		}
   1286  1.1.1.5  christos 	      local_got_refcounts[r_symndx] += 1;
   1287  1.1.1.5  christos 	    }
   1288  1.1.1.5  christos 	  break;
   1289      1.1  christos 
   1290  1.1.1.5  christos 	/* This relocation describes the C++ object vtable hierarchy.
   1291  1.1.1.5  christos 	   Reconstruct it for later use during GC.  */
   1292  1.1.1.5  christos 	case R_LM32_GNU_VTINHERIT:
   1293  1.1.1.5  christos 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   1294  1.1.1.5  christos 	    return FALSE;
   1295  1.1.1.5  christos 	  break;
   1296  1.1.1.5  christos 
   1297  1.1.1.5  christos 	/* This relocation describes which C++ vtable entries are actually
   1298  1.1.1.5  christos 	   used.  Record for later use during GC.  */
   1299  1.1.1.5  christos 	case R_LM32_GNU_VTENTRY:
   1300  1.1.1.5  christos 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   1301  1.1.1.5  christos 	    return FALSE;
   1302  1.1.1.5  christos 	  break;
   1303  1.1.1.5  christos 
   1304  1.1.1.5  christos 	}
   1305      1.1  christos     }
   1306      1.1  christos 
   1307      1.1  christos   return TRUE;
   1308      1.1  christos }
   1309      1.1  christos 
   1310      1.1  christos /* Finish up the dynamic sections.  */
   1311      1.1  christos 
   1312      1.1  christos static bfd_boolean
   1313      1.1  christos lm32_elf_finish_dynamic_sections (bfd *output_bfd,
   1314      1.1  christos 				  struct bfd_link_info *info)
   1315      1.1  christos {
   1316      1.1  christos   struct elf_lm32_link_hash_table *htab;
   1317      1.1  christos   bfd *dynobj;
   1318      1.1  christos   asection *sdyn;
   1319      1.1  christos   asection *sgot;
   1320      1.1  christos 
   1321      1.1  christos   htab = lm32_elf_hash_table (info);
   1322      1.1  christos   if (htab == NULL)
   1323      1.1  christos     return FALSE;
   1324      1.1  christos 
   1325      1.1  christos   dynobj = htab->root.dynobj;
   1326      1.1  christos 
   1327  1.1.1.5  christos   sgot = htab->root.sgotplt;
   1328  1.1.1.2  christos   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   1329      1.1  christos 
   1330      1.1  christos   if (htab->root.dynamic_sections_created)
   1331      1.1  christos     {
   1332      1.1  christos       asection *splt;
   1333      1.1  christos       Elf32_External_Dyn *dyncon, *dynconend;
   1334      1.1  christos 
   1335      1.1  christos       BFD_ASSERT (sgot != NULL && sdyn != NULL);
   1336      1.1  christos 
   1337      1.1  christos       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   1338      1.1  christos       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   1339      1.1  christos 
   1340      1.1  christos       for (; dyncon < dynconend; dyncon++)
   1341  1.1.1.5  christos 	{
   1342  1.1.1.5  christos 	  Elf_Internal_Dyn dyn;
   1343  1.1.1.5  christos 	  asection *s;
   1344      1.1  christos 
   1345  1.1.1.5  christos 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   1346      1.1  christos 
   1347  1.1.1.5  christos 	  switch (dyn.d_tag)
   1348  1.1.1.5  christos 	    {
   1349  1.1.1.5  christos 	    default:
   1350  1.1.1.5  christos 	      break;
   1351  1.1.1.5  christos 
   1352  1.1.1.5  christos 	    case DT_PLTGOT:
   1353  1.1.1.5  christos 	      s = htab->root.sgotplt;
   1354  1.1.1.5  christos 	      goto get_vma;
   1355  1.1.1.5  christos 	    case DT_JMPREL:
   1356  1.1.1.5  christos 	      s = htab->root.srelplt;
   1357  1.1.1.5  christos 	    get_vma:
   1358  1.1.1.5  christos 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   1359  1.1.1.5  christos 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   1360  1.1.1.5  christos 	      break;
   1361  1.1.1.5  christos 
   1362  1.1.1.5  christos 	    case DT_PLTRELSZ:
   1363  1.1.1.5  christos 	      s = htab->root.srelplt;
   1364  1.1.1.5  christos 	      dyn.d_un.d_val = s->size;
   1365  1.1.1.5  christos 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   1366  1.1.1.5  christos 	      break;
   1367  1.1.1.5  christos 	    }
   1368  1.1.1.5  christos 	}
   1369      1.1  christos 
   1370      1.1  christos       /* Fill in the first entry in the procedure linkage table.  */
   1371  1.1.1.5  christos       splt = htab->root.splt;
   1372      1.1  christos       if (splt && splt->size > 0)
   1373  1.1.1.5  christos 	{
   1374  1.1.1.5  christos 	  if (bfd_link_pic (info))
   1375  1.1.1.5  christos 	    {
   1376  1.1.1.5  christos 	      bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
   1377  1.1.1.5  christos 	      bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
   1378  1.1.1.5  christos 	      bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
   1379  1.1.1.5  christos 	      bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
   1380  1.1.1.5  christos 	      bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
   1381  1.1.1.5  christos 	    }
   1382  1.1.1.5  christos 	  else
   1383  1.1.1.5  christos 	    {
   1384  1.1.1.5  christos 	      unsigned long addr;
   1385  1.1.1.5  christos 	      /* addr = .got + 4 */
   1386  1.1.1.5  christos 	      addr = sgot->output_section->vma + sgot->output_offset + 4;
   1387  1.1.1.5  christos 	      bfd_put_32 (output_bfd,
   1388      1.1  christos 			  PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
   1389      1.1  christos 			  splt->contents);
   1390  1.1.1.5  christos 	      bfd_put_32 (output_bfd,
   1391      1.1  christos 			  PLT0_ENTRY_WORD1 | (addr & 0xffff),
   1392      1.1  christos 			  splt->contents + 4);
   1393  1.1.1.5  christos 	      bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
   1394  1.1.1.5  christos 	      bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
   1395  1.1.1.5  christos 	      bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
   1396  1.1.1.5  christos 	    }
   1397  1.1.1.5  christos 
   1398  1.1.1.5  christos 	  elf_section_data (splt->output_section)->this_hdr.sh_entsize =
   1399  1.1.1.5  christos 	    PLT_ENTRY_SIZE;
   1400  1.1.1.5  christos 	}
   1401      1.1  christos     }
   1402      1.1  christos 
   1403      1.1  christos   /* Fill in the first three entries in the global offset table.  */
   1404      1.1  christos   if (sgot && sgot->size > 0)
   1405      1.1  christos     {
   1406      1.1  christos       if (sdyn == NULL)
   1407  1.1.1.5  christos 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
   1408      1.1  christos       else
   1409  1.1.1.5  christos 	bfd_put_32 (output_bfd,
   1410  1.1.1.5  christos 		    sdyn->output_section->vma + sdyn->output_offset,
   1411  1.1.1.5  christos 		    sgot->contents);
   1412      1.1  christos       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
   1413      1.1  christos       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
   1414      1.1  christos 
   1415      1.1  christos       /* FIXME:  This can be null if create_dynamic_sections wasn't called. */
   1416      1.1  christos       if (elf_section_data (sgot->output_section) != NULL)
   1417  1.1.1.5  christos 	elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
   1418      1.1  christos     }
   1419      1.1  christos 
   1420      1.1  christos   if (lm32fdpic_fixup32_section (info))
   1421      1.1  christos     {
   1422      1.1  christos       struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
   1423      1.1  christos       bfd_vma got_value = hgot->root.u.def.value
   1424  1.1.1.5  christos 	    + hgot->root.u.def.section->output_section->vma
   1425  1.1.1.5  christos 	    + hgot->root.u.def.section->output_offset;
   1426      1.1  christos       struct bfd_link_hash_entry *hend;
   1427      1.1  christos 
   1428      1.1  christos       /* Last entry is pointer to GOT.  */
   1429      1.1  christos       _lm32fdpic_add_rofixup (output_bfd, lm32fdpic_fixup32_section (info), got_value);
   1430      1.1  christos 
   1431      1.1  christos       /* Check we wrote enough entries.  */
   1432      1.1  christos       if (lm32fdpic_fixup32_section (info)->size
   1433  1.1.1.5  christos 	      != (lm32fdpic_fixup32_section (info)->reloc_count * 4))
   1434  1.1.1.5  christos 	{
   1435  1.1.1.5  christos 	  _bfd_error_handler
   1436  1.1.1.6  christos 	    ("LINKER BUG: .rofixup section size mismatch: size/4 %" PRId64
   1437  1.1.1.6  christos 	     " != relocs %d",
   1438  1.1.1.6  christos 	    (int64_t) (lm32fdpic_fixup32_section (info)->size / 4),
   1439  1.1.1.5  christos 	    lm32fdpic_fixup32_section (info)->reloc_count);
   1440  1.1.1.5  christos 	  return FALSE;
   1441  1.1.1.5  christos 	}
   1442      1.1  christos 
   1443      1.1  christos       hend = bfd_link_hash_lookup (info->hash, "__ROFIXUP_END__",
   1444  1.1.1.5  christos 	      FALSE, FALSE, TRUE);
   1445      1.1  christos       if (hend
   1446  1.1.1.5  christos 	  && (hend->type == bfd_link_hash_defined
   1447  1.1.1.5  christos 	      || hend->type == bfd_link_hash_defweak))
   1448  1.1.1.5  christos 	{
   1449  1.1.1.5  christos 	  bfd_vma value =
   1450  1.1.1.5  christos 	    lm32fdpic_fixup32_section (info)->output_section->vma
   1451  1.1.1.5  christos 	    + lm32fdpic_fixup32_section (info)->output_offset
   1452  1.1.1.5  christos 	    + lm32fdpic_fixup32_section (info)->size
   1453  1.1.1.5  christos 	    - hend->u.def.section->output_section->vma
   1454  1.1.1.5  christos 	    - hend->u.def.section->output_offset;
   1455  1.1.1.5  christos 	  BFD_ASSERT (hend->u.def.value == value);
   1456  1.1.1.5  christos 	  if (hend->u.def.value != value)
   1457  1.1.1.5  christos 	    {
   1458  1.1.1.5  christos 	      _bfd_error_handler
   1459  1.1.1.6  christos 		("LINKER BUG: .rofixup section hend->u.def.value != value: %"
   1460  1.1.1.6  christos 		 PRId64 " != %" PRId64,
   1461  1.1.1.6  christos 		 (int64_t) hend->u.def.value, (int64_t) value);
   1462  1.1.1.5  christos 	      return FALSE;
   1463  1.1.1.5  christos 	    }
   1464  1.1.1.5  christos 	}
   1465      1.1  christos     }
   1466      1.1  christos 
   1467      1.1  christos   return TRUE;
   1468      1.1  christos }
   1469      1.1  christos 
   1470      1.1  christos /* Finish up dynamic symbol handling.  We set the contents of various
   1471      1.1  christos    dynamic sections here.  */
   1472      1.1  christos 
   1473      1.1  christos static bfd_boolean
   1474      1.1  christos lm32_elf_finish_dynamic_symbol (bfd *output_bfd,
   1475      1.1  christos 				struct bfd_link_info *info,
   1476      1.1  christos 				struct elf_link_hash_entry *h,
   1477      1.1  christos 				Elf_Internal_Sym *sym)
   1478      1.1  christos {
   1479      1.1  christos   struct elf_lm32_link_hash_table *htab;
   1480      1.1  christos   bfd_byte *loc;
   1481      1.1  christos 
   1482      1.1  christos   htab = lm32_elf_hash_table (info);
   1483      1.1  christos   if (htab == NULL)
   1484      1.1  christos     return FALSE;
   1485      1.1  christos 
   1486      1.1  christos   if (h->plt.offset != (bfd_vma) -1)
   1487      1.1  christos     {
   1488      1.1  christos       asection *splt;
   1489      1.1  christos       asection *sgot;
   1490      1.1  christos       asection *srela;
   1491      1.1  christos 
   1492      1.1  christos       bfd_vma plt_index;
   1493      1.1  christos       bfd_vma got_offset;
   1494      1.1  christos       Elf_Internal_Rela rela;
   1495      1.1  christos 
   1496      1.1  christos       /* This symbol has an entry in the procedure linkage table.  Set
   1497  1.1.1.5  christos 	 it up.  */
   1498      1.1  christos       BFD_ASSERT (h->dynindx != -1);
   1499      1.1  christos 
   1500  1.1.1.5  christos       splt = htab->root.splt;
   1501  1.1.1.5  christos       sgot = htab->root.sgotplt;
   1502  1.1.1.5  christos       srela = htab->root.srelplt;
   1503      1.1  christos       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
   1504      1.1  christos 
   1505      1.1  christos       /* Get the index in the procedure linkage table which
   1506  1.1.1.5  christos 	 corresponds to this symbol.  This is the index of this symbol
   1507  1.1.1.5  christos 	 in all the symbols for which we are making plt entries.  The
   1508  1.1.1.5  christos 	 first entry in the procedure linkage table is reserved.  */
   1509      1.1  christos       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
   1510      1.1  christos 
   1511      1.1  christos       /* Get the offset into the .got table of the entry that
   1512  1.1.1.5  christos 	corresponds to this function.  Each .got entry is 4 bytes.
   1513  1.1.1.5  christos 	The first three are reserved.  */
   1514      1.1  christos       got_offset = (plt_index + 3) * 4;
   1515      1.1  christos 
   1516      1.1  christos       /* Fill in the entry in the procedure linkage table.  */
   1517  1.1.1.3  christos       if (! bfd_link_pic (info))
   1518  1.1.1.5  christos 	{
   1519  1.1.1.5  christos 	  /* TODO */
   1520  1.1.1.5  christos 	}
   1521      1.1  christos       else
   1522  1.1.1.5  christos 	{
   1523  1.1.1.5  christos 	  /* TODO */
   1524  1.1.1.5  christos 	}
   1525      1.1  christos 
   1526      1.1  christos       /* Fill in the entry in the global offset table.  */
   1527      1.1  christos       bfd_put_32 (output_bfd,
   1528  1.1.1.5  christos 		  (splt->output_section->vma
   1529  1.1.1.5  christos 		   + splt->output_offset
   1530  1.1.1.5  christos 		   + h->plt.offset
   1531  1.1.1.5  christos 		   + 12), /* same offset */
   1532  1.1.1.5  christos 		  sgot->contents + got_offset);
   1533      1.1  christos 
   1534      1.1  christos       /* Fill in the entry in the .rela.plt section.  */
   1535      1.1  christos       rela.r_offset = (sgot->output_section->vma
   1536  1.1.1.5  christos 		       + sgot->output_offset
   1537  1.1.1.5  christos 		       + got_offset);
   1538      1.1  christos       rela.r_info = ELF32_R_INFO (h->dynindx, R_LM32_JMP_SLOT);
   1539      1.1  christos       rela.r_addend = 0;
   1540      1.1  christos       loc = srela->contents;
   1541      1.1  christos       loc += plt_index * sizeof (Elf32_External_Rela);
   1542      1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   1543      1.1  christos 
   1544      1.1  christos       if (!h->def_regular)
   1545  1.1.1.5  christos 	{
   1546  1.1.1.5  christos 	  /* Mark the symbol as undefined, rather than as defined in
   1547  1.1.1.5  christos 	     the .plt section.  Leave the value alone.  */
   1548  1.1.1.5  christos 	  sym->st_shndx = SHN_UNDEF;
   1549  1.1.1.5  christos 	}
   1550      1.1  christos 
   1551      1.1  christos     }
   1552      1.1  christos 
   1553      1.1  christos   if (h->got.offset != (bfd_vma) -1)
   1554      1.1  christos     {
   1555      1.1  christos       asection *sgot;
   1556      1.1  christos       asection *srela;
   1557      1.1  christos       Elf_Internal_Rela rela;
   1558      1.1  christos 
   1559      1.1  christos       /* This symbol has an entry in the global offset table.  Set it
   1560  1.1.1.5  christos 	 up.  */
   1561  1.1.1.5  christos       sgot = htab->root.sgot;
   1562  1.1.1.5  christos       srela = htab->root.srelgot;
   1563      1.1  christos       BFD_ASSERT (sgot != NULL && srela != NULL);
   1564      1.1  christos 
   1565      1.1  christos       rela.r_offset = (sgot->output_section->vma
   1566  1.1.1.5  christos 		       + sgot->output_offset
   1567  1.1.1.5  christos 		       + (h->got.offset &~ 1));
   1568      1.1  christos 
   1569      1.1  christos       /* If this is a -Bsymbolic link, and the symbol is defined
   1570  1.1.1.5  christos 	 locally, we just want to emit a RELATIVE reloc.  Likewise if
   1571  1.1.1.5  christos 	 the symbol was forced to be local because of a version file.
   1572  1.1.1.5  christos 	 The entry in the global offset table will already have been
   1573  1.1.1.5  christos 	 initialized in the relocate_section function.  */
   1574  1.1.1.3  christos       if (bfd_link_pic (info)
   1575  1.1.1.5  christos 	  && (info->symbolic
   1576      1.1  christos 	      || h->dynindx == -1
   1577      1.1  christos 	      || h->forced_local)
   1578  1.1.1.5  christos 	  && h->def_regular)
   1579  1.1.1.5  christos 	{
   1580  1.1.1.5  christos 	  rela.r_info = ELF32_R_INFO (0, R_LM32_RELATIVE);
   1581  1.1.1.5  christos 	  rela.r_addend = (h->root.u.def.value
   1582  1.1.1.5  christos 			   + h->root.u.def.section->output_section->vma
   1583  1.1.1.5  christos 			   + h->root.u.def.section->output_offset);
   1584  1.1.1.5  christos 	}
   1585      1.1  christos       else
   1586  1.1.1.5  christos 	{
   1587      1.1  christos 	  BFD_ASSERT ((h->got.offset & 1) == 0);
   1588  1.1.1.5  christos 	  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
   1589  1.1.1.5  christos 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_LM32_GLOB_DAT);
   1590  1.1.1.5  christos 	  rela.r_addend = 0;
   1591  1.1.1.5  christos 	}
   1592      1.1  christos 
   1593      1.1  christos       loc = srela->contents;
   1594      1.1  christos       loc += srela->reloc_count * sizeof (Elf32_External_Rela);
   1595      1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   1596      1.1  christos       ++srela->reloc_count;
   1597      1.1  christos     }
   1598      1.1  christos 
   1599      1.1  christos   if (h->needs_copy)
   1600      1.1  christos     {
   1601      1.1  christos       asection *s;
   1602      1.1  christos       Elf_Internal_Rela rela;
   1603      1.1  christos 
   1604      1.1  christos       /* This symbols needs a copy reloc.  Set it up.  */
   1605      1.1  christos       BFD_ASSERT (h->dynindx != -1
   1606  1.1.1.5  christos 		  && (h->root.type == bfd_link_hash_defined
   1607  1.1.1.5  christos 		      || h->root.type == bfd_link_hash_defweak));
   1608      1.1  christos 
   1609  1.1.1.2  christos       s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
   1610      1.1  christos       BFD_ASSERT (s != NULL);
   1611      1.1  christos 
   1612      1.1  christos       rela.r_offset = (h->root.u.def.value
   1613  1.1.1.5  christos 		       + h->root.u.def.section->output_section->vma
   1614  1.1.1.5  christos 		       + h->root.u.def.section->output_offset);
   1615      1.1  christos       rela.r_info = ELF32_R_INFO (h->dynindx, R_LM32_COPY);
   1616      1.1  christos       rela.r_addend = 0;
   1617      1.1  christos       loc = s->contents;
   1618      1.1  christos       loc += s->reloc_count * sizeof (Elf32_External_Rela);
   1619      1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   1620      1.1  christos       ++s->reloc_count;
   1621      1.1  christos     }
   1622      1.1  christos 
   1623      1.1  christos   /* Mark some specially defined symbols as absolute.  */
   1624  1.1.1.3  christos   if (h == htab->root.hdynamic || h == htab->root.hgot)
   1625      1.1  christos     sym->st_shndx = SHN_ABS;
   1626      1.1  christos 
   1627      1.1  christos   return TRUE;
   1628      1.1  christos }
   1629      1.1  christos 
   1630      1.1  christos static enum elf_reloc_type_class
   1631  1.1.1.3  christos lm32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1632  1.1.1.3  christos 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   1633  1.1.1.3  christos 			   const Elf_Internal_Rela *rela)
   1634      1.1  christos {
   1635      1.1  christos   switch ((int) ELF32_R_TYPE (rela->r_info))
   1636      1.1  christos     {
   1637      1.1  christos     case R_LM32_RELATIVE:  return reloc_class_relative;
   1638      1.1  christos     case R_LM32_JMP_SLOT:  return reloc_class_plt;
   1639      1.1  christos     case R_LM32_COPY:      return reloc_class_copy;
   1640  1.1.1.5  christos     default:		   return reloc_class_normal;
   1641      1.1  christos     }
   1642      1.1  christos }
   1643      1.1  christos 
   1644  1.1.1.5  christos /* Find dynamic relocs for H that apply to read-only sections.  */
   1645  1.1.1.5  christos 
   1646  1.1.1.5  christos static asection *
   1647  1.1.1.5  christos readonly_dynrelocs (struct elf_link_hash_entry *h)
   1648  1.1.1.5  christos {
   1649  1.1.1.5  christos   struct elf_dyn_relocs *p;
   1650  1.1.1.5  christos   struct elf_lm32_link_hash_entry *eh = (struct elf_lm32_link_hash_entry *) h;
   1651  1.1.1.5  christos 
   1652  1.1.1.5  christos   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   1653  1.1.1.5  christos     {
   1654  1.1.1.5  christos       asection *s = p->sec->output_section;
   1655  1.1.1.5  christos 
   1656  1.1.1.5  christos       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   1657  1.1.1.5  christos 	return p->sec;
   1658  1.1.1.5  christos     }
   1659  1.1.1.5  christos   return NULL;
   1660  1.1.1.5  christos }
   1661  1.1.1.5  christos 
   1662      1.1  christos /* Adjust a symbol defined by a dynamic object and referenced by a
   1663      1.1  christos    regular object.  The current definition is in some section of the
   1664      1.1  christos    dynamic object, but we're not including those sections.  We have to
   1665      1.1  christos    change the definition to something the rest of the link can
   1666      1.1  christos    understand.  */
   1667      1.1  christos 
   1668      1.1  christos static bfd_boolean
   1669      1.1  christos lm32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   1670      1.1  christos 				struct elf_link_hash_entry *h)
   1671      1.1  christos {
   1672      1.1  christos   struct elf_lm32_link_hash_table *htab;
   1673      1.1  christos   bfd *dynobj;
   1674      1.1  christos   asection *s;
   1675      1.1  christos 
   1676      1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   1677      1.1  christos 
   1678      1.1  christos   /* Make sure we know what is going on here.  */
   1679      1.1  christos   BFD_ASSERT (dynobj != NULL
   1680  1.1.1.5  christos 	      && (h->needs_plt
   1681  1.1.1.5  christos 		  || h->is_weakalias
   1682  1.1.1.5  christos 		  || (h->def_dynamic
   1683  1.1.1.5  christos 		      && h->ref_regular
   1684  1.1.1.5  christos 		      && !h->def_regular)));
   1685      1.1  christos 
   1686      1.1  christos   /* If this is a function, put it in the procedure linkage table.  We
   1687      1.1  christos      will fill in the contents of the procedure linkage table later,
   1688      1.1  christos      when we know the address of the .got section.  */
   1689      1.1  christos   if (h->type == STT_FUNC
   1690      1.1  christos       || h->needs_plt)
   1691      1.1  christos     {
   1692  1.1.1.3  christos       if (! bfd_link_pic (info)
   1693  1.1.1.5  christos 	  && !h->def_dynamic
   1694  1.1.1.5  christos 	  && !h->ref_dynamic
   1695      1.1  christos 	  && h->root.type != bfd_link_hash_undefweak
   1696      1.1  christos 	  && h->root.type != bfd_link_hash_undefined)
   1697  1.1.1.5  christos 	{
   1698  1.1.1.5  christos 	  /* This case can occur if we saw a PLT reloc in an input
   1699  1.1.1.5  christos 	     file, but the symbol was never referred to by a dynamic
   1700  1.1.1.5  christos 	     object.  In such a case, we don't actually need to build
   1701  1.1.1.5  christos 	     a procedure linkage table, and we can just do a PCREL
   1702  1.1.1.5  christos 	     reloc instead.  */
   1703  1.1.1.5  christos 	  h->plt.offset = (bfd_vma) -1;
   1704  1.1.1.5  christos 	  h->needs_plt = 0;
   1705  1.1.1.5  christos 	}
   1706      1.1  christos 
   1707      1.1  christos       return TRUE;
   1708      1.1  christos     }
   1709      1.1  christos   else
   1710      1.1  christos     h->plt.offset = (bfd_vma) -1;
   1711      1.1  christos 
   1712      1.1  christos   /* If this is a weak symbol, and there is a real definition, the
   1713      1.1  christos      processor independent code will have arranged for us to see the
   1714      1.1  christos      real definition first, and we can just use the same value.  */
   1715  1.1.1.5  christos   if (h->is_weakalias)
   1716      1.1  christos     {
   1717  1.1.1.5  christos       struct elf_link_hash_entry *def = weakdef (h);
   1718  1.1.1.5  christos       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   1719  1.1.1.5  christos       h->root.u.def.section = def->root.u.def.section;
   1720  1.1.1.5  christos       h->root.u.def.value = def->root.u.def.value;
   1721      1.1  christos       return TRUE;
   1722      1.1  christos     }
   1723      1.1  christos 
   1724      1.1  christos   /* This is a reference to a symbol defined by a dynamic object which
   1725      1.1  christos      is not a function.  */
   1726      1.1  christos 
   1727      1.1  christos   /* If we are creating a shared library, we must presume that the
   1728      1.1  christos      only references to the symbol are via the global offset table.
   1729      1.1  christos      For such cases we need not do anything here; the relocations will
   1730      1.1  christos      be handled correctly by relocate_section.  */
   1731  1.1.1.3  christos   if (bfd_link_pic (info))
   1732      1.1  christos     return TRUE;
   1733      1.1  christos 
   1734      1.1  christos   /* If there are no references to this symbol that do not use the
   1735      1.1  christos      GOT, we don't need to generate a copy reloc.  */
   1736      1.1  christos   if (!h->non_got_ref)
   1737      1.1  christos     return TRUE;
   1738      1.1  christos 
   1739      1.1  christos   /* If -z nocopyreloc was given, we won't generate them either.  */
   1740  1.1.1.5  christos   if (0 && info->nocopyreloc)
   1741      1.1  christos     {
   1742      1.1  christos       h->non_got_ref = 0;
   1743      1.1  christos       return TRUE;
   1744      1.1  christos     }
   1745      1.1  christos 
   1746  1.1.1.5  christos   /* If we don't find any dynamic relocs in read-only sections, then
   1747  1.1.1.5  christos      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   1748  1.1.1.5  christos   if (0 && !readonly_dynrelocs (h))
   1749      1.1  christos     {
   1750      1.1  christos       h->non_got_ref = 0;
   1751      1.1  christos       return TRUE;
   1752      1.1  christos     }
   1753      1.1  christos 
   1754      1.1  christos   /* We must allocate the symbol in our .dynbss section, which will
   1755      1.1  christos      become part of the .bss section of the executable.  There will be
   1756      1.1  christos      an entry for this symbol in the .dynsym section.  The dynamic
   1757      1.1  christos      object will contain position independent code, so all references
   1758      1.1  christos      from the dynamic object to this symbol will go through the global
   1759      1.1  christos      offset table.  The dynamic linker will use the .dynsym entry to
   1760      1.1  christos      determine the address it must put in the global offset table, so
   1761      1.1  christos      both the dynamic object and the regular object will refer to the
   1762      1.1  christos      same memory location for the variable.  */
   1763      1.1  christos 
   1764      1.1  christos   htab = lm32_elf_hash_table (info);
   1765      1.1  christos   if (htab == NULL)
   1766      1.1  christos     return FALSE;
   1767      1.1  christos 
   1768      1.1  christos   s = htab->sdynbss;
   1769      1.1  christos   BFD_ASSERT (s != NULL);
   1770      1.1  christos 
   1771      1.1  christos   /* We must generate a R_LM32_COPY reloc to tell the dynamic linker
   1772      1.1  christos      to copy the initial value out of the dynamic object and into the
   1773      1.1  christos      runtime process image.  We need to remember the offset into the
   1774      1.1  christos      .rela.bss section we are going to use.  */
   1775  1.1.1.2  christos   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   1776      1.1  christos     {
   1777      1.1  christos       asection *srel;
   1778      1.1  christos 
   1779      1.1  christos       srel = htab->srelbss;
   1780      1.1  christos       BFD_ASSERT (srel != NULL);
   1781      1.1  christos       srel->size += sizeof (Elf32_External_Rela);
   1782      1.1  christos       h->needs_copy = 1;
   1783      1.1  christos     }
   1784      1.1  christos 
   1785  1.1.1.3  christos   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   1786      1.1  christos }
   1787      1.1  christos 
   1788      1.1  christos /* Allocate space in .plt, .got and associated reloc sections for
   1789      1.1  christos    dynamic relocs.  */
   1790      1.1  christos 
   1791      1.1  christos static bfd_boolean
   1792      1.1  christos allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
   1793      1.1  christos {
   1794      1.1  christos   struct bfd_link_info *info;
   1795      1.1  christos   struct elf_lm32_link_hash_table *htab;
   1796      1.1  christos   struct elf_lm32_link_hash_entry *eh;
   1797  1.1.1.5  christos   struct elf_dyn_relocs *p;
   1798      1.1  christos 
   1799      1.1  christos   if (h->root.type == bfd_link_hash_indirect)
   1800      1.1  christos     return TRUE;
   1801      1.1  christos 
   1802      1.1  christos   info = (struct bfd_link_info *) inf;
   1803      1.1  christos   htab = lm32_elf_hash_table (info);
   1804      1.1  christos   if (htab == NULL)
   1805      1.1  christos     return FALSE;
   1806      1.1  christos 
   1807      1.1  christos   eh = (struct elf_lm32_link_hash_entry *) h;
   1808      1.1  christos 
   1809      1.1  christos   if (htab->root.dynamic_sections_created
   1810      1.1  christos       && h->plt.refcount > 0)
   1811      1.1  christos     {
   1812      1.1  christos       /* Make sure this symbol is output as a dynamic symbol.
   1813  1.1.1.5  christos 	 Undefined weak syms won't yet be marked as dynamic.  */
   1814      1.1  christos       if (h->dynindx == -1
   1815  1.1.1.5  christos 	  && !h->forced_local)
   1816  1.1.1.5  christos 	{
   1817  1.1.1.5  christos 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   1818  1.1.1.5  christos 	    return FALSE;
   1819  1.1.1.5  christos 	}
   1820      1.1  christos 
   1821  1.1.1.3  christos       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
   1822  1.1.1.5  christos 	{
   1823  1.1.1.5  christos 	  asection *s = htab->root.splt;
   1824  1.1.1.5  christos 
   1825  1.1.1.5  christos 	  /* If this is the first .plt entry, make room for the special
   1826  1.1.1.5  christos 	     first entry.  */
   1827  1.1.1.5  christos 	  if (s->size == 0)
   1828  1.1.1.5  christos 	    s->size += PLT_ENTRY_SIZE;
   1829  1.1.1.5  christos 
   1830  1.1.1.5  christos 	  h->plt.offset = s->size;
   1831  1.1.1.5  christos 
   1832  1.1.1.5  christos 	  /* If this symbol is not defined in a regular file, and we are
   1833  1.1.1.5  christos 	     not generating a shared library, then set the symbol to this
   1834  1.1.1.5  christos 	     location in the .plt.  This is required to make function
   1835  1.1.1.5  christos 	     pointers compare as equal between the normal executable and
   1836  1.1.1.5  christos 	     the shared library.  */
   1837  1.1.1.5  christos 	  if (! bfd_link_pic (info)
   1838  1.1.1.5  christos 	      && !h->def_regular)
   1839  1.1.1.5  christos 	    {
   1840  1.1.1.5  christos 	      h->root.u.def.section = s;
   1841  1.1.1.5  christos 	      h->root.u.def.value = h->plt.offset;
   1842  1.1.1.5  christos 	    }
   1843  1.1.1.5  christos 
   1844  1.1.1.5  christos 	  /* Make room for this entry.  */
   1845  1.1.1.5  christos 	  s->size += PLT_ENTRY_SIZE;
   1846  1.1.1.5  christos 
   1847  1.1.1.5  christos 	  /* We also need to make an entry in the .got.plt section, which
   1848  1.1.1.5  christos 	     will be placed in the .got section by the linker script.  */
   1849  1.1.1.5  christos 	  htab->root.sgotplt->size += 4;
   1850      1.1  christos 
   1851  1.1.1.5  christos 	  /* We also need to make an entry in the .rel.plt section.  */
   1852  1.1.1.5  christos 	  htab->root.srelplt->size += sizeof (Elf32_External_Rela);
   1853  1.1.1.5  christos 	}
   1854      1.1  christos       else
   1855  1.1.1.5  christos 	{
   1856  1.1.1.5  christos 	  h->plt.offset = (bfd_vma) -1;
   1857  1.1.1.5  christos 	  h->needs_plt = 0;
   1858  1.1.1.5  christos 	}
   1859      1.1  christos     }
   1860      1.1  christos   else
   1861      1.1  christos     {
   1862      1.1  christos       h->plt.offset = (bfd_vma) -1;
   1863      1.1  christos       h->needs_plt = 0;
   1864      1.1  christos     }
   1865      1.1  christos 
   1866      1.1  christos   if (h->got.refcount > 0)
   1867      1.1  christos     {
   1868      1.1  christos       asection *s;
   1869      1.1  christos       bfd_boolean dyn;
   1870      1.1  christos 
   1871      1.1  christos       /* Make sure this symbol is output as a dynamic symbol.
   1872  1.1.1.5  christos 	 Undefined weak syms won't yet be marked as dynamic.  */
   1873      1.1  christos       if (h->dynindx == -1
   1874  1.1.1.5  christos 	  && !h->forced_local)
   1875  1.1.1.5  christos 	{
   1876  1.1.1.5  christos 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   1877  1.1.1.5  christos 	    return FALSE;
   1878  1.1.1.5  christos 	}
   1879      1.1  christos 
   1880  1.1.1.5  christos       s = htab->root.sgot;
   1881      1.1  christos 
   1882      1.1  christos       h->got.offset = s->size;
   1883      1.1  christos       s->size += 4;
   1884      1.1  christos       dyn = htab->root.dynamic_sections_created;
   1885  1.1.1.3  christos       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
   1886  1.1.1.5  christos 	htab->root.srelgot->size += sizeof (Elf32_External_Rela);
   1887      1.1  christos     }
   1888      1.1  christos   else
   1889      1.1  christos     h->got.offset = (bfd_vma) -1;
   1890      1.1  christos 
   1891      1.1  christos   if (eh->dyn_relocs == NULL)
   1892      1.1  christos     return TRUE;
   1893      1.1  christos 
   1894      1.1  christos   /* In the shared -Bsymbolic case, discard space allocated for
   1895      1.1  christos      dynamic pc-relative relocs against symbols which turn out to be
   1896      1.1  christos      defined in regular objects.  For the normal shared case, discard
   1897      1.1  christos      space for pc-relative relocs that have become local due to symbol
   1898      1.1  christos      visibility changes.  */
   1899      1.1  christos 
   1900  1.1.1.3  christos   if (bfd_link_pic (info))
   1901      1.1  christos     {
   1902      1.1  christos       if (h->def_regular
   1903  1.1.1.5  christos 	  && (h->forced_local
   1904  1.1.1.5  christos 	      || info->symbolic))
   1905  1.1.1.5  christos 	{
   1906  1.1.1.5  christos 	  struct elf_dyn_relocs **pp;
   1907  1.1.1.5  christos 
   1908  1.1.1.5  christos 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
   1909  1.1.1.5  christos 	    {
   1910  1.1.1.5  christos 	      p->count -= p->pc_count;
   1911  1.1.1.5  christos 	      p->pc_count = 0;
   1912  1.1.1.5  christos 	      if (p->count == 0)
   1913  1.1.1.5  christos 		*pp = p->next;
   1914  1.1.1.5  christos 	      else
   1915  1.1.1.5  christos 		pp = &p->next;
   1916  1.1.1.5  christos 	    }
   1917  1.1.1.5  christos 	}
   1918      1.1  christos 
   1919      1.1  christos       /* Also discard relocs on undefined weak syms with non-default
   1920      1.1  christos 	 visibility.  */
   1921      1.1  christos       if (eh->dyn_relocs != NULL
   1922      1.1  christos 	  && h->root.type == bfd_link_hash_undefweak)
   1923      1.1  christos 	{
   1924      1.1  christos 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   1925      1.1  christos 	    eh->dyn_relocs = NULL;
   1926      1.1  christos 
   1927      1.1  christos 	  /* Make sure undefined weak symbols are output as a dynamic
   1928      1.1  christos 	     symbol in PIEs.  */
   1929      1.1  christos 	  else if (h->dynindx == -1
   1930      1.1  christos 		   && !h->forced_local)
   1931      1.1  christos 	    {
   1932      1.1  christos 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   1933      1.1  christos 		return FALSE;
   1934      1.1  christos 	    }
   1935      1.1  christos 	}
   1936      1.1  christos     }
   1937      1.1  christos   else
   1938      1.1  christos     {
   1939      1.1  christos       /* For the non-shared case, discard space for relocs against
   1940  1.1.1.5  christos 	 symbols which turn out to need copy relocs or are not
   1941  1.1.1.5  christos 	 dynamic.  */
   1942      1.1  christos 
   1943      1.1  christos       if (!h->non_got_ref
   1944  1.1.1.5  christos 	  && ((h->def_dynamic
   1945  1.1.1.5  christos 	       && !h->def_regular)
   1946  1.1.1.5  christos 	      || (htab->root.dynamic_sections_created
   1947  1.1.1.5  christos 		  && (h->root.type == bfd_link_hash_undefweak
   1948  1.1.1.5  christos 		      || h->root.type == bfd_link_hash_undefined))))
   1949  1.1.1.5  christos 	{
   1950  1.1.1.5  christos 	  /* Make sure this symbol is output as a dynamic symbol.
   1951  1.1.1.5  christos 	     Undefined weak syms won't yet be marked as dynamic.  */
   1952  1.1.1.5  christos 	  if (h->dynindx == -1
   1953  1.1.1.5  christos 	      && !h->forced_local)
   1954  1.1.1.5  christos 	    {
   1955  1.1.1.5  christos 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   1956  1.1.1.5  christos 		return FALSE;
   1957  1.1.1.5  christos 	    }
   1958  1.1.1.5  christos 
   1959  1.1.1.5  christos 	  /* If that succeeded, we know we'll be keeping all the
   1960  1.1.1.5  christos 	     relocs.  */
   1961  1.1.1.5  christos 	  if (h->dynindx != -1)
   1962  1.1.1.5  christos 	    goto keep;
   1963  1.1.1.5  christos 	}
   1964      1.1  christos 
   1965      1.1  christos       eh->dyn_relocs = NULL;
   1966      1.1  christos 
   1967      1.1  christos     keep: ;
   1968      1.1  christos     }
   1969      1.1  christos 
   1970      1.1  christos   /* Finally, allocate space.  */
   1971      1.1  christos   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   1972      1.1  christos     {
   1973      1.1  christos       asection *sreloc = elf_section_data (p->sec)->sreloc;
   1974      1.1  christos       sreloc->size += p->count * sizeof (Elf32_External_Rela);
   1975      1.1  christos     }
   1976      1.1  christos 
   1977      1.1  christos   return TRUE;
   1978      1.1  christos }
   1979      1.1  christos 
   1980  1.1.1.5  christos /* Set DF_TEXTREL if we find any dynamic relocs that apply to
   1981  1.1.1.5  christos    read-only sections.  */
   1982      1.1  christos 
   1983      1.1  christos static bfd_boolean
   1984  1.1.1.5  christos maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
   1985      1.1  christos {
   1986  1.1.1.5  christos   asection *sec;
   1987      1.1  christos 
   1988  1.1.1.5  christos   if (h->root.type == bfd_link_hash_indirect)
   1989  1.1.1.5  christos     return TRUE;
   1990      1.1  christos 
   1991  1.1.1.5  christos   sec = readonly_dynrelocs (h);
   1992  1.1.1.5  christos   if (sec != NULL)
   1993  1.1.1.5  christos     {
   1994  1.1.1.5  christos       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
   1995      1.1  christos 
   1996  1.1.1.5  christos       info->flags |= DF_TEXTREL;
   1997  1.1.1.5  christos       info->callbacks->minfo
   1998  1.1.1.6  christos 	(_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
   1999  1.1.1.5  christos 	 sec->owner, h->root.root.string, sec);
   2000      1.1  christos 
   2001  1.1.1.5  christos       /* Not an error, just cut short the traversal.  */
   2002  1.1.1.5  christos       return FALSE;
   2003      1.1  christos     }
   2004      1.1  christos   return TRUE;
   2005      1.1  christos }
   2006      1.1  christos 
   2007      1.1  christos /* Set the sizes of the dynamic sections.  */
   2008      1.1  christos 
   2009      1.1  christos static bfd_boolean
   2010      1.1  christos lm32_elf_size_dynamic_sections (bfd *output_bfd,
   2011  1.1.1.5  christos 				struct bfd_link_info *info)
   2012      1.1  christos {
   2013      1.1  christos   struct elf_lm32_link_hash_table *htab;
   2014      1.1  christos   bfd *dynobj;
   2015      1.1  christos   asection *s;
   2016      1.1  christos   bfd_boolean relocs;
   2017      1.1  christos   bfd *ibfd;
   2018      1.1  christos 
   2019      1.1  christos   htab = lm32_elf_hash_table (info);
   2020      1.1  christos   if (htab == NULL)
   2021      1.1  christos     return FALSE;
   2022      1.1  christos 
   2023      1.1  christos   dynobj = htab->root.dynobj;
   2024      1.1  christos   BFD_ASSERT (dynobj != NULL);
   2025      1.1  christos 
   2026      1.1  christos   if (htab->root.dynamic_sections_created)
   2027      1.1  christos     {
   2028      1.1  christos       /* Set the contents of the .interp section to the interpreter.  */
   2029  1.1.1.3  christos       if (bfd_link_executable (info) && !info->nointerp)
   2030      1.1  christos 	{
   2031  1.1.1.2  christos 	  s = bfd_get_linker_section (dynobj, ".interp");
   2032      1.1  christos 	  BFD_ASSERT (s != NULL);
   2033      1.1  christos 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   2034      1.1  christos 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   2035      1.1  christos 	}
   2036      1.1  christos     }
   2037      1.1  christos 
   2038      1.1  christos   /* Set up .got offsets for local syms, and space for local dynamic
   2039      1.1  christos      relocs.  */
   2040  1.1.1.3  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   2041      1.1  christos     {
   2042      1.1  christos       bfd_signed_vma *local_got;
   2043      1.1  christos       bfd_signed_vma *end_local_got;
   2044      1.1  christos       bfd_size_type locsymcount;
   2045      1.1  christos       Elf_Internal_Shdr *symtab_hdr;
   2046      1.1  christos       asection *srel;
   2047      1.1  christos 
   2048      1.1  christos       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   2049  1.1.1.5  christos 	continue;
   2050      1.1  christos 
   2051      1.1  christos       for (s = ibfd->sections; s != NULL; s = s->next)
   2052  1.1.1.5  christos 	{
   2053  1.1.1.5  christos 	  struct elf_dyn_relocs *p;
   2054      1.1  christos 
   2055  1.1.1.5  christos 	  for (p = ((struct elf_dyn_relocs *)
   2056  1.1.1.5  christos 		    elf_section_data (s)->local_dynrel);
   2057  1.1.1.5  christos 	       p != NULL;
   2058  1.1.1.5  christos 	       p = p->next)
   2059  1.1.1.5  christos 	    {
   2060  1.1.1.5  christos 	      if (! bfd_is_abs_section (p->sec)
   2061  1.1.1.5  christos 		  && bfd_is_abs_section (p->sec->output_section))
   2062  1.1.1.5  christos 		{
   2063  1.1.1.5  christos 		  /* Input section has been discarded, either because
   2064  1.1.1.5  christos 		     it is a copy of a linkonce section or due to
   2065  1.1.1.5  christos 		     linker script /DISCARD/, so we'll be discarding
   2066  1.1.1.5  christos 		     the relocs too.  */
   2067  1.1.1.5  christos 		}
   2068  1.1.1.5  christos 	      else if (p->count != 0)
   2069  1.1.1.5  christos 		{
   2070  1.1.1.5  christos 		  srel = elf_section_data (p->sec)->sreloc;
   2071  1.1.1.5  christos 		  srel->size += p->count * sizeof (Elf32_External_Rela);
   2072  1.1.1.5  christos 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   2073  1.1.1.5  christos 		    info->flags |= DF_TEXTREL;
   2074  1.1.1.5  christos 		}
   2075  1.1.1.5  christos 	    }
   2076  1.1.1.5  christos 	}
   2077      1.1  christos 
   2078      1.1  christos       local_got = elf_local_got_refcounts (ibfd);
   2079      1.1  christos       if (!local_got)
   2080  1.1.1.5  christos 	continue;
   2081      1.1  christos 
   2082      1.1  christos       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
   2083      1.1  christos       locsymcount = symtab_hdr->sh_info;
   2084      1.1  christos       end_local_got = local_got + locsymcount;
   2085  1.1.1.5  christos       s = htab->root.sgot;
   2086  1.1.1.5  christos       srel = htab->root.srelgot;
   2087      1.1  christos       for (; local_got < end_local_got; ++local_got)
   2088  1.1.1.5  christos 	{
   2089  1.1.1.5  christos 	  if (*local_got > 0)
   2090  1.1.1.5  christos 	    {
   2091  1.1.1.5  christos 	      *local_got = s->size;
   2092  1.1.1.5  christos 	      s->size += 4;
   2093  1.1.1.5  christos 	      if (bfd_link_pic (info))
   2094  1.1.1.5  christos 		srel->size += sizeof (Elf32_External_Rela);
   2095  1.1.1.5  christos 	    }
   2096  1.1.1.5  christos 	  else
   2097  1.1.1.5  christos 	    *local_got = (bfd_vma) -1;
   2098  1.1.1.5  christos 	}
   2099      1.1  christos     }
   2100      1.1  christos 
   2101      1.1  christos   /* Allocate global sym .plt and .got entries, and space for global
   2102      1.1  christos      sym dynamic relocs.  */
   2103      1.1  christos   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
   2104      1.1  christos 
   2105      1.1  christos   /* We now have determined the sizes of the various dynamic sections.
   2106      1.1  christos      Allocate memory for them.  */
   2107      1.1  christos   relocs = FALSE;
   2108      1.1  christos   for (s = dynobj->sections; s != NULL; s = s->next)
   2109      1.1  christos     {
   2110      1.1  christos       if ((s->flags & SEC_LINKER_CREATED) == 0)
   2111  1.1.1.5  christos 	continue;
   2112      1.1  christos 
   2113  1.1.1.5  christos       if (s == htab->root.splt
   2114  1.1.1.5  christos 	  || s == htab->root.sgot
   2115  1.1.1.5  christos 	  || s == htab->root.sgotplt
   2116      1.1  christos 	  || s == htab->sdynbss)
   2117  1.1.1.5  christos 	{
   2118  1.1.1.5  christos 	  /* Strip this section if we don't need it; see the
   2119  1.1.1.5  christos 	     comment below.  */
   2120  1.1.1.5  christos 	}
   2121      1.1  christos       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
   2122  1.1.1.5  christos 	{
   2123  1.1.1.5  christos 	  if (s->size != 0 && s != htab->root.srelplt)
   2124  1.1.1.5  christos 	    relocs = TRUE;
   2125  1.1.1.5  christos 
   2126  1.1.1.5  christos 	  /* We use the reloc_count field as a counter if we need
   2127  1.1.1.5  christos 	     to copy relocs into the output file.  */
   2128  1.1.1.5  christos 	  s->reloc_count = 0;
   2129  1.1.1.5  christos 	}
   2130      1.1  christos       else
   2131      1.1  christos 	/* It's not one of our sections, so don't allocate space.  */
   2132      1.1  christos 	continue;
   2133      1.1  christos 
   2134      1.1  christos       if (s->size == 0)
   2135  1.1.1.5  christos 	{
   2136  1.1.1.5  christos 	  /* If we don't need this section, strip it from the
   2137  1.1.1.5  christos 	     output file.  This is mostly to handle .rela.bss and
   2138  1.1.1.5  christos 	     .rela.plt.  We must create both sections in
   2139  1.1.1.5  christos 	     create_dynamic_sections, because they must be created
   2140  1.1.1.5  christos 	     before the linker maps input sections to output
   2141  1.1.1.5  christos 	     sections.  The linker does that before
   2142  1.1.1.5  christos 	     adjust_dynamic_symbol is called, and it is that
   2143  1.1.1.5  christos 	     function which decides whether anything needs to go
   2144  1.1.1.5  christos 	     into these sections.  */
   2145  1.1.1.5  christos 	  s->flags |= SEC_EXCLUDE;
   2146  1.1.1.5  christos 	  continue;
   2147  1.1.1.5  christos 	}
   2148      1.1  christos 
   2149      1.1  christos       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   2150      1.1  christos 	continue;
   2151      1.1  christos 
   2152      1.1  christos       /* Allocate memory for the section contents.  We use bfd_zalloc
   2153  1.1.1.5  christos 	 here in case unused entries are not reclaimed before the
   2154  1.1.1.5  christos 	 section's contents are written out.  This should not happen,
   2155  1.1.1.5  christos 	 but this way if it does, we get a R_LM32_NONE reloc instead
   2156  1.1.1.5  christos 	 of garbage.  */
   2157      1.1  christos       s->contents = bfd_zalloc (dynobj, s->size);
   2158      1.1  christos       if (s->contents == NULL)
   2159  1.1.1.5  christos 	return FALSE;
   2160      1.1  christos     }
   2161      1.1  christos 
   2162      1.1  christos   if (htab->root.dynamic_sections_created)
   2163      1.1  christos     {
   2164      1.1  christos       /* Add some entries to the .dynamic section.  We fill in the
   2165      1.1  christos 	 values later, in lm32_elf_finish_dynamic_sections, but we
   2166      1.1  christos 	 must add the entries now so that we get the correct size for
   2167      1.1  christos 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   2168      1.1  christos 	 dynamic linker and used by the debugger.  */
   2169      1.1  christos #define add_dynamic_entry(TAG, VAL) \
   2170      1.1  christos   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   2171      1.1  christos 
   2172  1.1.1.3  christos      if (bfd_link_executable (info))
   2173      1.1  christos 	{
   2174      1.1  christos 	  if (! add_dynamic_entry (DT_DEBUG, 0))
   2175      1.1  christos 	    return FALSE;
   2176      1.1  christos 	}
   2177      1.1  christos 
   2178  1.1.1.5  christos       if (htab->root.splt->size != 0)
   2179  1.1.1.5  christos 	{
   2180  1.1.1.5  christos 	  if (! add_dynamic_entry (DT_PLTGOT, 0)
   2181  1.1.1.5  christos 	      || ! add_dynamic_entry (DT_PLTRELSZ, 0)
   2182  1.1.1.5  christos 	      || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
   2183  1.1.1.5  christos 	      || ! add_dynamic_entry (DT_JMPREL, 0))
   2184  1.1.1.5  christos 	    return FALSE;
   2185  1.1.1.5  christos 	}
   2186      1.1  christos 
   2187      1.1  christos       if (relocs)
   2188  1.1.1.5  christos 	{
   2189  1.1.1.5  christos 	  if (! add_dynamic_entry (DT_RELA, 0)
   2190  1.1.1.5  christos 	      || ! add_dynamic_entry (DT_RELASZ, 0)
   2191  1.1.1.5  christos 	      || ! add_dynamic_entry (DT_RELAENT,
   2192  1.1.1.5  christos 				      sizeof (Elf32_External_Rela)))
   2193  1.1.1.5  christos 	    return FALSE;
   2194  1.1.1.5  christos 
   2195  1.1.1.5  christos 	  /* If any dynamic relocs apply to a read-only section,
   2196  1.1.1.5  christos 	     then we need a DT_TEXTREL entry.  */
   2197  1.1.1.5  christos 	  if ((info->flags & DF_TEXTREL) == 0)
   2198  1.1.1.5  christos 	    elf_link_hash_traverse (&htab->root, maybe_set_textrel, info);
   2199  1.1.1.5  christos 
   2200  1.1.1.5  christos 	  if ((info->flags & DF_TEXTREL) != 0)
   2201  1.1.1.5  christos 	    {
   2202  1.1.1.5  christos 	      if (! add_dynamic_entry (DT_TEXTREL, 0))
   2203  1.1.1.5  christos 		return FALSE;
   2204  1.1.1.5  christos 	    }
   2205  1.1.1.5  christos 	}
   2206      1.1  christos     }
   2207      1.1  christos #undef add_dynamic_entry
   2208      1.1  christos 
   2209      1.1  christos   /* Allocate .rofixup section.  */
   2210      1.1  christos   if (IS_FDPIC (output_bfd))
   2211      1.1  christos     {
   2212      1.1  christos       struct weak_symbol_list *list_start = NULL, *list_end = NULL;
   2213      1.1  christos       int rgot_weak_count = 0;
   2214      1.1  christos       int r32_count = 0;
   2215      1.1  christos       int rgot_count = 0;
   2216      1.1  christos       /* Look for deleted sections.  */
   2217  1.1.1.3  christos       for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   2218  1.1.1.5  christos 	{
   2219  1.1.1.5  christos 	  for (s = ibfd->sections; s != NULL; s = s->next)
   2220  1.1.1.5  christos 	    {
   2221  1.1.1.5  christos 	      if (s->reloc_count)
   2222  1.1.1.5  christos 		{
   2223  1.1.1.5  christos 		  /* Count relocs that need .rofixup entires.  */
   2224  1.1.1.5  christos 		  Elf_Internal_Rela *internal_relocs, *end;
   2225  1.1.1.5  christos 		  internal_relocs = elf_section_data (s)->relocs;
   2226  1.1.1.5  christos 		  if (internal_relocs == NULL)
   2227  1.1.1.5  christos 		    internal_relocs = (_bfd_elf_link_read_relocs (ibfd, s, NULL, NULL, FALSE));
   2228  1.1.1.5  christos 		  if (internal_relocs != NULL)
   2229  1.1.1.5  christos 		    {
   2230  1.1.1.5  christos 		      end = internal_relocs + s->reloc_count;
   2231  1.1.1.5  christos 		      while (internal_relocs < end)
   2232  1.1.1.5  christos 			{
   2233  1.1.1.5  christos 			  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
   2234  1.1.1.5  christos 			  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   2235  1.1.1.5  christos 			  unsigned long r_symndx;
   2236  1.1.1.5  christos 			  struct elf_link_hash_entry *h;
   2237  1.1.1.5  christos 
   2238  1.1.1.5  christos 			  symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
   2239  1.1.1.5  christos 			  sym_hashes = elf_sym_hashes (ibfd);
   2240  1.1.1.5  christos 			  r_symndx = ELF32_R_SYM (internal_relocs->r_info);
   2241  1.1.1.5  christos 			  h = NULL;
   2242  1.1.1.5  christos 			  if (r_symndx < symtab_hdr->sh_info)
   2243  1.1.1.5  christos 			    {
   2244  1.1.1.5  christos 			    }
   2245  1.1.1.5  christos 			  else
   2246  1.1.1.5  christos 			    {
   2247  1.1.1.5  christos 			      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   2248  1.1.1.5  christos 			      while (h->root.type == bfd_link_hash_indirect
   2249  1.1.1.5  christos 				     || h->root.type == bfd_link_hash_warning)
   2250  1.1.1.5  christos 				h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2251  1.1.1.5  christos 			      }
   2252  1.1.1.5  christos 
   2253  1.1.1.5  christos 			  /* Don't generate entries for weak symbols.  */
   2254  1.1.1.5  christos 			  if (!h || (h && h->root.type != bfd_link_hash_undefweak))
   2255  1.1.1.5  christos 			    {
   2256  1.1.1.5  christos 			      if (!discarded_section (s) && !((bfd_get_section_flags (ibfd, s) & SEC_ALLOC) == 0))
   2257  1.1.1.5  christos 				{
   2258  1.1.1.5  christos 				  switch (ELF32_R_TYPE (internal_relocs->r_info))
   2259  1.1.1.5  christos 				    {
   2260  1.1.1.5  christos 				    case R_LM32_32:
   2261  1.1.1.5  christos 				      r32_count++;
   2262  1.1.1.5  christos 				      break;
   2263  1.1.1.5  christos 				    case R_LM32_16_GOT:
   2264  1.1.1.5  christos 				      rgot_count++;
   2265  1.1.1.5  christos 				      break;
   2266  1.1.1.5  christos 				    }
   2267  1.1.1.5  christos 				}
   2268  1.1.1.5  christos 			    }
   2269  1.1.1.5  christos 			  else
   2270  1.1.1.5  christos 			    {
   2271  1.1.1.5  christos 			      struct weak_symbol_list *current, *new_entry;
   2272  1.1.1.5  christos 			      /* Is this symbol already in the list?  */
   2273  1.1.1.5  christos 			      for (current = list_start; current; current = current->next)
   2274  1.1.1.5  christos 				{
   2275  1.1.1.5  christos 				  if (!strcmp (current->name, h->root.root.string))
   2276  1.1.1.5  christos 				    break;
   2277  1.1.1.5  christos 				}
   2278  1.1.1.5  christos 			      if (!current && !discarded_section (s) && (bfd_get_section_flags (ibfd, s) & SEC_ALLOC))
   2279  1.1.1.5  christos 				{
   2280  1.1.1.5  christos 				  /* Will this have an entry in the GOT.  */
   2281  1.1.1.5  christos 				  if (ELF32_R_TYPE (internal_relocs->r_info) == R_LM32_16_GOT)
   2282  1.1.1.5  christos 				    {
   2283  1.1.1.5  christos 				      /* Create a new entry.  */
   2284  1.1.1.5  christos 				      new_entry = malloc (sizeof (struct weak_symbol_list));
   2285  1.1.1.5  christos 				      if (!new_entry)
   2286  1.1.1.5  christos 					return FALSE;
   2287  1.1.1.5  christos 				      new_entry->name = h->root.root.string;
   2288  1.1.1.5  christos 				      new_entry->next = NULL;
   2289  1.1.1.5  christos 				      /* Add to list */
   2290  1.1.1.5  christos 				      if (list_start == NULL)
   2291  1.1.1.5  christos 					{
   2292  1.1.1.5  christos 					  list_start = new_entry;
   2293  1.1.1.5  christos 					  list_end = new_entry;
   2294  1.1.1.5  christos 					}
   2295  1.1.1.5  christos 				      else
   2296  1.1.1.5  christos 					{
   2297  1.1.1.5  christos 					  list_end->next = new_entry;
   2298  1.1.1.5  christos 					  list_end = new_entry;
   2299  1.1.1.5  christos 					}
   2300  1.1.1.5  christos 				      /* Increase count of undefined weak symbols in the got.  */
   2301  1.1.1.5  christos 				      rgot_weak_count++;
   2302  1.1.1.5  christos 				    }
   2303  1.1.1.5  christos 				}
   2304  1.1.1.5  christos 			    }
   2305  1.1.1.5  christos 			  internal_relocs++;
   2306  1.1.1.5  christos 			}
   2307  1.1.1.5  christos 		    }
   2308  1.1.1.5  christos 		  else
   2309  1.1.1.5  christos 		    return FALSE;
   2310  1.1.1.5  christos 		}
   2311  1.1.1.5  christos 	    }
   2312  1.1.1.5  christos 	}
   2313      1.1  christos       /* Free list.  */
   2314      1.1  christos       while (list_start)
   2315  1.1.1.5  christos 	{
   2316  1.1.1.5  christos 	  list_end = list_start->next;
   2317  1.1.1.5  christos 	  free (list_start);
   2318  1.1.1.5  christos 	  list_start = list_end;
   2319  1.1.1.5  christos 	}
   2320      1.1  christos 
   2321      1.1  christos       /* Size sections.  */
   2322  1.1.1.5  christos       lm32fdpic_fixup32_section (info)->size
   2323  1.1.1.5  christos 	= (r32_count + (htab->root.sgot->size / 4) - rgot_weak_count + 1) * 4;
   2324      1.1  christos       if (lm32fdpic_fixup32_section (info)->size == 0)
   2325  1.1.1.5  christos 	lm32fdpic_fixup32_section (info)->flags |= SEC_EXCLUDE;
   2326      1.1  christos       else
   2327  1.1.1.5  christos 	{
   2328  1.1.1.5  christos 	  lm32fdpic_fixup32_section (info)->contents =
   2329  1.1.1.5  christos 	     bfd_zalloc (dynobj, lm32fdpic_fixup32_section (info)->size);
   2330  1.1.1.5  christos 	  if (lm32fdpic_fixup32_section (info)->contents == NULL)
   2331  1.1.1.5  christos 	    return FALSE;
   2332  1.1.1.5  christos 	}
   2333      1.1  christos     }
   2334      1.1  christos 
   2335      1.1  christos   return TRUE;
   2336      1.1  christos }
   2337      1.1  christos 
   2338      1.1  christos /* Create dynamic sections when linking against a dynamic object.  */
   2339      1.1  christos 
   2340      1.1  christos static bfd_boolean
   2341      1.1  christos lm32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   2342      1.1  christos {
   2343      1.1  christos   struct elf_lm32_link_hash_table *htab;
   2344      1.1  christos   flagword flags, pltflags;
   2345      1.1  christos   asection *s;
   2346      1.1  christos   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   2347      1.1  christos   int ptralign = 2; /* 32bit */
   2348      1.1  christos 
   2349      1.1  christos   htab = lm32_elf_hash_table (info);
   2350      1.1  christos   if (htab == NULL)
   2351      1.1  christos     return FALSE;
   2352      1.1  christos 
   2353      1.1  christos   /* Make sure we have a GOT - For the case where we have a dynamic object
   2354      1.1  christos      but none of the relocs in check_relocs */
   2355  1.1.1.5  christos   if (!_bfd_elf_create_got_section (abfd, info))
   2356      1.1  christos     return FALSE;
   2357      1.1  christos   if (IS_FDPIC (abfd) && (htab->sfixup32 == NULL))
   2358      1.1  christos     {
   2359      1.1  christos       if (! create_rofixup_section (abfd, info))
   2360  1.1.1.5  christos 	return FALSE;
   2361      1.1  christos     }
   2362      1.1  christos 
   2363      1.1  christos   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
   2364      1.1  christos      .rel[a].bss sections.  */
   2365      1.1  christos   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   2366  1.1.1.5  christos 	   | SEC_LINKER_CREATED);
   2367      1.1  christos 
   2368      1.1  christos   pltflags = flags;
   2369      1.1  christos   pltflags |= SEC_CODE;
   2370      1.1  christos   if (bed->plt_not_loaded)
   2371      1.1  christos     pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
   2372      1.1  christos   if (bed->plt_readonly)
   2373      1.1  christos     pltflags |= SEC_READONLY;
   2374      1.1  christos 
   2375  1.1.1.2  christos   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
   2376  1.1.1.5  christos   htab->root.splt = s;
   2377      1.1  christos   if (s == NULL
   2378      1.1  christos       || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
   2379      1.1  christos     return FALSE;
   2380      1.1  christos 
   2381      1.1  christos   if (bed->want_plt_sym)
   2382      1.1  christos     {
   2383      1.1  christos       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
   2384  1.1.1.5  christos 	 .plt section.  */
   2385      1.1  christos       struct bfd_link_hash_entry *bh = NULL;
   2386      1.1  christos       struct elf_link_hash_entry *h;
   2387      1.1  christos 
   2388      1.1  christos       if (! (_bfd_generic_link_add_one_symbol
   2389  1.1.1.5  christos 	     (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
   2390  1.1.1.5  christos 	      (bfd_vma) 0, NULL, FALSE,
   2391  1.1.1.5  christos 	      get_elf_backend_data (abfd)->collect, &bh)))
   2392  1.1.1.5  christos 	return FALSE;
   2393      1.1  christos       h = (struct elf_link_hash_entry *) bh;
   2394      1.1  christos       h->def_regular = 1;
   2395      1.1  christos       h->type = STT_OBJECT;
   2396      1.1  christos       htab->root.hplt = h;
   2397      1.1  christos 
   2398  1.1.1.3  christos       if (bfd_link_pic (info)
   2399  1.1.1.5  christos 	  && ! bfd_elf_link_record_dynamic_symbol (info, h))
   2400  1.1.1.5  christos 	return FALSE;
   2401      1.1  christos     }
   2402      1.1  christos 
   2403  1.1.1.2  christos   s = bfd_make_section_anyway_with_flags (abfd,
   2404  1.1.1.2  christos 					  bed->default_use_rela_p
   2405  1.1.1.2  christos 					  ? ".rela.plt" : ".rel.plt",
   2406  1.1.1.2  christos 					  flags | SEC_READONLY);
   2407  1.1.1.5  christos   htab->root.srelplt = s;
   2408      1.1  christos   if (s == NULL
   2409      1.1  christos       || ! bfd_set_section_alignment (abfd, s, ptralign))
   2410      1.1  christos     return FALSE;
   2411      1.1  christos 
   2412  1.1.1.5  christos   if (htab->root.sgot == NULL
   2413  1.1.1.5  christos       && !_bfd_elf_create_got_section (abfd, info))
   2414      1.1  christos     return FALSE;
   2415      1.1  christos 
   2416      1.1  christos   if (bed->want_dynbss)
   2417      1.1  christos     {
   2418      1.1  christos       /* The .dynbss section is a place to put symbols which are defined
   2419  1.1.1.5  christos 	 by dynamic objects, are referenced by regular objects, and are
   2420  1.1.1.5  christos 	 not functions.  We must allocate space for them in the process
   2421  1.1.1.5  christos 	 image and use a R_*_COPY reloc to tell the dynamic linker to
   2422  1.1.1.5  christos 	 initialize them at run time.  The linker script puts the .dynbss
   2423  1.1.1.5  christos 	 section into the .bss section of the final image.  */
   2424  1.1.1.2  christos       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
   2425  1.1.1.2  christos 					      SEC_ALLOC | SEC_LINKER_CREATED);
   2426      1.1  christos       htab->sdynbss = s;
   2427      1.1  christos       if (s == NULL)
   2428  1.1.1.5  christos 	return FALSE;
   2429      1.1  christos       /* The .rel[a].bss section holds copy relocs.  This section is not
   2430  1.1.1.5  christos 	 normally needed.  We need to create it here, though, so that the
   2431  1.1.1.5  christos 	 linker will map it to an output section.  We can't just create it
   2432  1.1.1.5  christos 	 only if we need it, because we will not know whether we need it
   2433  1.1.1.5  christos 	 until we have seen all the input files, and the first time the
   2434  1.1.1.5  christos 	 main linker code calls BFD after examining all the input files
   2435  1.1.1.5  christos 	 (size_dynamic_sections) the input sections have already been
   2436  1.1.1.5  christos 	 mapped to the output sections.  If the section turns out not to
   2437  1.1.1.5  christos 	 be needed, we can discard it later.  We will never need this
   2438  1.1.1.5  christos 	 section when generating a shared object, since they do not use
   2439  1.1.1.5  christos 	 copy relocs.  */
   2440  1.1.1.3  christos       if (! bfd_link_pic (info))
   2441  1.1.1.5  christos 	{
   2442  1.1.1.5  christos 	  s = bfd_make_section_anyway_with_flags (abfd,
   2443  1.1.1.2  christos 						  (bed->default_use_rela_p
   2444  1.1.1.2  christos 						   ? ".rela.bss" : ".rel.bss"),
   2445  1.1.1.2  christos 						  flags | SEC_READONLY);
   2446  1.1.1.5  christos 	  htab->srelbss = s;
   2447  1.1.1.5  christos 	  if (s == NULL
   2448  1.1.1.5  christos 	      || ! bfd_set_section_alignment (abfd, s, ptralign))
   2449  1.1.1.5  christos 	    return FALSE;
   2450  1.1.1.5  christos 	}
   2451      1.1  christos     }
   2452      1.1  christos 
   2453      1.1  christos   return TRUE;
   2454      1.1  christos }
   2455      1.1  christos 
   2456      1.1  christos /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   2457      1.1  christos 
   2458      1.1  christos static void
   2459      1.1  christos lm32_elf_copy_indirect_symbol (struct bfd_link_info *info,
   2460  1.1.1.5  christos 			       struct elf_link_hash_entry *dir,
   2461  1.1.1.5  christos 			       struct elf_link_hash_entry *ind)
   2462      1.1  christos {
   2463      1.1  christos   struct elf_lm32_link_hash_entry * edir;
   2464      1.1  christos   struct elf_lm32_link_hash_entry * eind;
   2465      1.1  christos 
   2466      1.1  christos   edir = (struct elf_lm32_link_hash_entry *) dir;
   2467      1.1  christos   eind = (struct elf_lm32_link_hash_entry *) ind;
   2468      1.1  christos 
   2469      1.1  christos   if (eind->dyn_relocs != NULL)
   2470      1.1  christos     {
   2471      1.1  christos       if (edir->dyn_relocs != NULL)
   2472  1.1.1.5  christos 	{
   2473  1.1.1.5  christos 	  struct elf_dyn_relocs **pp;
   2474  1.1.1.5  christos 	  struct elf_dyn_relocs *p;
   2475  1.1.1.5  christos 
   2476  1.1.1.5  christos 	  /* Add reloc counts against the indirect sym to the direct sym
   2477  1.1.1.5  christos 	     list.  Merge any entries against the same section.  */
   2478  1.1.1.5  christos 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
   2479  1.1.1.5  christos 	    {
   2480  1.1.1.5  christos 	      struct elf_dyn_relocs *q;
   2481  1.1.1.5  christos 
   2482  1.1.1.5  christos 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   2483  1.1.1.5  christos 		if (q->sec == p->sec)
   2484  1.1.1.5  christos 		  {
   2485  1.1.1.5  christos 		    q->pc_count += p->pc_count;
   2486  1.1.1.5  christos 		    q->count += p->count;
   2487  1.1.1.5  christos 		    *pp = p->next;
   2488  1.1.1.5  christos 		    break;
   2489  1.1.1.5  christos 		  }
   2490  1.1.1.5  christos 	      if (q == NULL)
   2491  1.1.1.5  christos 		pp = &p->next;
   2492  1.1.1.5  christos 	    }
   2493  1.1.1.5  christos 	  *pp = edir->dyn_relocs;
   2494  1.1.1.5  christos 	}
   2495      1.1  christos 
   2496      1.1  christos       edir->dyn_relocs = eind->dyn_relocs;
   2497      1.1  christos       eind->dyn_relocs = NULL;
   2498      1.1  christos     }
   2499      1.1  christos 
   2500      1.1  christos   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   2501      1.1  christos }
   2502      1.1  christos 
   2503      1.1  christos static bfd_boolean
   2504  1.1.1.3  christos lm32_elf_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
   2505      1.1  christos {
   2506  1.1.1.3  christos   if (!bfd_link_relocatable (info))
   2507      1.1  christos     {
   2508  1.1.1.3  christos       if (!bfd_elf_stack_segment_size (output_bfd, info,
   2509  1.1.1.3  christos 				       "__stacksize", DEFAULT_STACK_SIZE))
   2510  1.1.1.3  christos 	return FALSE;
   2511      1.1  christos 
   2512      1.1  christos       asection *sec = bfd_get_section_by_name (output_bfd, ".stack");
   2513      1.1  christos       if (sec)
   2514  1.1.1.3  christos 	sec->size = info->stacksize >= 0 ? info->stacksize : 0;
   2515      1.1  christos     }
   2516      1.1  christos 
   2517      1.1  christos   return TRUE;
   2518      1.1  christos }
   2519      1.1  christos 
   2520      1.1  christos static bfd_boolean
   2521      1.1  christos lm32_elf_fdpic_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   2522      1.1  christos {
   2523      1.1  christos   unsigned i;
   2524      1.1  christos 
   2525      1.1  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   2526      1.1  christos       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   2527      1.1  christos     return TRUE;
   2528      1.1  christos 
   2529  1.1.1.3  christos   if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
   2530      1.1  christos     return FALSE;
   2531      1.1  christos 
   2532      1.1  christos   if (! elf_tdata (ibfd) || ! elf_tdata (ibfd)->phdr
   2533      1.1  christos       || ! elf_tdata (obfd) || ! elf_tdata (obfd)->phdr)
   2534      1.1  christos     return TRUE;
   2535      1.1  christos 
   2536      1.1  christos   /* Copy the stack size.  */
   2537      1.1  christos   for (i = 0; i < elf_elfheader (ibfd)->e_phnum; i++)
   2538      1.1  christos     if (elf_tdata (ibfd)->phdr[i].p_type == PT_GNU_STACK)
   2539      1.1  christos       {
   2540      1.1  christos 	Elf_Internal_Phdr *iphdr = &elf_tdata (ibfd)->phdr[i];
   2541      1.1  christos 
   2542      1.1  christos 	for (i = 0; i < elf_elfheader (obfd)->e_phnum; i++)
   2543      1.1  christos 	  if (elf_tdata (obfd)->phdr[i].p_type == PT_GNU_STACK)
   2544      1.1  christos 	    {
   2545      1.1  christos 	      memcpy (&elf_tdata (obfd)->phdr[i], iphdr, sizeof (*iphdr));
   2546      1.1  christos 
   2547      1.1  christos 	      /* Rewrite the phdrs, since we're only called after they were first written.  */
   2548      1.1  christos 	      if (bfd_seek (obfd, (bfd_signed_vma) get_elf_backend_data (obfd)
   2549      1.1  christos 			    ->s->sizeof_ehdr, SEEK_SET) != 0
   2550      1.1  christos 		  || get_elf_backend_data (obfd)->s->write_out_phdrs (obfd, elf_tdata (obfd)->phdr,
   2551      1.1  christos 				     elf_elfheader (obfd)->e_phnum) != 0)
   2552      1.1  christos 		return FALSE;
   2553      1.1  christos 	      break;
   2554      1.1  christos 	    }
   2555      1.1  christos 
   2556      1.1  christos 	break;
   2557      1.1  christos       }
   2558      1.1  christos 
   2559      1.1  christos   return TRUE;
   2560      1.1  christos }
   2561      1.1  christos 
   2562      1.1  christos 
   2563  1.1.1.5  christos #define ELF_ARCH		bfd_arch_lm32
   2564      1.1  christos #define ELF_TARGET_ID		LM32_ELF_DATA
   2565  1.1.1.5  christos #define ELF_MACHINE_CODE	EM_LATTICEMICO32
   2566  1.1.1.5  christos #define ELF_MAXPAGESIZE		0x1000
   2567      1.1  christos 
   2568  1.1.1.5  christos #define TARGET_BIG_SYM		lm32_elf32_vec
   2569  1.1.1.5  christos #define TARGET_BIG_NAME		"elf32-lm32"
   2570      1.1  christos 
   2571  1.1.1.5  christos #define bfd_elf32_bfd_reloc_type_lookup		lm32_reloc_type_lookup
   2572  1.1.1.5  christos #define bfd_elf32_bfd_reloc_name_lookup		lm32_reloc_name_lookup
   2573  1.1.1.5  christos #define elf_info_to_howto			lm32_info_to_howto_rela
   2574  1.1.1.6  christos #define elf_info_to_howto_rel			NULL
   2575  1.1.1.5  christos #define elf_backend_rela_normal			1
   2576  1.1.1.5  christos #define elf_backend_object_p			lm32_elf_object_p
   2577  1.1.1.5  christos #define elf_backend_final_write_processing	lm32_elf_final_write_processing
   2578  1.1.1.3  christos #define elf_backend_stack_align			8
   2579  1.1.1.5  christos #define elf_backend_can_gc_sections		1
   2580  1.1.1.5  christos #define elf_backend_can_refcount		1
   2581  1.1.1.5  christos #define elf_backend_gc_mark_hook		lm32_elf_gc_mark_hook
   2582  1.1.1.5  christos #define elf_backend_plt_readonly		1
   2583  1.1.1.5  christos #define elf_backend_want_got_plt		1
   2584  1.1.1.5  christos #define elf_backend_want_plt_sym		0
   2585  1.1.1.5  christos #define elf_backend_got_header_size		12
   2586  1.1.1.5  christos #define elf_backend_dtrel_excludes_plt		1
   2587  1.1.1.5  christos #define bfd_elf32_bfd_link_hash_table_create	lm32_elf_link_hash_table_create
   2588  1.1.1.5  christos #define elf_backend_check_relocs		lm32_elf_check_relocs
   2589  1.1.1.5  christos #define elf_backend_reloc_type_class		lm32_elf_reloc_type_class
   2590  1.1.1.5  christos #define elf_backend_copy_indirect_symbol	lm32_elf_copy_indirect_symbol
   2591  1.1.1.5  christos #define elf_backend_size_dynamic_sections	lm32_elf_size_dynamic_sections
   2592  1.1.1.6  christos #define elf_backend_omit_section_dynsym		_bfd_elf_omit_section_dynsym_all
   2593  1.1.1.5  christos #define elf_backend_create_dynamic_sections	lm32_elf_create_dynamic_sections
   2594  1.1.1.5  christos #define elf_backend_finish_dynamic_sections	lm32_elf_finish_dynamic_sections
   2595  1.1.1.5  christos #define elf_backend_adjust_dynamic_symbol	lm32_elf_adjust_dynamic_symbol
   2596  1.1.1.5  christos #define elf_backend_finish_dynamic_symbol	lm32_elf_finish_dynamic_symbol
   2597  1.1.1.5  christos #define elf_backend_relocate_section		lm32_elf_relocate_section
   2598      1.1  christos 
   2599      1.1  christos #include "elf32-target.h"
   2600      1.1  christos 
   2601      1.1  christos #undef  ELF_MAXPAGESIZE
   2602      1.1  christos #define ELF_MAXPAGESIZE		0x4000
   2603      1.1  christos 
   2604      1.1  christos 
   2605      1.1  christos #undef  TARGET_BIG_SYM
   2606  1.1.1.5  christos #define TARGET_BIG_SYM		lm32_elf32_fdpic_vec
   2607  1.1.1.5  christos #undef	TARGET_BIG_NAME
   2608      1.1  christos #define TARGET_BIG_NAME		"elf32-lm32fdpic"
   2609      1.1  christos #undef	elf32_bed
   2610      1.1  christos #define	elf32_bed		elf32_lm32fdpic_bed
   2611      1.1  christos 
   2612  1.1.1.5  christos #undef	elf_backend_always_size_sections
   2613  1.1.1.5  christos #define elf_backend_always_size_sections	lm32_elf_always_size_sections
   2614  1.1.1.5  christos #undef	bfd_elf32_bfd_copy_private_bfd_data
   2615  1.1.1.5  christos #define bfd_elf32_bfd_copy_private_bfd_data	lm32_elf_fdpic_copy_private_bfd_data
   2616      1.1  christos 
   2617      1.1  christos #include "elf32-target.h"
   2618