Home | History | Annotate | Line # | Download | only in bfd
      1 /* FRV-specific support for 32-bit ELF.
      2    Copyright (C) 2002-2024 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/frv.h"
     26 #include "dwarf2.h"
     27 #include "hashtab.h"
     28 #include "libiberty.h"
     29 
     30 /* Forward declarations.  */
     31 
     32 
     33 static reloc_howto_type elf32_frv_howto_table [] =
     34 {
     35   /* This reloc does nothing.  */
     36   HOWTO (R_FRV_NONE,		/* type */
     37 	 0,			/* rightshift */
     38 	 0,			/* size */
     39 	 0,			/* bitsize */
     40 	 false,			/* pc_relative */
     41 	 0,			/* bitpos */
     42 	 complain_overflow_dont, /* complain_on_overflow */
     43 	 bfd_elf_generic_reloc,	/* special_function */
     44 	 "R_FRV_NONE",		/* name */
     45 	 false,			/* partial_inplace */
     46 	 0,			/* src_mask */
     47 	 0,			/* dst_mask */
     48 	 false),		/* pcrel_offset */
     49 
     50   /* A 32 bit absolute relocation.  */
     51   HOWTO (R_FRV_32,		/* type */
     52 	 0,			/* rightshift */
     53 	 4,			/* size */
     54 	 32,			/* bitsize */
     55 	 false,			/* pc_relative */
     56 	 0,			/* bitpos */
     57 	 complain_overflow_bitfield, /* complain_on_overflow */
     58 	 bfd_elf_generic_reloc,	/* special_function */
     59 	 "R_FRV_32",		/* name */
     60 	 false,			/* partial_inplace */
     61 	 0xffffffff,		/* src_mask */
     62 	 0xffffffff,		/* dst_mask */
     63 	 false),		/* pcrel_offset */
     64 
     65   /* A 16 bit pc-relative relocation.  */
     66   HOWTO (R_FRV_LABEL16,		/* type */
     67 	 2,			/* rightshift */
     68 	 4,			/* size */
     69 	 16,			/* bitsize */
     70 	 true,			/* pc_relative */
     71 	 0,			/* bitpos */
     72 	 complain_overflow_signed, /* complain_on_overflow */
     73 	 bfd_elf_generic_reloc,	/* special_function */
     74 	 "R_FRV_LABEL16",	/* name */
     75 	 false,			/* partial_inplace */
     76 	 0xffff,		/* src_mask */
     77 	 0xffff,		/* dst_mask */
     78 	 true),			/* pcrel_offset */
     79 
     80   /* A 24-bit pc-relative relocation.  */
     81   HOWTO (R_FRV_LABEL24,		/* type */
     82 	 2,			/* rightshift */
     83 	 4,			/* size */
     84 	 26,			/* bitsize */
     85 	 true,			/* pc_relative */
     86 	 0,			/* bitpos */
     87 	 complain_overflow_bitfield, /* complain_on_overflow */
     88 	 bfd_elf_generic_reloc,	/* special_function */
     89 	 "R_FRV_LABEL24",	/* name */
     90 	 false,			/* partial_inplace */
     91 	 0x7e03ffff,		/* src_mask */
     92 	 0x7e03ffff,		/* dst_mask */
     93 	 true),			/* pcrel_offset */
     94 
     95   HOWTO (R_FRV_LO16,		/* type */
     96 	 0,			/* rightshift */
     97 	 4,			/* size */
     98 	 16,			/* bitsize */
     99 	 false,			/* pc_relative */
    100 	 0,			/* bitpos */
    101 	 complain_overflow_dont, /* complain_on_overflow */
    102 	 bfd_elf_generic_reloc,	/* special_function */
    103 	 "R_FRV_LO16",		/* name */
    104 	 false,			/* partial_inplace */
    105 	 0xffff,		/* src_mask */
    106 	 0xffff,		/* dst_mask */
    107 	 false),		/* pcrel_offset */
    108 
    109   HOWTO (R_FRV_HI16,		/* type */
    110 	 0,			/* rightshift */
    111 	 4,			/* size */
    112 	 16,			/* bitsize */
    113 	 false,			/* pc_relative */
    114 	 0,			/* bitpos */
    115 	 complain_overflow_dont, /* complain_on_overflow */
    116 	 bfd_elf_generic_reloc,	/* special_function */
    117 	 "R_FRV_HI16",		/* name */
    118 	 false,			/* partial_inplace */
    119 	 0xffff,		/* src_mask */
    120 	 0xffff,		/* dst_mask */
    121 	 false),		/* pcrel_offset */
    122 
    123   HOWTO (R_FRV_GPREL12,		/* type */
    124 	 0,			/* rightshift */
    125 	 4,			/* size */
    126 	 12,			/* bitsize */
    127 	 false,			/* pc_relative */
    128 	 0,			/* bitpos */
    129 	 complain_overflow_dont, /* complain_on_overflow */
    130 	 bfd_elf_generic_reloc,	/* special_function */
    131 	 "R_FRV_GPREL12",	/* name */
    132 	 false,			/* partial_inplace */
    133 	 0xfff,			/* src_mask */
    134 	 0xfff,			/* dst_mask */
    135 	 false),		/* pcrel_offset */
    136 
    137   HOWTO (R_FRV_GPRELU12,	/* type */
    138 	 0,			/* rightshift */
    139 	 4,			/* size */
    140 	 12,			/* bitsize */
    141 	 false,			/* pc_relative */
    142 	 0,			/* bitpos */
    143 	 complain_overflow_dont, /* complain_on_overflow */
    144 	 bfd_elf_generic_reloc,	/* special_function */
    145 	 "R_FRV_GPRELU12",	/* name */
    146 	 false,			/* partial_inplace */
    147 	 0xfff,			/* src_mask */
    148 	 0x3f03f,		/* dst_mask */
    149 	 false),		/* pcrel_offset */
    150 
    151   HOWTO (R_FRV_GPREL32,		/* type */
    152 	 0,			/* rightshift */
    153 	 4,			/* size */
    154 	 32,			/* bitsize */
    155 	 false,			/* pc_relative */
    156 	 0,			/* bitpos */
    157 	 complain_overflow_dont, /* complain_on_overflow */
    158 	 bfd_elf_generic_reloc,	/* special_function */
    159 	 "R_FRV_GPREL32",	/* name */
    160 	 false,			/* partial_inplace */
    161 	 0xffffffff,		/* src_mask */
    162 	 0xffffffff,		/* dst_mask */
    163 	 false),		/* pcrel_offset */
    164 
    165   HOWTO (R_FRV_GPRELHI,		/* type */
    166 	 0,			/* rightshift */
    167 	 4,			/* size */
    168 	 16,			/* bitsize */
    169 	 false,			/* pc_relative */
    170 	 0,			/* bitpos */
    171 	 complain_overflow_dont, /* complain_on_overflow */
    172 	 bfd_elf_generic_reloc,	/* special_function */
    173 	 "R_FRV_GPRELHI",	/* name */
    174 	 false,			/* partial_inplace */
    175 	 0xffff,		/* src_mask */
    176 	 0xffff,		/* dst_mask */
    177 	 false),		/* pcrel_offset */
    178 
    179   HOWTO (R_FRV_GPRELLO,		/* type */
    180 	 0,			/* rightshift */
    181 	 4,			/* size */
    182 	 16,			/* bitsize */
    183 	 false,			/* pc_relative */
    184 	 0,			/* bitpos */
    185 	 complain_overflow_dont, /* complain_on_overflow */
    186 	 bfd_elf_generic_reloc,	/* special_function */
    187 	 "R_FRV_GPRELLO",	/* name */
    188 	 false,			/* partial_inplace */
    189 	 0xffff,		/* src_mask */
    190 	 0xffff,		/* dst_mask */
    191 	 false),		/* pcrel_offset */
    192 
    193   /* A 12-bit signed operand with the GOT offset for the address of
    194      the symbol.  */
    195   HOWTO (R_FRV_GOT12,		/* type */
    196 	 0,			/* rightshift */
    197 	 4,			/* size */
    198 	 12,			/* bitsize */
    199 	 false,			/* pc_relative */
    200 	 0,			/* bitpos */
    201 	 complain_overflow_signed, /* complain_on_overflow */
    202 	 bfd_elf_generic_reloc,	/* special_function */
    203 	 "R_FRV_GOT12",		/* name */
    204 	 false,			/* partial_inplace */
    205 	 0xfff,			/* src_mask */
    206 	 0xfff,			/* dst_mask */
    207 	 false),		/* pcrel_offset */
    208 
    209   /* The upper 16 bits of the GOT offset for the address of the
    210      symbol.  */
    211   HOWTO (R_FRV_GOTHI,		/* type */
    212 	 0,			/* rightshift */
    213 	 4,			/* size */
    214 	 16,			/* bitsize */
    215 	 false,			/* pc_relative */
    216 	 0,			/* bitpos */
    217 	 complain_overflow_dont, /* complain_on_overflow */
    218 	 bfd_elf_generic_reloc,	/* special_function */
    219 	 "R_FRV_GOTHI",		/* name */
    220 	 false,			/* partial_inplace */
    221 	 0xffff,		/* src_mask */
    222 	 0xffff,		/* dst_mask */
    223 	 false),		/* pcrel_offset */
    224 
    225   /* The lower 16 bits of the GOT offset for the address of the
    226      symbol.  */
    227   HOWTO (R_FRV_GOTLO,		/* type */
    228 	 0,			/* rightshift */
    229 	 4,			/* size */
    230 	 16,			/* bitsize */
    231 	 false,			/* pc_relative */
    232 	 0,			/* bitpos */
    233 	 complain_overflow_dont, /* complain_on_overflow */
    234 	 bfd_elf_generic_reloc,	/* special_function */
    235 	 "R_FRV_GOTLO",		/* name */
    236 	 false,			/* partial_inplace */
    237 	 0xffff,		/* src_mask */
    238 	 0xffff,		/* dst_mask */
    239 	 false),		/* pcrel_offset */
    240 
    241   /* The 32-bit address of the canonical descriptor of a function.  */
    242   HOWTO (R_FRV_FUNCDESC,	/* type */
    243 	 0,			/* rightshift */
    244 	 4,			/* size */
    245 	 32,			/* bitsize */
    246 	 false,			/* pc_relative */
    247 	 0,			/* bitpos */
    248 	 complain_overflow_bitfield, /* complain_on_overflow */
    249 	 bfd_elf_generic_reloc,	/* special_function */
    250 	 "R_FRV_FUNCDESC",	/* name */
    251 	 false,			/* partial_inplace */
    252 	 0xffffffff,		/* src_mask */
    253 	 0xffffffff,		/* dst_mask */
    254 	 false),		/* pcrel_offset */
    255 
    256   /* A 12-bit signed operand with the GOT offset for the address of
    257      canonical descriptor of a function.  */
    258   HOWTO (R_FRV_FUNCDESC_GOT12,	/* type */
    259 	 0,			/* rightshift */
    260 	 4,			/* size */
    261 	 12,			/* bitsize */
    262 	 false,			/* pc_relative */
    263 	 0,			/* bitpos */
    264 	 complain_overflow_signed, /* complain_on_overflow */
    265 	 bfd_elf_generic_reloc,	/* special_function */
    266 	 "R_FRV_FUNCDESC_GOT12", /* name */
    267 	 false,			/* partial_inplace */
    268 	 0xfff,			/* src_mask */
    269 	 0xfff,			/* dst_mask */
    270 	 false),		/* pcrel_offset */
    271 
    272   /* The upper 16 bits of the GOT offset for the address of the
    273      canonical descriptor of a function.  */
    274   HOWTO (R_FRV_FUNCDESC_GOTHI,	/* type */
    275 	 0,			/* rightshift */
    276 	 4,			/* size */
    277 	 16,			/* bitsize */
    278 	 false,			/* pc_relative */
    279 	 0,			/* bitpos */
    280 	 complain_overflow_dont, /* complain_on_overflow */
    281 	 bfd_elf_generic_reloc,	/* special_function */
    282 	 "R_FRV_FUNCDESC_GOTHI", /* name */
    283 	 false,			/* partial_inplace */
    284 	 0xffff,		/* src_mask */
    285 	 0xffff,		/* dst_mask */
    286 	 false),		/* pcrel_offset */
    287 
    288   /* The lower 16 bits of the GOT offset for the address of the
    289      canonical descriptor of a function.  */
    290   HOWTO (R_FRV_FUNCDESC_GOTLO,	/* type */
    291 	 0,			/* rightshift */
    292 	 4,			/* size */
    293 	 16,			/* bitsize */
    294 	 false,			/* pc_relative */
    295 	 0,			/* bitpos */
    296 	 complain_overflow_dont, /* complain_on_overflow */
    297 	 bfd_elf_generic_reloc,	/* special_function */
    298 	 "R_FRV_FUNCDESC_GOTLO", /* name */
    299 	 false,			/* partial_inplace */
    300 	 0xffff,		/* src_mask */
    301 	 0xffff,		/* dst_mask */
    302 	 false),		/* pcrel_offset */
    303 
    304   /* The 64-bit descriptor of a function.  */
    305   HOWTO (R_FRV_FUNCDESC_VALUE,	/* type */
    306 	 0,			/* rightshift */
    307 	 4,			/* size */
    308 	 64,			/* bitsize */
    309 	 false,			/* pc_relative */
    310 	 0,			/* bitpos */
    311 	 complain_overflow_bitfield, /* complain_on_overflow */
    312 	 bfd_elf_generic_reloc,	/* special_function */
    313 	 "R_FRV_FUNCDESC_VALUE", /* name */
    314 	 false,			/* partial_inplace */
    315 	 0xffffffff,		/* src_mask */
    316 	 0xffffffff,		/* dst_mask */
    317 	 false),		/* pcrel_offset */
    318 
    319   /* A 12-bit signed operand with the GOT offset for the address of
    320      canonical descriptor of a function.  */
    321   HOWTO (R_FRV_FUNCDESC_GOTOFF12, /* type */
    322 	 0,			/* rightshift */
    323 	 4,			/* size */
    324 	 12,			/* bitsize */
    325 	 false,			/* pc_relative */
    326 	 0,			/* bitpos */
    327 	 complain_overflow_signed, /* complain_on_overflow */
    328 	 bfd_elf_generic_reloc,	/* special_function */
    329 	 "R_FRV_FUNCDESC_GOTOFF12", /* name */
    330 	 false,			/* partial_inplace */
    331 	 0xfff,			/* src_mask */
    332 	 0xfff,			/* dst_mask */
    333 	 false),		/* pcrel_offset */
    334 
    335   /* The upper 16 bits of the GOT offset for the address of the
    336      canonical descriptor of a function.  */
    337   HOWTO (R_FRV_FUNCDESC_GOTOFFHI, /* type */
    338 	 0,			/* rightshift */
    339 	 4,			/* size */
    340 	 16,			/* bitsize */
    341 	 false,			/* pc_relative */
    342 	 0,			/* bitpos */
    343 	 complain_overflow_dont, /* complain_on_overflow */
    344 	 bfd_elf_generic_reloc,	/* special_function */
    345 	 "R_FRV_FUNCDESC_GOTOFFHI", /* name */
    346 	 false,			/* partial_inplace */
    347 	 0xffff,		/* src_mask */
    348 	 0xffff,		/* dst_mask */
    349 	 false),		/* pcrel_offset */
    350 
    351   /* The lower 16 bits of the GOT offset for the address of the
    352      canonical descriptor of a function.  */
    353   HOWTO (R_FRV_FUNCDESC_GOTOFFLO, /* type */
    354 	 0,			/* rightshift */
    355 	 4,			/* size */
    356 	 16,			/* bitsize */
    357 	 false,			/* pc_relative */
    358 	 0,			/* bitpos */
    359 	 complain_overflow_dont, /* complain_on_overflow */
    360 	 bfd_elf_generic_reloc,	/* special_function */
    361 	 "R_FRV_FUNCDESC_GOTOFFLO", /* name */
    362 	 false,			/* partial_inplace */
    363 	 0xffff,		/* src_mask */
    364 	 0xffff,		/* dst_mask */
    365 	 false),		/* pcrel_offset */
    366 
    367   /* A 12-bit signed operand with the GOT offset for the address of
    368      the symbol.  */
    369   HOWTO (R_FRV_GOTOFF12,	/* type */
    370 	 0,			/* rightshift */
    371 	 4,			/* size */
    372 	 12,			/* bitsize */
    373 	 false,			/* pc_relative */
    374 	 0,			/* bitpos */
    375 	 complain_overflow_signed, /* complain_on_overflow */
    376 	 bfd_elf_generic_reloc,	/* special_function */
    377 	 "R_FRV_GOTOFF12",	/* name */
    378 	 false,			/* partial_inplace */
    379 	 0xfff,			/* src_mask */
    380 	 0xfff,			/* dst_mask */
    381 	 false),		/* pcrel_offset */
    382 
    383   /* The upper 16 bits of the GOT offset for the address of the
    384      symbol.  */
    385   HOWTO (R_FRV_GOTOFFHI,	/* type */
    386 	 0,			/* rightshift */
    387 	 4,			/* size */
    388 	 16,			/* bitsize */
    389 	 false,			/* pc_relative */
    390 	 0,			/* bitpos */
    391 	 complain_overflow_dont, /* complain_on_overflow */
    392 	 bfd_elf_generic_reloc,	/* special_function */
    393 	 "R_FRV_GOTOFFHI",	/* name */
    394 	 false,			/* partial_inplace */
    395 	 0xffff,		/* src_mask */
    396 	 0xffff,		/* dst_mask */
    397 	 false),		/* pcrel_offset */
    398 
    399   /* The lower 16 bits of the GOT offset for the address of the
    400      symbol.  */
    401   HOWTO (R_FRV_GOTOFFLO,	/* type */
    402 	 0,			/* rightshift */
    403 	 4,			/* size */
    404 	 16,			/* bitsize */
    405 	 false,			/* pc_relative */
    406 	 0,			/* bitpos */
    407 	 complain_overflow_dont, /* complain_on_overflow */
    408 	 bfd_elf_generic_reloc,	/* special_function */
    409 	 "R_FRV_GOTOFFLO",	/* name */
    410 	 false,			/* partial_inplace */
    411 	 0xffff,		/* src_mask */
    412 	 0xffff,		/* dst_mask */
    413 	 false),		/* pcrel_offset */
    414 
    415   /* A 24-bit pc-relative relocation referencing the TLS PLT entry for
    416      a thread-local symbol.  If the symbol number is 0, it refers to
    417      the module.  */
    418   HOWTO (R_FRV_GETTLSOFF,	/* type */
    419 	 2,			/* rightshift */
    420 	 4,			/* size */
    421 	 26,			/* bitsize */
    422 	 true,			/* pc_relative */
    423 	 0,			/* bitpos */
    424 	 complain_overflow_bitfield, /* complain_on_overflow */
    425 	 bfd_elf_generic_reloc,	/* special_function */
    426 	 "R_FRV_GETTLSOFF",	/* name */
    427 	 false,			/* partial_inplace */
    428 	 0x7e03ffff,		/* src_mask */
    429 	 0x7e03ffff,		/* dst_mask */
    430 	 true),			/* pcrel_offset */
    431 
    432   /* A 64-bit TLS descriptor for a symbol.  This relocation is only
    433      valid as a REL, dynamic relocation.  */
    434   HOWTO (R_FRV_TLSDESC_VALUE,	/* type */
    435 	 0,			/* rightshift */
    436 	 4,			/* size */
    437 	 64,			/* bitsize */
    438 	 false,			/* pc_relative */
    439 	 0,			/* bitpos */
    440 	 complain_overflow_bitfield, /* complain_on_overflow */
    441 	 bfd_elf_generic_reloc,	/* special_function */
    442 	 "R_FRV_TLSDESC_VALUE", /* name */
    443 	 false,			/* partial_inplace */
    444 	 0xffffffff,		/* src_mask */
    445 	 0xffffffff,		/* dst_mask */
    446 	 false),		/* pcrel_offset */
    447 
    448   /* A 12-bit signed operand with the GOT offset for the TLS
    449      descriptor of the symbol.  */
    450   HOWTO (R_FRV_GOTTLSDESC12,	/* type */
    451 	 0,			/* rightshift */
    452 	 4,			/* size */
    453 	 12,			/* bitsize */
    454 	 false,			/* pc_relative */
    455 	 0,			/* bitpos */
    456 	 complain_overflow_signed, /* complain_on_overflow */
    457 	 bfd_elf_generic_reloc,	/* special_function */
    458 	 "R_FRV_GOTTLSDESC12",	/* name */
    459 	 false,			/* partial_inplace */
    460 	 0xfff,			/* src_mask */
    461 	 0xfff,			/* dst_mask */
    462 	 false),		/* pcrel_offset */
    463 
    464   /* The upper 16 bits of the GOT offset for the TLS descriptor of the
    465      symbol.  */
    466   HOWTO (R_FRV_GOTTLSDESCHI,	/* type */
    467 	 0,			/* rightshift */
    468 	 4,			/* size */
    469 	 16,			/* bitsize */
    470 	 false,			/* pc_relative */
    471 	 0,			/* bitpos */
    472 	 complain_overflow_dont, /* complain_on_overflow */
    473 	 bfd_elf_generic_reloc,	/* special_function */
    474 	 "R_FRV_GOTTLSDESCHI",	/* name */
    475 	 false,			/* partial_inplace */
    476 	 0xffff,		/* src_mask */
    477 	 0xffff,		/* dst_mask */
    478 	 false),		/* pcrel_offset */
    479 
    480   /* The lower 16 bits of the GOT offset for the TLS descriptor of the
    481      symbol.  */
    482   HOWTO (R_FRV_GOTTLSDESCLO,	/* type */
    483 	 0,			/* rightshift */
    484 	 4,			/* size */
    485 	 16,			/* bitsize */
    486 	 false,			/* pc_relative */
    487 	 0,			/* bitpos */
    488 	 complain_overflow_dont, /* complain_on_overflow */
    489 	 bfd_elf_generic_reloc,	/* special_function */
    490 	 "R_FRV_GOTTLSDESCLO",	/* name */
    491 	 false,			/* partial_inplace */
    492 	 0xffff,		/* src_mask */
    493 	 0xffff,		/* dst_mask */
    494 	 false),		/* pcrel_offset */
    495 
    496   /* A 12-bit signed operand with the offset from the module base
    497      address to the thread-local symbol address.  */
    498   HOWTO (R_FRV_TLSMOFF12,	 /* type */
    499 	 0,			/* rightshift */
    500 	 4,			/* size */
    501 	 12,			/* bitsize */
    502 	 false,			/* pc_relative */
    503 	 0,			/* bitpos */
    504 	 complain_overflow_signed, /* complain_on_overflow */
    505 	 bfd_elf_generic_reloc,	/* special_function */
    506 	 "R_FRV_TLSMOFF12",	/* name */
    507 	 false,			/* partial_inplace */
    508 	 0xfff,			/* src_mask */
    509 	 0xfff,			/* dst_mask */
    510 	 false),		/* pcrel_offset */
    511 
    512   /* The upper 16 bits of the offset from the module base address to
    513      the thread-local symbol address.  */
    514   HOWTO (R_FRV_TLSMOFFHI,	/* type */
    515 	 0,			/* rightshift */
    516 	 4,			/* size */
    517 	 16,			/* bitsize */
    518 	 false,			/* pc_relative */
    519 	 0,			/* bitpos */
    520 	 complain_overflow_dont, /* complain_on_overflow */
    521 	 bfd_elf_generic_reloc,	/* special_function */
    522 	 "R_FRV_TLSMOFFHI",	/* name */
    523 	 false,			/* partial_inplace */
    524 	 0xffff,		/* src_mask */
    525 	 0xffff,		/* dst_mask */
    526 	 false),		/* pcrel_offset */
    527 
    528   /* The lower 16 bits of the offset from the module base address to
    529      the thread-local symbol address.  */
    530   HOWTO (R_FRV_TLSMOFFLO,	/* type */
    531 	 0,			/* rightshift */
    532 	 4,			/* size */
    533 	 16,			/* bitsize */
    534 	 false,			/* pc_relative */
    535 	 0,			/* bitpos */
    536 	 complain_overflow_dont, /* complain_on_overflow */
    537 	 bfd_elf_generic_reloc,	/* special_function */
    538 	 "R_FRV_TLSMOFFLO",	/* name */
    539 	 false,			/* partial_inplace */
    540 	 0xffff,		/* src_mask */
    541 	 0xffff,		/* dst_mask */
    542 	 false),		/* pcrel_offset */
    543 
    544   /* A 12-bit signed operand with the GOT offset for the TLSOFF entry
    545      for a symbol.  */
    546   HOWTO (R_FRV_GOTTLSOFF12,	/* type */
    547 	 0,			/* rightshift */
    548 	 4,			/* size */
    549 	 12,			/* bitsize */
    550 	 false,			/* pc_relative */
    551 	 0,			/* bitpos */
    552 	 complain_overflow_signed, /* complain_on_overflow */
    553 	 bfd_elf_generic_reloc,	/* special_function */
    554 	 "R_FRV_GOTTLSOFF12",	/* name */
    555 	 false,			/* partial_inplace */
    556 	 0xfff,			/* src_mask */
    557 	 0xfff,			/* dst_mask */
    558 	 false),		/* pcrel_offset */
    559 
    560   /* The upper 16 bits of the GOT offset for the TLSOFF entry for a
    561      symbol.  */
    562   HOWTO (R_FRV_GOTTLSOFFHI,	/* type */
    563 	 0,			/* rightshift */
    564 	 4,			/* size */
    565 	 16,			/* bitsize */
    566 	 false,			/* pc_relative */
    567 	 0,			/* bitpos */
    568 	 complain_overflow_dont, /* complain_on_overflow */
    569 	 bfd_elf_generic_reloc,	/* special_function */
    570 	 "R_FRV_GOTTLSOFFHI",	/* name */
    571 	 false,			/* partial_inplace */
    572 	 0xffff,		/* src_mask */
    573 	 0xffff,		/* dst_mask */
    574 	 false),		/* pcrel_offset */
    575 
    576   /* The lower 16 bits of the GOT offset for the TLSOFF entry for a
    577      symbol.  */
    578   HOWTO (R_FRV_GOTTLSOFFLO,	/* type */
    579 	 0,			/* rightshift */
    580 	 4,			/* size */
    581 	 16,			/* bitsize */
    582 	 false,			/* pc_relative */
    583 	 0,			/* bitpos */
    584 	 complain_overflow_dont, /* complain_on_overflow */
    585 	 bfd_elf_generic_reloc,	/* special_function */
    586 	 "R_FRV_GOTTLSOFFLO",	/* name */
    587 	 false,			/* partial_inplace */
    588 	 0xffff,		/* src_mask */
    589 	 0xffff,		/* dst_mask */
    590 	 false),		/* pcrel_offset */
    591 
    592   /* The 32-bit offset from the thread pointer (not the module base
    593      address) to a thread-local symbol.  */
    594   HOWTO (R_FRV_TLSOFF,		/* type */
    595 	 0,			/* rightshift */
    596 	 4,			/* size */
    597 	 32,			/* bitsize */
    598 	 false,			/* pc_relative */
    599 	 0,			/* bitpos */
    600 	 complain_overflow_dont, /* complain_on_overflow */
    601 	 bfd_elf_generic_reloc,	/* special_function */
    602 	 "R_FRV_TLSOFF",	/* name */
    603 	 false,			/* partial_inplace */
    604 	 0xffffffff,		/* src_mask */
    605 	 0xffffffff,		/* dst_mask */
    606 	 false),		/* pcrel_offset */
    607 
    608   /* An annotation for linker relaxation, that denotes the
    609      symbol+addend whose TLS descriptor is referenced by the sum of
    610      the two input registers of an ldd instruction.  */
    611   HOWTO (R_FRV_TLSDESC_RELAX,	/* type */
    612 	 0,			/* rightshift */
    613 	 4,			/* size */
    614 	 0,			/* bitsize */
    615 	 false,			/* pc_relative */
    616 	 0,			/* bitpos */
    617 	 complain_overflow_dont, /* complain_on_overflow */
    618 	 bfd_elf_generic_reloc,	/* special_function */
    619 	 "R_FRV_TLSDESC_RELAX",	/* name */
    620 	 false,			/* partial_inplace */
    621 	 0,			/* src_mask */
    622 	 0,			/* dst_mask */
    623 	 false),		/* pcrel_offset */
    624 
    625   /* An annotation for linker relaxation, that denotes the
    626      symbol+addend whose TLS resolver entry point is given by the sum
    627      of the two register operands of an calll instruction.  */
    628   HOWTO (R_FRV_GETTLSOFF_RELAX,	/* type */
    629 	 0,			/* rightshift */
    630 	 4,			/* size */
    631 	 0,			/* bitsize */
    632 	 false,			/* pc_relative */
    633 	 0,			/* bitpos */
    634 	 complain_overflow_dont, /* complain_on_overflow */
    635 	 bfd_elf_generic_reloc,	/* special_function */
    636 	 "R_FRV_GETTLSOFF_RELAX", /* name */
    637 	 false,			/* partial_inplace */
    638 	 0,			/* src_mask */
    639 	 0,			/* dst_mask */
    640 	 false),		/* pcrel_offset */
    641 
    642   /* An annotation for linker relaxation, that denotes the
    643      symbol+addend whose TLS offset GOT entry is given by the sum of
    644      the two input registers of an ld instruction.  */
    645   HOWTO (R_FRV_TLSOFF_RELAX,	/* type */
    646 	 0,			/* rightshift */
    647 	 4,			/* size */
    648 	 0,			/* bitsize */
    649 	 false,			/* pc_relative */
    650 	 0,			/* bitpos */
    651 	 complain_overflow_bitfield, /* complain_on_overflow */
    652 	 bfd_elf_generic_reloc,	/* special_function */
    653 	 "R_FRV_TLSOFF_RELAX",	/* name */
    654 	 false,			/* partial_inplace */
    655 	 0,			/* src_mask */
    656 	 0,			/* dst_mask */
    657 	 false),		/* pcrel_offset */
    658 
    659   /* A 32-bit offset from the module base address to
    660      the thread-local symbol address.  */
    661   HOWTO (R_FRV_TLSMOFF,		/* type */
    662 	 0,			/* rightshift */
    663 	 4,			/* size */
    664 	 32,			/* bitsize */
    665 	 false,			/* pc_relative */
    666 	 0,			/* bitpos */
    667 	 complain_overflow_dont, /* complain_on_overflow */
    668 	 bfd_elf_generic_reloc,	/* special_function */
    669 	 "R_FRV_TLSMOFF",	/* name */
    670 	 false,			/* partial_inplace */
    671 	 0xffffffff,		/* src_mask */
    672 	 0xffffffff,		/* dst_mask */
    673 	 false),		/* pcrel_offset */
    674 };
    675 
    676 /* GNU extension to record C++ vtable hierarchy.  */
    677 static reloc_howto_type elf32_frv_vtinherit_howto =
    678   HOWTO (R_FRV_GNU_VTINHERIT,	/* type */
    679 	 0,			/* rightshift */
    680 	 4,			/* size */
    681 	 0,			/* bitsize */
    682 	 false,			/* pc_relative */
    683 	 0,			/* bitpos */
    684 	 complain_overflow_dont, /* complain_on_overflow */
    685 	 NULL,			/* special_function */
    686 	 "R_FRV_GNU_VTINHERIT", /* name */
    687 	 false,			/* partial_inplace */
    688 	 0,			/* src_mask */
    689 	 0,			/* dst_mask */
    690 	 false);		/* pcrel_offset */
    691 
    692   /* GNU extension to record C++ vtable member usage.  */
    693 static reloc_howto_type elf32_frv_vtentry_howto =
    694   HOWTO (R_FRV_GNU_VTENTRY,	/* type */
    695 	 0,			/* rightshift */
    696 	 4,			/* size */
    697 	 0,			/* bitsize */
    698 	 false,			/* pc_relative */
    699 	 0,			/* bitpos */
    700 	 complain_overflow_dont, /* complain_on_overflow */
    701 	 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
    702 	 "R_FRV_GNU_VTENTRY",	/* name */
    703 	 false,			/* partial_inplace */
    704 	 0,			/* src_mask */
    705 	 0,			/* dst_mask */
    706 	 false);		/* pcrel_offset */
    707 
    708 /* The following 3 relocations are REL.  The only difference to the
    709    entries in the table above are that partial_inplace is true.  */
    710 static reloc_howto_type elf32_frv_rel_32_howto =
    711   HOWTO (R_FRV_32,		/* type */
    712 	 0,			/* rightshift */
    713 	 4,			/* size */
    714 	 32,			/* bitsize */
    715 	 false,			/* pc_relative */
    716 	 0,			/* bitpos */
    717 	 complain_overflow_bitfield, /* complain_on_overflow */
    718 	 bfd_elf_generic_reloc,	/* special_function */
    719 	 "R_FRV_32",		/* name */
    720 	 true,			/* partial_inplace */
    721 	 0xffffffff,		/* src_mask */
    722 	 0xffffffff,		/* dst_mask */
    723 	 false);		/* pcrel_offset */
    724 
    725 static reloc_howto_type elf32_frv_rel_funcdesc_howto =
    726   HOWTO (R_FRV_FUNCDESC,	/* type */
    727 	 0,			/* rightshift */
    728 	 4,			/* size */
    729 	 32,			/* bitsize */
    730 	 false,			/* pc_relative */
    731 	 0,			/* bitpos */
    732 	 complain_overflow_bitfield, /* complain_on_overflow */
    733 	 bfd_elf_generic_reloc,	/* special_function */
    734 	 "R_FRV_FUNCDESC",	/* name */
    735 	 true,			/* partial_inplace */
    736 	 0xffffffff,		/* src_mask */
    737 	 0xffffffff,		/* dst_mask */
    738 	 false);		/* pcrel_offset */
    739 
    740 static reloc_howto_type elf32_frv_rel_funcdesc_value_howto =
    741   HOWTO (R_FRV_FUNCDESC_VALUE,	/* type */
    742 	 0,			/* rightshift */
    743 	 4,			/* size */
    744 	 64,			/* bitsize */
    745 	 false,			/* pc_relative */
    746 	 0,			/* bitpos */
    747 	 complain_overflow_bitfield, /* complain_on_overflow */
    748 	 bfd_elf_generic_reloc,	/* special_function */
    749 	 "R_FRV_FUNCDESC_VALUE", /* name */
    750 	 true,			/* partial_inplace */
    751 	 0xffffffff,		/* src_mask */
    752 	 0xffffffff,		/* dst_mask */
    753 	 false);		/* pcrel_offset */
    754 
    755 static reloc_howto_type elf32_frv_rel_tlsdesc_value_howto =
    756   /* A 64-bit TLS descriptor for a symbol.  The first word resolves to
    757      an entry point, and the second resolves to a special argument.
    758      If the symbol turns out to be in static TLS, the entry point is a
    759      return instruction, and the special argument is the TLS offset
    760      for the symbol.  If it's in dynamic TLS, the entry point is a TLS
    761      offset resolver, and the special argument is a pointer to a data
    762      structure allocated by the dynamic loader, containing the GOT
    763      address for the offset resolver, the module id, the offset within
    764      the module, and anything else the TLS offset resolver might need
    765      to determine the TLS offset for the symbol in the running
    766      thread.  */
    767   HOWTO (R_FRV_TLSDESC_VALUE,	/* type */
    768 	 0,			/* rightshift */
    769 	 4,			/* size */
    770 	 64,			/* bitsize */
    771 	 false,			/* pc_relative */
    772 	 0,			/* bitpos */
    773 	 complain_overflow_bitfield, /* complain_on_overflow */
    774 	 bfd_elf_generic_reloc,	/* special_function */
    775 	 "R_FRV_TLSDESC_VALUE", /* name */
    776 	 true,			/* partial_inplace */
    777 	 0xffffffff,		/* src_mask */
    778 	 0xffffffff,		/* dst_mask */
    779 	 false);		/* pcrel_offset */
    780 
    781 static reloc_howto_type elf32_frv_rel_tlsoff_howto =
    782   /* The 32-bit offset from the thread pointer (not the module base
    783      address) to a thread-local symbol.  */
    784   HOWTO (R_FRV_TLSOFF,		/* type */
    785 	 0,			/* rightshift */
    786 	 4,			/* size */
    787 	 32,			/* bitsize */
    788 	 false,			/* pc_relative */
    789 	 0,			/* bitpos */
    790 	 complain_overflow_bitfield, /* complain_on_overflow */
    791 	 bfd_elf_generic_reloc,	/* special_function */
    792 	 "R_FRV_TLSOFF",	/* name */
    793 	 true,			/* partial_inplace */
    794 	 0xffffffff,		/* src_mask */
    795 	 0xffffffff,		/* dst_mask */
    796 	 false);		/* pcrel_offset */
    797 
    798 
    799 
    800 extern const bfd_target frv_elf32_fdpic_vec;
    802 #define IS_FDPIC(bfd) ((bfd)->xvec == &frv_elf32_fdpic_vec)
    803 
    804 /* An extension of the elf hash table data structure, containing some
    805    additional FRV-specific data.  */
    806 struct frvfdpic_elf_link_hash_table
    807 {
    808   struct elf_link_hash_table elf;
    809 
    810   /* A pointer to the .rofixup section.  */
    811   asection *sgotfixup;
    812   /* GOT base offset.  */
    813   bfd_vma got0;
    814   /* Location of the first non-lazy PLT entry, i.e., the number of
    815      bytes taken by lazy PLT entries.  If locally-bound TLS
    816      descriptors require a ret instruction, it will be placed at this
    817      offset.  */
    818   bfd_vma plt0;
    819   /* A hash table holding information about which symbols were
    820      referenced with which PIC-related relocations.  */
    821   struct htab *relocs_info;
    822   /* Summary reloc information collected by
    823      _frvfdpic_count_got_plt_entries.  */
    824   struct _frvfdpic_dynamic_got_info *g;
    825 };
    826 
    827 /* Get the FRV ELF linker hash table from a link_info structure.  */
    828 
    829 #define frvfdpic_hash_table(p) \
    830   ((is_elf_hash_table ((p)->hash)					\
    831     && elf_hash_table_id (elf_hash_table (p)) == FRV_ELF_DATA)		\
    832    ? (struct frvfdpic_elf_link_hash_table *) (p)->hash : NULL)
    833 
    834 #define frvfdpic_got_section(info) \
    835   (frvfdpic_hash_table (info)->elf.sgot)
    836 #define frvfdpic_gotrel_section(info) \
    837   (frvfdpic_hash_table (info)->elf.srelgot)
    838 #define frvfdpic_gotfixup_section(info) \
    839   (frvfdpic_hash_table (info)->sgotfixup)
    840 #define frvfdpic_plt_section(info) \
    841   (frvfdpic_hash_table (info)->elf.splt)
    842 #define frvfdpic_pltrel_section(info) \
    843   (frvfdpic_hash_table (info)->elf.srelplt)
    844 #define frvfdpic_relocs_info(info) \
    845   (frvfdpic_hash_table (info)->relocs_info)
    846 #define frvfdpic_got_initial_offset(info) \
    847   (frvfdpic_hash_table (info)->got0)
    848 #define frvfdpic_plt_initial_offset(info) \
    849   (frvfdpic_hash_table (info)->plt0)
    850 #define frvfdpic_dynamic_got_plt_info(info) \
    851   (frvfdpic_hash_table (info)->g)
    852 
    853 /* Currently it's the same, but if some day we have a reason to change
    854    it, we'd better be using a different macro.
    855 
    856    FIXME: if there's any TLS PLT entry that uses local-exec or
    857    initial-exec models, we could use the ret at the end of any of them
    858    instead of adding one more.  */
    859 #define frvfdpic_plt_tls_ret_offset(info) \
    860   (frvfdpic_plt_initial_offset (info))
    861 
    862 /* The name of the dynamic interpreter.  This is put in the .interp
    863    section.  */
    864 
    865 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
    866 
    867 #define DEFAULT_STACK_SIZE 0x20000
    868 
    869 /* This structure is used to collect the number of entries present in
    870    each addressable range of the got.  */
    871 struct _frvfdpic_dynamic_got_info
    872 {
    873   /* Several bits of information about the current link.  */
    874   struct bfd_link_info *info;
    875   /* Total GOT size needed for GOT entries within the 12-, 16- or 32-bit
    876      ranges.  */
    877   bfd_vma got12, gotlos, gothilo;
    878   /* Total GOT size needed for function descriptor entries within the 12-,
    879      16- or 32-bit ranges.  */
    880   bfd_vma fd12, fdlos, fdhilo;
    881   /* Total GOT size needed by function descriptor entries referenced
    882      in PLT entries, that would be profitable to place in offsets
    883      close to the PIC register.  */
    884   bfd_vma fdplt;
    885   /* Total PLT size needed by lazy PLT entries.  */
    886   bfd_vma lzplt;
    887   /* Total GOT size needed for TLS descriptor entries within the 12-,
    888      16- or 32-bit ranges.  */
    889   bfd_vma tlsd12, tlsdlos, tlsdhilo;
    890   /* Total GOT size needed by TLS descriptors referenced in PLT
    891      entries, that would be profitable to place in offers close to the
    892      PIC register.  */
    893   bfd_vma tlsdplt;
    894   /* Total PLT size needed by TLS lazy PLT entries.  */
    895   bfd_vma tlslzplt;
    896   /* Number of relocations carried over from input object files.  */
    897   unsigned long relocs;
    898   /* Number of fixups introduced by relocations in input object files.  */
    899   unsigned long fixups;
    900   /* The number of fixups that reference the ret instruction added to
    901      the PLT for locally-resolved TLS descriptors.  */
    902   unsigned long tls_ret_refs;
    903 };
    904 
    905 /* This structure is used to assign offsets to got entries, function
    906    descriptors, plt entries and lazy plt entries.  */
    907 
    908 struct _frvfdpic_dynamic_got_plt_info
    909 {
    910   /* Summary information collected with _frvfdpic_count_got_plt_entries.  */
    911   struct _frvfdpic_dynamic_got_info g;
    912 
    913   /* For each addressable range, we record a MAX (positive) and MIN
    914      (negative) value.  CUR is used to assign got entries, and it's
    915      incremented from an initial positive value to MAX, then from MIN
    916      to FDCUR (unless FDCUR wraps around first).  FDCUR is used to
    917      assign function descriptors, and it's decreased from an initial
    918      non-positive value to MIN, then from MAX down to CUR (unless CUR
    919      wraps around first).  All of MIN, MAX, CUR and FDCUR always point
    920      to even words.  ODD, if non-zero, indicates an odd word to be
    921      used for the next got entry, otherwise CUR is used and
    922      incremented by a pair of words, wrapping around when it reaches
    923      MAX.  FDCUR is decremented (and wrapped) before the next function
    924      descriptor is chosen.  FDPLT indicates the number of remaining
    925      slots that can be used for function descriptors used only by PLT
    926      entries.
    927 
    928      TMAX, TMIN and TCUR are used to assign TLS descriptors.  TCUR
    929      starts as MAX, and grows up to TMAX, then wraps around to TMIN
    930      and grows up to MIN.  TLSDPLT indicates the number of remaining
    931      slots that can be used for TLS descriptors used only by TLS PLT
    932      entries.  */
    933   struct _frvfdpic_dynamic_got_alloc_data
    934   {
    935     bfd_signed_vma max, cur, odd, fdcur, min;
    936     bfd_signed_vma tmax, tcur, tmin;
    937     bfd_vma fdplt, tlsdplt;
    938   } got12, gotlos, gothilo;
    939 };
    940 
    941 /* Create an FRV ELF linker hash table.  */
    942 
    943 static struct bfd_link_hash_table *
    944 frvfdpic_elf_link_hash_table_create (bfd *abfd)
    945 {
    946   struct frvfdpic_elf_link_hash_table *ret;
    947   size_t amt = sizeof (struct frvfdpic_elf_link_hash_table);
    948 
    949   ret = bfd_zmalloc (amt);
    950   if (ret == NULL)
    951     return NULL;
    952 
    953   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
    954 				      _bfd_elf_link_hash_newfunc,
    955 				      sizeof (struct elf_link_hash_entry)))
    956     {
    957       free (ret);
    958       return NULL;
    959     }
    960 
    961   return &ret->elf.root;
    962 }
    963 
    964 /* Decide whether a reference to a symbol can be resolved locally or
    965    not.  If the symbol is protected, we want the local address, but
    966    its function descriptor must be assigned by the dynamic linker.  */
    967 #define FRVFDPIC_SYM_LOCAL(INFO, H) \
    968   (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
    969    || ! elf_hash_table (INFO)->dynamic_sections_created)
    970 #define FRVFDPIC_FUNCDESC_LOCAL(INFO, H) \
    971   ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
    972 
    973 /* This structure collects information on what kind of GOT, PLT or
    974    function descriptors are required by relocations that reference a
    975    certain symbol.  */
    976 struct frvfdpic_relocs_info
    977 {
    978   /* The index of the symbol, as stored in the relocation r_info, if
    979      we have a local symbol; -1 otherwise.  */
    980   long symndx;
    981   union
    982   {
    983     /* The input bfd in which the symbol is defined, if it's a local
    984        symbol.  */
    985     bfd *abfd;
    986     /* If symndx == -1, the hash table entry corresponding to a global
    987        symbol (even if it turns out to bind locally, in which case it
    988        should ideally be replaced with section's symndx + addend).  */
    989     struct elf_link_hash_entry *h;
    990   } d;
    991   /* The addend of the relocation that references the symbol.  */
    992   bfd_vma addend;
    993 
    994   /* The fields above are used to identify an entry.  The fields below
    995      contain information on how an entry is used and, later on, which
    996      locations it was assigned.  */
    997   /* The following 3 fields record whether the symbol+addend above was
    998      ever referenced with a GOT relocation.  The 12 suffix indicates a
    999      GOT12 relocation; los is used for GOTLO relocations that are not
   1000      matched by a GOTHI relocation; hilo is used for GOTLO/GOTHI
   1001      pairs.  */
   1002   unsigned got12:1;
   1003   unsigned gotlos:1;
   1004   unsigned gothilo:1;
   1005   /* Whether a FUNCDESC relocation references symbol+addend.  */
   1006   unsigned fd:1;
   1007   /* Whether a FUNCDESC_GOT relocation references symbol+addend.  */
   1008   unsigned fdgot12:1;
   1009   unsigned fdgotlos:1;
   1010   unsigned fdgothilo:1;
   1011   /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend.  */
   1012   unsigned fdgoff12:1;
   1013   unsigned fdgofflos:1;
   1014   unsigned fdgoffhilo:1;
   1015   /* Whether a GETTLSOFF relocation references symbol+addend.  */
   1016   unsigned tlsplt:1;
   1017   /* FIXME: we should probably add tlspltdesc, tlspltoff and
   1018      tlspltimm, to tell what kind of TLS PLT entry we're generating.
   1019      We might instead just pre-compute flags telling whether the
   1020      object is suitable for local exec, initial exec or general
   1021      dynamic addressing, and use that all over the place.  We could
   1022      also try to do a better job of merging TLSOFF and TLSDESC entries
   1023      in main executables, but perhaps we can get rid of TLSDESC
   1024      entirely in them instead.  */
   1025   /* Whether a GOTTLSDESC relocation references symbol+addend.  */
   1026   unsigned tlsdesc12:1;
   1027   unsigned tlsdesclos:1;
   1028   unsigned tlsdeschilo:1;
   1029   /* Whether a GOTTLSOFF relocation references symbol+addend.  */
   1030   unsigned tlsoff12:1;
   1031   unsigned tlsofflos:1;
   1032   unsigned tlsoffhilo:1;
   1033   /* Whether symbol+addend is referenced with GOTOFF12, GOTOFFLO or
   1034      GOTOFFHI relocations.  The addend doesn't really matter, since we
   1035      envision that this will only be used to check whether the symbol
   1036      is mapped to the same segment as the got.  */
   1037   unsigned gotoff:1;
   1038   /* Whether symbol+addend is referenced by a LABEL24 relocation.  */
   1039   unsigned call:1;
   1040   /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
   1041      relocation.  */
   1042   unsigned sym:1;
   1043   /* Whether we need a PLT entry for a symbol.  Should be implied by
   1044      something like:
   1045      (call && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h))  */
   1046   unsigned plt:1;
   1047   /* Whether a function descriptor should be created in this link unit
   1048      for symbol+addend.  Should be implied by something like:
   1049      (plt || fdgotoff12 || fdgotofflos || fdgotofflohi
   1050       || ((fd || fdgot12 || fdgotlos || fdgothilo)
   1051 	  && (symndx != -1 || FRVFDPIC_FUNCDESC_LOCAL (info, d.h))))  */
   1052   unsigned privfd:1;
   1053   /* Whether a lazy PLT entry is needed for this symbol+addend.
   1054      Should be implied by something like:
   1055      (privfd && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)
   1056       && ! (info->flags & DF_BIND_NOW))  */
   1057   unsigned lazyplt:1;
   1058   /* Whether we've already emitted GOT relocations and PLT entries as
   1059      needed for this symbol.  */
   1060   unsigned done:1;
   1061 
   1062   /* The number of R_FRV_32, R_FRV_FUNCDESC, R_FRV_FUNCDESC_VALUE and
   1063      R_FRV_TLSDESC_VALUE, R_FRV_TLSOFF relocations referencing
   1064      symbol+addend.  */
   1065   unsigned relocs32, relocsfd, relocsfdv, relocstlsd, relocstlsoff;
   1066 
   1067   /* The number of .rofixups entries and dynamic relocations allocated
   1068      for this symbol, minus any that might have already been used.  */
   1069   unsigned fixups, dynrelocs;
   1070 
   1071   /* The offsets of the GOT entries assigned to symbol+addend, to the
   1072      function descriptor's address, and to a function descriptor,
   1073      respectively.  Should be zero if unassigned.  The offsets are
   1074      counted from the value that will be assigned to the PIC register,
   1075      not from the beginning of the .got section.  */
   1076   bfd_signed_vma got_entry, fdgot_entry, fd_entry;
   1077   /* The offsets of the PLT entries assigned to symbol+addend,
   1078      non-lazy and lazy, respectively.  If unassigned, should be
   1079      (bfd_vma)-1.  */
   1080   bfd_vma plt_entry, lzplt_entry;
   1081   /* The offsets of the GOT entries for TLS offset and TLS descriptor.  */
   1082   bfd_signed_vma tlsoff_entry, tlsdesc_entry;
   1083   /* The offset of the TLS offset PLT entry.  */
   1084   bfd_vma tlsplt_entry;
   1085 };
   1086 
   1087 /* Compute a hash with the key fields of an frvfdpic_relocs_info entry.  */
   1088 static hashval_t
   1089 frvfdpic_relocs_info_hash (const void *entry_)
   1090 {
   1091   const struct frvfdpic_relocs_info *entry = entry_;
   1092 
   1093   return (entry->symndx == -1
   1094 	  ? (long) entry->d.h->root.root.hash
   1095 	  : entry->symndx + (long) entry->d.abfd->id * 257) + entry->addend;
   1096 }
   1097 
   1098 /* Test whether the key fields of two frvfdpic_relocs_info entries are
   1099    identical.  */
   1100 static int
   1101 frvfdpic_relocs_info_eq (const void *entry1, const void *entry2)
   1102 {
   1103   const struct frvfdpic_relocs_info *e1 = entry1;
   1104   const struct frvfdpic_relocs_info *e2 = entry2;
   1105 
   1106   return e1->symndx == e2->symndx && e1->addend == e2->addend
   1107     && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
   1108 }
   1109 
   1110 /* Find or create an entry in a hash table HT that matches the key
   1111    fields of the given ENTRY.  If it's not found, memory for a new
   1112    entry is allocated in ABFD's obstack.  */
   1113 static struct frvfdpic_relocs_info *
   1114 frvfdpic_relocs_info_find (struct htab *ht,
   1115 			   bfd *abfd,
   1116 			   const struct frvfdpic_relocs_info *entry,
   1117 			   enum insert_option insert)
   1118 {
   1119   struct frvfdpic_relocs_info **loc =
   1120     (struct frvfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
   1121 
   1122   if (! loc)
   1123     return NULL;
   1124 
   1125   if (*loc)
   1126     return *loc;
   1127 
   1128   *loc = bfd_zalloc (abfd, sizeof (**loc));
   1129 
   1130   if (! *loc)
   1131     return *loc;
   1132 
   1133   (*loc)->symndx = entry->symndx;
   1134   (*loc)->d = entry->d;
   1135   (*loc)->addend = entry->addend;
   1136   (*loc)->plt_entry = (bfd_vma)-1;
   1137   (*loc)->lzplt_entry = (bfd_vma)-1;
   1138   (*loc)->tlsplt_entry = (bfd_vma)-1;
   1139 
   1140   return *loc;
   1141 }
   1142 
   1143 /* Obtain the address of the entry in HT associated with H's symbol +
   1144    addend, creating a new entry if none existed.  ABFD is only used
   1145    for memory allocation purposes.  */
   1146 inline static struct frvfdpic_relocs_info *
   1147 frvfdpic_relocs_info_for_global (struct htab *ht,
   1148 				 bfd *abfd,
   1149 				 struct elf_link_hash_entry *h,
   1150 				 bfd_vma addend,
   1151 				 enum insert_option insert)
   1152 {
   1153   struct frvfdpic_relocs_info entry;
   1154 
   1155   entry.symndx = -1;
   1156   entry.d.h = h;
   1157   entry.addend = addend;
   1158 
   1159   return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
   1160 }
   1161 
   1162 /* Obtain the address of the entry in HT associated with the SYMNDXth
   1163    local symbol of the input bfd ABFD, plus the addend, creating a new
   1164    entry if none existed.  */
   1165 inline static struct frvfdpic_relocs_info *
   1166 frvfdpic_relocs_info_for_local (struct htab *ht,
   1167 				bfd *abfd,
   1168 				long symndx,
   1169 				bfd_vma addend,
   1170 				enum insert_option insert)
   1171 {
   1172   struct frvfdpic_relocs_info entry;
   1173 
   1174   entry.symndx = symndx;
   1175   entry.d.abfd = abfd;
   1176   entry.addend = addend;
   1177 
   1178   return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
   1179 }
   1180 
   1181 /* Merge fields set by check_relocs() of two entries that end up being
   1182    mapped to the same (presumably global) symbol.  */
   1183 
   1184 inline static void
   1185 frvfdpic_pic_merge_early_relocs_info (struct frvfdpic_relocs_info *e2,
   1186 				      struct frvfdpic_relocs_info const *e1)
   1187 {
   1188   e2->got12 |= e1->got12;
   1189   e2->gotlos |= e1->gotlos;
   1190   e2->gothilo |= e1->gothilo;
   1191   e2->fd |= e1->fd;
   1192   e2->fdgot12 |= e1->fdgot12;
   1193   e2->fdgotlos |= e1->fdgotlos;
   1194   e2->fdgothilo |= e1->fdgothilo;
   1195   e2->fdgoff12 |= e1->fdgoff12;
   1196   e2->fdgofflos |= e1->fdgofflos;
   1197   e2->fdgoffhilo |= e1->fdgoffhilo;
   1198   e2->tlsplt |= e1->tlsplt;
   1199   e2->tlsdesc12 |= e1->tlsdesc12;
   1200   e2->tlsdesclos |= e1->tlsdesclos;
   1201   e2->tlsdeschilo |= e1->tlsdeschilo;
   1202   e2->tlsoff12 |= e1->tlsoff12;
   1203   e2->tlsofflos |= e1->tlsofflos;
   1204   e2->tlsoffhilo |= e1->tlsoffhilo;
   1205   e2->gotoff |= e1->gotoff;
   1206   e2->call |= e1->call;
   1207   e2->sym |= e1->sym;
   1208 }
   1209 
   1210 /* Every block of 65535 lazy PLT entries shares a single call to the
   1211    resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
   1212    32767, counting from 0).  All other lazy PLT entries branch to it
   1213    in a single instruction.  */
   1214 
   1215 #define FRVFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) 8 * 65535 + 4)
   1216 #define FRVFDPIC_LZPLT_RESOLV_LOC (8 * 32767)
   1217 
   1218 /* Add a dynamic relocation to the SRELOC section.  */
   1219 
   1220 inline static bfd_vma
   1221 _frvfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
   1222 			 int reloc_type, long dynindx, bfd_vma addend,
   1223 			 struct frvfdpic_relocs_info *entry)
   1224 {
   1225   Elf_Internal_Rela outrel;
   1226   bfd_vma reloc_offset;
   1227 
   1228   outrel.r_offset = offset;
   1229   outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
   1230   outrel.r_addend = addend;
   1231 
   1232   reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
   1233   BFD_ASSERT (reloc_offset < sreloc->size);
   1234   bfd_elf32_swap_reloc_out (output_bfd, &outrel,
   1235 			    sreloc->contents + reloc_offset);
   1236   sreloc->reloc_count++;
   1237 
   1238   /* If the entry's index is zero, this relocation was probably to a
   1239      linkonce section that got discarded.  We reserved a dynamic
   1240      relocation, but it was for another entry than the one we got at
   1241      the time of emitting the relocation.  Unfortunately there's no
   1242      simple way for us to catch this situation, since the relocation
   1243      is cleared right before calling relocate_section, at which point
   1244      we no longer know what the relocation used to point to.  */
   1245   if (entry->symndx)
   1246     {
   1247       BFD_ASSERT (entry->dynrelocs > 0);
   1248       entry->dynrelocs--;
   1249     }
   1250 
   1251   return reloc_offset;
   1252 }
   1253 
   1254 /* Add a fixup to the ROFIXUP section.  */
   1255 
   1256 static bfd_vma
   1257 _frvfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
   1258 		       struct frvfdpic_relocs_info *entry)
   1259 {
   1260   bfd_vma fixup_offset;
   1261 
   1262   if (rofixup->flags & SEC_EXCLUDE)
   1263     return -1;
   1264 
   1265   fixup_offset = rofixup->reloc_count * 4;
   1266   if (rofixup->contents)
   1267     {
   1268       BFD_ASSERT (fixup_offset < rofixup->size);
   1269       bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
   1270     }
   1271   rofixup->reloc_count++;
   1272 
   1273   if (entry && entry->symndx)
   1274     {
   1275       /* See discussion about symndx == 0 in _frvfdpic_add_dyn_reloc
   1276 	 above.  */
   1277       BFD_ASSERT (entry->fixups > 0);
   1278       entry->fixups--;
   1279     }
   1280 
   1281   return fixup_offset;
   1282 }
   1283 
   1284 /* Find the segment number in which OSEC, and output section, is
   1285    located.  */
   1286 
   1287 static unsigned
   1288 _frvfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
   1289 {
   1290   Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
   1291 
   1292   return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
   1293 }
   1294 
   1295 inline static bool
   1296 _frvfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
   1297 {
   1298   unsigned seg = _frvfdpic_osec_to_segment (output_bfd, osec);
   1299 
   1300   return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
   1301 }
   1302 
   1303 #define FRVFDPIC_TLS_BIAS (2048 - 16)
   1304 
   1305 /* Return the base VMA address which should be subtracted from real addresses
   1306    when resolving TLSMOFF relocation.
   1307    This is PT_TLS segment p_vaddr, plus the 2048-16 bias.  */
   1308 
   1309 static bfd_vma
   1310 tls_biased_base (struct bfd_link_info *info)
   1311 {
   1312   /* If tls_sec is NULL, we should have signalled an error already.  */
   1313   if (elf_hash_table (info)->tls_sec == NULL)
   1314     return FRVFDPIC_TLS_BIAS;
   1315   return elf_hash_table (info)->tls_sec->vma + FRVFDPIC_TLS_BIAS;
   1316 }
   1317 
   1318 /* Generate relocations for GOT entries, function descriptors, and
   1319    code for PLT and lazy PLT entries.  */
   1320 
   1321 inline static bool
   1322 _frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
   1323 				       bfd *output_bfd,
   1324 				       struct bfd_link_info *info,
   1325 				       asection *sec,
   1326 				       Elf_Internal_Sym *sym,
   1327 				       bfd_vma addend)
   1328 
   1329 {
   1330   bfd_vma fd_lazy_rel_offset = (bfd_vma)-1;
   1331   int dynindx = -1;
   1332 
   1333   if (entry->done)
   1334     return true;
   1335   entry->done = 1;
   1336 
   1337   if (entry->got_entry || entry->fdgot_entry || entry->fd_entry
   1338       || entry->tlsoff_entry || entry->tlsdesc_entry)
   1339     {
   1340       /* If the symbol is dynamic, consider it for dynamic
   1341 	 relocations, otherwise decay to section + offset.  */
   1342       if (entry->symndx == -1 && entry->d.h->dynindx != -1)
   1343 	dynindx = entry->d.h->dynindx;
   1344       else
   1345 	{
   1346 	  if (sec
   1347 	      && sec->output_section
   1348 	      && ! bfd_is_abs_section (sec->output_section)
   1349 	      && ! bfd_is_und_section (sec->output_section))
   1350 	    dynindx = elf_section_data (sec->output_section)->dynindx;
   1351 	  else
   1352 	    dynindx = 0;
   1353 	}
   1354     }
   1355 
   1356   /* Generate relocation for GOT entry pointing to the symbol.  */
   1357   if (entry->got_entry)
   1358     {
   1359       int idx = dynindx;
   1360       bfd_vma ad = addend;
   1361 
   1362       /* If the symbol is dynamic but binds locally, use
   1363 	 section+offset.  */
   1364       if (sec && (entry->symndx != -1
   1365 		  || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1366 	{
   1367 	  if (entry->symndx == -1)
   1368 	    ad += entry->d.h->root.u.def.value;
   1369 	  else
   1370 	    ad += sym->st_value;
   1371 	  ad += sec->output_offset;
   1372 	  if (sec->output_section && elf_section_data (sec->output_section))
   1373 	    idx = elf_section_data (sec->output_section)->dynindx;
   1374 	  else
   1375 	    idx = 0;
   1376 	}
   1377 
   1378       /* If we're linking an executable at a fixed address, we can
   1379 	 omit the dynamic relocation as long as the symbol is local to
   1380 	 this module.  */
   1381       if (bfd_link_pde (info)
   1382 	  && (entry->symndx != -1
   1383 	      || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1384 	{
   1385 	  if (sec)
   1386 	    ad += sec->output_section->vma;
   1387 	  if (entry->symndx != -1
   1388 	      || entry->d.h->root.type != bfd_link_hash_undefweak)
   1389 	    _frvfdpic_add_rofixup (output_bfd,
   1390 				   frvfdpic_gotfixup_section (info),
   1391 				   frvfdpic_got_section (info)->output_section
   1392 				   ->vma
   1393 				   + frvfdpic_got_section (info)->output_offset
   1394 				   + frvfdpic_got_initial_offset (info)
   1395 				   + entry->got_entry, entry);
   1396 	}
   1397       else
   1398 	_frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
   1399 				 _bfd_elf_section_offset
   1400 				 (output_bfd, info,
   1401 				  frvfdpic_got_section (info),
   1402 				  frvfdpic_got_initial_offset (info)
   1403 				  + entry->got_entry)
   1404 				 + frvfdpic_got_section (info)
   1405 				 ->output_section->vma
   1406 				 + frvfdpic_got_section (info)->output_offset,
   1407 				 R_FRV_32, idx, ad, entry);
   1408 
   1409       bfd_put_32 (output_bfd, ad,
   1410 		  frvfdpic_got_section (info)->contents
   1411 		  + frvfdpic_got_initial_offset (info)
   1412 		  + entry->got_entry);
   1413     }
   1414 
   1415   /* Generate relocation for GOT entry pointing to a canonical
   1416      function descriptor.  */
   1417   if (entry->fdgot_entry)
   1418     {
   1419       int reloc, idx;
   1420       bfd_vma ad = 0;
   1421 
   1422       if (! (entry->symndx == -1
   1423 	     && entry->d.h->root.type == bfd_link_hash_undefweak
   1424 	     && FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1425 	{
   1426 	  /* If the symbol is dynamic and there may be dynamic symbol
   1427 	     resolution because we are, or are linked with, a shared
   1428 	     library, emit a FUNCDESC relocation such that the dynamic
   1429 	     linker will allocate the function descriptor.  If the
   1430 	     symbol needs a non-local function descriptor but binds
   1431 	     locally (e.g., its visibility is protected, emit a
   1432 	     dynamic relocation decayed to section+offset.  */
   1433 	  if (entry->symndx == -1
   1434 	      && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
   1435 	      && FRVFDPIC_SYM_LOCAL (info, entry->d.h)
   1436 	      && !bfd_link_pde (info))
   1437 	    {
   1438 	      reloc = R_FRV_FUNCDESC;
   1439 	      idx = elf_section_data (entry->d.h->root.u.def.section
   1440 				      ->output_section)->dynindx;
   1441 	      ad = entry->d.h->root.u.def.section->output_offset
   1442 		+ entry->d.h->root.u.def.value;
   1443 	    }
   1444 	  else if (entry->symndx == -1
   1445 		   && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
   1446 	    {
   1447 	      reloc = R_FRV_FUNCDESC;
   1448 	      idx = dynindx;
   1449 	      ad = addend;
   1450 	      if (ad)
   1451 		{
   1452 		  (*info->callbacks->reloc_dangerous)
   1453 		    (info, _("relocation requires zero addend"),
   1454 		     elf_hash_table (info)->dynobj,
   1455 		     frvfdpic_got_section (info),
   1456 		     entry->fdgot_entry);
   1457 		  return false;
   1458 		}
   1459 	    }
   1460 	  else
   1461 	    {
   1462 	      /* Otherwise, we know we have a private function descriptor,
   1463 		 so reference it directly.  */
   1464 	      if (elf_hash_table (info)->dynamic_sections_created)
   1465 		BFD_ASSERT (entry->privfd);
   1466 	      reloc = R_FRV_32;
   1467 	      idx = elf_section_data (frvfdpic_got_section (info)
   1468 				      ->output_section)->dynindx;
   1469 	      ad = frvfdpic_got_section (info)->output_offset
   1470 		+ frvfdpic_got_initial_offset (info) + entry->fd_entry;
   1471 	    }
   1472 
   1473 	  /* If there is room for dynamic symbol resolution, emit the
   1474 	     dynamic relocation.  However, if we're linking an
   1475 	     executable at a fixed location, we won't have emitted a
   1476 	     dynamic symbol entry for the got section, so idx will be
   1477 	     zero, which means we can and should compute the address
   1478 	     of the private descriptor ourselves.  */
   1479 	  if (bfd_link_pde (info)
   1480 	      && (entry->symndx != -1
   1481 		  || FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
   1482 	    {
   1483 	      ad += frvfdpic_got_section (info)->output_section->vma;
   1484 	      _frvfdpic_add_rofixup (output_bfd,
   1485 				     frvfdpic_gotfixup_section (info),
   1486 				     frvfdpic_got_section (info)
   1487 				     ->output_section->vma
   1488 				     + frvfdpic_got_section (info)
   1489 				     ->output_offset
   1490 				     + frvfdpic_got_initial_offset (info)
   1491 				     + entry->fdgot_entry, entry);
   1492 	    }
   1493 	  else
   1494 	    _frvfdpic_add_dyn_reloc (output_bfd,
   1495 				     frvfdpic_gotrel_section (info),
   1496 				     _bfd_elf_section_offset
   1497 				     (output_bfd, info,
   1498 				      frvfdpic_got_section (info),
   1499 				      frvfdpic_got_initial_offset (info)
   1500 				      + entry->fdgot_entry)
   1501 				     + frvfdpic_got_section (info)
   1502 				     ->output_section->vma
   1503 				     + frvfdpic_got_section (info)
   1504 				     ->output_offset,
   1505 				     reloc, idx, ad, entry);
   1506 	}
   1507 
   1508       bfd_put_32 (output_bfd, ad,
   1509 		  frvfdpic_got_section (info)->contents
   1510 		  + frvfdpic_got_initial_offset (info)
   1511 		  + entry->fdgot_entry);
   1512     }
   1513 
   1514   /* Generate relocation to fill in a private function descriptor in
   1515      the GOT.  */
   1516   if (entry->fd_entry)
   1517     {
   1518       int idx = dynindx;
   1519       bfd_vma ad = addend;
   1520       bfd_vma ofst;
   1521       long lowword, highword;
   1522 
   1523       /* If the symbol is dynamic but binds locally, use
   1524 	 section+offset.  */
   1525       if (sec && (entry->symndx != -1
   1526 		  || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1527 	{
   1528 	  if (entry->symndx == -1)
   1529 	    ad += entry->d.h->root.u.def.value;
   1530 	  else
   1531 	    ad += sym->st_value;
   1532 	  ad += sec->output_offset;
   1533 	  if (sec->output_section && elf_section_data (sec->output_section))
   1534 	    idx = elf_section_data (sec->output_section)->dynindx;
   1535 	  else
   1536 	    idx = 0;
   1537 	}
   1538 
   1539       /* If we're linking an executable at a fixed address, we can
   1540 	 omit the dynamic relocation as long as the symbol is local to
   1541 	 this module.  */
   1542       if (bfd_link_pde (info)
   1543 	  && (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1544 	{
   1545 	  if (sec)
   1546 	    ad += sec->output_section->vma;
   1547 	  ofst = 0;
   1548 	  if (entry->symndx != -1
   1549 	      || entry->d.h->root.type != bfd_link_hash_undefweak)
   1550 	    {
   1551 	      _frvfdpic_add_rofixup (output_bfd,
   1552 				     frvfdpic_gotfixup_section (info),
   1553 				     frvfdpic_got_section (info)
   1554 				     ->output_section->vma
   1555 				     + frvfdpic_got_section (info)
   1556 				     ->output_offset
   1557 				     + frvfdpic_got_initial_offset (info)
   1558 				     + entry->fd_entry, entry);
   1559 	      _frvfdpic_add_rofixup (output_bfd,
   1560 				     frvfdpic_gotfixup_section (info),
   1561 				     frvfdpic_got_section (info)
   1562 				     ->output_section->vma
   1563 				     + frvfdpic_got_section (info)
   1564 				     ->output_offset
   1565 				     + frvfdpic_got_initial_offset (info)
   1566 				     + entry->fd_entry + 4, entry);
   1567 	    }
   1568 	}
   1569       else
   1570 	{
   1571 	  ofst =
   1572 	    _frvfdpic_add_dyn_reloc (output_bfd,
   1573 				     entry->lazyplt
   1574 				     ? frvfdpic_pltrel_section (info)
   1575 				     : frvfdpic_gotrel_section (info),
   1576 				     _bfd_elf_section_offset
   1577 				     (output_bfd, info,
   1578 				      frvfdpic_got_section (info),
   1579 				      frvfdpic_got_initial_offset (info)
   1580 				      + entry->fd_entry)
   1581 				     + frvfdpic_got_section (info)
   1582 				     ->output_section->vma
   1583 				     + frvfdpic_got_section (info)
   1584 				     ->output_offset,
   1585 				     R_FRV_FUNCDESC_VALUE, idx, ad, entry);
   1586 	}
   1587 
   1588       /* If we've omitted the dynamic relocation, just emit the fixed
   1589 	 addresses of the symbol and of the local GOT base offset.  */
   1590       if (bfd_link_pde (info)
   1591 	  && sec
   1592 	  && sec->output_section)
   1593 	{
   1594 	  lowword = ad;
   1595 	  highword = frvfdpic_got_section (info)->output_section->vma
   1596 	    + frvfdpic_got_section (info)->output_offset
   1597 	    + frvfdpic_got_initial_offset (info);
   1598 	}
   1599       else if (entry->lazyplt)
   1600 	{
   1601 	  if (ad)
   1602 	    {
   1603 	      (*info->callbacks->reloc_dangerous)
   1604 		(info, _("relocation requires zero addend"),
   1605 		 elf_hash_table (info)->dynobj,
   1606 		 frvfdpic_got_section (info),
   1607 		 entry->fd_entry);
   1608 	      return false;
   1609 	    }
   1610 
   1611 	  fd_lazy_rel_offset = ofst;
   1612 
   1613 	  /* A function descriptor used for lazy or local resolving is
   1614 	     initialized such that its high word contains the output
   1615 	     section index in which the PLT entries are located, and
   1616 	     the low word contains the address of the lazy PLT entry
   1617 	     entry point, that must be within the memory region
   1618 	     assigned to that section.  */
   1619 	  lowword = entry->lzplt_entry + 4
   1620 	    + frvfdpic_plt_section (info)->output_offset
   1621 	    + frvfdpic_plt_section (info)->output_section->vma;
   1622 	  highword = _frvfdpic_osec_to_segment
   1623 	    (output_bfd, frvfdpic_plt_section (info)->output_section);
   1624 	}
   1625       else
   1626 	{
   1627 	  /* A function descriptor for a local function gets the index
   1628 	     of the section.  For a non-local function, it's
   1629 	     disregarded.  */
   1630 	  lowword = ad;
   1631 	  if (sec == NULL
   1632 	      || (entry->symndx == -1 && entry->d.h->dynindx != -1
   1633 		  && entry->d.h->dynindx == idx))
   1634 	    highword = 0;
   1635 	  else
   1636 	    highword = _frvfdpic_osec_to_segment
   1637 	      (output_bfd, sec->output_section);
   1638 	}
   1639 
   1640       bfd_put_32 (output_bfd, lowword,
   1641 		  frvfdpic_got_section (info)->contents
   1642 		  + frvfdpic_got_initial_offset (info)
   1643 		  + entry->fd_entry);
   1644       bfd_put_32 (output_bfd, highword,
   1645 		  frvfdpic_got_section (info)->contents
   1646 		  + frvfdpic_got_initial_offset (info)
   1647 		  + entry->fd_entry + 4);
   1648     }
   1649 
   1650   /* Generate code for the PLT entry.  */
   1651   if (entry->plt_entry != (bfd_vma) -1)
   1652     {
   1653       bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
   1654 	+ entry->plt_entry;
   1655 
   1656       BFD_ASSERT (entry->fd_entry);
   1657 
   1658       /* Figure out what kind of PLT entry we need, depending on the
   1659 	 location of the function descriptor within the GOT.  */
   1660       if (entry->fd_entry >= -(1 << (12 - 1))
   1661 	  && entry->fd_entry < (1 << (12 - 1)))
   1662 	{
   1663 	  /* lddi @(gr15, fd_entry), gr14 */
   1664 	  bfd_put_32 (output_bfd,
   1665 		      0x9cccf000 | (entry->fd_entry & ((1 << 12) - 1)),
   1666 		      plt_code);
   1667 	  plt_code += 4;
   1668 	}
   1669       else
   1670 	{
   1671 	  if (entry->fd_entry >= -(1 << (16 - 1))
   1672 	      && entry->fd_entry < (1 << (16 - 1)))
   1673 	    {
   1674 	      /* setlos lo(fd_entry), gr14 */
   1675 	      bfd_put_32 (output_bfd,
   1676 			  0x9cfc0000
   1677 			  | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
   1678 			  plt_code);
   1679 	      plt_code += 4;
   1680 	    }
   1681 	  else
   1682 	    {
   1683 	      /* sethi.p hi(fd_entry), gr14
   1684 		 setlo lo(fd_entry), gr14 */
   1685 	      bfd_put_32 (output_bfd,
   1686 			  0x1cf80000
   1687 			  | ((entry->fd_entry >> 16)
   1688 			     & (((bfd_vma)1 << 16) - 1)),
   1689 			  plt_code);
   1690 	      plt_code += 4;
   1691 	      bfd_put_32 (output_bfd,
   1692 			  0x9cf40000
   1693 			  | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
   1694 			  plt_code);
   1695 	      plt_code += 4;
   1696 	    }
   1697 	  /* ldd @(gr14,gr15),gr14 */
   1698 	  bfd_put_32 (output_bfd, 0x9c08e14f, plt_code);
   1699 	  plt_code += 4;
   1700 	}
   1701       /* jmpl @(gr14,gr0) */
   1702       bfd_put_32 (output_bfd, 0x8030e000, plt_code);
   1703     }
   1704 
   1705   /* Generate code for the lazy PLT entry.  */
   1706   if (entry->lzplt_entry != (bfd_vma) -1)
   1707     {
   1708       bfd_byte *lzplt_code = frvfdpic_plt_section (info)->contents
   1709 	+ entry->lzplt_entry;
   1710       bfd_vma resolverStub_addr;
   1711 
   1712       bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
   1713       lzplt_code += 4;
   1714 
   1715       resolverStub_addr = entry->lzplt_entry / FRVFDPIC_LZPLT_BLOCK_SIZE
   1716 	* FRVFDPIC_LZPLT_BLOCK_SIZE + FRVFDPIC_LZPLT_RESOLV_LOC;
   1717       if (resolverStub_addr >= frvfdpic_plt_initial_offset (info))
   1718 	resolverStub_addr = frvfdpic_plt_initial_offset (info) - 12;
   1719 
   1720       if (entry->lzplt_entry == resolverStub_addr)
   1721 	{
   1722 	  /* This is a lazy PLT entry that includes a resolver call.  */
   1723 	  /* ldd @(gr15,gr0), gr4
   1724 	     jmpl @(gr4,gr0)  */
   1725 	  bfd_put_32 (output_bfd, 0x8808f140, lzplt_code);
   1726 	  bfd_put_32 (output_bfd, 0x80304000, lzplt_code + 4);
   1727 	}
   1728       else
   1729 	{
   1730 	  /* bra  resolverStub */
   1731 	  bfd_put_32 (output_bfd,
   1732 		      0xc01a0000
   1733 		      | (((resolverStub_addr - entry->lzplt_entry)
   1734 			  / 4) & (((bfd_vma)1 << 16) - 1)),
   1735 		      lzplt_code);
   1736 	}
   1737     }
   1738 
   1739   /* Generate relocation for GOT entry holding the TLS offset.  */
   1740   if (entry->tlsoff_entry)
   1741     {
   1742       int idx = dynindx;
   1743       bfd_vma ad = addend;
   1744 
   1745       if (entry->symndx != -1
   1746 	  || FRVFDPIC_SYM_LOCAL (info, entry->d.h))
   1747 	{
   1748 	  /* If the symbol is dynamic but binds locally, use
   1749 	     section+offset.  */
   1750 	  if (sec)
   1751 	    {
   1752 	      if (entry->symndx == -1)
   1753 		ad += entry->d.h->root.u.def.value;
   1754 	      else
   1755 		ad += sym->st_value;
   1756 	      ad += sec->output_offset;
   1757 	      if (sec->output_section
   1758 		  && elf_section_data (sec->output_section))
   1759 		idx = elf_section_data (sec->output_section)->dynindx;
   1760 	      else
   1761 		idx = 0;
   1762 	    }
   1763 	}
   1764 
   1765       /* *ABS*+addend is special for TLS relocations, use only the
   1766 	 addend.  */
   1767       if (bfd_link_executable (info)
   1768 	  && idx == 0
   1769 	  && (bfd_is_abs_section (sec)
   1770 	      || bfd_is_und_section (sec)))
   1771 	;
   1772       /* If we're linking an executable, we can entirely omit the
   1773 	 dynamic relocation if the symbol is local to this module.  */
   1774       else if (bfd_link_executable (info)
   1775 	       && (entry->symndx != -1
   1776 		   || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1777 	{
   1778 	  if (sec)
   1779 	    ad += sec->output_section->vma - tls_biased_base (info);
   1780 	}
   1781       else
   1782 	{
   1783 	  if (idx == 0
   1784 	      && (bfd_is_abs_section (sec)
   1785 		  || bfd_is_und_section (sec)))
   1786 	    {
   1787 	      if (! elf_hash_table (info)->tls_sec)
   1788 		{
   1789 		  (*info->callbacks->undefined_symbol)
   1790 		    (info, "TLS section", elf_hash_table (info)->dynobj,
   1791 		     frvfdpic_got_section (info), entry->tlsoff_entry, true);
   1792 		  return false;
   1793 		}
   1794 	      idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
   1795 	      ad += FRVFDPIC_TLS_BIAS;
   1796 	    }
   1797 	  _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
   1798 				   _bfd_elf_section_offset
   1799 				   (output_bfd, info,
   1800 				    frvfdpic_got_section (info),
   1801 				    frvfdpic_got_initial_offset (info)
   1802 				    + entry->tlsoff_entry)
   1803 				   + frvfdpic_got_section (info)
   1804 				   ->output_section->vma
   1805 				   + frvfdpic_got_section (info)
   1806 				   ->output_offset,
   1807 				   R_FRV_TLSOFF, idx, ad, entry);
   1808 	}
   1809 
   1810       bfd_put_32 (output_bfd, ad,
   1811 		  frvfdpic_got_section (info)->contents
   1812 		  + frvfdpic_got_initial_offset (info)
   1813 		  + entry->tlsoff_entry);
   1814     }
   1815 
   1816   if (entry->tlsdesc_entry)
   1817     {
   1818       int idx = dynindx;
   1819       bfd_vma ad = addend;
   1820 
   1821       /* If the symbol is dynamic but binds locally, use
   1822 	 section+offset.  */
   1823       if (sec && (entry->symndx != -1
   1824 		  || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1825 	{
   1826 	  if (entry->symndx == -1)
   1827 	    ad += entry->d.h->root.u.def.value;
   1828 	  else
   1829 	    ad += sym->st_value;
   1830 	  ad += sec->output_offset;
   1831 	  if (sec->output_section && elf_section_data (sec->output_section))
   1832 	    idx = elf_section_data (sec->output_section)->dynindx;
   1833 	  else
   1834 	    idx = 0;
   1835 	}
   1836 
   1837       /* If we didn't set up a TLS offset entry, but we're linking an
   1838 	 executable and the symbol binds locally, we can use the
   1839 	 module offset in the TLS descriptor in relaxations.  */
   1840       if (bfd_link_executable (info) && ! entry->tlsoff_entry)
   1841 	entry->tlsoff_entry = entry->tlsdesc_entry + 4;
   1842 
   1843       if (bfd_link_pde (info)
   1844 	  && ((idx == 0
   1845 	       && (bfd_is_abs_section (sec)
   1846 		   || bfd_is_und_section (sec)))
   1847 	      || entry->symndx != -1
   1848 	      || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1849 	{
   1850 	  /* *ABS*+addend is special for TLS relocations, use only the
   1851 	     addend for the TLS offset, and take the module id as
   1852 	     0.  */
   1853 	  if (idx == 0
   1854 	      && (bfd_is_abs_section (sec)
   1855 		  || bfd_is_und_section (sec)))
   1856 	    ;
   1857 	  /* For other TLS symbols that bind locally, add the section
   1858 	     TLS offset to the addend.  */
   1859 	  else if (sec)
   1860 	    ad += sec->output_section->vma - tls_biased_base (info);
   1861 
   1862 	  bfd_put_32 (output_bfd,
   1863 		      frvfdpic_plt_section (info)->output_section->vma
   1864 		      + frvfdpic_plt_section (info)->output_offset
   1865 		      + frvfdpic_plt_tls_ret_offset (info),
   1866 		      frvfdpic_got_section (info)->contents
   1867 		      + frvfdpic_got_initial_offset (info)
   1868 		      + entry->tlsdesc_entry);
   1869 
   1870 	  _frvfdpic_add_rofixup (output_bfd,
   1871 				 frvfdpic_gotfixup_section (info),
   1872 				 frvfdpic_got_section (info)
   1873 				 ->output_section->vma
   1874 				 + frvfdpic_got_section (info)
   1875 				 ->output_offset
   1876 				 + frvfdpic_got_initial_offset (info)
   1877 				 + entry->tlsdesc_entry, entry);
   1878 
   1879 	  BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs);
   1880 
   1881 	  /* We've used one of the reserved fixups, so discount it so
   1882 	     that we can check at the end that we've used them
   1883 	     all.  */
   1884 	  frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs--;
   1885 
   1886 	  /* While at that, make sure the ret instruction makes to the
   1887 	     right location in the PLT.  We could do it only when we
   1888 	     got to 0, but since the check at the end will only print
   1889 	     a warning, make sure we have the ret in place in case the
   1890 	     warning is missed.  */
   1891 	  bfd_put_32 (output_bfd, 0xc03a4000,
   1892 		      frvfdpic_plt_section (info)->contents
   1893 		      + frvfdpic_plt_tls_ret_offset (info));
   1894 	}
   1895       else
   1896 	{
   1897 	  if (idx == 0
   1898 	      && (bfd_is_abs_section (sec)
   1899 		  || bfd_is_und_section (sec)))
   1900 	    {
   1901 	      if (! elf_hash_table (info)->tls_sec)
   1902 		{
   1903 		  (*info->callbacks->undefined_symbol)
   1904 		    (info, "TLS section", elf_hash_table (info)->dynobj,
   1905 		     frvfdpic_got_section (info), entry->tlsdesc_entry, true);
   1906 		  return false;
   1907 		}
   1908 	      idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
   1909 	      ad += FRVFDPIC_TLS_BIAS;
   1910 	    }
   1911 
   1912 	  _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
   1913 				   _bfd_elf_section_offset
   1914 				   (output_bfd, info,
   1915 				    frvfdpic_got_section (info),
   1916 				    frvfdpic_got_initial_offset (info)
   1917 				    + entry->tlsdesc_entry)
   1918 				   + frvfdpic_got_section (info)
   1919 				   ->output_section->vma
   1920 				   + frvfdpic_got_section (info)
   1921 				   ->output_offset,
   1922 				   R_FRV_TLSDESC_VALUE, idx, ad, entry);
   1923 
   1924 	  bfd_put_32 (output_bfd, 0,
   1925 		      frvfdpic_got_section (info)->contents
   1926 		      + frvfdpic_got_initial_offset (info)
   1927 		      + entry->tlsdesc_entry);
   1928 	}
   1929 
   1930       bfd_put_32 (output_bfd, ad,
   1931 		  frvfdpic_got_section (info)->contents
   1932 		  + frvfdpic_got_initial_offset (info)
   1933 		  + entry->tlsdesc_entry + 4);
   1934     }
   1935 
   1936   /* Generate code for the get-TLS-offset PLT entry.  */
   1937   if (entry->tlsplt_entry != (bfd_vma) -1)
   1938     {
   1939       bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
   1940 	+ entry->tlsplt_entry;
   1941 
   1942       if (bfd_link_executable (info)
   1943 	  && (entry->symndx != -1
   1944 	      || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
   1945 	{
   1946 	  int idx = dynindx;
   1947 	  bfd_vma ad = addend;
   1948 
   1949 	  /* sec may be NULL when referencing an undefweak symbol
   1950 	     while linking a static executable.  */
   1951 	  if (!sec)
   1952 	    {
   1953 	      BFD_ASSERT (entry->symndx == -1
   1954 			  && entry->d.h->root.type == bfd_link_hash_undefweak);
   1955 	    }
   1956 	  else
   1957 	    {
   1958 	      if (entry->symndx == -1)
   1959 		ad += entry->d.h->root.u.def.value;
   1960 	      else
   1961 		ad += sym->st_value;
   1962 	      ad += sec->output_offset;
   1963 	      if (sec->output_section
   1964 		  && elf_section_data (sec->output_section))
   1965 		idx = elf_section_data (sec->output_section)->dynindx;
   1966 	      else
   1967 		idx = 0;
   1968 	    }
   1969 
   1970 	  /* *ABS*+addend is special for TLS relocations, use only the
   1971 	     addend for the TLS offset, and take the module id as
   1972 	     0.  */
   1973 	  if (idx == 0
   1974 	      && (bfd_is_abs_section (sec)
   1975 		  || bfd_is_und_section (sec)))
   1976 	    ;
   1977 	  /* For other TLS symbols that bind locally, add the section
   1978 	     TLS offset to the addend.  */
   1979 	  else if (sec)
   1980 	    ad += sec->output_section->vma - tls_biased_base (info);
   1981 
   1982 	  if ((bfd_signed_vma)ad >= -(1 << (16 - 1))
   1983 	      && (bfd_signed_vma)ad < (1 << (16 - 1)))
   1984 	    {
   1985 	      /* setlos lo(ad), gr9 */
   1986 	      bfd_put_32 (output_bfd,
   1987 			  0x92fc0000
   1988 			  | (ad
   1989 			     & (((bfd_vma)1 << 16) - 1)),
   1990 			  plt_code);
   1991 	      plt_code += 4;
   1992 	    }
   1993 	  else
   1994 	    {
   1995 	      /* sethi.p hi(ad), gr9
   1996 		 setlo lo(ad), gr9 */
   1997 	      bfd_put_32 (output_bfd,
   1998 			  0x12f80000
   1999 			  | ((ad >> 16)
   2000 			     & (((bfd_vma)1 << 16) - 1)),
   2001 			  plt_code);
   2002 	      plt_code += 4;
   2003 	      bfd_put_32 (output_bfd,
   2004 			  0x92f40000
   2005 			  | (ad
   2006 			     & (((bfd_vma)1 << 16) - 1)),
   2007 			  plt_code);
   2008 	      plt_code += 4;
   2009 	    }
   2010 	  /* ret */
   2011 	  bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
   2012 	}
   2013       else if (entry->tlsoff_entry)
   2014 	{
   2015 	  /* Figure out what kind of PLT entry we need, depending on the
   2016 	     location of the TLS descriptor within the GOT.  */
   2017 	  if (entry->tlsoff_entry >= -(1 << (12 - 1))
   2018 	      && entry->tlsoff_entry < (1 << (12 - 1)))
   2019 	    {
   2020 	      /* ldi @(gr15, tlsoff_entry), gr9 */
   2021 	      bfd_put_32 (output_bfd,
   2022 			  0x92c8f000 | (entry->tlsoff_entry
   2023 					& ((1 << 12) - 1)),
   2024 			  plt_code);
   2025 	      plt_code += 4;
   2026 	    }
   2027 	  else
   2028 	    {
   2029 	      if (entry->tlsoff_entry >= -(1 << (16 - 1))
   2030 		  && entry->tlsoff_entry < (1 << (16 - 1)))
   2031 		{
   2032 		  /* setlos lo(tlsoff_entry), gr8 */
   2033 		  bfd_put_32 (output_bfd,
   2034 			      0x90fc0000
   2035 			      | (entry->tlsoff_entry
   2036 				 & (((bfd_vma)1 << 16) - 1)),
   2037 			      plt_code);
   2038 		  plt_code += 4;
   2039 		}
   2040 	      else
   2041 		{
   2042 		  /* sethi.p hi(tlsoff_entry), gr8
   2043 		     setlo lo(tlsoff_entry), gr8 */
   2044 		  bfd_put_32 (output_bfd,
   2045 			      0x10f80000
   2046 			      | ((entry->tlsoff_entry >> 16)
   2047 				 & (((bfd_vma)1 << 16) - 1)),
   2048 			      plt_code);
   2049 		  plt_code += 4;
   2050 		  bfd_put_32 (output_bfd,
   2051 			      0x90f40000
   2052 			      | (entry->tlsoff_entry
   2053 				 & (((bfd_vma)1 << 16) - 1)),
   2054 			      plt_code);
   2055 		  plt_code += 4;
   2056 		}
   2057 	      /* ld @(gr15,gr8),gr9 */
   2058 	      bfd_put_32 (output_bfd, 0x9008f108, plt_code);
   2059 	      plt_code += 4;
   2060 	    }
   2061 	  /* ret */
   2062 	  bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
   2063 	}
   2064       else
   2065 	{
   2066 	  BFD_ASSERT (entry->tlsdesc_entry);
   2067 
   2068 	  /* Figure out what kind of PLT entry we need, depending on the
   2069 	     location of the TLS descriptor within the GOT.  */
   2070 	  if (entry->tlsdesc_entry >= -(1 << (12 - 1))
   2071 	      && entry->tlsdesc_entry < (1 << (12 - 1)))
   2072 	    {
   2073 	      /* lddi @(gr15, tlsdesc_entry), gr8 */
   2074 	      bfd_put_32 (output_bfd,
   2075 			  0x90ccf000 | (entry->tlsdesc_entry
   2076 					& ((1 << 12) - 1)),
   2077 			  plt_code);
   2078 	      plt_code += 4;
   2079 	    }
   2080 	  else
   2081 	    {
   2082 	      if (entry->tlsdesc_entry >= -(1 << (16 - 1))
   2083 		  && entry->tlsdesc_entry < (1 << (16 - 1)))
   2084 		{
   2085 		  /* setlos lo(tlsdesc_entry), gr8 */
   2086 		  bfd_put_32 (output_bfd,
   2087 			      0x90fc0000
   2088 			      | (entry->tlsdesc_entry
   2089 				 & (((bfd_vma)1 << 16) - 1)),
   2090 			      plt_code);
   2091 		  plt_code += 4;
   2092 		}
   2093 	      else
   2094 		{
   2095 		  /* sethi.p hi(tlsdesc_entry), gr8
   2096 		     setlo lo(tlsdesc_entry), gr8 */
   2097 		  bfd_put_32 (output_bfd,
   2098 			      0x10f80000
   2099 			      | ((entry->tlsdesc_entry >> 16)
   2100 				 & (((bfd_vma)1 << 16) - 1)),
   2101 			      plt_code);
   2102 		  plt_code += 4;
   2103 		  bfd_put_32 (output_bfd,
   2104 			      0x90f40000
   2105 			      | (entry->tlsdesc_entry
   2106 				 & (((bfd_vma)1 << 16) - 1)),
   2107 			      plt_code);
   2108 		  plt_code += 4;
   2109 		}
   2110 	      /* ldd @(gr15,gr8),gr8 */
   2111 	      bfd_put_32 (output_bfd, 0x9008f148, plt_code);
   2112 	      plt_code += 4;
   2113 	    }
   2114 	  /* jmpl @(gr8,gr0) */
   2115 	  bfd_put_32 (output_bfd, 0x80308000, plt_code);
   2116 	}
   2117     }
   2118 
   2119   return true;
   2120 }
   2121 
   2122 /* Handle an FRV small data reloc.  */
   2123 
   2124 static bfd_reloc_status_type
   2125 elf32_frv_relocate_gprel12 (struct bfd_link_info *info,
   2126 			    bfd *input_bfd,
   2127 			    asection *input_section,
   2128 			    Elf_Internal_Rela *relocation,
   2129 			    bfd_byte *contents,
   2130 			    bfd_vma value)
   2131 {
   2132   bfd_vma insn;
   2133   bfd_vma gp;
   2134   struct bfd_link_hash_entry *h;
   2135 
   2136   h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
   2137 
   2138   gp = (h->u.def.value
   2139 	+ h->u.def.section->output_section->vma
   2140 	+ h->u.def.section->output_offset);
   2141 
   2142   value -= input_section->output_section->vma;
   2143   value -= (gp - input_section->output_section->vma);
   2144 
   2145   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
   2146 
   2147   value += relocation->r_addend;
   2148 
   2149   if ((long) value > 0x7ff || (long) value < -0x800)
   2150     return bfd_reloc_overflow;
   2151 
   2152   bfd_put_32 (input_bfd,
   2153 	      (insn & 0xfffff000) | (value & 0xfff),
   2154 	      contents + relocation->r_offset);
   2155 
   2156   return bfd_reloc_ok;
   2157 }
   2158 
   2159 /* Handle an FRV small data reloc. for the u12 field.  */
   2160 
   2161 static bfd_reloc_status_type
   2162 elf32_frv_relocate_gprelu12 (struct bfd_link_info *info,
   2163 			     bfd *input_bfd,
   2164 			     asection *input_section,
   2165 			     Elf_Internal_Rela *relocation,
   2166 			     bfd_byte *contents,
   2167 			     bfd_vma value)
   2168 {
   2169   bfd_vma insn;
   2170   bfd_vma gp;
   2171   struct bfd_link_hash_entry *h;
   2172   bfd_vma mask;
   2173 
   2174   h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
   2175 
   2176   gp = (h->u.def.value
   2177 	+ h->u.def.section->output_section->vma
   2178 	+ h->u.def.section->output_offset);
   2179 
   2180   value -= input_section->output_section->vma;
   2181   value -= (gp - input_section->output_section->vma);
   2182 
   2183   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
   2184 
   2185   value += relocation->r_addend;
   2186 
   2187   if ((long) value > 0x7ff || (long) value < -0x800)
   2188     return bfd_reloc_overflow;
   2189 
   2190   /* The high 6 bits go into bits 17-12. The low 6 bits go into bits 5-0.  */
   2191   mask = 0x3f03f;
   2192   insn = (insn & ~mask) | ((value & 0xfc0) << 12) | (value & 0x3f);
   2193 
   2194   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
   2195 
   2196   return bfd_reloc_ok;
   2197 }
   2198 
   2199 /* Handle an FRV ELF HI16 reloc.  */
   2200 
   2201 static bfd_reloc_status_type
   2202 elf32_frv_relocate_hi16 (bfd *input_bfd,
   2203 			 Elf_Internal_Rela *relhi,
   2204 			 bfd_byte *contents,
   2205 			 bfd_vma value)
   2206 {
   2207   bfd_vma insn;
   2208 
   2209   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
   2210 
   2211   value += relhi->r_addend;
   2212   value = ((value >> 16) & 0xffff);
   2213 
   2214   insn = (insn & 0xffff0000) | value;
   2215 
   2216   if ((long) value > 0xffff || (long) value < -0x10000)
   2217     return bfd_reloc_overflow;
   2218 
   2219   bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
   2220   return bfd_reloc_ok;
   2221 
   2222 }
   2223 static bfd_reloc_status_type
   2224 elf32_frv_relocate_lo16 (bfd *input_bfd,
   2225 			 Elf_Internal_Rela *rello,
   2226 			 bfd_byte *contents,
   2227 			 bfd_vma value)
   2228 {
   2229   bfd_vma insn;
   2230 
   2231   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
   2232 
   2233   value += rello->r_addend;
   2234   value = value & 0xffff;
   2235 
   2236   insn = (insn & 0xffff0000) | value;
   2237 
   2238   if ((long) value > 0xffff || (long) value < -0x10000)
   2239     return bfd_reloc_overflow;
   2240 
   2241   bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
   2242   return bfd_reloc_ok;
   2243 }
   2244 
   2245 /* Perform the relocation for the CALL label24 instruction.  */
   2246 
   2247 static bfd_reloc_status_type
   2248 elf32_frv_relocate_label24 (bfd *input_bfd,
   2249 			    asection *input_section,
   2250 			    Elf_Internal_Rela *rello,
   2251 			    bfd_byte *contents,
   2252 			    bfd_vma value)
   2253 {
   2254   bfd_vma insn;
   2255   bfd_vma label6;
   2256   bfd_vma label18;
   2257 
   2258   /* The format for the call instruction is:
   2259 
   2260     0 000000 0001111 000000000000000000
   2261       label6 opcode  label18
   2262 
   2263     The branch calculation is: pc + (4*label24)
   2264     where label24 is the concatenation of label6 and label18.  */
   2265 
   2266   /* Grab the instruction.  */
   2267   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
   2268 
   2269   value -= input_section->output_section->vma + input_section->output_offset;
   2270   value -= rello->r_offset;
   2271   value += rello->r_addend;
   2272 
   2273   value = value >> 2;
   2274 
   2275   label6  = value & 0xfc0000;
   2276   label6  = label6 << 7;
   2277 
   2278   label18 = value & 0x3ffff;
   2279 
   2280   insn = insn & 0x803c0000;
   2281   insn = insn | label6;
   2282   insn = insn | label18;
   2283 
   2284   bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
   2285 
   2286   return bfd_reloc_ok;
   2287 }
   2288 
   2289 static bfd_reloc_status_type
   2290 elf32_frv_relocate_gprelhi (struct bfd_link_info *info,
   2291 			    bfd *input_bfd,
   2292 			    asection *input_section,
   2293 			    Elf_Internal_Rela *relocation,
   2294 			    bfd_byte *contents,
   2295 			    bfd_vma value)
   2296 {
   2297   bfd_vma insn;
   2298   bfd_vma gp;
   2299   struct bfd_link_hash_entry *h;
   2300 
   2301   h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
   2302 
   2303   gp = (h->u.def.value
   2304 	+ h->u.def.section->output_section->vma
   2305 	+ h->u.def.section->output_offset);
   2306 
   2307   value -= input_section->output_section->vma;
   2308   value -= (gp - input_section->output_section->vma);
   2309   value += relocation->r_addend;
   2310   value = ((value >> 16) & 0xffff);
   2311 
   2312   if ((long) value > 0xffff || (long) value < -0x10000)
   2313     return bfd_reloc_overflow;
   2314 
   2315   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
   2316   insn = (insn & 0xffff0000) | value;
   2317 
   2318   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
   2319   return bfd_reloc_ok;
   2320 }
   2321 
   2322 static bfd_reloc_status_type
   2323 elf32_frv_relocate_gprello (struct bfd_link_info *info,
   2324 			    bfd *input_bfd,
   2325 			    asection *input_section,
   2326 			    Elf_Internal_Rela *relocation,
   2327 			    bfd_byte *contents,
   2328 			    bfd_vma value)
   2329 {
   2330   bfd_vma insn;
   2331   bfd_vma gp;
   2332   struct bfd_link_hash_entry *h;
   2333 
   2334   h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
   2335 
   2336   gp = (h->u.def.value
   2337 	+ h->u.def.section->output_section->vma
   2338 	+ h->u.def.section->output_offset);
   2339 
   2340   value -= input_section->output_section->vma;
   2341   value -= (gp - input_section->output_section->vma);
   2342   value += relocation->r_addend;
   2343   value = value & 0xffff;
   2344 
   2345   if ((long) value > 0xffff || (long) value < -0x10000)
   2346     return bfd_reloc_overflow;
   2347 
   2348   insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
   2349   insn = (insn & 0xffff0000) | value;
   2350 
   2351   bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
   2352 
   2353  return bfd_reloc_ok;
   2354 }
   2355 
   2356 static reloc_howto_type *
   2357 frv_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2358 		       bfd_reloc_code_real_type code)
   2359 {
   2360   switch (code)
   2361     {
   2362     default:
   2363       break;
   2364 
   2365     case BFD_RELOC_NONE:
   2366       return &elf32_frv_howto_table[ (int) R_FRV_NONE];
   2367 
   2368     case BFD_RELOC_32:
   2369       if (elf_elfheader (abfd)->e_type == ET_EXEC
   2370 	  || elf_elfheader (abfd)->e_type == ET_DYN)
   2371 	return &elf32_frv_rel_32_howto;
   2372       /* Fall through.  */
   2373     case BFD_RELOC_CTOR:
   2374       return &elf32_frv_howto_table[ (int) R_FRV_32];
   2375 
   2376     case BFD_RELOC_FRV_LABEL16:
   2377       return &elf32_frv_howto_table[ (int) R_FRV_LABEL16];
   2378 
   2379     case BFD_RELOC_FRV_LABEL24:
   2380       return &elf32_frv_howto_table[ (int) R_FRV_LABEL24];
   2381 
   2382     case BFD_RELOC_FRV_LO16:
   2383       return &elf32_frv_howto_table[ (int) R_FRV_LO16];
   2384 
   2385     case BFD_RELOC_FRV_HI16:
   2386       return &elf32_frv_howto_table[ (int) R_FRV_HI16];
   2387 
   2388     case BFD_RELOC_FRV_GPREL12:
   2389       return &elf32_frv_howto_table[ (int) R_FRV_GPREL12];
   2390 
   2391     case BFD_RELOC_FRV_GPRELU12:
   2392       return &elf32_frv_howto_table[ (int) R_FRV_GPRELU12];
   2393 
   2394     case BFD_RELOC_FRV_GPREL32:
   2395       return &elf32_frv_howto_table[ (int) R_FRV_GPREL32];
   2396 
   2397     case BFD_RELOC_FRV_GPRELHI:
   2398       return &elf32_frv_howto_table[ (int) R_FRV_GPRELHI];
   2399 
   2400     case BFD_RELOC_FRV_GPRELLO:
   2401       return &elf32_frv_howto_table[ (int) R_FRV_GPRELLO];
   2402 
   2403     case BFD_RELOC_FRV_GOT12:
   2404       return &elf32_frv_howto_table[ (int) R_FRV_GOT12];
   2405 
   2406     case BFD_RELOC_FRV_GOTHI:
   2407       return &elf32_frv_howto_table[ (int) R_FRV_GOTHI];
   2408 
   2409     case BFD_RELOC_FRV_GOTLO:
   2410       return &elf32_frv_howto_table[ (int) R_FRV_GOTLO];
   2411 
   2412     case BFD_RELOC_FRV_FUNCDESC:
   2413       if (elf_elfheader (abfd)->e_type == ET_EXEC
   2414 	  || elf_elfheader (abfd)->e_type == ET_DYN)
   2415 	return &elf32_frv_rel_funcdesc_howto;
   2416       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC];
   2417 
   2418     case BFD_RELOC_FRV_FUNCDESC_GOT12:
   2419       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOT12];
   2420 
   2421     case BFD_RELOC_FRV_FUNCDESC_GOTHI:
   2422       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTHI];
   2423 
   2424     case BFD_RELOC_FRV_FUNCDESC_GOTLO:
   2425       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTLO];
   2426 
   2427     case BFD_RELOC_FRV_FUNCDESC_VALUE:
   2428       if (elf_elfheader (abfd)->e_type == ET_EXEC
   2429 	  || elf_elfheader (abfd)->e_type == ET_DYN)
   2430 	return &elf32_frv_rel_funcdesc_value_howto;
   2431       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_VALUE];
   2432 
   2433     case BFD_RELOC_FRV_FUNCDESC_GOTOFF12:
   2434       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFF12];
   2435 
   2436     case BFD_RELOC_FRV_FUNCDESC_GOTOFFHI:
   2437       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFHI];
   2438 
   2439     case BFD_RELOC_FRV_FUNCDESC_GOTOFFLO:
   2440       return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFLO];
   2441 
   2442     case BFD_RELOC_FRV_GOTOFF12:
   2443       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFF12];
   2444 
   2445     case BFD_RELOC_FRV_GOTOFFHI:
   2446       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFHI];
   2447 
   2448     case BFD_RELOC_FRV_GOTOFFLO:
   2449       return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFLO];
   2450 
   2451     case BFD_RELOC_FRV_GETTLSOFF:
   2452       return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF];
   2453 
   2454     case BFD_RELOC_FRV_TLSDESC_VALUE:
   2455       if (elf_elfheader (abfd)->e_type == ET_EXEC
   2456 	  || elf_elfheader (abfd)->e_type == ET_DYN)
   2457 	return &elf32_frv_rel_tlsdesc_value_howto;
   2458       return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_VALUE];
   2459 
   2460     case BFD_RELOC_FRV_GOTTLSDESC12:
   2461       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESC12];
   2462 
   2463     case BFD_RELOC_FRV_GOTTLSDESCHI:
   2464       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCHI];
   2465 
   2466     case BFD_RELOC_FRV_GOTTLSDESCLO:
   2467       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCLO];
   2468 
   2469     case BFD_RELOC_FRV_TLSMOFF12:
   2470       return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF12];
   2471 
   2472     case BFD_RELOC_FRV_TLSMOFFHI:
   2473       return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFHI];
   2474 
   2475     case BFD_RELOC_FRV_TLSMOFFLO:
   2476       return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFLO];
   2477 
   2478     case BFD_RELOC_FRV_GOTTLSOFF12:
   2479       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFF12];
   2480 
   2481     case BFD_RELOC_FRV_GOTTLSOFFHI:
   2482       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFHI];
   2483 
   2484     case BFD_RELOC_FRV_GOTTLSOFFLO:
   2485       return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFLO];
   2486 
   2487     case BFD_RELOC_FRV_TLSOFF:
   2488       if (elf_elfheader (abfd)->e_type == ET_EXEC
   2489 	  || elf_elfheader (abfd)->e_type == ET_DYN)
   2490 	return &elf32_frv_rel_tlsoff_howto;
   2491       return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF];
   2492 
   2493     case BFD_RELOC_FRV_TLSDESC_RELAX:
   2494       return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_RELAX];
   2495 
   2496     case BFD_RELOC_FRV_GETTLSOFF_RELAX:
   2497       return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF_RELAX];
   2498 
   2499     case BFD_RELOC_FRV_TLSOFF_RELAX:
   2500       return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF_RELAX];
   2501 
   2502     case BFD_RELOC_FRV_TLSMOFF:
   2503       return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF];
   2504 
   2505     case BFD_RELOC_VTABLE_INHERIT:
   2506       return &elf32_frv_vtinherit_howto;
   2507 
   2508     case BFD_RELOC_VTABLE_ENTRY:
   2509       return &elf32_frv_vtentry_howto;
   2510     }
   2511 
   2512   return NULL;
   2513 }
   2514 
   2515 static reloc_howto_type *
   2516 frv_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
   2517 {
   2518   unsigned int i;
   2519 
   2520   for (i = 0;
   2521        i < sizeof (elf32_frv_howto_table) / sizeof (elf32_frv_howto_table[0]);
   2522        i++)
   2523     if (elf32_frv_howto_table[i].name != NULL
   2524 	&& strcasecmp (elf32_frv_howto_table[i].name, r_name) == 0)
   2525       return &elf32_frv_howto_table[i];
   2526 
   2527   if (strcasecmp (elf32_frv_vtinherit_howto.name, r_name) == 0)
   2528     return &elf32_frv_vtinherit_howto;
   2529   if (strcasecmp (elf32_frv_vtentry_howto.name, r_name) == 0)
   2530     return &elf32_frv_vtentry_howto;
   2531 
   2532   return NULL;
   2533 }
   2534 
   2535 /* Set the howto pointer for an FRV ELF reloc.  */
   2536 
   2537 static bool
   2538 frv_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
   2539 			arelent *cache_ptr,
   2540 			Elf_Internal_Rela *dst)
   2541 {
   2542   unsigned int r_type;
   2543 
   2544   r_type = ELF32_R_TYPE (dst->r_info);
   2545   switch (r_type)
   2546     {
   2547     case R_FRV_GNU_VTINHERIT:
   2548       cache_ptr->howto = &elf32_frv_vtinherit_howto;
   2549       break;
   2550 
   2551     case R_FRV_GNU_VTENTRY:
   2552       cache_ptr->howto = &elf32_frv_vtentry_howto;
   2553       break;
   2554 
   2555     default:
   2556       if (r_type >= ARRAY_SIZE (elf32_frv_howto_table))
   2557 	{
   2558 	  /* xgettext:c-format */
   2559 	  _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
   2560 			      abfd, r_type);
   2561 	  bfd_set_error (bfd_error_bad_value);
   2562 	  return false;
   2563 	}
   2564       cache_ptr->howto = & elf32_frv_howto_table [r_type];
   2565       break;
   2566     }
   2567   return true;
   2568 }
   2569 
   2570 /* Set the howto pointer for an FRV ELF REL reloc.  */
   2571 
   2572 static bool
   2573 frvfdpic_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
   2574 			    arelent *cache_ptr, Elf_Internal_Rela *dst)
   2575 {
   2576   unsigned int r_type;
   2577 
   2578   r_type = ELF32_R_TYPE (dst->r_info);
   2579   switch (r_type)
   2580     {
   2581     case R_FRV_32:
   2582       cache_ptr->howto = &elf32_frv_rel_32_howto;
   2583       break;
   2584 
   2585     case R_FRV_FUNCDESC:
   2586       cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
   2587       break;
   2588 
   2589     case R_FRV_FUNCDESC_VALUE:
   2590       cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
   2591       break;
   2592 
   2593     case R_FRV_TLSDESC_VALUE:
   2594       cache_ptr->howto = &elf32_frv_rel_tlsdesc_value_howto;
   2595       break;
   2596 
   2597     case R_FRV_TLSOFF:
   2598       cache_ptr->howto = &elf32_frv_rel_tlsoff_howto;
   2599       break;
   2600 
   2601     default:
   2602       cache_ptr->howto = NULL;
   2603       return false;
   2604     }
   2605   return true;
   2606 }
   2607 
   2608 /* Perform a single relocation.  By default we use the standard BFD
   2610    routines, but a few relocs, we have to do them ourselves.  */
   2611 
   2612 static bfd_reloc_status_type
   2613 frv_final_link_relocate (reloc_howto_type *howto,
   2614 			 bfd *input_bfd,
   2615 			 asection *input_section,
   2616 			 bfd_byte *contents,
   2617 			 Elf_Internal_Rela *rel,
   2618 			 bfd_vma relocation)
   2619 {
   2620   return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2621 				   contents, rel->r_offset, relocation,
   2622 				   rel->r_addend);
   2623 }
   2624 
   2625 
   2626 /* Relocate an FRV ELF section.
   2628 
   2629    The RELOCATE_SECTION function is called by the new ELF backend linker
   2630    to handle the relocations for a section.
   2631 
   2632    The relocs are always passed as Rela structures; if the section
   2633    actually uses Rel structures, the r_addend field will always be
   2634    zero.
   2635 
   2636    This function is responsible for adjusting the section contents as
   2637    necessary, and (if using Rela relocs and generating a relocatable
   2638    output file) adjusting the reloc addend as necessary.
   2639 
   2640    This function does not have to worry about setting the reloc
   2641    address or the reloc symbol index.
   2642 
   2643    LOCAL_SYMS is a pointer to the swapped in local symbols.
   2644 
   2645    LOCAL_SECTIONS is an array giving the section in the input file
   2646    corresponding to the st_shndx field of each local symbol.
   2647 
   2648    The global hash table entry for the global symbols can be found
   2649    via elf_sym_hashes (input_bfd).
   2650 
   2651    When generating relocatable output, this function must handle
   2652    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   2653    going to be the section symbol corresponding to the output
   2654    section, which means that the addend must be adjusted
   2655    accordingly.  */
   2656 
   2657 static int
   2658 elf32_frv_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
   2659 			    struct bfd_link_info *info,
   2660 			    bfd *input_bfd,
   2661 			    asection *input_section,
   2662 			    bfd_byte *contents,
   2663 			    Elf_Internal_Rela *relocs,
   2664 			    Elf_Internal_Sym *local_syms,
   2665 			    asection **local_sections)
   2666 {
   2667   Elf_Internal_Shdr *symtab_hdr;
   2668   struct elf_link_hash_entry **sym_hashes;
   2669   Elf_Internal_Rela *rel;
   2670   Elf_Internal_Rela *relend;
   2671   unsigned isec_segment, got_segment, plt_segment, gprel_segment, tls_segment,
   2672     check_segment[2];
   2673   int silence_segment_error = !bfd_link_pic (info);
   2674   unsigned long insn;
   2675 
   2676   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   2677   sym_hashes = elf_sym_hashes (input_bfd);
   2678   relend     = relocs + input_section->reloc_count;
   2679 
   2680   isec_segment = _frvfdpic_osec_to_segment (output_bfd,
   2681 					    input_section->output_section);
   2682   if (IS_FDPIC (output_bfd) && frvfdpic_got_section (info))
   2683     got_segment = _frvfdpic_osec_to_segment (output_bfd,
   2684 					     frvfdpic_got_section (info)
   2685 					     ->output_section);
   2686   else
   2687     got_segment = -1;
   2688   if (IS_FDPIC (output_bfd) && frvfdpic_gotfixup_section (info))
   2689     gprel_segment = _frvfdpic_osec_to_segment (output_bfd,
   2690 					       frvfdpic_gotfixup_section (info)
   2691 					       ->output_section);
   2692   else
   2693     gprel_segment = -1;
   2694   if (IS_FDPIC (output_bfd) && frvfdpic_plt_section (info))
   2695     plt_segment = _frvfdpic_osec_to_segment (output_bfd,
   2696 					     frvfdpic_plt_section (info)
   2697 					     ->output_section);
   2698   else
   2699     plt_segment = -1;
   2700   if (elf_hash_table (info)->tls_sec)
   2701     tls_segment = _frvfdpic_osec_to_segment (output_bfd,
   2702 					     elf_hash_table (info)->tls_sec);
   2703   else
   2704     tls_segment = -1;
   2705 
   2706   for (rel = relocs; rel < relend; rel ++)
   2707     {
   2708       reloc_howto_type *howto;
   2709       unsigned long r_symndx;
   2710       Elf_Internal_Sym *sym;
   2711       asection *sec;
   2712       struct elf_link_hash_entry *h;
   2713       bfd_vma relocation;
   2714       bfd_reloc_status_type r;
   2715       const char *name;
   2716       int r_type;
   2717       asection *osec;
   2718       struct frvfdpic_relocs_info *picrel = NULL;
   2719       bfd_vma orig_addend = rel->r_addend;
   2720 
   2721       r_type = ELF32_R_TYPE (rel->r_info);
   2722 
   2723       if (   r_type == R_FRV_GNU_VTINHERIT
   2724 	  || r_type == R_FRV_GNU_VTENTRY)
   2725 	continue;
   2726 
   2727       r_symndx = ELF32_R_SYM (rel->r_info);
   2728       howto  = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
   2729       h      = NULL;
   2730       sym    = NULL;
   2731       sec    = NULL;
   2732 
   2733       if (r_symndx < symtab_hdr->sh_info)
   2734 	{
   2735 	  sym = local_syms + r_symndx;
   2736 	  osec = sec = local_sections [r_symndx];
   2737 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   2738 
   2739 	  name = bfd_elf_string_from_elf_section
   2740 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
   2741 	  if (name == NULL || name[0] == 0)
   2742 	    name = bfd_section_name (sec);
   2743 	}
   2744       else
   2745 	{
   2746 	  bool warned, ignored;
   2747 	  bool unresolved_reloc;
   2748 
   2749 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   2750 				   r_symndx, symtab_hdr, sym_hashes,
   2751 				   h, sec, relocation,
   2752 				   unresolved_reloc, warned, ignored);
   2753 	  osec = sec;
   2754 	  name = h->root.root.string;
   2755 	}
   2756 
   2757       if (sec != NULL && discarded_section (sec))
   2758 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   2759 					 rel, 1, relend, howto, 0, contents);
   2760 
   2761       if (bfd_link_relocatable (info))
   2762 	continue;
   2763 
   2764       if (r_type != R_FRV_TLSMOFF
   2765 	  && h != NULL
   2766 	  && (h->root.type == bfd_link_hash_defined
   2767 	      || h->root.type == bfd_link_hash_defweak)
   2768 	  && !FRVFDPIC_SYM_LOCAL (info, h))
   2769 	{
   2770 	  osec = sec = NULL;
   2771 	  relocation = 0;
   2772 	}
   2773 
   2774       switch (r_type)
   2775 	{
   2776 	case R_FRV_LABEL24:
   2777 	case R_FRV_32:
   2778 	  if (! IS_FDPIC (output_bfd))
   2779 	    goto non_fdpic;
   2780 	  /* Fall through.  */
   2781 
   2782 	case R_FRV_GOT12:
   2783 	case R_FRV_GOTHI:
   2784 	case R_FRV_GOTLO:
   2785 	case R_FRV_FUNCDESC_GOT12:
   2786 	case R_FRV_FUNCDESC_GOTHI:
   2787 	case R_FRV_FUNCDESC_GOTLO:
   2788 	case R_FRV_GOTOFF12:
   2789 	case R_FRV_GOTOFFHI:
   2790 	case R_FRV_GOTOFFLO:
   2791 	case R_FRV_FUNCDESC_GOTOFF12:
   2792 	case R_FRV_FUNCDESC_GOTOFFHI:
   2793 	case R_FRV_FUNCDESC_GOTOFFLO:
   2794 	case R_FRV_FUNCDESC:
   2795 	case R_FRV_FUNCDESC_VALUE:
   2796 	case R_FRV_GETTLSOFF:
   2797 	case R_FRV_TLSDESC_VALUE:
   2798 	case R_FRV_GOTTLSDESC12:
   2799 	case R_FRV_GOTTLSDESCHI:
   2800 	case R_FRV_GOTTLSDESCLO:
   2801 	case R_FRV_TLSMOFF12:
   2802 	case R_FRV_TLSMOFFHI:
   2803 	case R_FRV_TLSMOFFLO:
   2804 	case R_FRV_GOTTLSOFF12:
   2805 	case R_FRV_GOTTLSOFFHI:
   2806 	case R_FRV_GOTTLSOFFLO:
   2807 	case R_FRV_TLSOFF:
   2808 	case R_FRV_TLSDESC_RELAX:
   2809 	case R_FRV_GETTLSOFF_RELAX:
   2810 	case R_FRV_TLSOFF_RELAX:
   2811 	case R_FRV_TLSMOFF:
   2812 	  if ((input_section->flags & SEC_ALLOC) == 0)
   2813 	    break;
   2814 
   2815 	  if (h != NULL)
   2816 	    picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info
   2817 						      (info), input_bfd, h,
   2818 						      orig_addend, INSERT);
   2819 	  else
   2820 	    /* In order to find the entry we created before, we must
   2821 	       use the original addend, not the one that may have been
   2822 	       modified by _bfd_elf_rela_local_sym().  */
   2823 	    picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
   2824 						     (info), input_bfd, r_symndx,
   2825 						     orig_addend, INSERT);
   2826 	  if (! picrel)
   2827 	    return false;
   2828 
   2829 	  if (!_frvfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
   2830 						      osec, sym,
   2831 						      rel->r_addend))
   2832 	    {
   2833 	      info->callbacks->einfo
   2834 		/* xgettext:c-format */
   2835 		(_("%H: relocation to `%s+%v'"
   2836 		   " may have caused the error above\n"),
   2837 		 input_bfd, input_section, rel->r_offset, name, rel->r_addend);
   2838 	      return false;
   2839 	    }
   2840 
   2841 	  break;
   2842 
   2843 	default:
   2844 	non_fdpic:
   2845 	  picrel = NULL;
   2846 	  if (h
   2847 	      && ! FRVFDPIC_SYM_LOCAL (info, h)
   2848 	      && _bfd_elf_section_offset (output_bfd, info, input_section,
   2849 					  rel->r_offset) != (bfd_vma) -1)
   2850 	    {
   2851 	      info->callbacks->einfo
   2852 		(_("%H: relocation references symbol"
   2853 		   " not defined in the module\n"),
   2854 		 input_bfd, input_section, rel->r_offset);
   2855 	      return false;
   2856 	    }
   2857 	  break;
   2858 	}
   2859 
   2860       switch (r_type)
   2861 	{
   2862 	case R_FRV_GETTLSOFF:
   2863 	case R_FRV_TLSDESC_VALUE:
   2864 	case R_FRV_GOTTLSDESC12:
   2865 	case R_FRV_GOTTLSDESCHI:
   2866 	case R_FRV_GOTTLSDESCLO:
   2867 	case R_FRV_TLSMOFF12:
   2868 	case R_FRV_TLSMOFFHI:
   2869 	case R_FRV_TLSMOFFLO:
   2870 	case R_FRV_GOTTLSOFF12:
   2871 	case R_FRV_GOTTLSOFFHI:
   2872 	case R_FRV_GOTTLSOFFLO:
   2873 	case R_FRV_TLSOFF:
   2874 	case R_FRV_TLSDESC_RELAX:
   2875 	case R_FRV_GETTLSOFF_RELAX:
   2876 	case R_FRV_TLSOFF_RELAX:
   2877 	case R_FRV_TLSMOFF:
   2878 	  if (sec && (bfd_is_abs_section (sec) || bfd_is_und_section (sec)))
   2879 	    relocation += tls_biased_base (info);
   2880 	  break;
   2881 
   2882 	default:
   2883 	  break;
   2884 	}
   2885 
   2886       /* Try to apply TLS relaxations.  */
   2887       if (1)
   2888 	switch (r_type)
   2889 	  {
   2890 
   2891 #define LOCAL_EXEC_P(info, picrel) \
   2892   (bfd_link_executable (info) \
   2893    && (picrel->symndx != -1 || FRVFDPIC_SYM_LOCAL ((info), (picrel)->d.h)))
   2894 #define INITIAL_EXEC_P(info, picrel) \
   2895   ((bfd_link_executable (info)|| (info)->flags & DF_STATIC_TLS) \
   2896    && (picrel)->tlsoff_entry)
   2897 
   2898 #define IN_RANGE_FOR_OFST12_P(value) \
   2899   ((bfd_vma)((value) + 2048) < (bfd_vma)4096)
   2900 #define IN_RANGE_FOR_SETLOS_P(value) \
   2901   ((bfd_vma)((value) + 32768) < (bfd_vma)65536)
   2902 #define TLSMOFF_IN_RANGE_FOR_SETLOS_P(value, info) \
   2903   (IN_RANGE_FOR_SETLOS_P ((value) - tls_biased_base (info)))
   2904 
   2905 #define RELAX_GETTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
   2906   (LOCAL_EXEC_P ((info), (picrel)) \
   2907    && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
   2908 #define RELAX_GETTLSOFF_INITIAL_EXEC_P(info, picrel) \
   2909   (INITIAL_EXEC_P ((info), (picrel)) \
   2910    && IN_RANGE_FOR_OFST12_P ((picrel)->tlsoff_entry))
   2911 
   2912 #define RELAX_TLSDESC_LOCAL_EXEC_P(info, picrel, value) \
   2913   (LOCAL_EXEC_P ((info), (picrel)))
   2914 #define RELAX_TLSDESC_INITIAL_EXEC_P(info, picrel) \
   2915   (INITIAL_EXEC_P ((info), (picrel)))
   2916 
   2917 #define RELAX_GOTTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
   2918   (LOCAL_EXEC_P ((info), (picrel)) \
   2919    && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
   2920 
   2921 	  case R_FRV_GETTLSOFF:
   2922 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   2923 
   2924 	    /* Is this a call instruction?  */
   2925 	    if ((insn & (unsigned long)0x01fc0000) != 0x003c0000)
   2926 	      {
   2927 		info->callbacks->einfo
   2928 		  (_("%H: R_FRV_GETTLSOFF not applied to a call instruction\n"),
   2929 		   input_bfd, input_section, rel->r_offset);
   2930 		return false;
   2931 	      }
   2932 
   2933 	    if (RELAX_GETTLSOFF_LOCAL_EXEC_P (info, picrel,
   2934 					      relocation + rel->r_addend))
   2935 	      {
   2936 		/* Replace the call instruction (except the packing bit)
   2937 		   with setlos #tlsmofflo(symbol+offset), gr9.  */
   2938 		insn &= (unsigned long)0x80000000;
   2939 		insn |= (unsigned long)0x12fc0000;
   2940 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   2941 
   2942 		r_type = R_FRV_TLSMOFFLO;
   2943 		howto  = elf32_frv_howto_table + r_type;
   2944 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   2945 	      }
   2946 
   2947 	    else if (RELAX_GETTLSOFF_INITIAL_EXEC_P (info, picrel))
   2948 	      {
   2949 		/* Replace the call instruction (except the packing bit)
   2950 		   with ldi @(gr15, #gottlsoff12(symbol+addend)), gr9.  */
   2951 		insn &= (unsigned long)0x80000000;
   2952 		insn |= (unsigned long)0x12c8f000;
   2953 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   2954 
   2955 		r_type = R_FRV_GOTTLSOFF12;
   2956 		howto  = elf32_frv_howto_table + r_type;
   2957 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   2958 	      }
   2959 
   2960 	    break;
   2961 
   2962 	  case R_FRV_GOTTLSDESC12:
   2963 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   2964 
   2965 	    /* Is this an lddi instruction?  */
   2966 	    if ((insn & (unsigned long)0x01fc0000) != 0x00cc0000)
   2967 	      {
   2968 		info->callbacks->einfo
   2969 		  (_("%H: R_FRV_GOTTLSDESC12"
   2970 		     " not applied to an lddi instruction\n"),
   2971 		   input_bfd, input_section, rel->r_offset);
   2972 		return false;
   2973 	      }
   2974 
   2975 	    if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
   2976 					    relocation + rel->r_addend)
   2977 		&& TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
   2978 						  info))
   2979 	      {
   2980 		/* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
   2981 		   with setlos #tlsmofflo(symbol+offset), gr<C+1>.
   2982 		   Preserve the packing bit.  */
   2983 		insn = (insn & (unsigned long)0x80000000)
   2984 		  | ((insn + (unsigned long)0x02000000)
   2985 		     & (unsigned long)0x7e000000);
   2986 		insn |= (unsigned long)0x00fc0000;
   2987 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   2988 
   2989 		r_type = R_FRV_TLSMOFFLO;
   2990 		howto  = elf32_frv_howto_table + r_type;
   2991 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   2992 	      }
   2993 
   2994 	    else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
   2995 						 relocation + rel->r_addend))
   2996 	      {
   2997 		/* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
   2998 		   with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
   2999 		   Preserve the packing bit.  */
   3000 		insn = (insn & (unsigned long)0x80000000)
   3001 		  | ((insn + (unsigned long)0x02000000)
   3002 		     & (unsigned long)0x7e000000);
   3003 		insn |= (unsigned long)0x00f80000;
   3004 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3005 
   3006 		r_type = R_FRV_TLSMOFFHI;
   3007 		howto  = elf32_frv_howto_table + r_type;
   3008 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3009 	      }
   3010 
   3011 	    else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
   3012 	      {
   3013 		/* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
   3014 		   with ldi @(grB, #gottlsoff12(symbol+offset),
   3015 		   gr<C+1>.  Preserve the packing bit.  If gottlsoff12
   3016 		   overflows, we'll error out, but that's sort-of ok,
   3017 		   since we'd started with gottlsdesc12, that's actually
   3018 		   more demanding.  Compiling with -fPIE instead of
   3019 		   -fpie would fix it; linking with --relax should fix
   3020 		   it as well.  */
   3021 		insn = (insn & (unsigned long)0x80cbf000)
   3022 		  | ((insn + (unsigned long)0x02000000)
   3023 		     & (unsigned long)0x7e000000);
   3024 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3025 
   3026 		r_type = R_FRV_GOTTLSOFF12;
   3027 		howto  = elf32_frv_howto_table + r_type;
   3028 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3029 	      }
   3030 
   3031 	    break;
   3032 
   3033 	  case R_FRV_GOTTLSDESCHI:
   3034 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3035 
   3036 	    /* Is this a sethi instruction?  */
   3037 	    if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
   3038 	      {
   3039 		info->callbacks->einfo
   3040 		  (_("%H: R_FRV_GOTTLSDESCHI"
   3041 		     " not applied to a sethi instruction\n"),
   3042 		   input_bfd, input_section, rel->r_offset);
   3043 		return false;
   3044 	      }
   3045 
   3046 	    if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
   3047 					    relocation + rel->r_addend)
   3048 		|| (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
   3049 		    && IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry)))
   3050 	      {
   3051 		/* Replace sethi with a nop.  Preserve the packing bit.  */
   3052 		insn &= (unsigned long)0x80000000;
   3053 		insn |= (unsigned long)0x00880000;
   3054 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3055 
   3056 		/* Nothing to relocate.  */
   3057 		continue;
   3058 	      }
   3059 
   3060 	    else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
   3061 	      {
   3062 		/* Simply decay GOTTLSDESC to GOTTLSOFF.  */
   3063 		r_type = R_FRV_GOTTLSOFFHI;
   3064 		howto  = elf32_frv_howto_table + r_type;
   3065 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3066 	      }
   3067 
   3068 	    break;
   3069 
   3070 	  case R_FRV_GOTTLSDESCLO:
   3071 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3072 
   3073 	    /* Is this a setlo or setlos instruction?  */
   3074 	    if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
   3075 	      {
   3076 		info->callbacks->einfo
   3077 		  (_("%H: R_FRV_GOTTLSDESCLO"
   3078 		     " not applied to a setlo or setlos instruction\n"),
   3079 		   input_bfd, input_section, rel->r_offset);
   3080 		return false;
   3081 	      }
   3082 
   3083 	    if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
   3084 					    relocation + rel->r_addend)
   3085 		|| (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
   3086 		    && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
   3087 	      {
   3088 		/* Replace setlo/setlos with a nop.  Preserve the
   3089 		   packing bit.  */
   3090 		insn &= (unsigned long)0x80000000;
   3091 		insn |= (unsigned long)0x00880000;
   3092 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3093 
   3094 		/* Nothing to relocate.  */
   3095 		continue;
   3096 	      }
   3097 
   3098 	    else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
   3099 	      {
   3100 		/* If the corresponding sethi (if it exists) decayed
   3101 		   to a nop, make sure this becomes (or already is) a
   3102 		   setlos, not setlo.  */
   3103 		if (IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry))
   3104 		  {
   3105 		    insn |= (unsigned long)0x00080000;
   3106 		    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3107 		  }
   3108 
   3109 		/* Simply decay GOTTLSDESC to GOTTLSOFF.  */
   3110 		r_type = R_FRV_GOTTLSOFFLO;
   3111 		howto  = elf32_frv_howto_table + r_type;
   3112 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3113 	      }
   3114 
   3115 	    break;
   3116 
   3117 	  case R_FRV_TLSDESC_RELAX:
   3118 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3119 
   3120 	    /* Is this an ldd instruction?  */
   3121 	    if ((insn & (unsigned long)0x01fc0fc0) != 0x00080140)
   3122 	      {
   3123 		info->callbacks->einfo
   3124 		  (_("%H: R_FRV_TLSDESC_RELAX"
   3125 		     " not applied to an ldd instruction\n"),
   3126 		   input_bfd, input_section, rel->r_offset);
   3127 		return false;
   3128 	      }
   3129 
   3130 	    if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
   3131 					    relocation + rel->r_addend)
   3132 		&& TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
   3133 						  info))
   3134 	      {
   3135 		/* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
   3136 		   with setlos #tlsmofflo(symbol+offset), gr<C+1>.
   3137 		   Preserve the packing bit.  */
   3138 		insn = (insn & (unsigned long)0x80000000)
   3139 		  | ((insn + (unsigned long)0x02000000)
   3140 		     & (unsigned long)0x7e000000);
   3141 		insn |= (unsigned long)0x00fc0000;
   3142 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3143 
   3144 		r_type = R_FRV_TLSMOFFLO;
   3145 		howto  = elf32_frv_howto_table + r_type;
   3146 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3147 	      }
   3148 
   3149 	    else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
   3150 						 relocation + rel->r_addend))
   3151 	      {
   3152 		/* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
   3153 		   with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
   3154 		   Preserve the packing bit.  */
   3155 		insn = (insn & (unsigned long)0x80000000)
   3156 		  | ((insn + (unsigned long)0x02000000)
   3157 		     & (unsigned long)0x7e000000);
   3158 		insn |= (unsigned long)0x00f80000;
   3159 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3160 
   3161 		r_type = R_FRV_TLSMOFFHI;
   3162 		howto  = elf32_frv_howto_table + r_type;
   3163 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3164 	      }
   3165 
   3166 	    else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
   3167 		     && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
   3168 	      {
   3169 		/* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
   3170 		   with ldi @(grB, #gottlsoff12(symbol+offset), gr<C+1>.
   3171 		   Preserve the packing bit.  */
   3172 		insn = (insn & (unsigned long)0x8003f000)
   3173 		  | (unsigned long)0x00c80000
   3174 		  | ((insn + (unsigned long)0x02000000)
   3175 		     & (unsigned long)0x7e000000);
   3176 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3177 
   3178 		r_type = R_FRV_GOTTLSOFF12;
   3179 		howto  = elf32_frv_howto_table + r_type;
   3180 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3181 	      }
   3182 
   3183 	    else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
   3184 	      {
   3185 		/* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
   3186 		   with ld #tlsoff(symbol+offset)@(grB, grA), gr<C+1>.
   3187 		   Preserve the packing bit.  */
   3188 		insn = (insn & (unsigned long)0x81ffffbf)
   3189 		  | ((insn + (unsigned long)0x02000000)
   3190 		     & (unsigned long)0x7e000000);
   3191 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3192 
   3193 		/* #tlsoff(symbol+offset) is just a relaxation
   3194 		    annotation, so there's nothing left to
   3195 		    relocate.  */
   3196 		continue;
   3197 	      }
   3198 
   3199 	    break;
   3200 
   3201 	  case R_FRV_GETTLSOFF_RELAX:
   3202 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3203 
   3204 	    /* Is this a calll or callil instruction?  */
   3205 	    if ((insn & (unsigned long)0x7ff80fc0) != 0x02300000)
   3206 	      {
   3207 		info->callbacks->einfo
   3208 		  (_("%H: R_FRV_GETTLSOFF_RELAX"
   3209 		     " not applied to a calll instruction\n"),
   3210 		   input_bfd, input_section, rel->r_offset);
   3211 		return false;
   3212 	      }
   3213 
   3214 	    if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
   3215 					    relocation + rel->r_addend)
   3216 		&& TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
   3217 						  info))
   3218 	      {
   3219 		/* Replace calll with a nop.  Preserve the packing bit.  */
   3220 		insn &= (unsigned long)0x80000000;
   3221 		insn |= (unsigned long)0x00880000;
   3222 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3223 
   3224 		/* Nothing to relocate.  */
   3225 		continue;
   3226 	      }
   3227 
   3228 	    else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
   3229 						 relocation + rel->r_addend))
   3230 	      {
   3231 		/* Replace calll with setlo #tlsmofflo(symbol+offset), gr9.
   3232 		   Preserve the packing bit.  */
   3233 		insn &= (unsigned long)0x80000000;
   3234 		insn |= (unsigned long)0x12f40000;
   3235 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3236 
   3237 		r_type = R_FRV_TLSMOFFLO;
   3238 		howto  = elf32_frv_howto_table + r_type;
   3239 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3240 	      }
   3241 
   3242 	    else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
   3243 	      {
   3244 		/* Replace calll with a nop.  Preserve the packing bit.  */
   3245 		insn &= (unsigned long)0x80000000;
   3246 		insn |= (unsigned long)0x00880000;
   3247 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3248 
   3249 		/* Nothing to relocate.  */
   3250 		continue;
   3251 	      }
   3252 
   3253 	    break;
   3254 
   3255 	  case R_FRV_GOTTLSOFF12:
   3256 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3257 
   3258 	    /* Is this an ldi instruction?  */
   3259 	    if ((insn & (unsigned long)0x01fc0000) != 0x00c80000)
   3260 	      {
   3261 		info->callbacks->einfo
   3262 		  (_("%H: R_FRV_GOTTLSOFF12"
   3263 		     " not applied to an ldi instruction\n"),
   3264 		   input_bfd, input_section, rel->r_offset);
   3265 		return false;
   3266 	      }
   3267 
   3268 	    if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
   3269 					      relocation + rel->r_addend))
   3270 	      {
   3271 		/* Replace ldi @(grB, #gottlsoff12(symbol+offset), grC
   3272 		   with setlos #tlsmofflo(symbol+offset), grC.
   3273 		   Preserve the packing bit.  */
   3274 		insn &= (unsigned long)0xfe000000;
   3275 		insn |= (unsigned long)0x00fc0000;
   3276 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3277 
   3278 		r_type = R_FRV_TLSMOFFLO;
   3279 		howto  = elf32_frv_howto_table + r_type;
   3280 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3281 	      }
   3282 
   3283 	    break;
   3284 
   3285 	  case R_FRV_GOTTLSOFFHI:
   3286 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3287 
   3288 	    /* Is this a sethi instruction?  */
   3289 	    if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
   3290 	      {
   3291 		info->callbacks->einfo
   3292 		  (_("%H: R_FRV_GOTTLSOFFHI"
   3293 		     " not applied to a sethi instruction\n"),
   3294 		   input_bfd, input_section, rel->r_offset);
   3295 		return false;
   3296 	      }
   3297 
   3298 	    if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
   3299 					      relocation + rel->r_addend)
   3300 		|| (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
   3301 		    && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
   3302 	      {
   3303 		/* Replace sethi with a nop.  Preserve the packing bit.  */
   3304 		insn &= (unsigned long)0x80000000;
   3305 		insn |= (unsigned long)0x00880000;
   3306 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3307 
   3308 		/* Nothing to relocate.  */
   3309 		continue;
   3310 	      }
   3311 
   3312 	    break;
   3313 
   3314 	  case R_FRV_GOTTLSOFFLO:
   3315 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3316 
   3317 	    /* Is this a setlo or setlos instruction?  */
   3318 	    if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
   3319 	      {
   3320 		info->callbacks->einfo
   3321 		  (_("%H: R_FRV_GOTTLSOFFLO"
   3322 		     " not applied to a setlo or setlos instruction\n"),
   3323 		   input_bfd, input_section, rel->r_offset);
   3324 		return false;
   3325 	      }
   3326 
   3327 	    if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
   3328 					      relocation + rel->r_addend)
   3329 		|| (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
   3330 		    && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
   3331 	      {
   3332 		/* Replace setlo/setlos with a nop.  Preserve the
   3333 		   packing bit.  */
   3334 		insn &= (unsigned long)0x80000000;
   3335 		insn |= (unsigned long)0x00880000;
   3336 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3337 
   3338 		/* Nothing to relocate.  */
   3339 		continue;
   3340 	      }
   3341 
   3342 	    break;
   3343 
   3344 	  case R_FRV_TLSOFF_RELAX:
   3345 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3346 
   3347 	    /* Is this an ld instruction?  */
   3348 	    if ((insn & (unsigned long)0x01fc0fc0) != 0x00080100)
   3349 	      {
   3350 		info->callbacks->einfo
   3351 		  (_("%H: R_FRV_TLSOFF_RELAX"
   3352 		     " not applied to an ld instruction\n"),
   3353 		   input_bfd, input_section, rel->r_offset);
   3354 		return false;
   3355 	      }
   3356 
   3357 	    if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
   3358 					      relocation + rel->r_addend))
   3359 	      {
   3360 		/* Replace ld #gottlsoff(symbol+offset)@(grB, grA), grC
   3361 		   with setlos #tlsmofflo(symbol+offset), grC.
   3362 		   Preserve the packing bit.  */
   3363 		insn &= (unsigned long)0xfe000000;
   3364 		insn |= (unsigned long)0x00fc0000;
   3365 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3366 
   3367 		r_type = R_FRV_TLSMOFFLO;
   3368 		howto  = elf32_frv_howto_table + r_type;
   3369 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3370 	      }
   3371 
   3372 	    else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
   3373 		     && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
   3374 	      {
   3375 		/* Replace ld #tlsoff(symbol+offset)@(grB, grA), grC
   3376 		   with ldi @(grB, #gottlsoff12(symbol+offset), grC.
   3377 		   Preserve the packing bit.  */
   3378 		insn = (insn & (unsigned long)0xfe03f000)
   3379 		  | (unsigned long)0x00c80000;
   3380 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3381 
   3382 		r_type = R_FRV_GOTTLSOFF12;
   3383 		howto  = elf32_frv_howto_table + r_type;
   3384 		rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   3385 	      }
   3386 
   3387 	    break;
   3388 
   3389 	  case R_FRV_TLSMOFFHI:
   3390 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3391 
   3392 	    /* Is this a sethi instruction?  */
   3393 	    if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
   3394 	      {
   3395 		info->callbacks->einfo
   3396 		  (_("%H: R_FRV_TLSMOFFHI"
   3397 		     " not applied to a sethi instruction\n"),
   3398 		   input_bfd, input_section, rel->r_offset);
   3399 		return false;
   3400 	      }
   3401 
   3402 	    if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
   3403 					       info))
   3404 	      {
   3405 		/* Replace sethi with a nop.  Preserve the packing bit.  */
   3406 		insn &= (unsigned long)0x80000000;
   3407 		insn |= (unsigned long)0x00880000;
   3408 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3409 
   3410 		/* Nothing to relocate.  */
   3411 		continue;
   3412 	      }
   3413 
   3414 	    break;
   3415 
   3416 	  case R_FRV_TLSMOFFLO:
   3417 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   3418 
   3419 	    /* Is this a setlo or setlos instruction?  */
   3420 	    if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
   3421 	      {
   3422 		info->callbacks->einfo
   3423 		  (_("R_FRV_TLSMOFFLO"
   3424 		     " not applied to a setlo or setlos instruction\n"),
   3425 		   input_bfd, input_section, rel->r_offset);
   3426 		return false;
   3427 	      }
   3428 
   3429 	    if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
   3430 					       info))
   3431 	      /* If the corresponding sethi (if it exists) decayed
   3432 		 to a nop, make sure this becomes (or already is) a
   3433 		 setlos, not setlo.  */
   3434 	      {
   3435 		insn |= (unsigned long)0x00080000;
   3436 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   3437 	      }
   3438 
   3439 	    break;
   3440 
   3441 	    /*
   3442 	      There's nothing to relax in these:
   3443 		R_FRV_TLSDESC_VALUE
   3444 		R_FRV_TLSOFF
   3445 		R_FRV_TLSMOFF12
   3446 		R_FRV_TLSMOFFHI
   3447 		R_FRV_TLSMOFFLO
   3448 		R_FRV_TLSMOFF
   3449 	    */
   3450 
   3451 	  default:
   3452 	    break;
   3453 	  }
   3454 
   3455       switch (r_type)
   3456 	{
   3457 	case R_FRV_LABEL24:
   3458 	  check_segment[0] = isec_segment;
   3459 	  if (! IS_FDPIC (output_bfd))
   3460 	    check_segment[1] = isec_segment;
   3461 	  else if (picrel->plt)
   3462 	    {
   3463 	      relocation = frvfdpic_plt_section (info)->output_section->vma
   3464 		+ frvfdpic_plt_section (info)->output_offset
   3465 		+ picrel->plt_entry;
   3466 	      check_segment[1] = plt_segment;
   3467 	    }
   3468 	  /* We don't want to warn on calls to undefined weak symbols,
   3469 	     as calls to them must be protected by non-NULL tests
   3470 	     anyway, and unprotected calls would invoke undefined
   3471 	     behavior.  */
   3472 	  else if (picrel->symndx == -1
   3473 		   && picrel->d.h->root.type == bfd_link_hash_undefweak)
   3474 	    check_segment[1] = check_segment[0];
   3475 	  else
   3476 	    check_segment[1] = sec
   3477 	      ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
   3478 	      : (unsigned)-1;
   3479 	  break;
   3480 
   3481 	case R_FRV_GOT12:
   3482 	case R_FRV_GOTHI:
   3483 	case R_FRV_GOTLO:
   3484 	  relocation = picrel->got_entry;
   3485 	  check_segment[0] = check_segment[1] = got_segment;
   3486 	  break;
   3487 
   3488 	case R_FRV_FUNCDESC_GOT12:
   3489 	case R_FRV_FUNCDESC_GOTHI:
   3490 	case R_FRV_FUNCDESC_GOTLO:
   3491 	  relocation = picrel->fdgot_entry;
   3492 	  check_segment[0] = check_segment[1] = got_segment;
   3493 	  break;
   3494 
   3495 	case R_FRV_GOTOFFHI:
   3496 	case R_FRV_GOTOFF12:
   3497 	case R_FRV_GOTOFFLO:
   3498 	  relocation -= frvfdpic_got_section (info)->output_section->vma
   3499 	    + frvfdpic_got_section (info)->output_offset
   3500 	    + frvfdpic_got_initial_offset (info);
   3501 	  check_segment[0] = got_segment;
   3502 	  check_segment[1] = sec
   3503 	    ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
   3504 	    : (unsigned)-1;
   3505 	  break;
   3506 
   3507 	case R_FRV_FUNCDESC_GOTOFF12:
   3508 	case R_FRV_FUNCDESC_GOTOFFHI:
   3509 	case R_FRV_FUNCDESC_GOTOFFLO:
   3510 	  relocation = picrel->fd_entry;
   3511 	  check_segment[0] = check_segment[1] = got_segment;
   3512 	  break;
   3513 
   3514 	case R_FRV_FUNCDESC:
   3515 	  {
   3516 	    int dynindx;
   3517 	    bfd_vma addend = rel->r_addend;
   3518 
   3519 	    if (! (h && h->root.type == bfd_link_hash_undefweak
   3520 		   && FRVFDPIC_SYM_LOCAL (info, h)))
   3521 	      {
   3522 		/* If the symbol is dynamic and there may be dynamic
   3523 		   symbol resolution because we are or are linked with a
   3524 		   shared library, emit a FUNCDESC relocation such that
   3525 		   the dynamic linker will allocate the function
   3526 		   descriptor.  If the symbol needs a non-local function
   3527 		   descriptor but binds locally (e.g., its visibility is
   3528 		   protected, emit a dynamic relocation decayed to
   3529 		   section+offset.  */
   3530 		if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h)
   3531 		    && FRVFDPIC_SYM_LOCAL (info, h)
   3532 		    && !bfd_link_pde (info))
   3533 		  {
   3534 		    dynindx = elf_section_data (h->root.u.def.section
   3535 						->output_section)->dynindx;
   3536 		    addend += h->root.u.def.section->output_offset
   3537 		      + h->root.u.def.value;
   3538 		  }
   3539 		else if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h))
   3540 		  {
   3541 		    if (addend)
   3542 		      {
   3543 			info->callbacks->einfo
   3544 			  (_("%H: %s references dynamic symbol"
   3545 			     " with nonzero addend\n"),
   3546 			   input_bfd, input_section, rel->r_offset,
   3547 			   "R_FRV_FUNCDESC");
   3548 			return false;
   3549 		      }
   3550 		    dynindx = h->dynindx;
   3551 		  }
   3552 		else
   3553 		  {
   3554 		    /* Otherwise, we know we have a private function
   3555 		       descriptor, so reference it directly.  */
   3556 		    BFD_ASSERT (picrel->privfd);
   3557 		    r_type = R_FRV_32;
   3558 		    dynindx = elf_section_data (frvfdpic_got_section (info)
   3559 						->output_section)->dynindx;
   3560 		    addend = frvfdpic_got_section (info)->output_offset
   3561 		      + frvfdpic_got_initial_offset (info)
   3562 		      + picrel->fd_entry;
   3563 		  }
   3564 
   3565 		/* If there is room for dynamic symbol resolution, emit
   3566 		   the dynamic relocation.  However, if we're linking an
   3567 		   executable at a fixed location, we won't have emitted a
   3568 		   dynamic symbol entry for the got section, so idx will
   3569 		   be zero, which means we can and should compute the
   3570 		   address of the private descriptor ourselves.  */
   3571 		if (bfd_link_pde (info)
   3572 		    && (!h || FRVFDPIC_FUNCDESC_LOCAL (info, h)))
   3573 		  {
   3574 		    addend += frvfdpic_got_section (info)->output_section->vma;
   3575 		    if ((bfd_section_flags (input_section->output_section)
   3576 			 & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
   3577 		      {
   3578 			bfd_vma offset;
   3579 
   3580 			if (_frvfdpic_osec_readonly_p (output_bfd,
   3581 						       input_section
   3582 						       ->output_section))
   3583 			  {
   3584 			    info->callbacks->einfo
   3585 			      (_("%H: cannot emit fixups"
   3586 				 " in read-only section\n"),
   3587 			       input_bfd, input_section, rel->r_offset);
   3588 			    return false;
   3589 			  }
   3590 
   3591 			offset = _bfd_elf_section_offset
   3592 			  (output_bfd, info,
   3593 			   input_section, rel->r_offset);
   3594 
   3595 			if (offset != (bfd_vma)-1)
   3596 			  _frvfdpic_add_rofixup (output_bfd,
   3597 						 frvfdpic_gotfixup_section
   3598 						 (info),
   3599 						 offset + input_section
   3600 						 ->output_section->vma
   3601 						 + input_section->output_offset,
   3602 						 picrel);
   3603 		      }
   3604 		  }
   3605 		else if ((bfd_section_flags (input_section->output_section)
   3606 			  & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
   3607 		  {
   3608 		    bfd_vma offset;
   3609 
   3610 		    if (_frvfdpic_osec_readonly_p (output_bfd,
   3611 						   input_section
   3612 						   ->output_section))
   3613 		      {
   3614 			info->callbacks->einfo
   3615 			  (_("%H: cannot emit dynamic relocations"
   3616 			     " in read-only section\n"),
   3617 			   input_bfd, input_section, rel->r_offset);
   3618 			return false;
   3619 		      }
   3620 
   3621 		    offset = _bfd_elf_section_offset
   3622 		      (output_bfd, info,
   3623 		       input_section, rel->r_offset);
   3624 
   3625 		    if (offset != (bfd_vma)-1)
   3626 		      _frvfdpic_add_dyn_reloc (output_bfd,
   3627 					       frvfdpic_gotrel_section (info),
   3628 					       offset + input_section
   3629 					       ->output_section->vma
   3630 					       + input_section->output_offset,
   3631 					       r_type, dynindx, addend, picrel);
   3632 		  }
   3633 		else
   3634 		  addend += frvfdpic_got_section (info)->output_section->vma;
   3635 	      }
   3636 
   3637 	    /* We want the addend in-place because dynamic
   3638 	       relocations are REL.  Setting relocation to it should
   3639 	       arrange for it to be installed.  */
   3640 	    relocation = addend - rel->r_addend;
   3641 	  }
   3642 	  check_segment[0] = check_segment[1] = got_segment;
   3643 	  break;
   3644 
   3645 	case R_FRV_32:
   3646 	  if (! IS_FDPIC (output_bfd))
   3647 	    {
   3648 	      check_segment[0] = check_segment[1] = -1;
   3649 	      break;
   3650 	    }
   3651 	  /* Fall through.  */
   3652 	case R_FRV_FUNCDESC_VALUE:
   3653 	  {
   3654 	    int dynindx;
   3655 	    bfd_vma addend = rel->r_addend;
   3656 
   3657 	    /* If the symbol is dynamic but binds locally, use
   3658 	       section+offset.  */
   3659 	    if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
   3660 	      {
   3661 		if (addend && r_type == R_FRV_FUNCDESC_VALUE)
   3662 		  {
   3663 		    info->callbacks->einfo
   3664 		      (_("%H: %s references dynamic symbol"
   3665 			 " with nonzero addend\n"),
   3666 		       input_bfd, input_section, rel->r_offset,
   3667 		       "R_FRV_FUNCDESC_VALUE");
   3668 		    return false;
   3669 		  }
   3670 		dynindx = h->dynindx;
   3671 	      }
   3672 	    else
   3673 	      {
   3674 		if (h)
   3675 		  addend += h->root.u.def.value;
   3676 		else
   3677 		  addend += sym->st_value;
   3678 		if (osec)
   3679 		  addend += osec->output_offset;
   3680 		if (osec && osec->output_section
   3681 		    && ! bfd_is_abs_section (osec->output_section)
   3682 		    && ! bfd_is_und_section (osec->output_section))
   3683 		  dynindx = elf_section_data (osec->output_section)->dynindx;
   3684 		else
   3685 		  dynindx = 0;
   3686 	      }
   3687 
   3688 	    /* If we're linking an executable at a fixed address, we
   3689 	       can omit the dynamic relocation as long as the symbol
   3690 	       is defined in the current link unit (which is implied
   3691 	       by its output section not being NULL).  */
   3692 	    if (bfd_link_pde (info)
   3693 		&& (!h || FRVFDPIC_SYM_LOCAL (info, h)))
   3694 	      {
   3695 		if (osec)
   3696 		  addend += osec->output_section->vma;
   3697 		if (IS_FDPIC (input_bfd)
   3698 		    && (bfd_section_flags (input_section->output_section)
   3699 			& (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
   3700 		  {
   3701 		    if (_frvfdpic_osec_readonly_p (output_bfd,
   3702 						   input_section
   3703 						   ->output_section))
   3704 		      {
   3705 			info->callbacks->einfo
   3706 			  (_("%H: cannot emit fixups in read-only section\n"),
   3707 			   input_bfd, input_section, rel->r_offset);
   3708 			return false;
   3709 		      }
   3710 		    if (!h || h->root.type != bfd_link_hash_undefweak)
   3711 		      {
   3712 			bfd_vma offset = _bfd_elf_section_offset
   3713 			  (output_bfd, info,
   3714 			   input_section, rel->r_offset);
   3715 
   3716 			if (offset != (bfd_vma)-1)
   3717 			  {
   3718 			    _frvfdpic_add_rofixup (output_bfd,
   3719 						   frvfdpic_gotfixup_section
   3720 						   (info),
   3721 						   offset + input_section
   3722 						   ->output_section->vma
   3723 						   + input_section->output_offset,
   3724 						   picrel);
   3725 			    if (r_type == R_FRV_FUNCDESC_VALUE)
   3726 			      _frvfdpic_add_rofixup
   3727 				(output_bfd,
   3728 				 frvfdpic_gotfixup_section (info),
   3729 				 offset
   3730 				 + input_section->output_section->vma
   3731 				 + input_section->output_offset + 4, picrel);
   3732 			  }
   3733 		      }
   3734 		  }
   3735 	      }
   3736 	    else
   3737 	      {
   3738 		if ((bfd_section_flags (input_section->output_section)
   3739 		     & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
   3740 		  {
   3741 		    bfd_vma offset;
   3742 
   3743 		    if (_frvfdpic_osec_readonly_p (output_bfd,
   3744 						   input_section
   3745 						   ->output_section))
   3746 		      {
   3747 			info->callbacks->einfo
   3748 			  (_("%H: cannot emit dynamic relocations"
   3749 			     " in read-only section\n"),
   3750 			   input_bfd, input_section, rel->r_offset);
   3751 			return false;
   3752 		      }
   3753 
   3754 		    offset = _bfd_elf_section_offset
   3755 		      (output_bfd, info,
   3756 		       input_section, rel->r_offset);
   3757 
   3758 		    if (offset != (bfd_vma)-1)
   3759 		      _frvfdpic_add_dyn_reloc (output_bfd,
   3760 					       frvfdpic_gotrel_section (info),
   3761 					       offset + input_section
   3762 					       ->output_section->vma
   3763 					       + input_section->output_offset,
   3764 					       r_type, dynindx, addend, picrel);
   3765 		  }
   3766 		else if (osec)
   3767 		  addend += osec->output_section->vma;
   3768 		/* We want the addend in-place because dynamic
   3769 		   relocations are REL.  Setting relocation to it
   3770 		   should arrange for it to be installed.  */
   3771 		relocation = addend - rel->r_addend;
   3772 	      }
   3773 
   3774 	    if (r_type == R_FRV_FUNCDESC_VALUE)
   3775 	      {
   3776 		/* If we've omitted the dynamic relocation, just emit
   3777 		   the fixed addresses of the symbol and of the local
   3778 		   GOT base offset.  */
   3779 		if (bfd_link_pde (info)
   3780 		    && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
   3781 		  bfd_put_32 (output_bfd,
   3782 			      frvfdpic_got_section (info)->output_section->vma
   3783 			      + frvfdpic_got_section (info)->output_offset
   3784 			      + frvfdpic_got_initial_offset (info),
   3785 			      contents + rel->r_offset + 4);
   3786 		else
   3787 		  /* A function descriptor used for lazy or local
   3788 		     resolving is initialized such that its high word
   3789 		     contains the output section index in which the
   3790 		     PLT entries are located, and the low word
   3791 		     contains the offset of the lazy PLT entry entry
   3792 		     point into that section.  */
   3793 		  bfd_put_32 (output_bfd,
   3794 			      h && ! FRVFDPIC_SYM_LOCAL (info, h)
   3795 			      ? 0
   3796 			      : _frvfdpic_osec_to_segment (output_bfd,
   3797 							   sec
   3798 							   ->output_section),
   3799 			      contents + rel->r_offset + 4);
   3800 	      }
   3801 	  }
   3802 	  check_segment[0] = check_segment[1] = got_segment;
   3803 	  break;
   3804 
   3805 	case R_FRV_GPREL12:
   3806 	case R_FRV_GPRELU12:
   3807 	case R_FRV_GPREL32:
   3808 	case R_FRV_GPRELHI:
   3809 	case R_FRV_GPRELLO:
   3810 	  check_segment[0] = gprel_segment;
   3811 	  check_segment[1] = sec
   3812 	    ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
   3813 	    : (unsigned)-1;
   3814 	  break;
   3815 
   3816 	case R_FRV_GETTLSOFF:
   3817 	  relocation = frvfdpic_plt_section (info)->output_section->vma
   3818 	    + frvfdpic_plt_section (info)->output_offset
   3819 	    + picrel->tlsplt_entry;
   3820 	  BFD_ASSERT (picrel->tlsplt_entry != (bfd_vma)-1
   3821 		      && picrel->tlsdesc_entry);
   3822 	  check_segment[0] = isec_segment;
   3823 	  check_segment[1] = plt_segment;
   3824 	  break;
   3825 
   3826 	case R_FRV_GOTTLSDESC12:
   3827 	case R_FRV_GOTTLSDESCHI:
   3828 	case R_FRV_GOTTLSDESCLO:
   3829 	  BFD_ASSERT (picrel->tlsdesc_entry);
   3830 	  relocation = picrel->tlsdesc_entry;
   3831 	  check_segment[0] = tls_segment;
   3832 	  check_segment[1] = sec
   3833 	    && ! bfd_is_abs_section (sec)
   3834 	    && ! bfd_is_und_section (sec)
   3835 	    ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
   3836 	    : tls_segment;
   3837 	  break;
   3838 
   3839 	case R_FRV_TLSMOFF12:
   3840 	case R_FRV_TLSMOFFHI:
   3841 	case R_FRV_TLSMOFFLO:
   3842 	case R_FRV_TLSMOFF:
   3843 	  check_segment[0] = tls_segment;
   3844 	  if (! sec)
   3845 	    check_segment[1] = -1;
   3846 	  else if (bfd_is_abs_section (sec)
   3847 		   || bfd_is_und_section (sec))
   3848 	    {
   3849 	      relocation = 0;
   3850 	      check_segment[1] = tls_segment;
   3851 	    }
   3852 	  else if (sec->output_section)
   3853 	    {
   3854 	      relocation -= tls_biased_base (info);
   3855 	      check_segment[1] =
   3856 		_frvfdpic_osec_to_segment (output_bfd, sec->output_section);
   3857 	    }
   3858 	  else
   3859 	    check_segment[1] = -1;
   3860 	  break;
   3861 
   3862 	case R_FRV_GOTTLSOFF12:
   3863 	case R_FRV_GOTTLSOFFHI:
   3864 	case R_FRV_GOTTLSOFFLO:
   3865 	  BFD_ASSERT (picrel->tlsoff_entry);
   3866 	  relocation = picrel->tlsoff_entry;
   3867 	  check_segment[0] = tls_segment;
   3868 	  check_segment[1] = sec
   3869 	    && ! bfd_is_abs_section (sec)
   3870 	    && ! bfd_is_und_section (sec)
   3871 	    ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
   3872 	    : tls_segment;
   3873 	  break;
   3874 
   3875 	case R_FRV_TLSDESC_VALUE:
   3876 	case R_FRV_TLSOFF:
   3877 	  /* These shouldn't be present in input object files.  */
   3878 	  check_segment[0] = check_segment[1] = isec_segment;
   3879 	  break;
   3880 
   3881 	case R_FRV_TLSDESC_RELAX:
   3882 	case R_FRV_GETTLSOFF_RELAX:
   3883 	case R_FRV_TLSOFF_RELAX:
   3884 	  /* These are just annotations for relaxation, nothing to do
   3885 	     here.  */
   3886 	  continue;
   3887 
   3888 	default:
   3889 	  check_segment[0] = isec_segment;
   3890 	  check_segment[1] = sec
   3891 	    ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
   3892 	    : (unsigned)-1;
   3893 	  break;
   3894 	}
   3895 
   3896       if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
   3897 	{
   3898 	  /* If you take this out, remove the #error from fdpic-static-6.d
   3899 	     in the ld testsuite.  */
   3900 	  /* This helps catch problems in GCC while we can't do more
   3901 	     than static linking.  The idea is to test whether the
   3902 	     input file basename is crt0.o only once.  */
   3903 	  if (silence_segment_error == 1)
   3904 	    silence_segment_error =
   3905 	      (strlen (bfd_get_filename (input_bfd)) == 6
   3906 	       && filename_cmp (bfd_get_filename (input_bfd), "crt0.o") == 0)
   3907 	      || (strlen (bfd_get_filename (input_bfd)) > 6
   3908 		  && filename_cmp (bfd_get_filename (input_bfd)
   3909 				   + strlen (bfd_get_filename (input_bfd)) - 7,
   3910 			     "/crt0.o") == 0)
   3911 	      ? -1 : 0;
   3912 	  if (!silence_segment_error
   3913 	      /* We don't want duplicate errors for undefined
   3914 		 symbols.  */
   3915 	      && !(picrel && picrel->symndx == -1
   3916 		   && picrel->d.h->root.type == bfd_link_hash_undefined))
   3917 	    {
   3918 	      info->callbacks->einfo
   3919 		/* xgettext:c-format */
   3920 		(_("%H: reloc against `%s' references a different segment\n"),
   3921 		 input_bfd, input_section, rel->r_offset, name);
   3922 	    }
   3923 	  if (!silence_segment_error && bfd_link_pic (info))
   3924 	    return false;
   3925 	  elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
   3926 	}
   3927 
   3928       switch (r_type)
   3929 	{
   3930 	case R_FRV_GOTOFFHI:
   3931 	case R_FRV_TLSMOFFHI:
   3932 	  /* We need the addend to be applied before we shift the
   3933 	     value right.  */
   3934 	  relocation += rel->r_addend;
   3935 	  /* Fall through.  */
   3936 	case R_FRV_GOTHI:
   3937 	case R_FRV_FUNCDESC_GOTHI:
   3938 	case R_FRV_FUNCDESC_GOTOFFHI:
   3939 	case R_FRV_GOTTLSOFFHI:
   3940 	case R_FRV_GOTTLSDESCHI:
   3941 	  relocation >>= 16;
   3942 	  /* Fall through.  */
   3943 
   3944 	case R_FRV_GOTLO:
   3945 	case R_FRV_FUNCDESC_GOTLO:
   3946 	case R_FRV_GOTOFFLO:
   3947 	case R_FRV_FUNCDESC_GOTOFFLO:
   3948 	case R_FRV_GOTTLSOFFLO:
   3949 	case R_FRV_GOTTLSDESCLO:
   3950 	case R_FRV_TLSMOFFLO:
   3951 	  relocation &= 0xffff;
   3952 	  break;
   3953 
   3954 	default:
   3955 	  break;
   3956 	}
   3957 
   3958       switch (r_type)
   3959 	{
   3960 	case R_FRV_LABEL24:
   3961 	  if (! IS_FDPIC (output_bfd) || ! picrel->plt)
   3962 	    break;
   3963 	  /* Fall through.  */
   3964 
   3965 	  /* When referencing a GOT entry, a function descriptor or a
   3966 	     PLT, we don't want the addend to apply to the reference,
   3967 	     but rather to the referenced symbol.  The actual entry
   3968 	     will have already been created taking the addend into
   3969 	     account, so cancel it out here.  */
   3970 	case R_FRV_GOT12:
   3971 	case R_FRV_GOTHI:
   3972 	case R_FRV_GOTLO:
   3973 	case R_FRV_FUNCDESC_GOT12:
   3974 	case R_FRV_FUNCDESC_GOTHI:
   3975 	case R_FRV_FUNCDESC_GOTLO:
   3976 	case R_FRV_FUNCDESC_GOTOFF12:
   3977 	case R_FRV_FUNCDESC_GOTOFFHI:
   3978 	case R_FRV_FUNCDESC_GOTOFFLO:
   3979 	case R_FRV_GETTLSOFF:
   3980 	case R_FRV_GOTTLSDESC12:
   3981 	case R_FRV_GOTTLSDESCHI:
   3982 	case R_FRV_GOTTLSDESCLO:
   3983 	case R_FRV_GOTTLSOFF12:
   3984 	case R_FRV_GOTTLSOFFHI:
   3985 	case R_FRV_GOTTLSOFFLO:
   3986 	  /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
   3987 	     here, since we do want to apply the addend to the others.
   3988 	     Note that we've applied the addend to GOTOFFHI before we
   3989 	     shifted it right.  */
   3990 	case R_FRV_GOTOFFHI:
   3991 	case R_FRV_TLSMOFFHI:
   3992 	  relocation -= rel->r_addend;
   3993 	  break;
   3994 
   3995 	default:
   3996 	  break;
   3997 	}
   3998 
   3999      if (r_type == R_FRV_HI16)
   4000        r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
   4001 
   4002      else if (r_type == R_FRV_LO16)
   4003        r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
   4004 
   4005      else if (r_type == R_FRV_LABEL24 || r_type == R_FRV_GETTLSOFF)
   4006        r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
   4007 				       contents, relocation);
   4008 
   4009      else if (r_type == R_FRV_GPREL12)
   4010        r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
   4011 				       contents, relocation);
   4012 
   4013      else if (r_type == R_FRV_GPRELU12)
   4014        r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
   4015 					contents, relocation);
   4016 
   4017      else if (r_type == R_FRV_GPRELLO)
   4018        r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
   4019 				       contents, relocation);
   4020 
   4021      else if (r_type == R_FRV_GPRELHI)
   4022        r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
   4023 				       contents, relocation);
   4024 
   4025      else if (r_type == R_FRV_TLSOFF
   4026 	      || r_type == R_FRV_TLSDESC_VALUE)
   4027        r = bfd_reloc_notsupported;
   4028 
   4029      else
   4030        r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
   4031 				    rel, relocation);
   4032 
   4033       if (r != bfd_reloc_ok)
   4034 	{
   4035 	  const char * msg = (const char *) NULL;
   4036 
   4037 	  switch (r)
   4038 	    {
   4039 	    case bfd_reloc_overflow:
   4040 	      (*info->callbacks->reloc_overflow)
   4041 		(info, (h ? &h->root : NULL), name, howto->name,
   4042 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
   4043 	      break;
   4044 
   4045 	    case bfd_reloc_undefined:
   4046 	      (*info->callbacks->undefined_symbol)
   4047 		(info, name, input_bfd, input_section, rel->r_offset, true);
   4048 	      break;
   4049 
   4050 	    case bfd_reloc_outofrange:
   4051 	      msg = _("internal error: out of range error");
   4052 	      break;
   4053 
   4054 	    case bfd_reloc_notsupported:
   4055 	      msg = _("internal error: unsupported relocation error");
   4056 	      break;
   4057 
   4058 	    case bfd_reloc_dangerous:
   4059 	      msg = _("internal error: dangerous relocation");
   4060 	      break;
   4061 
   4062 	    default:
   4063 	      msg = _("internal error: unknown error");
   4064 	      break;
   4065 	    }
   4066 
   4067 	  if (msg)
   4068 	    {
   4069 	      info->callbacks->einfo
   4070 		/* xgettext:c-format */
   4071 		(_("%H: reloc against `%s': %s\n"),
   4072 		 input_bfd, input_section, rel->r_offset, name, msg);
   4073 	      return false;
   4074 	    }
   4075 	}
   4076     }
   4077 
   4078   return true;
   4079 }
   4080 
   4081 /* Return the section that should be marked against GC for a given
   4083    relocation.  */
   4084 
   4085 static asection *
   4086 elf32_frv_gc_mark_hook (asection *sec,
   4087 			struct bfd_link_info *info,
   4088 			Elf_Internal_Rela *rel,
   4089 			struct elf_link_hash_entry *h,
   4090 			Elf_Internal_Sym *sym)
   4091 {
   4092   if (h != NULL)
   4093     switch (ELF32_R_TYPE (rel->r_info))
   4094       {
   4095       case R_FRV_GNU_VTINHERIT:
   4096       case R_FRV_GNU_VTENTRY:
   4097 	return NULL;
   4098       }
   4099 
   4100   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   4101 }
   4102 
   4103 /* Hook called by the linker routine which adds symbols from an object
   4105    file.  We use it to put .comm items in .scomm, and not .comm.  */
   4106 
   4107 static bool
   4108 elf32_frv_add_symbol_hook (bfd *abfd,
   4109 			   struct bfd_link_info *info,
   4110 			   Elf_Internal_Sym *sym,
   4111 			   const char **namep ATTRIBUTE_UNUSED,
   4112 			   flagword *flagsp ATTRIBUTE_UNUSED,
   4113 			   asection **secp,
   4114 			   bfd_vma *valp)
   4115 {
   4116   if (sym->st_shndx == SHN_COMMON
   4117       && !bfd_link_relocatable (info)
   4118       && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
   4119     {
   4120       /* Common symbols less than or equal to -G nn bytes are
   4121 	 automatically put into .sbss.  */
   4122 
   4123       asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
   4124 
   4125       if (scomm == NULL)
   4126 	{
   4127 	  scomm = bfd_make_section_with_flags (abfd, ".scommon",
   4128 					       (SEC_ALLOC
   4129 						| SEC_IS_COMMON
   4130 						| SEC_SMALL_DATA
   4131 						| SEC_LINKER_CREATED));
   4132 	  if (scomm == NULL)
   4133 	    return false;
   4134 	}
   4135 
   4136       *secp = scomm;
   4137       *valp = sym->st_size;
   4138     }
   4139 
   4140   return true;
   4141 }
   4142 
   4143 /* We need dynamic symbols for every section, since segments can
   4144    relocate independently.  */
   4145 static bool
   4146 _frvfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
   4147 				    struct bfd_link_info *info
   4148 				    ATTRIBUTE_UNUSED,
   4149 				    asection *p ATTRIBUTE_UNUSED)
   4150 {
   4151   switch (elf_section_data (p)->this_hdr.sh_type)
   4152     {
   4153     case SHT_PROGBITS:
   4154     case SHT_NOBITS:
   4155       /* If sh_type is yet undecided, assume it could be
   4156 	 SHT_PROGBITS/SHT_NOBITS.  */
   4157     case SHT_NULL:
   4158       return false;
   4159 
   4160       /* There shouldn't be section relative relocations
   4161 	 against any other section.  */
   4162     default:
   4163       return true;
   4164     }
   4165 }
   4166 
   4167 /* Create  a .got section, as well as its additional info field.  This
   4168    is almost entirely copied from
   4169    elflink.c:_bfd_elf_create_got_section().  */
   4170 
   4171 static bool
   4172 _frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
   4173 {
   4174   flagword flags, pltflags;
   4175   asection *s;
   4176   struct elf_link_hash_entry *h;
   4177   struct bfd_link_hash_entry *bh;
   4178   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4179   int ptralign;
   4180   int offset;
   4181 
   4182   /* This function may be called more than once.  */
   4183   s = elf_hash_table (info)->sgot;
   4184   if (s != NULL)
   4185     return true;
   4186 
   4187   /* Machine specific: although pointers are 32-bits wide, we want the
   4188      GOT to be aligned to a 64-bit boundary, such that function
   4189      descriptors in it can be accessed with 64-bit loads and
   4190      stores.  */
   4191   ptralign = 3;
   4192 
   4193   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4194 	   | SEC_LINKER_CREATED);
   4195   pltflags = flags;
   4196 
   4197   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   4198   elf_hash_table (info)->sgot = s;
   4199   if (s == NULL
   4200       || !bfd_set_section_alignment (s, ptralign))
   4201     return false;
   4202 
   4203   if (bed->want_got_sym)
   4204     {
   4205       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
   4206 	 (or .got.plt) section.  We don't do this in the linker script
   4207 	 because we don't want to define the symbol if we are not creating
   4208 	 a global offset table.  */
   4209       h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
   4210       elf_hash_table (info)->hgot = h;
   4211       if (h == NULL)
   4212 	return false;
   4213 
   4214       /* Machine-specific: we want the symbol for executables as
   4215 	 well.  */
   4216       if (! bfd_elf_link_record_dynamic_symbol (info, h))
   4217 	return false;
   4218     }
   4219 
   4220   /* The first bit of the global offset table is the header.  */
   4221   s->size += bed->got_header_size;
   4222 
   4223   /* This is the machine-specific part.  Create and initialize section
   4224      data for the got.  */
   4225   if (IS_FDPIC (abfd))
   4226     {
   4227       frvfdpic_relocs_info (info) = htab_try_create (1,
   4228 						     frvfdpic_relocs_info_hash,
   4229 						     frvfdpic_relocs_info_eq,
   4230 						     (htab_del) NULL);
   4231       if (! frvfdpic_relocs_info (info))
   4232 	return false;
   4233 
   4234       s = bfd_make_section_anyway_with_flags (abfd, ".rel.got",
   4235 					      (flags | SEC_READONLY));
   4236       elf_hash_table (info)->srelgot = s;
   4237       if (s == NULL
   4238 	  || !bfd_set_section_alignment (s, 2))
   4239 	return false;
   4240 
   4241       /* Machine-specific.  */
   4242       s = bfd_make_section_anyway_with_flags (abfd, ".rofixup",
   4243 					      (flags | SEC_READONLY));
   4244       if (s == NULL
   4245 	  || !bfd_set_section_alignment (s, 2))
   4246 	return false;
   4247 
   4248       frvfdpic_gotfixup_section (info) = s;
   4249       offset = -2048;
   4250       flags = BSF_GLOBAL;
   4251     }
   4252   else
   4253     {
   4254       offset = 2048;
   4255       flags = BSF_GLOBAL | BSF_WEAK;
   4256     }
   4257 
   4258   /* Define _gp in .rofixup, for FDPIC, or .got otherwise.  If it
   4259      turns out that we're linking with a different linker script, the
   4260      linker script will override it.  */
   4261   bh = NULL;
   4262   if (!(_bfd_generic_link_add_one_symbol
   4263 	(info, abfd, "_gp", flags, s, offset, (const char *) NULL, false,
   4264 	 bed->collect, &bh)))
   4265     return false;
   4266   h = (struct elf_link_hash_entry *) bh;
   4267   h->def_regular = 1;
   4268   h->type = STT_OBJECT;
   4269   /* h->other = STV_HIDDEN; */ /* Should we?  */
   4270 
   4271   /* Machine-specific: we want the symbol for executables as well.  */
   4272   if (IS_FDPIC (abfd) && ! bfd_elf_link_record_dynamic_symbol (info, h))
   4273     return false;
   4274 
   4275   if (!IS_FDPIC (abfd))
   4276     return true;
   4277 
   4278   /* FDPIC supports Thread Local Storage, and this may require a
   4279      procedure linkage table for TLS PLT entries.  */
   4280 
   4281   /* This is mostly copied from
   4282      elflink.c:_bfd_elf_create_dynamic_sections().  */
   4283 
   4284   flags = pltflags;
   4285   pltflags |= SEC_CODE;
   4286   if (bed->plt_not_loaded)
   4287     pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
   4288   if (bed->plt_readonly)
   4289     pltflags |= SEC_READONLY;
   4290 
   4291   s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
   4292   if (s == NULL
   4293       || !bfd_set_section_alignment (s, bed->plt_alignment))
   4294     return false;
   4295   /* FRV-specific: remember it.  */
   4296   frvfdpic_plt_section (info) = s;
   4297 
   4298   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
   4299      .plt section.  */
   4300   if (bed->want_plt_sym)
   4301     {
   4302       h = _bfd_elf_define_linkage_sym (abfd, info, s,
   4303 				       "_PROCEDURE_LINKAGE_TABLE_");
   4304       elf_hash_table (info)->hplt = h;
   4305       if (h == NULL)
   4306 	return false;
   4307     }
   4308 
   4309   /* FRV-specific: we want rel relocations for the plt.  */
   4310   s = bfd_make_section_anyway_with_flags (abfd, ".rel.plt",
   4311 					  flags | SEC_READONLY);
   4312   if (s == NULL
   4313       || !bfd_set_section_alignment (s, bed->s->log_file_align))
   4314     return false;
   4315   /* FRV-specific: remember it.  */
   4316   frvfdpic_pltrel_section (info) = s;
   4317 
   4318   return true;
   4319 }
   4320 
   4321 /* Make sure the got and plt sections exist, and that our pointers in
   4322    the link hash table point to them.  */
   4323 
   4324 static bool
   4325 elf32_frvfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   4326 {
   4327   /* This is mostly copied from
   4328      elflink.c:_bfd_elf_create_dynamic_sections().  */
   4329   flagword flags;
   4330   asection *s;
   4331   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
   4332 
   4333   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4334 	   | SEC_LINKER_CREATED);
   4335 
   4336   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
   4337      .rel[a].bss sections.  */
   4338 
   4339   /* FRV-specific: we want to create the GOT and the PLT in the FRV
   4340      way.  */
   4341   if (! _frv_create_got_section (abfd, info))
   4342     return false;
   4343 
   4344   /* FRV-specific: make sure we created everything we wanted.  */
   4345   BFD_ASSERT (frvfdpic_got_section (info) && frvfdpic_gotrel_section (info)
   4346 	      && frvfdpic_gotfixup_section (info)
   4347 	      && frvfdpic_plt_section (info)
   4348 	      && frvfdpic_pltrel_section (info));
   4349 
   4350   if (bed->want_dynbss)
   4351     {
   4352       /* The .dynbss section is a place to put symbols which are defined
   4353 	 by dynamic objects, are referenced by regular objects, and are
   4354 	 not functions.  We must allocate space for them in the process
   4355 	 image and use a R_*_COPY reloc to tell the dynamic linker to
   4356 	 initialize them at run time.  The linker script puts the .dynbss
   4357 	 section into the .bss section of the final image.  */
   4358       s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
   4359 					      SEC_ALLOC | SEC_LINKER_CREATED);
   4360       if (s == NULL)
   4361 	return false;
   4362 
   4363       /* The .rel[a].bss section holds copy relocs.  This section is not
   4364      normally needed.  We need to create it here, though, so that the
   4365      linker will map it to an output section.  We can't just create it
   4366      only if we need it, because we will not know whether we need it
   4367      until we have seen all the input files, and the first time the
   4368      main linker code calls BFD after examining all the input files
   4369      (size_dynamic_sections) the input sections have already been
   4370      mapped to the output sections.  If the section turns out not to
   4371      be needed, we can discard it later.  We will never need this
   4372      section when generating a shared object, since they do not use
   4373      copy relocs.  */
   4374       if (! bfd_link_pic (info))
   4375 	{
   4376 	  s = bfd_make_section_anyway_with_flags (abfd,
   4377 						  (bed->default_use_rela_p
   4378 						   ? ".rela.bss" : ".rel.bss"),
   4379 						  flags | SEC_READONLY);
   4380 	  if (s == NULL
   4381 	      || !bfd_set_section_alignment (s, bed->s->log_file_align))
   4382 	    return false;
   4383 	}
   4384     }
   4385 
   4386   return true;
   4387 }
   4388 
   4389 /* Compute the total GOT and PLT size required by each symbol in each
   4390    range.  Symbols may require up to 4 words in the GOT: an entry
   4391    pointing to the symbol, an entry pointing to its function
   4392    descriptor, and a private function descriptors taking two
   4393    words.  */
   4394 
   4395 static void
   4396 _frvfdpic_count_nontls_entries (struct frvfdpic_relocs_info *entry,
   4397 				struct _frvfdpic_dynamic_got_info *dinfo)
   4398 {
   4399   /* Allocate space for a GOT entry pointing to the symbol.  */
   4400   if (entry->got12)
   4401     dinfo->got12 += 4;
   4402   else if (entry->gotlos)
   4403     dinfo->gotlos += 4;
   4404   else if (entry->gothilo)
   4405     dinfo->gothilo += 4;
   4406   else
   4407     entry->relocs32--;
   4408   entry->relocs32++;
   4409 
   4410   /* Allocate space for a GOT entry pointing to the function
   4411      descriptor.  */
   4412   if (entry->fdgot12)
   4413     dinfo->got12 += 4;
   4414   else if (entry->fdgotlos)
   4415     dinfo->gotlos += 4;
   4416   else if (entry->fdgothilo)
   4417     dinfo->gothilo += 4;
   4418   else
   4419     entry->relocsfd--;
   4420   entry->relocsfd++;
   4421 
   4422   /* Decide whether we need a PLT entry, a function descriptor in the
   4423      GOT, and a lazy PLT entry for this symbol.  */
   4424   entry->plt = entry->call
   4425     && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
   4426     && elf_hash_table (dinfo->info)->dynamic_sections_created;
   4427   entry->privfd = entry->plt
   4428     || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
   4429     || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
   4430 	&& (entry->symndx != -1
   4431 	    || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
   4432   entry->lazyplt = entry->privfd
   4433     && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
   4434     && ! (dinfo->info->flags & DF_BIND_NOW)
   4435     && elf_hash_table (dinfo->info)->dynamic_sections_created;
   4436 
   4437   /* Allocate space for a function descriptor.  */
   4438   if (entry->fdgoff12)
   4439     dinfo->fd12 += 8;
   4440   else if (entry->fdgofflos)
   4441     dinfo->fdlos += 8;
   4442   else if (entry->privfd && entry->plt)
   4443     dinfo->fdplt += 8;
   4444   else if (entry->privfd)
   4445     dinfo->fdhilo += 8;
   4446   else
   4447     entry->relocsfdv--;
   4448   entry->relocsfdv++;
   4449 
   4450   if (entry->lazyplt)
   4451     dinfo->lzplt += 8;
   4452 }
   4453 
   4454 /* Compute the total GOT size required by each TLS symbol in each
   4455    range.  Symbols may require up to 5 words in the GOT: an entry
   4456    holding the TLS offset for the symbol, and an entry with a full TLS
   4457    descriptor taking 4 words.  */
   4458 
   4459 static void
   4460 _frvfdpic_count_tls_entries (struct frvfdpic_relocs_info *entry,
   4461 			     struct _frvfdpic_dynamic_got_info *dinfo,
   4462 			     bool subtract)
   4463 {
   4464   const int l = subtract ? -1 : 1;
   4465 
   4466   /* Allocate space for a GOT entry with the TLS offset of the
   4467      symbol.  */
   4468   if (entry->tlsoff12)
   4469     dinfo->got12 += 4 * l;
   4470   else if (entry->tlsofflos)
   4471     dinfo->gotlos += 4 * l;
   4472   else if (entry->tlsoffhilo)
   4473     dinfo->gothilo += 4 * l;
   4474   else
   4475     entry->relocstlsoff -= l;
   4476   entry->relocstlsoff += l;
   4477 
   4478   /* If there's any TLSOFF relocation, mark the output file as not
   4479      suitable for dlopening.  This mark will remain even if we relax
   4480      all such relocations, but this is not a problem, since we'll only
   4481      do so for executables, and we definitely don't want anyone
   4482      dlopening executables.  */
   4483   if (entry->relocstlsoff)
   4484     dinfo->info->flags |= DF_STATIC_TLS;
   4485 
   4486   /* Allocate space for a TLS descriptor.  */
   4487   if (entry->tlsdesc12)
   4488     dinfo->tlsd12 += 8 * l;
   4489   else if (entry->tlsdesclos)
   4490     dinfo->tlsdlos += 8 * l;
   4491   else if (entry->tlsplt)
   4492     dinfo->tlsdplt += 8 * l;
   4493   else if (entry->tlsdeschilo)
   4494     dinfo->tlsdhilo += 8 * l;
   4495   else
   4496     entry->relocstlsd -= l;
   4497   entry->relocstlsd += l;
   4498 }
   4499 
   4500 /* Compute the number of dynamic relocations and fixups that a symbol
   4501    requires, and add (or subtract) from the grand and per-symbol
   4502    totals.  */
   4503 
   4504 static void
   4505 _frvfdpic_count_relocs_fixups (struct frvfdpic_relocs_info *entry,
   4506 			       struct _frvfdpic_dynamic_got_info *dinfo,
   4507 			       bool subtract)
   4508 {
   4509   bfd_vma relocs = 0, fixups = 0, tlsrets = 0;
   4510 
   4511   if (!bfd_link_pde (dinfo->info))
   4512     {
   4513       relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv
   4514 	+ entry->relocstlsd;
   4515 
   4516       /* In the executable, TLS relocations to symbols that bind
   4517 	 locally (including those that resolve to global TLS offsets)
   4518 	 are resolved immediately, without any need for fixups or
   4519 	 dynamic relocations.  In shared libraries, however, we must
   4520 	 emit dynamic relocations even for local symbols, because we
   4521 	 don't know the module id the library is going to get at
   4522 	 run-time, nor its TLS base offset.  */
   4523       if (!bfd_link_executable (dinfo->info)
   4524 	  || (entry->symndx == -1
   4525 	      && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
   4526 	relocs += entry->relocstlsoff;
   4527     }
   4528   else
   4529     {
   4530       if (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
   4531 	{
   4532 	  if (entry->symndx != -1
   4533 	      || entry->d.h->root.type != bfd_link_hash_undefweak)
   4534 	    fixups += entry->relocs32 + 2 * entry->relocsfdv;
   4535 	  fixups += entry->relocstlsd;
   4536 	  tlsrets += entry->relocstlsd;
   4537 	}
   4538       else
   4539 	{
   4540 	  relocs += entry->relocs32 + entry->relocsfdv
   4541 	    + entry->relocstlsoff + entry->relocstlsd;
   4542 	}
   4543 
   4544       if (entry->symndx != -1
   4545 	  || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
   4546 	{
   4547 	  if (entry->symndx != -1
   4548 	      || entry->d.h->root.type != bfd_link_hash_undefweak)
   4549 	    fixups += entry->relocsfd;
   4550 	}
   4551       else
   4552 	relocs += entry->relocsfd;
   4553     }
   4554 
   4555   if (subtract)
   4556     {
   4557       relocs = - relocs;
   4558       fixups = - fixups;
   4559       tlsrets = - tlsrets;
   4560     }
   4561 
   4562   entry->dynrelocs += relocs;
   4563   entry->fixups += fixups;
   4564   dinfo->relocs += relocs;
   4565   dinfo->fixups += fixups;
   4566   dinfo->tls_ret_refs += tlsrets;
   4567 }
   4568 
   4569 /* Look for opportunities to relax TLS relocations.  We can assume
   4570    we're linking the main executable or a static-tls library, since
   4571    otherwise we wouldn't have got here.  When relaxing, we have to
   4572    first undo any previous accounting of TLS uses of fixups, dynamic
   4573    relocations, GOT and PLT entries.  */
   4574 
   4575 static void
   4576 _frvfdpic_relax_tls_entries (struct frvfdpic_relocs_info *entry,
   4577 			     struct _frvfdpic_dynamic_got_info *dinfo,
   4578 			     bool relaxing)
   4579 {
   4580   bool changed = ! relaxing;
   4581 
   4582   BFD_ASSERT (bfd_link_executable (dinfo->info)
   4583 	      || (dinfo->info->flags & DF_STATIC_TLS));
   4584 
   4585   if (entry->tlsdesc12 || entry->tlsdesclos || entry->tlsdeschilo)
   4586     {
   4587       if (! changed)
   4588 	{
   4589 	  _frvfdpic_count_relocs_fixups (entry, dinfo, true);
   4590 	  _frvfdpic_count_tls_entries (entry, dinfo, true);
   4591 	  changed = true;
   4592 	}
   4593 
   4594       /* When linking an executable, we can always decay GOTTLSDESC to
   4595 	 TLSMOFF, if the symbol is local, or GOTTLSOFF, otherwise.
   4596 	 When linking a static-tls shared library, using TLSMOFF is
   4597 	 not an option, but we can still use GOTTLSOFF.  When decaying
   4598 	 to GOTTLSOFF, we must keep the GOT entry in range.  We know
   4599 	 it has to fit because we'll be trading the 4 words of hte TLS
   4600 	 descriptor for a single word in the same range.  */
   4601       if (! bfd_link_executable (dinfo->info)
   4602 	  || (entry->symndx == -1
   4603 	      && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
   4604 	{
   4605 	  entry->tlsoff12 |= entry->tlsdesc12;
   4606 	  entry->tlsofflos |= entry->tlsdesclos;
   4607 	  entry->tlsoffhilo |= entry->tlsdeschilo;
   4608 	}
   4609 
   4610       entry->tlsdesc12 = entry->tlsdesclos = entry->tlsdeschilo = 0;
   4611     }
   4612 
   4613   /* We can only decay TLSOFFs or call #gettlsoff to TLSMOFF in the
   4614      main executable.  We have to check whether the symbol's TLSOFF is
   4615      in range for a setlos.  For symbols with a hash entry, we can
   4616      determine exactly what to do; for others locals, we don't have
   4617      addresses handy, so we use the size of the TLS section as an
   4618      approximation.  If we get it wrong, we'll retain a GOT entry
   4619      holding the TLS offset (without dynamic relocations or fixups),
   4620      but we'll still optimize away the loads from it.  Since TLS sizes
   4621      are generally very small, it's probably not worth attempting to
   4622      do better than this.  */
   4623   if ((entry->tlsplt
   4624        || entry->tlsoff12 || entry->tlsofflos || entry->tlsoffhilo)
   4625       && bfd_link_executable (dinfo->info) && relaxing
   4626       && ((entry->symndx == -1
   4627 	   && FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
   4628 	   /* The above may hold for an undefweak TLS symbol, so make
   4629 	      sure we don't have this case before accessing def.value
   4630 	      and def.section.  */
   4631 	   && (entry->d.h->root.type == bfd_link_hash_undefweak
   4632 	       || (bfd_vma)(entry->d.h->root.u.def.value
   4633 			    + (entry->d.h->root.u.def.section
   4634 			       ->output_section->vma)
   4635 			    + entry->d.h->root.u.def.section->output_offset
   4636 			    + entry->addend
   4637 			    - tls_biased_base (dinfo->info)
   4638 			    + 32768) < (bfd_vma)65536))
   4639 	  || (entry->symndx != -1
   4640 	      && (elf_hash_table (dinfo->info)->tls_sec->size
   4641 		  + entry->addend < 32768 + FRVFDPIC_TLS_BIAS))))
   4642     {
   4643       if (! changed)
   4644 	{
   4645 	  _frvfdpic_count_relocs_fixups (entry, dinfo, true);
   4646 	  _frvfdpic_count_tls_entries (entry, dinfo, true);
   4647 	  changed = true;
   4648 	}
   4649 
   4650       entry->tlsplt =
   4651 	entry->tlsoff12 = entry->tlsofflos = entry->tlsoffhilo = 0;
   4652     }
   4653 
   4654   /* We can decay `call #gettlsoff' to a ldi #tlsoff if we already
   4655      have a #gottlsoff12 relocation for this entry, or if we can fit
   4656      one more in the 12-bit (and 16-bit) ranges.  */
   4657   if (entry->tlsplt
   4658       && (entry->tlsoff12
   4659 	  || (relaxing
   4660 	      && dinfo->got12 + dinfo->fd12 + dinfo->tlsd12 <= 4096 - 12 - 4
   4661 	      && (dinfo->got12 + dinfo->fd12 + dinfo->tlsd12
   4662 		  + dinfo->gotlos + dinfo->fdlos + dinfo->tlsdlos
   4663 		  <= 65536 - 12 - 4))))
   4664     {
   4665       if (! changed)
   4666 	{
   4667 	  _frvfdpic_count_relocs_fixups (entry, dinfo, true);
   4668 	  _frvfdpic_count_tls_entries (entry, dinfo, true);
   4669 	  changed = true;
   4670 	}
   4671 
   4672       entry->tlsoff12 = 1;
   4673       entry->tlsplt = 0;
   4674     }
   4675 
   4676   if (changed)
   4677     {
   4678       _frvfdpic_count_tls_entries (entry, dinfo, false);
   4679       _frvfdpic_count_relocs_fixups (entry, dinfo, false);
   4680     }
   4681 
   4682   return;
   4683 }
   4684 
   4685 /* Compute the total GOT and PLT size required by each symbol in each range. *
   4686    Symbols may require up to 4 words in the GOT: an entry pointing to
   4687    the symbol, an entry pointing to its function descriptor, and a
   4688    private function descriptors taking two words.  */
   4689 
   4690 static int
   4691 _frvfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
   4692 {
   4693   struct frvfdpic_relocs_info *entry = *entryp;
   4694   struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
   4695 
   4696   _frvfdpic_count_nontls_entries (entry, dinfo);
   4697 
   4698   if (bfd_link_executable (dinfo->info)
   4699       || (dinfo->info->flags & DF_STATIC_TLS))
   4700     _frvfdpic_relax_tls_entries (entry, dinfo, false);
   4701   else
   4702     {
   4703       _frvfdpic_count_tls_entries (entry, dinfo, false);
   4704       _frvfdpic_count_relocs_fixups (entry, dinfo, false);
   4705     }
   4706 
   4707   return 1;
   4708 }
   4709 
   4710 /* Determine the positive and negative ranges to be used by each
   4711    offset range in the GOT.  FDCUR and CUR, that must be aligned to a
   4712    double-word boundary, are the minimum (negative) and maximum
   4713    (positive) GOT offsets already used by previous ranges, except for
   4714    an ODD entry that may have been left behind.  GOT and FD indicate
   4715    the size of GOT entries and function descriptors that must be
   4716    placed within the range from -WRAP to WRAP.  If there's room left,
   4717    up to FDPLT bytes should be reserved for additional function
   4718    descriptors.  */
   4719 
   4720 inline static bfd_signed_vma
   4721 _frvfdpic_compute_got_alloc_data (struct _frvfdpic_dynamic_got_alloc_data *gad,
   4722 				  bfd_signed_vma fdcur,
   4723 				  bfd_signed_vma odd,
   4724 				  bfd_signed_vma cur,
   4725 				  bfd_vma got,
   4726 				  bfd_vma fd,
   4727 				  bfd_vma fdplt,
   4728 				  bfd_vma tlsd,
   4729 				  bfd_vma tlsdplt,
   4730 				  bfd_vma wrap)
   4731 {
   4732   bfd_signed_vma wrapmin = -wrap;
   4733   const bfd_vma tdescsz = 8;
   4734 
   4735   /* Start at the given initial points.  */
   4736   gad->fdcur = fdcur;
   4737   gad->cur = cur;
   4738 
   4739   /* If we had an incoming odd word and we have any got entries that
   4740      are going to use it, consume it, otherwise leave gad->odd at
   4741      zero.  We might force gad->odd to zero and return the incoming
   4742      odd such that it is used by the next range, but then GOT entries
   4743      might appear to be out of order and we wouldn't be able to
   4744      shorten the GOT by one word if it turns out to end with an
   4745      unpaired GOT entry.  */
   4746   if (odd && got)
   4747     {
   4748       gad->odd = odd;
   4749       got -= 4;
   4750       odd = 0;
   4751     }
   4752   else
   4753     gad->odd = 0;
   4754 
   4755   /* If we're left with an unpaired GOT entry, compute its location
   4756      such that we can return it.  Otherwise, if got doesn't require an
   4757      odd number of words here, either odd was already zero in the
   4758      block above, or it was set to zero because got was non-zero, or
   4759      got was already zero.  In the latter case, we want the value of
   4760      odd to carry over to the return statement, so we don't want to
   4761      reset odd unless the condition below is true.  */
   4762   if (got & 4)
   4763     {
   4764       odd = cur + got;
   4765       got += 4;
   4766     }
   4767 
   4768   /* Compute the tentative boundaries of this range.  */
   4769   gad->max = cur + got;
   4770   gad->min = fdcur - fd;
   4771   gad->fdplt = 0;
   4772 
   4773   /* If function descriptors took too much space, wrap some of them
   4774      around.  */
   4775   if (gad->min < wrapmin)
   4776     {
   4777       gad->max += wrapmin - gad->min;
   4778       gad->tmin = gad->min = wrapmin;
   4779     }
   4780 
   4781   /* If GOT entries took too much space, wrap some of them around.
   4782      This may well cause gad->min to become lower than wrapmin.  This
   4783      will cause a relocation overflow later on, so we don't have to
   4784      report it here . */
   4785   if ((bfd_vma) gad->max > wrap)
   4786     {
   4787       gad->min -= gad->max - wrap;
   4788       gad->max = wrap;
   4789     }
   4790 
   4791   /* Add TLS descriptors.  */
   4792   gad->tmax = gad->max + tlsd;
   4793   gad->tmin = gad->min;
   4794   gad->tlsdplt = 0;
   4795 
   4796   /* If TLS descriptors took too much space, wrap an integral number
   4797      of them around.  */
   4798   if ((bfd_vma) gad->tmax > wrap)
   4799     {
   4800       bfd_vma wrapsize = gad->tmax - wrap;
   4801 
   4802       wrapsize += tdescsz / 2;
   4803       wrapsize &= ~ tdescsz / 2;
   4804 
   4805       gad->tmin -= wrapsize;
   4806       gad->tmax -= wrapsize;
   4807     }
   4808 
   4809   /* If there is space left and we have function descriptors
   4810      referenced in PLT entries that could take advantage of shorter
   4811      offsets, place them now.  */
   4812   if (fdplt && gad->tmin > wrapmin)
   4813     {
   4814       bfd_vma fds;
   4815 
   4816       if ((bfd_vma) (gad->tmin - wrapmin) < fdplt)
   4817 	fds = gad->tmin - wrapmin;
   4818       else
   4819 	fds = fdplt;
   4820 
   4821       fdplt -= fds;
   4822       gad->min -= fds;
   4823       gad->tmin -= fds;
   4824       gad->fdplt += fds;
   4825     }
   4826 
   4827   /* If there is more space left, try to place some more function
   4828      descriptors for PLT entries.  */
   4829   if (fdplt && (bfd_vma) gad->tmax < wrap)
   4830     {
   4831       bfd_vma fds;
   4832 
   4833       if ((bfd_vma) (wrap - gad->tmax) < fdplt)
   4834 	fds = wrap - gad->tmax;
   4835       else
   4836 	fds = fdplt;
   4837 
   4838       fdplt -= fds;
   4839       gad->max += fds;
   4840       gad->tmax += fds;
   4841       gad->fdplt += fds;
   4842     }
   4843 
   4844   /* If there is space left and we have TLS descriptors referenced in
   4845      PLT entries that could take advantage of shorter offsets, place
   4846      them now.  */
   4847   if (tlsdplt && gad->tmin > wrapmin)
   4848     {
   4849       bfd_vma tlsds;
   4850 
   4851       if ((bfd_vma) (gad->tmin - wrapmin) < tlsdplt)
   4852 	tlsds = (gad->tmin - wrapmin) & ~ (tdescsz / 2);
   4853       else
   4854 	tlsds = tlsdplt;
   4855 
   4856       tlsdplt -= tlsds;
   4857       gad->tmin -= tlsds;
   4858       gad->tlsdplt += tlsds;
   4859     }
   4860 
   4861   /* If there is more space left, try to place some more TLS
   4862      descriptors for PLT entries.  Although we could try to fit an
   4863      additional TLS descriptor with half of it just before before the
   4864      wrap point and another right past the wrap point, this might
   4865      cause us to run out of space for the next region, so don't do
   4866      it.  */
   4867   if (tlsdplt && (bfd_vma) gad->tmax < wrap - tdescsz / 2)
   4868     {
   4869       bfd_vma tlsds;
   4870 
   4871       if ((bfd_vma) (wrap - gad->tmax) < tlsdplt)
   4872 	tlsds = (wrap - gad->tmax) & ~ (tdescsz / 2);
   4873       else
   4874 	tlsds = tlsdplt;
   4875 
   4876       tlsdplt -= tlsds;
   4877       gad->tmax += tlsds;
   4878       gad->tlsdplt += tlsds;
   4879     }
   4880 
   4881   /* If odd was initially computed as an offset past the wrap point,
   4882      wrap it around.  */
   4883   if (odd > gad->max)
   4884     odd = gad->min + odd - gad->max;
   4885 
   4886   /* _frvfdpic_get_got_entry() below will always wrap gad->cur if needed
   4887      before returning, so do it here too.  This guarantees that,
   4888      should cur and fdcur meet at the wrap point, they'll both be
   4889      equal to min.  */
   4890   if (gad->cur == gad->max)
   4891     gad->cur = gad->min;
   4892 
   4893   /* Ditto for _frvfdpic_get_tlsdesc_entry().  */
   4894   gad->tcur = gad->max;
   4895   if (gad->tcur == gad->tmax)
   4896     gad->tcur = gad->tmin;
   4897 
   4898   return odd;
   4899 }
   4900 
   4901 /* Compute the location of the next GOT entry, given the allocation
   4902    data for a range.  */
   4903 
   4904 inline static bfd_signed_vma
   4905 _frvfdpic_get_got_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
   4906 {
   4907   bfd_signed_vma ret;
   4908 
   4909   if (gad->odd)
   4910     {
   4911       /* If there was an odd word left behind, use it.  */
   4912       ret = gad->odd;
   4913       gad->odd = 0;
   4914     }
   4915   else
   4916     {
   4917       /* Otherwise, use the word pointed to by cur, reserve the next
   4918 	 as an odd word, and skip to the next pair of words, possibly
   4919 	 wrapping around.  */
   4920       ret = gad->cur;
   4921       gad->odd = gad->cur + 4;
   4922       gad->cur += 8;
   4923       if (gad->cur == gad->max)
   4924 	gad->cur = gad->min;
   4925     }
   4926 
   4927   return ret;
   4928 }
   4929 
   4930 /* Compute the location of the next function descriptor entry in the
   4931    GOT, given the allocation data for a range.  */
   4932 
   4933 inline static bfd_signed_vma
   4934 _frvfdpic_get_fd_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
   4935 {
   4936   /* If we're at the bottom, wrap around, and only then allocate the
   4937      next pair of words.  */
   4938   if (gad->fdcur == gad->min)
   4939     gad->fdcur = gad->max;
   4940   return gad->fdcur -= 8;
   4941 }
   4942 
   4943 /* Compute the location of the next TLS descriptor entry in the GOT,
   4944    given the allocation data for a range.  */
   4945 inline static bfd_signed_vma
   4946 _frvfdpic_get_tlsdesc_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
   4947 {
   4948   bfd_signed_vma ret;
   4949 
   4950   ret = gad->tcur;
   4951 
   4952   gad->tcur += 8;
   4953 
   4954   /* If we're at the top of the region, wrap around to the bottom.  */
   4955   if (gad->tcur == gad->tmax)
   4956     gad->tcur = gad->tmin;
   4957 
   4958   return ret;
   4959 }
   4960 
   4961 /* Assign GOT offsets for every GOT entry and function descriptor.
   4962    Doing everything in a single pass is tricky.  */
   4963 
   4964 static int
   4965 _frvfdpic_assign_got_entries (void **entryp, void *info_)
   4966 {
   4967   struct frvfdpic_relocs_info *entry = *entryp;
   4968   struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
   4969 
   4970   if (entry->got12)
   4971     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->got12);
   4972   else if (entry->gotlos)
   4973     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
   4974   else if (entry->gothilo)
   4975     entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
   4976 
   4977   if (entry->fdgot12)
   4978     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->got12);
   4979   else if (entry->fdgotlos)
   4980     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
   4981   else if (entry->fdgothilo)
   4982     entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
   4983 
   4984   if (entry->fdgoff12)
   4985     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
   4986   else if (entry->plt && dinfo->got12.fdplt)
   4987     {
   4988       dinfo->got12.fdplt -= 8;
   4989       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
   4990     }
   4991   else if (entry->fdgofflos)
   4992     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
   4993   else if (entry->plt && dinfo->gotlos.fdplt)
   4994     {
   4995       dinfo->gotlos.fdplt -= 8;
   4996       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
   4997     }
   4998   else if (entry->plt)
   4999     {
   5000       dinfo->gothilo.fdplt -= 8;
   5001       entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
   5002     }
   5003   else if (entry->privfd)
   5004     entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
   5005 
   5006   if (entry->tlsoff12)
   5007     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->got12);
   5008   else if (entry->tlsofflos)
   5009     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
   5010   else if (entry->tlsoffhilo)
   5011     entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
   5012 
   5013   if (entry->tlsdesc12)
   5014     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
   5015   else if (entry->tlsplt && dinfo->got12.tlsdplt)
   5016     {
   5017       dinfo->got12.tlsdplt -= 8;
   5018       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
   5019     }
   5020   else if (entry->tlsdesclos)
   5021     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
   5022   else if (entry->tlsplt && dinfo->gotlos.tlsdplt)
   5023     {
   5024       dinfo->gotlos.tlsdplt -= 8;
   5025       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
   5026     }
   5027   else if (entry->tlsplt)
   5028     {
   5029       dinfo->gothilo.tlsdplt -= 8;
   5030       entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
   5031     }
   5032   else if (entry->tlsdeschilo)
   5033     entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
   5034 
   5035   return 1;
   5036 }
   5037 
   5038 /* Assign GOT offsets to private function descriptors used by PLT
   5039    entries (or referenced by 32-bit offsets), as well as PLT entries
   5040    and lazy PLT entries.  */
   5041 
   5042 static int
   5043 _frvfdpic_assign_plt_entries (void **entryp, void *info_)
   5044 {
   5045   struct frvfdpic_relocs_info *entry = *entryp;
   5046   struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
   5047 
   5048   if (entry->privfd)
   5049     BFD_ASSERT (entry->fd_entry);
   5050 
   5051   if (entry->plt)
   5052     {
   5053       int size;
   5054 
   5055       /* We use the section's raw size to mark the location of the
   5056 	 next PLT entry.  */
   5057       entry->plt_entry = frvfdpic_plt_section (dinfo->g.info)->size;
   5058 
   5059       /* Figure out the length of this PLT entry based on the
   5060 	 addressing mode we need to reach the function descriptor.  */
   5061       BFD_ASSERT (entry->fd_entry);
   5062       if (entry->fd_entry >= -(1 << (12 - 1))
   5063 	  && entry->fd_entry < (1 << (12 - 1)))
   5064 	size = 8;
   5065       else if (entry->fd_entry >= -(1 << (16 - 1))
   5066 	       && entry->fd_entry < (1 << (16 - 1)))
   5067 	size = 12;
   5068       else
   5069 	size = 16;
   5070 
   5071       frvfdpic_plt_section (dinfo->g.info)->size += size;
   5072     }
   5073 
   5074   if (entry->lazyplt)
   5075     {
   5076       entry->lzplt_entry = dinfo->g.lzplt;
   5077       dinfo->g.lzplt += 8;
   5078       /* If this entry is the one that gets the resolver stub, account
   5079 	 for the additional instruction.  */
   5080       if (entry->lzplt_entry % FRVFDPIC_LZPLT_BLOCK_SIZE
   5081 	  == FRVFDPIC_LZPLT_RESOLV_LOC)
   5082 	dinfo->g.lzplt += 4;
   5083     }
   5084 
   5085   if (entry->tlsplt)
   5086     {
   5087       int size;
   5088 
   5089       entry->tlsplt_entry
   5090 	= frvfdpic_plt_section (dinfo->g.info)->size;
   5091 
   5092       if (bfd_link_executable (dinfo->g.info)
   5093 	  && (entry->symndx != -1
   5094 	      || FRVFDPIC_SYM_LOCAL (dinfo->g.info, entry->d.h)))
   5095 	{
   5096 	  if ((bfd_signed_vma)entry->addend >= -(1 << (16 - 1))
   5097 	      /* FIXME: here we use the size of the TLS section
   5098 		 as an upper bound for the value of the TLS
   5099 		 symbol, because we may not know the exact value
   5100 		 yet.  If we get it wrong, we'll just waste a
   5101 		 word in the PLT, and we should never get even
   5102 		 close to 32 KiB of TLS anyway.  */
   5103 	      && elf_hash_table (dinfo->g.info)->tls_sec
   5104 	      && (elf_hash_table (dinfo->g.info)->tls_sec->size
   5105 		  + (bfd_signed_vma)(entry->addend) <= (1 << (16 - 1))))
   5106 	    size = 8;
   5107 	  else
   5108 	    size = 12;
   5109 	}
   5110       else if (entry->tlsoff_entry)
   5111 	{
   5112 	  if (entry->tlsoff_entry >= -(1 << (12 - 1))
   5113 	      && entry->tlsoff_entry < (1 << (12 - 1)))
   5114 	    size = 8;
   5115 	  else if (entry->tlsoff_entry >= -(1 << (16 - 1))
   5116 		   && entry->tlsoff_entry < (1 << (16 - 1)))
   5117 	    size = 12;
   5118 	  else
   5119 	    size = 16;
   5120 	}
   5121       else
   5122 	{
   5123 	  BFD_ASSERT (entry->tlsdesc_entry);
   5124 
   5125 	  if (entry->tlsdesc_entry >= -(1 << (12 - 1))
   5126 	      && entry->tlsdesc_entry < (1 << (12 - 1)))
   5127 	    size = 8;
   5128 	  else if (entry->tlsdesc_entry >= -(1 << (16 - 1))
   5129 		   && entry->tlsdesc_entry < (1 << (16 - 1)))
   5130 	    size = 12;
   5131 	  else
   5132 	    size = 16;
   5133 	}
   5134 
   5135       frvfdpic_plt_section (dinfo->g.info)->size += size;
   5136     }
   5137 
   5138   return 1;
   5139 }
   5140 
   5141 /* Cancel out any effects of calling _frvfdpic_assign_got_entries and
   5142    _frvfdpic_assign_plt_entries.  */
   5143 
   5144 static int
   5145 _frvfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
   5146 {
   5147   struct frvfdpic_relocs_info *entry = *entryp;
   5148 
   5149   entry->got_entry = 0;
   5150   entry->fdgot_entry = 0;
   5151   entry->fd_entry = 0;
   5152   entry->plt_entry = (bfd_vma)-1;
   5153   entry->lzplt_entry = (bfd_vma)-1;
   5154   entry->tlsoff_entry = 0;
   5155   entry->tlsdesc_entry = 0;
   5156   entry->tlsplt_entry = (bfd_vma)-1;
   5157 
   5158   return 1;
   5159 }
   5160 
   5161 /* Follow indirect and warning hash entries so that each got entry
   5162    points to the final symbol definition.  P must point to a pointer
   5163    to the hash table we're traversing.  Since this traversal may
   5164    modify the hash table, we set this pointer to NULL to indicate
   5165    we've made a potentially-destructive change to the hash table, so
   5166    the traversal must be restarted.  */
   5167 static int
   5168 _frvfdpic_resolve_final_relocs_info (void **entryp, void *p)
   5169 {
   5170   struct frvfdpic_relocs_info *entry = *entryp;
   5171   htab_t *htab = p;
   5172 
   5173   if (entry->symndx == -1)
   5174     {
   5175       struct elf_link_hash_entry *h = entry->d.h;
   5176       struct frvfdpic_relocs_info *oentry;
   5177 
   5178       while (h->root.type == bfd_link_hash_indirect
   5179 	     || h->root.type == bfd_link_hash_warning)
   5180 	h = (struct elf_link_hash_entry *)h->root.u.i.link;
   5181 
   5182       if (entry->d.h == h)
   5183 	return 1;
   5184 
   5185       oentry = frvfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
   5186 						NO_INSERT);
   5187 
   5188       if (oentry)
   5189 	{
   5190 	  /* Merge the two entries.  */
   5191 	  frvfdpic_pic_merge_early_relocs_info (oentry, entry);
   5192 	  htab_clear_slot (*htab, entryp);
   5193 	  return 1;
   5194 	}
   5195 
   5196       entry->d.h = h;
   5197 
   5198       /* If we can't find this entry with the new bfd hash, re-insert
   5199 	 it, and get the traversal restarted.  */
   5200       if (! htab_find (*htab, entry))
   5201 	{
   5202 	  htab_clear_slot (*htab, entryp);
   5203 	  entryp = htab_find_slot (*htab, entry, INSERT);
   5204 	  if (! *entryp)
   5205 	    *entryp = entry;
   5206 	  /* Abort the traversal, since the whole table may have
   5207 	     moved, and leave it up to the parent to restart the
   5208 	     process.  */
   5209 	  *(htab_t *)p = NULL;
   5210 	  return 0;
   5211 	}
   5212     }
   5213 
   5214   return 1;
   5215 }
   5216 
   5217 /* Compute the total size of the GOT, the PLT, the dynamic relocations
   5218    section and the rofixup section.  Assign locations for GOT and PLT
   5219    entries.  */
   5220 
   5221 static bool
   5222 _frvfdpic_size_got_plt (bfd *output_bfd,
   5223 			struct _frvfdpic_dynamic_got_plt_info *gpinfop)
   5224 {
   5225   bfd_signed_vma odd;
   5226   bfd_vma limit, tlslimit;
   5227   struct bfd_link_info *info = gpinfop->g.info;
   5228   bfd *dynobj = elf_hash_table (info)->dynobj;
   5229 
   5230   memcpy (frvfdpic_dynamic_got_plt_info (info), &gpinfop->g,
   5231 	  sizeof (gpinfop->g));
   5232 
   5233   odd = 12;
   5234   /* Compute the total size taken by entries in the 12-bit and 16-bit
   5235      ranges, to tell how many PLT function descriptors we can bring
   5236      into the 12-bit range without causing the 16-bit range to
   5237      overflow.  */
   5238   limit = odd + gpinfop->g.got12 + gpinfop->g.gotlos
   5239     + gpinfop->g.fd12 + gpinfop->g.fdlos
   5240     + gpinfop->g.tlsd12 + gpinfop->g.tlsdlos;
   5241   if (limit < (bfd_vma)1 << 16)
   5242     limit = ((bfd_vma)1 << 16) - limit;
   5243   else
   5244     limit = 0;
   5245   if (gpinfop->g.fdplt < limit)
   5246     {
   5247       tlslimit = (limit - gpinfop->g.fdplt) & ~ (bfd_vma) 8;
   5248       limit = gpinfop->g.fdplt;
   5249     }
   5250   else
   5251     tlslimit = 0;
   5252   if (gpinfop->g.tlsdplt < tlslimit)
   5253     tlslimit = gpinfop->g.tlsdplt;
   5254 
   5255   /* Determine the ranges of GOT offsets that we can use for each
   5256      range of addressing modes.  */
   5257   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->got12,
   5258 					  0,
   5259 					  odd,
   5260 					  16,
   5261 					  gpinfop->g.got12,
   5262 					  gpinfop->g.fd12,
   5263 					  limit,
   5264 					  gpinfop->g.tlsd12,
   5265 					  tlslimit,
   5266 					  (bfd_vma)1 << (12-1));
   5267   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gotlos,
   5268 					  gpinfop->got12.tmin,
   5269 					  odd,
   5270 					  gpinfop->got12.tmax,
   5271 					  gpinfop->g.gotlos,
   5272 					  gpinfop->g.fdlos,
   5273 					  gpinfop->g.fdplt
   5274 					  - gpinfop->got12.fdplt,
   5275 					  gpinfop->g.tlsdlos,
   5276 					  gpinfop->g.tlsdplt
   5277 					  - gpinfop->got12.tlsdplt,
   5278 					  (bfd_vma)1 << (16-1));
   5279   odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gothilo,
   5280 					  gpinfop->gotlos.tmin,
   5281 					  odd,
   5282 					  gpinfop->gotlos.tmax,
   5283 					  gpinfop->g.gothilo,
   5284 					  gpinfop->g.fdhilo,
   5285 					  gpinfop->g.fdplt
   5286 					  - gpinfop->got12.fdplt
   5287 					  - gpinfop->gotlos.fdplt,
   5288 					  gpinfop->g.tlsdhilo,
   5289 					  gpinfop->g.tlsdplt
   5290 					  - gpinfop->got12.tlsdplt
   5291 					  - gpinfop->gotlos.tlsdplt,
   5292 					  (bfd_vma)1 << (32-1));
   5293 
   5294   /* Now assign (most) GOT offsets.  */
   5295   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_got_entries,
   5296 		 gpinfop);
   5297 
   5298   frvfdpic_got_section (info)->size = gpinfop->gothilo.tmax
   5299     - gpinfop->gothilo.tmin
   5300     /* If an odd word is the last word of the GOT, we don't need this
   5301        word to be part of the GOT.  */
   5302     - (odd + 4 == gpinfop->gothilo.tmax ? 4 : 0);
   5303   if (frvfdpic_got_section (info)->size == 0)
   5304     frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
   5305   else if (frvfdpic_got_section (info)->size == 12
   5306 	   && ! elf_hash_table (info)->dynamic_sections_created)
   5307     {
   5308       frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
   5309       frvfdpic_got_section (info)->size = 0;
   5310     }
   5311   /* This will be non-NULL during relaxation.  The assumption is that
   5312      the size of one of these sections will never grow, only shrink,
   5313      so we can use the larger buffer we allocated before.  */
   5314   else if (frvfdpic_got_section (info)->contents == NULL)
   5315     {
   5316       frvfdpic_got_section (info)->contents =
   5317 	(bfd_byte *) bfd_zalloc (dynobj,
   5318 				 frvfdpic_got_section (info)->size);
   5319       if (frvfdpic_got_section (info)->contents == NULL)
   5320 	return false;
   5321     }
   5322 
   5323   if (frvfdpic_gotrel_section (info))
   5324     /* Subtract the number of lzplt entries, since those will generate
   5325        relocations in the pltrel section.  */
   5326     frvfdpic_gotrel_section (info)->size =
   5327       (gpinfop->g.relocs - gpinfop->g.lzplt / 8)
   5328       * get_elf_backend_data (output_bfd)->s->sizeof_rel;
   5329   else
   5330     BFD_ASSERT (gpinfop->g.relocs == 0);
   5331   if (frvfdpic_gotrel_section (info)->size == 0)
   5332     frvfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
   5333   else if (frvfdpic_gotrel_section (info)->contents == NULL)
   5334     {
   5335       frvfdpic_gotrel_section (info)->contents =
   5336 	(bfd_byte *) bfd_zalloc (dynobj,
   5337 				 frvfdpic_gotrel_section (info)->size);
   5338       if (frvfdpic_gotrel_section (info)->contents == NULL)
   5339 	return false;
   5340     }
   5341 
   5342   frvfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
   5343   if (frvfdpic_gotfixup_section (info)->size == 0)
   5344     frvfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
   5345   else if (frvfdpic_gotfixup_section (info)->contents == NULL)
   5346     {
   5347       frvfdpic_gotfixup_section (info)->contents =
   5348 	(bfd_byte *) bfd_zalloc (dynobj,
   5349 				 frvfdpic_gotfixup_section (info)->size);
   5350       if (frvfdpic_gotfixup_section (info)->contents == NULL)
   5351 	return false;
   5352     }
   5353 
   5354   if (frvfdpic_pltrel_section (info))
   5355     {
   5356       frvfdpic_pltrel_section (info)->size =
   5357 	gpinfop->g.lzplt / 8
   5358 	* get_elf_backend_data (output_bfd)->s->sizeof_rel;
   5359       if (frvfdpic_pltrel_section (info)->size == 0)
   5360 	frvfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
   5361       else if (frvfdpic_pltrel_section (info)->contents == NULL)
   5362 	{
   5363 	  frvfdpic_pltrel_section (info)->contents =
   5364 	    (bfd_byte *) bfd_zalloc (dynobj,
   5365 				     frvfdpic_pltrel_section (info)->size);
   5366 	  if (frvfdpic_pltrel_section (info)->contents == NULL)
   5367 	    return false;
   5368 	}
   5369     }
   5370 
   5371   /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
   5372      such that there's room for the additional instruction needed to
   5373      call the resolver.  Since _frvfdpic_assign_got_entries didn't
   5374      account for them, our block size is 4 bytes smaller than the real
   5375      block size.  */
   5376   if (frvfdpic_plt_section (info))
   5377     {
   5378       frvfdpic_plt_section (info)->size = gpinfop->g.lzplt
   5379 	+ ((gpinfop->g.lzplt + (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) - 8)
   5380 	   / (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) * 4);
   5381     }
   5382 
   5383   /* Reset it, such that _frvfdpic_assign_plt_entries() can use it to
   5384      actually assign lazy PLT entries addresses.  */
   5385   gpinfop->g.lzplt = 0;
   5386 
   5387   /* Save information that we're going to need to generate GOT and PLT
   5388      entries.  */
   5389   frvfdpic_got_initial_offset (info) = -gpinfop->gothilo.tmin;
   5390 
   5391   if (get_elf_backend_data (output_bfd)->want_got_sym)
   5392     elf_hash_table (info)->hgot->root.u.def.value
   5393       = frvfdpic_got_initial_offset (info);
   5394 
   5395   if (frvfdpic_plt_section (info))
   5396     frvfdpic_plt_initial_offset (info) =
   5397       frvfdpic_plt_section (info)->size;
   5398 
   5399   /* Allocate a ret statement at plt_initial_offset, to be used by
   5400      locally-resolved TLS descriptors.  */
   5401   if (gpinfop->g.tls_ret_refs)
   5402     frvfdpic_plt_section (info)->size += 4;
   5403 
   5404   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_plt_entries,
   5405 		 gpinfop);
   5406 
   5407   /* Allocate the PLT section contents only after
   5408      _frvfdpic_assign_plt_entries has a chance to add the size of the
   5409      non-lazy PLT entries.  */
   5410   if (frvfdpic_plt_section (info))
   5411     {
   5412       if (frvfdpic_plt_section (info)->size == 0)
   5413 	frvfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
   5414       else if (frvfdpic_plt_section (info)->contents == NULL)
   5415 	{
   5416 	  frvfdpic_plt_section (info)->contents =
   5417 	    (bfd_byte *) bfd_zalloc (dynobj,
   5418 				     frvfdpic_plt_section (info)->size);
   5419 	  if (frvfdpic_plt_section (info)->contents == NULL)
   5420 	    return false;
   5421 	}
   5422     }
   5423 
   5424   return true;
   5425 }
   5426 
   5427 /* Set the sizes of the dynamic sections.  */
   5428 
   5429 static bool
   5430 elf32_frvfdpic_late_size_sections (bfd *output_bfd,
   5431 				   struct bfd_link_info *info)
   5432 {
   5433   bfd *dynobj;
   5434   asection *s;
   5435   struct _frvfdpic_dynamic_got_plt_info gpinfo;
   5436 
   5437   dynobj = elf_hash_table (info)->dynobj;
   5438   if (dynobj == NULL)
   5439     return true;
   5440 
   5441   if (elf_hash_table (info)->dynamic_sections_created)
   5442     {
   5443       /* Set the contents of the .interp section to the interpreter.  */
   5444       if (bfd_link_executable (info) && !info->nointerp)
   5445 	{
   5446 	  s = bfd_get_linker_section (dynobj, ".interp");
   5447 	  BFD_ASSERT (s != NULL);
   5448 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   5449 	  s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
   5450 	}
   5451     }
   5452 
   5453   memset (&gpinfo, 0, sizeof (gpinfo));
   5454   gpinfo.g.info = info;
   5455 
   5456   for (;;)
   5457     {
   5458       htab_t relocs = frvfdpic_relocs_info (info);
   5459 
   5460       htab_traverse (relocs, _frvfdpic_resolve_final_relocs_info, &relocs);
   5461 
   5462       if (relocs == frvfdpic_relocs_info (info))
   5463 	break;
   5464     }
   5465 
   5466   htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_count_got_plt_entries,
   5467 		 &gpinfo.g);
   5468 
   5469   /* Allocate space to save the summary information, we're going to
   5470      use it if we're doing relaxations.  */
   5471   frvfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
   5472 
   5473   if (!_frvfdpic_size_got_plt (output_bfd, &gpinfo))
   5474     return false;
   5475 
   5476   return _bfd_elf_add_dynamic_tags (output_bfd, info, true);
   5477 }
   5478 
   5479 static bool
   5480 elf32_frvfdpic_early_size_sections (bfd *output_bfd,
   5481 				    struct bfd_link_info *info)
   5482 {
   5483   if (!bfd_link_relocatable (info)
   5484       && !bfd_elf_stack_segment_size (output_bfd, info,
   5485 				      "__stacksize", DEFAULT_STACK_SIZE))
   5486     return false;
   5487 
   5488   return true;
   5489 }
   5490 
   5491 /* Check whether any of the relocations was optimized away, and
   5492    subtract it from the relocation or fixup count.  */
   5493 static bool
   5494 _frvfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
   5495 				  struct bfd_link_info *info,
   5496 				  bool *changed)
   5497 {
   5498   Elf_Internal_Shdr *symtab_hdr;
   5499   struct elf_link_hash_entry **sym_hashes;
   5500   Elf_Internal_Rela *rel, *erel;
   5501 
   5502   if ((sec->flags & SEC_RELOC) == 0
   5503       || sec->reloc_count == 0)
   5504     return true;
   5505 
   5506   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   5507   sym_hashes = elf_sym_hashes (abfd);
   5508 
   5509   rel = elf_section_data (sec)->relocs;
   5510 
   5511   /* Now examine each relocation.  */
   5512   for (erel = rel + sec->reloc_count; rel < erel; rel++)
   5513     {
   5514       struct elf_link_hash_entry *h;
   5515       unsigned long r_symndx;
   5516       struct frvfdpic_relocs_info *picrel;
   5517       struct _frvfdpic_dynamic_got_info *dinfo;
   5518 
   5519       if (ELF32_R_TYPE (rel->r_info) != R_FRV_32
   5520 	  && ELF32_R_TYPE (rel->r_info) != R_FRV_FUNCDESC)
   5521 	continue;
   5522 
   5523       if (_bfd_elf_section_offset (sec->output_section->owner,
   5524 				   info, sec, rel->r_offset)
   5525 	  != (bfd_vma)-1)
   5526 	continue;
   5527 
   5528       r_symndx = ELF32_R_SYM (rel->r_info);
   5529       if (r_symndx < symtab_hdr->sh_info)
   5530 	h = NULL;
   5531       else
   5532 	{
   5533 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   5534 	  while (h->root.type == bfd_link_hash_indirect
   5535 		 || h->root.type == bfd_link_hash_warning)
   5536 	    h = (struct elf_link_hash_entry *)h->root.u.i.link;
   5537 	}
   5538 
   5539       if (h != NULL)
   5540 	picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
   5541 						  abfd, h,
   5542 						  rel->r_addend, NO_INSERT);
   5543       else
   5544 	picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info (info),
   5545 						 abfd, r_symndx,
   5546 						 rel->r_addend, NO_INSERT);
   5547 
   5548       if (! picrel)
   5549 	return false;
   5550 
   5551       *changed = true;
   5552       dinfo = frvfdpic_dynamic_got_plt_info (info);
   5553 
   5554       _frvfdpic_count_relocs_fixups (picrel, dinfo, true);
   5555       if (ELF32_R_TYPE (rel->r_info) == R_FRV_32)
   5556 	picrel->relocs32--;
   5557       else /* we know (ELF32_R_TYPE (rel->r_info) == R_FRV_FUNCDESC) */
   5558 	picrel->relocsfd--;
   5559       _frvfdpic_count_relocs_fixups (picrel, dinfo, false);
   5560     }
   5561 
   5562   return true;
   5563 }
   5564 
   5565 static bool
   5566 frvfdpic_elf_discard_info (bfd *ibfd,
   5567 			   struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
   5568 			   struct bfd_link_info *info)
   5569 {
   5570   bool changed = false;
   5571   asection *s;
   5572   bfd *obfd = NULL;
   5573 
   5574   /* Account for relaxation of .eh_frame section.  */
   5575   for (s = ibfd->sections; s; s = s->next)
   5576     if (s->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
   5577       {
   5578 	if (!_frvfdpic_check_discarded_relocs (ibfd, s, info, &changed))
   5579 	  return false;
   5580 	obfd = s->output_section->owner;
   5581       }
   5582 
   5583   if (changed)
   5584     {
   5585       struct _frvfdpic_dynamic_got_plt_info gpinfo;
   5586 
   5587       memset (&gpinfo, 0, sizeof (gpinfo));
   5588       memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info),
   5589 	      sizeof (gpinfo.g));
   5590 
   5591       /* Clear GOT and PLT assignments.  */
   5592       htab_traverse (frvfdpic_relocs_info (info),
   5593 		     _frvfdpic_reset_got_plt_entries,
   5594 		     NULL);
   5595 
   5596       if (!_frvfdpic_size_got_plt (obfd, &gpinfo))
   5597 	return false;
   5598     }
   5599 
   5600   return true;
   5601 }
   5602 
   5603 /* Look for opportunities to relax TLS relocations.  We can assume
   5604    we're linking the main executable or a static-tls library, since
   5605    otherwise we wouldn't have got here.  */
   5606 
   5607 static int
   5608 _frvfdpic_relax_got_plt_entries (void **entryp, void *dinfo_)
   5609 {
   5610   struct frvfdpic_relocs_info *entry = *entryp;
   5611   struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
   5612 
   5613   _frvfdpic_relax_tls_entries (entry, dinfo, true);
   5614 
   5615   return 1;
   5616 }
   5617 
   5618 static bool
   5619 elf32_frvfdpic_relax_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
   5620 			      struct bfd_link_info *info, bool *again)
   5621 {
   5622   struct _frvfdpic_dynamic_got_plt_info gpinfo;
   5623 
   5624   if (bfd_link_relocatable (info))
   5625     (*info->callbacks->einfo)
   5626       (_("%P%F: --relax and -r may not be used together\n"));
   5627 
   5628   /* If we return early, we didn't change anything.  */
   5629   *again = false;
   5630 
   5631   /* We'll do our thing when requested to relax the GOT section.  */
   5632   if (sec != frvfdpic_got_section (info))
   5633     return true;
   5634 
   5635   /* We can only relax when linking the main executable or a library
   5636      that can't be dlopened.  */
   5637   if (! bfd_link_executable (info) && ! (info->flags & DF_STATIC_TLS))
   5638     return true;
   5639 
   5640   /* If there isn't a TLS section for this binary, we can't do
   5641      anything about its TLS relocations (it probably doesn't have
   5642      any.  */
   5643   if (elf_hash_table (info)->tls_sec == NULL)
   5644     return true;
   5645 
   5646   memset (&gpinfo, 0, sizeof (gpinfo));
   5647   memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info), sizeof (gpinfo.g));
   5648 
   5649   /* Now look for opportunities to relax, adjusting the GOT usage
   5650      as needed.  */
   5651   htab_traverse (frvfdpic_relocs_info (info),
   5652 		 _frvfdpic_relax_got_plt_entries,
   5653 		 &gpinfo.g);
   5654 
   5655   /* If we changed anything, reset and re-assign GOT and PLT entries.  */
   5656   if (memcmp (frvfdpic_dynamic_got_plt_info (info),
   5657 	      &gpinfo.g, sizeof (gpinfo.g)) != 0)
   5658     {
   5659       /* Clear GOT and PLT assignments.  */
   5660       htab_traverse (frvfdpic_relocs_info (info),
   5661 		     _frvfdpic_reset_got_plt_entries,
   5662 		     NULL);
   5663 
   5664       /* The owner of the TLS section is the output bfd.  There should
   5665 	 be a better way to get to it.  */
   5666       if (!_frvfdpic_size_got_plt (elf_hash_table (info)->tls_sec->owner,
   5667 				   &gpinfo))
   5668 	return false;
   5669 
   5670       /* Repeat until we don't make any further changes.  We could fail to
   5671 	 introduce changes in a round if, for example, the 12-bit range is
   5672 	 full, but we later release some space by getting rid of TLS
   5673 	 descriptors in it.  We have to repeat the whole process because
   5674 	 we might have changed the size of a section processed before this
   5675 	 one.  */
   5676       *again = true;
   5677     }
   5678 
   5679   return true;
   5680 }
   5681 
   5682 /* Fill in code and data in dynamic sections.  */
   5683 
   5684 static bool
   5685 elf32_frv_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   5686 				   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   5687 {
   5688   /* Nothing to be done for non-FDPIC.  */
   5689   return true;
   5690 }
   5691 
   5692 static bool
   5693 elf32_frvfdpic_finish_dynamic_sections (bfd *output_bfd,
   5694 					struct bfd_link_info *info)
   5695 {
   5696   bfd *dynobj;
   5697   asection *sdyn;
   5698 
   5699   dynobj = elf_hash_table (info)->dynobj;
   5700 
   5701   if (frvfdpic_dynamic_got_plt_info (info))
   5702     {
   5703       BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs == 0);
   5704     }
   5705   if (frvfdpic_got_section (info))
   5706     {
   5707       BFD_ASSERT (frvfdpic_gotrel_section (info)->size
   5708 		  == (frvfdpic_gotrel_section (info)->reloc_count
   5709 		      * sizeof (Elf32_External_Rel)));
   5710 
   5711       if (frvfdpic_gotfixup_section (info))
   5712 	{
   5713 	  struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
   5714 	  bfd_vma got_value = hgot->root.u.def.value
   5715 	    + hgot->root.u.def.section->output_section->vma
   5716 	    + hgot->root.u.def.section->output_offset;
   5717 	  struct bfd_link_hash_entry *hend;
   5718 
   5719 	  _frvfdpic_add_rofixup (output_bfd, frvfdpic_gotfixup_section (info),
   5720 				 got_value, 0);
   5721 
   5722 	  if (frvfdpic_gotfixup_section (info)->size
   5723 	      != (frvfdpic_gotfixup_section (info)->reloc_count * 4))
   5724 	    {
   5725 	    error:
   5726 	      info->callbacks->einfo
   5727 		("LINKER BUG: .rofixup section size mismatch\n");
   5728 	      return false;
   5729 	    }
   5730 
   5731 	  hend = bfd_link_hash_lookup (info->hash, "__ROFIXUP_END__",
   5732 				       false, false, true);
   5733 	  if (hend
   5734 	      && (hend->type == bfd_link_hash_defined
   5735 		  || hend->type == bfd_link_hash_defweak)
   5736 	      && hend->u.def.section->output_section != NULL)
   5737 	    {
   5738 	      bfd_vma value =
   5739 		frvfdpic_gotfixup_section (info)->output_section->vma
   5740 		+ frvfdpic_gotfixup_section (info)->output_offset
   5741 		+ frvfdpic_gotfixup_section (info)->size
   5742 		- hend->u.def.section->output_section->vma
   5743 		- hend->u.def.section->output_offset;
   5744 	      BFD_ASSERT (hend->u.def.value == value);
   5745 	      if (hend->u.def.value != value)
   5746 		goto error;
   5747 	    }
   5748 	}
   5749     }
   5750   if (frvfdpic_pltrel_section (info))
   5751     {
   5752       BFD_ASSERT (frvfdpic_pltrel_section (info)->size
   5753 		  == (frvfdpic_pltrel_section (info)->reloc_count
   5754 		      * sizeof (Elf32_External_Rel)));
   5755     }
   5756 
   5757 
   5758   if (elf_hash_table (info)->dynamic_sections_created)
   5759     {
   5760       Elf32_External_Dyn * dyncon;
   5761       Elf32_External_Dyn * dynconend;
   5762 
   5763       sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   5764 
   5765       BFD_ASSERT (sdyn != NULL);
   5766 
   5767       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   5768       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   5769 
   5770       for (; dyncon < dynconend; dyncon++)
   5771 	{
   5772 	  Elf_Internal_Dyn dyn;
   5773 
   5774 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   5775 
   5776 	  switch (dyn.d_tag)
   5777 	    {
   5778 	    default:
   5779 	      break;
   5780 
   5781 	    case DT_PLTGOT:
   5782 	      dyn.d_un.d_ptr = frvfdpic_got_section (info)->output_section->vma
   5783 		+ frvfdpic_got_section (info)->output_offset
   5784 		+ frvfdpic_got_initial_offset (info);
   5785 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   5786 	      break;
   5787 
   5788 	    case DT_JMPREL:
   5789 	      dyn.d_un.d_ptr = frvfdpic_pltrel_section (info)
   5790 		->output_section->vma
   5791 		+ frvfdpic_pltrel_section (info)->output_offset;
   5792 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   5793 	      break;
   5794 
   5795 	    case DT_PLTRELSZ:
   5796 	      dyn.d_un.d_val = frvfdpic_pltrel_section (info)->size;
   5797 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   5798 	      break;
   5799 	    }
   5800 	}
   5801     }
   5802 
   5803   return true;
   5804 }
   5805 
   5806 /* Adjust a symbol defined by a dynamic object and referenced by a
   5807    regular object.  */
   5808 
   5809 static bool
   5810 elf32_frvfdpic_adjust_dynamic_symbol
   5811 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
   5812  struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
   5813 {
   5814   bfd * dynobj;
   5815 
   5816   dynobj = elf_hash_table (info)->dynobj;
   5817 
   5818   /* Make sure we know what is going on here.  */
   5819   BFD_ASSERT (dynobj != NULL
   5820 	      && (h->is_weakalias
   5821 		  || (h->def_dynamic
   5822 		      && h->ref_regular
   5823 		      && !h->def_regular)));
   5824 
   5825   /* If this is a weak symbol, and there is a real definition, the
   5826      processor independent code will have arranged for us to see the
   5827      real definition first, and we can just use the same value.  */
   5828   if (h->is_weakalias)
   5829     {
   5830       struct elf_link_hash_entry *def = weakdef (h);
   5831       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   5832       h->root.u.def.section = def->root.u.def.section;
   5833       h->root.u.def.value = def->root.u.def.value;
   5834       return true;
   5835     }
   5836 
   5837   return true;
   5838 }
   5839 
   5840 /* Perform any actions needed for dynamic symbols.  */
   5841 
   5842 static bool
   5843 elf32_frvfdpic_finish_dynamic_symbol
   5844 (bfd *output_bfd ATTRIBUTE_UNUSED,
   5845  struct bfd_link_info *info ATTRIBUTE_UNUSED,
   5846  struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   5847  Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
   5848 {
   5849   return true;
   5850 }
   5851 
   5852 /* Decide whether to attempt to turn absptr or lsda encodings in
   5853    shared libraries into pcrel within the given input section.  */
   5854 
   5855 static bool
   5856 frvfdpic_elf_use_relative_eh_frame
   5857 (bfd *input_bfd ATTRIBUTE_UNUSED,
   5858  struct bfd_link_info *info ATTRIBUTE_UNUSED,
   5859  asection *eh_frame_section ATTRIBUTE_UNUSED)
   5860 {
   5861   /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
   5862   return false;
   5863 }
   5864 
   5865 /* Adjust the contents of an eh_frame_hdr section before they're output.  */
   5866 
   5867 static bfd_byte
   5868 frvfdpic_elf_encode_eh_address (bfd *abfd,
   5869 				struct bfd_link_info *info,
   5870 				asection *osec, bfd_vma offset,
   5871 				asection *loc_sec, bfd_vma loc_offset,
   5872 				bfd_vma *encoded)
   5873 {
   5874   struct elf_link_hash_entry *h;
   5875 
   5876   h = elf_hash_table (info)->hgot;
   5877   BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
   5878 
   5879   if (! h || (_frvfdpic_osec_to_segment (abfd, osec)
   5880 	      == _frvfdpic_osec_to_segment (abfd, loc_sec->output_section)))
   5881     return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
   5882 				       loc_sec, loc_offset, encoded);
   5883 
   5884   BFD_ASSERT (_frvfdpic_osec_to_segment (abfd, osec)
   5885 	      == (_frvfdpic_osec_to_segment
   5886 		  (abfd, h->root.u.def.section->output_section)));
   5887 
   5888   *encoded = osec->vma + offset
   5889     - (h->root.u.def.value
   5890        + h->root.u.def.section->output_section->vma
   5891        + h->root.u.def.section->output_offset);
   5892 
   5893   return DW_EH_PE_datarel | DW_EH_PE_sdata4;
   5894 }
   5895 
   5896 /* Look through the relocs for a section during the first phase.
   5897 
   5898    Besides handling virtual table relocs for gc, we have to deal with
   5899    all sorts of PIC-related relocations.  We describe below the
   5900    general plan on how to handle such relocations, even though we only
   5901    collect information at this point, storing them in hash tables for
   5902    perusal of later passes.
   5903 
   5904    32 relocations are propagated to the linker output when creating
   5905    position-independent output.  LO16 and HI16 relocations are not
   5906    supposed to be encountered in this case.
   5907 
   5908    LABEL16 should always be resolvable by the linker, since it's only
   5909    used by branches.
   5910 
   5911    LABEL24, on the other hand, is used by calls.  If it turns out that
   5912    the target of a call is a dynamic symbol, a PLT entry must be
   5913    created for it, which triggers the creation of a private function
   5914    descriptor and, unless lazy binding is disabled, a lazy PLT entry.
   5915 
   5916    GPREL relocations require the referenced symbol to be in the same
   5917    segment as _gp, but this can only be checked later.
   5918 
   5919    All GOT, GOTOFF and FUNCDESC relocations require a .got section to
   5920    exist.  LABEL24 might as well, since it may require a PLT entry,
   5921    that will require a got.
   5922 
   5923    Non-FUNCDESC GOT relocations require a GOT entry to be created
   5924    regardless of whether the symbol is dynamic.  However, since a
   5925    global symbol that turns out to not be exported may have the same
   5926    address of a non-dynamic symbol, we don't assign GOT entries at
   5927    this point, such that we can share them in this case.  A relocation
   5928    for the GOT entry always has to be created, be it to offset a
   5929    private symbol by the section load address, be it to get the symbol
   5930    resolved dynamically.
   5931 
   5932    FUNCDESC GOT relocations require a GOT entry to be created, and
   5933    handled as if a FUNCDESC relocation was applied to the GOT entry in
   5934    an object file.
   5935 
   5936    FUNCDESC relocations referencing a symbol that turns out to NOT be
   5937    dynamic cause a private function descriptor to be created.  The
   5938    FUNCDESC relocation then decays to a 32 relocation that points at
   5939    the private descriptor.  If the symbol is dynamic, the FUNCDESC
   5940    relocation is propagated to the linker output, such that the
   5941    dynamic linker creates the canonical descriptor, pointing to the
   5942    dynamically-resolved definition of the function.
   5943 
   5944    Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
   5945    symbols that are assigned to the same segment as the GOT, but we
   5946    can only check this later, after we know the complete set of
   5947    symbols defined and/or exported.
   5948 
   5949    FUNCDESC GOTOFF relocations require a function descriptor to be
   5950    created and, unless lazy binding is disabled or the symbol is not
   5951    dynamic, a lazy PLT entry.  Since we can't tell at this point
   5952    whether a symbol is going to be dynamic, we have to decide later
   5953    whether to create a lazy PLT entry or bind the descriptor directly
   5954    to the private function.
   5955 
   5956    FUNCDESC_VALUE relocations are not supposed to be present in object
   5957    files, but they may very well be simply propagated to the linker
   5958    output, since they have no side effect.
   5959 
   5960 
   5961    A function descriptor always requires a FUNCDESC_VALUE relocation.
   5962    Whether it's in .plt.rel or not depends on whether lazy binding is
   5963    enabled and on whether the referenced symbol is dynamic.
   5964 
   5965    The existence of a lazy PLT requires the resolverStub lazy PLT
   5966    entry to be present.
   5967 
   5968 
   5969    As for assignment of GOT, PLT and lazy PLT entries, and private
   5970    descriptors, we might do them all sequentially, but we can do
   5971    better than that.  For example, we can place GOT entries and
   5972    private function descriptors referenced using 12-bit operands
   5973    closer to the PIC register value, such that these relocations don't
   5974    overflow.  Those that are only referenced with LO16 relocations
   5975    could come next, but we may as well place PLT-required function
   5976    descriptors in the 12-bit range to make them shorter.  Symbols
   5977    referenced with LO16/HI16 may come next, but we may place
   5978    additional function descriptors in the 16-bit range if we can
   5979    reliably tell that we've already placed entries that are ever
   5980    referenced with only LO16.  PLT entries are therefore generated as
   5981    small as possible, while not introducing relocation overflows in
   5982    GOT or FUNCDESC_GOTOFF relocations.  Lazy PLT entries could be
   5983    generated before or after PLT entries, but not intermingled with
   5984    them, such that we can have more lazy PLT entries in range for a
   5985    branch to the resolverStub.  The resolverStub should be emitted at
   5986    the most distant location from the first lazy PLT entry such that
   5987    it's still in range for a branch, or closer, if there isn't a need
   5988    for so many lazy PLT entries.  Additional lazy PLT entries may be
   5989    emitted after the resolverStub, as long as branches are still in
   5990    range.  If the branch goes out of range, longer lazy PLT entries
   5991    are emitted.
   5992 
   5993    We could further optimize PLT and lazy PLT entries by giving them
   5994    priority in assignment to closer-to-gr17 locations depending on the
   5995    number of occurrences of references to them (assuming a function
   5996    that's called more often is more important for performance, so its
   5997    PLT entry should be faster), or taking hints from the compiler.
   5998    Given infinite time and money... :-)  */
   5999 
   6000 static bool
   6001 elf32_frv_check_relocs (bfd *abfd,
   6002 			struct bfd_link_info *info,
   6003 			asection *sec,
   6004 			const Elf_Internal_Rela *relocs)
   6005 {
   6006   Elf_Internal_Shdr *symtab_hdr;
   6007   struct elf_link_hash_entry **sym_hashes;
   6008   const Elf_Internal_Rela *rel;
   6009   const Elf_Internal_Rela *rel_end;
   6010   bfd *dynobj;
   6011   struct frvfdpic_relocs_info *picrel;
   6012 
   6013   if (bfd_link_relocatable (info))
   6014     return true;
   6015 
   6016   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   6017   sym_hashes = elf_sym_hashes (abfd);
   6018 
   6019   dynobj = elf_hash_table (info)->dynobj;
   6020   rel_end = relocs + sec->reloc_count;
   6021   for (rel = relocs; rel < rel_end; rel++)
   6022     {
   6023       struct elf_link_hash_entry *h;
   6024       unsigned long r_symndx;
   6025 
   6026       r_symndx = ELF32_R_SYM (rel->r_info);
   6027       if (r_symndx < symtab_hdr->sh_info)
   6028 	h = NULL;
   6029       else
   6030 	{
   6031 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6032 	  while (h->root.type == bfd_link_hash_indirect
   6033 		 || h->root.type == bfd_link_hash_warning)
   6034 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6035 	}
   6036 
   6037       switch (ELF32_R_TYPE (rel->r_info))
   6038 	{
   6039 	case R_FRV_GETTLSOFF:
   6040 	case R_FRV_TLSDESC_VALUE:
   6041 	case R_FRV_GOTTLSDESC12:
   6042 	case R_FRV_GOTTLSDESCHI:
   6043 	case R_FRV_GOTTLSDESCLO:
   6044 	case R_FRV_GOTTLSOFF12:
   6045 	case R_FRV_GOTTLSOFFHI:
   6046 	case R_FRV_GOTTLSOFFLO:
   6047 	case R_FRV_TLSOFF:
   6048 	case R_FRV_GOT12:
   6049 	case R_FRV_GOTHI:
   6050 	case R_FRV_GOTLO:
   6051 	case R_FRV_FUNCDESC_GOT12:
   6052 	case R_FRV_FUNCDESC_GOTHI:
   6053 	case R_FRV_FUNCDESC_GOTLO:
   6054 	case R_FRV_GOTOFF12:
   6055 	case R_FRV_GOTOFFHI:
   6056 	case R_FRV_GOTOFFLO:
   6057 	case R_FRV_FUNCDESC_GOTOFF12:
   6058 	case R_FRV_FUNCDESC_GOTOFFHI:
   6059 	case R_FRV_FUNCDESC_GOTOFFLO:
   6060 	case R_FRV_FUNCDESC:
   6061 	case R_FRV_FUNCDESC_VALUE:
   6062 	case R_FRV_TLSMOFF12:
   6063 	case R_FRV_TLSMOFFHI:
   6064 	case R_FRV_TLSMOFFLO:
   6065 	case R_FRV_TLSMOFF:
   6066 	  if (! IS_FDPIC (abfd))
   6067 	    goto bad_reloc;
   6068 	  /* Fall through.  */
   6069 	case R_FRV_GPREL12:
   6070 	case R_FRV_GPRELU12:
   6071 	case R_FRV_GPRELHI:
   6072 	case R_FRV_GPRELLO:
   6073 	case R_FRV_LABEL24:
   6074 	case R_FRV_32:
   6075 	  if (! dynobj)
   6076 	    {
   6077 	      elf_hash_table (info)->dynobj = dynobj = abfd;
   6078 	      if (! _frv_create_got_section (abfd, info))
   6079 		return false;
   6080 	    }
   6081 	  if (! IS_FDPIC (abfd))
   6082 	    {
   6083 	      picrel = NULL;
   6084 	      break;
   6085 	    }
   6086 	  if (h != NULL)
   6087 	    {
   6088 	      if (h->dynindx == -1)
   6089 		switch (ELF_ST_VISIBILITY (h->other))
   6090 		  {
   6091 		  case STV_INTERNAL:
   6092 		  case STV_HIDDEN:
   6093 		    break;
   6094 		  default:
   6095 		    bfd_elf_link_record_dynamic_symbol (info, h);
   6096 		    break;
   6097 		  }
   6098 	      picrel
   6099 		= frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
   6100 						   abfd, h,
   6101 						   rel->r_addend, INSERT);
   6102 	    }
   6103 	  else
   6104 	    picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
   6105 						     (info), abfd, r_symndx,
   6106 						     rel->r_addend, INSERT);
   6107 	  if (! picrel)
   6108 	    return false;
   6109 	  break;
   6110 
   6111 	default:
   6112 	  picrel = NULL;
   6113 	  break;
   6114 	}
   6115 
   6116       switch (ELF32_R_TYPE (rel->r_info))
   6117 	{
   6118 	case R_FRV_LABEL24:
   6119 	  if (IS_FDPIC (abfd))
   6120 	    picrel->call = 1;
   6121 	  break;
   6122 
   6123 	case R_FRV_FUNCDESC_VALUE:
   6124 	  picrel->relocsfdv++;
   6125 	  if (bfd_section_flags (sec) & SEC_ALLOC)
   6126 	    picrel->relocs32--;
   6127 	  /* Fall through.  */
   6128 
   6129 	case R_FRV_32:
   6130 	  if (! IS_FDPIC (abfd))
   6131 	    break;
   6132 
   6133 	  picrel->sym = 1;
   6134 	  if (bfd_section_flags (sec) & SEC_ALLOC)
   6135 	    picrel->relocs32++;
   6136 	  break;
   6137 
   6138 	case R_FRV_GOT12:
   6139 	  picrel->got12 = 1;
   6140 	  break;
   6141 
   6142 	case R_FRV_GOTHI:
   6143 	case R_FRV_GOTLO:
   6144 	  picrel->gothilo = 1;
   6145 	  break;
   6146 
   6147 	case R_FRV_FUNCDESC_GOT12:
   6148 	  picrel->fdgot12 = 1;
   6149 	  break;
   6150 
   6151 	case R_FRV_FUNCDESC_GOTHI:
   6152 	case R_FRV_FUNCDESC_GOTLO:
   6153 	  picrel->fdgothilo = 1;
   6154 	  break;
   6155 
   6156 	case R_FRV_GOTOFF12:
   6157 	case R_FRV_GOTOFFHI:
   6158 	case R_FRV_GOTOFFLO:
   6159 	  picrel->gotoff = 1;
   6160 	  break;
   6161 
   6162 	case R_FRV_FUNCDESC_GOTOFF12:
   6163 	  picrel->fdgoff12 = 1;
   6164 	  break;
   6165 
   6166 	case R_FRV_FUNCDESC_GOTOFFHI:
   6167 	case R_FRV_FUNCDESC_GOTOFFLO:
   6168 	  picrel->fdgoffhilo = 1;
   6169 	  break;
   6170 
   6171 	case R_FRV_FUNCDESC:
   6172 	  picrel->fd = 1;
   6173 	  picrel->relocsfd++;
   6174 	  break;
   6175 
   6176 	case R_FRV_GETTLSOFF:
   6177 	  picrel->tlsplt = 1;
   6178 	  break;
   6179 
   6180 	case R_FRV_TLSDESC_VALUE:
   6181 	  picrel->relocstlsd++;
   6182 	  goto bad_reloc;
   6183 
   6184 	case R_FRV_GOTTLSDESC12:
   6185 	  picrel->tlsdesc12 = 1;
   6186 	  break;
   6187 
   6188 	case R_FRV_GOTTLSDESCHI:
   6189 	case R_FRV_GOTTLSDESCLO:
   6190 	  picrel->tlsdeschilo = 1;
   6191 	  break;
   6192 
   6193 	case R_FRV_TLSMOFF12:
   6194 	case R_FRV_TLSMOFFHI:
   6195 	case R_FRV_TLSMOFFLO:
   6196 	case R_FRV_TLSMOFF:
   6197 	  break;
   6198 
   6199 	case R_FRV_GOTTLSOFF12:
   6200 	  picrel->tlsoff12 = 1;
   6201 	  info->flags |= DF_STATIC_TLS;
   6202 	  break;
   6203 
   6204 	case R_FRV_GOTTLSOFFHI:
   6205 	case R_FRV_GOTTLSOFFLO:
   6206 	  picrel->tlsoffhilo = 1;
   6207 	  info->flags |= DF_STATIC_TLS;
   6208 	  break;
   6209 
   6210 	case R_FRV_TLSOFF:
   6211 	  picrel->relocstlsoff++;
   6212 	  info->flags |= DF_STATIC_TLS;
   6213 	  goto bad_reloc;
   6214 
   6215 	/* This relocation describes the C++ object vtable hierarchy.
   6216 	   Reconstruct it for later use during GC.  */
   6217 	case R_FRV_GNU_VTINHERIT:
   6218 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   6219 	    return false;
   6220 	  break;
   6221 
   6222 	/* This relocation describes which C++ vtable entries are actually
   6223 	   used.  Record for later use during GC.  */
   6224 	case R_FRV_GNU_VTENTRY:
   6225 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   6226 	    return false;
   6227 	  break;
   6228 
   6229 	case R_FRV_LABEL16:
   6230 	case R_FRV_LO16:
   6231 	case R_FRV_HI16:
   6232 	case R_FRV_GPREL12:
   6233 	case R_FRV_GPRELU12:
   6234 	case R_FRV_GPREL32:
   6235 	case R_FRV_GPRELHI:
   6236 	case R_FRV_GPRELLO:
   6237 	case R_FRV_TLSDESC_RELAX:
   6238 	case R_FRV_GETTLSOFF_RELAX:
   6239 	case R_FRV_TLSOFF_RELAX:
   6240 	  break;
   6241 
   6242 	default:
   6243 	bad_reloc:
   6244 	  /* xgettext:c-format */
   6245 	  _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
   6246 			      abfd, (unsigned int) ELF32_R_TYPE (rel->r_info));
   6247 	  return false;
   6248 	}
   6249     }
   6250 
   6251   return true;
   6252 }
   6253 
   6254 
   6255 /* Return the machine subcode from the ELF e_flags header.  */
   6257 
   6258 static int
   6259 elf32_frv_machine (bfd *abfd)
   6260 {
   6261   switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
   6262     {
   6263     default:		    break;
   6264     case EF_FRV_CPU_FR550:  return bfd_mach_fr550;
   6265     case EF_FRV_CPU_FR500:  return bfd_mach_fr500;
   6266     case EF_FRV_CPU_FR450:  return bfd_mach_fr450;
   6267     case EF_FRV_CPU_FR405:  return bfd_mach_fr400;
   6268     case EF_FRV_CPU_FR400:  return bfd_mach_fr400;
   6269     case EF_FRV_CPU_FR300:  return bfd_mach_fr300;
   6270     case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
   6271     case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
   6272     }
   6273 
   6274   return bfd_mach_frv;
   6275 }
   6276 
   6277 /* Set the right machine number for a FRV ELF file.  */
   6278 
   6279 static bool
   6280 elf32_frv_object_p (bfd *abfd)
   6281 {
   6282   bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
   6283   return (((elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC) != 0)
   6284 	  == (IS_FDPIC (abfd)));
   6285 }
   6286 
   6287 /* Function to set the ELF flag bits.  */
   6289 
   6290 static bool
   6291 frv_elf_set_private_flags (bfd *abfd, flagword flags)
   6292 {
   6293   elf_elfheader (abfd)->e_flags = flags;
   6294   elf_flags_init (abfd) = true;
   6295   return true;
   6296 }
   6297 
   6298 /* Return true if the architecture described by elf header flag
   6299    EXTENSION is an extension of the architecture described by BASE.  */
   6300 
   6301 static bool
   6302 frv_elf_arch_extension_p (flagword base, flagword extension)
   6303 {
   6304   if (base == extension)
   6305     return true;
   6306 
   6307   /* CPU_GENERIC code can be merged with code for a specific
   6308      architecture, in which case the result is marked as being
   6309      for the specific architecture.  Everything is therefore
   6310      an extension of CPU_GENERIC.  */
   6311   if (base == EF_FRV_CPU_GENERIC)
   6312     return true;
   6313 
   6314   if (extension == EF_FRV_CPU_FR450)
   6315     if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
   6316       return true;
   6317 
   6318   if (extension == EF_FRV_CPU_FR405)
   6319     if (base == EF_FRV_CPU_FR400)
   6320       return true;
   6321 
   6322   return false;
   6323 }
   6324 
   6325 /* Merge backend specific data from an object file to the output
   6326    object file when linking.  */
   6327 
   6328 static bool
   6329 frv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   6330 {
   6331   bfd *obfd = info->output_bfd;
   6332   flagword old_flags, old_partial;
   6333   flagword new_flags, new_partial;
   6334   bool error = false;
   6335   char new_opt[80];
   6336   char old_opt[80];
   6337 
   6338   /* FIXME: What should be checked when linking shared libraries?  */
   6339   if ((ibfd->flags & DYNAMIC) != 0)
   6340     return true;
   6341 
   6342   new_opt[0] = old_opt[0] = '\0';
   6343   new_flags = elf_elfheader (ibfd)->e_flags;
   6344   old_flags = elf_elfheader (obfd)->e_flags;
   6345 
   6346   if (new_flags & EF_FRV_FDPIC)
   6347     new_flags &= ~EF_FRV_PIC;
   6348 
   6349 #ifdef DEBUG
   6350   _bfd_error_handler
   6351     ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %s",
   6352      old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
   6353      bfd_get_filename (ibfd));
   6354 #endif
   6355 
   6356   if (!elf_flags_init (obfd))			/* First call, no flags set.  */
   6357     {
   6358       elf_flags_init (obfd) = true;
   6359       old_flags = new_flags;
   6360     }
   6361 
   6362   else if (new_flags == old_flags)		/* Compatible flags are ok.  */
   6363     ;
   6364 
   6365   else						/* Possibly incompatible flags.  */
   6366     {
   6367       /* Warn if different # of gprs are used.  Note, 0 means nothing is
   6368 	 said about the size of gprs.  */
   6369       new_partial = (new_flags & EF_FRV_GPR_MASK);
   6370       old_partial = (old_flags & EF_FRV_GPR_MASK);
   6371       if (new_partial == old_partial)
   6372 	;
   6373 
   6374       else if (new_partial == 0)
   6375 	;
   6376 
   6377       else if (old_partial == 0)
   6378 	old_flags |= new_partial;
   6379 
   6380       else
   6381 	{
   6382 	  switch (new_partial)
   6383 	    {
   6384 	    default:		strcat (new_opt, " -mgpr-??"); break;
   6385 	    case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
   6386 	    case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
   6387 	    }
   6388 
   6389 	  switch (old_partial)
   6390 	    {
   6391 	    default:		strcat (old_opt, " -mgpr-??"); break;
   6392 	    case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
   6393 	    case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
   6394 	    }
   6395 	}
   6396 
   6397       /* Warn if different # of fprs are used.  Note, 0 means nothing is
   6398 	 said about the size of fprs.  */
   6399       new_partial = (new_flags & EF_FRV_FPR_MASK);
   6400       old_partial = (old_flags & EF_FRV_FPR_MASK);
   6401       if (new_partial == old_partial)
   6402 	;
   6403 
   6404       else if (new_partial == 0)
   6405 	;
   6406 
   6407       else if (old_partial == 0)
   6408 	old_flags |= new_partial;
   6409 
   6410       else
   6411 	{
   6412 	  switch (new_partial)
   6413 	    {
   6414 	    default:		  strcat (new_opt, " -mfpr-?");      break;
   6415 	    case EF_FRV_FPR_32:   strcat (new_opt, " -mfpr-32");     break;
   6416 	    case EF_FRV_FPR_64:   strcat (new_opt, " -mfpr-64");     break;
   6417 	    case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
   6418 	    }
   6419 
   6420 	  switch (old_partial)
   6421 	    {
   6422 	    default:		  strcat (old_opt, " -mfpr-?");      break;
   6423 	    case EF_FRV_FPR_32:   strcat (old_opt, " -mfpr-32");     break;
   6424 	    case EF_FRV_FPR_64:   strcat (old_opt, " -mfpr-64");     break;
   6425 	    case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
   6426 	    }
   6427 	}
   6428 
   6429       /* Warn if different dword support was used.  Note, 0 means nothing is
   6430 	 said about the dword support.  */
   6431       new_partial = (new_flags & EF_FRV_DWORD_MASK);
   6432       old_partial = (old_flags & EF_FRV_DWORD_MASK);
   6433       if (new_partial == old_partial)
   6434 	;
   6435 
   6436       else if (new_partial == 0)
   6437 	;
   6438 
   6439       else if (old_partial == 0)
   6440 	old_flags |= new_partial;
   6441 
   6442       else
   6443 	{
   6444 	  switch (new_partial)
   6445 	    {
   6446 	    default:		   strcat (new_opt, " -mdword-?");  break;
   6447 	    case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword");    break;
   6448 	    case EF_FRV_DWORD_NO:  strcat (new_opt, " -mno-dword"); break;
   6449 	    }
   6450 
   6451 	  switch (old_partial)
   6452 	    {
   6453 	    default:		   strcat (old_opt, " -mdword-?");  break;
   6454 	    case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword");    break;
   6455 	    case EF_FRV_DWORD_NO:  strcat (old_opt, " -mno-dword"); break;
   6456 	    }
   6457 	}
   6458 
   6459       /* Or in flags that accumulate (ie, if one module uses it, mark that the
   6460 	 feature is used.  */
   6461       old_flags |= new_flags & (EF_FRV_DOUBLE
   6462 				| EF_FRV_MEDIA
   6463 				| EF_FRV_MULADD
   6464 				| EF_FRV_NON_PIC_RELOCS);
   6465 
   6466       /* If any module was compiled without -G0, clear the G0 bit.  */
   6467       old_flags = ((old_flags & ~ EF_FRV_G0)
   6468 		   | (old_flags & new_flags & EF_FRV_G0));
   6469 
   6470       /* If any module was compiled without -mnopack, clear the mnopack bit.  */
   6471       old_flags = ((old_flags & ~ EF_FRV_NOPACK)
   6472 		   | (old_flags & new_flags & EF_FRV_NOPACK));
   6473 
   6474       /* We don't have to do anything if the pic flags are the same, or the new
   6475 	 module(s) were compiled with -mlibrary-pic.  */
   6476       new_partial = (new_flags & EF_FRV_PIC_FLAGS);
   6477       old_partial = (old_flags & EF_FRV_PIC_FLAGS);
   6478       if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
   6479 	;
   6480 
   6481       /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
   6482 	 flags if any from the new module.  */
   6483       else if ((old_partial & EF_FRV_LIBPIC) != 0)
   6484 	old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
   6485 
   6486       /* If we have mixtures of -fpic and -fPIC, or in both bits.  */
   6487       else if (new_partial != 0 && old_partial != 0)
   6488 	old_flags |= new_partial;
   6489 
   6490       /* One module was compiled for pic and the other was not, see if we have
   6491 	 had any relocations that are not pic-safe.  */
   6492       else
   6493 	{
   6494 	  if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
   6495 	    old_flags |= new_partial;
   6496 	  else
   6497 	    {
   6498 	      old_flags &= ~ EF_FRV_PIC_FLAGS;
   6499 #ifndef FRV_NO_PIC_ERROR
   6500 	      error = true;
   6501 	      _bfd_error_handler
   6502 		/* xgettext:c-format */
   6503 		(_("%pB: compiled with %s and linked with modules"
   6504 		   " that use non-pic relocations"),
   6505 		 ibfd, (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
   6506 #endif
   6507 	    }
   6508 	}
   6509 
   6510       /* Warn if different cpu is used (allow a specific cpu to override
   6511 	 the generic cpu).  */
   6512       new_partial = (new_flags & EF_FRV_CPU_MASK);
   6513       old_partial = (old_flags & EF_FRV_CPU_MASK);
   6514       if (frv_elf_arch_extension_p (new_partial, old_partial))
   6515 	;
   6516 
   6517       else if (frv_elf_arch_extension_p (old_partial, new_partial))
   6518 	old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
   6519 
   6520       else
   6521 	{
   6522 	  switch (new_partial)
   6523 	    {
   6524 	    default:		     strcat (new_opt, " -mcpu=?");      break;
   6525 	    case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv");    break;
   6526 	    case EF_FRV_CPU_SIMPLE:  strcat (new_opt, " -mcpu=simple"); break;
   6527 	    case EF_FRV_CPU_FR550:   strcat (new_opt, " -mcpu=fr550");  break;
   6528 	    case EF_FRV_CPU_FR500:   strcat (new_opt, " -mcpu=fr500");  break;
   6529 	    case EF_FRV_CPU_FR450:   strcat (new_opt, " -mcpu=fr450");  break;
   6530 	    case EF_FRV_CPU_FR405:   strcat (new_opt, " -mcpu=fr405");  break;
   6531 	    case EF_FRV_CPU_FR400:   strcat (new_opt, " -mcpu=fr400");  break;
   6532 	    case EF_FRV_CPU_FR300:   strcat (new_opt, " -mcpu=fr300");  break;
   6533 	    case EF_FRV_CPU_TOMCAT:  strcat (new_opt, " -mcpu=tomcat"); break;
   6534 	    }
   6535 
   6536 	  switch (old_partial)
   6537 	    {
   6538 	    default:		     strcat (old_opt, " -mcpu=?");      break;
   6539 	    case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv");    break;
   6540 	    case EF_FRV_CPU_SIMPLE:  strcat (old_opt, " -mcpu=simple"); break;
   6541 	    case EF_FRV_CPU_FR550:   strcat (old_opt, " -mcpu=fr550");  break;
   6542 	    case EF_FRV_CPU_FR500:   strcat (old_opt, " -mcpu=fr500");  break;
   6543 	    case EF_FRV_CPU_FR450:   strcat (old_opt, " -mcpu=fr450");  break;
   6544 	    case EF_FRV_CPU_FR405:   strcat (old_opt, " -mcpu=fr405");  break;
   6545 	    case EF_FRV_CPU_FR400:   strcat (old_opt, " -mcpu=fr400");  break;
   6546 	    case EF_FRV_CPU_FR300:   strcat (old_opt, " -mcpu=fr300");  break;
   6547 	    case EF_FRV_CPU_TOMCAT:  strcat (old_opt, " -mcpu=tomcat"); break;
   6548 	    }
   6549 	}
   6550 
   6551       /* Print out any mismatches from above.  */
   6552       if (new_opt[0])
   6553 	{
   6554 	  error = true;
   6555 	  _bfd_error_handler
   6556 	    /* xgettext:c-format */
   6557 	    (_("%pB: compiled with %s and linked with modules compiled with %s"),
   6558 	     ibfd, new_opt, old_opt);
   6559 	}
   6560 
   6561       /* Warn about any other mismatches */
   6562       new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
   6563       old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
   6564       if (new_partial != old_partial)
   6565 	{
   6566 	  old_flags |= new_partial;
   6567 	  error = true;
   6568 	  _bfd_error_handler
   6569 	    /* xgettext:c-format */
   6570 	    (_("%pB: uses different unknown e_flags (%#x) fields"
   6571 	       " than previous modules (%#x)"),
   6572 	     ibfd, new_partial, old_partial);
   6573 	}
   6574     }
   6575 
   6576   /* If the cpu is -mcpu=simple, then set the -mnopack bit.  */
   6577   if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
   6578     old_flags |= EF_FRV_NOPACK;
   6579 
   6580   /* Update the old flags now with changes made above.  */
   6581   old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
   6582   elf_elfheader (obfd)->e_flags = old_flags;
   6583   if (old_partial != (old_flags & EF_FRV_CPU_MASK))
   6584     bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
   6585 
   6586   if (((new_flags & EF_FRV_FDPIC) == 0)
   6587       != (! IS_FDPIC (ibfd)))
   6588     {
   6589       error = true;
   6590       if (IS_FDPIC (obfd))
   6591 	_bfd_error_handler
   6592 	  (_("%pB: cannot link non-fdpic object file into fdpic executable"),
   6593 	   ibfd);
   6594       else
   6595 	_bfd_error_handler
   6596 	  (_("%pB: cannot link fdpic object file into non-fdpic executable"),
   6597 	   ibfd);
   6598     }
   6599 
   6600   if (error)
   6601     bfd_set_error (bfd_error_bad_value);
   6602 
   6603   return !error;
   6604 }
   6605 
   6606 
   6607 static bool
   6609 frv_elf_print_private_bfd_data (bfd *abfd, void * ptr)
   6610 {
   6611   FILE *file = (FILE *) ptr;
   6612   flagword flags;
   6613 
   6614   BFD_ASSERT (abfd != NULL && ptr != NULL);
   6615 
   6616   /* Print normal ELF private data.  */
   6617   _bfd_elf_print_private_bfd_data (abfd, ptr);
   6618 
   6619   flags = elf_elfheader (abfd)->e_flags;
   6620   fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
   6621 
   6622   switch (flags & EF_FRV_CPU_MASK)
   6623     {
   6624     default:							break;
   6625     case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple");	break;
   6626     case EF_FRV_CPU_FR550:  fprintf (file, " -mcpu=fr550");	break;
   6627     case EF_FRV_CPU_FR500:  fprintf (file, " -mcpu=fr500");	break;
   6628     case EF_FRV_CPU_FR450:  fprintf (file, " -mcpu=fr450");	break;
   6629     case EF_FRV_CPU_FR405:  fprintf (file, " -mcpu=fr405");	break;
   6630     case EF_FRV_CPU_FR400:  fprintf (file, " -mcpu=fr400");	break;
   6631     case EF_FRV_CPU_FR300:  fprintf (file, " -mcpu=fr300");	break;
   6632     case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat");	break;
   6633     }
   6634 
   6635   switch (flags & EF_FRV_GPR_MASK)
   6636     {
   6637     default:							break;
   6638     case EF_FRV_GPR_32: fprintf (file, " -mgpr-32");		break;
   6639     case EF_FRV_GPR_64: fprintf (file, " -mgpr-64");		break;
   6640     }
   6641 
   6642   switch (flags & EF_FRV_FPR_MASK)
   6643     {
   6644     default:							break;
   6645     case EF_FRV_FPR_32:   fprintf (file, " -mfpr-32");		break;
   6646     case EF_FRV_FPR_64:   fprintf (file, " -mfpr-64");		break;
   6647     case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float");	break;
   6648     }
   6649 
   6650   switch (flags & EF_FRV_DWORD_MASK)
   6651     {
   6652     default:							break;
   6653     case EF_FRV_DWORD_YES: fprintf (file, " -mdword");		break;
   6654     case EF_FRV_DWORD_NO:  fprintf (file, " -mno-dword");	break;
   6655     }
   6656 
   6657   if (flags & EF_FRV_DOUBLE)
   6658     fprintf (file, " -mdouble");
   6659 
   6660   if (flags & EF_FRV_MEDIA)
   6661     fprintf (file, " -mmedia");
   6662 
   6663   if (flags & EF_FRV_MULADD)
   6664     fprintf (file, " -mmuladd");
   6665 
   6666   if (flags & EF_FRV_PIC)
   6667     fprintf (file, " -fpic");
   6668 
   6669   if (flags & EF_FRV_BIGPIC)
   6670     fprintf (file, " -fPIC");
   6671 
   6672   if (flags & EF_FRV_LIBPIC)
   6673     fprintf (file, " -mlibrary-pic");
   6674 
   6675   if (flags & EF_FRV_FDPIC)
   6676     fprintf (file, " -mfdpic");
   6677 
   6678   if (flags & EF_FRV_NON_PIC_RELOCS)
   6679     fprintf (file, " non-pic relocations");
   6680 
   6681   if (flags & EF_FRV_G0)
   6682     fprintf (file, " -G0");
   6683 
   6684   fputc ('\n', file);
   6685   return true;
   6686 }
   6687 
   6688 
   6689 /* Support for core dump NOTE sections.  */
   6691 
   6692 static bool
   6693 elf32_frv_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   6694 {
   6695   int offset;
   6696   unsigned int raw_size;
   6697 
   6698   switch (note->descsz)
   6699     {
   6700       default:
   6701 	return false;
   6702 
   6703       /* The Linux/FRV elf_prstatus struct is 268 bytes long.  The other
   6704 	 hardcoded offsets and sizes listed below (and contained within
   6705 	 this lexical block) refer to fields in the target's elf_prstatus
   6706 	 struct.  */
   6707       case 268:
   6708 	/* `pr_cursig' is at offset 12.  */
   6709 	elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   6710 
   6711 	/* `pr_pid' is at offset 24.  */
   6712 	elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
   6713 
   6714 	/* `pr_reg' is at offset 72.  */
   6715 	offset = 72;
   6716 
   6717 	/* Most grok_prstatus implementations set `raw_size' to the size
   6718 	   of the pr_reg field.  For Linux/FRV, we set `raw_size' to be
   6719 	   the size of `pr_reg' plus the size of `pr_exec_fdpic_loadmap'
   6720 	   and `pr_interp_fdpic_loadmap', both of which (by design)
   6721 	   immediately follow `pr_reg'.  This will allow these fields to
   6722 	   be viewed by GDB as registers.
   6723 
   6724 	   `pr_reg' is 184 bytes long.  `pr_exec_fdpic_loadmap' and
   6725 	   `pr_interp_fdpic_loadmap' are 4 bytes each.  */
   6726 	raw_size = 184 + 4 + 4;
   6727 
   6728 	break;
   6729     }
   6730 
   6731   /* Make a ".reg/999" section.  */
   6732   return _bfd_elfcore_make_pseudosection (abfd, ".reg", raw_size,
   6733 					  note->descpos + offset);
   6734 }
   6735 
   6736 static bool
   6737 elf32_frv_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   6738 {
   6739   switch (note->descsz)
   6740     {
   6741       default:
   6742 	return false;
   6743 
   6744       /* The Linux/FRV elf_prpsinfo struct is 124 bytes long.  */
   6745       case 124:
   6746 
   6747 	/* `pr_fname' is found at offset 28 and is 16 bytes long.  */
   6748 	elf_tdata (abfd)->core->program
   6749 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
   6750 
   6751 	/* `pr_psargs' is found at offset 44 and is 80 bytes long.  */
   6752 	elf_tdata (abfd)->core->command
   6753 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
   6754     }
   6755 
   6756   /* Note that for some reason, a spurious space is tacked
   6757      onto the end of the args in some (at least one anyway)
   6758      implementations, so strip it off if it exists.  */
   6759 
   6760   {
   6761     char *command = elf_tdata (abfd)->core->command;
   6762     int n = strlen (command);
   6763 
   6764     if (0 < n && command[n - 1] == ' ')
   6765       command[n - 1] = '\0';
   6766   }
   6767 
   6768   return true;
   6769 }
   6770 #define ELF_ARCH		bfd_arch_frv
   6771 #define ELF_MACHINE_CODE	EM_CYGNUS_FRV
   6772 #define ELF_MAXPAGESIZE		0x1000
   6773 
   6774 #define TARGET_BIG_SYM		frv_elf32_vec
   6775 #define TARGET_BIG_NAME		"elf32-frv"
   6776 
   6777 #define elf_info_to_howto			frv_info_to_howto_rela
   6778 #define elf_backend_relocate_section		elf32_frv_relocate_section
   6779 #define elf_backend_gc_mark_hook		elf32_frv_gc_mark_hook
   6780 #define elf_backend_check_relocs		elf32_frv_check_relocs
   6781 #define elf_backend_object_p			elf32_frv_object_p
   6782 #define elf_backend_add_symbol_hook		elf32_frv_add_symbol_hook
   6783 
   6784 #define elf_backend_stack_align			8
   6785 #define elf_backend_can_gc_sections		1
   6786 #define elf_backend_rela_normal			1
   6787 
   6788 #define bfd_elf32_bfd_reloc_type_lookup		frv_reloc_type_lookup
   6789 #define bfd_elf32_bfd_reloc_name_lookup	frv_reloc_name_lookup
   6790 #define bfd_elf32_bfd_set_private_flags		frv_elf_set_private_flags
   6791 #define bfd_elf32_bfd_merge_private_bfd_data	frv_elf_merge_private_bfd_data
   6792 #define bfd_elf32_bfd_print_private_bfd_data	frv_elf_print_private_bfd_data
   6793 
   6794 #define elf_backend_want_got_sym	1
   6795 #define elf_backend_got_header_size	0
   6796 #define elf_backend_want_got_plt	0
   6797 #define elf_backend_plt_readonly	1
   6798 #define elf_backend_want_plt_sym	0
   6799 #define elf_backend_plt_header_size	0
   6800 
   6801 #define elf_backend_finish_dynamic_sections \
   6802 		elf32_frv_finish_dynamic_sections
   6803 
   6804 #define elf_backend_grok_prstatus	elf32_frv_grok_prstatus
   6805 #define elf_backend_grok_psinfo		elf32_frv_grok_psinfo
   6806 
   6807 #define elf_backend_linux_prpsinfo32_ugid16	true
   6808 
   6809 #include "elf32-target.h"
   6810 
   6811 #undef ELF_TARGET_ID
   6812 #define ELF_TARGET_ID		FRV_ELF_DATA
   6813 #undef ELF_MAXPAGESIZE
   6814 #define ELF_MAXPAGESIZE		0x4000
   6815 
   6816 #undef TARGET_BIG_SYM
   6817 #define TARGET_BIG_SYM		frv_elf32_fdpic_vec
   6818 #undef TARGET_BIG_NAME
   6819 #define TARGET_BIG_NAME		"elf32-frvfdpic"
   6820 #undef	elf32_bed
   6821 #define	elf32_bed		elf32_frvfdpic_bed
   6822 
   6823 #undef elf_info_to_howto_rel
   6824 #define elf_info_to_howto_rel	frvfdpic_info_to_howto_rel
   6825 
   6826 #undef bfd_elf32_bfd_link_hash_table_create
   6827 #define bfd_elf32_bfd_link_hash_table_create \
   6828 		frvfdpic_elf_link_hash_table_create
   6829 #undef elf_backend_early_size_sections
   6830 #define elf_backend_early_size_sections \
   6831 		elf32_frvfdpic_early_size_sections
   6832 
   6833 #undef elf_backend_create_dynamic_sections
   6834 #define elf_backend_create_dynamic_sections \
   6835 		elf32_frvfdpic_create_dynamic_sections
   6836 #undef elf_backend_adjust_dynamic_symbol
   6837 #define elf_backend_adjust_dynamic_symbol \
   6838 		elf32_frvfdpic_adjust_dynamic_symbol
   6839 #undef elf_backend_late_size_sections
   6840 #define elf_backend_late_size_sections \
   6841 		elf32_frvfdpic_late_size_sections
   6842 #undef bfd_elf32_bfd_relax_section
   6843 #define bfd_elf32_bfd_relax_section \
   6844   elf32_frvfdpic_relax_section
   6845 #undef elf_backend_finish_dynamic_symbol
   6846 #define elf_backend_finish_dynamic_symbol \
   6847 		elf32_frvfdpic_finish_dynamic_symbol
   6848 #undef elf_backend_finish_dynamic_sections
   6849 #define elf_backend_finish_dynamic_sections \
   6850 		elf32_frvfdpic_finish_dynamic_sections
   6851 
   6852 #undef elf_backend_discard_info
   6853 #define elf_backend_discard_info \
   6854 		frvfdpic_elf_discard_info
   6855 #undef elf_backend_can_make_relative_eh_frame
   6856 #define elf_backend_can_make_relative_eh_frame \
   6857 		frvfdpic_elf_use_relative_eh_frame
   6858 #undef elf_backend_can_make_lsda_relative_eh_frame
   6859 #define elf_backend_can_make_lsda_relative_eh_frame \
   6860 		frvfdpic_elf_use_relative_eh_frame
   6861 #undef elf_backend_encode_eh_address
   6862 #define elf_backend_encode_eh_address \
   6863 		frvfdpic_elf_encode_eh_address
   6864 
   6865 #undef elf_backend_may_use_rel_p
   6866 #define elf_backend_may_use_rel_p	1
   6867 #undef elf_backend_may_use_rela_p
   6868 #define elf_backend_may_use_rela_p	1
   6869 /* We use REL for dynamic relocations only.  */
   6870 #undef elf_backend_default_use_rela_p
   6871 #define elf_backend_default_use_rela_p	1
   6872 
   6873 #undef elf_backend_omit_section_dynsym
   6874 #define elf_backend_omit_section_dynsym _frvfdpic_link_omit_section_dynsym
   6875 
   6876 #include "elf32-target.h"
   6877