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