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