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"
41 /* Internal relocations used exclusively by the relaxation pass. */
42 #define R_RISCV_DELETE (R_RISCV_max + 1)
46 #define MINUS_ONE ((bfd_vma)0 - 1)
48 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
50 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
52 /* The name of the dynamic interpreter. This is put in the .interp
55 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
56 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
58 #define ELF_ARCH bfd_arch_riscv
59 #define ELF_TARGET_ID RISCV_ELF_DATA
60 #define ELF_MACHINE_CODE EM_RISCV
61 #define ELF_MAXPAGESIZE 0x1000
62 #define ELF_COMMONPAGESIZE 0x1000
64 #define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
66 /* RISC-V ELF linker hash entry. */
68 struct riscv_elf_link_hash_entry
70 struct elf_link_hash_entry elf
;
80 #define riscv_elf_hash_entry(ent) \
81 ((struct riscv_elf_link_hash_entry *) (ent))
83 struct _bfd_riscv_elf_obj_tdata
85 struct elf_obj_tdata root
;
87 /* tls_type for each local got entry. */
88 char *local_got_tls_type
;
91 #define _bfd_riscv_elf_tdata(abfd) \
92 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
94 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
95 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
97 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
98 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
99 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
101 #define is_riscv_elf(bfd) \
102 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
103 && elf_tdata (bfd) != NULL \
104 && elf_object_id (bfd) == RISCV_ELF_DATA)
107 elfNN_riscv_mkobject (bfd
*abfd
)
109 return bfd_elf_allocate_object (abfd
,
110 sizeof (struct _bfd_riscv_elf_obj_tdata
),
114 #include "elf/common.h"
115 #include "elf/internal.h"
117 struct riscv_elf_link_hash_table
119 struct elf_link_hash_table elf
;
121 /* Short-cuts to get to dynamic linker sections. */
124 /* The max alignment of output sections. */
125 bfd_vma max_alignment
;
127 /* Used by local STT_GNU_IFUNC symbols. */
128 htab_t loc_hash_table
;
129 void * loc_hash_memory
;
131 /* The index of the last unused .rel.iplt slot. */
132 bfd_vma last_iplt_index
;
134 /* The data segment phase, don't relax the section
135 when it is exp_seg_relro_adjust. */
136 int *data_segment_phase
;
139 /* Instruction access functions. */
140 #define riscv_get_insn(bits, ptr) \
141 ((bits) == 16 ? bfd_getl16 (ptr) \
142 : (bits) == 32 ? bfd_getl32 (ptr) \
143 : (bits) == 64 ? bfd_getl64 (ptr) \
144 : (abort (), (bfd_vma) - 1))
145 #define riscv_put_insn(bits, val, ptr) \
146 ((bits) == 16 ? bfd_putl16 (val, ptr) \
147 : (bits) == 32 ? bfd_putl32 (val, ptr) \
148 : (bits) == 64 ? bfd_putl64 (val, ptr) \
149 : (abort (), (void) 0))
151 /* Get the RISC-V ELF linker hash table from a link_info structure. */
152 #define riscv_elf_hash_table(p) \
153 ((is_elf_hash_table ((p)->hash) \
154 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
155 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
158 riscv_info_to_howto_rela (bfd
*abfd
,
160 Elf_Internal_Rela
*dst
)
162 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
163 return cache_ptr
->howto
!= NULL
;
167 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
169 const struct elf_backend_data
*bed
;
172 bed
= get_elf_backend_data (abfd
);
173 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
174 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
177 /* Return true if a relocation is modifying an instruction. */
180 riscv_is_insn_reloc (const reloc_howto_type
*howto
)
182 /* Heuristic: A multibyte destination with a nontrivial mask
184 return (howto
->bitsize
> 8
185 && howto
->dst_mask
!= 0
186 && ~(howto
->dst_mask
| (howto
->bitsize
< sizeof(bfd_vma
) * CHAR_BIT
187 ? (MINUS_ONE
<< howto
->bitsize
) : (bfd_vma
)0)) != 0);
191 #define PLT_HEADER_INSNS 8
192 #define PLT_ENTRY_INSNS 4
193 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
194 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
195 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
196 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
197 the other is used for link map. Other targets also reserve one more
198 entry used for runtime profile? */
199 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
201 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
204 # define MATCH_LREG MATCH_LW
206 # define MATCH_LREG MATCH_LD
209 /* Generate a PLT header. */
212 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
215 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
216 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
218 /* RVE has no t3 register, so this won't work, and is not supported. */
219 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
221 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
226 /* auipc t2, %hi(.got.plt)
227 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
228 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
229 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
230 addi t0, t2, %lo(.got.plt) # &.got.plt
231 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
232 l[w|d] t0, PTRSIZE(t0) # link map
235 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
236 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
237 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
238 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
239 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
240 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
241 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
242 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
247 /* Generate a PLT entry. */
250 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
253 /* RVE has no t3 register, so this won't work, and is not supported. */
254 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
256 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
261 /* auipc t3, %hi(.got.plt entry)
262 l[w|d] t3, %lo(.got.plt entry)(t3)
266 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
267 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
268 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
269 entry
[3] = RISCV_NOP
;
274 /* Create an entry in an RISC-V ELF linker hash table. */
276 static struct bfd_hash_entry
*
277 link_hash_newfunc (struct bfd_hash_entry
*entry
,
278 struct bfd_hash_table
*table
, const char *string
)
280 /* Allocate the structure if it has not already been allocated by a
285 bfd_hash_allocate (table
,
286 sizeof (struct riscv_elf_link_hash_entry
));
291 /* Call the allocation method of the superclass. */
292 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
295 struct riscv_elf_link_hash_entry
*eh
;
297 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
298 eh
->tls_type
= GOT_UNKNOWN
;
304 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
305 for local symbol so that we can handle local STT_GNU_IFUNC symbols
306 as global symbol. We reuse indx and dynstr_index for local symbol
307 hash since they aren't used by global symbols in this backend. */
310 riscv_elf_local_htab_hash (const void *ptr
)
312 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) ptr
;
313 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
316 /* Compare local hash entries. */
319 riscv_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
321 struct elf_link_hash_entry
*h1
= (struct elf_link_hash_entry
*) ptr1
;
322 struct elf_link_hash_entry
*h2
= (struct elf_link_hash_entry
*) ptr2
;
324 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
327 /* Find and/or create a hash entry for local symbol. */
329 static struct elf_link_hash_entry
*
330 riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table
*htab
,
331 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
334 struct riscv_elf_link_hash_entry eh
, *ret
;
335 asection
*sec
= abfd
->sections
;
336 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
337 ELFNN_R_SYM (rel
->r_info
));
340 eh
.elf
.indx
= sec
->id
;
341 eh
.elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
342 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &eh
, h
,
343 create
? INSERT
: NO_INSERT
);
350 ret
= (struct riscv_elf_link_hash_entry
*) *slot
;
354 ret
= (struct riscv_elf_link_hash_entry
*)
355 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
356 sizeof (struct riscv_elf_link_hash_entry
));
359 memset (ret
, 0, sizeof (*ret
));
360 ret
->elf
.indx
= sec
->id
;
361 ret
->elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
362 ret
->elf
.dynindx
= -1;
368 /* Destroy a RISC-V elf linker hash table. */
371 riscv_elf_link_hash_table_free (bfd
*obfd
)
373 struct riscv_elf_link_hash_table
*ret
374 = (struct riscv_elf_link_hash_table
*) obfd
->link
.hash
;
376 if (ret
->loc_hash_table
)
377 htab_delete (ret
->loc_hash_table
);
378 if (ret
->loc_hash_memory
)
379 objalloc_free ((struct objalloc
*) ret
->loc_hash_memory
);
381 _bfd_elf_link_hash_table_free (obfd
);
384 /* Create a RISC-V ELF linker hash table. */
386 static struct bfd_link_hash_table
*
387 riscv_elf_link_hash_table_create (bfd
*abfd
)
389 struct riscv_elf_link_hash_table
*ret
;
390 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
392 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
396 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
397 sizeof (struct riscv_elf_link_hash_entry
),
404 ret
->max_alignment
= (bfd_vma
) -1;
406 /* Create hash table for local ifunc. */
407 ret
->loc_hash_table
= htab_try_create (1024,
408 riscv_elf_local_htab_hash
,
409 riscv_elf_local_htab_eq
,
411 ret
->loc_hash_memory
= objalloc_create ();
412 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
414 riscv_elf_link_hash_table_free (abfd
);
417 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
419 return &ret
->elf
.root
;
422 /* Create the .got section. */
425 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
429 struct elf_link_hash_entry
*h
;
430 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
431 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
433 /* This function may be called more than once. */
434 if (htab
->sgot
!= NULL
)
437 flags
= bed
->dynamic_sec_flags
;
439 s
= bfd_make_section_anyway_with_flags (abfd
,
440 (bed
->rela_plts_and_copies_p
441 ? ".rela.got" : ".rel.got"),
442 (bed
->dynamic_sec_flags
445 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
449 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
451 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
455 /* The first bit of the global offset table is the header. */
456 s
->size
+= bed
->got_header_size
;
458 if (bed
->want_got_plt
)
460 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
462 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
466 /* Reserve room for the header. */
467 s
->size
+= GOTPLT_HEADER_SIZE
;
470 if (bed
->want_got_sym
)
472 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
473 section. We don't do this in the linker script because we don't want
474 to define the symbol if we are not creating a global offset
476 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
477 "_GLOBAL_OFFSET_TABLE_");
478 elf_hash_table (info
)->hgot
= h
;
486 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
487 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
491 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
492 struct bfd_link_info
*info
)
494 struct riscv_elf_link_hash_table
*htab
;
496 htab
= riscv_elf_hash_table (info
);
497 BFD_ASSERT (htab
!= NULL
);
499 if (!riscv_elf_create_got_section (dynobj
, info
))
502 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
505 if (!bfd_link_pic (info
))
507 /* Technically, this section doesn't have contents. It is used as the
508 target of TLS copy relocs, to copy TLS data from shared libraries into
509 the executable. However, if we don't mark it as loadable, then it
510 matches the IS_TBSS test in ldlang.c, and there is no run-time address
511 space allocated for it even though it has SEC_ALLOC. That test is
512 correct for .tbss, but not correct for this section. There is also
513 a second problem that having a section with no contents can only work
514 if it comes after all sections with contents in the same segment,
515 but the linker script does not guarantee that. This is just mixed in
516 with other .tdata.* sections. We can fix both problems by lying and
517 saying that there are contents. This section is expected to be small
518 so this should not cause a significant extra program startup cost. */
520 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
521 (SEC_ALLOC
| SEC_THREAD_LOCAL
522 | SEC_LOAD
| SEC_DATA
524 | SEC_LINKER_CREATED
));
527 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
528 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
534 /* Copy the extra info we tack onto an elf_link_hash_entry. */
537 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
538 struct elf_link_hash_entry
*dir
,
539 struct elf_link_hash_entry
*ind
)
541 struct riscv_elf_link_hash_entry
*edir
, *eind
;
543 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
544 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
546 if (ind
->root
.type
== bfd_link_hash_indirect
547 && dir
->got
.refcount
<= 0)
549 edir
->tls_type
= eind
->tls_type
;
550 eind
->tls_type
= GOT_UNKNOWN
;
552 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
556 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
557 unsigned long symndx
, char tls_type
)
559 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
561 *new_tls_type
|= tls_type
;
562 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
564 (*_bfd_error_handler
)
565 (_("%pB: `%s' accessed both as normal and thread local symbol"),
566 abfd
, h
? h
->root
.root
.string
: "<local>");
573 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
574 struct elf_link_hash_entry
*h
, long symndx
)
576 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
577 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
579 if (htab
->elf
.sgot
== NULL
)
581 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
587 h
->got
.refcount
+= 1;
591 /* This is a global offset table entry for a local symbol. */
592 if (elf_local_got_refcounts (abfd
) == NULL
)
594 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
595 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
597 _bfd_riscv_elf_local_got_tls_type (abfd
)
598 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
600 elf_local_got_refcounts (abfd
) [symndx
] += 1;
606 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
608 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
610 /* We propably can improve the information to tell users that they
611 should be recompile the code with -fPIC or -fPIE, just like what
613 (*_bfd_error_handler
)
614 (_("%pB: relocation %s against `%s' can not be used when making a shared "
615 "object; recompile with -fPIC"),
616 abfd
, r
? r
->name
: _("<unknown>"),
617 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
618 bfd_set_error (bfd_error_bad_value
);
622 /* Look through the relocs for a section during the first phase, and
623 allocate space in the global offset table or procedure linkage
627 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
628 asection
*sec
, const Elf_Internal_Rela
*relocs
)
630 struct riscv_elf_link_hash_table
*htab
;
631 Elf_Internal_Shdr
*symtab_hdr
;
632 struct elf_link_hash_entry
**sym_hashes
;
633 const Elf_Internal_Rela
*rel
;
634 asection
*sreloc
= NULL
;
636 if (bfd_link_relocatable (info
))
639 htab
= riscv_elf_hash_table (info
);
640 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
641 sym_hashes
= elf_sym_hashes (abfd
);
643 if (htab
->elf
.dynobj
== NULL
)
644 htab
->elf
.dynobj
= abfd
;
646 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
649 unsigned int r_symndx
;
650 struct elf_link_hash_entry
*h
;
652 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
653 r_type
= ELFNN_R_TYPE (rel
->r_info
);
655 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
657 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
662 if (r_symndx
< symtab_hdr
->sh_info
)
664 /* A local symbol. */
665 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
670 /* Check relocation against local STT_GNU_IFUNC symbol. */
671 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
673 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
677 /* Fake STT_GNU_IFUNC global symbol. */
678 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
680 h
->type
= STT_GNU_IFUNC
;
684 h
->root
.type
= bfd_link_hash_defined
;
691 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
692 while (h
->root
.type
== bfd_link_hash_indirect
693 || h
->root
.type
== bfd_link_hash_warning
)
694 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
704 case R_RISCV_CALL_PLT
:
706 case R_RISCV_GOT_HI20
:
707 case R_RISCV_PCREL_HI20
:
708 /* Create the ifunc sections, iplt and ipltgot, for static
710 if (h
->type
== STT_GNU_IFUNC
711 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
719 /* It is referenced by a non-shared object. */
725 case R_RISCV_TLS_GD_HI20
:
726 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
727 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
731 case R_RISCV_TLS_GOT_HI20
:
732 if (bfd_link_pic (info
))
733 info
->flags
|= DF_STATIC_TLS
;
734 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
735 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
739 case R_RISCV_GOT_HI20
:
740 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
741 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
746 case R_RISCV_CALL_PLT
:
747 /* These symbol requires a procedure linkage table entry.
748 We actually build the entry in adjust_dynamic_symbol,
749 because these might be a case of linking PIC code without
750 linking in any dynamic objects, in which case we don't
751 need to generate a procedure linkage table after all. */
753 /* If it is a local symbol, then we resolve it directly
754 without creating a PLT entry. */
759 h
->plt
.refcount
+= 1;
762 case R_RISCV_PCREL_HI20
:
764 && h
->type
== STT_GNU_IFUNC
)
767 h
->pointer_equality_needed
= 1;
769 /* We don't use the PCREL_HI20 in the data section,
770 so we always need the plt when it refers to
772 h
->plt
.refcount
+= 1;
778 case R_RISCV_RVC_BRANCH
:
779 case R_RISCV_RVC_JUMP
:
780 /* In shared libraries and pie, these relocs are known
782 if (bfd_link_pic (info
))
786 case R_RISCV_TPREL_HI20
:
787 if (!bfd_link_executable (info
))
788 return bad_static_reloc (abfd
, r_type
, h
);
790 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
794 if (bfd_link_pic (info
))
795 return bad_static_reloc (abfd
, r_type
, h
);
799 case R_RISCV_JUMP_SLOT
:
800 case R_RISCV_RELATIVE
:
808 && (!bfd_link_pic (info
)
809 || h
->type
== STT_GNU_IFUNC
))
811 /* This reloc might not bind locally. */
813 h
->pointer_equality_needed
= 1;
816 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
818 /* We may need a .plt entry if the symbol is a function
819 defined in a shared lib or is a function referenced
820 from the code or read-only section. */
821 h
->plt
.refcount
+= 1;
825 /* If we are creating a shared library, and this is a reloc
826 against a global symbol, or a non PC relative reloc
827 against a local symbol, then we need to copy the reloc
828 into the shared library. However, if we are linking with
829 -Bsymbolic, we do not need to copy a reloc against a
830 global symbol which is defined in an object we are
831 including in the link (i.e., DEF_REGULAR is set). At
832 this point we have not seen all the input files, so it is
833 possible that DEF_REGULAR is not set now but will be set
834 later (it is never cleared). In case of a weak definition,
835 DEF_REGULAR may be cleared later by a strong definition in
836 a shared library. We account for that possibility below by
837 storing information in the relocs_copied field of the hash
838 table entry. A similar situation occurs when creating
839 shared libraries and symbol visibility changes render the
842 If on the other hand, we are creating an executable, we
843 may need to keep relocations for symbols satisfied by a
844 dynamic library if we manage to avoid copy relocs for the
847 Generate dynamic pointer relocation against STT_GNU_IFUNC
848 symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
849 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
851 if ((bfd_link_pic (info
)
852 && (sec
->flags
& SEC_ALLOC
) != 0
853 && ((r
!= NULL
&& !r
->pc_relative
)
856 || h
->root
.type
== bfd_link_hash_defweak
857 || !h
->def_regular
))))
858 || (!bfd_link_pic (info
)
859 && (sec
->flags
& SEC_ALLOC
) != 0
861 && (h
->root
.type
== bfd_link_hash_defweak
863 || (!bfd_link_pic (info
)
865 && h
->type
== STT_GNU_IFUNC
866 && (sec
->flags
& SEC_CODE
) == 0))
868 struct elf_dyn_relocs
*p
;
869 struct elf_dyn_relocs
**head
;
871 /* When creating a shared object, we must copy these
872 relocs into the output file. We create a reloc
873 section in dynobj and make room for the reloc. */
876 sreloc
= _bfd_elf_make_dynamic_reloc_section
877 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
878 abfd
, /*rela?*/ true);
884 /* If this is a global symbol, we count the number of
885 relocations we need for this symbol. */
887 head
= &h
->dyn_relocs
;
890 /* Track dynamic relocs needed for local syms too.
891 We really need local syms available to do this
896 Elf_Internal_Sym
*isym
;
898 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
903 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
907 vpp
= &elf_section_data (s
)->local_dynrel
;
908 head
= (struct elf_dyn_relocs
**) vpp
;
912 if (p
== NULL
|| p
->sec
!= sec
)
914 size_t amt
= sizeof *p
;
915 p
= ((struct elf_dyn_relocs
*)
916 bfd_alloc (htab
->elf
.dynobj
, amt
));
927 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
932 case R_RISCV_GNU_VTINHERIT
:
933 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
937 case R_RISCV_GNU_VTENTRY
:
938 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
951 riscv_elf_gc_mark_hook (asection
*sec
,
952 struct bfd_link_info
*info
,
953 Elf_Internal_Rela
*rel
,
954 struct elf_link_hash_entry
*h
,
955 Elf_Internal_Sym
*sym
)
958 switch (ELFNN_R_TYPE (rel
->r_info
))
960 case R_RISCV_GNU_VTINHERIT
:
961 case R_RISCV_GNU_VTENTRY
:
965 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
968 /* Adjust a symbol defined by a dynamic object and referenced by a
969 regular object. The current definition is in some section of the
970 dynamic object, but we're not including those sections. We have to
971 change the definition to something the rest of the link can
975 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
976 struct elf_link_hash_entry
*h
)
978 struct riscv_elf_link_hash_table
*htab
;
979 struct riscv_elf_link_hash_entry
* eh
;
983 htab
= riscv_elf_hash_table (info
);
984 BFD_ASSERT (htab
!= NULL
);
986 dynobj
= htab
->elf
.dynobj
;
988 /* Make sure we know what is going on here. */
989 BFD_ASSERT (dynobj
!= NULL
991 || h
->type
== STT_GNU_IFUNC
995 && !h
->def_regular
)));
997 /* If this is a function, put it in the procedure linkage table. We
998 will fill in the contents of the procedure linkage table later
999 (although we could actually do it here). */
1000 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
1002 if (h
->plt
.refcount
<= 0
1003 || (h
->type
!= STT_GNU_IFUNC
1004 && (SYMBOL_CALLS_LOCAL (info
, h
)
1005 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1006 && h
->root
.type
== bfd_link_hash_undefweak
))))
1008 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1009 input file, but the symbol was never referred to by a dynamic
1010 object, or if all references were garbage collected. In such
1011 a case, we don't actually need to build a PLT entry. */
1012 h
->plt
.offset
= (bfd_vma
) -1;
1019 h
->plt
.offset
= (bfd_vma
) -1;
1021 /* If this is a weak symbol, and there is a real definition, the
1022 processor independent code will have arranged for us to see the
1023 real definition first, and we can just use the same value. */
1024 if (h
->is_weakalias
)
1026 struct elf_link_hash_entry
*def
= weakdef (h
);
1027 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1028 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1029 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1033 /* This is a reference to a symbol defined by a dynamic object which
1034 is not a function. */
1036 /* If we are creating a shared library, we must presume that the
1037 only references to the symbol are via the global offset table.
1038 For such cases we need not do anything here; the relocations will
1039 be handled correctly by relocate_section. */
1040 if (bfd_link_pic (info
))
1043 /* If there are no references to this symbol that do not use the
1044 GOT, we don't need to generate a copy reloc. */
1045 if (!h
->non_got_ref
)
1048 /* If -z nocopyreloc was given, we won't generate them either. */
1049 if (info
->nocopyreloc
)
1055 /* If we don't find any dynamic relocs in read-only sections, then
1056 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1057 if (!_bfd_elf_readonly_dynrelocs (h
))
1063 /* We must allocate the symbol in our .dynbss section, which will
1064 become part of the .bss section of the executable. There will be
1065 an entry for this symbol in the .dynsym section. The dynamic
1066 object will contain position independent code, so all references
1067 from the dynamic object to this symbol will go through the global
1068 offset table. The dynamic linker will use the .dynsym entry to
1069 determine the address it must put in the global offset table, so
1070 both the dynamic object and the regular object will refer to the
1071 same memory location for the variable. */
1073 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1074 to copy the initial value out of the dynamic object and into the
1075 runtime process image. We need to remember the offset into the
1076 .rel.bss section we are going to use. */
1077 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1078 if (eh
->tls_type
& ~GOT_NORMAL
)
1080 s
= htab
->sdyntdata
;
1081 srel
= htab
->elf
.srelbss
;
1083 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1085 s
= htab
->elf
.sdynrelro
;
1086 srel
= htab
->elf
.sreldynrelro
;
1090 s
= htab
->elf
.sdynbss
;
1091 srel
= htab
->elf
.srelbss
;
1093 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1095 srel
->size
+= sizeof (ElfNN_External_Rela
);
1099 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1102 /* Allocate space in .plt, .got and associated reloc sections for
1106 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1108 struct bfd_link_info
*info
;
1109 struct riscv_elf_link_hash_table
*htab
;
1110 struct elf_dyn_relocs
*p
;
1112 if (h
->root
.type
== bfd_link_hash_indirect
)
1115 info
= (struct bfd_link_info
*) inf
;
1116 htab
= riscv_elf_hash_table (info
);
1117 BFD_ASSERT (htab
!= NULL
);
1119 /* When we are generating pde, make sure gp symbol is output as a
1120 dynamic symbol. Then ld.so can set the gp register earlier, before
1121 resolving the ifunc. */
1122 if (!bfd_link_pic (info
)
1123 && htab
->elf
.dynamic_sections_created
1124 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1125 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1128 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1129 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1130 if they are defined and referenced in a non-shared object. */
1131 if (h
->type
== STT_GNU_IFUNC
1134 else if (htab
->elf
.dynamic_sections_created
1135 && h
->plt
.refcount
> 0)
1137 /* Make sure this symbol is output as a dynamic symbol.
1138 Undefined weak syms won't yet be marked as dynamic. */
1139 if (h
->dynindx
== -1
1140 && !h
->forced_local
)
1142 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1146 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1148 asection
*s
= htab
->elf
.splt
;
1151 s
->size
= PLT_HEADER_SIZE
;
1153 h
->plt
.offset
= s
->size
;
1155 /* Make room for this entry. */
1156 s
->size
+= PLT_ENTRY_SIZE
;
1158 /* We also need to make an entry in the .got.plt section. */
1159 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1161 /* We also need to make an entry in the .rela.plt section. */
1162 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1164 /* If this symbol is not defined in a regular file, and we are
1165 not generating a shared library, then set the symbol to this
1166 location in the .plt. This is required to make function
1167 pointers compare as equal between the normal executable and
1168 the shared library. */
1169 if (! bfd_link_pic (info
)
1172 h
->root
.u
.def
.section
= s
;
1173 h
->root
.u
.def
.value
= h
->plt
.offset
;
1178 h
->plt
.offset
= (bfd_vma
) -1;
1184 h
->plt
.offset
= (bfd_vma
) -1;
1188 if (h
->got
.refcount
> 0)
1192 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1194 /* Make sure this symbol is output as a dynamic symbol.
1195 Undefined weak syms won't yet be marked as dynamic. */
1196 if (h
->dynindx
== -1
1197 && !h
->forced_local
)
1199 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1204 h
->got
.offset
= s
->size
;
1205 dyn
= htab
->elf
.dynamic_sections_created
;
1206 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1208 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1209 if (tls_type
& GOT_TLS_GD
)
1211 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1212 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1215 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1216 if (tls_type
& GOT_TLS_IE
)
1218 s
->size
+= RISCV_ELF_WORD_BYTES
;
1219 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1224 s
->size
+= RISCV_ELF_WORD_BYTES
;
1225 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1226 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1227 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1231 h
->got
.offset
= (bfd_vma
) -1;
1233 if (h
->dyn_relocs
== NULL
)
1236 /* In the shared -Bsymbolic case, discard space allocated for
1237 dynamic pc-relative relocs against symbols which turn out to be
1238 defined in regular objects. For the normal shared case, discard
1239 space for pc-relative relocs that have become local due to symbol
1240 visibility changes. */
1242 if (bfd_link_pic (info
))
1244 if (SYMBOL_CALLS_LOCAL (info
, h
))
1246 struct elf_dyn_relocs
**pp
;
1248 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1250 p
->count
-= p
->pc_count
;
1259 /* Also discard relocs on undefined weak syms with non-default
1261 if (h
->dyn_relocs
!= NULL
1262 && h
->root
.type
== bfd_link_hash_undefweak
)
1264 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1265 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1266 h
->dyn_relocs
= NULL
;
1268 /* Make sure undefined weak symbols are output as a dynamic
1270 else if (h
->dynindx
== -1
1271 && !h
->forced_local
)
1273 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1280 /* For the non-shared case, discard space for relocs against
1281 symbols which turn out to need copy relocs or are not
1287 || (htab
->elf
.dynamic_sections_created
1288 && (h
->root
.type
== bfd_link_hash_undefweak
1289 || h
->root
.type
== bfd_link_hash_undefined
))))
1291 /* Make sure this symbol is output as a dynamic symbol.
1292 Undefined weak syms won't yet be marked as dynamic. */
1293 if (h
->dynindx
== -1
1294 && !h
->forced_local
)
1296 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1300 /* If that succeeded, we know we'll be keeping all the
1302 if (h
->dynindx
!= -1)
1306 h
->dyn_relocs
= NULL
;
1311 /* Finally, allocate space. */
1312 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1314 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1315 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1321 /* Allocate space in .plt, .got and associated reloc sections for
1322 ifunc dynamic relocs. */
1325 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1328 struct bfd_link_info
*info
;
1330 if (h
->root
.type
== bfd_link_hash_indirect
)
1333 if (h
->root
.type
== bfd_link_hash_warning
)
1334 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1336 info
= (struct bfd_link_info
*) inf
;
1338 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1339 here if it is defined and referenced in a non-shared object. */
1340 if (h
->type
== STT_GNU_IFUNC
1342 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1351 /* Allocate space in .plt, .got and associated reloc sections for
1352 local ifunc dynamic relocs. */
1355 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1357 struct elf_link_hash_entry
*h
1358 = (struct elf_link_hash_entry
*) *slot
;
1360 if (h
->type
!= STT_GNU_IFUNC
1364 || h
->root
.type
!= bfd_link_hash_defined
)
1367 return allocate_ifunc_dynrelocs (h
, inf
);
1371 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1373 struct riscv_elf_link_hash_table
*htab
;
1378 htab
= riscv_elf_hash_table (info
);
1379 BFD_ASSERT (htab
!= NULL
);
1380 dynobj
= htab
->elf
.dynobj
;
1381 BFD_ASSERT (dynobj
!= NULL
);
1383 if (elf_hash_table (info
)->dynamic_sections_created
)
1385 /* Set the contents of the .interp section to the interpreter. */
1386 if (bfd_link_executable (info
) && !info
->nointerp
)
1388 s
= bfd_get_linker_section (dynobj
, ".interp");
1389 BFD_ASSERT (s
!= NULL
);
1390 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1391 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1395 /* Set up .got offsets for local syms, and space for local dynamic
1397 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1399 bfd_signed_vma
*local_got
;
1400 bfd_signed_vma
*end_local_got
;
1401 char *local_tls_type
;
1402 bfd_size_type locsymcount
;
1403 Elf_Internal_Shdr
*symtab_hdr
;
1406 if (! is_riscv_elf (ibfd
))
1409 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1411 struct elf_dyn_relocs
*p
;
1413 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1415 if (!bfd_is_abs_section (p
->sec
)
1416 && bfd_is_abs_section (p
->sec
->output_section
))
1418 /* Input section has been discarded, either because
1419 it is a copy of a linkonce section or due to
1420 linker script /DISCARD/, so we'll be discarding
1423 else if (p
->count
!= 0)
1425 srel
= elf_section_data (p
->sec
)->sreloc
;
1426 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1427 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1428 info
->flags
|= DF_TEXTREL
;
1433 local_got
= elf_local_got_refcounts (ibfd
);
1437 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1438 locsymcount
= symtab_hdr
->sh_info
;
1439 end_local_got
= local_got
+ locsymcount
;
1440 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1442 srel
= htab
->elf
.srelgot
;
1443 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1447 *local_got
= s
->size
;
1448 s
->size
+= RISCV_ELF_WORD_BYTES
;
1449 if (*local_tls_type
& GOT_TLS_GD
)
1450 s
->size
+= RISCV_ELF_WORD_BYTES
;
1451 if (bfd_link_pic (info
)
1452 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1453 srel
->size
+= sizeof (ElfNN_External_Rela
);
1456 *local_got
= (bfd_vma
) -1;
1460 /* Allocate .plt and .got entries and space dynamic relocs for
1462 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1464 /* Allocate .plt and .got entries and space dynamic relocs for
1465 global ifunc symbols. */
1466 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1468 /* Allocate .plt and .got entries and space dynamic relocs for
1469 local ifunc symbols. */
1470 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1472 /* Used to resolve the dynamic relocs overwite problems when
1473 generating static executable. */
1474 if (htab
->elf
.irelplt
)
1475 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1477 if (htab
->elf
.sgotplt
)
1479 struct elf_link_hash_entry
*got
;
1480 got
= elf_link_hash_lookup (elf_hash_table (info
),
1481 "_GLOBAL_OFFSET_TABLE_",
1482 false, false, false);
1484 /* Don't allocate .got.plt section if there are no GOT nor PLT
1485 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1487 || !got
->ref_regular_nonweak
)
1488 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1489 && (htab
->elf
.splt
== NULL
1490 || htab
->elf
.splt
->size
== 0)
1491 && (htab
->elf
.sgot
== NULL
1492 || (htab
->elf
.sgot
->size
1493 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1494 htab
->elf
.sgotplt
->size
= 0;
1497 /* The check_relocs and adjust_dynamic_symbol entry points have
1498 determined the sizes of the various dynamic sections. Allocate
1500 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1502 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1505 if (s
== htab
->elf
.splt
1506 || s
== htab
->elf
.sgot
1507 || s
== htab
->elf
.sgotplt
1508 || s
== htab
->elf
.iplt
1509 || s
== htab
->elf
.igotplt
1510 || s
== htab
->elf
.sdynbss
1511 || s
== htab
->elf
.sdynrelro
1512 || s
== htab
->sdyntdata
)
1514 /* Strip this section if we don't need it; see the
1517 else if (startswith (s
->name
, ".rela"))
1521 /* We use the reloc_count field as a counter if we need
1522 to copy relocs into the output file. */
1528 /* It's not one of our sections. */
1534 /* If we don't need this section, strip it from the
1535 output file. This is mostly to handle .rela.bss and
1536 .rela.plt. We must create both sections in
1537 create_dynamic_sections, because they must be created
1538 before the linker maps input sections to output
1539 sections. The linker does that before
1540 adjust_dynamic_symbol is called, and it is that
1541 function which decides whether anything needs to go
1542 into these sections. */
1543 s
->flags
|= SEC_EXCLUDE
;
1547 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1550 /* Allocate memory for the section contents. Zero the memory
1551 for the benefit of .rela.plt, which has 4 unused entries
1552 at the beginning, and we don't want garbage. */
1553 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1554 if (s
->contents
== NULL
)
1558 return _bfd_elf_add_dynamic_tags (output_bfd
, info
, true);
1562 #define DTP_OFFSET 0x800
1564 /* Return the relocation value for a TLS dtp-relative reloc. */
1567 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1569 /* If tls_sec is NULL, we should have signalled an error already. */
1570 if (elf_hash_table (info
)->tls_sec
== NULL
)
1572 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1575 /* Return the relocation value for a static TLS tp-relative relocation. */
1578 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1580 /* If tls_sec is NULL, we should have signalled an error already. */
1581 if (elf_hash_table (info
)->tls_sec
== NULL
)
1583 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1586 /* Return the global pointer's value, or 0 if it is not in use. */
1589 riscv_global_pointer_value (struct bfd_link_info
*info
)
1591 struct bfd_link_hash_entry
*h
;
1593 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1594 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1597 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1600 /* Emplace a static relocation. */
1602 static bfd_reloc_status_type
1603 perform_relocation (const reloc_howto_type
*howto
,
1604 const Elf_Internal_Rela
*rel
,
1606 asection
*input_section
,
1610 if (howto
->pc_relative
)
1611 value
-= sec_addr (input_section
) + rel
->r_offset
;
1612 value
+= rel
->r_addend
;
1614 switch (ELFNN_R_TYPE (rel
->r_info
))
1617 case R_RISCV_TPREL_HI20
:
1618 case R_RISCV_PCREL_HI20
:
1619 case R_RISCV_GOT_HI20
:
1620 case R_RISCV_TLS_GOT_HI20
:
1621 case R_RISCV_TLS_GD_HI20
:
1622 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1623 return bfd_reloc_overflow
;
1624 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1627 case R_RISCV_LO12_I
:
1628 case R_RISCV_GPREL_I
:
1629 case R_RISCV_TPREL_LO12_I
:
1630 case R_RISCV_TPREL_I
:
1631 case R_RISCV_PCREL_LO12_I
:
1632 value
= ENCODE_ITYPE_IMM (value
);
1635 case R_RISCV_LO12_S
:
1636 case R_RISCV_GPREL_S
:
1637 case R_RISCV_TPREL_LO12_S
:
1638 case R_RISCV_TPREL_S
:
1639 case R_RISCV_PCREL_LO12_S
:
1640 value
= ENCODE_STYPE_IMM (value
);
1644 case R_RISCV_CALL_PLT
:
1645 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1646 return bfd_reloc_overflow
;
1647 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1648 | (ENCODE_ITYPE_IMM (value
) << 32);
1652 if (!VALID_JTYPE_IMM (value
))
1653 return bfd_reloc_overflow
;
1654 value
= ENCODE_JTYPE_IMM (value
);
1657 case R_RISCV_BRANCH
:
1658 if (!VALID_BTYPE_IMM (value
))
1659 return bfd_reloc_overflow
;
1660 value
= ENCODE_BTYPE_IMM (value
);
1663 case R_RISCV_RVC_BRANCH
:
1664 if (!VALID_CBTYPE_IMM (value
))
1665 return bfd_reloc_overflow
;
1666 value
= ENCODE_CBTYPE_IMM (value
);
1669 case R_RISCV_RVC_JUMP
:
1670 if (!VALID_CJTYPE_IMM (value
))
1671 return bfd_reloc_overflow
;
1672 value
= ENCODE_CJTYPE_IMM (value
);
1675 case R_RISCV_RVC_LUI
:
1676 if (RISCV_CONST_HIGH_PART (value
) == 0)
1678 /* Linker relaxation can convert an address equal to or greater than
1679 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1680 valid immediate. We can fix this by converting it to a C.LI. */
1681 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1682 contents
+ rel
->r_offset
);
1683 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1684 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1685 value
= ENCODE_CITYPE_IMM (0);
1687 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1688 return bfd_reloc_overflow
;
1690 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1708 case R_RISCV_32_PCREL
:
1709 case R_RISCV_TLS_DTPREL32
:
1710 case R_RISCV_TLS_DTPREL64
:
1713 case R_RISCV_DELETE
:
1714 return bfd_reloc_ok
;
1717 return bfd_reloc_notsupported
;
1721 if (riscv_is_insn_reloc (howto
))
1722 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1724 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1725 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1726 if (riscv_is_insn_reloc (howto
))
1727 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1729 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1731 return bfd_reloc_ok
;
1734 /* Remember all PC-relative high-part relocs we've encountered to help us
1735 later resolve the corresponding low-part relocs. */
1741 /* Relocation value with addend. */
1743 /* Original reloc type. */
1745 } riscv_pcrel_hi_reloc
;
1747 typedef struct riscv_pcrel_lo_reloc
1749 /* PC value of auipc. */
1751 /* Internal relocation. */
1752 const Elf_Internal_Rela
*reloc
;
1753 /* Record the following information helps to resolve the %pcrel
1754 which cross different input section. For now we build a hash
1755 for pcrel at the start of riscv_elf_relocate_section, and then
1756 free the hash at the end. But riscv_elf_relocate_section only
1757 handles an input section at a time, so that means we can only
1758 resolve the %pcrel_hi and %pcrel_lo which are in the same input
1759 section. Otherwise, we will report dangerous relocation errors
1760 for those %pcrel which are not in the same input section. */
1761 asection
*input_section
;
1762 struct bfd_link_info
*info
;
1763 reloc_howto_type
*howto
;
1765 /* The next riscv_pcrel_lo_reloc. */
1766 struct riscv_pcrel_lo_reloc
*next
;
1767 } riscv_pcrel_lo_reloc
;
1771 /* Hash table for riscv_pcrel_hi_reloc. */
1773 /* Linked list for riscv_pcrel_lo_reloc. */
1774 riscv_pcrel_lo_reloc
*lo_relocs
;
1775 } riscv_pcrel_relocs
;
1778 riscv_pcrel_reloc_hash (const void *entry
)
1780 const riscv_pcrel_hi_reloc
*e
= entry
;
1781 return (hashval_t
)(e
->address
>> 2);
1785 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1787 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1788 return e1
->address
== e2
->address
;
1792 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1794 p
->lo_relocs
= NULL
;
1795 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1796 riscv_pcrel_reloc_eq
, free
);
1797 return p
->hi_relocs
!= NULL
;
1801 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1803 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1807 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1812 htab_delete (p
->hi_relocs
);
1816 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1817 struct bfd_link_info
*info
,
1821 const reloc_howto_type
*howto
)
1823 /* We may need to reference low addreses in PC-relative modes even when the
1824 PC is far away from these addresses. For example, undefweak references
1825 need to produce the address 0 when linked. As 0 is far from the arbitrary
1826 addresses that we can link PC-relative programs at, the linker can't
1827 actually relocate references to those symbols. In order to allow these
1828 programs to work we simply convert the PC-relative auipc sequences to
1829 0-relative lui sequences. */
1830 if (bfd_link_pic (info
))
1833 /* If it's possible to reference the symbol using auipc we do so, as that's
1834 more in the spirit of the PC-relative relocations we're processing. */
1835 bfd_vma offset
= addr
- pc
;
1836 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1839 /* If it's impossible to reference this with a LUI-based offset then don't
1840 bother to convert it at all so users still see the PC-relative relocation
1841 in the truncation message. */
1842 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1845 rel
->r_info
= ELFNN_R_INFO (addr
, R_RISCV_HI20
);
1847 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1848 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1849 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1854 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
,
1860 bfd_vma offset
= absolute
? value
: value
- addr
;
1861 riscv_pcrel_hi_reloc entry
= {addr
, offset
, type
};
1862 riscv_pcrel_hi_reloc
**slot
=
1863 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1865 BFD_ASSERT (*slot
== NULL
);
1866 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1874 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1876 const Elf_Internal_Rela
*reloc
,
1877 asection
*input_section
,
1878 struct bfd_link_info
*info
,
1879 reloc_howto_type
*howto
,
1882 riscv_pcrel_lo_reloc
*entry
;
1883 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1886 *entry
= (riscv_pcrel_lo_reloc
) {addr
, reloc
, input_section
, info
,
1887 howto
, contents
, p
->lo_relocs
};
1888 p
->lo_relocs
= entry
;
1893 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1895 riscv_pcrel_lo_reloc
*r
;
1897 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1899 bfd
*input_bfd
= r
->input_section
->owner
;
1901 riscv_pcrel_hi_reloc search
= {r
->address
, 0, 0};
1902 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1903 /* There may be a risk if the %pcrel_lo with addend refers to
1904 an IFUNC symbol. The %pcrel_hi has been relocated to plt,
1905 so the corresponding %pcrel_lo with addend looks wrong. */
1906 char *string
= NULL
;
1908 string
= _("%pcrel_lo missing matching %pcrel_hi");
1909 else if (entry
->type
== R_RISCV_GOT_HI20
1910 && r
->reloc
->r_addend
!= 0)
1911 string
= _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
1912 else if (RISCV_CONST_HIGH_PART (entry
->value
)
1913 != RISCV_CONST_HIGH_PART (entry
->value
+ r
->reloc
->r_addend
))
1915 /* Check the overflow when adding reloc addend. */
1916 if (asprintf (&string
,
1917 _("%%pcrel_lo overflow with an addend, the "
1918 "value of %%pcrel_hi is 0x%" PRIx64
" without "
1919 "any addend, but may be 0x%" PRIx64
" after "
1920 "adding the %%pcrel_lo addend"),
1921 (int64_t) RISCV_CONST_HIGH_PART (entry
->value
),
1922 (int64_t) RISCV_CONST_HIGH_PART
1923 (entry
->value
+ r
->reloc
->r_addend
)) == -1)
1924 string
= _("%pcrel_lo overflow with an addend");
1929 (*r
->info
->callbacks
->reloc_dangerous
)
1930 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1934 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1935 input_bfd
, r
->contents
);
1941 /* Relocate a RISC-V ELF section.
1943 The RELOCATE_SECTION function is called by the new ELF backend linker
1944 to handle the relocations for a section.
1946 The relocs are always passed as Rela structures.
1948 This function is responsible for adjusting the section contents as
1949 necessary, and (if generating a relocatable output file) adjusting
1950 the reloc addend as necessary.
1952 This function does not have to worry about setting the reloc
1953 address or the reloc symbol index.
1955 LOCAL_SYMS is a pointer to the swapped in local symbols.
1957 LOCAL_SECTIONS is an array giving the section in the input file
1958 corresponding to the st_shndx field of each local symbol.
1960 The global hash table entry for the global symbols can be found
1961 via elf_sym_hashes (input_bfd).
1963 When generating relocatable output, this function must handle
1964 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1965 going to be the section symbol corresponding to the output
1966 section, which means that the addend must be adjusted
1970 riscv_elf_relocate_section (bfd
*output_bfd
,
1971 struct bfd_link_info
*info
,
1973 asection
*input_section
,
1975 Elf_Internal_Rela
*relocs
,
1976 Elf_Internal_Sym
*local_syms
,
1977 asection
**local_sections
)
1979 Elf_Internal_Rela
*rel
;
1980 Elf_Internal_Rela
*relend
;
1981 riscv_pcrel_relocs pcrel_relocs
;
1983 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1984 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1985 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1986 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1989 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1992 relend
= relocs
+ input_section
->reloc_count
;
1993 for (rel
= relocs
; rel
< relend
; rel
++)
1995 unsigned long r_symndx
;
1996 struct elf_link_hash_entry
*h
;
1997 Elf_Internal_Sym
*sym
;
2000 bfd_reloc_status_type r
= bfd_reloc_ok
;
2001 const char *name
= NULL
;
2002 bfd_vma off
, ie_off
;
2003 bool unresolved_reloc
, is_ie
= false;
2004 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
2005 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
2006 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2007 const char *msg
= NULL
;
2008 char *msg_buf
= NULL
;
2009 bool resolved_to_zero
;
2012 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
2015 /* This is a final link. */
2016 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
2020 unresolved_reloc
= false;
2021 if (r_symndx
< symtab_hdr
->sh_info
)
2023 sym
= local_syms
+ r_symndx
;
2024 sec
= local_sections
[r_symndx
];
2025 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2027 /* Relocate against local STT_GNU_IFUNC symbol. */
2028 if (!bfd_link_relocatable (info
)
2029 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2031 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, false);
2035 /* Set STT_GNU_IFUNC symbol value. */
2036 h
->root
.u
.def
.value
= sym
->st_value
;
2037 h
->root
.u
.def
.section
= sec
;
2042 bool warned
, ignored
;
2044 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2045 r_symndx
, symtab_hdr
, sym_hashes
,
2047 unresolved_reloc
, warned
, ignored
);
2050 /* To avoid generating warning messages about truncated
2051 relocations, set the relocation's address to be the same as
2052 the start of this section. */
2053 if (input_section
->output_section
!= NULL
)
2054 relocation
= input_section
->output_section
->vma
;
2060 if (sec
!= NULL
&& discarded_section (sec
))
2061 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2062 rel
, 1, relend
, howto
, 0, contents
);
2064 if (bfd_link_relocatable (info
))
2067 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2068 it here if it is defined in a non-shared object. */
2070 && h
->type
== STT_GNU_IFUNC
2073 asection
*plt
, *base_got
;
2075 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2077 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2078 STT_GNU_IFUNC symbol as STT_FUNC. */
2079 if (elf_section_type (input_section
) == SHT_NOTE
)
2082 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2083 sections because such sections are not SEC_ALLOC and
2084 thus ld.so will not process them. */
2085 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2090 else if (h
->plt
.offset
== (bfd_vma
) -1
2091 /* The following relocation may not need the .plt entries
2092 when all references to a STT_GNU_IFUNC symbols are done
2093 via GOT or static function pointers. */
2094 && r_type
!= R_RISCV_32
2095 && r_type
!= R_RISCV_64
2096 && r_type
!= R_RISCV_HI20
2097 && r_type
!= R_RISCV_GOT_HI20
2098 && r_type
!= R_RISCV_LO12_I
2099 && r_type
!= R_RISCV_LO12_S
)
2100 goto bad_ifunc_reloc
;
2102 /* STT_GNU_IFUNC symbol must go through PLT. */
2103 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2104 relocation
= plt
->output_section
->vma
2105 + plt
->output_offset
2112 if (rel
->r_addend
!= 0)
2114 if (h
->root
.root
.string
)
2115 name
= h
->root
.root
.string
;
2117 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2120 /* xgettext:c-format */
2121 (_("%pB: relocation %s against STT_GNU_IFUNC "
2122 "symbol `%s' has non-zero addend: %" PRId64
),
2123 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2124 bfd_set_error (bfd_error_bad_value
);
2128 /* Generate dynamic relocation only when there is a non-GOT
2129 reference in a shared object or there is no PLT. */
2130 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2131 || h
->plt
.offset
== (bfd_vma
) -1)
2133 Elf_Internal_Rela outrel
;
2136 /* Need a dynamic relocation to get the real function
2138 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2142 if (outrel
.r_offset
== (bfd_vma
) -1
2143 || outrel
.r_offset
== (bfd_vma
) -2)
2146 outrel
.r_offset
+= input_section
->output_section
->vma
2147 + input_section
->output_offset
;
2149 if (h
->dynindx
== -1
2151 || bfd_link_executable (info
))
2153 info
->callbacks
->minfo
2154 (_("Local IFUNC function `%s' in %pB\n"),
2155 h
->root
.root
.string
,
2156 h
->root
.u
.def
.section
->owner
);
2158 /* This symbol is resolved locally. */
2159 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2160 outrel
.r_addend
= h
->root
.u
.def
.value
2161 + h
->root
.u
.def
.section
->output_section
->vma
2162 + h
->root
.u
.def
.section
->output_offset
;
2166 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2167 outrel
.r_addend
= 0;
2170 /* Dynamic relocations are stored in
2171 1. .rela.ifunc section in PIC object.
2172 2. .rela.got section in dynamic executable.
2173 3. .rela.iplt section in static executable. */
2174 if (bfd_link_pic (info
))
2175 sreloc
= htab
->elf
.irelifunc
;
2176 else if (htab
->elf
.splt
!= NULL
)
2177 sreloc
= htab
->elf
.srelgot
;
2179 sreloc
= htab
->elf
.irelplt
;
2181 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2183 /* If this reloc is against an external symbol, we
2184 do not want to fiddle with the addend. Otherwise,
2185 we need to include the symbol value so that it
2186 becomes an addend for the dynamic reloc. For an
2187 internal symbol, we have updated addend. */
2192 case R_RISCV_GOT_HI20
:
2193 base_got
= htab
->elf
.sgot
;
2194 off
= h
->got
.offset
;
2196 if (base_got
== NULL
)
2199 if (off
== (bfd_vma
) -1)
2203 /* We can't use h->got.offset here to save state, or
2204 even just remember the offset, as finish_dynamic_symbol
2205 would use that as offset into .got. */
2207 if (htab
->elf
.splt
!= NULL
)
2209 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2211 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2212 base_got
= htab
->elf
.sgotplt
;
2216 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2217 off
= plt_idx
* GOT_ENTRY_SIZE
;
2218 base_got
= htab
->elf
.igotplt
;
2221 if (h
->dynindx
== -1
2225 /* This references the local definition. We must
2226 initialize this entry in the global offset table.
2227 Since the offset must always be a multiple of 8,
2228 we use the least significant bit to record
2229 whether we have initialized it already.
2231 When doing a dynamic link, we create a .rela.got
2232 relocation entry to initialize the value. This
2233 is done in the finish_dynamic_symbol routine. */
2238 bfd_put_NN (output_bfd
, relocation
,
2239 base_got
->contents
+ off
);
2240 /* Note that this is harmless for the case,
2241 as -1 | 1 still is -1. */
2247 relocation
= base_got
->output_section
->vma
2248 + base_got
->output_offset
+ off
;
2250 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2253 r
= bfd_reloc_overflow
;
2257 case R_RISCV_CALL_PLT
:
2259 case R_RISCV_LO12_I
:
2260 case R_RISCV_LO12_S
:
2263 case R_RISCV_PCREL_HI20
:
2264 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2267 r
= bfd_reloc_overflow
;
2272 if (h
->root
.root
.string
)
2273 name
= h
->root
.root
.string
;
2275 /* The entry of local ifunc is fake in global hash table,
2276 we should find the name by the original local symbol. */
2277 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2280 /* xgettext:c-format */
2281 (_("%pB: relocation %s against STT_GNU_IFUNC "
2282 "symbol `%s' isn't supported"), input_bfd
,
2284 bfd_set_error (bfd_error_bad_value
);
2291 name
= h
->root
.root
.string
;
2294 name
= (bfd_elf_string_from_elf_section
2295 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2296 if (name
== NULL
|| *name
== '\0')
2297 name
= bfd_section_name (sec
);
2300 resolved_to_zero
= (h
!= NULL
2301 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2307 case R_RISCV_TPREL_ADD
:
2309 case R_RISCV_JUMP_SLOT
:
2310 case R_RISCV_RELATIVE
:
2311 /* These require nothing of us at all. */
2315 case R_RISCV_BRANCH
:
2316 case R_RISCV_RVC_BRANCH
:
2317 case R_RISCV_RVC_LUI
:
2318 case R_RISCV_LO12_I
:
2319 case R_RISCV_LO12_S
:
2324 case R_RISCV_32_PCREL
:
2325 case R_RISCV_DELETE
:
2326 /* These require no special handling beyond perform_relocation. */
2329 case R_RISCV_GOT_HI20
:
2334 off
= h
->got
.offset
;
2335 BFD_ASSERT (off
!= (bfd_vma
) -1);
2336 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2337 pic
= bfd_link_pic (info
);
2339 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2340 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
2342 /* This is actually a static link, or it is a
2343 -Bsymbolic link and the symbol is defined
2344 locally, or the symbol was forced to be local
2345 because of a version file. We must initialize
2346 this entry in the global offset table. Since the
2347 offset must always be a multiple of the word size,
2348 we use the least significant bit to record whether
2349 we have initialized it already.
2351 When doing a dynamic link, we create a .rela.got
2352 relocation entry to initialize the value. This
2353 is done in the finish_dynamic_symbol routine. */
2358 bfd_put_NN (output_bfd
, relocation
,
2359 htab
->elf
.sgot
->contents
+ off
);
2364 unresolved_reloc
= false;
2368 BFD_ASSERT (local_got_offsets
!= NULL
2369 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2371 off
= local_got_offsets
[r_symndx
];
2373 /* The offset must always be a multiple of the word size.
2374 So, we can use the least significant bit to record
2375 whether we have already processed this entry. */
2380 if (bfd_link_pic (info
))
2383 Elf_Internal_Rela outrel
;
2385 /* We need to generate a R_RISCV_RELATIVE reloc
2386 for the dynamic linker. */
2387 s
= htab
->elf
.srelgot
;
2388 BFD_ASSERT (s
!= NULL
);
2390 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2392 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2393 outrel
.r_addend
= relocation
;
2395 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2398 bfd_put_NN (output_bfd
, relocation
,
2399 htab
->elf
.sgot
->contents
+ off
);
2400 local_got_offsets
[r_symndx
] |= 1;
2404 if (rel
->r_addend
!= 0)
2406 msg
= _("The addend isn't allowed for R_RISCV_GOT_HI20");
2407 r
= bfd_reloc_dangerous
;
2411 /* Address of got entry. */
2412 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2413 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
,
2414 relocation
, contents
,
2416 /* Update howto if relocation is changed. */
2417 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2418 ELFNN_R_TYPE (rel
->r_info
));
2420 r
= bfd_reloc_notsupported
;
2421 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2424 r
= bfd_reloc_overflow
;
2433 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2434 contents
+ rel
->r_offset
);
2435 relocation
= old_value
+ relocation
;
2445 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2446 contents
+ rel
->r_offset
);
2447 relocation
= old_value
- relocation
;
2452 case R_RISCV_CALL_PLT
:
2453 /* Handle a call to an undefined weak function. This won't be
2454 relaxed, so we have to handle it here. */
2455 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2456 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2458 /* We can use x0 as the base register. */
2459 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2460 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2461 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2462 /* Set the relocation value so that we get 0 after the pc
2463 relative adjustment. */
2464 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2469 case R_RISCV_RVC_JUMP
:
2470 /* This line has to match the check in _bfd_riscv_relax_section. */
2471 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
2473 /* Refer to the PLT entry. */
2474 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2475 unresolved_reloc
= false;
2479 case R_RISCV_TPREL_HI20
:
2480 relocation
= tpoff (info
, relocation
);
2483 case R_RISCV_TPREL_LO12_I
:
2484 case R_RISCV_TPREL_LO12_S
:
2485 relocation
= tpoff (info
, relocation
);
2488 case R_RISCV_TPREL_I
:
2489 case R_RISCV_TPREL_S
:
2490 relocation
= tpoff (info
, relocation
);
2491 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2493 /* We can use tp as the base register. */
2494 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2495 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2496 insn
|= X_TP
<< OP_SH_RS1
;
2497 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2500 r
= bfd_reloc_overflow
;
2503 case R_RISCV_GPREL_I
:
2504 case R_RISCV_GPREL_S
:
2506 bfd_vma gp
= riscv_global_pointer_value (info
);
2507 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2508 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2510 /* We can use x0 or gp as the base register. */
2511 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2512 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2515 rel
->r_addend
-= gp
;
2516 insn
|= X_GP
<< OP_SH_RS1
;
2518 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2521 r
= bfd_reloc_overflow
;
2525 case R_RISCV_PCREL_HI20
:
2526 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
, relocation
,
2528 /* Update howto if relocation is changed. */
2529 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2530 ELFNN_R_TYPE (rel
->r_info
));
2532 r
= bfd_reloc_notsupported
;
2533 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2534 relocation
+ rel
->r_addend
,
2536 r
= bfd_reloc_overflow
;
2539 case R_RISCV_PCREL_LO12_I
:
2540 case R_RISCV_PCREL_LO12_S
:
2541 /* We don't allow section symbols plus addends as the auipc address,
2542 because then riscv_relax_delete_bytes would have to search through
2543 all relocs to update these addends. This is also ambiguous, as
2544 we do allow offsets to be added to the target address, which are
2545 not to be used to find the auipc address. */
2546 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2547 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2550 msg
= _("%pcrel_lo section symbol with an addend");
2551 r
= bfd_reloc_dangerous
;
2555 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2556 input_section
, info
, howto
,
2559 r
= bfd_reloc_overflow
;
2562 case R_RISCV_TLS_DTPREL32
:
2563 case R_RISCV_TLS_DTPREL64
:
2564 relocation
= dtpoff (info
, relocation
);
2569 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2572 if ((bfd_link_pic (info
)
2574 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2575 && !resolved_to_zero
)
2576 || h
->root
.type
!= bfd_link_hash_undefweak
)
2577 && (!howto
->pc_relative
2578 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2579 || (!bfd_link_pic (info
)
2585 || h
->root
.type
== bfd_link_hash_undefweak
2586 || h
->root
.type
== bfd_link_hash_undefined
)))
2588 Elf_Internal_Rela outrel
;
2590 bool skip_static_relocation
, skip_dynamic_relocation
;
2592 /* When generating a shared object, these relocations
2593 are copied into the output file to be resolved at run
2597 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2599 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2600 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2601 outrel
.r_offset
+= sec_addr (input_section
);
2603 if (skip_dynamic_relocation
)
2604 memset (&outrel
, 0, sizeof outrel
);
2605 else if (h
!= NULL
&& h
->dynindx
!= -1
2606 && !(bfd_link_pic (info
)
2607 && SYMBOLIC_BIND (info
, h
)
2610 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2611 outrel
.r_addend
= rel
->r_addend
;
2615 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2616 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2619 sreloc
= elf_section_data (input_section
)->sreloc
;
2620 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2621 if (skip_static_relocation
)
2626 case R_RISCV_TLS_GOT_HI20
:
2630 case R_RISCV_TLS_GD_HI20
:
2633 off
= h
->got
.offset
;
2638 off
= local_got_offsets
[r_symndx
];
2639 local_got_offsets
[r_symndx
] |= 1;
2642 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2643 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2644 /* If this symbol is referenced by both GD and IE TLS, the IE
2645 reference's GOT slot follows the GD reference's slots. */
2647 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2648 ie_off
= 2 * GOT_ENTRY_SIZE
;
2654 Elf_Internal_Rela outrel
;
2656 bool need_relocs
= false;
2658 if (htab
->elf
.srelgot
== NULL
)
2664 dyn
= htab
->elf
.dynamic_sections_created
;
2665 pic
= bfd_link_pic (info
);
2667 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2668 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2672 /* The GOT entries have not been initialized yet. Do it
2673 now, and emit any relocations. */
2674 if ((bfd_link_pic (info
) || indx
!= 0)
2676 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2677 || h
->root
.type
!= bfd_link_hash_undefweak
))
2680 if (tls_type
& GOT_TLS_GD
)
2684 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2685 outrel
.r_addend
= 0;
2686 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2687 bfd_put_NN (output_bfd
, 0,
2688 htab
->elf
.sgot
->contents
+ off
);
2689 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2692 BFD_ASSERT (! unresolved_reloc
);
2693 bfd_put_NN (output_bfd
,
2694 dtpoff (info
, relocation
),
2695 (htab
->elf
.sgot
->contents
2696 + off
+ RISCV_ELF_WORD_BYTES
));
2700 bfd_put_NN (output_bfd
, 0,
2701 (htab
->elf
.sgot
->contents
2702 + off
+ RISCV_ELF_WORD_BYTES
));
2703 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2704 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2705 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2710 /* If we are not emitting relocations for a
2711 general dynamic reference, then we must be in a
2712 static link or an executable link with the
2713 symbol binding locally. Mark it as belonging
2714 to module 1, the executable. */
2715 bfd_put_NN (output_bfd
, 1,
2716 htab
->elf
.sgot
->contents
+ off
);
2717 bfd_put_NN (output_bfd
,
2718 dtpoff (info
, relocation
),
2719 (htab
->elf
.sgot
->contents
2720 + off
+ RISCV_ELF_WORD_BYTES
));
2724 if (tls_type
& GOT_TLS_IE
)
2728 bfd_put_NN (output_bfd
, 0,
2729 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2730 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2732 outrel
.r_addend
= 0;
2734 outrel
.r_addend
= tpoff (info
, relocation
);
2735 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2736 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2740 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2741 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2746 BFD_ASSERT (off
< (bfd_vma
) -2);
2747 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2748 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2751 r
= bfd_reloc_overflow
;
2752 unresolved_reloc
= false;
2756 r
= bfd_reloc_notsupported
;
2759 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2760 because such sections are not SEC_ALLOC and thus ld.so will
2761 not process them. */
2762 if (unresolved_reloc
2763 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2765 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2766 rel
->r_offset
) != (bfd_vma
) -1)
2771 case R_RISCV_RVC_JUMP
:
2772 if (asprintf (&msg_buf
,
2773 _("%%X%%P: relocation %s against `%s' can "
2774 "not be used when making a shared object; "
2775 "recompile with -fPIC\n"),
2777 h
->root
.root
.string
) == -1)
2782 if (asprintf (&msg_buf
,
2783 _("%%X%%P: unresolvable %s relocation against "
2786 h
->root
.root
.string
) == -1)
2792 r
= bfd_reloc_notsupported
;
2796 if (r
== bfd_reloc_ok
)
2797 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2798 input_bfd
, contents
);
2800 /* We should have already detected the error and set message before.
2801 If the error message isn't set since the linker runs out of memory
2802 or we don't set it before, then we should set the default message
2803 with the "internal error" string here. */
2809 case bfd_reloc_overflow
:
2810 info
->callbacks
->reloc_overflow
2811 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2812 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2815 case bfd_reloc_undefined
:
2816 info
->callbacks
->undefined_symbol
2817 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2821 case bfd_reloc_outofrange
:
2823 msg
= _("%X%P: internal error: out of range error\n");
2826 case bfd_reloc_notsupported
:
2828 msg
= _("%X%P: internal error: unsupported relocation error\n");
2831 case bfd_reloc_dangerous
:
2832 /* The error message should already be set. */
2834 msg
= _("dangerous relocation error");
2835 info
->callbacks
->reloc_dangerous
2836 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2840 msg
= _("%X%P: internal error: unknown error\n");
2844 /* Do not report error message for the dangerous relocation again. */
2845 if (msg
&& r
!= bfd_reloc_dangerous
)
2846 info
->callbacks
->einfo (msg
);
2848 /* Free the unused `msg_buf`. */
2851 /* We already reported the error via a callback, so don't try to report
2852 it again by returning false. That leads to spurious errors. */
2857 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2859 riscv_free_pcrel_relocs (&pcrel_relocs
);
2863 /* Finish up dynamic symbol handling. We set the contents of various
2864 dynamic sections here. */
2867 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2868 struct bfd_link_info
*info
,
2869 struct elf_link_hash_entry
*h
,
2870 Elf_Internal_Sym
*sym
)
2872 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2873 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2875 if (h
->plt
.offset
!= (bfd_vma
) -1)
2877 /* We've decided to create a PLT entry for this symbol. */
2879 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
2880 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2881 Elf_Internal_Rela rela
;
2882 asection
*plt
, *gotplt
, *relplt
;
2884 /* When building a static executable, use .iplt, .igot.plt and
2885 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2886 if (htab
->elf
.splt
!= NULL
)
2888 plt
= htab
->elf
.splt
;
2889 gotplt
= htab
->elf
.sgotplt
;
2890 relplt
= htab
->elf
.srelplt
;
2894 plt
= htab
->elf
.iplt
;
2895 gotplt
= htab
->elf
.igotplt
;
2896 relplt
= htab
->elf
.irelplt
;
2899 /* This symbol has an entry in the procedure linkage table. Set
2901 if ((h
->dynindx
== -1
2902 && !((h
->forced_local
|| bfd_link_executable (info
))
2904 && h
->type
== STT_GNU_IFUNC
))
2910 /* Calculate the address of the PLT header. */
2911 header_address
= sec_addr (plt
);
2913 /* Calculate the index of the entry and the offset of .got.plt entry.
2914 For static executables, we don't reserve anything. */
2915 if (plt
== htab
->elf
.splt
)
2917 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2918 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2922 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2923 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
2926 /* Calculate the address of the .got.plt entry. */
2927 got_address
= sec_addr (gotplt
) + got_offset
;
2929 /* Find out where the .plt entry should go. */
2930 loc
= plt
->contents
+ h
->plt
.offset
;
2932 /* Fill in the PLT entry itself. */
2933 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2934 header_address
+ h
->plt
.offset
,
2938 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2939 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
2941 /* Fill in the initial value of the .got.plt entry. */
2942 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
2943 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
2945 rela
.r_offset
= got_address
;
2947 if (h
->dynindx
== -1
2948 || ((bfd_link_executable (info
)
2949 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2951 && h
->type
== STT_GNU_IFUNC
))
2953 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2954 h
->root
.root
.string
,
2955 h
->root
.u
.def
.section
->owner
);
2957 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2958 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2959 asection
*sec
= h
->root
.u
.def
.section
;
2960 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2961 rela
.r_addend
= h
->root
.u
.def
.value
2962 + sec
->output_section
->vma
2963 + sec
->output_offset
;
2967 /* Fill in the entry in the .rela.plt section. */
2968 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2972 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2973 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2975 if (!h
->def_regular
)
2977 /* Mark the symbol as undefined, rather than as defined in
2978 the .plt section. Leave the value alone. */
2979 sym
->st_shndx
= SHN_UNDEF
;
2980 /* If the symbol is weak, we do need to clear the value.
2981 Otherwise, the PLT entry would provide a definition for
2982 the symbol even if the symbol wasn't defined anywhere,
2983 and so the symbol would never be NULL. */
2984 if (!h
->ref_regular_nonweak
)
2989 if (h
->got
.offset
!= (bfd_vma
) -1
2990 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2991 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2995 Elf_Internal_Rela rela
;
2996 bool use_elf_append_rela
= true;
2998 /* This symbol has an entry in the GOT. Set it up. */
3000 sgot
= htab
->elf
.sgot
;
3001 srela
= htab
->elf
.srelgot
;
3002 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
3004 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
3006 /* Handle the ifunc symbol in GOT entry. */
3008 && h
->type
== STT_GNU_IFUNC
)
3010 if (h
->plt
.offset
== (bfd_vma
) -1)
3012 /* STT_GNU_IFUNC is referenced without PLT. */
3014 if (htab
->elf
.splt
== NULL
)
3016 /* Use .rela.iplt section to store .got relocations
3017 in static executable. */
3018 srela
= htab
->elf
.irelplt
;
3020 /* Do not use riscv_elf_append_rela to add dynamic
3022 use_elf_append_rela
= false;
3025 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3027 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3028 h
->root
.root
.string
,
3029 h
->root
.u
.def
.section
->owner
);
3031 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3032 rela
.r_addend
= (h
->root
.u
.def
.value
3033 + h
->root
.u
.def
.section
->output_section
->vma
3034 + h
->root
.u
.def
.section
->output_offset
);
3038 /* Generate R_RISCV_NN. */
3039 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3040 BFD_ASSERT (h
->dynindx
!= -1);
3041 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3045 else if (bfd_link_pic (info
))
3047 /* Generate R_RISCV_NN. */
3048 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3049 BFD_ASSERT (h
->dynindx
!= -1);
3050 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3057 if (!h
->pointer_equality_needed
)
3060 /* For non-shared object, we can't use .got.plt, which
3061 contains the real function address if we need pointer
3062 equality. We load the GOT entry with the PLT entry. */
3063 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3064 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3065 + plt
->output_offset
3067 htab
->elf
.sgot
->contents
3068 + (h
->got
.offset
& ~(bfd_vma
) 1));
3072 else if (bfd_link_pic (info
)
3073 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3075 /* If this is a local symbol reference, we just want to emit
3076 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3077 or a pie link, or the symbol was forced to be local because
3078 of a version file. The entry in the global offset table will
3079 already have been initialized in the relocate_section function. */
3080 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3081 asection
*sec
= h
->root
.u
.def
.section
;
3082 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3083 rela
.r_addend
= (h
->root
.u
.def
.value
3084 + sec
->output_section
->vma
3085 + sec
->output_offset
);
3089 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3090 BFD_ASSERT (h
->dynindx
!= -1);
3091 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3095 bfd_put_NN (output_bfd
, 0,
3096 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3098 if (use_elf_append_rela
)
3099 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3102 /* Use riscv_elf_append_rela to add the dynamic relocs into
3103 .rela.iplt may cause the overwrite problems. Since we insert
3104 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3105 but the riscv_elf_append_rela adds the relocs to the place
3106 that are calculated from the reloc_index (in seqential).
3108 One solution is that add these dynamic relocs (GOT IFUNC)
3109 from the last of .rela.iplt section. */
3110 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3111 bfd_byte
*loc
= srela
->contents
3112 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3113 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3119 Elf_Internal_Rela rela
;
3122 /* This symbols needs a copy reloc. Set it up. */
3123 BFD_ASSERT (h
->dynindx
!= -1);
3125 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3126 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3128 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3129 s
= htab
->elf
.sreldynrelro
;
3131 s
= htab
->elf
.srelbss
;
3132 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3135 /* Mark some specially defined symbols as absolute. */
3136 if (h
== htab
->elf
.hdynamic
3137 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3138 sym
->st_shndx
= SHN_ABS
;
3143 /* Finish up local dynamic symbol handling. We set the contents of
3144 various dynamic sections here. */
3147 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3149 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3150 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3152 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3155 /* Finish up the dynamic sections. */
3158 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3159 bfd
*dynobj
, asection
*sdyn
)
3161 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3162 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3163 size_t dynsize
= bed
->s
->sizeof_dyn
;
3164 bfd_byte
*dyncon
, *dynconend
;
3166 dynconend
= sdyn
->contents
+ sdyn
->size
;
3167 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3169 Elf_Internal_Dyn dyn
;
3172 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3177 s
= htab
->elf
.sgotplt
;
3178 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3181 s
= htab
->elf
.srelplt
;
3182 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3185 s
= htab
->elf
.srelplt
;
3186 dyn
.d_un
.d_val
= s
->size
;
3192 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3198 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3199 struct bfd_link_info
*info
)
3203 struct riscv_elf_link_hash_table
*htab
;
3205 htab
= riscv_elf_hash_table (info
);
3206 BFD_ASSERT (htab
!= NULL
);
3207 dynobj
= htab
->elf
.dynobj
;
3209 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3211 if (elf_hash_table (info
)->dynamic_sections_created
)
3216 splt
= htab
->elf
.splt
;
3217 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3219 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3224 /* Fill in the head and tail entries in the procedure linkage table. */
3228 uint32_t plt_header
[PLT_HEADER_INSNS
];
3229 ret
= riscv_make_plt_header (output_bfd
,
3230 sec_addr (htab
->elf
.sgotplt
),
3231 sec_addr (splt
), plt_header
);
3235 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3236 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3238 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3243 if (htab
->elf
.sgotplt
)
3245 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3247 if (bfd_is_abs_section (output_section
))
3249 (*_bfd_error_handler
)
3250 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3254 if (htab
->elf
.sgotplt
->size
> 0)
3256 /* Write the first two entries in .got.plt, needed for the dynamic
3258 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3259 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3260 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3263 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3268 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3270 if (htab
->elf
.sgot
->size
> 0)
3272 /* Set the first entry in the global offset table to the address of
3273 the dynamic section. */
3274 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3275 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3278 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3281 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3282 htab_traverse (htab
->loc_hash_table
,
3283 riscv_elf_finish_local_dynamic_symbol
,
3289 /* Return address for Ith PLT stub in section PLT, for relocation REL
3290 or (bfd_vma) -1 if it should not be included. */
3293 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3294 const arelent
*rel ATTRIBUTE_UNUSED
)
3296 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3299 static enum elf_reloc_type_class
3300 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3301 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3302 const Elf_Internal_Rela
*rela
)
3304 switch (ELFNN_R_TYPE (rela
->r_info
))
3306 case R_RISCV_RELATIVE
:
3307 return reloc_class_relative
;
3308 case R_RISCV_JUMP_SLOT
:
3309 return reloc_class_plt
;
3311 return reloc_class_copy
;
3313 return reloc_class_normal
;
3317 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3321 riscv_float_abi_string (flagword flags
)
3323 switch (flags
& EF_RISCV_FLOAT_ABI
)
3325 case EF_RISCV_FLOAT_ABI_SOFT
:
3326 return "soft-float";
3328 case EF_RISCV_FLOAT_ABI_SINGLE
:
3329 return "single-float";
3331 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3332 return "double-float";
3334 case EF_RISCV_FLOAT_ABI_QUAD
:
3335 return "quad-float";
3342 /* The information of architecture elf attributes. */
3343 static riscv_subset_list_t in_subsets
;
3344 static riscv_subset_list_t out_subsets
;
3345 static riscv_subset_list_t merged_subsets
;
3347 /* Predicator for standard extension. */
3350 riscv_std_ext_p (const char *name
)
3352 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3355 /* Check if the versions are compatible. */
3358 riscv_version_mismatch (bfd
*ibfd
,
3359 struct riscv_subset_t
*in
,
3360 struct riscv_subset_t
*out
)
3362 if (in
== NULL
|| out
== NULL
)
3365 /* Since there are no version conflicts for now, we just report
3366 warning when the versions are mis-matched. */
3367 if (in
->major_version
!= out
->major_version
3368 || in
->minor_version
!= out
->minor_version
)
3370 if ((in
->major_version
== RISCV_UNKNOWN_VERSION
3371 && in
->minor_version
== RISCV_UNKNOWN_VERSION
)
3372 || (out
->major_version
== RISCV_UNKNOWN_VERSION
3373 && out
->minor_version
== RISCV_UNKNOWN_VERSION
))
3375 /* Do not report the warning when the version of input
3376 or output is RISCV_UNKNOWN_VERSION, since the extension
3377 is added implicitly. */
3381 (_("warning: %pB: mis-matched ISA version %d.%d for '%s' "
3382 "extension, the output version is %d.%d"),
3388 out
->minor_version
);
3390 /* Update the output ISA versions to the newest ones. */
3391 if ((in
->major_version
> out
->major_version
)
3392 || (in
->major_version
== out
->major_version
3393 && in
->minor_version
> out
->minor_version
))
3395 out
->major_version
= in
->major_version
;
3396 out
->minor_version
= in
->minor_version
;
3403 /* Return true if subset is 'i' or 'e'. */
3406 riscv_i_or_e_p (bfd
*ibfd
,
3408 struct riscv_subset_t
*subset
)
3410 if ((strcasecmp (subset
->name
, "e") != 0)
3411 && (strcasecmp (subset
->name
, "i") != 0))
3414 (_("error: %pB: corrupted ISA string '%s'. "
3415 "First letter should be 'i' or 'e' but got '%s'"),
3416 ibfd
, arch
, subset
->name
);
3422 /* Merge standard extensions.
3425 Return FALSE if failed to merge.
3429 `in_arch`: Raw ISA string for input object.
3430 `out_arch`: Raw ISA string for output object.
3431 `pin`: Subset list for input object.
3432 `pout`: Subset list for output object. */
3435 riscv_merge_std_ext (bfd
*ibfd
,
3436 const char *in_arch
,
3437 const char *out_arch
,
3438 struct riscv_subset_t
**pin
,
3439 struct riscv_subset_t
**pout
)
3441 const char *standard_exts
= "mafdqlcbjtpvn";
3443 struct riscv_subset_t
*in
= *pin
;
3444 struct riscv_subset_t
*out
= *pout
;
3446 /* First letter should be 'i' or 'e'. */
3447 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3450 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3453 if (strcasecmp (in
->name
, out
->name
) != 0)
3455 /* TODO: We might allow merge 'i' with 'e'. */
3457 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3458 ibfd
, in
->name
, out
->name
);
3461 else if (!riscv_version_mismatch (ibfd
, in
, out
))
3464 riscv_add_subset (&merged_subsets
,
3465 out
->name
, out
->major_version
, out
->minor_version
);
3470 /* Handle standard extension first. */
3471 for (p
= standard_exts
; *p
; ++p
)
3473 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3474 char find_ext
[2] = {*p
, '\0'};
3475 bool find_in
, find_out
;
3477 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3478 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3480 if (!find_in
&& !find_out
)
3485 && !riscv_version_mismatch (ibfd
, ext_in
, ext_out
))
3488 ext_merged
= find_out
? ext_out
: ext_in
;
3489 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3490 ext_merged
->major_version
, ext_merged
->minor_version
);
3493 /* Skip all standard extensions. */
3494 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3495 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3503 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3504 object subset list. Likewise for POUT and the output object. Return TRUE
3505 on success and FALSE when a conflict is found. */
3508 riscv_merge_multi_letter_ext (bfd
*ibfd
,
3509 riscv_subset_t
**pin
,
3510 riscv_subset_t
**pout
)
3512 riscv_subset_t
*in
= *pin
;
3513 riscv_subset_t
*out
= *pout
;
3514 riscv_subset_t
*tail
;
3520 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3524 /* `in' comes before `out', append `in' and increment. */
3525 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3531 /* `out' comes before `in', append `out' and increment. */
3532 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3533 out
->minor_version
);
3538 /* Both present, check version and increment both. */
3539 if (!riscv_version_mismatch (ibfd
, in
, out
))
3542 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3543 out
->minor_version
);
3551 /* If we're here, either `in' or `out' is running longer than
3552 the other. So, we need to append the corresponding tail. */
3553 tail
= in
? in
: out
;
3556 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3557 tail
->minor_version
);
3565 /* Merge Tag_RISCV_arch attribute. */
3568 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3570 riscv_subset_t
*in
, *out
;
3571 char *merged_arch_str
;
3573 unsigned xlen_in
, xlen_out
;
3574 merged_subsets
.head
= NULL
;
3575 merged_subsets
.tail
= NULL
;
3577 riscv_parse_subset_t riscv_rps_ld_in
=
3578 {&in_subsets
, _bfd_error_handler
, &xlen_in
, NULL
, false};
3579 riscv_parse_subset_t riscv_rps_ld_out
=
3580 {&out_subsets
, _bfd_error_handler
, &xlen_out
, NULL
, false};
3582 if (in_arch
== NULL
&& out_arch
== NULL
)
3584 if (in_arch
== NULL
&& out_arch
!= NULL
)
3586 if (in_arch
!= NULL
&& out_arch
== NULL
)
3589 /* Parse subset from ISA string. */
3590 if (!riscv_parse_subset (&riscv_rps_ld_in
, in_arch
))
3592 if (!riscv_parse_subset (&riscv_rps_ld_out
, out_arch
))
3595 /* Checking XLEN. */
3596 if (xlen_out
!= xlen_in
)
3599 (_("error: %pB: ISA string of input (%s) doesn't match "
3600 "output (%s)"), ibfd
, in_arch
, out_arch
);
3604 /* Merge subset list. */
3605 in
= in_subsets
.head
;
3606 out
= out_subsets
.head
;
3608 /* Merge standard extension. */
3609 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3612 /* Merge all non-single letter extensions with single call. */
3613 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
3616 if (xlen_in
!= xlen_out
)
3619 (_("error: %pB: XLEN of input (%u) doesn't match "
3620 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3624 if (xlen_in
!= ARCH_SIZE
)
3627 (_("error: %pB: unsupported XLEN (%u), you might be "
3628 "using wrong emulation"), ibfd
, xlen_in
);
3632 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3634 /* Release the subset lists. */
3635 riscv_release_subset_list (&in_subsets
);
3636 riscv_release_subset_list (&out_subsets
);
3637 riscv_release_subset_list (&merged_subsets
);
3639 return merged_arch_str
;
3642 /* Merge object attributes from IBFD into output_bfd of INFO.
3643 Raise an error if there are conflicting attributes. */
3646 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3648 bfd
*obfd
= info
->output_bfd
;
3649 obj_attribute
*in_attr
;
3650 obj_attribute
*out_attr
;
3652 bool priv_attrs_merged
= false;
3653 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3656 /* Skip linker created files. */
3657 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3660 /* Skip any input that doesn't have an attribute section.
3661 This enables to link object files without attribute section with
3663 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3666 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3668 /* This is the first object. Copy the attributes. */
3669 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3671 out_attr
= elf_known_obj_attributes_proc (obfd
);
3673 /* Use the Tag_null value to indicate the attributes have been
3680 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3681 out_attr
= elf_known_obj_attributes_proc (obfd
);
3683 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3687 case Tag_RISCV_arch
:
3688 if (!out_attr
[Tag_RISCV_arch
].s
)
3689 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3690 else if (in_attr
[Tag_RISCV_arch
].s
3691 && out_attr
[Tag_RISCV_arch
].s
)
3693 /* Check compatible. */
3695 riscv_merge_arch_attr_info (ibfd
,
3696 in_attr
[Tag_RISCV_arch
].s
,
3697 out_attr
[Tag_RISCV_arch
].s
);
3698 if (merged_arch
== NULL
)
3701 out_attr
[Tag_RISCV_arch
].s
= "";
3704 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3708 case Tag_RISCV_priv_spec
:
3709 case Tag_RISCV_priv_spec_minor
:
3710 case Tag_RISCV_priv_spec_revision
:
3711 /* If we have handled the privileged elf attributes, then skip it. */
3712 if (!priv_attrs_merged
)
3714 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3715 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3716 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3717 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3718 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3720 /* Get the privileged spec class from elf attributes. */
3721 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3725 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3730 /* Allow to link the object without the privileged specs. */
3731 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3733 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3734 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3735 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3737 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3738 && in_priv_spec
!= out_priv_spec
)
3741 (_("warning: %pB use privileged spec version %u.%u.%u but "
3742 "the output use version %u.%u.%u"),
3751 /* The privileged spec v1.9.1 can not be linked with others
3752 since the conflicts, so we plan to drop it in a year or
3754 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3755 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3758 (_("warning: privileged spec version 1.9.1 can not be "
3759 "linked with other spec versions"));
3762 /* Update the output privileged spec to the newest one. */
3763 if (in_priv_spec
> out_priv_spec
)
3765 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3766 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3767 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3770 priv_attrs_merged
= true;
3774 case Tag_RISCV_unaligned_access
:
3775 out_attr
[i
].i
|= in_attr
[i
].i
;
3778 case Tag_RISCV_stack_align
:
3779 if (out_attr
[i
].i
== 0)
3780 out_attr
[i
].i
= in_attr
[i
].i
;
3781 else if (in_attr
[i
].i
!= 0
3782 && out_attr
[i
].i
!= 0
3783 && out_attr
[i
].i
!= in_attr
[i
].i
)
3786 (_("error: %pB use %u-byte stack aligned but the output "
3787 "use %u-byte stack aligned"),
3788 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3794 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3797 /* If out_attr was copied from in_attr then it won't have a type yet. */
3798 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3799 out_attr
[i
].type
= in_attr
[i
].type
;
3802 /* Merge Tag_compatibility attributes and any common GNU ones. */
3803 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3806 /* Check for any attributes not known on RISC-V. */
3807 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3812 /* Merge backend specific data from an object file to the output
3813 object file when linking. */
3816 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3818 bfd
*obfd
= info
->output_bfd
;
3819 flagword new_flags
, old_flags
;
3821 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3824 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3826 (*_bfd_error_handler
)
3827 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3828 " target emulation `%s' does not match `%s'"),
3829 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3833 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3836 if (!riscv_merge_attributes (ibfd
, info
))
3839 /* Check to see if the input BFD actually contains any sections. If not,
3840 its flags may not have been initialized either, but it cannot actually
3841 cause any incompatibility. Do not short-circuit dynamic objects; their
3842 section list may be emptied by elf_link_add_object_symbols.
3844 Also check to see if there are no code sections in the input. In this
3845 case, there is no need to check for code specific flags. */
3846 if (!(ibfd
->flags
& DYNAMIC
))
3848 bool null_input_bfd
= true;
3849 bool only_data_sections
= true;
3852 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3854 null_input_bfd
= false;
3856 if ((bfd_section_flags (sec
)
3857 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3858 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3860 only_data_sections
= false;
3865 if (null_input_bfd
|| only_data_sections
)
3869 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3870 old_flags
= elf_elfheader (obfd
)->e_flags
;
3872 if (!elf_flags_init (obfd
))
3874 elf_flags_init (obfd
) = true;
3875 elf_elfheader (obfd
)->e_flags
= new_flags
;
3879 /* Disallow linking different float ABIs. */
3880 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3882 (*_bfd_error_handler
)
3883 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3884 riscv_float_abi_string (new_flags
),
3885 riscv_float_abi_string (old_flags
));
3889 /* Disallow linking RVE and non-RVE. */
3890 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3892 (*_bfd_error_handler
)
3893 (_("%pB: can't link RVE with other target"), ibfd
);
3897 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3898 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3903 bfd_set_error (bfd_error_bad_value
);
3907 /* A second format for recording PC-relative hi relocations. This stores the
3908 information required to relax them to GP-relative addresses. */
3910 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3911 struct riscv_pcgp_hi_reloc
3918 bool undefined_weak
;
3919 riscv_pcgp_hi_reloc
*next
;
3922 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3923 struct riscv_pcgp_lo_reloc
3926 riscv_pcgp_lo_reloc
*next
;
3931 riscv_pcgp_hi_reloc
*hi
;
3932 riscv_pcgp_lo_reloc
*lo
;
3933 } riscv_pcgp_relocs
;
3935 /* Initialize the pcgp reloc info in P. */
3938 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3945 /* Free the pcgp reloc info in P. */
3948 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3949 bfd
*abfd ATTRIBUTE_UNUSED
,
3950 asection
*sec ATTRIBUTE_UNUSED
)
3952 riscv_pcgp_hi_reloc
*c
;
3953 riscv_pcgp_lo_reloc
*l
;
3955 for (c
= p
->hi
; c
!= NULL
; )
3957 riscv_pcgp_hi_reloc
*next
= c
->next
;
3962 for (l
= p
->lo
; l
!= NULL
; )
3964 riscv_pcgp_lo_reloc
*next
= l
->next
;
3970 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3971 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3972 relax the corresponding lo part reloc. */
3975 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3976 bfd_vma hi_addend
, bfd_vma hi_addr
,
3977 unsigned hi_sym
, asection
*sym_sec
,
3978 bool undefined_weak
)
3980 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
3983 new->hi_sec_off
= hi_sec_off
;
3984 new->hi_addend
= hi_addend
;
3985 new->hi_addr
= hi_addr
;
3986 new->hi_sym
= hi_sym
;
3987 new->sym_sec
= sym_sec
;
3988 new->undefined_weak
= undefined_weak
;
3994 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3995 This is used by a lo part reloc to find the corresponding hi part reloc. */
3997 static riscv_pcgp_hi_reloc
*
3998 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4000 riscv_pcgp_hi_reloc
*c
;
4002 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4003 if (c
->hi_sec_off
== hi_sec_off
)
4008 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4009 This is used to record relocs that can't be relaxed. */
4012 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4014 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
4017 new->hi_sec_off
= hi_sec_off
;
4023 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4024 This is used by a hi part reloc to find the corresponding lo part reloc. */
4027 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4029 riscv_pcgp_lo_reloc
*c
;
4031 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4032 if (c
->hi_sec_off
== hi_sec_off
)
4038 riscv_update_pcgp_relocs (riscv_pcgp_relocs
*p
, asection
*deleted_sec
,
4039 bfd_vma deleted_addr
, size_t deleted_count
)
4041 /* Bytes have already been deleted and toaddr should match the old section
4042 size for our checks, so adjust it here. */
4043 bfd_vma toaddr
= deleted_sec
->size
+ deleted_count
;
4044 riscv_pcgp_lo_reloc
*l
;
4045 riscv_pcgp_hi_reloc
*h
;
4047 /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
4048 entries where they occur after the deleted bytes. */
4049 for (l
= p
->lo
; l
!= NULL
; l
= l
->next
)
4050 if (l
->hi_sec_off
> deleted_addr
4051 && l
->hi_sec_off
< toaddr
)
4052 l
->hi_sec_off
-= deleted_count
;
4054 /* Update both section offsets, and symbol values of pcrel_hi relocs where
4055 these values occur after the deleted bytes. */
4056 for (h
= p
->hi
; h
!= NULL
; h
= h
->next
)
4058 if (h
->hi_sec_off
> deleted_addr
4059 && h
->hi_sec_off
< toaddr
)
4060 h
->hi_sec_off
-= deleted_count
;
4061 if (h
->sym_sec
== deleted_sec
4062 && h
->hi_addr
> deleted_addr
4063 && h
->hi_addr
< toaddr
)
4064 h
->hi_addr
-= deleted_count
;
4068 /* Delete some bytes from a section while relaxing. */
4071 riscv_relax_delete_bytes (bfd
*abfd
,
4075 struct bfd_link_info
*link_info
,
4076 riscv_pcgp_relocs
*p
)
4078 unsigned int i
, symcount
;
4079 bfd_vma toaddr
= sec
->size
;
4080 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
4081 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4082 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4083 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4084 bfd_byte
*contents
= data
->this_hdr
.contents
;
4086 /* Actually delete the bytes. */
4088 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
4090 /* Adjust the location of all of the relocs. Note that we need not
4091 adjust the addends, since all PC-relative references must be against
4092 symbols, which we will adjust below. */
4093 for (i
= 0; i
< sec
->reloc_count
; i
++)
4094 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
4095 data
->relocs
[i
].r_offset
-= count
;
4097 /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
4098 table for which these values occur after the deleted bytes. */
4100 riscv_update_pcgp_relocs (p
, sec
, addr
, count
);
4102 /* Adjust the local symbols defined in this section. */
4103 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
4105 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
4106 if (sym
->st_shndx
== sec_shndx
)
4108 /* If the symbol is in the range of memory we just moved, we
4109 have to adjust its value. */
4110 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
4111 sym
->st_value
-= count
;
4113 /* If the symbol *spans* the bytes we just deleted (i.e. its
4114 *end* is in the moved bytes but its *start* isn't), then we
4115 must adjust its size.
4117 This test needs to use the original value of st_value, otherwise
4118 we might accidentally decrease size when deleting bytes right
4119 before the symbol. But since deleted relocs can't span across
4120 symbols, we can't have both a st_value and a st_size decrease,
4121 so it is simpler to just use an else. */
4122 else if (sym
->st_value
<= addr
4123 && sym
->st_value
+ sym
->st_size
> addr
4124 && sym
->st_value
+ sym
->st_size
<= toaddr
)
4125 sym
->st_size
-= count
;
4129 /* Now adjust the global symbols defined in this section. */
4130 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
4131 - symtab_hdr
->sh_info
);
4133 for (i
= 0; i
< symcount
; i
++)
4135 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
4137 /* The '--wrap SYMBOL' option is causing a pain when the object file,
4138 containing the definition of __wrap_SYMBOL, includes a direct
4139 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
4140 the same symbol (which is __wrap_SYMBOL), but still exist as two
4141 different symbols in 'sym_hashes', we don't want to adjust
4142 the global symbol __wrap_SYMBOL twice.
4144 The same problem occurs with symbols that are versioned_hidden, as
4145 foo becomes an alias for foo@BAR, and hence they need the same
4147 if (link_info
->wrap_hash
!= NULL
4148 || sym_hash
->versioned
!= unversioned
)
4150 struct elf_link_hash_entry
**cur_sym_hashes
;
4152 /* Loop only over the symbols which have already been checked. */
4153 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
4156 /* If the current symbol is identical to 'sym_hash', that means
4157 the symbol was already adjusted (or at least checked). */
4158 if (*cur_sym_hashes
== sym_hash
)
4161 /* Don't adjust the symbol again. */
4162 if (cur_sym_hashes
< &sym_hashes
[i
])
4166 if ((sym_hash
->root
.type
== bfd_link_hash_defined
4167 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
4168 && sym_hash
->root
.u
.def
.section
== sec
)
4170 /* As above, adjust the value if needed. */
4171 if (sym_hash
->root
.u
.def
.value
> addr
4172 && sym_hash
->root
.u
.def
.value
<= toaddr
)
4173 sym_hash
->root
.u
.def
.value
-= count
;
4175 /* As above, adjust the size if needed. */
4176 else if (sym_hash
->root
.u
.def
.value
<= addr
4177 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
4178 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
4179 sym_hash
->size
-= count
;
4186 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4187 struct bfd_link_info
*,
4188 Elf_Internal_Rela
*,
4189 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4190 riscv_pcgp_relocs
*,
4191 bool undefined_weak
);
4193 /* Relax AUIPC + JALR into JAL. */
4196 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4197 struct bfd_link_info
*link_info
,
4198 Elf_Internal_Rela
*rel
,
4200 bfd_vma max_alignment
,
4201 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4203 riscv_pcgp_relocs
*pcgp_relocs
,
4204 bool undefined_weak ATTRIBUTE_UNUSED
)
4206 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4207 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4208 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4209 bfd_vma auipc
, jalr
;
4210 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4212 /* If the call crosses section boundaries, an alignment directive could
4213 cause the PC-relative offset to later increase, so we need to add in the
4214 max alignment of any section inclusive from the call to the target.
4215 Otherwise, we only need to use the alignment of the current section. */
4216 if (VALID_JTYPE_IMM (foff
))
4218 if (sym_sec
->output_section
== sec
->output_section
4219 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4220 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4221 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4224 /* See if this function call can be shortened. */
4225 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4228 /* Shorten the function call. */
4229 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4231 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4232 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4233 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4234 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4236 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4237 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4241 /* Relax to C.J[AL] rd, addr. */
4242 r_type
= R_RISCV_RVC_JUMP
;
4243 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4246 else if (VALID_JTYPE_IMM (foff
))
4248 /* Relax to JAL rd, addr. */
4249 r_type
= R_RISCV_JAL
;
4250 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4254 /* Near zero, relax to JALR rd, x0, addr. */
4255 r_type
= R_RISCV_LO12_I
;
4256 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4259 /* Replace the R_RISCV_CALL reloc. */
4260 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4261 /* Replace the AUIPC. */
4262 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4264 /* Delete unnecessary JALR. */
4266 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4267 link_info
, pcgp_relocs
);
4270 /* Traverse all output sections and return the max alignment. */
4273 _bfd_riscv_get_max_alignment (asection
*sec
)
4275 unsigned int max_alignment_power
= 0;
4278 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4280 if (o
->alignment_power
> max_alignment_power
)
4281 max_alignment_power
= o
->alignment_power
;
4284 return (bfd_vma
) 1 << max_alignment_power
;
4287 /* Relax non-PIC global variable references to GP-relative references. */
4290 _bfd_riscv_relax_lui (bfd
*abfd
,
4293 struct bfd_link_info
*link_info
,
4294 Elf_Internal_Rela
*rel
,
4296 bfd_vma max_alignment
,
4297 bfd_vma reserve_size
,
4299 riscv_pcgp_relocs
*pcgp_relocs
,
4300 bool undefined_weak
)
4302 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4303 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4304 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4306 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4310 /* If gp and the symbol are in the same output section, which is not the
4311 abs section, then consider only that output section's alignment. */
4312 struct bfd_link_hash_entry
*h
=
4313 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4315 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4316 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4317 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4320 /* Is the reference in range of x0 or gp?
4321 Valid gp range conservatively because of alignment issue. */
4323 || (VALID_ITYPE_IMM (symval
)
4325 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4327 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4329 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4330 switch (ELFNN_R_TYPE (rel
->r_info
))
4332 case R_RISCV_LO12_I
:
4335 /* Change the RS1 to zero. */
4336 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4337 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4338 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4341 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4344 case R_RISCV_LO12_S
:
4347 /* Change the RS1 to zero. */
4348 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4349 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4350 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4353 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4357 /* We can delete the unnecessary LUI and reloc. */
4358 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4360 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4361 link_info
, pcgp_relocs
);
4368 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4369 account for this assuming page alignment at worst. In the presence of
4370 RELRO segment the linker aligns it by one page size, therefore sections
4371 after the segment can be moved more than one page. */
4374 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4375 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4376 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4377 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4378 : ELF_MAXPAGESIZE
)))
4380 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4381 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4382 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4383 if (rd
== 0 || rd
== X_SP
)
4386 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4387 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4389 /* Replace the R_RISCV_HI20 reloc. */
4390 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4393 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4394 link_info
, pcgp_relocs
);
4400 /* Relax non-PIC TLS references to TP-relative references. */
4403 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4405 asection
*sym_sec ATTRIBUTE_UNUSED
,
4406 struct bfd_link_info
*link_info
,
4407 Elf_Internal_Rela
*rel
,
4409 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4410 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4412 riscv_pcgp_relocs
*pcgp_relocs
,
4413 bool undefined_weak ATTRIBUTE_UNUSED
)
4415 /* See if this symbol is in range of tp. */
4416 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4419 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4420 switch (ELFNN_R_TYPE (rel
->r_info
))
4422 case R_RISCV_TPREL_LO12_I
:
4423 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4426 case R_RISCV_TPREL_LO12_S
:
4427 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4430 case R_RISCV_TPREL_HI20
:
4431 case R_RISCV_TPREL_ADD
:
4432 /* We can delete the unnecessary instruction and reloc. */
4433 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4435 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4443 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4444 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4447 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4449 struct bfd_link_info
*link_info
,
4450 Elf_Internal_Rela
*rel
,
4452 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4453 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4454 bool *again ATTRIBUTE_UNUSED
,
4455 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4456 bool undefined_weak ATTRIBUTE_UNUSED
)
4458 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4459 bfd_vma alignment
= 1, pos
;
4460 while (alignment
<= rel
->r_addend
)
4463 symval
-= rel
->r_addend
;
4464 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4465 bfd_vma nop_bytes
= aligned_addr
- symval
;
4467 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4468 sec
->sec_flg0
= true;
4470 /* Make sure there are enough NOPs to actually achieve the alignment. */
4471 if (rel
->r_addend
< nop_bytes
)
4474 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4475 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4476 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4477 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4478 bfd_set_error (bfd_error_bad_value
);
4482 /* Delete the reloc. */
4483 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4485 /* If the number of NOPs is already correct, there's nothing to do. */
4486 if (nop_bytes
== rel
->r_addend
)
4489 /* Write as many RISC-V NOPs as we need. */
4490 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4491 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4493 /* Write a final RVC NOP if need be. */
4494 if (nop_bytes
% 4 != 0)
4495 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4497 /* Delete the excess bytes. */
4498 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4499 rel
->r_addend
- nop_bytes
, link_info
,
4503 /* Relax PC-relative references to GP-relative references. */
4506 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4509 struct bfd_link_info
*link_info
,
4510 Elf_Internal_Rela
*rel
,
4512 bfd_vma max_alignment
,
4513 bfd_vma reserve_size
,
4514 bool *again ATTRIBUTE_UNUSED
,
4515 riscv_pcgp_relocs
*pcgp_relocs
,
4516 bool undefined_weak
)
4518 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4519 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4521 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4523 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4524 actual target address. */
4525 riscv_pcgp_hi_reloc hi_reloc
;
4526 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4527 switch (ELFNN_R_TYPE (rel
->r_info
))
4529 case R_RISCV_PCREL_LO12_I
:
4530 case R_RISCV_PCREL_LO12_S
:
4532 /* If the %lo has an addend, it isn't for the label pointing at the
4533 hi part instruction, but rather for the symbol pointed at by the
4534 hi part instruction. So we must subtract it here for the lookup.
4535 It is still used below in the final symbol address. */
4536 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4537 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4541 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4546 symval
= hi_reloc
.hi_addr
;
4547 sym_sec
= hi_reloc
.sym_sec
;
4549 /* We can not know whether the undefined weak symbol is referenced
4550 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4551 we have to record the 'undefined_weak' flag when handling the
4552 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4553 undefined_weak
= hi_reloc
.undefined_weak
;
4557 case R_RISCV_PCREL_HI20
:
4558 /* Mergeable symbols and code might later move out of range. */
4559 if (! undefined_weak
4560 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4563 /* If the cooresponding lo relocation has already been seen then it's not
4564 safe to relax this relocation. */
4565 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
4576 /* If gp and the symbol are in the same output section, which is not the
4577 abs section, then consider only that output section's alignment. */
4578 struct bfd_link_hash_entry
*h
=
4579 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4581 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4582 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4583 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4586 /* Is the reference in range of x0 or gp?
4587 Valid gp range conservatively because of alignment issue. */
4589 || (VALID_ITYPE_IMM (symval
)
4591 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4593 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4595 unsigned sym
= hi_reloc
.hi_sym
;
4596 switch (ELFNN_R_TYPE (rel
->r_info
))
4598 case R_RISCV_PCREL_LO12_I
:
4601 /* Change the RS1 to zero, and then modify the relocation
4602 type to R_RISCV_LO12_I. */
4603 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4604 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4605 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4606 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
4607 rel
->r_addend
= hi_reloc
.hi_addend
;
4611 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4612 rel
->r_addend
+= hi_reloc
.hi_addend
;
4616 case R_RISCV_PCREL_LO12_S
:
4619 /* Change the RS1 to zero, and then modify the relocation
4620 type to R_RISCV_LO12_S. */
4621 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4622 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4623 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4624 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
4625 rel
->r_addend
= hi_reloc
.hi_addend
;
4629 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4630 rel
->r_addend
+= hi_reloc
.hi_addend
;
4634 case R_RISCV_PCREL_HI20
:
4635 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
4639 ELFNN_R_SYM(rel
->r_info
),
4642 /* We can delete the unnecessary AUIPC and reloc. */
4643 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4655 /* Delete the bytes for R_RISCV_DELETE. */
4658 _bfd_riscv_relax_delete (bfd
*abfd
,
4660 asection
*sym_sec ATTRIBUTE_UNUSED
,
4661 struct bfd_link_info
*link_info
,
4662 Elf_Internal_Rela
*rel
,
4663 bfd_vma symval ATTRIBUTE_UNUSED
,
4664 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4665 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4666 bool *again ATTRIBUTE_UNUSED
,
4667 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4668 bool undefined_weak ATTRIBUTE_UNUSED
)
4670 if (!riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4673 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4677 /* Called by after_allocation to set the information of data segment
4681 bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info
*info
,
4682 int *data_segment_phase
)
4684 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4685 htab
->data_segment_phase
= data_segment_phase
;
4690 Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs.
4691 Pass 1: Deletes the bytes that PCREL relaxation in pass 0 made obsolete.
4692 Pass 2: Which cannot be disabled, handles code alignment directives. */
4695 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
4696 struct bfd_link_info
*info
,
4699 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
4700 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4701 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4702 Elf_Internal_Rela
*relocs
;
4705 bfd_vma max_alignment
, reserve_size
= 0;
4706 riscv_pcgp_relocs pcgp_relocs
;
4710 if (bfd_link_relocatable (info
)
4712 || (sec
->flags
& SEC_RELOC
) == 0
4713 || sec
->reloc_count
== 0
4714 || (info
->disable_target_specific_optimizations
4715 && info
->relax_pass
== 0)
4716 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
4717 and defined in ld/ldexp.h. */
4718 || *(htab
->data_segment_phase
) == 4)
4721 riscv_init_pcgp_relocs (&pcgp_relocs
);
4723 /* Read this BFD's relocs if we haven't done so already. */
4725 relocs
= data
->relocs
;
4726 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4727 info
->keep_memory
)))
4732 max_alignment
= htab
->max_alignment
;
4733 if (max_alignment
== (bfd_vma
) -1)
4735 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4736 htab
->max_alignment
= max_alignment
;
4740 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4742 /* Examine and consider relaxing each reloc. */
4743 for (i
= 0; i
< sec
->reloc_count
; i
++)
4746 Elf_Internal_Rela
*rel
= relocs
+ i
;
4747 relax_func_t relax_func
;
4748 int type
= ELFNN_R_TYPE (rel
->r_info
);
4751 bool undefined_weak
= false;
4754 if (info
->relax_pass
== 0)
4756 if (type
== R_RISCV_CALL
4757 || type
== R_RISCV_CALL_PLT
)
4758 relax_func
= _bfd_riscv_relax_call
;
4759 else if (type
== R_RISCV_HI20
4760 || type
== R_RISCV_LO12_I
4761 || type
== R_RISCV_LO12_S
)
4762 relax_func
= _bfd_riscv_relax_lui
;
4763 else if (type
== R_RISCV_TPREL_HI20
4764 || type
== R_RISCV_TPREL_ADD
4765 || type
== R_RISCV_TPREL_LO12_I
4766 || type
== R_RISCV_TPREL_LO12_S
)
4767 relax_func
= _bfd_riscv_relax_tls_le
;
4768 else if (!bfd_link_pic (info
)
4769 && (type
== R_RISCV_PCREL_HI20
4770 || type
== R_RISCV_PCREL_LO12_I
4771 || type
== R_RISCV_PCREL_LO12_S
))
4772 relax_func
= _bfd_riscv_relax_pc
;
4776 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4777 if (i
== sec
->reloc_count
- 1
4778 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4779 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4782 /* Skip over the R_RISCV_RELAX. */
4785 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4786 relax_func
= _bfd_riscv_relax_delete
;
4787 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4788 relax_func
= _bfd_riscv_relax_align
;
4792 data
->relocs
= relocs
;
4794 /* Read this BFD's contents if we haven't done so already. */
4795 if (!data
->this_hdr
.contents
4796 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4799 /* Read this BFD's symbols if we haven't done so already. */
4800 if (symtab_hdr
->sh_info
!= 0
4801 && !symtab_hdr
->contents
4802 && !(symtab_hdr
->contents
=
4803 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4804 symtab_hdr
->sh_info
,
4805 0, NULL
, NULL
, NULL
)))
4808 /* Get the value of the symbol referred to by the reloc. */
4809 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4811 /* A local symbol. */
4812 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4813 + ELFNN_R_SYM (rel
->r_info
));
4814 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4815 ? 0 : isym
->st_size
- rel
->r_addend
;
4817 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4818 a fake global symbol entry for this, so deal with the local ifunc
4820 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
4823 if (isym
->st_shndx
== SHN_UNDEF
)
4824 sym_sec
= sec
, symval
= rel
->r_offset
;
4827 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4828 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4830 /* The purpose of this code is unknown. It breaks linker scripts
4831 for embedded development that place sections at address zero.
4832 This code is believed to be unnecessary. Disabling it but not
4833 yet removing it, in case something breaks. */
4834 if (sec_addr (sym_sec
) == 0)
4837 symval
= isym
->st_value
;
4839 symtype
= ELF_ST_TYPE (isym
->st_info
);
4844 struct elf_link_hash_entry
*h
;
4846 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4847 h
= elf_sym_hashes (abfd
)[indx
];
4849 while (h
->root
.type
== bfd_link_hash_indirect
4850 || h
->root
.type
== bfd_link_hash_warning
)
4851 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4853 /* Disable the relaxation for ifunc. */
4854 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
4857 if (h
->root
.type
== bfd_link_hash_undefweak
4858 && (relax_func
== _bfd_riscv_relax_lui
4859 || relax_func
== _bfd_riscv_relax_pc
))
4861 /* For the lui and auipc relaxations, since the symbol
4862 value of an undefined weak symbol is always be zero,
4863 we can optimize the patterns into a single LI/MV/ADDI
4866 Note that, creating shared libraries and pie output may
4867 break the rule above. Fortunately, since we do not relax
4868 pc relocs when creating shared libraries and pie output,
4869 and the absolute address access for R_RISCV_HI20 isn't
4870 allowed when "-fPIC" is set, the problem of creating shared
4871 libraries can not happen currently. Once we support the
4872 auipc relaxations when creating shared libraries, then we will
4873 need the more rigorous checking for this optimization. */
4874 undefined_weak
= true;
4877 /* This line has to match the check in riscv_elf_relocate_section
4878 in the R_RISCV_CALL[_PLT] case. */
4879 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4881 sym_sec
= htab
->elf
.splt
;
4882 symval
= h
->plt
.offset
;
4884 else if (undefined_weak
)
4887 sym_sec
= bfd_und_section_ptr
;
4889 else if ((h
->root
.type
== bfd_link_hash_defined
4890 || h
->root
.type
== bfd_link_hash_defweak
)
4891 && h
->root
.u
.def
.section
!= NULL
4892 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4894 symval
= h
->root
.u
.def
.value
;
4895 sym_sec
= h
->root
.u
.def
.section
;
4900 if (h
->type
!= STT_FUNC
)
4902 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4906 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4907 && (sym_sec
->flags
& SEC_MERGE
))
4909 /* At this stage in linking, no SEC_MERGE symbol has been
4910 adjusted, so all references to such symbols need to be
4911 passed through _bfd_merged_section_offset. (Later, in
4912 relocate_section, all SEC_MERGE symbols *except* for
4913 section symbols have been adjusted.)
4915 gas may reduce relocations against symbols in SEC_MERGE
4916 sections to a relocation against the section symbol when
4917 the original addend was zero. When the reloc is against
4918 a section symbol we should include the addend in the
4919 offset passed to _bfd_merged_section_offset, since the
4920 location of interest is the original symbol. On the
4921 other hand, an access to "sym+addend" where "sym" is not
4922 a section symbol should not include the addend; Such an
4923 access is presumed to be an offset from "sym"; The
4924 location of interest is just "sym". */
4925 if (symtype
== STT_SECTION
)
4926 symval
+= rel
->r_addend
;
4928 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4929 elf_section_data (sym_sec
)->sec_info
,
4932 if (symtype
!= STT_SECTION
)
4933 symval
+= rel
->r_addend
;
4936 symval
+= rel
->r_addend
;
4938 symval
+= sec_addr (sym_sec
);
4940 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4941 max_alignment
, reserve_size
, again
,
4942 &pcgp_relocs
, undefined_weak
))
4949 if (relocs
!= data
->relocs
)
4951 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
4957 # define PRSTATUS_SIZE 204
4958 # define PRSTATUS_OFFSET_PR_CURSIG 12
4959 # define PRSTATUS_OFFSET_PR_PID 24
4960 # define PRSTATUS_OFFSET_PR_REG 72
4961 # define ELF_GREGSET_T_SIZE 128
4962 # define PRPSINFO_SIZE 128
4963 # define PRPSINFO_OFFSET_PR_PID 16
4964 # define PRPSINFO_OFFSET_PR_FNAME 32
4965 # define PRPSINFO_OFFSET_PR_PSARGS 48
4966 # define PRPSINFO_PR_FNAME_LENGTH 16
4967 # define PRPSINFO_PR_PSARGS_LENGTH 80
4969 # define PRSTATUS_SIZE 376
4970 # define PRSTATUS_OFFSET_PR_CURSIG 12
4971 # define PRSTATUS_OFFSET_PR_PID 32
4972 # define PRSTATUS_OFFSET_PR_REG 112
4973 # define ELF_GREGSET_T_SIZE 256
4974 # define PRPSINFO_SIZE 136
4975 # define PRPSINFO_OFFSET_PR_PID 24
4976 # define PRPSINFO_OFFSET_PR_FNAME 40
4977 # define PRPSINFO_OFFSET_PR_PSARGS 56
4978 # define PRPSINFO_PR_FNAME_LENGTH 16
4979 # define PRPSINFO_PR_PSARGS_LENGTH 80
4982 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
4983 before the generic code in elf.c. By checking the compiler defines we
4984 only perform any action here if the generic code would otherwise not be
4985 able to help us. The intention is that bare metal core dumps (where the
4986 prstatus_t and/or prpsinfo_t might not be available) will use this code,
4987 while non bare metal tools will use the generic elf code. */
4990 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
4991 char *buf ATTRIBUTE_UNUSED
,
4992 int *bufsiz ATTRIBUTE_UNUSED
,
4993 int note_type ATTRIBUTE_UNUSED
, ...)
5000 #if !defined (HAVE_PRPSINFO_T)
5003 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
5006 va_start (ap
, note_type
);
5007 memset (data
, 0, sizeof (data
));
5008 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
5009 PRPSINFO_PR_FNAME_LENGTH
);
5010 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5012 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5013 -Wstringop-truncation:
5014 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5016 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
5018 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
5019 PRPSINFO_PR_PSARGS_LENGTH
);
5020 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5024 return elfcore_write_note (abfd
, buf
, bufsiz
,
5025 "CORE", note_type
, data
, sizeof (data
));
5027 #endif /* !HAVE_PRPSINFO_T */
5029 #if !defined (HAVE_PRSTATUS_T)
5032 char data
[PRSTATUS_SIZE
];
5038 va_start (ap
, note_type
);
5039 memset (data
, 0, sizeof(data
));
5040 pid
= va_arg (ap
, long);
5041 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
5042 cursig
= va_arg (ap
, int);
5043 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
5044 greg
= va_arg (ap
, const void *);
5045 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
5046 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5048 return elfcore_write_note (abfd
, buf
, bufsiz
,
5049 "CORE", note_type
, data
, sizeof (data
));
5051 #endif /* !HAVE_PRSTATUS_T */
5055 /* Support for core dump NOTE sections. */
5058 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5060 switch (note
->descsz
)
5065 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5067 elf_tdata (abfd
)->core
->signal
5068 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5071 elf_tdata (abfd
)->core
->lwpid
5072 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5076 /* Make a ".reg/999" section. */
5077 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5078 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5082 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5084 switch (note
->descsz
)
5089 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5091 elf_tdata (abfd
)->core
->pid
5092 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5095 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5096 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5097 PRPSINFO_PR_FNAME_LENGTH
);
5100 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5101 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5102 PRPSINFO_PR_PSARGS_LENGTH
);
5106 /* Note that for some reason, a spurious space is tacked
5107 onto the end of the args in some (at least one anyway)
5108 implementations, so strip it off if it exists. */
5111 char *command
= elf_tdata (abfd
)->core
->command
;
5112 int n
= strlen (command
);
5114 if (0 < n
&& command
[n
- 1] == ' ')
5115 command
[n
- 1] = '\0';
5121 /* Set the right mach type. */
5124 riscv_elf_object_p (bfd
*abfd
)
5126 /* There are only two mach types in RISCV currently. */
5127 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5128 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5129 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5131 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5136 /* Determine whether an object attribute tag takes an integer, a
5140 riscv_elf_obj_attrs_arg_type (int tag
)
5142 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5145 /* Do not choose mapping symbols as a function name. */
5147 static bfd_size_type
5148 riscv_maybe_function_sym (const asymbol
*sym
,
5152 if (sym
->flags
& BSF_LOCAL
5153 && riscv_elf_is_mapping_symbols (sym
->name
))
5156 return _bfd_elf_maybe_function_sym (sym
, sec
, code_off
);
5159 /* Treat the following cases as target special symbols, they are
5163 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5165 /* PR27584, local and empty symbols. Since they are usually
5166 generated for pcrel relocations. */
5167 return (!strcmp (sym
->name
, "")
5168 || _bfd_elf_is_local_label_name (abfd
, sym
->name
)
5169 /* PR27916, mapping symbols. */
5170 || riscv_elf_is_mapping_symbols (sym
->name
));
5174 riscv_elf_additional_program_headers (bfd
*abfd
,
5175 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5179 /* See if we need a PT_RISCV_ATTRIBUTES segment. */
5180 if (bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
))
5187 riscv_elf_modify_segment_map (bfd
*abfd
,
5188 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5191 struct elf_segment_map
*m
, **pm
;
5194 /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5196 s
= bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
);
5199 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5200 if (m
->p_type
== PT_RISCV_ATTRIBUTES
)
5202 /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5207 m
= bfd_zalloc (abfd
, amt
);
5211 m
->p_type
= PT_RISCV_ATTRIBUTES
;
5215 /* We want to put it after the PHDR and INTERP segments. */
5216 pm
= &elf_seg_map (abfd
);
5218 && ((*pm
)->p_type
== PT_PHDR
5219 || (*pm
)->p_type
== PT_INTERP
))
5230 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5231 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5232 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5233 #define TARGET_BIG_NAME "elfNN-bigriscv"
5235 #define elf_backend_reloc_type_class riscv_reloc_type_class
5237 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5238 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5239 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5240 #define bfd_elfNN_bfd_merge_private_bfd_data \
5241 _bfd_riscv_elf_merge_private_bfd_data
5242 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5244 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5245 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5246 #define elf_backend_check_relocs riscv_elf_check_relocs
5247 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5248 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
5249 #define elf_backend_relocate_section riscv_elf_relocate_section
5250 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5251 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5252 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
5253 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5254 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5255 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5256 #define elf_backend_object_p riscv_elf_object_p
5257 #define elf_backend_write_core_note riscv_write_core_note
5258 #define elf_backend_maybe_function_sym riscv_maybe_function_sym
5259 #define elf_info_to_howto_rel NULL
5260 #define elf_info_to_howto riscv_info_to_howto_rela
5261 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5262 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5263 #define elf_backend_additional_program_headers \
5264 riscv_elf_additional_program_headers
5265 #define elf_backend_modify_segment_map riscv_elf_modify_segment_map
5267 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5269 #define elf_backend_can_gc_sections 1
5270 #define elf_backend_can_refcount 1
5271 #define elf_backend_want_got_plt 1
5272 #define elf_backend_plt_readonly 1
5273 #define elf_backend_plt_alignment 4
5274 #define elf_backend_want_plt_sym 1
5275 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5276 #define elf_backend_want_dynrelro 1
5277 #define elf_backend_rela_normal 1
5278 #define elf_backend_default_execstack 0
5280 #undef elf_backend_obj_attrs_vendor
5281 #define elf_backend_obj_attrs_vendor "riscv"
5282 #undef elf_backend_obj_attrs_arg_type
5283 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5284 #undef elf_backend_obj_attrs_section_type
5285 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5286 #undef elf_backend_obj_attrs_section
5287 #define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
5289 #include "elfNN-target.h"