* emulparams/m68kelf.sh (GENERATE_PIE_SCRIPT): Define.
[binutils.git] / bfd / elf32-frv.c
blob6ce1b9f1143df64dade724d57d892d7846d80791
1 /* FRV-specific support for 32-bit ELF.
2 Copyright 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/frv.h"
27 #include "elf/dwarf2.h"
28 #include "hashtab.h"
30 /* Forward declarations. */
31 static bfd_reloc_status_type elf32_frv_relocate_lo16
32 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
33 static bfd_reloc_status_type elf32_frv_relocate_hi16
34 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
35 static bfd_reloc_status_type elf32_frv_relocate_label24
36 PARAMS ((bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
37 static bfd_reloc_status_type elf32_frv_relocate_gprel12
38 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
39 bfd_byte *, bfd_vma));
40 static bfd_reloc_status_type elf32_frv_relocate_gprelu12
41 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
42 bfd_byte *, bfd_vma));
43 static bfd_reloc_status_type elf32_frv_relocate_gprello
44 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
45 bfd_byte *, bfd_vma));
46 static bfd_reloc_status_type elf32_frv_relocate_gprelhi
47 PARAMS ((struct bfd_link_info *, bfd *, asection *, Elf_Internal_Rela *,
48 bfd_byte *, bfd_vma));
49 static reloc_howto_type *frv_reloc_type_lookup
50 PARAMS ((bfd *, bfd_reloc_code_real_type));
51 static void frv_info_to_howto_rela
52 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
53 static bfd_boolean elf32_frv_relocate_section
54 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
55 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
56 static bfd_boolean elf32_frv_add_symbol_hook
57 PARAMS (( bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
58 const char **, flagword *, asection **, bfd_vma *));
59 static bfd_reloc_status_type frv_final_link_relocate
60 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
61 Elf_Internal_Rela *, bfd_vma));
62 static bfd_boolean elf32_frv_check_relocs
63 PARAMS ((bfd *, struct bfd_link_info *, asection *,
64 const Elf_Internal_Rela *));
65 static int elf32_frv_machine
66 PARAMS ((bfd *));
67 static bfd_boolean elf32_frv_object_p
68 PARAMS ((bfd *));
69 static bfd_boolean frv_elf_set_private_flags
70 PARAMS ((bfd *, flagword));
71 static bfd_boolean frv_elf_copy_private_bfd_data
72 PARAMS ((bfd *, bfd *));
73 static bfd_boolean frv_elf_merge_private_bfd_data
74 PARAMS ((bfd *, bfd *));
75 static bfd_boolean frv_elf_print_private_bfd_data
76 PARAMS ((bfd *, PTR));
77 static bfd_boolean elf32_frv_grok_prstatus (bfd * abfd,
78 Elf_Internal_Note * note);
79 static bfd_boolean elf32_frv_grok_psinfo (bfd * abfd,
80 Elf_Internal_Note * note);
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 64-bit 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 */
464 /* A 24-bit pc-relative relocation referencing the TLS PLT entry for
465 a thread-local symbol. If the symbol number is 0, it refers to
466 the module. */
467 HOWTO (R_FRV_GETTLSOFF, /* type */
468 2, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 26, /* bitsize */
471 TRUE, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_bitfield, /* complain_on_overflow */
474 bfd_elf_generic_reloc, /* special_function */
475 "R_FRV_GETTLSOFF", /* name */
476 FALSE, /* partial_inplace */
477 0x7e03ffff, /* src_mask */
478 0x7e03ffff, /* dst_mask */
479 TRUE), /* pcrel_offset */
481 /* A 64-bit TLS descriptor for a symbol. This relocation is only
482 valid as a REL, dynamic relocation. */
483 HOWTO (R_FRV_TLSDESC_VALUE, /* type */
484 0, /* rightshift */
485 2, /* size (0 = byte, 1 = short, 2 = long) */
486 64, /* bitsize */
487 FALSE, /* pc_relative */
488 0, /* bitpos */
489 complain_overflow_bitfield, /* complain_on_overflow */
490 bfd_elf_generic_reloc, /* special_function */
491 "R_FRV_TLSDESC_VALUE", /* name */
492 FALSE, /* partial_inplace */
493 0xffffffff, /* src_mask */
494 0xffffffff, /* dst_mask */
495 FALSE), /* pcrel_offset */
497 /* A 12-bit signed operand with the GOT offset for the TLS
498 descriptor of the symbol. */
499 HOWTO (R_FRV_GOTTLSDESC12, /* type */
500 0, /* rightshift */
501 2, /* size (0 = byte, 1 = short, 2 = long) */
502 12, /* bitsize */
503 FALSE, /* pc_relative */
504 0, /* bitpos */
505 complain_overflow_signed, /* complain_on_overflow */
506 bfd_elf_generic_reloc, /* special_function */
507 "R_FRV_GOTTLSDESC12", /* name */
508 FALSE, /* partial_inplace */
509 0xfff, /* src_mask */
510 0xfff, /* dst_mask */
511 FALSE), /* pcrel_offset */
513 /* The upper 16 bits of the GOT offset for the TLS descriptor of the
514 symbol. */
515 HOWTO (R_FRV_GOTTLSDESCHI, /* type */
516 0, /* rightshift */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
518 16, /* bitsize */
519 FALSE, /* pc_relative */
520 0, /* bitpos */
521 complain_overflow_dont, /* complain_on_overflow */
522 bfd_elf_generic_reloc, /* special_function */
523 "R_FRV_GOTTLSDESCHI", /* name */
524 FALSE, /* partial_inplace */
525 0xffff, /* src_mask */
526 0xffff, /* dst_mask */
527 FALSE), /* pcrel_offset */
529 /* The lower 16 bits of the GOT offset for the TLS descriptor of the
530 symbol. */
531 HOWTO (R_FRV_GOTTLSDESCLO, /* type */
532 0, /* rightshift */
533 2, /* size (0 = byte, 1 = short, 2 = long) */
534 16, /* bitsize */
535 FALSE, /* pc_relative */
536 0, /* bitpos */
537 complain_overflow_dont, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_FRV_GOTTLSDESCLO", /* name */
540 FALSE, /* partial_inplace */
541 0xffff, /* src_mask */
542 0xffff, /* dst_mask */
543 FALSE), /* pcrel_offset */
545 /* A 12-bit signed operand with the offset from the module base
546 address to the thread-local symbol address. */
547 HOWTO (R_FRV_TLSMOFF12, /* type */
548 0, /* rightshift */
549 2, /* size (0 = byte, 1 = short, 2 = long) */
550 12, /* bitsize */
551 FALSE, /* pc_relative */
552 0, /* bitpos */
553 complain_overflow_signed, /* complain_on_overflow */
554 bfd_elf_generic_reloc, /* special_function */
555 "R_FRV_TLSMOFF12", /* name */
556 FALSE, /* partial_inplace */
557 0xfff, /* src_mask */
558 0xfff, /* dst_mask */
559 FALSE), /* pcrel_offset */
561 /* The upper 16 bits of the offset from the module base address to
562 the thread-local symbol address. */
563 HOWTO (R_FRV_TLSMOFFHI, /* type */
564 0, /* rightshift */
565 2, /* size (0 = byte, 1 = short, 2 = long) */
566 16, /* bitsize */
567 FALSE, /* pc_relative */
568 0, /* bitpos */
569 complain_overflow_dont, /* complain_on_overflow */
570 bfd_elf_generic_reloc, /* special_function */
571 "R_FRV_TLSMOFFHI", /* name */
572 FALSE, /* partial_inplace */
573 0xffff, /* src_mask */
574 0xffff, /* dst_mask */
575 FALSE), /* pcrel_offset */
577 /* The lower 16 bits of the offset from the module base address to
578 the thread-local symbol address. */
579 HOWTO (R_FRV_TLSMOFFLO, /* type */
580 0, /* rightshift */
581 2, /* size (0 = byte, 1 = short, 2 = long) */
582 16, /* bitsize */
583 FALSE, /* pc_relative */
584 0, /* bitpos */
585 complain_overflow_dont, /* complain_on_overflow */
586 bfd_elf_generic_reloc, /* special_function */
587 "R_FRV_TLSMOFFLO", /* name */
588 FALSE, /* partial_inplace */
589 0xffff, /* src_mask */
590 0xffff, /* dst_mask */
591 FALSE), /* pcrel_offset */
593 /* A 12-bit signed operand with the GOT offset for the TLSOFF entry
594 for a symbol. */
595 HOWTO (R_FRV_GOTTLSOFF12, /* type */
596 0, /* rightshift */
597 2, /* size (0 = byte, 1 = short, 2 = long) */
598 12, /* bitsize */
599 FALSE, /* pc_relative */
600 0, /* bitpos */
601 complain_overflow_signed, /* complain_on_overflow */
602 bfd_elf_generic_reloc, /* special_function */
603 "R_FRV_GOTTLSOFF12", /* name */
604 FALSE, /* partial_inplace */
605 0xfff, /* src_mask */
606 0xfff, /* dst_mask */
607 FALSE), /* pcrel_offset */
609 /* The upper 16 bits of the GOT offset for the TLSOFF entry for a
610 symbol. */
611 HOWTO (R_FRV_GOTTLSOFFHI, /* type */
612 0, /* rightshift */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
614 16, /* bitsize */
615 FALSE, /* pc_relative */
616 0, /* bitpos */
617 complain_overflow_dont, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_FRV_GOTTLSOFFHI", /* name */
620 FALSE, /* partial_inplace */
621 0xffff, /* src_mask */
622 0xffff, /* dst_mask */
623 FALSE), /* pcrel_offset */
625 /* The lower 16 bits of the GOT offset for the TLSOFF entry for a
626 symbol. */
627 HOWTO (R_FRV_GOTTLSOFFLO, /* type */
628 0, /* rightshift */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
630 16, /* bitsize */
631 FALSE, /* pc_relative */
632 0, /* bitpos */
633 complain_overflow_dont, /* complain_on_overflow */
634 bfd_elf_generic_reloc, /* special_function */
635 "R_FRV_GOTTLSOFFLO", /* name */
636 FALSE, /* partial_inplace */
637 0xffff, /* src_mask */
638 0xffff, /* dst_mask */
639 FALSE), /* pcrel_offset */
641 /* The 32-bit offset from the thread pointer (not the module base
642 address) to a thread-local symbol. */
643 HOWTO (R_FRV_TLSOFF, /* type */
644 0, /* rightshift */
645 2, /* size (0 = byte, 1 = short, 2 = long) */
646 32, /* bitsize */
647 FALSE, /* pc_relative */
648 0, /* bitpos */
649 complain_overflow_dont, /* complain_on_overflow */
650 bfd_elf_generic_reloc, /* special_function */
651 "R_FRV_TLSOFF", /* name */
652 FALSE, /* partial_inplace */
653 0xffffffff, /* src_mask */
654 0xffffffff, /* dst_mask */
655 FALSE), /* pcrel_offset */
657 /* An annotation for linker relaxation, that denotes the
658 symbol+addend whose TLS descriptor is referenced by the sum of
659 the two input registers of an ldd instruction. */
660 HOWTO (R_FRV_TLSDESC_RELAX, /* type */
661 0, /* rightshift */
662 2, /* size (0 = byte, 1 = short, 2 = long) */
663 0, /* bitsize */
664 FALSE, /* pc_relative */
665 0, /* bitpos */
666 complain_overflow_dont, /* complain_on_overflow */
667 bfd_elf_generic_reloc, /* special_function */
668 "R_FRV_TLSDESC_RELAX", /* name */
669 FALSE, /* partial_inplace */
670 0, /* src_mask */
671 0, /* dst_mask */
672 FALSE), /* pcrel_offset */
674 /* An annotation for linker relaxation, that denotes the
675 symbol+addend whose TLS resolver entry point is given by the sum
676 of the two register operands of an calll instruction. */
677 HOWTO (R_FRV_GETTLSOFF_RELAX, /* type */
678 0, /* rightshift */
679 2, /* size (0 = byte, 1 = short, 2 = long) */
680 0, /* bitsize */
681 FALSE, /* pc_relative */
682 0, /* bitpos */
683 complain_overflow_dont, /* complain_on_overflow */
684 bfd_elf_generic_reloc, /* special_function */
685 "R_FRV_GETTLSOFF_RELAX", /* name */
686 FALSE, /* partial_inplace */
687 0, /* src_mask */
688 0, /* dst_mask */
689 FALSE), /* pcrel_offset */
691 /* An annotation for linker relaxation, that denotes the
692 symbol+addend whose TLS offset GOT entry is given by the sum of
693 the two input registers of an ld instruction. */
694 HOWTO (R_FRV_TLSOFF_RELAX, /* type */
695 0, /* rightshift */
696 2, /* size (0 = byte, 1 = short, 2 = long) */
697 0, /* bitsize */
698 FALSE, /* pc_relative */
699 0, /* bitpos */
700 complain_overflow_bitfield, /* complain_on_overflow */
701 bfd_elf_generic_reloc, /* special_function */
702 "R_FRV_TLSOFF_RELAX", /* name */
703 FALSE, /* partial_inplace */
704 0, /* src_mask */
705 0, /* dst_mask */
706 FALSE), /* pcrel_offset */
708 /* A 32-bit offset from the module base address to
709 the thread-local symbol address. */
710 HOWTO (R_FRV_TLSMOFF, /* type */
711 0, /* rightshift */
712 2, /* size (0 = byte, 1 = short, 2 = long) */
713 32, /* bitsize */
714 FALSE, /* pc_relative */
715 0, /* bitpos */
716 complain_overflow_dont, /* complain_on_overflow */
717 bfd_elf_generic_reloc, /* special_function */
718 "R_FRV_TLSMOFF", /* name */
719 FALSE, /* partial_inplace */
720 0xffffffff, /* src_mask */
721 0xffffffff, /* dst_mask */
722 FALSE), /* pcrel_offset */
725 /* GNU extension to record C++ vtable hierarchy. */
726 static reloc_howto_type elf32_frv_vtinherit_howto =
727 HOWTO (R_FRV_GNU_VTINHERIT, /* type */
728 0, /* rightshift */
729 2, /* size (0 = byte, 1 = short, 2 = long) */
730 0, /* bitsize */
731 FALSE, /* pc_relative */
732 0, /* bitpos */
733 complain_overflow_dont, /* complain_on_overflow */
734 NULL, /* special_function */
735 "R_FRV_GNU_VTINHERIT", /* name */
736 FALSE, /* partial_inplace */
737 0, /* src_mask */
738 0, /* dst_mask */
739 FALSE); /* pcrel_offset */
741 /* GNU extension to record C++ vtable member usage. */
742 static reloc_howto_type elf32_frv_vtentry_howto =
743 HOWTO (R_FRV_GNU_VTENTRY, /* type */
744 0, /* rightshift */
745 2, /* size (0 = byte, 1 = short, 2 = long) */
746 0, /* bitsize */
747 FALSE, /* pc_relative */
748 0, /* bitpos */
749 complain_overflow_dont, /* complain_on_overflow */
750 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
751 "R_FRV_GNU_VTENTRY", /* name */
752 FALSE, /* partial_inplace */
753 0, /* src_mask */
754 0, /* dst_mask */
755 FALSE); /* pcrel_offset */
757 /* The following 3 relocations are REL. The only difference to the
758 entries in the table above are that partial_inplace is TRUE. */
759 static reloc_howto_type elf32_frv_rel_32_howto =
760 HOWTO (R_FRV_32, /* type */
761 0, /* rightshift */
762 2, /* size (0 = byte, 1 = short, 2 = long) */
763 32, /* bitsize */
764 FALSE, /* pc_relative */
765 0, /* bitpos */
766 complain_overflow_bitfield, /* complain_on_overflow */
767 bfd_elf_generic_reloc, /* special_function */
768 "R_FRV_32", /* name */
769 TRUE, /* partial_inplace */
770 0xffffffff, /* src_mask */
771 0xffffffff, /* dst_mask */
772 FALSE); /* pcrel_offset */
774 static reloc_howto_type elf32_frv_rel_funcdesc_howto =
775 HOWTO (R_FRV_FUNCDESC, /* type */
776 0, /* rightshift */
777 2, /* size (0 = byte, 1 = short, 2 = long) */
778 32, /* bitsize */
779 FALSE, /* pc_relative */
780 0, /* bitpos */
781 complain_overflow_bitfield, /* complain_on_overflow */
782 bfd_elf_generic_reloc, /* special_function */
783 "R_FRV_FUNCDESC", /* name */
784 TRUE, /* partial_inplace */
785 0xffffffff, /* src_mask */
786 0xffffffff, /* dst_mask */
787 FALSE); /* pcrel_offset */
789 static reloc_howto_type elf32_frv_rel_funcdesc_value_howto =
790 HOWTO (R_FRV_FUNCDESC_VALUE, /* type */
791 0, /* rightshift */
792 2, /* size (0 = byte, 1 = short, 2 = long) */
793 64, /* bitsize */
794 FALSE, /* pc_relative */
795 0, /* bitpos */
796 complain_overflow_bitfield, /* complain_on_overflow */
797 bfd_elf_generic_reloc, /* special_function */
798 "R_FRV_FUNCDESC_VALUE", /* name */
799 TRUE, /* partial_inplace */
800 0xffffffff, /* src_mask */
801 0xffffffff, /* dst_mask */
802 FALSE); /* pcrel_offset */
804 static reloc_howto_type elf32_frv_rel_tlsdesc_value_howto =
805 /* A 64-bit TLS descriptor for a symbol. The first word resolves to
806 an entry point, and the second resolves to a special argument.
807 If the symbol turns out to be in static TLS, the entry point is a
808 return instruction, and the special argument is the TLS offset
809 for the symbol. If it's in dynamic TLS, the entry point is a TLS
810 offset resolver, and the special argument is a pointer to a data
811 structure allocated by the dynamic loader, containing the GOT
812 address for the offset resolver, the module id, the offset within
813 the module, and anything else the TLS offset resolver might need
814 to determine the TLS offset for the symbol in the running
815 thread. */
816 HOWTO (R_FRV_TLSDESC_VALUE, /* type */
817 0, /* rightshift */
818 2, /* size (0 = byte, 1 = short, 2 = long) */
819 64, /* bitsize */
820 FALSE, /* pc_relative */
821 0, /* bitpos */
822 complain_overflow_bitfield, /* complain_on_overflow */
823 bfd_elf_generic_reloc, /* special_function */
824 "R_FRV_TLSDESC_VALUE", /* name */
825 TRUE, /* partial_inplace */
826 0xffffffff, /* src_mask */
827 0xffffffff, /* dst_mask */
828 FALSE); /* pcrel_offset */
830 static reloc_howto_type elf32_frv_rel_tlsoff_howto =
831 /* The 32-bit offset from the thread pointer (not the module base
832 address) to a thread-local symbol. */
833 HOWTO (R_FRV_TLSOFF, /* type */
834 0, /* rightshift */
835 2, /* size (0 = byte, 1 = short, 2 = long) */
836 32, /* bitsize */
837 FALSE, /* pc_relative */
838 0, /* bitpos */
839 complain_overflow_bitfield, /* complain_on_overflow */
840 bfd_elf_generic_reloc, /* special_function */
841 "R_FRV_TLSOFF", /* name */
842 TRUE, /* partial_inplace */
843 0xffffffff, /* src_mask */
844 0xffffffff, /* dst_mask */
845 FALSE); /* pcrel_offset */
849 extern const bfd_target bfd_elf32_frvfdpic_vec;
850 #define IS_FDPIC(bfd) ((bfd)->xvec == &bfd_elf32_frvfdpic_vec)
852 /* An extension of the elf hash table data structure, containing some
853 additional FRV-specific data. */
854 struct frvfdpic_elf_link_hash_table
856 struct elf_link_hash_table elf;
858 /* A pointer to the .got section. */
859 asection *sgot;
860 /* A pointer to the .rel.got section. */
861 asection *sgotrel;
862 /* A pointer to the .rofixup section. */
863 asection *sgotfixup;
864 /* A pointer to the .plt section. */
865 asection *splt;
866 /* A pointer to the .rel.plt section. */
867 asection *spltrel;
868 /* GOT base offset. */
869 bfd_vma got0;
870 /* Location of the first non-lazy PLT entry, i.e., the number of
871 bytes taken by lazy PLT entries. If locally-bound TLS
872 descriptors require a ret instruction, it will be placed at this
873 offset. */
874 bfd_vma plt0;
875 /* A hash table holding information about which symbols were
876 referenced with which PIC-related relocations. */
877 struct htab *relocs_info;
878 /* Summary reloc information collected by
879 _frvfdpic_count_got_plt_entries. */
880 struct _frvfdpic_dynamic_got_info *g;
883 /* Get the FRV ELF linker hash table from a link_info structure. */
885 #define frvfdpic_hash_table(info) \
886 ((struct frvfdpic_elf_link_hash_table *) ((info)->hash))
888 #define frvfdpic_got_section(info) \
889 (frvfdpic_hash_table (info)->sgot)
890 #define frvfdpic_gotrel_section(info) \
891 (frvfdpic_hash_table (info)->sgotrel)
892 #define frvfdpic_gotfixup_section(info) \
893 (frvfdpic_hash_table (info)->sgotfixup)
894 #define frvfdpic_plt_section(info) \
895 (frvfdpic_hash_table (info)->splt)
896 #define frvfdpic_pltrel_section(info) \
897 (frvfdpic_hash_table (info)->spltrel)
898 #define frvfdpic_relocs_info(info) \
899 (frvfdpic_hash_table (info)->relocs_info)
900 #define frvfdpic_got_initial_offset(info) \
901 (frvfdpic_hash_table (info)->got0)
902 #define frvfdpic_plt_initial_offset(info) \
903 (frvfdpic_hash_table (info)->plt0)
904 #define frvfdpic_dynamic_got_plt_info(info) \
905 (frvfdpic_hash_table (info)->g)
907 /* Currently it's the same, but if some day we have a reason to change
908 it, we'd better be using a different macro.
910 FIXME: if there's any TLS PLT entry that uses local-exec or
911 initial-exec models, we could use the ret at the end of any of them
912 instead of adding one more. */
913 #define frvfdpic_plt_tls_ret_offset(info) \
914 (frvfdpic_plt_initial_offset (info))
916 /* The name of the dynamic interpreter. This is put in the .interp
917 section. */
919 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
921 #define DEFAULT_STACK_SIZE 0x20000
923 /* This structure is used to collect the number of entries present in
924 each addressable range of the got. */
925 struct _frvfdpic_dynamic_got_info
927 /* Several bits of information about the current link. */
928 struct bfd_link_info *info;
929 /* Total GOT size needed for GOT entries within the 12-, 16- or 32-bit
930 ranges. */
931 bfd_vma got12, gotlos, gothilo;
932 /* Total GOT size needed for function descriptor entries within the 12-,
933 16- or 32-bit ranges. */
934 bfd_vma fd12, fdlos, fdhilo;
935 /* Total GOT size needed by function descriptor entries referenced
936 in PLT entries, that would be profitable to place in offsets
937 close to the PIC register. */
938 bfd_vma fdplt;
939 /* Total PLT size needed by lazy PLT entries. */
940 bfd_vma lzplt;
941 /* Total GOT size needed for TLS descriptor entries within the 12-,
942 16- or 32-bit ranges. */
943 bfd_vma tlsd12, tlsdlos, tlsdhilo;
944 /* Total GOT size needed by TLS descriptors referenced in PLT
945 entries, that would be profitable to place in offers close to the
946 PIC register. */
947 bfd_vma tlsdplt;
948 /* Total PLT size needed by TLS lazy PLT entries. */
949 bfd_vma tlslzplt;
950 /* Number of relocations carried over from input object files. */
951 unsigned long relocs;
952 /* Number of fixups introduced by relocations in input object files. */
953 unsigned long fixups;
954 /* The number of fixups that reference the ret instruction added to
955 the PLT for locally-resolved TLS descriptors. */
956 unsigned long tls_ret_refs;
959 /* This structure is used to assign offsets to got entries, function
960 descriptors, plt entries and lazy plt entries. */
962 struct _frvfdpic_dynamic_got_plt_info
964 /* Summary information collected with _frvfdpic_count_got_plt_entries. */
965 struct _frvfdpic_dynamic_got_info g;
967 /* For each addressable range, we record a MAX (positive) and MIN
968 (negative) value. CUR is used to assign got entries, and it's
969 incremented from an initial positive value to MAX, then from MIN
970 to FDCUR (unless FDCUR wraps around first). FDCUR is used to
971 assign function descriptors, and it's decreased from an initial
972 non-positive value to MIN, then from MAX down to CUR (unless CUR
973 wraps around first). All of MIN, MAX, CUR and FDCUR always point
974 to even words. ODD, if non-zero, indicates an odd word to be
975 used for the next got entry, otherwise CUR is used and
976 incremented by a pair of words, wrapping around when it reaches
977 MAX. FDCUR is decremented (and wrapped) before the next function
978 descriptor is chosen. FDPLT indicates the number of remaining
979 slots that can be used for function descriptors used only by PLT
980 entries.
982 TMAX, TMIN and TCUR are used to assign TLS descriptors. TCUR
983 starts as MAX, and grows up to TMAX, then wraps around to TMIN
984 and grows up to MIN. TLSDPLT indicates the number of remaining
985 slots that can be used for TLS descriptors used only by TLS PLT
986 entries. */
987 struct _frvfdpic_dynamic_got_alloc_data
989 bfd_signed_vma max, cur, odd, fdcur, min;
990 bfd_signed_vma tmax, tcur, tmin;
991 bfd_vma fdplt, tlsdplt;
992 } got12, gotlos, gothilo;
995 /* Create an FRV ELF linker hash table. */
997 static struct bfd_link_hash_table *
998 frvfdpic_elf_link_hash_table_create (bfd *abfd)
1000 struct frvfdpic_elf_link_hash_table *ret;
1001 bfd_size_type amt = sizeof (struct frvfdpic_elf_link_hash_table);
1003 ret = bfd_zalloc (abfd, amt);
1004 if (ret == NULL)
1005 return NULL;
1007 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1008 _bfd_elf_link_hash_newfunc,
1009 sizeof (struct elf_link_hash_entry)))
1011 free (ret);
1012 return NULL;
1015 return &ret->elf.root;
1018 /* Decide whether a reference to a symbol can be resolved locally or
1019 not. If the symbol is protected, we want the local address, but
1020 its function descriptor must be assigned by the dynamic linker. */
1021 #define FRVFDPIC_SYM_LOCAL(INFO, H) \
1022 (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
1023 || ! elf_hash_table (INFO)->dynamic_sections_created)
1024 #define FRVFDPIC_FUNCDESC_LOCAL(INFO, H) \
1025 ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
1027 /* This structure collects information on what kind of GOT, PLT or
1028 function descriptors are required by relocations that reference a
1029 certain symbol. */
1030 struct frvfdpic_relocs_info
1032 /* The index of the symbol, as stored in the relocation r_info, if
1033 we have a local symbol; -1 otherwise. */
1034 long symndx;
1035 union
1037 /* The input bfd in which the symbol is defined, if it's a local
1038 symbol. */
1039 bfd *abfd;
1040 /* If symndx == -1, the hash table entry corresponding to a global
1041 symbol (even if it turns out to bind locally, in which case it
1042 should ideally be replaced with section's symndx + addend). */
1043 struct elf_link_hash_entry *h;
1044 } d;
1045 /* The addend of the relocation that references the symbol. */
1046 bfd_vma addend;
1048 /* The fields above are used to identify an entry. The fields below
1049 contain information on how an entry is used and, later on, which
1050 locations it was assigned. */
1051 /* The following 3 fields record whether the symbol+addend above was
1052 ever referenced with a GOT relocation. The 12 suffix indicates a
1053 GOT12 relocation; los is used for GOTLO relocations that are not
1054 matched by a GOTHI relocation; hilo is used for GOTLO/GOTHI
1055 pairs. */
1056 unsigned got12:1;
1057 unsigned gotlos:1;
1058 unsigned gothilo:1;
1059 /* Whether a FUNCDESC relocation references symbol+addend. */
1060 unsigned fd:1;
1061 /* Whether a FUNCDESC_GOT relocation references symbol+addend. */
1062 unsigned fdgot12:1;
1063 unsigned fdgotlos:1;
1064 unsigned fdgothilo:1;
1065 /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend. */
1066 unsigned fdgoff12:1;
1067 unsigned fdgofflos:1;
1068 unsigned fdgoffhilo:1;
1069 /* Whether a GETTLSOFF relocation references symbol+addend. */
1070 unsigned tlsplt:1;
1071 /* FIXME: we should probably add tlspltdesc, tlspltoff and
1072 tlspltimm, to tell what kind of TLS PLT entry we're generating.
1073 We might instead just pre-compute flags telling whether the
1074 object is suitable for local exec, initial exec or general
1075 dynamic addressing, and use that all over the place. We could
1076 also try to do a better job of merging TLSOFF and TLSDESC entries
1077 in main executables, but perhaps we can get rid of TLSDESC
1078 entirely in them instead. */
1079 /* Whether a GOTTLSDESC relocation references symbol+addend. */
1080 unsigned tlsdesc12:1;
1081 unsigned tlsdesclos:1;
1082 unsigned tlsdeschilo:1;
1083 /* Whether a GOTTLSOFF relocation references symbol+addend. */
1084 unsigned tlsoff12:1;
1085 unsigned tlsofflos:1;
1086 unsigned tlsoffhilo:1;
1087 /* Whether symbol+addend is referenced with GOTOFF12, GOTOFFLO or
1088 GOTOFFHI relocations. The addend doesn't really matter, since we
1089 envision that this will only be used to check whether the symbol
1090 is mapped to the same segment as the got. */
1091 unsigned gotoff:1;
1092 /* Whether symbol+addend is referenced by a LABEL24 relocation. */
1093 unsigned call:1;
1094 /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
1095 relocation. */
1096 unsigned sym:1;
1097 /* Whether we need a PLT entry for a symbol. Should be implied by
1098 something like:
1099 (call && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)) */
1100 unsigned plt:1;
1101 /* Whether a function descriptor should be created in this link unit
1102 for symbol+addend. Should be implied by something like:
1103 (plt || fdgotoff12 || fdgotofflos || fdgotofflohi
1104 || ((fd || fdgot12 || fdgotlos || fdgothilo)
1105 && (symndx != -1 || FRVFDPIC_FUNCDESC_LOCAL (info, d.h)))) */
1106 unsigned privfd:1;
1107 /* Whether a lazy PLT entry is needed for this symbol+addend.
1108 Should be implied by something like:
1109 (privfd && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)
1110 && ! (info->flags & DF_BIND_NOW)) */
1111 unsigned lazyplt:1;
1112 /* Whether we've already emitted GOT relocations and PLT entries as
1113 needed for this symbol. */
1114 unsigned done:1;
1116 /* The number of R_FRV_32, R_FRV_FUNCDESC, R_FRV_FUNCDESC_VALUE and
1117 R_FRV_TLSDESC_VALUE, R_FRV_TLSOFF relocations referencing
1118 symbol+addend. */
1119 unsigned relocs32, relocsfd, relocsfdv, relocstlsd, relocstlsoff;
1121 /* The number of .rofixups entries and dynamic relocations allocated
1122 for this symbol, minus any that might have already been used. */
1123 unsigned fixups, dynrelocs;
1125 /* The offsets of the GOT entries assigned to symbol+addend, to the
1126 function descriptor's address, and to a function descriptor,
1127 respectively. Should be zero if unassigned. The offsets are
1128 counted from the value that will be assigned to the PIC register,
1129 not from the beginning of the .got section. */
1130 bfd_signed_vma got_entry, fdgot_entry, fd_entry;
1131 /* The offsets of the PLT entries assigned to symbol+addend,
1132 non-lazy and lazy, respectively. If unassigned, should be
1133 (bfd_vma)-1. */
1134 bfd_vma plt_entry, lzplt_entry;
1135 /* The offsets of the GOT entries for TLS offset and TLS descriptor. */
1136 bfd_signed_vma tlsoff_entry, tlsdesc_entry;
1137 /* The offset of the TLS offset PLT entry. */
1138 bfd_vma tlsplt_entry;
1141 /* Compute a hash with the key fields of an frvfdpic_relocs_info entry. */
1142 static hashval_t
1143 frvfdpic_relocs_info_hash (const void *entry_)
1145 const struct frvfdpic_relocs_info *entry = entry_;
1147 return (entry->symndx == -1
1148 ? (long) entry->d.h->root.root.hash
1149 : entry->symndx + (long) entry->d.abfd->id * 257) + entry->addend;
1152 /* Test whether the key fields of two frvfdpic_relocs_info entries are
1153 identical. */
1154 static int
1155 frvfdpic_relocs_info_eq (const void *entry1, const void *entry2)
1157 const struct frvfdpic_relocs_info *e1 = entry1;
1158 const struct frvfdpic_relocs_info *e2 = entry2;
1160 return e1->symndx == e2->symndx && e1->addend == e2->addend
1161 && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
1164 /* Find or create an entry in a hash table HT that matches the key
1165 fields of the given ENTRY. If it's not found, memory for a new
1166 entry is allocated in ABFD's obstack. */
1167 static struct frvfdpic_relocs_info *
1168 frvfdpic_relocs_info_find (struct htab *ht,
1169 bfd *abfd,
1170 const struct frvfdpic_relocs_info *entry,
1171 enum insert_option insert)
1173 struct frvfdpic_relocs_info **loc =
1174 (struct frvfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
1176 if (! loc)
1177 return NULL;
1179 if (*loc)
1180 return *loc;
1182 *loc = bfd_zalloc (abfd, sizeof (**loc));
1184 if (! *loc)
1185 return *loc;
1187 (*loc)->symndx = entry->symndx;
1188 (*loc)->d = entry->d;
1189 (*loc)->addend = entry->addend;
1190 (*loc)->plt_entry = (bfd_vma)-1;
1191 (*loc)->lzplt_entry = (bfd_vma)-1;
1192 (*loc)->tlsplt_entry = (bfd_vma)-1;
1194 return *loc;
1197 /* Obtain the address of the entry in HT associated with H's symbol +
1198 addend, creating a new entry if none existed. ABFD is only used
1199 for memory allocation purposes. */
1200 inline static struct frvfdpic_relocs_info *
1201 frvfdpic_relocs_info_for_global (struct htab *ht,
1202 bfd *abfd,
1203 struct elf_link_hash_entry *h,
1204 bfd_vma addend,
1205 enum insert_option insert)
1207 struct frvfdpic_relocs_info entry;
1209 entry.symndx = -1;
1210 entry.d.h = h;
1211 entry.addend = addend;
1213 return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
1216 /* Obtain the address of the entry in HT associated with the SYMNDXth
1217 local symbol of the input bfd ABFD, plus the addend, creating a new
1218 entry if none existed. */
1219 inline static struct frvfdpic_relocs_info *
1220 frvfdpic_relocs_info_for_local (struct htab *ht,
1221 bfd *abfd,
1222 long symndx,
1223 bfd_vma addend,
1224 enum insert_option insert)
1226 struct frvfdpic_relocs_info entry;
1228 entry.symndx = symndx;
1229 entry.d.abfd = abfd;
1230 entry.addend = addend;
1232 return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
1235 /* Merge fields set by check_relocs() of two entries that end up being
1236 mapped to the same (presumably global) symbol. */
1238 inline static void
1239 frvfdpic_pic_merge_early_relocs_info (struct frvfdpic_relocs_info *e2,
1240 struct frvfdpic_relocs_info const *e1)
1242 e2->got12 |= e1->got12;
1243 e2->gotlos |= e1->gotlos;
1244 e2->gothilo |= e1->gothilo;
1245 e2->fd |= e1->fd;
1246 e2->fdgot12 |= e1->fdgot12;
1247 e2->fdgotlos |= e1->fdgotlos;
1248 e2->fdgothilo |= e1->fdgothilo;
1249 e2->fdgoff12 |= e1->fdgoff12;
1250 e2->fdgofflos |= e1->fdgofflos;
1251 e2->fdgoffhilo |= e1->fdgoffhilo;
1252 e2->tlsplt |= e1->tlsplt;
1253 e2->tlsdesc12 |= e1->tlsdesc12;
1254 e2->tlsdesclos |= e1->tlsdesclos;
1255 e2->tlsdeschilo |= e1->tlsdeschilo;
1256 e2->tlsoff12 |= e1->tlsoff12;
1257 e2->tlsofflos |= e1->tlsofflos;
1258 e2->tlsoffhilo |= e1->tlsoffhilo;
1259 e2->gotoff |= e1->gotoff;
1260 e2->call |= e1->call;
1261 e2->sym |= e1->sym;
1264 /* Every block of 65535 lazy PLT entries shares a single call to the
1265 resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
1266 32767, counting from 0). All other lazy PLT entries branch to it
1267 in a single instruction. */
1269 #define FRVFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) 8 * 65535 + 4)
1270 #define FRVFDPIC_LZPLT_RESOLV_LOC (8 * 32767)
1272 /* Add a dynamic relocation to the SRELOC section. */
1274 inline static bfd_vma
1275 _frvfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
1276 int reloc_type, long dynindx, bfd_vma addend,
1277 struct frvfdpic_relocs_info *entry)
1279 Elf_Internal_Rela outrel;
1280 bfd_vma reloc_offset;
1282 outrel.r_offset = offset;
1283 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
1284 outrel.r_addend = addend;
1286 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
1287 BFD_ASSERT (reloc_offset < sreloc->size);
1288 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1289 sreloc->contents + reloc_offset);
1290 sreloc->reloc_count++;
1292 /* If the entry's index is zero, this relocation was probably to a
1293 linkonce section that got discarded. We reserved a dynamic
1294 relocation, but it was for another entry than the one we got at
1295 the time of emitting the relocation. Unfortunately there's no
1296 simple way for us to catch this situation, since the relocation
1297 is cleared right before calling relocate_section, at which point
1298 we no longer know what the relocation used to point to. */
1299 if (entry->symndx)
1301 BFD_ASSERT (entry->dynrelocs > 0);
1302 entry->dynrelocs--;
1305 return reloc_offset;
1308 /* Add a fixup to the ROFIXUP section. */
1310 static bfd_vma
1311 _frvfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
1312 struct frvfdpic_relocs_info *entry)
1314 bfd_vma fixup_offset;
1316 if (rofixup->flags & SEC_EXCLUDE)
1317 return -1;
1319 fixup_offset = rofixup->reloc_count * 4;
1320 if (rofixup->contents)
1322 BFD_ASSERT (fixup_offset < rofixup->size);
1323 bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
1325 rofixup->reloc_count++;
1327 if (entry && entry->symndx)
1329 /* See discussion about symndx == 0 in _frvfdpic_add_dyn_reloc
1330 above. */
1331 BFD_ASSERT (entry->fixups > 0);
1332 entry->fixups--;
1335 return fixup_offset;
1338 /* Find the segment number in which OSEC, and output section, is
1339 located. */
1341 static unsigned
1342 _frvfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
1344 Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
1346 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
1349 inline static bfd_boolean
1350 _frvfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
1352 unsigned seg = _frvfdpic_osec_to_segment (output_bfd, osec);
1354 return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
1357 #define FRVFDPIC_TLS_BIAS (2048 - 16)
1359 /* Return the base VMA address which should be subtracted from real addresses
1360 when resolving TLSMOFF relocation.
1361 This is PT_TLS segment p_vaddr, plus the 2048-16 bias. */
1363 static bfd_vma
1364 tls_biased_base (struct bfd_link_info *info)
1366 /* If tls_sec is NULL, we should have signalled an error already. */
1367 if (elf_hash_table (info)->tls_sec == NULL)
1368 return FRVFDPIC_TLS_BIAS;
1369 return elf_hash_table (info)->tls_sec->vma + FRVFDPIC_TLS_BIAS;
1372 /* Generate relocations for GOT entries, function descriptors, and
1373 code for PLT and lazy PLT entries. */
1375 inline static bfd_boolean
1376 _frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
1377 bfd *output_bfd,
1378 struct bfd_link_info *info,
1379 asection *sec,
1380 Elf_Internal_Sym *sym,
1381 bfd_vma addend)
1384 bfd_vma fd_lazy_rel_offset = (bfd_vma)-1;
1385 int dynindx = -1;
1387 if (entry->done)
1388 return TRUE;
1389 entry->done = 1;
1391 if (entry->got_entry || entry->fdgot_entry || entry->fd_entry
1392 || entry->tlsoff_entry || entry->tlsdesc_entry)
1394 /* If the symbol is dynamic, consider it for dynamic
1395 relocations, otherwise decay to section + offset. */
1396 if (entry->symndx == -1 && entry->d.h->dynindx != -1)
1397 dynindx = entry->d.h->dynindx;
1398 else
1400 if (sec->output_section
1401 && ! bfd_is_abs_section (sec->output_section)
1402 && ! bfd_is_und_section (sec->output_section))
1403 dynindx = elf_section_data (sec->output_section)->dynindx;
1404 else
1405 dynindx = 0;
1409 /* Generate relocation for GOT entry pointing to the symbol. */
1410 if (entry->got_entry)
1412 int idx = dynindx;
1413 bfd_vma ad = addend;
1415 /* If the symbol is dynamic but binds locally, use
1416 section+offset. */
1417 if (sec && (entry->symndx != -1
1418 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1420 if (entry->symndx == -1)
1421 ad += entry->d.h->root.u.def.value;
1422 else
1423 ad += sym->st_value;
1424 ad += sec->output_offset;
1425 if (sec->output_section && elf_section_data (sec->output_section))
1426 idx = elf_section_data (sec->output_section)->dynindx;
1427 else
1428 idx = 0;
1431 /* If we're linking an executable at a fixed address, we can
1432 omit the dynamic relocation as long as the symbol is local to
1433 this module. */
1434 if (info->executable && !info->pie
1435 && (entry->symndx != -1
1436 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1438 if (sec)
1439 ad += sec->output_section->vma;
1440 if (entry->symndx != -1
1441 || entry->d.h->root.type != bfd_link_hash_undefweak)
1442 _frvfdpic_add_rofixup (output_bfd,
1443 frvfdpic_gotfixup_section (info),
1444 frvfdpic_got_section (info)->output_section
1445 ->vma
1446 + frvfdpic_got_section (info)->output_offset
1447 + frvfdpic_got_initial_offset (info)
1448 + entry->got_entry, entry);
1450 else
1451 _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1452 _bfd_elf_section_offset
1453 (output_bfd, info,
1454 frvfdpic_got_section (info),
1455 frvfdpic_got_initial_offset (info)
1456 + entry->got_entry)
1457 + frvfdpic_got_section (info)
1458 ->output_section->vma
1459 + frvfdpic_got_section (info)->output_offset,
1460 R_FRV_32, idx, ad, entry);
1462 bfd_put_32 (output_bfd, ad,
1463 frvfdpic_got_section (info)->contents
1464 + frvfdpic_got_initial_offset (info)
1465 + entry->got_entry);
1468 /* Generate relocation for GOT entry pointing to a canonical
1469 function descriptor. */
1470 if (entry->fdgot_entry)
1472 int reloc, idx;
1473 bfd_vma ad = 0;
1475 if (! (entry->symndx == -1
1476 && entry->d.h->root.type == bfd_link_hash_undefweak
1477 && FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1479 /* If the symbol is dynamic and there may be dynamic symbol
1480 resolution because we are, or are linked with, a shared
1481 library, emit a FUNCDESC relocation such that the dynamic
1482 linker will allocate the function descriptor. If the
1483 symbol needs a non-local function descriptor but binds
1484 locally (e.g., its visibility is protected, emit a
1485 dynamic relocation decayed to section+offset. */
1486 if (entry->symndx == -1
1487 && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
1488 && FRVFDPIC_SYM_LOCAL (info, entry->d.h)
1489 && !(info->executable && !info->pie))
1491 reloc = R_FRV_FUNCDESC;
1492 idx = elf_section_data (entry->d.h->root.u.def.section
1493 ->output_section)->dynindx;
1494 ad = entry->d.h->root.u.def.section->output_offset
1495 + entry->d.h->root.u.def.value;
1497 else if (entry->symndx == -1
1498 && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
1500 reloc = R_FRV_FUNCDESC;
1501 idx = dynindx;
1502 ad = addend;
1503 if (ad)
1505 (*info->callbacks->reloc_dangerous)
1506 (info, _("relocation requires zero addend"),
1507 elf_hash_table (info)->dynobj,
1508 frvfdpic_got_section (info),
1509 entry->fdgot_entry);
1510 return FALSE;
1513 else
1515 /* Otherwise, we know we have a private function descriptor,
1516 so reference it directly. */
1517 if (elf_hash_table (info)->dynamic_sections_created)
1518 BFD_ASSERT (entry->privfd);
1519 reloc = R_FRV_32;
1520 idx = elf_section_data (frvfdpic_got_section (info)
1521 ->output_section)->dynindx;
1522 ad = frvfdpic_got_section (info)->output_offset
1523 + frvfdpic_got_initial_offset (info) + entry->fd_entry;
1526 /* If there is room for dynamic symbol resolution, emit the
1527 dynamic relocation. However, if we're linking an
1528 executable at a fixed location, we won't have emitted a
1529 dynamic symbol entry for the got section, so idx will be
1530 zero, which means we can and should compute the address
1531 of the private descriptor ourselves. */
1532 if (info->executable && !info->pie
1533 && (entry->symndx != -1
1534 || FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
1536 ad += frvfdpic_got_section (info)->output_section->vma;
1537 _frvfdpic_add_rofixup (output_bfd,
1538 frvfdpic_gotfixup_section (info),
1539 frvfdpic_got_section (info)
1540 ->output_section->vma
1541 + frvfdpic_got_section (info)
1542 ->output_offset
1543 + frvfdpic_got_initial_offset (info)
1544 + entry->fdgot_entry, entry);
1546 else
1547 _frvfdpic_add_dyn_reloc (output_bfd,
1548 frvfdpic_gotrel_section (info),
1549 _bfd_elf_section_offset
1550 (output_bfd, info,
1551 frvfdpic_got_section (info),
1552 frvfdpic_got_initial_offset (info)
1553 + entry->fdgot_entry)
1554 + frvfdpic_got_section (info)
1555 ->output_section->vma
1556 + frvfdpic_got_section (info)
1557 ->output_offset,
1558 reloc, idx, ad, entry);
1561 bfd_put_32 (output_bfd, ad,
1562 frvfdpic_got_section (info)->contents
1563 + frvfdpic_got_initial_offset (info)
1564 + entry->fdgot_entry);
1567 /* Generate relocation to fill in a private function descriptor in
1568 the GOT. */
1569 if (entry->fd_entry)
1571 int idx = dynindx;
1572 bfd_vma ad = addend;
1573 bfd_vma ofst;
1574 long lowword, highword;
1576 /* If the symbol is dynamic but binds locally, use
1577 section+offset. */
1578 if (sec && (entry->symndx != -1
1579 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1581 if (entry->symndx == -1)
1582 ad += entry->d.h->root.u.def.value;
1583 else
1584 ad += sym->st_value;
1585 ad += sec->output_offset;
1586 if (sec->output_section && elf_section_data (sec->output_section))
1587 idx = elf_section_data (sec->output_section)->dynindx;
1588 else
1589 idx = 0;
1592 /* If we're linking an executable at a fixed address, we can
1593 omit the dynamic relocation as long as the symbol is local to
1594 this module. */
1595 if (info->executable && !info->pie
1596 && (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1598 if (sec)
1599 ad += sec->output_section->vma;
1600 ofst = 0;
1601 if (entry->symndx != -1
1602 || entry->d.h->root.type != bfd_link_hash_undefweak)
1604 _frvfdpic_add_rofixup (output_bfd,
1605 frvfdpic_gotfixup_section (info),
1606 frvfdpic_got_section (info)
1607 ->output_section->vma
1608 + frvfdpic_got_section (info)
1609 ->output_offset
1610 + frvfdpic_got_initial_offset (info)
1611 + entry->fd_entry, entry);
1612 _frvfdpic_add_rofixup (output_bfd,
1613 frvfdpic_gotfixup_section (info),
1614 frvfdpic_got_section (info)
1615 ->output_section->vma
1616 + frvfdpic_got_section (info)
1617 ->output_offset
1618 + frvfdpic_got_initial_offset (info)
1619 + entry->fd_entry + 4, entry);
1622 else
1624 ofst =
1625 _frvfdpic_add_dyn_reloc (output_bfd,
1626 entry->lazyplt
1627 ? frvfdpic_pltrel_section (info)
1628 : frvfdpic_gotrel_section (info),
1629 _bfd_elf_section_offset
1630 (output_bfd, info,
1631 frvfdpic_got_section (info),
1632 frvfdpic_got_initial_offset (info)
1633 + entry->fd_entry)
1634 + frvfdpic_got_section (info)
1635 ->output_section->vma
1636 + frvfdpic_got_section (info)
1637 ->output_offset,
1638 R_FRV_FUNCDESC_VALUE, idx, ad, entry);
1641 /* If we've omitted the dynamic relocation, just emit the fixed
1642 addresses of the symbol and of the local GOT base offset. */
1643 if (info->executable && !info->pie && sec && sec->output_section)
1645 lowword = ad;
1646 highword = frvfdpic_got_section (info)->output_section->vma
1647 + frvfdpic_got_section (info)->output_offset
1648 + frvfdpic_got_initial_offset (info);
1650 else if (entry->lazyplt)
1652 if (ad)
1654 (*info->callbacks->reloc_dangerous)
1655 (info, _("relocation requires zero addend"),
1656 elf_hash_table (info)->dynobj,
1657 frvfdpic_got_section (info),
1658 entry->fd_entry);
1659 return FALSE;
1662 fd_lazy_rel_offset = ofst;
1664 /* A function descriptor used for lazy or local resolving is
1665 initialized such that its high word contains the output
1666 section index in which the PLT entries are located, and
1667 the low word contains the address of the lazy PLT entry
1668 entry point, that must be within the memory region
1669 assigned to that section. */
1670 lowword = entry->lzplt_entry + 4
1671 + frvfdpic_plt_section (info)->output_offset
1672 + frvfdpic_plt_section (info)->output_section->vma;
1673 highword = _frvfdpic_osec_to_segment
1674 (output_bfd, frvfdpic_plt_section (info)->output_section);
1676 else
1678 /* A function descriptor for a local function gets the index
1679 of the section. For a non-local function, it's
1680 disregarded. */
1681 lowword = ad;
1682 if (entry->symndx == -1 && entry->d.h->dynindx != -1
1683 && entry->d.h->dynindx == idx)
1684 highword = 0;
1685 else
1686 highword = _frvfdpic_osec_to_segment
1687 (output_bfd, sec->output_section);
1690 bfd_put_32 (output_bfd, lowword,
1691 frvfdpic_got_section (info)->contents
1692 + frvfdpic_got_initial_offset (info)
1693 + entry->fd_entry);
1694 bfd_put_32 (output_bfd, highword,
1695 frvfdpic_got_section (info)->contents
1696 + frvfdpic_got_initial_offset (info)
1697 + entry->fd_entry + 4);
1700 /* Generate code for the PLT entry. */
1701 if (entry->plt_entry != (bfd_vma) -1)
1703 bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1704 + entry->plt_entry;
1706 BFD_ASSERT (entry->fd_entry);
1708 /* Figure out what kind of PLT entry we need, depending on the
1709 location of the function descriptor within the GOT. */
1710 if (entry->fd_entry >= -(1 << (12 - 1))
1711 && entry->fd_entry < (1 << (12 - 1)))
1713 /* lddi @(gr15, fd_entry), gr14 */
1714 bfd_put_32 (output_bfd,
1715 0x9cccf000 | (entry->fd_entry & ((1 << 12) - 1)),
1716 plt_code);
1717 plt_code += 4;
1719 else
1721 if (entry->fd_entry >= -(1 << (16 - 1))
1722 && entry->fd_entry < (1 << (16 - 1)))
1724 /* setlos lo(fd_entry), gr14 */
1725 bfd_put_32 (output_bfd,
1726 0x9cfc0000
1727 | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1728 plt_code);
1729 plt_code += 4;
1731 else
1733 /* sethi.p hi(fd_entry), gr14
1734 setlo lo(fd_entry), gr14 */
1735 bfd_put_32 (output_bfd,
1736 0x1cf80000
1737 | ((entry->fd_entry >> 16)
1738 & (((bfd_vma)1 << 16) - 1)),
1739 plt_code);
1740 plt_code += 4;
1741 bfd_put_32 (output_bfd,
1742 0x9cf40000
1743 | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1744 plt_code);
1745 plt_code += 4;
1747 /* ldd @(gr14,gr15),gr14 */
1748 bfd_put_32 (output_bfd, 0x9c08e14f, plt_code);
1749 plt_code += 4;
1751 /* jmpl @(gr14,gr0) */
1752 bfd_put_32 (output_bfd, 0x8030e000, plt_code);
1755 /* Generate code for the lazy PLT entry. */
1756 if (entry->lzplt_entry != (bfd_vma) -1)
1758 bfd_byte *lzplt_code = frvfdpic_plt_section (info)->contents
1759 + entry->lzplt_entry;
1760 bfd_vma resolverStub_addr;
1762 bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
1763 lzplt_code += 4;
1765 resolverStub_addr = entry->lzplt_entry / FRVFDPIC_LZPLT_BLOCK_SIZE
1766 * FRVFDPIC_LZPLT_BLOCK_SIZE + FRVFDPIC_LZPLT_RESOLV_LOC;
1767 if (resolverStub_addr >= frvfdpic_plt_initial_offset (info))
1768 resolverStub_addr = frvfdpic_plt_initial_offset (info) - 12;
1770 if (entry->lzplt_entry == resolverStub_addr)
1772 /* This is a lazy PLT entry that includes a resolver call. */
1773 /* ldd @(gr15,gr0), gr4
1774 jmpl @(gr4,gr0) */
1775 bfd_put_32 (output_bfd, 0x8808f140, lzplt_code);
1776 bfd_put_32 (output_bfd, 0x80304000, lzplt_code + 4);
1778 else
1780 /* bra resolverStub */
1781 bfd_put_32 (output_bfd,
1782 0xc01a0000
1783 | (((resolverStub_addr - entry->lzplt_entry)
1784 / 4) & (((bfd_vma)1 << 16) - 1)),
1785 lzplt_code);
1789 /* Generate relocation for GOT entry holding the TLS offset. */
1790 if (entry->tlsoff_entry)
1792 int idx = dynindx;
1793 bfd_vma ad = addend;
1795 if (entry->symndx != -1
1796 || FRVFDPIC_SYM_LOCAL (info, entry->d.h))
1798 /* If the symbol is dynamic but binds locally, use
1799 section+offset. */
1800 if (sec)
1802 if (entry->symndx == -1)
1803 ad += entry->d.h->root.u.def.value;
1804 else
1805 ad += sym->st_value;
1806 ad += sec->output_offset;
1807 if (sec->output_section
1808 && elf_section_data (sec->output_section))
1809 idx = elf_section_data (sec->output_section)->dynindx;
1810 else
1811 idx = 0;
1815 /* *ABS*+addend is special for TLS relocations, use only the
1816 addend. */
1817 if (info->executable
1818 && idx == 0
1819 && (bfd_is_abs_section (sec)
1820 || bfd_is_und_section (sec)))
1822 /* If we're linking an executable, we can entirely omit the
1823 dynamic relocation if the symbol is local to this module. */
1824 else if (info->executable
1825 && (entry->symndx != -1
1826 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1828 if (sec)
1829 ad += sec->output_section->vma - tls_biased_base (info);
1831 else
1833 if (idx == 0
1834 && (bfd_is_abs_section (sec)
1835 || bfd_is_und_section (sec)))
1837 if (! elf_hash_table (info)->tls_sec)
1839 (*info->callbacks->undefined_symbol)
1840 (info, "TLS section", elf_hash_table (info)->dynobj,
1841 frvfdpic_got_section (info), entry->tlsoff_entry, TRUE);
1842 return FALSE;
1844 idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
1845 ad += FRVFDPIC_TLS_BIAS;
1847 _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1848 _bfd_elf_section_offset
1849 (output_bfd, info,
1850 frvfdpic_got_section (info),
1851 frvfdpic_got_initial_offset (info)
1852 + entry->tlsoff_entry)
1853 + frvfdpic_got_section (info)
1854 ->output_section->vma
1855 + frvfdpic_got_section (info)
1856 ->output_offset,
1857 R_FRV_TLSOFF, idx, ad, entry);
1860 bfd_put_32 (output_bfd, ad,
1861 frvfdpic_got_section (info)->contents
1862 + frvfdpic_got_initial_offset (info)
1863 + entry->tlsoff_entry);
1866 if (entry->tlsdesc_entry)
1868 int idx = dynindx;
1869 bfd_vma ad = addend;
1871 /* If the symbol is dynamic but binds locally, use
1872 section+offset. */
1873 if (sec && (entry->symndx != -1
1874 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1876 if (entry->symndx == -1)
1877 ad += entry->d.h->root.u.def.value;
1878 else
1879 ad += sym->st_value;
1880 ad += sec->output_offset;
1881 if (sec->output_section && elf_section_data (sec->output_section))
1882 idx = elf_section_data (sec->output_section)->dynindx;
1883 else
1884 idx = 0;
1887 /* If we didn't set up a TLS offset entry, but we're linking an
1888 executable and the symbol binds locally, we can use the
1889 module offset in the TLS descriptor in relaxations. */
1890 if (info->executable && ! entry->tlsoff_entry)
1891 entry->tlsoff_entry = entry->tlsdesc_entry + 4;
1893 if (info->executable && !info->pie
1894 && ((idx == 0
1895 && (bfd_is_abs_section (sec)
1896 || bfd_is_und_section (sec)))
1897 || entry->symndx != -1
1898 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1900 /* *ABS*+addend is special for TLS relocations, use only the
1901 addend for the TLS offset, and take the module id as
1902 0. */
1903 if (idx == 0
1904 && (bfd_is_abs_section (sec)
1905 || bfd_is_und_section (sec)))
1907 /* For other TLS symbols that bind locally, add the section
1908 TLS offset to the addend. */
1909 else if (sec)
1910 ad += sec->output_section->vma - tls_biased_base (info);
1912 bfd_put_32 (output_bfd,
1913 frvfdpic_plt_section (info)->output_section->vma
1914 + frvfdpic_plt_section (info)->output_offset
1915 + frvfdpic_plt_tls_ret_offset (info),
1916 frvfdpic_got_section (info)->contents
1917 + frvfdpic_got_initial_offset (info)
1918 + entry->tlsdesc_entry);
1920 _frvfdpic_add_rofixup (output_bfd,
1921 frvfdpic_gotfixup_section (info),
1922 frvfdpic_got_section (info)
1923 ->output_section->vma
1924 + frvfdpic_got_section (info)
1925 ->output_offset
1926 + frvfdpic_got_initial_offset (info)
1927 + entry->tlsdesc_entry, entry);
1929 BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs);
1931 /* We've used one of the reserved fixups, so discount it so
1932 that we can check at the end that we've used them
1933 all. */
1934 frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs--;
1936 /* While at that, make sure the ret instruction makes to the
1937 right location in the PLT. We could do it only when we
1938 got to 0, but since the check at the end will only print
1939 a warning, make sure we have the ret in place in case the
1940 warning is missed. */
1941 bfd_put_32 (output_bfd, 0xc03a4000,
1942 frvfdpic_plt_section (info)->contents
1943 + frvfdpic_plt_tls_ret_offset (info));
1945 else
1947 if (idx == 0
1948 && (bfd_is_abs_section (sec)
1949 || bfd_is_und_section (sec)))
1951 if (! elf_hash_table (info)->tls_sec)
1953 (*info->callbacks->undefined_symbol)
1954 (info, "TLS section", elf_hash_table (info)->dynobj,
1955 frvfdpic_got_section (info), entry->tlsdesc_entry, TRUE);
1956 return FALSE;
1958 idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
1959 ad += FRVFDPIC_TLS_BIAS;
1962 _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1963 _bfd_elf_section_offset
1964 (output_bfd, info,
1965 frvfdpic_got_section (info),
1966 frvfdpic_got_initial_offset (info)
1967 + entry->tlsdesc_entry)
1968 + frvfdpic_got_section (info)
1969 ->output_section->vma
1970 + frvfdpic_got_section (info)
1971 ->output_offset,
1972 R_FRV_TLSDESC_VALUE, idx, ad, entry);
1974 bfd_put_32 (output_bfd, 0,
1975 frvfdpic_got_section (info)->contents
1976 + frvfdpic_got_initial_offset (info)
1977 + entry->tlsdesc_entry);
1980 bfd_put_32 (output_bfd, ad,
1981 frvfdpic_got_section (info)->contents
1982 + frvfdpic_got_initial_offset (info)
1983 + entry->tlsdesc_entry + 4);
1986 /* Generate code for the get-TLS-offset PLT entry. */
1987 if (entry->tlsplt_entry != (bfd_vma) -1)
1989 bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1990 + entry->tlsplt_entry;
1992 if (info->executable
1993 && (entry->symndx != -1
1994 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1996 int idx = dynindx;
1997 bfd_vma ad = addend;
1999 /* sec may be NULL when referencing an undefweak symbol
2000 while linking a static executable. */
2001 if (!sec)
2003 BFD_ASSERT (entry->symndx == -1
2004 && entry->d.h->root.type == bfd_link_hash_undefweak);
2006 else
2008 if (entry->symndx == -1)
2009 ad += entry->d.h->root.u.def.value;
2010 else
2011 ad += sym->st_value;
2012 ad += sec->output_offset;
2013 if (sec->output_section
2014 && elf_section_data (sec->output_section))
2015 idx = elf_section_data (sec->output_section)->dynindx;
2016 else
2017 idx = 0;
2020 /* *ABS*+addend is special for TLS relocations, use only the
2021 addend for the TLS offset, and take the module id as
2022 0. */
2023 if (idx == 0
2024 && (bfd_is_abs_section (sec)
2025 || bfd_is_und_section (sec)))
2027 /* For other TLS symbols that bind locally, add the section
2028 TLS offset to the addend. */
2029 else if (sec)
2030 ad += sec->output_section->vma - tls_biased_base (info);
2032 if ((bfd_signed_vma)ad >= -(1 << (16 - 1))
2033 && (bfd_signed_vma)ad < (1 << (16 - 1)))
2035 /* setlos lo(ad), gr9 */
2036 bfd_put_32 (output_bfd,
2037 0x92fc0000
2038 | (ad
2039 & (((bfd_vma)1 << 16) - 1)),
2040 plt_code);
2041 plt_code += 4;
2043 else
2045 /* sethi.p hi(ad), gr9
2046 setlo lo(ad), gr9 */
2047 bfd_put_32 (output_bfd,
2048 0x12f80000
2049 | ((ad >> 16)
2050 & (((bfd_vma)1 << 16) - 1)),
2051 plt_code);
2052 plt_code += 4;
2053 bfd_put_32 (output_bfd,
2054 0x92f40000
2055 | (ad
2056 & (((bfd_vma)1 << 16) - 1)),
2057 plt_code);
2058 plt_code += 4;
2060 /* ret */
2061 bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
2063 else if (entry->tlsoff_entry)
2065 /* Figure out what kind of PLT entry we need, depending on the
2066 location of the TLS descriptor within the GOT. */
2067 if (entry->tlsoff_entry >= -(1 << (12 - 1))
2068 && entry->tlsoff_entry < (1 << (12 - 1)))
2070 /* ldi @(gr15, tlsoff_entry), gr9 */
2071 bfd_put_32 (output_bfd,
2072 0x92c8f000 | (entry->tlsoff_entry
2073 & ((1 << 12) - 1)),
2074 plt_code);
2075 plt_code += 4;
2077 else
2079 if (entry->tlsoff_entry >= -(1 << (16 - 1))
2080 && entry->tlsoff_entry < (1 << (16 - 1)))
2082 /* setlos lo(tlsoff_entry), gr8 */
2083 bfd_put_32 (output_bfd,
2084 0x90fc0000
2085 | (entry->tlsoff_entry
2086 & (((bfd_vma)1 << 16) - 1)),
2087 plt_code);
2088 plt_code += 4;
2090 else
2092 /* sethi.p hi(tlsoff_entry), gr8
2093 setlo lo(tlsoff_entry), gr8 */
2094 bfd_put_32 (output_bfd,
2095 0x10f80000
2096 | ((entry->tlsoff_entry >> 16)
2097 & (((bfd_vma)1 << 16) - 1)),
2098 plt_code);
2099 plt_code += 4;
2100 bfd_put_32 (output_bfd,
2101 0x90f40000
2102 | (entry->tlsoff_entry
2103 & (((bfd_vma)1 << 16) - 1)),
2104 plt_code);
2105 plt_code += 4;
2107 /* ld @(gr15,gr8),gr9 */
2108 bfd_put_32 (output_bfd, 0x9008f108, plt_code);
2109 plt_code += 4;
2111 /* ret */
2112 bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
2114 else
2116 BFD_ASSERT (entry->tlsdesc_entry);
2118 /* Figure out what kind of PLT entry we need, depending on the
2119 location of the TLS descriptor within the GOT. */
2120 if (entry->tlsdesc_entry >= -(1 << (12 - 1))
2121 && entry->tlsdesc_entry < (1 << (12 - 1)))
2123 /* lddi @(gr15, tlsdesc_entry), gr8 */
2124 bfd_put_32 (output_bfd,
2125 0x90ccf000 | (entry->tlsdesc_entry
2126 & ((1 << 12) - 1)),
2127 plt_code);
2128 plt_code += 4;
2130 else
2132 if (entry->tlsdesc_entry >= -(1 << (16 - 1))
2133 && entry->tlsdesc_entry < (1 << (16 - 1)))
2135 /* setlos lo(tlsdesc_entry), gr8 */
2136 bfd_put_32 (output_bfd,
2137 0x90fc0000
2138 | (entry->tlsdesc_entry
2139 & (((bfd_vma)1 << 16) - 1)),
2140 plt_code);
2141 plt_code += 4;
2143 else
2145 /* sethi.p hi(tlsdesc_entry), gr8
2146 setlo lo(tlsdesc_entry), gr8 */
2147 bfd_put_32 (output_bfd,
2148 0x10f80000
2149 | ((entry->tlsdesc_entry >> 16)
2150 & (((bfd_vma)1 << 16) - 1)),
2151 plt_code);
2152 plt_code += 4;
2153 bfd_put_32 (output_bfd,
2154 0x90f40000
2155 | (entry->tlsdesc_entry
2156 & (((bfd_vma)1 << 16) - 1)),
2157 plt_code);
2158 plt_code += 4;
2160 /* ldd @(gr15,gr8),gr8 */
2161 bfd_put_32 (output_bfd, 0x9008f148, plt_code);
2162 plt_code += 4;
2164 /* jmpl @(gr8,gr0) */
2165 bfd_put_32 (output_bfd, 0x80308000, plt_code);
2169 return TRUE;
2172 /* Handle an FRV small data reloc. */
2174 static bfd_reloc_status_type
2175 elf32_frv_relocate_gprel12 (info, input_bfd, input_section, relocation,
2176 contents, value)
2177 struct bfd_link_info *info;
2178 bfd *input_bfd;
2179 asection *input_section;
2180 Elf_Internal_Rela *relocation;
2181 bfd_byte *contents;
2182 bfd_vma value;
2184 bfd_vma insn;
2185 bfd_vma gp;
2186 struct bfd_link_hash_entry *h;
2188 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2190 gp = (h->u.def.value
2191 + h->u.def.section->output_section->vma
2192 + h->u.def.section->output_offset);
2194 value -= input_section->output_section->vma;
2195 value -= (gp - input_section->output_section->vma);
2197 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2199 value += relocation->r_addend;
2201 if ((long) value > 0x7ff || (long) value < -0x800)
2202 return bfd_reloc_overflow;
2204 bfd_put_32 (input_bfd,
2205 (insn & 0xfffff000) | (value & 0xfff),
2206 contents + relocation->r_offset);
2208 return bfd_reloc_ok;
2211 /* Handle an FRV small data reloc. for the u12 field. */
2213 static bfd_reloc_status_type
2214 elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, relocation,
2215 contents, value)
2216 struct bfd_link_info *info;
2217 bfd *input_bfd;
2218 asection *input_section;
2219 Elf_Internal_Rela *relocation;
2220 bfd_byte *contents;
2221 bfd_vma value;
2223 bfd_vma insn;
2224 bfd_vma gp;
2225 struct bfd_link_hash_entry *h;
2226 bfd_vma mask;
2228 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2230 gp = (h->u.def.value
2231 + h->u.def.section->output_section->vma
2232 + h->u.def.section->output_offset);
2234 value -= input_section->output_section->vma;
2235 value -= (gp - input_section->output_section->vma);
2237 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2239 value += relocation->r_addend;
2241 if ((long) value > 0x7ff || (long) value < -0x800)
2242 return bfd_reloc_overflow;
2244 /* The high 6 bits go into bits 17-12. The low 6 bits go into bits 5-0. */
2245 mask = 0x3f03f;
2246 insn = (insn & ~mask) | ((value & 0xfc0) << 12) | (value & 0x3f);
2248 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2250 return bfd_reloc_ok;
2253 /* Handle an FRV ELF HI16 reloc. */
2255 static bfd_reloc_status_type
2256 elf32_frv_relocate_hi16 (input_bfd, relhi, contents, value)
2257 bfd *input_bfd;
2258 Elf_Internal_Rela *relhi;
2259 bfd_byte *contents;
2260 bfd_vma value;
2262 bfd_vma insn;
2264 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
2266 value += relhi->r_addend;
2267 value = ((value >> 16) & 0xffff);
2269 insn = (insn & 0xffff0000) | value;
2271 if ((long) value > 0xffff || (long) value < -0x10000)
2272 return bfd_reloc_overflow;
2274 bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
2275 return bfd_reloc_ok;
2278 static bfd_reloc_status_type
2279 elf32_frv_relocate_lo16 (input_bfd, rello, contents, value)
2280 bfd *input_bfd;
2281 Elf_Internal_Rela *rello;
2282 bfd_byte *contents;
2283 bfd_vma value;
2285 bfd_vma insn;
2287 insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
2289 value += rello->r_addend;
2290 value = value & 0xffff;
2292 insn = (insn & 0xffff0000) | value;
2294 if ((long) value > 0xffff || (long) value < -0x10000)
2295 return bfd_reloc_overflow;
2297 bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
2298 return bfd_reloc_ok;
2301 /* Perform the relocation for the CALL label24 instruction. */
2303 static bfd_reloc_status_type
2304 elf32_frv_relocate_label24 (input_bfd, input_section, rello, contents, value)
2305 bfd *input_bfd;
2306 asection *input_section;
2307 Elf_Internal_Rela *rello;
2308 bfd_byte *contents;
2309 bfd_vma value;
2311 bfd_vma insn;
2312 bfd_vma label6;
2313 bfd_vma label18;
2315 /* The format for the call instruction is:
2317 0 000000 0001111 000000000000000000
2318 label6 opcode label18
2320 The branch calculation is: pc + (4*label24)
2321 where label24 is the concatenation of label6 and label18. */
2323 /* Grab the instruction. */
2324 insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
2326 value -= input_section->output_section->vma + input_section->output_offset;
2327 value -= rello->r_offset;
2328 value += rello->r_addend;
2330 value = value >> 2;
2332 label6 = value & 0xfc0000;
2333 label6 = label6 << 7;
2335 label18 = value & 0x3ffff;
2337 insn = insn & 0x803c0000;
2338 insn = insn | label6;
2339 insn = insn | label18;
2341 bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
2343 return bfd_reloc_ok;
2346 static bfd_reloc_status_type
2347 elf32_frv_relocate_gprelhi (info, input_bfd, input_section, relocation,
2348 contents, value)
2349 struct bfd_link_info *info;
2350 bfd *input_bfd;
2351 asection *input_section;
2352 Elf_Internal_Rela *relocation;
2353 bfd_byte *contents;
2354 bfd_vma value;
2356 bfd_vma insn;
2357 bfd_vma gp;
2358 struct bfd_link_hash_entry *h;
2360 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2362 gp = (h->u.def.value
2363 + h->u.def.section->output_section->vma
2364 + h->u.def.section->output_offset);
2366 value -= input_section->output_section->vma;
2367 value -= (gp - input_section->output_section->vma);
2368 value += relocation->r_addend;
2369 value = ((value >> 16) & 0xffff);
2371 if ((long) value > 0xffff || (long) value < -0x10000)
2372 return bfd_reloc_overflow;
2374 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2375 insn = (insn & 0xffff0000) | value;
2377 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2378 return bfd_reloc_ok;
2381 static bfd_reloc_status_type
2382 elf32_frv_relocate_gprello (info, input_bfd, input_section, relocation,
2383 contents, value)
2384 struct bfd_link_info *info;
2385 bfd *input_bfd;
2386 asection *input_section;
2387 Elf_Internal_Rela *relocation;
2388 bfd_byte *contents;
2389 bfd_vma value;
2391 bfd_vma insn;
2392 bfd_vma gp;
2393 struct bfd_link_hash_entry *h;
2395 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2397 gp = (h->u.def.value
2398 + h->u.def.section->output_section->vma
2399 + h->u.def.section->output_offset);
2401 value -= input_section->output_section->vma;
2402 value -= (gp - input_section->output_section->vma);
2403 value += relocation->r_addend;
2404 value = value & 0xffff;
2406 if ((long) value > 0xffff || (long) value < -0x10000)
2407 return bfd_reloc_overflow;
2409 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2410 insn = (insn & 0xffff0000) | value;
2412 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2414 return bfd_reloc_ok;
2417 static reloc_howto_type *
2418 frv_reloc_type_lookup (abfd, code)
2419 bfd *abfd ATTRIBUTE_UNUSED;
2420 bfd_reloc_code_real_type code;
2422 switch (code)
2424 default:
2425 break;
2427 case BFD_RELOC_NONE:
2428 return &elf32_frv_howto_table[ (int) R_FRV_NONE];
2430 case BFD_RELOC_32:
2431 if (elf_elfheader (abfd)->e_type == ET_EXEC
2432 || elf_elfheader (abfd)->e_type == ET_DYN)
2433 return &elf32_frv_rel_32_howto;
2434 /* Fall through. */
2435 case BFD_RELOC_CTOR:
2436 return &elf32_frv_howto_table[ (int) R_FRV_32];
2438 case BFD_RELOC_FRV_LABEL16:
2439 return &elf32_frv_howto_table[ (int) R_FRV_LABEL16];
2441 case BFD_RELOC_FRV_LABEL24:
2442 return &elf32_frv_howto_table[ (int) R_FRV_LABEL24];
2444 case BFD_RELOC_FRV_LO16:
2445 return &elf32_frv_howto_table[ (int) R_FRV_LO16];
2447 case BFD_RELOC_FRV_HI16:
2448 return &elf32_frv_howto_table[ (int) R_FRV_HI16];
2450 case BFD_RELOC_FRV_GPREL12:
2451 return &elf32_frv_howto_table[ (int) R_FRV_GPREL12];
2453 case BFD_RELOC_FRV_GPRELU12:
2454 return &elf32_frv_howto_table[ (int) R_FRV_GPRELU12];
2456 case BFD_RELOC_FRV_GPREL32:
2457 return &elf32_frv_howto_table[ (int) R_FRV_GPREL32];
2459 case BFD_RELOC_FRV_GPRELHI:
2460 return &elf32_frv_howto_table[ (int) R_FRV_GPRELHI];
2462 case BFD_RELOC_FRV_GPRELLO:
2463 return &elf32_frv_howto_table[ (int) R_FRV_GPRELLO];
2465 case BFD_RELOC_FRV_GOT12:
2466 return &elf32_frv_howto_table[ (int) R_FRV_GOT12];
2468 case BFD_RELOC_FRV_GOTHI:
2469 return &elf32_frv_howto_table[ (int) R_FRV_GOTHI];
2471 case BFD_RELOC_FRV_GOTLO:
2472 return &elf32_frv_howto_table[ (int) R_FRV_GOTLO];
2474 case BFD_RELOC_FRV_FUNCDESC:
2475 if (elf_elfheader (abfd)->e_type == ET_EXEC
2476 || elf_elfheader (abfd)->e_type == ET_DYN)
2477 return &elf32_frv_rel_funcdesc_howto;
2478 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC];
2480 case BFD_RELOC_FRV_FUNCDESC_GOT12:
2481 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOT12];
2483 case BFD_RELOC_FRV_FUNCDESC_GOTHI:
2484 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTHI];
2486 case BFD_RELOC_FRV_FUNCDESC_GOTLO:
2487 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTLO];
2489 case BFD_RELOC_FRV_FUNCDESC_VALUE:
2490 if (elf_elfheader (abfd)->e_type == ET_EXEC
2491 || elf_elfheader (abfd)->e_type == ET_DYN)
2492 return &elf32_frv_rel_funcdesc_value_howto;
2493 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_VALUE];
2495 case BFD_RELOC_FRV_FUNCDESC_GOTOFF12:
2496 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFF12];
2498 case BFD_RELOC_FRV_FUNCDESC_GOTOFFHI:
2499 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFHI];
2501 case BFD_RELOC_FRV_FUNCDESC_GOTOFFLO:
2502 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFLO];
2504 case BFD_RELOC_FRV_GOTOFF12:
2505 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFF12];
2507 case BFD_RELOC_FRV_GOTOFFHI:
2508 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFHI];
2510 case BFD_RELOC_FRV_GOTOFFLO:
2511 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFLO];
2513 case BFD_RELOC_FRV_GETTLSOFF:
2514 return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF];
2516 case BFD_RELOC_FRV_TLSDESC_VALUE:
2517 if (elf_elfheader (abfd)->e_type == ET_EXEC
2518 || elf_elfheader (abfd)->e_type == ET_DYN)
2519 return &elf32_frv_rel_tlsdesc_value_howto;
2520 return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_VALUE];
2522 case BFD_RELOC_FRV_GOTTLSDESC12:
2523 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESC12];
2525 case BFD_RELOC_FRV_GOTTLSDESCHI:
2526 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCHI];
2528 case BFD_RELOC_FRV_GOTTLSDESCLO:
2529 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCLO];
2531 case BFD_RELOC_FRV_TLSMOFF12:
2532 return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF12];
2534 case BFD_RELOC_FRV_TLSMOFFHI:
2535 return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFHI];
2537 case BFD_RELOC_FRV_TLSMOFFLO:
2538 return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFLO];
2540 case BFD_RELOC_FRV_GOTTLSOFF12:
2541 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFF12];
2543 case BFD_RELOC_FRV_GOTTLSOFFHI:
2544 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFHI];
2546 case BFD_RELOC_FRV_GOTTLSOFFLO:
2547 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFLO];
2549 case BFD_RELOC_FRV_TLSOFF:
2550 if (elf_elfheader (abfd)->e_type == ET_EXEC
2551 || elf_elfheader (abfd)->e_type == ET_DYN)
2552 return &elf32_frv_rel_tlsoff_howto;
2553 return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF];
2555 case BFD_RELOC_FRV_TLSDESC_RELAX:
2556 return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_RELAX];
2558 case BFD_RELOC_FRV_GETTLSOFF_RELAX:
2559 return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF_RELAX];
2561 case BFD_RELOC_FRV_TLSOFF_RELAX:
2562 return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF_RELAX];
2564 case BFD_RELOC_FRV_TLSMOFF:
2565 return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF];
2567 case BFD_RELOC_VTABLE_INHERIT:
2568 return &elf32_frv_vtinherit_howto;
2570 case BFD_RELOC_VTABLE_ENTRY:
2571 return &elf32_frv_vtentry_howto;
2574 return NULL;
2577 static reloc_howto_type *
2578 frv_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
2580 unsigned int i;
2582 for (i = 0;
2583 i < sizeof (elf32_frv_howto_table) / sizeof (elf32_frv_howto_table[0]);
2584 i++)
2585 if (elf32_frv_howto_table[i].name != NULL
2586 && strcasecmp (elf32_frv_howto_table[i].name, r_name) == 0)
2587 return &elf32_frv_howto_table[i];
2589 if (strcasecmp (elf32_frv_vtinherit_howto.name, r_name) == 0)
2590 return &elf32_frv_vtinherit_howto;
2591 if (strcasecmp (elf32_frv_vtentry_howto.name, r_name) == 0)
2592 return &elf32_frv_vtentry_howto;
2594 return NULL;
2597 /* Set the howto pointer for an FRV ELF reloc. */
2599 static void
2600 frv_info_to_howto_rela (abfd, cache_ptr, dst)
2601 bfd *abfd ATTRIBUTE_UNUSED;
2602 arelent *cache_ptr;
2603 Elf_Internal_Rela *dst;
2605 unsigned int r_type;
2607 r_type = ELF32_R_TYPE (dst->r_info);
2608 switch (r_type)
2610 case R_FRV_GNU_VTINHERIT:
2611 cache_ptr->howto = &elf32_frv_vtinherit_howto;
2612 break;
2614 case R_FRV_GNU_VTENTRY:
2615 cache_ptr->howto = &elf32_frv_vtentry_howto;
2616 break;
2618 default:
2619 cache_ptr->howto = & elf32_frv_howto_table [r_type];
2620 break;
2624 /* Set the howto pointer for an FRV ELF REL reloc. */
2625 static void
2626 frvfdpic_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
2627 arelent *cache_ptr, Elf_Internal_Rela *dst)
2629 unsigned int r_type;
2631 r_type = ELF32_R_TYPE (dst->r_info);
2632 switch (r_type)
2634 case R_FRV_32:
2635 cache_ptr->howto = &elf32_frv_rel_32_howto;
2636 break;
2638 case R_FRV_FUNCDESC:
2639 cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
2640 break;
2642 case R_FRV_FUNCDESC_VALUE:
2643 cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
2644 break;
2646 case R_FRV_TLSDESC_VALUE:
2647 cache_ptr->howto = &elf32_frv_rel_tlsdesc_value_howto;
2648 break;
2650 case R_FRV_TLSOFF:
2651 cache_ptr->howto = &elf32_frv_rel_tlsoff_howto;
2652 break;
2654 default:
2655 cache_ptr->howto = NULL;
2656 break;
2660 /* Perform a single relocation. By default we use the standard BFD
2661 routines, but a few relocs, we have to do them ourselves. */
2663 static bfd_reloc_status_type
2664 frv_final_link_relocate (howto, input_bfd, input_section, contents, rel,
2665 relocation)
2666 reloc_howto_type *howto;
2667 bfd *input_bfd;
2668 asection *input_section;
2669 bfd_byte *contents;
2670 Elf_Internal_Rela *rel;
2671 bfd_vma relocation;
2673 return _bfd_final_link_relocate (howto, input_bfd, input_section,
2674 contents, rel->r_offset, relocation,
2675 rel->r_addend);
2679 /* Relocate an FRV ELF section.
2681 The RELOCATE_SECTION function is called by the new ELF backend linker
2682 to handle the relocations for a section.
2684 The relocs are always passed as Rela structures; if the section
2685 actually uses Rel structures, the r_addend field will always be
2686 zero.
2688 This function is responsible for adjusting the section contents as
2689 necessary, and (if using Rela relocs and generating a relocatable
2690 output file) adjusting the reloc addend as necessary.
2692 This function does not have to worry about setting the reloc
2693 address or the reloc symbol index.
2695 LOCAL_SYMS is a pointer to the swapped in local symbols.
2697 LOCAL_SECTIONS is an array giving the section in the input file
2698 corresponding to the st_shndx field of each local symbol.
2700 The global hash table entry for the global symbols can be found
2701 via elf_sym_hashes (input_bfd).
2703 When generating relocatable output, this function must handle
2704 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2705 going to be the section symbol corresponding to the output
2706 section, which means that the addend must be adjusted
2707 accordingly. */
2709 static bfd_boolean
2710 elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
2711 contents, relocs, local_syms, local_sections)
2712 bfd *output_bfd ATTRIBUTE_UNUSED;
2713 struct bfd_link_info *info;
2714 bfd *input_bfd;
2715 asection *input_section;
2716 bfd_byte *contents;
2717 Elf_Internal_Rela *relocs;
2718 Elf_Internal_Sym *local_syms;
2719 asection **local_sections;
2721 Elf_Internal_Shdr *symtab_hdr;
2722 struct elf_link_hash_entry **sym_hashes;
2723 Elf_Internal_Rela *rel;
2724 Elf_Internal_Rela *relend;
2725 unsigned isec_segment, got_segment, plt_segment, gprel_segment, tls_segment,
2726 check_segment[2];
2727 int silence_segment_error = !(info->shared || info->pie);
2728 unsigned long insn;
2730 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2731 sym_hashes = elf_sym_hashes (input_bfd);
2732 relend = relocs + input_section->reloc_count;
2734 isec_segment = _frvfdpic_osec_to_segment (output_bfd,
2735 input_section->output_section);
2736 if (IS_FDPIC (output_bfd) && frvfdpic_got_section (info))
2737 got_segment = _frvfdpic_osec_to_segment (output_bfd,
2738 frvfdpic_got_section (info)
2739 ->output_section);
2740 else
2741 got_segment = -1;
2742 if (IS_FDPIC (output_bfd) && frvfdpic_gotfixup_section (info))
2743 gprel_segment = _frvfdpic_osec_to_segment (output_bfd,
2744 frvfdpic_gotfixup_section (info)
2745 ->output_section);
2746 else
2747 gprel_segment = -1;
2748 if (IS_FDPIC (output_bfd) && frvfdpic_plt_section (info))
2749 plt_segment = _frvfdpic_osec_to_segment (output_bfd,
2750 frvfdpic_plt_section (info)
2751 ->output_section);
2752 else
2753 plt_segment = -1;
2754 if (elf_hash_table (info)->tls_sec)
2755 tls_segment = _frvfdpic_osec_to_segment (output_bfd,
2756 elf_hash_table (info)->tls_sec);
2757 else
2758 tls_segment = -1;
2760 for (rel = relocs; rel < relend; rel ++)
2762 reloc_howto_type *howto;
2763 unsigned long r_symndx;
2764 Elf_Internal_Sym *sym;
2765 asection *sec;
2766 struct elf_link_hash_entry *h;
2767 bfd_vma relocation;
2768 bfd_reloc_status_type r;
2769 const char * name = NULL;
2770 int r_type;
2771 asection *osec;
2772 struct frvfdpic_relocs_info *picrel;
2773 bfd_vma orig_addend = rel->r_addend;
2775 r_type = ELF32_R_TYPE (rel->r_info);
2777 if ( r_type == R_FRV_GNU_VTINHERIT
2778 || r_type == R_FRV_GNU_VTENTRY)
2779 continue;
2781 r_symndx = ELF32_R_SYM (rel->r_info);
2782 howto = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
2783 h = NULL;
2784 sym = NULL;
2785 sec = NULL;
2787 if (r_symndx < symtab_hdr->sh_info)
2789 sym = local_syms + r_symndx;
2790 osec = sec = local_sections [r_symndx];
2791 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2793 name = bfd_elf_string_from_elf_section
2794 (input_bfd, symtab_hdr->sh_link, sym->st_name);
2795 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
2797 else
2799 bfd_boolean warned;
2800 bfd_boolean unresolved_reloc;
2802 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2803 r_symndx, symtab_hdr, sym_hashes,
2804 h, sec, relocation,
2805 unresolved_reloc, warned);
2806 osec = sec;
2809 if (sec != NULL && elf_discarded_section (sec))
2811 /* For relocs against symbols from removed linkonce sections,
2812 or sections discarded by a linker script, we just want the
2813 section contents zeroed. Avoid any special processing. */
2814 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2815 rel->r_info = 0;
2816 rel->r_addend = 0;
2817 continue;
2820 if (info->relocatable)
2821 continue;
2823 if (r_type != R_FRV_TLSMOFF
2824 && h != NULL
2825 && (h->root.type == bfd_link_hash_defined
2826 || h->root.type == bfd_link_hash_defweak)
2827 && !FRVFDPIC_SYM_LOCAL (info, h))
2829 osec = sec = NULL;
2830 relocation = 0;
2833 switch (r_type)
2835 case R_FRV_LABEL24:
2836 case R_FRV_32:
2837 if (! IS_FDPIC (output_bfd))
2838 goto non_fdpic;
2840 case R_FRV_GOT12:
2841 case R_FRV_GOTHI:
2842 case R_FRV_GOTLO:
2843 case R_FRV_FUNCDESC_GOT12:
2844 case R_FRV_FUNCDESC_GOTHI:
2845 case R_FRV_FUNCDESC_GOTLO:
2846 case R_FRV_GOTOFF12:
2847 case R_FRV_GOTOFFHI:
2848 case R_FRV_GOTOFFLO:
2849 case R_FRV_FUNCDESC_GOTOFF12:
2850 case R_FRV_FUNCDESC_GOTOFFHI:
2851 case R_FRV_FUNCDESC_GOTOFFLO:
2852 case R_FRV_FUNCDESC:
2853 case R_FRV_FUNCDESC_VALUE:
2854 case R_FRV_GETTLSOFF:
2855 case R_FRV_TLSDESC_VALUE:
2856 case R_FRV_GOTTLSDESC12:
2857 case R_FRV_GOTTLSDESCHI:
2858 case R_FRV_GOTTLSDESCLO:
2859 case R_FRV_TLSMOFF12:
2860 case R_FRV_TLSMOFFHI:
2861 case R_FRV_TLSMOFFLO:
2862 case R_FRV_GOTTLSOFF12:
2863 case R_FRV_GOTTLSOFFHI:
2864 case R_FRV_GOTTLSOFFLO:
2865 case R_FRV_TLSOFF:
2866 case R_FRV_TLSDESC_RELAX:
2867 case R_FRV_GETTLSOFF_RELAX:
2868 case R_FRV_TLSOFF_RELAX:
2869 case R_FRV_TLSMOFF:
2870 if (h != NULL)
2871 picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info
2872 (info), input_bfd, h,
2873 orig_addend, INSERT);
2874 else
2875 /* In order to find the entry we created before, we must
2876 use the original addend, not the one that may have been
2877 modified by _bfd_elf_rela_local_sym(). */
2878 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
2879 (info), input_bfd, r_symndx,
2880 orig_addend, INSERT);
2881 if (! picrel)
2882 return FALSE;
2884 if (!_frvfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2885 osec, sym,
2886 rel->r_addend))
2888 (*_bfd_error_handler)
2889 (_("%B(%A+0x%x): relocation to `%s+%x' may have caused the error above"),
2890 input_bfd, input_section, rel->r_offset, name, rel->r_addend);
2891 return FALSE;
2894 break;
2896 default:
2897 non_fdpic:
2898 picrel = NULL;
2899 if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
2901 info->callbacks->warning
2902 (info, _("relocation references symbol not defined in the module"),
2903 name, input_bfd, input_section, rel->r_offset);
2904 return FALSE;
2906 break;
2909 switch (r_type)
2911 case R_FRV_GETTLSOFF:
2912 case R_FRV_TLSDESC_VALUE:
2913 case R_FRV_GOTTLSDESC12:
2914 case R_FRV_GOTTLSDESCHI:
2915 case R_FRV_GOTTLSDESCLO:
2916 case R_FRV_TLSMOFF12:
2917 case R_FRV_TLSMOFFHI:
2918 case R_FRV_TLSMOFFLO:
2919 case R_FRV_GOTTLSOFF12:
2920 case R_FRV_GOTTLSOFFHI:
2921 case R_FRV_GOTTLSOFFLO:
2922 case R_FRV_TLSOFF:
2923 case R_FRV_TLSDESC_RELAX:
2924 case R_FRV_GETTLSOFF_RELAX:
2925 case R_FRV_TLSOFF_RELAX:
2926 case R_FRV_TLSMOFF:
2927 if (sec && (bfd_is_abs_section (sec) || bfd_is_und_section (sec)))
2928 relocation += tls_biased_base (info);
2929 break;
2931 default:
2932 break;
2935 /* Try to apply TLS relaxations. */
2936 if (1)
2937 switch (r_type)
2940 #define LOCAL_EXEC_P(info, picrel) \
2941 ((info)->executable \
2942 && (picrel->symndx != -1 || FRVFDPIC_SYM_LOCAL ((info), (picrel)->d.h)))
2943 #define INITIAL_EXEC_P(info, picrel) \
2944 (((info)->executable || (info)->flags & DF_STATIC_TLS) \
2945 && (picrel)->tlsoff_entry)
2947 #define IN_RANGE_FOR_OFST12_P(value) \
2948 ((bfd_vma)((value) + 2048) < (bfd_vma)4096)
2949 #define IN_RANGE_FOR_SETLOS_P(value) \
2950 ((bfd_vma)((value) + 32768) < (bfd_vma)65536)
2951 #define TLSMOFF_IN_RANGE_FOR_SETLOS_P(value, info) \
2952 (IN_RANGE_FOR_SETLOS_P ((value) - tls_biased_base (info)))
2954 #define RELAX_GETTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2955 (LOCAL_EXEC_P ((info), (picrel)) \
2956 && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2957 #define RELAX_GETTLSOFF_INITIAL_EXEC_P(info, picrel) \
2958 (INITIAL_EXEC_P ((info), (picrel)) \
2959 && IN_RANGE_FOR_OFST12_P ((picrel)->tlsoff_entry))
2961 #define RELAX_TLSDESC_LOCAL_EXEC_P(info, picrel, value) \
2962 (LOCAL_EXEC_P ((info), (picrel)))
2963 #define RELAX_TLSDESC_INITIAL_EXEC_P(info, picrel) \
2964 (INITIAL_EXEC_P ((info), (picrel)))
2966 #define RELAX_GOTTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2967 (LOCAL_EXEC_P ((info), (picrel)) \
2968 && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2970 case R_FRV_GETTLSOFF:
2971 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2973 /* Is this a call instruction? */
2974 if ((insn & (unsigned long)0x01fc0000) != 0x003c0000)
2976 r = info->callbacks->warning
2977 (info,
2978 _("R_FRV_GETTLSOFF not applied to a call instruction"),
2979 name, input_bfd, input_section, rel->r_offset);
2980 return FALSE;
2983 if (RELAX_GETTLSOFF_LOCAL_EXEC_P (info, picrel,
2984 relocation + rel->r_addend))
2986 /* Replace the call instruction (except the packing bit)
2987 with setlos #tlsmofflo(symbol+offset), gr9. */
2988 insn &= (unsigned long)0x80000000;
2989 insn |= (unsigned long)0x12fc0000;
2990 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2992 r_type = R_FRV_TLSMOFFLO;
2993 howto = elf32_frv_howto_table + r_type;
2994 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2997 else if (RELAX_GETTLSOFF_INITIAL_EXEC_P (info, picrel))
2999 /* Replace the call instruction (except the packing bit)
3000 with ldi @(gr15, #gottlsoff12(symbol+addend)), gr9. */
3001 insn &= (unsigned long)0x80000000;
3002 insn |= (unsigned long)0x12c8f000;
3003 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3005 r_type = R_FRV_GOTTLSOFF12;
3006 howto = elf32_frv_howto_table + r_type;
3007 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3010 break;
3012 case R_FRV_GOTTLSDESC12:
3013 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3015 /* Is this an lddi instruction? */
3016 if ((insn & (unsigned long)0x01fc0000) != 0x00cc0000)
3018 r = info->callbacks->warning
3019 (info,
3020 _("R_FRV_GOTTLSDESC12 not applied to an lddi instruction"),
3021 name, input_bfd, input_section, rel->r_offset);
3022 return FALSE;
3025 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3026 relocation + rel->r_addend)
3027 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3028 info))
3030 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3031 with setlos #tlsmofflo(symbol+offset), gr<C+1>.
3032 Preserve the packing bit. */
3033 insn = (insn & (unsigned long)0x80000000)
3034 | ((insn + (unsigned long)0x02000000)
3035 & (unsigned long)0x7e000000);
3036 insn |= (unsigned long)0x00fc0000;
3037 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3039 r_type = R_FRV_TLSMOFFLO;
3040 howto = elf32_frv_howto_table + r_type;
3041 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3044 else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3045 relocation + rel->r_addend))
3047 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3048 with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
3049 Preserve the packing bit. */
3050 insn = (insn & (unsigned long)0x80000000)
3051 | ((insn + (unsigned long)0x02000000)
3052 & (unsigned long)0x7e000000);
3053 insn |= (unsigned long)0x00f80000;
3054 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3056 r_type = R_FRV_TLSMOFFHI;
3057 howto = elf32_frv_howto_table + r_type;
3058 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3061 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3063 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3064 with ldi @(grB, #gottlsoff12(symbol+offset),
3065 gr<C+1>. Preserve the packing bit. If gottlsoff12
3066 overflows, we'll error out, but that's sort-of ok,
3067 since we'd started with gottlsdesc12, that's actually
3068 more demanding. Compiling with -fPIE instead of
3069 -fpie would fix it; linking with --relax should fix
3070 it as well. */
3071 insn = (insn & (unsigned long)0x80cbf000)
3072 | ((insn + (unsigned long)0x02000000)
3073 & (unsigned long)0x7e000000);
3074 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3076 r_type = R_FRV_GOTTLSOFF12;
3077 howto = elf32_frv_howto_table + r_type;
3078 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3081 break;
3083 case R_FRV_GOTTLSDESCHI:
3084 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3086 /* Is this a sethi instruction? */
3087 if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3089 r = info->callbacks->warning
3090 (info,
3091 _("R_FRV_GOTTLSDESCHI not applied to a sethi instruction"),
3092 name, input_bfd, input_section, rel->r_offset);
3093 return FALSE;
3096 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3097 relocation + rel->r_addend)
3098 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3099 && IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry)))
3101 /* Replace sethi with a nop. Preserve the packing bit. */
3102 insn &= (unsigned long)0x80000000;
3103 insn |= (unsigned long)0x00880000;
3104 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3106 /* Nothing to relocate. */
3107 continue;
3110 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3112 /* Simply decay GOTTLSDESC to GOTTLSOFF. */
3113 r_type = R_FRV_GOTTLSOFFHI;
3114 howto = elf32_frv_howto_table + r_type;
3115 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3118 break;
3120 case R_FRV_GOTTLSDESCLO:
3121 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3123 /* Is this a setlo or setlos instruction? */
3124 if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3126 r = info->callbacks->warning
3127 (info,
3128 _("R_FRV_GOTTLSDESCLO"
3129 " not applied to a setlo or setlos instruction"),
3130 name, input_bfd, input_section, rel->r_offset);
3131 return FALSE;
3134 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3135 relocation + rel->r_addend)
3136 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3137 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3139 /* Replace setlo/setlos with a nop. Preserve the
3140 packing bit. */
3141 insn &= (unsigned long)0x80000000;
3142 insn |= (unsigned long)0x00880000;
3143 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3145 /* Nothing to relocate. */
3146 continue;
3149 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3151 /* If the corresponding sethi (if it exists) decayed
3152 to a nop, make sure this becomes (or already is) a
3153 setlos, not setlo. */
3154 if (IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry))
3156 insn |= (unsigned long)0x00080000;
3157 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3160 /* Simply decay GOTTLSDESC to GOTTLSOFF. */
3161 r_type = R_FRV_GOTTLSOFFLO;
3162 howto = elf32_frv_howto_table + r_type;
3163 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3166 break;
3168 case R_FRV_TLSDESC_RELAX:
3169 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3171 /* Is this an ldd instruction? */
3172 if ((insn & (unsigned long)0x01fc0fc0) != 0x00080140)
3174 r = info->callbacks->warning
3175 (info,
3176 _("R_FRV_TLSDESC_RELAX not applied to an ldd instruction"),
3177 name, input_bfd, input_section, rel->r_offset);
3178 return FALSE;
3181 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3182 relocation + rel->r_addend)
3183 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3184 info))
3186 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3187 with setlos #tlsmofflo(symbol+offset), gr<C+1>.
3188 Preserve the packing bit. */
3189 insn = (insn & (unsigned long)0x80000000)
3190 | ((insn + (unsigned long)0x02000000)
3191 & (unsigned long)0x7e000000);
3192 insn |= (unsigned long)0x00fc0000;
3193 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3195 r_type = R_FRV_TLSMOFFLO;
3196 howto = elf32_frv_howto_table + r_type;
3197 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3200 else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3201 relocation + rel->r_addend))
3203 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3204 with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
3205 Preserve the packing bit. */
3206 insn = (insn & (unsigned long)0x80000000)
3207 | ((insn + (unsigned long)0x02000000)
3208 & (unsigned long)0x7e000000);
3209 insn |= (unsigned long)0x00f80000;
3210 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3212 r_type = R_FRV_TLSMOFFHI;
3213 howto = elf32_frv_howto_table + r_type;
3214 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3217 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3218 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3220 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3221 with ldi @(grB, #gottlsoff12(symbol+offset), gr<C+1>.
3222 Preserve the packing bit. */
3223 insn = (insn & (unsigned long)0x8003f000)
3224 | (unsigned long)0x00c80000
3225 | ((insn + (unsigned long)0x02000000)
3226 & (unsigned long)0x7e000000);
3227 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3229 r_type = R_FRV_GOTTLSOFF12;
3230 howto = elf32_frv_howto_table + r_type;
3231 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3234 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3236 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3237 with ld #tlsoff(symbol+offset)@(grB, grA), gr<C+1>.
3238 Preserve the packing bit. */
3239 insn = (insn & (unsigned long)0x81ffffbf)
3240 | ((insn + (unsigned long)0x02000000)
3241 & (unsigned long)0x7e000000);
3242 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3244 /* #tlsoff(symbol+offset) is just a relaxation
3245 annotation, so there's nothing left to
3246 relocate. */
3247 continue;
3250 break;
3252 case R_FRV_GETTLSOFF_RELAX:
3253 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3255 /* Is this a calll or callil instruction? */
3256 if ((insn & (unsigned long)0x7ff80fc0) != 0x02300000)
3258 r = info->callbacks->warning
3259 (info,
3260 _("R_FRV_GETTLSOFF_RELAX"
3261 " not applied to a calll instruction"),
3262 name, input_bfd, input_section, rel->r_offset);
3263 return FALSE;
3266 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3267 relocation + rel->r_addend)
3268 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3269 info))
3271 /* Replace calll with a nop. Preserve the packing bit. */
3272 insn &= (unsigned long)0x80000000;
3273 insn |= (unsigned long)0x00880000;
3274 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3276 /* Nothing to relocate. */
3277 continue;
3280 else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3281 relocation + rel->r_addend))
3283 /* Replace calll with setlo #tlsmofflo(symbol+offset), gr9.
3284 Preserve the packing bit. */
3285 insn &= (unsigned long)0x80000000;
3286 insn |= (unsigned long)0x12f40000;
3287 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3289 r_type = R_FRV_TLSMOFFLO;
3290 howto = elf32_frv_howto_table + r_type;
3291 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3294 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3296 /* Replace calll with a nop. Preserve the packing bit. */
3297 insn &= (unsigned long)0x80000000;
3298 insn |= (unsigned long)0x00880000;
3299 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3301 /* Nothing to relocate. */
3302 continue;
3305 break;
3307 case R_FRV_GOTTLSOFF12:
3308 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3310 /* Is this an ldi instruction? */
3311 if ((insn & (unsigned long)0x01fc0000) != 0x00c80000)
3313 r = info->callbacks->warning
3314 (info,
3315 _("R_FRV_GOTTLSOFF12 not applied to an ldi instruction"),
3316 name, input_bfd, input_section, rel->r_offset);
3317 return FALSE;
3320 if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3321 relocation + rel->r_addend))
3323 /* Replace ldi @(grB, #gottlsoff12(symbol+offset), grC
3324 with setlos #tlsmofflo(symbol+offset), grC.
3325 Preserve the packing bit. */
3326 insn &= (unsigned long)0xfe000000;
3327 insn |= (unsigned long)0x00fc0000;
3328 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3330 r_type = R_FRV_TLSMOFFLO;
3331 howto = elf32_frv_howto_table + r_type;
3332 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3335 break;
3337 case R_FRV_GOTTLSOFFHI:
3338 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3340 /* Is this a sethi instruction? */
3341 if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3343 r = info->callbacks->warning
3344 (info,
3345 _("R_FRV_GOTTLSOFFHI not applied to a sethi instruction"),
3346 name, input_bfd, input_section, rel->r_offset);
3347 return FALSE;
3350 if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3351 relocation + rel->r_addend)
3352 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3353 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3355 /* Replace sethi with a nop. Preserve the packing bit. */
3356 insn &= (unsigned long)0x80000000;
3357 insn |= (unsigned long)0x00880000;
3358 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3360 /* Nothing to relocate. */
3361 continue;
3364 break;
3366 case R_FRV_GOTTLSOFFLO:
3367 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3369 /* Is this a setlo or setlos instruction? */
3370 if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3372 r = info->callbacks->warning
3373 (info,
3374 _("R_FRV_GOTTLSOFFLO"
3375 " not applied to a setlo or setlos instruction"),
3376 name, input_bfd, input_section, rel->r_offset);
3377 return FALSE;
3380 if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3381 relocation + rel->r_addend)
3382 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3383 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3385 /* Replace setlo/setlos with a nop. Preserve the
3386 packing bit. */
3387 insn &= (unsigned long)0x80000000;
3388 insn |= (unsigned long)0x00880000;
3389 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3391 /* Nothing to relocate. */
3392 continue;
3395 break;
3397 case R_FRV_TLSOFF_RELAX:
3398 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3400 /* Is this an ld instruction? */
3401 if ((insn & (unsigned long)0x01fc0fc0) != 0x00080100)
3403 r = info->callbacks->warning
3404 (info,
3405 _("R_FRV_TLSOFF_RELAX not applied to an ld instruction"),
3406 name, input_bfd, input_section, rel->r_offset);
3407 return FALSE;
3410 if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3411 relocation + rel->r_addend))
3413 /* Replace ld #gottlsoff(symbol+offset)@(grB, grA), grC
3414 with setlos #tlsmofflo(symbol+offset), grC.
3415 Preserve the packing bit. */
3416 insn &= (unsigned long)0xfe000000;
3417 insn |= (unsigned long)0x00fc0000;
3418 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3420 r_type = R_FRV_TLSMOFFLO;
3421 howto = elf32_frv_howto_table + r_type;
3422 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3425 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3426 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3428 /* Replace ld #tlsoff(symbol+offset)@(grB, grA), grC
3429 with ldi @(grB, #gottlsoff12(symbol+offset), grC.
3430 Preserve the packing bit. */
3431 insn = (insn & (unsigned long)0xfe03f000)
3432 | (unsigned long)0x00c80000;;
3433 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3435 r_type = R_FRV_GOTTLSOFF12;
3436 howto = elf32_frv_howto_table + r_type;
3437 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3440 break;
3442 case R_FRV_TLSMOFFHI:
3443 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3445 /* Is this a sethi instruction? */
3446 if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3448 r = info->callbacks->warning
3449 (info,
3450 _("R_FRV_TLSMOFFHI not applied to a sethi instruction"),
3451 name, input_bfd, input_section, rel->r_offset);
3452 return FALSE;
3455 if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3456 info))
3458 /* Replace sethi with a nop. Preserve the packing bit. */
3459 insn &= (unsigned long)0x80000000;
3460 insn |= (unsigned long)0x00880000;
3461 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3463 /* Nothing to relocate. */
3464 continue;
3467 break;
3469 case R_FRV_TLSMOFFLO:
3470 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3472 /* Is this a setlo or setlos instruction? */
3473 if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3475 r = info->callbacks->warning
3476 (info,
3477 _("R_FRV_TLSMOFFLO"
3478 " not applied to a setlo or setlos instruction"),
3479 name, input_bfd, input_section, rel->r_offset);
3480 return FALSE;
3483 if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3484 info))
3485 /* If the corresponding sethi (if it exists) decayed
3486 to a nop, make sure this becomes (or already is) a
3487 setlos, not setlo. */
3489 insn |= (unsigned long)0x00080000;
3490 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3493 break;
3496 There's nothing to relax in these:
3497 R_FRV_TLSDESC_VALUE
3498 R_FRV_TLSOFF
3499 R_FRV_TLSMOFF12
3500 R_FRV_TLSMOFFHI
3501 R_FRV_TLSMOFFLO
3502 R_FRV_TLSMOFF
3505 default:
3506 break;
3509 switch (r_type)
3511 case R_FRV_LABEL24:
3512 check_segment[0] = isec_segment;
3513 if (! IS_FDPIC (output_bfd))
3514 check_segment[1] = isec_segment;
3515 else if (picrel->plt)
3517 relocation = frvfdpic_plt_section (info)->output_section->vma
3518 + frvfdpic_plt_section (info)->output_offset
3519 + picrel->plt_entry;
3520 check_segment[1] = plt_segment;
3522 /* We don't want to warn on calls to undefined weak symbols,
3523 as calls to them must be protected by non-NULL tests
3524 anyway, and unprotected calls would invoke undefined
3525 behavior. */
3526 else if (picrel->symndx == -1
3527 && picrel->d.h->root.type == bfd_link_hash_undefweak)
3528 check_segment[1] = check_segment[0];
3529 else
3530 check_segment[1] = sec
3531 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3532 : (unsigned)-1;
3533 break;
3535 case R_FRV_GOT12:
3536 case R_FRV_GOTHI:
3537 case R_FRV_GOTLO:
3538 relocation = picrel->got_entry;
3539 check_segment[0] = check_segment[1] = got_segment;
3540 break;
3542 case R_FRV_FUNCDESC_GOT12:
3543 case R_FRV_FUNCDESC_GOTHI:
3544 case R_FRV_FUNCDESC_GOTLO:
3545 relocation = picrel->fdgot_entry;
3546 check_segment[0] = check_segment[1] = got_segment;
3547 break;
3549 case R_FRV_GOTOFFHI:
3550 case R_FRV_GOTOFF12:
3551 case R_FRV_GOTOFFLO:
3552 relocation -= frvfdpic_got_section (info)->output_section->vma
3553 + frvfdpic_got_section (info)->output_offset
3554 + frvfdpic_got_initial_offset (info);
3555 check_segment[0] = got_segment;
3556 check_segment[1] = sec
3557 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3558 : (unsigned)-1;
3559 break;
3561 case R_FRV_FUNCDESC_GOTOFF12:
3562 case R_FRV_FUNCDESC_GOTOFFHI:
3563 case R_FRV_FUNCDESC_GOTOFFLO:
3564 relocation = picrel->fd_entry;
3565 check_segment[0] = check_segment[1] = got_segment;
3566 break;
3568 case R_FRV_FUNCDESC:
3570 int dynindx;
3571 bfd_vma addend = rel->r_addend;
3573 if (! (h && h->root.type == bfd_link_hash_undefweak
3574 && FRVFDPIC_SYM_LOCAL (info, h)))
3576 /* If the symbol is dynamic and there may be dynamic
3577 symbol resolution because we are or are linked with a
3578 shared library, emit a FUNCDESC relocation such that
3579 the dynamic linker will allocate the function
3580 descriptor. If the symbol needs a non-local function
3581 descriptor but binds locally (e.g., its visibility is
3582 protected, emit a dynamic relocation decayed to
3583 section+offset. */
3584 if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h)
3585 && FRVFDPIC_SYM_LOCAL (info, h)
3586 && !(info->executable && !info->pie))
3588 dynindx = elf_section_data (h->root.u.def.section
3589 ->output_section)->dynindx;
3590 addend += h->root.u.def.section->output_offset
3591 + h->root.u.def.value;
3593 else if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h))
3595 if (addend)
3597 info->callbacks->warning
3598 (info, _("R_FRV_FUNCDESC references dynamic symbol with nonzero addend"),
3599 name, input_bfd, input_section, rel->r_offset);
3600 return FALSE;
3602 dynindx = h->dynindx;
3604 else
3606 /* Otherwise, we know we have a private function
3607 descriptor, so reference it directly. */
3608 BFD_ASSERT (picrel->privfd);
3609 r_type = R_FRV_32;
3610 dynindx = elf_section_data (frvfdpic_got_section (info)
3611 ->output_section)->dynindx;
3612 addend = frvfdpic_got_section (info)->output_offset
3613 + frvfdpic_got_initial_offset (info)
3614 + picrel->fd_entry;
3617 /* If there is room for dynamic symbol resolution, emit
3618 the dynamic relocation. However, if we're linking an
3619 executable at a fixed location, we won't have emitted a
3620 dynamic symbol entry for the got section, so idx will
3621 be zero, which means we can and should compute the
3622 address of the private descriptor ourselves. */
3623 if (info->executable && !info->pie
3624 && (!h || FRVFDPIC_FUNCDESC_LOCAL (info, h)))
3626 addend += frvfdpic_got_section (info)->output_section->vma;
3627 if ((bfd_get_section_flags (output_bfd,
3628 input_section->output_section)
3629 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3631 bfd_vma offset;
3633 if (_frvfdpic_osec_readonly_p (output_bfd,
3634 input_section
3635 ->output_section))
3637 info->callbacks->warning
3638 (info,
3639 _("cannot emit fixups in read-only section"),
3640 name, input_bfd, input_section, rel->r_offset);
3641 return FALSE;
3644 offset = _bfd_elf_section_offset
3645 (output_bfd, info,
3646 input_section, rel->r_offset);
3648 if (offset != (bfd_vma)-1)
3649 _frvfdpic_add_rofixup (output_bfd,
3650 frvfdpic_gotfixup_section
3651 (info),
3652 offset + input_section
3653 ->output_section->vma
3654 + input_section->output_offset,
3655 picrel);
3658 else if ((bfd_get_section_flags (output_bfd,
3659 input_section->output_section)
3660 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3662 bfd_vma offset;
3664 if (_frvfdpic_osec_readonly_p (output_bfd,
3665 input_section
3666 ->output_section))
3668 info->callbacks->warning
3669 (info,
3670 _("cannot emit dynamic relocations in read-only section"),
3671 name, input_bfd, input_section, rel->r_offset);
3672 return FALSE;
3675 offset = _bfd_elf_section_offset
3676 (output_bfd, info,
3677 input_section, rel->r_offset);
3679 if (offset != (bfd_vma)-1)
3680 _frvfdpic_add_dyn_reloc (output_bfd,
3681 frvfdpic_gotrel_section (info),
3682 offset + input_section
3683 ->output_section->vma
3684 + input_section->output_offset,
3685 r_type, dynindx, addend, picrel);
3687 else
3688 addend += frvfdpic_got_section (info)->output_section->vma;
3691 /* We want the addend in-place because dynamic
3692 relocations are REL. Setting relocation to it should
3693 arrange for it to be installed. */
3694 relocation = addend - rel->r_addend;
3696 check_segment[0] = check_segment[1] = got_segment;
3697 break;
3699 case R_FRV_32:
3700 if (! IS_FDPIC (output_bfd))
3702 check_segment[0] = check_segment[1] = -1;
3703 break;
3705 /* Fall through. */
3706 case R_FRV_FUNCDESC_VALUE:
3708 int dynindx;
3709 bfd_vma addend = rel->r_addend;
3711 /* If the symbol is dynamic but binds locally, use
3712 section+offset. */
3713 if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
3715 if (addend && r_type == R_FRV_FUNCDESC_VALUE)
3717 info->callbacks->warning
3718 (info, _("R_FRV_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
3719 name, input_bfd, input_section, rel->r_offset);
3720 return FALSE;
3722 dynindx = h->dynindx;
3724 else
3726 if (h)
3727 addend += h->root.u.def.value;
3728 else
3729 addend += sym->st_value;
3730 if (osec)
3731 addend += osec->output_offset;
3732 if (osec && osec->output_section
3733 && ! bfd_is_abs_section (osec->output_section)
3734 && ! bfd_is_und_section (osec->output_section))
3735 dynindx = elf_section_data (osec->output_section)->dynindx;
3736 else
3737 dynindx = 0;
3740 /* If we're linking an executable at a fixed address, we
3741 can omit the dynamic relocation as long as the symbol
3742 is defined in the current link unit (which is implied
3743 by its output section not being NULL). */
3744 if (info->executable && !info->pie
3745 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3747 if (osec)
3748 addend += osec->output_section->vma;
3749 if (IS_FDPIC (input_bfd)
3750 && (bfd_get_section_flags (output_bfd,
3751 input_section->output_section)
3752 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3754 if (_frvfdpic_osec_readonly_p (output_bfd,
3755 input_section
3756 ->output_section))
3758 info->callbacks->warning
3759 (info,
3760 _("cannot emit fixups in read-only section"),
3761 name, input_bfd, input_section, rel->r_offset);
3762 return FALSE;
3764 if (!h || h->root.type != bfd_link_hash_undefweak)
3766 bfd_vma offset = _bfd_elf_section_offset
3767 (output_bfd, info,
3768 input_section, rel->r_offset);
3770 if (offset != (bfd_vma)-1)
3772 _frvfdpic_add_rofixup (output_bfd,
3773 frvfdpic_gotfixup_section
3774 (info),
3775 offset + input_section
3776 ->output_section->vma
3777 + input_section->output_offset,
3778 picrel);
3779 if (r_type == R_FRV_FUNCDESC_VALUE)
3780 _frvfdpic_add_rofixup
3781 (output_bfd,
3782 frvfdpic_gotfixup_section (info),
3783 offset
3784 + input_section->output_section->vma
3785 + input_section->output_offset + 4, picrel);
3790 else
3792 if ((bfd_get_section_flags (output_bfd,
3793 input_section->output_section)
3794 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3796 bfd_vma offset;
3798 if (_frvfdpic_osec_readonly_p (output_bfd,
3799 input_section
3800 ->output_section))
3802 info->callbacks->warning
3803 (info,
3804 _("cannot emit dynamic relocations in read-only section"),
3805 name, input_bfd, input_section, rel->r_offset);
3806 return FALSE;
3809 offset = _bfd_elf_section_offset
3810 (output_bfd, info,
3811 input_section, rel->r_offset);
3813 if (offset != (bfd_vma)-1)
3814 _frvfdpic_add_dyn_reloc (output_bfd,
3815 frvfdpic_gotrel_section (info),
3816 offset + input_section
3817 ->output_section->vma
3818 + input_section->output_offset,
3819 r_type, dynindx, addend, picrel);
3821 else if (osec)
3822 addend += osec->output_section->vma;
3823 /* We want the addend in-place because dynamic
3824 relocations are REL. Setting relocation to it
3825 should arrange for it to be installed. */
3826 relocation = addend - rel->r_addend;
3829 if (r_type == R_FRV_FUNCDESC_VALUE)
3831 /* If we've omitted the dynamic relocation, just emit
3832 the fixed addresses of the symbol and of the local
3833 GOT base offset. */
3834 if (info->executable && !info->pie
3835 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3836 bfd_put_32 (output_bfd,
3837 frvfdpic_got_section (info)->output_section->vma
3838 + frvfdpic_got_section (info)->output_offset
3839 + frvfdpic_got_initial_offset (info),
3840 contents + rel->r_offset + 4);
3841 else
3842 /* A function descriptor used for lazy or local
3843 resolving is initialized such that its high word
3844 contains the output section index in which the
3845 PLT entries are located, and the low word
3846 contains the offset of the lazy PLT entry entry
3847 point into that section. */
3848 bfd_put_32 (output_bfd,
3849 h && ! FRVFDPIC_SYM_LOCAL (info, h)
3851 : _frvfdpic_osec_to_segment (output_bfd,
3853 ->output_section),
3854 contents + rel->r_offset + 4);
3857 check_segment[0] = check_segment[1] = got_segment;
3858 break;
3860 case R_FRV_GPREL12:
3861 case R_FRV_GPRELU12:
3862 case R_FRV_GPREL32:
3863 case R_FRV_GPRELHI:
3864 case R_FRV_GPRELLO:
3865 check_segment[0] = gprel_segment;
3866 check_segment[1] = sec
3867 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3868 : (unsigned)-1;
3869 break;
3871 case R_FRV_GETTLSOFF:
3872 relocation = frvfdpic_plt_section (info)->output_section->vma
3873 + frvfdpic_plt_section (info)->output_offset
3874 + picrel->tlsplt_entry;
3875 BFD_ASSERT (picrel->tlsplt_entry != (bfd_vma)-1
3876 && picrel->tlsdesc_entry);
3877 check_segment[0] = isec_segment;
3878 check_segment[1] = plt_segment;
3879 break;
3881 case R_FRV_GOTTLSDESC12:
3882 case R_FRV_GOTTLSDESCHI:
3883 case R_FRV_GOTTLSDESCLO:
3884 BFD_ASSERT (picrel->tlsdesc_entry);
3885 relocation = picrel->tlsdesc_entry;
3886 check_segment[0] = tls_segment;
3887 check_segment[1] = sec
3888 && ! bfd_is_abs_section (sec)
3889 && ! bfd_is_und_section (sec)
3890 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3891 : tls_segment;
3892 break;
3894 case R_FRV_TLSMOFF12:
3895 case R_FRV_TLSMOFFHI:
3896 case R_FRV_TLSMOFFLO:
3897 case R_FRV_TLSMOFF:
3898 check_segment[0] = tls_segment;
3899 if (! sec)
3900 check_segment[1] = -1;
3901 else if (bfd_is_abs_section (sec)
3902 || bfd_is_und_section (sec))
3904 relocation = 0;
3905 check_segment[1] = tls_segment;
3907 else if (sec->output_section)
3909 relocation -= tls_biased_base (info);
3910 check_segment[1] =
3911 _frvfdpic_osec_to_segment (output_bfd, sec->output_section);
3913 else
3914 check_segment[1] = -1;
3915 break;
3917 case R_FRV_GOTTLSOFF12:
3918 case R_FRV_GOTTLSOFFHI:
3919 case R_FRV_GOTTLSOFFLO:
3920 BFD_ASSERT (picrel->tlsoff_entry);
3921 relocation = picrel->tlsoff_entry;
3922 check_segment[0] = tls_segment;
3923 check_segment[1] = sec
3924 && ! bfd_is_abs_section (sec)
3925 && ! bfd_is_und_section (sec)
3926 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3927 : tls_segment;
3928 break;
3930 case R_FRV_TLSDESC_VALUE:
3931 case R_FRV_TLSOFF:
3932 /* These shouldn't be present in input object files. */
3933 check_segment[0] = check_segment[1] = isec_segment;
3934 break;
3936 case R_FRV_TLSDESC_RELAX:
3937 case R_FRV_GETTLSOFF_RELAX:
3938 case R_FRV_TLSOFF_RELAX:
3939 /* These are just annotations for relaxation, nothing to do
3940 here. */
3941 continue;
3943 default:
3944 check_segment[0] = isec_segment;
3945 check_segment[1] = sec
3946 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3947 : (unsigned)-1;
3948 break;
3951 if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
3953 /* If you take this out, remove the #error from fdpic-static-6.d
3954 in the ld testsuite. */
3955 /* This helps catch problems in GCC while we can't do more
3956 than static linking. The idea is to test whether the
3957 input file basename is crt0.o only once. */
3958 if (silence_segment_error == 1)
3959 silence_segment_error =
3960 (strlen (input_bfd->filename) == 6
3961 && strcmp (input_bfd->filename, "crt0.o") == 0)
3962 || (strlen (input_bfd->filename) > 6
3963 && strcmp (input_bfd->filename
3964 + strlen (input_bfd->filename) - 7,
3965 "/crt0.o") == 0)
3966 ? -1 : 0;
3967 if (!silence_segment_error
3968 /* We don't want duplicate errors for undefined
3969 symbols. */
3970 && !(picrel && picrel->symndx == -1
3971 && picrel->d.h->root.type == bfd_link_hash_undefined))
3973 if (info->shared || info->pie)
3974 (*_bfd_error_handler)
3975 (_("%B(%A+0x%lx): reloc against `%s': %s"),
3976 input_bfd, input_section, (long)rel->r_offset, name,
3977 _("relocation references a different segment"));
3978 else
3979 info->callbacks->warning
3980 (info,
3981 _("relocation references a different segment"),
3982 name, input_bfd, input_section, rel->r_offset);
3984 if (!silence_segment_error && (info->shared || info->pie))
3985 return FALSE;
3986 elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
3989 switch (r_type)
3991 case R_FRV_GOTOFFHI:
3992 case R_FRV_TLSMOFFHI:
3993 /* We need the addend to be applied before we shift the
3994 value right. */
3995 relocation += rel->r_addend;
3996 /* Fall through. */
3997 case R_FRV_GOTHI:
3998 case R_FRV_FUNCDESC_GOTHI:
3999 case R_FRV_FUNCDESC_GOTOFFHI:
4000 case R_FRV_GOTTLSOFFHI:
4001 case R_FRV_GOTTLSDESCHI:
4002 relocation >>= 16;
4003 /* Fall through. */
4005 case R_FRV_GOTLO:
4006 case R_FRV_FUNCDESC_GOTLO:
4007 case R_FRV_GOTOFFLO:
4008 case R_FRV_FUNCDESC_GOTOFFLO:
4009 case R_FRV_GOTTLSOFFLO:
4010 case R_FRV_GOTTLSDESCLO:
4011 case R_FRV_TLSMOFFLO:
4012 relocation &= 0xffff;
4013 break;
4015 default:
4016 break;
4019 switch (r_type)
4021 case R_FRV_LABEL24:
4022 if (! IS_FDPIC (output_bfd) || ! picrel->plt)
4023 break;
4024 /* Fall through. */
4026 /* When referencing a GOT entry, a function descriptor or a
4027 PLT, we don't want the addend to apply to the reference,
4028 but rather to the referenced symbol. The actual entry
4029 will have already been created taking the addend into
4030 account, so cancel it out here. */
4031 case R_FRV_GOT12:
4032 case R_FRV_GOTHI:
4033 case R_FRV_GOTLO:
4034 case R_FRV_FUNCDESC_GOT12:
4035 case R_FRV_FUNCDESC_GOTHI:
4036 case R_FRV_FUNCDESC_GOTLO:
4037 case R_FRV_FUNCDESC_GOTOFF12:
4038 case R_FRV_FUNCDESC_GOTOFFHI:
4039 case R_FRV_FUNCDESC_GOTOFFLO:
4040 case R_FRV_GETTLSOFF:
4041 case R_FRV_GOTTLSDESC12:
4042 case R_FRV_GOTTLSDESCHI:
4043 case R_FRV_GOTTLSDESCLO:
4044 case R_FRV_GOTTLSOFF12:
4045 case R_FRV_GOTTLSOFFHI:
4046 case R_FRV_GOTTLSOFFLO:
4047 /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
4048 here, since we do want to apply the addend to the others.
4049 Note that we've applied the addend to GOTOFFHI before we
4050 shifted it right. */
4051 case R_FRV_GOTOFFHI:
4052 case R_FRV_TLSMOFFHI:
4053 relocation -= rel->r_addend;
4054 break;
4056 default:
4057 break;
4060 if (r_type == R_FRV_HI16)
4061 r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
4063 else if (r_type == R_FRV_LO16)
4064 r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
4066 else if (r_type == R_FRV_LABEL24 || r_type == R_FRV_GETTLSOFF)
4067 r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
4068 contents, relocation);
4070 else if (r_type == R_FRV_GPREL12)
4071 r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
4072 contents, relocation);
4074 else if (r_type == R_FRV_GPRELU12)
4075 r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
4076 contents, relocation);
4078 else if (r_type == R_FRV_GPRELLO)
4079 r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
4080 contents, relocation);
4082 else if (r_type == R_FRV_GPRELHI)
4083 r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
4084 contents, relocation);
4086 else if (r_type == R_FRV_TLSOFF
4087 || r_type == R_FRV_TLSDESC_VALUE)
4088 r = bfd_reloc_notsupported;
4090 else
4091 r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
4092 rel, relocation);
4094 if (r != bfd_reloc_ok)
4096 const char * msg = (const char *) NULL;
4098 switch (r)
4100 case bfd_reloc_overflow:
4101 r = info->callbacks->reloc_overflow
4102 (info, (h ? &h->root : NULL), name, howto->name,
4103 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4104 break;
4106 case bfd_reloc_undefined:
4107 r = info->callbacks->undefined_symbol
4108 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
4109 break;
4111 case bfd_reloc_outofrange:
4112 msg = _("internal error: out of range error");
4113 break;
4115 case bfd_reloc_notsupported:
4116 msg = _("internal error: unsupported relocation error");
4117 break;
4119 case bfd_reloc_dangerous:
4120 msg = _("internal error: dangerous relocation");
4121 break;
4123 default:
4124 msg = _("internal error: unknown error");
4125 break;
4128 if (msg)
4130 (*_bfd_error_handler)
4131 (_("%B(%A+0x%lx): reloc against `%s': %s"),
4132 input_bfd, input_section, (long)rel->r_offset, name, msg);
4133 return FALSE;
4136 if (! r)
4137 return FALSE;
4141 return TRUE;
4144 /* Return the section that should be marked against GC for a given
4145 relocation. */
4147 static asection *
4148 elf32_frv_gc_mark_hook (asection *sec,
4149 struct bfd_link_info *info,
4150 Elf_Internal_Rela *rel,
4151 struct elf_link_hash_entry *h,
4152 Elf_Internal_Sym *sym)
4154 if (h != NULL)
4155 switch (ELF32_R_TYPE (rel->r_info))
4157 case R_FRV_GNU_VTINHERIT:
4158 case R_FRV_GNU_VTENTRY:
4159 return NULL;
4162 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4165 /* Hook called by the linker routine which adds symbols from an object
4166 file. We use it to put .comm items in .scomm, and not .comm. */
4168 static bfd_boolean
4169 elf32_frv_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
4170 bfd *abfd;
4171 struct bfd_link_info *info;
4172 Elf_Internal_Sym *sym;
4173 const char **namep ATTRIBUTE_UNUSED;
4174 flagword *flagsp ATTRIBUTE_UNUSED;
4175 asection **secp;
4176 bfd_vma *valp;
4178 if (sym->st_shndx == SHN_COMMON
4179 && !info->relocatable
4180 && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
4182 /* Common symbols less than or equal to -G nn bytes are
4183 automatically put into .sbss. */
4185 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
4187 if (scomm == NULL)
4189 scomm = bfd_make_section_with_flags (abfd, ".scommon",
4190 (SEC_ALLOC
4191 | SEC_IS_COMMON
4192 | SEC_LINKER_CREATED));
4193 if (scomm == NULL)
4194 return FALSE;
4197 *secp = scomm;
4198 *valp = sym->st_size;
4201 return TRUE;
4204 /* We need dynamic symbols for every section, since segments can
4205 relocate independently. */
4206 static bfd_boolean
4207 _frvfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
4208 struct bfd_link_info *info
4209 ATTRIBUTE_UNUSED,
4210 asection *p ATTRIBUTE_UNUSED)
4212 switch (elf_section_data (p)->this_hdr.sh_type)
4214 case SHT_PROGBITS:
4215 case SHT_NOBITS:
4216 /* If sh_type is yet undecided, assume it could be
4217 SHT_PROGBITS/SHT_NOBITS. */
4218 case SHT_NULL:
4219 return FALSE;
4221 /* There shouldn't be section relative relocations
4222 against any other section. */
4223 default:
4224 return TRUE;
4228 /* Create a .got section, as well as its additional info field. This
4229 is almost entirely copied from
4230 elflink.c:_bfd_elf_create_got_section(). */
4232 static bfd_boolean
4233 _frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
4235 flagword flags, pltflags;
4236 asection *s;
4237 struct elf_link_hash_entry *h;
4238 struct bfd_link_hash_entry *bh;
4239 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4240 int ptralign;
4241 int offset;
4243 /* This function may be called more than once. */
4244 s = bfd_get_section_by_name (abfd, ".got");
4245 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
4246 return TRUE;
4248 /* Machine specific: although pointers are 32-bits wide, we want the
4249 GOT to be aligned to a 64-bit boundary, such that function
4250 descriptors in it can be accessed with 64-bit loads and
4251 stores. */
4252 ptralign = 3;
4254 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4255 | SEC_LINKER_CREATED);
4256 pltflags = flags;
4258 s = bfd_make_section_with_flags (abfd, ".got", flags);
4259 if (s == NULL
4260 || !bfd_set_section_alignment (abfd, s, ptralign))
4261 return FALSE;
4263 if (bed->want_got_plt)
4265 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
4266 if (s == NULL
4267 || !bfd_set_section_alignment (abfd, s, ptralign))
4268 return FALSE;
4271 if (bed->want_got_sym)
4273 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
4274 (or .got.plt) section. We don't do this in the linker script
4275 because we don't want to define the symbol if we are not creating
4276 a global offset table. */
4277 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
4278 elf_hash_table (info)->hgot = h;
4279 if (h == NULL)
4280 return FALSE;
4282 /* Machine-specific: we want the symbol for executables as
4283 well. */
4284 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4285 return FALSE;
4288 /* The first bit of the global offset table is the header. */
4289 s->size += bed->got_header_size;
4291 /* This is the machine-specific part. Create and initialize section
4292 data for the got. */
4293 if (IS_FDPIC (abfd))
4295 frvfdpic_got_section (info) = s;
4296 frvfdpic_relocs_info (info) = htab_try_create (1,
4297 frvfdpic_relocs_info_hash,
4298 frvfdpic_relocs_info_eq,
4299 (htab_del) NULL);
4300 if (! frvfdpic_relocs_info (info))
4301 return FALSE;
4303 s = bfd_make_section_with_flags (abfd, ".rel.got",
4304 (flags | SEC_READONLY));
4305 if (s == NULL
4306 || ! bfd_set_section_alignment (abfd, s, 2))
4307 return FALSE;
4309 frvfdpic_gotrel_section (info) = s;
4311 /* Machine-specific. */
4312 s = bfd_make_section_with_flags (abfd, ".rofixup",
4313 (flags | SEC_READONLY));
4314 if (s == NULL
4315 || ! bfd_set_section_alignment (abfd, s, 2))
4316 return FALSE;
4318 frvfdpic_gotfixup_section (info) = s;
4319 offset = -2048;
4320 flags = BSF_GLOBAL;
4322 else
4324 offset = 2048;
4325 flags = BSF_GLOBAL | BSF_WEAK;
4328 /* Define _gp in .rofixup, for FDPIC, or .got otherwise. If it
4329 turns out that we're linking with a different linker script, the
4330 linker script will override it. */
4331 bh = NULL;
4332 if (!(_bfd_generic_link_add_one_symbol
4333 (info, abfd, "_gp", flags, s, offset, (const char *) NULL, FALSE,
4334 bed->collect, &bh)))
4335 return FALSE;
4336 h = (struct elf_link_hash_entry *) bh;
4337 h->def_regular = 1;
4338 h->type = STT_OBJECT;
4339 /* h->other = STV_HIDDEN; */ /* Should we? */
4341 /* Machine-specific: we want the symbol for executables as well. */
4342 if (IS_FDPIC (abfd) && ! bfd_elf_link_record_dynamic_symbol (info, h))
4343 return FALSE;
4345 if (!IS_FDPIC (abfd))
4346 return TRUE;
4348 /* FDPIC supports Thread Local Storage, and this may require a
4349 procedure linkage table for TLS PLT entries. */
4351 /* This is mostly copied from
4352 elflink.c:_bfd_elf_create_dynamic_sections(). */
4354 flags = pltflags;
4355 pltflags |= SEC_CODE;
4356 if (bed->plt_not_loaded)
4357 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
4358 if (bed->plt_readonly)
4359 pltflags |= SEC_READONLY;
4361 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
4362 if (s == NULL
4363 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
4364 return FALSE;
4365 /* FRV-specific: remember it. */
4366 frvfdpic_plt_section (info) = s;
4368 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
4369 .plt section. */
4370 if (bed->want_plt_sym)
4372 h = _bfd_elf_define_linkage_sym (abfd, info, s,
4373 "_PROCEDURE_LINKAGE_TABLE_");
4374 elf_hash_table (info)->hplt = h;
4375 if (h == NULL)
4376 return FALSE;
4379 /* FRV-specific: we want rel relocations for the plt. */
4380 s = bfd_make_section_with_flags (abfd, ".rel.plt",
4381 flags | SEC_READONLY);
4382 if (s == NULL
4383 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
4384 return FALSE;
4385 /* FRV-specific: remember it. */
4386 frvfdpic_pltrel_section (info) = s;
4388 return TRUE;
4391 /* Make sure the got and plt sections exist, and that our pointers in
4392 the link hash table point to them. */
4394 static bfd_boolean
4395 elf32_frvfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4397 /* This is mostly copied from
4398 elflink.c:_bfd_elf_create_dynamic_sections(). */
4399 flagword flags;
4400 asection *s;
4401 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4403 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4404 | SEC_LINKER_CREATED);
4406 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4407 .rel[a].bss sections. */
4409 /* FRV-specific: we want to create the GOT and the PLT in the FRV
4410 way. */
4411 if (! _frv_create_got_section (abfd, info))
4412 return FALSE;
4414 /* FRV-specific: make sure we created everything we wanted. */
4415 BFD_ASSERT (frvfdpic_got_section (info) && frvfdpic_gotrel_section (info)
4416 && frvfdpic_gotfixup_section (info)
4417 && frvfdpic_plt_section (info)
4418 && frvfdpic_pltrel_section (info));
4420 if (bed->want_dynbss)
4422 /* The .dynbss section is a place to put symbols which are defined
4423 by dynamic objects, are referenced by regular objects, and are
4424 not functions. We must allocate space for them in the process
4425 image and use a R_*_COPY reloc to tell the dynamic linker to
4426 initialize them at run time. The linker script puts the .dynbss
4427 section into the .bss section of the final image. */
4428 s = bfd_make_section_with_flags (abfd, ".dynbss",
4429 SEC_ALLOC | SEC_LINKER_CREATED);
4430 if (s == NULL)
4431 return FALSE;
4433 /* The .rel[a].bss section holds copy relocs. This section is not
4434 normally needed. We need to create it here, though, so that the
4435 linker will map it to an output section. We can't just create it
4436 only if we need it, because we will not know whether we need it
4437 until we have seen all the input files, and the first time the
4438 main linker code calls BFD after examining all the input files
4439 (size_dynamic_sections) the input sections have already been
4440 mapped to the output sections. If the section turns out not to
4441 be needed, we can discard it later. We will never need this
4442 section when generating a shared object, since they do not use
4443 copy relocs. */
4444 if (! info->shared)
4446 s = bfd_make_section_with_flags (abfd,
4447 (bed->default_use_rela_p
4448 ? ".rela.bss" : ".rel.bss"),
4449 flags | SEC_READONLY);
4450 if (s == NULL
4451 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
4452 return FALSE;
4456 return TRUE;
4459 /* Compute the total GOT and PLT size required by each symbol in each
4460 range. Symbols may require up to 4 words in the GOT: an entry
4461 pointing to the symbol, an entry pointing to its function
4462 descriptor, and a private function descriptors taking two
4463 words. */
4465 static void
4466 _frvfdpic_count_nontls_entries (struct frvfdpic_relocs_info *entry,
4467 struct _frvfdpic_dynamic_got_info *dinfo)
4469 /* Allocate space for a GOT entry pointing to the symbol. */
4470 if (entry->got12)
4471 dinfo->got12 += 4;
4472 else if (entry->gotlos)
4473 dinfo->gotlos += 4;
4474 else if (entry->gothilo)
4475 dinfo->gothilo += 4;
4476 else
4477 entry->relocs32--;
4478 entry->relocs32++;
4480 /* Allocate space for a GOT entry pointing to the function
4481 descriptor. */
4482 if (entry->fdgot12)
4483 dinfo->got12 += 4;
4484 else if (entry->fdgotlos)
4485 dinfo->gotlos += 4;
4486 else if (entry->fdgothilo)
4487 dinfo->gothilo += 4;
4488 else
4489 entry->relocsfd--;
4490 entry->relocsfd++;
4492 /* Decide whether we need a PLT entry, a function descriptor in the
4493 GOT, and a lazy PLT entry for this symbol. */
4494 entry->plt = entry->call
4495 && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4496 && elf_hash_table (dinfo->info)->dynamic_sections_created;
4497 entry->privfd = entry->plt
4498 || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
4499 || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
4500 && (entry->symndx != -1
4501 || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
4502 entry->lazyplt = entry->privfd
4503 && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4504 && ! (dinfo->info->flags & DF_BIND_NOW)
4505 && elf_hash_table (dinfo->info)->dynamic_sections_created;
4507 /* Allocate space for a function descriptor. */
4508 if (entry->fdgoff12)
4509 dinfo->fd12 += 8;
4510 else if (entry->fdgofflos)
4511 dinfo->fdlos += 8;
4512 else if (entry->privfd && entry->plt)
4513 dinfo->fdplt += 8;
4514 else if (entry->privfd)
4515 dinfo->fdhilo += 8;
4516 else
4517 entry->relocsfdv--;
4518 entry->relocsfdv++;
4520 if (entry->lazyplt)
4521 dinfo->lzplt += 8;
4524 /* Compute the total GOT size required by each TLS symbol in each
4525 range. Symbols may require up to 5 words in the GOT: an entry
4526 holding the TLS offset for the symbol, and an entry with a full TLS
4527 descriptor taking 4 words. */
4529 static void
4530 _frvfdpic_count_tls_entries (struct frvfdpic_relocs_info *entry,
4531 struct _frvfdpic_dynamic_got_info *dinfo,
4532 bfd_boolean subtract)
4534 const int l = subtract ? -1 : 1;
4536 /* Allocate space for a GOT entry with the TLS offset of the
4537 symbol. */
4538 if (entry->tlsoff12)
4539 dinfo->got12 += 4 * l;
4540 else if (entry->tlsofflos)
4541 dinfo->gotlos += 4 * l;
4542 else if (entry->tlsoffhilo)
4543 dinfo->gothilo += 4 * l;
4544 else
4545 entry->relocstlsoff -= l;
4546 entry->relocstlsoff += l;
4548 /* If there's any TLSOFF relocation, mark the output file as not
4549 suitable for dlopening. This mark will remain even if we relax
4550 all such relocations, but this is not a problem, since we'll only
4551 do so for executables, and we definitely don't want anyone
4552 dlopening executables. */
4553 if (entry->relocstlsoff)
4554 dinfo->info->flags |= DF_STATIC_TLS;
4556 /* Allocate space for a TLS descriptor. */
4557 if (entry->tlsdesc12)
4558 dinfo->tlsd12 += 8 * l;
4559 else if (entry->tlsdesclos)
4560 dinfo->tlsdlos += 8 * l;
4561 else if (entry->tlsplt)
4562 dinfo->tlsdplt += 8 * l;
4563 else if (entry->tlsdeschilo)
4564 dinfo->tlsdhilo += 8 * l;
4565 else
4566 entry->relocstlsd -= l;
4567 entry->relocstlsd += l;
4570 /* Compute the number of dynamic relocations and fixups that a symbol
4571 requires, and add (or subtract) from the grand and per-symbol
4572 totals. */
4574 static void
4575 _frvfdpic_count_relocs_fixups (struct frvfdpic_relocs_info *entry,
4576 struct _frvfdpic_dynamic_got_info *dinfo,
4577 bfd_boolean subtract)
4579 bfd_vma relocs = 0, fixups = 0, tlsrets = 0;
4581 if (!dinfo->info->executable || dinfo->info->pie)
4583 relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv
4584 + entry->relocstlsd;
4586 /* In the executable, TLS relocations to symbols that bind
4587 locally (including those that resolve to global TLS offsets)
4588 are resolved immediately, without any need for fixups or
4589 dynamic relocations. In shared libraries, however, we must
4590 emit dynamic relocations even for local symbols, because we
4591 don't know the module id the library is going to get at
4592 run-time, nor its TLS base offset. */
4593 if (!dinfo->info->executable
4594 || (entry->symndx == -1
4595 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4596 relocs += entry->relocstlsoff;
4598 else
4600 if (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
4602 if (entry->symndx != -1
4603 || entry->d.h->root.type != bfd_link_hash_undefweak)
4604 fixups += entry->relocs32 + 2 * entry->relocsfdv;
4605 fixups += entry->relocstlsd;
4606 tlsrets += entry->relocstlsd;
4608 else
4610 relocs += entry->relocs32 + entry->relocsfdv
4611 + entry->relocstlsoff + entry->relocstlsd;
4614 if (entry->symndx != -1
4615 || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
4617 if (entry->symndx != -1
4618 || entry->d.h->root.type != bfd_link_hash_undefweak)
4619 fixups += entry->relocsfd;
4621 else
4622 relocs += entry->relocsfd;
4625 if (subtract)
4627 relocs = - relocs;
4628 fixups = - fixups;
4629 tlsrets = - tlsrets;
4632 entry->dynrelocs += relocs;
4633 entry->fixups += fixups;
4634 dinfo->relocs += relocs;
4635 dinfo->fixups += fixups;
4636 dinfo->tls_ret_refs += tlsrets;
4639 /* Look for opportunities to relax TLS relocations. We can assume
4640 we're linking the main executable or a static-tls library, since
4641 otherwise we wouldn't have got here. When relaxing, we have to
4642 first undo any previous accounting of TLS uses of fixups, dynamic
4643 relocations, GOT and PLT entries. */
4645 static void
4646 _frvfdpic_relax_tls_entries (struct frvfdpic_relocs_info *entry,
4647 struct _frvfdpic_dynamic_got_info *dinfo,
4648 bfd_boolean relaxing)
4650 bfd_boolean changed = ! relaxing;
4652 BFD_ASSERT (dinfo->info->executable
4653 || (dinfo->info->flags & DF_STATIC_TLS));
4655 if (entry->tlsdesc12 || entry->tlsdesclos || entry->tlsdeschilo)
4657 if (! changed)
4659 _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4660 _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4661 changed = TRUE;
4664 /* When linking an executable, we can always decay GOTTLSDESC to
4665 TLSMOFF, if the symbol is local, or GOTTLSOFF, otherwise.
4666 When linking a static-tls shared library, using TLSMOFF is
4667 not an option, but we can still use GOTTLSOFF. When decaying
4668 to GOTTLSOFF, we must keep the GOT entry in range. We know
4669 it has to fit because we'll be trading the 4 words of hte TLS
4670 descriptor for a single word in the same range. */
4671 if (! dinfo->info->executable
4672 || (entry->symndx == -1
4673 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4675 entry->tlsoff12 |= entry->tlsdesc12;
4676 entry->tlsofflos |= entry->tlsdesclos;
4677 entry->tlsoffhilo |= entry->tlsdeschilo;
4680 entry->tlsdesc12 = entry->tlsdesclos = entry->tlsdeschilo = 0;
4683 /* We can only decay TLSOFFs or call #gettlsoff to TLSMOFF in the
4684 main executable. We have to check whether the symbol's TLSOFF is
4685 in range for a setlos. For symbols with a hash entry, we can
4686 determine exactly what to do; for others locals, we don't have
4687 addresses handy, so we use the size of the TLS section as an
4688 approximation. If we get it wrong, we'll retain a GOT entry
4689 holding the TLS offset (without dynamic relocations or fixups),
4690 but we'll still optimize away the loads from it. Since TLS sizes
4691 are generally very small, it's probably not worth attempting to
4692 do better than this. */
4693 if ((entry->tlsplt
4694 || entry->tlsoff12 || entry->tlsofflos || entry->tlsoffhilo)
4695 && dinfo->info->executable && relaxing
4696 && ((entry->symndx == -1
4697 && FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4698 /* The above may hold for an undefweak TLS symbol, so make
4699 sure we don't have this case before accessing def.value
4700 and def.section. */
4701 && (entry->d.h->root.type == bfd_link_hash_undefweak
4702 || (bfd_vma)(entry->d.h->root.u.def.value
4703 + (entry->d.h->root.u.def.section
4704 ->output_section->vma)
4705 + entry->d.h->root.u.def.section->output_offset
4706 + entry->addend
4707 - tls_biased_base (dinfo->info)
4708 + 32768) < (bfd_vma)65536))
4709 || (entry->symndx != -1
4710 && (elf_hash_table (dinfo->info)->tls_sec->size
4711 + abs (entry->addend) < 32768 + FRVFDPIC_TLS_BIAS))))
4713 if (! changed)
4715 _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4716 _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4717 changed = TRUE;
4720 entry->tlsplt =
4721 entry->tlsoff12 = entry->tlsofflos = entry->tlsoffhilo = 0;
4724 /* We can decay `call #gettlsoff' to a ldi #tlsoff if we already
4725 have a #gottlsoff12 relocation for this entry, or if we can fit
4726 one more in the 12-bit (and 16-bit) ranges. */
4727 if (entry->tlsplt
4728 && (entry->tlsoff12
4729 || (relaxing
4730 && dinfo->got12 + dinfo->fd12 + dinfo->tlsd12 <= 4096 - 12 - 4
4731 && (dinfo->got12 + dinfo->fd12 + dinfo->tlsd12
4732 + dinfo->gotlos + dinfo->fdlos + dinfo->tlsdlos
4733 <= 65536 - 12 - 4))))
4735 if (! changed)
4737 _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4738 _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4739 changed = TRUE;
4742 entry->tlsoff12 = 1;
4743 entry->tlsplt = 0;
4746 if (changed)
4748 _frvfdpic_count_tls_entries (entry, dinfo, FALSE);
4749 _frvfdpic_count_relocs_fixups (entry, dinfo, FALSE);
4752 return;
4755 /* Compute the total GOT and PLT size required by each symbol in each range. *
4756 Symbols may require up to 4 words in the GOT: an entry pointing to
4757 the symbol, an entry pointing to its function descriptor, and a
4758 private function descriptors taking two words. */
4760 static int
4761 _frvfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
4763 struct frvfdpic_relocs_info *entry = *entryp;
4764 struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
4766 _frvfdpic_count_nontls_entries (entry, dinfo);
4768 if (dinfo->info->executable || (dinfo->info->flags & DF_STATIC_TLS))
4769 _frvfdpic_relax_tls_entries (entry, dinfo, FALSE);
4770 else
4772 _frvfdpic_count_tls_entries (entry, dinfo, FALSE);
4773 _frvfdpic_count_relocs_fixups (entry, dinfo, FALSE);
4776 return 1;
4779 /* Determine the positive and negative ranges to be used by each
4780 offset range in the GOT. FDCUR and CUR, that must be aligned to a
4781 double-word boundary, are the minimum (negative) and maximum
4782 (positive) GOT offsets already used by previous ranges, except for
4783 an ODD entry that may have been left behind. GOT and FD indicate
4784 the size of GOT entries and function descriptors that must be
4785 placed within the range from -WRAP to WRAP. If there's room left,
4786 up to FDPLT bytes should be reserved for additional function
4787 descriptors. */
4789 inline static bfd_signed_vma
4790 _frvfdpic_compute_got_alloc_data (struct _frvfdpic_dynamic_got_alloc_data *gad,
4791 bfd_signed_vma fdcur,
4792 bfd_signed_vma odd,
4793 bfd_signed_vma cur,
4794 bfd_vma got,
4795 bfd_vma fd,
4796 bfd_vma fdplt,
4797 bfd_vma tlsd,
4798 bfd_vma tlsdplt,
4799 bfd_vma wrap)
4801 bfd_signed_vma wrapmin = -wrap;
4802 const bfd_vma tdescsz = 8;
4804 /* Start at the given initial points. */
4805 gad->fdcur = fdcur;
4806 gad->cur = cur;
4808 /* If we had an incoming odd word and we have any got entries that
4809 are going to use it, consume it, otherwise leave gad->odd at
4810 zero. We might force gad->odd to zero and return the incoming
4811 odd such that it is used by the next range, but then GOT entries
4812 might appear to be out of order and we wouldn't be able to
4813 shorten the GOT by one word if it turns out to end with an
4814 unpaired GOT entry. */
4815 if (odd && got)
4817 gad->odd = odd;
4818 got -= 4;
4819 odd = 0;
4821 else
4822 gad->odd = 0;
4824 /* If we're left with an unpaired GOT entry, compute its location
4825 such that we can return it. Otherwise, if got doesn't require an
4826 odd number of words here, either odd was already zero in the
4827 block above, or it was set to zero because got was non-zero, or
4828 got was already zero. In the latter case, we want the value of
4829 odd to carry over to the return statement, so we don't want to
4830 reset odd unless the condition below is true. */
4831 if (got & 4)
4833 odd = cur + got;
4834 got += 4;
4837 /* Compute the tentative boundaries of this range. */
4838 gad->max = cur + got;
4839 gad->min = fdcur - fd;
4840 gad->fdplt = 0;
4842 /* If function descriptors took too much space, wrap some of them
4843 around. */
4844 if (gad->min < wrapmin)
4846 gad->max += wrapmin - gad->min;
4847 gad->tmin = gad->min = wrapmin;
4850 /* If GOT entries took too much space, wrap some of them around.
4851 This may well cause gad->min to become lower than wrapmin. This
4852 will cause a relocation overflow later on, so we don't have to
4853 report it here . */
4854 if ((bfd_vma) gad->max > wrap)
4856 gad->min -= gad->max - wrap;
4857 gad->max = wrap;
4860 /* Add TLS descriptors. */
4861 gad->tmax = gad->max + tlsd;
4862 gad->tmin = gad->min;
4863 gad->tlsdplt = 0;
4865 /* If TLS descriptors took too much space, wrap an integral number
4866 of them around. */
4867 if ((bfd_vma) gad->tmax > wrap)
4869 bfd_vma wrapsize = gad->tmax - wrap;
4871 wrapsize += tdescsz / 2;
4872 wrapsize &= ~ tdescsz / 2;
4874 gad->tmin -= wrapsize;
4875 gad->tmax -= wrapsize;
4878 /* If there is space left and we have function descriptors
4879 referenced in PLT entries that could take advantage of shorter
4880 offsets, place them now. */
4881 if (fdplt && gad->tmin > wrapmin)
4883 bfd_vma fds;
4885 if ((bfd_vma) (gad->tmin - wrapmin) < fdplt)
4886 fds = gad->tmin - wrapmin;
4887 else
4888 fds = fdplt;
4890 fdplt -= fds;
4891 gad->min -= fds;
4892 gad->tmin -= fds;
4893 gad->fdplt += fds;
4896 /* If there is more space left, try to place some more function
4897 descriptors for PLT entries. */
4898 if (fdplt && (bfd_vma) gad->tmax < wrap)
4900 bfd_vma fds;
4902 if ((bfd_vma) (wrap - gad->tmax) < fdplt)
4903 fds = wrap - gad->tmax;
4904 else
4905 fds = fdplt;
4907 fdplt -= fds;
4908 gad->max += fds;
4909 gad->tmax += fds;
4910 gad->fdplt += fds;
4913 /* If there is space left and we have TLS descriptors referenced in
4914 PLT entries that could take advantage of shorter offsets, place
4915 them now. */
4916 if (tlsdplt && gad->tmin > wrapmin)
4918 bfd_vma tlsds;
4920 if ((bfd_vma) (gad->tmin - wrapmin) < tlsdplt)
4921 tlsds = (gad->tmin - wrapmin) & ~ (tdescsz / 2);
4922 else
4923 tlsds = tlsdplt;
4925 tlsdplt -= tlsds;
4926 gad->tmin -= tlsds;
4927 gad->tlsdplt += tlsds;
4930 /* If there is more space left, try to place some more TLS
4931 descriptors for PLT entries. Although we could try to fit an
4932 additional TLS descriptor with half of it just before before the
4933 wrap point and another right past the wrap point, this might
4934 cause us to run out of space for the next region, so don't do
4935 it. */
4936 if (tlsdplt && (bfd_vma) gad->tmax < wrap - tdescsz / 2)
4938 bfd_vma tlsds;
4940 if ((bfd_vma) (wrap - gad->tmax) < tlsdplt)
4941 tlsds = (wrap - gad->tmax) & ~ (tdescsz / 2);
4942 else
4943 tlsds = tlsdplt;
4945 tlsdplt -= tlsds;
4946 gad->tmax += tlsds;
4947 gad->tlsdplt += tlsds;
4950 /* If odd was initially computed as an offset past the wrap point,
4951 wrap it around. */
4952 if (odd > gad->max)
4953 odd = gad->min + odd - gad->max;
4955 /* _frvfdpic_get_got_entry() below will always wrap gad->cur if needed
4956 before returning, so do it here too. This guarantees that,
4957 should cur and fdcur meet at the wrap point, they'll both be
4958 equal to min. */
4959 if (gad->cur == gad->max)
4960 gad->cur = gad->min;
4962 /* Ditto for _frvfdpic_get_tlsdesc_entry(). */
4963 gad->tcur = gad->max;
4964 if (gad->tcur == gad->tmax)
4965 gad->tcur = gad->tmin;
4967 return odd;
4970 /* Compute the location of the next GOT entry, given the allocation
4971 data for a range. */
4973 inline static bfd_signed_vma
4974 _frvfdpic_get_got_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4976 bfd_signed_vma ret;
4978 if (gad->odd)
4980 /* If there was an odd word left behind, use it. */
4981 ret = gad->odd;
4982 gad->odd = 0;
4984 else
4986 /* Otherwise, use the word pointed to by cur, reserve the next
4987 as an odd word, and skip to the next pair of words, possibly
4988 wrapping around. */
4989 ret = gad->cur;
4990 gad->odd = gad->cur + 4;
4991 gad->cur += 8;
4992 if (gad->cur == gad->max)
4993 gad->cur = gad->min;
4996 return ret;
4999 /* Compute the location of the next function descriptor entry in the
5000 GOT, given the allocation data for a range. */
5002 inline static bfd_signed_vma
5003 _frvfdpic_get_fd_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
5005 /* If we're at the bottom, wrap around, and only then allocate the
5006 next pair of words. */
5007 if (gad->fdcur == gad->min)
5008 gad->fdcur = gad->max;
5009 return gad->fdcur -= 8;
5012 /* Compute the location of the next TLS descriptor entry in the GOT,
5013 given the allocation data for a range. */
5014 inline static bfd_signed_vma
5015 _frvfdpic_get_tlsdesc_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
5017 bfd_signed_vma ret;
5019 ret = gad->tcur;
5021 gad->tcur += 8;
5023 /* If we're at the top of the region, wrap around to the bottom. */
5024 if (gad->tcur == gad->tmax)
5025 gad->tcur = gad->tmin;
5027 return ret;
5030 /* Assign GOT offsets for every GOT entry and function descriptor.
5031 Doing everything in a single pass is tricky. */
5033 static int
5034 _frvfdpic_assign_got_entries (void **entryp, void *info_)
5036 struct frvfdpic_relocs_info *entry = *entryp;
5037 struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
5039 if (entry->got12)
5040 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5041 else if (entry->gotlos)
5042 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5043 else if (entry->gothilo)
5044 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5046 if (entry->fdgot12)
5047 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5048 else if (entry->fdgotlos)
5049 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5050 else if (entry->fdgothilo)
5051 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5053 if (entry->fdgoff12)
5054 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
5055 else if (entry->plt && dinfo->got12.fdplt)
5057 dinfo->got12.fdplt -= 8;
5058 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
5060 else if (entry->fdgofflos)
5061 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
5062 else if (entry->plt && dinfo->gotlos.fdplt)
5064 dinfo->gotlos.fdplt -= 8;
5065 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
5067 else if (entry->plt)
5069 dinfo->gothilo.fdplt -= 8;
5070 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
5072 else if (entry->privfd)
5073 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
5075 if (entry->tlsoff12)
5076 entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5077 else if (entry->tlsofflos)
5078 entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5079 else if (entry->tlsoffhilo)
5080 entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5082 if (entry->tlsdesc12)
5083 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5084 else if (entry->tlsplt && dinfo->got12.tlsdplt)
5086 dinfo->got12.tlsdplt -= 8;
5087 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5089 else if (entry->tlsdesclos)
5090 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5091 else if (entry->tlsplt && dinfo->gotlos.tlsdplt)
5093 dinfo->gotlos.tlsdplt -= 8;
5094 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5096 else if (entry->tlsplt)
5098 dinfo->gothilo.tlsdplt -= 8;
5099 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5101 else if (entry->tlsdeschilo)
5102 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5104 return 1;
5107 /* Assign GOT offsets to private function descriptors used by PLT
5108 entries (or referenced by 32-bit offsets), as well as PLT entries
5109 and lazy PLT entries. */
5111 static int
5112 _frvfdpic_assign_plt_entries (void **entryp, void *info_)
5114 struct frvfdpic_relocs_info *entry = *entryp;
5115 struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
5117 if (entry->privfd)
5118 BFD_ASSERT (entry->fd_entry);
5120 if (entry->plt)
5122 int size;
5124 /* We use the section's raw size to mark the location of the
5125 next PLT entry. */
5126 entry->plt_entry = frvfdpic_plt_section (dinfo->g.info)->size;
5128 /* Figure out the length of this PLT entry based on the
5129 addressing mode we need to reach the function descriptor. */
5130 BFD_ASSERT (entry->fd_entry);
5131 if (entry->fd_entry >= -(1 << (12 - 1))
5132 && entry->fd_entry < (1 << (12 - 1)))
5133 size = 8;
5134 else if (entry->fd_entry >= -(1 << (16 - 1))
5135 && entry->fd_entry < (1 << (16 - 1)))
5136 size = 12;
5137 else
5138 size = 16;
5140 frvfdpic_plt_section (dinfo->g.info)->size += size;
5143 if (entry->lazyplt)
5145 entry->lzplt_entry = dinfo->g.lzplt;
5146 dinfo->g.lzplt += 8;
5147 /* If this entry is the one that gets the resolver stub, account
5148 for the additional instruction. */
5149 if (entry->lzplt_entry % FRVFDPIC_LZPLT_BLOCK_SIZE
5150 == FRVFDPIC_LZPLT_RESOLV_LOC)
5151 dinfo->g.lzplt += 4;
5154 if (entry->tlsplt)
5156 int size;
5158 entry->tlsplt_entry
5159 = frvfdpic_plt_section (dinfo->g.info)->size;
5161 if (dinfo->g.info->executable
5162 && (entry->symndx != -1
5163 || FRVFDPIC_SYM_LOCAL (dinfo->g.info, entry->d.h)))
5165 if ((bfd_signed_vma)entry->addend >= -(1 << (16 - 1))
5166 /* FIXME: here we use the size of the TLS section
5167 as an upper bound for the value of the TLS
5168 symbol, because we may not know the exact value
5169 yet. If we get it wrong, we'll just waste a
5170 word in the PLT, and we should never get even
5171 close to 32 KiB of TLS anyway. */
5172 && elf_hash_table (dinfo->g.info)->tls_sec
5173 && (elf_hash_table (dinfo->g.info)->tls_sec->size
5174 + (bfd_signed_vma)(entry->addend) <= (1 << (16 - 1))))
5175 size = 8;
5176 else
5177 size = 12;
5179 else if (entry->tlsoff_entry)
5181 if (entry->tlsoff_entry >= -(1 << (12 - 1))
5182 && entry->tlsoff_entry < (1 << (12 - 1)))
5183 size = 8;
5184 else if (entry->tlsoff_entry >= -(1 << (16 - 1))
5185 && entry->tlsoff_entry < (1 << (16 - 1)))
5186 size = 12;
5187 else
5188 size = 16;
5190 else
5192 BFD_ASSERT (entry->tlsdesc_entry);
5194 if (entry->tlsdesc_entry >= -(1 << (12 - 1))
5195 && entry->tlsdesc_entry < (1 << (12 - 1)))
5196 size = 8;
5197 else if (entry->tlsdesc_entry >= -(1 << (16 - 1))
5198 && entry->tlsdesc_entry < (1 << (16 - 1)))
5199 size = 12;
5200 else
5201 size = 16;
5204 frvfdpic_plt_section (dinfo->g.info)->size += size;
5207 return 1;
5210 /* Cancel out any effects of calling _frvfdpic_assign_got_entries and
5211 _frvfdpic_assign_plt_entries. */
5213 static int
5214 _frvfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
5216 struct frvfdpic_relocs_info *entry = *entryp;
5218 entry->got_entry = 0;
5219 entry->fdgot_entry = 0;
5220 entry->fd_entry = 0;
5221 entry->plt_entry = (bfd_vma)-1;
5222 entry->lzplt_entry = (bfd_vma)-1;
5223 entry->tlsoff_entry = 0;
5224 entry->tlsdesc_entry = 0;
5225 entry->tlsplt_entry = (bfd_vma)-1;
5227 return 1;
5230 /* Follow indirect and warning hash entries so that each got entry
5231 points to the final symbol definition. P must point to a pointer
5232 to the hash table we're traversing. Since this traversal may
5233 modify the hash table, we set this pointer to NULL to indicate
5234 we've made a potentially-destructive change to the hash table, so
5235 the traversal must be restarted. */
5236 static int
5237 _frvfdpic_resolve_final_relocs_info (void **entryp, void *p)
5239 struct frvfdpic_relocs_info *entry = *entryp;
5240 htab_t *htab = p;
5242 if (entry->symndx == -1)
5244 struct elf_link_hash_entry *h = entry->d.h;
5245 struct frvfdpic_relocs_info *oentry;
5247 while (h->root.type == bfd_link_hash_indirect
5248 || h->root.type == bfd_link_hash_warning)
5249 h = (struct elf_link_hash_entry *)h->root.u.i.link;
5251 if (entry->d.h == h)
5252 return 1;
5254 oentry = frvfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
5255 NO_INSERT);
5257 if (oentry)
5259 /* Merge the two entries. */
5260 frvfdpic_pic_merge_early_relocs_info (oentry, entry);
5261 htab_clear_slot (*htab, entryp);
5262 return 1;
5265 entry->d.h = h;
5267 /* If we can't find this entry with the new bfd hash, re-insert
5268 it, and get the traversal restarted. */
5269 if (! htab_find (*htab, entry))
5271 htab_clear_slot (*htab, entryp);
5272 entryp = htab_find_slot (*htab, entry, INSERT);
5273 if (! *entryp)
5274 *entryp = entry;
5275 /* Abort the traversal, since the whole table may have
5276 moved, and leave it up to the parent to restart the
5277 process. */
5278 *(htab_t *)p = NULL;
5279 return 0;
5283 return 1;
5286 /* Compute the total size of the GOT, the PLT, the dynamic relocations
5287 section and the rofixup section. Assign locations for GOT and PLT
5288 entries. */
5290 static bfd_boolean
5291 _frvfdpic_size_got_plt (bfd *output_bfd,
5292 struct _frvfdpic_dynamic_got_plt_info *gpinfop)
5294 bfd_signed_vma odd;
5295 bfd_vma limit, tlslimit;
5296 struct bfd_link_info *info = gpinfop->g.info;
5297 bfd *dynobj = elf_hash_table (info)->dynobj;
5299 memcpy (frvfdpic_dynamic_got_plt_info (info), &gpinfop->g,
5300 sizeof (gpinfop->g));
5302 odd = 12;
5303 /* Compute the total size taken by entries in the 12-bit and 16-bit
5304 ranges, to tell how many PLT function descriptors we can bring
5305 into the 12-bit range without causing the 16-bit range to
5306 overflow. */
5307 limit = odd + gpinfop->g.got12 + gpinfop->g.gotlos
5308 + gpinfop->g.fd12 + gpinfop->g.fdlos
5309 + gpinfop->g.tlsd12 + gpinfop->g.tlsdlos;
5310 if (limit < (bfd_vma)1 << 16)
5311 limit = ((bfd_vma)1 << 16) - limit;
5312 else
5313 limit = 0;
5314 if (gpinfop->g.fdplt < limit)
5316 tlslimit = (limit - gpinfop->g.fdplt) & ~ (bfd_vma) 8;
5317 limit = gpinfop->g.fdplt;
5319 else
5320 tlslimit = 0;
5321 if (gpinfop->g.tlsdplt < tlslimit)
5322 tlslimit = gpinfop->g.tlsdplt;
5324 /* Determine the ranges of GOT offsets that we can use for each
5325 range of addressing modes. */
5326 odd = _frvfdpic_compute_got_alloc_data (&gpinfop->got12,
5328 odd,
5330 gpinfop->g.got12,
5331 gpinfop->g.fd12,
5332 limit,
5333 gpinfop->g.tlsd12,
5334 tlslimit,
5335 (bfd_vma)1 << (12-1));
5336 odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gotlos,
5337 gpinfop->got12.tmin,
5338 odd,
5339 gpinfop->got12.tmax,
5340 gpinfop->g.gotlos,
5341 gpinfop->g.fdlos,
5342 gpinfop->g.fdplt
5343 - gpinfop->got12.fdplt,
5344 gpinfop->g.tlsdlos,
5345 gpinfop->g.tlsdplt
5346 - gpinfop->got12.tlsdplt,
5347 (bfd_vma)1 << (16-1));
5348 odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gothilo,
5349 gpinfop->gotlos.tmin,
5350 odd,
5351 gpinfop->gotlos.tmax,
5352 gpinfop->g.gothilo,
5353 gpinfop->g.fdhilo,
5354 gpinfop->g.fdplt
5355 - gpinfop->got12.fdplt
5356 - gpinfop->gotlos.fdplt,
5357 gpinfop->g.tlsdhilo,
5358 gpinfop->g.tlsdplt
5359 - gpinfop->got12.tlsdplt
5360 - gpinfop->gotlos.tlsdplt,
5361 (bfd_vma)1 << (32-1));
5363 /* Now assign (most) GOT offsets. */
5364 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_got_entries,
5365 gpinfop);
5367 frvfdpic_got_section (info)->size = gpinfop->gothilo.tmax
5368 - gpinfop->gothilo.tmin
5369 /* If an odd word is the last word of the GOT, we don't need this
5370 word to be part of the GOT. */
5371 - (odd + 4 == gpinfop->gothilo.tmax ? 4 : 0);
5372 if (frvfdpic_got_section (info)->size == 0)
5373 frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5374 else if (frvfdpic_got_section (info)->size == 12
5375 && ! elf_hash_table (info)->dynamic_sections_created)
5377 frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5378 frvfdpic_got_section (info)->size = 0;
5380 /* This will be non-NULL during relaxation. The assumption is that
5381 the size of one of these sections will never grow, only shrink,
5382 so we can use the larger buffer we allocated before. */
5383 else if (frvfdpic_got_section (info)->contents == NULL)
5385 frvfdpic_got_section (info)->contents =
5386 (bfd_byte *) bfd_zalloc (dynobj,
5387 frvfdpic_got_section (info)->size);
5388 if (frvfdpic_got_section (info)->contents == NULL)
5389 return FALSE;
5392 if (frvfdpic_gotrel_section (info))
5393 /* Subtract the number of lzplt entries, since those will generate
5394 relocations in the pltrel section. */
5395 frvfdpic_gotrel_section (info)->size =
5396 (gpinfop->g.relocs - gpinfop->g.lzplt / 8)
5397 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5398 else
5399 BFD_ASSERT (gpinfop->g.relocs == 0);
5400 if (frvfdpic_gotrel_section (info)->size == 0)
5401 frvfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
5402 else if (frvfdpic_gotrel_section (info)->contents == NULL)
5404 frvfdpic_gotrel_section (info)->contents =
5405 (bfd_byte *) bfd_zalloc (dynobj,
5406 frvfdpic_gotrel_section (info)->size);
5407 if (frvfdpic_gotrel_section (info)->contents == NULL)
5408 return FALSE;
5411 frvfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
5412 if (frvfdpic_gotfixup_section (info)->size == 0)
5413 frvfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
5414 else if (frvfdpic_gotfixup_section (info)->contents == NULL)
5416 frvfdpic_gotfixup_section (info)->contents =
5417 (bfd_byte *) bfd_zalloc (dynobj,
5418 frvfdpic_gotfixup_section (info)->size);
5419 if (frvfdpic_gotfixup_section (info)->contents == NULL)
5420 return FALSE;
5423 if (frvfdpic_pltrel_section (info))
5425 frvfdpic_pltrel_section (info)->size =
5426 gpinfop->g.lzplt / 8
5427 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5428 if (frvfdpic_pltrel_section (info)->size == 0)
5429 frvfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
5430 else if (frvfdpic_pltrel_section (info)->contents == NULL)
5432 frvfdpic_pltrel_section (info)->contents =
5433 (bfd_byte *) bfd_zalloc (dynobj,
5434 frvfdpic_pltrel_section (info)->size);
5435 if (frvfdpic_pltrel_section (info)->contents == NULL)
5436 return FALSE;
5440 /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
5441 such that there's room for the additional instruction needed to
5442 call the resolver. Since _frvfdpic_assign_got_entries didn't
5443 account for them, our block size is 4 bytes smaller than the real
5444 block size. */
5445 if (frvfdpic_plt_section (info))
5447 frvfdpic_plt_section (info)->size = gpinfop->g.lzplt
5448 + ((gpinfop->g.lzplt + (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) - 8)
5449 / (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) * 4);
5452 /* Reset it, such that _frvfdpic_assign_plt_entries() can use it to
5453 actually assign lazy PLT entries addresses. */
5454 gpinfop->g.lzplt = 0;
5456 /* Save information that we're going to need to generate GOT and PLT
5457 entries. */
5458 frvfdpic_got_initial_offset (info) = -gpinfop->gothilo.tmin;
5460 if (get_elf_backend_data (output_bfd)->want_got_sym)
5461 elf_hash_table (info)->hgot->root.u.def.value
5462 = frvfdpic_got_initial_offset (info);
5464 if (frvfdpic_plt_section (info))
5465 frvfdpic_plt_initial_offset (info) =
5466 frvfdpic_plt_section (info)->size;
5468 /* Allocate a ret statement at plt_initial_offset, to be used by
5469 locally-resolved TLS descriptors. */
5470 if (gpinfop->g.tls_ret_refs)
5471 frvfdpic_plt_section (info)->size += 4;
5473 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_plt_entries,
5474 gpinfop);
5476 /* Allocate the PLT section contents only after
5477 _frvfdpic_assign_plt_entries has a chance to add the size of the
5478 non-lazy PLT entries. */
5479 if (frvfdpic_plt_section (info))
5481 if (frvfdpic_plt_section (info)->size == 0)
5482 frvfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
5483 else if (frvfdpic_plt_section (info)->contents == NULL)
5485 frvfdpic_plt_section (info)->contents =
5486 (bfd_byte *) bfd_zalloc (dynobj,
5487 frvfdpic_plt_section (info)->size);
5488 if (frvfdpic_plt_section (info)->contents == NULL)
5489 return FALSE;
5493 return TRUE;
5496 /* Set the sizes of the dynamic sections. */
5498 static bfd_boolean
5499 elf32_frvfdpic_size_dynamic_sections (bfd *output_bfd,
5500 struct bfd_link_info *info)
5502 bfd *dynobj;
5503 asection *s;
5504 struct _frvfdpic_dynamic_got_plt_info gpinfo;
5506 dynobj = elf_hash_table (info)->dynobj;
5507 BFD_ASSERT (dynobj != NULL);
5509 if (elf_hash_table (info)->dynamic_sections_created)
5511 /* Set the contents of the .interp section to the interpreter. */
5512 if (info->executable)
5514 s = bfd_get_section_by_name (dynobj, ".interp");
5515 BFD_ASSERT (s != NULL);
5516 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5517 s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
5521 memset (&gpinfo, 0, sizeof (gpinfo));
5522 gpinfo.g.info = info;
5524 for (;;)
5526 htab_t relocs = frvfdpic_relocs_info (info);
5528 htab_traverse (relocs, _frvfdpic_resolve_final_relocs_info, &relocs);
5530 if (relocs == frvfdpic_relocs_info (info))
5531 break;
5534 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_count_got_plt_entries,
5535 &gpinfo.g);
5537 /* Allocate space to save the summary information, we're going to
5538 use it if we're doing relaxations. */
5539 frvfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
5541 if (!_frvfdpic_size_got_plt (output_bfd, &gpinfo))
5542 return FALSE;
5544 if (elf_hash_table (info)->dynamic_sections_created)
5546 if (frvfdpic_got_section (info)->size)
5547 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0))
5548 return FALSE;
5550 if (frvfdpic_pltrel_section (info)->size)
5551 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
5552 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_REL)
5553 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
5554 return FALSE;
5556 if (frvfdpic_gotrel_section (info)->size)
5557 if (!_bfd_elf_add_dynamic_entry (info, DT_REL, 0)
5558 || !_bfd_elf_add_dynamic_entry (info, DT_RELSZ, 0)
5559 || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
5560 sizeof (Elf32_External_Rel)))
5561 return FALSE;
5564 return TRUE;
5567 static bfd_boolean
5568 elf32_frvfdpic_always_size_sections (bfd *output_bfd,
5569 struct bfd_link_info *info)
5571 if (!info->relocatable)
5573 struct elf_link_hash_entry *h;
5575 /* Force a PT_GNU_STACK segment to be created. */
5576 if (! elf_tdata (output_bfd)->stack_flags)
5577 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5579 /* Define __stacksize if it's not defined yet. */
5580 h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
5581 FALSE, FALSE, FALSE);
5582 if (! h || h->root.type != bfd_link_hash_defined
5583 || h->type != STT_OBJECT
5584 || !h->def_regular)
5586 struct bfd_link_hash_entry *bh = NULL;
5588 if (!(_bfd_generic_link_add_one_symbol
5589 (info, output_bfd, "__stacksize",
5590 BSF_GLOBAL, bfd_abs_section_ptr, DEFAULT_STACK_SIZE,
5591 (const char *) NULL, FALSE,
5592 get_elf_backend_data (output_bfd)->collect, &bh)))
5593 return FALSE;
5595 h = (struct elf_link_hash_entry *) bh;
5596 h->def_regular = 1;
5597 h->type = STT_OBJECT;
5598 /* This one must NOT be hidden. */
5602 return TRUE;
5605 /* Check whether any of the relocations was optimized away, and
5606 subtract it from the relocation or fixup count. */
5607 static bfd_boolean
5608 _frvfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
5609 struct bfd_link_info *info,
5611 bfd_boolean *changed)
5613 Elf_Internal_Shdr *symtab_hdr;
5614 struct elf_link_hash_entry **sym_hashes;
5615 Elf_Internal_Rela *rel, *erel;
5617 if ((sec->flags & SEC_RELOC) == 0
5618 || sec->reloc_count == 0)
5619 return TRUE;
5621 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5622 sym_hashes = elf_sym_hashes (abfd);
5624 rel = elf_section_data (sec)->relocs;
5626 /* Now examine each relocation. */
5627 for (erel = rel + sec->reloc_count; rel < erel; rel++)
5629 struct elf_link_hash_entry *h;
5630 unsigned long r_symndx;
5631 struct frvfdpic_relocs_info *picrel;
5632 struct _frvfdpic_dynamic_got_info *dinfo;
5634 if (ELF32_R_TYPE (rel->r_info) != R_FRV_32
5635 && ELF32_R_TYPE (rel->r_info) != R_FRV_FUNCDESC)
5636 continue;
5638 if (_bfd_elf_section_offset (sec->output_section->owner,
5639 info, sec, rel->r_offset)
5640 != (bfd_vma)-1)
5641 continue;
5643 r_symndx = ELF32_R_SYM (rel->r_info);
5644 if (r_symndx < symtab_hdr->sh_info)
5645 h = NULL;
5646 else
5648 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5649 while (h->root.type == bfd_link_hash_indirect
5650 || h->root.type == bfd_link_hash_warning)
5651 h = (struct elf_link_hash_entry *)h->root.u.i.link;
5654 if (h != NULL)
5655 picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
5656 abfd, h,
5657 rel->r_addend, NO_INSERT);
5658 else
5659 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info (info),
5660 abfd, r_symndx,
5661 rel->r_addend, NO_INSERT);
5663 if (! picrel)
5664 return FALSE;
5666 *changed = TRUE;
5667 dinfo = frvfdpic_dynamic_got_plt_info (info);
5669 _frvfdpic_count_relocs_fixups (picrel, dinfo, TRUE);
5670 if (ELF32_R_TYPE (rel->r_info) == R_FRV_32)
5671 picrel->relocs32--;
5672 else /* we know (ELF32_R_TYPE (rel->r_info) == R_FRV_FUNCDESC) */
5673 picrel->relocsfd--;
5674 _frvfdpic_count_relocs_fixups (picrel, dinfo, FALSE);
5677 return TRUE;
5680 static bfd_boolean
5681 frvfdpic_elf_discard_info (bfd *ibfd,
5682 struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
5683 struct bfd_link_info *info)
5685 bfd_boolean changed = FALSE;
5686 asection *s;
5687 bfd *obfd = NULL;
5689 /* Account for relaxation of .eh_frame section. */
5690 for (s = ibfd->sections; s; s = s->next)
5691 if (s->sec_info_type == ELF_INFO_TYPE_EH_FRAME)
5693 if (!_frvfdpic_check_discarded_relocs (ibfd, s, info, &changed))
5694 return FALSE;
5695 obfd = s->output_section->owner;
5698 if (changed)
5700 struct _frvfdpic_dynamic_got_plt_info gpinfo;
5702 memset (&gpinfo, 0, sizeof (gpinfo));
5703 memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info),
5704 sizeof (gpinfo.g));
5706 /* Clear GOT and PLT assignments. */
5707 htab_traverse (frvfdpic_relocs_info (info),
5708 _frvfdpic_reset_got_plt_entries,
5709 NULL);
5711 if (!_frvfdpic_size_got_plt (obfd, &gpinfo))
5712 return FALSE;
5715 return TRUE;
5718 /* Look for opportunities to relax TLS relocations. We can assume
5719 we're linking the main executable or a static-tls library, since
5720 otherwise we wouldn't have got here. */
5722 static int
5723 _frvfdpic_relax_got_plt_entries (void **entryp, void *dinfo_)
5725 struct frvfdpic_relocs_info *entry = *entryp;
5726 struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
5728 _frvfdpic_relax_tls_entries (entry, dinfo, TRUE);
5730 return 1;
5733 static bfd_boolean
5734 elf32_frvfdpic_relax_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
5735 struct bfd_link_info *info, bfd_boolean *again)
5737 struct _frvfdpic_dynamic_got_plt_info gpinfo;
5739 /* If we return early, we didn't change anything. */
5740 *again = FALSE;
5742 /* We'll do our thing when requested to relax the GOT section. */
5743 if (sec != frvfdpic_got_section (info))
5744 return TRUE;
5746 /* We can only relax when linking the main executable or a library
5747 that can't be dlopened. */
5748 if (! info->executable && ! (info->flags & DF_STATIC_TLS))
5749 return TRUE;
5751 /* If there isn't a TLS section for this binary, we can't do
5752 anything about its TLS relocations (it probably doesn't have
5753 any. */
5754 if (elf_hash_table (info)->tls_sec == NULL)
5755 return TRUE;
5757 memset (&gpinfo, 0, sizeof (gpinfo));
5758 memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info), sizeof (gpinfo.g));
5760 /* Now look for opportunities to relax, adjusting the GOT usage
5761 as needed. */
5762 htab_traverse (frvfdpic_relocs_info (info),
5763 _frvfdpic_relax_got_plt_entries,
5764 &gpinfo.g);
5766 /* If we changed anything, reset and re-assign GOT and PLT entries. */
5767 if (memcmp (frvfdpic_dynamic_got_plt_info (info),
5768 &gpinfo.g, sizeof (gpinfo.g)) != 0)
5770 /* Clear GOT and PLT assignments. */
5771 htab_traverse (frvfdpic_relocs_info (info),
5772 _frvfdpic_reset_got_plt_entries,
5773 NULL);
5775 /* The owner of the TLS section is the output bfd. There should
5776 be a better way to get to it. */
5777 if (!_frvfdpic_size_got_plt (elf_hash_table (info)->tls_sec->owner,
5778 &gpinfo))
5779 return FALSE;
5781 /* Repeat until we don't make any further changes. We could fail to
5782 introduce changes in a round if, for example, the 12-bit range is
5783 full, but we later release some space by getting rid of TLS
5784 descriptors in it. We have to repeat the whole process because
5785 we might have changed the size of a section processed before this
5786 one. */
5787 *again = TRUE;
5790 return TRUE;
5793 static bfd_boolean
5794 elf32_frvfdpic_modify_program_headers (bfd *output_bfd,
5795 struct bfd_link_info *info)
5797 struct elf_obj_tdata *tdata = elf_tdata (output_bfd);
5798 struct elf_segment_map *m;
5799 Elf_Internal_Phdr *p;
5801 /* objcopy and strip preserve what's already there using
5802 elf32_frvfdpic_copy_private_bfd_data (). */
5803 if (! info)
5804 return TRUE;
5806 for (p = tdata->phdr, m = tdata->segment_map; m != NULL; m = m->next, p++)
5807 if (m->p_type == PT_GNU_STACK)
5808 break;
5810 if (m)
5812 struct elf_link_hash_entry *h;
5814 /* Obtain the pointer to the __stacksize symbol. */
5815 h = elf_link_hash_lookup (elf_hash_table (info), "__stacksize",
5816 FALSE, FALSE, FALSE);
5817 if (h)
5819 while (h->root.type == bfd_link_hash_indirect
5820 || h->root.type == bfd_link_hash_warning)
5821 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5822 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
5825 /* Set the header p_memsz from the symbol value. We
5826 intentionally ignore the symbol section. */
5827 if (h && h->root.type == bfd_link_hash_defined)
5828 p->p_memsz = h->root.u.def.value;
5829 else
5830 p->p_memsz = DEFAULT_STACK_SIZE;
5832 p->p_align = 8;
5835 return TRUE;
5838 /* Fill in code and data in dynamic sections. */
5840 static bfd_boolean
5841 elf32_frv_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5842 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5844 /* Nothing to be done for non-FDPIC. */
5845 return TRUE;
5848 static bfd_boolean
5849 elf32_frvfdpic_finish_dynamic_sections (bfd *output_bfd,
5850 struct bfd_link_info *info)
5852 bfd *dynobj;
5853 asection *sdyn;
5855 dynobj = elf_hash_table (info)->dynobj;
5857 if (frvfdpic_dynamic_got_plt_info (info))
5859 BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs == 0);
5861 if (frvfdpic_got_section (info))
5863 BFD_ASSERT (frvfdpic_gotrel_section (info)->size
5864 == (frvfdpic_gotrel_section (info)->reloc_count
5865 * sizeof (Elf32_External_Rel)));
5867 if (frvfdpic_gotfixup_section (info))
5869 struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
5870 bfd_vma got_value = hgot->root.u.def.value
5871 + hgot->root.u.def.section->output_section->vma
5872 + hgot->root.u.def.section->output_offset;
5873 struct bfd_link_hash_entry *hend;
5875 _frvfdpic_add_rofixup (output_bfd, frvfdpic_gotfixup_section (info),
5876 got_value, 0);
5878 if (frvfdpic_gotfixup_section (info)->size
5879 != (frvfdpic_gotfixup_section (info)->reloc_count * 4))
5881 error:
5882 (*_bfd_error_handler)
5883 ("LINKER BUG: .rofixup section size mismatch");
5884 return FALSE;
5887 hend = bfd_link_hash_lookup (info->hash, "__ROFIXUP_END__",
5888 FALSE, FALSE, TRUE);
5889 if (hend
5890 && (hend->type == bfd_link_hash_defined
5891 || hend->type == bfd_link_hash_defweak))
5893 bfd_vma value =
5894 frvfdpic_gotfixup_section (info)->output_section->vma
5895 + frvfdpic_gotfixup_section (info)->output_offset
5896 + frvfdpic_gotfixup_section (info)->size
5897 - hend->u.def.section->output_section->vma
5898 - hend->u.def.section->output_offset;
5899 BFD_ASSERT (hend->u.def.value == value);
5900 if (hend->u.def.value != value)
5901 goto error;
5905 if (frvfdpic_pltrel_section (info))
5907 BFD_ASSERT (frvfdpic_pltrel_section (info)->size
5908 == (frvfdpic_pltrel_section (info)->reloc_count
5909 * sizeof (Elf32_External_Rel)));
5913 if (elf_hash_table (info)->dynamic_sections_created)
5915 Elf32_External_Dyn * dyncon;
5916 Elf32_External_Dyn * dynconend;
5918 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5920 BFD_ASSERT (sdyn != NULL);
5922 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5923 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5925 for (; dyncon < dynconend; dyncon++)
5927 Elf_Internal_Dyn dyn;
5929 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5931 switch (dyn.d_tag)
5933 default:
5934 break;
5936 case DT_PLTGOT:
5937 dyn.d_un.d_ptr = frvfdpic_got_section (info)->output_section->vma
5938 + frvfdpic_got_section (info)->output_offset
5939 + frvfdpic_got_initial_offset (info);
5940 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5941 break;
5943 case DT_JMPREL:
5944 dyn.d_un.d_ptr = frvfdpic_pltrel_section (info)
5945 ->output_section->vma
5946 + frvfdpic_pltrel_section (info)->output_offset;
5947 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5948 break;
5950 case DT_PLTRELSZ:
5951 dyn.d_un.d_val = frvfdpic_pltrel_section (info)->size;
5952 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5953 break;
5958 return TRUE;
5961 /* Adjust a symbol defined by a dynamic object and referenced by a
5962 regular object. */
5964 static bfd_boolean
5965 elf32_frvfdpic_adjust_dynamic_symbol
5966 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5967 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5969 bfd * dynobj;
5971 dynobj = elf_hash_table (info)->dynobj;
5973 /* Make sure we know what is going on here. */
5974 BFD_ASSERT (dynobj != NULL
5975 && (h->u.weakdef != NULL
5976 || (h->def_dynamic
5977 && h->ref_regular
5978 && !h->def_regular)));
5980 /* If this is a weak symbol, and there is a real definition, the
5981 processor independent code will have arranged for us to see the
5982 real definition first, and we can just use the same value. */
5983 if (h->u.weakdef != NULL)
5985 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5986 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5987 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5988 h->root.u.def.value = h->u.weakdef->root.u.def.value;
5991 return TRUE;
5994 /* Perform any actions needed for dynamic symbols. */
5996 static bfd_boolean
5997 elf32_frvfdpic_finish_dynamic_symbol
5998 (bfd *output_bfd ATTRIBUTE_UNUSED,
5999 struct bfd_link_info *info ATTRIBUTE_UNUSED,
6000 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
6001 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
6003 return TRUE;
6006 /* Decide whether to attempt to turn absptr or lsda encodings in
6007 shared libraries into pcrel within the given input section. */
6009 static bfd_boolean
6010 frvfdpic_elf_use_relative_eh_frame
6011 (bfd *input_bfd ATTRIBUTE_UNUSED,
6012 struct bfd_link_info *info ATTRIBUTE_UNUSED,
6013 asection *eh_frame_section ATTRIBUTE_UNUSED)
6015 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
6016 return FALSE;
6019 /* Adjust the contents of an eh_frame_hdr section before they're output. */
6021 static bfd_byte
6022 frvfdpic_elf_encode_eh_address (bfd *abfd,
6023 struct bfd_link_info *info,
6024 asection *osec, bfd_vma offset,
6025 asection *loc_sec, bfd_vma loc_offset,
6026 bfd_vma *encoded)
6028 struct elf_link_hash_entry *h;
6030 h = elf_hash_table (info)->hgot;
6031 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
6033 if (! h || (_frvfdpic_osec_to_segment (abfd, osec)
6034 == _frvfdpic_osec_to_segment (abfd, loc_sec->output_section)))
6035 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
6036 loc_sec, loc_offset, encoded);
6038 BFD_ASSERT (_frvfdpic_osec_to_segment (abfd, osec)
6039 == (_frvfdpic_osec_to_segment
6040 (abfd, h->root.u.def.section->output_section)));
6042 *encoded = osec->vma + offset
6043 - (h->root.u.def.value
6044 + h->root.u.def.section->output_section->vma
6045 + h->root.u.def.section->output_offset);
6047 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
6050 /* Look through the relocs for a section during the first phase.
6052 Besides handling virtual table relocs for gc, we have to deal with
6053 all sorts of PIC-related relocations. We describe below the
6054 general plan on how to handle such relocations, even though we only
6055 collect information at this point, storing them in hash tables for
6056 perusal of later passes.
6058 32 relocations are propagated to the linker output when creating
6059 position-independent output. LO16 and HI16 relocations are not
6060 supposed to be encountered in this case.
6062 LABEL16 should always be resolvable by the linker, since it's only
6063 used by branches.
6065 LABEL24, on the other hand, is used by calls. If it turns out that
6066 the target of a call is a dynamic symbol, a PLT entry must be
6067 created for it, which triggers the creation of a private function
6068 descriptor and, unless lazy binding is disabled, a lazy PLT entry.
6070 GPREL relocations require the referenced symbol to be in the same
6071 segment as _gp, but this can only be checked later.
6073 All GOT, GOTOFF and FUNCDESC relocations require a .got section to
6074 exist. LABEL24 might as well, since it may require a PLT entry,
6075 that will require a got.
6077 Non-FUNCDESC GOT relocations require a GOT entry to be created
6078 regardless of whether the symbol is dynamic. However, since a
6079 global symbol that turns out to not be exported may have the same
6080 address of a non-dynamic symbol, we don't assign GOT entries at
6081 this point, such that we can share them in this case. A relocation
6082 for the GOT entry always has to be created, be it to offset a
6083 private symbol by the section load address, be it to get the symbol
6084 resolved dynamically.
6086 FUNCDESC GOT relocations require a GOT entry to be created, and
6087 handled as if a FUNCDESC relocation was applied to the GOT entry in
6088 an object file.
6090 FUNCDESC relocations referencing a symbol that turns out to NOT be
6091 dynamic cause a private function descriptor to be created. The
6092 FUNCDESC relocation then decays to a 32 relocation that points at
6093 the private descriptor. If the symbol is dynamic, the FUNCDESC
6094 relocation is propagated to the linker output, such that the
6095 dynamic linker creates the canonical descriptor, pointing to the
6096 dynamically-resolved definition of the function.
6098 Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
6099 symbols that are assigned to the same segment as the GOT, but we
6100 can only check this later, after we know the complete set of
6101 symbols defined and/or exported.
6103 FUNCDESC GOTOFF relocations require a function descriptor to be
6104 created and, unless lazy binding is disabled or the symbol is not
6105 dynamic, a lazy PLT entry. Since we can't tell at this point
6106 whether a symbol is going to be dynamic, we have to decide later
6107 whether to create a lazy PLT entry or bind the descriptor directly
6108 to the private function.
6110 FUNCDESC_VALUE relocations are not supposed to be present in object
6111 files, but they may very well be simply propagated to the linker
6112 output, since they have no side effect.
6115 A function descriptor always requires a FUNCDESC_VALUE relocation.
6116 Whether it's in .plt.rel or not depends on whether lazy binding is
6117 enabled and on whether the referenced symbol is dynamic.
6119 The existence of a lazy PLT requires the resolverStub lazy PLT
6120 entry to be present.
6123 As for assignment of GOT, PLT and lazy PLT entries, and private
6124 descriptors, we might do them all sequentially, but we can do
6125 better than that. For example, we can place GOT entries and
6126 private function descriptors referenced using 12-bit operands
6127 closer to the PIC register value, such that these relocations don't
6128 overflow. Those that are only referenced with LO16 relocations
6129 could come next, but we may as well place PLT-required function
6130 descriptors in the 12-bit range to make them shorter. Symbols
6131 referenced with LO16/HI16 may come next, but we may place
6132 additional function descriptors in the 16-bit range if we can
6133 reliably tell that we've already placed entries that are ever
6134 referenced with only LO16. PLT entries are therefore generated as
6135 small as possible, while not introducing relocation overflows in
6136 GOT or FUNCDESC_GOTOFF relocations. Lazy PLT entries could be
6137 generated before or after PLT entries, but not intermingled with
6138 them, such that we can have more lazy PLT entries in range for a
6139 branch to the resolverStub. The resolverStub should be emitted at
6140 the most distant location from the first lazy PLT entry such that
6141 it's still in range for a branch, or closer, if there isn't a need
6142 for so many lazy PLT entries. Additional lazy PLT entries may be
6143 emitted after the resolverStub, as long as branches are still in
6144 range. If the branch goes out of range, longer lazy PLT entries
6145 are emitted.
6147 We could further optimize PLT and lazy PLT entries by giving them
6148 priority in assignment to closer-to-gr17 locations depending on the
6149 number of occurrences of references to them (assuming a function
6150 that's called more often is more important for performance, so its
6151 PLT entry should be faster), or taking hints from the compiler.
6152 Given infinite time and money... :-) */
6154 static bfd_boolean
6155 elf32_frv_check_relocs (abfd, info, sec, relocs)
6156 bfd *abfd;
6157 struct bfd_link_info *info;
6158 asection *sec;
6159 const Elf_Internal_Rela *relocs;
6161 Elf_Internal_Shdr *symtab_hdr;
6162 struct elf_link_hash_entry **sym_hashes;
6163 const Elf_Internal_Rela *rel;
6164 const Elf_Internal_Rela *rel_end;
6165 bfd *dynobj;
6166 struct frvfdpic_relocs_info *picrel;
6168 if (info->relocatable)
6169 return TRUE;
6171 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6172 sym_hashes = elf_sym_hashes (abfd);
6174 dynobj = elf_hash_table (info)->dynobj;
6175 rel_end = relocs + sec->reloc_count;
6176 for (rel = relocs; rel < rel_end; rel++)
6178 struct elf_link_hash_entry *h;
6179 unsigned long r_symndx;
6181 r_symndx = ELF32_R_SYM (rel->r_info);
6182 if (r_symndx < symtab_hdr->sh_info)
6183 h = NULL;
6184 else
6186 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6187 while (h->root.type == bfd_link_hash_indirect
6188 || h->root.type == bfd_link_hash_warning)
6189 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6192 switch (ELF32_R_TYPE (rel->r_info))
6194 case R_FRV_GETTLSOFF:
6195 case R_FRV_TLSDESC_VALUE:
6196 case R_FRV_GOTTLSDESC12:
6197 case R_FRV_GOTTLSDESCHI:
6198 case R_FRV_GOTTLSDESCLO:
6199 case R_FRV_GOTTLSOFF12:
6200 case R_FRV_GOTTLSOFFHI:
6201 case R_FRV_GOTTLSOFFLO:
6202 case R_FRV_TLSOFF:
6203 case R_FRV_GOT12:
6204 case R_FRV_GOTHI:
6205 case R_FRV_GOTLO:
6206 case R_FRV_FUNCDESC_GOT12:
6207 case R_FRV_FUNCDESC_GOTHI:
6208 case R_FRV_FUNCDESC_GOTLO:
6209 case R_FRV_GOTOFF12:
6210 case R_FRV_GOTOFFHI:
6211 case R_FRV_GOTOFFLO:
6212 case R_FRV_FUNCDESC_GOTOFF12:
6213 case R_FRV_FUNCDESC_GOTOFFHI:
6214 case R_FRV_FUNCDESC_GOTOFFLO:
6215 case R_FRV_FUNCDESC:
6216 case R_FRV_FUNCDESC_VALUE:
6217 case R_FRV_TLSMOFF12:
6218 case R_FRV_TLSMOFFHI:
6219 case R_FRV_TLSMOFFLO:
6220 case R_FRV_TLSMOFF:
6221 if (! IS_FDPIC (abfd))
6222 goto bad_reloc;
6223 /* Fall through. */
6224 case R_FRV_GPREL12:
6225 case R_FRV_GPRELU12:
6226 case R_FRV_GPRELHI:
6227 case R_FRV_GPRELLO:
6228 case R_FRV_LABEL24:
6229 case R_FRV_32:
6230 if (! dynobj)
6232 elf_hash_table (info)->dynobj = dynobj = abfd;
6233 if (! _frv_create_got_section (abfd, info))
6234 return FALSE;
6236 if (! IS_FDPIC (abfd))
6238 picrel = NULL;
6239 break;
6241 if (h != NULL)
6243 if (h->dynindx == -1)
6244 switch (ELF_ST_VISIBILITY (h->other))
6246 case STV_INTERNAL:
6247 case STV_HIDDEN:
6248 break;
6249 default:
6250 bfd_elf_link_record_dynamic_symbol (info, h);
6251 break;
6253 picrel
6254 = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
6255 abfd, h,
6256 rel->r_addend, INSERT);
6258 else
6259 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
6260 (info), abfd, r_symndx,
6261 rel->r_addend, INSERT);
6262 if (! picrel)
6263 return FALSE;
6264 break;
6266 default:
6267 picrel = NULL;
6268 break;
6271 switch (ELF32_R_TYPE (rel->r_info))
6273 case R_FRV_LABEL24:
6274 if (IS_FDPIC (abfd))
6275 picrel->call = 1;
6276 break;
6278 case R_FRV_FUNCDESC_VALUE:
6279 picrel->relocsfdv++;
6280 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
6281 picrel->relocs32--;
6282 /* Fall through. */
6284 case R_FRV_32:
6285 if (! IS_FDPIC (abfd))
6286 break;
6288 picrel->sym = 1;
6289 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
6290 picrel->relocs32++;
6291 break;
6293 case R_FRV_GOT12:
6294 picrel->got12 = 1;
6295 break;
6297 case R_FRV_GOTHI:
6298 case R_FRV_GOTLO:
6299 picrel->gothilo = 1;
6300 break;
6302 case R_FRV_FUNCDESC_GOT12:
6303 picrel->fdgot12 = 1;
6304 break;
6306 case R_FRV_FUNCDESC_GOTHI:
6307 case R_FRV_FUNCDESC_GOTLO:
6308 picrel->fdgothilo = 1;
6309 break;
6311 case R_FRV_GOTOFF12:
6312 case R_FRV_GOTOFFHI:
6313 case R_FRV_GOTOFFLO:
6314 picrel->gotoff = 1;
6315 break;
6317 case R_FRV_FUNCDESC_GOTOFF12:
6318 picrel->fdgoff12 = 1;
6319 break;
6321 case R_FRV_FUNCDESC_GOTOFFHI:
6322 case R_FRV_FUNCDESC_GOTOFFLO:
6323 picrel->fdgoffhilo = 1;
6324 break;
6326 case R_FRV_FUNCDESC:
6327 picrel->fd = 1;
6328 picrel->relocsfd++;
6329 break;
6331 case R_FRV_GETTLSOFF:
6332 picrel->tlsplt = 1;
6333 break;
6335 case R_FRV_TLSDESC_VALUE:
6336 picrel->relocstlsd++;
6337 goto bad_reloc;
6339 case R_FRV_GOTTLSDESC12:
6340 picrel->tlsdesc12 = 1;
6341 break;
6343 case R_FRV_GOTTLSDESCHI:
6344 case R_FRV_GOTTLSDESCLO:
6345 picrel->tlsdeschilo = 1;
6346 break;
6348 case R_FRV_TLSMOFF12:
6349 case R_FRV_TLSMOFFHI:
6350 case R_FRV_TLSMOFFLO:
6351 case R_FRV_TLSMOFF:
6352 break;
6354 case R_FRV_GOTTLSOFF12:
6355 picrel->tlsoff12 = 1;
6356 info->flags |= DF_STATIC_TLS;
6357 break;
6359 case R_FRV_GOTTLSOFFHI:
6360 case R_FRV_GOTTLSOFFLO:
6361 picrel->tlsoffhilo = 1;
6362 info->flags |= DF_STATIC_TLS;
6363 break;
6365 case R_FRV_TLSOFF:
6366 picrel->relocstlsoff++;
6367 info->flags |= DF_STATIC_TLS;
6368 goto bad_reloc;
6370 /* This relocation describes the C++ object vtable hierarchy.
6371 Reconstruct it for later use during GC. */
6372 case R_FRV_GNU_VTINHERIT:
6373 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6374 return FALSE;
6375 break;
6377 /* This relocation describes which C++ vtable entries are actually
6378 used. Record for later use during GC. */
6379 case R_FRV_GNU_VTENTRY:
6380 BFD_ASSERT (h != NULL);
6381 if (h != NULL
6382 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6383 return FALSE;
6384 break;
6386 case R_FRV_LABEL16:
6387 case R_FRV_LO16:
6388 case R_FRV_HI16:
6389 case R_FRV_GPREL12:
6390 case R_FRV_GPRELU12:
6391 case R_FRV_GPREL32:
6392 case R_FRV_GPRELHI:
6393 case R_FRV_GPRELLO:
6394 case R_FRV_TLSDESC_RELAX:
6395 case R_FRV_GETTLSOFF_RELAX:
6396 case R_FRV_TLSOFF_RELAX:
6397 break;
6399 default:
6400 bad_reloc:
6401 (*_bfd_error_handler)
6402 (_("%B: unsupported relocation type %i"),
6403 abfd, ELF32_R_TYPE (rel->r_info));
6404 return FALSE;
6408 return TRUE;
6412 /* Return the machine subcode from the ELF e_flags header. */
6414 static int
6415 elf32_frv_machine (abfd)
6416 bfd *abfd;
6418 switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
6420 default: break;
6421 case EF_FRV_CPU_FR550: return bfd_mach_fr550;
6422 case EF_FRV_CPU_FR500: return bfd_mach_fr500;
6423 case EF_FRV_CPU_FR450: return bfd_mach_fr450;
6424 case EF_FRV_CPU_FR405: return bfd_mach_fr400;
6425 case EF_FRV_CPU_FR400: return bfd_mach_fr400;
6426 case EF_FRV_CPU_FR300: return bfd_mach_fr300;
6427 case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
6428 case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
6431 return bfd_mach_frv;
6434 /* Set the right machine number for a FRV ELF file. */
6436 static bfd_boolean
6437 elf32_frv_object_p (abfd)
6438 bfd *abfd;
6440 bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
6441 return (((elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC) != 0)
6442 == (IS_FDPIC (abfd)));
6445 /* Function to set the ELF flag bits. */
6447 static bfd_boolean
6448 frv_elf_set_private_flags (abfd, flags)
6449 bfd *abfd;
6450 flagword flags;
6452 elf_elfheader (abfd)->e_flags = flags;
6453 elf_flags_init (abfd) = TRUE;
6454 return TRUE;
6457 /* Copy backend specific data from one object module to another. */
6459 static bfd_boolean
6460 frv_elf_copy_private_bfd_data (ibfd, obfd)
6461 bfd *ibfd;
6462 bfd *obfd;
6464 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6465 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6466 return TRUE;
6468 BFD_ASSERT (!elf_flags_init (obfd)
6469 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
6471 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6472 elf_flags_init (obfd) = TRUE;
6474 /* Copy object attributes. */
6475 _bfd_elf_copy_obj_attributes (ibfd, obfd);
6477 return TRUE;
6480 /* Return true if the architecture described by elf header flag
6481 EXTENSION is an extension of the architecture described by BASE. */
6483 static bfd_boolean
6484 frv_elf_arch_extension_p (flagword base, flagword extension)
6486 if (base == extension)
6487 return TRUE;
6489 /* CPU_GENERIC code can be merged with code for a specific
6490 architecture, in which case the result is marked as being
6491 for the specific architecture. Everything is therefore
6492 an extension of CPU_GENERIC. */
6493 if (base == EF_FRV_CPU_GENERIC)
6494 return TRUE;
6496 if (extension == EF_FRV_CPU_FR450)
6497 if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
6498 return TRUE;
6500 if (extension == EF_FRV_CPU_FR405)
6501 if (base == EF_FRV_CPU_FR400)
6502 return TRUE;
6504 return FALSE;
6507 static bfd_boolean
6508 elf32_frvfdpic_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6510 unsigned i;
6512 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6513 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6514 return TRUE;
6516 if (! frv_elf_copy_private_bfd_data (ibfd, obfd))
6517 return FALSE;
6519 if (! elf_tdata (ibfd) || ! elf_tdata (ibfd)->phdr
6520 || ! elf_tdata (obfd) || ! elf_tdata (obfd)->phdr)
6521 return TRUE;
6523 /* Copy the stack size. */
6524 for (i = 0; i < elf_elfheader (ibfd)->e_phnum; i++)
6525 if (elf_tdata (ibfd)->phdr[i].p_type == PT_GNU_STACK)
6527 Elf_Internal_Phdr *iphdr = &elf_tdata (ibfd)->phdr[i];
6529 for (i = 0; i < elf_elfheader (obfd)->e_phnum; i++)
6530 if (elf_tdata (obfd)->phdr[i].p_type == PT_GNU_STACK)
6532 memcpy (&elf_tdata (obfd)->phdr[i], iphdr, sizeof (*iphdr));
6534 /* Rewrite the phdrs, since we're only called after they
6535 were first written. */
6536 if (bfd_seek (obfd, (bfd_signed_vma) get_elf_backend_data (obfd)
6537 ->s->sizeof_ehdr, SEEK_SET) != 0
6538 || get_elf_backend_data (obfd)->s
6539 ->write_out_phdrs (obfd, elf_tdata (obfd)->phdr,
6540 elf_elfheader (obfd)->e_phnum) != 0)
6541 return FALSE;
6542 break;
6545 break;
6548 return TRUE;
6551 /* Merge backend specific data from an object file to the output
6552 object file when linking. */
6554 static bfd_boolean
6555 frv_elf_merge_private_bfd_data (ibfd, obfd)
6556 bfd *ibfd;
6557 bfd *obfd;
6559 flagword old_flags, old_partial;
6560 flagword new_flags, new_partial;
6561 bfd_boolean error = FALSE;
6562 char new_opt[80];
6563 char old_opt[80];
6565 new_opt[0] = old_opt[0] = '\0';
6566 new_flags = elf_elfheader (ibfd)->e_flags;
6567 old_flags = elf_elfheader (obfd)->e_flags;
6569 if (new_flags & EF_FRV_FDPIC)
6570 new_flags &= ~EF_FRV_PIC;
6572 #ifdef DEBUG
6573 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
6574 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
6575 bfd_get_filename (ibfd));
6576 #endif
6578 if (!elf_flags_init (obfd)) /* First call, no flags set. */
6580 elf_flags_init (obfd) = TRUE;
6581 old_flags = new_flags;
6584 else if (new_flags == old_flags) /* Compatible flags are ok. */
6587 else /* Possibly incompatible flags. */
6589 /* Warn if different # of gprs are used. Note, 0 means nothing is
6590 said about the size of gprs. */
6591 new_partial = (new_flags & EF_FRV_GPR_MASK);
6592 old_partial = (old_flags & EF_FRV_GPR_MASK);
6593 if (new_partial == old_partial)
6596 else if (new_partial == 0)
6599 else if (old_partial == 0)
6600 old_flags |= new_partial;
6602 else
6604 switch (new_partial)
6606 default: strcat (new_opt, " -mgpr-??"); break;
6607 case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
6608 case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
6611 switch (old_partial)
6613 default: strcat (old_opt, " -mgpr-??"); break;
6614 case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
6615 case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
6619 /* Warn if different # of fprs are used. Note, 0 means nothing is
6620 said about the size of fprs. */
6621 new_partial = (new_flags & EF_FRV_FPR_MASK);
6622 old_partial = (old_flags & EF_FRV_FPR_MASK);
6623 if (new_partial == old_partial)
6626 else if (new_partial == 0)
6629 else if (old_partial == 0)
6630 old_flags |= new_partial;
6632 else
6634 switch (new_partial)
6636 default: strcat (new_opt, " -mfpr-?"); break;
6637 case EF_FRV_FPR_32: strcat (new_opt, " -mfpr-32"); break;
6638 case EF_FRV_FPR_64: strcat (new_opt, " -mfpr-64"); break;
6639 case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
6642 switch (old_partial)
6644 default: strcat (old_opt, " -mfpr-?"); break;
6645 case EF_FRV_FPR_32: strcat (old_opt, " -mfpr-32"); break;
6646 case EF_FRV_FPR_64: strcat (old_opt, " -mfpr-64"); break;
6647 case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
6651 /* Warn if different dword support was used. Note, 0 means nothing is
6652 said about the dword support. */
6653 new_partial = (new_flags & EF_FRV_DWORD_MASK);
6654 old_partial = (old_flags & EF_FRV_DWORD_MASK);
6655 if (new_partial == old_partial)
6658 else if (new_partial == 0)
6661 else if (old_partial == 0)
6662 old_flags |= new_partial;
6664 else
6666 switch (new_partial)
6668 default: strcat (new_opt, " -mdword-?"); break;
6669 case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword"); break;
6670 case EF_FRV_DWORD_NO: strcat (new_opt, " -mno-dword"); break;
6673 switch (old_partial)
6675 default: strcat (old_opt, " -mdword-?"); break;
6676 case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword"); break;
6677 case EF_FRV_DWORD_NO: strcat (old_opt, " -mno-dword"); break;
6681 /* Or in flags that accumulate (ie, if one module uses it, mark that the
6682 feature is used. */
6683 old_flags |= new_flags & (EF_FRV_DOUBLE
6684 | EF_FRV_MEDIA
6685 | EF_FRV_MULADD
6686 | EF_FRV_NON_PIC_RELOCS);
6688 /* If any module was compiled without -G0, clear the G0 bit. */
6689 old_flags = ((old_flags & ~ EF_FRV_G0)
6690 | (old_flags & new_flags & EF_FRV_G0));
6692 /* If any module was compiled without -mnopack, clear the mnopack bit. */
6693 old_flags = ((old_flags & ~ EF_FRV_NOPACK)
6694 | (old_flags & new_flags & EF_FRV_NOPACK));
6696 /* We don't have to do anything if the pic flags are the same, or the new
6697 module(s) were compiled with -mlibrary-pic. */
6698 new_partial = (new_flags & EF_FRV_PIC_FLAGS);
6699 old_partial = (old_flags & EF_FRV_PIC_FLAGS);
6700 if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
6703 /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
6704 flags if any from the new module. */
6705 else if ((old_partial & EF_FRV_LIBPIC) != 0)
6706 old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
6708 /* If we have mixtures of -fpic and -fPIC, or in both bits. */
6709 else if (new_partial != 0 && old_partial != 0)
6710 old_flags |= new_partial;
6712 /* One module was compiled for pic and the other was not, see if we have
6713 had any relocations that are not pic-safe. */
6714 else
6716 if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
6717 old_flags |= new_partial;
6718 else
6720 old_flags &= ~ EF_FRV_PIC_FLAGS;
6721 #ifndef FRV_NO_PIC_ERROR
6722 error = TRUE;
6723 (*_bfd_error_handler)
6724 (_("%s: compiled with %s and linked with modules that use non-pic relocations"),
6725 bfd_get_filename (ibfd),
6726 (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
6727 #endif
6731 /* Warn if different cpu is used (allow a specific cpu to override
6732 the generic cpu). */
6733 new_partial = (new_flags & EF_FRV_CPU_MASK);
6734 old_partial = (old_flags & EF_FRV_CPU_MASK);
6735 if (frv_elf_arch_extension_p (new_partial, old_partial))
6738 else if (frv_elf_arch_extension_p (old_partial, new_partial))
6739 old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
6741 else
6743 switch (new_partial)
6745 default: strcat (new_opt, " -mcpu=?"); break;
6746 case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv"); break;
6747 case EF_FRV_CPU_SIMPLE: strcat (new_opt, " -mcpu=simple"); break;
6748 case EF_FRV_CPU_FR550: strcat (new_opt, " -mcpu=fr550"); break;
6749 case EF_FRV_CPU_FR500: strcat (new_opt, " -mcpu=fr500"); break;
6750 case EF_FRV_CPU_FR450: strcat (new_opt, " -mcpu=fr450"); break;
6751 case EF_FRV_CPU_FR405: strcat (new_opt, " -mcpu=fr405"); break;
6752 case EF_FRV_CPU_FR400: strcat (new_opt, " -mcpu=fr400"); break;
6753 case EF_FRV_CPU_FR300: strcat (new_opt, " -mcpu=fr300"); break;
6754 case EF_FRV_CPU_TOMCAT: strcat (new_opt, " -mcpu=tomcat"); break;
6757 switch (old_partial)
6759 default: strcat (old_opt, " -mcpu=?"); break;
6760 case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv"); break;
6761 case EF_FRV_CPU_SIMPLE: strcat (old_opt, " -mcpu=simple"); break;
6762 case EF_FRV_CPU_FR550: strcat (old_opt, " -mcpu=fr550"); break;
6763 case EF_FRV_CPU_FR500: strcat (old_opt, " -mcpu=fr500"); break;
6764 case EF_FRV_CPU_FR450: strcat (old_opt, " -mcpu=fr450"); break;
6765 case EF_FRV_CPU_FR405: strcat (old_opt, " -mcpu=fr405"); break;
6766 case EF_FRV_CPU_FR400: strcat (old_opt, " -mcpu=fr400"); break;
6767 case EF_FRV_CPU_FR300: strcat (old_opt, " -mcpu=fr300"); break;
6768 case EF_FRV_CPU_TOMCAT: strcat (old_opt, " -mcpu=tomcat"); break;
6772 /* Print out any mismatches from above. */
6773 if (new_opt[0])
6775 error = TRUE;
6776 (*_bfd_error_handler)
6777 (_("%s: compiled with %s and linked with modules compiled with %s"),
6778 bfd_get_filename (ibfd), new_opt, old_opt);
6781 /* Warn about any other mismatches */
6782 new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
6783 old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
6784 if (new_partial != old_partial)
6786 old_flags |= new_partial;
6787 error = TRUE;
6788 (*_bfd_error_handler)
6789 (_("%s: uses different unknown e_flags (0x%lx) fields than previous modules (0x%lx)"),
6790 bfd_get_filename (ibfd), (long)new_partial, (long)old_partial);
6794 /* If the cpu is -mcpu=simple, then set the -mnopack bit. */
6795 if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
6796 old_flags |= EF_FRV_NOPACK;
6798 /* Update the old flags now with changes made above. */
6799 old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
6800 elf_elfheader (obfd)->e_flags = old_flags;
6801 if (old_partial != (old_flags & EF_FRV_CPU_MASK))
6802 bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
6804 if (((new_flags & EF_FRV_FDPIC) == 0)
6805 != (! IS_FDPIC (ibfd)))
6807 error = TRUE;
6808 if (IS_FDPIC (obfd))
6809 (*_bfd_error_handler)
6810 (_("%s: cannot link non-fdpic object file into fdpic executable"),
6811 bfd_get_filename (ibfd));
6812 else
6813 (*_bfd_error_handler)
6814 (_("%s: cannot link fdpic object file into non-fdpic executable"),
6815 bfd_get_filename (ibfd));
6818 if (error)
6819 bfd_set_error (bfd_error_bad_value);
6821 return !error;
6825 bfd_boolean
6826 frv_elf_print_private_bfd_data (abfd, ptr)
6827 bfd *abfd;
6828 PTR ptr;
6830 FILE *file = (FILE *) ptr;
6831 flagword flags;
6833 BFD_ASSERT (abfd != NULL && ptr != NULL);
6835 /* Print normal ELF private data. */
6836 _bfd_elf_print_private_bfd_data (abfd, ptr);
6838 flags = elf_elfheader (abfd)->e_flags;
6839 fprintf (file, _("private flags = 0x%lx:"), (long)flags);
6841 switch (flags & EF_FRV_CPU_MASK)
6843 default: break;
6844 case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple"); break;
6845 case EF_FRV_CPU_FR550: fprintf (file, " -mcpu=fr550"); break;
6846 case EF_FRV_CPU_FR500: fprintf (file, " -mcpu=fr500"); break;
6847 case EF_FRV_CPU_FR450: fprintf (file, " -mcpu=fr450"); break;
6848 case EF_FRV_CPU_FR405: fprintf (file, " -mcpu=fr405"); break;
6849 case EF_FRV_CPU_FR400: fprintf (file, " -mcpu=fr400"); break;
6850 case EF_FRV_CPU_FR300: fprintf (file, " -mcpu=fr300"); break;
6851 case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat"); break;
6854 switch (flags & EF_FRV_GPR_MASK)
6856 default: break;
6857 case EF_FRV_GPR_32: fprintf (file, " -mgpr-32"); break;
6858 case EF_FRV_GPR_64: fprintf (file, " -mgpr-64"); break;
6861 switch (flags & EF_FRV_FPR_MASK)
6863 default: break;
6864 case EF_FRV_FPR_32: fprintf (file, " -mfpr-32"); break;
6865 case EF_FRV_FPR_64: fprintf (file, " -mfpr-64"); break;
6866 case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float"); break;
6869 switch (flags & EF_FRV_DWORD_MASK)
6871 default: break;
6872 case EF_FRV_DWORD_YES: fprintf (file, " -mdword"); break;
6873 case EF_FRV_DWORD_NO: fprintf (file, " -mno-dword"); break;
6876 if (flags & EF_FRV_DOUBLE)
6877 fprintf (file, " -mdouble");
6879 if (flags & EF_FRV_MEDIA)
6880 fprintf (file, " -mmedia");
6882 if (flags & EF_FRV_MULADD)
6883 fprintf (file, " -mmuladd");
6885 if (flags & EF_FRV_PIC)
6886 fprintf (file, " -fpic");
6888 if (flags & EF_FRV_BIGPIC)
6889 fprintf (file, " -fPIC");
6891 if (flags & EF_FRV_LIBPIC)
6892 fprintf (file, " -mlibrary-pic");
6894 if (flags & EF_FRV_FDPIC)
6895 fprintf (file, " -mfdpic");
6897 if (flags & EF_FRV_NON_PIC_RELOCS)
6898 fprintf (file, " non-pic relocations");
6900 if (flags & EF_FRV_G0)
6901 fprintf (file, " -G0");
6903 fputc ('\n', file);
6904 return TRUE;
6908 /* Support for core dump NOTE sections. */
6910 static bfd_boolean
6911 elf32_frv_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6913 int offset;
6914 unsigned int raw_size;
6916 switch (note->descsz)
6918 default:
6919 return FALSE;
6921 /* The Linux/FRV elf_prstatus struct is 268 bytes long. The other
6922 hardcoded offsets and sizes listed below (and contained within
6923 this lexical block) refer to fields in the target's elf_prstatus
6924 struct. */
6925 case 268:
6926 /* `pr_cursig' is at offset 12. */
6927 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
6929 /* `pr_pid' is at offset 24. */
6930 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
6932 /* `pr_reg' is at offset 72. */
6933 offset = 72;
6935 /* Most grok_prstatus implementations set `raw_size' to the size
6936 of the pr_reg field. For Linux/FRV, we set `raw_size' to be
6937 the size of `pr_reg' plus the size of `pr_exec_fdpic_loadmap'
6938 and `pr_interp_fdpic_loadmap', both of which (by design)
6939 immediately follow `pr_reg'. This will allow these fields to
6940 be viewed by GDB as registers.
6942 `pr_reg' is 184 bytes long. `pr_exec_fdpic_loadmap' and
6943 `pr_interp_fdpic_loadmap' are 4 bytes each. */
6944 raw_size = 184 + 4 + 4;
6946 break;
6949 /* Make a ".reg/999" section. */
6950 return _bfd_elfcore_make_pseudosection (abfd, ".reg", raw_size,
6951 note->descpos + offset);
6954 static bfd_boolean
6955 elf32_frv_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6957 switch (note->descsz)
6959 default:
6960 return FALSE;
6962 /* The Linux/FRV elf_prpsinfo struct is 124 bytes long. */
6963 case 124:
6965 /* `pr_fname' is found at offset 28 and is 16 bytes long. */
6966 elf_tdata (abfd)->core_program
6967 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
6969 /* `pr_psargs' is found at offset 44 and is 80 bytes long. */
6970 elf_tdata (abfd)->core_command
6971 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
6974 /* Note that for some reason, a spurious space is tacked
6975 onto the end of the args in some (at least one anyway)
6976 implementations, so strip it off if it exists. */
6979 char *command = elf_tdata (abfd)->core_command;
6980 int n = strlen (command);
6982 if (0 < n && command[n - 1] == ' ')
6983 command[n - 1] = '\0';
6986 return TRUE;
6988 #define ELF_ARCH bfd_arch_frv
6989 #define ELF_MACHINE_CODE EM_CYGNUS_FRV
6990 #define ELF_MAXPAGESIZE 0x1000
6992 #define TARGET_BIG_SYM bfd_elf32_frv_vec
6993 #define TARGET_BIG_NAME "elf32-frv"
6995 #define elf_info_to_howto frv_info_to_howto_rela
6996 #define elf_backend_relocate_section elf32_frv_relocate_section
6997 #define elf_backend_gc_mark_hook elf32_frv_gc_mark_hook
6998 #define elf_backend_check_relocs elf32_frv_check_relocs
6999 #define elf_backend_object_p elf32_frv_object_p
7000 #define elf_backend_add_symbol_hook elf32_frv_add_symbol_hook
7002 #define elf_backend_can_gc_sections 1
7003 #define elf_backend_rela_normal 1
7005 #define bfd_elf32_bfd_reloc_type_lookup frv_reloc_type_lookup
7006 #define bfd_elf32_bfd_reloc_name_lookup frv_reloc_name_lookup
7007 #define bfd_elf32_bfd_set_private_flags frv_elf_set_private_flags
7008 #define bfd_elf32_bfd_copy_private_bfd_data frv_elf_copy_private_bfd_data
7009 #define bfd_elf32_bfd_merge_private_bfd_data frv_elf_merge_private_bfd_data
7010 #define bfd_elf32_bfd_print_private_bfd_data frv_elf_print_private_bfd_data
7012 #define elf_backend_want_got_sym 1
7013 #define elf_backend_got_header_size 0
7014 #define elf_backend_want_got_plt 0
7015 #define elf_backend_plt_readonly 1
7016 #define elf_backend_want_plt_sym 0
7017 #define elf_backend_plt_header_size 0
7019 #define elf_backend_finish_dynamic_sections \
7020 elf32_frv_finish_dynamic_sections
7022 #define elf_backend_grok_prstatus elf32_frv_grok_prstatus
7023 #define elf_backend_grok_psinfo elf32_frv_grok_psinfo
7025 #include "elf32-target.h"
7027 #undef ELF_MAXPAGESIZE
7028 #define ELF_MAXPAGESIZE 0x4000
7030 #undef TARGET_BIG_SYM
7031 #define TARGET_BIG_SYM bfd_elf32_frvfdpic_vec
7032 #undef TARGET_BIG_NAME
7033 #define TARGET_BIG_NAME "elf32-frvfdpic"
7034 #undef elf32_bed
7035 #define elf32_bed elf32_frvfdpic_bed
7037 #undef elf_info_to_howto_rel
7038 #define elf_info_to_howto_rel frvfdpic_info_to_howto_rel
7040 #undef bfd_elf32_bfd_link_hash_table_create
7041 #define bfd_elf32_bfd_link_hash_table_create \
7042 frvfdpic_elf_link_hash_table_create
7043 #undef elf_backend_always_size_sections
7044 #define elf_backend_always_size_sections \
7045 elf32_frvfdpic_always_size_sections
7046 #undef elf_backend_modify_program_headers
7047 #define elf_backend_modify_program_headers \
7048 elf32_frvfdpic_modify_program_headers
7049 #undef bfd_elf32_bfd_copy_private_bfd_data
7050 #define bfd_elf32_bfd_copy_private_bfd_data \
7051 elf32_frvfdpic_copy_private_bfd_data
7053 #undef elf_backend_create_dynamic_sections
7054 #define elf_backend_create_dynamic_sections \
7055 elf32_frvfdpic_create_dynamic_sections
7056 #undef elf_backend_adjust_dynamic_symbol
7057 #define elf_backend_adjust_dynamic_symbol \
7058 elf32_frvfdpic_adjust_dynamic_symbol
7059 #undef elf_backend_size_dynamic_sections
7060 #define elf_backend_size_dynamic_sections \
7061 elf32_frvfdpic_size_dynamic_sections
7062 #undef bfd_elf32_bfd_relax_section
7063 #define bfd_elf32_bfd_relax_section \
7064 elf32_frvfdpic_relax_section
7065 #undef elf_backend_finish_dynamic_symbol
7066 #define elf_backend_finish_dynamic_symbol \
7067 elf32_frvfdpic_finish_dynamic_symbol
7068 #undef elf_backend_finish_dynamic_sections
7069 #define elf_backend_finish_dynamic_sections \
7070 elf32_frvfdpic_finish_dynamic_sections
7072 #undef elf_backend_discard_info
7073 #define elf_backend_discard_info \
7074 frvfdpic_elf_discard_info
7075 #undef elf_backend_can_make_relative_eh_frame
7076 #define elf_backend_can_make_relative_eh_frame \
7077 frvfdpic_elf_use_relative_eh_frame
7078 #undef elf_backend_can_make_lsda_relative_eh_frame
7079 #define elf_backend_can_make_lsda_relative_eh_frame \
7080 frvfdpic_elf_use_relative_eh_frame
7081 #undef elf_backend_encode_eh_address
7082 #define elf_backend_encode_eh_address \
7083 frvfdpic_elf_encode_eh_address
7085 #undef elf_backend_may_use_rel_p
7086 #define elf_backend_may_use_rel_p 1
7087 #undef elf_backend_may_use_rela_p
7088 #define elf_backend_may_use_rela_p 1
7089 /* We use REL for dynamic relocations only. */
7090 #undef elf_backend_default_use_rela_p
7091 #define elf_backend_default_use_rela_p 1
7093 #undef elf_backend_omit_section_dynsym
7094 #define elf_backend_omit_section_dynsym _frvfdpic_link_omit_section_dynsym
7096 #include "elf32-target.h"