Automatic date update in version.in
[binutils-gdb.git] / bfd / elf32-bfin.c
1 /* ADI Blackfin BFD support for 32-bit ELF.
2 Copyright (C) 2005-2022 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/bfin.h"
26 #include "dwarf2.h"
27 #include "hashtab.h"
28 #include "elf32-bfin.h"
29
30 /* FUNCTION : bfin_pltpc_reloc
31 ABSTRACT : TODO : figure out how to handle pltpc relocs. */
32 static bfd_reloc_status_type
33 bfin_pltpc_reloc (
34 bfd *abfd ATTRIBUTE_UNUSED,
35 arelent *reloc_entry ATTRIBUTE_UNUSED,
36 asymbol *symbol ATTRIBUTE_UNUSED,
37 void * data ATTRIBUTE_UNUSED,
38 asection *input_section ATTRIBUTE_UNUSED,
39 bfd *output_bfd ATTRIBUTE_UNUSED,
40 char **error_message ATTRIBUTE_UNUSED)
41 {
42 bfd_reloc_status_type flag = bfd_reloc_ok;
43 return flag;
44 }
45 \f
46
47 static bfd_reloc_status_type
48 bfin_pcrel24_reloc (bfd *abfd,
49 arelent *reloc_entry,
50 asymbol *symbol,
51 void * data,
52 asection *input_section,
53 bfd *output_bfd,
54 char **error_message ATTRIBUTE_UNUSED)
55 {
56 bfd_vma relocation;
57 bfd_size_type addr = reloc_entry->address;
58 bfd_vma output_base = 0;
59 reloc_howto_type *howto = reloc_entry->howto;
60 asection *output_section;
61 bool relocatable = (output_bfd != NULL);
62 bfd_size_type limit = bfd_get_section_limit_octets (abfd, input_section);
63
64 if (addr - 2 > limit || limit - (addr - 2) < 2)
65 return bfd_reloc_outofrange;
66
67 if (bfd_is_und_section (symbol->section)
68 && (symbol->flags & BSF_WEAK) == 0
69 && !relocatable)
70 return bfd_reloc_undefined;
71
72 if (bfd_is_com_section (symbol->section))
73 relocation = 0;
74 else
75 relocation = symbol->value;
76
77 output_section = symbol->section->output_section;
78
79 if (relocatable)
80 output_base = 0;
81 else
82 output_base = output_section->vma;
83
84 if (!relocatable || !strcmp (symbol->name, symbol->section->name))
85 relocation += output_base + symbol->section->output_offset;
86
87 if (!relocatable && !strcmp (symbol->name, symbol->section->name))
88 relocation += reloc_entry->addend;
89
90 relocation -= input_section->output_section->vma + input_section->output_offset;
91 relocation -= reloc_entry->address;
92
93 if (howto->complain_on_overflow != complain_overflow_dont)
94 {
95 bfd_reloc_status_type status;
96 status = bfd_check_overflow (howto->complain_on_overflow,
97 howto->bitsize,
98 howto->rightshift,
99 bfd_arch_bits_per_address(abfd),
100 relocation);
101 if (status != bfd_reloc_ok)
102 return status;
103 }
104
105 /* if rightshift is 1 and the number odd, return error. */
106 if (howto->rightshift && (relocation & 0x01))
107 {
108 _bfd_error_handler (_("relocation should be even number"));
109 return bfd_reloc_overflow;
110 }
111
112 relocation >>= (bfd_vma) howto->rightshift;
113 /* Shift everything up to where it's going to be used. */
114
115 relocation <<= (bfd_vma) howto->bitpos;
116
117 if (relocatable)
118 {
119 reloc_entry->address += input_section->output_offset;
120 reloc_entry->addend += symbol->section->output_offset;
121 }
122
123 {
124 short x;
125
126 /* We are getting reloc_entry->address 2 byte off from
127 the start of instruction. Assuming absolute postion
128 of the reloc data. But, following code had been written assuming
129 reloc address is starting at begining of instruction.
130 To compensate that I have increased the value of
131 relocation by 1 (effectively 2) and used the addr -2 instead of addr. */
132
133 relocation += 1;
134 x = bfd_get_16 (abfd, (bfd_byte *) data + addr - 2);
135 x = (x & 0xff00) | ((relocation >> 16) & 0xff);
136 bfd_put_16 (abfd, x, (unsigned char *) data + addr - 2);
137
138 x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
139 x = relocation & 0xFFFF;
140 bfd_put_16 (abfd, x, (unsigned char *) data + addr );
141 }
142 return bfd_reloc_ok;
143 }
144
145 static bfd_reloc_status_type
146 bfin_imm16_reloc (bfd *abfd,
147 arelent *reloc_entry,
148 asymbol *symbol,
149 void * data,
150 asection *input_section,
151 bfd *output_bfd,
152 char **error_message ATTRIBUTE_UNUSED)
153 {
154 bfd_vma relocation, x;
155 bfd_size_type reloc_addr = reloc_entry->address;
156 bfd_vma output_base = 0;
157 reloc_howto_type *howto = reloc_entry->howto;
158 asection *output_section;
159 bool relocatable = (output_bfd != NULL);
160 bfd_size_type limit = bfd_get_section_limit_octets (abfd, input_section);
161
162 /* Is the address of the relocation really within the section? */
163 if (reloc_addr > limit || limit - reloc_addr < 2)
164 return bfd_reloc_outofrange;
165
166 if (bfd_is_und_section (symbol->section)
167 && (symbol->flags & BSF_WEAK) == 0
168 && !relocatable)
169 return bfd_reloc_undefined;
170
171 output_section = symbol->section->output_section;
172 relocation = symbol->value;
173
174 /* Convert input-section-relative symbol value to absolute. */
175 if (relocatable)
176 output_base = 0;
177 else
178 output_base = output_section->vma;
179
180 if (!relocatable || !strcmp (symbol->name, symbol->section->name))
181 relocation += output_base + symbol->section->output_offset;
182
183 /* Add in supplied addend. */
184 relocation += reloc_entry->addend;
185
186 if (relocatable)
187 {
188 reloc_entry->address += input_section->output_offset;
189 reloc_entry->addend += symbol->section->output_offset;
190 }
191 else
192 {
193 reloc_entry->addend = 0;
194 }
195
196 if (howto->complain_on_overflow != complain_overflow_dont)
197 {
198 bfd_reloc_status_type flag;
199 flag = bfd_check_overflow (howto->complain_on_overflow,
200 howto->bitsize,
201 howto->rightshift,
202 bfd_arch_bits_per_address(abfd),
203 relocation);
204 if (flag != bfd_reloc_ok)
205 return flag;
206 }
207
208 /* Here the variable relocation holds the final address of the
209 symbol we are relocating against, plus any addend. */
210
211 relocation >>= (bfd_vma) howto->rightshift;
212 x = relocation;
213 bfd_put_16 (abfd, x, (unsigned char *) data + reloc_addr);
214 return bfd_reloc_ok;
215 }
216
217
218 static bfd_reloc_status_type
219 bfin_byte4_reloc (bfd *abfd,
220 arelent *reloc_entry,
221 asymbol *symbol,
222 void * data,
223 asection *input_section,
224 bfd *output_bfd,
225 char **error_message ATTRIBUTE_UNUSED)
226 {
227 bfd_vma relocation, x;
228 bfd_size_type addr = reloc_entry->address;
229 bfd_vma output_base = 0;
230 asection *output_section;
231 bool relocatable = (output_bfd != NULL);
232 bfd_size_type limit = bfd_get_section_limit_octets (abfd, input_section);
233
234 /* Is the address of the relocation really within the section? */
235 if (addr > limit || limit - addr < 4)
236 return bfd_reloc_outofrange;
237
238 if (bfd_is_und_section (symbol->section)
239 && (symbol->flags & BSF_WEAK) == 0
240 && !relocatable)
241 return bfd_reloc_undefined;
242
243 output_section = symbol->section->output_section;
244 relocation = symbol->value;
245 /* Convert input-section-relative symbol value to absolute. */
246 if (relocatable)
247 output_base = 0;
248 else
249 output_base = output_section->vma;
250
251 if ((symbol->name
252 && symbol->section->name
253 && !strcmp (symbol->name, symbol->section->name))
254 || !relocatable)
255 {
256 relocation += output_base + symbol->section->output_offset;
257 }
258
259 relocation += reloc_entry->addend;
260
261 if (relocatable)
262 {
263 /* This output will be relocatable ... like ld -r. */
264 reloc_entry->address += input_section->output_offset;
265 reloc_entry->addend += symbol->section->output_offset;
266 }
267 else
268 {
269 reloc_entry->addend = 0;
270 }
271
272 /* Here the variable relocation holds the final address of the
273 symbol we are relocating against, plus any addend. */
274 x = relocation & 0xFFFF0000;
275 x >>=16;
276 bfd_put_16 (abfd, x, (unsigned char *) data + addr + 2);
277
278 x = relocation & 0x0000FFFF;
279 bfd_put_16 (abfd, x, (unsigned char *) data + addr);
280 return bfd_reloc_ok;
281 }
282
283 /* bfin_bfd_reloc handles the blackfin arithmetic relocations.
284 Use this instead of bfd_perform_relocation. */
285 static bfd_reloc_status_type
286 bfin_bfd_reloc (bfd *abfd,
287 arelent *reloc_entry,
288 asymbol *symbol,
289 void * data,
290 asection *input_section,
291 bfd *output_bfd,
292 char **error_message ATTRIBUTE_UNUSED)
293 {
294 bfd_vma relocation;
295 bfd_size_type addr = reloc_entry->address;
296 bfd_vma output_base = 0;
297 reloc_howto_type *howto = reloc_entry->howto;
298 asection *output_section;
299 bool relocatable = (output_bfd != NULL);
300 bfd_size_type limit = bfd_get_section_limit_octets (abfd, input_section);
301
302 /* Is the address of the relocation really within the section? */
303 if (addr > limit || limit - addr < howto->size + 1u)
304 return bfd_reloc_outofrange;
305
306 if (bfd_is_und_section (symbol->section)
307 && (symbol->flags & BSF_WEAK) == 0
308 && !relocatable)
309 return bfd_reloc_undefined;
310
311 /* Get symbol value. (Common symbols are special.) */
312 if (bfd_is_com_section (symbol->section))
313 relocation = 0;
314 else
315 relocation = symbol->value;
316
317 output_section = symbol->section->output_section;
318
319 /* Convert input-section-relative symbol value to absolute. */
320 if (relocatable)
321 output_base = 0;
322 else
323 output_base = output_section->vma;
324
325 if (!relocatable || !strcmp (symbol->name, symbol->section->name))
326 relocation += output_base + symbol->section->output_offset;
327
328 if (!relocatable && !strcmp (symbol->name, symbol->section->name))
329 {
330 /* Add in supplied addend. */
331 relocation += reloc_entry->addend;
332 }
333
334 /* Here the variable relocation holds the final address of the
335 symbol we are relocating against, plus any addend. */
336
337 if (howto->pc_relative)
338 {
339 relocation -= input_section->output_section->vma + input_section->output_offset;
340
341 if (howto->pcrel_offset)
342 relocation -= reloc_entry->address;
343 }
344
345 if (relocatable)
346 {
347 reloc_entry->address += input_section->output_offset;
348 reloc_entry->addend += symbol->section->output_offset;
349 }
350
351 if (howto->complain_on_overflow != complain_overflow_dont)
352 {
353 bfd_reloc_status_type status;
354
355 status = bfd_check_overflow (howto->complain_on_overflow,
356 howto->bitsize,
357 howto->rightshift,
358 bfd_arch_bits_per_address(abfd),
359 relocation);
360 if (status != bfd_reloc_ok)
361 return status;
362 }
363
364 /* If rightshift is 1 and the number odd, return error. */
365 if (howto->rightshift && (relocation & 0x01))
366 {
367 _bfd_error_handler (_("relocation should be even number"));
368 return bfd_reloc_overflow;
369 }
370
371 relocation >>= (bfd_vma) howto->rightshift;
372
373 /* Shift everything up to where it's going to be used. */
374
375 relocation <<= (bfd_vma) howto->bitpos;
376
377 #define DOIT(x) \
378 x = ( (x & ~howto->dst_mask) | (relocation & howto->dst_mask))
379
380 /* handle 8 and 16 bit relocations here. */
381 switch (howto->size)
382 {
383 case 0:
384 {
385 char x = bfd_get_8 (abfd, (char *) data + addr);
386 DOIT (x);
387 bfd_put_8 (abfd, x, (unsigned char *) data + addr);
388 }
389 break;
390
391 case 1:
392 {
393 unsigned short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
394 DOIT (x);
395 bfd_put_16 (abfd, (bfd_vma) x, (unsigned char *) data + addr);
396 }
397 break;
398
399 default:
400 return bfd_reloc_other;
401 }
402
403 return bfd_reloc_ok;
404 }
405
406 /* HOWTO Table for blackfin.
407 Blackfin relocations are fairly complicated.
408 Some of the salient features are
409 a. Even numbered offsets. A number of (not all) relocations are
410 even numbered. This means that the rightmost bit is not stored.
411 Needs to right shift by 1 and check to see if value is not odd
412 b. A relocation can be an expression. An expression takes on
413 a variety of relocations arranged in a stack.
414 As a result, we cannot use the standard generic function as special
415 function. We will have our own, which is very similar to the standard
416 generic function except that it understands how to get the value from
417 the relocation stack. . */
418
419 #define BFIN_RELOC_MIN 0
420 #define BFIN_RELOC_MAX 0x21
421 #define BFIN_GNUEXT_RELOC_MIN 0x40
422 #define BFIN_GNUEXT_RELOC_MAX 0x43
423 #define BFIN_ARELOC_MIN 0xE0
424 #define BFIN_ARELOC_MAX 0xF3
425
426 static reloc_howto_type bfin_howto_table [] =
427 {
428 /* This reloc does nothing. . */
429 HOWTO (R_BFIN_UNUSED0, /* type. */
430 0, /* rightshift. */
431 3, /* size (0 = byte, 1 = short, 2 = long). */
432 0, /* bitsize. */
433 false, /* pc_relative. */
434 0, /* bitpos. */
435 complain_overflow_dont, /* complain_on_overflow. */
436 bfd_elf_generic_reloc, /* special_function. */
437 "R_BFIN_UNUSED0", /* name. */
438 false, /* partial_inplace. */
439 0, /* src_mask. */
440 0, /* dst_mask. */
441 false), /* pcrel_offset. */
442
443 HOWTO (R_BFIN_PCREL5M2, /* type. */
444 1, /* rightshift. */
445 1, /* size (0 = byte, 1 = short, 2 = long).. */
446 4, /* bitsize. */
447 true, /* pc_relative. */
448 0, /* bitpos. */
449 complain_overflow_unsigned, /* complain_on_overflow. */
450 bfin_bfd_reloc, /* special_function. */
451 "R_BFIN_PCREL5M2", /* name. */
452 false, /* partial_inplace. */
453 0, /* src_mask. */
454 0x0000000F, /* dst_mask. */
455 false), /* pcrel_offset. */
456
457 HOWTO (R_BFIN_UNUSED1, /* type. */
458 0, /* rightshift. */
459 3, /* size (0 = byte, 1 = short, 2 = long). */
460 0, /* bitsize. */
461 false, /* pc_relative. */
462 0, /* bitpos. */
463 complain_overflow_dont, /* complain_on_overflow. */
464 bfd_elf_generic_reloc, /* special_function. */
465 "R_BFIN_UNUSED1", /* name. */
466 false, /* partial_inplace. */
467 0, /* src_mask. */
468 0, /* dst_mask. */
469 false), /* pcrel_offset. */
470
471 HOWTO (R_BFIN_PCREL10, /* type. */
472 1, /* rightshift. */
473 1, /* size (0 = byte, 1 = short, 2 = long). */
474 10, /* bitsize. */
475 true, /* pc_relative. */
476 0, /* bitpos. */
477 complain_overflow_signed, /* complain_on_overflow. */
478 bfin_bfd_reloc, /* special_function. */
479 "R_BFIN_PCREL10", /* name. */
480 false, /* partial_inplace. */
481 0, /* src_mask. */
482 0x000003FF, /* dst_mask. */
483 true), /* pcrel_offset. */
484
485 HOWTO (R_BFIN_PCREL12_JUMP, /* type. */
486 1, /* rightshift. */
487 /* the offset is actually 13 bit
488 aligned on a word boundary so
489 only 12 bits have to be used.
490 Right shift the rightmost bit.. */
491 1, /* size (0 = byte, 1 = short, 2 = long). */
492 12, /* bitsize. */
493 true, /* pc_relative. */
494 0, /* bitpos. */
495 complain_overflow_signed, /* complain_on_overflow. */
496 bfin_bfd_reloc, /* special_function. */
497 "R_BFIN_PCREL12_JUMP", /* name. */
498 false, /* partial_inplace. */
499 0, /* src_mask. */
500 0x0FFF, /* dst_mask. */
501 true), /* pcrel_offset. */
502
503 HOWTO (R_BFIN_RIMM16, /* type. */
504 0, /* rightshift. */
505 1, /* size (0 = byte, 1 = short, 2 = long). */
506 16, /* bitsize. */
507 false, /* pc_relative. */
508 0, /* bitpos. */
509 complain_overflow_signed, /* complain_on_overflow. */
510 bfin_imm16_reloc, /* special_function. */
511 "R_BFIN_RIMM16", /* name. */
512 false, /* partial_inplace. */
513 0, /* src_mask. */
514 0x0000FFFF, /* dst_mask. */
515 true), /* pcrel_offset. */
516
517 HOWTO (R_BFIN_LUIMM16, /* type. */
518 0, /* rightshift. */
519 1, /* size (0 = byte, 1 = short, 2 = long). */
520 16, /* bitsize. */
521 false, /* pc_relative. */
522 0, /* bitpos. */
523 complain_overflow_dont, /* complain_on_overflow. */
524 bfin_imm16_reloc, /* special_function. */
525 "R_BFIN_LUIMM16", /* name. */
526 false, /* partial_inplace. */
527 0, /* src_mask. */
528 0x0000FFFF, /* dst_mask. */
529 true), /* pcrel_offset. */
530
531 HOWTO (R_BFIN_HUIMM16, /* type. */
532 16, /* rightshift. */
533 1, /* size (0 = byte, 1 = short, 2 = long). */
534 16, /* bitsize. */
535 false, /* pc_relative. */
536 0, /* bitpos. */
537 complain_overflow_unsigned, /* complain_on_overflow. */
538 bfin_imm16_reloc, /* special_function. */
539 "R_BFIN_HUIMM16", /* name. */
540 false, /* partial_inplace. */
541 0, /* src_mask. */
542 0x0000FFFF, /* dst_mask. */
543 true), /* pcrel_offset. */
544
545 HOWTO (R_BFIN_PCREL12_JUMP_S, /* type. */
546 1, /* rightshift. */
547 1, /* size (0 = byte, 1 = short, 2 = long). */
548 12, /* bitsize. */
549 true, /* pc_relative. */
550 0, /* bitpos. */
551 complain_overflow_signed, /* complain_on_overflow. */
552 bfin_bfd_reloc, /* special_function. */
553 "R_BFIN_PCREL12_JUMP_S", /* name. */
554 false, /* partial_inplace. */
555 0, /* src_mask. */
556 0x00000FFF, /* dst_mask. */
557 true), /* pcrel_offset. */
558
559 HOWTO (R_BFIN_PCREL24_JUMP_X, /* type. */
560 1, /* rightshift. */
561 2, /* size (0 = byte, 1 = short, 2 = long). */
562 24, /* bitsize. */
563 true, /* pc_relative. */
564 0, /* bitpos. */
565 complain_overflow_signed, /* complain_on_overflow. */
566 bfin_pcrel24_reloc, /* special_function. */
567 "R_BFIN_PCREL24_JUMP_X", /* name. */
568 false, /* partial_inplace. */
569 0, /* src_mask. */
570 0x00FFFFFF, /* dst_mask. */
571 true), /* pcrel_offset. */
572
573 HOWTO (R_BFIN_PCREL24, /* type. */
574 1, /* rightshift. */
575 2, /* size (0 = byte, 1 = short, 2 = long). */
576 24, /* bitsize. */
577 true, /* pc_relative. */
578 0, /* bitpos. */
579 complain_overflow_signed, /* complain_on_overflow. */
580 bfin_pcrel24_reloc, /* special_function. */
581 "R_BFIN_PCREL24", /* name. */
582 false, /* partial_inplace. */
583 0, /* src_mask. */
584 0x00FFFFFF, /* dst_mask. */
585 true), /* pcrel_offset. */
586
587 HOWTO (R_BFIN_UNUSEDB, /* type. */
588 0, /* rightshift. */
589 3, /* size (0 = byte, 1 = short, 2 = long). */
590 0, /* bitsize. */
591 false, /* pc_relative. */
592 0, /* bitpos. */
593 complain_overflow_dont, /* complain_on_overflow. */
594 bfd_elf_generic_reloc, /* special_function. */
595 "R_BFIN_UNUSEDB", /* name. */
596 false, /* partial_inplace. */
597 0, /* src_mask. */
598 0, /* dst_mask. */
599 false), /* pcrel_offset. */
600
601 HOWTO (R_BFIN_UNUSEDC, /* type. */
602 0, /* rightshift. */
603 3, /* size (0 = byte, 1 = short, 2 = long). */
604 0, /* bitsize. */
605 false, /* pc_relative. */
606 0, /* bitpos. */
607 complain_overflow_dont, /* complain_on_overflow. */
608 bfd_elf_generic_reloc, /* special_function. */
609 "R_BFIN_UNUSEDC", /* name. */
610 false, /* partial_inplace. */
611 0, /* src_mask. */
612 0, /* dst_mask. */
613 false), /* pcrel_offset. */
614
615 HOWTO (R_BFIN_PCREL24_JUMP_L, /* type. */
616 1, /* rightshift. */
617 2, /* size (0 = byte, 1 = short, 2 = long). */
618 24, /* bitsize. */
619 true, /* pc_relative. */
620 0, /* bitpos. */
621 complain_overflow_signed, /* complain_on_overflow. */
622 bfin_pcrel24_reloc, /* special_function. */
623 "R_BFIN_PCREL24_JUMP_L", /* name. */
624 false, /* partial_inplace. */
625 0, /* src_mask. */
626 0x00FFFFFF, /* dst_mask. */
627 true), /* pcrel_offset. */
628
629 HOWTO (R_BFIN_PCREL24_CALL_X, /* type. */
630 1, /* rightshift. */
631 2, /* size (0 = byte, 1 = short, 2 = long). */
632 24, /* bitsize. */
633 true, /* pc_relative. */
634 0, /* bitpos. */
635 complain_overflow_signed, /* complain_on_overflow. */
636 bfin_pcrel24_reloc, /* special_function. */
637 "R_BFIN_PCREL24_CALL_X", /* name. */
638 false, /* partial_inplace. */
639 0, /* src_mask. */
640 0x00FFFFFF, /* dst_mask. */
641 true), /* pcrel_offset. */
642
643 HOWTO (R_BFIN_VAR_EQ_SYMB, /* type. */
644 0, /* rightshift. */
645 2, /* size (0 = byte, 1 = short, 2 = long). */
646 32, /* bitsize. */
647 false, /* pc_relative. */
648 0, /* bitpos. */
649 complain_overflow_bitfield, /* complain_on_overflow. */
650 bfin_bfd_reloc, /* special_function. */
651 "R_BFIN_VAR_EQ_SYMB", /* name. */
652 false, /* partial_inplace. */
653 0, /* src_mask. */
654 0, /* dst_mask. */
655 false), /* pcrel_offset. */
656
657 HOWTO (R_BFIN_BYTE_DATA, /* type. */
658 0, /* rightshift. */
659 0, /* size (0 = byte, 1 = short, 2 = long). */
660 8, /* bitsize. */
661 false, /* pc_relative. */
662 0, /* bitpos. */
663 complain_overflow_unsigned, /* complain_on_overflow. */
664 bfin_bfd_reloc, /* special_function. */
665 "R_BFIN_BYTE_DATA", /* name. */
666 false, /* partial_inplace. */
667 0, /* src_mask. */
668 0xFF, /* dst_mask. */
669 true), /* pcrel_offset. */
670
671 HOWTO (R_BFIN_BYTE2_DATA, /* type. */
672 0, /* rightshift. */
673 1, /* size (0 = byte, 1 = short, 2 = long). */
674 16, /* bitsize. */
675 false, /* pc_relative. */
676 0, /* bitpos. */
677 complain_overflow_signed, /* complain_on_overflow. */
678 bfin_bfd_reloc, /* special_function. */
679 "R_BFIN_BYTE2_DATA", /* name. */
680 false, /* partial_inplace. */
681 0, /* src_mask. */
682 0xFFFF, /* dst_mask. */
683 true), /* pcrel_offset. */
684
685 HOWTO (R_BFIN_BYTE4_DATA, /* type. */
686 0, /* rightshift. */
687 2, /* size (0 = byte, 1 = short, 2 = long). */
688 32, /* bitsize. */
689 false, /* pc_relative. */
690 0, /* bitpos. */
691 complain_overflow_unsigned, /* complain_on_overflow. */
692 bfin_byte4_reloc, /* special_function. */
693 "R_BFIN_BYTE4_DATA", /* name. */
694 false, /* partial_inplace. */
695 0, /* src_mask. */
696 0xFFFFFFFF, /* dst_mask. */
697 true), /* pcrel_offset. */
698
699 HOWTO (R_BFIN_PCREL11, /* type. */
700 1, /* rightshift. */
701 1, /* size (0 = byte, 1 = short, 2 = long). */
702 10, /* bitsize. */
703 true, /* pc_relative. */
704 0, /* bitpos. */
705 complain_overflow_unsigned, /* complain_on_overflow. */
706 bfin_bfd_reloc, /* special_function. */
707 "R_BFIN_PCREL11", /* name. */
708 false, /* partial_inplace. */
709 0, /* src_mask. */
710 0x000003FF, /* dst_mask. */
711 false), /* pcrel_offset. */
712
713
714 /* A 18-bit signed operand with the GOT offset for the address of
715 the symbol. */
716 HOWTO (R_BFIN_GOT17M4, /* type */
717 2, /* rightshift */
718 1, /* size (0 = byte, 1 = short, 2 = long) */
719 16, /* bitsize */
720 false, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_signed, /* complain_on_overflow */
723 bfd_elf_generic_reloc, /* special_function */
724 "R_BFIN_GOT17M4", /* name */
725 false, /* partial_inplace */
726 0xffff, /* src_mask */
727 0xffff, /* dst_mask */
728 false), /* pcrel_offset */
729
730 /* The upper 16 bits of the GOT offset for the address of the
731 symbol. */
732 HOWTO (R_BFIN_GOTHI, /* type */
733 0, /* rightshift */
734 1, /* size (0 = byte, 1 = short, 2 = long) */
735 16, /* bitsize */
736 false, /* pc_relative */
737 0, /* bitpos */
738 complain_overflow_dont, /* complain_on_overflow */
739 bfd_elf_generic_reloc, /* special_function */
740 "R_BFIN_GOTHI", /* name */
741 false, /* partial_inplace */
742 0xffff, /* src_mask */
743 0xffff, /* dst_mask */
744 false), /* pcrel_offset */
745
746 /* The lower 16 bits of the GOT offset for the address of the
747 symbol. */
748 HOWTO (R_BFIN_GOTLO, /* type */
749 0, /* rightshift */
750 1, /* size (0 = byte, 1 = short, 2 = long) */
751 16, /* bitsize */
752 false, /* pc_relative */
753 0, /* bitpos */
754 complain_overflow_dont, /* complain_on_overflow */
755 bfd_elf_generic_reloc, /* special_function */
756 "R_BFIN_GOTLO", /* name */
757 false, /* partial_inplace */
758 0xffff, /* src_mask */
759 0xffff, /* dst_mask */
760 false), /* pcrel_offset */
761
762 /* The 32-bit address of the canonical descriptor of a function. */
763 HOWTO (R_BFIN_FUNCDESC, /* type */
764 0, /* rightshift */
765 2, /* size (0 = byte, 1 = short, 2 = long) */
766 32, /* bitsize */
767 false, /* pc_relative */
768 0, /* bitpos */
769 complain_overflow_bitfield, /* complain_on_overflow */
770 bfd_elf_generic_reloc, /* special_function */
771 "R_BFIN_FUNCDESC", /* name */
772 false, /* partial_inplace */
773 0xffffffff, /* src_mask */
774 0xffffffff, /* dst_mask */
775 false), /* pcrel_offset */
776
777 /* A 12-bit signed operand with the GOT offset for the address of
778 canonical descriptor of a function. */
779 HOWTO (R_BFIN_FUNCDESC_GOT17M4, /* type */
780 2, /* rightshift */
781 1, /* size (0 = byte, 1 = short, 2 = long) */
782 16, /* bitsize */
783 false, /* pc_relative */
784 0, /* bitpos */
785 complain_overflow_signed, /* complain_on_overflow */
786 bfd_elf_generic_reloc, /* special_function */
787 "R_BFIN_FUNCDESC_GOT17M4", /* name */
788 false, /* partial_inplace */
789 0xffff, /* src_mask */
790 0xffff, /* dst_mask */
791 false), /* pcrel_offset */
792
793 /* The upper 16 bits of the GOT offset for the address of the
794 canonical descriptor of a function. */
795 HOWTO (R_BFIN_FUNCDESC_GOTHI, /* type */
796 0, /* rightshift */
797 1, /* size (0 = byte, 1 = short, 2 = long) */
798 16, /* bitsize */
799 false, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_dont, /* complain_on_overflow */
802 bfd_elf_generic_reloc, /* special_function */
803 "R_BFIN_FUNCDESC_GOTHI", /* name */
804 false, /* partial_inplace */
805 0xffff, /* src_mask */
806 0xffff, /* dst_mask */
807 false), /* pcrel_offset */
808
809 /* The lower 16 bits of the GOT offset for the address of the
810 canonical descriptor of a function. */
811 HOWTO (R_BFIN_FUNCDESC_GOTLO, /* type */
812 0, /* rightshift */
813 1, /* size (0 = byte, 1 = short, 2 = long) */
814 16, /* bitsize */
815 false, /* pc_relative */
816 0, /* bitpos */
817 complain_overflow_dont, /* complain_on_overflow */
818 bfd_elf_generic_reloc, /* special_function */
819 "R_BFIN_FUNCDESC_GOTLO", /* name */
820 false, /* partial_inplace */
821 0xffff, /* src_mask */
822 0xffff, /* dst_mask */
823 false), /* pcrel_offset */
824
825 /* The 32-bit address of the canonical descriptor of a function. */
826 HOWTO (R_BFIN_FUNCDESC_VALUE, /* type */
827 0, /* rightshift */
828 2, /* size (0 = byte, 1 = short, 2 = long) */
829 64, /* bitsize */
830 false, /* pc_relative */
831 0, /* bitpos */
832 complain_overflow_bitfield, /* complain_on_overflow */
833 bfd_elf_generic_reloc, /* special_function */
834 "R_BFIN_FUNCDESC_VALUE", /* name */
835 false, /* partial_inplace */
836 0xffffffff, /* src_mask */
837 0xffffffff, /* dst_mask */
838 false), /* pcrel_offset */
839
840 /* A 12-bit signed operand with the GOT offset for the address of
841 canonical descriptor of a function. */
842 HOWTO (R_BFIN_FUNCDESC_GOTOFF17M4, /* type */
843 2, /* rightshift */
844 1, /* size (0 = byte, 1 = short, 2 = long) */
845 16, /* bitsize */
846 false, /* pc_relative */
847 0, /* bitpos */
848 complain_overflow_signed, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_BFIN_FUNCDESC_GOTOFF17M4", /* name */
851 false, /* partial_inplace */
852 0xffff, /* src_mask */
853 0xffff, /* dst_mask */
854 false), /* pcrel_offset */
855
856 /* The upper 16 bits of the GOT offset for the address of the
857 canonical descriptor of a function. */
858 HOWTO (R_BFIN_FUNCDESC_GOTOFFHI, /* type */
859 0, /* rightshift */
860 1, /* size (0 = byte, 1 = short, 2 = long) */
861 16, /* bitsize */
862 false, /* pc_relative */
863 0, /* bitpos */
864 complain_overflow_dont, /* complain_on_overflow */
865 bfd_elf_generic_reloc, /* special_function */
866 "R_BFIN_FUNCDESC_GOTOFFHI", /* name */
867 false, /* partial_inplace */
868 0xffff, /* src_mask */
869 0xffff, /* dst_mask */
870 false), /* pcrel_offset */
871
872 /* The lower 16 bits of the GOT offset for the address of the
873 canonical descriptor of a function. */
874 HOWTO (R_BFIN_FUNCDESC_GOTOFFLO, /* type */
875 0, /* rightshift */
876 1, /* size (0 = byte, 1 = short, 2 = long) */
877 16, /* bitsize */
878 false, /* pc_relative */
879 0, /* bitpos */
880 complain_overflow_dont, /* complain_on_overflow */
881 bfd_elf_generic_reloc, /* special_function */
882 "R_BFIN_FUNCDESC_GOTOFFLO", /* name */
883 false, /* partial_inplace */
884 0xffff, /* src_mask */
885 0xffff, /* dst_mask */
886 false), /* pcrel_offset */
887
888 /* A 12-bit signed operand with the GOT offset for the address of
889 the symbol. */
890 HOWTO (R_BFIN_GOTOFF17M4, /* type */
891 2, /* rightshift */
892 1, /* size (0 = byte, 1 = short, 2 = long) */
893 16, /* bitsize */
894 false, /* pc_relative */
895 0, /* bitpos */
896 complain_overflow_signed, /* complain_on_overflow */
897 bfd_elf_generic_reloc, /* special_function */
898 "R_BFIN_GOTOFF17M4", /* name */
899 false, /* partial_inplace */
900 0xffff, /* src_mask */
901 0xffff, /* dst_mask */
902 false), /* pcrel_offset */
903
904 /* The upper 16 bits of the GOT offset for the address of the
905 symbol. */
906 HOWTO (R_BFIN_GOTOFFHI, /* type */
907 0, /* rightshift */
908 1, /* size (0 = byte, 1 = short, 2 = long) */
909 16, /* bitsize */
910 false, /* pc_relative */
911 0, /* bitpos */
912 complain_overflow_dont, /* complain_on_overflow */
913 bfd_elf_generic_reloc, /* special_function */
914 "R_BFIN_GOTOFFHI", /* name */
915 false, /* partial_inplace */
916 0xffff, /* src_mask */
917 0xffff, /* dst_mask */
918 false), /* pcrel_offset */
919
920 /* The lower 16 bits of the GOT offset for the address of the
921 symbol. */
922 HOWTO (R_BFIN_GOTOFFLO, /* type */
923 0, /* rightshift */
924 1, /* size (0 = byte, 1 = short, 2 = long) */
925 16, /* bitsize */
926 false, /* pc_relative */
927 0, /* bitpos */
928 complain_overflow_dont, /* complain_on_overflow */
929 bfd_elf_generic_reloc, /* special_function */
930 "R_BFIN_GOTOFFLO", /* name */
931 false, /* partial_inplace */
932 0xffff, /* src_mask */
933 0xffff, /* dst_mask */
934 false), /* pcrel_offset */
935 };
936
937 static reloc_howto_type bfin_gnuext_howto_table [] =
938 {
939 HOWTO (R_BFIN_PLTPC, /* type. */
940 0, /* rightshift. */
941 1, /* size (0 = byte, 1 = short, 2 = long). */
942 16, /* bitsize. */
943 false, /* pc_relative. */
944 0, /* bitpos. */
945 complain_overflow_bitfield, /* complain_on_overflow. */
946 bfin_pltpc_reloc, /* special_function. */
947 "R_BFIN_PLTPC", /* name. */
948 false, /* partial_inplace. */
949 0xffff, /* src_mask. */
950 0xffff, /* dst_mask. */
951 false), /* pcrel_offset. */
952
953 HOWTO (R_BFIN_GOT, /* type. */
954 0, /* rightshift. */
955 1, /* size (0 = byte, 1 = short, 2 = long). */
956 16, /* bitsize. */
957 false, /* pc_relative. */
958 0, /* bitpos. */
959 complain_overflow_bitfield, /* complain_on_overflow. */
960 bfd_elf_generic_reloc, /* special_function. */
961 "R_BFIN_GOT", /* name. */
962 false, /* partial_inplace. */
963 0x7fff, /* src_mask. */
964 0x7fff, /* dst_mask. */
965 false), /* pcrel_offset. */
966
967 /* GNU extension to record C++ vtable hierarchy. */
968 HOWTO (R_BFIN_GNU_VTINHERIT, /* type. */
969 0, /* rightshift. */
970 2, /* size (0 = byte, 1 = short, 2 = long). */
971 0, /* bitsize. */
972 false, /* pc_relative. */
973 0, /* bitpos. */
974 complain_overflow_dont, /* complain_on_overflow. */
975 NULL, /* special_function. */
976 "R_BFIN_GNU_VTINHERIT", /* name. */
977 false, /* partial_inplace. */
978 0, /* src_mask. */
979 0, /* dst_mask. */
980 false), /* pcrel_offset. */
981
982 /* GNU extension to record C++ vtable member usage. */
983 HOWTO (R_BFIN_GNU_VTENTRY, /* type. */
984 0, /* rightshift. */
985 2, /* size (0 = byte, 1 = short, 2 = long). */
986 0, /* bitsize. */
987 false, /* pc_relative. */
988 0, /* bitpos. */
989 complain_overflow_dont, /* complain_on_overflow. */
990 _bfd_elf_rel_vtable_reloc_fn, /* special_function. */
991 "R_BFIN_GNU_VTENTRY", /* name. */
992 false, /* partial_inplace. */
993 0, /* src_mask. */
994 0, /* dst_mask. */
995 false) /* pcrel_offset. */
996 };
997
998 struct bfin_reloc_map
999 {
1000 bfd_reloc_code_real_type bfd_reloc_val;
1001 unsigned int bfin_reloc_val;
1002 };
1003
1004 static const struct bfin_reloc_map bfin_reloc_map [] =
1005 {
1006 { BFD_RELOC_NONE, R_BFIN_UNUSED0 },
1007 { BFD_RELOC_BFIN_5_PCREL, R_BFIN_PCREL5M2 },
1008 { BFD_RELOC_NONE, R_BFIN_UNUSED1 },
1009 { BFD_RELOC_BFIN_10_PCREL, R_BFIN_PCREL10 },
1010 { BFD_RELOC_BFIN_12_PCREL_JUMP, R_BFIN_PCREL12_JUMP },
1011 { BFD_RELOC_BFIN_16_IMM, R_BFIN_RIMM16 },
1012 { BFD_RELOC_BFIN_16_LOW, R_BFIN_LUIMM16 },
1013 { BFD_RELOC_BFIN_16_HIGH, R_BFIN_HUIMM16 },
1014 { BFD_RELOC_BFIN_12_PCREL_JUMP_S, R_BFIN_PCREL12_JUMP_S },
1015 { BFD_RELOC_24_PCREL, R_BFIN_PCREL24 },
1016 { BFD_RELOC_24_PCREL, R_BFIN_PCREL24 },
1017 { BFD_RELOC_BFIN_24_PCREL_JUMP_L, R_BFIN_PCREL24_JUMP_L },
1018 { BFD_RELOC_NONE, R_BFIN_UNUSEDB },
1019 { BFD_RELOC_NONE, R_BFIN_UNUSEDC },
1020 { BFD_RELOC_BFIN_24_PCREL_CALL_X, R_BFIN_PCREL24_CALL_X },
1021 { BFD_RELOC_8, R_BFIN_BYTE_DATA },
1022 { BFD_RELOC_16, R_BFIN_BYTE2_DATA },
1023 { BFD_RELOC_32, R_BFIN_BYTE4_DATA },
1024 { BFD_RELOC_BFIN_11_PCREL, R_BFIN_PCREL11 },
1025 { BFD_RELOC_BFIN_GOT, R_BFIN_GOT },
1026 { BFD_RELOC_BFIN_PLTPC, R_BFIN_PLTPC },
1027
1028 { BFD_RELOC_BFIN_GOT17M4, R_BFIN_GOT17M4 },
1029 { BFD_RELOC_BFIN_GOTHI, R_BFIN_GOTHI },
1030 { BFD_RELOC_BFIN_GOTLO, R_BFIN_GOTLO },
1031 { BFD_RELOC_BFIN_FUNCDESC, R_BFIN_FUNCDESC },
1032 { BFD_RELOC_BFIN_FUNCDESC_GOT17M4, R_BFIN_FUNCDESC_GOT17M4 },
1033 { BFD_RELOC_BFIN_FUNCDESC_GOTHI, R_BFIN_FUNCDESC_GOTHI },
1034 { BFD_RELOC_BFIN_FUNCDESC_GOTLO, R_BFIN_FUNCDESC_GOTLO },
1035 { BFD_RELOC_BFIN_FUNCDESC_VALUE, R_BFIN_FUNCDESC_VALUE },
1036 { BFD_RELOC_BFIN_FUNCDESC_GOTOFF17M4, R_BFIN_FUNCDESC_GOTOFF17M4 },
1037 { BFD_RELOC_BFIN_FUNCDESC_GOTOFFHI, R_BFIN_FUNCDESC_GOTOFFHI },
1038 { BFD_RELOC_BFIN_FUNCDESC_GOTOFFLO, R_BFIN_FUNCDESC_GOTOFFLO },
1039 { BFD_RELOC_BFIN_GOTOFF17M4, R_BFIN_GOTOFF17M4 },
1040 { BFD_RELOC_BFIN_GOTOFFHI, R_BFIN_GOTOFFHI },
1041 { BFD_RELOC_BFIN_GOTOFFLO, R_BFIN_GOTOFFLO },
1042
1043 { BFD_RELOC_VTABLE_INHERIT, R_BFIN_GNU_VTINHERIT },
1044 { BFD_RELOC_VTABLE_ENTRY, R_BFIN_GNU_VTENTRY },
1045 };
1046
1047
1048 static bool
1049 bfin_info_to_howto (bfd *abfd,
1050 arelent *cache_ptr,
1051 Elf_Internal_Rela *dst)
1052 {
1053 unsigned int r_type;
1054
1055 r_type = ELF32_R_TYPE (dst->r_info);
1056
1057 if (r_type <= BFIN_RELOC_MAX)
1058 cache_ptr->howto = &bfin_howto_table [r_type];
1059
1060 else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
1061 cache_ptr->howto = &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
1062
1063 else
1064 {
1065 /* xgettext:c-format */
1066 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1067 abfd, r_type);
1068 bfd_set_error (bfd_error_bad_value);
1069 return false;
1070 }
1071
1072 return true;
1073 }
1074
1075 /* Given a BFD reloc type, return the howto. */
1076 static reloc_howto_type *
1077 bfin_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
1078 bfd_reloc_code_real_type code)
1079 {
1080 unsigned int i;
1081 unsigned int r_type = (unsigned int) -1;
1082
1083 for (i = sizeof (bfin_reloc_map) / sizeof (bfin_reloc_map[0]); i--;)
1084 if (bfin_reloc_map[i].bfd_reloc_val == code)
1085 r_type = bfin_reloc_map[i].bfin_reloc_val;
1086
1087 if (r_type <= BFIN_RELOC_MAX)
1088 return &bfin_howto_table [r_type];
1089
1090 else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
1091 return &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
1092
1093 return (reloc_howto_type *) NULL;
1094 }
1095
1096 static reloc_howto_type *
1097 bfin_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1098 const char *r_name)
1099 {
1100 unsigned int i;
1101
1102 for (i = 0;
1103 i < (sizeof (bfin_howto_table)
1104 / sizeof (bfin_howto_table[0]));
1105 i++)
1106 if (bfin_howto_table[i].name != NULL
1107 && strcasecmp (bfin_howto_table[i].name, r_name) == 0)
1108 return &bfin_howto_table[i];
1109
1110 for (i = 0;
1111 i < (sizeof (bfin_gnuext_howto_table)
1112 / sizeof (bfin_gnuext_howto_table[0]));
1113 i++)
1114 if (bfin_gnuext_howto_table[i].name != NULL
1115 && strcasecmp (bfin_gnuext_howto_table[i].name, r_name) == 0)
1116 return &bfin_gnuext_howto_table[i];
1117
1118 return NULL;
1119 }
1120
1121 /* Given a bfin relocation type, return the howto. */
1122 static reloc_howto_type *
1123 bfin_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
1124 unsigned int r_type)
1125 {
1126 if (r_type <= BFIN_RELOC_MAX)
1127 return &bfin_howto_table [r_type];
1128
1129 else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
1130 return &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
1131
1132 return (reloc_howto_type *) NULL;
1133 }
1134
1135 /* Set by ld emulation if --code-in-l1. */
1136 bool elf32_bfin_code_in_l1 = 0;
1137
1138 /* Set by ld emulation if --data-in-l1. */
1139 bool elf32_bfin_data_in_l1 = 0;
1140
1141 static bool
1142 elf32_bfin_final_write_processing (bfd *abfd)
1143 {
1144 if (elf32_bfin_code_in_l1)
1145 elf_elfheader (abfd)->e_flags |= EF_BFIN_CODE_IN_L1;
1146 if (elf32_bfin_data_in_l1)
1147 elf_elfheader (abfd)->e_flags |= EF_BFIN_DATA_IN_L1;
1148 return _bfd_elf_final_write_processing (abfd);
1149 }
1150
1151 /* Return TRUE if the name is a local label.
1152 bfin local labels begin with L$. */
1153 static bool
1154 bfin_is_local_label_name (bfd *abfd, const char *label)
1155 {
1156 if (label[0] == 'L' && label[1] == '$' )
1157 return true;
1158
1159 return _bfd_elf_is_local_label_name (abfd, label);
1160 }
1161 \f
1162 /* Look through the relocs for a section during the first phase, and
1163 allocate space in the global offset table or procedure linkage
1164 table. */
1165
1166 static bool
1167 bfin_check_relocs (bfd * abfd,
1168 struct bfd_link_info *info,
1169 asection *sec,
1170 const Elf_Internal_Rela *relocs)
1171 {
1172 bfd *dynobj;
1173 Elf_Internal_Shdr *symtab_hdr;
1174 struct elf_link_hash_entry **sym_hashes;
1175 bfd_signed_vma *local_got_refcounts;
1176 const Elf_Internal_Rela *rel;
1177 const Elf_Internal_Rela *rel_end;
1178 asection *sgot;
1179 asection *srelgot;
1180
1181 if (bfd_link_relocatable (info))
1182 return true;
1183
1184 dynobj = elf_hash_table (info)->dynobj;
1185 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1186 sym_hashes = elf_sym_hashes (abfd);
1187 local_got_refcounts = elf_local_got_refcounts (abfd);
1188
1189 sgot = NULL;
1190 srelgot = NULL;
1191
1192 rel_end = relocs + sec->reloc_count;
1193 for (rel = relocs; rel < rel_end; rel++)
1194 {
1195 unsigned long r_symndx;
1196 struct elf_link_hash_entry *h;
1197
1198 r_symndx = ELF32_R_SYM (rel->r_info);
1199 if (r_symndx < symtab_hdr->sh_info)
1200 h = NULL;
1201 else
1202 {
1203 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1204 while (h->root.type == bfd_link_hash_indirect
1205 || h->root.type == bfd_link_hash_warning)
1206 h = (struct elf_link_hash_entry *)h->root.u.i.link;
1207 }
1208
1209 switch (ELF32_R_TYPE (rel->r_info))
1210 {
1211 /* This relocation describes the C++ object vtable hierarchy.
1212 Reconstruct it for later use during GC. */
1213 case R_BFIN_GNU_VTINHERIT:
1214 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1215 return false;
1216 break;
1217
1218 /* This relocation describes which C++ vtable entries
1219 are actually used. Record for later use during GC. */
1220 case R_BFIN_GNU_VTENTRY:
1221 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1222 return false;
1223 break;
1224
1225 case R_BFIN_GOT:
1226 if (h != NULL
1227 && strcmp (h->root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1228 break;
1229 /* Fall through. */
1230
1231 if (dynobj == NULL)
1232 {
1233 /* Create the .got section. */
1234 elf_hash_table (info)->dynobj = dynobj = abfd;
1235 if (!_bfd_elf_create_got_section (dynobj, info))
1236 return false;
1237 }
1238
1239 sgot = elf_hash_table (info)->sgot;
1240 srelgot = elf_hash_table (info)->srelgot;
1241 BFD_ASSERT (sgot != NULL);
1242
1243 if (h != NULL)
1244 {
1245 if (h->got.refcount == 0)
1246 {
1247 /* Make sure this symbol is output as a dynamic symbol. */
1248 if (h->dynindx == -1 && !h->forced_local)
1249 {
1250 if (!bfd_elf_link_record_dynamic_symbol (info, h))
1251 return false;
1252 }
1253
1254 /* Allocate space in the .got section. */
1255 sgot->size += 4;
1256 /* Allocate relocation space. */
1257 srelgot->size += sizeof (Elf32_External_Rela);
1258 }
1259 h->got.refcount++;
1260 }
1261 else
1262 {
1263 /* This is a global offset table entry for a local symbol. */
1264 if (local_got_refcounts == NULL)
1265 {
1266 bfd_size_type size;
1267
1268 size = symtab_hdr->sh_info;
1269 size *= sizeof (bfd_signed_vma);
1270 local_got_refcounts = ((bfd_signed_vma *)
1271 bfd_zalloc (abfd, size));
1272 if (local_got_refcounts == NULL)
1273 return false;
1274 elf_local_got_refcounts (abfd) = local_got_refcounts;
1275 }
1276 if (local_got_refcounts[r_symndx] == 0)
1277 {
1278 sgot->size += 4;
1279 if (bfd_link_pic (info))
1280 {
1281 /* If we are generating a shared object, we need to
1282 output a R_68K_RELATIVE reloc so that the dynamic
1283 linker can adjust this GOT entry. */
1284 srelgot->size += sizeof (Elf32_External_Rela);
1285 }
1286 }
1287 local_got_refcounts[r_symndx]++;
1288 }
1289 break;
1290
1291 default:
1292 break;
1293 }
1294 }
1295
1296 return true;
1297 }
1298
1299 static enum elf_reloc_type_class
1300 elf32_bfin_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
1301 const asection *rel_sec ATTRIBUTE_UNUSED,
1302 const Elf_Internal_Rela * rela)
1303 {
1304 switch ((int) ELF32_R_TYPE (rela->r_info))
1305 {
1306 default:
1307 return reloc_class_normal;
1308 }
1309 }
1310 \f
1311 static bfd_reloc_status_type
1312 bfin_final_link_relocate (Elf_Internal_Rela *rel, reloc_howto_type *howto,
1313 bfd *input_bfd, asection *input_section,
1314 bfd_byte *contents, bfd_vma address,
1315 bfd_vma value, bfd_vma addend)
1316 {
1317 int r_type = ELF32_R_TYPE (rel->r_info);
1318
1319 if (r_type == R_BFIN_PCREL24 || r_type == R_BFIN_PCREL24_JUMP_L)
1320 {
1321 bfd_reloc_status_type r = bfd_reloc_ok;
1322 bfd_vma x;
1323 bfd_size_type limit = bfd_get_section_limit_octets (input_bfd,
1324 input_section);
1325
1326 if (address - 2 > limit || limit - (address - 2) < 4)
1327 return bfd_reloc_outofrange;
1328
1329 value += addend;
1330
1331 /* Perform usual pc-relative correction. */
1332 value -= input_section->output_section->vma + input_section->output_offset;
1333 value -= address;
1334
1335 /* We are getting reloc_entry->address 2 byte off from
1336 the start of instruction. Assuming absolute postion
1337 of the reloc data. But, following code had been written assuming
1338 reloc address is starting at begining of instruction.
1339 To compensate that I have increased the value of
1340 relocation by 1 (effectively 2) and used the addr -2 instead of addr. */
1341
1342 value += 2;
1343 address -= 2;
1344
1345 if ((value & 0xFF000000) != 0
1346 && (value & 0xFF000000) != 0xFF000000)
1347 r = bfd_reloc_overflow;
1348
1349 value >>= 1;
1350
1351 x = bfd_get_16 (input_bfd, contents + address);
1352 x = (x & 0xff00) | ((value >> 16) & 0xff);
1353 bfd_put_16 (input_bfd, x, contents + address);
1354
1355 x = bfd_get_16 (input_bfd, contents + address + 2);
1356 x = value & 0xFFFF;
1357 bfd_put_16 (input_bfd, x, contents + address + 2);
1358 return r;
1359 }
1360
1361 return _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
1362 rel->r_offset, value, addend);
1363
1364 }
1365
1366 static int
1367 bfin_relocate_section (bfd * output_bfd,
1368 struct bfd_link_info *info,
1369 bfd * input_bfd,
1370 asection * input_section,
1371 bfd_byte * contents,
1372 Elf_Internal_Rela * relocs,
1373 Elf_Internal_Sym * local_syms,
1374 asection ** local_sections)
1375 {
1376 bfd *dynobj;
1377 Elf_Internal_Shdr *symtab_hdr;
1378 struct elf_link_hash_entry **sym_hashes;
1379 bfd_vma *local_got_offsets;
1380 asection *sgot;
1381 Elf_Internal_Rela *rel;
1382 Elf_Internal_Rela *relend;
1383 int i = 0;
1384
1385 dynobj = elf_hash_table (info)->dynobj;
1386 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1387 sym_hashes = elf_sym_hashes (input_bfd);
1388 local_got_offsets = elf_local_got_offsets (input_bfd);
1389
1390 sgot = NULL;
1391
1392 rel = relocs;
1393 relend = relocs + input_section->reloc_count;
1394 for (; rel < relend; rel++, i++)
1395 {
1396 int r_type;
1397 reloc_howto_type *howto;
1398 unsigned long r_symndx;
1399 struct elf_link_hash_entry *h;
1400 Elf_Internal_Sym *sym;
1401 asection *sec;
1402 bfd_vma relocation = 0;
1403 bool unresolved_reloc;
1404 bfd_reloc_status_type r;
1405 bfd_vma address;
1406
1407 r_type = ELF32_R_TYPE (rel->r_info);
1408 if (r_type < 0 || r_type >= 243)
1409 {
1410 bfd_set_error (bfd_error_bad_value);
1411 return false;
1412 }
1413
1414 if (r_type == R_BFIN_GNU_VTENTRY
1415 || r_type == R_BFIN_GNU_VTINHERIT)
1416 continue;
1417
1418 howto = bfin_reloc_type_lookup (input_bfd, r_type);
1419 if (howto == NULL)
1420 {
1421 bfd_set_error (bfd_error_bad_value);
1422 return false;
1423 }
1424 r_symndx = ELF32_R_SYM (rel->r_info);
1425
1426 h = NULL;
1427 sym = NULL;
1428 sec = NULL;
1429 unresolved_reloc = false;
1430
1431 if (r_symndx < symtab_hdr->sh_info)
1432 {
1433 sym = local_syms + r_symndx;
1434 sec = local_sections[r_symndx];
1435 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1436 }
1437 else
1438 {
1439 bool warned, ignored;
1440
1441 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1442 r_symndx, symtab_hdr, sym_hashes,
1443 h, sec, relocation,
1444 unresolved_reloc, warned, ignored);
1445 }
1446
1447 if (sec != NULL && discarded_section (sec))
1448 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1449 rel, 1, relend, howto, 0, contents);
1450
1451 if (bfd_link_relocatable (info))
1452 continue;
1453
1454 address = rel->r_offset;
1455
1456 /* Then, process normally. */
1457 switch (r_type)
1458 {
1459 case R_BFIN_GNU_VTINHERIT:
1460 case R_BFIN_GNU_VTENTRY:
1461 return bfd_reloc_ok;
1462
1463 case R_BFIN_GOT:
1464 /* Relocation is to the address of the entry for this symbol
1465 in the global offset table. */
1466 if (h != NULL
1467 && strcmp (h->root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1468 goto do_default;
1469 /* Fall through. */
1470 /* Relocation is the offset of the entry for this symbol in
1471 the global offset table. */
1472
1473 {
1474 bfd_vma off;
1475
1476 if (dynobj == NULL)
1477 {
1478 /* Create the .got section. */
1479 elf_hash_table (info)->dynobj = dynobj = output_bfd;
1480 if (!_bfd_elf_create_got_section (dynobj, info))
1481 return false;
1482 }
1483
1484 sgot = elf_hash_table (info)->sgot;
1485 BFD_ASSERT (sgot != NULL);
1486
1487 if (h != NULL)
1488 {
1489 bool dyn;
1490
1491 off = h->got.offset;
1492 BFD_ASSERT (off != (bfd_vma) - 1);
1493 dyn = elf_hash_table (info)->dynamic_sections_created;
1494
1495 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1496 bfd_link_pic (info),
1497 h)
1498 || (bfd_link_pic (info)
1499 && (info->symbolic
1500 || h->dynindx == -1
1501 || h->forced_local)
1502 && h->def_regular))
1503 {
1504 /* This is actually a static link, or it is a
1505 -Bsymbolic link and the symbol is defined
1506 locally, or the symbol was forced to be local
1507 because of a version file.. We must initialize
1508 this entry in the global offset table. Since
1509 the offset must always be a multiple of 4, we
1510 use the least significant bit to record whether
1511 we have initialized it already.
1512
1513 When doing a dynamic link, we create a .rela.got
1514 relocation entry to initialize the value. This
1515 is done in the finish_dynamic_symbol routine. */
1516 if ((off & 1) != 0)
1517 off &= ~1;
1518 else
1519 {
1520 bfd_put_32 (output_bfd, relocation,
1521 sgot->contents + off);
1522 h->got.offset |= 1;
1523 }
1524 }
1525 else
1526 unresolved_reloc = false;
1527 }
1528 else
1529 {
1530 BFD_ASSERT (local_got_offsets != NULL);
1531 off = local_got_offsets[r_symndx];
1532 BFD_ASSERT (off != (bfd_vma) - 1);
1533
1534 /* The offset must always be a multiple of 4. We use
1535 the least significant bit to record whether we have
1536 already generated the necessary reloc. */
1537 if ((off & 1) != 0)
1538 off &= ~1;
1539 else
1540 {
1541 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1542
1543 if (bfd_link_pic (info))
1544 {
1545 asection *s;
1546 Elf_Internal_Rela outrel;
1547 bfd_byte *loc;
1548
1549 s = elf_hash_table (info)->srelgot;
1550 BFD_ASSERT (s != NULL);
1551
1552 outrel.r_offset = (sgot->output_section->vma
1553 + sgot->output_offset + off);
1554 outrel.r_info =
1555 ELF32_R_INFO (0, R_BFIN_PCREL24);
1556 outrel.r_addend = relocation;
1557 loc = s->contents;
1558 loc +=
1559 s->reloc_count++ * sizeof (Elf32_External_Rela);
1560 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1561 }
1562
1563 local_got_offsets[r_symndx] |= 1;
1564 }
1565 }
1566
1567 relocation = sgot->output_offset + off;
1568 rel->r_addend = 0;
1569 /* bfin : preg = [preg + 17bitdiv4offset] relocation is div by 4. */
1570 relocation /= 4;
1571 }
1572 goto do_default;
1573
1574 default:
1575 do_default:
1576 r = bfin_final_link_relocate (rel, howto, input_bfd, input_section,
1577 contents, address,
1578 relocation, rel->r_addend);
1579
1580 break;
1581 }
1582
1583 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1584 because such sections are not SEC_ALLOC and thus ld.so will
1585 not process them. */
1586 if (unresolved_reloc
1587 && !((input_section->flags & SEC_DEBUGGING) != 0 && h->def_dynamic)
1588 && _bfd_elf_section_offset (output_bfd, info, input_section,
1589 rel->r_offset) != (bfd_vma) -1)
1590 {
1591 _bfd_error_handler
1592 /* xgettext:c-format */
1593 (_("%pB(%pA+%#" PRIx64 "): "
1594 "unresolvable relocation against symbol `%s'"),
1595 input_bfd, input_section, (uint64_t) rel->r_offset,
1596 h->root.root.string);
1597 return false;
1598 }
1599
1600 if (r != bfd_reloc_ok)
1601 {
1602 const char *name;
1603
1604 if (h != NULL)
1605 name = h->root.root.string;
1606 else
1607 {
1608 name = bfd_elf_string_from_elf_section (input_bfd,
1609 symtab_hdr->sh_link,
1610 sym->st_name);
1611 if (name == NULL)
1612 return false;
1613 if (*name == '\0')
1614 name = bfd_section_name (sec);
1615 }
1616
1617 if (r == bfd_reloc_overflow)
1618 (*info->callbacks->reloc_overflow)
1619 (info, (h ? &h->root : NULL), name, howto->name,
1620 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1621 else
1622 {
1623 _bfd_error_handler
1624 /* xgettext:c-format */
1625 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
1626 input_bfd, input_section, (uint64_t) rel->r_offset,
1627 name, (int) r);
1628 return false;
1629 }
1630 }
1631 }
1632
1633 return true;
1634 }
1635
1636 static asection *
1637 bfin_gc_mark_hook (asection * sec,
1638 struct bfd_link_info *info,
1639 Elf_Internal_Rela * rel,
1640 struct elf_link_hash_entry *h,
1641 Elf_Internal_Sym * sym)
1642 {
1643 if (h != NULL)
1644 switch (ELF32_R_TYPE (rel->r_info))
1645 {
1646 case R_BFIN_GNU_VTINHERIT:
1647 case R_BFIN_GNU_VTENTRY:
1648 return NULL;
1649 }
1650
1651 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1652 }
1653 \f
1654 extern const bfd_target bfin_elf32_fdpic_vec;
1655 #define IS_FDPIC(bfd) ((bfd)->xvec == &bfin_elf32_fdpic_vec)
1656
1657 /* An extension of the elf hash table data structure,
1658 containing some additional Blackfin-specific data. */
1659 struct bfinfdpic_elf_link_hash_table
1660 {
1661 struct elf_link_hash_table elf;
1662
1663 /* A pointer to the .rofixup section. */
1664 asection *sgotfixup;
1665 /* GOT base offset. */
1666 bfd_vma got0;
1667 /* Location of the first non-lazy PLT entry, i.e., the number of
1668 bytes taken by lazy PLT entries. */
1669 bfd_vma plt0;
1670 /* A hash table holding information about which symbols were
1671 referenced with which PIC-related relocations. */
1672 struct htab *relocs_info;
1673 /* Summary reloc information collected by
1674 _bfinfdpic_count_got_plt_entries. */
1675 struct _bfinfdpic_dynamic_got_info *g;
1676 };
1677
1678 /* Get the Blackfin ELF linker hash table from a link_info structure. */
1679
1680 #define bfinfdpic_hash_table(p) \
1681 ((is_elf_hash_table ((p)->hash) \
1682 && elf_hash_table_id (elf_hash_table (p)) == BFIN_ELF_DATA) \
1683 ? (struct bfinfdpic_elf_link_hash_table *) (p)->hash : NULL)
1684
1685 #define bfinfdpic_got_section(info) \
1686 (bfinfdpic_hash_table (info)->elf.sgot)
1687 #define bfinfdpic_gotrel_section(info) \
1688 (bfinfdpic_hash_table (info)->elf.srelgot)
1689 #define bfinfdpic_gotfixup_section(info) \
1690 (bfinfdpic_hash_table (info)->sgotfixup)
1691 #define bfinfdpic_plt_section(info) \
1692 (bfinfdpic_hash_table (info)->elf.splt)
1693 #define bfinfdpic_pltrel_section(info) \
1694 (bfinfdpic_hash_table (info)->elf.srelplt)
1695 #define bfinfdpic_relocs_info(info) \
1696 (bfinfdpic_hash_table (info)->relocs_info)
1697 #define bfinfdpic_got_initial_offset(info) \
1698 (bfinfdpic_hash_table (info)->got0)
1699 #define bfinfdpic_plt_initial_offset(info) \
1700 (bfinfdpic_hash_table (info)->plt0)
1701 #define bfinfdpic_dynamic_got_plt_info(info) \
1702 (bfinfdpic_hash_table (info)->g)
1703
1704 /* The name of the dynamic interpreter. This is put in the .interp
1705 section. */
1706
1707 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1708
1709 #define DEFAULT_STACK_SIZE 0x20000
1710
1711 /* This structure is used to collect the number of entries present in
1712 each addressable range of the got. */
1713 struct _bfinfdpic_dynamic_got_info
1714 {
1715 /* Several bits of information about the current link. */
1716 struct bfd_link_info *info;
1717 /* Total size needed for GOT entries within the 18- or 32-bit
1718 ranges. */
1719 bfd_vma got17m4, gothilo;
1720 /* Total size needed for function descriptor entries within the 18-
1721 or 32-bit ranges. */
1722 bfd_vma fd17m4, fdhilo;
1723 /* Total size needed function descriptor entries referenced in PLT
1724 entries, that would be profitable to place in offsets close to
1725 the PIC register. */
1726 bfd_vma fdplt;
1727 /* Total size needed by lazy PLT entries. */
1728 bfd_vma lzplt;
1729 /* Number of relocations carried over from input object files. */
1730 unsigned long relocs;
1731 /* Number of fixups introduced by relocations in input object files. */
1732 unsigned long fixups;
1733 };
1734
1735 /* Create a Blackfin ELF linker hash table. */
1736
1737 static struct bfd_link_hash_table *
1738 bfinfdpic_elf_link_hash_table_create (bfd *abfd)
1739 {
1740 struct bfinfdpic_elf_link_hash_table *ret;
1741 size_t amt = sizeof (struct bfinfdpic_elf_link_hash_table);
1742
1743 ret = bfd_zmalloc (amt);
1744 if (ret == NULL)
1745 return NULL;
1746
1747 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1748 _bfd_elf_link_hash_newfunc,
1749 sizeof (struct elf_link_hash_entry),
1750 BFIN_ELF_DATA))
1751 {
1752 free (ret);
1753 return NULL;
1754 }
1755
1756 return &ret->elf.root;
1757 }
1758
1759 /* Decide whether a reference to a symbol can be resolved locally or
1760 not. If the symbol is protected, we want the local address, but
1761 its function descriptor must be assigned by the dynamic linker. */
1762 #define BFINFDPIC_SYM_LOCAL(INFO, H) \
1763 (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
1764 || ! elf_hash_table (INFO)->dynamic_sections_created)
1765 #define BFINFDPIC_FUNCDESC_LOCAL(INFO, H) \
1766 ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
1767
1768 /* This structure collects information on what kind of GOT, PLT or
1769 function descriptors are required by relocations that reference a
1770 certain symbol. */
1771 struct bfinfdpic_relocs_info
1772 {
1773 /* The index of the symbol, as stored in the relocation r_info, if
1774 we have a local symbol; -1 otherwise. */
1775 long symndx;
1776 union
1777 {
1778 /* The input bfd in which the symbol is defined, if it's a local
1779 symbol. */
1780 bfd *abfd;
1781 /* If symndx == -1, the hash table entry corresponding to a global
1782 symbol (even if it turns out to bind locally, in which case it
1783 should ideally be replaced with section's symndx + addend). */
1784 struct elf_link_hash_entry *h;
1785 } d;
1786 /* The addend of the relocation that references the symbol. */
1787 bfd_vma addend;
1788
1789 /* The fields above are used to identify an entry. The fields below
1790 contain information on how an entry is used and, later on, which
1791 locations it was assigned. */
1792 /* The following 2 fields record whether the symbol+addend above was
1793 ever referenced with a GOT relocation. The 17M4 suffix indicates a
1794 GOT17M4 relocation; hilo is used for GOTLO/GOTHI pairs. */
1795 unsigned got17m4;
1796 unsigned gothilo;
1797 /* Whether a FUNCDESC relocation references symbol+addend. */
1798 unsigned fd;
1799 /* Whether a FUNCDESC_GOT relocation references symbol+addend. */
1800 unsigned fdgot17m4;
1801 unsigned fdgothilo;
1802 /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend. */
1803 unsigned fdgoff17m4;
1804 unsigned fdgoffhilo;
1805 /* Whether symbol+addend is referenced with GOTOFF17M4, GOTOFFLO or
1806 GOTOFFHI relocations. The addend doesn't really matter, since we
1807 envision that this will only be used to check whether the symbol
1808 is mapped to the same segment as the got. */
1809 unsigned gotoff;
1810 /* Whether symbol+addend is referenced by a LABEL24 relocation. */
1811 unsigned call;
1812 /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
1813 relocation. */
1814 unsigned sym;
1815 /* Whether we need a PLT entry for a symbol. Should be implied by
1816 something like:
1817 (call && symndx == -1 && ! BFINFDPIC_SYM_LOCAL (info, d.h)) */
1818 unsigned plt:1;
1819 /* Whether a function descriptor should be created in this link unit
1820 for symbol+addend. Should be implied by something like:
1821 (plt || fdgotoff17m4 || fdgotofflohi
1822 || ((fd || fdgot17m4 || fdgothilo)
1823 && (symndx != -1 || BFINFDPIC_FUNCDESC_LOCAL (info, d.h)))) */
1824 unsigned privfd:1;
1825 /* Whether a lazy PLT entry is needed for this symbol+addend.
1826 Should be implied by something like:
1827 (privfd && symndx == -1 && ! BFINFDPIC_SYM_LOCAL (info, d.h)
1828 && ! (info->flags & DF_BIND_NOW)) */
1829 unsigned lazyplt:1;
1830 /* Whether we've already emitted GOT relocations and PLT entries as
1831 needed for this symbol. */
1832 unsigned done:1;
1833
1834 /* The number of R_BFIN_BYTE4_DATA, R_BFIN_FUNCDESC and R_BFIN_FUNCDESC_VALUE
1835 relocations referencing the symbol. */
1836 unsigned relocs32, relocsfd, relocsfdv;
1837
1838 /* The number of .rofixups entries and dynamic relocations allocated
1839 for this symbol, minus any that might have already been used. */
1840 unsigned fixups, dynrelocs;
1841
1842 /* The offsets of the GOT entries assigned to symbol+addend, to the
1843 function descriptor's address, and to a function descriptor,
1844 respectively. Should be zero if unassigned. The offsets are
1845 counted from the value that will be assigned to the PIC register,
1846 not from the beginning of the .got section. */
1847 bfd_signed_vma got_entry, fdgot_entry, fd_entry;
1848 /* The offsets of the PLT entries assigned to symbol+addend,
1849 non-lazy and lazy, respectively. If unassigned, should be
1850 (bfd_vma)-1. */
1851 bfd_vma plt_entry, lzplt_entry;
1852 };
1853
1854 /* Compute a hash with the key fields of an bfinfdpic_relocs_info entry. */
1855 static hashval_t
1856 bfinfdpic_relocs_info_hash (const void *entry_)
1857 {
1858 const struct bfinfdpic_relocs_info *entry = entry_;
1859
1860 return (entry->symndx == -1
1861 ? (long) entry->d.h->root.root.hash
1862 : entry->symndx + (long) entry->d.abfd->id * 257) + entry->addend;
1863 }
1864
1865 /* Test whether the key fields of two bfinfdpic_relocs_info entries are
1866 identical. */
1867 static int
1868 bfinfdpic_relocs_info_eq (const void *entry1, const void *entry2)
1869 {
1870 const struct bfinfdpic_relocs_info *e1 = entry1;
1871 const struct bfinfdpic_relocs_info *e2 = entry2;
1872
1873 return e1->symndx == e2->symndx && e1->addend == e2->addend
1874 && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
1875 }
1876
1877 /* Find or create an entry in a hash table HT that matches the key
1878 fields of the given ENTRY. If it's not found, memory for a new
1879 entry is allocated in ABFD's obstack. */
1880 static struct bfinfdpic_relocs_info *
1881 bfinfdpic_relocs_info_find (struct htab *ht,
1882 bfd *abfd,
1883 const struct bfinfdpic_relocs_info *entry,
1884 enum insert_option insert)
1885 {
1886 struct bfinfdpic_relocs_info **loc;
1887
1888 if (!ht)
1889 return NULL;
1890
1891 loc = (struct bfinfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
1892
1893 if (! loc)
1894 return NULL;
1895
1896 if (*loc)
1897 return *loc;
1898
1899 *loc = bfd_zalloc (abfd, sizeof (**loc));
1900
1901 if (! *loc)
1902 return *loc;
1903
1904 (*loc)->symndx = entry->symndx;
1905 (*loc)->d = entry->d;
1906 (*loc)->addend = entry->addend;
1907 (*loc)->plt_entry = (bfd_vma)-1;
1908 (*loc)->lzplt_entry = (bfd_vma)-1;
1909
1910 return *loc;
1911 }
1912
1913 /* Obtain the address of the entry in HT associated with H's symbol +
1914 addend, creating a new entry if none existed. ABFD is only used
1915 for memory allocation purposes. */
1916 inline static struct bfinfdpic_relocs_info *
1917 bfinfdpic_relocs_info_for_global (struct htab *ht,
1918 bfd *abfd,
1919 struct elf_link_hash_entry *h,
1920 bfd_vma addend,
1921 enum insert_option insert)
1922 {
1923 struct bfinfdpic_relocs_info entry;
1924
1925 entry.symndx = -1;
1926 entry.d.h = h;
1927 entry.addend = addend;
1928
1929 return bfinfdpic_relocs_info_find (ht, abfd, &entry, insert);
1930 }
1931
1932 /* Obtain the address of the entry in HT associated with the SYMNDXth
1933 local symbol of the input bfd ABFD, plus the addend, creating a new
1934 entry if none existed. */
1935 inline static struct bfinfdpic_relocs_info *
1936 bfinfdpic_relocs_info_for_local (struct htab *ht,
1937 bfd *abfd,
1938 long symndx,
1939 bfd_vma addend,
1940 enum insert_option insert)
1941 {
1942 struct bfinfdpic_relocs_info entry;
1943
1944 entry.symndx = symndx;
1945 entry.d.abfd = abfd;
1946 entry.addend = addend;
1947
1948 return bfinfdpic_relocs_info_find (ht, abfd, &entry, insert);
1949 }
1950
1951 /* Merge fields set by check_relocs() of two entries that end up being
1952 mapped to the same (presumably global) symbol. */
1953
1954 inline static void
1955 bfinfdpic_pic_merge_early_relocs_info (struct bfinfdpic_relocs_info *e2,
1956 struct bfinfdpic_relocs_info const *e1)
1957 {
1958 e2->got17m4 |= e1->got17m4;
1959 e2->gothilo |= e1->gothilo;
1960 e2->fd |= e1->fd;
1961 e2->fdgot17m4 |= e1->fdgot17m4;
1962 e2->fdgothilo |= e1->fdgothilo;
1963 e2->fdgoff17m4 |= e1->fdgoff17m4;
1964 e2->fdgoffhilo |= e1->fdgoffhilo;
1965 e2->gotoff |= e1->gotoff;
1966 e2->call |= e1->call;
1967 e2->sym |= e1->sym;
1968 }
1969
1970 /* Every block of 65535 lazy PLT entries shares a single call to the
1971 resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
1972 32767, counting from 0). All other lazy PLT entries branch to it
1973 in a single instruction. */
1974
1975 #define LZPLT_RESOLVER_EXTRA 10
1976 #define LZPLT_NORMAL_SIZE 6
1977 #define LZPLT_ENTRIES 1362
1978
1979 #define BFINFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) LZPLT_NORMAL_SIZE * LZPLT_ENTRIES + LZPLT_RESOLVER_EXTRA)
1980 #define BFINFDPIC_LZPLT_RESOLV_LOC (LZPLT_NORMAL_SIZE * LZPLT_ENTRIES / 2)
1981
1982 /* Add a dynamic relocation to the SRELOC section. */
1983
1984 inline static bfd_vma
1985 _bfinfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
1986 int reloc_type, long dynindx, bfd_vma addend,
1987 struct bfinfdpic_relocs_info *entry)
1988 {
1989 Elf_Internal_Rela outrel;
1990 bfd_vma reloc_offset;
1991
1992 outrel.r_offset = offset;
1993 outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
1994 outrel.r_addend = addend;
1995
1996 reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
1997 BFD_ASSERT (reloc_offset < sreloc->size);
1998 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1999 sreloc->contents + reloc_offset);
2000 sreloc->reloc_count++;
2001
2002 /* If the entry's index is zero, this relocation was probably to a
2003 linkonce section that got discarded. We reserved a dynamic
2004 relocation, but it was for another entry than the one we got at
2005 the time of emitting the relocation. Unfortunately there's no
2006 simple way for us to catch this situation, since the relocation
2007 is cleared right before calling relocate_section, at which point
2008 we no longer know what the relocation used to point to. */
2009 if (entry->symndx)
2010 {
2011 BFD_ASSERT (entry->dynrelocs > 0);
2012 entry->dynrelocs--;
2013 }
2014
2015 return reloc_offset;
2016 }
2017
2018 /* Add a fixup to the ROFIXUP section. */
2019
2020 static bfd_vma
2021 _bfinfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
2022 struct bfinfdpic_relocs_info *entry)
2023 {
2024 bfd_vma fixup_offset;
2025
2026 if (rofixup->flags & SEC_EXCLUDE)
2027 return -1;
2028
2029 fixup_offset = rofixup->reloc_count * 4;
2030 if (rofixup->contents)
2031 {
2032 BFD_ASSERT (fixup_offset < rofixup->size);
2033 bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
2034 }
2035 rofixup->reloc_count++;
2036
2037 if (entry && entry->symndx)
2038 {
2039 /* See discussion about symndx == 0 in _bfinfdpic_add_dyn_reloc
2040 above. */
2041 BFD_ASSERT (entry->fixups > 0);
2042 entry->fixups--;
2043 }
2044
2045 return fixup_offset;
2046 }
2047
2048 /* Find the segment number in which OSEC, and output section, is
2049 located. */
2050
2051 static unsigned
2052 _bfinfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
2053 {
2054 Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
2055
2056 return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
2057 }
2058
2059 inline static bool
2060 _bfinfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
2061 {
2062 unsigned seg = _bfinfdpic_osec_to_segment (output_bfd, osec);
2063
2064 return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
2065 }
2066
2067 /* Generate relocations for GOT entries, function descriptors, and
2068 code for PLT and lazy PLT entries. */
2069
2070 inline static bool
2071 _bfinfdpic_emit_got_relocs_plt_entries (struct bfinfdpic_relocs_info *entry,
2072 bfd *output_bfd,
2073 struct bfd_link_info *info,
2074 asection *sec,
2075 Elf_Internal_Sym *sym,
2076 bfd_vma addend)
2077 {
2078 bfd_vma fd_lazy_rel_offset = (bfd_vma) -1;
2079 int dynindx = -1;
2080
2081 if (entry->done)
2082 return true;
2083 entry->done = 1;
2084
2085 if (entry->got_entry || entry->fdgot_entry || entry->fd_entry)
2086 {
2087 /* If the symbol is dynamic, consider it for dynamic
2088 relocations, otherwise decay to section + offset. */
2089 if (entry->symndx == -1 && entry->d.h->dynindx != -1)
2090 dynindx = entry->d.h->dynindx;
2091 else
2092 {
2093 if (sec
2094 && sec->output_section
2095 && ! bfd_is_abs_section (sec->output_section)
2096 && ! bfd_is_und_section (sec->output_section))
2097 dynindx = elf_section_data (sec->output_section)->dynindx;
2098 else
2099 dynindx = 0;
2100 }
2101 }
2102
2103 /* Generate relocation for GOT entry pointing to the symbol. */
2104 if (entry->got_entry)
2105 {
2106 int idx = dynindx;
2107 bfd_vma ad = addend;
2108
2109 /* If the symbol is dynamic but binds locally, use
2110 section+offset. */
2111 if (sec && (entry->symndx != -1
2112 || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2113 {
2114 if (entry->symndx == -1)
2115 ad += entry->d.h->root.u.def.value;
2116 else
2117 ad += sym->st_value;
2118 ad += sec->output_offset;
2119 if (sec->output_section && elf_section_data (sec->output_section))
2120 idx = elf_section_data (sec->output_section)->dynindx;
2121 else
2122 idx = 0;
2123 }
2124
2125 /* If we're linking an executable at a fixed address, we can
2126 omit the dynamic relocation as long as the symbol is local to
2127 this module. */
2128 if (bfd_link_pde (info)
2129 && (entry->symndx != -1
2130 || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2131 {
2132 if (sec)
2133 ad += sec->output_section->vma;
2134 if (entry->symndx != -1
2135 || entry->d.h->root.type != bfd_link_hash_undefweak)
2136 _bfinfdpic_add_rofixup (output_bfd,
2137 bfinfdpic_gotfixup_section (info),
2138 bfinfdpic_got_section (info)->output_section
2139 ->vma
2140 + bfinfdpic_got_section (info)->output_offset
2141 + bfinfdpic_got_initial_offset (info)
2142 + entry->got_entry, entry);
2143 }
2144 else
2145 _bfinfdpic_add_dyn_reloc (output_bfd, bfinfdpic_gotrel_section (info),
2146 _bfd_elf_section_offset
2147 (output_bfd, info,
2148 bfinfdpic_got_section (info),
2149 bfinfdpic_got_initial_offset (info)
2150 + entry->got_entry)
2151 + bfinfdpic_got_section (info)
2152 ->output_section->vma
2153 + bfinfdpic_got_section (info)->output_offset,
2154 R_BFIN_BYTE4_DATA, idx, ad, entry);
2155
2156 bfd_put_32 (output_bfd, ad,
2157 bfinfdpic_got_section (info)->contents
2158 + bfinfdpic_got_initial_offset (info)
2159 + entry->got_entry);
2160 }
2161
2162 /* Generate relocation for GOT entry pointing to a canonical
2163 function descriptor. */
2164 if (entry->fdgot_entry)
2165 {
2166 int reloc, idx;
2167 bfd_vma ad = 0;
2168
2169 if (! (entry->symndx == -1
2170 && entry->d.h->root.type == bfd_link_hash_undefweak
2171 && BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2172 {
2173 /* If the symbol is dynamic and there may be dynamic symbol
2174 resolution because we are, or are linked with, a shared
2175 library, emit a FUNCDESC relocation such that the dynamic
2176 linker will allocate the function descriptor. If the
2177 symbol needs a non-local function descriptor but binds
2178 locally (e.g., its visibility is protected, emit a
2179 dynamic relocation decayed to section+offset. */
2180 if (entry->symndx == -1
2181 && ! BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
2182 && BFINFDPIC_SYM_LOCAL (info, entry->d.h)
2183 && !bfd_link_pde (info))
2184 {
2185 reloc = R_BFIN_FUNCDESC;
2186 idx = elf_section_data (entry->d.h->root.u.def.section
2187 ->output_section)->dynindx;
2188 ad = entry->d.h->root.u.def.section->output_offset
2189 + entry->d.h->root.u.def.value;
2190 }
2191 else if (entry->symndx == -1
2192 && ! BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
2193 {
2194 reloc = R_BFIN_FUNCDESC;
2195 idx = dynindx;
2196 ad = addend;
2197 if (ad)
2198 return false;
2199 }
2200 else
2201 {
2202 /* Otherwise, we know we have a private function descriptor,
2203 so reference it directly. */
2204 if (elf_hash_table (info)->dynamic_sections_created)
2205 BFD_ASSERT (entry->privfd);
2206 reloc = R_BFIN_BYTE4_DATA;
2207 idx = elf_section_data (bfinfdpic_got_section (info)
2208 ->output_section)->dynindx;
2209 ad = bfinfdpic_got_section (info)->output_offset
2210 + bfinfdpic_got_initial_offset (info) + entry->fd_entry;
2211 }
2212
2213 /* If there is room for dynamic symbol resolution, emit the
2214 dynamic relocation. However, if we're linking an
2215 executable at a fixed location, we won't have emitted a
2216 dynamic symbol entry for the got section, so idx will be
2217 zero, which means we can and should compute the address
2218 of the private descriptor ourselves. */
2219 if (bfd_link_pde (info)
2220 && (entry->symndx != -1
2221 || BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
2222 {
2223 ad += bfinfdpic_got_section (info)->output_section->vma;
2224 _bfinfdpic_add_rofixup (output_bfd,
2225 bfinfdpic_gotfixup_section (info),
2226 bfinfdpic_got_section (info)
2227 ->output_section->vma
2228 + bfinfdpic_got_section (info)
2229 ->output_offset
2230 + bfinfdpic_got_initial_offset (info)
2231 + entry->fdgot_entry, entry);
2232 }
2233 else
2234 _bfinfdpic_add_dyn_reloc (output_bfd,
2235 bfinfdpic_gotrel_section (info),
2236 _bfd_elf_section_offset
2237 (output_bfd, info,
2238 bfinfdpic_got_section (info),
2239 bfinfdpic_got_initial_offset (info)
2240 + entry->fdgot_entry)
2241 + bfinfdpic_got_section (info)
2242 ->output_section->vma
2243 + bfinfdpic_got_section (info)
2244 ->output_offset,
2245 reloc, idx, ad, entry);
2246 }
2247
2248 bfd_put_32 (output_bfd, ad,
2249 bfinfdpic_got_section (info)->contents
2250 + bfinfdpic_got_initial_offset (info)
2251 + entry->fdgot_entry);
2252 }
2253
2254 /* Generate relocation to fill in a private function descriptor in
2255 the GOT. */
2256 if (entry->fd_entry)
2257 {
2258 int idx = dynindx;
2259 bfd_vma ad = addend;
2260 bfd_vma ofst;
2261 long lowword, highword;
2262
2263 /* If the symbol is dynamic but binds locally, use
2264 section+offset. */
2265 if (sec && (entry->symndx != -1
2266 || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2267 {
2268 if (entry->symndx == -1)
2269 ad += entry->d.h->root.u.def.value;
2270 else
2271 ad += sym->st_value;
2272 ad += sec->output_offset;
2273 if (sec->output_section && elf_section_data (sec->output_section))
2274 idx = elf_section_data (sec->output_section)->dynindx;
2275 else
2276 idx = 0;
2277 }
2278
2279 /* If we're linking an executable at a fixed address, we can
2280 omit the dynamic relocation as long as the symbol is local to
2281 this module. */
2282 if (bfd_link_pde (info)
2283 && (entry->symndx != -1 || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2284 {
2285 if (sec)
2286 ad += sec->output_section->vma;
2287 ofst = 0;
2288 if (entry->symndx != -1
2289 || entry->d.h->root.type != bfd_link_hash_undefweak)
2290 {
2291 _bfinfdpic_add_rofixup (output_bfd,
2292 bfinfdpic_gotfixup_section (info),
2293 bfinfdpic_got_section (info)
2294 ->output_section->vma
2295 + bfinfdpic_got_section (info)
2296 ->output_offset
2297 + bfinfdpic_got_initial_offset (info)
2298 + entry->fd_entry, entry);
2299 _bfinfdpic_add_rofixup (output_bfd,
2300 bfinfdpic_gotfixup_section (info),
2301 bfinfdpic_got_section (info)
2302 ->output_section->vma
2303 + bfinfdpic_got_section (info)
2304 ->output_offset
2305 + bfinfdpic_got_initial_offset (info)
2306 + entry->fd_entry + 4, entry);
2307 }
2308 }
2309 else
2310 {
2311 ofst
2312 = _bfinfdpic_add_dyn_reloc (output_bfd,
2313 entry->lazyplt
2314 ? bfinfdpic_pltrel_section (info)
2315 : bfinfdpic_gotrel_section (info),
2316 _bfd_elf_section_offset
2317 (output_bfd, info,
2318 bfinfdpic_got_section (info),
2319 bfinfdpic_got_initial_offset (info)
2320 + entry->fd_entry)
2321 + bfinfdpic_got_section (info)
2322 ->output_section->vma
2323 + bfinfdpic_got_section (info)
2324 ->output_offset,
2325 R_BFIN_FUNCDESC_VALUE, idx, ad, entry);
2326 }
2327
2328 /* If we've omitted the dynamic relocation, just emit the fixed
2329 addresses of the symbol and of the local GOT base offset. */
2330 if (bfd_link_pde (info)
2331 && sec
2332 && sec->output_section)
2333 {
2334 lowword = ad;
2335 highword = bfinfdpic_got_section (info)->output_section->vma
2336 + bfinfdpic_got_section (info)->output_offset
2337 + bfinfdpic_got_initial_offset (info);
2338 }
2339 else if (entry->lazyplt)
2340 {
2341 if (ad)
2342 return false;
2343
2344 fd_lazy_rel_offset = ofst;
2345
2346 /* A function descriptor used for lazy or local resolving is
2347 initialized such that its high word contains the output
2348 section index in which the PLT entries are located, and
2349 the low word contains the address of the lazy PLT entry
2350 entry point, that must be within the memory region
2351 assigned to that section. */
2352 lowword = entry->lzplt_entry + 4
2353 + bfinfdpic_plt_section (info)->output_offset
2354 + bfinfdpic_plt_section (info)->output_section->vma;
2355 highword = _bfinfdpic_osec_to_segment
2356 (output_bfd, bfinfdpic_plt_section (info)->output_section);
2357 }
2358 else
2359 {
2360 /* A function descriptor for a local function gets the index
2361 of the section. For a non-local function, it's
2362 disregarded. */
2363 lowword = ad;
2364 if (sec == NULL
2365 || (entry->symndx == -1 && entry->d.h->dynindx != -1
2366 && entry->d.h->dynindx == idx))
2367 highword = 0;
2368 else
2369 highword = _bfinfdpic_osec_to_segment
2370 (output_bfd, sec->output_section);
2371 }
2372
2373 bfd_put_32 (output_bfd, lowword,
2374 bfinfdpic_got_section (info)->contents
2375 + bfinfdpic_got_initial_offset (info)
2376 + entry->fd_entry);
2377 bfd_put_32 (output_bfd, highword,
2378 bfinfdpic_got_section (info)->contents
2379 + bfinfdpic_got_initial_offset (info)
2380 + entry->fd_entry + 4);
2381 }
2382
2383 /* Generate code for the PLT entry. */
2384 if (entry->plt_entry != (bfd_vma) -1)
2385 {
2386 bfd_byte *plt_code = bfinfdpic_plt_section (info)->contents
2387 + entry->plt_entry;
2388
2389 BFD_ASSERT (entry->fd_entry);
2390
2391 /* Figure out what kind of PLT entry we need, depending on the
2392 location of the function descriptor within the GOT. */
2393 if (entry->fd_entry >= -(1 << (18 - 1))
2394 && entry->fd_entry + 4 < (1 << (18 - 1)))
2395 {
2396 /* P1 = [P3 + fd_entry]; P3 = [P3 + fd_entry + 4] */
2397 bfd_put_32 (output_bfd,
2398 0xe519 | ((entry->fd_entry << 14) & 0xFFFF0000),
2399 plt_code);
2400 bfd_put_32 (output_bfd,
2401 0xe51b | (((entry->fd_entry + 4) << 14) & 0xFFFF0000),
2402 plt_code + 4);
2403 plt_code += 8;
2404 }
2405 else
2406 {
2407 /* P1.L = fd_entry; P1.H = fd_entry;
2408 P3 = P3 + P1;
2409 P1 = [P3];
2410 P3 = [P3 + 4]; */
2411 bfd_put_32 (output_bfd,
2412 0xe109 | (entry->fd_entry << 16),
2413 plt_code);
2414 bfd_put_32 (output_bfd,
2415 0xe149 | (entry->fd_entry & 0xFFFF0000),
2416 plt_code + 4);
2417 bfd_put_16 (output_bfd, 0x5ad9, plt_code + 8);
2418 bfd_put_16 (output_bfd, 0x9159, plt_code + 10);
2419 bfd_put_16 (output_bfd, 0xac5b, plt_code + 12);
2420 plt_code += 14;
2421 }
2422 /* JUMP (P1) */
2423 bfd_put_16 (output_bfd, 0x0051, plt_code);
2424 }
2425
2426 /* Generate code for the lazy PLT entry. */
2427 if (entry->lzplt_entry != (bfd_vma) -1)
2428 {
2429 bfd_byte *lzplt_code = bfinfdpic_plt_section (info)->contents
2430 + entry->lzplt_entry;
2431 bfd_vma resolverStub_addr;
2432
2433 bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
2434 lzplt_code += 4;
2435
2436 resolverStub_addr = entry->lzplt_entry / BFINFDPIC_LZPLT_BLOCK_SIZE
2437 * BFINFDPIC_LZPLT_BLOCK_SIZE + BFINFDPIC_LZPLT_RESOLV_LOC;
2438 if (resolverStub_addr >= bfinfdpic_plt_initial_offset (info))
2439 resolverStub_addr = bfinfdpic_plt_initial_offset (info) - LZPLT_NORMAL_SIZE - LZPLT_RESOLVER_EXTRA;
2440
2441 if (entry->lzplt_entry == resolverStub_addr)
2442 {
2443 /* This is a lazy PLT entry that includes a resolver call.
2444 P2 = [P3];
2445 R3 = [P3 + 4];
2446 JUMP (P2); */
2447 bfd_put_32 (output_bfd,
2448 0xa05b915a,
2449 lzplt_code);
2450 bfd_put_16 (output_bfd, 0x0052, lzplt_code + 4);
2451 }
2452 else
2453 {
2454 /* JUMP.S resolverStub */
2455 bfd_put_16 (output_bfd,
2456 0x2000
2457 | (((resolverStub_addr - entry->lzplt_entry)
2458 / 2) & (((bfd_vma)1 << 12) - 1)),
2459 lzplt_code);
2460 }
2461 }
2462
2463 return true;
2464 }
2465 \f
2466 /* Relocate an Blackfin ELF section.
2467
2468 The RELOCATE_SECTION function is called by the new ELF backend linker
2469 to handle the relocations for a section.
2470
2471 The relocs are always passed as Rela structures; if the section
2472 actually uses Rel structures, the r_addend field will always be
2473 zero.
2474
2475 This function is responsible for adjusting the section contents as
2476 necessary, and (if using Rela relocs and generating a relocatable
2477 output file) adjusting the reloc addend as necessary.
2478
2479 This function does not have to worry about setting the reloc
2480 address or the reloc symbol index.
2481
2482 LOCAL_SYMS is a pointer to the swapped in local symbols.
2483
2484 LOCAL_SECTIONS is an array giving the section in the input file
2485 corresponding to the st_shndx field of each local symbol.
2486
2487 The global hash table entry for the global symbols can be found
2488 via elf_sym_hashes (input_bfd).
2489
2490 When generating relocatable output, this function must handle
2491 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2492 going to be the section symbol corresponding to the output
2493 section, which means that the addend must be adjusted
2494 accordingly. */
2495
2496 static int
2497 bfinfdpic_relocate_section (bfd * output_bfd,
2498 struct bfd_link_info *info,
2499 bfd * input_bfd,
2500 asection * input_section,
2501 bfd_byte * contents,
2502 Elf_Internal_Rela * relocs,
2503 Elf_Internal_Sym * local_syms,
2504 asection ** local_sections)
2505 {
2506 Elf_Internal_Shdr *symtab_hdr;
2507 struct elf_link_hash_entry **sym_hashes;
2508 Elf_Internal_Rela *rel;
2509 Elf_Internal_Rela *relend;
2510 unsigned isec_segment, got_segment, plt_segment,
2511 check_segment[2];
2512 int silence_segment_error = !bfd_link_pic (info);
2513
2514 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2515 sym_hashes = elf_sym_hashes (input_bfd);
2516 relend = relocs + input_section->reloc_count;
2517
2518 isec_segment = _bfinfdpic_osec_to_segment (output_bfd,
2519 input_section->output_section);
2520 if (IS_FDPIC (output_bfd) && bfinfdpic_got_section (info))
2521 got_segment = _bfinfdpic_osec_to_segment (output_bfd,
2522 bfinfdpic_got_section (info)
2523 ->output_section);
2524 else
2525 got_segment = -1;
2526 if (IS_FDPIC (output_bfd) && elf_hash_table (info)->dynamic_sections_created)
2527 plt_segment = _bfinfdpic_osec_to_segment (output_bfd,
2528 bfinfdpic_plt_section (info)
2529 ->output_section);
2530 else
2531 plt_segment = -1;
2532
2533 for (rel = relocs; rel < relend; rel ++)
2534 {
2535 reloc_howto_type *howto;
2536 unsigned long r_symndx;
2537 Elf_Internal_Sym *sym;
2538 asection *sec;
2539 struct elf_link_hash_entry *h;
2540 bfd_vma relocation;
2541 bfd_reloc_status_type r;
2542 const char * name = NULL;
2543 int r_type;
2544 asection *osec;
2545 struct bfinfdpic_relocs_info *picrel;
2546 bfd_vma orig_addend = rel->r_addend;
2547
2548 r_type = ELF32_R_TYPE (rel->r_info);
2549
2550 if (r_type == R_BFIN_GNU_VTINHERIT
2551 || r_type == R_BFIN_GNU_VTENTRY)
2552 continue;
2553
2554 r_symndx = ELF32_R_SYM (rel->r_info);
2555 howto = bfin_reloc_type_lookup (input_bfd, r_type);
2556 if (howto == NULL)
2557 {
2558 bfd_set_error (bfd_error_bad_value);
2559 return false;
2560 }
2561
2562 h = NULL;
2563 sym = NULL;
2564 sec = NULL;
2565 picrel = NULL;
2566
2567 if (r_symndx < symtab_hdr->sh_info)
2568 {
2569 sym = local_syms + r_symndx;
2570 osec = sec = local_sections [r_symndx];
2571 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2572
2573 name = bfd_elf_string_from_elf_section
2574 (input_bfd, symtab_hdr->sh_link, sym->st_name);
2575 name = name == NULL ? bfd_section_name (sec) : name;
2576 }
2577 else
2578 {
2579 bool warned, ignored;
2580 bool unresolved_reloc;
2581
2582 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2583 r_symndx, symtab_hdr, sym_hashes,
2584 h, sec, relocation,
2585 unresolved_reloc, warned, ignored);
2586 osec = sec;
2587 }
2588
2589 if (sec != NULL && discarded_section (sec))
2590 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2591 rel, 1, relend, howto, 0, contents);
2592
2593 if (bfd_link_relocatable (info))
2594 continue;
2595
2596 if (h != NULL
2597 && (h->root.type == bfd_link_hash_defined
2598 || h->root.type == bfd_link_hash_defweak)
2599 && !BFINFDPIC_SYM_LOCAL (info, h))
2600 {
2601 osec = sec = NULL;
2602 relocation = 0;
2603 }
2604
2605 switch (r_type)
2606 {
2607 case R_BFIN_PCREL24:
2608 case R_BFIN_PCREL24_JUMP_L:
2609 case R_BFIN_BYTE4_DATA:
2610 if (! IS_FDPIC (output_bfd))
2611 goto non_fdpic;
2612 /* Fall through. */
2613
2614 case R_BFIN_GOT17M4:
2615 case R_BFIN_GOTHI:
2616 case R_BFIN_GOTLO:
2617 case R_BFIN_FUNCDESC_GOT17M4:
2618 case R_BFIN_FUNCDESC_GOTHI:
2619 case R_BFIN_FUNCDESC_GOTLO:
2620 case R_BFIN_GOTOFF17M4:
2621 case R_BFIN_GOTOFFHI:
2622 case R_BFIN_GOTOFFLO:
2623 case R_BFIN_FUNCDESC_GOTOFF17M4:
2624 case R_BFIN_FUNCDESC_GOTOFFHI:
2625 case R_BFIN_FUNCDESC_GOTOFFLO:
2626 case R_BFIN_FUNCDESC:
2627 case R_BFIN_FUNCDESC_VALUE:
2628 if ((input_section->flags & SEC_ALLOC) == 0)
2629 break;
2630
2631 if (h != NULL)
2632 picrel = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info
2633 (info), input_bfd, h,
2634 orig_addend, INSERT);
2635 else
2636 /* In order to find the entry we created before, we must
2637 use the original addend, not the one that may have been
2638 modified by _bfd_elf_rela_local_sym(). */
2639 picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info
2640 (info), input_bfd, r_symndx,
2641 orig_addend, INSERT);
2642 if (! picrel)
2643 return false;
2644
2645 if (!_bfinfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2646 osec, sym,
2647 rel->r_addend))
2648 {
2649 _bfd_error_handler
2650 /* xgettext:c-format */
2651 (_("%pB: relocation at `%pA+%#" PRIx64 "' "
2652 "references symbol `%s' with nonzero addend"),
2653 input_bfd, input_section, (uint64_t) rel->r_offset, name);
2654 return false;
2655
2656 }
2657
2658 break;
2659
2660 default:
2661 non_fdpic:
2662 picrel = NULL;
2663 if (h && ! BFINFDPIC_SYM_LOCAL (info, h)
2664 && _bfd_elf_section_offset (output_bfd, info, input_section,
2665 rel->r_offset) != (bfd_vma) -1)
2666 {
2667 info->callbacks->warning
2668 (info, _("relocation references symbol not defined in the module"),
2669 name, input_bfd, input_section, rel->r_offset);
2670 return false;
2671 }
2672 break;
2673 }
2674
2675 switch (r_type)
2676 {
2677 case R_BFIN_PCREL24:
2678 case R_BFIN_PCREL24_JUMP_L:
2679 check_segment[0] = isec_segment;
2680 if (! IS_FDPIC (output_bfd))
2681 check_segment[1] = isec_segment;
2682 else if (picrel->plt)
2683 {
2684 relocation = bfinfdpic_plt_section (info)->output_section->vma
2685 + bfinfdpic_plt_section (info)->output_offset
2686 + picrel->plt_entry;
2687 check_segment[1] = plt_segment;
2688 }
2689 /* We don't want to warn on calls to undefined weak symbols,
2690 as calls to them must be protected by non-NULL tests
2691 anyway, and unprotected calls would invoke undefined
2692 behavior. */
2693 else if (picrel->symndx == -1
2694 && picrel->d.h->root.type == bfd_link_hash_undefweak)
2695 check_segment[1] = check_segment[0];
2696 else
2697 check_segment[1] = sec
2698 ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
2699 : (unsigned)-1;
2700 break;
2701
2702 case R_BFIN_GOT17M4:
2703 case R_BFIN_GOTHI:
2704 case R_BFIN_GOTLO:
2705 relocation = picrel->got_entry;
2706 check_segment[0] = check_segment[1] = got_segment;
2707 break;
2708
2709 case R_BFIN_FUNCDESC_GOT17M4:
2710 case R_BFIN_FUNCDESC_GOTHI:
2711 case R_BFIN_FUNCDESC_GOTLO:
2712 relocation = picrel->fdgot_entry;
2713 check_segment[0] = check_segment[1] = got_segment;
2714 break;
2715
2716 case R_BFIN_GOTOFFHI:
2717 case R_BFIN_GOTOFF17M4:
2718 case R_BFIN_GOTOFFLO:
2719 relocation -= bfinfdpic_got_section (info)->output_section->vma
2720 + bfinfdpic_got_section (info)->output_offset
2721 + bfinfdpic_got_initial_offset (info);
2722 check_segment[0] = got_segment;
2723 check_segment[1] = sec
2724 ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
2725 : (unsigned)-1;
2726 break;
2727
2728 case R_BFIN_FUNCDESC_GOTOFF17M4:
2729 case R_BFIN_FUNCDESC_GOTOFFHI:
2730 case R_BFIN_FUNCDESC_GOTOFFLO:
2731 relocation = picrel->fd_entry;
2732 check_segment[0] = check_segment[1] = got_segment;
2733 break;
2734
2735 case R_BFIN_FUNCDESC:
2736 if ((input_section->flags & SEC_ALLOC) != 0)
2737 {
2738 int dynindx;
2739 bfd_vma addend = rel->r_addend;
2740
2741 if (! (h && h->root.type == bfd_link_hash_undefweak
2742 && BFINFDPIC_SYM_LOCAL (info, h)))
2743 {
2744 /* If the symbol is dynamic and there may be dynamic
2745 symbol resolution because we are or are linked with a
2746 shared library, emit a FUNCDESC relocation such that
2747 the dynamic linker will allocate the function
2748 descriptor. If the symbol needs a non-local function
2749 descriptor but binds locally (e.g., its visibility is
2750 protected, emit a dynamic relocation decayed to
2751 section+offset. */
2752 if (h && ! BFINFDPIC_FUNCDESC_LOCAL (info, h)
2753 && BFINFDPIC_SYM_LOCAL (info, h)
2754 && !bfd_link_pde (info))
2755 {
2756 dynindx = elf_section_data (h->root.u.def.section
2757 ->output_section)->dynindx;
2758 addend += h->root.u.def.section->output_offset
2759 + h->root.u.def.value;
2760 }
2761 else if (h && ! BFINFDPIC_FUNCDESC_LOCAL (info, h))
2762 {
2763 if (addend)
2764 {
2765 info->callbacks->warning
2766 (info, _("R_BFIN_FUNCDESC references dynamic symbol with nonzero addend"),
2767 name, input_bfd, input_section, rel->r_offset);
2768 return false;
2769 }
2770 dynindx = h->dynindx;
2771 }
2772 else
2773 {
2774 /* Otherwise, we know we have a private function
2775 descriptor, so reference it directly. */
2776 BFD_ASSERT (picrel->privfd);
2777 r_type = R_BFIN_BYTE4_DATA;
2778 dynindx = elf_section_data (bfinfdpic_got_section (info)
2779 ->output_section)->dynindx;
2780 addend = bfinfdpic_got_section (info)->output_offset
2781 + bfinfdpic_got_initial_offset (info)
2782 + picrel->fd_entry;
2783 }
2784
2785 /* If there is room for dynamic symbol resolution, emit
2786 the dynamic relocation. However, if we're linking an
2787 executable at a fixed location, we won't have emitted a
2788 dynamic symbol entry for the got section, so idx will
2789 be zero, which means we can and should compute the
2790 address of the private descriptor ourselves. */
2791 if (bfd_link_pde (info)
2792 && (!h || BFINFDPIC_FUNCDESC_LOCAL (info, h)))
2793 {
2794 bfd_vma offset;
2795
2796 addend += bfinfdpic_got_section (info)->output_section->vma;
2797 if ((bfd_section_flags (input_section->output_section)
2798 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2799 {
2800 if (_bfinfdpic_osec_readonly_p (output_bfd,
2801 input_section
2802 ->output_section))
2803 {
2804 info->callbacks->warning
2805 (info,
2806 _("cannot emit fixups in read-only section"),
2807 name, input_bfd, input_section, rel->r_offset);
2808 return false;
2809 }
2810
2811 offset = _bfd_elf_section_offset
2812 (output_bfd, info,
2813 input_section, rel->r_offset);
2814
2815 if (offset != (bfd_vma)-1)
2816 _bfinfdpic_add_rofixup (output_bfd,
2817 bfinfdpic_gotfixup_section
2818 (info),
2819 offset + input_section
2820 ->output_section->vma
2821 + input_section->output_offset,
2822 picrel);
2823 }
2824 }
2825 else if ((bfd_section_flags (input_section->output_section)
2826 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2827 {
2828 bfd_vma offset;
2829
2830 if (_bfinfdpic_osec_readonly_p (output_bfd,
2831 input_section
2832 ->output_section))
2833 {
2834 info->callbacks->warning
2835 (info,
2836 _("cannot emit dynamic relocations in read-only section"),
2837 name, input_bfd, input_section, rel->r_offset);
2838 return false;
2839 }
2840 offset = _bfd_elf_section_offset (output_bfd, info,
2841 input_section, rel->r_offset);
2842
2843 if (offset != (bfd_vma)-1)
2844 _bfinfdpic_add_dyn_reloc (output_bfd,
2845 bfinfdpic_gotrel_section (info),
2846 offset + input_section
2847 ->output_section->vma
2848 + input_section->output_offset,
2849 r_type,
2850 dynindx, addend, picrel);
2851 }
2852 else
2853 addend += bfinfdpic_got_section (info)->output_section->vma;
2854 }
2855
2856 /* We want the addend in-place because dynamic
2857 relocations are REL. Setting relocation to it should
2858 arrange for it to be installed. */
2859 relocation = addend - rel->r_addend;
2860 }
2861 check_segment[0] = check_segment[1] = got_segment;
2862 break;
2863
2864 case R_BFIN_BYTE4_DATA:
2865 if (! IS_FDPIC (output_bfd))
2866 {
2867 check_segment[0] = check_segment[1] = -1;
2868 break;
2869 }
2870 /* Fall through. */
2871 case R_BFIN_FUNCDESC_VALUE:
2872 {
2873 int dynindx;
2874 bfd_vma addend = rel->r_addend;
2875 bfd_vma offset;
2876 offset = _bfd_elf_section_offset (output_bfd, info,
2877 input_section, rel->r_offset);
2878
2879 /* If the symbol is dynamic but binds locally, use
2880 section+offset. */
2881 if (h && ! BFINFDPIC_SYM_LOCAL (info, h))
2882 {
2883 if (addend && r_type == R_BFIN_FUNCDESC_VALUE)
2884 {
2885 info->callbacks->warning
2886 (info, _("R_BFIN_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
2887 name, input_bfd, input_section, rel->r_offset);
2888 return false;
2889 }
2890 dynindx = h->dynindx;
2891 }
2892 else
2893 {
2894 if (h)
2895 addend += h->root.u.def.value;
2896 else
2897 addend += sym->st_value;
2898 if (osec)
2899 addend += osec->output_offset;
2900 if (osec && osec->output_section
2901 && ! bfd_is_abs_section (osec->output_section)
2902 && ! bfd_is_und_section (osec->output_section))
2903 dynindx = elf_section_data (osec->output_section)->dynindx;
2904 else
2905 dynindx = 0;
2906 }
2907
2908 /* If we're linking an executable at a fixed address, we
2909 can omit the dynamic relocation as long as the symbol
2910 is defined in the current link unit (which is implied
2911 by its output section not being NULL). */
2912 if (bfd_link_pde (info)
2913 && (!h || BFINFDPIC_SYM_LOCAL (info, h)))
2914 {
2915 if (osec)
2916 addend += osec->output_section->vma;
2917 if (IS_FDPIC (input_bfd)
2918 && (bfd_section_flags (input_section->output_section)
2919 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2920 {
2921 if (_bfinfdpic_osec_readonly_p (output_bfd,
2922 input_section
2923 ->output_section))
2924 {
2925 info->callbacks->warning
2926 (info,
2927 _("cannot emit fixups in read-only section"),
2928 name, input_bfd, input_section, rel->r_offset);
2929 return false;
2930 }
2931 if (!h || h->root.type != bfd_link_hash_undefweak)
2932 {
2933 if (offset != (bfd_vma)-1)
2934 {
2935 _bfinfdpic_add_rofixup (output_bfd,
2936 bfinfdpic_gotfixup_section
2937 (info),
2938 offset + input_section
2939 ->output_section->vma
2940 + input_section->output_offset,
2941 picrel);
2942
2943 if (r_type == R_BFIN_FUNCDESC_VALUE)
2944 _bfinfdpic_add_rofixup
2945 (output_bfd,
2946 bfinfdpic_gotfixup_section (info),
2947 offset + input_section->output_section->vma
2948 + input_section->output_offset + 4, picrel);
2949 }
2950 }
2951 }
2952 }
2953 else
2954 {
2955 if ((bfd_section_flags (input_section->output_section)
2956 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2957 {
2958 if (_bfinfdpic_osec_readonly_p (output_bfd,
2959 input_section
2960 ->output_section))
2961 {
2962 info->callbacks->warning
2963 (info,
2964 _("cannot emit dynamic relocations in read-only section"),
2965 name, input_bfd, input_section, rel->r_offset);
2966 return false;
2967 }
2968
2969 if (offset != (bfd_vma)-1)
2970 _bfinfdpic_add_dyn_reloc (output_bfd,
2971 bfinfdpic_gotrel_section (info),
2972 offset
2973 + input_section->output_section->vma
2974 + input_section->output_offset,
2975 r_type, dynindx, addend, picrel);
2976 }
2977 else if (osec)
2978 addend += osec->output_section->vma;
2979 /* We want the addend in-place because dynamic
2980 relocations are REL. Setting relocation to it
2981 should arrange for it to be installed. */
2982 relocation = addend - rel->r_addend;
2983 }
2984
2985 if (r_type == R_BFIN_FUNCDESC_VALUE)
2986 {
2987 /* If we've omitted the dynamic relocation, just emit
2988 the fixed addresses of the symbol and of the local
2989 GOT base offset. */
2990 if (bfd_link_pde (info)
2991 && (!h || BFINFDPIC_SYM_LOCAL (info, h)))
2992 bfd_put_32 (output_bfd,
2993 bfinfdpic_got_section (info)->output_section->vma
2994 + bfinfdpic_got_section (info)->output_offset
2995 + bfinfdpic_got_initial_offset (info),
2996 contents + rel->r_offset + 4);
2997 else
2998 /* A function descriptor used for lazy or local
2999 resolving is initialized such that its high word
3000 contains the output section index in which the
3001 PLT entries are located, and the low word
3002 contains the offset of the lazy PLT entry entry
3003 point into that section. */
3004 bfd_put_32 (output_bfd,
3005 h && ! BFINFDPIC_SYM_LOCAL (info, h)
3006 ? 0
3007 : _bfinfdpic_osec_to_segment (output_bfd,
3008 sec
3009 ->output_section),
3010 contents + rel->r_offset + 4);
3011 }
3012 }
3013 check_segment[0] = check_segment[1] = got_segment;
3014 break;
3015
3016 default:
3017 check_segment[0] = isec_segment;
3018 check_segment[1] = sec
3019 ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
3020 : (unsigned)-1;
3021 break;
3022 }
3023
3024 if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
3025 {
3026 #if 1 /* If you take this out, remove the #error from fdpic-static-6.d
3027 in the ld testsuite. */
3028 /* This helps catch problems in GCC while we can't do more
3029 than static linking. The idea is to test whether the
3030 input file basename is crt0.o only once. */
3031 if (silence_segment_error == 1)
3032 silence_segment_error =
3033 (strlen (bfd_get_filename (input_bfd)) == 6
3034 && filename_cmp (bfd_get_filename (input_bfd), "crt0.o") == 0)
3035 || (strlen (bfd_get_filename (input_bfd)) > 6
3036 && filename_cmp (bfd_get_filename (input_bfd)
3037 + strlen (bfd_get_filename (input_bfd)) - 7,
3038 "/crt0.o") == 0)
3039 ? -1 : 0;
3040 #endif
3041 if (!silence_segment_error
3042 /* We don't want duplicate errors for undefined
3043 symbols. */
3044 && !(picrel && picrel->symndx == -1
3045 && picrel->d.h->root.type == bfd_link_hash_undefined))
3046 info->callbacks->warning
3047 (info,
3048 bfd_link_pic (info)
3049 ? _("relocations between different segments are not supported")
3050 : _("warning: relocation references a different segment"),
3051 name, input_bfd, input_section, rel->r_offset);
3052 if (!silence_segment_error && bfd_link_pic (info))
3053 return false;
3054 elf_elfheader (output_bfd)->e_flags |= EF_BFIN_PIC;
3055 }
3056
3057 switch (r_type)
3058 {
3059 case R_BFIN_GOTOFFHI:
3060 /* We need the addend to be applied before we shift the
3061 value right. */
3062 relocation += rel->r_addend;
3063 /* Fall through. */
3064 case R_BFIN_GOTHI:
3065 case R_BFIN_FUNCDESC_GOTHI:
3066 case R_BFIN_FUNCDESC_GOTOFFHI:
3067 relocation >>= 16;
3068 /* Fall through. */
3069
3070 case R_BFIN_GOTLO:
3071 case R_BFIN_FUNCDESC_GOTLO:
3072 case R_BFIN_GOTOFFLO:
3073 case R_BFIN_FUNCDESC_GOTOFFLO:
3074 relocation &= 0xffff;
3075 break;
3076
3077 default:
3078 break;
3079 }
3080
3081 switch (r_type)
3082 {
3083 case R_BFIN_PCREL24:
3084 case R_BFIN_PCREL24_JUMP_L:
3085 if (! IS_FDPIC (output_bfd) || ! picrel->plt)
3086 break;
3087 /* Fall through. */
3088
3089 /* When referencing a GOT entry, a function descriptor or a
3090 PLT, we don't want the addend to apply to the reference,
3091 but rather to the referenced symbol. The actual entry
3092 will have already been created taking the addend into
3093 account, so cancel it out here. */
3094 case R_BFIN_GOT17M4:
3095 case R_BFIN_GOTHI:
3096 case R_BFIN_GOTLO:
3097 case R_BFIN_FUNCDESC_GOT17M4:
3098 case R_BFIN_FUNCDESC_GOTHI:
3099 case R_BFIN_FUNCDESC_GOTLO:
3100 case R_BFIN_FUNCDESC_GOTOFF17M4:
3101 case R_BFIN_FUNCDESC_GOTOFFHI:
3102 case R_BFIN_FUNCDESC_GOTOFFLO:
3103 /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF17M4
3104 here, since we do want to apply the addend to the others.
3105 Note that we've applied the addend to GOTOFFHI before we
3106 shifted it right. */
3107 case R_BFIN_GOTOFFHI:
3108 relocation -= rel->r_addend;
3109 break;
3110
3111 default:
3112 break;
3113 }
3114
3115 r = bfin_final_link_relocate (rel, howto, input_bfd, input_section,
3116 contents, rel->r_offset,
3117 relocation, rel->r_addend);
3118
3119 if (r != bfd_reloc_ok)
3120 {
3121 const char * msg = (const char *) NULL;
3122
3123 switch (r)
3124 {
3125 case bfd_reloc_overflow:
3126 (*info->callbacks->reloc_overflow)
3127 (info, (h ? &h->root : NULL), name, howto->name,
3128 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3129 break;
3130
3131 case bfd_reloc_undefined:
3132 (*info->callbacks->undefined_symbol)
3133 (info, name, input_bfd, input_section, rel->r_offset, true);
3134 break;
3135
3136 case bfd_reloc_outofrange:
3137 msg = _("internal error: out of range error");
3138 break;
3139
3140 case bfd_reloc_notsupported:
3141 msg = _("internal error: unsupported relocation error");
3142 break;
3143
3144 case bfd_reloc_dangerous:
3145 msg = _("internal error: dangerous relocation");
3146 break;
3147
3148 default:
3149 msg = _("internal error: unknown error");
3150 break;
3151 }
3152
3153 if (msg)
3154 (*info->callbacks->warning) (info, msg, name, input_bfd,
3155 input_section, rel->r_offset);
3156 }
3157 }
3158
3159 return true;
3160 }
3161
3162 /* We need dynamic symbols for every section, since segments can
3163 relocate independently. */
3164 static bool
3165 _bfinfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
3166 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3167 asection *p)
3168 {
3169 switch (elf_section_data (p)->this_hdr.sh_type)
3170 {
3171 case SHT_PROGBITS:
3172 case SHT_NOBITS:
3173 /* If sh_type is yet undecided, assume it could be
3174 SHT_PROGBITS/SHT_NOBITS. */
3175 case SHT_NULL:
3176 return false;
3177
3178 /* There shouldn't be section relative relocations
3179 against any other section. */
3180 default:
3181 return true;
3182 }
3183 }
3184
3185 /* Create a .got section, as well as its additional info field. This
3186 is almost entirely copied from
3187 elflink.c:_bfd_elf_create_got_section(). */
3188
3189 static bool
3190 _bfin_create_got_section (bfd *abfd, struct bfd_link_info *info)
3191 {
3192 flagword flags, pltflags;
3193 asection *s;
3194 struct elf_link_hash_entry *h;
3195 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3196 int ptralign;
3197
3198 /* This function may be called more than once. */
3199 s = elf_hash_table (info)->sgot;
3200 if (s != NULL)
3201 return true;
3202
3203 /* Machine specific: although pointers are 32-bits wide, we want the
3204 GOT to be aligned to a 64-bit boundary, such that function
3205 descriptors in it can be accessed with 64-bit loads and
3206 stores. */
3207 ptralign = 3;
3208
3209 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3210 | SEC_LINKER_CREATED);
3211 pltflags = flags;
3212
3213 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3214 elf_hash_table (info)->sgot = s;
3215 if (s == NULL
3216 || !bfd_set_section_alignment (s, ptralign))
3217 return false;
3218
3219 if (bed->want_got_sym)
3220 {
3221 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
3222 (or .got.plt) section. We don't do this in the linker script
3223 because we don't want to define the symbol if we are not creating
3224 a global offset table. */
3225 h = _bfd_elf_define_linkage_sym (abfd, info, s, "__GLOBAL_OFFSET_TABLE_");
3226 elf_hash_table (info)->hgot = h;
3227 if (h == NULL)
3228 return false;
3229
3230 /* Machine-specific: we want the symbol for executables as
3231 well. */
3232 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3233 return false;
3234 }
3235
3236 /* The first bit of the global offset table is the header. */
3237 s->size += bed->got_header_size;
3238
3239 /* This is the machine-specific part. Create and initialize section
3240 data for the got. */
3241 if (IS_FDPIC (abfd))
3242 {
3243 bfinfdpic_relocs_info (info) = htab_try_create (1,
3244 bfinfdpic_relocs_info_hash,
3245 bfinfdpic_relocs_info_eq,
3246 (htab_del) NULL);
3247 if (! bfinfdpic_relocs_info (info))
3248 return false;
3249
3250 s = bfd_make_section_anyway_with_flags (abfd, ".rel.got",
3251 (flags | SEC_READONLY));
3252 if (s == NULL
3253 || !bfd_set_section_alignment (s, 2))
3254 return false;
3255
3256 bfinfdpic_gotrel_section (info) = s;
3257
3258 /* Machine-specific. */
3259 s = bfd_make_section_anyway_with_flags (abfd, ".rofixup",
3260 (flags | SEC_READONLY));
3261 if (s == NULL
3262 || !bfd_set_section_alignment (s, 2))
3263 return false;
3264
3265 bfinfdpic_gotfixup_section (info) = s;
3266 }
3267
3268 pltflags |= SEC_CODE;
3269 if (bed->plt_not_loaded)
3270 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
3271 if (bed->plt_readonly)
3272 pltflags |= SEC_READONLY;
3273
3274 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
3275 if (s == NULL
3276 || !bfd_set_section_alignment (s, bed->plt_alignment))
3277 return false;
3278 /* Blackfin-specific: remember it. */
3279 bfinfdpic_plt_section (info) = s;
3280
3281 if (bed->want_plt_sym)
3282 {
3283 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3284 .plt section. */
3285 struct bfd_link_hash_entry *bh = NULL;
3286
3287 if (! (_bfd_generic_link_add_one_symbol
3288 (info, abfd, "__PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
3289 false, get_elf_backend_data (abfd)->collect, &bh)))
3290 return false;
3291 h = (struct elf_link_hash_entry *) bh;
3292 h->def_regular = 1;
3293 h->type = STT_OBJECT;
3294
3295 if (! bfd_link_executable (info)
3296 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3297 return false;
3298 }
3299
3300 /* Blackfin-specific: we want rel relocations for the plt. */
3301 s = bfd_make_section_anyway_with_flags (abfd, ".rel.plt",
3302 flags | SEC_READONLY);
3303 if (s == NULL
3304 || !bfd_set_section_alignment (s, bed->s->log_file_align))
3305 return false;
3306 /* Blackfin-specific: remember it. */
3307 bfinfdpic_pltrel_section (info) = s;
3308
3309 return true;
3310 }
3311
3312 /* Make sure the got and plt sections exist, and that our pointers in
3313 the link hash table point to them. */
3314
3315 static bool
3316 elf32_bfinfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3317 {
3318 /* This is mostly copied from
3319 elflink.c:_bfd_elf_create_dynamic_sections(). */
3320 flagword flags;
3321 asection *s;
3322 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3323
3324 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3325 | SEC_LINKER_CREATED);
3326
3327 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3328 .rel[a].bss sections. */
3329
3330 /* Blackfin-specific: we want to create the GOT in the Blackfin way. */
3331 if (! _bfin_create_got_section (abfd, info))
3332 return false;
3333
3334 /* Blackfin-specific: make sure we created everything we wanted. */
3335 BFD_ASSERT (bfinfdpic_got_section (info) && bfinfdpic_gotrel_section (info)
3336 /* && bfinfdpic_gotfixup_section (info) */
3337 && bfinfdpic_plt_section (info)
3338 && bfinfdpic_pltrel_section (info));
3339
3340 if (bed->want_dynbss)
3341 {
3342 /* The .dynbss section is a place to put symbols which are defined
3343 by dynamic objects, are referenced by regular objects, and are
3344 not functions. We must allocate space for them in the process
3345 image and use a R_*_COPY reloc to tell the dynamic linker to
3346 initialize them at run time. The linker script puts the .dynbss
3347 section into the .bss section of the final image. */
3348 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
3349 SEC_ALLOC | SEC_LINKER_CREATED);
3350 if (s == NULL)
3351 return false;
3352
3353 /* The .rel[a].bss section holds copy relocs. This section is not
3354 normally needed. We need to create it here, though, so that the
3355 linker will map it to an output section. We can't just create it
3356 only if we need it, because we will not know whether we need it
3357 until we have seen all the input files, and the first time the
3358 main linker code calls BFD after examining all the input files
3359 (size_dynamic_sections) the input sections have already been
3360 mapped to the output sections. If the section turns out not to
3361 be needed, we can discard it later. We will never need this
3362 section when generating a shared object, since they do not use
3363 copy relocs. */
3364 if (! bfd_link_pic (info))
3365 {
3366 s = bfd_make_section_anyway_with_flags (abfd,
3367 ".rela.bss",
3368 flags | SEC_READONLY);
3369 if (s == NULL
3370 || !bfd_set_section_alignment (s, bed->s->log_file_align))
3371 return false;
3372 }
3373 }
3374
3375 return true;
3376 }
3377
3378 /* Compute the total GOT size required by each symbol in each range.
3379 Symbols may require up to 4 words in the GOT: an entry pointing to
3380 the symbol, an entry pointing to its function descriptor, and a
3381 private function descriptors taking two words. */
3382
3383 static void
3384 _bfinfdpic_count_nontls_entries (struct bfinfdpic_relocs_info *entry,
3385 struct _bfinfdpic_dynamic_got_info *dinfo)
3386 {
3387 /* Allocate space for a GOT entry pointing to the symbol. */
3388 if (entry->got17m4)
3389 dinfo->got17m4 += 4;
3390 else if (entry->gothilo)
3391 dinfo->gothilo += 4;
3392 else
3393 entry->relocs32--;
3394 entry->relocs32++;
3395
3396 /* Allocate space for a GOT entry pointing to the function
3397 descriptor. */
3398 if (entry->fdgot17m4)
3399 dinfo->got17m4 += 4;
3400 else if (entry->fdgothilo)
3401 dinfo->gothilo += 4;
3402 else
3403 entry->relocsfd--;
3404 entry->relocsfd++;
3405
3406 /* Decide whether we need a PLT entry, a function descriptor in the
3407 GOT, and a lazy PLT entry for this symbol. */
3408 entry->plt = entry->call
3409 && entry->symndx == -1 && ! BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
3410 && elf_hash_table (dinfo->info)->dynamic_sections_created;
3411 entry->privfd = entry->plt
3412 || entry->fdgoff17m4 || entry->fdgoffhilo
3413 || ((entry->fd || entry->fdgot17m4 || entry->fdgothilo)
3414 && (entry->symndx != -1
3415 || BFINFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
3416 entry->lazyplt = entry->privfd
3417 && entry->symndx == -1 && ! BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
3418 && ! (dinfo->info->flags & DF_BIND_NOW)
3419 && elf_hash_table (dinfo->info)->dynamic_sections_created;
3420
3421 /* Allocate space for a function descriptor. */
3422 if (entry->fdgoff17m4)
3423 dinfo->fd17m4 += 8;
3424 else if (entry->privfd && entry->plt)
3425 dinfo->fdplt += 8;
3426 else if (entry->privfd)
3427 dinfo->fdhilo += 8;
3428 else
3429 entry->relocsfdv--;
3430 entry->relocsfdv++;
3431
3432 if (entry->lazyplt)
3433 dinfo->lzplt += LZPLT_NORMAL_SIZE;
3434 }
3435
3436 /* Compute the number of dynamic relocations and fixups that a symbol
3437 requires, and add (or subtract) from the grand and per-symbol
3438 totals. */
3439
3440 static void
3441 _bfinfdpic_count_relocs_fixups (struct bfinfdpic_relocs_info *entry,
3442 struct _bfinfdpic_dynamic_got_info *dinfo,
3443 bool subtract)
3444 {
3445 bfd_vma relocs = 0, fixups = 0;
3446
3447 if (!bfd_link_pde (dinfo->info))
3448 relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv;
3449 else
3450 {
3451 if (entry->symndx != -1 || BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
3452 {
3453 if (entry->symndx != -1
3454 || entry->d.h->root.type != bfd_link_hash_undefweak)
3455 fixups += entry->relocs32 + 2 * entry->relocsfdv;
3456 }
3457 else
3458 relocs += entry->relocs32 + entry->relocsfdv;
3459
3460 if (entry->symndx != -1
3461 || BFINFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
3462 {
3463 if (entry->symndx != -1
3464 || entry->d.h->root.type != bfd_link_hash_undefweak)
3465 fixups += entry->relocsfd;
3466 }
3467 else
3468 relocs += entry->relocsfd;
3469 }
3470
3471 if (subtract)
3472 {
3473 relocs = - relocs;
3474 fixups = - fixups;
3475 }
3476
3477 entry->dynrelocs += relocs;
3478 entry->fixups += fixups;
3479 dinfo->relocs += relocs;
3480 dinfo->fixups += fixups;
3481 }
3482
3483 /* Compute the total GOT and PLT size required by each symbol in each range. *
3484 Symbols may require up to 4 words in the GOT: an entry pointing to
3485 the symbol, an entry pointing to its function descriptor, and a
3486 private function descriptors taking two words. */
3487
3488 static int
3489 _bfinfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
3490 {
3491 struct bfinfdpic_relocs_info *entry = *entryp;
3492 struct _bfinfdpic_dynamic_got_info *dinfo = dinfo_;
3493
3494 _bfinfdpic_count_nontls_entries (entry, dinfo);
3495
3496 _bfinfdpic_count_relocs_fixups (entry, dinfo, false);
3497
3498 return 1;
3499 }
3500
3501 /* This structure is used to assign offsets to got entries, function
3502 descriptors, plt entries and lazy plt entries. */
3503
3504 struct _bfinfdpic_dynamic_got_plt_info
3505 {
3506 /* Summary information collected with _bfinfdpic_count_got_plt_entries. */
3507 struct _bfinfdpic_dynamic_got_info g;
3508
3509 /* For each addressable range, we record a MAX (positive) and MIN
3510 (negative) value. CUR is used to assign got entries, and it's
3511 incremented from an initial positive value to MAX, then from MIN
3512 to FDCUR (unless FDCUR wraps around first). FDCUR is used to
3513 assign function descriptors, and it's decreased from an initial
3514 non-positive value to MIN, then from MAX down to CUR (unless CUR
3515 wraps around first). All of MIN, MAX, CUR and FDCUR always point
3516 to even words. ODD, if non-zero, indicates an odd word to be
3517 used for the next got entry, otherwise CUR is used and
3518 incremented by a pair of words, wrapping around when it reaches
3519 MAX. FDCUR is decremented (and wrapped) before the next function
3520 descriptor is chosen. FDPLT indicates the number of remaining
3521 slots that can be used for function descriptors used only by PLT
3522 entries. */
3523 struct _bfinfdpic_dynamic_got_alloc_data
3524 {
3525 bfd_signed_vma max, cur, odd, fdcur, min;
3526 bfd_vma fdplt;
3527 } got17m4, gothilo;
3528 };
3529
3530 /* Determine the positive and negative ranges to be used by each
3531 offset range in the GOT. FDCUR and CUR, that must be aligned to a
3532 double-word boundary, are the minimum (negative) and maximum
3533 (positive) GOT offsets already used by previous ranges, except for
3534 an ODD entry that may have been left behind. GOT and FD indicate
3535 the size of GOT entries and function descriptors that must be
3536 placed within the range from -WRAP to WRAP. If there's room left,
3537 up to FDPLT bytes should be reserved for additional function
3538 descriptors. */
3539
3540 inline static bfd_signed_vma
3541 _bfinfdpic_compute_got_alloc_data (struct _bfinfdpic_dynamic_got_alloc_data *gad,
3542 bfd_signed_vma fdcur,
3543 bfd_signed_vma odd,
3544 bfd_signed_vma cur,
3545 bfd_vma got,
3546 bfd_vma fd,
3547 bfd_vma fdplt,
3548 bfd_vma wrap)
3549 {
3550 bfd_signed_vma wrapmin = -wrap;
3551
3552 /* Start at the given initial points. */
3553 gad->fdcur = fdcur;
3554 gad->cur = cur;
3555
3556 /* If we had an incoming odd word and we have any got entries that
3557 are going to use it, consume it, otherwise leave gad->odd at
3558 zero. We might force gad->odd to zero and return the incoming
3559 odd such that it is used by the next range, but then GOT entries
3560 might appear to be out of order and we wouldn't be able to
3561 shorten the GOT by one word if it turns out to end with an
3562 unpaired GOT entry. */
3563 if (odd && got)
3564 {
3565 gad->odd = odd;
3566 got -= 4;
3567 odd = 0;
3568 }
3569 else
3570 gad->odd = 0;
3571
3572 /* If we're left with an unpaired GOT entry, compute its location
3573 such that we can return it. Otherwise, if got doesn't require an
3574 odd number of words here, either odd was already zero in the
3575 block above, or it was set to zero because got was non-zero, or
3576 got was already zero. In the latter case, we want the value of
3577 odd to carry over to the return statement, so we don't want to
3578 reset odd unless the condition below is true. */
3579 if (got & 4)
3580 {
3581 odd = cur + got;
3582 got += 4;
3583 }
3584
3585 /* Compute the tentative boundaries of this range. */
3586 gad->max = cur + got;
3587 gad->min = fdcur - fd;
3588 gad->fdplt = 0;
3589
3590 /* If function descriptors took too much space, wrap some of them
3591 around. */
3592 if (gad->min < wrapmin)
3593 {
3594 gad->max += wrapmin - gad->min;
3595 gad->min = wrapmin;
3596 }
3597 /* If there is space left and we have function descriptors
3598 referenced in PLT entries that could take advantage of shorter
3599 offsets, place them here. */
3600 else if (fdplt && gad->min > wrapmin)
3601 {
3602 bfd_vma fds;
3603 if ((bfd_vma) (gad->min - wrapmin) < fdplt)
3604 fds = gad->min - wrapmin;
3605 else
3606 fds = fdplt;
3607
3608 fdplt -= fds;
3609 gad->min -= fds;
3610 gad->fdplt += fds;
3611 }
3612
3613 /* If GOT entries took too much space, wrap some of them around.
3614 This may well cause gad->min to become lower than wrapmin. This
3615 will cause a relocation overflow later on, so we don't have to
3616 report it here . */
3617 if ((bfd_vma) gad->max > wrap)
3618 {
3619 gad->min -= gad->max - wrap;
3620 gad->max = wrap;
3621 }
3622 /* If there is more space left, try to place some more function
3623 descriptors for PLT entries. */
3624 else if (fdplt && (bfd_vma) gad->max < wrap)
3625 {
3626 bfd_vma fds;
3627 if ((bfd_vma) (wrap - gad->max) < fdplt)
3628 fds = wrap - gad->max;
3629 else
3630 fds = fdplt;
3631
3632 fdplt -= fds;
3633 gad->max += fds;
3634 gad->fdplt += fds;
3635 }
3636
3637 /* If odd was initially computed as an offset past the wrap point,
3638 wrap it around. */
3639 if (odd > gad->max)
3640 odd = gad->min + odd - gad->max;
3641
3642 /* _bfinfdpic_get_got_entry() below will always wrap gad->cur if needed
3643 before returning, so do it here too. This guarantees that,
3644 should cur and fdcur meet at the wrap point, they'll both be
3645 equal to min. */
3646 if (gad->cur == gad->max)
3647 gad->cur = gad->min;
3648
3649 return odd;
3650 }
3651
3652 /* Compute the location of the next GOT entry, given the allocation
3653 data for a range. */
3654
3655 inline static bfd_signed_vma
3656 _bfinfdpic_get_got_entry (struct _bfinfdpic_dynamic_got_alloc_data *gad)
3657 {
3658 bfd_signed_vma ret;
3659
3660 if (gad->odd)
3661 {
3662 /* If there was an odd word left behind, use it. */
3663 ret = gad->odd;
3664 gad->odd = 0;
3665 }
3666 else
3667 {
3668 /* Otherwise, use the word pointed to by cur, reserve the next
3669 as an odd word, and skip to the next pair of words, possibly
3670 wrapping around. */
3671 ret = gad->cur;
3672 gad->odd = gad->cur + 4;
3673 gad->cur += 8;
3674 if (gad->cur == gad->max)
3675 gad->cur = gad->min;
3676 }
3677
3678 return ret;
3679 }
3680
3681 /* Compute the location of the next function descriptor entry in the
3682 GOT, given the allocation data for a range. */
3683
3684 inline static bfd_signed_vma
3685 _bfinfdpic_get_fd_entry (struct _bfinfdpic_dynamic_got_alloc_data *gad)
3686 {
3687 /* If we're at the bottom, wrap around, and only then allocate the
3688 next pair of words. */
3689 if (gad->fdcur == gad->min)
3690 gad->fdcur = gad->max;
3691 return gad->fdcur -= 8;
3692 }
3693
3694 /* Assign GOT offsets for every GOT entry and function descriptor.
3695 Doing everything in a single pass is tricky. */
3696
3697 static int
3698 _bfinfdpic_assign_got_entries (void **entryp, void *info_)
3699 {
3700 struct bfinfdpic_relocs_info *entry = *entryp;
3701 struct _bfinfdpic_dynamic_got_plt_info *dinfo = info_;
3702
3703 if (entry->got17m4)
3704 entry->got_entry = _bfinfdpic_get_got_entry (&dinfo->got17m4);
3705 else if (entry->gothilo)
3706 entry->got_entry = _bfinfdpic_get_got_entry (&dinfo->gothilo);
3707
3708 if (entry->fdgot17m4)
3709 entry->fdgot_entry = _bfinfdpic_get_got_entry (&dinfo->got17m4);
3710 else if (entry->fdgothilo)
3711 entry->fdgot_entry = _bfinfdpic_get_got_entry (&dinfo->gothilo);
3712
3713 if (entry->fdgoff17m4)
3714 entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
3715 else if (entry->plt && dinfo->got17m4.fdplt)
3716 {
3717 dinfo->got17m4.fdplt -= 8;
3718 entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
3719 }
3720 else if (entry->plt)
3721 {
3722 dinfo->gothilo.fdplt -= 8;
3723 entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
3724 }
3725 else if (entry->privfd)
3726 entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
3727
3728 return 1;
3729 }
3730
3731 /* Assign GOT offsets to private function descriptors used by PLT
3732 entries (or referenced by 32-bit offsets), as well as PLT entries
3733 and lazy PLT entries. */
3734
3735 static int
3736 _bfinfdpic_assign_plt_entries (void **entryp, void *info_)
3737 {
3738 struct bfinfdpic_relocs_info *entry = *entryp;
3739 struct _bfinfdpic_dynamic_got_plt_info *dinfo = info_;
3740
3741 /* If this symbol requires a local function descriptor, allocate
3742 one. */
3743 if (entry->privfd && entry->fd_entry == 0)
3744 {
3745 if (dinfo->got17m4.fdplt)
3746 {
3747 entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
3748 dinfo->got17m4.fdplt -= 8;
3749 }
3750 else
3751 {
3752 BFD_ASSERT (dinfo->gothilo.fdplt);
3753 entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
3754 dinfo->gothilo.fdplt -= 8;
3755 }
3756 }
3757
3758 if (entry->plt)
3759 {
3760 int size;
3761
3762 /* We use the section's raw size to mark the location of the
3763 next PLT entry. */
3764 entry->plt_entry = bfinfdpic_plt_section (dinfo->g.info)->size;
3765
3766 /* Figure out the length of this PLT entry based on the
3767 addressing mode we need to reach the function descriptor. */
3768 BFD_ASSERT (entry->fd_entry);
3769 if (entry->fd_entry >= -(1 << (18 - 1))
3770 && entry->fd_entry + 4 < (1 << (18 - 1)))
3771 size = 10;
3772 else
3773 size = 16;
3774
3775 bfinfdpic_plt_section (dinfo->g.info)->size += size;
3776 }
3777
3778 if (entry->lazyplt)
3779 {
3780 entry->lzplt_entry = dinfo->g.lzplt;
3781 dinfo->g.lzplt += LZPLT_NORMAL_SIZE;
3782 /* If this entry is the one that gets the resolver stub, account
3783 for the additional instruction. */
3784 if (entry->lzplt_entry % BFINFDPIC_LZPLT_BLOCK_SIZE
3785 == BFINFDPIC_LZPLT_RESOLV_LOC)
3786 dinfo->g.lzplt += LZPLT_RESOLVER_EXTRA;
3787 }
3788
3789 return 1;
3790 }
3791
3792 /* Cancel out any effects of calling _bfinfdpic_assign_got_entries and
3793 _bfinfdpic_assign_plt_entries. */
3794
3795 static int
3796 _bfinfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
3797 {
3798 struct bfinfdpic_relocs_info *entry = *entryp;
3799
3800 entry->got_entry = 0;
3801 entry->fdgot_entry = 0;
3802 entry->fd_entry = 0;
3803 entry->plt_entry = (bfd_vma)-1;
3804 entry->lzplt_entry = (bfd_vma)-1;
3805
3806 return 1;
3807 }
3808
3809 /* Follow indirect and warning hash entries so that each got entry
3810 points to the final symbol definition. P must point to a pointer
3811 to the hash table we're traversing. Since this traversal may
3812 modify the hash table, we set this pointer to NULL to indicate
3813 we've made a potentially-destructive change to the hash table, so
3814 the traversal must be restarted. */
3815 static int
3816 _bfinfdpic_resolve_final_relocs_info (void **entryp, void *p)
3817 {
3818 struct bfinfdpic_relocs_info *entry = *entryp;
3819 htab_t *htab = p;
3820
3821 if (entry->symndx == -1)
3822 {
3823 struct elf_link_hash_entry *h = entry->d.h;
3824 struct bfinfdpic_relocs_info *oentry;
3825
3826 while (h->root.type == bfd_link_hash_indirect
3827 || h->root.type == bfd_link_hash_warning)
3828 h = (struct elf_link_hash_entry *)h->root.u.i.link;
3829
3830 if (entry->d.h == h)
3831 return 1;
3832
3833 oentry = bfinfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
3834 NO_INSERT);
3835
3836 if (oentry)
3837 {
3838 /* Merge the two entries. */
3839 bfinfdpic_pic_merge_early_relocs_info (oentry, entry);
3840 htab_clear_slot (*htab, entryp);
3841 return 1;
3842 }
3843
3844 entry->d.h = h;
3845
3846 /* If we can't find this entry with the new bfd hash, re-insert
3847 it, and get the traversal restarted. */
3848 if (! htab_find (*htab, entry))
3849 {
3850 htab_clear_slot (*htab, entryp);
3851 entryp = htab_find_slot (*htab, entry, INSERT);
3852 if (! *entryp)
3853 *entryp = entry;
3854 /* Abort the traversal, since the whole table may have
3855 moved, and leave it up to the parent to restart the
3856 process. */
3857 *(htab_t *)p = NULL;
3858 return 0;
3859 }
3860 }
3861
3862 return 1;
3863 }
3864
3865 /* Compute the total size of the GOT, the PLT, the dynamic relocations
3866 section and the rofixup section. Assign locations for GOT and PLT
3867 entries. */
3868
3869 static bool
3870 _bfinfdpic_size_got_plt (bfd *output_bfd,
3871 struct _bfinfdpic_dynamic_got_plt_info *gpinfop)
3872 {
3873 bfd_signed_vma odd;
3874 bfd_vma limit;
3875 struct bfd_link_info *info = gpinfop->g.info;
3876 bfd *dynobj = elf_hash_table (info)->dynobj;
3877
3878 memcpy (bfinfdpic_dynamic_got_plt_info (info), &gpinfop->g,
3879 sizeof (gpinfop->g));
3880
3881 odd = 12;
3882 /* Compute the total size taken by entries in the 18-bit range,
3883 to tell how many PLT function descriptors we can bring into it
3884 without causing it to overflow. */
3885 limit = odd + gpinfop->g.got17m4 + gpinfop->g.fd17m4;
3886 if (limit < (bfd_vma)1 << 18)
3887 limit = ((bfd_vma)1 << 18) - limit;
3888 else
3889 limit = 0;
3890 if (gpinfop->g.fdplt < limit)
3891 limit = gpinfop->g.fdplt;
3892
3893 /* Determine the ranges of GOT offsets that we can use for each
3894 range of addressing modes. */
3895 odd = _bfinfdpic_compute_got_alloc_data (&gpinfop->got17m4,
3896 0,
3897 odd,
3898 16,
3899 gpinfop->g.got17m4,
3900 gpinfop->g.fd17m4,
3901 limit,
3902 (bfd_vma)1 << (18-1));
3903 odd = _bfinfdpic_compute_got_alloc_data (&gpinfop->gothilo,
3904 gpinfop->got17m4.min,
3905 odd,
3906 gpinfop->got17m4.max,
3907 gpinfop->g.gothilo,
3908 gpinfop->g.fdhilo,
3909 gpinfop->g.fdplt - gpinfop->got17m4.fdplt,
3910 (bfd_vma)1 << (32-1));
3911
3912 /* Now assign (most) GOT offsets. */
3913 htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_assign_got_entries,
3914 gpinfop);
3915
3916 bfinfdpic_got_section (info)->size = gpinfop->gothilo.max
3917 - gpinfop->gothilo.min
3918 /* If an odd word is the last word of the GOT, we don't need this
3919 word to be part of the GOT. */
3920 - (odd + 4 == gpinfop->gothilo.max ? 4 : 0);
3921 if (bfinfdpic_got_section (info)->size == 0)
3922 bfinfdpic_got_section (info)->flags |= SEC_EXCLUDE;
3923 else if (bfinfdpic_got_section (info)->size == 12
3924 && ! elf_hash_table (info)->dynamic_sections_created)
3925 {
3926 bfinfdpic_got_section (info)->flags |= SEC_EXCLUDE;
3927 bfinfdpic_got_section (info)->size = 0;
3928 }
3929 else
3930 {
3931 bfinfdpic_got_section (info)->contents =
3932 (bfd_byte *) bfd_zalloc (dynobj,
3933 bfinfdpic_got_section (info)->size);
3934 if (bfinfdpic_got_section (info)->contents == NULL)
3935 return false;
3936 }
3937
3938 if (elf_hash_table (info)->dynamic_sections_created)
3939 /* Subtract the number of lzplt entries, since those will generate
3940 relocations in the pltrel section. */
3941 bfinfdpic_gotrel_section (info)->size =
3942 (gpinfop->g.relocs - gpinfop->g.lzplt / LZPLT_NORMAL_SIZE)
3943 * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3944 else
3945 BFD_ASSERT (gpinfop->g.relocs == 0);
3946 if (bfinfdpic_gotrel_section (info)->size == 0)
3947 bfinfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
3948 else
3949 {
3950 bfinfdpic_gotrel_section (info)->contents =
3951 (bfd_byte *) bfd_zalloc (dynobj,
3952 bfinfdpic_gotrel_section (info)->size);
3953 if (bfinfdpic_gotrel_section (info)->contents == NULL)
3954 return false;
3955 }
3956
3957 bfinfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
3958 if (bfinfdpic_gotfixup_section (info)->size == 0)
3959 bfinfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
3960 else
3961 {
3962 bfinfdpic_gotfixup_section (info)->contents =
3963 (bfd_byte *) bfd_zalloc (dynobj,
3964 bfinfdpic_gotfixup_section (info)->size);
3965 if (bfinfdpic_gotfixup_section (info)->contents == NULL)
3966 return false;
3967 }
3968
3969 if (elf_hash_table (info)->dynamic_sections_created)
3970 bfinfdpic_pltrel_section (info)->size =
3971 gpinfop->g.lzplt / LZPLT_NORMAL_SIZE * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3972 if (bfinfdpic_pltrel_section (info)->size == 0)
3973 bfinfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
3974 else
3975 {
3976 bfinfdpic_pltrel_section (info)->contents =
3977 (bfd_byte *) bfd_zalloc (dynobj,
3978 bfinfdpic_pltrel_section (info)->size);
3979 if (bfinfdpic_pltrel_section (info)->contents == NULL)
3980 return false;
3981 }
3982
3983 /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
3984 such that there's room for the additional instruction needed to
3985 call the resolver. Since _bfinfdpic_assign_got_entries didn't
3986 account for them, our block size is 4 bytes smaller than the real
3987 block size. */
3988 if (elf_hash_table (info)->dynamic_sections_created)
3989 {
3990 bfinfdpic_plt_section (info)->size = gpinfop->g.lzplt
3991 + ((gpinfop->g.lzplt + (BFINFDPIC_LZPLT_BLOCK_SIZE - 4) - LZPLT_NORMAL_SIZE)
3992 / (BFINFDPIC_LZPLT_BLOCK_SIZE - 4) * LZPLT_RESOLVER_EXTRA);
3993 }
3994
3995 /* Reset it, such that _bfinfdpic_assign_plt_entries() can use it to
3996 actually assign lazy PLT entries addresses. */
3997 gpinfop->g.lzplt = 0;
3998
3999 /* Save information that we're going to need to generate GOT and PLT
4000 entries. */
4001 bfinfdpic_got_initial_offset (info) = -gpinfop->gothilo.min;
4002
4003 if (get_elf_backend_data (output_bfd)->want_got_sym)
4004 elf_hash_table (info)->hgot->root.u.def.value
4005 = bfinfdpic_got_initial_offset (info);
4006
4007 if (elf_hash_table (info)->dynamic_sections_created)
4008 bfinfdpic_plt_initial_offset (info) =
4009 bfinfdpic_plt_section (info)->size;
4010
4011 htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_assign_plt_entries,
4012 gpinfop);
4013
4014 /* Allocate the PLT section contents only after
4015 _bfinfdpic_assign_plt_entries has a chance to add the size of the
4016 non-lazy PLT entries. */
4017 if (bfinfdpic_plt_section (info)->size == 0)
4018 bfinfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
4019 else
4020 {
4021 bfinfdpic_plt_section (info)->contents =
4022 (bfd_byte *) bfd_zalloc (dynobj,
4023 bfinfdpic_plt_section (info)->size);
4024 if (bfinfdpic_plt_section (info)->contents == NULL)
4025 return false;
4026 }
4027
4028 return true;
4029 }
4030
4031 /* Set the sizes of the dynamic sections. */
4032
4033 static bool
4034 elf32_bfinfdpic_size_dynamic_sections (bfd *output_bfd,
4035 struct bfd_link_info *info)
4036 {
4037 struct elf_link_hash_table *htab;
4038 bfd *dynobj;
4039 asection *s;
4040 struct _bfinfdpic_dynamic_got_plt_info gpinfo;
4041
4042 htab = elf_hash_table (info);
4043 dynobj = htab->dynobj;
4044 BFD_ASSERT (dynobj != NULL);
4045
4046 if (htab->dynamic_sections_created)
4047 {
4048 /* Set the contents of the .interp section to the interpreter. */
4049 if (bfd_link_executable (info) && !info->nointerp)
4050 {
4051 s = bfd_get_linker_section (dynobj, ".interp");
4052 BFD_ASSERT (s != NULL);
4053 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4054 s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
4055 }
4056 }
4057
4058 memset (&gpinfo, 0, sizeof (gpinfo));
4059 gpinfo.g.info = info;
4060
4061 for (;;)
4062 {
4063 htab_t relocs = bfinfdpic_relocs_info (info);
4064
4065 htab_traverse (relocs, _bfinfdpic_resolve_final_relocs_info, &relocs);
4066
4067 if (relocs == bfinfdpic_relocs_info (info))
4068 break;
4069 }
4070
4071 htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_count_got_plt_entries,
4072 &gpinfo.g);
4073
4074 /* Allocate space to save the summary information, we're going to
4075 use it if we're doing relaxations. */
4076 bfinfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
4077
4078 if (!_bfinfdpic_size_got_plt (output_bfd, &gpinfo))
4079 return false;
4080
4081 s = bfd_get_linker_section (dynobj, ".dynbss");
4082 if (s && s->size == 0)
4083 s->flags |= SEC_EXCLUDE;
4084
4085 s = bfd_get_linker_section (dynobj, ".rela.bss");
4086 if (s && s->size == 0)
4087 s->flags |= SEC_EXCLUDE;
4088
4089 return _bfd_elf_add_dynamic_tags (output_bfd, info, true);
4090 }
4091
4092 static bool
4093 elf32_bfinfdpic_always_size_sections (bfd *output_bfd,
4094 struct bfd_link_info *info)
4095 {
4096 if (!bfd_link_relocatable (info)
4097 && !bfd_elf_stack_segment_size (output_bfd, info,
4098 "__stacksize", DEFAULT_STACK_SIZE))
4099 return false;
4100
4101 return true;
4102 }
4103
4104 /* Check whether any of the relocations was optimized away, and
4105 subtract it from the relocation or fixup count. */
4106 static bool
4107 _bfinfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
4108 struct bfd_link_info *info,
4109 bool *changed)
4110 {
4111 Elf_Internal_Shdr *symtab_hdr;
4112 struct elf_link_hash_entry **sym_hashes;
4113 Elf_Internal_Rela *rel, *erel;
4114
4115 if ((sec->flags & SEC_RELOC) == 0
4116 || sec->reloc_count == 0)
4117 return true;
4118
4119 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4120 sym_hashes = elf_sym_hashes (abfd);
4121
4122 rel = elf_section_data (sec)->relocs;
4123
4124 /* Now examine each relocation. */
4125 for (erel = rel + sec->reloc_count; rel < erel; rel++)
4126 {
4127 struct elf_link_hash_entry *h;
4128 unsigned long r_symndx;
4129 struct bfinfdpic_relocs_info *picrel;
4130 struct _bfinfdpic_dynamic_got_info *dinfo;
4131
4132 if (ELF32_R_TYPE (rel->r_info) != R_BFIN_BYTE4_DATA
4133 && ELF32_R_TYPE (rel->r_info) != R_BFIN_FUNCDESC)
4134 continue;
4135
4136 if (_bfd_elf_section_offset (sec->output_section->owner,
4137 info, sec, rel->r_offset)
4138 != (bfd_vma)-1)
4139 continue;
4140
4141 r_symndx = ELF32_R_SYM (rel->r_info);
4142 if (r_symndx < symtab_hdr->sh_info)
4143 h = NULL;
4144 else
4145 {
4146 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4147 while (h->root.type == bfd_link_hash_indirect
4148 || h->root.type == bfd_link_hash_warning)
4149 h = (struct elf_link_hash_entry *)h->root.u.i.link;
4150 }
4151
4152 if (h != NULL)
4153 picrel = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info (info),
4154 abfd, h,
4155 rel->r_addend, NO_INSERT);
4156 else
4157 picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info (info),
4158 abfd, r_symndx,
4159 rel->r_addend, NO_INSERT);
4160
4161 if (! picrel)
4162 return false;
4163
4164 *changed = true;
4165 dinfo = bfinfdpic_dynamic_got_plt_info (info);
4166
4167 _bfinfdpic_count_relocs_fixups (picrel, dinfo, true);
4168 if (ELF32_R_TYPE (rel->r_info) == R_BFIN_BYTE4_DATA)
4169 picrel->relocs32--;
4170 else /* we know (ELF32_R_TYPE (rel->r_info) == R_BFIN_FUNCDESC) */
4171 picrel->relocsfd--;
4172 _bfinfdpic_count_relocs_fixups (picrel, dinfo, false);
4173 }
4174
4175 return true;
4176 }
4177
4178 static bool
4179 bfinfdpic_elf_discard_info (bfd *ibfd,
4180 struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
4181 struct bfd_link_info *info)
4182 {
4183 bool changed = false;
4184 asection *s;
4185 bfd *obfd = NULL;
4186
4187 /* Account for relaxation of .eh_frame section. */
4188 for (s = ibfd->sections; s; s = s->next)
4189 if (s->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
4190 {
4191 if (!_bfinfdpic_check_discarded_relocs (ibfd, s, info, &changed))
4192 return false;
4193 obfd = s->output_section->owner;
4194 }
4195
4196 if (changed)
4197 {
4198 struct _bfinfdpic_dynamic_got_plt_info gpinfo;
4199
4200 memset (&gpinfo, 0, sizeof (gpinfo));
4201 memcpy (&gpinfo.g, bfinfdpic_dynamic_got_plt_info (info),
4202 sizeof (gpinfo.g));
4203
4204 /* Clear GOT and PLT assignments. */
4205 htab_traverse (bfinfdpic_relocs_info (info),
4206 _bfinfdpic_reset_got_plt_entries,
4207 NULL);
4208
4209 if (!_bfinfdpic_size_got_plt (obfd, &gpinfo))
4210 return false;
4211 }
4212
4213 return true;
4214 }
4215
4216 static bool
4217 elf32_bfinfdpic_finish_dynamic_sections (bfd *output_bfd,
4218 struct bfd_link_info *info)
4219 {
4220 bfd *dynobj;
4221 asection *sdyn;
4222
4223 dynobj = elf_hash_table (info)->dynobj;
4224
4225 if (bfinfdpic_got_section (info))
4226 {
4227 BFD_ASSERT (bfinfdpic_gotrel_section (info)->size
4228 /* PR 17334: It appears that the GOT section can end up
4229 being bigger than the number of relocs. Presumably
4230 because some relocs have been deleted. A test case has
4231 yet to be generated for verify this, but in the meantime
4232 the test below has been changed from == to >= so that
4233 applications can continue to be built. */
4234 >= (bfinfdpic_gotrel_section (info)->reloc_count
4235 * sizeof (Elf32_External_Rel)));
4236
4237 if (bfinfdpic_gotfixup_section (info))
4238 {
4239 struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
4240 bfd_vma got_value = hgot->root.u.def.value
4241 + hgot->root.u.def.section->output_section->vma
4242 + hgot->root.u.def.section->output_offset;
4243
4244 _bfinfdpic_add_rofixup (output_bfd, bfinfdpic_gotfixup_section (info),
4245 got_value, 0);
4246
4247 if (bfinfdpic_gotfixup_section (info)->size
4248 != (bfinfdpic_gotfixup_section (info)->reloc_count * 4))
4249 {
4250 _bfd_error_handler
4251 ("LINKER BUG: .rofixup section size mismatch");
4252 return false;
4253 }
4254 }
4255 }
4256 if (elf_hash_table (info)->dynamic_sections_created)
4257 {
4258 BFD_ASSERT (bfinfdpic_pltrel_section (info)->size
4259 == (bfinfdpic_pltrel_section (info)->reloc_count
4260 * sizeof (Elf32_External_Rel)));
4261 }
4262
4263 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4264
4265 if (elf_hash_table (info)->dynamic_sections_created)
4266 {
4267 Elf32_External_Dyn * dyncon;
4268 Elf32_External_Dyn * dynconend;
4269
4270 BFD_ASSERT (sdyn != NULL);
4271
4272 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4273 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4274
4275 for (; dyncon < dynconend; dyncon++)
4276 {
4277 Elf_Internal_Dyn dyn;
4278
4279 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4280
4281 switch (dyn.d_tag)
4282 {
4283 default:
4284 break;
4285
4286 case DT_PLTGOT:
4287 dyn.d_un.d_ptr = bfinfdpic_got_section (info)->output_section->vma
4288 + bfinfdpic_got_section (info)->output_offset
4289 + bfinfdpic_got_initial_offset (info);
4290 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4291 break;
4292
4293 case DT_JMPREL:
4294 dyn.d_un.d_ptr = bfinfdpic_pltrel_section (info)
4295 ->output_section->vma
4296 + bfinfdpic_pltrel_section (info)->output_offset;
4297 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4298 break;
4299
4300 case DT_PLTRELSZ:
4301 dyn.d_un.d_val = bfinfdpic_pltrel_section (info)->size;
4302 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4303 break;
4304 }
4305 }
4306 }
4307
4308 return true;
4309 }
4310
4311 /* Adjust a symbol defined by a dynamic object and referenced by a
4312 regular object. */
4313
4314 static bool
4315 elf32_bfinfdpic_adjust_dynamic_symbol (struct bfd_link_info *info,
4316 struct elf_link_hash_entry *h)
4317 {
4318 bfd * dynobj;
4319
4320 dynobj = elf_hash_table (info)->dynobj;
4321
4322 /* Make sure we know what is going on here. */
4323 BFD_ASSERT (dynobj != NULL
4324 && (h->is_weakalias
4325 || (h->def_dynamic
4326 && h->ref_regular
4327 && !h->def_regular)));
4328
4329 /* If this is a weak symbol, and there is a real definition, the
4330 processor independent code will have arranged for us to see the
4331 real definition first, and we can just use the same value. */
4332 if (h->is_weakalias)
4333 {
4334 struct elf_link_hash_entry *def = weakdef (h);
4335 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4336 h->root.u.def.section = def->root.u.def.section;
4337 h->root.u.def.value = def->root.u.def.value;
4338 }
4339
4340 return true;
4341 }
4342
4343 /* Perform any actions needed for dynamic symbols. */
4344
4345 static bool
4346 elf32_bfinfdpic_finish_dynamic_symbol
4347 (bfd *output_bfd ATTRIBUTE_UNUSED,
4348 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4349 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
4350 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
4351 {
4352 return true;
4353 }
4354
4355 /* Decide whether to attempt to turn absptr or lsda encodings in
4356 shared libraries into pcrel within the given input section. */
4357
4358 static bool
4359 bfinfdpic_elf_use_relative_eh_frame
4360 (bfd *input_bfd ATTRIBUTE_UNUSED,
4361 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4362 asection *eh_frame_section ATTRIBUTE_UNUSED)
4363 {
4364 /* We can't use PC-relative encodings in FDPIC binaries, in general. */
4365 return false;
4366 }
4367
4368 /* Adjust the contents of an eh_frame_hdr section before they're output. */
4369
4370 static bfd_byte
4371 bfinfdpic_elf_encode_eh_address (bfd *abfd,
4372 struct bfd_link_info *info,
4373 asection *osec, bfd_vma offset,
4374 asection *loc_sec, bfd_vma loc_offset,
4375 bfd_vma *encoded)
4376 {
4377 struct elf_link_hash_entry *h;
4378
4379 h = elf_hash_table (info)->hgot;
4380 BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
4381
4382 if (! h || (_bfinfdpic_osec_to_segment (abfd, osec)
4383 == _bfinfdpic_osec_to_segment (abfd, loc_sec->output_section)))
4384 return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
4385 loc_sec, loc_offset, encoded);
4386
4387 BFD_ASSERT (_bfinfdpic_osec_to_segment (abfd, osec)
4388 == (_bfinfdpic_osec_to_segment
4389 (abfd, h->root.u.def.section->output_section)));
4390
4391 *encoded = osec->vma + offset
4392 - (h->root.u.def.value
4393 + h->root.u.def.section->output_section->vma
4394 + h->root.u.def.section->output_offset);
4395
4396 return DW_EH_PE_datarel | DW_EH_PE_sdata4;
4397 }
4398
4399
4400
4401 /* Look through the relocs for a section during the first phase.
4402
4403 Besides handling virtual table relocs for gc, we have to deal with
4404 all sorts of PIC-related relocations. We describe below the
4405 general plan on how to handle such relocations, even though we only
4406 collect information at this point, storing them in hash tables for
4407 perusal of later passes.
4408
4409 32 relocations are propagated to the linker output when creating
4410 position-independent output. LO16 and HI16 relocations are not
4411 supposed to be encountered in this case.
4412
4413 LABEL16 should always be resolvable by the linker, since it's only
4414 used by branches.
4415
4416 LABEL24, on the other hand, is used by calls. If it turns out that
4417 the target of a call is a dynamic symbol, a PLT entry must be
4418 created for it, which triggers the creation of a private function
4419 descriptor and, unless lazy binding is disabled, a lazy PLT entry.
4420
4421 GPREL relocations require the referenced symbol to be in the same
4422 segment as _gp, but this can only be checked later.
4423
4424 All GOT, GOTOFF and FUNCDESC relocations require a .got section to
4425 exist. LABEL24 might as well, since it may require a PLT entry,
4426 that will require a got.
4427
4428 Non-FUNCDESC GOT relocations require a GOT entry to be created
4429 regardless of whether the symbol is dynamic. However, since a
4430 global symbol that turns out to not be exported may have the same
4431 address of a non-dynamic symbol, we don't assign GOT entries at
4432 this point, such that we can share them in this case. A relocation
4433 for the GOT entry always has to be created, be it to offset a
4434 private symbol by the section load address, be it to get the symbol
4435 resolved dynamically.
4436
4437 FUNCDESC GOT relocations require a GOT entry to be created, and
4438 handled as if a FUNCDESC relocation was applied to the GOT entry in
4439 an object file.
4440
4441 FUNCDESC relocations referencing a symbol that turns out to NOT be
4442 dynamic cause a private function descriptor to be created. The
4443 FUNCDESC relocation then decays to a 32 relocation that points at
4444 the private descriptor. If the symbol is dynamic, the FUNCDESC
4445 relocation is propagated to the linker output, such that the
4446 dynamic linker creates the canonical descriptor, pointing to the
4447 dynamically-resolved definition of the function.
4448
4449 Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
4450 symbols that are assigned to the same segment as the GOT, but we
4451 can only check this later, after we know the complete set of
4452 symbols defined and/or exported.
4453
4454 FUNCDESC GOTOFF relocations require a function descriptor to be
4455 created and, unless lazy binding is disabled or the symbol is not
4456 dynamic, a lazy PLT entry. Since we can't tell at this point
4457 whether a symbol is going to be dynamic, we have to decide later
4458 whether to create a lazy PLT entry or bind the descriptor directly
4459 to the private function.
4460
4461 FUNCDESC_VALUE relocations are not supposed to be present in object
4462 files, but they may very well be simply propagated to the linker
4463 output, since they have no side effect.
4464
4465
4466 A function descriptor always requires a FUNCDESC_VALUE relocation.
4467 Whether it's in .plt.rel or not depends on whether lazy binding is
4468 enabled and on whether the referenced symbol is dynamic.
4469
4470 The existence of a lazy PLT requires the resolverStub lazy PLT
4471 entry to be present.
4472
4473
4474 As for assignment of GOT, PLT and lazy PLT entries, and private
4475 descriptors, we might do them all sequentially, but we can do
4476 better than that. For example, we can place GOT entries and
4477 private function descriptors referenced using 12-bit operands
4478 closer to the PIC register value, such that these relocations don't
4479 overflow. Those that are only referenced with LO16 relocations
4480 could come next, but we may as well place PLT-required function
4481 descriptors in the 12-bit range to make them shorter. Symbols
4482 referenced with LO16/HI16 may come next, but we may place
4483 additional function descriptors in the 16-bit range if we can
4484 reliably tell that we've already placed entries that are ever
4485 referenced with only LO16. PLT entries are therefore generated as
4486 small as possible, while not introducing relocation overflows in
4487 GOT or FUNCDESC_GOTOFF relocations. Lazy PLT entries could be
4488 generated before or after PLT entries, but not intermingled with
4489 them, such that we can have more lazy PLT entries in range for a
4490 branch to the resolverStub. The resolverStub should be emitted at
4491 the most distant location from the first lazy PLT entry such that
4492 it's still in range for a branch, or closer, if there isn't a need
4493 for so many lazy PLT entries. Additional lazy PLT entries may be
4494 emitted after the resolverStub, as long as branches are still in
4495 range. If the branch goes out of range, longer lazy PLT entries
4496 are emitted.
4497
4498 We could further optimize PLT and lazy PLT entries by giving them
4499 priority in assignment to closer-to-gr17 locations depending on the
4500 number of occurrences of references to them (assuming a function
4501 that's called more often is more important for performance, so its
4502 PLT entry should be faster), or taking hints from the compiler.
4503 Given infinite time and money... :-) */
4504
4505 static bool
4506 bfinfdpic_check_relocs (bfd *abfd, struct bfd_link_info *info,
4507 asection *sec, const Elf_Internal_Rela *relocs)
4508 {
4509 Elf_Internal_Shdr *symtab_hdr;
4510 struct elf_link_hash_entry **sym_hashes;
4511 const Elf_Internal_Rela *rel;
4512 const Elf_Internal_Rela *rel_end;
4513 bfd *dynobj;
4514 struct bfinfdpic_relocs_info *picrel;
4515
4516 if (bfd_link_relocatable (info))
4517 return true;
4518
4519 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4520 sym_hashes = elf_sym_hashes (abfd);
4521
4522 dynobj = elf_hash_table (info)->dynobj;
4523 rel_end = relocs + sec->reloc_count;
4524 for (rel = relocs; rel < rel_end; rel++)
4525 {
4526 struct elf_link_hash_entry *h;
4527 unsigned long r_symndx;
4528
4529 r_symndx = ELF32_R_SYM (rel->r_info);
4530 if (r_symndx < symtab_hdr->sh_info)
4531 h = NULL;
4532 else
4533 {
4534 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4535 while (h->root.type == bfd_link_hash_indirect
4536 || h->root.type == bfd_link_hash_warning)
4537 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4538 }
4539
4540 switch (ELF32_R_TYPE (rel->r_info))
4541 {
4542 case R_BFIN_GOT17M4:
4543 case R_BFIN_GOTHI:
4544 case R_BFIN_GOTLO:
4545 case R_BFIN_FUNCDESC_GOT17M4:
4546 case R_BFIN_FUNCDESC_GOTHI:
4547 case R_BFIN_FUNCDESC_GOTLO:
4548 case R_BFIN_GOTOFF17M4:
4549 case R_BFIN_GOTOFFHI:
4550 case R_BFIN_GOTOFFLO:
4551 case R_BFIN_FUNCDESC_GOTOFF17M4:
4552 case R_BFIN_FUNCDESC_GOTOFFHI:
4553 case R_BFIN_FUNCDESC_GOTOFFLO:
4554 case R_BFIN_FUNCDESC:
4555 case R_BFIN_FUNCDESC_VALUE:
4556 if (! IS_FDPIC (abfd))
4557 goto bad_reloc;
4558 /* Fall through. */
4559 case R_BFIN_PCREL24:
4560 case R_BFIN_PCREL24_JUMP_L:
4561 case R_BFIN_BYTE4_DATA:
4562 if (IS_FDPIC (abfd) && ! dynobj)
4563 {
4564 elf_hash_table (info)->dynobj = dynobj = abfd;
4565 if (! _bfin_create_got_section (abfd, info))
4566 return false;
4567 }
4568 if (! IS_FDPIC (abfd))
4569 {
4570 picrel = NULL;
4571 break;
4572 }
4573 if (h != NULL)
4574 {
4575 if (h->dynindx == -1)
4576 switch (ELF_ST_VISIBILITY (h->other))
4577 {
4578 case STV_INTERNAL:
4579 case STV_HIDDEN:
4580 break;
4581 default:
4582 bfd_elf_link_record_dynamic_symbol (info, h);
4583 break;
4584 }
4585 picrel
4586 = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info (info),
4587 abfd, h,
4588 rel->r_addend, INSERT);
4589 }
4590 else
4591 picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info
4592 (info), abfd, r_symndx,
4593 rel->r_addend, INSERT);
4594 if (! picrel)
4595 return false;
4596 break;
4597
4598 default:
4599 picrel = NULL;
4600 break;
4601 }
4602
4603 switch (ELF32_R_TYPE (rel->r_info))
4604 {
4605 case R_BFIN_PCREL24:
4606 case R_BFIN_PCREL24_JUMP_L:
4607 if (IS_FDPIC (abfd))
4608 picrel->call++;
4609 break;
4610
4611 case R_BFIN_FUNCDESC_VALUE:
4612 picrel->relocsfdv++;
4613 if (bfd_section_flags (sec) & SEC_ALLOC)
4614 picrel->relocs32--;
4615 /* Fall through. */
4616
4617 case R_BFIN_BYTE4_DATA:
4618 if (! IS_FDPIC (abfd))
4619 break;
4620
4621 picrel->sym++;
4622 if (bfd_section_flags (sec) & SEC_ALLOC)
4623 picrel->relocs32++;
4624 break;
4625
4626 case R_BFIN_GOT17M4:
4627 picrel->got17m4++;
4628 break;
4629
4630 case R_BFIN_GOTHI:
4631 case R_BFIN_GOTLO:
4632 picrel->gothilo++;
4633 break;
4634
4635 case R_BFIN_FUNCDESC_GOT17M4:
4636 picrel->fdgot17m4++;
4637 break;
4638
4639 case R_BFIN_FUNCDESC_GOTHI:
4640 case R_BFIN_FUNCDESC_GOTLO:
4641 picrel->fdgothilo++;
4642 break;
4643
4644 case R_BFIN_GOTOFF17M4:
4645 case R_BFIN_GOTOFFHI:
4646 case R_BFIN_GOTOFFLO:
4647 picrel->gotoff++;
4648 break;
4649
4650 case R_BFIN_FUNCDESC_GOTOFF17M4:
4651 picrel->fdgoff17m4++;
4652 break;
4653
4654 case R_BFIN_FUNCDESC_GOTOFFHI:
4655 case R_BFIN_FUNCDESC_GOTOFFLO:
4656 picrel->fdgoffhilo++;
4657 break;
4658
4659 case R_BFIN_FUNCDESC:
4660 picrel->fd++;
4661 picrel->relocsfd++;
4662 break;
4663
4664 /* This relocation describes the C++ object vtable hierarchy.
4665 Reconstruct it for later use during GC. */
4666 case R_BFIN_GNU_VTINHERIT:
4667 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4668 return false;
4669 break;
4670
4671 /* This relocation describes which C++ vtable entries are actually
4672 used. Record for later use during GC. */
4673 case R_BFIN_GNU_VTENTRY:
4674 BFD_ASSERT (h != NULL);
4675 if (h != NULL
4676 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4677 return false;
4678 break;
4679
4680 case R_BFIN_HUIMM16:
4681 case R_BFIN_LUIMM16:
4682 case R_BFIN_PCREL12_JUMP_S:
4683 case R_BFIN_PCREL10:
4684 break;
4685
4686 default:
4687 bad_reloc:
4688 _bfd_error_handler
4689 /* xgettext:c-format */
4690 (_("%pB: unsupported relocation type %#x"),
4691 abfd, (int) ELF32_R_TYPE (rel->r_info));
4692 return false;
4693 }
4694 }
4695
4696 return true;
4697 }
4698
4699 /* Set the right machine number for a Blackfin ELF file. */
4700
4701 static bool
4702 elf32_bfin_object_p (bfd *abfd)
4703 {
4704 bfd_default_set_arch_mach (abfd, bfd_arch_bfin, 0);
4705 return (((elf_elfheader (abfd)->e_flags & EF_BFIN_FDPIC) != 0)
4706 == (IS_FDPIC (abfd)));
4707 }
4708
4709 static bool
4710 elf32_bfin_set_private_flags (bfd * abfd, flagword flags)
4711 {
4712 elf_elfheader (abfd)->e_flags = flags;
4713 elf_flags_init (abfd) = true;
4714 return true;
4715 }
4716
4717 /* Display the flags field. */
4718 static bool
4719 elf32_bfin_print_private_bfd_data (bfd * abfd, void * ptr)
4720 {
4721 FILE *file = (FILE *) ptr;
4722 flagword flags;
4723
4724 BFD_ASSERT (abfd != NULL && ptr != NULL);
4725
4726 /* Print normal ELF private data. */
4727 _bfd_elf_print_private_bfd_data (abfd, ptr);
4728
4729 flags = elf_elfheader (abfd)->e_flags;
4730
4731 /* xgettext:c-format */
4732 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
4733
4734 if (flags & EF_BFIN_PIC)
4735 fprintf (file, " -fpic");
4736
4737 if (flags & EF_BFIN_FDPIC)
4738 fprintf (file, " -mfdpic");
4739
4740 fputc ('\n', file);
4741
4742 return true;
4743 }
4744
4745 /* Merge backend specific data from an object file to the output
4746 object file when linking. */
4747
4748 static bool
4749 elf32_bfin_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4750 {
4751 bfd *obfd = info->output_bfd;
4752 flagword old_flags, new_flags;
4753 bool error = false;
4754
4755 /* FIXME: What should be checked when linking shared libraries? */
4756 if ((ibfd->flags & DYNAMIC) != 0)
4757 return true;
4758
4759 new_flags = elf_elfheader (ibfd)->e_flags;
4760 old_flags = elf_elfheader (obfd)->e_flags;
4761
4762 if (new_flags & EF_BFIN_FDPIC)
4763 new_flags &= ~EF_BFIN_PIC;
4764
4765 #ifndef DEBUG
4766 if (0)
4767 #endif
4768 _bfd_error_handler
4769 ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %pB",
4770 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no", ibfd);
4771
4772 if (!elf_flags_init (obfd)) /* First call, no flags set. */
4773 {
4774 elf_flags_init (obfd) = true;
4775 elf_elfheader (obfd)->e_flags = new_flags;
4776 }
4777
4778 if (((new_flags & EF_BFIN_FDPIC) == 0) != (! IS_FDPIC (obfd)))
4779 {
4780 error = true;
4781 if (IS_FDPIC (obfd))
4782 _bfd_error_handler
4783 (_("%pB: cannot link non-fdpic object file into fdpic executable"),
4784 ibfd);
4785 else
4786 _bfd_error_handler
4787 (_("%pB: cannot link fdpic object file into non-fdpic executable"),
4788 ibfd);
4789 }
4790
4791 if (error)
4792 bfd_set_error (bfd_error_bad_value);
4793
4794 return !error;
4795 }
4796 \f
4797 /* bfin ELF linker hash entry. */
4798
4799 struct bfin_link_hash_entry
4800 {
4801 struct elf_link_hash_entry root;
4802
4803 /* Number of PC relative relocs copied for this symbol. */
4804 struct bfin_pcrel_relocs_copied *pcrel_relocs_copied;
4805 };
4806
4807 #define bfin_hash_entry(ent) ((struct bfin_link_hash_entry *) (ent))
4808
4809 static struct bfd_hash_entry *
4810 bfin_link_hash_newfunc (struct bfd_hash_entry *entry,
4811 struct bfd_hash_table *table, const char *string)
4812 {
4813 struct bfd_hash_entry *ret = entry;
4814
4815 /* Allocate the structure if it has not already been allocated by a
4816 subclass. */
4817 if (ret == NULL)
4818 ret = bfd_hash_allocate (table, sizeof (struct bfin_link_hash_entry));
4819 if (ret == NULL)
4820 return ret;
4821
4822 /* Call the allocation method of the superclass. */
4823 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
4824 if (ret != NULL)
4825 bfin_hash_entry (ret)->pcrel_relocs_copied = NULL;
4826
4827 return ret;
4828 }
4829
4830 /* Create an bfin ELF linker hash table. */
4831
4832 static struct bfd_link_hash_table *
4833 bfin_link_hash_table_create (bfd * abfd)
4834 {
4835 struct elf_link_hash_table *ret;
4836 size_t amt = sizeof (struct elf_link_hash_table);
4837
4838 ret = bfd_zmalloc (amt);
4839 if (ret == NULL)
4840 return NULL;
4841
4842 if (!_bfd_elf_link_hash_table_init (ret, abfd, bfin_link_hash_newfunc,
4843 sizeof (struct elf_link_hash_entry),
4844 BFIN_ELF_DATA))
4845 {
4846 free (ret);
4847 return NULL;
4848 }
4849
4850 return &ret->root;
4851 }
4852
4853 /* The size in bytes of an entry in the procedure linkage table. */
4854
4855 /* Finish up the dynamic sections. */
4856
4857 static bool
4858 bfin_finish_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
4859 struct bfd_link_info *info)
4860 {
4861 bfd *dynobj;
4862 asection *sdyn;
4863
4864 dynobj = elf_hash_table (info)->dynobj;
4865
4866 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4867
4868 if (elf_hash_table (info)->dynamic_sections_created)
4869 {
4870 Elf32_External_Dyn *dyncon, *dynconend;
4871
4872 BFD_ASSERT (sdyn != NULL);
4873
4874 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4875 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4876 for (; dyncon < dynconend; dyncon++)
4877 {
4878 Elf_Internal_Dyn dyn;
4879
4880 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4881
4882 }
4883
4884 }
4885 return true;
4886 }
4887
4888 /* Finish up dynamic symbol handling. We set the contents of various
4889 dynamic sections here. */
4890
4891 static bool
4892 bfin_finish_dynamic_symbol (bfd * output_bfd,
4893 struct bfd_link_info *info,
4894 struct elf_link_hash_entry *h,
4895 Elf_Internal_Sym * sym)
4896 {
4897 if (h->got.offset != (bfd_vma) - 1)
4898 {
4899 asection *sgot;
4900 asection *srela;
4901 Elf_Internal_Rela rela;
4902 bfd_byte *loc;
4903
4904 /* This symbol has an entry in the global offset table.
4905 Set it up. */
4906
4907 sgot = elf_hash_table (info)->sgot;
4908 srela = elf_hash_table (info)->srelgot;
4909 BFD_ASSERT (sgot != NULL && srela != NULL);
4910
4911 rela.r_offset = (sgot->output_section->vma
4912 + sgot->output_offset
4913 + (h->got.offset & ~(bfd_vma) 1));
4914
4915 /* If this is a -Bsymbolic link, and the symbol is defined
4916 locally, we just want to emit a RELATIVE reloc. Likewise if
4917 the symbol was forced to be local because of a version file.
4918 The entry in the global offset table will already have been
4919 initialized in the relocate_section function. */
4920 if (bfd_link_pic (info)
4921 && (info->symbolic
4922 || h->dynindx == -1 || h->forced_local) && h->def_regular)
4923 {
4924 _bfd_error_handler (_("*** check this relocation %s"),
4925 __FUNCTION__);
4926 rela.r_info = ELF32_R_INFO (0, R_BFIN_PCREL24);
4927 rela.r_addend = bfd_get_signed_32 (output_bfd,
4928 (sgot->contents
4929 +
4930 (h->got.
4931 offset & ~(bfd_vma) 1)));
4932 }
4933 else
4934 {
4935 bfd_put_32 (output_bfd, (bfd_vma) 0,
4936 sgot->contents + (h->got.offset & ~(bfd_vma) 1));
4937 rela.r_info = ELF32_R_INFO (h->dynindx, R_BFIN_GOT);
4938 rela.r_addend = 0;
4939 }
4940
4941 loc = srela->contents;
4942 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
4943 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4944 }
4945
4946 if (h->needs_copy)
4947 {
4948 BFD_ASSERT (0);
4949 }
4950 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4951 if (strcmp (h->root.root.string, "__DYNAMIC") == 0
4952 || h == elf_hash_table (info)->hgot)
4953 sym->st_shndx = SHN_ABS;
4954
4955 return true;
4956 }
4957
4958 /* Adjust a symbol defined by a dynamic object and referenced by a
4959 regular object. The current definition is in some section of the
4960 dynamic object, but we're not including those sections. We have to
4961 change the definition to something the rest of the link can
4962 understand. */
4963
4964 static bool
4965 bfin_adjust_dynamic_symbol (struct bfd_link_info *info,
4966 struct elf_link_hash_entry *h)
4967 {
4968 bfd *dynobj;
4969 asection *s;
4970 unsigned int power_of_two;
4971
4972 dynobj = elf_hash_table (info)->dynobj;
4973
4974 /* Make sure we know what is going on here. */
4975 BFD_ASSERT (dynobj != NULL
4976 && (h->needs_plt
4977 || h->is_weakalias
4978 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
4979
4980 /* If this is a function, put it in the procedure linkage table. We
4981 will fill in the contents of the procedure linkage table later,
4982 when we know the address of the .got section. */
4983 if (h->type == STT_FUNC || h->needs_plt)
4984 {
4985 BFD_ASSERT(0);
4986 }
4987
4988 /* If this is a weak symbol, and there is a real definition, the
4989 processor independent code will have arranged for us to see the
4990 real definition first, and we can just use the same value. */
4991 if (h->is_weakalias)
4992 {
4993 struct elf_link_hash_entry *def = weakdef (h);
4994 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4995 h->root.u.def.section = def->root.u.def.section;
4996 h->root.u.def.value = def->root.u.def.value;
4997 return true;
4998 }
4999
5000 /* This is a reference to a symbol defined by a dynamic object which
5001 is not a function. */
5002
5003 /* If we are creating a shared library, we must presume that the
5004 only references to the symbol are via the global offset table.
5005 For such cases we need not do anything here; the relocations will
5006 be handled correctly by relocate_section. */
5007 if (bfd_link_pic (info))
5008 return true;
5009
5010 /* We must allocate the symbol in our .dynbss section, which will
5011 become part of the .bss section of the executable. There will be
5012 an entry for this symbol in the .dynsym section. The dynamic
5013 object will contain position independent code, so all references
5014 from the dynamic object to this symbol will go through the global
5015 offset table. The dynamic linker will use the .dynsym entry to
5016 determine the address it must put in the global offset table, so
5017 both the dynamic object and the regular object will refer to the
5018 same memory location for the variable. */
5019
5020 s = bfd_get_linker_section (dynobj, ".dynbss");
5021 BFD_ASSERT (s != NULL);
5022
5023 #if 0 /* Bfin does not currently have a COPY reloc. */
5024 /* We must generate a R_BFIN_COPY reloc to tell the dynamic linker to
5025 copy the initial value out of the dynamic object and into the
5026 runtime process image. We need to remember the offset into the
5027 .rela.bss section we are going to use. */
5028 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5029 {
5030 asection *srel;
5031
5032 srel = bfd_get_linker_section (dynobj, ".rela.bss");
5033 BFD_ASSERT (srel != NULL);
5034 srel->size += sizeof (Elf32_External_Rela);
5035 h->needs_copy = 1;
5036 }
5037 #else
5038 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5039 {
5040 _bfd_error_handler (_("the bfin target does not currently support the generation of copy relocations"));
5041 return false;
5042 }
5043 #endif
5044 /* We need to figure out the alignment required for this symbol. I
5045 have no idea how ELF linkers handle this. */
5046 power_of_two = bfd_log2 (h->size);
5047 if (power_of_two > 3)
5048 power_of_two = 3;
5049
5050 /* Apply the required alignment. */
5051 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
5052 if (power_of_two > bfd_section_alignment (s))
5053 {
5054 if (!bfd_set_section_alignment (s, power_of_two))
5055 return false;
5056 }
5057
5058 /* Define the symbol as being at this point in the section. */
5059 h->root.u.def.section = s;
5060 h->root.u.def.value = s->size;
5061
5062 /* Increment the section size to make room for the symbol. */
5063 s->size += h->size;
5064
5065 return true;
5066 }
5067
5068 /* The bfin linker needs to keep track of the number of relocs that it
5069 decides to copy in check_relocs for each symbol. This is so that it
5070 can discard PC relative relocs if it doesn't need them when linking
5071 with -Bsymbolic. We store the information in a field extending the
5072 regular ELF linker hash table. */
5073
5074 /* This structure keeps track of the number of PC relative relocs we have
5075 copied for a given symbol. */
5076
5077 struct bfin_pcrel_relocs_copied
5078 {
5079 /* Next section. */
5080 struct bfin_pcrel_relocs_copied *next;
5081 /* A section in dynobj. */
5082 asection *section;
5083 /* Number of relocs copied in this section. */
5084 bfd_size_type count;
5085 };
5086
5087 /* This function is called via elf_link_hash_traverse if we are
5088 creating a shared object. In the -Bsymbolic case it discards the
5089 space allocated to copy PC relative relocs against symbols which
5090 are defined in regular objects. For the normal shared case, it
5091 discards space for pc-relative relocs that have become local due to
5092 symbol visibility changes. We allocated space for them in the
5093 check_relocs routine, but we won't fill them in in the
5094 relocate_section routine.
5095
5096 We also check whether any of the remaining relocations apply
5097 against a readonly section, and set the DF_TEXTREL flag in this
5098 case. */
5099
5100 static bool
5101 bfin_discard_copies (struct elf_link_hash_entry *h, void * inf)
5102 {
5103 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5104 struct bfin_pcrel_relocs_copied *s;
5105
5106 if (!h->def_regular || (!info->symbolic && !h->forced_local))
5107 {
5108 if ((info->flags & DF_TEXTREL) == 0)
5109 {
5110 /* Look for relocations against read-only sections. */
5111 for (s = bfin_hash_entry (h)->pcrel_relocs_copied;
5112 s != NULL; s = s->next)
5113 if ((s->section->flags & SEC_READONLY) != 0)
5114 {
5115 info->flags |= DF_TEXTREL;
5116 break;
5117 }
5118 }
5119
5120 return true;
5121 }
5122
5123 for (s = bfin_hash_entry (h)->pcrel_relocs_copied;
5124 s != NULL; s = s->next)
5125 s->section->size -= s->count * sizeof (Elf32_External_Rela);
5126
5127 return true;
5128 }
5129
5130 static bool
5131 bfin_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
5132 struct bfd_link_info *info)
5133 {
5134 bfd *dynobj;
5135 asection *s;
5136 bool relocs;
5137
5138 dynobj = elf_hash_table (info)->dynobj;
5139 BFD_ASSERT (dynobj != NULL);
5140
5141 if (elf_hash_table (info)->dynamic_sections_created)
5142 {
5143 /* Set the contents of the .interp section to the interpreter. */
5144 if (bfd_link_executable (info) && !info->nointerp)
5145 {
5146 s = bfd_get_linker_section (dynobj, ".interp");
5147 BFD_ASSERT (s != NULL);
5148 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5149 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5150 }
5151 }
5152 else
5153 {
5154 /* We may have created entries in the .rela.got section.
5155 However, if we are not creating the dynamic sections, we will
5156 not actually use these entries. Reset the size of .rela.got,
5157 which will cause it to get stripped from the output file
5158 below. */
5159 s = elf_hash_table (info)->srelgot;
5160 if (s != NULL)
5161 s->size = 0;
5162 }
5163
5164 /* If this is a -Bsymbolic shared link, then we need to discard all
5165 PC relative relocs against symbols defined in a regular object.
5166 For the normal shared case we discard the PC relative relocs
5167 against symbols that have become local due to visibility changes.
5168 We allocated space for them in the check_relocs routine, but we
5169 will not fill them in in the relocate_section routine. */
5170 if (bfd_link_pic (info))
5171 elf_link_hash_traverse (elf_hash_table (info),
5172 bfin_discard_copies, info);
5173
5174 /* The check_relocs and adjust_dynamic_symbol entry points have
5175 determined the sizes of the various dynamic sections. Allocate
5176 memory for them. */
5177 relocs = false;
5178 for (s = dynobj->sections; s != NULL; s = s->next)
5179 {
5180 const char *name;
5181 bool strip;
5182
5183 if ((s->flags & SEC_LINKER_CREATED) == 0)
5184 continue;
5185
5186 /* It's OK to base decisions on the section name, because none
5187 of the dynobj section names depend upon the input files. */
5188 name = bfd_section_name (s);
5189
5190 strip = false;
5191
5192 if (startswith (name, ".rela"))
5193 {
5194 if (s->size == 0)
5195 {
5196 /* If we don't need this section, strip it from the
5197 output file. This is mostly to handle .rela.bss and
5198 .rela.plt. We must create both sections in
5199 create_dynamic_sections, because they must be created
5200 before the linker maps input sections to output
5201 sections. The linker does that before
5202 adjust_dynamic_symbol is called, and it is that
5203 function which decides whether anything needs to go
5204 into these sections. */
5205 strip = true;
5206 }
5207 else
5208 {
5209 relocs = true;
5210
5211 /* We use the reloc_count field as a counter if we need
5212 to copy relocs into the output file. */
5213 s->reloc_count = 0;
5214 }
5215 }
5216 else if (! startswith (name, ".got"))
5217 {
5218 /* It's not one of our sections, so don't allocate space. */
5219 continue;
5220 }
5221
5222 if (strip)
5223 {
5224 s->flags |= SEC_EXCLUDE;
5225 continue;
5226 }
5227
5228 /* Allocate memory for the section contents. */
5229 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
5230 Unused entries should be reclaimed before the section's contents
5231 are written out, but at the moment this does not happen. Thus in
5232 order to prevent writing out garbage, we initialise the section's
5233 contents to zero. */
5234 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
5235 if (s->contents == NULL && s->size != 0)
5236 return false;
5237 }
5238
5239 if (elf_hash_table (info)->dynamic_sections_created)
5240 {
5241 /* Add some entries to the .dynamic section. We fill in the
5242 values later, in bfin_finish_dynamic_sections, but we
5243 must add the entries now so that we get the correct size for
5244 the .dynamic section. The DT_DEBUG entry is filled in by the
5245 dynamic linker and used by the debugger. */
5246 #define add_dynamic_entry(TAG, VAL) \
5247 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5248
5249 if (!bfd_link_pic (info))
5250 {
5251 if (!add_dynamic_entry (DT_DEBUG, 0))
5252 return false;
5253 }
5254
5255
5256 if (relocs)
5257 {
5258 if (!add_dynamic_entry (DT_RELA, 0)
5259 || !add_dynamic_entry (DT_RELASZ, 0)
5260 || !add_dynamic_entry (DT_RELAENT,
5261 sizeof (Elf32_External_Rela)))
5262 return false;
5263 }
5264
5265 if ((info->flags & DF_TEXTREL) != 0)
5266 {
5267 if (!add_dynamic_entry (DT_TEXTREL, 0))
5268 return false;
5269 }
5270 }
5271 #undef add_dynamic_entry
5272
5273 return true;
5274 }
5275 \f
5276 /* Given a .data section and a .emreloc in-memory section, store
5277 relocation information into the .emreloc section which can be
5278 used at runtime to relocate the section. This is called by the
5279 linker when the --embedded-relocs switch is used. This is called
5280 after the add_symbols entry point has been called for all the
5281 objects, and before the final_link entry point is called. */
5282
5283 bool
5284 bfd_bfin_elf32_create_embedded_relocs (bfd *abfd,
5285 struct bfd_link_info *info,
5286 asection *datasec,
5287 asection *relsec,
5288 char **errmsg)
5289 {
5290 Elf_Internal_Shdr *symtab_hdr;
5291 Elf_Internal_Sym *isymbuf = NULL;
5292 Elf_Internal_Rela *internal_relocs = NULL;
5293 Elf_Internal_Rela *irel, *irelend;
5294 bfd_byte *p;
5295 bfd_size_type amt;
5296
5297 BFD_ASSERT (! bfd_link_relocatable (info));
5298
5299 *errmsg = NULL;
5300
5301 if (datasec->reloc_count == 0)
5302 return true;
5303
5304 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5305
5306 /* Get a copy of the native relocations. */
5307 internal_relocs = (_bfd_elf_link_read_relocs
5308 (abfd, datasec, NULL, (Elf_Internal_Rela *) NULL,
5309 info->keep_memory));
5310 if (internal_relocs == NULL)
5311 goto error_return;
5312
5313 amt = (bfd_size_type) datasec->reloc_count * 12;
5314 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
5315 if (relsec->contents == NULL)
5316 goto error_return;
5317
5318 p = relsec->contents;
5319
5320 irelend = internal_relocs + datasec->reloc_count;
5321 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
5322 {
5323 asection *targetsec;
5324
5325 /* We are going to write a four byte longword into the runtime
5326 reloc section. The longword will be the address in the data
5327 section which must be relocated. It is followed by the name
5328 of the target section NUL-padded or truncated to 8
5329 characters. */
5330
5331 /* We can only relocate absolute longword relocs at run time. */
5332 if (ELF32_R_TYPE (irel->r_info) != (int) R_BFIN_BYTE4_DATA)
5333 {
5334 *errmsg = _("unsupported relocation type");
5335 bfd_set_error (bfd_error_bad_value);
5336 goto error_return;
5337 }
5338
5339 /* Get the target section referred to by the reloc. */
5340 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
5341 {
5342 /* A local symbol. */
5343 Elf_Internal_Sym *isym;
5344
5345 /* Read this BFD's local symbols if we haven't done so already. */
5346 if (isymbuf == NULL)
5347 {
5348 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5349 if (isymbuf == NULL)
5350 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
5351 symtab_hdr->sh_info, 0,
5352 NULL, NULL, NULL);
5353 if (isymbuf == NULL)
5354 goto error_return;
5355 }
5356
5357 isym = isymbuf + ELF32_R_SYM (irel->r_info);
5358 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5359 }
5360 else
5361 {
5362 unsigned long indx;
5363 struct elf_link_hash_entry *h;
5364
5365 /* An external symbol. */
5366 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
5367 h = elf_sym_hashes (abfd)[indx];
5368 BFD_ASSERT (h != NULL);
5369 if (h->root.type == bfd_link_hash_defined
5370 || h->root.type == bfd_link_hash_defweak)
5371 targetsec = h->root.u.def.section;
5372 else
5373 targetsec = NULL;
5374 }
5375
5376 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
5377 memset (p + 4, 0, 8);
5378 if (targetsec != NULL)
5379 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
5380 }
5381
5382 if (symtab_hdr->contents != (unsigned char *) isymbuf)
5383 free (isymbuf);
5384 if (elf_section_data (datasec)->relocs != internal_relocs)
5385 free (internal_relocs);
5386 return true;
5387
5388 error_return:
5389 if (symtab_hdr->contents != (unsigned char *) isymbuf)
5390 free (isymbuf);
5391 if (elf_section_data (datasec)->relocs != internal_relocs)
5392 free (internal_relocs);
5393 return false;
5394 }
5395
5396 struct bfd_elf_special_section const elf32_bfin_special_sections[] =
5397 {
5398 { ".l1.text", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5399 { ".l1.data", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
5400 { NULL, 0, 0, 0, 0 }
5401 };
5402
5403 \f
5404 #define TARGET_LITTLE_SYM bfin_elf32_vec
5405 #define TARGET_LITTLE_NAME "elf32-bfin"
5406 #define ELF_ARCH bfd_arch_bfin
5407 #define ELF_TARGET_ID BFIN_ELF_DATA
5408 #define ELF_MACHINE_CODE EM_BLACKFIN
5409 #define ELF_MAXPAGESIZE 0x1000
5410 #define elf_symbol_leading_char '_'
5411
5412 #define bfd_elf32_bfd_reloc_type_lookup bfin_bfd_reloc_type_lookup
5413 #define bfd_elf32_bfd_reloc_name_lookup \
5414 bfin_bfd_reloc_name_lookup
5415 #define elf_info_to_howto bfin_info_to_howto
5416 #define elf_info_to_howto_rel NULL
5417 #define elf_backend_object_p elf32_bfin_object_p
5418
5419 #define bfd_elf32_bfd_is_local_label_name \
5420 bfin_is_local_label_name
5421
5422 #define elf_backend_create_dynamic_sections \
5423 _bfd_elf_create_dynamic_sections
5424 #define bfd_elf32_bfd_link_hash_table_create \
5425 bfin_link_hash_table_create
5426 #define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
5427
5428 #define elf_backend_check_relocs bfin_check_relocs
5429 #define elf_backend_adjust_dynamic_symbol \
5430 bfin_adjust_dynamic_symbol
5431 #define elf_backend_size_dynamic_sections \
5432 bfin_size_dynamic_sections
5433 #define elf_backend_relocate_section bfin_relocate_section
5434 #define elf_backend_finish_dynamic_symbol \
5435 bfin_finish_dynamic_symbol
5436 #define elf_backend_finish_dynamic_sections \
5437 bfin_finish_dynamic_sections
5438 #define elf_backend_gc_mark_hook bfin_gc_mark_hook
5439 #define bfd_elf32_bfd_merge_private_bfd_data \
5440 elf32_bfin_merge_private_bfd_data
5441 #define bfd_elf32_bfd_set_private_flags \
5442 elf32_bfin_set_private_flags
5443 #define bfd_elf32_bfd_print_private_bfd_data \
5444 elf32_bfin_print_private_bfd_data
5445 #define elf_backend_final_write_processing \
5446 elf32_bfin_final_write_processing
5447 #define elf_backend_reloc_type_class elf32_bfin_reloc_type_class
5448 #define elf_backend_stack_align 8
5449 #define elf_backend_can_gc_sections 1
5450 #define elf_backend_special_sections elf32_bfin_special_sections
5451 #define elf_backend_can_refcount 1
5452 #define elf_backend_want_got_plt 0
5453 #define elf_backend_plt_readonly 1
5454 #define elf_backend_want_plt_sym 0
5455 #define elf_backend_got_header_size 12
5456 #define elf_backend_rela_normal 1
5457
5458 #include "elf32-target.h"
5459
5460 #undef TARGET_LITTLE_SYM
5461 #define TARGET_LITTLE_SYM bfin_elf32_fdpic_vec
5462 #undef TARGET_LITTLE_NAME
5463 #define TARGET_LITTLE_NAME "elf32-bfinfdpic"
5464 #undef elf32_bed
5465 #define elf32_bed elf32_bfinfdpic_bed
5466
5467 #undef elf_backend_got_header_size
5468 #define elf_backend_got_header_size 0
5469
5470 #undef elf_backend_relocate_section
5471 #define elf_backend_relocate_section bfinfdpic_relocate_section
5472 #undef elf_backend_check_relocs
5473 #define elf_backend_check_relocs bfinfdpic_check_relocs
5474
5475 #undef bfd_elf32_bfd_link_hash_table_create
5476 #define bfd_elf32_bfd_link_hash_table_create \
5477 bfinfdpic_elf_link_hash_table_create
5478 #undef elf_backend_always_size_sections
5479 #define elf_backend_always_size_sections \
5480 elf32_bfinfdpic_always_size_sections
5481
5482 #undef elf_backend_create_dynamic_sections
5483 #define elf_backend_create_dynamic_sections \
5484 elf32_bfinfdpic_create_dynamic_sections
5485 #undef elf_backend_adjust_dynamic_symbol
5486 #define elf_backend_adjust_dynamic_symbol \
5487 elf32_bfinfdpic_adjust_dynamic_symbol
5488 #undef elf_backend_size_dynamic_sections
5489 #define elf_backend_size_dynamic_sections \
5490 elf32_bfinfdpic_size_dynamic_sections
5491 #undef elf_backend_finish_dynamic_symbol
5492 #define elf_backend_finish_dynamic_symbol \
5493 elf32_bfinfdpic_finish_dynamic_symbol
5494 #undef elf_backend_finish_dynamic_sections
5495 #define elf_backend_finish_dynamic_sections \
5496 elf32_bfinfdpic_finish_dynamic_sections
5497
5498 #undef elf_backend_discard_info
5499 #define elf_backend_discard_info \
5500 bfinfdpic_elf_discard_info
5501 #undef elf_backend_can_make_relative_eh_frame
5502 #define elf_backend_can_make_relative_eh_frame \
5503 bfinfdpic_elf_use_relative_eh_frame
5504 #undef elf_backend_can_make_lsda_relative_eh_frame
5505 #define elf_backend_can_make_lsda_relative_eh_frame \
5506 bfinfdpic_elf_use_relative_eh_frame
5507 #undef elf_backend_encode_eh_address
5508 #define elf_backend_encode_eh_address \
5509 bfinfdpic_elf_encode_eh_address
5510
5511 #undef elf_backend_may_use_rel_p
5512 #define elf_backend_may_use_rel_p 1
5513 #undef elf_backend_may_use_rela_p
5514 #define elf_backend_may_use_rela_p 1
5515 /* We use REL for dynamic relocations only. */
5516 #undef elf_backend_default_use_rela_p
5517 #define elf_backend_default_use_rela_p 1
5518
5519 #undef elf_backend_omit_section_dynsym
5520 #define elf_backend_omit_section_dynsym _bfinfdpic_link_omit_section_dynsym
5521
5522 #include "elf32-target.h"