Home | History | Annotate | Line # | Download | only in bfd
elfnn-kvx.c revision 1.1.1.3
      1 /* KVX-specific support for NN-bit ELF.
      2    Copyright (C) 2009-2026 Free Software Foundation, Inc.
      3    Contributed by Kalray SA.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; see the file COPYING3. If not,
     19    see <http://www.gnu.org/licenses/>.  */
     20 
     21 #include "sysdep.h"
     22 #include "bfd.h"
     23 #include "libiberty.h"
     24 #include "libbfd.h"
     25 #include "elf-bfd.h"
     26 #include "bfdlink.h"
     27 #include "objalloc.h"
     28 #include "elf/kvx.h"
     29 #include "elfxx-kvx.h"
     30 
     31 #define ARCH_SIZE	NN
     32 
     33 #if ARCH_SIZE == 64
     34 #define LOG_FILE_ALIGN	3
     35 #endif
     36 
     37 #if ARCH_SIZE == 32
     38 #define LOG_FILE_ALIGN	2
     39 #endif
     40 
     41 #define IS_KVX_TLS_RELOC(R_TYPE)			\
     42   ((R_TYPE) == BFD_RELOC_KVX_S37_TLS_LE_LO10	\
     43    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LE_UP27	\
     44    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_LO10	\
     45    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_UP27	\
     46    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_EX6	\
     47    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_DTPOFF_LO10	\
     48    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_DTPOFF_UP27	\
     49    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_LO10	\
     50    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_UP27	\
     51    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_EX6	\
     52    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_IE_LO10	\
     53    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_IE_UP27	\
     54    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_LO10	\
     55    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_UP27	\
     56    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_EX6	\
     57    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_GD_LO10	\
     58    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_GD_UP27	\
     59    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_LO10	\
     60    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_UP27	\
     61    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_EX6	\
     62    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LD_LO10	\
     63    || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LD_UP27	\
     64    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_LO10	\
     65    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_UP27	\
     66    || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_EX6	\
     67    )
     68 
     69 #define IS_KVX_TLS_RELAX_RELOC(R_TYPE) 0
     70 
     71 #define ELIMINATE_COPY_RELOCS 0
     72 
     73 /* Return size of a relocation entry.  HTAB is the bfd's
     74    elf_kvx_link_hash_entry.  */
     75 #define RELOC_SIZE(HTAB) (sizeof (ElfNN_External_Rela))
     76 
     77 /* GOT Entry size - 8 bytes in ELF64 and 4 bytes in ELF32.  */
     78 #define GOT_ENTRY_SIZE                  (ARCH_SIZE / 8)
     79 #define PLT_ENTRY_SIZE                  (32)
     80 
     81 #define PLT_SMALL_ENTRY_SIZE            (4*4)
     82 
     83 /* Encoding of the nop instruction */
     84 #define INSN_NOP 0x00f0037f
     85 
     86 #define kvx_compute_jump_table_size(htab)		\
     87   (((htab)->root.srelplt == NULL) ? 0			\
     88    : (htab)->root.srelplt->reloc_count * GOT_ENTRY_SIZE)
     89 
     90 static const bfd_byte elfNN_kvx_small_plt0_entry[PLT_ENTRY_SIZE] =
     91 {
     92  /* FIXME KVX: no first entry, not used yet */
     93   0
     94 };
     95 
     96 /* Per function entry in a procedure linkage table looks like this
     97    if the distance between the PLTGOT and the PLT is < 4GB use
     98    these PLT entries.  */
     99 static const bfd_byte elfNN_kvx_small_plt_entry[PLT_SMALL_ENTRY_SIZE] =
    100 {
    101   0x10, 0x00, 0xc4, 0x0f,       /* get $r16 = $pc     ;; */
    102 #if ARCH_SIZE == 32
    103   0x10, 0x00, 0x40, 0xb0,       /* lwz $r16 = 0[$r16]   ;; */
    104 #else
    105   0x10, 0x00, 0x40, 0xb8,       /* ld $r16 = 0[$r16] ;; */
    106 #endif
    107   0x00, 0x00, 0x00, 0x18,       /* upper 27 bits for LSU */
    108   0x10, 0x00, 0xd8, 0x0f,	/* igoto $r16          ;; */
    109 };
    110 
    111 /* Long stub use 43bits format of make. */
    112 static const uint32_t elfNN_kvx_long_branch_stub[] =
    113 {
    114   0xe0400000,      /* make $r16 = LO10<emm43> EX6<imm43> */
    115   0x00000000,      /* UP27<imm43> ;; */
    116   0x0fd80010,      /* igoto "r16  ;; */
    117 };
    118 
    119 #define elf_info_to_howto               elfNN_kvx_info_to_howto
    120 #define elf_info_to_howto_rel           elfNN_kvx_info_to_howto
    121 
    122 #define KVX_ELF_ABI_VERSION		0
    123 
    124 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
    125 #define ALL_ONES (~ (bfd_vma) 0)
    126 
    127 /* Indexed by the bfd interal reloc enumerators.
    128    Therefore, the table needs to be synced with BFD_RELOC_KVX_*
    129    in reloc.c.   */
    130 
    131 #define KVX_KV3_V1_KV3_V2_KV4_V1
    132 #include "elfxx-kvx-relocs.h"
    133 #undef KVX_KV3_V1_KV3_V2_KV4_V1
    134 
    135 /* Given HOWTO, return the bfd internal relocation enumerator.  */
    136 
    137 static bfd_reloc_code_real_type
    138 elfNN_kvx_bfd_reloc_from_howto (reloc_howto_type *howto)
    139 {
    140   const int size = (int) ARRAY_SIZE (elf_kvx_howto_table);
    141   const ptrdiff_t offset = howto - elf_kvx_howto_table;
    142 
    143   if (offset >= 0 && offset < size)
    144     return BFD_RELOC_KVX_RELOC_START + offset + 1;
    145 
    146   return BFD_RELOC_KVX_RELOC_START + 1;
    147 }
    148 
    149 /* Given R_TYPE, return the bfd internal relocation enumerator.  */
    150 
    151 static bfd_reloc_code_real_type
    152 elfNN_kvx_bfd_reloc_from_type (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type)
    153 {
    154   static bool initialized_p = false;
    155   /* Indexed by R_TYPE, values are offsets in the howto_table.  */
    156   static unsigned int offsets[R_KVX_end];
    157 
    158   if (!initialized_p)
    159     {
    160       unsigned int i;
    161 
    162       for (i = 0; i < ARRAY_SIZE (elf_kvx_howto_table); ++i)
    163 	offsets[elf_kvx_howto_table[i].type] = i;
    164 
    165       initialized_p = true;
    166     }
    167 
    168   /* PR 17512: file: b371e70a.  */
    169   if (r_type >= R_KVX_end)
    170     {
    171       bfd_set_error (bfd_error_bad_value);
    172       return BFD_RELOC_KVX_RELOC_END;
    173     }
    174 
    175   return (BFD_RELOC_KVX_RELOC_START + 1) + offsets[r_type];
    176 }
    177 
    178 struct elf_kvx_reloc_map
    179 {
    180   bfd_reloc_code_real_type from;
    181   bfd_reloc_code_real_type to;
    182 };
    183 
    184 /* Map bfd generic reloc to KVX-specific reloc.  */
    185 static const struct elf_kvx_reloc_map elf_kvx_reloc_map[] =
    186 {
    187   {BFD_RELOC_NONE, BFD_RELOC_KVX_NONE},
    188 
    189   /* Basic data relocations.  */
    190   {BFD_RELOC_CTOR, BFD_RELOC_KVX_NN},
    191   {BFD_RELOC_64, BFD_RELOC_KVX_64},
    192   {BFD_RELOC_32, BFD_RELOC_KVX_32},
    193   {BFD_RELOC_16, BFD_RELOC_KVX_16},
    194   {BFD_RELOC_8,  BFD_RELOC_KVX_8},
    195 
    196   {BFD_RELOC_64_PCREL, BFD_RELOC_KVX_64_PCREL},
    197   {BFD_RELOC_32_PCREL, BFD_RELOC_KVX_32_PCREL},
    198 };
    199 
    200 /* Given the bfd internal relocation enumerator in CODE, return the
    201    corresponding howto entry.  */
    202 
    203 static reloc_howto_type *
    204 elfNN_kvx_howto_from_bfd_reloc (bfd_reloc_code_real_type code)
    205 {
    206   unsigned int i;
    207 
    208   /* Convert bfd generic reloc to KVX-specific reloc.  */
    209   if (code < BFD_RELOC_KVX_RELOC_START || code > BFD_RELOC_KVX_RELOC_END)
    210     for (i = 0; i < ARRAY_SIZE (elf_kvx_reloc_map) ; i++)
    211       if (elf_kvx_reloc_map[i].from == code)
    212 	{
    213 	  code = elf_kvx_reloc_map[i].to;
    214 	  break;
    215 	}
    216 
    217   if (code > BFD_RELOC_KVX_RELOC_START && code < BFD_RELOC_KVX_RELOC_END)
    218       return &elf_kvx_howto_table[code - (BFD_RELOC_KVX_RELOC_START + 1)];
    219 
    220   return NULL;
    221 }
    222 
    223 static reloc_howto_type *
    224 elfNN_kvx_howto_from_type (bfd *abfd, unsigned int r_type)
    225 {
    226   bfd_reloc_code_real_type val;
    227   reloc_howto_type *howto;
    228 
    229 #if ARCH_SIZE == 32
    230   if (r_type > 256)
    231     {
    232       bfd_set_error (bfd_error_bad_value);
    233       return NULL;
    234     }
    235 #endif
    236 
    237   val = elfNN_kvx_bfd_reloc_from_type (abfd, r_type);
    238   howto = elfNN_kvx_howto_from_bfd_reloc (val);
    239 
    240   if (howto != NULL)
    241     return howto;
    242 
    243   bfd_set_error (bfd_error_bad_value);
    244   return NULL;
    245 }
    246 
    247 static bool
    248 elfNN_kvx_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
    249 			 Elf_Internal_Rela *elf_reloc)
    250 {
    251   unsigned int r_type;
    252 
    253   r_type = ELFNN_R_TYPE (elf_reloc->r_info);
    254   bfd_reloc->howto = elfNN_kvx_howto_from_type (abfd, r_type);
    255 
    256   if (bfd_reloc->howto == NULL)
    257     {
    258       /* xgettext:c-format */
    259       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
    260 			  abfd, r_type);
    261       return false;
    262     }
    263   return true;
    264 }
    265 
    266 static reloc_howto_type *
    267 elfNN_kvx_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    268 			     bfd_reloc_code_real_type code)
    269 {
    270   reloc_howto_type *howto = elfNN_kvx_howto_from_bfd_reloc (code);
    271 
    272   if (howto != NULL)
    273     return howto;
    274 
    275   bfd_set_error (bfd_error_bad_value);
    276   return NULL;
    277 }
    278 
    279 static reloc_howto_type *
    280 elfNN_kvx_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    281 			     const char *r_name)
    282 {
    283   unsigned int i;
    284 
    285   for (i = 0; i < ARRAY_SIZE (elf_kvx_howto_table); ++i)
    286     if (elf_kvx_howto_table[i].name != NULL
    287 	&& strcasecmp (elf_kvx_howto_table[i].name, r_name) == 0)
    288       return &elf_kvx_howto_table[i];
    289 
    290   return NULL;
    291 }
    292 
    293 #define TARGET_LITTLE_SYM               kvx_elfNN_vec
    294 #define TARGET_LITTLE_NAME              "elfNN-kvx"
    295 
    296 /* The linker script knows the section names for placement.
    297    The entry_names are used to do simple name mangling on the stubs.
    298    Given a function name, and its type, the stub can be found. The
    299    name can be changed. The only requirement is the %s be present.  */
    300 #define STUB_ENTRY_NAME   "__%s_veneer"
    301 
    302 /* The name of the dynamic interpreter.  This is put in the .interp
    303    section.  */
    304 #define ELF_DYNAMIC_INTERPRETER     "/lib/ld.so.1"
    305 
    306 
    307 /* PCREL 27 is signed-extended and scaled by 4 */
    308 #define KVX_MAX_FWD_CALL_OFFSET \
    309   (((1 << 26) - 1) << 2)
    310 #define KVX_MAX_BWD_CALL_OFFSET \
    311   (-((1 << 26) << 2))
    312 
    313 /* Check that the destination of the call is within the PCREL27
    314    range. */
    315 static int
    316 kvx_valid_call_p (bfd_vma value, bfd_vma place)
    317 {
    318   bfd_signed_vma offset = (bfd_signed_vma) (value - place);
    319   return (offset <= KVX_MAX_FWD_CALL_OFFSET
    320 	  && offset >= KVX_MAX_BWD_CALL_OFFSET);
    321 }
    322 
    323 /* Section name for stubs is the associated section name plus this
    324    string.  */
    325 #define STUB_SUFFIX ".stub"
    326 
    327 enum elf_kvx_stub_type
    328 {
    329   kvx_stub_none,
    330   kvx_stub_long_branch,
    331 };
    332 
    333 struct elf_kvx_stub_hash_entry
    334 {
    335   /* Base hash table entry structure.  */
    336   struct bfd_hash_entry root;
    337 
    338   /* The stub section.  */
    339   asection *stub_sec;
    340 
    341   /* Offset within stub_sec of the beginning of this stub.  */
    342   bfd_vma stub_offset;
    343 
    344   /* Given the symbol's value and its section we can determine its final
    345      value when building the stubs (so the stub knows where to jump).  */
    346   bfd_vma target_value;
    347   asection *target_section;
    348 
    349   enum elf_kvx_stub_type stub_type;
    350 
    351   /* The symbol table entry, if any, that this was derived from.  */
    352   struct elf_kvx_link_hash_entry *h;
    353 
    354   /* Destination symbol type */
    355   unsigned char st_type;
    356 
    357   /* Where this stub is being called from, or, in the case of combined
    358      stub sections, the first input section in the group.  */
    359   asection *id_sec;
    360 
    361   /* The name for the local symbol at the start of this stub.  The
    362      stub name in the hash table has to be unique; this does not, so
    363      it can be friendlier.  */
    364   char *output_name;
    365 };
    366 
    367 /* Used to build a map of a section.  This is required for mixed-endian
    368    code/data.  */
    369 
    370 typedef struct elf_elf_section_map
    371 {
    372   bfd_vma vma;
    373   char type;
    374 }
    375 elf_kvx_section_map;
    376 
    377 
    378 typedef struct _kvx_elf_section_data
    379 {
    380   struct bfd_elf_section_data elf;
    381   unsigned int mapcount;
    382   unsigned int mapsize;
    383   elf_kvx_section_map *map;
    384 }
    385 _kvx_elf_section_data;
    386 
    387 #define elf_kvx_section_data(sec) \
    388   ((_kvx_elf_section_data *) elf_section_data (sec))
    389 
    390 struct elf_kvx_local_symbol
    391 {
    392   unsigned int got_type;
    393   bfd_signed_vma got_refcount;
    394   bfd_vma got_offset;
    395 };
    396 
    397 struct elf_kvx_obj_tdata
    398 {
    399   struct elf_obj_tdata root;
    400 
    401   /* local symbol descriptors */
    402   struct elf_kvx_local_symbol *locals;
    403 
    404   /* Zero to warn when linking objects with incompatible enum sizes.  */
    405   int no_enum_size_warning;
    406 
    407   /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
    408   int no_wchar_size_warning;
    409 };
    410 
    411 #define elf_kvx_tdata(bfd)				\
    412   ((struct elf_kvx_obj_tdata *) (bfd)->tdata.any)
    413 
    414 #define elf_kvx_locals(bfd) (elf_kvx_tdata (bfd)->locals)
    415 
    416 #define is_kvx_elf(bfd)				\
    417   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    418    && elf_tdata (bfd) != NULL				\
    419    && elf_object_id (bfd) == KVX_ELF_DATA)
    420 
    421 static bool
    422 elfNN_kvx_mkobject (bfd *abfd)
    423 {
    424   return bfd_elf_allocate_object (abfd, sizeof (struct elf_kvx_obj_tdata));
    425 }
    426 
    427 #define elf_kvx_hash_entry(ent) \
    428   ((struct elf_kvx_link_hash_entry *)(ent))
    429 
    430 #define GOT_UNKNOWN    0
    431 #define GOT_NORMAL     1
    432 
    433 #define GOT_TLS_GD     2
    434 #define GOT_TLS_IE     4
    435 #define GOT_TLS_LD     8
    436 
    437 /* KVX ELF linker hash entry.  */
    438 struct elf_kvx_link_hash_entry
    439 {
    440   struct elf_link_hash_entry root;
    441 
    442   /* Since PLT entries have variable size, we need to record the
    443      index into .got.plt instead of recomputing it from the PLT
    444      offset.  */
    445   bfd_signed_vma plt_got_offset;
    446 
    447   /* Bit mask representing the type of GOT entry(s) if any required by
    448      this symbol.  */
    449   unsigned int got_type;
    450 
    451   /* A pointer to the most recently used stub hash entry against this
    452      symbol.  */
    453   struct elf_kvx_stub_hash_entry *stub_cache;
    454 };
    455 
    456 /* Get the KVX elf linker hash table from a link_info structure.  */
    457 #define elf_kvx_hash_table(info)					\
    458   ((struct elf_kvx_link_hash_table *) ((info)->hash))
    459 
    460 #define kvx_stub_hash_lookup(table, string, create, copy)		\
    461   ((struct elf_kvx_stub_hash_entry *)				\
    462    bfd_hash_lookup ((table), (string), (create), (copy)))
    463 
    464 /* KVX ELF linker hash table.  */
    465 struct elf_kvx_link_hash_table
    466 {
    467   /* The main hash table.  */
    468   struct elf_link_hash_table root;
    469 
    470   /* Nonzero to force PIC branch veneers.  */
    471   int pic_veneer;
    472 
    473   /* The number of bytes in the initial entry in the PLT.  */
    474   bfd_size_type plt_header_size;
    475 
    476   /* The number of bytes in the subsequent PLT etries.  */
    477   bfd_size_type plt_entry_size;
    478 
    479   /* The bytes of the subsequent PLT entry.  */
    480   const bfd_byte *plt_entry;
    481 
    482   /* Short-cuts to get to dynamic linker sections.  */
    483   asection *sdynbss;
    484   asection *srelbss;
    485 
    486   /* Small local sym cache.  */
    487   struct sym_cache sym_cache;
    488 
    489   /* For convenience in allocate_dynrelocs.  */
    490   bfd *obfd;
    491 
    492   /* The amount of space used by the reserved portion of the sgotplt
    493      section, plus whatever space is used by the jump slots.  */
    494   bfd_vma sgotplt_jump_table_size;
    495 
    496   /* The stub hash table.  */
    497   struct bfd_hash_table stub_hash_table;
    498 
    499   /* Linker stub bfd.  */
    500   bfd *stub_bfd;
    501 
    502   /* Linker call-backs.  */
    503   asection *(*add_stub_section) (const char *, asection *);
    504   void (*layout_sections_again) (void);
    505 
    506   /* Array to keep track of which stub sections have been created, and
    507      information on stub grouping.  */
    508   struct map_stub
    509   {
    510     /* This is the section to which stubs in the group will be
    511        attached.  */
    512     asection *link_sec;
    513     /* The stub section.  */
    514     asection *stub_sec;
    515   } *stub_group;
    516 
    517   /* Assorted information used by elfNN_kvx_size_stubs.  */
    518   unsigned int bfd_count;
    519   unsigned int top_index;
    520   asection **input_list;
    521 };
    522 
    523 /* Create an entry in an KVX ELF linker hash table.  */
    524 
    525 static struct bfd_hash_entry *
    526 elfNN_kvx_link_hash_newfunc (struct bfd_hash_entry *entry,
    527 			     struct bfd_hash_table *table,
    528 			     const char *string)
    529 {
    530   struct elf_kvx_link_hash_entry *ret =
    531     (struct elf_kvx_link_hash_entry *) entry;
    532 
    533   /* Allocate the structure if it has not already been allocated by a
    534      subclass.  */
    535   if (ret == NULL)
    536     ret = bfd_hash_allocate (table,
    537 			     sizeof (struct elf_kvx_link_hash_entry));
    538   if (ret == NULL)
    539     return (struct bfd_hash_entry *) ret;
    540 
    541   /* Call the allocation method of the superclass.  */
    542   ret = ((struct elf_kvx_link_hash_entry *)
    543 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
    544 				     table, string));
    545   if (ret != NULL)
    546     {
    547       ret->got_type = GOT_UNKNOWN;
    548       ret->plt_got_offset = (bfd_vma) - 1;
    549       ret->stub_cache = NULL;
    550     }
    551 
    552   return (struct bfd_hash_entry *) ret;
    553 }
    554 
    555 /* Initialize an entry in the stub hash table.  */
    556 
    557 static struct bfd_hash_entry *
    558 stub_hash_newfunc (struct bfd_hash_entry *entry,
    559 		   struct bfd_hash_table *table, const char *string)
    560 {
    561   /* Allocate the structure if it has not already been allocated by a
    562      subclass.  */
    563   if (entry == NULL)
    564     {
    565       entry = bfd_hash_allocate (table,
    566 				 sizeof (struct
    567 					 elf_kvx_stub_hash_entry));
    568       if (entry == NULL)
    569 	return entry;
    570     }
    571 
    572   /* Call the allocation method of the superclass.  */
    573   entry = bfd_hash_newfunc (entry, table, string);
    574   if (entry != NULL)
    575     {
    576       struct elf_kvx_stub_hash_entry *eh;
    577 
    578       /* Initialize the local fields.  */
    579       eh = (struct elf_kvx_stub_hash_entry *) entry;
    580       eh->stub_sec = NULL;
    581       eh->stub_offset = 0;
    582       eh->target_value = 0;
    583       eh->target_section = NULL;
    584       eh->stub_type = kvx_stub_none;
    585       eh->h = NULL;
    586       eh->id_sec = NULL;
    587     }
    588 
    589   return entry;
    590 }
    591 
    592 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
    593 
    594 static void
    595 elfNN_kvx_copy_indirect_symbol (struct bfd_link_info *info,
    596 				struct elf_link_hash_entry *dir,
    597 				struct elf_link_hash_entry *ind)
    598 {
    599   struct elf_kvx_link_hash_entry *edir, *eind;
    600 
    601   edir = (struct elf_kvx_link_hash_entry *) dir;
    602   eind = (struct elf_kvx_link_hash_entry *) ind;
    603 
    604   if (ind->root.type == bfd_link_hash_indirect)
    605     {
    606       /* Copy over PLT info.  */
    607       if (dir->got.refcount <= 0)
    608 	{
    609 	  edir->got_type = eind->got_type;
    610 	  eind->got_type = GOT_UNKNOWN;
    611 	}
    612     }
    613 
    614   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
    615 }
    616 
    617 /* Destroy a KVX elf linker hash table.  */
    618 
    619 static void
    620 elfNN_kvx_link_hash_table_free (bfd *obfd)
    621 {
    622   struct elf_kvx_link_hash_table *ret
    623     = (struct elf_kvx_link_hash_table *) obfd->link.hash;
    624 
    625   bfd_hash_table_free (&ret->stub_hash_table);
    626   _bfd_elf_link_hash_table_free (obfd);
    627 }
    628 
    629 /* Create a KVX elf linker hash table.  */
    630 
    631 static struct bfd_link_hash_table *
    632 elfNN_kvx_link_hash_table_create (bfd *abfd)
    633 {
    634   struct elf_kvx_link_hash_table *ret;
    635   bfd_size_type amt = sizeof (struct elf_kvx_link_hash_table);
    636 
    637   ret = bfd_zmalloc (amt);
    638   if (ret == NULL)
    639     return NULL;
    640 
    641   if (!_bfd_elf_link_hash_table_init
    642       (&ret->root, abfd, elfNN_kvx_link_hash_newfunc,
    643        sizeof (struct elf_kvx_link_hash_entry)))
    644     {
    645       free (ret);
    646       return NULL;
    647     }
    648 
    649   ret->plt_header_size = PLT_ENTRY_SIZE;
    650   ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
    651   ret->plt_entry = elfNN_kvx_small_plt_entry;
    652 
    653   ret->obfd = abfd;
    654 
    655   if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
    656 			    sizeof (struct elf_kvx_stub_hash_entry)))
    657     {
    658       _bfd_elf_link_hash_table_free (abfd);
    659       return NULL;
    660     }
    661 
    662   ret->root.root.hash_table_free = elfNN_kvx_link_hash_table_free;
    663 
    664   return &ret->root.root;
    665 }
    666 
    667 static bfd_reloc_status_type
    668 kvx_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
    669 	      bfd_vma offset, bfd_vma value)
    670 {
    671   reloc_howto_type *howto;
    672 
    673   howto = elfNN_kvx_howto_from_type (input_bfd, r_type);
    674   r_type = elfNN_kvx_bfd_reloc_from_type (input_bfd, r_type);
    675   return _bfd_kvx_elf_put_addend (input_bfd,
    676 				  input_section->contents + offset, r_type,
    677 				  howto, value);
    678 }
    679 
    680 /* Determine the type of stub needed, if any, for a call.  */
    681 
    682 static enum elf_kvx_stub_type
    683 kvx_type_of_stub (asection *input_sec,
    684 		  const Elf_Internal_Rela *rel,
    685 		  asection *sym_sec,
    686 		  unsigned char st_type,
    687 		  bfd_vma destination)
    688 {
    689   bfd_vma location;
    690   bfd_signed_vma branch_offset;
    691   unsigned int r_type;
    692   enum elf_kvx_stub_type stub_type = kvx_stub_none;
    693 
    694   if (st_type != STT_FUNC
    695       && (sym_sec == input_sec))
    696     return stub_type;
    697 
    698   /* Determine where the call point is.  */
    699   location = (input_sec->output_offset
    700 	      + input_sec->output_section->vma + rel->r_offset);
    701 
    702   branch_offset = (bfd_signed_vma) (destination - location);
    703 
    704   r_type = ELFNN_R_TYPE (rel->r_info);
    705 
    706   /* We don't want to redirect any old unconditional jump in this way,
    707      only one which is being used for a sibcall, where it is
    708      acceptable for the R16 and R17 registers to be clobbered.  */
    709   if (r_type == R_KVX_PCREL27
    710       && (branch_offset > KVX_MAX_FWD_CALL_OFFSET
    711 	  || branch_offset < KVX_MAX_BWD_CALL_OFFSET))
    712     {
    713       stub_type = kvx_stub_long_branch;
    714     }
    715 
    716   return stub_type;
    717 }
    718 
    719 /* Build a name for an entry in the stub hash table.  */
    720 
    721 static char *
    722 elfNN_kvx_stub_name (const asection *input_section,
    723 		     const asection *sym_sec,
    724 		     const struct elf_kvx_link_hash_entry *hash,
    725 		     const Elf_Internal_Rela *rel)
    726 {
    727   char *stub_name;
    728   bfd_size_type len;
    729 
    730   if (hash)
    731     {
    732       len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
    733       stub_name = bfd_malloc (len);
    734       if (stub_name != NULL)
    735 	snprintf (stub_name, len, "%08x_%s+%" PRIx64 "x",
    736 		  (unsigned int) input_section->id,
    737 		  hash->root.root.root.string,
    738 		  (uint64_t) rel->r_addend);
    739     }
    740   else
    741     {
    742       len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
    743       stub_name = bfd_malloc (len);
    744       if (stub_name != NULL)
    745 	snprintf (stub_name, len, "%08x_%x:%x+%" PRIx64 "x",
    746 		  (unsigned int) input_section->id,
    747 		  (unsigned int) sym_sec->id,
    748 		  (unsigned int) ELFNN_R_SYM (rel->r_info),
    749 		  (uint64_t) rel->r_addend);
    750     }
    751 
    752   return stub_name;
    753 }
    754 
    755 /* Return true if symbol H should be hashed in the `.gnu.hash' section.  For
    756    executable PLT slots where the executable never takes the address of those
    757    functions, the function symbols are not added to the hash table.  */
    758 
    759 static bool
    760 elf_kvx_hash_symbol (struct elf_link_hash_entry *h)
    761 {
    762   if (h->plt.offset != (bfd_vma) -1
    763       && !h->def_regular
    764       && !h->pointer_equality_needed)
    765     return false;
    766 
    767   return _bfd_elf_hash_symbol (h);
    768 }
    769 
    770 
    771 /* Look up an entry in the stub hash.  Stub entries are cached because
    772    creating the stub name takes a bit of time.  */
    773 
    774 static struct elf_kvx_stub_hash_entry *
    775 elfNN_kvx_get_stub_entry (const asection *input_section,
    776 			  const asection *sym_sec,
    777 			  struct elf_link_hash_entry *hash,
    778 			  const Elf_Internal_Rela *rel,
    779 			  struct elf_kvx_link_hash_table *htab)
    780 {
    781   struct elf_kvx_stub_hash_entry *stub_entry;
    782   struct elf_kvx_link_hash_entry *h =
    783     (struct elf_kvx_link_hash_entry *) hash;
    784   const asection *id_sec;
    785 
    786   if ((input_section->flags & SEC_CODE) == 0)
    787     return NULL;
    788 
    789   /* If this input section is part of a group of sections sharing one
    790      stub section, then use the id of the first section in the group.
    791      Stub names need to include a section id, as there may well be
    792      more than one stub used to reach say, printf, and we need to
    793      distinguish between them.  */
    794   id_sec = htab->stub_group[input_section->id].link_sec;
    795 
    796   if (h != NULL && h->stub_cache != NULL
    797       && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
    798     {
    799       stub_entry = h->stub_cache;
    800     }
    801   else
    802     {
    803       char *stub_name;
    804 
    805       stub_name = elfNN_kvx_stub_name (id_sec, sym_sec, h, rel);
    806       if (stub_name == NULL)
    807 	return NULL;
    808 
    809       stub_entry = kvx_stub_hash_lookup (&htab->stub_hash_table,
    810 					 stub_name, false, false);
    811       if (h != NULL)
    812 	h->stub_cache = stub_entry;
    813 
    814       free (stub_name);
    815     }
    816 
    817   return stub_entry;
    818 }
    819 
    820 
    821 /* Create a stub section.  */
    822 
    823 static asection *
    824 _bfd_kvx_create_stub_section (asection *section,
    825 			      struct elf_kvx_link_hash_table *htab)
    826 
    827 {
    828   size_t namelen;
    829   bfd_size_type len;
    830   char *s_name;
    831 
    832   namelen = strlen (section->name);
    833   len = namelen + sizeof (STUB_SUFFIX);
    834   s_name = bfd_alloc (htab->stub_bfd, len);
    835   if (s_name == NULL)
    836     return NULL;
    837 
    838   memcpy (s_name, section->name, namelen);
    839   memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
    840   return (*htab->add_stub_section) (s_name, section);
    841 }
    842 
    843 
    844 /* Find or create a stub section for a link section.
    845 
    846    Fix or create the stub section used to collect stubs attached to
    847    the specified link section.  */
    848 
    849 static asection *
    850 _bfd_kvx_get_stub_for_link_section (asection *link_section,
    851 				    struct elf_kvx_link_hash_table *htab)
    852 {
    853   if (htab->stub_group[link_section->id].stub_sec == NULL)
    854     htab->stub_group[link_section->id].stub_sec
    855       = _bfd_kvx_create_stub_section (link_section, htab);
    856   return htab->stub_group[link_section->id].stub_sec;
    857 }
    858 
    859 
    860 /* Find or create a stub section in the stub group for an input
    861    section.  */
    862 
    863 static asection *
    864 _bfd_kvx_create_or_find_stub_sec (asection *section,
    865 				  struct elf_kvx_link_hash_table *htab)
    866 {
    867   asection *link_sec = htab->stub_group[section->id].link_sec;
    868   return _bfd_kvx_get_stub_for_link_section (link_sec, htab);
    869 }
    870 
    871 
    872 /* Add a new stub entry in the stub group associated with an input
    873    section to the stub hash.  Not all fields of the new stub entry are
    874    initialised.  */
    875 
    876 static struct elf_kvx_stub_hash_entry *
    877 _bfd_kvx_add_stub_entry_in_group (const char *stub_name,
    878 				  asection *section,
    879 				  struct elf_kvx_link_hash_table *htab)
    880 {
    881   asection *link_sec;
    882   asection *stub_sec;
    883   struct elf_kvx_stub_hash_entry *stub_entry;
    884 
    885   link_sec = htab->stub_group[section->id].link_sec;
    886   stub_sec = _bfd_kvx_create_or_find_stub_sec (section, htab);
    887 
    888   /* Enter this entry into the linker stub hash table.  */
    889   stub_entry = kvx_stub_hash_lookup (&htab->stub_hash_table, stub_name,
    890 				     true, false);
    891   if (stub_entry == NULL)
    892     {
    893       /* xgettext:c-format */
    894       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
    895 			  section->owner, stub_name);
    896       return NULL;
    897     }
    898 
    899   stub_entry->stub_sec = stub_sec;
    900   stub_entry->stub_offset = 0;
    901   stub_entry->id_sec = link_sec;
    902 
    903   return stub_entry;
    904 }
    905 
    906 static bool
    907 kvx_build_one_stub (struct bfd_hash_entry *gen_entry,
    908 		    void *in_arg)
    909 {
    910   struct elf_kvx_stub_hash_entry *stub_entry;
    911   asection *stub_sec;
    912   bfd *stub_bfd;
    913   bfd_byte *loc;
    914   bfd_vma sym_value;
    915   unsigned int template_size;
    916   const uint32_t *template;
    917   unsigned int i;
    918   struct bfd_link_info *info;
    919 
    920   /* Massage our args to the form they really have.  */
    921   stub_entry = (struct elf_kvx_stub_hash_entry *) gen_entry;
    922 
    923   info = (struct bfd_link_info *) in_arg;
    924 
    925   /* Fail if the target section could not be assigned to an output
    926      section.  The user should fix his linker script.  */
    927   if (stub_entry->target_section->output_section == NULL
    928       && info->non_contiguous_regions)
    929     info->callbacks->fatal (_("%P: Could not assign '%pA' to an output section. "
    930 			      "Retry without "
    931 			      "--enable-non-contiguous-regions.\n"),
    932 			    stub_entry->target_section);
    933 
    934   stub_sec = stub_entry->stub_sec;
    935 
    936   /* Make a note of the offset within the stubs for this entry.  */
    937   stub_entry->stub_offset = stub_sec->size;
    938   loc = stub_sec->contents + stub_entry->stub_offset;
    939 
    940   stub_bfd = stub_sec->owner;
    941 
    942   /* This is the address of the stub destination.  */
    943   sym_value = (stub_entry->target_value
    944 	       + stub_entry->target_section->output_offset
    945 	       + stub_entry->target_section->output_section->vma);
    946 
    947   switch (stub_entry->stub_type)
    948     {
    949     case kvx_stub_long_branch:
    950       template = elfNN_kvx_long_branch_stub;
    951       template_size = sizeof (elfNN_kvx_long_branch_stub);
    952       break;
    953     default:
    954       abort ();
    955     }
    956 
    957   for (i = 0; i < (template_size / sizeof template[0]); i++)
    958     {
    959       bfd_putl32 (template[i], loc);
    960       loc += 4;
    961     }
    962 
    963   stub_sec->size += template_size;
    964 
    965   switch (stub_entry->stub_type)
    966     {
    967     case kvx_stub_long_branch:
    968       /* The stub uses a make insn with 43bits immediate.
    969 	 We need to apply 3 relocations:
    970 	 BFD_RELOC_KVX_S43_LO10,
    971 	 BFD_RELOC_KVX_S43_UP27,
    972 	 BFD_RELOC_KVX_S43_EX6.  */
    973       if (kvx_relocate (R_KVX_S43_LO10, stub_bfd, stub_sec,
    974 			stub_entry->stub_offset, sym_value) != bfd_reloc_ok)
    975 	BFD_FAIL ();
    976       if (kvx_relocate (R_KVX_S43_EX6, stub_bfd, stub_sec,
    977 			stub_entry->stub_offset, sym_value) != bfd_reloc_ok)
    978 	BFD_FAIL ();
    979       if (kvx_relocate (R_KVX_S43_UP27, stub_bfd, stub_sec,
    980 			stub_entry->stub_offset + 4, sym_value) != bfd_reloc_ok)
    981 	BFD_FAIL ();
    982       break;
    983     default:
    984       abort ();
    985     }
    986 
    987   return true;
    988 }
    989 
    990 /* As above, but don't actually build the stub.  Just bump offset so
    991    we know stub section sizes.  */
    992 
    993 static bool
    994 kvx_size_one_stub (struct bfd_hash_entry *gen_entry,
    995 		   void *in_arg ATTRIBUTE_UNUSED)
    996 {
    997   struct elf_kvx_stub_hash_entry *stub_entry;
    998   int size;
    999 
   1000   /* Massage our args to the form they really have.  */
   1001   stub_entry = (struct elf_kvx_stub_hash_entry *) gen_entry;
   1002 
   1003   switch (stub_entry->stub_type)
   1004     {
   1005     case kvx_stub_long_branch:
   1006       size = sizeof (elfNN_kvx_long_branch_stub);
   1007       break;
   1008     default:
   1009       abort ();
   1010     }
   1011 
   1012   stub_entry->stub_sec->size += size;
   1013   return true;
   1014 }
   1015 
   1016 /* External entry points for sizing and building linker stubs.  */
   1017 
   1018 /* Set up various things so that we can make a list of input sections
   1019    for each output section included in the link.  Returns -1 on error,
   1020    0 when no stubs will be needed, and 1 on success.  */
   1021 
   1022 int
   1023 elfNN_kvx_setup_section_lists (bfd *output_bfd,
   1024 			       struct bfd_link_info *info)
   1025 {
   1026   bfd *input_bfd;
   1027   unsigned int bfd_count;
   1028   unsigned int top_id, top_index;
   1029   asection *section;
   1030   asection **input_list, **list;
   1031   bfd_size_type amt;
   1032   struct elf_kvx_link_hash_table *htab =
   1033     elf_kvx_hash_table (info);
   1034 
   1035   if (!is_elf_hash_table ((const struct bfd_link_hash_table *)htab))
   1036     return 0;
   1037 
   1038   /* Count the number of input BFDs and find the top input section id.  */
   1039   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
   1040        input_bfd != NULL; input_bfd = input_bfd->link.next)
   1041     {
   1042       bfd_count += 1;
   1043       for (section = input_bfd->sections;
   1044 	   section != NULL; section = section->next)
   1045 	{
   1046 	  if (top_id < section->id)
   1047 	    top_id = section->id;
   1048 	}
   1049     }
   1050   htab->bfd_count = bfd_count;
   1051 
   1052   amt = sizeof (struct map_stub) * (top_id + 1);
   1053   htab->stub_group = bfd_zmalloc (amt);
   1054   if (htab->stub_group == NULL)
   1055     return -1;
   1056 
   1057   /* We can't use output_bfd->section_count here to find the top output
   1058      section index as some sections may have been removed, and
   1059      _bfd_strip_section_from_output doesn't renumber the indices.  */
   1060   for (section = output_bfd->sections, top_index = 0;
   1061        section != NULL; section = section->next)
   1062     {
   1063       if (top_index < section->index)
   1064 	top_index = section->index;
   1065     }
   1066 
   1067   htab->top_index = top_index;
   1068   amt = sizeof (asection *) * (top_index + 1);
   1069   input_list = bfd_malloc (amt);
   1070   htab->input_list = input_list;
   1071   if (input_list == NULL)
   1072     return -1;
   1073 
   1074   /* For sections we aren't interested in, mark their entries with a
   1075      value we can check later.  */
   1076   list = input_list + top_index;
   1077   do
   1078     *list = bfd_abs_section_ptr;
   1079   while (list-- != input_list);
   1080 
   1081   for (section = output_bfd->sections;
   1082        section != NULL; section = section->next)
   1083     {
   1084       if ((section->flags & SEC_CODE) != 0)
   1085 	input_list[section->index] = NULL;
   1086     }
   1087 
   1088   return 1;
   1089 }
   1090 
   1091 /* Used by elfNN_kvx_next_input_section and group_sections.  */
   1092 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
   1093 
   1094 /* The linker repeatedly calls this function for each input section,
   1095    in the order that input sections are linked into output sections.
   1096    Build lists of input sections to determine groupings between which
   1097    we may insert linker stubs.  */
   1098 
   1099 void
   1100 elfNN_kvx_next_input_section (struct bfd_link_info *info, asection *isec)
   1101 {
   1102   struct elf_kvx_link_hash_table *htab =
   1103     elf_kvx_hash_table (info);
   1104 
   1105   if (isec->output_section->index <= htab->top_index)
   1106     {
   1107       asection **list = htab->input_list + isec->output_section->index;
   1108 
   1109       if (*list != bfd_abs_section_ptr)
   1110 	{
   1111 	  /* Steal the link_sec pointer for our list.  */
   1112 	  /* This happens to make the list in reverse order,
   1113 	     which is what we want.  */
   1114 	  PREV_SEC (isec) = *list;
   1115 	  *list = isec;
   1116 	}
   1117     }
   1118 }
   1119 
   1120 /* See whether we can group stub sections together.  Grouping stub
   1121    sections may result in fewer stubs.  More importantly, we need to
   1122    put all .init* and .fini* stubs at the beginning of the .init or
   1123    .fini output sections respectively, because glibc splits the
   1124    _init and _fini functions into multiple parts.  Putting a stub in
   1125    the middle of a function is not a good idea.  */
   1126 
   1127 static void
   1128 group_sections (struct elf_kvx_link_hash_table *htab,
   1129 		bfd_size_type stub_group_size,
   1130 		bool stubs_always_after_branch)
   1131 {
   1132   asection **list = htab->input_list;
   1133 
   1134   do
   1135     {
   1136       asection *tail = *list;
   1137       asection *head;
   1138 
   1139       if (tail == bfd_abs_section_ptr)
   1140 	continue;
   1141 
   1142       /* Reverse the list: we must avoid placing stubs at the
   1143 	 beginning of the section because the beginning of the text
   1144 	 section may be required for an interrupt vector in bare metal
   1145 	 code.  */
   1146 #define NEXT_SEC PREV_SEC
   1147       head = NULL;
   1148       while (tail != NULL)
   1149 	{
   1150 	  /* Pop from tail.  */
   1151 	  asection *item = tail;
   1152 	  tail = PREV_SEC (item);
   1153 
   1154 	  /* Push on head.  */
   1155 	  NEXT_SEC (item) = head;
   1156 	  head = item;
   1157 	}
   1158 
   1159       while (head != NULL)
   1160 	{
   1161 	  asection *curr;
   1162 	  asection *next;
   1163 	  bfd_vma stub_group_start = head->output_offset;
   1164 	  bfd_vma end_of_next;
   1165 
   1166 	  curr = head;
   1167 	  while (NEXT_SEC (curr) != NULL)
   1168 	    {
   1169 	      next = NEXT_SEC (curr);
   1170 	      end_of_next = next->output_offset + next->size;
   1171 	      if (end_of_next - stub_group_start >= stub_group_size)
   1172 		/* End of NEXT is too far from start, so stop.  */
   1173 		break;
   1174 	      /* Add NEXT to the group.  */
   1175 	      curr = next;
   1176 	    }
   1177 
   1178 	  /* OK, the size from the start to the start of CURR is less
   1179 	     than stub_group_size and thus can be handled by one stub
   1180 	     section.  (Or the head section is itself larger than
   1181 	     stub_group_size, in which case we may be toast.)
   1182 	     We should really be keeping track of the total size of
   1183 	     stubs added here, as stubs contribute to the final output
   1184 	     section size.  */
   1185 	  do
   1186 	    {
   1187 	      next = NEXT_SEC (head);
   1188 	      /* Set up this stub group.  */
   1189 	      htab->stub_group[head->id].link_sec = curr;
   1190 	    }
   1191 	  while (head != curr && (head = next) != NULL);
   1192 
   1193 	  /* But wait, there's more!  Input sections up to stub_group_size
   1194 	     bytes after the stub section can be handled by it too.  */
   1195 	  if (!stubs_always_after_branch)
   1196 	    {
   1197 	      stub_group_start = curr->output_offset + curr->size;
   1198 
   1199 	      while (next != NULL)
   1200 		{
   1201 		  end_of_next = next->output_offset + next->size;
   1202 		  if (end_of_next - stub_group_start >= stub_group_size)
   1203 		    /* End of NEXT is too far from stubs, so stop.  */
   1204 		    break;
   1205 		  /* Add NEXT to the stub group.  */
   1206 		  head = next;
   1207 		  next = NEXT_SEC (head);
   1208 		  htab->stub_group[head->id].link_sec = curr;
   1209 		}
   1210 	    }
   1211 	  head = next;
   1212 	}
   1213     }
   1214   while (list++ != htab->input_list + htab->top_index);
   1215 
   1216   free (htab->input_list);
   1217 }
   1218 
   1219 static void
   1220 _bfd_kvx_resize_stubs (struct elf_kvx_link_hash_table *htab)
   1221 {
   1222   asection *section;
   1223 
   1224   /* OK, we've added some stubs.  Find out the new size of the
   1225      stub sections.  */
   1226   for (section = htab->stub_bfd->sections;
   1227        section != NULL; section = section->next)
   1228     {
   1229       /* Ignore non-stub sections.  */
   1230       if (!strstr (section->name, STUB_SUFFIX))
   1231 	continue;
   1232       section->size = 0;
   1233     }
   1234 
   1235   bfd_hash_traverse (&htab->stub_hash_table, kvx_size_one_stub, htab);
   1236 }
   1237 
   1238 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   1239 
   1240 bool
   1241 kvx_elfNN_init_stub_bfd (struct bfd_link_info *info,
   1242 			bfd *stub_bfd)
   1243 {
   1244   struct elf_kvx_link_hash_table *htab;
   1245 
   1246   elf_elfheader (stub_bfd)->e_ident[EI_CLASS] = ELFCLASSNN;
   1247 
   1248 /* Always hook our dynamic sections into the first bfd, which is the
   1249    linker created stub bfd.  This ensures that the GOT header is at
   1250    the start of the output TOC section.  */
   1251   htab = elf_kvx_hash_table (info);
   1252   if (htab == NULL)
   1253     return false;
   1254 
   1255   return true;
   1256 }
   1257 
   1258 /* Determine and set the size of the stub section for a final link.
   1259 
   1260    The basic idea here is to examine all the relocations looking for
   1261    PC-relative calls to a target that is unreachable with a 27bits
   1262    immediate (found in call and goto).  */
   1263 
   1264 bool
   1265 elfNN_kvx_size_stubs (bfd *output_bfd,
   1266 		     bfd *stub_bfd,
   1267 		     struct bfd_link_info *info,
   1268 		     bfd_signed_vma group_size,
   1269 		     asection * (*add_stub_section) (const char *,
   1270 						     asection *),
   1271 		     void (*layout_sections_again) (void))
   1272 {
   1273   bfd_size_type stub_group_size;
   1274   bool stubs_always_before_branch;
   1275   bool stub_changed = false;
   1276   struct elf_kvx_link_hash_table *htab = elf_kvx_hash_table (info);
   1277 
   1278   /* Propagate mach to stub bfd, because it may not have been
   1279      finalized when we created stub_bfd.  */
   1280   bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
   1281 		     bfd_get_mach (output_bfd));
   1282 
   1283   /* Stash our params away.  */
   1284   htab->stub_bfd = stub_bfd;
   1285   htab->add_stub_section = add_stub_section;
   1286   htab->layout_sections_again = layout_sections_again;
   1287   stubs_always_before_branch = group_size < 0;
   1288   if (group_size < 0)
   1289     stub_group_size = -group_size;
   1290   else
   1291     stub_group_size = group_size;
   1292 
   1293   if (stub_group_size == 1)
   1294     {
   1295       /* Default values.  */
   1296       /* KVX branch range is +-256MB. The value used is 1MB less.  */
   1297       stub_group_size = 255 * 1024 * 1024;
   1298     }
   1299 
   1300   group_sections (htab, stub_group_size, stubs_always_before_branch);
   1301 
   1302   (*htab->layout_sections_again) ();
   1303 
   1304   while (1)
   1305     {
   1306       bfd *input_bfd;
   1307 
   1308       for (input_bfd = info->input_bfds;
   1309 	   input_bfd != NULL; input_bfd = input_bfd->link.next)
   1310 	{
   1311 	  Elf_Internal_Shdr *symtab_hdr;
   1312 	  asection *section;
   1313 	  Elf_Internal_Sym *local_syms = NULL;
   1314 
   1315 	  if (!is_kvx_elf (input_bfd)
   1316 	      || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
   1317 	    continue;
   1318 
   1319 	  /* We'll need the symbol table in a second.  */
   1320 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   1321 	  if (symtab_hdr->sh_info == 0)
   1322 	    continue;
   1323 
   1324 	  /* Walk over each section attached to the input bfd.  */
   1325 	  for (section = input_bfd->sections;
   1326 	       section != NULL; section = section->next)
   1327 	    {
   1328 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   1329 
   1330 	      /* If there aren't any relocs, then there's nothing more
   1331 		 to do.  */
   1332 	      if ((section->flags & SEC_RELOC) == 0
   1333 		  || section->reloc_count == 0
   1334 		  || (section->flags & SEC_CODE) == 0)
   1335 		continue;
   1336 
   1337 	      /* If this section is a link-once section that will be
   1338 		 discarded, then don't create any stubs.  */
   1339 	      if (section->output_section == NULL
   1340 		  || section->output_section->owner != output_bfd)
   1341 		continue;
   1342 
   1343 	      /* Get the relocs.  */
   1344 	      internal_relocs
   1345 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL,
   1346 					     NULL, info->keep_memory);
   1347 	      if (internal_relocs == NULL)
   1348 		goto error_ret_free_local;
   1349 
   1350 	      /* Now examine each relocation.  */
   1351 	      irela = internal_relocs;
   1352 	      irelaend = irela + section->reloc_count;
   1353 	      for (; irela < irelaend; irela++)
   1354 		{
   1355 		  unsigned int r_type, r_indx;
   1356 		  enum elf_kvx_stub_type stub_type;
   1357 		  struct elf_kvx_stub_hash_entry *stub_entry;
   1358 		  asection *sym_sec;
   1359 		  bfd_vma sym_value;
   1360 		  bfd_vma destination;
   1361 		  struct elf_kvx_link_hash_entry *hash;
   1362 		  const char *sym_name;
   1363 		  char *stub_name;
   1364 		  const asection *id_sec;
   1365 		  unsigned char st_type;
   1366 		  bfd_size_type len;
   1367 
   1368 		  r_type = ELFNN_R_TYPE (irela->r_info);
   1369 		  r_indx = ELFNN_R_SYM (irela->r_info);
   1370 
   1371 		  if (r_type >= (unsigned int) R_KVX_end)
   1372 		    {
   1373 		      bfd_set_error (bfd_error_bad_value);
   1374 		    error_ret_free_internal:
   1375 		      if (elf_section_data (section)->relocs == NULL)
   1376 			free (internal_relocs);
   1377 		      goto error_ret_free_local;
   1378 		    }
   1379 
   1380 		  /* Only look for stubs on unconditional branch and
   1381 		     branch and link instructions.  */
   1382 		  /* This catches CALL and GOTO insn */
   1383 		  if (r_type != (unsigned int) R_KVX_PCREL27)
   1384 		    continue;
   1385 
   1386 		  /* Now determine the call target, its name, value,
   1387 		     section.  */
   1388 		  sym_sec = NULL;
   1389 		  sym_value = 0;
   1390 		  destination = 0;
   1391 		  hash = NULL;
   1392 		  sym_name = NULL;
   1393 		  if (r_indx < symtab_hdr->sh_info)
   1394 		    {
   1395 		      /* It's a local symbol.  */
   1396 		      Elf_Internal_Sym *sym;
   1397 		      Elf_Internal_Shdr *hdr;
   1398 
   1399 		      if (local_syms == NULL)
   1400 			{
   1401 			  local_syms
   1402 			    = (Elf_Internal_Sym *) symtab_hdr->contents;
   1403 			  if (local_syms == NULL)
   1404 			    local_syms
   1405 			      = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   1406 						      symtab_hdr->sh_info, 0,
   1407 						      NULL, NULL, NULL);
   1408 			  if (local_syms == NULL)
   1409 			    goto error_ret_free_internal;
   1410 			}
   1411 
   1412 		      sym = local_syms + r_indx;
   1413 		      hdr = elf_elfsections (input_bfd)[sym->st_shndx];
   1414 		      sym_sec = hdr->bfd_section;
   1415 		      if (!sym_sec)
   1416 			/* This is an undefined symbol.  It can never
   1417 			   be resolved.  */
   1418 			continue;
   1419 
   1420 		      if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
   1421 			sym_value = sym->st_value;
   1422 		      destination = (sym_value + irela->r_addend
   1423 				     + sym_sec->output_offset
   1424 				     + sym_sec->output_section->vma);
   1425 		      st_type = ELF_ST_TYPE (sym->st_info);
   1426 		      sym_name
   1427 			= bfd_elf_string_from_elf_section (input_bfd,
   1428 							   symtab_hdr->sh_link,
   1429 							   sym->st_name);
   1430 		    }
   1431 		  else
   1432 		    {
   1433 		      int e_indx;
   1434 
   1435 		      e_indx = r_indx - symtab_hdr->sh_info;
   1436 		      hash = ((struct elf_kvx_link_hash_entry *)
   1437 			      elf_sym_hashes (input_bfd)[e_indx]);
   1438 
   1439 		      while (hash->root.root.type == bfd_link_hash_indirect
   1440 			     || hash->root.root.type == bfd_link_hash_warning)
   1441 			hash = ((struct elf_kvx_link_hash_entry *)
   1442 				hash->root.root.u.i.link);
   1443 
   1444 		      if (hash->root.root.type == bfd_link_hash_defined
   1445 			  || hash->root.root.type == bfd_link_hash_defweak)
   1446 			{
   1447 			  struct elf_kvx_link_hash_table *globals =
   1448 			    elf_kvx_hash_table (info);
   1449 			  sym_sec = hash->root.root.u.def.section;
   1450 			  sym_value = hash->root.root.u.def.value;
   1451 			  /* For a destination in a shared library,
   1452 			     use the PLT stub as target address to
   1453 			     decide whether a branch stub is
   1454 			     needed.  */
   1455 			  if (globals->root.splt != NULL && hash != NULL
   1456 			      && hash->root.plt.offset != (bfd_vma) - 1)
   1457 			    {
   1458 			      sym_sec = globals->root.splt;
   1459 			      sym_value = hash->root.plt.offset;
   1460 			      if (sym_sec->output_section != NULL)
   1461 				destination = (sym_value
   1462 					       + sym_sec->output_offset
   1463 					       + sym_sec->output_section->vma);
   1464 			    }
   1465 			  else if (sym_sec->output_section != NULL)
   1466 			    destination = (sym_value + irela->r_addend
   1467 					   + sym_sec->output_offset
   1468 					   + sym_sec->output_section->vma);
   1469 			}
   1470 		      else if (hash->root.root.type == bfd_link_hash_undefined
   1471 			       || (hash->root.root.type
   1472 				   == bfd_link_hash_undefweak))
   1473 			{
   1474 			  /* For a shared library, use the PLT stub as
   1475 			     target address to decide whether a long
   1476 			     branch stub is needed.
   1477 			     For absolute code, they cannot be handled.  */
   1478 			  struct elf_kvx_link_hash_table *globals =
   1479 			    elf_kvx_hash_table (info);
   1480 
   1481 			  if (globals->root.splt != NULL && hash != NULL
   1482 			      && hash->root.plt.offset != (bfd_vma) - 1)
   1483 			    {
   1484 			      sym_sec = globals->root.splt;
   1485 			      sym_value = hash->root.plt.offset;
   1486 			      if (sym_sec->output_section != NULL)
   1487 				destination = (sym_value
   1488 					       + sym_sec->output_offset
   1489 					       + sym_sec->output_section->vma);
   1490 			    }
   1491 			  else
   1492 			    continue;
   1493 			}
   1494 		      else
   1495 			{
   1496 			  bfd_set_error (bfd_error_bad_value);
   1497 			  goto error_ret_free_internal;
   1498 			}
   1499 		      st_type = ELF_ST_TYPE (hash->root.type);
   1500 		      sym_name = hash->root.root.root.string;
   1501 		    }
   1502 
   1503 		  /* Determine what (if any) linker stub is needed.  */
   1504 		  stub_type = kvx_type_of_stub (section, irela, sym_sec,
   1505 						st_type, destination);
   1506 		  if (stub_type == kvx_stub_none)
   1507 		    continue;
   1508 
   1509 		  /* Support for grouping stub sections.  */
   1510 		  id_sec = htab->stub_group[section->id].link_sec;
   1511 
   1512 		  /* Get the name of this stub.  */
   1513 		  stub_name = elfNN_kvx_stub_name (id_sec, sym_sec, hash,
   1514 						  irela);
   1515 		  if (!stub_name)
   1516 		    goto error_ret_free_internal;
   1517 
   1518 		  stub_entry =
   1519 		    kvx_stub_hash_lookup (&htab->stub_hash_table,
   1520 					 stub_name, false, false);
   1521 		  if (stub_entry != NULL)
   1522 		    {
   1523 		      /* The proper stub has already been created.  */
   1524 		      free (stub_name);
   1525 		      /* Always update this stub's target since it may have
   1526 			 changed after layout.  */
   1527 		      stub_entry->target_value = sym_value + irela->r_addend;
   1528 		      continue;
   1529 		    }
   1530 
   1531 		  stub_entry = _bfd_kvx_add_stub_entry_in_group
   1532 		    (stub_name, section, htab);
   1533 		  if (stub_entry == NULL)
   1534 		    {
   1535 		      free (stub_name);
   1536 		      goto error_ret_free_internal;
   1537 		    }
   1538 
   1539 		  stub_entry->target_value = sym_value + irela->r_addend;
   1540 		  stub_entry->target_section = sym_sec;
   1541 		  stub_entry->stub_type = stub_type;
   1542 		  stub_entry->h = hash;
   1543 		  stub_entry->st_type = st_type;
   1544 
   1545 		  if (sym_name == NULL)
   1546 		    sym_name = "unnamed";
   1547 		  len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
   1548 		  stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
   1549 		  if (stub_entry->output_name == NULL)
   1550 		    {
   1551 		      free (stub_name);
   1552 		      goto error_ret_free_internal;
   1553 		    }
   1554 
   1555 		  snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
   1556 			    sym_name);
   1557 
   1558 		  stub_changed = true;
   1559 		}
   1560 
   1561 	      /* We're done with the internal relocs, free them.  */
   1562 	      if (elf_section_data (section)->relocs == NULL)
   1563 		free (internal_relocs);
   1564 	    }
   1565 	}
   1566 
   1567       if (!stub_changed)
   1568 	break;
   1569 
   1570       _bfd_kvx_resize_stubs (htab);
   1571 
   1572       /* Ask the linker to do its stuff.  */
   1573       (*htab->layout_sections_again) ();
   1574       stub_changed = false;
   1575     }
   1576 
   1577   return true;
   1578 
   1579 error_ret_free_local:
   1580   return false;
   1581 
   1582 }
   1583 
   1584 /* Build all the stubs associated with the current output file.  The
   1585    stubs are kept in a hash table attached to the main linker hash
   1586    table.  We also set up the .plt entries for statically linked PIC
   1587    functions here.  This function is called via kvx_elf_finish in the
   1588    linker.  */
   1589 
   1590 bool
   1591 elfNN_kvx_build_stubs (struct bfd_link_info *info)
   1592 {
   1593   asection *stub_sec;
   1594   struct bfd_hash_table *table;
   1595   struct elf_kvx_link_hash_table *htab;
   1596 
   1597   htab = elf_kvx_hash_table (info);
   1598 
   1599   for (stub_sec = htab->stub_bfd->sections;
   1600        stub_sec != NULL; stub_sec = stub_sec->next)
   1601     {
   1602       bfd_size_type size;
   1603 
   1604       /* Ignore non-stub sections.  */
   1605       if (!strstr (stub_sec->name, STUB_SUFFIX))
   1606 	continue;
   1607 
   1608       /* Allocate memory to hold the linker stubs.  */
   1609       size = stub_sec->size;
   1610       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
   1611       if (stub_sec->contents == NULL && size != 0)
   1612 	return false;
   1613       stub_sec->alloced = 1;
   1614       stub_sec->size = 0;
   1615     }
   1616 
   1617   /* Build the stubs as directed by the stub hash table.  */
   1618   table = &htab->stub_hash_table;
   1619   bfd_hash_traverse (table, kvx_build_one_stub, info);
   1620 
   1621   return true;
   1622 }
   1623 
   1624 static bfd_vma
   1625 kvx_calculate_got_entry_vma (struct elf_link_hash_entry *h,
   1626 				 struct elf_kvx_link_hash_table
   1627 				 *globals, struct bfd_link_info *info,
   1628 				 bfd_vma value, bfd *output_bfd,
   1629 				 bool *unresolved_reloc_p)
   1630 {
   1631   bfd_vma off = (bfd_vma) - 1;
   1632   asection *basegot = globals->root.sgot;
   1633   bool dyn = globals->root.dynamic_sections_created;
   1634 
   1635   if (h != NULL)
   1636     {
   1637       BFD_ASSERT (basegot != NULL);
   1638       off = h->got.offset;
   1639       BFD_ASSERT (off != (bfd_vma) - 1);
   1640       if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
   1641 	  || (bfd_link_pic (info)
   1642 	      && SYMBOL_REFERENCES_LOCAL (info, h))
   1643 	  || (ELF_ST_VISIBILITY (h->other)
   1644 	      && h->root.type == bfd_link_hash_undefweak))
   1645 	{
   1646 	  /* This is actually a static link, or it is a -Bsymbolic link
   1647 	     and the symbol is defined locally.  We must initialize this
   1648 	     entry in the global offset table.  Since the offset must
   1649 	     always be a multiple of 8 (4 in the case of ILP32), we use
   1650 	     the least significant bit to record whether we have
   1651 	     initialized it already.
   1652 	     When doing a dynamic link, we create a .rel(a).got relocation
   1653 	     entry to initialize the value.  This is done in the
   1654 	     finish_dynamic_symbol routine.  */
   1655 	  if ((off & 1) != 0)
   1656 	    off &= ~1;
   1657 	  else
   1658 	    {
   1659 	      bfd_put_NN (output_bfd, value, basegot->contents + off);
   1660 	      h->got.offset |= 1;
   1661 	    }
   1662 	}
   1663       else
   1664 	*unresolved_reloc_p = false;
   1665     }
   1666 
   1667   return off;
   1668 }
   1669 
   1670 static unsigned int
   1671 kvx_reloc_got_type (bfd_reloc_code_real_type r_type)
   1672 {
   1673   switch (r_type)
   1674     {
   1675       /* Extracted with:
   1676 	 awk 'match ($0, /HOWTO.*R_(KVX.*_GOT(OFF)?(64)?_.*),/,ary) \
   1677 	 {print "case BFD_RELOC_" ary[1] ":";}' elfxx-kvxc.def  */
   1678     case BFD_RELOC_KVX_S37_GOTOFF_LO10:
   1679     case BFD_RELOC_KVX_S37_GOTOFF_UP27:
   1680 
   1681     case BFD_RELOC_KVX_S37_GOT_LO10:
   1682     case BFD_RELOC_KVX_S37_GOT_UP27:
   1683 
   1684     case BFD_RELOC_KVX_S43_GOTOFF_LO10:
   1685     case BFD_RELOC_KVX_S43_GOTOFF_UP27:
   1686     case BFD_RELOC_KVX_S43_GOTOFF_EX6:
   1687 
   1688     case BFD_RELOC_KVX_S43_GOT_LO10:
   1689     case BFD_RELOC_KVX_S43_GOT_UP27:
   1690     case BFD_RELOC_KVX_S43_GOT_EX6:
   1691       return GOT_NORMAL;
   1692 
   1693     case BFD_RELOC_KVX_S37_TLS_GD_LO10:
   1694     case BFD_RELOC_KVX_S37_TLS_GD_UP27:
   1695     case BFD_RELOC_KVX_S43_TLS_GD_LO10:
   1696     case BFD_RELOC_KVX_S43_TLS_GD_UP27:
   1697     case BFD_RELOC_KVX_S43_TLS_GD_EX6:
   1698       return GOT_TLS_GD;
   1699 
   1700     case BFD_RELOC_KVX_S37_TLS_LD_LO10:
   1701     case BFD_RELOC_KVX_S37_TLS_LD_UP27:
   1702     case BFD_RELOC_KVX_S43_TLS_LD_LO10:
   1703     case BFD_RELOC_KVX_S43_TLS_LD_UP27:
   1704     case BFD_RELOC_KVX_S43_TLS_LD_EX6:
   1705       return GOT_TLS_LD;
   1706 
   1707     case BFD_RELOC_KVX_S37_TLS_IE_LO10:
   1708     case BFD_RELOC_KVX_S37_TLS_IE_UP27:
   1709     case BFD_RELOC_KVX_S43_TLS_IE_LO10:
   1710     case BFD_RELOC_KVX_S43_TLS_IE_UP27:
   1711     case BFD_RELOC_KVX_S43_TLS_IE_EX6:
   1712       return GOT_TLS_IE;
   1713 
   1714     default:
   1715       break;
   1716     }
   1717   return GOT_UNKNOWN;
   1718 }
   1719 
   1720 static bool
   1721 kvx_can_relax_tls (bfd *input_bfd ATTRIBUTE_UNUSED,
   1722 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
   1723 		       bfd_reloc_code_real_type r_type ATTRIBUTE_UNUSED,
   1724 		       struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
   1725 		       unsigned long r_symndx ATTRIBUTE_UNUSED)
   1726 {
   1727   if (! IS_KVX_TLS_RELAX_RELOC (r_type))
   1728     return false;
   1729 
   1730   /* Relaxing hook. Disabled on KVX. */
   1731   /* See elfnn-aarch64.c */
   1732   return true;
   1733 }
   1734 
   1735 /* Given the relocation code R_TYPE, return the relaxed bfd reloc
   1736    enumerator.  */
   1737 
   1738 static bfd_reloc_code_real_type
   1739 kvx_tls_transition (bfd *input_bfd,
   1740 			struct bfd_link_info *info,
   1741 			unsigned int r_type,
   1742 			struct elf_link_hash_entry *h,
   1743 			unsigned long r_symndx)
   1744 {
   1745   bfd_reloc_code_real_type bfd_r_type
   1746     = elfNN_kvx_bfd_reloc_from_type (input_bfd, r_type);
   1747 
   1748   if (! kvx_can_relax_tls (input_bfd, info, bfd_r_type, h, r_symndx))
   1749     return bfd_r_type;
   1750 
   1751   return bfd_r_type;
   1752 }
   1753 
   1754 /* Return the base VMA address which should be subtracted from real addresses
   1755    when resolving R_KVX_*_TLS_GD_* and R_KVX_*_TLS_LD_* relocation.  */
   1756 
   1757 static bfd_vma
   1758 dtpoff_base (struct bfd_link_info *info)
   1759 {
   1760   /* If tls_sec is NULL, we should have signalled an error already.  */
   1761   BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
   1762   return elf_hash_table (info)->tls_sec->vma;
   1763 }
   1764 
   1765 /* Return the base VMA address which should be subtracted from real addresses
   1766    when resolving R_KVX_*_TLS_IE_* and R_KVX_*_TLS_LE_* relocations.  */
   1767 
   1768 static bfd_vma
   1769 tpoff_base (struct bfd_link_info *info)
   1770 {
   1771   struct elf_link_hash_table *htab = elf_hash_table (info);
   1772 
   1773   /* If tls_sec is NULL, we should have signalled an error already.  */
   1774   BFD_ASSERT (htab->tls_sec != NULL);
   1775 
   1776   bfd_vma base = align_power ((bfd_vma) 0,
   1777 			      htab->tls_sec->alignment_power);
   1778   return htab->tls_sec->vma - base;
   1779 }
   1780 
   1781 static bfd_vma *
   1782 symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
   1783 		       unsigned long r_symndx)
   1784 {
   1785   /* Calculate the address of the GOT entry for symbol
   1786      referred to in h.  */
   1787   if (h != NULL)
   1788     return &h->got.offset;
   1789   else
   1790     {
   1791       /* local symbol */
   1792       struct elf_kvx_local_symbol *l;
   1793 
   1794       l = elf_kvx_locals (input_bfd);
   1795       return &l[r_symndx].got_offset;
   1796     }
   1797 }
   1798 
   1799 static void
   1800 symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
   1801 			unsigned long r_symndx)
   1802 {
   1803   bfd_vma *p;
   1804   p = symbol_got_offset_ref (input_bfd, h, r_symndx);
   1805   *p |= 1;
   1806 }
   1807 
   1808 static int
   1809 symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
   1810 			  unsigned long r_symndx)
   1811 {
   1812   bfd_vma value;
   1813   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
   1814   return value & 1;
   1815 }
   1816 
   1817 static bfd_vma
   1818 symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
   1819 		   unsigned long r_symndx)
   1820 {
   1821   bfd_vma value;
   1822   value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
   1823   value &= ~1;
   1824   return value;
   1825 }
   1826 
   1827 /* N_ONES produces N one bits, without overflowing machine arithmetic.  */
   1828 #define N_ONES(n) (((((bfd_vma) 1 << ((n) -1)) - 1) << 1) | 1)
   1829 
   1830 /* This is a copy/paste + modification from
   1831    reloc.c:_bfd_relocate_contents. Relocations are applied to 32bits
   1832    words, so all overflow checks will overflow for values above
   1833    32bits.  */
   1834 static bfd_reloc_status_type
   1835 check_signed_overflow (enum complain_overflow complain_on_overflow,
   1836 		       bfd_reloc_code_real_type bfd_r_type, bfd *input_bfd,
   1837 		       bfd_vma relocation)
   1838 {
   1839   bfd_reloc_status_type flag = bfd_reloc_ok;
   1840   bfd_vma addrmask, fieldmask, signmask, ss;
   1841   bfd_vma a, b, sum;
   1842   bfd_vma x = 0;
   1843 
   1844   /* These usually come from howto struct. As we don't check for
   1845      values fitting in bitfields or in subpart of words, we set all
   1846      these to values to check as if the field is starting from first
   1847      bit.  */
   1848   unsigned int rightshift = 0;
   1849   unsigned int bitpos = 0;
   1850   unsigned int bitsize = 0;
   1851   bfd_vma src_mask = -1;
   1852 
   1853   /* Only regular symbol relocations are checked here. Others
   1854      relocations (GOT, TLS) could be checked if the need is
   1855      confirmed. At the moment, we keep previous behavior
   1856      (ie. unchecked) for those. */
   1857   switch (bfd_r_type)
   1858     {
   1859     case BFD_RELOC_KVX_S37_LO10:
   1860     case BFD_RELOC_KVX_S37_UP27:
   1861       bitsize = 37;
   1862       break;
   1863 
   1864     case BFD_RELOC_KVX_S32_LO5:
   1865     case BFD_RELOC_KVX_S32_UP27:
   1866       bitsize = 32;
   1867       break;
   1868 
   1869     case BFD_RELOC_KVX_S43_LO10:
   1870     case BFD_RELOC_KVX_S43_UP27:
   1871     case BFD_RELOC_KVX_S43_EX6:
   1872       bitsize = 43;
   1873       break;
   1874 
   1875     case BFD_RELOC_KVX_S64_LO10:
   1876     case BFD_RELOC_KVX_S64_UP27:
   1877     case BFD_RELOC_KVX_S64_EX27:
   1878       bitsize = 64;
   1879       break;
   1880 
   1881     default:
   1882       return bfd_reloc_ok;
   1883     }
   1884 
   1885   /* direct copy/paste from reloc.c below */
   1886 
   1887   /* Get the values to be added together.  For signed and unsigned
   1888      relocations, we assume that all values should be truncated to
   1889      the size of an address.  For bitfields, all the bits matter.
   1890      See also bfd_check_overflow.  */
   1891   fieldmask = N_ONES (bitsize);
   1892   signmask = ~fieldmask;
   1893   addrmask = (N_ONES (bfd_arch_bits_per_address (input_bfd))
   1894 	      | (fieldmask << rightshift));
   1895   a = (relocation & addrmask) >> rightshift;
   1896   b = (x & src_mask & addrmask) >> bitpos;
   1897   addrmask >>= rightshift;
   1898 
   1899   switch (complain_on_overflow)
   1900     {
   1901     case complain_overflow_signed:
   1902       /* If any sign bits are set, all sign bits must be set.
   1903 	 That is, A must be a valid negative address after
   1904 	 shifting.  */
   1905       signmask = ~(fieldmask >> 1);
   1906       /* Fall thru */
   1907 
   1908     case complain_overflow_bitfield:
   1909       /* Much like the signed check, but for a field one bit
   1910 	 wider.  We allow a bitfield to represent numbers in the
   1911 	 range -2**n to 2**n-1, where n is the number of bits in the
   1912 	 field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
   1913 	 can't overflow, which is exactly what we want.  */
   1914       ss = a & signmask;
   1915       if (ss != 0 && ss != (addrmask & signmask))
   1916 	flag = bfd_reloc_overflow;
   1917 
   1918       /* We only need this next bit of code if the sign bit of B
   1919 	 is below the sign bit of A.  This would only happen if
   1920 	 SRC_MASK had fewer bits than BITSIZE.  Note that if
   1921 	 SRC_MASK has more bits than BITSIZE, we can get into
   1922 	 trouble; we would need to verify that B is in range, as
   1923 	 we do for A above.  */
   1924       ss = ((~src_mask) >> 1) & src_mask;
   1925       ss >>= bitpos;
   1926 
   1927       /* Set all the bits above the sign bit.  */
   1928       b = (b ^ ss) - ss;
   1929 
   1930       /* Now we can do the addition.  */
   1931       sum = a + b;
   1932 
   1933       /* See if the result has the correct sign.  Bits above the
   1934 	 sign bit are junk now; ignore them.  If the sum is
   1935 	 positive, make sure we did not have all negative inputs;
   1936 	 if the sum is negative, make sure we did not have all
   1937 	 positive inputs.  The test below looks only at the sign
   1938 	 bits, and it really just
   1939 	 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
   1940 
   1941 	 We mask with addrmask here to explicitly allow an address
   1942 	 wrap-around.  The Linux kernel relies on it, and it is
   1943 	 the only way to write assembler code which can run when
   1944 	 loaded at a location 0x80000000 away from the location at
   1945 	 which it is linked.  */
   1946       if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
   1947 	flag = bfd_reloc_overflow;
   1948       break;
   1949 
   1950     case complain_overflow_unsigned:
   1951       /* Checking for an unsigned overflow is relatively easy:
   1952 	 trim the addresses and add, and trim the result as well.
   1953 	 Overflow is normally indicated when the result does not
   1954 	 fit in the field.  However, we also need to consider the
   1955 	 case when, e.g., fieldmask is 0x7fffffff or smaller, an
   1956 	 input is 0x80000000, and bfd_vma is only 32 bits; then we
   1957 	 will get sum == 0, but there is an overflow, since the
   1958 	 inputs did not fit in the field.  Instead of doing a
   1959 	 separate test, we can check for this by or-ing in the
   1960 	 operands when testing for the sum overflowing its final
   1961 	 field.  */
   1962       sum = (a + b) & addrmask;
   1963       if ((a | b | sum) & signmask)
   1964 	flag = bfd_reloc_overflow;
   1965       break;
   1966 
   1967     default:
   1968       abort ();
   1969     }
   1970   return flag;
   1971 }
   1972 
   1973 /* Perform a relocation as part of a final link.  */
   1974 static bfd_reloc_status_type
   1975 elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
   1976 			       bfd *input_bfd,
   1977 			       bfd *output_bfd,
   1978 			       asection *input_section,
   1979 			       bfd_byte *contents,
   1980 			       Elf_Internal_Rela *rel,
   1981 			       bfd_vma value,
   1982 			       struct bfd_link_info *info,
   1983 			       asection *sym_sec,
   1984 			       struct elf_link_hash_entry *h,
   1985 			       bool *unresolved_reloc_p,
   1986 			       bool save_addend,
   1987 			       bfd_vma *saved_addend,
   1988 			       Elf_Internal_Sym *sym)
   1989 {
   1990   Elf_Internal_Shdr *symtab_hdr;
   1991   unsigned int r_type = howto->type;
   1992   bfd_reloc_code_real_type bfd_r_type
   1993     = elfNN_kvx_bfd_reloc_from_howto (howto);
   1994   bfd_reloc_code_real_type new_bfd_r_type;
   1995   unsigned long r_symndx;
   1996   bfd_byte *hit_data = contents + rel->r_offset;
   1997   bfd_vma place, off;
   1998   bfd_vma addend;
   1999   struct elf_kvx_link_hash_table *globals;
   2000   bool weak_undef_p;
   2001   asection *base_got;
   2002   bfd_reloc_status_type rret = bfd_reloc_ok;
   2003   bool resolved_to_zero;
   2004   globals = elf_kvx_hash_table (info);
   2005 
   2006   symtab_hdr = &elf_symtab_hdr (input_bfd);
   2007 
   2008   BFD_ASSERT (is_kvx_elf (input_bfd));
   2009 
   2010   r_symndx = ELFNN_R_SYM (rel->r_info);
   2011 
   2012   /* It is possible to have linker relaxations on some TLS access
   2013      models.  Update our information here.  */
   2014   new_bfd_r_type = kvx_tls_transition (input_bfd, info, r_type, h, r_symndx);
   2015   if (new_bfd_r_type != bfd_r_type)
   2016     {
   2017       bfd_r_type = new_bfd_r_type;
   2018       howto = elfNN_kvx_howto_from_bfd_reloc (bfd_r_type);
   2019       BFD_ASSERT (howto != NULL);
   2020       r_type = howto->type;
   2021     }
   2022 
   2023   place = input_section->output_section->vma
   2024     + input_section->output_offset + rel->r_offset;
   2025 
   2026   /* Get addend, accumulating the addend for consecutive relocs
   2027      which refer to the same offset.  */
   2028   addend = saved_addend ? *saved_addend : 0;
   2029   addend += rel->r_addend;
   2030 
   2031   weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
   2032 		  : bfd_is_und_section (sym_sec));
   2033   resolved_to_zero = (h != NULL
   2034 		      && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
   2035 
   2036   switch (bfd_r_type)
   2037     {
   2038     case BFD_RELOC_KVX_NN:
   2039 #if ARCH_SIZE == 64
   2040     case BFD_RELOC_KVX_32:
   2041 #endif
   2042     case BFD_RELOC_KVX_S37_LO10:
   2043     case BFD_RELOC_KVX_S37_UP27:
   2044 
   2045     case BFD_RELOC_KVX_S32_LO5:
   2046     case BFD_RELOC_KVX_S32_UP27:
   2047 
   2048     case BFD_RELOC_KVX_S43_LO10:
   2049     case BFD_RELOC_KVX_S43_UP27:
   2050     case BFD_RELOC_KVX_S43_EX6:
   2051 
   2052     case BFD_RELOC_KVX_S64_LO10:
   2053     case BFD_RELOC_KVX_S64_UP27:
   2054     case BFD_RELOC_KVX_S64_EX27:
   2055       /* When generating a shared library or PIE, these relocations
   2056 	 are copied into the output file to be resolved at run time.  */
   2057       if (bfd_link_pic (info)
   2058 	  && (input_section->flags & SEC_ALLOC)
   2059 	  && (h == NULL
   2060 	      || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2061 		  && !resolved_to_zero)
   2062 	      || h->root.type != bfd_link_hash_undefweak))
   2063 	{
   2064 	  Elf_Internal_Rela outrel;
   2065 	  bfd_byte *loc;
   2066 	  bool skip, relocate;
   2067 	  asection *sreloc;
   2068 
   2069 	  *unresolved_reloc_p = false;
   2070 
   2071 	  skip = false;
   2072 	  relocate = false;
   2073 
   2074 	  outrel.r_addend = addend;
   2075 	  outrel.r_offset =
   2076 	    _bfd_elf_section_offset (output_bfd, info, input_section,
   2077 				     rel->r_offset);
   2078 	  if (outrel.r_offset == (bfd_vma) - 1)
   2079 	    skip = true;
   2080 	  else if (outrel.r_offset == (bfd_vma) - 2)
   2081 	    {
   2082 	      skip = true;
   2083 	      relocate = true;
   2084 	    }
   2085 
   2086 	  outrel.r_offset += (input_section->output_section->vma
   2087 			      + input_section->output_offset);
   2088 
   2089 	  if (skip)
   2090 	    memset (&outrel, 0, sizeof outrel);
   2091 	  else if (h != NULL
   2092 		   && h->dynindx != -1
   2093 		   && (!bfd_link_pic (info) || !info->symbolic
   2094 		       || !h->def_regular))
   2095 	    outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
   2096 	  else if (bfd_r_type == BFD_RELOC_KVX_32
   2097 		   || bfd_r_type == BFD_RELOC_KVX_64)
   2098 	    {
   2099 	      int symbol;
   2100 
   2101 	      /* On SVR4-ish systems, the dynamic loader cannot
   2102 		 relocate the text and data segments independently,
   2103 		 so the symbol does not matter.  */
   2104 	      symbol = 0;
   2105 	      outrel.r_info = ELFNN_R_INFO (symbol, R_KVX_RELATIVE);
   2106 	      outrel.r_addend += value;
   2107 	    }
   2108 	  else if (bfd_link_pic (info) && info->symbolic)
   2109 	    {
   2110 	      goto skip_because_pic;
   2111 	    }
   2112 	  else
   2113 	    {
   2114 	      /* We may endup here from bad input code trying to
   2115 		 insert relocation on symbols within code.  We do not
   2116 		 want that currently, and such code should use GOT +
   2117 		 KVX_32/64 reloc that translate in KVX_RELATIVE.  */
   2118 	      const char *name;
   2119 	      if (h && h->root.root.string)
   2120 		name = h->root.root.string;
   2121 	      else
   2122 		name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
   2123 					 NULL);
   2124 
   2125 	      (*_bfd_error_handler)
   2126 		/* xgettext:c-format */
   2127 		(_("%pB(%pA+%#" PRIx64 "): "
   2128 		   "unresolvable %s relocation in section `%s'"),
   2129 		 input_bfd, input_section, (uint64_t) rel->r_offset, howto->name,
   2130 		 name);
   2131 	      return bfd_reloc_notsupported;
   2132 	    }
   2133 
   2134 	  sreloc = elf_section_data (input_section)->sreloc;
   2135 	  if (sreloc == NULL || sreloc->contents == NULL)
   2136 	    return bfd_reloc_notsupported;
   2137 
   2138 	  loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
   2139 	  bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
   2140 
   2141 	  if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
   2142 	    {
   2143 	      /* Sanity to check that we have previously allocated
   2144 		 sufficient space in the relocation section for the
   2145 		 number of relocations we actually want to emit.  */
   2146 	      abort ();
   2147 	    }
   2148 
   2149 	  /* If this reloc is against an external symbol, we do not want to
   2150 	     fiddle with the addend.  Otherwise, we need to include the symbol
   2151 	     value so that it becomes an addend for the dynamic reloc.  */
   2152 	  if (!relocate)
   2153 	    return bfd_reloc_ok;
   2154 
   2155 	  rret = check_signed_overflow (complain_overflow_signed, bfd_r_type,
   2156 					input_bfd, value + addend);
   2157 	  if (rret != bfd_reloc_ok)
   2158 	    return rret;
   2159 
   2160 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2161 					   contents, rel->r_offset, value,
   2162 					   addend);
   2163 	}
   2164 
   2165     skip_because_pic:
   2166       rret = check_signed_overflow (complain_overflow_signed, bfd_r_type,
   2167 				    input_bfd, value + addend);
   2168       if (rret != bfd_reloc_ok)
   2169 	return rret;
   2170 
   2171       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2172 				       contents, rel->r_offset, value,
   2173 				       addend);
   2174       break;
   2175 
   2176     case BFD_RELOC_KVX_PCREL17:
   2177     case BFD_RELOC_KVX_PCREL27:
   2178       {
   2179 	/* BCU insn are always first in a bundle, so there is no need
   2180 	   to correct the address using offset within bundle.  */
   2181 
   2182 	asection *splt = globals->root.splt;
   2183 	bool via_plt_p =
   2184 	  splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
   2185 
   2186 	/* A call to an undefined weak symbol is converted to a jump to
   2187 	   the next instruction unless a PLT entry will be created.
   2188 	   The jump to the next instruction is optimized as a NOP.
   2189 	   Do the same for local undefined symbols.  */
   2190 	if (weak_undef_p && ! via_plt_p)
   2191 	  {
   2192 	    bfd_putl32 (INSN_NOP, hit_data);
   2193 	    return bfd_reloc_ok;
   2194 	  }
   2195 
   2196 	/* If the call goes through a PLT entry, make sure to
   2197 	   check distance to the right destination address.  */
   2198 	if (via_plt_p)
   2199 	  value = (splt->output_section->vma
   2200 		   + splt->output_offset + h->plt.offset);
   2201 
   2202 	/* Check if a stub has to be inserted because the destination
   2203 	   is too far away.  */
   2204 	struct elf_kvx_stub_hash_entry *stub_entry = NULL;
   2205 
   2206 	/* If the target symbol is global and marked as a function the
   2207 	   relocation applies a function call or a tail call.  In this
   2208 	   situation we can veneer out of range branches.  The veneers
   2209 	   use R16 and R17 hence cannot be used arbitrary out of range
   2210 	   branches that occur within the body of a function.  */
   2211 
   2212 	/* Check if a stub has to be inserted because the destination
   2213 	   is too far away.  */
   2214 	if (! kvx_valid_call_p (value, place))
   2215 	  {
   2216 	    /* The target is out of reach, so redirect the branch to
   2217 	       the local stub for this function.  */
   2218 	    stub_entry = elfNN_kvx_get_stub_entry (input_section,
   2219 						   sym_sec, h,
   2220 						   rel, globals);
   2221 	    if (stub_entry != NULL)
   2222 	      value = (stub_entry->stub_offset
   2223 		       + stub_entry->stub_sec->output_offset
   2224 		       + stub_entry->stub_sec->output_section->vma);
   2225 	    /* We have redirected the destination to stub entry address,
   2226 	       so ignore any addend record in the original rela entry.  */
   2227 	    addend = 0;
   2228 	  }
   2229       }
   2230       *unresolved_reloc_p = false;
   2231 
   2232       /* FALLTHROUGH */
   2233 
   2234       /* PCREL 32 are used in dwarf2 table for exception handling */
   2235     case BFD_RELOC_KVX_32_PCREL:
   2236     case BFD_RELOC_KVX_S64_PCREL_LO10:
   2237     case BFD_RELOC_KVX_S64_PCREL_UP27:
   2238     case BFD_RELOC_KVX_S64_PCREL_EX27:
   2239     case BFD_RELOC_KVX_S37_PCREL_LO10:
   2240     case BFD_RELOC_KVX_S37_PCREL_UP27:
   2241     case BFD_RELOC_KVX_S43_PCREL_LO10:
   2242     case BFD_RELOC_KVX_S43_PCREL_UP27:
   2243     case BFD_RELOC_KVX_S43_PCREL_EX6:
   2244       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2245 				       contents, rel->r_offset, value,
   2246 				       addend);
   2247       break;
   2248 
   2249     case BFD_RELOC_KVX_S37_TLS_LE_LO10:
   2250     case BFD_RELOC_KVX_S37_TLS_LE_UP27:
   2251 
   2252     case BFD_RELOC_KVX_S43_TLS_LE_LO10:
   2253     case BFD_RELOC_KVX_S43_TLS_LE_UP27:
   2254     case BFD_RELOC_KVX_S43_TLS_LE_EX6:
   2255       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2256 				       contents, rel->r_offset,
   2257 				       value - tpoff_base (info), addend);
   2258       break;
   2259 
   2260     case BFD_RELOC_KVX_S37_TLS_DTPOFF_LO10:
   2261     case BFD_RELOC_KVX_S37_TLS_DTPOFF_UP27:
   2262 
   2263     case BFD_RELOC_KVX_S43_TLS_DTPOFF_LO10:
   2264     case BFD_RELOC_KVX_S43_TLS_DTPOFF_UP27:
   2265     case BFD_RELOC_KVX_S43_TLS_DTPOFF_EX6:
   2266       return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2267 				       contents, rel->r_offset,
   2268 				       value - dtpoff_base (info), addend);
   2269 
   2270     case BFD_RELOC_KVX_S37_TLS_GD_UP27:
   2271     case BFD_RELOC_KVX_S37_TLS_GD_LO10:
   2272 
   2273     case BFD_RELOC_KVX_S43_TLS_GD_UP27:
   2274     case BFD_RELOC_KVX_S43_TLS_GD_EX6:
   2275     case BFD_RELOC_KVX_S43_TLS_GD_LO10:
   2276 
   2277     case BFD_RELOC_KVX_S37_TLS_IE_UP27:
   2278     case BFD_RELOC_KVX_S37_TLS_IE_LO10:
   2279 
   2280     case BFD_RELOC_KVX_S43_TLS_IE_UP27:
   2281     case BFD_RELOC_KVX_S43_TLS_IE_EX6:
   2282     case BFD_RELOC_KVX_S43_TLS_IE_LO10:
   2283 
   2284     case BFD_RELOC_KVX_S37_TLS_LD_UP27:
   2285     case BFD_RELOC_KVX_S37_TLS_LD_LO10:
   2286 
   2287     case BFD_RELOC_KVX_S43_TLS_LD_UP27:
   2288     case BFD_RELOC_KVX_S43_TLS_LD_EX6:
   2289     case BFD_RELOC_KVX_S43_TLS_LD_LO10:
   2290 
   2291       if (globals->root.sgot == NULL)
   2292 	return bfd_reloc_notsupported;
   2293       value = symbol_got_offset (input_bfd, h, r_symndx);
   2294 
   2295       _bfd_final_link_relocate (howto, input_bfd, input_section,
   2296 				contents, rel->r_offset, value, addend);
   2297       *unresolved_reloc_p = false;
   2298       break;
   2299 
   2300     case BFD_RELOC_KVX_S37_GOTADDR_UP27:
   2301     case BFD_RELOC_KVX_S37_GOTADDR_LO10:
   2302 
   2303     case BFD_RELOC_KVX_S43_GOTADDR_UP27:
   2304     case BFD_RELOC_KVX_S43_GOTADDR_EX6:
   2305     case BFD_RELOC_KVX_S43_GOTADDR_LO10:
   2306 
   2307     case BFD_RELOC_KVX_S64_GOTADDR_UP27:
   2308     case BFD_RELOC_KVX_S64_GOTADDR_EX27:
   2309     case BFD_RELOC_KVX_S64_GOTADDR_LO10:
   2310       {
   2311 	if (globals->root.sgot == NULL)
   2312 	  BFD_ASSERT (h != NULL);
   2313 
   2314 	value = globals->root.sgot->output_section->vma
   2315 	  + globals->root.sgot->output_offset;
   2316 
   2317 	return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2318 					 contents, rel->r_offset, value,
   2319 					 addend);
   2320       }
   2321       break;
   2322 
   2323     case BFD_RELOC_KVX_S37_GOTOFF_LO10:
   2324     case BFD_RELOC_KVX_S37_GOTOFF_UP27:
   2325 
   2326     case BFD_RELOC_KVX_32_GOTOFF:
   2327     case BFD_RELOC_KVX_64_GOTOFF:
   2328 
   2329     case BFD_RELOC_KVX_S43_GOTOFF_LO10:
   2330     case BFD_RELOC_KVX_S43_GOTOFF_UP27:
   2331     case BFD_RELOC_KVX_S43_GOTOFF_EX6:
   2332 
   2333       {
   2334 	asection *basegot = globals->root.sgot;
   2335 	/* BFD_ASSERT(h == NULL); */
   2336 	BFD_ASSERT(globals->root.sgot != NULL);
   2337 	value -= basegot->output_section->vma + basegot->output_offset;
   2338 	return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2339 					 contents, rel->r_offset, value,
   2340 					 addend);
   2341       }
   2342       break;
   2343 
   2344     case BFD_RELOC_KVX_S37_GOT_LO10:
   2345     case BFD_RELOC_KVX_S37_GOT_UP27:
   2346 
   2347     case BFD_RELOC_KVX_32_GOT:
   2348     case BFD_RELOC_KVX_64_GOT:
   2349 
   2350     case BFD_RELOC_KVX_S43_GOT_LO10:
   2351     case BFD_RELOC_KVX_S43_GOT_UP27:
   2352     case BFD_RELOC_KVX_S43_GOT_EX6:
   2353 
   2354       if (globals->root.sgot == NULL)
   2355 	BFD_ASSERT (h != NULL);
   2356 
   2357       if (h != NULL)
   2358 	{
   2359 	  value = kvx_calculate_got_entry_vma (h, globals, info, value,
   2360 					       output_bfd,
   2361 					       unresolved_reloc_p);
   2362 #ifdef UGLY_DEBUG
   2363 	  printf("GOT_LO/HI for %s, value %x\n", h->root.root.string, value);
   2364 #endif
   2365 
   2366 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2367 					   contents, rel->r_offset, value,
   2368 					   addend);
   2369 	}
   2370       else
   2371 	{
   2372 #ifdef UGLY_DEBUG
   2373 	  printf("GOT_LO/HI with h NULL, initial value %x\n", value);
   2374 #endif
   2375 	  struct elf_kvx_local_symbol *locals = elf_kvx_locals (input_bfd);
   2376 
   2377 	  if (locals == NULL)
   2378 	    {
   2379 	      int howto_index = bfd_r_type - BFD_RELOC_KVX_RELOC_START;
   2380 	      _bfd_error_handler
   2381 		/* xgettext:c-format */
   2382 		(_("%pB: local symbol descriptor table be NULL when applying "
   2383 		   "relocation %s against local symbol"),
   2384 		 input_bfd, elf_kvx_howto_table[howto_index].name);
   2385 	      abort ();
   2386 	    }
   2387 
   2388 	  off = symbol_got_offset (input_bfd, h, r_symndx);
   2389 	  base_got = globals->root.sgot;
   2390 	  bfd_vma got_entry_addr = (base_got->output_section->vma
   2391 				    + base_got->output_offset + off);
   2392 
   2393 	  if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
   2394 	    {
   2395 	      bfd_put_64 (output_bfd, value, base_got->contents + off);
   2396 
   2397 	      if (bfd_link_pic (info))
   2398 		{
   2399 		  asection *s;
   2400 		  Elf_Internal_Rela outrel;
   2401 
   2402 		  /* For PIC executables and shared libraries we need
   2403 		     to relocate the GOT entry at run time.  */
   2404 		  s = globals->root.srelgot;
   2405 		  if (s == NULL)
   2406 		    abort ();
   2407 
   2408 		  outrel.r_offset = got_entry_addr;
   2409 		  outrel.r_info = ELFNN_R_INFO (0, R_KVX_RELATIVE);
   2410 		  outrel.r_addend = value;
   2411 		  _bfd_elf_append_rela (output_bfd, s, &outrel);
   2412 		}
   2413 
   2414 	      symbol_got_offset_mark (input_bfd, h, r_symndx);
   2415 	    }
   2416 
   2417 	  /* Update the relocation value to GOT entry addr as we have
   2418 	     transformed the direct data access into an indirect data
   2419 	     access through GOT.  */
   2420 	  value = got_entry_addr;
   2421 
   2422 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
   2423 					   contents, rel->r_offset, off, 0);
   2424 	}
   2425       break;
   2426 
   2427     default:
   2428       return bfd_reloc_notsupported;
   2429     }
   2430 
   2431   if (saved_addend)
   2432     *saved_addend = value;
   2433 
   2434   /* Only apply the final relocation in a sequence.  */
   2435   if (save_addend)
   2436     return bfd_reloc_continue;
   2437 
   2438   return _bfd_kvx_elf_put_addend (input_bfd, hit_data, bfd_r_type,
   2439 				  howto, value);
   2440 }
   2441 
   2442 
   2443 
   2444 /* Relocate a KVX ELF section.  */
   2445 
   2446 static int
   2447 elfNN_kvx_relocate_section (bfd *output_bfd,
   2448 			    struct bfd_link_info *info,
   2449 			    bfd *input_bfd,
   2450 			    asection *input_section,
   2451 			    bfd_byte *contents,
   2452 			    Elf_Internal_Rela *relocs,
   2453 			    Elf_Internal_Sym *local_syms,
   2454 			    asection **local_sections)
   2455 {
   2456   Elf_Internal_Shdr *symtab_hdr;
   2457   struct elf_link_hash_entry **sym_hashes;
   2458   Elf_Internal_Rela *rel;
   2459   Elf_Internal_Rela *relend;
   2460   const char *name;
   2461   struct elf_kvx_link_hash_table *globals;
   2462   bool save_addend = false;
   2463   bfd_vma addend = 0;
   2464 
   2465   globals = elf_kvx_hash_table (info);
   2466 
   2467   symtab_hdr = &elf_symtab_hdr (input_bfd);
   2468   sym_hashes = elf_sym_hashes (input_bfd);
   2469 
   2470   rel = relocs;
   2471   relend = relocs + input_section->reloc_count;
   2472   for (; rel < relend; rel++)
   2473     {
   2474       unsigned int r_type;
   2475       bfd_reloc_code_real_type bfd_r_type;
   2476       reloc_howto_type *howto;
   2477       unsigned long r_symndx;
   2478       Elf_Internal_Sym *sym;
   2479       asection *sec;
   2480       struct elf_link_hash_entry *h;
   2481       bfd_vma relocation;
   2482       bfd_reloc_status_type r;
   2483       arelent bfd_reloc;
   2484       char sym_type;
   2485       bool unresolved_reloc = false;
   2486       char *error_message = NULL;
   2487 
   2488       r_symndx = ELFNN_R_SYM (rel->r_info);
   2489       r_type = ELFNN_R_TYPE (rel->r_info);
   2490 
   2491       bfd_reloc.howto = elfNN_kvx_howto_from_type (input_bfd, r_type);
   2492       howto = bfd_reloc.howto;
   2493 
   2494       if (howto == NULL)
   2495 	return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
   2496 
   2497       bfd_r_type = elfNN_kvx_bfd_reloc_from_howto (howto);
   2498 
   2499       h = NULL;
   2500       sym = NULL;
   2501       sec = NULL;
   2502 
   2503       if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
   2504 	{
   2505 	  sym = local_syms + r_symndx;
   2506 	  sym_type = ELFNN_ST_TYPE (sym->st_info);
   2507 	  sec = local_sections[r_symndx];
   2508 
   2509 	  /* An object file might have a reference to a local
   2510 	     undefined symbol.  This is a draft object file, but we
   2511 	     should at least do something about it.  */
   2512 	  if (r_type != R_KVX_NONE
   2513 	      && r_type != R_KVX_S37_GOTADDR_LO10
   2514 	      && r_type != R_KVX_S37_GOTADDR_UP27
   2515 	      && r_type != R_KVX_S64_GOTADDR_LO10
   2516 	      && r_type != R_KVX_S64_GOTADDR_UP27
   2517 	      && r_type != R_KVX_S64_GOTADDR_EX27
   2518 	      && r_type != R_KVX_S43_GOTADDR_LO10
   2519 	      && r_type != R_KVX_S43_GOTADDR_UP27
   2520 	      && r_type != R_KVX_S43_GOTADDR_EX6
   2521 	      && bfd_is_und_section (sec)
   2522 	      && ELF_ST_BIND (sym->st_info) != STB_WEAK)
   2523 	    (*info->callbacks->undefined_symbol)
   2524 	      (info, bfd_elf_string_from_elf_section
   2525 	       (input_bfd, symtab_hdr->sh_link, sym->st_name),
   2526 	       input_bfd, input_section, rel->r_offset, true);
   2527 
   2528 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   2529 	}
   2530       else
   2531 	{
   2532 	  bool warned, ignored;
   2533 
   2534 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   2535 				   r_symndx, symtab_hdr, sym_hashes,
   2536 				   h, sec, relocation,
   2537 				   unresolved_reloc, warned, ignored);
   2538 
   2539 	  sym_type = h->type;
   2540 	}
   2541 
   2542       if (sec != NULL && discarded_section (sec))
   2543 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   2544 					 rel, 1, relend, R_KVX_NONE,
   2545 					 howto, 0, contents);
   2546 
   2547       if (bfd_link_relocatable (info))
   2548 	continue;
   2549 
   2550       if (h != NULL)
   2551 	name = h->root.root.string;
   2552       else
   2553 	{
   2554 	  name = (bfd_elf_string_from_elf_section
   2555 		  (input_bfd, symtab_hdr->sh_link, sym->st_name));
   2556 	  if (name == NULL || *name == '\0')
   2557 	    name = bfd_section_name (sec);
   2558 	}
   2559 
   2560       if (r_symndx != 0
   2561 	  && r_type != R_KVX_NONE
   2562 	  && (h == NULL
   2563 	      || h->root.type == bfd_link_hash_defined
   2564 	      || h->root.type == bfd_link_hash_defweak)
   2565 	  && IS_KVX_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
   2566 	{
   2567 	  (*_bfd_error_handler)
   2568 	    ((sym_type == STT_TLS
   2569 	      /* xgettext:c-format */
   2570 	      ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
   2571 	      /* xgettext:c-format */
   2572 	      : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
   2573 	     input_bfd,
   2574 	     input_section, (uint64_t) rel->r_offset, howto->name, name);
   2575 	}
   2576 
   2577       /* Original aarch64 has relaxation handling for TLS here. */
   2578       r = bfd_reloc_continue;
   2579 
   2580       /* There may be multiple consecutive relocations for the
   2581 	 same offset.  In that case we are supposed to treat the
   2582 	 output of each relocation as the addend for the next.  */
   2583       if (rel + 1 < relend
   2584 	  && rel->r_offset == rel[1].r_offset
   2585 	  && ELFNN_R_TYPE (rel[1].r_info) != R_KVX_NONE)
   2586 
   2587 	save_addend = true;
   2588       else
   2589 	save_addend = false;
   2590 
   2591       if (r == bfd_reloc_continue)
   2592 	r = elfNN_kvx_final_link_relocate (howto, input_bfd, output_bfd,
   2593 					   input_section, contents, rel,
   2594 					   relocation, info, sec,
   2595 					   h, &unresolved_reloc,
   2596 					   save_addend, &addend, sym);
   2597 
   2598       switch (elfNN_kvx_bfd_reloc_from_type (input_bfd, r_type))
   2599 	{
   2600 	case BFD_RELOC_KVX_S37_TLS_GD_LO10:
   2601 	case BFD_RELOC_KVX_S37_TLS_GD_UP27:
   2602 
   2603 	case BFD_RELOC_KVX_S43_TLS_GD_LO10:
   2604 	case BFD_RELOC_KVX_S43_TLS_GD_UP27:
   2605 	case BFD_RELOC_KVX_S43_TLS_GD_EX6:
   2606 
   2607 	case BFD_RELOC_KVX_S37_TLS_LD_LO10:
   2608 	case BFD_RELOC_KVX_S37_TLS_LD_UP27:
   2609 
   2610 	case BFD_RELOC_KVX_S43_TLS_LD_LO10:
   2611 	case BFD_RELOC_KVX_S43_TLS_LD_UP27:
   2612 	case BFD_RELOC_KVX_S43_TLS_LD_EX6:
   2613 
   2614 	  if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
   2615 	    {
   2616 	      bool need_relocs = false;
   2617 	      bfd_byte *loc;
   2618 	      int indx;
   2619 	      bfd_vma off;
   2620 
   2621 	      off = symbol_got_offset (input_bfd, h, r_symndx);
   2622 	      indx = h && h->dynindx != -1 ? h->dynindx : 0;
   2623 
   2624 	      need_relocs =
   2625 		(bfd_link_pic (info) || indx != 0) &&
   2626 		(h == NULL
   2627 		 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2628 		 || h->root.type != bfd_link_hash_undefweak);
   2629 
   2630 	      BFD_ASSERT (globals->root.srelgot != NULL);
   2631 
   2632 	      if (need_relocs)
   2633 		{
   2634 		  Elf_Internal_Rela rela;
   2635 		  rela.r_info = ELFNN_R_INFO (indx, R_KVX_64_DTPMOD);
   2636 		  rela.r_addend = 0;
   2637 		  rela.r_offset = globals->root.sgot->output_section->vma +
   2638 		    globals->root.sgot->output_offset + off;
   2639 
   2640 		  loc = globals->root.srelgot->contents;
   2641 		  loc += globals->root.srelgot->reloc_count++
   2642 		    * RELOC_SIZE (htab);
   2643 		  bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   2644 
   2645 		  bfd_reloc_code_real_type real_type =
   2646 		    elfNN_kvx_bfd_reloc_from_type (input_bfd, r_type);
   2647 
   2648 		  if (real_type == BFD_RELOC_KVX_S37_TLS_LD_LO10
   2649 		      || real_type == BFD_RELOC_KVX_S37_TLS_LD_UP27
   2650 		      || real_type == BFD_RELOC_KVX_S43_TLS_LD_LO10
   2651 		      || real_type == BFD_RELOC_KVX_S43_TLS_LD_UP27
   2652 		      || real_type == BFD_RELOC_KVX_S43_TLS_LD_EX6)
   2653 		    {
   2654 		      /* For local dynamic, don't generate DTPOFF in any case.
   2655 			 Initialize the DTPOFF slot into zero, so we get module
   2656 			 base address when invoke runtime TLS resolver.  */
   2657 		      bfd_put_NN (output_bfd, 0,
   2658 				  globals->root.sgot->contents + off
   2659 				  + GOT_ENTRY_SIZE);
   2660 		    }
   2661 		  else if (indx == 0)
   2662 		    {
   2663 		      bfd_put_NN (output_bfd,
   2664 				  relocation - dtpoff_base (info),
   2665 				  globals->root.sgot->contents + off
   2666 				  + GOT_ENTRY_SIZE);
   2667 		    }
   2668 		  else
   2669 		    {
   2670 		      /* This TLS symbol is global. We emit a
   2671 			 relocation to fixup the tls offset at load
   2672 			 time.  */
   2673 		      rela.r_info =
   2674 			ELFNN_R_INFO (indx, R_KVX_64_DTPOFF);
   2675 		      rela.r_addend = 0;
   2676 		      rela.r_offset =
   2677 			(globals->root.sgot->output_section->vma
   2678 			 + globals->root.sgot->output_offset + off
   2679 			 + GOT_ENTRY_SIZE);
   2680 
   2681 		      loc = globals->root.srelgot->contents;
   2682 		      loc += globals->root.srelgot->reloc_count++
   2683 			* RELOC_SIZE (globals);
   2684 		      bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   2685 		      bfd_put_NN (output_bfd, (bfd_vma) 0,
   2686 				  globals->root.sgot->contents + off
   2687 				  + GOT_ENTRY_SIZE);
   2688 		    }
   2689 		}
   2690 	      else
   2691 		{
   2692 		  bfd_put_NN (output_bfd, (bfd_vma) 1,
   2693 			      globals->root.sgot->contents + off);
   2694 		  bfd_put_NN (output_bfd,
   2695 			      relocation - dtpoff_base (info),
   2696 			      globals->root.sgot->contents + off
   2697 			      + GOT_ENTRY_SIZE);
   2698 		}
   2699 
   2700 	      symbol_got_offset_mark (input_bfd, h, r_symndx);
   2701 	    }
   2702 	  break;
   2703 
   2704 	case BFD_RELOC_KVX_S37_TLS_IE_LO10:
   2705 	case BFD_RELOC_KVX_S37_TLS_IE_UP27:
   2706 
   2707 	case BFD_RELOC_KVX_S43_TLS_IE_LO10:
   2708 	case BFD_RELOC_KVX_S43_TLS_IE_UP27:
   2709 	case BFD_RELOC_KVX_S43_TLS_IE_EX6:
   2710 	  if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
   2711 	    {
   2712 	      bool need_relocs = false;
   2713 	      bfd_byte *loc;
   2714 	      int indx;
   2715 	      bfd_vma off;
   2716 
   2717 	      off = symbol_got_offset (input_bfd, h, r_symndx);
   2718 
   2719 	      indx = h && h->dynindx != -1 ? h->dynindx : 0;
   2720 
   2721 	      need_relocs =
   2722 		(bfd_link_pic (info) || indx != 0) &&
   2723 		(h == NULL
   2724 		 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2725 		 || h->root.type != bfd_link_hash_undefweak);
   2726 
   2727 	      BFD_ASSERT (globals->root.srelgot != NULL);
   2728 
   2729 	      if (need_relocs)
   2730 		{
   2731 		  Elf_Internal_Rela rela;
   2732 
   2733 		  if (indx == 0)
   2734 		    rela.r_addend = relocation - dtpoff_base (info);
   2735 		  else
   2736 		    rela.r_addend = 0;
   2737 
   2738 		  rela.r_info = ELFNN_R_INFO (indx, R_KVX_64_TPOFF);
   2739 		  rela.r_offset = globals->root.sgot->output_section->vma +
   2740 		    globals->root.sgot->output_offset + off;
   2741 
   2742 		  loc = globals->root.srelgot->contents;
   2743 		  loc += globals->root.srelgot->reloc_count++
   2744 		    * RELOC_SIZE (htab);
   2745 
   2746 		  bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   2747 
   2748 		  bfd_put_NN (output_bfd, rela.r_addend,
   2749 			      globals->root.sgot->contents + off);
   2750 		}
   2751 	      else
   2752 		bfd_put_NN (output_bfd, relocation - tpoff_base (info),
   2753 			    globals->root.sgot->contents + off);
   2754 
   2755 	      symbol_got_offset_mark (input_bfd, h, r_symndx);
   2756 	    }
   2757 	  break;
   2758 
   2759 	default:
   2760 	  break;
   2761 	}
   2762 
   2763       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   2764 	 because such sections are not SEC_ALLOC and thus ld.so will
   2765 	 not process them.  */
   2766       if (unresolved_reloc
   2767 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   2768 	       && h->def_dynamic)
   2769 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   2770 				      +rel->r_offset) != (bfd_vma) - 1)
   2771 	{
   2772 	  (*_bfd_error_handler)
   2773 	    /* xgettext:c-format */
   2774 	    (_("%pB(%pA+%#" PRIx64 "): "
   2775 	       "unresolvable %s relocation against symbol `%s'"),
   2776 	     input_bfd, input_section, (uint64_t) rel->r_offset, howto->name,
   2777 	     h->root.root.string);
   2778 	  return false;
   2779 	}
   2780 
   2781       if (r != bfd_reloc_ok && r != bfd_reloc_continue)
   2782 	{
   2783 	  switch (r)
   2784 	    {
   2785 	    case bfd_reloc_overflow:
   2786 	      (*info->callbacks->reloc_overflow)
   2787 		(info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
   2788 		 input_bfd, input_section, rel->r_offset);
   2789 
   2790 	      /* Original aarch64 code had a check for alignement correctness */
   2791 	      break;
   2792 
   2793 	    case bfd_reloc_undefined:
   2794 	      (*info->callbacks->undefined_symbol)
   2795 		(info, name, input_bfd, input_section, rel->r_offset, true);
   2796 	      break;
   2797 
   2798 	    case bfd_reloc_outofrange:
   2799 	      error_message = _("out of range");
   2800 	      goto common_error;
   2801 
   2802 	    case bfd_reloc_notsupported:
   2803 	      error_message = _("unsupported relocation");
   2804 	      goto common_error;
   2805 
   2806 	    case bfd_reloc_dangerous:
   2807 	      /* error_message should already be set.  */
   2808 	      goto common_error;
   2809 
   2810 	    default:
   2811 	      error_message = _("unknown error");
   2812 	      /* Fall through.  */
   2813 
   2814 	    common_error:
   2815 	      BFD_ASSERT (error_message != NULL);
   2816 	      (*info->callbacks->reloc_dangerous)
   2817 		(info, error_message, input_bfd, input_section, rel->r_offset);
   2818 	      break;
   2819 	    }
   2820 	}
   2821 
   2822       if (!save_addend)
   2823 	addend = 0;
   2824     }
   2825 
   2826   return true;
   2827 }
   2828 
   2829 /* Set the right machine number.  */
   2830 
   2831 static bool
   2832 elfNN_kvx_object_p (bfd *abfd)
   2833 {
   2834   /* must be coherent with default arch in cpu-kvx.c */
   2835   int e_set = bfd_mach_kv3_1;
   2836 
   2837   if (elf_elfheader (abfd)->e_machine == EM_KVX)
   2838     {
   2839       int e_core = elf_elfheader (abfd)->e_flags & ELF_KVX_CORE_MASK;
   2840       switch(e_core)
   2841 	{
   2842 #if ARCH_SIZE == 64
   2843 	case ELF_KVX_CORE_KV3_1 : e_set = bfd_mach_kv3_1_64; break;
   2844 	case ELF_KVX_CORE_KV3_2 : e_set = bfd_mach_kv3_2_64; break;
   2845 	case ELF_KVX_CORE_KV4_1 : e_set = bfd_mach_kv4_1_64; break;
   2846 #else
   2847 	case ELF_KVX_CORE_KV3_1 : e_set = bfd_mach_kv3_1; break;
   2848 	case ELF_KVX_CORE_KV3_2 : e_set = bfd_mach_kv3_2; break;
   2849 	case ELF_KVX_CORE_KV4_1 : e_set = bfd_mach_kv4_1; break;
   2850 #endif
   2851 	default:
   2852 	  (*_bfd_error_handler)(_("%s: Bad ELF id: `%d'"),
   2853 				abfd->filename, e_core);
   2854 	}
   2855     }
   2856   return bfd_default_set_arch_mach (abfd, bfd_arch_kvx, e_set);
   2857 }
   2858 
   2859 /* Function to keep KVX specific flags in the ELF header.  */
   2860 
   2861 static bool
   2862 elfNN_kvx_set_private_flags (bfd *abfd, flagword flags)
   2863 {
   2864   if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
   2865     {
   2866     }
   2867   else
   2868     {
   2869       elf_elfheader (abfd)->e_flags = flags;
   2870       elf_flags_init (abfd) = true;
   2871     }
   2872 
   2873   return true;
   2874 }
   2875 
   2876 /* Merge backend specific data from an object file to the output
   2877    object file when linking.  */
   2878 
   2879 static bool
   2880 elfNN_kvx_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   2881 {
   2882   bfd *obfd = info->output_bfd;
   2883   flagword out_flags;
   2884   flagword in_flags;
   2885   bool flags_compatible = true;
   2886   asection *sec;
   2887 
   2888   /* Check if we have the same endianess.  */
   2889   if (!_bfd_generic_verify_endian_match (ibfd, info))
   2890     return false;
   2891 
   2892   if (!is_kvx_elf (ibfd))
   2893     return true;
   2894 
   2895   /* The input BFD must have had its flags initialised.  */
   2896   /* The following seems bogus to me -- The flags are initialized in
   2897      the assembler but I don't think an elf_flags_init field is
   2898      written into the object.  */
   2899   /* BFD_ASSERT (elf_flags_init (ibfd)); */
   2900 
   2901   if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
   2902     {
   2903       const char *msg;
   2904 
   2905       if (bfd_get_arch_size (ibfd) == 32
   2906 	  && bfd_get_arch_size (obfd) == 64)
   2907 	msg = _("%s: compiled as 32-bit object and %s is 64-bit");
   2908       else if (bfd_get_arch_size (ibfd) == 64
   2909 	       && bfd_get_arch_size (obfd) == 32)
   2910 	msg = _("%s: compiled as 64-bit object and %s is 32-bit");
   2911       else
   2912 	msg = _("%s: object size does not match that of target %s");
   2913 
   2914       (*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
   2915 			     bfd_get_filename (obfd));
   2916       bfd_set_error (bfd_error_wrong_format);
   2917       return false;
   2918     }
   2919 
   2920   in_flags = elf_elfheader (ibfd)->e_flags;
   2921   out_flags = elf_elfheader (obfd)->e_flags;
   2922 
   2923   if (!elf_flags_init (obfd))
   2924     {
   2925       /* If the input is the default architecture and had the default
   2926 	 flags then do not bother setting the flags for the output
   2927 	 architecture, instead allow future merges to do this.  If no
   2928 	 future merges ever set these flags then they will retain their
   2929 	 uninitialised values, which surprise surprise, correspond
   2930 	 to the default values.  */
   2931       if (bfd_get_arch_info (ibfd)->the_default
   2932 	  && elf_elfheader (ibfd)->e_flags == 0)
   2933 	return true;
   2934 
   2935       elf_flags_init (obfd) = true;
   2936       elf_elfheader (obfd)->e_flags = in_flags;
   2937 
   2938       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   2939 	  && bfd_get_arch_info (obfd)->the_default)
   2940 	return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   2941 				  bfd_get_mach (ibfd));
   2942 
   2943       return true;
   2944     }
   2945 
   2946   /* Identical flags must be compatible.  */
   2947   if (in_flags == out_flags)
   2948     return true;
   2949 
   2950   /* Check to see if the input BFD actually contains any sections.  If
   2951      not, its flags may not have been initialised either, but it
   2952      cannot actually cause any incompatiblity.  Do not short-circuit
   2953      dynamic objects; their section list may be emptied by
   2954      elf_link_add_object_symbols.
   2955 
   2956      Also check to see if there are no code sections in the input.
   2957      In this case there is no need to check for code specific flags.
   2958      XXX - do we need to worry about floating-point format compatability
   2959      in data sections ?  */
   2960   if (!(ibfd->flags & DYNAMIC))
   2961     {
   2962       bool null_input_bfd = true;
   2963       bool only_data_sections = true;
   2964 
   2965       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   2966 	{
   2967 	  if ((bfd_section_flags (sec)
   2968 	       & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
   2969 	      == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
   2970 	    only_data_sections = false;
   2971 
   2972 	  null_input_bfd = false;
   2973 	  break;
   2974 	}
   2975 
   2976       if (null_input_bfd || only_data_sections)
   2977 	return true;
   2978     }
   2979   return flags_compatible;
   2980 }
   2981 
   2982 /* Display the flags field.  */
   2983 
   2984 static bool
   2985 elfNN_kvx_print_private_bfd_data (bfd *abfd, void *ptr)
   2986 {
   2987   FILE *file = (FILE *) ptr;
   2988   unsigned long flags;
   2989 
   2990   BFD_ASSERT (abfd != NULL && ptr != NULL);
   2991 
   2992   /* Print normal ELF private data.  */
   2993   _bfd_elf_print_private_bfd_data (abfd, ptr);
   2994 
   2995   flags = elf_elfheader (abfd)->e_flags;
   2996   /* Ignore init flag - it may not be set, despite the flags field
   2997      containing valid data.  */
   2998 
   2999   /* xgettext:c-format */
   3000   fprintf (file, _("Private flags = 0x%lx : "), elf_elfheader (abfd)->e_flags);
   3001   if((flags & ELF_KVX_ABI_64B_ADDR_BIT) == ELF_KVX_ABI_64B_ADDR_BIT)
   3002     {
   3003       if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV3_1))
   3004 	fprintf (file, _("Coolidge (kv3) V1 64 bits"));
   3005       else if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV3_2))
   3006 	fprintf (file, _("Coolidge (kv3) V2 64 bits"));
   3007       else if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV4_1))
   3008 	fprintf (file, _("Coolidge (kv4) V1 64 bits"));
   3009     }
   3010   else
   3011     {
   3012       if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV3_1))
   3013 	fprintf (file, _("Coolidge (kv3) V1 32 bits"));
   3014       else if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV3_2))
   3015 	fprintf (file, _("Coolidge (kv3) V2 32 bits"));
   3016       else if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV4_1))
   3017 	fprintf (file, _("Coolidge (kv4) V1 32 bits"));
   3018     }
   3019 
   3020   fputc ('\n', file);
   3021 
   3022   return true;
   3023 }
   3024 
   3025 /* Adjust a symbol defined by a dynamic object and referenced by a
   3026    regular object.  The current definition is in some section of the
   3027    dynamic object, but we're not including those sections.  We have to
   3028    change the definition to something the rest of the link can
   3029    understand.	*/
   3030 
   3031 static bool
   3032 elfNN_kvx_adjust_dynamic_symbol (struct bfd_link_info *info,
   3033 				 struct elf_link_hash_entry *h)
   3034 {
   3035   struct elf_kvx_link_hash_table *htab;
   3036   asection *s;
   3037 
   3038   /* If this is a function, put it in the procedure linkage table.  We
   3039      will fill in the contents of the procedure linkage table later,
   3040      when we know the address of the .got section.  */
   3041   if (h->type == STT_FUNC || h->needs_plt)
   3042     {
   3043       if (h->plt.refcount <= 0
   3044 	  || ((SYMBOL_CALLS_LOCAL (info, h)
   3045 	       || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   3046 		   && h->root.type == bfd_link_hash_undefweak))))
   3047 	{
   3048 	  /* This case can occur if we saw a CALL26 reloc in
   3049 	     an input file, but the symbol wasn't referred to
   3050 	     by a dynamic object or all references were
   3051 	     garbage collected. In which case we can end up
   3052 	     resolving.  */
   3053 	  h->plt.offset = (bfd_vma) - 1;
   3054 	  h->needs_plt = 0;
   3055 	}
   3056 
   3057       return true;
   3058     }
   3059   else
   3060     /* Otherwise, reset to -1.  */
   3061     h->plt.offset = (bfd_vma) - 1;
   3062 
   3063 
   3064   /* If this is a weak symbol, and there is a real definition, the
   3065      processor independent code will have arranged for us to see the
   3066      real definition first, and we can just use the same value.  */
   3067   if (h->is_weakalias)
   3068     {
   3069       struct elf_link_hash_entry *def = weakdef (h);
   3070       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   3071       h->root.u.def.section = def->root.u.def.section;
   3072       h->root.u.def.value = def->root.u.def.value;
   3073       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
   3074 	h->non_got_ref = def->non_got_ref;
   3075       return true;
   3076     }
   3077 
   3078   /* If we are creating a shared library, we must presume that the
   3079      only references to the symbol are via the global offset table.
   3080      For such cases we need not do anything here; the relocations will
   3081      be handled correctly by relocate_section.  */
   3082   if (bfd_link_pic (info))
   3083     return true;
   3084 
   3085   /* If there are no references to this symbol that do not use the
   3086      GOT, we don't need to generate a copy reloc.  */
   3087   if (!h->non_got_ref)
   3088     return true;
   3089 
   3090   /* If -z nocopyreloc was given, we won't generate them either.  */
   3091   if (info->nocopyreloc)
   3092     {
   3093       h->non_got_ref = 0;
   3094       return true;
   3095     }
   3096 
   3097   /* We must allocate the symbol in our .dynbss section, which will
   3098      become part of the .bss section of the executable.  There will be
   3099      an entry for this symbol in the .dynsym section.  The dynamic
   3100      object will contain position independent code, so all references
   3101      from the dynamic object to this symbol will go through the global
   3102      offset table.  The dynamic linker will use the .dynsym entry to
   3103      determine the address it must put in the global offset table, so
   3104      both the dynamic object and the regular object will refer to the
   3105      same memory location for the variable.  */
   3106 
   3107   htab = elf_kvx_hash_table (info);
   3108 
   3109   /* We must generate a R_KVX_COPY reloc to tell the dynamic linker
   3110      to copy the initial value out of the dynamic object and into the
   3111      runtime process image.  */
   3112   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   3113     {
   3114       htab->srelbss->size += RELOC_SIZE (htab);
   3115       h->needs_copy = 1;
   3116     }
   3117 
   3118   s = htab->sdynbss;
   3119 
   3120   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   3121 }
   3122 
   3123 static bool
   3124 elfNN_kvx_allocate_local_symbols (bfd *abfd, unsigned number)
   3125 {
   3126   struct elf_kvx_local_symbol *locals;
   3127   locals = elf_kvx_locals (abfd);
   3128   if (locals == NULL)
   3129     {
   3130       locals = (struct elf_kvx_local_symbol *)
   3131 	bfd_zalloc (abfd, number * sizeof (struct elf_kvx_local_symbol));
   3132       if (locals == NULL)
   3133 	return false;
   3134       elf_kvx_locals (abfd) = locals;
   3135     }
   3136   return true;
   3137 }
   3138 
   3139 /* Create the .got section to hold the global offset table.  */
   3140 
   3141 static bool
   3142 kvx_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
   3143 {
   3144   elf_backend_data *bed = get_elf_backend_data (abfd);
   3145   flagword flags;
   3146   asection *s;
   3147   struct elf_link_hash_entry *h;
   3148   struct elf_link_hash_table *htab = elf_hash_table (info);
   3149 
   3150   /* This function may be called more than once.  */
   3151   s = bfd_get_linker_section (abfd, ".got");
   3152   if (s != NULL)
   3153     return true;
   3154 
   3155   flags = bed->dynamic_sec_flags;
   3156 
   3157   s = bfd_make_section_anyway_with_flags (abfd,
   3158 					  (bed->rela_plts_and_copies_p
   3159 					   ? ".rela.got" : ".rel.got"),
   3160 					  (bed->dynamic_sec_flags
   3161 					   | SEC_READONLY));
   3162   if (s == NULL
   3163       || !bfd_set_section_alignment (s, bed->s->log_file_align))
   3164 
   3165     return false;
   3166   htab->srelgot = s;
   3167 
   3168   s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   3169   if (s == NULL
   3170       || !bfd_set_section_alignment (s, bed->s->log_file_align))
   3171     return false;
   3172   htab->sgot = s;
   3173   htab->sgot->size += GOT_ENTRY_SIZE;
   3174 
   3175   if (bed->want_got_sym)
   3176     {
   3177       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
   3178 	 (or .got.plt) section.  We don't do this in the linker script
   3179 	 because we don't want to define the symbol if we are not creating
   3180 	 a global offset table.  */
   3181       h = _bfd_elf_define_linkage_sym (abfd, info, s,
   3182 				       "_GLOBAL_OFFSET_TABLE_");
   3183       elf_hash_table (info)->hgot = h;
   3184       if (h == NULL)
   3185 	return false;
   3186     }
   3187 
   3188   if (bed->want_got_plt)
   3189     {
   3190       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
   3191       if (s == NULL
   3192 	  || !bfd_set_section_alignment (s,
   3193 					 bed->s->log_file_align))
   3194 	return false;
   3195       htab->sgotplt = s;
   3196     }
   3197 
   3198   /* The first bit of the global offset table is the header.  */
   3199   s->size += bed->got_header_size;
   3200 
   3201   /* we still need to handle got content when doing static link with PIC */
   3202   if (bfd_link_executable (info) && !bfd_link_pic (info)) {
   3203     htab->dynobj = abfd;
   3204   }
   3205 
   3206   return true;
   3207 }
   3208 
   3209 /* Look through the relocs for a section during the first phase.  */
   3210 
   3211 static bool
   3212 elfNN_kvx_check_relocs (bfd *abfd, struct bfd_link_info *info,
   3213 			    asection *sec, const Elf_Internal_Rela *relocs)
   3214 {
   3215   Elf_Internal_Shdr *symtab_hdr;
   3216   struct elf_link_hash_entry **sym_hashes;
   3217   const Elf_Internal_Rela *rel;
   3218   const Elf_Internal_Rela *rel_end;
   3219   asection *sreloc;
   3220 
   3221   struct elf_kvx_link_hash_table *htab;
   3222 
   3223   if (bfd_link_relocatable (info))
   3224     return true;
   3225 
   3226   BFD_ASSERT (is_kvx_elf (abfd));
   3227 
   3228   htab = elf_kvx_hash_table (info);
   3229   sreloc = NULL;
   3230 
   3231   symtab_hdr = &elf_symtab_hdr (abfd);
   3232   sym_hashes = elf_sym_hashes (abfd);
   3233 
   3234   rel_end = relocs + sec->reloc_count;
   3235   for (rel = relocs; rel < rel_end; rel++)
   3236     {
   3237       struct elf_link_hash_entry *h;
   3238       unsigned int r_symndx;
   3239       unsigned int r_type;
   3240       bfd_reloc_code_real_type bfd_r_type;
   3241       Elf_Internal_Sym *isym;
   3242 
   3243       r_symndx = ELFNN_R_SYM (rel->r_info);
   3244       r_type = ELFNN_R_TYPE (rel->r_info);
   3245 
   3246       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
   3247 	{
   3248 	  /* xgettext:c-format */
   3249 	  _bfd_error_handler (_("%pB: bad symbol index: %d"), abfd, r_symndx);
   3250 	  return false;
   3251 	}
   3252 
   3253       if (r_symndx < symtab_hdr->sh_info)
   3254 	{
   3255 	  /* A local symbol.  */
   3256 	  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   3257 					abfd, r_symndx);
   3258 	  if (isym == NULL)
   3259 	    return false;
   3260 
   3261 	  h = NULL;
   3262 	}
   3263       else
   3264 	{
   3265 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   3266 	  while (h->root.type == bfd_link_hash_indirect
   3267 		 || h->root.type == bfd_link_hash_warning)
   3268 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3269 	}
   3270 
   3271       /* Could be done earlier, if h were already available.  */
   3272       bfd_r_type = kvx_tls_transition (abfd, info, r_type, h, r_symndx);
   3273 
   3274       if (h != NULL)
   3275 	{
   3276 	  /* Create the ifunc sections for static executables.  If we
   3277 	     never see an indirect function symbol nor we are building
   3278 	     a static executable, those sections will be empty and
   3279 	     won't appear in output.  */
   3280 	  switch (bfd_r_type)
   3281 	    {
   3282 	    default:
   3283 	      break;
   3284 	    }
   3285 
   3286 	  /* It is referenced by a non-shared object. */
   3287 	  h->ref_regular = 1;
   3288 	}
   3289 
   3290       switch (bfd_r_type)
   3291 	{
   3292 
   3293 	case BFD_RELOC_KVX_S43_LO10:
   3294 	case BFD_RELOC_KVX_S43_UP27:
   3295 	case BFD_RELOC_KVX_S43_EX6:
   3296 
   3297 	case BFD_RELOC_KVX_S37_LO10:
   3298 	case BFD_RELOC_KVX_S37_UP27:
   3299 
   3300 	case BFD_RELOC_KVX_S64_LO10:
   3301 	case BFD_RELOC_KVX_S64_UP27:
   3302 	case BFD_RELOC_KVX_S64_EX27:
   3303 
   3304 	case BFD_RELOC_KVX_32:
   3305 	case BFD_RELOC_KVX_64:
   3306 
   3307 	  /* We don't need to handle relocs into sections not going into
   3308 	     the "real" output.  */
   3309 	  if ((sec->flags & SEC_ALLOC) == 0)
   3310 	    break;
   3311 
   3312 	  if (h != NULL)
   3313 	    {
   3314 	      if (!bfd_link_pic (info))
   3315 		h->non_got_ref = 1;
   3316 
   3317 	      h->plt.refcount += 1;
   3318 	      h->pointer_equality_needed = 1;
   3319 	    }
   3320 
   3321 	  /* No need to do anything if we're not creating a shared
   3322 	     object.  */
   3323 	  if (! bfd_link_pic (info))
   3324 	    break;
   3325 
   3326 	  {
   3327 	    struct elf_dyn_relocs *p;
   3328 	    struct elf_dyn_relocs **head;
   3329 
   3330 	    /* We must copy these reloc types into the output file.
   3331 	       Create a reloc section in dynobj and make room for
   3332 	       this reloc.  */
   3333 	    if (sreloc == NULL)
   3334 	      {
   3335 		if (htab->root.dynobj == NULL)
   3336 		  htab->root.dynobj = abfd;
   3337 
   3338 		sreloc = _bfd_elf_make_dynamic_reloc_section
   3339 		  (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ true);
   3340 
   3341 		if (sreloc == NULL)
   3342 		  return false;
   3343 	      }
   3344 
   3345 	    /* If this is a global symbol, we count the number of
   3346 	       relocations we need for this symbol.  */
   3347 	    if (h != NULL)
   3348 	      {
   3349 		head = &h->dyn_relocs;
   3350 	      }
   3351 	    else
   3352 	      {
   3353 		/* Track dynamic relocs needed for local syms too.
   3354 		   We really need local syms available to do this
   3355 		   easily.  Oh well.  */
   3356 
   3357 		asection *s;
   3358 		void **vpp;
   3359 
   3360 		isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   3361 					      abfd, r_symndx);
   3362 		if (isym == NULL)
   3363 		  return false;
   3364 
   3365 		s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   3366 		if (s == NULL)
   3367 		  s = sec;
   3368 
   3369 		/* Beware of type punned pointers vs strict aliasing
   3370 		   rules.  */
   3371 		vpp = &(elf_section_data (s)->local_dynrel);
   3372 		head = (struct elf_dyn_relocs **) vpp;
   3373 	      }
   3374 
   3375 	    p = *head;
   3376 	    if (p == NULL || p->sec != sec)
   3377 	      {
   3378 		bfd_size_type amt = sizeof *p;
   3379 		p = ((struct elf_dyn_relocs *)
   3380 		     bfd_zalloc (htab->root.dynobj, amt));
   3381 		if (p == NULL)
   3382 		  return false;
   3383 		p->next = *head;
   3384 		*head = p;
   3385 		p->sec = sec;
   3386 	      }
   3387 
   3388 	    p->count += 1;
   3389 
   3390 	  }
   3391 	  break;
   3392 
   3393 	case BFD_RELOC_KVX_S37_GOT_LO10:
   3394 	case BFD_RELOC_KVX_S37_GOT_UP27:
   3395 
   3396 	case BFD_RELOC_KVX_S37_GOTOFF_LO10:
   3397 	case BFD_RELOC_KVX_S37_GOTOFF_UP27:
   3398 
   3399 	case BFD_RELOC_KVX_S43_GOT_LO10:
   3400 	case BFD_RELOC_KVX_S43_GOT_UP27:
   3401 	case BFD_RELOC_KVX_S43_GOT_EX6:
   3402 
   3403 	case BFD_RELOC_KVX_S43_GOTOFF_LO10:
   3404 	case BFD_RELOC_KVX_S43_GOTOFF_UP27:
   3405 	case BFD_RELOC_KVX_S43_GOTOFF_EX6:
   3406 
   3407 	case BFD_RELOC_KVX_S37_TLS_GD_LO10:
   3408 	case BFD_RELOC_KVX_S37_TLS_GD_UP27:
   3409 
   3410 	case BFD_RELOC_KVX_S43_TLS_GD_LO10:
   3411 	case BFD_RELOC_KVX_S43_TLS_GD_UP27:
   3412 	case BFD_RELOC_KVX_S43_TLS_GD_EX6:
   3413 
   3414 	case BFD_RELOC_KVX_S37_TLS_IE_LO10:
   3415 	case BFD_RELOC_KVX_S37_TLS_IE_UP27:
   3416 
   3417 	case BFD_RELOC_KVX_S43_TLS_IE_LO10:
   3418 	case BFD_RELOC_KVX_S43_TLS_IE_UP27:
   3419 	case BFD_RELOC_KVX_S43_TLS_IE_EX6:
   3420 
   3421 	case BFD_RELOC_KVX_S37_TLS_LD_LO10:
   3422 	case BFD_RELOC_KVX_S37_TLS_LD_UP27:
   3423 
   3424 	case BFD_RELOC_KVX_S43_TLS_LD_LO10:
   3425 	case BFD_RELOC_KVX_S43_TLS_LD_UP27:
   3426 	case BFD_RELOC_KVX_S43_TLS_LD_EX6:
   3427 	  {
   3428 	    unsigned got_type;
   3429 	    unsigned old_got_type;
   3430 
   3431 	    got_type = kvx_reloc_got_type (bfd_r_type);
   3432 
   3433 	    if (h)
   3434 	      {
   3435 		h->got.refcount += 1;
   3436 		old_got_type = elf_kvx_hash_entry (h)->got_type;
   3437 	      }
   3438 	    else
   3439 	      {
   3440 		struct elf_kvx_local_symbol *locals;
   3441 
   3442 		if (!elfNN_kvx_allocate_local_symbols
   3443 		    (abfd, symtab_hdr->sh_info))
   3444 		  return false;
   3445 
   3446 		locals = elf_kvx_locals (abfd);
   3447 		BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
   3448 		locals[r_symndx].got_refcount += 1;
   3449 		old_got_type = locals[r_symndx].got_type;
   3450 	      }
   3451 
   3452 	    /* We will already have issued an error message if there
   3453 	       is a TLS/non-TLS mismatch, based on the symbol type.
   3454 	       So just combine any TLS types needed.  */
   3455 	    if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
   3456 		&& got_type != GOT_NORMAL)
   3457 	      got_type |= old_got_type;
   3458 
   3459 	    /* If the symbol is accessed by both IE and GD methods, we
   3460 	       are able to relax.  Turn off the GD flag, without
   3461 	       messing up with any other kind of TLS types that may be
   3462 	       involved.  */
   3463 	    /* Disabled untested and unused TLS */
   3464 	    /* if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type)) */
   3465 	    /*   got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD); */
   3466 
   3467 	    if (old_got_type != got_type)
   3468 	      {
   3469 		if (h != NULL)
   3470 		  elf_kvx_hash_entry (h)->got_type = got_type;
   3471 		else
   3472 		  {
   3473 		    struct elf_kvx_local_symbol *locals;
   3474 		    locals = elf_kvx_locals (abfd);
   3475 		    BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
   3476 		    locals[r_symndx].got_type = got_type;
   3477 		  }
   3478 	      }
   3479 
   3480 	    if (htab->root.dynobj == NULL)
   3481 	      htab->root.dynobj = abfd;
   3482 	    if (! kvx_elf_create_got_section (htab->root.dynobj, info))
   3483 	      return false;
   3484 	    break;
   3485 	  }
   3486 
   3487 	case BFD_RELOC_KVX_S64_GOTADDR_LO10:
   3488 	case BFD_RELOC_KVX_S64_GOTADDR_UP27:
   3489 	case BFD_RELOC_KVX_S64_GOTADDR_EX27:
   3490 
   3491 	case BFD_RELOC_KVX_S43_GOTADDR_LO10:
   3492 	case BFD_RELOC_KVX_S43_GOTADDR_UP27:
   3493 	case BFD_RELOC_KVX_S43_GOTADDR_EX6:
   3494 
   3495 	case BFD_RELOC_KVX_S37_GOTADDR_LO10:
   3496 	case BFD_RELOC_KVX_S37_GOTADDR_UP27:
   3497 
   3498 	  if (htab->root.dynobj == NULL)
   3499 	    htab->root.dynobj = abfd;
   3500 	  if (! kvx_elf_create_got_section (htab->root.dynobj, info))
   3501 	    return false;
   3502 	  break;
   3503 
   3504 	case BFD_RELOC_KVX_PCREL27:
   3505 	case BFD_RELOC_KVX_PCREL17:
   3506 	  /* If this is a local symbol then we resolve it
   3507 	     directly without creating a PLT entry.  */
   3508 	  if (h == NULL)
   3509 	    continue;
   3510 
   3511 	  h->needs_plt = 1;
   3512 	  if (h->plt.refcount <= 0)
   3513 	    h->plt.refcount = 1;
   3514 	  else
   3515 	    h->plt.refcount += 1;
   3516 	  break;
   3517 
   3518 	default:
   3519 	  break;
   3520 	}
   3521     }
   3522 
   3523   return true;
   3524 }
   3525 
   3526 static bool
   3527 elfNN_kvx_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
   3528 {
   3529   Elf_Internal_Ehdr *i_ehdrp;	/* ELF file header, internal form.  */
   3530 
   3531   if (!_bfd_elf_init_file_header (abfd, link_info))
   3532     return false;
   3533 
   3534   i_ehdrp = elf_elfheader (abfd);
   3535   i_ehdrp->e_ident[EI_ABIVERSION] = KVX_ELF_ABI_VERSION;
   3536   return true;
   3537 }
   3538 
   3539 static enum elf_reloc_type_class
   3540 elfNN_kvx_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   3541 				const asection *rel_sec ATTRIBUTE_UNUSED,
   3542 				const Elf_Internal_Rela *rela)
   3543 {
   3544   switch ((int) ELFNN_R_TYPE (rela->r_info))
   3545     {
   3546     case R_KVX_RELATIVE:
   3547       return reloc_class_relative;
   3548     case R_KVX_JMP_SLOT:
   3549       return reloc_class_plt;
   3550     case R_KVX_COPY:
   3551       return reloc_class_copy;
   3552     default:
   3553       return reloc_class_normal;
   3554     }
   3555 }
   3556 
   3557 /* A structure used to record a list of sections, independently
   3558    of the next and prev fields in the asection structure.  */
   3559 typedef struct section_list
   3560 {
   3561   asection *sec;
   3562   struct section_list *next;
   3563   struct section_list *prev;
   3564 }
   3565 section_list;
   3566 
   3567 typedef struct
   3568 {
   3569   void *finfo;
   3570   struct bfd_link_info *info;
   3571   asection *sec;
   3572   int sec_shndx;
   3573   int (*func) (void *, const char *, Elf_Internal_Sym *,
   3574 	       asection *, struct elf_link_hash_entry *);
   3575 } output_arch_syminfo;
   3576 
   3577 /* Output a single local symbol for a generated stub.  */
   3578 
   3579 static bool
   3580 elfNN_kvx_output_stub_sym (output_arch_syminfo *osi, const char *name,
   3581 			       bfd_vma offset, bfd_vma size)
   3582 {
   3583   Elf_Internal_Sym sym;
   3584 
   3585   sym.st_value = (osi->sec->output_section->vma
   3586 		  + osi->sec->output_offset + offset);
   3587   sym.st_size = size;
   3588   sym.st_other = 0;
   3589   sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
   3590   sym.st_shndx = osi->sec_shndx;
   3591   return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
   3592 }
   3593 
   3594 static bool
   3595 kvx_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   3596 {
   3597   struct elf_kvx_stub_hash_entry *stub_entry;
   3598   asection *stub_sec;
   3599   bfd_vma addr;
   3600   char *stub_name;
   3601   output_arch_syminfo *osi;
   3602 
   3603   /* Massage our args to the form they really have.  */
   3604   stub_entry = (struct elf_kvx_stub_hash_entry *) gen_entry;
   3605   osi = (output_arch_syminfo *) in_arg;
   3606 
   3607   stub_sec = stub_entry->stub_sec;
   3608 
   3609   /* Ensure this stub is attached to the current section being
   3610      processed.  */
   3611   if (stub_sec != osi->sec)
   3612     return true;
   3613 
   3614   addr = (bfd_vma) stub_entry->stub_offset;
   3615 
   3616   stub_name = stub_entry->output_name;
   3617 
   3618   switch (stub_entry->stub_type)
   3619     {
   3620     case kvx_stub_long_branch:
   3621       if (!elfNN_kvx_output_stub_sym
   3622 	  (osi, stub_name, addr, sizeof (elfNN_kvx_long_branch_stub)))
   3623 	return false;
   3624       break;
   3625 
   3626     default:
   3627       abort ();
   3628     }
   3629 
   3630   return true;
   3631 }
   3632 
   3633 /* Output mapping symbols for linker generated sections.  */
   3634 
   3635 static bool
   3636 elfNN_kvx_output_arch_local_syms (bfd *output_bfd,
   3637 				  struct bfd_link_info *info,
   3638 				  void *finfo,
   3639 				  int (*func) (void *, const char *,
   3640 					       Elf_Internal_Sym *,
   3641 					       asection *,
   3642 					       struct elf_link_hash_entry *))
   3643 {
   3644   output_arch_syminfo osi;
   3645   struct elf_kvx_link_hash_table *htab;
   3646 
   3647   htab = elf_kvx_hash_table (info);
   3648 
   3649   osi.finfo = finfo;
   3650   osi.info = info;
   3651   osi.func = func;
   3652 
   3653   /* Long calls stubs.  */
   3654   if (htab->stub_bfd && htab->stub_bfd->sections)
   3655     {
   3656       asection *stub_sec;
   3657 
   3658       for (stub_sec = htab->stub_bfd->sections;
   3659 	   stub_sec != NULL; stub_sec = stub_sec->next)
   3660 	{
   3661 	  /* Ignore non-stub sections.  */
   3662 	  if (!strstr (stub_sec->name, STUB_SUFFIX))
   3663 	    continue;
   3664 
   3665 	  osi.sec = stub_sec;
   3666 
   3667 	  osi.sec_shndx = _bfd_elf_section_from_bfd_section
   3668 	    (output_bfd, osi.sec->output_section);
   3669 
   3670 	  bfd_hash_traverse (&htab->stub_hash_table, kvx_map_one_stub,
   3671 			     &osi);
   3672 	}
   3673     }
   3674 
   3675   /* Finally, output mapping symbols for the PLT.  */
   3676   if (!htab->root.splt || htab->root.splt->size == 0)
   3677     return true;
   3678 
   3679   osi.sec_shndx = _bfd_elf_section_from_bfd_section
   3680     (output_bfd, htab->root.splt->output_section);
   3681   osi.sec = htab->root.splt;
   3682 
   3683   return true;
   3684 
   3685 }
   3686 
   3687 /* Allocate target specific section data.  */
   3688 
   3689 static bool
   3690 elfNN_kvx_new_section_hook (bfd *abfd, asection *sec)
   3691 {
   3692   _kvx_elf_section_data *sdata;
   3693 
   3694   sdata = bfd_zalloc (abfd, sizeof (*sdata));
   3695   if (sdata == NULL)
   3696     return false;
   3697   sec->used_by_bfd = sdata;
   3698 
   3699   return _bfd_elf_new_section_hook (abfd, sec);
   3700 }
   3701 
   3702 /* Create dynamic sections. This is different from the ARM backend in that
   3703    the got, plt, gotplt and their relocation sections are all created in the
   3704    standard part of the bfd elf backend.  */
   3705 
   3706 static bool
   3707 elfNN_kvx_create_dynamic_sections (bfd *dynobj,
   3708 				   struct bfd_link_info *info)
   3709 {
   3710   struct elf_kvx_link_hash_table *htab;
   3711 
   3712   /* We need to create .got section.  */
   3713   if (!kvx_elf_create_got_section (dynobj, info))
   3714     return false;
   3715 
   3716   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   3717     return false;
   3718 
   3719   htab = elf_kvx_hash_table (info);
   3720   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
   3721   if (!bfd_link_pic (info))
   3722     htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
   3723 
   3724   if (!htab->sdynbss || (!bfd_link_pic (info) && !htab->srelbss))
   3725     abort ();
   3726 
   3727   return true;
   3728 }
   3729 
   3730 
   3731 /* Allocate space in .plt, .got and associated reloc sections for
   3732    dynamic relocs.  */
   3733 
   3734 static bool
   3735 elfNN_kvx_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   3736 {
   3737   struct bfd_link_info *info;
   3738   struct elf_kvx_link_hash_table *htab;
   3739   struct elf_dyn_relocs *p;
   3740 
   3741   /* An example of a bfd_link_hash_indirect symbol is versioned
   3742      symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
   3743      -> __gxx_personality_v0(bfd_link_hash_defined)
   3744 
   3745      There is no need to process bfd_link_hash_indirect symbols here
   3746      because we will also be presented with the concrete instance of
   3747      the symbol and elfNN_kvx_copy_indirect_symbol () will have been
   3748      called to copy all relevant data from the generic to the concrete
   3749      symbol instance.  */
   3750   if (h->root.type == bfd_link_hash_indirect)
   3751     return true;
   3752 
   3753   if (h->root.type == bfd_link_hash_warning)
   3754     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3755 
   3756   info = (struct bfd_link_info *) inf;
   3757   htab = elf_kvx_hash_table (info);
   3758 
   3759   if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
   3760     {
   3761       /* Make sure this symbol is output as a dynamic symbol.
   3762 	 Undefined weak syms won't yet be marked as dynamic.  */
   3763       if (h->dynindx == -1 && !h->forced_local)
   3764 	{
   3765 	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3766 	    return false;
   3767 	}
   3768 
   3769       if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
   3770 	{
   3771 	  asection *s = htab->root.splt;
   3772 
   3773 	  /* If this is the first .plt entry, make room for the special
   3774 	     first entry.  */
   3775 	  if (s->size == 0)
   3776 	    s->size += htab->plt_header_size;
   3777 
   3778 	  h->plt.offset = s->size;
   3779 
   3780 	  /* If this symbol is not defined in a regular file, and we are
   3781 	     not generating a shared library, then set the symbol to this
   3782 	     location in the .plt.  This is required to make function
   3783 	     pointers compare as equal between the normal executable and
   3784 	     the shared library.  */
   3785 	  if (!bfd_link_pic (info) && !h->def_regular)
   3786 	    {
   3787 	      h->root.u.def.section = s;
   3788 	      h->root.u.def.value = h->plt.offset;
   3789 	    }
   3790 
   3791 	  /* Make room for this entry. For now we only create the
   3792 	     small model PLT entries. We later need to find a way
   3793 	     of relaxing into these from the large model PLT entries.  */
   3794 	  s->size += PLT_SMALL_ENTRY_SIZE;
   3795 
   3796 	  /* We also need to make an entry in the .got.plt section, which
   3797 	     will be placed in the .got section by the linker script.  */
   3798 	  htab->root.sgotplt->size += GOT_ENTRY_SIZE;
   3799 
   3800 	  /* We also need to make an entry in the .rela.plt section.  */
   3801 	  htab->root.srelplt->size += RELOC_SIZE (htab);
   3802 
   3803 	  /* We need to ensure that all GOT entries that serve the PLT
   3804 	     are consecutive with the special GOT slots [0] [1] and
   3805 	     [2]. Any addtional relocations must be placed after the
   3806 	     PLT related entries.  We abuse the reloc_count such that
   3807 	     during sizing we adjust reloc_count to indicate the
   3808 	     number of PLT related reserved entries.  In subsequent
   3809 	     phases when filling in the contents of the reloc entries,
   3810 	     PLT related entries are placed by computing their PLT
   3811 	     index (0 .. reloc_count). While other none PLT relocs are
   3812 	     placed at the slot indicated by reloc_count and
   3813 	     reloc_count is updated.  */
   3814 
   3815 	  htab->root.srelplt->reloc_count++;
   3816 	}
   3817       else
   3818 	{
   3819 	  h->plt.offset = (bfd_vma) - 1;
   3820 	  h->needs_plt = 0;
   3821 	}
   3822     }
   3823   else
   3824     {
   3825       h->plt.offset = (bfd_vma) - 1;
   3826       h->needs_plt = 0;
   3827     }
   3828 
   3829   if (h->got.refcount > 0)
   3830     {
   3831       bool dyn;
   3832       unsigned got_type = elf_kvx_hash_entry (h)->got_type;
   3833 
   3834       h->got.offset = (bfd_vma) - 1;
   3835 
   3836       dyn = htab->root.dynamic_sections_created;
   3837 
   3838       /* Make sure this symbol is output as a dynamic symbol.
   3839 	 Undefined weak syms won't yet be marked as dynamic.  */
   3840       if (dyn && h->dynindx == -1 && !h->forced_local)
   3841 	{
   3842 	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3843 	    return false;
   3844 	}
   3845 
   3846       if (got_type == GOT_UNKNOWN)
   3847 	{
   3848 	  (*_bfd_error_handler)
   3849 	    (_("relocation against `%s' has faulty GOT type "),
   3850 	     (h) ? h->root.root.string : "a local symbol");
   3851 	  bfd_set_error (bfd_error_bad_value);
   3852 	  return false;
   3853 	}
   3854       else if (got_type == GOT_NORMAL)
   3855 	{
   3856 	  h->got.offset = htab->root.sgot->size;
   3857 	  htab->root.sgot->size += GOT_ENTRY_SIZE;
   3858 	  if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3859 	       || h->root.type != bfd_link_hash_undefweak)
   3860 	      && (bfd_link_pic (info)
   3861 		  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
   3862 	    {
   3863 	      htab->root.srelgot->size += RELOC_SIZE (htab);
   3864 	    }
   3865 	}
   3866       else
   3867 	{
   3868 	  int indx;
   3869 
   3870 	  /* Any of these will require 2 GOT slots because
   3871 	   * they use __tls_get_addr() */
   3872 	  if (got_type & (GOT_TLS_GD | GOT_TLS_LD))
   3873 	    {
   3874 	      h->got.offset = htab->root.sgot->size;
   3875 	      htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
   3876 	    }
   3877 
   3878 	  if (got_type & GOT_TLS_IE)
   3879 	    {
   3880 	      h->got.offset = htab->root.sgot->size;
   3881 	      htab->root.sgot->size += GOT_ENTRY_SIZE;
   3882 	    }
   3883 
   3884 	  indx = h && h->dynindx != -1 ? h->dynindx : 0;
   3885 	  if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   3886 	       || h->root.type != bfd_link_hash_undefweak)
   3887 	      && (bfd_link_pic (info)
   3888 		  || indx != 0
   3889 		  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
   3890 	    {
   3891 	      /* Only the GD case requires 2 relocations. */
   3892 	      if (got_type & GOT_TLS_GD)
   3893 		htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
   3894 
   3895 	      /* LD needs a DTPMOD reloc, IE needs a DTPOFF. */
   3896 	      if (got_type & (GOT_TLS_LD | GOT_TLS_IE))
   3897 		htab->root.srelgot->size += RELOC_SIZE (htab);
   3898 	    }
   3899 	}
   3900     }
   3901   else
   3902     {
   3903       h->got.offset = (bfd_vma) - 1;
   3904     }
   3905 
   3906   if (h->dyn_relocs == NULL)
   3907     return true;
   3908 
   3909   /* In the shared -Bsymbolic case, discard space allocated for
   3910      dynamic pc-relative relocs against symbols which turn out to be
   3911      defined in regular objects.  For the normal shared case, discard
   3912      space for pc-relative relocs that have become local due to symbol
   3913      visibility changes.  */
   3914 
   3915   if (bfd_link_pic (info))
   3916     {
   3917       /* Relocs that use pc_count are those that appear on a call
   3918 	 insn, or certain REL relocs that can generated via assembly.
   3919 	 We want calls to protected symbols to resolve directly to the
   3920 	 function rather than going via the plt.  If people want
   3921 	 function pointer comparisons to work as expected then they
   3922 	 should avoid writing weird assembly.  */
   3923       if (SYMBOL_CALLS_LOCAL (info, h))
   3924 	{
   3925 	  struct elf_dyn_relocs **pp;
   3926 
   3927 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
   3928 	    {
   3929 	      p->count -= p->pc_count;
   3930 	      p->pc_count = 0;
   3931 	      if (p->count == 0)
   3932 		*pp = p->next;
   3933 	      else
   3934 		pp = &p->next;
   3935 	    }
   3936 	}
   3937 
   3938       /* Also discard relocs on undefined weak syms with non-default
   3939 	 visibility.  */
   3940       if (h->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
   3941 	{
   3942 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   3943 	      || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   3944 	    h->dyn_relocs = NULL;
   3945 
   3946 	  /* Make sure undefined weak symbols are output as a dynamic
   3947 	     symbol in PIEs.  */
   3948 	  else if (h->dynindx == -1
   3949 		   && !h->forced_local
   3950 		   && !bfd_elf_link_record_dynamic_symbol (info, h))
   3951 	    return false;
   3952 	}
   3953 
   3954     }
   3955   else if (ELIMINATE_COPY_RELOCS)
   3956     {
   3957       /* For the non-shared case, discard space for relocs against
   3958 	 symbols which turn out to need copy relocs or are not
   3959 	 dynamic.  */
   3960 
   3961       if (!h->non_got_ref
   3962 	  && ((h->def_dynamic
   3963 	       && !h->def_regular)
   3964 	      || (htab->root.dynamic_sections_created
   3965 		  && (h->root.type == bfd_link_hash_undefweak
   3966 		      || h->root.type == bfd_link_hash_undefined))))
   3967 	{
   3968 	  /* Make sure this symbol is output as a dynamic symbol.
   3969 	     Undefined weak syms won't yet be marked as dynamic.  */
   3970 	  if (h->dynindx == -1
   3971 	      && !h->forced_local
   3972 	      && !bfd_elf_link_record_dynamic_symbol (info, h))
   3973 	    return false;
   3974 
   3975 	  /* If that succeeded, we know we'll be keeping all the
   3976 	     relocs.  */
   3977 	  if (h->dynindx != -1)
   3978 	    goto keep;
   3979 	}
   3980 
   3981       h->dyn_relocs = NULL;
   3982 
   3983     keep:;
   3984     }
   3985 
   3986   /* Finally, allocate space.  */
   3987   for (p = h->dyn_relocs; p != NULL; p = p->next)
   3988     {
   3989       asection *sreloc;
   3990 
   3991       sreloc = elf_section_data (p->sec)->sreloc;
   3992 
   3993       BFD_ASSERT (sreloc != NULL);
   3994 
   3995       sreloc->size += p->count * RELOC_SIZE (htab);
   3996     }
   3997 
   3998   return true;
   3999 }
   4000 
   4001 /* Find any dynamic relocs that apply to read-only sections.  */
   4002 
   4003 static bool
   4004 kvx_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
   4005 {
   4006   struct elf_dyn_relocs * p;
   4007 
   4008   for (p = h->dyn_relocs; p != NULL; p = p->next)
   4009     {
   4010       asection *s = p->sec;
   4011 
   4012       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   4013 	{
   4014 	  struct bfd_link_info *info = (struct bfd_link_info *) inf;
   4015 
   4016 	  info->flags |= DF_TEXTREL;
   4017 	  info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' in "
   4018 				    "read-only section `%pA'\n"),
   4019 				  s->owner, h->root.root.string, s);
   4020 
   4021 	  /* Not an error, just cut short the traversal.  */
   4022 	  return false;
   4023 	}
   4024     }
   4025   return true;
   4026 }
   4027 
   4028 /* This is the most important function of all . Innocuosly named
   4029    though !  */
   4030 static bool
   4031 elfNN_kvx_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   4032 			      struct bfd_link_info *info)
   4033 {
   4034   struct elf_kvx_link_hash_table *htab;
   4035   bfd *dynobj;
   4036   asection *s;
   4037   bool relocs;
   4038   bfd *ibfd;
   4039 
   4040   htab = elf_kvx_hash_table ((info));
   4041   dynobj = htab->root.dynobj;
   4042   if (dynobj == NULL)
   4043     return true;
   4044 
   4045   if (htab->root.dynamic_sections_created)
   4046     {
   4047       if (bfd_link_executable (info) && !info->nointerp)
   4048 	{
   4049 	  s = htab->root.interp;
   4050 	  if (s == NULL)
   4051 	    abort ();
   4052 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   4053 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   4054 	  s->alloced = 1;
   4055 	}
   4056     }
   4057 
   4058   /* Set up .got offsets for local syms, and space for local dynamic
   4059      relocs.  */
   4060   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   4061     {
   4062       struct elf_kvx_local_symbol *locals = NULL;
   4063       Elf_Internal_Shdr *symtab_hdr;
   4064       asection *srel;
   4065       unsigned int i;
   4066 
   4067       if (!is_kvx_elf (ibfd))
   4068 	continue;
   4069 
   4070       for (s = ibfd->sections; s != NULL; s = s->next)
   4071 	{
   4072 	  struct elf_dyn_relocs *p;
   4073 
   4074 	  for (p = (struct elf_dyn_relocs *)
   4075 		 (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
   4076 	    {
   4077 	      if (!bfd_is_abs_section (p->sec)
   4078 		  && bfd_is_abs_section (p->sec->output_section))
   4079 		{
   4080 		  /* Input section has been discarded, either because
   4081 		     it is a copy of a linkonce section or due to
   4082 		     linker script /DISCARD/, so we'll be discarding
   4083 		     the relocs too.  */
   4084 		}
   4085 	      else if (p->count != 0)
   4086 		{
   4087 		  srel = elf_section_data (p->sec)->sreloc;
   4088 		  srel->size += p->count * RELOC_SIZE (htab);
   4089 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   4090 		    info->flags |= DF_TEXTREL;
   4091 		}
   4092 	    }
   4093 	}
   4094 
   4095       locals = elf_kvx_locals (ibfd);
   4096       if (!locals)
   4097 	continue;
   4098 
   4099       symtab_hdr = &elf_symtab_hdr (ibfd);
   4100       srel = htab->root.srelgot;
   4101       for (i = 0; i < symtab_hdr->sh_info; i++)
   4102 	{
   4103 	  locals[i].got_offset = (bfd_vma) - 1;
   4104 	  if (locals[i].got_refcount > 0)
   4105 	    {
   4106 	      unsigned got_type = locals[i].got_type;
   4107 	      if (got_type & (GOT_TLS_GD | GOT_TLS_LD))
   4108 		{
   4109 		  locals[i].got_offset = htab->root.sgot->size;
   4110 		  htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
   4111 		}
   4112 
   4113 	      if (got_type & (GOT_NORMAL | GOT_TLS_IE ))
   4114 		{
   4115 		  locals[i].got_offset = htab->root.sgot->size;
   4116 		  htab->root.sgot->size += GOT_ENTRY_SIZE;
   4117 		}
   4118 
   4119 	      if (got_type == GOT_UNKNOWN)
   4120 		{
   4121 		}
   4122 
   4123 	      if (bfd_link_pic (info))
   4124 		{
   4125 		  if (got_type & GOT_TLS_GD)
   4126 		    htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
   4127 
   4128 		  if (got_type & GOT_TLS_IE
   4129 		      || got_type & GOT_TLS_LD
   4130 		      || got_type & GOT_NORMAL)
   4131 		    htab->root.srelgot->size += RELOC_SIZE (htab);
   4132 		}
   4133 	    }
   4134 	  else
   4135 	    {
   4136 	      locals[i].got_refcount = (bfd_vma) - 1;
   4137 	    }
   4138 	}
   4139     }
   4140 
   4141 
   4142   /* Allocate global sym .plt and .got entries, and space for global
   4143      sym dynamic relocs.  */
   4144   elf_link_hash_traverse (&htab->root, elfNN_kvx_allocate_dynrelocs,
   4145 			  info);
   4146 
   4147   /* For every jump slot reserved in the sgotplt, reloc_count is
   4148      incremented.  However, when we reserve space for TLS descriptors,
   4149      it's not incremented, so in order to compute the space reserved
   4150      for them, it suffices to multiply the reloc count by the jump
   4151      slot size.  */
   4152 
   4153   if (htab->root.srelplt)
   4154     htab->sgotplt_jump_table_size = kvx_compute_jump_table_size (htab);
   4155 
   4156   /* We now have determined the sizes of the various dynamic sections.
   4157      Allocate memory for them.  */
   4158   relocs = false;
   4159   for (s = dynobj->sections; s != NULL; s = s->next)
   4160     {
   4161       if ((s->flags & SEC_LINKER_CREATED) == 0)
   4162 	continue;
   4163 
   4164       if (s == htab->root.splt
   4165 	  || s == htab->root.sgot
   4166 	  || s == htab->root.sgotplt
   4167 	  || s == htab->root.iplt
   4168 	  || s == htab->root.igotplt || s == htab->sdynbss)
   4169 	{
   4170 	  /* Strip this section if we don't need it; see the
   4171 	     comment below.  */
   4172 	}
   4173       else if (startswith (bfd_section_name (s), ".rela"))
   4174 	{
   4175 	  if (s->size != 0 && s != htab->root.srelplt)
   4176 	    relocs = true;
   4177 
   4178 	  /* We use the reloc_count field as a counter if we need
   4179 	     to copy relocs into the output file.  */
   4180 	  if (s != htab->root.srelplt)
   4181 	    s->reloc_count = 0;
   4182 	}
   4183       else
   4184 	{
   4185 	  /* It's not one of our sections, so don't allocate space.  */
   4186 	  continue;
   4187 	}
   4188 
   4189       if (s->size == 0)
   4190 	{
   4191 	  /* If we don't need this section, strip it from the
   4192 	     output file.  This is mostly to handle .rela.bss and
   4193 	     .rela.plt.  We must create both sections in
   4194 	     create_dynamic_sections, because they must be created
   4195 	     before the linker maps input sections to output
   4196 	     sections.  The linker does that before
   4197 	     adjust_dynamic_symbol is called, and it is that
   4198 	     function which decides whether anything needs to go
   4199 	     into these sections.  */
   4200 
   4201 	  s->flags |= SEC_EXCLUDE;
   4202 	  continue;
   4203 	}
   4204 
   4205       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   4206 	continue;
   4207 
   4208       /* Allocate memory for the section contents.  We use bfd_zalloc
   4209 	 here in case unused entries are not reclaimed before the
   4210 	 section's contents are written out.  This should not happen,
   4211 	 but this way if it does, we get a R_KVX_NONE reloc instead
   4212 	 of garbage.  */
   4213       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
   4214       if (s->contents == NULL)
   4215 	return false;
   4216       s->alloced = 1;
   4217     }
   4218 
   4219   if (htab->root.dynamic_sections_created)
   4220     {
   4221       /* Add some entries to the .dynamic section.  We fill in the
   4222 	 values later, in elfNN_kvx_finish_dynamic_sections, but we
   4223 	 must add the entries now so that we get the correct size for
   4224 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   4225 	 dynamic linker and used by the debugger.  */
   4226 #define add_dynamic_entry(TAG, VAL)			\
   4227       _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   4228 
   4229       if (bfd_link_executable (info))
   4230 	{
   4231 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   4232 	    return false;
   4233 	}
   4234 
   4235       if (htab->root.splt->size != 0)
   4236 	{
   4237 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   4238 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   4239 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   4240 	      || !add_dynamic_entry (DT_JMPREL, 0))
   4241 	    return false;
   4242 	}
   4243 
   4244       if (relocs)
   4245 	{
   4246 	  if (!add_dynamic_entry (DT_RELA, 0)
   4247 	      || !add_dynamic_entry (DT_RELASZ, 0)
   4248 	      || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
   4249 	    return false;
   4250 
   4251 	  /* If any dynamic relocs apply to a read-only section,
   4252 	     then we need a DT_TEXTREL entry.  */
   4253 	  if ((info->flags & DF_TEXTREL) == 0)
   4254 	    elf_link_hash_traverse (&htab->root, kvx_readonly_dynrelocs,
   4255 				    info);
   4256 
   4257 	  if ((info->flags & DF_TEXTREL) != 0)
   4258 	    {
   4259 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   4260 		return false;
   4261 	    }
   4262 	}
   4263     }
   4264 #undef add_dynamic_entry
   4265 
   4266   return true;
   4267 }
   4268 
   4269 static inline void
   4270 elf_kvx_update_plt_entry (bfd *output_bfd,
   4271 			  bfd_reloc_code_real_type r_type,
   4272 			  bfd_byte *plt_entry, bfd_vma value)
   4273 {
   4274   reloc_howto_type *howto = elfNN_kvx_howto_from_bfd_reloc (r_type);
   4275   BFD_ASSERT(howto != NULL);
   4276   _bfd_kvx_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
   4277 }
   4278 
   4279 static void
   4280 elfNN_kvx_create_small_pltn_entry (struct elf_link_hash_entry *h,
   4281 				   struct elf_kvx_link_hash_table *htab,
   4282 				   bfd *output_bfd)
   4283 {
   4284   bfd_byte *plt_entry;
   4285   bfd_vma plt_index;
   4286   bfd_vma got_offset;
   4287   bfd_vma gotplt_entry_address;
   4288   bfd_vma plt_entry_address;
   4289   Elf_Internal_Rela rela;
   4290   bfd_byte *loc;
   4291   asection *plt, *gotplt, *relplt;
   4292 
   4293   plt = htab->root.splt;
   4294   gotplt = htab->root.sgotplt;
   4295   relplt = htab->root.srelplt;
   4296 
   4297   /* Get the index in the procedure linkage table which
   4298      corresponds to this symbol.  This is the index of this symbol
   4299      in all the symbols for which we are making plt entries.  The
   4300      first entry in the procedure linkage table is reserved.
   4301 
   4302      Get the offset into the .got table of the entry that
   4303      corresponds to this function.	Each .got entry is GOT_ENTRY_SIZE
   4304      bytes. The first three are reserved for the dynamic linker.
   4305 
   4306      For static executables, we don't reserve anything.  */
   4307 
   4308   if (plt == htab->root.splt)
   4309     {
   4310       plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
   4311       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
   4312     }
   4313   else
   4314     {
   4315       plt_index = h->plt.offset / htab->plt_entry_size;
   4316       got_offset = plt_index * GOT_ENTRY_SIZE;
   4317     }
   4318 
   4319   plt_entry = plt->contents + h->plt.offset;
   4320   plt_entry_address = plt->output_section->vma
   4321     + plt->output_offset + h->plt.offset;
   4322   gotplt_entry_address = gotplt->output_section->vma +
   4323     gotplt->output_offset + got_offset;
   4324 
   4325   /* Copy in the boiler-plate for the PLTn entry.  */
   4326   memcpy (plt_entry, elfNN_kvx_small_plt_entry, PLT_SMALL_ENTRY_SIZE);
   4327 
   4328   /* Patch the loading of the GOT entry, relative to the PLT entry
   4329      address. */
   4330 
   4331   /* Use 37bits offset for both 32 and 64bits mode.
   4332      Fill the LO10 of of lw $r9 = 0[$r14].  */
   4333   elf_kvx_update_plt_entry(output_bfd, BFD_RELOC_KVX_S37_LO10,
   4334 			   plt_entry+4,
   4335 			   gotplt_entry_address - plt_entry_address);
   4336 
   4337   /* Fill the UP27 of of lw $r9 = 0[$r14].  */
   4338   elf_kvx_update_plt_entry(output_bfd, BFD_RELOC_KVX_S37_UP27,
   4339 			   plt_entry+8,
   4340 			   gotplt_entry_address - plt_entry_address);
   4341 
   4342   rela.r_offset = gotplt_entry_address;
   4343 
   4344   /* Fill in the entry in the .rela.plt section.  */
   4345   rela.r_info = ELFNN_R_INFO (h->dynindx, R_KVX_JMP_SLOT);
   4346   rela.r_addend = 0;
   4347 
   4348   /* Compute the relocation entry to used based on PLT index and do
   4349      not adjust reloc_count. The reloc_count has already been adjusted
   4350      to account for this entry.  */
   4351   loc = relplt->contents + plt_index * RELOC_SIZE (htab);
   4352   bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   4353 }
   4354 
   4355 /* Size sections even though they're not dynamic.  We use it to setup
   4356    _TLS_MODULE_BASE_, if needed.  */
   4357 
   4358 static bool
   4359 elfNN_kvx_early_size_sections (bfd *output_bfd, struct bfd_link_info *info)
   4360 {
   4361   asection *tls_sec;
   4362 
   4363   if (bfd_link_relocatable (info))
   4364     return true;
   4365 
   4366   tls_sec = elf_hash_table (info)->tls_sec;
   4367 
   4368   if (tls_sec)
   4369     {
   4370       struct elf_link_hash_entry *tlsbase;
   4371 
   4372       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
   4373 				      "_TLS_MODULE_BASE_", true, true, false);
   4374 
   4375       if (tlsbase)
   4376 	{
   4377 	  struct bfd_link_hash_entry *h = NULL;
   4378 	  elf_backend_data *bed = get_elf_backend_data (output_bfd);
   4379 
   4380 	  if (!(_bfd_generic_link_add_one_symbol
   4381 		(info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
   4382 		 tls_sec, 0, NULL, false, bed->collect, &h)))
   4383 	    return false;
   4384 
   4385 	  tlsbase->type = STT_TLS;
   4386 	  tlsbase = (struct elf_link_hash_entry *) h;
   4387 	  tlsbase->def_regular = 1;
   4388 	  tlsbase->other = STV_HIDDEN;
   4389 	  (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
   4390 	}
   4391     }
   4392 
   4393   return true;
   4394 }
   4395 
   4396 /* Finish up dynamic symbol handling.  We set the contents of various
   4397    dynamic sections here.  */
   4398 static bool
   4399 elfNN_kvx_finish_dynamic_symbol (bfd *output_bfd,
   4400 				 struct bfd_link_info *info,
   4401 				 struct elf_link_hash_entry *h,
   4402 				 Elf_Internal_Sym *sym)
   4403 {
   4404   struct elf_kvx_link_hash_table *htab;
   4405   htab = elf_kvx_hash_table (info);
   4406 
   4407   if (h->plt.offset != (bfd_vma) - 1)
   4408     {
   4409       asection *plt = NULL, *gotplt = NULL, *relplt = NULL;
   4410 
   4411       /* This symbol has an entry in the procedure linkage table.  Set
   4412 	 it up.  */
   4413 
   4414       if (htab->root.splt != NULL)
   4415 	{
   4416 	  plt = htab->root.splt;
   4417 	  gotplt = htab->root.sgotplt;
   4418 	  relplt = htab->root.srelplt;
   4419 	}
   4420 
   4421       /* This symbol has an entry in the procedure linkage table.  Set
   4422 	 it up.	 */
   4423       if ((h->dynindx == -1
   4424 	   && !((h->forced_local || bfd_link_executable (info))
   4425 		&& h->def_regular
   4426 		&& h->type == STT_GNU_IFUNC))
   4427 	  || plt == NULL
   4428 	  || gotplt == NULL
   4429 	  || relplt == NULL)
   4430 	abort ();
   4431 
   4432       elfNN_kvx_create_small_pltn_entry (h, htab, output_bfd);
   4433       if (!h->def_regular)
   4434 	{
   4435 	  /* Mark the symbol as undefined, rather than as defined in
   4436 	     the .plt section.  */
   4437 	  sym->st_shndx = SHN_UNDEF;
   4438 	  /* If the symbol is weak we need to clear the value.
   4439 	     Otherwise, the PLT entry would provide a definition for
   4440 	     the symbol even if the symbol wasn't defined anywhere,
   4441 	     and so the symbol would never be NULL.  Leave the value if
   4442 	     there were any relocations where pointer equality matters
   4443 	     (this is a clue for the dynamic linker, to make function
   4444 	     pointer comparisons work between an application and shared
   4445 	     library).  */
   4446 	  if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
   4447 	    sym->st_value = 0;
   4448 	}
   4449     }
   4450 
   4451   if (h->got.offset != (bfd_vma) - 1
   4452       && elf_kvx_hash_entry (h)->got_type == GOT_NORMAL)
   4453     {
   4454       Elf_Internal_Rela rela;
   4455       bfd_byte *loc;
   4456 
   4457       /* This symbol has an entry in the global offset table.  Set it
   4458 	 up.  */
   4459       if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
   4460 	abort ();
   4461 
   4462       rela.r_offset = (htab->root.sgot->output_section->vma
   4463 		       + htab->root.sgot->output_offset
   4464 		       + (h->got.offset & ~(bfd_vma) 1));
   4465 
   4466 #ifdef UGLY_DEBUG
   4467       printf("setting rela at offset 0x%x(0x%x + 0x%x + 0x%x) for %s\n",
   4468 	     rela.r_offset,
   4469 	     htab->root.sgot->output_section->vma,
   4470 	     htab->root.sgot->output_offset,
   4471 	     h->got.offset,
   4472 	     h->root.root.string);
   4473 #endif
   4474 
   4475       if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
   4476 	{
   4477 	  if (!h->def_regular)
   4478 	    return false;
   4479 
   4480 	  /* in case of PLT related GOT entry, it is not clear who is
   4481 	     supposed to set the LSB of GOT entry...
   4482 	     kvx_calculate_got_entry_vma() would be a good candidate,
   4483 	     but it is not called currently
   4484 	     So we are commenting it ATM.  */
   4485 	  // BFD_ASSERT ((h->got.offset & 1) != 0);
   4486 	  rela.r_info = ELFNN_R_INFO (0, R_KVX_RELATIVE);
   4487 	  rela.r_addend = (h->root.u.def.value
   4488 			   + h->root.u.def.section->output_section->vma
   4489 			   + h->root.u.def.section->output_offset);
   4490 	}
   4491       else
   4492 	{
   4493 	  BFD_ASSERT ((h->got.offset & 1) == 0);
   4494 	  bfd_put_NN (output_bfd, (bfd_vma) 0,
   4495 		      htab->root.sgot->contents + h->got.offset);
   4496 	  rela.r_info = ELFNN_R_INFO (h->dynindx, R_KVX_GLOB_DAT);
   4497 	  rela.r_addend = 0;
   4498 	}
   4499 
   4500       loc = htab->root.srelgot->contents;
   4501       loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
   4502       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   4503     }
   4504 
   4505   if (h->needs_copy)
   4506     {
   4507       Elf_Internal_Rela rela;
   4508       bfd_byte *loc;
   4509 
   4510       /* This symbol needs a copy reloc.  Set it up.  */
   4511 
   4512       if (h->dynindx == -1
   4513 	  || (h->root.type != bfd_link_hash_defined
   4514 	      && h->root.type != bfd_link_hash_defweak)
   4515 	  || htab->srelbss == NULL)
   4516 	abort ();
   4517 
   4518       rela.r_offset = (h->root.u.def.value
   4519 		       + h->root.u.def.section->output_section->vma
   4520 		       + h->root.u.def.section->output_offset);
   4521       rela.r_info = ELFNN_R_INFO (h->dynindx, R_KVX_COPY);
   4522       rela.r_addend = 0;
   4523       loc = htab->srelbss->contents;
   4524       loc += htab->srelbss->reloc_count++ * RELOC_SIZE (htab);
   4525       bfd_elfNN_swap_reloca_out (output_bfd, &rela, loc);
   4526     }
   4527 
   4528   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
   4529      be NULL for local symbols.  */
   4530   if (sym != NULL
   4531       && (h == elf_hash_table (info)->hdynamic
   4532 	  || h == elf_hash_table (info)->hgot))
   4533     sym->st_shndx = SHN_ABS;
   4534 
   4535   return true;
   4536 }
   4537 
   4538 static void
   4539 elfNN_kvx_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
   4540 				 struct elf_kvx_link_hash_table *htab)
   4541 {
   4542   memcpy (htab->root.splt->contents, elfNN_kvx_small_plt0_entry,
   4543 	  PLT_ENTRY_SIZE);
   4544   elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
   4545     PLT_ENTRY_SIZE;
   4546 }
   4547 
   4548 static bool
   4549 elfNN_kvx_finish_dynamic_sections (bfd *output_bfd,
   4550 				   struct bfd_link_info *info,
   4551 				   bfd_byte *buf ATTRIBUTE_UNUSED)
   4552 {
   4553   struct elf_kvx_link_hash_table *htab;
   4554   bfd *dynobj;
   4555   asection *sdyn;
   4556 
   4557   htab = elf_kvx_hash_table (info);
   4558   dynobj = htab->root.dynobj;
   4559   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   4560 
   4561   if (htab->root.dynamic_sections_created)
   4562     {
   4563       ElfNN_External_Dyn *dyncon, *dynconend;
   4564 
   4565       if (sdyn == NULL || htab->root.sgot == NULL)
   4566 	abort ();
   4567 
   4568       dyncon = (ElfNN_External_Dyn *) sdyn->contents;
   4569       dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
   4570       for (; dyncon < dynconend; dyncon++)
   4571 	{
   4572 	  Elf_Internal_Dyn dyn;
   4573 	  asection *s;
   4574 
   4575 	  bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
   4576 
   4577 	  switch (dyn.d_tag)
   4578 	    {
   4579 	    default:
   4580 	      continue;
   4581 
   4582 	    case DT_PLTGOT:
   4583 	      s = htab->root.sgotplt;
   4584 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   4585 	      break;
   4586 
   4587 	    case DT_JMPREL:
   4588 	      s = htab->root.srelplt;
   4589 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   4590 	      break;
   4591 
   4592 	    case DT_PLTRELSZ:
   4593 	      s = htab->root.srelplt;
   4594 	      dyn.d_un.d_val = s->size;
   4595 	      break;
   4596 
   4597 	    case DT_RELASZ:
   4598 	      /* The procedure linkage table relocs (DT_JMPREL) should
   4599 		 not be included in the overall relocs (DT_RELA).
   4600 		 Therefore, we override the DT_RELASZ entry here to
   4601 		 make it not include the JMPREL relocs.  Since the
   4602 		 linker script arranges for .rela.plt to follow all
   4603 		 other relocation sections, we don't have to worry
   4604 		 about changing the DT_RELA entry.  */
   4605 	      if (htab->root.srelplt != NULL)
   4606 		{
   4607 		  s = htab->root.srelplt;
   4608 		  dyn.d_un.d_val -= s->size;
   4609 		}
   4610 	      break;
   4611 	    }
   4612 
   4613 	  bfd_elfNN_swap_dyn_out (output_bfd, &dyn, dyncon);
   4614 	}
   4615 
   4616     }
   4617 
   4618   /* Fill in the special first entry in the procedure linkage table.  */
   4619   if (htab->root.splt && htab->root.splt->size > 0)
   4620     {
   4621       elfNN_kvx_init_small_plt0_entry (output_bfd, htab);
   4622 
   4623       elf_section_data (htab->root.splt->output_section)->
   4624 	this_hdr.sh_entsize = htab->plt_entry_size;
   4625     }
   4626 
   4627   if (htab->root.sgotplt)
   4628     {
   4629       if (bfd_is_abs_section (htab->root.sgotplt->output_section))
   4630 	{
   4631 	  (*_bfd_error_handler)
   4632 	    (_("discarded output section: `%pA'"), htab->root.sgotplt);
   4633 	  return false;
   4634 	}
   4635 
   4636       /* Fill in the first three entries in the global offset table.  */
   4637       if (htab->root.sgotplt->size > 0)
   4638 	{
   4639 	  bfd_put_NN (output_bfd, (bfd_vma) 0, htab->root.sgotplt->contents);
   4640 
   4641 	  /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
   4642 	  bfd_put_NN (output_bfd,
   4643 		      (bfd_vma) 0,
   4644 		      htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
   4645 	  bfd_put_NN (output_bfd,
   4646 		      (bfd_vma) 0,
   4647 		      htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
   4648 	}
   4649 
   4650       if (htab->root.sgot)
   4651 	{
   4652 	  if (htab->root.sgot->size > 0)
   4653 	    {
   4654 	      bfd_vma addr =
   4655 		sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
   4656 	      bfd_put_NN (output_bfd, addr, htab->root.sgot->contents);
   4657 	    }
   4658 	}
   4659 
   4660       elf_section_data (htab->root.sgotplt->output_section)->
   4661 	this_hdr.sh_entsize = GOT_ENTRY_SIZE;
   4662     }
   4663 
   4664   if (htab->root.sgot && htab->root.sgot->size > 0)
   4665     elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
   4666       = GOT_ENTRY_SIZE;
   4667 
   4668   return true;
   4669 }
   4670 
   4671 /* Return address for Ith PLT stub in section PLT, for relocation REL
   4672    or (bfd_vma) -1 if it should not be included.  */
   4673 
   4674 static bfd_vma
   4675 elfNN_kvx_plt_sym_val (bfd_vma i, const asection *plt,
   4676 		       const arelent *rel ATTRIBUTE_UNUSED)
   4677 {
   4678   return plt->vma + PLT_ENTRY_SIZE + i * PLT_SMALL_ENTRY_SIZE;
   4679 }
   4680 
   4681 #define ELF_ARCH			bfd_arch_kvx
   4682 #define ELF_TARGET_ID			KVX_ELF_DATA
   4683 #define ELF_MACHINE_CODE		EM_KVX
   4684 #define ELF_MAXPAGESIZE			0x10000
   4685 #define ELF_MINPAGESIZE			0x1000
   4686 #define ELF_COMMONPAGESIZE		0x1000
   4687 
   4688 #define bfd_elfNN_bfd_link_hash_table_create    \
   4689   elfNN_kvx_link_hash_table_create
   4690 
   4691 #define bfd_elfNN_bfd_merge_private_bfd_data	\
   4692   elfNN_kvx_merge_private_bfd_data
   4693 
   4694 #define bfd_elfNN_bfd_print_private_bfd_data	\
   4695   elfNN_kvx_print_private_bfd_data
   4696 
   4697 #define bfd_elfNN_bfd_reloc_type_lookup		\
   4698   elfNN_kvx_reloc_type_lookup
   4699 
   4700 #define bfd_elfNN_bfd_reloc_name_lookup		\
   4701   elfNN_kvx_reloc_name_lookup
   4702 
   4703 #define bfd_elfNN_bfd_set_private_flags		\
   4704   elfNN_kvx_set_private_flags
   4705 
   4706 #define bfd_elfNN_mkobject			\
   4707   elfNN_kvx_mkobject
   4708 
   4709 #define bfd_elfNN_new_section_hook		\
   4710   elfNN_kvx_new_section_hook
   4711 
   4712 #define elf_backend_adjust_dynamic_symbol	\
   4713   elfNN_kvx_adjust_dynamic_symbol
   4714 
   4715 #define elf_backend_early_size_sections		\
   4716   elfNN_kvx_early_size_sections
   4717 
   4718 #define elf_backend_check_relocs		\
   4719   elfNN_kvx_check_relocs
   4720 
   4721 #define elf_backend_copy_indirect_symbol	\
   4722   elfNN_kvx_copy_indirect_symbol
   4723 
   4724 /* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
   4725    to them in our hash.  */
   4726 #define elf_backend_create_dynamic_sections	\
   4727   elfNN_kvx_create_dynamic_sections
   4728 
   4729 #define elf_backend_init_index_section		\
   4730   _bfd_elf_init_2_index_sections
   4731 
   4732 #define elf_backend_finish_dynamic_sections	\
   4733   elfNN_kvx_finish_dynamic_sections
   4734 
   4735 #define elf_backend_finish_dynamic_symbol	\
   4736   elfNN_kvx_finish_dynamic_symbol
   4737 
   4738 #define elf_backend_object_p			\
   4739   elfNN_kvx_object_p
   4740 
   4741 #define elf_backend_output_arch_local_syms      \
   4742   elfNN_kvx_output_arch_local_syms
   4743 
   4744 #define elf_backend_plt_sym_val			\
   4745   elfNN_kvx_plt_sym_val
   4746 
   4747 #define elf_backend_init_file_header		\
   4748   elfNN_kvx_init_file_header
   4749 
   4750 #define elf_backend_init_process_headers	\
   4751   elfNN_kvx_init_process_headers
   4752 
   4753 #define elf_backend_relocate_section		\
   4754   elfNN_kvx_relocate_section
   4755 
   4756 #define elf_backend_reloc_type_class		\
   4757   elfNN_kvx_reloc_type_class
   4758 
   4759 #define elf_backend_late_size_sections	\
   4760   elfNN_kvx_late_size_sections
   4761 
   4762 #define elf_backend_can_refcount       1
   4763 #define elf_backend_can_gc_sections    1
   4764 #define elf_backend_plt_readonly       1
   4765 #define elf_backend_want_got_plt       1
   4766 #define elf_backend_want_plt_sym       0
   4767 #define elf_backend_may_use_rel_p      0
   4768 #define elf_backend_may_use_rela_p     1
   4769 #define elf_backend_default_use_rela_p 1
   4770 #define elf_backend_rela_normal        1
   4771 #define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
   4772 #define elf_backend_default_execstack  0
   4773 #define elf_backend_extern_protected_data 1
   4774 #define elf_backend_hash_symbol elf_kvx_hash_symbol
   4775 
   4776 #include "elfNN-target.h"
   4777