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