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