1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2021 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
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 #include "cpu-riscv.h"
42 /* Internal relocations used exclusively by the relaxation pass. */
43 #define R_RISCV_DELETE (R_RISCV_max + 1)
47 #define MINUS_ONE ((bfd_vma)0 - 1)
49 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
51 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
53 /* The name of the dynamic interpreter. This is put in the .interp
56 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
57 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
59 #define ELF_ARCH bfd_arch_riscv
60 #define ELF_TARGET_ID RISCV_ELF_DATA
61 #define ELF_MACHINE_CODE EM_RISCV
62 #define ELF_MAXPAGESIZE 0x1000
63 #define ELF_COMMONPAGESIZE 0x1000
65 /* RISC-V ELF linker hash entry. */
67 struct riscv_elf_link_hash_entry
69 struct elf_link_hash_entry elf
;
79 #define riscv_elf_hash_entry(ent) \
80 ((struct riscv_elf_link_hash_entry *) (ent))
82 struct _bfd_riscv_elf_obj_tdata
84 struct elf_obj_tdata root
;
86 /* tls_type for each local got entry. */
87 char *local_got_tls_type
;
90 #define _bfd_riscv_elf_tdata(abfd) \
91 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
93 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
94 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
96 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
97 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
98 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
100 #define is_riscv_elf(bfd) \
101 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
102 && elf_tdata (bfd) != NULL \
103 && elf_object_id (bfd) == RISCV_ELF_DATA)
106 elfNN_riscv_mkobject (bfd
*abfd
)
108 return bfd_elf_allocate_object (abfd
,
109 sizeof (struct _bfd_riscv_elf_obj_tdata
),
113 #include "elf/common.h"
114 #include "elf/internal.h"
116 struct riscv_elf_link_hash_table
118 struct elf_link_hash_table elf
;
120 /* Short-cuts to get to dynamic linker sections. */
123 /* The max alignment of output sections. */
124 bfd_vma max_alignment
;
126 /* Used by local STT_GNU_IFUNC symbols. */
127 htab_t loc_hash_table
;
128 void * loc_hash_memory
;
130 /* The index of the last unused .rel.iplt slot. */
131 bfd_vma last_iplt_index
;
133 /* Re-run the relaxations from relax pass 0 if TRUE. */
137 /* Instruction access functions. */
138 #define riscv_get_insn(bits, ptr) \
139 ((bits) == 16 ? bfd_getl16 (ptr) \
140 : (bits) == 32 ? bfd_getl32 (ptr) \
141 : (bits) == 64 ? bfd_getl64 (ptr) \
142 : (abort (), (bfd_vma) - 1))
143 #define riscv_put_insn(bits, val, ptr) \
144 ((bits) == 16 ? bfd_putl16 (val, ptr) \
145 : (bits) == 32 ? bfd_putl32 (val, ptr) \
146 : (bits) == 64 ? bfd_putl64 (val, ptr) \
147 : (abort (), (void) 0))
149 /* Get the RISC-V ELF linker hash table from a link_info structure. */
150 #define riscv_elf_hash_table(p) \
151 ((is_elf_hash_table ((p)->hash) \
152 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
153 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
156 riscv_info_to_howto_rela (bfd
*abfd
,
158 Elf_Internal_Rela
*dst
)
160 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
161 return cache_ptr
->howto
!= NULL
;
165 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
167 const struct elf_backend_data
*bed
;
170 bed
= get_elf_backend_data (abfd
);
171 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
172 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
175 /* Return true if a relocation is modifying an instruction. */
178 riscv_is_insn_reloc (const reloc_howto_type
*howto
)
180 /* Heuristic: A multibyte destination with a nontrivial mask
182 return (howto
->bitsize
> 8
183 && howto
->dst_mask
!= 0
184 && ~(howto
->dst_mask
| (howto
->bitsize
< sizeof(bfd_vma
) * CHAR_BIT
185 ? (MINUS_ONE
<< howto
->bitsize
) : (bfd_vma
)0)) != 0);
189 #define PLT_HEADER_INSNS 8
190 #define PLT_ENTRY_INSNS 4
191 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
192 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
193 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
194 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
195 the other is used for link map. Other targets also reserve one more
196 entry used for runtime profile? */
197 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
199 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
202 # define MATCH_LREG MATCH_LW
204 # define MATCH_LREG MATCH_LD
207 /* Generate a PLT header. */
210 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
213 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
214 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
216 /* RVE has no t3 register, so this won't work, and is not supported. */
217 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
219 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
224 /* auipc t2, %hi(.got.plt)
225 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
226 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
227 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
228 addi t0, t2, %lo(.got.plt) # &.got.plt
229 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
230 l[w|d] t0, PTRSIZE(t0) # link map
233 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
234 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
235 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
236 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
237 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
238 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
239 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
240 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
245 /* Generate a PLT entry. */
248 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
251 /* RVE has no t3 register, so this won't work, and is not supported. */
252 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
254 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
259 /* auipc t3, %hi(.got.plt entry)
260 l[w|d] t3, %lo(.got.plt entry)(t3)
264 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
265 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
266 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
267 entry
[3] = RISCV_NOP
;
272 /* Create an entry in an RISC-V ELF linker hash table. */
274 static struct bfd_hash_entry
*
275 link_hash_newfunc (struct bfd_hash_entry
*entry
,
276 struct bfd_hash_table
*table
, const char *string
)
278 /* Allocate the structure if it has not already been allocated by a
283 bfd_hash_allocate (table
,
284 sizeof (struct riscv_elf_link_hash_entry
));
289 /* Call the allocation method of the superclass. */
290 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
293 struct riscv_elf_link_hash_entry
*eh
;
295 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
296 eh
->tls_type
= GOT_UNKNOWN
;
302 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
303 for local symbol so that we can handle local STT_GNU_IFUNC symbols
304 as global symbol. We reuse indx and dynstr_index for local symbol
305 hash since they aren't used by global symbols in this backend. */
308 riscv_elf_local_htab_hash (const void *ptr
)
310 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) ptr
;
311 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
314 /* Compare local hash entries. */
317 riscv_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
319 struct elf_link_hash_entry
*h1
= (struct elf_link_hash_entry
*) ptr1
;
320 struct elf_link_hash_entry
*h2
= (struct elf_link_hash_entry
*) ptr2
;
322 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
325 /* Find and/or create a hash entry for local symbol. */
327 static struct elf_link_hash_entry
*
328 riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table
*htab
,
329 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
332 struct riscv_elf_link_hash_entry eh
, *ret
;
333 asection
*sec
= abfd
->sections
;
334 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
335 ELFNN_R_SYM (rel
->r_info
));
338 eh
.elf
.indx
= sec
->id
;
339 eh
.elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
340 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &eh
, h
,
341 create
? INSERT
: NO_INSERT
);
348 ret
= (struct riscv_elf_link_hash_entry
*) *slot
;
352 ret
= (struct riscv_elf_link_hash_entry
*)
353 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
354 sizeof (struct riscv_elf_link_hash_entry
));
357 memset (ret
, 0, sizeof (*ret
));
358 ret
->elf
.indx
= sec
->id
;
359 ret
->elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
360 ret
->elf
.dynindx
= -1;
366 /* Destroy a RISC-V elf linker hash table. */
369 riscv_elf_link_hash_table_free (bfd
*obfd
)
371 struct riscv_elf_link_hash_table
*ret
372 = (struct riscv_elf_link_hash_table
*) obfd
->link
.hash
;
374 if (ret
->loc_hash_table
)
375 htab_delete (ret
->loc_hash_table
);
376 if (ret
->loc_hash_memory
)
377 objalloc_free ((struct objalloc
*) ret
->loc_hash_memory
);
379 _bfd_elf_link_hash_table_free (obfd
);
382 /* Create a RISC-V ELF linker hash table. */
384 static struct bfd_link_hash_table
*
385 riscv_elf_link_hash_table_create (bfd
*abfd
)
387 struct riscv_elf_link_hash_table
*ret
;
388 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
390 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
394 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
395 sizeof (struct riscv_elf_link_hash_entry
),
402 ret
->max_alignment
= (bfd_vma
) -1;
403 ret
->restart_relax
= false;
405 /* Create hash table for local ifunc. */
406 ret
->loc_hash_table
= htab_try_create (1024,
407 riscv_elf_local_htab_hash
,
408 riscv_elf_local_htab_eq
,
410 ret
->loc_hash_memory
= objalloc_create ();
411 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
413 riscv_elf_link_hash_table_free (abfd
);
416 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
418 return &ret
->elf
.root
;
421 /* Create the .got section. */
424 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
428 struct elf_link_hash_entry
*h
;
429 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
430 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
432 /* This function may be called more than once. */
433 if (htab
->sgot
!= NULL
)
436 flags
= bed
->dynamic_sec_flags
;
438 s
= bfd_make_section_anyway_with_flags (abfd
,
439 (bed
->rela_plts_and_copies_p
440 ? ".rela.got" : ".rel.got"),
441 (bed
->dynamic_sec_flags
444 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
448 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
450 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
454 /* The first bit of the global offset table is the header. */
455 s
->size
+= bed
->got_header_size
;
457 if (bed
->want_got_plt
)
459 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
461 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
465 /* Reserve room for the header. */
466 s
->size
+= GOTPLT_HEADER_SIZE
;
469 if (bed
->want_got_sym
)
471 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
472 section. We don't do this in the linker script because we don't want
473 to define the symbol if we are not creating a global offset
475 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
476 "_GLOBAL_OFFSET_TABLE_");
477 elf_hash_table (info
)->hgot
= h
;
485 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
486 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
490 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
491 struct bfd_link_info
*info
)
493 struct riscv_elf_link_hash_table
*htab
;
495 htab
= riscv_elf_hash_table (info
);
496 BFD_ASSERT (htab
!= NULL
);
498 if (!riscv_elf_create_got_section (dynobj
, info
))
501 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
504 if (!bfd_link_pic (info
))
506 /* Technically, this section doesn't have contents. It is used as the
507 target of TLS copy relocs, to copy TLS data from shared libraries into
508 the executable. However, if we don't mark it as loadable, then it
509 matches the IS_TBSS test in ldlang.c, and there is no run-time address
510 space allocated for it even though it has SEC_ALLOC. That test is
511 correct for .tbss, but not correct for this section. There is also
512 a second problem that having a section with no contents can only work
513 if it comes after all sections with contents in the same segment,
514 but the linker script does not guarantee that. This is just mixed in
515 with other .tdata.* sections. We can fix both problems by lying and
516 saying that there are contents. This section is expected to be small
517 so this should not cause a significant extra program startup cost. */
519 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
520 (SEC_ALLOC
| SEC_THREAD_LOCAL
521 | SEC_LOAD
| SEC_DATA
523 | SEC_LINKER_CREATED
));
526 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
527 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
533 /* Copy the extra info we tack onto an elf_link_hash_entry. */
536 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
537 struct elf_link_hash_entry
*dir
,
538 struct elf_link_hash_entry
*ind
)
540 struct riscv_elf_link_hash_entry
*edir
, *eind
;
542 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
543 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
545 if (ind
->root
.type
== bfd_link_hash_indirect
546 && dir
->got
.refcount
<= 0)
548 edir
->tls_type
= eind
->tls_type
;
549 eind
->tls_type
= GOT_UNKNOWN
;
551 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
555 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
556 unsigned long symndx
, char tls_type
)
558 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
560 *new_tls_type
|= tls_type
;
561 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
563 (*_bfd_error_handler
)
564 (_("%pB: `%s' accessed both as normal and thread local symbol"),
565 abfd
, h
? h
->root
.root
.string
: "<local>");
572 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
573 struct elf_link_hash_entry
*h
, long symndx
)
575 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
576 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
578 if (htab
->elf
.sgot
== NULL
)
580 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
586 h
->got
.refcount
+= 1;
590 /* This is a global offset table entry for a local symbol. */
591 if (elf_local_got_refcounts (abfd
) == NULL
)
593 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
594 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
596 _bfd_riscv_elf_local_got_tls_type (abfd
)
597 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
599 elf_local_got_refcounts (abfd
) [symndx
] += 1;
605 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
607 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
609 /* We propably can improve the information to tell users that they
610 should be recompile the code with -fPIC or -fPIE, just like what
612 (*_bfd_error_handler
)
613 (_("%pB: relocation %s against `%s' can not be used when making a shared "
614 "object; recompile with -fPIC"),
615 abfd
, r
? r
->name
: _("<unknown>"),
616 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
617 bfd_set_error (bfd_error_bad_value
);
621 /* Look through the relocs for a section during the first phase, and
622 allocate space in the global offset table or procedure linkage
626 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
627 asection
*sec
, const Elf_Internal_Rela
*relocs
)
629 struct riscv_elf_link_hash_table
*htab
;
630 Elf_Internal_Shdr
*symtab_hdr
;
631 struct elf_link_hash_entry
**sym_hashes
;
632 const Elf_Internal_Rela
*rel
;
633 asection
*sreloc
= NULL
;
635 if (bfd_link_relocatable (info
))
638 htab
= riscv_elf_hash_table (info
);
639 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
640 sym_hashes
= elf_sym_hashes (abfd
);
642 if (htab
->elf
.dynobj
== NULL
)
643 htab
->elf
.dynobj
= abfd
;
645 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
648 unsigned int r_symndx
;
649 struct elf_link_hash_entry
*h
;
651 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
652 r_type
= ELFNN_R_TYPE (rel
->r_info
);
654 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
656 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
661 if (r_symndx
< symtab_hdr
->sh_info
)
663 /* A local symbol. */
664 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
669 /* Check relocation against local STT_GNU_IFUNC symbol. */
670 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
672 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
676 /* Fake STT_GNU_IFUNC global symbol. */
677 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
679 h
->type
= STT_GNU_IFUNC
;
683 h
->root
.type
= bfd_link_hash_defined
;
690 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
691 while (h
->root
.type
== bfd_link_hash_indirect
692 || h
->root
.type
== bfd_link_hash_warning
)
693 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
703 case R_RISCV_CALL_PLT
:
705 case R_RISCV_GOT_HI20
:
706 case R_RISCV_PCREL_HI20
:
707 /* Create the ifunc sections, iplt and ipltgot, for static
709 if (h
->type
== STT_GNU_IFUNC
710 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
718 /* It is referenced by a non-shared object. */
724 case R_RISCV_TLS_GD_HI20
:
725 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
726 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
730 case R_RISCV_TLS_GOT_HI20
:
731 if (bfd_link_pic (info
))
732 info
->flags
|= DF_STATIC_TLS
;
733 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
734 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
738 case R_RISCV_GOT_HI20
:
739 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
740 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
745 case R_RISCV_CALL_PLT
:
746 /* These symbol requires a procedure linkage table entry.
747 We actually build the entry in adjust_dynamic_symbol,
748 because these might be a case of linking PIC code without
749 linking in any dynamic objects, in which case we don't
750 need to generate a procedure linkage table after all. */
752 /* If it is a local symbol, then we resolve it directly
753 without creating a PLT entry. */
758 h
->plt
.refcount
+= 1;
761 case R_RISCV_PCREL_HI20
:
763 && h
->type
== STT_GNU_IFUNC
)
766 h
->pointer_equality_needed
= 1;
768 /* We don't use the PCREL_HI20 in the data section,
769 so we always need the plt when it refers to
771 h
->plt
.refcount
+= 1;
777 case R_RISCV_RVC_BRANCH
:
778 case R_RISCV_RVC_JUMP
:
779 /* In shared libraries and pie, these relocs are known
781 if (bfd_link_pic (info
))
785 case R_RISCV_TPREL_HI20
:
786 if (!bfd_link_executable (info
))
787 return bad_static_reloc (abfd
, r_type
, h
);
789 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
793 if (bfd_link_pic (info
))
794 return bad_static_reloc (abfd
, r_type
, h
);
798 case R_RISCV_JUMP_SLOT
:
799 case R_RISCV_RELATIVE
:
807 && (!bfd_link_pic (info
)
808 || h
->type
== STT_GNU_IFUNC
))
810 /* This reloc might not bind locally. */
812 h
->pointer_equality_needed
= 1;
815 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
817 /* We may need a .plt entry if the symbol is a function
818 defined in a shared lib or is a function referenced
819 from the code or read-only section. */
820 h
->plt
.refcount
+= 1;
824 /* If we are creating a shared library, and this is a reloc
825 against a global symbol, or a non PC relative reloc
826 against a local symbol, then we need to copy the reloc
827 into the shared library. However, if we are linking with
828 -Bsymbolic, we do not need to copy a reloc against a
829 global symbol which is defined in an object we are
830 including in the link (i.e., DEF_REGULAR is set). At
831 this point we have not seen all the input files, so it is
832 possible that DEF_REGULAR is not set now but will be set
833 later (it is never cleared). In case of a weak definition,
834 DEF_REGULAR may be cleared later by a strong definition in
835 a shared library. We account for that possibility below by
836 storing information in the relocs_copied field of the hash
837 table entry. A similar situation occurs when creating
838 shared libraries and symbol visibility changes render the
841 If on the other hand, we are creating an executable, we
842 may need to keep relocations for symbols satisfied by a
843 dynamic library if we manage to avoid copy relocs for the
846 Generate dynamic pointer relocation against STT_GNU_IFUNC
847 symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
848 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
850 if ((bfd_link_pic (info
)
851 && (sec
->flags
& SEC_ALLOC
) != 0
852 && ((r
!= NULL
&& !r
->pc_relative
)
855 || h
->root
.type
== bfd_link_hash_defweak
856 || !h
->def_regular
))))
857 || (!bfd_link_pic (info
)
858 && (sec
->flags
& SEC_ALLOC
) != 0
860 && (h
->root
.type
== bfd_link_hash_defweak
862 || (!bfd_link_pic (info
)
864 && h
->type
== STT_GNU_IFUNC
865 && (sec
->flags
& SEC_CODE
) == 0))
867 struct elf_dyn_relocs
*p
;
868 struct elf_dyn_relocs
**head
;
870 /* When creating a shared object, we must copy these
871 relocs into the output file. We create a reloc
872 section in dynobj and make room for the reloc. */
875 sreloc
= _bfd_elf_make_dynamic_reloc_section
876 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
877 abfd
, /*rela?*/ true);
883 /* If this is a global symbol, we count the number of
884 relocations we need for this symbol. */
886 head
= &h
->dyn_relocs
;
889 /* Track dynamic relocs needed for local syms too.
890 We really need local syms available to do this
895 Elf_Internal_Sym
*isym
;
897 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
902 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
906 vpp
= &elf_section_data (s
)->local_dynrel
;
907 head
= (struct elf_dyn_relocs
**) vpp
;
911 if (p
== NULL
|| p
->sec
!= sec
)
913 size_t amt
= sizeof *p
;
914 p
= ((struct elf_dyn_relocs
*)
915 bfd_alloc (htab
->elf
.dynobj
, amt
));
926 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
931 case R_RISCV_GNU_VTINHERIT
:
932 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
936 case R_RISCV_GNU_VTENTRY
:
937 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
950 riscv_elf_gc_mark_hook (asection
*sec
,
951 struct bfd_link_info
*info
,
952 Elf_Internal_Rela
*rel
,
953 struct elf_link_hash_entry
*h
,
954 Elf_Internal_Sym
*sym
)
957 switch (ELFNN_R_TYPE (rel
->r_info
))
959 case R_RISCV_GNU_VTINHERIT
:
960 case R_RISCV_GNU_VTENTRY
:
964 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
967 /* Adjust a symbol defined by a dynamic object and referenced by a
968 regular object. The current definition is in some section of the
969 dynamic object, but we're not including those sections. We have to
970 change the definition to something the rest of the link can
974 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
975 struct elf_link_hash_entry
*h
)
977 struct riscv_elf_link_hash_table
*htab
;
978 struct riscv_elf_link_hash_entry
* eh
;
982 htab
= riscv_elf_hash_table (info
);
983 BFD_ASSERT (htab
!= NULL
);
985 dynobj
= htab
->elf
.dynobj
;
987 /* Make sure we know what is going on here. */
988 BFD_ASSERT (dynobj
!= NULL
990 || h
->type
== STT_GNU_IFUNC
994 && !h
->def_regular
)));
996 /* If this is a function, put it in the procedure linkage table. We
997 will fill in the contents of the procedure linkage table later
998 (although we could actually do it here). */
999 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
1001 if (h
->plt
.refcount
<= 0
1002 || (h
->type
!= STT_GNU_IFUNC
1003 && (SYMBOL_CALLS_LOCAL (info
, h
)
1004 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1005 && h
->root
.type
== bfd_link_hash_undefweak
))))
1007 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1008 input file, but the symbol was never referred to by a dynamic
1009 object, or if all references were garbage collected. In such
1010 a case, we don't actually need to build a PLT entry. */
1011 h
->plt
.offset
= (bfd_vma
) -1;
1018 h
->plt
.offset
= (bfd_vma
) -1;
1020 /* If this is a weak symbol, and there is a real definition, the
1021 processor independent code will have arranged for us to see the
1022 real definition first, and we can just use the same value. */
1023 if (h
->is_weakalias
)
1025 struct elf_link_hash_entry
*def
= weakdef (h
);
1026 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1027 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1028 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1032 /* This is a reference to a symbol defined by a dynamic object which
1033 is not a function. */
1035 /* If we are creating a shared library, we must presume that the
1036 only references to the symbol are via the global offset table.
1037 For such cases we need not do anything here; the relocations will
1038 be handled correctly by relocate_section. */
1039 if (bfd_link_pic (info
))
1042 /* If there are no references to this symbol that do not use the
1043 GOT, we don't need to generate a copy reloc. */
1044 if (!h
->non_got_ref
)
1047 /* If -z nocopyreloc was given, we won't generate them either. */
1048 if (info
->nocopyreloc
)
1054 /* If we don't find any dynamic relocs in read-only sections, then
1055 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1056 if (!_bfd_elf_readonly_dynrelocs (h
))
1062 /* We must allocate the symbol in our .dynbss section, which will
1063 become part of the .bss section of the executable. There will be
1064 an entry for this symbol in the .dynsym section. The dynamic
1065 object will contain position independent code, so all references
1066 from the dynamic object to this symbol will go through the global
1067 offset table. The dynamic linker will use the .dynsym entry to
1068 determine the address it must put in the global offset table, so
1069 both the dynamic object and the regular object will refer to the
1070 same memory location for the variable. */
1072 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1073 to copy the initial value out of the dynamic object and into the
1074 runtime process image. We need to remember the offset into the
1075 .rel.bss section we are going to use. */
1076 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1077 if (eh
->tls_type
& ~GOT_NORMAL
)
1079 s
= htab
->sdyntdata
;
1080 srel
= htab
->elf
.srelbss
;
1082 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1084 s
= htab
->elf
.sdynrelro
;
1085 srel
= htab
->elf
.sreldynrelro
;
1089 s
= htab
->elf
.sdynbss
;
1090 srel
= htab
->elf
.srelbss
;
1092 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1094 srel
->size
+= sizeof (ElfNN_External_Rela
);
1098 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1101 /* Allocate space in .plt, .got and associated reloc sections for
1105 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1107 struct bfd_link_info
*info
;
1108 struct riscv_elf_link_hash_table
*htab
;
1109 struct elf_dyn_relocs
*p
;
1111 if (h
->root
.type
== bfd_link_hash_indirect
)
1114 info
= (struct bfd_link_info
*) inf
;
1115 htab
= riscv_elf_hash_table (info
);
1116 BFD_ASSERT (htab
!= NULL
);
1118 /* When we are generating pde, make sure gp symbol is output as a
1119 dynamic symbol. Then ld.so can set the gp register earlier, before
1120 resolving the ifunc. */
1121 if (!bfd_link_pic (info
)
1122 && htab
->elf
.dynamic_sections_created
1123 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1124 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1127 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1128 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1129 if they are defined and referenced in a non-shared object. */
1130 if (h
->type
== STT_GNU_IFUNC
1133 else if (htab
->elf
.dynamic_sections_created
1134 && h
->plt
.refcount
> 0)
1136 /* Make sure this symbol is output as a dynamic symbol.
1137 Undefined weak syms won't yet be marked as dynamic. */
1138 if (h
->dynindx
== -1
1139 && !h
->forced_local
)
1141 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1145 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1147 asection
*s
= htab
->elf
.splt
;
1150 s
->size
= PLT_HEADER_SIZE
;
1152 h
->plt
.offset
= s
->size
;
1154 /* Make room for this entry. */
1155 s
->size
+= PLT_ENTRY_SIZE
;
1157 /* We also need to make an entry in the .got.plt section. */
1158 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1160 /* We also need to make an entry in the .rela.plt section. */
1161 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1163 /* If this symbol is not defined in a regular file, and we are
1164 not generating a shared library, then set the symbol to this
1165 location in the .plt. This is required to make function
1166 pointers compare as equal between the normal executable and
1167 the shared library. */
1168 if (! bfd_link_pic (info
)
1171 h
->root
.u
.def
.section
= s
;
1172 h
->root
.u
.def
.value
= h
->plt
.offset
;
1177 h
->plt
.offset
= (bfd_vma
) -1;
1183 h
->plt
.offset
= (bfd_vma
) -1;
1187 if (h
->got
.refcount
> 0)
1191 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1193 /* Make sure this symbol is output as a dynamic symbol.
1194 Undefined weak syms won't yet be marked as dynamic. */
1195 if (h
->dynindx
== -1
1196 && !h
->forced_local
)
1198 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1203 h
->got
.offset
= s
->size
;
1204 dyn
= htab
->elf
.dynamic_sections_created
;
1205 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1207 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1208 if (tls_type
& GOT_TLS_GD
)
1210 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1211 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1214 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1215 if (tls_type
& GOT_TLS_IE
)
1217 s
->size
+= RISCV_ELF_WORD_BYTES
;
1218 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1223 s
->size
+= RISCV_ELF_WORD_BYTES
;
1224 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1225 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1226 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1230 h
->got
.offset
= (bfd_vma
) -1;
1232 if (h
->dyn_relocs
== NULL
)
1235 /* In the shared -Bsymbolic case, discard space allocated for
1236 dynamic pc-relative relocs against symbols which turn out to be
1237 defined in regular objects. For the normal shared case, discard
1238 space for pc-relative relocs that have become local due to symbol
1239 visibility changes. */
1241 if (bfd_link_pic (info
))
1243 if (SYMBOL_CALLS_LOCAL (info
, h
))
1245 struct elf_dyn_relocs
**pp
;
1247 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1249 p
->count
-= p
->pc_count
;
1258 /* Also discard relocs on undefined weak syms with non-default
1260 if (h
->dyn_relocs
!= NULL
1261 && h
->root
.type
== bfd_link_hash_undefweak
)
1263 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1264 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1265 h
->dyn_relocs
= NULL
;
1267 /* Make sure undefined weak symbols are output as a dynamic
1269 else if (h
->dynindx
== -1
1270 && !h
->forced_local
)
1272 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1279 /* For the non-shared case, discard space for relocs against
1280 symbols which turn out to need copy relocs or are not
1286 || (htab
->elf
.dynamic_sections_created
1287 && (h
->root
.type
== bfd_link_hash_undefweak
1288 || h
->root
.type
== bfd_link_hash_undefined
))))
1290 /* Make sure this symbol is output as a dynamic symbol.
1291 Undefined weak syms won't yet be marked as dynamic. */
1292 if (h
->dynindx
== -1
1293 && !h
->forced_local
)
1295 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1299 /* If that succeeded, we know we'll be keeping all the
1301 if (h
->dynindx
!= -1)
1305 h
->dyn_relocs
= NULL
;
1310 /* Finally, allocate space. */
1311 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1313 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1314 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1320 /* Allocate space in .plt, .got and associated reloc sections for
1321 ifunc dynamic relocs. */
1324 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1327 struct bfd_link_info
*info
;
1329 if (h
->root
.type
== bfd_link_hash_indirect
)
1332 if (h
->root
.type
== bfd_link_hash_warning
)
1333 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1335 info
= (struct bfd_link_info
*) inf
;
1337 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1338 here if it is defined and referenced in a non-shared object. */
1339 if (h
->type
== STT_GNU_IFUNC
1341 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1350 /* Allocate space in .plt, .got and associated reloc sections for
1351 local ifunc dynamic relocs. */
1354 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1356 struct elf_link_hash_entry
*h
1357 = (struct elf_link_hash_entry
*) *slot
;
1359 if (h
->type
!= STT_GNU_IFUNC
1363 || h
->root
.type
!= bfd_link_hash_defined
)
1366 return allocate_ifunc_dynrelocs (h
, inf
);
1370 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1372 struct riscv_elf_link_hash_table
*htab
;
1377 htab
= riscv_elf_hash_table (info
);
1378 BFD_ASSERT (htab
!= NULL
);
1379 dynobj
= htab
->elf
.dynobj
;
1380 BFD_ASSERT (dynobj
!= NULL
);
1382 if (elf_hash_table (info
)->dynamic_sections_created
)
1384 /* Set the contents of the .interp section to the interpreter. */
1385 if (bfd_link_executable (info
) && !info
->nointerp
)
1387 s
= bfd_get_linker_section (dynobj
, ".interp");
1388 BFD_ASSERT (s
!= NULL
);
1389 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1390 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1394 /* Set up .got offsets for local syms, and space for local dynamic
1396 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1398 bfd_signed_vma
*local_got
;
1399 bfd_signed_vma
*end_local_got
;
1400 char *local_tls_type
;
1401 bfd_size_type locsymcount
;
1402 Elf_Internal_Shdr
*symtab_hdr
;
1405 if (! is_riscv_elf (ibfd
))
1408 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1410 struct elf_dyn_relocs
*p
;
1412 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1414 if (!bfd_is_abs_section (p
->sec
)
1415 && bfd_is_abs_section (p
->sec
->output_section
))
1417 /* Input section has been discarded, either because
1418 it is a copy of a linkonce section or due to
1419 linker script /DISCARD/, so we'll be discarding
1422 else if (p
->count
!= 0)
1424 srel
= elf_section_data (p
->sec
)->sreloc
;
1425 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1426 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1427 info
->flags
|= DF_TEXTREL
;
1432 local_got
= elf_local_got_refcounts (ibfd
);
1436 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1437 locsymcount
= symtab_hdr
->sh_info
;
1438 end_local_got
= local_got
+ locsymcount
;
1439 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1441 srel
= htab
->elf
.srelgot
;
1442 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1446 *local_got
= s
->size
;
1447 s
->size
+= RISCV_ELF_WORD_BYTES
;
1448 if (*local_tls_type
& GOT_TLS_GD
)
1449 s
->size
+= RISCV_ELF_WORD_BYTES
;
1450 if (bfd_link_pic (info
)
1451 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1452 srel
->size
+= sizeof (ElfNN_External_Rela
);
1455 *local_got
= (bfd_vma
) -1;
1459 /* Allocate .plt and .got entries and space dynamic relocs for
1461 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1463 /* Allocate .plt and .got entries and space dynamic relocs for
1464 global ifunc symbols. */
1465 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1467 /* Allocate .plt and .got entries and space dynamic relocs for
1468 local ifunc symbols. */
1469 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1471 /* Used to resolve the dynamic relocs overwite problems when
1472 generating static executable. */
1473 if (htab
->elf
.irelplt
)
1474 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1476 if (htab
->elf
.sgotplt
)
1478 struct elf_link_hash_entry
*got
;
1479 got
= elf_link_hash_lookup (elf_hash_table (info
),
1480 "_GLOBAL_OFFSET_TABLE_",
1481 false, false, false);
1483 /* Don't allocate .got.plt section if there are no GOT nor PLT
1484 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1486 || !got
->ref_regular_nonweak
)
1487 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1488 && (htab
->elf
.splt
== NULL
1489 || htab
->elf
.splt
->size
== 0)
1490 && (htab
->elf
.sgot
== NULL
1491 || (htab
->elf
.sgot
->size
1492 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1493 htab
->elf
.sgotplt
->size
= 0;
1496 /* The check_relocs and adjust_dynamic_symbol entry points have
1497 determined the sizes of the various dynamic sections. Allocate
1499 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1501 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1504 if (s
== htab
->elf
.splt
1505 || s
== htab
->elf
.sgot
1506 || s
== htab
->elf
.sgotplt
1507 || s
== htab
->elf
.iplt
1508 || s
== htab
->elf
.igotplt
1509 || s
== htab
->elf
.sdynbss
1510 || s
== htab
->elf
.sdynrelro
1511 || s
== htab
->sdyntdata
)
1513 /* Strip this section if we don't need it; see the
1516 else if (startswith (s
->name
, ".rela"))
1520 /* We use the reloc_count field as a counter if we need
1521 to copy relocs into the output file. */
1527 /* It's not one of our sections. */
1533 /* If we don't need this section, strip it from the
1534 output file. This is mostly to handle .rela.bss and
1535 .rela.plt. We must create both sections in
1536 create_dynamic_sections, because they must be created
1537 before the linker maps input sections to output
1538 sections. The linker does that before
1539 adjust_dynamic_symbol is called, and it is that
1540 function which decides whether anything needs to go
1541 into these sections. */
1542 s
->flags
|= SEC_EXCLUDE
;
1546 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1549 /* Allocate memory for the section contents. Zero the memory
1550 for the benefit of .rela.plt, which has 4 unused entries
1551 at the beginning, and we don't want garbage. */
1552 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1553 if (s
->contents
== NULL
)
1557 return _bfd_elf_add_dynamic_tags (output_bfd
, info
, true);
1561 #define DTP_OFFSET 0x800
1563 /* Return the relocation value for a TLS dtp-relative reloc. */
1566 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1568 /* If tls_sec is NULL, we should have signalled an error already. */
1569 if (elf_hash_table (info
)->tls_sec
== NULL
)
1571 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1574 /* Return the relocation value for a static TLS tp-relative relocation. */
1577 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1579 /* If tls_sec is NULL, we should have signalled an error already. */
1580 if (elf_hash_table (info
)->tls_sec
== NULL
)
1582 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1585 /* Return the global pointer's value, or 0 if it is not in use. */
1588 riscv_global_pointer_value (struct bfd_link_info
*info
)
1590 struct bfd_link_hash_entry
*h
;
1592 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1593 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1596 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1599 /* Emplace a static relocation. */
1601 static bfd_reloc_status_type
1602 perform_relocation (const reloc_howto_type
*howto
,
1603 const Elf_Internal_Rela
*rel
,
1605 asection
*input_section
,
1609 if (howto
->pc_relative
)
1610 value
-= sec_addr (input_section
) + rel
->r_offset
;
1611 value
+= rel
->r_addend
;
1613 switch (ELFNN_R_TYPE (rel
->r_info
))
1616 case R_RISCV_TPREL_HI20
:
1617 case R_RISCV_PCREL_HI20
:
1618 case R_RISCV_GOT_HI20
:
1619 case R_RISCV_TLS_GOT_HI20
:
1620 case R_RISCV_TLS_GD_HI20
:
1621 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1622 return bfd_reloc_overflow
;
1623 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1626 case R_RISCV_LO12_I
:
1627 case R_RISCV_GPREL_I
:
1628 case R_RISCV_TPREL_LO12_I
:
1629 case R_RISCV_TPREL_I
:
1630 case R_RISCV_PCREL_LO12_I
:
1631 value
= ENCODE_ITYPE_IMM (value
);
1634 case R_RISCV_LO12_S
:
1635 case R_RISCV_GPREL_S
:
1636 case R_RISCV_TPREL_LO12_S
:
1637 case R_RISCV_TPREL_S
:
1638 case R_RISCV_PCREL_LO12_S
:
1639 value
= ENCODE_STYPE_IMM (value
);
1643 case R_RISCV_CALL_PLT
:
1644 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1645 return bfd_reloc_overflow
;
1646 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1647 | (ENCODE_ITYPE_IMM (value
) << 32);
1651 if (!VALID_JTYPE_IMM (value
))
1652 return bfd_reloc_overflow
;
1653 value
= ENCODE_JTYPE_IMM (value
);
1656 case R_RISCV_BRANCH
:
1657 if (!VALID_BTYPE_IMM (value
))
1658 return bfd_reloc_overflow
;
1659 value
= ENCODE_BTYPE_IMM (value
);
1662 case R_RISCV_RVC_BRANCH
:
1663 if (!VALID_CBTYPE_IMM (value
))
1664 return bfd_reloc_overflow
;
1665 value
= ENCODE_CBTYPE_IMM (value
);
1668 case R_RISCV_RVC_JUMP
:
1669 if (!VALID_CJTYPE_IMM (value
))
1670 return bfd_reloc_overflow
;
1671 value
= ENCODE_CJTYPE_IMM (value
);
1674 case R_RISCV_RVC_LUI
:
1675 if (RISCV_CONST_HIGH_PART (value
) == 0)
1677 /* Linker relaxation can convert an address equal to or greater than
1678 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1679 valid immediate. We can fix this by converting it to a C.LI. */
1680 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1681 contents
+ rel
->r_offset
);
1682 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1683 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1684 value
= ENCODE_CITYPE_IMM (0);
1686 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1687 return bfd_reloc_overflow
;
1689 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1707 case R_RISCV_32_PCREL
:
1708 case R_RISCV_TLS_DTPREL32
:
1709 case R_RISCV_TLS_DTPREL64
:
1712 case R_RISCV_DELETE
:
1713 return bfd_reloc_ok
;
1716 return bfd_reloc_notsupported
;
1720 if (riscv_is_insn_reloc (howto
))
1721 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1723 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1724 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1725 if (riscv_is_insn_reloc (howto
))
1726 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1728 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1730 return bfd_reloc_ok
;
1733 /* Remember all PC-relative high-part relocs we've encountered to help us
1734 later resolve the corresponding low-part relocs. */
1740 } riscv_pcrel_hi_reloc
;
1742 typedef struct riscv_pcrel_lo_reloc
1744 asection
*input_section
;
1745 struct bfd_link_info
*info
;
1746 reloc_howto_type
*howto
;
1747 const Elf_Internal_Rela
*reloc
;
1751 struct riscv_pcrel_lo_reloc
*next
;
1752 } riscv_pcrel_lo_reloc
;
1757 riscv_pcrel_lo_reloc
*lo_relocs
;
1758 } riscv_pcrel_relocs
;
1761 riscv_pcrel_reloc_hash (const void *entry
)
1763 const riscv_pcrel_hi_reloc
*e
= entry
;
1764 return (hashval_t
)(e
->address
>> 2);
1768 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1770 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1771 return e1
->address
== e2
->address
;
1775 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1777 p
->lo_relocs
= NULL
;
1778 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1779 riscv_pcrel_reloc_eq
, free
);
1780 return p
->hi_relocs
!= NULL
;
1784 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1786 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1790 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1795 htab_delete (p
->hi_relocs
);
1799 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1800 struct bfd_link_info
*info
,
1804 const reloc_howto_type
*howto
,
1805 bfd
*input_bfd ATTRIBUTE_UNUSED
)
1807 /* We may need to reference low addreses in PC-relative modes even when the
1808 PC is far away from these addresses. For example, undefweak references
1809 need to produce the address 0 when linked. As 0 is far from the arbitrary
1810 addresses that we can link PC-relative programs at, the linker can't
1811 actually relocate references to those symbols. In order to allow these
1812 programs to work we simply convert the PC-relative auipc sequences to
1813 0-relative lui sequences. */
1814 if (bfd_link_pic (info
))
1817 /* If it's possible to reference the symbol using auipc we do so, as that's
1818 more in the spirit of the PC-relative relocations we're processing. */
1819 bfd_vma offset
= addr
- pc
;
1820 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1823 /* If it's impossible to reference this with a LUI-based offset then don't
1824 bother to convert it at all so users still see the PC-relative relocation
1825 in the truncation message. */
1826 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1829 rel
->r_info
= ELFNN_R_INFO (addr
, R_RISCV_HI20
);
1831 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1832 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1833 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1838 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1839 bfd_vma value
, bool absolute
)
1841 bfd_vma offset
= absolute
? value
: value
- addr
;
1842 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1843 riscv_pcrel_hi_reloc
**slot
=
1844 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1846 BFD_ASSERT (*slot
== NULL
);
1847 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1855 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1856 asection
*input_section
,
1857 struct bfd_link_info
*info
,
1858 reloc_howto_type
*howto
,
1859 const Elf_Internal_Rela
*reloc
,
1864 riscv_pcrel_lo_reloc
*entry
;
1865 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1868 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1869 name
, contents
, p
->lo_relocs
};
1870 p
->lo_relocs
= entry
;
1875 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1877 riscv_pcrel_lo_reloc
*r
;
1879 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1881 bfd
*input_bfd
= r
->input_section
->owner
;
1883 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1884 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1886 /* Check for overflow into bit 11 when adding reloc addend. */
1887 || (!(entry
->value
& 0x800)
1888 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1890 char *string
= (entry
== NULL
1891 ? "%pcrel_lo missing matching %pcrel_hi"
1892 : "%pcrel_lo overflow with an addend");
1893 (*r
->info
->callbacks
->reloc_dangerous
)
1894 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1898 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1899 input_bfd
, r
->contents
);
1905 /* Relocate a RISC-V ELF section.
1907 The RELOCATE_SECTION function is called by the new ELF backend linker
1908 to handle the relocations for a section.
1910 The relocs are always passed as Rela structures.
1912 This function is responsible for adjusting the section contents as
1913 necessary, and (if generating a relocatable output file) adjusting
1914 the reloc addend as necessary.
1916 This function does not have to worry about setting the reloc
1917 address or the reloc symbol index.
1919 LOCAL_SYMS is a pointer to the swapped in local symbols.
1921 LOCAL_SECTIONS is an array giving the section in the input file
1922 corresponding to the st_shndx field of each local symbol.
1924 The global hash table entry for the global symbols can be found
1925 via elf_sym_hashes (input_bfd).
1927 When generating relocatable output, this function must handle
1928 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1929 going to be the section symbol corresponding to the output
1930 section, which means that the addend must be adjusted
1934 riscv_elf_relocate_section (bfd
*output_bfd
,
1935 struct bfd_link_info
*info
,
1937 asection
*input_section
,
1939 Elf_Internal_Rela
*relocs
,
1940 Elf_Internal_Sym
*local_syms
,
1941 asection
**local_sections
)
1943 Elf_Internal_Rela
*rel
;
1944 Elf_Internal_Rela
*relend
;
1945 riscv_pcrel_relocs pcrel_relocs
;
1947 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1948 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1949 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1950 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1953 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1956 relend
= relocs
+ input_section
->reloc_count
;
1957 for (rel
= relocs
; rel
< relend
; rel
++)
1959 unsigned long r_symndx
;
1960 struct elf_link_hash_entry
*h
;
1961 Elf_Internal_Sym
*sym
;
1964 bfd_reloc_status_type r
= bfd_reloc_ok
;
1965 const char *name
= NULL
;
1966 bfd_vma off
, ie_off
;
1967 bool unresolved_reloc
, is_ie
= false;
1968 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1969 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1970 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1971 const char *msg
= NULL
;
1972 char *msg_buf
= NULL
;
1973 bool resolved_to_zero
;
1976 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1979 /* This is a final link. */
1980 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1984 unresolved_reloc
= false;
1985 if (r_symndx
< symtab_hdr
->sh_info
)
1987 sym
= local_syms
+ r_symndx
;
1988 sec
= local_sections
[r_symndx
];
1989 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1991 /* Relocate against local STT_GNU_IFUNC symbol. */
1992 if (!bfd_link_relocatable (info
)
1993 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
1995 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, false);
1999 /* Set STT_GNU_IFUNC symbol value. */
2000 h
->root
.u
.def
.value
= sym
->st_value
;
2001 h
->root
.u
.def
.section
= sec
;
2006 bool warned
, ignored
;
2008 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2009 r_symndx
, symtab_hdr
, sym_hashes
,
2011 unresolved_reloc
, warned
, ignored
);
2014 /* To avoid generating warning messages about truncated
2015 relocations, set the relocation's address to be the same as
2016 the start of this section. */
2017 if (input_section
->output_section
!= NULL
)
2018 relocation
= input_section
->output_section
->vma
;
2024 if (sec
!= NULL
&& discarded_section (sec
))
2025 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2026 rel
, 1, relend
, howto
, 0, contents
);
2028 if (bfd_link_relocatable (info
))
2031 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2032 it here if it is defined in a non-shared object. */
2034 && h
->type
== STT_GNU_IFUNC
2037 asection
*plt
, *base_got
;
2039 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2041 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2042 STT_GNU_IFUNC symbol as STT_FUNC. */
2043 if (elf_section_type (input_section
) == SHT_NOTE
)
2046 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2047 sections because such sections are not SEC_ALLOC and
2048 thus ld.so will not process them. */
2049 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2054 else if (h
->plt
.offset
== (bfd_vma
) -1
2055 /* The following relocation may not need the .plt entries
2056 when all references to a STT_GNU_IFUNC symbols are done
2057 via GOT or static function pointers. */
2058 && r_type
!= R_RISCV_32
2059 && r_type
!= R_RISCV_64
2060 && r_type
!= R_RISCV_HI20
2061 && r_type
!= R_RISCV_GOT_HI20
2062 && r_type
!= R_RISCV_LO12_I
2063 && r_type
!= R_RISCV_LO12_S
)
2064 goto bad_ifunc_reloc
;
2066 /* STT_GNU_IFUNC symbol must go through PLT. */
2067 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2068 relocation
= plt
->output_section
->vma
2069 + plt
->output_offset
2076 if (rel
->r_addend
!= 0)
2078 if (h
->root
.root
.string
)
2079 name
= h
->root
.root
.string
;
2081 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2084 /* xgettext:c-format */
2085 (_("%pB: relocation %s against STT_GNU_IFUNC "
2086 "symbol `%s' has non-zero addend: %" PRId64
),
2087 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2088 bfd_set_error (bfd_error_bad_value
);
2092 /* Generate dynamic relocation only when there is a non-GOT
2093 reference in a shared object or there is no PLT. */
2094 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2095 || h
->plt
.offset
== (bfd_vma
) -1)
2097 Elf_Internal_Rela outrel
;
2100 /* Need a dynamic relocation to get the real function
2102 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2106 if (outrel
.r_offset
== (bfd_vma
) -1
2107 || outrel
.r_offset
== (bfd_vma
) -2)
2110 outrel
.r_offset
+= input_section
->output_section
->vma
2111 + input_section
->output_offset
;
2113 if (h
->dynindx
== -1
2115 || bfd_link_executable (info
))
2117 info
->callbacks
->minfo
2118 (_("Local IFUNC function `%s' in %pB\n"),
2119 h
->root
.root
.string
,
2120 h
->root
.u
.def
.section
->owner
);
2122 /* This symbol is resolved locally. */
2123 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2124 outrel
.r_addend
= h
->root
.u
.def
.value
2125 + h
->root
.u
.def
.section
->output_section
->vma
2126 + h
->root
.u
.def
.section
->output_offset
;
2130 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2131 outrel
.r_addend
= 0;
2134 /* Dynamic relocations are stored in
2135 1. .rela.ifunc section in PIC object.
2136 2. .rela.got section in dynamic executable.
2137 3. .rela.iplt section in static executable. */
2138 if (bfd_link_pic (info
))
2139 sreloc
= htab
->elf
.irelifunc
;
2140 else if (htab
->elf
.splt
!= NULL
)
2141 sreloc
= htab
->elf
.srelgot
;
2143 sreloc
= htab
->elf
.irelplt
;
2145 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2147 /* If this reloc is against an external symbol, we
2148 do not want to fiddle with the addend. Otherwise,
2149 we need to include the symbol value so that it
2150 becomes an addend for the dynamic reloc. For an
2151 internal symbol, we have updated addend. */
2156 case R_RISCV_GOT_HI20
:
2157 base_got
= htab
->elf
.sgot
;
2158 off
= h
->got
.offset
;
2160 if (base_got
== NULL
)
2163 if (off
== (bfd_vma
) -1)
2167 /* We can't use h->got.offset here to save state, or
2168 even just remember the offset, as finish_dynamic_symbol
2169 would use that as offset into .got. */
2171 if (htab
->elf
.splt
!= NULL
)
2173 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2175 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2176 base_got
= htab
->elf
.sgotplt
;
2180 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2181 off
= plt_idx
* GOT_ENTRY_SIZE
;
2182 base_got
= htab
->elf
.igotplt
;
2185 if (h
->dynindx
== -1
2189 /* This references the local definition. We must
2190 initialize this entry in the global offset table.
2191 Since the offset must always be a multiple of 8,
2192 we use the least significant bit to record
2193 whether we have initialized it already.
2195 When doing a dynamic link, we create a .rela.got
2196 relocation entry to initialize the value. This
2197 is done in the finish_dynamic_symbol routine. */
2202 bfd_put_NN (output_bfd
, relocation
,
2203 base_got
->contents
+ off
);
2204 /* Note that this is harmless for the case,
2205 as -1 | 1 still is -1. */
2211 relocation
= base_got
->output_section
->vma
2212 + base_got
->output_offset
+ off
;
2214 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2215 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2217 r
= bfd_reloc_notsupported
;
2218 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2220 r
= bfd_reloc_overflow
;
2224 case R_RISCV_CALL_PLT
:
2226 case R_RISCV_LO12_I
:
2227 case R_RISCV_LO12_S
:
2230 case R_RISCV_PCREL_HI20
:
2231 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2232 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2234 r
= bfd_reloc_notsupported
;
2235 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2237 r
= bfd_reloc_overflow
;
2242 if (h
->root
.root
.string
)
2243 name
= h
->root
.root
.string
;
2245 /* The entry of local ifunc is fake in global hash table,
2246 we should find the name by the original local symbol. */
2247 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2250 /* xgettext:c-format */
2251 (_("%pB: relocation %s against STT_GNU_IFUNC "
2252 "symbol `%s' isn't supported"), input_bfd
,
2254 bfd_set_error (bfd_error_bad_value
);
2261 name
= h
->root
.root
.string
;
2264 name
= (bfd_elf_string_from_elf_section
2265 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2266 if (name
== NULL
|| *name
== '\0')
2267 name
= bfd_section_name (sec
);
2270 resolved_to_zero
= (h
!= NULL
2271 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2277 case R_RISCV_TPREL_ADD
:
2279 case R_RISCV_JUMP_SLOT
:
2280 case R_RISCV_RELATIVE
:
2281 /* These require nothing of us at all. */
2285 case R_RISCV_BRANCH
:
2286 case R_RISCV_RVC_BRANCH
:
2287 case R_RISCV_RVC_LUI
:
2288 case R_RISCV_LO12_I
:
2289 case R_RISCV_LO12_S
:
2294 case R_RISCV_32_PCREL
:
2295 case R_RISCV_DELETE
:
2296 /* These require no special handling beyond perform_relocation. */
2299 case R_RISCV_GOT_HI20
:
2304 off
= h
->got
.offset
;
2305 BFD_ASSERT (off
!= (bfd_vma
) -1);
2306 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2307 pic
= bfd_link_pic (info
);
2309 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2310 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
2312 /* This is actually a static link, or it is a
2313 -Bsymbolic link and the symbol is defined
2314 locally, or the symbol was forced to be local
2315 because of a version file. We must initialize
2316 this entry in the global offset table. Since the
2317 offset must always be a multiple of the word size,
2318 we use the least significant bit to record whether
2319 we have initialized it already.
2321 When doing a dynamic link, we create a .rela.got
2322 relocation entry to initialize the value. This
2323 is done in the finish_dynamic_symbol routine. */
2328 bfd_put_NN (output_bfd
, relocation
,
2329 htab
->elf
.sgot
->contents
+ off
);
2334 unresolved_reloc
= false;
2338 BFD_ASSERT (local_got_offsets
!= NULL
2339 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2341 off
= local_got_offsets
[r_symndx
];
2343 /* The offset must always be a multiple of the word size.
2344 So, we can use the least significant bit to record
2345 whether we have already processed this entry. */
2350 if (bfd_link_pic (info
))
2353 Elf_Internal_Rela outrel
;
2355 /* We need to generate a R_RISCV_RELATIVE reloc
2356 for the dynamic linker. */
2357 s
= htab
->elf
.srelgot
;
2358 BFD_ASSERT (s
!= NULL
);
2360 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2362 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2363 outrel
.r_addend
= relocation
;
2365 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2368 bfd_put_NN (output_bfd
, relocation
,
2369 htab
->elf
.sgot
->contents
+ off
);
2370 local_got_offsets
[r_symndx
] |= 1;
2373 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2374 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2381 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2382 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2384 r
= bfd_reloc_notsupported
;
2385 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2386 relocation
, absolute
))
2387 r
= bfd_reloc_overflow
;
2395 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2396 contents
+ rel
->r_offset
);
2397 relocation
= old_value
+ relocation
;
2407 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2408 contents
+ rel
->r_offset
);
2409 relocation
= old_value
- relocation
;
2414 case R_RISCV_CALL_PLT
:
2415 /* Handle a call to an undefined weak function. This won't be
2416 relaxed, so we have to handle it here. */
2417 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2418 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2420 /* We can use x0 as the base register. */
2421 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2422 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2423 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2424 /* Set the relocation value so that we get 0 after the pc
2425 relative adjustment. */
2426 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2431 case R_RISCV_RVC_JUMP
:
2432 /* This line has to match the check in _bfd_riscv_relax_section. */
2433 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
2435 /* Refer to the PLT entry. */
2436 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2437 unresolved_reloc
= false;
2441 case R_RISCV_TPREL_HI20
:
2442 relocation
= tpoff (info
, relocation
);
2445 case R_RISCV_TPREL_LO12_I
:
2446 case R_RISCV_TPREL_LO12_S
:
2447 relocation
= tpoff (info
, relocation
);
2450 case R_RISCV_TPREL_I
:
2451 case R_RISCV_TPREL_S
:
2452 relocation
= tpoff (info
, relocation
);
2453 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2455 /* We can use tp as the base register. */
2456 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2457 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2458 insn
|= X_TP
<< OP_SH_RS1
;
2459 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2462 r
= bfd_reloc_overflow
;
2465 case R_RISCV_GPREL_I
:
2466 case R_RISCV_GPREL_S
:
2468 bfd_vma gp
= riscv_global_pointer_value (info
);
2469 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2470 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2472 /* We can use x0 or gp as the base register. */
2473 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2474 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2477 rel
->r_addend
-= gp
;
2478 insn
|= X_GP
<< OP_SH_RS1
;
2480 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2483 r
= bfd_reloc_overflow
;
2487 case R_RISCV_PCREL_HI20
:
2488 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2495 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2496 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2498 r
= bfd_reloc_notsupported
;
2499 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2500 relocation
+ rel
->r_addend
,
2502 r
= bfd_reloc_overflow
;
2505 case R_RISCV_PCREL_LO12_I
:
2506 case R_RISCV_PCREL_LO12_S
:
2507 /* We don't allow section symbols plus addends as the auipc address,
2508 because then riscv_relax_delete_bytes would have to search through
2509 all relocs to update these addends. This is also ambiguous, as
2510 we do allow offsets to be added to the target address, which are
2511 not to be used to find the auipc address. */
2512 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2513 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2516 msg
= _("%pcrel_lo section symbol with an addend");
2517 r
= bfd_reloc_dangerous
;
2521 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
2522 howto
, rel
, relocation
, name
,
2525 r
= bfd_reloc_overflow
;
2528 case R_RISCV_TLS_DTPREL32
:
2529 case R_RISCV_TLS_DTPREL64
:
2530 relocation
= dtpoff (info
, relocation
);
2535 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2538 if ((bfd_link_pic (info
)
2540 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2541 && !resolved_to_zero
)
2542 || h
->root
.type
!= bfd_link_hash_undefweak
)
2543 && (!howto
->pc_relative
2544 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2545 || (!bfd_link_pic (info
)
2551 || h
->root
.type
== bfd_link_hash_undefweak
2552 || h
->root
.type
== bfd_link_hash_undefined
)))
2554 Elf_Internal_Rela outrel
;
2556 bool skip_static_relocation
, skip_dynamic_relocation
;
2558 /* When generating a shared object, these relocations
2559 are copied into the output file to be resolved at run
2563 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2565 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2566 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2567 outrel
.r_offset
+= sec_addr (input_section
);
2569 if (skip_dynamic_relocation
)
2570 memset (&outrel
, 0, sizeof outrel
);
2571 else if (h
!= NULL
&& h
->dynindx
!= -1
2572 && !(bfd_link_pic (info
)
2573 && SYMBOLIC_BIND (info
, h
)
2576 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2577 outrel
.r_addend
= rel
->r_addend
;
2581 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2582 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2585 sreloc
= elf_section_data (input_section
)->sreloc
;
2586 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2587 if (skip_static_relocation
)
2592 case R_RISCV_TLS_GOT_HI20
:
2596 case R_RISCV_TLS_GD_HI20
:
2599 off
= h
->got
.offset
;
2604 off
= local_got_offsets
[r_symndx
];
2605 local_got_offsets
[r_symndx
] |= 1;
2608 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2609 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2610 /* If this symbol is referenced by both GD and IE TLS, the IE
2611 reference's GOT slot follows the GD reference's slots. */
2613 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2614 ie_off
= 2 * GOT_ENTRY_SIZE
;
2620 Elf_Internal_Rela outrel
;
2622 bool need_relocs
= false;
2624 if (htab
->elf
.srelgot
== NULL
)
2630 dyn
= htab
->elf
.dynamic_sections_created
;
2631 pic
= bfd_link_pic (info
);
2633 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2634 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2638 /* The GOT entries have not been initialized yet. Do it
2639 now, and emit any relocations. */
2640 if ((bfd_link_pic (info
) || indx
!= 0)
2642 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2643 || h
->root
.type
!= bfd_link_hash_undefweak
))
2646 if (tls_type
& GOT_TLS_GD
)
2650 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2651 outrel
.r_addend
= 0;
2652 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2653 bfd_put_NN (output_bfd
, 0,
2654 htab
->elf
.sgot
->contents
+ off
);
2655 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2658 BFD_ASSERT (! unresolved_reloc
);
2659 bfd_put_NN (output_bfd
,
2660 dtpoff (info
, relocation
),
2661 (htab
->elf
.sgot
->contents
2662 + off
+ RISCV_ELF_WORD_BYTES
));
2666 bfd_put_NN (output_bfd
, 0,
2667 (htab
->elf
.sgot
->contents
2668 + off
+ RISCV_ELF_WORD_BYTES
));
2669 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2670 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2671 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2676 /* If we are not emitting relocations for a
2677 general dynamic reference, then we must be in a
2678 static link or an executable link with the
2679 symbol binding locally. Mark it as belonging
2680 to module 1, the executable. */
2681 bfd_put_NN (output_bfd
, 1,
2682 htab
->elf
.sgot
->contents
+ off
);
2683 bfd_put_NN (output_bfd
,
2684 dtpoff (info
, relocation
),
2685 (htab
->elf
.sgot
->contents
2686 + off
+ RISCV_ELF_WORD_BYTES
));
2690 if (tls_type
& GOT_TLS_IE
)
2694 bfd_put_NN (output_bfd
, 0,
2695 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2696 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2698 outrel
.r_addend
= 0;
2700 outrel
.r_addend
= tpoff (info
, relocation
);
2701 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2702 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2706 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2707 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2712 BFD_ASSERT (off
< (bfd_vma
) -2);
2713 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2714 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2716 r
= bfd_reloc_overflow
;
2717 unresolved_reloc
= false;
2721 r
= bfd_reloc_notsupported
;
2724 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2725 because such sections are not SEC_ALLOC and thus ld.so will
2726 not process them. */
2727 if (unresolved_reloc
2728 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2730 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2731 rel
->r_offset
) != (bfd_vma
) -1)
2736 case R_RISCV_RVC_JUMP
:
2737 if (asprintf (&msg_buf
,
2738 _("%%X%%P: relocation %s against `%s' can "
2739 "not be used when making a shared object; "
2740 "recompile with -fPIC\n"),
2742 h
->root
.root
.string
) == -1)
2747 if (asprintf (&msg_buf
,
2748 _("%%X%%P: unresolvable %s relocation against "
2751 h
->root
.root
.string
) == -1)
2757 r
= bfd_reloc_notsupported
;
2761 if (r
== bfd_reloc_ok
)
2762 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2763 input_bfd
, contents
);
2765 /* We should have already detected the error and set message before.
2766 If the error message isn't set since the linker runs out of memory
2767 or we don't set it before, then we should set the default message
2768 with the "internal error" string here. */
2774 case bfd_reloc_overflow
:
2775 info
->callbacks
->reloc_overflow
2776 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2777 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2780 case bfd_reloc_undefined
:
2781 info
->callbacks
->undefined_symbol
2782 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2786 case bfd_reloc_outofrange
:
2788 msg
= _("%X%P: internal error: out of range error\n");
2791 case bfd_reloc_notsupported
:
2793 msg
= _("%X%P: internal error: unsupported relocation error\n");
2796 case bfd_reloc_dangerous
:
2797 /* The error message should already be set. */
2799 msg
= _("dangerous relocation error");
2800 info
->callbacks
->reloc_dangerous
2801 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2805 msg
= _("%X%P: internal error: unknown error\n");
2809 /* Do not report error message for the dangerous relocation again. */
2810 if (msg
&& r
!= bfd_reloc_dangerous
)
2811 info
->callbacks
->einfo (msg
);
2813 /* Free the unused `msg_buf`. */
2816 /* We already reported the error via a callback, so don't try to report
2817 it again by returning false. That leads to spurious errors. */
2822 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2824 riscv_free_pcrel_relocs (&pcrel_relocs
);
2828 /* Finish up dynamic symbol handling. We set the contents of various
2829 dynamic sections here. */
2832 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2833 struct bfd_link_info
*info
,
2834 struct elf_link_hash_entry
*h
,
2835 Elf_Internal_Sym
*sym
)
2837 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2838 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2840 if (h
->plt
.offset
!= (bfd_vma
) -1)
2842 /* We've decided to create a PLT entry for this symbol. */
2844 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
2845 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2846 Elf_Internal_Rela rela
;
2847 asection
*plt
, *gotplt
, *relplt
;
2849 /* When building a static executable, use .iplt, .igot.plt and
2850 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2851 if (htab
->elf
.splt
!= NULL
)
2853 plt
= htab
->elf
.splt
;
2854 gotplt
= htab
->elf
.sgotplt
;
2855 relplt
= htab
->elf
.srelplt
;
2859 plt
= htab
->elf
.iplt
;
2860 gotplt
= htab
->elf
.igotplt
;
2861 relplt
= htab
->elf
.irelplt
;
2864 /* This symbol has an entry in the procedure linkage table. Set
2866 if ((h
->dynindx
== -1
2867 && !((h
->forced_local
|| bfd_link_executable (info
))
2869 && h
->type
== STT_GNU_IFUNC
))
2875 /* Calculate the address of the PLT header. */
2876 header_address
= sec_addr (plt
);
2878 /* Calculate the index of the entry and the offset of .got.plt entry.
2879 For static executables, we don't reserve anything. */
2880 if (plt
== htab
->elf
.splt
)
2882 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2883 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2887 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2888 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
2891 /* Calculate the address of the .got.plt entry. */
2892 got_address
= sec_addr (gotplt
) + got_offset
;
2894 /* Find out where the .plt entry should go. */
2895 loc
= plt
->contents
+ h
->plt
.offset
;
2897 /* Fill in the PLT entry itself. */
2898 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2899 header_address
+ h
->plt
.offset
,
2903 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2904 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
2906 /* Fill in the initial value of the .got.plt entry. */
2907 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
2908 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
2910 rela
.r_offset
= got_address
;
2912 if (h
->dynindx
== -1
2913 || ((bfd_link_executable (info
)
2914 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2916 && h
->type
== STT_GNU_IFUNC
))
2918 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2919 h
->root
.root
.string
,
2920 h
->root
.u
.def
.section
->owner
);
2922 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2923 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2924 asection
*sec
= h
->root
.u
.def
.section
;
2925 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2926 rela
.r_addend
= h
->root
.u
.def
.value
2927 + sec
->output_section
->vma
2928 + sec
->output_offset
;
2932 /* Fill in the entry in the .rela.plt section. */
2933 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2937 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2938 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2940 if (!h
->def_regular
)
2942 /* Mark the symbol as undefined, rather than as defined in
2943 the .plt section. Leave the value alone. */
2944 sym
->st_shndx
= SHN_UNDEF
;
2945 /* If the symbol is weak, we do need to clear the value.
2946 Otherwise, the PLT entry would provide a definition for
2947 the symbol even if the symbol wasn't defined anywhere,
2948 and so the symbol would never be NULL. */
2949 if (!h
->ref_regular_nonweak
)
2954 if (h
->got
.offset
!= (bfd_vma
) -1
2955 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2956 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2960 Elf_Internal_Rela rela
;
2961 bool use_elf_append_rela
= true;
2963 /* This symbol has an entry in the GOT. Set it up. */
2965 sgot
= htab
->elf
.sgot
;
2966 srela
= htab
->elf
.srelgot
;
2967 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2969 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2971 /* Handle the ifunc symbol in GOT entry. */
2973 && h
->type
== STT_GNU_IFUNC
)
2975 if (h
->plt
.offset
== (bfd_vma
) -1)
2977 /* STT_GNU_IFUNC is referenced without PLT. */
2979 if (htab
->elf
.splt
== NULL
)
2981 /* Use .rela.iplt section to store .got relocations
2982 in static executable. */
2983 srela
= htab
->elf
.irelplt
;
2985 /* Do not use riscv_elf_append_rela to add dynamic
2987 use_elf_append_rela
= false;
2990 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
2992 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2993 h
->root
.root
.string
,
2994 h
->root
.u
.def
.section
->owner
);
2996 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2997 rela
.r_addend
= (h
->root
.u
.def
.value
2998 + h
->root
.u
.def
.section
->output_section
->vma
2999 + h
->root
.u
.def
.section
->output_offset
);
3003 /* Generate R_RISCV_NN. */
3004 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3005 BFD_ASSERT (h
->dynindx
!= -1);
3006 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3010 else if (bfd_link_pic (info
))
3012 /* Generate R_RISCV_NN. */
3013 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3014 BFD_ASSERT (h
->dynindx
!= -1);
3015 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3022 if (!h
->pointer_equality_needed
)
3025 /* For non-shared object, we can't use .got.plt, which
3026 contains the real function address if we need pointer
3027 equality. We load the GOT entry with the PLT entry. */
3028 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3029 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3030 + plt
->output_offset
3032 htab
->elf
.sgot
->contents
3033 + (h
->got
.offset
& ~(bfd_vma
) 1));
3037 else if (bfd_link_pic (info
)
3038 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3040 /* If this is a local symbol reference, we just want to emit
3041 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3042 or a pie link, or the symbol was forced to be local because
3043 of a version file. The entry in the global offset table will
3044 already have been initialized in the relocate_section function. */
3045 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3046 asection
*sec
= h
->root
.u
.def
.section
;
3047 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3048 rela
.r_addend
= (h
->root
.u
.def
.value
3049 + sec
->output_section
->vma
3050 + sec
->output_offset
);
3054 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3055 BFD_ASSERT (h
->dynindx
!= -1);
3056 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3060 bfd_put_NN (output_bfd
, 0,
3061 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3063 if (use_elf_append_rela
)
3064 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3067 /* Use riscv_elf_append_rela to add the dynamic relocs into
3068 .rela.iplt may cause the overwrite problems. Since we insert
3069 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3070 but the riscv_elf_append_rela adds the relocs to the place
3071 that are calculated from the reloc_index (in seqential).
3073 One solution is that add these dynamic relocs (GOT IFUNC)
3074 from the last of .rela.iplt section. */
3075 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3076 bfd_byte
*loc
= srela
->contents
3077 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3078 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3084 Elf_Internal_Rela rela
;
3087 /* This symbols needs a copy reloc. Set it up. */
3088 BFD_ASSERT (h
->dynindx
!= -1);
3090 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3091 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3093 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3094 s
= htab
->elf
.sreldynrelro
;
3096 s
= htab
->elf
.srelbss
;
3097 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3100 /* Mark some specially defined symbols as absolute. */
3101 if (h
== htab
->elf
.hdynamic
3102 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3103 sym
->st_shndx
= SHN_ABS
;
3108 /* Finish up local dynamic symbol handling. We set the contents of
3109 various dynamic sections here. */
3112 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3114 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3115 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3117 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3120 /* Finish up the dynamic sections. */
3123 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3124 bfd
*dynobj
, asection
*sdyn
)
3126 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3127 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3128 size_t dynsize
= bed
->s
->sizeof_dyn
;
3129 bfd_byte
*dyncon
, *dynconend
;
3131 dynconend
= sdyn
->contents
+ sdyn
->size
;
3132 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3134 Elf_Internal_Dyn dyn
;
3137 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3142 s
= htab
->elf
.sgotplt
;
3143 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3146 s
= htab
->elf
.srelplt
;
3147 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3150 s
= htab
->elf
.srelplt
;
3151 dyn
.d_un
.d_val
= s
->size
;
3157 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3163 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3164 struct bfd_link_info
*info
)
3168 struct riscv_elf_link_hash_table
*htab
;
3170 htab
= riscv_elf_hash_table (info
);
3171 BFD_ASSERT (htab
!= NULL
);
3172 dynobj
= htab
->elf
.dynobj
;
3174 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3176 if (elf_hash_table (info
)->dynamic_sections_created
)
3181 splt
= htab
->elf
.splt
;
3182 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3184 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3189 /* Fill in the head and tail entries in the procedure linkage table. */
3193 uint32_t plt_header
[PLT_HEADER_INSNS
];
3194 ret
= riscv_make_plt_header (output_bfd
,
3195 sec_addr (htab
->elf
.sgotplt
),
3196 sec_addr (splt
), plt_header
);
3200 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3201 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3203 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3208 if (htab
->elf
.sgotplt
)
3210 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3212 if (bfd_is_abs_section (output_section
))
3214 (*_bfd_error_handler
)
3215 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3219 if (htab
->elf
.sgotplt
->size
> 0)
3221 /* Write the first two entries in .got.plt, needed for the dynamic
3223 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3224 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3225 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3228 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3233 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3235 if (htab
->elf
.sgot
->size
> 0)
3237 /* Set the first entry in the global offset table to the address of
3238 the dynamic section. */
3239 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3240 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3243 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3246 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3247 htab_traverse (htab
->loc_hash_table
,
3248 riscv_elf_finish_local_dynamic_symbol
,
3254 /* Return address for Ith PLT stub in section PLT, for relocation REL
3255 or (bfd_vma) -1 if it should not be included. */
3258 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3259 const arelent
*rel ATTRIBUTE_UNUSED
)
3261 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3264 static enum elf_reloc_type_class
3265 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3266 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3267 const Elf_Internal_Rela
*rela
)
3269 switch (ELFNN_R_TYPE (rela
->r_info
))
3271 case R_RISCV_RELATIVE
:
3272 return reloc_class_relative
;
3273 case R_RISCV_JUMP_SLOT
:
3274 return reloc_class_plt
;
3276 return reloc_class_copy
;
3278 return reloc_class_normal
;
3282 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3286 riscv_float_abi_string (flagword flags
)
3288 switch (flags
& EF_RISCV_FLOAT_ABI
)
3290 case EF_RISCV_FLOAT_ABI_SOFT
:
3291 return "soft-float";
3293 case EF_RISCV_FLOAT_ABI_SINGLE
:
3294 return "single-float";
3296 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3297 return "double-float";
3299 case EF_RISCV_FLOAT_ABI_QUAD
:
3300 return "quad-float";
3307 /* The information of architecture elf attributes. */
3308 static riscv_subset_list_t in_subsets
;
3309 static riscv_subset_list_t out_subsets
;
3310 static riscv_subset_list_t merged_subsets
;
3312 /* Predicator for standard extension. */
3315 riscv_std_ext_p (const char *name
)
3317 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3320 /* Check if the versions are compatible. */
3323 riscv_version_mismatch (bfd
*ibfd
,
3324 struct riscv_subset_t
*in
,
3325 struct riscv_subset_t
*out
)
3327 if (in
== NULL
|| out
== NULL
)
3330 /* Since there are no version conflicts for now, we just report
3331 warning when the versions are mis-matched. */
3332 if (in
->major_version
!= out
->major_version
3333 || in
->minor_version
!= out
->minor_version
)
3335 if ((in
->major_version
== RISCV_UNKNOWN_VERSION
3336 && in
->minor_version
== RISCV_UNKNOWN_VERSION
)
3337 || (out
->major_version
== RISCV_UNKNOWN_VERSION
3338 && out
->minor_version
== RISCV_UNKNOWN_VERSION
))
3340 /* Do not report the warning when the version of input
3341 or output is RISCV_UNKNOWN_VERSION, since the extension
3342 is added implicitly. */
3346 (_("warning: %pB: mis-matched ISA version %d.%d for '%s' "
3347 "extension, the output version is %d.%d"),
3353 out
->minor_version
);
3355 /* Update the output ISA versions to the newest ones. */
3356 if ((in
->major_version
> out
->major_version
)
3357 || (in
->major_version
== out
->major_version
3358 && in
->minor_version
> out
->minor_version
))
3360 out
->major_version
= in
->major_version
;
3361 out
->minor_version
= in
->minor_version
;
3368 /* Return true if subset is 'i' or 'e'. */
3371 riscv_i_or_e_p (bfd
*ibfd
,
3373 struct riscv_subset_t
*subset
)
3375 if ((strcasecmp (subset
->name
, "e") != 0)
3376 && (strcasecmp (subset
->name
, "i") != 0))
3379 (_("error: %pB: corrupted ISA string '%s'. "
3380 "First letter should be 'i' or 'e' but got '%s'"),
3381 ibfd
, arch
, subset
->name
);
3387 /* Merge standard extensions.
3390 Return FALSE if failed to merge.
3394 `in_arch`: Raw ISA string for input object.
3395 `out_arch`: Raw ISA string for output object.
3396 `pin`: Subset list for input object.
3397 `pout`: Subset list for output object. */
3400 riscv_merge_std_ext (bfd
*ibfd
,
3401 const char *in_arch
,
3402 const char *out_arch
,
3403 struct riscv_subset_t
**pin
,
3404 struct riscv_subset_t
**pout
)
3406 const char *standard_exts
= riscv_supported_std_ext ();
3408 struct riscv_subset_t
*in
= *pin
;
3409 struct riscv_subset_t
*out
= *pout
;
3411 /* First letter should be 'i' or 'e'. */
3412 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3415 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3418 if (strcasecmp (in
->name
, out
->name
) != 0)
3420 /* TODO: We might allow merge 'i' with 'e'. */
3422 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3423 ibfd
, in
->name
, out
->name
);
3426 else if (!riscv_version_mismatch (ibfd
, in
, out
))
3429 riscv_add_subset (&merged_subsets
,
3430 out
->name
, out
->major_version
, out
->minor_version
);
3435 /* Handle standard extension first. */
3436 for (p
= standard_exts
; *p
; ++p
)
3438 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3439 char find_ext
[2] = {*p
, '\0'};
3440 bool find_in
, find_out
;
3442 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3443 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3445 if (!find_in
&& !find_out
)
3450 && !riscv_version_mismatch (ibfd
, ext_in
, ext_out
))
3453 ext_merged
= find_out
? ext_out
: ext_in
;
3454 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3455 ext_merged
->major_version
, ext_merged
->minor_version
);
3458 /* Skip all standard extensions. */
3459 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3460 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3468 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3469 object subset list. Likewise for POUT and the output object. Return TRUE
3470 on success and FALSE when a conflict is found. */
3473 riscv_merge_multi_letter_ext (bfd
*ibfd
,
3474 riscv_subset_t
**pin
,
3475 riscv_subset_t
**pout
)
3477 riscv_subset_t
*in
= *pin
;
3478 riscv_subset_t
*out
= *pout
;
3479 riscv_subset_t
*tail
;
3485 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3489 /* `in' comes before `out', append `in' and increment. */
3490 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3496 /* `out' comes before `in', append `out' and increment. */
3497 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3498 out
->minor_version
);
3503 /* Both present, check version and increment both. */
3504 if (!riscv_version_mismatch (ibfd
, in
, out
))
3507 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3508 out
->minor_version
);
3516 /* If we're here, either `in' or `out' is running longer than
3517 the other. So, we need to append the corresponding tail. */
3518 tail
= in
? in
: out
;
3521 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3522 tail
->minor_version
);
3530 /* Merge Tag_RISCV_arch attribute. */
3533 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3535 riscv_subset_t
*in
, *out
;
3536 char *merged_arch_str
;
3538 unsigned xlen_in
, xlen_out
;
3539 merged_subsets
.head
= NULL
;
3540 merged_subsets
.tail
= NULL
;
3542 riscv_parse_subset_t rpe_in
;
3543 riscv_parse_subset_t rpe_out
;
3545 /* Only assembler needs to check the default version of ISA, so just set
3546 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
3547 rpe_in
.subset_list
= &in_subsets
;
3548 rpe_in
.error_handler
= _bfd_error_handler
;
3549 rpe_in
.xlen
= &xlen_in
;
3550 rpe_in
.get_default_version
= NULL
;
3552 rpe_out
.subset_list
= &out_subsets
;
3553 rpe_out
.error_handler
= _bfd_error_handler
;
3554 rpe_out
.xlen
= &xlen_out
;
3555 rpe_out
.get_default_version
= NULL
;
3557 if (in_arch
== NULL
&& out_arch
== NULL
)
3560 if (in_arch
== NULL
&& out_arch
!= NULL
)
3563 if (in_arch
!= NULL
&& out_arch
== NULL
)
3566 /* Parse subset from ISA string. */
3567 if (!riscv_parse_subset (&rpe_in
, in_arch
))
3570 if (!riscv_parse_subset (&rpe_out
, out_arch
))
3573 /* Checking XLEN. */
3574 if (xlen_out
!= xlen_in
)
3577 (_("error: %pB: ISA string of input (%s) doesn't match "
3578 "output (%s)"), ibfd
, in_arch
, out_arch
);
3582 /* Merge subset list. */
3583 in
= in_subsets
.head
;
3584 out
= out_subsets
.head
;
3586 /* Merge standard extension. */
3587 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3590 /* Merge all non-single letter extensions with single call. */
3591 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
3594 if (xlen_in
!= xlen_out
)
3597 (_("error: %pB: XLEN of input (%u) doesn't match "
3598 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3602 if (xlen_in
!= ARCH_SIZE
)
3605 (_("error: %pB: unsupported XLEN (%u), you might be "
3606 "using wrong emulation"), ibfd
, xlen_in
);
3610 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3612 /* Release the subset lists. */
3613 riscv_release_subset_list (&in_subsets
);
3614 riscv_release_subset_list (&out_subsets
);
3615 riscv_release_subset_list (&merged_subsets
);
3617 return merged_arch_str
;
3620 /* Merge object attributes from IBFD into output_bfd of INFO.
3621 Raise an error if there are conflicting attributes. */
3624 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3626 bfd
*obfd
= info
->output_bfd
;
3627 obj_attribute
*in_attr
;
3628 obj_attribute
*out_attr
;
3630 bool priv_attrs_merged
= false;
3631 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3634 /* Skip linker created files. */
3635 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3638 /* Skip any input that doesn't have an attribute section.
3639 This enables to link object files without attribute section with
3641 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3644 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3646 /* This is the first object. Copy the attributes. */
3647 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3649 out_attr
= elf_known_obj_attributes_proc (obfd
);
3651 /* Use the Tag_null value to indicate the attributes have been
3658 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3659 out_attr
= elf_known_obj_attributes_proc (obfd
);
3661 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3665 case Tag_RISCV_arch
:
3666 if (!out_attr
[Tag_RISCV_arch
].s
)
3667 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3668 else if (in_attr
[Tag_RISCV_arch
].s
3669 && out_attr
[Tag_RISCV_arch
].s
)
3671 /* Check compatible. */
3673 riscv_merge_arch_attr_info (ibfd
,
3674 in_attr
[Tag_RISCV_arch
].s
,
3675 out_attr
[Tag_RISCV_arch
].s
);
3676 if (merged_arch
== NULL
)
3679 out_attr
[Tag_RISCV_arch
].s
= "";
3682 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3686 case Tag_RISCV_priv_spec
:
3687 case Tag_RISCV_priv_spec_minor
:
3688 case Tag_RISCV_priv_spec_revision
:
3689 /* If we have handled the privileged elf attributes, then skip it. */
3690 if (!priv_attrs_merged
)
3692 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3693 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3694 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3695 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3696 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3698 /* Get the privileged spec class from elf attributes. */
3699 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3703 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3708 /* Allow to link the object without the privileged specs. */
3709 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3711 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3712 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3713 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3715 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3716 && in_priv_spec
!= out_priv_spec
)
3719 (_("warning: %pB use privileged spec version %u.%u.%u but "
3720 "the output use version %u.%u.%u"),
3729 /* The privileged spec v1.9.1 can not be linked with others
3730 since the conflicts, so we plan to drop it in a year or
3732 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3733 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3736 (_("warning: privileged spec version 1.9.1 can not be "
3737 "linked with other spec versions"));
3740 /* Update the output privileged spec to the newest one. */
3741 if (in_priv_spec
> out_priv_spec
)
3743 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3744 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3745 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3748 priv_attrs_merged
= true;
3752 case Tag_RISCV_unaligned_access
:
3753 out_attr
[i
].i
|= in_attr
[i
].i
;
3756 case Tag_RISCV_stack_align
:
3757 if (out_attr
[i
].i
== 0)
3758 out_attr
[i
].i
= in_attr
[i
].i
;
3759 else if (in_attr
[i
].i
!= 0
3760 && out_attr
[i
].i
!= 0
3761 && out_attr
[i
].i
!= in_attr
[i
].i
)
3764 (_("error: %pB use %u-byte stack aligned but the output "
3765 "use %u-byte stack aligned"),
3766 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3772 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3775 /* If out_attr was copied from in_attr then it won't have a type yet. */
3776 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3777 out_attr
[i
].type
= in_attr
[i
].type
;
3780 /* Merge Tag_compatibility attributes and any common GNU ones. */
3781 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3784 /* Check for any attributes not known on RISC-V. */
3785 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3790 /* Merge backend specific data from an object file to the output
3791 object file when linking. */
3794 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3796 bfd
*obfd
= info
->output_bfd
;
3797 flagword new_flags
, old_flags
;
3799 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3802 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3804 (*_bfd_error_handler
)
3805 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3806 " target emulation `%s' does not match `%s'"),
3807 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3811 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3814 if (!riscv_merge_attributes (ibfd
, info
))
3817 /* Check to see if the input BFD actually contains any sections. If not,
3818 its flags may not have been initialized either, but it cannot actually
3819 cause any incompatibility. Do not short-circuit dynamic objects; their
3820 section list may be emptied by elf_link_add_object_symbols.
3822 Also check to see if there are no code sections in the input. In this
3823 case, there is no need to check for code specific flags. */
3824 if (!(ibfd
->flags
& DYNAMIC
))
3826 bool null_input_bfd
= true;
3827 bool only_data_sections
= true;
3830 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3832 null_input_bfd
= false;
3834 if ((bfd_section_flags (sec
)
3835 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3836 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3838 only_data_sections
= false;
3843 if (null_input_bfd
|| only_data_sections
)
3847 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3848 old_flags
= elf_elfheader (obfd
)->e_flags
;
3850 if (!elf_flags_init (obfd
))
3852 elf_flags_init (obfd
) = true;
3853 elf_elfheader (obfd
)->e_flags
= new_flags
;
3857 /* Disallow linking different float ABIs. */
3858 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3860 (*_bfd_error_handler
)
3861 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3862 riscv_float_abi_string (new_flags
),
3863 riscv_float_abi_string (old_flags
));
3867 /* Disallow linking RVE and non-RVE. */
3868 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3870 (*_bfd_error_handler
)
3871 (_("%pB: can't link RVE with other target"), ibfd
);
3875 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3876 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3881 bfd_set_error (bfd_error_bad_value
);
3885 /* Delete some bytes from a section while relaxing. */
3888 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3889 struct bfd_link_info
*link_info
)
3891 unsigned int i
, symcount
;
3892 bfd_vma toaddr
= sec
->size
;
3893 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3894 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3895 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3896 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3897 bfd_byte
*contents
= data
->this_hdr
.contents
;
3899 /* Actually delete the bytes. */
3901 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3903 /* Adjust the location of all of the relocs. Note that we need not
3904 adjust the addends, since all PC-relative references must be against
3905 symbols, which we will adjust below. */
3906 for (i
= 0; i
< sec
->reloc_count
; i
++)
3907 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3908 data
->relocs
[i
].r_offset
-= count
;
3910 /* Adjust the local symbols defined in this section. */
3911 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3913 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3914 if (sym
->st_shndx
== sec_shndx
)
3916 /* If the symbol is in the range of memory we just moved, we
3917 have to adjust its value. */
3918 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3919 sym
->st_value
-= count
;
3921 /* If the symbol *spans* the bytes we just deleted (i.e. its
3922 *end* is in the moved bytes but its *start* isn't), then we
3923 must adjust its size.
3925 This test needs to use the original value of st_value, otherwise
3926 we might accidentally decrease size when deleting bytes right
3927 before the symbol. But since deleted relocs can't span across
3928 symbols, we can't have both a st_value and a st_size decrease,
3929 so it is simpler to just use an else. */
3930 else if (sym
->st_value
<= addr
3931 && sym
->st_value
+ sym
->st_size
> addr
3932 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3933 sym
->st_size
-= count
;
3937 /* Now adjust the global symbols defined in this section. */
3938 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3939 - symtab_hdr
->sh_info
);
3941 for (i
= 0; i
< symcount
; i
++)
3943 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3945 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3946 containing the definition of __wrap_SYMBOL, includes a direct
3947 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3948 the same symbol (which is __wrap_SYMBOL), but still exist as two
3949 different symbols in 'sym_hashes', we don't want to adjust
3950 the global symbol __wrap_SYMBOL twice.
3952 The same problem occurs with symbols that are versioned_hidden, as
3953 foo becomes an alias for foo@BAR, and hence they need the same
3955 if (link_info
->wrap_hash
!= NULL
3956 || sym_hash
->versioned
== versioned_hidden
)
3958 struct elf_link_hash_entry
**cur_sym_hashes
;
3960 /* Loop only over the symbols which have already been checked. */
3961 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3964 /* If the current symbol is identical to 'sym_hash', that means
3965 the symbol was already adjusted (or at least checked). */
3966 if (*cur_sym_hashes
== sym_hash
)
3969 /* Don't adjust the symbol again. */
3970 if (cur_sym_hashes
< &sym_hashes
[i
])
3974 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3975 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3976 && sym_hash
->root
.u
.def
.section
== sec
)
3978 /* As above, adjust the value if needed. */
3979 if (sym_hash
->root
.u
.def
.value
> addr
3980 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3981 sym_hash
->root
.u
.def
.value
-= count
;
3983 /* As above, adjust the size if needed. */
3984 else if (sym_hash
->root
.u
.def
.value
<= addr
3985 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3986 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3987 sym_hash
->size
-= count
;
3994 /* A second format for recording PC-relative hi relocations. This stores the
3995 information required to relax them to GP-relative addresses. */
3997 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3998 struct riscv_pcgp_hi_reloc
4005 bool undefined_weak
;
4006 riscv_pcgp_hi_reloc
*next
;
4009 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
4010 struct riscv_pcgp_lo_reloc
4013 riscv_pcgp_lo_reloc
*next
;
4018 riscv_pcgp_hi_reloc
*hi
;
4019 riscv_pcgp_lo_reloc
*lo
;
4020 } riscv_pcgp_relocs
;
4022 /* Initialize the pcgp reloc info in P. */
4025 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
4032 /* Free the pcgp reloc info in P. */
4035 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
4036 bfd
*abfd ATTRIBUTE_UNUSED
,
4037 asection
*sec ATTRIBUTE_UNUSED
)
4039 riscv_pcgp_hi_reloc
*c
;
4040 riscv_pcgp_lo_reloc
*l
;
4042 for (c
= p
->hi
; c
!= NULL
; )
4044 riscv_pcgp_hi_reloc
*next
= c
->next
;
4049 for (l
= p
->lo
; l
!= NULL
; )
4051 riscv_pcgp_lo_reloc
*next
= l
->next
;
4057 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
4058 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4059 relax the corresponding lo part reloc. */
4062 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
4063 bfd_vma hi_addend
, bfd_vma hi_addr
,
4064 unsigned hi_sym
, asection
*sym_sec
,
4065 bool undefined_weak
)
4067 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
4070 new->hi_sec_off
= hi_sec_off
;
4071 new->hi_addend
= hi_addend
;
4072 new->hi_addr
= hi_addr
;
4073 new->hi_sym
= hi_sym
;
4074 new->sym_sec
= sym_sec
;
4075 new->undefined_weak
= undefined_weak
;
4081 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4082 This is used by a lo part reloc to find the corresponding hi part reloc. */
4084 static riscv_pcgp_hi_reloc
*
4085 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4087 riscv_pcgp_hi_reloc
*c
;
4089 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4090 if (c
->hi_sec_off
== hi_sec_off
)
4095 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4096 This is used to record relocs that can't be relaxed. */
4099 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4101 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
4104 new->hi_sec_off
= hi_sec_off
;
4110 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4111 This is used by a hi part reloc to find the corresponding lo part reloc. */
4114 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4116 riscv_pcgp_lo_reloc
*c
;
4118 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4119 if (c
->hi_sec_off
== hi_sec_off
)
4124 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4125 struct bfd_link_info
*,
4126 Elf_Internal_Rela
*,
4127 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4128 riscv_pcgp_relocs
*,
4129 bool undefined_weak
);
4131 /* Relax AUIPC + JALR into JAL. */
4134 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4135 struct bfd_link_info
*link_info
,
4136 Elf_Internal_Rela
*rel
,
4138 bfd_vma max_alignment
,
4139 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4141 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4142 bool undefined_weak ATTRIBUTE_UNUSED
)
4144 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4145 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4146 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4147 bfd_vma auipc
, jalr
;
4148 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4150 /* If the call crosses section boundaries, an alignment directive could
4151 cause the PC-relative offset to later increase, so we need to add in the
4152 max alignment of any section inclusive from the call to the target.
4153 Otherwise, we only need to use the alignment of the current section. */
4154 if (VALID_JTYPE_IMM (foff
))
4156 if (sym_sec
->output_section
== sec
->output_section
4157 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4158 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4159 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4162 /* See if this function call can be shortened. */
4163 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4166 /* Shorten the function call. */
4167 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4169 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4170 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4171 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4172 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4174 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4175 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4179 /* Relax to C.J[AL] rd, addr. */
4180 r_type
= R_RISCV_RVC_JUMP
;
4181 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4184 else if (VALID_JTYPE_IMM (foff
))
4186 /* Relax to JAL rd, addr. */
4187 r_type
= R_RISCV_JAL
;
4188 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4192 /* Near zero, relax to JALR rd, x0, addr. */
4193 r_type
= R_RISCV_LO12_I
;
4194 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4197 /* Replace the R_RISCV_CALL reloc. */
4198 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4199 /* Replace the AUIPC. */
4200 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4202 /* Delete unnecessary JALR. */
4204 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4208 /* Traverse all output sections and return the max alignment. */
4211 _bfd_riscv_get_max_alignment (asection
*sec
)
4213 unsigned int max_alignment_power
= 0;
4216 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4218 if (o
->alignment_power
> max_alignment_power
)
4219 max_alignment_power
= o
->alignment_power
;
4222 return (bfd_vma
) 1 << max_alignment_power
;
4225 /* Relax non-PIC global variable references to GP-relative references. */
4228 _bfd_riscv_relax_lui (bfd
*abfd
,
4231 struct bfd_link_info
*link_info
,
4232 Elf_Internal_Rela
*rel
,
4234 bfd_vma max_alignment
,
4235 bfd_vma reserve_size
,
4237 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4238 bool undefined_weak
)
4240 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4241 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4242 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4244 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4248 /* If gp and the symbol are in the same output section, which is not the
4249 abs section, then consider only that output section's alignment. */
4250 struct bfd_link_hash_entry
*h
=
4251 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4253 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4254 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4255 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4258 /* Is the reference in range of x0 or gp?
4259 Valid gp range conservatively because of alignment issue. */
4261 || (VALID_ITYPE_IMM (symval
)
4263 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4265 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4267 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4268 switch (ELFNN_R_TYPE (rel
->r_info
))
4270 case R_RISCV_LO12_I
:
4273 /* Change the RS1 to zero. */
4274 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4275 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4276 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4279 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4282 case R_RISCV_LO12_S
:
4285 /* Change the RS1 to zero. */
4286 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4287 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4288 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4291 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4295 /* We can delete the unnecessary LUI and reloc. */
4296 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4298 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4306 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4307 account for this assuming page alignment at worst. In the presence of
4308 RELRO segment the linker aligns it by one page size, therefore sections
4309 after the segment can be moved more than one page. */
4312 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4313 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4314 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4315 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4316 : ELF_MAXPAGESIZE
)))
4318 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4319 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4320 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4321 if (rd
== 0 || rd
== X_SP
)
4324 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4325 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4327 /* Replace the R_RISCV_HI20 reloc. */
4328 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4331 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4338 /* Relax non-PIC TLS references to TP-relative references. */
4341 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4343 asection
*sym_sec ATTRIBUTE_UNUSED
,
4344 struct bfd_link_info
*link_info
,
4345 Elf_Internal_Rela
*rel
,
4347 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4348 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4350 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
4351 bool undefined_weak ATTRIBUTE_UNUSED
)
4353 /* See if this symbol is in range of tp. */
4354 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4357 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4358 switch (ELFNN_R_TYPE (rel
->r_info
))
4360 case R_RISCV_TPREL_LO12_I
:
4361 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4364 case R_RISCV_TPREL_LO12_S
:
4365 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4368 case R_RISCV_TPREL_HI20
:
4369 case R_RISCV_TPREL_ADD
:
4370 /* We can delete the unnecessary instruction and reloc. */
4371 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4373 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
4380 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4381 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4384 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4386 struct bfd_link_info
*link_info
,
4387 Elf_Internal_Rela
*rel
,
4389 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4390 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4391 bool *again ATTRIBUTE_UNUSED
,
4392 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
4393 bool undefined_weak ATTRIBUTE_UNUSED
)
4395 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4396 bfd_vma alignment
= 1, pos
;
4397 while (alignment
<= rel
->r_addend
)
4400 symval
-= rel
->r_addend
;
4401 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4402 bfd_vma nop_bytes
= aligned_addr
- symval
;
4404 /* Make sure there are enough NOPs to actually achieve the alignment. */
4405 if (rel
->r_addend
< nop_bytes
)
4408 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4409 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4410 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4411 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4412 bfd_set_error (bfd_error_bad_value
);
4416 /* Delete the reloc. */
4417 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4419 /* If the number of NOPs is already correct, there's nothing to do. */
4420 if (nop_bytes
== rel
->r_addend
)
4423 /* Write as many RISC-V NOPs as we need. */
4424 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4425 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4427 /* Write a final RVC NOP if need be. */
4428 if (nop_bytes
% 4 != 0)
4429 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4431 /* Delete the excess bytes. */
4432 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4433 rel
->r_addend
- nop_bytes
, link_info
);
4436 /* Relax PC-relative references to GP-relative references. */
4439 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4442 struct bfd_link_info
*link_info
,
4443 Elf_Internal_Rela
*rel
,
4445 bfd_vma max_alignment
,
4446 bfd_vma reserve_size
,
4447 bool *again ATTRIBUTE_UNUSED
,
4448 riscv_pcgp_relocs
*pcgp_relocs
,
4449 bool undefined_weak
)
4451 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4452 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4454 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4456 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4457 actual target address. */
4458 riscv_pcgp_hi_reloc hi_reloc
;
4459 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4460 switch (ELFNN_R_TYPE (rel
->r_info
))
4462 case R_RISCV_PCREL_LO12_I
:
4463 case R_RISCV_PCREL_LO12_S
:
4465 /* If the %lo has an addend, it isn't for the label pointing at the
4466 hi part instruction, but rather for the symbol pointed at by the
4467 hi part instruction. So we must subtract it here for the lookup.
4468 It is still used below in the final symbol address. */
4469 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4470 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4474 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4479 symval
= hi_reloc
.hi_addr
;
4480 sym_sec
= hi_reloc
.sym_sec
;
4482 /* We can not know whether the undefined weak symbol is referenced
4483 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4484 we have to record the 'undefined_weak' flag when handling the
4485 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4486 undefined_weak
= hi_reloc
.undefined_weak
;
4490 case R_RISCV_PCREL_HI20
:
4491 /* Mergeable symbols and code might later move out of range. */
4492 if (! undefined_weak
4493 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4496 /* If the cooresponding lo relocation has already been seen then it's not
4497 safe to relax this relocation. */
4498 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
4509 /* If gp and the symbol are in the same output section, which is not the
4510 abs section, then consider only that output section's alignment. */
4511 struct bfd_link_hash_entry
*h
=
4512 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4514 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4515 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4516 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4519 /* Is the reference in range of x0 or gp?
4520 Valid gp range conservatively because of alignment issue. */
4522 || (VALID_ITYPE_IMM (symval
)
4524 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4526 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4528 unsigned sym
= hi_reloc
.hi_sym
;
4529 switch (ELFNN_R_TYPE (rel
->r_info
))
4531 case R_RISCV_PCREL_LO12_I
:
4534 /* Change the RS1 to zero, and then modify the relocation
4535 type to R_RISCV_LO12_I. */
4536 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4537 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4538 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4539 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
4540 rel
->r_addend
= hi_reloc
.hi_addend
;
4544 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4545 rel
->r_addend
+= hi_reloc
.hi_addend
;
4549 case R_RISCV_PCREL_LO12_S
:
4552 /* Change the RS1 to zero, and then modify the relocation
4553 type to R_RISCV_LO12_S. */
4554 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4555 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4556 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4557 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
4558 rel
->r_addend
= hi_reloc
.hi_addend
;
4562 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4563 rel
->r_addend
+= hi_reloc
.hi_addend
;
4567 case R_RISCV_PCREL_HI20
:
4568 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
4572 ELFNN_R_SYM(rel
->r_info
),
4575 /* We can delete the unnecessary AUIPC and reloc. */
4576 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4588 /* Delete the bytes for R_RISCV_DELETE. */
4591 _bfd_riscv_relax_delete (bfd
*abfd
,
4593 asection
*sym_sec ATTRIBUTE_UNUSED
,
4594 struct bfd_link_info
*link_info
,
4595 Elf_Internal_Rela
*rel
,
4596 bfd_vma symval ATTRIBUTE_UNUSED
,
4597 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4598 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4600 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4601 bool undefined_weak ATTRIBUTE_UNUSED
)
4603 if (!riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4606 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4611 /* Called by after_allocation to check if we need to run the whole
4612 relaxations again. */
4615 bfd_elfNN_riscv_restart_relax_sections (struct bfd_link_info
*info
)
4617 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4618 bool restart
= htab
->restart_relax
;
4619 /* Reset the flag. */
4620 htab
->restart_relax
= false;
4626 Pass 0: Shortens code sequences for LUI/CALL/TPREL relocs.
4627 Pass 1: Shortens code sequences for PCREL relocs.
4628 Pass 2: Deletes the bytes that pass 1 made obsolete.
4629 Pass 3: Which cannot be disabled, handles code alignment directives.
4631 The `again` is used to determine whether the relax pass itself needs to
4632 run again. And the `restart_relax` is used to determine if we need to
4633 run the whole relax passes again from 0 to 2. Once we have deleted the
4634 code between relax pass 0 to 2, the restart_relax will be set to TRUE,
4635 and we should run the whole relaxations again to give them more chances
4636 to shorten the code.
4638 Since we can't relax anything else once we start to handle the alignments,
4639 we will only enter into the relax pass 3 when the restart_relax is FALSE. */
4642 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
4643 struct bfd_link_info
*info
,
4646 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
4647 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4648 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4649 Elf_Internal_Rela
*relocs
;
4652 bfd_vma max_alignment
, reserve_size
= 0;
4653 riscv_pcgp_relocs pcgp_relocs
;
4657 if (bfd_link_relocatable (info
)
4658 || (sec
->flags
& SEC_RELOC
) == 0
4659 || sec
->reloc_count
== 0
4660 || (info
->disable_target_specific_optimizations
4661 && info
->relax_pass
< 2)
4662 || (htab
->restart_relax
4663 && info
->relax_pass
== 3))
4666 riscv_init_pcgp_relocs (&pcgp_relocs
);
4668 /* Read this BFD's relocs if we haven't done so already. */
4670 relocs
= data
->relocs
;
4671 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4672 info
->keep_memory
)))
4677 max_alignment
= htab
->max_alignment
;
4678 if (max_alignment
== (bfd_vma
) -1)
4680 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4681 htab
->max_alignment
= max_alignment
;
4685 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4687 /* Examine and consider relaxing each reloc. */
4688 for (i
= 0; i
< sec
->reloc_count
; i
++)
4691 Elf_Internal_Rela
*rel
= relocs
+ i
;
4692 relax_func_t relax_func
;
4693 int type
= ELFNN_R_TYPE (rel
->r_info
);
4696 bool undefined_weak
= false;
4699 if (info
->relax_pass
== 0)
4701 if (type
== R_RISCV_CALL
4702 || type
== R_RISCV_CALL_PLT
)
4703 relax_func
= _bfd_riscv_relax_call
;
4704 else if (type
== R_RISCV_HI20
4705 || type
== R_RISCV_LO12_I
4706 || type
== R_RISCV_LO12_S
)
4707 relax_func
= _bfd_riscv_relax_lui
;
4708 else if (type
== R_RISCV_TPREL_HI20
4709 || type
== R_RISCV_TPREL_ADD
4710 || type
== R_RISCV_TPREL_LO12_I
4711 || type
== R_RISCV_TPREL_LO12_S
)
4712 relax_func
= _bfd_riscv_relax_tls_le
;
4716 else if (info
->relax_pass
== 1
4717 && !bfd_link_pic (info
)
4718 && (type
== R_RISCV_PCREL_HI20
4719 || type
== R_RISCV_PCREL_LO12_I
4720 || type
== R_RISCV_PCREL_LO12_S
))
4721 relax_func
= _bfd_riscv_relax_pc
;
4722 else if (info
->relax_pass
== 2 && type
== R_RISCV_DELETE
)
4723 relax_func
= _bfd_riscv_relax_delete
;
4724 else if (info
->relax_pass
== 3 && type
== R_RISCV_ALIGN
)
4725 relax_func
= _bfd_riscv_relax_align
;
4729 if (info
->relax_pass
< 2)
4731 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4732 if (i
== sec
->reloc_count
- 1
4733 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4734 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4737 /* Skip over the R_RISCV_RELAX. */
4741 data
->relocs
= relocs
;
4743 /* Read this BFD's contents if we haven't done so already. */
4744 if (!data
->this_hdr
.contents
4745 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4748 /* Read this BFD's symbols if we haven't done so already. */
4749 if (symtab_hdr
->sh_info
!= 0
4750 && !symtab_hdr
->contents
4751 && !(symtab_hdr
->contents
=
4752 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4753 symtab_hdr
->sh_info
,
4754 0, NULL
, NULL
, NULL
)))
4757 /* Get the value of the symbol referred to by the reloc. */
4758 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4760 /* A local symbol. */
4761 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4762 + ELFNN_R_SYM (rel
->r_info
));
4763 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4764 ? 0 : isym
->st_size
- rel
->r_addend
;
4766 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4767 a fake global symbol entry for this, so deal with the local ifunc
4769 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
4772 if (isym
->st_shndx
== SHN_UNDEF
)
4773 sym_sec
= sec
, symval
= rel
->r_offset
;
4776 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4777 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4779 /* The purpose of this code is unknown. It breaks linker scripts
4780 for embedded development that place sections at address zero.
4781 This code is believed to be unnecessary. Disabling it but not
4782 yet removing it, in case something breaks. */
4783 if (sec_addr (sym_sec
) == 0)
4786 symval
= isym
->st_value
;
4788 symtype
= ELF_ST_TYPE (isym
->st_info
);
4793 struct elf_link_hash_entry
*h
;
4795 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4796 h
= elf_sym_hashes (abfd
)[indx
];
4798 while (h
->root
.type
== bfd_link_hash_indirect
4799 || h
->root
.type
== bfd_link_hash_warning
)
4800 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4802 /* Disable the relaxation for ifunc. */
4803 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
4806 if (h
->root
.type
== bfd_link_hash_undefweak
4807 && (relax_func
== _bfd_riscv_relax_lui
4808 || relax_func
== _bfd_riscv_relax_pc
))
4810 /* For the lui and auipc relaxations, since the symbol
4811 value of an undefined weak symbol is always be zero,
4812 we can optimize the patterns into a single LI/MV/ADDI
4815 Note that, creating shared libraries and pie output may
4816 break the rule above. Fortunately, since we do not relax
4817 pc relocs when creating shared libraries and pie output,
4818 and the absolute address access for R_RISCV_HI20 isn't
4819 allowed when "-fPIC" is set, the problem of creating shared
4820 libraries can not happen currently. Once we support the
4821 auipc relaxations when creating shared libraries, then we will
4822 need the more rigorous checking for this optimization. */
4823 undefined_weak
= true;
4826 /* This line has to match the check in riscv_elf_relocate_section
4827 in the R_RISCV_CALL[_PLT] case. */
4828 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4830 sym_sec
= htab
->elf
.splt
;
4831 symval
= h
->plt
.offset
;
4833 else if (undefined_weak
)
4836 sym_sec
= bfd_und_section_ptr
;
4838 else if ((h
->root
.type
== bfd_link_hash_defined
4839 || h
->root
.type
== bfd_link_hash_defweak
)
4840 && h
->root
.u
.def
.section
!= NULL
4841 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4843 symval
= h
->root
.u
.def
.value
;
4844 sym_sec
= h
->root
.u
.def
.section
;
4849 if (h
->type
!= STT_FUNC
)
4851 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4855 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4856 && (sym_sec
->flags
& SEC_MERGE
))
4858 /* At this stage in linking, no SEC_MERGE symbol has been
4859 adjusted, so all references to such symbols need to be
4860 passed through _bfd_merged_section_offset. (Later, in
4861 relocate_section, all SEC_MERGE symbols *except* for
4862 section symbols have been adjusted.)
4864 gas may reduce relocations against symbols in SEC_MERGE
4865 sections to a relocation against the section symbol when
4866 the original addend was zero. When the reloc is against
4867 a section symbol we should include the addend in the
4868 offset passed to _bfd_merged_section_offset, since the
4869 location of interest is the original symbol. On the
4870 other hand, an access to "sym+addend" where "sym" is not
4871 a section symbol should not include the addend; Such an
4872 access is presumed to be an offset from "sym"; The
4873 location of interest is just "sym". */
4874 if (symtype
== STT_SECTION
)
4875 symval
+= rel
->r_addend
;
4877 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4878 elf_section_data (sym_sec
)->sec_info
,
4881 if (symtype
!= STT_SECTION
)
4882 symval
+= rel
->r_addend
;
4885 symval
+= rel
->r_addend
;
4887 symval
+= sec_addr (sym_sec
);
4889 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4890 max_alignment
, reserve_size
, again
,
4891 &pcgp_relocs
, undefined_weak
))
4898 if (relocs
!= data
->relocs
)
4900 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
4903 htab
->restart_relax
= true;
4909 # define PRSTATUS_SIZE 204
4910 # define PRSTATUS_OFFSET_PR_CURSIG 12
4911 # define PRSTATUS_OFFSET_PR_PID 24
4912 # define PRSTATUS_OFFSET_PR_REG 72
4913 # define ELF_GREGSET_T_SIZE 128
4914 # define PRPSINFO_SIZE 128
4915 # define PRPSINFO_OFFSET_PR_PID 16
4916 # define PRPSINFO_OFFSET_PR_FNAME 32
4917 # define PRPSINFO_OFFSET_PR_PSARGS 48
4918 # define PRPSINFO_PR_FNAME_LENGTH 16
4919 # define PRPSINFO_PR_PSARGS_LENGTH 80
4921 # define PRSTATUS_SIZE 376
4922 # define PRSTATUS_OFFSET_PR_CURSIG 12
4923 # define PRSTATUS_OFFSET_PR_PID 32
4924 # define PRSTATUS_OFFSET_PR_REG 112
4925 # define ELF_GREGSET_T_SIZE 256
4926 # define PRPSINFO_SIZE 136
4927 # define PRPSINFO_OFFSET_PR_PID 24
4928 # define PRPSINFO_OFFSET_PR_FNAME 40
4929 # define PRPSINFO_OFFSET_PR_PSARGS 56
4930 # define PRPSINFO_PR_FNAME_LENGTH 16
4931 # define PRPSINFO_PR_PSARGS_LENGTH 80
4934 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
4935 before the generic code in elf.c. By checking the compiler defines we
4936 only perform any action here if the generic code would otherwise not be
4937 able to help us. The intention is that bare metal core dumps (where the
4938 prstatus_t and/or prpsinfo_t might not be available) will use this code,
4939 while non bare metal tools will use the generic elf code. */
4942 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
4943 char *buf ATTRIBUTE_UNUSED
,
4944 int *bufsiz ATTRIBUTE_UNUSED
,
4945 int note_type ATTRIBUTE_UNUSED
, ...)
4952 #if !defined (HAVE_PRPSINFO_T)
4955 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
4958 va_start (ap
, note_type
);
4959 memset (data
, 0, sizeof (data
));
4960 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
4961 PRPSINFO_PR_FNAME_LENGTH
);
4962 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
4964 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
4965 -Wstringop-truncation:
4966 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
4968 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
4970 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
4971 PRPSINFO_PR_PSARGS_LENGTH
);
4972 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
4976 return elfcore_write_note (abfd
, buf
, bufsiz
,
4977 "CORE", note_type
, data
, sizeof (data
));
4979 #endif /* !HAVE_PRPSINFO_T */
4981 #if !defined (HAVE_PRSTATUS_T)
4984 char data
[PRSTATUS_SIZE
];
4990 va_start (ap
, note_type
);
4991 memset (data
, 0, sizeof(data
));
4992 pid
= va_arg (ap
, long);
4993 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
4994 cursig
= va_arg (ap
, int);
4995 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
4996 greg
= va_arg (ap
, const void *);
4997 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
4998 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5000 return elfcore_write_note (abfd
, buf
, bufsiz
,
5001 "CORE", note_type
, data
, sizeof (data
));
5003 #endif /* !HAVE_PRSTATUS_T */
5007 /* Support for core dump NOTE sections. */
5010 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5012 switch (note
->descsz
)
5017 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5019 elf_tdata (abfd
)->core
->signal
5020 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5023 elf_tdata (abfd
)->core
->lwpid
5024 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5028 /* Make a ".reg/999" section. */
5029 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5030 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5034 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5036 switch (note
->descsz
)
5041 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5043 elf_tdata (abfd
)->core
->pid
5044 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5047 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5048 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5049 PRPSINFO_PR_FNAME_LENGTH
);
5052 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5053 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5054 PRPSINFO_PR_PSARGS_LENGTH
);
5058 /* Note that for some reason, a spurious space is tacked
5059 onto the end of the args in some (at least one anyway)
5060 implementations, so strip it off if it exists. */
5063 char *command
= elf_tdata (abfd
)->core
->command
;
5064 int n
= strlen (command
);
5066 if (0 < n
&& command
[n
- 1] == ' ')
5067 command
[n
- 1] = '\0';
5073 /* Set the right mach type. */
5076 riscv_elf_object_p (bfd
*abfd
)
5078 /* There are only two mach types in RISCV currently. */
5079 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5080 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5081 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5083 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5088 /* Determine whether an object attribute tag takes an integer, a
5092 riscv_elf_obj_attrs_arg_type (int tag
)
5094 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5097 /* PR27584, Omit local and empty symbols since they usually generated
5098 for pcrel relocations. */
5101 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5103 return (!strcmp (sym
->name
, "")
5104 || _bfd_elf_is_local_label_name (abfd
, sym
->name
));
5107 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5108 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5109 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5110 #define TARGET_BIG_NAME "elfNN-bigriscv"
5112 #define elf_backend_reloc_type_class riscv_reloc_type_class
5114 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5115 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5116 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5117 #define bfd_elfNN_bfd_merge_private_bfd_data \
5118 _bfd_riscv_elf_merge_private_bfd_data
5119 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5121 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5122 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5123 #define elf_backend_check_relocs riscv_elf_check_relocs
5124 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5125 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
5126 #define elf_backend_relocate_section riscv_elf_relocate_section
5127 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5128 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5129 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
5130 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5131 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5132 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5133 #define elf_backend_object_p riscv_elf_object_p
5134 #define elf_backend_write_core_note riscv_write_core_note
5135 #define elf_info_to_howto_rel NULL
5136 #define elf_info_to_howto riscv_info_to_howto_rela
5137 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5138 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5140 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5142 #define elf_backend_can_gc_sections 1
5143 #define elf_backend_can_refcount 1
5144 #define elf_backend_want_got_plt 1
5145 #define elf_backend_plt_readonly 1
5146 #define elf_backend_plt_alignment 4
5147 #define elf_backend_want_plt_sym 1
5148 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5149 #define elf_backend_want_dynrelro 1
5150 #define elf_backend_rela_normal 1
5151 #define elf_backend_default_execstack 0
5153 #undef elf_backend_obj_attrs_vendor
5154 #define elf_backend_obj_attrs_vendor "riscv"
5155 #undef elf_backend_obj_attrs_arg_type
5156 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5157 #undef elf_backend_obj_attrs_section_type
5158 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5159 #undef elf_backend_obj_attrs_section
5160 #define elf_backend_obj_attrs_section ".riscv.attributes"
5162 #include "elfNN-target.h"