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