Home | History | Annotate | Line # | Download | only in bfd
elfnn-riscv.c revision 1.3.14.1
      1 /* RISC-V-specific support for NN-bit ELF.
      2    Copyright (C) 2011-2018 Free Software Foundation, Inc.
      3 
      4    Contributed by Andrew Waterman (andrew (at) sifive.com).
      5    Based on TILE-Gx and MIPS targets.
      6 
      7    This file is part of BFD, the Binary File Descriptor library.
      8 
      9    This program is free software; you can redistribute it and/or modify
     10    it under the terms of the GNU General Public License as published by
     11    the Free Software Foundation; either version 3 of the License, or
     12    (at your option) any later version.
     13 
     14    This program is distributed in the hope that it will be useful,
     15    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17    GNU General Public License for more details.
     18 
     19    You should have received a copy of the GNU General Public License
     20    along with this program; see the file COPYING3. If not,
     21    see <http://www.gnu.org/licenses/>.  */
     22 
     23 /* This file handles RISC-V ELF targets.  */
     24 
     25 #include "sysdep.h"
     26 #include "bfd.h"
     27 #include "libiberty.h"
     28 #include "libbfd.h"
     29 #include "bfd_stdint.h"
     30 #include "elf-bfd.h"
     31 #include "bfdlink.h"
     32 #include "objalloc.h"
     33 #include "elfxx-riscv.h"
     34 #include "elf/riscv.h"
     35 #include "opcode/riscv.h"
     36 
     37 /* Internal relocations used exclusively by the relaxation pass.  */
     38 #define R_RISCV_DELETE (R_RISCV_max + 1)
     39 
     40 #define ARCH_SIZE NN
     41 
     42 #define MINUS_ONE ((bfd_vma)0 - 1)
     43 
     44 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
     45 
     46 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
     47 
     48 /* The name of the dynamic interpreter.  This is put in the .interp
     49    section.  */
     50 
     51 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
     52 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
     53 
     54 #define ELF_ARCH			bfd_arch_riscv
     55 #define ELF_TARGET_ID			RISCV_ELF_DATA
     56 #define ELF_MACHINE_CODE		EM_RISCV
     57 #define ELF_MAXPAGESIZE			0x1000
     58 #define ELF_COMMONPAGESIZE		0x1000
     59 
     60 /* RISC-V ELF linker hash entry.  */
     61 
     62 struct riscv_elf_link_hash_entry
     63 {
     64   struct elf_link_hash_entry elf;
     65 
     66   /* Track dynamic relocs copied for this symbol.  */
     67   struct elf_dyn_relocs *dyn_relocs;
     68 
     69 #define GOT_UNKNOWN     0
     70 #define GOT_NORMAL      1
     71 #define GOT_TLS_GD      2
     72 #define GOT_TLS_IE      4
     73 #define GOT_TLS_LE      8
     74   char tls_type;
     75 };
     76 
     77 #define riscv_elf_hash_entry(ent) \
     78   ((struct riscv_elf_link_hash_entry *)(ent))
     79 
     80 struct _bfd_riscv_elf_obj_tdata
     81 {
     82   struct elf_obj_tdata root;
     83 
     84   /* tls_type for each local got entry.  */
     85   char *local_got_tls_type;
     86 };
     87 
     88 #define _bfd_riscv_elf_tdata(abfd) \
     89   ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
     90 
     91 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
     92   (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
     93 
     94 #define _bfd_riscv_elf_tls_type(abfd, h, symndx)		\
     95   (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type		\
     96      : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
     97 
     98 #define is_riscv_elf(bfd)				\
     99   (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
    100    && elf_tdata (bfd) != NULL				\
    101    && elf_object_id (bfd) == RISCV_ELF_DATA)
    102 
    103 #include "elf/common.h"
    104 #include "elf/internal.h"
    105 
    106 struct riscv_elf_link_hash_table
    107 {
    108   struct elf_link_hash_table elf;
    109 
    110   /* Short-cuts to get to dynamic linker sections.  */
    111   asection *sdyntdata;
    112 
    113   /* Small local sym to section mapping cache.  */
    114   struct sym_cache sym_cache;
    115 
    116   /* The max alignment of output sections.  */
    117   bfd_vma max_alignment;
    118 };
    119 
    120 
    121 /* Get the RISC-V ELF linker hash table from a link_info structure.  */
    122 #define riscv_elf_hash_table(p) \
    123   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    124   == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
    125 
    126 static void
    127 riscv_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
    128 			  arelent *cache_ptr,
    129 			  Elf_Internal_Rela *dst)
    130 {
    131   cache_ptr->howto = riscv_elf_rtype_to_howto (ELFNN_R_TYPE (dst->r_info));
    132 }
    133 
    134 static void
    135 riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
    136 {
    137   const struct elf_backend_data *bed;
    138   bfd_byte *loc;
    139 
    140   bed = get_elf_backend_data (abfd);
    141   loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
    142   bed->s->swap_reloca_out (abfd, rel, loc);
    143 }
    144 
    145 /* PLT/GOT stuff.  */
    146 
    147 #define PLT_HEADER_INSNS 8
    148 #define PLT_ENTRY_INSNS 4
    149 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
    150 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
    151 
    152 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
    153 
    154 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
    155 
    156 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
    157 
    158 static bfd_vma
    159 riscv_elf_got_plt_val (bfd_vma plt_index, struct bfd_link_info *info)
    160 {
    161   return sec_addr (riscv_elf_hash_table (info)->elf.sgotplt)
    162 	 + GOTPLT_HEADER_SIZE + (plt_index * GOT_ENTRY_SIZE);
    163 }
    164 
    165 #if ARCH_SIZE == 32
    166 # define MATCH_LREG MATCH_LW
    167 #else
    168 # define MATCH_LREG MATCH_LD
    169 #endif
    170 
    171 /* Generate a PLT header.  */
    172 
    173 static void
    174 riscv_make_plt_header (bfd_vma gotplt_addr, bfd_vma addr, uint32_t *entry)
    175 {
    176   bfd_vma gotplt_offset_high = RISCV_PCREL_HIGH_PART (gotplt_addr, addr);
    177   bfd_vma gotplt_offset_low = RISCV_PCREL_LOW_PART (gotplt_addr, addr);
    178 
    179   /* auipc  t2, %hi(.got.plt)
    180      sub    t1, t1, t3		     # shifted .got.plt offset + hdr size + 12
    181      l[w|d] t3, %lo(.got.plt)(t2)    # _dl_runtime_resolve
    182      addi   t1, t1, -(hdr size + 12) # shifted .got.plt offset
    183      addi   t0, t2, %lo(.got.plt)    # &.got.plt
    184      srli   t1, t1, log2(16/PTRSIZE) # .got.plt offset
    185      l[w|d] t0, PTRSIZE(t0)	     # link map
    186      jr	    t3 */
    187 
    188   entry[0] = RISCV_UTYPE (AUIPC, X_T2, gotplt_offset_high);
    189   entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T3);
    190   entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, gotplt_offset_low);
    191   entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, -(PLT_HEADER_SIZE + 12));
    192   entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, gotplt_offset_low);
    193   entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
    194   entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
    195   entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
    196 }
    197 
    198 /* Generate a PLT entry.  */
    199 
    200 static void
    201 riscv_make_plt_entry (bfd_vma got, bfd_vma addr, uint32_t *entry)
    202 {
    203   /* auipc  t3, %hi(.got.plt entry)
    204      l[w|d] t3, %lo(.got.plt entry)(t3)
    205      jalr   t1, t3
    206      nop */
    207 
    208   entry[0] = RISCV_UTYPE (AUIPC, X_T3, RISCV_PCREL_HIGH_PART (got, addr));
    209   entry[1] = RISCV_ITYPE (LREG,  X_T3, X_T3, RISCV_PCREL_LOW_PART (got, addr));
    210   entry[2] = RISCV_ITYPE (JALR, X_T1, X_T3, 0);
    211   entry[3] = RISCV_NOP;
    212 }
    213 
    214 /* Create an entry in an RISC-V ELF linker hash table.  */
    215 
    216 static struct bfd_hash_entry *
    217 link_hash_newfunc (struct bfd_hash_entry *entry,
    218 		   struct bfd_hash_table *table, const char *string)
    219 {
    220   /* Allocate the structure if it has not already been allocated by a
    221      subclass.  */
    222   if (entry == NULL)
    223     {
    224       entry =
    225 	bfd_hash_allocate (table,
    226 			   sizeof (struct riscv_elf_link_hash_entry));
    227       if (entry == NULL)
    228 	return entry;
    229     }
    230 
    231   /* Call the allocation method of the superclass.  */
    232   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
    233   if (entry != NULL)
    234     {
    235       struct riscv_elf_link_hash_entry *eh;
    236 
    237       eh = (struct riscv_elf_link_hash_entry *) entry;
    238       eh->dyn_relocs = NULL;
    239       eh->tls_type = GOT_UNKNOWN;
    240     }
    241 
    242   return entry;
    243 }
    244 
    245 /* Create a RISC-V ELF linker hash table.  */
    246 
    247 static struct bfd_link_hash_table *
    248 riscv_elf_link_hash_table_create (bfd *abfd)
    249 {
    250   struct riscv_elf_link_hash_table *ret;
    251   bfd_size_type amt = sizeof (struct riscv_elf_link_hash_table);
    252 
    253   ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
    254   if (ret == NULL)
    255     return NULL;
    256 
    257   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
    258 				      sizeof (struct riscv_elf_link_hash_entry),
    259 				      RISCV_ELF_DATA))
    260     {
    261       free (ret);
    262       return NULL;
    263     }
    264 
    265   ret->max_alignment = (bfd_vma) -1;
    266   return &ret->elf.root;
    267 }
    268 
    269 /* Create the .got section.  */
    270 
    271 static bfd_boolean
    272 riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
    273 {
    274   flagword flags;
    275   asection *s, *s_got;
    276   struct elf_link_hash_entry *h;
    277   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    278   struct elf_link_hash_table *htab = elf_hash_table (info);
    279 
    280   /* This function may be called more than once.  */
    281   if (htab->sgot != NULL)
    282     return TRUE;
    283 
    284   flags = bed->dynamic_sec_flags;
    285 
    286   s = bfd_make_section_anyway_with_flags (abfd,
    287 					  (bed->rela_plts_and_copies_p
    288 					   ? ".rela.got" : ".rel.got"),
    289 					  (bed->dynamic_sec_flags
    290 					   | SEC_READONLY));
    291   if (s == NULL
    292       || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    293     return FALSE;
    294   htab->srelgot = s;
    295 
    296   s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
    297   if (s == NULL
    298       || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
    299     return FALSE;
    300   htab->sgot = s;
    301 
    302   /* The first bit of the global offset table is the header.  */
    303   s->size += bed->got_header_size;
    304 
    305   if (bed->want_got_plt)
    306     {
    307       s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
    308       if (s == NULL
    309 	  || !bfd_set_section_alignment (abfd, s,
    310 					 bed->s->log_file_align))
    311 	return FALSE;
    312       htab->sgotplt = s;
    313 
    314       /* Reserve room for the header.  */
    315       s->size += GOTPLT_HEADER_SIZE;
    316     }
    317 
    318   if (bed->want_got_sym)
    319     {
    320       /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
    321 	 section.  We don't do this in the linker script because we don't want
    322 	 to define the symbol if we are not creating a global offset
    323 	 table.  */
    324       h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
    325 				       "_GLOBAL_OFFSET_TABLE_");
    326       elf_hash_table (info)->hgot = h;
    327       if (h == NULL)
    328 	return FALSE;
    329     }
    330 
    331   return TRUE;
    332 }
    333 
    334 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
    335    .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
    336    hash table.  */
    337 
    338 static bfd_boolean
    339 riscv_elf_create_dynamic_sections (bfd *dynobj,
    340 				   struct bfd_link_info *info)
    341 {
    342   struct riscv_elf_link_hash_table *htab;
    343 
    344   htab = riscv_elf_hash_table (info);
    345   BFD_ASSERT (htab != NULL);
    346 
    347   if (!riscv_elf_create_got_section (dynobj, info))
    348     return FALSE;
    349 
    350   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
    351     return FALSE;
    352 
    353   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
    354   if (!bfd_link_pic (info))
    355     {
    356       htab->sdyntdata =
    357 	bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
    358 					    SEC_ALLOC | SEC_THREAD_LOCAL);
    359     }
    360 
    361   if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
    362       || (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
    363     abort ();
    364 
    365   return TRUE;
    366 }
    367 
    368 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
    369 
    370 static void
    371 riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
    372 				struct elf_link_hash_entry *dir,
    373 				struct elf_link_hash_entry *ind)
    374 {
    375   struct riscv_elf_link_hash_entry *edir, *eind;
    376 
    377   edir = (struct riscv_elf_link_hash_entry *) dir;
    378   eind = (struct riscv_elf_link_hash_entry *) ind;
    379 
    380   if (eind->dyn_relocs != NULL)
    381     {
    382       if (edir->dyn_relocs != NULL)
    383 	{
    384 	  struct elf_dyn_relocs **pp;
    385 	  struct elf_dyn_relocs *p;
    386 
    387 	  /* Add reloc counts against the indirect sym to the direct sym
    388 	     list.  Merge any entries against the same section.  */
    389 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
    390 	    {
    391 	      struct elf_dyn_relocs *q;
    392 
    393 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
    394 		if (q->sec == p->sec)
    395 		  {
    396 		    q->pc_count += p->pc_count;
    397 		    q->count += p->count;
    398 		    *pp = p->next;
    399 		    break;
    400 		  }
    401 	      if (q == NULL)
    402 		pp = &p->next;
    403 	    }
    404 	  *pp = edir->dyn_relocs;
    405 	}
    406 
    407       edir->dyn_relocs = eind->dyn_relocs;
    408       eind->dyn_relocs = NULL;
    409     }
    410 
    411   if (ind->root.type == bfd_link_hash_indirect
    412       && dir->got.refcount <= 0)
    413     {
    414       edir->tls_type = eind->tls_type;
    415       eind->tls_type = GOT_UNKNOWN;
    416     }
    417   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
    418 }
    419 
    420 static bfd_boolean
    421 riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
    422 			   unsigned long symndx, char tls_type)
    423 {
    424   char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
    425 
    426   *new_tls_type |= tls_type;
    427   if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
    428     {
    429       (*_bfd_error_handler)
    430 	(_("%B: `%s' accessed both as normal and thread local symbol"),
    431 	 abfd, h ? h->root.root.string : "<local>");
    432       return FALSE;
    433     }
    434   return TRUE;
    435 }
    436 
    437 static bfd_boolean
    438 riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
    439 				struct elf_link_hash_entry *h, long symndx)
    440 {
    441   struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
    442   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    443 
    444   if (htab->elf.sgot == NULL)
    445     {
    446       if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
    447 	return FALSE;
    448     }
    449 
    450   if (h != NULL)
    451     {
    452       h->got.refcount += 1;
    453       return TRUE;
    454     }
    455 
    456   /* This is a global offset table entry for a local symbol.  */
    457   if (elf_local_got_refcounts (abfd) == NULL)
    458     {
    459       bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
    460       if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
    461 	return FALSE;
    462       _bfd_riscv_elf_local_got_tls_type (abfd)
    463 	= (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
    464     }
    465   elf_local_got_refcounts (abfd) [symndx] += 1;
    466 
    467   return TRUE;
    468 }
    469 
    470 static bfd_boolean
    471 bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
    472 {
    473   (*_bfd_error_handler)
    474     (_("%B: relocation %s against `%s' can not be used when making a shared "
    475        "object; recompile with -fPIC"),
    476       abfd, riscv_elf_rtype_to_howto (r_type)->name,
    477       h != NULL ? h->root.root.string : "a local symbol");
    478   bfd_set_error (bfd_error_bad_value);
    479   return FALSE;
    480 }
    481 /* Look through the relocs for a section during the first phase, and
    482    allocate space in the global offset table or procedure linkage
    483    table.  */
    484 
    485 static bfd_boolean
    486 riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
    487 			asection *sec, const Elf_Internal_Rela *relocs)
    488 {
    489   struct riscv_elf_link_hash_table *htab;
    490   Elf_Internal_Shdr *symtab_hdr;
    491   struct elf_link_hash_entry **sym_hashes;
    492   const Elf_Internal_Rela *rel;
    493   asection *sreloc = NULL;
    494 
    495   if (bfd_link_relocatable (info))
    496     return TRUE;
    497 
    498   htab = riscv_elf_hash_table (info);
    499   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
    500   sym_hashes = elf_sym_hashes (abfd);
    501 
    502   if (htab->elf.dynobj == NULL)
    503     htab->elf.dynobj = abfd;
    504 
    505   for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
    506     {
    507       unsigned int r_type;
    508       unsigned int r_symndx;
    509       struct elf_link_hash_entry *h;
    510 
    511       r_symndx = ELFNN_R_SYM (rel->r_info);
    512       r_type = ELFNN_R_TYPE (rel->r_info);
    513 
    514       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
    515 	{
    516 	  (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
    517 				 abfd, r_symndx);
    518 	  return FALSE;
    519 	}
    520 
    521       if (r_symndx < symtab_hdr->sh_info)
    522 	h = NULL;
    523       else
    524 	{
    525 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
    526 	  while (h->root.type == bfd_link_hash_indirect
    527 		 || h->root.type == bfd_link_hash_warning)
    528 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
    529 	}
    530 
    531       switch (r_type)
    532 	{
    533 	case R_RISCV_TLS_GD_HI20:
    534 	  if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
    535 	      || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
    536 	    return FALSE;
    537 	  break;
    538 
    539 	case R_RISCV_TLS_GOT_HI20:
    540 	  if (bfd_link_pic (info))
    541 	    info->flags |= DF_STATIC_TLS;
    542 	  if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
    543 	      || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
    544 	    return FALSE;
    545 	  break;
    546 
    547 	case R_RISCV_GOT_HI20:
    548 	  if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
    549 	      || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
    550 	    return FALSE;
    551 	  break;
    552 
    553 	case R_RISCV_CALL_PLT:
    554 	  /* This symbol requires a procedure linkage table entry.  We
    555 	     actually build the entry in adjust_dynamic_symbol,
    556 	     because this might be a case of linking PIC code without
    557 	     linking in any dynamic objects, in which case we don't
    558 	     need to generate a procedure linkage table after all.  */
    559 
    560 	  if (h != NULL)
    561 	    {
    562 	      h->needs_plt = 1;
    563 	      h->plt.refcount += 1;
    564 	    }
    565 	  break;
    566 
    567 	case R_RISCV_CALL:
    568 	case R_RISCV_JAL:
    569 	case R_RISCV_BRANCH:
    570 	case R_RISCV_RVC_BRANCH:
    571 	case R_RISCV_RVC_JUMP:
    572 	case R_RISCV_PCREL_HI20:
    573 	  /* In shared libraries, these relocs are known to bind locally.  */
    574 	  if (bfd_link_pic (info))
    575 	    break;
    576 	  goto static_reloc;
    577 
    578 	case R_RISCV_TPREL_HI20:
    579 	  if (!bfd_link_executable (info))
    580 	    return bad_static_reloc (abfd, r_type, h);
    581 	  if (h != NULL)
    582 	    riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
    583 	  goto static_reloc;
    584 
    585 	case R_RISCV_HI20:
    586 	  if (bfd_link_pic (info))
    587 	    return bad_static_reloc (abfd, r_type, h);
    588 	  /* Fall through.  */
    589 
    590 	case R_RISCV_COPY:
    591 	case R_RISCV_JUMP_SLOT:
    592 	case R_RISCV_RELATIVE:
    593 	case R_RISCV_64:
    594 	case R_RISCV_32:
    595 	  /* Fall through.  */
    596 
    597 	static_reloc:
    598 	  /* This reloc might not bind locally.  */
    599 	  if (h != NULL)
    600 	    h->non_got_ref = 1;
    601 
    602 	  if (h != NULL && !bfd_link_pic (info))
    603 	    {
    604 	      /* We may need a .plt entry if the function this reloc
    605 		 refers to is in a shared lib.  */
    606 	      h->plt.refcount += 1;
    607 	    }
    608 
    609 	  /* If we are creating a shared library, and this is a reloc
    610 	     against a global symbol, or a non PC relative reloc
    611 	     against a local symbol, then we need to copy the reloc
    612 	     into the shared library.  However, if we are linking with
    613 	     -Bsymbolic, we do not need to copy a reloc against a
    614 	     global symbol which is defined in an object we are
    615 	     including in the link (i.e., DEF_REGULAR is set).  At
    616 	     this point we have not seen all the input files, so it is
    617 	     possible that DEF_REGULAR is not set now but will be set
    618 	     later (it is never cleared).  In case of a weak definition,
    619 	     DEF_REGULAR may be cleared later by a strong definition in
    620 	     a shared library.  We account for that possibility below by
    621 	     storing information in the relocs_copied field of the hash
    622 	     table entry.  A similar situation occurs when creating
    623 	     shared libraries and symbol visibility changes render the
    624 	     symbol local.
    625 
    626 	     If on the other hand, we are creating an executable, we
    627 	     may need to keep relocations for symbols satisfied by a
    628 	     dynamic library if we manage to avoid copy relocs for the
    629 	     symbol.  */
    630 	  if ((bfd_link_pic (info)
    631 	       && (sec->flags & SEC_ALLOC) != 0
    632 	       && (! riscv_elf_rtype_to_howto (r_type)->pc_relative
    633 		   || (h != NULL
    634 		       && (! info->symbolic
    635 			   || h->root.type == bfd_link_hash_defweak
    636 			   || !h->def_regular))))
    637 	      || (!bfd_link_pic (info)
    638 		  && (sec->flags & SEC_ALLOC) != 0
    639 		  && h != NULL
    640 		  && (h->root.type == bfd_link_hash_defweak
    641 		      || !h->def_regular)))
    642 	    {
    643 	      struct elf_dyn_relocs *p;
    644 	      struct elf_dyn_relocs **head;
    645 
    646 	      /* When creating a shared object, we must copy these
    647 		 relocs into the output file.  We create a reloc
    648 		 section in dynobj and make room for the reloc.  */
    649 	      if (sreloc == NULL)
    650 		{
    651 		  sreloc = _bfd_elf_make_dynamic_reloc_section
    652 		    (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES,
    653 		    abfd, /*rela?*/ TRUE);
    654 
    655 		  if (sreloc == NULL)
    656 		    return FALSE;
    657 		}
    658 
    659 	      /* If this is a global symbol, we count the number of
    660 		 relocations we need for this symbol.  */
    661 	      if (h != NULL)
    662 		head = &((struct riscv_elf_link_hash_entry *) h)->dyn_relocs;
    663 	      else
    664 		{
    665 		  /* Track dynamic relocs needed for local syms too.
    666 		     We really need local syms available to do this
    667 		     easily.  Oh well.  */
    668 
    669 		  asection *s;
    670 		  void *vpp;
    671 		  Elf_Internal_Sym *isym;
    672 
    673 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
    674 						abfd, r_symndx);
    675 		  if (isym == NULL)
    676 		    return FALSE;
    677 
    678 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
    679 		  if (s == NULL)
    680 		    s = sec;
    681 
    682 		  vpp = &elf_section_data (s)->local_dynrel;
    683 		  head = (struct elf_dyn_relocs **) vpp;
    684 		}
    685 
    686 	      p = *head;
    687 	      if (p == NULL || p->sec != sec)
    688 		{
    689 		  bfd_size_type amt = sizeof *p;
    690 		  p = ((struct elf_dyn_relocs *)
    691 		       bfd_alloc (htab->elf.dynobj, amt));
    692 		  if (p == NULL)
    693 		    return FALSE;
    694 		  p->next = *head;
    695 		  *head = p;
    696 		  p->sec = sec;
    697 		  p->count = 0;
    698 		  p->pc_count = 0;
    699 		}
    700 
    701 	      p->count += 1;
    702 	      p->pc_count += riscv_elf_rtype_to_howto (r_type)->pc_relative;
    703 	    }
    704 
    705 	  break;
    706 
    707 	case R_RISCV_GNU_VTINHERIT:
    708 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
    709 	    return FALSE;
    710 	  break;
    711 
    712 	case R_RISCV_GNU_VTENTRY:
    713 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
    714 	    return FALSE;
    715 	  break;
    716 
    717 	default:
    718 	  break;
    719 	}
    720     }
    721 
    722   return TRUE;
    723 }
    724 
    725 static asection *
    726 riscv_elf_gc_mark_hook (asection *sec,
    727 			struct bfd_link_info *info,
    728 			Elf_Internal_Rela *rel,
    729 			struct elf_link_hash_entry *h,
    730 			Elf_Internal_Sym *sym)
    731 {
    732   if (h != NULL)
    733     switch (ELFNN_R_TYPE (rel->r_info))
    734       {
    735       case R_RISCV_GNU_VTINHERIT:
    736       case R_RISCV_GNU_VTENTRY:
    737 	return NULL;
    738       }
    739 
    740   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
    741 }
    742 
    743 /* Find dynamic relocs for H that apply to read-only sections.  */
    744 
    745 static asection *
    746 readonly_dynrelocs (struct elf_link_hash_entry *h)
    747 {
    748   struct elf_dyn_relocs *p;
    749 
    750   for (p = riscv_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
    751     {
    752       asection *s = p->sec->output_section;
    753 
    754       if (s != NULL && (s->flags & SEC_READONLY) != 0)
    755 	return p->sec;
    756     }
    757   return NULL;
    758 }
    759 
    760 /* Adjust a symbol defined by a dynamic object and referenced by a
    761    regular object.  The current definition is in some section of the
    762    dynamic object, but we're not including those sections.  We have to
    763    change the definition to something the rest of the link can
    764    understand.  */
    765 
    766 static bfd_boolean
    767 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
    768 				 struct elf_link_hash_entry *h)
    769 {
    770   struct riscv_elf_link_hash_table *htab;
    771   struct riscv_elf_link_hash_entry * eh;
    772   bfd *dynobj;
    773   asection *s, *srel;
    774 
    775   htab = riscv_elf_hash_table (info);
    776   BFD_ASSERT (htab != NULL);
    777 
    778   dynobj = htab->elf.dynobj;
    779 
    780   /* Make sure we know what is going on here.  */
    781   BFD_ASSERT (dynobj != NULL
    782 	      && (h->needs_plt
    783 		  || h->type == STT_GNU_IFUNC
    784 		  || h->is_weakalias
    785 		  || (h->def_dynamic
    786 		      && h->ref_regular
    787 		      && !h->def_regular)));
    788 
    789   /* If this is a function, put it in the procedure linkage table.  We
    790      will fill in the contents of the procedure linkage table later
    791      (although we could actually do it here).  */
    792   if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
    793     {
    794       if (h->plt.refcount <= 0
    795 	  || SYMBOL_CALLS_LOCAL (info, h)
    796 	  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
    797 	      && h->root.type == bfd_link_hash_undefweak))
    798 	{
    799 	  /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
    800 	     input file, but the symbol was never referred to by a dynamic
    801 	     object, or if all references were garbage collected.  In such
    802 	     a case, we don't actually need to build a PLT entry.  */
    803 	  h->plt.offset = (bfd_vma) -1;
    804 	  h->needs_plt = 0;
    805 	}
    806 
    807       return TRUE;
    808     }
    809   else
    810     h->plt.offset = (bfd_vma) -1;
    811 
    812   /* If this is a weak symbol, and there is a real definition, the
    813      processor independent code will have arranged for us to see the
    814      real definition first, and we can just use the same value.  */
    815   if (h->is_weakalias)
    816     {
    817       struct elf_link_hash_entry *def = weakdef (h);
    818       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
    819       h->root.u.def.section = def->root.u.def.section;
    820       h->root.u.def.value = def->root.u.def.value;
    821       return TRUE;
    822     }
    823 
    824   /* This is a reference to a symbol defined by a dynamic object which
    825      is not a function.  */
    826 
    827   /* If we are creating a shared library, we must presume that the
    828      only references to the symbol are via the global offset table.
    829      For such cases we need not do anything here; the relocations will
    830      be handled correctly by relocate_section.  */
    831   if (bfd_link_pic (info))
    832     return TRUE;
    833 
    834   /* If there are no references to this symbol that do not use the
    835      GOT, we don't need to generate a copy reloc.  */
    836   if (!h->non_got_ref)
    837     return TRUE;
    838 
    839   /* If -z nocopyreloc was given, we won't generate them either.  */
    840   if (info->nocopyreloc)
    841     {
    842       h->non_got_ref = 0;
    843       return TRUE;
    844     }
    845 
    846   /* If we don't find any dynamic relocs in read-only sections, then
    847      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
    848   if (!readonly_dynrelocs (h))
    849     {
    850       h->non_got_ref = 0;
    851       return TRUE;
    852     }
    853 
    854   /* We must allocate the symbol in our .dynbss section, which will
    855      become part of the .bss section of the executable.  There will be
    856      an entry for this symbol in the .dynsym section.  The dynamic
    857      object will contain position independent code, so all references
    858      from the dynamic object to this symbol will go through the global
    859      offset table.  The dynamic linker will use the .dynsym entry to
    860      determine the address it must put in the global offset table, so
    861      both the dynamic object and the regular object will refer to the
    862      same memory location for the variable.  */
    863 
    864   /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
    865      to copy the initial value out of the dynamic object and into the
    866      runtime process image.  We need to remember the offset into the
    867      .rel.bss section we are going to use.  */
    868   eh = (struct riscv_elf_link_hash_entry *) h;
    869   if (eh->tls_type & ~GOT_NORMAL)
    870     {
    871       s = htab->sdyntdata;
    872       srel = htab->elf.srelbss;
    873     }
    874   else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
    875     {
    876       s = htab->elf.sdynrelro;
    877       srel = htab->elf.sreldynrelro;
    878     }
    879   else
    880     {
    881       s = htab->elf.sdynbss;
    882       srel = htab->elf.srelbss;
    883     }
    884   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
    885     {
    886       srel->size += sizeof (ElfNN_External_Rela);
    887       h->needs_copy = 1;
    888     }
    889 
    890   return _bfd_elf_adjust_dynamic_copy (info, h, s);
    891 }
    892 
    893 /* Allocate space in .plt, .got and associated reloc sections for
    894    dynamic relocs.  */
    895 
    896 static bfd_boolean
    897 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
    898 {
    899   struct bfd_link_info *info;
    900   struct riscv_elf_link_hash_table *htab;
    901   struct riscv_elf_link_hash_entry *eh;
    902   struct elf_dyn_relocs *p;
    903 
    904   if (h->root.type == bfd_link_hash_indirect)
    905     return TRUE;
    906 
    907   info = (struct bfd_link_info *) inf;
    908   htab = riscv_elf_hash_table (info);
    909   BFD_ASSERT (htab != NULL);
    910 
    911   if (htab->elf.dynamic_sections_created
    912       && h->plt.refcount > 0)
    913     {
    914       /* Make sure this symbol is output as a dynamic symbol.
    915 	 Undefined weak syms won't yet be marked as dynamic.  */
    916       if (h->dynindx == -1
    917 	  && !h->forced_local)
    918 	{
    919 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
    920 	    return FALSE;
    921 	}
    922 
    923       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
    924 	{
    925 	  asection *s = htab->elf.splt;
    926 
    927 	  if (s->size == 0)
    928 	    s->size = PLT_HEADER_SIZE;
    929 
    930 	  h->plt.offset = s->size;
    931 
    932 	  /* Make room for this entry.  */
    933 	  s->size += PLT_ENTRY_SIZE;
    934 
    935 	  /* We also need to make an entry in the .got.plt section.  */
    936 	  htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
    937 
    938 	  /* We also need to make an entry in the .rela.plt section.  */
    939 	  htab->elf.srelplt->size += sizeof (ElfNN_External_Rela);
    940 
    941 	  /* If this symbol is not defined in a regular file, and we are
    942 	     not generating a shared library, then set the symbol to this
    943 	     location in the .plt.  This is required to make function
    944 	     pointers compare as equal between the normal executable and
    945 	     the shared library.  */
    946 	  if (! bfd_link_pic (info)
    947 	      && !h->def_regular)
    948 	    {
    949 	      h->root.u.def.section = s;
    950 	      h->root.u.def.value = h->plt.offset;
    951 	    }
    952 	}
    953       else
    954 	{
    955 	  h->plt.offset = (bfd_vma) -1;
    956 	  h->needs_plt = 0;
    957 	}
    958     }
    959   else
    960     {
    961       h->plt.offset = (bfd_vma) -1;
    962       h->needs_plt = 0;
    963     }
    964 
    965   if (h->got.refcount > 0)
    966     {
    967       asection *s;
    968       bfd_boolean dyn;
    969       int tls_type = riscv_elf_hash_entry (h)->tls_type;
    970 
    971       /* Make sure this symbol is output as a dynamic symbol.
    972 	 Undefined weak syms won't yet be marked as dynamic.  */
    973       if (h->dynindx == -1
    974 	  && !h->forced_local)
    975 	{
    976 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
    977 	    return FALSE;
    978 	}
    979 
    980       s = htab->elf.sgot;
    981       h->got.offset = s->size;
    982       dyn = htab->elf.dynamic_sections_created;
    983       if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
    984 	{
    985 	  /* TLS_GD needs two dynamic relocs and two GOT slots.  */
    986 	  if (tls_type & GOT_TLS_GD)
    987 	    {
    988 	      s->size += 2 * RISCV_ELF_WORD_BYTES;
    989 	      htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela);
    990 	    }
    991 
    992 	  /* TLS_IE needs one dynamic reloc and one GOT slot.  */
    993 	  if (tls_type & GOT_TLS_IE)
    994 	    {
    995 	      s->size += RISCV_ELF_WORD_BYTES;
    996 	      htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
    997 	    }
    998 	}
    999       else
   1000 	{
   1001 	  s->size += RISCV_ELF_WORD_BYTES;
   1002 	  if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
   1003 	    htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
   1004 	}
   1005     }
   1006   else
   1007     h->got.offset = (bfd_vma) -1;
   1008 
   1009   eh = (struct riscv_elf_link_hash_entry *) h;
   1010   if (eh->dyn_relocs == NULL)
   1011     return TRUE;
   1012 
   1013   /* In the shared -Bsymbolic case, discard space allocated for
   1014      dynamic pc-relative relocs against symbols which turn out to be
   1015      defined in regular objects.  For the normal shared case, discard
   1016      space for pc-relative relocs that have become local due to symbol
   1017      visibility changes.  */
   1018 
   1019   if (bfd_link_pic (info))
   1020     {
   1021       if (SYMBOL_CALLS_LOCAL (info, h))
   1022 	{
   1023 	  struct elf_dyn_relocs **pp;
   1024 
   1025 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   1026 	    {
   1027 	      p->count -= p->pc_count;
   1028 	      p->pc_count = 0;
   1029 	      if (p->count == 0)
   1030 		*pp = p->next;
   1031 	      else
   1032 		pp = &p->next;
   1033 	    }
   1034 	}
   1035 
   1036       /* Also discard relocs on undefined weak syms with non-default
   1037 	 visibility.  */
   1038       if (eh->dyn_relocs != NULL
   1039 	  && h->root.type == bfd_link_hash_undefweak)
   1040 	{
   1041 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   1042 	    eh->dyn_relocs = NULL;
   1043 
   1044 	  /* Make sure undefined weak symbols are output as a dynamic
   1045 	     symbol in PIEs.  */
   1046 	  else if (h->dynindx == -1
   1047 		   && !h->forced_local)
   1048 	    {
   1049 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   1050 		return FALSE;
   1051 	    }
   1052 	}
   1053     }
   1054   else
   1055     {
   1056       /* For the non-shared case, discard space for relocs against
   1057 	 symbols which turn out to need copy relocs or are not
   1058 	 dynamic.  */
   1059 
   1060       if (!h->non_got_ref
   1061 	  && ((h->def_dynamic
   1062 	       && !h->def_regular)
   1063 	      || (htab->elf.dynamic_sections_created
   1064 		  && (h->root.type == bfd_link_hash_undefweak
   1065 		      || h->root.type == bfd_link_hash_undefined))))
   1066 	{
   1067 	  /* Make sure this symbol is output as a dynamic symbol.
   1068 	     Undefined weak syms won't yet be marked as dynamic.  */
   1069 	  if (h->dynindx == -1
   1070 	      && !h->forced_local)
   1071 	    {
   1072 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   1073 		return FALSE;
   1074 	    }
   1075 
   1076 	  /* If that succeeded, we know we'll be keeping all the
   1077 	     relocs.  */
   1078 	  if (h->dynindx != -1)
   1079 	    goto keep;
   1080 	}
   1081 
   1082       eh->dyn_relocs = NULL;
   1083 
   1084     keep: ;
   1085     }
   1086 
   1087   /* Finally, allocate space.  */
   1088   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   1089     {
   1090       asection *sreloc = elf_section_data (p->sec)->sreloc;
   1091       sreloc->size += p->count * sizeof (ElfNN_External_Rela);
   1092     }
   1093 
   1094   return TRUE;
   1095 }
   1096 
   1097 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
   1098    read-only sections.  */
   1099 
   1100 static bfd_boolean
   1101 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
   1102 {
   1103   asection *sec;
   1104 
   1105   if (h->root.type == bfd_link_hash_indirect)
   1106     return TRUE;
   1107 
   1108   sec = readonly_dynrelocs (h);
   1109   if (sec != NULL)
   1110     {
   1111       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
   1112 
   1113       info->flags |= DF_TEXTREL;
   1114       info->callbacks->minfo
   1115 	(_("%B: dynamic relocation against `%T' in read-only section `%A'\n"),
   1116 	 sec->owner, h->root.root.string, sec);
   1117 
   1118       /* Not an error, just cut short the traversal.  */
   1119       return FALSE;
   1120     }
   1121   return TRUE;
   1122 }
   1123 
   1124 static bfd_boolean
   1125 riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
   1126 {
   1127   struct riscv_elf_link_hash_table *htab;
   1128   bfd *dynobj;
   1129   asection *s;
   1130   bfd *ibfd;
   1131 
   1132   htab = riscv_elf_hash_table (info);
   1133   BFD_ASSERT (htab != NULL);
   1134   dynobj = htab->elf.dynobj;
   1135   BFD_ASSERT (dynobj != NULL);
   1136 
   1137   if (elf_hash_table (info)->dynamic_sections_created)
   1138     {
   1139       /* Set the contents of the .interp section to the interpreter.  */
   1140       if (bfd_link_executable (info) && !info->nointerp)
   1141 	{
   1142 	  s = bfd_get_linker_section (dynobj, ".interp");
   1143 	  BFD_ASSERT (s != NULL);
   1144 	  s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1;
   1145 	  s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER;
   1146 	}
   1147     }
   1148 
   1149   /* Set up .got offsets for local syms, and space for local dynamic
   1150      relocs.  */
   1151   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   1152     {
   1153       bfd_signed_vma *local_got;
   1154       bfd_signed_vma *end_local_got;
   1155       char *local_tls_type;
   1156       bfd_size_type locsymcount;
   1157       Elf_Internal_Shdr *symtab_hdr;
   1158       asection *srel;
   1159 
   1160       if (! is_riscv_elf (ibfd))
   1161 	continue;
   1162 
   1163       for (s = ibfd->sections; s != NULL; s = s->next)
   1164 	{
   1165 	  struct elf_dyn_relocs *p;
   1166 
   1167 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   1168 	    {
   1169 	      if (!bfd_is_abs_section (p->sec)
   1170 		  && bfd_is_abs_section (p->sec->output_section))
   1171 		{
   1172 		  /* Input section has been discarded, either because
   1173 		     it is a copy of a linkonce section or due to
   1174 		     linker script /DISCARD/, so we'll be discarding
   1175 		     the relocs too.  */
   1176 		}
   1177 	      else if (p->count != 0)
   1178 		{
   1179 		  srel = elf_section_data (p->sec)->sreloc;
   1180 		  srel->size += p->count * sizeof (ElfNN_External_Rela);
   1181 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   1182 		    info->flags |= DF_TEXTREL;
   1183 		}
   1184 	    }
   1185 	}
   1186 
   1187       local_got = elf_local_got_refcounts (ibfd);
   1188       if (!local_got)
   1189 	continue;
   1190 
   1191       symtab_hdr = &elf_symtab_hdr (ibfd);
   1192       locsymcount = symtab_hdr->sh_info;
   1193       end_local_got = local_got + locsymcount;
   1194       local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd);
   1195       s = htab->elf.sgot;
   1196       srel = htab->elf.srelgot;
   1197       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
   1198 	{
   1199 	  if (*local_got > 0)
   1200 	    {
   1201 	      *local_got = s->size;
   1202 	      s->size += RISCV_ELF_WORD_BYTES;
   1203 	      if (*local_tls_type & GOT_TLS_GD)
   1204 		s->size += RISCV_ELF_WORD_BYTES;
   1205 	      if (bfd_link_pic (info)
   1206 		  || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
   1207 		srel->size += sizeof (ElfNN_External_Rela);
   1208 	    }
   1209 	  else
   1210 	    *local_got = (bfd_vma) -1;
   1211 	}
   1212     }
   1213 
   1214   /* Allocate global sym .plt and .got entries, and space for global
   1215      sym dynamic relocs.  */
   1216   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   1217 
   1218   if (htab->elf.sgotplt)
   1219     {
   1220       struct elf_link_hash_entry *got;
   1221       got = elf_link_hash_lookup (elf_hash_table (info),
   1222 				  "_GLOBAL_OFFSET_TABLE_",
   1223 				  FALSE, FALSE, FALSE);
   1224 
   1225       /* Don't allocate .got.plt section if there are no GOT nor PLT
   1226 	 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
   1227       if ((got == NULL
   1228 	   || !got->ref_regular_nonweak)
   1229 	  && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE)
   1230 	  && (htab->elf.splt == NULL
   1231 	      || htab->elf.splt->size == 0)
   1232 	  && (htab->elf.sgot == NULL
   1233 	      || (htab->elf.sgot->size
   1234 		  == get_elf_backend_data (output_bfd)->got_header_size)))
   1235 	htab->elf.sgotplt->size = 0;
   1236     }
   1237 
   1238   /* The check_relocs and adjust_dynamic_symbol entry points have
   1239      determined the sizes of the various dynamic sections.  Allocate
   1240      memory for them.  */
   1241   for (s = dynobj->sections; s != NULL; s = s->next)
   1242     {
   1243       if ((s->flags & SEC_LINKER_CREATED) == 0)
   1244 	continue;
   1245 
   1246       if (s == htab->elf.splt
   1247 	  || s == htab->elf.sgot
   1248 	  || s == htab->elf.sgotplt
   1249 	  || s == htab->elf.sdynbss
   1250 	  || s == htab->elf.sdynrelro)
   1251 	{
   1252 	  /* Strip this section if we don't need it; see the
   1253 	     comment below.  */
   1254 	}
   1255       else if (strncmp (s->name, ".rela", 5) == 0)
   1256 	{
   1257 	  if (s->size != 0)
   1258 	    {
   1259 	      /* We use the reloc_count field as a counter if we need
   1260 		 to copy relocs into the output file.  */
   1261 	      s->reloc_count = 0;
   1262 	    }
   1263 	}
   1264       else
   1265 	{
   1266 	  /* It's not one of our sections.  */
   1267 	  continue;
   1268 	}
   1269 
   1270       if (s->size == 0)
   1271 	{
   1272 	  /* If we don't need this section, strip it from the
   1273 	     output file.  This is mostly to handle .rela.bss and
   1274 	     .rela.plt.  We must create both sections in
   1275 	     create_dynamic_sections, because they must be created
   1276 	     before the linker maps input sections to output
   1277 	     sections.  The linker does that before
   1278 	     adjust_dynamic_symbol is called, and it is that
   1279 	     function which decides whether anything needs to go
   1280 	     into these sections.  */
   1281 	  s->flags |= SEC_EXCLUDE;
   1282 	  continue;
   1283 	}
   1284 
   1285       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   1286 	continue;
   1287 
   1288       /* Allocate memory for the section contents.  Zero the memory
   1289 	 for the benefit of .rela.plt, which has 4 unused entries
   1290 	 at the beginning, and we don't want garbage.  */
   1291       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
   1292       if (s->contents == NULL)
   1293 	return FALSE;
   1294     }
   1295 
   1296   if (elf_hash_table (info)->dynamic_sections_created)
   1297     {
   1298       /* Add some entries to the .dynamic section.  We fill in the
   1299 	 values later, in riscv_elf_finish_dynamic_sections, but we
   1300 	 must add the entries now so that we get the correct size for
   1301 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   1302 	 dynamic linker and used by the debugger.  */
   1303 #define add_dynamic_entry(TAG, VAL) \
   1304   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   1305 
   1306       if (bfd_link_executable (info))
   1307 	{
   1308 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   1309 	    return FALSE;
   1310 	}
   1311 
   1312       if (htab->elf.srelplt->size != 0)
   1313 	{
   1314 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   1315 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   1316 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   1317 	      || !add_dynamic_entry (DT_JMPREL, 0))
   1318 	    return FALSE;
   1319 	}
   1320 
   1321       if (!add_dynamic_entry (DT_RELA, 0)
   1322 	  || !add_dynamic_entry (DT_RELASZ, 0)
   1323 	  || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
   1324 	return FALSE;
   1325 
   1326       /* If any dynamic relocs apply to a read-only section,
   1327 	 then we need a DT_TEXTREL entry.  */
   1328       if ((info->flags & DF_TEXTREL) == 0)
   1329 	elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
   1330 
   1331       if (info->flags & DF_TEXTREL)
   1332 	{
   1333 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
   1334 	    return FALSE;
   1335 	}
   1336     }
   1337 #undef add_dynamic_entry
   1338 
   1339   return TRUE;
   1340 }
   1341 
   1342 #define TP_OFFSET 0
   1343 #define DTP_OFFSET 0x800
   1344 
   1345 /* Return the relocation value for a TLS dtp-relative reloc.  */
   1346 
   1347 static bfd_vma
   1348 dtpoff (struct bfd_link_info *info, bfd_vma address)
   1349 {
   1350   /* If tls_sec is NULL, we should have signalled an error already.  */
   1351   if (elf_hash_table (info)->tls_sec == NULL)
   1352     return 0;
   1353   return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
   1354 }
   1355 
   1356 /* Return the relocation value for a static TLS tp-relative relocation.  */
   1357 
   1358 static bfd_vma
   1359 tpoff (struct bfd_link_info *info, bfd_vma address)
   1360 {
   1361   /* If tls_sec is NULL, we should have signalled an error already.  */
   1362   if (elf_hash_table (info)->tls_sec == NULL)
   1363     return 0;
   1364   return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
   1365 }
   1366 
   1367 /* Return the global pointer's value, or 0 if it is not in use.  */
   1368 
   1369 static bfd_vma
   1370 riscv_global_pointer_value (struct bfd_link_info *info)
   1371 {
   1372   struct bfd_link_hash_entry *h;
   1373 
   1374   h = bfd_link_hash_lookup (info->hash, RISCV_GP_SYMBOL, FALSE, FALSE, TRUE);
   1375   if (h == NULL || h->type != bfd_link_hash_defined)
   1376     return 0;
   1377 
   1378   return h->u.def.value + sec_addr (h->u.def.section);
   1379 }
   1380 
   1381 /* Emplace a static relocation.  */
   1382 
   1383 static bfd_reloc_status_type
   1384 perform_relocation (const reloc_howto_type *howto,
   1385 		    const Elf_Internal_Rela *rel,
   1386 		    bfd_vma value,
   1387 		    asection *input_section,
   1388 		    bfd *input_bfd,
   1389 		    bfd_byte *contents)
   1390 {
   1391   if (howto->pc_relative)
   1392     value -= sec_addr (input_section) + rel->r_offset;
   1393   value += rel->r_addend;
   1394 
   1395   switch (ELFNN_R_TYPE (rel->r_info))
   1396     {
   1397     case R_RISCV_HI20:
   1398     case R_RISCV_TPREL_HI20:
   1399     case R_RISCV_PCREL_HI20:
   1400     case R_RISCV_GOT_HI20:
   1401     case R_RISCV_TLS_GOT_HI20:
   1402     case R_RISCV_TLS_GD_HI20:
   1403       if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
   1404 	return bfd_reloc_overflow;
   1405       value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
   1406       break;
   1407 
   1408     case R_RISCV_LO12_I:
   1409     case R_RISCV_GPREL_I:
   1410     case R_RISCV_TPREL_LO12_I:
   1411     case R_RISCV_TPREL_I:
   1412     case R_RISCV_PCREL_LO12_I:
   1413       value = ENCODE_ITYPE_IMM (value);
   1414       break;
   1415 
   1416     case R_RISCV_LO12_S:
   1417     case R_RISCV_GPREL_S:
   1418     case R_RISCV_TPREL_LO12_S:
   1419     case R_RISCV_TPREL_S:
   1420     case R_RISCV_PCREL_LO12_S:
   1421       value = ENCODE_STYPE_IMM (value);
   1422       break;
   1423 
   1424     case R_RISCV_CALL:
   1425     case R_RISCV_CALL_PLT:
   1426       if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
   1427 	return bfd_reloc_overflow;
   1428       value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
   1429 	      | (ENCODE_ITYPE_IMM (value) << 32);
   1430       break;
   1431 
   1432     case R_RISCV_JAL:
   1433       if (!VALID_UJTYPE_IMM (value))
   1434 	return bfd_reloc_overflow;
   1435       value = ENCODE_UJTYPE_IMM (value);
   1436       break;
   1437 
   1438     case R_RISCV_BRANCH:
   1439       if (!VALID_SBTYPE_IMM (value))
   1440 	return bfd_reloc_overflow;
   1441       value = ENCODE_SBTYPE_IMM (value);
   1442       break;
   1443 
   1444     case R_RISCV_RVC_BRANCH:
   1445       if (!VALID_RVC_B_IMM (value))
   1446 	return bfd_reloc_overflow;
   1447       value = ENCODE_RVC_B_IMM (value);
   1448       break;
   1449 
   1450     case R_RISCV_RVC_JUMP:
   1451       if (!VALID_RVC_J_IMM (value))
   1452 	return bfd_reloc_overflow;
   1453       value = ENCODE_RVC_J_IMM (value);
   1454       break;
   1455 
   1456     case R_RISCV_RVC_LUI:
   1457       if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value)))
   1458 	return bfd_reloc_overflow;
   1459       value = ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value));
   1460       break;
   1461 
   1462     case R_RISCV_32:
   1463     case R_RISCV_64:
   1464     case R_RISCV_ADD8:
   1465     case R_RISCV_ADD16:
   1466     case R_RISCV_ADD32:
   1467     case R_RISCV_ADD64:
   1468     case R_RISCV_SUB6:
   1469     case R_RISCV_SUB8:
   1470     case R_RISCV_SUB16:
   1471     case R_RISCV_SUB32:
   1472     case R_RISCV_SUB64:
   1473     case R_RISCV_SET6:
   1474     case R_RISCV_SET8:
   1475     case R_RISCV_SET16:
   1476     case R_RISCV_SET32:
   1477     case R_RISCV_32_PCREL:
   1478     case R_RISCV_TLS_DTPREL32:
   1479     case R_RISCV_TLS_DTPREL64:
   1480       break;
   1481 
   1482     case R_RISCV_DELETE:
   1483       return bfd_reloc_ok;
   1484 
   1485     default:
   1486       return bfd_reloc_notsupported;
   1487     }
   1488 
   1489   bfd_vma word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
   1490   word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
   1491   bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
   1492 
   1493   return bfd_reloc_ok;
   1494 }
   1495 
   1496 /* Remember all PC-relative high-part relocs we've encountered to help us
   1497    later resolve the corresponding low-part relocs.  */
   1498 
   1499 typedef struct
   1500 {
   1501   bfd_vma address;
   1502   bfd_vma value;
   1503 } riscv_pcrel_hi_reloc;
   1504 
   1505 typedef struct riscv_pcrel_lo_reloc
   1506 {
   1507   asection *			 input_section;
   1508   struct bfd_link_info *	 info;
   1509   reloc_howto_type *		 howto;
   1510   const Elf_Internal_Rela *	 reloc;
   1511   bfd_vma			 addr;
   1512   const char *			 name;
   1513   bfd_byte *			 contents;
   1514   struct riscv_pcrel_lo_reloc *	 next;
   1515 } riscv_pcrel_lo_reloc;
   1516 
   1517 typedef struct
   1518 {
   1519   htab_t hi_relocs;
   1520   riscv_pcrel_lo_reloc *lo_relocs;
   1521 } riscv_pcrel_relocs;
   1522 
   1523 static hashval_t
   1524 riscv_pcrel_reloc_hash (const void *entry)
   1525 {
   1526   const riscv_pcrel_hi_reloc *e = entry;
   1527   return (hashval_t)(e->address >> 2);
   1528 }
   1529 
   1530 static bfd_boolean
   1531 riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
   1532 {
   1533   const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
   1534   return e1->address == e2->address;
   1535 }
   1536 
   1537 static bfd_boolean
   1538 riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
   1539 {
   1540 
   1541   p->lo_relocs = NULL;
   1542   p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
   1543 			      riscv_pcrel_reloc_eq, free);
   1544   return p->hi_relocs != NULL;
   1545 }
   1546 
   1547 static void
   1548 riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
   1549 {
   1550   riscv_pcrel_lo_reloc *cur = p->lo_relocs;
   1551 
   1552   while (cur != NULL)
   1553     {
   1554       riscv_pcrel_lo_reloc *next = cur->next;
   1555       free (cur);
   1556       cur = next;
   1557     }
   1558 
   1559   htab_delete (p->hi_relocs);
   1560 }
   1561 
   1562 static bfd_boolean
   1563 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela *rel,
   1564 			   struct bfd_link_info *info,
   1565 			   bfd_vma pc,
   1566 			   bfd_vma addr,
   1567 			   bfd_byte *contents,
   1568 			   const reloc_howto_type *howto,
   1569 			   bfd *input_bfd)
   1570 {
   1571   /* We may need to reference low addreses in PC-relative modes even when the
   1572    * PC is far away from these addresses.  For example, undefweak references
   1573    * need to produce the address 0 when linked.  As 0 is far from the arbitrary
   1574    * addresses that we can link PC-relative programs at, the linker can't
   1575    * actually relocate references to those symbols.  In order to allow these
   1576    * programs to work we simply convert the PC-relative auipc sequences to
   1577    * 0-relative lui sequences.  */
   1578   if (bfd_link_pic (info))
   1579     return FALSE;
   1580 
   1581   /* If it's possible to reference the symbol using auipc we do so, as that's
   1582    * more in the spirit of the PC-relative relocations we're processing.  */
   1583   bfd_vma offset = addr - pc;
   1584   if (ARCH_SIZE == 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset)))
   1585     return FALSE;
   1586 
   1587   /* If it's impossible to reference this with a LUI-based offset then don't
   1588    * bother to convert it at all so users still see the PC-relative relocation
   1589    * in the truncation message.  */
   1590   if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr)))
   1591     return FALSE;
   1592 
   1593   rel->r_info = ELFNN_R_INFO(addr, R_RISCV_HI20);
   1594 
   1595   bfd_vma insn = bfd_get(howto->bitsize, input_bfd, contents + rel->r_offset);
   1596   insn = (insn & ~MASK_AUIPC) | MATCH_LUI;
   1597   bfd_put(howto->bitsize, input_bfd, insn, contents + rel->r_offset);
   1598   return TRUE;
   1599 }
   1600 
   1601 static bfd_boolean
   1602 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p, bfd_vma addr,
   1603 			     bfd_vma value, bfd_boolean absolute)
   1604 {
   1605   bfd_vma offset = absolute ? value : value - addr;
   1606   riscv_pcrel_hi_reloc entry = {addr, offset};
   1607   riscv_pcrel_hi_reloc **slot =
   1608     (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
   1609 
   1610   BFD_ASSERT (*slot == NULL);
   1611   *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
   1612   if (*slot == NULL)
   1613     return FALSE;
   1614   **slot = entry;
   1615   return TRUE;
   1616 }
   1617 
   1618 static bfd_boolean
   1619 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
   1620 			     asection *input_section,
   1621 			     struct bfd_link_info *info,
   1622 			     reloc_howto_type *howto,
   1623 			     const Elf_Internal_Rela *reloc,
   1624 			     bfd_vma addr,
   1625 			     const char *name,
   1626 			     bfd_byte *contents)
   1627 {
   1628   riscv_pcrel_lo_reloc *entry;
   1629   entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
   1630   if (entry == NULL)
   1631     return FALSE;
   1632   *entry = (riscv_pcrel_lo_reloc) {input_section, info, howto, reloc, addr,
   1633 				   name, contents, p->lo_relocs};
   1634   p->lo_relocs = entry;
   1635   return TRUE;
   1636 }
   1637 
   1638 static bfd_boolean
   1639 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
   1640 {
   1641   riscv_pcrel_lo_reloc *r;
   1642 
   1643   for (r = p->lo_relocs; r != NULL; r = r->next)
   1644     {
   1645       bfd *input_bfd = r->input_section->owner;
   1646 
   1647       riscv_pcrel_hi_reloc search = {r->addr, 0};
   1648       riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
   1649       if (entry == NULL)
   1650 	{
   1651 	  ((*r->info->callbacks->reloc_overflow)
   1652 	   (r->info, NULL, r->name, r->howto->name, (bfd_vma) 0,
   1653 	    input_bfd, r->input_section, r->reloc->r_offset));
   1654 	  return TRUE;
   1655 	}
   1656 
   1657       perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
   1658 			  input_bfd, r->contents);
   1659     }
   1660 
   1661   return TRUE;
   1662 }
   1663 
   1664 /* Relocate a RISC-V ELF section.
   1665 
   1666    The RELOCATE_SECTION function is called by the new ELF backend linker
   1667    to handle the relocations for a section.
   1668 
   1669    The relocs are always passed as Rela structures.
   1670 
   1671    This function is responsible for adjusting the section contents as
   1672    necessary, and (if generating a relocatable output file) adjusting
   1673    the reloc addend as necessary.
   1674 
   1675    This function does not have to worry about setting the reloc
   1676    address or the reloc symbol index.
   1677 
   1678    LOCAL_SYMS is a pointer to the swapped in local symbols.
   1679 
   1680    LOCAL_SECTIONS is an array giving the section in the input file
   1681    corresponding to the st_shndx field of each local symbol.
   1682 
   1683    The global hash table entry for the global symbols can be found
   1684    via elf_sym_hashes (input_bfd).
   1685 
   1686    When generating relocatable output, this function must handle
   1687    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   1688    going to be the section symbol corresponding to the output
   1689    section, which means that the addend must be adjusted
   1690    accordingly.  */
   1691 
   1692 static bfd_boolean
   1693 riscv_elf_relocate_section (bfd *output_bfd,
   1694 			    struct bfd_link_info *info,
   1695 			    bfd *input_bfd,
   1696 			    asection *input_section,
   1697 			    bfd_byte *contents,
   1698 			    Elf_Internal_Rela *relocs,
   1699 			    Elf_Internal_Sym *local_syms,
   1700 			    asection **local_sections)
   1701 {
   1702   Elf_Internal_Rela *rel;
   1703   Elf_Internal_Rela *relend;
   1704   riscv_pcrel_relocs pcrel_relocs;
   1705   bfd_boolean ret = FALSE;
   1706   asection *sreloc = elf_section_data (input_section)->sreloc;
   1707   struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
   1708   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
   1709   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
   1710   bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
   1711   bfd_boolean absolute;
   1712 
   1713   if (!riscv_init_pcrel_relocs (&pcrel_relocs))
   1714     return FALSE;
   1715 
   1716   relend = relocs + input_section->reloc_count;
   1717   for (rel = relocs; rel < relend; rel++)
   1718     {
   1719       unsigned long r_symndx;
   1720       struct elf_link_hash_entry *h;
   1721       Elf_Internal_Sym *sym;
   1722       asection *sec;
   1723       bfd_vma relocation;
   1724       bfd_reloc_status_type r = bfd_reloc_ok;
   1725       const char *name;
   1726       bfd_vma off, ie_off;
   1727       bfd_boolean unresolved_reloc, is_ie = FALSE;
   1728       bfd_vma pc = sec_addr (input_section) + rel->r_offset;
   1729       int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
   1730       reloc_howto_type *howto = riscv_elf_rtype_to_howto (r_type);
   1731       const char *msg = NULL;
   1732 
   1733       if (r_type == R_RISCV_GNU_VTINHERIT || r_type == R_RISCV_GNU_VTENTRY)
   1734 	continue;
   1735 
   1736       /* This is a final link.  */
   1737       r_symndx = ELFNN_R_SYM (rel->r_info);
   1738       h = NULL;
   1739       sym = NULL;
   1740       sec = NULL;
   1741       unresolved_reloc = FALSE;
   1742       if (r_symndx < symtab_hdr->sh_info)
   1743 	{
   1744 	  sym = local_syms + r_symndx;
   1745 	  sec = local_sections[r_symndx];
   1746 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   1747 	}
   1748       else
   1749 	{
   1750 	  bfd_boolean warned, ignored;
   1751 
   1752 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   1753 				   r_symndx, symtab_hdr, sym_hashes,
   1754 				   h, sec, relocation,
   1755 				   unresolved_reloc, warned, ignored);
   1756 	  if (warned)
   1757 	    {
   1758 	      /* To avoid generating warning messages about truncated
   1759 		 relocations, set the relocation's address to be the same as
   1760 		 the start of this section.  */
   1761 	      if (input_section->output_section != NULL)
   1762 		relocation = input_section->output_section->vma;
   1763 	      else
   1764 		relocation = 0;
   1765 	    }
   1766 	}
   1767 
   1768       if (sec != NULL && discarded_section (sec))
   1769 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   1770 					 rel, 1, relend, howto, 0, contents);
   1771 
   1772       if (bfd_link_relocatable (info))
   1773 	continue;
   1774 
   1775       if (h != NULL)
   1776 	name = h->root.root.string;
   1777       else
   1778 	{
   1779 	  name = (bfd_elf_string_from_elf_section
   1780 		  (input_bfd, symtab_hdr->sh_link, sym->st_name));
   1781 	  if (name == NULL || *name == '\0')
   1782 	    name = bfd_section_name (input_bfd, sec);
   1783 	}
   1784 
   1785       switch (r_type)
   1786 	{
   1787 	case R_RISCV_NONE:
   1788 	case R_RISCV_RELAX:
   1789 	case R_RISCV_TPREL_ADD:
   1790 	case R_RISCV_COPY:
   1791 	case R_RISCV_JUMP_SLOT:
   1792 	case R_RISCV_RELATIVE:
   1793 	  /* These require nothing of us at all.  */
   1794 	  continue;
   1795 
   1796 	case R_RISCV_HI20:
   1797 	case R_RISCV_BRANCH:
   1798 	case R_RISCV_RVC_BRANCH:
   1799 	case R_RISCV_RVC_LUI:
   1800 	case R_RISCV_LO12_I:
   1801 	case R_RISCV_LO12_S:
   1802 	case R_RISCV_SET6:
   1803 	case R_RISCV_SET8:
   1804 	case R_RISCV_SET16:
   1805 	case R_RISCV_SET32:
   1806 	case R_RISCV_32_PCREL:
   1807 	case R_RISCV_DELETE:
   1808 	  /* These require no special handling beyond perform_relocation.  */
   1809 	  break;
   1810 
   1811 	case R_RISCV_GOT_HI20:
   1812 	  if (h != NULL)
   1813 	    {
   1814 	      bfd_boolean dyn, pic;
   1815 
   1816 	      off = h->got.offset;
   1817 	      BFD_ASSERT (off != (bfd_vma) -1);
   1818 	      dyn = elf_hash_table (info)->dynamic_sections_created;
   1819 	      pic = bfd_link_pic (info);
   1820 
   1821 	      if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
   1822 		  || (pic && SYMBOL_REFERENCES_LOCAL (info, h)))
   1823 		{
   1824 		  /* This is actually a static link, or it is a
   1825 		     -Bsymbolic link and the symbol is defined
   1826 		     locally, or the symbol was forced to be local
   1827 		     because of a version file.  We must initialize
   1828 		     this entry in the global offset table.  Since the
   1829 		     offset must always be a multiple of the word size,
   1830 		     we use the least significant bit to record whether
   1831 		     we have initialized it already.
   1832 
   1833 		     When doing a dynamic link, we create a .rela.got
   1834 		     relocation entry to initialize the value.  This
   1835 		     is done in the finish_dynamic_symbol routine.  */
   1836 		  if ((off & 1) != 0)
   1837 		    off &= ~1;
   1838 		  else
   1839 		    {
   1840 		      bfd_put_NN (output_bfd, relocation,
   1841 				  htab->elf.sgot->contents + off);
   1842 		      h->got.offset |= 1;
   1843 		    }
   1844 		}
   1845 	      else
   1846 		unresolved_reloc = FALSE;
   1847 	    }
   1848 	  else
   1849 	    {
   1850 	      BFD_ASSERT (local_got_offsets != NULL
   1851 			  && local_got_offsets[r_symndx] != (bfd_vma) -1);
   1852 
   1853 	      off = local_got_offsets[r_symndx];
   1854 
   1855 	      /* The offset must always be a multiple of the word size.
   1856 		 So, we can use the least significant bit to record
   1857 		 whether we have already processed this entry.  */
   1858 	      if ((off & 1) != 0)
   1859 		off &= ~1;
   1860 	      else
   1861 		{
   1862 		  if (bfd_link_pic (info))
   1863 		    {
   1864 		      asection *s;
   1865 		      Elf_Internal_Rela outrel;
   1866 
   1867 		      /* We need to generate a R_RISCV_RELATIVE reloc
   1868 			 for the dynamic linker.  */
   1869 		      s = htab->elf.srelgot;
   1870 		      BFD_ASSERT (s != NULL);
   1871 
   1872 		      outrel.r_offset = sec_addr (htab->elf.sgot) + off;
   1873 		      outrel.r_info =
   1874 			ELFNN_R_INFO (0, R_RISCV_RELATIVE);
   1875 		      outrel.r_addend = relocation;
   1876 		      relocation = 0;
   1877 		      riscv_elf_append_rela (output_bfd, s, &outrel);
   1878 		    }
   1879 
   1880 		  bfd_put_NN (output_bfd, relocation,
   1881 			      htab->elf.sgot->contents + off);
   1882 		  local_got_offsets[r_symndx] |= 1;
   1883 		}
   1884 	    }
   1885 	  relocation = sec_addr (htab->elf.sgot) + off;
   1886 	  absolute = riscv_zero_pcrel_hi_reloc (rel,
   1887 						info,
   1888 						pc,
   1889 						relocation,
   1890 						contents,
   1891 						howto,
   1892 						input_bfd);
   1893 	  r_type = ELFNN_R_TYPE (rel->r_info);
   1894 	  howto = riscv_elf_rtype_to_howto (r_type);
   1895 	  if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
   1896 					    relocation, absolute))
   1897 	    r = bfd_reloc_overflow;
   1898 	  break;
   1899 
   1900 	case R_RISCV_ADD8:
   1901 	case R_RISCV_ADD16:
   1902 	case R_RISCV_ADD32:
   1903 	case R_RISCV_ADD64:
   1904 	  {
   1905 	    bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
   1906 					 contents + rel->r_offset);
   1907 	    relocation = old_value + relocation;
   1908 	  }
   1909 	  break;
   1910 
   1911 	case R_RISCV_SUB6:
   1912 	case R_RISCV_SUB8:
   1913 	case R_RISCV_SUB16:
   1914 	case R_RISCV_SUB32:
   1915 	case R_RISCV_SUB64:
   1916 	  {
   1917 	    bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
   1918 					 contents + rel->r_offset);
   1919 	    relocation = old_value - relocation;
   1920 	  }
   1921 	  break;
   1922 
   1923 	case R_RISCV_CALL_PLT:
   1924 	case R_RISCV_CALL:
   1925 	case R_RISCV_JAL:
   1926 	case R_RISCV_RVC_JUMP:
   1927 	  if (bfd_link_pic (info) && h != NULL && h->plt.offset != MINUS_ONE)
   1928 	    {
   1929 	      /* Refer to the PLT entry.  */
   1930 	      relocation = sec_addr (htab->elf.splt) + h->plt.offset;
   1931 	      unresolved_reloc = FALSE;
   1932 	    }
   1933 	  break;
   1934 
   1935 	case R_RISCV_TPREL_HI20:
   1936 	  relocation = tpoff (info, relocation);
   1937 	  break;
   1938 
   1939 	case R_RISCV_TPREL_LO12_I:
   1940 	case R_RISCV_TPREL_LO12_S:
   1941 	  relocation = tpoff (info, relocation);
   1942 	  break;
   1943 
   1944 	case R_RISCV_TPREL_I:
   1945 	case R_RISCV_TPREL_S:
   1946 	  relocation = tpoff (info, relocation);
   1947 	  if (VALID_ITYPE_IMM (relocation + rel->r_addend))
   1948 	    {
   1949 	      /* We can use tp as the base register.  */
   1950 	      bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   1951 	      insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
   1952 	      insn |= X_TP << OP_SH_RS1;
   1953 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   1954 	    }
   1955 	  else
   1956 	    r = bfd_reloc_overflow;
   1957 	  break;
   1958 
   1959 	case R_RISCV_GPREL_I:
   1960 	case R_RISCV_GPREL_S:
   1961 	  {
   1962 	    bfd_vma gp = riscv_global_pointer_value (info);
   1963 	    bfd_boolean x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
   1964 	    if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
   1965 	      {
   1966 		/* We can use x0 or gp as the base register.  */
   1967 		bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   1968 		insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
   1969 		if (!x0_base)
   1970 		  {
   1971 		    rel->r_addend -= gp;
   1972 		    insn |= X_GP << OP_SH_RS1;
   1973 		  }
   1974 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   1975 	      }
   1976 	    else
   1977 	      r = bfd_reloc_overflow;
   1978 	    break;
   1979 	  }
   1980 
   1981 	case R_RISCV_PCREL_HI20:
   1982 	  absolute = riscv_zero_pcrel_hi_reloc (rel,
   1983 						info,
   1984 						pc,
   1985 						relocation,
   1986 						contents,
   1987 						howto,
   1988 						input_bfd);
   1989 	  r_type = ELFNN_R_TYPE (rel->r_info);
   1990 	  howto = riscv_elf_rtype_to_howto (r_type);
   1991 	  if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
   1992 					    relocation + rel->r_addend,
   1993 					    absolute))
   1994 	    r = bfd_reloc_overflow;
   1995 	  break;
   1996 
   1997 	case R_RISCV_PCREL_LO12_I:
   1998 	case R_RISCV_PCREL_LO12_S:
   1999 	  if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, input_section, info,
   2000 					   howto, rel, relocation, name,
   2001 					   contents))
   2002 	    continue;
   2003 	  r = bfd_reloc_overflow;
   2004 	  break;
   2005 
   2006 	case R_RISCV_TLS_DTPREL32:
   2007 	case R_RISCV_TLS_DTPREL64:
   2008 	  relocation = dtpoff (info, relocation);
   2009 	  break;
   2010 
   2011 	case R_RISCV_32:
   2012 	case R_RISCV_64:
   2013 	  if ((input_section->flags & SEC_ALLOC) == 0)
   2014 	    break;
   2015 
   2016 	  if ((bfd_link_pic (info)
   2017 	       && (h == NULL
   2018 		   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2019 		   || h->root.type != bfd_link_hash_undefweak)
   2020 	       && (! howto->pc_relative
   2021 		   || !SYMBOL_CALLS_LOCAL (info, h)))
   2022 	      || (!bfd_link_pic (info)
   2023 		  && h != NULL
   2024 		  && h->dynindx != -1
   2025 		  && !h->non_got_ref
   2026 		  && ((h->def_dynamic
   2027 		       && !h->def_regular)
   2028 		      || h->root.type == bfd_link_hash_undefweak
   2029 		      || h->root.type == bfd_link_hash_undefined)))
   2030 	    {
   2031 	      Elf_Internal_Rela outrel;
   2032 	      bfd_boolean skip_static_relocation, skip_dynamic_relocation;
   2033 
   2034 	      /* When generating a shared object, these relocations
   2035 		 are copied into the output file to be resolved at run
   2036 		 time.  */
   2037 
   2038 	      outrel.r_offset =
   2039 		_bfd_elf_section_offset (output_bfd, info, input_section,
   2040 					 rel->r_offset);
   2041 	      skip_static_relocation = outrel.r_offset != (bfd_vma) -2;
   2042 	      skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2;
   2043 	      outrel.r_offset += sec_addr (input_section);
   2044 
   2045 	      if (skip_dynamic_relocation)
   2046 		memset (&outrel, 0, sizeof outrel);
   2047 	      else if (h != NULL && h->dynindx != -1
   2048 		       && !(bfd_link_pic (info)
   2049 			    && SYMBOLIC_BIND (info, h)
   2050 			    && h->def_regular))
   2051 		{
   2052 		  outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
   2053 		  outrel.r_addend = rel->r_addend;
   2054 		}
   2055 	      else
   2056 		{
   2057 		  outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
   2058 		  outrel.r_addend = relocation + rel->r_addend;
   2059 		}
   2060 
   2061 	      riscv_elf_append_rela (output_bfd, sreloc, &outrel);
   2062 	      if (skip_static_relocation)
   2063 		continue;
   2064 	    }
   2065 	  break;
   2066 
   2067 	case R_RISCV_TLS_GOT_HI20:
   2068 	  is_ie = TRUE;
   2069 	  /* Fall through.  */
   2070 
   2071 	case R_RISCV_TLS_GD_HI20:
   2072 	  if (h != NULL)
   2073 	    {
   2074 	      off = h->got.offset;
   2075 	      h->got.offset |= 1;
   2076 	    }
   2077 	  else
   2078 	    {
   2079 	      off = local_got_offsets[r_symndx];
   2080 	      local_got_offsets[r_symndx] |= 1;
   2081 	    }
   2082 
   2083 	  tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
   2084 	  BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
   2085 	  /* If this symbol is referenced by both GD and IE TLS, the IE
   2086 	     reference's GOT slot follows the GD reference's slots.  */
   2087 	  ie_off = 0;
   2088 	  if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
   2089 	    ie_off = 2 * GOT_ENTRY_SIZE;
   2090 
   2091 	  if ((off & 1) != 0)
   2092 	    off &= ~1;
   2093 	  else
   2094 	    {
   2095 	      Elf_Internal_Rela outrel;
   2096 	      int indx = 0;
   2097 	      bfd_boolean need_relocs = FALSE;
   2098 
   2099 	      if (htab->elf.srelgot == NULL)
   2100 		abort ();
   2101 
   2102 	      if (h != NULL)
   2103 		{
   2104 		  bfd_boolean dyn, pic;
   2105 		  dyn = htab->elf.dynamic_sections_created;
   2106 		  pic = bfd_link_pic (info);
   2107 
   2108 		  if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
   2109 		      && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h)))
   2110 		    indx = h->dynindx;
   2111 		}
   2112 
   2113 	      /* The GOT entries have not been initialized yet.  Do it
   2114 		 now, and emit any relocations.  */
   2115 	      if ((bfd_link_pic (info) || indx != 0)
   2116 		  && (h == NULL
   2117 		      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   2118 		      || h->root.type != bfd_link_hash_undefweak))
   2119 		    need_relocs = TRUE;
   2120 
   2121 	      if (tls_type & GOT_TLS_GD)
   2122 		{
   2123 		  if (need_relocs)
   2124 		    {
   2125 		      outrel.r_offset = sec_addr (htab->elf.sgot) + off;
   2126 		      outrel.r_addend = 0;
   2127 		      outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN);
   2128 		      bfd_put_NN (output_bfd, 0,
   2129 				  htab->elf.sgot->contents + off);
   2130 		      riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
   2131 		      if (indx == 0)
   2132 			{
   2133 			  BFD_ASSERT (! unresolved_reloc);
   2134 			  bfd_put_NN (output_bfd,
   2135 				      dtpoff (info, relocation),
   2136 				      (htab->elf.sgot->contents + off +
   2137 				       RISCV_ELF_WORD_BYTES));
   2138 			}
   2139 		      else
   2140 			{
   2141 			  bfd_put_NN (output_bfd, 0,
   2142 				      (htab->elf.sgot->contents + off +
   2143 				       RISCV_ELF_WORD_BYTES));
   2144 			  outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN);
   2145 			  outrel.r_offset += RISCV_ELF_WORD_BYTES;
   2146 			  riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
   2147 			}
   2148 		    }
   2149 		  else
   2150 		    {
   2151 		      /* If we are not emitting relocations for a
   2152 			 general dynamic reference, then we must be in a
   2153 			 static link or an executable link with the
   2154 			 symbol binding locally.  Mark it as belonging
   2155 			 to module 1, the executable.  */
   2156 		      bfd_put_NN (output_bfd, 1,
   2157 				  htab->elf.sgot->contents + off);
   2158 		      bfd_put_NN (output_bfd,
   2159 				  dtpoff (info, relocation),
   2160 				  (htab->elf.sgot->contents + off +
   2161 				   RISCV_ELF_WORD_BYTES));
   2162 		   }
   2163 		}
   2164 
   2165 	      if (tls_type & GOT_TLS_IE)
   2166 		{
   2167 		  if (need_relocs)
   2168 		    {
   2169 		      bfd_put_NN (output_bfd, 0,
   2170 				  htab->elf.sgot->contents + off + ie_off);
   2171 		      outrel.r_offset = sec_addr (htab->elf.sgot)
   2172 				       + off + ie_off;
   2173 		      outrel.r_addend = 0;
   2174 		      if (indx == 0)
   2175 			outrel.r_addend = tpoff (info, relocation);
   2176 		      outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN);
   2177 		      riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
   2178 		    }
   2179 		  else
   2180 		    {
   2181 		      bfd_put_NN (output_bfd, tpoff (info, relocation),
   2182 				  htab->elf.sgot->contents + off + ie_off);
   2183 		    }
   2184 		}
   2185 	    }
   2186 
   2187 	  BFD_ASSERT (off < (bfd_vma) -2);
   2188 	  relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
   2189 	  if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
   2190 					    relocation, FALSE))
   2191 	    r = bfd_reloc_overflow;
   2192 	  unresolved_reloc = FALSE;
   2193 	  break;
   2194 
   2195 	default:
   2196 	  r = bfd_reloc_notsupported;
   2197 	}
   2198 
   2199       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   2200 	 because such sections are not SEC_ALLOC and thus ld.so will
   2201 	 not process them.  */
   2202       if (unresolved_reloc
   2203 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   2204 	       && h->def_dynamic)
   2205 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   2206 				      rel->r_offset) != (bfd_vma) -1)
   2207 	{
   2208 	  (*_bfd_error_handler)
   2209 	    (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
   2210 	     input_bfd,
   2211 	     input_section,
   2212 	     rel->r_offset,
   2213 	     howto->name,
   2214 	     h->root.root.string);
   2215 	  continue;
   2216 	}
   2217 
   2218       if (r == bfd_reloc_ok)
   2219 	r = perform_relocation (howto, rel, relocation, input_section,
   2220 				input_bfd, contents);
   2221 
   2222       switch (r)
   2223 	{
   2224 	case bfd_reloc_ok:
   2225 	  continue;
   2226 
   2227 	case bfd_reloc_overflow:
   2228 	  info->callbacks->reloc_overflow
   2229 	    (info, (h ? &h->root : NULL), name, howto->name,
   2230 	     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
   2231 	  break;
   2232 
   2233 	case bfd_reloc_undefined:
   2234 	  info->callbacks->undefined_symbol
   2235 	    (info, name, input_bfd, input_section, rel->r_offset,
   2236 	     TRUE);
   2237 	  break;
   2238 
   2239 	case bfd_reloc_outofrange:
   2240 	  msg = _("internal error: out of range error");
   2241 	  break;
   2242 
   2243 	case bfd_reloc_notsupported:
   2244 	  msg = _("internal error: unsupported relocation error");
   2245 	  break;
   2246 
   2247 	case bfd_reloc_dangerous:
   2248 	  msg = _("internal error: dangerous relocation");
   2249 	  break;
   2250 
   2251 	default:
   2252 	  msg = _("internal error: unknown error");
   2253 	  break;
   2254 	}
   2255 
   2256       if (msg)
   2257 	info->callbacks->warning
   2258 	  (info, msg, name, input_bfd, input_section, rel->r_offset);
   2259       goto out;
   2260     }
   2261 
   2262   ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
   2263 out:
   2264   riscv_free_pcrel_relocs (&pcrel_relocs);
   2265   return ret;
   2266 }
   2267 
   2268 /* Finish up dynamic symbol handling.  We set the contents of various
   2269    dynamic sections here.  */
   2270 
   2271 static bfd_boolean
   2272 riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
   2273 				 struct bfd_link_info *info,
   2274 				 struct elf_link_hash_entry *h,
   2275 				 Elf_Internal_Sym *sym)
   2276 {
   2277   struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
   2278   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   2279 
   2280   if (h->plt.offset != (bfd_vma) -1)
   2281     {
   2282       /* We've decided to create a PLT entry for this symbol.  */
   2283       bfd_byte *loc;
   2284       bfd_vma i, header_address, plt_idx, got_address;
   2285       uint32_t plt_entry[PLT_ENTRY_INSNS];
   2286       Elf_Internal_Rela rela;
   2287 
   2288       BFD_ASSERT (h->dynindx != -1);
   2289 
   2290       /* Calculate the address of the PLT header.  */
   2291       header_address = sec_addr (htab->elf.splt);
   2292 
   2293       /* Calculate the index of the entry.  */
   2294       plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
   2295 
   2296       /* Calculate the address of the .got.plt entry.  */
   2297       got_address = riscv_elf_got_plt_val (plt_idx, info);
   2298 
   2299       /* Find out where the .plt entry should go.  */
   2300       loc = htab->elf.splt->contents + h->plt.offset;
   2301 
   2302       /* Fill in the PLT entry itself.  */
   2303       riscv_make_plt_entry (got_address, header_address + h->plt.offset,
   2304 			    plt_entry);
   2305       for (i = 0; i < PLT_ENTRY_INSNS; i++)
   2306 	bfd_put_32 (output_bfd, plt_entry[i], loc + 4*i);
   2307 
   2308       /* Fill in the initial value of the .got.plt entry.  */
   2309       loc = htab->elf.sgotplt->contents
   2310 	    + (got_address - sec_addr (htab->elf.sgotplt));
   2311       bfd_put_NN (output_bfd, sec_addr (htab->elf.splt), loc);
   2312 
   2313       /* Fill in the entry in the .rela.plt section.  */
   2314       rela.r_offset = got_address;
   2315       rela.r_addend = 0;
   2316       rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
   2317 
   2318       loc = htab->elf.srelplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
   2319       bed->s->swap_reloca_out (output_bfd, &rela, loc);
   2320 
   2321       if (!h->def_regular)
   2322 	{
   2323 	  /* Mark the symbol as undefined, rather than as defined in
   2324 	     the .plt section.  Leave the value alone.  */
   2325 	  sym->st_shndx = SHN_UNDEF;
   2326 	  /* If the symbol is weak, we do need to clear the value.
   2327 	     Otherwise, the PLT entry would provide a definition for
   2328 	     the symbol even if the symbol wasn't defined anywhere,
   2329 	     and so the symbol would never be NULL.  */
   2330 	  if (!h->ref_regular_nonweak)
   2331 	    sym->st_value = 0;
   2332 	}
   2333     }
   2334 
   2335   if (h->got.offset != (bfd_vma) -1
   2336       && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
   2337     {
   2338       asection *sgot;
   2339       asection *srela;
   2340       Elf_Internal_Rela rela;
   2341 
   2342       /* This symbol has an entry in the GOT.  Set it up.  */
   2343 
   2344       sgot = htab->elf.sgot;
   2345       srela = htab->elf.srelgot;
   2346       BFD_ASSERT (sgot != NULL && srela != NULL);
   2347 
   2348       rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
   2349 
   2350       /* If this is a -Bsymbolic link, and the symbol is defined
   2351 	 locally, we just want to emit a RELATIVE reloc.  Likewise if
   2352 	 the symbol was forced to be local because of a version file.
   2353 	 The entry in the global offset table will already have been
   2354 	 initialized in the relocate_section function.  */
   2355       if (bfd_link_pic (info)
   2356 	  && (info->symbolic || h->dynindx == -1)
   2357 	  && h->def_regular)
   2358 	{
   2359 	  asection *sec = h->root.u.def.section;
   2360 	  rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
   2361 	  rela.r_addend = (h->root.u.def.value
   2362 			   + sec->output_section->vma
   2363 			   + sec->output_offset);
   2364 	}
   2365       else
   2366 	{
   2367 	  BFD_ASSERT (h->dynindx != -1);
   2368 	  rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
   2369 	  rela.r_addend = 0;
   2370 	}
   2371 
   2372       bfd_put_NN (output_bfd, 0,
   2373 		  sgot->contents + (h->got.offset & ~(bfd_vma) 1));
   2374       riscv_elf_append_rela (output_bfd, srela, &rela);
   2375     }
   2376 
   2377   if (h->needs_copy)
   2378     {
   2379       Elf_Internal_Rela rela;
   2380       asection *s;
   2381 
   2382       /* This symbols needs a copy reloc.  Set it up.  */
   2383       BFD_ASSERT (h->dynindx != -1);
   2384 
   2385       rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
   2386       rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY);
   2387       rela.r_addend = 0;
   2388       if (h->root.u.def.section == htab->elf.sdynrelro)
   2389 	s = htab->elf.sreldynrelro;
   2390       else
   2391 	s = htab->elf.srelbss;
   2392       riscv_elf_append_rela (output_bfd, s, &rela);
   2393     }
   2394 
   2395   /* Mark some specially defined symbols as absolute.  */
   2396   if (h == htab->elf.hdynamic
   2397       || (h == htab->elf.hgot || h == htab->elf.hplt))
   2398     sym->st_shndx = SHN_ABS;
   2399 
   2400   return TRUE;
   2401 }
   2402 
   2403 /* Finish up the dynamic sections.  */
   2404 
   2405 static bfd_boolean
   2406 riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
   2407 		  bfd *dynobj, asection *sdyn)
   2408 {
   2409   struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
   2410   const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
   2411   size_t dynsize = bed->s->sizeof_dyn;
   2412   bfd_byte *dyncon, *dynconend;
   2413 
   2414   dynconend = sdyn->contents + sdyn->size;
   2415   for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
   2416     {
   2417       Elf_Internal_Dyn dyn;
   2418       asection *s;
   2419 
   2420       bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
   2421 
   2422       switch (dyn.d_tag)
   2423 	{
   2424 	case DT_PLTGOT:
   2425 	  s = htab->elf.sgotplt;
   2426 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   2427 	  break;
   2428 	case DT_JMPREL:
   2429 	  s = htab->elf.srelplt;
   2430 	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   2431 	  break;
   2432 	case DT_PLTRELSZ:
   2433 	  s = htab->elf.srelplt;
   2434 	  dyn.d_un.d_val = s->size;
   2435 	  break;
   2436 	default:
   2437 	  continue;
   2438 	}
   2439 
   2440       bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
   2441     }
   2442   return TRUE;
   2443 }
   2444 
   2445 static bfd_boolean
   2446 riscv_elf_finish_dynamic_sections (bfd *output_bfd,
   2447 				   struct bfd_link_info *info)
   2448 {
   2449   bfd *dynobj;
   2450   asection *sdyn;
   2451   struct riscv_elf_link_hash_table *htab;
   2452 
   2453   htab = riscv_elf_hash_table (info);
   2454   BFD_ASSERT (htab != NULL);
   2455   dynobj = htab->elf.dynobj;
   2456 
   2457   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   2458 
   2459   if (elf_hash_table (info)->dynamic_sections_created)
   2460     {
   2461       asection *splt;
   2462       bfd_boolean ret;
   2463 
   2464       splt = htab->elf.splt;
   2465       BFD_ASSERT (splt != NULL && sdyn != NULL);
   2466 
   2467       ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
   2468 
   2469       if (!ret)
   2470 	return ret;
   2471 
   2472       /* Fill in the head and tail entries in the procedure linkage table.  */
   2473       if (splt->size > 0)
   2474 	{
   2475 	  int i;
   2476 	  uint32_t plt_header[PLT_HEADER_INSNS];
   2477 	  riscv_make_plt_header (sec_addr (htab->elf.sgotplt),
   2478 				 sec_addr (splt), plt_header);
   2479 
   2480 	  for (i = 0; i < PLT_HEADER_INSNS; i++)
   2481 	    bfd_put_32 (output_bfd, plt_header[i], splt->contents + 4*i);
   2482 
   2483 	  elf_section_data (splt->output_section)->this_hdr.sh_entsize
   2484 	    = PLT_ENTRY_SIZE;
   2485 	}
   2486     }
   2487 
   2488   if (htab->elf.sgotplt)
   2489     {
   2490       asection *output_section = htab->elf.sgotplt->output_section;
   2491 
   2492       if (bfd_is_abs_section (output_section))
   2493 	{
   2494 	  (*_bfd_error_handler)
   2495 	    (_("discarded output section: `%A'"), htab->elf.sgotplt);
   2496 	  return FALSE;
   2497 	}
   2498 
   2499       if (htab->elf.sgotplt->size > 0)
   2500 	{
   2501 	  /* Write the first two entries in .got.plt, needed for the dynamic
   2502 	     linker.  */
   2503 	  bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
   2504 	  bfd_put_NN (output_bfd, (bfd_vma) 0,
   2505 		      htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
   2506 	}
   2507 
   2508       elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
   2509     }
   2510 
   2511   if (htab->elf.sgot)
   2512     {
   2513       asection *output_section = htab->elf.sgot->output_section;
   2514 
   2515       if (htab->elf.sgot->size > 0)
   2516 	{
   2517 	  /* Set the first entry in the global offset table to the address of
   2518 	     the dynamic section.  */
   2519 	  bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
   2520 	  bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
   2521 	}
   2522 
   2523       elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
   2524     }
   2525 
   2526   return TRUE;
   2527 }
   2528 
   2529 /* Return address for Ith PLT stub in section PLT, for relocation REL
   2530    or (bfd_vma) -1 if it should not be included.  */
   2531 
   2532 static bfd_vma
   2533 riscv_elf_plt_sym_val (bfd_vma i, const asection *plt,
   2534 		       const arelent *rel ATTRIBUTE_UNUSED)
   2535 {
   2536   return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
   2537 }
   2538 
   2539 static enum elf_reloc_type_class
   2540 riscv_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   2541 			const asection *rel_sec ATTRIBUTE_UNUSED,
   2542 			const Elf_Internal_Rela *rela)
   2543 {
   2544   switch (ELFNN_R_TYPE (rela->r_info))
   2545     {
   2546     case R_RISCV_RELATIVE:
   2547       return reloc_class_relative;
   2548     case R_RISCV_JUMP_SLOT:
   2549       return reloc_class_plt;
   2550     case R_RISCV_COPY:
   2551       return reloc_class_copy;
   2552     default:
   2553       return reloc_class_normal;
   2554     }
   2555 }
   2556 
   2557 /* Merge backend specific data from an object file to the output
   2558    object file when linking.  */
   2559 
   2560 static bfd_boolean
   2561 _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   2562 {
   2563   bfd *obfd = info->output_bfd;
   2564   flagword new_flags = elf_elfheader (ibfd)->e_flags;
   2565   flagword old_flags = elf_elfheader (obfd)->e_flags;
   2566 
   2567   if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd))
   2568     return TRUE;
   2569 
   2570   if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
   2571     {
   2572       (*_bfd_error_handler)
   2573 	(_("%B: ABI is incompatible with that of the selected emulation:\n"
   2574 	   "  target emulation `%s' does not match `%s'"),
   2575 	 ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
   2576       return FALSE;
   2577     }
   2578 
   2579   if (!_bfd_elf_merge_object_attributes (ibfd, info))
   2580     return FALSE;
   2581 
   2582   if (! elf_flags_init (obfd))
   2583     {
   2584       elf_flags_init (obfd) = TRUE;
   2585       elf_elfheader (obfd)->e_flags = new_flags;
   2586       return TRUE;
   2587     }
   2588 
   2589   /* Disallow linking different float ABIs.  */
   2590   if ((old_flags ^ new_flags) & EF_RISCV_FLOAT_ABI)
   2591     {
   2592       (*_bfd_error_handler)
   2593 	(_("%B: can't link hard-float modules with soft-float modules"), ibfd);
   2594       goto fail;
   2595     }
   2596 
   2597   /* Allow linking RVC and non-RVC, and keep the RVC flag.  */
   2598   elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_RVC;
   2599 
   2600   return TRUE;
   2601 
   2602 fail:
   2603   bfd_set_error (bfd_error_bad_value);
   2604   return FALSE;
   2605 }
   2606 
   2607 /* Delete some bytes from a section while relaxing.  */
   2608 
   2609 static bfd_boolean
   2610 riscv_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, size_t count)
   2611 {
   2612   unsigned int i, symcount;
   2613   bfd_vma toaddr = sec->size;
   2614   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
   2615   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2616   unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   2617   struct bfd_elf_section_data *data = elf_section_data (sec);
   2618   bfd_byte *contents = data->this_hdr.contents;
   2619 
   2620   /* Actually delete the bytes.  */
   2621   sec->size -= count;
   2622   memmove (contents + addr, contents + addr + count, toaddr - addr - count);
   2623 
   2624   /* Adjust the location of all of the relocs.  Note that we need not
   2625      adjust the addends, since all PC-relative references must be against
   2626      symbols, which we will adjust below.  */
   2627   for (i = 0; i < sec->reloc_count; i++)
   2628     if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr)
   2629       data->relocs[i].r_offset -= count;
   2630 
   2631   /* Adjust the local symbols defined in this section.  */
   2632   for (i = 0; i < symtab_hdr->sh_info; i++)
   2633     {
   2634       Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
   2635       if (sym->st_shndx == sec_shndx)
   2636 	{
   2637 	  /* If the symbol is in the range of memory we just moved, we
   2638 	     have to adjust its value.  */
   2639 	  if (sym->st_value > addr && sym->st_value <= toaddr)
   2640 	    sym->st_value -= count;
   2641 
   2642 	  /* If the symbol *spans* the bytes we just deleted (i.e. its
   2643 	     *end* is in the moved bytes but its *start* isn't), then we
   2644 	     must adjust its size.  */
   2645 	  if (sym->st_value <= addr
   2646 	      && sym->st_value + sym->st_size > addr
   2647 	      && sym->st_value + sym->st_size <= toaddr)
   2648 	    sym->st_size -= count;
   2649 	}
   2650     }
   2651 
   2652   /* Now adjust the global symbols defined in this section.  */
   2653   symcount = ((symtab_hdr->sh_size / sizeof (ElfNN_External_Sym))
   2654 	      - symtab_hdr->sh_info);
   2655 
   2656   for (i = 0; i < symcount; i++)
   2657     {
   2658       struct elf_link_hash_entry *sym_hash = sym_hashes[i];
   2659 
   2660       if ((sym_hash->root.type == bfd_link_hash_defined
   2661 	   || sym_hash->root.type == bfd_link_hash_defweak)
   2662 	  && sym_hash->root.u.def.section == sec)
   2663 	{
   2664 	  /* As above, adjust the value if needed.  */
   2665 	  if (sym_hash->root.u.def.value > addr
   2666 	      && sym_hash->root.u.def.value <= toaddr)
   2667 	    sym_hash->root.u.def.value -= count;
   2668 
   2669 	  /* As above, adjust the size if needed.  */
   2670 	  if (sym_hash->root.u.def.value <= addr
   2671 	      && sym_hash->root.u.def.value + sym_hash->size > addr
   2672 	      && sym_hash->root.u.def.value + sym_hash->size <= toaddr)
   2673 	    sym_hash->size -= count;
   2674 	}
   2675     }
   2676 
   2677   return TRUE;
   2678 }
   2679 
   2680 /* A second format for recording PC-relative hi relocations.  This stores the
   2681    information required to relax them to GP-relative addresses.  */
   2682 
   2683 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc;
   2684 struct riscv_pcgp_hi_reloc
   2685 {
   2686   bfd_vma hi_sec_off;
   2687   bfd_vma hi_addend;
   2688   bfd_vma hi_addr;
   2689   unsigned hi_sym;
   2690   asection *sym_sec;
   2691   riscv_pcgp_hi_reloc *next;
   2692 };
   2693 
   2694 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc;
   2695 struct riscv_pcgp_lo_reloc
   2696 {
   2697   bfd_vma hi_sec_off;
   2698   riscv_pcgp_lo_reloc *next;
   2699 };
   2700 
   2701 typedef struct
   2702 {
   2703   riscv_pcgp_hi_reloc *hi;
   2704   riscv_pcgp_lo_reloc *lo;
   2705 } riscv_pcgp_relocs;
   2706 
   2707 static bfd_boolean
   2708 riscv_init_pcgp_relocs (riscv_pcgp_relocs *p)
   2709 {
   2710   p->hi = NULL;
   2711   p->lo = NULL;
   2712   return TRUE;
   2713 }
   2714 
   2715 static void
   2716 riscv_free_pcgp_relocs (riscv_pcgp_relocs *p,
   2717 			bfd *abfd ATTRIBUTE_UNUSED,
   2718 			asection *sec ATTRIBUTE_UNUSED)
   2719 {
   2720   riscv_pcgp_hi_reloc *c;
   2721   riscv_pcgp_lo_reloc *l;
   2722 
   2723   for (c = p->hi; c != NULL;)
   2724     {
   2725       riscv_pcgp_hi_reloc *next = c->next;
   2726       free (c);
   2727       c = next;
   2728     }
   2729 
   2730   for (l = p->lo; l != NULL;)
   2731     {
   2732       riscv_pcgp_lo_reloc *next = l->next;
   2733       free (l);
   2734       l = next;
   2735     }
   2736 }
   2737 
   2738 static bfd_boolean
   2739 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off,
   2740 			    bfd_vma hi_addend, bfd_vma hi_addr,
   2741 			    unsigned hi_sym, asection *sym_sec)
   2742 {
   2743   riscv_pcgp_hi_reloc *new = bfd_malloc (sizeof(*new));
   2744   if (!new)
   2745     return FALSE;
   2746   new->hi_sec_off = hi_sec_off;
   2747   new->hi_addend = hi_addend;
   2748   new->hi_addr = hi_addr;
   2749   new->hi_sym = hi_sym;
   2750   new->sym_sec = sym_sec;
   2751   new->next = p->hi;
   2752   p->hi = new;
   2753   return TRUE;
   2754 }
   2755 
   2756 static riscv_pcgp_hi_reloc *
   2757 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
   2758 {
   2759   riscv_pcgp_hi_reloc *c;
   2760 
   2761   for (c = p->hi; c != NULL; c = c->next)
   2762     if (c->hi_sec_off == hi_sec_off)
   2763       return c;
   2764   return NULL;
   2765 }
   2766 
   2767 static bfd_boolean
   2768 riscv_delete_pcgp_hi_reloc(riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
   2769 {
   2770   bfd_boolean out = FALSE;
   2771   riscv_pcgp_hi_reloc *c;
   2772 
   2773   for (c = p->hi; c != NULL; c = c->next)
   2774       if (c->hi_sec_off == hi_sec_off)
   2775 	out = TRUE;
   2776 
   2777   return out;
   2778 }
   2779 
   2780 static bfd_boolean
   2781 riscv_use_pcgp_hi_reloc(riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
   2782 {
   2783   bfd_boolean out = FALSE;
   2784   riscv_pcgp_hi_reloc *c;
   2785 
   2786   for (c = p->hi; c != NULL; c = c->next)
   2787     if (c->hi_sec_off == hi_sec_off)
   2788       out = TRUE;
   2789 
   2790   return out;
   2791 }
   2792 
   2793 static bfd_boolean
   2794 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
   2795 {
   2796   riscv_pcgp_lo_reloc *new = bfd_malloc (sizeof(*new));
   2797   if (!new)
   2798     return FALSE;
   2799   new->hi_sec_off = hi_sec_off;
   2800   new->next = p->lo;
   2801   p->lo = new;
   2802   return TRUE;
   2803 }
   2804 
   2805 static bfd_boolean
   2806 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
   2807 {
   2808   riscv_pcgp_lo_reloc *c;
   2809 
   2810   for (c = p->lo; c != NULL; c = c->next)
   2811     if (c->hi_sec_off == hi_sec_off)
   2812       return TRUE;
   2813   return FALSE;
   2814 }
   2815 
   2816 static bfd_boolean
   2817 riscv_delete_pcgp_lo_reloc (riscv_pcgp_relocs *p ATTRIBUTE_UNUSED,
   2818 			    bfd_vma lo_sec_off ATTRIBUTE_UNUSED,
   2819 			    size_t bytes ATTRIBUTE_UNUSED)
   2820 {
   2821   return TRUE;
   2822 }
   2823 
   2824 typedef bfd_boolean (*relax_func_t) (bfd *, asection *, asection *,
   2825 				     struct bfd_link_info *,
   2826 				     Elf_Internal_Rela *,
   2827 				     bfd_vma, bfd_vma, bfd_vma, bfd_boolean *,
   2828 				     riscv_pcgp_relocs *);
   2829 
   2830 /* Relax AUIPC + JALR into JAL.  */
   2831 
   2832 static bfd_boolean
   2833 _bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
   2834 		       struct bfd_link_info *link_info,
   2835 		       Elf_Internal_Rela *rel,
   2836 		       bfd_vma symval,
   2837 		       bfd_vma max_alignment,
   2838 		       bfd_vma reserve_size ATTRIBUTE_UNUSED,
   2839 		       bfd_boolean *again,
   2840 		       riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED)
   2841 {
   2842   bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
   2843   bfd_signed_vma foff = symval - (sec_addr (sec) + rel->r_offset);
   2844   bfd_boolean near_zero = (symval + RISCV_IMM_REACH/2) < RISCV_IMM_REACH;
   2845   bfd_vma auipc, jalr;
   2846   int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
   2847 
   2848   /* If the call crosses section boundaries, an alignment directive could
   2849      cause the PC-relative offset to later increase.  */
   2850   if (VALID_UJTYPE_IMM (foff) && sym_sec->output_section != sec->output_section)
   2851     foff += (foff < 0 ? -max_alignment : max_alignment);
   2852 
   2853   /* See if this function call can be shortened.  */
   2854   if (!VALID_UJTYPE_IMM (foff) && !(!bfd_link_pic (link_info) && near_zero))
   2855     return TRUE;
   2856 
   2857   /* Shorten the function call.  */
   2858   BFD_ASSERT (rel->r_offset + 8 <= sec->size);
   2859 
   2860   auipc = bfd_get_32 (abfd, contents + rel->r_offset);
   2861   jalr = bfd_get_32 (abfd, contents + rel->r_offset + 4);
   2862   rd = (jalr >> OP_SH_RD) & OP_MASK_RD;
   2863   rvc = rvc && VALID_RVC_J_IMM (foff) && ARCH_SIZE == 32;
   2864 
   2865   if (rvc && (rd == 0 || rd == X_RA))
   2866     {
   2867       /* Relax to C.J[AL] rd, addr.  */
   2868       r_type = R_RISCV_RVC_JUMP;
   2869       auipc = rd == 0 ? MATCH_C_J : MATCH_C_JAL;
   2870       len = 2;
   2871     }
   2872   else if (VALID_UJTYPE_IMM (foff))
   2873     {
   2874       /* Relax to JAL rd, addr.  */
   2875       r_type = R_RISCV_JAL;
   2876       auipc = MATCH_JAL | (rd << OP_SH_RD);
   2877     }
   2878   else /* near_zero */
   2879     {
   2880       /* Relax to JALR rd, x0, addr.  */
   2881       r_type = R_RISCV_LO12_I;
   2882       auipc = MATCH_JALR | (rd << OP_SH_RD);
   2883     }
   2884 
   2885   /* Replace the R_RISCV_CALL reloc.  */
   2886   rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), r_type);
   2887   /* Replace the AUIPC.  */
   2888   bfd_put (8 * len, abfd, auipc, contents + rel->r_offset);
   2889 
   2890   /* Delete unnecessary JALR.  */
   2891   *again = TRUE;
   2892   return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len);
   2893 }
   2894 
   2895 /* Traverse all output sections and return the max alignment.  */
   2896 
   2897 static bfd_vma
   2898 _bfd_riscv_get_max_alignment (asection *sec)
   2899 {
   2900   unsigned int max_alignment_power = 0;
   2901   asection *o;
   2902 
   2903   for (o = sec->output_section->owner->sections; o != NULL; o = o->next)
   2904     {
   2905       if (o->alignment_power > max_alignment_power)
   2906 	max_alignment_power = o->alignment_power;
   2907     }
   2908 
   2909   return (bfd_vma) 1 << max_alignment_power;
   2910 }
   2911 
   2912 /* Relax non-PIC global variable references.  */
   2913 
   2914 static bfd_boolean
   2915 _bfd_riscv_relax_lui (bfd *abfd,
   2916 		      asection *sec,
   2917 		      asection *sym_sec,
   2918 		      struct bfd_link_info *link_info,
   2919 		      Elf_Internal_Rela *rel,
   2920 		      bfd_vma symval,
   2921 		      bfd_vma max_alignment,
   2922 		      bfd_vma reserve_size,
   2923 		      bfd_boolean *again,
   2924 		      riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED)
   2925 {
   2926   bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
   2927   bfd_vma gp = riscv_global_pointer_value (link_info);
   2928   int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
   2929 
   2930   /* Mergeable symbols and code might later move out of range.  */
   2931   if (sym_sec->flags & (SEC_MERGE | SEC_CODE))
   2932     return TRUE;
   2933 
   2934   BFD_ASSERT (rel->r_offset + 4 <= sec->size);
   2935 
   2936   if (gp)
   2937     {
   2938       /* If gp and the symbol are in the same output section, then
   2939 	 consider only that section's alignment.  */
   2940       struct bfd_link_hash_entry *h =
   2941 	bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, FALSE, FALSE,
   2942 			      TRUE);
   2943       if (h->u.def.section->output_section == sym_sec->output_section)
   2944 	max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
   2945     }
   2946 
   2947   /* Is the reference in range of x0 or gp?
   2948      Valid gp range conservatively because of alignment issue.  */
   2949   if (VALID_ITYPE_IMM (symval)
   2950       || (symval >= gp
   2951 	  && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
   2952       || (symval < gp
   2953 	  && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size)))
   2954     {
   2955       unsigned sym = ELFNN_R_SYM (rel->r_info);
   2956       switch (ELFNN_R_TYPE (rel->r_info))
   2957 	{
   2958 	case R_RISCV_LO12_I:
   2959 	  rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
   2960 	  return TRUE;
   2961 
   2962 	case R_RISCV_LO12_S:
   2963 	  rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
   2964 	  return TRUE;
   2965 
   2966 	case R_RISCV_HI20:
   2967 	  /* We can delete the unnecessary LUI and reloc.  */
   2968 	  rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
   2969 	  *again = TRUE;
   2970 	  return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4);
   2971 
   2972 	default:
   2973 	  abort ();
   2974 	}
   2975     }
   2976 
   2977   /* Can we relax LUI to C.LUI?  Alignment might move the section forward;
   2978      account for this assuming page alignment at worst.  */
   2979   if (use_rvc
   2980       && ELFNN_R_TYPE (rel->r_info) == R_RISCV_HI20
   2981       && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval))
   2982       && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval + ELF_MAXPAGESIZE)))
   2983     {
   2984       /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp).  */
   2985       bfd_vma lui = bfd_get_32 (abfd, contents + rel->r_offset);
   2986       unsigned rd = ((unsigned)lui >> OP_SH_RD) & OP_MASK_RD;
   2987       if (rd == 0 || rd == X_SP)
   2988 	return TRUE;
   2989 
   2990       lui = (lui & (OP_MASK_RD << OP_SH_RD)) | MATCH_C_LUI;
   2991       bfd_put_32 (abfd, lui, contents + rel->r_offset);
   2992 
   2993       /* Replace the R_RISCV_HI20 reloc.  */
   2994       rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_RVC_LUI);
   2995 
   2996       *again = TRUE;
   2997       return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2);
   2998     }
   2999 
   3000   return TRUE;
   3001 }
   3002 
   3003 /* Relax non-PIC TLS references.  */
   3004 
   3005 static bfd_boolean
   3006 _bfd_riscv_relax_tls_le (bfd *abfd,
   3007 			 asection *sec,
   3008 			 asection *sym_sec ATTRIBUTE_UNUSED,
   3009 			 struct bfd_link_info *link_info,
   3010 			 Elf_Internal_Rela *rel,
   3011 			 bfd_vma symval,
   3012 			 bfd_vma max_alignment ATTRIBUTE_UNUSED,
   3013 			 bfd_vma reserve_size ATTRIBUTE_UNUSED,
   3014 			 bfd_boolean *again,
   3015 			 riscv_pcgp_relocs *prcel_relocs ATTRIBUTE_UNUSED)
   3016 {
   3017   /* See if this symbol is in range of tp.  */
   3018   if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
   3019     return TRUE;
   3020 
   3021   BFD_ASSERT (rel->r_offset + 4 <= sec->size);
   3022   switch (ELFNN_R_TYPE (rel->r_info))
   3023     {
   3024     case R_RISCV_TPREL_LO12_I:
   3025       rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_I);
   3026       return TRUE;
   3027 
   3028     case R_RISCV_TPREL_LO12_S:
   3029       rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_S);
   3030       return TRUE;
   3031 
   3032     case R_RISCV_TPREL_HI20:
   3033     case R_RISCV_TPREL_ADD:
   3034       /* We can delete the unnecessary instruction and reloc.  */
   3035       rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
   3036       *again = TRUE;
   3037       return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4);
   3038 
   3039     default:
   3040       abort ();
   3041     }
   3042 }
   3043 
   3044 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.  */
   3045 
   3046 static bfd_boolean
   3047 _bfd_riscv_relax_align (bfd *abfd, asection *sec,
   3048 			asection *sym_sec,
   3049 			struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   3050 			Elf_Internal_Rela *rel,
   3051 			bfd_vma symval,
   3052 			bfd_vma max_alignment ATTRIBUTE_UNUSED,
   3053 			bfd_vma reserve_size ATTRIBUTE_UNUSED,
   3054 			bfd_boolean *again ATTRIBUTE_UNUSED,
   3055 			riscv_pcgp_relocs *pcrel_relocs ATTRIBUTE_UNUSED)
   3056 {
   3057   bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
   3058   bfd_vma alignment = 1, pos;
   3059   while (alignment <= rel->r_addend)
   3060     alignment *= 2;
   3061 
   3062   symval -= rel->r_addend;
   3063   bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
   3064   bfd_vma nop_bytes = aligned_addr - symval;
   3065 
   3066   /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else.  */
   3067   sec->sec_flg0 = TRUE;
   3068 
   3069   /* Make sure there are enough NOPs to actually achieve the alignment.  */
   3070   if (rel->r_addend < nop_bytes)
   3071     {
   3072       (*_bfd_error_handler)
   3073 	(_("%B(%A+0x%lx): %d bytes required for alignment "
   3074 	   "to %d-byte boundary, but only %d present"),
   3075 	   abfd, sym_sec, rel->r_offset, nop_bytes, alignment, rel->r_addend);
   3076       bfd_set_error (bfd_error_bad_value);
   3077       return FALSE;
   3078     }
   3079 
   3080   /* Delete the reloc.  */
   3081   rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
   3082 
   3083   /* If the number of NOPs is already correct, there's nothing to do.  */
   3084   if (nop_bytes == rel->r_addend)
   3085     return TRUE;
   3086 
   3087   /* Write as many RISC-V NOPs as we need.  */
   3088   for (pos = 0; pos < (nop_bytes & -4); pos += 4)
   3089     bfd_put_32 (abfd, RISCV_NOP, contents + rel->r_offset + pos);
   3090 
   3091   /* Write a final RVC NOP if need be.  */
   3092   if (nop_bytes % 4 != 0)
   3093     bfd_put_16 (abfd, RVC_NOP, contents + rel->r_offset + pos);
   3094 
   3095   /* Delete the excess bytes.  */
   3096   return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes,
   3097 				   rel->r_addend - nop_bytes);
   3098 }
   3099 
   3100 /* Relax PC-relative references to GP-relative references.  */
   3101 
   3102 static bfd_boolean
   3103 _bfd_riscv_relax_pc  (bfd *abfd,
   3104 		      asection *sec,
   3105 		      asection *sym_sec,
   3106 		      struct bfd_link_info *link_info,
   3107 		      Elf_Internal_Rela *rel,
   3108 		      bfd_vma symval,
   3109 		      bfd_vma max_alignment,
   3110 		      bfd_vma reserve_size,
   3111 		      bfd_boolean *again ATTRIBUTE_UNUSED,
   3112 		      riscv_pcgp_relocs *pcgp_relocs)
   3113 {
   3114   bfd_vma gp = riscv_global_pointer_value (link_info);
   3115 
   3116   BFD_ASSERT (rel->r_offset + 4 <= sec->size);
   3117 
   3118   /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
   3119    * actual target address.  */
   3120   riscv_pcgp_hi_reloc hi_reloc = {0};
   3121   switch (ELFNN_R_TYPE (rel->r_info))
   3122     {
   3123     case R_RISCV_PCREL_LO12_I:
   3124     case R_RISCV_PCREL_LO12_S:
   3125       {
   3126 	riscv_pcgp_hi_reloc *hi = riscv_find_pcgp_hi_reloc (pcgp_relocs,
   3127 							    symval - sec_addr(sym_sec));
   3128 	if (hi == NULL)
   3129 	  {
   3130 	    riscv_record_pcgp_lo_reloc (pcgp_relocs, symval - sec_addr(sym_sec));
   3131 	    return TRUE;
   3132 	  }
   3133 
   3134 	hi_reloc = *hi;
   3135 	symval = hi_reloc.hi_addr;
   3136 	sym_sec = hi_reloc.sym_sec;
   3137 	if (!riscv_use_pcgp_hi_reloc(pcgp_relocs, hi->hi_sec_off))
   3138 	  (*_bfd_error_handler)
   3139 	   (_("%B(%A+0x%lx): Unable to clear RISCV_PCREL_HI20 reloc"
   3140 	      "for cooresponding RISCV_PCREL_LO12 reloc"),
   3141 	    abfd, sec, rel->r_offset);
   3142       }
   3143       break;
   3144 
   3145     case R_RISCV_PCREL_HI20:
   3146       /* Mergeable symbols and code might later move out of range.  */
   3147       if (sym_sec->flags & (SEC_MERGE | SEC_CODE))
   3148 	return TRUE;
   3149 
   3150       /* If the cooresponding lo relocation has already been seen then it's not
   3151        * safe to relax this relocation.  */
   3152       if (riscv_find_pcgp_lo_reloc (pcgp_relocs, rel->r_offset))
   3153 	return TRUE;
   3154 
   3155       break;
   3156 
   3157     default:
   3158       abort ();
   3159     }
   3160 
   3161   if (gp)
   3162     {
   3163       /* If gp and the symbol are in the same output section, then
   3164 	 consider only that section's alignment.  */
   3165       struct bfd_link_hash_entry *h =
   3166 	bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, FALSE, FALSE, TRUE);
   3167       if (h->u.def.section->output_section == sym_sec->output_section)
   3168 	max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
   3169     }
   3170 
   3171   /* Is the reference in range of x0 or gp?
   3172      Valid gp range conservatively because of alignment issue.  */
   3173   if (VALID_ITYPE_IMM (symval)
   3174       || (symval >= gp
   3175 	  && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
   3176       || (symval < gp
   3177 	  && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size)))
   3178     {
   3179       unsigned sym = hi_reloc.hi_sym;
   3180       switch (ELFNN_R_TYPE (rel->r_info))
   3181 	{
   3182 	case R_RISCV_PCREL_LO12_I:
   3183 	  rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
   3184 	  rel->r_addend += hi_reloc.hi_addend;
   3185 	  return riscv_delete_pcgp_lo_reloc (pcgp_relocs, rel->r_offset, 4);
   3186 
   3187 	case R_RISCV_PCREL_LO12_S:
   3188 	  rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
   3189 	  rel->r_addend += hi_reloc.hi_addend;
   3190 	  return riscv_delete_pcgp_lo_reloc (pcgp_relocs, rel->r_offset, 4);
   3191 
   3192 	case R_RISCV_PCREL_HI20:
   3193 	  riscv_record_pcgp_hi_reloc (pcgp_relocs,
   3194 				      rel->r_offset,
   3195 				      rel->r_addend,
   3196 				      symval,
   3197 				      ELFNN_R_SYM(rel->r_info),
   3198 				      sym_sec);
   3199 	  /* We can delete the unnecessary AUIPC and reloc.  */
   3200 	  rel->r_info = ELFNN_R_INFO (0, R_RISCV_DELETE);
   3201 	  rel->r_addend = 4;
   3202 	  return riscv_delete_pcgp_hi_reloc (pcgp_relocs, rel->r_offset);
   3203 
   3204 	default:
   3205 	  abort ();
   3206 	}
   3207     }
   3208 
   3209   return TRUE;
   3210 }
   3211 
   3212 /* Relax PC-relative references to GP-relative references.  */
   3213 
   3214 static bfd_boolean
   3215 _bfd_riscv_relax_delete (bfd *abfd,
   3216 			 asection *sec,
   3217 			 asection *sym_sec ATTRIBUTE_UNUSED,
   3218 			 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   3219 			 Elf_Internal_Rela *rel,
   3220 			 bfd_vma symval ATTRIBUTE_UNUSED,
   3221 			 bfd_vma max_alignment ATTRIBUTE_UNUSED,
   3222 			 bfd_vma reserve_size ATTRIBUTE_UNUSED,
   3223 			 bfd_boolean *again ATTRIBUTE_UNUSED,
   3224 			 riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED)
   3225 {
   3226   if (!riscv_relax_delete_bytes(abfd, sec, rel->r_offset, rel->r_addend))
   3227     return FALSE;
   3228   rel->r_info = ELFNN_R_INFO(0, R_RISCV_NONE);
   3229   return TRUE;
   3230 }
   3231 
   3232 /* Relax a section.  Pass 0 shortens code sequences unless disabled.  Pass 1
   3233    deletes the bytes that pass 0 made obselete.  Pass 2, which cannot be
   3234    disabled, handles code alignment directives.  */
   3235 
   3236 static bfd_boolean
   3237 _bfd_riscv_relax_section (bfd *abfd, asection *sec,
   3238 			  struct bfd_link_info *info,
   3239 			  bfd_boolean *again)
   3240 {
   3241   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
   3242   struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
   3243   struct bfd_elf_section_data *data = elf_section_data (sec);
   3244   Elf_Internal_Rela *relocs;
   3245   bfd_boolean ret = FALSE;
   3246   unsigned int i;
   3247   bfd_vma max_alignment, reserve_size = 0;
   3248   riscv_pcgp_relocs pcgp_relocs;
   3249 
   3250   *again = FALSE;
   3251 
   3252   if (bfd_link_relocatable (info)
   3253       || sec->sec_flg0
   3254       || (sec->flags & SEC_RELOC) == 0
   3255       || sec->reloc_count == 0
   3256       || (info->disable_target_specific_optimizations
   3257 	  && info->relax_pass == 0))
   3258     return TRUE;
   3259 
   3260   riscv_init_pcgp_relocs (&pcgp_relocs);
   3261 
   3262   /* Read this BFD's relocs if we haven't done so already.  */
   3263   if (data->relocs)
   3264     relocs = data->relocs;
   3265   else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   3266 						 info->keep_memory)))
   3267     goto fail;
   3268 
   3269   if (htab)
   3270     {
   3271       max_alignment = htab->max_alignment;
   3272       if (max_alignment == (bfd_vma) -1)
   3273 	{
   3274 	  max_alignment = _bfd_riscv_get_max_alignment (sec);
   3275 	  htab->max_alignment = max_alignment;
   3276 	}
   3277     }
   3278   else
   3279     max_alignment = _bfd_riscv_get_max_alignment (sec);
   3280 
   3281   /* Examine and consider relaxing each reloc.  */
   3282   for (i = 0; i < sec->reloc_count; i++)
   3283     {
   3284       asection *sym_sec;
   3285       Elf_Internal_Rela *rel = relocs + i;
   3286       relax_func_t relax_func;
   3287       int type = ELFNN_R_TYPE (rel->r_info);
   3288       bfd_vma symval;
   3289 
   3290       relax_func = NULL;
   3291       if (info->relax_pass == 0)
   3292 	{
   3293 	  if (type == R_RISCV_CALL || type == R_RISCV_CALL_PLT)
   3294 	    relax_func = _bfd_riscv_relax_call;
   3295 	  else if (type == R_RISCV_HI20
   3296 		   || type == R_RISCV_LO12_I
   3297 		   || type == R_RISCV_LO12_S)
   3298 	    relax_func = _bfd_riscv_relax_lui;
   3299 	  else if (!bfd_link_pic(info)
   3300 		   && (type == R_RISCV_PCREL_HI20
   3301 		   || type == R_RISCV_PCREL_LO12_I
   3302 		   || type == R_RISCV_PCREL_LO12_S))
   3303 	    relax_func = _bfd_riscv_relax_pc;
   3304 	  else if (type == R_RISCV_TPREL_HI20
   3305 		   || type == R_RISCV_TPREL_ADD
   3306 		   || type == R_RISCV_TPREL_LO12_I
   3307 		   || type == R_RISCV_TPREL_LO12_S)
   3308 	    relax_func = _bfd_riscv_relax_tls_le;
   3309 	  else
   3310 	    continue;
   3311 
   3312 	  /* Only relax this reloc if it is paired with R_RISCV_RELAX.  */
   3313 	  if (i == sec->reloc_count - 1
   3314 	      || ELFNN_R_TYPE ((rel + 1)->r_info) != R_RISCV_RELAX
   3315 	      || rel->r_offset != (rel + 1)->r_offset)
   3316 	    continue;
   3317 
   3318 	  /* Skip over the R_RISCV_RELAX.  */
   3319 	  i++;
   3320 	}
   3321       else if (info->relax_pass == 1 && type == R_RISCV_DELETE)
   3322 	relax_func = _bfd_riscv_relax_delete;
   3323       else if (info->relax_pass == 2 && type == R_RISCV_ALIGN)
   3324 	relax_func = _bfd_riscv_relax_align;
   3325       else
   3326 	continue;
   3327 
   3328       data->relocs = relocs;
   3329 
   3330       /* Read this BFD's contents if we haven't done so already.  */
   3331       if (!data->this_hdr.contents
   3332 	  && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
   3333 	goto fail;
   3334 
   3335       /* Read this BFD's symbols if we haven't done so already.  */
   3336       if (symtab_hdr->sh_info != 0
   3337 	  && !symtab_hdr->contents
   3338 	  && !(symtab_hdr->contents =
   3339 	       (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
   3340 						       symtab_hdr->sh_info,
   3341 						       0, NULL, NULL, NULL)))
   3342 	goto fail;
   3343 
   3344       /* Get the value of the symbol referred to by the reloc.  */
   3345       if (ELFNN_R_SYM (rel->r_info) < symtab_hdr->sh_info)
   3346 	{
   3347 	  /* A local symbol.  */
   3348 	  Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents
   3349 				    + ELFNN_R_SYM (rel->r_info));
   3350 	  reserve_size = (isym->st_size - rel->r_addend) > isym->st_size
   3351 	    ? 0 : isym->st_size - rel->r_addend;
   3352 
   3353 	  if (isym->st_shndx == SHN_UNDEF)
   3354 	    sym_sec = sec, symval = sec_addr (sec) + rel->r_offset;
   3355 	  else
   3356 	    {
   3357 	      BFD_ASSERT (isym->st_shndx < elf_numsections (abfd));
   3358 	      sym_sec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
   3359 	      if (sec_addr (sym_sec) == 0)
   3360 		continue;
   3361 	      symval = sec_addr (sym_sec) + isym->st_value;
   3362 	    }
   3363 	}
   3364       else
   3365 	{
   3366 	  unsigned long indx;
   3367 	  struct elf_link_hash_entry *h;
   3368 
   3369 	  indx = ELFNN_R_SYM (rel->r_info) - symtab_hdr->sh_info;
   3370 	  h = elf_sym_hashes (abfd)[indx];
   3371 
   3372 	  while (h->root.type == bfd_link_hash_indirect
   3373 		 || h->root.type == bfd_link_hash_warning)
   3374 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3375 
   3376 	  if (h->plt.offset != MINUS_ONE)
   3377 	    symval = sec_addr (htab->elf.splt) + h->plt.offset;
   3378 	  else if (h->root.u.def.section->output_section == NULL
   3379 		   || (h->root.type != bfd_link_hash_defined
   3380 		       && h->root.type != bfd_link_hash_defweak))
   3381 	    continue;
   3382 	  else
   3383 	    symval = sec_addr (h->root.u.def.section) + h->root.u.def.value;
   3384 
   3385 	  if (h->type != STT_FUNC)
   3386 	    reserve_size =
   3387 	      (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_addend;
   3388 	  sym_sec = h->root.u.def.section;
   3389 	}
   3390 
   3391       symval += rel->r_addend;
   3392 
   3393       if (!relax_func (abfd, sec, sym_sec, info, rel, symval,
   3394 		       max_alignment, reserve_size, again,
   3395 		       &pcgp_relocs))
   3396 	goto fail;
   3397     }
   3398 
   3399   ret = TRUE;
   3400 
   3401 fail:
   3402   if (relocs != data->relocs)
   3403     free (relocs);
   3404   riscv_free_pcgp_relocs(&pcgp_relocs, abfd, sec);
   3405 
   3406   return ret;
   3407 }
   3408 
   3409 #if ARCH_SIZE == 32
   3410 # define PRSTATUS_SIZE			0 /* FIXME */
   3411 # define PRSTATUS_OFFSET_PR_CURSIG	12
   3412 # define PRSTATUS_OFFSET_PR_PID		24
   3413 # define PRSTATUS_OFFSET_PR_REG		72
   3414 # define ELF_GREGSET_T_SIZE		128
   3415 # define PRPSINFO_SIZE			128
   3416 # define PRPSINFO_OFFSET_PR_PID		16
   3417 # define PRPSINFO_OFFSET_PR_FNAME	32
   3418 # define PRPSINFO_OFFSET_PR_PSARGS	48
   3419 #else
   3420 # define PRSTATUS_SIZE			376
   3421 # define PRSTATUS_OFFSET_PR_CURSIG	12
   3422 # define PRSTATUS_OFFSET_PR_PID		32
   3423 # define PRSTATUS_OFFSET_PR_REG		112
   3424 # define ELF_GREGSET_T_SIZE		256
   3425 # define PRPSINFO_SIZE			136
   3426 # define PRPSINFO_OFFSET_PR_PID		24
   3427 # define PRPSINFO_OFFSET_PR_FNAME	40
   3428 # define PRPSINFO_OFFSET_PR_PSARGS	56
   3429 #endif
   3430 
   3431 /* Support for core dump NOTE sections.  */
   3432 
   3433 static bfd_boolean
   3434 riscv_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   3435 {
   3436   switch (note->descsz)
   3437     {
   3438       default:
   3439 	return FALSE;
   3440 
   3441       case PRSTATUS_SIZE:  /* sizeof(struct elf_prstatus) on Linux/RISC-V.  */
   3442 	/* pr_cursig */
   3443 	elf_tdata (abfd)->core->signal
   3444 	  = bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
   3445 
   3446 	/* pr_pid */
   3447 	elf_tdata (abfd)->core->lwpid
   3448 	  = bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
   3449 	break;
   3450     }
   3451 
   3452   /* Make a ".reg/999" section.  */
   3453   return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
   3454 					  note->descpos + PRSTATUS_OFFSET_PR_REG);
   3455 }
   3456 
   3457 static bfd_boolean
   3458 riscv_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   3459 {
   3460   switch (note->descsz)
   3461     {
   3462       default:
   3463 	return FALSE;
   3464 
   3465       case PRPSINFO_SIZE: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V.  */
   3466 	/* pr_pid */
   3467 	elf_tdata (abfd)->core->pid
   3468 	  = bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
   3469 
   3470 	/* pr_fname */
   3471 	elf_tdata (abfd)->core->program = _bfd_elfcore_strndup
   3472 	  (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME, 16);
   3473 
   3474 	/* pr_psargs */
   3475 	elf_tdata (abfd)->core->command = _bfd_elfcore_strndup
   3476 	  (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS, 80);
   3477 	break;
   3478     }
   3479 
   3480   /* Note that for some reason, a spurious space is tacked
   3481      onto the end of the args in some (at least one anyway)
   3482      implementations, so strip it off if it exists.  */
   3483 
   3484   {
   3485     char *command = elf_tdata (abfd)->core->command;
   3486     int n = strlen (command);
   3487 
   3488     if (0 < n && command[n - 1] == ' ')
   3489       command[n - 1] = '\0';
   3490   }
   3491 
   3492   return TRUE;
   3493 }
   3494 
   3495 /* Set the right mach type.  */
   3496 static bfd_boolean
   3497 riscv_elf_object_p (bfd *abfd)
   3498 {
   3499   /* There are only two mach types in RISCV currently.  */
   3500   if (strcmp (abfd->xvec->name, "elf32-littleriscv") == 0)
   3501     bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv32);
   3502   else
   3503     bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv64);
   3504 
   3505   return TRUE;
   3506 }
   3507 
   3508 
   3509 #define TARGET_LITTLE_SYM		riscv_elfNN_vec
   3510 #define TARGET_LITTLE_NAME		"elfNN-littleriscv"
   3511 
   3512 #define elf_backend_reloc_type_class	     riscv_reloc_type_class
   3513 
   3514 #define bfd_elfNN_bfd_reloc_name_lookup	     riscv_reloc_name_lookup
   3515 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
   3516 #define bfd_elfNN_bfd_reloc_type_lookup	     riscv_reloc_type_lookup
   3517 #define bfd_elfNN_bfd_merge_private_bfd_data \
   3518   _bfd_riscv_elf_merge_private_bfd_data
   3519 
   3520 #define elf_backend_copy_indirect_symbol     riscv_elf_copy_indirect_symbol
   3521 #define elf_backend_create_dynamic_sections  riscv_elf_create_dynamic_sections
   3522 #define elf_backend_check_relocs	     riscv_elf_check_relocs
   3523 #define elf_backend_adjust_dynamic_symbol    riscv_elf_adjust_dynamic_symbol
   3524 #define elf_backend_size_dynamic_sections    riscv_elf_size_dynamic_sections
   3525 #define elf_backend_relocate_section	     riscv_elf_relocate_section
   3526 #define elf_backend_finish_dynamic_symbol    riscv_elf_finish_dynamic_symbol
   3527 #define elf_backend_finish_dynamic_sections  riscv_elf_finish_dynamic_sections
   3528 #define elf_backend_gc_mark_hook	     riscv_elf_gc_mark_hook
   3529 #define elf_backend_plt_sym_val		     riscv_elf_plt_sym_val
   3530 #define elf_backend_grok_prstatus	     riscv_elf_grok_prstatus
   3531 #define elf_backend_grok_psinfo		     riscv_elf_grok_psinfo
   3532 #define elf_backend_object_p		     riscv_elf_object_p
   3533 #define elf_info_to_howto_rel		     NULL
   3534 #define elf_info_to_howto		     riscv_info_to_howto_rela
   3535 #define bfd_elfNN_bfd_relax_section	     _bfd_riscv_relax_section
   3536 
   3537 #define elf_backend_init_index_section	     _bfd_elf_init_1_index_section
   3538 
   3539 #define elf_backend_can_gc_sections	1
   3540 #define elf_backend_can_refcount	1
   3541 #define elf_backend_want_got_plt	1
   3542 #define elf_backend_plt_readonly	1
   3543 #define elf_backend_plt_alignment	4
   3544 #define elf_backend_want_plt_sym	1
   3545 #define elf_backend_got_header_size	(ARCH_SIZE / 8)
   3546 #define elf_backend_want_dynrelro	1
   3547 #define elf_backend_rela_normal		1
   3548 #define elf_backend_default_execstack	0
   3549 
   3550 #include "elfNN-target.h"
   3551