* config/tc-arm.c (arm_cpus): Correct arch field for arm1026ej-s.
[binutils.git] / bfd / elf32-frv.c
blob9564671af7bb7a0419576afc5a46529b03fe8ddb
1 /* FRV-specific support for 32-bit ELF.
2 Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24 #include "elf/frv.h"
25 #include "elf/dwarf2.h"
26 #include "hashtab.h"
28 /* Forward declarations. */
29 static bfd_reloc_status_type elf32_frv_relocate_lo16
30 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
31 static bfd_reloc_status_type elf32_frv_relocate_hi16
32 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
33 static bfd_reloc_status_type elf32_frv_relocate_label24
34 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
35 static bfd_reloc_status_type elf32_frv_relocate_gprel12
36 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
37 bfd_byte *, bfd_vma));
38 static bfd_reloc_status_type elf32_frv_relocate_gprelu12
39 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
40 bfd_byte *, bfd_vma));
41 static bfd_reloc_status_type elf32_frv_relocate_gprello
42 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
43 bfd_byte *, bfd_vma));
44 static bfd_reloc_status_type elf32_frv_relocate_gprelhi
45 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
46 bfd_byte *, bfd_vma));
47 static reloc_howto_type *frv_reloc_type_lookup
48 PARAMS ((bfd *, bfd_reloc_code_real_type));
49 static void frv_info_to_howto_rela
50 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
51 static bfd_boolean elf32_frv_relocate_section
52 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
53 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
54 static bfd_boolean elf32_frv_add_symbol_hook
55 PARAMS (( bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
56 const char **, flagword *, asection **, bfd_vma *));
57 static bfd_reloc_status_type frv_final_link_relocate
58 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
59 Elf_Internal_Rela *, bfd_vma));
60 static bfd_boolean elf32_frv_gc_sweep_hook
61 PARAMS ((bfd *, struct bfd_link_info *, asection *, const
62 Elf_Internal_Rela *));
63 static asection * elf32_frv_gc_mark_hook
64 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
65 struct elf_link_hash_entry *, Elf_Internal_Sym *));
66 static bfd_boolean elf32_frv_check_relocs
67 PARAMS ((bfd *, struct bfd_link_info *, asection *,
68 const Elf_Internal_Rela *));
69 static int elf32_frv_machine
70 PARAMS ((bfd *));
71 static bfd_boolean elf32_frv_object_p
72 PARAMS ((bfd *));
73 static bfd_boolean frv_elf_set_private_flags
74 PARAMS ((bfd *, flagword));
75 static bfd_boolean frv_elf_copy_private_bfd_data
76 PARAMS ((bfd *, bfd *));
77 static bfd_boolean frv_elf_merge_private_bfd_data
78 PARAMS ((bfd *, bfd *));
79 static bfd_boolean frv_elf_print_private_bfd_data
80 PARAMS ((bfd *, PTR));
82 static reloc_howto_type elf32_frv_howto_table [] =
84 /* This reloc does nothing. */
85 HOWTO (R_FRV_NONE, /* type */
86 0, /* rightshift */
87 2, /* size (0 = byte, 1 = short, 2 = long) */
88 32, /* bitsize */
89 FALSE, /* pc_relative */
90 0, /* bitpos */
91 complain_overflow_bitfield, /* complain_on_overflow */
92 bfd_elf_generic_reloc, /* special_function */
93 "R_FRV_NONE", /* name */
94 FALSE, /* partial_inplace */
95 0, /* src_mask */
96 0, /* dst_mask */
97 FALSE), /* pcrel_offset */
99 /* A 32 bit absolute relocation. */
100 HOWTO (R_FRV_32, /* type */
101 0, /* rightshift */
102 2, /* size (0 = byte, 1 = short, 2 = long) */
103 32, /* bitsize */
104 FALSE, /* pc_relative */
105 0, /* bitpos */
106 complain_overflow_bitfield, /* complain_on_overflow */
107 bfd_elf_generic_reloc, /* special_function */
108 "R_FRV_32", /* name */
109 FALSE, /* partial_inplace */
110 0xffffffff, /* src_mask */
111 0xffffffff, /* dst_mask */
112 FALSE), /* pcrel_offset */
114 /* A 16 bit pc-relative relocation. */
115 HOWTO (R_FRV_LABEL16, /* type */
116 2, /* rightshift */
117 2, /* size (0 = byte, 1 = short, 2 = long) */
118 16, /* bitsize */
119 TRUE, /* pc_relative */
120 0, /* bitpos */
121 complain_overflow_signed, /* complain_on_overflow */
122 bfd_elf_generic_reloc, /* special_function */
123 "R_FRV_LABEL16", /* name */
124 FALSE, /* partial_inplace */
125 0xffff, /* src_mask */
126 0xffff, /* dst_mask */
127 TRUE), /* pcrel_offset */
129 /* A 24-bit pc-relative relocation. */
130 HOWTO (R_FRV_LABEL24, /* type */
131 2, /* rightshift */
132 2, /* size (0 = byte, 1 = short, 2 = long) */
133 26, /* bitsize */
134 TRUE, /* pc_relative */
135 0, /* bitpos */
136 complain_overflow_bitfield, /* complain_on_overflow */
137 bfd_elf_generic_reloc, /* special_function */
138 "R_FRV_LABEL24", /* name */
139 FALSE, /* partial_inplace */
140 0x7e03ffff, /* src_mask */
141 0x7e03ffff, /* dst_mask */
142 TRUE), /* pcrel_offset */
144 HOWTO (R_FRV_LO16, /* type */
145 0, /* rightshift */
146 2, /* size (0 = byte, 1 = short, 2 = long) */
147 16, /* bitsize */
148 FALSE, /* pc_relative */
149 0, /* bitpos */
150 complain_overflow_dont, /* complain_on_overflow */
151 bfd_elf_generic_reloc, /* special_function */
152 "R_FRV_LO16", /* name */
153 FALSE, /* partial_inplace */
154 0xffff, /* src_mask */
155 0xffff, /* dst_mask */
156 FALSE), /* pcrel_offset */
158 HOWTO (R_FRV_HI16, /* type */
159 0, /* rightshift */
160 2, /* size (0 = byte, 1 = short, 2 = long) */
161 16, /* bitsize */
162 FALSE, /* pc_relative */
163 0, /* bitpos */
164 complain_overflow_dont, /* complain_on_overflow */
165 bfd_elf_generic_reloc, /* special_function */
166 "R_FRV_HI16", /* name */
167 FALSE, /* partial_inplace */
168 0xffff, /* src_mask */
169 0xffff, /* dst_mask */
170 FALSE), /* pcrel_offset */
172 HOWTO (R_FRV_GPREL12, /* type */
173 0, /* rightshift */
174 2, /* size (0 = byte, 1 = short, 2 = long) */
175 12, /* bitsize */
176 FALSE, /* pc_relative */
177 0, /* bitpos */
178 complain_overflow_dont, /* complain_on_overflow */
179 bfd_elf_generic_reloc, /* special_function */
180 "R_FRV_GPREL12", /* name */
181 FALSE, /* partial_inplace */
182 0xfff, /* src_mask */
183 0xfff, /* dst_mask */
184 FALSE), /* pcrel_offset */
186 HOWTO (R_FRV_GPRELU12, /* type */
187 0, /* rightshift */
188 2, /* size (0 = byte, 1 = short, 2 = long) */
189 12, /* bitsize */
190 FALSE, /* pc_relative */
191 0, /* bitpos */
192 complain_overflow_dont, /* complain_on_overflow */
193 bfd_elf_generic_reloc, /* special_function */
194 "R_FRV_GPRELU12", /* name */
195 FALSE, /* partial_inplace */
196 0xfff, /* src_mask */
197 0x3f03f, /* dst_mask */
198 FALSE), /* pcrel_offset */
200 HOWTO (R_FRV_GPREL32, /* type */
201 0, /* rightshift */
202 2, /* size (0 = byte, 1 = short, 2 = long) */
203 32, /* bitsize */
204 FALSE, /* pc_relative */
205 0, /* bitpos */
206 complain_overflow_dont, /* complain_on_overflow */
207 bfd_elf_generic_reloc, /* special_function */
208 "R_FRV_GPREL32", /* name */
209 FALSE, /* partial_inplace */
210 0xffffffff, /* src_mask */
211 0xffffffff, /* dst_mask */
212 FALSE), /* pcrel_offset */
214 HOWTO (R_FRV_GPRELHI, /* type */
215 0, /* rightshift */
216 2, /* size (0 = byte, 1 = short, 2 = long) */
217 16, /* bitsize */
218 FALSE, /* pc_relative */
219 0, /* bitpos */
220 complain_overflow_dont, /* complain_on_overflow */
221 bfd_elf_generic_reloc, /* special_function */
222 "R_FRV_GPRELHI", /* name */
223 FALSE, /* partial_inplace */
224 0xffff, /* src_mask */
225 0xffff, /* dst_mask */
226 FALSE), /* pcrel_offset */
228 HOWTO (R_FRV_GPRELLO, /* type */
229 0, /* rightshift */
230 2, /* size (0 = byte, 1 = short, 2 = long) */
231 16, /* bitsize */
232 FALSE, /* pc_relative */
233 0, /* bitpos */
234 complain_overflow_dont, /* complain_on_overflow */
235 bfd_elf_generic_reloc, /* special_function */
236 "R_FRV_GPRELLO", /* name */
237 FALSE, /* partial_inplace */
238 0xffff, /* src_mask */
239 0xffff, /* dst_mask */
240 FALSE), /* pcrel_offset */
242 /* A 12-bit signed operand with the GOT offset for the address of
243 the symbol. */
244 HOWTO (R_FRV_GOT12, /* type */
245 0, /* rightshift */
246 2, /* size (0 = byte, 1 = short, 2 = long) */
247 12, /* bitsize */
248 FALSE, /* pc_relative */
249 0, /* bitpos */
250 complain_overflow_signed, /* complain_on_overflow */
251 bfd_elf_generic_reloc, /* special_function */
252 "R_FRV_GOT12", /* name */
253 FALSE, /* partial_inplace */
254 0xfff, /* src_mask */
255 0xfff, /* dst_mask */
256 FALSE), /* pcrel_offset */
258 /* The upper 16 bits of the GOT offset for the address of the
259 symbol. */
260 HOWTO (R_FRV_GOTHI, /* type */
261 0, /* rightshift */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
263 16, /* bitsize */
264 FALSE, /* pc_relative */
265 0, /* bitpos */
266 complain_overflow_dont, /* complain_on_overflow */
267 bfd_elf_generic_reloc, /* special_function */
268 "R_FRV_GOTHI", /* name */
269 FALSE, /* partial_inplace */
270 0xffff, /* src_mask */
271 0xffff, /* dst_mask */
272 FALSE), /* pcrel_offset */
274 /* The lower 16 bits of the GOT offset for the address of the
275 symbol. */
276 HOWTO (R_FRV_GOTLO, /* type */
277 0, /* rightshift */
278 2, /* size (0 = byte, 1 = short, 2 = long) */
279 16, /* bitsize */
280 FALSE, /* pc_relative */
281 0, /* bitpos */
282 complain_overflow_dont, /* complain_on_overflow */
283 bfd_elf_generic_reloc, /* special_function */
284 "R_FRV_GOTLO", /* name */
285 FALSE, /* partial_inplace */
286 0xffff, /* src_mask */
287 0xffff, /* dst_mask */
288 FALSE), /* pcrel_offset */
290 /* The 32-bit address of the canonical descriptor of a function. */
291 HOWTO (R_FRV_FUNCDESC, /* type */
292 0, /* rightshift */
293 2, /* size (0 = byte, 1 = short, 2 = long) */
294 32, /* bitsize */
295 FALSE, /* pc_relative */
296 0, /* bitpos */
297 complain_overflow_bitfield, /* complain_on_overflow */
298 bfd_elf_generic_reloc, /* special_function */
299 "R_FRV_FUNCDESC", /* name */
300 FALSE, /* partial_inplace */
301 0xffffffff, /* src_mask */
302 0xffffffff, /* dst_mask */
303 FALSE), /* pcrel_offset */
305 /* A 12-bit signed operand with the GOT offset for the address of
306 canonical descriptor of a function. */
307 HOWTO (R_FRV_FUNCDESC_GOT12, /* type */
308 0, /* rightshift */
309 2, /* size (0 = byte, 1 = short, 2 = long) */
310 12, /* bitsize */
311 FALSE, /* pc_relative */
312 0, /* bitpos */
313 complain_overflow_signed, /* complain_on_overflow */
314 bfd_elf_generic_reloc, /* special_function */
315 "R_FRV_FUNCDESC_GOT12", /* name */
316 FALSE, /* partial_inplace */
317 0xfff, /* src_mask */
318 0xfff, /* dst_mask */
319 FALSE), /* pcrel_offset */
321 /* The upper 16 bits of the GOT offset for the address of the
322 canonical descriptor of a function. */
323 HOWTO (R_FRV_FUNCDESC_GOTHI, /* type */
324 0, /* rightshift */
325 2, /* size (0 = byte, 1 = short, 2 = long) */
326 16, /* bitsize */
327 FALSE, /* pc_relative */
328 0, /* bitpos */
329 complain_overflow_dont, /* complain_on_overflow */
330 bfd_elf_generic_reloc, /* special_function */
331 "R_FRV_FUNCDESC_GOTHI", /* name */
332 FALSE, /* partial_inplace */
333 0xffff, /* src_mask */
334 0xffff, /* dst_mask */
335 FALSE), /* pcrel_offset */
337 /* The lower 16 bits of the GOT offset for the address of the
338 canonical descriptor of a function. */
339 HOWTO (R_FRV_FUNCDESC_GOTLO, /* type */
340 0, /* rightshift */
341 2, /* size (0 = byte, 1 = short, 2 = long) */
342 16, /* bitsize */
343 FALSE, /* pc_relative */
344 0, /* bitpos */
345 complain_overflow_dont, /* complain_on_overflow */
346 bfd_elf_generic_reloc, /* special_function */
347 "R_FRV_FUNCDESC_GOTLO", /* name */
348 FALSE, /* partial_inplace */
349 0xffff, /* src_mask */
350 0xffff, /* dst_mask */
351 FALSE), /* pcrel_offset */
353 /* The 32-bit address of the canonical descriptor of a function. */
354 HOWTO (R_FRV_FUNCDESC_VALUE, /* type */
355 0, /* rightshift */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
357 64, /* bitsize */
358 FALSE, /* pc_relative */
359 0, /* bitpos */
360 complain_overflow_bitfield, /* complain_on_overflow */
361 bfd_elf_generic_reloc, /* special_function */
362 "R_FRV_FUNCDESC_VALUE", /* name */
363 FALSE, /* partial_inplace */
364 0xffffffff, /* src_mask */
365 0xffffffff, /* dst_mask */
366 FALSE), /* pcrel_offset */
368 /* A 12-bit signed operand with the GOT offset for the address of
369 canonical descriptor of a function. */
370 HOWTO (R_FRV_FUNCDESC_GOTOFF12, /* type */
371 0, /* rightshift */
372 2, /* size (0 = byte, 1 = short, 2 = long) */
373 12, /* bitsize */
374 FALSE, /* pc_relative */
375 0, /* bitpos */
376 complain_overflow_signed, /* complain_on_overflow */
377 bfd_elf_generic_reloc, /* special_function */
378 "R_FRV_FUNCDESC_GOTOFF12", /* name */
379 FALSE, /* partial_inplace */
380 0xfff, /* src_mask */
381 0xfff, /* dst_mask */
382 FALSE), /* pcrel_offset */
384 /* The upper 16 bits of the GOT offset for the address of the
385 canonical descriptor of a function. */
386 HOWTO (R_FRV_FUNCDESC_GOTOFFHI, /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 16, /* bitsize */
390 FALSE, /* pc_relative */
391 0, /* bitpos */
392 complain_overflow_dont, /* complain_on_overflow */
393 bfd_elf_generic_reloc, /* special_function */
394 "R_FRV_FUNCDESC_GOTOFFHI", /* name */
395 FALSE, /* partial_inplace */
396 0xffff, /* src_mask */
397 0xffff, /* dst_mask */
398 FALSE), /* pcrel_offset */
400 /* The lower 16 bits of the GOT offset for the address of the
401 canonical descriptor of a function. */
402 HOWTO (R_FRV_FUNCDESC_GOTOFFLO, /* type */
403 0, /* rightshift */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
405 16, /* bitsize */
406 FALSE, /* pc_relative */
407 0, /* bitpos */
408 complain_overflow_dont, /* complain_on_overflow */
409 bfd_elf_generic_reloc, /* special_function */
410 "R_FRV_FUNCDESC_GOTOFFLO", /* name */
411 FALSE, /* partial_inplace */
412 0xffff, /* src_mask */
413 0xffff, /* dst_mask */
414 FALSE), /* pcrel_offset */
416 /* A 12-bit signed operand with the GOT offset for the address of
417 the symbol. */
418 HOWTO (R_FRV_GOTOFF12, /* type */
419 0, /* rightshift */
420 2, /* size (0 = byte, 1 = short, 2 = long) */
421 12, /* bitsize */
422 FALSE, /* pc_relative */
423 0, /* bitpos */
424 complain_overflow_signed, /* complain_on_overflow */
425 bfd_elf_generic_reloc, /* special_function */
426 "R_FRV_GOTOFF12", /* name */
427 FALSE, /* partial_inplace */
428 0xfff, /* src_mask */
429 0xfff, /* dst_mask */
430 FALSE), /* pcrel_offset */
432 /* The upper 16 bits of the GOT offset for the address of the
433 symbol. */
434 HOWTO (R_FRV_GOTOFFHI, /* type */
435 0, /* rightshift */
436 2, /* size (0 = byte, 1 = short, 2 = long) */
437 16, /* bitsize */
438 FALSE, /* pc_relative */
439 0, /* bitpos */
440 complain_overflow_dont, /* complain_on_overflow */
441 bfd_elf_generic_reloc, /* special_function */
442 "R_FRV_GOTOFFHI", /* name */
443 FALSE, /* partial_inplace */
444 0xffff, /* src_mask */
445 0xffff, /* dst_mask */
446 FALSE), /* pcrel_offset */
448 /* The lower 16 bits of the GOT offset for the address of the
449 symbol. */
450 HOWTO (R_FRV_GOTOFFLO, /* type */
451 0, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 16, /* bitsize */
454 FALSE, /* pc_relative */
455 0, /* bitpos */
456 complain_overflow_dont, /* complain_on_overflow */
457 bfd_elf_generic_reloc, /* special_function */
458 "R_FRV_GOTOFFLO", /* name */
459 FALSE, /* partial_inplace */
460 0xffff, /* src_mask */
461 0xffff, /* dst_mask */
462 FALSE), /* pcrel_offset */
466 /* GNU extension to record C++ vtable hierarchy. */
467 static reloc_howto_type elf32_frv_vtinherit_howto =
468 HOWTO (R_FRV_GNU_VTINHERIT, /* type */
469 0, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 0, /* bitsize */
472 FALSE, /* pc_relative */
473 0, /* bitpos */
474 complain_overflow_dont, /* complain_on_overflow */
475 NULL, /* special_function */
476 "R_FRV_GNU_VTINHERIT", /* name */
477 FALSE, /* partial_inplace */
478 0, /* src_mask */
479 0, /* dst_mask */
480 FALSE); /* pcrel_offset */
482 /* GNU extension to record C++ vtable member usage. */
483 static reloc_howto_type elf32_frv_vtentry_howto =
484 HOWTO (R_FRV_GNU_VTENTRY, /* type */
485 0, /* rightshift */
486 2, /* size (0 = byte, 1 = short, 2 = long) */
487 0, /* bitsize */
488 FALSE, /* pc_relative */
489 0, /* bitpos */
490 complain_overflow_dont, /* complain_on_overflow */
491 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
492 "R_FRV_GNU_VTENTRY", /* name */
493 FALSE, /* partial_inplace */
494 0, /* src_mask */
495 0, /* dst_mask */
496 FALSE); /* pcrel_offset */
498 /* The following 3 relocations are REL. The only difference to the
499 entries in the table above are that partial_inplace is TRUE. */
500 static reloc_howto_type elf32_frv_rel_32_howto =
501 HOWTO (R_FRV_32, /* type */
502 0, /* rightshift */
503 2, /* size (0 = byte, 1 = short, 2 = long) */
504 32, /* bitsize */
505 FALSE, /* pc_relative */
506 0, /* bitpos */
507 complain_overflow_bitfield, /* complain_on_overflow */
508 bfd_elf_generic_reloc, /* special_function */
509 "R_FRV_32", /* name */
510 TRUE, /* partial_inplace */
511 0xffffffff, /* src_mask */
512 0xffffffff, /* dst_mask */
513 FALSE); /* pcrel_offset */
515 static reloc_howto_type elf32_frv_rel_funcdesc_howto =
516 HOWTO (R_FRV_FUNCDESC, /* type */
517 0, /* rightshift */
518 2, /* size (0 = byte, 1 = short, 2 = long) */
519 32, /* bitsize */
520 FALSE, /* pc_relative */
521 0, /* bitpos */
522 complain_overflow_bitfield, /* complain_on_overflow */
523 bfd_elf_generic_reloc, /* special_function */
524 "R_FRV_FUNCDESC", /* name */
525 TRUE, /* partial_inplace */
526 0xffffffff, /* src_mask */
527 0xffffffff, /* dst_mask */
528 FALSE); /* pcrel_offset */
530 static reloc_howto_type elf32_frv_rel_funcdesc_value_howto =
531 HOWTO (R_FRV_FUNCDESC_VALUE, /* type */
532 0, /* rightshift */
533 2, /* size (0 = byte, 1 = short, 2 = long) */
534 64, /* bitsize */
535 FALSE, /* pc_relative */
536 0, /* bitpos */
537 complain_overflow_bitfield, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_FRV_FUNCDESC_VALUE", /* name */
540 TRUE, /* partial_inplace */
541 0xffffffff, /* src_mask */
542 0xffffffff, /* dst_mask */
543 FALSE); /* pcrel_offset */
546 /* Map BFD reloc types to FRV ELF reloc types. */
547 #if 0
548 struct frv_reloc_map
550 unsigned int bfd_reloc_val;
551 unsigned int frv_reloc_val;
554 static const struct frv_reloc_map frv_reloc_map [] =
556 { BFD_RELOC_NONE, R_FRV_NONE },
557 { BFD_RELOC_32, R_FRV_32 },
558 { BFD_RELOC_FRV_LABEL16, R_FRV_LABEL16 },
559 { BFD_RELOC_FRV_LABEL24, R_FRV_LABEL24 },
560 { BFD_RELOC_FRV_LO16, R_FRV_LO16 },
561 { BFD_RELOC_FRV_HI16, R_FRV_HI16 },
562 { BFD_RELOC_FRV_GPREL12, R_FRV_GPREL12 },
563 { BFD_RELOC_FRV_GPRELU12, R_FRV_GPRELU12 },
564 { BFD_RELOC_FRV_GPREL32, R_FRV_GPREL32 },
565 { BFD_RELOC_FRV_GPRELHI, R_FRV_GPRELHI },
566 { BFD_RELOC_FRV_GPRELLO, R_FRV_GPRELLO },
567 { BFD_RELOC_FRV_GOT12, R_FRV_GOT12 },
568 { BFD_RELOC_FRV_GOTHI, R_FRV_GOTHI },
569 { BFD_RELOC_FRV_GOTLO, R_FRV_GOTLO },
570 { BFD_RELOC_FRV_FUNCDESC, R_FRV_FUNCDESC },
571 { BFD_RELOC_FRV_FUNCDESC_GOT12, R_FRV_FUNCDESC_GOT12 },
572 { BFD_RELOC_FRV_FUNCDESC_GOTHI, R_FRV_FUNCDESC_GOTHI },
573 { BFD_RELOC_FRV_FUNCDESC_GOTLO, R_FRV_FUNCDESC_GOTLO },
574 { BFD_RELOC_FRV_FUNCDESC_VALUE, R_FRV_FUNCDESC_VALUE },
575 { BFD_RELOC_FRV_FUNCDESC_GOTOFF12, R_FRV_FUNCDESC_GOTOFF12 },
576 { BFD_RELOC_FRV_FUNCDESC_GOTOFFHI, R_FRV_FUNCDESC_GOTOFFHI },
577 { BFD_RELOC_FRV_FUNCDESC_GOTOFFLO, R_FRV_FUNCDESC_GOTOFFLO },
578 { BFD_RELOC_FRV_GOTOFF12, R_FRV_GOTOFF12 },
579 { BFD_RELOC_FRV_GOTOFFHI, R_FRV_GOTOFFHI },
580 { BFD_RELOC_FRV_GOTOFFLO, R_FRV_GOTOFFLO },
581 { BFD_RELOC_VTABLE_INHERIT, R_FRV_GNU_VTINHERIT },
582 { BFD_RELOC_VTABLE_ENTRY, R_FRV_GNU_VTENTRY },
584 #endif
586 extern const bfd_target bfd_elf32_frvfdpic_vec;
587 #define IS_FDPIC(bfd) ((bfd)->xvec == &bfd_elf32_frvfdpic_vec)
589 /* An extension of the elf hash table data structure, containing some
590 additional FRV-specific data. */
591 struct frvfdpic_elf_link_hash_table
593 struct elf_link_hash_table elf;
595 /* A pointer to the .got section. */
596 asection *sgot;
597 /* A pointer to the .rel.got section. */
598 asection *sgotrel;
599 /* A pointer to the .rofixup section. */
600 asection *sgotfixup;
601 /* A pointer to the .plt section. */
602 asection *splt;
603 /* A pointer to the .rel.plt section. */
604 asection *spltrel;
605 /* GOT base offset. */
606 bfd_vma got0;
607 /* Location of the first non-lazy PLT entry, i.e., the number of
608 bytes taken by lazy PLT entries. */
609 bfd_vma plt0;
610 /* A hash table holding information about which symbols were
611 referenced with which PIC-related relocations. */
612 struct htab *relocs_info;
615 /* Get the FRV ELF linker hash table from a link_info structure. */
617 #define frvfdpic_hash_table(info) \
618 ((struct frvfdpic_elf_link_hash_table *) ((info)->hash))
620 #define frvfdpic_got_section(info) \
621 (frvfdpic_hash_table (info)->sgot)
622 #define frvfdpic_gotrel_section(info) \
623 (frvfdpic_hash_table (info)->sgotrel)
624 #define frvfdpic_gotfixup_section(info) \
625 (frvfdpic_hash_table (info)->sgotfixup)
626 #define frvfdpic_plt_section(info) \
627 (frvfdpic_hash_table (info)->splt)
628 #define frvfdpic_pltrel_section(info) \
629 (frvfdpic_hash_table (info)->spltrel)
630 #define frvfdpic_relocs_info(info) \
631 (frvfdpic_hash_table (info)->relocs_info)
632 #define frvfdpic_got_initial_offset(info) \
633 (frvfdpic_hash_table (info)->got0)
634 #define frvfdpic_plt_initial_offset(info) \
635 (frvfdpic_hash_table (info)->plt0)
637 /* Create an FRV ELF linker hash table. */
639 static struct bfd_link_hash_table *
640 frvfdpic_elf_link_hash_table_create (bfd *abfd)
642 struct frvfdpic_elf_link_hash_table *ret;
643 bfd_size_type amt = sizeof (struct frvfdpic_elf_link_hash_table);
645 ret = bfd_zalloc (abfd, amt);
646 if (ret == NULL)
647 return NULL;
649 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
650 _bfd_elf_link_hash_newfunc))
652 free (ret);
653 return NULL;
656 return &ret->elf.root;
659 /* Decide whether a reference to a symbol can be resolved locally or
660 not. If the symbol is protected, we want the local address, but
661 its function descriptor must be assigned by the dynamic linker. */
662 #define FRVFDPIC_SYM_LOCAL(INFO, H) \
663 (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
664 || ! elf_hash_table (INFO)->dynamic_sections_created)
665 #define FRVFDPIC_FUNCDESC_LOCAL(INFO, H) \
666 ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
668 /* This structure collects information on what kind of GOT, PLT or
669 function descriptors are required by relocations that reference a
670 certain symbol. */
671 struct frvfdpic_relocs_info
673 /* The index of the symbol, as stored in the relocation r_info, if
674 we have a local symbol; -1 otherwise. */
675 long symndx;
676 union
678 /* The input bfd in which the symbol is defined, if it's a local
679 symbol. */
680 bfd *abfd;
681 /* If symndx == -1, the hash table entry corresponding to a global
682 symbol (even if it turns out to bind locally, in which case it
683 should ideally be replaced with section's symndx + addend). */
684 struct elf_link_hash_entry *h;
685 } d;
686 /* The addend of the relocation that references the symbol. */
687 bfd_vma addend;
689 /* The fields above are used to identify an entry. The fields below
690 contain information on how an entry is used and, later on, which
691 locations it was assigned. */
692 /* The following 3 fields record whether the symbol+addend above was
693 ever referenced with a GOT relocation. The 12 suffix indicates a
694 GOT12 relocation; los is used for GOTLO relocations that are not
695 matched by a GOTHI relocation; hilo is used for GOTLO/GOTHI
696 pairs. */
697 unsigned got12:1;
698 unsigned gotlos:1;
699 unsigned gothilo:1;
700 /* Whether a FUNCDESC relocation references symbol+addend. */
701 unsigned fd:1;
702 /* Whether a FUNCDESC_GOT relocation references symbol+addend. */
703 unsigned fdgot12:1;
704 unsigned fdgotlos:1;
705 unsigned fdgothilo:1;
706 /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend. */
707 unsigned fdgoff12:1;
708 unsigned fdgofflos:1;
709 unsigned fdgoffhilo:1;
710 /* Whether symbol+addend is referenced with GOTOFF12, GOTOFFLO or
711 GOTOFFHI relocations. The addend doesn't really matter, since we
712 envision that this will only be used to check whether the symbol
713 is mapped to the same segment as the got. */
714 unsigned gotoff:1;
715 /* Whether symbol+addend is referenced by a LABEL24 relocation. */
716 unsigned call:1;
717 /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
718 relocation. */
719 unsigned sym:1;
720 /* Whether we need a PLT entry for a symbol. Should be implied by
721 something like:
722 (call && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)) */
723 unsigned plt:1;
724 /* Whether a function descriptor should be created in this link unit
725 for symbol+addend. Should be implied by something like:
726 (plt || fdgotoff12 || fdgotofflos || fdgotofflohi
727 || ((fd || fdgot12 || fdgotlos || fdgothilo)
728 && (symndx != -1 || FRVFDPIC_FUNCDESC_LOCAL (info, d.h)))) */
729 unsigned privfd:1;
730 /* Whether a lazy PLT entry is needed for this symbol+addend.
731 Should be implied by something like:
732 (privfd && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)
733 && ! (info->flags & DF_BIND_NOW)) */
734 unsigned lazyplt:1;
735 /* Whether we've already emitted GOT relocations and PLT entries as
736 needed for this symbol. */
737 unsigned done:1;
739 /* The number of R_FRV_32, R_FRV_FUNCDESC and R_FRV_FUNCDESC_VALUE
740 relocations referencing the symbol. */
741 unsigned relocs32, relocsfd, relocsfdv;
743 /* The number of .rofixups entries and dynamic relocations allocated
744 for this symbol, minus any that might have already been used. */
745 unsigned fixups, dynrelocs;
747 /* The offsets of the GOT entries assigned to symbol+addend, to the
748 function descriptor's address, and to a function descriptor,
749 respectively. Should be zero if unassigned. The offsets are
750 counted from the value that will be assigned to the PIC register,
751 not from the beginning of the .got section. */
752 bfd_signed_vma got_entry, fdgot_entry, fd_entry;
753 /* The offsets of the PLT entries assigned to symbol+addend,
754 non-lazy and lazy, respectively. If unassigned, should be
755 (bfd_vma)-1. */
756 bfd_vma plt_entry, lzplt_entry;
759 /* Compute a hash with the key fields of an frvfdpic_relocs_info entry. */
760 static hashval_t
761 frvfdpic_relocs_info_hash (const void *entry_)
763 const struct frvfdpic_relocs_info *entry = entry_;
765 return (entry->symndx == -1
766 ? entry->d.h->root.root.hash
767 : entry->symndx + entry->d.abfd->id * 257) + entry->addend;
770 /* Test whether the key fields of two frvfdpic_relocs_info entries are
771 identical. */
772 static int
773 frvfdpic_relocs_info_eq (const void *entry1, const void *entry2)
775 const struct frvfdpic_relocs_info *e1 = entry1;
776 const struct frvfdpic_relocs_info *e2 = entry2;
778 return e1->symndx == e2->symndx && e1->addend == e2->addend
779 && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
782 /* Find or create an entry in a hash table HT that matches the key
783 fields of the given ENTRY. If it's not found, memory for a new
784 entry is allocated in ABFD's obstack. */
785 static struct frvfdpic_relocs_info *
786 frvfdpic_relocs_info_find (struct htab *ht,
787 bfd *abfd,
788 const struct frvfdpic_relocs_info *entry,
789 enum insert_option insert)
791 struct frvfdpic_relocs_info **loc =
792 (struct frvfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
794 if (! loc)
795 return NULL;
797 if (*loc)
798 return *loc;
800 *loc = bfd_zalloc (abfd, sizeof (**loc));
802 if (! *loc)
803 return *loc;
805 (*loc)->symndx = entry->symndx;
806 (*loc)->d = entry->d;
807 (*loc)->addend = entry->addend;
808 (*loc)->plt_entry = (bfd_vma)-1;
809 (*loc)->lzplt_entry = (bfd_vma)-1;
811 return *loc;
814 /* Obtain the address of the entry in HT associated with H's symbol +
815 addend, creating a new entry if none existed. ABFD is only used
816 for memory allocation purposes. */
817 inline static struct frvfdpic_relocs_info *
818 frvfdpic_relocs_info_for_global (struct htab *ht,
819 bfd *abfd,
820 struct elf_link_hash_entry *h,
821 bfd_vma addend,
822 enum insert_option insert)
824 struct frvfdpic_relocs_info entry;
826 entry.symndx = -1;
827 entry.d.h = h;
828 entry.addend = addend;
830 return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
833 /* Obtain the address of the entry in HT associated with the SYMNDXth
834 local symbol of the input bfd ABFD, plus the addend, creating a new
835 entry if none existed. */
836 inline static struct frvfdpic_relocs_info *
837 frvfdpic_relocs_info_for_local (struct htab *ht,
838 bfd *abfd,
839 long symndx,
840 bfd_vma addend,
841 enum insert_option insert)
843 struct frvfdpic_relocs_info entry;
845 entry.symndx = symndx;
846 entry.d.abfd = abfd;
847 entry.addend = addend;
849 return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
852 /* Merge fields set by check_relocs() of two entries that end up being
853 mapped to the same (presumably global) symbol. */
855 inline static void
856 frvfdpic_pic_merge_early_relocs_info (struct frvfdpic_relocs_info *e2,
857 struct frvfdpic_relocs_info const *e1)
859 e2->got12 |= e1->got12;
860 e2->gotlos |= e1->gotlos;
861 e2->gothilo |= e1->gothilo;
862 e2->fd |= e1->fd;
863 e2->fdgot12 |= e1->fdgot12;
864 e2->fdgotlos |= e1->fdgotlos;
865 e2->fdgothilo |= e1->fdgothilo;
866 e2->fdgoff12 |= e1->fdgoff12;
867 e2->fdgofflos |= e1->fdgofflos;
868 e2->fdgoffhilo |= e1->fdgoffhilo;
869 e2->gotoff |= e1->gotoff;
870 e2->call |= e1->call;
871 e2->sym |= e1->sym;
873 #if 0
874 /* These are set in _frvfdpic_count_got_plt_entries() or later, and this
875 function is only called in _frvfdpic_resolve_final_relocs_info(), that
876 runs just before it, so we don't have to worry about the fields
877 below. */
879 e2->plt |= e1->plt;
880 e2->privfd |= e1->privfd;
881 e2->lazyplt |= e1->lazyplt;
882 e2->done |= e1->done;
884 e2->relocs32 += e1->relocs32;
885 e2->relocsfd += e1->relocsfd;
886 e2->relocsfdv += e1->relocsfdv;
887 e2->fixups += e1->fixups;
888 e2->dynrelocs += e1->dynrelocs;
890 if (abs (e1->got_entry) < abs (e2->got_entry))
891 e2->got_entry = e1->got_entry;
892 if (abs (e1->fdgot_entry) < abs (e2->fdgot_entry))
893 e2->fdgot_entry = e1->fdgot_entry;
894 if (abs (e1->fd_entry) < abs (e2->fd_entry))
895 e2->fd_entry = e1->fd_entry;
897 if (e1->plt_entry < e2->plt_entry)
898 e2->plt_entry = e1->plt_entry;
899 if (e1->lzplt_entry < e2->lzplt_entry)
900 e2->lzplt_entry = e1->lzplt_entry;
901 #endif
904 /* Every block of 65535 lazy PLT entries shares a single call to the
905 resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
906 32767, counting from 0). All other lazy PLT entries branch to it
907 in a single instruction. */
909 #define FRVFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) 8 * 65535 + 4)
910 #define FRVFDPIC_LZPLT_RESOLV_LOC (8 * 32767)
912 /* Add a dynamic relocation to the SRELOC section. */
914 inline static bfd_vma
915 _frvfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
916 int reloc_type, long dynindx, bfd_vma addend,
917 struct frvfdpic_relocs_info *entry)
919 Elf_Internal_Rela outrel;
920 bfd_vma reloc_offset;
922 outrel.r_offset = offset;
923 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
924 outrel.r_addend = addend;
926 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
927 BFD_ASSERT (reloc_offset < sreloc->size);
928 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
929 sreloc->contents + reloc_offset);
930 sreloc->reloc_count++;
932 /* If the entry's index is zero, this relocation was probably to a
933 linkonce section that got discarded. We reserved a dynamic
934 relocation, but it was for another entry than the one we got at
935 the time of emitting the relocation. Unfortunately there's no
936 simple way for us to catch this situation, since the relocation
937 is cleared right before calling relocate_section, at which point
938 we no longer know what the relocation used to point to. */
939 if (entry->symndx)
941 BFD_ASSERT (entry->dynrelocs > 0);
942 entry->dynrelocs--;
945 return reloc_offset;
948 /* Add a fixup to the ROFIXUP section. */
950 static bfd_vma
951 _frvfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
952 struct frvfdpic_relocs_info *entry)
954 bfd_vma fixup_offset;
956 if (rofixup->flags & SEC_EXCLUDE)
957 return -1;
959 fixup_offset = rofixup->reloc_count * 4;
960 if (rofixup->contents)
962 BFD_ASSERT (fixup_offset < rofixup->size);
963 bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
965 rofixup->reloc_count++;
967 if (entry && entry->symndx)
969 /* See discussion about symndx == 0 in _frvfdpic_add_dyn_reloc
970 above. */
971 BFD_ASSERT (entry->fixups > 0);
972 entry->fixups--;
975 return fixup_offset;
978 /* Find the segment number in which OSEC, and output section, is
979 located. */
981 static unsigned
982 _frvfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
984 struct elf_segment_map *m;
985 Elf_Internal_Phdr *p;
987 /* Find the segment that contains the output_section. */
988 for (m = elf_tdata (output_bfd)->segment_map,
989 p = elf_tdata (output_bfd)->phdr;
990 m != NULL;
991 m = m->next, p++)
993 int i;
995 for (i = m->count - 1; i >= 0; i--)
996 if (m->sections[i] == osec)
997 break;
999 if (i >= 0)
1000 break;
1003 return p - elf_tdata (output_bfd)->phdr;
1006 inline static bfd_boolean
1007 _frvfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
1009 unsigned seg = _frvfdpic_osec_to_segment (output_bfd, osec);
1011 return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
1014 /* Generate relocations for GOT entries, function descriptors, and
1015 code for PLT and lazy PLT entries. */
1017 inline static bfd_boolean
1018 _frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
1019 bfd *output_bfd,
1020 struct bfd_link_info *info,
1021 asection *sec,
1022 Elf_Internal_Sym *sym,
1023 bfd_vma addend)
1026 bfd_vma fd_lazy_rel_offset = (bfd_vma)-1;
1027 int dynindx = -1;
1029 if (entry->done)
1030 return TRUE;
1031 entry->done = 1;
1033 if (entry->got_entry || entry->fdgot_entry || entry->fd_entry)
1035 /* If the symbol is dynamic, consider it for dynamic
1036 relocations, otherwise decay to section + offset. */
1037 if (entry->symndx == -1 && entry->d.h->dynindx != -1)
1038 dynindx = entry->d.h->dynindx;
1039 else
1041 if (sec->output_section
1042 && ! bfd_is_abs_section (sec->output_section)
1043 && ! bfd_is_und_section (sec->output_section))
1044 dynindx = elf_section_data (sec->output_section)->dynindx;
1045 else
1046 dynindx = 0;
1050 /* Generate relocation for GOT entry pointing to the symbol. */
1051 if (entry->got_entry)
1053 int idx = dynindx;
1054 bfd_vma ad = addend;
1056 /* If the symbol is dynamic but binds locally, use
1057 section+offset. */
1058 if (sec && (entry->symndx != -1
1059 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1061 if (entry->symndx == -1)
1062 ad += entry->d.h->root.u.def.value;
1063 else
1064 ad += sym->st_value;
1065 ad += sec->output_offset;
1066 if (sec->output_section && elf_section_data (sec->output_section))
1067 idx = elf_section_data (sec->output_section)->dynindx;
1068 else
1069 idx = 0;
1072 /* If we're linking an executable at a fixed address, we can
1073 omit the dynamic relocation as long as the symbol is local to
1074 this module. */
1075 if (info->executable && !info->pie
1076 && (entry->symndx != -1
1077 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1079 if (sec)
1080 ad += sec->output_section->vma;
1081 if (entry->symndx != -1
1082 || entry->d.h->root.type != bfd_link_hash_undefweak)
1083 _frvfdpic_add_rofixup (output_bfd,
1084 frvfdpic_gotfixup_section (info),
1085 frvfdpic_got_section (info)->output_section
1086 ->vma
1087 + frvfdpic_got_section (info)->output_offset
1088 + frvfdpic_got_initial_offset (info)
1089 + entry->got_entry, entry);
1091 else
1092 _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1093 _bfd_elf_section_offset
1094 (output_bfd, info,
1095 frvfdpic_got_section (info),
1096 frvfdpic_got_initial_offset (info)
1097 + entry->got_entry)
1098 + frvfdpic_got_section (info)
1099 ->output_section->vma
1100 + frvfdpic_got_section (info)->output_offset,
1101 R_FRV_32, idx, ad, entry);
1103 bfd_put_32 (output_bfd, ad,
1104 frvfdpic_got_section (info)->contents
1105 + frvfdpic_got_initial_offset (info)
1106 + entry->got_entry);
1109 /* Generate relocation for GOT entry pointing to a canonical
1110 function descriptor. */
1111 if (entry->fdgot_entry)
1113 int reloc, idx;
1114 bfd_vma ad = 0;
1116 if (! (entry->symndx == -1
1117 && entry->d.h->root.type == bfd_link_hash_undefweak
1118 && FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1120 /* If the symbol is dynamic and there may be dynamic symbol
1121 resolution because we are, or are linked with, a shared
1122 library, emit a FUNCDESC relocation such that the dynamic
1123 linker will allocate the function descriptor. If the
1124 symbol needs a non-local function descriptor but binds
1125 locally (e.g., its visibility is protected, emit a
1126 dynamic relocation decayed to section+offset. */
1127 if (entry->symndx == -1
1128 && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
1129 && FRVFDPIC_SYM_LOCAL (info, entry->d.h)
1130 && !(info->executable && !info->pie))
1132 reloc = R_FRV_FUNCDESC;
1133 idx = elf_section_data (entry->d.h->root.u.def.section
1134 ->output_section)->dynindx;
1135 ad = entry->d.h->root.u.def.section->output_offset
1136 + entry->d.h->root.u.def.value;
1138 else if (entry->symndx == -1
1139 && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
1141 reloc = R_FRV_FUNCDESC;
1142 idx = dynindx;
1143 ad = addend;
1144 if (ad)
1145 return FALSE;
1147 else
1149 /* Otherwise, we know we have a private function descriptor,
1150 so reference it directly. */
1151 if (elf_hash_table (info)->dynamic_sections_created)
1152 BFD_ASSERT (entry->privfd);
1153 reloc = R_FRV_32;
1154 idx = elf_section_data (frvfdpic_got_section (info)
1155 ->output_section)->dynindx;
1156 ad = frvfdpic_got_section (info)->output_offset
1157 + frvfdpic_got_initial_offset (info) + entry->fd_entry;
1160 /* If there is room for dynamic symbol resolution, emit the
1161 dynamic relocation. However, if we're linking an
1162 executable at a fixed location, we won't have emitted a
1163 dynamic symbol entry for the got section, so idx will be
1164 zero, which means we can and should compute the address
1165 of the private descriptor ourselves. */
1166 if (info->executable && !info->pie
1167 && (entry->symndx != -1
1168 || FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
1170 ad += frvfdpic_got_section (info)->output_section->vma;
1171 _frvfdpic_add_rofixup (output_bfd,
1172 frvfdpic_gotfixup_section (info),
1173 frvfdpic_got_section (info)
1174 ->output_section->vma
1175 + frvfdpic_got_section (info)
1176 ->output_offset
1177 + frvfdpic_got_initial_offset (info)
1178 + entry->fdgot_entry, entry);
1180 else
1181 _frvfdpic_add_dyn_reloc (output_bfd,
1182 frvfdpic_gotrel_section (info),
1183 _bfd_elf_section_offset
1184 (output_bfd, info,
1185 frvfdpic_got_section (info),
1186 frvfdpic_got_initial_offset (info)
1187 + entry->fdgot_entry)
1188 + frvfdpic_got_section (info)
1189 ->output_section->vma
1190 + frvfdpic_got_section (info)
1191 ->output_offset,
1192 reloc, idx, ad, entry);
1195 bfd_put_32 (output_bfd, ad,
1196 frvfdpic_got_section (info)->contents
1197 + frvfdpic_got_initial_offset (info)
1198 + entry->fdgot_entry);
1201 /* Generate relocation to fill in a private function descriptor in
1202 the GOT. */
1203 if (entry->fd_entry)
1205 int idx = dynindx;
1206 bfd_vma ad = addend;
1207 bfd_vma ofst;
1208 long lowword, highword;
1210 /* If the symbol is dynamic but binds locally, use
1211 section+offset. */
1212 if (sec && (entry->symndx != -1
1213 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1215 if (entry->symndx == -1)
1216 ad += entry->d.h->root.u.def.value;
1217 else
1218 ad += sym->st_value;
1219 ad += sec->output_offset;
1220 if (sec->output_section && elf_section_data (sec->output_section))
1221 idx = elf_section_data (sec->output_section)->dynindx;
1222 else
1223 idx = 0;
1226 /* If we're linking an executable at a fixed address, we can
1227 omit the dynamic relocation as long as the symbol is local to
1228 this module. */
1229 if (info->executable && !info->pie
1230 && (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1232 if (sec)
1233 ad += sec->output_section->vma;
1234 ofst = 0;
1235 if (entry->symndx != -1
1236 || entry->d.h->root.type != bfd_link_hash_undefweak)
1238 _frvfdpic_add_rofixup (output_bfd,
1239 frvfdpic_gotfixup_section (info),
1240 frvfdpic_got_section (info)
1241 ->output_section->vma
1242 + frvfdpic_got_section (info)
1243 ->output_offset
1244 + frvfdpic_got_initial_offset (info)
1245 + entry->fd_entry, entry);
1246 _frvfdpic_add_rofixup (output_bfd,
1247 frvfdpic_gotfixup_section (info),
1248 frvfdpic_got_section (info)
1249 ->output_section->vma
1250 + frvfdpic_got_section (info)
1251 ->output_offset
1252 + frvfdpic_got_initial_offset (info)
1253 + entry->fd_entry + 4, entry);
1256 else
1258 ofst =
1259 _frvfdpic_add_dyn_reloc (output_bfd,
1260 entry->lazyplt
1261 ? frvfdpic_pltrel_section (info)
1262 : frvfdpic_gotrel_section (info),
1263 _bfd_elf_section_offset
1264 (output_bfd, info,
1265 frvfdpic_got_section (info),
1266 frvfdpic_got_initial_offset (info)
1267 + entry->fd_entry)
1268 + frvfdpic_got_section (info)
1269 ->output_section->vma
1270 + frvfdpic_got_section (info)
1271 ->output_offset,
1272 R_FRV_FUNCDESC_VALUE, idx, ad, entry);
1275 /* If we've omitted the dynamic relocation, just emit the fixed
1276 addresses of the symbol and of the local GOT base offset. */
1277 if (info->executable && !info->pie && sec && sec->output_section)
1279 lowword = ad;
1280 highword = frvfdpic_got_section (info)->output_section->vma
1281 + frvfdpic_got_section (info)->output_offset
1282 + frvfdpic_got_initial_offset (info);
1284 else if (entry->lazyplt)
1286 if (ad)
1287 return FALSE;
1289 fd_lazy_rel_offset = ofst;
1291 /* A function descriptor used for lazy or local resolving is
1292 initialized such that its high word contains the output
1293 section index in which the PLT entries are located, and
1294 the low word contains the address of the lazy PLT entry
1295 entry point, that must be within the memory region
1296 assigned to that section. */
1297 lowword = entry->lzplt_entry + 4
1298 + frvfdpic_plt_section (info)->output_offset
1299 + frvfdpic_plt_section (info)->output_section->vma;
1300 highword = _frvfdpic_osec_to_segment
1301 (output_bfd, frvfdpic_plt_section (info)->output_section);
1303 else
1305 /* A function descriptor for a local function gets the index
1306 of the section. For a non-local function, it's
1307 disregarded. */
1308 lowword = ad;
1309 if (entry->symndx == -1 && entry->d.h->dynindx != -1
1310 && entry->d.h->dynindx == idx)
1311 highword = 0;
1312 else
1313 highword = _frvfdpic_osec_to_segment
1314 (output_bfd, sec->output_section);
1317 bfd_put_32 (output_bfd, lowword,
1318 frvfdpic_got_section (info)->contents
1319 + frvfdpic_got_initial_offset (info)
1320 + entry->fd_entry);
1321 bfd_put_32 (output_bfd, highword,
1322 frvfdpic_got_section (info)->contents
1323 + frvfdpic_got_initial_offset (info)
1324 + entry->fd_entry + 4);
1327 /* Generate code for the PLT entry. */
1328 if (entry->plt_entry != (bfd_vma) -1)
1330 bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1331 + entry->plt_entry;
1333 BFD_ASSERT (entry->fd_entry);
1335 /* Figure out what kind of PLT entry we need, depending on the
1336 location of the function descriptor within the GOT. */
1337 if (entry->fd_entry >= -(1 << (12 - 1))
1338 && entry->fd_entry < (1 << (12 - 1)))
1340 /* lddi @(gr15, fd_entry), gr14 */
1341 bfd_put_32 (output_bfd,
1342 0x9cccf000 | (entry->fd_entry & ((1 << 12) - 1)),
1343 plt_code);
1344 plt_code += 4;
1346 else
1348 if (entry->fd_entry >= -(1 << (16 - 1))
1349 && entry->fd_entry < (1 << (16 - 1)))
1351 /* setlos lo(fd_entry), gr14 */
1352 bfd_put_32 (output_bfd,
1353 0x9cfc0000
1354 | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1355 plt_code);
1356 plt_code += 4;
1358 else
1360 /* sethi.p hi(fd_entry), gr14
1361 setlo lo(fd_entry), gr14 */
1362 bfd_put_32 (output_bfd,
1363 0x1cf80000
1364 | ((entry->fd_entry >> 16)
1365 & (((bfd_vma)1 << 16) - 1)),
1366 plt_code);
1367 bfd_put_32 (output_bfd,
1368 0x9cf40000
1369 | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1370 plt_code);
1371 plt_code += 8;
1373 /* ldd @(gr14,gr15),gr14 */
1374 bfd_put_32 (output_bfd, 0x9c08e14f, plt_code);
1375 plt_code += 4;
1377 /* jmpl @(gr14,gr0) */
1378 bfd_put_32 (output_bfd, 0x8030e000, plt_code);
1381 /* Generate code for the lazy PLT entry. */
1382 if (entry->lzplt_entry != (bfd_vma) -1)
1384 bfd_byte *lzplt_code = frvfdpic_plt_section (info)->contents
1385 + entry->lzplt_entry;
1386 bfd_vma resolverStub_addr;
1388 bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
1389 lzplt_code += 4;
1391 resolverStub_addr = entry->lzplt_entry / FRVFDPIC_LZPLT_BLOCK_SIZE
1392 * FRVFDPIC_LZPLT_BLOCK_SIZE + FRVFDPIC_LZPLT_RESOLV_LOC;
1393 if (resolverStub_addr >= frvfdpic_plt_initial_offset (info))
1394 resolverStub_addr = frvfdpic_plt_initial_offset (info) - 12;
1396 if (entry->lzplt_entry == resolverStub_addr)
1398 /* This is a lazy PLT entry that includes a resolver call. */
1399 /* ldd @(gr15,gr0), gr4
1400 jmpl @(gr4,gr0) */
1401 bfd_put_32 (output_bfd, 0x8808f140, lzplt_code);
1402 bfd_put_32 (output_bfd, 0x80304000, lzplt_code + 4);
1404 else
1406 /* bra resolverStub */
1407 bfd_put_32 (output_bfd,
1408 0xc01a0000
1409 | (((resolverStub_addr - entry->lzplt_entry)
1410 / 4) & (((bfd_vma)1 << 16) - 1)),
1411 lzplt_code);
1415 return TRUE;
1418 /* Handle an FRV small data reloc. */
1420 static bfd_reloc_status_type
1421 elf32_frv_relocate_gprel12 (info, input_bfd, input_section, relocation,
1422 contents, value)
1423 struct bfd_link_info *info;
1424 bfd *input_bfd;
1425 asection *input_section;
1426 Elf_Internal_Rela *relocation;
1427 bfd_byte *contents;
1428 bfd_vma value;
1430 bfd_vma insn;
1431 bfd_vma gp;
1432 struct bfd_link_hash_entry *h;
1434 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1436 gp = (h->u.def.value
1437 + h->u.def.section->output_section->vma
1438 + h->u.def.section->output_offset);
1440 value -= input_section->output_section->vma;
1441 value -= (gp - input_section->output_section->vma);
1443 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1445 value += relocation->r_addend;
1447 if ((long) value > 0x7ff || (long) value < -0x800)
1448 return bfd_reloc_overflow;
1450 bfd_put_32 (input_bfd,
1451 (insn & 0xfffff000) | (value & 0xfff),
1452 contents + relocation->r_offset);
1454 return bfd_reloc_ok;
1457 /* Handle an FRV small data reloc. for the u12 field. */
1459 static bfd_reloc_status_type
1460 elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, relocation,
1461 contents, value)
1462 struct bfd_link_info *info;
1463 bfd *input_bfd;
1464 asection *input_section;
1465 Elf_Internal_Rela *relocation;
1466 bfd_byte *contents;
1467 bfd_vma value;
1469 bfd_vma insn;
1470 bfd_vma gp;
1471 struct bfd_link_hash_entry *h;
1472 bfd_vma mask;
1474 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1476 gp = (h->u.def.value
1477 + h->u.def.section->output_section->vma
1478 + h->u.def.section->output_offset);
1480 value -= input_section->output_section->vma;
1481 value -= (gp - input_section->output_section->vma);
1483 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1485 value += relocation->r_addend;
1487 if ((long) value > 0x7ff || (long) value < -0x800)
1488 return bfd_reloc_overflow;
1490 /* The high 6 bits go into bits 17-12. The low 6 bits go into bits 5-0. */
1491 mask = 0x3f03f;
1492 insn = (insn & ~mask) | ((value & 0xfc0) << 12) | (value & 0x3f);
1494 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1496 return bfd_reloc_ok;
1499 /* Handle an FRV ELF HI16 reloc. */
1501 static bfd_reloc_status_type
1502 elf32_frv_relocate_hi16 (input_bfd, relhi, contents, value)
1503 bfd *input_bfd;
1504 Elf_Internal_Rela *relhi;
1505 bfd_byte *contents;
1506 bfd_vma value;
1508 bfd_vma insn;
1510 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
1512 value += relhi->r_addend;
1513 value = ((value >> 16) & 0xffff);
1515 insn = (insn & 0xffff0000) | value;
1517 if ((long) value > 0xffff || (long) value < -0x10000)
1518 return bfd_reloc_overflow;
1520 bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
1521 return bfd_reloc_ok;
1524 static bfd_reloc_status_type
1525 elf32_frv_relocate_lo16 (input_bfd, rello, contents, value)
1526 bfd *input_bfd;
1527 Elf_Internal_Rela *rello;
1528 bfd_byte *contents;
1529 bfd_vma value;
1531 bfd_vma insn;
1533 insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
1535 value += rello->r_addend;
1536 value = value & 0xffff;
1538 insn = (insn & 0xffff0000) | value;
1540 if ((long) value > 0xffff || (long) value < -0x10000)
1541 return bfd_reloc_overflow;
1543 bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
1544 return bfd_reloc_ok;
1547 /* Perform the relocation for the CALL label24 instruction. */
1549 static bfd_reloc_status_type
1550 elf32_frv_relocate_label24 (input_bfd, input_section, rello, contents, value)
1551 bfd *input_bfd;
1552 asection *input_section;
1553 Elf_Internal_Rela *rello;
1554 bfd_byte *contents;
1555 bfd_vma value;
1557 bfd_vma insn;
1558 bfd_vma label6;
1559 bfd_vma label18;
1561 /* The format for the call instruction is:
1563 0 000000 0001111 000000000000000000
1564 label6 opcode label18
1566 The branch calculation is: pc + (4*label24)
1567 where label24 is the concatenation of label6 and label18. */
1569 /* Grab the instruction. */
1570 insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
1572 value -= input_section->output_section->vma + input_section->output_offset;
1573 value -= rello->r_offset;
1574 value += rello->r_addend;
1576 value = value >> 2;
1578 label6 = value & 0xfc0000;
1579 label6 = label6 << 7;
1581 label18 = value & 0x3ffff;
1583 insn = insn & 0x803c0000;
1584 insn = insn | label6;
1585 insn = insn | label18;
1587 bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
1589 return bfd_reloc_ok;
1592 static bfd_reloc_status_type
1593 elf32_frv_relocate_gprelhi (info, input_bfd, input_section, relocation,
1594 contents, value)
1595 struct bfd_link_info *info;
1596 bfd *input_bfd;
1597 asection *input_section;
1598 Elf_Internal_Rela *relocation;
1599 bfd_byte *contents;
1600 bfd_vma value;
1602 bfd_vma insn;
1603 bfd_vma gp;
1604 struct bfd_link_hash_entry *h;
1606 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1608 gp = (h->u.def.value
1609 + h->u.def.section->output_section->vma
1610 + h->u.def.section->output_offset);
1612 value -= input_section->output_section->vma;
1613 value -= (gp - input_section->output_section->vma);
1614 value += relocation->r_addend;
1615 value = ((value >> 16) & 0xffff);
1617 if ((long) value > 0xffff || (long) value < -0x10000)
1618 return bfd_reloc_overflow;
1620 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1621 insn = (insn & 0xffff0000) | value;
1623 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1624 return bfd_reloc_ok;
1627 static bfd_reloc_status_type
1628 elf32_frv_relocate_gprello (info, input_bfd, input_section, relocation,
1629 contents, value)
1630 struct bfd_link_info *info;
1631 bfd *input_bfd;
1632 asection *input_section;
1633 Elf_Internal_Rela *relocation;
1634 bfd_byte *contents;
1635 bfd_vma value;
1637 bfd_vma insn;
1638 bfd_vma gp;
1639 struct bfd_link_hash_entry *h;
1641 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1643 gp = (h->u.def.value
1644 + h->u.def.section->output_section->vma
1645 + h->u.def.section->output_offset);
1647 value -= input_section->output_section->vma;
1648 value -= (gp - input_section->output_section->vma);
1649 value += relocation->r_addend;
1650 value = value & 0xffff;
1652 if ((long) value > 0xffff || (long) value < -0x10000)
1653 return bfd_reloc_overflow;
1655 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
1656 insn = (insn & 0xffff0000) | value;
1658 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
1660 return bfd_reloc_ok;
1663 static reloc_howto_type *
1664 frv_reloc_type_lookup (abfd, code)
1665 bfd *abfd ATTRIBUTE_UNUSED;
1666 bfd_reloc_code_real_type code;
1668 switch (code)
1670 default:
1671 break;
1673 case BFD_RELOC_NONE:
1674 return &elf32_frv_howto_table[ (int) R_FRV_NONE];
1676 case BFD_RELOC_32:
1677 if (elf_elfheader (abfd)->e_type == ET_EXEC
1678 || elf_elfheader (abfd)->e_type == ET_DYN)
1679 return &elf32_frv_rel_32_howto;
1680 /* Fall through. */
1681 case BFD_RELOC_CTOR:
1682 return &elf32_frv_howto_table[ (int) R_FRV_32];
1684 case BFD_RELOC_FRV_LABEL16:
1685 return &elf32_frv_howto_table[ (int) R_FRV_LABEL16];
1687 case BFD_RELOC_FRV_LABEL24:
1688 return &elf32_frv_howto_table[ (int) R_FRV_LABEL24];
1690 case BFD_RELOC_FRV_LO16:
1691 return &elf32_frv_howto_table[ (int) R_FRV_LO16];
1693 case BFD_RELOC_FRV_HI16:
1694 return &elf32_frv_howto_table[ (int) R_FRV_HI16];
1696 case BFD_RELOC_FRV_GPREL12:
1697 return &elf32_frv_howto_table[ (int) R_FRV_GPREL12];
1699 case BFD_RELOC_FRV_GPRELU12:
1700 return &elf32_frv_howto_table[ (int) R_FRV_GPRELU12];
1702 case BFD_RELOC_FRV_GPREL32:
1703 return &elf32_frv_howto_table[ (int) R_FRV_GPREL32];
1705 case BFD_RELOC_FRV_GPRELHI:
1706 return &elf32_frv_howto_table[ (int) R_FRV_GPRELHI];
1708 case BFD_RELOC_FRV_GPRELLO:
1709 return &elf32_frv_howto_table[ (int) R_FRV_GPRELLO];
1711 case BFD_RELOC_FRV_GOT12:
1712 return &elf32_frv_howto_table[ (int) R_FRV_GOT12];
1714 case BFD_RELOC_FRV_GOTHI:
1715 return &elf32_frv_howto_table[ (int) R_FRV_GOTHI];
1717 case BFD_RELOC_FRV_GOTLO:
1718 return &elf32_frv_howto_table[ (int) R_FRV_GOTLO];
1720 case BFD_RELOC_FRV_FUNCDESC:
1721 if (elf_elfheader (abfd)->e_type == ET_EXEC
1722 || elf_elfheader (abfd)->e_type == ET_DYN)
1723 return &elf32_frv_rel_funcdesc_howto;
1724 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC];
1726 case BFD_RELOC_FRV_FUNCDESC_GOT12:
1727 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOT12];
1729 case BFD_RELOC_FRV_FUNCDESC_GOTHI:
1730 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTHI];
1732 case BFD_RELOC_FRV_FUNCDESC_GOTLO:
1733 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTLO];
1735 case BFD_RELOC_FRV_FUNCDESC_VALUE:
1736 if (elf_elfheader (abfd)->e_type == ET_EXEC
1737 || elf_elfheader (abfd)->e_type == ET_DYN)
1738 return &elf32_frv_rel_funcdesc_value_howto;
1739 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_VALUE];
1741 case BFD_RELOC_FRV_FUNCDESC_GOTOFF12:
1742 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFF12];
1744 case BFD_RELOC_FRV_FUNCDESC_GOTOFFHI:
1745 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFHI];
1747 case BFD_RELOC_FRV_FUNCDESC_GOTOFFLO:
1748 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFLO];
1750 case BFD_RELOC_FRV_GOTOFF12:
1751 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFF12];
1753 case BFD_RELOC_FRV_GOTOFFHI:
1754 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFHI];
1756 case BFD_RELOC_FRV_GOTOFFLO:
1757 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFLO];
1759 case BFD_RELOC_VTABLE_INHERIT:
1760 return &elf32_frv_vtinherit_howto;
1762 case BFD_RELOC_VTABLE_ENTRY:
1763 return &elf32_frv_vtentry_howto;
1766 return NULL;
1769 /* Set the howto pointer for an FRV ELF reloc. */
1771 static void
1772 frv_info_to_howto_rela (abfd, cache_ptr, dst)
1773 bfd *abfd ATTRIBUTE_UNUSED;
1774 arelent *cache_ptr;
1775 Elf_Internal_Rela *dst;
1777 unsigned int r_type;
1779 r_type = ELF32_R_TYPE (dst->r_info);
1780 switch (r_type)
1782 case R_FRV_GNU_VTINHERIT:
1783 cache_ptr->howto = &elf32_frv_vtinherit_howto;
1784 break;
1786 case R_FRV_GNU_VTENTRY:
1787 cache_ptr->howto = &elf32_frv_vtentry_howto;
1788 break;
1790 default:
1791 cache_ptr->howto = & elf32_frv_howto_table [r_type];
1792 break;
1796 /* Set the howto pointer for an FRV ELF REL reloc. */
1797 static void
1798 frvfdpic_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
1799 arelent *cache_ptr, Elf_Internal_Rela *dst)
1801 unsigned int r_type;
1803 r_type = ELF32_R_TYPE (dst->r_info);
1804 switch (r_type)
1806 case R_FRV_32:
1807 cache_ptr->howto = &elf32_frv_rel_32_howto;
1808 break;
1810 case R_FRV_FUNCDESC:
1811 cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
1812 break;
1814 case R_FRV_FUNCDESC_VALUE:
1815 cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
1816 break;
1818 default:
1819 cache_ptr->howto = NULL;
1820 break;
1824 /* Perform a single relocation. By default we use the standard BFD
1825 routines, but a few relocs, we have to do them ourselves. */
1827 static bfd_reloc_status_type
1828 frv_final_link_relocate (howto, input_bfd, input_section, contents, rel,
1829 relocation)
1830 reloc_howto_type *howto;
1831 bfd *input_bfd;
1832 asection *input_section;
1833 bfd_byte *contents;
1834 Elf_Internal_Rela *rel;
1835 bfd_vma relocation;
1837 return _bfd_final_link_relocate (howto, input_bfd, input_section,
1838 contents, rel->r_offset, relocation,
1839 rel->r_addend);
1843 /* Relocate an FRV ELF section.
1845 The RELOCATE_SECTION function is called by the new ELF backend linker
1846 to handle the relocations for a section.
1848 The relocs are always passed as Rela structures; if the section
1849 actually uses Rel structures, the r_addend field will always be
1850 zero.
1852 This function is responsible for adjusting the section contents as
1853 necessary, and (if using Rela relocs and generating a relocatable
1854 output file) adjusting the reloc addend as necessary.
1856 This function does not have to worry about setting the reloc
1857 address or the reloc symbol index.
1859 LOCAL_SYMS is a pointer to the swapped in local symbols.
1861 LOCAL_SECTIONS is an array giving the section in the input file
1862 corresponding to the st_shndx field of each local symbol.
1864 The global hash table entry for the global symbols can be found
1865 via elf_sym_hashes (input_bfd).
1867 When generating relocatable output, this function must handle
1868 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1869 going to be the section symbol corresponding to the output
1870 section, which means that the addend must be adjusted
1871 accordingly. */
1873 static bfd_boolean
1874 elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
1875 contents, relocs, local_syms, local_sections)
1876 bfd *output_bfd ATTRIBUTE_UNUSED;
1877 struct bfd_link_info *info;
1878 bfd *input_bfd;
1879 asection *input_section;
1880 bfd_byte *contents;
1881 Elf_Internal_Rela *relocs;
1882 Elf_Internal_Sym *local_syms;
1883 asection **local_sections;
1885 Elf_Internal_Shdr *symtab_hdr;
1886 struct elf_link_hash_entry **sym_hashes;
1887 Elf_Internal_Rela *rel;
1888 Elf_Internal_Rela *relend;
1889 unsigned isec_segment, got_segment, plt_segment, gprel_segment,
1890 check_segment[2];
1891 int silence_segment_error = !(info->shared || info->pie);
1893 if (info->relocatable)
1894 return TRUE;
1896 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1897 sym_hashes = elf_sym_hashes (input_bfd);
1898 relend = relocs + input_section->reloc_count;
1900 isec_segment = _frvfdpic_osec_to_segment (output_bfd,
1901 input_section->output_section);
1902 if (IS_FDPIC (output_bfd) && frvfdpic_got_section (info))
1903 got_segment = _frvfdpic_osec_to_segment (output_bfd,
1904 frvfdpic_got_section (info)
1905 ->output_section);
1906 else
1907 got_segment = -1;
1908 if (IS_FDPIC (output_bfd) && frvfdpic_gotfixup_section (info))
1909 gprel_segment = _frvfdpic_osec_to_segment (output_bfd,
1910 frvfdpic_gotfixup_section (info)
1911 ->output_section);
1912 else
1913 gprel_segment = -1;
1914 if (IS_FDPIC (output_bfd) && elf_hash_table (info)->dynamic_sections_created)
1915 plt_segment = _frvfdpic_osec_to_segment (output_bfd,
1916 frvfdpic_plt_section (info)
1917 ->output_section);
1918 else
1919 plt_segment = -1;
1921 for (rel = relocs; rel < relend; rel ++)
1923 reloc_howto_type *howto;
1924 unsigned long r_symndx;
1925 Elf_Internal_Sym *sym;
1926 asection *sec;
1927 struct elf_link_hash_entry *h;
1928 bfd_vma relocation;
1929 bfd_reloc_status_type r;
1930 const char * name = NULL;
1931 int r_type;
1932 asection *osec;
1933 struct frvfdpic_relocs_info *picrel;
1934 bfd_vma orig_addend = rel->r_addend;
1936 r_type = ELF32_R_TYPE (rel->r_info);
1938 if ( r_type == R_FRV_GNU_VTINHERIT
1939 || r_type == R_FRV_GNU_VTENTRY)
1940 continue;
1942 /* This is a final link. */
1943 r_symndx = ELF32_R_SYM (rel->r_info);
1944 howto = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
1945 h = NULL;
1946 sym = NULL;
1947 sec = NULL;
1949 if (r_symndx < symtab_hdr->sh_info)
1951 sym = local_syms + r_symndx;
1952 osec = sec = local_sections [r_symndx];
1953 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1955 name = bfd_elf_string_from_elf_section
1956 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1957 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1959 else
1961 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1963 while (h->root.type == bfd_link_hash_indirect
1964 || h->root.type == bfd_link_hash_warning)
1965 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1967 name = h->root.root.string;
1969 if ((h->root.type == bfd_link_hash_defined
1970 || h->root.type == bfd_link_hash_defweak)
1971 && ! FRVFDPIC_SYM_LOCAL (info, h))
1973 sec = NULL;
1974 relocation = 0;
1976 else
1977 if (h->root.type == bfd_link_hash_defined
1978 || h->root.type == bfd_link_hash_defweak)
1980 sec = h->root.u.def.section;
1981 relocation = (h->root.u.def.value
1982 + sec->output_section->vma
1983 + sec->output_offset);
1985 else if (h->root.type == bfd_link_hash_undefweak)
1987 relocation = 0;
1989 else if (info->unresolved_syms_in_objects == RM_IGNORE
1990 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1991 relocation = 0;
1992 else
1994 if (! ((*info->callbacks->undefined_symbol)
1995 (info, h->root.root.string, input_bfd,
1996 input_section, rel->r_offset,
1997 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
1998 || ELF_ST_VISIBILITY (h->other)))))
1999 return FALSE;
2000 relocation = 0;
2002 osec = sec;
2005 switch (r_type)
2007 case R_FRV_LABEL24:
2008 case R_FRV_32:
2009 if (! IS_FDPIC (output_bfd))
2010 goto non_fdpic;
2012 case R_FRV_GOT12:
2013 case R_FRV_GOTHI:
2014 case R_FRV_GOTLO:
2015 case R_FRV_FUNCDESC_GOT12:
2016 case R_FRV_FUNCDESC_GOTHI:
2017 case R_FRV_FUNCDESC_GOTLO:
2018 case R_FRV_GOTOFF12:
2019 case R_FRV_GOTOFFHI:
2020 case R_FRV_GOTOFFLO:
2021 case R_FRV_FUNCDESC_GOTOFF12:
2022 case R_FRV_FUNCDESC_GOTOFFHI:
2023 case R_FRV_FUNCDESC_GOTOFFLO:
2024 case R_FRV_FUNCDESC:
2025 case R_FRV_FUNCDESC_VALUE:
2026 if (h != NULL)
2027 picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info
2028 (info), input_bfd, h,
2029 orig_addend, INSERT);
2030 else
2031 /* In order to find the entry we created before, we must
2032 use the original addend, not the one that may have been
2033 modified by _bfd_elf_rela_local_sym(). */
2034 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
2035 (info), input_bfd, r_symndx,
2036 orig_addend, INSERT);
2037 if (! picrel)
2038 return FALSE;
2040 if (!_frvfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2041 osec, sym,
2042 rel->r_addend))
2044 (*_bfd_error_handler)
2045 (_("%B: relocation at `%A+0x%x' references symbol `%s' with nonzero addend"),
2046 input_bfd, input_section, rel->r_offset, name);
2047 return FALSE;
2051 break;
2053 default:
2054 non_fdpic:
2055 picrel = NULL;
2056 if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
2058 info->callbacks->warning
2059 (info, _("relocation references symbol not defined in the module"),
2060 name, input_bfd, input_section, rel->r_offset);
2061 return FALSE;
2063 break;
2066 switch (r_type)
2068 case R_FRV_LABEL24:
2069 check_segment[0] = isec_segment;
2070 if (! IS_FDPIC (output_bfd))
2071 check_segment[1] = isec_segment;
2072 else if (picrel->plt)
2074 relocation = frvfdpic_plt_section (info)->output_section->vma
2075 + frvfdpic_plt_section (info)->output_offset
2076 + picrel->plt_entry;
2077 check_segment[1] = plt_segment;
2079 /* We don't want to warn on calls to undefined weak symbols,
2080 as calls to them must be protected by non-NULL tests
2081 anyway, and unprotected calls would invoke undefined
2082 behavior. */
2083 else if (picrel->symndx == -1
2084 && picrel->d.h->root.type == bfd_link_hash_undefweak)
2085 check_segment[1] = check_segment[0];
2086 else
2087 check_segment[1] = sec
2088 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
2089 : (unsigned)-1;
2090 break;
2092 case R_FRV_GOT12:
2093 case R_FRV_GOTHI:
2094 case R_FRV_GOTLO:
2095 relocation = picrel->got_entry;
2096 check_segment[0] = check_segment[1] = got_segment;
2097 break;
2099 case R_FRV_FUNCDESC_GOT12:
2100 case R_FRV_FUNCDESC_GOTHI:
2101 case R_FRV_FUNCDESC_GOTLO:
2102 relocation = picrel->fdgot_entry;
2103 check_segment[0] = check_segment[1] = got_segment;
2104 break;
2106 case R_FRV_GOTOFFHI:
2107 case R_FRV_GOTOFF12:
2108 case R_FRV_GOTOFFLO:
2109 relocation -= frvfdpic_got_section (info)->output_section->vma
2110 + frvfdpic_got_section (info)->output_offset
2111 + frvfdpic_got_initial_offset (info);
2112 check_segment[0] = got_segment;
2113 check_segment[1] = sec
2114 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
2115 : (unsigned)-1;
2116 break;
2118 case R_FRV_FUNCDESC_GOTOFF12:
2119 case R_FRV_FUNCDESC_GOTOFFHI:
2120 case R_FRV_FUNCDESC_GOTOFFLO:
2121 relocation = picrel->fd_entry;
2122 check_segment[0] = check_segment[1] = got_segment;
2123 break;
2125 case R_FRV_FUNCDESC:
2127 int dynindx;
2128 bfd_vma addend = rel->r_addend;
2130 if (! (h && h->root.type == bfd_link_hash_undefweak
2131 && FRVFDPIC_SYM_LOCAL (info, h)))
2133 /* If the symbol is dynamic and there may be dynamic
2134 symbol resolution because we are or are linked with a
2135 shared library, emit a FUNCDESC relocation such that
2136 the dynamic linker will allocate the function
2137 descriptor. If the symbol needs a non-local function
2138 descriptor but binds locally (e.g., its visibility is
2139 protected, emit a dynamic relocation decayed to
2140 section+offset. */
2141 if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h)
2142 && FRVFDPIC_SYM_LOCAL (info, h)
2143 && !(info->executable && !info->pie))
2145 dynindx = elf_section_data (h->root.u.def.section
2146 ->output_section)->dynindx;
2147 addend += h->root.u.def.section->output_offset
2148 + h->root.u.def.value;
2150 else if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h))
2152 if (addend)
2154 info->callbacks->warning
2155 (info, _("R_FRV_FUNCDESC references dynamic symbol with nonzero addend"),
2156 name, input_bfd, input_section, rel->r_offset);
2157 return FALSE;
2159 dynindx = h->dynindx;
2161 else
2163 /* Otherwise, we know we have a private function
2164 descriptor, so reference it directly. */
2165 BFD_ASSERT (picrel->privfd);
2166 r_type = R_FRV_32;
2167 dynindx = elf_section_data (frvfdpic_got_section (info)
2168 ->output_section)->dynindx;
2169 addend = frvfdpic_got_section (info)->output_offset
2170 + frvfdpic_got_initial_offset (info)
2171 + picrel->fd_entry;
2174 /* If there is room for dynamic symbol resolution, emit
2175 the dynamic relocation. However, if we're linking an
2176 executable at a fixed location, we won't have emitted a
2177 dynamic symbol entry for the got section, so idx will
2178 be zero, which means we can and should compute the
2179 address of the private descriptor ourselves. */
2180 if (info->executable && !info->pie
2181 && (!h || FRVFDPIC_FUNCDESC_LOCAL (info, h)))
2183 addend += frvfdpic_got_section (info)->output_section->vma;
2184 if ((bfd_get_section_flags (output_bfd,
2185 input_section->output_section)
2186 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2188 if (_frvfdpic_osec_readonly_p (output_bfd,
2189 input_section
2190 ->output_section))
2192 info->callbacks->warning
2193 (info,
2194 _("cannot emit fixups in read-only section"),
2195 name, input_bfd, input_section, rel->r_offset);
2196 return FALSE;
2198 _frvfdpic_add_rofixup (output_bfd,
2199 frvfdpic_gotfixup_section
2200 (info),
2201 _bfd_elf_section_offset
2202 (output_bfd, info,
2203 input_section, rel->r_offset)
2204 + input_section
2205 ->output_section->vma
2206 + input_section->output_offset,
2207 picrel);
2210 else if ((bfd_get_section_flags (output_bfd,
2211 input_section->output_section)
2212 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2214 if (_frvfdpic_osec_readonly_p (output_bfd,
2215 input_section
2216 ->output_section))
2218 info->callbacks->warning
2219 (info,
2220 _("cannot emit dynamic relocations in read-only section"),
2221 name, input_bfd, input_section, rel->r_offset);
2222 return FALSE;
2224 _frvfdpic_add_dyn_reloc (output_bfd,
2225 frvfdpic_gotrel_section (info),
2226 _bfd_elf_section_offset
2227 (output_bfd, info,
2228 input_section, rel->r_offset)
2229 + input_section
2230 ->output_section->vma
2231 + input_section->output_offset,
2232 r_type, dynindx, addend, picrel);
2234 else
2235 addend += frvfdpic_got_section (info)->output_section->vma;
2238 /* We want the addend in-place because dynamic
2239 relocations are REL. Setting relocation to it should
2240 arrange for it to be installed. */
2241 relocation = addend - rel->r_addend;
2243 check_segment[0] = check_segment[1] = got_segment;
2244 break;
2246 case R_FRV_32:
2247 if (! IS_FDPIC (output_bfd))
2249 check_segment[0] = check_segment[1] = -1;
2250 break;
2252 /* Fall through. */
2253 case R_FRV_FUNCDESC_VALUE:
2255 int dynindx;
2256 bfd_vma addend = rel->r_addend;
2258 /* If the symbol is dynamic but binds locally, use
2259 section+offset. */
2260 if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
2262 if (addend && r_type == R_FRV_FUNCDESC_VALUE)
2264 info->callbacks->warning
2265 (info, _("R_FRV_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
2266 name, input_bfd, input_section, rel->r_offset);
2267 return FALSE;
2269 dynindx = h->dynindx;
2271 else
2273 if (h)
2274 addend += h->root.u.def.value;
2275 else
2276 addend += sym->st_value;
2277 if (osec)
2278 addend += osec->output_offset;
2279 if (osec && osec->output_section
2280 && ! bfd_is_abs_section (osec->output_section)
2281 && ! bfd_is_und_section (osec->output_section))
2282 dynindx = elf_section_data (osec->output_section)->dynindx;
2283 else
2284 dynindx = 0;
2287 /* If we're linking an executable at a fixed address, we
2288 can omit the dynamic relocation as long as the symbol
2289 is defined in the current link unit (which is implied
2290 by its output section not being NULL). */
2291 if (info->executable && !info->pie
2292 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
2294 if (osec)
2295 addend += osec->output_section->vma;
2296 if (IS_FDPIC (input_bfd)
2297 && (bfd_get_section_flags (output_bfd,
2298 input_section->output_section)
2299 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2301 if (_frvfdpic_osec_readonly_p (output_bfd,
2302 input_section
2303 ->output_section))
2305 info->callbacks->warning
2306 (info,
2307 _("cannot emit fixups in read-only section"),
2308 name, input_bfd, input_section, rel->r_offset);
2309 return FALSE;
2311 if (!h || h->root.type != bfd_link_hash_undefweak)
2313 _frvfdpic_add_rofixup (output_bfd,
2314 frvfdpic_gotfixup_section
2315 (info),
2316 _bfd_elf_section_offset
2317 (output_bfd, info,
2318 input_section, rel->r_offset)
2319 + input_section
2320 ->output_section->vma
2321 + input_section->output_offset,
2322 picrel);
2323 if (r_type == R_FRV_FUNCDESC_VALUE)
2324 _frvfdpic_add_rofixup
2325 (output_bfd,
2326 frvfdpic_gotfixup_section (info),
2327 _bfd_elf_section_offset
2328 (output_bfd, info,
2329 input_section, rel->r_offset)
2330 + input_section->output_section->vma
2331 + input_section->output_offset + 4, picrel);
2335 else
2337 if ((bfd_get_section_flags (output_bfd,
2338 input_section->output_section)
2339 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2341 if (_frvfdpic_osec_readonly_p (output_bfd,
2342 input_section
2343 ->output_section))
2345 info->callbacks->warning
2346 (info,
2347 _("cannot emit dynamic relocations in read-only section"),
2348 name, input_bfd, input_section, rel->r_offset);
2349 return FALSE;
2351 _frvfdpic_add_dyn_reloc (output_bfd,
2352 frvfdpic_gotrel_section (info),
2353 _bfd_elf_section_offset
2354 (output_bfd, info,
2355 input_section, rel->r_offset)
2356 + input_section
2357 ->output_section->vma
2358 + input_section->output_offset,
2359 r_type, dynindx, addend, picrel);
2361 else if (osec)
2362 addend += osec->output_section->vma;
2363 /* We want the addend in-place because dynamic
2364 relocations are REL. Setting relocation to it
2365 should arrange for it to be installed. */
2366 relocation = addend - rel->r_addend;
2369 if (r_type == R_FRV_FUNCDESC_VALUE)
2371 /* If we've omitted the dynamic relocation, just emit
2372 the fixed addresses of the symbol and of the local
2373 GOT base offset. */
2374 if (info->executable && !info->pie
2375 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
2376 bfd_put_32 (output_bfd,
2377 frvfdpic_got_section (info)->output_section->vma
2378 + frvfdpic_got_section (info)->output_offset
2379 + frvfdpic_got_initial_offset (info),
2380 contents + rel->r_offset + 4);
2381 else
2382 /* A function descriptor used for lazy or local
2383 resolving is initialized such that its high word
2384 contains the output section index in which the
2385 PLT entries are located, and the low word
2386 contains the offset of the lazy PLT entry entry
2387 point into that section. */
2388 bfd_put_32 (output_bfd,
2389 h && ! FRVFDPIC_SYM_LOCAL (info, h)
2391 : _frvfdpic_osec_to_segment (output_bfd,
2393 ->output_section),
2394 contents + rel->r_offset + 4);
2397 check_segment[0] = check_segment[1] = got_segment;
2398 break;
2400 case R_FRV_GPREL12:
2401 case R_FRV_GPRELU12:
2402 case R_FRV_GPREL32:
2403 case R_FRV_GPRELHI:
2404 case R_FRV_GPRELLO:
2405 check_segment[0] = gprel_segment;
2406 check_segment[1] = sec
2407 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
2408 : (unsigned)-1;
2409 break;
2411 default:
2412 check_segment[0] = isec_segment;
2413 check_segment[1] = sec
2414 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
2415 : (unsigned)-1;
2416 break;
2419 if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
2421 #if 1 /* If you take this out, remove the #error from fdpic-static-6.d
2422 in the ld testsuite. */
2423 /* This helps catch problems in GCC while we can't do more
2424 than static linking. The idea is to test whether the
2425 input file basename is crt0.o only once. */
2426 if (silence_segment_error == 1)
2427 silence_segment_error =
2428 (strlen (input_bfd->filename) == 6
2429 && strcmp (input_bfd->filename, "crt0.o") == 0)
2430 || (strlen (input_bfd->filename) > 6
2431 && strcmp (input_bfd->filename
2432 + strlen (input_bfd->filename) - 7,
2433 "/crt0.o") == 0)
2434 ? -1 : 0;
2435 #endif
2436 if (!silence_segment_error
2437 /* We don't want duplicate errors for undefined
2438 symbols. */
2439 && !(picrel && picrel->symndx == -1
2440 && picrel->d.h->root.type == bfd_link_hash_undefined))
2441 info->callbacks->warning
2442 (info,
2443 (info->shared || info->pie)
2444 ? _("relocations between different segments are not supported")
2445 : _("warning: relocation references a different segment"),
2446 name, input_bfd, input_section, rel->r_offset);
2447 if (!silence_segment_error && (info->shared || info->pie))
2448 return FALSE;
2449 elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
2452 switch (r_type)
2454 case R_FRV_GOTOFFHI:
2455 /* We need the addend to be applied before we shift the
2456 value right. */
2457 relocation += rel->r_addend;
2458 /* Fall through. */
2459 case R_FRV_GOTHI:
2460 case R_FRV_FUNCDESC_GOTHI:
2461 case R_FRV_FUNCDESC_GOTOFFHI:
2462 relocation >>= 16;
2463 /* Fall through. */
2465 case R_FRV_GOTLO:
2466 case R_FRV_FUNCDESC_GOTLO:
2467 case R_FRV_GOTOFFLO:
2468 case R_FRV_FUNCDESC_GOTOFFLO:
2469 relocation &= 0xffff;
2470 break;
2472 default:
2473 break;
2476 switch (r_type)
2478 case R_FRV_LABEL24:
2479 if (! IS_FDPIC (output_bfd) || ! picrel->plt)
2480 break;
2481 /* Fall through. */
2483 /* When referencing a GOT entry, a function descriptor or a
2484 PLT, we don't want the addend to apply to the reference,
2485 but rather to the referenced symbol. The actual entry
2486 will have already been created taking the addend into
2487 account, so cancel it out here. */
2488 case R_FRV_GOT12:
2489 case R_FRV_GOTHI:
2490 case R_FRV_GOTLO:
2491 case R_FRV_FUNCDESC_GOT12:
2492 case R_FRV_FUNCDESC_GOTHI:
2493 case R_FRV_FUNCDESC_GOTLO:
2494 case R_FRV_FUNCDESC_GOTOFF12:
2495 case R_FRV_FUNCDESC_GOTOFFHI:
2496 case R_FRV_FUNCDESC_GOTOFFLO:
2497 /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
2498 here, since we do want to apply the addend to the others.
2499 Note that we've applied the addend to GOTOFFHI before we
2500 shifted it right. */
2501 case R_FRV_GOTOFFHI:
2502 relocation -= rel->r_addend;
2503 break;
2505 default:
2506 break;
2509 if (r_type == R_FRV_HI16)
2510 r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
2512 else if (r_type == R_FRV_LO16)
2513 r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
2515 else if (r_type == R_FRV_LABEL24)
2516 r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
2517 contents, relocation);
2519 else if (r_type == R_FRV_GPREL12)
2520 r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
2521 contents, relocation);
2523 else if (r_type == R_FRV_GPRELU12)
2524 r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
2525 contents, relocation);
2527 else if (r_type == R_FRV_GPRELLO)
2528 r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
2529 contents, relocation);
2531 else if (r_type == R_FRV_GPRELHI)
2532 r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
2533 contents, relocation);
2535 else
2536 r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
2537 rel, relocation);
2539 if (r != bfd_reloc_ok)
2541 const char * msg = (const char *) NULL;
2543 switch (r)
2545 case bfd_reloc_overflow:
2546 r = info->callbacks->reloc_overflow
2547 (info, (h ? &h->root : NULL), name, howto->name,
2548 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2549 break;
2551 case bfd_reloc_undefined:
2552 r = info->callbacks->undefined_symbol
2553 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
2554 break;
2556 case bfd_reloc_outofrange:
2557 msg = _("internal error: out of range error");
2558 break;
2560 case bfd_reloc_notsupported:
2561 msg = _("internal error: unsupported relocation error");
2562 break;
2564 case bfd_reloc_dangerous:
2565 msg = _("internal error: dangerous relocation");
2566 break;
2568 default:
2569 msg = _("internal error: unknown error");
2570 break;
2573 if (msg)
2574 r = info->callbacks->warning
2575 (info, msg, name, input_bfd, input_section, rel->r_offset);
2577 if (! r)
2578 return FALSE;
2582 return TRUE;
2585 /* Return the section that should be marked against GC for a given
2586 relocation. */
2588 static asection *
2589 elf32_frv_gc_mark_hook (sec, info, rel, h, sym)
2590 asection *sec;
2591 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2592 Elf_Internal_Rela *rel;
2593 struct elf_link_hash_entry *h;
2594 Elf_Internal_Sym *sym;
2596 if (h != NULL)
2598 switch (ELF32_R_TYPE (rel->r_info))
2600 case R_FRV_GNU_VTINHERIT:
2601 case R_FRV_GNU_VTENTRY:
2602 break;
2604 default:
2605 switch (h->root.type)
2607 default:
2608 break;
2610 case bfd_link_hash_defined:
2611 case bfd_link_hash_defweak:
2612 return h->root.u.def.section;
2614 case bfd_link_hash_common:
2615 return h->root.u.c.p->section;
2619 else
2620 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2622 return NULL;
2625 /* Update the got entry reference counts for the section being removed. */
2627 static bfd_boolean
2628 elf32_frv_gc_sweep_hook (abfd, info, sec, relocs)
2629 bfd *abfd ATTRIBUTE_UNUSED;
2630 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2631 asection *sec ATTRIBUTE_UNUSED;
2632 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
2634 return TRUE;
2638 /* Hook called by the linker routine which adds symbols from an object
2639 file. We use it to put .comm items in .scomm, and not .comm. */
2641 static bfd_boolean
2642 elf32_frv_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2643 bfd *abfd;
2644 struct bfd_link_info *info;
2645 Elf_Internal_Sym *sym;
2646 const char **namep ATTRIBUTE_UNUSED;
2647 flagword *flagsp ATTRIBUTE_UNUSED;
2648 asection **secp;
2649 bfd_vma *valp;
2651 if (sym->st_shndx == SHN_COMMON
2652 && !info->relocatable
2653 && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
2655 /* Common symbols less than or equal to -G nn bytes are
2656 automatically put into .sbss. */
2658 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
2660 if (scomm == NULL)
2662 scomm = bfd_make_section (abfd, ".scommon");
2663 if (scomm == NULL
2664 || !bfd_set_section_flags (abfd, scomm, (SEC_ALLOC
2665 | SEC_IS_COMMON
2666 | SEC_LINKER_CREATED)))
2667 return FALSE;
2670 *secp = scomm;
2671 *valp = sym->st_size;
2674 return TRUE;
2677 /* We need dynamic symbols for every section, since segments can
2678 relocate independently. */
2679 static bfd_boolean
2680 _frvfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2681 struct bfd_link_info *info
2682 ATTRIBUTE_UNUSED,
2683 asection *p ATTRIBUTE_UNUSED)
2685 switch (elf_section_data (p)->this_hdr.sh_type)
2687 case SHT_PROGBITS:
2688 case SHT_NOBITS:
2689 /* If sh_type is yet undecided, assume it could be
2690 SHT_PROGBITS/SHT_NOBITS. */
2691 case SHT_NULL:
2692 return FALSE;
2694 /* There shouldn't be section relative relocations
2695 against any other section. */
2696 default:
2697 return TRUE;
2701 /* Create a .got section, as well as its additional info field. This
2702 is almost entirely copied from
2703 elflink.c:_bfd_elf_create_got_section(). */
2705 static bfd_boolean
2706 _frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
2708 flagword flags;
2709 asection *s;
2710 struct elf_link_hash_entry *h;
2711 struct bfd_link_hash_entry *bh;
2712 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2713 int ptralign;
2714 int offset;
2716 /* This function may be called more than once. */
2717 s = bfd_get_section_by_name (abfd, ".got");
2718 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
2719 return TRUE;
2721 /* Machine specific: although pointers are 32-bits wide, we want the
2722 GOT to be aligned to a 64-bit boundary, such that function
2723 descriptors in it can be accessed with 64-bit loads and
2724 stores. */
2725 ptralign = 3;
2727 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2728 | SEC_LINKER_CREATED);
2730 s = bfd_make_section (abfd, ".got");
2731 if (s == NULL
2732 || !bfd_set_section_flags (abfd, s, flags)
2733 || !bfd_set_section_alignment (abfd, s, ptralign))
2734 return FALSE;
2736 if (bed->want_got_plt)
2738 s = bfd_make_section (abfd, ".got.plt");
2739 if (s == NULL
2740 || !bfd_set_section_flags (abfd, s, flags)
2741 || !bfd_set_section_alignment (abfd, s, ptralign))
2742 return FALSE;
2745 if (bed->want_got_sym)
2747 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
2748 (or .got.plt) section. We don't do this in the linker script
2749 because we don't want to define the symbol if we are not creating
2750 a global offset table. */
2751 bh = NULL;
2752 if (!(_bfd_generic_link_add_one_symbol
2753 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
2754 bed->got_symbol_offset, (const char *) NULL, FALSE,
2755 bed->collect, &bh)))
2756 return FALSE;
2757 h = (struct elf_link_hash_entry *) bh;
2758 h->def_regular = 1;
2759 h->type = STT_OBJECT;
2761 /* Machine-specific: we want the symbol for executables as
2762 well. */
2763 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2764 return FALSE;
2766 elf_hash_table (info)->hgot = h;
2769 /* The first bit of the global offset table is the header. */
2770 s->size += bed->got_header_size + bed->got_symbol_offset;
2772 /* This is the machine-specific part. Create and initialize section
2773 data for the got. */
2774 if (IS_FDPIC (abfd))
2776 frvfdpic_got_section (info) = s;
2777 frvfdpic_relocs_info (info) = htab_try_create (1, frvfdpic_relocs_info_hash,
2778 frvfdpic_relocs_info_eq,
2779 (htab_del) NULL);
2780 if (! frvfdpic_relocs_info (info))
2781 return FALSE;
2783 s = bfd_make_section (abfd, ".rel.got");
2784 if (s == NULL
2785 || ! bfd_set_section_flags (abfd, s, (flags | SEC_READONLY))
2786 || ! bfd_set_section_alignment (abfd, s, 2))
2787 return FALSE;
2789 frvfdpic_gotrel_section (info) = s;
2791 /* Machine-specific. */
2792 s = bfd_make_section (abfd, ".rofixup");
2793 if (s == NULL
2794 || ! bfd_set_section_flags (abfd, s, (flags | SEC_READONLY))
2795 || ! bfd_set_section_alignment (abfd, s, 2))
2796 return FALSE;
2798 frvfdpic_gotfixup_section (info) = s;
2799 offset = -2048;
2800 flags = BSF_GLOBAL;
2802 else
2804 offset = 2048;
2805 flags = BSF_GLOBAL | BSF_WEAK;
2808 /* Define _gp in .rofixup, for FDPIC, or .got otherwise. If it
2809 turns out that we're linking with a different linker script, the
2810 linker script will override it. */
2811 bh = NULL;
2812 if (!(_bfd_generic_link_add_one_symbol
2813 (info, abfd, "_gp", flags, s, offset, (const char *) NULL, FALSE,
2814 bed->collect, &bh)))
2815 return FALSE;
2816 h = (struct elf_link_hash_entry *) bh;
2817 h->def_regular = 1;
2818 h->type = STT_OBJECT;
2820 /* Machine-specific: we want the symbol for executables as well. */
2821 if (IS_FDPIC (abfd) && ! bfd_elf_link_record_dynamic_symbol (info, h))
2822 return FALSE;
2824 return TRUE;
2827 /* Make sure the got and plt sections exist, and that our pointers in
2828 the link hash table point to them. */
2830 static bfd_boolean
2831 elf32_frvfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2833 /* This is mostly copied from
2834 elflink.c:_bfd_elf_create_dynamic_sections(). */
2835 flagword flags, pltflags;
2836 asection *s;
2837 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2839 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2840 .rel[a].bss sections. */
2842 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2843 | SEC_LINKER_CREATED);
2845 pltflags = flags;
2846 pltflags |= SEC_CODE;
2847 if (bed->plt_not_loaded)
2848 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
2849 if (bed->plt_readonly)
2850 pltflags |= SEC_READONLY;
2852 s = bfd_make_section (abfd, ".plt");
2853 if (s == NULL
2854 || ! bfd_set_section_flags (abfd, s, pltflags)
2855 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
2856 return FALSE;
2857 /* FRV-specific: remember it. */
2858 frvfdpic_plt_section (info) = s;
2860 if (bed->want_plt_sym)
2862 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2863 .plt section. */
2864 struct elf_link_hash_entry *h;
2865 struct bfd_link_hash_entry *bh = NULL;
2867 if (! (_bfd_generic_link_add_one_symbol
2868 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
2869 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2870 return FALSE;
2871 h = (struct elf_link_hash_entry *) bh;
2872 h->def_regular = 1;
2873 h->type = STT_OBJECT;
2875 if (! info->executable
2876 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2877 return FALSE;
2880 /* FRV-specific: we want rel relocations for the plt. */
2881 s = bfd_make_section (abfd, ".rel.plt");
2882 if (s == NULL
2883 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2884 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
2885 return FALSE;
2886 /* FRV-specific: remember it. */
2887 frvfdpic_pltrel_section (info) = s;
2889 /* FRV-specific: we want to create the GOT in the FRV way. */
2890 if (! _frv_create_got_section (abfd, info))
2891 return FALSE;
2893 /* FRV-specific: make sure we created everything we wanted. */
2894 BFD_ASSERT (frvfdpic_got_section (info) && frvfdpic_gotrel_section (info)
2895 && frvfdpic_gotfixup_section (info)
2896 && frvfdpic_plt_section (info)
2897 && frvfdpic_pltrel_section (info));
2899 if (bed->want_dynbss)
2901 /* The .dynbss section is a place to put symbols which are defined
2902 by dynamic objects, are referenced by regular objects, and are
2903 not functions. We must allocate space for them in the process
2904 image and use a R_*_COPY reloc to tell the dynamic linker to
2905 initialize them at run time. The linker script puts the .dynbss
2906 section into the .bss section of the final image. */
2907 s = bfd_make_section (abfd, ".dynbss");
2908 if (s == NULL
2909 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
2910 return FALSE;
2912 /* The .rel[a].bss section holds copy relocs. This section is not
2913 normally needed. We need to create it here, though, so that the
2914 linker will map it to an output section. We can't just create it
2915 only if we need it, because we will not know whether we need it
2916 until we have seen all the input files, and the first time the
2917 main linker code calls BFD after examining all the input files
2918 (size_dynamic_sections) the input sections have already been
2919 mapped to the output sections. If the section turns out not to
2920 be needed, we can discard it later. We will never need this
2921 section when generating a shared object, since they do not use
2922 copy relocs. */
2923 if (! info->shared)
2925 s = bfd_make_section (abfd,
2926 (bed->default_use_rela_p
2927 ? ".rela.bss" : ".rel.bss"));
2928 if (s == NULL
2929 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2930 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
2931 return FALSE;
2935 return TRUE;
2938 /* The name of the dynamic interpreter. This is put in the .interp
2939 section. */
2941 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
2943 #define DEFAULT_STACK_SIZE 0x20000
2945 /* This structure is used to collect the number of entries present in
2946 each addressable range of the got. */
2947 struct _frvfdpic_dynamic_got_info
2949 /* Several bits of information about the current link. */
2950 struct bfd_link_info *info;
2951 /* Total size needed for GOT entries within the 12-, 16- or 32-bit
2952 ranges. */
2953 bfd_vma got12, gotlos, gothilo;
2954 /* Total size needed for function descriptor entries within the 12-,
2955 16- or 32-bit ranges. */
2956 bfd_vma fd12, fdlos, fdhilo;
2957 /* Total size needed function descriptor entries referenced in PLT
2958 entries, that would be profitable to place in offsets close to
2959 the PIC register. */
2960 bfd_vma fdplt;
2961 /* Total size needed by lazy PLT entries. */
2962 bfd_vma lzplt;
2963 /* Number of relocations carried over from input object files. */
2964 unsigned long relocs;
2965 /* Number of fixups introduced by relocations in input object files. */
2966 unsigned long fixups;
2969 /* Compute the total GOT size required by each symbol in each range.
2970 Symbols may require up to 4 words in the GOT: an entry pointing to
2971 the symbol, an entry pointing to its function descriptor, and a
2972 private function descriptors taking two words. */
2974 static int
2975 _frvfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
2977 struct frvfdpic_relocs_info *entry = *entryp;
2978 struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
2979 unsigned relocs = 0, fixups = 0;
2981 /* Allocate space for a GOT entry pointing to the symbol. */
2982 if (entry->got12)
2983 dinfo->got12 += 4;
2984 else if (entry->gotlos)
2985 dinfo->gotlos += 4;
2986 else if (entry->gothilo)
2987 dinfo->gothilo += 4;
2988 else
2989 entry->relocs32--;
2990 entry->relocs32++;
2992 /* Allocate space for a GOT entry pointing to the function
2993 descriptor. */
2994 if (entry->fdgot12)
2995 dinfo->got12 += 4;
2996 else if (entry->fdgotlos)
2997 dinfo->gotlos += 4;
2998 else if (entry->fdgothilo)
2999 dinfo->gothilo += 4;
3000 else
3001 entry->relocsfd--;
3002 entry->relocsfd++;
3004 /* Decide whether we need a PLT entry, a function descriptor in the
3005 GOT, and a lazy PLT entry for this symbol. */
3006 entry->plt = entry->call
3007 && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
3008 && elf_hash_table (dinfo->info)->dynamic_sections_created;
3009 entry->privfd = entry->plt
3010 || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
3011 || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
3012 && (entry->symndx != -1
3013 || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
3014 entry->lazyplt = entry->privfd
3015 && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
3016 && ! (dinfo->info->flags & DF_BIND_NOW)
3017 && elf_hash_table (dinfo->info)->dynamic_sections_created;
3019 /* Allocate space for a function descriptor. */
3020 if (entry->fdgoff12)
3021 dinfo->fd12 += 8;
3022 else if (entry->fdgofflos)
3023 dinfo->fdlos += 8;
3024 else if (entry->privfd && entry->plt)
3025 dinfo->fdplt += 8;
3026 else if (entry->privfd)
3027 dinfo->fdhilo += 8;
3028 else
3029 entry->relocsfdv--;
3030 entry->relocsfdv++;
3032 if (entry->lazyplt)
3033 dinfo->lzplt += 8;
3035 if (!dinfo->info->executable || dinfo->info->pie)
3036 relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv;
3037 else
3039 if (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
3041 if (entry->symndx != -1
3042 || entry->d.h->root.type != bfd_link_hash_undefweak)
3043 fixups += entry->relocs32 + 2 * entry->relocsfdv;
3045 else
3046 relocs += entry->relocs32 + entry->relocsfdv;
3048 if (entry->symndx != -1
3049 || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
3051 if (entry->symndx != -1
3052 || entry->d.h->root.type != bfd_link_hash_undefweak)
3053 fixups += entry->relocsfd;
3055 else
3056 relocs += entry->relocsfd;
3059 entry->dynrelocs += relocs;
3060 entry->fixups += fixups;
3061 dinfo->relocs += relocs;
3062 dinfo->fixups += fixups;
3064 return 1;
3067 /* This structure is used to assign offsets to got entries, function
3068 descriptors, plt entries and lazy plt entries. */
3070 struct _frvfdpic_dynamic_got_plt_info
3072 /* Summary information collected with _frvfdpic_count_got_plt_entries. */
3073 struct _frvfdpic_dynamic_got_info g;
3075 /* For each addressable range, we record a MAX (positive) and MIN
3076 (negative) value. CUR is used to assign got entries, and it's
3077 incremented from an initial positive value to MAX, then from MIN
3078 to FDCUR (unless FDCUR wraps around first). FDCUR is used to
3079 assign function descriptors, and it's decreased from an initial
3080 non-positive value to MIN, then from MAX down to CUR (unless CUR
3081 wraps around first). All of MIN, MAX, CUR and FDCUR always point
3082 to even words. ODD, if non-zero, indicates an odd word to be
3083 used for the next got entry, otherwise CUR is used and
3084 incremented by a pair of words, wrapping around when it reaches
3085 MAX. FDCUR is decremented (and wrapped) before the next function
3086 descriptor is chosen. FDPLT indicates the number of remaining
3087 slots that can be used for function descriptors used only by PLT
3088 entries. */
3089 struct _frvfdpic_dynamic_got_alloc_data
3091 bfd_signed_vma max, cur, odd, fdcur, min;
3092 bfd_vma fdplt;
3093 } got12, gotlos, gothilo;
3096 /* Determine the positive and negative ranges to be used by each
3097 offset range in the GOT. FDCUR and CUR, that must be aligned to a
3098 double-word boundary, are the minimum (negative) and maximum
3099 (positive) GOT offsets already used by previous ranges, except for
3100 an ODD entry that may have been left behind. GOT and FD indicate
3101 the size of GOT entries and function descriptors that must be
3102 placed within the range from -WRAP to WRAP. If there's room left,
3103 up to FDPLT bytes should be reserved for additional function
3104 descriptors. */
3106 inline static bfd_signed_vma
3107 _frvfdpic_compute_got_alloc_data (struct _frvfdpic_dynamic_got_alloc_data *gad,
3108 bfd_signed_vma fdcur,
3109 bfd_signed_vma odd,
3110 bfd_signed_vma cur,
3111 bfd_vma got,
3112 bfd_vma fd,
3113 bfd_vma fdplt,
3114 bfd_vma wrap)
3116 bfd_signed_vma wrapmin = -wrap;
3118 /* Start at the given initial points. */
3119 gad->fdcur = fdcur;
3120 gad->cur = cur;
3122 /* If we had an incoming odd word and we have any got entries that
3123 are going to use it, consume it, otherwise leave gad->odd at
3124 zero. We might force gad->odd to zero and return the incoming
3125 odd such that it is used by the next range, but then GOT entries
3126 might appear to be out of order and we wouldn't be able to
3127 shorten the GOT by one word if it turns out to end with an
3128 unpaired GOT entry. */
3129 if (odd && got)
3131 gad->odd = odd;
3132 got -= 4;
3133 odd = 0;
3135 else
3136 gad->odd = 0;
3138 /* If we're left with an unpaired GOT entry, compute its location
3139 such that we can return it. Otherwise, if got doesn't require an
3140 odd number of words here, either odd was already zero in the
3141 block above, or it was set to zero because got was non-zero, or
3142 got was already zero. In the latter case, we want the value of
3143 odd to carry over to the return statement, so we don't want to
3144 reset odd unless the condition below is true. */
3145 if (got & 4)
3147 odd = cur + got;
3148 got += 4;
3151 /* Compute the tentative boundaries of this range. */
3152 gad->max = cur + got;
3153 gad->min = fdcur - fd;
3154 gad->fdplt = 0;
3156 /* If function descriptors took too much space, wrap some of them
3157 around. */
3158 if (gad->min < wrapmin)
3160 gad->max += wrapmin - gad->min;
3161 gad->min = wrapmin;
3163 /* If there is space left and we have function descriptors
3164 referenced in PLT entries that could take advantage of shorter
3165 offsets, place them here. */
3166 else if (fdplt && gad->min > wrapmin)
3168 bfd_vma fds;
3169 if ((bfd_vma) (gad->min - wrapmin) < fdplt)
3170 fds = gad->min - wrapmin;
3171 else
3172 fds = fdplt;
3174 fdplt -= fds;
3175 gad->min -= fds;
3176 gad->fdplt += fds;
3179 /* If GOT entries took too much space, wrap some of them around.
3180 This may well cause gad->min to become lower than wrapmin. This
3181 will cause a relocation overflow later on, so we don't have to
3182 report it here . */
3183 if ((bfd_vma) gad->max > wrap)
3185 gad->min -= gad->max - wrap;
3186 gad->max = wrap;
3188 /* If there is more space left, try to place some more function
3189 descriptors for PLT entries. */
3190 else if (fdplt && (bfd_vma) gad->max < wrap)
3192 bfd_vma fds;
3193 if ((bfd_vma) (wrap - gad->max) < fdplt)
3194 fds = wrap - gad->max;
3195 else
3196 fds = fdplt;
3198 fdplt -= fds;
3199 gad->max += fds;
3200 gad->fdplt += fds;
3203 /* If odd was initially computed as an offset past the wrap point,
3204 wrap it around. */
3205 if (odd > gad->max)
3206 odd = gad->min + odd - gad->max;
3208 /* _frvfdpic_get_got_entry() below will always wrap gad->cur if needed
3209 before returning, so do it here too. This guarantees that,
3210 should cur and fdcur meet at the wrap point, they'll both be
3211 equal to min. */
3212 if (gad->cur == gad->max)
3213 gad->cur = gad->min;
3215 return odd;
3218 /* Compute the location of the next GOT entry, given the allocation
3219 data for a range. */
3221 inline static bfd_signed_vma
3222 _frvfdpic_get_got_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
3224 bfd_signed_vma ret;
3226 if (gad->odd)
3228 /* If there was an odd word left behind, use it. */
3229 ret = gad->odd;
3230 gad->odd = 0;
3232 else
3234 /* Otherwise, use the word pointed to by cur, reserve the next
3235 as an odd word, and skip to the next pair of words, possibly
3236 wrapping around. */
3237 ret = gad->cur;
3238 gad->odd = gad->cur + 4;
3239 gad->cur += 8;
3240 if (gad->cur == gad->max)
3241 gad->cur = gad->min;
3244 return ret;
3247 /* Compute the location of the next function descriptor entry in the
3248 GOT, given the allocation data for a range. */
3250 inline static bfd_signed_vma
3251 _frvfdpic_get_fd_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
3253 /* If we're at the bottom, wrap around, and only then allocate the
3254 next pair of words. */
3255 if (gad->fdcur == gad->min)
3256 gad->fdcur = gad->max;
3257 return gad->fdcur -= 8;
3260 /* Assign GOT offsets for every GOT entry and function descriptor.
3261 Doing everything in a single pass is tricky. */
3263 static int
3264 _frvfdpic_assign_got_entries (void **entryp, void *info_)
3266 struct frvfdpic_relocs_info *entry = *entryp;
3267 struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
3269 if (entry->got12)
3270 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->got12);
3271 else if (entry->gotlos)
3272 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
3273 else if (entry->gothilo)
3274 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
3276 if (entry->fdgot12)
3277 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->got12);
3278 else if (entry->fdgotlos)
3279 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
3280 else if (entry->fdgothilo)
3281 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
3283 if (entry->fdgoff12)
3284 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
3285 else if (entry->plt && dinfo->got12.fdplt)
3287 dinfo->got12.fdplt -= 8;
3288 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
3290 else if (entry->fdgofflos)
3291 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
3292 else if (entry->plt && dinfo->gotlos.fdplt)
3294 dinfo->gotlos.fdplt -= 8;
3295 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
3297 else if (entry->plt)
3299 dinfo->gothilo.fdplt -= 8;
3300 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
3302 else if (entry->privfd)
3303 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
3305 return 1;
3308 /* Assign GOT offsets to private function descriptors used by PLT
3309 entries (or referenced by 32-bit offsets), as well as PLT entries
3310 and lazy PLT entries. */
3312 static int
3313 _frvfdpic_assign_plt_entries (void **entryp, void *info_)
3315 struct frvfdpic_relocs_info *entry = *entryp;
3316 struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
3318 /* If this symbol requires a local function descriptor, allocate
3319 one. */
3320 if (entry->privfd && entry->fd_entry == 0)
3322 if (dinfo->got12.fdplt)
3324 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
3325 dinfo->got12.fdplt -= 8;
3327 else if (dinfo->gotlos.fdplt)
3329 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
3330 dinfo->gotlos.fdplt -= 8;
3332 else
3334 BFD_ASSERT (dinfo->gothilo.fdplt);
3335 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
3336 dinfo->gothilo.fdplt -= 8;
3340 if (entry->plt)
3342 int size;
3344 /* We use the section's raw size to mark the location of the
3345 next PLT entry. */
3346 entry->plt_entry = frvfdpic_plt_section (dinfo->g.info)->size;
3348 /* Figure out the length of this PLT entry based on the
3349 addressing mode we need to reach the function descriptor. */
3350 BFD_ASSERT (entry->fd_entry);
3351 if (entry->fd_entry >= -(1 << (12 - 1))
3352 && entry->fd_entry < (1 << (12 - 1)))
3353 size = 8;
3354 else if (entry->fd_entry >= -(1 << (16 - 1))
3355 && entry->fd_entry < (1 << (16 - 1)))
3356 size = 12;
3357 else
3358 size = 16;
3360 frvfdpic_plt_section (dinfo->g.info)->size += size;
3363 if (entry->lazyplt)
3365 entry->lzplt_entry = dinfo->g.lzplt;
3366 dinfo->g.lzplt += 8;
3367 /* If this entry is the one that gets the resolver stub, account
3368 for the additional instruction. */
3369 if (entry->lzplt_entry % FRVFDPIC_LZPLT_BLOCK_SIZE
3370 == FRVFDPIC_LZPLT_RESOLV_LOC)
3371 dinfo->g.lzplt += 4;
3374 return 1;
3377 /* Follow indirect and warning hash entries so that each got entry
3378 points to the final symbol definition. P must point to a pointer
3379 to the hash table we're traversing. Since this traversal may
3380 modify the hash table, we set this pointer to NULL to indicate
3381 we've made a potentially-destructive change to the hash table, so
3382 the traversal must be restarted. */
3383 static int
3384 _frvfdpic_resolve_final_relocs_info (void **entryp, void *p)
3386 struct frvfdpic_relocs_info *entry = *entryp;
3387 htab_t *htab = p;
3389 if (entry->symndx == -1)
3391 struct elf_link_hash_entry *h = entry->d.h;
3392 struct frvfdpic_relocs_info *oentry;
3394 while (h->root.type == bfd_link_hash_indirect
3395 || h->root.type == bfd_link_hash_warning)
3396 h = (struct elf_link_hash_entry *)h->root.u.i.link;
3398 if (entry->d.h == h)
3399 return 1;
3401 oentry = frvfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
3402 NO_INSERT);
3404 if (oentry)
3406 /* Merge the two entries. */
3407 frvfdpic_pic_merge_early_relocs_info (oentry, entry);
3408 htab_clear_slot (*htab, entryp);
3409 return 1;
3412 entry->d.h = h;
3414 /* If we can't find this entry with the new bfd hash, re-insert
3415 it, and get the traversal restarted. */
3416 if (! htab_find (*htab, entry))
3418 htab_clear_slot (*htab, entryp);
3419 entryp = htab_find_slot (*htab, entry, INSERT);
3420 if (! *entryp)
3421 *entryp = entry;
3422 /* Abort the traversal, since the whole table may have
3423 moved, and leave it up to the parent to restart the
3424 process. */
3425 *(htab_t *)p = NULL;
3426 return 0;
3430 return 1;
3433 /* Set the sizes of the dynamic sections. */
3435 static bfd_boolean
3436 elf32_frvfdpic_size_dynamic_sections (bfd *output_bfd,
3437 struct bfd_link_info *info)
3439 bfd *dynobj;
3440 asection *s;
3441 struct _frvfdpic_dynamic_got_plt_info gpinfo;
3442 bfd_signed_vma odd;
3443 bfd_vma limit;
3445 dynobj = elf_hash_table (info)->dynobj;
3446 BFD_ASSERT (dynobj != NULL);
3448 if (elf_hash_table (info)->dynamic_sections_created)
3450 /* Set the contents of the .interp section to the interpreter. */
3451 if (info->executable)
3453 s = bfd_get_section_by_name (dynobj, ".interp");
3454 BFD_ASSERT (s != NULL);
3455 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3456 s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
3460 memset (&gpinfo, 0, sizeof (gpinfo));
3461 gpinfo.g.info = info;
3463 for (;;)
3465 htab_t relocs = frvfdpic_relocs_info (info);
3467 htab_traverse (relocs, _frvfdpic_resolve_final_relocs_info, &relocs);
3469 if (relocs == frvfdpic_relocs_info (info))
3470 break;
3473 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_count_got_plt_entries,
3474 &gpinfo.g);
3476 odd = 12;
3477 /* Compute the total size taken by entries in the 12-bit and 16-bit
3478 ranges, to tell how many PLT function descriptors we can bring
3479 into the 12-bit range without causing the 16-bit range to
3480 overflow. */
3481 limit = odd + gpinfo.g.got12 + gpinfo.g.gotlos
3482 + gpinfo.g.fd12 + gpinfo.g.fdlos;
3483 if (limit < (bfd_vma)1 << 16)
3484 limit = ((bfd_vma)1 << 16) - limit;
3485 else
3486 limit = 0;
3487 if (gpinfo.g.fdplt < limit)
3488 limit = gpinfo.g.fdplt;
3490 /* Determine the ranges of GOT offsets that we can use for each
3491 range of addressing modes. */
3492 odd = _frvfdpic_compute_got_alloc_data (&gpinfo.got12,
3494 odd,
3496 gpinfo.g.got12,
3497 gpinfo.g.fd12,
3498 limit,
3499 (bfd_vma)1 << (12-1));
3500 odd = _frvfdpic_compute_got_alloc_data (&gpinfo.gotlos,
3501 gpinfo.got12.min,
3502 odd,
3503 gpinfo.got12.max,
3504 gpinfo.g.gotlos,
3505 gpinfo.g.fdlos,
3506 gpinfo.g.fdplt - gpinfo.got12.fdplt,
3507 (bfd_vma)1 << (16-1));
3508 odd = _frvfdpic_compute_got_alloc_data (&gpinfo.gothilo,
3509 gpinfo.gotlos.min,
3510 odd,
3511 gpinfo.gotlos.max,
3512 gpinfo.g.gothilo,
3513 gpinfo.g.fdhilo,
3514 gpinfo.g.fdplt - gpinfo.got12.fdplt
3515 - gpinfo.gotlos.fdplt,
3516 (bfd_vma)1 << (32-1));
3518 /* Now assign (most) GOT offsets. */
3519 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_got_entries,
3520 &gpinfo);
3522 frvfdpic_got_section (info)->size = gpinfo.gothilo.max
3523 - gpinfo.gothilo.min
3524 /* If an odd word is the last word of the GOT, we don't need this
3525 word to be part of the GOT. */
3526 - (odd + 4 == gpinfo.gothilo.max ? 4 : 0);
3527 if (frvfdpic_got_section (info)->size == 0)
3528 frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
3529 else if (frvfdpic_got_section (info)->size == 12
3530 && ! elf_hash_table (info)->dynamic_sections_created)
3532 frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
3533 frvfdpic_got_section (info)->size = 0;
3535 else
3537 frvfdpic_got_section (info)->contents =
3538 (bfd_byte *) bfd_zalloc (dynobj,
3539 frvfdpic_got_section (info)->size);
3540 if (frvfdpic_got_section (info)->contents == NULL)
3541 return FALSE;
3544 if (elf_hash_table (info)->dynamic_sections_created)
3545 /* Subtract the number of lzplt entries, since those will generate
3546 relocations in the pltrel section. */
3547 frvfdpic_gotrel_section (info)->size =
3548 (gpinfo.g.relocs - gpinfo.g.lzplt / 8)
3549 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3550 else
3551 BFD_ASSERT (gpinfo.g.relocs == 0);
3552 if (frvfdpic_gotrel_section (info)->size == 0)
3553 frvfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
3554 else
3556 frvfdpic_gotrel_section (info)->contents =
3557 (bfd_byte *) bfd_zalloc (dynobj,
3558 frvfdpic_gotrel_section (info)->size);
3559 if (frvfdpic_gotrel_section (info)->contents == NULL)
3560 return FALSE;
3563 frvfdpic_gotfixup_section (info)->size = (gpinfo.g.fixups + 1) * 4;
3564 if (frvfdpic_gotfixup_section (info)->size == 0)
3565 frvfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
3566 else
3568 frvfdpic_gotfixup_section (info)->contents =
3569 (bfd_byte *) bfd_zalloc (dynobj,
3570 frvfdpic_gotfixup_section (info)->size);
3571 if (frvfdpic_gotfixup_section (info)->contents == NULL)
3572 return FALSE;
3575 if (elf_hash_table (info)->dynamic_sections_created)
3577 frvfdpic_pltrel_section (info)->size =
3578 gpinfo.g.lzplt / 8 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3579 if (frvfdpic_pltrel_section (info)->size == 0)
3580 frvfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
3581 else
3583 frvfdpic_pltrel_section (info)->contents =
3584 (bfd_byte *) bfd_zalloc (dynobj,
3585 frvfdpic_pltrel_section (info)->size);
3586 if (frvfdpic_pltrel_section (info)->contents == NULL)
3587 return FALSE;
3591 /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
3592 such that there's room for the additional instruction needed to
3593 call the resolver. Since _frvfdpic_assign_got_entries didn't
3594 account for them, our block size is 4 bytes smaller than the real
3595 block size. */
3596 if (elf_hash_table (info)->dynamic_sections_created)
3598 frvfdpic_plt_section (info)->size = gpinfo.g.lzplt
3599 + ((gpinfo.g.lzplt + (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) - 8)
3600 / (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) * 4);
3603 /* Reset it, such that _frvfdpic_assign_plt_entries() can use it to
3604 actually assign lazy PLT entries addresses. */
3605 gpinfo.g.lzplt = 0;
3607 /* Save information that we're going to need to generate GOT and PLT
3608 entries. */
3609 frvfdpic_got_initial_offset (info) = -gpinfo.gothilo.min;
3611 if (get_elf_backend_data (output_bfd)->want_got_sym)
3612 elf_hash_table (info)->hgot->root.u.def.value
3613 += frvfdpic_got_initial_offset (info);
3615 if (elf_hash_table (info)->dynamic_sections_created)
3616 frvfdpic_plt_initial_offset (info) =
3617 frvfdpic_plt_section (info)->size;
3619 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_plt_entries,
3620 &gpinfo);
3622 /* Allocate the PLT section contents only after
3623 _frvfdpic_assign_plt_entries has a chance to add the size of the
3624 non-lazy PLT entries. */
3625 if (elf_hash_table (info)->dynamic_sections_created)
3627 if (frvfdpic_plt_section (info)->size == 0)
3628 frvfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
3629 else
3631 frvfdpic_plt_section (info)->contents =
3632 (bfd_byte *) bfd_zalloc (dynobj,
3633 frvfdpic_plt_section (info)->size);
3634 if (frvfdpic_plt_section (info)->contents == NULL)
3635 return FALSE;
3639 if (elf_hash_table (info)->dynamic_sections_created)
3641 if (frvfdpic_got_section (info)->size)
3642 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0))
3643 return FALSE;
3645 if (frvfdpic_pltrel_section (info)->size)
3646 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
3647 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_REL)
3648 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
3649 return FALSE;
3651 if (frvfdpic_gotrel_section (info)->size)
3652 if (!_bfd_elf_add_dynamic_entry (info, DT_REL, 0)
3653 || !_bfd_elf_add_dynamic_entry (info, DT_RELSZ, 0)
3654 || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
3655 sizeof (Elf32_External_Rel)))
3656 return FALSE;
3659 return TRUE;
3662 static bfd_boolean
3663 elf32_frvfdpic_always_size_sections (bfd *output_bfd,
3664 struct bfd_link_info *info)
3666 if (!info->relocatable)
3668 struct elf_link_hash_entry *h;
3669 asection *sec;
3671 /* Force a PT_GNU_STACK segment to be created. */
3672 if (! elf_tdata (output_bfd)->stack_flags)
3673 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
3675 /* Define __stacksize if it's not defined yet. */
3676 h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
3677 FALSE, FALSE, FALSE);
3678 if (! h || h->root.type != bfd_link_hash_defined
3679 || h->type != STT_OBJECT
3680 || !h->def_regular)
3682 struct bfd_link_hash_entry *bh = NULL;
3684 if (!(_bfd_generic_link_add_one_symbol
3685 (info, output_bfd, "__stacksize",
3686 BSF_GLOBAL, bfd_abs_section_ptr, DEFAULT_STACK_SIZE,
3687 (const char *) NULL, FALSE,
3688 get_elf_backend_data (output_bfd)->collect, &bh)))
3689 return FALSE;
3691 h = (struct elf_link_hash_entry *) bh;
3692 h->def_regular = 1;
3693 h->type = STT_OBJECT;
3696 /* Create a stack section, and set its alignment. */
3697 sec = bfd_make_section (output_bfd, ".stack");
3699 if (sec == NULL
3700 || ! bfd_set_section_alignment (output_bfd, sec, 3))
3701 return FALSE;
3704 return TRUE;
3707 static bfd_boolean
3708 elf32_frvfdpic_modify_segment_map (bfd *output_bfd,
3709 struct bfd_link_info *info)
3711 struct elf_segment_map *m;
3713 /* objcopy and strip preserve what's already there using
3714 elf32_frvfdpic_copy_private_bfd_data (). */
3715 if (! info)
3716 return TRUE;
3718 for (m = elf_tdata (output_bfd)->segment_map; m != NULL; m = m->next)
3719 if (m->p_type == PT_GNU_STACK)
3720 break;
3722 if (m)
3724 asection *sec = bfd_get_section_by_name (output_bfd, ".stack");
3725 struct elf_link_hash_entry *h;
3727 if (sec)
3729 /* Obtain the pointer to the __stacksize symbol. */
3730 h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
3731 FALSE, FALSE, FALSE);
3732 while (h->root.type == bfd_link_hash_indirect
3733 || h->root.type == bfd_link_hash_warning)
3734 h = (struct elf_link_hash_entry *)h->root.u.i.link;
3735 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
3737 /* Set the section size from the symbol value. We
3738 intentionally ignore the symbol section. */
3739 if (h->root.type == bfd_link_hash_defined)
3740 sec->size = h->root.u.def.value;
3741 else
3742 sec->size = DEFAULT_STACK_SIZE;
3744 /* Add the stack section to the PT_GNU_STACK segment,
3745 such that its size and alignment requirements make it
3746 to the segment. */
3747 m->sections[m->count] = sec;
3748 m->count++;
3752 return TRUE;
3755 /* Fill in code and data in dynamic sections. */
3757 static bfd_boolean
3758 elf32_frv_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3759 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3761 /* Nothing to be done for non-FDPIC. */
3762 return TRUE;
3765 static bfd_boolean
3766 elf32_frvfdpic_finish_dynamic_sections (bfd *output_bfd,
3767 struct bfd_link_info *info)
3769 bfd *dynobj;
3770 asection *sdyn;
3772 dynobj = elf_hash_table (info)->dynobj;
3774 if (frvfdpic_got_section (info))
3776 BFD_ASSERT (frvfdpic_gotrel_section (info)->size
3777 == (frvfdpic_gotrel_section (info)->reloc_count
3778 * sizeof (Elf32_External_Rel)));
3780 if (frvfdpic_gotfixup_section (info))
3782 struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
3783 bfd_vma got_value = hgot->root.u.def.value
3784 + hgot->root.u.def.section->output_section->vma
3785 + hgot->root.u.def.section->output_offset;
3787 _frvfdpic_add_rofixup (output_bfd, frvfdpic_gotfixup_section (info),
3788 got_value, 0);
3790 if (frvfdpic_gotfixup_section (info)->size
3791 != (frvfdpic_gotfixup_section (info)->reloc_count * 4))
3793 (*_bfd_error_handler)
3794 ("LINKER BUG: .rofixup section size mismatch");
3795 return FALSE;
3799 if (elf_hash_table (info)->dynamic_sections_created)
3801 BFD_ASSERT (frvfdpic_pltrel_section (info)->size
3802 == (frvfdpic_pltrel_section (info)->reloc_count
3803 * sizeof (Elf32_External_Rel)));
3806 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3808 if (elf_hash_table (info)->dynamic_sections_created)
3810 Elf32_External_Dyn * dyncon;
3811 Elf32_External_Dyn * dynconend;
3813 BFD_ASSERT (sdyn != NULL);
3815 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3816 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3818 for (; dyncon < dynconend; dyncon++)
3820 Elf_Internal_Dyn dyn;
3822 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3824 switch (dyn.d_tag)
3826 default:
3827 break;
3829 case DT_PLTGOT:
3830 dyn.d_un.d_ptr = frvfdpic_got_section (info)->output_section->vma
3831 + frvfdpic_got_section (info)->output_offset
3832 + frvfdpic_got_initial_offset (info);
3833 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3834 break;
3836 case DT_JMPREL:
3837 dyn.d_un.d_ptr = frvfdpic_pltrel_section (info)
3838 ->output_section->vma
3839 + frvfdpic_pltrel_section (info)->output_offset;
3840 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3841 break;
3843 case DT_PLTRELSZ:
3844 dyn.d_un.d_val = frvfdpic_pltrel_section (info)->size;
3845 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3846 break;
3851 return TRUE;
3854 /* Adjust a symbol defined by a dynamic object and referenced by a
3855 regular object. */
3857 static bfd_boolean
3858 elf32_frvfdpic_adjust_dynamic_symbol
3859 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3860 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
3862 bfd * dynobj;
3864 dynobj = elf_hash_table (info)->dynobj;
3866 /* Make sure we know what is going on here. */
3867 BFD_ASSERT (dynobj != NULL
3868 && (h->u.weakdef != NULL
3869 || (h->def_dynamic
3870 && h->ref_regular
3871 && !h->def_regular)));
3873 /* If this is a weak symbol, and there is a real definition, the
3874 processor independent code will have arranged for us to see the
3875 real definition first, and we can just use the same value. */
3876 if (h->u.weakdef != NULL)
3878 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3879 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3880 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3881 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3884 return TRUE;
3887 /* Perform any actions needed for dynamic symbols. */
3889 static bfd_boolean
3890 elf32_frvfdpic_finish_dynamic_symbol
3891 (bfd *output_bfd ATTRIBUTE_UNUSED,
3892 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3893 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
3894 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
3896 return TRUE;
3899 /* Decide whether to attempt to turn absptr or lsda encodings in
3900 shared libraries into pcrel within the given input section. */
3902 static bfd_boolean
3903 frvfdpic_elf_use_relative_eh_frame
3904 (bfd *input_bfd ATTRIBUTE_UNUSED,
3905 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3906 asection *eh_frame_section ATTRIBUTE_UNUSED)
3908 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
3909 return FALSE;
3912 /* Adjust the contents of an eh_frame_hdr section before they're output. */
3914 static bfd_byte
3915 frvfdpic_elf_encode_eh_address (bfd *abfd,
3916 struct bfd_link_info *info,
3917 asection *osec, bfd_vma offset,
3918 asection *loc_sec, bfd_vma loc_offset,
3919 bfd_vma *encoded)
3921 struct elf_link_hash_entry *h;
3923 h = elf_hash_table (info)->hgot;
3924 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
3926 if (! h || (_frvfdpic_osec_to_segment (abfd, osec)
3927 == _frvfdpic_osec_to_segment (abfd, loc_sec->output_section)))
3928 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
3929 loc_sec, loc_offset, encoded);
3931 BFD_ASSERT (_frvfdpic_osec_to_segment (abfd, osec)
3932 == (_frvfdpic_osec_to_segment
3933 (abfd, h->root.u.def.section->output_section)));
3935 *encoded = osec->vma + offset
3936 - (h->root.u.def.value
3937 + h->root.u.def.section->output_section->vma
3938 + h->root.u.def.section->output_offset);
3940 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
3943 /* Look through the relocs for a section during the first phase.
3945 Besides handling virtual table relocs for gc, we have to deal with
3946 all sorts of PIC-related relocations. We describe below the
3947 general plan on how to handle such relocations, even though we only
3948 collect information at this point, storing them in hash tables for
3949 perusal of later passes.
3951 32 relocations are propagated to the linker output when creating
3952 position-independent output. LO16 and HI16 relocations are not
3953 supposed to be encountered in this case.
3955 LABEL16 should always be resolvable by the linker, since it's only
3956 used by branches.
3958 LABEL24, on the other hand, is used by calls. If it turns out that
3959 the target of a call is a dynamic symbol, a PLT entry must be
3960 created for it, which triggers the creation of a private function
3961 descriptor and, unless lazy binding is disabled, a lazy PLT entry.
3963 GPREL relocations require the referenced symbol to be in the same
3964 segment as _gp, but this can only be checked later.
3966 All GOT, GOTOFF and FUNCDESC relocations require a .got section to
3967 exist. LABEL24 might as well, since it may require a PLT entry,
3968 that will require a got.
3970 Non-FUNCDESC GOT relocations require a GOT entry to be created
3971 regardless of whether the symbol is dynamic. However, since a
3972 global symbol that turns out to not be exported may have the same
3973 address of a non-dynamic symbol, we don't assign GOT entries at
3974 this point, such that we can share them in this case. A relocation
3975 for the GOT entry always has to be created, be it to offset a
3976 private symbol by the section load address, be it to get the symbol
3977 resolved dynamically.
3979 FUNCDESC GOT relocations require a GOT entry to be created, and
3980 handled as if a FUNCDESC relocation was applied to the GOT entry in
3981 an object file.
3983 FUNCDESC relocations referencing a symbol that turns out to NOT be
3984 dynamic cause a private function descriptor to be created. The
3985 FUNCDESC relocation then decays to a 32 relocation that points at
3986 the private descriptor. If the symbol is dynamic, the FUNCDESC
3987 relocation is propagated to the linker output, such that the
3988 dynamic linker creates the canonical descriptor, pointing to the
3989 dynamically-resolved definition of the function.
3991 Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
3992 symbols that are assigned to the same segment as the GOT, but we
3993 can only check this later, after we know the complete set of
3994 symbols defined and/or exported.
3996 FUNCDESC GOTOFF relocations require a function descriptor to be
3997 created and, unless lazy binding is disabled or the symbol is not
3998 dynamic, a lazy PLT entry. Since we can't tell at this point
3999 whether a symbol is going to be dynamic, we have to decide later
4000 whether to create a lazy PLT entry or bind the descriptor directly
4001 to the private function.
4003 FUNCDESC_VALUE relocations are not supposed to be present in object
4004 files, but they may very well be simply propagated to the linker
4005 output, since they have no side effect.
4008 A function descriptor always requires a FUNCDESC_VALUE relocation.
4009 Whether it's in .plt.rel or not depends on whether lazy binding is
4010 enabled and on whether the referenced symbol is dynamic.
4012 The existence of a lazy PLT requires the resolverStub lazy PLT
4013 entry to be present.
4016 As for assignment of GOT, PLT and lazy PLT entries, and private
4017 descriptors, we might do them all sequentially, but we can do
4018 better than that. For example, we can place GOT entries and
4019 private function descriptors referenced using 12-bit operands
4020 closer to the PIC register value, such that these relocations don't
4021 overflow. Those that are only referenced with LO16 relocations
4022 could come next, but we may as well place PLT-required function
4023 descriptors in the 12-bit range to make them shorter. Symbols
4024 referenced with LO16/HI16 may come next, but we may place
4025 additional function descriptors in the 16-bit range if we can
4026 reliably tell that we've already placed entries that are ever
4027 referenced with only LO16. PLT entries are therefore generated as
4028 small as possible, while not introducing relocation overflows in
4029 GOT or FUNCDESC_GOTOFF relocations. Lazy PLT entries could be
4030 generated before or after PLT entries, but not intermingled with
4031 them, such that we can have more lazy PLT entries in range for a
4032 branch to the resolverStub. The resolverStub should be emitted at
4033 the most distant location from the first lazy PLT entry such that
4034 it's still in range for a branch, or closer, if there isn't a need
4035 for so many lazy PLT entries. Additional lazy PLT entries may be
4036 emitted after the resolverStub, as long as branches are still in
4037 range. If the branch goes out of range, longer lazy PLT entries
4038 are emitted.
4040 We could further optimize PLT and lazy PLT entries by giving them
4041 priority in assignment to closer-to-gr17 locations depending on the
4042 number of occurrences of references to them (assuming a function
4043 that's called more often is more important for performance, so its
4044 PLT entry should be faster), or taking hints from the compiler.
4045 Given infinite time and money... :-) */
4047 static bfd_boolean
4048 elf32_frv_check_relocs (abfd, info, sec, relocs)
4049 bfd *abfd;
4050 struct bfd_link_info *info;
4051 asection *sec;
4052 const Elf_Internal_Rela *relocs;
4054 Elf_Internal_Shdr *symtab_hdr;
4055 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4056 const Elf_Internal_Rela *rel;
4057 const Elf_Internal_Rela *rel_end;
4058 bfd *dynobj;
4059 struct frvfdpic_relocs_info *picrel;
4061 if (info->relocatable)
4062 return TRUE;
4064 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4065 sym_hashes = elf_sym_hashes (abfd);
4066 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
4067 if (!elf_bad_symtab (abfd))
4068 sym_hashes_end -= symtab_hdr->sh_info;
4070 dynobj = elf_hash_table (info)->dynobj;
4071 rel_end = relocs + sec->reloc_count;
4072 for (rel = relocs; rel < rel_end; rel++)
4074 struct elf_link_hash_entry *h;
4075 unsigned long r_symndx;
4077 r_symndx = ELF32_R_SYM (rel->r_info);
4078 if (r_symndx < symtab_hdr->sh_info)
4079 h = NULL;
4080 else
4081 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4083 switch (ELF32_R_TYPE (rel->r_info))
4085 case R_FRV_GOT12:
4086 case R_FRV_GOTHI:
4087 case R_FRV_GOTLO:
4088 case R_FRV_FUNCDESC_GOT12:
4089 case R_FRV_FUNCDESC_GOTHI:
4090 case R_FRV_FUNCDESC_GOTLO:
4091 case R_FRV_GOTOFF12:
4092 case R_FRV_GOTOFFHI:
4093 case R_FRV_GOTOFFLO:
4094 case R_FRV_FUNCDESC_GOTOFF12:
4095 case R_FRV_FUNCDESC_GOTOFFHI:
4096 case R_FRV_FUNCDESC_GOTOFFLO:
4097 case R_FRV_FUNCDESC:
4098 case R_FRV_FUNCDESC_VALUE:
4099 if (! IS_FDPIC (abfd))
4100 goto bad_reloc;
4101 /* Fall through. */
4102 case R_FRV_GPREL12:
4103 case R_FRV_GPRELU12:
4104 case R_FRV_GPRELHI:
4105 case R_FRV_GPRELLO:
4106 case R_FRV_LABEL24:
4107 case R_FRV_32:
4108 if (! dynobj)
4110 elf_hash_table (info)->dynobj = dynobj = abfd;
4111 if (! _frv_create_got_section (abfd, info))
4112 return FALSE;
4114 if (! IS_FDPIC (abfd))
4116 picrel = NULL;
4117 break;
4119 if (h != NULL)
4121 if (h->dynindx == -1)
4122 switch (ELF_ST_VISIBILITY (h->other))
4124 case STV_INTERNAL:
4125 case STV_HIDDEN:
4126 break;
4127 default:
4128 bfd_elf_link_record_dynamic_symbol (info, h);
4129 break;
4131 picrel
4132 = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
4133 abfd, h,
4134 rel->r_addend, INSERT);
4136 else
4137 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
4138 (info), abfd, r_symndx,
4139 rel->r_addend, INSERT);
4140 if (! picrel)
4141 return FALSE;
4142 break;
4144 default:
4145 picrel = NULL;
4146 break;
4149 switch (ELF32_R_TYPE (rel->r_info))
4151 case R_FRV_LABEL24:
4152 if (IS_FDPIC (abfd))
4153 picrel->call = 1;
4154 break;
4156 case R_FRV_FUNCDESC_VALUE:
4157 picrel->relocsfdv++;
4158 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
4159 picrel->relocs32--;
4160 /* Fall through. */
4162 case R_FRV_32:
4163 if (! IS_FDPIC (abfd))
4164 break;
4166 picrel->sym = 1;
4167 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
4168 picrel->relocs32++;
4169 break;
4171 case R_FRV_GOT12:
4172 picrel->got12 = 1;
4173 break;
4175 case R_FRV_GOTHI:
4176 case R_FRV_GOTLO:
4177 picrel->gothilo = 1;
4178 break;
4180 case R_FRV_FUNCDESC_GOT12:
4181 picrel->fdgot12 = 1;
4182 break;
4184 case R_FRV_FUNCDESC_GOTHI:
4185 case R_FRV_FUNCDESC_GOTLO:
4186 picrel->fdgothilo = 1;
4187 break;
4189 case R_FRV_GOTOFF12:
4190 case R_FRV_GOTOFFHI:
4191 case R_FRV_GOTOFFLO:
4192 picrel->gotoff = 1;
4193 break;
4195 case R_FRV_FUNCDESC_GOTOFF12:
4196 picrel->fdgoff12 = 1;
4197 break;
4199 case R_FRV_FUNCDESC_GOTOFFHI:
4200 case R_FRV_FUNCDESC_GOTOFFLO:
4201 picrel->fdgoffhilo = 1;
4202 break;
4204 case R_FRV_FUNCDESC:
4205 picrel->fd = 1;
4206 picrel->relocsfd++;
4207 break;
4209 /* This relocation describes the C++ object vtable hierarchy.
4210 Reconstruct it for later use during GC. */
4211 case R_FRV_GNU_VTINHERIT:
4212 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4213 return FALSE;
4214 break;
4216 /* This relocation describes which C++ vtable entries are actually
4217 used. Record for later use during GC. */
4218 case R_FRV_GNU_VTENTRY:
4219 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4220 return FALSE;
4221 break;
4223 case R_FRV_LABEL16:
4224 case R_FRV_LO16:
4225 case R_FRV_HI16:
4226 case R_FRV_GPREL12:
4227 case R_FRV_GPRELU12:
4228 case R_FRV_GPREL32:
4229 case R_FRV_GPRELHI:
4230 case R_FRV_GPRELLO:
4231 break;
4233 default:
4234 bad_reloc:
4235 (*_bfd_error_handler)
4236 (_("%B: unsupported relocation type %i"),
4237 abfd, ELF32_R_TYPE (rel->r_info));
4238 return FALSE;
4242 return TRUE;
4246 /* Return the machine subcode from the ELF e_flags header. */
4248 static int
4249 elf32_frv_machine (abfd)
4250 bfd *abfd;
4252 switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
4254 default: break;
4255 case EF_FRV_CPU_FR550: return bfd_mach_fr550;
4256 case EF_FRV_CPU_FR500: return bfd_mach_fr500;
4257 case EF_FRV_CPU_FR450: return bfd_mach_fr450;
4258 case EF_FRV_CPU_FR405: return bfd_mach_fr400;
4259 case EF_FRV_CPU_FR400: return bfd_mach_fr400;
4260 case EF_FRV_CPU_FR300: return bfd_mach_fr300;
4261 case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
4262 case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
4265 return bfd_mach_frv;
4268 /* Set the right machine number for a FRV ELF file. */
4270 static bfd_boolean
4271 elf32_frv_object_p (abfd)
4272 bfd *abfd;
4274 bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
4275 return (((elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC) != 0)
4276 == (IS_FDPIC (abfd)));
4279 /* Function to set the ELF flag bits. */
4281 static bfd_boolean
4282 frv_elf_set_private_flags (abfd, flags)
4283 bfd *abfd;
4284 flagword flags;
4286 elf_elfheader (abfd)->e_flags = flags;
4287 elf_flags_init (abfd) = TRUE;
4288 return TRUE;
4291 /* Copy backend specific data from one object module to another. */
4293 static bfd_boolean
4294 frv_elf_copy_private_bfd_data (ibfd, obfd)
4295 bfd *ibfd;
4296 bfd *obfd;
4298 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4299 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4300 return TRUE;
4302 BFD_ASSERT (!elf_flags_init (obfd)
4303 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
4305 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
4306 elf_flags_init (obfd) = TRUE;
4307 return TRUE;
4310 /* Return true if the architecture described by elf header flag
4311 EXTENSION is an extension of the architecture described by BASE. */
4313 static bfd_boolean
4314 frv_elf_arch_extension_p (flagword base, flagword extension)
4316 if (base == extension)
4317 return TRUE;
4319 /* CPU_GENERIC code can be merged with code for a specific
4320 architecture, in which case the result is marked as being
4321 for the specific architecture. Everything is therefore
4322 an extension of CPU_GENERIC. */
4323 if (base == EF_FRV_CPU_GENERIC)
4324 return TRUE;
4326 if (extension == EF_FRV_CPU_FR450)
4327 if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
4328 return TRUE;
4330 if (extension == EF_FRV_CPU_FR405)
4331 if (base == EF_FRV_CPU_FR400)
4332 return TRUE;
4334 return FALSE;
4337 static bfd_boolean
4338 elf32_frvfdpic_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4340 unsigned i;
4342 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4343 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4344 return TRUE;
4346 if (! frv_elf_copy_private_bfd_data (ibfd, obfd))
4347 return FALSE;
4349 if (! elf_tdata (ibfd) || ! elf_tdata (ibfd)->phdr
4350 || ! elf_tdata (obfd) || ! elf_tdata (obfd)->phdr)
4351 return TRUE;
4353 /* Copy the stack size. */
4354 for (i = 0; i < elf_elfheader (ibfd)->e_phnum; i++)
4355 if (elf_tdata (ibfd)->phdr[i].p_type == PT_GNU_STACK)
4357 Elf_Internal_Phdr *iphdr = &elf_tdata (ibfd)->phdr[i];
4359 for (i = 0; i < elf_elfheader (obfd)->e_phnum; i++)
4360 if (elf_tdata (obfd)->phdr[i].p_type == PT_GNU_STACK)
4362 memcpy (&elf_tdata (obfd)->phdr[i], iphdr, sizeof (*iphdr));
4364 /* Rewrite the phdrs, since we're only called after they
4365 were first written. */
4366 if (bfd_seek (obfd, (bfd_signed_vma) get_elf_backend_data (obfd)
4367 ->s->sizeof_ehdr, SEEK_SET) != 0
4368 || get_elf_backend_data (obfd)->s
4369 ->write_out_phdrs (obfd, elf_tdata (obfd)->phdr,
4370 elf_elfheader (obfd)->e_phnum) != 0)
4371 return FALSE;
4372 break;
4375 break;
4378 return TRUE;
4381 /* Merge backend specific data from an object file to the output
4382 object file when linking. */
4384 static bfd_boolean
4385 frv_elf_merge_private_bfd_data (ibfd, obfd)
4386 bfd *ibfd;
4387 bfd *obfd;
4389 flagword old_flags, old_partial;
4390 flagword new_flags, new_partial;
4391 bfd_boolean error = FALSE;
4392 char new_opt[80];
4393 char old_opt[80];
4395 new_opt[0] = old_opt[0] = '\0';
4396 new_flags = elf_elfheader (ibfd)->e_flags;
4397 old_flags = elf_elfheader (obfd)->e_flags;
4399 if (new_flags & EF_FRV_FDPIC)
4400 new_flags &= ~EF_FRV_PIC;
4402 #ifdef DEBUG
4403 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
4404 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
4405 bfd_get_filename (ibfd));
4406 #endif
4408 if (!elf_flags_init (obfd)) /* First call, no flags set. */
4410 elf_flags_init (obfd) = TRUE;
4411 old_flags = new_flags;
4414 else if (new_flags == old_flags) /* Compatible flags are ok. */
4417 else /* Possibly incompatible flags. */
4419 /* Warn if different # of gprs are used. Note, 0 means nothing is
4420 said about the size of gprs. */
4421 new_partial = (new_flags & EF_FRV_GPR_MASK);
4422 old_partial = (old_flags & EF_FRV_GPR_MASK);
4423 if (new_partial == old_partial)
4426 else if (new_partial == 0)
4429 else if (old_partial == 0)
4430 old_flags |= new_partial;
4432 else
4434 switch (new_partial)
4436 default: strcat (new_opt, " -mgpr-??"); break;
4437 case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
4438 case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
4441 switch (old_partial)
4443 default: strcat (old_opt, " -mgpr-??"); break;
4444 case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
4445 case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
4449 /* Warn if different # of fprs are used. Note, 0 means nothing is
4450 said about the size of fprs. */
4451 new_partial = (new_flags & EF_FRV_FPR_MASK);
4452 old_partial = (old_flags & EF_FRV_FPR_MASK);
4453 if (new_partial == old_partial)
4456 else if (new_partial == 0)
4459 else if (old_partial == 0)
4460 old_flags |= new_partial;
4462 else
4464 switch (new_partial)
4466 default: strcat (new_opt, " -mfpr-?"); break;
4467 case EF_FRV_FPR_32: strcat (new_opt, " -mfpr-32"); break;
4468 case EF_FRV_FPR_64: strcat (new_opt, " -mfpr-64"); break;
4469 case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
4472 switch (old_partial)
4474 default: strcat (old_opt, " -mfpr-?"); break;
4475 case EF_FRV_FPR_32: strcat (old_opt, " -mfpr-32"); break;
4476 case EF_FRV_FPR_64: strcat (old_opt, " -mfpr-64"); break;
4477 case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
4481 /* Warn if different dword support was used. Note, 0 means nothing is
4482 said about the dword support. */
4483 new_partial = (new_flags & EF_FRV_DWORD_MASK);
4484 old_partial = (old_flags & EF_FRV_DWORD_MASK);
4485 if (new_partial == old_partial)
4488 else if (new_partial == 0)
4491 else if (old_partial == 0)
4492 old_flags |= new_partial;
4494 else
4496 switch (new_partial)
4498 default: strcat (new_opt, " -mdword-?"); break;
4499 case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword"); break;
4500 case EF_FRV_DWORD_NO: strcat (new_opt, " -mno-dword"); break;
4503 switch (old_partial)
4505 default: strcat (old_opt, " -mdword-?"); break;
4506 case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword"); break;
4507 case EF_FRV_DWORD_NO: strcat (old_opt, " -mno-dword"); break;
4511 /* Or in flags that accumulate (ie, if one module uses it, mark that the
4512 feature is used. */
4513 old_flags |= new_flags & (EF_FRV_DOUBLE
4514 | EF_FRV_MEDIA
4515 | EF_FRV_MULADD
4516 | EF_FRV_NON_PIC_RELOCS);
4518 /* If any module was compiled without -G0, clear the G0 bit. */
4519 old_flags = ((old_flags & ~ EF_FRV_G0)
4520 | (old_flags & new_flags & EF_FRV_G0));
4522 /* If any module was compiled without -mnopack, clear the mnopack bit. */
4523 old_flags = ((old_flags & ~ EF_FRV_NOPACK)
4524 | (old_flags & new_flags & EF_FRV_NOPACK));
4526 /* We don't have to do anything if the pic flags are the same, or the new
4527 module(s) were compiled with -mlibrary-pic. */
4528 new_partial = (new_flags & EF_FRV_PIC_FLAGS);
4529 old_partial = (old_flags & EF_FRV_PIC_FLAGS);
4530 if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
4533 /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
4534 flags if any from the new module. */
4535 else if ((old_partial & EF_FRV_LIBPIC) != 0)
4536 old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
4538 /* If we have mixtures of -fpic and -fPIC, or in both bits. */
4539 else if (new_partial != 0 && old_partial != 0)
4540 old_flags |= new_partial;
4542 /* One module was compiled for pic and the other was not, see if we have
4543 had any relocations that are not pic-safe. */
4544 else
4546 if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
4547 old_flags |= new_partial;
4548 else
4550 old_flags &= ~ EF_FRV_PIC_FLAGS;
4551 #ifndef FRV_NO_PIC_ERROR
4552 error = TRUE;
4553 (*_bfd_error_handler)
4554 (_("%s: compiled with %s and linked with modules that use non-pic relocations"),
4555 bfd_get_filename (ibfd),
4556 (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
4557 #endif
4561 /* Warn if different cpu is used (allow a specific cpu to override
4562 the generic cpu). */
4563 new_partial = (new_flags & EF_FRV_CPU_MASK);
4564 old_partial = (old_flags & EF_FRV_CPU_MASK);
4565 if (frv_elf_arch_extension_p (new_partial, old_partial))
4568 else if (frv_elf_arch_extension_p (old_partial, new_partial))
4569 old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
4571 else
4573 switch (new_partial)
4575 default: strcat (new_opt, " -mcpu=?"); break;
4576 case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv"); break;
4577 case EF_FRV_CPU_SIMPLE: strcat (new_opt, " -mcpu=simple"); break;
4578 case EF_FRV_CPU_FR550: strcat (new_opt, " -mcpu=fr550"); break;
4579 case EF_FRV_CPU_FR500: strcat (new_opt, " -mcpu=fr500"); break;
4580 case EF_FRV_CPU_FR450: strcat (new_opt, " -mcpu=fr450"); break;
4581 case EF_FRV_CPU_FR405: strcat (new_opt, " -mcpu=fr405"); break;
4582 case EF_FRV_CPU_FR400: strcat (new_opt, " -mcpu=fr400"); break;
4583 case EF_FRV_CPU_FR300: strcat (new_opt, " -mcpu=fr300"); break;
4584 case EF_FRV_CPU_TOMCAT: strcat (new_opt, " -mcpu=tomcat"); break;
4587 switch (old_partial)
4589 default: strcat (old_opt, " -mcpu=?"); break;
4590 case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv"); break;
4591 case EF_FRV_CPU_SIMPLE: strcat (old_opt, " -mcpu=simple"); break;
4592 case EF_FRV_CPU_FR550: strcat (old_opt, " -mcpu=fr550"); break;
4593 case EF_FRV_CPU_FR500: strcat (old_opt, " -mcpu=fr500"); break;
4594 case EF_FRV_CPU_FR450: strcat (old_opt, " -mcpu=fr450"); break;
4595 case EF_FRV_CPU_FR405: strcat (old_opt, " -mcpu=fr405"); break;
4596 case EF_FRV_CPU_FR400: strcat (old_opt, " -mcpu=fr400"); break;
4597 case EF_FRV_CPU_FR300: strcat (old_opt, " -mcpu=fr300"); break;
4598 case EF_FRV_CPU_TOMCAT: strcat (old_opt, " -mcpu=tomcat"); break;
4602 /* Print out any mismatches from above. */
4603 if (new_opt[0])
4605 error = TRUE;
4606 (*_bfd_error_handler)
4607 (_("%s: compiled with %s and linked with modules compiled with %s"),
4608 bfd_get_filename (ibfd), new_opt, old_opt);
4611 /* Warn about any other mismatches */
4612 new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
4613 old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
4614 if (new_partial != old_partial)
4616 old_flags |= new_partial;
4617 error = TRUE;
4618 (*_bfd_error_handler)
4619 (_("%s: uses different unknown e_flags (0x%lx) fields than previous modules (0x%lx)"),
4620 bfd_get_filename (ibfd), (long)new_partial, (long)old_partial);
4624 /* If the cpu is -mcpu=simple, then set the -mnopack bit. */
4625 if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
4626 old_flags |= EF_FRV_NOPACK;
4628 /* Update the old flags now with changes made above. */
4629 old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
4630 elf_elfheader (obfd)->e_flags = old_flags;
4631 if (old_partial != (old_flags & EF_FRV_CPU_MASK))
4632 bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
4634 if (((new_flags & EF_FRV_FDPIC) == 0)
4635 != (! IS_FDPIC (ibfd)))
4637 error = TRUE;
4638 if (IS_FDPIC (obfd))
4639 (*_bfd_error_handler)
4640 (_("%s: cannot link non-fdpic object file into fdpic executable"),
4641 bfd_get_filename (ibfd));
4642 else
4643 (*_bfd_error_handler)
4644 (_("%s: cannot link fdpic object file into non-fdpic executable"),
4645 bfd_get_filename (ibfd));
4648 if (error)
4649 bfd_set_error (bfd_error_bad_value);
4651 return !error;
4655 bfd_boolean
4656 frv_elf_print_private_bfd_data (abfd, ptr)
4657 bfd *abfd;
4658 PTR ptr;
4660 FILE *file = (FILE *) ptr;
4661 flagword flags;
4663 BFD_ASSERT (abfd != NULL && ptr != NULL);
4665 /* Print normal ELF private data. */
4666 _bfd_elf_print_private_bfd_data (abfd, ptr);
4668 flags = elf_elfheader (abfd)->e_flags;
4669 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
4671 switch (flags & EF_FRV_CPU_MASK)
4673 default: break;
4674 case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple"); break;
4675 case EF_FRV_CPU_FR550: fprintf (file, " -mcpu=fr550"); break;
4676 case EF_FRV_CPU_FR500: fprintf (file, " -mcpu=fr500"); break;
4677 case EF_FRV_CPU_FR450: fprintf (file, " -mcpu=fr450"); break;
4678 case EF_FRV_CPU_FR405: fprintf (file, " -mcpu=fr405"); break;
4679 case EF_FRV_CPU_FR400: fprintf (file, " -mcpu=fr400"); break;
4680 case EF_FRV_CPU_FR300: fprintf (file, " -mcpu=fr300"); break;
4681 case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat"); break;
4684 switch (flags & EF_FRV_GPR_MASK)
4686 default: break;
4687 case EF_FRV_GPR_32: fprintf (file, " -mgpr-32"); break;
4688 case EF_FRV_GPR_64: fprintf (file, " -mgpr-64"); break;
4691 switch (flags & EF_FRV_FPR_MASK)
4693 default: break;
4694 case EF_FRV_FPR_32: fprintf (file, " -mfpr-32"); break;
4695 case EF_FRV_FPR_64: fprintf (file, " -mfpr-64"); break;
4696 case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float"); break;
4699 switch (flags & EF_FRV_DWORD_MASK)
4701 default: break;
4702 case EF_FRV_DWORD_YES: fprintf (file, " -mdword"); break;
4703 case EF_FRV_DWORD_NO: fprintf (file, " -mno-dword"); break;
4706 if (flags & EF_FRV_DOUBLE)
4707 fprintf (file, " -mdouble");
4709 if (flags & EF_FRV_MEDIA)
4710 fprintf (file, " -mmedia");
4712 if (flags & EF_FRV_MULADD)
4713 fprintf (file, " -mmuladd");
4715 if (flags & EF_FRV_PIC)
4716 fprintf (file, " -fpic");
4718 if (flags & EF_FRV_BIGPIC)
4719 fprintf (file, " -fPIC");
4721 if (flags & EF_FRV_LIBPIC)
4722 fprintf (file, " -mlibrary-pic");
4724 if (flags & EF_FRV_FDPIC)
4725 fprintf (file, " -mfdpic");
4727 if (flags & EF_FRV_NON_PIC_RELOCS)
4728 fprintf (file, " non-pic relocations");
4730 if (flags & EF_FRV_G0)
4731 fprintf (file, " -G0");
4733 fputc ('\n', file);
4734 return TRUE;
4738 #define ELF_ARCH bfd_arch_frv
4739 #define ELF_MACHINE_CODE EM_CYGNUS_FRV
4740 #define ELF_MAXPAGESIZE 0x1000
4742 #define TARGET_BIG_SYM bfd_elf32_frv_vec
4743 #define TARGET_BIG_NAME "elf32-frv"
4745 #define elf_info_to_howto frv_info_to_howto_rela
4746 #define elf_backend_relocate_section elf32_frv_relocate_section
4747 #define elf_backend_gc_mark_hook elf32_frv_gc_mark_hook
4748 #define elf_backend_gc_sweep_hook elf32_frv_gc_sweep_hook
4749 #define elf_backend_check_relocs elf32_frv_check_relocs
4750 #define elf_backend_object_p elf32_frv_object_p
4751 #define elf_backend_add_symbol_hook elf32_frv_add_symbol_hook
4753 #define elf_backend_can_gc_sections 1
4754 #define elf_backend_rela_normal 1
4756 #define bfd_elf32_bfd_reloc_type_lookup frv_reloc_type_lookup
4757 #define bfd_elf32_bfd_set_private_flags frv_elf_set_private_flags
4758 #define bfd_elf32_bfd_copy_private_bfd_data frv_elf_copy_private_bfd_data
4759 #define bfd_elf32_bfd_merge_private_bfd_data frv_elf_merge_private_bfd_data
4760 #define bfd_elf32_bfd_print_private_bfd_data frv_elf_print_private_bfd_data
4762 #define elf_backend_want_got_sym 1
4763 #define elf_backend_got_header_size 0
4764 #define elf_backend_want_got_plt 0
4765 #define elf_backend_plt_readonly 1
4766 #define elf_backend_want_plt_sym 0
4767 #define elf_backend_plt_header_size 0
4769 #define elf_backend_finish_dynamic_sections \
4770 elf32_frv_finish_dynamic_sections
4772 #include "elf32-target.h"
4774 #undef ELF_MAXPAGESIZE
4775 #define ELF_MAXPAGESIZE 0x4000
4777 #undef TARGET_BIG_SYM
4778 #define TARGET_BIG_SYM bfd_elf32_frvfdpic_vec
4779 #undef TARGET_BIG_NAME
4780 #define TARGET_BIG_NAME "elf32-frvfdpic"
4781 #undef elf32_bed
4782 #define elf32_bed elf32_frvfdpic_bed
4784 #undef elf_info_to_howto_rel
4785 #define elf_info_to_howto_rel frvfdpic_info_to_howto_rel
4787 #undef bfd_elf32_bfd_link_hash_table_create
4788 #define bfd_elf32_bfd_link_hash_table_create \
4789 frvfdpic_elf_link_hash_table_create
4790 #undef elf_backend_always_size_sections
4791 #define elf_backend_always_size_sections \
4792 elf32_frvfdpic_always_size_sections
4793 #undef elf_backend_modify_segment_map
4794 #define elf_backend_modify_segment_map \
4795 elf32_frvfdpic_modify_segment_map
4796 #undef bfd_elf32_bfd_copy_private_bfd_data
4797 #define bfd_elf32_bfd_copy_private_bfd_data \
4798 elf32_frvfdpic_copy_private_bfd_data
4800 #undef elf_backend_create_dynamic_sections
4801 #define elf_backend_create_dynamic_sections \
4802 elf32_frvfdpic_create_dynamic_sections
4803 #undef elf_backend_adjust_dynamic_symbol
4804 #define elf_backend_adjust_dynamic_symbol \
4805 elf32_frvfdpic_adjust_dynamic_symbol
4806 #undef elf_backend_size_dynamic_sections
4807 #define elf_backend_size_dynamic_sections \
4808 elf32_frvfdpic_size_dynamic_sections
4809 #undef elf_backend_finish_dynamic_symbol
4810 #define elf_backend_finish_dynamic_symbol \
4811 elf32_frvfdpic_finish_dynamic_symbol
4812 #undef elf_backend_finish_dynamic_sections
4813 #define elf_backend_finish_dynamic_sections \
4814 elf32_frvfdpic_finish_dynamic_sections
4816 #undef elf_backend_can_make_relative_eh_frame
4817 #define elf_backend_can_make_relative_eh_frame \
4818 frvfdpic_elf_use_relative_eh_frame
4819 #undef elf_backend_can_make_lsda_relative_eh_frame
4820 #define elf_backend_can_make_lsda_relative_eh_frame \
4821 frvfdpic_elf_use_relative_eh_frame
4822 #undef elf_backend_encode_eh_address
4823 #define elf_backend_encode_eh_address \
4824 frvfdpic_elf_encode_eh_address
4826 #undef elf_backend_may_use_rel_p
4827 #define elf_backend_may_use_rel_p 1
4828 #undef elf_backend_may_use_rela_p
4829 #define elf_backend_may_use_rela_p 1
4830 /* We use REL for dynamic relocations only. */
4831 #undef elf_backend_default_use_rela_p
4832 #define elf_backend_default_use_rela_p 1
4834 #undef elf_backend_omit_section_dynsym
4835 #define elf_backend_omit_section_dynsym _frvfdpic_link_omit_section_dynsym
4837 #include "elf32-target.h"