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