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