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