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