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