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