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