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