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