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