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