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