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