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