gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / bfd / elf32-frv.c
1 /* FRV-specific support for 32-bit ELF.
2 Copyright (C) 2002-2020 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/frv.h"
26 #include "dwarf2.h"
27 #include "hashtab.h"
28 #include "libiberty.h"
29
30 /* Forward declarations. */
31
32
33 static reloc_howto_type elf32_frv_howto_table [] =
34 {
35 /* This reloc does nothing. */
36 HOWTO (R_FRV_NONE, /* type */
37 0, /* rightshift */
38 3, /* size (0 = byte, 1 = short, 2 = long) */
39 0, /* bitsize */
40 FALSE, /* pc_relative */
41 0, /* bitpos */
42 complain_overflow_dont, /* complain_on_overflow */
43 bfd_elf_generic_reloc, /* special_function */
44 "R_FRV_NONE", /* name */
45 FALSE, /* partial_inplace */
46 0, /* src_mask */
47 0, /* dst_mask */
48 FALSE), /* pcrel_offset */
49
50 /* A 32 bit absolute relocation. */
51 HOWTO (R_FRV_32, /* type */
52 0, /* rightshift */
53 2, /* size (0 = byte, 1 = short, 2 = long) */
54 32, /* bitsize */
55 FALSE, /* pc_relative */
56 0, /* bitpos */
57 complain_overflow_bitfield, /* complain_on_overflow */
58 bfd_elf_generic_reloc, /* special_function */
59 "R_FRV_32", /* name */
60 FALSE, /* partial_inplace */
61 0xffffffff, /* src_mask */
62 0xffffffff, /* dst_mask */
63 FALSE), /* pcrel_offset */
64
65 /* A 16 bit pc-relative relocation. */
66 HOWTO (R_FRV_LABEL16, /* type */
67 2, /* rightshift */
68 2, /* size (0 = byte, 1 = short, 2 = long) */
69 16, /* bitsize */
70 TRUE, /* pc_relative */
71 0, /* bitpos */
72 complain_overflow_signed, /* complain_on_overflow */
73 bfd_elf_generic_reloc, /* special_function */
74 "R_FRV_LABEL16", /* name */
75 FALSE, /* partial_inplace */
76 0xffff, /* src_mask */
77 0xffff, /* dst_mask */
78 TRUE), /* pcrel_offset */
79
80 /* A 24-bit pc-relative relocation. */
81 HOWTO (R_FRV_LABEL24, /* type */
82 2, /* rightshift */
83 2, /* size (0 = byte, 1 = short, 2 = long) */
84 26, /* bitsize */
85 TRUE, /* pc_relative */
86 0, /* bitpos */
87 complain_overflow_bitfield, /* complain_on_overflow */
88 bfd_elf_generic_reloc, /* special_function */
89 "R_FRV_LABEL24", /* name */
90 FALSE, /* partial_inplace */
91 0x7e03ffff, /* src_mask */
92 0x7e03ffff, /* dst_mask */
93 TRUE), /* pcrel_offset */
94
95 HOWTO (R_FRV_LO16, /* type */
96 0, /* rightshift */
97 2, /* size (0 = byte, 1 = short, 2 = long) */
98 16, /* bitsize */
99 FALSE, /* pc_relative */
100 0, /* bitpos */
101 complain_overflow_dont, /* complain_on_overflow */
102 bfd_elf_generic_reloc, /* special_function */
103 "R_FRV_LO16", /* name */
104 FALSE, /* partial_inplace */
105 0xffff, /* src_mask */
106 0xffff, /* dst_mask */
107 FALSE), /* pcrel_offset */
108
109 HOWTO (R_FRV_HI16, /* type */
110 0, /* rightshift */
111 2, /* size (0 = byte, 1 = short, 2 = long) */
112 16, /* bitsize */
113 FALSE, /* pc_relative */
114 0, /* bitpos */
115 complain_overflow_dont, /* complain_on_overflow */
116 bfd_elf_generic_reloc, /* special_function */
117 "R_FRV_HI16", /* name */
118 FALSE, /* partial_inplace */
119 0xffff, /* src_mask */
120 0xffff, /* dst_mask */
121 FALSE), /* pcrel_offset */
122
123 HOWTO (R_FRV_GPREL12, /* type */
124 0, /* rightshift */
125 2, /* size (0 = byte, 1 = short, 2 = long) */
126 12, /* bitsize */
127 FALSE, /* pc_relative */
128 0, /* bitpos */
129 complain_overflow_dont, /* complain_on_overflow */
130 bfd_elf_generic_reloc, /* special_function */
131 "R_FRV_GPREL12", /* name */
132 FALSE, /* partial_inplace */
133 0xfff, /* src_mask */
134 0xfff, /* dst_mask */
135 FALSE), /* pcrel_offset */
136
137 HOWTO (R_FRV_GPRELU12, /* type */
138 0, /* rightshift */
139 2, /* size (0 = byte, 1 = short, 2 = long) */
140 12, /* bitsize */
141 FALSE, /* pc_relative */
142 0, /* bitpos */
143 complain_overflow_dont, /* complain_on_overflow */
144 bfd_elf_generic_reloc, /* special_function */
145 "R_FRV_GPRELU12", /* name */
146 FALSE, /* partial_inplace */
147 0xfff, /* src_mask */
148 0x3f03f, /* dst_mask */
149 FALSE), /* pcrel_offset */
150
151 HOWTO (R_FRV_GPREL32, /* type */
152 0, /* rightshift */
153 2, /* size (0 = byte, 1 = short, 2 = long) */
154 32, /* bitsize */
155 FALSE, /* pc_relative */
156 0, /* bitpos */
157 complain_overflow_dont, /* complain_on_overflow */
158 bfd_elf_generic_reloc, /* special_function */
159 "R_FRV_GPREL32", /* name */
160 FALSE, /* partial_inplace */
161 0xffffffff, /* src_mask */
162 0xffffffff, /* dst_mask */
163 FALSE), /* pcrel_offset */
164
165 HOWTO (R_FRV_GPRELHI, /* type */
166 0, /* rightshift */
167 2, /* size (0 = byte, 1 = short, 2 = long) */
168 16, /* bitsize */
169 FALSE, /* pc_relative */
170 0, /* bitpos */
171 complain_overflow_dont, /* complain_on_overflow */
172 bfd_elf_generic_reloc, /* special_function */
173 "R_FRV_GPRELHI", /* name */
174 FALSE, /* partial_inplace */
175 0xffff, /* src_mask */
176 0xffff, /* dst_mask */
177 FALSE), /* pcrel_offset */
178
179 HOWTO (R_FRV_GPRELLO, /* type */
180 0, /* rightshift */
181 2, /* size (0 = byte, 1 = short, 2 = long) */
182 16, /* bitsize */
183 FALSE, /* pc_relative */
184 0, /* bitpos */
185 complain_overflow_dont, /* complain_on_overflow */
186 bfd_elf_generic_reloc, /* special_function */
187 "R_FRV_GPRELLO", /* name */
188 FALSE, /* partial_inplace */
189 0xffff, /* src_mask */
190 0xffff, /* dst_mask */
191 FALSE), /* pcrel_offset */
192
193 /* A 12-bit signed operand with the GOT offset for the address of
194 the symbol. */
195 HOWTO (R_FRV_GOT12, /* type */
196 0, /* rightshift */
197 2, /* size (0 = byte, 1 = short, 2 = long) */
198 12, /* bitsize */
199 FALSE, /* pc_relative */
200 0, /* bitpos */
201 complain_overflow_signed, /* complain_on_overflow */
202 bfd_elf_generic_reloc, /* special_function */
203 "R_FRV_GOT12", /* name */
204 FALSE, /* partial_inplace */
205 0xfff, /* src_mask */
206 0xfff, /* dst_mask */
207 FALSE), /* pcrel_offset */
208
209 /* The upper 16 bits of the GOT offset for the address of the
210 symbol. */
211 HOWTO (R_FRV_GOTHI, /* type */
212 0, /* rightshift */
213 2, /* size (0 = byte, 1 = short, 2 = long) */
214 16, /* bitsize */
215 FALSE, /* pc_relative */
216 0, /* bitpos */
217 complain_overflow_dont, /* complain_on_overflow */
218 bfd_elf_generic_reloc, /* special_function */
219 "R_FRV_GOTHI", /* name */
220 FALSE, /* partial_inplace */
221 0xffff, /* src_mask */
222 0xffff, /* dst_mask */
223 FALSE), /* pcrel_offset */
224
225 /* The lower 16 bits of the GOT offset for the address of the
226 symbol. */
227 HOWTO (R_FRV_GOTLO, /* type */
228 0, /* rightshift */
229 2, /* size (0 = byte, 1 = short, 2 = long) */
230 16, /* bitsize */
231 FALSE, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_dont, /* complain_on_overflow */
234 bfd_elf_generic_reloc, /* special_function */
235 "R_FRV_GOTLO", /* name */
236 FALSE, /* partial_inplace */
237 0xffff, /* src_mask */
238 0xffff, /* dst_mask */
239 FALSE), /* pcrel_offset */
240
241 /* The 32-bit address of the canonical descriptor of a function. */
242 HOWTO (R_FRV_FUNCDESC, /* type */
243 0, /* rightshift */
244 2, /* size (0 = byte, 1 = short, 2 = long) */
245 32, /* bitsize */
246 FALSE, /* pc_relative */
247 0, /* bitpos */
248 complain_overflow_bitfield, /* complain_on_overflow */
249 bfd_elf_generic_reloc, /* special_function */
250 "R_FRV_FUNCDESC", /* name */
251 FALSE, /* partial_inplace */
252 0xffffffff, /* src_mask */
253 0xffffffff, /* dst_mask */
254 FALSE), /* pcrel_offset */
255
256 /* A 12-bit signed operand with the GOT offset for the address of
257 canonical descriptor of a function. */
258 HOWTO (R_FRV_FUNCDESC_GOT12, /* type */
259 0, /* rightshift */
260 2, /* size (0 = byte, 1 = short, 2 = long) */
261 12, /* bitsize */
262 FALSE, /* pc_relative */
263 0, /* bitpos */
264 complain_overflow_signed, /* complain_on_overflow */
265 bfd_elf_generic_reloc, /* special_function */
266 "R_FRV_FUNCDESC_GOT12", /* name */
267 FALSE, /* partial_inplace */
268 0xfff, /* src_mask */
269 0xfff, /* dst_mask */
270 FALSE), /* pcrel_offset */
271
272 /* The upper 16 bits of the GOT offset for the address of the
273 canonical descriptor of a function. */
274 HOWTO (R_FRV_FUNCDESC_GOTHI, /* type */
275 0, /* rightshift */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
277 16, /* bitsize */
278 FALSE, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_dont, /* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_FRV_FUNCDESC_GOTHI", /* name */
283 FALSE, /* partial_inplace */
284 0xffff, /* src_mask */
285 0xffff, /* dst_mask */
286 FALSE), /* pcrel_offset */
287
288 /* The lower 16 bits of the GOT offset for the address of the
289 canonical descriptor of a function. */
290 HOWTO (R_FRV_FUNCDESC_GOTLO, /* type */
291 0, /* rightshift */
292 2, /* size (0 = byte, 1 = short, 2 = long) */
293 16, /* bitsize */
294 FALSE, /* pc_relative */
295 0, /* bitpos */
296 complain_overflow_dont, /* complain_on_overflow */
297 bfd_elf_generic_reloc, /* special_function */
298 "R_FRV_FUNCDESC_GOTLO", /* name */
299 FALSE, /* partial_inplace */
300 0xffff, /* src_mask */
301 0xffff, /* dst_mask */
302 FALSE), /* pcrel_offset */
303
304 /* The 64-bit descriptor of a function. */
305 HOWTO (R_FRV_FUNCDESC_VALUE, /* type */
306 0, /* rightshift */
307 2, /* size (0 = byte, 1 = short, 2 = long) */
308 64, /* bitsize */
309 FALSE, /* pc_relative */
310 0, /* bitpos */
311 complain_overflow_bitfield, /* complain_on_overflow */
312 bfd_elf_generic_reloc, /* special_function */
313 "R_FRV_FUNCDESC_VALUE", /* name */
314 FALSE, /* partial_inplace */
315 0xffffffff, /* src_mask */
316 0xffffffff, /* dst_mask */
317 FALSE), /* pcrel_offset */
318
319 /* A 12-bit signed operand with the GOT offset for the address of
320 canonical descriptor of a function. */
321 HOWTO (R_FRV_FUNCDESC_GOTOFF12, /* type */
322 0, /* rightshift */
323 2, /* size (0 = byte, 1 = short, 2 = long) */
324 12, /* bitsize */
325 FALSE, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_signed, /* complain_on_overflow */
328 bfd_elf_generic_reloc, /* special_function */
329 "R_FRV_FUNCDESC_GOTOFF12", /* name */
330 FALSE, /* partial_inplace */
331 0xfff, /* src_mask */
332 0xfff, /* dst_mask */
333 FALSE), /* pcrel_offset */
334
335 /* The upper 16 bits of the GOT offset for the address of the
336 canonical descriptor of a function. */
337 HOWTO (R_FRV_FUNCDESC_GOTOFFHI, /* type */
338 0, /* rightshift */
339 2, /* size (0 = byte, 1 = short, 2 = long) */
340 16, /* bitsize */
341 FALSE, /* pc_relative */
342 0, /* bitpos */
343 complain_overflow_dont, /* complain_on_overflow */
344 bfd_elf_generic_reloc, /* special_function */
345 "R_FRV_FUNCDESC_GOTOFFHI", /* name */
346 FALSE, /* partial_inplace */
347 0xffff, /* src_mask */
348 0xffff, /* dst_mask */
349 FALSE), /* pcrel_offset */
350
351 /* The lower 16 bits of the GOT offset for the address of the
352 canonical descriptor of a function. */
353 HOWTO (R_FRV_FUNCDESC_GOTOFFLO, /* type */
354 0, /* rightshift */
355 2, /* size (0 = byte, 1 = short, 2 = long) */
356 16, /* bitsize */
357 FALSE, /* pc_relative */
358 0, /* bitpos */
359 complain_overflow_dont, /* complain_on_overflow */
360 bfd_elf_generic_reloc, /* special_function */
361 "R_FRV_FUNCDESC_GOTOFFLO", /* name */
362 FALSE, /* partial_inplace */
363 0xffff, /* src_mask */
364 0xffff, /* dst_mask */
365 FALSE), /* pcrel_offset */
366
367 /* A 12-bit signed operand with the GOT offset for the address of
368 the symbol. */
369 HOWTO (R_FRV_GOTOFF12, /* type */
370 0, /* rightshift */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
372 12, /* bitsize */
373 FALSE, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_signed, /* complain_on_overflow */
376 bfd_elf_generic_reloc, /* special_function */
377 "R_FRV_GOTOFF12", /* name */
378 FALSE, /* partial_inplace */
379 0xfff, /* src_mask */
380 0xfff, /* dst_mask */
381 FALSE), /* pcrel_offset */
382
383 /* The upper 16 bits of the GOT offset for the address of the
384 symbol. */
385 HOWTO (R_FRV_GOTOFFHI, /* type */
386 0, /* rightshift */
387 2, /* size (0 = byte, 1 = short, 2 = long) */
388 16, /* bitsize */
389 FALSE, /* pc_relative */
390 0, /* bitpos */
391 complain_overflow_dont, /* complain_on_overflow */
392 bfd_elf_generic_reloc, /* special_function */
393 "R_FRV_GOTOFFHI", /* name */
394 FALSE, /* partial_inplace */
395 0xffff, /* src_mask */
396 0xffff, /* dst_mask */
397 FALSE), /* pcrel_offset */
398
399 /* The lower 16 bits of the GOT offset for the address of the
400 symbol. */
401 HOWTO (R_FRV_GOTOFFLO, /* type */
402 0, /* rightshift */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
404 16, /* bitsize */
405 FALSE, /* pc_relative */
406 0, /* bitpos */
407 complain_overflow_dont, /* complain_on_overflow */
408 bfd_elf_generic_reloc, /* special_function */
409 "R_FRV_GOTOFFLO", /* name */
410 FALSE, /* partial_inplace */
411 0xffff, /* src_mask */
412 0xffff, /* dst_mask */
413 FALSE), /* pcrel_offset */
414
415 /* A 24-bit pc-relative relocation referencing the TLS PLT entry for
416 a thread-local symbol. If the symbol number is 0, it refers to
417 the module. */
418 HOWTO (R_FRV_GETTLSOFF, /* type */
419 2, /* rightshift */
420 2, /* size (0 = byte, 1 = short, 2 = long) */
421 26, /* bitsize */
422 TRUE, /* pc_relative */
423 0, /* bitpos */
424 complain_overflow_bitfield, /* complain_on_overflow */
425 bfd_elf_generic_reloc, /* special_function */
426 "R_FRV_GETTLSOFF", /* name */
427 FALSE, /* partial_inplace */
428 0x7e03ffff, /* src_mask */
429 0x7e03ffff, /* dst_mask */
430 TRUE), /* pcrel_offset */
431
432 /* A 64-bit TLS descriptor for a symbol. This relocation is only
433 valid as a REL, dynamic relocation. */
434 HOWTO (R_FRV_TLSDESC_VALUE, /* type */
435 0, /* rightshift */
436 2, /* size (0 = byte, 1 = short, 2 = long) */
437 64, /* bitsize */
438 FALSE, /* pc_relative */
439 0, /* bitpos */
440 complain_overflow_bitfield, /* complain_on_overflow */
441 bfd_elf_generic_reloc, /* special_function */
442 "R_FRV_TLSDESC_VALUE", /* name */
443 FALSE, /* partial_inplace */
444 0xffffffff, /* src_mask */
445 0xffffffff, /* dst_mask */
446 FALSE), /* pcrel_offset */
447
448 /* A 12-bit signed operand with the GOT offset for the TLS
449 descriptor of the symbol. */
450 HOWTO (R_FRV_GOTTLSDESC12, /* type */
451 0, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 12, /* bitsize */
454 FALSE, /* pc_relative */
455 0, /* bitpos */
456 complain_overflow_signed, /* complain_on_overflow */
457 bfd_elf_generic_reloc, /* special_function */
458 "R_FRV_GOTTLSDESC12", /* name */
459 FALSE, /* partial_inplace */
460 0xfff, /* src_mask */
461 0xfff, /* dst_mask */
462 FALSE), /* pcrel_offset */
463
464 /* The upper 16 bits of the GOT offset for the TLS descriptor of the
465 symbol. */
466 HOWTO (R_FRV_GOTTLSDESCHI, /* type */
467 0, /* rightshift */
468 2, /* size (0 = byte, 1 = short, 2 = long) */
469 16, /* bitsize */
470 FALSE, /* pc_relative */
471 0, /* bitpos */
472 complain_overflow_dont, /* complain_on_overflow */
473 bfd_elf_generic_reloc, /* special_function */
474 "R_FRV_GOTTLSDESCHI", /* name */
475 FALSE, /* partial_inplace */
476 0xffff, /* src_mask */
477 0xffff, /* dst_mask */
478 FALSE), /* pcrel_offset */
479
480 /* The lower 16 bits of the GOT offset for the TLS descriptor of the
481 symbol. */
482 HOWTO (R_FRV_GOTTLSDESCLO, /* type */
483 0, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 16, /* bitsize */
486 FALSE, /* pc_relative */
487 0, /* bitpos */
488 complain_overflow_dont, /* complain_on_overflow */
489 bfd_elf_generic_reloc, /* special_function */
490 "R_FRV_GOTTLSDESCLO", /* name */
491 FALSE, /* partial_inplace */
492 0xffff, /* src_mask */
493 0xffff, /* dst_mask */
494 FALSE), /* pcrel_offset */
495
496 /* A 12-bit signed operand with the offset from the module base
497 address to the thread-local symbol address. */
498 HOWTO (R_FRV_TLSMOFF12, /* type */
499 0, /* rightshift */
500 2, /* size (0 = byte, 1 = short, 2 = long) */
501 12, /* bitsize */
502 FALSE, /* pc_relative */
503 0, /* bitpos */
504 complain_overflow_signed, /* complain_on_overflow */
505 bfd_elf_generic_reloc, /* special_function */
506 "R_FRV_TLSMOFF12", /* name */
507 FALSE, /* partial_inplace */
508 0xfff, /* src_mask */
509 0xfff, /* dst_mask */
510 FALSE), /* pcrel_offset */
511
512 /* The upper 16 bits of the offset from the module base address to
513 the thread-local symbol address. */
514 HOWTO (R_FRV_TLSMOFFHI, /* type */
515 0, /* rightshift */
516 2, /* size (0 = byte, 1 = short, 2 = long) */
517 16, /* bitsize */
518 FALSE, /* pc_relative */
519 0, /* bitpos */
520 complain_overflow_dont, /* complain_on_overflow */
521 bfd_elf_generic_reloc, /* special_function */
522 "R_FRV_TLSMOFFHI", /* name */
523 FALSE, /* partial_inplace */
524 0xffff, /* src_mask */
525 0xffff, /* dst_mask */
526 FALSE), /* pcrel_offset */
527
528 /* The lower 16 bits of the offset from the module base address to
529 the thread-local symbol address. */
530 HOWTO (R_FRV_TLSMOFFLO, /* type */
531 0, /* rightshift */
532 2, /* size (0 = byte, 1 = short, 2 = long) */
533 16, /* bitsize */
534 FALSE, /* pc_relative */
535 0, /* bitpos */
536 complain_overflow_dont, /* complain_on_overflow */
537 bfd_elf_generic_reloc, /* special_function */
538 "R_FRV_TLSMOFFLO", /* name */
539 FALSE, /* partial_inplace */
540 0xffff, /* src_mask */
541 0xffff, /* dst_mask */
542 FALSE), /* pcrel_offset */
543
544 /* A 12-bit signed operand with the GOT offset for the TLSOFF entry
545 for a symbol. */
546 HOWTO (R_FRV_GOTTLSOFF12, /* type */
547 0, /* rightshift */
548 2, /* size (0 = byte, 1 = short, 2 = long) */
549 12, /* bitsize */
550 FALSE, /* pc_relative */
551 0, /* bitpos */
552 complain_overflow_signed, /* complain_on_overflow */
553 bfd_elf_generic_reloc, /* special_function */
554 "R_FRV_GOTTLSOFF12", /* name */
555 FALSE, /* partial_inplace */
556 0xfff, /* src_mask */
557 0xfff, /* dst_mask */
558 FALSE), /* pcrel_offset */
559
560 /* The upper 16 bits of the GOT offset for the TLSOFF entry for a
561 symbol. */
562 HOWTO (R_FRV_GOTTLSOFFHI, /* type */
563 0, /* rightshift */
564 2, /* size (0 = byte, 1 = short, 2 = long) */
565 16, /* bitsize */
566 FALSE, /* pc_relative */
567 0, /* bitpos */
568 complain_overflow_dont, /* complain_on_overflow */
569 bfd_elf_generic_reloc, /* special_function */
570 "R_FRV_GOTTLSOFFHI", /* name */
571 FALSE, /* partial_inplace */
572 0xffff, /* src_mask */
573 0xffff, /* dst_mask */
574 FALSE), /* pcrel_offset */
575
576 /* The lower 16 bits of the GOT offset for the TLSOFF entry for a
577 symbol. */
578 HOWTO (R_FRV_GOTTLSOFFLO, /* type */
579 0, /* rightshift */
580 2, /* size (0 = byte, 1 = short, 2 = long) */
581 16, /* bitsize */
582 FALSE, /* pc_relative */
583 0, /* bitpos */
584 complain_overflow_dont, /* complain_on_overflow */
585 bfd_elf_generic_reloc, /* special_function */
586 "R_FRV_GOTTLSOFFLO", /* name */
587 FALSE, /* partial_inplace */
588 0xffff, /* src_mask */
589 0xffff, /* dst_mask */
590 FALSE), /* pcrel_offset */
591
592 /* The 32-bit offset from the thread pointer (not the module base
593 address) to a thread-local symbol. */
594 HOWTO (R_FRV_TLSOFF, /* type */
595 0, /* rightshift */
596 2, /* size (0 = byte, 1 = short, 2 = long) */
597 32, /* bitsize */
598 FALSE, /* pc_relative */
599 0, /* bitpos */
600 complain_overflow_dont, /* complain_on_overflow */
601 bfd_elf_generic_reloc, /* special_function */
602 "R_FRV_TLSOFF", /* name */
603 FALSE, /* partial_inplace */
604 0xffffffff, /* src_mask */
605 0xffffffff, /* dst_mask */
606 FALSE), /* pcrel_offset */
607
608 /* An annotation for linker relaxation, that denotes the
609 symbol+addend whose TLS descriptor is referenced by the sum of
610 the two input registers of an ldd instruction. */
611 HOWTO (R_FRV_TLSDESC_RELAX, /* type */
612 0, /* rightshift */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
614 0, /* 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_TLSDESC_RELAX", /* name */
620 FALSE, /* partial_inplace */
621 0, /* src_mask */
622 0, /* dst_mask */
623 FALSE), /* pcrel_offset */
624
625 /* An annotation for linker relaxation, that denotes the
626 symbol+addend whose TLS resolver entry point is given by the sum
627 of the two register operands of an calll instruction. */
628 HOWTO (R_FRV_GETTLSOFF_RELAX, /* type */
629 0, /* rightshift */
630 2, /* size (0 = byte, 1 = short, 2 = long) */
631 0, /* bitsize */
632 FALSE, /* pc_relative */
633 0, /* bitpos */
634 complain_overflow_dont, /* complain_on_overflow */
635 bfd_elf_generic_reloc, /* special_function */
636 "R_FRV_GETTLSOFF_RELAX", /* name */
637 FALSE, /* partial_inplace */
638 0, /* src_mask */
639 0, /* dst_mask */
640 FALSE), /* pcrel_offset */
641
642 /* An annotation for linker relaxation, that denotes the
643 symbol+addend whose TLS offset GOT entry is given by the sum of
644 the two input registers of an ld instruction. */
645 HOWTO (R_FRV_TLSOFF_RELAX, /* type */
646 0, /* rightshift */
647 2, /* size (0 = byte, 1 = short, 2 = long) */
648 0, /* bitsize */
649 FALSE, /* pc_relative */
650 0, /* bitpos */
651 complain_overflow_bitfield, /* complain_on_overflow */
652 bfd_elf_generic_reloc, /* special_function */
653 "R_FRV_TLSOFF_RELAX", /* name */
654 FALSE, /* partial_inplace */
655 0, /* src_mask */
656 0, /* dst_mask */
657 FALSE), /* pcrel_offset */
658
659 /* A 32-bit offset from the module base address to
660 the thread-local symbol address. */
661 HOWTO (R_FRV_TLSMOFF, /* type */
662 0, /* rightshift */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
664 32, /* bitsize */
665 FALSE, /* pc_relative */
666 0, /* bitpos */
667 complain_overflow_dont, /* complain_on_overflow */
668 bfd_elf_generic_reloc, /* special_function */
669 "R_FRV_TLSMOFF", /* name */
670 FALSE, /* partial_inplace */
671 0xffffffff, /* src_mask */
672 0xffffffff, /* dst_mask */
673 FALSE), /* pcrel_offset */
674 };
675
676 /* GNU extension to record C++ vtable hierarchy. */
677 static reloc_howto_type elf32_frv_vtinherit_howto =
678 HOWTO (R_FRV_GNU_VTINHERIT, /* type */
679 0, /* rightshift */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
681 0, /* bitsize */
682 FALSE, /* pc_relative */
683 0, /* bitpos */
684 complain_overflow_dont, /* complain_on_overflow */
685 NULL, /* special_function */
686 "R_FRV_GNU_VTINHERIT", /* name */
687 FALSE, /* partial_inplace */
688 0, /* src_mask */
689 0, /* dst_mask */
690 FALSE); /* pcrel_offset */
691
692 /* GNU extension to record C++ vtable member usage. */
693 static reloc_howto_type elf32_frv_vtentry_howto =
694 HOWTO (R_FRV_GNU_VTENTRY, /* 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_dont, /* complain_on_overflow */
701 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
702 "R_FRV_GNU_VTENTRY", /* name */
703 FALSE, /* partial_inplace */
704 0, /* src_mask */
705 0, /* dst_mask */
706 FALSE); /* pcrel_offset */
707
708 /* The following 3 relocations are REL. The only difference to the
709 entries in the table above are that partial_inplace is TRUE. */
710 static reloc_howto_type elf32_frv_rel_32_howto =
711 HOWTO (R_FRV_32, /* type */
712 0, /* rightshift */
713 2, /* size (0 = byte, 1 = short, 2 = long) */
714 32, /* bitsize */
715 FALSE, /* pc_relative */
716 0, /* bitpos */
717 complain_overflow_bitfield, /* complain_on_overflow */
718 bfd_elf_generic_reloc, /* special_function */
719 "R_FRV_32", /* name */
720 TRUE, /* partial_inplace */
721 0xffffffff, /* src_mask */
722 0xffffffff, /* dst_mask */
723 FALSE); /* pcrel_offset */
724
725 static reloc_howto_type elf32_frv_rel_funcdesc_howto =
726 HOWTO (R_FRV_FUNCDESC, /* type */
727 0, /* rightshift */
728 2, /* size (0 = byte, 1 = short, 2 = long) */
729 32, /* bitsize */
730 FALSE, /* pc_relative */
731 0, /* bitpos */
732 complain_overflow_bitfield, /* complain_on_overflow */
733 bfd_elf_generic_reloc, /* special_function */
734 "R_FRV_FUNCDESC", /* name */
735 TRUE, /* partial_inplace */
736 0xffffffff, /* src_mask */
737 0xffffffff, /* dst_mask */
738 FALSE); /* pcrel_offset */
739
740 static reloc_howto_type elf32_frv_rel_funcdesc_value_howto =
741 HOWTO (R_FRV_FUNCDESC_VALUE, /* type */
742 0, /* rightshift */
743 2, /* size (0 = byte, 1 = short, 2 = long) */
744 64, /* bitsize */
745 FALSE, /* pc_relative */
746 0, /* bitpos */
747 complain_overflow_bitfield, /* complain_on_overflow */
748 bfd_elf_generic_reloc, /* special_function */
749 "R_FRV_FUNCDESC_VALUE", /* name */
750 TRUE, /* partial_inplace */
751 0xffffffff, /* src_mask */
752 0xffffffff, /* dst_mask */
753 FALSE); /* pcrel_offset */
754
755 static reloc_howto_type elf32_frv_rel_tlsdesc_value_howto =
756 /* A 64-bit TLS descriptor for a symbol. The first word resolves to
757 an entry point, and the second resolves to a special argument.
758 If the symbol turns out to be in static TLS, the entry point is a
759 return instruction, and the special argument is the TLS offset
760 for the symbol. If it's in dynamic TLS, the entry point is a TLS
761 offset resolver, and the special argument is a pointer to a data
762 structure allocated by the dynamic loader, containing the GOT
763 address for the offset resolver, the module id, the offset within
764 the module, and anything else the TLS offset resolver might need
765 to determine the TLS offset for the symbol in the running
766 thread. */
767 HOWTO (R_FRV_TLSDESC_VALUE, /* type */
768 0, /* rightshift */
769 2, /* size (0 = byte, 1 = short, 2 = long) */
770 64, /* bitsize */
771 FALSE, /* pc_relative */
772 0, /* bitpos */
773 complain_overflow_bitfield, /* complain_on_overflow */
774 bfd_elf_generic_reloc, /* special_function */
775 "R_FRV_TLSDESC_VALUE", /* name */
776 TRUE, /* partial_inplace */
777 0xffffffff, /* src_mask */
778 0xffffffff, /* dst_mask */
779 FALSE); /* pcrel_offset */
780
781 static reloc_howto_type elf32_frv_rel_tlsoff_howto =
782 /* The 32-bit offset from the thread pointer (not the module base
783 address) to a thread-local symbol. */
784 HOWTO (R_FRV_TLSOFF, /* type */
785 0, /* rightshift */
786 2, /* size (0 = byte, 1 = short, 2 = long) */
787 32, /* bitsize */
788 FALSE, /* pc_relative */
789 0, /* bitpos */
790 complain_overflow_bitfield, /* complain_on_overflow */
791 bfd_elf_generic_reloc, /* special_function */
792 "R_FRV_TLSOFF", /* name */
793 TRUE, /* partial_inplace */
794 0xffffffff, /* src_mask */
795 0xffffffff, /* dst_mask */
796 FALSE); /* pcrel_offset */
797
798
799 \f
800 extern const bfd_target frv_elf32_fdpic_vec;
801 #define IS_FDPIC(bfd) ((bfd)->xvec == &frv_elf32_fdpic_vec)
802
803 /* An extension of the elf hash table data structure, containing some
804 additional FRV-specific data. */
805 struct frvfdpic_elf_link_hash_table
806 {
807 struct elf_link_hash_table elf;
808
809 /* A pointer to the .rofixup section. */
810 asection *sgotfixup;
811 /* GOT base offset. */
812 bfd_vma got0;
813 /* Location of the first non-lazy PLT entry, i.e., the number of
814 bytes taken by lazy PLT entries. If locally-bound TLS
815 descriptors require a ret instruction, it will be placed at this
816 offset. */
817 bfd_vma plt0;
818 /* A hash table holding information about which symbols were
819 referenced with which PIC-related relocations. */
820 struct htab *relocs_info;
821 /* Summary reloc information collected by
822 _frvfdpic_count_got_plt_entries. */
823 struct _frvfdpic_dynamic_got_info *g;
824 };
825
826 /* Get the FRV ELF linker hash table from a link_info structure. */
827
828 #define frvfdpic_hash_table(p) \
829 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
830 == FRV_ELF_DATA ? ((struct frvfdpic_elf_link_hash_table *) ((p)->hash)) : NULL)
831
832 #define frvfdpic_got_section(info) \
833 (frvfdpic_hash_table (info)->elf.sgot)
834 #define frvfdpic_gotrel_section(info) \
835 (frvfdpic_hash_table (info)->elf.srelgot)
836 #define frvfdpic_gotfixup_section(info) \
837 (frvfdpic_hash_table (info)->sgotfixup)
838 #define frvfdpic_plt_section(info) \
839 (frvfdpic_hash_table (info)->elf.splt)
840 #define frvfdpic_pltrel_section(info) \
841 (frvfdpic_hash_table (info)->elf.srelplt)
842 #define frvfdpic_relocs_info(info) \
843 (frvfdpic_hash_table (info)->relocs_info)
844 #define frvfdpic_got_initial_offset(info) \
845 (frvfdpic_hash_table (info)->got0)
846 #define frvfdpic_plt_initial_offset(info) \
847 (frvfdpic_hash_table (info)->plt0)
848 #define frvfdpic_dynamic_got_plt_info(info) \
849 (frvfdpic_hash_table (info)->g)
850
851 /* Currently it's the same, but if some day we have a reason to change
852 it, we'd better be using a different macro.
853
854 FIXME: if there's any TLS PLT entry that uses local-exec or
855 initial-exec models, we could use the ret at the end of any of them
856 instead of adding one more. */
857 #define frvfdpic_plt_tls_ret_offset(info) \
858 (frvfdpic_plt_initial_offset (info))
859
860 /* The name of the dynamic interpreter. This is put in the .interp
861 section. */
862
863 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
864
865 #define DEFAULT_STACK_SIZE 0x20000
866
867 /* This structure is used to collect the number of entries present in
868 each addressable range of the got. */
869 struct _frvfdpic_dynamic_got_info
870 {
871 /* Several bits of information about the current link. */
872 struct bfd_link_info *info;
873 /* Total GOT size needed for GOT entries within the 12-, 16- or 32-bit
874 ranges. */
875 bfd_vma got12, gotlos, gothilo;
876 /* Total GOT size needed for function descriptor entries within the 12-,
877 16- or 32-bit ranges. */
878 bfd_vma fd12, fdlos, fdhilo;
879 /* Total GOT size needed by function descriptor entries referenced
880 in PLT entries, that would be profitable to place in offsets
881 close to the PIC register. */
882 bfd_vma fdplt;
883 /* Total PLT size needed by lazy PLT entries. */
884 bfd_vma lzplt;
885 /* Total GOT size needed for TLS descriptor entries within the 12-,
886 16- or 32-bit ranges. */
887 bfd_vma tlsd12, tlsdlos, tlsdhilo;
888 /* Total GOT size needed by TLS descriptors referenced in PLT
889 entries, that would be profitable to place in offers close to the
890 PIC register. */
891 bfd_vma tlsdplt;
892 /* Total PLT size needed by TLS lazy PLT entries. */
893 bfd_vma tlslzplt;
894 /* Number of relocations carried over from input object files. */
895 unsigned long relocs;
896 /* Number of fixups introduced by relocations in input object files. */
897 unsigned long fixups;
898 /* The number of fixups that reference the ret instruction added to
899 the PLT for locally-resolved TLS descriptors. */
900 unsigned long tls_ret_refs;
901 };
902
903 /* This structure is used to assign offsets to got entries, function
904 descriptors, plt entries and lazy plt entries. */
905
906 struct _frvfdpic_dynamic_got_plt_info
907 {
908 /* Summary information collected with _frvfdpic_count_got_plt_entries. */
909 struct _frvfdpic_dynamic_got_info g;
910
911 /* For each addressable range, we record a MAX (positive) and MIN
912 (negative) value. CUR is used to assign got entries, and it's
913 incremented from an initial positive value to MAX, then from MIN
914 to FDCUR (unless FDCUR wraps around first). FDCUR is used to
915 assign function descriptors, and it's decreased from an initial
916 non-positive value to MIN, then from MAX down to CUR (unless CUR
917 wraps around first). All of MIN, MAX, CUR and FDCUR always point
918 to even words. ODD, if non-zero, indicates an odd word to be
919 used for the next got entry, otherwise CUR is used and
920 incremented by a pair of words, wrapping around when it reaches
921 MAX. FDCUR is decremented (and wrapped) before the next function
922 descriptor is chosen. FDPLT indicates the number of remaining
923 slots that can be used for function descriptors used only by PLT
924 entries.
925
926 TMAX, TMIN and TCUR are used to assign TLS descriptors. TCUR
927 starts as MAX, and grows up to TMAX, then wraps around to TMIN
928 and grows up to MIN. TLSDPLT indicates the number of remaining
929 slots that can be used for TLS descriptors used only by TLS PLT
930 entries. */
931 struct _frvfdpic_dynamic_got_alloc_data
932 {
933 bfd_signed_vma max, cur, odd, fdcur, min;
934 bfd_signed_vma tmax, tcur, tmin;
935 bfd_vma fdplt, tlsdplt;
936 } got12, gotlos, gothilo;
937 };
938
939 /* Create an FRV ELF linker hash table. */
940
941 static struct bfd_link_hash_table *
942 frvfdpic_elf_link_hash_table_create (bfd *abfd)
943 {
944 struct frvfdpic_elf_link_hash_table *ret;
945 size_t amt = sizeof (struct frvfdpic_elf_link_hash_table);
946
947 ret = bfd_zmalloc (amt);
948 if (ret == NULL)
949 return NULL;
950
951 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
952 _bfd_elf_link_hash_newfunc,
953 sizeof (struct elf_link_hash_entry),
954 FRV_ELF_DATA))
955 {
956 free (ret);
957 return NULL;
958 }
959
960 return &ret->elf.root;
961 }
962
963 /* Decide whether a reference to a symbol can be resolved locally or
964 not. If the symbol is protected, we want the local address, but
965 its function descriptor must be assigned by the dynamic linker. */
966 #define FRVFDPIC_SYM_LOCAL(INFO, H) \
967 (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
968 || ! elf_hash_table (INFO)->dynamic_sections_created)
969 #define FRVFDPIC_FUNCDESC_LOCAL(INFO, H) \
970 ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
971
972 /* This structure collects information on what kind of GOT, PLT or
973 function descriptors are required by relocations that reference a
974 certain symbol. */
975 struct frvfdpic_relocs_info
976 {
977 /* The index of the symbol, as stored in the relocation r_info, if
978 we have a local symbol; -1 otherwise. */
979 long symndx;
980 union
981 {
982 /* The input bfd in which the symbol is defined, if it's a local
983 symbol. */
984 bfd *abfd;
985 /* If symndx == -1, the hash table entry corresponding to a global
986 symbol (even if it turns out to bind locally, in which case it
987 should ideally be replaced with section's symndx + addend). */
988 struct elf_link_hash_entry *h;
989 } d;
990 /* The addend of the relocation that references the symbol. */
991 bfd_vma addend;
992
993 /* The fields above are used to identify an entry. The fields below
994 contain information on how an entry is used and, later on, which
995 locations it was assigned. */
996 /* The following 3 fields record whether the symbol+addend above was
997 ever referenced with a GOT relocation. The 12 suffix indicates a
998 GOT12 relocation; los is used for GOTLO relocations that are not
999 matched by a GOTHI relocation; hilo is used for GOTLO/GOTHI
1000 pairs. */
1001 unsigned got12:1;
1002 unsigned gotlos:1;
1003 unsigned gothilo:1;
1004 /* Whether a FUNCDESC relocation references symbol+addend. */
1005 unsigned fd:1;
1006 /* Whether a FUNCDESC_GOT relocation references symbol+addend. */
1007 unsigned fdgot12:1;
1008 unsigned fdgotlos:1;
1009 unsigned fdgothilo:1;
1010 /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend. */
1011 unsigned fdgoff12:1;
1012 unsigned fdgofflos:1;
1013 unsigned fdgoffhilo:1;
1014 /* Whether a GETTLSOFF relocation references symbol+addend. */
1015 unsigned tlsplt:1;
1016 /* FIXME: we should probably add tlspltdesc, tlspltoff and
1017 tlspltimm, to tell what kind of TLS PLT entry we're generating.
1018 We might instead just pre-compute flags telling whether the
1019 object is suitable for local exec, initial exec or general
1020 dynamic addressing, and use that all over the place. We could
1021 also try to do a better job of merging TLSOFF and TLSDESC entries
1022 in main executables, but perhaps we can get rid of TLSDESC
1023 entirely in them instead. */
1024 /* Whether a GOTTLSDESC relocation references symbol+addend. */
1025 unsigned tlsdesc12:1;
1026 unsigned tlsdesclos:1;
1027 unsigned tlsdeschilo:1;
1028 /* Whether a GOTTLSOFF relocation references symbol+addend. */
1029 unsigned tlsoff12:1;
1030 unsigned tlsofflos:1;
1031 unsigned tlsoffhilo:1;
1032 /* Whether symbol+addend is referenced with GOTOFF12, GOTOFFLO or
1033 GOTOFFHI relocations. The addend doesn't really matter, since we
1034 envision that this will only be used to check whether the symbol
1035 is mapped to the same segment as the got. */
1036 unsigned gotoff:1;
1037 /* Whether symbol+addend is referenced by a LABEL24 relocation. */
1038 unsigned call:1;
1039 /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
1040 relocation. */
1041 unsigned sym:1;
1042 /* Whether we need a PLT entry for a symbol. Should be implied by
1043 something like:
1044 (call && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)) */
1045 unsigned plt:1;
1046 /* Whether a function descriptor should be created in this link unit
1047 for symbol+addend. Should be implied by something like:
1048 (plt || fdgotoff12 || fdgotofflos || fdgotofflohi
1049 || ((fd || fdgot12 || fdgotlos || fdgothilo)
1050 && (symndx != -1 || FRVFDPIC_FUNCDESC_LOCAL (info, d.h)))) */
1051 unsigned privfd:1;
1052 /* Whether a lazy PLT entry is needed for this symbol+addend.
1053 Should be implied by something like:
1054 (privfd && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)
1055 && ! (info->flags & DF_BIND_NOW)) */
1056 unsigned lazyplt:1;
1057 /* Whether we've already emitted GOT relocations and PLT entries as
1058 needed for this symbol. */
1059 unsigned done:1;
1060
1061 /* The number of R_FRV_32, R_FRV_FUNCDESC, R_FRV_FUNCDESC_VALUE and
1062 R_FRV_TLSDESC_VALUE, R_FRV_TLSOFF relocations referencing
1063 symbol+addend. */
1064 unsigned relocs32, relocsfd, relocsfdv, relocstlsd, relocstlsoff;
1065
1066 /* The number of .rofixups entries and dynamic relocations allocated
1067 for this symbol, minus any that might have already been used. */
1068 unsigned fixups, dynrelocs;
1069
1070 /* The offsets of the GOT entries assigned to symbol+addend, to the
1071 function descriptor's address, and to a function descriptor,
1072 respectively. Should be zero if unassigned. The offsets are
1073 counted from the value that will be assigned to the PIC register,
1074 not from the beginning of the .got section. */
1075 bfd_signed_vma got_entry, fdgot_entry, fd_entry;
1076 /* The offsets of the PLT entries assigned to symbol+addend,
1077 non-lazy and lazy, respectively. If unassigned, should be
1078 (bfd_vma)-1. */
1079 bfd_vma plt_entry, lzplt_entry;
1080 /* The offsets of the GOT entries for TLS offset and TLS descriptor. */
1081 bfd_signed_vma tlsoff_entry, tlsdesc_entry;
1082 /* The offset of the TLS offset PLT entry. */
1083 bfd_vma tlsplt_entry;
1084 };
1085
1086 /* Compute a hash with the key fields of an frvfdpic_relocs_info entry. */
1087 static hashval_t
1088 frvfdpic_relocs_info_hash (const void *entry_)
1089 {
1090 const struct frvfdpic_relocs_info *entry = entry_;
1091
1092 return (entry->symndx == -1
1093 ? (long) entry->d.h->root.root.hash
1094 : entry->symndx + (long) entry->d.abfd->id * 257) + entry->addend;
1095 }
1096
1097 /* Test whether the key fields of two frvfdpic_relocs_info entries are
1098 identical. */
1099 static int
1100 frvfdpic_relocs_info_eq (const void *entry1, const void *entry2)
1101 {
1102 const struct frvfdpic_relocs_info *e1 = entry1;
1103 const struct frvfdpic_relocs_info *e2 = entry2;
1104
1105 return e1->symndx == e2->symndx && e1->addend == e2->addend
1106 && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
1107 }
1108
1109 /* Find or create an entry in a hash table HT that matches the key
1110 fields of the given ENTRY. If it's not found, memory for a new
1111 entry is allocated in ABFD's obstack. */
1112 static struct frvfdpic_relocs_info *
1113 frvfdpic_relocs_info_find (struct htab *ht,
1114 bfd *abfd,
1115 const struct frvfdpic_relocs_info *entry,
1116 enum insert_option insert)
1117 {
1118 struct frvfdpic_relocs_info **loc =
1119 (struct frvfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
1120
1121 if (! loc)
1122 return NULL;
1123
1124 if (*loc)
1125 return *loc;
1126
1127 *loc = bfd_zalloc (abfd, sizeof (**loc));
1128
1129 if (! *loc)
1130 return *loc;
1131
1132 (*loc)->symndx = entry->symndx;
1133 (*loc)->d = entry->d;
1134 (*loc)->addend = entry->addend;
1135 (*loc)->plt_entry = (bfd_vma)-1;
1136 (*loc)->lzplt_entry = (bfd_vma)-1;
1137 (*loc)->tlsplt_entry = (bfd_vma)-1;
1138
1139 return *loc;
1140 }
1141
1142 /* Obtain the address of the entry in HT associated with H's symbol +
1143 addend, creating a new entry if none existed. ABFD is only used
1144 for memory allocation purposes. */
1145 inline static struct frvfdpic_relocs_info *
1146 frvfdpic_relocs_info_for_global (struct htab *ht,
1147 bfd *abfd,
1148 struct elf_link_hash_entry *h,
1149 bfd_vma addend,
1150 enum insert_option insert)
1151 {
1152 struct frvfdpic_relocs_info entry;
1153
1154 entry.symndx = -1;
1155 entry.d.h = h;
1156 entry.addend = addend;
1157
1158 return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
1159 }
1160
1161 /* Obtain the address of the entry in HT associated with the SYMNDXth
1162 local symbol of the input bfd ABFD, plus the addend, creating a new
1163 entry if none existed. */
1164 inline static struct frvfdpic_relocs_info *
1165 frvfdpic_relocs_info_for_local (struct htab *ht,
1166 bfd *abfd,
1167 long symndx,
1168 bfd_vma addend,
1169 enum insert_option insert)
1170 {
1171 struct frvfdpic_relocs_info entry;
1172
1173 entry.symndx = symndx;
1174 entry.d.abfd = abfd;
1175 entry.addend = addend;
1176
1177 return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
1178 }
1179
1180 /* Merge fields set by check_relocs() of two entries that end up being
1181 mapped to the same (presumably global) symbol. */
1182
1183 inline static void
1184 frvfdpic_pic_merge_early_relocs_info (struct frvfdpic_relocs_info *e2,
1185 struct frvfdpic_relocs_info const *e1)
1186 {
1187 e2->got12 |= e1->got12;
1188 e2->gotlos |= e1->gotlos;
1189 e2->gothilo |= e1->gothilo;
1190 e2->fd |= e1->fd;
1191 e2->fdgot12 |= e1->fdgot12;
1192 e2->fdgotlos |= e1->fdgotlos;
1193 e2->fdgothilo |= e1->fdgothilo;
1194 e2->fdgoff12 |= e1->fdgoff12;
1195 e2->fdgofflos |= e1->fdgofflos;
1196 e2->fdgoffhilo |= e1->fdgoffhilo;
1197 e2->tlsplt |= e1->tlsplt;
1198 e2->tlsdesc12 |= e1->tlsdesc12;
1199 e2->tlsdesclos |= e1->tlsdesclos;
1200 e2->tlsdeschilo |= e1->tlsdeschilo;
1201 e2->tlsoff12 |= e1->tlsoff12;
1202 e2->tlsofflos |= e1->tlsofflos;
1203 e2->tlsoffhilo |= e1->tlsoffhilo;
1204 e2->gotoff |= e1->gotoff;
1205 e2->call |= e1->call;
1206 e2->sym |= e1->sym;
1207 }
1208
1209 /* Every block of 65535 lazy PLT entries shares a single call to the
1210 resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
1211 32767, counting from 0). All other lazy PLT entries branch to it
1212 in a single instruction. */
1213
1214 #define FRVFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) 8 * 65535 + 4)
1215 #define FRVFDPIC_LZPLT_RESOLV_LOC (8 * 32767)
1216
1217 /* Add a dynamic relocation to the SRELOC section. */
1218
1219 inline static bfd_vma
1220 _frvfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
1221 int reloc_type, long dynindx, bfd_vma addend,
1222 struct frvfdpic_relocs_info *entry)
1223 {
1224 Elf_Internal_Rela outrel;
1225 bfd_vma reloc_offset;
1226
1227 outrel.r_offset = offset;
1228 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
1229 outrel.r_addend = addend;
1230
1231 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
1232 BFD_ASSERT (reloc_offset < sreloc->size);
1233 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1234 sreloc->contents + reloc_offset);
1235 sreloc->reloc_count++;
1236
1237 /* If the entry's index is zero, this relocation was probably to a
1238 linkonce section that got discarded. We reserved a dynamic
1239 relocation, but it was for another entry than the one we got at
1240 the time of emitting the relocation. Unfortunately there's no
1241 simple way for us to catch this situation, since the relocation
1242 is cleared right before calling relocate_section, at which point
1243 we no longer know what the relocation used to point to. */
1244 if (entry->symndx)
1245 {
1246 BFD_ASSERT (entry->dynrelocs > 0);
1247 entry->dynrelocs--;
1248 }
1249
1250 return reloc_offset;
1251 }
1252
1253 /* Add a fixup to the ROFIXUP section. */
1254
1255 static bfd_vma
1256 _frvfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
1257 struct frvfdpic_relocs_info *entry)
1258 {
1259 bfd_vma fixup_offset;
1260
1261 if (rofixup->flags & SEC_EXCLUDE)
1262 return -1;
1263
1264 fixup_offset = rofixup->reloc_count * 4;
1265 if (rofixup->contents)
1266 {
1267 BFD_ASSERT (fixup_offset < rofixup->size);
1268 bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
1269 }
1270 rofixup->reloc_count++;
1271
1272 if (entry && entry->symndx)
1273 {
1274 /* See discussion about symndx == 0 in _frvfdpic_add_dyn_reloc
1275 above. */
1276 BFD_ASSERT (entry->fixups > 0);
1277 entry->fixups--;
1278 }
1279
1280 return fixup_offset;
1281 }
1282
1283 /* Find the segment number in which OSEC, and output section, is
1284 located. */
1285
1286 static unsigned
1287 _frvfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
1288 {
1289 Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
1290
1291 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
1292 }
1293
1294 inline static bfd_boolean
1295 _frvfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
1296 {
1297 unsigned seg = _frvfdpic_osec_to_segment (output_bfd, osec);
1298
1299 return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
1300 }
1301
1302 #define FRVFDPIC_TLS_BIAS (2048 - 16)
1303
1304 /* Return the base VMA address which should be subtracted from real addresses
1305 when resolving TLSMOFF relocation.
1306 This is PT_TLS segment p_vaddr, plus the 2048-16 bias. */
1307
1308 static bfd_vma
1309 tls_biased_base (struct bfd_link_info *info)
1310 {
1311 /* If tls_sec is NULL, we should have signalled an error already. */
1312 if (elf_hash_table (info)->tls_sec == NULL)
1313 return FRVFDPIC_TLS_BIAS;
1314 return elf_hash_table (info)->tls_sec->vma + FRVFDPIC_TLS_BIAS;
1315 }
1316
1317 /* Generate relocations for GOT entries, function descriptors, and
1318 code for PLT and lazy PLT entries. */
1319
1320 inline static bfd_boolean
1321 _frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
1322 bfd *output_bfd,
1323 struct bfd_link_info *info,
1324 asection *sec,
1325 Elf_Internal_Sym *sym,
1326 bfd_vma addend)
1327
1328 {
1329 bfd_vma fd_lazy_rel_offset = (bfd_vma)-1;
1330 int dynindx = -1;
1331
1332 if (entry->done)
1333 return TRUE;
1334 entry->done = 1;
1335
1336 if (entry->got_entry || entry->fdgot_entry || entry->fd_entry
1337 || entry->tlsoff_entry || entry->tlsdesc_entry)
1338 {
1339 /* If the symbol is dynamic, consider it for dynamic
1340 relocations, otherwise decay to section + offset. */
1341 if (entry->symndx == -1 && entry->d.h->dynindx != -1)
1342 dynindx = entry->d.h->dynindx;
1343 else
1344 {
1345 if (sec
1346 && sec->output_section
1347 && ! bfd_is_abs_section (sec->output_section)
1348 && ! bfd_is_und_section (sec->output_section))
1349 dynindx = elf_section_data (sec->output_section)->dynindx;
1350 else
1351 dynindx = 0;
1352 }
1353 }
1354
1355 /* Generate relocation for GOT entry pointing to the symbol. */
1356 if (entry->got_entry)
1357 {
1358 int idx = dynindx;
1359 bfd_vma ad = addend;
1360
1361 /* If the symbol is dynamic but binds locally, use
1362 section+offset. */
1363 if (sec && (entry->symndx != -1
1364 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1365 {
1366 if (entry->symndx == -1)
1367 ad += entry->d.h->root.u.def.value;
1368 else
1369 ad += sym->st_value;
1370 ad += sec->output_offset;
1371 if (sec->output_section && elf_section_data (sec->output_section))
1372 idx = elf_section_data (sec->output_section)->dynindx;
1373 else
1374 idx = 0;
1375 }
1376
1377 /* If we're linking an executable at a fixed address, we can
1378 omit the dynamic relocation as long as the symbol is local to
1379 this module. */
1380 if (bfd_link_pde (info)
1381 && (entry->symndx != -1
1382 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1383 {
1384 if (sec)
1385 ad += sec->output_section->vma;
1386 if (entry->symndx != -1
1387 || entry->d.h->root.type != bfd_link_hash_undefweak)
1388 _frvfdpic_add_rofixup (output_bfd,
1389 frvfdpic_gotfixup_section (info),
1390 frvfdpic_got_section (info)->output_section
1391 ->vma
1392 + frvfdpic_got_section (info)->output_offset
1393 + frvfdpic_got_initial_offset (info)
1394 + entry->got_entry, entry);
1395 }
1396 else
1397 _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1398 _bfd_elf_section_offset
1399 (output_bfd, info,
1400 frvfdpic_got_section (info),
1401 frvfdpic_got_initial_offset (info)
1402 + entry->got_entry)
1403 + frvfdpic_got_section (info)
1404 ->output_section->vma
1405 + frvfdpic_got_section (info)->output_offset,
1406 R_FRV_32, idx, ad, entry);
1407
1408 bfd_put_32 (output_bfd, ad,
1409 frvfdpic_got_section (info)->contents
1410 + frvfdpic_got_initial_offset (info)
1411 + entry->got_entry);
1412 }
1413
1414 /* Generate relocation for GOT entry pointing to a canonical
1415 function descriptor. */
1416 if (entry->fdgot_entry)
1417 {
1418 int reloc, idx;
1419 bfd_vma ad = 0;
1420
1421 if (! (entry->symndx == -1
1422 && entry->d.h->root.type == bfd_link_hash_undefweak
1423 && FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1424 {
1425 /* If the symbol is dynamic and there may be dynamic symbol
1426 resolution because we are, or are linked with, a shared
1427 library, emit a FUNCDESC relocation such that the dynamic
1428 linker will allocate the function descriptor. If the
1429 symbol needs a non-local function descriptor but binds
1430 locally (e.g., its visibility is protected, emit a
1431 dynamic relocation decayed to section+offset. */
1432 if (entry->symndx == -1
1433 && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
1434 && FRVFDPIC_SYM_LOCAL (info, entry->d.h)
1435 && !bfd_link_pde (info))
1436 {
1437 reloc = R_FRV_FUNCDESC;
1438 idx = elf_section_data (entry->d.h->root.u.def.section
1439 ->output_section)->dynindx;
1440 ad = entry->d.h->root.u.def.section->output_offset
1441 + entry->d.h->root.u.def.value;
1442 }
1443 else if (entry->symndx == -1
1444 && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
1445 {
1446 reloc = R_FRV_FUNCDESC;
1447 idx = dynindx;
1448 ad = addend;
1449 if (ad)
1450 {
1451 (*info->callbacks->reloc_dangerous)
1452 (info, _("relocation requires zero addend"),
1453 elf_hash_table (info)->dynobj,
1454 frvfdpic_got_section (info),
1455 entry->fdgot_entry);
1456 return FALSE;
1457 }
1458 }
1459 else
1460 {
1461 /* Otherwise, we know we have a private function descriptor,
1462 so reference it directly. */
1463 if (elf_hash_table (info)->dynamic_sections_created)
1464 BFD_ASSERT (entry->privfd);
1465 reloc = R_FRV_32;
1466 idx = elf_section_data (frvfdpic_got_section (info)
1467 ->output_section)->dynindx;
1468 ad = frvfdpic_got_section (info)->output_offset
1469 + frvfdpic_got_initial_offset (info) + entry->fd_entry;
1470 }
1471
1472 /* If there is room for dynamic symbol resolution, emit the
1473 dynamic relocation. However, if we're linking an
1474 executable at a fixed location, we won't have emitted a
1475 dynamic symbol entry for the got section, so idx will be
1476 zero, which means we can and should compute the address
1477 of the private descriptor ourselves. */
1478 if (bfd_link_pde (info)
1479 && (entry->symndx != -1
1480 || FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
1481 {
1482 ad += frvfdpic_got_section (info)->output_section->vma;
1483 _frvfdpic_add_rofixup (output_bfd,
1484 frvfdpic_gotfixup_section (info),
1485 frvfdpic_got_section (info)
1486 ->output_section->vma
1487 + frvfdpic_got_section (info)
1488 ->output_offset
1489 + frvfdpic_got_initial_offset (info)
1490 + entry->fdgot_entry, entry);
1491 }
1492 else
1493 _frvfdpic_add_dyn_reloc (output_bfd,
1494 frvfdpic_gotrel_section (info),
1495 _bfd_elf_section_offset
1496 (output_bfd, info,
1497 frvfdpic_got_section (info),
1498 frvfdpic_got_initial_offset (info)
1499 + entry->fdgot_entry)
1500 + frvfdpic_got_section (info)
1501 ->output_section->vma
1502 + frvfdpic_got_section (info)
1503 ->output_offset,
1504 reloc, idx, ad, entry);
1505 }
1506
1507 bfd_put_32 (output_bfd, ad,
1508 frvfdpic_got_section (info)->contents
1509 + frvfdpic_got_initial_offset (info)
1510 + entry->fdgot_entry);
1511 }
1512
1513 /* Generate relocation to fill in a private function descriptor in
1514 the GOT. */
1515 if (entry->fd_entry)
1516 {
1517 int idx = dynindx;
1518 bfd_vma ad = addend;
1519 bfd_vma ofst;
1520 long lowword, highword;
1521
1522 /* If the symbol is dynamic but binds locally, use
1523 section+offset. */
1524 if (sec && (entry->symndx != -1
1525 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1526 {
1527 if (entry->symndx == -1)
1528 ad += entry->d.h->root.u.def.value;
1529 else
1530 ad += sym->st_value;
1531 ad += sec->output_offset;
1532 if (sec->output_section && elf_section_data (sec->output_section))
1533 idx = elf_section_data (sec->output_section)->dynindx;
1534 else
1535 idx = 0;
1536 }
1537
1538 /* If we're linking an executable at a fixed address, we can
1539 omit the dynamic relocation as long as the symbol is local to
1540 this module. */
1541 if (bfd_link_pde (info)
1542 && (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1543 {
1544 if (sec)
1545 ad += sec->output_section->vma;
1546 ofst = 0;
1547 if (entry->symndx != -1
1548 || entry->d.h->root.type != bfd_link_hash_undefweak)
1549 {
1550 _frvfdpic_add_rofixup (output_bfd,
1551 frvfdpic_gotfixup_section (info),
1552 frvfdpic_got_section (info)
1553 ->output_section->vma
1554 + frvfdpic_got_section (info)
1555 ->output_offset
1556 + frvfdpic_got_initial_offset (info)
1557 + entry->fd_entry, entry);
1558 _frvfdpic_add_rofixup (output_bfd,
1559 frvfdpic_gotfixup_section (info),
1560 frvfdpic_got_section (info)
1561 ->output_section->vma
1562 + frvfdpic_got_section (info)
1563 ->output_offset
1564 + frvfdpic_got_initial_offset (info)
1565 + entry->fd_entry + 4, entry);
1566 }
1567 }
1568 else
1569 {
1570 ofst =
1571 _frvfdpic_add_dyn_reloc (output_bfd,
1572 entry->lazyplt
1573 ? frvfdpic_pltrel_section (info)
1574 : frvfdpic_gotrel_section (info),
1575 _bfd_elf_section_offset
1576 (output_bfd, info,
1577 frvfdpic_got_section (info),
1578 frvfdpic_got_initial_offset (info)
1579 + entry->fd_entry)
1580 + frvfdpic_got_section (info)
1581 ->output_section->vma
1582 + frvfdpic_got_section (info)
1583 ->output_offset,
1584 R_FRV_FUNCDESC_VALUE, idx, ad, entry);
1585 }
1586
1587 /* If we've omitted the dynamic relocation, just emit the fixed
1588 addresses of the symbol and of the local GOT base offset. */
1589 if (bfd_link_pde (info)
1590 && sec
1591 && sec->output_section)
1592 {
1593 lowword = ad;
1594 highword = frvfdpic_got_section (info)->output_section->vma
1595 + frvfdpic_got_section (info)->output_offset
1596 + frvfdpic_got_initial_offset (info);
1597 }
1598 else if (entry->lazyplt)
1599 {
1600 if (ad)
1601 {
1602 (*info->callbacks->reloc_dangerous)
1603 (info, _("relocation requires zero addend"),
1604 elf_hash_table (info)->dynobj,
1605 frvfdpic_got_section (info),
1606 entry->fd_entry);
1607 return FALSE;
1608 }
1609
1610 fd_lazy_rel_offset = ofst;
1611
1612 /* A function descriptor used for lazy or local resolving is
1613 initialized such that its high word contains the output
1614 section index in which the PLT entries are located, and
1615 the low word contains the address of the lazy PLT entry
1616 entry point, that must be within the memory region
1617 assigned to that section. */
1618 lowword = entry->lzplt_entry + 4
1619 + frvfdpic_plt_section (info)->output_offset
1620 + frvfdpic_plt_section (info)->output_section->vma;
1621 highword = _frvfdpic_osec_to_segment
1622 (output_bfd, frvfdpic_plt_section (info)->output_section);
1623 }
1624 else
1625 {
1626 /* A function descriptor for a local function gets the index
1627 of the section. For a non-local function, it's
1628 disregarded. */
1629 lowword = ad;
1630 if (sec == NULL
1631 || (entry->symndx == -1 && entry->d.h->dynindx != -1
1632 && entry->d.h->dynindx == idx))
1633 highword = 0;
1634 else
1635 highword = _frvfdpic_osec_to_segment
1636 (output_bfd, sec->output_section);
1637 }
1638
1639 bfd_put_32 (output_bfd, lowword,
1640 frvfdpic_got_section (info)->contents
1641 + frvfdpic_got_initial_offset (info)
1642 + entry->fd_entry);
1643 bfd_put_32 (output_bfd, highword,
1644 frvfdpic_got_section (info)->contents
1645 + frvfdpic_got_initial_offset (info)
1646 + entry->fd_entry + 4);
1647 }
1648
1649 /* Generate code for the PLT entry. */
1650 if (entry->plt_entry != (bfd_vma) -1)
1651 {
1652 bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1653 + entry->plt_entry;
1654
1655 BFD_ASSERT (entry->fd_entry);
1656
1657 /* Figure out what kind of PLT entry we need, depending on the
1658 location of the function descriptor within the GOT. */
1659 if (entry->fd_entry >= -(1 << (12 - 1))
1660 && entry->fd_entry < (1 << (12 - 1)))
1661 {
1662 /* lddi @(gr15, fd_entry), gr14 */
1663 bfd_put_32 (output_bfd,
1664 0x9cccf000 | (entry->fd_entry & ((1 << 12) - 1)),
1665 plt_code);
1666 plt_code += 4;
1667 }
1668 else
1669 {
1670 if (entry->fd_entry >= -(1 << (16 - 1))
1671 && entry->fd_entry < (1 << (16 - 1)))
1672 {
1673 /* setlos lo(fd_entry), gr14 */
1674 bfd_put_32 (output_bfd,
1675 0x9cfc0000
1676 | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1677 plt_code);
1678 plt_code += 4;
1679 }
1680 else
1681 {
1682 /* sethi.p hi(fd_entry), gr14
1683 setlo lo(fd_entry), gr14 */
1684 bfd_put_32 (output_bfd,
1685 0x1cf80000
1686 | ((entry->fd_entry >> 16)
1687 & (((bfd_vma)1 << 16) - 1)),
1688 plt_code);
1689 plt_code += 4;
1690 bfd_put_32 (output_bfd,
1691 0x9cf40000
1692 | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1693 plt_code);
1694 plt_code += 4;
1695 }
1696 /* ldd @(gr14,gr15),gr14 */
1697 bfd_put_32 (output_bfd, 0x9c08e14f, plt_code);
1698 plt_code += 4;
1699 }
1700 /* jmpl @(gr14,gr0) */
1701 bfd_put_32 (output_bfd, 0x8030e000, plt_code);
1702 }
1703
1704 /* Generate code for the lazy PLT entry. */
1705 if (entry->lzplt_entry != (bfd_vma) -1)
1706 {
1707 bfd_byte *lzplt_code = frvfdpic_plt_section (info)->contents
1708 + entry->lzplt_entry;
1709 bfd_vma resolverStub_addr;
1710
1711 bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
1712 lzplt_code += 4;
1713
1714 resolverStub_addr = entry->lzplt_entry / FRVFDPIC_LZPLT_BLOCK_SIZE
1715 * FRVFDPIC_LZPLT_BLOCK_SIZE + FRVFDPIC_LZPLT_RESOLV_LOC;
1716 if (resolverStub_addr >= frvfdpic_plt_initial_offset (info))
1717 resolverStub_addr = frvfdpic_plt_initial_offset (info) - 12;
1718
1719 if (entry->lzplt_entry == resolverStub_addr)
1720 {
1721 /* This is a lazy PLT entry that includes a resolver call. */
1722 /* ldd @(gr15,gr0), gr4
1723 jmpl @(gr4,gr0) */
1724 bfd_put_32 (output_bfd, 0x8808f140, lzplt_code);
1725 bfd_put_32 (output_bfd, 0x80304000, lzplt_code + 4);
1726 }
1727 else
1728 {
1729 /* bra resolverStub */
1730 bfd_put_32 (output_bfd,
1731 0xc01a0000
1732 | (((resolverStub_addr - entry->lzplt_entry)
1733 / 4) & (((bfd_vma)1 << 16) - 1)),
1734 lzplt_code);
1735 }
1736 }
1737
1738 /* Generate relocation for GOT entry holding the TLS offset. */
1739 if (entry->tlsoff_entry)
1740 {
1741 int idx = dynindx;
1742 bfd_vma ad = addend;
1743
1744 if (entry->symndx != -1
1745 || FRVFDPIC_SYM_LOCAL (info, entry->d.h))
1746 {
1747 /* If the symbol is dynamic but binds locally, use
1748 section+offset. */
1749 if (sec)
1750 {
1751 if (entry->symndx == -1)
1752 ad += entry->d.h->root.u.def.value;
1753 else
1754 ad += sym->st_value;
1755 ad += sec->output_offset;
1756 if (sec->output_section
1757 && elf_section_data (sec->output_section))
1758 idx = elf_section_data (sec->output_section)->dynindx;
1759 else
1760 idx = 0;
1761 }
1762 }
1763
1764 /* *ABS*+addend is special for TLS relocations, use only the
1765 addend. */
1766 if (bfd_link_executable (info)
1767 && idx == 0
1768 && (bfd_is_abs_section (sec)
1769 || bfd_is_und_section (sec)))
1770 ;
1771 /* If we're linking an executable, we can entirely omit the
1772 dynamic relocation if the symbol is local to this module. */
1773 else if (bfd_link_executable (info)
1774 && (entry->symndx != -1
1775 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1776 {
1777 if (sec)
1778 ad += sec->output_section->vma - tls_biased_base (info);
1779 }
1780 else
1781 {
1782 if (idx == 0
1783 && (bfd_is_abs_section (sec)
1784 || bfd_is_und_section (sec)))
1785 {
1786 if (! elf_hash_table (info)->tls_sec)
1787 {
1788 (*info->callbacks->undefined_symbol)
1789 (info, "TLS section", elf_hash_table (info)->dynobj,
1790 frvfdpic_got_section (info), entry->tlsoff_entry, TRUE);
1791 return FALSE;
1792 }
1793 idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
1794 ad += FRVFDPIC_TLS_BIAS;
1795 }
1796 _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1797 _bfd_elf_section_offset
1798 (output_bfd, info,
1799 frvfdpic_got_section (info),
1800 frvfdpic_got_initial_offset (info)
1801 + entry->tlsoff_entry)
1802 + frvfdpic_got_section (info)
1803 ->output_section->vma
1804 + frvfdpic_got_section (info)
1805 ->output_offset,
1806 R_FRV_TLSOFF, idx, ad, entry);
1807 }
1808
1809 bfd_put_32 (output_bfd, ad,
1810 frvfdpic_got_section (info)->contents
1811 + frvfdpic_got_initial_offset (info)
1812 + entry->tlsoff_entry);
1813 }
1814
1815 if (entry->tlsdesc_entry)
1816 {
1817 int idx = dynindx;
1818 bfd_vma ad = addend;
1819
1820 /* If the symbol is dynamic but binds locally, use
1821 section+offset. */
1822 if (sec && (entry->symndx != -1
1823 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1824 {
1825 if (entry->symndx == -1)
1826 ad += entry->d.h->root.u.def.value;
1827 else
1828 ad += sym->st_value;
1829 ad += sec->output_offset;
1830 if (sec->output_section && elf_section_data (sec->output_section))
1831 idx = elf_section_data (sec->output_section)->dynindx;
1832 else
1833 idx = 0;
1834 }
1835
1836 /* If we didn't set up a TLS offset entry, but we're linking an
1837 executable and the symbol binds locally, we can use the
1838 module offset in the TLS descriptor in relaxations. */
1839 if (bfd_link_executable (info) && ! entry->tlsoff_entry)
1840 entry->tlsoff_entry = entry->tlsdesc_entry + 4;
1841
1842 if (bfd_link_pde (info)
1843 && ((idx == 0
1844 && (bfd_is_abs_section (sec)
1845 || bfd_is_und_section (sec)))
1846 || entry->symndx != -1
1847 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1848 {
1849 /* *ABS*+addend is special for TLS relocations, use only the
1850 addend for the TLS offset, and take the module id as
1851 0. */
1852 if (idx == 0
1853 && (bfd_is_abs_section (sec)
1854 || bfd_is_und_section (sec)))
1855 ;
1856 /* For other TLS symbols that bind locally, add the section
1857 TLS offset to the addend. */
1858 else if (sec)
1859 ad += sec->output_section->vma - tls_biased_base (info);
1860
1861 bfd_put_32 (output_bfd,
1862 frvfdpic_plt_section (info)->output_section->vma
1863 + frvfdpic_plt_section (info)->output_offset
1864 + frvfdpic_plt_tls_ret_offset (info),
1865 frvfdpic_got_section (info)->contents
1866 + frvfdpic_got_initial_offset (info)
1867 + entry->tlsdesc_entry);
1868
1869 _frvfdpic_add_rofixup (output_bfd,
1870 frvfdpic_gotfixup_section (info),
1871 frvfdpic_got_section (info)
1872 ->output_section->vma
1873 + frvfdpic_got_section (info)
1874 ->output_offset
1875 + frvfdpic_got_initial_offset (info)
1876 + entry->tlsdesc_entry, entry);
1877
1878 BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs);
1879
1880 /* We've used one of the reserved fixups, so discount it so
1881 that we can check at the end that we've used them
1882 all. */
1883 frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs--;
1884
1885 /* While at that, make sure the ret instruction makes to the
1886 right location in the PLT. We could do it only when we
1887 got to 0, but since the check at the end will only print
1888 a warning, make sure we have the ret in place in case the
1889 warning is missed. */
1890 bfd_put_32 (output_bfd, 0xc03a4000,
1891 frvfdpic_plt_section (info)->contents
1892 + frvfdpic_plt_tls_ret_offset (info));
1893 }
1894 else
1895 {
1896 if (idx == 0
1897 && (bfd_is_abs_section (sec)
1898 || bfd_is_und_section (sec)))
1899 {
1900 if (! elf_hash_table (info)->tls_sec)
1901 {
1902 (*info->callbacks->undefined_symbol)
1903 (info, "TLS section", elf_hash_table (info)->dynobj,
1904 frvfdpic_got_section (info), entry->tlsdesc_entry, TRUE);
1905 return FALSE;
1906 }
1907 idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
1908 ad += FRVFDPIC_TLS_BIAS;
1909 }
1910
1911 _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1912 _bfd_elf_section_offset
1913 (output_bfd, info,
1914 frvfdpic_got_section (info),
1915 frvfdpic_got_initial_offset (info)
1916 + entry->tlsdesc_entry)
1917 + frvfdpic_got_section (info)
1918 ->output_section->vma
1919 + frvfdpic_got_section (info)
1920 ->output_offset,
1921 R_FRV_TLSDESC_VALUE, idx, ad, entry);
1922
1923 bfd_put_32 (output_bfd, 0,
1924 frvfdpic_got_section (info)->contents
1925 + frvfdpic_got_initial_offset (info)
1926 + entry->tlsdesc_entry);
1927 }
1928
1929 bfd_put_32 (output_bfd, ad,
1930 frvfdpic_got_section (info)->contents
1931 + frvfdpic_got_initial_offset (info)
1932 + entry->tlsdesc_entry + 4);
1933 }
1934
1935 /* Generate code for the get-TLS-offset PLT entry. */
1936 if (entry->tlsplt_entry != (bfd_vma) -1)
1937 {
1938 bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1939 + entry->tlsplt_entry;
1940
1941 if (bfd_link_executable (info)
1942 && (entry->symndx != -1
1943 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1944 {
1945 int idx = dynindx;
1946 bfd_vma ad = addend;
1947
1948 /* sec may be NULL when referencing an undefweak symbol
1949 while linking a static executable. */
1950 if (!sec)
1951 {
1952 BFD_ASSERT (entry->symndx == -1
1953 && entry->d.h->root.type == bfd_link_hash_undefweak);
1954 }
1955 else
1956 {
1957 if (entry->symndx == -1)
1958 ad += entry->d.h->root.u.def.value;
1959 else
1960 ad += sym->st_value;
1961 ad += sec->output_offset;
1962 if (sec->output_section
1963 && elf_section_data (sec->output_section))
1964 idx = elf_section_data (sec->output_section)->dynindx;
1965 else
1966 idx = 0;
1967 }
1968
1969 /* *ABS*+addend is special for TLS relocations, use only the
1970 addend for the TLS offset, and take the module id as
1971 0. */
1972 if (idx == 0
1973 && (bfd_is_abs_section (sec)
1974 || bfd_is_und_section (sec)))
1975 ;
1976 /* For other TLS symbols that bind locally, add the section
1977 TLS offset to the addend. */
1978 else if (sec)
1979 ad += sec->output_section->vma - tls_biased_base (info);
1980
1981 if ((bfd_signed_vma)ad >= -(1 << (16 - 1))
1982 && (bfd_signed_vma)ad < (1 << (16 - 1)))
1983 {
1984 /* setlos lo(ad), gr9 */
1985 bfd_put_32 (output_bfd,
1986 0x92fc0000
1987 | (ad
1988 & (((bfd_vma)1 << 16) - 1)),
1989 plt_code);
1990 plt_code += 4;
1991 }
1992 else
1993 {
1994 /* sethi.p hi(ad), gr9
1995 setlo lo(ad), gr9 */
1996 bfd_put_32 (output_bfd,
1997 0x12f80000
1998 | ((ad >> 16)
1999 & (((bfd_vma)1 << 16) - 1)),
2000 plt_code);
2001 plt_code += 4;
2002 bfd_put_32 (output_bfd,
2003 0x92f40000
2004 | (ad
2005 & (((bfd_vma)1 << 16) - 1)),
2006 plt_code);
2007 plt_code += 4;
2008 }
2009 /* ret */
2010 bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
2011 }
2012 else if (entry->tlsoff_entry)
2013 {
2014 /* Figure out what kind of PLT entry we need, depending on the
2015 location of the TLS descriptor within the GOT. */
2016 if (entry->tlsoff_entry >= -(1 << (12 - 1))
2017 && entry->tlsoff_entry < (1 << (12 - 1)))
2018 {
2019 /* ldi @(gr15, tlsoff_entry), gr9 */
2020 bfd_put_32 (output_bfd,
2021 0x92c8f000 | (entry->tlsoff_entry
2022 & ((1 << 12) - 1)),
2023 plt_code);
2024 plt_code += 4;
2025 }
2026 else
2027 {
2028 if (entry->tlsoff_entry >= -(1 << (16 - 1))
2029 && entry->tlsoff_entry < (1 << (16 - 1)))
2030 {
2031 /* setlos lo(tlsoff_entry), gr8 */
2032 bfd_put_32 (output_bfd,
2033 0x90fc0000
2034 | (entry->tlsoff_entry
2035 & (((bfd_vma)1 << 16) - 1)),
2036 plt_code);
2037 plt_code += 4;
2038 }
2039 else
2040 {
2041 /* sethi.p hi(tlsoff_entry), gr8
2042 setlo lo(tlsoff_entry), gr8 */
2043 bfd_put_32 (output_bfd,
2044 0x10f80000
2045 | ((entry->tlsoff_entry >> 16)
2046 & (((bfd_vma)1 << 16) - 1)),
2047 plt_code);
2048 plt_code += 4;
2049 bfd_put_32 (output_bfd,
2050 0x90f40000
2051 | (entry->tlsoff_entry
2052 & (((bfd_vma)1 << 16) - 1)),
2053 plt_code);
2054 plt_code += 4;
2055 }
2056 /* ld @(gr15,gr8),gr9 */
2057 bfd_put_32 (output_bfd, 0x9008f108, plt_code);
2058 plt_code += 4;
2059 }
2060 /* ret */
2061 bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
2062 }
2063 else
2064 {
2065 BFD_ASSERT (entry->tlsdesc_entry);
2066
2067 /* Figure out what kind of PLT entry we need, depending on the
2068 location of the TLS descriptor within the GOT. */
2069 if (entry->tlsdesc_entry >= -(1 << (12 - 1))
2070 && entry->tlsdesc_entry < (1 << (12 - 1)))
2071 {
2072 /* lddi @(gr15, tlsdesc_entry), gr8 */
2073 bfd_put_32 (output_bfd,
2074 0x90ccf000 | (entry->tlsdesc_entry
2075 & ((1 << 12) - 1)),
2076 plt_code);
2077 plt_code += 4;
2078 }
2079 else
2080 {
2081 if (entry->tlsdesc_entry >= -(1 << (16 - 1))
2082 && entry->tlsdesc_entry < (1 << (16 - 1)))
2083 {
2084 /* setlos lo(tlsdesc_entry), gr8 */
2085 bfd_put_32 (output_bfd,
2086 0x90fc0000
2087 | (entry->tlsdesc_entry
2088 & (((bfd_vma)1 << 16) - 1)),
2089 plt_code);
2090 plt_code += 4;
2091 }
2092 else
2093 {
2094 /* sethi.p hi(tlsdesc_entry), gr8
2095 setlo lo(tlsdesc_entry), gr8 */
2096 bfd_put_32 (output_bfd,
2097 0x10f80000
2098 | ((entry->tlsdesc_entry >> 16)
2099 & (((bfd_vma)1 << 16) - 1)),
2100 plt_code);
2101 plt_code += 4;
2102 bfd_put_32 (output_bfd,
2103 0x90f40000
2104 | (entry->tlsdesc_entry
2105 & (((bfd_vma)1 << 16) - 1)),
2106 plt_code);
2107 plt_code += 4;
2108 }
2109 /* ldd @(gr15,gr8),gr8 */
2110 bfd_put_32 (output_bfd, 0x9008f148, plt_code);
2111 plt_code += 4;
2112 }
2113 /* jmpl @(gr8,gr0) */
2114 bfd_put_32 (output_bfd, 0x80308000, plt_code);
2115 }
2116 }
2117
2118 return TRUE;
2119 }
2120
2121 /* Handle an FRV small data reloc. */
2122
2123 static bfd_reloc_status_type
2124 elf32_frv_relocate_gprel12 (struct bfd_link_info *info,
2125 bfd *input_bfd,
2126 asection *input_section,
2127 Elf_Internal_Rela *relocation,
2128 bfd_byte *contents,
2129 bfd_vma value)
2130 {
2131 bfd_vma insn;
2132 bfd_vma gp;
2133 struct bfd_link_hash_entry *h;
2134
2135 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2136
2137 gp = (h->u.def.value
2138 + h->u.def.section->output_section->vma
2139 + h->u.def.section->output_offset);
2140
2141 value -= input_section->output_section->vma;
2142 value -= (gp - input_section->output_section->vma);
2143
2144 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2145
2146 value += relocation->r_addend;
2147
2148 if ((long) value > 0x7ff || (long) value < -0x800)
2149 return bfd_reloc_overflow;
2150
2151 bfd_put_32 (input_bfd,
2152 (insn & 0xfffff000) | (value & 0xfff),
2153 contents + relocation->r_offset);
2154
2155 return bfd_reloc_ok;
2156 }
2157
2158 /* Handle an FRV small data reloc. for the u12 field. */
2159
2160 static bfd_reloc_status_type
2161 elf32_frv_relocate_gprelu12 (struct bfd_link_info *info,
2162 bfd *input_bfd,
2163 asection *input_section,
2164 Elf_Internal_Rela *relocation,
2165 bfd_byte *contents,
2166 bfd_vma value)
2167 {
2168 bfd_vma insn;
2169 bfd_vma gp;
2170 struct bfd_link_hash_entry *h;
2171 bfd_vma mask;
2172
2173 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2174
2175 gp = (h->u.def.value
2176 + h->u.def.section->output_section->vma
2177 + h->u.def.section->output_offset);
2178
2179 value -= input_section->output_section->vma;
2180 value -= (gp - input_section->output_section->vma);
2181
2182 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2183
2184 value += relocation->r_addend;
2185
2186 if ((long) value > 0x7ff || (long) value < -0x800)
2187 return bfd_reloc_overflow;
2188
2189 /* The high 6 bits go into bits 17-12. The low 6 bits go into bits 5-0. */
2190 mask = 0x3f03f;
2191 insn = (insn & ~mask) | ((value & 0xfc0) << 12) | (value & 0x3f);
2192
2193 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2194
2195 return bfd_reloc_ok;
2196 }
2197
2198 /* Handle an FRV ELF HI16 reloc. */
2199
2200 static bfd_reloc_status_type
2201 elf32_frv_relocate_hi16 (bfd *input_bfd,
2202 Elf_Internal_Rela *relhi,
2203 bfd_byte *contents,
2204 bfd_vma value)
2205 {
2206 bfd_vma insn;
2207
2208 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
2209
2210 value += relhi->r_addend;
2211 value = ((value >> 16) & 0xffff);
2212
2213 insn = (insn & 0xffff0000) | value;
2214
2215 if ((long) value > 0xffff || (long) value < -0x10000)
2216 return bfd_reloc_overflow;
2217
2218 bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
2219 return bfd_reloc_ok;
2220
2221 }
2222 static bfd_reloc_status_type
2223 elf32_frv_relocate_lo16 (bfd *input_bfd,
2224 Elf_Internal_Rela *rello,
2225 bfd_byte *contents,
2226 bfd_vma value)
2227 {
2228 bfd_vma insn;
2229
2230 insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
2231
2232 value += rello->r_addend;
2233 value = value & 0xffff;
2234
2235 insn = (insn & 0xffff0000) | value;
2236
2237 if ((long) value > 0xffff || (long) value < -0x10000)
2238 return bfd_reloc_overflow;
2239
2240 bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
2241 return bfd_reloc_ok;
2242 }
2243
2244 /* Perform the relocation for the CALL label24 instruction. */
2245
2246 static bfd_reloc_status_type
2247 elf32_frv_relocate_label24 (bfd *input_bfd,
2248 asection *input_section,
2249 Elf_Internal_Rela *rello,
2250 bfd_byte *contents,
2251 bfd_vma value)
2252 {
2253 bfd_vma insn;
2254 bfd_vma label6;
2255 bfd_vma label18;
2256
2257 /* The format for the call instruction is:
2258
2259 0 000000 0001111 000000000000000000
2260 label6 opcode label18
2261
2262 The branch calculation is: pc + (4*label24)
2263 where label24 is the concatenation of label6 and label18. */
2264
2265 /* Grab the instruction. */
2266 insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
2267
2268 value -= input_section->output_section->vma + input_section->output_offset;
2269 value -= rello->r_offset;
2270 value += rello->r_addend;
2271
2272 value = value >> 2;
2273
2274 label6 = value & 0xfc0000;
2275 label6 = label6 << 7;
2276
2277 label18 = value & 0x3ffff;
2278
2279 insn = insn & 0x803c0000;
2280 insn = insn | label6;
2281 insn = insn | label18;
2282
2283 bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
2284
2285 return bfd_reloc_ok;
2286 }
2287
2288 static bfd_reloc_status_type
2289 elf32_frv_relocate_gprelhi (struct bfd_link_info *info,
2290 bfd *input_bfd,
2291 asection *input_section,
2292 Elf_Internal_Rela *relocation,
2293 bfd_byte *contents,
2294 bfd_vma value)
2295 {
2296 bfd_vma insn;
2297 bfd_vma gp;
2298 struct bfd_link_hash_entry *h;
2299
2300 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2301
2302 gp = (h->u.def.value
2303 + h->u.def.section->output_section->vma
2304 + h->u.def.section->output_offset);
2305
2306 value -= input_section->output_section->vma;
2307 value -= (gp - input_section->output_section->vma);
2308 value += relocation->r_addend;
2309 value = ((value >> 16) & 0xffff);
2310
2311 if ((long) value > 0xffff || (long) value < -0x10000)
2312 return bfd_reloc_overflow;
2313
2314 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2315 insn = (insn & 0xffff0000) | value;
2316
2317 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2318 return bfd_reloc_ok;
2319 }
2320
2321 static bfd_reloc_status_type
2322 elf32_frv_relocate_gprello (struct bfd_link_info *info,
2323 bfd *input_bfd,
2324 asection *input_section,
2325 Elf_Internal_Rela *relocation,
2326 bfd_byte *contents,
2327 bfd_vma value)
2328 {
2329 bfd_vma insn;
2330 bfd_vma gp;
2331 struct bfd_link_hash_entry *h;
2332
2333 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
2334
2335 gp = (h->u.def.value
2336 + h->u.def.section->output_section->vma
2337 + h->u.def.section->output_offset);
2338
2339 value -= input_section->output_section->vma;
2340 value -= (gp - input_section->output_section->vma);
2341 value += relocation->r_addend;
2342 value = value & 0xffff;
2343
2344 if ((long) value > 0xffff || (long) value < -0x10000)
2345 return bfd_reloc_overflow;
2346
2347 insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2348 insn = (insn & 0xffff0000) | value;
2349
2350 bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2351
2352 return bfd_reloc_ok;
2353 }
2354
2355 static reloc_howto_type *
2356 frv_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2357 bfd_reloc_code_real_type code)
2358 {
2359 switch (code)
2360 {
2361 default:
2362 break;
2363
2364 case BFD_RELOC_NONE:
2365 return &elf32_frv_howto_table[ (int) R_FRV_NONE];
2366
2367 case BFD_RELOC_32:
2368 if (elf_elfheader (abfd)->e_type == ET_EXEC
2369 || elf_elfheader (abfd)->e_type == ET_DYN)
2370 return &elf32_frv_rel_32_howto;
2371 /* Fall through. */
2372 case BFD_RELOC_CTOR:
2373 return &elf32_frv_howto_table[ (int) R_FRV_32];
2374
2375 case BFD_RELOC_FRV_LABEL16:
2376 return &elf32_frv_howto_table[ (int) R_FRV_LABEL16];
2377
2378 case BFD_RELOC_FRV_LABEL24:
2379 return &elf32_frv_howto_table[ (int) R_FRV_LABEL24];
2380
2381 case BFD_RELOC_FRV_LO16:
2382 return &elf32_frv_howto_table[ (int) R_FRV_LO16];
2383
2384 case BFD_RELOC_FRV_HI16:
2385 return &elf32_frv_howto_table[ (int) R_FRV_HI16];
2386
2387 case BFD_RELOC_FRV_GPREL12:
2388 return &elf32_frv_howto_table[ (int) R_FRV_GPREL12];
2389
2390 case BFD_RELOC_FRV_GPRELU12:
2391 return &elf32_frv_howto_table[ (int) R_FRV_GPRELU12];
2392
2393 case BFD_RELOC_FRV_GPREL32:
2394 return &elf32_frv_howto_table[ (int) R_FRV_GPREL32];
2395
2396 case BFD_RELOC_FRV_GPRELHI:
2397 return &elf32_frv_howto_table[ (int) R_FRV_GPRELHI];
2398
2399 case BFD_RELOC_FRV_GPRELLO:
2400 return &elf32_frv_howto_table[ (int) R_FRV_GPRELLO];
2401
2402 case BFD_RELOC_FRV_GOT12:
2403 return &elf32_frv_howto_table[ (int) R_FRV_GOT12];
2404
2405 case BFD_RELOC_FRV_GOTHI:
2406 return &elf32_frv_howto_table[ (int) R_FRV_GOTHI];
2407
2408 case BFD_RELOC_FRV_GOTLO:
2409 return &elf32_frv_howto_table[ (int) R_FRV_GOTLO];
2410
2411 case BFD_RELOC_FRV_FUNCDESC:
2412 if (elf_elfheader (abfd)->e_type == ET_EXEC
2413 || elf_elfheader (abfd)->e_type == ET_DYN)
2414 return &elf32_frv_rel_funcdesc_howto;
2415 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC];
2416
2417 case BFD_RELOC_FRV_FUNCDESC_GOT12:
2418 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOT12];
2419
2420 case BFD_RELOC_FRV_FUNCDESC_GOTHI:
2421 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTHI];
2422
2423 case BFD_RELOC_FRV_FUNCDESC_GOTLO:
2424 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTLO];
2425
2426 case BFD_RELOC_FRV_FUNCDESC_VALUE:
2427 if (elf_elfheader (abfd)->e_type == ET_EXEC
2428 || elf_elfheader (abfd)->e_type == ET_DYN)
2429 return &elf32_frv_rel_funcdesc_value_howto;
2430 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_VALUE];
2431
2432 case BFD_RELOC_FRV_FUNCDESC_GOTOFF12:
2433 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFF12];
2434
2435 case BFD_RELOC_FRV_FUNCDESC_GOTOFFHI:
2436 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFHI];
2437
2438 case BFD_RELOC_FRV_FUNCDESC_GOTOFFLO:
2439 return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFLO];
2440
2441 case BFD_RELOC_FRV_GOTOFF12:
2442 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFF12];
2443
2444 case BFD_RELOC_FRV_GOTOFFHI:
2445 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFHI];
2446
2447 case BFD_RELOC_FRV_GOTOFFLO:
2448 return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFLO];
2449
2450 case BFD_RELOC_FRV_GETTLSOFF:
2451 return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF];
2452
2453 case BFD_RELOC_FRV_TLSDESC_VALUE:
2454 if (elf_elfheader (abfd)->e_type == ET_EXEC
2455 || elf_elfheader (abfd)->e_type == ET_DYN)
2456 return &elf32_frv_rel_tlsdesc_value_howto;
2457 return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_VALUE];
2458
2459 case BFD_RELOC_FRV_GOTTLSDESC12:
2460 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESC12];
2461
2462 case BFD_RELOC_FRV_GOTTLSDESCHI:
2463 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCHI];
2464
2465 case BFD_RELOC_FRV_GOTTLSDESCLO:
2466 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCLO];
2467
2468 case BFD_RELOC_FRV_TLSMOFF12:
2469 return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF12];
2470
2471 case BFD_RELOC_FRV_TLSMOFFHI:
2472 return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFHI];
2473
2474 case BFD_RELOC_FRV_TLSMOFFLO:
2475 return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFLO];
2476
2477 case BFD_RELOC_FRV_GOTTLSOFF12:
2478 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFF12];
2479
2480 case BFD_RELOC_FRV_GOTTLSOFFHI:
2481 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFHI];
2482
2483 case BFD_RELOC_FRV_GOTTLSOFFLO:
2484 return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFLO];
2485
2486 case BFD_RELOC_FRV_TLSOFF:
2487 if (elf_elfheader (abfd)->e_type == ET_EXEC
2488 || elf_elfheader (abfd)->e_type == ET_DYN)
2489 return &elf32_frv_rel_tlsoff_howto;
2490 return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF];
2491
2492 case BFD_RELOC_FRV_TLSDESC_RELAX:
2493 return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_RELAX];
2494
2495 case BFD_RELOC_FRV_GETTLSOFF_RELAX:
2496 return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF_RELAX];
2497
2498 case BFD_RELOC_FRV_TLSOFF_RELAX:
2499 return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF_RELAX];
2500
2501 case BFD_RELOC_FRV_TLSMOFF:
2502 return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF];
2503
2504 case BFD_RELOC_VTABLE_INHERIT:
2505 return &elf32_frv_vtinherit_howto;
2506
2507 case BFD_RELOC_VTABLE_ENTRY:
2508 return &elf32_frv_vtentry_howto;
2509 }
2510
2511 return NULL;
2512 }
2513
2514 static reloc_howto_type *
2515 frv_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
2516 {
2517 unsigned int i;
2518
2519 for (i = 0;
2520 i < sizeof (elf32_frv_howto_table) / sizeof (elf32_frv_howto_table[0]);
2521 i++)
2522 if (elf32_frv_howto_table[i].name != NULL
2523 && strcasecmp (elf32_frv_howto_table[i].name, r_name) == 0)
2524 return &elf32_frv_howto_table[i];
2525
2526 if (strcasecmp (elf32_frv_vtinherit_howto.name, r_name) == 0)
2527 return &elf32_frv_vtinherit_howto;
2528 if (strcasecmp (elf32_frv_vtentry_howto.name, r_name) == 0)
2529 return &elf32_frv_vtentry_howto;
2530
2531 return NULL;
2532 }
2533
2534 /* Set the howto pointer for an FRV ELF reloc. */
2535
2536 static bfd_boolean
2537 frv_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
2538 arelent *cache_ptr,
2539 Elf_Internal_Rela *dst)
2540 {
2541 unsigned int r_type;
2542
2543 r_type = ELF32_R_TYPE (dst->r_info);
2544 switch (r_type)
2545 {
2546 case R_FRV_GNU_VTINHERIT:
2547 cache_ptr->howto = &elf32_frv_vtinherit_howto;
2548 break;
2549
2550 case R_FRV_GNU_VTENTRY:
2551 cache_ptr->howto = &elf32_frv_vtentry_howto;
2552 break;
2553
2554 default:
2555 if (r_type >= ARRAY_SIZE (elf32_frv_howto_table))
2556 {
2557 /* xgettext:c-format */
2558 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2559 abfd, r_type);
2560 bfd_set_error (bfd_error_bad_value);
2561 return FALSE;
2562 }
2563 cache_ptr->howto = & elf32_frv_howto_table [r_type];
2564 break;
2565 }
2566 return TRUE;
2567 }
2568
2569 /* Set the howto pointer for an FRV ELF REL reloc. */
2570
2571 static bfd_boolean
2572 frvfdpic_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
2573 arelent *cache_ptr, Elf_Internal_Rela *dst)
2574 {
2575 unsigned int r_type;
2576
2577 r_type = ELF32_R_TYPE (dst->r_info);
2578 switch (r_type)
2579 {
2580 case R_FRV_32:
2581 cache_ptr->howto = &elf32_frv_rel_32_howto;
2582 break;
2583
2584 case R_FRV_FUNCDESC:
2585 cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
2586 break;
2587
2588 case R_FRV_FUNCDESC_VALUE:
2589 cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
2590 break;
2591
2592 case R_FRV_TLSDESC_VALUE:
2593 cache_ptr->howto = &elf32_frv_rel_tlsdesc_value_howto;
2594 break;
2595
2596 case R_FRV_TLSOFF:
2597 cache_ptr->howto = &elf32_frv_rel_tlsoff_howto;
2598 break;
2599
2600 default:
2601 cache_ptr->howto = NULL;
2602 return FALSE;
2603 }
2604 return TRUE;
2605 }
2606 \f
2607 /* Perform a single relocation. By default we use the standard BFD
2608 routines, but a few relocs, we have to do them ourselves. */
2609
2610 static bfd_reloc_status_type
2611 frv_final_link_relocate (reloc_howto_type *howto,
2612 bfd *input_bfd,
2613 asection *input_section,
2614 bfd_byte *contents,
2615 Elf_Internal_Rela *rel,
2616 bfd_vma relocation)
2617 {
2618 return _bfd_final_link_relocate (howto, input_bfd, input_section,
2619 contents, rel->r_offset, relocation,
2620 rel->r_addend);
2621 }
2622
2623 \f
2624 /* Relocate an FRV ELF section.
2625
2626 The RELOCATE_SECTION function is called by the new ELF backend linker
2627 to handle the relocations for a section.
2628
2629 The relocs are always passed as Rela structures; if the section
2630 actually uses Rel structures, the r_addend field will always be
2631 zero.
2632
2633 This function is responsible for adjusting the section contents as
2634 necessary, and (if using Rela relocs and generating a relocatable
2635 output file) adjusting the reloc addend as necessary.
2636
2637 This function does not have to worry about setting the reloc
2638 address or the reloc symbol index.
2639
2640 LOCAL_SYMS is a pointer to the swapped in local symbols.
2641
2642 LOCAL_SECTIONS is an array giving the section in the input file
2643 corresponding to the st_shndx field of each local symbol.
2644
2645 The global hash table entry for the global symbols can be found
2646 via elf_sym_hashes (input_bfd).
2647
2648 When generating relocatable output, this function must handle
2649 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2650 going to be the section symbol corresponding to the output
2651 section, which means that the addend must be adjusted
2652 accordingly. */
2653
2654 static bfd_boolean
2655 elf32_frv_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
2656 struct bfd_link_info *info,
2657 bfd *input_bfd,
2658 asection *input_section,
2659 bfd_byte *contents,
2660 Elf_Internal_Rela *relocs,
2661 Elf_Internal_Sym *local_syms,
2662 asection **local_sections)
2663 {
2664 Elf_Internal_Shdr *symtab_hdr;
2665 struct elf_link_hash_entry **sym_hashes;
2666 Elf_Internal_Rela *rel;
2667 Elf_Internal_Rela *relend;
2668 unsigned isec_segment, got_segment, plt_segment, gprel_segment, tls_segment,
2669 check_segment[2];
2670 int silence_segment_error = !bfd_link_pic (info);
2671 unsigned long insn;
2672
2673 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2674 sym_hashes = elf_sym_hashes (input_bfd);
2675 relend = relocs + input_section->reloc_count;
2676
2677 isec_segment = _frvfdpic_osec_to_segment (output_bfd,
2678 input_section->output_section);
2679 if (IS_FDPIC (output_bfd) && frvfdpic_got_section (info))
2680 got_segment = _frvfdpic_osec_to_segment (output_bfd,
2681 frvfdpic_got_section (info)
2682 ->output_section);
2683 else
2684 got_segment = -1;
2685 if (IS_FDPIC (output_bfd) && frvfdpic_gotfixup_section (info))
2686 gprel_segment = _frvfdpic_osec_to_segment (output_bfd,
2687 frvfdpic_gotfixup_section (info)
2688 ->output_section);
2689 else
2690 gprel_segment = -1;
2691 if (IS_FDPIC (output_bfd) && frvfdpic_plt_section (info))
2692 plt_segment = _frvfdpic_osec_to_segment (output_bfd,
2693 frvfdpic_plt_section (info)
2694 ->output_section);
2695 else
2696 plt_segment = -1;
2697 if (elf_hash_table (info)->tls_sec)
2698 tls_segment = _frvfdpic_osec_to_segment (output_bfd,
2699 elf_hash_table (info)->tls_sec);
2700 else
2701 tls_segment = -1;
2702
2703 for (rel = relocs; rel < relend; rel ++)
2704 {
2705 reloc_howto_type *howto;
2706 unsigned long r_symndx;
2707 Elf_Internal_Sym *sym;
2708 asection *sec;
2709 struct elf_link_hash_entry *h;
2710 bfd_vma relocation;
2711 bfd_reloc_status_type r;
2712 const char *name;
2713 int r_type;
2714 asection *osec;
2715 struct frvfdpic_relocs_info *picrel = NULL;
2716 bfd_vma orig_addend = rel->r_addend;
2717
2718 r_type = ELF32_R_TYPE (rel->r_info);
2719
2720 if ( r_type == R_FRV_GNU_VTINHERIT
2721 || r_type == R_FRV_GNU_VTENTRY)
2722 continue;
2723
2724 r_symndx = ELF32_R_SYM (rel->r_info);
2725 howto = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
2726 h = NULL;
2727 sym = NULL;
2728 sec = NULL;
2729
2730 if (r_symndx < symtab_hdr->sh_info)
2731 {
2732 sym = local_syms + r_symndx;
2733 osec = sec = local_sections [r_symndx];
2734 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2735
2736 name = bfd_elf_string_from_elf_section
2737 (input_bfd, symtab_hdr->sh_link, sym->st_name);
2738 if (name == NULL || name[0] == 0)
2739 name = bfd_section_name (sec);
2740 }
2741 else
2742 {
2743 bfd_boolean warned, ignored;
2744 bfd_boolean unresolved_reloc;
2745
2746 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2747 r_symndx, symtab_hdr, sym_hashes,
2748 h, sec, relocation,
2749 unresolved_reloc, warned, ignored);
2750 osec = sec;
2751 name = h->root.root.string;
2752 }
2753
2754 if (sec != NULL && discarded_section (sec))
2755 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2756 rel, 1, relend, howto, 0, contents);
2757
2758 if (bfd_link_relocatable (info))
2759 continue;
2760
2761 if (r_type != R_FRV_TLSMOFF
2762 && h != NULL
2763 && (h->root.type == bfd_link_hash_defined
2764 || h->root.type == bfd_link_hash_defweak)
2765 && !FRVFDPIC_SYM_LOCAL (info, h))
2766 {
2767 osec = sec = NULL;
2768 relocation = 0;
2769 }
2770
2771 switch (r_type)
2772 {
2773 case R_FRV_LABEL24:
2774 case R_FRV_32:
2775 if (! IS_FDPIC (output_bfd))
2776 goto non_fdpic;
2777 /* Fall through. */
2778
2779 case R_FRV_GOT12:
2780 case R_FRV_GOTHI:
2781 case R_FRV_GOTLO:
2782 case R_FRV_FUNCDESC_GOT12:
2783 case R_FRV_FUNCDESC_GOTHI:
2784 case R_FRV_FUNCDESC_GOTLO:
2785 case R_FRV_GOTOFF12:
2786 case R_FRV_GOTOFFHI:
2787 case R_FRV_GOTOFFLO:
2788 case R_FRV_FUNCDESC_GOTOFF12:
2789 case R_FRV_FUNCDESC_GOTOFFHI:
2790 case R_FRV_FUNCDESC_GOTOFFLO:
2791 case R_FRV_FUNCDESC:
2792 case R_FRV_FUNCDESC_VALUE:
2793 case R_FRV_GETTLSOFF:
2794 case R_FRV_TLSDESC_VALUE:
2795 case R_FRV_GOTTLSDESC12:
2796 case R_FRV_GOTTLSDESCHI:
2797 case R_FRV_GOTTLSDESCLO:
2798 case R_FRV_TLSMOFF12:
2799 case R_FRV_TLSMOFFHI:
2800 case R_FRV_TLSMOFFLO:
2801 case R_FRV_GOTTLSOFF12:
2802 case R_FRV_GOTTLSOFFHI:
2803 case R_FRV_GOTTLSOFFLO:
2804 case R_FRV_TLSOFF:
2805 case R_FRV_TLSDESC_RELAX:
2806 case R_FRV_GETTLSOFF_RELAX:
2807 case R_FRV_TLSOFF_RELAX:
2808 case R_FRV_TLSMOFF:
2809 if ((input_section->flags & SEC_ALLOC) == 0)
2810 break;
2811
2812 if (h != NULL)
2813 picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info
2814 (info), input_bfd, h,
2815 orig_addend, INSERT);
2816 else
2817 /* In order to find the entry we created before, we must
2818 use the original addend, not the one that may have been
2819 modified by _bfd_elf_rela_local_sym(). */
2820 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
2821 (info), input_bfd, r_symndx,
2822 orig_addend, INSERT);
2823 if (! picrel)
2824 return FALSE;
2825
2826 if (!_frvfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2827 osec, sym,
2828 rel->r_addend))
2829 {
2830 info->callbacks->einfo
2831 /* xgettext:c-format */
2832 (_("%H: relocation to `%s+%v'"
2833 " may have caused the error above\n"),
2834 input_bfd, input_section, rel->r_offset, name, rel->r_addend);
2835 return FALSE;
2836 }
2837
2838 break;
2839
2840 default:
2841 non_fdpic:
2842 picrel = NULL;
2843 if (h
2844 && ! FRVFDPIC_SYM_LOCAL (info, h)
2845 && _bfd_elf_section_offset (output_bfd, info, input_section,
2846 rel->r_offset) != (bfd_vma) -1)
2847 {
2848 info->callbacks->einfo
2849 (_("%H: relocation references symbol"
2850 " not defined in the module\n"),
2851 input_bfd, input_section, rel->r_offset);
2852 return FALSE;
2853 }
2854 break;
2855 }
2856
2857 switch (r_type)
2858 {
2859 case R_FRV_GETTLSOFF:
2860 case R_FRV_TLSDESC_VALUE:
2861 case R_FRV_GOTTLSDESC12:
2862 case R_FRV_GOTTLSDESCHI:
2863 case R_FRV_GOTTLSDESCLO:
2864 case R_FRV_TLSMOFF12:
2865 case R_FRV_TLSMOFFHI:
2866 case R_FRV_TLSMOFFLO:
2867 case R_FRV_GOTTLSOFF12:
2868 case R_FRV_GOTTLSOFFHI:
2869 case R_FRV_GOTTLSOFFLO:
2870 case R_FRV_TLSOFF:
2871 case R_FRV_TLSDESC_RELAX:
2872 case R_FRV_GETTLSOFF_RELAX:
2873 case R_FRV_TLSOFF_RELAX:
2874 case R_FRV_TLSMOFF:
2875 if (sec && (bfd_is_abs_section (sec) || bfd_is_und_section (sec)))
2876 relocation += tls_biased_base (info);
2877 break;
2878
2879 default:
2880 break;
2881 }
2882
2883 /* Try to apply TLS relaxations. */
2884 if (1)
2885 switch (r_type)
2886 {
2887
2888 #define LOCAL_EXEC_P(info, picrel) \
2889 (bfd_link_executable (info) \
2890 && (picrel->symndx != -1 || FRVFDPIC_SYM_LOCAL ((info), (picrel)->d.h)))
2891 #define INITIAL_EXEC_P(info, picrel) \
2892 ((bfd_link_executable (info)|| (info)->flags & DF_STATIC_TLS) \
2893 && (picrel)->tlsoff_entry)
2894
2895 #define IN_RANGE_FOR_OFST12_P(value) \
2896 ((bfd_vma)((value) + 2048) < (bfd_vma)4096)
2897 #define IN_RANGE_FOR_SETLOS_P(value) \
2898 ((bfd_vma)((value) + 32768) < (bfd_vma)65536)
2899 #define TLSMOFF_IN_RANGE_FOR_SETLOS_P(value, info) \
2900 (IN_RANGE_FOR_SETLOS_P ((value) - tls_biased_base (info)))
2901
2902 #define RELAX_GETTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2903 (LOCAL_EXEC_P ((info), (picrel)) \
2904 && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2905 #define RELAX_GETTLSOFF_INITIAL_EXEC_P(info, picrel) \
2906 (INITIAL_EXEC_P ((info), (picrel)) \
2907 && IN_RANGE_FOR_OFST12_P ((picrel)->tlsoff_entry))
2908
2909 #define RELAX_TLSDESC_LOCAL_EXEC_P(info, picrel, value) \
2910 (LOCAL_EXEC_P ((info), (picrel)))
2911 #define RELAX_TLSDESC_INITIAL_EXEC_P(info, picrel) \
2912 (INITIAL_EXEC_P ((info), (picrel)))
2913
2914 #define RELAX_GOTTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2915 (LOCAL_EXEC_P ((info), (picrel)) \
2916 && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2917
2918 case R_FRV_GETTLSOFF:
2919 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2920
2921 /* Is this a call instruction? */
2922 if ((insn & (unsigned long)0x01fc0000) != 0x003c0000)
2923 {
2924 info->callbacks->einfo
2925 (_("%H: R_FRV_GETTLSOFF not applied to a call instruction\n"),
2926 input_bfd, input_section, rel->r_offset);
2927 return FALSE;
2928 }
2929
2930 if (RELAX_GETTLSOFF_LOCAL_EXEC_P (info, picrel,
2931 relocation + rel->r_addend))
2932 {
2933 /* Replace the call instruction (except the packing bit)
2934 with setlos #tlsmofflo(symbol+offset), gr9. */
2935 insn &= (unsigned long)0x80000000;
2936 insn |= (unsigned long)0x12fc0000;
2937 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2938
2939 r_type = R_FRV_TLSMOFFLO;
2940 howto = elf32_frv_howto_table + r_type;
2941 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2942 }
2943
2944 else if (RELAX_GETTLSOFF_INITIAL_EXEC_P (info, picrel))
2945 {
2946 /* Replace the call instruction (except the packing bit)
2947 with ldi @(gr15, #gottlsoff12(symbol+addend)), gr9. */
2948 insn &= (unsigned long)0x80000000;
2949 insn |= (unsigned long)0x12c8f000;
2950 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2951
2952 r_type = R_FRV_GOTTLSOFF12;
2953 howto = elf32_frv_howto_table + r_type;
2954 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2955 }
2956
2957 break;
2958
2959 case R_FRV_GOTTLSDESC12:
2960 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2961
2962 /* Is this an lddi instruction? */
2963 if ((insn & (unsigned long)0x01fc0000) != 0x00cc0000)
2964 {
2965 info->callbacks->einfo
2966 (_("%H: R_FRV_GOTTLSDESC12"
2967 " not applied to an lddi instruction\n"),
2968 input_bfd, input_section, rel->r_offset);
2969 return FALSE;
2970 }
2971
2972 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
2973 relocation + rel->r_addend)
2974 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
2975 info))
2976 {
2977 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
2978 with setlos #tlsmofflo(symbol+offset), gr<C+1>.
2979 Preserve the packing bit. */
2980 insn = (insn & (unsigned long)0x80000000)
2981 | ((insn + (unsigned long)0x02000000)
2982 & (unsigned long)0x7e000000);
2983 insn |= (unsigned long)0x00fc0000;
2984 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2985
2986 r_type = R_FRV_TLSMOFFLO;
2987 howto = elf32_frv_howto_table + r_type;
2988 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2989 }
2990
2991 else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
2992 relocation + rel->r_addend))
2993 {
2994 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
2995 with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
2996 Preserve the packing bit. */
2997 insn = (insn & (unsigned long)0x80000000)
2998 | ((insn + (unsigned long)0x02000000)
2999 & (unsigned long)0x7e000000);
3000 insn |= (unsigned long)0x00f80000;
3001 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3002
3003 r_type = R_FRV_TLSMOFFHI;
3004 howto = elf32_frv_howto_table + r_type;
3005 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3006 }
3007
3008 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3009 {
3010 /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3011 with ldi @(grB, #gottlsoff12(symbol+offset),
3012 gr<C+1>. Preserve the packing bit. If gottlsoff12
3013 overflows, we'll error out, but that's sort-of ok,
3014 since we'd started with gottlsdesc12, that's actually
3015 more demanding. Compiling with -fPIE instead of
3016 -fpie would fix it; linking with --relax should fix
3017 it as well. */
3018 insn = (insn & (unsigned long)0x80cbf000)
3019 | ((insn + (unsigned long)0x02000000)
3020 & (unsigned long)0x7e000000);
3021 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3022
3023 r_type = R_FRV_GOTTLSOFF12;
3024 howto = elf32_frv_howto_table + r_type;
3025 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3026 }
3027
3028 break;
3029
3030 case R_FRV_GOTTLSDESCHI:
3031 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3032
3033 /* Is this a sethi instruction? */
3034 if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3035 {
3036 info->callbacks->einfo
3037 (_("%H: R_FRV_GOTTLSDESCHI"
3038 " not applied to a sethi instruction\n"),
3039 input_bfd, input_section, rel->r_offset);
3040 return FALSE;
3041 }
3042
3043 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3044 relocation + rel->r_addend)
3045 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3046 && IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry)))
3047 {
3048 /* Replace sethi with a nop. Preserve the packing bit. */
3049 insn &= (unsigned long)0x80000000;
3050 insn |= (unsigned long)0x00880000;
3051 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3052
3053 /* Nothing to relocate. */
3054 continue;
3055 }
3056
3057 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3058 {
3059 /* Simply decay GOTTLSDESC to GOTTLSOFF. */
3060 r_type = R_FRV_GOTTLSOFFHI;
3061 howto = elf32_frv_howto_table + r_type;
3062 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3063 }
3064
3065 break;
3066
3067 case R_FRV_GOTTLSDESCLO:
3068 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3069
3070 /* Is this a setlo or setlos instruction? */
3071 if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3072 {
3073 info->callbacks->einfo
3074 (_("%H: R_FRV_GOTTLSDESCLO"
3075 " not applied to a setlo or setlos instruction\n"),
3076 input_bfd, input_section, rel->r_offset);
3077 return FALSE;
3078 }
3079
3080 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3081 relocation + rel->r_addend)
3082 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3083 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3084 {
3085 /* Replace setlo/setlos with a nop. Preserve the
3086 packing bit. */
3087 insn &= (unsigned long)0x80000000;
3088 insn |= (unsigned long)0x00880000;
3089 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3090
3091 /* Nothing to relocate. */
3092 continue;
3093 }
3094
3095 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3096 {
3097 /* If the corresponding sethi (if it exists) decayed
3098 to a nop, make sure this becomes (or already is) a
3099 setlos, not setlo. */
3100 if (IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry))
3101 {
3102 insn |= (unsigned long)0x00080000;
3103 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3104 }
3105
3106 /* Simply decay GOTTLSDESC to GOTTLSOFF. */
3107 r_type = R_FRV_GOTTLSOFFLO;
3108 howto = elf32_frv_howto_table + r_type;
3109 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3110 }
3111
3112 break;
3113
3114 case R_FRV_TLSDESC_RELAX:
3115 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3116
3117 /* Is this an ldd instruction? */
3118 if ((insn & (unsigned long)0x01fc0fc0) != 0x00080140)
3119 {
3120 info->callbacks->einfo
3121 (_("%H: R_FRV_TLSDESC_RELAX"
3122 " not applied to an ldd instruction\n"),
3123 input_bfd, input_section, rel->r_offset);
3124 return FALSE;
3125 }
3126
3127 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3128 relocation + rel->r_addend)
3129 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3130 info))
3131 {
3132 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3133 with setlos #tlsmofflo(symbol+offset), gr<C+1>.
3134 Preserve the packing bit. */
3135 insn = (insn & (unsigned long)0x80000000)
3136 | ((insn + (unsigned long)0x02000000)
3137 & (unsigned long)0x7e000000);
3138 insn |= (unsigned long)0x00fc0000;
3139 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3140
3141 r_type = R_FRV_TLSMOFFLO;
3142 howto = elf32_frv_howto_table + r_type;
3143 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3144 }
3145
3146 else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3147 relocation + rel->r_addend))
3148 {
3149 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3150 with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
3151 Preserve the packing bit. */
3152 insn = (insn & (unsigned long)0x80000000)
3153 | ((insn + (unsigned long)0x02000000)
3154 & (unsigned long)0x7e000000);
3155 insn |= (unsigned long)0x00f80000;
3156 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3157
3158 r_type = R_FRV_TLSMOFFHI;
3159 howto = elf32_frv_howto_table + r_type;
3160 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3161 }
3162
3163 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3164 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3165 {
3166 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3167 with ldi @(grB, #gottlsoff12(symbol+offset), gr<C+1>.
3168 Preserve the packing bit. */
3169 insn = (insn & (unsigned long)0x8003f000)
3170 | (unsigned long)0x00c80000
3171 | ((insn + (unsigned long)0x02000000)
3172 & (unsigned long)0x7e000000);
3173 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3174
3175 r_type = R_FRV_GOTTLSOFF12;
3176 howto = elf32_frv_howto_table + r_type;
3177 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3178 }
3179
3180 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3181 {
3182 /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3183 with ld #tlsoff(symbol+offset)@(grB, grA), gr<C+1>.
3184 Preserve the packing bit. */
3185 insn = (insn & (unsigned long)0x81ffffbf)
3186 | ((insn + (unsigned long)0x02000000)
3187 & (unsigned long)0x7e000000);
3188 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3189
3190 /* #tlsoff(symbol+offset) is just a relaxation
3191 annotation, so there's nothing left to
3192 relocate. */
3193 continue;
3194 }
3195
3196 break;
3197
3198 case R_FRV_GETTLSOFF_RELAX:
3199 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3200
3201 /* Is this a calll or callil instruction? */
3202 if ((insn & (unsigned long)0x7ff80fc0) != 0x02300000)
3203 {
3204 info->callbacks->einfo
3205 (_("%H: R_FRV_GETTLSOFF_RELAX"
3206 " not applied to a calll instruction\n"),
3207 input_bfd, input_section, rel->r_offset);
3208 return FALSE;
3209 }
3210
3211 if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3212 relocation + rel->r_addend)
3213 && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3214 info))
3215 {
3216 /* Replace calll with a nop. Preserve the packing bit. */
3217 insn &= (unsigned long)0x80000000;
3218 insn |= (unsigned long)0x00880000;
3219 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3220
3221 /* Nothing to relocate. */
3222 continue;
3223 }
3224
3225 else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3226 relocation + rel->r_addend))
3227 {
3228 /* Replace calll with setlo #tlsmofflo(symbol+offset), gr9.
3229 Preserve the packing bit. */
3230 insn &= (unsigned long)0x80000000;
3231 insn |= (unsigned long)0x12f40000;
3232 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3233
3234 r_type = R_FRV_TLSMOFFLO;
3235 howto = elf32_frv_howto_table + r_type;
3236 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3237 }
3238
3239 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3240 {
3241 /* Replace calll with a nop. Preserve the packing bit. */
3242 insn &= (unsigned long)0x80000000;
3243 insn |= (unsigned long)0x00880000;
3244 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3245
3246 /* Nothing to relocate. */
3247 continue;
3248 }
3249
3250 break;
3251
3252 case R_FRV_GOTTLSOFF12:
3253 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3254
3255 /* Is this an ldi instruction? */
3256 if ((insn & (unsigned long)0x01fc0000) != 0x00c80000)
3257 {
3258 info->callbacks->einfo
3259 (_("%H: R_FRV_GOTTLSOFF12"
3260 " not applied to an ldi instruction\n"),
3261 input_bfd, input_section, rel->r_offset);
3262 return FALSE;
3263 }
3264
3265 if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3266 relocation + rel->r_addend))
3267 {
3268 /* Replace ldi @(grB, #gottlsoff12(symbol+offset), grC
3269 with setlos #tlsmofflo(symbol+offset), grC.
3270 Preserve the packing bit. */
3271 insn &= (unsigned long)0xfe000000;
3272 insn |= (unsigned long)0x00fc0000;
3273 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3274
3275 r_type = R_FRV_TLSMOFFLO;
3276 howto = elf32_frv_howto_table + r_type;
3277 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3278 }
3279
3280 break;
3281
3282 case R_FRV_GOTTLSOFFHI:
3283 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3284
3285 /* Is this a sethi instruction? */
3286 if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3287 {
3288 info->callbacks->einfo
3289 (_("%H: R_FRV_GOTTLSOFFHI"
3290 " not applied to a sethi instruction\n"),
3291 input_bfd, input_section, rel->r_offset);
3292 return FALSE;
3293 }
3294
3295 if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3296 relocation + rel->r_addend)
3297 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3298 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3299 {
3300 /* Replace sethi with a nop. Preserve the packing bit. */
3301 insn &= (unsigned long)0x80000000;
3302 insn |= (unsigned long)0x00880000;
3303 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3304
3305 /* Nothing to relocate. */
3306 continue;
3307 }
3308
3309 break;
3310
3311 case R_FRV_GOTTLSOFFLO:
3312 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3313
3314 /* Is this a setlo or setlos instruction? */
3315 if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3316 {
3317 info->callbacks->einfo
3318 (_("%H: R_FRV_GOTTLSOFFLO"
3319 " not applied to a setlo or setlos instruction\n"),
3320 input_bfd, input_section, rel->r_offset);
3321 return FALSE;
3322 }
3323
3324 if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3325 relocation + rel->r_addend)
3326 || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3327 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3328 {
3329 /* Replace setlo/setlos with a nop. Preserve the
3330 packing bit. */
3331 insn &= (unsigned long)0x80000000;
3332 insn |= (unsigned long)0x00880000;
3333 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3334
3335 /* Nothing to relocate. */
3336 continue;
3337 }
3338
3339 break;
3340
3341 case R_FRV_TLSOFF_RELAX:
3342 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3343
3344 /* Is this an ld instruction? */
3345 if ((insn & (unsigned long)0x01fc0fc0) != 0x00080100)
3346 {
3347 info->callbacks->einfo
3348 (_("%H: R_FRV_TLSOFF_RELAX"
3349 " not applied to an ld instruction\n"),
3350 input_bfd, input_section, rel->r_offset);
3351 return FALSE;
3352 }
3353
3354 if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3355 relocation + rel->r_addend))
3356 {
3357 /* Replace ld #gottlsoff(symbol+offset)@(grB, grA), grC
3358 with setlos #tlsmofflo(symbol+offset), grC.
3359 Preserve the packing bit. */
3360 insn &= (unsigned long)0xfe000000;
3361 insn |= (unsigned long)0x00fc0000;
3362 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3363
3364 r_type = R_FRV_TLSMOFFLO;
3365 howto = elf32_frv_howto_table + r_type;
3366 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3367 }
3368
3369 else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3370 && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3371 {
3372 /* Replace ld #tlsoff(symbol+offset)@(grB, grA), grC
3373 with ldi @(grB, #gottlsoff12(symbol+offset), grC.
3374 Preserve the packing bit. */
3375 insn = (insn & (unsigned long)0xfe03f000)
3376 | (unsigned long)0x00c80000;
3377 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3378
3379 r_type = R_FRV_GOTTLSOFF12;
3380 howto = elf32_frv_howto_table + r_type;
3381 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3382 }
3383
3384 break;
3385
3386 case R_FRV_TLSMOFFHI:
3387 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3388
3389 /* Is this a sethi instruction? */
3390 if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3391 {
3392 info->callbacks->einfo
3393 (_("%H: R_FRV_TLSMOFFHI"
3394 " not applied to a sethi instruction\n"),
3395 input_bfd, input_section, rel->r_offset);
3396 return FALSE;
3397 }
3398
3399 if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3400 info))
3401 {
3402 /* Replace sethi with a nop. Preserve the packing bit. */
3403 insn &= (unsigned long)0x80000000;
3404 insn |= (unsigned long)0x00880000;
3405 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3406
3407 /* Nothing to relocate. */
3408 continue;
3409 }
3410
3411 break;
3412
3413 case R_FRV_TLSMOFFLO:
3414 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3415
3416 /* Is this a setlo or setlos instruction? */
3417 if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3418 {
3419 info->callbacks->einfo
3420 (_("R_FRV_TLSMOFFLO"
3421 " not applied to a setlo or setlos instruction\n"),
3422 input_bfd, input_section, rel->r_offset);
3423 return FALSE;
3424 }
3425
3426 if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3427 info))
3428 /* If the corresponding sethi (if it exists) decayed
3429 to a nop, make sure this becomes (or already is) a
3430 setlos, not setlo. */
3431 {
3432 insn |= (unsigned long)0x00080000;
3433 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3434 }
3435
3436 break;
3437
3438 /*
3439 There's nothing to relax in these:
3440 R_FRV_TLSDESC_VALUE
3441 R_FRV_TLSOFF
3442 R_FRV_TLSMOFF12
3443 R_FRV_TLSMOFFHI
3444 R_FRV_TLSMOFFLO
3445 R_FRV_TLSMOFF
3446 */
3447
3448 default:
3449 break;
3450 }
3451
3452 switch (r_type)
3453 {
3454 case R_FRV_LABEL24:
3455 check_segment[0] = isec_segment;
3456 if (! IS_FDPIC (output_bfd))
3457 check_segment[1] = isec_segment;
3458 else if (picrel->plt)
3459 {
3460 relocation = frvfdpic_plt_section (info)->output_section->vma
3461 + frvfdpic_plt_section (info)->output_offset
3462 + picrel->plt_entry;
3463 check_segment[1] = plt_segment;
3464 }
3465 /* We don't want to warn on calls to undefined weak symbols,
3466 as calls to them must be protected by non-NULL tests
3467 anyway, and unprotected calls would invoke undefined
3468 behavior. */
3469 else if (picrel->symndx == -1
3470 && picrel->d.h->root.type == bfd_link_hash_undefweak)
3471 check_segment[1] = check_segment[0];
3472 else
3473 check_segment[1] = sec
3474 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3475 : (unsigned)-1;
3476 break;
3477
3478 case R_FRV_GOT12:
3479 case R_FRV_GOTHI:
3480 case R_FRV_GOTLO:
3481 relocation = picrel->got_entry;
3482 check_segment[0] = check_segment[1] = got_segment;
3483 break;
3484
3485 case R_FRV_FUNCDESC_GOT12:
3486 case R_FRV_FUNCDESC_GOTHI:
3487 case R_FRV_FUNCDESC_GOTLO:
3488 relocation = picrel->fdgot_entry;
3489 check_segment[0] = check_segment[1] = got_segment;
3490 break;
3491
3492 case R_FRV_GOTOFFHI:
3493 case R_FRV_GOTOFF12:
3494 case R_FRV_GOTOFFLO:
3495 relocation -= frvfdpic_got_section (info)->output_section->vma
3496 + frvfdpic_got_section (info)->output_offset
3497 + frvfdpic_got_initial_offset (info);
3498 check_segment[0] = got_segment;
3499 check_segment[1] = sec
3500 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3501 : (unsigned)-1;
3502 break;
3503
3504 case R_FRV_FUNCDESC_GOTOFF12:
3505 case R_FRV_FUNCDESC_GOTOFFHI:
3506 case R_FRV_FUNCDESC_GOTOFFLO:
3507 relocation = picrel->fd_entry;
3508 check_segment[0] = check_segment[1] = got_segment;
3509 break;
3510
3511 case R_FRV_FUNCDESC:
3512 {
3513 int dynindx;
3514 bfd_vma addend = rel->r_addend;
3515
3516 if (! (h && h->root.type == bfd_link_hash_undefweak
3517 && FRVFDPIC_SYM_LOCAL (info, h)))
3518 {
3519 /* If the symbol is dynamic and there may be dynamic
3520 symbol resolution because we are or are linked with a
3521 shared library, emit a FUNCDESC relocation such that
3522 the dynamic linker will allocate the function
3523 descriptor. If the symbol needs a non-local function
3524 descriptor but binds locally (e.g., its visibility is
3525 protected, emit a dynamic relocation decayed to
3526 section+offset. */
3527 if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h)
3528 && FRVFDPIC_SYM_LOCAL (info, h)
3529 && !bfd_link_pde (info))
3530 {
3531 dynindx = elf_section_data (h->root.u.def.section
3532 ->output_section)->dynindx;
3533 addend += h->root.u.def.section->output_offset
3534 + h->root.u.def.value;
3535 }
3536 else if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h))
3537 {
3538 if (addend)
3539 {
3540 info->callbacks->einfo
3541 (_("%H: %s references dynamic symbol"
3542 " with nonzero addend\n"),
3543 input_bfd, input_section, rel->r_offset,
3544 "R_FRV_FUNCDESC");
3545 return FALSE;
3546 }
3547 dynindx = h->dynindx;
3548 }
3549 else
3550 {
3551 /* Otherwise, we know we have a private function
3552 descriptor, so reference it directly. */
3553 BFD_ASSERT (picrel->privfd);
3554 r_type = R_FRV_32;
3555 dynindx = elf_section_data (frvfdpic_got_section (info)
3556 ->output_section)->dynindx;
3557 addend = frvfdpic_got_section (info)->output_offset
3558 + frvfdpic_got_initial_offset (info)
3559 + picrel->fd_entry;
3560 }
3561
3562 /* If there is room for dynamic symbol resolution, emit
3563 the dynamic relocation. However, if we're linking an
3564 executable at a fixed location, we won't have emitted a
3565 dynamic symbol entry for the got section, so idx will
3566 be zero, which means we can and should compute the
3567 address of the private descriptor ourselves. */
3568 if (bfd_link_pde (info)
3569 && (!h || FRVFDPIC_FUNCDESC_LOCAL (info, h)))
3570 {
3571 addend += frvfdpic_got_section (info)->output_section->vma;
3572 if ((bfd_section_flags (input_section->output_section)
3573 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3574 {
3575 bfd_vma offset;
3576
3577 if (_frvfdpic_osec_readonly_p (output_bfd,
3578 input_section
3579 ->output_section))
3580 {
3581 info->callbacks->einfo
3582 (_("%H: cannot emit fixups"
3583 " in read-only section\n"),
3584 input_bfd, input_section, rel->r_offset);
3585 return FALSE;
3586 }
3587
3588 offset = _bfd_elf_section_offset
3589 (output_bfd, info,
3590 input_section, rel->r_offset);
3591
3592 if (offset != (bfd_vma)-1)
3593 _frvfdpic_add_rofixup (output_bfd,
3594 frvfdpic_gotfixup_section
3595 (info),
3596 offset + input_section
3597 ->output_section->vma
3598 + input_section->output_offset,
3599 picrel);
3600 }
3601 }
3602 else if ((bfd_section_flags (input_section->output_section)
3603 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3604 {
3605 bfd_vma offset;
3606
3607 if (_frvfdpic_osec_readonly_p (output_bfd,
3608 input_section
3609 ->output_section))
3610 {
3611 info->callbacks->einfo
3612 (_("%H: cannot emit dynamic relocations"
3613 " in read-only section\n"),
3614 input_bfd, input_section, rel->r_offset);
3615 return FALSE;
3616 }
3617
3618 offset = _bfd_elf_section_offset
3619 (output_bfd, info,
3620 input_section, rel->r_offset);
3621
3622 if (offset != (bfd_vma)-1)
3623 _frvfdpic_add_dyn_reloc (output_bfd,
3624 frvfdpic_gotrel_section (info),
3625 offset + input_section
3626 ->output_section->vma
3627 + input_section->output_offset,
3628 r_type, dynindx, addend, picrel);
3629 }
3630 else
3631 addend += frvfdpic_got_section (info)->output_section->vma;
3632 }
3633
3634 /* We want the addend in-place because dynamic
3635 relocations are REL. Setting relocation to it should
3636 arrange for it to be installed. */
3637 relocation = addend - rel->r_addend;
3638 }
3639 check_segment[0] = check_segment[1] = got_segment;
3640 break;
3641
3642 case R_FRV_32:
3643 if (! IS_FDPIC (output_bfd))
3644 {
3645 check_segment[0] = check_segment[1] = -1;
3646 break;
3647 }
3648 /* Fall through. */
3649 case R_FRV_FUNCDESC_VALUE:
3650 {
3651 int dynindx;
3652 bfd_vma addend = rel->r_addend;
3653
3654 /* If the symbol is dynamic but binds locally, use
3655 section+offset. */
3656 if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
3657 {
3658 if (addend && r_type == R_FRV_FUNCDESC_VALUE)
3659 {
3660 info->callbacks->einfo
3661 (_("%H: %s references dynamic symbol"
3662 " with nonzero addend\n"),
3663 input_bfd, input_section, rel->r_offset,
3664 "R_FRV_FUNCDESC_VALUE");
3665 return FALSE;
3666 }
3667 dynindx = h->dynindx;
3668 }
3669 else
3670 {
3671 if (h)
3672 addend += h->root.u.def.value;
3673 else
3674 addend += sym->st_value;
3675 if (osec)
3676 addend += osec->output_offset;
3677 if (osec && osec->output_section
3678 && ! bfd_is_abs_section (osec->output_section)
3679 && ! bfd_is_und_section (osec->output_section))
3680 dynindx = elf_section_data (osec->output_section)->dynindx;
3681 else
3682 dynindx = 0;
3683 }
3684
3685 /* If we're linking an executable at a fixed address, we
3686 can omit the dynamic relocation as long as the symbol
3687 is defined in the current link unit (which is implied
3688 by its output section not being NULL). */
3689 if (bfd_link_pde (info)
3690 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3691 {
3692 if (osec)
3693 addend += osec->output_section->vma;
3694 if (IS_FDPIC (input_bfd)
3695 && (bfd_section_flags (input_section->output_section)
3696 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3697 {
3698 if (_frvfdpic_osec_readonly_p (output_bfd,
3699 input_section
3700 ->output_section))
3701 {
3702 info->callbacks->einfo
3703 (_("%H: cannot emit fixups in read-only section\n"),
3704 input_bfd, input_section, rel->r_offset);
3705 return FALSE;
3706 }
3707 if (!h || h->root.type != bfd_link_hash_undefweak)
3708 {
3709 bfd_vma offset = _bfd_elf_section_offset
3710 (output_bfd, info,
3711 input_section, rel->r_offset);
3712
3713 if (offset != (bfd_vma)-1)
3714 {
3715 _frvfdpic_add_rofixup (output_bfd,
3716 frvfdpic_gotfixup_section
3717 (info),
3718 offset + input_section
3719 ->output_section->vma
3720 + input_section->output_offset,
3721 picrel);
3722 if (r_type == R_FRV_FUNCDESC_VALUE)
3723 _frvfdpic_add_rofixup
3724 (output_bfd,
3725 frvfdpic_gotfixup_section (info),
3726 offset
3727 + input_section->output_section->vma
3728 + input_section->output_offset + 4, picrel);
3729 }
3730 }
3731 }
3732 }
3733 else
3734 {
3735 if ((bfd_section_flags (input_section->output_section)
3736 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3737 {
3738 bfd_vma offset;
3739
3740 if (_frvfdpic_osec_readonly_p (output_bfd,
3741 input_section
3742 ->output_section))
3743 {
3744 info->callbacks->einfo
3745 (_("%H: cannot emit dynamic relocations"
3746 " in read-only section\n"),
3747 input_bfd, input_section, rel->r_offset);
3748 return FALSE;
3749 }
3750
3751 offset = _bfd_elf_section_offset
3752 (output_bfd, info,
3753 input_section, rel->r_offset);
3754
3755 if (offset != (bfd_vma)-1)
3756 _frvfdpic_add_dyn_reloc (output_bfd,
3757 frvfdpic_gotrel_section (info),
3758 offset + input_section
3759 ->output_section->vma
3760 + input_section->output_offset,
3761 r_type, dynindx, addend, picrel);
3762 }
3763 else if (osec)
3764 addend += osec->output_section->vma;
3765 /* We want the addend in-place because dynamic
3766 relocations are REL. Setting relocation to it
3767 should arrange for it to be installed. */
3768 relocation = addend - rel->r_addend;
3769 }
3770
3771 if (r_type == R_FRV_FUNCDESC_VALUE)
3772 {
3773 /* If we've omitted the dynamic relocation, just emit
3774 the fixed addresses of the symbol and of the local
3775 GOT base offset. */
3776 if (bfd_link_pde (info)
3777 && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3778 bfd_put_32 (output_bfd,
3779 frvfdpic_got_section (info)->output_section->vma
3780 + frvfdpic_got_section (info)->output_offset
3781 + frvfdpic_got_initial_offset (info),
3782 contents + rel->r_offset + 4);
3783 else
3784 /* A function descriptor used for lazy or local
3785 resolving is initialized such that its high word
3786 contains the output section index in which the
3787 PLT entries are located, and the low word
3788 contains the offset of the lazy PLT entry entry
3789 point into that section. */
3790 bfd_put_32 (output_bfd,
3791 h && ! FRVFDPIC_SYM_LOCAL (info, h)
3792 ? 0
3793 : _frvfdpic_osec_to_segment (output_bfd,
3794 sec
3795 ->output_section),
3796 contents + rel->r_offset + 4);
3797 }
3798 }
3799 check_segment[0] = check_segment[1] = got_segment;
3800 break;
3801
3802 case R_FRV_GPREL12:
3803 case R_FRV_GPRELU12:
3804 case R_FRV_GPREL32:
3805 case R_FRV_GPRELHI:
3806 case R_FRV_GPRELLO:
3807 check_segment[0] = gprel_segment;
3808 check_segment[1] = sec
3809 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3810 : (unsigned)-1;
3811 break;
3812
3813 case R_FRV_GETTLSOFF:
3814 relocation = frvfdpic_plt_section (info)->output_section->vma
3815 + frvfdpic_plt_section (info)->output_offset
3816 + picrel->tlsplt_entry;
3817 BFD_ASSERT (picrel->tlsplt_entry != (bfd_vma)-1
3818 && picrel->tlsdesc_entry);
3819 check_segment[0] = isec_segment;
3820 check_segment[1] = plt_segment;
3821 break;
3822
3823 case R_FRV_GOTTLSDESC12:
3824 case R_FRV_GOTTLSDESCHI:
3825 case R_FRV_GOTTLSDESCLO:
3826 BFD_ASSERT (picrel->tlsdesc_entry);
3827 relocation = picrel->tlsdesc_entry;
3828 check_segment[0] = tls_segment;
3829 check_segment[1] = sec
3830 && ! bfd_is_abs_section (sec)
3831 && ! bfd_is_und_section (sec)
3832 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3833 : tls_segment;
3834 break;
3835
3836 case R_FRV_TLSMOFF12:
3837 case R_FRV_TLSMOFFHI:
3838 case R_FRV_TLSMOFFLO:
3839 case R_FRV_TLSMOFF:
3840 check_segment[0] = tls_segment;
3841 if (! sec)
3842 check_segment[1] = -1;
3843 else if (bfd_is_abs_section (sec)
3844 || bfd_is_und_section (sec))
3845 {
3846 relocation = 0;
3847 check_segment[1] = tls_segment;
3848 }
3849 else if (sec->output_section)
3850 {
3851 relocation -= tls_biased_base (info);
3852 check_segment[1] =
3853 _frvfdpic_osec_to_segment (output_bfd, sec->output_section);
3854 }
3855 else
3856 check_segment[1] = -1;
3857 break;
3858
3859 case R_FRV_GOTTLSOFF12:
3860 case R_FRV_GOTTLSOFFHI:
3861 case R_FRV_GOTTLSOFFLO:
3862 BFD_ASSERT (picrel->tlsoff_entry);
3863 relocation = picrel->tlsoff_entry;
3864 check_segment[0] = tls_segment;
3865 check_segment[1] = sec
3866 && ! bfd_is_abs_section (sec)
3867 && ! bfd_is_und_section (sec)
3868 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3869 : tls_segment;
3870 break;
3871
3872 case R_FRV_TLSDESC_VALUE:
3873 case R_FRV_TLSOFF:
3874 /* These shouldn't be present in input object files. */
3875 check_segment[0] = check_segment[1] = isec_segment;
3876 break;
3877
3878 case R_FRV_TLSDESC_RELAX:
3879 case R_FRV_GETTLSOFF_RELAX:
3880 case R_FRV_TLSOFF_RELAX:
3881 /* These are just annotations for relaxation, nothing to do
3882 here. */
3883 continue;
3884
3885 default:
3886 check_segment[0] = isec_segment;
3887 check_segment[1] = sec
3888 ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3889 : (unsigned)-1;
3890 break;
3891 }
3892
3893 if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
3894 {
3895 /* If you take this out, remove the #error from fdpic-static-6.d
3896 in the ld testsuite. */
3897 /* This helps catch problems in GCC while we can't do more
3898 than static linking. The idea is to test whether the
3899 input file basename is crt0.o only once. */
3900 if (silence_segment_error == 1)
3901 silence_segment_error =
3902 (strlen (bfd_get_filename (input_bfd)) == 6
3903 && filename_cmp (bfd_get_filename (input_bfd), "crt0.o") == 0)
3904 || (strlen (bfd_get_filename (input_bfd)) > 6
3905 && filename_cmp (bfd_get_filename (input_bfd)
3906 + strlen (bfd_get_filename (input_bfd)) - 7,
3907 "/crt0.o") == 0)
3908 ? -1 : 0;
3909 if (!silence_segment_error
3910 /* We don't want duplicate errors for undefined
3911 symbols. */
3912 && !(picrel && picrel->symndx == -1
3913 && picrel->d.h->root.type == bfd_link_hash_undefined))
3914 {
3915 info->callbacks->einfo
3916 /* xgettext:c-format */
3917 (_("%H: reloc against `%s' references a different segment\n"),
3918 input_bfd, input_section, rel->r_offset, name);
3919 }
3920 if (!silence_segment_error && bfd_link_pic (info))
3921 return FALSE;
3922 elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
3923 }
3924
3925 switch (r_type)
3926 {
3927 case R_FRV_GOTOFFHI:
3928 case R_FRV_TLSMOFFHI:
3929 /* We need the addend to be applied before we shift the
3930 value right. */
3931 relocation += rel->r_addend;
3932 /* Fall through. */
3933 case R_FRV_GOTHI:
3934 case R_FRV_FUNCDESC_GOTHI:
3935 case R_FRV_FUNCDESC_GOTOFFHI:
3936 case R_FRV_GOTTLSOFFHI:
3937 case R_FRV_GOTTLSDESCHI:
3938 relocation >>= 16;
3939 /* Fall through. */
3940
3941 case R_FRV_GOTLO:
3942 case R_FRV_FUNCDESC_GOTLO:
3943 case R_FRV_GOTOFFLO:
3944 case R_FRV_FUNCDESC_GOTOFFLO:
3945 case R_FRV_GOTTLSOFFLO:
3946 case R_FRV_GOTTLSDESCLO:
3947 case R_FRV_TLSMOFFLO:
3948 relocation &= 0xffff;
3949 break;
3950
3951 default:
3952 break;
3953 }
3954
3955 switch (r_type)
3956 {
3957 case R_FRV_LABEL24:
3958 if (! IS_FDPIC (output_bfd) || ! picrel->plt)
3959 break;
3960 /* Fall through. */
3961
3962 /* When referencing a GOT entry, a function descriptor or a
3963 PLT, we don't want the addend to apply to the reference,
3964 but rather to the referenced symbol. The actual entry
3965 will have already been created taking the addend into
3966 account, so cancel it out here. */
3967 case R_FRV_GOT12:
3968 case R_FRV_GOTHI:
3969 case R_FRV_GOTLO:
3970 case R_FRV_FUNCDESC_GOT12:
3971 case R_FRV_FUNCDESC_GOTHI:
3972 case R_FRV_FUNCDESC_GOTLO:
3973 case R_FRV_FUNCDESC_GOTOFF12:
3974 case R_FRV_FUNCDESC_GOTOFFHI:
3975 case R_FRV_FUNCDESC_GOTOFFLO:
3976 case R_FRV_GETTLSOFF:
3977 case R_FRV_GOTTLSDESC12:
3978 case R_FRV_GOTTLSDESCHI:
3979 case R_FRV_GOTTLSDESCLO:
3980 case R_FRV_GOTTLSOFF12:
3981 case R_FRV_GOTTLSOFFHI:
3982 case R_FRV_GOTTLSOFFLO:
3983 /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
3984 here, since we do want to apply the addend to the others.
3985 Note that we've applied the addend to GOTOFFHI before we
3986 shifted it right. */
3987 case R_FRV_GOTOFFHI:
3988 case R_FRV_TLSMOFFHI:
3989 relocation -= rel->r_addend;
3990 break;
3991
3992 default:
3993 break;
3994 }
3995
3996 if (r_type == R_FRV_HI16)
3997 r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
3998
3999 else if (r_type == R_FRV_LO16)
4000 r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
4001
4002 else if (r_type == R_FRV_LABEL24 || r_type == R_FRV_GETTLSOFF)
4003 r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
4004 contents, relocation);
4005
4006 else if (r_type == R_FRV_GPREL12)
4007 r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
4008 contents, relocation);
4009
4010 else if (r_type == R_FRV_GPRELU12)
4011 r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
4012 contents, relocation);
4013
4014 else if (r_type == R_FRV_GPRELLO)
4015 r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
4016 contents, relocation);
4017
4018 else if (r_type == R_FRV_GPRELHI)
4019 r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
4020 contents, relocation);
4021
4022 else if (r_type == R_FRV_TLSOFF
4023 || r_type == R_FRV_TLSDESC_VALUE)
4024 r = bfd_reloc_notsupported;
4025
4026 else
4027 r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
4028 rel, relocation);
4029
4030 if (r != bfd_reloc_ok)
4031 {
4032 const char * msg = (const char *) NULL;
4033
4034 switch (r)
4035 {
4036 case bfd_reloc_overflow:
4037 (*info->callbacks->reloc_overflow)
4038 (info, (h ? &h->root : NULL), name, howto->name,
4039 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4040 break;
4041
4042 case bfd_reloc_undefined:
4043 (*info->callbacks->undefined_symbol)
4044 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
4045 break;
4046
4047 case bfd_reloc_outofrange:
4048 msg = _("internal error: out of range error");
4049 break;
4050
4051 case bfd_reloc_notsupported:
4052 msg = _("internal error: unsupported relocation error");
4053 break;
4054
4055 case bfd_reloc_dangerous:
4056 msg = _("internal error: dangerous relocation");
4057 break;
4058
4059 default:
4060 msg = _("internal error: unknown error");
4061 break;
4062 }
4063
4064 if (msg)
4065 {
4066 info->callbacks->einfo
4067 /* xgettext:c-format */
4068 (_("%H: reloc against `%s': %s\n"),
4069 input_bfd, input_section, rel->r_offset, name, msg);
4070 return FALSE;
4071 }
4072 }
4073 }
4074
4075 return TRUE;
4076 }
4077 \f
4078 /* Return the section that should be marked against GC for a given
4079 relocation. */
4080
4081 static asection *
4082 elf32_frv_gc_mark_hook (asection *sec,
4083 struct bfd_link_info *info,
4084 Elf_Internal_Rela *rel,
4085 struct elf_link_hash_entry *h,
4086 Elf_Internal_Sym *sym)
4087 {
4088 if (h != NULL)
4089 switch (ELF32_R_TYPE (rel->r_info))
4090 {
4091 case R_FRV_GNU_VTINHERIT:
4092 case R_FRV_GNU_VTENTRY:
4093 return NULL;
4094 }
4095
4096 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4097 }
4098 \f
4099 /* Hook called by the linker routine which adds symbols from an object
4100 file. We use it to put .comm items in .scomm, and not .comm. */
4101
4102 static bfd_boolean
4103 elf32_frv_add_symbol_hook (bfd *abfd,
4104 struct bfd_link_info *info,
4105 Elf_Internal_Sym *sym,
4106 const char **namep ATTRIBUTE_UNUSED,
4107 flagword *flagsp ATTRIBUTE_UNUSED,
4108 asection **secp,
4109 bfd_vma *valp)
4110 {
4111 if (sym->st_shndx == SHN_COMMON
4112 && !bfd_link_relocatable (info)
4113 && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
4114 {
4115 /* Common symbols less than or equal to -G nn bytes are
4116 automatically put into .sbss. */
4117
4118 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
4119
4120 if (scomm == NULL)
4121 {
4122 scomm = bfd_make_section_with_flags (abfd, ".scommon",
4123 (SEC_ALLOC
4124 | SEC_IS_COMMON
4125 | SEC_LINKER_CREATED));
4126 if (scomm == NULL)
4127 return FALSE;
4128 }
4129
4130 *secp = scomm;
4131 *valp = sym->st_size;
4132 }
4133
4134 return TRUE;
4135 }
4136
4137 /* We need dynamic symbols for every section, since segments can
4138 relocate independently. */
4139 static bfd_boolean
4140 _frvfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
4141 struct bfd_link_info *info
4142 ATTRIBUTE_UNUSED,
4143 asection *p ATTRIBUTE_UNUSED)
4144 {
4145 switch (elf_section_data (p)->this_hdr.sh_type)
4146 {
4147 case SHT_PROGBITS:
4148 case SHT_NOBITS:
4149 /* If sh_type is yet undecided, assume it could be
4150 SHT_PROGBITS/SHT_NOBITS. */
4151 case SHT_NULL:
4152 return FALSE;
4153
4154 /* There shouldn't be section relative relocations
4155 against any other section. */
4156 default:
4157 return TRUE;
4158 }
4159 }
4160
4161 /* Create a .got section, as well as its additional info field. This
4162 is almost entirely copied from
4163 elflink.c:_bfd_elf_create_got_section(). */
4164
4165 static bfd_boolean
4166 _frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
4167 {
4168 flagword flags, pltflags;
4169 asection *s;
4170 struct elf_link_hash_entry *h;
4171 struct bfd_link_hash_entry *bh;
4172 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4173 int ptralign;
4174 int offset;
4175
4176 /* This function may be called more than once. */
4177 s = elf_hash_table (info)->sgot;
4178 if (s != NULL)
4179 return TRUE;
4180
4181 /* Machine specific: although pointers are 32-bits wide, we want the
4182 GOT to be aligned to a 64-bit boundary, such that function
4183 descriptors in it can be accessed with 64-bit loads and
4184 stores. */
4185 ptralign = 3;
4186
4187 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4188 | SEC_LINKER_CREATED);
4189 pltflags = flags;
4190
4191 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4192 elf_hash_table (info)->sgot = s;
4193 if (s == NULL
4194 || !bfd_set_section_alignment (s, ptralign))
4195 return FALSE;
4196
4197 if (bed->want_got_sym)
4198 {
4199 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
4200 (or .got.plt) section. We don't do this in the linker script
4201 because we don't want to define the symbol if we are not creating
4202 a global offset table. */
4203 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
4204 elf_hash_table (info)->hgot = h;
4205 if (h == NULL)
4206 return FALSE;
4207
4208 /* Machine-specific: we want the symbol for executables as
4209 well. */
4210 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4211 return FALSE;
4212 }
4213
4214 /* The first bit of the global offset table is the header. */
4215 s->size += bed->got_header_size;
4216
4217 /* This is the machine-specific part. Create and initialize section
4218 data for the got. */
4219 if (IS_FDPIC (abfd))
4220 {
4221 frvfdpic_relocs_info (info) = htab_try_create (1,
4222 frvfdpic_relocs_info_hash,
4223 frvfdpic_relocs_info_eq,
4224 (htab_del) NULL);
4225 if (! frvfdpic_relocs_info (info))
4226 return FALSE;
4227
4228 s = bfd_make_section_anyway_with_flags (abfd, ".rel.got",
4229 (flags | SEC_READONLY));
4230 elf_hash_table (info)->srelgot = s;
4231 if (s == NULL
4232 || !bfd_set_section_alignment (s, 2))
4233 return FALSE;
4234
4235 /* Machine-specific. */
4236 s = bfd_make_section_anyway_with_flags (abfd, ".rofixup",
4237 (flags | SEC_READONLY));
4238 if (s == NULL
4239 || !bfd_set_section_alignment (s, 2))
4240 return FALSE;
4241
4242 frvfdpic_gotfixup_section (info) = s;
4243 offset = -2048;
4244 flags = BSF_GLOBAL;
4245 }
4246 else
4247 {
4248 offset = 2048;
4249 flags = BSF_GLOBAL | BSF_WEAK;
4250 }
4251
4252 /* Define _gp in .rofixup, for FDPIC, or .got otherwise. If it
4253 turns out that we're linking with a different linker script, the
4254 linker script will override it. */
4255 bh = NULL;
4256 if (!(_bfd_generic_link_add_one_symbol
4257 (info, abfd, "_gp", flags, s, offset, (const char *) NULL, FALSE,
4258 bed->collect, &bh)))
4259 return FALSE;
4260 h = (struct elf_link_hash_entry *) bh;
4261 h->def_regular = 1;
4262 h->type = STT_OBJECT;
4263 /* h->other = STV_HIDDEN; */ /* Should we? */
4264
4265 /* Machine-specific: we want the symbol for executables as well. */
4266 if (IS_FDPIC (abfd) && ! bfd_elf_link_record_dynamic_symbol (info, h))
4267 return FALSE;
4268
4269 if (!IS_FDPIC (abfd))
4270 return TRUE;
4271
4272 /* FDPIC supports Thread Local Storage, and this may require a
4273 procedure linkage table for TLS PLT entries. */
4274
4275 /* This is mostly copied from
4276 elflink.c:_bfd_elf_create_dynamic_sections(). */
4277
4278 flags = pltflags;
4279 pltflags |= SEC_CODE;
4280 if (bed->plt_not_loaded)
4281 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
4282 if (bed->plt_readonly)
4283 pltflags |= SEC_READONLY;
4284
4285 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
4286 if (s == NULL
4287 || !bfd_set_section_alignment (s, bed->plt_alignment))
4288 return FALSE;
4289 /* FRV-specific: remember it. */
4290 frvfdpic_plt_section (info) = s;
4291
4292 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
4293 .plt section. */
4294 if (bed->want_plt_sym)
4295 {
4296 h = _bfd_elf_define_linkage_sym (abfd, info, s,
4297 "_PROCEDURE_LINKAGE_TABLE_");
4298 elf_hash_table (info)->hplt = h;
4299 if (h == NULL)
4300 return FALSE;
4301 }
4302
4303 /* FRV-specific: we want rel relocations for the plt. */
4304 s = bfd_make_section_anyway_with_flags (abfd, ".rel.plt",
4305 flags | SEC_READONLY);
4306 if (s == NULL
4307 || !bfd_set_section_alignment (s, bed->s->log_file_align))
4308 return FALSE;
4309 /* FRV-specific: remember it. */
4310 frvfdpic_pltrel_section (info) = s;
4311
4312 return TRUE;
4313 }
4314
4315 /* Make sure the got and plt sections exist, and that our pointers in
4316 the link hash table point to them. */
4317
4318 static bfd_boolean
4319 elf32_frvfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4320 {
4321 /* This is mostly copied from
4322 elflink.c:_bfd_elf_create_dynamic_sections(). */
4323 flagword flags;
4324 asection *s;
4325 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4326
4327 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4328 | SEC_LINKER_CREATED);
4329
4330 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4331 .rel[a].bss sections. */
4332
4333 /* FRV-specific: we want to create the GOT and the PLT in the FRV
4334 way. */
4335 if (! _frv_create_got_section (abfd, info))
4336 return FALSE;
4337
4338 /* FRV-specific: make sure we created everything we wanted. */
4339 BFD_ASSERT (frvfdpic_got_section (info) && frvfdpic_gotrel_section (info)
4340 && frvfdpic_gotfixup_section (info)
4341 && frvfdpic_plt_section (info)
4342 && frvfdpic_pltrel_section (info));
4343
4344 if (bed->want_dynbss)
4345 {
4346 /* The .dynbss section is a place to put symbols which are defined
4347 by dynamic objects, are referenced by regular objects, and are
4348 not functions. We must allocate space for them in the process
4349 image and use a R_*_COPY reloc to tell the dynamic linker to
4350 initialize them at run time. The linker script puts the .dynbss
4351 section into the .bss section of the final image. */
4352 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
4353 SEC_ALLOC | SEC_LINKER_CREATED);
4354 if (s == NULL)
4355 return FALSE;
4356
4357 /* The .rel[a].bss section holds copy relocs. This section is not
4358 normally needed. We need to create it here, though, so that the
4359 linker will map it to an output section. We can't just create it
4360 only if we need it, because we will not know whether we need it
4361 until we have seen all the input files, and the first time the
4362 main linker code calls BFD after examining all the input files
4363 (size_dynamic_sections) the input sections have already been
4364 mapped to the output sections. If the section turns out not to
4365 be needed, we can discard it later. We will never need this
4366 section when generating a shared object, since they do not use
4367 copy relocs. */
4368 if (! bfd_link_pic (info))
4369 {
4370 s = bfd_make_section_anyway_with_flags (abfd,
4371 (bed->default_use_rela_p
4372 ? ".rela.bss" : ".rel.bss"),
4373 flags | SEC_READONLY);
4374 if (s == NULL
4375 || !bfd_set_section_alignment (s, bed->s->log_file_align))
4376 return FALSE;
4377 }
4378 }
4379
4380 return TRUE;
4381 }
4382
4383 /* Compute the total GOT and PLT size required by each symbol in each
4384 range. Symbols may require up to 4 words in the GOT: an entry
4385 pointing to the symbol, an entry pointing to its function
4386 descriptor, and a private function descriptors taking two
4387 words. */
4388
4389 static void
4390 _frvfdpic_count_nontls_entries (struct frvfdpic_relocs_info *entry,
4391 struct _frvfdpic_dynamic_got_info *dinfo)
4392 {
4393 /* Allocate space for a GOT entry pointing to the symbol. */
4394 if (entry->got12)
4395 dinfo->got12 += 4;
4396 else if (entry->gotlos)
4397 dinfo->gotlos += 4;
4398 else if (entry->gothilo)
4399 dinfo->gothilo += 4;
4400 else
4401 entry->relocs32--;
4402 entry->relocs32++;
4403
4404 /* Allocate space for a GOT entry pointing to the function
4405 descriptor. */
4406 if (entry->fdgot12)
4407 dinfo->got12 += 4;
4408 else if (entry->fdgotlos)
4409 dinfo->gotlos += 4;
4410 else if (entry->fdgothilo)
4411 dinfo->gothilo += 4;
4412 else
4413 entry->relocsfd--;
4414 entry->relocsfd++;
4415
4416 /* Decide whether we need a PLT entry, a function descriptor in the
4417 GOT, and a lazy PLT entry for this symbol. */
4418 entry->plt = entry->call
4419 && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4420 && elf_hash_table (dinfo->info)->dynamic_sections_created;
4421 entry->privfd = entry->plt
4422 || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
4423 || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
4424 && (entry->symndx != -1
4425 || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
4426 entry->lazyplt = entry->privfd
4427 && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4428 && ! (dinfo->info->flags & DF_BIND_NOW)
4429 && elf_hash_table (dinfo->info)->dynamic_sections_created;
4430
4431 /* Allocate space for a function descriptor. */
4432 if (entry->fdgoff12)
4433 dinfo->fd12 += 8;
4434 else if (entry->fdgofflos)
4435 dinfo->fdlos += 8;
4436 else if (entry->privfd && entry->plt)
4437 dinfo->fdplt += 8;
4438 else if (entry->privfd)
4439 dinfo->fdhilo += 8;
4440 else
4441 entry->relocsfdv--;
4442 entry->relocsfdv++;
4443
4444 if (entry->lazyplt)
4445 dinfo->lzplt += 8;
4446 }
4447
4448 /* Compute the total GOT size required by each TLS symbol in each
4449 range. Symbols may require up to 5 words in the GOT: an entry
4450 holding the TLS offset for the symbol, and an entry with a full TLS
4451 descriptor taking 4 words. */
4452
4453 static void
4454 _frvfdpic_count_tls_entries (struct frvfdpic_relocs_info *entry,
4455 struct _frvfdpic_dynamic_got_info *dinfo,
4456 bfd_boolean subtract)
4457 {
4458 const int l = subtract ? -1 : 1;
4459
4460 /* Allocate space for a GOT entry with the TLS offset of the
4461 symbol. */
4462 if (entry->tlsoff12)
4463 dinfo->got12 += 4 * l;
4464 else if (entry->tlsofflos)
4465 dinfo->gotlos += 4 * l;
4466 else if (entry->tlsoffhilo)
4467 dinfo->gothilo += 4 * l;
4468 else
4469 entry->relocstlsoff -= l;
4470 entry->relocstlsoff += l;
4471
4472 /* If there's any TLSOFF relocation, mark the output file as not
4473 suitable for dlopening. This mark will remain even if we relax
4474 all such relocations, but this is not a problem, since we'll only
4475 do so for executables, and we definitely don't want anyone
4476 dlopening executables. */
4477 if (entry->relocstlsoff)
4478 dinfo->info->flags |= DF_STATIC_TLS;
4479
4480 /* Allocate space for a TLS descriptor. */
4481 if (entry->tlsdesc12)
4482 dinfo->tlsd12 += 8 * l;
4483 else if (entry->tlsdesclos)
4484 dinfo->tlsdlos += 8 * l;
4485 else if (entry->tlsplt)
4486 dinfo->tlsdplt += 8 * l;
4487 else if (entry->tlsdeschilo)
4488 dinfo->tlsdhilo += 8 * l;
4489 else
4490 entry->relocstlsd -= l;
4491 entry->relocstlsd += l;
4492 }
4493
4494 /* Compute the number of dynamic relocations and fixups that a symbol
4495 requires, and add (or subtract) from the grand and per-symbol
4496 totals. */
4497
4498 static void
4499 _frvfdpic_count_relocs_fixups (struct frvfdpic_relocs_info *entry,
4500 struct _frvfdpic_dynamic_got_info *dinfo,
4501 bfd_boolean subtract)
4502 {
4503 bfd_vma relocs = 0, fixups = 0, tlsrets = 0;
4504
4505 if (!bfd_link_pde (dinfo->info))
4506 {
4507 relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv
4508 + entry->relocstlsd;
4509
4510 /* In the executable, TLS relocations to symbols that bind
4511 locally (including those that resolve to global TLS offsets)
4512 are resolved immediately, without any need for fixups or
4513 dynamic relocations. In shared libraries, however, we must
4514 emit dynamic relocations even for local symbols, because we
4515 don't know the module id the library is going to get at
4516 run-time, nor its TLS base offset. */
4517 if (!bfd_link_executable (dinfo->info)
4518 || (entry->symndx == -1
4519 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4520 relocs += entry->relocstlsoff;
4521 }
4522 else
4523 {
4524 if (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
4525 {
4526 if (entry->symndx != -1
4527 || entry->d.h->root.type != bfd_link_hash_undefweak)
4528 fixups += entry->relocs32 + 2 * entry->relocsfdv;
4529 fixups += entry->relocstlsd;
4530 tlsrets += entry->relocstlsd;
4531 }
4532 else
4533 {
4534 relocs += entry->relocs32 + entry->relocsfdv
4535 + entry->relocstlsoff + entry->relocstlsd;
4536 }
4537
4538 if (entry->symndx != -1
4539 || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
4540 {
4541 if (entry->symndx != -1
4542 || entry->d.h->root.type != bfd_link_hash_undefweak)
4543 fixups += entry->relocsfd;
4544 }
4545 else
4546 relocs += entry->relocsfd;
4547 }
4548
4549 if (subtract)
4550 {
4551 relocs = - relocs;
4552 fixups = - fixups;
4553 tlsrets = - tlsrets;
4554 }
4555
4556 entry->dynrelocs += relocs;
4557 entry->fixups += fixups;
4558 dinfo->relocs += relocs;
4559 dinfo->fixups += fixups;
4560 dinfo->tls_ret_refs += tlsrets;
4561 }
4562
4563 /* Look for opportunities to relax TLS relocations. We can assume
4564 we're linking the main executable or a static-tls library, since
4565 otherwise we wouldn't have got here. When relaxing, we have to
4566 first undo any previous accounting of TLS uses of fixups, dynamic
4567 relocations, GOT and PLT entries. */
4568
4569 static void
4570 _frvfdpic_relax_tls_entries (struct frvfdpic_relocs_info *entry,
4571 struct _frvfdpic_dynamic_got_info *dinfo,
4572 bfd_boolean relaxing)
4573 {
4574 bfd_boolean changed = ! relaxing;
4575
4576 BFD_ASSERT (bfd_link_executable (dinfo->info)
4577 || (dinfo->info->flags & DF_STATIC_TLS));
4578
4579 if (entry->tlsdesc12 || entry->tlsdesclos || entry->tlsdeschilo)
4580 {
4581 if (! changed)
4582 {
4583 _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4584 _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4585 changed = TRUE;
4586 }
4587
4588 /* When linking an executable, we can always decay GOTTLSDESC to
4589 TLSMOFF, if the symbol is local, or GOTTLSOFF, otherwise.
4590 When linking a static-tls shared library, using TLSMOFF is
4591 not an option, but we can still use GOTTLSOFF. When decaying
4592 to GOTTLSOFF, we must keep the GOT entry in range. We know
4593 it has to fit because we'll be trading the 4 words of hte TLS
4594 descriptor for a single word in the same range. */
4595 if (! bfd_link_executable (dinfo->info)
4596 || (entry->symndx == -1
4597 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4598 {
4599 entry->tlsoff12 |= entry->tlsdesc12;
4600 entry->tlsofflos |= entry->tlsdesclos;
4601 entry->tlsoffhilo |= entry->tlsdeschilo;
4602 }
4603
4604 entry->tlsdesc12 = entry->tlsdesclos = entry->tlsdeschilo = 0;
4605 }
4606
4607 /* We can only decay TLSOFFs or call #gettlsoff to TLSMOFF in the
4608 main executable. We have to check whether the symbol's TLSOFF is
4609 in range for a setlos. For symbols with a hash entry, we can
4610 determine exactly what to do; for others locals, we don't have
4611 addresses handy, so we use the size of the TLS section as an
4612 approximation. If we get it wrong, we'll retain a GOT entry
4613 holding the TLS offset (without dynamic relocations or fixups),
4614 but we'll still optimize away the loads from it. Since TLS sizes
4615 are generally very small, it's probably not worth attempting to
4616 do better than this. */
4617 if ((entry->tlsplt
4618 || entry->tlsoff12 || entry->tlsofflos || entry->tlsoffhilo)
4619 && bfd_link_executable (dinfo->info) && relaxing
4620 && ((entry->symndx == -1
4621 && FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4622 /* The above may hold for an undefweak TLS symbol, so make
4623 sure we don't have this case before accessing def.value
4624 and def.section. */
4625 && (entry->d.h->root.type == bfd_link_hash_undefweak
4626 || (bfd_vma)(entry->d.h->root.u.def.value
4627 + (entry->d.h->root.u.def.section
4628 ->output_section->vma)
4629 + entry->d.h->root.u.def.section->output_offset
4630 + entry->addend
4631 - tls_biased_base (dinfo->info)
4632 + 32768) < (bfd_vma)65536))
4633 || (entry->symndx != -1
4634 && (elf_hash_table (dinfo->info)->tls_sec->size
4635 + entry->addend < 32768 + FRVFDPIC_TLS_BIAS))))
4636 {
4637 if (! changed)
4638 {
4639 _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4640 _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4641 changed = TRUE;
4642 }
4643
4644 entry->tlsplt =
4645 entry->tlsoff12 = entry->tlsofflos = entry->tlsoffhilo = 0;
4646 }
4647
4648 /* We can decay `call #gettlsoff' to a ldi #tlsoff if we already
4649 have a #gottlsoff12 relocation for this entry, or if we can fit
4650 one more in the 12-bit (and 16-bit) ranges. */
4651 if (entry->tlsplt
4652 && (entry->tlsoff12
4653 || (relaxing
4654 && dinfo->got12 + dinfo->fd12 + dinfo->tlsd12 <= 4096 - 12 - 4
4655 && (dinfo->got12 + dinfo->fd12 + dinfo->tlsd12
4656 + dinfo->gotlos + dinfo->fdlos + dinfo->tlsdlos
4657 <= 65536 - 12 - 4))))
4658 {
4659 if (! changed)
4660 {
4661 _frvfdpic_count_relocs_fixups (entry, dinfo, TRUE);
4662 _frvfdpic_count_tls_entries (entry, dinfo, TRUE);
4663 changed = TRUE;
4664 }
4665
4666 entry->tlsoff12 = 1;
4667 entry->tlsplt = 0;
4668 }
4669
4670 if (changed)
4671 {
4672 _frvfdpic_count_tls_entries (entry, dinfo, FALSE);
4673 _frvfdpic_count_relocs_fixups (entry, dinfo, FALSE);
4674 }
4675
4676 return;
4677 }
4678
4679 /* Compute the total GOT and PLT size required by each symbol in each range. *
4680 Symbols may require up to 4 words in the GOT: an entry pointing to
4681 the symbol, an entry pointing to its function descriptor, and a
4682 private function descriptors taking two words. */
4683
4684 static int
4685 _frvfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
4686 {
4687 struct frvfdpic_relocs_info *entry = *entryp;
4688 struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
4689
4690 _frvfdpic_count_nontls_entries (entry, dinfo);
4691
4692 if (bfd_link_executable (dinfo->info)
4693 || (dinfo->info->flags & DF_STATIC_TLS))
4694 _frvfdpic_relax_tls_entries (entry, dinfo, FALSE);
4695 else
4696 {
4697 _frvfdpic_count_tls_entries (entry, dinfo, FALSE);
4698 _frvfdpic_count_relocs_fixups (entry, dinfo, FALSE);
4699 }
4700
4701 return 1;
4702 }
4703
4704 /* Determine the positive and negative ranges to be used by each
4705 offset range in the GOT. FDCUR and CUR, that must be aligned to a
4706 double-word boundary, are the minimum (negative) and maximum
4707 (positive) GOT offsets already used by previous ranges, except for
4708 an ODD entry that may have been left behind. GOT and FD indicate
4709 the size of GOT entries and function descriptors that must be
4710 placed within the range from -WRAP to WRAP. If there's room left,
4711 up to FDPLT bytes should be reserved for additional function
4712 descriptors. */
4713
4714 inline static bfd_signed_vma
4715 _frvfdpic_compute_got_alloc_data (struct _frvfdpic_dynamic_got_alloc_data *gad,
4716 bfd_signed_vma fdcur,
4717 bfd_signed_vma odd,
4718 bfd_signed_vma cur,
4719 bfd_vma got,
4720 bfd_vma fd,
4721 bfd_vma fdplt,
4722 bfd_vma tlsd,
4723 bfd_vma tlsdplt,
4724 bfd_vma wrap)
4725 {
4726 bfd_signed_vma wrapmin = -wrap;
4727 const bfd_vma tdescsz = 8;
4728
4729 /* Start at the given initial points. */
4730 gad->fdcur = fdcur;
4731 gad->cur = cur;
4732
4733 /* If we had an incoming odd word and we have any got entries that
4734 are going to use it, consume it, otherwise leave gad->odd at
4735 zero. We might force gad->odd to zero and return the incoming
4736 odd such that it is used by the next range, but then GOT entries
4737 might appear to be out of order and we wouldn't be able to
4738 shorten the GOT by one word if it turns out to end with an
4739 unpaired GOT entry. */
4740 if (odd && got)
4741 {
4742 gad->odd = odd;
4743 got -= 4;
4744 odd = 0;
4745 }
4746 else
4747 gad->odd = 0;
4748
4749 /* If we're left with an unpaired GOT entry, compute its location
4750 such that we can return it. Otherwise, if got doesn't require an
4751 odd number of words here, either odd was already zero in the
4752 block above, or it was set to zero because got was non-zero, or
4753 got was already zero. In the latter case, we want the value of
4754 odd to carry over to the return statement, so we don't want to
4755 reset odd unless the condition below is true. */
4756 if (got & 4)
4757 {
4758 odd = cur + got;
4759 got += 4;
4760 }
4761
4762 /* Compute the tentative boundaries of this range. */
4763 gad->max = cur + got;
4764 gad->min = fdcur - fd;
4765 gad->fdplt = 0;
4766
4767 /* If function descriptors took too much space, wrap some of them
4768 around. */
4769 if (gad->min < wrapmin)
4770 {
4771 gad->max += wrapmin - gad->min;
4772 gad->tmin = gad->min = wrapmin;
4773 }
4774
4775 /* If GOT entries took too much space, wrap some of them around.
4776 This may well cause gad->min to become lower than wrapmin. This
4777 will cause a relocation overflow later on, so we don't have to
4778 report it here . */
4779 if ((bfd_vma) gad->max > wrap)
4780 {
4781 gad->min -= gad->max - wrap;
4782 gad->max = wrap;
4783 }
4784
4785 /* Add TLS descriptors. */
4786 gad->tmax = gad->max + tlsd;
4787 gad->tmin = gad->min;
4788 gad->tlsdplt = 0;
4789
4790 /* If TLS descriptors took too much space, wrap an integral number
4791 of them around. */
4792 if ((bfd_vma) gad->tmax > wrap)
4793 {
4794 bfd_vma wrapsize = gad->tmax - wrap;
4795
4796 wrapsize += tdescsz / 2;
4797 wrapsize &= ~ tdescsz / 2;
4798
4799 gad->tmin -= wrapsize;
4800 gad->tmax -= wrapsize;
4801 }
4802
4803 /* If there is space left and we have function descriptors
4804 referenced in PLT entries that could take advantage of shorter
4805 offsets, place them now. */
4806 if (fdplt && gad->tmin > wrapmin)
4807 {
4808 bfd_vma fds;
4809
4810 if ((bfd_vma) (gad->tmin - wrapmin) < fdplt)
4811 fds = gad->tmin - wrapmin;
4812 else
4813 fds = fdplt;
4814
4815 fdplt -= fds;
4816 gad->min -= fds;
4817 gad->tmin -= fds;
4818 gad->fdplt += fds;
4819 }
4820
4821 /* If there is more space left, try to place some more function
4822 descriptors for PLT entries. */
4823 if (fdplt && (bfd_vma) gad->tmax < wrap)
4824 {
4825 bfd_vma fds;
4826
4827 if ((bfd_vma) (wrap - gad->tmax) < fdplt)
4828 fds = wrap - gad->tmax;
4829 else
4830 fds = fdplt;
4831
4832 fdplt -= fds;
4833 gad->max += fds;
4834 gad->tmax += fds;
4835 gad->fdplt += fds;
4836 }
4837
4838 /* If there is space left and we have TLS descriptors referenced in
4839 PLT entries that could take advantage of shorter offsets, place
4840 them now. */
4841 if (tlsdplt && gad->tmin > wrapmin)
4842 {
4843 bfd_vma tlsds;
4844
4845 if ((bfd_vma) (gad->tmin - wrapmin) < tlsdplt)
4846 tlsds = (gad->tmin - wrapmin) & ~ (tdescsz / 2);
4847 else
4848 tlsds = tlsdplt;
4849
4850 tlsdplt -= tlsds;
4851 gad->tmin -= tlsds;
4852 gad->tlsdplt += tlsds;
4853 }
4854
4855 /* If there is more space left, try to place some more TLS
4856 descriptors for PLT entries. Although we could try to fit an
4857 additional TLS descriptor with half of it just before before the
4858 wrap point and another right past the wrap point, this might
4859 cause us to run out of space for the next region, so don't do
4860 it. */
4861 if (tlsdplt && (bfd_vma) gad->tmax < wrap - tdescsz / 2)
4862 {
4863 bfd_vma tlsds;
4864
4865 if ((bfd_vma) (wrap - gad->tmax) < tlsdplt)
4866 tlsds = (wrap - gad->tmax) & ~ (tdescsz / 2);
4867 else
4868 tlsds = tlsdplt;
4869
4870 tlsdplt -= tlsds;
4871 gad->tmax += tlsds;
4872 gad->tlsdplt += tlsds;
4873 }
4874
4875 /* If odd was initially computed as an offset past the wrap point,
4876 wrap it around. */
4877 if (odd > gad->max)
4878 odd = gad->min + odd - gad->max;
4879
4880 /* _frvfdpic_get_got_entry() below will always wrap gad->cur if needed
4881 before returning, so do it here too. This guarantees that,
4882 should cur and fdcur meet at the wrap point, they'll both be
4883 equal to min. */
4884 if (gad->cur == gad->max)
4885 gad->cur = gad->min;
4886
4887 /* Ditto for _frvfdpic_get_tlsdesc_entry(). */
4888 gad->tcur = gad->max;
4889 if (gad->tcur == gad->tmax)
4890 gad->tcur = gad->tmin;
4891
4892 return odd;
4893 }
4894
4895 /* Compute the location of the next GOT entry, given the allocation
4896 data for a range. */
4897
4898 inline static bfd_signed_vma
4899 _frvfdpic_get_got_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4900 {
4901 bfd_signed_vma ret;
4902
4903 if (gad->odd)
4904 {
4905 /* If there was an odd word left behind, use it. */
4906 ret = gad->odd;
4907 gad->odd = 0;
4908 }
4909 else
4910 {
4911 /* Otherwise, use the word pointed to by cur, reserve the next
4912 as an odd word, and skip to the next pair of words, possibly
4913 wrapping around. */
4914 ret = gad->cur;
4915 gad->odd = gad->cur + 4;
4916 gad->cur += 8;
4917 if (gad->cur == gad->max)
4918 gad->cur = gad->min;
4919 }
4920
4921 return ret;
4922 }
4923
4924 /* Compute the location of the next function descriptor entry in the
4925 GOT, given the allocation data for a range. */
4926
4927 inline static bfd_signed_vma
4928 _frvfdpic_get_fd_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4929 {
4930 /* If we're at the bottom, wrap around, and only then allocate the
4931 next pair of words. */
4932 if (gad->fdcur == gad->min)
4933 gad->fdcur = gad->max;
4934 return gad->fdcur -= 8;
4935 }
4936
4937 /* Compute the location of the next TLS descriptor entry in the GOT,
4938 given the allocation data for a range. */
4939 inline static bfd_signed_vma
4940 _frvfdpic_get_tlsdesc_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4941 {
4942 bfd_signed_vma ret;
4943
4944 ret = gad->tcur;
4945
4946 gad->tcur += 8;
4947
4948 /* If we're at the top of the region, wrap around to the bottom. */
4949 if (gad->tcur == gad->tmax)
4950 gad->tcur = gad->tmin;
4951
4952 return ret;
4953 }
4954
4955 /* Assign GOT offsets for every GOT entry and function descriptor.
4956 Doing everything in a single pass is tricky. */
4957
4958 static int
4959 _frvfdpic_assign_got_entries (void **entryp, void *info_)
4960 {
4961 struct frvfdpic_relocs_info *entry = *entryp;
4962 struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
4963
4964 if (entry->got12)
4965 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->got12);
4966 else if (entry->gotlos)
4967 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
4968 else if (entry->gothilo)
4969 entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
4970
4971 if (entry->fdgot12)
4972 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->got12);
4973 else if (entry->fdgotlos)
4974 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
4975 else if (entry->fdgothilo)
4976 entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
4977
4978 if (entry->fdgoff12)
4979 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
4980 else if (entry->plt && dinfo->got12.fdplt)
4981 {
4982 dinfo->got12.fdplt -= 8;
4983 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
4984 }
4985 else if (entry->fdgofflos)
4986 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
4987 else if (entry->plt && dinfo->gotlos.fdplt)
4988 {
4989 dinfo->gotlos.fdplt -= 8;
4990 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
4991 }
4992 else if (entry->plt)
4993 {
4994 dinfo->gothilo.fdplt -= 8;
4995 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
4996 }
4997 else if (entry->privfd)
4998 entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
4999
5000 if (entry->tlsoff12)
5001 entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5002 else if (entry->tlsofflos)
5003 entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5004 else if (entry->tlsoffhilo)
5005 entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5006
5007 if (entry->tlsdesc12)
5008 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5009 else if (entry->tlsplt && dinfo->got12.tlsdplt)
5010 {
5011 dinfo->got12.tlsdplt -= 8;
5012 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5013 }
5014 else if (entry->tlsdesclos)
5015 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5016 else if (entry->tlsplt && dinfo->gotlos.tlsdplt)
5017 {
5018 dinfo->gotlos.tlsdplt -= 8;
5019 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5020 }
5021 else if (entry->tlsplt)
5022 {
5023 dinfo->gothilo.tlsdplt -= 8;
5024 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5025 }
5026 else if (entry->tlsdeschilo)
5027 entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5028
5029 return 1;
5030 }
5031
5032 /* Assign GOT offsets to private function descriptors used by PLT
5033 entries (or referenced by 32-bit offsets), as well as PLT entries
5034 and lazy PLT entries. */
5035
5036 static int
5037 _frvfdpic_assign_plt_entries (void **entryp, void *info_)
5038 {
5039 struct frvfdpic_relocs_info *entry = *entryp;
5040 struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
5041
5042 if (entry->privfd)
5043 BFD_ASSERT (entry->fd_entry);
5044
5045 if (entry->plt)
5046 {
5047 int size;
5048
5049 /* We use the section's raw size to mark the location of the
5050 next PLT entry. */
5051 entry->plt_entry = frvfdpic_plt_section (dinfo->g.info)->size;
5052
5053 /* Figure out the length of this PLT entry based on the
5054 addressing mode we need to reach the function descriptor. */
5055 BFD_ASSERT (entry->fd_entry);
5056 if (entry->fd_entry >= -(1 << (12 - 1))
5057 && entry->fd_entry < (1 << (12 - 1)))
5058 size = 8;
5059 else if (entry->fd_entry >= -(1 << (16 - 1))
5060 && entry->fd_entry < (1 << (16 - 1)))
5061 size = 12;
5062 else
5063 size = 16;
5064
5065 frvfdpic_plt_section (dinfo->g.info)->size += size;
5066 }
5067
5068 if (entry->lazyplt)
5069 {
5070 entry->lzplt_entry = dinfo->g.lzplt;
5071 dinfo->g.lzplt += 8;
5072 /* If this entry is the one that gets the resolver stub, account
5073 for the additional instruction. */
5074 if (entry->lzplt_entry % FRVFDPIC_LZPLT_BLOCK_SIZE
5075 == FRVFDPIC_LZPLT_RESOLV_LOC)
5076 dinfo->g.lzplt += 4;
5077 }
5078
5079 if (entry->tlsplt)
5080 {
5081 int size;
5082
5083 entry->tlsplt_entry
5084 = frvfdpic_plt_section (dinfo->g.info)->size;
5085
5086 if (bfd_link_executable (dinfo->g.info)
5087 && (entry->symndx != -1
5088 || FRVFDPIC_SYM_LOCAL (dinfo->g.info, entry->d.h)))
5089 {
5090 if ((bfd_signed_vma)entry->addend >= -(1 << (16 - 1))
5091 /* FIXME: here we use the size of the TLS section
5092 as an upper bound for the value of the TLS
5093 symbol, because we may not know the exact value
5094 yet. If we get it wrong, we'll just waste a
5095 word in the PLT, and we should never get even
5096 close to 32 KiB of TLS anyway. */
5097 && elf_hash_table (dinfo->g.info)->tls_sec
5098 && (elf_hash_table (dinfo->g.info)->tls_sec->size
5099 + (bfd_signed_vma)(entry->addend) <= (1 << (16 - 1))))
5100 size = 8;
5101 else
5102 size = 12;
5103 }
5104 else if (entry->tlsoff_entry)
5105 {
5106 if (entry->tlsoff_entry >= -(1 << (12 - 1))
5107 && entry->tlsoff_entry < (1 << (12 - 1)))
5108 size = 8;
5109 else if (entry->tlsoff_entry >= -(1 << (16 - 1))
5110 && entry->tlsoff_entry < (1 << (16 - 1)))
5111 size = 12;
5112 else
5113 size = 16;
5114 }
5115 else
5116 {
5117 BFD_ASSERT (entry->tlsdesc_entry);
5118
5119 if (entry->tlsdesc_entry >= -(1 << (12 - 1))
5120 && entry->tlsdesc_entry < (1 << (12 - 1)))
5121 size = 8;
5122 else if (entry->tlsdesc_entry >= -(1 << (16 - 1))
5123 && entry->tlsdesc_entry < (1 << (16 - 1)))
5124 size = 12;
5125 else
5126 size = 16;
5127 }
5128
5129 frvfdpic_plt_section (dinfo->g.info)->size += size;
5130 }
5131
5132 return 1;
5133 }
5134
5135 /* Cancel out any effects of calling _frvfdpic_assign_got_entries and
5136 _frvfdpic_assign_plt_entries. */
5137
5138 static int
5139 _frvfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
5140 {
5141 struct frvfdpic_relocs_info *entry = *entryp;
5142
5143 entry->got_entry = 0;
5144 entry->fdgot_entry = 0;
5145 entry->fd_entry = 0;
5146 entry->plt_entry = (bfd_vma)-1;
5147 entry->lzplt_entry = (bfd_vma)-1;
5148 entry->tlsoff_entry = 0;
5149 entry->tlsdesc_entry = 0;
5150 entry->tlsplt_entry = (bfd_vma)-1;
5151
5152 return 1;
5153 }
5154
5155 /* Follow indirect and warning hash entries so that each got entry
5156 points to the final symbol definition. P must point to a pointer
5157 to the hash table we're traversing. Since this traversal may
5158 modify the hash table, we set this pointer to NULL to indicate
5159 we've made a potentially-destructive change to the hash table, so
5160 the traversal must be restarted. */
5161 static int
5162 _frvfdpic_resolve_final_relocs_info (void **entryp, void *p)
5163 {
5164 struct frvfdpic_relocs_info *entry = *entryp;
5165 htab_t *htab = p;
5166
5167 if (entry->symndx == -1)
5168 {
5169 struct elf_link_hash_entry *h = entry->d.h;
5170 struct frvfdpic_relocs_info *oentry;
5171
5172 while (h->root.type == bfd_link_hash_indirect
5173 || h->root.type == bfd_link_hash_warning)
5174 h = (struct elf_link_hash_entry *)h->root.u.i.link;
5175
5176 if (entry->d.h == h)
5177 return 1;
5178
5179 oentry = frvfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
5180 NO_INSERT);
5181
5182 if (oentry)
5183 {
5184 /* Merge the two entries. */
5185 frvfdpic_pic_merge_early_relocs_info (oentry, entry);
5186 htab_clear_slot (*htab, entryp);
5187 return 1;
5188 }
5189
5190 entry->d.h = h;
5191
5192 /* If we can't find this entry with the new bfd hash, re-insert
5193 it, and get the traversal restarted. */
5194 if (! htab_find (*htab, entry))
5195 {
5196 htab_clear_slot (*htab, entryp);
5197 entryp = htab_find_slot (*htab, entry, INSERT);
5198 if (! *entryp)
5199 *entryp = entry;
5200 /* Abort the traversal, since the whole table may have
5201 moved, and leave it up to the parent to restart the
5202 process. */
5203 *(htab_t *)p = NULL;
5204 return 0;
5205 }
5206 }
5207
5208 return 1;
5209 }
5210
5211 /* Compute the total size of the GOT, the PLT, the dynamic relocations
5212 section and the rofixup section. Assign locations for GOT and PLT
5213 entries. */
5214
5215 static bfd_boolean
5216 _frvfdpic_size_got_plt (bfd *output_bfd,
5217 struct _frvfdpic_dynamic_got_plt_info *gpinfop)
5218 {
5219 bfd_signed_vma odd;
5220 bfd_vma limit, tlslimit;
5221 struct bfd_link_info *info = gpinfop->g.info;
5222 bfd *dynobj = elf_hash_table (info)->dynobj;
5223
5224 memcpy (frvfdpic_dynamic_got_plt_info (info), &gpinfop->g,
5225 sizeof (gpinfop->g));
5226
5227 odd = 12;
5228 /* Compute the total size taken by entries in the 12-bit and 16-bit
5229 ranges, to tell how many PLT function descriptors we can bring
5230 into the 12-bit range without causing the 16-bit range to
5231 overflow. */
5232 limit = odd + gpinfop->g.got12 + gpinfop->g.gotlos
5233 + gpinfop->g.fd12 + gpinfop->g.fdlos
5234 + gpinfop->g.tlsd12 + gpinfop->g.tlsdlos;
5235 if (limit < (bfd_vma)1 << 16)
5236 limit = ((bfd_vma)1 << 16) - limit;
5237 else
5238 limit = 0;
5239 if (gpinfop->g.fdplt < limit)
5240 {
5241 tlslimit = (limit - gpinfop->g.fdplt) & ~ (bfd_vma) 8;
5242 limit = gpinfop->g.fdplt;
5243 }
5244 else
5245 tlslimit = 0;
5246 if (gpinfop->g.tlsdplt < tlslimit)
5247 tlslimit = gpinfop->g.tlsdplt;
5248
5249 /* Determine the ranges of GOT offsets that we can use for each
5250 range of addressing modes. */
5251 odd = _frvfdpic_compute_got_alloc_data (&gpinfop->got12,
5252 0,
5253 odd,
5254 16,
5255 gpinfop->g.got12,
5256 gpinfop->g.fd12,
5257 limit,
5258 gpinfop->g.tlsd12,
5259 tlslimit,
5260 (bfd_vma)1 << (12-1));
5261 odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gotlos,
5262 gpinfop->got12.tmin,
5263 odd,
5264 gpinfop->got12.tmax,
5265 gpinfop->g.gotlos,
5266 gpinfop->g.fdlos,
5267 gpinfop->g.fdplt
5268 - gpinfop->got12.fdplt,
5269 gpinfop->g.tlsdlos,
5270 gpinfop->g.tlsdplt
5271 - gpinfop->got12.tlsdplt,
5272 (bfd_vma)1 << (16-1));
5273 odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gothilo,
5274 gpinfop->gotlos.tmin,
5275 odd,
5276 gpinfop->gotlos.tmax,
5277 gpinfop->g.gothilo,
5278 gpinfop->g.fdhilo,
5279 gpinfop->g.fdplt
5280 - gpinfop->got12.fdplt
5281 - gpinfop->gotlos.fdplt,
5282 gpinfop->g.tlsdhilo,
5283 gpinfop->g.tlsdplt
5284 - gpinfop->got12.tlsdplt
5285 - gpinfop->gotlos.tlsdplt,
5286 (bfd_vma)1 << (32-1));
5287
5288 /* Now assign (most) GOT offsets. */
5289 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_got_entries,
5290 gpinfop);
5291
5292 frvfdpic_got_section (info)->size = gpinfop->gothilo.tmax
5293 - gpinfop->gothilo.tmin
5294 /* If an odd word is the last word of the GOT, we don't need this
5295 word to be part of the GOT. */
5296 - (odd + 4 == gpinfop->gothilo.tmax ? 4 : 0);
5297 if (frvfdpic_got_section (info)->size == 0)
5298 frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5299 else if (frvfdpic_got_section (info)->size == 12
5300 && ! elf_hash_table (info)->dynamic_sections_created)
5301 {
5302 frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5303 frvfdpic_got_section (info)->size = 0;
5304 }
5305 /* This will be non-NULL during relaxation. The assumption is that
5306 the size of one of these sections will never grow, only shrink,
5307 so we can use the larger buffer we allocated before. */
5308 else if (frvfdpic_got_section (info)->contents == NULL)
5309 {
5310 frvfdpic_got_section (info)->contents =
5311 (bfd_byte *) bfd_zalloc (dynobj,
5312 frvfdpic_got_section (info)->size);
5313 if (frvfdpic_got_section (info)->contents == NULL)
5314 return FALSE;
5315 }
5316
5317 if (frvfdpic_gotrel_section (info))
5318 /* Subtract the number of lzplt entries, since those will generate
5319 relocations in the pltrel section. */
5320 frvfdpic_gotrel_section (info)->size =
5321 (gpinfop->g.relocs - gpinfop->g.lzplt / 8)
5322 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5323 else
5324 BFD_ASSERT (gpinfop->g.relocs == 0);
5325 if (frvfdpic_gotrel_section (info)->size == 0)
5326 frvfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
5327 else if (frvfdpic_gotrel_section (info)->contents == NULL)
5328 {
5329 frvfdpic_gotrel_section (info)->contents =
5330 (bfd_byte *) bfd_zalloc (dynobj,
5331 frvfdpic_gotrel_section (info)->size);
5332 if (frvfdpic_gotrel_section (info)->contents == NULL)
5333 return FALSE;
5334 }
5335
5336 frvfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
5337 if (frvfdpic_gotfixup_section (info)->size == 0)
5338 frvfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
5339 else if (frvfdpic_gotfixup_section (info)->contents == NULL)
5340 {
5341 frvfdpic_gotfixup_section (info)->contents =
5342 (bfd_byte *) bfd_zalloc (dynobj,
5343 frvfdpic_gotfixup_section (info)->size);
5344 if (frvfdpic_gotfixup_section (info)->contents == NULL)
5345 return FALSE;
5346 }
5347
5348 if (frvfdpic_pltrel_section (info))
5349 {
5350 frvfdpic_pltrel_section (info)->size =
5351 gpinfop->g.lzplt / 8
5352 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5353 if (frvfdpic_pltrel_section (info)->size == 0)
5354 frvfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
5355 else if (frvfdpic_pltrel_section (info)->contents == NULL)
5356 {
5357 frvfdpic_pltrel_section (info)->contents =
5358 (bfd_byte *) bfd_zalloc (dynobj,
5359 frvfdpic_pltrel_section (info)->size);
5360 if (frvfdpic_pltrel_section (info)->contents == NULL)
5361 return FALSE;
5362 }
5363 }
5364
5365 /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
5366 such that there's room for the additional instruction needed to
5367 call the resolver. Since _frvfdpic_assign_got_entries didn't
5368 account for them, our block size is 4 bytes smaller than the real
5369 block size. */
5370 if (frvfdpic_plt_section (info))
5371 {
5372 frvfdpic_plt_section (info)->size = gpinfop->g.lzplt
5373 + ((gpinfop->g.lzplt + (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) - 8)
5374 / (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) * 4);
5375 }
5376
5377 /* Reset it, such that _frvfdpic_assign_plt_entries() can use it to
5378 actually assign lazy PLT entries addresses. */
5379 gpinfop->g.lzplt = 0;
5380
5381 /* Save information that we're going to need to generate GOT and PLT
5382 entries. */
5383 frvfdpic_got_initial_offset (info) = -gpinfop->gothilo.tmin;
5384
5385 if (get_elf_backend_data (output_bfd)->want_got_sym)
5386 elf_hash_table (info)->hgot->root.u.def.value
5387 = frvfdpic_got_initial_offset (info);
5388
5389 if (frvfdpic_plt_section (info))
5390 frvfdpic_plt_initial_offset (info) =
5391 frvfdpic_plt_section (info)->size;
5392
5393 /* Allocate a ret statement at plt_initial_offset, to be used by
5394 locally-resolved TLS descriptors. */
5395 if (gpinfop->g.tls_ret_refs)
5396 frvfdpic_plt_section (info)->size += 4;
5397
5398 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_plt_entries,
5399 gpinfop);
5400
5401 /* Allocate the PLT section contents only after
5402 _frvfdpic_assign_plt_entries has a chance to add the size of the
5403 non-lazy PLT entries. */
5404 if (frvfdpic_plt_section (info))
5405 {
5406 if (frvfdpic_plt_section (info)->size == 0)
5407 frvfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
5408 else if (frvfdpic_plt_section (info)->contents == NULL)
5409 {
5410 frvfdpic_plt_section (info)->contents =
5411 (bfd_byte *) bfd_zalloc (dynobj,
5412 frvfdpic_plt_section (info)->size);
5413 if (frvfdpic_plt_section (info)->contents == NULL)
5414 return FALSE;
5415 }
5416 }
5417
5418 return TRUE;
5419 }
5420
5421 /* Set the sizes of the dynamic sections. */
5422
5423 static bfd_boolean
5424 elf32_frvfdpic_size_dynamic_sections (bfd *output_bfd,
5425 struct bfd_link_info *info)
5426 {
5427 bfd *dynobj;
5428 asection *s;
5429 struct _frvfdpic_dynamic_got_plt_info gpinfo;
5430
5431 dynobj = elf_hash_table (info)->dynobj;
5432 BFD_ASSERT (dynobj != NULL);
5433
5434 if (elf_hash_table (info)->dynamic_sections_created)
5435 {
5436 /* Set the contents of the .interp section to the interpreter. */
5437 if (bfd_link_executable (info) && !info->nointerp)
5438 {
5439 s = bfd_get_linker_section (dynobj, ".interp");
5440 BFD_ASSERT (s != NULL);
5441 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5442 s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
5443 }
5444 }
5445
5446 memset (&gpinfo, 0, sizeof (gpinfo));
5447 gpinfo.g.info = info;
5448
5449 for (;;)
5450 {
5451 htab_t relocs = frvfdpic_relocs_info (info);
5452
5453 htab_traverse (relocs, _frvfdpic_resolve_final_relocs_info, &relocs);
5454
5455 if (relocs == frvfdpic_relocs_info (info))
5456 break;
5457 }
5458
5459 htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_count_got_plt_entries,
5460 &gpinfo.g);
5461
5462 /* Allocate space to save the summary information, we're going to
5463 use it if we're doing relaxations. */
5464 frvfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
5465
5466 if (!_frvfdpic_size_got_plt (output_bfd, &gpinfo))
5467 return FALSE;
5468
5469 if (elf_hash_table (info)->dynamic_sections_created)
5470 {
5471 if (frvfdpic_got_section (info)->size)
5472 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0))
5473 return FALSE;
5474
5475 if (frvfdpic_pltrel_section (info)->size)
5476 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
5477 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_REL)
5478 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
5479 return FALSE;
5480
5481 if (frvfdpic_gotrel_section (info)->size)
5482 if (!_bfd_elf_add_dynamic_entry (info, DT_REL, 0)
5483 || !_bfd_elf_add_dynamic_entry (info, DT_RELSZ, 0)
5484 || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
5485 sizeof (Elf32_External_Rel)))
5486 return FALSE;
5487 }
5488
5489 return TRUE;
5490 }
5491
5492 static bfd_boolean
5493 elf32_frvfdpic_always_size_sections (bfd *output_bfd,
5494 struct bfd_link_info *info)
5495 {
5496 if (!bfd_link_relocatable (info)
5497 && !bfd_elf_stack_segment_size (output_bfd, info,
5498 "__stacksize", DEFAULT_STACK_SIZE))
5499 return FALSE;
5500
5501 return TRUE;
5502 }
5503
5504 /* Check whether any of the relocations was optimized away, and
5505 subtract it from the relocation or fixup count. */
5506 static bfd_boolean
5507 _frvfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
5508 struct bfd_link_info *info,
5509
5510 bfd_boolean *changed)
5511 {
5512 Elf_Internal_Shdr *symtab_hdr;
5513 struct elf_link_hash_entry **sym_hashes;
5514 Elf_Internal_Rela *rel, *erel;
5515
5516 if ((sec->flags & SEC_RELOC) == 0
5517 || sec->reloc_count == 0)
5518 return TRUE;
5519
5520 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5521 sym_hashes = elf_sym_hashes (abfd);
5522
5523 rel = elf_section_data (sec)->relocs;
5524
5525 /* Now examine each relocation. */
5526 for (erel = rel + sec->reloc_count; rel < erel; rel++)
5527 {
5528 struct elf_link_hash_entry *h;
5529 unsigned long r_symndx;
5530 struct frvfdpic_relocs_info *picrel;
5531 struct _frvfdpic_dynamic_got_info *dinfo;
5532
5533 if (ELF32_R_TYPE (rel->r_info) != R_FRV_32
5534 && ELF32_R_TYPE (rel->r_info) != R_FRV_FUNCDESC)
5535 continue;
5536
5537 if (_bfd_elf_section_offset (sec->output_section->owner,
5538 info, sec, rel->r_offset)
5539 != (bfd_vma)-1)
5540 continue;
5541
5542 r_symndx = ELF32_R_SYM (rel->r_info);
5543 if (r_symndx < symtab_hdr->sh_info)
5544 h = NULL;
5545 else
5546 {
5547 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5548 while (h->root.type == bfd_link_hash_indirect
5549 || h->root.type == bfd_link_hash_warning)
5550 h = (struct elf_link_hash_entry *)h->root.u.i.link;
5551 }
5552
5553 if (h != NULL)
5554 picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
5555 abfd, h,
5556 rel->r_addend, NO_INSERT);
5557 else
5558 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info (info),
5559 abfd, r_symndx,
5560 rel->r_addend, NO_INSERT);
5561
5562 if (! picrel)
5563 return FALSE;
5564
5565 *changed = TRUE;
5566 dinfo = frvfdpic_dynamic_got_plt_info (info);
5567
5568 _frvfdpic_count_relocs_fixups (picrel, dinfo, TRUE);
5569 if (ELF32_R_TYPE (rel->r_info) == R_FRV_32)
5570 picrel->relocs32--;
5571 else /* we know (ELF32_R_TYPE (rel->r_info) == R_FRV_FUNCDESC) */
5572 picrel->relocsfd--;
5573 _frvfdpic_count_relocs_fixups (picrel, dinfo, FALSE);
5574 }
5575
5576 return TRUE;
5577 }
5578
5579 static bfd_boolean
5580 frvfdpic_elf_discard_info (bfd *ibfd,
5581 struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
5582 struct bfd_link_info *info)
5583 {
5584 bfd_boolean changed = FALSE;
5585 asection *s;
5586 bfd *obfd = NULL;
5587
5588 /* Account for relaxation of .eh_frame section. */
5589 for (s = ibfd->sections; s; s = s->next)
5590 if (s->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
5591 {
5592 if (!_frvfdpic_check_discarded_relocs (ibfd, s, info, &changed))
5593 return FALSE;
5594 obfd = s->output_section->owner;
5595 }
5596
5597 if (changed)
5598 {
5599 struct _frvfdpic_dynamic_got_plt_info gpinfo;
5600
5601 memset (&gpinfo, 0, sizeof (gpinfo));
5602 memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info),
5603 sizeof (gpinfo.g));
5604
5605 /* Clear GOT and PLT assignments. */
5606 htab_traverse (frvfdpic_relocs_info (info),
5607 _frvfdpic_reset_got_plt_entries,
5608 NULL);
5609
5610 if (!_frvfdpic_size_got_plt (obfd, &gpinfo))
5611 return FALSE;
5612 }
5613
5614 return TRUE;
5615 }
5616
5617 /* Look for opportunities to relax TLS relocations. We can assume
5618 we're linking the main executable or a static-tls library, since
5619 otherwise we wouldn't have got here. */
5620
5621 static int
5622 _frvfdpic_relax_got_plt_entries (void **entryp, void *dinfo_)
5623 {
5624 struct frvfdpic_relocs_info *entry = *entryp;
5625 struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
5626
5627 _frvfdpic_relax_tls_entries (entry, dinfo, TRUE);
5628
5629 return 1;
5630 }
5631
5632 static bfd_boolean
5633 elf32_frvfdpic_relax_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
5634 struct bfd_link_info *info, bfd_boolean *again)
5635 {
5636 struct _frvfdpic_dynamic_got_plt_info gpinfo;
5637
5638 if (bfd_link_relocatable (info))
5639 (*info->callbacks->einfo)
5640 (_("%P%F: --relax and -r may not be used together\n"));
5641
5642 /* If we return early, we didn't change anything. */
5643 *again = FALSE;
5644
5645 /* We'll do our thing when requested to relax the GOT section. */
5646 if (sec != frvfdpic_got_section (info))
5647 return TRUE;
5648
5649 /* We can only relax when linking the main executable or a library
5650 that can't be dlopened. */
5651 if (! bfd_link_executable (info) && ! (info->flags & DF_STATIC_TLS))
5652 return TRUE;
5653
5654 /* If there isn't a TLS section for this binary, we can't do
5655 anything about its TLS relocations (it probably doesn't have
5656 any. */
5657 if (elf_hash_table (info)->tls_sec == NULL)
5658 return TRUE;
5659
5660 memset (&gpinfo, 0, sizeof (gpinfo));
5661 memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info), sizeof (gpinfo.g));
5662
5663 /* Now look for opportunities to relax, adjusting the GOT usage
5664 as needed. */
5665 htab_traverse (frvfdpic_relocs_info (info),
5666 _frvfdpic_relax_got_plt_entries,
5667 &gpinfo.g);
5668
5669 /* If we changed anything, reset and re-assign GOT and PLT entries. */
5670 if (memcmp (frvfdpic_dynamic_got_plt_info (info),
5671 &gpinfo.g, sizeof (gpinfo.g)) != 0)
5672 {
5673 /* Clear GOT and PLT assignments. */
5674 htab_traverse (frvfdpic_relocs_info (info),
5675 _frvfdpic_reset_got_plt_entries,
5676 NULL);
5677
5678 /* The owner of the TLS section is the output bfd. There should
5679 be a better way to get to it. */
5680 if (!_frvfdpic_size_got_plt (elf_hash_table (info)->tls_sec->owner,
5681 &gpinfo))
5682 return FALSE;
5683
5684 /* Repeat until we don't make any further changes. We could fail to
5685 introduce changes in a round if, for example, the 12-bit range is
5686 full, but we later release some space by getting rid of TLS
5687 descriptors in it. We have to repeat the whole process because
5688 we might have changed the size of a section processed before this
5689 one. */
5690 *again = TRUE;
5691 }
5692
5693 return TRUE;
5694 }
5695
5696 /* Fill in code and data in dynamic sections. */
5697
5698 static bfd_boolean
5699 elf32_frv_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5700 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5701 {
5702 /* Nothing to be done for non-FDPIC. */
5703 return TRUE;
5704 }
5705
5706 static bfd_boolean
5707 elf32_frvfdpic_finish_dynamic_sections (bfd *output_bfd,
5708 struct bfd_link_info *info)
5709 {
5710 bfd *dynobj;
5711 asection *sdyn;
5712
5713 dynobj = elf_hash_table (info)->dynobj;
5714
5715 if (frvfdpic_dynamic_got_plt_info (info))
5716 {
5717 BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs == 0);
5718 }
5719 if (frvfdpic_got_section (info))
5720 {
5721 BFD_ASSERT (frvfdpic_gotrel_section (info)->size
5722 == (frvfdpic_gotrel_section (info)->reloc_count
5723 * sizeof (Elf32_External_Rel)));
5724
5725 if (frvfdpic_gotfixup_section (info))
5726 {
5727 struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
5728 bfd_vma got_value = hgot->root.u.def.value
5729 + hgot->root.u.def.section->output_section->vma
5730 + hgot->root.u.def.section->output_offset;
5731 struct bfd_link_hash_entry *hend;
5732
5733 _frvfdpic_add_rofixup (output_bfd, frvfdpic_gotfixup_section (info),
5734 got_value, 0);
5735
5736 if (frvfdpic_gotfixup_section (info)->size
5737 != (frvfdpic_gotfixup_section (info)->reloc_count * 4))
5738 {
5739 error:
5740 info->callbacks->einfo
5741 ("LINKER BUG: .rofixup section size mismatch\n");
5742 return FALSE;
5743 }
5744
5745 hend = bfd_link_hash_lookup (info->hash, "__ROFIXUP_END__",
5746 FALSE, FALSE, TRUE);
5747 if (hend
5748 && (hend->type == bfd_link_hash_defined
5749 || hend->type == bfd_link_hash_defweak)
5750 && hend->u.def.section->output_section != NULL)
5751 {
5752 bfd_vma value =
5753 frvfdpic_gotfixup_section (info)->output_section->vma
5754 + frvfdpic_gotfixup_section (info)->output_offset
5755 + frvfdpic_gotfixup_section (info)->size
5756 - hend->u.def.section->output_section->vma
5757 - hend->u.def.section->output_offset;
5758 BFD_ASSERT (hend->u.def.value == value);
5759 if (hend->u.def.value != value)
5760 goto error;
5761 }
5762 }
5763 }
5764 if (frvfdpic_pltrel_section (info))
5765 {
5766 BFD_ASSERT (frvfdpic_pltrel_section (info)->size
5767 == (frvfdpic_pltrel_section (info)->reloc_count
5768 * sizeof (Elf32_External_Rel)));
5769 }
5770
5771
5772 if (elf_hash_table (info)->dynamic_sections_created)
5773 {
5774 Elf32_External_Dyn * dyncon;
5775 Elf32_External_Dyn * dynconend;
5776
5777 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5778
5779 BFD_ASSERT (sdyn != NULL);
5780
5781 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5782 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5783
5784 for (; dyncon < dynconend; dyncon++)
5785 {
5786 Elf_Internal_Dyn dyn;
5787
5788 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5789
5790 switch (dyn.d_tag)
5791 {
5792 default:
5793 break;
5794
5795 case DT_PLTGOT:
5796 dyn.d_un.d_ptr = frvfdpic_got_section (info)->output_section->vma
5797 + frvfdpic_got_section (info)->output_offset
5798 + frvfdpic_got_initial_offset (info);
5799 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5800 break;
5801
5802 case DT_JMPREL:
5803 dyn.d_un.d_ptr = frvfdpic_pltrel_section (info)
5804 ->output_section->vma
5805 + frvfdpic_pltrel_section (info)->output_offset;
5806 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5807 break;
5808
5809 case DT_PLTRELSZ:
5810 dyn.d_un.d_val = frvfdpic_pltrel_section (info)->size;
5811 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5812 break;
5813 }
5814 }
5815 }
5816
5817 return TRUE;
5818 }
5819
5820 /* Adjust a symbol defined by a dynamic object and referenced by a
5821 regular object. */
5822
5823 static bfd_boolean
5824 elf32_frvfdpic_adjust_dynamic_symbol
5825 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5826 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5827 {
5828 bfd * dynobj;
5829
5830 dynobj = elf_hash_table (info)->dynobj;
5831
5832 /* Make sure we know what is going on here. */
5833 BFD_ASSERT (dynobj != NULL
5834 && (h->is_weakalias
5835 || (h->def_dynamic
5836 && h->ref_regular
5837 && !h->def_regular)));
5838
5839 /* If this is a weak symbol, and there is a real definition, the
5840 processor independent code will have arranged for us to see the
5841 real definition first, and we can just use the same value. */
5842 if (h->is_weakalias)
5843 {
5844 struct elf_link_hash_entry *def = weakdef (h);
5845 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
5846 h->root.u.def.section = def->root.u.def.section;
5847 h->root.u.def.value = def->root.u.def.value;
5848 return TRUE;
5849 }
5850
5851 return TRUE;
5852 }
5853
5854 /* Perform any actions needed for dynamic symbols. */
5855
5856 static bfd_boolean
5857 elf32_frvfdpic_finish_dynamic_symbol
5858 (bfd *output_bfd ATTRIBUTE_UNUSED,
5859 struct bfd_link_info *info ATTRIBUTE_UNUSED,
5860 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
5861 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5862 {
5863 return TRUE;
5864 }
5865
5866 /* Decide whether to attempt to turn absptr or lsda encodings in
5867 shared libraries into pcrel within the given input section. */
5868
5869 static bfd_boolean
5870 frvfdpic_elf_use_relative_eh_frame
5871 (bfd *input_bfd ATTRIBUTE_UNUSED,
5872 struct bfd_link_info *info ATTRIBUTE_UNUSED,
5873 asection *eh_frame_section ATTRIBUTE_UNUSED)
5874 {
5875 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
5876 return FALSE;
5877 }
5878
5879 /* Adjust the contents of an eh_frame_hdr section before they're output. */
5880
5881 static bfd_byte
5882 frvfdpic_elf_encode_eh_address (bfd *abfd,
5883 struct bfd_link_info *info,
5884 asection *osec, bfd_vma offset,
5885 asection *loc_sec, bfd_vma loc_offset,
5886 bfd_vma *encoded)
5887 {
5888 struct elf_link_hash_entry *h;
5889
5890 h = elf_hash_table (info)->hgot;
5891 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
5892
5893 if (! h || (_frvfdpic_osec_to_segment (abfd, osec)
5894 == _frvfdpic_osec_to_segment (abfd, loc_sec->output_section)))
5895 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
5896 loc_sec, loc_offset, encoded);
5897
5898 BFD_ASSERT (_frvfdpic_osec_to_segment (abfd, osec)
5899 == (_frvfdpic_osec_to_segment
5900 (abfd, h->root.u.def.section->output_section)));
5901
5902 *encoded = osec->vma + offset
5903 - (h->root.u.def.value
5904 + h->root.u.def.section->output_section->vma
5905 + h->root.u.def.section->output_offset);
5906
5907 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
5908 }
5909
5910 /* Look through the relocs for a section during the first phase.
5911
5912 Besides handling virtual table relocs for gc, we have to deal with
5913 all sorts of PIC-related relocations. We describe below the
5914 general plan on how to handle such relocations, even though we only
5915 collect information at this point, storing them in hash tables for
5916 perusal of later passes.
5917
5918 32 relocations are propagated to the linker output when creating
5919 position-independent output. LO16 and HI16 relocations are not
5920 supposed to be encountered in this case.
5921
5922 LABEL16 should always be resolvable by the linker, since it's only
5923 used by branches.
5924
5925 LABEL24, on the other hand, is used by calls. If it turns out that
5926 the target of a call is a dynamic symbol, a PLT entry must be
5927 created for it, which triggers the creation of a private function
5928 descriptor and, unless lazy binding is disabled, a lazy PLT entry.
5929
5930 GPREL relocations require the referenced symbol to be in the same
5931 segment as _gp, but this can only be checked later.
5932
5933 All GOT, GOTOFF and FUNCDESC relocations require a .got section to
5934 exist. LABEL24 might as well, since it may require a PLT entry,
5935 that will require a got.
5936
5937 Non-FUNCDESC GOT relocations require a GOT entry to be created
5938 regardless of whether the symbol is dynamic. However, since a
5939 global symbol that turns out to not be exported may have the same
5940 address of a non-dynamic symbol, we don't assign GOT entries at
5941 this point, such that we can share them in this case. A relocation
5942 for the GOT entry always has to be created, be it to offset a
5943 private symbol by the section load address, be it to get the symbol
5944 resolved dynamically.
5945
5946 FUNCDESC GOT relocations require a GOT entry to be created, and
5947 handled as if a FUNCDESC relocation was applied to the GOT entry in
5948 an object file.
5949
5950 FUNCDESC relocations referencing a symbol that turns out to NOT be
5951 dynamic cause a private function descriptor to be created. The
5952 FUNCDESC relocation then decays to a 32 relocation that points at
5953 the private descriptor. If the symbol is dynamic, the FUNCDESC
5954 relocation is propagated to the linker output, such that the
5955 dynamic linker creates the canonical descriptor, pointing to the
5956 dynamically-resolved definition of the function.
5957
5958 Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
5959 symbols that are assigned to the same segment as the GOT, but we
5960 can only check this later, after we know the complete set of
5961 symbols defined and/or exported.
5962
5963 FUNCDESC GOTOFF relocations require a function descriptor to be
5964 created and, unless lazy binding is disabled or the symbol is not
5965 dynamic, a lazy PLT entry. Since we can't tell at this point
5966 whether a symbol is going to be dynamic, we have to decide later
5967 whether to create a lazy PLT entry or bind the descriptor directly
5968 to the private function.
5969
5970 FUNCDESC_VALUE relocations are not supposed to be present in object
5971 files, but they may very well be simply propagated to the linker
5972 output, since they have no side effect.
5973
5974
5975 A function descriptor always requires a FUNCDESC_VALUE relocation.
5976 Whether it's in .plt.rel or not depends on whether lazy binding is
5977 enabled and on whether the referenced symbol is dynamic.
5978
5979 The existence of a lazy PLT requires the resolverStub lazy PLT
5980 entry to be present.
5981
5982
5983 As for assignment of GOT, PLT and lazy PLT entries, and private
5984 descriptors, we might do them all sequentially, but we can do
5985 better than that. For example, we can place GOT entries and
5986 private function descriptors referenced using 12-bit operands
5987 closer to the PIC register value, such that these relocations don't
5988 overflow. Those that are only referenced with LO16 relocations
5989 could come next, but we may as well place PLT-required function
5990 descriptors in the 12-bit range to make them shorter. Symbols
5991 referenced with LO16/HI16 may come next, but we may place
5992 additional function descriptors in the 16-bit range if we can
5993 reliably tell that we've already placed entries that are ever
5994 referenced with only LO16. PLT entries are therefore generated as
5995 small as possible, while not introducing relocation overflows in
5996 GOT or FUNCDESC_GOTOFF relocations. Lazy PLT entries could be
5997 generated before or after PLT entries, but not intermingled with
5998 them, such that we can have more lazy PLT entries in range for a
5999 branch to the resolverStub. The resolverStub should be emitted at
6000 the most distant location from the first lazy PLT entry such that
6001 it's still in range for a branch, or closer, if there isn't a need
6002 for so many lazy PLT entries. Additional lazy PLT entries may be
6003 emitted after the resolverStub, as long as branches are still in
6004 range. If the branch goes out of range, longer lazy PLT entries
6005 are emitted.
6006
6007 We could further optimize PLT and lazy PLT entries by giving them
6008 priority in assignment to closer-to-gr17 locations depending on the
6009 number of occurrences of references to them (assuming a function
6010 that's called more often is more important for performance, so its
6011 PLT entry should be faster), or taking hints from the compiler.
6012 Given infinite time and money... :-) */
6013
6014 static bfd_boolean
6015 elf32_frv_check_relocs (bfd *abfd,
6016 struct bfd_link_info *info,
6017 asection *sec,
6018 const Elf_Internal_Rela *relocs)
6019 {
6020 Elf_Internal_Shdr *symtab_hdr;
6021 struct elf_link_hash_entry **sym_hashes;
6022 const Elf_Internal_Rela *rel;
6023 const Elf_Internal_Rela *rel_end;
6024 bfd *dynobj;
6025 struct frvfdpic_relocs_info *picrel;
6026
6027 if (bfd_link_relocatable (info))
6028 return TRUE;
6029
6030 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6031 sym_hashes = elf_sym_hashes (abfd);
6032
6033 dynobj = elf_hash_table (info)->dynobj;
6034 rel_end = relocs + sec->reloc_count;
6035 for (rel = relocs; rel < rel_end; rel++)
6036 {
6037 struct elf_link_hash_entry *h;
6038 unsigned long r_symndx;
6039
6040 r_symndx = ELF32_R_SYM (rel->r_info);
6041 if (r_symndx < symtab_hdr->sh_info)
6042 h = NULL;
6043 else
6044 {
6045 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6046 while (h->root.type == bfd_link_hash_indirect
6047 || h->root.type == bfd_link_hash_warning)
6048 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6049 }
6050
6051 switch (ELF32_R_TYPE (rel->r_info))
6052 {
6053 case R_FRV_GETTLSOFF:
6054 case R_FRV_TLSDESC_VALUE:
6055 case R_FRV_GOTTLSDESC12:
6056 case R_FRV_GOTTLSDESCHI:
6057 case R_FRV_GOTTLSDESCLO:
6058 case R_FRV_GOTTLSOFF12:
6059 case R_FRV_GOTTLSOFFHI:
6060 case R_FRV_GOTTLSOFFLO:
6061 case R_FRV_TLSOFF:
6062 case R_FRV_GOT12:
6063 case R_FRV_GOTHI:
6064 case R_FRV_GOTLO:
6065 case R_FRV_FUNCDESC_GOT12:
6066 case R_FRV_FUNCDESC_GOTHI:
6067 case R_FRV_FUNCDESC_GOTLO:
6068 case R_FRV_GOTOFF12:
6069 case R_FRV_GOTOFFHI:
6070 case R_FRV_GOTOFFLO:
6071 case R_FRV_FUNCDESC_GOTOFF12:
6072 case R_FRV_FUNCDESC_GOTOFFHI:
6073 case R_FRV_FUNCDESC_GOTOFFLO:
6074 case R_FRV_FUNCDESC:
6075 case R_FRV_FUNCDESC_VALUE:
6076 case R_FRV_TLSMOFF12:
6077 case R_FRV_TLSMOFFHI:
6078 case R_FRV_TLSMOFFLO:
6079 case R_FRV_TLSMOFF:
6080 if (! IS_FDPIC (abfd))
6081 goto bad_reloc;
6082 /* Fall through. */
6083 case R_FRV_GPREL12:
6084 case R_FRV_GPRELU12:
6085 case R_FRV_GPRELHI:
6086 case R_FRV_GPRELLO:
6087 case R_FRV_LABEL24:
6088 case R_FRV_32:
6089 if (! dynobj)
6090 {
6091 elf_hash_table (info)->dynobj = dynobj = abfd;
6092 if (! _frv_create_got_section (abfd, info))
6093 return FALSE;
6094 }
6095 if (! IS_FDPIC (abfd))
6096 {
6097 picrel = NULL;
6098 break;
6099 }
6100 if (h != NULL)
6101 {
6102 if (h->dynindx == -1)
6103 switch (ELF_ST_VISIBILITY (h->other))
6104 {
6105 case STV_INTERNAL:
6106 case STV_HIDDEN:
6107 break;
6108 default:
6109 bfd_elf_link_record_dynamic_symbol (info, h);
6110 break;
6111 }
6112 picrel
6113 = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
6114 abfd, h,
6115 rel->r_addend, INSERT);
6116 }
6117 else
6118 picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
6119 (info), abfd, r_symndx,
6120 rel->r_addend, INSERT);
6121 if (! picrel)
6122 return FALSE;
6123 break;
6124
6125 default:
6126 picrel = NULL;
6127 break;
6128 }
6129
6130 switch (ELF32_R_TYPE (rel->r_info))
6131 {
6132 case R_FRV_LABEL24:
6133 if (IS_FDPIC (abfd))
6134 picrel->call = 1;
6135 break;
6136
6137 case R_FRV_FUNCDESC_VALUE:
6138 picrel->relocsfdv++;
6139 if (bfd_section_flags (sec) & SEC_ALLOC)
6140 picrel->relocs32--;
6141 /* Fall through. */
6142
6143 case R_FRV_32:
6144 if (! IS_FDPIC (abfd))
6145 break;
6146
6147 picrel->sym = 1;
6148 if (bfd_section_flags (sec) & SEC_ALLOC)
6149 picrel->relocs32++;
6150 break;
6151
6152 case R_FRV_GOT12:
6153 picrel->got12 = 1;
6154 break;
6155
6156 case R_FRV_GOTHI:
6157 case R_FRV_GOTLO:
6158 picrel->gothilo = 1;
6159 break;
6160
6161 case R_FRV_FUNCDESC_GOT12:
6162 picrel->fdgot12 = 1;
6163 break;
6164
6165 case R_FRV_FUNCDESC_GOTHI:
6166 case R_FRV_FUNCDESC_GOTLO:
6167 picrel->fdgothilo = 1;
6168 break;
6169
6170 case R_FRV_GOTOFF12:
6171 case R_FRV_GOTOFFHI:
6172 case R_FRV_GOTOFFLO:
6173 picrel->gotoff = 1;
6174 break;
6175
6176 case R_FRV_FUNCDESC_GOTOFF12:
6177 picrel->fdgoff12 = 1;
6178 break;
6179
6180 case R_FRV_FUNCDESC_GOTOFFHI:
6181 case R_FRV_FUNCDESC_GOTOFFLO:
6182 picrel->fdgoffhilo = 1;
6183 break;
6184
6185 case R_FRV_FUNCDESC:
6186 picrel->fd = 1;
6187 picrel->relocsfd++;
6188 break;
6189
6190 case R_FRV_GETTLSOFF:
6191 picrel->tlsplt = 1;
6192 break;
6193
6194 case R_FRV_TLSDESC_VALUE:
6195 picrel->relocstlsd++;
6196 goto bad_reloc;
6197
6198 case R_FRV_GOTTLSDESC12:
6199 picrel->tlsdesc12 = 1;
6200 break;
6201
6202 case R_FRV_GOTTLSDESCHI:
6203 case R_FRV_GOTTLSDESCLO:
6204 picrel->tlsdeschilo = 1;
6205 break;
6206
6207 case R_FRV_TLSMOFF12:
6208 case R_FRV_TLSMOFFHI:
6209 case R_FRV_TLSMOFFLO:
6210 case R_FRV_TLSMOFF:
6211 break;
6212
6213 case R_FRV_GOTTLSOFF12:
6214 picrel->tlsoff12 = 1;
6215 info->flags |= DF_STATIC_TLS;
6216 break;
6217
6218 case R_FRV_GOTTLSOFFHI:
6219 case R_FRV_GOTTLSOFFLO:
6220 picrel->tlsoffhilo = 1;
6221 info->flags |= DF_STATIC_TLS;
6222 break;
6223
6224 case R_FRV_TLSOFF:
6225 picrel->relocstlsoff++;
6226 info->flags |= DF_STATIC_TLS;
6227 goto bad_reloc;
6228
6229 /* This relocation describes the C++ object vtable hierarchy.
6230 Reconstruct it for later use during GC. */
6231 case R_FRV_GNU_VTINHERIT:
6232 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6233 return FALSE;
6234 break;
6235
6236 /* This relocation describes which C++ vtable entries are actually
6237 used. Record for later use during GC. */
6238 case R_FRV_GNU_VTENTRY:
6239 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6240 return FALSE;
6241 break;
6242
6243 case R_FRV_LABEL16:
6244 case R_FRV_LO16:
6245 case R_FRV_HI16:
6246 case R_FRV_GPREL12:
6247 case R_FRV_GPRELU12:
6248 case R_FRV_GPREL32:
6249 case R_FRV_GPRELHI:
6250 case R_FRV_GPRELLO:
6251 case R_FRV_TLSDESC_RELAX:
6252 case R_FRV_GETTLSOFF_RELAX:
6253 case R_FRV_TLSOFF_RELAX:
6254 break;
6255
6256 default:
6257 bad_reloc:
6258 /* xgettext:c-format */
6259 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
6260 abfd, (unsigned int) ELF32_R_TYPE (rel->r_info));
6261 return FALSE;
6262 }
6263 }
6264
6265 return TRUE;
6266 }
6267
6268 \f
6269 /* Return the machine subcode from the ELF e_flags header. */
6270
6271 static int
6272 elf32_frv_machine (bfd *abfd)
6273 {
6274 switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
6275 {
6276 default: break;
6277 case EF_FRV_CPU_FR550: return bfd_mach_fr550;
6278 case EF_FRV_CPU_FR500: return bfd_mach_fr500;
6279 case EF_FRV_CPU_FR450: return bfd_mach_fr450;
6280 case EF_FRV_CPU_FR405: return bfd_mach_fr400;
6281 case EF_FRV_CPU_FR400: return bfd_mach_fr400;
6282 case EF_FRV_CPU_FR300: return bfd_mach_fr300;
6283 case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
6284 case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
6285 }
6286
6287 return bfd_mach_frv;
6288 }
6289
6290 /* Set the right machine number for a FRV ELF file. */
6291
6292 static bfd_boolean
6293 elf32_frv_object_p (bfd *abfd)
6294 {
6295 bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
6296 return (((elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC) != 0)
6297 == (IS_FDPIC (abfd)));
6298 }
6299 \f
6300 /* Function to set the ELF flag bits. */
6301
6302 static bfd_boolean
6303 frv_elf_set_private_flags (bfd *abfd, flagword flags)
6304 {
6305 elf_elfheader (abfd)->e_flags = flags;
6306 elf_flags_init (abfd) = TRUE;
6307 return TRUE;
6308 }
6309
6310 /* Return true if the architecture described by elf header flag
6311 EXTENSION is an extension of the architecture described by BASE. */
6312
6313 static bfd_boolean
6314 frv_elf_arch_extension_p (flagword base, flagword extension)
6315 {
6316 if (base == extension)
6317 return TRUE;
6318
6319 /* CPU_GENERIC code can be merged with code for a specific
6320 architecture, in which case the result is marked as being
6321 for the specific architecture. Everything is therefore
6322 an extension of CPU_GENERIC. */
6323 if (base == EF_FRV_CPU_GENERIC)
6324 return TRUE;
6325
6326 if (extension == EF_FRV_CPU_FR450)
6327 if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
6328 return TRUE;
6329
6330 if (extension == EF_FRV_CPU_FR405)
6331 if (base == EF_FRV_CPU_FR400)
6332 return TRUE;
6333
6334 return FALSE;
6335 }
6336
6337 /* Merge backend specific data from an object file to the output
6338 object file when linking. */
6339
6340 static bfd_boolean
6341 frv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6342 {
6343 bfd *obfd = info->output_bfd;
6344 flagword old_flags, old_partial;
6345 flagword new_flags, new_partial;
6346 bfd_boolean error = FALSE;
6347 char new_opt[80];
6348 char old_opt[80];
6349
6350 /* FIXME: What should be checked when linking shared libraries? */
6351 if ((ibfd->flags & DYNAMIC) != 0)
6352 return TRUE;
6353
6354 new_opt[0] = old_opt[0] = '\0';
6355 new_flags = elf_elfheader (ibfd)->e_flags;
6356 old_flags = elf_elfheader (obfd)->e_flags;
6357
6358 if (new_flags & EF_FRV_FDPIC)
6359 new_flags &= ~EF_FRV_PIC;
6360
6361 #ifdef DEBUG
6362 _bfd_error_handler
6363 ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %s",
6364 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
6365 bfd_get_filename (ibfd));
6366 #endif
6367
6368 if (!elf_flags_init (obfd)) /* First call, no flags set. */
6369 {
6370 elf_flags_init (obfd) = TRUE;
6371 old_flags = new_flags;
6372 }
6373
6374 else if (new_flags == old_flags) /* Compatible flags are ok. */
6375 ;
6376
6377 else /* Possibly incompatible flags. */
6378 {
6379 /* Warn if different # of gprs are used. Note, 0 means nothing is
6380 said about the size of gprs. */
6381 new_partial = (new_flags & EF_FRV_GPR_MASK);
6382 old_partial = (old_flags & EF_FRV_GPR_MASK);
6383 if (new_partial == old_partial)
6384 ;
6385
6386 else if (new_partial == 0)
6387 ;
6388
6389 else if (old_partial == 0)
6390 old_flags |= new_partial;
6391
6392 else
6393 {
6394 switch (new_partial)
6395 {
6396 default: strcat (new_opt, " -mgpr-??"); break;
6397 case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
6398 case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
6399 }
6400
6401 switch (old_partial)
6402 {
6403 default: strcat (old_opt, " -mgpr-??"); break;
6404 case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
6405 case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
6406 }
6407 }
6408
6409 /* Warn if different # of fprs are used. Note, 0 means nothing is
6410 said about the size of fprs. */
6411 new_partial = (new_flags & EF_FRV_FPR_MASK);
6412 old_partial = (old_flags & EF_FRV_FPR_MASK);
6413 if (new_partial == old_partial)
6414 ;
6415
6416 else if (new_partial == 0)
6417 ;
6418
6419 else if (old_partial == 0)
6420 old_flags |= new_partial;
6421
6422 else
6423 {
6424 switch (new_partial)
6425 {
6426 default: strcat (new_opt, " -mfpr-?"); break;
6427 case EF_FRV_FPR_32: strcat (new_opt, " -mfpr-32"); break;
6428 case EF_FRV_FPR_64: strcat (new_opt, " -mfpr-64"); break;
6429 case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
6430 }
6431
6432 switch (old_partial)
6433 {
6434 default: strcat (old_opt, " -mfpr-?"); break;
6435 case EF_FRV_FPR_32: strcat (old_opt, " -mfpr-32"); break;
6436 case EF_FRV_FPR_64: strcat (old_opt, " -mfpr-64"); break;
6437 case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
6438 }
6439 }
6440
6441 /* Warn if different dword support was used. Note, 0 means nothing is
6442 said about the dword support. */
6443 new_partial = (new_flags & EF_FRV_DWORD_MASK);
6444 old_partial = (old_flags & EF_FRV_DWORD_MASK);
6445 if (new_partial == old_partial)
6446 ;
6447
6448 else if (new_partial == 0)
6449 ;
6450
6451 else if (old_partial == 0)
6452 old_flags |= new_partial;
6453
6454 else
6455 {
6456 switch (new_partial)
6457 {
6458 default: strcat (new_opt, " -mdword-?"); break;
6459 case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword"); break;
6460 case EF_FRV_DWORD_NO: strcat (new_opt, " -mno-dword"); break;
6461 }
6462
6463 switch (old_partial)
6464 {
6465 default: strcat (old_opt, " -mdword-?"); break;
6466 case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword"); break;
6467 case EF_FRV_DWORD_NO: strcat (old_opt, " -mno-dword"); break;
6468 }
6469 }
6470
6471 /* Or in flags that accumulate (ie, if one module uses it, mark that the
6472 feature is used. */
6473 old_flags |= new_flags & (EF_FRV_DOUBLE
6474 | EF_FRV_MEDIA
6475 | EF_FRV_MULADD
6476 | EF_FRV_NON_PIC_RELOCS);
6477
6478 /* If any module was compiled without -G0, clear the G0 bit. */
6479 old_flags = ((old_flags & ~ EF_FRV_G0)
6480 | (old_flags & new_flags & EF_FRV_G0));
6481
6482 /* If any module was compiled without -mnopack, clear the mnopack bit. */
6483 old_flags = ((old_flags & ~ EF_FRV_NOPACK)
6484 | (old_flags & new_flags & EF_FRV_NOPACK));
6485
6486 /* We don't have to do anything if the pic flags are the same, or the new
6487 module(s) were compiled with -mlibrary-pic. */
6488 new_partial = (new_flags & EF_FRV_PIC_FLAGS);
6489 old_partial = (old_flags & EF_FRV_PIC_FLAGS);
6490 if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
6491 ;
6492
6493 /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
6494 flags if any from the new module. */
6495 else if ((old_partial & EF_FRV_LIBPIC) != 0)
6496 old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
6497
6498 /* If we have mixtures of -fpic and -fPIC, or in both bits. */
6499 else if (new_partial != 0 && old_partial != 0)
6500 old_flags |= new_partial;
6501
6502 /* One module was compiled for pic and the other was not, see if we have
6503 had any relocations that are not pic-safe. */
6504 else
6505 {
6506 if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
6507 old_flags |= new_partial;
6508 else
6509 {
6510 old_flags &= ~ EF_FRV_PIC_FLAGS;
6511 #ifndef FRV_NO_PIC_ERROR
6512 error = TRUE;
6513 _bfd_error_handler
6514 /* xgettext:c-format */
6515 (_("%pB: compiled with %s and linked with modules"
6516 " that use non-pic relocations"),
6517 ibfd, (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
6518 #endif
6519 }
6520 }
6521
6522 /* Warn if different cpu is used (allow a specific cpu to override
6523 the generic cpu). */
6524 new_partial = (new_flags & EF_FRV_CPU_MASK);
6525 old_partial = (old_flags & EF_FRV_CPU_MASK);
6526 if (frv_elf_arch_extension_p (new_partial, old_partial))
6527 ;
6528
6529 else if (frv_elf_arch_extension_p (old_partial, new_partial))
6530 old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
6531
6532 else
6533 {
6534 switch (new_partial)
6535 {
6536 default: strcat (new_opt, " -mcpu=?"); break;
6537 case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv"); break;
6538 case EF_FRV_CPU_SIMPLE: strcat (new_opt, " -mcpu=simple"); break;
6539 case EF_FRV_CPU_FR550: strcat (new_opt, " -mcpu=fr550"); break;
6540 case EF_FRV_CPU_FR500: strcat (new_opt, " -mcpu=fr500"); break;
6541 case EF_FRV_CPU_FR450: strcat (new_opt, " -mcpu=fr450"); break;
6542 case EF_FRV_CPU_FR405: strcat (new_opt, " -mcpu=fr405"); break;
6543 case EF_FRV_CPU_FR400: strcat (new_opt, " -mcpu=fr400"); break;
6544 case EF_FRV_CPU_FR300: strcat (new_opt, " -mcpu=fr300"); break;
6545 case EF_FRV_CPU_TOMCAT: strcat (new_opt, " -mcpu=tomcat"); break;
6546 }
6547
6548 switch (old_partial)
6549 {
6550 default: strcat (old_opt, " -mcpu=?"); break;
6551 case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv"); break;
6552 case EF_FRV_CPU_SIMPLE: strcat (old_opt, " -mcpu=simple"); break;
6553 case EF_FRV_CPU_FR550: strcat (old_opt, " -mcpu=fr550"); break;
6554 case EF_FRV_CPU_FR500: strcat (old_opt, " -mcpu=fr500"); break;
6555 case EF_FRV_CPU_FR450: strcat (old_opt, " -mcpu=fr450"); break;
6556 case EF_FRV_CPU_FR405: strcat (old_opt, " -mcpu=fr405"); break;
6557 case EF_FRV_CPU_FR400: strcat (old_opt, " -mcpu=fr400"); break;
6558 case EF_FRV_CPU_FR300: strcat (old_opt, " -mcpu=fr300"); break;
6559 case EF_FRV_CPU_TOMCAT: strcat (old_opt, " -mcpu=tomcat"); break;
6560 }
6561 }
6562
6563 /* Print out any mismatches from above. */
6564 if (new_opt[0])
6565 {
6566 error = TRUE;
6567 _bfd_error_handler
6568 /* xgettext:c-format */
6569 (_("%pB: compiled with %s and linked with modules compiled with %s"),
6570 ibfd, new_opt, old_opt);
6571 }
6572
6573 /* Warn about any other mismatches */
6574 new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
6575 old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
6576 if (new_partial != old_partial)
6577 {
6578 old_flags |= new_partial;
6579 error = TRUE;
6580 _bfd_error_handler
6581 /* xgettext:c-format */
6582 (_("%pB: uses different unknown e_flags (%#x) fields"
6583 " than previous modules (%#x)"),
6584 ibfd, new_partial, old_partial);
6585 }
6586 }
6587
6588 /* If the cpu is -mcpu=simple, then set the -mnopack bit. */
6589 if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
6590 old_flags |= EF_FRV_NOPACK;
6591
6592 /* Update the old flags now with changes made above. */
6593 old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
6594 elf_elfheader (obfd)->e_flags = old_flags;
6595 if (old_partial != (old_flags & EF_FRV_CPU_MASK))
6596 bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
6597
6598 if (((new_flags & EF_FRV_FDPIC) == 0)
6599 != (! IS_FDPIC (ibfd)))
6600 {
6601 error = TRUE;
6602 if (IS_FDPIC (obfd))
6603 _bfd_error_handler
6604 (_("%pB: cannot link non-fdpic object file into fdpic executable"),
6605 ibfd);
6606 else
6607 _bfd_error_handler
6608 (_("%pB: cannot link fdpic object file into non-fdpic executable"),
6609 ibfd);
6610 }
6611
6612 if (error)
6613 bfd_set_error (bfd_error_bad_value);
6614
6615 return !error;
6616 }
6617
6618 \f
6619 static bfd_boolean
6620 frv_elf_print_private_bfd_data (bfd *abfd, void * ptr)
6621 {
6622 FILE *file = (FILE *) ptr;
6623 flagword flags;
6624
6625 BFD_ASSERT (abfd != NULL && ptr != NULL);
6626
6627 /* Print normal ELF private data. */
6628 _bfd_elf_print_private_bfd_data (abfd, ptr);
6629
6630 flags = elf_elfheader (abfd)->e_flags;
6631 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
6632
6633 switch (flags & EF_FRV_CPU_MASK)
6634 {
6635 default: break;
6636 case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple"); break;
6637 case EF_FRV_CPU_FR550: fprintf (file, " -mcpu=fr550"); break;
6638 case EF_FRV_CPU_FR500: fprintf (file, " -mcpu=fr500"); break;
6639 case EF_FRV_CPU_FR450: fprintf (file, " -mcpu=fr450"); break;
6640 case EF_FRV_CPU_FR405: fprintf (file, " -mcpu=fr405"); break;
6641 case EF_FRV_CPU_FR400: fprintf (file, " -mcpu=fr400"); break;
6642 case EF_FRV_CPU_FR300: fprintf (file, " -mcpu=fr300"); break;
6643 case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat"); break;
6644 }
6645
6646 switch (flags & EF_FRV_GPR_MASK)
6647 {
6648 default: break;
6649 case EF_FRV_GPR_32: fprintf (file, " -mgpr-32"); break;
6650 case EF_FRV_GPR_64: fprintf (file, " -mgpr-64"); break;
6651 }
6652
6653 switch (flags & EF_FRV_FPR_MASK)
6654 {
6655 default: break;
6656 case EF_FRV_FPR_32: fprintf (file, " -mfpr-32"); break;
6657 case EF_FRV_FPR_64: fprintf (file, " -mfpr-64"); break;
6658 case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float"); break;
6659 }
6660
6661 switch (flags & EF_FRV_DWORD_MASK)
6662 {
6663 default: break;
6664 case EF_FRV_DWORD_YES: fprintf (file, " -mdword"); break;
6665 case EF_FRV_DWORD_NO: fprintf (file, " -mno-dword"); break;
6666 }
6667
6668 if (flags & EF_FRV_DOUBLE)
6669 fprintf (file, " -mdouble");
6670
6671 if (flags & EF_FRV_MEDIA)
6672 fprintf (file, " -mmedia");
6673
6674 if (flags & EF_FRV_MULADD)
6675 fprintf (file, " -mmuladd");
6676
6677 if (flags & EF_FRV_PIC)
6678 fprintf (file, " -fpic");
6679
6680 if (flags & EF_FRV_BIGPIC)
6681 fprintf (file, " -fPIC");
6682
6683 if (flags & EF_FRV_LIBPIC)
6684 fprintf (file, " -mlibrary-pic");
6685
6686 if (flags & EF_FRV_FDPIC)
6687 fprintf (file, " -mfdpic");
6688
6689 if (flags & EF_FRV_NON_PIC_RELOCS)
6690 fprintf (file, " non-pic relocations");
6691
6692 if (flags & EF_FRV_G0)
6693 fprintf (file, " -G0");
6694
6695 fputc ('\n', file);
6696 return TRUE;
6697 }
6698
6699 \f
6700 /* Support for core dump NOTE sections. */
6701
6702 static bfd_boolean
6703 elf32_frv_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6704 {
6705 int offset;
6706 unsigned int raw_size;
6707
6708 switch (note->descsz)
6709 {
6710 default:
6711 return FALSE;
6712
6713 /* The Linux/FRV elf_prstatus struct is 268 bytes long. The other
6714 hardcoded offsets and sizes listed below (and contained within
6715 this lexical block) refer to fields in the target's elf_prstatus
6716 struct. */
6717 case 268:
6718 /* `pr_cursig' is at offset 12. */
6719 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
6720
6721 /* `pr_pid' is at offset 24. */
6722 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
6723
6724 /* `pr_reg' is at offset 72. */
6725 offset = 72;
6726
6727 /* Most grok_prstatus implementations set `raw_size' to the size
6728 of the pr_reg field. For Linux/FRV, we set `raw_size' to be
6729 the size of `pr_reg' plus the size of `pr_exec_fdpic_loadmap'
6730 and `pr_interp_fdpic_loadmap', both of which (by design)
6731 immediately follow `pr_reg'. This will allow these fields to
6732 be viewed by GDB as registers.
6733
6734 `pr_reg' is 184 bytes long. `pr_exec_fdpic_loadmap' and
6735 `pr_interp_fdpic_loadmap' are 4 bytes each. */
6736 raw_size = 184 + 4 + 4;
6737
6738 break;
6739 }
6740
6741 /* Make a ".reg/999" section. */
6742 return _bfd_elfcore_make_pseudosection (abfd, ".reg", raw_size,
6743 note->descpos + offset);
6744 }
6745
6746 static bfd_boolean
6747 elf32_frv_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6748 {
6749 switch (note->descsz)
6750 {
6751 default:
6752 return FALSE;
6753
6754 /* The Linux/FRV elf_prpsinfo struct is 124 bytes long. */
6755 case 124:
6756
6757 /* `pr_fname' is found at offset 28 and is 16 bytes long. */
6758 elf_tdata (abfd)->core->program
6759 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
6760
6761 /* `pr_psargs' is found at offset 44 and is 80 bytes long. */
6762 elf_tdata (abfd)->core->command
6763 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
6764 }
6765
6766 /* Note that for some reason, a spurious space is tacked
6767 onto the end of the args in some (at least one anyway)
6768 implementations, so strip it off if it exists. */
6769
6770 {
6771 char *command = elf_tdata (abfd)->core->command;
6772 int n = strlen (command);
6773
6774 if (0 < n && command[n - 1] == ' ')
6775 command[n - 1] = '\0';
6776 }
6777
6778 return TRUE;
6779 }
6780 #define ELF_ARCH bfd_arch_frv
6781 #define ELF_MACHINE_CODE EM_CYGNUS_FRV
6782 #define ELF_MAXPAGESIZE 0x1000
6783
6784 #define TARGET_BIG_SYM frv_elf32_vec
6785 #define TARGET_BIG_NAME "elf32-frv"
6786
6787 #define elf_info_to_howto frv_info_to_howto_rela
6788 #define elf_backend_relocate_section elf32_frv_relocate_section
6789 #define elf_backend_gc_mark_hook elf32_frv_gc_mark_hook
6790 #define elf_backend_check_relocs elf32_frv_check_relocs
6791 #define elf_backend_object_p elf32_frv_object_p
6792 #define elf_backend_add_symbol_hook elf32_frv_add_symbol_hook
6793
6794 #define elf_backend_stack_align 8
6795 #define elf_backend_can_gc_sections 1
6796 #define elf_backend_rela_normal 1
6797
6798 #define bfd_elf32_bfd_reloc_type_lookup frv_reloc_type_lookup
6799 #define bfd_elf32_bfd_reloc_name_lookup frv_reloc_name_lookup
6800 #define bfd_elf32_bfd_set_private_flags frv_elf_set_private_flags
6801 #define bfd_elf32_bfd_merge_private_bfd_data frv_elf_merge_private_bfd_data
6802 #define bfd_elf32_bfd_print_private_bfd_data frv_elf_print_private_bfd_data
6803
6804 #define elf_backend_want_got_sym 1
6805 #define elf_backend_got_header_size 0
6806 #define elf_backend_want_got_plt 0
6807 #define elf_backend_plt_readonly 1
6808 #define elf_backend_want_plt_sym 0
6809 #define elf_backend_plt_header_size 0
6810
6811 #define elf_backend_finish_dynamic_sections \
6812 elf32_frv_finish_dynamic_sections
6813
6814 #define elf_backend_grok_prstatus elf32_frv_grok_prstatus
6815 #define elf_backend_grok_psinfo elf32_frv_grok_psinfo
6816
6817 #define elf_backend_linux_prpsinfo32_ugid16 TRUE
6818
6819 #include "elf32-target.h"
6820
6821 #undef ELF_TARGET_ID
6822 #define ELF_TARGET_ID FRV_ELF_DATA
6823 #undef ELF_MAXPAGESIZE
6824 #define ELF_MAXPAGESIZE 0x4000
6825
6826 #undef TARGET_BIG_SYM
6827 #define TARGET_BIG_SYM frv_elf32_fdpic_vec
6828 #undef TARGET_BIG_NAME
6829 #define TARGET_BIG_NAME "elf32-frvfdpic"
6830 #undef elf32_bed
6831 #define elf32_bed elf32_frvfdpic_bed
6832
6833 #undef elf_info_to_howto_rel
6834 #define elf_info_to_howto_rel frvfdpic_info_to_howto_rel
6835
6836 #undef bfd_elf32_bfd_link_hash_table_create
6837 #define bfd_elf32_bfd_link_hash_table_create \
6838 frvfdpic_elf_link_hash_table_create
6839 #undef elf_backend_always_size_sections
6840 #define elf_backend_always_size_sections \
6841 elf32_frvfdpic_always_size_sections
6842
6843 #undef elf_backend_create_dynamic_sections
6844 #define elf_backend_create_dynamic_sections \
6845 elf32_frvfdpic_create_dynamic_sections
6846 #undef elf_backend_adjust_dynamic_symbol
6847 #define elf_backend_adjust_dynamic_symbol \
6848 elf32_frvfdpic_adjust_dynamic_symbol
6849 #undef elf_backend_size_dynamic_sections
6850 #define elf_backend_size_dynamic_sections \
6851 elf32_frvfdpic_size_dynamic_sections
6852 #undef bfd_elf32_bfd_relax_section
6853 #define bfd_elf32_bfd_relax_section \
6854 elf32_frvfdpic_relax_section
6855 #undef elf_backend_finish_dynamic_symbol
6856 #define elf_backend_finish_dynamic_symbol \
6857 elf32_frvfdpic_finish_dynamic_symbol
6858 #undef elf_backend_finish_dynamic_sections
6859 #define elf_backend_finish_dynamic_sections \
6860 elf32_frvfdpic_finish_dynamic_sections
6861
6862 #undef elf_backend_discard_info
6863 #define elf_backend_discard_info \
6864 frvfdpic_elf_discard_info
6865 #undef elf_backend_can_make_relative_eh_frame
6866 #define elf_backend_can_make_relative_eh_frame \
6867 frvfdpic_elf_use_relative_eh_frame
6868 #undef elf_backend_can_make_lsda_relative_eh_frame
6869 #define elf_backend_can_make_lsda_relative_eh_frame \
6870 frvfdpic_elf_use_relative_eh_frame
6871 #undef elf_backend_encode_eh_address
6872 #define elf_backend_encode_eh_address \
6873 frvfdpic_elf_encode_eh_address
6874
6875 #undef elf_backend_may_use_rel_p
6876 #define elf_backend_may_use_rel_p 1
6877 #undef elf_backend_may_use_rela_p
6878 #define elf_backend_may_use_rela_p 1
6879 /* We use REL for dynamic relocations only. */
6880 #undef elf_backend_default_use_rela_p
6881 #define elf_backend_default_use_rela_p 1
6882
6883 #undef elf_backend_omit_section_dynsym
6884 #define elf_backend_omit_section_dynsym _frvfdpic_link_omit_section_dynsym
6885
6886 #include "elf32-target.h"
This page took 0.192512 seconds and 4 git commands to generate.