1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
5 Based on TILE-Gx and MIPS targets.
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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/>. */
23 /* This file handles RISC-V ELF targets. */
31 #include "elfxx-riscv.h"
32 #include "elf/riscv.h"
33 #include "opcode/riscv.h"
35 /* Internal relocations used exclusively by the relaxation pass. */
36 #define R_RISCV_DELETE (R_RISCV_max + 1)
40 #define MINUS_ONE ((bfd_vma)0 - 1)
42 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
44 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
46 /* The name of the dynamic interpreter. This is put in the .interp
49 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
50 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
52 #define ELF_ARCH bfd_arch_riscv
53 #define ELF_TARGET_ID RISCV_ELF_DATA
54 #define ELF_MACHINE_CODE EM_RISCV
55 #define ELF_MAXPAGESIZE 0x1000
56 #define ELF_COMMONPAGESIZE 0x1000
58 /* RISC-V ELF linker hash entry. */
60 struct riscv_elf_link_hash_entry
62 struct elf_link_hash_entry elf;
64 /* Track dynamic relocs copied for this symbol. */
65 struct elf_dyn_relocs *dyn_relocs;
75 #define riscv_elf_hash_entry(ent) \
76 ((struct riscv_elf_link_hash_entry *)(ent))
78 struct _bfd_riscv_elf_obj_tdata
80 struct elf_obj_tdata root;
82 /* tls_type for each local got entry. */
83 char *local_got_tls_type;
86 #define _bfd_riscv_elf_tdata(abfd) \
87 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
89 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
90 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
92 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
93 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
94 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
96 #define is_riscv_elf(bfd) \
97 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
98 && elf_tdata (bfd) != NULL \
99 && elf_object_id (bfd) == RISCV_ELF_DATA)
102 elfNN_riscv_mkobject (bfd *abfd)
104 return bfd_elf_allocate_object (abfd,
105 sizeof (struct _bfd_riscv_elf_obj_tdata),
109 #include "elf/common.h"
110 #include "elf/internal.h"
112 struct riscv_elf_link_hash_table
114 struct elf_link_hash_table elf;
116 /* Short-cuts to get to dynamic linker sections. */
119 /* Small local sym to section mapping cache. */
120 struct sym_cache sym_cache;
122 /* The max alignment of output sections. */
123 bfd_vma max_alignment;
127 /* Get the RISC-V ELF linker hash table from a link_info structure. */
128 #define riscv_elf_hash_table(p) \
129 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
130 == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
133 riscv_info_to_howto_rela (bfd *abfd,
135 Elf_Internal_Rela *dst)
137 cache_ptr->howto = riscv_elf_rtype_to_howto (abfd, ELFNN_R_TYPE (dst->r_info));
138 return cache_ptr->howto != NULL;
142 riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
144 const struct elf_backend_data *bed;
147 bed = get_elf_backend_data (abfd);
148 loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
149 bed->s->swap_reloca_out (abfd, rel, loc);
154 #define PLT_HEADER_INSNS 8
155 #define PLT_ENTRY_INSNS 4
156 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
157 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
159 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
161 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
163 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
166 riscv_elf_got_plt_val (bfd_vma plt_index, struct bfd_link_info *info)
168 return sec_addr (riscv_elf_hash_table (info)->elf.sgotplt)
169 + GOTPLT_HEADER_SIZE + (plt_index * GOT_ENTRY_SIZE);
173 # define MATCH_LREG MATCH_LW
175 # define MATCH_LREG MATCH_LD
178 /* Generate a PLT header. */
181 riscv_make_plt_header (bfd *output_bfd, bfd_vma gotplt_addr, bfd_vma addr,
184 bfd_vma gotplt_offset_high = RISCV_PCREL_HIGH_PART (gotplt_addr, addr);
185 bfd_vma gotplt_offset_low = RISCV_PCREL_LOW_PART (gotplt_addr, addr);
187 /* RVE has no t3 register, so this won't work, and is not supported. */
188 if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
190 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
195 /* auipc t2, %hi(.got.plt)
196 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
197 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
198 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
199 addi t0, t2, %lo(.got.plt) # &.got.plt
200 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
201 l[w|d] t0, PTRSIZE(t0) # link map
204 entry[0] = RISCV_UTYPE (AUIPC, X_T2, gotplt_offset_high);
205 entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T3);
206 entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, gotplt_offset_low);
207 entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, -(PLT_HEADER_SIZE + 12));
208 entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, gotplt_offset_low);
209 entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
210 entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
211 entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
216 /* Generate a PLT entry. */
219 riscv_make_plt_entry (bfd *output_bfd, bfd_vma got, bfd_vma addr,
222 /* RVE has no t3 register, so this won't work, and is not supported. */
223 if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
225 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
230 /* auipc t3, %hi(.got.plt entry)
231 l[w|d] t3, %lo(.got.plt entry)(t3)
235 entry[0] = RISCV_UTYPE (AUIPC, X_T3, RISCV_PCREL_HIGH_PART (got, addr));
236 entry[1] = RISCV_ITYPE (LREG, X_T3, X_T3, RISCV_PCREL_LOW_PART (got, addr));
237 entry[2] = RISCV_ITYPE (JALR, X_T1, X_T3, 0);
238 entry[3] = RISCV_NOP;
243 /* Create an entry in an RISC-V ELF linker hash table. */
245 static struct bfd_hash_entry *
246 link_hash_newfunc (struct bfd_hash_entry *entry,
247 struct bfd_hash_table *table, const char *string)
249 /* Allocate the structure if it has not already been allocated by a
254 bfd_hash_allocate (table,
255 sizeof (struct riscv_elf_link_hash_entry));
260 /* Call the allocation method of the superclass. */
261 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
264 struct riscv_elf_link_hash_entry *eh;
266 eh = (struct riscv_elf_link_hash_entry *) entry;
267 eh->dyn_relocs = NULL;
268 eh->tls_type = GOT_UNKNOWN;
274 /* Create a RISC-V ELF linker hash table. */
276 static struct bfd_link_hash_table *
277 riscv_elf_link_hash_table_create (bfd *abfd)
279 struct riscv_elf_link_hash_table *ret;
280 size_t amt = sizeof (struct riscv_elf_link_hash_table);
282 ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
286 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
287 sizeof (struct riscv_elf_link_hash_entry),
294 ret->max_alignment = (bfd_vma) -1;
295 return &ret->elf.root;
298 /* Create the .got section. */
301 riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
305 struct elf_link_hash_entry *h;
306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
307 struct elf_link_hash_table *htab = elf_hash_table (info);
309 /* This function may be called more than once. */
310 if (htab->sgot != NULL)
313 flags = bed->dynamic_sec_flags;
315 s = bfd_make_section_anyway_with_flags (abfd,
316 (bed->rela_plts_and_copies_p
317 ? ".rela.got" : ".rel.got"),
318 (bed->dynamic_sec_flags
321 || !bfd_set_section_alignment (s, bed->s->log_file_align))
325 s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
327 || !bfd_set_section_alignment (s, bed->s->log_file_align))
331 /* The first bit of the global offset table is the header. */
332 s->size += bed->got_header_size;
334 if (bed->want_got_plt)
336 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
338 || !bfd_set_section_alignment (s, bed->s->log_file_align))
342 /* Reserve room for the header. */
343 s->size += GOTPLT_HEADER_SIZE;
346 if (bed->want_got_sym)
348 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
349 section. We don't do this in the linker script because we don't want
350 to define the symbol if we are not creating a global offset
352 h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
353 "_GLOBAL_OFFSET_TABLE_");
354 elf_hash_table (info)->hgot = h;
362 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
363 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
367 riscv_elf_create_dynamic_sections (bfd *dynobj,
368 struct bfd_link_info *info)
370 struct riscv_elf_link_hash_table *htab;
372 htab = riscv_elf_hash_table (info);
373 BFD_ASSERT (htab != NULL);
375 if (!riscv_elf_create_got_section (dynobj, info))
378 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
381 if (!bfd_link_pic (info))
383 /* Technically, this section doesn't have contents. It is used as the
384 target of TLS copy relocs, to copy TLS data from shared libraries into
385 the executable. However, if we don't mark it as loadable, then it
386 matches the IS_TBSS test in ldlang.c, and there is no run-time address
387 space allocated for it even though it has SEC_ALLOC. That test is
388 correct for .tbss, but not correct for this section. There is also
389 a second problem that having a section with no contents can only work
390 if it comes after all sections with contents in the same segment,
391 but the linker script does not guarantee that. This is just mixed in
392 with other .tdata.* sections. We can fix both problems by lying and
393 saying that there are contents. This section is expected to be small
394 so this should not cause a significant extra program startup cost. */
396 bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
397 (SEC_ALLOC | SEC_THREAD_LOCAL
398 | SEC_LOAD | SEC_DATA
400 | SEC_LINKER_CREATED));
403 if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
404 || (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
410 /* Copy the extra info we tack onto an elf_link_hash_entry. */
413 riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
414 struct elf_link_hash_entry *dir,
415 struct elf_link_hash_entry *ind)
417 struct riscv_elf_link_hash_entry *edir, *eind;
419 edir = (struct riscv_elf_link_hash_entry *) dir;
420 eind = (struct riscv_elf_link_hash_entry *) ind;
422 if (eind->dyn_relocs != NULL)
424 if (edir->dyn_relocs != NULL)
426 struct elf_dyn_relocs **pp;
427 struct elf_dyn_relocs *p;
429 /* Add reloc counts against the indirect sym to the direct sym
430 list. Merge any entries against the same section. */
431 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
433 struct elf_dyn_relocs *q;
435 for (q = edir->dyn_relocs; q != NULL; q = q->next)
436 if (q->sec == p->sec)
438 q->pc_count += p->pc_count;
439 q->count += p->count;
446 *pp = edir->dyn_relocs;
449 edir->dyn_relocs = eind->dyn_relocs;
450 eind->dyn_relocs = NULL;
453 if (ind->root.type == bfd_link_hash_indirect
454 && dir->got.refcount <= 0)
456 edir->tls_type = eind->tls_type;
457 eind->tls_type = GOT_UNKNOWN;
459 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
463 riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
464 unsigned long symndx, char tls_type)
466 char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
468 *new_tls_type |= tls_type;
469 if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
471 (*_bfd_error_handler)
472 (_("%pB: `%s' accessed both as normal and thread local symbol"),
473 abfd, h ? h->root.root.string : "<local>");
480 riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
481 struct elf_link_hash_entry *h, long symndx)
483 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
484 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
486 if (htab->elf.sgot == NULL)
488 if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
494 h->got.refcount += 1;
498 /* This is a global offset table entry for a local symbol. */
499 if (elf_local_got_refcounts (abfd) == NULL)
501 bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
502 if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
504 _bfd_riscv_elf_local_got_tls_type (abfd)
505 = (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
507 elf_local_got_refcounts (abfd) [symndx] += 1;
513 bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
515 reloc_howto_type * r = riscv_elf_rtype_to_howto (abfd, r_type);
517 (*_bfd_error_handler)
518 (_("%pB: relocation %s against `%s' can not be used when making a shared "
519 "object; recompile with -fPIC"),
520 abfd, r ? r->name : _("<unknown>"),
521 h != NULL ? h->root.root.string : "a local symbol");
522 bfd_set_error (bfd_error_bad_value);
525 /* Look through the relocs for a section during the first phase, and
526 allocate space in the global offset table or procedure linkage
530 riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
531 asection *sec, const Elf_Internal_Rela *relocs)
533 struct riscv_elf_link_hash_table *htab;
534 Elf_Internal_Shdr *symtab_hdr;
535 struct elf_link_hash_entry **sym_hashes;
536 const Elf_Internal_Rela *rel;
537 asection *sreloc = NULL;
539 if (bfd_link_relocatable (info))
542 htab = riscv_elf_hash_table (info);
543 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
544 sym_hashes = elf_sym_hashes (abfd);
546 if (htab->elf.dynobj == NULL)
547 htab->elf.dynobj = abfd;
549 for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
552 unsigned int r_symndx;
553 struct elf_link_hash_entry *h;
555 r_symndx = ELFNN_R_SYM (rel->r_info);
556 r_type = ELFNN_R_TYPE (rel->r_info);
558 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
560 (*_bfd_error_handler) (_("%pB: bad symbol index: %d"),
565 if (r_symndx < symtab_hdr->sh_info)
569 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
570 while (h->root.type == bfd_link_hash_indirect
571 || h->root.type == bfd_link_hash_warning)
572 h = (struct elf_link_hash_entry *) h->root.u.i.link;
577 case R_RISCV_TLS_GD_HI20:
578 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
579 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
583 case R_RISCV_TLS_GOT_HI20:
584 if (bfd_link_pic (info))
585 info->flags |= DF_STATIC_TLS;
586 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
587 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
591 case R_RISCV_GOT_HI20:
592 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
593 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
597 case R_RISCV_CALL_PLT:
598 /* This symbol requires a procedure linkage table entry. We
599 actually build the entry in adjust_dynamic_symbol,
600 because this might be a case of linking PIC code without
601 linking in any dynamic objects, in which case we don't
602 need to generate a procedure linkage table after all. */
607 h->plt.refcount += 1;
614 case R_RISCV_RVC_BRANCH:
615 case R_RISCV_RVC_JUMP:
616 case R_RISCV_PCREL_HI20:
617 /* In shared libraries, these relocs are known to bind locally. */
618 if (bfd_link_pic (info))
622 case R_RISCV_TPREL_HI20:
623 if (!bfd_link_executable (info))
624 return bad_static_reloc (abfd, r_type, h);
626 riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
630 if (bfd_link_pic (info))
631 return bad_static_reloc (abfd, r_type, h);
635 case R_RISCV_JUMP_SLOT:
636 case R_RISCV_RELATIVE:
642 /* This reloc might not bind locally. */
646 if (h != NULL && !bfd_link_pic (info))
648 /* We may need a .plt entry if the function this reloc
649 refers to is in a shared lib. */
650 h->plt.refcount += 1;
653 /* If we are creating a shared library, and this is a reloc
654 against a global symbol, or a non PC relative reloc
655 against a local symbol, then we need to copy the reloc
656 into the shared library. However, if we are linking with
657 -Bsymbolic, we do not need to copy a reloc against a
658 global symbol which is defined in an object we are
659 including in the link (i.e., DEF_REGULAR is set). At
660 this point we have not seen all the input files, so it is
661 possible that DEF_REGULAR is not set now but will be set
662 later (it is never cleared). In case of a weak definition,
663 DEF_REGULAR may be cleared later by a strong definition in
664 a shared library. We account for that possibility below by
665 storing information in the relocs_copied field of the hash
666 table entry. A similar situation occurs when creating
667 shared libraries and symbol visibility changes render the
670 If on the other hand, we are creating an executable, we
671 may need to keep relocations for symbols satisfied by a
672 dynamic library if we manage to avoid copy relocs for the
674 reloc_howto_type * r = riscv_elf_rtype_to_howto (abfd, r_type);
676 if ((bfd_link_pic (info)
677 && (sec->flags & SEC_ALLOC) != 0
678 && ((r != NULL && ! r->pc_relative)
681 || h->root.type == bfd_link_hash_defweak
682 || !h->def_regular))))
683 || (!bfd_link_pic (info)
684 && (sec->flags & SEC_ALLOC) != 0
686 && (h->root.type == bfd_link_hash_defweak
687 || !h->def_regular)))
689 struct elf_dyn_relocs *p;
690 struct elf_dyn_relocs **head;
692 /* When creating a shared object, we must copy these
693 relocs into the output file. We create a reloc
694 section in dynobj and make room for the reloc. */
697 sreloc = _bfd_elf_make_dynamic_reloc_section
698 (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES,
699 abfd, /*rela?*/ TRUE);
705 /* If this is a global symbol, we count the number of
706 relocations we need for this symbol. */
708 head = &((struct riscv_elf_link_hash_entry *) h)->dyn_relocs;
711 /* Track dynamic relocs needed for local syms too.
712 We really need local syms available to do this
717 Elf_Internal_Sym *isym;
719 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
724 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
728 vpp = &elf_section_data (s)->local_dynrel;
729 head = (struct elf_dyn_relocs **) vpp;
733 if (p == NULL || p->sec != sec)
735 size_t amt = sizeof *p;
736 p = ((struct elf_dyn_relocs *)
737 bfd_alloc (htab->elf.dynobj, amt));
748 p->pc_count += r == NULL ? 0 : r->pc_relative;
753 case R_RISCV_GNU_VTINHERIT:
754 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
758 case R_RISCV_GNU_VTENTRY:
759 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
772 riscv_elf_gc_mark_hook (asection *sec,
773 struct bfd_link_info *info,
774 Elf_Internal_Rela *rel,
775 struct elf_link_hash_entry *h,
776 Elf_Internal_Sym *sym)
779 switch (ELFNN_R_TYPE (rel->r_info))
781 case R_RISCV_GNU_VTINHERIT:
782 case R_RISCV_GNU_VTENTRY:
786 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
789 /* Find dynamic relocs for H that apply to read-only sections. */
792 readonly_dynrelocs (struct elf_link_hash_entry *h)
794 struct elf_dyn_relocs *p;
796 for (p = riscv_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
798 asection *s = p->sec->output_section;
800 if (s != NULL && (s->flags & SEC_READONLY) != 0)
806 /* Adjust a symbol defined by a dynamic object and referenced by a
807 regular object. The current definition is in some section of the
808 dynamic object, but we're not including those sections. We have to
809 change the definition to something the rest of the link can
813 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
814 struct elf_link_hash_entry *h)
816 struct riscv_elf_link_hash_table *htab;
817 struct riscv_elf_link_hash_entry * eh;
821 htab = riscv_elf_hash_table (info);
822 BFD_ASSERT (htab != NULL);
824 dynobj = htab->elf.dynobj;
826 /* Make sure we know what is going on here. */
827 BFD_ASSERT (dynobj != NULL
829 || h->type == STT_GNU_IFUNC
833 && !h->def_regular)));
835 /* If this is a function, put it in the procedure linkage table. We
836 will fill in the contents of the procedure linkage table later
837 (although we could actually do it here). */
838 if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
840 if (h->plt.refcount <= 0
841 || SYMBOL_CALLS_LOCAL (info, h)
842 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
843 && h->root.type == bfd_link_hash_undefweak))
845 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
846 input file, but the symbol was never referred to by a dynamic
847 object, or if all references were garbage collected. In such
848 a case, we don't actually need to build a PLT entry. */
849 h->plt.offset = (bfd_vma) -1;
856 h->plt.offset = (bfd_vma) -1;
858 /* If this is a weak symbol, and there is a real definition, the
859 processor independent code will have arranged for us to see the
860 real definition first, and we can just use the same value. */
863 struct elf_link_hash_entry *def = weakdef (h);
864 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
865 h->root.u.def.section = def->root.u.def.section;
866 h->root.u.def.value = def->root.u.def.value;
870 /* This is a reference to a symbol defined by a dynamic object which
871 is not a function. */
873 /* If we are creating a shared library, we must presume that the
874 only references to the symbol are via the global offset table.
875 For such cases we need not do anything here; the relocations will
876 be handled correctly by relocate_section. */
877 if (bfd_link_pic (info))
880 /* If there are no references to this symbol that do not use the
881 GOT, we don't need to generate a copy reloc. */
885 /* If -z nocopyreloc was given, we won't generate them either. */
886 if (info->nocopyreloc)
892 /* If we don't find any dynamic relocs in read-only sections, then
893 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
894 if (!readonly_dynrelocs (h))
900 /* We must allocate the symbol in our .dynbss section, which will
901 become part of the .bss section of the executable. There will be
902 an entry for this symbol in the .dynsym section. The dynamic
903 object will contain position independent code, so all references
904 from the dynamic object to this symbol will go through the global
905 offset table. The dynamic linker will use the .dynsym entry to
906 determine the address it must put in the global offset table, so
907 both the dynamic object and the regular object will refer to the
908 same memory location for the variable. */
910 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
911 to copy the initial value out of the dynamic object and into the
912 runtime process image. We need to remember the offset into the
913 .rel.bss section we are going to use. */
914 eh = (struct riscv_elf_link_hash_entry *) h;
915 if (eh->tls_type & ~GOT_NORMAL)
918 srel = htab->elf.srelbss;
920 else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
922 s = htab->elf.sdynrelro;
923 srel = htab->elf.sreldynrelro;
927 s = htab->elf.sdynbss;
928 srel = htab->elf.srelbss;
930 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
932 srel->size += sizeof (ElfNN_External_Rela);
936 return _bfd_elf_adjust_dynamic_copy (info, h, s);
939 /* Allocate space in .plt, .got and associated reloc sections for
943 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
945 struct bfd_link_info *info;
946 struct riscv_elf_link_hash_table *htab;
947 struct riscv_elf_link_hash_entry *eh;
948 struct elf_dyn_relocs *p;
950 if (h->root.type == bfd_link_hash_indirect)
953 info = (struct bfd_link_info *) inf;
954 htab = riscv_elf_hash_table (info);
955 BFD_ASSERT (htab != NULL);
957 if (htab->elf.dynamic_sections_created
958 && h->plt.refcount > 0)
960 /* Make sure this symbol is output as a dynamic symbol.
961 Undefined weak syms won't yet be marked as dynamic. */
965 if (! bfd_elf_link_record_dynamic_symbol (info, h))
969 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
971 asection *s = htab->elf.splt;
974 s->size = PLT_HEADER_SIZE;
976 h->plt.offset = s->size;
978 /* Make room for this entry. */
979 s->size += PLT_ENTRY_SIZE;
981 /* We also need to make an entry in the .got.plt section. */
982 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
984 /* We also need to make an entry in the .rela.plt section. */
985 htab->elf.srelplt->size += sizeof (ElfNN_External_Rela);
987 /* If this symbol is not defined in a regular file, and we are
988 not generating a shared library, then set the symbol to this
989 location in the .plt. This is required to make function
990 pointers compare as equal between the normal executable and
991 the shared library. */
992 if (! bfd_link_pic (info)
995 h->root.u.def.section = s;
996 h->root.u.def.value = h->plt.offset;
1001 h->plt.offset = (bfd_vma) -1;
1007 h->plt.offset = (bfd_vma) -1;
1011 if (h->got.refcount > 0)
1015 int tls_type = riscv_elf_hash_entry (h)->tls_type;
1017 /* Make sure this symbol is output as a dynamic symbol.
1018 Undefined weak syms won't yet be marked as dynamic. */
1019 if (h->dynindx == -1
1020 && !h->forced_local)
1022 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1027 h->got.offset = s->size;
1028 dyn = htab->elf.dynamic_sections_created;
1029 if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
1031 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1032 if (tls_type & GOT_TLS_GD)
1034 s->size += 2 * RISCV_ELF_WORD_BYTES;
1035 htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela);
1038 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1039 if (tls_type & GOT_TLS_IE)
1041 s->size += RISCV_ELF_WORD_BYTES;
1042 htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1047 s->size += RISCV_ELF_WORD_BYTES;
1048 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
1049 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1050 htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1054 h->got.offset = (bfd_vma) -1;
1056 eh = (struct riscv_elf_link_hash_entry *) h;
1057 if (eh->dyn_relocs == NULL)
1060 /* In the shared -Bsymbolic case, discard space allocated for
1061 dynamic pc-relative relocs against symbols which turn out to be
1062 defined in regular objects. For the normal shared case, discard
1063 space for pc-relative relocs that have become local due to symbol
1064 visibility changes. */
1066 if (bfd_link_pic (info))
1068 if (SYMBOL_CALLS_LOCAL (info, h))
1070 struct elf_dyn_relocs **pp;
1072 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1074 p->count -= p->pc_count;
1083 /* Also discard relocs on undefined weak syms with non-default
1085 if (eh->dyn_relocs != NULL
1086 && h->root.type == bfd_link_hash_undefweak)
1088 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1089 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1090 eh->dyn_relocs = NULL;
1092 /* Make sure undefined weak symbols are output as a dynamic
1094 else if (h->dynindx == -1
1095 && !h->forced_local)
1097 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1104 /* For the non-shared case, discard space for relocs against
1105 symbols which turn out to need copy relocs or are not
1111 || (htab->elf.dynamic_sections_created
1112 && (h->root.type == bfd_link_hash_undefweak
1113 || h->root.type == bfd_link_hash_undefined))))
1115 /* Make sure this symbol is output as a dynamic symbol.
1116 Undefined weak syms won't yet be marked as dynamic. */
1117 if (h->dynindx == -1
1118 && !h->forced_local)
1120 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1124 /* If that succeeded, we know we'll be keeping all the
1126 if (h->dynindx != -1)
1130 eh->dyn_relocs = NULL;
1135 /* Finally, allocate space. */
1136 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1138 asection *sreloc = elf_section_data (p->sec)->sreloc;
1139 sreloc->size += p->count * sizeof (ElfNN_External_Rela);
1145 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
1146 read-only sections. */
1149 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
1153 if (h->root.type == bfd_link_hash_indirect)
1156 sec = readonly_dynrelocs (h);
1159 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
1161 info->flags |= DF_TEXTREL;
1162 info->callbacks->minfo
1163 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
1164 sec->owner, h->root.root.string, sec);
1166 /* Not an error, just cut short the traversal. */
1173 riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
1175 struct riscv_elf_link_hash_table *htab;
1180 htab = riscv_elf_hash_table (info);
1181 BFD_ASSERT (htab != NULL);
1182 dynobj = htab->elf.dynobj;
1183 BFD_ASSERT (dynobj != NULL);
1185 if (elf_hash_table (info)->dynamic_sections_created)
1187 /* Set the contents of the .interp section to the interpreter. */
1188 if (bfd_link_executable (info) && !info->nointerp)
1190 s = bfd_get_linker_section (dynobj, ".interp");
1191 BFD_ASSERT (s != NULL);
1192 s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1;
1193 s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER;
1197 /* Set up .got offsets for local syms, and space for local dynamic
1199 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1201 bfd_signed_vma *local_got;
1202 bfd_signed_vma *end_local_got;
1203 char *local_tls_type;
1204 bfd_size_type locsymcount;
1205 Elf_Internal_Shdr *symtab_hdr;
1208 if (! is_riscv_elf (ibfd))
1211 for (s = ibfd->sections; s != NULL; s = s->next)
1213 struct elf_dyn_relocs *p;
1215 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
1217 if (!bfd_is_abs_section (p->sec)
1218 && bfd_is_abs_section (p->sec->output_section))
1220 /* Input section has been discarded, either because
1221 it is a copy of a linkonce section or due to
1222 linker script /DISCARD/, so we'll be discarding
1225 else if (p->count != 0)
1227 srel = elf_section_data (p->sec)->sreloc;
1228 srel->size += p->count * sizeof (ElfNN_External_Rela);
1229 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1230 info->flags |= DF_TEXTREL;
1235 local_got = elf_local_got_refcounts (ibfd);
1239 symtab_hdr = &elf_symtab_hdr (ibfd);
1240 locsymcount = symtab_hdr->sh_info;
1241 end_local_got = local_got + locsymcount;
1242 local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd);
1244 srel = htab->elf.srelgot;
1245 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1249 *local_got = s->size;
1250 s->size += RISCV_ELF_WORD_BYTES;
1251 if (*local_tls_type & GOT_TLS_GD)
1252 s->size += RISCV_ELF_WORD_BYTES;
1253 if (bfd_link_pic (info)
1254 || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
1255 srel->size += sizeof (ElfNN_External_Rela);
1258 *local_got = (bfd_vma) -1;
1262 /* Allocate global sym .plt and .got entries, and space for global
1263 sym dynamic relocs. */
1264 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
1266 if (htab->elf.sgotplt)
1268 struct elf_link_hash_entry *got;
1269 got = elf_link_hash_lookup (elf_hash_table (info),
1270 "_GLOBAL_OFFSET_TABLE_",
1271 FALSE, FALSE, FALSE);
1273 /* Don't allocate .got.plt section if there are no GOT nor PLT
1274 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1276 || !got->ref_regular_nonweak)
1277 && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE)
1278 && (htab->elf.splt == NULL
1279 || htab->elf.splt->size == 0)
1280 && (htab->elf.sgot == NULL
1281 || (htab->elf.sgot->size
1282 == get_elf_backend_data (output_bfd)->got_header_size)))
1283 htab->elf.sgotplt->size = 0;
1286 /* The check_relocs and adjust_dynamic_symbol entry points have
1287 determined the sizes of the various dynamic sections. Allocate
1289 for (s = dynobj->sections; s != NULL; s = s->next)
1291 if ((s->flags & SEC_LINKER_CREATED) == 0)
1294 if (s == htab->elf.splt
1295 || s == htab->elf.sgot
1296 || s == htab->elf.sgotplt
1297 || s == htab->elf.sdynbss
1298 || s == htab->elf.sdynrelro
1299 || s == htab->sdyntdata)
1301 /* Strip this section if we don't need it; see the
1304 else if (strncmp (s->name, ".rela", 5) == 0)
1308 /* We use the reloc_count field as a counter if we need
1309 to copy relocs into the output file. */
1315 /* It's not one of our sections. */
1321 /* If we don't need this section, strip it from the
1322 output file. This is mostly to handle .rela.bss and
1323 .rela.plt. We must create both sections in
1324 create_dynamic_sections, because they must be created
1325 before the linker maps input sections to output
1326 sections. The linker does that before
1327 adjust_dynamic_symbol is called, and it is that
1328 function which decides whether anything needs to go
1329 into these sections. */
1330 s->flags |= SEC_EXCLUDE;
1334 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1337 /* Allocate memory for the section contents. Zero the memory
1338 for the benefit of .rela.plt, which has 4 unused entries
1339 at the beginning, and we don't want garbage. */
1340 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1341 if (s->contents == NULL)
1345 if (elf_hash_table (info)->dynamic_sections_created)
1347 /* Add some entries to the .dynamic section. We fill in the
1348 values later, in riscv_elf_finish_dynamic_sections, but we
1349 must add the entries now so that we get the correct size for
1350 the .dynamic section. The DT_DEBUG entry is filled in by the
1351 dynamic linker and used by the debugger. */
1352 #define add_dynamic_entry(TAG, VAL) \
1353 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1355 if (bfd_link_executable (info))
1357 if (!add_dynamic_entry (DT_DEBUG, 0))
1361 if (htab->elf.srelplt->size != 0)
1363 if (!add_dynamic_entry (DT_PLTGOT, 0)
1364 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1365 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1366 || !add_dynamic_entry (DT_JMPREL, 0))
1370 if (!add_dynamic_entry (DT_RELA, 0)
1371 || !add_dynamic_entry (DT_RELASZ, 0)
1372 || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
1375 /* If any dynamic relocs apply to a read-only section,
1376 then we need a DT_TEXTREL entry. */
1377 if ((info->flags & DF_TEXTREL) == 0)
1378 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
1380 if (info->flags & DF_TEXTREL)
1382 if (!add_dynamic_entry (DT_TEXTREL, 0))
1386 #undef add_dynamic_entry
1392 #define DTP_OFFSET 0x800
1394 /* Return the relocation value for a TLS dtp-relative reloc. */
1397 dtpoff (struct bfd_link_info *info, bfd_vma address)
1399 /* If tls_sec is NULL, we should have signalled an error already. */
1400 if (elf_hash_table (info)->tls_sec == NULL)
1402 return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
1405 /* Return the relocation value for a static TLS tp-relative relocation. */
1408 tpoff (struct bfd_link_info *info, bfd_vma address)
1410 /* If tls_sec is NULL, we should have signalled an error already. */
1411 if (elf_hash_table (info)->tls_sec == NULL)
1413 return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
1416 /* Return the global pointer's value, or 0 if it is not in use. */
1419 riscv_global_pointer_value (struct bfd_link_info *info)
1421 struct bfd_link_hash_entry *h;
1423 h = bfd_link_hash_lookup (info->hash, RISCV_GP_SYMBOL, FALSE, FALSE, TRUE);
1424 if (h == NULL || h->type != bfd_link_hash_defined)
1427 return h->u.def.value + sec_addr (h->u.def.section);
1430 /* Emplace a static relocation. */
1432 static bfd_reloc_status_type
1433 perform_relocation (const reloc_howto_type *howto,
1434 const Elf_Internal_Rela *rel,
1436 asection *input_section,
1440 if (howto->pc_relative)
1441 value -= sec_addr (input_section) + rel->r_offset;
1442 value += rel->r_addend;
1444 switch (ELFNN_R_TYPE (rel->r_info))
1447 case R_RISCV_TPREL_HI20:
1448 case R_RISCV_PCREL_HI20:
1449 case R_RISCV_GOT_HI20:
1450 case R_RISCV_TLS_GOT_HI20:
1451 case R_RISCV_TLS_GD_HI20:
1452 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1453 return bfd_reloc_overflow;
1454 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1457 case R_RISCV_LO12_I:
1458 case R_RISCV_GPREL_I:
1459 case R_RISCV_TPREL_LO12_I:
1460 case R_RISCV_TPREL_I:
1461 case R_RISCV_PCREL_LO12_I:
1462 value = ENCODE_ITYPE_IMM (value);
1465 case R_RISCV_LO12_S:
1466 case R_RISCV_GPREL_S:
1467 case R_RISCV_TPREL_LO12_S:
1468 case R_RISCV_TPREL_S:
1469 case R_RISCV_PCREL_LO12_S:
1470 value = ENCODE_STYPE_IMM (value);
1474 case R_RISCV_CALL_PLT:
1475 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1476 return bfd_reloc_overflow;
1477 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
1478 | (ENCODE_ITYPE_IMM (value) << 32);
1482 if (!VALID_UJTYPE_IMM (value))
1483 return bfd_reloc_overflow;
1484 value = ENCODE_UJTYPE_IMM (value);
1487 case R_RISCV_BRANCH:
1488 if (!VALID_SBTYPE_IMM (value))
1489 return bfd_reloc_overflow;
1490 value = ENCODE_SBTYPE_IMM (value);
1493 case R_RISCV_RVC_BRANCH:
1494 if (!VALID_RVC_B_IMM (value))
1495 return bfd_reloc_overflow;
1496 value = ENCODE_RVC_B_IMM (value);
1499 case R_RISCV_RVC_JUMP:
1500 if (!VALID_RVC_J_IMM (value))
1501 return bfd_reloc_overflow;
1502 value = ENCODE_RVC_J_IMM (value);
1505 case R_RISCV_RVC_LUI:
1506 if (RISCV_CONST_HIGH_PART (value) == 0)
1508 /* Linker relaxation can convert an address equal to or greater than
1509 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1510 valid immediate. We can fix this by converting it to a C.LI. */
1511 bfd_vma insn = bfd_get (howto->bitsize, input_bfd,
1512 contents + rel->r_offset);
1513 insn = (insn & ~MATCH_C_LUI) | MATCH_C_LI;
1514 bfd_put (howto->bitsize, input_bfd, insn, contents + rel->r_offset);
1515 value = ENCODE_RVC_IMM (0);
1517 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value)))
1518 return bfd_reloc_overflow;
1520 value = ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value));
1538 case R_RISCV_32_PCREL:
1539 case R_RISCV_TLS_DTPREL32:
1540 case R_RISCV_TLS_DTPREL64:
1543 case R_RISCV_DELETE:
1544 return bfd_reloc_ok;
1547 return bfd_reloc_notsupported;
1550 bfd_vma word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
1551 word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
1552 bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
1554 return bfd_reloc_ok;
1557 /* Remember all PC-relative high-part relocs we've encountered to help us
1558 later resolve the corresponding low-part relocs. */
1564 } riscv_pcrel_hi_reloc;
1566 typedef struct riscv_pcrel_lo_reloc
1568 asection * input_section;
1569 struct bfd_link_info * info;
1570 reloc_howto_type * howto;
1571 const Elf_Internal_Rela * reloc;
1574 bfd_byte * contents;
1575 struct riscv_pcrel_lo_reloc * next;
1576 } riscv_pcrel_lo_reloc;
1581 riscv_pcrel_lo_reloc *lo_relocs;
1582 } riscv_pcrel_relocs;
1585 riscv_pcrel_reloc_hash (const void *entry)
1587 const riscv_pcrel_hi_reloc *e = entry;
1588 return (hashval_t)(e->address >> 2);
1592 riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
1594 const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
1595 return e1->address == e2->address;
1599 riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
1602 p->lo_relocs = NULL;
1603 p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
1604 riscv_pcrel_reloc_eq, free);
1605 return p->hi_relocs != NULL;
1609 riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
1611 riscv_pcrel_lo_reloc *cur = p->lo_relocs;
1615 riscv_pcrel_lo_reloc *next = cur->next;
1620 htab_delete (p->hi_relocs);
1624 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela *rel,
1625 struct bfd_link_info *info,
1629 const reloc_howto_type *howto,
1632 /* We may need to reference low addreses in PC-relative modes even when the
1633 * PC is far away from these addresses. For example, undefweak references
1634 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1635 * addresses that we can link PC-relative programs at, the linker can't
1636 * actually relocate references to those symbols. In order to allow these
1637 * programs to work we simply convert the PC-relative auipc sequences to
1638 * 0-relative lui sequences. */
1639 if (bfd_link_pic (info))
1642 /* If it's possible to reference the symbol using auipc we do so, as that's
1643 * more in the spirit of the PC-relative relocations we're processing. */
1644 bfd_vma offset = addr - pc;
1645 if (ARCH_SIZE == 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset)))
1648 /* If it's impossible to reference this with a LUI-based offset then don't
1649 * bother to convert it at all so users still see the PC-relative relocation
1650 * in the truncation message. */
1651 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr)))
1654 rel->r_info = ELFNN_R_INFO(addr, R_RISCV_HI20);
1656 bfd_vma insn = bfd_get(howto->bitsize, input_bfd, contents + rel->r_offset);
1657 insn = (insn & ~MASK_AUIPC) | MATCH_LUI;
1658 bfd_put(howto->bitsize, input_bfd, insn, contents + rel->r_offset);
1663 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p, bfd_vma addr,
1664 bfd_vma value, bfd_boolean absolute)
1666 bfd_vma offset = absolute ? value : value - addr;
1667 riscv_pcrel_hi_reloc entry = {addr, offset};
1668 riscv_pcrel_hi_reloc **slot =
1669 (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
1671 BFD_ASSERT (*slot == NULL);
1672 *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
1680 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
1681 asection *input_section,
1682 struct bfd_link_info *info,
1683 reloc_howto_type *howto,
1684 const Elf_Internal_Rela *reloc,
1689 riscv_pcrel_lo_reloc *entry;
1690 entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
1693 *entry = (riscv_pcrel_lo_reloc) {input_section, info, howto, reloc, addr,
1694 name, contents, p->lo_relocs};
1695 p->lo_relocs = entry;
1700 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
1702 riscv_pcrel_lo_reloc *r;
1704 for (r = p->lo_relocs; r != NULL; r = r->next)
1706 bfd *input_bfd = r->input_section->owner;
1708 riscv_pcrel_hi_reloc search = {r->addr, 0};
1709 riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
1711 /* Check for overflow into bit 11 when adding reloc addend. */
1712 || (! (entry->value & 0x800)
1713 && ((entry->value + r->reloc->r_addend) & 0x800)))
1715 char *string = (entry == NULL
1716 ? "%pcrel_lo missing matching %pcrel_hi"
1717 : "%pcrel_lo overflow with an addend");
1718 (*r->info->callbacks->reloc_dangerous)
1719 (r->info, string, input_bfd, r->input_section, r->reloc->r_offset);
1723 perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
1724 input_bfd, r->contents);
1730 /* Relocate a RISC-V ELF section.
1732 The RELOCATE_SECTION function is called by the new ELF backend linker
1733 to handle the relocations for a section.
1735 The relocs are always passed as Rela structures.
1737 This function is responsible for adjusting the section contents as
1738 necessary, and (if generating a relocatable output file) adjusting
1739 the reloc addend as necessary.
1741 This function does not have to worry about setting the reloc
1742 address or the reloc symbol index.
1744 LOCAL_SYMS is a pointer to the swapped in local symbols.
1746 LOCAL_SECTIONS is an array giving the section in the input file
1747 corresponding to the st_shndx field of each local symbol.
1749 The global hash table entry for the global symbols can be found
1750 via elf_sym_hashes (input_bfd).
1752 When generating relocatable output, this function must handle
1753 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1754 going to be the section symbol corresponding to the output
1755 section, which means that the addend must be adjusted
1759 riscv_elf_relocate_section (bfd *output_bfd,
1760 struct bfd_link_info *info,
1762 asection *input_section,
1764 Elf_Internal_Rela *relocs,
1765 Elf_Internal_Sym *local_syms,
1766 asection **local_sections)
1768 Elf_Internal_Rela *rel;
1769 Elf_Internal_Rela *relend;
1770 riscv_pcrel_relocs pcrel_relocs;
1771 bfd_boolean ret = FALSE;
1772 asection *sreloc = elf_section_data (input_section)->sreloc;
1773 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
1774 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
1775 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1776 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
1777 bfd_boolean absolute;
1779 if (!riscv_init_pcrel_relocs (&pcrel_relocs))
1782 relend = relocs + input_section->reloc_count;
1783 for (rel = relocs; rel < relend; rel++)
1785 unsigned long r_symndx;
1786 struct elf_link_hash_entry *h;
1787 Elf_Internal_Sym *sym;
1790 bfd_reloc_status_type r = bfd_reloc_ok;
1792 bfd_vma off, ie_off;
1793 bfd_boolean unresolved_reloc, is_ie = FALSE;
1794 bfd_vma pc = sec_addr (input_section) + rel->r_offset;
1795 int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
1796 reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
1797 const char *msg = NULL;
1798 char *msg_buf = NULL;
1799 bfd_boolean resolved_to_zero;
1802 || r_type == R_RISCV_GNU_VTINHERIT || r_type == R_RISCV_GNU_VTENTRY)
1805 /* This is a final link. */
1806 r_symndx = ELFNN_R_SYM (rel->r_info);
1810 unresolved_reloc = FALSE;
1811 if (r_symndx < symtab_hdr->sh_info)
1813 sym = local_syms + r_symndx;
1814 sec = local_sections[r_symndx];
1815 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1819 bfd_boolean warned, ignored;
1821 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1822 r_symndx, symtab_hdr, sym_hashes,
1824 unresolved_reloc, warned, ignored);
1827 /* To avoid generating warning messages about truncated
1828 relocations, set the relocation's address to be the same as
1829 the start of this section. */
1830 if (input_section->output_section != NULL)
1831 relocation = input_section->output_section->vma;
1837 if (sec != NULL && discarded_section (sec))
1838 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1839 rel, 1, relend, howto, 0, contents);
1841 if (bfd_link_relocatable (info))
1845 name = h->root.root.string;
1848 name = (bfd_elf_string_from_elf_section
1849 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1850 if (name == NULL || *name == '\0')
1851 name = bfd_section_name (sec);
1854 resolved_to_zero = (h != NULL
1855 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
1861 case R_RISCV_TPREL_ADD:
1863 case R_RISCV_JUMP_SLOT:
1864 case R_RISCV_RELATIVE:
1865 /* These require nothing of us at all. */
1869 case R_RISCV_BRANCH:
1870 case R_RISCV_RVC_BRANCH:
1871 case R_RISCV_RVC_LUI:
1872 case R_RISCV_LO12_I:
1873 case R_RISCV_LO12_S:
1878 case R_RISCV_32_PCREL:
1879 case R_RISCV_DELETE:
1880 /* These require no special handling beyond perform_relocation. */
1883 case R_RISCV_GOT_HI20:
1886 bfd_boolean dyn, pic;
1888 off = h->got.offset;
1889 BFD_ASSERT (off != (bfd_vma) -1);
1890 dyn = elf_hash_table (info)->dynamic_sections_created;
1891 pic = bfd_link_pic (info);
1893 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
1894 || (pic && SYMBOL_REFERENCES_LOCAL (info, h)))
1896 /* This is actually a static link, or it is a
1897 -Bsymbolic link and the symbol is defined
1898 locally, or the symbol was forced to be local
1899 because of a version file. We must initialize
1900 this entry in the global offset table. Since the
1901 offset must always be a multiple of the word size,
1902 we use the least significant bit to record whether
1903 we have initialized it already.
1905 When doing a dynamic link, we create a .rela.got
1906 relocation entry to initialize the value. This
1907 is done in the finish_dynamic_symbol routine. */
1912 bfd_put_NN (output_bfd, relocation,
1913 htab->elf.sgot->contents + off);
1918 unresolved_reloc = FALSE;
1922 BFD_ASSERT (local_got_offsets != NULL
1923 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1925 off = local_got_offsets[r_symndx];
1927 /* The offset must always be a multiple of the word size.
1928 So, we can use the least significant bit to record
1929 whether we have already processed this entry. */
1934 if (bfd_link_pic (info))
1937 Elf_Internal_Rela outrel;
1939 /* We need to generate a R_RISCV_RELATIVE reloc
1940 for the dynamic linker. */
1941 s = htab->elf.srelgot;
1942 BFD_ASSERT (s != NULL);
1944 outrel.r_offset = sec_addr (htab->elf.sgot) + off;
1946 ELFNN_R_INFO (0, R_RISCV_RELATIVE);
1947 outrel.r_addend = relocation;
1949 riscv_elf_append_rela (output_bfd, s, &outrel);
1952 bfd_put_NN (output_bfd, relocation,
1953 htab->elf.sgot->contents + off);
1954 local_got_offsets[r_symndx] |= 1;
1957 relocation = sec_addr (htab->elf.sgot) + off;
1958 absolute = riscv_zero_pcrel_hi_reloc (rel,
1965 r_type = ELFNN_R_TYPE (rel->r_info);
1966 howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
1968 r = bfd_reloc_notsupported;
1969 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
1970 relocation, absolute))
1971 r = bfd_reloc_overflow;
1979 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
1980 contents + rel->r_offset);
1981 relocation = old_value + relocation;
1991 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
1992 contents + rel->r_offset);
1993 relocation = old_value - relocation;
1998 case R_RISCV_CALL_PLT:
1999 /* Handle a call to an undefined weak function. This won't be
2000 relaxed, so we have to handle it here. */
2001 if (h != NULL && h->root.type == bfd_link_hash_undefweak
2002 && (!bfd_link_pic (info) || h->plt.offset == MINUS_ONE))
2004 /* We can use x0 as the base register. */
2005 bfd_vma insn = bfd_get_32 (input_bfd,
2006 contents + rel->r_offset + 4);
2007 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2008 bfd_put_32 (input_bfd, insn, contents + rel->r_offset + 4);
2009 /* Set the relocation value so that we get 0 after the pc
2010 relative adjustment. */
2011 relocation = sec_addr (input_section) + rel->r_offset;
2016 case R_RISCV_RVC_JUMP:
2017 /* This line has to match the check in _bfd_riscv_relax_section. */
2018 if (bfd_link_pic (info) && h != NULL && h->plt.offset != MINUS_ONE)
2020 /* Refer to the PLT entry. */
2021 relocation = sec_addr (htab->elf.splt) + h->plt.offset;
2022 unresolved_reloc = FALSE;
2026 case R_RISCV_TPREL_HI20:
2027 relocation = tpoff (info, relocation);
2030 case R_RISCV_TPREL_LO12_I:
2031 case R_RISCV_TPREL_LO12_S:
2032 relocation = tpoff (info, relocation);
2035 case R_RISCV_TPREL_I:
2036 case R_RISCV_TPREL_S:
2037 relocation = tpoff (info, relocation);
2038 if (VALID_ITYPE_IMM (relocation + rel->r_addend))
2040 /* We can use tp as the base register. */
2041 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2042 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2043 insn |= X_TP << OP_SH_RS1;
2044 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2047 r = bfd_reloc_overflow;
2050 case R_RISCV_GPREL_I:
2051 case R_RISCV_GPREL_S:
2053 bfd_vma gp = riscv_global_pointer_value (info);
2054 bfd_boolean x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
2055 if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
2057 /* We can use x0 or gp as the base register. */
2058 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2059 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2062 rel->r_addend -= gp;
2063 insn |= X_GP << OP_SH_RS1;
2065 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2068 r = bfd_reloc_overflow;
2072 case R_RISCV_PCREL_HI20:
2073 absolute = riscv_zero_pcrel_hi_reloc (rel,
2080 r_type = ELFNN_R_TYPE (rel->r_info);
2081 howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
2083 r = bfd_reloc_notsupported;
2084 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2085 relocation + rel->r_addend,
2087 r = bfd_reloc_overflow;
2090 case R_RISCV_PCREL_LO12_I:
2091 case R_RISCV_PCREL_LO12_S:
2092 /* We don't allow section symbols plus addends as the auipc address,
2093 because then riscv_relax_delete_bytes would have to search through
2094 all relocs to update these addends. This is also ambiguous, as
2095 we do allow offsets to be added to the target address, which are
2096 not to be used to find the auipc address. */
2097 if (((sym != NULL && (ELF_ST_TYPE (sym->st_info) == STT_SECTION))
2098 || (h != NULL && h->type == STT_SECTION))
2101 msg = _("%pcrel_lo section symbol with an addend");
2102 r = bfd_reloc_dangerous;
2106 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, input_section, info,
2107 howto, rel, relocation, name,
2110 r = bfd_reloc_overflow;
2113 case R_RISCV_TLS_DTPREL32:
2114 case R_RISCV_TLS_DTPREL64:
2115 relocation = dtpoff (info, relocation);
2120 if ((input_section->flags & SEC_ALLOC) == 0)
2123 if ((bfd_link_pic (info)
2125 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2126 && !resolved_to_zero)
2127 || h->root.type != bfd_link_hash_undefweak)
2128 && (! howto->pc_relative
2129 || !SYMBOL_CALLS_LOCAL (info, h)))
2130 || (!bfd_link_pic (info)
2136 || h->root.type == bfd_link_hash_undefweak
2137 || h->root.type == bfd_link_hash_undefined)))
2139 Elf_Internal_Rela outrel;
2140 bfd_boolean skip_static_relocation, skip_dynamic_relocation;
2142 /* When generating a shared object, these relocations
2143 are copied into the output file to be resolved at run
2147 _bfd_elf_section_offset (output_bfd, info, input_section,
2149 skip_static_relocation = outrel.r_offset != (bfd_vma) -2;
2150 skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2;
2151 outrel.r_offset += sec_addr (input_section);
2153 if (skip_dynamic_relocation)
2154 memset (&outrel, 0, sizeof outrel);
2155 else if (h != NULL && h->dynindx != -1
2156 && !(bfd_link_pic (info)
2157 && SYMBOLIC_BIND (info, h)
2160 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
2161 outrel.r_addend = rel->r_addend;
2165 outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2166 outrel.r_addend = relocation + rel->r_addend;
2169 riscv_elf_append_rela (output_bfd, sreloc, &outrel);
2170 if (skip_static_relocation)
2175 case R_RISCV_TLS_GOT_HI20:
2179 case R_RISCV_TLS_GD_HI20:
2182 off = h->got.offset;
2187 off = local_got_offsets[r_symndx];
2188 local_got_offsets[r_symndx] |= 1;
2191 tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
2192 BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
2193 /* If this symbol is referenced by both GD and IE TLS, the IE
2194 reference's GOT slot follows the GD reference's slots. */
2196 if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
2197 ie_off = 2 * GOT_ENTRY_SIZE;
2203 Elf_Internal_Rela outrel;
2205 bfd_boolean need_relocs = FALSE;
2207 if (htab->elf.srelgot == NULL)
2212 bfd_boolean dyn, pic;
2213 dyn = htab->elf.dynamic_sections_created;
2214 pic = bfd_link_pic (info);
2216 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
2217 && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h)))
2221 /* The GOT entries have not been initialized yet. Do it
2222 now, and emit any relocations. */
2223 if ((bfd_link_pic (info) || indx != 0)
2225 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2226 || h->root.type != bfd_link_hash_undefweak))
2229 if (tls_type & GOT_TLS_GD)
2233 outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2234 outrel.r_addend = 0;
2235 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN);
2236 bfd_put_NN (output_bfd, 0,
2237 htab->elf.sgot->contents + off);
2238 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2241 BFD_ASSERT (! unresolved_reloc);
2242 bfd_put_NN (output_bfd,
2243 dtpoff (info, relocation),
2244 (htab->elf.sgot->contents + off +
2245 RISCV_ELF_WORD_BYTES));
2249 bfd_put_NN (output_bfd, 0,
2250 (htab->elf.sgot->contents + off +
2251 RISCV_ELF_WORD_BYTES));
2252 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN);
2253 outrel.r_offset += RISCV_ELF_WORD_BYTES;
2254 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2259 /* If we are not emitting relocations for a
2260 general dynamic reference, then we must be in a
2261 static link or an executable link with the
2262 symbol binding locally. Mark it as belonging
2263 to module 1, the executable. */
2264 bfd_put_NN (output_bfd, 1,
2265 htab->elf.sgot->contents + off);
2266 bfd_put_NN (output_bfd,
2267 dtpoff (info, relocation),
2268 (htab->elf.sgot->contents + off +
2269 RISCV_ELF_WORD_BYTES));
2273 if (tls_type & GOT_TLS_IE)
2277 bfd_put_NN (output_bfd, 0,
2278 htab->elf.sgot->contents + off + ie_off);
2279 outrel.r_offset = sec_addr (htab->elf.sgot)
2281 outrel.r_addend = 0;
2283 outrel.r_addend = tpoff (info, relocation);
2284 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN);
2285 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2289 bfd_put_NN (output_bfd, tpoff (info, relocation),
2290 htab->elf.sgot->contents + off + ie_off);
2295 BFD_ASSERT (off < (bfd_vma) -2);
2296 relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
2297 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2299 r = bfd_reloc_overflow;
2300 unresolved_reloc = FALSE;
2304 r = bfd_reloc_notsupported;
2307 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2308 because such sections are not SEC_ALLOC and thus ld.so will
2309 not process them. */
2310 if (unresolved_reloc
2311 && !((input_section->flags & SEC_DEBUGGING) != 0
2313 && _bfd_elf_section_offset (output_bfd, info, input_section,
2314 rel->r_offset) != (bfd_vma) -1)
2320 case R_RISCV_RVC_JUMP:
2321 if (asprintf (&msg_buf,
2322 _("%%X%%P: relocation %s against `%s' can "
2323 "not be used when making a shared object; "
2324 "recompile with -fPIC\n"),
2326 h->root.root.string) == -1)
2331 if (asprintf (&msg_buf,
2332 _("%%X%%P: unresolvable %s relocation against "
2335 h->root.root.string) == -1)
2341 r = bfd_reloc_notsupported;
2344 if (r == bfd_reloc_ok)
2345 r = perform_relocation (howto, rel, relocation, input_section,
2346 input_bfd, contents);
2348 /* We should have already detected the error and set message before.
2349 If the error message isn't set since the linker runs out of memory
2350 or we don't set it before, then we should set the default message
2351 with the "internal error" string here. */
2357 case bfd_reloc_overflow:
2358 info->callbacks->reloc_overflow
2359 (info, (h ? &h->root : NULL), name, howto->name,
2360 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2363 case bfd_reloc_undefined:
2364 info->callbacks->undefined_symbol
2365 (info, name, input_bfd, input_section, rel->r_offset,
2369 case bfd_reloc_outofrange:
2371 msg = _("%X%P: internal error: out of range error\n");
2374 case bfd_reloc_notsupported:
2376 msg = _("%X%P: internal error: unsupported relocation error\n");
2379 case bfd_reloc_dangerous:
2380 /* The error message should already be set. */
2382 msg = _("dangerous relocation error");
2383 info->callbacks->reloc_dangerous
2384 (info, msg, input_bfd, input_section, rel->r_offset);
2388 msg = _("%X%P: internal error: unknown error\n");
2392 /* Do not report error message for the dangerous relocation again. */
2393 if (msg && r != bfd_reloc_dangerous)
2394 info->callbacks->einfo (msg);
2396 /* Free the unused `msg_buf`. */
2399 /* We already reported the error via a callback, so don't try to report
2400 it again by returning false. That leads to spurious errors. */
2405 ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
2407 riscv_free_pcrel_relocs (&pcrel_relocs);
2411 /* Finish up dynamic symbol handling. We set the contents of various
2412 dynamic sections here. */
2415 riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
2416 struct bfd_link_info *info,
2417 struct elf_link_hash_entry *h,
2418 Elf_Internal_Sym *sym)
2420 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2421 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2423 if (h->plt.offset != (bfd_vma) -1)
2425 /* We've decided to create a PLT entry for this symbol. */
2427 bfd_vma i, header_address, plt_idx, got_address;
2428 uint32_t plt_entry[PLT_ENTRY_INSNS];
2429 Elf_Internal_Rela rela;
2431 BFD_ASSERT (h->dynindx != -1);
2433 /* Calculate the address of the PLT header. */
2434 header_address = sec_addr (htab->elf.splt);
2436 /* Calculate the index of the entry. */
2437 plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
2439 /* Calculate the address of the .got.plt entry. */
2440 got_address = riscv_elf_got_plt_val (plt_idx, info);
2442 /* Find out where the .plt entry should go. */
2443 loc = htab->elf.splt->contents + h->plt.offset;
2445 /* Fill in the PLT entry itself. */
2446 if (! riscv_make_plt_entry (output_bfd, got_address,
2447 header_address + h->plt.offset,
2451 for (i = 0; i < PLT_ENTRY_INSNS; i++)
2452 bfd_put_32 (output_bfd, plt_entry[i], loc + 4*i);
2454 /* Fill in the initial value of the .got.plt entry. */
2455 loc = htab->elf.sgotplt->contents
2456 + (got_address - sec_addr (htab->elf.sgotplt));
2457 bfd_put_NN (output_bfd, sec_addr (htab->elf.splt), loc);
2459 /* Fill in the entry in the .rela.plt section. */
2460 rela.r_offset = got_address;
2462 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
2464 loc = htab->elf.srelplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
2465 bed->s->swap_reloca_out (output_bfd, &rela, loc);
2467 if (!h->def_regular)
2469 /* Mark the symbol as undefined, rather than as defined in
2470 the .plt section. Leave the value alone. */
2471 sym->st_shndx = SHN_UNDEF;
2472 /* If the symbol is weak, we do need to clear the value.
2473 Otherwise, the PLT entry would provide a definition for
2474 the symbol even if the symbol wasn't defined anywhere,
2475 and so the symbol would never be NULL. */
2476 if (!h->ref_regular_nonweak)
2481 if (h->got.offset != (bfd_vma) -1
2482 && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE))
2483 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
2487 Elf_Internal_Rela rela;
2489 /* This symbol has an entry in the GOT. Set it up. */
2491 sgot = htab->elf.sgot;
2492 srela = htab->elf.srelgot;
2493 BFD_ASSERT (sgot != NULL && srela != NULL);
2495 rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
2497 /* If this is a local symbol reference, we just want to emit a RELATIVE
2498 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2499 the symbol was forced to be local because of a version file.
2500 The entry in the global offset table will already have been
2501 initialized in the relocate_section function. */
2502 if (bfd_link_pic (info)
2503 && SYMBOL_REFERENCES_LOCAL (info, h))
2505 BFD_ASSERT((h->got.offset & 1) != 0);
2506 asection *sec = h->root.u.def.section;
2507 rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2508 rela.r_addend = (h->root.u.def.value
2509 + sec->output_section->vma
2510 + sec->output_offset);
2514 BFD_ASSERT((h->got.offset & 1) == 0);
2515 BFD_ASSERT (h->dynindx != -1);
2516 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
2520 bfd_put_NN (output_bfd, 0,
2521 sgot->contents + (h->got.offset & ~(bfd_vma) 1));
2522 riscv_elf_append_rela (output_bfd, srela, &rela);
2527 Elf_Internal_Rela rela;
2530 /* This symbols needs a copy reloc. Set it up. */
2531 BFD_ASSERT (h->dynindx != -1);
2533 rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
2534 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY);
2536 if (h->root.u.def.section == htab->elf.sdynrelro)
2537 s = htab->elf.sreldynrelro;
2539 s = htab->elf.srelbss;
2540 riscv_elf_append_rela (output_bfd, s, &rela);
2543 /* Mark some specially defined symbols as absolute. */
2544 if (h == htab->elf.hdynamic
2545 || (h == htab->elf.hgot || h == htab->elf.hplt))
2546 sym->st_shndx = SHN_ABS;
2551 /* Finish up the dynamic sections. */
2554 riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
2555 bfd *dynobj, asection *sdyn)
2557 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2558 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2559 size_t dynsize = bed->s->sizeof_dyn;
2560 bfd_byte *dyncon, *dynconend;
2562 dynconend = sdyn->contents + sdyn->size;
2563 for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
2565 Elf_Internal_Dyn dyn;
2568 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
2573 s = htab->elf.sgotplt;
2574 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2577 s = htab->elf.srelplt;
2578 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2581 s = htab->elf.srelplt;
2582 dyn.d_un.d_val = s->size;
2588 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
2594 riscv_elf_finish_dynamic_sections (bfd *output_bfd,
2595 struct bfd_link_info *info)
2599 struct riscv_elf_link_hash_table *htab;
2601 htab = riscv_elf_hash_table (info);
2602 BFD_ASSERT (htab != NULL);
2603 dynobj = htab->elf.dynobj;
2605 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2607 if (elf_hash_table (info)->dynamic_sections_created)
2612 splt = htab->elf.splt;
2613 BFD_ASSERT (splt != NULL && sdyn != NULL);
2615 ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
2620 /* Fill in the head and tail entries in the procedure linkage table. */
2624 uint32_t plt_header[PLT_HEADER_INSNS];
2625 ret = riscv_make_plt_header (output_bfd,
2626 sec_addr (htab->elf.sgotplt),
2627 sec_addr (splt), plt_header);
2631 for (i = 0; i < PLT_HEADER_INSNS; i++)
2632 bfd_put_32 (output_bfd, plt_header[i], splt->contents + 4*i);
2634 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2639 if (htab->elf.sgotplt)
2641 asection *output_section = htab->elf.sgotplt->output_section;
2643 if (bfd_is_abs_section (output_section))
2645 (*_bfd_error_handler)
2646 (_("discarded output section: `%pA'"), htab->elf.sgotplt);
2650 if (htab->elf.sgotplt->size > 0)
2652 /* Write the first two entries in .got.plt, needed for the dynamic
2654 bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
2655 bfd_put_NN (output_bfd, (bfd_vma) 0,
2656 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
2659 elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
2664 asection *output_section = htab->elf.sgot->output_section;
2666 if (htab->elf.sgot->size > 0)
2668 /* Set the first entry in the global offset table to the address of
2669 the dynamic section. */
2670 bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
2671 bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
2674 elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
2680 /* Return address for Ith PLT stub in section PLT, for relocation REL
2681 or (bfd_vma) -1 if it should not be included. */
2684 riscv_elf_plt_sym_val (bfd_vma i, const asection *plt,
2685 const arelent *rel ATTRIBUTE_UNUSED)
2687 return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
2690 static enum elf_reloc_type_class
2691 riscv_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2692 const asection *rel_sec ATTRIBUTE_UNUSED,
2693 const Elf_Internal_Rela *rela)
2695 switch (ELFNN_R_TYPE (rela->r_info))
2697 case R_RISCV_RELATIVE:
2698 return reloc_class_relative;
2699 case R_RISCV_JUMP_SLOT:
2700 return reloc_class_plt;
2702 return reloc_class_copy;
2704 return reloc_class_normal;
2708 /* Given the ELF header flags in FLAGS, it returns a string that describes the
2712 riscv_float_abi_string (flagword flags)
2714 switch (flags & EF_RISCV_FLOAT_ABI)
2716 case EF_RISCV_FLOAT_ABI_SOFT:
2717 return "soft-float";
2719 case EF_RISCV_FLOAT_ABI_SINGLE:
2720 return "single-float";
2722 case EF_RISCV_FLOAT_ABI_DOUBLE:
2723 return "double-float";
2725 case EF_RISCV_FLOAT_ABI_QUAD:
2726 return "quad-float";
2733 /* The information of architecture attribute. */
2734 static riscv_subset_list_t in_subsets;
2735 static riscv_subset_list_t out_subsets;
2736 static riscv_subset_list_t merged_subsets;
2738 /* Predicator for standard extension. */
2741 riscv_std_ext_p (const char *name)
2743 return (strlen (name) == 1) && (name[0] != 'x') && (name[0] != 's');
2746 /* Error handler when version mis-match. */
2749 riscv_version_mismatch (bfd *ibfd,
2750 struct riscv_subset_t *in,
2751 struct riscv_subset_t *out)
2754 (_("error: %pB: Mis-matched ISA version for '%s' extension. "
2757 in->major_version, in->minor_version,
2758 out->major_version, out->minor_version);
2761 /* Return true if subset is 'i' or 'e'. */
2764 riscv_i_or_e_p (bfd *ibfd,
2766 struct riscv_subset_t *subset)
2768 if ((strcasecmp (subset->name, "e") != 0)
2769 && (strcasecmp (subset->name, "i") != 0))
2772 (_("error: %pB: corrupted ISA string '%s'. "
2773 "First letter should be 'i' or 'e' but got '%s'."),
2774 ibfd, arch, subset->name);
2780 /* Merge standard extensions.
2783 Return FALSE if failed to merge.
2787 `in_arch`: Raw arch string for input object.
2788 `out_arch`: Raw arch string for output object.
2789 `pin`: subset list for input object, and it'll skip all merged subset after
2791 `pout`: Like `pin`, but for output object. */
2794 riscv_merge_std_ext (bfd *ibfd,
2795 const char *in_arch,
2796 const char *out_arch,
2797 struct riscv_subset_t **pin,
2798 struct riscv_subset_t **pout)
2800 const char *standard_exts = riscv_supported_std_ext ();
2802 struct riscv_subset_t *in = *pin;
2803 struct riscv_subset_t *out = *pout;
2805 /* First letter should be 'i' or 'e'. */
2806 if (!riscv_i_or_e_p (ibfd, in_arch, in))
2809 if (!riscv_i_or_e_p (ibfd, out_arch, out))
2812 if (strcasecmp (in->name, out->name) != 0)
2814 /* TODO: We might allow merge 'i' with 'e'. */
2816 (_("error: %pB: Mis-matched ISA string to merge '%s' and '%s'."),
2817 ibfd, in->name, out->name);
2820 else if ((in->major_version != out->major_version) ||
2821 (in->minor_version != out->minor_version))
2823 /* TODO: Allow different merge policy. */
2824 riscv_version_mismatch (ibfd, in, out);
2828 riscv_add_subset (&merged_subsets,
2829 in->name, in->major_version, in->minor_version);
2834 /* Handle standard extension first. */
2835 for (p = standard_exts; *p; ++p)
2837 char find_ext[2] = {*p, '\0'};
2838 struct riscv_subset_t *find_in =
2839 riscv_lookup_subset (&in_subsets, find_ext);
2840 struct riscv_subset_t *find_out =
2841 riscv_lookup_subset (&out_subsets, find_ext);
2843 if (find_in == NULL && find_out == NULL)
2846 /* Check version is same or not. */
2847 /* TODO: Allow different merge policy. */
2848 if ((find_in != NULL && find_out != NULL)
2849 && ((find_in->major_version != find_out->major_version)
2850 || (find_in->minor_version != find_out->minor_version)))
2852 riscv_version_mismatch (ibfd, in, out);
2856 struct riscv_subset_t *merged = find_in ? find_in : find_out;
2857 riscv_add_subset (&merged_subsets, merged->name,
2858 merged->major_version, merged->minor_version);
2861 /* Skip all standard extensions. */
2862 while ((in != NULL) && riscv_std_ext_p (in->name)) in = in->next;
2863 while ((out != NULL) && riscv_std_ext_p (out->name)) out = out->next;
2871 /* If C is a prefix class, then return the EXT string without the prefix.
2872 Otherwise return the entire EXT string. */
2875 riscv_skip_prefix (const char *ext, riscv_isa_ext_class_t c)
2879 case RV_ISA_CLASS_X: return &ext[1];
2880 case RV_ISA_CLASS_S: return &ext[1];
2881 case RV_ISA_CLASS_Z: return &ext[1];
2882 default: return ext;
2886 /* Compare prefixed extension names canonically. */
2889 riscv_prefix_cmp (const char *a, const char *b)
2891 riscv_isa_ext_class_t ca = riscv_get_prefix_class (a);
2892 riscv_isa_ext_class_t cb = riscv_get_prefix_class (b);
2894 /* Extension name without prefix */
2895 const char *anp = riscv_skip_prefix (a, ca);
2896 const char *bnp = riscv_skip_prefix (b, cb);
2899 return strcasecmp (anp, bnp);
2901 return (int)ca - (int)cb;
2904 /* Merge multi letter extensions. PIN is a pointer to the head of the input
2905 object subset list. Likewise for POUT and the output object. Return TRUE
2906 on success and FALSE when a conflict is found. */
2909 riscv_merge_multi_letter_ext (bfd *ibfd,
2910 riscv_subset_t **pin,
2911 riscv_subset_t **pout)
2913 riscv_subset_t *in = *pin;
2914 riscv_subset_t *out = *pout;
2915 riscv_subset_t *tail;
2921 cmp = riscv_prefix_cmp (in->name, out->name);
2925 /* `in' comes before `out', append `in' and increment. */
2926 riscv_add_subset (&merged_subsets, in->name, in->major_version,
2932 /* `out' comes before `in', append `out' and increment. */
2933 riscv_add_subset (&merged_subsets, out->name, out->major_version,
2934 out->minor_version);
2939 /* Both present, check version and increment both. */
2940 if ((in->major_version != out->major_version)
2941 || (in->minor_version != out->minor_version))
2943 riscv_version_mismatch (ibfd, in, out);
2947 riscv_add_subset (&merged_subsets, out->name, out->major_version,
2948 out->minor_version);
2955 /* If we're here, either `in' or `out' is running longer than
2956 the other. So, we need to append the corresponding tail. */
2957 tail = in ? in : out;
2961 riscv_add_subset (&merged_subsets, tail->name, tail->major_version,
2962 tail->minor_version);
2970 /* Merge Tag_RISCV_arch attribute. */
2973 riscv_merge_arch_attr_info (bfd *ibfd, char *in_arch, char *out_arch)
2975 riscv_subset_t *in, *out;
2976 char *merged_arch_str;
2978 unsigned xlen_in, xlen_out;
2979 merged_subsets.head = NULL;
2980 merged_subsets.tail = NULL;
2982 riscv_parse_subset_t rpe_in;
2983 riscv_parse_subset_t rpe_out;
2985 /* Only assembler needs to check the default version of ISA, so just set
2986 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
2987 rpe_in.subset_list = &in_subsets;
2988 rpe_in.error_handler = _bfd_error_handler;
2989 rpe_in.xlen = &xlen_in;
2990 rpe_in.get_default_version = NULL;
2992 rpe_out.subset_list = &out_subsets;
2993 rpe_out.error_handler = _bfd_error_handler;
2994 rpe_out.xlen = &xlen_out;
2995 rpe_out.get_default_version = NULL;
2997 if (in_arch == NULL && out_arch == NULL)
3000 if (in_arch == NULL && out_arch != NULL)
3003 if (in_arch != NULL && out_arch == NULL)
3006 /* Parse subset from arch string. */
3007 if (!riscv_parse_subset (&rpe_in, in_arch))
3010 if (!riscv_parse_subset (&rpe_out, out_arch))
3013 /* Checking XLEN. */
3014 if (xlen_out != xlen_in)
3017 (_("error: %pB: ISA string of input (%s) doesn't match "
3018 "output (%s)."), ibfd, in_arch, out_arch);
3022 /* Merge subset list. */
3023 in = in_subsets.head;
3024 out = out_subsets.head;
3026 /* Merge standard extension. */
3027 if (!riscv_merge_std_ext (ibfd, in_arch, out_arch, &in, &out))
3030 /* Merge all non-single letter extensions with single call. */
3031 if (!riscv_merge_multi_letter_ext (ibfd, &in, &out))
3034 if (xlen_in != xlen_out)
3037 (_("error: %pB: XLEN of input (%u) doesn't match "
3038 "output (%u)."), ibfd, xlen_in, xlen_out);
3042 if (xlen_in != ARCH_SIZE)
3045 (_("error: %pB: Unsupported XLEN (%u), you might be "
3046 "using wrong emulation."), ibfd, xlen_in);
3050 merged_arch_str = riscv_arch_str (ARCH_SIZE, &merged_subsets);
3052 /* Release the subset lists. */
3053 riscv_release_subset_list (&in_subsets);
3054 riscv_release_subset_list (&out_subsets);
3055 riscv_release_subset_list (&merged_subsets);
3057 return merged_arch_str;
3060 /* Merge object attributes from IBFD into output_bfd of INFO.
3061 Raise an error if there are conflicting attributes. */
3064 riscv_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
3066 bfd *obfd = info->output_bfd;
3067 obj_attribute *in_attr;
3068 obj_attribute *out_attr;
3069 bfd_boolean result = TRUE;
3070 const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
3073 /* Skip linker created files. */
3074 if (ibfd->flags & BFD_LINKER_CREATED)
3077 /* Skip any input that doesn't have an attribute section.
3078 This enables to link object files without attribute section with
3080 if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
3083 if (!elf_known_obj_attributes_proc (obfd)[0].i)
3085 /* This is the first object. Copy the attributes. */
3086 _bfd_elf_copy_obj_attributes (ibfd, obfd);
3088 out_attr = elf_known_obj_attributes_proc (obfd);
3090 /* Use the Tag_null value to indicate the attributes have been
3097 in_attr = elf_known_obj_attributes_proc (ibfd);
3098 out_attr = elf_known_obj_attributes_proc (obfd);
3100 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
3104 case Tag_RISCV_arch:
3105 if (!out_attr[Tag_RISCV_arch].s)
3106 out_attr[Tag_RISCV_arch].s = in_attr[Tag_RISCV_arch].s;
3107 else if (in_attr[Tag_RISCV_arch].s
3108 && out_attr[Tag_RISCV_arch].s)
3110 /* Check arch compatible. */
3112 riscv_merge_arch_attr_info (ibfd,
3113 in_attr[Tag_RISCV_arch].s,
3114 out_attr[Tag_RISCV_arch].s);
3115 if (merged_arch == NULL)
3118 out_attr[Tag_RISCV_arch].s = "";
3121 out_attr[Tag_RISCV_arch].s = merged_arch;
3124 case Tag_RISCV_priv_spec:
3125 case Tag_RISCV_priv_spec_minor:
3126 case Tag_RISCV_priv_spec_revision:
3127 if (out_attr[i].i != in_attr[i].i)
3130 (_("error: %pB: conflicting priv spec version "
3131 "(major/minor/revision)."), ibfd);
3135 case Tag_RISCV_unaligned_access:
3136 out_attr[i].i |= in_attr[i].i;
3138 case Tag_RISCV_stack_align:
3139 if (out_attr[i].i == 0)
3140 out_attr[i].i = in_attr[i].i;
3141 else if (in_attr[i].i != 0
3142 && out_attr[i].i != 0
3143 && out_attr[i].i != in_attr[i].i)
3146 (_("error: %pB use %u-byte stack aligned but the output "
3147 "use %u-byte stack aligned."),
3148 ibfd, in_attr[i].i, out_attr[i].i);
3153 result &= _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
3156 /* If out_attr was copied from in_attr then it won't have a type yet. */
3157 if (in_attr[i].type && !out_attr[i].type)
3158 out_attr[i].type = in_attr[i].type;
3161 /* Merge Tag_compatibility attributes and any common GNU ones. */
3162 if (!_bfd_elf_merge_object_attributes (ibfd, info))
3165 /* Check for any attributes not known on RISC-V. */
3166 result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
3171 /* Merge backend specific data from an object file to the output
3172 object file when linking. */
3175 _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3177 bfd *obfd = info->output_bfd;
3178 flagword new_flags, old_flags;
3180 if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd))
3183 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
3185 (*_bfd_error_handler)
3186 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3187 " target emulation `%s' does not match `%s'"),
3188 ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
3192 if (!_bfd_elf_merge_object_attributes (ibfd, info))
3195 if (!riscv_merge_attributes (ibfd, info))
3198 new_flags = elf_elfheader (ibfd)->e_flags;
3199 old_flags = elf_elfheader (obfd)->e_flags;
3201 if (! elf_flags_init (obfd))
3203 elf_flags_init (obfd) = TRUE;
3204 elf_elfheader (obfd)->e_flags = new_flags;
3208 /* Check to see if the input BFD actually contains any sections. If not,
3209 its flags may not have been initialized either, but it cannot actually
3210 cause any incompatibility. Do not short-circuit dynamic objects; their
3211 section list may be emptied by elf_link_add_object_symbols.
3213 Also check to see if there are no code sections in the input. In this
3214 case, there is no need to check for code specific flags. */
3215 if (!(ibfd->flags & DYNAMIC))
3217 bfd_boolean null_input_bfd = TRUE;
3218 bfd_boolean only_data_sections = TRUE;
3221 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3223 if ((bfd_section_flags (sec)
3224 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3225 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3226 only_data_sections = FALSE;
3228 null_input_bfd = FALSE;
3232 if (null_input_bfd || only_data_sections)
3236 /* Disallow linking different float ABIs. */
3237 if ((old_flags ^ new_flags) & EF_RISCV_FLOAT_ABI)
3239 (*_bfd_error_handler)
3240 (_("%pB: can't link %s modules with %s modules"), ibfd,
3241 riscv_float_abi_string (new_flags),
3242 riscv_float_abi_string (old_flags));
3246 /* Disallow linking RVE and non-RVE. */
3247 if ((old_flags ^ new_flags) & EF_RISCV_RVE)
3249 (*_bfd_error_handler)
3250 (_("%pB: can't link RVE with other target"), ibfd);
3254 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3255 elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_RVC;
3260 bfd_set_error (bfd_error_bad_value);
3264 /* Delete some bytes from a section while relaxing. */
3267 riscv_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, size_t count,
3268 struct bfd_link_info *link_info)
3270 unsigned int i, symcount;
3271 bfd_vma toaddr = sec->size;
3272 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
3273 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3274 unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3275 struct bfd_elf_section_data *data = elf_section_data (sec);
3276 bfd_byte *contents = data->this_hdr.contents;
3278 /* Actually delete the bytes. */
3280 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
3282 /* Adjust the location of all of the relocs. Note that we need not
3283 adjust the addends, since all PC-relative references must be against
3284 symbols, which we will adjust below. */
3285 for (i = 0; i < sec->reloc_count; i++)
3286 if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr)
3287 data->relocs[i].r_offset -= count;
3289 /* Adjust the local symbols defined in this section. */
3290 for (i = 0; i < symtab_hdr->sh_info; i++)
3292 Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
3293 if (sym->st_shndx == sec_shndx)
3295 /* If the symbol is in the range of memory we just moved, we
3296 have to adjust its value. */
3297 if (sym->st_value > addr && sym->st_value <= toaddr)
3298 sym->st_value -= count;
3300 /* If the symbol *spans* the bytes we just deleted (i.e. its
3301 *end* is in the moved bytes but its *start* isn't), then we
3302 must adjust its size.
3304 This test needs to use the original value of st_value, otherwise
3305 we might accidentally decrease size when deleting bytes right
3306 before the symbol. But since deleted relocs can't span across
3307 symbols, we can't have both a st_value and a st_size decrease,
3308 so it is simpler to just use an else. */
3309 else if (sym->st_value <= addr
3310 && sym->st_value + sym->st_size > addr
3311 && sym->st_value + sym->st_size <= toaddr)
3312 sym->st_size -= count;
3316 /* Now adjust the global symbols defined in this section. */
3317 symcount = ((symtab_hdr->sh_size / sizeof (ElfNN_External_Sym))
3318 - symtab_hdr->sh_info);
3320 for (i = 0; i < symcount; i++)
3322 struct elf_link_hash_entry *sym_hash = sym_hashes[i];
3324 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3325 containing the definition of __wrap_SYMBOL, includes a direct
3326 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3327 the same symbol (which is __wrap_SYMBOL), but still exist as two
3328 different symbols in 'sym_hashes', we don't want to adjust
3329 the global symbol __wrap_SYMBOL twice. */
3330 /* The same problem occurs with symbols that are versioned_hidden, as
3331 foo becomes an alias for foo@BAR, and hence they need the same
3333 if (link_info->wrap_hash != NULL
3334 || sym_hash->versioned == versioned_hidden)
3336 struct elf_link_hash_entry **cur_sym_hashes;
3338 /* Loop only over the symbols which have already been checked. */
3339 for (cur_sym_hashes = sym_hashes; cur_sym_hashes < &sym_hashes[i];
3342 /* If the current symbol is identical to 'sym_hash', that means
3343 the symbol was already adjusted (or at least checked). */
3344 if (*cur_sym_hashes == sym_hash)
3347 /* Don't adjust the symbol again. */
3348 if (cur_sym_hashes < &sym_hashes[i])
3352 if ((sym_hash->root.type == bfd_link_hash_defined
3353 || sym_hash->root.type == bfd_link_hash_defweak)
3354 && sym_hash->root.u.def.section == sec)
3356 /* As above, adjust the value if needed. */
3357 if (sym_hash->root.u.def.value > addr
3358 && sym_hash->root.u.def.value <= toaddr)
3359 sym_hash->root.u.def.value -= count;
3361 /* As above, adjust the size if needed. */
3362 else if (sym_hash->root.u.def.value <= addr
3363 && sym_hash->root.u.def.value + sym_hash->size > addr
3364 && sym_hash->root.u.def.value + sym_hash->size <= toaddr)
3365 sym_hash->size -= count;
3372 /* A second format for recording PC-relative hi relocations. This stores the
3373 information required to relax them to GP-relative addresses. */
3375 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc;
3376 struct riscv_pcgp_hi_reloc
3383 bfd_boolean undefined_weak;
3384 riscv_pcgp_hi_reloc *next;
3387 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc;
3388 struct riscv_pcgp_lo_reloc
3391 riscv_pcgp_lo_reloc *next;
3396 riscv_pcgp_hi_reloc *hi;
3397 riscv_pcgp_lo_reloc *lo;
3398 } riscv_pcgp_relocs;
3400 /* Initialize the pcgp reloc info in P. */
3403 riscv_init_pcgp_relocs (riscv_pcgp_relocs *p)
3410 /* Free the pcgp reloc info in P. */
3413 riscv_free_pcgp_relocs (riscv_pcgp_relocs *p,
3414 bfd *abfd ATTRIBUTE_UNUSED,
3415 asection *sec ATTRIBUTE_UNUSED)
3417 riscv_pcgp_hi_reloc *c;
3418 riscv_pcgp_lo_reloc *l;
3420 for (c = p->hi; c != NULL;)
3422 riscv_pcgp_hi_reloc *next = c->next;
3427 for (l = p->lo; l != NULL;)
3429 riscv_pcgp_lo_reloc *next = l->next;
3435 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3436 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3437 relax the corresponding lo part reloc. */
3440 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off,
3441 bfd_vma hi_addend, bfd_vma hi_addr,
3442 unsigned hi_sym, asection *sym_sec,
3443 bfd_boolean undefined_weak)
3445 riscv_pcgp_hi_reloc *new = bfd_malloc (sizeof(*new));
3448 new->hi_sec_off = hi_sec_off;
3449 new->hi_addend = hi_addend;
3450 new->hi_addr = hi_addr;
3451 new->hi_sym = hi_sym;
3452 new->sym_sec = sym_sec;
3453 new->undefined_weak = undefined_weak;
3459 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3460 This is used by a lo part reloc to find the corresponding hi part reloc. */
3462 static riscv_pcgp_hi_reloc *
3463 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
3465 riscv_pcgp_hi_reloc *c;
3467 for (c = p->hi; c != NULL; c = c->next)
3468 if (c->hi_sec_off == hi_sec_off)
3473 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3474 This is used to record relocs that can't be relaxed. */
3477 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
3479 riscv_pcgp_lo_reloc *new = bfd_malloc (sizeof(*new));
3482 new->hi_sec_off = hi_sec_off;
3488 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3489 This is used by a hi part reloc to find the corresponding lo part reloc. */
3492 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
3494 riscv_pcgp_lo_reloc *c;
3496 for (c = p->lo; c != NULL; c = c->next)
3497 if (c->hi_sec_off == hi_sec_off)
3502 typedef bfd_boolean (*relax_func_t) (bfd *, asection *, asection *,
3503 struct bfd_link_info *,
3504 Elf_Internal_Rela *,
3505 bfd_vma, bfd_vma, bfd_vma, bfd_boolean *,
3506 riscv_pcgp_relocs *,
3507 bfd_boolean undefined_weak);
3509 /* Relax AUIPC + JALR into JAL. */
3512 _bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
3513 struct bfd_link_info *link_info,
3514 Elf_Internal_Rela *rel,
3516 bfd_vma max_alignment,
3517 bfd_vma reserve_size ATTRIBUTE_UNUSED,
3519 riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
3520 bfd_boolean undefined_weak ATTRIBUTE_UNUSED)
3522 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
3523 bfd_signed_vma foff = symval - (sec_addr (sec) + rel->r_offset);
3524 bfd_boolean near_zero = (symval + RISCV_IMM_REACH/2) < RISCV_IMM_REACH;
3525 bfd_vma auipc, jalr;
3526 int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
3528 /* If the call crosses section boundaries, an alignment directive could
3529 cause the PC-relative offset to later increase, so we need to add in the
3530 max alignment of any section inclusive from the call to the target.
3531 Otherwise, we only need to use the alignment of the current section. */
3532 if (VALID_UJTYPE_IMM (foff))
3534 if (sym_sec->output_section == sec->output_section
3535 && sym_sec->output_section != bfd_abs_section_ptr)
3536 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
3537 foff += (foff < 0 ? -max_alignment : max_alignment);
3540 /* See if this function call can be shortened. */
3541 if (!VALID_UJTYPE_IMM (foff) && !(!bfd_link_pic (link_info) && near_zero))
3544 /* Shorten the function call. */
3545 BFD_ASSERT (rel->r_offset + 8 <= sec->size);
3547 auipc = bfd_get_32 (abfd, contents + rel->r_offset);
3548 jalr = bfd_get_32 (abfd, contents + rel->r_offset + 4);
3549 rd = (jalr >> OP_SH_RD) & OP_MASK_RD;
3550 rvc = rvc && VALID_RVC_J_IMM (foff);
3552 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
3553 rvc = rvc && (rd == 0 || (rd == X_RA && ARCH_SIZE == 32));
3557 /* Relax to C.J[AL] rd, addr. */
3558 r_type = R_RISCV_RVC_JUMP;
3559 auipc = rd == 0 ? MATCH_C_J : MATCH_C_JAL;
3562 else if (VALID_UJTYPE_IMM (foff))
3564 /* Relax to JAL rd, addr. */
3565 r_type = R_RISCV_JAL;
3566 auipc = MATCH_JAL | (rd << OP_SH_RD);
3568 else /* near_zero */
3570 /* Relax to JALR rd, x0, addr. */
3571 r_type = R_RISCV_LO12_I;
3572 auipc = MATCH_JALR | (rd << OP_SH_RD);
3575 /* Replace the R_RISCV_CALL reloc. */
3576 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), r_type);
3577 /* Replace the AUIPC. */
3578 bfd_put (8 * len, abfd, auipc, contents + rel->r_offset);
3580 /* Delete unnecessary JALR. */
3582 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len,
3586 /* Traverse all output sections and return the max alignment. */
3589 _bfd_riscv_get_max_alignment (asection *sec)
3591 unsigned int max_alignment_power = 0;
3594 for (o = sec->output_section->owner->sections; o != NULL; o = o->next)
3596 if (o->alignment_power > max_alignment_power)
3597 max_alignment_power = o->alignment_power;
3600 return (bfd_vma) 1 << max_alignment_power;
3603 /* Relax non-PIC global variable references. */
3606 _bfd_riscv_relax_lui (bfd *abfd,
3609 struct bfd_link_info *link_info,
3610 Elf_Internal_Rela *rel,
3612 bfd_vma max_alignment,
3613 bfd_vma reserve_size,
3615 riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
3616 bfd_boolean undefined_weak)
3618 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
3619 bfd_vma gp = riscv_global_pointer_value (link_info);
3620 int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
3622 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
3626 /* If gp and the symbol are in the same output section, which is not the
3627 abs section, then consider only that output section's alignment. */
3628 struct bfd_link_hash_entry *h =
3629 bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, FALSE, FALSE,
3631 if (h->u.def.section->output_section == sym_sec->output_section
3632 && sym_sec->output_section != bfd_abs_section_ptr)
3633 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
3636 /* Is the reference in range of x0 or gp?
3637 Valid gp range conservatively because of alignment issue. */
3639 || (VALID_ITYPE_IMM (symval)
3641 && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
3643 && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
3645 unsigned sym = ELFNN_R_SYM (rel->r_info);
3646 switch (ELFNN_R_TYPE (rel->r_info))
3648 case R_RISCV_LO12_I:
3651 /* Change the RS1 to zero. */
3652 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
3653 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
3654 bfd_put_32 (abfd, insn, contents + rel->r_offset);
3657 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
3660 case R_RISCV_LO12_S:
3663 /* Change the RS1 to zero. */
3664 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
3665 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
3666 bfd_put_32 (abfd, insn, contents + rel->r_offset);
3669 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
3673 /* We can delete the unnecessary LUI and reloc. */
3674 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
3676 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4,
3684 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
3685 account for this assuming page alignment at worst. In the presence of
3686 RELRO segment the linker aligns it by one page size, therefore sections
3687 after the segment can be moved more than one page. */
3690 && ELFNN_R_TYPE (rel->r_info) == R_RISCV_HI20
3691 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval))
3692 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval)
3693 + (link_info->relro ? 2 * ELF_MAXPAGESIZE
3694 : ELF_MAXPAGESIZE)))
3696 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
3697 bfd_vma lui = bfd_get_32 (abfd, contents + rel->r_offset);
3698 unsigned rd = ((unsigned)lui >> OP_SH_RD) & OP_MASK_RD;
3699 if (rd == 0 || rd == X_SP)
3702 lui = (lui & (OP_MASK_RD << OP_SH_RD)) | MATCH_C_LUI;
3703 bfd_put_32 (abfd, lui, contents + rel->r_offset);
3705 /* Replace the R_RISCV_HI20 reloc. */
3706 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_RVC_LUI);
3709 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2,
3716 /* Relax non-PIC TLS references. */
3719 _bfd_riscv_relax_tls_le (bfd *abfd,
3721 asection *sym_sec ATTRIBUTE_UNUSED,
3722 struct bfd_link_info *link_info,
3723 Elf_Internal_Rela *rel,
3725 bfd_vma max_alignment ATTRIBUTE_UNUSED,
3726 bfd_vma reserve_size ATTRIBUTE_UNUSED,
3728 riscv_pcgp_relocs *prcel_relocs ATTRIBUTE_UNUSED,
3729 bfd_boolean undefined_weak ATTRIBUTE_UNUSED)
3731 /* See if this symbol is in range of tp. */
3732 if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
3735 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
3736 switch (ELFNN_R_TYPE (rel->r_info))
3738 case R_RISCV_TPREL_LO12_I:
3739 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_I);
3742 case R_RISCV_TPREL_LO12_S:
3743 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_S);
3746 case R_RISCV_TPREL_HI20:
3747 case R_RISCV_TPREL_ADD:
3748 /* We can delete the unnecessary instruction and reloc. */
3749 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
3751 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info);
3758 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
3761 _bfd_riscv_relax_align (bfd *abfd, asection *sec,
3763 struct bfd_link_info *link_info,
3764 Elf_Internal_Rela *rel,
3766 bfd_vma max_alignment ATTRIBUTE_UNUSED,
3767 bfd_vma reserve_size ATTRIBUTE_UNUSED,
3768 bfd_boolean *again ATTRIBUTE_UNUSED,
3769 riscv_pcgp_relocs *pcrel_relocs ATTRIBUTE_UNUSED,
3770 bfd_boolean undefined_weak ATTRIBUTE_UNUSED)
3772 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
3773 bfd_vma alignment = 1, pos;
3774 while (alignment <= rel->r_addend)
3777 symval -= rel->r_addend;
3778 bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
3779 bfd_vma nop_bytes = aligned_addr - symval;
3781 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
3782 sec->sec_flg0 = TRUE;
3784 /* Make sure there are enough NOPs to actually achieve the alignment. */
3785 if (rel->r_addend < nop_bytes)
3788 (_("%pB(%pA+%#" PRIx64 "): %" PRId64 " bytes required for alignment "
3789 "to %" PRId64 "-byte boundary, but only %" PRId64 " present"),
3790 abfd, sym_sec, (uint64_t) rel->r_offset,
3791 (int64_t) nop_bytes, (int64_t) alignment, (int64_t) rel->r_addend);
3792 bfd_set_error (bfd_error_bad_value);
3796 /* Delete the reloc. */
3797 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
3799 /* If the number of NOPs is already correct, there's nothing to do. */
3800 if (nop_bytes == rel->r_addend)
3803 /* Write as many RISC-V NOPs as we need. */
3804 for (pos = 0; pos < (nop_bytes & -4); pos += 4)
3805 bfd_put_32 (abfd, RISCV_NOP, contents + rel->r_offset + pos);
3807 /* Write a final RVC NOP if need be. */
3808 if (nop_bytes % 4 != 0)
3809 bfd_put_16 (abfd, RVC_NOP, contents + rel->r_offset + pos);
3811 /* Delete the excess bytes. */
3812 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes,
3813 rel->r_addend - nop_bytes, link_info);
3816 /* Relax PC-relative references to GP-relative references. */
3819 _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
3822 struct bfd_link_info *link_info,
3823 Elf_Internal_Rela *rel,
3825 bfd_vma max_alignment,
3826 bfd_vma reserve_size,
3827 bfd_boolean *again ATTRIBUTE_UNUSED,
3828 riscv_pcgp_relocs *pcgp_relocs,
3829 bfd_boolean undefined_weak)
3831 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
3832 bfd_vma gp = riscv_global_pointer_value (link_info);
3834 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
3836 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
3837 * actual target address. */
3838 riscv_pcgp_hi_reloc hi_reloc;
3839 memset (&hi_reloc, 0, sizeof (hi_reloc));
3840 switch (ELFNN_R_TYPE (rel->r_info))
3842 case R_RISCV_PCREL_LO12_I:
3843 case R_RISCV_PCREL_LO12_S:
3845 /* If the %lo has an addend, it isn't for the label pointing at the
3846 hi part instruction, but rather for the symbol pointed at by the
3847 hi part instruction. So we must subtract it here for the lookup.
3848 It is still used below in the final symbol address. */
3849 bfd_vma hi_sec_off = symval - sec_addr (sym_sec) - rel->r_addend;
3850 riscv_pcgp_hi_reloc *hi = riscv_find_pcgp_hi_reloc (pcgp_relocs,
3854 riscv_record_pcgp_lo_reloc (pcgp_relocs, hi_sec_off);
3859 symval = hi_reloc.hi_addr;
3860 sym_sec = hi_reloc.sym_sec;
3862 /* We can not know whether the undefined weak symbol is referenced
3863 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
3864 we have to record the 'undefined_weak' flag when handling the
3865 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
3866 undefined_weak = hi_reloc.undefined_weak;
3870 case R_RISCV_PCREL_HI20:
3871 /* Mergeable symbols and code might later move out of range. */
3872 if (! undefined_weak
3873 && sym_sec->flags & (SEC_MERGE | SEC_CODE))
3876 /* If the cooresponding lo relocation has already been seen then it's not
3877 * safe to relax this relocation. */
3878 if (riscv_find_pcgp_lo_reloc (pcgp_relocs, rel->r_offset))
3889 /* If gp and the symbol are in the same output section, which is not the
3890 abs section, then consider only that output section's alignment. */
3891 struct bfd_link_hash_entry *h =
3892 bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, FALSE, FALSE,
3894 if (h->u.def.section->output_section == sym_sec->output_section
3895 && sym_sec->output_section != bfd_abs_section_ptr)
3896 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
3899 /* Is the reference in range of x0 or gp?
3900 Valid gp range conservatively because of alignment issue. */
3902 || (VALID_ITYPE_IMM (symval)
3904 && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
3906 && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
3908 unsigned sym = hi_reloc.hi_sym;
3909 switch (ELFNN_R_TYPE (rel->r_info))
3911 case R_RISCV_PCREL_LO12_I:
3914 /* Change the RS1 to zero, and then modify the relocation
3915 type to R_RISCV_LO12_I. */
3916 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
3917 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
3918 bfd_put_32 (abfd, insn, contents + rel->r_offset);
3919 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_LO12_I);
3920 rel->r_addend = hi_reloc.hi_addend;
3924 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
3925 rel->r_addend += hi_reloc.hi_addend;
3929 case R_RISCV_PCREL_LO12_S:
3932 /* Change the RS1 to zero, and then modify the relocation
3933 type to R_RISCV_LO12_S. */
3934 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
3935 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
3936 bfd_put_32 (abfd, insn, contents + rel->r_offset);
3937 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_LO12_S);
3938 rel->r_addend = hi_reloc.hi_addend;
3942 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
3943 rel->r_addend += hi_reloc.hi_addend;
3947 case R_RISCV_PCREL_HI20:
3948 riscv_record_pcgp_hi_reloc (pcgp_relocs,
3952 ELFNN_R_SYM(rel->r_info),
3955 /* We can delete the unnecessary AUIPC and reloc. */
3956 rel->r_info = ELFNN_R_INFO (0, R_RISCV_DELETE);
3968 /* Relax PC-relative references to GP-relative references. */
3971 _bfd_riscv_relax_delete (bfd *abfd,
3973 asection *sym_sec ATTRIBUTE_UNUSED,
3974 struct bfd_link_info *link_info,
3975 Elf_Internal_Rela *rel,
3976 bfd_vma symval ATTRIBUTE_UNUSED,
3977 bfd_vma max_alignment ATTRIBUTE_UNUSED,
3978 bfd_vma reserve_size ATTRIBUTE_UNUSED,
3979 bfd_boolean *again ATTRIBUTE_UNUSED,
3980 riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
3981 bfd_boolean undefined_weak ATTRIBUTE_UNUSED)
3983 if (!riscv_relax_delete_bytes(abfd, sec, rel->r_offset, rel->r_addend,
3986 rel->r_info = ELFNN_R_INFO(0, R_RISCV_NONE);
3990 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
3991 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
3992 disabled, handles code alignment directives. */
3995 _bfd_riscv_relax_section (bfd *abfd, asection *sec,
3996 struct bfd_link_info *info,
3999 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
4000 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
4001 struct bfd_elf_section_data *data = elf_section_data (sec);
4002 Elf_Internal_Rela *relocs;
4003 bfd_boolean ret = FALSE;
4005 bfd_vma max_alignment, reserve_size = 0;
4006 riscv_pcgp_relocs pcgp_relocs;
4010 if (bfd_link_relocatable (info)
4012 || (sec->flags & SEC_RELOC) == 0
4013 || sec->reloc_count == 0
4014 || (info->disable_target_specific_optimizations
4015 && info->relax_pass == 0))
4018 riscv_init_pcgp_relocs (&pcgp_relocs);
4020 /* Read this BFD's relocs if we haven't done so already. */
4022 relocs = data->relocs;
4023 else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
4024 info->keep_memory)))
4029 max_alignment = htab->max_alignment;
4030 if (max_alignment == (bfd_vma) -1)
4032 max_alignment = _bfd_riscv_get_max_alignment (sec);
4033 htab->max_alignment = max_alignment;
4037 max_alignment = _bfd_riscv_get_max_alignment (sec);
4039 /* Examine and consider relaxing each reloc. */
4040 for (i = 0; i < sec->reloc_count; i++)
4043 Elf_Internal_Rela *rel = relocs + i;
4044 relax_func_t relax_func;
4045 int type = ELFNN_R_TYPE (rel->r_info);
4048 bfd_boolean undefined_weak = FALSE;
4051 if (info->relax_pass == 0)
4053 if (type == R_RISCV_CALL || type == R_RISCV_CALL_PLT)
4054 relax_func = _bfd_riscv_relax_call;
4055 else if (type == R_RISCV_HI20
4056 || type == R_RISCV_LO12_I
4057 || type == R_RISCV_LO12_S)
4058 relax_func = _bfd_riscv_relax_lui;
4059 else if (!bfd_link_pic(info)
4060 && (type == R_RISCV_PCREL_HI20
4061 || type == R_RISCV_PCREL_LO12_I
4062 || type == R_RISCV_PCREL_LO12_S))
4063 relax_func = _bfd_riscv_relax_pc;
4064 else if (type == R_RISCV_TPREL_HI20
4065 || type == R_RISCV_TPREL_ADD
4066 || type == R_RISCV_TPREL_LO12_I
4067 || type == R_RISCV_TPREL_LO12_S)
4068 relax_func = _bfd_riscv_relax_tls_le;
4072 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4073 if (i == sec->reloc_count - 1
4074 || ELFNN_R_TYPE ((rel + 1)->r_info) != R_RISCV_RELAX
4075 || rel->r_offset != (rel + 1)->r_offset)
4078 /* Skip over the R_RISCV_RELAX. */
4081 else if (info->relax_pass == 1 && type == R_RISCV_DELETE)
4082 relax_func = _bfd_riscv_relax_delete;
4083 else if (info->relax_pass == 2 && type == R_RISCV_ALIGN)
4084 relax_func = _bfd_riscv_relax_align;
4088 data->relocs = relocs;
4090 /* Read this BFD's contents if we haven't done so already. */
4091 if (!data->this_hdr.contents
4092 && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
4095 /* Read this BFD's symbols if we haven't done so already. */
4096 if (symtab_hdr->sh_info != 0
4097 && !symtab_hdr->contents
4098 && !(symtab_hdr->contents =
4099 (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
4100 symtab_hdr->sh_info,
4101 0, NULL, NULL, NULL)))
4104 /* Get the value of the symbol referred to by the reloc. */
4105 if (ELFNN_R_SYM (rel->r_info) < symtab_hdr->sh_info)
4107 /* A local symbol. */
4108 Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents
4109 + ELFNN_R_SYM (rel->r_info));
4110 reserve_size = (isym->st_size - rel->r_addend) > isym->st_size
4111 ? 0 : isym->st_size - rel->r_addend;
4113 if (isym->st_shndx == SHN_UNDEF)
4114 sym_sec = sec, symval = rel->r_offset;
4117 BFD_ASSERT (isym->st_shndx < elf_numsections (abfd));
4118 sym_sec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
4120 /* The purpose of this code is unknown. It breaks linker scripts
4121 for embedded development that place sections at address zero.
4122 This code is believed to be unnecessary. Disabling it but not
4123 yet removing it, in case something breaks. */
4124 if (sec_addr (sym_sec) == 0)
4127 symval = isym->st_value;
4129 symtype = ELF_ST_TYPE (isym->st_info);
4134 struct elf_link_hash_entry *h;
4136 indx = ELFNN_R_SYM (rel->r_info) - symtab_hdr->sh_info;
4137 h = elf_sym_hashes (abfd)[indx];
4139 while (h->root.type == bfd_link_hash_indirect
4140 || h->root.type == bfd_link_hash_warning)
4141 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4143 if (h->root.type == bfd_link_hash_undefweak
4144 && (relax_func == _bfd_riscv_relax_lui
4145 || relax_func == _bfd_riscv_relax_pc))
4147 /* For the lui and auipc relaxations, since the symbol
4148 value of an undefined weak symbol is always be zero,
4149 we can optimize the patterns into a single LI/MV/ADDI
4152 Note that, creating shared libraries and pie output may
4153 break the rule above. Fortunately, since we do not relax
4154 pc relocs when creating shared libraries and pie output,
4155 and the absolute address access for R_RISCV_HI20 isn't
4156 allowed when "-fPIC" is set, the problem of creating shared
4157 libraries can not happen currently. Once we support the
4158 auipc relaxations when creating shared libraries, then we will
4159 need the more rigorous checking for this optimization. */
4160 undefined_weak = TRUE;
4163 /* This line has to match the check in riscv_elf_relocate_section
4164 in the R_RISCV_CALL[_PLT] case. */
4165 if (bfd_link_pic (info) && h->plt.offset != MINUS_ONE)
4167 sym_sec = htab->elf.splt;
4168 symval = h->plt.offset;
4170 else if (undefined_weak)
4173 sym_sec = bfd_und_section_ptr;
4175 else if ((h->root.type == bfd_link_hash_defined
4176 || h->root.type == bfd_link_hash_defweak)
4177 && h->root.u.def.section != NULL
4178 && h->root.u.def.section->output_section != NULL)
4180 symval = h->root.u.def.value;
4181 sym_sec = h->root.u.def.section;
4186 if (h->type != STT_FUNC)
4188 (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_addend;
4192 if (sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE
4193 && (sym_sec->flags & SEC_MERGE))
4195 /* At this stage in linking, no SEC_MERGE symbol has been
4196 adjusted, so all references to such symbols need to be
4197 passed through _bfd_merged_section_offset. (Later, in
4198 relocate_section, all SEC_MERGE symbols *except* for
4199 section symbols have been adjusted.)
4201 gas may reduce relocations against symbols in SEC_MERGE
4202 sections to a relocation against the section symbol when
4203 the original addend was zero. When the reloc is against
4204 a section symbol we should include the addend in the
4205 offset passed to _bfd_merged_section_offset, since the
4206 location of interest is the original symbol. On the
4207 other hand, an access to "sym+addend" where "sym" is not
4208 a section symbol should not include the addend; Such an
4209 access is presumed to be an offset from "sym"; The
4210 location of interest is just "sym". */
4211 if (symtype == STT_SECTION)
4212 symval += rel->r_addend;
4214 symval = _bfd_merged_section_offset (abfd, &sym_sec,
4215 elf_section_data (sym_sec)->sec_info,
4218 if (symtype != STT_SECTION)
4219 symval += rel->r_addend;
4222 symval += rel->r_addend;
4224 symval += sec_addr (sym_sec);
4226 if (!relax_func (abfd, sec, sym_sec, info, rel, symval,
4227 max_alignment, reserve_size, again,
4228 &pcgp_relocs, undefined_weak))
4235 if (relocs != data->relocs)
4237 riscv_free_pcgp_relocs(&pcgp_relocs, abfd, sec);
4243 # define PRSTATUS_SIZE 204
4244 # define PRSTATUS_OFFSET_PR_CURSIG 12
4245 # define PRSTATUS_OFFSET_PR_PID 24
4246 # define PRSTATUS_OFFSET_PR_REG 72
4247 # define ELF_GREGSET_T_SIZE 128
4248 # define PRPSINFO_SIZE 128
4249 # define PRPSINFO_OFFSET_PR_PID 16
4250 # define PRPSINFO_OFFSET_PR_FNAME 32
4251 # define PRPSINFO_OFFSET_PR_PSARGS 48
4253 # define PRSTATUS_SIZE 376
4254 # define PRSTATUS_OFFSET_PR_CURSIG 12
4255 # define PRSTATUS_OFFSET_PR_PID 32
4256 # define PRSTATUS_OFFSET_PR_REG 112
4257 # define ELF_GREGSET_T_SIZE 256
4258 # define PRPSINFO_SIZE 136
4259 # define PRPSINFO_OFFSET_PR_PID 24
4260 # define PRPSINFO_OFFSET_PR_FNAME 40
4261 # define PRPSINFO_OFFSET_PR_PSARGS 56
4264 /* Support for core dump NOTE sections. */
4267 riscv_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
4269 switch (note->descsz)
4274 case PRSTATUS_SIZE: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4276 elf_tdata (abfd)->core->signal
4277 = bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
4280 elf_tdata (abfd)->core->lwpid
4281 = bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
4285 /* Make a ".reg/999" section. */
4286 return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
4287 note->descpos + PRSTATUS_OFFSET_PR_REG);
4291 riscv_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
4293 switch (note->descsz)
4298 case PRPSINFO_SIZE: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4300 elf_tdata (abfd)->core->pid
4301 = bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
4304 elf_tdata (abfd)->core->program = _bfd_elfcore_strndup
4305 (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME, 16);
4308 elf_tdata (abfd)->core->command = _bfd_elfcore_strndup
4309 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS, 80);
4313 /* Note that for some reason, a spurious space is tacked
4314 onto the end of the args in some (at least one anyway)
4315 implementations, so strip it off if it exists. */
4318 char *command = elf_tdata (abfd)->core->command;
4319 int n = strlen (command);
4321 if (0 < n && command[n - 1] == ' ')
4322 command[n - 1] = '\0';
4328 /* Set the right mach type. */
4330 riscv_elf_object_p (bfd *abfd)
4332 /* There are only two mach types in RISCV currently. */
4333 if (strcmp (abfd->xvec->name, "elf32-littleriscv") == 0)
4334 bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv32);
4336 bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv64);
4341 /* Determine whether an object attribute tag takes an integer, a
4345 riscv_elf_obj_attrs_arg_type (int tag)
4347 return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
4350 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4351 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4353 #define elf_backend_reloc_type_class riscv_reloc_type_class
4355 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4356 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4357 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4358 #define bfd_elfNN_bfd_merge_private_bfd_data \
4359 _bfd_riscv_elf_merge_private_bfd_data
4361 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4362 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4363 #define elf_backend_check_relocs riscv_elf_check_relocs
4364 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4365 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4366 #define elf_backend_relocate_section riscv_elf_relocate_section
4367 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4368 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4369 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4370 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4371 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4372 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4373 #define elf_backend_object_p riscv_elf_object_p
4374 #define elf_info_to_howto_rel NULL
4375 #define elf_info_to_howto riscv_info_to_howto_rela
4376 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4377 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
4379 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4381 #define elf_backend_can_gc_sections 1
4382 #define elf_backend_can_refcount 1
4383 #define elf_backend_want_got_plt 1
4384 #define elf_backend_plt_readonly 1
4385 #define elf_backend_plt_alignment 4
4386 #define elf_backend_want_plt_sym 1
4387 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4388 #define elf_backend_want_dynrelro 1
4389 #define elf_backend_rela_normal 1
4390 #define elf_backend_default_execstack 0
4392 #undef elf_backend_obj_attrs_vendor
4393 #define elf_backend_obj_attrs_vendor "riscv"
4394 #undef elf_backend_obj_attrs_arg_type
4395 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4396 #undef elf_backend_obj_attrs_section_type
4397 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4398 #undef elf_backend_obj_attrs_section
4399 #define elf_backend_obj_attrs_section ".riscv.attributes"
4401 #include "elfNN-target.h"