Home | History | Annotate | Download | only in bfd
      1 /* CRIS-specific support for 32-bit ELF.
      2    Copyright (C) 2000-2014 Free Software Foundation, Inc.
      3    Contributed by Axis Communications AB.
      4    Written by Hans-Peter Nilsson, based on elf32-fr30.c
      5    PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
      6 
      7    This file is part of BFD, the Binary File Descriptor library.
      8 
      9    This program is free software; you can redistribute it and/or modify
     10    it under the terms of the GNU General Public License as published by
     11    the Free Software Foundation; either version 3 of the License, or
     12    (at your option) any later version.
     13 
     14    This program is distributed in the hope that it will be useful,
     15    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17    GNU General Public License for more details.
     18 
     19    You should have received a copy of the GNU General Public License
     20    along with this program; if not, write to the Free Software
     21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     22    MA 02110-1301, USA.  */
     23 
     24 #include "sysdep.h"
     25 #include "bfd.h"
     26 #include "libbfd.h"
     27 #include "elf-bfd.h"
     28 #include "elf/cris.h"
     29 #include <limits.h>
     30 
     31 bfd_reloc_status_type
     32 cris_elf_pcrel_reloc (bfd *, arelent *, asymbol *, void *,
     33 		      asection *, bfd *, char **);
     34 static bfd_boolean
     35 cris_elf_set_mach_from_flags (bfd *, unsigned long);
     36 
     37 /* Forward declarations.  */
     38 static reloc_howto_type cris_elf_howto_table [] =
     39 {
     40   /* This reloc does nothing.  */
     41   HOWTO (R_CRIS_NONE,		/* type */
     42 	 0,			/* rightshift */
     43 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
     44 	 32,			/* bitsize */
     45 	 FALSE,			/* pc_relative */
     46 	 0,			/* bitpos */
     47 	 complain_overflow_bitfield, /* complain_on_overflow */
     48 	 bfd_elf_generic_reloc,	/* special_function */
     49 	 "R_CRIS_NONE",		/* name */
     50 	 FALSE,			/* partial_inplace */
     51 	 0,			/* src_mask */
     52 	 0,			/* dst_mask */
     53 	 FALSE),		/* pcrel_offset */
     54 
     55   /* An 8 bit absolute relocation.  */
     56   HOWTO (R_CRIS_8,		/* type */
     57 	 0,			/* rightshift */
     58 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
     59 	 8,			/* bitsize */
     60 	 FALSE,			/* pc_relative */
     61 	 0,			/* bitpos */
     62 	 complain_overflow_bitfield, /* complain_on_overflow */
     63 	 bfd_elf_generic_reloc,	/* special_function */
     64 	 "R_CRIS_8",		/* name */
     65 	 FALSE,			/* partial_inplace */
     66 	 0x0000,		/* src_mask */
     67 	 0x00ff,		/* dst_mask */
     68 	 FALSE),		/* pcrel_offset */
     69 
     70   /* A 16 bit absolute relocation.  */
     71   HOWTO (R_CRIS_16,		/* type */
     72 	 0,			/* rightshift */
     73 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
     74 	 16,			/* bitsize */
     75 	 FALSE,			/* pc_relative */
     76 	 0,			/* bitpos */
     77 	 complain_overflow_bitfield, /* complain_on_overflow */
     78 	 bfd_elf_generic_reloc,	/* special_function */
     79 	 "R_CRIS_16",		/* name */
     80 	 FALSE,			/* partial_inplace */
     81 	 0x00000000,		/* src_mask */
     82 	 0x0000ffff,		/* dst_mask */
     83 	 FALSE),		/* pcrel_offset */
     84 
     85   /* A 32 bit absolute relocation.  */
     86   HOWTO (R_CRIS_32,		/* type */
     87 	 0,			/* rightshift */
     88 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
     89 	 32,			/* bitsize */
     90 	 FALSE,			/* pc_relative */
     91 	 0,			/* bitpos */
     92 	 /* We don't want overflow complaints for 64-bit vma builds
     93 	    for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
     94 	    32-bit ELF) where sym=0xc0001234.
     95 	    Don't do this for the PIC relocs, as we don't expect to
     96 	    see them with large offsets.  */
     97 	 complain_overflow_dont, /* complain_on_overflow */
     98 	 bfd_elf_generic_reloc,	/* special_function */
     99 	 "R_CRIS_32",		/* name */
    100 	 FALSE,			/* partial_inplace */
    101 	 0x00000000,		/* src_mask */
    102 	 0xffffffff,		/* dst_mask */
    103 	 FALSE),		/* pcrel_offset */
    104 
    105   /* An 8 bit PC-relative relocation.  */
    106   HOWTO (R_CRIS_8_PCREL,	/* type */
    107 	 0,			/* rightshift */
    108 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    109 	 8,			/* bitsize */
    110 	 TRUE,			/* pc_relative */
    111 	 0,			/* bitpos */
    112 	 complain_overflow_bitfield, /* complain_on_overflow */
    113 	 cris_elf_pcrel_reloc,	/* special_function */
    114 	 "R_CRIS_8_PCREL",	/* name */
    115 	 FALSE,			/* partial_inplace */
    116 	 0x0000,		/* src_mask */
    117 	 0x00ff,		/* dst_mask */
    118 	 TRUE),			/* pcrel_offset */
    119 
    120   /* A 16 bit PC-relative relocation.  */
    121   HOWTO (R_CRIS_16_PCREL,	/* type */
    122 	 0,			/* rightshift */
    123 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    124 	 16,			/* bitsize */
    125 	 TRUE,			/* pc_relative */
    126 	 0,			/* bitpos */
    127 	 complain_overflow_bitfield, /* complain_on_overflow */
    128 	 cris_elf_pcrel_reloc,	/* special_function */
    129 	 "R_CRIS_16_PCREL",	/* name */
    130 	 FALSE,			/* partial_inplace */
    131 	 0x00000000,		/* src_mask */
    132 	 0x0000ffff,		/* dst_mask */
    133 	 TRUE),			/* pcrel_offset */
    134 
    135   /* A 32 bit PC-relative relocation.  */
    136   HOWTO (R_CRIS_32_PCREL,	/* type */
    137 	 0,			/* rightshift */
    138 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    139 	 32,			/* bitsize */
    140 	 TRUE,			/* pc_relative */
    141 	 0,			/* bitpos */
    142 	 complain_overflow_bitfield, /* complain_on_overflow */
    143 	 cris_elf_pcrel_reloc,	/* special_function */
    144 	 "R_CRIS_32_PCREL",	/* name */
    145 	 FALSE,			/* partial_inplace */
    146 	 0x00000000,		/* src_mask */
    147 	 0xffffffff,		/* dst_mask */
    148 	 TRUE),			/* pcrel_offset */
    149 
    150   /* GNU extension to record C++ vtable hierarchy.  */
    151   HOWTO (R_CRIS_GNU_VTINHERIT,	/* type */
    152 	 0,			/* rightshift */
    153 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    154 	 0,			/* bitsize */
    155 	 FALSE,			/* pc_relative */
    156 	 0,			/* bitpos */
    157 	 complain_overflow_dont, /* complain_on_overflow */
    158 	 NULL,			/* special_function */
    159 	 "R_CRIS_GNU_VTINHERIT", /* name */
    160 	 FALSE,			/* partial_inplace */
    161 	 0,			/* src_mask */
    162 	 0,			/* dst_mask */
    163 	 FALSE),		/* pcrel_offset */
    164 
    165   /* GNU extension to record C++ vtable member usage.  */
    166   HOWTO (R_CRIS_GNU_VTENTRY,	/* type */
    167 	 0,			/* rightshift */
    168 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    169 	 0,			/* bitsize */
    170 	 FALSE,			/* pc_relative */
    171 	 0,			/* bitpos */
    172 	 complain_overflow_dont, /* complain_on_overflow */
    173 	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
    174 	 "R_CRIS_GNU_VTENTRY",	 /* name */
    175 	 FALSE,			/* partial_inplace */
    176 	 0,			/* src_mask */
    177 	 0,			/* dst_mask */
    178 	 FALSE),		/* pcrel_offset */
    179 
    180   /* This is used only by the dynamic linker.  The symbol should exist
    181      both in the object being run and in some shared library.  The
    182      dynamic linker copies the data addressed by the symbol from the
    183      shared library into the object, because the object being
    184      run has to have the data at some particular address.  */
    185   HOWTO (R_CRIS_COPY,		/* type */
    186 	 0,			/* rightshift */
    187 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    188 	 32,			/* bitsize */
    189 	 FALSE,			/* pc_relative */
    190 	 0,			/* bitpos */
    191 	 complain_overflow_bitfield, /* complain_on_overflow */
    192 	 bfd_elf_generic_reloc,	/* special_function */
    193 	 "R_CRIS_COPY",		/* name */
    194 	 FALSE,			/* partial_inplace */
    195 	 0,			/* src_mask */
    196 	 0,			/* dst_mask */
    197 	 FALSE),		/* pcrel_offset */
    198 
    199   /* Like R_CRIS_32, but used when setting global offset table entries.  */
    200   HOWTO (R_CRIS_GLOB_DAT,	/* type */
    201 	 0,			/* rightshift */
    202 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    203 	 32,			/* bitsize */
    204 	 FALSE,			/* pc_relative */
    205 	 0,			/* bitpos */
    206 	 complain_overflow_bitfield, /* complain_on_overflow */
    207 	 bfd_elf_generic_reloc,	/* special_function */
    208 	 "R_CRIS_GLOB_DAT",	/* name */
    209 	 FALSE,			/* partial_inplace */
    210 	 0,			/* src_mask */
    211 	 0xffffffff,		/* dst_mask */
    212 	 FALSE),		/* pcrel_offset */
    213 
    214   /* Marks a procedure linkage table entry for a symbol.  */
    215   HOWTO (R_CRIS_JUMP_SLOT,	/* type */
    216 	 0,			/* rightshift */
    217 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    218 	 32,			/* bitsize */
    219 	 FALSE,			/* pc_relative */
    220 	 0,			/* bitpos */
    221 	 complain_overflow_bitfield, /* complain_on_overflow */
    222 	 bfd_elf_generic_reloc,	/* special_function */
    223 	 "R_CRIS_JUMP_SLOT",	/* name */
    224 	 FALSE,			/* partial_inplace */
    225 	 0,			/* src_mask */
    226 	 0,			/* dst_mask */
    227 	 FALSE),		/* pcrel_offset */
    228 
    229   /* Used only by the dynamic linker.  When the object is run, this
    230      longword is set to the load address of the object, plus the
    231      addend.  */
    232   HOWTO (R_CRIS_RELATIVE,	/* type */
    233 	 0,			/* rightshift */
    234 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    235 	 32,			/* bitsize */
    236 	 FALSE,			/* pc_relative */
    237 	 0,			/* bitpos */
    238 	 complain_overflow_bitfield, /* complain_on_overflow */
    239 	 bfd_elf_generic_reloc,	/* special_function */
    240 	 "R_CRIS_RELATIVE",	/* name */
    241 	 FALSE,			/* partial_inplace */
    242 	 0,			/* src_mask */
    243 	 0xffffffff,		/* dst_mask */
    244 	 FALSE),		/* pcrel_offset */
    245 
    246   /* Like R_CRIS_32, but referring to the GOT table entry for the symbol.  */
    247   HOWTO (R_CRIS_16_GOT,		/* type */
    248 	 0,			/* rightshift */
    249 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    250 	 16,			/* bitsize */
    251 	 FALSE,			/* pc_relative */
    252 	 0,			/* bitpos */
    253 	 complain_overflow_bitfield, /* complain_on_overflow */
    254 	 bfd_elf_generic_reloc,	/* special_function */
    255 	 "R_CRIS_16_GOT",	/* name */
    256 	 FALSE,			/* partial_inplace */
    257 	 0,			/* src_mask */
    258 	 0xffff,		/* dst_mask */
    259 	 FALSE),		/* pcrel_offset */
    260 
    261   HOWTO (R_CRIS_32_GOT,		/* type */
    262 	 0,			/* rightshift */
    263 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    264 	 32,			/* bitsize */
    265 	 FALSE,			/* pc_relative */
    266 	 0,			/* bitpos */
    267 	 complain_overflow_bitfield, /* complain_on_overflow */
    268 	 bfd_elf_generic_reloc,	/* special_function */
    269 	 "R_CRIS_32_GOT",	/* name */
    270 	 FALSE,			/* partial_inplace */
    271 	 0,			/* src_mask */
    272 	 0xffffffff,		/* dst_mask */
    273 	 FALSE),		/* pcrel_offset */
    274 
    275   /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
    276      the GOT table for the symbol.  */
    277   HOWTO (R_CRIS_16_GOTPLT,	/* type */
    278 	 0,			/* rightshift */
    279 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    280 	 16,			/* bitsize */
    281 	 FALSE,			/* pc_relative */
    282 	 0,			/* bitpos */
    283 	 complain_overflow_bitfield, /* complain_on_overflow */
    284 	 bfd_elf_generic_reloc,	/* special_function */
    285 	 "R_CRIS_16_GOTPLT",	/* name */
    286 	 FALSE,			/* partial_inplace */
    287 	 0,			/* src_mask */
    288 	 0xffff,		/* dst_mask */
    289 	 FALSE),		/* pcrel_offset */
    290 
    291   HOWTO (R_CRIS_32_GOTPLT,	/* type */
    292 	 0,			/* rightshift */
    293 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    294 	 32,			/* bitsize */
    295 	 FALSE,			/* pc_relative */
    296 	 0,			/* bitpos */
    297 	 complain_overflow_bitfield, /* complain_on_overflow */
    298 	 bfd_elf_generic_reloc,	/* special_function */
    299 	 "R_CRIS_32_GOTPLT",	/* name */
    300 	 FALSE,			/* partial_inplace */
    301 	 0,			/* src_mask */
    302 	 0xffffffff,		/* dst_mask */
    303 	 FALSE),		/* pcrel_offset */
    304 
    305   /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
    306      be necessary.  */
    307   HOWTO (R_CRIS_32_GOTREL,	/* type */
    308 	 0,			/* rightshift */
    309 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    310 	 32,			/* bitsize */
    311 	 FALSE,			/* pc_relative */
    312 	 0,			/* bitpos */
    313 	 complain_overflow_bitfield, /* complain_on_overflow */
    314 	 bfd_elf_generic_reloc,	/* special_function */
    315 	 "R_CRIS_32_GOTREL",	/* name */
    316 	 FALSE,			/* partial_inplace */
    317 	 0,			/* src_mask */
    318 	 0xffffffff,		/* dst_mask */
    319 	 FALSE),		/* pcrel_offset */
    320 
    321   /* A 32-bit offset from GOT to entry for this symbol in PLT and request
    322      to create PLT entry for symbol.  */
    323   HOWTO (R_CRIS_32_PLT_GOTREL,	/* type */
    324 	 0,			/* rightshift */
    325 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    326 	 32,			/* bitsize */
    327 	 FALSE,			/* pc_relative */
    328 	 0,			/* bitpos */
    329 	 complain_overflow_bitfield, /* complain_on_overflow */
    330 	 bfd_elf_generic_reloc,	/* special_function */
    331 	 "R_CRIS_32_PLT_GOTREL", /* name */
    332 	 FALSE,			/* partial_inplace */
    333 	 0,			/* src_mask */
    334 	 0xffffffff,		/* dst_mask */
    335 	 FALSE),		/* pcrel_offset */
    336 
    337   /* A 32-bit offset from PC (location after the relocation) + addend to
    338      entry for this symbol in PLT and request to create PLT entry for
    339      symbol.  */
    340   HOWTO (R_CRIS_32_PLT_PCREL,	/* type */
    341 	 0,			/* rightshift */
    342 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    343 	 32,			/* bitsize */
    344 	 TRUE,			/* pc_relative */
    345 	 0,			/* bitpos */
    346 	 complain_overflow_bitfield, /* complain_on_overflow */
    347 	 cris_elf_pcrel_reloc,	/* special_function */
    348 	 "R_CRIS_32_PLT_PCREL",	/* name */
    349 	 FALSE,			/* partial_inplace */
    350 	 0,			/* src_mask */
    351 	 0xffffffff,		/* dst_mask */
    352 	 TRUE),			/* pcrel_offset */
    353 
    354   /* We don't handle these in any special manner and cross-format
    355      linking is not supported; just recognize them enough to pass them
    356      around.  FIXME: do the same for most PIC relocs and add sanity
    357      tests to actually refuse gracefully to handle these and PIC
    358      relocs for cross-format linking.  */
    359 #define TLSHOWTO32(name) \
    360  HOWTO (name, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, \
    361         bfd_elf_generic_reloc, #name, FALSE, 0, 0xffffffff, FALSE)
    362 #define TLSHOWTO16X(name, X)	     \
    363  HOWTO (name, 0, 1, 16, FALSE, 0, complain_overflow_ ## X, \
    364         bfd_elf_generic_reloc, #name, FALSE, 0, 0xffff, FALSE)
    365 #define TLSHOWTO16(name) TLSHOWTO16X(name, unsigned)
    366 #define TLSHOWTO16S(name) TLSHOWTO16X(name, signed)
    367 
    368   TLSHOWTO32 (R_CRIS_32_GOT_GD),
    369   TLSHOWTO16 (R_CRIS_16_GOT_GD),
    370   TLSHOWTO32 (R_CRIS_32_GD),
    371   TLSHOWTO32 (R_CRIS_DTP),
    372   TLSHOWTO32 (R_CRIS_32_DTPREL),
    373   TLSHOWTO16S (R_CRIS_16_DTPREL),
    374   TLSHOWTO32 (R_CRIS_32_GOT_TPREL),
    375   TLSHOWTO16S (R_CRIS_16_GOT_TPREL),
    376   TLSHOWTO32 (R_CRIS_32_TPREL),
    377   TLSHOWTO16S (R_CRIS_16_TPREL),
    378   TLSHOWTO32 (R_CRIS_DTPMOD),
    379   TLSHOWTO32 (R_CRIS_32_IE)
    380 };
    381 
    382 /* Map BFD reloc types to CRIS ELF reloc types.  */
    384 
    385 struct cris_reloc_map
    386 {
    387   bfd_reloc_code_real_type bfd_reloc_val;
    388   unsigned int cris_reloc_val;
    389 };
    390 
    391 static const struct cris_reloc_map cris_reloc_map [] =
    392 {
    393   { BFD_RELOC_NONE,		R_CRIS_NONE },
    394   { BFD_RELOC_8,		R_CRIS_8 },
    395   { BFD_RELOC_16,		R_CRIS_16 },
    396   { BFD_RELOC_32,		R_CRIS_32 },
    397   { BFD_RELOC_8_PCREL,		R_CRIS_8_PCREL },
    398   { BFD_RELOC_16_PCREL,		R_CRIS_16_PCREL },
    399   { BFD_RELOC_32_PCREL,		R_CRIS_32_PCREL },
    400   { BFD_RELOC_VTABLE_INHERIT,	R_CRIS_GNU_VTINHERIT },
    401   { BFD_RELOC_VTABLE_ENTRY,	R_CRIS_GNU_VTENTRY },
    402   { BFD_RELOC_CRIS_COPY,	R_CRIS_COPY },
    403   { BFD_RELOC_CRIS_GLOB_DAT,	R_CRIS_GLOB_DAT },
    404   { BFD_RELOC_CRIS_JUMP_SLOT,	R_CRIS_JUMP_SLOT },
    405   { BFD_RELOC_CRIS_RELATIVE,	R_CRIS_RELATIVE },
    406   { BFD_RELOC_CRIS_16_GOT,	R_CRIS_16_GOT },
    407   { BFD_RELOC_CRIS_32_GOT,	R_CRIS_32_GOT },
    408   { BFD_RELOC_CRIS_16_GOTPLT,	R_CRIS_16_GOTPLT },
    409   { BFD_RELOC_CRIS_32_GOTPLT,	R_CRIS_32_GOTPLT },
    410   { BFD_RELOC_CRIS_32_GOTREL,	R_CRIS_32_GOTREL },
    411   { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
    412   { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL },
    413   { BFD_RELOC_CRIS_32_GOT_GD,	R_CRIS_32_GOT_GD },
    414   { BFD_RELOC_CRIS_16_GOT_GD,	R_CRIS_16_GOT_GD },
    415   { BFD_RELOC_CRIS_32_GD,	R_CRIS_32_GD },
    416   { BFD_RELOC_CRIS_DTP,	R_CRIS_DTP },
    417   { BFD_RELOC_CRIS_32_DTPREL,	R_CRIS_32_DTPREL },
    418   { BFD_RELOC_CRIS_16_DTPREL,	R_CRIS_16_DTPREL },
    419   { BFD_RELOC_CRIS_32_GOT_TPREL, R_CRIS_32_GOT_TPREL },
    420   { BFD_RELOC_CRIS_16_GOT_TPREL, R_CRIS_16_GOT_TPREL },
    421   { BFD_RELOC_CRIS_32_TPREL,	R_CRIS_32_TPREL },
    422   { BFD_RELOC_CRIS_16_TPREL,	R_CRIS_16_TPREL },
    423   { BFD_RELOC_CRIS_DTPMOD,	R_CRIS_DTPMOD },
    424   { BFD_RELOC_CRIS_32_IE,	R_CRIS_32_IE }
    425 };
    426 
    427 static reloc_howto_type *
    428 cris_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
    429 			bfd_reloc_code_real_type code)
    430 {
    431   unsigned int i;
    432 
    433   for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
    434     if (cris_reloc_map [i].bfd_reloc_val == code)
    435       return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
    436 
    437   return NULL;
    438 }
    439 
    440 static reloc_howto_type *
    441 cris_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
    442 {
    443   unsigned int i;
    444 
    445   for (i = 0;
    446        i < sizeof (cris_elf_howto_table) / sizeof (cris_elf_howto_table[0]);
    447        i++)
    448     if (cris_elf_howto_table[i].name != NULL
    449 	&& strcasecmp (cris_elf_howto_table[i].name, r_name) == 0)
    450       return &cris_elf_howto_table[i];
    451 
    452   return NULL;
    453 }
    454 
    455 /* Set the howto pointer for an CRIS ELF reloc.  */
    456 
    457 static void
    458 cris_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
    459 			 arelent * cache_ptr,
    460 			 Elf_Internal_Rela * dst)
    461 {
    462   enum elf_cris_reloc_type r_type;
    463 
    464   r_type = ELF32_R_TYPE (dst->r_info);
    465   BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
    466   cache_ptr->howto = & cris_elf_howto_table [r_type];
    467 }
    468 
    469 bfd_reloc_status_type
    470 cris_elf_pcrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
    471 		      arelent *reloc_entry,
    472 		      asymbol *symbol,
    473 		      void * data ATTRIBUTE_UNUSED,
    474 		      asection *input_section,
    475 		      bfd *output_bfd,
    476 		      char **error_message ATTRIBUTE_UNUSED)
    477 {
    478   /* By default (using only bfd_elf_generic_reloc when linking to
    479      non-ELF formats) PC-relative relocs are relative to the beginning
    480      of the reloc.  CRIS PC-relative relocs are relative to the position
    481      *after* the reloc because that's what pre-CRISv32 PC points to
    482      after reading an insn field with that reloc.  (For CRISv32, PC is
    483      actually relative to the start of the insn, but we keep the old
    484      definition.)  Still, we use as much generic machinery as we can.
    485 
    486      Only adjust when doing a final link.  */
    487   if (output_bfd == (bfd *) NULL)
    488     reloc_entry->addend -= 1 << reloc_entry->howto->size;
    489 
    490   return
    491     bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
    492 			   input_section, output_bfd, error_message);
    493 }
    494 
    495 /* Support for core dump NOTE sections.
    497    The slightly unintuitive code layout is an attempt to keep at least
    498    some similarities with other ports, hoping to simplify general
    499    changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart.  */
    500 
    501 static bfd_boolean
    502 cris_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
    503 {
    504   int offset;
    505   size_t size;
    506 
    507   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
    508     switch (note->descsz)
    509       {
    510       default:
    511 	return FALSE;
    512 
    513       case 202:		/* Linux/CRISv32 */
    514 	/* pr_cursig */
    515 	elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
    516 
    517 	/* pr_pid */
    518 	elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
    519 
    520 	/* pr_reg */
    521 	offset = 70;
    522 	size = 128;
    523 
    524 	break;
    525       }
    526   else
    527     switch (note->descsz)
    528       {
    529       default:
    530 	return FALSE;
    531 
    532       case 214:		/* Linux/CRIS */
    533 	/* pr_cursig */
    534 	elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
    535 
    536 	/* pr_pid */
    537 	elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
    538 
    539 	/* pr_reg */
    540 	offset = 70;
    541 	size = 140;
    542 
    543 	break;
    544       }
    545 
    546   /* Make a ".reg/999" section.  */
    547   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
    548 					  size, note->descpos + offset);
    549 }
    550 
    551 static bfd_boolean
    552 cris_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
    553 {
    554   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
    555     switch (note->descsz)
    556       {
    557       default:
    558 	return FALSE;
    559 
    560       case 124:		/* Linux/CRISv32 elf_prpsinfo */
    561 	elf_tdata (abfd)->core->program
    562 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
    563 	elf_tdata (abfd)->core->command
    564 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
    565       }
    566   else
    567     switch (note->descsz)
    568       {
    569       default:
    570 	return FALSE;
    571 
    572       case 124:		/* Linux/CRIS elf_prpsinfo */
    573 	elf_tdata (abfd)->core->program
    574 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
    575 	elf_tdata (abfd)->core->command
    576 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
    577       }
    578 
    579   /* Note that for some reason, a spurious space is tacked
    580      onto the end of the args in some (at least one anyway)
    581      implementations, so strip it off if it exists.  */
    582 
    583   {
    584     char *command = elf_tdata (abfd)->core->command;
    585     int n = strlen (command);
    586 
    587     if (0 < n && command[n - 1] == ' ')
    588       command[n - 1] = '\0';
    589   }
    590 
    591   return TRUE;
    592 }
    593 
    594 /* The name of the dynamic interpreter.  This is put in the .interp
    596    section.  */
    597 
    598 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
    599 
    600 /* The size in bytes of an entry in the procedure linkage table.  */
    601 
    602 #define PLT_ENTRY_SIZE 20
    603 #define PLT_ENTRY_SIZE_V32 26
    604 
    605 /* The first entry in an absolute procedure linkage table looks like this.  */
    606 
    607 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
    608 {
    609   0xfc, 0xe1,
    610   0x7e, 0x7e,	/* push mof.  */
    611   0x7f, 0x0d,   /*  (dip [pc+]) */
    612   0, 0, 0, 0,	/*  Replaced with address of .got + 4.  */
    613   0x30, 0x7a,	/* move [...],mof */
    614   0x7f, 0x0d,   /*  (dip [pc+]) */
    615   0, 0, 0, 0,	/*  Replaced with address of .got + 8.  */
    616   0x30, 0x09	/* jump [...] */
    617 };
    618 
    619 static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
    620 {
    621   0x84, 0xe2,	/* subq 4,$sp */
    622   0x6f, 0xfe,	/* move.d 0,$acr */
    623   0, 0, 0, 0,	/*  Replaced by address of .got + 4.  */
    624   0x7e, 0x7a,	/* move $mof,[$sp] */
    625   0x3f, 0x7a,	/* move [$acr],$mof */
    626   0x04, 0xf2,	/* addq 4,acr */
    627   0x6f, 0xfa,	/* move.d [$acr],$acr */
    628   0xbf, 0x09,	/* jump $acr */
    629   0xb0, 0x05,	/* nop */
    630   0, 0		/*  Pad out to 26 bytes.  */
    631 };
    632 
    633 /* Subsequent entries in an absolute procedure linkage table look like
    634    this.  */
    635 
    636 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
    637 {
    638   0x7f, 0x0d,   /*  (dip [pc+]) */
    639   0, 0, 0, 0,	/*  Replaced with address of this symbol in .got.  */
    640   0x30, 0x09,	/* jump [...] */
    641   0x3f,	 0x7e,	/* move [pc+],mof */
    642   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
    643   0x2f, 0xfe,	/* add.d [pc+],pc */
    644   0xec, 0xff,
    645   0xff, 0xff	/*  Replaced with offset to start of .plt.  */
    646 };
    647 
    648 static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
    649 {
    650   0x6f, 0xfe,	/* move.d 0,$acr */
    651   0, 0, 0, 0,	/*  Replaced with address of this symbol in .got.  */
    652   0x6f, 0xfa,   /* move.d [$acr],$acr */
    653   0xbf, 0x09,   /* jump $acr */
    654   0xb0, 0x05,	/* nop */
    655   0x3f, 0x7e,	/* move 0,mof */
    656   0, 0, 0, 0,	/*  Replaced with offset into relocation table. */
    657   0xbf, 0x0e,	/* ba start_of_plt0_entry */
    658   0, 0, 0, 0,	/*  Replaced with offset to plt0 entry.  */
    659   0xb0, 0x05	/* nop */
    660 };
    661 
    662 /* The first entry in a PIC procedure linkage table looks like this.  */
    663 
    664 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
    665 {
    666   0xfc, 0xe1, 0x7e, 0x7e,	/* push mof */
    667   0x04, 0x01, 0x30, 0x7a,	/* move [r0+4],mof */
    668   0x08, 0x01, 0x30, 0x09,	/* jump [r0+8] */
    669   0, 0, 0, 0, 0, 0, 0, 0,	/*  Pad out to 20 bytes.  */
    670 };
    671 
    672 static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
    673 {
    674   0x84, 0xe2,	/* subq 4,$sp */
    675   0x04, 0x01,	/* addoq 4,$r0,$acr */
    676   0x7e, 0x7a,	/* move $mof,[$sp] */
    677   0x3f, 0x7a,	/* move [$acr],$mof */
    678   0x04, 0xf2,	/* addq 4,$acr */
    679   0x6f, 0xfa,	/* move.d [$acr],$acr */
    680   0xbf, 0x09,	/* jump $acr */
    681   0xb0, 0x05,	/* nop */
    682   0, 0,		/*  Pad out to 26 bytes.  */
    683   0, 0, 0, 0,
    684   0, 0, 0, 0
    685 };
    686 
    687 /* Subsequent entries in a PIC procedure linkage table look like this.  */
    688 
    689 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
    690 {
    691   0x6f, 0x0d,   /*  (bdap [pc+].d,r0) */
    692   0, 0, 0, 0,	/*  Replaced with offset of this symbol in .got.  */
    693   0x30, 0x09,	/* jump [...] */
    694   0x3f, 0x7e,	/* move [pc+],mof */
    695   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
    696   0x2f, 0xfe,	/* add.d [pc+],pc */
    697   0xec, 0xff,	/*  Replaced with offset to start of .plt.  */
    698   0xff, 0xff
    699 };
    700 
    701 static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
    702 {
    703   0x6f, 0x0d,	/* addo.d 0,$r0,$acr */
    704   0, 0, 0, 0,	/*  Replaced with offset of this symbol in .got.  */
    705   0x6f, 0xfa,	/* move.d [$acr],$acr */
    706   0xbf, 0x09,	/* jump $acr */
    707   0xb0, 0x05,	/* nop */
    708   0x3f, 0x7e,	/* move relocoffs,$mof */
    709   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
    710   0xbf, 0x0e,	/* ba start_of_plt */
    711   0, 0, 0, 0,	/*  Replaced with offset to start of .plt.  */
    712   0xb0, 0x05	/* nop */
    713 };
    714 
    715 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
    717    (and most other PIC/shlib stuff).  Check that we don't drift away
    718    without reason.
    719 
    720    The CRIS linker, like the m68k and i386 linkers (and probably the rest
    721    too) needs to keep track of the number of relocs that it decides to
    722    copy in check_relocs for each symbol.  This is so that it can discard
    723    PC relative relocs if it doesn't need them when linking with
    724    -Bsymbolic.  We store the information in a field extending the regular
    725    ELF linker hash table.  */
    726 
    727 /* This structure keeps track of the number of PC relative relocs we have
    728    copied for a given symbol.  */
    729 
    730 struct elf_cris_pcrel_relocs_copied
    731 {
    732   /* Next section.  */
    733   struct elf_cris_pcrel_relocs_copied *next;
    734 
    735   /* A section in dynobj.  */
    736   asection *section;
    737 
    738   /* Number of relocs copied in this section.  */
    739   bfd_size_type count;
    740 
    741   /* Example of reloc being copied, for message.  */
    742   enum elf_cris_reloc_type r_type;
    743 };
    744 
    745 /* CRIS ELF linker hash entry.  */
    746 
    747 struct elf_cris_link_hash_entry
    748 {
    749   struct elf_link_hash_entry root;
    750 
    751   /* Number of PC relative relocs copied for this symbol.  */
    752   struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
    753 
    754   /* The GOTPLT references are CRIS-specific; the goal is to avoid having
    755      both a general GOT and a PLT-specific GOT entry for the same symbol,
    756      when it is referenced both as a function and as a function pointer.
    757 
    758      Number of GOTPLT references for a function.  */
    759   bfd_signed_vma gotplt_refcount;
    760 
    761   /* Actual GOTPLT index for this symbol, if applicable, or zero if not
    762      (zero is never used as an index).  FIXME: We should be able to fold
    763      this with gotplt_refcount in a union, like the got and plt unions in
    764      elf_link_hash_entry.  */
    765   bfd_size_type gotplt_offset;
    766 
    767   /* The root.got.refcount is the sum of the regular reference counts
    768      (this) and those members below.  We have to keep a separate count
    769      to track when we've found the first (or last) reference to a
    770      regular got entry.  The offset is in root.got.offset.  */
    771   bfd_signed_vma reg_got_refcount;
    772 
    773   /* Similar to the above, the number of reloc references to this
    774      symbols that need a R_CRIS_32_TPREL slot.  The offset is in
    775      root.got.offset, because this and .dtp_refcount can't validly
    776      happen when there's also a regular GOT entry; that's invalid
    777      input for which an error is emitted.  */
    778   bfd_signed_vma tprel_refcount;
    779 
    780   /* Similar to the above, the number of reloc references to this
    781      symbols that need a R_CRIS_DTP slot.  The offset is in
    782      root.got.offset; plus 4 if .tprel_refcount > 0.  */
    783   bfd_signed_vma dtp_refcount;
    784 };
    785 
    786 static bfd_boolean
    787 elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *,
    788 				      void * );
    789 static bfd_boolean
    790 elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *,
    791 					  void *);
    792 
    793 /* The local_got_refcounts and local_got_offsets are a multiple of
    794    LSNUM in size, namely LGOT_ALLOC_NELTS_FOR(LSNUM) (plus one for the
    795    refcount for GOT itself, see code), with the summary / group offset
    796    for local symbols located at offset N, reference counts for
    797    ordinary (address) relocs at offset N + LSNUM, for R_CRIS_DTP
    798    relocs at offset N + 2*LSNUM, and for R_CRIS_32_TPREL relocs at N +
    799    3*LSNUM.  */
    800 
    801 #define LGOT_REG_NDX(x) ((x) + symtab_hdr->sh_info)
    802 #define LGOT_DTP_NDX(x) ((x) + 2 * symtab_hdr->sh_info)
    803 #define LGOT_TPREL_NDX(x) ((x) + 3 * symtab_hdr->sh_info)
    804 #define LGOT_ALLOC_NELTS_FOR(x) ((x) * 4)
    805 
    806 /* CRIS ELF linker hash table.  */
    807 
    808 struct elf_cris_link_hash_table
    809 {
    810   struct elf_link_hash_table root;
    811 
    812   /* We can't use the PLT offset and calculate to get the GOTPLT offset,
    813      since we try and avoid creating GOTPLT:s when there's already a GOT.
    814      Instead, we keep and update the next available index here.  */
    815   bfd_size_type next_gotplt_entry;
    816 
    817   /* The number of R_CRIS_32_DTPREL and R_CRIS_16_DTPREL that have
    818      been seen for any input; if != 0, then the constant-offset
    819      R_CRIS_DTPMOD is needed for this DSO/executable.  This turns
    820      negative at relocation, so that we don't need an extra flag for
    821      when the reloc is output.  */
    822   bfd_signed_vma dtpmod_refcount;
    823 };
    824 
    825 /* Traverse a CRIS ELF linker hash table.  */
    826 
    827 #define elf_cris_link_hash_traverse(table, func, info)			\
    828   (elf_link_hash_traverse						\
    829    (&(table)->root,							\
    830     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
    831     (info)))
    832 
    833 /* Get the CRIS ELF linker hash table from a link_info structure.  */
    834 
    835 #define elf_cris_hash_table(p) \
    836   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    837   == CRIS_ELF_DATA ? ((struct elf_cris_link_hash_table *) ((p)->hash)) : NULL)
    838 
    839 /* Get the CRIS ELF linker hash entry from a regular hash entry (the
    840    "parent class").  The .root reference is just a simple type
    841    check on the argument.  */
    842 
    843 #define elf_cris_hash_entry(p) \
    844  ((struct elf_cris_link_hash_entry *) (&(p)->root))
    845 
    846 /* Create an entry in a CRIS ELF linker hash table.  */
    847 
    848 static struct bfd_hash_entry *
    849 elf_cris_link_hash_newfunc (struct bfd_hash_entry *entry,
    850 			    struct bfd_hash_table *table,
    851 			    const char *string)
    852 {
    853   struct elf_cris_link_hash_entry *ret =
    854     (struct elf_cris_link_hash_entry *) entry;
    855 
    856   /* Allocate the structure if it has not already been allocated by a
    857      subclass.  */
    858   if (ret == (struct elf_cris_link_hash_entry *) NULL)
    859     ret = ((struct elf_cris_link_hash_entry *)
    860 	   bfd_hash_allocate (table,
    861 			      sizeof (struct elf_cris_link_hash_entry)));
    862   if (ret == (struct elf_cris_link_hash_entry *) NULL)
    863     return (struct bfd_hash_entry *) ret;
    864 
    865   /* Call the allocation method of the superclass.  */
    866   ret = ((struct elf_cris_link_hash_entry *)
    867 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
    868 				     table, string));
    869   if (ret != (struct elf_cris_link_hash_entry *) NULL)
    870     {
    871       ret->pcrel_relocs_copied = NULL;
    872       ret->gotplt_refcount = 0;
    873       ret->gotplt_offset = 0;
    874       ret->dtp_refcount = 0;
    875       ret->tprel_refcount = 0;
    876       ret->reg_got_refcount = 0;
    877     }
    878 
    879   return (struct bfd_hash_entry *) ret;
    880 }
    881 
    882 /* Create a CRIS ELF linker hash table.  */
    883 
    884 static struct bfd_link_hash_table *
    885 elf_cris_link_hash_table_create (bfd *abfd)
    886 {
    887   struct elf_cris_link_hash_table *ret;
    888   bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
    889 
    890   ret = ((struct elf_cris_link_hash_table *) bfd_zmalloc (amt));
    891   if (ret == (struct elf_cris_link_hash_table *) NULL)
    892     return NULL;
    893 
    894   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
    895 				      elf_cris_link_hash_newfunc,
    896 				      sizeof (struct elf_cris_link_hash_entry),
    897 				      CRIS_ELF_DATA))
    898     {
    899       free (ret);
    900       return NULL;
    901     }
    902 
    903   /* Initialize to skip over the first three entries in the gotplt; they
    904      are used for run-time symbol evaluation.  */
    905   ret->next_gotplt_entry = 12;
    906 
    907   return &ret->root.root;
    908 }
    909 
    910 /* Perform a single relocation.  By default we use the standard BFD
    912    routines, with a few tweaks.  */
    913 
    914 static bfd_reloc_status_type
    915 cris_final_link_relocate (reloc_howto_type *  howto,
    916 			  bfd *               input_bfd,
    917 			  asection *          input_section,
    918 			  bfd_byte *          contents,
    919 			  Elf_Internal_Rela * rel,
    920 			  bfd_vma             relocation)
    921 {
    922   bfd_reloc_status_type r;
    923   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
    924 
    925   /* PC-relative relocations are relative to the position *after*
    926      the reloc.  Note that for R_CRIS_8_PCREL the adjustment is
    927      not a single byte, since PC must be 16-bit-aligned.  */
    928   switch (r_type)
    929     {
    930       /* Check that the 16-bit GOT relocs are positive.  */
    931     case R_CRIS_16_GOTPLT:
    932     case R_CRIS_16_GOT:
    933       if ((bfd_signed_vma) relocation < 0)
    934 	return bfd_reloc_overflow;
    935       break;
    936 
    937     case R_CRIS_32_PLT_PCREL:
    938     case R_CRIS_32_PCREL:
    939       relocation -= 2;
    940       /* Fall through.  */
    941     case R_CRIS_8_PCREL:
    942     case R_CRIS_16_PCREL:
    943       relocation -= 2;
    944       break;
    945 
    946     default:
    947       break;
    948     }
    949 
    950   r = _bfd_final_link_relocate (howto, input_bfd, input_section,
    951 				contents, rel->r_offset,
    952 				relocation, rel->r_addend);
    953   return r;
    954 }
    955 
    956 
    958 /* The number of errors left before we stop outputting reloc-specific
    959    explanatory messages.  By coincidence, this works nicely together
    960    with the default number of messages you'll get from LD about
    961    "relocation truncated to fit" messages before you get an
    962    "additional relocation overflows omitted from the output".  */
    963 static int additional_relocation_error_msg_count = 10;
    964 
    965 /* Relocate an CRIS ELF section.  See elf32-fr30.c, from where this was
    966    copied, for further comments.  */
    967 
    968 static bfd_boolean
    969 cris_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
    970 			   struct bfd_link_info *info,
    971 			   bfd *input_bfd,
    972 			   asection *input_section,
    973 			   bfd_byte *contents,
    974 			   Elf_Internal_Rela *relocs,
    975 			   Elf_Internal_Sym *local_syms,
    976 			   asection **local_sections)
    977 {
    978   struct elf_cris_link_hash_table * htab;
    979   bfd *dynobj;
    980   Elf_Internal_Shdr *symtab_hdr;
    981   struct elf_link_hash_entry **sym_hashes;
    982   bfd_vma *local_got_offsets;
    983   asection *sgot;
    984   asection *splt;
    985   asection *sreloc;
    986   Elf_Internal_Rela *rel;
    987   Elf_Internal_Rela *relend;
    988   asection *srelgot;
    989 
    990   htab = elf_cris_hash_table (info);
    991   if (htab == NULL)
    992     return FALSE;
    993 
    994   dynobj = elf_hash_table (info)->dynobj;
    995   local_got_offsets = elf_local_got_offsets (input_bfd);
    996   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
    997   sym_hashes = elf_sym_hashes (input_bfd);
    998   relend     = relocs + input_section->reloc_count;
    999 
   1000   sgot = NULL;
   1001   splt = NULL;
   1002   sreloc = NULL;
   1003   srelgot = NULL;
   1004 
   1005   if (dynobj != NULL)
   1006     {
   1007       splt = bfd_get_linker_section (dynobj, ".plt");
   1008       sgot = bfd_get_linker_section (dynobj, ".got");
   1009     }
   1010 
   1011   for (rel = relocs; rel < relend; rel ++)
   1012     {
   1013       reloc_howto_type *howto;
   1014       unsigned long r_symndx;
   1015       Elf_Internal_Sym *sym;
   1016       asection *sec;
   1017       struct elf_link_hash_entry *h;
   1018       bfd_vma relocation;
   1019       bfd_reloc_status_type r;
   1020       const char *symname = NULL;
   1021       enum elf_cris_reloc_type r_type;
   1022 
   1023       r_type = ELF32_R_TYPE (rel->r_info);
   1024 
   1025       if (   r_type == R_CRIS_GNU_VTINHERIT
   1026 	  || r_type == R_CRIS_GNU_VTENTRY)
   1027 	continue;
   1028 
   1029       r_symndx = ELF32_R_SYM (rel->r_info);
   1030       howto  = cris_elf_howto_table + r_type;
   1031       h      = NULL;
   1032       sym    = NULL;
   1033       sec    = NULL;
   1034 
   1035       if (r_symndx < symtab_hdr->sh_info)
   1036 	{
   1037 	  sym = local_syms + r_symndx;
   1038 	  sec = local_sections [r_symndx];
   1039 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   1040 
   1041 	  symname = (bfd_elf_string_from_elf_section
   1042 		     (input_bfd, symtab_hdr->sh_link, sym->st_name));
   1043 	  if (symname == NULL)
   1044 	    symname = bfd_section_name (input_bfd, sec);
   1045 	}
   1046       else
   1047 	{
   1048 	  bfd_boolean warned, ignored;
   1049 	  bfd_boolean unresolved_reloc;
   1050 
   1051 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   1052 				   r_symndx, symtab_hdr, sym_hashes,
   1053 				   h, sec, relocation,
   1054 				   unresolved_reloc, warned, ignored);
   1055 
   1056 	  symname = h->root.root.string;
   1057 
   1058 	  if (unresolved_reloc
   1059 	      /* Perhaps we should detect the cases that
   1060 		 sec->output_section is expected to be NULL like i386 and
   1061 		 m68k, but apparently (and according to elfxx-ia64.c) all
   1062 		 valid cases are where the symbol is defined in a shared
   1063 		 object which we link dynamically against.  This includes
   1064 		 PLT relocs for which we've created a PLT entry and other
   1065 		 relocs for which we're prepared to create dynamic
   1066 		 relocations.
   1067 
   1068 		 For now, new situations cause us to just err when
   1069 		 sec->output_offset is NULL but the object with the symbol
   1070 		 is *not* dynamically linked against.  Thus this will
   1071 		 automatically remind us so we can see if there are other
   1072 		 valid cases we need to revisit.  */
   1073 	      && (sec->owner->flags & DYNAMIC) != 0)
   1074 	    relocation = 0;
   1075 
   1076 	  else if (h->root.type == bfd_link_hash_defined
   1077 		   || h->root.type == bfd_link_hash_defweak)
   1078 	    {
   1079 	      /* Here follow the cases where the relocation value must
   1080 		 be zero (or when further handling is simplified when
   1081 		 zero).  I can't claim to understand the various
   1082 		 conditions and they weren't described in the files
   1083 		 where I copied them from (elf32-m68k.c and
   1084 		 elf32-i386.c), but let's mention examples of where
   1085 		 they happen.  FIXME: Perhaps define and use a
   1086 		 dynamic_symbol_p function like ia64.
   1087 
   1088 		 - When creating a shared library, we can have an
   1089 		 ordinary relocation for a symbol defined in a shared
   1090 		 library (perhaps the one we create).  We then make
   1091 		 the relocation value zero, as the value seen now will
   1092 		 be added into the relocation addend in this shared
   1093 		 library, but must be handled only at dynamic-link
   1094 		 time.  FIXME: Not sure this example covers the
   1095 		 h->elf_link_hash_flags test, though it's there in
   1096 		 other targets.  */
   1097 	      if (info->shared
   1098 		  && ((! info->symbolic && h->dynindx != -1)
   1099 		      || !h->def_regular)
   1100 		  && (input_section->flags & SEC_ALLOC) != 0
   1101 		  && (r_type == R_CRIS_8
   1102 		      || r_type == R_CRIS_16
   1103 		      || r_type == R_CRIS_32
   1104 		      || r_type == R_CRIS_8_PCREL
   1105 		      || r_type == R_CRIS_16_PCREL
   1106 		      || r_type == R_CRIS_32_PCREL))
   1107 		relocation = 0;
   1108 	      else if (!info->relocatable && unresolved_reloc
   1109 		       && (_bfd_elf_section_offset (output_bfd, info,
   1110 						    input_section,
   1111 						    rel->r_offset)
   1112 			   != (bfd_vma) -1))
   1113 		{
   1114 		  _bfd_error_handler
   1115 		    (_("%B, section %A: unresolvable relocation %s against symbol `%s'"),
   1116 		     input_bfd,
   1117 		     input_section,
   1118 		     cris_elf_howto_table[r_type].name,
   1119 		     symname);
   1120 		  bfd_set_error (bfd_error_bad_value);
   1121 		  return FALSE;
   1122 		}
   1123 	    }
   1124 	}
   1125 
   1126       if (sec != NULL && discarded_section (sec))
   1127 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   1128 					 rel, 1, relend, howto, 0, contents);
   1129 
   1130       if (info->relocatable)
   1131 	continue;
   1132 
   1133       switch (r_type)
   1134 	{
   1135 	case R_CRIS_16_GOTPLT:
   1136 	case R_CRIS_32_GOTPLT:
   1137 	  /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
   1138 	     but we require a PLT, and the PLT handling will take care of
   1139 	     filling in the PLT-specific GOT entry.  For the GOT offset,
   1140 	     calculate it as we do when filling it in for the .got.plt
   1141 	     section.  If we don't have a PLT, punt to GOT handling.  */
   1142 	  if (h != NULL
   1143 	      && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
   1144 	    {
   1145 	      asection *sgotplt
   1146 		= bfd_get_linker_section (dynobj, ".got.plt");
   1147 	      bfd_vma got_offset;
   1148 
   1149 	      BFD_ASSERT (h->dynindx != -1);
   1150 	      BFD_ASSERT (sgotplt != NULL);
   1151 
   1152 	      got_offset
   1153 		= ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
   1154 
   1155 	      relocation = got_offset;
   1156 	      break;
   1157 	    }
   1158 
   1159 	  /* We didn't make a PLT entry for this symbol.  Maybe everything is
   1160 	     folded into the GOT.  Other than folding, this happens when
   1161 	     statically linking PIC code, or when using -Bsymbolic.  Check
   1162 	     that we instead have a GOT entry as done for us by
   1163 	     elf_cris_adjust_dynamic_symbol, and drop through into the
   1164 	     ordinary GOT cases.  This must not happen for the
   1165 	     executable, because any reference it does to a function
   1166 	     that is satisfied by a DSO must generate a PLT.  We assume
   1167 	     these call-specific relocs don't address non-functions.  */
   1168 	  if (h != NULL
   1169 	      && (h->got.offset == (bfd_vma) -1
   1170 		  || (!info->shared
   1171 		      && !(h->def_regular
   1172 			   || (!h->def_dynamic
   1173 			       && h->root.type == bfd_link_hash_undefweak)))))
   1174 	    {
   1175 	      (*_bfd_error_handler)
   1176 		((h->got.offset == (bfd_vma) -1)
   1177 		 ? _("%B, section %A: No PLT nor GOT for relocation %s"
   1178 		     " against symbol `%s'")
   1179 		 : _("%B, section %A: No PLT for relocation %s"
   1180 		     " against symbol `%s'"),
   1181 		 input_bfd,
   1182 		 input_section,
   1183 		 cris_elf_howto_table[r_type].name,
   1184 		 (symname != NULL && symname[0] != '\0'
   1185 		  ? symname : _("[whose name is lost]")));
   1186 
   1187 	      /* FIXME: Perhaps blaming input is not the right thing to
   1188 		 do; this is probably an internal error.  But it is true
   1189 		 that we didn't like that particular input.  */
   1190 	      bfd_set_error (bfd_error_bad_value);
   1191 	      return FALSE;
   1192 	    }
   1193 	  /* Fall through.  */
   1194 
   1195 	  /* The size of the actual relocation is not used here; we only
   1196 	     fill in the GOT table here.  */
   1197 	case R_CRIS_16_GOT:
   1198 	case R_CRIS_32_GOT:
   1199 	  {
   1200 	    bfd_vma off;
   1201 
   1202 	    /* Note that despite using RELA relocations, the .got contents
   1203 	       is always filled in with the link-relative relocation
   1204 	       value; the addend.  */
   1205 
   1206 	    if (h != NULL)
   1207 	      {
   1208 		off = h->got.offset;
   1209 		BFD_ASSERT (off != (bfd_vma) -1);
   1210 
   1211 		if (!elf_hash_table (info)->dynamic_sections_created
   1212 		    || (! info->shared
   1213 			&& (h->def_regular
   1214 			    || h->type == STT_FUNC
   1215 			    || h->needs_plt))
   1216 		    || (info->shared
   1217 			&& (info->symbolic || h->dynindx == -1)
   1218 			&& h->def_regular))
   1219 		  {
   1220 		    /* This wasn't checked above for ! info->shared, but
   1221 		       must hold there if we get here; the symbol must
   1222 		       be defined in the regular program or be undefweak
   1223 		       or be a function or otherwise need a PLT.  */
   1224 		    BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
   1225 				|| info->shared
   1226 				|| h->def_regular
   1227 				|| h->type == STT_FUNC
   1228 				|| h->needs_plt
   1229 				|| h->root.type == bfd_link_hash_undefweak);
   1230 
   1231 		    /* This is actually a static link, or it is a
   1232 		       -Bsymbolic link and the symbol is defined locally,
   1233 		       or is undefweak, or the symbol was forced to be
   1234 		       local because of a version file, or we're not
   1235 		       creating a dynamic object.  We must initialize this
   1236 		       entry in the global offset table.  Since the offset
   1237 		       must always be a multiple of 4, we use the least
   1238 		       significant bit to record whether we have
   1239 		       initialized it already.
   1240 
   1241 		       If this GOT entry should be runtime-initialized, we
   1242 		       will create a .rela.got relocation entry to
   1243 		       initialize the value.  This is done in the
   1244 		       finish_dynamic_symbol routine.  */
   1245 		    if ((off & 1) != 0)
   1246 		      off &= ~1;
   1247 		    else
   1248 		      {
   1249 			bfd_put_32 (output_bfd, relocation,
   1250 				    sgot->contents + off);
   1251 			h->got.offset |= 1;
   1252 		      }
   1253 		  }
   1254 	      }
   1255 	    else
   1256 	      {
   1257 		BFD_ASSERT (local_got_offsets != NULL
   1258 			    && local_got_offsets[r_symndx] != (bfd_vma) -1);
   1259 
   1260 		off = local_got_offsets[r_symndx];
   1261 
   1262 		/* The offset must always be a multiple of 4.  We use
   1263 		   the least significant bit to record whether we have
   1264 		   already generated the necessary reloc.  */
   1265 		if ((off & 1) != 0)
   1266 		  off &= ~1;
   1267 		else
   1268 		  {
   1269 		    bfd_put_32 (output_bfd, relocation, sgot->contents + off);
   1270 
   1271 		    if (info->shared)
   1272 		      {
   1273 			Elf_Internal_Rela outrel;
   1274 			bfd_byte *loc;
   1275 
   1276 			if (srelgot == NULL)
   1277 			  srelgot
   1278 			    = bfd_get_linker_section (dynobj, ".rela.got");
   1279 			BFD_ASSERT (srelgot != NULL);
   1280 
   1281 			outrel.r_offset = (sgot->output_section->vma
   1282 					   + sgot->output_offset
   1283 					   + off);
   1284 			outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
   1285 			outrel.r_addend = relocation;
   1286 			loc = srelgot->contents;
   1287 			loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
   1288 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   1289 		      }
   1290 
   1291 		    local_got_offsets[r_symndx] |= 1;
   1292 		  }
   1293 	      }
   1294 
   1295 	    relocation = sgot->output_offset + off;
   1296 	    if (rel->r_addend != 0)
   1297 	      {
   1298 		/* We can't do anything for a relocation which is against
   1299 		   a symbol *plus offset*.  GOT holds relocations for
   1300 		   symbols.  Make this an error; the compiler isn't
   1301 		   allowed to pass us these kinds of things.  */
   1302 		if (h == NULL)
   1303 		  (*_bfd_error_handler)
   1304 		    (_("%B, section %A: relocation %s with non-zero addend %d"
   1305 		       " against local symbol"),
   1306 		     input_bfd,
   1307 		     input_section,
   1308 		     cris_elf_howto_table[r_type].name,
   1309 		     rel->r_addend);
   1310 		else
   1311 		  (*_bfd_error_handler)
   1312 		    (_("%B, section %A: relocation %s with non-zero addend %d"
   1313 		       " against symbol `%s'"),
   1314 		     input_bfd,
   1315 		     input_section,
   1316 		     cris_elf_howto_table[r_type].name,
   1317 		     rel->r_addend,
   1318 		     symname[0] != '\0' ? symname : _("[whose name is lost]"));
   1319 
   1320 		bfd_set_error (bfd_error_bad_value);
   1321 		return FALSE;
   1322 	      }
   1323 	  }
   1324 	  break;
   1325 
   1326 	case R_CRIS_32_GOTREL:
   1327 	  /* This relocation must only be performed against local symbols.
   1328 	     It's also ok when we link a program and the symbol is either
   1329 	     defined in an ordinary (non-DSO) object or is undefined weak.  */
   1330 	  if (h != NULL
   1331 	      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   1332 	      && !(!info->shared
   1333 		   && (h->def_regular
   1334 		       || (!h->def_dynamic
   1335 			   && h->root.type == bfd_link_hash_undefweak))))
   1336 	    {
   1337 	      (*_bfd_error_handler)
   1338 		(_("%B, section %A: relocation %s is"
   1339 		   " not allowed for global symbol: `%s'"),
   1340 		 input_bfd,
   1341 		 input_section,
   1342 		 cris_elf_howto_table[r_type].name,
   1343 		 symname);
   1344 	      bfd_set_error (bfd_error_bad_value);
   1345 	      return FALSE;
   1346 	    }
   1347 
   1348 	  /* This can happen if we get a link error with the input ELF
   1349 	     variant mismatching the output variant.  Emit an error so
   1350 	     it's noticed if it happens elsewhere.  */
   1351 	  if (sgot == NULL)
   1352 	    {
   1353 	      (*_bfd_error_handler)
   1354 		(_("%B, section %A: relocation %s with no GOT created"),
   1355 		 input_bfd,
   1356 		 input_section,
   1357 		 cris_elf_howto_table[r_type].name);
   1358 	      bfd_set_error (bfd_error_bad_value);
   1359 	      return FALSE;
   1360 	    }
   1361 
   1362 	  /* This relocation is like a PC-relative one, except the
   1363 	     reference point is the location of GOT.  Note that
   1364 	     sgot->output_offset is not involved in this calculation.  We
   1365 	     always want the start of entire .got section, not the
   1366 	     position after the reserved header.  */
   1367 	  relocation -= sgot->output_section->vma;
   1368 	  break;
   1369 
   1370 	case R_CRIS_32_PLT_PCREL:
   1371 	  /* Relocation is to the entry for this symbol in the
   1372 	     procedure linkage table.  */
   1373 
   1374 	  /* Resolve a PLT_PCREL reloc against a local symbol directly,
   1375 	     without using the procedure linkage table.  */
   1376 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   1377 	    break;
   1378 
   1379 	  if (h->plt.offset == (bfd_vma) -1
   1380 	      || splt == NULL)
   1381 	    {
   1382 	      /* We didn't make a PLT entry for this symbol.  This
   1383 		 happens when statically linking PIC code, or when
   1384 		 using -Bsymbolic.  */
   1385 	      break;
   1386 	    }
   1387 
   1388 	  relocation = (splt->output_section->vma
   1389 			+ splt->output_offset
   1390 			+ h->plt.offset);
   1391 	  break;
   1392 
   1393 	case R_CRIS_32_PLT_GOTREL:
   1394 	  /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
   1395 	     start of the .got section.  See also comment at
   1396 	     R_CRIS_32_GOT.  */
   1397 	  relocation -= sgot->output_section->vma;
   1398 
   1399 	  /* Resolve a PLT_GOTREL reloc against a local symbol directly,
   1400 	     without using the procedure linkage table.  */
   1401 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   1402 	    break;
   1403 
   1404 	  if (h->plt.offset == (bfd_vma) -1
   1405 	      || splt == NULL)
   1406 	    {
   1407 	      /* We didn't make a PLT entry for this symbol.  This
   1408 		 happens when statically linking PIC code, or when
   1409 		 using -Bsymbolic.  */
   1410 	      break;
   1411 	    }
   1412 
   1413 	  relocation = (splt->output_section->vma
   1414 			+ splt->output_offset
   1415 			+ h->plt.offset
   1416 			- sgot->output_section->vma);
   1417 	  break;
   1418 
   1419 	case R_CRIS_8_PCREL:
   1420 	case R_CRIS_16_PCREL:
   1421 	case R_CRIS_32_PCREL:
   1422 	  /* If the symbol was local, we need no shlib-specific handling.  */
   1423 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   1424 	      || h->dynindx == -1)
   1425 	    break;
   1426 
   1427 	  /* Fall through.  */
   1428 	case R_CRIS_8:
   1429 	case R_CRIS_16:
   1430 	case R_CRIS_32:
   1431 	  if (info->shared
   1432 	      && r_symndx != STN_UNDEF
   1433 	      && (input_section->flags & SEC_ALLOC) != 0
   1434 	      && ((r_type != R_CRIS_8_PCREL
   1435 		   && r_type != R_CRIS_16_PCREL
   1436 		   && r_type != R_CRIS_32_PCREL)
   1437 		  || (!info->symbolic
   1438 		      || (h != NULL && !h->def_regular))))
   1439 	    {
   1440 	      Elf_Internal_Rela outrel;
   1441 	      bfd_byte *loc;
   1442 	      bfd_boolean skip, relocate;
   1443 
   1444 	      /* When generating a shared object, these relocations
   1445 		 are copied into the output file to be resolved at run
   1446 		 time.  */
   1447 
   1448 	      if (sreloc == NULL)
   1449 		{
   1450 		  sreloc = _bfd_elf_get_dynamic_reloc_section
   1451 		    (dynobj, input_section, /*rela?*/ TRUE);
   1452 		  /* The section should have been created in cris_elf_check_relocs,
   1453 		     but that function will not be called for objects which fail in
   1454 		     cris_elf_merge_private_bfd_data.  */
   1455 		  if (sreloc == NULL)
   1456 		    {
   1457 		      bfd_set_error (bfd_error_bad_value);
   1458 		      return FALSE;
   1459 		    }
   1460 		}
   1461 
   1462 	      skip = FALSE;
   1463 	      relocate = FALSE;
   1464 
   1465 	      outrel.r_offset =
   1466 		_bfd_elf_section_offset (output_bfd, info, input_section,
   1467 					 rel->r_offset);
   1468 	      if (outrel.r_offset == (bfd_vma) -1)
   1469 		skip = TRUE;
   1470 	      else if (outrel.r_offset == (bfd_vma) -2
   1471 		       /* For now, undefined weak symbols with non-default
   1472 			  visibility (yielding 0), like exception info for
   1473 			  discarded sections, will get a R_CRIS_NONE
   1474 			  relocation rather than no relocation, because we
   1475 			  notice too late that the symbol doesn't need a
   1476 			  relocation.  */
   1477 		       || (h != NULL
   1478 			   && h->root.type == bfd_link_hash_undefweak
   1479 			   && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))
   1480 		skip = TRUE, relocate = TRUE;
   1481 	      outrel.r_offset += (input_section->output_section->vma
   1482 				  + input_section->output_offset);
   1483 
   1484 	      if (skip)
   1485 		memset (&outrel, 0, sizeof outrel);
   1486 	      /* h->dynindx may be -1 if the symbol was marked to
   1487 		 become local.  */
   1488 	      else if (h != NULL
   1489 		       && ((! info->symbolic && h->dynindx != -1)
   1490 			   || !h->def_regular))
   1491 		{
   1492 		  BFD_ASSERT (h->dynindx != -1);
   1493 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
   1494 		  outrel.r_addend = relocation + rel->r_addend;
   1495 		}
   1496 	      else
   1497 		{
   1498 		  outrel.r_addend = relocation + rel->r_addend;
   1499 
   1500 		  if (r_type == R_CRIS_32)
   1501 		    {
   1502 		      relocate = TRUE;
   1503 		      outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
   1504 		    }
   1505 		  else
   1506 		    {
   1507 		      long indx;
   1508 
   1509 		      if (bfd_is_abs_section (sec))
   1510 			indx = 0;
   1511 		      else if (sec == NULL || sec->owner == NULL)
   1512 			{
   1513 			  bfd_set_error (bfd_error_bad_value);
   1514 			  return FALSE;
   1515 			}
   1516 		      else
   1517 			{
   1518 			  asection *osec;
   1519 
   1520 			  /* We are turning this relocation into one
   1521 			     against a section symbol.  It would be
   1522 			     proper to subtract the symbol's value,
   1523 			     osec->vma, from the emitted reloc addend,
   1524 			     but ld.so expects buggy relocs.  */
   1525 			  osec = sec->output_section;
   1526 			  indx = elf_section_data (osec)->dynindx;
   1527 			  if (indx == 0)
   1528 			    {
   1529 			      osec = htab->root.text_index_section;
   1530 			      indx = elf_section_data (osec)->dynindx;
   1531 			    }
   1532 			  BFD_ASSERT (indx != 0);
   1533 			}
   1534 
   1535 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
   1536 		    }
   1537 		}
   1538 
   1539 	      loc = sreloc->contents;
   1540 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
   1541 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   1542 
   1543 	      /* This reloc will be computed at runtime, so there's no
   1544                  need to do anything now, except for R_CRIS_32 relocations
   1545                  that have been turned into R_CRIS_RELATIVE.  */
   1546 	      if (!relocate)
   1547 		continue;
   1548 	    }
   1549 
   1550 	  break;
   1551 
   1552 	case R_CRIS_16_DTPREL:
   1553 	case R_CRIS_32_DTPREL:
   1554 	  /* This relocation must only be performed against local
   1555 	     symbols, or to sections that are not loadable.  It's also
   1556 	     ok when we link a program and the symbol is defined in an
   1557 	     ordinary (non-DSO) object (if it's undefined there, we've
   1558 	     already seen an error).  */
   1559 	  if (h != NULL
   1560 	      && (input_section->flags & SEC_ALLOC) != 0
   1561 	      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   1562 	      && (info->shared
   1563 		  || (!h->def_regular
   1564 		      && h->root.type != bfd_link_hash_undefined)))
   1565 	    {
   1566 	      (*_bfd_error_handler)
   1567 		((h->root.type == bfd_link_hash_undefined)
   1568 		 /* We shouldn't get here for GCC-emitted code.  */
   1569 		 ? _("%B, section %A: relocation %s has an undefined"
   1570 		     " reference to `%s', perhaps a declaration mixup?")
   1571 		 : ("%B, section %A: relocation %s is"
   1572 		     " not allowed for `%s', a global symbol with default"
   1573 		     " visibility, perhaps a declaration mixup?"),
   1574 		 input_bfd,
   1575 		 input_section,
   1576 		 cris_elf_howto_table[r_type].name,
   1577 		 symname != NULL && symname[0] != '\0'
   1578 		 ? symname : _("[whose name is lost]"));
   1579 	      bfd_set_error (bfd_error_bad_value);
   1580 	      return FALSE;
   1581 	    }
   1582 
   1583 	  BFD_ASSERT ((input_section->flags & SEC_ALLOC) == 0
   1584 		      || htab->dtpmod_refcount != 0);
   1585 
   1586 	  /* Fill in a R_CRIS_DTPMOD reloc at offset 3 if we haven't
   1587 	     already done so.  Note that we do this in .got.plt, not
   1588 	     in .got, as .got.plt contains the first part, still the
   1589 	     reloc is against .got, because the linker script directs
   1590 	     (is required to direct) them both into .got.  */
   1591 	  if (htab->dtpmod_refcount > 0
   1592 	      && (input_section->flags & SEC_ALLOC) != 0)
   1593 	    {
   1594 	      asection *sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
   1595 	      BFD_ASSERT (sgotplt != NULL);
   1596 
   1597 	      if (info->shared)
   1598 		{
   1599 		  Elf_Internal_Rela outrel;
   1600 		  bfd_byte *loc;
   1601 
   1602 		  if (srelgot == NULL)
   1603 		    srelgot = bfd_get_linker_section (dynobj, ".rela.got");
   1604 		  BFD_ASSERT (srelgot != NULL);
   1605 		  loc = srelgot->contents;
   1606 		  loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
   1607 
   1608 		  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 12);
   1609 		  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
   1610 		  outrel.r_offset = (sgotplt->output_section->vma
   1611 				     + sgotplt->output_offset
   1612 				     + 12);
   1613 		  outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTPMOD);
   1614 		  outrel.r_addend = 0;
   1615 		  bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   1616 		}
   1617 	      else
   1618 		{
   1619 		  /* For an executable, the GOT entry contents is known.  */
   1620 		  bfd_put_32 (output_bfd, (bfd_vma) 1, sgotplt->contents + 12);
   1621 		  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
   1622 		}
   1623 
   1624 	      /* Reverse the sign to mark that we've emitted the
   1625 		 required GOT entry.  */
   1626 	      htab->dtpmod_refcount = - htab->dtpmod_refcount;
   1627 	    }
   1628 
   1629 	  /* The relocation is the offset from the start of the module
   1630 	     TLS block to the (local) symbol.  */
   1631 	  relocation -= elf_hash_table (info)->tls_sec == NULL
   1632 	    ? 0 : elf_hash_table (info)->tls_sec->vma;
   1633 	  break;
   1634 
   1635 	case R_CRIS_32_GD:
   1636 	  if (info->shared)
   1637 	    {
   1638 	      bfd_set_error (bfd_error_invalid_operation);
   1639 
   1640 	      /* We've already informed in cris_elf_check_relocs that
   1641 		 this is an error.  */
   1642 	      return FALSE;
   1643 	    }
   1644 	  /* Fall through.  */
   1645 
   1646 	case R_CRIS_16_GOT_GD:
   1647 	case R_CRIS_32_GOT_GD:
   1648 	  if (rel->r_addend != 0)
   1649 	    {
   1650 	      /* We can't do anything for a relocation which is against a
   1651 		 symbol *plus offset*.  The GOT holds relocations for
   1652 		 symbols.  Make this an error; the compiler isn't allowed
   1653 		 to pass us these kinds of things.  */
   1654 	      (*_bfd_error_handler)
   1655 		(_("%B, section %A: relocation %s with non-zero addend %d"
   1656 		   " against symbol `%s'"),
   1657 		 input_bfd,
   1658 		 input_section,
   1659 		 cris_elf_howto_table[r_type].name,
   1660 		 rel->r_addend,
   1661 		 symname[0] != '\0' ? symname : _("[whose name is lost]"));
   1662 
   1663 	      bfd_set_error (bfd_error_bad_value);
   1664 	      return FALSE;
   1665 	    }
   1666 
   1667 	  if (!info->shared
   1668 	      && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
   1669 	    {
   1670 	      /* Known contents of the GOT.  */
   1671 	      bfd_vma off;
   1672 
   1673 	      /* The symbol is defined in the program, so just write
   1674 		 (1, known_tpoffset) into the GOT.  */
   1675 	      relocation -= elf_hash_table (info)->tls_sec->vma;
   1676 
   1677 	      if (h != NULL)
   1678 		{
   1679 		  off = elf_cris_hash_entry (h)->tprel_refcount > 0
   1680 		    ? h->got.offset + 4 : h->got.offset;
   1681 		}
   1682 	      else
   1683 		{
   1684 		  off = local_got_offsets[r_symndx];
   1685 		  if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
   1686 		    off += 4;
   1687 		}
   1688 
   1689 	      /* We use bit 1 of the offset as a flag for GOT entry with
   1690 		 the R_CRIS_DTP reloc, setting it when we've emitted the
   1691 		 GOT entry and reloc.  Bit 0 is used for R_CRIS_32_TPREL
   1692 		 relocs.  */
   1693 	      if ((off & 2) == 0)
   1694 		{
   1695 		  off &= ~3;
   1696 
   1697 		  if (h != NULL)
   1698 		    h->got.offset |= 2;
   1699 		  else
   1700 		    local_got_offsets[r_symndx] |= 2;
   1701 
   1702 		  bfd_put_32 (output_bfd, 1, sgot->contents + off);
   1703 		  bfd_put_32 (output_bfd, relocation, sgot->contents + off + 4);
   1704 		}
   1705 	      else
   1706 		off &= ~3;
   1707 
   1708 	      relocation = sgot->output_offset + off
   1709 		+ (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
   1710 	    }
   1711 	  else
   1712 	    {
   1713 	      /* Not all parts of the GOT entry are known; emit a real
   1714 		 relocation.  */
   1715 	      bfd_vma off;
   1716 
   1717 	      if (h != NULL)
   1718 		off = elf_cris_hash_entry (h)->tprel_refcount > 0
   1719 		  ? h->got.offset + 4 : h->got.offset;
   1720 	      else
   1721 		{
   1722 		  off = local_got_offsets[r_symndx];
   1723 		  if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
   1724 		    off += 4;
   1725 		}
   1726 
   1727 	      /* See above re bit 1 and bit 0 usage.  */
   1728 	      if ((off & 2) == 0)
   1729 		{
   1730 		  Elf_Internal_Rela outrel;
   1731 		  bfd_byte *loc;
   1732 
   1733 		  off &= ~3;
   1734 
   1735 		  if (h != NULL)
   1736 		    h->got.offset |= 2;
   1737 		  else
   1738 		    local_got_offsets[r_symndx] |= 2;
   1739 
   1740 		  /* Clear the target contents of the GOT (just as a
   1741 		     gesture; it's already cleared on allocation): this
   1742 		     relocation is not like the other dynrelocs.  */
   1743 		  bfd_put_32 (output_bfd, 0, sgot->contents + off);
   1744 		  bfd_put_32 (output_bfd, 0, sgot->contents + off + 4);
   1745 
   1746 		  if (srelgot == NULL)
   1747 		    srelgot = bfd_get_linker_section (dynobj, ".rela.got");
   1748 		  BFD_ASSERT (srelgot != NULL);
   1749 
   1750 		  if (h != NULL && h->dynindx != -1)
   1751 		    {
   1752 		      outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_DTP);
   1753 		      relocation = 0;
   1754 		    }
   1755 		  else
   1756 		    {
   1757 		      outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTP);
   1758 
   1759 		      /* NULL if we had an error.  */
   1760 		      relocation -= elf_hash_table (info)->tls_sec == NULL
   1761 			? 0 : elf_hash_table (info)->tls_sec->vma;
   1762 		    }
   1763 
   1764 		  outrel.r_offset = (sgot->output_section->vma
   1765 				     + sgot->output_offset
   1766 				     + off);
   1767 		  outrel.r_addend = relocation;
   1768 		  loc = srelgot->contents;
   1769 		  loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
   1770 
   1771 		  /* NULL if we had an error.  */
   1772 		  if (srelgot->contents != NULL)
   1773 		    bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   1774 		}
   1775 	      else
   1776 		off &= ~3;
   1777 
   1778 	      relocation = sgot->output_offset + off
   1779 		+ (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
   1780 	    }
   1781 
   1782 	  /* The GOT-relative offset to the GOT entry is the
   1783 	     relocation, or for R_CRIS_32_GD, the actual address of
   1784 	     the GOT entry.  */
   1785 	  break;
   1786 
   1787 	case R_CRIS_32_IE:
   1788 	  if (info->shared)
   1789 	    {
   1790 	      bfd_set_error (bfd_error_invalid_operation);
   1791 
   1792 	      /* We've already informed in cris_elf_check_relocs that
   1793 		 this is an error.  */
   1794 	      return FALSE;
   1795 	    }
   1796 	  /* Fall through.  */
   1797 
   1798 	case R_CRIS_32_GOT_TPREL:
   1799 	case R_CRIS_16_GOT_TPREL:
   1800 	  if (rel->r_addend != 0)
   1801 	    {
   1802 	      /* We can't do anything for a relocation which is
   1803 		 against a symbol *plus offset*.  GOT holds
   1804 		 relocations for symbols.  Make this an error; the
   1805 		 compiler isn't allowed to pass us these kinds of
   1806 		 things.  */
   1807 	      (*_bfd_error_handler)
   1808 		(_("%B, section %A: relocation %s with non-zero addend %d"
   1809 		   " against symbol `%s'"),
   1810 		 input_bfd,
   1811 		 input_section,
   1812 		 cris_elf_howto_table[r_type].name,
   1813 		 rel->r_addend,
   1814 		 symname[0] != '\0' ? symname : _("[whose name is lost]"));
   1815 	      bfd_set_error (bfd_error_bad_value);
   1816 	      return FALSE;
   1817 	    }
   1818 
   1819 	  if (!info->shared
   1820 	      && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
   1821 	    {
   1822 	      /* Known contents of the GOT.  */
   1823 	      bfd_vma off;
   1824 
   1825 	      /* The symbol is defined in the program, so just write
   1826 		 the -prog_tls_size+known_tpoffset into the GOT.  */
   1827 	      relocation -= elf_hash_table (info)->tls_sec->vma;
   1828 	      relocation -= elf_hash_table (info)->tls_size;
   1829 
   1830 	      if (h != NULL)
   1831 		off = h->got.offset;
   1832 	      else
   1833 		off = local_got_offsets[r_symndx];
   1834 
   1835 	      /* Bit 0 is used to mark whether we've emitted the required
   1836 		 entry (and if needed R_CRIS_32_TPREL reloc).  Bit 1
   1837 		 is used similarly for R_CRIS_DTP, see above.  */
   1838 	      if ((off & 1) == 0)
   1839 		{
   1840 		  off &= ~3;
   1841 
   1842 		  if (h != NULL)
   1843 		    h->got.offset |= 1;
   1844 		  else
   1845 		    local_got_offsets[r_symndx] |= 1;
   1846 
   1847 		  bfd_put_32 (output_bfd, relocation, sgot->contents + off);
   1848 		}
   1849 	      else
   1850 		off &= ~3;
   1851 
   1852 	      relocation = sgot->output_offset + off
   1853 		+ (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
   1854 	    }
   1855 	  else
   1856 	    {
   1857 	      /* Emit a real relocation.  */
   1858 	      bfd_vma off;
   1859 
   1860 	      if (h != NULL)
   1861 		off = h->got.offset;
   1862 	      else
   1863 		off = local_got_offsets[r_symndx];
   1864 
   1865 	      /* See above re usage of bit 0 and 1.  */
   1866 	      if ((off & 1) == 0)
   1867 		{
   1868 		  Elf_Internal_Rela outrel;
   1869 		  bfd_byte *loc;
   1870 
   1871 		  off &= ~3;
   1872 
   1873 		  if (h != NULL)
   1874 		    h->got.offset |= 1;
   1875 		  else
   1876 		    local_got_offsets[r_symndx] |= 1;
   1877 
   1878 		  if (srelgot == NULL)
   1879 		    srelgot = bfd_get_linker_section (dynobj, ".rela.got");
   1880 		  BFD_ASSERT (srelgot != NULL);
   1881 
   1882 		  if (h != NULL && h->dynindx != -1)
   1883 		    {
   1884 		      outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_32_TPREL);
   1885 		      relocation = 0;
   1886 		    }
   1887 		  else
   1888 		    {
   1889 		      outrel.r_info = ELF32_R_INFO (0, R_CRIS_32_TPREL);
   1890 
   1891 		      /* NULL if we had an error.  */
   1892 		      relocation -= elf_hash_table (info)->tls_sec == NULL
   1893 			? 0 : elf_hash_table (info)->tls_sec->vma;
   1894 		    }
   1895 
   1896 		  /* Just "define" the initial contents in some
   1897 		     semi-logical way.  */
   1898 		  bfd_put_32 (output_bfd, relocation, sgot->contents + off);
   1899 
   1900 		  outrel.r_offset = (sgot->output_section->vma
   1901 				     + sgot->output_offset
   1902 				     + off);
   1903 		  outrel.r_addend = relocation;
   1904 		  loc = srelgot->contents;
   1905 		  loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
   1906 		  /* NULL if we had an error.  */
   1907 		  if (srelgot->contents != NULL)
   1908 		    bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   1909 		}
   1910 	      else
   1911 		off &= ~3;
   1912 
   1913 	      relocation = sgot->output_offset + off
   1914 		+ (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
   1915 	    }
   1916 
   1917 	  /* The GOT-relative offset to the GOT entry is the relocation,
   1918 	     or for R_CRIS_32_GD, the actual address of the GOT entry.  */
   1919 	  break;
   1920 
   1921 	case R_CRIS_16_TPREL:
   1922 	case R_CRIS_32_TPREL:
   1923 	  /* This relocation must only be performed against symbols
   1924 	     defined in an ordinary (non-DSO) object.  */
   1925 	  if (info->shared)
   1926 	    {
   1927 	      bfd_set_error (bfd_error_invalid_operation);
   1928 
   1929 	      /* We've already informed in cris_elf_check_relocs that
   1930 		 this is an error.  */
   1931 	      return FALSE;
   1932 	    }
   1933 
   1934 	  if (h != NULL
   1935 	      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   1936 	      && !(h->def_regular || ELF_COMMON_DEF_P (h))
   1937 	      /* If it's undefined, then an error message has already
   1938 		 been emitted.  */
   1939 	      && h->root.type != bfd_link_hash_undefined)
   1940 	    {
   1941 	      (*_bfd_error_handler)
   1942 		(_("%B, section %A: relocation %s is"
   1943 		   " not allowed for symbol: `%s'"
   1944 		   " which is defined outside the program,"
   1945 		   " perhaps a declaration mixup?"),
   1946 		 input_bfd,
   1947 		 input_section,
   1948 		 cris_elf_howto_table[r_type].name,
   1949 		 symname);
   1950 	      bfd_set_error (bfd_error_bad_value);
   1951 	      return FALSE;
   1952 	    }
   1953 
   1954 	  /* NULL if we had an error.  */
   1955 	  relocation -= elf_hash_table (info)->tls_sec == NULL
   1956 	    ? 0
   1957 	    : (elf_hash_table (info)->tls_sec->vma
   1958 	       + elf_hash_table (info)->tls_size);
   1959 
   1960 	  /* The TLS-relative offset is the relocation.  */
   1961 	  break;
   1962 
   1963 	default:
   1964 	  BFD_FAIL ();
   1965 	  return FALSE;
   1966 	}
   1967 
   1968       r = cris_final_link_relocate (howto, input_bfd, input_section,
   1969 				     contents, rel, relocation);
   1970 
   1971       if (r != bfd_reloc_ok)
   1972 	{
   1973 	  const char * msg = (const char *) NULL;
   1974 
   1975 	  switch (r)
   1976 	    {
   1977 	    case bfd_reloc_overflow:
   1978 	      r = info->callbacks->reloc_overflow
   1979 		(info, (h ? &h->root : NULL), symname, howto->name,
   1980 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
   1981 	      if (additional_relocation_error_msg_count > 0)
   1982 		{
   1983 		  additional_relocation_error_msg_count--;
   1984 		  switch (r_type)
   1985 		    {
   1986 		    case R_CRIS_16_GOTPLT:
   1987 		    case R_CRIS_16_GOT:
   1988 
   1989 		      /* Not just TLS is involved here, so we make
   1990 			 generation and message depend on -fPIC/-fpic
   1991 			 only.  */
   1992 		    case R_CRIS_16_GOT_TPREL:
   1993 		    case R_CRIS_16_GOT_GD:
   1994 		      (*_bfd_error_handler)
   1995 			(_("(too many global variables for -fpic:"
   1996 			   " recompile with -fPIC)"));
   1997 		      break;
   1998 
   1999 		    case R_CRIS_16_TPREL:
   2000 		    case R_CRIS_16_DTPREL:
   2001 		      (*_bfd_error_handler)
   2002 			(_("(thread-local data too big for -fpic or"
   2003 			   " -msmall-tls: recompile with -fPIC or"
   2004 			   " -mno-small-tls)"));
   2005 		      break;
   2006 
   2007 		      /* No known cause for overflow for other relocs.  */
   2008 		    default:
   2009 		      break;
   2010 		    }
   2011 		}
   2012 	      break;
   2013 
   2014 	    case bfd_reloc_undefined:
   2015 	      r = info->callbacks->undefined_symbol
   2016 		(info, symname, input_bfd, input_section, rel->r_offset,
   2017 		 TRUE);
   2018 	      break;
   2019 
   2020 	    case bfd_reloc_outofrange:
   2021 	      msg = _("internal error: out of range error");
   2022 	      break;
   2023 
   2024 	    case bfd_reloc_notsupported:
   2025 	      msg = _("internal error: unsupported relocation error");
   2026 	      break;
   2027 
   2028 	    case bfd_reloc_dangerous:
   2029 	      msg = _("internal error: dangerous relocation");
   2030 	      break;
   2031 
   2032 	    default:
   2033 	      msg = _("internal error: unknown error");
   2034 	      break;
   2035 	    }
   2036 
   2037 	  if (msg)
   2038 	    r = info->callbacks->warning
   2039 	      (info, msg, symname, input_bfd, input_section, rel->r_offset);
   2040 
   2041 	  if (! r)
   2042 	    return FALSE;
   2043 	}
   2044     }
   2045 
   2046   return TRUE;
   2047 }
   2048 
   2049 /* Finish up dynamic symbol handling.  We set the contents of various
   2051    dynamic sections here.  */
   2052 
   2053 static bfd_boolean
   2054 elf_cris_finish_dynamic_symbol (bfd *output_bfd,
   2055 				struct bfd_link_info *info,
   2056 				struct elf_link_hash_entry *h,
   2057 				Elf_Internal_Sym *sym)
   2058 {
   2059   struct elf_cris_link_hash_table * htab;
   2060   bfd *dynobj;
   2061 
   2062   /* Where in the plt entry to put values.  */
   2063   int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
   2064 
   2065   /* What offset to add to the distance to the first PLT entry for the
   2066      value at plt_off3.   */
   2067   int plt_off3_value_bias = 4;
   2068 
   2069   /* Where in the PLT entry the call-dynlink-stub is (happens to be same
   2070      for PIC and non-PIC for v32 and pre-v32).  */
   2071   int plt_stub_offset = 8;
   2072   int plt_entry_size = PLT_ENTRY_SIZE;
   2073   const bfd_byte *plt_entry = elf_cris_plt_entry;
   2074   const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
   2075 
   2076   htab = elf_cris_hash_table (info);
   2077   if (htab == NULL)
   2078     return FALSE;
   2079 
   2080   /* Adjust the various PLT entry offsets.  */
   2081   if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
   2082     {
   2083       plt_off2 = 14;
   2084       plt_off3 = 20;
   2085       plt_off3_value_bias = -2;
   2086       plt_stub_offset = 12;
   2087       plt_entry_size = PLT_ENTRY_SIZE_V32;
   2088       plt_entry = elf_cris_plt_entry_v32;
   2089       plt_pic_entry = elf_cris_pic_plt_entry_v32;
   2090     }
   2091 
   2092   dynobj = elf_hash_table (info)->dynobj;
   2093 
   2094   if (h->plt.offset != (bfd_vma) -1)
   2095     {
   2096       asection *splt;
   2097       asection *sgotplt;
   2098       asection *srela;
   2099       bfd_vma got_base;
   2100 
   2101       bfd_vma gotplt_offset
   2102 	= elf_cris_hash_entry (h)->gotplt_offset;
   2103       Elf_Internal_Rela rela;
   2104       bfd_byte *loc;
   2105       bfd_boolean has_gotplt = gotplt_offset != 0;
   2106 
   2107       /* Get the index in the .rela.plt relocations for the .got.plt
   2108 	 entry that corresponds to this symbol.
   2109 	 We have to count backwards here, and the result is only valid
   2110 	 as an index into .rela.plt.  We also have to undo the effect
   2111 	 of the R_CRIS_DTPMOD entry at .got index 3 (offset 12 into
   2112 	 .got.plt) for which gotplt_offset is adjusted, because while
   2113 	 that entry goes into .got.plt, its relocation goes into
   2114 	 .rela.got, not .rela.plt.  (It's not PLT-specific; not to be
   2115 	 processed as part of the runtime lazy .rela.plt relocation).
   2116 	 FIXME: There be literal constants here...  */
   2117       bfd_vma rela_plt_index
   2118 	= (htab->dtpmod_refcount != 0
   2119 	   ? gotplt_offset/4 - 2 - 3 : gotplt_offset/4 - 3);
   2120 
   2121       /* Get the offset into the .got table of the entry that corresponds
   2122 	 to this function.  Note that we embed knowledge that "incoming"
   2123 	 .got goes after .got.plt in the output without padding (pointer
   2124 	 aligned).  However, that knowledge is present in several other
   2125 	 places too.  */
   2126       bfd_vma got_offset
   2127 	= (has_gotplt
   2128 	   ? gotplt_offset
   2129 	   : h->got.offset + htab->next_gotplt_entry);
   2130 
   2131       /* This symbol has an entry in the procedure linkage table.  Set it
   2132 	 up.  */
   2133 
   2134       BFD_ASSERT (h->dynindx != -1);
   2135 
   2136       splt = bfd_get_linker_section (dynobj, ".plt");
   2137       sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
   2138       srela = bfd_get_linker_section (dynobj, ".rela.plt");
   2139       BFD_ASSERT (splt != NULL && sgotplt != NULL
   2140 		  && (! has_gotplt || srela != NULL));
   2141 
   2142       got_base = sgotplt->output_section->vma + sgotplt->output_offset;
   2143 
   2144       /* Fill in the entry in the procedure linkage table.  */
   2145       if (! info->shared)
   2146 	{
   2147 	  memcpy (splt->contents + h->plt.offset, plt_entry,
   2148 		  plt_entry_size);
   2149 
   2150 	  /* We need to enter the absolute address of the GOT entry here.  */
   2151 	  bfd_put_32 (output_bfd, got_base + got_offset,
   2152 		      splt->contents + h->plt.offset + plt_off1);
   2153 	}
   2154       else
   2155 	{
   2156 	  memcpy (splt->contents + h->plt.offset, plt_pic_entry,
   2157 		  plt_entry_size);
   2158 	  bfd_put_32 (output_bfd, got_offset,
   2159 		      splt->contents + h->plt.offset + plt_off1);
   2160 	}
   2161 
   2162       /* Fill in the plt entry and make a relocation, if this is a "real"
   2163 	 PLT entry.  */
   2164       if (has_gotplt)
   2165 	{
   2166 	  /* Fill in the offset to the reloc table.  */
   2167 	  bfd_put_32 (output_bfd,
   2168 		      rela_plt_index * sizeof (Elf32_External_Rela),
   2169 		      splt->contents + h->plt.offset + plt_off2);
   2170 
   2171 	  /* Fill in the offset to the first PLT entry, where to "jump".  */
   2172 	  bfd_put_32 (output_bfd,
   2173 		      - (h->plt.offset + plt_off3 + plt_off3_value_bias),
   2174 		      splt->contents + h->plt.offset + plt_off3);
   2175 
   2176 	  /* Fill in the entry in the global offset table with the address of
   2177 	     the relocating stub.  */
   2178 	  bfd_put_32 (output_bfd,
   2179 		      (splt->output_section->vma
   2180 		       + splt->output_offset
   2181 		       + h->plt.offset
   2182 		       + plt_stub_offset),
   2183 		      sgotplt->contents + got_offset);
   2184 
   2185 	  /* Fill in the entry in the .rela.plt section.  */
   2186 	  rela.r_offset = (sgotplt->output_section->vma
   2187 			   + sgotplt->output_offset
   2188 			   + got_offset);
   2189 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
   2190 	  rela.r_addend = 0;
   2191 	  loc = srela->contents + rela_plt_index * sizeof (Elf32_External_Rela);
   2192 	  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   2193 	}
   2194 
   2195       if (!h->def_regular)
   2196 	{
   2197 	  /* Mark the symbol as undefined, rather than as defined in
   2198 	     the .plt section.  Leave the value alone.  */
   2199 	  sym->st_shndx = SHN_UNDEF;
   2200 
   2201 	  /* FIXME: From elf32-sparc.c 2001-02-19 (1.18).  I still don't
   2202 	     know whether resetting the value is significant; if it really
   2203 	     is, rather than a quirk or bug in the sparc port, then I
   2204 	     believe we'd see this elsewhere.  */
   2205 	  /* If the symbol is weak, we do need to clear the value.
   2206 	     Otherwise, the PLT entry would provide a definition for
   2207 	     the symbol even if the symbol wasn't defined anywhere,
   2208 	     and so the symbol would never be NULL.  */
   2209 	  if (!h->ref_regular_nonweak)
   2210 	    sym->st_value = 0;
   2211 	}
   2212     }
   2213 
   2214   /* For an ordinary program, we emit .got relocs only for symbols that
   2215      are in the dynamic-symbols table and are either defined by the
   2216      program or are undefined weak symbols, or are function symbols
   2217      where we do not output a PLT: the PLT reloc was output above and all
   2218      references to the function symbol are redirected to the PLT.  */
   2219   if (h->got.offset != (bfd_vma) -1
   2220       && (elf_cris_hash_entry (h)->reg_got_refcount > 0)
   2221       && (info->shared
   2222 	  || (h->dynindx != -1
   2223 	      && h->plt.offset == (bfd_vma) -1
   2224 	      && !h->def_regular
   2225 	      && h->root.type != bfd_link_hash_undefweak)))
   2226     {
   2227       asection *sgot;
   2228       asection *srela;
   2229       Elf_Internal_Rela rela;
   2230       bfd_byte *loc;
   2231       bfd_byte *where;
   2232 
   2233       /* This symbol has an entry in the global offset table.  Set it up.  */
   2234 
   2235       sgot = bfd_get_linker_section (dynobj, ".got");
   2236       srela = bfd_get_linker_section (dynobj, ".rela.got");
   2237       BFD_ASSERT (sgot != NULL && srela != NULL);
   2238 
   2239       rela.r_offset = (sgot->output_section->vma
   2240 		       + sgot->output_offset
   2241 		       + (h->got.offset &~ (bfd_vma) 1));
   2242 
   2243       /* If this is a static link, or it is a -Bsymbolic link and the
   2244 	 symbol is defined locally or was forced to be local because
   2245 	 of a version file, we just want to emit a RELATIVE reloc.
   2246 	 The entry in the global offset table will already have been
   2247 	 initialized in the relocate_section function.  */
   2248       where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
   2249       if (! elf_hash_table (info)->dynamic_sections_created
   2250 	  || (info->shared
   2251 	      && (info->symbolic || h->dynindx == -1)
   2252 	      && h->def_regular))
   2253 	{
   2254 	  rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
   2255 	  rela.r_addend = bfd_get_signed_32 (output_bfd, where);
   2256 	}
   2257       else
   2258 	{
   2259 	  bfd_put_32 (output_bfd, (bfd_vma) 0, where);
   2260 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
   2261 	  rela.r_addend = 0;
   2262 	}
   2263 
   2264       loc = srela->contents;
   2265       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
   2266       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   2267     }
   2268 
   2269   if (h->needs_copy)
   2270     {
   2271       asection *s;
   2272       Elf_Internal_Rela rela;
   2273       bfd_byte *loc;
   2274 
   2275       /* This symbol needs a copy reloc.  Set it up.  */
   2276 
   2277       BFD_ASSERT (h->dynindx != -1
   2278 		  && (h->root.type == bfd_link_hash_defined
   2279 		      || h->root.type == bfd_link_hash_defweak));
   2280 
   2281       s = bfd_get_linker_section (dynobj, ".rela.bss");
   2282       BFD_ASSERT (s != NULL);
   2283 
   2284       rela.r_offset = (h->root.u.def.value
   2285 		       + h->root.u.def.section->output_section->vma
   2286 		       + h->root.u.def.section->output_offset);
   2287       rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
   2288       rela.r_addend = 0;
   2289       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
   2290       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   2291     }
   2292 
   2293   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
   2294   if (h == elf_hash_table (info)->hdynamic
   2295       || h == elf_hash_table (info)->hgot)
   2296     sym->st_shndx = SHN_ABS;
   2297 
   2298   return TRUE;
   2299 }
   2300 
   2301 /* Finish up the dynamic sections.  Do *not* emit relocs here, as their
   2303    offsets were changed, as part of -z combreloc handling, from those we
   2304    computed.  */
   2305 
   2306 static bfd_boolean
   2307 elf_cris_finish_dynamic_sections (bfd *output_bfd,
   2308 				  struct bfd_link_info *info)
   2309 {
   2310   bfd *dynobj;
   2311   asection *sgot;
   2312   asection *sdyn;
   2313 
   2314   dynobj = elf_hash_table (info)->dynobj;
   2315 
   2316   sgot = bfd_get_linker_section (dynobj, ".got.plt");
   2317   BFD_ASSERT (sgot != NULL);
   2318   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   2319 
   2320   if (elf_hash_table (info)->dynamic_sections_created)
   2321     {
   2322       asection *splt;
   2323       Elf32_External_Dyn *dyncon, *dynconend;
   2324 
   2325       splt = bfd_get_linker_section (dynobj, ".plt");
   2326       BFD_ASSERT (splt != NULL && sdyn != NULL);
   2327 
   2328       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   2329       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   2330       for (; dyncon < dynconend; dyncon++)
   2331 	{
   2332 	  Elf_Internal_Dyn dyn;
   2333 	  asection *s;
   2334 
   2335 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   2336 
   2337 	  switch (dyn.d_tag)
   2338 	    {
   2339 	    default:
   2340 	      break;
   2341 
   2342 	    case DT_PLTGOT:
   2343 	      s = bfd_get_section_by_name (output_bfd, ".got");
   2344 	      BFD_ASSERT (s != NULL);
   2345 	      dyn.d_un.d_ptr = s->vma;
   2346 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   2347 	      break;
   2348 
   2349 	    case DT_JMPREL:
   2350 	      /* Yes, we *can* have a .plt and no .plt.rela, for instance
   2351 		 if all symbols are found in the .got (not .got.plt).  */
   2352 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
   2353 	      dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
   2354 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   2355 	      break;
   2356 
   2357 	    case DT_PLTRELSZ:
   2358 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
   2359 	      if (s == NULL)
   2360 		dyn.d_un.d_val = 0;
   2361 	      else
   2362 		dyn.d_un.d_val = s->size;
   2363 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   2364 	      break;
   2365 
   2366 	    case DT_RELASZ:
   2367 	      /* The procedure linkage table relocs (DT_JMPREL) should
   2368 		 not be included in the overall relocs (DT_RELA).
   2369 		 Therefore, we override the DT_RELASZ entry here to
   2370 		 make it not include the JMPREL relocs.  Since the
   2371 		 linker script arranges for .rela.plt to follow all
   2372 		 other relocation sections, we don't have to worry
   2373 		 about changing the DT_RELA entry.  */
   2374 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
   2375 	      if (s != NULL)
   2376 		dyn.d_un.d_val -= s->size;
   2377 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   2378 	      break;
   2379 	    }
   2380 	}
   2381 
   2382       /* Fill in the first entry in the procedure linkage table.  */
   2383       if (splt->size > 0)
   2384 	{
   2385 	  if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
   2386 	    {
   2387 	      if (info->shared)
   2388 		memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
   2389 			PLT_ENTRY_SIZE_V32);
   2390 	      else
   2391 		{
   2392 		  memcpy (splt->contents, elf_cris_plt0_entry_v32,
   2393 			  PLT_ENTRY_SIZE_V32);
   2394 		  bfd_put_32 (output_bfd,
   2395 			      sgot->output_section->vma
   2396 			      + sgot->output_offset + 4,
   2397 			      splt->contents + 4);
   2398 
   2399 		  elf_section_data (splt->output_section)->this_hdr.sh_entsize
   2400 		    = PLT_ENTRY_SIZE_V32;
   2401 		}
   2402 	    }
   2403 	  else
   2404 	    {
   2405 	      if (info->shared)
   2406 		memcpy (splt->contents, elf_cris_pic_plt0_entry,
   2407 			PLT_ENTRY_SIZE);
   2408 	      else
   2409 		{
   2410 		  memcpy (splt->contents, elf_cris_plt0_entry,
   2411 			  PLT_ENTRY_SIZE);
   2412 		  bfd_put_32 (output_bfd,
   2413 			      sgot->output_section->vma
   2414 			      + sgot->output_offset + 4,
   2415 			      splt->contents + 6);
   2416 		  bfd_put_32 (output_bfd,
   2417 			      sgot->output_section->vma
   2418 			      + sgot->output_offset + 8,
   2419 			      splt->contents + 14);
   2420 
   2421 		  elf_section_data (splt->output_section)->this_hdr.sh_entsize
   2422 		    = PLT_ENTRY_SIZE;
   2423 		}
   2424             }
   2425 	}
   2426     }
   2427 
   2428   /* Fill in the first three entries in the global offset table.  */
   2429   if (sgot->size > 0)
   2430     {
   2431       if (sdyn == NULL)
   2432 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
   2433       else
   2434 	bfd_put_32 (output_bfd,
   2435 		    sdyn->output_section->vma + sdyn->output_offset,
   2436 		    sgot->contents);
   2437       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
   2438       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
   2439     }
   2440 
   2441   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
   2442 
   2443   return TRUE;
   2444 }
   2445 
   2446 /* Return the section that should be marked against GC for a given
   2448    relocation.  */
   2449 
   2450 static asection *
   2451 cris_elf_gc_mark_hook (asection *sec,
   2452 		       struct bfd_link_info *info,
   2453 		       Elf_Internal_Rela *rel,
   2454 		       struct elf_link_hash_entry *h,
   2455 		       Elf_Internal_Sym *sym)
   2456 {
   2457   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
   2458   if (h != NULL)
   2459     switch (r_type)
   2460       {
   2461       case R_CRIS_GNU_VTINHERIT:
   2462       case R_CRIS_GNU_VTENTRY:
   2463 	return NULL;
   2464 
   2465       default:
   2466 	break;
   2467       }
   2468 
   2469   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   2470 }
   2471 
   2472 /* Update the got entry reference counts for the section being removed.  */
   2473 
   2474 static bfd_boolean
   2475 cris_elf_gc_sweep_hook (bfd *abfd,
   2476 			struct bfd_link_info *info,
   2477 			asection *sec,
   2478 			const Elf_Internal_Rela *relocs)
   2479 {
   2480   struct elf_cris_link_hash_table * htab;
   2481   Elf_Internal_Shdr *symtab_hdr;
   2482   struct elf_link_hash_entry **sym_hashes;
   2483   bfd_signed_vma *local_got_refcounts;
   2484   const Elf_Internal_Rela *rel, *relend;
   2485   bfd *dynobj;
   2486   asection *sgot;
   2487   asection *srelgot;
   2488 
   2489   if (info->relocatable)
   2490     return TRUE;
   2491 
   2492   dynobj = elf_hash_table (info)->dynobj;
   2493   if (dynobj == NULL)
   2494     return TRUE;
   2495 
   2496   htab = elf_cris_hash_table (info);
   2497   if (htab == NULL)
   2498     return FALSE;
   2499 
   2500   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2501   sym_hashes = elf_sym_hashes (abfd);
   2502   local_got_refcounts = elf_local_got_refcounts (abfd);
   2503 
   2504   sgot = bfd_get_linker_section (dynobj, ".got");
   2505   srelgot = bfd_get_linker_section (dynobj, ".rela.got");
   2506 
   2507   relend = relocs + sec->reloc_count;
   2508   for (rel = relocs; rel < relend; rel++)
   2509     {
   2510       unsigned long r_symndx;
   2511       struct elf_link_hash_entry *h = NULL;
   2512       bfd_signed_vma got_element_size = 4;
   2513       bfd_signed_vma *specific_refcount = NULL;
   2514       enum elf_cris_reloc_type r_type;
   2515 
   2516       r_symndx = ELF32_R_SYM (rel->r_info);
   2517       if (r_symndx >= symtab_hdr->sh_info)
   2518 	{
   2519 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   2520 	  while (h->root.type == bfd_link_hash_indirect
   2521 		 || h->root.type == bfd_link_hash_warning)
   2522 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2523 	}
   2524 
   2525       r_type = ELF32_R_TYPE (rel->r_info);
   2526       switch (r_type)
   2527 	{
   2528 	case R_CRIS_32_GOT:
   2529 	case R_CRIS_16_GOT:
   2530 	case R_CRIS_16_GOTPLT:
   2531 	case R_CRIS_32_GOTPLT:
   2532 	  specific_refcount = h != NULL
   2533 	    ? &((struct elf_cris_link_hash_entry *) h)->reg_got_refcount
   2534 	    : &local_got_refcounts[LGOT_REG_NDX (r_symndx)];
   2535 	  break;
   2536 
   2537 	case R_CRIS_32_GD:
   2538 	case R_CRIS_32_GOT_GD:
   2539 	case R_CRIS_16_GOT_GD:
   2540 	  got_element_size = 8;
   2541 	  specific_refcount = h != NULL
   2542 	    ? &((struct elf_cris_link_hash_entry *) h)->dtp_refcount
   2543 	    : &local_got_refcounts[LGOT_DTP_NDX (r_symndx)];
   2544 	  break;
   2545 
   2546 	case R_CRIS_32_IE:
   2547 	case R_CRIS_16_GOT_TPREL:
   2548 	case R_CRIS_32_GOT_TPREL:
   2549 	  specific_refcount = h != NULL
   2550 	    ? &((struct elf_cris_link_hash_entry *) h)->tprel_refcount
   2551 	    : &local_got_refcounts[LGOT_TPREL_NDX (r_symndx)];
   2552 	  break;
   2553 
   2554 	default:
   2555 	  break;
   2556 	}
   2557 
   2558       switch (r_type)
   2559 	{
   2560 	case R_CRIS_32_IE:
   2561 	case R_CRIS_32_GD:
   2562 	case R_CRIS_16_GOT_TPREL:
   2563 	case R_CRIS_32_GOT_TPREL:
   2564 	case R_CRIS_32_GOT_GD:
   2565 	case R_CRIS_16_GOT_GD:
   2566 	case R_CRIS_16_GOT:
   2567 	case R_CRIS_32_GOT:
   2568 	  if (h != NULL)
   2569 	    {
   2570 	      /* If the counters are 0 when we got here, we've
   2571 		 miscounted somehow somewhere, an internal error.  */
   2572 	      BFD_ASSERT (h->got.refcount > 0);
   2573 	      --h->got.refcount;
   2574 
   2575 	      BFD_ASSERT (*specific_refcount > 0);
   2576 	      --*specific_refcount;
   2577 	      if (*specific_refcount == 0)
   2578 		{
   2579 		  /* We don't need the .got entry any more.  */
   2580 		  sgot->size -= got_element_size;
   2581 		  srelgot->size -= sizeof (Elf32_External_Rela);
   2582 		}
   2583 	      break;
   2584 	    }
   2585 
   2586 	local_got_reloc:
   2587 	  if (local_got_refcounts != NULL)
   2588 	    {
   2589 	      /* If the counters are 0 when we got here, we've
   2590 		 miscounted somehow somewhere, an internal error.  */
   2591 	      BFD_ASSERT (local_got_refcounts[r_symndx] > 0);
   2592 	      --local_got_refcounts[r_symndx];
   2593 
   2594 	      BFD_ASSERT (*specific_refcount > 0);
   2595 	      --*specific_refcount;
   2596 	      if (*specific_refcount == 0)
   2597 		{
   2598 		  /* We don't need the .got entry any more.  */
   2599 		  sgot->size -= got_element_size;
   2600 		  if (info->shared)
   2601 		    srelgot->size -= sizeof (Elf32_External_Rela);
   2602 		}
   2603 	    }
   2604 	  break;
   2605 
   2606 	case R_CRIS_16_GOTPLT:
   2607 	case R_CRIS_32_GOTPLT:
   2608 	  /* For local symbols, treat these like GOT relocs.  */
   2609 	  if (h == NULL)
   2610 	    goto local_got_reloc;
   2611 	  else
   2612 	    /* For global symbols, adjust the reloc-specific refcount.  */
   2613 	    elf_cris_hash_entry (h)->gotplt_refcount--;
   2614 	  /* Fall through.  */
   2615 
   2616 	case R_CRIS_32_PLT_GOTREL:
   2617 	  /* FIXME: We don't garbage-collect away the .got section.  */
   2618 	  if (local_got_refcounts != NULL)
   2619 	    local_got_refcounts[-1]--;
   2620 	  /* Fall through.  */
   2621 
   2622 	case R_CRIS_8:
   2623 	case R_CRIS_16:
   2624 	case R_CRIS_32:
   2625 	case R_CRIS_8_PCREL:
   2626 	case R_CRIS_16_PCREL:
   2627 	case R_CRIS_32_PCREL:
   2628 	case R_CRIS_32_PLT_PCREL:
   2629 	  /* Negate the increment we did in cris_elf_check_relocs.  */
   2630 	  if (h != NULL)
   2631 	    {
   2632 	      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2633 		  && h->plt.refcount > 0)
   2634 		--h->plt.refcount;
   2635 	    }
   2636 	  break;
   2637 
   2638 	case R_CRIS_32_DTPREL:
   2639 	  /* This'd be a .dtpreld entry in e.g. debug info.  */
   2640 	  if ((sec->flags & SEC_ALLOC) == 0)
   2641 	    break;
   2642 	  /* Fall through.  */
   2643 	case R_CRIS_16_DTPREL:
   2644 	  htab->dtpmod_refcount--;
   2645 	  if (htab->dtpmod_refcount == 0)
   2646 	    htab->next_gotplt_entry -= 8;
   2647 	  BFD_ASSERT (local_got_refcounts != NULL);
   2648 	  local_got_refcounts[-1]--;
   2649 	  break;
   2650 
   2651 	default:
   2652 	  break;
   2653 	}
   2654     }
   2655 
   2656   return TRUE;
   2657 }
   2658 
   2659 /* The elf_backend_plt_sym_val hook function.  */
   2660 
   2661 static bfd_vma
   2662 cris_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED, const asection *plt,
   2663 		      const arelent *rel)
   2664 {
   2665   bfd_size_type plt_entry_size;
   2666   bfd_size_type pltoffs;
   2667   bfd *abfd = plt->owner;
   2668 
   2669   /* Same for CRIS and CRIS v32; see elf_cris_(|pic_)plt_entry(|_v32)[].  */
   2670   bfd_size_type plt_entry_got_offset = 2;
   2671   bfd_size_type plt_sec_size;
   2672   bfd_size_type got_vma_for_dyn;
   2673   asection *got;
   2674 
   2675   /* FIXME: the .got section should be readily available also when
   2676      we're not linking.  */
   2677   if ((got = bfd_get_section_by_name (abfd, ".got")) == NULL)
   2678     return (bfd_vma) -1;
   2679 
   2680   plt_sec_size =  bfd_section_size (plt->owner, plt);
   2681   plt_entry_size
   2682     = (bfd_get_mach (abfd) == bfd_mach_cris_v32
   2683        ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
   2684 
   2685   /* Data in PLT is GOT-relative for DYN, but absolute for EXE.  */
   2686   got_vma_for_dyn = (abfd->flags & EXEC_P) ? 0 : got->vma;
   2687 
   2688   /* Because we can have merged GOT entries; a single .got entry for
   2689      both GOT and the PLT part of the GOT (.got.plt), the index of the
   2690      reloc in .rela.plt is not the same as the index in the PLT.
   2691      Instead, we have to hunt down the GOT offset in the PLT that
   2692      corresponds to that of this reloc.  Unfortunately, we will only
   2693      be called for the .rela.plt relocs, so we'll miss synthetic
   2694      symbols for .plt entries with merged GOT entries.  (FIXME:
   2695      fixable by providing our own bfd_elf32_get_synthetic_symtab.
   2696      Doesn't seem worthwile at time of this writing.)  FIXME: we've
   2697      gone from O(1) to O(N) (N number of PLT entries) for finding each
   2698      PLT address.  Shouldn't matter in practice though.  */
   2699 
   2700   for (pltoffs = plt_entry_size;
   2701        pltoffs < plt_sec_size;
   2702        pltoffs += plt_entry_size)
   2703     {
   2704       bfd_size_type got_offset;
   2705       bfd_byte gotoffs_raw[4];
   2706 
   2707       if (!bfd_get_section_contents (abfd, (asection *) plt, gotoffs_raw,
   2708 				     pltoffs + plt_entry_got_offset,
   2709 				     sizeof (gotoffs_raw)))
   2710 	return (bfd_vma) -1;
   2711 
   2712       got_offset = bfd_get_32 (abfd, gotoffs_raw);
   2713       if (got_offset + got_vma_for_dyn == rel->address)
   2714 	return plt->vma + pltoffs;
   2715     }
   2716 
   2717   /* While it's tempting to BFD_ASSERT that we shouldn't get here,
   2718      that'd not be graceful behavior for invalid input.  */
   2719   return (bfd_vma) -1;
   2720 }
   2721 
   2722 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
   2723    entry but we found we will not create any.  Called when we find we will
   2724    not have any PLT for this symbol, by for example
   2725    elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
   2726    or elf_cris_size_dynamic_sections if no dynamic sections will be
   2727    created (we're only linking static objects).  */
   2728 
   2729 static bfd_boolean
   2730 elf_cris_adjust_gotplt_to_got (struct elf_cris_link_hash_entry *h, void * p)
   2731 {
   2732   struct bfd_link_info *info = (struct bfd_link_info *) p;
   2733 
   2734   /* A GOTPLT reloc, when activated, is supposed to be included into
   2735      the PLT refcount.  */
   2736   BFD_ASSERT (h->gotplt_refcount == 0
   2737 	      || h->gotplt_refcount <= h->root.plt.refcount);
   2738 
   2739   /* If nobody wanted a GOTPLT with this symbol, we're done.  */
   2740   if (h->gotplt_refcount <= 0)
   2741     return TRUE;
   2742 
   2743   if (h->reg_got_refcount > 0)
   2744     {
   2745       /* There's a GOT entry for this symbol.  Just adjust the refcounts.
   2746 	 Probably not necessary at this stage, but keeping them accurate
   2747 	 helps avoiding surprises later.  */
   2748       h->root.got.refcount += h->gotplt_refcount;
   2749       h->reg_got_refcount += h->gotplt_refcount;
   2750       h->gotplt_refcount = 0;
   2751     }
   2752   else
   2753     {
   2754       /* No GOT entry for this symbol.  We need to create one.  */
   2755       bfd *dynobj = elf_hash_table (info)->dynobj;
   2756       asection *sgot;
   2757       asection *srelgot;
   2758 
   2759       BFD_ASSERT (dynobj != NULL);
   2760       sgot = bfd_get_linker_section (dynobj, ".got");
   2761       srelgot = bfd_get_linker_section (dynobj, ".rela.got");
   2762 
   2763       /* Put accurate refcounts there.  */
   2764       h->root.got.refcount += h->gotplt_refcount;
   2765       h->reg_got_refcount = h->gotplt_refcount;
   2766 
   2767       h->gotplt_refcount = 0;
   2768 
   2769       /* We always have a .got and a .rela.got section if there were
   2770 	 GOTPLT relocs in input.  */
   2771       BFD_ASSERT (sgot != NULL && srelgot != NULL);
   2772 
   2773       /* Allocate space in the .got section.  */
   2774       sgot->size += 4;
   2775 
   2776       /* Allocate relocation space.  */
   2777       srelgot->size += sizeof (Elf32_External_Rela);
   2778     }
   2779 
   2780   return TRUE;
   2781 }
   2782 
   2783 /* Try to fold PLT entries with GOT entries.  There are two cases when we
   2784    want to do this:
   2785 
   2786    - When all PLT references are GOTPLT references, and there are GOT
   2787      references, and this is not the executable.  We don't have to
   2788      generate a PLT at all.
   2789 
   2790    - When there are both (ordinary) PLT references and GOT references,
   2791      and this isn't the executable.
   2792      We want to make the PLT reference use the ordinary GOT entry rather
   2793      than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
   2794      since the GOT entry will have to be resolved at startup anyway.
   2795 
   2796    Though the latter case is handled when room for the PLT is allocated,
   2797    not here.
   2798 
   2799    By folding into the GOT, we may need a round-trip to a PLT in the
   2800    executable for calls, a loss in performance.  Still, losing a
   2801    reloc is a win in size and at least in start-up time.
   2802 
   2803    Note that this function is called before symbols are forced local by
   2804    version scripts.  The differing cases are handled by
   2805    elf_cris_hide_symbol.  */
   2806 
   2807 static bfd_boolean
   2808 elf_cris_try_fold_plt_to_got (struct elf_cris_link_hash_entry *h, void * p)
   2809 {
   2810   struct bfd_link_info *info = (struct bfd_link_info *) p;
   2811 
   2812   /* If there are no GOT references for this symbol, we can't fold any
   2813      other reference so there's nothing to do.  Likewise if there are no
   2814      PLT references; GOTPLT references included.  */
   2815   if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
   2816     return TRUE;
   2817 
   2818   /* GOTPLT relocs are supposed to be included into the PLT refcount.  */
   2819   BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
   2820 
   2821   if (h->gotplt_refcount == h->root.plt.refcount)
   2822     {
   2823       /* The only PLT references are GOTPLT references, and there are GOT
   2824 	 references.  Convert PLT to GOT references.  */
   2825       if (! elf_cris_adjust_gotplt_to_got (h, info))
   2826 	return FALSE;
   2827 
   2828       /* Clear the PLT references, so no PLT will be created.  */
   2829       h->root.plt.offset = (bfd_vma) -1;
   2830     }
   2831 
   2832   return TRUE;
   2833 }
   2834 
   2835 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
   2836    to use a GOT entry (and create one) rather than requiring a GOTPLT
   2837    entry.  */
   2838 
   2839 static void
   2840 elf_cris_hide_symbol (struct bfd_link_info *info,
   2841 		      struct elf_link_hash_entry *h,
   2842 		      bfd_boolean force_local)
   2843 {
   2844   elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
   2845 
   2846   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   2847 }
   2848 
   2849 /* Adjust a symbol defined by a dynamic object and referenced by a
   2850    regular object.  The current definition is in some section of the
   2851    dynamic object, but we're not including those sections.  We have to
   2852    change the definition to something the rest of the link can
   2853    understand.  */
   2854 
   2855 static bfd_boolean
   2856 elf_cris_adjust_dynamic_symbol (struct bfd_link_info *info,
   2857 				struct elf_link_hash_entry *h)
   2858 {
   2859   struct elf_cris_link_hash_table * htab;
   2860   bfd *dynobj;
   2861   asection *s;
   2862   bfd_size_type plt_entry_size;
   2863 
   2864   htab = elf_cris_hash_table (info);
   2865   if (htab == NULL)
   2866     return FALSE;
   2867 
   2868   dynobj = elf_hash_table (info)->dynobj;
   2869 
   2870   /* Make sure we know what is going on here.  */
   2871   BFD_ASSERT (dynobj != NULL
   2872 	      && (h->needs_plt
   2873 		  || h->u.weakdef != NULL
   2874 		  || (h->def_dynamic
   2875 		      && h->ref_regular
   2876 		      && !h->def_regular)));
   2877 
   2878   plt_entry_size
   2879     = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
   2880        ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
   2881 
   2882   /* If this is a function, put it in the procedure linkage table.  We
   2883      will fill in the contents of the procedure linkage table later,
   2884      when we know the address of the .got section.  */
   2885   if (h->type == STT_FUNC
   2886       || h->needs_plt)
   2887     {
   2888       /* If we link a program (not a DSO), we'll get rid of unnecessary
   2889 	 PLT entries; we point to the actual symbols -- even for pic
   2890 	 relocs, because a program built with -fpic should have the same
   2891 	 result as one built without -fpic, specifically considering weak
   2892 	 symbols.
   2893 	 FIXME: m68k and i386 differ here, for unclear reasons.  */
   2894       if (! info->shared
   2895 	  && !h->def_dynamic)
   2896 	{
   2897 	  /* This case can occur if we saw a PLT reloc in an input file,
   2898 	     but the symbol was not defined by a dynamic object.  In such
   2899 	     a case, we don't actually need to build a procedure linkage
   2900 	     table, and we can just do an absolute or PC reloc instead, or
   2901 	     change a .got.plt index to a .got index for GOTPLT relocs.  */
   2902 	  BFD_ASSERT (h->needs_plt);
   2903 	  h->needs_plt = 0;
   2904 	  h->plt.offset = (bfd_vma) -1;
   2905 	  return
   2906 	    elf_cris_adjust_gotplt_to_got ((struct
   2907 					    elf_cris_link_hash_entry *) h,
   2908 					   info);
   2909 	}
   2910 
   2911       /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
   2912 	 where a pointer-equivalent symbol was unimportant (i.e. more
   2913 	 like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
   2914 	 of the PLT.  We can't for the executable, because the GOT
   2915 	 entries will point to the PLT there (and be constant).  */
   2916       if (info->shared
   2917 	  && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
   2918 					    h, info))
   2919 	return FALSE;
   2920 
   2921       /* GC or folding may have rendered this entry unused.  */
   2922       if (h->plt.refcount <= 0)
   2923 	{
   2924 	  h->needs_plt = 0;
   2925 	  h->plt.offset = (bfd_vma) -1;
   2926 	  return TRUE;
   2927 	}
   2928 
   2929       /* Make sure this symbol is output as a dynamic symbol.  */
   2930       if (h->dynindx == -1)
   2931 	{
   2932 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   2933 	    return FALSE;
   2934 	}
   2935 
   2936       s = bfd_get_linker_section (dynobj, ".plt");
   2937       BFD_ASSERT (s != NULL);
   2938 
   2939       /* If this is the first .plt entry, make room for the special
   2940 	 first entry.  */
   2941       if (s->size == 0)
   2942 	s->size += plt_entry_size;
   2943 
   2944       /* If this symbol is not defined in a regular file, and we are
   2945 	 not generating a shared library, then set the symbol to this
   2946 	 location in the .plt.  */
   2947       if (!info->shared
   2948 	  && !h->def_regular)
   2949 	{
   2950 	  h->root.u.def.section = s;
   2951 	  h->root.u.def.value = s->size;
   2952 	}
   2953 
   2954       /* If there's already a GOT entry, use that, not a .got.plt.  A
   2955 	 GOT field still has a reference count when we get here; it's
   2956 	 not yet changed to an offset.  We can't do this for an
   2957 	 executable, because then the reloc associated with the PLT
   2958 	 would get a non-PLT reloc pointing to the PLT.  FIXME: Move
   2959 	 this to elf_cris_try_fold_plt_to_got.  */
   2960       if (info->shared && h->got.refcount > 0)
   2961 	{
   2962 	  h->got.refcount += h->plt.refcount;
   2963 
   2964 	  /* Mark the PLT offset to use the GOT entry by setting the low
   2965 	     bit in the plt offset; it is always a multiple of
   2966 	     plt_entry_size (which is at least a multiple of 2).  */
   2967 	  BFD_ASSERT ((s->size % plt_entry_size) == 0);
   2968 
   2969 	  /* Change the PLT refcount to an offset.  */
   2970 	  h->plt.offset = s->size;
   2971 
   2972 	  /* By not setting gotplt_offset (i.e. it remains at 0), we signal
   2973 	     that the got entry should be used instead.  */
   2974 	  BFD_ASSERT (((struct elf_cris_link_hash_entry *)
   2975 		       h)->gotplt_offset == 0);
   2976 
   2977 	  /* Make room for this entry.  */
   2978 	  s->size += plt_entry_size;
   2979 
   2980 	  return TRUE;
   2981 	}
   2982 
   2983       /* No GOT reference for this symbol; prepare for an ordinary PLT.  */
   2984       h->plt.offset = s->size;
   2985 
   2986       /* Make room for this entry.  */
   2987       s->size += plt_entry_size;
   2988 
   2989       /* We also need to make an entry in the .got.plt section, which
   2990 	 will be placed in the .got section by the linker script.  */
   2991       ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
   2992 	= htab->next_gotplt_entry;
   2993       htab->next_gotplt_entry += 4;
   2994 
   2995       s = bfd_get_linker_section (dynobj, ".got.plt");
   2996       BFD_ASSERT (s != NULL);
   2997       s->size += 4;
   2998 
   2999       /* We also need to make an entry in the .rela.plt section.  */
   3000 
   3001       s = bfd_get_linker_section (dynobj, ".rela.plt");
   3002       BFD_ASSERT (s != NULL);
   3003       s->size += sizeof (Elf32_External_Rela);
   3004 
   3005       return TRUE;
   3006     }
   3007 
   3008   /* Reinitialize the plt offset now that it is not used as a reference
   3009      count any more.  */
   3010   h->plt.offset = (bfd_vma) -1;
   3011 
   3012   /* If this is a weak symbol, and there is a real definition, the
   3013      processor independent code will have arranged for us to see the
   3014      real definition first, and we can just use the same value.  */
   3015   if (h->u.weakdef != NULL)
   3016     {
   3017       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   3018 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   3019       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   3020       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   3021       return TRUE;
   3022     }
   3023 
   3024   /* This is a reference to a symbol defined by a dynamic object which
   3025      is not a function.  */
   3026 
   3027   /* If we are creating a shared library, we must presume that the
   3028      only references to the symbol are via the global offset table.
   3029      For such cases we need not do anything here; the relocations will
   3030      be handled correctly by relocate_section.  */
   3031   if (info->shared)
   3032     return TRUE;
   3033 
   3034   /* If there are no references to this symbol that do not use the
   3035      GOT, we don't need to generate a copy reloc.  */
   3036   if (!h->non_got_ref)
   3037     return TRUE;
   3038 
   3039   /* We must allocate the symbol in our .dynbss section, which will
   3040      become part of the .bss section of the executable.  There will be
   3041      an entry for this symbol in the .dynsym section.  The dynamic
   3042      object will contain position independent code, so all references
   3043      from the dynamic object to this symbol will go through the global
   3044      offset table.  The dynamic linker will use the .dynsym entry to
   3045      determine the address it must put in the global offset table, so
   3046      both the dynamic object and the regular object will refer to the
   3047      same memory location for the variable.  */
   3048 
   3049   s = bfd_get_linker_section (dynobj, ".dynbss");
   3050   BFD_ASSERT (s != NULL);
   3051 
   3052   /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
   3053      copy the initial value out of the dynamic object and into the
   3054      runtime process image.  We need to remember the offset into the
   3055      .rela.bss section we are going to use.  */
   3056   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   3057     {
   3058       asection *srel;
   3059 
   3060       srel = bfd_get_linker_section (dynobj, ".rela.bss");
   3061       BFD_ASSERT (srel != NULL);
   3062       srel->size += sizeof (Elf32_External_Rela);
   3063       h->needs_copy = 1;
   3064     }
   3065 
   3066   return _bfd_elf_adjust_dynamic_copy (h, s);
   3067 }
   3068 
   3069 /* Adjust our "subclass" elements for an indirect symbol.  */
   3070 
   3071 static void
   3072 elf_cris_copy_indirect_symbol (struct bfd_link_info *info,
   3073 			       struct elf_link_hash_entry *dir,
   3074 			       struct elf_link_hash_entry *ind)
   3075 {
   3076   struct elf_cris_link_hash_entry *edir, *eind;
   3077 
   3078   edir = (struct elf_cris_link_hash_entry *) dir;
   3079   eind = (struct elf_cris_link_hash_entry *) ind;
   3080 
   3081   /* Only indirect symbols are replaced; we're not interested in
   3082      updating any of EIND's fields for other symbols.  */
   3083   if (eind->root.root.type != bfd_link_hash_indirect)
   3084     {
   3085       /* Still, we need to copy flags for e.g. weak definitions.  */
   3086       _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   3087       return;
   3088     }
   3089 
   3090   BFD_ASSERT (edir->gotplt_offset == 0 || eind->gotplt_offset == 0);
   3091 
   3092 #define XMOVOPZ(F, OP, Z) edir->F OP eind->F; eind->F = Z
   3093 #define XMOVE(F) XMOVOPZ (F, +=, 0)
   3094   if (eind->pcrel_relocs_copied != NULL)
   3095     {
   3096       if (edir->pcrel_relocs_copied != NULL)
   3097 	{
   3098 	  struct elf_cris_pcrel_relocs_copied **pp;
   3099 	  struct elf_cris_pcrel_relocs_copied *p;
   3100 
   3101 	  /* Add reloc counts against the indirect sym to the direct sym
   3102 	     list.  Merge any entries against the same section.  */
   3103 	  for (pp = &eind->pcrel_relocs_copied; *pp != NULL;)
   3104 	    {
   3105 	      struct elf_cris_pcrel_relocs_copied *q;
   3106 	      p = *pp;
   3107 	      for (q = edir->pcrel_relocs_copied; q != NULL; q = q->next)
   3108 		if (q->section == p->section)
   3109 		  {
   3110 		    q->count += p->count;
   3111 		    *pp = p->next;
   3112 		    break;
   3113 		  }
   3114 	      if (q == NULL)
   3115 		pp = &p->next;
   3116 	    }
   3117 	  *pp = edir->pcrel_relocs_copied;
   3118 	}
   3119       XMOVOPZ (pcrel_relocs_copied, =, NULL);
   3120     }
   3121   XMOVE (gotplt_refcount);
   3122   XMOVE (gotplt_offset);
   3123   XMOVE (reg_got_refcount);
   3124   XMOVE (tprel_refcount);
   3125   XMOVE (dtp_refcount);
   3126 #undef XMOVE
   3127 #undef XMOVOPZ
   3128 
   3129   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   3130 }
   3131 
   3132 /* Look through the relocs for a section during the first phase.  */
   3133 
   3134 static bfd_boolean
   3135 cris_elf_check_relocs (bfd *abfd,
   3136 		       struct bfd_link_info *info,
   3137 		       asection *sec,
   3138 		       const Elf_Internal_Rela *relocs)
   3139 {
   3140   struct elf_cris_link_hash_table * htab;
   3141   bfd *dynobj;
   3142   Elf_Internal_Shdr *symtab_hdr;
   3143   struct elf_link_hash_entry **sym_hashes;
   3144   bfd_signed_vma *local_got_refcounts;
   3145   const Elf_Internal_Rela *rel;
   3146   const Elf_Internal_Rela *rel_end;
   3147   asection *sgot;
   3148   asection *srelgot;
   3149   asection *sreloc;
   3150 
   3151   if (info->relocatable)
   3152     return TRUE;
   3153 
   3154   htab = elf_cris_hash_table (info);
   3155   if (htab == NULL)
   3156     return FALSE;
   3157 
   3158   dynobj = elf_hash_table (info)->dynobj;
   3159   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   3160   sym_hashes = elf_sym_hashes (abfd);
   3161   local_got_refcounts = elf_local_got_refcounts (abfd);
   3162 
   3163   sgot = NULL;
   3164   srelgot = NULL;
   3165   sreloc = NULL;
   3166 
   3167   rel_end = relocs + sec->reloc_count;
   3168   for (rel = relocs; rel < rel_end; rel++)
   3169     {
   3170       struct elf_link_hash_entry *h;
   3171       unsigned long r_symndx;
   3172       enum elf_cris_reloc_type r_type;
   3173       bfd_signed_vma got_element_size = 4;
   3174       unsigned long r_symndx_lgot = INT_MAX;
   3175 
   3176       r_symndx = ELF32_R_SYM (rel->r_info);
   3177       if (r_symndx < symtab_hdr->sh_info)
   3178 	{
   3179 	  h = NULL;
   3180 	  r_symndx_lgot = LGOT_REG_NDX (r_symndx);
   3181 	}
   3182       else
   3183 	{
   3184 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   3185 	  while (h->root.type == bfd_link_hash_indirect
   3186 		 || h->root.type == bfd_link_hash_warning)
   3187 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3188 
   3189 	  /* PR15323, ref flags aren't set for references in the same
   3190 	     object.  */
   3191 	  h->root.non_ir_ref = 1;
   3192 	}
   3193 
   3194       r_type = ELF32_R_TYPE (rel->r_info);
   3195 
   3196       /* Some relocs require linker-created sections; we need to hang them
   3197 	 on the first input bfd we found that contained dynamic relocs.  */
   3198       switch (r_type)
   3199 	{
   3200 	case R_CRIS_32_DTPREL:
   3201 	  if ((sec->flags & SEC_ALLOC) == 0)
   3202 	    /* This'd be a .dtpreld entry in e.g. debug info.  We have
   3203 	       several different switch statements below, but none of
   3204 	       that is needed; we need no preparations for resolving
   3205 	       R_CRIS_32_DTPREL into a non-allocated section (debug
   3206 	       info), so let's just move on to the next
   3207 	       relocation.  */
   3208 	    continue;
   3209 	  /* Fall through.  */
   3210 	case R_CRIS_16_DTPREL:
   3211 	  /* The first .got.plt entry is right after the R_CRIS_DTPMOD
   3212 	     entry at index 3. */
   3213 	  if (htab->dtpmod_refcount == 0)
   3214 	    htab->next_gotplt_entry += 8;
   3215 
   3216 	  htab->dtpmod_refcount++;
   3217 	  /* Fall through.  */
   3218 
   3219 	case R_CRIS_32_IE:
   3220 	case R_CRIS_32_GD:
   3221 	case R_CRIS_16_GOT_GD:
   3222 	case R_CRIS_32_GOT_GD:
   3223 	case R_CRIS_32_GOT_TPREL:
   3224 	case R_CRIS_16_GOT_TPREL:
   3225 	case R_CRIS_16_GOT:
   3226 	case R_CRIS_32_GOT:
   3227 	case R_CRIS_32_GOTREL:
   3228 	case R_CRIS_32_PLT_GOTREL:
   3229 	case R_CRIS_32_PLT_PCREL:
   3230 	case R_CRIS_16_GOTPLT:
   3231 	case R_CRIS_32_GOTPLT:
   3232 	  if (dynobj == NULL)
   3233 	    {
   3234 	      elf_hash_table (info)->dynobj = dynobj = abfd;
   3235 
   3236 	      /* We could handle this if we can get a handle on the
   3237 		 output bfd in elf_cris_adjust_dynamic_symbol.  Failing
   3238 		 that, we must insist on dynobj being a specific mach.  */
   3239 	      if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
   3240 		{
   3241 		  (*_bfd_error_handler)
   3242 		    (_("%B, section %A:\n  v10/v32 compatible object %s"
   3243 		       " must not contain a PIC relocation"),
   3244 		     abfd, sec);
   3245 		  return FALSE;
   3246 		}
   3247 	    }
   3248 
   3249 	  if (sgot == NULL)
   3250 	    {
   3251 	      /* We may have a dynobj but no .got section, if machine-
   3252 		 independent parts of the linker found a reason to create
   3253 		 a dynobj.  We want to create the .got section now, so we
   3254 		 can assume it's always present whenever there's a dynobj.
   3255 		 It's ok to call this function more than once.  */
   3256 	      if (!_bfd_elf_create_got_section (dynobj, info))
   3257 		return FALSE;
   3258 
   3259 	      sgot = bfd_get_linker_section (dynobj, ".got");
   3260 	    }
   3261 
   3262 	  if (local_got_refcounts == NULL)
   3263 	    {
   3264 	      bfd_size_type amt;
   3265 
   3266 	      /* We use index local_got_refcounts[-1] to count all
   3267 		 GOT-relative relocations that do not have explicit
   3268 		 GOT entries.  */
   3269 	      amt = LGOT_ALLOC_NELTS_FOR (symtab_hdr->sh_info) + 1;
   3270 	      amt *= sizeof (bfd_signed_vma);
   3271 	      local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
   3272 	      if (local_got_refcounts == NULL)
   3273 		return FALSE;
   3274 
   3275 	      local_got_refcounts++;
   3276 	      elf_local_got_refcounts (abfd) = local_got_refcounts;
   3277 	    }
   3278 	  break;
   3279 
   3280 	default:
   3281 	  break;
   3282 	}
   3283 
   3284       /* Some relocs require a global offset table (but perhaps not a
   3285 	 specific GOT entry).  */
   3286       switch (r_type)
   3287 	{
   3288 	case R_CRIS_16_DTPREL:
   3289 	case R_CRIS_32_DTPREL:
   3290 	  /* Not requesting .got.rela for an executable: the contents
   3291 	     of the first entry is constant there.  For a shared
   3292 	     library, we need .got.rela for the R_CRIS_DTPMOD
   3293 	     relocation at index 3.  */
   3294 	  if (!info->shared)
   3295 	    break;
   3296 	  /* Fall through.  */
   3297 
   3298 	case R_CRIS_32_IE:
   3299 	case R_CRIS_32_GD:
   3300 	case R_CRIS_16_GOT_GD:
   3301 	case R_CRIS_32_GOT_GD:
   3302 	case R_CRIS_32_GOT_TPREL:
   3303 	case R_CRIS_16_GOT_TPREL:
   3304 	  /* Fall through.  */
   3305 
   3306 	  /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
   3307 	     entry only for local symbols.  Unfortunately, we don't know
   3308 	     until later on if there's a version script that forces the
   3309 	     symbol local.  We must have the .rela.got section in place
   3310 	     before we know if the symbol looks global now, so we need
   3311 	     to treat the reloc just like for R_CRIS_16_GOT and
   3312 	     R_CRIS_32_GOT.  */
   3313 	case R_CRIS_16_GOTPLT:
   3314 	case R_CRIS_32_GOTPLT:
   3315 	case R_CRIS_16_GOT:
   3316 	case R_CRIS_32_GOT:
   3317 	  if (srelgot == NULL
   3318 	      && (h != NULL || info->shared))
   3319 	    {
   3320 	      srelgot = bfd_get_linker_section (dynobj, ".rela.got");
   3321 	      if (srelgot == NULL)
   3322 		{
   3323 		  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
   3324 				    | SEC_IN_MEMORY | SEC_LINKER_CREATED
   3325 				    | SEC_READONLY);
   3326 		  srelgot = bfd_make_section_anyway_with_flags (dynobj,
   3327 								".rela.got",
   3328 								flags);
   3329 		  if (srelgot == NULL
   3330 		      || !bfd_set_section_alignment (dynobj, srelgot, 2))
   3331 		    return FALSE;
   3332 		}
   3333 	    }
   3334 	  break;
   3335 
   3336 	default:
   3337 	  break;
   3338 	}
   3339 
   3340       /* Warn and error for invalid input.  */
   3341       switch (r_type)
   3342 	{
   3343 	case R_CRIS_32_IE:
   3344 	case R_CRIS_32_TPREL:
   3345 	case R_CRIS_16_TPREL:
   3346 	case R_CRIS_32_GD:
   3347 	  if (info->shared)
   3348 	    {
   3349 	      (*_bfd_error_handler)
   3350 		(_("%B, section %A:\n  relocation %s not valid"
   3351 		   " in a shared object;"
   3352 		   " typically an option mixup, recompile with -fPIC"),
   3353 		 abfd,
   3354 		 sec,
   3355 		 cris_elf_howto_table[r_type].name);
   3356 	      /* Don't return FALSE here; we want messages for all of
   3357 		 these and the error behavior is ungraceful
   3358 		 anyway.  */
   3359 	    }
   3360 	default:
   3361 	  break;
   3362 	}
   3363 
   3364       switch (r_type)
   3365 	{
   3366 	case R_CRIS_32_GD:
   3367 	case R_CRIS_16_GOT_GD:
   3368 	case R_CRIS_32_GOT_GD:
   3369 	  /* These are requests for tls_index entries, run-time R_CRIS_DTP.  */
   3370 	  got_element_size = 8;
   3371 	  r_symndx_lgot = LGOT_DTP_NDX (r_symndx);
   3372 	  break;
   3373 
   3374 	case R_CRIS_16_DTPREL:
   3375 	case R_CRIS_32_DTPREL:
   3376 	  /* These two just request for the constant-index
   3377 	     module-local tls_index-sized GOT entry, which we add
   3378 	     elsewhere.  */
   3379 	  break;
   3380 
   3381 	case R_CRIS_32_IE:
   3382 	case R_CRIS_32_GOT_TPREL:
   3383 	case R_CRIS_16_GOT_TPREL:
   3384 	  r_symndx_lgot = LGOT_TPREL_NDX (r_symndx);
   3385 
   3386 	  /* Those relocs also require that a DSO is of type
   3387 	     Initial Exec.  Like other targets, we don't reset this
   3388 	     flag even if the relocs are GC:ed away.  */
   3389 	  if (info->shared)
   3390 	    info->flags |= DF_STATIC_TLS;
   3391 	  break;
   3392 
   3393 	  /* Let's list the other assembler-generated TLS-relocs too,
   3394 	     just to show that they're not forgotten. */
   3395 	case R_CRIS_16_TPREL:
   3396 	case R_CRIS_32_TPREL:
   3397 	default:
   3398 	  break;
   3399 	}
   3400 
   3401       switch (r_type)
   3402         {
   3403 	case R_CRIS_16_GOTPLT:
   3404 	case R_CRIS_32_GOTPLT:
   3405 	  /* Mark that we need a GOT entry if the PLT entry (and its GOT
   3406 	     entry) is eliminated.  We can only do this for a non-local
   3407 	     symbol.  */
   3408 	  if (h != NULL)
   3409 	    {
   3410 	      elf_cris_hash_entry (h)->gotplt_refcount++;
   3411 	      goto handle_gotplt_reloc;
   3412 	    }
   3413 	  /* If h is NULL then this is a local symbol, and we must make a
   3414 	     GOT entry for it, so handle it like a GOT reloc.  */
   3415 	  /* Fall through.  */
   3416 
   3417 	case R_CRIS_32_IE:
   3418 	case R_CRIS_32_GD:
   3419 	case R_CRIS_16_GOT_GD:
   3420 	case R_CRIS_32_GOT_GD:
   3421 	case R_CRIS_32_GOT_TPREL:
   3422 	case R_CRIS_16_GOT_TPREL:
   3423 	case R_CRIS_16_GOT:
   3424 	case R_CRIS_32_GOT:
   3425 	  /* This symbol requires a global offset table entry.  */
   3426 	  if (h != NULL)
   3427 	    {
   3428 	      if (h->got.refcount == 0)
   3429 		{
   3430 		  /* Make sure this symbol is output as a dynamic symbol.  */
   3431 		  if (h->dynindx == -1)
   3432 		    {
   3433 		      if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3434 			return FALSE;
   3435 		    }
   3436 		}
   3437 
   3438 	      /* Update the sum of reloc counts for this symbol.  */
   3439 	      h->got.refcount++;
   3440 
   3441 	      switch (r_type)
   3442 		{
   3443 		case R_CRIS_16_GOT:
   3444 		case R_CRIS_32_GOT:
   3445 		  if (elf_cris_hash_entry (h)->reg_got_refcount == 0)
   3446 		    {
   3447 		      /* Allocate space in the .got section.  */
   3448 		      sgot->size += got_element_size;
   3449 		      /* Allocate relocation space.  */
   3450 		      srelgot->size += sizeof (Elf32_External_Rela);
   3451 		    }
   3452 		  elf_cris_hash_entry (h)->reg_got_refcount++;
   3453 		  break;
   3454 
   3455 		case R_CRIS_32_GD:
   3456 		case R_CRIS_16_GOT_GD:
   3457 		case R_CRIS_32_GOT_GD:
   3458 		  if (elf_cris_hash_entry (h)->dtp_refcount == 0)
   3459 		    {
   3460 		      /* Allocate space in the .got section.  */
   3461 		      sgot->size += got_element_size;
   3462 		      /* Allocate relocation space.  */
   3463 		      srelgot->size += sizeof (Elf32_External_Rela);
   3464 		    }
   3465 		  elf_cris_hash_entry (h)->dtp_refcount++;
   3466 		  break;
   3467 
   3468 		case R_CRIS_32_IE:
   3469 		case R_CRIS_32_GOT_TPREL:
   3470 		case R_CRIS_16_GOT_TPREL:
   3471 		  if (elf_cris_hash_entry (h)->tprel_refcount == 0)
   3472 		    {
   3473 		      /* Allocate space in the .got section.  */
   3474 		      sgot->size += got_element_size;
   3475 		      /* Allocate relocation space.  */
   3476 		      srelgot->size += sizeof (Elf32_External_Rela);
   3477 		    }
   3478 		  elf_cris_hash_entry (h)->tprel_refcount++;
   3479 		  break;
   3480 
   3481 		default:
   3482 		  BFD_FAIL ();
   3483 		  break;
   3484 		}
   3485 	    }
   3486 	  else
   3487 	    {
   3488 	      /* This is a global offset table entry for a local symbol.  */
   3489 	      if (local_got_refcounts[r_symndx_lgot] == 0)
   3490 		{
   3491 		  sgot->size += got_element_size;
   3492 		  if (info->shared)
   3493 		    {
   3494 		      /* If we are generating a shared object, we need
   3495 			 to output a R_CRIS_RELATIVE reloc so that the
   3496 			 dynamic linker can adjust this GOT entry.
   3497 			 Similarly for non-regular got entries.  */
   3498 		      srelgot->size += sizeof (Elf32_External_Rela);
   3499 		    }
   3500 		}
   3501 	      /* Update the reloc-specific count.  */
   3502 	      local_got_refcounts[r_symndx_lgot]++;
   3503 
   3504 	      /* This one is the sum of all the others.  */
   3505 	      local_got_refcounts[r_symndx]++;
   3506 	    }
   3507 	  break;
   3508 
   3509 	case R_CRIS_16_DTPREL:
   3510 	case R_CRIS_32_DTPREL:
   3511 	case R_CRIS_32_GOTREL:
   3512 	  /* This reference requires a global offset table.
   3513 	     FIXME: The actual refcount isn't used currently; the .got
   3514 	     section can't be removed if there were any references in the
   3515 	     input.  */
   3516 	  local_got_refcounts[-1]++;
   3517 	  break;
   3518 
   3519 	handle_gotplt_reloc:
   3520 
   3521 	case R_CRIS_32_PLT_GOTREL:
   3522 	  /* This reference requires a global offset table.  */
   3523 	  local_got_refcounts[-1]++;
   3524 	  /* Fall through.  */
   3525 
   3526 	case R_CRIS_32_PLT_PCREL:
   3527 	  /* This symbol requires a procedure linkage table entry.  We
   3528 	     actually build the entry in adjust_dynamic_symbol,
   3529              because this might be a case of linking PIC code which is
   3530              never referenced by a dynamic object, in which case we
   3531              don't need to generate a procedure linkage table entry
   3532              after all.  */
   3533 
   3534 	  /* Beware: if we'd check for visibility of the symbol here
   3535 	     (and not marking the need for a PLT when non-visible), we'd
   3536 	     get into trouble with keeping handling consistent with
   3537 	     regards to relocs found before definition and GOTPLT
   3538 	     handling.  Eliminable PLT entries will be dealt with later
   3539 	     anyway.  */
   3540 	  if (h == NULL)
   3541 	    continue;
   3542 
   3543 	  h->needs_plt = 1;
   3544 	  h->plt.refcount++;
   3545 	  break;
   3546 
   3547 	case R_CRIS_8:
   3548 	case R_CRIS_16:
   3549 	case R_CRIS_32:
   3550 	  /* Let's help debug shared library creation.  Any of these
   3551 	     relocs *can* be used in shared libs, but pages containing
   3552 	     them cannot be shared, so they're not appropriate for
   3553 	     common use.  Don't warn for sections we don't care about,
   3554 	     such as debug sections or non-constant sections.  We
   3555 	     can't help tables of (global) function pointers, for
   3556 	     example, though they must be emitted in a (writable) data
   3557 	     section to avoid having impure text sections.  */
   3558 	  if (info->shared
   3559 	      && (sec->flags & SEC_ALLOC) != 0
   3560 	      && (sec->flags & SEC_READONLY) != 0)
   3561 	    {
   3562 	      /* FIXME: How do we make this optionally a warning only?  */
   3563 	      (*_bfd_error_handler)
   3564 		(_("%B, section %A:\n  relocation %s should not"
   3565 		   " be used in a shared object; recompile with -fPIC"),
   3566 		 abfd,
   3567 		 sec,
   3568 		 cris_elf_howto_table[r_type].name);
   3569 	    }
   3570 
   3571 	  /* We don't need to handle relocs into sections not going into
   3572 	     the "real" output.  */
   3573 	  if ((sec->flags & SEC_ALLOC) == 0)
   3574 	    break;
   3575 
   3576 	  if (h != NULL)
   3577 	    {
   3578 	      h->non_got_ref = 1;
   3579 
   3580 	      /* Make sure a plt entry is created for this symbol if it
   3581 		 turns out to be a function defined by a dynamic object.  */
   3582 	      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   3583 		h->plt.refcount++;
   3584 	    }
   3585 
   3586 	  /* If we are creating a shared library and this is not a local
   3587 	     symbol, we need to copy the reloc into the shared library.
   3588 	     However when linking with -Bsymbolic and this is a global
   3589 	     symbol which is defined in an object we are including in the
   3590 	     link (i.e., DEF_REGULAR is set), then we can resolve the
   3591 	     reloc directly.  At this point we have not seen all the input
   3592 	     files, so it is possible that DEF_REGULAR is not set now but
   3593 	     will be set later (it is never cleared).  In case of a weak
   3594 	     definition, DEF_REGULAR may be cleared later by a strong
   3595 	     definition in a shared library.  We account for that
   3596 	     possibility below by storing information in the relocs_copied
   3597 	     field of the hash table entry.  A similar situation occurs
   3598 	     when creating shared libraries and symbol visibility changes
   3599 	     render the symbol local.  */
   3600 
   3601 	  /* No need to do anything if we're not creating a shared object.  */
   3602 	  if (! info->shared)
   3603 	    break;
   3604 
   3605 	  /* We may need to create a reloc section in the dynobj and made room
   3606 	     for this reloc.  */
   3607 	  if (sreloc == NULL)
   3608 	    {
   3609 	      sreloc = _bfd_elf_make_dynamic_reloc_section
   3610 		(sec, dynobj, 2, abfd, /*rela?*/ TRUE);
   3611 
   3612 	      if (sreloc == NULL)
   3613 		return FALSE;
   3614 	    }
   3615 
   3616 	  if (sec->flags & SEC_READONLY)
   3617 	    info->flags |= DF_TEXTREL;
   3618 
   3619 	  sreloc->size += sizeof (Elf32_External_Rela);
   3620 	  break;
   3621 
   3622 	case R_CRIS_8_PCREL:
   3623 	case R_CRIS_16_PCREL:
   3624 	case R_CRIS_32_PCREL:
   3625 	  if (h != NULL)
   3626 	    {
   3627 	      h->non_got_ref = 1;
   3628 
   3629 	      /* Make sure a plt entry is created for this symbol if it
   3630 		 turns out to be a function defined by a dynamic object.  */
   3631 	      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   3632 		h->plt.refcount++;
   3633 	    }
   3634 
   3635 	  /* If we are creating a shared library and this is not a local
   3636 	     symbol, we need to copy the reloc into the shared library.
   3637 	     However when linking with -Bsymbolic and this is a global
   3638 	     symbol which is defined in an object we are including in the
   3639 	     link (i.e., DEF_REGULAR is set), then we can resolve the
   3640 	     reloc directly.  At this point we have not seen all the input
   3641 	     files, so it is possible that DEF_REGULAR is not set now but
   3642 	     will be set later (it is never cleared).  In case of a weak
   3643 	     definition, DEF_REGULAR may be cleared later by a strong
   3644 	     definition in a shared library.  We account for that
   3645 	     possibility below by storing information in the relocs_copied
   3646 	     field of the hash table entry.  A similar situation occurs
   3647 	     when creating shared libraries and symbol visibility changes
   3648 	     render the symbol local.  */
   3649 
   3650 	  /* No need to do anything if we're not creating a shared object.  */
   3651 	  if (! info->shared)
   3652 	    break;
   3653 
   3654 	  /* We don't need to handle relocs into sections not going into
   3655 	     the "real" output.  */
   3656 	  if ((sec->flags & SEC_ALLOC) == 0)
   3657 	    break;
   3658 
   3659 	  /* If the symbol is local, then we know already we can
   3660 	     eliminate the reloc.  */
   3661 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   3662 	    break;
   3663 
   3664 	  /* If this is with -Bsymbolic and the symbol isn't weak, and
   3665 	     is defined by an ordinary object (the ones we include in
   3666 	     this shared library) then we can also eliminate the
   3667 	     reloc.  See comment above for more eliminable cases which
   3668 	     we can't identify at this time.  */
   3669 	  if (info->symbolic
   3670 	      && h->root.type != bfd_link_hash_defweak
   3671 	      && h->def_regular)
   3672 	    break;
   3673 
   3674 	  /* We may need to create a reloc section in the dynobj and made room
   3675 	     for this reloc.  */
   3676 	  if (sreloc == NULL)
   3677 	    {
   3678 	      sreloc = _bfd_elf_make_dynamic_reloc_section
   3679 		(sec, dynobj, 2, abfd, /*rela?*/ TRUE);
   3680 
   3681 	      if (sreloc == NULL)
   3682 		return FALSE;
   3683 	    }
   3684 
   3685 	  sreloc->size += sizeof (Elf32_External_Rela);
   3686 
   3687 	  /* We count the number of PC relative relocations we have
   3688 	     entered for this symbol, so that we can discard them
   3689 	     again if the symbol is later defined by a regular object.
   3690 	     We know that h is really a pointer to an
   3691 	     elf_cris_link_hash_entry.  */
   3692 	  {
   3693 	    struct elf_cris_link_hash_entry *eh;
   3694 	    struct elf_cris_pcrel_relocs_copied *p;
   3695 
   3696 	    eh = elf_cris_hash_entry (h);
   3697 
   3698 	    for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
   3699 	      if (p->section == sec)
   3700 		break;
   3701 
   3702 	    if (p == NULL)
   3703 	      {
   3704 		p = ((struct elf_cris_pcrel_relocs_copied *)
   3705 		     bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
   3706 		if (p == NULL)
   3707 		  return FALSE;
   3708 		p->next = eh->pcrel_relocs_copied;
   3709 		eh->pcrel_relocs_copied = p;
   3710 		p->section = sec;
   3711 		p->count = 0;
   3712 		p->r_type = r_type;
   3713 	      }
   3714 
   3715 	    ++p->count;
   3716 	  }
   3717 	  break;
   3718 
   3719         /* This relocation describes the C++ object vtable hierarchy.
   3720            Reconstruct it for later use during GC.  */
   3721         case R_CRIS_GNU_VTINHERIT:
   3722           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   3723             return FALSE;
   3724           break;
   3725 
   3726         /* This relocation describes which C++ vtable entries are actually
   3727            used.  Record for later use during GC.  */
   3728         case R_CRIS_GNU_VTENTRY:
   3729           BFD_ASSERT (h != NULL);
   3730           if (h != NULL
   3731               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   3732             return FALSE;
   3733           break;
   3734 
   3735 	case R_CRIS_16_TPREL:
   3736 	case R_CRIS_32_TPREL:
   3737 	  /* Already warned above, when necessary.  */
   3738 	  break;
   3739 
   3740 	default:
   3741 	  /* Other relocs do not appear here.  */
   3742 	  bfd_set_error (bfd_error_bad_value);
   3743 	  return FALSE;
   3744         }
   3745     }
   3746 
   3747   return TRUE;
   3748 }
   3749 
   3750 /* Set the sizes of the dynamic sections.  */
   3751 
   3752 static bfd_boolean
   3753 elf_cris_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   3754 				struct bfd_link_info *info)
   3755 {
   3756   struct elf_cris_link_hash_table * htab;
   3757   bfd *dynobj;
   3758   asection *s;
   3759   bfd_boolean plt;
   3760   bfd_boolean relocs;
   3761 
   3762   htab = elf_cris_hash_table (info);
   3763   if (htab == NULL)
   3764     return FALSE;
   3765 
   3766   dynobj = elf_hash_table (info)->dynobj;
   3767   BFD_ASSERT (dynobj != NULL);
   3768 
   3769   if (elf_hash_table (info)->dynamic_sections_created)
   3770     {
   3771       /* Set the contents of the .interp section to the interpreter.  */
   3772       if (info->executable)
   3773 	{
   3774 	  s = bfd_get_linker_section (dynobj, ".interp");
   3775 	  BFD_ASSERT (s != NULL);
   3776 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   3777 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   3778 	}
   3779     }
   3780   else
   3781     {
   3782       /* Adjust all expected GOTPLT uses to use a GOT entry instead.  */
   3783       elf_cris_link_hash_traverse (htab, elf_cris_adjust_gotplt_to_got,
   3784 				   info);
   3785 
   3786       /* We may have created entries in the .rela.got section.
   3787 	 However, if we are not creating the dynamic sections, we will
   3788 	 not actually use these entries.  Reset the size of .rela.got,
   3789 	 which will cause it to get stripped from the output file
   3790 	 below.  */
   3791       s = bfd_get_linker_section (dynobj, ".rela.got");
   3792       if (s != NULL)
   3793 	s->size = 0;
   3794     }
   3795 
   3796   /* If this is a -Bsymbolic shared link, then we need to discard all PC
   3797      relative relocs against symbols defined in a regular object.  We
   3798      allocated space for them in the check_relocs routine, but we will not
   3799      fill them in in the relocate_section routine.  We also discard space
   3800      for relocs that have become for local symbols due to symbol
   3801      visibility changes.  For programs, we discard space for relocs for
   3802      symbols not referenced by any dynamic object.  */
   3803   if (info->shared)
   3804     elf_cris_link_hash_traverse (htab,
   3805 				 elf_cris_discard_excess_dso_dynamics,
   3806 				 info);
   3807   else
   3808     elf_cris_link_hash_traverse (htab,
   3809 				 elf_cris_discard_excess_program_dynamics,
   3810 				 info);
   3811 
   3812   /* The check_relocs and adjust_dynamic_symbol entry points have
   3813      determined the sizes of the various dynamic sections.  Allocate
   3814      memory for them.  */
   3815   plt = FALSE;
   3816   relocs = FALSE;
   3817   for (s = dynobj->sections; s != NULL; s = s->next)
   3818     {
   3819       const char *name;
   3820 
   3821       if ((s->flags & SEC_LINKER_CREATED) == 0)
   3822 	continue;
   3823 
   3824       /* It's OK to base decisions on the section name, because none
   3825 	 of the dynobj section names depend upon the input files.  */
   3826       name = bfd_get_section_name (dynobj, s);
   3827 
   3828       if (strcmp (name, ".plt") == 0)
   3829 	{
   3830 	  /* Remember whether there is a PLT.  */
   3831 	  plt = s->size != 0;
   3832 	}
   3833       else if (strcmp (name, ".got.plt") == 0)
   3834 	{
   3835 	  /* The .got.plt contains the .got header as well as the
   3836 	     actual .got.plt contents.  The .got header may contain a
   3837 	     R_CRIS_DTPMOD entry at index 3.  */
   3838 	  s->size += htab->dtpmod_refcount != 0
   3839 	    ? 8 : 0;
   3840 	}
   3841       else if (CONST_STRNEQ (name, ".rela"))
   3842 	{
   3843 	  if (strcmp (name, ".rela.got") == 0
   3844 	      && htab->dtpmod_refcount != 0
   3845 	      && info->shared)
   3846 	    s->size += sizeof (Elf32_External_Rela);
   3847 
   3848 	  if (s->size != 0)
   3849 	    {
   3850 	      /* Remember whether there are any reloc sections other
   3851                  than .rela.plt.  */
   3852 	      if (strcmp (name, ".rela.plt") != 0)
   3853 		  relocs = TRUE;
   3854 
   3855 	      /* We use the reloc_count field as a counter if we need
   3856 		 to copy relocs into the output file.  */
   3857 	      s->reloc_count = 0;
   3858 	    }
   3859 	}
   3860       else if (! CONST_STRNEQ (name, ".got")
   3861 	       && strcmp (name, ".dynbss") != 0)
   3862 	{
   3863 	  /* It's not one of our sections, so don't allocate space.  */
   3864 	  continue;
   3865 	}
   3866 
   3867       if (s->size == 0)
   3868 	{
   3869 	  /* If we don't need this section, strip it from the
   3870 	     output file.  This is mostly to handle .rela.bss and
   3871 	     .rela.plt.  We must create both sections in
   3872 	     create_dynamic_sections, because they must be created
   3873 	     before the linker maps input sections to output
   3874 	     sections.  The linker does that before
   3875 	     adjust_dynamic_symbol is called, and it is that
   3876 	     function which decides whether anything needs to go
   3877 	     into these sections.  */
   3878 	  s->flags |= SEC_EXCLUDE;
   3879 	  continue;
   3880 	}
   3881 
   3882       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   3883 	continue;
   3884 
   3885       /* Allocate memory for the section contents. We use bfd_zalloc here
   3886 	 in case unused entries are not reclaimed before the section's
   3887 	 contents are written out.  This should not happen, but this way
   3888 	 if it does, we will not write out garbage.  For reloc sections,
   3889 	 this will make entries have the type R_CRIS_NONE.  */
   3890       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
   3891       if (s->contents == NULL)
   3892 	return FALSE;
   3893     }
   3894 
   3895   if (elf_hash_table (info)->dynamic_sections_created)
   3896     {
   3897       /* Add some entries to the .dynamic section.  We fill in the
   3898 	 values later, in elf_cris_finish_dynamic_sections, but we
   3899 	 must add the entries now so that we get the correct size for
   3900 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   3901 	 dynamic linker and used by the debugger.  */
   3902 #define add_dynamic_entry(TAG, VAL) \
   3903   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   3904 
   3905       if (!info->shared)
   3906 	{
   3907 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   3908 	    return FALSE;
   3909 	}
   3910 
   3911       if (plt)
   3912 	{
   3913 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   3914 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   3915 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   3916 	      || !add_dynamic_entry (DT_JMPREL, 0))
   3917 	    return FALSE;
   3918 	}
   3919 
   3920       if (relocs)
   3921 	{
   3922 	  if (!add_dynamic_entry (DT_RELA, 0)
   3923 	      || !add_dynamic_entry (DT_RELASZ, 0)
   3924 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
   3925 	    return FALSE;
   3926 	}
   3927 
   3928       if ((info->flags & DF_TEXTREL) != 0)
   3929 	{
   3930 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
   3931 	    return FALSE;
   3932 	  info->flags |= DF_TEXTREL;
   3933 	}
   3934     }
   3935 #undef add_dynamic_entry
   3936 
   3937   return TRUE;
   3938 }
   3939 
   3940 /* This function is called via elf_cris_link_hash_traverse if we are
   3941    creating a shared object.  In the -Bsymbolic case, it discards the
   3942    space allocated to copy PC relative relocs against symbols which
   3943    are defined in regular objects.  For the normal non-symbolic case,
   3944    we also discard space for relocs that have become local due to
   3945    symbol visibility changes.  We allocated space for them in the
   3946    check_relocs routine, but we won't fill them in in the
   3947    relocate_section routine.  */
   3948 
   3949 static bfd_boolean
   3950 elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *h,
   3951 				      void * inf)
   3952 {
   3953   struct elf_cris_pcrel_relocs_copied *s;
   3954   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   3955 
   3956   /* If a symbol has been forced local or we have found a regular
   3957      definition for the symbolic link case, then we won't be needing
   3958      any relocs.  */
   3959   if (h->root.def_regular
   3960       && (h->root.forced_local
   3961 	  || info->symbolic))
   3962     {
   3963       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
   3964 	{
   3965 	  asection *sreloc
   3966 	    = _bfd_elf_get_dynamic_reloc_section (elf_hash_table (info)
   3967 						  ->dynobj,
   3968 						  s->section,
   3969 						  /*rela?*/ TRUE);
   3970 	  sreloc->size -= s->count * sizeof (Elf32_External_Rela);
   3971 	}
   3972       return TRUE;
   3973     }
   3974 
   3975   /* If we have accounted for PC-relative relocs for read-only
   3976      sections, now is the time to warn for them.  We can't do it in
   3977      cris_elf_check_relocs, because we don't know the status of all
   3978      symbols at that time (and it's common to force symbols local
   3979      late).  */
   3980 
   3981   for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
   3982     if ((s->section->flags & SEC_READONLY) != 0)
   3983       {
   3984 	/* FIXME: How do we make this optionally a warning only?  */
   3985 	(*_bfd_error_handler)
   3986 	  (_("%B, section `%A', to symbol `%s':\n"
   3987 	     "  relocation %s should not be used"
   3988 	     " in a shared object; recompile with -fPIC"),
   3989 	   s->section->owner,
   3990 	   s->section,
   3991 	   h->root.root.root.string,
   3992 	   cris_elf_howto_table[s->r_type].name);
   3993 
   3994 	info->flags |= DF_TEXTREL;
   3995       }
   3996 
   3997   return TRUE;
   3998 }
   3999 
   4000 /* This function is called via elf_cris_link_hash_traverse if we are *not*
   4001    creating a shared object.  We discard space for relocs for symbols put
   4002    in the .got, but which we found we do not have to resolve at run-time.  */
   4003 
   4004 static bfd_boolean
   4005 elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *h,
   4006 					  void * inf)
   4007 {
   4008   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   4009 
   4010   /* If we're not creating a shared library and have a symbol which is
   4011      referred to by .got references, but the symbol is defined locally,
   4012      (or rather, not defined by a DSO) then lose the reloc for the .got
   4013      (don't allocate room for it).  Likewise for relocs for something
   4014      for which we create a PLT.  */
   4015   if (!h->root.def_dynamic
   4016       || h->root.plt.refcount > 0)
   4017     {
   4018       if (h->reg_got_refcount > 0
   4019 	  /* The size of this section is only valid and in sync with the
   4020 	     various reference counts if we do dynamic; don't decrement it
   4021 	     otherwise.  */
   4022 	  && elf_hash_table (info)->dynamic_sections_created)
   4023 	{
   4024 	  bfd *dynobj = elf_hash_table (info)->dynobj;
   4025 	  asection *srelgot;
   4026 
   4027 	  BFD_ASSERT (dynobj != NULL);
   4028 
   4029 	  srelgot = bfd_get_linker_section (dynobj, ".rela.got");
   4030 
   4031 	  BFD_ASSERT (srelgot != NULL);
   4032 
   4033 	  srelgot->size -= sizeof (Elf32_External_Rela);
   4034 	}
   4035 
   4036       /* If the locally-defined symbol isn't used by a DSO, then we don't
   4037 	 have to export it as a dynamic symbol.  This was already done for
   4038 	 functions; doing this for all symbols would presumably not
   4039 	 introduce new problems.  Of course we don't do this if we're
   4040 	 exporting all dynamic symbols.  */
   4041       if (! info->export_dynamic
   4042 	  && h->root.dynindx != -1
   4043 	  && !h->root.def_dynamic
   4044 	  && !h->root.ref_dynamic)
   4045 	{
   4046 	  h->root.dynindx = -1;
   4047 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4048 				  h->root.dynstr_index);
   4049 	}
   4050     }
   4051 
   4052   return TRUE;
   4053 }
   4054 
   4055 /* Reject a file depending on presence and expectation of prefixed
   4056    underscores on symbols.  */
   4057 
   4058 static bfd_boolean
   4059 cris_elf_object_p (bfd *abfd)
   4060 {
   4061   if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
   4062     return FALSE;
   4063 
   4064   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
   4065     return (bfd_get_symbol_leading_char (abfd) == '_');
   4066   else
   4067     return (bfd_get_symbol_leading_char (abfd) == 0);
   4068 }
   4069 
   4070 /* Mark presence or absence of leading underscore.  Set machine type
   4071    flags from mach type.  */
   4072 
   4073 static void
   4074 cris_elf_final_write_processing (bfd *abfd,
   4075 				 bfd_boolean linker ATTRIBUTE_UNUSED)
   4076 {
   4077   unsigned long e_flags = elf_elfheader (abfd)->e_flags;
   4078 
   4079   e_flags &= ~EF_CRIS_UNDERSCORE;
   4080   if (bfd_get_symbol_leading_char (abfd) == '_')
   4081     e_flags |= EF_CRIS_UNDERSCORE;
   4082 
   4083   switch (bfd_get_mach (abfd))
   4084     {
   4085     case bfd_mach_cris_v0_v10:
   4086       e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
   4087       break;
   4088 
   4089     case bfd_mach_cris_v10_v32:
   4090       e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
   4091       break;
   4092 
   4093     case bfd_mach_cris_v32:
   4094       e_flags |= EF_CRIS_VARIANT_V32;
   4095       break;
   4096 
   4097     default:
   4098       _bfd_abort (__FILE__, __LINE__,
   4099 		  _("Unexpected machine number"));
   4100     }
   4101 
   4102   elf_elfheader (abfd)->e_flags = e_flags;
   4103 }
   4104 
   4105 /* Set the mach type from e_flags value.  */
   4106 
   4107 static bfd_boolean
   4108 cris_elf_set_mach_from_flags (bfd *abfd,
   4109 			      unsigned long flags)
   4110 {
   4111   switch (flags & EF_CRIS_VARIANT_MASK)
   4112     {
   4113     case EF_CRIS_VARIANT_ANY_V0_V10:
   4114       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
   4115       break;
   4116 
   4117     case EF_CRIS_VARIANT_V32:
   4118       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
   4119       break;
   4120 
   4121     case EF_CRIS_VARIANT_COMMON_V10_V32:
   4122       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
   4123       break;
   4124 
   4125     default:
   4126       /* Since we don't recognize them, we obviously can't support them
   4127 	 with this code; we'd have to require that all future handling
   4128 	 would be optional.  */
   4129       bfd_set_error (bfd_error_wrong_format);
   4130       return FALSE;
   4131     }
   4132 
   4133   return TRUE;
   4134 }
   4135 
   4136 /* Display the flags field.  */
   4137 
   4138 static bfd_boolean
   4139 cris_elf_print_private_bfd_data (bfd *abfd, void * ptr)
   4140 {
   4141   FILE *file = (FILE *) ptr;
   4142 
   4143   BFD_ASSERT (abfd != NULL && ptr != NULL);
   4144 
   4145   _bfd_elf_print_private_bfd_data (abfd, ptr);
   4146 
   4147   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
   4148 
   4149   if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
   4150     fprintf (file, _(" [symbols have a _ prefix]"));
   4151   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
   4152       == EF_CRIS_VARIANT_COMMON_V10_V32)
   4153     fprintf (file, _(" [v10 and v32]"));
   4154   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
   4155       == EF_CRIS_VARIANT_V32)
   4156     fprintf (file, _(" [v32]"));
   4157 
   4158   fputc ('\n', file);
   4159   return TRUE;
   4160 }
   4161 
   4162 /* Don't mix files with and without a leading underscore.  */
   4163 
   4164 static bfd_boolean
   4165 cris_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   4166 {
   4167   int imach, omach;
   4168 
   4169   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
   4170     return FALSE;
   4171 
   4172   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   4173       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   4174     return TRUE;
   4175 
   4176   imach = bfd_get_mach (ibfd);
   4177 
   4178   if (! elf_flags_init (obfd))
   4179     {
   4180       /* This happens when ld starts out with a 'blank' output file.  */
   4181       elf_flags_init (obfd) = TRUE;
   4182 
   4183       /* We ignore the linker-set mach, and instead set it according to
   4184 	 the first input file.  This would also happen if we could
   4185 	 somehow filter out the OUTPUT_ARCH () setting from elf.sc.
   4186 	 This allows us to keep the same linker config across
   4187 	 cris(v0..v10) and crisv32.  The drawback is that we can't force
   4188 	 the output type, which might be a sane thing to do for a
   4189 	 v10+v32 compatibility object.  */
   4190       if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
   4191 	return FALSE;
   4192     }
   4193 
   4194   if (bfd_get_symbol_leading_char (ibfd)
   4195       != bfd_get_symbol_leading_char (obfd))
   4196     {
   4197       (*_bfd_error_handler)
   4198 	(bfd_get_symbol_leading_char (ibfd) == '_'
   4199 	 ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols")
   4200 	 : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
   4201 	 ibfd);
   4202       bfd_set_error (bfd_error_bad_value);
   4203       return FALSE;
   4204     }
   4205 
   4206   omach = bfd_get_mach (obfd);
   4207 
   4208   if (imach != omach)
   4209     {
   4210       /* We can get an incompatible combination only if either is
   4211 	 bfd_mach_cris_v32, and the other one isn't compatible.  */
   4212       if ((imach == bfd_mach_cris_v32
   4213 	   && omach != bfd_mach_cris_v10_v32)
   4214 	  || (omach == bfd_mach_cris_v32
   4215 	      && imach != bfd_mach_cris_v10_v32))
   4216 	{
   4217 	  (*_bfd_error_handler)
   4218 	    ((imach == bfd_mach_cris_v32)
   4219 	     ? _("%B contains CRIS v32 code, incompatible"
   4220 		 " with previous objects")
   4221 	     : _("%B contains non-CRIS-v32 code, incompatible"
   4222 		 " with previous objects"),
   4223 	     ibfd);
   4224 	  bfd_set_error (bfd_error_bad_value);
   4225 	  return FALSE;
   4226 	}
   4227 
   4228       /* We don't have to check the case where the input is compatible
   4229 	 with v10 and v32, because the output is already known to be set
   4230 	 to the other (compatible) mach.  */
   4231       if (omach == bfd_mach_cris_v10_v32
   4232 	  && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
   4233 	return FALSE;
   4234     }
   4235 
   4236   return TRUE;
   4237 }
   4238 
   4239 /* Do side-effects of e_flags copying to obfd.  */
   4240 
   4241 static bfd_boolean
   4242 cris_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   4243 {
   4244   /* Call the base function.  */
   4245   if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
   4246     return FALSE;
   4247 
   4248   /* If output is big-endian for some obscure reason, stop here.  */
   4249   if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
   4250     return FALSE;
   4251 
   4252   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   4253       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   4254     return TRUE;
   4255 
   4256   /* Do what we really came here for.  */
   4257   return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
   4258 }
   4259 
   4260 static enum elf_reloc_type_class
   4261 elf_cris_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   4262 			   const asection *rel_sec ATTRIBUTE_UNUSED,
   4263 			   const Elf_Internal_Rela *rela)
   4264 {
   4265   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rela->r_info);
   4266   switch (r_type)
   4267     {
   4268     case R_CRIS_RELATIVE:
   4269       return reloc_class_relative;
   4270     case R_CRIS_JUMP_SLOT:
   4271       return reloc_class_plt;
   4272     case R_CRIS_COPY:
   4273       return reloc_class_copy;
   4274     default:
   4275       return reloc_class_normal;
   4276     }
   4277 }
   4278 
   4279 /* The elf_backend_got_elt_size worker.  For one symbol, we can have up to
   4280    two GOT entries from three types with two different sizes.  We handle
   4281    it as a single entry, so we can use the regular offset-calculation
   4282    machinery.  */
   4283 
   4284 static bfd_vma
   4285 elf_cris_got_elt_size (bfd *abfd ATTRIBUTE_UNUSED,
   4286 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   4287 		       struct elf_link_hash_entry *hr,
   4288 		       bfd *ibfd,
   4289 		       unsigned long symndx)
   4290 {
   4291   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) hr;
   4292   bfd_vma eltsiz = 0;
   4293 
   4294   /* We may have one regular GOT entry or up to two TLS GOT
   4295      entries.  */
   4296   if (h == NULL)
   4297     {
   4298       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
   4299       bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (ibfd);
   4300 
   4301       BFD_ASSERT (local_got_refcounts != NULL);
   4302 
   4303       if (local_got_refcounts[LGOT_REG_NDX (symndx)] > 0)
   4304 	{
   4305 	  /* We can't have a variable referred to both as a regular
   4306 	     variable and through TLS relocs.  */
   4307 	  BFD_ASSERT (local_got_refcounts[LGOT_DTP_NDX (symndx)] == 0
   4308 		      && local_got_refcounts[LGOT_TPREL_NDX (symndx)] == 0);
   4309 	  return 4;
   4310 	}
   4311 
   4312       if (local_got_refcounts[LGOT_DTP_NDX (symndx)] > 0)
   4313 	eltsiz += 8;
   4314 
   4315       if (local_got_refcounts[LGOT_TPREL_NDX (symndx)] > 0)
   4316 	eltsiz += 4;
   4317     }
   4318   else
   4319     {
   4320       struct elf_cris_link_hash_entry *hh = elf_cris_hash_entry (h);
   4321       if (hh->reg_got_refcount > 0)
   4322 	{
   4323 	  /* The actual error-on-input is emitted elsewhere.  */
   4324 	  BFD_ASSERT (hh->dtp_refcount == 0 && hh->tprel_refcount == 0);
   4325 	  return 4;
   4326 	}
   4327 
   4328       if (hh->dtp_refcount > 0)
   4329 	eltsiz += 8;
   4330 
   4331       if (hh->tprel_refcount > 0)
   4332 	eltsiz += 4;
   4333     }
   4334 
   4335   /* We're only called when h->got.refcount is non-zero, so we must
   4336      have a non-zero size.  */
   4337   BFD_ASSERT (eltsiz != 0);
   4338   return eltsiz;
   4339 }
   4340 
   4341 #define ELF_ARCH		bfd_arch_cris
   4343 #define ELF_TARGET_ID		CRIS_ELF_DATA
   4344 #define ELF_MACHINE_CODE	EM_CRIS
   4345 #define ELF_MAXPAGESIZE		0x2000
   4346 
   4347 #define TARGET_LITTLE_SYM	cris_elf32_vec
   4348 #define TARGET_LITTLE_NAME	"elf32-cris"
   4349 #define elf_symbol_leading_char 0
   4350 
   4351 #define elf_info_to_howto_rel			NULL
   4352 #define elf_info_to_howto			cris_info_to_howto_rela
   4353 #define elf_backend_relocate_section		cris_elf_relocate_section
   4354 #define elf_backend_gc_mark_hook		cris_elf_gc_mark_hook
   4355 #define elf_backend_gc_sweep_hook		cris_elf_gc_sweep_hook
   4356 #define elf_backend_plt_sym_val			cris_elf_plt_sym_val
   4357 #define elf_backend_check_relocs                cris_elf_check_relocs
   4358 #define elf_backend_grok_prstatus		cris_elf_grok_prstatus
   4359 #define elf_backend_grok_psinfo			cris_elf_grok_psinfo
   4360 
   4361 #define elf_backend_can_gc_sections		1
   4362 #define elf_backend_can_refcount		1
   4363 
   4364 #define elf_backend_object_p			cris_elf_object_p
   4365 #define elf_backend_final_write_processing \
   4366 	cris_elf_final_write_processing
   4367 #define bfd_elf32_bfd_print_private_bfd_data \
   4368 	cris_elf_print_private_bfd_data
   4369 #define bfd_elf32_bfd_merge_private_bfd_data \
   4370 	cris_elf_merge_private_bfd_data
   4371 #define bfd_elf32_bfd_copy_private_bfd_data \
   4372 	cris_elf_copy_private_bfd_data
   4373 
   4374 #define bfd_elf32_bfd_reloc_type_lookup		cris_reloc_type_lookup
   4375 #define bfd_elf32_bfd_reloc_name_lookup	cris_reloc_name_lookup
   4376 
   4377 #define bfd_elf32_bfd_link_hash_table_create \
   4378 	elf_cris_link_hash_table_create
   4379 #define elf_backend_adjust_dynamic_symbol \
   4380 	elf_cris_adjust_dynamic_symbol
   4381 #define elf_backend_copy_indirect_symbol \
   4382 	elf_cris_copy_indirect_symbol
   4383 #define elf_backend_size_dynamic_sections \
   4384 	elf_cris_size_dynamic_sections
   4385 #define elf_backend_init_index_section		_bfd_elf_init_1_index_section
   4386 #define elf_backend_finish_dynamic_symbol \
   4387 	elf_cris_finish_dynamic_symbol
   4388 #define elf_backend_finish_dynamic_sections \
   4389 	elf_cris_finish_dynamic_sections
   4390 #define elf_backend_create_dynamic_sections \
   4391 	_bfd_elf_create_dynamic_sections
   4392 #define bfd_elf32_bfd_final_link \
   4393 	bfd_elf_gc_common_final_link
   4394 #define elf_backend_hide_symbol			elf_cris_hide_symbol
   4395 #define elf_backend_reloc_type_class		elf_cris_reloc_type_class
   4396 
   4397 #define elf_backend_want_got_plt	1
   4398 #define elf_backend_plt_readonly	1
   4399 #define elf_backend_want_plt_sym	0
   4400 #define elf_backend_got_header_size	12
   4401 #define elf_backend_got_elt_size elf_cris_got_elt_size
   4402 
   4403 /* Later, we my want to optimize RELA entries into REL entries for dynamic
   4404    linking and libraries (if it's a win of any significance).  Until then,
   4405    take the easy route.  */
   4406 #define elf_backend_may_use_rel_p 0
   4407 #define elf_backend_may_use_rela_p 1
   4408 #define elf_backend_rela_normal		1
   4409 
   4410 #include "elf32-target.h"
   4411 
   4412 #undef TARGET_LITTLE_SYM
   4413 #undef TARGET_LITTLE_NAME
   4414 #undef elf_symbol_leading_char
   4415 
   4416 #define TARGET_LITTLE_SYM cris_elf32_us_vec
   4417 #define TARGET_LITTLE_NAME "elf32-us-cris"
   4418 #define elf_symbol_leading_char '_'
   4419 #undef elf32_bed
   4420 #define elf32_bed elf32_us_cris_bed
   4421 
   4422 #include "elf32-target.h"
   4423