Home | History | Annotate | Line # | Download | only in bfd
elf32-or1k.c revision 1.4
      1  1.1  christos /* Or1k-specific support for 32-bit ELF.
      2  1.4  christos    Copyright (C) 2001-2016 Free Software Foundation, Inc.
      3  1.1  christos    Contributed for OR32 by Johan Rydberg, jrydberg (at) opencores.org
      4  1.1  christos 
      5  1.1  christos    PIC parts added by Stefan Kristiansson, stefan.kristiansson (at) saunalahti.fi,
      6  1.1  christos    largely based on elf32-m32r.c and elf32-microblaze.c.
      7  1.1  christos 
      8  1.1  christos    This file is part of BFD, the Binary File Descriptor library.
      9  1.1  christos 
     10  1.1  christos    This program is free software; you can redistribute it and/or modify
     11  1.1  christos    it under the terms of the GNU General Public License as published by
     12  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     13  1.1  christos    (at your option) any later version.
     14  1.1  christos 
     15  1.1  christos    This program is distributed in the hope that it will be useful,
     16  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  1.1  christos    GNU General Public License for more details.
     19  1.1  christos 
     20  1.1  christos    You should have received a copy of the GNU General Public License
     21  1.1  christos    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
     22  1.1  christos 
     23  1.1  christos #include "sysdep.h"
     24  1.1  christos #include "bfd.h"
     25  1.1  christos #include "libbfd.h"
     26  1.1  christos #include "elf-bfd.h"
     27  1.1  christos #include "elf/or1k.h"
     28  1.1  christos #include "libiberty.h"
     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 0x19800000 /* l.movhi r12, 0 <- hi(.got+4) */
     33  1.1  christos #define PLT0_ENTRY_WORD1 0xa98c0000 /* l.ori r12, r12, 0 <- lo(.got+4) */
     34  1.1  christos #define PLT0_ENTRY_WORD2 0x85ec0004 /* l.lwz r15, 4(r12) <- *(.got+8)*/
     35  1.1  christos #define PLT0_ENTRY_WORD3 0x44007800 /* l.jr r15 */
     36  1.1  christos #define PLT0_ENTRY_WORD4 0x858c0000 /* l.lwz r12, 0(r12) */
     37  1.1  christos 
     38  1.1  christos #define PLT0_PIC_ENTRY_WORD0 0x85900004 /* l.lwz r12, 4(r16) */
     39  1.1  christos #define PLT0_PIC_ENTRY_WORD1 0x85f00008 /* l.lwz r15, 8(r16) */
     40  1.1  christos #define PLT0_PIC_ENTRY_WORD2 0x44007800 /* l.jr r15 */
     41  1.1  christos #define PLT0_PIC_ENTRY_WORD3 0x15000000 /* l.nop */
     42  1.1  christos #define PLT0_PIC_ENTRY_WORD4 0x15000000 /* l.nop */
     43  1.1  christos 
     44  1.1  christos #define PLT_ENTRY_WORD0 0x19800000 /* l.movhi r12, 0 <- hi(got idx addr) */
     45  1.1  christos #define PLT_ENTRY_WORD1 0xa98c0000 /* l.ori r12, r12, 0 <- lo(got idx addr) */
     46  1.1  christos #define PLT_ENTRY_WORD2 0x858c0000 /* l.lwz r12, 0(r12) */
     47  1.1  christos #define PLT_ENTRY_WORD3 0x44006000 /* l.jr r12 */
     48  1.1  christos #define PLT_ENTRY_WORD4 0xa9600000 /* l.ori r11, r0, 0 <- reloc offset */
     49  1.1  christos 
     50  1.1  christos #define PLT_PIC_ENTRY_WORD0 0x85900000 /* l.lwz r12, 0(r16) <- index in got */
     51  1.1  christos #define PLT_PIC_ENTRY_WORD1 0xa9600000 /* l.ori r11, r0, 0 <- reloc offset */
     52  1.1  christos #define PLT_PIC_ENTRY_WORD2 0x44006000 /* l.jr r12 */
     53  1.1  christos #define PLT_PIC_ENTRY_WORD3 0x15000000 /* l.nop */
     54  1.1  christos #define PLT_PIC_ENTRY_WORD4 0x15000000 /* l.nop */
     55  1.1  christos 
     56  1.1  christos #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
     57  1.1  christos 
     58  1.1  christos static reloc_howto_type or1k_elf_howto_table[] =
     59  1.1  christos {
     60  1.1  christos   /* This reloc does nothing.  */
     61  1.1  christos   HOWTO (R_OR1K_NONE,           /* type */
     62  1.1  christos          0,                     /* rightshift */
     63  1.3  christos          3,                     /* size (0 = byte, 1 = short, 2 = long) */
     64  1.3  christos          0,                     /* bitsize */
     65  1.1  christos          FALSE,                 /* pc_relative */
     66  1.1  christos          0,                     /* bitpos */
     67  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
     68  1.1  christos          bfd_elf_generic_reloc, /* special_function */
     69  1.1  christos          "R_OR1K_NONE",         /* name */
     70  1.1  christos          FALSE,                 /* partial_inplace */
     71  1.1  christos          0,                     /* src_mask */
     72  1.1  christos          0,                     /* dst_mask */
     73  1.1  christos          FALSE),                /* pcrel_offset */
     74  1.1  christos 
     75  1.1  christos   HOWTO (R_OR1K_32,
     76  1.1  christos          0,                     /* rightshift */
     77  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
     78  1.1  christos          32,                    /* bitsize */
     79  1.1  christos          FALSE,                 /* pc_relative */
     80  1.1  christos          0,                     /* bitpos */
     81  1.1  christos          complain_overflow_unsigned, /* complain_on_overflow */
     82  1.1  christos          bfd_elf_generic_reloc, /* special_function */
     83  1.1  christos          "R_OR1K_32",           /* name */
     84  1.1  christos          FALSE,                 /* partial_inplace */
     85  1.1  christos          0,                     /* src_mask */
     86  1.1  christos          0xffffffff,            /* dst_mask */
     87  1.1  christos          FALSE),                /* pcrel_offset */
     88  1.1  christos 
     89  1.1  christos   HOWTO (R_OR1K_16,
     90  1.1  christos          0,                     /* rightshift */
     91  1.1  christos          1,                     /* size (0 = byte, 1 = short, 2 = long) */
     92  1.1  christos          16,                    /* bitsize */
     93  1.1  christos          FALSE,                 /* pc_relative */
     94  1.1  christos          0,                     /* bitpos */
     95  1.1  christos          complain_overflow_unsigned, /* complain_on_overflow */
     96  1.1  christos          bfd_elf_generic_reloc, /* special_function */
     97  1.1  christos          "R_OR1K_16",           /* name */
     98  1.1  christos          FALSE,                 /* partial_inplace */
     99  1.1  christos          0,                     /* src_mask */
    100  1.1  christos          0xffff,                /* dst_mask */
    101  1.1  christos          FALSE),                /* pcrel_offset */
    102  1.1  christos 
    103  1.1  christos   HOWTO (R_OR1K_8,
    104  1.1  christos          0,                     /* rightshift */
    105  1.1  christos          0,                     /* size (0 = byte, 1 = short, 2 = long) */
    106  1.1  christos          8,                     /* bitsize */
    107  1.1  christos          FALSE,                 /* pc_relative */
    108  1.1  christos          0,                     /* bitpos */
    109  1.1  christos          complain_overflow_unsigned, /* complain_on_overflow */
    110  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    111  1.1  christos          "R_OR1K_8",            /* name */
    112  1.1  christos          FALSE,                 /* partial_inplace */
    113  1.1  christos          0,                     /* src_mask */
    114  1.1  christos          0xff,                  /* dst_mask */
    115  1.1  christos          FALSE),                /* pcrel_offset */
    116  1.1  christos 
    117  1.1  christos   HOWTO (R_OR1K_LO_16_IN_INSN, /* type */
    118  1.1  christos          0,                     /* rightshift */
    119  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    120  1.1  christos          16,                    /* bitsize */
    121  1.1  christos          FALSE,                 /* pc_relative */
    122  1.1  christos          0,                     /* bitpos */
    123  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    124  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    125  1.1  christos          "R_OR1K_LO_16_IN_INSN", /* name */
    126  1.1  christos          FALSE,                 /* partial_inplace */
    127  1.1  christos          0,                     /* src_mask */
    128  1.1  christos          0x0000ffff,            /* dst_mask */
    129  1.1  christos          FALSE),                /* pcrel_offset */
    130  1.1  christos 
    131  1.1  christos   HOWTO (R_OR1K_HI_16_IN_INSN, /* type */
    132  1.1  christos          16,                    /* rightshift */
    133  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    134  1.1  christos          16,                    /* bitsize */
    135  1.1  christos          FALSE,                 /* pc_relative */
    136  1.1  christos          0,                     /* bitpos */
    137  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    138  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    139  1.1  christos          "R_OR1K_HI_16_IN_INSN", /* name */
    140  1.1  christos          FALSE,                 /* partial_inplace */
    141  1.1  christos          0,                     /* src_mask */
    142  1.1  christos          0x0000ffff,            /* dst_mask */
    143  1.1  christos          FALSE),                /* pcrel_offset */
    144  1.1  christos 
    145  1.1  christos   /* A PC relative 26 bit relocation, right shifted by 2.  */
    146  1.1  christos   HOWTO (R_OR1K_INSN_REL_26, /* type */
    147  1.1  christos          2,                     /* rightshift */
    148  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    149  1.1  christos          26,                    /* bitsize */
    150  1.1  christos          TRUE,                  /* pc_relative */
    151  1.1  christos          0,                     /* bitpos */
    152  1.1  christos          complain_overflow_signed, /* complain_on_overflow */
    153  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    154  1.1  christos          "R_OR1K_INSN_REL_26", /* name */
    155  1.1  christos          FALSE,                 /* partial_inplace */
    156  1.1  christos          0,                     /* src_mask */
    157  1.1  christos          0x03ffffff,            /* dst_mask */
    158  1.1  christos          TRUE),                 /* pcrel_offset */
    159  1.1  christos 
    160  1.1  christos   /* GNU extension to record C++ vtable hierarchy.  */
    161  1.1  christos   HOWTO (R_OR1K_GNU_VTINHERIT, /* type */
    162  1.1  christos          0,                     /* rightshift */
    163  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    164  1.1  christos          0,                     /* bitsize */
    165  1.1  christos          FALSE,                 /* pc_relative */
    166  1.1  christos          0,                     /* bitpos */
    167  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    168  1.1  christos          NULL,                  /* special_function */
    169  1.1  christos          "R_OR1K_GNU_VTINHERIT", /* name */
    170  1.1  christos          FALSE,                 /* partial_inplace */
    171  1.1  christos          0,                     /* src_mask */
    172  1.1  christos          0,                     /* dst_mask */
    173  1.1  christos          FALSE),                /* pcrel_offset */
    174  1.1  christos 
    175  1.1  christos   /* GNU extension to record C++ vtable member usage.  */
    176  1.1  christos   HOWTO (R_OR1K_GNU_VTENTRY, /* type */
    177  1.1  christos          0,                     /* rightshift */
    178  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    179  1.1  christos          0,                     /* bitsize */
    180  1.1  christos          FALSE,                 /* pc_relative */
    181  1.1  christos          0,                     /* bitpos */
    182  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    183  1.1  christos          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
    184  1.1  christos          "R_OR1K_GNU_VTENTRY", /* name */
    185  1.1  christos          FALSE,                 /* partial_inplace */
    186  1.1  christos          0,                     /* src_mask */
    187  1.1  christos          0,                     /* dst_mask */
    188  1.1  christos          FALSE),                /* pcrel_offset */
    189  1.1  christos 
    190  1.1  christos   HOWTO (R_OR1K_32_PCREL,
    191  1.1  christos          0,                     /* rightshift */
    192  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    193  1.1  christos          32,                    /* bitsize */
    194  1.1  christos          TRUE,                  /* pc_relative */
    195  1.1  christos          0,                     /* bitpos */
    196  1.1  christos          complain_overflow_signed, /* complain_on_overflow */
    197  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    198  1.1  christos          "R_OR1K_32_PCREL",     /* name */
    199  1.1  christos          FALSE,                 /* partial_inplace */
    200  1.1  christos          0,                     /* src_mask */
    201  1.1  christos          0xffffffff,            /* dst_mask */
    202  1.4  christos          TRUE),                 /* pcrel_offset */
    203  1.1  christos 
    204  1.1  christos   HOWTO (R_OR1K_16_PCREL,
    205  1.1  christos          0,                     /* rightshift */
    206  1.1  christos          1,                     /* size (0 = byte, 1 = short, 2 = long) */
    207  1.1  christos          16,                    /* bitsize */
    208  1.1  christos          TRUE,                  /* pc_relative */
    209  1.1  christos          0,                     /* bitpos */
    210  1.1  christos          complain_overflow_signed, /* complain_on_overflow */
    211  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    212  1.1  christos          "R_OR1K_16_PCREL",     /* name */
    213  1.1  christos          FALSE,                 /* partial_inplace */
    214  1.1  christos          0,                     /* src_mask */
    215  1.1  christos          0xffff,                /* dst_mask */
    216  1.4  christos          TRUE),                 /* pcrel_offset */
    217  1.1  christos 
    218  1.1  christos   HOWTO (R_OR1K_8_PCREL,
    219  1.1  christos          0,                     /* rightshift */
    220  1.1  christos          0,                     /* size (0 = byte, 1 = short, 2 = long) */
    221  1.1  christos          8,                     /* bitsize */
    222  1.1  christos          TRUE,                  /* pc_relative */
    223  1.1  christos          0,                     /* bitpos */
    224  1.1  christos          complain_overflow_signed, /* complain_on_overflow */
    225  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    226  1.1  christos          "R_OR1K_8_PCREL",      /* name */
    227  1.1  christos          FALSE,                 /* partial_inplace */
    228  1.1  christos          0,                     /* src_mask */
    229  1.1  christos          0xff,                  /* dst_mask */
    230  1.4  christos          TRUE),                 /* pcrel_offset */
    231  1.1  christos 
    232  1.1  christos    HOWTO (R_OR1K_GOTPC_HI16,    /* Type.  */
    233  1.1  christos          16,                    /* Rightshift.  */
    234  1.1  christos          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
    235  1.1  christos          16,                    /* Bitsize.  */
    236  1.1  christos          TRUE,                  /* PC_relative.  */
    237  1.1  christos          0,                     /* Bitpos.  */
    238  1.1  christos          complain_overflow_dont, /* Complain on overflow.  */
    239  1.1  christos          bfd_elf_generic_reloc, /* Special Function.  */
    240  1.1  christos          "R_OR1K_GOTPC_HI16",   /* Name.  */
    241  1.1  christos          FALSE,         /* Partial Inplace.  */
    242  1.1  christos          0,                     /* Source Mask.  */
    243  1.1  christos          0xffff,                /* Dest Mask.  */
    244  1.1  christos          TRUE),                 /* PC relative offset?  */
    245  1.1  christos 
    246  1.1  christos    HOWTO (R_OR1K_GOTPC_LO16,    /* Type.  */
    247  1.1  christos          0,                     /* Rightshift.  */
    248  1.1  christos          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
    249  1.1  christos          16,                    /* Bitsize.  */
    250  1.1  christos          TRUE,                  /* PC_relative.  */
    251  1.1  christos          0,                     /* Bitpos.  */
    252  1.1  christos          complain_overflow_dont, /* Complain on overflow.  */
    253  1.1  christos          bfd_elf_generic_reloc, /* Special Function.  */
    254  1.1  christos          "R_OR1K_GOTPC_LO16",   /* Name.  */
    255  1.1  christos          FALSE,         /* Partial Inplace.  */
    256  1.1  christos          0,                     /* Source Mask.  */
    257  1.1  christos          0xffff,                /* Dest Mask.  */
    258  1.1  christos          TRUE),                 /* PC relative offset?  */
    259  1.1  christos 
    260  1.1  christos   HOWTO (R_OR1K_GOT16,          /* type */
    261  1.1  christos          0,                     /* rightshift */
    262  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    263  1.1  christos          16,                    /* bitsize */
    264  1.1  christos          FALSE,                 /* pc_relative */
    265  1.1  christos          0,                     /* bitpos */
    266  1.1  christos          complain_overflow_signed, /* complain_on_overflow */
    267  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    268  1.1  christos          "R_OR1K_GOT16",        /* name */
    269  1.1  christos          FALSE,                 /* partial_inplace */
    270  1.1  christos          0,                     /* src_mask */
    271  1.1  christos          0xffff,                /* dst_mask */
    272  1.1  christos          FALSE),                /* pcrel_offset */
    273  1.1  christos 
    274  1.1  christos   /* A 26 bit PLT relocation.  Shifted by 2.  */
    275  1.1  christos   HOWTO (R_OR1K_PLT26,  /* Type.  */
    276  1.1  christos          2,                     /* Rightshift.  */
    277  1.1  christos          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
    278  1.1  christos          26,                    /* Bitsize.  */
    279  1.1  christos          TRUE,                  /* PC_relative.  */
    280  1.1  christos          0,                     /* Bitpos.  */
    281  1.1  christos          complain_overflow_dont, /* Complain on overflow.  */
    282  1.1  christos          bfd_elf_generic_reloc,/* Special Function.  */
    283  1.1  christos          "R_OR1K_PLT26",        /* Name.  */
    284  1.1  christos          FALSE,         /* Partial Inplace.  */
    285  1.1  christos          0,                     /* Source Mask.  */
    286  1.1  christos          0x03ffffff,            /* Dest Mask.  */
    287  1.1  christos          TRUE),                 /* PC relative offset?  */
    288  1.1  christos 
    289  1.1  christos   HOWTO (R_OR1K_GOTOFF_HI16,    /* type */
    290  1.1  christos          16,                    /* rightshift */
    291  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    292  1.1  christos          16,                    /* bitsize */
    293  1.1  christos          FALSE,                 /* pc_relative */
    294  1.1  christos          0,                     /* bitpos */
    295  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    296  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    297  1.1  christos          "R_OR1K_GOTOFF_HI16",  /* name */
    298  1.1  christos          FALSE,                 /* partial_inplace */
    299  1.1  christos          0x0,                   /* src_mask */
    300  1.1  christos          0xffff,                /* dst_mask */
    301  1.1  christos          FALSE),                /* pcrel_offset */
    302  1.1  christos 
    303  1.1  christos   HOWTO (R_OR1K_GOTOFF_LO16,    /* type */
    304  1.1  christos          0,                     /* rightshift */
    305  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    306  1.1  christos          16,                    /* bitsize */
    307  1.1  christos          FALSE,                 /* pc_relative */
    308  1.1  christos          0,                     /* bitpos */
    309  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    310  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    311  1.1  christos          "R_OR1K_GOTOFF_LO16",  /* name */
    312  1.1  christos          FALSE,                 /* partial_inplace */
    313  1.1  christos          0x0,                   /* src_mask */
    314  1.1  christos          0xffff,                /* dst_mask */
    315  1.1  christos          FALSE),                /* pcrel_offset */
    316  1.1  christos 
    317  1.1  christos   HOWTO (R_OR1K_COPY,           /* type */
    318  1.1  christos          0,                     /* rightshift */
    319  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    320  1.1  christos          32,                    /* bitsize */
    321  1.1  christos          FALSE,                 /* pc_relative */
    322  1.1  christos          0,                     /* bitpos */
    323  1.1  christos          complain_overflow_bitfield, /* complain_on_overflow */
    324  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    325  1.1  christos          "R_OR1K_COPY",         /* name */
    326  1.1  christos          FALSE,                 /* partial_inplace */
    327  1.1  christos          0xffffffff,            /* src_mask */
    328  1.1  christos          0xffffffff,            /* dst_mask */
    329  1.1  christos          FALSE),                /* pcrel_offset */
    330  1.1  christos 
    331  1.1  christos   HOWTO (R_OR1K_GLOB_DAT,       /* type */
    332  1.1  christos          0,                     /* rightshift */
    333  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    334  1.1  christos          32,                    /* bitsize */
    335  1.1  christos          FALSE,                 /* pc_relative */
    336  1.1  christos          0,                     /* bitpos */
    337  1.1  christos          complain_overflow_bitfield, /* complain_on_overflow */
    338  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    339  1.1  christos          "R_OR1K_GLOB_DAT",     /* name */
    340  1.1  christos          FALSE,                 /* partial_inplace */
    341  1.1  christos          0xffffffff,            /* src_mask */
    342  1.1  christos          0xffffffff,            /* dst_mask */
    343  1.1  christos          FALSE),                /* pcrel_offset */
    344  1.1  christos 
    345  1.1  christos   HOWTO (R_OR1K_JMP_SLOT,       /* type */
    346  1.1  christos          0,                     /* rightshift */
    347  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    348  1.1  christos          32,                    /* bitsize */
    349  1.1  christos          FALSE,                 /* pc_relative */
    350  1.1  christos          0,                     /* bitpos */
    351  1.1  christos          complain_overflow_bitfield, /* complain_on_overflow */
    352  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    353  1.1  christos          "R_OR1K_JMP_SLOT",     /* name */
    354  1.1  christos          FALSE,                 /* partial_inplace */
    355  1.1  christos          0xffffffff,            /* src_mask */
    356  1.1  christos          0xffffffff,            /* dst_mask */
    357  1.1  christos          FALSE),                /* pcrel_offset */
    358  1.1  christos 
    359  1.1  christos   HOWTO (R_OR1K_RELATIVE,       /* type */
    360  1.1  christos          0,                     /* rightshift */
    361  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    362  1.1  christos          32,                    /* bitsize */
    363  1.1  christos          FALSE,                 /* pc_relative */
    364  1.1  christos          0,                     /* bitpos */
    365  1.1  christos          complain_overflow_bitfield, /* complain_on_overflow */
    366  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    367  1.1  christos          "R_OR1K_RELATIVE",     /* name */
    368  1.1  christos          FALSE,                 /* partial_inplace */
    369  1.1  christos          0xffffffff,            /* src_mask */
    370  1.1  christos          0xffffffff,            /* dst_mask */
    371  1.1  christos          FALSE),                /* pcrel_offset */
    372  1.1  christos 
    373  1.1  christos   HOWTO (R_OR1K_TLS_GD_HI16,    /* type */
    374  1.1  christos          16,                    /* rightshift */
    375  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    376  1.1  christos          16,                    /* bitsize */
    377  1.1  christos          FALSE,                 /* pc_relative */
    378  1.1  christos          0,                     /* bitpos */
    379  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    380  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    381  1.1  christos          "R_OR1K_TLS_GD_HI16",  /* name */
    382  1.1  christos          FALSE,                 /* partial_inplace */
    383  1.1  christos          0x0,                   /* src_mask */
    384  1.1  christos          0xffff,                /* dst_mask */
    385  1.1  christos          FALSE),                /* pcrel_offset */
    386  1.1  christos 
    387  1.1  christos   HOWTO (R_OR1K_TLS_GD_LO16,    /* type */
    388  1.1  christos          0,                     /* rightshift */
    389  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    390  1.1  christos          16,                    /* bitsize */
    391  1.1  christos          FALSE,                 /* pc_relative */
    392  1.1  christos          0,                     /* bitpos */
    393  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    394  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    395  1.1  christos          "R_OR1K_TLS_GD_LO16",  /* name */
    396  1.1  christos          FALSE,                 /* partial_inplace */
    397  1.1  christos          0x0,                   /* src_mask */
    398  1.1  christos          0xffff,                /* dst_mask */
    399  1.1  christos          FALSE),                /* pcrel_offset */
    400  1.1  christos 
    401  1.1  christos   HOWTO (R_OR1K_TLS_LDM_HI16,   /* type */
    402  1.1  christos          16,                    /* rightshift */
    403  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    404  1.1  christos          16,                    /* bitsize */
    405  1.1  christos          FALSE,                 /* pc_relative */
    406  1.1  christos          0,                     /* bitpos */
    407  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    408  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    409  1.1  christos          "R_OR1K_TLS_LDM_HI16", /* name */
    410  1.1  christos          FALSE,                 /* partial_inplace */
    411  1.1  christos          0x0,                   /* src_mask */
    412  1.1  christos          0xffff,                /* dst_mask */
    413  1.1  christos          FALSE),                /* pcrel_offset */
    414  1.1  christos 
    415  1.1  christos   HOWTO (R_OR1K_TLS_LDM_LO16,   /* type */
    416  1.1  christos          0,                     /* rightshift */
    417  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    418  1.1  christos          16,                    /* bitsize */
    419  1.1  christos          FALSE,                 /* pc_relative */
    420  1.1  christos          0,                     /* bitpos */
    421  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    422  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    423  1.1  christos          "R_OR1K_TLS_LDM_LO16", /* name */
    424  1.1  christos          FALSE,                 /* partial_inplace */
    425  1.1  christos          0x0,                   /* src_mask */
    426  1.1  christos          0xffff,                /* dst_mask */
    427  1.1  christos          FALSE),                /* pcrel_offset */
    428  1.1  christos 
    429  1.1  christos   HOWTO (R_OR1K_TLS_LDO_HI16,   /* type */
    430  1.1  christos          16,                    /* rightshift */
    431  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    432  1.1  christos          16,                    /* bitsize */
    433  1.1  christos          FALSE,                 /* pc_relative */
    434  1.1  christos          0,                     /* bitpos */
    435  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    436  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    437  1.1  christos          "R_OR1K_TLS_LDO_HI16", /* name */
    438  1.1  christos          FALSE,                 /* partial_inplace */
    439  1.1  christos          0x0,                   /* src_mask */
    440  1.1  christos          0xffff,                /* dst_mask */
    441  1.1  christos          FALSE),                /* pcrel_offset */
    442  1.1  christos 
    443  1.1  christos   HOWTO (R_OR1K_TLS_LDO_LO16,   /* type */
    444  1.1  christos          0,                     /* rightshift */
    445  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    446  1.1  christos          16,                    /* bitsize */
    447  1.1  christos          FALSE,                 /* pc_relative */
    448  1.1  christos          0,                     /* bitpos */
    449  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    450  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    451  1.1  christos          "R_OR1K_TLS_LDO_LO16", /* name */
    452  1.1  christos          FALSE,                 /* partial_inplace */
    453  1.1  christos          0x0,                   /* src_mask */
    454  1.1  christos          0xffff,                /* dst_mask */
    455  1.1  christos          FALSE),                /* pcrel_offset */
    456  1.1  christos 
    457  1.1  christos   HOWTO (R_OR1K_TLS_IE_HI16,    /* type */
    458  1.1  christos          16,                    /* rightshift */
    459  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    460  1.1  christos          16,                    /* bitsize */
    461  1.1  christos          FALSE,                 /* pc_relative */
    462  1.1  christos          0,                     /* bitpos */
    463  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    464  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    465  1.1  christos          "R_OR1K_TLS_IE_HI16",  /* name */
    466  1.1  christos          FALSE,                 /* partial_inplace */
    467  1.1  christos          0x0,                   /* src_mask */
    468  1.1  christos          0xffff,                /* dst_mask */
    469  1.1  christos          FALSE),                /* pcrel_offset */
    470  1.1  christos 
    471  1.1  christos   HOWTO (R_OR1K_TLS_IE_LO16,    /* type */
    472  1.1  christos          0,                     /* rightshift */
    473  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    474  1.1  christos          16,                    /* bitsize */
    475  1.1  christos          FALSE,                 /* pc_relative */
    476  1.1  christos          0,                     /* bitpos */
    477  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    478  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    479  1.1  christos          "R_OR1K_TLS_IE_LO16",  /* name */
    480  1.1  christos          FALSE,                 /* partial_inplace */
    481  1.1  christos          0x0,                   /* src_mask */
    482  1.1  christos          0xffff,                /* dst_mask */
    483  1.1  christos          FALSE),                /* pcrel_offset */
    484  1.1  christos 
    485  1.1  christos   HOWTO (R_OR1K_TLS_LE_HI16,    /* type */
    486  1.1  christos          16,                    /* rightshift */
    487  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    488  1.1  christos          16,                    /* bitsize */
    489  1.1  christos          FALSE,                 /* pc_relative */
    490  1.1  christos          0,                     /* bitpos */
    491  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    492  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    493  1.1  christos          "R_OR1K_TLS_LE_HI16",  /* name */
    494  1.1  christos          FALSE,                 /* partial_inplace */
    495  1.1  christos          0x0,                   /* src_mask */
    496  1.1  christos          0xffff,                /* dst_mask */
    497  1.1  christos          FALSE),                /* pcrel_offset */
    498  1.1  christos 
    499  1.1  christos   HOWTO (R_OR1K_TLS_LE_LO16,    /* type */
    500  1.1  christos          0,                     /* rightshift */
    501  1.1  christos          2,                     /* size (0 = byte, 1 = short, 2 = long) */
    502  1.1  christos          16,                    /* bitsize */
    503  1.1  christos          FALSE,                 /* pc_relative */
    504  1.1  christos          0,                     /* bitpos */
    505  1.1  christos          complain_overflow_dont, /* complain_on_overflow */
    506  1.1  christos          bfd_elf_generic_reloc, /* special_function */
    507  1.1  christos          "R_OR1K_TLS_LE_LO16",  /* name */
    508  1.1  christos          FALSE,                 /* partial_inplace */
    509  1.1  christos          0x0,                   /* src_mask */
    510  1.1  christos          0xffff,                /* dst_mask */
    511  1.1  christos          FALSE),                /* pcrel_offset */
    512  1.1  christos 
    513  1.1  christos };
    514  1.1  christos 
    515  1.1  christos /* Map BFD reloc types to Or1k ELF reloc types.  */
    516  1.1  christos 
    517  1.1  christos struct or1k_reloc_map
    518  1.1  christos {
    519  1.1  christos   bfd_reloc_code_real_type bfd_reloc_val;
    520  1.1  christos   unsigned int or1k_reloc_val;
    521  1.1  christos };
    522  1.1  christos 
    523  1.1  christos static const struct or1k_reloc_map or1k_reloc_map[] =
    524  1.1  christos {
    525  1.1  christos   { BFD_RELOC_NONE,             R_OR1K_NONE },
    526  1.1  christos   { BFD_RELOC_32,               R_OR1K_32 },
    527  1.1  christos   { BFD_RELOC_16,               R_OR1K_16 },
    528  1.1  christos   { BFD_RELOC_8,                R_OR1K_8 },
    529  1.1  christos   { BFD_RELOC_LO16,             R_OR1K_LO_16_IN_INSN },
    530  1.1  christos   { BFD_RELOC_HI16,             R_OR1K_HI_16_IN_INSN },
    531  1.1  christos   { BFD_RELOC_OR1K_REL_26,      R_OR1K_INSN_REL_26 },
    532  1.1  christos   { BFD_RELOC_VTABLE_ENTRY,     R_OR1K_GNU_VTENTRY },
    533  1.1  christos   { BFD_RELOC_VTABLE_INHERIT,   R_OR1K_GNU_VTINHERIT },
    534  1.1  christos   { BFD_RELOC_32_PCREL,         R_OR1K_32_PCREL },
    535  1.1  christos   { BFD_RELOC_16_PCREL,         R_OR1K_16_PCREL },
    536  1.1  christos   { BFD_RELOC_8_PCREL,          R_OR1K_8_PCREL },
    537  1.1  christos   { BFD_RELOC_OR1K_GOTPC_HI16,  R_OR1K_GOTPC_HI16 },
    538  1.1  christos   { BFD_RELOC_OR1K_GOTPC_LO16,  R_OR1K_GOTPC_LO16 },
    539  1.1  christos   { BFD_RELOC_OR1K_GOT16,       R_OR1K_GOT16 },
    540  1.1  christos   { BFD_RELOC_OR1K_PLT26,       R_OR1K_PLT26 },
    541  1.1  christos   { BFD_RELOC_OR1K_GOTOFF_HI16, R_OR1K_GOTOFF_HI16 },
    542  1.1  christos   { BFD_RELOC_OR1K_GOTOFF_LO16, R_OR1K_GOTOFF_LO16 },
    543  1.1  christos   { BFD_RELOC_OR1K_GLOB_DAT,    R_OR1K_GLOB_DAT },
    544  1.1  christos   { BFD_RELOC_OR1K_COPY,        R_OR1K_COPY },
    545  1.1  christos   { BFD_RELOC_OR1K_JMP_SLOT,    R_OR1K_JMP_SLOT },
    546  1.1  christos   { BFD_RELOC_OR1K_RELATIVE,    R_OR1K_RELATIVE },
    547  1.1  christos   { BFD_RELOC_OR1K_TLS_GD_HI16, R_OR1K_TLS_GD_HI16 },
    548  1.1  christos   { BFD_RELOC_OR1K_TLS_GD_LO16, R_OR1K_TLS_GD_LO16 },
    549  1.1  christos   { BFD_RELOC_OR1K_TLS_LDM_HI16,        R_OR1K_TLS_LDM_HI16 },
    550  1.1  christos   { BFD_RELOC_OR1K_TLS_LDM_LO16,        R_OR1K_TLS_LDM_LO16 },
    551  1.1  christos   { BFD_RELOC_OR1K_TLS_LDO_HI16,        R_OR1K_TLS_LDO_HI16 },
    552  1.1  christos   { BFD_RELOC_OR1K_TLS_LDO_LO16,        R_OR1K_TLS_LDO_LO16 },
    553  1.1  christos   { BFD_RELOC_OR1K_TLS_IE_HI16, R_OR1K_TLS_IE_HI16 },
    554  1.1  christos   { BFD_RELOC_OR1K_TLS_IE_LO16, R_OR1K_TLS_IE_LO16 },
    555  1.1  christos   { BFD_RELOC_OR1K_TLS_LE_HI16, R_OR1K_TLS_LE_HI16 },
    556  1.1  christos   { BFD_RELOC_OR1K_TLS_LE_LO16, R_OR1K_TLS_LE_LO16 },
    557  1.1  christos };
    558  1.1  christos 
    559  1.1  christos /* The linker needs to keep track of the number of relocs that it
    560  1.1  christos    decides to copy as dynamic relocs in check_relocs for each symbol.
    561  1.1  christos    This is so that it can later discard them if they are found to be
    562  1.1  christos    unnecessary.  We store the information in a field extending the
    563  1.1  christos    regular ELF linker hash table.  */
    564  1.1  christos 
    565  1.1  christos struct elf_or1k_dyn_relocs
    566  1.1  christos {
    567  1.1  christos   struct elf_or1k_dyn_relocs *next;
    568  1.1  christos 
    569  1.1  christos   /* The input section of the reloc.  */
    570  1.1  christos   asection *sec;
    571  1.1  christos 
    572  1.1  christos   /* Total number of relocs copied for the input section.  */
    573  1.1  christos   bfd_size_type count;
    574  1.1  christos 
    575  1.1  christos   /* Number of pc-relative relocs copied for the input section.  */
    576  1.1  christos   bfd_size_type pc_count;
    577  1.1  christos };
    578  1.1  christos 
    579  1.1  christos #define TLS_UNKNOWN    0
    580  1.1  christos #define TLS_NONE       1
    581  1.1  christos #define TLS_GD         2
    582  1.1  christos #define TLS_LD         3
    583  1.1  christos #define TLS_IE         4
    584  1.1  christos #define TLS_LE         5
    585  1.1  christos 
    586  1.1  christos /* ELF linker hash entry.  */
    587  1.1  christos struct elf_or1k_link_hash_entry
    588  1.1  christos {
    589  1.1  christos   struct elf_link_hash_entry root;
    590  1.1  christos 
    591  1.1  christos   /* Track dynamic relocs copied for this symbol.  */
    592  1.1  christos   struct elf_or1k_dyn_relocs *dyn_relocs;
    593  1.1  christos 
    594  1.1  christos   /* Track type of TLS access.  */
    595  1.1  christos   unsigned char tls_type;
    596  1.1  christos };
    597  1.1  christos 
    598  1.1  christos /* ELF object data.  */
    599  1.1  christos struct elf_or1k_obj_tdata
    600  1.1  christos {
    601  1.1  christos   struct elf_obj_tdata root;
    602  1.1  christos 
    603  1.1  christos   /* tls_type for each local got entry.  */
    604  1.1  christos   unsigned char *local_tls_type;
    605  1.1  christos };
    606  1.1  christos 
    607  1.1  christos #define elf_or1k_tdata(abfd) \
    608  1.1  christos   ((struct elf_or1k_obj_tdata *) (abfd)->tdata.any)
    609  1.1  christos 
    610  1.1  christos #define elf_or1k_local_tls_type(abfd) \
    611  1.1  christos   (elf_or1k_tdata (abfd)->local_tls_type)
    612  1.1  christos 
    613  1.1  christos /* ELF linker hash table.  */
    614  1.1  christos struct elf_or1k_link_hash_table
    615  1.1  christos {
    616  1.1  christos   struct elf_link_hash_table root;
    617  1.1  christos 
    618  1.1  christos   /* Short-cuts to get to dynamic linker sections.  */
    619  1.1  christos   asection *sgot;
    620  1.1  christos   asection *sgotplt;
    621  1.1  christos   asection *srelgot;
    622  1.1  christos   asection *splt;
    623  1.1  christos   asection *srelplt;
    624  1.1  christos   asection *sdynbss;
    625  1.1  christos   asection *srelbss;
    626  1.1  christos 
    627  1.1  christos   /* Small local sym to section mapping cache.  */
    628  1.1  christos   struct sym_cache sym_sec;
    629  1.1  christos };
    630  1.1  christos 
    631  1.1  christos /* Get the ELF linker hash table from a link_info structure.  */
    632  1.1  christos #define or1k_elf_hash_table(p) \
    633  1.1  christos   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    634  1.1  christos    == OR1K_ELF_DATA ? ((struct elf_or1k_link_hash_table *) ((p)->hash)) : NULL)
    635  1.1  christos 
    636  1.1  christos static bfd_boolean
    637  1.1  christos elf_or1k_mkobject (bfd *abfd)
    638  1.1  christos {
    639  1.1  christos   return bfd_elf_allocate_object (abfd, sizeof (struct elf_or1k_obj_tdata),
    640  1.1  christos                                   OR1K_ELF_DATA);
    641  1.1  christos }
    642  1.1  christos 
    643  1.1  christos /* Create an entry in an or1k ELF linker hash table.  */
    644  1.1  christos 
    645  1.1  christos static struct bfd_hash_entry *
    646  1.1  christos or1k_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
    647  1.1  christos                             struct bfd_hash_table *table,
    648  1.1  christos                             const char *string)
    649  1.1  christos {
    650  1.1  christos   struct elf_or1k_link_hash_entry *ret =
    651  1.1  christos     (struct elf_or1k_link_hash_entry *) entry;
    652  1.1  christos 
    653  1.1  christos   /* Allocate the structure if it has not already been allocated by a
    654  1.1  christos      subclass.  */
    655  1.1  christos   if (ret == NULL)
    656  1.1  christos     ret = bfd_hash_allocate (table,
    657  1.1  christos                              sizeof (struct elf_or1k_link_hash_entry));
    658  1.1  christos   if (ret == NULL)
    659  1.1  christos     return NULL;
    660  1.1  christos 
    661  1.1  christos   /* Call the allocation method of the superclass.  */
    662  1.1  christos   ret = ((struct elf_or1k_link_hash_entry *)
    663  1.1  christos          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
    664  1.1  christos                                      table, string));
    665  1.1  christos   if (ret != NULL)
    666  1.1  christos     {
    667  1.1  christos       struct elf_or1k_link_hash_entry *eh;
    668  1.1  christos 
    669  1.1  christos       eh = (struct elf_or1k_link_hash_entry *) ret;
    670  1.1  christos       eh->dyn_relocs = NULL;
    671  1.1  christos       eh->tls_type = TLS_UNKNOWN;
    672  1.1  christos     }
    673  1.1  christos 
    674  1.1  christos   return (struct bfd_hash_entry *) ret;
    675  1.1  christos }
    676  1.1  christos 
    677  1.1  christos /* Create an or1k ELF linker hash table.  */
    678  1.1  christos 
    679  1.1  christos static struct bfd_link_hash_table *
    680  1.1  christos or1k_elf_link_hash_table_create (bfd *abfd)
    681  1.1  christos {
    682  1.1  christos   struct elf_or1k_link_hash_table *ret;
    683  1.1  christos   bfd_size_type amt = sizeof (struct elf_or1k_link_hash_table);
    684  1.1  christos 
    685  1.1  christos   ret = bfd_zmalloc (amt);
    686  1.1  christos   if (ret == NULL)
    687  1.1  christos     return NULL;
    688  1.1  christos 
    689  1.1  christos   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
    690  1.1  christos                                       or1k_elf_link_hash_newfunc,
    691  1.1  christos                                       sizeof (struct elf_or1k_link_hash_entry),
    692  1.1  christos                                       OR1K_ELF_DATA))
    693  1.1  christos     {
    694  1.1  christos       free (ret);
    695  1.1  christos       return NULL;
    696  1.1  christos     }
    697  1.1  christos 
    698  1.1  christos   return &ret->root.root;
    699  1.1  christos }
    700  1.1  christos 
    701  1.1  christos static reloc_howto_type *
    702  1.1  christos or1k_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
    703  1.1  christos 			bfd_reloc_code_real_type code)
    704  1.1  christos {
    705  1.1  christos   unsigned int i;
    706  1.1  christos 
    707  1.3  christos   for (i = ARRAY_SIZE (or1k_reloc_map); i--;)
    708  1.1  christos     if (or1k_reloc_map[i].bfd_reloc_val == code)
    709  1.1  christos       return & or1k_elf_howto_table[or1k_reloc_map[i].or1k_reloc_val];
    710  1.1  christos 
    711  1.1  christos   return NULL;
    712  1.1  christos }
    713  1.1  christos 
    714  1.1  christos static reloc_howto_type *
    715  1.1  christos or1k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    716  1.1  christos 			const char *r_name)
    717  1.1  christos {
    718  1.1  christos   unsigned int i;
    719  1.1  christos 
    720  1.1  christos   for (i = 0;
    721  1.1  christos        i < (sizeof (or1k_elf_howto_table)
    722  1.1  christos             / sizeof (or1k_elf_howto_table[0]));
    723  1.1  christos        i++)
    724  1.1  christos     if (or1k_elf_howto_table[i].name != NULL
    725  1.1  christos         && strcasecmp (or1k_elf_howto_table[i].name, r_name) == 0)
    726  1.1  christos       return &or1k_elf_howto_table[i];
    727  1.1  christos 
    728  1.1  christos   return NULL;
    729  1.1  christos }
    730  1.1  christos 
    731  1.1  christos /* Set the howto pointer for an Or1k ELF reloc.  */
    732  1.1  christos 
    733  1.1  christos static void
    734  1.1  christos or1k_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
    735  1.1  christos 			 arelent * cache_ptr,
    736  1.1  christos 			 Elf_Internal_Rela * dst)
    737  1.1  christos {
    738  1.1  christos   unsigned int r_type;
    739  1.1  christos 
    740  1.1  christos   r_type = ELF32_R_TYPE (dst->r_info);
    741  1.1  christos   if (r_type >= (unsigned int) R_OR1K_max)
    742  1.1  christos     {
    743  1.3  christos       _bfd_error_handler (_("%B: invalid OR1K reloc number: %d"), abfd, r_type);
    744  1.1  christos       r_type = 0;
    745  1.1  christos     }
    746  1.1  christos   cache_ptr->howto = & or1k_elf_howto_table[r_type];
    747  1.1  christos }
    748  1.1  christos 
    749  1.1  christos 
    750  1.1  christos /* Return the relocation value for @tpoff relocations..  */
    751  1.1  christos static bfd_vma
    752  1.1  christos tpoff (struct bfd_link_info *info, bfd_vma address)
    753  1.1  christos {
    754  1.1  christos   /* If tls_sec is NULL, we should have signalled an error already.  */
    755  1.1  christos   if (elf_hash_table (info)->tls_sec == NULL)
    756  1.1  christos     return 0;
    757  1.1  christos 
    758  1.1  christos   /* The thread pointer on or1k stores the address after the TCB where
    759  1.1  christos      the data is, just compute the difference. No need to compensate
    760  1.1  christos      for the size of TCB.  */
    761  1.1  christos   return (address - elf_hash_table (info)->tls_sec->vma);
    762  1.1  christos }
    763  1.1  christos 
    764  1.1  christos /* Relocate an Or1k ELF section.
    765  1.1  christos 
    766  1.1  christos    The RELOCATE_SECTION function is called by the new ELF backend linker
    767  1.1  christos    to handle the relocations for a section.
    768  1.1  christos 
    769  1.1  christos    The relocs are always passed as Rela structures; if the section
    770  1.1  christos    actually uses Rel structures, the r_addend field will always be
    771  1.1  christos    zero.
    772  1.1  christos 
    773  1.1  christos    This function is responsible for adjusting the section contents as
    774  1.1  christos    necessary, and (if using Rela relocs and generating a relocatable
    775  1.1  christos    output file) adjusting the reloc addend as necessary.
    776  1.1  christos 
    777  1.1  christos    This function does not have to worry about setting the reloc
    778  1.1  christos    address or the reloc symbol index.
    779  1.1  christos 
    780  1.1  christos    LOCAL_SYMS is a pointer to the swapped in local symbols.
    781  1.1  christos 
    782  1.1  christos    LOCAL_SECTIONS is an array giving the section in the input file
    783  1.1  christos    corresponding to the st_shndx field of each local symbol.
    784  1.1  christos 
    785  1.1  christos    The global hash table entry for the global symbols can be found
    786  1.1  christos    via elf_sym_hashes (input_bfd).
    787  1.1  christos 
    788  1.1  christos    When generating relocatable output, this function must handle
    789  1.1  christos    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
    790  1.1  christos    going to be the section symbol corresponding to the output
    791  1.1  christos    section, which means that the addend must be adjusted
    792  1.1  christos    accordingly.  */
    793  1.1  christos 
    794  1.1  christos static bfd_boolean
    795  1.1  christos or1k_elf_relocate_section (bfd *output_bfd,
    796  1.1  christos 			   struct bfd_link_info *info,
    797  1.1  christos 			   bfd *input_bfd,
    798  1.1  christos 			   asection *input_section,
    799  1.1  christos 			   bfd_byte *contents,
    800  1.1  christos 			   Elf_Internal_Rela *relocs,
    801  1.1  christos 			   Elf_Internal_Sym *local_syms,
    802  1.1  christos 			   asection **local_sections)
    803  1.1  christos {
    804  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
    805  1.1  christos   struct elf_link_hash_entry **sym_hashes;
    806  1.1  christos   Elf_Internal_Rela *rel;
    807  1.1  christos   Elf_Internal_Rela *relend;
    808  1.1  christos   struct elf_or1k_link_hash_table *htab = or1k_elf_hash_table (info);
    809  1.1  christos   bfd *dynobj;
    810  1.1  christos   asection *sreloc;
    811  1.1  christos   bfd_vma *local_got_offsets;
    812  1.1  christos   asection *sgot;
    813  1.1  christos 
    814  1.1  christos   if (htab == NULL)
    815  1.1  christos     return FALSE;
    816  1.1  christos 
    817  1.1  christos   dynobj = htab->root.dynobj;
    818  1.1  christos   local_got_offsets = elf_local_got_offsets (input_bfd);
    819  1.1  christos 
    820  1.1  christos   sreloc = elf_section_data (input_section)->sreloc;
    821  1.1  christos 
    822  1.1  christos   sgot = htab->sgot;
    823  1.1  christos 
    824  1.1  christos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
    825  1.1  christos   sym_hashes = elf_sym_hashes (input_bfd);
    826  1.1  christos   relend = relocs + input_section->reloc_count;
    827  1.1  christos 
    828  1.1  christos   for (rel = relocs; rel < relend; rel++)
    829  1.1  christos     {
    830  1.1  christos       reloc_howto_type *howto;
    831  1.1  christos       unsigned long r_symndx;
    832  1.1  christos       Elf_Internal_Sym *sym;
    833  1.1  christos       asection *sec;
    834  1.1  christos       struct elf_link_hash_entry *h;
    835  1.1  christos       bfd_vma relocation;
    836  1.1  christos       bfd_reloc_status_type r;
    837  1.1  christos       const char *name = NULL;
    838  1.1  christos       int r_type;
    839  1.1  christos 
    840  1.1  christos       r_type = ELF32_R_TYPE (rel->r_info);
    841  1.1  christos       r_symndx = ELF32_R_SYM (rel->r_info);
    842  1.1  christos 
    843  1.1  christos       if (r_type == R_OR1K_GNU_VTINHERIT
    844  1.1  christos           || r_type == R_OR1K_GNU_VTENTRY)
    845  1.1  christos         continue;
    846  1.1  christos 
    847  1.1  christos       if (r_type < 0 || r_type >= (int) R_OR1K_max)
    848  1.1  christos         {
    849  1.1  christos           bfd_set_error (bfd_error_bad_value);
    850  1.1  christos           return FALSE;
    851  1.1  christos         }
    852  1.1  christos 
    853  1.1  christos       howto = or1k_elf_howto_table + ELF32_R_TYPE (rel->r_info);
    854  1.1  christos       h = NULL;
    855  1.1  christos       sym = NULL;
    856  1.1  christos       sec = NULL;
    857  1.1  christos 
    858  1.1  christos       if (r_symndx < symtab_hdr->sh_info)
    859  1.1  christos         {
    860  1.1  christos           sym = local_syms + r_symndx;
    861  1.1  christos           sec = local_sections[r_symndx];
    862  1.1  christos           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
    863  1.1  christos 
    864  1.1  christos           name = bfd_elf_string_from_elf_section
    865  1.1  christos             (input_bfd, symtab_hdr->sh_link, sym->st_name);
    866  1.1  christos           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
    867  1.1  christos         }
    868  1.1  christos       else
    869  1.1  christos         {
    870  1.1  christos           bfd_boolean unresolved_reloc, warned, ignored;
    871  1.1  christos 
    872  1.1  christos           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
    873  1.1  christos                                    r_symndx, symtab_hdr, sym_hashes,
    874  1.1  christos                                    h, sec, relocation,
    875  1.1  christos                                    unresolved_reloc, warned, ignored);
    876  1.1  christos         }
    877  1.1  christos 
    878  1.1  christos       if (sec != NULL && discarded_section (sec))
    879  1.1  christos         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
    880  1.1  christos                                          rel, 1, relend, howto, 0, contents);
    881  1.1  christos 
    882  1.4  christos       if (bfd_link_relocatable (info))
    883  1.1  christos         continue;
    884  1.1  christos 
    885  1.1  christos       switch (howto->type)
    886  1.1  christos         {
    887  1.1  christos         case R_OR1K_PLT26:
    888  1.1  christos           {
    889  1.1  christos             if (htab->splt != NULL && h != NULL
    890  1.1  christos                 && h->plt.offset != (bfd_vma) -1)
    891  1.1  christos               {
    892  1.1  christos                 relocation = (htab->splt->output_section->vma
    893  1.1  christos                               + htab->splt->output_offset
    894  1.1  christos                               + h->plt.offset);
    895  1.1  christos               }
    896  1.1  christos             break;
    897  1.1  christos           }
    898  1.1  christos 
    899  1.1  christos         case R_OR1K_GOT16:
    900  1.1  christos           /* Relocation is to the entry for this symbol in the global
    901  1.1  christos              offset table.  */
    902  1.1  christos           BFD_ASSERT (sgot != NULL);
    903  1.1  christos           if (h != NULL)
    904  1.1  christos             {
    905  1.1  christos               bfd_boolean dyn;
    906  1.1  christos               bfd_vma off;
    907  1.1  christos 
    908  1.1  christos               off = h->got.offset;
    909  1.1  christos               BFD_ASSERT (off != (bfd_vma) -1);
    910  1.1  christos 
    911  1.1  christos               dyn = htab->root.dynamic_sections_created;
    912  1.4  christos               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
    913  1.4  christos 						     bfd_link_pic (info),
    914  1.4  christos 						     h)
    915  1.4  christos                   || (bfd_link_pic (info)
    916  1.1  christos                       && SYMBOL_REFERENCES_LOCAL (info, h)))
    917  1.1  christos                 {
    918  1.1  christos                   /* This is actually a static link, or it is a
    919  1.1  christos                      -Bsymbolic link and the symbol is defined
    920  1.1  christos                      locally, or the symbol was forced to be local
    921  1.1  christos                      because of a version file.  We must initialize
    922  1.1  christos                      this entry in the global offset table.  Since the
    923  1.1  christos                      offset must always be a multiple of 4, we use the
    924  1.1  christos                      least significant bit to record whether we have
    925  1.1  christos                      initialized it already.
    926  1.1  christos 
    927  1.1  christos                      When doing a dynamic link, we create a .rela.got
    928  1.1  christos                      relocation entry to initialize the value.  This
    929  1.1  christos                      is done in the finish_dynamic_symbol routine.  */
    930  1.1  christos                   if ((off & 1) != 0)
    931  1.1  christos                     off &= ~1;
    932  1.1  christos                   else
    933  1.1  christos                     {
    934  1.1  christos                       /* Write entry in GOT.  */
    935  1.1  christos                       bfd_put_32 (output_bfd, relocation,
    936  1.1  christos                                   sgot->contents + off);
    937  1.1  christos                       /* Mark GOT entry as having been written.  */
    938  1.1  christos                       h->got.offset |= 1;
    939  1.1  christos                     }
    940  1.1  christos                 }
    941  1.1  christos 
    942  1.1  christos               relocation = sgot->output_offset + off;
    943  1.1  christos             }
    944  1.1  christos           else
    945  1.1  christos             {
    946  1.1  christos               bfd_vma off;
    947  1.1  christos               bfd_byte *loc;
    948  1.1  christos 
    949  1.1  christos               BFD_ASSERT (local_got_offsets != NULL
    950  1.1  christos                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
    951  1.1  christos 
    952  1.1  christos               /* Get offset into GOT table.  */
    953  1.1  christos               off = local_got_offsets[r_symndx];
    954  1.1  christos 
    955  1.1  christos               /* The offset must always be a multiple of 4.  We use
    956  1.1  christos                  the least significant bit to record whether we have
    957  1.1  christos                  already processed this entry.  */
    958  1.1  christos               if ((off & 1) != 0)
    959  1.1  christos                 off &= ~1;
    960  1.1  christos               else
    961  1.1  christos                 {
    962  1.1  christos                   /* Write entry in GOT.  */
    963  1.1  christos                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
    964  1.4  christos                   if (bfd_link_pic (info))
    965  1.1  christos                     {
    966  1.1  christos                       asection *srelgot;
    967  1.1  christos                       Elf_Internal_Rela outrel;
    968  1.1  christos 
    969  1.1  christos                       /* We need to generate a R_OR1K_RELATIVE reloc
    970  1.1  christos                          for the dynamic linker.  */
    971  1.1  christos                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
    972  1.1  christos                       BFD_ASSERT (srelgot != NULL);
    973  1.1  christos 
    974  1.1  christos                       outrel.r_offset = (sgot->output_section->vma
    975  1.1  christos                                          + sgot->output_offset
    976  1.1  christos                                          + off);
    977  1.1  christos                       outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
    978  1.1  christos                       outrel.r_addend = relocation;
    979  1.1  christos                       loc = srelgot->contents;
    980  1.1  christos                       loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
    981  1.1  christos                       bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
    982  1.1  christos                       ++srelgot->reloc_count;
    983  1.1  christos                     }
    984  1.1  christos 
    985  1.1  christos                   local_got_offsets[r_symndx] |= 1;
    986  1.1  christos                 }
    987  1.1  christos               relocation = sgot->output_offset + off;
    988  1.1  christos             }
    989  1.1  christos 
    990  1.1  christos           /* Addend should be zero.  */
    991  1.1  christos           if (rel->r_addend != 0)
    992  1.1  christos             (*_bfd_error_handler)
    993  1.1  christos               (_("internal error: addend should be zero for R_OR1K_GOT16"));
    994  1.1  christos 
    995  1.1  christos           break;
    996  1.1  christos 
    997  1.1  christos         case R_OR1K_GOTOFF_LO16:
    998  1.1  christos         case R_OR1K_GOTOFF_HI16:
    999  1.1  christos           /* Relocation is offset from GOT.  */
   1000  1.1  christos           BFD_ASSERT (sgot != NULL);
   1001  1.1  christos           relocation -= sgot->output_section->vma;
   1002  1.1  christos           break;
   1003  1.1  christos 
   1004  1.1  christos         case R_OR1K_INSN_REL_26:
   1005  1.1  christos         case R_OR1K_HI_16_IN_INSN:
   1006  1.1  christos         case R_OR1K_LO_16_IN_INSN:
   1007  1.1  christos         case R_OR1K_32:
   1008  1.1  christos           /* R_OR1K_16? */
   1009  1.1  christos           {
   1010  1.1  christos             /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
   1011  1.1  christos                from removed linkonce sections, or sections discarded by
   1012  1.1  christos                a linker script.  */
   1013  1.1  christos             if (r_symndx == STN_UNDEF
   1014  1.1  christos                 || (input_section->flags & SEC_ALLOC) == 0)
   1015  1.1  christos               break;
   1016  1.1  christos 
   1017  1.4  christos             if ((bfd_link_pic (info)
   1018  1.1  christos                  && (h == NULL
   1019  1.1  christos                      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   1020  1.1  christos                      || h->root.type != bfd_link_hash_undefweak)
   1021  1.1  christos 		 && (howto->type != R_OR1K_INSN_REL_26
   1022  1.1  christos 		     || !SYMBOL_CALLS_LOCAL (info, h)))
   1023  1.4  christos                 || (!bfd_link_pic (info)
   1024  1.1  christos                     && h != NULL
   1025  1.1  christos                     && h->dynindx != -1
   1026  1.1  christos                     && !h->non_got_ref
   1027  1.1  christos                     && ((h->def_dynamic
   1028  1.1  christos                          && !h->def_regular)
   1029  1.1  christos                         || h->root.type == bfd_link_hash_undefweak
   1030  1.1  christos                         || h->root.type == bfd_link_hash_undefined)))
   1031  1.1  christos               {
   1032  1.1  christos                 Elf_Internal_Rela outrel;
   1033  1.1  christos                 bfd_byte *loc;
   1034  1.1  christos                 bfd_boolean skip;
   1035  1.1  christos 
   1036  1.1  christos                 /* When generating a shared object, these relocations
   1037  1.1  christos                    are copied into the output file to be resolved at run
   1038  1.1  christos                    time.  */
   1039  1.1  christos 
   1040  1.1  christos                 BFD_ASSERT (sreloc != NULL);
   1041  1.1  christos 
   1042  1.1  christos                 skip = FALSE;
   1043  1.1  christos 
   1044  1.1  christos                 outrel.r_offset =
   1045  1.1  christos                   _bfd_elf_section_offset (output_bfd, info, input_section,
   1046  1.1  christos                                            rel->r_offset);
   1047  1.1  christos                 if (outrel.r_offset == (bfd_vma) -1)
   1048  1.1  christos                   skip = TRUE;
   1049  1.1  christos                 else if (outrel.r_offset == (bfd_vma) -2)
   1050  1.1  christos                   skip = TRUE;
   1051  1.1  christos                 outrel.r_offset += (input_section->output_section->vma
   1052  1.1  christos                                     + input_section->output_offset);
   1053  1.1  christos 
   1054  1.1  christos                 if (skip)
   1055  1.1  christos                   memset (&outrel, 0, sizeof outrel);
   1056  1.1  christos                 /* h->dynindx may be -1 if the symbol was marked to
   1057  1.1  christos                    become local.  */
   1058  1.1  christos                 else if (h != NULL
   1059  1.1  christos                          && ((! info->symbolic && h->dynindx != -1)
   1060  1.1  christos                              || !h->def_regular))
   1061  1.1  christos                   {
   1062  1.1  christos                     BFD_ASSERT (h->dynindx != -1);
   1063  1.1  christos                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
   1064  1.1  christos                     outrel.r_addend = rel->r_addend;
   1065  1.1  christos                   }
   1066  1.1  christos                 else
   1067  1.1  christos                   {
   1068  1.1  christos                     if (r_type == R_OR1K_32)
   1069  1.1  christos                       {
   1070  1.1  christos                         outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
   1071  1.1  christos                         outrel.r_addend = relocation + rel->r_addend;
   1072  1.1  christos                       }
   1073  1.1  christos                     else
   1074  1.1  christos                       {
   1075  1.1  christos                         BFD_FAIL ();
   1076  1.1  christos                         (*_bfd_error_handler)
   1077  1.1  christos                           (_("%B: probably compiled without -fPIC?"),
   1078  1.1  christos                            input_bfd);
   1079  1.1  christos                         bfd_set_error (bfd_error_bad_value);
   1080  1.1  christos                         return FALSE;
   1081  1.1  christos                       }
   1082  1.1  christos                   }
   1083  1.1  christos 
   1084  1.1  christos                 loc = sreloc->contents;
   1085  1.1  christos                 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
   1086  1.1  christos                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   1087  1.1  christos                 break;
   1088  1.1  christos               }
   1089  1.1  christos             break;
   1090  1.1  christos           }
   1091  1.1  christos 
   1092  1.1  christos         case R_OR1K_TLS_LDM_HI16:
   1093  1.1  christos         case R_OR1K_TLS_LDM_LO16:
   1094  1.1  christos         case R_OR1K_TLS_LDO_HI16:
   1095  1.1  christos         case R_OR1K_TLS_LDO_LO16:
   1096  1.1  christos           /* TODO: implement support for local dynamic.  */
   1097  1.1  christos           BFD_FAIL ();
   1098  1.1  christos           (*_bfd_error_handler)
   1099  1.1  christos             (_("%B: support for local dynamic not implemented"),
   1100  1.1  christos              input_bfd);
   1101  1.1  christos           bfd_set_error (bfd_error_bad_value);
   1102  1.1  christos           return FALSE;
   1103  1.1  christos 
   1104  1.1  christos 
   1105  1.1  christos         case R_OR1K_TLS_GD_HI16:
   1106  1.1  christos         case R_OR1K_TLS_GD_LO16:
   1107  1.1  christos         case R_OR1K_TLS_IE_HI16:
   1108  1.1  christos         case R_OR1K_TLS_IE_LO16:
   1109  1.1  christos           {
   1110  1.1  christos             bfd_vma gotoff;
   1111  1.1  christos             Elf_Internal_Rela rela;
   1112  1.1  christos             bfd_byte *loc;
   1113  1.1  christos             int dynamic;
   1114  1.1  christos 
   1115  1.1  christos             sreloc = bfd_get_section_by_name (dynobj, ".rela.got");
   1116  1.1  christos 
   1117  1.1  christos             /* Mark as TLS related GOT entry by setting
   1118  1.1  christos                bit 2 as well as bit 1.  */
   1119  1.1  christos             if (h != NULL)
   1120  1.1  christos               {
   1121  1.1  christos                 gotoff = h->got.offset;
   1122  1.1  christos                 h->got.offset |= 3;
   1123  1.1  christos               }
   1124  1.1  christos             else
   1125  1.1  christos               {
   1126  1.1  christos                 gotoff = local_got_offsets[r_symndx];
   1127  1.1  christos                 local_got_offsets[r_symndx] |= 3;
   1128  1.1  christos               }
   1129  1.1  christos 
   1130  1.1  christos             /* Only process the relocation once.  */
   1131  1.1  christos             if (gotoff & 1)
   1132  1.1  christos               {
   1133  1.1  christos                 relocation = sgot->output_offset + (gotoff  & ~3);
   1134  1.1  christos                 break;
   1135  1.1  christos               }
   1136  1.1  christos 
   1137  1.1  christos             BFD_ASSERT (elf_hash_table (info)->hgot == NULL
   1138  1.1  christos 			|| elf_hash_table (info)->hgot->root.u.def.value == 0);
   1139  1.1  christos 
   1140  1.1  christos             /* Dynamic entries will require relocations. if we do not need
   1141  1.1  christos                them we will just use the default R_OR1K_NONE and
   1142  1.1  christos                not set anything.  */
   1143  1.4  christos             dynamic = bfd_link_pic (info)
   1144  1.1  christos 	      || (sec && (sec->flags & SEC_ALLOC) != 0
   1145  1.1  christos 		  && h != NULL
   1146  1.1  christos 		  && (h->root.type == bfd_link_hash_defweak || !h->def_regular));
   1147  1.1  christos 
   1148  1.1  christos             /* Shared GD.  */
   1149  1.1  christos             if (dynamic && (howto->type == R_OR1K_TLS_GD_HI16
   1150  1.1  christos 			    || howto->type == R_OR1K_TLS_GD_LO16))
   1151  1.1  christos               {
   1152  1.1  christos                 int i;
   1153  1.1  christos 
   1154  1.1  christos                 /* Add DTPMOD and DTPOFF GOT and rela entries.  */
   1155  1.1  christos                 for (i = 0; i < 2; ++i)
   1156  1.1  christos                   {
   1157  1.1  christos                     rela.r_offset = sgot->output_section->vma +
   1158  1.1  christos                       sgot->output_offset + gotoff + i*4;
   1159  1.1  christos                     if (h != NULL && h->dynindx != -1)
   1160  1.1  christos                       {
   1161  1.1  christos                         rela.r_info = ELF32_R_INFO (h->dynindx,
   1162  1.1  christos                             (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
   1163  1.1  christos                         rela.r_addend = 0;
   1164  1.1  christos                       }
   1165  1.1  christos                     else
   1166  1.1  christos                       {
   1167  1.1  christos                         rela.r_info = ELF32_R_INFO (0,
   1168  1.1  christos                             (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
   1169  1.1  christos                         rela.r_addend = tpoff (info, relocation);
   1170  1.1  christos                       }
   1171  1.1  christos 
   1172  1.1  christos                     loc = sreloc->contents;
   1173  1.1  christos                     loc += sreloc->reloc_count++ *
   1174  1.1  christos                       sizeof (Elf32_External_Rela);
   1175  1.1  christos 
   1176  1.1  christos                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   1177  1.1  christos                     bfd_put_32 (output_bfd, 0, sgot->contents + gotoff + i*4);
   1178  1.1  christos                   }
   1179  1.1  christos               }
   1180  1.1  christos             /* Static GD.  */
   1181  1.1  christos             else if (howto->type == R_OR1K_TLS_GD_HI16
   1182  1.1  christos                      || howto->type == R_OR1K_TLS_GD_LO16)
   1183  1.1  christos               {
   1184  1.1  christos                 bfd_put_32 (output_bfd, 1, sgot->contents + gotoff);
   1185  1.1  christos                 bfd_put_32 (output_bfd, tpoff (info, relocation),
   1186  1.1  christos                     sgot->contents + gotoff + 4);
   1187  1.1  christos               }
   1188  1.1  christos             /* Shared IE.  */
   1189  1.1  christos             else if (dynamic)
   1190  1.1  christos               {
   1191  1.1  christos                 /* Add TPOFF GOT and rela entries.  */
   1192  1.1  christos                 rela.r_offset = sgot->output_section->vma +
   1193  1.1  christos                   sgot->output_offset + gotoff;
   1194  1.1  christos                 if (h != NULL && h->dynindx != -1)
   1195  1.1  christos                   {
   1196  1.1  christos                     rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_TLS_TPOFF);
   1197  1.1  christos                     rela.r_addend = 0;
   1198  1.1  christos                   }
   1199  1.1  christos                 else
   1200  1.1  christos                   {
   1201  1.1  christos                     rela.r_info = ELF32_R_INFO (0, R_OR1K_TLS_TPOFF);
   1202  1.1  christos                     rela.r_addend = tpoff (info, relocation);
   1203  1.1  christos                   }
   1204  1.1  christos 
   1205  1.1  christos                 loc = sreloc->contents;
   1206  1.1  christos                 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
   1207  1.1  christos 
   1208  1.1  christos                 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   1209  1.1  christos                 bfd_put_32 (output_bfd, 0, sgot->contents + gotoff);
   1210  1.1  christos               }
   1211  1.1  christos             /* Static IE.  */
   1212  1.1  christos             else
   1213  1.1  christos               {
   1214  1.1  christos                 bfd_put_32 (output_bfd, tpoff (info, relocation),
   1215  1.1  christos 			    sgot->contents + gotoff);
   1216  1.1  christos               }
   1217  1.1  christos             relocation = sgot->output_offset + gotoff;
   1218  1.1  christos             break;
   1219  1.1  christos           }
   1220  1.1  christos         case R_OR1K_TLS_LE_HI16:
   1221  1.1  christos         case R_OR1K_TLS_LE_LO16:
   1222  1.1  christos 
   1223  1.1  christos           /* Relocation is offset from TP.  */
   1224  1.1  christos           relocation = tpoff (info, relocation);
   1225  1.1  christos           break;
   1226  1.1  christos 
   1227  1.1  christos         case R_OR1K_TLS_DTPMOD:
   1228  1.1  christos         case R_OR1K_TLS_DTPOFF:
   1229  1.1  christos         case R_OR1K_TLS_TPOFF:
   1230  1.1  christos           /* These are resolved dynamically on load and shouldn't
   1231  1.1  christos              be used as linker input.  */
   1232  1.1  christos           BFD_FAIL ();
   1233  1.1  christos           (*_bfd_error_handler)
   1234  1.1  christos             (_("%B: will not resolve runtime TLS relocation"),
   1235  1.1  christos              input_bfd);
   1236  1.1  christos           bfd_set_error (bfd_error_bad_value);
   1237  1.1  christos           return FALSE;
   1238  1.1  christos 
   1239  1.1  christos         default:
   1240  1.1  christos           break;
   1241  1.1  christos         }
   1242  1.1  christos       r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
   1243  1.1  christos                                     rel->r_offset, relocation, rel->r_addend);
   1244  1.1  christos 
   1245  1.1  christos       if (r != bfd_reloc_ok)
   1246  1.1  christos         {
   1247  1.1  christos           const char *msg = NULL;
   1248  1.1  christos 
   1249  1.1  christos           switch (r)
   1250  1.1  christos             {
   1251  1.1  christos             case bfd_reloc_overflow:
   1252  1.4  christos 	      (*info->callbacks->reloc_overflow)
   1253  1.1  christos                 (info, (h ? &h->root : NULL), name, howto->name,
   1254  1.1  christos                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
   1255  1.1  christos               break;
   1256  1.1  christos 
   1257  1.1  christos             case bfd_reloc_undefined:
   1258  1.4  christos 	      (*info->callbacks->undefined_symbol)
   1259  1.1  christos                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
   1260  1.1  christos               break;
   1261  1.1  christos 
   1262  1.1  christos             case bfd_reloc_outofrange:
   1263  1.1  christos               msg = _("internal error: out of range error");
   1264  1.1  christos               break;
   1265  1.1  christos 
   1266  1.1  christos             case bfd_reloc_notsupported:
   1267  1.1  christos               msg = _("internal error: unsupported relocation error");
   1268  1.1  christos               break;
   1269  1.1  christos 
   1270  1.1  christos             case bfd_reloc_dangerous:
   1271  1.1  christos               msg = _("internal error: dangerous relocation");
   1272  1.1  christos               break;
   1273  1.1  christos 
   1274  1.1  christos             default:
   1275  1.1  christos               msg = _("internal error: unknown error");
   1276  1.1  christos               break;
   1277  1.1  christos             }
   1278  1.1  christos 
   1279  1.1  christos           if (msg)
   1280  1.4  christos 	    (*info->callbacks->warning) (info, msg, name, input_bfd,
   1281  1.4  christos 					 input_section, rel->r_offset);
   1282  1.1  christos         }
   1283  1.1  christos     }
   1284  1.1  christos 
   1285  1.1  christos   return TRUE;
   1286  1.1  christos }
   1287  1.1  christos 
   1288  1.1  christos /* Return the section that should be marked against GC for a given
   1289  1.1  christos    relocation.  */
   1290  1.1  christos 
   1291  1.1  christos static asection *
   1292  1.1  christos or1k_elf_gc_mark_hook (asection *sec,
   1293  1.1  christos 		       struct bfd_link_info *info,
   1294  1.1  christos 		       Elf_Internal_Rela *rel,
   1295  1.1  christos 		       struct elf_link_hash_entry *h,
   1296  1.1  christos 		       Elf_Internal_Sym *sym)
   1297  1.1  christos {
   1298  1.1  christos   if (h != NULL)
   1299  1.1  christos     switch (ELF32_R_TYPE (rel->r_info))
   1300  1.1  christos       {
   1301  1.1  christos       case R_OR1K_GNU_VTINHERIT:
   1302  1.1  christos       case R_OR1K_GNU_VTENTRY:
   1303  1.1  christos         return NULL;
   1304  1.1  christos       }
   1305  1.1  christos 
   1306  1.1  christos   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   1307  1.1  christos }
   1308  1.1  christos 
   1309  1.1  christos static bfd_boolean
   1310  1.1  christos or1k_elf_gc_sweep_hook (bfd *abfd,
   1311  1.1  christos                         struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1312  1.1  christos                         asection *sec,
   1313  1.1  christos                         const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
   1314  1.1  christos {
   1315  1.1  christos   /* Update the got entry reference counts for the section being removed.  */
   1316  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1317  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   1318  1.1  christos   bfd_signed_vma *local_got_refcounts;
   1319  1.1  christos   const Elf_Internal_Rela *rel, *relend;
   1320  1.1  christos 
   1321  1.1  christos   elf_section_data (sec)->local_dynrel = NULL;
   1322  1.1  christos 
   1323  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1324  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   1325  1.1  christos   local_got_refcounts = elf_local_got_refcounts (abfd);
   1326  1.1  christos 
   1327  1.1  christos   relend = relocs + sec->reloc_count;
   1328  1.1  christos   for (rel = relocs; rel < relend; rel++)
   1329  1.1  christos     {
   1330  1.1  christos       unsigned long r_symndx;
   1331  1.1  christos       struct elf_link_hash_entry *h = NULL;
   1332  1.1  christos 
   1333  1.1  christos       r_symndx = ELF32_R_SYM (rel->r_info);
   1334  1.1  christos       if (r_symndx >= symtab_hdr->sh_info)
   1335  1.1  christos         {
   1336  1.1  christos           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   1337  1.1  christos           while (h->root.type == bfd_link_hash_indirect
   1338  1.1  christos                  || h->root.type == bfd_link_hash_warning)
   1339  1.1  christos             h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1340  1.1  christos         }
   1341  1.1  christos 
   1342  1.1  christos       switch (ELF32_R_TYPE (rel->r_info))
   1343  1.1  christos         {
   1344  1.1  christos         case R_OR1K_GOT16:
   1345  1.1  christos           if (h != NULL)
   1346  1.1  christos             {
   1347  1.1  christos               if (h->got.refcount > 0)
   1348  1.1  christos                 h->got.refcount--;
   1349  1.1  christos             }
   1350  1.1  christos           else
   1351  1.1  christos             {
   1352  1.1  christos               if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
   1353  1.1  christos                 local_got_refcounts[r_symndx]--;
   1354  1.1  christos             }
   1355  1.1  christos           break;
   1356  1.1  christos 
   1357  1.1  christos         default:
   1358  1.1  christos           break;
   1359  1.1  christos         }
   1360  1.1  christos     }
   1361  1.1  christos   return TRUE;
   1362  1.1  christos }
   1363  1.1  christos 
   1364  1.1  christos /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
   1365  1.1  christos    shortcuts to them in our hash table.  */
   1366  1.1  christos 
   1367  1.1  christos static bfd_boolean
   1368  1.1  christos create_got_section (bfd *dynobj, struct bfd_link_info *info)
   1369  1.1  christos {
   1370  1.1  christos   struct elf_or1k_link_hash_table *htab;
   1371  1.1  christos   asection *s;
   1372  1.1  christos 
   1373  1.1  christos   /* This function may be called more than once.  */
   1374  1.1  christos   s = bfd_get_section_by_name (dynobj, ".got");
   1375  1.1  christos   if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
   1376  1.1  christos     return TRUE;
   1377  1.1  christos 
   1378  1.1  christos   htab = or1k_elf_hash_table (info);
   1379  1.1  christos   if (htab == NULL)
   1380  1.1  christos     return FALSE;
   1381  1.1  christos 
   1382  1.1  christos   if (! _bfd_elf_create_got_section (dynobj, info))
   1383  1.1  christos     return FALSE;
   1384  1.1  christos 
   1385  1.1  christos   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
   1386  1.1  christos   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
   1387  1.1  christos   htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
   1388  1.1  christos 
   1389  1.1  christos   if (! htab->sgot || ! htab->sgotplt || ! htab->srelgot)
   1390  1.1  christos     abort ();
   1391  1.1  christos 
   1392  1.1  christos   if (! bfd_set_section_flags (dynobj, htab->srelgot, SEC_ALLOC
   1393  1.1  christos                                | SEC_LOAD
   1394  1.1  christos                                | SEC_HAS_CONTENTS
   1395  1.1  christos                                | SEC_IN_MEMORY
   1396  1.1  christos                                | SEC_LINKER_CREATED
   1397  1.1  christos                                | SEC_READONLY)
   1398  1.1  christos       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
   1399  1.1  christos     return FALSE;
   1400  1.1  christos 
   1401  1.1  christos   return TRUE;
   1402  1.1  christos }
   1403  1.1  christos 
   1404  1.1  christos /* Look through the relocs for a section during the first phase.  */
   1405  1.1  christos 
   1406  1.1  christos static bfd_boolean
   1407  1.1  christos or1k_elf_check_relocs (bfd *abfd,
   1408  1.1  christos 		       struct bfd_link_info *info,
   1409  1.1  christos 		       asection *sec,
   1410  1.1  christos 		       const Elf_Internal_Rela *relocs)
   1411  1.1  christos {
   1412  1.1  christos   Elf_Internal_Shdr *symtab_hdr;
   1413  1.1  christos   struct elf_link_hash_entry **sym_hashes;
   1414  1.1  christos   const Elf_Internal_Rela *rel;
   1415  1.1  christos 
   1416  1.1  christos   const Elf_Internal_Rela *rel_end;
   1417  1.1  christos   struct elf_or1k_link_hash_table *htab;
   1418  1.1  christos   bfd *dynobj;
   1419  1.1  christos   asection *sreloc = NULL;
   1420  1.1  christos 
   1421  1.4  christos   if (bfd_link_relocatable (info))
   1422  1.1  christos     return TRUE;
   1423  1.1  christos 
   1424  1.1  christos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1425  1.1  christos   sym_hashes = elf_sym_hashes (abfd);
   1426  1.1  christos 
   1427  1.1  christos   htab = or1k_elf_hash_table (info);
   1428  1.1  christos   if (htab == NULL)
   1429  1.1  christos     return FALSE;
   1430  1.1  christos 
   1431  1.1  christos   dynobj = htab->root.dynobj;
   1432  1.1  christos 
   1433  1.1  christos   rel_end = relocs + sec->reloc_count;
   1434  1.1  christos   for (rel = relocs; rel < rel_end; rel++)
   1435  1.1  christos     {
   1436  1.1  christos       struct elf_link_hash_entry *h;
   1437  1.1  christos       unsigned long r_symndx;
   1438  1.1  christos       unsigned char tls_type;
   1439  1.1  christos 
   1440  1.1  christos       r_symndx = ELF32_R_SYM (rel->r_info);
   1441  1.1  christos       if (r_symndx < symtab_hdr->sh_info)
   1442  1.1  christos         h = NULL;
   1443  1.1  christos       else
   1444  1.1  christos         {
   1445  1.1  christos           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   1446  1.1  christos           while (h->root.type == bfd_link_hash_indirect
   1447  1.1  christos                  || h->root.type == bfd_link_hash_warning)
   1448  1.1  christos             h = (struct elf_link_hash_entry *) h->root.u.i.link;
   1449  1.1  christos 
   1450  1.1  christos           /* PR15323, ref flags aren't set for references in the same
   1451  1.1  christos              object.  */
   1452  1.1  christos           h->root.non_ir_ref = 1;
   1453  1.1  christos         }
   1454  1.1  christos 
   1455  1.1  christos       switch (ELF32_R_TYPE (rel->r_info))
   1456  1.1  christos         {
   1457  1.1  christos         case R_OR1K_TLS_GD_HI16:
   1458  1.1  christos         case R_OR1K_TLS_GD_LO16:
   1459  1.1  christos           tls_type = TLS_GD;
   1460  1.1  christos           break;
   1461  1.1  christos         case R_OR1K_TLS_LDM_HI16:
   1462  1.1  christos         case R_OR1K_TLS_LDM_LO16:
   1463  1.1  christos         case R_OR1K_TLS_LDO_HI16:
   1464  1.1  christos         case R_OR1K_TLS_LDO_LO16:
   1465  1.1  christos           tls_type = TLS_LD;
   1466  1.1  christos           break;
   1467  1.1  christos         case R_OR1K_TLS_IE_HI16:
   1468  1.1  christos         case R_OR1K_TLS_IE_LO16:
   1469  1.1  christos           tls_type = TLS_IE;
   1470  1.1  christos           break;
   1471  1.1  christos         case R_OR1K_TLS_LE_HI16:
   1472  1.1  christos         case R_OR1K_TLS_LE_LO16:
   1473  1.1  christos           tls_type = TLS_LE;
   1474  1.1  christos           break;
   1475  1.1  christos         default:
   1476  1.1  christos           tls_type = TLS_NONE;
   1477  1.1  christos         }
   1478  1.1  christos 
   1479  1.1  christos       /* Record TLS type.  */
   1480  1.1  christos       if (h != NULL)
   1481  1.1  christos           ((struct elf_or1k_link_hash_entry *) h)->tls_type = tls_type;
   1482  1.1  christos       else
   1483  1.1  christos         {
   1484  1.1  christos           unsigned char *local_tls_type;
   1485  1.1  christos 
   1486  1.1  christos           /* This is a TLS type record for a local symbol.  */
   1487  1.1  christos           local_tls_type = (unsigned char *) elf_or1k_local_tls_type (abfd);
   1488  1.1  christos           if (local_tls_type == NULL)
   1489  1.1  christos             {
   1490  1.1  christos               bfd_size_type size;
   1491  1.1  christos 
   1492  1.1  christos               size = symtab_hdr->sh_info;
   1493  1.1  christos               local_tls_type = bfd_zalloc (abfd, size);
   1494  1.1  christos               if (local_tls_type == NULL)
   1495  1.1  christos                 return FALSE;
   1496  1.1  christos               elf_or1k_local_tls_type (abfd) = local_tls_type;
   1497  1.1  christos             }
   1498  1.1  christos           local_tls_type[r_symndx] = tls_type;
   1499  1.1  christos         }
   1500  1.1  christos 
   1501  1.1  christos       switch (ELF32_R_TYPE (rel->r_info))
   1502  1.1  christos         {
   1503  1.1  christos           /* This relocation describes the C++ object vtable hierarchy.
   1504  1.1  christos              Reconstruct it for later use during GC.  */
   1505  1.1  christos         case R_OR1K_GNU_VTINHERIT:
   1506  1.1  christos           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   1507  1.1  christos             return FALSE;
   1508  1.1  christos           break;
   1509  1.1  christos 
   1510  1.1  christos           /* This relocation describes which C++ vtable entries are actually
   1511  1.1  christos              used.  Record for later use during GC.  */
   1512  1.1  christos         case R_OR1K_GNU_VTENTRY:
   1513  1.1  christos           BFD_ASSERT (h != NULL);
   1514  1.1  christos           if (h != NULL
   1515  1.1  christos               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   1516  1.1  christos             return FALSE;
   1517  1.1  christos           break;
   1518  1.1  christos 
   1519  1.1  christos           /* This relocation requires .plt entry.  */
   1520  1.1  christos         case R_OR1K_PLT26:
   1521  1.1  christos           if (h != NULL)
   1522  1.1  christos             {
   1523  1.1  christos               h->needs_plt = 1;
   1524  1.1  christos               h->plt.refcount += 1;
   1525  1.1  christos             }
   1526  1.1  christos           break;
   1527  1.1  christos 
   1528  1.1  christos         case R_OR1K_GOT16:
   1529  1.1  christos         case R_OR1K_GOTOFF_HI16:
   1530  1.1  christos         case R_OR1K_GOTOFF_LO16:
   1531  1.1  christos         case R_OR1K_TLS_GD_HI16:
   1532  1.1  christos         case R_OR1K_TLS_GD_LO16:
   1533  1.1  christos         case R_OR1K_TLS_IE_HI16:
   1534  1.1  christos         case R_OR1K_TLS_IE_LO16:
   1535  1.1  christos           if (htab->sgot == NULL)
   1536  1.1  christos             {
   1537  1.1  christos               if (dynobj == NULL)
   1538  1.1  christos                 htab->root.dynobj = dynobj = abfd;
   1539  1.1  christos               if (! create_got_section (dynobj, info))
   1540  1.1  christos                 return FALSE;
   1541  1.1  christos             }
   1542  1.1  christos 
   1543  1.1  christos           if (ELF32_R_TYPE (rel->r_info) != R_OR1K_GOTOFF_HI16 &&
   1544  1.1  christos               ELF32_R_TYPE (rel->r_info) != R_OR1K_GOTOFF_LO16)
   1545  1.1  christos             {
   1546  1.1  christos               if (h != NULL)
   1547  1.1  christos                 h->got.refcount += 1;
   1548  1.1  christos               else
   1549  1.1  christos                 {
   1550  1.1  christos                   bfd_signed_vma *local_got_refcounts;
   1551  1.1  christos 
   1552  1.1  christos                   /* This is a global offset table entry for a local symbol.  */
   1553  1.1  christos                   local_got_refcounts = elf_local_got_refcounts (abfd);
   1554  1.1  christos                   if (local_got_refcounts == NULL)
   1555  1.1  christos                     {
   1556  1.1  christos                       bfd_size_type size;
   1557  1.1  christos 
   1558  1.1  christos                       size = symtab_hdr->sh_info;
   1559  1.1  christos                       size *= sizeof (bfd_signed_vma);
   1560  1.1  christos                       local_got_refcounts = bfd_zalloc (abfd, size);
   1561  1.1  christos                       if (local_got_refcounts == NULL)
   1562  1.1  christos                         return FALSE;
   1563  1.1  christos                       elf_local_got_refcounts (abfd) = local_got_refcounts;
   1564  1.1  christos                     }
   1565  1.1  christos                   local_got_refcounts[r_symndx] += 1;
   1566  1.1  christos                 }
   1567  1.1  christos             }
   1568  1.1  christos           break;
   1569  1.1  christos 
   1570  1.1  christos         case R_OR1K_INSN_REL_26:
   1571  1.1  christos         case R_OR1K_HI_16_IN_INSN:
   1572  1.1  christos         case R_OR1K_LO_16_IN_INSN:
   1573  1.1  christos         case R_OR1K_32:
   1574  1.1  christos           /* R_OR1K_16? */
   1575  1.1  christos           {
   1576  1.4  christos             if (h != NULL && !bfd_link_pic (info))
   1577  1.1  christos               {
   1578  1.1  christos                 /* We may need a copy reloc.  */
   1579  1.1  christos                 h->non_got_ref = 1;
   1580  1.1  christos 
   1581  1.1  christos                 /* We may also need a .plt entry.  */
   1582  1.1  christos                 h->plt.refcount += 1;
   1583  1.1  christos                 if (ELF32_R_TYPE (rel->r_info) != R_OR1K_INSN_REL_26)
   1584  1.1  christos                   h->pointer_equality_needed = 1;
   1585  1.1  christos               }
   1586  1.1  christos 
   1587  1.1  christos             /* If we are creating a shared library, and this is a reloc
   1588  1.1  christos                against a global symbol, or a non PC relative reloc
   1589  1.1  christos                against a local symbol, then we need to copy the reloc
   1590  1.1  christos                into the shared library.  However, if we are linking with
   1591  1.1  christos                -Bsymbolic, we do not need to copy a reloc against a
   1592  1.1  christos                global symbol which is defined in an object we are
   1593  1.1  christos                including in the link (i.e., DEF_REGULAR is set).  At
   1594  1.1  christos                this point we have not seen all the input files, so it is
   1595  1.1  christos                possible that DEF_REGULAR is not set now but will be set
   1596  1.1  christos                later (it is never cleared).  In case of a weak definition,
   1597  1.1  christos                DEF_REGULAR may be cleared later by a strong definition in
   1598  1.1  christos                a shared library.  We account for that possibility below by
   1599  1.1  christos                storing information in the relocs_copied field of the hash
   1600  1.1  christos                table entry.  A similar situation occurs when creating
   1601  1.1  christos                shared libraries and symbol visibility changes render the
   1602  1.1  christos                symbol local.
   1603  1.1  christos 
   1604  1.1  christos                If on the other hand, we are creating an executable, we
   1605  1.1  christos                may need to keep relocations for symbols satisfied by a
   1606  1.1  christos                dynamic library if we manage to avoid copy relocs for the
   1607  1.1  christos                symbol.  */
   1608  1.1  christos 
   1609  1.4  christos             if ((bfd_link_pic (info)
   1610  1.1  christos                  && (sec->flags & SEC_ALLOC) != 0
   1611  1.1  christos                  && (ELF32_R_TYPE (rel->r_info) != R_OR1K_INSN_REL_26
   1612  1.1  christos                      || (h != NULL
   1613  1.1  christos                          && (!SYMBOLIC_BIND (info, h)
   1614  1.1  christos                              || h->root.type == bfd_link_hash_defweak
   1615  1.1  christos                              || !h->def_regular))))
   1616  1.4  christos                 || (!bfd_link_pic (info)
   1617  1.1  christos                     && (sec->flags & SEC_ALLOC) != 0
   1618  1.1  christos                     && h != NULL
   1619  1.1  christos                     && (h->root.type == bfd_link_hash_defweak
   1620  1.1  christos                         || !h->def_regular)))
   1621  1.1  christos               {
   1622  1.1  christos                 struct elf_or1k_dyn_relocs *p;
   1623  1.1  christos                 struct elf_or1k_dyn_relocs **head;
   1624  1.1  christos 
   1625  1.1  christos                 /* When creating a shared object, we must copy these
   1626  1.1  christos                    relocs into the output file.  We create a reloc
   1627  1.1  christos                    section in dynobj and make room for the reloc.  */
   1628  1.1  christos                 if (sreloc == NULL)
   1629  1.1  christos                   {
   1630  1.1  christos                     const char *name;
   1631  1.1  christos                     unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
   1632  1.1  christos                     unsigned int shnam = _bfd_elf_single_rel_hdr (sec)->sh_name;
   1633  1.1  christos 
   1634  1.1  christos                     name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
   1635  1.1  christos                     if (name == NULL)
   1636  1.1  christos                       return FALSE;
   1637  1.1  christos 
   1638  1.1  christos                     if (strncmp (name, ".rela", 5) != 0
   1639  1.1  christos                         || strcmp (bfd_get_section_name (abfd, sec),
   1640  1.1  christos                                    name + 5) != 0)
   1641  1.1  christos                       {
   1642  1.1  christos                         (*_bfd_error_handler)
   1643  1.1  christos                           (_("%B: bad relocation section name `%s\'"),
   1644  1.1  christos                            abfd, name);
   1645  1.1  christos                       }
   1646  1.1  christos 
   1647  1.1  christos                     if (htab->root.dynobj == NULL)
   1648  1.1  christos                       htab->root.dynobj = abfd;
   1649  1.1  christos                     dynobj = htab->root.dynobj;
   1650  1.1  christos 
   1651  1.1  christos                     sreloc = bfd_get_section_by_name (dynobj, name);
   1652  1.1  christos                     if (sreloc == NULL)
   1653  1.1  christos                       {
   1654  1.1  christos                         sreloc = _bfd_elf_make_dynamic_reloc_section
   1655  1.1  christos                           (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
   1656  1.1  christos 
   1657  1.1  christos                         if (sreloc == NULL)
   1658  1.1  christos                           return FALSE;
   1659  1.1  christos                       }
   1660  1.1  christos                     elf_section_data (sec)->sreloc = sreloc;
   1661  1.1  christos                   }
   1662  1.1  christos 
   1663  1.1  christos                 /* If this is a global symbol, we count the number of
   1664  1.1  christos                    relocations we need for this symbol.  */
   1665  1.1  christos                 if (h != NULL)
   1666  1.1  christos                   head = &((struct elf_or1k_link_hash_entry *) h)->dyn_relocs;
   1667  1.1  christos                 else
   1668  1.1  christos                   {
   1669  1.1  christos                     /* Track dynamic relocs needed for local syms too.
   1670  1.1  christos                        We really need local syms available to do this
   1671  1.1  christos                        easily.  Oh well.  */
   1672  1.1  christos 
   1673  1.1  christos                     asection *s;
   1674  1.1  christos                     Elf_Internal_Sym *isym;
   1675  1.1  christos                     void *vpp;
   1676  1.1  christos 
   1677  1.1  christos                     isym = bfd_sym_from_r_symndx (&htab->sym_sec,
   1678  1.1  christos                                                   abfd, r_symndx);
   1679  1.1  christos                     if (isym == NULL)
   1680  1.1  christos                       return FALSE;
   1681  1.1  christos 
   1682  1.1  christos                     s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   1683  1.1  christos                     if (s == NULL)
   1684  1.1  christos                       return FALSE;
   1685  1.1  christos 
   1686  1.1  christos                     vpp = &elf_section_data (s)->local_dynrel;
   1687  1.1  christos                     head = (struct elf_or1k_dyn_relocs **) vpp;
   1688  1.1  christos                   }
   1689  1.1  christos 
   1690  1.1  christos                 p = *head;
   1691  1.1  christos                 if (p == NULL || p->sec != sec)
   1692  1.1  christos                   {
   1693  1.1  christos                     bfd_size_type amt = sizeof *p;
   1694  1.1  christos                     p = ((struct elf_or1k_dyn_relocs *)
   1695  1.1  christos                          bfd_alloc (htab->root.dynobj, amt));
   1696  1.1  christos                     if (p == NULL)
   1697  1.1  christos                       return FALSE;
   1698  1.1  christos                     p->next = *head;
   1699  1.1  christos                     *head = p;
   1700  1.1  christos                     p->sec = sec;
   1701  1.1  christos                     p->count = 0;
   1702  1.1  christos                     p->pc_count = 0;
   1703  1.1  christos                   }
   1704  1.1  christos 
   1705  1.1  christos                 p->count += 1;
   1706  1.1  christos                 if (ELF32_R_TYPE (rel->r_info) == R_OR1K_INSN_REL_26)
   1707  1.1  christos                   p->pc_count += 1;
   1708  1.1  christos               }
   1709  1.1  christos           }
   1710  1.1  christos           break;
   1711  1.1  christos         }
   1712  1.1  christos     }
   1713  1.1  christos 
   1714  1.1  christos   return TRUE;
   1715  1.1  christos }
   1716  1.1  christos 
   1717  1.1  christos /* Finish up the dynamic sections.  */
   1718  1.1  christos 
   1719  1.1  christos static bfd_boolean
   1720  1.1  christos or1k_elf_finish_dynamic_sections (bfd *output_bfd,
   1721  1.1  christos                                   struct bfd_link_info *info)
   1722  1.1  christos {
   1723  1.1  christos   bfd *dynobj;
   1724  1.1  christos   asection *sdyn, *sgot;
   1725  1.1  christos   struct elf_or1k_link_hash_table *htab;
   1726  1.1  christos 
   1727  1.1  christos   htab = or1k_elf_hash_table (info);
   1728  1.1  christos   if (htab == NULL)
   1729  1.1  christos     return FALSE;
   1730  1.1  christos 
   1731  1.1  christos   dynobj = htab->root.dynobj;
   1732  1.1  christos 
   1733  1.1  christos   sgot = htab->sgotplt;
   1734  1.1  christos   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
   1735  1.1  christos 
   1736  1.1  christos   if (htab->root.dynamic_sections_created)
   1737  1.1  christos     {
   1738  1.1  christos       asection *splt;
   1739  1.1  christos       Elf32_External_Dyn *dyncon, *dynconend;
   1740  1.1  christos 
   1741  1.1  christos       BFD_ASSERT (sgot != NULL && sdyn != NULL);
   1742  1.1  christos 
   1743  1.1  christos       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   1744  1.1  christos       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   1745  1.1  christos 
   1746  1.1  christos       for (; dyncon < dynconend; dyncon++)
   1747  1.1  christos         {
   1748  1.1  christos           Elf_Internal_Dyn dyn;
   1749  1.1  christos           asection *s;
   1750  1.1  christos 
   1751  1.1  christos           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   1752  1.1  christos 
   1753  1.1  christos           switch (dyn.d_tag)
   1754  1.1  christos             {
   1755  1.1  christos             default:
   1756  1.1  christos               continue;
   1757  1.1  christos 
   1758  1.1  christos             case DT_PLTGOT:
   1759  1.4  christos               s = htab->sgotplt;
   1760  1.4  christos               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   1761  1.1  christos               break;
   1762  1.1  christos 
   1763  1.1  christos             case DT_JMPREL:
   1764  1.4  christos               s = htab->srelplt;
   1765  1.4  christos               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   1766  1.1  christos               break;
   1767  1.1  christos 
   1768  1.1  christos             case DT_PLTRELSZ:
   1769  1.4  christos               s = htab->srelplt;
   1770  1.1  christos               dyn.d_un.d_val = s->size;
   1771  1.1  christos               break;
   1772  1.1  christos 
   1773  1.1  christos             case DT_RELASZ:
   1774  1.1  christos               /* My reading of the SVR4 ABI indicates that the
   1775  1.1  christos                  procedure linkage table relocs (DT_JMPREL) should be
   1776  1.1  christos                  included in the overall relocs (DT_RELA).  This is
   1777  1.1  christos                  what Solaris does.  However, UnixWare can not handle
   1778  1.1  christos                  that case.  Therefore, we override the DT_RELASZ entry
   1779  1.1  christos                  here to make it not include the JMPREL relocs.  Since
   1780  1.1  christos                  the linker script arranges for .rela.plt to follow all
   1781  1.1  christos                  other relocation sections, we don't have to worry
   1782  1.1  christos                  about changing the DT_RELA entry.  */
   1783  1.1  christos               if (htab->srelplt != NULL)
   1784  1.1  christos                 {
   1785  1.4  christos                   s = htab->srelplt;
   1786  1.1  christos                   dyn.d_un.d_val -= s->size;
   1787  1.1  christos                 }
   1788  1.1  christos               break;
   1789  1.1  christos             }
   1790  1.1  christos           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   1791  1.1  christos         }
   1792  1.1  christos 
   1793  1.1  christos 
   1794  1.1  christos       /* Fill in the first entry in the procedure linkage table.  */
   1795  1.1  christos       splt = htab->splt;
   1796  1.1  christos       if (splt && splt->size > 0)
   1797  1.1  christos         {
   1798  1.4  christos           if (bfd_link_pic (info))
   1799  1.1  christos             {
   1800  1.1  christos               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0,
   1801  1.1  christos                           splt->contents);
   1802  1.1  christos               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1,
   1803  1.1  christos                           splt->contents + 4);
   1804  1.1  christos               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2,
   1805  1.1  christos                           splt->contents + 8);
   1806  1.1  christos               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3,
   1807  1.1  christos                           splt->contents + 12);
   1808  1.1  christos               bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4,
   1809  1.1  christos                           splt->contents + 16);
   1810  1.1  christos             }
   1811  1.1  christos           else
   1812  1.1  christos             {
   1813  1.1  christos               unsigned long addr;
   1814  1.1  christos               /* addr = .got + 4 */
   1815  1.1  christos               addr = sgot->output_section->vma + sgot->output_offset + 4;
   1816  1.1  christos               bfd_put_32 (output_bfd,
   1817  1.1  christos                           PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
   1818  1.1  christos                           splt->contents);
   1819  1.1  christos               bfd_put_32 (output_bfd,
   1820  1.1  christos                           PLT0_ENTRY_WORD1 | (addr & 0xffff),
   1821  1.1  christos                           splt->contents + 4);
   1822  1.1  christos               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
   1823  1.1  christos               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
   1824  1.1  christos               bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
   1825  1.1  christos             }
   1826  1.1  christos 
   1827  1.1  christos           elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
   1828  1.1  christos         }
   1829  1.1  christos     }
   1830  1.1  christos 
   1831  1.1  christos   /* Set the first entry in the global offset table to the address of
   1832  1.1  christos      the dynamic section.  */
   1833  1.1  christos   if (sgot && sgot->size > 0)
   1834  1.1  christos     {
   1835  1.1  christos       if (sdyn == NULL)
   1836  1.1  christos         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
   1837  1.1  christos       else
   1838  1.1  christos         bfd_put_32 (output_bfd,
   1839  1.1  christos                     sdyn->output_section->vma + sdyn->output_offset,
   1840  1.1  christos                     sgot->contents);
   1841  1.1  christos       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
   1842  1.1  christos     }
   1843  1.1  christos 
   1844  1.1  christos   if (htab->sgot && htab->sgot->size > 0)
   1845  1.1  christos     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
   1846  1.1  christos 
   1847  1.1  christos   return TRUE;
   1848  1.1  christos }
   1849  1.1  christos 
   1850  1.1  christos /* Finish up dynamic symbol handling.  We set the contents of various
   1851  1.1  christos    dynamic sections here.  */
   1852  1.1  christos 
   1853  1.1  christos static bfd_boolean
   1854  1.1  christos or1k_elf_finish_dynamic_symbol (bfd *output_bfd,
   1855  1.1  christos                                 struct bfd_link_info *info,
   1856  1.1  christos                                 struct elf_link_hash_entry *h,
   1857  1.1  christos                                 Elf_Internal_Sym *sym)
   1858  1.1  christos {
   1859  1.1  christos   struct elf_or1k_link_hash_table *htab;
   1860  1.1  christos   bfd_byte *loc;
   1861  1.1  christos 
   1862  1.1  christos   htab = or1k_elf_hash_table (info);
   1863  1.1  christos   if (htab == NULL)
   1864  1.1  christos     return FALSE;
   1865  1.1  christos 
   1866  1.1  christos   if (h->plt.offset != (bfd_vma) -1)
   1867  1.1  christos     {
   1868  1.1  christos       asection *splt;
   1869  1.1  christos       asection *sgot;
   1870  1.1  christos       asection *srela;
   1871  1.1  christos 
   1872  1.1  christos       bfd_vma plt_index;
   1873  1.1  christos       bfd_vma got_offset;
   1874  1.1  christos       bfd_vma got_addr;
   1875  1.1  christos       Elf_Internal_Rela rela;
   1876  1.1  christos 
   1877  1.1  christos       /* This symbol has an entry in the procedure linkage table.  Set
   1878  1.1  christos          it up.  */
   1879  1.1  christos       BFD_ASSERT (h->dynindx != -1);
   1880  1.1  christos 
   1881  1.1  christos       splt = htab->splt;
   1882  1.1  christos       sgot = htab->sgotplt;
   1883  1.1  christos       srela = htab->srelplt;
   1884  1.1  christos       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
   1885  1.1  christos 
   1886  1.1  christos       /* Get the index in the procedure linkage table which
   1887  1.1  christos          corresponds to this symbol.  This is the index of this symbol
   1888  1.1  christos          in all the symbols for which we are making plt entries.  The
   1889  1.1  christos          first entry in the procedure linkage table is reserved.  */
   1890  1.1  christos       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
   1891  1.1  christos 
   1892  1.1  christos       /* Get the offset into the .got table of the entry that
   1893  1.1  christos         corresponds to this function.  Each .got entry is 4 bytes.
   1894  1.1  christos         The first three are reserved.  */
   1895  1.1  christos       got_offset = (plt_index + 3) * 4;
   1896  1.1  christos       got_addr = got_offset;
   1897  1.1  christos 
   1898  1.1  christos       /* Fill in the entry in the procedure linkage table.  */
   1899  1.4  christos       if (! bfd_link_pic (info))
   1900  1.1  christos         {
   1901  1.1  christos           got_addr += htab->sgotplt->output_section->vma
   1902  1.1  christos             + htab->sgotplt->output_offset;
   1903  1.1  christos           bfd_put_32 (output_bfd, PLT_ENTRY_WORD0 | ((got_addr >> 16) & 0xffff),
   1904  1.1  christos                       splt->contents + h->plt.offset);
   1905  1.1  christos           bfd_put_32 (output_bfd, PLT_ENTRY_WORD1 | (got_addr & 0xffff),
   1906  1.1  christos                       splt->contents + h->plt.offset + 4);
   1907  1.1  christos           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD2,
   1908  1.1  christos                       splt->contents + h->plt.offset + 8);
   1909  1.1  christos           bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD3,
   1910  1.1  christos                       splt->contents + h->plt.offset + 12);
   1911  1.1  christos           bfd_put_32 (output_bfd, PLT_ENTRY_WORD4
   1912  1.1  christos                       | plt_index * sizeof (Elf32_External_Rela),
   1913  1.1  christos                       splt->contents + h->plt.offset + 16);
   1914  1.1  christos         }
   1915  1.1  christos       else
   1916  1.1  christos         {
   1917  1.1  christos           bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD0 | (got_addr & 0xffff),
   1918  1.1  christos                       splt->contents + h->plt.offset);
   1919  1.1  christos           bfd_put_32 (output_bfd, PLT_PIC_ENTRY_WORD1
   1920  1.1  christos                       | plt_index * sizeof (Elf32_External_Rela),
   1921  1.1  christos                       splt->contents + h->plt.offset + 4);
   1922  1.1  christos           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD2,
   1923  1.1  christos                       splt->contents + h->plt.offset + 8);
   1924  1.1  christos           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD3,
   1925  1.1  christos                       splt->contents + h->plt.offset + 12);
   1926  1.1  christos           bfd_put_32 (output_bfd, (bfd_vma) PLT_PIC_ENTRY_WORD4,
   1927  1.1  christos                       splt->contents + h->plt.offset + 16);
   1928  1.1  christos         }
   1929  1.1  christos 
   1930  1.1  christos       /* Fill in the entry in the global offset table.  */
   1931  1.1  christos       bfd_put_32 (output_bfd,
   1932  1.1  christos                   (splt->output_section->vma
   1933  1.1  christos                    + splt->output_offset), /* Same offset.  */
   1934  1.1  christos                   sgot->contents + got_offset);
   1935  1.1  christos 
   1936  1.1  christos       /* Fill in the entry in the .rela.plt section.  */
   1937  1.1  christos       rela.r_offset = (sgot->output_section->vma
   1938  1.1  christos                        + sgot->output_offset
   1939  1.1  christos                        + got_offset);
   1940  1.1  christos       rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_JMP_SLOT);
   1941  1.1  christos       rela.r_addend = 0;
   1942  1.1  christos       loc = srela->contents;
   1943  1.1  christos       loc += plt_index * sizeof (Elf32_External_Rela);
   1944  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   1945  1.1  christos 
   1946  1.1  christos       if (!h->def_regular)
   1947  1.1  christos         {
   1948  1.1  christos           /* Mark the symbol as undefined, rather than as defined in
   1949  1.1  christos              the .plt section.  Leave the value alone.  */
   1950  1.1  christos           sym->st_shndx = SHN_UNDEF;
   1951  1.1  christos         }
   1952  1.1  christos 
   1953  1.1  christos     }
   1954  1.1  christos 
   1955  1.1  christos   if (h->got.offset != (bfd_vma) -1
   1956  1.1  christos       && (h->got.offset & 2) == 0) /* Homemade TLS check.  */
   1957  1.1  christos     {
   1958  1.1  christos       asection *sgot;
   1959  1.1  christos       asection *srela;
   1960  1.1  christos       Elf_Internal_Rela rela;
   1961  1.1  christos 
   1962  1.1  christos       /* This symbol has an entry in the global offset table.  Set it
   1963  1.1  christos          up.  */
   1964  1.1  christos       sgot = htab->sgot;
   1965  1.1  christos       srela = htab->srelgot;
   1966  1.1  christos       BFD_ASSERT (sgot != NULL && srela != NULL);
   1967  1.1  christos 
   1968  1.1  christos       rela.r_offset = (sgot->output_section->vma
   1969  1.1  christos                        + sgot->output_offset
   1970  1.1  christos                        + (h->got.offset &~ 1));
   1971  1.1  christos 
   1972  1.1  christos       /* If this is a -Bsymbolic link, and the symbol is defined
   1973  1.1  christos          locally, we just want to emit a RELATIVE reloc.  Likewise if
   1974  1.1  christos          the symbol was forced to be local because of a version file.
   1975  1.1  christos          The entry in the global offset table will already have been
   1976  1.1  christos          initialized in the relocate_section function.  */
   1977  1.4  christos       if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
   1978  1.1  christos         {
   1979  1.1  christos           rela.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
   1980  1.1  christos           rela.r_addend = (h->root.u.def.value
   1981  1.1  christos                            + h->root.u.def.section->output_section->vma
   1982  1.1  christos                            + h->root.u.def.section->output_offset);
   1983  1.1  christos         }
   1984  1.1  christos       else
   1985  1.1  christos         {
   1986  1.1  christos           BFD_ASSERT ((h->got.offset & 1) == 0);
   1987  1.1  christos           bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
   1988  1.1  christos           rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_GLOB_DAT);
   1989  1.1  christos           rela.r_addend = 0;
   1990  1.1  christos         }
   1991  1.1  christos 
   1992  1.1  christos       loc = srela->contents;
   1993  1.1  christos       loc += srela->reloc_count * sizeof (Elf32_External_Rela);
   1994  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   1995  1.1  christos       ++srela->reloc_count;
   1996  1.1  christos     }
   1997  1.1  christos 
   1998  1.1  christos   if (h->needs_copy)
   1999  1.1  christos     {
   2000  1.1  christos       asection *s;
   2001  1.1  christos       Elf_Internal_Rela rela;
   2002  1.1  christos 
   2003  1.1  christos       /* This symbols needs a copy reloc.  Set it up.  */
   2004  1.1  christos       BFD_ASSERT (h->dynindx != -1
   2005  1.1  christos                   && (h->root.type == bfd_link_hash_defined
   2006  1.1  christos                       || h->root.type == bfd_link_hash_defweak));
   2007  1.1  christos 
   2008  1.1  christos       s = bfd_get_section_by_name (h->root.u.def.section->owner,
   2009  1.1  christos                                    ".rela.bss");
   2010  1.1  christos       BFD_ASSERT (s != NULL);
   2011  1.1  christos 
   2012  1.1  christos       rela.r_offset = (h->root.u.def.value
   2013  1.1  christos                        + h->root.u.def.section->output_section->vma
   2014  1.1  christos                        + h->root.u.def.section->output_offset);
   2015  1.1  christos       rela.r_info = ELF32_R_INFO (h->dynindx, R_OR1K_COPY);
   2016  1.1  christos       rela.r_addend = 0;
   2017  1.1  christos       loc = s->contents;
   2018  1.1  christos       loc += s->reloc_count * sizeof (Elf32_External_Rela);
   2019  1.1  christos       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   2020  1.1  christos       ++s->reloc_count;
   2021  1.1  christos     }
   2022  1.1  christos 
   2023  1.1  christos   /* Mark some specially defined symbols as absolute.  */
   2024  1.1  christos   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
   2025  1.1  christos       || h == htab->root.hgot)
   2026  1.1  christos     sym->st_shndx = SHN_ABS;
   2027  1.1  christos 
   2028  1.1  christos   return TRUE;
   2029  1.1  christos }
   2030  1.1  christos 
   2031  1.1  christos static enum elf_reloc_type_class
   2032  1.1  christos or1k_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2033  1.1  christos                            const asection *rel_sec ATTRIBUTE_UNUSED,
   2034  1.1  christos                            const Elf_Internal_Rela *rela)
   2035  1.1  christos {
   2036  1.1  christos   switch ((int) ELF32_R_TYPE (rela->r_info))
   2037  1.1  christos     {
   2038  1.1  christos     case R_OR1K_RELATIVE:  return reloc_class_relative;
   2039  1.1  christos     case R_OR1K_JMP_SLOT:  return reloc_class_plt;
   2040  1.1  christos     case R_OR1K_COPY:      return reloc_class_copy;
   2041  1.1  christos     default:               return reloc_class_normal;
   2042  1.1  christos     }
   2043  1.1  christos }
   2044  1.1  christos 
   2045  1.1  christos /* Adjust a symbol defined by a dynamic object and referenced by a
   2046  1.1  christos    regular object.  The current definition is in some section of the
   2047  1.1  christos    dynamic object, but we're not including those sections.  We have to
   2048  1.1  christos    change the definition to something the rest of the link can
   2049  1.1  christos    understand.  */
   2050  1.1  christos 
   2051  1.1  christos static bfd_boolean
   2052  1.1  christos or1k_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   2053  1.1  christos                                 struct elf_link_hash_entry *h)
   2054  1.1  christos {
   2055  1.1  christos   struct elf_or1k_link_hash_table *htab;
   2056  1.1  christos   struct elf_or1k_link_hash_entry *eh;
   2057  1.1  christos   struct elf_or1k_dyn_relocs *p;
   2058  1.1  christos   bfd *dynobj;
   2059  1.1  christos   asection *s;
   2060  1.1  christos 
   2061  1.1  christos   dynobj = elf_hash_table (info)->dynobj;
   2062  1.1  christos 
   2063  1.1  christos   /* Make sure we know what is going on here.  */
   2064  1.1  christos   BFD_ASSERT (dynobj != NULL
   2065  1.1  christos               && (h->needs_plt
   2066  1.1  christos                   || h->u.weakdef != NULL
   2067  1.1  christos                   || (h->def_dynamic
   2068  1.1  christos                       && h->ref_regular
   2069  1.1  christos                       && !h->def_regular)));
   2070  1.1  christos 
   2071  1.1  christos   /* If this is a function, put it in the procedure linkage table.  We
   2072  1.1  christos      will fill in the contents of the procedure linkage table later,
   2073  1.1  christos      when we know the address of the .got section.  */
   2074  1.1  christos   if (h->type == STT_FUNC
   2075  1.1  christos       || h->needs_plt)
   2076  1.1  christos     {
   2077  1.4  christos       if (! bfd_link_pic (info)
   2078  1.1  christos           && !h->def_dynamic
   2079  1.1  christos           && !h->ref_dynamic
   2080  1.1  christos           && h->root.type != bfd_link_hash_undefweak
   2081  1.1  christos           && h->root.type != bfd_link_hash_undefined)
   2082  1.1  christos         {
   2083  1.1  christos           /* This case can occur if we saw a PLT reloc in an input
   2084  1.1  christos              file, but the symbol was never referred to by a dynamic
   2085  1.1  christos              object.  In such a case, we don't actually need to build
   2086  1.1  christos              a procedure linkage table, and we can just do a PCREL
   2087  1.1  christos              reloc instead.  */
   2088  1.1  christos           h->plt.offset = (bfd_vma) -1;
   2089  1.1  christos           h->needs_plt = 0;
   2090  1.1  christos         }
   2091  1.1  christos 
   2092  1.1  christos       return TRUE;
   2093  1.1  christos     }
   2094  1.1  christos   else
   2095  1.1  christos     h->plt.offset = (bfd_vma) -1;
   2096  1.1  christos 
   2097  1.1  christos   /* If this is a weak symbol, and there is a real definition, the
   2098  1.1  christos      processor independent code will have arranged for us to see the
   2099  1.1  christos      real definition first, and we can just use the same value.  */
   2100  1.1  christos   if (h->u.weakdef != NULL)
   2101  1.1  christos     {
   2102  1.1  christos       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   2103  1.1  christos                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
   2104  1.1  christos       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   2105  1.1  christos       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   2106  1.1  christos       return TRUE;
   2107  1.1  christos     }
   2108  1.1  christos 
   2109  1.1  christos   /* This is a reference to a symbol defined by a dynamic object which
   2110  1.1  christos      is not a function.  */
   2111  1.1  christos 
   2112  1.1  christos   /* If we are creating a shared library, we must presume that the
   2113  1.1  christos      only references to the symbol are via the global offset table.
   2114  1.1  christos      For such cases we need not do anything here; the relocations will
   2115  1.1  christos      be handled correctly by relocate_section.  */
   2116  1.4  christos   if (bfd_link_pic (info))
   2117  1.1  christos     return TRUE;
   2118  1.1  christos 
   2119  1.1  christos   /* If there are no references to this symbol that do not use the
   2120  1.1  christos      GOT, we don't need to generate a copy reloc.  */
   2121  1.1  christos   if (!h->non_got_ref)
   2122  1.1  christos     return TRUE;
   2123  1.1  christos 
   2124  1.1  christos   /* If -z nocopyreloc was given, we won't generate them either.  */
   2125  1.1  christos   if (info->nocopyreloc)
   2126  1.1  christos     {
   2127  1.1  christos       h->non_got_ref = 0;
   2128  1.1  christos       return TRUE;
   2129  1.1  christos     }
   2130  1.1  christos 
   2131  1.1  christos   eh = (struct elf_or1k_link_hash_entry *) h;
   2132  1.1  christos   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   2133  1.1  christos     {
   2134  1.1  christos       s = p->sec->output_section;
   2135  1.1  christos       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
   2136  1.1  christos         break;
   2137  1.1  christos     }
   2138  1.1  christos 
   2139  1.1  christos   /* If we didn't find any dynamic relocs in sections which needs the
   2140  1.1  christos      copy reloc, then we'll be keeping the dynamic relocs and avoiding
   2141  1.1  christos      the copy reloc.  */
   2142  1.1  christos   if (p == NULL)
   2143  1.1  christos     {
   2144  1.1  christos       h->non_got_ref = 0;
   2145  1.1  christos       return TRUE;
   2146  1.1  christos     }
   2147  1.1  christos 
   2148  1.1  christos   /* We must allocate the symbol in our .dynbss section, which will
   2149  1.1  christos      become part of the .bss section of the executable.  There will be
   2150  1.1  christos      an entry for this symbol in the .dynsym section.  The dynamic
   2151  1.1  christos      object will contain position independent code, so all references
   2152  1.1  christos      from the dynamic object to this symbol will go through the global
   2153  1.1  christos      offset table.  The dynamic linker will use the .dynsym entry to
   2154  1.1  christos      determine the address it must put in the global offset table, so
   2155  1.1  christos      both the dynamic object and the regular object will refer to the
   2156  1.1  christos      same memory location for the variable.  */
   2157  1.1  christos 
   2158  1.1  christos   htab = or1k_elf_hash_table (info);
   2159  1.1  christos   if (htab == NULL)
   2160  1.1  christos     return FALSE;
   2161  1.1  christos 
   2162  1.1  christos   s = htab->sdynbss;
   2163  1.1  christos   BFD_ASSERT (s != NULL);
   2164  1.1  christos 
   2165  1.1  christos   /* We must generate a R_OR1K_COPY reloc to tell the dynamic linker
   2166  1.1  christos      to copy the initial value out of the dynamic object and into the
   2167  1.1  christos      runtime process image.  We need to remember the offset into the
   2168  1.1  christos      .rela.bss section we are going to use.  */
   2169  1.1  christos   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   2170  1.1  christos     {
   2171  1.1  christos       asection *srel;
   2172  1.1  christos 
   2173  1.1  christos       srel = htab->srelbss;
   2174  1.1  christos       BFD_ASSERT (srel != NULL);
   2175  1.1  christos       srel->size += sizeof (Elf32_External_Rela);
   2176  1.1  christos       h->needs_copy = 1;
   2177  1.1  christos     }
   2178  1.1  christos 
   2179  1.1  christos   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   2180  1.1  christos }
   2181  1.1  christos 
   2182  1.1  christos /* Allocate space in .plt, .got and associated reloc sections for
   2183  1.1  christos    dynamic relocs.  */
   2184  1.1  christos 
   2185  1.1  christos static bfd_boolean
   2186  1.1  christos allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
   2187  1.1  christos {
   2188  1.1  christos   struct bfd_link_info *info;
   2189  1.1  christos   struct elf_or1k_link_hash_table *htab;
   2190  1.1  christos   struct elf_or1k_link_hash_entry *eh;
   2191  1.1  christos   struct elf_or1k_dyn_relocs *p;
   2192  1.1  christos 
   2193  1.1  christos   if (h->root.type == bfd_link_hash_indirect)
   2194  1.1  christos     return TRUE;
   2195  1.1  christos 
   2196  1.1  christos   info = (struct bfd_link_info *) inf;
   2197  1.1  christos   htab = or1k_elf_hash_table (info);
   2198  1.1  christos   if (htab == NULL)
   2199  1.1  christos     return FALSE;
   2200  1.1  christos 
   2201  1.1  christos   eh = (struct elf_or1k_link_hash_entry *) h;
   2202  1.1  christos 
   2203  1.1  christos   if (htab->root.dynamic_sections_created
   2204  1.1  christos       && h->plt.refcount > 0)
   2205  1.1  christos     {
   2206  1.1  christos       /* Make sure this symbol is output as a dynamic symbol.
   2207  1.1  christos          Undefined weak syms won't yet be marked as dynamic.  */
   2208  1.1  christos       if (h->dynindx == -1
   2209  1.1  christos           && !h->forced_local)
   2210  1.1  christos         {
   2211  1.1  christos           if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2212  1.1  christos             return FALSE;
   2213  1.1  christos         }
   2214  1.1  christos 
   2215  1.4  christos       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
   2216  1.1  christos         {
   2217  1.1  christos           asection *s = htab->splt;
   2218  1.1  christos 
   2219  1.1  christos           /* If this is the first .plt entry, make room for the special
   2220  1.1  christos              first entry.  */
   2221  1.1  christos           if (s->size == 0)
   2222  1.1  christos             s->size = PLT_ENTRY_SIZE;
   2223  1.1  christos 
   2224  1.1  christos           h->plt.offset = s->size;
   2225  1.1  christos 
   2226  1.1  christos           /* If this symbol is not defined in a regular file, and we are
   2227  1.1  christos              not generating a shared library, then set the symbol to this
   2228  1.1  christos              location in the .plt.  This is required to make function
   2229  1.1  christos              pointers compare as equal between the normal executable and
   2230  1.1  christos              the shared library.  */
   2231  1.4  christos           if (! bfd_link_pic (info)
   2232  1.1  christos               && !h->def_regular)
   2233  1.1  christos             {
   2234  1.1  christos               h->root.u.def.section = s;
   2235  1.1  christos               h->root.u.def.value = h->plt.offset;
   2236  1.1  christos             }
   2237  1.1  christos 
   2238  1.1  christos           /* Make room for this entry.  */
   2239  1.1  christos           s->size += PLT_ENTRY_SIZE;
   2240  1.1  christos 
   2241  1.1  christos           /* We also need to make an entry in the .got.plt section, which
   2242  1.1  christos              will be placed in the .got section by the linker script.  */
   2243  1.1  christos           htab->sgotplt->size += 4;
   2244  1.1  christos 
   2245  1.1  christos           /* We also need to make an entry in the .rel.plt section.  */
   2246  1.1  christos           htab->srelplt->size += sizeof (Elf32_External_Rela);
   2247  1.1  christos         }
   2248  1.1  christos       else
   2249  1.1  christos         {
   2250  1.1  christos           h->plt.offset = (bfd_vma) -1;
   2251  1.1  christos           h->needs_plt = 0;
   2252  1.1  christos         }
   2253  1.1  christos     }
   2254  1.1  christos   else
   2255  1.1  christos     {
   2256  1.1  christos       h->plt.offset = (bfd_vma) -1;
   2257  1.1  christos       h->needs_plt = 0;
   2258  1.1  christos     }
   2259  1.1  christos 
   2260  1.1  christos   if (h->got.refcount > 0)
   2261  1.1  christos     {
   2262  1.1  christos       asection *s;
   2263  1.1  christos       bfd_boolean dyn;
   2264  1.1  christos       unsigned char tls_type;
   2265  1.1  christos 
   2266  1.1  christos       /* Make sure this symbol is output as a dynamic symbol.
   2267  1.1  christos          Undefined weak syms won't yet be marked as dynamic.  */
   2268  1.1  christos       if (h->dynindx == -1
   2269  1.1  christos           && !h->forced_local)
   2270  1.1  christos         {
   2271  1.1  christos           if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2272  1.1  christos             return FALSE;
   2273  1.1  christos         }
   2274  1.1  christos 
   2275  1.1  christos       s = htab->sgot;
   2276  1.1  christos 
   2277  1.1  christos       h->got.offset = s->size;
   2278  1.1  christos 
   2279  1.1  christos       tls_type = ((struct elf_or1k_link_hash_entry *) h)->tls_type;
   2280  1.1  christos 
   2281  1.1  christos       /* TLS GD requires two GOT and two relocs.  */
   2282  1.1  christos       if (tls_type == TLS_GD)
   2283  1.1  christos         s->size += 8;
   2284  1.1  christos       else
   2285  1.1  christos         s->size += 4;
   2286  1.1  christos       dyn = htab->root.dynamic_sections_created;
   2287  1.4  christos       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
   2288  1.1  christos         {
   2289  1.1  christos           if (tls_type == TLS_GD)
   2290  1.1  christos             htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
   2291  1.1  christos           else
   2292  1.1  christos             htab->srelgot->size += sizeof (Elf32_External_Rela);
   2293  1.1  christos         }
   2294  1.1  christos     }
   2295  1.1  christos   else
   2296  1.1  christos     h->got.offset = (bfd_vma) -1;
   2297  1.1  christos 
   2298  1.1  christos   if (eh->dyn_relocs == NULL)
   2299  1.1  christos     return TRUE;
   2300  1.1  christos 
   2301  1.1  christos   /* In the shared -Bsymbolic case, discard space allocated for
   2302  1.1  christos      dynamic pc-relative relocs against symbols which turn out to be
   2303  1.1  christos      defined in regular objects.  For the normal shared case, discard
   2304  1.1  christos      space for pc-relative relocs that have become local due to symbol
   2305  1.1  christos      visibility changes.  */
   2306  1.1  christos 
   2307  1.4  christos   if (bfd_link_pic (info))
   2308  1.1  christos     {
   2309  1.1  christos       if (SYMBOL_CALLS_LOCAL (info, h))
   2310  1.1  christos         {
   2311  1.1  christos           struct elf_or1k_dyn_relocs **pp;
   2312  1.1  christos 
   2313  1.1  christos           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
   2314  1.1  christos             {
   2315  1.1  christos               p->count -= p->pc_count;
   2316  1.1  christos               p->pc_count = 0;
   2317  1.1  christos               if (p->count == 0)
   2318  1.1  christos                 *pp = p->next;
   2319  1.1  christos               else
   2320  1.1  christos                 pp = &p->next;
   2321  1.1  christos             }
   2322  1.1  christos         }
   2323  1.1  christos 
   2324  1.1  christos       /* Also discard relocs on undefined weak syms with non-default
   2325  1.1  christos          visibility.  */
   2326  1.1  christos       if (eh->dyn_relocs != NULL
   2327  1.1  christos           && h->root.type == bfd_link_hash_undefweak)
   2328  1.1  christos         {
   2329  1.1  christos           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   2330  1.1  christos             eh->dyn_relocs = NULL;
   2331  1.1  christos 
   2332  1.1  christos           /* Make sure undefined weak symbols are output as a dynamic
   2333  1.1  christos              symbol in PIEs.  */
   2334  1.1  christos           else if (h->dynindx == -1
   2335  1.1  christos                    && !h->forced_local)
   2336  1.1  christos             {
   2337  1.1  christos               if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2338  1.1  christos                 return FALSE;
   2339  1.1  christos             }
   2340  1.1  christos         }
   2341  1.1  christos     }
   2342  1.1  christos   else
   2343  1.1  christos     {
   2344  1.1  christos       /* For the non-shared case, discard space for relocs against
   2345  1.1  christos          symbols which turn out to need copy relocs or are not
   2346  1.1  christos          dynamic.  */
   2347  1.1  christos 
   2348  1.1  christos       if (!h->non_got_ref
   2349  1.1  christos           && ((h->def_dynamic
   2350  1.1  christos                && !h->def_regular)
   2351  1.1  christos               || (htab->root.dynamic_sections_created
   2352  1.1  christos                   && (h->root.type == bfd_link_hash_undefweak
   2353  1.1  christos                       || h->root.type == bfd_link_hash_undefined))))
   2354  1.1  christos         {
   2355  1.1  christos           /* Make sure this symbol is output as a dynamic symbol.
   2356  1.1  christos              Undefined weak syms won't yet be marked as dynamic.  */
   2357  1.1  christos           if (h->dynindx == -1
   2358  1.1  christos               && !h->forced_local)
   2359  1.1  christos             {
   2360  1.1  christos               if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2361  1.1  christos                 return FALSE;
   2362  1.1  christos             }
   2363  1.1  christos 
   2364  1.1  christos           /* If that succeeded, we know we'll be keeping all the
   2365  1.1  christos              relocs.  */
   2366  1.1  christos           if (h->dynindx != -1)
   2367  1.1  christos             goto keep;
   2368  1.1  christos         }
   2369  1.1  christos 
   2370  1.1  christos       eh->dyn_relocs = NULL;
   2371  1.1  christos 
   2372  1.1  christos     keep: ;
   2373  1.1  christos     }
   2374  1.1  christos 
   2375  1.1  christos   /* Finally, allocate space.  */
   2376  1.1  christos   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   2377  1.1  christos     {
   2378  1.1  christos       asection *sreloc = elf_section_data (p->sec)->sreloc;
   2379  1.1  christos       sreloc->size += p->count * sizeof (Elf32_External_Rela);
   2380  1.1  christos     }
   2381  1.1  christos 
   2382  1.1  christos   return TRUE;
   2383  1.1  christos }
   2384  1.1  christos 
   2385  1.1  christos /* Find any dynamic relocs that apply to read-only sections.  */
   2386  1.1  christos 
   2387  1.1  christos static bfd_boolean
   2388  1.1  christos readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
   2389  1.1  christos {
   2390  1.1  christos   struct elf_or1k_link_hash_entry *eh;
   2391  1.1  christos   struct elf_or1k_dyn_relocs *p;
   2392  1.1  christos 
   2393  1.1  christos   eh = (struct elf_or1k_link_hash_entry *) h;
   2394  1.1  christos   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   2395  1.1  christos     {
   2396  1.1  christos       asection *s = p->sec->output_section;
   2397  1.1  christos 
   2398  1.1  christos       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   2399  1.1  christos         {
   2400  1.1  christos           struct bfd_link_info *info = (struct bfd_link_info *) inf;
   2401  1.1  christos 
   2402  1.1  christos           info->flags |= DF_TEXTREL;
   2403  1.1  christos 
   2404  1.1  christos           /* Not an error, just cut short the traversal.  */
   2405  1.1  christos           return FALSE;
   2406  1.1  christos         }
   2407  1.1  christos     }
   2408  1.1  christos   return TRUE;
   2409  1.1  christos }
   2410  1.1  christos 
   2411  1.1  christos /* Set the sizes of the dynamic sections.  */
   2412  1.1  christos 
   2413  1.1  christos static bfd_boolean
   2414  1.1  christos or1k_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   2415  1.1  christos                                 struct bfd_link_info *info)
   2416  1.1  christos {
   2417  1.1  christos   struct elf_or1k_link_hash_table *htab;
   2418  1.1  christos   bfd *dynobj;
   2419  1.1  christos   asection *s;
   2420  1.1  christos   bfd_boolean relocs;
   2421  1.1  christos   bfd *ibfd;
   2422  1.1  christos 
   2423  1.1  christos   htab = or1k_elf_hash_table (info);
   2424  1.1  christos   if (htab == NULL)
   2425  1.1  christos     return FALSE;
   2426  1.1  christos 
   2427  1.1  christos   dynobj = htab->root.dynobj;
   2428  1.1  christos   BFD_ASSERT (dynobj != NULL);
   2429  1.1  christos 
   2430  1.1  christos   if (htab->root.dynamic_sections_created)
   2431  1.1  christos     {
   2432  1.1  christos       /* Set the contents of the .interp section to the interpreter.  */
   2433  1.4  christos       if (bfd_link_executable (info) && !info->nointerp)
   2434  1.1  christos         {
   2435  1.1  christos           s = bfd_get_section_by_name (dynobj, ".interp");
   2436  1.1  christos           BFD_ASSERT (s != NULL);
   2437  1.1  christos           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   2438  1.1  christos           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   2439  1.1  christos         }
   2440  1.1  christos     }
   2441  1.1  christos 
   2442  1.1  christos   /* Set up .got offsets for local syms, and space for local dynamic
   2443  1.1  christos      relocs.  */
   2444  1.1  christos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   2445  1.1  christos     {
   2446  1.1  christos       bfd_signed_vma *local_got;
   2447  1.1  christos       bfd_signed_vma *end_local_got;
   2448  1.1  christos       bfd_size_type locsymcount;
   2449  1.1  christos       Elf_Internal_Shdr *symtab_hdr;
   2450  1.1  christos       unsigned char *local_tls_type;
   2451  1.1  christos       asection *srel;
   2452  1.1  christos 
   2453  1.1  christos       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   2454  1.1  christos         continue;
   2455  1.1  christos 
   2456  1.1  christos       for (s = ibfd->sections; s != NULL; s = s->next)
   2457  1.1  christos         {
   2458  1.1  christos           struct elf_or1k_dyn_relocs *p;
   2459  1.1  christos 
   2460  1.1  christos           for (p = ((struct elf_or1k_dyn_relocs *)
   2461  1.1  christos                     elf_section_data (s)->local_dynrel);
   2462  1.1  christos                p != NULL;
   2463  1.1  christos                p = p->next)
   2464  1.1  christos             {
   2465  1.1  christos               if (! bfd_is_abs_section (p->sec)
   2466  1.1  christos                   && bfd_is_abs_section (p->sec->output_section))
   2467  1.1  christos                 {
   2468  1.1  christos                   /* Input section has been discarded, either because
   2469  1.1  christos                      it is a copy of a linkonce section or due to
   2470  1.1  christos                      linker script /DISCARD/, so we'll be discarding
   2471  1.1  christos                      the relocs too.  */
   2472  1.1  christos                 }
   2473  1.1  christos               else if (p->count != 0)
   2474  1.1  christos                 {
   2475  1.1  christos                   srel = elf_section_data (p->sec)->sreloc;
   2476  1.1  christos                   srel->size += p->count * sizeof (Elf32_External_Rela);
   2477  1.1  christos                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   2478  1.1  christos                     info->flags |= DF_TEXTREL;
   2479  1.1  christos                 }
   2480  1.1  christos             }
   2481  1.1  christos         }
   2482  1.1  christos 
   2483  1.1  christos       local_got = elf_local_got_refcounts (ibfd);
   2484  1.1  christos       if (!local_got)
   2485  1.1  christos         continue;
   2486  1.1  christos 
   2487  1.1  christos       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
   2488  1.1  christos       locsymcount = symtab_hdr->sh_info;
   2489  1.1  christos       end_local_got = local_got + locsymcount;
   2490  1.1  christos       s = htab->sgot;
   2491  1.1  christos       srel = htab->srelgot;
   2492  1.1  christos       local_tls_type = (unsigned char *) elf_or1k_local_tls_type (ibfd);
   2493  1.1  christos       for (; local_got < end_local_got; ++local_got)
   2494  1.1  christos         {
   2495  1.1  christos           if (*local_got > 0)
   2496  1.1  christos             {
   2497  1.1  christos               *local_got = s->size;
   2498  1.1  christos 
   2499  1.1  christos               /* TLS GD requires two GOT and two relocs.  */
   2500  1.1  christos               if (local_tls_type != NULL && *local_tls_type == TLS_GD)
   2501  1.1  christos                 s->size += 8;
   2502  1.1  christos               else
   2503  1.1  christos                 s->size += 4;
   2504  1.4  christos               if (bfd_link_pic (info))
   2505  1.1  christos                 {
   2506  1.1  christos                   if (local_tls_type != NULL && *local_tls_type == TLS_GD)
   2507  1.1  christos                     srel->size += 2 * sizeof (Elf32_External_Rela);
   2508  1.1  christos                   else
   2509  1.1  christos                     srel->size += sizeof (Elf32_External_Rela);
   2510  1.1  christos                 }
   2511  1.1  christos             }
   2512  1.1  christos           else
   2513  1.1  christos 
   2514  1.1  christos             *local_got = (bfd_vma) -1;
   2515  1.1  christos 
   2516  1.1  christos           if (local_tls_type)
   2517  1.1  christos             ++local_tls_type;
   2518  1.1  christos         }
   2519  1.1  christos     }
   2520  1.1  christos 
   2521  1.1  christos   /* Allocate global sym .plt and .got entries, and space for global
   2522  1.1  christos      sym dynamic relocs.  */
   2523  1.1  christos   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
   2524  1.1  christos 
   2525  1.1  christos   /* We now have determined the sizes of the various dynamic sections.
   2526  1.1  christos      Allocate memory for them.  */
   2527  1.1  christos   relocs = FALSE;
   2528  1.1  christos   for (s = dynobj->sections; s != NULL; s = s->next)
   2529  1.1  christos     {
   2530  1.1  christos       if ((s->flags & SEC_LINKER_CREATED) == 0)
   2531  1.1  christos         continue;
   2532  1.1  christos 
   2533  1.1  christos       if (s == htab->splt
   2534  1.1  christos           || s == htab->sgot
   2535  1.1  christos           || s == htab->sgotplt
   2536  1.1  christos           || s == htab->sdynbss)
   2537  1.1  christos         {
   2538  1.1  christos           /* Strip this section if we don't need it; see the
   2539  1.1  christos              comment below.  */
   2540  1.1  christos         }
   2541  1.1  christos       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
   2542  1.1  christos         {
   2543  1.1  christos           if (s->size != 0 && s != htab->srelplt)
   2544  1.1  christos             relocs = TRUE;
   2545  1.1  christos 
   2546  1.1  christos           /* We use the reloc_count field as a counter if we need
   2547  1.1  christos              to copy relocs into the output file.  */
   2548  1.1  christos           s->reloc_count = 0;
   2549  1.1  christos         }
   2550  1.1  christos       else
   2551  1.1  christos         /* It's not one of our sections, so don't allocate space.  */
   2552  1.1  christos         continue;
   2553  1.1  christos 
   2554  1.1  christos       if (s->size == 0)
   2555  1.1  christos         {
   2556  1.1  christos           /* If we don't need this section, strip it from the
   2557  1.1  christos              output file.  This is mostly to handle .rela.bss and
   2558  1.1  christos              .rela.plt.  We must create both sections in
   2559  1.1  christos              create_dynamic_sections, because they must be created
   2560  1.1  christos              before the linker maps input sections to output
   2561  1.1  christos              sections.  The linker does that before
   2562  1.1  christos              adjust_dynamic_symbol is called, and it is that
   2563  1.1  christos              function which decides whether anything needs to go
   2564  1.1  christos              into these sections.  */
   2565  1.1  christos           s->flags |= SEC_EXCLUDE;
   2566  1.1  christos           continue;
   2567  1.1  christos         }
   2568  1.1  christos 
   2569  1.1  christos       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   2570  1.1  christos         continue;
   2571  1.1  christos 
   2572  1.1  christos       /* Allocate memory for the section contents.  We use bfd_zalloc
   2573  1.1  christos          here in case unused entries are not reclaimed before the
   2574  1.1  christos          section's contents are written out.  This should not happen,
   2575  1.1  christos          but this way if it does, we get a R_OR1K_NONE reloc instead
   2576  1.1  christos          of garbage.  */
   2577  1.1  christos       s->contents = bfd_zalloc (dynobj, s->size);
   2578  1.1  christos 
   2579  1.1  christos       if (s->contents == NULL)
   2580  1.1  christos         return FALSE;
   2581  1.1  christos     }
   2582  1.1  christos 
   2583  1.1  christos   if (htab->root.dynamic_sections_created)
   2584  1.1  christos     {
   2585  1.1  christos       /* Add some entries to the .dynamic section.  We fill in the
   2586  1.1  christos          values later, in or1k_elf_finish_dynamic_sections, but we
   2587  1.1  christos          must add the entries now so that we get the correct size for
   2588  1.1  christos          the .dynamic section.  The DT_DEBUG entry is filled in by the
   2589  1.1  christos          dynamic linker and used by the debugger.  */
   2590  1.1  christos #define add_dynamic_entry(TAG, VAL) \
   2591  1.1  christos   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   2592  1.1  christos 
   2593  1.4  christos      if (bfd_link_executable (info))
   2594  1.1  christos        {
   2595  1.1  christos          if (! add_dynamic_entry (DT_DEBUG, 0))
   2596  1.1  christos            return FALSE;
   2597  1.1  christos        }
   2598  1.1  christos 
   2599  1.1  christos      if (htab->splt->size != 0)
   2600  1.1  christos        {
   2601  1.1  christos          if (! add_dynamic_entry (DT_PLTGOT, 0)
   2602  1.1  christos              || ! add_dynamic_entry (DT_PLTRELSZ, 0)
   2603  1.1  christos              || ! add_dynamic_entry (DT_PLTREL, DT_RELA)
   2604  1.1  christos              || ! add_dynamic_entry (DT_JMPREL, 0))
   2605  1.1  christos            return FALSE;
   2606  1.1  christos         }
   2607  1.1  christos 
   2608  1.1  christos      if (relocs)
   2609  1.1  christos        {
   2610  1.1  christos          if (! add_dynamic_entry (DT_RELA, 0)
   2611  1.1  christos              || ! add_dynamic_entry (DT_RELASZ, 0)
   2612  1.1  christos              || ! add_dynamic_entry (DT_RELAENT,
   2613  1.1  christos                                      sizeof (Elf32_External_Rela)))
   2614  1.1  christos            return FALSE;
   2615  1.1  christos 
   2616  1.1  christos          /* If any dynamic relocs apply to a read-only section,
   2617  1.1  christos             then we need a DT_TEXTREL entry.  */
   2618  1.1  christos          if ((info->flags & DF_TEXTREL) == 0)
   2619  1.1  christos            elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
   2620  1.1  christos                                    info);
   2621  1.1  christos 
   2622  1.1  christos          if ((info->flags & DF_TEXTREL) != 0)
   2623  1.1  christos            {
   2624  1.1  christos              if (! add_dynamic_entry (DT_TEXTREL, 0))
   2625  1.1  christos                return FALSE;
   2626  1.1  christos            }
   2627  1.1  christos        }
   2628  1.1  christos     }
   2629  1.1  christos 
   2630  1.1  christos #undef add_dynamic_entry
   2631  1.1  christos   return TRUE;
   2632  1.1  christos }
   2633  1.1  christos 
   2634  1.1  christos /* Create dynamic sections when linking against a dynamic object.  */
   2635  1.1  christos 
   2636  1.1  christos static bfd_boolean
   2637  1.1  christos or1k_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
   2638  1.1  christos {
   2639  1.1  christos   struct elf_or1k_link_hash_table *htab;
   2640  1.1  christos 
   2641  1.1  christos   htab = or1k_elf_hash_table (info);
   2642  1.1  christos   if (htab == NULL)
   2643  1.1  christos     return FALSE;
   2644  1.1  christos 
   2645  1.1  christos   if (!htab->sgot && !create_got_section (dynobj, info))
   2646  1.1  christos     return FALSE;
   2647  1.1  christos 
   2648  1.1  christos   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   2649  1.1  christos     return FALSE;
   2650  1.1  christos 
   2651  1.1  christos   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
   2652  1.1  christos   htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
   2653  1.1  christos   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
   2654  1.4  christos   if (!bfd_link_pic (info))
   2655  1.1  christos     htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
   2656  1.1  christos 
   2657  1.1  christos   if (!htab->splt || !htab->srelplt || !htab->sdynbss
   2658  1.4  christos       || (!bfd_link_pic (info) && !htab->srelbss))
   2659  1.1  christos     abort ();
   2660  1.1  christos 
   2661  1.1  christos   return TRUE;
   2662  1.1  christos }
   2663  1.1  christos 
   2664  1.1  christos /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   2665  1.1  christos 
   2666  1.1  christos static void
   2667  1.1  christos or1k_elf_copy_indirect_symbol (struct bfd_link_info *info,
   2668  1.1  christos                                struct elf_link_hash_entry *dir,
   2669  1.1  christos                                struct elf_link_hash_entry *ind)
   2670  1.1  christos {
   2671  1.1  christos   struct elf_or1k_link_hash_entry * edir;
   2672  1.1  christos   struct elf_or1k_link_hash_entry * eind;
   2673  1.1  christos 
   2674  1.1  christos   edir = (struct elf_or1k_link_hash_entry *) dir;
   2675  1.1  christos   eind = (struct elf_or1k_link_hash_entry *) ind;
   2676  1.1  christos 
   2677  1.1  christos   if (eind->dyn_relocs != NULL)
   2678  1.1  christos     {
   2679  1.1  christos       if (edir->dyn_relocs != NULL)
   2680  1.1  christos         {
   2681  1.1  christos           struct elf_or1k_dyn_relocs **pp;
   2682  1.1  christos           struct elf_or1k_dyn_relocs *p;
   2683  1.1  christos 
   2684  1.1  christos           /* Add reloc counts against the indirect sym to the direct sym
   2685  1.1  christos              list.  Merge any entries against the same section.  */
   2686  1.1  christos           for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
   2687  1.1  christos             {
   2688  1.1  christos               struct elf_or1k_dyn_relocs *q;
   2689  1.1  christos 
   2690  1.1  christos               for (q = edir->dyn_relocs; q != NULL; q = q->next)
   2691  1.1  christos                 if (q->sec == p->sec)
   2692  1.1  christos                   {
   2693  1.1  christos                     q->pc_count += p->pc_count;
   2694  1.1  christos                     q->count += p->count;
   2695  1.1  christos                     *pp = p->next;
   2696  1.1  christos                     break;
   2697  1.1  christos                   }
   2698  1.1  christos               if (q == NULL)
   2699  1.1  christos                 pp = &p->next;
   2700  1.1  christos             }
   2701  1.1  christos           *pp = edir->dyn_relocs;
   2702  1.1  christos         }
   2703  1.1  christos 
   2704  1.1  christos       edir->dyn_relocs = eind->dyn_relocs;
   2705  1.1  christos       eind->dyn_relocs = NULL;
   2706  1.1  christos     }
   2707  1.1  christos 
   2708  1.1  christos   if (ind->root.type == bfd_link_hash_indirect)
   2709  1.1  christos     {
   2710  1.1  christos       if (dir->got.refcount <= 0)
   2711  1.1  christos         {
   2712  1.1  christos           edir->tls_type = eind->tls_type;
   2713  1.1  christos           eind->tls_type = TLS_UNKNOWN;
   2714  1.1  christos         }
   2715  1.1  christos     }
   2716  1.1  christos 
   2717  1.1  christos   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   2718  1.1  christos }
   2719  1.1  christos 
   2720  1.1  christos /* Set the right machine number.  */
   2721  1.1  christos 
   2722  1.1  christos static bfd_boolean
   2723  1.1  christos or1k_elf_object_p (bfd *abfd)
   2724  1.1  christos {
   2725  1.1  christos   unsigned long mach = bfd_mach_or1k;
   2726  1.1  christos 
   2727  1.1  christos   if (elf_elfheader (abfd)->e_flags & EF_OR1K_NODELAY)
   2728  1.1  christos     mach = bfd_mach_or1knd;
   2729  1.1  christos 
   2730  1.1  christos   return bfd_default_set_arch_mach (abfd, bfd_arch_or1k, mach);
   2731  1.1  christos }
   2732  1.1  christos 
   2733  1.1  christos /* Store the machine number in the flags field.  */
   2734  1.1  christos 
   2735  1.1  christos static void
   2736  1.1  christos or1k_elf_final_write_processing (bfd *abfd,
   2737  1.1  christos 				 bfd_boolean linker ATTRIBUTE_UNUSED)
   2738  1.1  christos {
   2739  1.1  christos   switch (bfd_get_mach (abfd))
   2740  1.1  christos     {
   2741  1.1  christos     default:
   2742  1.1  christos     case bfd_mach_or1k:
   2743  1.1  christos       break;
   2744  1.1  christos     case bfd_mach_or1knd:
   2745  1.1  christos       elf_elfheader (abfd)->e_flags |= EF_OR1K_NODELAY;
   2746  1.1  christos       break;
   2747  1.1  christos     }
   2748  1.1  christos }
   2749  1.1  christos 
   2750  1.1  christos static bfd_boolean
   2751  1.1  christos or1k_elf_set_private_flags (bfd *abfd, flagword flags)
   2752  1.1  christos {
   2753  1.1  christos   BFD_ASSERT (!elf_flags_init (abfd)
   2754  1.1  christos               || elf_elfheader (abfd)->e_flags == flags);
   2755  1.1  christos 
   2756  1.1  christos   elf_elfheader (abfd)->e_flags = flags;
   2757  1.1  christos   elf_flags_init (abfd) = TRUE;
   2758  1.1  christos   return TRUE;
   2759  1.1  christos }
   2760  1.1  christos 
   2761  1.1  christos /* Make sure all input files are consistent with respect to
   2762  1.1  christos    EF_OR1K_NODELAY flag setting.  */
   2763  1.1  christos 
   2764  1.1  christos static bfd_boolean
   2765  1.1  christos elf32_or1k_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   2766  1.1  christos {
   2767  1.1  christos   flagword out_flags;
   2768  1.1  christos   flagword in_flags;
   2769  1.1  christos 
   2770  1.1  christos   in_flags  = elf_elfheader (ibfd)->e_flags;
   2771  1.1  christos   out_flags = elf_elfheader (obfd)->e_flags;
   2772  1.1  christos 
   2773  1.1  christos   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   2774  1.1  christos       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   2775  1.1  christos     return TRUE;
   2776  1.1  christos 
   2777  1.1  christos   if (!elf_flags_init (obfd))
   2778  1.1  christos     {
   2779  1.1  christos       elf_flags_init (obfd) = TRUE;
   2780  1.1  christos       elf_elfheader (obfd)->e_flags = in_flags;
   2781  1.1  christos 
   2782  1.1  christos       return TRUE;
   2783  1.1  christos     }
   2784  1.1  christos 
   2785  1.1  christos   if (in_flags == out_flags)
   2786  1.1  christos     return TRUE;
   2787  1.1  christos 
   2788  1.1  christos   if ((in_flags & EF_OR1K_NODELAY) != (out_flags & EF_OR1K_NODELAY))
   2789  1.1  christos     {
   2790  1.1  christos       (*_bfd_error_handler)
   2791  1.1  christos 	(_("%B: EF_OR1K_NODELAY flag mismatch with previous modules"), ibfd);
   2792  1.1  christos 
   2793  1.1  christos       bfd_set_error (bfd_error_bad_value);
   2794  1.1  christos       return FALSE;
   2795  1.1  christos     }
   2796  1.1  christos 
   2797  1.1  christos   return TRUE;
   2798  1.1  christos 
   2799  1.1  christos }
   2800  1.1  christos 
   2801  1.1  christos #define ELF_ARCH                        bfd_arch_or1k
   2802  1.1  christos #define ELF_MACHINE_CODE                EM_OR1K
   2803  1.1  christos #define ELF_TARGET_ID                   OR1K_ELF_DATA
   2804  1.1  christos #define ELF_MAXPAGESIZE                 0x2000
   2805  1.1  christos 
   2806  1.1  christos #define TARGET_BIG_SYM                  or1k_elf32_vec
   2807  1.1  christos #define TARGET_BIG_NAME                 "elf32-or1k"
   2808  1.1  christos 
   2809  1.1  christos #define elf_info_to_howto_rel           NULL
   2810  1.1  christos #define elf_info_to_howto               or1k_info_to_howto_rela
   2811  1.1  christos #define elf_backend_relocate_section    or1k_elf_relocate_section
   2812  1.1  christos #define elf_backend_gc_mark_hook        or1k_elf_gc_mark_hook
   2813  1.1  christos #define elf_backend_gc_sweep_hook       or1k_elf_gc_sweep_hook
   2814  1.1  christos #define elf_backend_check_relocs        or1k_elf_check_relocs
   2815  1.1  christos #define elf_backend_reloc_type_class    or1k_elf_reloc_type_class
   2816  1.1  christos #define elf_backend_can_gc_sections     1
   2817  1.1  christos #define elf_backend_rela_normal         1
   2818  1.1  christos 
   2819  1.1  christos #define bfd_elf32_mkobject                   elf_or1k_mkobject
   2820  1.1  christos 
   2821  1.1  christos #define bfd_elf32_bfd_merge_private_bfd_data elf32_or1k_merge_private_bfd_data
   2822  1.1  christos #define bfd_elf32_bfd_set_private_flags or1k_elf_set_private_flags
   2823  1.1  christos #define bfd_elf32_bfd_reloc_type_lookup or1k_reloc_type_lookup
   2824  1.1  christos #define bfd_elf32_bfd_reloc_name_lookup or1k_reloc_name_lookup
   2825  1.1  christos 
   2826  1.1  christos #define elf_backend_object_p                or1k_elf_object_p
   2827  1.1  christos #define elf_backend_final_write_processing  or1k_elf_final_write_processing
   2828  1.1  christos #define elf_backend_can_refcount                1
   2829  1.1  christos 
   2830  1.1  christos #define elf_backend_plt_readonly                1
   2831  1.1  christos #define elf_backend_want_got_plt                1
   2832  1.1  christos #define elf_backend_want_plt_sym                0
   2833  1.1  christos #define elf_backend_got_header_size             12
   2834  1.1  christos #define bfd_elf32_bfd_link_hash_table_create    or1k_elf_link_hash_table_create
   2835  1.1  christos #define elf_backend_copy_indirect_symbol        or1k_elf_copy_indirect_symbol
   2836  1.1  christos #define elf_backend_create_dynamic_sections     or1k_elf_create_dynamic_sections
   2837  1.1  christos #define elf_backend_finish_dynamic_sections     or1k_elf_finish_dynamic_sections
   2838  1.1  christos #define elf_backend_size_dynamic_sections       or1k_elf_size_dynamic_sections
   2839  1.1  christos #define elf_backend_adjust_dynamic_symbol       or1k_elf_adjust_dynamic_symbol
   2840  1.1  christos #define elf_backend_finish_dynamic_symbol       or1k_elf_finish_dynamic_symbol
   2841  1.1  christos 
   2842  1.1  christos #include "elf32-target.h"
   2843