]>
Commit | Line | Data |
---|---|---|
06c15ad7 | 1 | /* CRIS-specific support for 32-bit ELF. |
b2a8e766 | 2 | Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
06c15ad7 HPN |
3 | Contributed by Axis Communications AB. |
4 | Written by Hans-Peter Nilsson, based on elf32-fr30.c | |
915e5146 | 5 | PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c. |
06c15ad7 | 6 | |
ae9a127f | 7 | This file is part of BFD, the Binary File Descriptor library. |
06c15ad7 | 8 | |
ae9a127f NC |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
06c15ad7 | 13 | |
ae9a127f NC |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
06c15ad7 | 18 | |
ae9a127f NC |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
06c15ad7 HPN |
22 | |
23 | #include "bfd.h" | |
24 | #include "sysdep.h" | |
25 | #include "libbfd.h" | |
26 | #include "elf-bfd.h" | |
27 | #include "elf/cris.h" | |
28 | ||
29 | /* Forward declarations. */ | |
30 | static reloc_howto_type * cris_reloc_type_lookup | |
31 | PARAMS ((bfd *abfd, bfd_reloc_code_real_type code)); | |
32 | ||
a7c10850 | 33 | static void cris_info_to_howto_rela |
947216bf | 34 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); |
06c15ad7 | 35 | |
b34976b6 | 36 | static bfd_boolean cris_elf_grok_prstatus |
72209a1f HPN |
37 | PARAMS ((bfd *abfd, Elf_Internal_Note *note)); |
38 | ||
b34976b6 | 39 | static bfd_boolean cris_elf_grok_psinfo |
72209a1f HPN |
40 | PARAMS ((bfd *abfd, Elf_Internal_Note *note)); |
41 | ||
b34976b6 | 42 | static bfd_boolean cris_elf_relocate_section |
06c15ad7 HPN |
43 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
44 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
45 | ||
46 | static bfd_reloc_status_type cris_final_link_relocate | |
47 | PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *, | |
48 | Elf_Internal_Rela *, bfd_vma)); | |
49 | ||
b34976b6 | 50 | static bfd_boolean cris_elf_gc_sweep_hook |
06c15ad7 HPN |
51 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
52 | const Elf_Internal_Rela *)); | |
53 | ||
54 | static asection * cris_elf_gc_mark_hook | |
1e2f5b6e | 55 | PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, |
06c15ad7 HPN |
56 | struct elf_link_hash_entry *, Elf_Internal_Sym *)); |
57 | ||
b34976b6 | 58 | static bfd_boolean cris_elf_object_p PARAMS ((bfd *)); |
4da81684 | 59 | |
b34976b6 | 60 | static void cris_elf_final_write_processing PARAMS ((bfd *, bfd_boolean)); |
4da81684 | 61 | |
b34976b6 | 62 | static bfd_boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR)); |
4da81684 | 63 | |
b34976b6 | 64 | static bfd_boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *)); |
4da81684 | 65 | |
915e5146 | 66 | struct elf_cris_link_hash_entry; |
b34976b6 | 67 | static bfd_boolean elf_cris_discard_excess_dso_dynamics |
4d96d128 | 68 | PARAMS ((struct elf_cris_link_hash_entry *, PTR)); |
b34976b6 | 69 | static bfd_boolean elf_cris_discard_excess_program_dynamics |
915e5146 | 70 | PARAMS ((struct elf_cris_link_hash_entry *, PTR)); |
b34976b6 | 71 | static bfd_boolean elf_cris_adjust_gotplt_to_got |
915e5146 | 72 | PARAMS ((struct elf_cris_link_hash_entry *, PTR)); |
b34976b6 | 73 | static bfd_boolean elf_cris_try_fold_plt_to_got |
915e5146 HPN |
74 | PARAMS ((struct elf_cris_link_hash_entry *, PTR)); |
75 | static struct bfd_hash_entry *elf_cris_link_hash_newfunc | |
76 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
77 | static struct bfd_link_hash_table *elf_cris_link_hash_table_create | |
78 | PARAMS ((bfd *)); | |
b34976b6 | 79 | static bfd_boolean elf_cris_adjust_dynamic_symbol |
915e5146 | 80 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); |
b34976b6 | 81 | static bfd_boolean cris_elf_check_relocs |
99c79b2e AJ |
82 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
83 | const Elf_Internal_Rela *)); | |
84 | ||
b34976b6 | 85 | static bfd_boolean elf_cris_size_dynamic_sections |
915e5146 | 86 | PARAMS ((bfd *, struct bfd_link_info *)); |
b34976b6 | 87 | static bfd_boolean elf_cris_finish_dynamic_symbol |
915e5146 HPN |
88 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, |
89 | Elf_Internal_Sym *)); | |
b34976b6 | 90 | static bfd_boolean elf_cris_finish_dynamic_sections |
915e5146 HPN |
91 | PARAMS ((bfd *, struct bfd_link_info *)); |
92 | static void elf_cris_hide_symbol | |
b34976b6 | 93 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean)); |
99e4ae17 | 94 | static enum elf_reloc_type_class elf_cris_reloc_type_class |
f51e552e | 95 | PARAMS ((const Elf_Internal_Rela *)); |
915e5146 | 96 | |
06c15ad7 HPN |
97 | static reloc_howto_type cris_elf_howto_table [] = |
98 | { | |
99 | /* This reloc does nothing. */ | |
100 | HOWTO (R_CRIS_NONE, /* type */ | |
101 | 0, /* rightshift */ | |
102 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
103 | 32, /* bitsize */ | |
b34976b6 | 104 | FALSE, /* pc_relative */ |
06c15ad7 HPN |
105 | 0, /* bitpos */ |
106 | complain_overflow_bitfield, /* complain_on_overflow */ | |
107 | bfd_elf_generic_reloc, /* special_function */ | |
108 | "R_CRIS_NONE", /* name */ | |
b34976b6 | 109 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
110 | 0, /* src_mask */ |
111 | 0, /* dst_mask */ | |
b34976b6 | 112 | FALSE), /* pcrel_offset */ |
06c15ad7 HPN |
113 | |
114 | /* An 8 bit absolute relocation. */ | |
115 | HOWTO (R_CRIS_8, /* type */ | |
116 | 0, /* rightshift */ | |
117 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
118 | 8, /* bitsize */ | |
b34976b6 | 119 | FALSE, /* pc_relative */ |
06c15ad7 HPN |
120 | 0, /* bitpos */ |
121 | complain_overflow_bitfield, /* complain_on_overflow */ | |
122 | bfd_elf_generic_reloc, /* special_function */ | |
123 | "R_CRIS_8", /* name */ | |
b34976b6 | 124 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
125 | 0x0000, /* src_mask */ |
126 | 0x00ff, /* dst_mask */ | |
b34976b6 | 127 | FALSE), /* pcrel_offset */ |
06c15ad7 HPN |
128 | |
129 | /* A 16 bit absolute relocation. */ | |
130 | HOWTO (R_CRIS_16, /* type */ | |
131 | 0, /* rightshift */ | |
132 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
133 | 16, /* bitsize */ | |
b34976b6 | 134 | FALSE, /* pc_relative */ |
06c15ad7 HPN |
135 | 0, /* bitpos */ |
136 | complain_overflow_bitfield, /* complain_on_overflow */ | |
137 | bfd_elf_generic_reloc, /* special_function */ | |
138 | "R_CRIS_16", /* name */ | |
b34976b6 | 139 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
140 | 0x00000000, /* src_mask */ |
141 | 0x0000ffff, /* dst_mask */ | |
b34976b6 | 142 | FALSE), /* pcrel_offset */ |
06c15ad7 HPN |
143 | |
144 | /* A 32 bit absolute relocation. */ | |
145 | HOWTO (R_CRIS_32, /* type */ | |
146 | 0, /* rightshift */ | |
147 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
148 | 32, /* bitsize */ | |
b34976b6 | 149 | FALSE, /* pc_relative */ |
06c15ad7 HPN |
150 | 0, /* bitpos */ |
151 | complain_overflow_bitfield, /* complain_on_overflow */ | |
152 | bfd_elf_generic_reloc, /* special_function */ | |
153 | "R_CRIS_32", /* name */ | |
b34976b6 | 154 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
155 | 0x00000000, /* src_mask */ |
156 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 157 | FALSE), /* pcrel_offset */ |
06c15ad7 | 158 | |
915e5146 | 159 | /* An 8 bit PC-relative relocation. */ |
06c15ad7 HPN |
160 | HOWTO (R_CRIS_8_PCREL, /* type */ |
161 | 0, /* rightshift */ | |
162 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
163 | 8, /* bitsize */ | |
b34976b6 | 164 | TRUE, /* pc_relative */ |
06c15ad7 HPN |
165 | 0, /* bitpos */ |
166 | complain_overflow_bitfield, /* complain_on_overflow */ | |
167 | bfd_elf_generic_reloc, /* special_function */ | |
168 | "R_CRIS_8_PCREL", /* name */ | |
b34976b6 | 169 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
170 | 0x0000, /* src_mask */ |
171 | 0x00ff, /* dst_mask */ | |
b34976b6 | 172 | TRUE), /* pcrel_offset */ |
06c15ad7 | 173 | |
915e5146 | 174 | /* A 16 bit PC-relative relocation. */ |
06c15ad7 HPN |
175 | HOWTO (R_CRIS_16_PCREL, /* type */ |
176 | 0, /* rightshift */ | |
177 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
178 | 16, /* bitsize */ | |
b34976b6 | 179 | TRUE, /* pc_relative */ |
06c15ad7 HPN |
180 | 0, /* bitpos */ |
181 | complain_overflow_bitfield, /* complain_on_overflow */ | |
182 | bfd_elf_generic_reloc, /* special_function */ | |
44aa49bb | 183 | "R_CRIS_16_PCREL", /* name */ |
b34976b6 | 184 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
185 | 0x00000000, /* src_mask */ |
186 | 0x0000ffff, /* dst_mask */ | |
b34976b6 | 187 | TRUE), /* pcrel_offset */ |
06c15ad7 | 188 | |
915e5146 | 189 | /* A 32 bit PC-relative relocation. */ |
06c15ad7 HPN |
190 | HOWTO (R_CRIS_32_PCREL, /* type */ |
191 | 0, /* rightshift */ | |
192 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
193 | 32, /* bitsize */ | |
b34976b6 | 194 | TRUE, /* pc_relative */ |
06c15ad7 HPN |
195 | 0, /* bitpos */ |
196 | complain_overflow_bitfield, /* complain_on_overflow */ | |
197 | bfd_elf_generic_reloc, /* special_function */ | |
44aa49bb | 198 | "R_CRIS_32_PCREL", /* name */ |
b34976b6 | 199 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
200 | 0x00000000, /* src_mask */ |
201 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 202 | TRUE), /* pcrel_offset */ |
06c15ad7 | 203 | |
915e5146 HPN |
204 | /* GNU extension to record C++ vtable hierarchy. */ |
205 | HOWTO (R_CRIS_GNU_VTINHERIT, /* type */ | |
06c15ad7 HPN |
206 | 0, /* rightshift */ |
207 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
208 | 0, /* bitsize */ | |
b34976b6 | 209 | FALSE, /* pc_relative */ |
06c15ad7 HPN |
210 | 0, /* bitpos */ |
211 | complain_overflow_dont, /* complain_on_overflow */ | |
212 | NULL, /* special_function */ | |
213 | "R_CRIS_GNU_VTINHERIT", /* name */ | |
b34976b6 | 214 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
215 | 0, /* src_mask */ |
216 | 0, /* dst_mask */ | |
b34976b6 | 217 | FALSE), /* pcrel_offset */ |
06c15ad7 | 218 | |
915e5146 HPN |
219 | /* GNU extension to record C++ vtable member usage. */ |
220 | HOWTO (R_CRIS_GNU_VTENTRY, /* type */ | |
06c15ad7 HPN |
221 | 0, /* rightshift */ |
222 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
223 | 0, /* bitsize */ | |
b34976b6 | 224 | FALSE, /* pc_relative */ |
06c15ad7 HPN |
225 | 0, /* bitpos */ |
226 | complain_overflow_dont, /* complain_on_overflow */ | |
227 | _bfd_elf_rel_vtable_reloc_fn, /* special_function */ | |
228 | "R_CRIS_GNU_VTENTRY", /* name */ | |
b34976b6 | 229 | FALSE, /* partial_inplace */ |
06c15ad7 HPN |
230 | 0, /* src_mask */ |
231 | 0, /* dst_mask */ | |
b34976b6 | 232 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
233 | |
234 | /* This is used only by the dynamic linker. The symbol should exist | |
235 | both in the object being run and in some shared library. The | |
236 | dynamic linker copies the data addressed by the symbol from the | |
237 | shared library into the object, because the object being | |
238 | run has to have the data at some particular address. */ | |
239 | HOWTO (R_CRIS_COPY, /* type */ | |
240 | 0, /* rightshift */ | |
241 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
242 | 32, /* bitsize */ | |
b34976b6 | 243 | FALSE, /* pc_relative */ |
915e5146 HPN |
244 | 0, /* bitpos */ |
245 | complain_overflow_bitfield, /* complain_on_overflow */ | |
246 | bfd_elf_generic_reloc, /* special_function */ | |
247 | "R_CRIS_COPY", /* name */ | |
b34976b6 | 248 | FALSE, /* partial_inplace */ |
915e5146 HPN |
249 | 0, /* src_mask */ |
250 | 0, /* dst_mask */ | |
b34976b6 | 251 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
252 | |
253 | /* Like R_CRIS_32, but used when setting global offset table entries. */ | |
254 | HOWTO (R_CRIS_GLOB_DAT, /* type */ | |
255 | 0, /* rightshift */ | |
256 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
257 | 32, /* bitsize */ | |
b34976b6 | 258 | FALSE, /* pc_relative */ |
915e5146 HPN |
259 | 0, /* bitpos */ |
260 | complain_overflow_bitfield, /* complain_on_overflow */ | |
261 | bfd_elf_generic_reloc, /* special_function */ | |
262 | "R_CRIS_GLOB_DAT", /* name */ | |
b34976b6 | 263 | FALSE, /* partial_inplace */ |
915e5146 HPN |
264 | 0, /* src_mask */ |
265 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 266 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
267 | |
268 | /* Marks a procedure linkage table entry for a symbol. */ | |
269 | HOWTO (R_CRIS_JUMP_SLOT, /* type */ | |
270 | 0, /* rightshift */ | |
271 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
272 | 32, /* bitsize */ | |
b34976b6 | 273 | FALSE, /* pc_relative */ |
915e5146 HPN |
274 | 0, /* bitpos */ |
275 | complain_overflow_bitfield, /* complain_on_overflow */ | |
276 | bfd_elf_generic_reloc, /* special_function */ | |
277 | "R_CRIS_JUMP_SLOT", /* name */ | |
b34976b6 | 278 | FALSE, /* partial_inplace */ |
915e5146 HPN |
279 | 0, /* src_mask */ |
280 | 0, /* dst_mask */ | |
b34976b6 | 281 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
282 | |
283 | /* Used only by the dynamic linker. When the object is run, this | |
284 | longword is set to the load address of the object, plus the | |
285 | addend. */ | |
286 | HOWTO (R_CRIS_RELATIVE, /* type */ | |
287 | 0, /* rightshift */ | |
288 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
289 | 32, /* bitsize */ | |
b34976b6 | 290 | FALSE, /* pc_relative */ |
915e5146 HPN |
291 | 0, /* bitpos */ |
292 | complain_overflow_bitfield, /* complain_on_overflow */ | |
293 | bfd_elf_generic_reloc, /* special_function */ | |
294 | "R_CRIS_RELATIVE", /* name */ | |
b34976b6 | 295 | FALSE, /* partial_inplace */ |
915e5146 HPN |
296 | 0, /* src_mask */ |
297 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 298 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
299 | |
300 | /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */ | |
301 | HOWTO (R_CRIS_16_GOT, /* type */ | |
302 | 0, /* rightshift */ | |
303 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
304 | 16, /* bitsize */ | |
b34976b6 | 305 | FALSE, /* pc_relative */ |
915e5146 HPN |
306 | 0, /* bitpos */ |
307 | complain_overflow_bitfield, /* complain_on_overflow */ | |
308 | bfd_elf_generic_reloc, /* special_function */ | |
309 | "R_CRIS_16_GOT", /* name */ | |
b34976b6 | 310 | FALSE, /* partial_inplace */ |
915e5146 HPN |
311 | 0, /* src_mask */ |
312 | 0xffff, /* dst_mask */ | |
b34976b6 | 313 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
314 | |
315 | HOWTO (R_CRIS_32_GOT, /* type */ | |
316 | 0, /* rightshift */ | |
317 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
318 | 32, /* bitsize */ | |
b34976b6 | 319 | FALSE, /* pc_relative */ |
915e5146 HPN |
320 | 0, /* bitpos */ |
321 | complain_overflow_bitfield, /* complain_on_overflow */ | |
322 | bfd_elf_generic_reloc, /* special_function */ | |
323 | "R_CRIS_32_GOT", /* name */ | |
b34976b6 | 324 | FALSE, /* partial_inplace */ |
915e5146 HPN |
325 | 0, /* src_mask */ |
326 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 327 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
328 | |
329 | /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of | |
330 | the GOT table for the symbol. */ | |
331 | HOWTO (R_CRIS_16_GOTPLT, /* type */ | |
332 | 0, /* rightshift */ | |
333 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
334 | 16, /* bitsize */ | |
b34976b6 | 335 | FALSE, /* pc_relative */ |
915e5146 HPN |
336 | 0, /* bitpos */ |
337 | complain_overflow_bitfield, /* complain_on_overflow */ | |
338 | bfd_elf_generic_reloc, /* special_function */ | |
339 | "R_CRIS_16_GOTPLT", /* name */ | |
b34976b6 | 340 | FALSE, /* partial_inplace */ |
915e5146 HPN |
341 | 0, /* src_mask */ |
342 | 0xffff, /* dst_mask */ | |
b34976b6 | 343 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
344 | |
345 | HOWTO (R_CRIS_32_GOTPLT, /* type */ | |
346 | 0, /* rightshift */ | |
347 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
348 | 32, /* bitsize */ | |
b34976b6 | 349 | FALSE, /* pc_relative */ |
915e5146 HPN |
350 | 0, /* bitpos */ |
351 | complain_overflow_bitfield, /* complain_on_overflow */ | |
352 | bfd_elf_generic_reloc, /* special_function */ | |
353 | "R_CRIS_32_GOTPLT", /* name */ | |
b34976b6 | 354 | FALSE, /* partial_inplace */ |
915e5146 HPN |
355 | 0, /* src_mask */ |
356 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 357 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
358 | |
359 | /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should | |
360 | be necessary. */ | |
361 | HOWTO (R_CRIS_32_GOTREL, /* type */ | |
362 | 0, /* rightshift */ | |
363 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
364 | 32, /* bitsize */ | |
b34976b6 | 365 | FALSE, /* pc_relative */ |
915e5146 HPN |
366 | 0, /* bitpos */ |
367 | complain_overflow_bitfield, /* complain_on_overflow */ | |
368 | bfd_elf_generic_reloc, /* special_function */ | |
369 | "R_CRIS_32_GOTREL", /* name */ | |
b34976b6 | 370 | FALSE, /* partial_inplace */ |
915e5146 HPN |
371 | 0, /* src_mask */ |
372 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 373 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
374 | |
375 | /* A 32-bit offset from GOT to entry for this symbol in PLT and request | |
376 | to create PLT entry for symbol. */ | |
377 | HOWTO (R_CRIS_32_PLT_GOTREL, /* type */ | |
378 | 0, /* rightshift */ | |
379 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
380 | 32, /* bitsize */ | |
b34976b6 | 381 | FALSE, /* pc_relative */ |
915e5146 HPN |
382 | 0, /* bitpos */ |
383 | complain_overflow_bitfield, /* complain_on_overflow */ | |
384 | bfd_elf_generic_reloc, /* special_function */ | |
385 | "R_CRIS_32_PLT_GOTREL", /* name */ | |
b34976b6 | 386 | FALSE, /* partial_inplace */ |
915e5146 HPN |
387 | 0, /* src_mask */ |
388 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 389 | FALSE), /* pcrel_offset */ |
915e5146 HPN |
390 | |
391 | /* A 32-bit offset from PC (location after the relocation) + addend to | |
392 | entry for this symbol in PLT and request to create PLT entry for | |
393 | symbol. */ | |
394 | HOWTO (R_CRIS_32_PLT_PCREL, /* type */ | |
395 | 0, /* rightshift */ | |
396 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
397 | 32, /* bitsize */ | |
b34976b6 | 398 | TRUE, /* pc_relative */ |
915e5146 HPN |
399 | 0, /* bitpos */ |
400 | complain_overflow_bitfield, /* complain_on_overflow */ | |
401 | bfd_elf_generic_reloc, /* special_function */ | |
402 | "R_CRIS_32_PLT_PCREL", /* name */ | |
b34976b6 | 403 | FALSE, /* partial_inplace */ |
915e5146 HPN |
404 | 0, /* src_mask */ |
405 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 406 | TRUE) /* pcrel_offset */ |
06c15ad7 HPN |
407 | }; |
408 | \f | |
409 | /* Map BFD reloc types to CRIS ELF reloc types. */ | |
410 | ||
411 | struct cris_reloc_map | |
412 | { | |
413 | bfd_reloc_code_real_type bfd_reloc_val; | |
414 | unsigned int cris_reloc_val; | |
415 | }; | |
416 | ||
417 | static const struct cris_reloc_map cris_reloc_map [] = | |
418 | { | |
419 | { BFD_RELOC_NONE, R_CRIS_NONE }, | |
420 | { BFD_RELOC_8, R_CRIS_8 }, | |
421 | { BFD_RELOC_16, R_CRIS_16 }, | |
422 | { BFD_RELOC_32, R_CRIS_32 }, | |
423 | { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL }, | |
424 | { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL }, | |
425 | { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL }, | |
426 | { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT }, | |
915e5146 HPN |
427 | { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY }, |
428 | { BFD_RELOC_CRIS_COPY, R_CRIS_COPY }, | |
429 | { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT }, | |
430 | { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT }, | |
431 | { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE }, | |
432 | { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT }, | |
433 | { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT }, | |
434 | { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT }, | |
435 | { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT }, | |
436 | { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL }, | |
437 | { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL }, | |
438 | { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL } | |
06c15ad7 HPN |
439 | }; |
440 | ||
441 | static reloc_howto_type * | |
442 | cris_reloc_type_lookup (abfd, code) | |
443 | bfd * abfd ATTRIBUTE_UNUSED; | |
444 | bfd_reloc_code_real_type code; | |
445 | { | |
908d94bf | 446 | unsigned int i; |
06c15ad7 | 447 | |
908d94bf | 448 | for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++) |
06c15ad7 HPN |
449 | if (cris_reloc_map [i].bfd_reloc_val == code) |
450 | return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val]; | |
a7c10850 | 451 | |
06c15ad7 HPN |
452 | return NULL; |
453 | } | |
454 | ||
455 | /* Set the howto pointer for an CRIS ELF reloc. */ | |
456 | ||
457 | static void | |
458 | cris_info_to_howto_rela (abfd, cache_ptr, dst) | |
459 | bfd * abfd ATTRIBUTE_UNUSED; | |
460 | arelent * cache_ptr; | |
947216bf | 461 | Elf_Internal_Rela * dst; |
06c15ad7 HPN |
462 | { |
463 | unsigned int r_type; | |
464 | ||
465 | r_type = ELF32_R_TYPE (dst->r_info); | |
466 | BFD_ASSERT (r_type < (unsigned int) R_CRIS_max); | |
467 | cache_ptr->howto = & cris_elf_howto_table [r_type]; | |
468 | } | |
469 | \f | |
72209a1f HPN |
470 | /* Support for core dump NOTE sections. */ |
471 | ||
b34976b6 | 472 | static bfd_boolean |
72209a1f HPN |
473 | cris_elf_grok_prstatus (abfd, note) |
474 | bfd *abfd; | |
475 | Elf_Internal_Note *note; | |
476 | { | |
477 | int offset; | |
eea6121a | 478 | size_t size; |
72209a1f HPN |
479 | |
480 | switch (note->descsz) | |
481 | { | |
482 | default: | |
b34976b6 | 483 | return FALSE; |
72209a1f | 484 | |
bc0d28ee | 485 | case 214: /* Linux/CRIS */ |
72209a1f HPN |
486 | /* pr_cursig */ |
487 | elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12); | |
488 | ||
489 | /* pr_pid */ | |
490 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22); | |
491 | ||
492 | /* pr_reg */ | |
493 | offset = 70; | |
eea6121a | 494 | size = 140; |
72209a1f HPN |
495 | |
496 | break; | |
497 | } | |
498 | ||
499 | /* Make a ".reg/999" section. */ | |
500 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
eea6121a | 501 | size, note->descpos + offset); |
72209a1f HPN |
502 | } |
503 | ||
b34976b6 | 504 | static bfd_boolean |
72209a1f HPN |
505 | cris_elf_grok_psinfo (abfd, note) |
506 | bfd *abfd; | |
507 | Elf_Internal_Note *note; | |
508 | { | |
509 | switch (note->descsz) | |
510 | { | |
511 | default: | |
b34976b6 | 512 | return FALSE; |
72209a1f HPN |
513 | |
514 | case 124: /* Linux/CRIS elf_prpsinfo */ | |
515 | elf_tdata (abfd)->core_program | |
516 | = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16); | |
517 | elf_tdata (abfd)->core_command | |
518 | = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80); | |
519 | } | |
520 | ||
521 | /* Note that for some reason, a spurious space is tacked | |
522 | onto the end of the args in some (at least one anyway) | |
523 | implementations, so strip it off if it exists. */ | |
524 | ||
525 | { | |
526 | char *command = elf_tdata (abfd)->core_command; | |
527 | int n = strlen (command); | |
528 | ||
529 | if (0 < n && command[n - 1] == ' ') | |
530 | command[n - 1] = '\0'; | |
531 | } | |
532 | ||
b34976b6 | 533 | return TRUE; |
72209a1f HPN |
534 | } |
535 | \f | |
915e5146 HPN |
536 | /* The name of the dynamic interpreter. This is put in the .interp |
537 | section. */ | |
538 | ||
539 | #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1" | |
540 | ||
541 | /* The size in bytes of an entry in the procedure linkage table. */ | |
542 | ||
543 | #define PLT_ENTRY_SIZE 20 | |
544 | ||
545 | /* The first entry in an absolute procedure linkage table looks like this. */ | |
546 | ||
547 | static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] = | |
548 | { | |
549 | 0xfc, 0xe1, | |
cedb70c5 | 550 | 0x7e, 0x7e, /* push mof. */ |
915e5146 HPN |
551 | 0x7f, 0x0d, /* (dip [pc+]) */ |
552 | 0, 0, 0, 0, /* Replaced with address of .got + 4. */ | |
553 | 0x30, 0x7a, /* move [...],mof */ | |
554 | 0x7f, 0x0d, /* (dip [pc+]) */ | |
555 | 0, 0, 0, 0, /* Replaced with address of .got + 8. */ | |
556 | 0x30, 0x09, /* jump [...] */ | |
557 | }; | |
558 | ||
559 | /* Subsequent entries in an absolute procedure linkage table look like | |
560 | this. */ | |
561 | ||
562 | static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] = | |
563 | { | |
564 | 0x7f, 0x0d, /* (dip [pc+]) */ | |
565 | 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */ | |
566 | 0x30, 0x09, /* jump [...] */ | |
567 | 0x3f, 0x7e, /* move [pc+],mof */ | |
568 | 0, 0, 0, 0, /* Replaced with offset into relocation table. */ | |
569 | 0x2f, 0xfe, /* add.d [pc+],pc */ | |
570 | 0xec, 0xff, | |
571 | 0xff, 0xff /* Replaced with offset to start of .plt. */ | |
572 | }; | |
573 | ||
574 | /* The first entry in a PIC procedure linkage table looks like this. */ | |
575 | ||
576 | static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] = | |
577 | { | |
578 | 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */ | |
579 | 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */ | |
580 | 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */ | |
581 | 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */ | |
582 | }; | |
583 | ||
584 | /* Subsequent entries in a PIC procedure linkage table look like this. */ | |
585 | ||
586 | static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] = | |
587 | { | |
588 | 0x6f, 0x0d, /* (bdap [pc+].d,r0) */ | |
589 | 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */ | |
590 | 0x30, 0x09, /* jump [...] */ | |
591 | 0x3f, 0x7e, /* move [pc+],mof */ | |
592 | 0, 0, 0, 0, /* Replaced with offset into relocation table. */ | |
593 | 0x2f, 0xfe, /* add.d [pc+],pc */ | |
594 | 0xec, 0xff, /* Replaced with offset to start of .plt. */ | |
595 | 0xff, 0xff | |
596 | }; | |
597 | \f | |
598 | /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits | |
599 | (and most other PIC/shlib stuff). Check that we don't drift away | |
600 | without reason. | |
601 | ||
602 | The CRIS linker, like the m68k and i386 linkers (and probably the rest | |
603 | too) needs to keep track of the number of relocs that it decides to | |
604 | copy in check_relocs for each symbol. This is so that it can discard | |
605 | PC relative relocs if it doesn't need them when linking with | |
606 | -Bsymbolic. We store the information in a field extending the regular | |
607 | ELF linker hash table. */ | |
608 | ||
609 | /* This structure keeps track of the number of PC relative relocs we have | |
610 | copied for a given symbol. */ | |
611 | ||
612 | struct elf_cris_pcrel_relocs_copied | |
613 | { | |
614 | /* Next section. */ | |
615 | struct elf_cris_pcrel_relocs_copied *next; | |
616 | /* A section in dynobj. */ | |
617 | asection *section; | |
618 | /* Number of relocs copied in this section. */ | |
619 | bfd_size_type count; | |
620 | }; | |
621 | ||
622 | /* CRIS ELF linker hash entry. */ | |
623 | ||
624 | struct elf_cris_link_hash_entry | |
625 | { | |
626 | struct elf_link_hash_entry root; | |
627 | ||
628 | /* Number of PC relative relocs copied for this symbol. */ | |
629 | struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied; | |
630 | ||
631 | /* The GOTPLT references are CRIS-specific; the goal is to avoid having | |
632 | both a general GOT and a PLT-specific GOT entry for the same symbol, | |
633 | when it is referenced both as a function and as a function pointer. | |
634 | ||
635 | Number of GOTPLT references for a function. */ | |
636 | bfd_signed_vma gotplt_refcount; | |
637 | ||
638 | /* Actual GOTPLT index for this symbol, if applicable, or zero if not | |
639 | (zero is never used as an index). FIXME: We should be able to fold | |
640 | this with gotplt_refcount in a union, like the got and plt unions in | |
641 | elf_link_hash_entry. */ | |
642 | bfd_size_type gotplt_offset; | |
643 | }; | |
644 | ||
645 | /* CRIS ELF linker hash table. */ | |
646 | ||
647 | struct elf_cris_link_hash_table | |
648 | { | |
649 | struct elf_link_hash_table root; | |
650 | ||
651 | /* We can't use the PLT offset and calculate to get the GOTPLT offset, | |
652 | since we try and avoid creating GOTPLT:s when there's already a GOT. | |
653 | Instead, we keep and update the next available index here. */ | |
654 | bfd_size_type next_gotplt_entry; | |
655 | }; | |
656 | ||
657 | /* Traverse a CRIS ELF linker hash table. */ | |
658 | ||
659 | #define elf_cris_link_hash_traverse(table, func, info) \ | |
660 | (elf_link_hash_traverse \ | |
661 | (&(table)->root, \ | |
b34976b6 | 662 | (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ |
915e5146 HPN |
663 | (info))) |
664 | ||
665 | /* Get the CRIS ELF linker hash table from a link_info structure. */ | |
666 | ||
667 | #define elf_cris_hash_table(p) \ | |
668 | ((struct elf_cris_link_hash_table *) (p)->hash) | |
669 | ||
670 | /* Create an entry in a CRIS ELF linker hash table. */ | |
671 | ||
672 | static struct bfd_hash_entry * | |
673 | elf_cris_link_hash_newfunc (entry, table, string) | |
674 | struct bfd_hash_entry *entry; | |
675 | struct bfd_hash_table *table; | |
676 | const char *string; | |
677 | { | |
678 | struct elf_cris_link_hash_entry *ret = | |
679 | (struct elf_cris_link_hash_entry *) entry; | |
680 | ||
681 | /* Allocate the structure if it has not already been allocated by a | |
682 | subclass. */ | |
683 | if (ret == (struct elf_cris_link_hash_entry *) NULL) | |
684 | ret = ((struct elf_cris_link_hash_entry *) | |
685 | bfd_hash_allocate (table, | |
686 | sizeof (struct elf_cris_link_hash_entry))); | |
687 | if (ret == (struct elf_cris_link_hash_entry *) NULL) | |
688 | return (struct bfd_hash_entry *) ret; | |
689 | ||
690 | /* Call the allocation method of the superclass. */ | |
691 | ret = ((struct elf_cris_link_hash_entry *) | |
692 | _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
693 | table, string)); | |
694 | if (ret != (struct elf_cris_link_hash_entry *) NULL) | |
695 | { | |
696 | ret->pcrel_relocs_copied = NULL; | |
697 | ret->gotplt_refcount = 0; | |
698 | ret->gotplt_offset = 0; | |
699 | } | |
700 | ||
701 | return (struct bfd_hash_entry *) ret; | |
702 | } | |
703 | ||
704 | /* Create a CRIS ELF linker hash table. */ | |
705 | ||
706 | static struct bfd_link_hash_table * | |
707 | elf_cris_link_hash_table_create (abfd) | |
708 | bfd *abfd; | |
709 | { | |
710 | struct elf_cris_link_hash_table *ret; | |
dc810e39 | 711 | bfd_size_type amt = sizeof (struct elf_cris_link_hash_table); |
915e5146 | 712 | |
e2d34d7d | 713 | ret = ((struct elf_cris_link_hash_table *) bfd_malloc (amt)); |
915e5146 HPN |
714 | if (ret == (struct elf_cris_link_hash_table *) NULL) |
715 | return NULL; | |
716 | ||
717 | if (! _bfd_elf_link_hash_table_init (&ret->root, abfd, | |
718 | elf_cris_link_hash_newfunc)) | |
719 | { | |
e2d34d7d | 720 | free (ret); |
915e5146 HPN |
721 | return NULL; |
722 | } | |
723 | ||
724 | /* Initialize to skip over the first three entries in the gotplt; they | |
725 | are used for run-time symbol evaluation. */ | |
726 | ret->next_gotplt_entry = 12; | |
727 | ||
728 | return &ret->root.root; | |
729 | } | |
730 | \f | |
06c15ad7 | 731 | /* Perform a single relocation. By default we use the standard BFD |
915e5146 | 732 | routines, with a few tweaks. */ |
06c15ad7 HPN |
733 | |
734 | static bfd_reloc_status_type | |
735 | cris_final_link_relocate (howto, input_bfd, input_section, contents, rel, | |
736 | relocation) | |
737 | reloc_howto_type * howto; | |
738 | bfd * input_bfd; | |
739 | asection * input_section; | |
740 | bfd_byte * contents; | |
741 | Elf_Internal_Rela * rel; | |
742 | bfd_vma relocation; | |
743 | { | |
915e5146 HPN |
744 | bfd_reloc_status_type r; |
745 | ||
746 | /* PC-relative relocations are relative to the position *after* | |
747 | the reloc. Note that for R_CRIS_8_PCREL the adjustment is | |
748 | not a single byte, since PC must be 16-bit-aligned. */ | |
749 | switch (ELF32_R_TYPE (rel->r_info)) | |
750 | { | |
751 | /* Check that the 16-bit GOT relocs are positive. */ | |
752 | case R_CRIS_16_GOTPLT: | |
753 | case R_CRIS_16_GOT: | |
754 | if ((bfd_signed_vma) relocation < 0) | |
755 | return bfd_reloc_overflow; | |
756 | break; | |
757 | ||
758 | case R_CRIS_32_PLT_PCREL: | |
759 | case R_CRIS_32_PCREL: | |
760 | relocation -= 2; | |
761 | /* Fall through. */ | |
762 | case R_CRIS_8_PCREL: | |
763 | case R_CRIS_16_PCREL: | |
764 | relocation -= 2; | |
765 | break; | |
766 | ||
767 | default: | |
768 | break; | |
769 | } | |
770 | ||
771 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
06c15ad7 HPN |
772 | contents, rel->r_offset, |
773 | relocation, rel->r_addend); | |
774 | return r; | |
775 | } | |
776 | \f | |
777 | /* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was | |
778 | copied, for further comments. */ | |
779 | ||
b34976b6 | 780 | static bfd_boolean |
06c15ad7 HPN |
781 | cris_elf_relocate_section (output_bfd, info, input_bfd, input_section, |
782 | contents, relocs, local_syms, local_sections) | |
b34976b6 AM |
783 | bfd *output_bfd ATTRIBUTE_UNUSED; |
784 | struct bfd_link_info *info; | |
785 | bfd *input_bfd; | |
786 | asection *input_section; | |
787 | bfd_byte *contents; | |
788 | Elf_Internal_Rela *relocs; | |
789 | Elf_Internal_Sym *local_syms; | |
790 | asection **local_sections; | |
06c15ad7 | 791 | { |
915e5146 | 792 | bfd *dynobj; |
b34976b6 AM |
793 | Elf_Internal_Shdr *symtab_hdr; |
794 | struct elf_link_hash_entry **sym_hashes; | |
915e5146 HPN |
795 | bfd_vma *local_got_offsets; |
796 | asection *sgot; | |
797 | asection *splt; | |
798 | asection *sreloc; | |
b34976b6 AM |
799 | Elf_Internal_Rela *rel; |
800 | Elf_Internal_Rela *relend; | |
06c15ad7 | 801 | |
1049f94e | 802 | if (info->relocatable) |
b34976b6 | 803 | return TRUE; |
f0fe0e16 | 804 | |
915e5146 HPN |
805 | dynobj = elf_hash_table (info)->dynobj; |
806 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
06c15ad7 HPN |
807 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
808 | sym_hashes = elf_sym_hashes (input_bfd); | |
809 | relend = relocs + input_section->reloc_count; | |
810 | ||
915e5146 HPN |
811 | sgot = NULL; |
812 | splt = NULL; | |
813 | sreloc = NULL; | |
814 | ||
815 | if (dynobj != NULL) | |
816 | { | |
817 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
818 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
819 | } | |
820 | ||
06c15ad7 HPN |
821 | for (rel = relocs; rel < relend; rel ++) |
822 | { | |
b34976b6 AM |
823 | reloc_howto_type *howto; |
824 | unsigned long r_symndx; | |
825 | Elf_Internal_Sym *sym; | |
826 | asection *sec; | |
827 | struct elf_link_hash_entry *h; | |
828 | bfd_vma relocation; | |
829 | bfd_reloc_status_type r; | |
830 | const char *symname = NULL; | |
831 | int r_type; | |
a7c10850 | 832 | |
06c15ad7 | 833 | r_type = ELF32_R_TYPE (rel->r_info); |
a7c10850 | 834 | |
06c15ad7 HPN |
835 | if ( r_type == R_CRIS_GNU_VTINHERIT |
836 | || r_type == R_CRIS_GNU_VTENTRY) | |
837 | continue; | |
a7c10850 | 838 | |
06c15ad7 | 839 | /* This is a final link. */ |
f0fe0e16 | 840 | r_symndx = ELF32_R_SYM (rel->r_info); |
915e5146 | 841 | howto = cris_elf_howto_table + r_type; |
06c15ad7 HPN |
842 | h = NULL; |
843 | sym = NULL; | |
844 | sec = NULL; | |
a7c10850 | 845 | |
06c15ad7 HPN |
846 | if (r_symndx < symtab_hdr->sh_info) |
847 | { | |
848 | sym = local_syms + r_symndx; | |
849 | sec = local_sections [r_symndx]; | |
8517fae7 | 850 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); |
a7c10850 | 851 | |
dc810e39 AM |
852 | symname = (bfd_elf_string_from_elf_section |
853 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); | |
854 | if (symname == NULL) | |
855 | symname = bfd_section_name (input_bfd, sec); | |
06c15ad7 HPN |
856 | } |
857 | else | |
858 | { | |
560e09e9 NC |
859 | bfd_boolean warned; |
860 | bfd_boolean unresolved_reloc; | |
0a4787a0 | 861 | |
b2a8e766 AM |
862 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
863 | r_symndx, symtab_hdr, sym_hashes, | |
864 | h, sec, relocation, | |
865 | unresolved_reloc, warned); | |
915e5146 | 866 | |
560e09e9 | 867 | if (unresolved_reloc |
915e5146 HPN |
868 | /* Perhaps we should detect the cases that |
869 | sec->output_section is expected to be NULL like i386 and | |
870 | m68k, but apparently (and according to elfxx-ia64.c) all | |
871 | valid cases are where the symbol is defined in a shared | |
872 | object which we link dynamically against. This includes | |
873 | PLT relocs for which we've created a PLT entry and other | |
874 | relocs for which we're prepared to create dynamic | |
875 | relocations. | |
876 | ||
877 | For now, new situations cause us to just err when | |
878 | sec->output_offset is NULL but the object with the symbol | |
879 | is *not* dynamically linked against. Thus this will | |
880 | automatically remind us so we can see if there are other | |
881 | valid cases we need to revisit. */ | |
560e09e9 NC |
882 | && (sec->owner->flags & DYNAMIC) != 0) |
883 | relocation = 0; | |
884 | ||
885 | else if (h->root.type == bfd_link_hash_defined | |
886 | || h->root.type == bfd_link_hash_defweak) | |
887 | { | |
888 | /* Here follow the cases where the relocation value must | |
889 | be zero (or when further handling is simplified when | |
890 | zero). I can't claim to understand the various | |
891 | conditions and they weren't described in the files | |
892 | where I copied them from (elf32-m68k.c and | |
893 | elf32-i386.c), but let's mention examples of where | |
894 | they happen. FIXME: Perhaps define and use a | |
895 | dynamic_symbol_p function like ia64. | |
896 | ||
897 | - When creating a shared library, we can have an | |
898 | ordinary relocation for a symbol defined in a shared | |
899 | library (perhaps the one we create). We then make | |
900 | the relocation value zero, as the value seen now will | |
901 | be added into the relocation addend in this shared | |
902 | library, but must be handled only at dynamic-link | |
903 | time. FIXME: Not sure this example covers the | |
904 | h->elf_link_hash_flags test, though it's there in | |
905 | other targets. */ | |
906 | if (info->shared | |
907 | && ((! info->symbolic && h->dynindx != -1) | |
908 | || (h->elf_link_hash_flags | |
909 | & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
910 | && (input_section->flags & SEC_ALLOC) != 0 | |
911 | && (r_type == R_CRIS_8 | |
912 | || r_type == R_CRIS_16 | |
913 | || r_type == R_CRIS_32 | |
914 | || r_type == R_CRIS_8_PCREL | |
915 | || r_type == R_CRIS_16_PCREL | |
916 | || r_type == R_CRIS_32_PCREL)) | |
915e5146 | 917 | relocation = 0; |
560e09e9 | 918 | else if (unresolved_reloc) |
915e5146 | 919 | { |
560e09e9 | 920 | _bfd_error_handler |
8f615d07 AM |
921 | (_("%s: unresolvable relocation %s against symbol `%s' from %s section"), |
922 | bfd_archive_filename (input_bfd), | |
923 | cris_elf_howto_table[r_type].name, | |
924 | symname, | |
925 | bfd_get_section_name (input_bfd, input_section)); | |
915e5146 | 926 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 927 | return FALSE; |
915e5146 | 928 | } |
06c15ad7 | 929 | } |
06c15ad7 | 930 | } |
a7c10850 | 931 | |
915e5146 | 932 | switch (r_type) |
06c15ad7 | 933 | { |
915e5146 HPN |
934 | case R_CRIS_16_GOTPLT: |
935 | case R_CRIS_32_GOTPLT: | |
936 | /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT, | |
937 | but we require a PLT, and the PLT handling will take care of | |
938 | filling in the PLT-specific GOT entry. For the GOT offset, | |
939 | calculate it as we do when filling it in for the .got.plt | |
940 | section. If we don't have a PLT, punt to GOT handling. */ | |
941 | if (h != NULL | |
942 | && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0) | |
06c15ad7 | 943 | { |
915e5146 HPN |
944 | asection *sgotplt |
945 | = bfd_get_section_by_name (dynobj, ".got.plt"); | |
946 | bfd_vma got_offset; | |
a7c10850 | 947 | |
915e5146 HPN |
948 | BFD_ASSERT (h->dynindx != -1); |
949 | BFD_ASSERT (sgotplt != NULL); | |
a7c10850 | 950 | |
915e5146 HPN |
951 | got_offset |
952 | = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset; | |
06c15ad7 | 953 | |
915e5146 | 954 | relocation = got_offset; |
06c15ad7 | 955 | break; |
915e5146 | 956 | } |
06c15ad7 | 957 | |
4d96d128 | 958 | /* We didn't make a PLT entry for this symbol. Maybe everything is |
915e5146 HPN |
959 | folded into the GOT. Other than folding, this happens when |
960 | statically linking PIC code, or when using -Bsymbolic. Check | |
961 | that we instead have a GOT entry as done for us by | |
962 | elf_cris_adjust_dynamic_symbol, and drop through into the | |
e4a2175c HPN |
963 | ordinary GOT cases. This must not happen for the |
964 | executable, because any reference it does to a function | |
965 | that is satisfied by a DSO must generate a PLT. We assume | |
966 | these call-specific relocs don't address non-functions. */ | |
967 | if (h != NULL | |
968 | && (h->got.offset == (bfd_vma) -1 | |
969 | || (!info->shared | |
970 | && !((h->elf_link_hash_flags | |
971 | & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
972 | || ((h->elf_link_hash_flags | |
973 | & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
974 | && h->root.type == bfd_link_hash_undefweak))))) | |
915e5146 HPN |
975 | { |
976 | (*_bfd_error_handler) | |
e4a2175c HPN |
977 | ((h->got.offset == (bfd_vma) -1) |
978 | ? _("%s: No PLT nor GOT for relocation %s against\ | |
979 | symbol `%s' from %s section") | |
980 | : _("%s: No PLT for relocation %s against\ | |
981 | symbol `%s' from %s section"), | |
8f615d07 | 982 | bfd_archive_filename (input_bfd), |
915e5146 | 983 | cris_elf_howto_table[r_type].name, |
dc810e39 | 984 | symname[0] != '\0' ? symname : _("[whose name is lost]"), |
915e5146 HPN |
985 | bfd_get_section_name (input_bfd, input_section)); |
986 | ||
987 | /* FIXME: Perhaps blaming input is not the right thing to | |
988 | do; this is probably an internal error. But it is true | |
989 | that we didn't like that particular input. */ | |
990 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 991 | return FALSE; |
915e5146 HPN |
992 | } |
993 | /* Fall through. */ | |
994 | ||
995 | /* The size of the actual relocation is not used here; we only | |
996 | fill in the GOT table here. */ | |
997 | case R_CRIS_16_GOT: | |
998 | case R_CRIS_32_GOT: | |
999 | { | |
1000 | bfd_vma off; | |
1001 | ||
1002 | /* Note that despite using RELA relocations, the .got contents | |
1003 | is always filled in with the link-relative relocation | |
1004 | value; the addend. */ | |
1005 | ||
1006 | if (h != NULL) | |
1007 | { | |
1008 | off = h->got.offset; | |
1009 | BFD_ASSERT (off != (bfd_vma) -1); | |
1010 | ||
1011 | if (!elf_hash_table (info)->dynamic_sections_created | |
205cfeb0 | 1012 | || (! info->shared |
e4a2175c HPN |
1013 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) |
1014 | || h->type == STT_FUNC | |
1015 | || (h->elf_link_hash_flags | |
1016 | & ELF_LINK_HASH_NEEDS_PLT))) | |
915e5146 HPN |
1017 | || (info->shared |
1018 | && (info->symbolic || h->dynindx == -1) | |
1019 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
1020 | { | |
4d96d128 | 1021 | /* This wasn't checked above for ! info->shared, but |
e4a2175c HPN |
1022 | must hold there if we get here; the symbol must |
1023 | be defined in the regular program or be undefweak | |
1024 | or be a function or otherwise need a PLT. */ | |
6849c52f HPN |
1025 | BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created |
1026 | || info->shared | |
4d96d128 | 1027 | || (h->elf_link_hash_flags |
6849c52f | 1028 | & ELF_LINK_HASH_DEF_REGULAR) != 0 |
e4a2175c HPN |
1029 | || h->type == STT_FUNC |
1030 | || (h->elf_link_hash_flags | |
1031 | & ELF_LINK_HASH_NEEDS_PLT) | |
6849c52f | 1032 | || h->root.type == bfd_link_hash_undefweak); |
4d96d128 | 1033 | |
915e5146 | 1034 | /* This is actually a static link, or it is a |
6849c52f HPN |
1035 | -Bsymbolic link and the symbol is defined locally, |
1036 | or is undefweak, or the symbol was forced to be | |
1037 | local because of a version file, or we're not | |
1038 | creating a dynamic object. We must initialize this | |
1039 | entry in the global offset table. Since the offset | |
1040 | must always be a multiple of 4, we use the least | |
1041 | significant bit to record whether we have | |
1042 | initialized it already. | |
1043 | ||
1044 | If this GOT entry should be runtime-initialized, we | |
1045 | will create a .rela.got relocation entry to | |
1046 | initialize the value. This is done in the | |
1047 | finish_dynamic_symbol routine. */ | |
915e5146 HPN |
1048 | if ((off & 1) != 0) |
1049 | off &= ~1; | |
1050 | else | |
1051 | { | |
1052 | bfd_put_32 (output_bfd, relocation, | |
1053 | sgot->contents + off); | |
1054 | h->got.offset |= 1; | |
1055 | } | |
1056 | } | |
1057 | } | |
1058 | else | |
1059 | { | |
1060 | BFD_ASSERT (local_got_offsets != NULL | |
1061 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
1062 | ||
1063 | off = local_got_offsets[r_symndx]; | |
1064 | ||
1065 | /* The offset must always be a multiple of 4. We use | |
1066 | the least significant bit to record whether we have | |
1067 | already generated the necessary reloc. */ | |
1068 | if ((off & 1) != 0) | |
1069 | off &= ~1; | |
1070 | else | |
1071 | { | |
1072 | bfd_put_32 (output_bfd, relocation, sgot->contents + off); | |
1073 | ||
1074 | if (info->shared) | |
1075 | { | |
947216bf | 1076 | asection *s; |
915e5146 | 1077 | Elf_Internal_Rela outrel; |
947216bf | 1078 | bfd_byte *loc; |
915e5146 | 1079 | |
947216bf AM |
1080 | s = bfd_get_section_by_name (dynobj, ".rela.got"); |
1081 | BFD_ASSERT (s != NULL); | |
915e5146 HPN |
1082 | |
1083 | outrel.r_offset = (sgot->output_section->vma | |
1084 | + sgot->output_offset | |
1085 | + off); | |
1086 | outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE); | |
1087 | outrel.r_addend = relocation; | |
947216bf AM |
1088 | loc = s->contents; |
1089 | loc += s->reloc_count++ * sizeof (Elf32_External_Rela); | |
1090 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); | |
915e5146 HPN |
1091 | } |
1092 | ||
1093 | local_got_offsets[r_symndx] |= 1; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | relocation = sgot->output_offset + off; | |
1098 | if (rel->r_addend != 0) | |
1099 | { | |
1100 | /* We can't do anything for a relocation which is against | |
1101 | a symbol *plus offset*. GOT holds relocations for | |
1102 | symbols. Make this an error; the compiler isn't | |
1103 | allowed to pass us these kinds of things. */ | |
1104 | if (h == NULL) | |
1105 | (*_bfd_error_handler) | |
1106 | (_("%s: relocation %s with non-zero addend %d against local symbol from %s section"), | |
8f615d07 | 1107 | bfd_archive_filename (input_bfd), |
915e5146 HPN |
1108 | cris_elf_howto_table[r_type].name, |
1109 | rel->r_addend, | |
1110 | bfd_get_section_name (input_bfd, input_section)); | |
1111 | else | |
1112 | (*_bfd_error_handler) | |
1113 | (_("%s: relocation %s with non-zero addend %d against symbol `%s' from %s section"), | |
8f615d07 | 1114 | bfd_archive_filename (input_bfd), |
915e5146 HPN |
1115 | cris_elf_howto_table[r_type].name, |
1116 | rel->r_addend, | |
dc810e39 | 1117 | symname[0] != '\0' ? symname : _("[whose name is lost]"), |
915e5146 HPN |
1118 | bfd_get_section_name (input_bfd, input_section)); |
1119 | ||
1120 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 1121 | return FALSE; |
915e5146 HPN |
1122 | } |
1123 | } | |
1124 | break; | |
06c15ad7 | 1125 | |
915e5146 | 1126 | case R_CRIS_32_GOTREL: |
3dab95d3 HPN |
1127 | /* This relocation must only be performed against local symbols. |
1128 | It's also ok when we link a program and the symbol is either | |
1129 | defined in an ordinary (non-DSO) object or is undefined weak. */ | |
1130 | if (h != NULL | |
1131 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
1132 | && !(!info->shared | |
1133 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
1134 | || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
1135 | && h->root.type == bfd_link_hash_undefweak)))) | |
915e5146 HPN |
1136 | { |
1137 | (*_bfd_error_handler) | |
1138 | (_("%s: relocation %s is not allowed for global symbol: `%s' from %s section"), | |
8f615d07 | 1139 | bfd_archive_filename (input_bfd), |
915e5146 | 1140 | cris_elf_howto_table[r_type].name, |
dc810e39 | 1141 | symname, |
915e5146 HPN |
1142 | bfd_get_section_name (input_bfd, input_section)); |
1143 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 1144 | return FALSE; |
06c15ad7 HPN |
1145 | } |
1146 | ||
58255df3 HPN |
1147 | /* This can happen if we get a link error with the input ELF |
1148 | variant mismatching the output variant. Emit an error so | |
1149 | it's noticed if it happens elsewhere. */ | |
1150 | if (sgot == NULL) | |
1151 | { | |
1152 | (*_bfd_error_handler) | |
1153 | (_("%s: relocation %s in section %s with no GOT created"), | |
1154 | bfd_archive_filename (input_bfd), | |
1155 | cris_elf_howto_table[r_type].name, | |
1156 | bfd_get_section_name (input_bfd, input_section)); | |
1157 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 1158 | return FALSE; |
58255df3 HPN |
1159 | } |
1160 | ||
915e5146 HPN |
1161 | /* This relocation is like a PC-relative one, except the |
1162 | reference point is the location of GOT. Note that | |
1163 | sgot->output_offset is not involved in this calculation. We | |
1164 | always want the start of entire .got section, not the | |
1165 | position after the reserved header. */ | |
1166 | relocation -= sgot->output_section->vma; | |
1167 | break; | |
06c15ad7 | 1168 | |
915e5146 HPN |
1169 | case R_CRIS_32_PLT_PCREL: |
1170 | /* Relocation is to the entry for this symbol in the | |
1171 | procedure linkage table. */ | |
06c15ad7 | 1172 | |
915e5146 HPN |
1173 | /* Resolve a PLT_PCREL reloc against a local symbol directly, |
1174 | without using the procedure linkage table. */ | |
0a4787a0 | 1175 | if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
915e5146 HPN |
1176 | break; |
1177 | ||
1178 | if (h->plt.offset == (bfd_vma) -1 | |
1179 | || splt == NULL) | |
1180 | { | |
1181 | /* We didn't make a PLT entry for this symbol. This | |
1182 | happens when statically linking PIC code, or when | |
1183 | using -Bsymbolic. */ | |
1184 | break; | |
1185 | } | |
1186 | ||
1187 | relocation = (splt->output_section->vma | |
1188 | + splt->output_offset | |
1189 | + h->plt.offset); | |
1190 | break; | |
1191 | ||
1192 | case R_CRIS_32_PLT_GOTREL: | |
1193 | /* Like R_CRIS_32_PLT_PCREL, but the reference point is the | |
1194 | start of the .got section. See also comment at | |
1195 | R_CRIS_32_GOT. */ | |
1196 | relocation -= sgot->output_section->vma; | |
1197 | ||
1198 | /* Resolve a PLT_GOTREL reloc against a local symbol directly, | |
1199 | without using the procedure linkage table. */ | |
0a4787a0 | 1200 | if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
915e5146 HPN |
1201 | break; |
1202 | ||
1203 | if (h->plt.offset == (bfd_vma) -1 | |
1204 | || splt == NULL) | |
1205 | { | |
1206 | /* We didn't make a PLT entry for this symbol. This | |
1207 | happens when statically linking PIC code, or when | |
1208 | using -Bsymbolic. */ | |
1209 | break; | |
1210 | } | |
1211 | ||
1212 | relocation = (splt->output_section->vma | |
1213 | + splt->output_offset | |
1214 | + h->plt.offset | |
1215 | - sgot->output_section->vma); | |
1216 | break; | |
1217 | ||
1218 | case R_CRIS_8_PCREL: | |
1219 | case R_CRIS_16_PCREL: | |
1220 | case R_CRIS_32_PCREL: | |
1221 | /* If the symbol was local, we need no shlib-specific handling. */ | |
0a4787a0 | 1222 | if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
915e5146 HPN |
1223 | break; |
1224 | ||
1225 | /* Fall through. */ | |
1226 | case R_CRIS_8: | |
1227 | case R_CRIS_16: | |
1228 | case R_CRIS_32: | |
1229 | if (info->shared | |
ec338859 | 1230 | && r_symndx != 0 |
915e5146 HPN |
1231 | && (input_section->flags & SEC_ALLOC) != 0 |
1232 | && ((r_type != R_CRIS_8_PCREL | |
1233 | && r_type != R_CRIS_16_PCREL | |
1234 | && r_type != R_CRIS_32_PCREL) | |
1235 | || (!info->symbolic | |
1236 | || (h->elf_link_hash_flags | |
1237 | & ELF_LINK_HASH_DEF_REGULAR) == 0))) | |
1238 | { | |
1239 | Elf_Internal_Rela outrel; | |
947216bf | 1240 | bfd_byte *loc; |
b34976b6 | 1241 | bfd_boolean skip, relocate; |
915e5146 HPN |
1242 | |
1243 | /* When generating a shared object, these relocations | |
1244 | are copied into the output file to be resolved at run | |
1245 | time. */ | |
1246 | ||
1247 | if (sreloc == NULL) | |
1248 | { | |
1249 | const char *name; | |
1250 | ||
1251 | name = (bfd_elf_string_from_elf_section | |
1252 | (input_bfd, | |
1253 | elf_elfheader (input_bfd)->e_shstrndx, | |
1254 | elf_section_data (input_section)->rel_hdr.sh_name)); | |
1255 | if (name == NULL) | |
b34976b6 | 1256 | return FALSE; |
915e5146 HPN |
1257 | |
1258 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
1259 | && strcmp (bfd_get_section_name (input_bfd, | |
1260 | input_section), | |
1261 | name + 5) == 0); | |
1262 | ||
1263 | sreloc = bfd_get_section_by_name (dynobj, name); | |
1264 | ||
1265 | /* That section should have been created in | |
1266 | cris_elf_check_relocs, but that function will not be | |
1267 | called for objects which fail in | |
1268 | cris_elf_merge_private_bfd_data. */ | |
1269 | if (sreloc == NULL) | |
1270 | { | |
1271 | (*_bfd_error_handler) | |
1272 | (_("%s: Internal inconsistency; no relocation section %s"), | |
8f615d07 | 1273 | bfd_archive_filename (input_bfd), |
915e5146 HPN |
1274 | name); |
1275 | ||
1276 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 1277 | return FALSE; |
915e5146 HPN |
1278 | } |
1279 | } | |
1280 | ||
b34976b6 AM |
1281 | skip = FALSE; |
1282 | relocate = FALSE; | |
915e5146 | 1283 | |
c629eae0 JJ |
1284 | outrel.r_offset = |
1285 | _bfd_elf_section_offset (output_bfd, info, input_section, | |
1286 | rel->r_offset); | |
1287 | if (outrel.r_offset == (bfd_vma) -1) | |
b34976b6 | 1288 | skip = TRUE; |
0bb2d96a | 1289 | else if (outrel.r_offset == (bfd_vma) -2) |
b34976b6 | 1290 | skip = TRUE, relocate = TRUE; |
915e5146 HPN |
1291 | outrel.r_offset += (input_section->output_section->vma |
1292 | + input_section->output_offset); | |
1293 | ||
1294 | if (skip) | |
0bb2d96a | 1295 | memset (&outrel, 0, sizeof outrel); |
915e5146 HPN |
1296 | /* h->dynindx may be -1 if the symbol was marked to |
1297 | become local. */ | |
1298 | else if (h != NULL | |
1299 | && ((! info->symbolic && h->dynindx != -1) | |
1300 | || (h->elf_link_hash_flags | |
1301 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
1302 | { | |
1303 | BFD_ASSERT (h->dynindx != -1); | |
915e5146 HPN |
1304 | outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); |
1305 | outrel.r_addend = relocation + rel->r_addend; | |
1306 | } | |
1307 | else | |
1308 | { | |
1309 | if (r_type == R_CRIS_32) | |
1310 | { | |
b34976b6 | 1311 | relocate = TRUE; |
915e5146 HPN |
1312 | outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE); |
1313 | outrel.r_addend = relocation + rel->r_addend; | |
1314 | } | |
1315 | else | |
1316 | { | |
1317 | long indx; | |
1318 | ||
8517fae7 | 1319 | if (bfd_is_abs_section (sec)) |
915e5146 HPN |
1320 | indx = 0; |
1321 | else if (sec == NULL || sec->owner == NULL) | |
1322 | { | |
1323 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 1324 | return FALSE; |
915e5146 HPN |
1325 | } |
1326 | else | |
1327 | { | |
1328 | asection *osec; | |
1329 | ||
1330 | osec = sec->output_section; | |
1331 | indx = elf_section_data (osec)->dynindx; | |
1332 | BFD_ASSERT (indx > 0); | |
1333 | } | |
1334 | ||
915e5146 HPN |
1335 | outrel.r_info = ELF32_R_INFO (indx, r_type); |
1336 | outrel.r_addend = relocation + rel->r_addend; | |
1337 | } | |
1338 | } | |
1339 | ||
947216bf AM |
1340 | loc = sreloc->contents; |
1341 | loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela); | |
1342 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); | |
915e5146 HPN |
1343 | |
1344 | /* This reloc will be computed at runtime, so there's no | |
1345 | need to do anything now, except for R_CRIS_32 relocations | |
1346 | that have been turned into R_CRIS_RELATIVE. */ | |
1347 | if (!relocate) | |
1348 | continue; | |
1349 | } | |
1350 | ||
1351 | break; | |
1352 | } | |
1353 | ||
1354 | r = cris_final_link_relocate (howto, input_bfd, input_section, | |
1355 | contents, rel, relocation); | |
1356 | ||
1357 | if (r != bfd_reloc_ok) | |
1358 | { | |
1359 | const char * msg = (const char *) NULL; | |
1360 | ||
1361 | switch (r) | |
1362 | { | |
1363 | case bfd_reloc_overflow: | |
1364 | r = info->callbacks->reloc_overflow | |
dc810e39 | 1365 | (info, symname, howto->name, (bfd_vma) 0, |
915e5146 HPN |
1366 | input_bfd, input_section, rel->r_offset); |
1367 | break; | |
1368 | ||
1369 | case bfd_reloc_undefined: | |
1370 | r = info->callbacks->undefined_symbol | |
dc810e39 | 1371 | (info, symname, input_bfd, input_section, rel->r_offset, |
b34976b6 | 1372 | TRUE); |
915e5146 HPN |
1373 | break; |
1374 | ||
1375 | case bfd_reloc_outofrange: | |
1376 | msg = _("internal error: out of range error"); | |
1377 | break; | |
1378 | ||
1379 | case bfd_reloc_notsupported: | |
1380 | msg = _("internal error: unsupported relocation error"); | |
1381 | break; | |
1382 | ||
1383 | case bfd_reloc_dangerous: | |
1384 | msg = _("internal error: dangerous relocation"); | |
1385 | break; | |
1386 | ||
1387 | default: | |
1388 | msg = _("internal error: unknown error"); | |
1389 | break; | |
1390 | } | |
1391 | ||
1392 | if (msg) | |
1393 | r = info->callbacks->warning | |
dc810e39 | 1394 | (info, msg, symname, input_bfd, input_section, rel->r_offset); |
915e5146 HPN |
1395 | |
1396 | if (! r) | |
b34976b6 | 1397 | return FALSE; |
915e5146 HPN |
1398 | } |
1399 | } | |
1400 | ||
b34976b6 | 1401 | return TRUE; |
915e5146 HPN |
1402 | } |
1403 | \f | |
1404 | /* Finish up dynamic symbol handling. We set the contents of various | |
1405 | dynamic sections here. */ | |
1406 | ||
b34976b6 | 1407 | static bfd_boolean |
915e5146 HPN |
1408 | elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym) |
1409 | bfd *output_bfd; | |
1410 | struct bfd_link_info *info; | |
1411 | struct elf_link_hash_entry *h; | |
1412 | Elf_Internal_Sym *sym; | |
1413 | { | |
1414 | bfd *dynobj; | |
1415 | int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16; | |
1416 | ||
1417 | dynobj = elf_hash_table (info)->dynobj; | |
1418 | ||
1419 | if (h->plt.offset != (bfd_vma) -1) | |
1420 | { | |
1421 | asection *splt; | |
1422 | asection *sgotplt; | |
1423 | asection *sgot; | |
1424 | asection *srela; | |
1425 | bfd_vma got_base; | |
1426 | ||
1427 | bfd_vma gotplt_offset | |
1428 | = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset; | |
1429 | Elf_Internal_Rela rela; | |
947216bf | 1430 | bfd_byte *loc; |
b34976b6 | 1431 | bfd_boolean has_gotplt = gotplt_offset != 0; |
915e5146 HPN |
1432 | |
1433 | /* Get the index in the procedure linkage table which | |
1434 | corresponds to this symbol. This is the index of this symbol | |
1435 | in all the symbols for which we are making plt entries. The | |
1436 | first entry in the procedure linkage table is reserved. */ | |
1437 | /* We have to count backwards here, and the result is only valid as | |
1438 | an index into .got.plt and its relocations. FIXME: Constants... */ | |
1439 | bfd_vma gotplt_index = gotplt_offset/4 - 3; | |
1440 | ||
1441 | /* Get the offset into the .got table of the entry that corresponds | |
1442 | to this function. Note that we embed knowledge that "incoming" | |
1443 | .got goes after .got.plt in the output without padding (pointer | |
1444 | aligned). However, that knowledge is present in several other | |
c152c796 | 1445 | places too. */ |
915e5146 HPN |
1446 | bfd_vma got_offset |
1447 | = (has_gotplt | |
1448 | ? gotplt_offset | |
1449 | : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry); | |
1450 | ||
1451 | /* This symbol has an entry in the procedure linkage table. Set it | |
1452 | up. */ | |
1453 | ||
1454 | BFD_ASSERT (h->dynindx != -1); | |
1455 | ||
1456 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1457 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1458 | sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); | |
1459 | srela = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
1460 | BFD_ASSERT (splt != NULL && sgotplt != NULL | |
1461 | && (! has_gotplt || srela != NULL)); | |
1462 | ||
1463 | got_base = sgotplt->output_section->vma + sgotplt->output_offset; | |
1464 | ||
1465 | /* Fill in the entry in the procedure linkage table. */ | |
1466 | if (! info->shared) | |
1467 | { | |
1468 | memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry, | |
1469 | PLT_ENTRY_SIZE); | |
1470 | ||
1471 | /* We need to enter the absolute address of the GOT entry here. */ | |
1472 | bfd_put_32 (output_bfd, got_base + got_offset, | |
1473 | splt->contents + h->plt.offset + plt_off1); | |
1474 | } | |
1475 | else | |
1476 | { | |
1477 | memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry, | |
1478 | PLT_ENTRY_SIZE); | |
1479 | bfd_put_32 (output_bfd, got_offset, | |
1480 | splt->contents + h->plt.offset + plt_off1); | |
1481 | } | |
1482 | ||
1483 | /* Fill in the plt entry and make a relocation, if this is a "real" | |
1484 | PLT entry. */ | |
1485 | if (has_gotplt) | |
1486 | { | |
1487 | /* Fill in the offset into the reloc table. */ | |
1488 | bfd_put_32 (output_bfd, | |
1489 | gotplt_index * sizeof (Elf32_External_Rela), | |
1490 | splt->contents + h->plt.offset + plt_off2); | |
1491 | ||
1492 | /* Fill in the offset to the first PLT entry, where to "jump". */ | |
1493 | bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4), | |
1494 | splt->contents + h->plt.offset + plt_off3); | |
1495 | ||
1496 | /* Fill in the entry in the global offset table with the address of | |
1497 | the relocating stub. */ | |
1498 | bfd_put_32 (output_bfd, | |
1499 | (splt->output_section->vma | |
1500 | + splt->output_offset | |
1501 | + h->plt.offset | |
1502 | + 8), | |
1503 | sgotplt->contents + got_offset); | |
1504 | ||
1505 | /* Fill in the entry in the .rela.plt section. */ | |
1506 | rela.r_offset = (sgotplt->output_section->vma | |
1507 | + sgotplt->output_offset | |
1508 | + got_offset); | |
1509 | rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT); | |
1510 | rela.r_addend = 0; | |
947216bf AM |
1511 | loc = srela->contents + gotplt_index * sizeof (Elf32_External_Rela); |
1512 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
915e5146 HPN |
1513 | } |
1514 | ||
1515 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1516 | { | |
1517 | /* Mark the symbol as undefined, rather than as defined in | |
1518 | the .plt section. Leave the value alone. */ | |
1519 | sym->st_shndx = SHN_UNDEF; | |
1520 | ||
1521 | /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't | |
1522 | know whether resetting the value is significant; if it really | |
1523 | is, rather than a quirk or bug in the sparc port, then I | |
1524 | believe we'd see this elsewhere. */ | |
1525 | /* If the symbol is weak, we do need to clear the value. | |
1526 | Otherwise, the PLT entry would provide a definition for | |
1527 | the symbol even if the symbol wasn't defined anywhere, | |
1528 | and so the symbol would never be NULL. */ | |
1529 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK) | |
1530 | == 0) | |
1531 | sym->st_value = 0; | |
1532 | } | |
1533 | } | |
1534 | ||
e4a2175c HPN |
1535 | /* For an ordinary program, we emit .got relocs only for symbols that |
1536 | are in the dynamic-symbols table and are either defined by the | |
1537 | program or are undefined weak symbols, or are function symbols | |
1538 | where we do not output a PLT: the PLT reloc was output above and all | |
1539 | references to the function symbol are redirected to the PLT. */ | |
4d96d128 | 1540 | if (h->got.offset != (bfd_vma) -1 |
6849c52f HPN |
1541 | && (info->shared |
1542 | || (h->dynindx != -1 | |
e4a2175c | 1543 | && h->plt.offset == (bfd_vma) -1 |
6849c52f HPN |
1544 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 |
1545 | && h->root.type != bfd_link_hash_undefweak))) | |
915e5146 HPN |
1546 | { |
1547 | asection *sgot; | |
1548 | asection *srela; | |
1549 | Elf_Internal_Rela rela; | |
947216bf | 1550 | bfd_byte *loc; |
dc810e39 | 1551 | bfd_byte *where; |
915e5146 HPN |
1552 | |
1553 | /* This symbol has an entry in the global offset table. Set it up. */ | |
1554 | ||
1555 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1556 | srela = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1557 | BFD_ASSERT (sgot != NULL && srela != NULL); | |
1558 | ||
1559 | rela.r_offset = (sgot->output_section->vma | |
1560 | + sgot->output_offset | |
dc810e39 | 1561 | + (h->got.offset &~ (bfd_vma) 1)); |
915e5146 HPN |
1562 | |
1563 | /* If this is a static link, or it is a -Bsymbolic link and the | |
1564 | symbol is defined locally or was forced to be local because | |
1565 | of a version file, we just want to emit a RELATIVE reloc. | |
1566 | The entry in the global offset table will already have been | |
1567 | initialized in the relocate_section function. */ | |
dc810e39 | 1568 | where = sgot->contents + (h->got.offset &~ (bfd_vma) 1); |
915e5146 HPN |
1569 | if (! elf_hash_table (info)->dynamic_sections_created |
1570 | || (info->shared | |
1571 | && (info->symbolic || h->dynindx == -1) | |
1572 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
1573 | { | |
1574 | rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE); | |
dc810e39 | 1575 | rela.r_addend = bfd_get_signed_32 (output_bfd, where); |
915e5146 HPN |
1576 | } |
1577 | else | |
1578 | { | |
dc810e39 | 1579 | bfd_put_32 (output_bfd, (bfd_vma) 0, where); |
915e5146 HPN |
1580 | rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT); |
1581 | rela.r_addend = 0; | |
1582 | } | |
1583 | ||
947216bf AM |
1584 | loc = srela->contents; |
1585 | loc += srela->reloc_count++ * sizeof (Elf32_External_Rela); | |
1586 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
915e5146 HPN |
1587 | } |
1588 | ||
1589 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) | |
1590 | { | |
1591 | asection *s; | |
1592 | Elf_Internal_Rela rela; | |
947216bf | 1593 | bfd_byte *loc; |
915e5146 HPN |
1594 | |
1595 | /* This symbol needs a copy reloc. Set it up. */ | |
1596 | ||
1597 | BFD_ASSERT (h->dynindx != -1 | |
1598 | && (h->root.type == bfd_link_hash_defined | |
1599 | || h->root.type == bfd_link_hash_defweak)); | |
1600 | ||
1601 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
1602 | ".rela.bss"); | |
1603 | BFD_ASSERT (s != NULL); | |
1604 | ||
1605 | rela.r_offset = (h->root.u.def.value | |
1606 | + h->root.u.def.section->output_section->vma | |
1607 | + h->root.u.def.section->output_offset); | |
1608 | rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY); | |
1609 | rela.r_addend = 0; | |
947216bf AM |
1610 | loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela); |
1611 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
915e5146 HPN |
1612 | } |
1613 | ||
1614 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ | |
1615 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
1616 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
1617 | sym->st_shndx = SHN_ABS; | |
1618 | ||
b34976b6 | 1619 | return TRUE; |
915e5146 HPN |
1620 | } |
1621 | \f | |
1622 | /* Finish up the dynamic sections. */ | |
1623 | ||
b34976b6 | 1624 | static bfd_boolean |
915e5146 HPN |
1625 | elf_cris_finish_dynamic_sections (output_bfd, info) |
1626 | bfd *output_bfd; | |
1627 | struct bfd_link_info *info; | |
1628 | { | |
1629 | bfd *dynobj; | |
1630 | asection *sgot; | |
1631 | asection *sdyn; | |
1632 | ||
1633 | dynobj = elf_hash_table (info)->dynobj; | |
1634 | ||
1635 | sgot = bfd_get_section_by_name (dynobj, ".got.plt"); | |
1636 | BFD_ASSERT (sgot != NULL); | |
1637 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
1638 | ||
1639 | if (elf_hash_table (info)->dynamic_sections_created) | |
1640 | { | |
1641 | asection *splt; | |
1642 | Elf32_External_Dyn *dyncon, *dynconend; | |
1643 | ||
1644 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1645 | BFD_ASSERT (splt != NULL && sdyn != NULL); | |
1646 | ||
1647 | dyncon = (Elf32_External_Dyn *) sdyn->contents; | |
eea6121a | 1648 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); |
915e5146 HPN |
1649 | for (; dyncon < dynconend; dyncon++) |
1650 | { | |
1651 | Elf_Internal_Dyn dyn; | |
1652 | asection *s; | |
1653 | ||
1654 | bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); | |
1655 | ||
1656 | switch (dyn.d_tag) | |
1657 | { | |
1658 | default: | |
1659 | break; | |
1660 | ||
1661 | case DT_PLTGOT: | |
1662 | s = bfd_get_section_by_name (output_bfd, ".got"); | |
1663 | BFD_ASSERT (s != NULL); | |
1664 | dyn.d_un.d_ptr = s->vma; | |
1665 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
1666 | break; | |
1667 | ||
1668 | case DT_JMPREL: | |
1669 | /* Yes, we *can* have a .plt and no .plt.rela, for instance | |
1670 | if all symbols are found in the .got (not .got.plt). */ | |
1671 | s = bfd_get_section_by_name (output_bfd, ".rela.plt"); | |
1672 | dyn.d_un.d_ptr = s != NULL ? s->vma : 0; | |
1673 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
1674 | break; | |
1675 | ||
1676 | case DT_PLTRELSZ: | |
1677 | s = bfd_get_section_by_name (output_bfd, ".rela.plt"); | |
1678 | if (s == NULL) | |
1679 | dyn.d_un.d_val = 0; | |
915e5146 | 1680 | else |
eea6121a | 1681 | dyn.d_un.d_val = s->size; |
915e5146 HPN |
1682 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); |
1683 | break; | |
1684 | ||
1685 | case DT_RELASZ: | |
1686 | /* The procedure linkage table relocs (DT_JMPREL) should | |
1687 | not be included in the overall relocs (DT_RELA). | |
1688 | Therefore, we override the DT_RELASZ entry here to | |
1689 | make it not include the JMPREL relocs. Since the | |
1690 | linker script arranges for .rela.plt to follow all | |
1691 | other relocation sections, we don't have to worry | |
1692 | about changing the DT_RELA entry. */ | |
1693 | s = bfd_get_section_by_name (output_bfd, ".rela.plt"); | |
1694 | if (s != NULL) | |
eea6121a | 1695 | dyn.d_un.d_val -= s->size; |
915e5146 HPN |
1696 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); |
1697 | break; | |
1698 | } | |
1699 | } | |
1700 | ||
1701 | /* Fill in the first entry in the procedure linkage table. */ | |
eea6121a | 1702 | if (splt->size > 0) |
915e5146 HPN |
1703 | { |
1704 | if (info->shared) | |
1705 | memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE); | |
1706 | else | |
1707 | { | |
1708 | memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE); | |
1709 | bfd_put_32 (output_bfd, | |
1710 | sgot->output_section->vma + sgot->output_offset + 4, | |
1711 | splt->contents + 6); | |
1712 | bfd_put_32 (output_bfd, | |
1713 | sgot->output_section->vma + sgot->output_offset + 8, | |
1714 | splt->contents + 14); | |
1715 | ||
1716 | elf_section_data (splt->output_section)->this_hdr.sh_entsize | |
1717 | = PLT_ENTRY_SIZE; | |
1718 | } | |
1719 | } | |
1720 | } | |
1721 | ||
1722 | /* Fill in the first three entries in the global offset table. */ | |
eea6121a | 1723 | if (sgot->size > 0) |
915e5146 HPN |
1724 | { |
1725 | if (sdyn == NULL) | |
1726 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents); | |
1727 | else | |
1728 | bfd_put_32 (output_bfd, | |
1729 | sdyn->output_section->vma + sdyn->output_offset, | |
1730 | sgot->contents); | |
1731 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4); | |
1732 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8); | |
1733 | } | |
1734 | ||
1735 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; | |
1736 | ||
b34976b6 | 1737 | return TRUE; |
06c15ad7 HPN |
1738 | } |
1739 | \f | |
1740 | /* Return the section that should be marked against GC for a given | |
1741 | relocation. */ | |
1742 | ||
1743 | static asection * | |
1e2f5b6e | 1744 | cris_elf_gc_mark_hook (sec, info, rel, h, sym) |
b34976b6 AM |
1745 | asection *sec; |
1746 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
1747 | Elf_Internal_Rela *rel; | |
1748 | struct elf_link_hash_entry *h; | |
1749 | Elf_Internal_Sym *sym; | |
06c15ad7 HPN |
1750 | { |
1751 | if (h != NULL) | |
1752 | { | |
1753 | switch (ELF32_R_TYPE (rel->r_info)) | |
1754 | { | |
1755 | case R_CRIS_GNU_VTINHERIT: | |
1756 | case R_CRIS_GNU_VTENTRY: | |
1757 | break; | |
1758 | ||
1759 | default: | |
1760 | switch (h->root.type) | |
1761 | { | |
1762 | case bfd_link_hash_defined: | |
1763 | case bfd_link_hash_defweak: | |
1764 | return h->root.u.def.section; | |
1765 | ||
1766 | case bfd_link_hash_common: | |
1767 | return h->root.u.c.p->section; | |
1768 | ||
1769 | default: | |
1770 | break; | |
1771 | } | |
1772 | } | |
1773 | } | |
1774 | else | |
1e2f5b6e | 1775 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); |
06c15ad7 HPN |
1776 | |
1777 | return NULL; | |
1778 | } | |
1779 | ||
1780 | /* Update the got entry reference counts for the section being removed. */ | |
1781 | ||
b34976b6 | 1782 | static bfd_boolean |
06c15ad7 | 1783 | cris_elf_gc_sweep_hook (abfd, info, sec, relocs) |
b34976b6 AM |
1784 | bfd *abfd ATTRIBUTE_UNUSED; |
1785 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
1786 | asection *sec ATTRIBUTE_UNUSED; | |
1787 | const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED; | |
06c15ad7 | 1788 | { |
915e5146 HPN |
1789 | Elf_Internal_Shdr *symtab_hdr; |
1790 | struct elf_link_hash_entry **sym_hashes; | |
1791 | bfd_signed_vma *local_got_refcounts; | |
1792 | const Elf_Internal_Rela *rel, *relend; | |
915e5146 HPN |
1793 | bfd *dynobj; |
1794 | asection *sgot; | |
1795 | asection *srelgot; | |
1796 | ||
915e5146 HPN |
1797 | dynobj = elf_hash_table (info)->dynobj; |
1798 | if (dynobj == NULL) | |
b34976b6 | 1799 | return TRUE; |
915e5146 | 1800 | |
babfd660 KH |
1801 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1802 | sym_hashes = elf_sym_hashes (abfd); | |
1803 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
1804 | ||
915e5146 HPN |
1805 | sgot = bfd_get_section_by_name (dynobj, ".got"); |
1806 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1807 | ||
1808 | relend = relocs + sec->reloc_count; | |
1809 | for (rel = relocs; rel < relend; rel++) | |
1810 | { | |
babfd660 KH |
1811 | unsigned long r_symndx; |
1812 | struct elf_link_hash_entry *h; | |
1813 | ||
915e5146 HPN |
1814 | switch (ELF32_R_TYPE (rel->r_info)) |
1815 | { | |
1816 | case R_CRIS_16_GOT: | |
1817 | case R_CRIS_32_GOT: | |
1818 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1819 | if (r_symndx >= symtab_hdr->sh_info) | |
1820 | { | |
1821 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1822 | if (h->got.refcount > 0) | |
1823 | { | |
1824 | --h->got.refcount; | |
1825 | if (h->got.refcount == 0) | |
1826 | { | |
1827 | /* We don't need the .got entry any more. */ | |
eea6121a AM |
1828 | sgot->size -= 4; |
1829 | srelgot->size -= sizeof (Elf32_External_Rela); | |
915e5146 HPN |
1830 | } |
1831 | } | |
1832 | break; | |
1833 | } | |
1834 | ||
1835 | local_got_reloc: | |
1836 | if (local_got_refcounts != NULL) | |
1837 | { | |
1838 | if (local_got_refcounts[r_symndx] > 0) | |
1839 | { | |
1840 | --local_got_refcounts[r_symndx]; | |
1841 | if (local_got_refcounts[r_symndx] == 0) | |
1842 | { | |
1843 | /* We don't need the .got entry any more. */ | |
eea6121a | 1844 | sgot->size -= 4; |
915e5146 | 1845 | if (info->shared) |
eea6121a | 1846 | srelgot->size -= sizeof (Elf32_External_Rela); |
915e5146 HPN |
1847 | } |
1848 | } | |
1849 | } | |
1850 | break; | |
1851 | ||
1852 | case R_CRIS_16_GOTPLT: | |
1853 | case R_CRIS_32_GOTPLT: | |
1854 | /* For local symbols, treat these like GOT relocs. */ | |
1855 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1856 | if (r_symndx < symtab_hdr->sh_info) | |
1857 | goto local_got_reloc; | |
e4a2175c | 1858 | /* Fall through. */ |
915e5146 HPN |
1859 | case R_CRIS_32_PLT_GOTREL: |
1860 | /* FIXME: We don't garbage-collect away the .got section. */ | |
1861 | if (local_got_refcounts != NULL) | |
1862 | local_got_refcounts[-1]--; | |
1863 | /* Fall through. */ | |
915e5146 HPN |
1864 | case R_CRIS_8_PCREL: |
1865 | case R_CRIS_16_PCREL: | |
1866 | case R_CRIS_32_PCREL: | |
1867 | case R_CRIS_32_PLT_PCREL: | |
1868 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1869 | if (r_symndx >= symtab_hdr->sh_info) | |
1870 | { | |
1871 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
0a4787a0 HPN |
1872 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
1873 | && h->plt.refcount > 0) | |
915e5146 HPN |
1874 | --h->plt.refcount; |
1875 | } | |
1876 | break; | |
1877 | ||
1878 | default: | |
1879 | break; | |
1880 | } | |
1881 | } | |
1882 | ||
b34976b6 | 1883 | return TRUE; |
06c15ad7 HPN |
1884 | } |
1885 | ||
915e5146 HPN |
1886 | /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT |
1887 | entry but we found we will not create any. Called when we find we will | |
4d96d128 HPN |
1888 | not have any PLT for this symbol, by for example |
1889 | elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link, | |
1890 | or elf_cris_size_dynamic_sections if no dynamic sections will be | |
1891 | created (we're only linking static objects). */ | |
915e5146 | 1892 | |
b34976b6 | 1893 | static bfd_boolean |
915e5146 HPN |
1894 | elf_cris_adjust_gotplt_to_got (h, p) |
1895 | struct elf_cris_link_hash_entry *h; | |
1896 | PTR p; | |
1897 | { | |
1898 | struct bfd_link_info *info = (struct bfd_link_info *) p; | |
1899 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
1900 | ||
1901 | BFD_ASSERT (dynobj != NULL); | |
1902 | ||
e92d460e AM |
1903 | if (h->root.root.type == bfd_link_hash_warning) |
1904 | h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link; | |
1905 | ||
915e5146 HPN |
1906 | /* If nobody wanted a GOTPLT with this symbol, we're done. */ |
1907 | if (h->gotplt_refcount <= 0) | |
b34976b6 | 1908 | return TRUE; |
915e5146 HPN |
1909 | |
1910 | if (h->root.got.refcount > 0) | |
1911 | { | |
1912 | /* There's a GOT entry for this symbol. Just adjust the refcount. | |
1913 | Probably not necessary at this stage, but keeping it accurate | |
1914 | helps avoiding surprises later. */ | |
1915 | h->root.got.refcount += h->gotplt_refcount; | |
1916 | h->gotplt_refcount = -1; | |
1917 | } | |
1918 | else | |
1919 | { | |
4d96d128 | 1920 | /* No GOT entry for this symbol. We need to create one. */ |
915e5146 HPN |
1921 | asection *sgot = bfd_get_section_by_name (dynobj, ".got"); |
1922 | asection *srelgot | |
1923 | = bfd_get_section_by_name (dynobj, ".rela.got"); | |
1924 | ||
1925 | /* Put an accurate refcount there. */ | |
1926 | h->root.got.refcount = h->gotplt_refcount; | |
1927 | ||
1928 | h->gotplt_refcount = -1; | |
1929 | ||
44aa49bb HPN |
1930 | /* We always have a .got and a .rela.got section if there were |
1931 | GOTPLT relocs in input. */ | |
1932 | BFD_ASSERT (sgot != NULL && srelgot != NULL); | |
915e5146 HPN |
1933 | |
1934 | /* Allocate space in the .got section. */ | |
eea6121a | 1935 | sgot->size += 4; |
915e5146 HPN |
1936 | |
1937 | /* Allocate relocation space. */ | |
eea6121a | 1938 | srelgot->size += sizeof (Elf32_External_Rela); |
915e5146 HPN |
1939 | } |
1940 | ||
b34976b6 | 1941 | return TRUE; |
915e5146 HPN |
1942 | } |
1943 | ||
1944 | /* Try to fold PLT entries with GOT entries. There are two cases when we | |
1945 | want to do this: | |
1946 | ||
1947 | - When all PLT references are GOTPLT references, and there are GOT | |
e4a2175c HPN |
1948 | references, and this is not the executable. We don't have to |
1949 | generate a PLT at all. | |
915e5146 | 1950 | |
e4a2175c HPN |
1951 | - When there are both (ordinary) PLT references and GOT references, |
1952 | and this isn't the executable. | |
915e5146 | 1953 | We want to make the PLT reference use the ordinary GOT entry rather |
e4a2175c HPN |
1954 | than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry, |
1955 | since the GOT entry will have to be resolved at startup anyway. | |
915e5146 HPN |
1956 | |
1957 | Though the latter case is handled when room for the PLT is allocated, | |
1958 | not here. | |
1959 | ||
e4a2175c HPN |
1960 | By folding into the GOT, we may need a round-trip to a PLT in the |
1961 | executable for calls, a loss in performance. Still, losing a | |
1962 | reloc is a win in size and at least in start-up time. | |
1963 | ||
915e5146 HPN |
1964 | Note that this function is called before symbols are forced local by |
1965 | version scripts. The differing cases are handled by | |
1966 | elf_cris_hide_symbol. */ | |
1967 | ||
b34976b6 | 1968 | static bfd_boolean |
915e5146 HPN |
1969 | elf_cris_try_fold_plt_to_got (h, p) |
1970 | struct elf_cris_link_hash_entry *h; | |
1971 | PTR p; | |
1972 | { | |
1973 | struct bfd_link_info *info = (struct bfd_link_info *) p; | |
1974 | ||
1975 | /* If there are no GOT references for this symbol, we can't fold any | |
1976 | other reference so there's nothing to do. Likewise if there are no | |
1977 | PLT references; GOTPLT references included. */ | |
1978 | if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0) | |
b34976b6 | 1979 | return TRUE; |
915e5146 HPN |
1980 | |
1981 | /* GOTPLT relocs are supposed to be included into the PLT refcount. */ | |
1982 | BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount); | |
1983 | ||
1984 | if (h->gotplt_refcount == h->root.plt.refcount) | |
1985 | { | |
4d96d128 | 1986 | /* The only PLT references are GOTPLT references, and there are GOT |
915e5146 HPN |
1987 | references. Convert PLT to GOT references. */ |
1988 | if (! elf_cris_adjust_gotplt_to_got (h, info)) | |
b34976b6 | 1989 | return FALSE; |
915e5146 HPN |
1990 | |
1991 | /* Clear the PLT references, so no PLT will be created. */ | |
1992 | h->root.plt.offset = (bfd_vma) -1; | |
1993 | } | |
1994 | ||
b34976b6 | 1995 | return TRUE; |
915e5146 HPN |
1996 | } |
1997 | ||
1998 | /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc | |
1999 | to use a GOT entry (and create one) rather than requiring a GOTPLT | |
2000 | entry. */ | |
2001 | ||
2002 | static void | |
e5094212 | 2003 | elf_cris_hide_symbol (info, h, force_local) |
915e5146 HPN |
2004 | struct bfd_link_info *info; |
2005 | struct elf_link_hash_entry *h; | |
b34976b6 | 2006 | bfd_boolean force_local; |
915e5146 HPN |
2007 | { |
2008 | elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info); | |
2009 | ||
e5094212 | 2010 | _bfd_elf_link_hash_hide_symbol (info, h, force_local); |
915e5146 HPN |
2011 | } |
2012 | ||
2013 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
2014 | regular object. The current definition is in some section of the | |
2015 | dynamic object, but we're not including those sections. We have to | |
2016 | change the definition to something the rest of the link can | |
2017 | understand. */ | |
2018 | ||
b34976b6 | 2019 | static bfd_boolean |
915e5146 HPN |
2020 | elf_cris_adjust_dynamic_symbol (info, h) |
2021 | struct bfd_link_info *info; | |
2022 | struct elf_link_hash_entry *h; | |
2023 | { | |
2024 | bfd *dynobj; | |
2025 | asection *s; | |
2026 | unsigned int power_of_two; | |
2027 | ||
2028 | dynobj = elf_hash_table (info)->dynobj; | |
2029 | ||
2030 | /* Make sure we know what is going on here. */ | |
2031 | BFD_ASSERT (dynobj != NULL | |
2032 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) | |
2033 | || h->weakdef != NULL | |
2034 | || ((h->elf_link_hash_flags | |
2035 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
2036 | && (h->elf_link_hash_flags | |
2037 | & ELF_LINK_HASH_REF_REGULAR) != 0 | |
2038 | && (h->elf_link_hash_flags | |
2039 | & ELF_LINK_HASH_DEF_REGULAR) == 0))); | |
2040 | ||
2041 | /* If this is a function, put it in the procedure linkage table. We | |
2042 | will fill in the contents of the procedure linkage table later, | |
2043 | when we know the address of the .got section. */ | |
2044 | if (h->type == STT_FUNC | |
2045 | || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) | |
2046 | { | |
6849c52f HPN |
2047 | /* If we link a program (not a DSO), we'll get rid of unnecessary |
2048 | PLT entries; we point to the actual symbols -- even for pic | |
2049 | relocs, because a program built with -fpic should have the same | |
2050 | result as one built without -fpic, specifically considering weak | |
2051 | symbols. | |
2052 | FIXME: m68k and i386 differ here, for unclear reasons. */ | |
915e5146 | 2053 | if (! info->shared |
6849c52f | 2054 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0) |
915e5146 HPN |
2055 | { |
2056 | /* This case can occur if we saw a PLT reloc in an input file, | |
6849c52f HPN |
2057 | but the symbol was not defined by a dynamic object. In such |
2058 | a case, we don't actually need to build a procedure linkage | |
2059 | table, and we can just do an absolute or PC reloc instead, or | |
4d96d128 | 2060 | change a .got.plt index to a .got index for GOTPLT relocs. */ |
915e5146 | 2061 | BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0); |
6849c52f | 2062 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; |
915e5146 | 2063 | h->plt.offset = (bfd_vma) -1; |
915e5146 HPN |
2064 | return |
2065 | elf_cris_adjust_gotplt_to_got ((struct | |
2066 | elf_cris_link_hash_entry *) h, | |
2067 | info); | |
2068 | } | |
2069 | ||
e4a2175c HPN |
2070 | /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT; |
2071 | where a pointer-equivalent symbol was unimportant (i.e. more | |
2072 | like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid | |
2073 | of the PLT. We can't for the executable, because the GOT | |
2074 | entries will point to the PLT there (and be constant). */ | |
2075 | if (info->shared | |
2076 | && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*) | |
2077 | h, info)) | |
b34976b6 | 2078 | return FALSE; |
915e5146 HPN |
2079 | |
2080 | /* GC or folding may have rendered this entry unused. */ | |
2081 | if (h->plt.refcount <= 0) | |
2082 | { | |
2083 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
2084 | h->plt.offset = (bfd_vma) -1; | |
b34976b6 | 2085 | return TRUE; |
915e5146 HPN |
2086 | } |
2087 | ||
2088 | /* Make sure this symbol is output as a dynamic symbol. */ | |
2089 | if (h->dynindx == -1) | |
2090 | { | |
c152c796 | 2091 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 2092 | return FALSE; |
915e5146 HPN |
2093 | } |
2094 | ||
2095 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
2096 | BFD_ASSERT (s != NULL); | |
2097 | ||
2098 | /* If this is the first .plt entry, make room for the special | |
2099 | first entry. */ | |
eea6121a AM |
2100 | if (s->size == 0) |
2101 | s->size += PLT_ENTRY_SIZE; | |
915e5146 HPN |
2102 | |
2103 | /* If this symbol is not defined in a regular file, and we are | |
2104 | not generating a shared library, then set the symbol to this | |
6849c52f | 2105 | location in the .plt. */ |
915e5146 HPN |
2106 | if (!info->shared |
2107 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
2108 | { | |
2109 | h->root.u.def.section = s; | |
eea6121a | 2110 | h->root.u.def.value = s->size; |
915e5146 HPN |
2111 | } |
2112 | ||
4d96d128 HPN |
2113 | /* If there's already a GOT entry, use that, not a .got.plt. A |
2114 | GOT field still has a reference count when we get here; it's | |
e4a2175c HPN |
2115 | not yet changed to an offset. We can't do this for an |
2116 | executable, because then the reloc associated with the PLT | |
2117 | would get a non-PLT reloc pointing to the PLT. FIXME: Move | |
2118 | this to elf_cris_try_fold_plt_to_got. */ | |
2119 | if (info->shared && h->got.refcount > 0) | |
915e5146 HPN |
2120 | { |
2121 | h->got.refcount += h->plt.refcount; | |
2122 | ||
2123 | /* Mark the PLT offset to use the GOT entry by setting the low | |
2124 | bit in the plt offset; it is always a multiple of | |
2125 | pointer-size. */ | |
eea6121a | 2126 | BFD_ASSERT ((s->size & 3) == 0); |
915e5146 HPN |
2127 | |
2128 | /* Change the PLT refcount to an offset. */ | |
eea6121a | 2129 | h->plt.offset = s->size; |
915e5146 HPN |
2130 | |
2131 | /* By not setting gotplt_offset (i.e. it remains at 0), we signal | |
2132 | that the got entry should be used instead. */ | |
2133 | BFD_ASSERT (((struct elf_cris_link_hash_entry *) | |
2134 | h)->gotplt_offset == 0); | |
2135 | ||
2136 | /* Make room for this entry. */ | |
eea6121a | 2137 | s->size += PLT_ENTRY_SIZE; |
915e5146 | 2138 | |
b34976b6 | 2139 | return TRUE; |
915e5146 HPN |
2140 | } |
2141 | ||
2142 | /* No GOT reference for this symbol; prepare for an ordinary PLT. */ | |
eea6121a | 2143 | h->plt.offset = s->size; |
915e5146 HPN |
2144 | |
2145 | /* Make room for this entry. */ | |
eea6121a | 2146 | s->size += PLT_ENTRY_SIZE; |
915e5146 HPN |
2147 | |
2148 | /* We also need to make an entry in the .got.plt section, which | |
2149 | will be placed in the .got section by the linker script. */ | |
2150 | ((struct elf_cris_link_hash_entry *) h)->gotplt_offset | |
2151 | = elf_cris_hash_table (info)->next_gotplt_entry; | |
2152 | elf_cris_hash_table (info)->next_gotplt_entry += 4; | |
2153 | ||
2154 | s = bfd_get_section_by_name (dynobj, ".got.plt"); | |
2155 | BFD_ASSERT (s != NULL); | |
eea6121a | 2156 | s->size += 4; |
915e5146 HPN |
2157 | |
2158 | /* We also need to make an entry in the .rela.plt section. */ | |
2159 | ||
2160 | s = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
2161 | BFD_ASSERT (s != NULL); | |
eea6121a | 2162 | s->size += sizeof (Elf32_External_Rela); |
915e5146 | 2163 | |
b34976b6 | 2164 | return TRUE; |
915e5146 HPN |
2165 | } |
2166 | ||
2167 | /* Reinitialize the plt offset now that it is not used as a reference | |
2168 | count any more. */ | |
2169 | h->plt.offset = (bfd_vma) -1; | |
2170 | ||
2171 | /* If this is a weak symbol, and there is a real definition, the | |
2172 | processor independent code will have arranged for us to see the | |
2173 | real definition first, and we can just use the same value. */ | |
2174 | if (h->weakdef != NULL) | |
2175 | { | |
2176 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined | |
2177 | || h->weakdef->root.type == bfd_link_hash_defweak); | |
2178 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
2179 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
b34976b6 | 2180 | return TRUE; |
915e5146 HPN |
2181 | } |
2182 | ||
2183 | /* This is a reference to a symbol defined by a dynamic object which | |
2184 | is not a function. */ | |
2185 | ||
2186 | /* If we are creating a shared library, we must presume that the | |
2187 | only references to the symbol are via the global offset table. | |
2188 | For such cases we need not do anything here; the relocations will | |
2189 | be handled correctly by relocate_section. */ | |
2190 | if (info->shared) | |
b34976b6 | 2191 | return TRUE; |
915e5146 HPN |
2192 | |
2193 | /* If there are no references to this symbol that do not use the | |
2194 | GOT, we don't need to generate a copy reloc. */ | |
2195 | if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0) | |
b34976b6 | 2196 | return TRUE; |
915e5146 HPN |
2197 | |
2198 | /* We must allocate the symbol in our .dynbss section, which will | |
2199 | become part of the .bss section of the executable. There will be | |
2200 | an entry for this symbol in the .dynsym section. The dynamic | |
2201 | object will contain position independent code, so all references | |
2202 | from the dynamic object to this symbol will go through the global | |
2203 | offset table. The dynamic linker will use the .dynsym entry to | |
2204 | determine the address it must put in the global offset table, so | |
2205 | both the dynamic object and the regular object will refer to the | |
2206 | same memory location for the variable. */ | |
2207 | ||
2208 | s = bfd_get_section_by_name (dynobj, ".dynbss"); | |
2209 | BFD_ASSERT (s != NULL); | |
2210 | ||
2211 | /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to | |
2212 | copy the initial value out of the dynamic object and into the | |
2213 | runtime process image. We need to remember the offset into the | |
2214 | .rela.bss section we are going to use. */ | |
2215 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) | |
2216 | { | |
2217 | asection *srel; | |
2218 | ||
2219 | srel = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
2220 | BFD_ASSERT (srel != NULL); | |
eea6121a | 2221 | srel->size += sizeof (Elf32_External_Rela); |
915e5146 HPN |
2222 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; |
2223 | } | |
2224 | ||
2225 | /* Historic precedent: m68k and i386 allow max 8-byte alignment for the | |
2226 | thing to copy; so do we. */ | |
2227 | ||
2228 | /* We need to figure out the alignment required for this symbol. I | |
2229 | have no idea how ELF linkers handle this. */ | |
2230 | power_of_two = bfd_log2 (h->size); | |
2231 | if (power_of_two > 3) | |
2232 | power_of_two = 3; | |
2233 | ||
2234 | /* Apply the required alignment. */ | |
eea6121a | 2235 | s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two)); |
915e5146 HPN |
2236 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) |
2237 | { | |
2238 | if (!bfd_set_section_alignment (dynobj, s, power_of_two)) | |
b34976b6 | 2239 | return FALSE; |
915e5146 HPN |
2240 | } |
2241 | ||
2242 | /* Define the symbol as being at this point in the section. */ | |
2243 | h->root.u.def.section = s; | |
eea6121a | 2244 | h->root.u.def.value = s->size; |
915e5146 HPN |
2245 | |
2246 | /* Increment the section size to make room for the symbol. */ | |
eea6121a | 2247 | s->size += h->size; |
915e5146 | 2248 | |
b34976b6 | 2249 | return TRUE; |
915e5146 HPN |
2250 | } |
2251 | ||
2252 | /* Look through the relocs for a section during the first phase. */ | |
a7c10850 | 2253 | |
b34976b6 | 2254 | static bfd_boolean |
06c15ad7 HPN |
2255 | cris_elf_check_relocs (abfd, info, sec, relocs) |
2256 | bfd *abfd; | |
2257 | struct bfd_link_info *info; | |
2258 | asection *sec; | |
2259 | const Elf_Internal_Rela *relocs; | |
2260 | { | |
915e5146 | 2261 | bfd *dynobj; |
06c15ad7 HPN |
2262 | Elf_Internal_Shdr *symtab_hdr; |
2263 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
915e5146 | 2264 | bfd_signed_vma *local_got_refcounts; |
06c15ad7 HPN |
2265 | const Elf_Internal_Rela *rel; |
2266 | const Elf_Internal_Rela *rel_end; | |
915e5146 HPN |
2267 | asection *sgot; |
2268 | asection *srelgot; | |
2269 | asection *sreloc; | |
a7c10850 | 2270 | |
1049f94e | 2271 | if (info->relocatable) |
b34976b6 | 2272 | return TRUE; |
a7c10850 | 2273 | |
915e5146 | 2274 | dynobj = elf_hash_table (info)->dynobj; |
06c15ad7 HPN |
2275 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2276 | sym_hashes = elf_sym_hashes (abfd); | |
a7c10850 | 2277 | sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym); |
915e5146 HPN |
2278 | local_got_refcounts = elf_local_got_refcounts (abfd); |
2279 | ||
2280 | sgot = NULL; | |
2281 | srelgot = NULL; | |
2282 | sreloc = NULL; | |
2283 | ||
06c15ad7 HPN |
2284 | if (!elf_bad_symtab (abfd)) |
2285 | sym_hashes_end -= symtab_hdr->sh_info; | |
a7c10850 | 2286 | |
06c15ad7 HPN |
2287 | rel_end = relocs + sec->reloc_count; |
2288 | for (rel = relocs; rel < rel_end; rel++) | |
2289 | { | |
2290 | struct elf_link_hash_entry *h; | |
2291 | unsigned long r_symndx; | |
915e5146 | 2292 | enum elf_cris_reloc_type r_type; |
a7c10850 | 2293 | |
06c15ad7 HPN |
2294 | r_symndx = ELF32_R_SYM (rel->r_info); |
2295 | if (r_symndx < symtab_hdr->sh_info) | |
2296 | h = NULL; | |
2297 | else | |
2298 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
a7c10850 | 2299 | |
915e5146 HPN |
2300 | r_type = ELF32_R_TYPE (rel->r_info); |
2301 | ||
2302 | /* Some relocs require linker-created sections; we need to hang them | |
2303 | on the first input bfd we found that contained dynamic relocs. */ | |
2304 | switch (r_type) | |
2305 | { | |
2306 | case R_CRIS_16_GOT: | |
2307 | case R_CRIS_32_GOT: | |
2308 | case R_CRIS_32_GOTREL: | |
2309 | case R_CRIS_32_PLT_GOTREL: | |
2310 | case R_CRIS_32_PLT_PCREL: | |
2311 | case R_CRIS_16_GOTPLT: | |
2312 | case R_CRIS_32_GOTPLT: | |
2313 | if (dynobj == NULL) | |
2314 | { | |
2315 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
2316 | ||
2317 | /* Create the .got section, so we can assume it's always | |
2318 | present whenever there's a dynobj. */ | |
2319 | if (!_bfd_elf_create_got_section (dynobj, info)) | |
b34976b6 | 2320 | return FALSE; |
915e5146 HPN |
2321 | } |
2322 | break; | |
2323 | ||
2324 | default: | |
2325 | break; | |
2326 | } | |
2327 | ||
2328 | /* Some relocs require a global offset table (but perhaps not a | |
2329 | specific GOT entry). */ | |
4d96d128 | 2330 | switch (r_type) |
915e5146 | 2331 | { |
44aa49bb HPN |
2332 | /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT |
2333 | entry only for local symbols. Unfortunately, we don't know | |
2334 | until later on if there's a version script that forces the | |
2335 | symbol local. We must have the .rela.got section in place | |
2336 | before we know if the symbol looks global now, so we need | |
2337 | to treat the reloc just like for R_CRIS_16_GOT and | |
2338 | R_CRIS_32_GOT. */ | |
2339 | case R_CRIS_16_GOTPLT: | |
2340 | case R_CRIS_32_GOTPLT: | |
915e5146 HPN |
2341 | case R_CRIS_16_GOT: |
2342 | case R_CRIS_32_GOT: | |
44aa49bb HPN |
2343 | if (srelgot == NULL |
2344 | && (h != NULL || info->shared)) | |
2345 | { | |
2346 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); | |
2347 | if (srelgot == NULL) | |
2348 | { | |
2349 | srelgot = bfd_make_section (dynobj, ".rela.got"); | |
2350 | if (srelgot == NULL | |
2351 | || !bfd_set_section_flags (dynobj, srelgot, | |
2352 | (SEC_ALLOC | |
2353 | | SEC_LOAD | |
2354 | | SEC_HAS_CONTENTS | |
2355 | | SEC_IN_MEMORY | |
2356 | | SEC_LINKER_CREATED | |
2357 | | SEC_READONLY)) | |
2358 | || !bfd_set_section_alignment (dynobj, srelgot, 2)) | |
b34976b6 | 2359 | return FALSE; |
44aa49bb HPN |
2360 | } |
2361 | } | |
2362 | /* Fall through. */ | |
2363 | ||
915e5146 HPN |
2364 | case R_CRIS_32_GOTREL: |
2365 | case R_CRIS_32_PLT_GOTREL: | |
2366 | if (sgot == NULL) | |
2367 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
2368 | ||
2369 | if (local_got_refcounts == NULL) | |
2370 | { | |
dc810e39 | 2371 | bfd_size_type amt; |
915e5146 HPN |
2372 | |
2373 | /* We use index local_got_refcounts[-1] to count all | |
2374 | GOT-relative relocations that do not have explicit | |
2375 | GOT entries. */ | |
dc810e39 AM |
2376 | amt = symtab_hdr->sh_info + 1; |
2377 | amt *= sizeof (bfd_signed_vma); | |
51b64d56 | 2378 | local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt)); |
915e5146 | 2379 | if (local_got_refcounts == NULL) |
b34976b6 | 2380 | return FALSE; |
915e5146 HPN |
2381 | |
2382 | local_got_refcounts++; | |
2383 | elf_local_got_refcounts (abfd) = local_got_refcounts; | |
2384 | } | |
2385 | break; | |
2386 | ||
2387 | default: | |
2388 | break; | |
2389 | } | |
2390 | ||
4d96d128 | 2391 | switch (r_type) |
06c15ad7 | 2392 | { |
915e5146 HPN |
2393 | case R_CRIS_16_GOTPLT: |
2394 | case R_CRIS_32_GOTPLT: | |
2395 | /* Mark that we need a GOT entry if the PLT entry (and its GOT | |
2396 | entry) is eliminated. We can only do this for a non-local | |
2397 | symbol. */ | |
2398 | if (h != NULL) | |
2399 | { | |
2400 | ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++; | |
2401 | goto handle_gotplt_reloc; | |
2402 | } | |
2403 | /* If h is NULL then this is a local symbol, and we must make a | |
2404 | GOT entry for it, so handle it like a GOT reloc. */ | |
2405 | /* Fall through. */ | |
2406 | ||
2407 | case R_CRIS_16_GOT: | |
2408 | case R_CRIS_32_GOT: | |
2409 | /* This symbol requires a global offset table entry. */ | |
915e5146 HPN |
2410 | if (h != NULL) |
2411 | { | |
51b64d56 | 2412 | if (h->got.refcount == 0) |
915e5146 | 2413 | { |
915e5146 HPN |
2414 | /* Make sure this symbol is output as a dynamic symbol. */ |
2415 | if (h->dynindx == -1) | |
2416 | { | |
c152c796 | 2417 | if (!bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 2418 | return FALSE; |
915e5146 HPN |
2419 | } |
2420 | ||
2421 | /* Allocate space in the .got section. */ | |
eea6121a | 2422 | sgot->size += 4; |
915e5146 | 2423 | /* Allocate relocation space. */ |
eea6121a | 2424 | srelgot->size += sizeof (Elf32_External_Rela); |
915e5146 | 2425 | } |
51b64d56 | 2426 | h->got.refcount++; |
915e5146 HPN |
2427 | } |
2428 | else | |
2429 | { | |
2430 | /* This is a global offset table entry for a local symbol. */ | |
51b64d56 | 2431 | if (local_got_refcounts[r_symndx] == 0) |
915e5146 | 2432 | { |
eea6121a | 2433 | sgot->size += 4; |
915e5146 HPN |
2434 | if (info->shared) |
2435 | { | |
2436 | /* If we are generating a shared object, we need to | |
2437 | output a R_CRIS_RELATIVE reloc so that the dynamic | |
2438 | linker can adjust this GOT entry. */ | |
eea6121a | 2439 | srelgot->size += sizeof (Elf32_External_Rela); |
915e5146 HPN |
2440 | } |
2441 | } | |
51b64d56 | 2442 | local_got_refcounts[r_symndx]++; |
915e5146 HPN |
2443 | } |
2444 | break; | |
2445 | ||
2446 | case R_CRIS_32_GOTREL: | |
2447 | /* This reference requires a global offset table. | |
2448 | FIXME: The actual refcount isn't used currently; the .got | |
2449 | section can't be removed if there were any references in the | |
2450 | input. */ | |
2451 | local_got_refcounts[-1]++; | |
2452 | break; | |
2453 | ||
2454 | handle_gotplt_reloc: | |
2455 | ||
2456 | case R_CRIS_32_PLT_GOTREL: | |
2457 | /* This reference requires a global offset table. */ | |
2458 | local_got_refcounts[-1]++; | |
2459 | /* Fall through. */ | |
2460 | ||
2461 | case R_CRIS_32_PLT_PCREL: | |
2462 | /* This symbol requires a procedure linkage table entry. We | |
2463 | actually build the entry in adjust_dynamic_symbol, | |
2464 | because this might be a case of linking PIC code which is | |
2465 | never referenced by a dynamic object, in which case we | |
2466 | don't need to generate a procedure linkage table entry | |
2467 | after all. */ | |
2468 | ||
2469 | /* If this is a local symbol, we resolve it directly without | |
2470 | creating a procedure linkage table entry. */ | |
0a4787a0 | 2471 | if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
915e5146 HPN |
2472 | continue; |
2473 | ||
2474 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; | |
51b64d56 | 2475 | h->plt.refcount++; |
915e5146 HPN |
2476 | break; |
2477 | ||
2478 | case R_CRIS_8: | |
2479 | case R_CRIS_16: | |
2480 | case R_CRIS_32: | |
2481 | /* Let's help debug shared library creation. Any of these | |
2482 | relocs can be used in shared libs, but pages containing them | |
2483 | cannot be shared. Don't warn for sections we don't care | |
2484 | about, such as debug sections or non-constant sections. We | |
2485 | can't help tables of (global) function pointers, for example, | |
2486 | though they must be emitted in a data section to avoid having | |
2487 | impure text sections. */ | |
2488 | if (info->shared | |
2489 | && (sec->flags & SEC_ALLOC) != 0 | |
2490 | && (sec->flags & SEC_READONLY) != 0) | |
2491 | { | |
2492 | /* FIXME: How do we make this optionally a warning only? */ | |
8f615d07 AM |
2493 | (*_bfd_error_handler) |
2494 | (_("%s, section %s:\n relocation %s should not be used in a shared object; recompile with -fPIC"), | |
2495 | bfd_archive_filename (abfd), | |
2496 | sec->name, | |
2497 | cris_elf_howto_table[r_type].name); | |
915e5146 HPN |
2498 | } |
2499 | /* Fall through. */ | |
2500 | ||
2501 | case R_CRIS_8_PCREL: | |
2502 | case R_CRIS_16_PCREL: | |
2503 | case R_CRIS_32_PCREL: | |
2504 | if (h != NULL) | |
2505 | { | |
2506 | h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF; | |
2507 | ||
2508 | /* Make sure a plt entry is created for this symbol if it | |
2509 | turns out to be a function defined by a dynamic object. */ | |
0a4787a0 HPN |
2510 | if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) |
2511 | h->plt.refcount++; | |
915e5146 HPN |
2512 | } |
2513 | ||
2514 | /* If we are creating a shared library and this is not a local | |
2515 | symbol, we need to copy the reloc into the shared library. | |
2516 | However when linking with -Bsymbolic and this is a global | |
2517 | symbol which is defined in an object we are including in the | |
2518 | link (i.e., DEF_REGULAR is set), then we can resolve the | |
2519 | reloc directly. At this point we have not seen all the input | |
2520 | files, so it is possible that DEF_REGULAR is not set now but | |
2521 | will be set later (it is never cleared). In case of a weak | |
2522 | definition, DEF_REGULAR may be cleared later by a strong | |
2523 | definition in a shared library. We account for that | |
2524 | possibility below by storing information in the relocs_copied | |
2525 | field of the hash table entry. A similar situation occurs | |
2526 | when creating shared libraries and symbol visibility changes | |
2527 | render the symbol local. */ | |
2528 | ||
2529 | /* No need to do anything if we're not creating a shared object. */ | |
2530 | if (! info->shared) | |
2531 | break; | |
2532 | ||
2533 | /* We don't need to handle relocs into sections not going into | |
2534 | the "real" output. */ | |
2535 | if ((sec->flags & SEC_ALLOC) == 0) | |
2536 | break; | |
2537 | ||
2538 | /* We can only eliminate PC-relative relocs. */ | |
2539 | if (r_type == R_CRIS_8_PCREL | |
2540 | || r_type == R_CRIS_16_PCREL | |
2541 | || r_type == R_CRIS_32_PCREL) | |
2542 | { | |
2543 | /* If the symbol is local, then we can eliminate the reloc. */ | |
0a4787a0 | 2544 | if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) |
915e5146 HPN |
2545 | break; |
2546 | ||
2547 | /* If this is with -Bsymbolic and the symbol isn't weak, and | |
2548 | is defined by an ordinary object (the ones we include in | |
2549 | this shared library) then we can also eliminate the | |
2550 | reloc. See comment above for more eliminable cases which | |
2551 | we can't identify at this time. */ | |
2552 | if (info->symbolic | |
2553 | && h->root.type != bfd_link_hash_defweak | |
2554 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) | |
2555 | break; | |
2556 | } | |
2557 | ||
2558 | /* We create a reloc section in dynobj and make room for this | |
2559 | reloc. */ | |
2560 | if (sreloc == NULL) | |
2561 | { | |
2562 | const char *name; | |
2563 | ||
2564 | name = (bfd_elf_string_from_elf_section | |
2565 | (abfd, | |
2566 | elf_elfheader (abfd)->e_shstrndx, | |
2567 | elf_section_data (sec)->rel_hdr.sh_name)); | |
2568 | if (name == NULL) | |
b34976b6 | 2569 | return FALSE; |
915e5146 HPN |
2570 | |
2571 | BFD_ASSERT (strncmp (name, ".rela", 5) == 0 | |
2572 | && strcmp (bfd_get_section_name (abfd, sec), | |
2573 | name + 5) == 0); | |
2574 | ||
2575 | sreloc = bfd_get_section_by_name (dynobj, name); | |
2576 | if (sreloc == NULL) | |
2577 | { | |
2578 | sreloc = bfd_make_section (dynobj, name); | |
2579 | if (sreloc == NULL | |
2580 | || !bfd_set_section_flags (dynobj, sreloc, | |
2581 | (SEC_ALLOC | |
2582 | | SEC_LOAD | |
2583 | | SEC_HAS_CONTENTS | |
2584 | | SEC_IN_MEMORY | |
2585 | | SEC_LINKER_CREATED | |
2586 | | SEC_READONLY)) | |
2587 | || !bfd_set_section_alignment (dynobj, sreloc, 2)) | |
b34976b6 | 2588 | return FALSE; |
915e5146 | 2589 | } |
99e4ae17 AJ |
2590 | if (sec->flags & SEC_READONLY) |
2591 | info->flags |= DF_TEXTREL; | |
915e5146 HPN |
2592 | } |
2593 | ||
eea6121a | 2594 | sreloc->size += sizeof (Elf32_External_Rela); |
915e5146 HPN |
2595 | |
2596 | /* If we are linking with -Bsymbolic, we count the number of PC | |
2597 | relative relocations we have entered for this symbol, so that | |
2598 | we can discard them again if the symbol is later defined by a | |
2599 | regular object. We know that h is really a pointer to an | |
2600 | elf_cris_link_hash_entry. */ | |
4d96d128 HPN |
2601 | if ((r_type == R_CRIS_8_PCREL |
2602 | || r_type == R_CRIS_16_PCREL | |
2603 | || r_type == R_CRIS_32_PCREL) | |
915e5146 HPN |
2604 | && info->symbolic) |
2605 | { | |
2606 | struct elf_cris_link_hash_entry *eh; | |
2607 | struct elf_cris_pcrel_relocs_copied *p; | |
2608 | ||
2609 | eh = (struct elf_cris_link_hash_entry *) h; | |
2610 | ||
2611 | for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next) | |
2612 | if (p->section == sreloc) | |
2613 | break; | |
2614 | ||
2615 | if (p == NULL) | |
2616 | { | |
2617 | p = ((struct elf_cris_pcrel_relocs_copied *) | |
dc810e39 | 2618 | bfd_alloc (dynobj, (bfd_size_type) sizeof *p)); |
915e5146 | 2619 | if (p == NULL) |
b34976b6 | 2620 | return FALSE; |
915e5146 HPN |
2621 | p->next = eh->pcrel_relocs_copied; |
2622 | eh->pcrel_relocs_copied = p; | |
2623 | p->section = sreloc; | |
2624 | p->count = 0; | |
2625 | } | |
2626 | ||
2627 | ++p->count; | |
2628 | } | |
2629 | break; | |
2630 | ||
06c15ad7 HPN |
2631 | /* This relocation describes the C++ object vtable hierarchy. |
2632 | Reconstruct it for later use during GC. */ | |
2633 | case R_CRIS_GNU_VTINHERIT: | |
c152c796 | 2634 | if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) |
b34976b6 | 2635 | return FALSE; |
06c15ad7 | 2636 | break; |
a7c10850 | 2637 | |
06c15ad7 HPN |
2638 | /* This relocation describes which C++ vtable entries are actually |
2639 | used. Record for later use during GC. */ | |
2640 | case R_CRIS_GNU_VTENTRY: | |
c152c796 | 2641 | if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) |
b34976b6 | 2642 | return FALSE; |
06c15ad7 | 2643 | break; |
4d96d128 HPN |
2644 | |
2645 | default: | |
2646 | /* Other relocs do not appear here. */ | |
2647 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 2648 | return FALSE; |
06c15ad7 HPN |
2649 | } |
2650 | } | |
a7c10850 | 2651 | |
b34976b6 | 2652 | return TRUE; |
06c15ad7 | 2653 | } |
4da81684 | 2654 | |
915e5146 HPN |
2655 | /* Set the sizes of the dynamic sections. */ |
2656 | ||
b34976b6 | 2657 | static bfd_boolean |
915e5146 | 2658 | elf_cris_size_dynamic_sections (output_bfd, info) |
99e4ae17 | 2659 | bfd *output_bfd ATTRIBUTE_UNUSED; |
915e5146 HPN |
2660 | struct bfd_link_info *info; |
2661 | { | |
2662 | bfd *dynobj; | |
2663 | asection *s; | |
b34976b6 AM |
2664 | bfd_boolean plt; |
2665 | bfd_boolean relocs; | |
915e5146 HPN |
2666 | |
2667 | dynobj = elf_hash_table (info)->dynobj; | |
2668 | BFD_ASSERT (dynobj != NULL); | |
2669 | ||
2670 | if (elf_hash_table (info)->dynamic_sections_created) | |
2671 | { | |
2672 | /* Set the contents of the .interp section to the interpreter. */ | |
893c4fe2 | 2673 | if (info->executable) |
915e5146 HPN |
2674 | { |
2675 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
2676 | BFD_ASSERT (s != NULL); | |
eea6121a | 2677 | s->size = sizeof ELF_DYNAMIC_INTERPRETER; |
915e5146 HPN |
2678 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; |
2679 | } | |
2680 | } | |
2681 | else | |
2682 | { | |
2683 | /* Adjust all expected GOTPLT uses to use a GOT entry instead. */ | |
2684 | elf_cris_link_hash_traverse (elf_cris_hash_table (info), | |
2685 | elf_cris_adjust_gotplt_to_got, | |
2686 | (PTR) info); | |
2687 | ||
2688 | /* We may have created entries in the .rela.got section. | |
2689 | However, if we are not creating the dynamic sections, we will | |
2690 | not actually use these entries. Reset the size of .rela.got, | |
2691 | which will cause it to get stripped from the output file | |
2692 | below. */ | |
2693 | s = bfd_get_section_by_name (dynobj, ".rela.got"); | |
2694 | if (s != NULL) | |
eea6121a | 2695 | s->size = 0; |
915e5146 HPN |
2696 | } |
2697 | ||
2698 | /* If this is a -Bsymbolic shared link, then we need to discard all PC | |
2699 | relative relocs against symbols defined in a regular object. We | |
2700 | allocated space for them in the check_relocs routine, but we will not | |
2701 | fill them in in the relocate_section routine. We also discard space | |
2702 | for relocs that have become for local symbols due to symbol | |
4d96d128 HPN |
2703 | visibility changes. For programs, we discard space for relocs for |
2704 | symbols not referenced by any dynamic object. */ | |
915e5146 HPN |
2705 | if (info->shared) |
2706 | elf_cris_link_hash_traverse (elf_cris_hash_table (info), | |
4d96d128 HPN |
2707 | elf_cris_discard_excess_dso_dynamics, |
2708 | (PTR) info); | |
2709 | else | |
2710 | elf_cris_link_hash_traverse (elf_cris_hash_table (info), | |
2711 | elf_cris_discard_excess_program_dynamics, | |
915e5146 HPN |
2712 | (PTR) info); |
2713 | ||
2714 | /* The check_relocs and adjust_dynamic_symbol entry points have | |
2715 | determined the sizes of the various dynamic sections. Allocate | |
2716 | memory for them. */ | |
b34976b6 AM |
2717 | plt = FALSE; |
2718 | relocs = FALSE; | |
915e5146 HPN |
2719 | for (s = dynobj->sections; s != NULL; s = s->next) |
2720 | { | |
2721 | const char *name; | |
b34976b6 | 2722 | bfd_boolean strip; |
915e5146 HPN |
2723 | |
2724 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
2725 | continue; | |
2726 | ||
2727 | /* It's OK to base decisions on the section name, because none | |
2728 | of the dynobj section names depend upon the input files. */ | |
2729 | name = bfd_get_section_name (dynobj, s); | |
2730 | ||
b34976b6 | 2731 | strip = FALSE; |
915e5146 HPN |
2732 | |
2733 | if (strcmp (name, ".plt") == 0) | |
2734 | { | |
eea6121a | 2735 | if (s->size == 0) |
915e5146 HPN |
2736 | { |
2737 | /* Strip this section if we don't need it; see the | |
2738 | comment below. */ | |
b34976b6 | 2739 | strip = TRUE; |
915e5146 HPN |
2740 | } |
2741 | else | |
2742 | { | |
2743 | /* Remember whether there is a PLT. */ | |
b34976b6 | 2744 | plt = TRUE; |
915e5146 HPN |
2745 | } |
2746 | } | |
2747 | else if (strncmp (name, ".rela", 5) == 0) | |
2748 | { | |
eea6121a | 2749 | if (s->size == 0) |
915e5146 HPN |
2750 | { |
2751 | /* If we don't need this section, strip it from the | |
2752 | output file. This is mostly to handle .rela.bss and | |
2753 | .rela.plt. We must create both sections in | |
2754 | create_dynamic_sections, because they must be created | |
2755 | before the linker maps input sections to output | |
2756 | sections. The linker does that before | |
2757 | adjust_dynamic_symbol is called, and it is that | |
2758 | function which decides whether anything needs to go | |
2759 | into these sections. */ | |
b34976b6 | 2760 | strip = TRUE; |
915e5146 HPN |
2761 | } |
2762 | else | |
2763 | { | |
915e5146 HPN |
2764 | /* Remember whether there are any reloc sections other |
2765 | than .rela.plt. */ | |
2766 | if (strcmp (name, ".rela.plt") != 0) | |
b34976b6 | 2767 | relocs = TRUE; |
915e5146 | 2768 | |
915e5146 HPN |
2769 | /* We use the reloc_count field as a counter if we need |
2770 | to copy relocs into the output file. */ | |
2771 | s->reloc_count = 0; | |
2772 | } | |
2773 | } | |
2774 | else if (strncmp (name, ".got", 4) != 0) | |
2775 | { | |
2776 | /* It's not one of our sections, so don't allocate space. */ | |
2777 | continue; | |
2778 | } | |
2779 | ||
2780 | if (strip) | |
2781 | { | |
2782 | _bfd_strip_section_from_output (info, s); | |
2783 | continue; | |
2784 | } | |
2785 | ||
2786 | /* Allocate memory for the section contents. We use bfd_zalloc here | |
2787 | in case unused entries are not reclaimed before the section's | |
2788 | contents are written out. This should not happen, but this way | |
2789 | if it does, we will not write out garbage. For reloc sections, | |
2790 | this will make entries have the type R_CRIS_NONE. */ | |
eea6121a AM |
2791 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); |
2792 | if (s->contents == NULL && s->size != 0) | |
b34976b6 | 2793 | return FALSE; |
915e5146 HPN |
2794 | } |
2795 | ||
2796 | if (elf_hash_table (info)->dynamic_sections_created) | |
2797 | { | |
2798 | /* Add some entries to the .dynamic section. We fill in the | |
2799 | values later, in elf_cris_finish_dynamic_sections, but we | |
2800 | must add the entries now so that we get the correct size for | |
2801 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
2802 | dynamic linker and used by the debugger. */ | |
dc810e39 | 2803 | #define add_dynamic_entry(TAG, VAL) \ |
5a580b3a | 2804 | _bfd_elf_add_dynamic_entry (info, TAG, VAL) |
dc810e39 | 2805 | |
915e5146 HPN |
2806 | if (!info->shared) |
2807 | { | |
dc810e39 | 2808 | if (!add_dynamic_entry (DT_DEBUG, 0)) |
b34976b6 | 2809 | return FALSE; |
915e5146 HPN |
2810 | } |
2811 | ||
2812 | if (plt) | |
2813 | { | |
dc810e39 AM |
2814 | if (!add_dynamic_entry (DT_PLTGOT, 0) |
2815 | || !add_dynamic_entry (DT_PLTRELSZ, 0) | |
2816 | || !add_dynamic_entry (DT_PLTREL, DT_RELA) | |
2817 | || !add_dynamic_entry (DT_JMPREL, 0)) | |
b34976b6 | 2818 | return FALSE; |
915e5146 HPN |
2819 | } |
2820 | ||
2821 | if (relocs) | |
2822 | { | |
dc810e39 AM |
2823 | if (!add_dynamic_entry (DT_RELA, 0) |
2824 | || !add_dynamic_entry (DT_RELASZ, 0) | |
2825 | || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))) | |
b34976b6 | 2826 | return FALSE; |
915e5146 HPN |
2827 | } |
2828 | ||
99e4ae17 | 2829 | if ((info->flags & DF_TEXTREL) != 0) |
915e5146 | 2830 | { |
dc810e39 | 2831 | if (!add_dynamic_entry (DT_TEXTREL, 0)) |
b34976b6 | 2832 | return FALSE; |
915e5146 HPN |
2833 | info->flags |= DF_TEXTREL; |
2834 | } | |
2835 | } | |
dc810e39 | 2836 | #undef add_dynamic_entry |
915e5146 | 2837 | |
b34976b6 | 2838 | return TRUE; |
915e5146 HPN |
2839 | } |
2840 | ||
2841 | /* This function is called via elf_cris_link_hash_traverse if we are | |
2842 | creating a shared object. In the -Bsymbolic case, it discards the | |
2843 | space allocated to copy PC relative relocs against symbols which | |
2844 | are defined in regular objects. For the normal non-symbolic case, | |
2845 | we also discard space for relocs that have become local due to | |
2846 | symbol visibility changes. We allocated space for them in the | |
2847 | check_relocs routine, but we won't fill them in in the | |
2848 | relocate_section routine. */ | |
2849 | ||
b34976b6 | 2850 | static bfd_boolean |
4d96d128 | 2851 | elf_cris_discard_excess_dso_dynamics (h, inf) |
915e5146 HPN |
2852 | struct elf_cris_link_hash_entry *h; |
2853 | PTR inf; | |
2854 | { | |
2855 | struct elf_cris_pcrel_relocs_copied *s; | |
2856 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
2857 | ||
e92d460e AM |
2858 | if (h->root.root.type == bfd_link_hash_warning) |
2859 | h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link; | |
2860 | ||
915e5146 HPN |
2861 | /* If a symbol has been forced local or we have found a regular |
2862 | definition for the symbolic link case, then we won't be needing | |
2863 | any relocs. */ | |
2864 | if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
2865 | && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0 | |
2866 | || info->symbolic)) | |
2867 | { | |
2868 | for (s = h->pcrel_relocs_copied; s != NULL; s = s->next) | |
eea6121a | 2869 | s->section->size -= s->count * sizeof (Elf32_External_Rela); |
4d96d128 HPN |
2870 | } |
2871 | ||
b34976b6 | 2872 | return TRUE; |
4d96d128 HPN |
2873 | } |
2874 | ||
2875 | /* This function is called via elf_cris_link_hash_traverse if we are *not* | |
2876 | creating a shared object. We discard space for relocs for symbols put | |
2877 | in the .got, but which we found we do not have to resolve at run-time. */ | |
2878 | ||
b34976b6 | 2879 | static bfd_boolean |
4d96d128 HPN |
2880 | elf_cris_discard_excess_program_dynamics (h, inf) |
2881 | struct elf_cris_link_hash_entry *h; | |
2882 | PTR inf; | |
2883 | { | |
2884 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
2885 | ||
e92d460e AM |
2886 | if (h->root.root.type == bfd_link_hash_warning) |
2887 | h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link; | |
2888 | ||
4d96d128 HPN |
2889 | /* If we're not creating a shared library and have a symbol which is |
2890 | referred to by .got references, but the symbol is defined locally, | |
e4a2175c HPN |
2891 | (or rather, not defined by a DSO) then lose the reloc for the .got |
2892 | (don't allocate room for it). Likewise for relocs for something | |
2893 | for which we create a PLT. */ | |
2894 | if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
2895 | || h->root.plt.refcount > 0) | |
4d96d128 HPN |
2896 | { |
2897 | if (h->root.got.refcount > 0 | |
2898 | /* The size of this section is only valid and in sync with the | |
2899 | various reference counts if we do dynamic; don't decrement it | |
2900 | otherwise. */ | |
2901 | && elf_hash_table (info)->dynamic_sections_created) | |
2902 | { | |
2903 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
2904 | asection *srelgot; | |
2905 | ||
2906 | BFD_ASSERT (dynobj != NULL); | |
99c79b2e | 2907 | |
4d96d128 HPN |
2908 | srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); |
2909 | ||
2910 | BFD_ASSERT (srelgot != NULL); | |
2911 | ||
eea6121a | 2912 | srelgot->size -= sizeof (Elf32_External_Rela); |
4d96d128 HPN |
2913 | } |
2914 | ||
2915 | /* If the locally-defined symbol isn't used by a DSO, then we don't | |
2916 | have to export it as a dynamic symbol. This was already done for | |
2917 | functions; doing this for all symbols would presumably not | |
e5dfef09 HPN |
2918 | introduce new problems. Of course we don't do this if we're |
2919 | exporting all dynamic symbols. */ | |
0741c7be | 2920 | if (! info->export_dynamic |
e4a2175c HPN |
2921 | && (h->root.elf_link_hash_flags |
2922 | & (ELF_LINK_HASH_DEF_DYNAMIC|ELF_LINK_HASH_REF_DYNAMIC)) == 0) | |
e5dfef09 HPN |
2923 | { |
2924 | h->root.dynindx = -1; | |
2925 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
2926 | h->root.dynstr_index); | |
2927 | } | |
915e5146 HPN |
2928 | } |
2929 | ||
b34976b6 | 2930 | return TRUE; |
915e5146 HPN |
2931 | } |
2932 | ||
2933 | /* Reject a file depending on presence and expectation of prefixed | |
2934 | underscores on symbols. */ | |
4da81684 | 2935 | |
b34976b6 | 2936 | static bfd_boolean |
4da81684 HPN |
2937 | cris_elf_object_p (abfd) |
2938 | bfd *abfd; | |
2939 | { | |
2940 | if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)) | |
2941 | return (bfd_get_symbol_leading_char (abfd) == '_'); | |
2942 | else | |
2943 | return (bfd_get_symbol_leading_char (abfd) == 0); | |
2944 | } | |
2945 | ||
2946 | /* Mark presence or absence of leading underscore. */ | |
2947 | ||
2948 | static void | |
2949 | cris_elf_final_write_processing (abfd, linker) | |
2950 | bfd *abfd; | |
b34976b6 | 2951 | bfd_boolean linker ATTRIBUTE_UNUSED; |
4da81684 HPN |
2952 | { |
2953 | if (bfd_get_symbol_leading_char (abfd) == '_') | |
2954 | elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE; | |
2955 | else | |
2956 | elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE; | |
2957 | } | |
2958 | ||
2959 | /* Display the flags field. */ | |
2960 | ||
b34976b6 | 2961 | static bfd_boolean |
4da81684 HPN |
2962 | cris_elf_print_private_bfd_data (abfd, ptr) |
2963 | bfd *abfd; | |
2964 | PTR ptr; | |
2965 | { | |
2966 | FILE *file = (FILE *) ptr; | |
2967 | ||
2968 | BFD_ASSERT (abfd != NULL && ptr != NULL) | |
2969 | ||
2970 | _bfd_elf_print_private_bfd_data (abfd, ptr); | |
2971 | ||
2972 | fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags); | |
2973 | ||
2974 | if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE) | |
2975 | fprintf (file, _(" [symbols have a _ prefix]")); | |
2976 | ||
2977 | fputc ('\n', file); | |
b34976b6 | 2978 | return TRUE; |
4da81684 HPN |
2979 | } |
2980 | ||
2981 | /* Don't mix files with and without a leading underscore. */ | |
2982 | ||
b34976b6 | 2983 | static bfd_boolean |
4da81684 HPN |
2984 | cris_elf_merge_private_bfd_data (ibfd, obfd) |
2985 | bfd *ibfd; | |
2986 | bfd *obfd; | |
2987 | { | |
2988 | flagword old_flags, new_flags; | |
2989 | ||
82e51918 | 2990 | if (! _bfd_generic_verify_endian_match (ibfd, obfd)) |
b34976b6 | 2991 | return FALSE; |
4da81684 HPN |
2992 | |
2993 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
2994 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 2995 | return TRUE; |
4da81684 HPN |
2996 | |
2997 | if (! elf_flags_init (obfd)) | |
2998 | { | |
2999 | /* This happens when ld starts out with a 'blank' output file. */ | |
b34976b6 | 3000 | elf_flags_init (obfd) = TRUE; |
4da81684 HPN |
3001 | |
3002 | /* Set flags according to current bfd_target. */ | |
b34976b6 | 3003 | cris_elf_final_write_processing (obfd, FALSE); |
4da81684 HPN |
3004 | } |
3005 | ||
3006 | old_flags = elf_elfheader (obfd)->e_flags; | |
3007 | new_flags = elf_elfheader (ibfd)->e_flags; | |
3008 | ||
3009 | /* Is this good or bad? We'll follow with other excluding flags. */ | |
3010 | if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE)) | |
3011 | { | |
3012 | (*_bfd_error_handler) | |
3013 | ((new_flags & EF_CRIS_UNDERSCORE) | |
3014 | ? _("%s: uses _-prefixed symbols, but writing file with non-prefixed symbols") | |
3015 | : _("%s: uses non-prefixed symbols, but writing file with _-prefixed symbols"), | |
8f615d07 | 3016 | bfd_archive_filename (ibfd)); |
4da81684 | 3017 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 3018 | return FALSE; |
4da81684 HPN |
3019 | } |
3020 | ||
b34976b6 | 3021 | return TRUE; |
4da81684 | 3022 | } |
99e4ae17 AJ |
3023 | |
3024 | ||
3025 | static enum elf_reloc_type_class | |
f51e552e AM |
3026 | elf_cris_reloc_type_class (rela) |
3027 | const Elf_Internal_Rela *rela; | |
99e4ae17 | 3028 | { |
f51e552e | 3029 | switch ((int) ELF32_R_TYPE (rela->r_info)) |
99e4ae17 AJ |
3030 | { |
3031 | case R_CRIS_RELATIVE: | |
3032 | return reloc_class_relative; | |
3033 | case R_CRIS_JUMP_SLOT: | |
3034 | return reloc_class_plt; | |
3035 | case R_CRIS_COPY: | |
3036 | return reloc_class_copy; | |
3037 | default: | |
3038 | return reloc_class_normal; | |
3039 | } | |
3040 | } | |
06c15ad7 HPN |
3041 | \f |
3042 | #define ELF_ARCH bfd_arch_cris | |
3043 | #define ELF_MACHINE_CODE EM_CRIS | |
3044 | #define ELF_MAXPAGESIZE 0x2000 | |
3045 | ||
3046 | #define TARGET_LITTLE_SYM bfd_elf32_cris_vec | |
3047 | #define TARGET_LITTLE_NAME "elf32-cris" | |
4da81684 | 3048 | #define elf_symbol_leading_char 0 |
06c15ad7 HPN |
3049 | |
3050 | #define elf_info_to_howto_rel NULL | |
3051 | #define elf_info_to_howto cris_info_to_howto_rela | |
3052 | #define elf_backend_relocate_section cris_elf_relocate_section | |
3053 | #define elf_backend_gc_mark_hook cris_elf_gc_mark_hook | |
3054 | #define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook | |
3055 | #define elf_backend_check_relocs cris_elf_check_relocs | |
72209a1f HPN |
3056 | #define elf_backend_grok_prstatus cris_elf_grok_prstatus |
3057 | #define elf_backend_grok_psinfo cris_elf_grok_psinfo | |
06c15ad7 HPN |
3058 | |
3059 | #define elf_backend_can_gc_sections 1 | |
51b64d56 | 3060 | #define elf_backend_can_refcount 1 |
06c15ad7 | 3061 | |
4da81684 HPN |
3062 | #define elf_backend_object_p cris_elf_object_p |
3063 | #define elf_backend_final_write_processing \ | |
3064 | cris_elf_final_write_processing | |
3065 | #define bfd_elf32_bfd_print_private_bfd_data \ | |
3066 | cris_elf_print_private_bfd_data | |
3067 | #define bfd_elf32_bfd_merge_private_bfd_data \ | |
3068 | cris_elf_merge_private_bfd_data | |
3069 | ||
06c15ad7 HPN |
3070 | #define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup |
3071 | ||
915e5146 HPN |
3072 | #define bfd_elf32_bfd_link_hash_table_create \ |
3073 | elf_cris_link_hash_table_create | |
3074 | #define elf_backend_adjust_dynamic_symbol \ | |
3075 | elf_cris_adjust_dynamic_symbol | |
3076 | #define elf_backend_size_dynamic_sections \ | |
3077 | elf_cris_size_dynamic_sections | |
3078 | #define elf_backend_finish_dynamic_symbol \ | |
3079 | elf_cris_finish_dynamic_symbol | |
3080 | #define elf_backend_finish_dynamic_sections \ | |
3081 | elf_cris_finish_dynamic_sections | |
3082 | #define elf_backend_create_dynamic_sections \ | |
3083 | _bfd_elf_create_dynamic_sections | |
3084 | #define bfd_elf32_bfd_final_link \ | |
c152c796 | 3085 | bfd_elf_gc_common_final_link |
915e5146 | 3086 | #define elf_backend_hide_symbol elf_cris_hide_symbol |
99e4ae17 | 3087 | #define elf_backend_reloc_type_class elf_cris_reloc_type_class |
915e5146 HPN |
3088 | |
3089 | #define elf_backend_want_got_plt 1 | |
3090 | #define elf_backend_plt_readonly 1 | |
3091 | #define elf_backend_want_plt_sym 0 | |
3092 | #define elf_backend_got_header_size 12 | |
915e5146 | 3093 | |
06c15ad7 HPN |
3094 | /* Later, we my want to optimize RELA entries into REL entries for dynamic |
3095 | linking and libraries (if it's a win of any significance). Until then, | |
3096 | take the easy route. */ | |
3097 | #define elf_backend_may_use_rel_p 0 | |
3098 | #define elf_backend_may_use_rela_p 1 | |
f0fe0e16 | 3099 | #define elf_backend_rela_normal 1 |
06c15ad7 HPN |
3100 | |
3101 | #include "elf32-target.h" | |
4da81684 HPN |
3102 | |
3103 | #define INCLUDED_TARGET_FILE | |
3104 | ||
3105 | #undef TARGET_LITTLE_SYM | |
3106 | #undef TARGET_LITTLE_NAME | |
3107 | #undef elf_symbol_leading_char | |
3108 | ||
3109 | #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec | |
3110 | #define TARGET_LITTLE_NAME "elf32-us-cris" | |
3111 | #define elf_symbol_leading_char '_' | |
3112 | ||
3113 | #include "elf32-target.h" |