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